Jia Liu | 31d157a | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | //===-- X86Disassembler.h - Disassembler for x86 and x86_64 -----*- C++ -*-===// |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // The X86 disassembler is a table-driven disassembler for the 16-, 32-, and |
| 11 | // 64-bit X86 instruction sets. The main decode sequence for an assembly |
| 12 | // instruction in this disassembler is: |
| 13 | // |
| 14 | // 1. Read the prefix bytes and determine the attributes of the instruction. |
| 15 | // These attributes, recorded in enum attributeBits |
| 16 | // (X86DisassemblerDecoderCommon.h), form a bitmask. The table CONTEXTS_SYM |
| 17 | // provides a mapping from bitmasks to contexts, which are represented by |
| 18 | // enum InstructionContext (ibid.). |
| 19 | // |
| 20 | // 2. Read the opcode, and determine what kind of opcode it is. The |
| 21 | // disassembler distinguishes four kinds of opcodes, which are enumerated in |
| 22 | // OpcodeType (X86DisassemblerDecoderCommon.h): one-byte (0xnn), two-byte |
| 23 | // (0x0f 0xnn), three-byte-38 (0x0f 0x38 0xnn), or three-byte-3a |
| 24 | // (0x0f 0x3a 0xnn). Mandatory prefixes are treated as part of the context. |
| 25 | // |
| 26 | // 3. Depending on the opcode type, look in one of four ClassDecision structures |
| 27 | // (X86DisassemblerDecoderCommon.h). Use the opcode class to determine which |
| 28 | // OpcodeDecision (ibid.) to look the opcode in. Look up the opcode, to get |
| 29 | // a ModRMDecision (ibid.). |
| 30 | // |
| 31 | // 4. Some instructions, such as escape opcodes or extended opcodes, or even |
| 32 | // instructions that have ModRM*Reg / ModRM*Mem forms in LLVM, need the |
| 33 | // ModR/M byte to complete decode. The ModRMDecision's type is an entry from |
| 34 | // ModRMDecisionType (X86DisassemblerDecoderCommon.h) that indicates if the |
| 35 | // ModR/M byte is required and how to interpret it. |
| 36 | // |
| 37 | // 5. After resolving the ModRMDecision, the disassembler has a unique ID |
| 38 | // of type InstrUID (X86DisassemblerDecoderCommon.h). Looking this ID up in |
| 39 | // INSTRUCTIONS_SYM yields the name of the instruction and the encodings and |
| 40 | // meanings of its operands. |
| 41 | // |
| 42 | // 6. For each operand, its encoding is an entry from OperandEncoding |
| 43 | // (X86DisassemblerDecoderCommon.h) and its type is an entry from |
| 44 | // OperandType (ibid.). The encoding indicates how to read it from the |
| 45 | // instruction; the type indicates how to interpret the value once it has |
| 46 | // been read. For example, a register operand could be stored in the R/M |
| 47 | // field of the ModR/M byte, the REG field of the ModR/M byte, or added to |
| 48 | // the main opcode. This is orthogonal from its meaning (an GPR or an XMM |
| 49 | // register, for instance). Given this information, the operands can be |
| 50 | // extracted and interpreted. |
| 51 | // |
| 52 | // 7. As the last step, the disassembler translates the instruction information |
| 53 | // and operands into a format understandable by the client - in this case, an |
| 54 | // MCInst for use by the MC infrastructure. |
| 55 | // |
| 56 | // The disassembler is broken broadly into two parts: the table emitter that |
| 57 | // emits the instruction decode tables discussed above during compilation, and |
| 58 | // the disassembler itself. The table emitter is documented in more detail in |
| 59 | // utils/TableGen/X86DisassemblerEmitter.h. |
| 60 | // |
| 61 | // X86Disassembler.h contains the public interface for the disassembler, |
| 62 | // adhering to the MCDisassembler interface. |
| 63 | // X86Disassembler.cpp contains the code responsible for step 7, and for |
| 64 | // invoking the decoder to execute steps 1-6. |
| 65 | // X86DisassemblerDecoderCommon.h contains the definitions needed by both the |
| 66 | // table emitter and the disassembler. |
| 67 | // X86DisassemblerDecoder.h contains the public interface of the decoder, |
| 68 | // factored out into C for possible use by other projects. |
| 69 | // X86DisassemblerDecoder.c contains the source code of the decoder, which is |
| 70 | // responsible for steps 1-6. |
| 71 | // |
| 72 | //===----------------------------------------------------------------------===// |
| 73 | |
| 74 | #ifndef X86DISASSEMBLER_H |
| 75 | #define X86DISASSEMBLER_H |
| 76 | |
| 77 | #define INSTRUCTION_SPECIFIER_FIELDS \ |
| 78 | const char* name; |
| 79 | |
| 80 | #define INSTRUCTION_IDS \ |
Craig Topper | ce8f4c5 | 2012-02-09 07:45:30 +0000 | [diff] [blame] | 81 | unsigned instructionIDs; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 82 | |
| 83 | #include "X86DisassemblerDecoderCommon.h" |
| 84 | |
| 85 | #undef INSTRUCTION_SPECIFIER_FIELDS |
| 86 | #undef INSTRUCTION_IDS |
| 87 | |
| 88 | #include "llvm/MC/MCDisassembler.h" |
| 89 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 90 | namespace llvm { |
| 91 | |
| 92 | class MCInst; |
Benjamin Kramer | 953362c | 2012-02-11 14:50:54 +0000 | [diff] [blame] | 93 | class MCInstrInfo; |
James Molloy | b950585 | 2011-09-07 17:24:38 +0000 | [diff] [blame] | 94 | class MCSubtargetInfo; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 95 | class MemoryObject; |
| 96 | class raw_ostream; |
Sean Callanan | 9899f70 | 2010-04-13 21:21:57 +0000 | [diff] [blame] | 97 | |
| 98 | struct EDInstInfo; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 99 | |
| 100 | namespace 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. |
| 105 | class X86GenericDisassembler : public MCDisassembler { |
Benjamin Kramer | 953362c | 2012-02-11 14:50:54 +0000 | [diff] [blame] | 106 | const MCInstrInfo *MII; |
Craig Topper | 224c1b2 | 2011-12-21 08:06:52 +0000 | [diff] [blame] | 107 | public: |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 108 | /// Constructor - Initializes the disassembler. |
| 109 | /// |
| 110 | /// @param mode - The X86 architecture mode to decode for. |
Benjamin Kramer | 953362c | 2012-02-11 14:50:54 +0000 | [diff] [blame] | 111 | X86GenericDisassembler(const MCSubtargetInfo &STI, DisassemblerMode mode, |
| 112 | const MCInstrInfo *MII); |
Craig Topper | 224c1b2 | 2011-12-21 08:06:52 +0000 | [diff] [blame] | 113 | private: |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 114 | ~X86GenericDisassembler(); |
Craig Topper | 224c1b2 | 2011-12-21 08:06:52 +0000 | [diff] [blame] | 115 | public: |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 116 | |
| 117 | /// getInstruction - See MCDisassembler. |
Owen Anderson | 83e3f67 | 2011-08-17 17:44:15 +0000 | [diff] [blame] | 118 | DecodeStatus getInstruction(MCInst &instr, |
| 119 | uint64_t &size, |
Derek Schuff | adef06a | 2012-02-29 01:09:06 +0000 | [diff] [blame^] | 120 | const MemoryObject ®ion, |
Owen Anderson | 83e3f67 | 2011-08-17 17:44:15 +0000 | [diff] [blame] | 121 | uint64_t address, |
Owen Anderson | 98c5dda | 2011-09-15 23:38:46 +0000 | [diff] [blame] | 122 | raw_ostream &vStream, |
| 123 | raw_ostream &cStream) const; |
Sean Callanan | 9899f70 | 2010-04-13 21:21:57 +0000 | [diff] [blame] | 124 | |
| 125 | /// getEDInfo - See MCDisassembler. |
Benjamin Kramer | 88b6fc0 | 2012-02-11 14:51:07 +0000 | [diff] [blame] | 126 | const EDInstInfo *getEDInfo() const; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 127 | private: |
| 128 | DisassemblerMode fMode; |
| 129 | }; |
| 130 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 131 | } // namespace X86Disassembler |
Craig Topper | 224c1b2 | 2011-12-21 08:06:52 +0000 | [diff] [blame] | 132 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 133 | } // namespace llvm |
Craig Topper | 224c1b2 | 2011-12-21 08:06:52 +0000 | [diff] [blame] | 134 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 135 | #endif |