ROSE  0.11.109.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 #include <Rose/BinaryAnalysis/InstructionSemantics/BaseSemantics/Types.h>
8 
9 #include <boost/enable_shared_from_this.hpp>
10 #include <boost/serialization/access.hpp>
11 #include <boost/serialization/export.hpp>
12 #include <boost/serialization/nvp.hpp>
13 #include <boost/serialization/shared_ptr.hpp>
14 
15 namespace Rose {
16 namespace BinaryAnalysis {
17 namespace InstructionSemantics {
18 namespace BaseSemantics {
19 
21 // Instruction Dispatcher
23 
26 public:
27  virtual ~InsnProcessor() {}
28  virtual void process(const DispatcherPtr &dispatcher, SgAsmInstruction *insn) = 0;
29 };
30 
44 class Dispatcher: public boost::enable_shared_from_this<Dispatcher> {
45 public:
47  using Ptr = DispatcherPtr;
48 
49 private:
50  RiscOperatorsPtr operators_;
51 
52 protected:
54  size_t addrWidth_;
57  // Dispatchers keep a table of all the kinds of instructions they can handle. The lookup key is typically some sort of
58  // instruction identifier, such as from SgAsmX86Instruction::get_kind(), and comes from the iprocKey() virtual method.
59  typedef std::vector<InsnProcessor*> InsnProcessors;
60  InsnProcessors iproc_table;
61 
62 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
63 private:
64  friend class boost::serialization::access;
65 
66  template<class S>
67  void serialize(S &s, const unsigned /*version*/) {
68  s & boost::serialization::make_nvp("operators", operators_); // for backward compatibility
69  s & BOOST_SERIALIZATION_NVP(regdict);
70  s & BOOST_SERIALIZATION_NVP(addrWidth_);
71  s & BOOST_SERIALIZATION_NVP(autoResetInstructionPointer_);
72  //s & iproc_table; -- not saved
73  }
74 #endif
75 
77  // Real constructors
78 protected:
79  // Prototypical constructor
80  Dispatcher();
81 
82  // Prototypical constructor
83  Dispatcher(size_t addrWidth, const RegisterDictionaryPtr&);
84 
85  Dispatcher(const RiscOperatorsPtr&, size_t addrWidth, const RegisterDictionaryPtr&);
86 
87 public:
88  virtual ~Dispatcher();
89 
91  // Static allocating constructors. None since this is an abstract class
92 
93 
95  // Virtual constructors
96 public:
98  virtual DispatcherPtr create(const RiscOperatorsPtr &ops, size_t addrWidth, const RegisterDictionaryPtr&) const = 0;
99 
101  // Methods to process instructions
102 public:
104  virtual void processInstruction(SgAsmInstruction *insn);
105 
107  // Instruction processor table operations
108 public:
113 
117  virtual void iprocReplace(SgAsmInstruction *insn, InsnProcessor *iproc);
118 
120  virtual int iprocKey(SgAsmInstruction*) const = 0;
121 
125  virtual void iprocSet(int key, InsnProcessor *iproc);
126 
128  virtual InsnProcessor *iprocGet(int key);
129 
131  // Convenience methods that defer the call to some member object
132 public:
133  // Deprecated [Robb Matzke 2020-11-19]
134  virtual RiscOperatorsPtr get_operators() const ROSE_DEPRECATED("use \"operators\" instead") { return operators_; }
135 
141  virtual RiscOperatorsPtr operators() const { return operators_; }
142  virtual void operators(const RiscOperatorsPtr &ops);
147  virtual StatePtr currentState() const;
148 
150  virtual SValuePtr protoval() const;
151 
156  virtual SgAsmInstruction* currentInstruction() const;
157 
161  virtual SValuePtr undefined_(size_t nbits) const;
162  virtual SValuePtr unspecified_(size_t nbits) const;
166  virtual SValuePtr number_(size_t nbits, uint64_t number) const;
167 
169  // Methods related to registers
170 public:
187  RegisterDictionaryPtr registerDictionary() const /*final*/;
188  void registerDictionary(const RegisterDictionaryPtr &rd) /*final*/;
191  // Old names, the ones which are overridden in subclasses if necessary. These are deprecated and might go away someday, so
192  // it's in your best interrest to use C++11 "override" in your declarations.
193  virtual RegisterDictionaryPtr get_register_dictionary() const;
194  virtual void set_register_dictionary(const RegisterDictionaryPtr &regdict);
195 
200  virtual RegisterDescriptor findRegister(const std::string &regname, size_t nbits=0, bool allowMissing=false) const;
201 
208  size_t addressWidth() const { return addrWidth_; }
209  void addressWidth(size_t nbits);
214 
216  virtual RegisterDescriptor stackPointerRegister() const = 0;
217 
219  virtual RegisterDescriptor stackFrameRegister() const = 0;
220 
222  virtual RegisterDescriptor callReturnRegister() const = 0;
223 
231  void autoResetInstructionPointer(bool b) { autoResetInstructionPointer_ = b; }
235  // Miscellaneous methods that tend to be the same for most dispatchers
237 public:
243  virtual void initializeState(const StatePtr&);
244 
254 
259 
263  virtual void incrementRegisters(SgAsmExpression*);
264 
268  virtual void decrementRegisters(SgAsmExpression*);
269 
274  virtual void preUpdate(SgAsmExpression*, const BaseSemantics::SValuePtr &enabled);
275 
280  virtual void postUpdate(SgAsmExpression*, const BaseSemantics::SValuePtr &enabled);
281 
285  virtual SValuePtr effectiveAddress(SgAsmExpression*, size_t nbits=0);
286 
292  virtual SValuePtr read(SgAsmExpression*, size_t value_nbits=0, size_t addr_nbits=0);
293 
297  virtual void write(SgAsmExpression*, const SValuePtr &value, size_t addr_nbits=0);
298 };
299 
300 } // namespace
301 } // namespace
302 } // namespace
303 } // namespace
304 
306 
307 #endif
308 #endif
virtual void incrementRegisters(SgAsmExpression *)
Increment all auto-increment registers in the expression.
Base classes for instruction semantics.
Definition: Dispatcher.h:18
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:55
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:230
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:44
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:25
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:208
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:54
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:231
RegisterDictionaryPtr regdict
See registerDictionary property.
Definition: Dispatcher.h:53
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:141