My Project
slgh_compile.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 #include "sleighbase.hh"
17 #include "pcodecompile.hh"
18 #include "filemanage.hh"
19 #include <iostream>
20 #include <sstream>
21 
22 // High-level control of the sleigh compilation process
23 
24 struct RtlPair {
25  ConstructTpl *section; // A p-code section
26  SymbolScope *scope; // and its associated symbol scope
27  RtlPair(void) { section = (ConstructTpl *)0; scope = (SymbolScope *)0; }
28  RtlPair(ConstructTpl *sec,SymbolScope *sc) { section = sec; scope = sc; }
29 };
30 
32  int4 nextindex;
33  RtlPair main;
34  vector<RtlPair> named;
35 public:
37  ConstructTpl *getMainSection(void) const { return main.section; }
38  ConstructTpl *getNamedSection(int4 index) const { return named[index].section; }
39  RtlPair getMainPair(void) const { return main; }
40  RtlPair getNamedPair(int4 i) const { return named[i]; }
41  void setNextIndex(int4 i) { nextindex = i; }
42  int4 getMaxId(void) const { return named.size(); }
43  void append(ConstructTpl *rtl,SymbolScope *scope);
44 };
45 
46 struct SpaceQuality { // Qualities of an address space
47  enum { // Class of space
48  ramtype,
49  registertype
50  };
51  string name;
52  uint4 type;
53  uint4 size;
54  uint4 wordsize; // number of bytes in unit of the space
55  bool isdefault;
56  SpaceQuality(const string &nm);
57 };
58 
59 struct FieldQuality {
60  string name;
61  uint4 low,high;
62  bool signext;
63  bool flow;
64  bool hex;
65  FieldQuality(string *nm,uintb *l,uintb *h);
66 };
67 
68 class WithBlock {
69  SubtableSymbol *ss;
70  PatternEquation *pateq;
71  vector<ContextChange *> contvec;
72 public:
73  WithBlock(void) { pateq = (PatternEquation *)0; }
74  void set(SubtableSymbol *s, PatternEquation *pq, vector<ContextChange *> *cvec);
75  ~WithBlock(void);
76  static PatternEquation *collectAndPrependPattern(const list<WithBlock> &stack, PatternEquation *pateq);
77  static vector<ContextChange *> *collectAndPrependContext(const list<WithBlock> &stack, vector<ContextChange *> *contvec);
78  static SubtableSymbol *getCurrentSubtable(const list<WithBlock> &stack);
79 };
80 
81 class SleighCompile;
82 
84  struct OptimizeRecord {
85  int4 writeop;
86  int4 readop;
87  int4 inslot;
88  int4 writecount;
89  int4 readcount;
90  int4 writesection;
91  int4 readsection;
92  int4 opttype;
93  OptimizeRecord(void) {
94  writeop = -1; readop = -1; inslot=-1; writecount=0; readcount=0; writesection=-2; readsection=-2; opttype=-1; }
95  };
96  SleighCompile *compiler;
97  int4 unnecessarypcode;
98  int4 readnowrite;
99  int4 writenoread;
100  bool printextwarning;
101  bool printdeadwarning;
102  SubtableSymbol *root_symbol;
103  vector<SubtableSymbol *> postorder;
104  map<SubtableSymbol *,int4> sizemap; // Sizes associated with tables
105  OperandSymbol *getOperandSymbol(int4 slot,OpTpl *op,Constructor *ct);
106  void printOpName(ostream &s,OpTpl *op);
107  void printOpError(OpTpl *op,Constructor *ct,int4 err1,int4 err2,const string &message);
108  int4 recoverSize(const ConstTpl &sizeconst,Constructor *ct);
109  bool checkOpMisuse(OpTpl *op,Constructor *ct);
110  bool sizeRestriction(OpTpl *op,Constructor *ct);
111  bool checkConstructorSection(Constructor *ct,ConstructTpl *cttpl);
112  bool checkVarnodeTruncation(Constructor *ct,int4 slot,OpTpl *op,VarnodeTpl *vn,bool isbigendian);
113  bool checkSectionTruncations(Constructor *ct,ConstructTpl *cttpl,bool isbigendian);
114  bool checkSubtable(SubtableSymbol *sym);
115  void dealWithUnnecessaryExt(OpTpl *op,Constructor *ct);
116  void dealWithUnnecessaryTrunc(OpTpl *op,Constructor *ct);
117  void setPostOrder(SubtableSymbol *root); // Establish table ordering
118 
119  // Optimization routines
120  static void examineVn(map<uintb,OptimizeRecord> &recs,const VarnodeTpl *vn,uint4 i,int4 inslot,int4 secnum);
121  static bool possibleIntersection(const VarnodeTpl *vn1,const VarnodeTpl *vn2);
122  bool readWriteInterference(const VarnodeTpl *vn,const OpTpl *op,bool checkread) const;
123  void optimizeGather1(Constructor *ct,map<uintb,OptimizeRecord> &recs,int4 secnum) const;
124  void optimizeGather2(Constructor *ct,map<uintb,OptimizeRecord> &recs,int4 secnum) const;
125  OptimizeRecord *findValidRule(Constructor *ct,map<uintb,OptimizeRecord> &recs) const;
126  void applyOptimization(Constructor *ct,const OptimizeRecord &rec);
127  void checkUnusedTemps(Constructor *ct,const map<uintb,OptimizeRecord> &recs);
128  void optimize(Constructor *ct);
129 public:
130  ConsistencyChecker(SleighCompile *sleigh, SubtableSymbol *rt,bool unnecessary,bool warndead);
131  bool test(void);
132  bool testTruncations(bool isbigendian);
133  void optimizeAll(void);
134  int4 getNumUnnecessaryPcode(void) const { return unnecessarypcode; }
135  int4 getNumReadNoWrite(void) const { return readnowrite; }
136  int4 getNumWriteNoRead(void) const { return writenoread; }
137 };
138 
139 struct FieldContext {
140  VarnodeSymbol *sym;
141  FieldQuality *qual;
142  bool operator<(const FieldContext &op2) const;
143  FieldContext(VarnodeSymbol *s,FieldQuality *q) { sym=s; qual=q; }
144 };
145 
146 class MacroBuilder : public PcodeBuilder {
147  SleighCompile *slgh;
148  bool haserror;
149  vector<OpTpl *> &outvec;
150  vector<HandleTpl *> params;
151  bool transferOp(OpTpl *op,vector<HandleTpl *> &params);
152  virtual void dump( OpTpl *op );
153  void free(void);
154  void reportError(const Location* loc, const string &val);
155 public:
156  MacroBuilder(SleighCompile *sl,vector<OpTpl *> &ovec,uint4 lbcnt) : PcodeBuilder(lbcnt),outvec(ovec) {
157  slgh = sl; haserror = false; }
158  void setMacroOp(OpTpl *macroop);
159  bool hasError(void) const { return haserror; }
160  virtual ~MacroBuilder(void) { free(); }
161  virtual void appendBuild(OpTpl *bld,int4 secnum) { dump(bld); }
162  virtual void delaySlot(OpTpl *op) { dump(op); }
163  virtual void setLabel(OpTpl *op);
164  virtual void appendCrossBuild(OpTpl *bld,int4 secnum) { dump(bld); }
165 };
166 
167 class SleighPcode : public PcodeCompile {
168  SleighCompile *compiler;
169  virtual uintb allocateTemp(void);
170  virtual const Location *getLocation(SleighSymbol *sym) const;
171  virtual void reportError(const Location* loc, const string &msg);
172  virtual void reportWarning(const Location* loc, const string &msg);
173  virtual void addSymbol(SleighSymbol *sym);
174 public:
175  SleighPcode(void) : PcodeCompile() { compiler = (SleighCompile *)0; }
176  void setCompiler(SleighCompile *comp) { compiler = comp; }
177 };
178 
179 class SleighCompile : public SleighBase {
180  friend class SleighPcode;
181 public:
182  SleighPcode pcode;
183 private:
184  map<string,string> preproc_defines; // Defines for the preprocessor
185  vector<FieldContext> contexttable;
186  vector<ConstructTpl *> macrotable;
187  vector<Token *> tokentable;
188  vector<SubtableSymbol *> tables;
189  vector<SectionSymbol *> sections;
190  list<WithBlock> withstack;
191  Constructor *curct; // Current constructor being defined
192  MacroSymbol *curmacro; // Current macro being defined
193  bool contextlock; // If the context layout has been established yet
194  vector<string> relpath; // Relative path (to cwd) for each filename
195  vector<string> filename; // Stack of current files being parsed
196  vector<int4> lineno; // Current line number for each file in stack
197  map<Constructor *, Location> ctorLocationMap; // Map constructor to its defining parse location
198  map<SleighSymbol *, Location> symbolLocationMap; // Map symbol to its defining parse location
199  int4 userop_count; // Number of userops defined
200  bool warnunnecessarypcode; // True if we warn of unnecessary ZEXT or SEXT
201  bool warndeadtemps; // True if we warn of temporaries that are written but not read
202  bool lenientconflicterrors; // True if we ignore most pattern conflict errors
203  bool warnalllocalcollisions; // True if local export collisions generate individual warnings
204  bool warnallnops; // True if pcode NOPs generate individual warnings
205  vector<string> noplist; // List of individual NOP warnings
206  mutable Location currentLocCache; // Location for (last) request of current location
207  int4 errors;
208 
209  const Location* getCurrentLocation(void) const;
210  void predefinedSymbols(void);
211  int4 calcContextVarLayout(int4 start,int4 sz,int4 numbits);
212  void buildDecisionTrees(void);
213  void buildPatterns(void);
214  void checkConsistency(void);
215  static int4 findCollision(map<uintb,int4> &local2Operand,const vector<uintb> &locals,int operand);
216  bool checkLocalExports(Constructor *ct);
217  void checkLocalCollisions(void);
218  void checkNops(void);
219  string checkSymbols(SymbolScope *scope);
220  void addSymbol(SleighSymbol *sym);
221  SleighSymbol *dedupSymbolList(vector<SleighSymbol *> *symlist);
222  bool expandMacros(ConstructTpl *ctpl,const vector<ConstructTpl *> &macrotable);
223  bool finalizeSections(Constructor *big,SectionVector *vec);
224  static void shiftUniqueVn(VarnodeTpl *vn,int4 sa);
225  static void shiftUniqueOp(OpTpl *op,int4 sa);
226  static void shiftUniqueHandle(HandleTpl *hand,int4 sa);
227  static void shiftUniqueConstruct(ConstructTpl *tpl,int4 sa);
228  void checkUniqueAllocation(void);
229 public:
230  SleighCompile(void);
231  const Location *getLocation(Constructor* ctor) const;
232  const Location *getLocation(SleighSymbol *sym) const;
233  string formatStatusMessage(const Location* loc, const string &msg);
234  void reportError(const string &msg);
235  void reportError(const Location *loc, const string &msg);
236  void reportWarning(const string &msg);
237  void reportWarning(const Location *loc, const string &msg);
238  int4 numErrors(void) const { return errors; }
239  void reportInfo(const string &msg);
240  void reportInfo(const Location *loc, const string &msg);
241 
242 
243  uintb getUniqueAddr(void);
244  void setUnnecessaryPcodeWarning(bool val) { warnunnecessarypcode = val; }
245  void setDeadTempWarning(bool val) { warndeadtemps = val; }
246  void setEnforceLocalKeyWord(bool val) { pcode.setEnforceLocalKey(val); }
247  void setLenientConflict(bool val) { lenientconflicterrors = val; }
248  void setLocalCollisionWarning(bool val) { warnalllocalcollisions = val; }
249  void setAllNopWarning(bool val) { warnallnops = val; }
250  void process(void);
251 
252  // Lexer functions
253  void calcContextLayout(void);
254  string grabCurrentFilePath(void) const;
255  void parseFromNewFile(const string &fname);
256  void parsePreprocMacro(void);
257  void parseFileFinished(void);
258  void nextLine(void) { lineno.back() += 1; }
259  bool getPreprocValue(const string &nm,string &res) const;
260  void setPreprocValue(const string &nm,const string &value);
261  bool undefinePreprocValue(const string &nm);
262 
263  // Parser functions
264  TokenSymbol *defineToken(string *name,uintb *sz);
265  void addTokenField(TokenSymbol *sym,FieldQuality *qual);
266  bool addContextField(VarnodeSymbol *sym,FieldQuality *qual);
267  void newSpace(SpaceQuality *qual);
268  SectionSymbol *newSectionSymbol(const string &nm);
269  void setEndian(int4 end);
270  void setAlignment(int4 val) { alignment = val; }
271  void defineVarnodes(SpaceSymbol *spacesym,uintb *off,uintb *size,vector<string> *names);
272  void defineBitrange(string *name,VarnodeSymbol *sym,uint4 bitoffset,uint4 numb);
273  void addUserOp(vector<string> *names);
274  void attachValues(vector<SleighSymbol *> *symlist,vector<intb> *numlist);
275  void attachNames(vector<SleighSymbol *> *symlist,vector<string> *names);
276  void attachVarnodes(vector<SleighSymbol *> *symlist,vector<SleighSymbol *> *varlist);
277  SubtableSymbol *newTable(string *nm);
278  void newOperand(Constructor *ct,string *nm);
279  VarnodeTpl *addressOf(VarnodeTpl *var,uint4 size);
280  PatternEquation *constrainOperand(OperandSymbol *sym,PatternExpression *patexp);
281  void defineOperand(OperandSymbol *sym,PatternExpression *patexp);
282  PatternEquation *defineInvisibleOperand(TripleSymbol *sym);
283  void selfDefine(OperandSymbol *sym);
284  ConstructTpl *setResultVarnode(ConstructTpl *ct,VarnodeTpl *vn);
285  ConstructTpl *setResultStarVarnode(ConstructTpl *ct,StarQuality *star,VarnodeTpl *vn);
286  bool contextMod(vector<ContextChange *> *vec,ContextSymbol *sym,PatternExpression *pe);
287  void contextSet(vector<ContextChange *> *vec,TripleSymbol *sym,ContextSymbol *cvar);
288  MacroSymbol *createMacro(string *name,vector<string> *param);
289  void compareMacroParams(MacroSymbol *sym,const vector<ExprTree *> &param);
290  vector<OpTpl *> *createMacroUse(MacroSymbol *sym,vector<ExprTree *> *param);
291  SectionVector *standaloneSection(ConstructTpl *main);
292  SectionVector *firstNamedSection(ConstructTpl *main,SectionSymbol *sym);
293  SectionVector *nextNamedSection(SectionVector *vec,ConstructTpl *section,SectionSymbol *sym);
294  SectionVector *finalNamedSection(SectionVector *vec,ConstructTpl *section);
295  vector<OpTpl *> *createCrossBuild(VarnodeTpl *addr,SectionSymbol *sym);
296  Constructor *createConstructor(SubtableSymbol *sym);
297  bool isInRoot(Constructor *ct) const { return (root == ct->getParent()); }
298  void resetConstructors(void);
299  void pushWith(SubtableSymbol *ss,PatternEquation *pateq,vector<ContextChange *> *contvec);
300  void popWith(void);
301  void buildConstructor(Constructor *big,PatternEquation *pateq,vector<ContextChange *> *contvec,SectionVector *vec);
302  void buildMacro(MacroSymbol *sym,ConstructTpl *rtl);
303  void recordNop(void);
304 
305  // Virtual functions (not used by the compiler)
306  virtual void initialize(DocumentStorage &store) {}
307  virtual int4 instructionLength(const Address &baseaddr) const { return 0; }
308  virtual int4 oneInstruction(PcodeEmit &emit,const Address &baseaddr) const { return 0; }
309  virtual int4 printAssembly(AssemblyEmit &emit,const Address &baseaddr) const { return 0; }
310 };
311 
312 extern SleighCompile *slgh;
313 extern int yydebug;
A container for parsed XML documents.
Definition: xml.hh:249
Definition: slghsymbol.hh:146
Definition: semantics.hh:192
virtual int4 oneInstruction(PcodeEmit &emit, const Address &baseaddr) const
Transform a single machine instruction into pcode.
Definition: slgh_compile.hh:308
Definition: semantics.hh:137
Abstract class for emitting pcode to an application.
Definition: translate.hh:76
Definition: slghpatexpress.hh:56
Definition: slgh_compile.hh:31
Definition: slgh_compile.hh:146
Common core of classes that read or write SLEIGH specification files natively.
Definition: sleighbase.hh:29
Definition: slgh_compile.hh:139
Definition: pcodecompile.hh:32
Definition: slghsymbol.hh:245
Definition: slgh_compile.hh:59
Definition: slghsymbol.hh:54
Definition: slghsymbol.hh:116
virtual int4 printAssembly(AssemblyEmit &emit, const Address &baseaddr) const
Disassemble a single machine instruction.
Definition: slgh_compile.hh:309
A low-level machine address for labelling bytes and data.
Definition: address.hh:46
Definition: slghsymbol.hh:279
Definition: slghsymbol.hh:23
Definition: slgh_compile.hh:24
Base class for applications that process SLEIGH format specifications.
Definition: slgh_compile.hh:83
Definition: semantics.hh:107
Definition: slgh_compile.hh:167
Definition: semantics.hh:161
Definition: slghsymbol.hh:107
Definition: slgh_compile.hh:46
Definition: semantics.hh:31
Definition: pcodecompile.hh:55
virtual int4 instructionLength(const Address &baseaddr) const
Get the length of a machine instruction.
Definition: slgh_compile.hh:307
Definition: slghsymbol.hh:313
Definition: slgh_compile.hh:68
virtual void initialize(DocumentStorage &store)
Initialize the translator given XML configuration documents.
Definition: slgh_compile.hh:306
Definition: slgh_compile.hh:179
Definition: slghsymbol.hh:99
Abstract class for emitting disassembly to an application.
Definition: translate.hh:118
Definition: pcodecompile.hh:21
Definition: slghsymbol.hh:592
Definition: slghsymbol.hh:561
Definition: slghsymbol.hh:466
Definition: semantics.hh:77
Definition: slghpatexpress.hh:336