ROSE  0.11.51.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 
45  virtual bool operator()(const MemoryCellPtr&) const = 0;
46  };
47 
49  class AllCells: public Predicate {
50  public:
51  virtual bool operator()(const MemoryCellPtr&) const ROSE_OVERRIDE {
52  return true;
53  }
54  };
55 
59  class NonWrittenCells: public Predicate {
60  public:
61  virtual bool operator()(const MemoryCellPtr&) const ROSE_OVERRIDE;
62  };
63 
64 private:
65  SValuePtr address_; // Address of memory cell.
66  SValuePtr value_; // Value stored at that address.
67  AddressSet writers_; // Instructions that wrote to this cell
68  InputOutputPropertySet ioProperties_;
69 
71  // Serialization
72 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
73 private:
74  friend class boost::serialization::access;
75 
76  template<class S>
77  void serialize(S &s, const unsigned /*version*/) {
78  s & BOOST_SERIALIZATION_NVP(address_);
79  s & BOOST_SERIALIZATION_NVP(value_);
80  s & BOOST_SERIALIZATION_NVP(writers_);
81  s & BOOST_SERIALIZATION_NVP(ioProperties_);
82  }
83 #endif
84 
86  // Real constructors
87 protected:
88  MemoryCell(); // for serialization
89 
90  MemoryCell(const SValuePtr &address, const SValuePtr &value);
91 
92  // deep-copy cell list so modifying this new one doesn't alter the existing one
93  MemoryCell(const MemoryCell &other);
94 
95 public:
96  virtual ~MemoryCell();
97 
99  // Static allocating constructors
100 public:
103  return MemoryCellPtr(new MemoryCell(address, value));
104  }
105 
107  static MemoryCellPtr instance(const MemoryCellPtr &other) {
108  return MemoryCellPtr(new MemoryCell(*other));
109  }
110 
112  // Virtual constructors
113 public:
116  return instance(address, value);
117  }
118 
120  virtual MemoryCellPtr clone() const {
121  return MemoryCellPtr(new MemoryCell(*this));
122  }
123 
125  // Dynamic pointer casts. No-op since this is the base class.
126 public:
127  static MemoryCellPtr promote(const MemoryCellPtr &x) {
128  ASSERT_not_null(x);
129  return x;
130  }
131 
133  // Methods first declared at this level of the class hierarchy
134 public:
143  SValuePtr address() const /*final*/ {
144  return get_address();
145  }
146  void address(const SValuePtr &addr) /*final*/ {
147  set_address(addr);
148  }
159  SValuePtr value() const /*final*/ {
160  return get_value();
161  }
162  void value(const SValuePtr &v) /*final*/ {
163  set_value(v);
164  }
170  virtual const AddressSet& getWriters() const {
171  return writers_;
172  }
173 
180  bool insertWriter(rose_addr_t writerVa) /*final*/ { return writers_.insert(writerVa); }
181  virtual bool insertWriters(const AddressSet &writerVas) { return writers_.insert(writerVas); }
190  bool eraseWriter(rose_addr_t writerVa) /*final*/ { return writers_.erase(writerVa); }
191  virtual bool eraseWriters(const AddressSet &writerVas) { return writers_.erase(writerVas); }
199  void setWriter(rose_addr_t writerVa) /*final*/;
200  virtual void setWriters(const AddressSet &writerVas) { writers_.insert(writerVas); }
206  virtual void eraseWriters() { writers_.clear(); }
207 
214  const InputOutputPropertySet& ioProperties() const { return ioProperties_; }
215  InputOutputPropertySet& ioProperties() { return ioProperties_; }
226  bool mayAlias(const MemoryCellPtr &other, RiscOperators *addrOps) const /*final*/ {
227  return may_alias(other, addrOps);
228  }
229 
230  // [Robb Matzke 2021-03-18]: deprecated, but not marked as such because we need to still call it in order to support
231  // user-defined subclasses that might have reimplemented it. Users: be sure to mark them as "override" so you'll notice
232  // when we switch to just "mayAlias".
233  virtual bool may_alias(const MemoryCellPtr &other, RiscOperators *addrOps) const;
234 
242  bool mustAlias(const MemoryCellPtr &other, RiscOperators *addrOps) const /*final*/ {
243  return must_alias(other, addrOps);
244  }
245 
246  // [Robb Matzke 2021-03-18]: deprecated, but not marked as such because we need to still call it in order to support
247  // user-defined subclasses that might have reimplemented it. Users: be sure to mark them as "override" so you'll notice
248  // when we switch to just "mustAlias".
249  virtual bool must_alias(const MemoryCellPtr &other, RiscOperators *addrOps) const;
250 
256  virtual void hash(Combinatorics::Hasher&) const;
257 
260  void print(std::ostream &stream) const;
261  virtual void print(std::ostream&, Formatter&) const;
266  MemoryCellPtr obj;
267  Formatter &fmt;
268  public:
269  WithFormatter(const MemoryCellPtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
270  void print(std::ostream &stream) const { obj->print(stream, fmt); }
271  };
272 
282  WithFormatter operator+(const std::string &linePrefix);
285  // [Robb Matzke 2021-03-18]: deprecated.
286  // Virtual functions for the address property. These will eventually be removed, so it's in your best interest to use C++11
287  // "override" in your subclasses. We cannot mark these as ROSE_DEPRECATED because the value property needs to call them.
288  virtual SValuePtr get_address() const {
289  return address_;
290  }
291  virtual void set_address(const SValuePtr &addr) {
292  ASSERT_not_null(addr);
293  address_ = addr;
294  }
295 
296  // [Robb Matzke 2021-03-18]: deprecated
297  // Virtual functions for the value property. These will eventually be removed, so it's in your best interest to use C++11
298  // "override" in your subclasses. We cannot mark these as ROSE_DEPRECATED because the value property needs to call them.
299  virtual SValuePtr get_value() const {
300  return value_;
301  }
302  virtual void set_value(const SValuePtr &v) {
303  ASSERT_not_null(v);
304  value_ = v;
305  }
306 
307 
308 };
309 
310 
311 std::ostream& operator<<(std::ostream&, const MemoryCell&);
312 std::ostream& operator<<(std::ostream&, const MemoryCell::WithFormatter&);
313 
314 } // namespace
315 } // namespace
316 } // namespace
317 } // namespace
318 
320 
321 #endif
322 #endif
virtual void hash(Combinatorics::Hasher &) const
Hash the address and value.
void clear()
Erase all values.
Definition: Set.h:268
bool insert(const Value &value)
Insert a value.
Definition: Set.h:228
virtual bool operator()(const MemoryCellPtr &) const =0
Invoked for some cell.
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 &) const ROSE_OVERRIDE
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.
virtual bool operator()(const MemoryCellPtr &) const ROSE_OVERRIDE
Invoked for some cell.
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 void setWriters(const AddressSet &writerVas)
Sets writer information.