Ghidra Decompiler Analysis Engine
semantics.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 __SEMANTICS__
17 #define __SEMANTICS__
18 
19 #include "context.hh"
20 
21 // We remap these opcodes for internal use during pcode generation
22 
23 #define BUILD CPUI_MULTIEQUAL
24 #define DELAY_SLOT CPUI_INDIRECT
25 #define CROSSBUILD CPUI_PTRSUB
26 #define MACROBUILD CPUI_CAST
27 #define LABELBUILD CPUI_PTRADD
28 
29 class Translate; // Forward declaration
30 class HandleTpl; // Forward declaration
31 class ConstTpl {
32 public:
33  enum const_type { real=0, handle=1, j_start=2, j_next=3, j_curspace=4,
34  j_curspace_size=5, spaceid=6, j_relative=7,
35  j_flowref=8, j_flowref_size=9, j_flowdest=10, j_flowdest_size=11 };
36  enum v_field { v_space=0, v_offset=1, v_size=2, v_offset_plus=3 };
37 private:
38  const_type type;
39  union {
40  // uintb real; // an actual constant
41  AddrSpace *spaceid; // Id (pointer) for registered space
42  int4 handle_index; // Place holder for run-time determined value
43  } value;
44  uintb value_real;
45  v_field select; // Which part of handle to use as constant
46  static void printHandleSelector(ostream &s,v_field val);
47  static v_field readHandleSelector(const string &name);
48 public:
49  ConstTpl(void) { type = real; value_real = 0; }
50  ConstTpl(const ConstTpl &op2) {
51  type=op2.type; value=op2.value; value_real=op2.value_real; select=op2.select; }
52  ConstTpl(const_type tp,uintb val);
53  ConstTpl(const_type tp);
54  ConstTpl(AddrSpace *sid);
55  ConstTpl(const_type tp,int4 ht,v_field vf);
56  ConstTpl(const_type tp,int4 ht,v_field vf,uintb plus);
57  bool isConstSpace(void) const;
58  bool isUniqueSpace(void) const;
59  bool operator==(const ConstTpl &op2) const;
60  bool operator<(const ConstTpl &op2) const;
61  uintb getReal(void) const { return value_real; }
62  AddrSpace *getSpace(void) const { return value.spaceid; }
63  int4 getHandleIndex(void) const { return value.handle_index; }
64  const_type getType(void) const { return type; }
65  v_field getSelect(void) const { return select; }
66  uintb fix(const ParserWalker &walker) const;
67  AddrSpace *fixSpace(const ParserWalker &walker) const;
68  void transfer(const vector<HandleTpl *> &params);
69  bool isZero(void) const { return ((type==real)&&(value_real==0)); }
70  void changeHandleIndex(const vector<int4> &handmap);
71  void fillinSpace(FixedHandle &hand,const ParserWalker &walker) const;
72  void fillinOffset(FixedHandle &hand,const ParserWalker &walker) const;
73  void saveXml(ostream &s) const;
74  void restoreXml(const Element *el,const AddrSpaceManager *manage);
75 };
76 
77 class VarnodeTpl {
78  friend class OpTpl;
79  friend class HandleTpl;
80  ConstTpl space,offset,size;
81  bool unnamed_flag;
82 public:
83  VarnodeTpl(int4 hand,bool zerosize);
84  VarnodeTpl(void) : space(), offset(), size() { unnamed_flag=false; }
85  VarnodeTpl(const ConstTpl &sp,const ConstTpl &off,const ConstTpl &sz);
86  VarnodeTpl(const VarnodeTpl &vn);
87  const ConstTpl &getSpace(void) const { return space; }
88  const ConstTpl &getOffset(void) const { return offset; }
89  const ConstTpl &getSize(void) const { return size; }
90  bool isDynamic(const ParserWalker &walker) const;
91  int4 transfer(const vector<HandleTpl *> &params);
92  bool isZeroSize(void) const { return size.isZero(); }
93  bool operator<(const VarnodeTpl &op2) const;
94  void setOffset(uintb constVal) { offset = ConstTpl(ConstTpl::real,constVal); }
95  void setRelative(uintb constVal) { offset = ConstTpl(ConstTpl::j_relative,constVal); }
96  void setSize(const ConstTpl &sz ) { size = sz; }
97  bool isUnnamed(void) const { return unnamed_flag; }
98  void setUnnamed(bool val) { unnamed_flag = val; }
99  bool isLocalTemp(void) const;
100  bool isRelative(void) const { return (offset.getType() == ConstTpl::j_relative); }
101  void changeHandleIndex(const vector<int4> &handmap);
102  bool adjustTruncation(int4 sz,bool isbigendian);
103  void saveXml(ostream &s) const;
104  void restoreXml(const Element *el,const AddrSpaceManager *manage);
105 };
106 
107 class HandleTpl {
108  ConstTpl space;
109  ConstTpl size;
110  ConstTpl ptrspace;
111  ConstTpl ptroffset;
112  ConstTpl ptrsize;
113  ConstTpl temp_space;
114  ConstTpl temp_offset;
115 public:
116  HandleTpl(void) {}
117  HandleTpl(const VarnodeTpl *vn);
118  HandleTpl(const ConstTpl &spc,const ConstTpl &sz,const VarnodeTpl *vn,
119  AddrSpace *t_space,uintb t_offset);
120  const ConstTpl &getSpace(void) const { return space; }
121  const ConstTpl &getPtrSpace(void) const { return ptrspace; }
122  const ConstTpl &getPtrOffset(void) const { return ptroffset; }
123  const ConstTpl &getPtrSize(void) const { return ptrsize; }
124  const ConstTpl &getSize(void) const { return size; }
125  const ConstTpl &getTempSpace(void) const { return temp_space; }
126  const ConstTpl &getTempOffset(void) const { return temp_offset; }
127  void setSize(const ConstTpl &sz) { size = sz; }
128  void setPtrSize(const ConstTpl &sz) { ptrsize=sz; }
129  void setPtrOffset(uintb val) { ptroffset = ConstTpl(ConstTpl::real,val); }
130  void setTempOffset(uintb val) { temp_offset = ConstTpl(ConstTpl::real,val); }
131  void fix(FixedHandle &hand,const ParserWalker &walker) const;
132  void changeHandleIndex(const vector<int4> &handmap);
133  void saveXml(ostream &s) const;
134  void restoreXml(const Element *el,const AddrSpaceManager *manage);
135 };
136 
137 class OpTpl {
138  VarnodeTpl *output;
139  OpCode opc;
140  vector<VarnodeTpl *> input;
141 public:
142  OpTpl(void) {}
143  OpTpl(OpCode oc) { opc = oc; output = (VarnodeTpl *)0; }
144  ~OpTpl(void);
145  VarnodeTpl *getOut(void) const { return output; }
146  int4 numInput(void) const { return input.size(); }
147  VarnodeTpl *getIn(int4 i) const { return input[i]; }
148  OpCode getOpcode(void) const { return opc; }
149  bool isZeroSize(void) const;
150  void setOpcode(OpCode o) { opc = o; }
151  void setOutput(VarnodeTpl *vt) { output = vt; }
152  void clearOutput(void) { delete output; output = (VarnodeTpl *)0; }
153  void addInput(VarnodeTpl *vt) { input.push_back(vt); }
154  void setInput(VarnodeTpl *vt,int4 slot) { input[slot] = vt; }
155  void removeInput(int4 index);
156  void changeHandleIndex(const vector<int4> &handmap);
157  void saveXml(ostream &s) const;
158  void restoreXml(const Element *el,const AddrSpaceManager *manage);
159 };
160 
162  friend class SleighCompile;
163 protected:
164  uint4 delayslot;
165  uint4 numlabels; // Number of label templates
166  vector<OpTpl *> vec;
167  HandleTpl *result;
168  void setOpvec(vector<OpTpl *> &opvec) { vec = opvec; }
169  void setNumLabels(uint4 val) { numlabels = val; }
170 public:
171  ConstructTpl(void) { delayslot=0; numlabels=0; result = (HandleTpl *)0; }
172  ~ConstructTpl(void);
173  uint4 delaySlot(void) const { return delayslot; }
174  uint4 numLabels(void) const { return numlabels; }
175  const vector<OpTpl *> &getOpvec(void) const { return vec; }
176  HandleTpl *getResult(void) const { return result; }
177  bool addOp(OpTpl *ot);
178  bool addOpList(const vector<OpTpl *> &oplist);
179  void setResult(HandleTpl *t) { result = t; }
180  int4 fillinBuild(vector<int4> &check,AddrSpace *const_space);
181  bool buildOnly(void) const;
182  void changeHandleIndex(const vector<int4> &handmap);
183  void setInput(VarnodeTpl *vn,int4 index,int4 slot);
184  void setOutput(VarnodeTpl *vn,int4 index);
185  void deleteOps(const vector<int4> &indices);
186  void saveXml(ostream &s,int4 sectionid) const;
187  int4 restoreXml(const Element *el,const AddrSpaceManager *manage);
188 };
189 
190 class PcodeEmit; // Forward declaration for emitter
191 
192 class PcodeBuilder { // SLEIGH specific pcode generator
193  uint4 labelbase;
194  uint4 labelcount;
195 protected:
196  ParserWalker *walker;
197  virtual void dump( OpTpl *op )=0;
198 public:
199  PcodeBuilder(uint4 lbcnt) { labelbase=labelcount=lbcnt; }
200  virtual ~PcodeBuilder(void) {}
201 
202  uint4 getLabelBase(void) const { return labelbase; }
203  ParserWalker *getCurrentWalker() const { return walker; }
204  void build(ConstructTpl *construct,int4 secnum);
205  virtual void appendBuild(OpTpl *bld,int4 secnum)=0;
206  virtual void delaySlot(OpTpl *op)=0;
207  virtual void setLabel(OpTpl *op)=0;
208  virtual void appendCrossBuild(OpTpl *bld,int4 secnum)=0;
209 };
210 
211 #endif
PcodeEmit
Abstract class for emitting pcode to an application.
Definition: translate.hh:76
AddrSpace
A region where processor data is stored.
Definition: space.hh:73
ConstTpl
Definition: semantics.hh:31
SleighCompile
Definition: slgh_compile.hh:185
VarnodeTpl
Definition: semantics.hh:77
Element
An XML element. A node in the DOM tree.
Definition: xml.hh:150
OpTpl
Definition: semantics.hh:137
HandleTpl
Definition: semantics.hh:107
Translate
The interface to a translation engine for a processor.
Definition: translate.hh:294
OpCode
OpCode
The op-code defining a specific p-code operation (PcodeOp)
Definition: opcodes.hh:35
FixedHandle
Definition: context.hh:35
AddrSpaceManager
A manager for different address spaces.
Definition: translate.hh:218
ConstructTpl
Definition: semantics.hh:161
SleighBase::saveXml
void saveXml(ostream &s) const
Write out the SLEIGH specification as an XML <sleigh> tag.
Definition: sleighbase.cc:185
PcodeBuilder
Definition: semantics.hh:192
ParserWalker
Definition: context.hh:124
SleighBase::restoreXml
void restoreXml(const Element *el)
Read a SLEIGH specification from XML.
Definition: sleighbase.cc:222