ROSE  0.9.11.42
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_; }
213  virtual bool may_alias(const MemoryCellPtr &other, RiscOperators *addrOps) const;
214 
218  virtual bool must_alias(const MemoryCellPtr &other, RiscOperators *addrOps) const;
219 
222  void print(std::ostream &stream) const {
223  Formatter fmt;
224  print(stream, fmt);
225  }
226  virtual void print(std::ostream&, Formatter&) const;
231  MemoryCellPtr obj;
232  Formatter &fmt;
233  public:
234  WithFormatter(const MemoryCellPtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
235  void print(std::ostream &stream) const { obj->print(stream, fmt); }
236  };
237 
245  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
248 };
249 
250 
251 std::ostream& operator<<(std::ostream&, const MemoryCell&);
252 std::ostream& operator<<(std::ostream&, const MemoryCell::WithFormatter&);
253 
254 } // namespace
255 } // namespace
256 } // namespace
257 } // namespace
258 
259 #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
virtual bool operator()(const MemoryCellPtr &) const =0
Invoked for some cell.
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.
Main namespace for the ROSE library.
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
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:245
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.
Base class for most instruction semantics RISC operators.
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:246
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
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:222
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