ROSE  0.11.21.0
BaseSemanticsDispatcher.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 <BaseSemanticsTypes.h>
7 #include <Registers.h>
8 
9 #include <boost/serialization/access.hpp>
10 #include <boost/serialization/nvp.hpp>
11 #include <boost/serialization/shared_ptr.hpp>
12 
13 namespace Rose {
14 namespace BinaryAnalysis {
15 namespace InstructionSemantics2 {
16 namespace BaseSemantics {
17 
19 // Instruction Dispatcher
21 
24 public:
25  virtual ~InsnProcessor() {}
26  virtual void process(const DispatcherPtr &dispatcher, SgAsmInstruction *insn) = 0;
27 };
28 
42 class Dispatcher: public boost::enable_shared_from_this<Dispatcher> {
43  RiscOperatorsPtr operators_;
44 
45 protected:
47  size_t addrWidth_;
50  // Dispatchers keep a table of all the kinds of instructions they can handle. The lookup key is typically some sort of
51  // instruction identifier, such as from SgAsmX86Instruction::get_kind(), and comes from the iproc_key() virtual method.
52  typedef std::vector<InsnProcessor*> InsnProcessors;
53  InsnProcessors iproc_table;
54 
55 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
56 private:
57  friend class boost::serialization::access;
58 
59  template<class S>
60  void serialize(S &s, const unsigned /*version*/) {
61  s & boost::serialization::make_nvp("operators", operators_); // for backward compatibility
62  s & BOOST_SERIALIZATION_NVP(regdict);
63  s & BOOST_SERIALIZATION_NVP(addrWidth_);
64  s & BOOST_SERIALIZATION_NVP(autoResetInstructionPointer_);
65  //s & iproc_table; -- not saved
66  }
67 #endif
68 
70  // Real constructors
71 protected:
72  // Prototypical constructor
73  Dispatcher(): regdict(NULL), addrWidth_(0), autoResetInstructionPointer_(true) {}
74 
75  // Prototypical constructor
76  Dispatcher(size_t addrWidth, const RegisterDictionary *regs)
77  : regdict(regs), addrWidth_(addrWidth), autoResetInstructionPointer_(true) {}
78 
79  Dispatcher(const RiscOperatorsPtr &ops, size_t addrWidth, const RegisterDictionary *regs)
80  : operators_(ops), regdict(regs), addrWidth_(addrWidth), autoResetInstructionPointer_(true) {
81  ASSERT_not_null(operators_);
82  ASSERT_not_null(regs);
83  }
84 
85 public:
87  typedef DispatcherPtr Ptr;
88 
89 public:
90  virtual ~Dispatcher() {
91  for (InsnProcessors::iterator iter=iproc_table.begin(); iter!=iproc_table.end(); ++iter)
92  delete *iter;
93  }
94 
96  // Static allocating constructors. None since this is an abstract class
97 
98 
100  // Virtual constructors
101 public:
103  virtual DispatcherPtr create(const RiscOperatorsPtr &ops, size_t addrWidth=0, const RegisterDictionary *regs=NULL) const = 0;
104 
106  // Methods to process instructions
107 public:
109  virtual void processInstruction(SgAsmInstruction *insn);
110 
112  // Instruction processor table operations
113 public:
118 
122  virtual void iproc_replace(SgAsmInstruction *insn, InsnProcessor *iproc);
123 
125  virtual int iproc_key(SgAsmInstruction*) const = 0;
126 
130  virtual void iproc_set(int key, InsnProcessor *iproc);
131 
133  virtual InsnProcessor *iproc_get(int key);
134 
136  // Convenience methods that defer the call to some member object
137 public:
138  // Deprecated [Robb Matzke 2020-11-19]
139  virtual RiscOperatorsPtr get_operators() const ROSE_DEPRECATED("use \"operators\" instead") { return operators_; }
140 
146  virtual RiscOperatorsPtr operators() const { return operators_; }
147  virtual void operators(const RiscOperatorsPtr &ops);
152  virtual StatePtr currentState() const;
153 
155  virtual SValuePtr protoval() const;
156 
161  virtual SgAsmInstruction* currentInstruction() const;
162 
166  virtual SValuePtr undefined_(size_t nbits) const;
167  virtual SValuePtr unspecified_(size_t nbits) const;
171  virtual SValuePtr number_(size_t nbits, uint64_t number) const;
172 
174  // Methods related to registers
175 public:
188  return regdict;
189  }
190  virtual void set_register_dictionary(const RegisterDictionary *regdict) {
191  this->regdict = regdict;
192  }
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 callReturnRegister() const = 0;
219 
227  void autoResetInstructionPointer(bool b) { autoResetInstructionPointer_ = b; }
231  // Miscellaneous methods that tend to be the same for most dispatchers
233 public:
239  virtual void initializeState(const StatePtr&);
240 
250 
255 
259  virtual void incrementRegisters(SgAsmExpression*);
260 
264  virtual void decrementRegisters(SgAsmExpression*);
265 
270  virtual void preUpdate(SgAsmExpression*);
271 
276  virtual void postUpdate(SgAsmExpression*);
277 
281  virtual SValuePtr effectiveAddress(SgAsmExpression*, size_t nbits=0);
282 
288  virtual SValuePtr read(SgAsmExpression*, size_t value_nbits=0, size_t addr_nbits=0);
289 
293  virtual void write(SgAsmExpression*, const SValuePtr &value, size_t addr_nbits=0);
294 };
295 
296 } // namespace
297 } // namespace
298 } // namespace
299 } // namespace
300 
301 #endif
302 #endif
void autoResetInstructionPointer(bool b)
Property: Reset instruction pointer register for each instruction.
bool autoResetInstructionPointer() const
Property: Reset instruction pointer register for each instruction.
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 iproc_replace(SgAsmInstruction *insn, InsnProcessor *iproc)
Replace an instruction processor with another.
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 void preUpdate(SgAsmExpression *)
Update registers for pre-add expressions.
virtual const RegisterDictionary * get_register_dictionary() const
Access the register dictionary.
virtual InsnProcessor * iproc_lookup(SgAsmInstruction *insn)
Lookup the processor for an instruction.
boost::shared_ptr< Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
virtual int iproc_key(SgAsmInstruction *) const =0
Given an instruction, return the InsnProcessor key that can be used as an index into the iproc_table...
Reference to memory locations.
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.
virtual RiscOperatorsPtr operators() const
Property: RISC operators.
Functor that knows how to dispatch a single kind of instruction.
DispatcherPtr Ptr
Shared-ownership pointer for a Dispatcher object.
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 set_register_dictionary().
Base class for expressions.
virtual SValuePtr unspecified_(size_t nbits) const
Return a new undefined semantic value.
virtual void postUpdate(SgAsmExpression *)
Update registers for post-add expressions.
virtual RegisterDescriptor findRegister(const std::string &regname, size_t nbits=0, bool allowMissing=false) const
Lookup a register by name.
virtual void iproc_set(int key, InsnProcessor *iproc)
Set an iproc table entry to the specified value.
virtual InsnProcessor * iproc_get(int key)
Obtain an iproc table entry for the specified key.
bool autoResetInstructionPointer_
Reset instruction pointer register for each instruction.
Defines registers available for a particular architecture.
Definition: Registers.h:38
virtual RegisterDescriptor callReturnRegister() const =0
Returns the function call return address register.
virtual void set_register_dictionary(const RegisterDictionary *regdict)
Access the register dictionary.
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.