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.
316 #define ENTRY(x) SIB_INDEX_##x,
323 * SIBBase - All possible values of the SIB base field.
327 #define ENTRY(x) SIB_BASE_##x,
334 * EADisplacement - Possible displacement types for effective-address
345 * Reg - All possible values of the reg field in the ModR/M byte.
348 #define ENTRY(x) MODRM_REG_##x,
355 * SegmentOverride - All possible segment overrides.
369 * VEXLeadingOpcodeByte - Possible values for the VEX.m-mmmm field
376 } VEXLeadingOpcodeByte;
379 * VEXPrefixCode - Possible values for the VEX.pp field
383 VEX_PREFIX_NONE = 0x0,
389 typedef uint8_t BOOL;
392 * byteReader_t - Type for the byte reader that the consumer must provide to
393 * the decoder. Reads a single byte from the instruction's address space.
394 * @param arg - A baton that the consumer can associate with any internal
395 * state that it needs.
396 * @param byte - A pointer to a single byte in memory that should be set to
397 * contain the value at address.
398 * @param address - The address in the instruction's address space that should
400 * @return - -1 if the byte cannot be read for any reason; 0 otherwise.
402 typedef int (*byteReader_t)(void* arg, uint8_t* byte, uint64_t address);
405 * dlog_t - Type for the logging function that the consumer can provide to
406 * get debugging output from the decoder.
407 * @param arg - A baton that the consumer can associate with any internal
408 * state that it needs.
409 * @param log - A string that contains the message. Will be reused after
410 * the logger returns.
412 typedef void (*dlog_t)(void* arg, const char *log);
415 * The x86 internal instruction, which is produced by the decoder.
417 struct InternalInstruction {
418 /* Reader interface (C) */
420 /* Opaque value passed to the reader */
422 /* The address of the next byte to read via the reader */
423 uint64_t readerCursor;
425 /* Logger interface (C) */
427 /* Opaque value passed to the logger */
430 /* General instruction information */
432 /* The mode to disassemble for (64-bit, protected, real) */
433 DisassemblerMode mode;
434 /* The start of the instruction, usable with the reader */
435 uint64_t startLocation;
436 /* The length of the instruction, in bytes */
441 /* 1 if the prefix byte corresponding to the entry is present; 0 if not */
442 uint8_t prefixPresent[0x100];
443 /* contains the location (for use with the reader) of the prefix byte */
444 uint64_t prefixLocations[0x100];
445 /* The value of the VEX prefix, if present */
446 uint8_t vexPrefix[3];
447 /* The length of the VEX prefix (0 if not present) */
449 /* The value of the REX prefix, if present */
451 /* The location where a mandatory prefix would have to be (i.e., right before
452 the opcode, or right before the REX prefix if one is present) */
453 uint64_t necessaryPrefixLocation;
454 /* The segment override type */
455 SegmentOverride segmentOverride;
457 /* Sizes of various critical pieces of data, in bytes */
458 uint8_t registerSize;
460 uint8_t displacementSize;
461 uint8_t immediateSize;
465 /* The value of the two-byte escape prefix (usually 0x0f) */
466 uint8_t twoByteEscape;
467 /* The value of the three-byte escape prefix (usually 0x38 or 0x3a) */
468 uint8_t threeByteEscape;
469 /* The last byte of the opcode, not counting any ModR/M extension */
471 /* The ModR/M byte of the instruction, if it is an opcode extension */
472 uint8_t modRMExtension;
476 /* The type of opcode, used for indexing into the array of decode tables */
477 OpcodeType opcodeType;
478 /* The instruction ID, extracted from the decode table */
479 uint16_t instructionID;
480 /* The specifier for the instruction, from the instruction info table */
481 const struct InstructionSpecifier *spec;
483 /* state for additional bytes, consumed during operand decode. Pattern:
484 consumed___ indicates that the byte was already consumed and does not
485 need to be consumed again */
487 /* The VEX.vvvv field, which contains a third register operand for some AVX
491 /* The ModR/M byte, which contains most register operands and some portion of
492 all memory operands */
496 /* The SIB byte, used for more complex 32- or 64-bit memory operands */
500 /* The displacement, used for memory operands */
501 BOOL consumedDisplacement;
502 int32_t displacement;
504 /* Immediates. There can be two in some cases */
505 uint8_t numImmediatesConsumed;
506 uint8_t numImmediatesTranslated;
507 uint64_t immediates[2];
509 /* A register or immediate operand encoded into the opcode */
510 BOOL consumedOpcodeModifier;
511 uint8_t opcodeModifier;
514 /* Portions of the ModR/M byte */
516 /* These fields determine the allowable values for the ModR/M fields, which
517 depend on operand and address widths */
522 /* The Mod and R/M fields can encode a base for an effective address, or a
523 register. These are separated into two fields here */
525 EADisplacement eaDisplacement;
526 /* The reg field always encodes a register */
535 /* decodeInstruction - Decode one instruction and store the decoding results in
536 * a buffer provided by the consumer.
537 * @param insn - The buffer to store the instruction in. Allocated by the
539 * @param reader - The byteReader_t for the bytes to be read.
540 * @param readerArg - An argument to pass to the reader for storing context
541 * specific to the consumer. May be NULL.
542 * @param logger - The dlog_t to be used in printing status messages from the
543 * disassembler. May be NULL.
544 * @param loggerArg - An argument to pass to the logger for storing context
545 * specific to the logger. May be NULL.
546 * @param startLoc - The address (in the reader's address space) of the first
547 * byte in the instruction.
548 * @param mode - The mode (16-bit, 32-bit, 64-bit) to decode in.
549 * @return - Nonzero if there was an error during decode, 0 otherwise.
551 int decodeInstruction(struct InternalInstruction* insn,
558 DisassemblerMode mode);
560 /* x86DisassemblerDebug - C-accessible function for printing a message to
562 * @param file - The name of the file printing the debug message.
563 * @param line - The line number that printed the debug message.
564 * @param s - The message to print.
567 void x86DisassemblerDebug(const char *file,
571 const char *x86DisassemblerGetInstrName(unsigned Opcode, void *mii);