ROSE 0.11.145.192
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
42class State: public boost::enable_shared_from_this<State> {
43public:
45 using Ptr = StatePtr;
46
47private:
48 SValuePtr protoval_; // initial value used to create additional values as needed.
49 std::vector<AddressSpacePtr> addressSpaces_; // ordered address spaces
50
52 // 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 ASSERT_require2(version >= 2, version);
61 s & BOOST_SERIALIZATION_NVP(protoval_);
62 s & BOOST_SERIALIZATION_NVP(addressSpaces_);
63 }
64#endif
65
66
68 // Real constructors.
70protected:
71 // needed for serialization
72 State();
73
74 State(const RegisterStatePtr &registers, const MemoryStatePtr &memory, const RegisterStatePtr &interrupts);
75 State(const RegisterStatePtr &registers, const MemoryStatePtr &memory);
76
77 // deep-copy the registers and memory
78 State(const State &other);
79
80public:
81 virtual ~State();
82
84 // 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
97 static StatePtr instance(const StatePtr &other);
98
100 // Virtual constructors.
102public:
107 virtual StatePtr create(const RegisterStatePtr &registers, const MemoryStatePtr &memory) const;
108
113 virtual StatePtr clone() const;
114
116 // Dynamic pointer casts. No-op since this is the base class.
118public:
119 static StatePtr promote(const StatePtr&);
120
122 // Properties of a state.
124public:
135
137 // Address space declaration and searching.
139public:
148
150 const std::vector<AddressSpacePtr>& addressSpaces() const;
151
157
163
165 // Low-level operations on address spaces.
167public:
184 RiscOperators &addrOps, RiscOperators &valOps);
185
190 RiscOperators &addrOps, RiscOperators &valOps);
191
203 void write(const AddressSpacePtr&, const AddressSpaceAddress&, const SValuePtr &value,
204 RiscOperators &addrOps, RiscOperators &valOps);
205
218 virtual bool merge(const StatePtr &other, RiscOperators *addrOps, RiscOperators *valOps);
219
221 // Operations on address spaces found by searching.
223public:
228 virtual void clear();
229
236
243
249
255
267
279
286 bool hasInterruptState() const;
287
292
297
301 virtual void writeRegister(RegisterDescriptor desc, const SValuePtr &value, RiscOperators *ops);
302
306 virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt,
307 RiscOperators *addrOps, RiscOperators *valOps);
308
312 virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt, RiscOperators *addrOps, RiscOperators *valOps);
313
317 virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value, RiscOperators *addrOps, RiscOperators *valOps);
318
327 virtual SValuePtr readInterrupt(unsigned major, unsigned minor, const SValuePtr &dflt, RiscOperators *valOps);
328
337 virtual SValuePtr peekInterrupt(unsigned major, unsigned minor, const SValuePtr &dflt, RiscOperators *valOps);
338
345 virtual bool writeInterrupt(unsigned major, unsigned minor, const SValuePtr &value, RiscOperators *valOps);
346
351 SValuePtr raiseInterrupt(unsigned major, unsigned minor, RiscOperators *valOps);
352
358 SValuePtr clearInterrupt(unsigned major, unsigned minor, RiscOperators *valOps);
359
367 bool isInterruptDefinitelyRaised(unsigned major, unsigned minor, RiscOperators *valOps);
368
376 bool isInterruptDefinitelyClear(unsigned major, unsigned minor, RiscOperators *valOps);
377
382 virtual void hash(Combinatorics::Hasher&, RiscOperators *addrOps, RiscOperators *valOps) const;
383
390 void printRegisters(std::ostream &stream, const std::string &prefix = "");
391 virtual void printRegisters(std::ostream &stream, Formatter &fmt) const;
399 void printMemory(std::ostream &stream, const std::string &prefix = "") const;
400 virtual void printMemory(std::ostream &stream, Formatter &fmt) const;
409 void printInterrupts(std::ostream&, const std::string &prefix = "");
410 virtual void printInterrupts(std::ostream &stream, Formatter &fmt) const;
419 void print(std::ostream &stream, const std::string &prefix = "") const;
420 virtual void print(std::ostream&, Formatter&) const;
426 std::string toString() const;
427
430 StatePtr obj;
431 Formatter &fmt;
432 public:
433 WithFormatter(const StatePtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
434 void print(std::ostream &stream) const { obj->print(stream, fmt); }
435 };
436
452 WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
454 WithFormatter operator+(const std::string &linePrefix);
457};
458
459std::ostream& operator<<(std::ostream&, const State&);
460std::ostream& operator<<(std::ostream&, const State::WithFormatter&);
461
462} // namespace
463} // namespace
464} // namespace
465} // namespace
466
467#ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
470#endif
471
472#endif
473#endif
Base class for most instruction semantics RISC operators.
Base class for semantics machine states.
Definition State.h:42
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:452
virtual bool merge(const StatePtr &other, RiscOperators *addrOps, RiscOperators *valOps)
Merge operation for data flow analysis.
SValuePtr read(const AddressSpacePtr &, const AddressSpaceAddress &, const SValuePtr &dflt, RiscOperators &addrOps, RiscOperators &valOps)
Read a value from an address space.
virtual bool writeInterrupt(unsigned major, unsigned minor, const SValuePtr &value, RiscOperators *valOps)
Write an interrupt state.
SValuePtr protoval() const
Property: Prototypical value.
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.
AddressSpacePtr findFirstAddressSpace(AddressSpacePurpose, const std::string &name) const
Find the first address space with the specified purpose and name.
virtual SValuePtr readInterrupt(unsigned major, unsigned minor, const SValuePtr &dflt, RiscOperators *valOps)
Read an interrupt state.
SValuePtr peek(const AddressSpacePtr &, const AddressSpaceAddress &, const SValuePtr &dflt, RiscOperators &addrOps, RiscOperators &valOps)
Peek at a value in an address space.
const std::vector< AddressSpacePtr > & addressSpaces() const
The list of all address spaces.
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 address spaces.
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:453
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.
void write(const AddressSpacePtr &, const AddressSpaceAddress &, const SValuePtr &value, RiscOperators &addrOps, RiscOperators &valOps)
Write a value to an address space.
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.
void insertAddressSpace(const AddressSpacePtr &)
Insert an address space into this state.
RegisterStatePtr registerState() const
Property: Register state.
virtual SValuePtr readRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops)
Read a value from a register.
AddressSpacePtr findFirstAddressSpace(AddressSpacePurpose) const
Find the first address space with the specified purpose.
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 address spaces.
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.
Base classes for instruction semantics.
boost::shared_ptr< MemoryState > MemoryStatePtr
Shared-ownership pointer to a memory state.
boost::shared_ptr< AddressSpace > AddressSpacePtr
Shared-ownership pointer for AddressSpace objects.
boost::shared_ptr< RegisterState > RegisterStatePtr
Shared-ownership pointer to a register state.
The ROSE library.