Ghidra Decompiler Analysis Engine
pcodecompile.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 __PCODE_COMPILE__
17 #define __PCODE_COMPILE__
18 
19 #include "slghsymbol.hh"
20 
21 class Location {
22  string filename;
23  int4 lineno;
24 public:
25  Location(void) {}
26  Location(const string &fname, const int4 line) { filename = fname; lineno = line; }
27  string getFilename(void) const { return filename; }
28  int4 getLineno(void) const { return lineno; }
29  string format(void) const;
30 };
31 
32 struct StarQuality {
33  ConstTpl id;
34  uint4 size;
35 };
36 
37 class ExprTree { // A flattened expression tree
38  friend class PcodeCompile;
39  vector<OpTpl *> *ops; // flattened ops making up the expression
40  VarnodeTpl *outvn; // Output varnode of the expression
41  // If the last op has an output, -outvn- is
42  // a COPY of that varnode
43 public:
44  ExprTree(void) { ops = (vector<OpTpl *> *)0; outvn = (VarnodeTpl *)0; }
45  ExprTree(VarnodeTpl *vn);
46  ExprTree(OpTpl *op);
47  ~ExprTree(void);
48  void setOutput(VarnodeTpl *newout);
49  VarnodeTpl *getOut(void) { return outvn; }
50  const ConstTpl &getSize(void) const { return outvn->getSize(); }
51  static vector<OpTpl *> *appendParams(OpTpl *op,vector<ExprTree *> *param);
52  static vector<OpTpl *> *toVector(ExprTree *expr);
53 };
54 
55 class PcodeCompile {
56  AddrSpace *defaultspace;
57  AddrSpace *constantspace;
58  AddrSpace *uniqspace;
59  uint4 local_labelcount; // Number of labels in current constructor
60  bool enforceLocalKey; // Force slaspec to use 'local' keyword when defining temporary varnodes
61  virtual uintb allocateTemp(void)=0;
62  virtual void addSymbol(SleighSymbol *sym)=0;
63 public:
64  PcodeCompile(void) { defaultspace=(AddrSpace *)0; constantspace=(AddrSpace *)0;
65  uniqspace=(AddrSpace *)0; local_labelcount=0; enforceLocalKey=false; }
66  virtual ~PcodeCompile(void) {}
67  virtual const Location *getLocation(SleighSymbol *sym) const=0;
68  virtual void reportError(const Location *loc, const string &msg)=0;
69  virtual void reportWarning(const Location *loc, const string &msg)=0;
70  void resetLabelCount(void) { local_labelcount=0; }
71  void setDefaultSpace(AddrSpace *spc) { defaultspace = spc; }
72  void setConstantSpace(AddrSpace *spc) { constantspace = spc; }
73  void setUniqueSpace(AddrSpace *spc) { uniqspace = spc; }
74  void setEnforceLocalKey(bool val) { enforceLocalKey = val; }
75  AddrSpace *getDefaultSpace(void) const { return defaultspace; }
76  AddrSpace *getConstantSpace(void) const { return constantspace; }
77  VarnodeTpl *buildTemporary(void);
78  LabelSymbol *defineLabel(string *name);
79  vector<OpTpl *> *placeLabel(LabelSymbol *sym);
80  vector<OpTpl *> *newOutput(bool usesLocalKey,ExprTree *rhs,string *varname,uint4 size=0);
81  void newLocalDefinition(string *varname,uint4 size=0);
82  ExprTree *createOp(OpCode opc,ExprTree *vn);
83  ExprTree *createOp(OpCode opc,ExprTree *vn1,ExprTree *vn2);
84  ExprTree *createOpOut(VarnodeTpl *outvn,OpCode opc,ExprTree *vn1,ExprTree *vn2);
85  ExprTree *createOpOutUnary(VarnodeTpl *outvn,OpCode opc,ExprTree *vn);
86  vector<OpTpl *> *createOpNoOut(OpCode opc,ExprTree *vn);
87  vector<OpTpl *> *createOpNoOut(OpCode opc,ExprTree *vn1,ExprTree *vn2);
88  vector<OpTpl *> *createOpConst(OpCode opc,uintb val);
89  ExprTree *createLoad(StarQuality *qual,ExprTree *ptr);
90  vector<OpTpl *> *createStore(StarQuality *qual,ExprTree *ptr,ExprTree *val);
91  ExprTree *createUserOp(UserOpSymbol *sym,vector<ExprTree *> *param);
92  vector<OpTpl *> *createUserOpNoOut(UserOpSymbol *sym,vector<ExprTree *> *param);
93  ExprTree *createVariadic(OpCode opc,vector<ExprTree *> *param);
94  void appendOp(OpCode opc,ExprTree *res,uintb constval,int4 constsz);
95  VarnodeTpl *buildTruncatedVarnode(VarnodeTpl *basevn,uint4 bitoffset,uint4 numbits);
96  vector<OpTpl *> *assignBitRange(VarnodeTpl *vn,uint4 bitoffset,uint4 numbits,ExprTree *rhs);
97  ExprTree *createBitRange(SpecificSymbol *sym,uint4 bitoffset,uint4 numbits);
98  VarnodeTpl *addressOf(VarnodeTpl *var,uint4 size);
99  static void force_size(VarnodeTpl *vt,const ConstTpl &size,const vector<OpTpl *> &ops);
100  static void matchSize(int4 j,OpTpl *op,bool inputonly,const vector<OpTpl *> &ops);
101  static void fillinZero(OpTpl *op,const vector<OpTpl *> &ops);
102  static bool propagateSize(ConstructTpl *ct);
103 };
104 
105 #endif
AddrSpace
A region where processor data is stored.
Definition: space.hh:73
ConstTpl
Definition: semantics.hh:31
VarnodeTpl
Definition: semantics.hh:77
UserOpSymbol
Definition: slghsymbol.hh:131
PcodeCompile
Definition: pcodecompile.hh:55
OpTpl
Definition: semantics.hh:137
Location
Definition: pcodecompile.hh:21
LabelSymbol
Definition: slghsymbol.hh:611
OpCode
OpCode
The op-code defining a specific p-code operation (PcodeOp)
Definition: opcodes.hh:35
SpecificSymbol
Definition: slghsymbol.hh:165
ExprTree
Definition: pcodecompile.hh:37
ConstructTpl
Definition: semantics.hh:161
SleighSymbol
Definition: slghsymbol.hh:23
StarQuality
Definition: pcodecompile.hh:32