ROSE 0.11.145.147
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#include <Rose/BinaryAnalysis/InstructionSemantics/BaseSemantics/BasicTypes.h>
6
7#include <Rose/BinaryAnalysis/RegisterDescriptor.h>
8#include <Combinatorics.h> // rose
9
10#include <boost/enable_shared_from_this.hpp>
11#include <boost/serialization/access.hpp>
12#include <boost/serialization/export.hpp>
13#include <boost/serialization/nvp.hpp>
14#include <boost/serialization/shared_ptr.hpp>
15
16namespace Rose {
17namespace BinaryAnalysis {
18namespace InstructionSemantics {
19namespace BaseSemantics {
20
22// State
24
41class State: public boost::enable_shared_from_this<State> {
42public:
44 using Ptr = StatePtr;
45
46private:
47 SValuePtr protoval_; // Initial value used to create additional values as needed.
48 RegisterStatePtr registers_; // All machine register values for this semantic state.
49 MemoryStatePtr memory_; // All memory for this semantic state.
50 RegisterStatePtr interrupts_; // Whether interrupts occurred.
51
53 // Serialization
54#ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
55private:
56 friend class boost::serialization::access;
57
58 template<class S>
59 void serialize(S &s, const unsigned version) {
60 s & BOOST_SERIALIZATION_NVP(protoval_);
61 s & BOOST_SERIALIZATION_NVP(registers_);
62 s & BOOST_SERIALIZATION_NVP(memory_);
63 if (version > 0)
64 s & BOOST_SERIALIZATION_NVP(interrupts_);
65 }
66#endif
67
68
70 // Real constructors
71protected:
72 // needed for serialization
73 State();
74
75 State(const RegisterStatePtr &registers, const MemoryStatePtr &memory, const RegisterStatePtr &interrupts);
76 State(const RegisterStatePtr &registers, const MemoryStatePtr &memory);
77
78 // deep-copy the registers and memory
79 State(const State &other);
80
81public:
82 virtual ~State();
83
85 // Static allocating constructors
86public:
88 static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory, const RegisterStatePtr &interrupts);
89
91 static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory);
92
94 static StatePtr instance(const StatePtr &other);
95
97 // Virtual constructors
98public:
102 virtual StatePtr create(const RegisterStatePtr &registers, const MemoryStatePtr &memory) const;
103
107 virtual StatePtr clone() const;
108
110 // Dynamic pointer casts. No-op since this is the base class.
111public:
112 static StatePtr promote(const StatePtr&);
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.
116public:
119
121 virtual void clear();
122
127
132
137
142
159 bool hasInterruptState() const;
160
166
172
177 virtual void writeRegister(RegisterDescriptor desc, const SValuePtr &value, RiscOperators *ops);
178
183 virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt,
184 RiscOperators *addrOps, RiscOperators *valOps);
185
190 virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt,
191 RiscOperators *addrOps, RiscOperators *valOps);
192
197 virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value, RiscOperators *addrOps, RiscOperators *valOps);
198
205 virtual SValuePtr readInterrupt(unsigned major, unsigned minor, const SValuePtr &dflt, RiscOperators *valOps);
206
213 virtual SValuePtr peekInterrupt(unsigned major, unsigned minor, const SValuePtr &dflt, RiscOperators *valOps);
214
220 virtual bool writeInterrupt(unsigned major, unsigned minor, const SValuePtr &value, RiscOperators *valOps);
221
226 SValuePtr raiseInterrupt(unsigned major, unsigned minor, RiscOperators *valOps);
227
232 SValuePtr clearInterrupt(unsigned major, unsigned minor, RiscOperators *valOps);
233
241 bool isInterruptDefinitelyRaised(unsigned major, unsigned minor, RiscOperators *valOps);
242
250 bool isInterruptDefinitelyClear(unsigned major, unsigned minor, RiscOperators *valOps);
251
255 virtual void hash(Combinatorics::Hasher&, RiscOperators *addrOps, RiscOperators *valOps) const;
256
262 void printRegisters(std::ostream &stream, const std::string &prefix = "");
263 virtual void printRegisters(std::ostream &stream, Formatter &fmt) const;
271 void printMemory(std::ostream &stream, const std::string &prefix = "") const;
272 virtual void printMemory(std::ostream &stream, Formatter &fmt) const;
281 void printInterrupts(std::ostream&, const std::string &prefix = "");
282 virtual void printInterrupts(std::ostream &stream, Formatter &fmt) const;
287 void print(std::ostream &stream, const std::string &prefix = "") const;
288 virtual void print(std::ostream&, Formatter&) const;
294 std::string toString() const;
295
298 StatePtr obj;
299 Formatter &fmt;
300 public:
301 WithFormatter(const StatePtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
302 void print(std::ostream &stream) const { obj->print(stream, fmt); }
303 };
304
320 WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
322 WithFormatter operator+(const std::string &linePrefix);
330 virtual bool merge(const StatePtr &other, RiscOperators *ops);
331};
332
333std::ostream& operator<<(std::ostream&, const State&);
334std::ostream& operator<<(std::ostream&, const State::WithFormatter&);
335
336} // namespace
337} // namespace
338} // namespace
339} // namespace
340
341#ifdef BOOST_HAVE_SERIALIZATION_LIB
344#endif
345
346#endif
347#endif
Base class for most instruction semantics RISC operators.
Base class for semantics machine states.
Definition State.h:41
RegisterStatePtr interruptState() const
Property: Interrupt state.
virtual void writeRegister(RegisterDescriptor desc, const SValuePtr &value, RiscOperators *ops)
Write a value to a register.
MemoryStatePtr memoryState() const
Property: Memory state.
WithFormatter with_format(Formatter &fmt)
Used for printing states with formatting.
Definition State.h:320
virtual bool writeInterrupt(unsigned major, unsigned minor, const SValuePtr &value, RiscOperators *valOps)
Write an interrupt state.
void printMemory(std::ostream &stream, const std::string &prefix="") const
Print memory contents.
std::string toString() const
Convert the state to a string for debugging.
virtual void printInterrupts(std::ostream &stream, Formatter &fmt) const
Print interrupt states.
static StatePtr instance(const StatePtr &other)
Instantiate a new copy of an existing state.
virtual SValuePtr readInterrupt(unsigned major, unsigned minor, const SValuePtr &dflt, RiscOperators *valOps)
Read an interrupt state.
virtual StatePtr create(const RegisterStatePtr &registers, const MemoryStatePtr &memory) const
Virtual constructor.
bool isInterruptDefinitelyClear(unsigned major, unsigned minor, RiscOperators *valOps)
Test an interrupt.
static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory, const RegisterStatePtr &interrupts)
Instantiate a new state object with specified register, memory, and interrupt states.
virtual bool merge(const StatePtr &other, RiscOperators *ops)
Merge operation for data flow analysis.
bool hasInterruptState() const
Tests whether an interrupt state is present.
SValuePtr raiseInterrupt(unsigned major, unsigned minor, RiscOperators *valOps)
Raise an interrupt.
WithFormatter operator+(Formatter &fmt)
Used for printing states with formatting.
Definition State.h:321
virtual void printRegisters(std::ostream &stream, Formatter &fmt) const
Print the register contents.
virtual SValuePtr peekRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops)
Read register without side effects.
virtual void hash(Combinatorics::Hasher &, RiscOperators *addrOps, RiscOperators *valOps) const
Compute a hash of the state.
virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt, RiscOperators *addrOps, RiscOperators *valOps)
Read a value from memory.
virtual SValuePtr peekInterrupt(unsigned major, unsigned minor, const SValuePtr &dflt, RiscOperators *valOps)
Read an interrupt state without side effects.
virtual void print(std::ostream &, Formatter &) const
Print the state.
SValuePtr clearInterrupt(unsigned major, unsigned minor, RiscOperators *valOps)
Clear an interrupt.
bool isInterruptDefinitelyRaised(unsigned major, unsigned minor, RiscOperators *valOps)
Test an interrupt.
void print(std::ostream &stream, const std::string &prefix="") const
Print the state.
virtual StatePtr clone() const
Virtual copy constructor.
void interruptState(const RegisterStatePtr &)
Property: Interrupt state.
WithFormatter operator+(const std::string &linePrefix)
Used for printing states with formatting.
virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value, RiscOperators *addrOps, RiscOperators *valOps)
Write a value to memory.
virtual void printMemory(std::ostream &stream, Formatter &fmt) const
Print memory contents.
void printRegisters(std::ostream &stream, const std::string &prefix="")
Print the register contents.
RegisterStatePtr registerState() const
Property: Register state.
virtual SValuePtr readRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops)
Read a value from a register.
virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt, RiscOperators *addrOps, RiscOperators *valOps)
Read from memory without side effects.
static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory)
Instantiate a new state object with specified register and memory states.
void zeroRegisters()
Initialize all registers to zero.
void printInterrupts(std::ostream &, const std::string &prefix="")
Print interrupt states.
Describes (part of) a physical CPU register.
boost::shared_ptr< MemoryState > MemoryStatePtr
Shared-ownership pointer to a memory state.
boost::shared_ptr< RegisterState > RegisterStatePtr
Shared-ownership pointer to a register state.
The ROSE library.