ROSE  0.11.51.0
Dispatcher.h
1 #ifndef ROSE_BinaryAnalysis_InstructionSemantics2_BaseSemantics_Dispatcher_H
2 #define ROSE_BinaryAnalysis_InstructionSemantics2_BaseSemantics_Dispatcher_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BinaryAnalysis/InstructionSemantics2/BaseSemantics/Types.h>
7 #include <Rose/BinaryAnalysis/Registers.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 InstructionSemantics2 {
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(): regdict(NULL), addrWidth_(0), autoResetInstructionPointer_(true) {}
81 
82  // Prototypical constructor
83  Dispatcher(size_t addrWidth, const RegisterDictionary *regs)
84  : regdict(regs), addrWidth_(addrWidth), autoResetInstructionPointer_(true) {}
85 
86  Dispatcher(const RiscOperatorsPtr &ops, size_t addrWidth, const RegisterDictionary *regs)
87  : operators_(ops), regdict(regs), addrWidth_(addrWidth), autoResetInstructionPointer_(true) {
88  ASSERT_not_null(operators_);
89  ASSERT_not_null(regs);
90  }
91 
92 public:
93  virtual ~Dispatcher() {
94  for (InsnProcessors::iterator iter=iproc_table.begin(); iter!=iproc_table.end(); ++iter)
95  delete *iter;
96  }
97 
99  // Static allocating constructors. None since this is an abstract class
100 
101 
103  // Virtual constructors
104 public:
106  virtual DispatcherPtr create(const RiscOperatorsPtr &ops, size_t addrWidth=0, const RegisterDictionary *regs=NULL) const = 0;
107 
109  // Methods to process instructions
110 public:
112  virtual void processInstruction(SgAsmInstruction *insn);
113 
115  // Instruction processor table operations
116 public:
120  virtual InsnProcessor *iprocLookup(SgAsmInstruction *insn);
121 
125  virtual void iprocReplace(SgAsmInstruction *insn, InsnProcessor *iproc);
126 
128  virtual int iprocKey(SgAsmInstruction*) const = 0;
129 
133  virtual void iprocSet(int key, InsnProcessor *iproc);
134 
136  virtual InsnProcessor *iprocGet(int key);
137 
139  // Convenience methods that defer the call to some member object
140 public:
141  // Deprecated [Robb Matzke 2020-11-19]
142  virtual RiscOperatorsPtr get_operators() const ROSE_DEPRECATED("use \"operators\" instead") { return operators_; }
143 
149  virtual RiscOperatorsPtr operators() const { return operators_; }
150  virtual void operators(const RiscOperatorsPtr &ops);
155  virtual StatePtr currentState() const;
156 
158  virtual SValuePtr protoval() const;
159 
164  virtual SgAsmInstruction* currentInstruction() const;
165 
169  virtual SValuePtr undefined_(size_t nbits) const;
170  virtual SValuePtr unspecified_(size_t nbits) const;
174  virtual SValuePtr number_(size_t nbits, uint64_t number) const;
175 
177  // Methods related to registers
178 public:
195  const RegisterDictionary* registerDictionary() const /*final*/ {
196  return get_register_dictionary();
197  }
198  void registerDictionary(const RegisterDictionary *rd) /*final*/ {
199  set_register_dictionary(rd);
200  }
204  // Old names, the ones which are overridden in subclasses if necessary. These are deprecated and might go away someday, so
205  // it's in your best interrest to use C++11 "override" in your declarations.
206  virtual const RegisterDictionary *get_register_dictionary() const {
207  return regdict;
208  }
209  virtual void set_register_dictionary(const RegisterDictionary *regdict) {
210  this->regdict = regdict;
211  }
212 
217  virtual RegisterDescriptor findRegister(const std::string &regname, size_t nbits=0, bool allowMissing=false) const;
218 
225  size_t addressWidth() const { return addrWidth_; }
226  void addressWidth(size_t nbits);
231 
233  virtual RegisterDescriptor stackPointerRegister() const = 0;
234 
236  virtual RegisterDescriptor stackFrameRegister() const = 0;
237 
239  virtual RegisterDescriptor callReturnRegister() const = 0;
240 
248  void autoResetInstructionPointer(bool b) { autoResetInstructionPointer_ = b; }
252  // Miscellaneous methods that tend to be the same for most dispatchers
254 public:
260  virtual void initializeState(const StatePtr&);
261 
271 
276 
280  virtual void incrementRegisters(SgAsmExpression*);
281 
285  virtual void decrementRegisters(SgAsmExpression*);
286 
291  virtual void preUpdate(SgAsmExpression*, const BaseSemantics::SValuePtr &enabled);
292 
297  virtual void postUpdate(SgAsmExpression*, const BaseSemantics::SValuePtr &enabled);
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 
323 
324 #endif
325 #endif
void autoResetInstructionPointer(bool b)
Property: Reset instruction pointer register for each instruction.
Definition: Dispatcher.h:248
bool autoResetInstructionPointer() const
Property: Reset instruction pointer register for each instruction.
Definition: Dispatcher.h:247
virtual void iprocSet(int key, InsnProcessor *iproc)
Set an iproc table entry to the specified value.
const RegisterDictionary * registerDictionary() const
Property: Register dictionary.
Definition: Dispatcher.h:195
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
virtual void initializeState(const StatePtr &)
Initialize the state.
virtual RegisterDescriptor segmentRegister(SgAsmMemoryReferenceExpression *)
Returns a register descriptor for the segment part of a memory reference expression.
virtual void preUpdate(SgAsmExpression *, const BaseSemantics::SValuePtr &enabled)
Update registers for pre-add expressions.
Base class for machine instructions.
virtual SValuePtr undefined_(size_t nbits) const
Return a new undefined semantic value.
virtual void incrementRegisters(SgAsmExpression *)
Increment all auto-increment registers in the expression.
virtual RegisterDescriptor instructionPointerRegister() const =0
Returns the instruction pointer register.
virtual StatePtr currentState() const
Get a pointer to the state object.
boost::shared_ptr< State > StatePtr
Shared-ownership pointer to a semantic state.
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 SValuePtr protoval() const
Return the prototypical value.
Main namespace for the ROSE library.
virtual InsnProcessor * iprocGet(int key)
Obtain an iproc table entry for the specified key.
virtual void iprocReplace(SgAsmInstruction *insn, InsnProcessor *iproc)
Replace an instruction processor with another.
boost::shared_ptr< Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
Reference to memory locations.
virtual InsnProcessor * iprocLookup(SgAsmInstruction *insn)
Lookup the processor for an instruction.
Base classes for instruction semantics.
Definition: Dispatcher.h:18
virtual SgAsmInstruction * currentInstruction() const
Returns the instruction that is being processed.
virtual DispatcherPtr create(const RiscOperatorsPtr &ops, size_t addrWidth=0, const RegisterDictionary *regs=NULL) const =0
Virtual constructor.
virtual void processInstruction(SgAsmInstruction *insn)
Process a single instruction.
Describes (part of) a physical CPU register.
size_t addressWidth() const
Property: Width of memory addresses.
Definition: Dispatcher.h:225
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 RiscOperatorsPtr operators() const
Property: RISC operators.
Definition: Dispatcher.h:149
Functor that knows how to dispatch a single kind of instruction.
Definition: Dispatcher.h:25
virtual void decrementRegisters(SgAsmExpression *)
Decrement all auto-decrement registers in the expression.
virtual SValuePtr effectiveAddress(SgAsmExpression *, size_t nbits=0)
Returns a memory address by evaluating the address expression.
const RegisterDictionary * regdict
See registerDictionary property.
Definition: Dispatcher.h:53
Base class for expressions.
Dispatches instructions through the RISC layer.
Definition: Dispatcher.h:44
virtual SValuePtr unspecified_(size_t nbits) const
Return a new undefined semantic value.
virtual RegisterDescriptor stackFrameRegister() const =0
Returns the stack call frame register.
virtual RegisterDescriptor findRegister(const std::string &regname, size_t nbits=0, bool allowMissing=false) const
Lookup a register by name.
void registerDictionary(const RegisterDictionary *rd)
Property: Register dictionary.
Definition: Dispatcher.h:198
DispatcherPtr Ptr
Shared-ownership pointer for a Dispatcher object.
Definition: Dispatcher.h:47
virtual void postUpdate(SgAsmExpression *, const BaseSemantics::SValuePtr &enabled)
Update registers for post-add expressions.
bool autoResetInstructionPointer_
Reset instruction pointer register for each instruction.
Definition: Dispatcher.h:55
Defines registers available for a particular architecture.
Definition: Registers.h:37
virtual RegisterDescriptor callReturnRegister() const =0
Returns the function call return address register.
virtual RegisterDescriptor stackPointerRegister() const =0
Returns the stack pointer register.
virtual SValuePtr number_(size_t nbits, uint64_t number) const
Return a semantic value representing a number.
virtual SValuePtr read(SgAsmExpression *, size_t value_nbits=0, size_t addr_nbits=0)
Reads an R-value expression.