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 // Accessor functions for various fields of an Intel instruction
25 #define modFromModRM(modRM) (((modRM) & 0xc0) >> 6)
26 #define regFromModRM(modRM) (((modRM) & 0x38) >> 3)
27 #define rmFromModRM(modRM) ((modRM) & 0x7)
28 #define scaleFromSIB(sib) (((sib) & 0xc0) >> 6)
29 #define indexFromSIB(sib) (((sib) & 0x38) >> 3)
30 #define baseFromSIB(sib) ((sib) & 0x7)
31 #define wFromREX(rex) (((rex) & 0x8) >> 3)
32 #define rFromREX(rex) (((rex) & 0x4) >> 2)
33 #define xFromREX(rex) (((rex) & 0x2) >> 1)
34 #define bFromREX(rex) ((rex) & 0x1)
36 #define rFromEVEX2of4(evex) (((~(evex)) & 0x80) >> 7)
37 #define xFromEVEX2of4(evex) (((~(evex)) & 0x40) >> 6)
38 #define bFromEVEX2of4(evex) (((~(evex)) & 0x20) >> 5)
39 #define r2FromEVEX2of4(evex) (((~(evex)) & 0x10) >> 4)
40 #define mmFromEVEX2of4(evex) ((evex) & 0x3)
41 #define wFromEVEX3of4(evex) (((evex) & 0x80) >> 7)
42 #define vvvvFromEVEX3of4(evex) (((~(evex)) & 0x78) >> 3)
43 #define ppFromEVEX3of4(evex) ((evex) & 0x3)
44 #define zFromEVEX4of4(evex) (((evex) & 0x80) >> 7)
45 #define l2FromEVEX4of4(evex) (((evex) & 0x40) >> 6)
46 #define lFromEVEX4of4(evex) (((evex) & 0x20) >> 5)
47 #define bFromEVEX4of4(evex) (((evex) & 0x10) >> 4)
48 #define v2FromEVEX4of4(evex) (((~evex) & 0x8) >> 3)
49 #define aaaFromEVEX4of4(evex) ((evex) & 0x7)
51 #define rFromVEX2of3(vex) (((~(vex)) & 0x80) >> 7)
52 #define xFromVEX2of3(vex) (((~(vex)) & 0x40) >> 6)
53 #define bFromVEX2of3(vex) (((~(vex)) & 0x20) >> 5)
54 #define mmmmmFromVEX2of3(vex) ((vex) & 0x1f)
55 #define wFromVEX3of3(vex) (((vex) & 0x80) >> 7)
56 #define vvvvFromVEX3of3(vex) (((~(vex)) & 0x78) >> 3)
57 #define lFromVEX3of3(vex) (((vex) & 0x4) >> 2)
58 #define ppFromVEX3of3(vex) ((vex) & 0x3)
60 #define rFromVEX2of2(vex) (((~(vex)) & 0x80) >> 7)
61 #define vvvvFromVEX2of2(vex) (((~(vex)) & 0x78) >> 3)
62 #define lFromVEX2of2(vex) (((vex) & 0x4) >> 2)
63 #define ppFromVEX2of2(vex) ((vex) & 0x3)
65 #define rFromXOP2of3(xop) (((~(xop)) & 0x80) >> 7)
66 #define xFromXOP2of3(xop) (((~(xop)) & 0x40) >> 6)
67 #define bFromXOP2of3(xop) (((~(xop)) & 0x20) >> 5)
68 #define mmmmmFromXOP2of3(xop) ((xop) & 0x1f)
69 #define wFromXOP3of3(xop) (((xop) & 0x80) >> 7)
70 #define vvvvFromXOP3of3(vex) (((~(vex)) & 0x78) >> 3)
71 #define lFromXOP3of3(xop) (((xop) & 0x4) >> 2)
72 #define ppFromXOP3of3(xop) ((xop) & 0x3)
74 // These enums represent Intel registers for use by the decoder.
97 #define EA_BASES_16BIT \
133 #define EA_BASES_32BIT \
169 #define EA_BASES_64BIT \
327 #define REGS_MASK_PAIRS \
333 #define REGS_SEGMENT \
359 #define REGS_CONTROL \
383 #define ALL_EA_BASES \
388 #define ALL_SIB_BASES \
409 /// All possible values of the base field for effective-address
410 /// computations, a.k.a. the Mod and R/M fields of the ModR/M byte.
411 /// We distinguish between bases (EA_BASE_*) and registers that just happen
412 /// to be referred to when Mod == 0b11 (EA_REG_*).
415 #define ENTRY(x) EA_BASE_##x,
418 #define ENTRY(x) EA_REG_##x,
424 /// All possible values of the SIB index field.
425 /// borrows entries from ALL_EA_BASES with the special case that
426 /// sib is synonymous with NONE.
427 /// Vector SIB: index can be XMM or YMM.
430 #define ENTRY(x) SIB_INDEX_##x,
439 /// All possible values of the SIB base field.
442 #define ENTRY(x) SIB_BASE_##x,
448 /// Possible displacement types for effective-address computations.
456 /// All possible values of the reg field in the ModR/M byte.
458 #define ENTRY(x) MODRM_REG_##x,
464 /// All possible segment overrides.
465 enum SegmentOverride
{
476 /// Possible values for the VEX.m-mmmm field
477 enum VEXLeadingOpcodeByte
{
484 XOP_MAP_SELECT_8
= 0x8,
485 XOP_MAP_SELECT_9
= 0x9,
486 XOP_MAP_SELECT_A
= 0xA
489 /// Possible values for the VEX.pp/EVEX.pp field
491 VEX_PREFIX_NONE
= 0x0,
497 enum VectorExtensionType
{
498 TYPE_NO_VEX_XOP
= 0x0,
505 /// Type for the byte reader that the consumer must provide to
506 /// the decoder. Reads a single byte from the instruction's address space.
507 /// \param arg A baton that the consumer can associate with any internal
508 /// state that it needs.
509 /// \param byte A pointer to a single byte in memory that should be set to
510 /// contain the value at address.
511 /// \param address The address in the instruction's address space that should
513 /// \return -1 if the byte cannot be read for any reason; 0 otherwise.
514 typedef int (*byteReader_t
)(const void *arg
, uint8_t *byte
, uint64_t address
);
516 /// Type for the logging function that the consumer can provide to
517 /// get debugging output from the decoder.
518 /// \param arg A baton that the consumer can associate with any internal
519 /// state that it needs.
520 /// \param log A string that contains the message. Will be reused after
521 /// the logger returns.
522 typedef void (*dlog_t
)(void *arg
, const char *log
);
524 /// The specification for how to extract and interpret a full instruction and
526 struct InstructionSpecifier
{
530 /// The x86 internal instruction, which is produced by the decoder.
531 struct InternalInstruction
{
532 // Reader interface (C)
534 // Opaque value passed to the reader
535 const void* readerArg
;
536 // The address of the next byte to read via the reader
537 uint64_t readerCursor
;
539 // Logger interface (C)
541 // Opaque value passed to the logger
544 // General instruction information
546 // The mode to disassemble for (64-bit, protected, real)
547 DisassemblerMode mode
;
548 // The start of the instruction, usable with the reader
549 uint64_t startLocation
;
550 // The length of the instruction, in bytes
555 // The possible mandatory prefix
556 uint8_t mandatoryPrefix
;
557 // The value of the vector extension prefix(EVEX/VEX/XOP), if present
558 uint8_t vectorExtensionPrefix
[4];
559 // The type of the vector extension prefix
560 VectorExtensionType vectorExtensionType
;
561 // The value of the REX prefix, if present
563 // The segment override type
564 SegmentOverride segmentOverride
;
565 // 1 if the prefix byte, 0xf2 or 0xf3 is xacquire or xrelease
566 bool xAcquireRelease
;
568 // Address-size override
570 // Operand-size override
574 // The repeat prefix if any
575 uint8_t repeatPrefix
;
577 // Sizes of various critical pieces of data, in bytes
578 uint8_t registerSize
;
580 uint8_t displacementSize
;
581 uint8_t immediateSize
;
583 // Offsets from the start of the instruction to the pieces of data, which is
584 // needed to find relocation entries for adding symbolic operands.
585 uint8_t displacementOffset
;
586 uint8_t immediateOffset
;
590 // The last byte of the opcode, not counting any ModR/M extension
595 // The type of opcode, used for indexing into the array of decode tables
596 OpcodeType opcodeType
;
597 // The instruction ID, extracted from the decode table
598 uint16_t instructionID
;
599 // The specifier for the instruction, from the instruction info table
600 const InstructionSpecifier
*spec
;
602 // state for additional bytes, consumed during operand decode. Pattern:
603 // consumed___ indicates that the byte was already consumed and does not
604 // need to be consumed again.
606 // The VEX.vvvv field, which contains a third register operand for some AVX
610 // The writemask for AVX-512 instructions which is contained in EVEX.aaa
613 // The ModR/M byte, which contains most register operands and some portion of
614 // all memory operands.
618 // The SIB byte, used for more complex 32- or 64-bit memory operands
622 // The displacement, used for memory operands
623 bool consumedDisplacement
;
624 int32_t displacement
;
626 // Immediates. There can be two in some cases
627 uint8_t numImmediatesConsumed
;
628 uint8_t numImmediatesTranslated
;
629 uint64_t immediates
[2];
631 // A register or immediate operand encoded into the opcode
634 // Portions of the ModR/M byte
636 // These fields determine the allowable values for the ModR/M fields, which
637 // depend on operand and address widths.
641 // The Mod and R/M fields can encode a base for an effective address, or a
642 // register. These are separated into two fields here.
644 EADisplacement eaDisplacement
;
645 // The reg field always encodes a register
649 SIBIndex sibIndexBase
;
654 // Embedded rounding control.
657 ArrayRef
<OperandSpecifier
> operands
;
660 /// Decode one instruction and store the decoding results in
661 /// a buffer provided by the consumer.
662 /// \param insn The buffer to store the instruction in. Allocated by the
664 /// \param reader The byteReader_t for the bytes to be read.
665 /// \param readerArg An argument to pass to the reader for storing context
666 /// specific to the consumer. May be NULL.
667 /// \param logger The dlog_t to be used in printing status messages from the
668 /// disassembler. May be NULL.
669 /// \param loggerArg An argument to pass to the logger for storing context
670 /// specific to the logger. May be NULL.
671 /// \param startLoc The address (in the reader's address space) of the first
672 /// byte in the instruction.
673 /// \param mode The mode (16-bit, 32-bit, 64-bit) to decode in.
674 /// \return Nonzero if there was an error during decode, 0 otherwise.
675 int decodeInstruction(InternalInstruction
*insn
,
677 const void *readerArg
,
682 DisassemblerMode mode
);
684 /// Print a message to debugs()
685 /// \param file The name of the file printing the debug message.
686 /// \param line The line number that printed the debug message.
687 /// \param s The message to print.
688 void Debug(const char *file
, unsigned line
, const char *s
);
690 StringRef
GetInstrName(unsigned Opcode
, const void *mii
);
692 } // namespace X86Disassembler