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 dlog_t loggerFn = logger;
122 if (&vStream == &nulls())
123 loggerFn = 0; // Disable logging completely if it's going to nulls().
125 int ret = decodeInstruction(&internalInstr,
134 size = internalInstr.readerCursor - address;
138 size = internalInstr.length;
139 return (!translateInstruction(instr, internalInstr)) ? Success : Fail;
144 // Private code that translates from struct InternalInstructions to MCInsts.
147 /// translateRegister - Translates an internal register to the appropriate LLVM
148 /// register, and appends it as an operand to an MCInst.
150 /// @param mcInst - The MCInst to append to.
151 /// @param reg - The Reg to append.
152 static void translateRegister(MCInst &mcInst, Reg reg) {
153 #define ENTRY(x) X86::x,
154 uint8_t llvmRegnums[] = {
160 uint8_t llvmRegnum = llvmRegnums[reg];
161 mcInst.addOperand(MCOperand::CreateReg(llvmRegnum));
164 /// translateImmediate - Appends an immediate operand to an MCInst.
166 /// @param mcInst - The MCInst to append to.
167 /// @param immediate - The immediate value to append.
168 /// @param operand - The operand, as stored in the descriptor table.
169 /// @param insn - The internal instruction.
170 static void translateImmediate(MCInst &mcInst, uint64_t immediate,
171 const OperandSpecifier &operand,
172 InternalInstruction &insn) {
173 // Sign-extend the immediate if necessary.
175 OperandType type = operand.type;
177 if (type == TYPE_RELv) {
178 switch (insn.displacementSize) {
195 // By default sign-extend all X86 immediates based on their encoding.
196 else if (type == TYPE_IMM8 || type == TYPE_IMM16 || type == TYPE_IMM32 ||
197 type == TYPE_IMM64) {
198 uint32_t Opcode = mcInst.getOpcode();
199 switch (operand.encoding) {
203 // Special case those X86 instructions that use the imm8 as a set of
204 // bits, bit count, etc. and are not sign-extend.
205 if (Opcode != X86::BLENDPSrri && Opcode != X86::BLENDPDrri &&
206 Opcode != X86::PBLENDWrri && Opcode != X86::MPSADBWrri &&
207 Opcode != X86::DPPSrri && Opcode != X86::DPPDrri &&
208 Opcode != X86::INSERTPSrr && Opcode != X86::VBLENDPSYrri &&
209 Opcode != X86::VBLENDPSYrmi && Opcode != X86::VBLENDPDYrri &&
210 Opcode != X86::VBLENDPDYrmi && Opcode != X86::VPBLENDWrri &&
211 Opcode != X86::VMPSADBWrri && Opcode != X86::VDPPSYrri &&
212 Opcode != X86::VDPPSYrmi && Opcode != X86::VDPPDrri &&
213 Opcode != X86::VINSERTPSrr)
230 mcInst.addOperand(MCOperand::CreateReg(X86::XMM0 + (immediate >> 4)));
233 mcInst.addOperand(MCOperand::CreateReg(X86::YMM0 + (immediate >> 4)));
238 immediate |= ~(0xffull);
241 if(immediate & 0x8000)
242 immediate |= ~(0xffffull);
247 if(immediate & 0x80000000)
248 immediate |= ~(0xffffffffull);
252 // operand is 64 bits wide. Do nothing.
256 mcInst.addOperand(MCOperand::CreateImm(immediate));
259 /// translateRMRegister - Translates a register stored in the R/M field of the
260 /// ModR/M byte to its LLVM equivalent and appends it to an MCInst.
261 /// @param mcInst - The MCInst to append to.
262 /// @param insn - The internal instruction to extract the R/M field
264 /// @return - 0 on success; -1 otherwise
265 static bool translateRMRegister(MCInst &mcInst,
266 InternalInstruction &insn) {
267 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
268 debug("A R/M register operand may not have a SIB byte");
272 switch (insn.eaBase) {
274 debug("Unexpected EA base register");
277 debug("EA_BASE_NONE for ModR/M base");
279 #define ENTRY(x) case EA_BASE_##x:
282 debug("A R/M register operand may not have a base; "
283 "the operand must be a register.");
287 mcInst.addOperand(MCOperand::CreateReg(X86::x)); break;
295 /// translateRMMemory - Translates a memory operand stored in the Mod and R/M
296 /// fields of an internal instruction (and possibly its SIB byte) to a memory
297 /// operand in LLVM's format, and appends it to an MCInst.
299 /// @param mcInst - The MCInst to append to.
300 /// @param insn - The instruction to extract Mod, R/M, and SIB fields
302 /// @return - 0 on success; nonzero otherwise
303 static bool translateRMMemory(MCInst &mcInst, InternalInstruction &insn) {
304 // Addresses in an MCInst are represented as five operands:
305 // 1. basereg (register) The R/M base, or (if there is a SIB) the
307 // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified
309 // 3. indexreg (register) x86_registerNONE, or (if there is a SIB)
310 // the index (which is multiplied by the
312 // 4. displacement (immediate) 0, or the displacement if there is one
313 // 5. segmentreg (register) x86_registerNONE for now, but could be set
314 // if we have segment overrides
317 MCOperand scaleAmount;
319 MCOperand displacement;
320 MCOperand segmentReg;
322 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
323 if (insn.sibBase != SIB_BASE_NONE) {
324 switch (insn.sibBase) {
326 debug("Unexpected sibBase");
330 baseReg = MCOperand::CreateReg(X86::x); break;
335 baseReg = MCOperand::CreateReg(0);
338 if (insn.sibIndex != SIB_INDEX_NONE) {
339 switch (insn.sibIndex) {
341 debug("Unexpected sibIndex");
344 case SIB_INDEX_##x: \
345 indexReg = MCOperand::CreateReg(X86::x); break;
351 indexReg = MCOperand::CreateReg(0);
354 scaleAmount = MCOperand::CreateImm(insn.sibScale);
356 switch (insn.eaBase) {
358 if (insn.eaDisplacement == EA_DISP_NONE) {
359 debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
362 if (insn.mode == MODE_64BIT)
363 baseReg = MCOperand::CreateReg(X86::RIP); // Section 2.2.1.6
365 baseReg = MCOperand::CreateReg(0);
367 indexReg = MCOperand::CreateReg(0);
370 baseReg = MCOperand::CreateReg(X86::BX);
371 indexReg = MCOperand::CreateReg(X86::SI);
374 baseReg = MCOperand::CreateReg(X86::BX);
375 indexReg = MCOperand::CreateReg(X86::DI);
378 baseReg = MCOperand::CreateReg(X86::BP);
379 indexReg = MCOperand::CreateReg(X86::SI);
382 baseReg = MCOperand::CreateReg(X86::BP);
383 indexReg = MCOperand::CreateReg(X86::DI);
386 indexReg = MCOperand::CreateReg(0);
387 switch (insn.eaBase) {
389 debug("Unexpected eaBase");
391 // Here, we will use the fill-ins defined above. However,
392 // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and
393 // sib and sib64 were handled in the top-level if, so they're only
394 // placeholders to keep the compiler happy.
397 baseReg = MCOperand::CreateReg(X86::x); break;
400 #define ENTRY(x) case EA_REG_##x:
403 debug("A R/M memory operand may not be a register; "
404 "the base field must be a base.");
409 scaleAmount = MCOperand::CreateImm(1);
412 displacement = MCOperand::CreateImm(insn.displacement);
414 static const uint8_t segmentRegnums[SEG_OVERRIDE_max] = {
415 0, // SEG_OVERRIDE_NONE
424 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
426 mcInst.addOperand(baseReg);
427 mcInst.addOperand(scaleAmount);
428 mcInst.addOperand(indexReg);
429 mcInst.addOperand(displacement);
430 mcInst.addOperand(segmentReg);
434 /// translateRM - Translates an operand stored in the R/M (and possibly SIB)
435 /// byte of an instruction to LLVM form, and appends it to an MCInst.
437 /// @param mcInst - The MCInst to append to.
438 /// @param operand - The operand, as stored in the descriptor table.
439 /// @param insn - The instruction to extract Mod, R/M, and SIB fields
441 /// @return - 0 on success; nonzero otherwise
442 static bool translateRM(MCInst &mcInst, const OperandSpecifier &operand,
443 InternalInstruction &insn) {
444 switch (operand.type) {
446 debug("Unexpected type for a R/M operand");
462 case TYPE_CONTROLREG:
463 return translateRMRegister(mcInst, insn);
483 return translateRMMemory(mcInst, insn);
487 /// translateFPRegister - Translates a stack position on the FPU stack to its
488 /// LLVM form, and appends it to an MCInst.
490 /// @param mcInst - The MCInst to append to.
491 /// @param stackPos - The stack position to translate.
492 /// @return - 0 on success; nonzero otherwise.
493 static bool translateFPRegister(MCInst &mcInst,
496 debug("Invalid FP stack position");
500 mcInst.addOperand(MCOperand::CreateReg(X86::ST0 + stackPos));
505 /// translateOperand - Translates an operand stored in an internal instruction
506 /// to LLVM's format and appends it to an MCInst.
508 /// @param mcInst - The MCInst to append to.
509 /// @param operand - The operand, as stored in the descriptor table.
510 /// @param insn - The internal instruction.
511 /// @return - false on success; true otherwise.
512 static bool translateOperand(MCInst &mcInst, const OperandSpecifier &operand,
513 InternalInstruction &insn) {
514 switch (operand.encoding) {
516 debug("Unhandled operand encoding during translation");
519 translateRegister(mcInst, insn.reg);
522 return translateRM(mcInst, operand, insn);
529 debug("Translation of code offsets isn't supported.");
537 translateImmediate(mcInst,
538 insn.immediates[insn.numImmediatesTranslated++],
546 translateRegister(mcInst, insn.opcodeRegister);
549 return translateFPRegister(mcInst, insn.opcodeModifier);
551 translateRegister(mcInst, insn.opcodeRegister);
554 translateRegister(mcInst, insn.vvvv);
557 return translateOperand(mcInst,
558 insn.spec->operands[operand.type - TYPE_DUP0],
563 /// translateInstruction - Translates an internal instruction and all its
564 /// operands to an MCInst.
566 /// @param mcInst - The MCInst to populate with the instruction's data.
567 /// @param insn - The internal instruction.
568 /// @return - false on success; true otherwise.
569 static bool translateInstruction(MCInst &mcInst,
570 InternalInstruction &insn) {
572 debug("Instruction has no specification");
576 mcInst.setOpcode(insn.instructionID);
580 insn.numImmediatesTranslated = 0;
582 for (index = 0; index < X86_MAX_OPERANDS; ++index) {
583 if (insn.spec->operands[index].encoding != ENCODING_NONE) {
584 if (translateOperand(mcInst, insn.spec->operands[index], insn)) {
593 static MCDisassembler *createX86_32Disassembler(const Target &T, const MCSubtargetInfo &STI) {
594 return new X86Disassembler::X86GenericDisassembler(STI, MODE_32BIT);
597 static MCDisassembler *createX86_64Disassembler(const Target &T, const MCSubtargetInfo &STI) {
598 return new X86Disassembler::X86GenericDisassembler(STI, MODE_64BIT);
601 extern "C" void LLVMInitializeX86Disassembler() {
602 // Register the disassembler.
603 TargetRegistry::RegisterMCDisassembler(TheX86_32Target,
604 createX86_32Disassembler);
605 TargetRegistry::RegisterMCDisassembler(TheX86_64Target,
606 createX86_64Disassembler);