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,
118 raw_ostream &cStream) const {
119 InternalInstruction internalInstr;
121 int ret = decodeInstruction(&internalInstr,
130 size = internalInstr.readerCursor - address;
134 size = internalInstr.length;
135 return (!translateInstruction(instr, internalInstr)) ? Success : Fail;
140 // Private code that translates from struct InternalInstructions to MCInsts.
143 /// translateRegister - Translates an internal register to the appropriate LLVM
144 /// register, and appends it as an operand to an MCInst.
146 /// @param mcInst - The MCInst to append to.
147 /// @param reg - The Reg to append.
148 static void translateRegister(MCInst &mcInst, Reg reg) {
149 #define ENTRY(x) X86::x,
150 uint8_t llvmRegnums[] = {
156 uint8_t llvmRegnum = llvmRegnums[reg];
157 mcInst.addOperand(MCOperand::CreateReg(llvmRegnum));
160 /// translateImmediate - Appends an immediate operand to an MCInst.
162 /// @param mcInst - The MCInst to append to.
163 /// @param immediate - The immediate value to append.
164 /// @param operand - The operand, as stored in the descriptor table.
165 /// @param insn - The internal instruction.
166 static void translateImmediate(MCInst &mcInst, uint64_t immediate,
167 const OperandSpecifier &operand,
168 InternalInstruction &insn) {
169 // Sign-extend the immediate if necessary.
171 OperandType type = operand.type;
173 if (type == TYPE_RELv) {
174 switch (insn.displacementSize) {
191 // By default sign-extend all X86 immediates based on their encoding.
192 else if (type == TYPE_IMM8 || type == TYPE_IMM16 || type == TYPE_IMM32 ||
193 type == TYPE_IMM64) {
194 uint32_t Opcode = mcInst.getOpcode();
195 switch (operand.encoding) {
199 // Special case those X86 instructions that use the imm8 as a set of
200 // bits, bit count, etc. and are not sign-extend.
201 if (Opcode != X86::BLENDPSrri && Opcode != X86::BLENDPDrri &&
202 Opcode != X86::PBLENDWrri && Opcode != X86::MPSADBWrri &&
203 Opcode != X86::DPPSrri && Opcode != X86::DPPDrri &&
204 Opcode != X86::INSERTPSrr && Opcode != X86::VBLENDPSYrri &&
205 Opcode != X86::VBLENDPSYrmi && Opcode != X86::VBLENDPDYrri &&
206 Opcode != X86::VBLENDPDYrmi && Opcode != X86::VPBLENDWrri &&
207 Opcode != X86::VMPSADBWrri && Opcode != X86::VDPPSYrri &&
208 Opcode != X86::VDPPSYrmi && Opcode != X86::VDPPDrri &&
209 Opcode != X86::VINSERTPSrr)
226 mcInst.addOperand(MCOperand::CreateReg(X86::XMM0 + (immediate >> 4)));
229 mcInst.addOperand(MCOperand::CreateReg(X86::YMM0 + (immediate >> 4)));
234 immediate |= ~(0xffull);
237 if(immediate & 0x8000)
238 immediate |= ~(0xffffull);
243 if(immediate & 0x80000000)
244 immediate |= ~(0xffffffffull);
248 // operand is 64 bits wide. Do nothing.
252 mcInst.addOperand(MCOperand::CreateImm(immediate));
255 /// translateRMRegister - Translates a register stored in the R/M field of the
256 /// ModR/M byte to its LLVM equivalent and appends it to an MCInst.
257 /// @param mcInst - The MCInst to append to.
258 /// @param insn - The internal instruction to extract the R/M field
260 /// @return - 0 on success; -1 otherwise
261 static bool translateRMRegister(MCInst &mcInst,
262 InternalInstruction &insn) {
263 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
264 debug("A R/M register operand may not have a SIB byte");
268 switch (insn.eaBase) {
270 debug("Unexpected EA base register");
273 debug("EA_BASE_NONE for ModR/M base");
275 #define ENTRY(x) case EA_BASE_##x:
278 debug("A R/M register operand may not have a base; "
279 "the operand must be a register.");
283 mcInst.addOperand(MCOperand::CreateReg(X86::x)); break;
291 /// translateRMMemory - Translates a memory operand stored in the Mod and R/M
292 /// fields of an internal instruction (and possibly its SIB byte) to a memory
293 /// operand in LLVM's format, and appends it to an MCInst.
295 /// @param mcInst - The MCInst to append to.
296 /// @param insn - The instruction to extract Mod, R/M, and SIB fields
298 /// @return - 0 on success; nonzero otherwise
299 static bool translateRMMemory(MCInst &mcInst, InternalInstruction &insn) {
300 // Addresses in an MCInst are represented as five operands:
301 // 1. basereg (register) The R/M base, or (if there is a SIB) the
303 // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified
305 // 3. indexreg (register) x86_registerNONE, or (if there is a SIB)
306 // the index (which is multiplied by the
308 // 4. displacement (immediate) 0, or the displacement if there is one
309 // 5. segmentreg (register) x86_registerNONE for now, but could be set
310 // if we have segment overrides
313 MCOperand scaleAmount;
315 MCOperand displacement;
316 MCOperand segmentReg;
318 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
319 if (insn.sibBase != SIB_BASE_NONE) {
320 switch (insn.sibBase) {
322 debug("Unexpected sibBase");
326 baseReg = MCOperand::CreateReg(X86::x); break;
331 baseReg = MCOperand::CreateReg(0);
334 if (insn.sibIndex != SIB_INDEX_NONE) {
335 switch (insn.sibIndex) {
337 debug("Unexpected sibIndex");
340 case SIB_INDEX_##x: \
341 indexReg = MCOperand::CreateReg(X86::x); break;
347 indexReg = MCOperand::CreateReg(0);
350 scaleAmount = MCOperand::CreateImm(insn.sibScale);
352 switch (insn.eaBase) {
354 if (insn.eaDisplacement == EA_DISP_NONE) {
355 debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
358 if (insn.mode == MODE_64BIT)
359 baseReg = MCOperand::CreateReg(X86::RIP); // Section 2.2.1.6
361 baseReg = MCOperand::CreateReg(0);
363 indexReg = MCOperand::CreateReg(0);
366 baseReg = MCOperand::CreateReg(X86::BX);
367 indexReg = MCOperand::CreateReg(X86::SI);
370 baseReg = MCOperand::CreateReg(X86::BX);
371 indexReg = MCOperand::CreateReg(X86::DI);
374 baseReg = MCOperand::CreateReg(X86::BP);
375 indexReg = MCOperand::CreateReg(X86::SI);
378 baseReg = MCOperand::CreateReg(X86::BP);
379 indexReg = MCOperand::CreateReg(X86::DI);
382 indexReg = MCOperand::CreateReg(0);
383 switch (insn.eaBase) {
385 debug("Unexpected eaBase");
387 // Here, we will use the fill-ins defined above. However,
388 // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and
389 // sib and sib64 were handled in the top-level if, so they're only
390 // placeholders to keep the compiler happy.
393 baseReg = MCOperand::CreateReg(X86::x); break;
396 #define ENTRY(x) case EA_REG_##x:
399 debug("A R/M memory operand may not be a register; "
400 "the base field must be a base.");
405 scaleAmount = MCOperand::CreateImm(1);
408 displacement = MCOperand::CreateImm(insn.displacement);
410 static const uint8_t segmentRegnums[SEG_OVERRIDE_max] = {
411 0, // SEG_OVERRIDE_NONE
420 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
422 mcInst.addOperand(baseReg);
423 mcInst.addOperand(scaleAmount);
424 mcInst.addOperand(indexReg);
425 mcInst.addOperand(displacement);
426 mcInst.addOperand(segmentReg);
430 /// translateRM - Translates an operand stored in the R/M (and possibly SIB)
431 /// byte of an instruction to LLVM form, and appends it to an MCInst.
433 /// @param mcInst - The MCInst to append to.
434 /// @param operand - The operand, as stored in the descriptor table.
435 /// @param insn - The instruction to extract Mod, R/M, and SIB fields
437 /// @return - 0 on success; nonzero otherwise
438 static bool translateRM(MCInst &mcInst, const OperandSpecifier &operand,
439 InternalInstruction &insn) {
440 switch (operand.type) {
442 debug("Unexpected type for a R/M operand");
458 case TYPE_CONTROLREG:
459 return translateRMRegister(mcInst, insn);
479 return translateRMMemory(mcInst, insn);
483 /// translateFPRegister - Translates a stack position on the FPU stack to its
484 /// LLVM form, and appends it to an MCInst.
486 /// @param mcInst - The MCInst to append to.
487 /// @param stackPos - The stack position to translate.
488 /// @return - 0 on success; nonzero otherwise.
489 static bool translateFPRegister(MCInst &mcInst,
492 debug("Invalid FP stack position");
496 mcInst.addOperand(MCOperand::CreateReg(X86::ST0 + stackPos));
501 /// translateOperand - Translates an operand stored in an internal instruction
502 /// to LLVM's format and appends it to an MCInst.
504 /// @param mcInst - The MCInst to append to.
505 /// @param operand - The operand, as stored in the descriptor table.
506 /// @param insn - The internal instruction.
507 /// @return - false on success; true otherwise.
508 static bool translateOperand(MCInst &mcInst, const OperandSpecifier &operand,
509 InternalInstruction &insn) {
510 switch (operand.encoding) {
512 debug("Unhandled operand encoding during translation");
515 translateRegister(mcInst, insn.reg);
518 return translateRM(mcInst, operand, insn);
525 debug("Translation of code offsets isn't supported.");
533 translateImmediate(mcInst,
534 insn.immediates[insn.numImmediatesTranslated++],
542 translateRegister(mcInst, insn.opcodeRegister);
545 return translateFPRegister(mcInst, insn.opcodeModifier);
547 translateRegister(mcInst, insn.opcodeRegister);
550 translateRegister(mcInst, insn.vvvv);
553 return translateOperand(mcInst,
554 insn.spec->operands[operand.type - TYPE_DUP0],
559 /// translateInstruction - Translates an internal instruction and all its
560 /// operands to an MCInst.
562 /// @param mcInst - The MCInst to populate with the instruction's data.
563 /// @param insn - The internal instruction.
564 /// @return - false on success; true otherwise.
565 static bool translateInstruction(MCInst &mcInst,
566 InternalInstruction &insn) {
568 debug("Instruction has no specification");
572 mcInst.setOpcode(insn.instructionID);
576 insn.numImmediatesTranslated = 0;
578 for (index = 0; index < X86_MAX_OPERANDS; ++index) {
579 if (insn.spec->operands[index].encoding != ENCODING_NONE) {
580 if (translateOperand(mcInst, insn.spec->operands[index], insn)) {
589 static MCDisassembler *createX86_32Disassembler(const Target &T, const MCSubtargetInfo &STI) {
590 return new X86Disassembler::X86_32Disassembler(STI);
593 static MCDisassembler *createX86_64Disassembler(const Target &T, const MCSubtargetInfo &STI) {
594 return new X86Disassembler::X86_64Disassembler(STI);
597 extern "C" void LLVMInitializeX86Disassembler() {
598 // Register the disassembler.
599 TargetRegistry::RegisterMCDisassembler(TheX86_32Target,
600 createX86_32Disassembler);
601 TargetRegistry::RegisterMCDisassembler(TheX86_64Target,
602 createX86_64Disassembler);