ROSE 0.11.145.237
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
12#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
13#include <boost/serialization/access.hpp>
14#include <boost/serialization/export.hpp>
15#include <boost/serialization/nvp.hpp>
16#include <boost/serialization/shared_ptr.hpp>
17#endif
18
19namespace Rose {
20namespace BinaryAnalysis {
21namespace InstructionSemantics {
22namespace BaseSemantics {
23
25// State
27
45class State: public boost::enable_shared_from_this<State> {
46public:
48 using Ptr = StatePtr;
49
50private:
51 SValuePtr protoval_; // initial value used to create additional values as needed.
52 std::vector<AddressSpacePtr> addressSpaces_; // ordered address spaces
53
55 // Serialization.
57#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
58private:
59 friend class boost::serialization::access;
60
61 template<class S>
62 void serialize(S &s, const unsigned version) {
63 ASSERT_require2(version >= 2, version);
64 s & BOOST_SERIALIZATION_NVP(protoval_);
65 s & BOOST_SERIALIZATION_NVP(addressSpaces_);
66 }
67#endif
68
69
71 // Real constructors.
73protected:
74 // needed for serialization
75 State();
76
77 State(const RegisterStatePtr &registers, const MemoryStatePtr &memory, const RegisterStatePtr &interrupts);
78 State(const RegisterStatePtr &registers, const MemoryStatePtr &memory);
79
80 // deep-copy the registers and memory
81 State(const State &other);
82
83public:
84 virtual ~State();
85
87 // Static allocating constructors
89public:
91 static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory, const RegisterStatePtr &interrupts);
92
94 static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory);
95
100 static StatePtr instance(const StatePtr &other);
101
103 // Virtual constructors.
105public:
110 virtual StatePtr create(const RegisterStatePtr &registers, const MemoryStatePtr &memory) const;
111
116 virtual StatePtr clone() const;
117
119 // Dynamic pointer casts. No-op since this is the base class.
121public:
122 static StatePtr promote(const StatePtr&);
123
125 // Properties of a state.
127public:
138
140 // Address space declaration and searching.
142public:
151
153 const std::vector<AddressSpacePtr>& addressSpaces() const;
154
160
166
168 // Low-level operations on address spaces.
170public:
186 virtual SValuePtr read(const AddressSpacePtr&, const AddressSpaceAddress&, const SValuePtr &dflt,
187 RiscOperators &addrOps, RiscOperators &valOps);
188
192 virtual SValuePtr peek(const AddressSpacePtr&, const AddressSpaceAddress&, const SValuePtr &dflt,
193 RiscOperators &addrOps, RiscOperators &valOps);
194
206 virtual void write(const AddressSpacePtr&, const AddressSpaceAddress&, const SValuePtr &value,
207 RiscOperators &addrOps, RiscOperators &valOps);
208
221 virtual bool merge(const StatePtr &other, RiscOperators *addrOps, RiscOperators *valOps);
222
224 // Operations on address spaces found by searching.
226public:
231 virtual void clear();
232
239
246
252
259
271
283
290 bool hasInterruptState() const;
291
296
301
305 virtual void writeRegister(RegisterDescriptor desc, const SValuePtr &value, RiscOperators *ops);
306
310 virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt,
311 RiscOperators *addrOps, RiscOperators *valOps);
312
316 virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt, RiscOperators *addrOps, RiscOperators *valOps);
317
321 virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value, RiscOperators *addrOps, RiscOperators *valOps);
322
331 virtual SValuePtr readInterrupt(unsigned major, unsigned minor, const SValuePtr &dflt, RiscOperators *valOps);
332
341 virtual SValuePtr peekInterrupt(unsigned major, unsigned minor, const SValuePtr &dflt, RiscOperators *valOps);
342
349 virtual bool writeInterrupt(unsigned major, unsigned minor, const SValuePtr &value, RiscOperators *valOps);
350
355 SValuePtr raiseInterrupt(unsigned major, unsigned minor, RiscOperators *valOps);
356
362 SValuePtr clearInterrupt(unsigned major, unsigned minor, RiscOperators *valOps);
363
371 bool isInterruptDefinitelyRaised(unsigned major, unsigned minor, RiscOperators *valOps);
372
380 bool isInterruptDefinitelyClear(unsigned major, unsigned minor, RiscOperators *valOps);
381
386 virtual void hash(Combinatorics::Hasher&, RiscOperators *addrOps, RiscOperators *valOps) const;
387
394 void printRegisters(std::ostream &stream, const std::string &prefix = "");
395 virtual void printRegisters(std::ostream &stream, Formatter &fmt) const;
403 void printMemory(std::ostream &stream, const std::string &prefix = "") const;
404 virtual void printMemory(std::ostream &stream, Formatter &fmt) const;
413 void printInterrupts(std::ostream&, const std::string &prefix = "");
414 virtual void printInterrupts(std::ostream &stream, Formatter &fmt) const;
423 void print(std::ostream &stream, const std::string &prefix = "") const;
424 virtual void print(std::ostream&, Formatter&) const;
430 std::string toString() const;
431
434 StatePtr obj;
435 Formatter &fmt;
436 public:
437 WithFormatter(const StatePtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
438 void print(std::ostream &stream) const { obj->print(stream, fmt); }
439 };
440
456 WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
458 WithFormatter operator+(const std::string &linePrefix);
461};
462
463std::ostream& operator<<(std::ostream&, const State&);
464std::ostream& operator<<(std::ostream&, const State::WithFormatter&);
465
466} // namespace
467} // namespace
468} // namespace
469} // namespace
470
471#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
474#endif
475
476#endif
477#endif
Base class for most instruction semantics RISC operators.
Base class for semantics machine states.
Definition State.h:45
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:456
virtual bool merge(const StatePtr &other, RiscOperators *addrOps, RiscOperators *valOps)
Merge operation for data flow analysis.
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.
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:457
virtual SValuePtr read(const AddressSpacePtr &, const AddressSpaceAddress &, const SValuePtr &dflt, RiscOperators &addrOps, RiscOperators &valOps)
Read a value from an address space.
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.
void insertAddressSpace(const AddressSpacePtr &)
Insert an address space into this state.
RegisterStatePtr registerState() const
Property: Register state.
virtual SValuePtr peek(const AddressSpacePtr &, const AddressSpaceAddress &, const SValuePtr &dflt, RiscOperators &addrOps, RiscOperators &valOps)
Peek at a value in an address space.
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.
virtual void write(const AddressSpacePtr &, const AddressSpaceAddress &, const SValuePtr &value, RiscOperators &addrOps, RiscOperators &valOps)
Write a value to an address space.
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.