1 //===-- X86DisassemblerDecoderInternal.h - Disassembler decoder -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is part of the X86 Disassembler.
11 // It contains the public interface of the instruction decoder.
12 // Documentation for the disassembler can be found in X86Disassembler.h.
14 //===----------------------------------------------------------------------===//
16 #ifndef X86DISASSEMBLERDECODER_H
17 #define X86DISASSEMBLERDECODER_H
19 #define INSTRUCTION_IDS \
20 uint16_t instructionIDs;
22 #include "X86DisassemblerDecoderCommon.h"
24 #undef INSTRUCTION_SPECIFIER_FIELDS
25 #undef INSTRUCTION_IDS
28 namespace X86Disassembler {
31 * Accessor functions for various fields of an Intel instruction
33 #define modFromModRM(modRM) (((modRM) & 0xc0) >> 6)
34 #define regFromModRM(modRM) (((modRM) & 0x38) >> 3)
35 #define rmFromModRM(modRM) ((modRM) & 0x7)
36 #define scaleFromSIB(sib) (((sib) & 0xc0) >> 6)
37 #define indexFromSIB(sib) (((sib) & 0x38) >> 3)
38 #define baseFromSIB(sib) ((sib) & 0x7)
39 #define wFromREX(rex) (((rex) & 0x8) >> 3)
40 #define rFromREX(rex) (((rex) & 0x4) >> 2)
41 #define xFromREX(rex) (((rex) & 0x2) >> 1)
42 #define bFromREX(rex) ((rex) & 0x1)
44 #define rFromEVEX2of4(evex) (((~(evex)) & 0x80) >> 7)
45 #define xFromEVEX2of4(evex) (((~(evex)) & 0x40) >> 6)
46 #define bFromEVEX2of4(evex) (((~(evex)) & 0x20) >> 5)
47 #define r2FromEVEX2of4(evex) (((~(evex)) & 0x10) >> 4)
48 #define mmFromEVEX2of4(evex) ((evex) & 0x3)
49 #define wFromEVEX3of4(evex) (((evex) & 0x80) >> 7)
50 #define vvvvFromEVEX3of4(evex) (((~(evex)) & 0x78) >> 3)
51 #define ppFromEVEX3of4(evex) ((evex) & 0x3)
52 #define zFromEVEX4of4(evex) (((evex) & 0x80) >> 7)
53 #define l2FromEVEX4of4(evex) (((evex) & 0x40) >> 6)
54 #define lFromEVEX4of4(evex) (((evex) & 0x20) >> 5)
55 #define bFromEVEX4of4(evex) (((evex) & 0x10) >> 4)
56 #define v2FromEVEX4of4(evex) (((~evex) & 0x8) >> 3)
57 #define aaaFromEVEX4of4(evex) ((evex) & 0x7)
59 #define rFromVEX2of3(vex) (((~(vex)) & 0x80) >> 7)
60 #define xFromVEX2of3(vex) (((~(vex)) & 0x40) >> 6)
61 #define bFromVEX2of3(vex) (((~(vex)) & 0x20) >> 5)
62 #define mmmmmFromVEX2of3(vex) ((vex) & 0x1f)
63 #define wFromVEX3of3(vex) (((vex) & 0x80) >> 7)
64 #define vvvvFromVEX3of3(vex) (((~(vex)) & 0x78) >> 3)
65 #define lFromVEX3of3(vex) (((vex) & 0x4) >> 2)
66 #define ppFromVEX3of3(vex) ((vex) & 0x3)
68 #define rFromVEX2of2(vex) (((~(vex)) & 0x80) >> 7)
69 #define vvvvFromVEX2of2(vex) (((~(vex)) & 0x78) >> 3)
70 #define lFromVEX2of2(vex) (((vex) & 0x4) >> 2)
71 #define ppFromVEX2of2(vex) ((vex) & 0x3)
73 #define rFromXOP2of3(xop) (((~(xop)) & 0x80) >> 7)
74 #define xFromXOP2of3(xop) (((~(xop)) & 0x40) >> 6)
75 #define bFromXOP2of3(xop) (((~(xop)) & 0x20) >> 5)
76 #define mmmmmFromXOP2of3(xop) ((xop) & 0x1f)
77 #define wFromXOP3of3(xop) (((xop) & 0x80) >> 7)
78 #define vvvvFromXOP3of3(vex) (((~(vex)) & 0x78) >> 3)
79 #define lFromXOP3of3(xop) (((xop) & 0x4) >> 2)
80 #define ppFromXOP3of3(xop) ((xop) & 0x3)
83 * These enums represent Intel registers for use by the decoder.
108 #define EA_BASES_16BIT \
144 #define EA_BASES_32BIT \
180 #define EA_BASES_64BIT \
338 #define REGS_SEGMENT \
356 #define REGS_CONTROL \
367 #define ALL_EA_BASES \
372 #define ALL_SIB_BASES \
392 * EABase - All possible values of the base field for effective-address
393 * computations, a.k.a. the Mod and R/M fields of the ModR/M byte. We
394 * distinguish between bases (EA_BASE_*) and registers that just happen to be
395 * referred to when Mod == 0b11 (EA_REG_*).
399 #define ENTRY(x) EA_BASE_##x,
402 #define ENTRY(x) EA_REG_##x,
409 * SIBIndex - All possible values of the SIB index field.
410 * Borrows entries from ALL_EA_BASES with the special case that
411 * sib is synonymous with NONE.
412 * Vector SIB: index can be XMM or YMM.
416 #define ENTRY(x) SIB_INDEX_##x,
426 * SIBBase - All possible values of the SIB base field.
430 #define ENTRY(x) SIB_BASE_##x,
437 * EADisplacement - Possible displacement types for effective-address
448 * Reg - All possible values of the reg field in the ModR/M byte.
451 #define ENTRY(x) MODRM_REG_##x,
458 * SegmentOverride - All possible segment overrides.
472 * VEXLeadingOpcodeByte - Possible values for the VEX.m-mmmm field
479 } VEXLeadingOpcodeByte;
482 XOP_MAP_SELECT_8 = 0x8,
483 XOP_MAP_SELECT_9 = 0x9,
484 XOP_MAP_SELECT_A = 0xA
488 * VEXPrefixCode - Possible values for the VEX.pp/EVEX.pp field
492 VEX_PREFIX_NONE = 0x0,
499 TYPE_NO_VEX_XOP = 0x0,
504 } VectorExtensionType;
506 typedef uint8_t BOOL;
509 * byteReader_t - Type for the byte reader that the consumer must provide to
510 * the decoder. Reads a single byte from the instruction's address space.
511 * @param arg - A baton that the consumer can associate with any internal
512 * state that it needs.
513 * @param byte - A pointer to a single byte in memory that should be set to
514 * contain the value at address.
515 * @param address - The address in the instruction's address space that should
517 * @return - -1 if the byte cannot be read for any reason; 0 otherwise.
519 typedef int (*byteReader_t)(const void* arg, uint8_t* byte, uint64_t address);
522 * dlog_t - Type for the logging function that the consumer can provide to
523 * get debugging output from the decoder.
524 * @param arg - A baton that the consumer can associate with any internal
525 * state that it needs.
526 * @param log - A string that contains the message. Will be reused after
527 * the logger returns.
529 typedef void (*dlog_t)(void* arg, const char *log);
532 * The specification for how to extract and interpret a full instruction and
535 struct InstructionSpecifier {
540 * The x86 internal instruction, which is produced by the decoder.
542 struct InternalInstruction {
543 /* Reader interface (C) */
545 /* Opaque value passed to the reader */
546 const void* readerArg;
547 /* The address of the next byte to read via the reader */
548 uint64_t readerCursor;
550 /* Logger interface (C) */
552 /* Opaque value passed to the logger */
555 /* General instruction information */
557 /* The mode to disassemble for (64-bit, protected, real) */
558 DisassemblerMode mode;
559 /* The start of the instruction, usable with the reader */
560 uint64_t startLocation;
561 /* The length of the instruction, in bytes */
566 /* 1 if the prefix byte corresponding to the entry is present; 0 if not */
567 uint8_t prefixPresent[0x100];
568 /* contains the location (for use with the reader) of the prefix byte */
569 uint64_t prefixLocations[0x100];
570 /* The value of the vector extension prefix(EVEX/VEX/XOP), if present */
571 uint8_t vectorExtensionPrefix[4];
572 /* The type of the vector extension prefix */
573 VectorExtensionType vectorExtensionType;
574 /* The value of the REX prefix, if present */
576 /* The location where a mandatory prefix would have to be (i.e., right before
577 the opcode, or right before the REX prefix if one is present) */
578 uint64_t necessaryPrefixLocation;
579 /* The segment override type */
580 SegmentOverride segmentOverride;
581 /* 1 if the prefix byte, 0xf2 or 0xf3 is xacquire or xrelease */
582 BOOL xAcquireRelease;
584 /* Sizes of various critical pieces of data, in bytes */
585 uint8_t registerSize;
587 uint8_t displacementSize;
588 uint8_t immediateSize;
590 /* Offsets from the start of the instruction to the pieces of data, which is
591 needed to find relocation entries for adding symbolic operands */
592 uint8_t displacementOffset;
593 uint8_t immediateOffset;
597 /* The last byte of the opcode, not counting any ModR/M extension */
599 /* The ModR/M byte of the instruction, if it is an opcode extension */
600 uint8_t modRMExtension;
604 /* The type of opcode, used for indexing into the array of decode tables */
605 OpcodeType opcodeType;
606 /* The instruction ID, extracted from the decode table */
607 uint16_t instructionID;
608 /* The specifier for the instruction, from the instruction info table */
609 const struct InstructionSpecifier *spec;
611 /* state for additional bytes, consumed during operand decode. Pattern:
612 consumed___ indicates that the byte was already consumed and does not
613 need to be consumed again */
615 /* The VEX.vvvv field, which contains a third register operand for some AVX
619 /* The writemask for AVX-512 instructions which is contained in EVEX.aaa */
622 /* The ModR/M byte, which contains most register operands and some portion of
623 all memory operands */
627 /* The SIB byte, used for more complex 32- or 64-bit memory operands */
631 /* The displacement, used for memory operands */
632 BOOL consumedDisplacement;
633 int32_t displacement;
635 /* Immediates. There can be two in some cases */
636 uint8_t numImmediatesConsumed;
637 uint8_t numImmediatesTranslated;
638 uint64_t immediates[2];
640 /* A register or immediate operand encoded into the opcode */
643 /* Portions of the ModR/M byte */
645 /* These fields determine the allowable values for the ModR/M fields, which
646 depend on operand and address widths */
651 /* The Mod and R/M fields can encode a base for an effective address, or a
652 register. These are separated into two fields here */
654 EADisplacement eaDisplacement;
655 /* The reg field always encodes a register */
663 const struct OperandSpecifier *operands;
666 /* decodeInstruction - Decode one instruction and store the decoding results in
667 * a buffer provided by the consumer.
668 * @param insn - The buffer to store the instruction in. Allocated by the
670 * @param reader - The byteReader_t for the bytes to be read.
671 * @param readerArg - An argument to pass to the reader for storing context
672 * specific to the consumer. May be NULL.
673 * @param logger - The dlog_t to be used in printing status messages from the
674 * disassembler. May be NULL.
675 * @param loggerArg - An argument to pass to the logger for storing context
676 * specific to the logger. May be NULL.
677 * @param startLoc - The address (in the reader's address space) of the first
678 * byte in the instruction.
679 * @param mode - The mode (16-bit, 32-bit, 64-bit) to decode in.
680 * @return - Nonzero if there was an error during decode, 0 otherwise.
682 int decodeInstruction(struct InternalInstruction* insn,
684 const void* readerArg,
689 DisassemblerMode mode);
691 /* \brief Debug - Print a message to debugs()
692 * @param file - The name of the file printing the debug message.
693 * @param line - The line number that printed the debug message.
694 * @param s - The message to print.
697 void Debug(const char *file, unsigned line, const char *s);
699 const char *GetInstrName(unsigned Opcode, const void *mii);
701 } // namespace X86Disassembler