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_SPECIFIER_FIELDS \
22 #define INSTRUCTION_IDS \
23 uint16_t instructionIDs;
25 #include "X86DisassemblerDecoderCommon.h"
27 #undef INSTRUCTION_SPECIFIER_FIELDS
28 #undef INSTRUCTION_IDS
31 namespace X86Disassembler {
34 * Accessor functions for various fields of an Intel instruction
36 #define modFromModRM(modRM) (((modRM) & 0xc0) >> 6)
37 #define regFromModRM(modRM) (((modRM) & 0x38) >> 3)
38 #define rmFromModRM(modRM) ((modRM) & 0x7)
39 #define scaleFromSIB(sib) (((sib) & 0xc0) >> 6)
40 #define indexFromSIB(sib) (((sib) & 0x38) >> 3)
41 #define baseFromSIB(sib) ((sib) & 0x7)
42 #define wFromREX(rex) (((rex) & 0x8) >> 3)
43 #define rFromREX(rex) (((rex) & 0x4) >> 2)
44 #define xFromREX(rex) (((rex) & 0x2) >> 1)
45 #define bFromREX(rex) ((rex) & 0x1)
47 #define rFromEVEX2of4(evex) (((~(evex)) & 0x80) >> 7)
48 #define xFromEVEX2of4(evex) (((~(evex)) & 0x40) >> 6)
49 #define bFromEVEX2of4(evex) (((~(evex)) & 0x20) >> 5)
50 #define r2FromEVEX2of4(evex) (((~(evex)) & 0x10) >> 4)
51 #define mmFromEVEX2of4(evex) ((evex) & 0x3)
52 #define wFromEVEX3of4(evex) (((evex) & 0x80) >> 7)
53 #define vvvvFromEVEX3of4(evex) (((~(evex)) & 0x78) >> 3)
54 #define ppFromEVEX3of4(evex) ((evex) & 0x3)
55 #define zFromEVEX4of4(evex) (((evex) & 0x80) >> 7)
56 #define l2FromEVEX4of4(evex) (((evex) & 0x40) >> 6)
57 #define lFromEVEX4of4(evex) (((evex) & 0x20) >> 5)
58 #define bFromEVEX4of4(evex) (((evex) & 0x10) >> 4)
59 #define v2FromEVEX4of4(evex) (((~evex) & 0x8) >> 3)
60 #define aaaFromEVEX4of4(evex) ((evex) & 0x7)
62 #define rFromVEX2of3(vex) (((~(vex)) & 0x80) >> 7)
63 #define xFromVEX2of3(vex) (((~(vex)) & 0x40) >> 6)
64 #define bFromVEX2of3(vex) (((~(vex)) & 0x20) >> 5)
65 #define mmmmmFromVEX2of3(vex) ((vex) & 0x1f)
66 #define wFromVEX3of3(vex) (((vex) & 0x80) >> 7)
67 #define vvvvFromVEX3of3(vex) (((~(vex)) & 0x78) >> 3)
68 #define lFromVEX3of3(vex) (((vex) & 0x4) >> 2)
69 #define ppFromVEX3of3(vex) ((vex) & 0x3)
71 #define rFromVEX2of2(vex) (((~(vex)) & 0x80) >> 7)
72 #define vvvvFromVEX2of2(vex) (((~(vex)) & 0x78) >> 3)
73 #define lFromVEX2of2(vex) (((vex) & 0x4) >> 2)
74 #define ppFromVEX2of2(vex) ((vex) & 0x3)
76 #define rFromXOP2of3(xop) (((~(xop)) & 0x80) >> 7)
77 #define xFromXOP2of3(xop) (((~(xop)) & 0x40) >> 6)
78 #define bFromXOP2of3(xop) (((~(xop)) & 0x20) >> 5)
79 #define mmmmmFromXOP2of3(xop) ((xop) & 0x1f)
80 #define wFromXOP3of3(xop) (((xop) & 0x80) >> 7)
81 #define vvvvFromXOP3of3(vex) (((~(vex)) & 0x78) >> 3)
82 #define lFromXOP3of3(xop) (((xop) & 0x4) >> 2)
83 #define ppFromXOP3of3(xop) ((xop) & 0x3)
86 * These enums represent Intel registers for use by the decoder.
111 #define EA_BASES_16BIT \
147 #define EA_BASES_32BIT \
183 #define EA_BASES_64BIT \
341 #define REGS_SEGMENT \
359 #define REGS_CONTROL \
370 #define ALL_EA_BASES \
375 #define ALL_SIB_BASES \
395 * EABase - All possible values of the base field for effective-address
396 * computations, a.k.a. the Mod and R/M fields of the ModR/M byte. We
397 * distinguish between bases (EA_BASE_*) and registers that just happen to be
398 * referred to when Mod == 0b11 (EA_REG_*).
402 #define ENTRY(x) EA_BASE_##x,
405 #define ENTRY(x) EA_REG_##x,
412 * SIBIndex - All possible values of the SIB index field.
413 * Borrows entries from ALL_EA_BASES with the special case that
414 * sib is synonymous with NONE.
415 * Vector SIB: index can be XMM or YMM.
419 #define ENTRY(x) SIB_INDEX_##x,
429 * SIBBase - All possible values of the SIB base field.
433 #define ENTRY(x) SIB_BASE_##x,
440 * EADisplacement - Possible displacement types for effective-address
451 * Reg - All possible values of the reg field in the ModR/M byte.
454 #define ENTRY(x) MODRM_REG_##x,
461 * SegmentOverride - All possible segment overrides.
475 * VEXLeadingOpcodeByte - Possible values for the VEX.m-mmmm field
482 } VEXLeadingOpcodeByte;
485 XOP_MAP_SELECT_8 = 0x8,
486 XOP_MAP_SELECT_9 = 0x9,
487 XOP_MAP_SELECT_A = 0xA
491 * VEXPrefixCode - Possible values for the VEX.pp/EVEX.pp field
495 VEX_PREFIX_NONE = 0x0,
502 TYPE_NO_VEX_XOP = 0x0,
507 } VectorExtensionType;
509 typedef uint8_t BOOL;
512 * byteReader_t - Type for the byte reader that the consumer must provide to
513 * the decoder. Reads a single byte from the instruction's address space.
514 * @param arg - A baton that the consumer can associate with any internal
515 * state that it needs.
516 * @param byte - A pointer to a single byte in memory that should be set to
517 * contain the value at address.
518 * @param address - The address in the instruction's address space that should
520 * @return - -1 if the byte cannot be read for any reason; 0 otherwise.
522 typedef int (*byteReader_t)(const void* arg, uint8_t* byte, uint64_t address);
525 * dlog_t - Type for the logging function that the consumer can provide to
526 * get debugging output from the decoder.
527 * @param arg - A baton that the consumer can associate with any internal
528 * state that it needs.
529 * @param log - A string that contains the message. Will be reused after
530 * the logger returns.
532 typedef void (*dlog_t)(void* arg, const char *log);
535 * The x86 internal instruction, which is produced by the decoder.
537 struct InternalInstruction {
538 /* Reader interface (C) */
540 /* Opaque value passed to the reader */
541 const void* readerArg;
542 /* The address of the next byte to read via the reader */
543 uint64_t readerCursor;
545 /* Logger interface (C) */
547 /* Opaque value passed to the logger */
550 /* General instruction information */
552 /* The mode to disassemble for (64-bit, protected, real) */
553 DisassemblerMode mode;
554 /* The start of the instruction, usable with the reader */
555 uint64_t startLocation;
556 /* The length of the instruction, in bytes */
561 /* 1 if the prefix byte corresponding to the entry is present; 0 if not */
562 uint8_t prefixPresent[0x100];
563 /* contains the location (for use with the reader) of the prefix byte */
564 uint64_t prefixLocations[0x100];
565 /* The value of the vector extension prefix(EVEX/VEX/XOP), if present */
566 uint8_t vectorExtensionPrefix[4];
567 /* The type of the vector extension prefix */
568 VectorExtensionType vectorExtensionType;
569 /* The value of the REX prefix, if present */
571 /* The location where a mandatory prefix would have to be (i.e., right before
572 the opcode, or right before the REX prefix if one is present) */
573 uint64_t necessaryPrefixLocation;
574 /* The segment override type */
575 SegmentOverride segmentOverride;
576 /* 1 if the prefix byte, 0xf2 or 0xf3 is xacquire or xrelease */
577 BOOL xAcquireRelease;
579 /* Sizes of various critical pieces of data, in bytes */
580 uint8_t registerSize;
582 uint8_t displacementSize;
583 uint8_t immediateSize;
585 /* Offsets from the start of the instruction to the pieces of data, which is
586 needed to find relocation entries for adding symbolic operands */
587 uint8_t displacementOffset;
588 uint8_t immediateOffset;
592 /* The last byte of the opcode, not counting any ModR/M extension */
594 /* The ModR/M byte of the instruction, if it is an opcode extension */
595 uint8_t modRMExtension;
599 /* The type of opcode, used for indexing into the array of decode tables */
600 OpcodeType opcodeType;
601 /* The instruction ID, extracted from the decode table */
602 uint16_t instructionID;
603 /* The specifier for the instruction, from the instruction info table */
604 const struct InstructionSpecifier *spec;
606 /* state for additional bytes, consumed during operand decode. Pattern:
607 consumed___ indicates that the byte was already consumed and does not
608 need to be consumed again */
610 /* The VEX.vvvv field, which contains a third register operand for some AVX
614 /* The writemask for AVX-512 instructions which is contained in EVEX.aaa */
617 /* The ModR/M byte, which contains most register operands and some portion of
618 all memory operands */
622 /* The SIB byte, used for more complex 32- or 64-bit memory operands */
626 /* The displacement, used for memory operands */
627 BOOL consumedDisplacement;
628 int32_t displacement;
630 /* Immediates. There can be two in some cases */
631 uint8_t numImmediatesConsumed;
632 uint8_t numImmediatesTranslated;
633 uint64_t immediates[2];
635 /* A register or immediate operand encoded into the opcode */
638 /* Portions of the ModR/M byte */
640 /* These fields determine the allowable values for the ModR/M fields, which
641 depend on operand and address widths */
646 /* The Mod and R/M fields can encode a base for an effective address, or a
647 register. These are separated into two fields here */
649 EADisplacement eaDisplacement;
650 /* The reg field always encodes a register */
658 const struct OperandSpecifier *operands;
661 /* decodeInstruction - Decode one instruction and store the decoding results in
662 * a buffer provided by the consumer.
663 * @param insn - The buffer to store the instruction in. Allocated by the
665 * @param reader - The byteReader_t for the bytes to be read.
666 * @param readerArg - An argument to pass to the reader for storing context
667 * specific to the consumer. May be NULL.
668 * @param logger - The dlog_t to be used in printing status messages from the
669 * disassembler. May be NULL.
670 * @param loggerArg - An argument to pass to the logger for storing context
671 * specific to the logger. May be NULL.
672 * @param startLoc - The address (in the reader's address space) of the first
673 * byte in the instruction.
674 * @param mode - The mode (16-bit, 32-bit, 64-bit) to decode in.
675 * @return - Nonzero if there was an error during decode, 0 otherwise.
677 int decodeInstruction(struct InternalInstruction* insn,
679 const void* readerArg,
684 DisassemblerMode mode);
686 /* \brief Debug - Print a message to debugs()
687 * @param file - The name of the file printing the debug message.
688 * @param line - The line number that printed the debug message.
689 * @param s - The message to print.
692 void Debug(const char *file, unsigned line, const char *s);
694 const char *GetInstrName(unsigned Opcode, const void *mii);
696 } // namespace X86Disassembler