ROSE  0.9.12.28
BaseSemanticsDispatcher.h
1 #ifndef ROSE_BinaryAnalysis_InstructionSemantics2_BaseSemantics_Dispatcher_H
2 #define ROSE_BinaryAnalysis_InstructionSemantics2_BaseSemantics_Dispatcher_H
3 
4 #include <BaseSemanticsTypes.h>
5 #include <Registers.h>
6 
7 #include <boost/serialization/access.hpp>
8 #include <boost/serialization/nvp.hpp>
9 #include <boost/serialization/shared_ptr.hpp>
10 
11 namespace Rose {
12 namespace BinaryAnalysis {
13 namespace InstructionSemantics2 {
14 namespace BaseSemantics {
15 
17 // Instruction Dispatcher
19 
22 public:
23  virtual ~InsnProcessor() {}
24  virtual void process(const DispatcherPtr &dispatcher, SgAsmInstruction *insn) = 0;
25 };
26 
40 class Dispatcher: public boost::enable_shared_from_this<Dispatcher> {
41 protected:
42  RiscOperatorsPtr operators;
44  size_t addrWidth_;
47  // Dispatchers keep a table of all the kinds of instructions they can handle. The lookup key is typically some sort of
48  // instruction identifier, such as from SgAsmX86Instruction::get_kind(), and comes from the iproc_key() virtual method.
49  typedef std::vector<InsnProcessor*> InsnProcessors;
50  InsnProcessors iproc_table;
51 
52 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
53 private:
54  friend class boost::serialization::access;
55 
56  template<class S>
57  void serialize(S &s, const unsigned /*version*/) {
58  s & BOOST_SERIALIZATION_NVP(operators);
59  s & BOOST_SERIALIZATION_NVP(regdict);
60  s & BOOST_SERIALIZATION_NVP(addrWidth_);
61  s & BOOST_SERIALIZATION_NVP(autoResetInstructionPointer_);
62  //s & iproc_table; -- not saved
63  }
64 #endif
65 
67  // Real constructors
68 protected:
69  // Prototypical constructor
70  Dispatcher(): regdict(NULL), addrWidth_(0), autoResetInstructionPointer_(true) {}
71 
72  // Prototypical constructor
73  Dispatcher(size_t addrWidth, const RegisterDictionary *regs)
74  : regdict(regs), addrWidth_(addrWidth), autoResetInstructionPointer_(true) {}
75 
76  Dispatcher(const RiscOperatorsPtr &ops, size_t addrWidth, const RegisterDictionary *regs)
77  : operators(ops), regdict(regs), addrWidth_(addrWidth), autoResetInstructionPointer_(true) {
78  ASSERT_not_null(operators);
79  ASSERT_not_null(regs);
80  }
81 
82 public:
84  typedef DispatcherPtr Ptr;
85 
86 public:
87  virtual ~Dispatcher() {
88  for (InsnProcessors::iterator iter=iproc_table.begin(); iter!=iproc_table.end(); ++iter)
89  delete *iter;
90  }
91 
93  // Static allocating constructors. None since this is an abstract class
94 
95 
97  // Virtual constructors
98 public:
100  virtual DispatcherPtr create(const RiscOperatorsPtr &ops, size_t addrWidth=0, const RegisterDictionary *regs=NULL) const = 0;
101 
103  // Methods to process instructions
104 public:
106  virtual void processInstruction(SgAsmInstruction *insn);
107 
109  // Instruction processor table operations
110 public:
115 
119  virtual void iproc_replace(SgAsmInstruction *insn, InsnProcessor *iproc);
120 
122  virtual int iproc_key(SgAsmInstruction*) const = 0;
123 
127  virtual void iproc_set(int key, InsnProcessor *iproc);
128 
130  virtual InsnProcessor *iproc_get(int key);
131 
133  // Convenience methods that defer the call to some member object
134 public:
136  virtual RiscOperatorsPtr get_operators() const { return operators; }
137 
140  virtual StatePtr currentState() const;
141 
143  virtual SValuePtr protoval() const;
144 
149  virtual SgAsmInstruction* currentInstruction() const;
150 
154  virtual SValuePtr undefined_(size_t nbits) const;
155  virtual SValuePtr unspecified_(size_t nbits) const;
159  virtual SValuePtr number_(size_t nbits, uint64_t number) const;
160 
162  // Methods related to registers
163 public:
176  return regdict;
177  }
178  virtual void set_register_dictionary(const RegisterDictionary *regdict) {
179  this->regdict = regdict;
180  }
187  virtual RegisterDescriptor findRegister(const std::string &regname, size_t nbits=0, bool allowMissing=false) const;
188 
195  size_t addressWidth() const { return addrWidth_; }
196  void addressWidth(size_t nbits);
201 
203  virtual RegisterDescriptor stackPointerRegister() const = 0;
204 
206  virtual RegisterDescriptor callReturnRegister() const = 0;
207 
215  void autoResetInstructionPointer(bool b) { autoResetInstructionPointer_ = b; }
219  // Miscellaneous methods that tend to be the same for most dispatchers
221 public:
222 
232 
237 
241  virtual void incrementRegisters(SgAsmExpression*);
242 
246  virtual void decrementRegisters(SgAsmExpression*);
247 
251  virtual SValuePtr effectiveAddress(SgAsmExpression*, size_t nbits=0);
252 
258  virtual SValuePtr read(SgAsmExpression*, size_t value_nbits=0, size_t addr_nbits=0);
259 
263  virtual void write(SgAsmExpression*, const SValuePtr &value, size_t addr_nbits=0);
264 };
265 
266 } // namespace
267 } // namespace
268 } // namespace
269 } // namespace
270 
271 #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:35
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.