ROSE  0.9.10.91
InstructionProvider.h
1 #ifndef ROSE_BinaryAnalysis_Partitioner2_InstructionProvider_H
2 #define ROSE_BinaryAnalysis_Partitioner2_InstructionProvider_H
3 
4 #include "Disassembler.h"
5 #include "BaseSemantics2.h"
6 #include "AstSerialization.h"
7 
8 #include <boost/serialization/access.hpp>
9 #include <Sawyer/Assert.h>
10 #include <Sawyer/HashMap.h>
11 #include <Sawyer/SharedPointer.h>
12 
13 namespace Rose {
14 namespace BinaryAnalysis {
15 
29 public:
32 
35 
36 private:
37  Disassembler *disassembler_;
38  MemoryMap::Ptr memMap_;
39  mutable InsnMap insnMap_; // this is a cache
40  bool useDisassembler_;
41 
42 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
43 private:
44  friend class boost::serialization::access;
45 
46  template<class S>
47  void save(S &s, const unsigned /*version*/) const {
48  roseAstSerializationRegistration(s); // so we can save instructions through SgAsmInstruction base ptrs
49  bool hasDisassembler = disassembler_ != NULL;
50  s <<BOOST_SERIALIZATION_NVP(hasDisassembler);
51  s <<BOOST_SERIALIZATION_NVP(useDisassembler_);
52  s <<BOOST_SERIALIZATION_NVP(memMap_);
53  s <<BOOST_SERIALIZATION_NVP(insnMap_);
54  if (hasDisassembler) {
55  std::string disName = disassembler_->name();
56  s <<BOOST_SERIALIZATION_NVP(disName);
57  }
58  }
59 
60  template<class S>
61  void load(S &s, const unsigned /*version*/) {
62  roseAstSerializationRegistration(s);
63  bool hasDisassembler = false;
64  s >>BOOST_SERIALIZATION_NVP(hasDisassembler);
65  s >>BOOST_SERIALIZATION_NVP(useDisassembler_);
66  s >>BOOST_SERIALIZATION_NVP(memMap_);
67  s >>BOOST_SERIALIZATION_NVP(insnMap_);
68  if (hasDisassembler) {
69  std::string disName;
70  s >>BOOST_SERIALIZATION_NVP(disName);
71  disassembler_ = Disassembler::lookup(disName);
72  ASSERT_not_null2(disassembler_, "disassembler name=" + disName);
73  }
74  }
75 
76  BOOST_SERIALIZATION_SPLIT_MEMBER();
77 #endif
78 
79 protected:
80  InstructionProvider()
81  : disassembler_(NULL), useDisassembler_(false) {
82  // Start off with a large map to reduce early rehashing. There will probably be a lot of instructions.
83  insnMap_.rehash(1000000);
84  }
85 
86  InstructionProvider(Disassembler *disassembler, const MemoryMap::Ptr &map)
87  : disassembler_(disassembler), memMap_(map), useDisassembler_(true) {
88  ASSERT_not_null(disassembler);
89  // Start off with a large map to reduce early rehashing. There will probably be a lot of instructions.
90  insnMap_.rehash(1000000);
91  }
92 
93 public:
104  static Ptr instance(Disassembler *disassembler, const MemoryMap::Ptr &map) {
105  return Ptr(new InstructionProvider(disassembler, map));
106  }
107 
114  bool isDisassemblerEnabled() const {
115  return useDisassembler_;
116  }
117  void enableDisassembler(bool enable=true) {
118  ASSERT_require(!enable || disassembler_);
119  useDisassembler_ = enable;
120  }
122  useDisassembler_ = false;
123  }
132  SgAsmInstruction* operator[](rose_addr_t va) const;
133 
138  void insert(SgAsmInstruction*);
139 
144  Disassembler* disassembler() const { return disassembler_; }
145 
152  size_t nCached() const { return insnMap_.size(); }
153 
155  const RegisterDictionary* registerDictionary() const { return disassembler_->registerDictionary(); }
156 
158  const CallingConvention::Dictionary& callingConventions() const { return disassembler_->callingConventions(); }
159 
162 
164  RegisterDescriptor stackPointerRegister() const { return disassembler_->stackPointerRegister(); }
165 
169  RegisterDescriptor stackFrameRegister() const { return disassembler_->stackFrameRegister(); }
170 
174  RegisterDescriptor stackSegmentRegister() const { return disassembler_->stackSegmentRegister(); }
175 
177  ByteOrder::Endianness defaultByteOrder() const { return disassembler_->byteOrder(); }
178 
185 
187  void showStatistics() const;
188 };
189 
190 } // namespace
191 } // namespace
192 
193 #endif
void registerDictionary(const RegisterDictionary *rdict)
Properties: Register dictionary.
Definition: Disassembler.h:237
RegisterDescriptor instructionPointerRegister() const
Register used as the instruction pointer.
Sawyer::Container::HashMap< rose_addr_t, SgAsmInstruction * > InsnMap
Mapping from address to instruction.
const CallingConvention::Dictionary & callingConventions() const
Returns the calling convention dictionary.
void disableDisassembler()
Enable or disable the disassembler.
Disassembler * disassembler() const
Returns the disassembler.
RegisterDescriptor stackSegmentRegister() const
Register used as a segment to access stack memory.
void showStatistics() const
Print some partitioner performance statistics.
Base class for machine instructions.
Provides and caches instructions.
RegisterDescriptor stackPointerRegister() const
Register used as a user-mode stack pointer.
const std::string & name() const
Property: Name by which disassembler is registered.
Definition: Disassembler.h:169
InstructionSemantics2::BaseSemantics::DispatcherPtr dispatcher() const
Instruction dispatcher.
virtual RegisterDescriptor stackFrameRegister() const
Returns the register that ponts to the stack frame.
Definition: Disassembler.h:267
Main namespace for the ROSE library.
RegisterDescriptor stackFrameRegister() const
Register used for function call frames.
Describes (part of) a physical CPU register.
virtual RegisterDescriptor stackSegmentRegister() const
Returns the segment register for accessing the stack.
Definition: Disassembler.h:273
ByteOrder::Endianness defaultByteOrder() const
Default memory byte order.
ByteOrder::Endianness byteOrder() const
Property: Byte order of instructions in memory.
Definition: Disassembler.h:215
size_t size() const
Number of nodes, keys, or values in this container.
Definition: HashMap.h:308
boost::shared_ptr< class Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
virtual RegisterDescriptor instructionPointerRegister() const
Returns the register that points to instructions.
Definition: Disassembler.h:255
std::vector< Definition::Ptr > Dictionary
A ordered collection of calling convention definitions.
bool isDisassemblerEnabled() const
Enable or disable the disassembler.
static Disassembler * lookup(SgAsmGenericHeader *)
Finds a suitable disassembler.
Sawyer::SharedPointer< InstructionProvider > Ptr
Shared-ownership pointer to an InstructionProvider.
void insert(SgAsmInstruction *)
Insert an instruction into the cache.
const RegisterDictionary * registerDictionary() const
Returns the register dictionary.
Defines registers available for a particular architecture.
Definition: Registers.h:32
void rehash(size_t nBuckets)
Change number of buckets.
Definition: HashMap.h:334
const CallingConvention::Dictionary & callingConventions() const
Property: Calling convention dictionary.
Definition: Disassembler.h:249
Base class for reference counted objects.
Definition: SharedObject.h:22
const Rose::BinaryAnalysis::InstructionSemantics2::BaseSemantics::DispatcherPtr & dispatcher() const
Return an instruction semantics dispatcher if possible.
Definition: Disassembler.h:282
static Ptr instance(Disassembler *disassembler, const MemoryMap::Ptr &map)
Static allocating Constructor.
void enableDisassembler(bool enable=true)
Enable or disable the disassembler.
virtual RegisterDescriptor stackPointerRegister() const
Returns the register that points to the stack.
Definition: Disassembler.h:261
SgAsmInstruction * operator[](rose_addr_t va) const
Returns the instruction at the specified virtual address, or null.
Virtual base class for instruction disassemblers.
Definition: Disassembler.h:42
size_t nCached() const
Returns number of cached starting addresses.