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/MCSubtargetInfo.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/MemoryObject.h"
27 #include "llvm/Support/TargetRegistry.h"
28 #include "llvm/Support/raw_ostream.h"
30 #define GET_REGINFO_ENUM
31 #include "X86GenRegisterInfo.inc"
32 #define GET_INSTRINFO_ENUM
33 #include "X86GenInstrInfo.inc"
34 #include "X86GenEDInfo.inc"
37 using namespace llvm::X86Disassembler;
39 void x86DisassemblerDebug(const char *file,
42 dbgs() << file << ":" << line << ": " << s;
45 #define debug(s) DEBUG(x86DisassemblerDebug(__FILE__, __LINE__, s));
49 // Fill-ins to make the compiler happy. These constants are never actually
50 // assigned; they are just filler to make an automatically-generated switch
63 extern Target TheX86_32Target, TheX86_64Target;
67 static bool translateInstruction(MCInst &target,
68 InternalInstruction &source);
70 X86GenericDisassembler::X86GenericDisassembler(const MCSubtargetInfo &STI, DisassemblerMode mode) :
75 X86GenericDisassembler::~X86GenericDisassembler() {
78 EDInstInfo *X86GenericDisassembler::getEDInfo() const {
82 /// regionReader - a callback function that wraps the readByte method from
85 /// @param arg - The generic callback parameter. In this case, this should
86 /// be a pointer to a MemoryObject.
87 /// @param byte - A pointer to the byte to be read.
88 /// @param address - The address to be read.
89 static int regionReader(void* arg, uint8_t* byte, uint64_t address) {
90 MemoryObject* region = static_cast<MemoryObject*>(arg);
91 return region->readByte(address, byte);
94 /// logger - a callback function that wraps the operator<< method from
97 /// @param arg - The generic callback parameter. This should be a pointe
99 /// @param log - A string to be logged. logger() adds a newline.
100 static void logger(void* arg, const char* log) {
104 raw_ostream &vStream = *(static_cast<raw_ostream*>(arg));
105 vStream << log << "\n";
109 // Public interface for the disassembler
112 MCDisassembler::DecodeStatus
113 X86GenericDisassembler::getInstruction(MCInst &instr,
115 const MemoryObject ®ion,
117 raw_ostream &vStream) const {
118 InternalInstruction internalInstr;
120 int ret = decodeInstruction(&internalInstr,
129 size = internalInstr.readerCursor - address;
133 size = internalInstr.length;
134 return (!translateInstruction(instr, internalInstr)) ? Success : Fail;
139 // Private code that translates from struct InternalInstructions to MCInsts.
142 /// translateRegister - Translates an internal register to the appropriate LLVM
143 /// register, and appends it as an operand to an MCInst.
145 /// @param mcInst - The MCInst to append to.
146 /// @param reg - The Reg to append.
147 static void translateRegister(MCInst &mcInst, Reg reg) {
148 #define ENTRY(x) X86::x,
149 uint8_t llvmRegnums[] = {
155 uint8_t llvmRegnum = llvmRegnums[reg];
156 mcInst.addOperand(MCOperand::CreateReg(llvmRegnum));
159 /// translateImmediate - Appends an immediate operand to an MCInst.
161 /// @param mcInst - The MCInst to append to.
162 /// @param immediate - The immediate value to append.
163 /// @param operand - The operand, as stored in the descriptor table.
164 /// @param insn - The internal instruction.
165 static void translateImmediate(MCInst &mcInst, uint64_t immediate,
166 const OperandSpecifier &operand,
167 InternalInstruction &insn) {
168 // Sign-extend the immediate if necessary.
170 OperandType type = operand.type;
172 if (type == TYPE_RELv) {
173 switch (insn.displacementSize) {
190 // By default sign-extend all X86 immediates based on their encoding.
191 else if (type == TYPE_IMM8 || type == TYPE_IMM16 || type == TYPE_IMM32 ||
192 type == TYPE_IMM64) {
193 uint32_t Opcode = mcInst.getOpcode();
194 switch (operand.encoding) {
198 // Special case those X86 instructions that use the imm8 as a set of
199 // bits, bit count, etc. and are not sign-extend.
200 if (Opcode != X86::BLENDPSrri && Opcode != X86::BLENDPDrri &&
201 Opcode != X86::PBLENDWrri && Opcode != X86::MPSADBWrri &&
202 Opcode != X86::DPPSrri && Opcode != X86::DPPDrri &&
203 Opcode != X86::INSERTPSrr && Opcode != X86::VBLENDPSYrri &&
204 Opcode != X86::VBLENDPSYrmi && Opcode != X86::VBLENDPDYrri &&
205 Opcode != X86::VBLENDPDYrmi && Opcode != X86::VPBLENDWrri &&
206 Opcode != X86::VMPSADBWrri && Opcode != X86::VDPPSYrri &&
207 Opcode != X86::VDPPSYrmi && Opcode != X86::VDPPDrri &&
208 Opcode != X86::VINSERTPSrr)
225 mcInst.addOperand(MCOperand::CreateReg(X86::XMM0 + (immediate >> 4)));
228 mcInst.addOperand(MCOperand::CreateReg(X86::YMM0 + (immediate >> 4)));
233 immediate |= ~(0xffull);
236 if(immediate & 0x8000)
237 immediate |= ~(0xffffull);
242 if(immediate & 0x80000000)
243 immediate |= ~(0xffffffffull);
247 // operand is 64 bits wide. Do nothing.
251 mcInst.addOperand(MCOperand::CreateImm(immediate));
254 /// translateRMRegister - Translates a register stored in the R/M field of the
255 /// ModR/M byte to its LLVM equivalent and appends it to an MCInst.
256 /// @param mcInst - The MCInst to append to.
257 /// @param insn - The internal instruction to extract the R/M field
259 /// @return - 0 on success; -1 otherwise
260 static bool translateRMRegister(MCInst &mcInst,
261 InternalInstruction &insn) {
262 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
263 debug("A R/M register operand may not have a SIB byte");
267 switch (insn.eaBase) {
269 debug("Unexpected EA base register");
272 debug("EA_BASE_NONE for ModR/M base");
274 #define ENTRY(x) case EA_BASE_##x:
277 debug("A R/M register operand may not have a base; "
278 "the operand must be a register.");
282 mcInst.addOperand(MCOperand::CreateReg(X86::x)); break;
290 /// translateRMMemory - Translates a memory operand stored in the Mod and R/M
291 /// fields of an internal instruction (and possibly its SIB byte) to a memory
292 /// operand in LLVM's format, and appends it to an MCInst.
294 /// @param mcInst - The MCInst to append to.
295 /// @param insn - The instruction to extract Mod, R/M, and SIB fields
297 /// @return - 0 on success; nonzero otherwise
298 static bool translateRMMemory(MCInst &mcInst, InternalInstruction &insn) {
299 // Addresses in an MCInst are represented as five operands:
300 // 1. basereg (register) The R/M base, or (if there is a SIB) the
302 // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified
304 // 3. indexreg (register) x86_registerNONE, or (if there is a SIB)
305 // the index (which is multiplied by the
307 // 4. displacement (immediate) 0, or the displacement if there is one
308 // 5. segmentreg (register) x86_registerNONE for now, but could be set
309 // if we have segment overrides
312 MCOperand scaleAmount;
314 MCOperand displacement;
315 MCOperand segmentReg;
317 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
318 if (insn.sibBase != SIB_BASE_NONE) {
319 switch (insn.sibBase) {
321 debug("Unexpected sibBase");
325 baseReg = MCOperand::CreateReg(X86::x); break;
330 baseReg = MCOperand::CreateReg(0);
333 if (insn.sibIndex != SIB_INDEX_NONE) {
334 switch (insn.sibIndex) {
336 debug("Unexpected sibIndex");
339 case SIB_INDEX_##x: \
340 indexReg = MCOperand::CreateReg(X86::x); break;
346 indexReg = MCOperand::CreateReg(0);
349 scaleAmount = MCOperand::CreateImm(insn.sibScale);
351 switch (insn.eaBase) {
353 if (insn.eaDisplacement == EA_DISP_NONE) {
354 debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
357 if (insn.mode == MODE_64BIT)
358 baseReg = MCOperand::CreateReg(X86::RIP); // Section 2.2.1.6
360 baseReg = MCOperand::CreateReg(0);
362 indexReg = MCOperand::CreateReg(0);
365 baseReg = MCOperand::CreateReg(X86::BX);
366 indexReg = MCOperand::CreateReg(X86::SI);
369 baseReg = MCOperand::CreateReg(X86::BX);
370 indexReg = MCOperand::CreateReg(X86::DI);
373 baseReg = MCOperand::CreateReg(X86::BP);
374 indexReg = MCOperand::CreateReg(X86::SI);
377 baseReg = MCOperand::CreateReg(X86::BP);
378 indexReg = MCOperand::CreateReg(X86::DI);
381 indexReg = MCOperand::CreateReg(0);
382 switch (insn.eaBase) {
384 debug("Unexpected eaBase");
386 // Here, we will use the fill-ins defined above. However,
387 // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and
388 // sib and sib64 were handled in the top-level if, so they're only
389 // placeholders to keep the compiler happy.
392 baseReg = MCOperand::CreateReg(X86::x); break;
395 #define ENTRY(x) case EA_REG_##x:
398 debug("A R/M memory operand may not be a register; "
399 "the base field must be a base.");
404 scaleAmount = MCOperand::CreateImm(1);
407 displacement = MCOperand::CreateImm(insn.displacement);
409 static const uint8_t segmentRegnums[SEG_OVERRIDE_max] = {
410 0, // SEG_OVERRIDE_NONE
419 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
421 mcInst.addOperand(baseReg);
422 mcInst.addOperand(scaleAmount);
423 mcInst.addOperand(indexReg);
424 mcInst.addOperand(displacement);
425 mcInst.addOperand(segmentReg);
429 /// translateRM - Translates an operand stored in the R/M (and possibly SIB)
430 /// byte of an instruction to LLVM form, and appends it to an MCInst.
432 /// @param mcInst - The MCInst to append to.
433 /// @param operand - The operand, as stored in the descriptor table.
434 /// @param insn - The instruction to extract Mod, R/M, and SIB fields
436 /// @return - 0 on success; nonzero otherwise
437 static bool translateRM(MCInst &mcInst, const OperandSpecifier &operand,
438 InternalInstruction &insn) {
439 switch (operand.type) {
441 debug("Unexpected type for a R/M operand");
457 case TYPE_CONTROLREG:
458 return translateRMRegister(mcInst, insn);
478 return translateRMMemory(mcInst, insn);
482 /// translateFPRegister - Translates a stack position on the FPU stack to its
483 /// LLVM form, and appends it to an MCInst.
485 /// @param mcInst - The MCInst to append to.
486 /// @param stackPos - The stack position to translate.
487 /// @return - 0 on success; nonzero otherwise.
488 static bool translateFPRegister(MCInst &mcInst,
491 debug("Invalid FP stack position");
495 mcInst.addOperand(MCOperand::CreateReg(X86::ST0 + stackPos));
500 /// translateOperand - Translates an operand stored in an internal instruction
501 /// to LLVM's format and appends it to an MCInst.
503 /// @param mcInst - The MCInst to append to.
504 /// @param operand - The operand, as stored in the descriptor table.
505 /// @param insn - The internal instruction.
506 /// @return - false on success; true otherwise.
507 static bool translateOperand(MCInst &mcInst, const OperandSpecifier &operand,
508 InternalInstruction &insn) {
509 switch (operand.encoding) {
511 debug("Unhandled operand encoding during translation");
514 translateRegister(mcInst, insn.reg);
517 return translateRM(mcInst, operand, insn);
524 debug("Translation of code offsets isn't supported.");
532 translateImmediate(mcInst,
533 insn.immediates[insn.numImmediatesTranslated++],
541 translateRegister(mcInst, insn.opcodeRegister);
544 return translateFPRegister(mcInst, insn.opcodeModifier);
546 translateRegister(mcInst, insn.opcodeRegister);
549 translateRegister(mcInst, insn.vvvv);
552 return translateOperand(mcInst,
553 insn.spec->operands[operand.type - TYPE_DUP0],
558 /// translateInstruction - Translates an internal instruction and all its
559 /// operands to an MCInst.
561 /// @param mcInst - The MCInst to populate with the instruction's data.
562 /// @param insn - The internal instruction.
563 /// @return - false on success; true otherwise.
564 static bool translateInstruction(MCInst &mcInst,
565 InternalInstruction &insn) {
567 debug("Instruction has no specification");
571 mcInst.setOpcode(insn.instructionID);
575 insn.numImmediatesTranslated = 0;
577 for (index = 0; index < X86_MAX_OPERANDS; ++index) {
578 if (insn.spec->operands[index].encoding != ENCODING_NONE) {
579 if (translateOperand(mcInst, insn.spec->operands[index], insn)) {
588 static MCDisassembler *createX86_32Disassembler(const Target &T, const MCSubtargetInfo &STI) {
589 return new X86Disassembler::X86_32Disassembler(STI);
592 static MCDisassembler *createX86_64Disassembler(const Target &T, const MCSubtargetInfo &STI) {
593 return new X86Disassembler::X86_64Disassembler(STI);
596 extern "C" void LLVMInitializeX86Disassembler() {
597 // Register the disassembler.
598 TargetRegistry::RegisterMCDisassembler(TheX86_32Target,
599 createX86_32Disassembler);
600 TargetRegistry::RegisterMCDisassembler(TheX86_64Target,
601 createX86_64Disassembler);