ROSE 0.11.145.147
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#include <rose_extent.h>
9#include <Map.h> // rose
10
11#include <Sawyer/SharedPointer.h>
12#include <Sawyer/SharedObject.h>
13
14#include <boost/serialization/access.hpp>
15#include <boost/serialization/map.hpp>
16#include <boost/serialization/string.hpp>
17#include <map>
18#include <ostream>
19#include <queue>
20#include <string>
21#include <vector>
22
23namespace Rose {
24namespace BinaryAnalysis {
25
47
49 // Types
51public:
54
56 using Entries = std::map<std::string/*name*/, RegisterDescriptor>;
57
60
61private:
62 // a descriptor can have more than one name
63 using Reverse = std::map<RegisterDescriptor, std::vector<std::string>>;
64
66 // Data members
68private:
69 std::string name_; // name of the dictionary, usually an architecture name like 'i386'
70 Entries forward_; // N:1 forward lookup from name to descriptor
71 Reverse reverse_; // 1:N reverse lookup from descriptor to names
72 RegisterDescriptor instructionPointer_; // optional special instruction pointer register
73 RegisterDescriptor stackPointer_; // optional special stack pointer register
74 RegisterDescriptor stackFrame_; // optional special stack frame pointer register
75 RegisterDescriptor stackSegment_; // optional special stack segment register
76 RegisterDescriptor callReturn_; // optional special call return address register
77
79 // Serialization
81#ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
82private:
83 friend class boost::serialization::access;
84
85 template<class S>
86 void serialize(S &s, const unsigned /*version*/) {
87 s & BOOST_SERIALIZATION_NVP(name_);
88 s & BOOST_SERIALIZATION_NVP(forward_);
89 s & BOOST_SERIALIZATION_NVP(reverse_);
90 }
91#endif
92
94 // Constructors and destructor
96protected:
98
99 RegisterDictionary(const std::string &name);
100
102
103public:
105 static Ptr instance(const std::string &name);
106
109
111 // Properties
113public:
114
118 const std::string &name() const;
119 void name(const std::string&);
123 const Entries& registers() const;
124
126 size_t size() const;
127
129 // Mutators
131public:
132
137 void insert(const std::string &name, RegisterDescriptor);
138
143 void insert(const std::string &name, unsigned majr, unsigned minr, unsigned offset, unsigned nbits);
144
149 void insert(const Ptr&);
150
156 void resize(const std::string &name, unsigned new_nbits);
157
159 // Lookup
161public:
166 RegisterDescriptor find(const std::string &name) const;
167
172 RegisterDescriptor findOrThrow(const std::string &name) const;
173
178 const std::string& lookup(RegisterDescriptor) const;
179
185
193 RegisterDescriptor findLargestRegister(unsigned major, unsigned minor, size_t maxWidth=0) const;
194
196 // Register names
198
203
208 std::string nameOrQuad(RegisterDescriptor) const;
209
214 std::string nameAndQuad(RegisterDescriptor) const;
215
220 std::string quadAndName(RegisterDescriptor) const;
221
223 // Special registers
225public:
233 void instructionPointerRegister(const std::string&);
243 void stackPointerRegister(const std::string&);
253 void stackFrameRegister(const std::string&);
263 void stackSegmentRegister(const std::string&);
273 void callReturnRegister(const std::string&);
277 // Other queries
279
288
294
296 unsigned firstUnusedMajor() const;
297
299 unsigned firstUnusedMinor(unsigned majr) const;
300
313 public:
314 enum Direction { ASCENDING, DESCENDING };
315 explicit SortBySize(Direction d=DESCENDING): direction(d) {}
316 bool operator()(RegisterDescriptor a, RegisterDescriptor b) const {
317 return ASCENDING==direction ?
318 a.nBits() < b.nBits() :
319 a.nBits() > b.nBits();
320 }
321 protected:
322 Direction direction;
323 };
324
351 template<class Compare>
353 Compare order = SortBySize(),
354 bool reconsiderParts = true);
355
371
387
389 // Debugging
391
398 void print(std::ostream&) const;
399 friend std::ostream& operator<<(std::ostream&, const RegisterDictionary&);
401};
402
403std::ostream&
404operator<<(std::ostream&, const RegisterDictionary&);
405
406template<class Compare>
408RegisterDictionary::filterNonoverlapping(RegisterDescriptors desc, Compare order, bool reconsiderParts) {
409 RegisterDescriptors retval;
410 Map<std::pair<int/*major*/, int/*minor*/>, ExtentMap> have_bits; // the union of the bits of descriptors we will return
411
412 // Process the descriptors in the order specified.
413 std::priority_queue<RegisterDescriptor, RegisterDescriptors, Compare> heap(order, desc);
414 while (!heap.empty()) {
415 const RegisterDescriptor cur_desc = heap.top();
416 heap.pop();
417 const std::pair<int, int> cur_majmin(cur_desc.majorNumber(), cur_desc.minorNumber());
418 const Extent cur_extent(cur_desc.offset(), cur_desc.nBits());
419 ExtentMap &have_extents = have_bits[cur_majmin];
420 if (have_extents.distinct(cur_extent)) {
421 // We're not returning any of these bits yet, so add the whole descriptor
422 retval.push_back(cur_desc);
423 have_extents.insert(cur_extent);
424 } else if (reconsiderParts) {
425 // We're already returning some (or all) of these bits. Split the cur_extent into sub-parts by subtracting the
426 // stuff we're already returning.
427 ExtentMap parts;
428 parts.insert(cur_extent);
429 parts.erase_ranges(have_extents);
430 for (ExtentMap::iterator pi = parts.begin(); pi != parts.end(); ++pi) {
431 const Extent &part = pi->first;
432 RegisterDescriptor part_desc(cur_desc.majorNumber(), cur_desc.minorNumber(), part.first(), part.size());
433 heap.push(part_desc);
434 }
435 }
436 }
437 return retval;
438}
439
440} // namespace
441} // namespace
442
443#endif
444#endif
Extends std::map with methods that return optional values.
Definition util/Map.h:13
bool distinct(const Range &r) const
Determines if a range map does not contain any part of the specified range.
Definition rangemap.h:1253
iterator begin()
First-item iterator.
Definition rangemap.h:901
void erase_ranges(const OtherMap &other)
Erase ranges from this map.
Definition rangemap.h:1177
iterator end()
End-item iterator.
Definition rangemap.h:913
iterator insert(Range new_range, Value new_value=Value(), bool make_hole=true)
Insert a range/value pair into the map.
Definition rangemap.h:1188
A contiguous range of values.
Definition rangemap.h:50
void first(const Value &first)
Accessor for the first value of a range.
Definition rangemap.h:103
Value size() const
Returns the number of values represented by the range.
Definition rangemap.h:147
Describes (part of) a physical CPU register.
size_t offset() const
Property: Offset to least-significant bit.
unsigned minorNumber() const
Property: Minor number.
unsigned majorNumber() const
Property: Major number.
size_t nBits() const
Property: Size in bits.
Compares number of bits in register descriptors.
Defines registers available for a particular architecture.
RegisterDescriptor stackFrameRegister() const
Property: The register that ponts to the stack frame.
RegisterDescriptors getSmallestRegisters() const
Returns a list of the smallest non-overlapping registers.
void name(const std::string &)
Property: Architecture name.
void stackPointerRegister(RegisterDescriptor)
Property: The register that points to the stack.
Rose::BinaryAnalysis::RegisterParts getAllParts() const
Returns all register parts.
void stackFrameRegister(RegisterDescriptor)
Property: The register that ponts to the stack frame.
void stackSegmentRegister(RegisterDescriptor)
Property: The segment register for accessing the stack.
const std::string & name() const
Property: Architecture name.
static Ptr instance(const std::string &name)
Allocating constructor for an empty dictionary.
void insert(const std::string &name, RegisterDescriptor)
Insert a definition into the dictionary.
bool exists(RegisterDescriptor) const
Determine if a register descriptor exists.
void instructionPointerRegister(RegisterDescriptor)
Property: The register that points to instructions.
static Ptr instanceNull()
Mostly empty dictionary for the null ISA.
RegisterDescriptor findOrThrow(const std::string &name) const
Find a register by name.
void stackSegmentRegister(const std::string &)
Property: The segment register for accessing the stack.
Rose::BinaryAnalysis::RegisterDescriptors RegisterDescriptors
List of register descriptors in dictionary.
std::string quadAndName(RegisterDescriptor) const
Quad and name.
void callReturnRegister(const std::string &)
Property: The register that holds the return address for a function.
unsigned firstUnusedMajor() const
Returns the first unused major register number.
Sawyer::Optional< std::string > name(RegisterDescriptor) const
Name of the register or nothing.
RegisterDescriptors getDescriptors() const
Returns the list of all register descriptors.
void stackFrameRegister(const std::string &)
Property: The register that ponts to the stack frame.
std::string nameAndQuad(RegisterDescriptor) const
Name and quad.
void instructionPointerRegister(const std::string &)
Property: The register that points to instructions.
void callReturnRegister(RegisterDescriptor)
Property: The register that holds the return address for a function.
void print(std::ostream &) const
Prints the contents of this register dictionary.
const std::string & lookup(RegisterDescriptor) const
Returns a register name for a given descriptor.
void insert(const std::string &name, unsigned majr, unsigned minr, unsigned offset, unsigned nbits)
Insert a definition into the dictionary.
std::map< std::string, RegisterDescriptor > Entries
List of registers in dictionary.
RegisterDescriptor instructionPointerRegister() const
Property: The register that points to instructions.
static RegisterDescriptors filterNonoverlapping(RegisterDescriptors reglist, Compare order=SortBySize(), bool reconsiderParts=true)
Returns the list of non-overlapping registers or register parts.
RegisterDescriptor callReturnRegister() const
Property: The register that holds the return address for a function.
void resize(const std::string &name, unsigned new_nbits)
Changes the size of a register.
void stackPointerRegister(const std::string &)
Property: The register that points to the stack.
const Entries & registers() const
Returns the list of all register definitions in the dictionary.
size_t size() const
Return the number of entries in the dictionary.
friend std::ostream & operator<<(std::ostream &, const RegisterDictionary &)
Prints the contents of this register dictionary.
RegisterDescriptor findLargestRegister(unsigned major, unsigned minor, size_t maxWidth=0) const
Finds the first largest register with specified major and minor number.
RegisterDescriptor stackPointerRegister() const
Property: The register that points to the stack.
unsigned firstUnusedMinor(unsigned majr) const
Returns the first unused minor register number.
RegisterDescriptor stackSegmentRegister() const
Property: The segment register for accessing the stack.
RegisterDescriptors getLargestRegisters() const
Returns a list of the largest non-overlapping registers.
void insert(const Ptr &)
Inserts definitions from another dictionary into this dictionary.
RegisterDescriptor find(const std::string &name) const
Find a register by name.
std::string nameOrQuad(RegisterDescriptor) const
Name or quad.
Holds a set of registers without regard for register boundaries.
Holds a value or nothing.
Definition Optional.h:56
Base class for reference counted objects.
Sawyer::SharedPointer< RegisterDictionary > RegisterDictionaryPtr
Reference counting pointer.
std::vector< RegisterDescriptor > RegisterDescriptors
List of register descriptors in dictionary.
The ROSE library.