ROSE  0.10.5.0
BaseSemanticsDispatcher.h
1 #ifndef ROSE_BinaryAnalysis_InstructionSemantics2_BaseSemantics_Dispatcher_H
2 #define ROSE_BinaryAnalysis_InstructionSemantics2_BaseSemantics_Dispatcher_H
3 #include <rosePublicConfig.h>
4 #ifdef ROSE_BUILD_BINARY_ANALYSIS_SUPPORT
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 protected:
44  RiscOperatorsPtr operators;
46  size_t addrWidth_;
49  // Dispatchers keep a table of all the kinds of instructions they can handle. The lookup key is typically some sort of
50  // instruction identifier, such as from SgAsmX86Instruction::get_kind(), and comes from the iproc_key() virtual method.
51  typedef std::vector<InsnProcessor*> InsnProcessors;
52  InsnProcessors iproc_table;
53 
54 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
55 private:
56  friend class boost::serialization::access;
57 
58  template<class S>
59  void serialize(S &s, const unsigned /*version*/) {
60  s & BOOST_SERIALIZATION_NVP(operators);
61  s & BOOST_SERIALIZATION_NVP(regdict);
62  s & BOOST_SERIALIZATION_NVP(addrWidth_);
63  s & BOOST_SERIALIZATION_NVP(autoResetInstructionPointer_);
64  //s & iproc_table; -- not saved
65  }
66 #endif
67 
69  // Real constructors
70 protected:
71  // Prototypical constructor
72  Dispatcher(): regdict(NULL), addrWidth_(0), autoResetInstructionPointer_(true) {}
73 
74  // Prototypical constructor
75  Dispatcher(size_t addrWidth, const RegisterDictionary *regs)
76  : regdict(regs), addrWidth_(addrWidth), autoResetInstructionPointer_(true) {}
77 
78  Dispatcher(const RiscOperatorsPtr &ops, size_t addrWidth, const RegisterDictionary *regs)
79  : operators(ops), regdict(regs), addrWidth_(addrWidth), autoResetInstructionPointer_(true) {
80  ASSERT_not_null(operators);
81  ASSERT_not_null(regs);
82  }
83 
84 public:
86  typedef DispatcherPtr Ptr;
87 
88 public:
89  virtual ~Dispatcher() {
90  for (InsnProcessors::iterator iter=iproc_table.begin(); iter!=iproc_table.end(); ++iter)
91  delete *iter;
92  }
93 
95  // Static allocating constructors. None since this is an abstract class
96 
97 
99  // Virtual constructors
100 public:
102  virtual DispatcherPtr create(const RiscOperatorsPtr &ops, size_t addrWidth=0, const RegisterDictionary *regs=NULL) const = 0;
103 
105  // Methods to process instructions
106 public:
108  virtual void processInstruction(SgAsmInstruction *insn);
109 
111  // Instruction processor table operations
112 public:
117 
121  virtual void iproc_replace(SgAsmInstruction *insn, InsnProcessor *iproc);
122 
124  virtual int iproc_key(SgAsmInstruction*) const = 0;
125 
129  virtual void iproc_set(int key, InsnProcessor *iproc);
130 
132  virtual InsnProcessor *iproc_get(int key);
133 
135  // Convenience methods that defer the call to some member object
136 public:
138  virtual RiscOperatorsPtr get_operators() const { return operators; }
139 
142  virtual StatePtr currentState() const;
143 
145  virtual SValuePtr protoval() const;
146 
151  virtual SgAsmInstruction* currentInstruction() const;
152 
156  virtual SValuePtr undefined_(size_t nbits) const;
157  virtual SValuePtr unspecified_(size_t nbits) const;
161  virtual SValuePtr number_(size_t nbits, uint64_t number) const;
162 
164  // Methods related to registers
165 public:
178  return regdict;
179  }
180  virtual void set_register_dictionary(const RegisterDictionary *regdict) {
181  this->regdict = regdict;
182  }
189  virtual RegisterDescriptor findRegister(const std::string &regname, size_t nbits=0, bool allowMissing=false) const;
190 
197  size_t addressWidth() const { return addrWidth_; }
198  void addressWidth(size_t nbits);
203 
205  virtual RegisterDescriptor stackPointerRegister() const = 0;
206 
208  virtual RegisterDescriptor callReturnRegister() const = 0;
209 
217  void autoResetInstructionPointer(bool b) { autoResetInstructionPointer_ = b; }
221  // Miscellaneous methods that tend to be the same for most dispatchers
223 public:
224 
234 
239 
243  virtual void incrementRegisters(SgAsmExpression*);
244 
248  virtual void decrementRegisters(SgAsmExpression*);
249 
253  virtual SValuePtr effectiveAddress(SgAsmExpression*, size_t nbits=0);
254 
260  virtual SValuePtr read(SgAsmExpression*, size_t value_nbits=0, size_t addr_nbits=0);
261 
265  virtual void write(SgAsmExpression*, const SValuePtr &value, size_t addr_nbits=0);
266 };
267 
268 } // namespace
269 } // namespace
270 } // namespace
271 } // namespace
272 
273 #endif
274 #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 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 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 get_operators() const
Get a pointer to the RISC operators object.
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 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.