ROSE  0.11.98.0
RegisterDictionary.h
1 #ifndef ROSE_BinaryAnalysis_RegisterDictionary_H
2 #define ROSE_BinaryAnalysis_RegisterDictionary_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BinaryAnalysis/RegisterDescriptor.h>
7 #include <Rose/BinaryAnalysis/RegisterParts.h>
8 
9 #include <Sawyer/SharedPointer.h>
10 #include <Sawyer/SharedObject.h>
11 
12 #include <boost/serialization/access.hpp>
13 #include <boost/serialization/map.hpp>
14 #include <boost/serialization/string.hpp>
15 #include <map>
16 #include <ostream>
17 #include <queue>
18 #include <string>
19 #include <vector>
20 
21 namespace Rose {
22 namespace BinaryAnalysis {
23 
45 
47  // Types
49 public:
52 
54  using Entries = std::map<std::string/*name*/, RegisterDescriptor>;
55 
57  using RegisterDescriptors = std::vector<RegisterDescriptor>;
58 
59 private:
60  // a descriptor can have more than one name
61  using Reverse = std::map<RegisterDescriptor, std::vector<std::string>>;
62 
64  // Data members
66 private:
67  std::string name_; // name of the dictionary, usually an architecture name like 'i386'
68  Entries forward_; // N:1 forward lookup from name to descriptor
69  Reverse reverse_; // 1:N reverse lookup from descriptor to names
70 
72  // Serialization
74 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
75 private:
76  friend class boost::serialization::access;
77 
78  template<class S>
79  void serialize(S &s, const unsigned /*version*/) {
80  s & BOOST_SERIALIZATION_NVP(name_);
81  s & BOOST_SERIALIZATION_NVP(forward_);
82  s & BOOST_SERIALIZATION_NVP(reverse_);
83  }
84 #endif
85 
87  // Constructors and destructor
89 protected:
91 
92  RegisterDictionary(const std::string &name);
93 
95 
96 public:
98  static Ptr instance(const std::string &name);
99 
101  static Ptr instanceNull();
102 
112  static Ptr instanceI8086();
113 
117  static Ptr instanceI8088();
118 
122  static Ptr instanceI286();
123 
130  static Ptr instanceI386();
131 
133  static Ptr instanceI386Math();
134 
138  static Ptr instanceI486();
139 
143  static Ptr instancePentium();
144 
149  static Ptr instancePentiumiii();
150 
152  static Ptr instancePentium4();
153 
165  static Ptr instanceAmd64();
166 
167 #ifdef ROSE_ENABLE_ASM_AARCH64
168 
173  static Ptr instanceAarch64();
174 #endif
175 
176 #ifdef ROSE_ENABLE_ASM_AARCH32
177 
180  static Ptr instanceAarch32();
181 #endif
182 
184  static Ptr instancePowerpc32();
185 
187  static Ptr instancePowerpc64();
188 
192  static Ptr instanceMips32();
193 
201 
203  static Ptr instanceM68000();
204 
207 
209  static Ptr instanceColdfire();
210 
212  static Ptr instanceColdfireEmac();
213 
215  static Ptr instanceCil();
216 
217 
228  // Properties
231 public:
232 
236  const std::string &name() const;
237  void name(const std::string&);
241  const Entries& registers() const;
242 
244  // Mutators
246 public:
247 
252  void insert(const std::string &name, RegisterDescriptor);
253 
258  void insert(const std::string &name, unsigned majr, unsigned minr, unsigned offset, unsigned nbits);
259 
264  void insert(const Ptr&);
265 
271  void resize(const std::string &name, unsigned new_nbits);
272 
274  // Lookup
276 public:
281  RegisterDescriptor find(const std::string &name) const;
282 
287  RegisterDescriptor findOrThrow(const std::string &name) const;
288 
294  const std::string& lookup(RegisterDescriptor) const;
295 
300  bool exists(RegisterDescriptor) const;
301 
309  RegisterDescriptor findLargestRegister(unsigned major, unsigned minor, size_t maxWidth=0) const;
310 
319 
325 
327  unsigned firstUnusedMajor() const;
328 
330  unsigned firstUnusedMinor(unsigned majr) const;
331 
343  class SortBySize {
344  public:
345  enum Direction { ASCENDING, DESCENDING };
346  explicit SortBySize(Direction d=DESCENDING): direction(d) {}
347  bool operator()(RegisterDescriptor a, RegisterDescriptor b) const {
348  return ASCENDING==direction ?
349  a.nBits() < b.nBits() :
350  a.nBits() > b.nBits();
351  }
352  protected:
353  Direction direction;
354  };
355 
382  template<class Compare>
384  Compare order = SortBySize(),
385  bool reconsiderParts = true);
386 
402 
418 
425  void print(std::ostream&) const;
426  friend std::ostream& operator<<(std::ostream&, const RegisterDictionary&);
430  size_t size() const;
431 };
432 
433 std::ostream&
434 operator<<(std::ostream&, const RegisterDictionary&);
435 
436 template<class Compare>
438 RegisterDictionary::filterNonoverlapping(RegisterDescriptors desc, Compare order, bool reconsiderParts) {
439  RegisterDescriptors retval;
440  Map<std::pair<int/*major*/, int/*minor*/>, ExtentMap> have_bits; // the union of the bits of descriptors we will return
441 
442  // Process the descriptors in the order specified.
443  std::priority_queue<RegisterDescriptor, RegisterDescriptors, Compare> heap(order, desc);
444  while (!heap.empty()) {
445  const RegisterDescriptor cur_desc = heap.top();
446  heap.pop();
447  const std::pair<int, int> cur_majmin(cur_desc.majorNumber(), cur_desc.minorNumber());
448  const Extent cur_extent(cur_desc.offset(), cur_desc.nBits());
449  ExtentMap &have_extents = have_bits[cur_majmin];
450  if (have_extents.distinct(cur_extent)) {
451  // We're not returning any of these bits yet, so add the whole descriptor
452  retval.push_back(cur_desc);
453  have_extents.insert(cur_extent);
454  } else if (reconsiderParts) {
455  // We're already returning some (or all) of these bits. Split the cur_extent into sub-parts by subtracting the
456  // stuff we're already returning.
457  ExtentMap parts;
458  parts.insert(cur_extent);
459  parts.erase_ranges(have_extents);
460  for (ExtentMap::iterator pi = parts.begin(); pi != parts.end(); ++pi) {
461  const Extent &part = pi->first;
462  RegisterDescriptor part_desc(cur_desc.majorNumber(), cur_desc.minorNumber(), part.first(), part.size());
463  heap.push(part_desc);
464  }
465  }
466  }
467  return retval;
468 }
469 
470 } // namespace
471 } // namespace
472 
473 #endif
474 #endif
static Ptr instance(const std::string &name)
Allocating constructor for an empty dictionary.
unsigned majorNumber() const
Property: Major number.
static Ptr instanceI486()
Intel 80486 registers.
const Entries & registers() const
Returns the list of all register definitions in the dictionary.
size_t nBits() const
Property: Size in bits.
Rose::BinaryAnalysis::RegisterParts getAllParts() const
Returns all register parts.
static Ptr instanceColdfire()
FreeScale ColdFire generic hardware registers.
const std::string & lookup(RegisterDescriptor) const
Returns a register name for a given descriptor.
A contiguous range of values.
Definition: rangemap.h:50
InsSetArchitecture
Instruction sets organized by families.
RegisterDescriptor findOrThrow(const std::string &name) const
Find a register by name.
static Ptr instanceMips32AlternateNames()
MIPS32 Release 1 with special registers.
static Ptr instanceI386()
Intel 80386 registers.
Main namespace for the ROSE library.
Holds a set of registers without regard for register boundaries.
Definition: RegisterParts.h:28
void print(std::ostream &) const
Prints the contents of this register dictionary.
size_t size() const
Return the number of entries in the dictionary.
Compares number of bits in register descriptors.
const std::string & name() const
Property: Architecture name.
iterator insert(Range new_range, Value new_value=Value(), bool make_hole=true)
Insert a range/value pair into the map.
Definition: rangemap.h:1192
void insert(const std::string &name, RegisterDescriptor)
Insert a definition into the dictionary.
static Ptr instancePentium()
Intel Pentium registers.
static Ptr instanceI8088()
Intel 8088 registers.
static Ptr instanceM68000()
Motorola M68330 register names.
void erase_ranges(const OtherMap &other)
Erase ranges from this map.
Definition: rangemap.h:1181
unsigned firstUnusedMajor() const
Returns the first unused major register number.
static Ptr instanceAmd64()
Amd64 registers.
Describes (part of) a physical CPU register.
RegisterDescriptor find(const std::string &name) const
Find a register by name.
static Ptr instancePentiumiii()
Intel Pentium III registers.
static Ptr instanceNull()
Mostly empty dictionary for the null ISA.
iterator begin()
First-item iterator.
Definition: rangemap.h:905
size_t offset() const
Property: Offset to least-significant bit.
Value size() const
Returns the number of values represented by the range.
Definition: rangemap.h:147
RegisterDescriptors getLargestRegisters() const
Returns a list of the largest non-overlapping registers.
static Ptr instancePowerpc32()
PowerPC-32 registers.
RegisterDescriptors getDescriptors() const
Returns the list of all register descriptors.
Extends std::map with methods that return optional values.
Definition: Map.h:10
static RegisterDescriptors filterNonoverlapping(RegisterDescriptors reglist, Compare order=SortBySize(), bool reconsiderParts=true)
Returns the list of non-overlapping registers or register parts.
Binary analysis.
unsigned firstUnusedMinor(unsigned majr) const
Returns the first unused minor register number.
static Ptr instanceI286()
Intel 80286 registers.
iterator end()
End-item iterator.
Definition: rangemap.h:917
static Ptr instanceMips32()
MIPS32 Release 1.
RegisterDescriptors getSmallestRegisters() const
Returns a list of the smallest non-overlapping registers.
bool exists(RegisterDescriptor) const
Determine if a register descriptor exists.
RegisterDictionaryPtr Ptr
Reference counting pointer.
std::map< std::string, RegisterDescriptor > Entries
List of registers in dictionary.
Base class for reference counted objects.
Definition: SharedObject.h:64
static Ptr instanceCil()
CIL register names.
static Ptr instanceM68000AlternateNames()
Motorola M68330 alternate registers.
static Ptr instanceI386Math()
Intel 80386 with 80387 math co-processor.
static Ptr instanceForIsa(SgAsmExecutableFileFormat::InsSetArchitecture)
Class method to choose an appropriate register dictionary for an instruction set architecture.
void first(const Value &first)
Accessor for the first value of a range.
Definition: rangemap.h:103
std::vector< RegisterDescriptor > RegisterDescriptors
List of register descriptors in dictionary.
Defines registers available for a particular architecture.
void resize(const std::string &name, unsigned new_nbits)
Changes the size of a register.
unsigned minorNumber() const
Property: Minor number.
static Ptr instancePentium4()
Intel Pentium 4 registers.
friend std::ostream & operator<<(std::ostream &, const RegisterDictionary &)
Prints the contents of this register dictionary.
Represents an interpretation of a binary container.
static Ptr instancePowerpc64()
PowerPC-64 registers.
static Ptr instanceColdfireEmac()
Registers for FreeScale ColdFire CPUs with EMAC (extended multiply-accumulate) unit.
static Ptr instanceI8086()
Intel 8086 registers.
RegisterDescriptor findLargestRegister(unsigned major, unsigned minor, size_t maxWidth=0) const
Finds the first largest register with specified major and minor number.