ROSE  0.11.145.0
State.h
1 #ifndef ROSE_BinaryAnalysis_InstructionSemantics_BaseSemantics_State_H
2 #define ROSE_BinaryAnalysis_InstructionSemantics_BaseSemantics_State_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BinaryAnalysis/InstructionSemantics/BaseSemantics/BasicTypes.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/nvp.hpp>
12 #include <boost/serialization/shared_ptr.hpp>
13 
14 namespace Rose {
15 namespace BinaryAnalysis {
16 namespace InstructionSemantics {
17 namespace BaseSemantics {
18 
20 // State
22 
39 class State: public boost::enable_shared_from_this<State> {
40 public:
42  using Ptr = StatePtr;
43 
44 private:
45  SValuePtr protoval_; // Initial value used to create additional values as needed.
46  RegisterStatePtr registers_; // All machine register values for this semantic state.
47  MemoryStatePtr memory_; // All memory for this semantic state.
48 
50  // Serialization
51 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
52 private:
53  friend class boost::serialization::access;
54 
55  template<class S>
56  void serialize(S &s, const unsigned /*version*/) {
57  s & BOOST_SERIALIZATION_NVP(protoval_);
58  s & BOOST_SERIALIZATION_NVP(registers_);
59  s & BOOST_SERIALIZATION_NVP(memory_);
60  }
61 #endif
62 
63 
65  // Real constructors
66 protected:
67  // needed for serialization
68  State();
69  State(const RegisterStatePtr &registers, const MemoryStatePtr &memory);
70 
71  // deep-copy the registers and memory
72  State(const State &other);
73 
74 public:
75  virtual ~State();
76 
78  // Static allocating constructors
79 public:
81  static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory) {
82  return StatePtr(new State(registers, memory));
83  }
84 
86  static StatePtr instance(const StatePtr &other) {
87  return StatePtr(new State(*other));
88  }
89 
91  // Virtual constructors
92 public:
94  virtual StatePtr create(const RegisterStatePtr &registers, const MemoryStatePtr &memory) const {
95  return instance(registers, memory);
96  }
97 
101  virtual StatePtr clone() const {
102  StatePtr self = boost::const_pointer_cast<State>(shared_from_this());
103  return instance(self);
104  }
105 
107  // Dynamic pointer casts. No-op since this is the base class.
108 public:
109  static StatePtr promote(const StatePtr &x) {
110  ASSERT_not_null(x);
111  return x;
112  }
113 
115  // Other methods that are part of our API. Most of these just chain to either the register state and/or the memory state.
116 public:
118  SValuePtr protoval() const;
119 
121  virtual void clear();
122 
126  void zeroRegisters();
127 
131  void clearMemory();
132 
137  return registers_;
138  }
139 
144  return memory_;
145  }
146 
151  virtual SValuePtr readRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops);
152 
157  virtual SValuePtr peekRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops);
158 
163  virtual void writeRegister(RegisterDescriptor desc, const SValuePtr &value, RiscOperators *ops);
164 
169  virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt,
170  RiscOperators *addrOps, RiscOperators *valOps);
171 
176  virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt,
177  RiscOperators *addrOps, RiscOperators *valOps);
178 
183  virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value, RiscOperators *addrOps, RiscOperators *valOps);
184 
188  virtual void hash(Combinatorics::Hasher&, RiscOperators *addrOps, RiscOperators *valOps) const;
189 
195  void printRegisters(std::ostream &stream, const std::string &prefix = "");
196  virtual void printRegisters(std::ostream &stream, Formatter &fmt) const;
204  void printMemory(std::ostream &stream, const std::string &prefix = "") const;
205  virtual void printMemory(std::ostream &stream, Formatter &fmt) const;
210  void print(std::ostream &stream, const std::string &prefix = "") const;
211  virtual void print(std::ostream&, Formatter&) const;
217  std::string toString() const;
218 
221  StatePtr obj;
222  Formatter &fmt;
223  public:
224  WithFormatter(const StatePtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
225  void print(std::ostream &stream) const { obj->print(stream, fmt); }
226  };
227 
243  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
245  WithFormatter operator+(const std::string &linePrefix);
253  virtual bool merge(const StatePtr &other, RiscOperators *ops);
254 };
255 
256 std::ostream& operator<<(std::ostream&, const State&);
257 std::ostream& operator<<(std::ostream&, const State::WithFormatter&);
258 
259 } // namespace
260 } // namespace
261 } // namespace
262 } // namespace
263 
265 
266 #endif
267 #endif
virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value, RiscOperators *addrOps, RiscOperators *valOps)
Write a value to memory.
virtual void hash(Combinatorics::Hasher &, RiscOperators *addrOps, RiscOperators *valOps) const
Compute a hash of the state.
virtual StatePtr clone() const
Virtual copy constructor.
Definition: State.h:101
void printMemory(std::ostream &stream, const std::string &prefix="") const
Print memory contents.
virtual SValuePtr peekRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops)
Read register without side effects.
WithFormatter with_format(Formatter &fmt)
Used for printing states with formatting.
Definition: State.h:243
boost::shared_ptr< RegisterState > RegisterStatePtr
Shared-ownership pointer to a register state.
void zeroRegisters()
Initialize all registers to zero.
virtual void writeRegister(RegisterDescriptor desc, const SValuePtr &value, RiscOperators *ops)
Write a value to a register.
Main namespace for the ROSE library.
std::string toString() const
Convert the state to a string for debugging.
virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt, RiscOperators *addrOps, RiscOperators *valOps)
Read from memory without side effects.
boost::shared_ptr< State > StatePtr
Shared-ownership pointer to a semantic state.
static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory)
Instantiate a new state object with specified register and memory states.
Definition: State.h:81
MemoryStatePtr memoryState() const
Property: Memory state.
Definition: State.h:143
boost::shared_ptr< MemoryState > MemoryStatePtr
Shared-ownership pointer to a memory state.
virtual bool merge(const StatePtr &other, RiscOperators *ops)
Merge operation for data flow analysis.
Describes (part of) a physical CPU register.
WithFormatter operator+(Formatter &fmt)
Used for printing states with formatting.
Definition: State.h:244
virtual StatePtr create(const RegisterStatePtr &registers, const MemoryStatePtr &memory) const
Virtual constructor.
Definition: State.h:94
static StatePtr instance(const StatePtr &other)
Instantiate a new copy of an existing state.
Definition: State.h:86
virtual SValuePtr readRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops)
Read a value from a register.
Sawyer::SharedPointer< SValue > SValuePtr
Shared-ownership pointer to a semantic value in any domain.
void print(std::ostream &stream, const std::string &prefix="") const
Print the state.
RegisterStatePtr registerState() const
Property: Register state.
Definition: State.h:136
Base class for most instruction semantics RISC operators.
Definition: RiscOperators.h:49
Base class for semantics machine states.
Definition: State.h:39
void printRegisters(std::ostream &stream, const std::string &prefix="")
Print the register contents.
virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt, RiscOperators *addrOps, RiscOperators *valOps)
Read a value from memory.