ROSE  0.9.9.139
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/Map.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 
83  InstructionProvider(Disassembler *disassembler, const MemoryMap::Ptr &map)
84  : disassembler_(disassembler), memMap_(map), useDisassembler_(true) {
85  ASSERT_not_null(disassembler);
86  }
87 
88 public:
99  static Ptr instance(Disassembler *disassembler, const MemoryMap::Ptr &map) {
100  return Ptr(new InstructionProvider(disassembler, map));
101  }
102 
109  bool isDisassemblerEnabled() const {
110  return useDisassembler_;
111  }
112  void enableDisassembler(bool enable=true) {
113  ASSERT_require(!enable || disassembler_);
114  useDisassembler_ = enable;
115  }
117  useDisassembler_ = false;
118  }
127  SgAsmInstruction* operator[](rose_addr_t va) const;
128 
133  void insert(SgAsmInstruction*);
134 
139  Disassembler* disassembler() const { return disassembler_; }
140 
147  size_t nCached() const { return insnMap_.size(); }
148 
150  const RegisterDictionary* registerDictionary() const { return disassembler_->registerDictionary(); }
151 
153  const CallingConvention::Dictionary& callingConventions() const { return disassembler_->callingConventions(); }
154 
157 
159  RegisterDescriptor stackPointerRegister() const { return disassembler_->stackPointerRegister(); }
160 
164  RegisterDescriptor stackSegmentRegister() const { return disassembler_->stackSegmentRegister(); }
165 
167  ByteOrder::Endianness defaultByteOrder() const { return disassembler_->byteOrder(); }
168 
175 };
176 
177 } // namespace
178 } // namespace
179 
180 #endif
void registerDictionary(const RegisterDictionary *rdict)
Properties: Register dictionary.
Definition: Disassembler.h:234
RegisterDescriptor instructionPointerRegister() const
Register used as the instruction pointer.
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.
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:166
InstructionSemantics2::BaseSemantics::DispatcherPtr dispatcher() const
Instruction dispatcher.
Main namespace for the ROSE library.
Describes (part of) a physical CPU register.
virtual RegisterDescriptor stackSegmentRegister() const
Returns the segment register for accessing the stack.
Definition: Disassembler.h:265
ByteOrder::Endianness defaultByteOrder() const
Default memory byte order.
Sawyer::Container::Map< rose_addr_t, SgAsmInstruction * > InsnMap
Mapping from address to instruction.
ByteOrder::Endianness byteOrder() const
Property: Byte order of instructions in memory.
Definition: Disassembler.h:212
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:252
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
const CallingConvention::Dictionary & callingConventions() const
Property: Calling convention dictionary.
Definition: Disassembler.h:246
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:274
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:258
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:41
size_t size() const
Number of nodes, keys, or values in this container.
Definition: Sawyer/Map.h:322
size_t nCached() const
Returns number of cached starting addresses.