ROSE 0.11.145.237
Dispatcher.h
1#ifndef ROSE_BinaryAnalysis_InstructionSemantics_BaseSemantics_Dispatcher_H
2#define ROSE_BinaryAnalysis_InstructionSemantics_BaseSemantics_Dispatcher_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_BINARY_ANALYSIS
5
6#include <Rose/BinaryAnalysis/BasicTypes.h>
7
8#include <boost/enable_shared_from_this.hpp>
9
10#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
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#endif
16
17namespace Rose {
18namespace BinaryAnalysis {
19namespace InstructionSemantics {
20namespace BaseSemantics {
21
23// Instruction Dispatcher
25
28public:
29 virtual ~InsnProcessor() {}
30 virtual void process(const DispatcherPtr &dispatcher, SgAsmInstruction *insn) = 0;
31};
32
46class Dispatcher: public boost::enable_shared_from_this<Dispatcher> {
47public:
50
51private:
52 Architecture::BaseConstPtr architecture_; // Required architecture
53 RiscOperatorsPtr operators_;
54
55protected:
58 // Dispatchers keep a table of all the kinds of instructions they can handle. The lookup key is typically some sort of
59 // instruction identifier, such as from SgAsmX86Instruction::get_kind(), and comes from the iprocKey() virtual method.
60 typedef std::vector<InsnProcessor*> InsnProcessors;
61 InsnProcessors iproc_table;
62
63#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
64private:
65 friend class boost::serialization::access;
66
67 template<class S>
68 void serializeCommon(S &s, const unsigned version) {
69 ASSERT_always_require(version >= 2);
70 s & boost::serialization::make_nvp("operators", operators_); // for backward compatibility
71 s & BOOST_SERIALIZATION_NVP(autoResetInstructionPointer_);
72 //s & iproc_table; -- not saved
73 }
74
75 template<class S>
76 void save(S &s, const unsigned version) const {
77 const_cast<Dispatcher*>(this)->serializeCommon(s, version);
78 ASSERT_not_null(architecture_);
79 const std::string architecture = Architecture::name(architecture_);
80 s & BOOST_SERIALIZATION_NVP(architecture);
81 }
82
83 template<class S>
84 void load(S &s, const unsigned version) {
85 serializeCommon(s, version);
86 std::string architecture;
87 s & BOOST_SERIALIZATION_NVP(architecture);
88 architecture_ = Architecture::findByName(architecture).orThrow();
89 }
90
91 BOOST_SERIALIZATION_SPLIT_MEMBER();
92#endif
93
95 // Real constructors
96protected:
97 Dispatcher(); // used only by boost::serialization
98
99 // Prototypical constructor
100 explicit Dispatcher(const Architecture::BaseConstPtr&);
101
102 Dispatcher(const Architecture::BaseConstPtr&, const RiscOperatorsPtr&);
103
104public:
105 virtual ~Dispatcher();
106
108 // Static allocating constructors. None since this is an abstract class
109
110
112 // Virtual constructors
113public:
115 virtual DispatcherPtr create(const RiscOperatorsPtr &ops) const = 0;
116
118 // Methods to process instructions
119public:
124
130 virtual void processDelaySlot(SgAsmInstruction *delayInsn);
131
132protected:
133 // The part of processing instructions that's common to both `processInstruction` and `processDelaySlot`.
134 virtual void processCommon();
135
137 // Instruction processor table operations
138public:
143
147 virtual void iprocReplace(SgAsmInstruction *insn, InsnProcessor *iproc);
148
150 virtual int iprocKey(SgAsmInstruction*) const = 0;
151
155 virtual void iprocSet(int key, InsnProcessor *iproc);
156
158 virtual InsnProcessor *iprocGet(int key);
159
161 // Convenience methods that defer the call to some member object
162public:
167
174 virtual void operators(const RiscOperatorsPtr&);
179 virtual StatePtr currentState() const;
180
182 virtual SValuePtr protoval() const;
183
189
193 virtual SValuePtr undefined_(size_t nbits) const;
194 virtual SValuePtr unspecified_(size_t nbits) const;
198 virtual SValuePtr number_(size_t nbits, uint64_t number) const;
199
201 // Methods related to registers
202public:
210
215 virtual RegisterDescriptor findRegister(const std::string &regname, size_t nbits=0, bool allowMissing=false) const;
216
221 size_t addressWidth() const;
222
225
228
231
234
247 // Miscellaneous methods that tend to be the same for most dispatchers
248public:
254 virtual void initializeState(const StatePtr&);
255
265
270
275
280
285 virtual void preUpdate(SgAsmExpression*, const BaseSemantics::SValuePtr &enabled);
286
292
296 virtual SValuePtr effectiveAddress(SgAsmExpression*, size_t nbits=0);
297
303 virtual SValuePtr read(SgAsmExpression*, size_t value_nbits=0, size_t addr_nbits=0);
304
308 virtual void write(SgAsmExpression*, const SValuePtr &value, size_t addr_nbits=0);
309};
310
311} // namespace
312} // namespace
313} // namespace
314} // namespace
315
316#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
319#endif
320
321#endif
322#endif
Dispatches instructions through the RISC layer.
Definition Dispatcher.h:46
virtual void operators(const RiscOperatorsPtr &)
Property: RISC operators.
virtual void initializeState(const StatePtr &)
Initialize the state.
Architecture::BaseConstPtr architecture() const
Property: Architecture.
virtual InsnProcessor * iprocLookup(SgAsmInstruction *insn)
Lookup the processor for an instruction.
virtual RegisterDescriptor stackPointerRegister() const
Returns the stack pointer register.
virtual void postUpdate(SgAsmExpression *, const BaseSemantics::SValuePtr &enabled)
Update registers for post-add expressions.
virtual SValuePtr protoval() const
Return the prototypical value.
bool autoResetInstructionPointer_
Reset instruction pointer register for each instruction.
Definition Dispatcher.h:56
virtual void preUpdate(SgAsmExpression *, const BaseSemantics::SValuePtr &enabled)
Update registers for pre-add expressions.
virtual SValuePtr unspecified_(size_t nbits) const
Return a new undefined semantic value.
virtual SValuePtr read(SgAsmExpression *, size_t value_nbits=0, size_t addr_nbits=0)
Reads an R-value expression.
virtual StatePtr currentState() const
Get a pointer to the state object.
virtual RegisterDescriptor stackFrameRegister() const
Returns the stack call frame register.
virtual int iprocKey(SgAsmInstruction *) const =0
Given an instruction, return the InsnProcessor key that can be used as an index into the iproc_table.
virtual SValuePtr number_(size_t nbits, uint64_t number) const
Return a semantic value representing a number.
virtual RegisterDescriptor callReturnRegister() const
Returns the function call return address register.
virtual void incrementRegisters(SgAsmExpression *)
Increment all auto-increment registers in the expression.
virtual SValuePtr undefined_(size_t nbits) const
Return a new undefined semantic value.
virtual void decrementRegisters(SgAsmExpression *)
Decrement all auto-decrement registers in the expression.
virtual InsnProcessor * iprocGet(int key)
Obtain an iproc table entry for the specified key.
virtual RegisterDescriptor findRegister(const std::string &regname, size_t nbits=0, bool allowMissing=false) const
Lookup a register by name.
virtual SValuePtr effectiveAddress(SgAsmExpression *, size_t nbits=0)
Returns a memory address by evaluating the address expression.
bool autoResetInstructionPointer() const
Property: Reset instruction pointer register for each instruction.
Definition Dispatcher.h:241
virtual void iprocReplace(SgAsmInstruction *insn, InsnProcessor *iproc)
Replace an instruction processor with another.
virtual void processInstruction(SgAsmInstruction *insn)
Process a single instruction.
size_t addressWidth() const
Property: Width of memory addresses in bits.
virtual SgAsmInstruction * currentInstruction() const
Returns the instruction that is being processed.
virtual void write(SgAsmExpression *, const SValuePtr &value, size_t addr_nbits=0)
Writes to an L-value expression.
virtual void advanceInstructionPointer(SgAsmInstruction *)
Update the instruction pointer register.
virtual DispatcherPtr create(const RiscOperatorsPtr &ops) const =0
Virtual constructor.
void autoResetInstructionPointer(bool b)
Property: Reset instruction pointer register for each instruction.
Definition Dispatcher.h:242
virtual RegisterDescriptor segmentRegister(SgAsmMemoryReferenceExpression *)
Returns a register descriptor for the segment part of a memory reference expression.
virtual void iprocSet(int key, InsnProcessor *iproc)
Set an iproc table entry to the specified value.
RegisterDictionaryPtr registerDictionary() const
Property: Register dictionary.
virtual RegisterDescriptor instructionPointerRegister() const
Returns the instruction pointer register.
virtual RiscOperatorsPtr operators() const
Property: RISC operators.
virtual void processDelaySlot(SgAsmInstruction *delayInsn)
Process a delay slot.
Functor that knows how to dispatch a single kind of instruction.
Definition Dispatcher.h:27
Describes (part of) a physical CPU register.
Base class for expressions.
Base class for machine instructions.
Reference to memory locations.
const std::string & name(const BaseConstPtr &)
Architecture name free function.
std::shared_ptr< const Base > BaseConstPtr
Reference counted pointer for Architecture::Base.
Sawyer::Result< BasePtr, NotFound > findByName(const std::string &)
Look up a new architecture by name.
Base classes for instruction semantics.
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
boost::shared_ptr< Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
The ROSE library.