ROSE  0.11.51.0
MemoryState.h
1 #ifndef ROSE_BinaryAnalysis_InstructionSemantics2_BaseSemantics_MemoryState_H
2 #define ROSE_BinaryAnalysis_InstructionSemantics2_BaseSemantics_MemoryState_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BinaryAnalysis/InstructionSemantics2/BaseSemantics/Types.h>
7 
8 #include <boost/enable_shared_from_this.hpp>
9 #include <boost/serialization/access.hpp>
10 #include <boost/serialization/export.hpp>
11 #include <boost/serialization/shared_ptr.hpp>
12 
13 namespace Rose {
14 namespace BinaryAnalysis {
15 namespace InstructionSemantics2 {
16 namespace BaseSemantics {
17 
19 // Memory State
21 
25 class MemoryState: public boost::enable_shared_from_this<MemoryState> {
26 public:
29 
30 private:
31  SValuePtr addrProtoval_;
32  SValuePtr valProtoval_;
33  ByteOrder::Endianness byteOrder_;
34  MergerPtr merger_;
35  bool byteRestricted_; // are cell values all exactly one byte wide?
36 
38  // Serialization
39 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
40 private:
41  friend class boost::serialization::access;
42 
43  template<class S>
44  void serialize(S &s, const unsigned /*version*/) {
45  s & BOOST_SERIALIZATION_NVP(addrProtoval_);
46  s & BOOST_SERIALIZATION_NVP(valProtoval_);
47  s & BOOST_SERIALIZATION_NVP(byteOrder_);
48  //s & merger_ -- not saved
49  s & BOOST_SERIALIZATION_NVP(byteRestricted_);
50  }
51 #endif
52 
53 
55  // Real constructors
56 protected:
57  MemoryState(); // for serialization
58  MemoryState(const SValuePtr &addrProtoval, const SValuePtr &valProtoval);
59  MemoryState(const MemoryStatePtr &other);
60 
61 public:
62  virtual ~MemoryState();
63 
65  // Static allocating constructors. None needed since this class is abstract
66 
68  // Virtual constructors
69 public:
76  virtual MemoryStatePtr create(const SValuePtr &addrProtoval, const SValuePtr &valProtoval) const = 0;
77 
79  virtual MemoryStatePtr clone() const = 0;
80 
82  // Dynamic pointer casts. No-op since this is the base class.
83 public:
84  static MemoryStatePtr promote(const MemoryStatePtr &x) {
85  ASSERT_not_null(x);
86  return x;
87  }
88 
90  // Methods first declared at this level of the class hierarchy
91 public:
100  MergerPtr merger() const;
101  void merger(const MergerPtr&);
106  SValuePtr get_addr_protoval() const { return addrProtoval_; }
107 
110  SValuePtr get_val_protoval() const { return valProtoval_; }
111 
113  virtual void clear() = 0;
114 
122  bool byteRestricted() const { return byteRestricted_; }
123  void byteRestricted(bool b) { byteRestricted_ = b; }
128  ByteOrder::Endianness get_byteOrder() const { return byteOrder_; }
129  void set_byteOrder(ByteOrder::Endianness bo) { byteOrder_ = bo; }
135  virtual bool merge(const MemoryStatePtr &other, RiscOperators *addrOps, RiscOperators *valOps) = 0;
136 
156  virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt,
157  RiscOperators *addrOps, RiscOperators *valOps) = 0;
158 
163  virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt,
164  RiscOperators *addrOps, RiscOperators *valOps) = 0;
165 
175  virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value,
176  RiscOperators *addrOps, RiscOperators *valOps) = 0;
177 
178 
183  virtual void hash(Combinatorics::Hasher&, RiscOperators *addrOps, RiscOperators *valOps) const = 0;
184 
187  void print(std::ostream&, const std::string prefix = "") const;
188  virtual void print(std::ostream&, Formatter&) const = 0;
193  MemoryStatePtr obj;
194  Formatter &fmt;
195  public:
196  WithFormatter(const MemoryStatePtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
197  void print(std::ostream &stream) const { obj->print(stream, fmt); }
198  };
199 
215  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
217  WithFormatter operator+(const std::string &linePrefix);
219 };
220 
221 std::ostream& operator<<(std::ostream&, const MemoryState&);
222 std::ostream& operator<<(std::ostream&, const MemoryState::WithFormatter&);
223 
224 } // namespace
225 } // namespace
226 } // namespace
227 } // namespace
228 
230 
231 #endif
232 #endif
WithFormatter operator+(Formatter &fmt)
Used for printing memory states with formatting.
Definition: MemoryState.h:216
void print(std::ostream &, const std::string prefix="") const
Print a memory state to more than one line of output.
virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt, RiscOperators *addrOps, RiscOperators *valOps)=0
Read a value from memory without side effects.
boost::shared_ptr< MemoryState > MemoryStatePtr
Shared-ownership pointer to a memory state.
virtual void hash(Combinatorics::Hasher &, RiscOperators *addrOps, RiscOperators *valOps) const =0
Calculate a hash for this memory state.
MemoryStatePtr Ptr
Shared-ownership pointer for a MemoryState.
Definition: MemoryState.h:28
ByteOrder::Endianness get_byteOrder() const
Memory byte order.
Definition: MemoryState.h:128
virtual MemoryStatePtr create(const SValuePtr &addrProtoval, const SValuePtr &valProtoval) const =0
Virtual allocating constructor.
Main namespace for the ROSE library.
void byteRestricted(bool b)
Indicates whether memory cell values are required to be eight bits wide.
Definition: MemoryState.h:123
SValuePtr get_addr_protoval() const
Return the address protoval.
Definition: MemoryState.h:106
virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt, RiscOperators *addrOps, RiscOperators *valOps)=0
Read a value from memory.
Base classes for instruction semantics.
Definition: Dispatcher.h:18
Sawyer::SharedPointer< Merger > MergerPtr
Shared-ownership pointer for Merger classes.
virtual bool merge(const MemoryStatePtr &other, RiscOperators *addrOps, RiscOperators *valOps)=0
Merge memory states for data flow analysis.
Base class for most instruction semantics RISC operators.
Definition: RiscOperators.h:48
WithFormatter with_format(Formatter &fmt)
Used for printing memory states with formatting.
Definition: MemoryState.h:215
virtual MemoryStatePtr clone() const =0
Virtual allocating copy constructor.
SValuePtr get_val_protoval() const
Return the value protoval.
Definition: MemoryState.h:110
void set_byteOrder(ByteOrder::Endianness bo)
Memory byte order.
Definition: MemoryState.h:129
bool byteRestricted() const
Indicates whether memory cell values are required to be eight bits wide.
Definition: MemoryState.h:122
virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value, RiscOperators *addrOps, RiscOperators *valOps)=0
Write a value to memory.