ROSE  0.11.51.0
State.h
1 #ifndef ROSE_BinaryAnalysis_InstructionSemantics2_BaseSemantics_State_H
2 #define ROSE_BinaryAnalysis_InstructionSemantics2_BaseSemantics_State_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/nvp.hpp>
12 #include <boost/serialization/shared_ptr.hpp>
13 
14 namespace Rose {
15 namespace BinaryAnalysis {
16 namespace InstructionSemantics2 {
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() /*final*/ {
127  zero_registers();
128  }
129 
130  // [Robb Matzke 2021-03-18]: deprecated. This will be removed, although it is currently used. Users should be already
131  // using C++11 "override" in order to be notified when we make the switch.
132  virtual void zero_registers();
133 
137  void clearMemory() /*final*/ {
138  clear_memory();
139  }
140 
141  // [Robb Matzke 2021-03-18]: deprecated. This will be removed, although it is currently used. Users should be already
142  // using C++11 "override" in order to be notified when we make the switch.
143  virtual void clear_memory();
144 
149  return registers_;
150  }
151 
156  return memory_;
157  }
158 
163  virtual SValuePtr readRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops);
164 
169  virtual SValuePtr peekRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops);
170 
175  virtual void writeRegister(RegisterDescriptor desc, const SValuePtr &value, RiscOperators *ops);
176 
181  virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt,
182  RiscOperators *addrOps, RiscOperators *valOps);
183 
188  virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt,
189  RiscOperators *addrOps, RiscOperators *valOps);
190 
195  virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value, RiscOperators *addrOps, RiscOperators *valOps);
196 
200  virtual void hash(Combinatorics::Hasher&, RiscOperators *addrOps, RiscOperators *valOps) const;
201 
207  void printRegisters(std::ostream &stream, const std::string &prefix = "");
208  virtual void printRegisters(std::ostream &stream, Formatter &fmt) const;
216  void printMemory(std::ostream &stream, const std::string &prefix = "") const;
217  virtual void printMemory(std::ostream &stream, Formatter &fmt) const;
222  void print(std::ostream &stream, const std::string &prefix = "") const;
223  virtual void print(std::ostream&, Formatter&) const;
229  std::string toString() const;
230 
233  StatePtr obj;
234  Formatter &fmt;
235  public:
236  WithFormatter(const StatePtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
237  void print(std::ostream &stream) const { obj->print(stream, fmt); }
238  };
239 
255  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
257  WithFormatter operator+(const std::string &linePrefix);
265  virtual bool merge(const StatePtr &other, RiscOperators *ops);
266 };
267 
268 std::ostream& operator<<(std::ostream&, const State&);
269 std::ostream& operator<<(std::ostream&, const State::WithFormatter&);
270 
271 } // namespace
272 } // namespace
273 } // namespace
274 } // namespace
275 
277 
278 #endif
279 #endif
MemoryStatePtr memoryState() const
Property: Memory state.
Definition: State.h:155
void printMemory(std::ostream &stream, const std::string &prefix="") const
Print memory contents.
RegisterStatePtr registerState() const
Property: Register state.
Definition: State.h:148
boost::shared_ptr< MemoryState > MemoryStatePtr
Shared-ownership pointer to a memory state.
boost::shared_ptr< State > StatePtr
Shared-ownership pointer to a semantic state.
virtual SValuePtr peekRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops)
Read register without side effects.
virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value, RiscOperators *addrOps, RiscOperators *valOps)
Write a value to memory.
Main namespace for the ROSE library.
virtual StatePtr create(const RegisterStatePtr &registers, const MemoryStatePtr &memory) const
Virtual constructor.
Definition: State.h:94
WithFormatter operator+(Formatter &fmt)
Used for printing states with formatting.
Definition: State.h:256
virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt, RiscOperators *addrOps, RiscOperators *valOps)
Read from memory without side effects.
static StatePtr instance(const StatePtr &other)
Instantiate a new copy of an existing state.
Definition: State.h:86
virtual bool merge(const StatePtr &other, RiscOperators *ops)
Merge operation for data flow analysis.
Base classes for instruction semantics.
Definition: Dispatcher.h:18
Describes (part of) a physical CPU register.
StatePtr Ptr
Shared-ownership pointer for a State.
Definition: State.h:42
Base class for semantics machine states.
Definition: State.h:39
WithFormatter with_format(Formatter &fmt)
Used for printing states with formatting.
Definition: State.h:255
virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt, RiscOperators *addrOps, RiscOperators *valOps)
Read a value from memory.
Base class for most instruction semantics RISC operators.
Definition: RiscOperators.h:48
static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory)
Instantiate a new state object with specified register and memory states.
Definition: State.h:81
virtual StatePtr clone() const
Virtual copy constructor.
Definition: State.h:101
Sawyer::SharedPointer< SValue > SValuePtr
Shared-ownership pointer to a semantic value in any domain.
void zeroRegisters()
Initialize all registers to zero.
Definition: State.h:126
std::string toString() const
Convert the state to a string for debugging.
void printRegisters(std::ostream &stream, const std::string &prefix="")
Print the register contents.
virtual void writeRegister(RegisterDescriptor desc, const SValuePtr &value, RiscOperators *ops)
Write a value to a register.
virtual void hash(Combinatorics::Hasher &, RiscOperators *addrOps, RiscOperators *valOps) const
Compute a hash of the state.
void print(std::ostream &stream, const std::string &prefix="") const
Print the state.
boost::shared_ptr< RegisterState > RegisterStatePtr
Shared-ownership pointer to a register state.
virtual SValuePtr readRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops)
Read a value from a register.