1 //===-- X86DisassemblerDecoderInternal.h - Disassembler decoder -*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is part of the X86 Disassembler.
10 // It contains the public interface of the instruction decoder.
11 // Documentation for the disassembler can be found in X86Disassembler.h.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LIB_TARGET_X86_DISASSEMBLER_X86DISASSEMBLERDECODER_H
16 #define LLVM_LIB_TARGET_X86_DISASSEMBLER_X86DISASSEMBLERDECODER_H
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/Support/X86DisassemblerDecoderCommon.h"
22 namespace X86Disassembler
{
24 #define bitFromOffset0(val) ((val) & 0x1)
25 #define bitFromOffset1(val) (((val) >> 1) & 0x1)
26 #define bitFromOffset2(val) (((val) >> 2) & 0x1)
27 #define bitFromOffset3(val) (((val) >> 3) & 0x1)
28 #define bitFromOffset4(val) (((val) >> 4) & 0x1)
29 #define bitFromOffset5(val) (((val) >> 5) & 0x1)
30 #define bitFromOffset6(val) (((val) >> 6) & 0x1)
31 #define bitFromOffset7(val) (((val) >> 7) & 0x1)
32 #define twoBitsFromOffset0(val) ((val) & 0x3)
33 #define twoBitsFromOffset6(val) (((val) >> 6) & 0x3)
34 #define threeBitsFromOffset0(val) ((val) & 0x7)
35 #define threeBitsFromOffset3(val) (((val) >> 3) & 0x7)
36 #define fiveBitsFromOffset0(val) ((val) & 0x1f)
37 #define invertedBitFromOffset2(val) (((~(val)) >> 2) & 0x1)
38 #define invertedBitFromOffset3(val) (((~(val)) >> 3) & 0x1)
39 #define invertedBitFromOffset4(val) (((~(val)) >> 4) & 0x1)
40 #define invertedBitFromOffset5(val) (((~(val)) >> 5) & 0x1)
41 #define invertedBitFromOffset6(val) (((~(val)) >> 6) & 0x1)
42 #define invertedBitFromOffset7(val) (((~(val)) >> 7) & 0x1)
43 #define invertedFourBitsFromOffset3(val) (((~(val)) >> 3) & 0xf)
45 #define modFromModRM(modRM) twoBitsFromOffset6(modRM)
46 #define regFromModRM(modRM) threeBitsFromOffset3(modRM)
47 #define rmFromModRM(modRM) threeBitsFromOffset0(modRM)
49 #define scaleFromSIB(sib) twoBitsFromOffset6(sib)
50 #define indexFromSIB(sib) threeBitsFromOffset3(sib)
51 #define baseFromSIB(sib) threeBitsFromOffset0(sib)
53 #define wFromREX(rex) bitFromOffset3(rex)
54 #define rFromREX(rex) bitFromOffset2(rex)
55 #define xFromREX(rex) bitFromOffset1(rex)
56 #define bFromREX(rex) bitFromOffset0(rex)
58 #define mFromREX2(rex2) bitFromOffset7(rex2)
59 #define r2FromREX2(rex2) bitFromOffset6(rex2)
60 #define x2FromREX2(rex2) bitFromOffset5(rex2)
61 #define b2FromREX2(rex2) bitFromOffset4(rex2)
62 #define wFromREX2(rex2) bitFromOffset3(rex2)
63 #define rFromREX2(rex2) bitFromOffset2(rex2)
64 #define xFromREX2(rex2) bitFromOffset1(rex2)
65 #define bFromREX2(rex2) bitFromOffset0(rex2)
67 #define rFromXOP2of3(xop) invertedBitFromOffset7(xop)
68 #define xFromXOP2of3(xop) invertedBitFromOffset6(xop)
69 #define bFromXOP2of3(xop) invertedBitFromOffset5(xop)
70 #define mmmmmFromXOP2of3(xop) fiveBitsFromOffset0(xop)
71 #define wFromXOP3of3(xop) bitFromOffset7(xop)
72 #define vvvvFromXOP3of3(xop) invertedFourBitsFromOffset3(xop)
73 #define lFromXOP3of3(xop) bitFromOffset2(xop)
74 #define ppFromXOP3of3(xop) twoBitsFromOffset0(xop)
76 #define rFromVEX2of2(vex) invertedBitFromOffset7(vex)
77 #define vvvvFromVEX2of2(vex) invertedFourBitsFromOffset3(vex)
78 #define lFromVEX2of2(vex) bitFromOffset2(vex)
79 #define ppFromVEX2of2(vex) twoBitsFromOffset0(vex)
81 #define rFromVEX2of3(vex) invertedBitFromOffset7(vex)
82 #define xFromVEX2of3(vex) invertedBitFromOffset6(vex)
83 #define bFromVEX2of3(vex) invertedBitFromOffset5(vex)
84 #define mmmmmFromVEX2of3(vex) fiveBitsFromOffset0(vex)
85 #define wFromVEX3of3(vex) bitFromOffset7(vex)
86 #define vvvvFromVEX3of3(vex) invertedFourBitsFromOffset3(vex)
87 #define lFromVEX3of3(vex) bitFromOffset2(vex)
88 #define ppFromVEX3of3(vex) twoBitsFromOffset0(vex)
90 #define rFromEVEX2of4(evex) invertedBitFromOffset7(evex)
91 #define xFromEVEX2of4(evex) invertedBitFromOffset6(evex)
92 #define bFromEVEX2of4(evex) invertedBitFromOffset5(evex)
93 #define r2FromEVEX2of4(evex) invertedBitFromOffset4(evex)
94 #define b2FromEVEX2of4(evex) bitFromOffset3(evex)
95 #define mmmFromEVEX2of4(evex) threeBitsFromOffset0(evex)
96 #define wFromEVEX3of4(evex) bitFromOffset7(evex)
97 #define vvvvFromEVEX3of4(evex) invertedFourBitsFromOffset3(evex)
98 #define x2FromEVEX3of4(evex) invertedBitFromOffset2(evex)
99 #define ppFromEVEX3of4(evex) twoBitsFromOffset0(evex)
100 #define zFromEVEX4of4(evex) bitFromOffset7(evex)
101 #define l2FromEVEX4of4(evex) bitFromOffset6(evex)
102 #define lFromEVEX4of4(evex) bitFromOffset5(evex)
103 #define bFromEVEX4of4(evex) bitFromOffset4(evex)
104 #define v2FromEVEX4of4(evex) invertedBitFromOffset3(evex)
105 #define aaaFromEVEX4of4(evex) threeBitsFromOffset0(evex)
106 #define nfFromEVEX4of4(evex) bitFromOffset2(evex)
108 // These enums represent Intel registers for use by the decoder.
147 #define EA_BASES_16BIT \
215 #define EA_BASES_32BIT \
283 #define EA_BASES_64BIT \
473 #define REGS_MASK_PAIRS \
479 #define REGS_SEGMENT \
505 #define REGS_CONTROL \
534 #define ALL_EA_BASES \
539 #define ALL_SIB_BASES \
560 /// All possible values of the base field for effective-address
561 /// computations, a.k.a. the Mod and R/M fields of the ModR/M byte.
562 /// We distinguish between bases (EA_BASE_*) and registers that just happen
563 /// to be referred to when Mod == 0b11 (EA_REG_*).
566 #define ENTRY(x) EA_BASE_##x,
569 #define ENTRY(x) EA_REG_##x,
575 /// All possible values of the SIB index field.
576 /// borrows entries from ALL_EA_BASES with the special case that
577 /// sib is synonymous with NONE.
578 /// Vector SIB: index can be XMM or YMM.
581 #define ENTRY(x) SIB_INDEX_##x,
590 /// All possible values of the SIB base field.
593 #define ENTRY(x) SIB_BASE_##x,
599 /// Possible displacement types for effective-address computations.
600 enum EADisplacement
{
607 /// All possible values of the reg field in the ModR/M byte.
609 #define ENTRY(x) MODRM_REG_##x,
615 /// All possible segment overrides.
616 enum SegmentOverride
{
627 /// Possible values for the VEX.m-mmmm field
628 enum VEXLeadingOpcodeByte
{
639 XOP_MAP_SELECT_8
= 0x8,
640 XOP_MAP_SELECT_9
= 0x9,
641 XOP_MAP_SELECT_A
= 0xA
644 /// Possible values for the VEX.pp/EVEX.pp field
646 VEX_PREFIX_NONE
= 0x0,
652 enum VectorExtensionType
{
653 TYPE_NO_VEX_XOP
= 0x0,
660 /// The specification for how to extract and interpret a full instruction and
662 struct InstructionSpecifier
{
666 /// The x86 internal instruction, which is produced by the decoder.
667 struct InternalInstruction
{
668 // Opaque value passed to the reader
669 llvm::ArrayRef
<uint8_t> bytes
;
670 // The address of the next byte to read via the reader
671 uint64_t readerCursor
;
673 // General instruction information
675 // The mode to disassemble for (64-bit, protected, real)
676 DisassemblerMode mode
;
677 // The start of the instruction, usable with the reader
678 uint64_t startLocation
;
679 // The length of the instruction, in bytes
684 // The possible mandatory prefix
685 uint8_t mandatoryPrefix
;
686 // The value of the vector extension prefix(EVEX/VEX/XOP), if present
687 uint8_t vectorExtensionPrefix
[4];
688 // The type of the vector extension prefix
689 VectorExtensionType vectorExtensionType
;
690 // The value of the REX2 prefix, if present
691 uint8_t rex2ExtensionPrefix
[2];
692 // The value of the REX prefix, if present
694 // The segment override type
695 SegmentOverride segmentOverride
;
696 // 1 if the prefix byte, 0xf2 or 0xf3 is xacquire or xrelease
697 bool xAcquireRelease
;
699 // Address-size override
701 // Operand-size override
705 // The repeat prefix if any
706 uint8_t repeatPrefix
;
708 // Sizes of various critical pieces of data, in bytes
709 uint8_t registerSize
;
711 uint8_t displacementSize
;
712 uint8_t immediateSize
;
714 // Offsets from the start of the instruction to the pieces of data, which is
715 // needed to find relocation entries for adding symbolic operands.
716 uint8_t displacementOffset
;
717 uint8_t immediateOffset
;
721 // The last byte of the opcode, not counting any ModR/M extension
726 // The type of opcode, used for indexing into the array of decode tables
727 OpcodeType opcodeType
;
728 // The instruction ID, extracted from the decode table
729 uint16_t instructionID
;
730 // The specifier for the instruction, from the instruction info table
731 const InstructionSpecifier
*spec
;
733 // state for additional bytes, consumed during operand decode. Pattern:
734 // consumed___ indicates that the byte was already consumed and does not
735 // need to be consumed again.
737 // The VEX.vvvv field, which contains a third register operand for some AVX
741 // The writemask for AVX-512 instructions which is contained in EVEX.aaa
744 // The ModR/M byte, which contains most register operands and some portion of
745 // all memory operands.
749 // The SIB byte, used for more complex 32- or 64-bit memory operands
752 // The displacement, used for memory operands
753 int32_t displacement
;
755 // Immediates. There can be two in some cases
756 uint8_t numImmediatesConsumed
;
757 uint8_t numImmediatesTranslated
;
758 uint64_t immediates
[2];
760 // A register or immediate operand encoded into the opcode
763 // Portions of the ModR/M byte
765 // These fields determine the allowable values for the ModR/M fields, which
766 // depend on operand and address widths.
770 // The Mod and R/M fields can encode a base for an effective address, or a
771 // register. These are separated into two fields here.
773 EADisplacement eaDisplacement
;
774 // The reg field always encodes a register
778 SIBIndex sibIndexBase
;
783 // Embedded rounding control.
786 ArrayRef
<OperandSpecifier
> operands
;
789 } // namespace X86Disassembler