94 #define DEBUG_TYPE "x86-disassembler" 96 #define debug(s) LLVM_DEBUG(dbgs() << __LINE__ << ": " << s); 121 #include "X86GenDisassemblerTables.inc" 124 uint8_t opcode, uint8_t modRM) {
129 dec = &
ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
132 dec = &
TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
135 dec = &
THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
138 dec = &
THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
141 dec = &
XOP8_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
144 dec = &
XOP9_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
147 dec = &
XOPA_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
167 return modRMTable[dec->
instructionIDs + ((modRM & 0x38) >> 3) + 8];
169 case MODRM_SPLITMISC:
182 byte = insn->
bytes[offset];
187 auto r = insn->
bytes;
189 if (offset +
sizeof(
T) > r.size())
192 for (
unsigned i = 0; i <
sizeof(
T); ++i)
193 ret |= (uint64_t)r[offset + i] << (i * 8);
200 return insn->
mode ==
MODE_64BIT && prefix >= 0x40 && prefix <= 0x4f;
226 if ((byte == 0xf2 || byte == 0xf3) && !
peek(insn, nextByte)) {
232 if (((nextByte == 0xf0) ||
233 ((nextByte & 0xfe) == 0x86 || (nextByte & 0xf8) == 0x90))) {
235 if (!(byte == 0xf3 && nextByte == 0x90))
242 if (byte == 0xf3 && (nextByte == 0x88 || nextByte == 0x89 ||
243 nextByte == 0xc6 || nextByte == 0xc7)) {
247 if (
isREX(insn, nextByte)) {
253 if (
peek(insn, nnextByte))
266 if (
peek(insn, nextByte))
274 if (
isREX(insn, nextByte) || nextByte == 0x0f || nextByte == 0x66)
301 if (
peek(insn, nextByte))
323 uint8_t byte1, byte2;
329 if (
peek(insn, byte2)) {
335 ((~byte1 & 0xc) == 0xc) && ((byte2 & 0x4) == 0x4)) {
365 "Found EVEX prefix 0x%hhx 0x%hhx 0x%hhx 0x%hhx",
369 }
else if (byte == 0xc4) {
371 if (
peek(insn, byte1)) {
400 }
else if (byte == 0xc5) {
402 if (
peek(insn, byte1)) {
432 }
else if (byte == 0x8f) {
434 if (
peek(insn, byte1)) {
439 if ((byte1 & 0x38) != 0x0)
471 }
else if (
isREX(insn, byte)) {
472 if (
peek(insn, nextByte))
518 sibBaseBase = SIB_BASE_EAX;
522 sibBaseBase = SIB_BASE_RAX;
601 uint8_t mod, rm, reg, evexrm;
646 EABase eaBaseBase = EA_BASE_BX_SI;
715 insn->
eaBase = EA_BASE_sib;
738 #define GENERIC_FIXUP_FUNC(name, base, prefix, mask) \ 739 static uint16_t name(struct InternalInstruction *insn, OperandType type, \ 740 uint8_t index, uint8_t *valid) { \ 744 debug("Unhandled register type"); \ 748 return base + index; \ 753 if (insn->rexPrefix && index >= 4 && index <= 7) { \ 754 return prefix##_SPL + (index - 4); \ 756 return prefix##_AL + index; \ 762 return prefix##_AX + index; \ 767 return prefix##_EAX + index; \ 772 return prefix##_RAX + index; \ 774 return prefix##_ZMM0 + index; \ 776 return prefix##_YMM0 + index; \ 778 return prefix##_XMM0 + index; \ 783 return prefix##_K0 + index; \ 787 return prefix##_K0_K1 + (index / 2); \ 789 return prefix##_MM0 + (index & 0x7); \ 790 case TYPE_SEGMENTREG: \ 791 if ((index & 7) > 5) \ 793 return prefix##_ES + (index & 7); \ 794 case TYPE_DEBUGREG: \ 795 return prefix##_DR0 + index; \ 796 case TYPE_CONTROLREG: \ 797 return prefix##_CR0 + index; \ 801 return prefix##_BND0 + index; \ 803 return prefix##_XMM0 + index; \ 805 return prefix##_YMM0 + index; \ 807 return prefix##_ZMM0 + index; \ 838 debug(
"Expected a REG or R/M encoding in fixupReg");
842 (
Reg)fixupRegValue(insn, (
OperandType)op->type, insn->vvvv, &valid);
848 insn->reg - insn->regBase, &valid);
853 if (insn->eaBase >= insn->eaRegBase) {
854 insn->eaBase = (
EABase)fixupRMValue(
855 insn, (
OperandType)op->type, insn->eaBase - insn->eaRegBase, &valid);
876 dbgs() <<
format(
"Unhandled mm field for instruction (0x%hhx)",
893 dbgs() <<
format(
"Unhandled m-mmmm field for instruction (0x%hhx)",
913 dbgs() <<
format(
"Unhandled m-mmmm field for instruction (0x%hhx)",
931 if (current == 0x0f) {
933 dbgs() <<
format(
"Found a two-byte escape prefix (0x%hhx)", current));
937 if (current == 0x38) {
944 }
else if (current == 0x3a) {
951 }
else if (current == 0x0f) {
953 dbgs() <<
format(
"Found a 3dnow escape prefix (0x%hhx)", current));
981 for (
int i = 0;; i++) {
982 if (orig[i] ==
'\0' && equiv[i] ==
'\0')
984 if (orig[i] ==
'\0' || equiv[i] ==
'\0')
986 if (orig[i] != equiv[i]) {
987 if ((orig[i] ==
'Q' || orig[i] ==
'L') && equiv[i] ==
'W')
989 if ((orig[i] ==
'6' || orig[i] ==
'3') && equiv[i] ==
'1')
991 if ((orig[i] ==
'4' || orig[i] ==
'2') && equiv[i] ==
'6')
1000 for (
int i = 0;; ++i) {
1001 if (name[i] ==
'\0')
1003 if (name[i] ==
'6' && name[i + 1] ==
'4')
1226 auto SpecName = mii->
getName(instructionIDWithREXW);
1228 if (!
is64Bit(SpecName.data())) {
1289 specName = mii->
getName(instructionID);
1290 specWithOpSizeName = mii->
getName(instructionIDWithOpsize);
1308 uint16_t instructionIDWithNewOpcode;
1331 insn->
spec = specWithNewOpcode;
1373 (
Reg)(MODRM_REG_EAX +
1378 (
Reg)(MODRM_REG_RAX +
1456 insn->
vvvv =
static_cast<Reg>(vvvv);
1478 int hasVVVV, needVVVV;
1485 needVVVV = hasVVVV && (insn->
vvvv != 0);
1488 switch (
Op.encoding) {
1496 needVVVV = hasVVVV & ((insn->
vvvv & 0xf) != 0);
1501 if (insn->
eaBase != EA_BASE_sib && insn->
eaBase != EA_BASE_sib64)
1516 debug(
"Unhandled VSIB index type");
1557 if (
Op.type == TYPE_XMM ||
Op.type == TYPE_YMM)
1618 case ENCODING_WRITEMASK:
1625 LLVM_DEBUG(
dbgs() <<
"Encountered an operand with an unknown encoding.");
1665 std::unique_ptr<const MCInstrInfo> MII;
1668 std::unique_ptr<const MCInstrInfo> MII);
1680 X86GenericDisassembler::X86GenericDisassembler(
1683 std::unique_ptr<const MCInstrInfo> MII)
1686 if (FB[X86::Mode16Bit]) {
1689 }
else if (FB[X86::Mode32Bit]) {
1692 }
else if (FB[X86::Mode64Bit]) {
1703 CommentStream = &CStream;
1757 #define ENTRY(x) X86::x, 1761 MCPhysReg llvmRegnum = llvmRegnums[reg];
1785 uint64_t Address, uint64_t
Offset,
1800 const void *Decoder) {
1876 if (type == TYPE_REL) {
1888 if(immediate & 0x80)
1889 immediate |= ~(0xffull);
1892 if(immediate & 0x8000)
1893 immediate |= ~(0xffffull);
1896 if(immediate & 0x80000000)
1897 immediate |= ~(0xffffffffull);
1904 if(immediate & 0x80)
1905 immediate |= ~(0xffull);
1908 if(immediate & 0x8000)
1909 immediate |= ~(0xffffull);
1912 if(immediate & 0x80000000)
1913 immediate |= ~(0xffffffffull);
1918 else if (type == TYPE_IMM) {
1923 if(immediate & 0x80)
1924 immediate |= ~(0xffull);
1927 if(immediate & 0x8000)
1928 immediate |= ~(0xffffull);
1931 if(immediate & 0x80000000)
1932 immediate |= ~(0xffffffffull);
1959 if (type == TYPE_MOFFS) {
1974 if (insn.
eaBase == EA_BASE_sib || insn.
eaBase == EA_BASE_sib64) {
1975 debug(
"A R/M register operand may not have a SIB byte");
1981 debug(
"Unexpected EA base register");
1984 debug(
"EA_BASE_NONE for ModR/M base");
1986 #define ENTRY(x) case EA_BASE_##x: 1989 debug(
"A R/M register operand may not have a base; " 1990 "the operand must be a register.");
1994 mcInst.addOperand(MCOperand::createReg(X86::x)); break; 2031 if (insn.
eaBase == EA_BASE_sib || insn.
eaBase == EA_BASE_sib64) {
2035 debug(
"Unexpected sibBase");
2038 case SIB_BASE_##x: \ 2039 baseReg = MCOperand::createReg(X86::x); break; 2050 debug(
"Unexpected sibIndex");
2053 case SIB_INDEX_##x: \ 2054 indexReg = MCOperand::createReg(X86::x); break; 2074 insn.
sibBase != SIB_BASE_R12D && insn.
sibBase != SIB_BASE_R12)) {
2086 debug(
"EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
2124 debug(
"Unexpected eaBase");
2132 baseReg = MCOperand::createReg(X86::x); break; 2135 #define ENTRY(x) case EA_REG_##x: 2138 debug(
"A R/M memory operand may not be a register; " 2139 "the base field must be a base.");
2172 switch (operand.
type) {
2174 debug(
"Unexpected type for a R/M operand");
2188 case TYPE_CONTROLREG:
2216 uint8_t maskRegNum) {
2217 if (maskRegNum >= 8) {
2218 debug(
"Invalid mask register number");
2238 debug(
"Unhandled operand encoding during translation");
2243 case ENCODING_WRITEMASK:
2299 debug(
"Instruction has no specification");
2309 if(mcInst.
getOpcode() == X86::REP_PREFIX)
2311 else if(mcInst.
getOpcode() == X86::REPNE_PREFIX)
2318 if (
Op.encoding != ENCODING_NONE) {
2332 return new X86GenericDisassembler(STI, Ctx, std::move(MII));
#define bFromEVEX4of4(evex)
static int readPrefixes(struct InternalInstruction *insn)
static int readImmediate(struct InternalInstruction *insn, uint8_t size)
VectorExtensionType vectorExtensionType
static int readOpcodeRegister(struct InternalInstruction *insn, uint8_t size)
#define wFromEVEX3of4(evex)
This class represents lattice values for constants.
The specification for how to extract and interpret a full instruction and its operands.
#define bFromVEX2of3(vex)
DecodeStatus
Ternary decode status.
Superclass for all disassemblers.
static bool consume(InternalInstruction *insn, T &ptr)
#define rmFromModRM(modRM)
bool tryAddingSymbolicOperand(MCInst &Inst, int64_t Value, uint64_t Address, bool IsBranch, uint64_t Offset, uint64_t InstSize) const
static bool is16BitEquivalent(const char *orig, const char *equiv)
#define zFromEVEX4of4(evex)
MCInstrInfo * createMCInstrInfo() const
createMCInstrInfo - Create a MCInstrInfo implementation.
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
static void translateImmediate(MCInst &mcInst, uint64_t immediate, const OperandSpecifier &operand, InternalInstruction &insn, const MCDisassembler *Dis)
translateImmediate - Appends an immediate operand to an MCInst.
static int readSIB(struct InternalInstruction *insn)
#define vvvvFromVEX2of2(vex)
static void RegisterMCDisassembler(Target &T, Target::MCDisassemblerCtorTy Fn)
RegisterMCDisassembler - Register a MCDisassembler implementation for the given target.
#define vvvvFromEVEX3of4(evex)
#define r2FromEVEX2of4(evex)
static int readOperands(struct InternalInstruction *insn)
#define aaaFromEVEX4of4(evex)
#define bFromEVEX2of4(evex)
SIBIndex
All possible values of the SIB index field.
static MCOperand createReg(unsigned Reg)
#define rFromEVEX2of4(evex)
const FeatureBitset & getFeatureBits() const
#define xFromEVEX2of4(evex)
static int readMaskRegister(struct InternalInstruction *insn)
static InstrUID decode(OpcodeType type, InstructionContext insnContext, uint8_t opcode, uint8_t modRM)
#define rFromVEX2of2(vex)
Reg
All possible values of the reg field in the ModR/M byte.
#define lFromVEX2of2(vex)
static bool translateInstruction(MCInst &target, InternalInstruction &source, const MCDisassembler *Dis)
translateInstruction - Translates an internal instruction and all its operands to an MCInst...
static void translateFPRegister(MCInst &mcInst, uint8_t stackPos)
translateFPRegister - Translates a stack position on the FPU stack to its LLVM form, and appends it to an MCInst.
Context object for machine code objects.
#define xFromXOP2of3(xop)
static void translateRegister(MCInst &mcInst, Reg reg)
translateRegister - Translates an internal register to the appropriate LLVM register, and appends it as an operand to an MCInst.
#define v2FromEVEX4of4(evex)
uint8_t numImmediatesTranslated
SegmentOverride segmentOverride
static void tryAddingPcLoadReferenceComment(uint64_t Address, uint64_t Value, const void *Decoder)
tryAddingPcLoadReferenceComment - trys to add a comment as to what is being referenced by a load inst...
#define THREEDNOW_MAP_SYM
static bool readOpcode(struct InternalInstruction *insn)
static bool translateOperand(MCInst &mcInst, const OperandSpecifier &operand, InternalInstruction &insn, const MCDisassembler *Dis)
translateOperand - Translates an operand stored in an internal instruction to LLVM's format and appen...
static bool isREX(struct InternalInstruction *insn, uint8_t prefix)
ModRMDecision modRMDecisions[256]
static int nextByte(ArrayRef< uint8_t > Bytes, uint64_t &Size)
#define ppFromVEX3of3(vex)
#define mmmmmFromXOP2of3(xop)
Instances of this class represent a single low-level machine instruction.
EABase
All possible values of the base field for effective-address computations, a.k.a.
#define vvvvFromVEX3of3(vex)
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
The specification for how to extract and interpret one operand.
void tryAddingPcLoadReferenceComment(int64_t Value, uint64_t Address) const
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
static bool translateSrcIndex(MCInst &mcInst, InternalInstruction &insn)
translateSrcIndex - Appends a source index operand to an MCInst.
Container class for subtarget features.
size_t size() const
size - Get the array size.
static int getInstructionID(struct InternalInstruction *insn, const MCInstrInfo *mii)
Interface to description of machine instruction set.
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeX86Disassembler()
static int readModRM(struct InternalInstruction *insn)
#define bFromXOP2of3(xop)
static int fixupReg(struct InternalInstruction *insn, const struct OperandSpecifier *op)
static const uint8_t segmentRegnums[SEG_OVERRIDE_max]
#define lFromVEX3of3(vex)
#define ppFromVEX2of2(vex)
The x86 internal instruction, which is produced by the decoder.
StringRef getName(unsigned Opcode) const
Returns the name for the instructions with the given opcode.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
#define lFromXOP3of3(xop)
EADisplacement eaDisplacement
void setFlags(unsigned F)
#define mmmmmFromVEX2of3(vex)
void setOpcode(unsigned Op)
#define ppFromEVEX3of4(evex)
#define scaleFromSIB(sib)
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
static bool translateRM(MCInst &mcInst, const OperandSpecifier &operand, InternalInstruction &insn, const MCDisassembler *Dis)
translateRM - Translates an operand stored in the R/M (and possibly SIB) byte of an instruction to LL...
ArrayRef< OperandSpecifier > operands
const InstructionSpecifier * spec
#define xFromVEX2of3(vex)
static bool translateRMMemory(MCInst &mcInst, InternalInstruction &insn, const MCDisassembler *Dis)
translateRMMemory - Translates a memory operand stored in the Mod and R/M fields of an internal instr...
#define rFromXOP2of3(xop)
llvm::ArrayRef< uint8_t > bytes
#define LLVM_EXTERNAL_VISIBILITY
#define CASE_ENCODING_VSIB
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Target - Wrapper for Target specific information.
#define lFromEVEX4of4(evex)
#define l2FromEVEX4of4(evex)
static int getInstructionIDWithAttrMask(uint16_t *instructionID, struct InternalInstruction *insn, uint16_t attrMask)
#define wFromVEX3of3(vex)
static int readVVVV(struct InternalInstruction *insn)
#define wFromXOP3of3(xop)
uint8_t displacementOffset
static bool translateDstIndex(MCInst &mcInst, InternalInstruction &insn)
translateDstIndex - Appends a destination index operand to an MCInst.
#define indexFromSIB(sib)
static bool translateRMRegister(MCInst &mcInst, InternalInstruction &insn)
translateRMRegister - Translates a register stored in the R/M field of the ModR/M byte to its LLVM eq...
SIBBase
All possible values of the SIB base field.
uint8_t numImmediatesConsumed
static MCDisassembler * createX86Disassembler(const Target &T, const MCSubtargetInfo &STI, MCContext &Ctx)
#define mmFromEVEX2of4(evex)
Generic base class for all target subtargets.
Target & getTheX86_32Target()
#define rFromVEX2of3(vex)
#define regFromModRM(modRM)
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
#define modFromModRM(modRM)
OpcodeDecision opcodeDecisions[IC_max]
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
uint8_t vectorExtensionPrefix[4]
static bool isBranch(unsigned Opcode)
LLVM Value Representation.
#define GENERIC_FIXUP_FUNC(name, base, prefix, mask)
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
This class implements an extremely fast bulk output stream that can only output to a stream...
void addOperand(const MCOperand &Op)
StringRef - Represent a constant reference to a string, i.e.
#define vvvvFromXOP3of3(vex)
static bool tryAddingSymbolicOperand(int64_t Value, bool isBranch, uint64_t Address, uint64_t Offset, uint64_t Width, MCInst &MI, const MCDisassembler *Dis)
tryAddingSymbolicOperand - trys to add a symbolic operand in place of the immediate Value in the MCIn...
unsigned getOpcode() const
static int readDisplacement(struct InternalInstruction *insn)
Instances of this class represent operands of the MCInst class.
Target & getTheX86_64Target()
static MCOperand createImm(int64_t Val)
static bool is64Bit(const char *name)
#define ppFromXOP3of3(xop)
static bool peek(struct InternalInstruction *insn, uint8_t &byte)
static bool translateMaskRegister(MCInst &mcInst, uint8_t maskRegNum)
translateMaskRegister - Translates a 3-bit mask register number to LLVM form, and appends it to an MC...
bool empty() const
empty - Check if the array is empty.
DisassemblerMode
Decoding mode for the Intel disassembler.