ROSE  0.10.7.0
MemoryCell.h
1 #ifndef ROSE_BinaryAnalysis_InstructionSemantics2_MemoryCell_H
2 #define ROSE_BinaryAnalysis_InstructionSemantics2_MemoryCell_H
3 #include <rosePublicConfig.h>
4 #ifdef ROSE_BUILD_BINARY_ANALYSIS_SUPPORT
5 
6 #include <BaseSemantics2.h>
7 #include <Sawyer/Set.h>
8 #include <boost/serialization/access.hpp>
9 #include <boost/serialization/list.hpp>
10 
11 namespace Rose {
12 namespace BinaryAnalysis {
13 namespace InstructionSemantics2 {
14 namespace BaseSemantics {
15 
17 typedef boost::shared_ptr<class MemoryCell> MemoryCellPtr;
18 
26 class MemoryCell: public boost::enable_shared_from_this<MemoryCell> {
27 public:
31  class Visitor {
32  public:
33  virtual ~Visitor() {}
34  virtual void operator()(MemoryCellPtr&) = 0;
35  };
36 
38  class Predicate {
39  public:
40  virtual ~Predicate() {};
41 
43  virtual bool operator()(const MemoryCellPtr&) const = 0;
44  };
45 
47  class AllCells: public Predicate {
48  public:
49  virtual bool operator()(const MemoryCellPtr&) const ROSE_OVERRIDE {
50  return true;
51  }
52  };
53 
57  class NonWrittenCells: public Predicate {
58  public:
59  virtual bool operator()(const MemoryCellPtr&) const ROSE_OVERRIDE;
60  };
61 
62 private:
63  SValuePtr address_; // Address of memory cell.
64  SValuePtr value_; // Value stored at that address.
65  AddressSet writers_; // Instructions that wrote to this cell
66  InputOutputPropertySet ioProperties_;
67 
69  // Serialization
70 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
71 private:
72  friend class boost::serialization::access;
73 
74  template<class S>
75  void serialize(S &s, const unsigned /*version*/) {
76  s & BOOST_SERIALIZATION_NVP(address_);
77  s & BOOST_SERIALIZATION_NVP(value_);
78  s & BOOST_SERIALIZATION_NVP(writers_);
79  s & BOOST_SERIALIZATION_NVP(ioProperties_);
80  }
81 #endif
82 
84  // Real constructors
85 protected:
86  MemoryCell() {} // for serialization
87 
88  MemoryCell(const SValuePtr &address, const SValuePtr &value)
89  : address_(address), value_(value) {
90  ASSERT_not_null(address);
91  ASSERT_not_null(value);
92  }
93 
94  // deep-copy cell list so modifying this new one doesn't alter the existing one
95  MemoryCell(const MemoryCell &other)
96  : boost::enable_shared_from_this<MemoryCell>(other) {
97  address_ = other.address_->copy();
98  value_ = other.value_->copy();
99  writers_ = other.writers_;
100  ioProperties_ = other.ioProperties_;
101  }
102 
103 public:
104  virtual ~MemoryCell() {}
105 
107  // Static allocating constructors
108 public:
110  static MemoryCellPtr instance(const SValuePtr &address, const SValuePtr &value) {
111  return MemoryCellPtr(new MemoryCell(address, value));
112  }
113 
115  static MemoryCellPtr instance(const MemoryCellPtr &other) {
116  return MemoryCellPtr(new MemoryCell(*other));
117  }
118 
120  // Virtual constructors
121 public:
123  virtual MemoryCellPtr create(const SValuePtr &address, const SValuePtr &value) {
124  return instance(address, value);
125  }
126 
128  virtual MemoryCellPtr clone() const {
129  return MemoryCellPtr(new MemoryCell(*this));
130  }
131 
133  // Dynamic pointer casts. No-op since this is the base class.
134 public:
135  static MemoryCellPtr promote(const MemoryCellPtr &x) {
136  ASSERT_not_null(x);
137  return x;
138  }
139 
141  // Methods first declared at this level of the class hierarchy
142 public:
145  virtual SValuePtr get_address() const { return address_; }
146  virtual void set_address(const SValuePtr &addr) {
147  ASSERT_not_null(addr);
148  address_ = addr;
149  }
154  virtual SValuePtr get_value() const { return value_; }
155  virtual void set_value(const SValuePtr &v) {
156  ASSERT_not_null(v);
157  value_ = v;
158  }
164  virtual const AddressSet& getWriters() const {
165  return writers_;
166  }
167 
174  bool insertWriter(rose_addr_t writerVa) /*final*/ { return writers_.insert(writerVa); }
175  virtual bool insertWriters(const AddressSet &writerVas) { return writers_.insert(writerVas); }
184  bool eraseWriter(rose_addr_t writerVa) /*final*/ { return writers_.erase(writerVa); }
185  virtual bool eraseWriters(const AddressSet &writerVas) { return writers_.erase(writerVas); }
193  void setWriter(rose_addr_t writerVa) /*final*/;
194  virtual void setWriters(const AddressSet &writerVas) { writers_.insert(writerVas); }
200  virtual void eraseWriters() { writers_.clear(); }
201 
208  const InputOutputPropertySet& ioProperties() const { return ioProperties_; }
209  InputOutputPropertySet& ioProperties() { return ioProperties_; }
215  virtual bool may_alias(const MemoryCellPtr &other, RiscOperators *addrOps) const;
216 
220  virtual bool must_alias(const MemoryCellPtr &other, RiscOperators *addrOps) const;
221 
224  void print(std::ostream &stream) const {
225  Formatter fmt;
226  print(stream, fmt);
227  }
228  virtual void print(std::ostream&, Formatter&) const;
233  MemoryCellPtr obj;
234  Formatter &fmt;
235  public:
236  WithFormatter(const MemoryCellPtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
237  void print(std::ostream &stream) const { obj->print(stream, fmt); }
238  };
239 
247  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
249  WithFormatter operator+(const std::string &linePrefix) {
250  static Formatter fmt;
251  fmt.set_line_prefix(linePrefix);
252  return with_format(fmt);
253  }
255 };
256 
257 
258 std::ostream& operator<<(std::ostream&, const MemoryCell&);
259 std::ostream& operator<<(std::ostream&, const MemoryCell::WithFormatter&);
260 
261 } // namespace
262 } // namespace
263 } // namespace
264 } // namespace
265 
266 #endif
267 #endif
boost::shared_ptr< class MemoryCell > MemoryCellPtr
Shared-ownership pointer to a semantic memory cell.
Definition: MemoryCell.h:17
void set_line_prefix(const std::string &s)
The string to print at the start of each line.
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:184
virtual bool operator()(const MemoryCellPtr &) const =0
Invoked for some cell.
InputOutputPropertySet & ioProperties()
Properties: Boolean property set.
Definition: MemoryCell.h:209
virtual bool insertWriters(const AddressSet &writerVas)
Insert writer information.
Definition: MemoryCell.h:175
virtual SValuePtr get_address() const
Accessor for the memory cell address.
Definition: MemoryCell.h:145
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:208
virtual SValuePtr get_value() const
Accessor for the value stored at a memory location.
Definition: MemoryCell.h:154
Sawyer::Container::Set< rose_addr_t > AddressSet
A set of concrete virtual addresses.
Definition: MemoryCell.h:28
WithFormatter operator+(const std::string &linePrefix)
Used for printing states with formatting.
Definition: MemoryCell.h:249
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:247
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:110
WithFormatter operator+(Formatter &fmt)
Used for printing states with formatting.
Definition: MemoryCell.h:248
virtual void set_address(const SValuePtr &addr)
Accessor for the memory cell address.
Definition: MemoryCell.h:146
virtual void set_value(const SValuePtr &v)
Accessor for the value stored at a memory location.
Definition: MemoryCell.h:155
Sawyer::SharedPointer< SValue > SValuePtr
Shared-ownership pointer to a semantic value in any domain.
virtual const AddressSet & getWriters() const
Get writer information.
Definition: MemoryCell.h:164
virtual bool eraseWriters(const AddressSet &writerVas)
Erase specified writers.
Definition: MemoryCell.h:185
virtual bool operator()(const MemoryCellPtr &) const ROSE_OVERRIDE
Invoked for some cell.
Definition: MemoryCell.h:49
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:174
static MemoryCellPtr instance(const MemoryCellPtr &other)
Instantiates a new copy of an existing cell.
Definition: MemoryCell.h:115
virtual MemoryCellPtr clone() const
Creates a new deep-copy of this memory cell.
Definition: MemoryCell.h:128
void print(std::ostream &stream) const
Print the memory cell on a single line.
Definition: MemoryCell.h:224
virtual MemoryCellPtr create(const SValuePtr &address, const SValuePtr &value)
Creates a new memory cell object with the specified address and value.
Definition: MemoryCell.h:123
virtual void setWriters(const AddressSet &writerVas)
Sets writer information.
Definition: MemoryCell.h:194