ROSE 0.11.145.147
Disassembler/M68k.h
1/* Disassembly specific to Motorola architectures */
2#ifndef ROSE_BinaryAnalysis_Disassembler_M68k_H
3#define ROSE_BinaryAnalysis_Disassembler_M68k_H
4#include <featureTests.h>
5#ifdef ROSE_ENABLE_BINARY_ANALYSIS
6#include <Rose/BinaryAnalysis/Disassembler/Base.h>
7
8#include <Rose/BinaryAnalysis/Architecture/BasicTypes.h>
9#include <Rose/BinaryAnalysis/InstructionEnumsM68k.h>
10#include "BitPattern.h"
11
12#include <boost/serialization/access.hpp>
13#include <boost/serialization/base_object.hpp>
14#include <boost/serialization/export.hpp>
15#include <boost/serialization/split_member.hpp>
16
17namespace Rose {
18namespace BinaryAnalysis {
19namespace Disassembler {
20
22class M68k: public Base {
23public:
25 using Ptr = M68kPtr;
26
27 // State mutated during the call to disassembleOne. Used internally.
28 struct State: boost::noncopyable { // noncopyable is so we don't accidentally pass it by value
30 rose_addr_t insn_va;
32 size_t niwords;
33 size_t niwords_used;
35 State()
36 : insn_va(0), niwords(0), niwords_used(0) {}
37 };
38
39public:
48 class Decoder {
49 public:
50 Decoder(const std::string &name, unsigned family, const BitPattern<uint16_t> &pattern)
51 : name(name), family(family), pattern(pattern) {}
52 virtual ~Decoder() {}
53 std::string name; // for debugging; same as class name but without the "M68k_" prefix
54 unsigned family; // bitmask of M68kFamily bits
55 BitPattern<uint16_t> pattern; // bits that match
56 typedef M68k D;
57 virtual SgAsmM68kInstruction *operator()(State&, const D *d, unsigned w0) = 0;
58 };
59
60private:
61 M68kFamily family;
63 // The instruction disassembly table is an array indexed by the high-order nybble of the first 16-bit word of the
64 // instruction's pattern, the so-called "operator" bits. Since most instruction disassembler have invariant operator
65 // bits, we can divide the table into 16 entries for these invariant bits, and another entry (index 16) for the cases
66 // with a variable operator byte. Each of these 17 buckets is an unordered list of instruction disassemblers whose
67 // patterns we attempt to match one at a time (the insertion function checks that there are no ambiguities).
68 typedef std::list<Decoder*> IdisList;
69 typedef std::vector<IdisList> IdisTable;
70 IdisTable idis_table;
71
72#ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
73private:
74 friend class boost::serialization::access;
75
76 template<class S>
77 void serialize_common(S &s, const unsigned /*version*/) {
78 s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
79 s & BOOST_SERIALIZATION_NVP(family);
80 //s & idis_table; -- not saved
81 }
82
83 template<class S>
84 void save(S &s, const unsigned version) const {
85 serialize_common(s, version);
86 }
87
88 template<class S>
89 void load(S &s, const unsigned version) {
90 serialize_common(s, version);
91 init();
92 }
93
94 BOOST_SERIALIZATION_SPLIT_MEMBER();
95#endif
96
97protected:
98 // undocumented constructor for serialization. The init() will be called by the serialization.
99 explicit M68k(const Architecture::BaseConstPtr&);
100
101 M68k(const Architecture::BaseConstPtr&, M68kFamily family);
102
103public:
112 static Ptr instance(const Architecture::BaseConstPtr&, M68kFamily);
113
114 virtual Base::Ptr clone() const override;
115 virtual SgAsmInstruction *disassembleOne(const MemoryMap::Ptr&, rose_addr_t start_va,
116 AddressSet *successors = nullptr) override;
118
119 typedef std::pair<SgAsmExpression*, SgAsmExpression*> ExpressionPair;
120
124 Decoder* find_idis(uint16_t *insn_bytes, size_t nbytes) const;
125
129
131 void start_instruction(State &state, const MemoryMap::Ptr &map, rose_addr_t start_va) const{
132 state.map = map;
133 state.insn_va = start_va;
134 state.niwords = 0;
135 memset(state.iwords, 0, sizeof state.iwords);
136 state.niwords_used = 0;
137 }
138
140 uint16_t instructionWord(State&, size_t n) const;
141
143 size_t extensionWordsUsed(State&) const;
144
146 SgAsmType *makeType(State&, M68kDataFormat) const;
147
149 SgAsmRegisterReferenceExpression *makeDataRegister(State&, unsigned regnum, M68kDataFormat, size_t bit_offset=0) const;
150
152 SgAsmRegisterReferenceExpression *makeAddressRegister(State&, unsigned regnum, M68kDataFormat, size_t bit_offset=0) const;
153
156 SgAsmMemoryReferenceExpression *makeAddressRegisterPreDecrement(State&, unsigned regnum, M68kDataFormat fmt) const;
157
160 SgAsmMemoryReferenceExpression *makeAddressRegisterPostIncrement(State&, unsigned regnum, M68kDataFormat fmt) const;
161
164 SgAsmRegisterReferenceExpression *makeDataAddressRegister(State&, unsigned regnum, M68kDataFormat fmt,
165 size_t bit_offset=0) const;
166
172 SgAsmRegisterNames *makeRegistersFromMask(State&, unsigned mask, M68kDataFormat fmt, bool reverse=false) const;
173
179 SgAsmRegisterNames *makeFPRegistersFromMask(State&, unsigned mask, M68kDataFormat fmt, bool reverse=false) const;
180
183
186
189
192
195
198
203
206
208 SgAsmIntegerValueExpression *makeImmediateValue(State&, M68kDataFormat fmt, unsigned value) const;
209
211 SgAsmIntegerValueExpression *makeImmediateExtension(State&, M68kDataFormat fmt, size_t ext_word_idx) const;
212
219 SgAsmExpression *makeEffectiveAddress(State&, unsigned modreg, M68kDataFormat fmt, size_t ext_offset) const;
220 SgAsmExpression *makeEffectiveAddress(State&, unsigned mode, unsigned reg, M68kDataFormat fmt, size_t ext_offset) const;
227
230 ExpressionPair makeOffsetWidthPair(State&, unsigned extension_word) const;
231
233 SgAsmM68kInstruction *makeInstruction(State&, M68kInstructionKind, M68kDataFormat,
234 SgAsmExpression *arg0=NULL, SgAsmExpression *arg1=NULL, SgAsmExpression *arg2=NULL,
235 SgAsmExpression *arg3=NULL, SgAsmExpression *arg4=NULL, SgAsmExpression *arg5=NULL,
236 SgAsmExpression *arg6=NULL) const;
237
239 M68kFamily get_family() const { return family; }
240
241private:
242 void init();
243};
244
245} // namespace
246} // namespace
247} // namespace
248
249#ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
250BOOST_CLASS_EXPORT_KEY(Rose::BinaryAnalysis::Disassembler::M68k);
251#endif
252
253#endif
254#endif
Virtual base class for instruction disassemblers.
Interface for disassembling a single instruction.
Disassembler for Motorola M68k-based instruction set architectures.
SgAsmRegisterReferenceExpression * makeProgramCounter(State &) const
Create a reference to the program counter register.
SgAsmExpression * makeAddress(State &, SgAsmExpression *expr) const
Converts a memory-reference expression to an address.
ExpressionPair makeOffsetWidthPair(State &, unsigned extension_word) const
Create an offset width pair from an extension word.
SgAsmRegisterNames * makeRegistersFromMask(State &, unsigned mask, M68kDataFormat fmt, bool reverse=false) const
Create a list of data and/or address registers.
virtual Base::Ptr clone() const override
Creates a new copy of a disassembler.
SgAsmRegisterReferenceExpression * makeMacRegister(State &, M68kMacRegister) const
Create a MAC register reference expression.
virtual SgAsmInstruction * makeUnknownInstruction(const Exception &) override
Makes an unknown instruction from an exception.
SgAsmIntegerValueExpression * makeImmediateValue(State &, M68kDataFormat fmt, unsigned value) const
Create an integer expression from a specified value.
SgAsmM68kInstruction * makeInstruction(State &, M68kInstructionKind, M68kDataFormat, SgAsmExpression *arg0=NULL, SgAsmExpression *arg1=NULL, SgAsmExpression *arg2=NULL, SgAsmExpression *arg3=NULL, SgAsmExpression *arg4=NULL, SgAsmExpression *arg5=NULL, SgAsmExpression *arg6=NULL) const
Build an instruction.
SgAsmMemoryReferenceExpression * makeAddressRegisterPostIncrement(State &, unsigned regnum, M68kDataFormat fmt) const
Make a memory reference expression using an address register in post-increment mode.
SgAsmRegisterNames * makeFPRegistersFromMask(State &, unsigned mask, M68kDataFormat fmt, bool reverse=false) const
Create a list of floating-point data registers.
SgAsmRegisterReferenceExpression * makeFPRegister(State &, unsigned regnum) const
Create a floating point register.
SgAsmExpression * makeEffectiveAddress(State &, unsigned modreg, M68kDataFormat fmt, size_t ext_offset) const
Create an expression for m68k "<ea>x" or "<ea>y".
SgAsmRegisterReferenceExpression * makeDataRegister(State &, unsigned regnum, M68kDataFormat, size_t bit_offset=0) const
Create a data register reference expression.
virtual SgAsmInstruction * disassembleOne(const MemoryMap::Ptr &, rose_addr_t start_va, AddressSet *successors=nullptr) override
This is the lowest level disassembly function and is implemented in the architecture-specific subclas...
static Ptr instance(const Architecture::BaseConstPtr &, M68kFamily)
Allocating constructor for a specific family.
SgAsmRegisterReferenceExpression * makeMacAccumulatorRegister(State &, unsigned accumIndex) const
Create a MAC accumulator register.
uint16_t instructionWord(State &, size_t n) const
Return the Nth instruction word.
size_t extensionWordsUsed(State &) const
Returns number of instruction words referenced so far in the current instruction.
SgAsmType * makeType(State &, M68kDataFormat) const
Create a ROSE data type for m68k data format.
SgAsmIntegerValueExpression * makeImmediateExtension(State &, M68kDataFormat fmt, size_t ext_word_idx) const
Create an integer expression from extension words.
SgAsmRegisterReferenceExpression * makeStatusRegister(State &) const
Create a reference to the status register.
void insert_idis(Decoder *)
Insert an instruction-specific disassembler.
SgAsmRegisterReferenceExpression * makeColdFireControlRegister(State &, unsigned regnum) const
Create control register for ColdFire cpu.
SgAsmExpression * makeEffectiveAddress(State &, unsigned mode, unsigned reg, M68kDataFormat fmt, size_t ext_offset) const
Create an expression for m68k "<ea>x" or "<ea>y".
SgAsmRegisterReferenceExpression * makeRegister(RegisterDescriptor) const
Generic ways to make a register.
void start_instruction(State &state, const MemoryMap::Ptr &map, rose_addr_t start_va) const
Called by disassembleOne() to initialize the disassembler state for the next instruction.
SgAsmRegisterReferenceExpression * makeConditionCodeRegister(State &) const
Create a reference to the condition code register.
SgAsmMemoryReferenceExpression * makeAddressRegisterPreDecrement(State &, unsigned regnum, M68kDataFormat fmt) const
Make a memory reference expression using an address register in pre-decrement mode.
SgAsmRegisterReferenceExpression * makeDataAddressRegister(State &, unsigned regnum, M68kDataFormat fmt, size_t bit_offset=0) const
Create either a data or address register reference expression.
SgAsmRegisterReferenceExpression * makeAddressRegister(State &, unsigned regnum, M68kDataFormat, size_t bit_offset=0) const
Create an address register reference expression.
Decoder * find_idis(uint16_t *insn_bytes, size_t nbytes) const
Find an instruction-specific disassembler.
M68kFamily get_family() const
Returns ISA family specified in constructor.
Describes (part of) a physical CPU register.
Describes a pattern of bits in a finite number of words.
Definition BitPattern.h:56
Reference-counting intrusive smart pointer.
Base class for expressions.
Base class for machine instructions.
Base class for integer values.
Reference to memory locations.
An ordered list of registers.
Base class for references to a machine register.
Base class for binary types.
std::shared_ptr< const Base > BaseConstPtr
Reference counted pointer for Architecture::Base.
The ROSE library.
rose_addr_t insn_va
Address of instruction.
MemoryMap::Ptr map
Map from which to read instruction words.
size_t niwords_used
High water number of instruction words used by instructionWord().
size_t niwords
Number of instruction words read.