ROSE  0.11.145.0
AddressUsageMap.h
1 #ifndef ROSE_BinaryAnalysis_Partitioner2_AddressUsageMap_H
2 #define ROSE_BinaryAnalysis_Partitioner2_AddressUsageMap_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 #include <Rose/BinaryAnalysis/Partitioner2/BasicTypes.h>
6 
7 #include <AstSerialization.h> // rose
8 
9 #include <Sawyer/IntervalMap.h>
10 #include <Sawyer/IntervalSet.h>
11 #include <Sawyer/Optional.h>
12 
13 #include <boost/serialization/access.hpp>
14 #include <boost/serialization/vector.hpp>
15 
16 #include <algorithm>
17 #include <ostream>
18 #include <string>
19 
20 namespace Rose {
21 namespace BinaryAnalysis {
22 namespace Partitioner2 {
23 
25 // AddressUser
27 
33 class AddressUser {
34  SgAsmInstruction *insn_;
35  std::vector<BasicBlockPtr> bblocks_; // sorted and unique
36  DataBlockPtr dblock_;
37 
38 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
39 private:
40  friend class boost::serialization::access;
41 
42  template<class S>
43  void serialize(S &s, const unsigned version) {
44  transferAst(s, insn_);
45  s & BOOST_SERIALIZATION_NVP(bblocks_);
46  if (version < 1) {
47  ASSERT_not_reachable("Rose::BinaryAnalysis::Partitioner2::AddressUser version 0 no longer supported");
48  } else {
49  s & BOOST_SERIALIZATION_NVP(dblock_);
50  }
51  }
52 
53 #endif
54 
55 public:
57  AddressUser();
58 
65 
67  explicit AddressUser(const DataBlockPtr&);
68 
69  ~AddressUser();
70 
75  rose_addr_t address() const;
76 
78  bool isBasicBlock() const;
79 
81  bool isDataBlock() const;
82 
86  bool isEmpty() const;
87 
91  SgAsmInstruction* insn() const;
92 
98 
104  const std::vector<BasicBlockPtr>& basicBlocks() const;
105 
107  void insertBasicBlock(const BasicBlockPtr &bblock);
108 
110  void eraseBasicBlock(const BasicBlockPtr &bblock);
111 
115  DataBlockPtr dataBlock() const;
116 
122  BasicBlockPtr isBlockEntry() const;
123 
129  bool operator==(const AddressUser &other) const;
130 
137  bool operator<(const AddressUser&) const;
138 
140  void print(std::ostream&) const;
141 
145  bool isConsistent() const;
146 
150  operator bool() const {
151  return !isEmpty();
152  }
153 };
154 
155 
157 // AddressUsers
159 
166  std::vector<AddressUser> users_; // sorted
167 
168 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
169 private:
170  friend class boost::serialization::access;
171 
172  template<class S>
173  void serialize(S &s, const unsigned /*version*/) {
174  s & BOOST_SERIALIZATION_NVP(users_);
175  }
176 #endif
177 
178 public:
180  AddressUsers();
181  ~AddressUsers();
182 
184  explicit AddressUsers(SgAsmInstruction *insn, const BasicBlockPtr&);
185 
187  explicit AddressUsers(const DataBlockPtr&);
188 
197  SgAsmInstruction* instructionExists(rose_addr_t va) const;
208  BasicBlockPtr basicBlockExists(rose_addr_t va) const;
219  DataBlockPtr dataBlockExists(rose_addr_t va, rose_addr_t size) const;
230  AddressUser findInstruction(rose_addr_t va) const;
242  AddressUser findBasicBlock(rose_addr_t va) const;
252  AddressUser findDataBlock(const DataBlockPtr&) const;
253  AddressUser findDataBlock(rose_addr_t va, rose_addr_t size) const;
261 
267 
269  void insert(const AddressUsers&);
270 
276 
282 
286  static bool selectAllUsers(const AddressUser&);
287 
292  static bool selectBasicBlocks(const AddressUser&);
293 
298  static bool selectDataBlocks(const AddressUser&);
299 
303  template<class UserPredicate>
304  AddressUsers select(UserPredicate predicate) const {
305  AddressUsers retval;
306  for (const AddressUser &user: users_) {
307  if (predicate(user))
308  retval.users_.push_back(user);
309  }
310  return retval;
311  }
312 
316  const std::vector<AddressUser>& addressUsers() const;
317 
322 
327 
333  std::vector<SgAsmInstruction*> instructions() const;
334 
340  std::vector<BasicBlockPtr> instructionOwners() const;
341 
345  std::vector<DataBlockPtr> dataBlocks() const;
346 
348  size_t size() const;
349 
353  bool isEmpty() const;
354 
356  AddressUsers intersection(const AddressUsers&) const;
357 
359  AddressUsers union_(const AddressUsers&) const;
360 
362  bool operator==(const AddressUsers &other) const;
363 
365  void print(std::ostream&) const;
366 
370  bool isConsistent() const;
371 };
372 
373 
375 // AddressUsageMap
377 
385  Map map_;
386 
387 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
388 private:
389  friend class boost::serialization::access;
390 
391  template<class S>
392  void serialize(S &s, const unsigned /*version*/) {
393  s & BOOST_SERIALIZATION_NVP(map_);
394  }
395 #endif
396 
397 public:
398  AddressUsageMap();
399  ~AddressUsageMap();
400 
401 public:
406  bool isEmpty() const;
407 
409  void clear();
410 
414  size_t size() const;
415 
420  AddressInterval hull() const;
421 
425  AddressIntervalSet extent() const;
426 
432  bool exists(rose_addr_t) const;
433 
441  bool anyExists(const AddressInterval&) const;
442  bool anyExists(const AddressIntervalSet&) const;
452  AddressIntervalSet unusedExtent(size_t nBits) const;
462  AddressInterval nextUnused(rose_addr_t minVa) const;
463 
470  SgAsmInstruction* instructionExists(rose_addr_t va) const;
482  BasicBlockPtr basicBlockExists(rose_addr_t startOfBlock) const;
492  DataBlockPtr dataBlockExists(rose_addr_t va, rose_addr_t size) const;
503  AddressUser findInstruction(rose_addr_t va) const;
515  AddressUser findBasicBlock(rose_addr_t va) const;
525  AddressUser findDataBlock(const DataBlockPtr&) const;
526  AddressUser findDataBlock(rose_addr_t va, rose_addr_t size) const;
535 
541 
548 
553 
562  AddressUsers spanning(const AddressInterval&) const;
563 
564  template<class UserPredicate>
565  AddressUsers spanning(const AddressInterval &interval, UserPredicate userPredicate) const {
566  AddressUsers retval;
567  size_t nIters = 0;
568  for (const Map::Node &node: map_.findAll(interval)) {
569  AddressUsers users = node.value().select(userPredicate);
570  retval = 0==nIters++ ? users : retval.intersection(users);
571  if (retval.isEmpty())
572  break;
573  }
574  return retval;
575  }
588 
589  template<class UserPredicate>
590  AddressUsers overlapping(const AddressInterval &interval, UserPredicate userPredicate) const {
591  AddressUsers retval;
592  for (const Map::Node &node: map_.findAll(interval))
593  retval.insert(node.value().select(userPredicate));
594  return retval;
595  }
607  // FIXME[Robb P. Matzke 2014-08-26]: not implemented yet
609 
610  //template<class UserPredicate>
611  //AddressUsers containedIn(const AddressInterval &interval, UserPredicate userPredicate) const {...}
619  Sawyer::Optional<rose_addr_t> leastUnmapped(rose_addr_t startVa) const;
620 
624  void print(std::ostream&, const std::string &prefix="") const;
625 
629  void checkConsistency() const;
630 };
631 
632 } // namespace
633 } // namespace
634 } // namespace
635 
636 // Class versions must be at global scope
638 
639 #endif
640 #endif
SgAsmInstruction * instructionExists(SgAsmInstruction *) const
Determines whether the specified instruction or an equivalent exists.
AddressUser insertDataBlock(const DataBlockPtr &)
Insert the data block.
void insertBasicBlock(const BasicBlockPtr &bblock)
Add another basic block to the set of basic blocks.
std::vector< SgAsmInstruction * > instructions() const
Returns all instructions.
size_t size() const
Number of addresses represented by the map.
bool isEmpty() const
Determines whether this address user list is empty.
void insert(const AddressUsers &)
Insert one set of address users into another.
DataBlockPtr dataBlock() const
Returns the data block.
static bool selectDataBlocks(const AddressUser &)
Selector to select data blocks.
AddressUsers overlapping(const AddressInterval &) const
Users that overlap the interval.
void clear()
Reset map to initial empty state.
AddressUser findBasicBlock(const BasicBlockPtr &) const
Find an AddressUser record for the specified basic block, or equivalent.
BasicBlockPtr isBlockEntry() const
Determines if this user is a first instruction of a basic block.
AddressUser()
Default constructed user is empty.
rose_addr_t address() const
Address of user.
Base class for machine instructions.
BasicBlockPtr basicBlockExists(const BasicBlockPtr &) const
Determine if a basic block exists.
AddressUser insertInstruction(SgAsmInstruction *, const BasicBlockPtr &)
Insert an instruction/basic block pair.
static bool selectBasicBlocks(const AddressUser &)
Selector to select instructions and basic blocks.
void print(std::ostream &, const std::string &prefix="") const
Dump the contents of this AUM to a stream.
AddressUsers union_(const AddressUsers &) const
Computes the union of this list with another.
DataBlockPtr dataBlockExists(const DataBlockPtr &) const
Determines if a data block exists.
AddressUsers spanning(const AddressInterval &interval, UserPredicate userPredicate) const
Find address users that span the entire interval.
Main namespace for the ROSE library.
const std::vector< AddressUser > & addressUsers() const
Return all address users.
SgAsmInstruction * eraseInstruction(SgAsmInstruction *, const BasicBlockPtr &)
Remove the specified instruction/basic block pair.
bool isConsistent() const
Perform logic consistency checks.
AddressInterval nextUnused(rose_addr_t minVa) const
Next unused address interval.
AddressUser findDataBlock(const DataBlockPtr &) const
Find an AddressUser record for the specified data block, or equivalent.
bool isBasicBlock() const
Predicate returning true if user is a basic block or instruction.
std::vector< DataBlockPtr > dataBlocks() const
Returns all data blocks.
SgAsmInstruction * insn() const
Return the instruction.
AddressUsers overlapping(const AddressInterval &interval, UserPredicate userPredicate) const
Users that overlap the interval.
AddressUsers instructionUsers() const
Returns all instruction users.
bool operator==(const AddressUsers &other) const
True if two lists are equal.
AddressUser findBasicBlock(const BasicBlockPtr &) const
Find an AddressUser record for the specified basic block, or equivalent.
void print(std::ostream &) const
Prints pairs space separated on a single line.
void eraseBasicBlock(const BasicBlockPtr &bblock)
Remove a basic block from the set of basic blocks.
void print(std::ostream &) const
Print the pair on one line.
AddressIntervalSet unusedExtent(size_t nBits) const
Addresses not represented.
BasicBlockPtr basicBlockExists(const BasicBlockPtr &) const
Determines whether the specified basic block or an equivalent exists.
void checkConsistency() const
Check invariants.
bool isDataBlock() const
Predicate returning true if user is a data block.
const std::vector< BasicBlockPtr > & basicBlocks() const
Returns all basic blocks to which this instruction belongs.
AddressInterval hull() const
Minimum and maximum used addresses.
SgAsmInstruction * instructionExists(SgAsmInstruction *) const
Determines whether the specified instruction or an equivalent exists.
BasicBlockPtr firstBasicBlock() const
Returns an arbitrary basic block.
boost::iterator_range< NodeIterator > findAll(const Interval &interval)
Finds all nodes overlapping the specified interval.
Definition: IntervalMap.h:390
bool operator<(const AddressUser &) const
Compare two users for sorting.
Sawyer::Optional< rose_addr_t > leastUnmapped(rose_addr_t startVa) const
Returns the least unmapped address with specified lower limit.
DataBlockPtr dataBlockExists(const DataBlockPtr &) const
Determines whether the specified data block or an equivalent exists.
AddressUsers select(UserPredicate predicate) const
Selects certain users from a list.
bool exists(rose_addr_t) const
Predicate to determine whether an address is used.
DataBlockPtr eraseDataBlock(const DataBlockPtr &)
Remove the specified data block.
AddressUser findInstruction(SgAsmInstruction *) const
Find an AddressUser record for the specified instruction, or equivalent.
AddressUser findDataBlock(const DataBlockPtr &) const
Find an AddressUser record for the specified data block, or equivalent.
SgAsmInstruction * eraseInstruction(SgAsmInstruction *, const BasicBlockPtr &)
Erase an instruction/basic block pair from this list.
AddressUsers intersection(const AddressUsers &) const
Computes the intersection of this list with another.
AddressIntervalSet extent() const
Addresses represented.
bool isEmpty() const
True if this object was default constructed.
bool operator==(const AddressUser &other) const
Compare two users for equality.
AddressUser findInstruction(SgAsmInstruction *) const
Find an AddressUser record for the specified instruction, or equivalent.
bool isEmpty() const
Determines whether a map is empty.
bool anyExists(const AddressInterval &) const
Predicate to determine whether any of the specified addresses are used.
std::vector< BasicBlockPtr > instructionOwners() const
Returns all basic blocks.
size_t size() const
Number of address users.
static bool selectAllUsers(const AddressUser &)
Selector to select all users.
bool isConsistent() const
Check logical consistency.
AddressUsers dataBlockUsers() const
Returns all data block users.
AddressUsers spanning(const AddressInterval &) const
Find address users that span the entire interval.
AddressUser insertInstruction(SgAsmInstruction *, const BasicBlockPtr &)
Insert the instruction along with an owning basic block.
DataBlockPtr eraseDataBlock(const DataBlockPtr &)
Erase a data block from this list.
AddressUsers containedIn(const AddressInterval &) const
Users that are fully contained in the interval.
Type for stored nodes.
Definition: Sawyer/Map.h:89
AddressUser insertDataBlock(const DataBlockPtr &)
Insert a data block.