Ghidra Decompiler Analysis Engine
cpool.hh
Go to the documentation of this file.
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  */
18 
19 #ifndef __CPOOL__
20 #define __CPOOL__
21 
22 #include "type.hh"
23 
44 class CPoolRecord {
45 public:
47  enum {
56  };
57  enum {
60  };
61 private:
62  friend class ConstantPool;
63  uint4 tag;
64  uint4 flags;
65  string token;
66  uintb value;
67  Datatype *type;
68  uint1 *byteData;
69  int4 byteDataLen;
70 public:
71  CPoolRecord(void) { type = (Datatype *)0; byteData = (uint1 *)0; }
72  ~CPoolRecord(void) { if (byteData != (uint1 *)0) delete [] byteData; }
73  uint4 getTag(void) const { return tag; }
74  const string &getToken(void) const { return token; }
75  const uint1 *getByteData(void) const { return byteData; }
76  int4 getByteDataLength(void) const { return byteDataLen; }
77  Datatype *getType(void) const { return type; }
78  uintb getValue(void) const { return value; }
79  bool isConstructor(void) const { return ((flags & is_constructor)!=0); }
80  bool isDestructor(void) const { return ((flags & is_destructor)!=0); }
81  void saveXml(ostream &s) const;
82  void restoreXml(const Element *el,TypeFactory &typegrp);
83 };
84 
92 class ConstantPool {
99  virtual CPoolRecord *createRecord(const vector<uintb> &refs)=0;
100 public:
101  virtual ~ConstantPool() {}
102 
107  virtual const CPoolRecord *getRecord(const vector<uintb> &refs) const=0;
108 
117  void putRecord(const vector<uintb> &refs,uint4 tag,const string &tok,Datatype *ct);
118 
127  const CPoolRecord *restoreXmlRecord(const vector<uintb> &refs,const Element *el,TypeFactory &typegrp);
128 
129  virtual bool empty(void) const=0;
130  virtual void clear(void)=0;
131 
137  virtual void saveXml(ostream &s) const=0;
138 
145  virtual void restoreXml(const Element *el,TypeFactory &typegrp)=0;
146 };
147 
154 
163  class CheapSorter {
164  public:
165  uintb a;
166  uintb b;
167  CheapSorter(void) { a = 0; b = 0; }
168  CheapSorter(const CheapSorter &op2) { a = op2.a; b = op2.b; }
169  CheapSorter(const vector<uintb> &refs) { a = refs[0]; b = (refs.size() > 1) ? refs[1] : 0; }
170 
175  bool operator<(const CheapSorter &op2) const {
176  if (a != op2.a) return (a<op2.a);
177  return (b < op2.b);
178  }
179 
183  void apply(vector<uintb> &refs) const { refs.push_back(a); refs.push_back(b); }
184 
185  void saveXml(ostream &s) const;
186  void restoreXml(const Element *el);
187  };
188  map<CheapSorter,CPoolRecord> cpoolMap;
189  virtual CPoolRecord *createRecord(const vector<uintb> &refs);
190 public:
191  virtual const CPoolRecord *getRecord(const vector<uintb> &refs) const;
192  virtual bool empty(void) const { return cpoolMap.empty(); }
193  virtual void clear(void) { cpoolMap.clear(); }
194  virtual void saveXml(ostream &s) const;
195  virtual void restoreXml(const Element *el,TypeFactory &typegrp);
196 };
197 
198 #endif
ConstantPoolInternal::getRecord
virtual const CPoolRecord * getRecord(const vector< uintb > &refs) const
Retrieve a constant pool record (CPoolRecord) given a reference to it.
Definition: cpool.cc:202
ConstantPool::restoreXmlRecord
const CPoolRecord * restoreXmlRecord(const vector< uintb > &refs, const Element *el, TypeFactory &typegrp)
Restore a CPoolRecord given a reference and an XML stream.
Definition: cpool.cc:159
CPoolRecord::restoreXml
void restoreXml(const Element *el, TypeFactory &typegrp)
Restore object from XML stream.
Definition: cpool.cc:75
CPoolRecord::string_literal
@ string_literal
Constant reference to string (passed back as byteData)
Definition: cpool.hh:49
CPoolRecord::pointer_field
@ pointer_field
Pointer to a field, name in token, data-type in type.
Definition: cpool.hh:52
CPoolRecord
A description of a byte-code object referenced by a constant.
Definition: cpool.hh:44
CPoolRecord::saveXml
void saveXml(ostream &s) const
Save object to an XML stream.
Definition: cpool.cc:20
CPoolRecord::class_reference
@ class_reference
Reference to (system level) class object, token holds class name.
Definition: cpool.hh:50
ConstantPool
An interface to the pool of constant objects for byte-code languages.
Definition: cpool.hh:92
CPoolRecord::check_cast
@ check_cast
Pointer to object, new name in token, new data-type in type.
Definition: cpool.hh:55
ConstantPool::putRecord
void putRecord(const vector< uintb > &refs, uint4 tag, const string &tok, Datatype *ct)
A a new constant pool record to this database.
Definition: cpool.cc:150
Element
An XML element. A node in the DOM tree.
Definition: xml.hh:150
CPoolRecord::is_destructor
@ is_destructor
Referenced method is a destructor.
Definition: cpool.hh:59
ConstantPool::getRecord
virtual const CPoolRecord * getRecord(const vector< uintb > &refs) const =0
Retrieve a constant pool record (CPoolRecord) given a reference to it.
ConstantPoolInternal::saveXml
virtual void saveXml(ostream &s) const
Save all records in this container to an XML stream.
Definition: cpool.cc:213
ConstantPoolInternal::clear
virtual void clear(void)
Release any (local) resources.
Definition: cpool.hh:193
CPoolRecord::pointer_method
@ pointer_method
Pointer to a method, name in token, signature in type.
Definition: cpool.hh:51
ConstantPool::clear
virtual void clear(void)=0
Release any (local) resources.
ConstantPoolInternal::empty
virtual bool empty(void) const
Is the container empty of records.
Definition: cpool.hh:192
Datatype
The base datatype class for the decompiler.
Definition: type.hh:62
TypeFactory
Container class for all Datatype objects in an Architecture.
Definition: type.hh:396
ConstantPool::saveXml
virtual void saveXml(ostream &s) const =0
Save all records in this container to an XML stream.
CPoolRecord::primitive
@ primitive
Constant value of data-type type, cpool operator can be eliminated.
Definition: cpool.hh:48
type.hh
Classes for describing and printing data-types.
CPoolRecord::is_constructor
@ is_constructor
Referenced method is a constructor.
Definition: cpool.hh:58
ConstantPoolInternal::restoreXml
virtual void restoreXml(const Element *el, TypeFactory &typegrp)
Restore constant pool records from an XML stream.
Definition: cpool.cc:225
CPoolRecord::instance_of
@ instance_of
Boolean value, token is language specific indicator, type is boolean data-type.
Definition: cpool.hh:54
ConstantPool::restoreXml
virtual void restoreXml(const Element *el, TypeFactory &typegrp)=0
Restore constant pool records from an XML stream.
ConstantPoolInternal
An implementation of the ConstantPool interface storing records internally in RAM.
Definition: cpool.hh:153
ConstantPool::empty
virtual bool empty(void) const =0
Is the container empty of records.
CPoolRecord::array_length
@ array_length
Integer length, token is language specific indicator, type is integral data-type.
Definition: cpool.hh:53