ROSE  0.11.98.0
MemoryCell.h
1 #ifndef ROSE_BinaryAnalysis_InstructionSemantics_BaseSemantics_MemoryCell_H
2 #define ROSE_BinaryAnalysis_InstructionSemantics_BaseSemantics_MemoryCell_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BinaryAnalysis/InstructionSemantics/BaseSemantics/Types.h>
7 
8 #include <Sawyer/Set.h>
9 
10 #include <boost/enable_shared_from_this.hpp>
11 #include <boost/serialization/access.hpp>
12 #include <boost/serialization/export.hpp>
13 #include <boost/serialization/list.hpp>
14 
15 namespace Rose {
16 namespace BinaryAnalysis {
17 namespace InstructionSemantics {
18 namespace BaseSemantics {
19 
27 class MemoryCell: public boost::enable_shared_from_this<MemoryCell> {
28 public:
30  using Ptr = MemoryCellPtr;
31 
33  class Visitor {
34  public:
35  virtual ~Visitor() {}
36  virtual void operator()(MemoryCellPtr&) = 0;
37  };
38 
40  class Predicate {
41  public:
42  virtual ~Predicate() {};
43 
47  virtual bool operator()(const MemoryCellPtr&) = 0;
48  };
49 
51  class AllCells: public Predicate {
52  public:
53  virtual bool operator()(const MemoryCellPtr&) override {
54  return true;
55  }
56  };
57 
61  class NonWrittenCells: public Predicate {
62  public:
63  virtual bool operator()(const MemoryCellPtr&) override;
64  };
65 
66 private:
67  SValuePtr address_; // Address of memory cell.
68  SValuePtr value_; // Value stored at that address.
69  AddressSet writers_; // Instructions that wrote to this cell
70  InputOutputPropertySet ioProperties_;
71  unsigned position_ = 0; // position when printing a memory state
72 
74  // Serialization
75 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
76 private:
77  friend class boost::serialization::access;
78 
79  template<class S>
80  void serialize(S &s, const unsigned version) {
81  s & BOOST_SERIALIZATION_NVP(address_);
82  s & BOOST_SERIALIZATION_NVP(value_);
83  s & BOOST_SERIALIZATION_NVP(writers_);
84  s & BOOST_SERIALIZATION_NVP(ioProperties_);
85  if (version >= 1)
86  s & BOOST_SERIALIZATION_NVP(position_);
87  }
88 #endif
89 
91  // Real constructors
92 protected:
93  MemoryCell(); // for serialization
94 
95  MemoryCell(const SValuePtr &address, const SValuePtr &value);
96 
97  // deep-copy cell list so modifying this new one doesn't alter the existing one
98  MemoryCell(const MemoryCell &other);
99 
100 public:
101  virtual ~MemoryCell();
102 
104  // Static allocating constructors
105 public:
108  return MemoryCellPtr(new MemoryCell(address, value));
109  }
110 
112  static MemoryCellPtr instance(const MemoryCellPtr &other) {
113  return MemoryCellPtr(new MemoryCell(*other));
114  }
115 
117  // Virtual constructors
118 public:
121  return instance(address, value);
122  }
123 
125  virtual MemoryCellPtr clone() const {
126  return MemoryCellPtr(new MemoryCell(*this));
127  }
128 
130  // Dynamic pointer casts. No-op since this is the base class.
131 public:
132  static MemoryCellPtr promote(const MemoryCellPtr &x) {
133  ASSERT_not_null(x);
134  return x;
135  }
136 
138  // Methods first declared at this level of the class hierarchy
139 public:
145  virtual SValuePtr address() const;
146  virtual void address(const SValuePtr &addr);
154  virtual SValuePtr value() const;
155  virtual void value(const SValuePtr &v);
161  virtual const AddressSet& getWriters() const {
162  return writers_;
163  }
164 
171  bool insertWriter(rose_addr_t writerVa) /*final*/ { return writers_.insert(writerVa); }
172  virtual bool insertWriters(const AddressSet &writerVas) { return writers_.insert(writerVas); }
181  bool eraseWriter(rose_addr_t writerVa) /*final*/ { return writers_.erase(writerVa); }
182  virtual bool eraseWriters(const AddressSet &writerVas) { return writers_.erase(writerVas); }
190  void setWriter(rose_addr_t writerVa) /*final*/;
191  virtual void setWriters(const AddressSet &writerVas) { writers_.insert(writerVas); }
197  virtual void eraseWriters() { writers_.clear(); }
198 
205  const InputOutputPropertySet& ioProperties() const { return ioProperties_; }
206  InputOutputPropertySet& ioProperties() { return ioProperties_; }
218  unsigned position() const { return position_; }
219  void position(unsigned p) { position_ = p; }
227  bool mayAlias(const MemoryCellPtr &other, RiscOperators *addrOps) const;
228 
234  bool mustAlias(const MemoryCellPtr &other, RiscOperators *addrOps) const;
235 
241  virtual void hash(Combinatorics::Hasher&) const;
242 
245  void print(std::ostream &stream) const;
246  virtual void print(std::ostream&, Formatter&) const;
251  MemoryCellPtr obj;
252  Formatter &fmt;
253  public:
254  WithFormatter(const MemoryCellPtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
255  void print(std::ostream &stream) const { obj->print(stream, fmt); }
256  };
257 
267  WithFormatter operator+(const std::string &linePrefix);
269 };
270 
271 
272 std::ostream& operator<<(std::ostream&, const MemoryCell&);
273 std::ostream& operator<<(std::ostream&, const MemoryCell::WithFormatter&);
274 
275 } // namespace
276 } // namespace
277 } // namespace
278 } // namespace
279 
282 
283 #endif
284 #endif
Base classes for instruction semantics.
Definition: Dispatcher.h:18
virtual bool eraseWriters(const AddressSet &writerVas)
Erase specified writers.
Definition: MemoryCell.h:182
virtual bool operator()(const MemoryCellPtr &)=0
Invoked for some cell.
static MemoryCellPtr instance(const MemoryCellPtr &other)
Instantiates a new copy of an existing cell.
Definition: MemoryCell.h:112
void print(std::ostream &stream) const
Print the memory cell on a single line.
InputOutputPropertySet & ioProperties()
Properties: Boolean property set.
Definition: MemoryCell.h:206
virtual bool insertWriters(const AddressSet &writerVas)
Insert writer information.
Definition: MemoryCell.h:172
void clear()
Erase all values.
Definition: Set.h:268
bool insert(const Value &value)
Insert a value.
Definition: Set.h:228
unsigned position() const
Property: Position in listings.
Definition: MemoryCell.h:218
virtual bool operator()(const MemoryCellPtr &) override
Invoked for some cell.
Definition: MemoryCell.h:53
Main namespace for the ROSE library.
boost::shared_ptr< MemoryCell > MemoryCellPtr
Shared-ownership pointer to a memory cell.
virtual MemoryCellPtr clone() const
Creates a new deep-copy of this memory cell.
Definition: MemoryCell.h:125
virtual void setWriters(const AddressSet &writerVas)
Sets writer information.
Definition: MemoryCell.h:191
static MemoryCellPtr instance(const SValuePtr &address, const SValuePtr &value)
Instantiates a new memory cell object with the specified address and value.
Definition: MemoryCell.h:107
void setWriter(rose_addr_t writerVa)
Sets writer information.
virtual SValuePtr value() const
Property: Memory cell value.
bool eraseWriter(rose_addr_t writerVa)
Erase specified writers.
Definition: MemoryCell.h:181
virtual const AddressSet & getWriters() const
Get writer information.
Definition: MemoryCell.h:161
bool erase(const Value &value)
Erase a value.
Definition: Set.h:248
Binary analysis.
virtual bool operator()(const MemoryCellPtr &) override
Invoked for some cell.
Sawyer::SharedPointer< SValue > SValuePtr
Shared-ownership pointer to a semantic value in any domain.
bool insertWriter(rose_addr_t writerVa)
Insert writer information.
Definition: MemoryCell.h:171
WithFormatter with_format(Formatter &)
Used for printing states with formatting.
bool mayAlias(const MemoryCellPtr &other, RiscOperators *addrOps) const
Test whether two memory cells can alias one another.
void position(unsigned p)
Property: Position in listings.
Definition: MemoryCell.h:219
Base class for most instruction semantics RISC operators.
Definition: RiscOperators.h:48
bool mustAlias(const MemoryCellPtr &other, RiscOperators *addrOps) const
Test whether two memory cells must alias one another.
const InputOutputPropertySet & ioProperties() const
Properties: Boolean property set.
Definition: MemoryCell.h:205
virtual MemoryCellPtr create(const SValuePtr &address, const SValuePtr &value)
Creates a new memory cell object with the specified address and value.
Definition: MemoryCell.h:120
WithFormatter operator+(Formatter &)
Used for printing states with formatting.
virtual SValuePtr address() const
Property: Memory cell address.
virtual void hash(Combinatorics::Hasher &) const
Hash the address and value.