ROSE  0.9.12.28
BaseSemanticsState.h
1 #ifndef ROSE_BinaryAnalysis_InstructionSemantics2_BaseSemantics_State_H
2 #define ROSE_BinaryAnalysis_InstructionSemantics2_BaseSemantics_State_H
3 
4 #include <BaseSemanticsTypes.h>
5 
6 #include <boost/serialization/access.hpp>
7 #include <boost/serialization/nvp.hpp>
8 #include <boost/serialization/shared_ptr.hpp>
9 
10 namespace Rose {
11 namespace BinaryAnalysis {
12 namespace InstructionSemantics2 {
13 namespace BaseSemantics {
14 
16 // State
18 
35 class State: public boost::enable_shared_from_this<State> {
36  SValuePtr protoval_; // Initial value used to create additional values as needed.
37  RegisterStatePtr registers_; // All machine register values for this semantic state.
38  MemoryStatePtr memory_; // All memory for this semantic state.
39 
41  // Serialization
42 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
43 private:
44  friend class boost::serialization::access;
45 
46  template<class S>
47  void serialize(S &s, const unsigned /*version*/) {
48  s & BOOST_SERIALIZATION_NVP(protoval_);
49  s & BOOST_SERIALIZATION_NVP(registers_);
50  s & BOOST_SERIALIZATION_NVP(memory_);
51  }
52 #endif
53 
54 
56  // Real constructors
57 protected:
58  // needed for serialization
59  State();
60  State(const RegisterStatePtr &registers, const MemoryStatePtr &memory);
61 
62  // deep-copy the registers and memory
63  State(const State &other);
64 
65 public:
67  typedef StatePtr Ptr;
68 
69 public:
70  virtual ~State();
71 
73  // Static allocating constructors
74 public:
76  static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory) {
77  return StatePtr(new State(registers, memory));
78  }
79 
81  static StatePtr instance(const StatePtr &other) {
82  return StatePtr(new State(*other));
83  }
84 
86  // Virtual constructors
87 public:
89  virtual StatePtr create(const RegisterStatePtr &registers, const MemoryStatePtr &memory) const {
90  return instance(registers, memory);
91  }
92 
96  virtual StatePtr clone() const {
97  StatePtr self = boost::const_pointer_cast<State>(shared_from_this());
98  return instance(self);
99  }
100 
102  // Dynamic pointer casts. No-op since this is the base class.
103 public:
104  static StatePtr promote(const StatePtr &x) {
105  ASSERT_not_null(x);
106  return x;
107  }
108 
110  // Other methods that are part of our API. Most of these just chain to either the register state and/or the memory state.
111 public:
113  SValuePtr protoval() const { return protoval_; }
114 
116  virtual void clear();
117 
121  virtual void zero_registers();
122 
126  virtual void clear_memory();
127 
132  return registers_;
133  }
134 
139  return memory_;
140  }
141 
146  virtual SValuePtr readRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops);
147 
152  virtual SValuePtr peekRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops);
153 
158  virtual void writeRegister(RegisterDescriptor desc, const SValuePtr &value, RiscOperators *ops);
159 
164  virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt,
165  RiscOperators *addrOps, RiscOperators *valOps);
166 
171  virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt,
172  RiscOperators *addrOps, RiscOperators *valOps);
173 
178  virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value, RiscOperators *addrOps, RiscOperators *valOps);
179 
185  void printRegisters(std::ostream &stream, const std::string &prefix = "");
186  virtual void printRegisters(std::ostream &stream, Formatter &fmt) const;
194  void printMemory(std::ostream &stream, const std::string &prefix = "") const;
195  virtual void printMemory(std::ostream &stream, Formatter &fmt) const;
200  void print(std::ostream &stream, const std::string &prefix = "") const;
201  virtual void print(std::ostream&, Formatter&) const;
206  StatePtr obj;
207  Formatter &fmt;
208  public:
209  WithFormatter(const StatePtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
210  void print(std::ostream &stream) const { obj->print(stream, fmt); }
211  };
212 
220  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
229  virtual bool merge(const StatePtr &other, RiscOperators *ops);
230 };
231 
232 std::ostream& operator<<(std::ostream&, const State&);
233 std::ostream& operator<<(std::ostream&, const State::WithFormatter&);
234 
235 } // namespace
236 } // namespace
237 } // namespace
238 } // namespace
239 
240 #endif
MemoryStatePtr memoryState() const
Property: Memory state.
void printMemory(std::ostream &stream, const std::string &prefix="") const
Print memory contents.
RegisterStatePtr registerState() const
Property: Register state.
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.
WithFormatter operator+(Formatter &fmt)
Used for printing states with formatting.
virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt, RiscOperators *addrOps, RiscOperators *valOps)
Read from memory without side effects.
virtual void clear_memory()
Clear all memory locations.
virtual void zero_registers()
Initialize all registers to zero.
static StatePtr instance(const StatePtr &other)
Instantiate a new copy of an existing state.
virtual bool merge(const StatePtr &other, RiscOperators *ops)
Merge operation for data flow analysis.
Describes (part of) a physical CPU register.
WithFormatter with_format(Formatter &fmt)
Used for printing states with formatting.
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.
static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory)
Instantiate a new state object with specified register and memory states.
virtual StatePtr clone() const
Virtual copy constructor.
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.
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.
StatePtr Ptr
Shared-ownership pointer for a State.
virtual SValuePtr readRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops)
Read a value from a register.