X86Disassembler.h revision 207618
1141296Sdas//===- X86Disassembler.h - Disassembler for x86 and x86_64 ------*- C++ -*-===//
2141296Sdas//
32116Sjkh//                     The LLVM Compiler Infrastructure
42116Sjkh//
52116Sjkh// This file is distributed under the University of Illinois Open Source
62116Sjkh// License. See LICENSE.TXT for details.
7141296Sdas//
82116Sjkh//===----------------------------------------------------------------------===//
9141296Sdas//
102116Sjkh// The X86 disassembler is a table-driven disassembler for the 16-, 32-, and
112116Sjkh// 64-bit X86 instruction sets.  The main decode sequence for an assembly
122116Sjkh// instruction in this disassembler is:
132116Sjkh//
14176207Sbde// 1. Read the prefix bytes and determine the attributes of the instruction.
15176207Sbde//    These attributes, recorded in enum attributeBits
162116Sjkh//    (X86DisassemblerDecoderCommon.h), form a bitmask.  The table CONTEXTS_SYM
172116Sjkh//    provides a mapping from bitmasks to contexts, which are represented by
18141296Sdas//    enum InstructionContext (ibid.).
19141296Sdas//
20141296Sdas// 2. Read the opcode, and determine what kind of opcode it is.  The
212116Sjkh//    disassembler distinguishes four kinds of opcodes, which are enumerated in
22141296Sdas//    OpcodeType (X86DisassemblerDecoderCommon.h): one-byte (0xnn), two-byte
232116Sjkh//    (0x0f 0xnn), three-byte-38 (0x0f 0x38 0xnn), or three-byte-3a
242116Sjkh//    (0x0f 0x3a 0xnn).  Mandatory prefixes are treated as part of the context.
252116Sjkh//
26177765Sdas// 3. Depending on the opcode type, look in one of four ClassDecision structures
27177765Sdas//    (X86DisassemblerDecoderCommon.h).  Use the opcode class to determine which
282116Sjkh//    OpcodeDecision (ibid.) to look the opcode in.  Look up the opcode, to get
292116Sjkh//    a ModRMDecision (ibid.).
302116Sjkh//
312116Sjkh// 4. Some instructions, such as escape opcodes or extended opcodes, or even
322116Sjkh//    instructions that have ModRM*Reg / ModRM*Mem forms in LLVM, need the
332116Sjkh//    ModR/M byte to complete decode.  The ModRMDecision's type is an entry from
3497413Salfred//    ModRMDecisionType (X86DisassemblerDecoderCommon.h) that indicates if the
35117912Speter//    ModR/M byte is required and how to interpret it.
362116Sjkh//
372116Sjkh// 5. After resolving the ModRMDecision, the disassembler has a unique ID
382116Sjkh//    of type InstrUID (X86DisassemblerDecoderCommon.h).  Looking this ID up in
392116Sjkh//    INSTRUCTIONS_SYM yields the name of the instruction and the encodings and
402116Sjkh//    meanings of its operands.
412116Sjkh//
422116Sjkh// 6. For each operand, its encoding is an entry from OperandEncoding
432116Sjkh//    (X86DisassemblerDecoderCommon.h) and its type is an entry from
442116Sjkh//    OperandType (ibid.).  The encoding indicates how to read it from the
452116Sjkh//    instruction; the type indicates how to interpret the value once it has
462116Sjkh//    been read.  For example, a register operand could be stored in the R/M
472116Sjkh//    field of the ModR/M byte, the REG field of the ModR/M byte, or added to
482116Sjkh//    the main opcode.  This is orthogonal from its meaning (an GPR or an XMM
492116Sjkh//    register, for instance).  Given this information, the operands can be
502116Sjkh//    extracted and interpreted.
512116Sjkh//
52176207Sbde// 7. As the last step, the disassembler translates the instruction information
532116Sjkh//    and operands into a format understandable by the client - in this case, an
542116Sjkh//    MCInst for use by the MC infrastructure.
552116Sjkh//
562116Sjkh// The disassembler is broken broadly into two parts: the table emitter that
572116Sjkh// emits the instruction decode tables discussed above during compilation, and
582116Sjkh// the disassembler itself.  The table emitter is documented in more detail in
592116Sjkh// utils/TableGen/X86DisassemblerEmitter.h.
602116Sjkh//
612116Sjkh// X86Disassembler.h contains the public interface for the disassembler,
622116Sjkh//   adhering to the MCDisassembler interface.
632116Sjkh// X86Disassembler.cpp contains the code responsible for step 7, and for
642116Sjkh//   invoking the decoder to execute steps 1-6.
652116Sjkh// X86DisassemblerDecoderCommon.h contains the definitions needed by both the
662116Sjkh//   table emitter and the disassembler.
672116Sjkh// X86DisassemblerDecoder.h contains the public interface of the decoder,
682116Sjkh//   factored out into C for possible use by other projects.
692116Sjkh// X86DisassemblerDecoder.c contains the source code of the decoder, which is
702116Sjkh//   responsible for steps 1-6.
712116Sjkh//
72176207Sbde//===----------------------------------------------------------------------===//
732116Sjkh
742116Sjkh#ifndef X86DISASSEMBLER_H
752116Sjkh#define X86DISASSEMBLER_H
76177765Sdas
77177765Sdas#define INSTRUCTION_SPECIFIER_FIELDS  \
78177765Sdas  const char*             name;
79177765Sdas
80#define INSTRUCTION_IDS               \
81  InstrUID*  instructionIDs;
82
83#include "X86DisassemblerDecoderCommon.h"
84
85#undef INSTRUCTION_SPECIFIER_FIELDS
86#undef INSTRUCTION_IDS
87
88#include "llvm/MC/MCDisassembler.h"
89
90struct InternalInstruction;
91
92namespace llvm {
93
94class MCInst;
95class MemoryObject;
96class raw_ostream;
97
98struct EDInstInfo;
99
100namespace X86Disassembler {
101
102/// X86GenericDisassembler - Generic disassembler for all X86 platforms.
103///   All each platform class should have to do is subclass the constructor, and
104///   provide a different disassemblerMode value.
105class X86GenericDisassembler : public MCDisassembler {
106protected:
107  /// Constructor     - Initializes the disassembler.
108  ///
109  /// @param mode     - The X86 architecture mode to decode for.
110  X86GenericDisassembler(DisassemblerMode mode);
111public:
112  ~X86GenericDisassembler();
113
114  /// getInstruction - See MCDisassembler.
115  bool getInstruction(MCInst &instr,
116                      uint64_t &size,
117                      const MemoryObject &region,
118                      uint64_t address,
119                      raw_ostream &vStream) const;
120
121  /// getEDInfo - See MCDisassembler.
122  EDInstInfo *getEDInfo() const;
123private:
124  DisassemblerMode              fMode;
125};
126
127/// X86_16Disassembler - 16-bit X86 disassembler.
128class X86_16Disassembler : public X86GenericDisassembler {
129public:
130  X86_16Disassembler() :
131    X86GenericDisassembler(MODE_16BIT) {
132  }
133};
134
135/// X86_16Disassembler - 32-bit X86 disassembler.
136class X86_32Disassembler : public X86GenericDisassembler {
137public:
138  X86_32Disassembler() :
139    X86GenericDisassembler(MODE_32BIT) {
140  }
141};
142
143/// X86_16Disassembler - 64-bit X86 disassembler.
144class X86_64Disassembler : public X86GenericDisassembler {
145public:
146  X86_64Disassembler() :
147    X86GenericDisassembler(MODE_64BIT) {
148  }
149};
150
151} // namespace X86Disassembler
152
153} // namespace llvm
154
155#endif
156