ROSE 2.9.0
Loading...
Searching...
No Matches
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
63public:
64 // Make a constant number_
65 virtual SValuePtr makeConstant(const std::string &kind, int64_t value, size_t nBits);
66 virtual int8_t asS1(const SgAsmExpression* expr);
67 virtual uint8_t asU1(const SgAsmExpression* expr);
68
69#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
70private:
71 friend class boost::serialization::access;
72
73 template<class S>
74 void serializeCommon(S &s, const unsigned version) {
75 ASSERT_always_require(version >= 2);
76 s & boost::serialization::make_nvp("operators", operators_); // for backward compatibility
77 s & BOOST_SERIALIZATION_NVP(autoResetInstructionPointer_);
78 //s & iproc_table; -- not saved
79 }
80
81 template<class S>
82 void save(S &s, const unsigned version) const {
83 const_cast<Dispatcher*>(this)->serializeCommon(s, version);
84 ASSERT_not_null(architecture_);
85 const std::string architecture = Architecture::name(architecture_);
86 s & BOOST_SERIALIZATION_NVP(architecture);
87 }
88
89 template<class S>
90 void load(S &s, const unsigned version) {
91 serializeCommon(s, version);
92 std::string architecture;
93 s & BOOST_SERIALIZATION_NVP(architecture);
94 architecture_ = Architecture::findByName(architecture).orThrow();
95 }
96
97 BOOST_SERIALIZATION_SPLIT_MEMBER();
98#endif
99
101 // Real constructors
102protected:
103 Dispatcher(); // used only by boost::serialization
104
105 // Prototypical constructor
106 explicit Dispatcher(const Architecture::BaseConstPtr&);
107
108 Dispatcher(const Architecture::BaseConstPtr&, const RiscOperatorsPtr&);
109
110public:
111 virtual ~Dispatcher();
112
114 // Static allocating constructors. None since this is an abstract class
115
116
118 // Virtual constructors
119public:
121 virtual DispatcherPtr create(const RiscOperatorsPtr &ops) const = 0;
122
124 // Methods to process instructions
125public:
130
136 virtual void processDelaySlot(SgAsmInstruction *delayInsn);
137
138protected:
139 // The part of processing instructions that's common to both `processInstruction` and `processDelaySlot`.
140 virtual void processCommon();
141
143 // Instruction processor table operations
144public:
149
153 virtual void iprocReplace(SgAsmInstruction *insn, InsnProcessor *iproc);
154
156 virtual int iprocKey(SgAsmInstruction*) const = 0;
157
161 virtual void iprocSet(int key, InsnProcessor *iproc);
162
164 virtual InsnProcessor *iprocGet(int key);
165
167 // Convenience methods that defer the call to some member object
168public:
173
180 virtual void operators(const RiscOperatorsPtr&);
185 virtual StatePtr currentState() const;
186
188 virtual SValuePtr protoval() const;
189
195
199 virtual SValuePtr undefined_(size_t nbits) const;
200 virtual SValuePtr unspecified_(size_t nbits) const;
204 virtual SValuePtr number_(size_t nbits, uint64_t number) const;
205
207 // Methods related to registers
208public:
216
221 virtual RegisterDescriptor findRegister(const std::string &regname, size_t nbits=0, bool allowMissing=false) const;
222
227 size_t addressWidth() const;
228
231
234
237
240
253 // Miscellaneous methods that tend to be the same for most dispatchers
254public:
260 virtual void initializeState(const StatePtr&);
261
271
276
281
286
291 virtual void preUpdate(SgAsmExpression*, const BaseSemantics::SValuePtr &enabled);
292
298
302 virtual SValuePtr effectiveAddress(SgAsmExpression*, size_t nbits=0);
303
309 virtual SValuePtr read(SgAsmExpression*, size_t value_nbits=0, size_t addr_nbits=0);
310
314 virtual void write(SgAsmExpression*, const SValuePtr &value, size_t addr_nbits=0);
315};
316
317} // namespace
318} // namespace
319} // namespace
320} // namespace
321
322#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
325#endif
326
327#endif
328#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:247
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:248
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.