ROSE  0.11.96.0
Registers.h
1 #ifndef ROSE_BinaryAnalysis_Registers_H
2 #define ROSE_BinaryAnalysis_Registers_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BinaryAnalysis/RegisterParts.h>
7 
8 #include <boost/serialization/access.hpp>
9 #include <boost/serialization/map.hpp>
10 #include <boost/serialization/string.hpp>
11 
12 #include <queue>
13 
14 namespace Rose {
15 namespace BinaryAnalysis {
16 
38 public:
39  typedef std::map<std::string/*name*/, RegisterDescriptor> Entries;
40  typedef std::vector<RegisterDescriptor> RegisterDescriptors;
41 
43  static const RegisterDictionary *dictionary_null();
44 
54  static const RegisterDictionary *dictionary_i8086();
55 
59  static const RegisterDictionary *dictionary_i8088();
60 
64  static const RegisterDictionary *dictionary_i286();
65 
72  static const RegisterDictionary *dictionary_i386();
73 
76 
80  static const RegisterDictionary *dictionary_i486();
81 
85  static const RegisterDictionary *dictionary_pentium();
86 
92 
95 
107  static const RegisterDictionary *dictionary_amd64();
108 
109 #ifdef ROSE_ENABLE_ASM_AARCH64
110 
115  static const RegisterDictionary* dictionary_aarch64();
116 #endif
117 
118 #ifdef ROSE_ENABLE_ASM_AARCH32
119 
122  static const RegisterDictionary* dictionary_aarch32();
123 #endif
124 
127 
130 
134  static const RegisterDictionary *dictionary_mips32();
135 
143 
145  static const RegisterDictionary *dictionary_m68000();
146 
149 
151  static const RegisterDictionary *dictionary_coldfire();
152 
155 
157  static const RegisterDictionary *dictionary_Cil();
158 
159 private:
160  typedef std::map<RegisterDescriptor, std::vector<std::string> > Reverse; // a descriptor can have more than one name
161  std::string name; /*name of the dictionary, usually an architecture name like 'i386'*/
162  Entries forward;
163  Reverse reverse;
164 
165 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
166 private:
167  friend class boost::serialization::access;
168 
169  template<class S>
170  void serialize(S &s, const unsigned /*version*/) {
171  s & BOOST_SERIALIZATION_NVP(name);
172  s & BOOST_SERIALIZATION_NVP(forward);
173  s & BOOST_SERIALIZATION_NVP(reverse);
174  }
175 #endif
176 
177 protected:
178  // needed for serialization
179  RegisterDictionary() {}
180 
181 public:
182  RegisterDictionary(const std::string &name)
183  :name(name) {}
184  RegisterDictionary(const RegisterDictionary& other) {
185  *this = other;
186  }
187 
196  const std::string &get_architecture_name() const {
197  return name;
198  }
199 
202  void set_architecture_name(const std::string &name) {
203  this->name = name;
204  }
205 
208  void insert(const std::string &name, RegisterDescriptor);
209 
212  void insert(const std::string &name, unsigned majr, unsigned minr, unsigned offset, unsigned nbits);
213 
216  void insert(const RegisterDictionary*);
217 
220  void insert(const RegisterDictionary&);
221 
225  void resize(const std::string &name, unsigned new_nbits);
226 
231  RegisterDescriptor find(const std::string &name) const;
232 
237  RegisterDescriptor findOrThrow(const std::string &name) const;
238 
242  const std::string& lookup(RegisterDescriptor) const;
243 
250 
258  RegisterDescriptor findLargestRegister(unsigned major, unsigned minor, size_t maxWidth=0) const;
259 
268 
271  const Entries& get_registers() const;
272  Entries& get_registers();
277  RegisterDescriptors get_descriptors() const;
278 
280  unsigned firstUnusedMajor() const;
281 
283  unsigned firstUnusedMinor(unsigned majr) const;
284 
296  class SortBySize {
297  public:
298  enum Direction { ASCENDING, DESCENDING };
299  explicit SortBySize(Direction d=DESCENDING): direction(d) {}
300  bool operator()(RegisterDescriptor a, RegisterDescriptor b) const {
301  return ASCENDING==direction ?
302  a.nBits() < b.nBits() :
303  a.nBits() > b.nBits();
304  }
305  protected:
306  Direction direction;
307  };
308 
334  template<class Compare>
335  static RegisterDescriptors filter_nonoverlapping(RegisterDescriptors reglist,
336  Compare order=SortBySize(),
337  bool reconsider_parts=true);
338 
351  RegisterDescriptors get_largest_registers() const;
352 
364  RegisterDescriptors get_smallest_registers() const;
365 
368  void print(std::ostream&) const;
369  friend std::ostream& operator<<(std::ostream&, const RegisterDictionary&);
370 
372  size_t size() const { return forward.size(); }
373 };
374 
378 public:
380  explicit RegisterNames(const RegisterDictionary *dict=NULL)
381  : dflt_dict(dict), prefix("REG"), show_offset(-1), offset_prefix("@"), show_size(-1), size_prefix("+") {}
382 
385  std::string operator()(RegisterDescriptor, const RegisterDictionary *dict=NULL) const;
386 
388  std::string prefix;
389  std::string suffix;
391  std::string offset_prefix;
392  std::string offset_suffix;
393  int show_size;
394  std::string size_prefix;
395  std::string size_suffix;
396 };
397 
398 /*******************************************************************************************************************************
399  * Template definitions
400  *******************************************************************************************************************************/
401 
402 
403 
404 template<class Compare>
405 RegisterDictionary::RegisterDescriptors
406 RegisterDictionary::filter_nonoverlapping(RegisterDescriptors desc, Compare order, bool reconsider_parts)
407 {
408  RegisterDescriptors retval;
409  Map<std::pair<int/*major*/, int/*minor*/>, ExtentMap> have_bits; // the union of the bits of descriptors we will return
410 
411  // Process the descriptors in the order specified.
412  std::priority_queue<RegisterDescriptor, RegisterDescriptors, Compare> heap(order, desc);
413  while (!heap.empty()) {
414  const RegisterDescriptor cur_desc = heap.top();
415  heap.pop();
416  const std::pair<int, int> cur_majmin(cur_desc.majorNumber(), cur_desc.minorNumber());
417  const Extent cur_extent(cur_desc.offset(), cur_desc.nBits());
418  ExtentMap &have_extents = have_bits[cur_majmin];
419  if (have_extents.distinct(cur_extent)) {
420  // We're not returning any of these bits yet, so add the whole descriptor
421  retval.push_back(cur_desc);
422  have_extents.insert(cur_extent);
423  } else if (reconsider_parts) {
424  // We're already returning some (or all) of these bits. Split the cur_extent into sub-parts by subtracting the
425  // stuff we're already returning.
426  ExtentMap parts;
427  parts.insert(cur_extent);
428  parts.erase_ranges(have_extents);
429  for (ExtentMap::iterator pi=parts.begin(); pi!=parts.end(); ++pi) {
430  const Extent &part = pi->first;
431  RegisterDescriptor part_desc(cur_desc.majorNumber(), cur_desc.minorNumber(), part.first(), part.size());
432  heap.push(part_desc);
433  }
434  }
435  }
436  return retval;
437 }
438 
439 } // namespace
440 } // namespace
441 
442 #endif
443 #endif
int show_size
0=>never; positive=>always; negative=>when offset is non-zero
Definition: Registers.h:393
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_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:391
Rose::BinaryAnalysis::RegisterParts getAllParts() const
Returns all register parts.
static const RegisterDictionary * dictionary_i386_387()
Intel 80386 with 80387 math co-processor.
static const RegisterDictionary * dictionary_powerpc32()
PowerPC-32 registers.
const Entries & get_registers() const
Returns the list of all register definitions in the dictionary.
const std::string & lookup(RegisterDescriptor) const
Returns a register name for a given descriptor.
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:392
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:406
RegisterDescriptor findOrThrow(const std::string &name) const
Find a register by name.
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.
Main namespace for the ROSE library.
Holds a set of registers without regard for register boundaries.
Definition: RegisterParts.h:27
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:372
Compares number of bits in register descriptors.
Definition: Registers.h:296
RegisterNames(const RegisterDictionary *dict=NULL)
Constructor.
Definition: Registers.h:380
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:390
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:394
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:196
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.
static const RegisterDictionary * dictionary_Cil()
CIL 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.
std::string suffix
String to print at the very end of the generated name.
Definition: Registers.h:389
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:395
RegisterDescriptors get_smallest_registers() const
Returns a list of the smallest non-overlapping registers.
RegisterDescriptor find(const std::string &name) const
Find a register by name.
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:377
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:202
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:388
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:387
unsigned firstUnusedMinor(unsigned majr) const
Returns the first unused minor register number.
static const RegisterDictionary * dictionary_null()
Mostly empty dictionary for the null ISA.
iterator end()
End-item iterator.
Definition: rangemap.h:917
static const RegisterDictionary * dictionary_powerpc64()
PowerPC-64 registers.
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:37
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.