ROSE  0.11.2.0
Utility.h
1 #ifndef ROSE_Partitioner2_Utility_H
2 #define ROSE_Partitioner2_Utility_H
3 
4 #include <rosePublicConfig.h>
5 #ifdef ROSE_BUILD_BINARY_ANALYSIS_SUPPORT
6 
7 #include <Partitioner2/AddressUsageMap.h>
8 #include <Partitioner2/BasicBlock.h>
9 #include <Partitioner2/DataBlock.h>
10 #include <Partitioner2/Function.h>
11 
12 #include "Diagnostics.h"
13 
14 #include <algorithm>
15 #include <ostream>
16 
17 namespace Rose {
18 namespace BinaryAnalysis {
19 namespace Partitioner2 {
20 
21 extern Sawyer::Message::Facility mlog;
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 
163 protected:
166  : Sawyer::CommandLine::ValueParser(valueSaver) {}
167 public:
170 
171  static Ptr instance() {
172  return Ptr(new AddressIntervalParser);
173  }
174 
175  static Ptr instance(const Sawyer::CommandLine::ValueSaver::Ptr &valueSaver) {
176  return Ptr(new AddressIntervalParser(valueSaver));
177  }
178 
179  static std::string docString();
180 
185  static AddressInterval parse(const char *input, const char **rest);
186 
193  static AddressInterval parse(const std::string &input);
194 
195 private:
196  virtual Sawyer::CommandLine::ParsedValue operator()(const char *input, const char **rest,
197  const Sawyer::CommandLine::Location &loc) ROSE_OVERRIDE;
198 };
199 
200 AddressIntervalParser::Ptr addressIntervalParser(AddressInterval &storage);
201 AddressIntervalParser::Ptr addressIntervalParser(std::vector<AddressInterval> &storage);
202 AddressIntervalParser::Ptr addressIntervalParser();
203 
205 class Trigger {
206 public:
207  typedef AddressInterval SizeInterval; // okay to use 64-bit integers for the counters
208  struct Settings {
209  SizeInterval when; // when to trigger based on nCalls_
210  Settings(): when(0) {}
211  };
212 private:
213  size_t nCalls_; // number of times called
214  Settings settings_;
215 public:
217  Trigger(): nCalls_(0) {}
218 
220  explicit Trigger(const Settings &settings): nCalls_(0), settings_(settings) {}
221 
223  Trigger(size_t nSkip, size_t nTimes): nCalls_(0) {
224  settings_.when = nTimes ? SizeInterval::baseSize(nSkip, nTimes) : SizeInterval();
225  }
226 
228  static Trigger once() { return Trigger(0, 1); }
229 
231  static Trigger always() { return Trigger(0, size_t(-1)); }
232 
234  static Trigger never() { return Trigger(); }
235 
237  bool isArmed() const { return !settings_.when.isEmpty() && nCalls_<=settings_.when.greatest(); }
238 
240  bool shouldTrigger() { return settings_.when.isContaining(nCalls_++); }
241 
243  size_t nCalls() const { return nCalls_; }
244 
246  void reset() { nCalls_ = 0; }
247 
249  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
250 
252  static std::string docString();
253 };
254 
256 size_t serialNumber();
257 
258 
259 
260 } // namespace
261 } // namespace
262 } // namespace
263 
264 #endif
265 #endif
Instruction basic block.
const ValueSaver::Ptr valueSaver() const
Property: functor responsible for saving a parsed value in user storage.
bool isEmpty() const
True if interval is empty.
Definition: Interval.h:197
Trigger(const Settings &settings)
Armed for triggering when number of calls falls within when.
Definition: Utility.h:220
Base class for machine instructions.
Collection of streams.
Definition: Message.h:1599
Sawyer::SharedPointer< DataBlock > Ptr
Shared pointer to a data block.
Definition: DataBlock.h:35
A collection of related switch declarations.
Represents a synthesized function.
size_t nCalls() const
Number of times called.
Definition: Utility.h:243
bool isContaining(const Interval &other) const
Containment predicate.
Definition: Interval.h:216
Main namespace for the ROSE library.
Sawyer::SharedPointer< AddressIntervalParser > Ptr
Shared-ownership pointer to an AddressIntervalParser.
Definition: Utility.h:169
static Interval baseSize(rose_addr_t lo, rose_addr_t size)
Construct an interval from one endpoint and a size.
Definition: Interval.h:161
size_t serialNumber()
Return the next serial number.
Information about a parsed switch value.
Trigger based on number of times called.
Definition: Utility.h:205
static std::string docString()
Documentation for command-line switches.
bool isArmed() const
True if trigger is armed.
Definition: Utility.h:237
Trigger()
Trigger armed for single call.
Definition: Utility.h:217
Sawyer::SharedPointer< BasicBlock > Ptr
Shared pointer to a basic block.
Definition: BasicBlock.h:135
static Sawyer::CommandLine::SwitchGroup switches(Settings &)
Command-line switches to initialize settings.
Position within a command-line.
Base class parsing a value from input.
Represents no value.
Definition: Optional.h:32
T greatest() const
Returns upper limit.
Definition: Interval.h:191
static Trigger always()
Armed to always trigger.
Definition: Utility.h:231
static Trigger never()
Armed to never trigger.
Definition: Utility.h:234
FunctionPtr Ptr
Shared-ownership pointer for function.
Definition: Function.h:52
bool shouldTrigger()
Increment calls and return true if triggering.
Definition: Utility.h:240
static Trigger once()
Armed for one call.
Definition: Utility.h:228
void reset()
Reset number of calls to zero.
Definition: Utility.h:246
static AddressInterval parse(const char *input, const char **rest)
Parse an interval from a C string.
Trigger(size_t nSkip, size_t nTimes)
Armed for triggering after nSkip calls but not more than nTimes times.
Definition: Utility.h:223