ROSE  0.9.10.47
MemoryCell.h
1 #ifndef ROSE_BinaryAnalysis_InstructionSemantics2_MemoryCell_H
2 #define ROSE_BinaryAnalysis_InstructionSemantics2_MemoryCell_H
3 
4 #include <BaseSemantics2.h>
5 #include <Sawyer/Set.h>
6 #include <boost/serialization/access.hpp>
7 #include <boost/serialization/list.hpp>
8 
9 namespace Rose {
10 namespace BinaryAnalysis {
11 namespace InstructionSemantics2 {
12 namespace BaseSemantics {
13 
15 typedef boost::shared_ptr<class MemoryCell> MemoryCellPtr;
16 
24 class MemoryCell: public boost::enable_shared_from_this<MemoryCell> {
25 public:
29  class Visitor {
30  public:
31  virtual ~Visitor() {}
32  virtual void operator()(MemoryCellPtr&) = 0;
33  };
34 
36  class Predicate {
37  public:
38  virtual ~Predicate() {};
39 
41  virtual bool operator()(const MemoryCellPtr&) const = 0;
42  };
43 
45  class AllCells: public Predicate {
46  public:
47  virtual bool operator()(const MemoryCellPtr&) const ROSE_OVERRIDE {
48  return true;
49  }
50  };
51 
55  class NonWrittenCells: public Predicate {
56  public:
57  virtual bool operator()(const MemoryCellPtr&) const ROSE_OVERRIDE;
58  };
59 
60 private:
61  SValuePtr address_; // Address of memory cell.
62  SValuePtr value_; // Value stored at that address.
63  AddressSet writers_; // Instructions that wrote to this cell
64  InputOutputPropertySet ioProperties_;
65 
67  // Serialization
68 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
69 private:
70  friend class boost::serialization::access;
71 
72  template<class S>
73  void serialize(S &s, const unsigned /*version*/) {
74  s & BOOST_SERIALIZATION_NVP(address_);
75  s & BOOST_SERIALIZATION_NVP(value_);
76  s & BOOST_SERIALIZATION_NVP(writers_);
77  s & BOOST_SERIALIZATION_NVP(ioProperties_);
78  }
79 #endif
80 
82  // Real constructors
83 protected:
84  MemoryCell() {} // for serialization
85 
86  MemoryCell(const SValuePtr &address, const SValuePtr &value)
87  : address_(address), value_(value) {
88  ASSERT_not_null(address);
89  ASSERT_not_null(value);
90  }
91 
92  // deep-copy cell list so modifying this new one doesn't alter the existing one
93  MemoryCell(const MemoryCell &other)
94  : boost::enable_shared_from_this<MemoryCell>(other) {
95  address_ = other.address_->copy();
96  value_ = other.value_->copy();
97  writers_ = other.writers_;
98  ioProperties_ = other.ioProperties_;
99  }
100 
101 public:
102  virtual ~MemoryCell() {}
103 
105  // Static allocating constructors
106 public:
108  static MemoryCellPtr instance(const SValuePtr &address, const SValuePtr &value) {
109  return MemoryCellPtr(new MemoryCell(address, value));
110  }
111 
113  static MemoryCellPtr instance(const MemoryCellPtr &other) {
114  return MemoryCellPtr(new MemoryCell(*other));
115  }
116 
118  // Virtual constructors
119 public:
121  virtual MemoryCellPtr create(const SValuePtr &address, const SValuePtr &value) {
122  return instance(address, value);
123  }
124 
126  virtual MemoryCellPtr clone() const {
127  return MemoryCellPtr(new MemoryCell(*this));
128  }
129 
131  // Dynamic pointer casts. No-op since this is the base class.
132 public:
133  static MemoryCellPtr promote(const MemoryCellPtr &x) {
134  ASSERT_not_null(x);
135  return x;
136  }
137 
139  // Methods first declared at this level of the class hierarchy
140 public:
143  virtual SValuePtr get_address() const { return address_; }
144  virtual void set_address(const SValuePtr &addr) {
145  ASSERT_not_null(addr);
146  address_ = addr;
147  }
152  virtual SValuePtr get_value() const { return value_; }
153  virtual void set_value(const SValuePtr &v) {
154  ASSERT_not_null(v);
155  value_ = v;
156  }
162  virtual const AddressSet& getWriters() const {
163  return writers_;
164  }
165 
172  bool insertWriter(rose_addr_t writerVa) /*final*/ { return writers_.insert(writerVa); }
173  virtual bool insertWriters(const AddressSet &writerVas) { return writers_.insert(writerVas); }
182  bool eraseWriter(rose_addr_t writerVa) /*final*/ { return writers_.erase(writerVa); }
183  virtual bool eraseWriters(const AddressSet &writerVas) { return writers_.erase(writerVas); }
191  void setWriter(rose_addr_t writerVa) /*final*/;
192  virtual void setWriters(const AddressSet &writerVas) { writers_.insert(writerVas); }
198  virtual void eraseWriters() { writers_.clear(); }
199 
206  const InputOutputPropertySet& ioProperties() const { return ioProperties_; }
207  InputOutputPropertySet& ioProperties() { return ioProperties_; }
211  //----------------------------------------------------------------------
212  // The following writers API is deprecated. [Robb P. Matzke 2015-08-10]
213  //----------------------------------------------------------------------
214 
215  virtual boost::optional<rose_addr_t> get_latest_writer() const ROSE_DEPRECATED("use getWriters instead") {
216  AddressSet vas = getWriters();
217  if (vas.isEmpty())
218  return boost::optional<rose_addr_t>();
219  return *vas.values().begin(); // return an arbitrary writer
220  }
221  virtual void set_latest_writer(rose_addr_t writer_va) ROSE_DEPRECATED("use setWriter instead") {
222  setWriter(writer_va);
223  }
224  virtual void clear_latest_writer() ROSE_DEPRECATED("use clearWriters instead") {
225  eraseWriters();
226  }
227  virtual Sawyer::Optional<rose_addr_t> latestWriter() const ROSE_DEPRECATED("use getWriters instead") {
228  AddressSet vas = getWriters();
229  if (vas.isEmpty())
230  return Sawyer::Nothing();
231  return *vas.values().begin(); // return an arbitrary writer
232  }
233  virtual void latestWriter(rose_addr_t writerVa) ROSE_DEPRECATED("use setWriter instead") {
234  setWriter(writerVa);
235  }
236  virtual void latestWriter(const Sawyer::Optional<rose_addr_t> w) ROSE_DEPRECATED("use setWriter instead") {
237  if (w) {
238  setWriter(*w);
239  } else {
240  eraseWriters();
241  }
242  }
243  virtual void clearLatestWriter() ROSE_DEPRECATED("use eraseWrites instead") {
244  eraseWriters();
245  }
246 
250  virtual bool may_alias(const MemoryCellPtr &other, RiscOperators *addrOps) const;
251 
255  virtual bool must_alias(const MemoryCellPtr &other, RiscOperators *addrOps) const;
256 
259  void print(std::ostream &stream) const {
260  Formatter fmt;
261  print(stream, fmt);
262  }
263  virtual void print(std::ostream&, Formatter&) const;
268  MemoryCellPtr obj;
269  Formatter &fmt;
270  public:
271  WithFormatter(const MemoryCellPtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
272  void print(std::ostream &stream) const { obj->print(stream, fmt); }
273  };
274 
282  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
285 };
286 
287 
288 std::ostream& operator<<(std::ostream&, const MemoryCell&);
289 std::ostream& operator<<(std::ostream&, const MemoryCell::WithFormatter&);
290 
291 } // namespace
292 } // namespace
293 } // namespace
294 } // namespace
295 
296 #endif
boost::shared_ptr< class MemoryCell > MemoryCellPtr
Shared-ownership pointer to a semantic memory cell.
Definition: MemoryCell.h:15
void clear()
Erase all values.
Definition: Set.h:262
bool insert(const Value &value)
Insert a value.
Definition: Set.h:222
bool eraseWriter(rose_addr_t writerVa)
Erase specified writers.
Definition: MemoryCell.h:182
InputOutputPropertySet & ioProperties()
Properties: Boolean property set.
Definition: MemoryCell.h:207
virtual bool insertWriters(const AddressSet &writerVas)
Insert writer information.
Definition: MemoryCell.h:173
virtual SValuePtr get_address() const
Accessor for the memory cell address.
Definition: MemoryCell.h:143
virtual bool may_alias(const MemoryCellPtr &other, RiscOperators *addrOps) const
Determines whether two memory cells can alias one another.
const InputOutputPropertySet & ioProperties() const
Properties: Boolean property set.
Definition: MemoryCell.h:206
virtual SValuePtr get_value() const
Accessor for the value stored at a memory location.
Definition: MemoryCell.h:152
Sawyer::Container::Set< rose_addr_t > AddressSet
A set of concrete virtual addresses.
Definition: MemoryCell.h:26
virtual bool operator()(const MemoryCellPtr &) const ROSE_OVERRIDE
Invoked for some cell.
WithFormatter with_format(Formatter &fmt)
Used for printing states with formatting.
Definition: MemoryCell.h:282
virtual bool operator()(const MemoryCellPtr &) const =0
Invoked for some cell.
Sawyer::SharedPointer< class SValue > SValuePtr
Shared-ownership pointer to a semantic value in any domain.
bool erase(const Value &value)
Erase a value.
Definition: Set.h:242
void setWriter(rose_addr_t writerVa)
Sets writer information.
static MemoryCellPtr instance(const SValuePtr &address, const SValuePtr &value)
Instantiates a new memory cell object with the specified address and value.
Definition: MemoryCell.h:108
WithFormatter operator+(Formatter &fmt)
Used for printing states with formatting.
Definition: MemoryCell.h:283
virtual void set_address(const SValuePtr &addr)
Accessor for the memory cell address.
Definition: MemoryCell.h:144
virtual void set_value(const SValuePtr &v)
Accessor for the value stored at a memory location.
Definition: MemoryCell.h:153
virtual const AddressSet & getWriters() const
Get writer information.
Definition: MemoryCell.h:162
virtual bool eraseWriters(const AddressSet &writerVas)
Erase specified writers.
Definition: MemoryCell.h:183
virtual bool operator()(const MemoryCellPtr &) const ROSE_OVERRIDE
Invoked for some cell.
Definition: MemoryCell.h:47
virtual bool must_alias(const MemoryCellPtr &other, RiscOperators *addrOps) const
Determines whether two memory cells must alias one another.
bool insertWriter(rose_addr_t writerVa)
Insert writer information.
Definition: MemoryCell.h:172
Represents no value.
Definition: Optional.h:32
static MemoryCellPtr instance(const MemoryCellPtr &other)
Instantiates a new copy of an existing cell.
Definition: MemoryCell.h:113
virtual MemoryCellPtr clone() const
Creates a new deep-copy of this memory cell.
Definition: MemoryCell.h:126
void print(std::ostream &stream) const
Print the memory cell on a single line.
Definition: MemoryCell.h:259
virtual MemoryCellPtr create(const SValuePtr &address, const SValuePtr &value)
Creates a new memory cell object with the specified address and value.
Definition: MemoryCell.h:121
virtual void setWriters(const AddressSet &writerVas)
Sets writer information.
Definition: MemoryCell.h:192