xref: /aosp_15_r20/external/capstone/arch/X86/X86Disassembler.h (revision 9a0e4156d50a75a99ec4f1653a0e9602a5d45c18)
1*9a0e4156SSadaf Ebrahimi //===-- X86Disassembler.h - Disassembler for x86 and x86_64 -----*- C++ -*-===//
2*9a0e4156SSadaf Ebrahimi //
3*9a0e4156SSadaf Ebrahimi //                     The LLVM Compiler Infrastructure
4*9a0e4156SSadaf Ebrahimi //
5*9a0e4156SSadaf Ebrahimi // This file is distributed under the University of Illinois Open Source
6*9a0e4156SSadaf Ebrahimi // License. See LICENSE.TXT for details.
7*9a0e4156SSadaf Ebrahimi //
8*9a0e4156SSadaf Ebrahimi //===----------------------------------------------------------------------===//
9*9a0e4156SSadaf Ebrahimi //
10*9a0e4156SSadaf Ebrahimi // The X86 disassembler is a table-driven disassembler for the 16-, 32-, and
11*9a0e4156SSadaf Ebrahimi // 64-bit X86 instruction sets.  The main decode sequence for an assembly
12*9a0e4156SSadaf Ebrahimi // instruction in this disassembler is:
13*9a0e4156SSadaf Ebrahimi //
14*9a0e4156SSadaf Ebrahimi // 1. Read the prefix bytes and determine the attributes of the instruction.
15*9a0e4156SSadaf Ebrahimi //    These attributes, recorded in enum attributeBits
16*9a0e4156SSadaf Ebrahimi //    (X86DisassemblerDecoderCommon.h), form a bitmask.  The table CONTEXTS_SYM
17*9a0e4156SSadaf Ebrahimi //    provides a mapping from bitmasks to contexts, which are represented by
18*9a0e4156SSadaf Ebrahimi //    enum InstructionContext (ibid.).
19*9a0e4156SSadaf Ebrahimi //
20*9a0e4156SSadaf Ebrahimi // 2. Read the opcode, and determine what kind of opcode it is.  The
21*9a0e4156SSadaf Ebrahimi //    disassembler distinguishes four kinds of opcodes, which are enumerated in
22*9a0e4156SSadaf Ebrahimi //    OpcodeType (X86DisassemblerDecoderCommon.h): one-byte (0xnn), two-byte
23*9a0e4156SSadaf Ebrahimi //    (0x0f 0xnn), three-byte-38 (0x0f 0x38 0xnn), or three-byte-3a
24*9a0e4156SSadaf Ebrahimi //    (0x0f 0x3a 0xnn).  Mandatory prefixes are treated as part of the context.
25*9a0e4156SSadaf Ebrahimi //
26*9a0e4156SSadaf Ebrahimi // 3. Depending on the opcode type, look in one of four ClassDecision structures
27*9a0e4156SSadaf Ebrahimi //    (X86DisassemblerDecoderCommon.h).  Use the opcode class to determine which
28*9a0e4156SSadaf Ebrahimi //    OpcodeDecision (ibid.) to look the opcode in.  Look up the opcode, to get
29*9a0e4156SSadaf Ebrahimi //    a ModRMDecision (ibid.).
30*9a0e4156SSadaf Ebrahimi //
31*9a0e4156SSadaf Ebrahimi // 4. Some instructions, such as escape opcodes or extended opcodes, or even
32*9a0e4156SSadaf Ebrahimi //    instructions that have ModRM*Reg / ModRM*Mem forms in LLVM, need the
33*9a0e4156SSadaf Ebrahimi //    ModR/M byte to complete decode.  The ModRMDecision's type is an entry from
34*9a0e4156SSadaf Ebrahimi //    ModRMDecisionType (X86DisassemblerDecoderCommon.h) that indicates if the
35*9a0e4156SSadaf Ebrahimi //    ModR/M byte is required and how to interpret it.
36*9a0e4156SSadaf Ebrahimi //
37*9a0e4156SSadaf Ebrahimi // 5. After resolving the ModRMDecision, the disassembler has a unique ID
38*9a0e4156SSadaf Ebrahimi //    of type InstrUID (X86DisassemblerDecoderCommon.h).  Looking this ID up in
39*9a0e4156SSadaf Ebrahimi //    INSTRUCTIONS_SYM yields the name of the instruction and the encodings and
40*9a0e4156SSadaf Ebrahimi //    meanings of its operands.
41*9a0e4156SSadaf Ebrahimi //
42*9a0e4156SSadaf Ebrahimi // 6. For each operand, its encoding is an entry from OperandEncoding
43*9a0e4156SSadaf Ebrahimi //    (X86DisassemblerDecoderCommon.h) and its type is an entry from
44*9a0e4156SSadaf Ebrahimi //    OperandType (ibid.).  The encoding indicates how to read it from the
45*9a0e4156SSadaf Ebrahimi //    instruction; the type indicates how to interpret the value once it has
46*9a0e4156SSadaf Ebrahimi //    been read.  For example, a register operand could be stored in the R/M
47*9a0e4156SSadaf Ebrahimi //    field of the ModR/M byte, the REG field of the ModR/M byte, or added to
48*9a0e4156SSadaf Ebrahimi //    the main opcode.  This is orthogonal from its meaning (an GPR or an XMM
49*9a0e4156SSadaf Ebrahimi //    register, for instance).  Given this information, the operands can be
50*9a0e4156SSadaf Ebrahimi //    extracted and interpreted.
51*9a0e4156SSadaf Ebrahimi //
52*9a0e4156SSadaf Ebrahimi // 7. As the last step, the disassembler translates the instruction information
53*9a0e4156SSadaf Ebrahimi //    and operands into a format understandable by the client - in this case, an
54*9a0e4156SSadaf Ebrahimi //    MCInst for use by the MC infrastructure.
55*9a0e4156SSadaf Ebrahimi //
56*9a0e4156SSadaf Ebrahimi // The disassembler is broken broadly into two parts: the table emitter that
57*9a0e4156SSadaf Ebrahimi // emits the instruction decode tables discussed above during compilation, and
58*9a0e4156SSadaf Ebrahimi // the disassembler itself.  The table emitter is documented in more detail in
59*9a0e4156SSadaf Ebrahimi // utils/TableGen/X86DisassemblerEmitter.h.
60*9a0e4156SSadaf Ebrahimi //
61*9a0e4156SSadaf Ebrahimi // X86Disassembler.h contains the public interface for the disassembler,
62*9a0e4156SSadaf Ebrahimi //   adhering to the MCDisassembler interface.
63*9a0e4156SSadaf Ebrahimi // X86Disassembler.cpp contains the code responsible for step 7, and for
64*9a0e4156SSadaf Ebrahimi //   invoking the decoder to execute steps 1-6.
65*9a0e4156SSadaf Ebrahimi // X86DisassemblerDecoderCommon.h contains the definitions needed by both the
66*9a0e4156SSadaf Ebrahimi //   table emitter and the disassembler.
67*9a0e4156SSadaf Ebrahimi // X86DisassemblerDecoder.h contains the public interface of the decoder,
68*9a0e4156SSadaf Ebrahimi //   factored out into C for possible use by other projects.
69*9a0e4156SSadaf Ebrahimi // X86DisassemblerDecoder.c contains the source code of the decoder, which is
70*9a0e4156SSadaf Ebrahimi //   responsible for steps 1-6.
71*9a0e4156SSadaf Ebrahimi //
72*9a0e4156SSadaf Ebrahimi //===----------------------------------------------------------------------===//
73*9a0e4156SSadaf Ebrahimi 
74*9a0e4156SSadaf Ebrahimi /* Capstone Disassembly Engine */
75*9a0e4156SSadaf Ebrahimi /* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */
76*9a0e4156SSadaf Ebrahimi 
77*9a0e4156SSadaf Ebrahimi #ifndef CS_X86_DISASSEMBLER_H
78*9a0e4156SSadaf Ebrahimi #define CS_X86_DISASSEMBLER_H
79*9a0e4156SSadaf Ebrahimi 
80*9a0e4156SSadaf Ebrahimi #include "capstone/capstone.h"
81*9a0e4156SSadaf Ebrahimi 
82*9a0e4156SSadaf Ebrahimi #include "../../MCInst.h"
83*9a0e4156SSadaf Ebrahimi 
84*9a0e4156SSadaf Ebrahimi #include "../../MCRegisterInfo.h"
85*9a0e4156SSadaf Ebrahimi #include "X86DisassemblerDecoderCommon.h"
86*9a0e4156SSadaf Ebrahimi 
87*9a0e4156SSadaf Ebrahimi bool X86_getInstruction(csh handle, const uint8_t *code, size_t code_len,
88*9a0e4156SSadaf Ebrahimi 		MCInst *instr, uint16_t *size, uint64_t address, void *info);
89*9a0e4156SSadaf Ebrahimi 
90*9a0e4156SSadaf Ebrahimi void X86_init(MCRegisterInfo *MRI);
91*9a0e4156SSadaf Ebrahimi 
92*9a0e4156SSadaf Ebrahimi #endif
93