ROSE  0.11.109.0
InstructionProvider.h
1 #ifndef ROSE_BinaryAnalysis_Partitioner2_InstructionProvider_H
2 #define ROSE_BinaryAnalysis_Partitioner2_InstructionProvider_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BinaryAnalysis/BasicTypes.h>
7 #include <Rose/BinaryAnalysis/CallingConvention.h>
8 #include <Rose/BinaryAnalysis/Disassembler/BasicTypes.h>
9 #include <Rose/BinaryAnalysis/InstructionSemantics/BaseSemantics.h>
10 #include "AstSerialization.h"
11 
12 #include <boost/serialization/access.hpp>
13 #include <Sawyer/Assert.h>
14 #include <Sawyer/HashMap.h>
15 #include <Sawyer/SharedPointer.h>
16 
17 namespace Rose {
18 namespace BinaryAnalysis {
19 
33 public:
36 
39 
40 private:
41  Disassembler::BasePtr disassembler_;
42  MemoryMap::Ptr memMap_;
43  mutable InsnMap insnMap_; // this is a cache
44  bool useDisassembler_;
45 
46 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
47 private:
48  friend class boost::serialization::access;
49 
50  template<class S>
51  void save(S &s, const unsigned /*version*/) const {
52  roseAstSerializationRegistration(s); // so we can save instructions through SgAsmInstruction base ptrs
53  bool hasDisassembler = disassembler_ != NULL;
54  s <<BOOST_SERIALIZATION_NVP(hasDisassembler);
55  s <<BOOST_SERIALIZATION_NVP(useDisassembler_);
56  s <<BOOST_SERIALIZATION_NVP(memMap_);
57  s <<BOOST_SERIALIZATION_NVP(insnMap_);
58  if (hasDisassembler) {
59  std::string disName = Disassembler::name(disassembler_);
60  s <<BOOST_SERIALIZATION_NVP(disName);
61  }
62  }
63 
64  template<class S>
65  void load(S &s, const unsigned /*version*/) {
66  roseAstSerializationRegistration(s);
67  bool hasDisassembler = false;
68  s >>BOOST_SERIALIZATION_NVP(hasDisassembler);
69  s >>BOOST_SERIALIZATION_NVP(useDisassembler_);
70  s >>BOOST_SERIALIZATION_NVP(memMap_);
71  s >>BOOST_SERIALIZATION_NVP(insnMap_);
72  if (hasDisassembler) {
73  std::string disName;
74  s >>BOOST_SERIALIZATION_NVP(disName);
75  disassembler_ = Disassembler::lookup(disName);
76  ASSERT_not_null2(disassembler_, "disassembler name=" + disName);
77  }
78  }
79 
80  BOOST_SERIALIZATION_SPLIT_MEMBER();
81 #endif
82 
83 protected:
84  InstructionProvider();
85 
86  InstructionProvider(const Disassembler::BasePtr &disassembler, const MemoryMap::Ptr &map);
87 
88 public:
89  ~InstructionProvider();
90 
102  return Ptr(new InstructionProvider(disassembler, map));
103  }
104 
111  bool isDisassemblerEnabled() const {
112  return useDisassembler_;
113  }
114  void enableDisassembler(bool enable=true);
116  useDisassembler_ = false;
117  }
126  SgAsmInstruction* operator[](rose_addr_t va) const;
127 
132  void insert(SgAsmInstruction*);
133 
139 
146  size_t nCached() const { return insnMap_.size(); }
147 
150 
153 
156 
159 
164 
170 
175 
177  ByteOrder::Endianness defaultByteOrder() const;
178 
180  size_t wordSize() const;
181 
183  size_t instructionAlignment() const;
184 
191 
193  void showStatistics() const;
194 };
195 
196 } // namespace
197 } // namespace
198 
199 #endif
200 #endif
RegisterDescriptor instructionPointerRegister() const
Register used as the instruction pointer.
InstructionSemantics::BaseSemantics::DispatcherPtr dispatcher() const
Instruction dispatcher.
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.
RegisterDescriptor stackSegmentRegister() const
Register used as a segment to access stack memory.
size_t wordSize() const
Word size in bits.
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.
Disassembler::BasePtr disassembler() const
Returns the disassembler.
Main namespace for the ROSE library.
RegisterDescriptor stackFrameRegister() const
Register used for function call frames.
ByteOrder::Endianness defaultByteOrder() const
Default memory byte order.
boost::shared_ptr< Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
size_t size() const
Number of nodes, keys, or values in this container.
Definition: HashMap.h:316
RegisterDescriptor callReturnRegister() const
Register holding a function call's return address.
MemoryMapPtr Ptr
Reference counting pointer.
Definition: MemoryMap.h:115
std::vector< Definition::Ptr > Dictionary
A ordered collection of calling convention definitions.
Describes (part of) a physical CPU register.
static Ptr instance(const Disassembler::BasePtr &disassembler, const MemoryMap::Ptr &map)
Static allocating Constructor.
bool isDisassemblerEnabled() const
Enable or disable the disassembler.
RegisterDictionaryPtr registerDictionary() const
Returns the register dictionary.
Sawyer::SharedPointer< InstructionProvider > Ptr
Shared-ownership pointer.
void insert(SgAsmInstruction *)
Insert an instruction into the cache.
const std::string & name(const BasePtr &)
Disassembler name free function.
Base class for reference counted objects.
Definition: SharedObject.h:64
BasePtr lookup(SgAsmGenericHeader *)
Finds a suitable disassembler for a file header.
void enableDisassembler(bool enable=true)
Enable or disable the disassembler.
Sawyer::SharedPointer< Base > BasePtr
Reference counted pointer for disassemblers.
SgAsmInstruction * operator[](rose_addr_t va) const
Returns the instruction at the specified virtual address, or null.
size_t instructionAlignment() const
Alignment requirement for instructions.
size_t nCached() const
Returns number of cached starting addresses.