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/Support/Debug.h"
25 #include "llvm/Support/MemoryObject.h"
26 #include "llvm/Support/TargetRegistry.h"
27 #include "llvm/Support/raw_ostream.h"
29 #define GET_REGINFO_ENUM
30 #include "X86GenRegisterInfo.inc"
31 #include "X86GenEDInfo.inc"
34 using namespace llvm::X86Disassembler;
36 void x86DisassemblerDebug(const char *file,
39 dbgs() << file << ":" << line << ": " << s;
42 #define debug(s) DEBUG(x86DisassemblerDebug(__FILE__, __LINE__, s));
46 // Fill-ins to make the compiler happy. These constants are never actually
47 // assigned; they are just filler to make an automatically-generated switch
60 extern Target TheX86_32Target, TheX86_64Target;
64 static bool translateInstruction(MCInst &target,
65 InternalInstruction &source);
67 X86GenericDisassembler::X86GenericDisassembler(DisassemblerMode mode) :
72 X86GenericDisassembler::~X86GenericDisassembler() {
75 EDInstInfo *X86GenericDisassembler::getEDInfo() const {
79 /// regionReader - a callback function that wraps the readByte method from
82 /// @param arg - The generic callback parameter. In this case, this should
83 /// be a pointer to a MemoryObject.
84 /// @param byte - A pointer to the byte to be read.
85 /// @param address - The address to be read.
86 static int regionReader(void* arg, uint8_t* byte, uint64_t address) {
87 MemoryObject* region = static_cast<MemoryObject*>(arg);
88 return region->readByte(address, byte);
91 /// logger - a callback function that wraps the operator<< method from
94 /// @param arg - The generic callback parameter. This should be a pointe
96 /// @param log - A string to be logged. logger() adds a newline.
97 static void logger(void* arg, const char* log) {
101 raw_ostream &vStream = *(static_cast<raw_ostream*>(arg));
102 vStream << log << "\n";
106 // Public interface for the disassembler
109 MCDisassembler::DecodeStatus
110 X86GenericDisassembler::getInstruction(MCInst &instr,
112 const MemoryObject ®ion,
114 raw_ostream &vStream) const {
115 InternalInstruction internalInstr;
117 int ret = decodeInstruction(&internalInstr,
126 size = internalInstr.readerCursor - address;
130 size = internalInstr.length;
131 return (!translateInstruction(instr, internalInstr)) ? Success : Fail;
136 // Private code that translates from struct InternalInstructions to MCInsts.
139 /// translateRegister - Translates an internal register to the appropriate LLVM
140 /// register, and appends it as an operand to an MCInst.
142 /// @param mcInst - The MCInst to append to.
143 /// @param reg - The Reg to append.
144 static void translateRegister(MCInst &mcInst, Reg reg) {
145 #define ENTRY(x) X86::x,
146 uint8_t llvmRegnums[] = {
152 uint8_t llvmRegnum = llvmRegnums[reg];
153 mcInst.addOperand(MCOperand::CreateReg(llvmRegnum));
156 /// translateImmediate - Appends an immediate operand to an MCInst.
158 /// @param mcInst - The MCInst to append to.
159 /// @param immediate - The immediate value to append.
160 /// @param operand - The operand, as stored in the descriptor table.
161 /// @param insn - The internal instruction.
162 static void translateImmediate(MCInst &mcInst, uint64_t immediate,
163 const OperandSpecifier &operand,
164 InternalInstruction &insn) {
165 // Sign-extend the immediate if necessary.
167 OperandType type = operand.type;
169 if (type == TYPE_RELv) {
170 switch (insn.displacementSize) {
192 immediate |= ~(0xffull);
195 if(immediate & 0x8000)
196 immediate |= ~(0xffffull);
201 if(immediate & 0x80000000)
202 immediate |= ~(0xffffffffull);
206 // operand is 64 bits wide. Do nothing.
210 mcInst.addOperand(MCOperand::CreateImm(immediate));
213 /// translateRMRegister - Translates a register stored in the R/M field of the
214 /// ModR/M byte to its LLVM equivalent and appends it to an MCInst.
215 /// @param mcInst - The MCInst to append to.
216 /// @param insn - The internal instruction to extract the R/M field
218 /// @return - 0 on success; -1 otherwise
219 static bool translateRMRegister(MCInst &mcInst,
220 InternalInstruction &insn) {
221 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
222 debug("A R/M register operand may not have a SIB byte");
226 switch (insn.eaBase) {
228 debug("Unexpected EA base register");
231 debug("EA_BASE_NONE for ModR/M base");
233 #define ENTRY(x) case EA_BASE_##x:
236 debug("A R/M register operand may not have a base; "
237 "the operand must be a register.");
241 mcInst.addOperand(MCOperand::CreateReg(X86::x)); break;
249 /// translateRMMemory - Translates a memory operand stored in the Mod and R/M
250 /// fields of an internal instruction (and possibly its SIB byte) to a memory
251 /// operand in LLVM's format, and appends it to an MCInst.
253 /// @param mcInst - The MCInst to append to.
254 /// @param insn - The instruction to extract Mod, R/M, and SIB fields
256 /// @return - 0 on success; nonzero otherwise
257 static bool translateRMMemory(MCInst &mcInst, InternalInstruction &insn) {
258 // Addresses in an MCInst are represented as five operands:
259 // 1. basereg (register) The R/M base, or (if there is a SIB) the
261 // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified
263 // 3. indexreg (register) x86_registerNONE, or (if there is a SIB)
264 // the index (which is multiplied by the
266 // 4. displacement (immediate) 0, or the displacement if there is one
267 // 5. segmentreg (register) x86_registerNONE for now, but could be set
268 // if we have segment overrides
271 MCOperand scaleAmount;
273 MCOperand displacement;
274 MCOperand segmentReg;
276 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
277 if (insn.sibBase != SIB_BASE_NONE) {
278 switch (insn.sibBase) {
280 debug("Unexpected sibBase");
284 baseReg = MCOperand::CreateReg(X86::x); break;
289 baseReg = MCOperand::CreateReg(0);
292 if (insn.sibIndex != SIB_INDEX_NONE) {
293 switch (insn.sibIndex) {
295 debug("Unexpected sibIndex");
298 case SIB_INDEX_##x: \
299 indexReg = MCOperand::CreateReg(X86::x); break;
305 indexReg = MCOperand::CreateReg(0);
308 scaleAmount = MCOperand::CreateImm(insn.sibScale);
310 switch (insn.eaBase) {
312 if (insn.eaDisplacement == EA_DISP_NONE) {
313 debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
316 if (insn.mode == MODE_64BIT)
317 baseReg = MCOperand::CreateReg(X86::RIP); // Section 2.2.1.6
319 baseReg = MCOperand::CreateReg(0);
321 indexReg = MCOperand::CreateReg(0);
324 baseReg = MCOperand::CreateReg(X86::BX);
325 indexReg = MCOperand::CreateReg(X86::SI);
328 baseReg = MCOperand::CreateReg(X86::BX);
329 indexReg = MCOperand::CreateReg(X86::DI);
332 baseReg = MCOperand::CreateReg(X86::BP);
333 indexReg = MCOperand::CreateReg(X86::SI);
336 baseReg = MCOperand::CreateReg(X86::BP);
337 indexReg = MCOperand::CreateReg(X86::DI);
340 indexReg = MCOperand::CreateReg(0);
341 switch (insn.eaBase) {
343 debug("Unexpected eaBase");
345 // Here, we will use the fill-ins defined above. However,
346 // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and
347 // sib and sib64 were handled in the top-level if, so they're only
348 // placeholders to keep the compiler happy.
351 baseReg = MCOperand::CreateReg(X86::x); break;
354 #define ENTRY(x) case EA_REG_##x:
357 debug("A R/M memory operand may not be a register; "
358 "the base field must be a base.");
363 scaleAmount = MCOperand::CreateImm(1);
366 displacement = MCOperand::CreateImm(insn.displacement);
368 static const uint8_t segmentRegnums[SEG_OVERRIDE_max] = {
369 0, // SEG_OVERRIDE_NONE
378 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
380 mcInst.addOperand(baseReg);
381 mcInst.addOperand(scaleAmount);
382 mcInst.addOperand(indexReg);
383 mcInst.addOperand(displacement);
384 mcInst.addOperand(segmentReg);
388 /// translateRM - Translates an operand stored in the R/M (and possibly SIB)
389 /// byte of an instruction to LLVM form, and appends it to an MCInst.
391 /// @param mcInst - The MCInst to append to.
392 /// @param operand - The operand, as stored in the descriptor table.
393 /// @param insn - The instruction to extract Mod, R/M, and SIB fields
395 /// @return - 0 on success; nonzero otherwise
396 static bool translateRM(MCInst &mcInst, const OperandSpecifier &operand,
397 InternalInstruction &insn) {
398 switch (operand.type) {
400 debug("Unexpected type for a R/M operand");
416 case TYPE_CONTROLREG:
417 return translateRMRegister(mcInst, insn);
437 return translateRMMemory(mcInst, insn);
441 /// translateFPRegister - Translates a stack position on the FPU stack to its
442 /// LLVM form, and appends it to an MCInst.
444 /// @param mcInst - The MCInst to append to.
445 /// @param stackPos - The stack position to translate.
446 /// @return - 0 on success; nonzero otherwise.
447 static bool translateFPRegister(MCInst &mcInst,
450 debug("Invalid FP stack position");
454 mcInst.addOperand(MCOperand::CreateReg(X86::ST0 + stackPos));
459 /// translateOperand - Translates an operand stored in an internal instruction
460 /// to LLVM's format and appends it to an MCInst.
462 /// @param mcInst - The MCInst to append to.
463 /// @param operand - The operand, as stored in the descriptor table.
464 /// @param insn - The internal instruction.
465 /// @return - false on success; true otherwise.
466 static bool translateOperand(MCInst &mcInst, const OperandSpecifier &operand,
467 InternalInstruction &insn) {
468 switch (operand.encoding) {
470 debug("Unhandled operand encoding during translation");
473 translateRegister(mcInst, insn.reg);
476 return translateRM(mcInst, operand, insn);
483 debug("Translation of code offsets isn't supported.");
491 translateImmediate(mcInst,
492 insn.immediates[insn.numImmediatesTranslated++],
500 translateRegister(mcInst, insn.opcodeRegister);
503 return translateFPRegister(mcInst, insn.opcodeModifier);
505 translateRegister(mcInst, insn.opcodeRegister);
508 translateRegister(mcInst, insn.vvvv);
511 return translateOperand(mcInst,
512 insn.spec->operands[operand.type - TYPE_DUP0],
517 /// translateInstruction - Translates an internal instruction and all its
518 /// operands to an MCInst.
520 /// @param mcInst - The MCInst to populate with the instruction's data.
521 /// @param insn - The internal instruction.
522 /// @return - false on success; true otherwise.
523 static bool translateInstruction(MCInst &mcInst,
524 InternalInstruction &insn) {
526 debug("Instruction has no specification");
530 mcInst.setOpcode(insn.instructionID);
534 insn.numImmediatesTranslated = 0;
536 for (index = 0; index < X86_MAX_OPERANDS; ++index) {
537 if (insn.spec->operands[index].encoding != ENCODING_NONE) {
538 if (translateOperand(mcInst, insn.spec->operands[index], insn)) {
547 static MCDisassembler *createX86_32Disassembler(const Target &T) {
548 return new X86Disassembler::X86_32Disassembler;
551 static MCDisassembler *createX86_64Disassembler(const Target &T) {
552 return new X86Disassembler::X86_64Disassembler;
555 extern "C" void LLVMInitializeX86Disassembler() {
556 // Register the disassembler.
557 TargetRegistry::RegisterMCDisassembler(TheX86_32Target,
558 createX86_32Disassembler);
559 TargetRegistry::RegisterMCDisassembler(TheX86_64Target,
560 createX86_64Disassembler);