1#ifndef ROSE_BinaryAnalysis_InstructionCache_H
2#define ROSE_BinaryAnalysis_InstructionCache_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_BINARY_ANALYSIS
6#include <Rose/BinaryAnalysis/Address.h>
7#include <Rose/BinaryAnalysis/BasicTypes.h>
8#include <Rose/BinaryAnalysis/Disassembler/BasicTypes.h>
9#include <Rose/Exception.h>
12#include <unordered_map>
17namespace BinaryAnalysis {
19class InstructionCache;
21class LockedInstruction;
42 mutable SAWYER_THREAD_TRAITS::Mutex mutex_;
50 mutable size_t lastAccess;
72 : cache{cache}, state{ABSENT}, u{.va = va} {
73 ASSERT_not_null(cache);
77 : cache{cache}, state{PRESENT}, u{.ast =
nullptr} {
78 ASSERT_not_null(cache);
117 void updateTimerNS()
const;
139 mutable SAWYER_THREAD_TRAITS::Mutex mutex_;
207 explicit operator bool()
const;
274 mutable SAWYER_THREAD_TRAITS::Mutex mutex_;
275 std::shared_ptr<ManagedInstruction> mi_;
314 explicit operator bool()
const;
378 mutable SAWYER_THREAD_TRAITS::Mutex mutex_;
379 std::unordered_map<rose_addr_t, InstructionPtr> insns_;
456inline InstructionPtr&
458 SAWYER_THREAD_TRAITS::LockGuard2
lock(mutex_, other.mutex_);
465 SAWYER_THREAD_TRAITS::LockGuard
lock(mutex_);
466 ASSERT_not_null(mi_);
477ManagedInstruction::lock()
const {
478 SAWYER_THREAD_TRAITS::LockGuard lock(mutex_);
480 return makePresentNS();
484ManagedInstruction::updateTimerNS()
const {
485 static size_t nextTimer = 0;
486 lastAccess = ++nextTimer;
489inline LockedInstruction
490ManagedInstruction::makePresentNS()
const {
491 if (ABSENT == state) {
493 ASSERT_not_null(decoded);
497 return LockedInstruction{u.ast};
502 SAWYER_THREAD_TRAITS::LockGuard lock(mutex_);
503 ASSERT_not_null(insn);
508InstructionPtr::operator bool()
const {
509 SAWYER_THREAD_TRAITS::LockGuard lock(mutex_);
510 return mi_.get() ? !(*mi_).isNull() :
false;
514ManagedInstruction::isNull()
const {
515 SAWYER_THREAD_TRAITS::LockGuard lock(mutex_);
520 return u.ast ==
nullptr;
525 SAWYER_THREAD_TRAITS::LockGuard
lock(mutex_);
526 return mi_ && !(*mi_).isNull()? true :
false;
Exceptions thrown by this layer.
Cache of instruction ASTs.
InstructionPtr get(rose_addr_t va)
Obtain the instruction at a specified address.
void evict()
Garbage collection.
LockedInstruction lock(rose_addr_t va)
Shortcut to obtain a lock.
MemoryMapPtr memoryMap() const
Property: memory map providing the opcodes for the instructions.
Disassembler::BasePtr decoder() const
Property: the decoder used to construct the instruction ASTs from data in memory.
InstructionCache(const MemoryMapPtr &, const Disassembler::BasePtr &decoder)
Construct a new instruction cache.
InstructionGuard(const InstructionPtr &insn)
Constuct the object and acquire locks.
Pointer to an instruction.
bool operator==(const InstructionPtr &other) const
Comparison.
bool operator>=(const InstructionPtr &other) const
Comparison.
SgAsmInstruction * take()
Give ownership to caller.
InstructionPtr(const InstructionPtr &other)
Copy constructor.
bool operator>(const InstructionPtr &other) const
Comparison.
bool operator!=(const InstructionPtr &other) const
Comparison.
void reset()
Clear the pointer.
LockedInstruction lock() const
Return a locking pointer.
bool operator<=(const InstructionPtr &other) const
Comparison.
bool operator<(const InstructionPtr &other) const
Comparison.
bool operator==(std::nullptr_t) const
Comparison.
LockedInstruction operator->() const
Dereference.
InstructionPtr()
Construct a null pointer.
InstructionPtr & operator=(const InstructionPtr &other)
Assignment operator.
Smart pointer that locks an instruction AST.
SgAsmInstruction * get() const
Get the raw pointer.
LockedInstruction & operator=(const LockedInstruction &other)
Assignment.
SgAsmInstruction * operator->() const
Dereference.
SgAsmInstruction & operator*() const
Dereference.
LockedInstruction(SgAsmInstruction *insn)
Point to a specific instruction.
void reset()
Reset to null.
~LockedInstruction()
Destructor.
LockedInstruction(const LockedInstruction &other)
Copy constructor.
LockedInstruction(const InstructionPtr &insn)
Point to a specific instruction.
LockedInstruction()
Construct a null pointer.
Wrapper for AST that can be evicted.
LockedInstruction operator->() const
Access an instruction member.
Base class for all ROSE exceptions.
Base class for reference counted objects.
Base class for machine instructions.