ROSE  0.11.145.0
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 
14 namespace Rose {
15 namespace BinaryAnalysis {
16 namespace InstructionSemantics {
17 namespace BaseSemantics {
18 
20 // Instruction Dispatcher
22 
25 public:
26  virtual ~InsnProcessor() {}
27  virtual void process(const DispatcherPtr &dispatcher, SgAsmInstruction *insn) = 0;
28 };
29 
43 class Dispatcher: public boost::enable_shared_from_this<Dispatcher> {
44 public:
46  using Ptr = DispatcherPtr;
47 
48 private:
49  RiscOperatorsPtr operators_;
50 
51 protected:
53  size_t addrWidth_;
56  // Dispatchers keep a table of all the kinds of instructions they can handle. The lookup key is typically some sort of
57  // instruction identifier, such as from SgAsmX86Instruction::get_kind(), and comes from the iprocKey() virtual method.
58  typedef std::vector<InsnProcessor*> InsnProcessors;
59  InsnProcessors iproc_table;
60 
61 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
62 private:
63  friend class boost::serialization::access;
64 
65  template<class S>
66  void serialize(S &s, const unsigned /*version*/) {
67  s & boost::serialization::make_nvp("operators", operators_); // for backward compatibility
68  s & BOOST_SERIALIZATION_NVP(regdict);
69  s & BOOST_SERIALIZATION_NVP(addrWidth_);
70  s & BOOST_SERIALIZATION_NVP(autoResetInstructionPointer_);
71  //s & iproc_table; -- not saved
72  }
73 #endif
74 
76  // Real constructors
77 protected:
78  // Prototypical constructor
79  Dispatcher();
80 
81  // Prototypical constructor
82  Dispatcher(size_t addrWidth, const RegisterDictionaryPtr&);
83 
84  Dispatcher(const RiscOperatorsPtr&, size_t addrWidth, const RegisterDictionaryPtr&);
85 
86 public:
87  virtual ~Dispatcher();
88 
90  // Static allocating constructors. None since this is an abstract class
91 
92 
94  // Virtual constructors
95 public:
97  virtual DispatcherPtr create(const RiscOperatorsPtr &ops, size_t addrWidth, const RegisterDictionaryPtr&) const = 0;
98 
100  // Methods to process instructions
101 public:
103  virtual void processInstruction(SgAsmInstruction *insn);
104 
106  // Instruction processor table operations
107 public:
112 
116  virtual void iprocReplace(SgAsmInstruction *insn, InsnProcessor *iproc);
117 
119  virtual int iprocKey(SgAsmInstruction*) const = 0;
120 
124  virtual void iprocSet(int key, InsnProcessor *iproc);
125 
127  virtual InsnProcessor *iprocGet(int key);
128 
130  // Convenience methods that defer the call to some member object
131 public:
132  // Deprecated [Robb Matzke 2020-11-19]
133  virtual RiscOperatorsPtr get_operators() const ROSE_DEPRECATED("use \"operators\" instead") { return operators_; }
134 
140  virtual RiscOperatorsPtr operators() const { return operators_; }
141  virtual void operators(const RiscOperatorsPtr &ops);
146  virtual StatePtr currentState() const;
147 
149  virtual SValuePtr protoval() const;
150 
155  virtual SgAsmInstruction* currentInstruction() const;
156 
160  virtual SValuePtr undefined_(size_t nbits) const;
161  virtual SValuePtr unspecified_(size_t nbits) const;
165  virtual SValuePtr number_(size_t nbits, uint64_t number) const;
166 
168  // Methods related to registers
169 public:
186  RegisterDictionaryPtr registerDictionary() const /*final*/;
187  void registerDictionary(const RegisterDictionaryPtr &rd) /*final*/;
190  // Old names, the ones which are overridden in subclasses if necessary. These are deprecated and might go away someday, so
191  // it's in your best interrest to use C++11 "override" in your declarations.
192  virtual RegisterDictionaryPtr get_register_dictionary() const;
193  virtual void set_register_dictionary(const RegisterDictionaryPtr &regdict);
194 
199  virtual RegisterDescriptor findRegister(const std::string &regname, size_t nbits=0, bool allowMissing=false) const;
200 
207  size_t addressWidth() const { return addrWidth_; }
208  void addressWidth(size_t nbits);
213 
215  virtual RegisterDescriptor stackPointerRegister() const = 0;
216 
218  virtual RegisterDescriptor stackFrameRegister() const = 0;
219 
221  virtual RegisterDescriptor callReturnRegister() const = 0;
222 
230  void autoResetInstructionPointer(bool b) { autoResetInstructionPointer_ = b; }
234  // Miscellaneous methods that tend to be the same for most dispatchers
236 public:
242  virtual void initializeState(const StatePtr&);
243 
253 
258 
262  virtual void incrementRegisters(SgAsmExpression*);
263 
267  virtual void decrementRegisters(SgAsmExpression*);
268 
273  virtual void preUpdate(SgAsmExpression*, const BaseSemantics::SValuePtr &enabled);
274 
279  virtual void postUpdate(SgAsmExpression*, const BaseSemantics::SValuePtr &enabled);
280 
284  virtual SValuePtr effectiveAddress(SgAsmExpression*, size_t nbits=0);
285 
291  virtual SValuePtr read(SgAsmExpression*, size_t value_nbits=0, size_t addr_nbits=0);
292 
296  virtual void write(SgAsmExpression*, const SValuePtr &value, size_t addr_nbits=0);
297 };
298 
299 } // namespace
300 } // namespace
301 } // namespace
302 } // namespace
303 
305 
306 #endif
307 #endif
virtual void incrementRegisters(SgAsmExpression *)
Increment all auto-increment registers in the expression.
virtual RegisterDescriptor stackFrameRegister() const =0
Returns the stack call frame register.
virtual RegisterDescriptor callReturnRegister() const =0
Returns the function call return address register.
virtual DispatcherPtr create(const RiscOperatorsPtr &ops, size_t addrWidth, const RegisterDictionaryPtr &) const =0
Virtual constructor.
virtual SValuePtr undefined_(size_t nbits) const
Return a new undefined semantic value.
virtual InsnProcessor * iprocLookup(SgAsmInstruction *insn)
Lookup the processor for an instruction.
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
Base class for machine instructions.
bool autoResetInstructionPointer_
Reset instruction pointer register for each instruction.
Definition: Dispatcher.h:54
virtual RegisterDescriptor segmentRegister(SgAsmMemoryReferenceExpression *)
Returns a register descriptor for the segment part of a memory reference expression.
virtual void iprocReplace(SgAsmInstruction *insn, InsnProcessor *iproc)
Replace an instruction processor with another.
bool autoResetInstructionPointer() const
Property: Reset instruction pointer register for each instruction.
Definition: Dispatcher.h:229
virtual void advanceInstructionPointer(SgAsmInstruction *)
Update the instruction pointer register.
STL namespace.
virtual RegisterDescriptor findRegister(const std::string &regname, size_t nbits=0, bool allowMissing=false) const
Lookup a register by name.
virtual int iprocKey(SgAsmInstruction *) const =0
Given an instruction, return the InsnProcessor key that can be used as an index into the iproc_table...
Main namespace for the ROSE library.
virtual RegisterDescriptor instructionPointerRegister() const =0
Returns the instruction pointer register.
virtual InsnProcessor * iprocGet(int key)
Obtain an iproc table entry for the specified key.
RegisterDictionaryPtr registerDictionary() const
Property: Register dictionary.
virtual void initializeState(const StatePtr &)
Initialize the state.
boost::shared_ptr< State > StatePtr
Shared-ownership pointer to a semantic state.
boost::shared_ptr< Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
virtual void processInstruction(SgAsmInstruction *insn)
Process a single instruction.
Dispatches instructions through the RISC layer.
Definition: Dispatcher.h:43
Reference to memory locations.
virtual SValuePtr read(SgAsmExpression *, size_t value_nbits=0, size_t addr_nbits=0)
Reads an R-value expression.
Functor that knows how to dispatch a single kind of instruction.
Definition: Dispatcher.h:24
Describes (part of) a physical CPU register.
virtual SgAsmInstruction * currentInstruction() const
Returns the instruction that is being processed.
size_t addressWidth() const
Property: Width of memory addresses.
Definition: Dispatcher.h:207
virtual StatePtr currentState() const
Get a pointer to the state object.
virtual void iprocSet(int key, InsnProcessor *iproc)
Set an iproc table entry to the specified value.
virtual SValuePtr unspecified_(size_t nbits) const
Return a new undefined semantic value.
virtual RegisterDescriptor stackPointerRegister() const =0
Returns the stack pointer register.
Base class for expressions.
virtual void preUpdate(SgAsmExpression *, const BaseSemantics::SValuePtr &enabled)
Update registers for pre-add expressions.
virtual void write(SgAsmExpression *, const SValuePtr &value, size_t addr_nbits=0)
Writes to an L-value expression.
virtual SValuePtr effectiveAddress(SgAsmExpression *, size_t nbits=0)
Returns a memory address by evaluating the address expression.
virtual SValuePtr protoval() const
Return the prototypical value.
virtual void postUpdate(SgAsmExpression *, const BaseSemantics::SValuePtr &enabled)
Update registers for post-add expressions.
size_t addrWidth_
Width of memory addresses in bits.
Definition: Dispatcher.h:53
virtual void decrementRegisters(SgAsmExpression *)
Decrement all auto-decrement registers in the expression.
void autoResetInstructionPointer(bool b)
Property: Reset instruction pointer register for each instruction.
Definition: Dispatcher.h:230
RegisterDictionaryPtr regdict
See registerDictionary property.
Definition: Dispatcher.h:52
virtual SValuePtr number_(size_t nbits, uint64_t number) const
Return a semantic value representing a number.
virtual RiscOperatorsPtr operators() const
Property: RISC operators.
Definition: Dispatcher.h:140