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/Target/TargetRegistry.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/MemoryObject.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 bool X86GenericDisassembler::getInstruction(MCInst
&instr
,
111 const MemoryObject
®ion
,
113 raw_ostream
&vStream
) const {
114 InternalInstruction internalInstr
;
116 int ret
= decodeInstruction(&internalInstr
,
125 size
= internalInstr
.readerCursor
- address
;
129 size
= internalInstr
.length
;
130 return !translateInstruction(instr
, internalInstr
);
135 // Private code that translates from struct InternalInstructions to MCInsts.
138 /// translateRegister - Translates an internal register to the appropriate LLVM
139 /// register, and appends it as an operand to an MCInst.
141 /// @param mcInst - The MCInst to append to.
142 /// @param reg - The Reg to append.
143 static void translateRegister(MCInst
&mcInst
, Reg reg
) {
144 #define ENTRY(x) X86::x,
145 uint8_t llvmRegnums
[] = {
151 uint8_t llvmRegnum
= llvmRegnums
[reg
];
152 mcInst
.addOperand(MCOperand::CreateReg(llvmRegnum
));
155 /// translateImmediate - Appends an immediate operand to an MCInst.
157 /// @param mcInst - The MCInst to append to.
158 /// @param immediate - The immediate value to append.
159 /// @param operand - The operand, as stored in the descriptor table.
160 /// @param insn - The internal instruction.
161 static void translateImmediate(MCInst
&mcInst
, uint64_t immediate
,
162 const OperandSpecifier
&operand
,
163 InternalInstruction
&insn
) {
164 // Sign-extend the immediate if necessary.
166 OperandType type
= operand
.type
;
168 if (type
== TYPE_RELv
) {
169 switch (insn
.displacementSize
) {
191 immediate
|= ~(0xffull
);
194 if(immediate
& 0x8000)
195 immediate
|= ~(0xffffull
);
200 if(immediate
& 0x80000000)
201 immediate
|= ~(0xffffffffull
);
205 // operand is 64 bits wide. Do nothing.
209 mcInst
.addOperand(MCOperand::CreateImm(immediate
));
212 /// translateRMRegister - Translates a register stored in the R/M field of the
213 /// ModR/M byte to its LLVM equivalent and appends it to an MCInst.
214 /// @param mcInst - The MCInst to append to.
215 /// @param insn - The internal instruction to extract the R/M field
217 /// @return - 0 on success; -1 otherwise
218 static bool translateRMRegister(MCInst
&mcInst
,
219 InternalInstruction
&insn
) {
220 if (insn
.eaBase
== EA_BASE_sib
|| insn
.eaBase
== EA_BASE_sib64
) {
221 debug("A R/M register operand may not have a SIB byte");
225 switch (insn
.eaBase
) {
227 debug("Unexpected EA base register");
230 debug("EA_BASE_NONE for ModR/M base");
232 #define ENTRY(x) case EA_BASE_##x:
235 debug("A R/M register operand may not have a base; "
236 "the operand must be a register.");
240 mcInst.addOperand(MCOperand::CreateReg(X86::x)); break;
248 /// translateRMMemory - Translates a memory operand stored in the Mod and R/M
249 /// fields of an internal instruction (and possibly its SIB byte) to a memory
250 /// operand in LLVM's format, and appends it to an MCInst.
252 /// @param mcInst - The MCInst to append to.
253 /// @param insn - The instruction to extract Mod, R/M, and SIB fields
255 /// @return - 0 on success; nonzero otherwise
256 static bool translateRMMemory(MCInst
&mcInst
, InternalInstruction
&insn
) {
257 // Addresses in an MCInst are represented as five operands:
258 // 1. basereg (register) The R/M base, or (if there is a SIB) the
260 // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified
262 // 3. indexreg (register) x86_registerNONE, or (if there is a SIB)
263 // the index (which is multiplied by the
265 // 4. displacement (immediate) 0, or the displacement if there is one
266 // 5. segmentreg (register) x86_registerNONE for now, but could be set
267 // if we have segment overrides
270 MCOperand scaleAmount
;
272 MCOperand displacement
;
273 MCOperand segmentReg
;
275 if (insn
.eaBase
== EA_BASE_sib
|| insn
.eaBase
== EA_BASE_sib64
) {
276 if (insn
.sibBase
!= SIB_BASE_NONE
) {
277 switch (insn
.sibBase
) {
279 debug("Unexpected sibBase");
283 baseReg = MCOperand::CreateReg(X86::x); break;
288 baseReg
= MCOperand::CreateReg(0);
291 if (insn
.sibIndex
!= SIB_INDEX_NONE
) {
292 switch (insn
.sibIndex
) {
294 debug("Unexpected sibIndex");
297 case SIB_INDEX_##x: \
298 indexReg = MCOperand::CreateReg(X86::x); break;
304 indexReg
= MCOperand::CreateReg(0);
307 scaleAmount
= MCOperand::CreateImm(insn
.sibScale
);
309 switch (insn
.eaBase
) {
311 if (insn
.eaDisplacement
== EA_DISP_NONE
) {
312 debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
315 if (insn
.mode
== MODE_64BIT
)
316 baseReg
= MCOperand::CreateReg(X86::RIP
); // Section 2.2.1.6
318 baseReg
= MCOperand::CreateReg(0);
320 indexReg
= MCOperand::CreateReg(0);
323 baseReg
= MCOperand::CreateReg(X86::BX
);
324 indexReg
= MCOperand::CreateReg(X86::SI
);
327 baseReg
= MCOperand::CreateReg(X86::BX
);
328 indexReg
= MCOperand::CreateReg(X86::DI
);
331 baseReg
= MCOperand::CreateReg(X86::BP
);
332 indexReg
= MCOperand::CreateReg(X86::SI
);
335 baseReg
= MCOperand::CreateReg(X86::BP
);
336 indexReg
= MCOperand::CreateReg(X86::DI
);
339 indexReg
= MCOperand::CreateReg(0);
340 switch (insn
.eaBase
) {
342 debug("Unexpected eaBase");
344 // Here, we will use the fill-ins defined above. However,
345 // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and
346 // sib and sib64 were handled in the top-level if, so they're only
347 // placeholders to keep the compiler happy.
350 baseReg = MCOperand::CreateReg(X86::x); break;
353 #define ENTRY(x) case EA_REG_##x:
356 debug("A R/M memory operand may not be a register; "
357 "the base field must be a base.");
362 scaleAmount
= MCOperand::CreateImm(1);
365 displacement
= MCOperand::CreateImm(insn
.displacement
);
367 static const uint8_t segmentRegnums
[SEG_OVERRIDE_max
] = {
368 0, // SEG_OVERRIDE_NONE
377 segmentReg
= MCOperand::CreateReg(segmentRegnums
[insn
.segmentOverride
]);
379 mcInst
.addOperand(baseReg
);
380 mcInst
.addOperand(scaleAmount
);
381 mcInst
.addOperand(indexReg
);
382 mcInst
.addOperand(displacement
);
383 mcInst
.addOperand(segmentReg
);
387 /// translateRM - Translates an operand stored in the R/M (and possibly SIB)
388 /// byte of an instruction to LLVM form, and appends it to an MCInst.
390 /// @param mcInst - The MCInst to append to.
391 /// @param operand - The operand, as stored in the descriptor table.
392 /// @param insn - The instruction to extract Mod, R/M, and SIB fields
394 /// @return - 0 on success; nonzero otherwise
395 static bool translateRM(MCInst
&mcInst
, const OperandSpecifier
&operand
,
396 InternalInstruction
&insn
) {
397 switch (operand
.type
) {
399 debug("Unexpected type for a R/M operand");
415 case TYPE_CONTROLREG
:
416 return translateRMRegister(mcInst
, insn
);
436 return translateRMMemory(mcInst
, insn
);
440 /// translateFPRegister - Translates a stack position on the FPU stack to its
441 /// LLVM form, and appends it to an MCInst.
443 /// @param mcInst - The MCInst to append to.
444 /// @param stackPos - The stack position to translate.
445 /// @return - 0 on success; nonzero otherwise.
446 static bool translateFPRegister(MCInst
&mcInst
,
449 debug("Invalid FP stack position");
453 mcInst
.addOperand(MCOperand::CreateReg(X86::ST0
+ stackPos
));
458 /// translateOperand - Translates an operand stored in an internal instruction
459 /// to LLVM's format and appends it to an MCInst.
461 /// @param mcInst - The MCInst to append to.
462 /// @param operand - The operand, as stored in the descriptor table.
463 /// @param insn - The internal instruction.
464 /// @return - false on success; true otherwise.
465 static bool translateOperand(MCInst
&mcInst
, const OperandSpecifier
&operand
,
466 InternalInstruction
&insn
) {
467 switch (operand
.encoding
) {
469 debug("Unhandled operand encoding during translation");
472 translateRegister(mcInst
, insn
.reg
);
475 return translateRM(mcInst
, operand
, insn
);
482 debug("Translation of code offsets isn't supported.");
490 translateImmediate(mcInst
,
491 insn
.immediates
[insn
.numImmediatesTranslated
++],
499 translateRegister(mcInst
, insn
.opcodeRegister
);
502 return translateFPRegister(mcInst
, insn
.opcodeModifier
);
504 translateRegister(mcInst
, insn
.opcodeRegister
);
507 translateRegister(mcInst
, insn
.vvvv
);
510 return translateOperand(mcInst
,
511 insn
.spec
->operands
[operand
.type
- TYPE_DUP0
],
516 /// translateInstruction - Translates an internal instruction and all its
517 /// operands to an MCInst.
519 /// @param mcInst - The MCInst to populate with the instruction's data.
520 /// @param insn - The internal instruction.
521 /// @return - false on success; true otherwise.
522 static bool translateInstruction(MCInst
&mcInst
,
523 InternalInstruction
&insn
) {
525 debug("Instruction has no specification");
529 mcInst
.setOpcode(insn
.instructionID
);
533 insn
.numImmediatesTranslated
= 0;
535 for (index
= 0; index
< X86_MAX_OPERANDS
; ++index
) {
536 if (insn
.spec
->operands
[index
].encoding
!= ENCODING_NONE
) {
537 if (translateOperand(mcInst
, insn
.spec
->operands
[index
], insn
)) {
546 static MCDisassembler
*createX86_32Disassembler(const Target
&T
) {
547 return new X86Disassembler::X86_32Disassembler
;
550 static MCDisassembler
*createX86_64Disassembler(const Target
&T
) {
551 return new X86Disassembler::X86_64Disassembler
;
554 extern "C" void LLVMInitializeX86Disassembler() {
555 // Register the disassembler.
556 TargetRegistry::RegisterMCDisassembler(TheX86_32Target
,
557 createX86_32Disassembler
);
558 TargetRegistry::RegisterMCDisassembler(TheX86_64Target
,
559 createX86_64Disassembler
);