ROSE  0.11.145.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/BasicTypes.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 #include <list>
15 
16 namespace Rose {
17 namespace BinaryAnalysis {
18 namespace InstructionSemantics {
19 namespace BaseSemantics {
20 
29 class MemoryCell: public boost::enable_shared_from_this<MemoryCell> {
30 public:
32  using Ptr = MemoryCellPtr;
33 
35  class Visitor {
36  public:
37  virtual ~Visitor() {}
38  virtual void operator()(MemoryCellPtr&) = 0;
39  };
40 
42  class Predicate {
43  public:
44  virtual ~Predicate() {};
45 
49  virtual bool operator()(const MemoryCellPtr&) = 0;
50  };
51 
53  class AllCells: public Predicate {
54  public:
55  virtual bool operator()(const MemoryCellPtr&) override {
56  return true;
57  }
58  };
59 
63  class NonWrittenCells: public Predicate {
64  public:
65  virtual bool operator()(const MemoryCellPtr&) override;
66  };
67 
68 private:
69  SValuePtr address_; // Address of memory cell.
70  SValuePtr value_; // Value stored at that address.
71  AddressSet writers_; // Instructions that wrote to this cell
72  InputOutputPropertySet ioProperties_;
73  unsigned position_ = 0; // position when printing a memory state
74 
76  // Serialization
77 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
78 private:
79  friend class boost::serialization::access;
80 
81  template<class S>
82  void serialize(S &s, const unsigned version) {
83  s & BOOST_SERIALIZATION_NVP(address_);
84  s & BOOST_SERIALIZATION_NVP(value_);
85  s & BOOST_SERIALIZATION_NVP(writers_);
86  s & BOOST_SERIALIZATION_NVP(ioProperties_);
87  if (version >= 1)
88  s & BOOST_SERIALIZATION_NVP(position_);
89  }
90 #endif
91 
93  // Real constructors
94 protected:
95  MemoryCell(); // for serialization
96 
97  MemoryCell(const SValuePtr &address, const SValuePtr &value);
98 
99  // deep-copy cell list so modifying this new one doesn't alter the existing one
100  MemoryCell(const MemoryCell &other);
101 
102 public:
103  virtual ~MemoryCell();
104 
106  // Static allocating constructors
107 public:
110  return MemoryCellPtr(new MemoryCell(address, value));
111  }
112 
114  static MemoryCellPtr instance(const MemoryCellPtr &other) {
115  return MemoryCellPtr(new MemoryCell(*other));
116  }
117 
119  // Virtual constructors
120 public:
123  return instance(address, value);
124  }
125 
127  virtual MemoryCellPtr clone() const {
128  return MemoryCellPtr(new MemoryCell(*this));
129  }
130 
132  // Dynamic pointer casts. No-op since this is the base class.
133 public:
134  static MemoryCellPtr promote(const MemoryCellPtr &x) {
135  ASSERT_not_null(x);
136  return x;
137  }
138 
140  // Methods first declared at this level of the class hierarchy
141 public:
147  virtual SValuePtr address() const;
148  virtual void address(const SValuePtr &addr);
156  virtual SValuePtr value() const;
157  virtual void value(const SValuePtr &v);
163  virtual const AddressSet& getWriters() const {
164  return writers_;
165  }
166 
173  bool insertWriter(rose_addr_t writerVa) /*final*/ { return writers_.insert(writerVa); }
174  virtual bool insertWriters(const AddressSet &writerVas) { return writers_.insert(writerVas); }
183  bool eraseWriter(rose_addr_t writerVa) /*final*/ { return writers_.erase(writerVa); }
184  virtual bool eraseWriters(const AddressSet &writerVas) { return writers_.erase(writerVas); }
192  void setWriter(rose_addr_t writerVa) /*final*/;
193  virtual void setWriters(const AddressSet &writerVas) { writers_.insert(writerVas); }
199  virtual void eraseWriters() { writers_.clear(); }
200 
207  const InputOutputPropertySet& ioProperties() const { return ioProperties_; }
208  InputOutputPropertySet& ioProperties() { return ioProperties_; }
220  unsigned position() const { return position_; }
221  void position(unsigned p) { position_ = p; }
229  bool mayAlias(const MemoryCellPtr &other, RiscOperators *addrOps) const;
230 
236  bool mustAlias(const MemoryCellPtr &other, RiscOperators *addrOps) const;
237 
243  virtual void hash(Combinatorics::Hasher&) const;
244 
247  void print(std::ostream &stream) const;
248  virtual void print(std::ostream&, Formatter&) const;
253  MemoryCellPtr obj;
254  Formatter &fmt;
255  public:
256  WithFormatter(const MemoryCellPtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
257  void print(std::ostream &stream) const { obj->print(stream, fmt); }
258  };
259 
269  WithFormatter operator+(const std::string &linePrefix);
271 };
272 
274 using CellList = std::list<MemoryCellPtr>;
275 
276 std::ostream& operator<<(std::ostream&, const MemoryCell&);
277 std::ostream& operator<<(std::ostream&, const MemoryCell::WithFormatter&);
278 
279 } // namespace
280 } // namespace
281 } // namespace
282 } // namespace
283 
286 
287 #endif
288 #endif
virtual bool eraseWriters(const AddressSet &writerVas)
Erase specified writers.
Definition: MemoryCell.h:184
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:114
void print(std::ostream &stream) const
Print the memory cell on a single line.
InputOutputPropertySet & ioProperties()
Properties: Boolean property set.
Definition: MemoryCell.h:208
virtual bool insertWriters(const AddressSet &writerVas)
Insert writer information.
Definition: MemoryCell.h:174
void clear()
Erase all values.
Definition: Set.h:282
bool insert(const Value &value)
Insert a value.
Definition: Set.h:242
unsigned position() const
Property: Position in listings.
Definition: MemoryCell.h:220
virtual bool operator()(const MemoryCellPtr &) override
Invoked for some cell.
Definition: MemoryCell.h:55
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:127
virtual void setWriters(const AddressSet &writerVas)
Sets writer information.
Definition: MemoryCell.h:193
static MemoryCellPtr instance(const SValuePtr &address, const SValuePtr &value)
Instantiates a new memory cell object with the specified address and value.
Definition: MemoryCell.h:109
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:183
virtual const AddressSet & getWriters() const
Get writer information.
Definition: MemoryCell.h:163
std::list< MemoryCellPtr > CellList
List of memory cells.
Definition: MemoryCell.h:274
bool erase(const Value &value)
Erase a value.
Definition: Set.h:262
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:173
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:221
Base class for most instruction semantics RISC operators.
Definition: RiscOperators.h:49
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:207
virtual MemoryCellPtr create(const SValuePtr &address, const SValuePtr &value)
Creates a new memory cell object with the specified address and value.
Definition: MemoryCell.h:122
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.