1 //===-- X86Disassembler.cpp - Disassembler for x86 and x86_64 -------------===//
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 code to translate the data produced by the decoder into
13 // Documentation for the disassembler can be found in X86Disassembler.h.
15 //===----------------------------------------------------------------------===//
17 #include "X86Disassembler.h"
18 #include "X86DisassemblerDecoder.h"
20 #include "llvm/MC/EDInstInfo.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCDisassembler.h"
24 #include "llvm/MC/MCInst.h"
25 #include "llvm/MC/MCInstrInfo.h"
26 #include "llvm/MC/MCSubtargetInfo.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/MemoryObject.h"
29 #include "llvm/Support/TargetRegistry.h"
30 #include "llvm/Support/raw_ostream.h"
32 #define GET_REGINFO_ENUM
33 #include "X86GenRegisterInfo.inc"
34 #define GET_INSTRINFO_ENUM
35 #include "X86GenInstrInfo.inc"
36 #include "X86GenEDInfo.inc"
39 using namespace llvm::X86Disassembler;
41 void x86DisassemblerDebug(const char *file,
44 dbgs() << file << ":" << line << ": " << s;
47 const char *x86DisassemblerGetInstrName(unsigned Opcode, void *mii) {
48 const MCInstrInfo *MII = static_cast<const MCInstrInfo *>(mii);
49 return MII->getName(Opcode);
52 #define debug(s) DEBUG(x86DisassemblerDebug(__FILE__, __LINE__, s));
56 // Fill-ins to make the compiler happy. These constants are never actually
57 // assigned; they are just filler to make an automatically-generated switch
70 extern Target TheX86_32Target, TheX86_64Target;
74 static bool translateInstruction(MCInst &target,
75 InternalInstruction &source,
76 const MCDisassembler *Dis);
78 X86GenericDisassembler::X86GenericDisassembler(const MCSubtargetInfo &STI,
79 DisassemblerMode mode,
80 const MCInstrInfo *MII)
81 : MCDisassembler(STI), MII(MII), fMode(mode) {}
83 X86GenericDisassembler::~X86GenericDisassembler() {
87 const EDInstInfo *X86GenericDisassembler::getEDInfo() const {
91 /// regionReader - a callback function that wraps the readByte method from
94 /// @param arg - The generic callback parameter. In this case, this should
95 /// be a pointer to a MemoryObject.
96 /// @param byte - A pointer to the byte to be read.
97 /// @param address - The address to be read.
98 static int regionReader(void* arg, uint8_t* byte, uint64_t address) {
99 MemoryObject* region = static_cast<MemoryObject*>(arg);
100 return region->readByte(address, byte);
103 /// logger - a callback function that wraps the operator<< method from
106 /// @param arg - The generic callback parameter. This should be a pointe
107 /// to a raw_ostream.
108 /// @param log - A string to be logged. logger() adds a newline.
109 static void logger(void* arg, const char* log) {
113 raw_ostream &vStream = *(static_cast<raw_ostream*>(arg));
114 vStream << log << "\n";
118 // Public interface for the disassembler
121 MCDisassembler::DecodeStatus
122 X86GenericDisassembler::getInstruction(MCInst &instr,
124 const MemoryObject ®ion,
126 raw_ostream &vStream,
127 raw_ostream &cStream) const {
128 CommentStream = &cStream;
130 InternalInstruction internalInstr;
132 dlog_t loggerFn = logger;
133 if (&vStream == &nulls())
134 loggerFn = 0; // Disable logging completely if it's going to nulls().
136 int ret = decodeInstruction(&internalInstr,
146 size = internalInstr.readerCursor - address;
150 size = internalInstr.length;
151 return (!translateInstruction(instr, internalInstr, this)) ?
157 // Private code that translates from struct InternalInstructions to MCInsts.
160 /// translateRegister - Translates an internal register to the appropriate LLVM
161 /// register, and appends it as an operand to an MCInst.
163 /// @param mcInst - The MCInst to append to.
164 /// @param reg - The Reg to append.
165 static void translateRegister(MCInst &mcInst, Reg reg) {
166 #define ENTRY(x) X86::x,
167 uint8_t llvmRegnums[] = {
173 uint8_t llvmRegnum = llvmRegnums[reg];
174 mcInst.addOperand(MCOperand::CreateReg(llvmRegnum));
177 /// tryAddingSymbolicOperand - trys to add a symbolic operand in place of the
178 /// immediate Value in the MCInst.
180 /// @param Value - The immediate Value, has had any PC adjustment made by
182 /// @param isBranch - If the instruction is a branch instruction
183 /// @param Address - The starting address of the instruction
184 /// @param Offset - The byte offset to this immediate in the instruction
185 /// @param Width - The byte width of this immediate in the instruction
187 /// If the getOpInfo() function was set when setupForSymbolicDisassembly() was
188 /// called then that function is called to get any symbolic information for the
189 /// immediate in the instruction using the Address, Offset and Width. If that
190 /// returns non-zero then the symbolic information it returns is used to create
191 /// an MCExpr and that is added as an operand to the MCInst. If getOpInfo()
192 /// returns zero and isBranch is true then a symbol look up for immediate Value
193 /// is done and if a symbol is found an MCExpr is created with that, else
194 /// an MCExpr with the immediate Value is created. This function returns true
195 /// if it adds an operand to the MCInst and false otherwise.
196 static bool tryAddingSymbolicOperand(int64_t Value, bool isBranch,
197 uint64_t Address, uint64_t Offset,
198 uint64_t Width, MCInst &MI,
199 const MCDisassembler *Dis) {
200 LLVMOpInfoCallback getOpInfo = Dis->getLLVMOpInfoCallback();
201 struct LLVMOpInfo1 SymbolicOp;
202 memset(&SymbolicOp, '\0', sizeof(struct LLVMOpInfo1));
203 SymbolicOp.Value = Value;
204 void *DisInfo = Dis->getDisInfoBlock();
207 !getOpInfo(DisInfo, Address, Offset, Width, 1, &SymbolicOp)) {
208 // Clear SymbolicOp.Value from above and also all other fields.
209 memset(&SymbolicOp, '\0', sizeof(struct LLVMOpInfo1));
210 LLVMSymbolLookupCallback SymbolLookUp = Dis->getLLVMSymbolLookupCallback();
213 uint64_t ReferenceType;
215 ReferenceType = LLVMDisassembler_ReferenceType_In_Branch;
217 ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
218 const char *ReferenceName;
219 const char *Name = SymbolLookUp(DisInfo, Value, &ReferenceType, Address,
222 SymbolicOp.AddSymbol.Name = Name;
223 SymbolicOp.AddSymbol.Present = true;
225 // For branches always create an MCExpr so it gets printed as hex address.
227 SymbolicOp.Value = Value;
229 if(ReferenceType == LLVMDisassembler_ReferenceType_Out_SymbolStub)
230 (*Dis->CommentStream) << "symbol stub for: " << ReferenceName;
231 if (!Name && !isBranch)
235 MCContext *Ctx = Dis->getMCContext();
236 const MCExpr *Add = NULL;
237 if (SymbolicOp.AddSymbol.Present) {
238 if (SymbolicOp.AddSymbol.Name) {
239 StringRef Name(SymbolicOp.AddSymbol.Name);
240 MCSymbol *Sym = Ctx->GetOrCreateSymbol(Name);
241 Add = MCSymbolRefExpr::Create(Sym, *Ctx);
243 Add = MCConstantExpr::Create((int)SymbolicOp.AddSymbol.Value, *Ctx);
247 const MCExpr *Sub = NULL;
248 if (SymbolicOp.SubtractSymbol.Present) {
249 if (SymbolicOp.SubtractSymbol.Name) {
250 StringRef Name(SymbolicOp.SubtractSymbol.Name);
251 MCSymbol *Sym = Ctx->GetOrCreateSymbol(Name);
252 Sub = MCSymbolRefExpr::Create(Sym, *Ctx);
254 Sub = MCConstantExpr::Create((int)SymbolicOp.SubtractSymbol.Value, *Ctx);
258 const MCExpr *Off = NULL;
259 if (SymbolicOp.Value != 0)
260 Off = MCConstantExpr::Create(SymbolicOp.Value, *Ctx);
266 LHS = MCBinaryExpr::CreateSub(Add, Sub, *Ctx);
268 LHS = MCUnaryExpr::CreateMinus(Sub, *Ctx);
270 Expr = MCBinaryExpr::CreateAdd(LHS, Off, *Ctx);
275 Expr = MCBinaryExpr::CreateAdd(Add, Off, *Ctx);
282 Expr = MCConstantExpr::Create(0, *Ctx);
285 MI.addOperand(MCOperand::CreateExpr(Expr));
290 /// tryAddingPcLoadReferenceComment - trys to add a comment as to what is being
291 /// referenced by a load instruction with the base register that is the rip.
292 /// These can often be addresses in a literal pool. The Address of the
293 /// instruction and its immediate Value are used to determine the address
294 /// being referenced in the literal pool entry. The SymbolLookUp call back will
295 /// return a pointer to a literal 'C' string if the referenced address is an
296 /// address into a section with 'C' string literals.
297 static void tryAddingPcLoadReferenceComment(uint64_t Address, uint64_t Value,
298 const void *Decoder) {
299 const MCDisassembler *Dis = static_cast<const MCDisassembler*>(Decoder);
300 LLVMSymbolLookupCallback SymbolLookUp = Dis->getLLVMSymbolLookupCallback();
302 void *DisInfo = Dis->getDisInfoBlock();
303 uint64_t ReferenceType = LLVMDisassembler_ReferenceType_In_PCrel_Load;
304 const char *ReferenceName;
305 (void)SymbolLookUp(DisInfo, Value, &ReferenceType, Address, &ReferenceName);
306 if(ReferenceType == LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr)
307 (*Dis->CommentStream) << "literal pool for: " << ReferenceName;
311 /// translateImmediate - Appends an immediate operand to an MCInst.
313 /// @param mcInst - The MCInst to append to.
314 /// @param immediate - The immediate value to append.
315 /// @param operand - The operand, as stored in the descriptor table.
316 /// @param insn - The internal instruction.
317 static void translateImmediate(MCInst &mcInst, uint64_t immediate,
318 const OperandSpecifier &operand,
319 InternalInstruction &insn,
320 const MCDisassembler *Dis) {
321 // Sign-extend the immediate if necessary.
323 OperandType type = (OperandType)operand.type;
325 bool isBranch = false;
327 if (type == TYPE_RELv) {
329 pcrel = insn.startLocation +
330 insn.displacementOffset + insn.displacementSize;
331 switch (insn.displacementSize) {
348 // By default sign-extend all X86 immediates based on their encoding.
349 else if (type == TYPE_IMM8 || type == TYPE_IMM16 || type == TYPE_IMM32 ||
350 type == TYPE_IMM64) {
351 uint32_t Opcode = mcInst.getOpcode();
352 switch (operand.encoding) {
356 // Special case those X86 instructions that use the imm8 as a set of
357 // bits, bit count, etc. and are not sign-extend.
358 if (Opcode != X86::BLENDPSrri && Opcode != X86::BLENDPDrri &&
359 Opcode != X86::PBLENDWrri && Opcode != X86::MPSADBWrri &&
360 Opcode != X86::DPPSrri && Opcode != X86::DPPDrri &&
361 Opcode != X86::INSERTPSrr && Opcode != X86::VBLENDPSYrri &&
362 Opcode != X86::VBLENDPSYrmi && Opcode != X86::VBLENDPDYrri &&
363 Opcode != X86::VBLENDPDYrmi && Opcode != X86::VPBLENDWrri &&
364 Opcode != X86::VMPSADBWrri && Opcode != X86::VDPPSYrri &&
365 Opcode != X86::VDPPSYrmi && Opcode != X86::VDPPDrri &&
366 Opcode != X86::VINSERTPSrr)
383 mcInst.addOperand(MCOperand::CreateReg(X86::XMM0 + (immediate >> 4)));
386 mcInst.addOperand(MCOperand::CreateReg(X86::YMM0 + (immediate >> 4)));
390 pcrel = insn.startLocation + insn.immediateOffset + insn.immediateSize;
391 // fall through to sign extend the immediate if needed.
394 immediate |= ~(0xffull);
397 if(immediate & 0x8000)
398 immediate |= ~(0xffffull);
403 pcrel = insn.startLocation + insn.immediateOffset + insn.immediateSize;
404 // fall through to sign extend the immediate if needed.
406 if(immediate & 0x80000000)
407 immediate |= ~(0xffffffffull);
411 // operand is 64 bits wide. Do nothing.
415 if(!tryAddingSymbolicOperand(immediate + pcrel, isBranch, insn.startLocation,
416 insn.immediateOffset, insn.immediateSize,
418 mcInst.addOperand(MCOperand::CreateImm(immediate));
421 /// translateRMRegister - Translates a register stored in the R/M field of the
422 /// ModR/M byte to its LLVM equivalent and appends it to an MCInst.
423 /// @param mcInst - The MCInst to append to.
424 /// @param insn - The internal instruction to extract the R/M field
426 /// @return - 0 on success; -1 otherwise
427 static bool translateRMRegister(MCInst &mcInst,
428 InternalInstruction &insn) {
429 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
430 debug("A R/M register operand may not have a SIB byte");
434 switch (insn.eaBase) {
436 debug("Unexpected EA base register");
439 debug("EA_BASE_NONE for ModR/M base");
441 #define ENTRY(x) case EA_BASE_##x:
444 debug("A R/M register operand may not have a base; "
445 "the operand must be a register.");
449 mcInst.addOperand(MCOperand::CreateReg(X86::x)); break;
457 /// translateRMMemory - Translates a memory operand stored in the Mod and R/M
458 /// fields of an internal instruction (and possibly its SIB byte) to a memory
459 /// operand in LLVM's format, and appends it to an MCInst.
461 /// @param mcInst - The MCInst to append to.
462 /// @param insn - The instruction to extract Mod, R/M, and SIB fields
464 /// @return - 0 on success; nonzero otherwise
465 static bool translateRMMemory(MCInst &mcInst, InternalInstruction &insn,
466 const MCDisassembler *Dis) {
467 // Addresses in an MCInst are represented as five operands:
468 // 1. basereg (register) The R/M base, or (if there is a SIB) the
470 // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified
472 // 3. indexreg (register) x86_registerNONE, or (if there is a SIB)
473 // the index (which is multiplied by the
475 // 4. displacement (immediate) 0, or the displacement if there is one
476 // 5. segmentreg (register) x86_registerNONE for now, but could be set
477 // if we have segment overrides
480 MCOperand scaleAmount;
482 MCOperand displacement;
483 MCOperand segmentReg;
486 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
487 if (insn.sibBase != SIB_BASE_NONE) {
488 switch (insn.sibBase) {
490 debug("Unexpected sibBase");
494 baseReg = MCOperand::CreateReg(X86::x); break;
499 baseReg = MCOperand::CreateReg(0);
502 if (insn.sibIndex != SIB_INDEX_NONE) {
503 switch (insn.sibIndex) {
505 debug("Unexpected sibIndex");
508 case SIB_INDEX_##x: \
509 indexReg = MCOperand::CreateReg(X86::x); break;
515 indexReg = MCOperand::CreateReg(0);
518 scaleAmount = MCOperand::CreateImm(insn.sibScale);
520 switch (insn.eaBase) {
522 if (insn.eaDisplacement == EA_DISP_NONE) {
523 debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
526 if (insn.mode == MODE_64BIT){
527 pcrel = insn.startLocation +
528 insn.displacementOffset + insn.displacementSize;
529 tryAddingPcLoadReferenceComment(insn.startLocation +
530 insn.displacementOffset,
531 insn.displacement + pcrel, Dis);
532 baseReg = MCOperand::CreateReg(X86::RIP); // Section 2.2.1.6
535 baseReg = MCOperand::CreateReg(0);
537 indexReg = MCOperand::CreateReg(0);
540 baseReg = MCOperand::CreateReg(X86::BX);
541 indexReg = MCOperand::CreateReg(X86::SI);
544 baseReg = MCOperand::CreateReg(X86::BX);
545 indexReg = MCOperand::CreateReg(X86::DI);
548 baseReg = MCOperand::CreateReg(X86::BP);
549 indexReg = MCOperand::CreateReg(X86::SI);
552 baseReg = MCOperand::CreateReg(X86::BP);
553 indexReg = MCOperand::CreateReg(X86::DI);
556 indexReg = MCOperand::CreateReg(0);
557 switch (insn.eaBase) {
559 debug("Unexpected eaBase");
561 // Here, we will use the fill-ins defined above. However,
562 // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and
563 // sib and sib64 were handled in the top-level if, so they're only
564 // placeholders to keep the compiler happy.
567 baseReg = MCOperand::CreateReg(X86::x); break;
570 #define ENTRY(x) case EA_REG_##x:
573 debug("A R/M memory operand may not be a register; "
574 "the base field must be a base.");
579 scaleAmount = MCOperand::CreateImm(1);
582 displacement = MCOperand::CreateImm(insn.displacement);
584 static const uint8_t segmentRegnums[SEG_OVERRIDE_max] = {
585 0, // SEG_OVERRIDE_NONE
594 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
596 mcInst.addOperand(baseReg);
597 mcInst.addOperand(scaleAmount);
598 mcInst.addOperand(indexReg);
599 if(!tryAddingSymbolicOperand(insn.displacement + pcrel, false,
600 insn.startLocation, insn.displacementOffset,
601 insn.displacementSize, mcInst, Dis))
602 mcInst.addOperand(displacement);
603 mcInst.addOperand(segmentReg);
607 /// translateRM - Translates an operand stored in the R/M (and possibly SIB)
608 /// byte of an instruction to LLVM form, and appends it to an MCInst.
610 /// @param mcInst - The MCInst to append to.
611 /// @param operand - The operand, as stored in the descriptor table.
612 /// @param insn - The instruction to extract Mod, R/M, and SIB fields
614 /// @return - 0 on success; nonzero otherwise
615 static bool translateRM(MCInst &mcInst, const OperandSpecifier &operand,
616 InternalInstruction &insn, const MCDisassembler *Dis) {
617 switch (operand.type) {
619 debug("Unexpected type for a R/M operand");
635 case TYPE_CONTROLREG:
636 return translateRMRegister(mcInst, insn);
656 return translateRMMemory(mcInst, insn, Dis);
660 /// translateFPRegister - Translates a stack position on the FPU stack to its
661 /// LLVM form, and appends it to an MCInst.
663 /// @param mcInst - The MCInst to append to.
664 /// @param stackPos - The stack position to translate.
665 /// @return - 0 on success; nonzero otherwise.
666 static bool translateFPRegister(MCInst &mcInst,
669 debug("Invalid FP stack position");
673 mcInst.addOperand(MCOperand::CreateReg(X86::ST0 + stackPos));
678 /// translateOperand - Translates an operand stored in an internal instruction
679 /// to LLVM's format and appends it to an MCInst.
681 /// @param mcInst - The MCInst to append to.
682 /// @param operand - The operand, as stored in the descriptor table.
683 /// @param insn - The internal instruction.
684 /// @return - false on success; true otherwise.
685 static bool translateOperand(MCInst &mcInst, const OperandSpecifier &operand,
686 InternalInstruction &insn,
687 const MCDisassembler *Dis) {
688 switch (operand.encoding) {
690 debug("Unhandled operand encoding during translation");
693 translateRegister(mcInst, insn.reg);
696 return translateRM(mcInst, operand, insn, Dis);
703 debug("Translation of code offsets isn't supported.");
711 translateImmediate(mcInst,
712 insn.immediates[insn.numImmediatesTranslated++],
721 translateRegister(mcInst, insn.opcodeRegister);
724 return translateFPRegister(mcInst, insn.opcodeModifier);
726 translateRegister(mcInst, insn.opcodeRegister);
729 translateRegister(mcInst, insn.vvvv);
732 return translateOperand(mcInst,
733 insn.spec->operands[operand.type - TYPE_DUP0],
738 /// translateInstruction - Translates an internal instruction and all its
739 /// operands to an MCInst.
741 /// @param mcInst - The MCInst to populate with the instruction's data.
742 /// @param insn - The internal instruction.
743 /// @return - false on success; true otherwise.
744 static bool translateInstruction(MCInst &mcInst,
745 InternalInstruction &insn,
746 const MCDisassembler *Dis) {
748 debug("Instruction has no specification");
752 mcInst.setOpcode(insn.instructionID);
756 insn.numImmediatesTranslated = 0;
758 for (index = 0; index < X86_MAX_OPERANDS; ++index) {
759 if (insn.spec->operands[index].encoding != ENCODING_NONE) {
760 if (translateOperand(mcInst, insn.spec->operands[index], insn, Dis)) {
769 static MCDisassembler *createX86_32Disassembler(const Target &T,
770 const MCSubtargetInfo &STI) {
771 return new X86Disassembler::X86GenericDisassembler(STI, MODE_32BIT,
772 T.createMCInstrInfo());
775 static MCDisassembler *createX86_64Disassembler(const Target &T,
776 const MCSubtargetInfo &STI) {
777 return new X86Disassembler::X86GenericDisassembler(STI, MODE_64BIT,
778 T.createMCInstrInfo());
781 extern "C" void LLVMInitializeX86Disassembler() {
782 // Register the disassembler.
783 TargetRegistry::RegisterMCDisassembler(TheX86_32Target,
784 createX86_32Disassembler);
785 TargetRegistry::RegisterMCDisassembler(TheX86_64Target,
786 createX86_64Disassembler);