ROSE  0.9.11.56
Registers.h
1 #ifndef ROSE_BinaryAnalysis_Registers_H
2 #define ROSE_BinaryAnalysis_Registers_H
3 
4 #include "RegisterParts.h"
5 
6 #include <boost/serialization/access.hpp>
7 #include <boost/serialization/map.hpp>
8 #include <boost/serialization/string.hpp>
9 
10 #include <queue>
11 
12 namespace Rose {
13 namespace BinaryAnalysis {
14 
36 public:
37  typedef std::map<std::string/*name*/, RegisterDescriptor> Entries;
38  typedef std::vector<RegisterDescriptor> RegisterDescriptors;
39 
40 
50  static const RegisterDictionary *dictionary_i8086();
51 
55  static const RegisterDictionary *dictionary_i8088();
56 
60  static const RegisterDictionary *dictionary_i286();
61 
68  static const RegisterDictionary *dictionary_i386();
69 
72 
76  static const RegisterDictionary *dictionary_i486();
77 
81  static const RegisterDictionary *dictionary_pentium();
82 
88 
91 
103  static const RegisterDictionary *dictionary_amd64();
104 
116  static const RegisterDictionary *dictionary_arm7();
117 
119  static const RegisterDictionary *dictionary_powerpc();
120 
124  static const RegisterDictionary *dictionary_mips32();
125 
133 
135  static const RegisterDictionary *dictionary_m68000();
136 
139 
141  static const RegisterDictionary *dictionary_coldfire();
142 
145 
146 private:
147  typedef std::map<RegisterDescriptor, std::vector<std::string> > Reverse; // a descriptor can have more than one name
148  std::string name; /*name of the dictionary, usually an architecture name like 'i386'*/
149  Entries forward;
150  Reverse reverse;
151 
152 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
153 private:
154  friend class boost::serialization::access;
155 
156  template<class S>
157  void serialize(S &s, const unsigned /*version*/) {
158  s & BOOST_SERIALIZATION_NVP(name);
159  s & BOOST_SERIALIZATION_NVP(forward);
160  s & BOOST_SERIALIZATION_NVP(reverse);
161  }
162 #endif
163 
164 protected:
165  // needed for serialization
166  RegisterDictionary() {}
167 
168 public:
169  RegisterDictionary(const std::string &name)
170  :name(name) {}
171  RegisterDictionary(const RegisterDictionary& other) {
172  *this = other;
173  }
174 
183  const std::string &get_architecture_name() const {
184  return name;
185  }
186 
189  void set_architecture_name(const std::string &name) {
190  this->name = name;
191  }
192 
195  void insert(const std::string &name, RegisterDescriptor);
196 
199  void insert(const std::string &name, unsigned majr, unsigned minr, unsigned offset, unsigned nbits);
200 
203  void insert(const RegisterDictionary*);
204 
207  void insert(const RegisterDictionary&);
208 
212  void resize(const std::string &name, unsigned new_nbits);
213 
217  const RegisterDescriptor *lookup(const std::string &name) const;
218 
222  const std::string& lookup(RegisterDescriptor) const;
223 
230 
238  RegisterDescriptor findLargestRegister(unsigned major, unsigned minor, size_t maxWidth=0) const;
239 
248 
251  const Entries& get_registers() const;
252  Entries& get_registers();
257  RegisterDescriptors get_descriptors() const;
258 
260  unsigned firstUnusedMajor() const;
261 
263  unsigned firstUnusedMinor(unsigned majr) const;
264 
276  class SortBySize {
277  public:
278  enum Direction { ASCENDING, DESCENDING };
279  explicit SortBySize(Direction d=DESCENDING): direction(d) {}
280  bool operator()(RegisterDescriptor a, RegisterDescriptor b) const {
281  return ASCENDING==direction ?
282  a.nBits() < b.nBits() :
283  a.nBits() > b.nBits();
284  }
285  protected:
286  Direction direction;
287  };
288 
314  template<class Compare>
315  static RegisterDescriptors filter_nonoverlapping(RegisterDescriptors reglist,
316  Compare order=SortBySize(),
317  bool reconsider_parts=true);
318 
331  RegisterDescriptors get_largest_registers() const;
332 
344  RegisterDescriptors get_smallest_registers() const;
345 
348  void print(std::ostream&) const;
349  friend std::ostream& operator<<(std::ostream&, const RegisterDictionary&);
350 
352  size_t size() const { return forward.size(); }
353 };
354 
358 public:
360  explicit RegisterNames(const RegisterDictionary *dict=NULL)
361  : dflt_dict(dict), prefix("REG"), show_offset(-1), offset_prefix("@"), show_size(-1), size_prefix("+") {}
362 
365  std::string operator()(RegisterDescriptor, const RegisterDictionary *dict=NULL) const;
366 
368  std::string prefix;
369  std::string suffix;
371  std::string offset_prefix;
372  std::string offset_suffix;
373  int show_size;
374  std::string size_prefix;
375  std::string size_suffix;
376 };
377 
378 /*******************************************************************************************************************************
379  * Template definitions
380  *******************************************************************************************************************************/
381 
382 
383 
384 template<class Compare>
385 RegisterDictionary::RegisterDescriptors
386 RegisterDictionary::filter_nonoverlapping(RegisterDescriptors desc, Compare order, bool reconsider_parts)
387 {
388  RegisterDescriptors retval;
389  Map<std::pair<int/*major*/, int/*minor*/>, ExtentMap> have_bits; // the union of the bits of descriptors we will return
390 
391  // Process the descriptors in the order specified.
392  std::priority_queue<RegisterDescriptor, RegisterDescriptors, Compare> heap(order, desc);
393  while (!heap.empty()) {
394  const RegisterDescriptor cur_desc = heap.top();
395  heap.pop();
396  const std::pair<int, int> cur_majmin(cur_desc.majorNumber(), cur_desc.minorNumber());
397  const Extent cur_extent(cur_desc.offset(), cur_desc.nBits());
398  ExtentMap &have_extents = have_bits[cur_majmin];
399  if (have_extents.distinct(cur_extent)) {
400  // We're not returning any of these bits yet, so add the whole descriptor
401  retval.push_back(cur_desc);
402  have_extents.insert(cur_extent);
403  } else if (reconsider_parts) {
404  // We're already returning some (or all) of these bits. Split the cur_extent into sub-parts by subtracting the
405  // stuff we're already returning.
406  ExtentMap parts;
407  parts.insert(cur_extent);
408  parts.erase_ranges(have_extents);
409  for (ExtentMap::iterator pi=parts.begin(); pi!=parts.end(); ++pi) {
410  const Extent &part = pi->first;
411  RegisterDescriptor part_desc(cur_desc.majorNumber(), cur_desc.minorNumber(), part.first(), part.size());
412  heap.push(part_desc);
413  }
414  }
415  }
416  return retval;
417 }
418 
419 } // namespace
420 } // namespace
421 
422 #endif
int show_size
0=>never; positive=>always; negative=>when offset is non-zero
Definition: Registers.h:373
const RegisterDescriptor * exists(RegisterDescriptor) const
Determine if a register descriptor exists.
static const RegisterDictionary * dictionary_i8088()
Intel 8088 registers.
static const RegisterDictionary * dictionary_pentium()
Intel Pentium registers.
unsigned majorNumber() const
Property: Major number.
static const RegisterDictionary * dictionary_arm7()
ARM7 registers.
static const RegisterDictionary * dictionary_coldfire_emac()
Registers for FreeScale ColdFire CPUs with EMAC (extended multiply-accumulate) unit.
size_t nBits() const
Property: Size in bits.
RegisterDescriptors get_largest_registers() const
Returns a list of the largest non-overlapping registers.
RegisterDescriptors get_descriptors() const
Returns the list of all register descriptors.
std::string offset_prefix
String printed before the offset when the offset is shown.
Definition: Registers.h:371
Rose::BinaryAnalysis::RegisterParts getAllParts() const
Returns all register parts.
static const RegisterDictionary * dictionary_i386_387()
Intel 80386 with 80387 math co-processor.
const Entries & get_registers() const
Returns the list of all register definitions in the dictionary.
A contiguous range of values.
Definition: rangemap.h:50
std::string offset_suffix
String printed after the offset when the offset is shown.
Definition: Registers.h:372
InsSetArchitecture
Instruction sets organized by families.
static RegisterDescriptors filter_nonoverlapping(RegisterDescriptors reglist, Compare order=SortBySize(), bool reconsider_parts=true)
Returns the list of non-overlapping registers or register parts.
Definition: Registers.h:386
static const RegisterDictionary * dictionary_m68000()
Motorola M68330 register names.
static const RegisterDictionary * dictionary_pentium4()
Intel Pentium 4 registers.
static const RegisterDictionary * dictionary_i386()
Intel 80386 registers.
static const RegisterDictionary * dictionary_powerpc()
PowerPC registers.
Main namespace for the ROSE library.
Holds a set of registers without regard for register boundaries.
Definition: RegisterParts.h:25
void print(std::ostream &) const
Prints the contents of this register dictionary.
size_t size() const
Return the number of entries in the dictionary.
Definition: Registers.h:352
const RegisterDescriptor * lookup(const std::string &name) const
Returns a descriptor for a given register name.
Compares number of bits in register descriptors.
Definition: Registers.h:276
RegisterNames(const RegisterDictionary *dict=NULL)
Constructor.
Definition: Registers.h:360
static const RegisterDictionary * dictionary_m68000_altnames()
Motorola M68330 alternate registers.
int show_offset
0=>never show offset; positive=>always show; negative=>show only when non-zero
Definition: Registers.h:370
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
std::string size_prefix
String printed prior to the size when the size is printed.
Definition: Registers.h:374
static const RegisterDictionary * dictionary_coldfire()
FreeScale ColdFire generic hardware registers.
const std::string & get_architecture_name() const
Obtain the name of the dictionary.
Definition: Registers.h:183
void insert(const std::string &name, RegisterDescriptor)
Insert a definition into the dictionary.
std::string operator()(RegisterDescriptor, const RegisterDictionary *dict=NULL) const
Obtain a name for a register descriptor.
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.
std::string suffix
String to print at the very end of the generated name.
Definition: Registers.h:369
Describes (part of) a physical CPU register.
static const RegisterDictionary * dictionary_for_isa(SgAsmExecutableFileFormat::InsSetArchitecture)
Class method to choose an appropriate register dictionary for an instruction set architecture.
std::string size_suffix
String printed after the size when the size is printed.
Definition: Registers.h:375
RegisterDescriptors get_smallest_registers() const
Returns a list of the smallest non-overlapping registers.
static const RegisterDictionary * dictionary_i486()
Intel 80486 registers.
Prints a register name even when no dictionary is available or when the dictionary doesn't contain an...
Definition: Registers.h:357
iterator begin()
First-item iterator.
Definition: rangemap.h:905
void set_architecture_name(const std::string &name)
Set the name of the dictionary.
Definition: Registers.h:189
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
std::string prefix
The leading part of a register name.
Definition: Registers.h:368
Extends std::map with methods that return optional values.
Definition: Map.h:10
const RegisterDictionary * dflt_dict
Dictionary supplied to the constructor.
Definition: Registers.h:367
unsigned firstUnusedMinor(unsigned majr) const
Returns the first unused minor register number.
iterator end()
End-item iterator.
Definition: rangemap.h:917
static const RegisterDictionary * dictionary_i8086()
Intel 8086 registers.
static const RegisterDictionary * dictionary_amd64()
Amd64 registers.
static const RegisterDictionary * dictionary_pentiumiii()
Intel Pentium III registers.
void first(const Value &first)
Accessor for the first value of a range.
Definition: rangemap.h:103
Defines registers available for a particular architecture.
Definition: Registers.h:35
static const RegisterDictionary * dictionary_mips32()
MIPS32 Release 1.
void resize(const std::string &name, unsigned new_nbits)
Changes the size of a register.
static const RegisterDictionary * dictionary_i286()
Intel 80286 registers.
unsigned minorNumber() const
Property: Minor number.
Represents an interpretation of a binary container.
static const RegisterDictionary * dictionary_mips32_altnames()
MIPS32 Release 1 with special registers.
RegisterDescriptor findLargestRegister(unsigned major, unsigned minor, size_t maxWidth=0) const
Finds the first largest register with specified major and minor number.