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
25 #define INSTRUCTION_IDS \
26 unsigned instructionIDs;
28 #include "X86DisassemblerDecoderCommon.h"
30 #undef INSTRUCTION_SPECIFIER_FIELDS
31 #undef INSTRUCTION_IDS
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 rFromVEX2of3(vex) (((~(vex)) & 0x80) >> 7)
48 #define xFromVEX2of3(vex) (((~(vex)) & 0x40) >> 6)
49 #define bFromVEX2of3(vex) (((~(vex)) & 0x20) >> 5)
50 #define mmmmmFromVEX2of3(vex) ((vex) & 0x1f)
51 #define wFromVEX3of3(vex) (((vex) & 0x80) >> 7)
52 #define vvvvFromVEX3of3(vex) (((~(vex)) & 0x78) >> 3)
53 #define lFromVEX3of3(vex) (((vex) & 0x4) >> 2)
54 #define ppFromVEX3of3(vex) ((vex) & 0x3)
56 #define rFromVEX2of2(vex) (((~(vex)) & 0x80) >> 7)
57 #define vvvvFromVEX2of2(vex) (((~(vex)) & 0x78) >> 3)
58 #define lFromVEX2of2(vex) (((vex) & 0x4) >> 2)
59 #define ppFromVEX2of2(vex) ((vex) & 0x3)
62 * These enums represent Intel registers for use by the decoder.
87 #define EA_BASES_16BIT \
123 #define EA_BASES_32BIT \
159 #define EA_BASES_64BIT \
241 #define REGS_SEGMENT \
259 #define REGS_CONTROL \
270 #define ALL_EA_BASES \
275 #define ALL_SIB_BASES \
293 * EABase - All possible values of the base field for effective-address
294 * computations, a.k.a. the Mod and R/M fields of the ModR/M byte. We
295 * distinguish between bases (EA_BASE_*) and registers that just happen to be
296 * referred to when Mod == 0b11 (EA_REG_*).
300 #define ENTRY(x) EA_BASE_##x,
303 #define ENTRY(x) EA_REG_##x,
310 * SIBIndex - All possible values of the SIB index field.
311 * Borrows entries from ALL_EA_BASES with the special case that
312 * sib is synonymous with NONE.
313 * Vector SIB: index can be XMM or YMM.
317 #define ENTRY(x) SIB_INDEX_##x,
326 * SIBBase - All possible values of the SIB base field.
330 #define ENTRY(x) SIB_BASE_##x,
337 * EADisplacement - Possible displacement types for effective-address
348 * Reg - All possible values of the reg field in the ModR/M byte.
351 #define ENTRY(x) MODRM_REG_##x,
358 * SegmentOverride - All possible segment overrides.
372 * VEXLeadingOpcodeByte - Possible values for the VEX.m-mmmm field
379 } VEXLeadingOpcodeByte;
382 * VEXPrefixCode - Possible values for the VEX.pp field
386 VEX_PREFIX_NONE = 0x0,
392 typedef uint8_t BOOL;
395 * byteReader_t - Type for the byte reader that the consumer must provide to
396 * the decoder. Reads a single byte from the instruction's address space.
397 * @param arg - A baton that the consumer can associate with any internal
398 * state that it needs.
399 * @param byte - A pointer to a single byte in memory that should be set to
400 * contain the value at address.
401 * @param address - The address in the instruction's address space that should
403 * @return - -1 if the byte cannot be read for any reason; 0 otherwise.
405 typedef int (*byteReader_t)(void* arg, uint8_t* byte, uint64_t address);
408 * dlog_t - Type for the logging function that the consumer can provide to
409 * get debugging output from the decoder.
410 * @param arg - A baton that the consumer can associate with any internal
411 * state that it needs.
412 * @param log - A string that contains the message. Will be reused after
413 * the logger returns.
415 typedef void (*dlog_t)(void* arg, const char *log);
418 * The x86 internal instruction, which is produced by the decoder.
420 struct InternalInstruction {
421 /* Reader interface (C) */
423 /* Opaque value passed to the reader */
425 /* The address of the next byte to read via the reader */
426 uint64_t readerCursor;
428 /* Logger interface (C) */
430 /* Opaque value passed to the logger */
433 /* General instruction information */
435 /* The mode to disassemble for (64-bit, protected, real) */
436 DisassemblerMode mode;
437 /* The start of the instruction, usable with the reader */
438 uint64_t startLocation;
439 /* The length of the instruction, in bytes */
444 /* 1 if the prefix byte corresponding to the entry is present; 0 if not */
445 uint8_t prefixPresent[0x100];
446 /* contains the location (for use with the reader) of the prefix byte */
447 uint64_t prefixLocations[0x100];
448 /* The value of the VEX prefix, if present */
449 uint8_t vexPrefix[3];
450 /* The length of the VEX prefix (0 if not present) */
452 /* The value of the REX prefix, if present */
454 /* The location where a mandatory prefix would have to be (i.e., right before
455 the opcode, or right before the REX prefix if one is present) */
456 uint64_t necessaryPrefixLocation;
457 /* The segment override type */
458 SegmentOverride segmentOverride;
460 /* Sizes of various critical pieces of data, in bytes */
461 uint8_t registerSize;
463 uint8_t displacementSize;
464 uint8_t immediateSize;
466 /* Offsets from the start of the instruction to the pieces of data, which is
467 needed to find relocation entries for adding symbolic operands */
468 uint8_t displacementOffset;
469 uint8_t immediateOffset;
473 /* The value of the two-byte escape prefix (usually 0x0f) */
474 uint8_t twoByteEscape;
475 /* The value of the three-byte escape prefix (usually 0x38 or 0x3a) */
476 uint8_t threeByteEscape;
477 /* The last byte of the opcode, not counting any ModR/M extension */
479 /* The ModR/M byte of the instruction, if it is an opcode extension */
480 uint8_t modRMExtension;
484 /* The type of opcode, used for indexing into the array of decode tables */
485 OpcodeType opcodeType;
486 /* The instruction ID, extracted from the decode table */
487 uint16_t instructionID;
488 /* The specifier for the instruction, from the instruction info table */
489 const struct InstructionSpecifier *spec;
491 /* state for additional bytes, consumed during operand decode. Pattern:
492 consumed___ indicates that the byte was already consumed and does not
493 need to be consumed again */
495 /* The VEX.vvvv field, which contains a third register operand for some AVX
499 /* The ModR/M byte, which contains most register operands and some portion of
500 all memory operands */
504 /* The SIB byte, used for more complex 32- or 64-bit memory operands */
508 /* The displacement, used for memory operands */
509 BOOL consumedDisplacement;
510 int32_t displacement;
512 /* Immediates. There can be two in some cases */
513 uint8_t numImmediatesConsumed;
514 uint8_t numImmediatesTranslated;
515 uint64_t immediates[2];
517 /* A register or immediate operand encoded into the opcode */
518 BOOL consumedOpcodeModifier;
519 uint8_t opcodeModifier;
522 /* Portions of the ModR/M byte */
524 /* These fields determine the allowable values for the ModR/M fields, which
525 depend on operand and address widths */
530 /* The Mod and R/M fields can encode a base for an effective address, or a
531 register. These are separated into two fields here */
533 EADisplacement eaDisplacement;
534 /* The reg field always encodes a register */
543 /* decodeInstruction - Decode one instruction and store the decoding results in
544 * a buffer provided by the consumer.
545 * @param insn - The buffer to store the instruction in. Allocated by the
547 * @param reader - The byteReader_t for the bytes to be read.
548 * @param readerArg - An argument to pass to the reader for storing context
549 * specific to the consumer. May be NULL.
550 * @param logger - The dlog_t to be used in printing status messages from the
551 * disassembler. May be NULL.
552 * @param loggerArg - An argument to pass to the logger for storing context
553 * specific to the logger. May be NULL.
554 * @param startLoc - The address (in the reader's address space) of the first
555 * byte in the instruction.
556 * @param mode - The mode (16-bit, 32-bit, 64-bit) to decode in.
557 * @return - Nonzero if there was an error during decode, 0 otherwise.
559 int decodeInstruction(struct InternalInstruction* insn,
566 DisassemblerMode mode);
568 /* x86DisassemblerDebug - C-accessible function for printing a message to
570 * @param file - The name of the file printing the debug message.
571 * @param line - The line number that printed the debug message.
572 * @param s - The message to print.
575 void x86DisassemblerDebug(const char *file,
579 const char *x86DisassemblerGetInstrName(unsigned Opcode, void *mii);