ROSE 0.11.145.192
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#include <boost/serialization/access.hpp>
10#include <boost/serialization/export.hpp>
11#include <boost/serialization/nvp.hpp>
12#include <boost/serialization/shared_ptr.hpp>
13
14namespace Rose {
15namespace BinaryAnalysis {
16namespace InstructionSemantics {
17namespace BaseSemantics {
18
20// Instruction Dispatcher
22
25public:
26 virtual ~InsnProcessor() {}
27 virtual void process(const DispatcherPtr &dispatcher, SgAsmInstruction *insn) = 0;
28};
29
43class Dispatcher: public boost::enable_shared_from_this<Dispatcher> {
44public:
47
48private:
49 Architecture::BaseConstPtr architecture_; // Required architecture
50 RiscOperatorsPtr operators_;
51
52protected:
55 // Dispatchers keep a table of all the kinds of instructions they can handle. The lookup key is typically some sort of
56 // instruction identifier, such as from SgAsmX86Instruction::get_kind(), and comes from the iprocKey() virtual method.
57 typedef std::vector<InsnProcessor*> InsnProcessors;
58 InsnProcessors iproc_table;
59
60#ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
61private:
62 friend class boost::serialization::access;
63
64 template<class S>
65 void serializeCommon(S &s, const unsigned version) {
66 ASSERT_always_require(version >= 2);
67 s & boost::serialization::make_nvp("operators", operators_); // for backward compatibility
68 s & BOOST_SERIALIZATION_NVP(autoResetInstructionPointer_);
69 //s & iproc_table; -- not saved
70 }
71
72 template<class S>
73 void save(S &s, const unsigned version) const {
74 const_cast<Dispatcher*>(this)->serializeCommon(s, version);
75 ASSERT_not_null(architecture_);
76 const std::string architecture = Architecture::name(architecture_);
77 s & BOOST_SERIALIZATION_NVP(architecture);
78 }
79
80 template<class S>
81 void load(S &s, const unsigned version) {
82 serializeCommon(s, version);
83 std::string architecture;
84 s & BOOST_SERIALIZATION_NVP(architecture);
85 architecture_ = Architecture::findByName(architecture).orThrow();
86 }
87
88 BOOST_SERIALIZATION_SPLIT_MEMBER();
89#endif
90
92 // Real constructors
93protected:
94 Dispatcher(); // used only by boost::serialization
95
96 // Prototypical constructor
97 explicit Dispatcher(const Architecture::BaseConstPtr&);
98
99 Dispatcher(const Architecture::BaseConstPtr&, const RiscOperatorsPtr&);
100
101public:
102 virtual ~Dispatcher();
103
105 // Static allocating constructors. None since this is an abstract class
106
107
109 // Virtual constructors
110public:
112 virtual DispatcherPtr create(const RiscOperatorsPtr &ops) const = 0;
113
115 // Methods to process instructions
116public:
121
127 virtual void processDelaySlot(SgAsmInstruction *delayInsn);
128
129protected:
130 // The part of processing instructions that's common to both `processInstruction` and `processDelaySlot`.
131 virtual void processCommon();
132
134 // Instruction processor table operations
135public:
140
144 virtual void iprocReplace(SgAsmInstruction *insn, InsnProcessor *iproc);
145
147 virtual int iprocKey(SgAsmInstruction*) const = 0;
148
152 virtual void iprocSet(int key, InsnProcessor *iproc);
153
155 virtual InsnProcessor *iprocGet(int key);
156
158 // Convenience methods that defer the call to some member object
159public:
164
171 virtual void operators(const RiscOperatorsPtr&);
176 virtual StatePtr currentState() const;
177
179 virtual SValuePtr protoval() const;
180
186
190 virtual SValuePtr undefined_(size_t nbits) const;
191 virtual SValuePtr unspecified_(size_t nbits) const;
195 virtual SValuePtr number_(size_t nbits, uint64_t number) const;
196
198 // Methods related to registers
199public:
207
212 virtual RegisterDescriptor findRegister(const std::string &regname, size_t nbits=0, bool allowMissing=false) const;
213
218 size_t addressWidth() const;
219
222
225
228
231
244 // Miscellaneous methods that tend to be the same for most dispatchers
245public:
251 virtual void initializeState(const StatePtr&);
252
262
267
272
277
282 virtual void preUpdate(SgAsmExpression*, const BaseSemantics::SValuePtr &enabled);
283
289
293 virtual SValuePtr effectiveAddress(SgAsmExpression*, size_t nbits=0);
294
300 virtual SValuePtr read(SgAsmExpression*, size_t value_nbits=0, size_t addr_nbits=0);
301
305 virtual void write(SgAsmExpression*, const SValuePtr &value, size_t addr_nbits=0);
306};
307
308} // namespace
309} // namespace
310} // namespace
311} // namespace
312
315
316#endif
317#endif
Dispatches instructions through the RISC layer.
Definition Dispatcher.h:43
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:53
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:238
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:239
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:24
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.