ROSE  0.11.87.0
Utility.h
1 #ifndef ROSE_BinaryAnalysis_Partitioner2_Utility_H
2 #define ROSE_BinaryAnalysis_Partitioner2_Utility_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BinaryAnalysis/Partitioner2/AddressUsageMap.h>
7 #include <Rose/BinaryAnalysis/Partitioner2/BasicBlock.h>
8 #include <Rose/BinaryAnalysis/Partitioner2/DataBlock.h>
9 #include <Rose/BinaryAnalysis/Partitioner2/Function.h>
10 #include <Rose/CommandLine/IntervalParser.h>
11 
12 #include <Rose/Diagnostics.h>
13 
14 #include <algorithm>
15 #include <ostream>
16 
17 namespace Rose {
18 namespace BinaryAnalysis {
19 namespace Partitioner2 {
20 
22 void initDiagnostics();
23 
24 bool sortBasicBlocksByAddress(const BasicBlock::Ptr&, const BasicBlock::Ptr&);
25 bool sortDataBlocks(const DataBlock::Ptr&, const DataBlock::Ptr&);
26 bool sortFunctionsByAddress(const Function::Ptr&, const Function::Ptr&);
27 bool sortFunctionNodesByAddress(const SgAsmFunction*, const SgAsmFunction*);
28 bool sortByExpression(const BasicBlock::Successor&, const BasicBlock::Successor&);
29 bool sortBlocksForAst(SgAsmBlock*, SgAsmBlock*);
30 bool sortInstructionsByAddress(SgAsmInstruction*, SgAsmInstruction*);
31 
32 template<class Container, class Comparator>
33 static bool
34 isSorted(const Container &container, Comparator sorted, bool distinct=true) {
35  typename Container::const_iterator current = container.begin();
36  if (current!=container.end()) {
37  typename Container::const_iterator next = current;
38  while (++next != container.end()) {
39  if ((distinct && !sorted(*current, *next)) || sorted(*next, *current))
40  return false;
41  ++current;
42  }
43  }
44  return true;
45 }
46 
47 template<class Container, class Value, class Comparator>
48 typename Container::const_iterator
49 lowerBound(const Container &container, const Value &item, Comparator cmp) {
50  return std::lower_bound(container.begin(), container.end(), item, cmp);
51 }
52 template<class Container, class Value, class Comparator>
53 typename Container::iterator
54 lowerBound(Container &container, const Value &item, Comparator cmp) {
55  return std::lower_bound(container.begin(), container.end(), item, cmp);
56 }
57 
58 // Return true if two container elements are equal according to the sorting comparator
59 template<class Value, class Comparator>
60 bool
61 equalUnique(const Value &a, const Value &b, Comparator cmp) {
62  return !cmp(a, b) && !cmp(b, a); // equal iff neither one is less than the other
63 }
64 
65 // Insert an item into a sorted container if it doesn't exist yet in the container. Returns true iff inserted.
66 template<class Container, class Value, class Comparator>
67 bool
68 insertUnique(Container &container, const Value &item, Comparator cmp) {
69  ASSERT_require(!ROSE_PARTITIONER_EXPENSIVE_CHECKS || isSorted(container, cmp, true)); // unique, sorted items
70  typename Container::iterator lb = lowerBound(container, item, cmp);
71  if (lb==container.end() || !equalUnique(*lb, item, cmp)) {
72  container.insert(lb, item);
73  ASSERT_require(!ROSE_PARTITIONER_EXPENSIVE_CHECKS || isSorted(container, cmp, true));
74  return true;
75  }
76  return false;
77 }
78 
79 // Insert an intem into a sorted continer, replacing any existing item that compares equal to it.
80 template<class Container, class Value, class Comparator>
81 void
82 replaceOrInsert(Container &container, const Value &item, Comparator cmp) {
83  ASSERT_require(!ROSE_PARTITIONER_EXPENSIVE_CHECKS || isSorted(container, cmp, true)); // unique, sorted items
84  typename Container::iterator lb = lowerBound(container, item, cmp);
85  if (lb == container.end() || !equalUnique(*lb, item, cmp)) {
86  container.insert(lb, item);
87  } else {
88  *lb = item;
89  }
90  ASSERT_require(!ROSE_PARTITIONER_EXPENSIVE_CHECKS || isSorted(container, cmp, true));
91 }
92 
93 // Erase an item from a sorted container if it doesn't exist yet in the container. Returns true iff erased.
94 template<class Container, class Value, class Comparator>
95 bool
96 eraseUnique(Container &container, const Value &item, Comparator cmp) {
97  ASSERT_require(!ROSE_PARTITIONER_EXPENSIVE_CHECKS || isSorted(container, cmp, true)); // unique, sorted items
98  typename Container::iterator lb = lowerBound(container, item, cmp);
99  if (lb!=container.end() && equalUnique(*lb, item, cmp)) {
100  container.erase(lb);
101  return true;
102  }
103  return false;
104 }
105 
106 // Check existence of an item in a sorted container. Returns true iff the item exists.
107 template<class Container, class Value, class Comparator>
108 bool
109 existsUnique(const Container &container, const Value &item, Comparator cmp) {
110  if (item==NULL)
111  return false;
112  ASSERT_require(!ROSE_PARTITIONER_EXPENSIVE_CHECKS || isSorted(container, cmp, true)); // unique, sorted items
113  typename Container::const_iterator lb = lowerBound(container, item, cmp);
114  if (lb==container.end() || cmp(*lb, item) || cmp(item, *lb))
115  return false;
116  return true;
117 }
118 
119 // Retrieve an item from a sorted container
120 template<class Container, class Value, class Comparator>
122 getUnique(const Container &container, const Value &item, Comparator cmp) {
123  if (item==NULL)
124  return Sawyer::Nothing();
125  ASSERT_require(!ROSE_PARTITIONER_EXPENSIVE_CHECKS || isSorted(container, cmp, true)); // unique, sorted items
126  typename Container::const_iterator lb = lowerBound(container, item, cmp);
127  if (lb==container.end() || cmp(*lb, item) || cmp(item, *lb))
128  return Sawyer::Nothing();
129  return *lb;
130 }
131 
132 template<class Container, class Comparator>
133 bool
134 isSupersetUnique(const Container &sup, const Container &sub, Comparator lessThan) {
135  ASSERT_require(!ROSE_PARTITIONER_EXPENSIVE_CHECKS || isSorted(sup, lessThan, true)); // unique, sorted items
136  ASSERT_require(!ROSE_PARTITIONER_EXPENSIVE_CHECKS || isSorted(sub, lessThan, true)); // unique, sorted items
137  typename Container::const_iterator isup = sup.begin(), isub = sub.begin();
138  while (isup != sup.end() && isub != sub.end()) {
139  while (isup != sup.end() && lessThan(*isup, *isub))
140  ++isup;
141  if (isup == sup.end() || lessThan(*isub, *isup))
142  return false;
143  ++isup, ++isub;
144  }
145  return isub == sub.end();
146 }
147 
148 std::ostream& operator<<(std::ostream&, const AddressUser&);
149 std::ostream& operator<<(std::ostream&, const AddressUsers&);
150 std::ostream& operator<<(std::ostream&, const AddressUsageMap&);
151 
164 
165 protected:
168  : Super(valueSaver) {}
169 public:
172 
174  static Ptr instance() {
175  return Ptr(new AddressIntervalParser);
176  }
177 
180  return Ptr(new AddressIntervalParser(valueSaver));
181  }
182 
184  static std::string docString();
185 };
186 
187 AddressIntervalParser::Ptr addressIntervalParser(AddressInterval &storage);
188 AddressIntervalParser::Ptr addressIntervalParser(std::vector<AddressInterval> &storage);
189 AddressIntervalParser::Ptr addressIntervalParser(AddressIntervalSet &storage);
190 AddressIntervalParser::Ptr addressIntervalParser();
191 
193 class Trigger {
194 public:
195  typedef AddressInterval SizeInterval; // okay to use 64-bit integers for the counters
196  struct Settings {
197  SizeInterval when; // when to trigger based on nCalls_
198  Settings(): when(0) {}
199  };
200 private:
201  size_t nCalls_; // number of times called
202  Settings settings_;
203 public:
205  Trigger(): nCalls_(0) {}
206 
208  explicit Trigger(const Settings &settings): nCalls_(0), settings_(settings) {}
209 
211  Trigger(size_t nSkip, size_t nTimes): nCalls_(0) {
212  settings_.when = nTimes ? SizeInterval::baseSize(nSkip, nTimes) : SizeInterval();
213  }
214 
216  static Trigger once() { return Trigger(0, 1); }
217 
219  static Trigger always() { return Trigger(0, size_t(-1)); }
220 
222  static Trigger never() { return Trigger(); }
223 
225  bool isArmed() const { return !settings_.when.isEmpty() && nCalls_<=settings_.when.greatest(); }
226 
228  bool shouldTrigger() { return settings_.when.isContaining(nCalls_++); }
229 
231  size_t nCalls() const { return nCalls_; }
232 
234  void reset() { nCalls_ = 0; }
235 
237  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
238 
240  static std::string docString();
241 };
242 
244 size_t serialNumber();
245 
246 
247 
248 } // namespace
249 } // namespace
250 } // namespace
251 
252 #endif
253 #endif
Instruction basic block.
const ValueSaver::Ptr valueSaver() const
Property: functor responsible for saving a parsed value in user storage.
static Ptr instance()
Default allocating constructor.
Definition: Utility.h:174
bool isEmpty() const
True if interval is empty.
Definition: Interval.h:219
Trigger(const Settings &settings)
Armed for triggering when number of calls falls within when.
Definition: Utility.h:208
Base class for machine instructions.
Collection of streams.
Definition: Message.h:1606
Sawyer::SharedPointer< AddressIntervalParser > Ptr
Shared-ownership pointer to an AddressIntervalParser.
Definition: Utility.h:171
ROSE_DLL_API Sawyer::Message::Facility mlog
Diagnostic facility for the ROSE library as a whole.
Sawyer::SharedPointer< DataBlock > Ptr
Shared pointer to a data block.
Definition: DataBlock.h:34
A collection of related switch declarations.
Represents a synthesized function.
size_t nCalls() const
Number of times called.
Definition: Utility.h:231
bool isContaining(const Interval &other) const
Containment predicate.
Definition: Interval.h:238
Main namespace for the ROSE library.
static Interval baseSize(rose_addr_t lo, rose_addr_t size)
Construct an interval from one endpoint and a size.
Definition: Interval.h:162
size_t serialNumber()
Return the next serial number.
Trigger based on number of times called.
Definition: Utility.h:193
static std::string docString()
Documentation for command-line switches.
bool isArmed() const
True if trigger is armed.
Definition: Utility.h:225
Trigger()
Trigger armed for single call.
Definition: Utility.h:205
static Ptr instance(const Sawyer::CommandLine::ValueSaver::Ptr &valueSaver)
Allocating constructor.
Definition: Utility.h:179
Sawyer::SharedPointer< BasicBlock > Ptr
Shared pointer to a basic block.
Definition: BasicBlock.h:134
void initDiagnostics()
Initialize diagnostics.
static Sawyer::CommandLine::SwitchGroup switches(Settings &)
Command-line switches to initialize settings.
static std::string docString()
Runtime documentation.
Represents no value.
Definition: Optional.h:32
T greatest() const
Returns upper limit.
Definition: Interval.h:213
static Trigger always()
Armed to always trigger.
Definition: Utility.h:219
static Trigger never()
Armed to never trigger.
Definition: Utility.h:222
FunctionPtr Ptr
Shared-ownership pointer for function.
Definition: Function.h:51
bool shouldTrigger()
Increment calls and return true if triggering.
Definition: Utility.h:228
static Trigger once()
Armed for one call.
Definition: Utility.h:216
void reset()
Reset number of calls to zero.
Definition: Utility.h:234
Trigger(size_t nSkip, size_t nTimes)
Armed for triggering after nSkip calls but not more than nTimes times.
Definition: Utility.h:211