ROSE 2.1.0
Loading...
Searching...
No Matches
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 std::vector<AddressSpacePtr> addressSpaces_; // ordered address spaces
52 SValuePtr protoval_; // Initial value used to create additional values as needed.
53 RegisterStatePtr registers_; // All machine register values for this semantic state.
54 MemoryStatePtr memory_; // All memory for this semantic state.
55 RegisterStatePtr interrupts_; // Whether interrupts occurred.
56 FrameStatePtr frame_; // Stores JVM local variables and operand stack for executing method.
57
59 // Serialization.
61#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
62private:
63 friend class boost::serialization::access;
64
65 template<class S>
66 void serialize(S &s, const unsigned version) {
67 ASSERT_require2(version >= 2, version);
68 s & BOOST_SERIALIZATION_NVP(protoval_);
69 s & BOOST_SERIALIZATION_NVP(addressSpaces_);
70 s & BOOST_SERIALIZATION_NVP(registers_);
71 s & BOOST_SERIALIZATION_NVP(memory_);
72 s & BOOST_SERIALIZATION_NVP(interrupts_);
73 s & BOOST_SERIALIZATION_NVP(frame_);
74 }
75#endif
76
77
79 // Real constructors.
81protected:
82 // needed for serialization
83 State();
84
85 State(const RegisterStatePtr &registers, const MemoryStatePtr &memory, const RegisterStatePtr &interrupts, const FrameStatePtr &frame);
86 State(const RegisterStatePtr &registers, const MemoryStatePtr &memory, const RegisterStatePtr &interrupts);
87 State(const RegisterStatePtr &registers, const MemoryStatePtr &memory);
88
89 // deep-copy the registers and memory
90 State(const State &other);
91
92public:
93 virtual ~State();
94
96 // Static allocating constructors
98public:
100 static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory,
101 const RegisterStatePtr &interrupts, const FrameStatePtr &frame);
102
104 static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory, const RegisterStatePtr &interrupts);
105
107 static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory);
108
113 static StatePtr instance(const StatePtr &other);
114
116 // Virtual constructors.
118public:
123 virtual StatePtr create(const RegisterStatePtr &registers, const MemoryStatePtr &memory) const;
124
129 virtual StatePtr clone() const;
130
132 // Dynamic pointer casts. No-op since this is the base class.
134public:
135 static StatePtr promote(const StatePtr&);
136
138 // Properties of a state.
140public:
151
153 // Address space declaration and searching.
155public:
164
166 const std::vector<AddressSpacePtr>& addressSpaces() const;
167
173
179
181 // Low-level operations on address spaces.
183public:
199 virtual SValuePtr read(const AddressSpacePtr&, const AddressSpaceAddress&, const SValuePtr &dflt,
200 RiscOperators &addrOps, RiscOperators &valOps);
201
205 virtual SValuePtr peek(const AddressSpacePtr&, const AddressSpaceAddress&, const SValuePtr &dflt,
206 RiscOperators &addrOps, RiscOperators &valOps);
207
219 virtual void write(const AddressSpacePtr&, const AddressSpaceAddress&, const SValuePtr &value,
220 RiscOperators &addrOps, RiscOperators &valOps);
221
234 virtual bool merge(const StatePtr &other, RiscOperators *addrOps, RiscOperators *valOps);
235
237 // Operations on address spaces found by searching.
239public:
244 virtual void clear();
245
252
259
265
272
284
296
303 bool hasInterruptState() const;
304
313
319 bool hasFrameState() const;
320
321 virtual SValuePtr readLocal(uint8_t index);
322 virtual void writeLocal(uint8_t index, const SValuePtr &value);
323
328
332 virtual void pushOperand(const SValuePtr &value);
333
338
343
347 virtual void writeRegister(RegisterDescriptor desc, const SValuePtr &value, RiscOperators *ops);
348
352 virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt,
353 RiscOperators *addrOps, RiscOperators *valOps);
354
358 virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt, RiscOperators *addrOps, RiscOperators *valOps);
359
363 virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value, RiscOperators *addrOps, RiscOperators *valOps);
364
373 virtual SValuePtr readInterrupt(unsigned major, unsigned minor, const SValuePtr &dflt, RiscOperators *valOps);
374
383 virtual SValuePtr peekInterrupt(unsigned major, unsigned minor, const SValuePtr &dflt, RiscOperators *valOps);
384
391 virtual bool writeInterrupt(unsigned major, unsigned minor, const SValuePtr &value, RiscOperators *valOps);
392
397 SValuePtr raiseInterrupt(unsigned major, unsigned minor, RiscOperators *valOps);
398
404 SValuePtr clearInterrupt(unsigned major, unsigned minor, RiscOperators *valOps);
405
413 bool isInterruptDefinitelyRaised(unsigned major, unsigned minor, RiscOperators *valOps);
414
422 bool isInterruptDefinitelyClear(unsigned major, unsigned minor, RiscOperators *valOps);
423
428 virtual void hash(Combinatorics::Hasher&, RiscOperators *addrOps, RiscOperators *valOps) const;
429
436 void printRegisters(std::ostream &stream, const std::string &prefix = "");
437 virtual void printRegisters(std::ostream &stream, Formatter &fmt) const;
445 void printMemory(std::ostream &stream, const std::string &prefix = "") const;
446 virtual void printMemory(std::ostream &stream, Formatter &fmt) const;
455 void printInterrupts(std::ostream&, const std::string &prefix = "");
456 virtual void printInterrupts(std::ostream &stream, Formatter &fmt) const;
465 void print(std::ostream &stream, const std::string &prefix = "") const;
466 virtual void print(std::ostream&, Formatter&) const;
472 std::string toString() const;
473
476 StatePtr obj;
477 Formatter &fmt;
478 public:
479 WithFormatter(const StatePtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
480 void print(std::ostream &stream) const { obj->print(stream, fmt); }
481 };
482
498 WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
500 WithFormatter operator+(const std::string &linePrefix);
508 virtual bool merge(const StatePtr &other, RiscOperators *ops);
509};
510
511std::ostream& operator<<(std::ostream&, const State&);
512std::ostream& operator<<(std::ostream&, const State::WithFormatter&);
513
514} // namespace
515} // namespace
516} // namespace
517} // namespace
518
519#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
522#endif
523
524#endif
525#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:498
virtual bool merge(const StatePtr &other, RiscOperators *addrOps, RiscOperators *valOps)
Merge operation for data flow analysis.
virtual void pushOperand(const SValuePtr &value)
Push an operand value to a frame.
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.
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:499
virtual SValuePtr popOperand()
Pop an operand value from the frame.
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.
static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory, const RegisterStatePtr &interrupts, const FrameStatePtr &frame)
Instantiate a new state object with specified register, memory, interrupt, and frame address spaces.
virtual SValuePtr readRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops)
Read a value from a register.
FrameStatePtr frameState() const
Property: Frame state.
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.
bool hasFrameState() const
Tests whether an frame state is present.
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< FrameState > FrameStatePtr
Shared-ownership pointer to a frame state.
boost::shared_ptr< RegisterState > RegisterStatePtr
Shared-ownership pointer to a register state.
The ROSE library.