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
23 #define INSTRUCTION_SPECIFIER_FIELDS \
26 #define INSTRUCTION_IDS \
27 uint16_t instructionIDs;
29 #include "X86DisassemblerDecoderCommon.h"
31 #undef INSTRUCTION_SPECIFIER_FIELDS
32 #undef INSTRUCTION_IDS
35 * Accessor functions for various fields of an Intel instruction
37 #define modFromModRM(modRM) (((modRM) & 0xc0) >> 6)
38 #define regFromModRM(modRM) (((modRM) & 0x38) >> 3)
39 #define rmFromModRM(modRM) ((modRM) & 0x7)
40 #define scaleFromSIB(sib) (((sib) & 0xc0) >> 6)
41 #define indexFromSIB(sib) (((sib) & 0x38) >> 3)
42 #define baseFromSIB(sib) ((sib) & 0x7)
43 #define wFromREX(rex) (((rex) & 0x8) >> 3)
44 #define rFromREX(rex) (((rex) & 0x4) >> 2)
45 #define xFromREX(rex) (((rex) & 0x2) >> 1)
46 #define bFromREX(rex) ((rex) & 0x1)
48 #define rFromEVEX2of4(evex) (((~(evex)) & 0x80) >> 7)
49 #define xFromEVEX2of4(evex) (((~(evex)) & 0x40) >> 6)
50 #define bFromEVEX2of4(evex) (((~(evex)) & 0x20) >> 5)
51 #define r2FromEVEX2of4(evex) (((~(evex)) & 0x10) >> 4)
52 #define mmFromEVEX2of4(evex) ((evex) & 0x3)
53 #define wFromEVEX3of4(evex) (((evex) & 0x80) >> 7)
54 #define vvvvFromEVEX3of4(evex) (((~(evex)) & 0x78) >> 3)
55 #define ppFromEVEX3of4(evex) ((evex) & 0x3)
56 #define zFromEVEX4of4(evex) (((evex) & 0x80) >> 7)
57 #define l2FromEVEX4of4(evex) (((evex) & 0x40) >> 6)
58 #define lFromEVEX4of4(evex) (((evex) & 0x20) >> 5)
59 #define bFromEVEX4of4(evex) (((evex) & 0x10) >> 4)
60 #define v2FromEVEX4of4(evex) (((~evex) & 0x8) >> 3)
61 #define aaaFromEVEX4of4(evex) ((evex) & 0x7)
63 #define rFromVEX2of3(vex) (((~(vex)) & 0x80) >> 7)
64 #define xFromVEX2of3(vex) (((~(vex)) & 0x40) >> 6)
65 #define bFromVEX2of3(vex) (((~(vex)) & 0x20) >> 5)
66 #define mmmmmFromVEX2of3(vex) ((vex) & 0x1f)
67 #define wFromVEX3of3(vex) (((vex) & 0x80) >> 7)
68 #define vvvvFromVEX3of3(vex) (((~(vex)) & 0x78) >> 3)
69 #define lFromVEX3of3(vex) (((vex) & 0x4) >> 2)
70 #define ppFromVEX3of3(vex) ((vex) & 0x3)
72 #define rFromVEX2of2(vex) (((~(vex)) & 0x80) >> 7)
73 #define vvvvFromVEX2of2(vex) (((~(vex)) & 0x78) >> 3)
74 #define lFromVEX2of2(vex) (((vex) & 0x4) >> 2)
75 #define ppFromVEX2of2(vex) ((vex) & 0x3)
77 #define rFromXOP2of3(xop) (((~(xop)) & 0x80) >> 7)
78 #define xFromXOP2of3(xop) (((~(xop)) & 0x40) >> 6)
79 #define bFromXOP2of3(xop) (((~(xop)) & 0x20) >> 5)
80 #define mmmmmFromXOP2of3(xop) ((xop) & 0x1f)
81 #define wFromXOP3of3(xop) (((xop) & 0x80) >> 7)
82 #define vvvvFromXOP3of3(vex) (((~(vex)) & 0x78) >> 3)
83 #define lFromXOP3of3(xop) (((xop) & 0x4) >> 2)
84 #define ppFromXOP3of3(xop) ((xop) & 0x3)
87 * These enums represent Intel registers for use by the decoder.
112 #define EA_BASES_16BIT \
148 #define EA_BASES_32BIT \
184 #define EA_BASES_64BIT \
342 #define REGS_SEGMENT \
360 #define REGS_CONTROL \
371 #define ALL_EA_BASES \
376 #define ALL_SIB_BASES \
396 * EABase - All possible values of the base field for effective-address
397 * computations, a.k.a. the Mod and R/M fields of the ModR/M byte. We
398 * distinguish between bases (EA_BASE_*) and registers that just happen to be
399 * referred to when Mod == 0b11 (EA_REG_*).
403 #define ENTRY(x) EA_BASE_##x,
406 #define ENTRY(x) EA_REG_##x,
413 * SIBIndex - All possible values of the SIB index field.
414 * Borrows entries from ALL_EA_BASES with the special case that
415 * sib is synonymous with NONE.
416 * Vector SIB: index can be XMM or YMM.
420 #define ENTRY(x) SIB_INDEX_##x,
430 * SIBBase - All possible values of the SIB base field.
434 #define ENTRY(x) SIB_BASE_##x,
441 * EADisplacement - Possible displacement types for effective-address
452 * Reg - All possible values of the reg field in the ModR/M byte.
455 #define ENTRY(x) MODRM_REG_##x,
462 * SegmentOverride - All possible segment overrides.
476 * VEXLeadingOpcodeByte - Possible values for the VEX.m-mmmm field
483 } VEXLeadingOpcodeByte;
486 XOP_MAP_SELECT_8 = 0x8,
487 XOP_MAP_SELECT_9 = 0x9,
488 XOP_MAP_SELECT_A = 0xA
492 * VEXPrefixCode - Possible values for the VEX.pp/EVEX.pp field
496 VEX_PREFIX_NONE = 0x0,
503 TYPE_NO_VEX_XOP = 0x0,
508 } VectorExtensionType;
510 typedef uint8_t BOOL;
513 * byteReader_t - Type for the byte reader that the consumer must provide to
514 * the decoder. Reads a single byte from the instruction's address space.
515 * @param arg - A baton that the consumer can associate with any internal
516 * state that it needs.
517 * @param byte - A pointer to a single byte in memory that should be set to
518 * contain the value at address.
519 * @param address - The address in the instruction's address space that should
521 * @return - -1 if the byte cannot be read for any reason; 0 otherwise.
523 typedef int (*byteReader_t)(const void* arg, uint8_t* byte, uint64_t address);
526 * dlog_t - Type for the logging function that the consumer can provide to
527 * get debugging output from the decoder.
528 * @param arg - A baton that the consumer can associate with any internal
529 * state that it needs.
530 * @param log - A string that contains the message. Will be reused after
531 * the logger returns.
533 typedef void (*dlog_t)(void* arg, const char *log);
536 * The x86 internal instruction, which is produced by the decoder.
538 struct InternalInstruction {
539 /* Reader interface (C) */
541 /* Opaque value passed to the reader */
542 const void* readerArg;
543 /* The address of the next byte to read via the reader */
544 uint64_t readerCursor;
546 /* Logger interface (C) */
548 /* Opaque value passed to the logger */
551 /* General instruction information */
553 /* The mode to disassemble for (64-bit, protected, real) */
554 DisassemblerMode mode;
555 /* The start of the instruction, usable with the reader */
556 uint64_t startLocation;
557 /* The length of the instruction, in bytes */
562 /* 1 if the prefix byte corresponding to the entry is present; 0 if not */
563 uint8_t prefixPresent[0x100];
564 /* contains the location (for use with the reader) of the prefix byte */
565 uint64_t prefixLocations[0x100];
566 /* The value of the vector extension prefix(EVEX/VEX/XOP), if present */
567 uint8_t vectorExtensionPrefix[4];
568 /* The type of the vector extension prefix */
569 VectorExtensionType vectorExtensionType;
570 /* The value of the REX prefix, if present */
572 /* The location where a mandatory prefix would have to be (i.e., right before
573 the opcode, or right before the REX prefix if one is present) */
574 uint64_t necessaryPrefixLocation;
575 /* The segment override type */
576 SegmentOverride segmentOverride;
577 /* 1 if the prefix byte, 0xf2 or 0xf3 is xacquire or xrelease */
578 BOOL xAcquireRelease;
580 /* Sizes of various critical pieces of data, in bytes */
581 uint8_t registerSize;
583 uint8_t displacementSize;
584 uint8_t immediateSize;
586 /* Offsets from the start of the instruction to the pieces of data, which is
587 needed to find relocation entries for adding symbolic operands */
588 uint8_t displacementOffset;
589 uint8_t immediateOffset;
593 /* The last byte of the opcode, not counting any ModR/M extension */
595 /* The ModR/M byte of the instruction, if it is an opcode extension */
596 uint8_t modRMExtension;
600 /* The type of opcode, used for indexing into the array of decode tables */
601 OpcodeType opcodeType;
602 /* The instruction ID, extracted from the decode table */
603 uint16_t instructionID;
604 /* The specifier for the instruction, from the instruction info table */
605 const struct InstructionSpecifier *spec;
607 /* state for additional bytes, consumed during operand decode. Pattern:
608 consumed___ indicates that the byte was already consumed and does not
609 need to be consumed again */
611 /* The VEX.vvvv field, which contains a third register operand for some AVX
615 /* The writemask for AVX-512 instructions which is contained in EVEX.aaa */
618 /* The ModR/M byte, which contains most register operands and some portion of
619 all memory operands */
623 /* The SIB byte, used for more complex 32- or 64-bit memory operands */
627 /* The displacement, used for memory operands */
628 BOOL consumedDisplacement;
629 int32_t displacement;
631 /* Immediates. There can be two in some cases */
632 uint8_t numImmediatesConsumed;
633 uint8_t numImmediatesTranslated;
634 uint64_t immediates[2];
636 /* A register or immediate operand encoded into the opcode */
639 /* Portions of the ModR/M byte */
641 /* These fields determine the allowable values for the ModR/M fields, which
642 depend on operand and address widths */
647 /* The Mod and R/M fields can encode a base for an effective address, or a
648 register. These are separated into two fields here */
650 EADisplacement eaDisplacement;
651 /* The reg field always encodes a register */
659 const struct OperandSpecifier *operands;
662 /* decodeInstruction - Decode one instruction and store the decoding results in
663 * a buffer provided by the consumer.
664 * @param insn - The buffer to store the instruction in. Allocated by the
666 * @param reader - The byteReader_t for the bytes to be read.
667 * @param readerArg - An argument to pass to the reader for storing context
668 * specific to the consumer. May be NULL.
669 * @param logger - The dlog_t to be used in printing status messages from the
670 * disassembler. May be NULL.
671 * @param loggerArg - An argument to pass to the logger for storing context
672 * specific to the logger. May be NULL.
673 * @param startLoc - The address (in the reader's address space) of the first
674 * byte in the instruction.
675 * @param mode - The mode (16-bit, 32-bit, 64-bit) to decode in.
676 * @return - Nonzero if there was an error during decode, 0 otherwise.
678 int decodeInstruction(struct InternalInstruction* insn,
680 const void* readerArg,
685 DisassemblerMode mode);
687 /* x86DisassemblerDebug - C-accessible function for printing a message to
689 * @param file - The name of the file printing the debug message.
690 * @param line - The line number that printed the debug message.
691 * @param s - The message to print.
694 void x86DisassemblerDebug(const char *file,
698 const char *x86DisassemblerGetInstrName(unsigned Opcode, const void *mii);