ROSE  0.9.11.56
Utility.h
1 #ifndef ROSE_Partitioner2_Utility_H
2 #define ROSE_Partitioner2_Utility_H
3 
4 #include <Partitioner2/AddressUsageMap.h>
5 #include <Partitioner2/BasicBlock.h>
6 #include <Partitioner2/ControlFlowGraph.h>
7 #include <Partitioner2/DataBlock.h>
8 #include <Partitioner2/Function.h>
9 
10 #include "Diagnostics.h"
11 
12 #include <algorithm>
13 #include <ostream>
14 
15 namespace Rose {
16 namespace BinaryAnalysis {
17 namespace Partitioner2 {
18 
19 extern Sawyer::Message::Facility mlog;
20 void initDiagnostics();
21 
22 bool sortBasicBlocksByAddress(const BasicBlock::Ptr&, const BasicBlock::Ptr&);
23 bool sortDataBlocks(const DataBlock::Ptr&, const DataBlock::Ptr&);
24 bool sortFunctionsByAddress(const Function::Ptr&, const Function::Ptr&);
25 bool sortFunctionNodesByAddress(const SgAsmFunction*, const SgAsmFunction*);
26 bool sortByExpression(const BasicBlock::Successor&, const BasicBlock::Successor&);
27 bool sortVerticesByAddress(const ControlFlowGraph::ConstVertexIterator&, const ControlFlowGraph::ConstVertexIterator&);
28 bool sortEdgesBySrc(const ControlFlowGraph::ConstEdgeIterator&, const ControlFlowGraph::ConstEdgeIterator&);
29 bool sortEdgesByDst(const ControlFlowGraph::ConstEdgeIterator&, const ControlFlowGraph::ConstEdgeIterator&);
30 bool sortBlocksForAst(SgAsmBlock*, SgAsmBlock*);
31 bool sortInstructionsByAddress(SgAsmInstruction*, SgAsmInstruction*);
32 
33 template<class Container, class Comparator>
34 static bool
35 isSorted(const Container &container, Comparator sorted, bool distinct=true) {
36  typename Container::const_iterator current = container.begin();
37  if (current!=container.end()) {
38  typename Container::const_iterator next = current;
39  while (++next != container.end()) {
40  if ((distinct && !sorted(*current, *next)) || sorted(*next, *current))
41  return false;
42  ++current;
43  }
44  }
45  return true;
46 }
47 
48 template<class Container, class Value, class Comparator>
49 typename Container::const_iterator
50 lowerBound(const Container &container, const Value &item, Comparator cmp) {
51  return std::lower_bound(container.begin(), container.end(), item, cmp);
52 }
53 template<class Container, class Value, class Comparator>
54 typename Container::iterator
55 lowerBound(Container &container, const Value &item, Comparator cmp) {
56  return std::lower_bound(container.begin(), container.end(), item, cmp);
57 }
58 
59 // Return true if two container elements are equal according to the sorting comparator
60 template<class Value, class Comparator>
61 bool
62 equalUnique(const Value &a, const Value &b, Comparator cmp) {
63  return !cmp(a, b) && !cmp(b, a); // equal iff neither one is less than the other
64 }
65 
66 // Insert an item into a sorted container if it doesn't exist yet in the container. Returns true iff inserted.
67 template<class Container, class Value, class Comparator>
68 bool
69 insertUnique(Container &container, const Value &item, Comparator cmp) {
70  ASSERT_require(!ROSE_PARTITIONER_EXPENSIVE_CHECKS || isSorted(container, cmp, true)); // unique, sorted items
71  typename Container::iterator lb = lowerBound(container, item, cmp);
72  if (lb==container.end() || !equalUnique(*lb, item, cmp)) {
73  container.insert(lb, item);
74  ASSERT_require(!ROSE_PARTITIONER_EXPENSIVE_CHECKS || isSorted(container, cmp, true));
75  return true;
76  }
77  return false;
78 }
79 
80 // Insert an intem into a sorted continer, replacing any existing item that compares equal to it.
81 template<class Container, class Value, class Comparator>
82 void
83 replaceOrInsert(Container &container, const Value &item, Comparator cmp) {
84  ASSERT_require(!ROSE_PARTITIONER_EXPENSIVE_CHECKS || isSorted(container, cmp, true)); // unique, sorted items
85  typename Container::iterator lb = lowerBound(container, item, cmp);
86  if (lb == container.end() || !equalUnique(*lb, item, cmp)) {
87  container.insert(lb, item);
88  } else {
89  *lb = item;
90  }
91  ASSERT_require(!ROSE_PARTITIONER_EXPENSIVE_CHECKS || isSorted(container, cmp, true));
92 }
93 
94 // Erase an item from a sorted container if it doesn't exist yet in the container. Returns true iff erased.
95 template<class Container, class Value, class Comparator>
96 bool
97 eraseUnique(Container &container, const Value &item, Comparator cmp) {
98  ASSERT_require(!ROSE_PARTITIONER_EXPENSIVE_CHECKS || isSorted(container, cmp, true)); // unique, sorted items
99  typename Container::iterator lb = lowerBound(container, item, cmp);
100  if (lb!=container.end() && equalUnique(*lb, item, cmp)) {
101  container.erase(lb);
102  return true;
103  }
104  return false;
105 }
106 
107 // Check existence of an item in a sorted container. Returns true iff the item exists.
108 template<class Container, class Value, class Comparator>
109 bool
110 existsUnique(const Container &container, const Value &item, Comparator cmp) {
111  if (item==NULL)
112  return false;
113  ASSERT_require(!ROSE_PARTITIONER_EXPENSIVE_CHECKS || isSorted(container, cmp, true)); // unique, sorted items
114  typename Container::const_iterator lb = lowerBound(container, item, cmp);
115  if (lb==container.end() || cmp(*lb, item) || cmp(item, *lb))
116  return false;
117  return true;
118 }
119 
120 // Retrieve an item from a sorted container
121 template<class Container, class Value, class Comparator>
123 getUnique(const Container &container, const Value &item, Comparator cmp) {
124  if (item==NULL)
125  return Sawyer::Nothing();
126  ASSERT_require(!ROSE_PARTITIONER_EXPENSIVE_CHECKS || isSorted(container, cmp, true)); // unique, sorted items
127  typename Container::const_iterator lb = lowerBound(container, item, cmp);
128  if (lb==container.end() || cmp(*lb, item) || cmp(item, *lb))
129  return Sawyer::Nothing();
130  return *lb;
131 }
132 
133 template<class Container, class Comparator>
134 bool
135 isSupersetUnique(const Container &sup, const Container &sub, Comparator lessThan) {
136  ASSERT_require(!ROSE_PARTITIONER_EXPENSIVE_CHECKS || isSorted(sup, lessThan, true)); // unique, sorted items
137  ASSERT_require(!ROSE_PARTITIONER_EXPENSIVE_CHECKS || isSorted(sub, lessThan, true)); // unique, sorted items
138  typename Container::const_iterator isup = sup.begin(), isub = sub.begin();
139  while (isup != sup.end() && isub != sub.end()) {
140  while (isup != sup.end() && lessThan(*isup, *isub))
141  ++isup;
142  if (isup == sup.end() || lessThan(*isub, *isup))
143  return false;
144  ++isup, ++isub;
145  }
146  return isub == sub.end();
147 }
148 
149 std::ostream& operator<<(std::ostream&, const AddressUser&);
150 std::ostream& operator<<(std::ostream&, const AddressUsers&);
151 std::ostream& operator<<(std::ostream&, const AddressUsageMap&);
152 std::ostream& operator<<(std::ostream&, const ControlFlowGraph::Vertex&);
153 std::ostream& operator<<(std::ostream&, const ControlFlowGraph::Edge&);
154 
166 protected:
169  : Sawyer::CommandLine::ValueParser(valueSaver) {}
170 public:
173 
174  static Ptr instance() {
175  return Ptr(new AddressIntervalParser);
176  }
177 
178  static Ptr instance(const Sawyer::CommandLine::ValueSaver::Ptr &valueSaver) {
179  return Ptr(new AddressIntervalParser(valueSaver));
180  }
181 
182  static std::string docString();
183 
188  static AddressInterval parse(const char *input, const char **rest);
189 
196  static AddressInterval parse(const std::string &input);
197 
198 private:
199  virtual Sawyer::CommandLine::ParsedValue operator()(const char *input, const char **rest,
200  const Sawyer::CommandLine::Location &loc) ROSE_OVERRIDE;
201 };
202 
203 AddressIntervalParser::Ptr addressIntervalParser(AddressInterval &storage);
204 AddressIntervalParser::Ptr addressIntervalParser(std::vector<AddressInterval> &storage);
205 AddressIntervalParser::Ptr addressIntervalParser();
206 
208 class Trigger {
209 public:
210  typedef AddressInterval SizeInterval; // okay to use 64-bit integers for the counters
211  struct Settings {
212  SizeInterval when; // when to trigger based on nCalls_
213  Settings(): when(0) {}
214  };
215 private:
216  size_t nCalls_; // number of times called
217  Settings settings_;
218 public:
220  Trigger(): nCalls_(0) {}
221 
223  explicit Trigger(const Settings &settings): nCalls_(0), settings_(settings) {}
224 
226  Trigger(size_t nSkip, size_t nTimes): nCalls_(0) {
227  settings_.when = nTimes ? SizeInterval::baseSize(nSkip, nTimes) : SizeInterval();
228  }
229 
231  static Trigger once() { return Trigger(0, 1); }
232 
234  static Trigger always() { return Trigger(0, size_t(-1)); }
235 
237  static Trigger never() { return Trigger(); }
238 
240  bool isArmed() const { return !settings_.when.isEmpty() && nCalls_<=settings_.when.greatest(); }
241 
243  bool shouldTrigger() { return settings_.when.isContaining(nCalls_++); }
244 
246  size_t nCalls() const { return nCalls_; }
247 
249  void reset() { nCalls_ = 0; }
250 
252  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
253 
255  static std::string docString();
256 };
257 
259 size_t serialNumber();
260 
261 
262 
263 } // namespace
264 } // namespace
265 } // namespace
266 
267 #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:223
Base class for machine instructions.
Collection of streams.
Definition: Message.h:1579
Sawyer::SharedPointer< DataBlock > Ptr
Shared pointer to a data block.
Definition: DataBlock.h:31
A collection of related switch declarations.
Represents a synthesized function.
size_t nCalls() const
Number of times called.
Definition: Utility.h:246
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:172
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:208
static std::string docString()
Documentation for command-line switches.
bool isArmed() const
True if trigger is armed.
Definition: Utility.h:240
Trigger()
Trigger armed for single call.
Definition: Utility.h:220
Sawyer::SharedPointer< BasicBlock > Ptr
Shared pointer to a basic block.
Definition: BasicBlock.h:131
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:234
static Trigger never()
Armed to never trigger.
Definition: Utility.h:237
FunctionPtr Ptr
Shared-ownership pointer for function.
Definition: Function.h:48
bool shouldTrigger()
Increment calls and return true if triggering.
Definition: Utility.h:243
static Trigger once()
Armed for one call.
Definition: Utility.h:231
void reset()
Reset number of calls to zero.
Definition: Utility.h:249
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:226