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)
227 immediate |= ~(0xffull);
230 if(immediate & 0x8000)
231 immediate |= ~(0xffffull);
236 if(immediate & 0x80000000)
237 immediate |= ~(0xffffffffull);
241 // operand is 64 bits wide. Do nothing.
245 mcInst.addOperand(MCOperand::CreateImm(immediate));
248 /// translateRMRegister - Translates a register stored in the R/M field of the
249 /// ModR/M byte to its LLVM equivalent and appends it to an MCInst.
250 /// @param mcInst - The MCInst to append to.
251 /// @param insn - The internal instruction to extract the R/M field
253 /// @return - 0 on success; -1 otherwise
254 static bool translateRMRegister(MCInst &mcInst,
255 InternalInstruction &insn) {
256 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
257 debug("A R/M register operand may not have a SIB byte");
261 switch (insn.eaBase) {
263 debug("Unexpected EA base register");
266 debug("EA_BASE_NONE for ModR/M base");
268 #define ENTRY(x) case EA_BASE_##x:
271 debug("A R/M register operand may not have a base; "
272 "the operand must be a register.");
276 mcInst.addOperand(MCOperand::CreateReg(X86::x)); break;
284 /// translateRMMemory - Translates a memory operand stored in the Mod and R/M
285 /// fields of an internal instruction (and possibly its SIB byte) to a memory
286 /// operand in LLVM's format, and appends it to an MCInst.
288 /// @param mcInst - The MCInst to append to.
289 /// @param insn - The instruction to extract Mod, R/M, and SIB fields
291 /// @return - 0 on success; nonzero otherwise
292 static bool translateRMMemory(MCInst &mcInst, InternalInstruction &insn) {
293 // Addresses in an MCInst are represented as five operands:
294 // 1. basereg (register) The R/M base, or (if there is a SIB) the
296 // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified
298 // 3. indexreg (register) x86_registerNONE, or (if there is a SIB)
299 // the index (which is multiplied by the
301 // 4. displacement (immediate) 0, or the displacement if there is one
302 // 5. segmentreg (register) x86_registerNONE for now, but could be set
303 // if we have segment overrides
306 MCOperand scaleAmount;
308 MCOperand displacement;
309 MCOperand segmentReg;
311 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
312 if (insn.sibBase != SIB_BASE_NONE) {
313 switch (insn.sibBase) {
315 debug("Unexpected sibBase");
319 baseReg = MCOperand::CreateReg(X86::x); break;
324 baseReg = MCOperand::CreateReg(0);
327 if (insn.sibIndex != SIB_INDEX_NONE) {
328 switch (insn.sibIndex) {
330 debug("Unexpected sibIndex");
333 case SIB_INDEX_##x: \
334 indexReg = MCOperand::CreateReg(X86::x); break;
340 indexReg = MCOperand::CreateReg(0);
343 scaleAmount = MCOperand::CreateImm(insn.sibScale);
345 switch (insn.eaBase) {
347 if (insn.eaDisplacement == EA_DISP_NONE) {
348 debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
351 if (insn.mode == MODE_64BIT)
352 baseReg = MCOperand::CreateReg(X86::RIP); // Section 2.2.1.6
354 baseReg = MCOperand::CreateReg(0);
356 indexReg = MCOperand::CreateReg(0);
359 baseReg = MCOperand::CreateReg(X86::BX);
360 indexReg = MCOperand::CreateReg(X86::SI);
363 baseReg = MCOperand::CreateReg(X86::BX);
364 indexReg = MCOperand::CreateReg(X86::DI);
367 baseReg = MCOperand::CreateReg(X86::BP);
368 indexReg = MCOperand::CreateReg(X86::SI);
371 baseReg = MCOperand::CreateReg(X86::BP);
372 indexReg = MCOperand::CreateReg(X86::DI);
375 indexReg = MCOperand::CreateReg(0);
376 switch (insn.eaBase) {
378 debug("Unexpected eaBase");
380 // Here, we will use the fill-ins defined above. However,
381 // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and
382 // sib and sib64 were handled in the top-level if, so they're only
383 // placeholders to keep the compiler happy.
386 baseReg = MCOperand::CreateReg(X86::x); break;
389 #define ENTRY(x) case EA_REG_##x:
392 debug("A R/M memory operand may not be a register; "
393 "the base field must be a base.");
398 scaleAmount = MCOperand::CreateImm(1);
401 displacement = MCOperand::CreateImm(insn.displacement);
403 static const uint8_t segmentRegnums[SEG_OVERRIDE_max] = {
404 0, // SEG_OVERRIDE_NONE
413 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
415 mcInst.addOperand(baseReg);
416 mcInst.addOperand(scaleAmount);
417 mcInst.addOperand(indexReg);
418 mcInst.addOperand(displacement);
419 mcInst.addOperand(segmentReg);
423 /// translateRM - Translates an operand stored in the R/M (and possibly SIB)
424 /// byte of an instruction to LLVM form, and appends it to an MCInst.
426 /// @param mcInst - The MCInst to append to.
427 /// @param operand - The operand, as stored in the descriptor table.
428 /// @param insn - The instruction to extract Mod, R/M, and SIB fields
430 /// @return - 0 on success; nonzero otherwise
431 static bool translateRM(MCInst &mcInst, const OperandSpecifier &operand,
432 InternalInstruction &insn) {
433 switch (operand.type) {
435 debug("Unexpected type for a R/M operand");
451 case TYPE_CONTROLREG:
452 return translateRMRegister(mcInst, insn);
472 return translateRMMemory(mcInst, insn);
476 /// translateFPRegister - Translates a stack position on the FPU stack to its
477 /// LLVM form, and appends it to an MCInst.
479 /// @param mcInst - The MCInst to append to.
480 /// @param stackPos - The stack position to translate.
481 /// @return - 0 on success; nonzero otherwise.
482 static bool translateFPRegister(MCInst &mcInst,
485 debug("Invalid FP stack position");
489 mcInst.addOperand(MCOperand::CreateReg(X86::ST0 + stackPos));
494 /// translateOperand - Translates an operand stored in an internal instruction
495 /// to LLVM's format and appends it to an MCInst.
497 /// @param mcInst - The MCInst to append to.
498 /// @param operand - The operand, as stored in the descriptor table.
499 /// @param insn - The internal instruction.
500 /// @return - false on success; true otherwise.
501 static bool translateOperand(MCInst &mcInst, const OperandSpecifier &operand,
502 InternalInstruction &insn) {
503 switch (operand.encoding) {
505 debug("Unhandled operand encoding during translation");
508 translateRegister(mcInst, insn.reg);
511 return translateRM(mcInst, operand, insn);
518 debug("Translation of code offsets isn't supported.");
526 translateImmediate(mcInst,
527 insn.immediates[insn.numImmediatesTranslated++],
535 translateRegister(mcInst, insn.opcodeRegister);
538 return translateFPRegister(mcInst, insn.opcodeModifier);
540 translateRegister(mcInst, insn.opcodeRegister);
543 translateRegister(mcInst, insn.vvvv);
546 return translateOperand(mcInst,
547 insn.spec->operands[operand.type - TYPE_DUP0],
552 /// translateInstruction - Translates an internal instruction and all its
553 /// operands to an MCInst.
555 /// @param mcInst - The MCInst to populate with the instruction's data.
556 /// @param insn - The internal instruction.
557 /// @return - false on success; true otherwise.
558 static bool translateInstruction(MCInst &mcInst,
559 InternalInstruction &insn) {
561 debug("Instruction has no specification");
565 mcInst.setOpcode(insn.instructionID);
569 insn.numImmediatesTranslated = 0;
571 for (index = 0; index < X86_MAX_OPERANDS; ++index) {
572 if (insn.spec->operands[index].encoding != ENCODING_NONE) {
573 if (translateOperand(mcInst, insn.spec->operands[index], insn)) {
582 static MCDisassembler *createX86_32Disassembler(const Target &T, const MCSubtargetInfo &STI) {
583 return new X86Disassembler::X86_32Disassembler(STI);
586 static MCDisassembler *createX86_64Disassembler(const Target &T, const MCSubtargetInfo &STI) {
587 return new X86Disassembler::X86_64Disassembler(STI);
590 extern "C" void LLVMInitializeX86Disassembler() {
591 // Register the disassembler.
592 TargetRegistry::RegisterMCDisassembler(TheX86_32Target,
593 createX86_32Disassembler);
594 TargetRegistry::RegisterMCDisassembler(TheX86_64Target,
595 createX86_64Disassembler);