ROSE  0.11.87.0
BaseSemantics/MemoryCell.h
1 #ifndef ROSE_BinaryAnalysis_InstructionSemantics2_BaseSemantics_MemoryCell_H
2 #define ROSE_BinaryAnalysis_InstructionSemantics2_BaseSemantics_MemoryCell_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BinaryAnalysis/InstructionSemantics2/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 InstructionSemantics2 {
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:
148  SValuePtr address() const /*final*/ {
149  return get_address();
150  }
151  void address(const SValuePtr &addr) /*final*/ {
152  set_address(addr);
153  }
164  SValuePtr value() const /*final*/ {
165  return get_value();
166  }
167  void value(const SValuePtr &v) /*final*/ {
168  set_value(v);
169  }
175  virtual const AddressSet& getWriters() const {
176  return writers_;
177  }
178 
185  bool insertWriter(rose_addr_t writerVa) /*final*/ { return writers_.insert(writerVa); }
186  virtual bool insertWriters(const AddressSet &writerVas) { return writers_.insert(writerVas); }
195  bool eraseWriter(rose_addr_t writerVa) /*final*/ { return writers_.erase(writerVa); }
196  virtual bool eraseWriters(const AddressSet &writerVas) { return writers_.erase(writerVas); }
204  void setWriter(rose_addr_t writerVa) /*final*/;
205  virtual void setWriters(const AddressSet &writerVas) { writers_.insert(writerVas); }
211  virtual void eraseWriters() { writers_.clear(); }
212 
219  const InputOutputPropertySet& ioProperties() const { return ioProperties_; }
220  InputOutputPropertySet& ioProperties() { return ioProperties_; }
232  unsigned position() const { return position_; }
233  void position(unsigned p) { position_ = p; }
244  bool mayAlias(const MemoryCellPtr &other, RiscOperators *addrOps) const /*final*/ {
245  return may_alias(other, addrOps);
246  }
247 
248  // [Robb Matzke 2021-03-18]: deprecated, but not marked as such because we need to still call it in order to support
249  // user-defined subclasses that might have reimplemented it. Users: be sure to mark them as "override" so you'll notice
250  // when we switch to just "mayAlias".
251  virtual bool may_alias(const MemoryCellPtr &other, RiscOperators *addrOps) const;
252 
260  bool mustAlias(const MemoryCellPtr &other, RiscOperators *addrOps) const /*final*/ {
261  return must_alias(other, addrOps);
262  }
263 
264  // [Robb Matzke 2021-03-18]: deprecated, but not marked as such because we need to still call it in order to support
265  // user-defined subclasses that might have reimplemented it. Users: be sure to mark them as "override" so you'll notice
266  // when we switch to just "mustAlias".
267  virtual bool must_alias(const MemoryCellPtr &other, RiscOperators *addrOps) const;
268 
274  virtual void hash(Combinatorics::Hasher&) const;
275 
278  void print(std::ostream &stream) const;
279  virtual void print(std::ostream&, Formatter&) const;
284  MemoryCellPtr obj;
285  Formatter &fmt;
286  public:
287  WithFormatter(const MemoryCellPtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
288  void print(std::ostream &stream) const { obj->print(stream, fmt); }
289  };
290 
300  WithFormatter operator+(const std::string &linePrefix);
303  // [Robb Matzke 2021-03-18]: deprecated.
304  // Virtual functions for the address property. These will eventually be removed, so it's in your best interest to use C++11
305  // "override" in your subclasses. We cannot mark these as ROSE_DEPRECATED because the value property needs to call them.
306  virtual SValuePtr get_address() const {
307  return address_;
308  }
309  virtual void set_address(const SValuePtr &addr) {
310  ASSERT_not_null(addr);
311  address_ = addr;
312  }
313 
314  // [Robb Matzke 2021-03-18]: deprecated
315  // Virtual functions for the value property. These will eventually be removed, so it's in your best interest to use C++11
316  // "override" in your subclasses. We cannot mark these as ROSE_DEPRECATED because the value property needs to call them.
317  virtual SValuePtr get_value() const {
318  return value_;
319  }
320  virtual void set_value(const SValuePtr &v) {
321  ASSERT_not_null(v);
322  value_ = v;
323  }
324 };
325 
326 
327 std::ostream& operator<<(std::ostream&, const MemoryCell&);
328 std::ostream& operator<<(std::ostream&, const MemoryCell::WithFormatter&);
329 
330 } // namespace
331 } // namespace
332 } // namespace
333 } // namespace
334 
337 
338 #endif
339 #endif
virtual void hash(Combinatorics::Hasher &) const
Hash the address and value.
virtual bool operator()(const MemoryCellPtr &) override
Invoked for some cell.
void clear()
Erase all values.
Definition: Set.h:268
bool insert(const Value &value)
Insert a value.
Definition: Set.h:228
void address(const SValuePtr &addr)
Property: Memory cell address.
InputOutputPropertySet & ioProperties()
Properties: Boolean property set.
virtual bool insertWriters(const AddressSet &writerVas)
Insert writer information.
Main namespace for the ROSE library.
const InputOutputPropertySet & ioProperties() const
Properties: Boolean property set.
WithFormatter with_format(Formatter &)
Used for printing states with formatting.
WithFormatter operator+(Formatter &)
Used for printing states with formatting.
Base classes for instruction semantics.
Definition: Dispatcher.h:18
bool mustAlias(const MemoryCellPtr &other, RiscOperators *addrOps) const
Test whether two memory cells must alias one another.
virtual bool operator()(const MemoryCellPtr &)=0
Invoked for some cell.
bool erase(const Value &value)
Erase a value.
Definition: Set.h:248
void setWriter(rose_addr_t writerVa)
Sets writer information.
Base class for most instruction semantics RISC operators.
Definition: RiscOperators.h:48
static MemoryCellPtr instance(const SValuePtr &address, const SValuePtr &value)
Instantiates a new memory cell object with the specified address and value.
boost::shared_ptr< MemoryCell > MemoryCellPtr
Shared-ownership pointer to a memory cell.
Sawyer::SharedPointer< SValue > SValuePtr
Shared-ownership pointer to a semantic value in any domain.
virtual const AddressSet & getWriters() const
Get writer information.
bool mayAlias(const MemoryCellPtr &other, RiscOperators *addrOps) const
Test whether two memory cells can alias one another.
virtual bool eraseWriters(const AddressSet &writerVas)
Erase specified writers.
bool insertWriter(rose_addr_t writerVa)
Insert writer information.
static MemoryCellPtr instance(const MemoryCellPtr &other)
Instantiates a new copy of an existing cell.
virtual MemoryCellPtr clone() const
Creates a new deep-copy of this memory cell.
void print(std::ostream &stream) const
Print the memory cell on a single line.
virtual MemoryCellPtr create(const SValuePtr &address, const SValuePtr &value)
Creates a new memory cell object with the specified address and value.
virtual bool operator()(const MemoryCellPtr &) override
Invoked for some cell.
virtual void setWriters(const AddressSet &writerVas)
Sets writer information.