1 //===- X86Disassembler.cpp - Disassembler for x86 and x86_64 ----*- 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 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/MCDisassembler.h"
22 #include "llvm/MC/MCDisassembler.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/MC/MCInstrInfo.h"
25 #include "llvm/MC/MCSubtargetInfo.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/MemoryObject.h"
28 #include "llvm/Support/TargetRegistry.h"
29 #include "llvm/Support/raw_ostream.h"
31 #define GET_REGINFO_ENUM
32 #include "X86GenRegisterInfo.inc"
33 #define GET_INSTRINFO_ENUM
34 #include "X86GenInstrInfo.inc"
35 #include "X86GenEDInfo.inc"
38 using namespace llvm::X86Disassembler;
40 void x86DisassemblerDebug(const char *file,
43 dbgs() << file << ":" << line << ": " << s;
46 const char *x86DisassemblerGetInstrName(unsigned Opcode, void *mii) {
47 const MCInstrInfo *MII = static_cast<const MCInstrInfo *>(mii);
48 return MII->getName(Opcode);
51 #define debug(s) DEBUG(x86DisassemblerDebug(__FILE__, __LINE__, s));
55 // Fill-ins to make the compiler happy. These constants are never actually
56 // assigned; they are just filler to make an automatically-generated switch
69 extern Target TheX86_32Target, TheX86_64Target;
73 static bool translateInstruction(MCInst &target,
74 InternalInstruction &source);
76 X86GenericDisassembler::X86GenericDisassembler(const MCSubtargetInfo &STI,
77 DisassemblerMode mode,
78 const MCInstrInfo *MII)
79 : MCDisassembler(STI), MII(MII), fMode(mode) {}
81 X86GenericDisassembler::~X86GenericDisassembler() {
85 const EDInstInfo *X86GenericDisassembler::getEDInfo() const {
89 /// regionReader - a callback function that wraps the readByte method from
92 /// @param arg - The generic callback parameter. In this case, this should
93 /// be a pointer to a MemoryObject.
94 /// @param byte - A pointer to the byte to be read.
95 /// @param address - The address to be read.
96 static int regionReader(void* arg, uint8_t* byte, uint64_t address) {
97 MemoryObject* region = static_cast<MemoryObject*>(arg);
98 return region->readByte(address, byte);
101 /// logger - a callback function that wraps the operator<< method from
104 /// @param arg - The generic callback parameter. This should be a pointe
105 /// to a raw_ostream.
106 /// @param log - A string to be logged. logger() adds a newline.
107 static void logger(void* arg, const char* log) {
111 raw_ostream &vStream = *(static_cast<raw_ostream*>(arg));
112 vStream << log << "\n";
116 // Public interface for the disassembler
119 MCDisassembler::DecodeStatus
120 X86GenericDisassembler::getInstruction(MCInst &instr,
122 MemoryObject ®ion,
124 raw_ostream &vStream,
125 raw_ostream &cStream) const {
126 InternalInstruction internalInstr;
128 dlog_t loggerFn = logger;
129 if (&vStream == &nulls())
130 loggerFn = 0; // Disable logging completely if it's going to nulls().
132 int ret = decodeInstruction(&internalInstr,
142 size = internalInstr.readerCursor - address;
146 size = internalInstr.length;
147 return (!translateInstruction(instr, internalInstr)) ? Success : Fail;
152 // Private code that translates from struct InternalInstructions to MCInsts.
155 /// translateRegister - Translates an internal register to the appropriate LLVM
156 /// register, and appends it as an operand to an MCInst.
158 /// @param mcInst - The MCInst to append to.
159 /// @param reg - The Reg to append.
160 static void translateRegister(MCInst &mcInst, Reg reg) {
161 #define ENTRY(x) X86::x,
162 uint8_t llvmRegnums[] = {
168 uint8_t llvmRegnum = llvmRegnums[reg];
169 mcInst.addOperand(MCOperand::CreateReg(llvmRegnum));
172 /// translateImmediate - Appends an immediate operand to an MCInst.
174 /// @param mcInst - The MCInst to append to.
175 /// @param immediate - The immediate value to append.
176 /// @param operand - The operand, as stored in the descriptor table.
177 /// @param insn - The internal instruction.
178 static void translateImmediate(MCInst &mcInst, uint64_t immediate,
179 const OperandSpecifier &operand,
180 InternalInstruction &insn) {
181 // Sign-extend the immediate if necessary.
183 OperandType type = operand.type;
185 if (type == TYPE_RELv) {
186 switch (insn.displacementSize) {
203 // By default sign-extend all X86 immediates based on their encoding.
204 else if (type == TYPE_IMM8 || type == TYPE_IMM16 || type == TYPE_IMM32 ||
205 type == TYPE_IMM64) {
206 uint32_t Opcode = mcInst.getOpcode();
207 switch (operand.encoding) {
211 // Special case those X86 instructions that use the imm8 as a set of
212 // bits, bit count, etc. and are not sign-extend.
213 if (Opcode != X86::BLENDPSrri && Opcode != X86::BLENDPDrri &&
214 Opcode != X86::PBLENDWrri && Opcode != X86::MPSADBWrri &&
215 Opcode != X86::DPPSrri && Opcode != X86::DPPDrri &&
216 Opcode != X86::INSERTPSrr && Opcode != X86::VBLENDPSYrri &&
217 Opcode != X86::VBLENDPSYrmi && Opcode != X86::VBLENDPDYrri &&
218 Opcode != X86::VBLENDPDYrmi && Opcode != X86::VPBLENDWrri &&
219 Opcode != X86::VMPSADBWrri && Opcode != X86::VDPPSYrri &&
220 Opcode != X86::VDPPSYrmi && Opcode != X86::VDPPDrri &&
221 Opcode != X86::VINSERTPSrr)
238 mcInst.addOperand(MCOperand::CreateReg(X86::XMM0 + (immediate >> 4)));
241 mcInst.addOperand(MCOperand::CreateReg(X86::YMM0 + (immediate >> 4)));
246 immediate |= ~(0xffull);
249 if(immediate & 0x8000)
250 immediate |= ~(0xffffull);
255 if(immediate & 0x80000000)
256 immediate |= ~(0xffffffffull);
260 // operand is 64 bits wide. Do nothing.
264 mcInst.addOperand(MCOperand::CreateImm(immediate));
267 /// translateRMRegister - Translates a register stored in the R/M field of the
268 /// ModR/M byte to its LLVM equivalent and appends it to an MCInst.
269 /// @param mcInst - The MCInst to append to.
270 /// @param insn - The internal instruction to extract the R/M field
272 /// @return - 0 on success; -1 otherwise
273 static bool translateRMRegister(MCInst &mcInst,
274 InternalInstruction &insn) {
275 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
276 debug("A R/M register operand may not have a SIB byte");
280 switch (insn.eaBase) {
282 debug("Unexpected EA base register");
285 debug("EA_BASE_NONE for ModR/M base");
287 #define ENTRY(x) case EA_BASE_##x:
290 debug("A R/M register operand may not have a base; "
291 "the operand must be a register.");
295 mcInst.addOperand(MCOperand::CreateReg(X86::x)); break;
303 /// translateRMMemory - Translates a memory operand stored in the Mod and R/M
304 /// fields of an internal instruction (and possibly its SIB byte) to a memory
305 /// operand in LLVM's format, and appends it to an MCInst.
307 /// @param mcInst - The MCInst to append to.
308 /// @param insn - The instruction to extract Mod, R/M, and SIB fields
310 /// @return - 0 on success; nonzero otherwise
311 static bool translateRMMemory(MCInst &mcInst, InternalInstruction &insn) {
312 // Addresses in an MCInst are represented as five operands:
313 // 1. basereg (register) The R/M base, or (if there is a SIB) the
315 // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified
317 // 3. indexreg (register) x86_registerNONE, or (if there is a SIB)
318 // the index (which is multiplied by the
320 // 4. displacement (immediate) 0, or the displacement if there is one
321 // 5. segmentreg (register) x86_registerNONE for now, but could be set
322 // if we have segment overrides
325 MCOperand scaleAmount;
327 MCOperand displacement;
328 MCOperand segmentReg;
330 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
331 if (insn.sibBase != SIB_BASE_NONE) {
332 switch (insn.sibBase) {
334 debug("Unexpected sibBase");
338 baseReg = MCOperand::CreateReg(X86::x); break;
343 baseReg = MCOperand::CreateReg(0);
346 if (insn.sibIndex != SIB_INDEX_NONE) {
347 switch (insn.sibIndex) {
349 debug("Unexpected sibIndex");
352 case SIB_INDEX_##x: \
353 indexReg = MCOperand::CreateReg(X86::x); break;
359 indexReg = MCOperand::CreateReg(0);
362 scaleAmount = MCOperand::CreateImm(insn.sibScale);
364 switch (insn.eaBase) {
366 if (insn.eaDisplacement == EA_DISP_NONE) {
367 debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
370 if (insn.mode == MODE_64BIT)
371 baseReg = MCOperand::CreateReg(X86::RIP); // Section 2.2.1.6
373 baseReg = MCOperand::CreateReg(0);
375 indexReg = MCOperand::CreateReg(0);
378 baseReg = MCOperand::CreateReg(X86::BX);
379 indexReg = MCOperand::CreateReg(X86::SI);
382 baseReg = MCOperand::CreateReg(X86::BX);
383 indexReg = MCOperand::CreateReg(X86::DI);
386 baseReg = MCOperand::CreateReg(X86::BP);
387 indexReg = MCOperand::CreateReg(X86::SI);
390 baseReg = MCOperand::CreateReg(X86::BP);
391 indexReg = MCOperand::CreateReg(X86::DI);
394 indexReg = MCOperand::CreateReg(0);
395 switch (insn.eaBase) {
397 debug("Unexpected eaBase");
399 // Here, we will use the fill-ins defined above. However,
400 // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and
401 // sib and sib64 were handled in the top-level if, so they're only
402 // placeholders to keep the compiler happy.
405 baseReg = MCOperand::CreateReg(X86::x); break;
408 #define ENTRY(x) case EA_REG_##x:
411 debug("A R/M memory operand may not be a register; "
412 "the base field must be a base.");
417 scaleAmount = MCOperand::CreateImm(1);
420 displacement = MCOperand::CreateImm(insn.displacement);
422 static const uint8_t segmentRegnums[SEG_OVERRIDE_max] = {
423 0, // SEG_OVERRIDE_NONE
432 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
434 mcInst.addOperand(baseReg);
435 mcInst.addOperand(scaleAmount);
436 mcInst.addOperand(indexReg);
437 mcInst.addOperand(displacement);
438 mcInst.addOperand(segmentReg);
442 /// translateRM - Translates an operand stored in the R/M (and possibly SIB)
443 /// byte of an instruction to LLVM form, and appends it to an MCInst.
445 /// @param mcInst - The MCInst to append to.
446 /// @param operand - The operand, as stored in the descriptor table.
447 /// @param insn - The instruction to extract Mod, R/M, and SIB fields
449 /// @return - 0 on success; nonzero otherwise
450 static bool translateRM(MCInst &mcInst, const OperandSpecifier &operand,
451 InternalInstruction &insn) {
452 switch (operand.type) {
454 debug("Unexpected type for a R/M operand");
470 case TYPE_CONTROLREG:
471 return translateRMRegister(mcInst, insn);
491 return translateRMMemory(mcInst, insn);
495 /// translateFPRegister - Translates a stack position on the FPU stack to its
496 /// LLVM form, and appends it to an MCInst.
498 /// @param mcInst - The MCInst to append to.
499 /// @param stackPos - The stack position to translate.
500 /// @return - 0 on success; nonzero otherwise.
501 static bool translateFPRegister(MCInst &mcInst,
504 debug("Invalid FP stack position");
508 mcInst.addOperand(MCOperand::CreateReg(X86::ST0 + stackPos));
513 /// translateOperand - Translates an operand stored in an internal instruction
514 /// to LLVM's format and appends it to an MCInst.
516 /// @param mcInst - The MCInst to append to.
517 /// @param operand - The operand, as stored in the descriptor table.
518 /// @param insn - The internal instruction.
519 /// @return - false on success; true otherwise.
520 static bool translateOperand(MCInst &mcInst, const OperandSpecifier &operand,
521 InternalInstruction &insn) {
522 switch (operand.encoding) {
524 debug("Unhandled operand encoding during translation");
527 translateRegister(mcInst, insn.reg);
530 return translateRM(mcInst, operand, insn);
537 debug("Translation of code offsets isn't supported.");
545 translateImmediate(mcInst,
546 insn.immediates[insn.numImmediatesTranslated++],
554 translateRegister(mcInst, insn.opcodeRegister);
557 return translateFPRegister(mcInst, insn.opcodeModifier);
559 translateRegister(mcInst, insn.opcodeRegister);
562 translateRegister(mcInst, insn.vvvv);
565 return translateOperand(mcInst,
566 insn.spec->operands[operand.type - TYPE_DUP0],
571 /// translateInstruction - Translates an internal instruction and all its
572 /// operands to an MCInst.
574 /// @param mcInst - The MCInst to populate with the instruction's data.
575 /// @param insn - The internal instruction.
576 /// @return - false on success; true otherwise.
577 static bool translateInstruction(MCInst &mcInst,
578 InternalInstruction &insn) {
580 debug("Instruction has no specification");
584 mcInst.setOpcode(insn.instructionID);
588 insn.numImmediatesTranslated = 0;
590 for (index = 0; index < X86_MAX_OPERANDS; ++index) {
591 if (insn.spec->operands[index].encoding != ENCODING_NONE) {
592 if (translateOperand(mcInst, insn.spec->operands[index], insn)) {
601 static MCDisassembler *createX86_32Disassembler(const Target &T,
602 const MCSubtargetInfo &STI) {
603 return new X86Disassembler::X86GenericDisassembler(STI, MODE_32BIT,
604 T.createMCInstrInfo());
607 static MCDisassembler *createX86_64Disassembler(const Target &T,
608 const MCSubtargetInfo &STI) {
609 return new X86Disassembler::X86GenericDisassembler(STI, MODE_64BIT,
610 T.createMCInstrInfo());
613 extern "C" void LLVMInitializeX86Disassembler() {
614 // Register the disassembler.
615 TargetRegistry::RegisterMCDisassembler(TheX86_32Target,
616 createX86_32Disassembler);
617 TargetRegistry::RegisterMCDisassembler(TheX86_64Target,
618 createX86_64Disassembler);