ROSE  0.9.9.109
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  address_ = other.address_->copy();
95  value_ = other.value_->copy();
96  writers_ = other.writers_;
97  ioProperties_ = other.ioProperties_;
98  }
99 
100 public:
101  virtual ~MemoryCell() {}
102 
104  // Static allocating constructors
105 public:
107  static MemoryCellPtr instance(const SValuePtr &address, const SValuePtr &value) {
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:
120  virtual MemoryCellPtr create(const SValuePtr &address, const SValuePtr &value) {
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:
142  virtual SValuePtr get_address() const { return address_; }
143  virtual void set_address(const SValuePtr &addr) {
144  ASSERT_not_null(addr);
145  address_ = addr;
146  }
151  virtual SValuePtr get_value() const { return value_; }
152  virtual void set_value(const SValuePtr &v) {
153  ASSERT_not_null(v);
154  value_ = v;
155  }
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_; }
210  //----------------------------------------------------------------------
211  // The following writers API is deprecated. [Robb P. Matzke 2015-08-10]
212  //----------------------------------------------------------------------
213 
214  virtual boost::optional<rose_addr_t> get_latest_writer() const ROSE_DEPRECATED("use getWriters instead") {
215  AddressSet vas = getWriters();
216  if (vas.isEmpty())
217  return boost::optional<rose_addr_t>();
218  return *vas.values().begin(); // return an arbitrary writer
219  }
220  virtual void set_latest_writer(rose_addr_t writer_va) ROSE_DEPRECATED("use setWriter instead") {
221  setWriter(writer_va);
222  }
223  virtual void clear_latest_writer() ROSE_DEPRECATED("use clearWriters instead") {
224  eraseWriters();
225  }
226  virtual Sawyer::Optional<rose_addr_t> latestWriter() const ROSE_DEPRECATED("use getWriters instead") {
227  AddressSet vas = getWriters();
228  if (vas.isEmpty())
229  return Sawyer::Nothing();
230  return *vas.values().begin(); // return an arbitrary writer
231  }
232  virtual void latestWriter(rose_addr_t writerVa) ROSE_DEPRECATED("use setWriter instead") {
233  setWriter(writerVa);
234  }
235  virtual void latestWriter(const Sawyer::Optional<rose_addr_t> w) ROSE_DEPRECATED("use setWriter instead") {
236  if (w) {
237  setWriter(*w);
238  } else {
239  eraseWriters();
240  }
241  }
242  virtual void clearLatestWriter() ROSE_DEPRECATED("use eraseWrites instead") {
243  eraseWriters();
244  }
245 
249  virtual bool may_alias(const MemoryCellPtr &other, RiscOperators *addrOps) const;
250 
254  virtual bool must_alias(const MemoryCellPtr &other, RiscOperators *addrOps) const;
255 
258  void print(std::ostream &stream) const {
259  Formatter fmt;
260  print(stream, fmt);
261  }
262  virtual void print(std::ostream&, Formatter&) const;
267  MemoryCellPtr obj;
268  Formatter &fmt;
269  public:
270  WithFormatter(const MemoryCellPtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
271  void print(std::ostream &stream) const { obj->print(stream, fmt); }
272  };
273 
281  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
284 };
285 
286 
287 std::ostream& operator<<(std::ostream&, const MemoryCell&);
288 std::ostream& operator<<(std::ostream&, const MemoryCell::WithFormatter&);
289 
290 } // namespace
291 } // namespace
292 } // namespace
293 } // namespace
294 
295 #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:181
virtual bool operator()(const MemoryCellPtr &) const =0
Invoked for some cell.
InputOutputPropertySet & ioProperties()
Properties: Boolean property set.
Definition: MemoryCell.h:206
virtual bool insertWriters(const AddressSet &writerVas)
Insert writer information.
Definition: MemoryCell.h:172
virtual SValuePtr get_address() const
Accessor for the memory cell address.
Definition: MemoryCell.h:142
virtual bool may_alias(const MemoryCellPtr &other, RiscOperators *addrOps) const
Determines whether two memory cells can alias one another.
Main namespace for the ROSE library.
const InputOutputPropertySet & ioProperties() const
Properties: Boolean property set.
Definition: MemoryCell.h:205
virtual SValuePtr get_value() const
Accessor for the value stored at a memory location.
Definition: MemoryCell.h:151
Sawyer::Container::Set< rose_addr_t > AddressSet
A set of concrete virtual addresses.
Definition: MemoryCell.h:26
Base classes for instruction semantics.
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:281
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:107
WithFormatter operator+(Formatter &fmt)
Used for printing states with formatting.
Definition: MemoryCell.h:282
virtual void set_address(const SValuePtr &addr)
Accessor for the memory cell address.
Definition: MemoryCell.h:143
virtual void set_value(const SValuePtr &v)
Accessor for the value stored at a memory location.
Definition: MemoryCell.h:152
virtual const AddressSet & getWriters() const
Get writer information.
Definition: MemoryCell.h:161
virtual bool eraseWriters(const AddressSet &writerVas)
Erase specified writers.
Definition: MemoryCell.h:182
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:171
Represents no value.
Definition: Optional.h:32
static MemoryCellPtr instance(const MemoryCellPtr &other)
Instantiates a new copy of an existing cell.
Definition: MemoryCell.h:112
virtual MemoryCellPtr clone() const
Creates a new deep-copy of this memory cell.
Definition: MemoryCell.h:125
void print(std::ostream &stream) const
Print the memory cell on a single line.
Definition: MemoryCell.h:258
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
virtual void setWriters(const AddressSet &writerVas)
Sets writer information.
Definition: MemoryCell.h:191