blob: 99617e7a40c9185681b86c2a7d5c3cccc7098e2f [file] [log] [blame]
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +00001//===- X86Disassembler.cpp - Disassembler for x86 and x86_64 ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Sean Callanan8ed9f512009-12-19 02:59:52 +00009//
10// This file is part of the X86 Disassembler.
11// It contains code to translate the data produced by the decoder into
12// MCInsts.
13// Documentation for the disassembler can be found in X86Disassembler.h.
14//
15//===----------------------------------------------------------------------===//
16
17#include "X86Disassembler.h"
18#include "X86DisassemblerDecoder.h"
19#include "X86InstrInfo.h"
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +000020
21#include "llvm/MC/MCDisassembler.h"
Sean Callanan8ed9f512009-12-19 02:59:52 +000022#include "llvm/MC/MCDisassembler.h"
23#include "llvm/MC/MCInst.h"
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +000024#include "llvm/Target/TargetRegistry.h"
Sean Callanan8ed9f512009-12-19 02:59:52 +000025#include "llvm/Support/MemoryObject.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/raw_ostream.h"
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +000028using namespace llvm;
Sean Callanan8ed9f512009-12-19 02:59:52 +000029using namespace llvm::X86Disassembler;
30
31namespace llvm {
32
33// Fill-ins to make the compiler happy. These constants are never actually
34// assigned; they are just filler to make an automatically-generated switch
35// statement work.
36namespace X86 {
37 enum {
38 BX_SI = 500,
39 BX_DI = 501,
40 BP_SI = 502,
41 BP_DI = 503,
42 sib = 504,
43 sib64 = 505
44 };
45}
46
47}
48
49static void translateInstruction(MCInst &target,
50 InternalInstruction &source);
51
52X86GenericDisassembler::X86GenericDisassembler(DisassemblerMode mode) :
53 MCDisassembler(),
54 fMode(mode) {
55}
56
57X86GenericDisassembler::~X86GenericDisassembler() {
58}
59
60/// regionReader - a callback function that wraps the readByte method from
61/// MemoryObject.
62///
63/// @param arg - The generic callback parameter. In this case, this should
64/// be a pointer to a MemoryObject.
65/// @param byte - A pointer to the byte to be read.
66/// @param address - The address to be read.
67static int regionReader(void* arg, uint8_t* byte, uint64_t address) {
68 MemoryObject* region = static_cast<MemoryObject*>(arg);
69 return region->readByte(address, byte);
70}
71
72/// logger - a callback function that wraps the operator<< method from
73/// raw_ostream.
74///
75/// @param arg - The generic callback parameter. This should be a pointe
76/// to a raw_ostream.
77/// @param log - A string to be logged. logger() adds a newline.
78static void logger(void* arg, const char* log) {
79 if (!arg)
80 return;
81
82 raw_ostream &vStream = *(static_cast<raw_ostream*>(arg));
83 vStream << log << "\n";
84}
85
86//
87// Public interface for the disassembler
88//
89
90bool X86GenericDisassembler::getInstruction(MCInst &instr,
91 uint64_t &size,
92 const MemoryObject &region,
93 uint64_t address,
94 raw_ostream &vStream) const {
95 InternalInstruction internalInstr;
96
97 int ret = decodeInstruction(&internalInstr,
98 regionReader,
99 (void*)&region,
100 logger,
101 (void*)&vStream,
102 address,
103 fMode);
104
105 if(ret) {
106 size = internalInstr.readerCursor - address;
107 return false;
108 }
109 else {
110 size = internalInstr.length;
111 translateInstruction(instr, internalInstr);
112 return true;
113 }
114}
115
116//
117// Private code that translates from struct InternalInstructions to MCInsts.
118//
119
120/// translateRegister - Translates an internal register to the appropriate LLVM
121/// register, and appends it as an operand to an MCInst.
122///
123/// @param mcInst - The MCInst to append to.
124/// @param reg - The Reg to append.
125static void translateRegister(MCInst &mcInst, Reg reg) {
126#define ENTRY(x) X86::x,
127 uint8_t llvmRegnums[] = {
128 ALL_REGS
129 0
130 };
131#undef ENTRY
132
133 uint8_t llvmRegnum = llvmRegnums[reg];
134 mcInst.addOperand(MCOperand::CreateReg(llvmRegnum));
135}
136
137/// translateImmediate - Appends an immediate operand to an MCInst.
138///
139/// @param mcInst - The MCInst to append to.
140/// @param immediate - The immediate value to append.
141static void translateImmediate(MCInst &mcInst, uint64_t immediate) {
142 mcInst.addOperand(MCOperand::CreateImm(immediate));
143}
144
145/// translateRMRegister - Translates a register stored in the R/M field of the
146/// ModR/M byte to its LLVM equivalent and appends it to an MCInst.
147/// @param mcInst - The MCInst to append to.
148/// @param insn - The internal instruction to extract the R/M field
149/// from.
150static void translateRMRegister(MCInst &mcInst,
151 InternalInstruction &insn) {
152 assert(insn.eaBase != EA_BASE_sib && insn.eaBase != EA_BASE_sib64 &&
153 "A R/M register operand may not have a SIB byte");
154
155 switch (insn.eaBase) {
156 case EA_BASE_NONE:
157 llvm_unreachable("EA_BASE_NONE for ModR/M base");
158 break;
159#define ENTRY(x) case EA_BASE_##x:
160 ALL_EA_BASES
161#undef ENTRY
162 llvm_unreachable("A R/M register operand may not have a base; "
163 "the operand must be a register.");
164 break;
165#define ENTRY(x) \
166 case EA_REG_##x: \
167 mcInst.addOperand(MCOperand::CreateReg(X86::x)); break;
168 ALL_REGS
169#undef ENTRY
170 default:
171 llvm_unreachable("Unexpected EA base register");
172 }
173}
174
175/// translateRMMemory - Translates a memory operand stored in the Mod and R/M
176/// fields of an internal instruction (and possibly its SIB byte) to a memory
177/// operand in LLVM's format, and appends it to an MCInst.
178///
179/// @param mcInst - The MCInst to append to.
180/// @param insn - The instruction to extract Mod, R/M, and SIB fields
181/// from.
182static void translateRMMemory(MCInst &mcInst,
183 InternalInstruction &insn) {
184 // Addresses in an MCInst are represented as five operands:
185 // 1. basereg (register) The R/M base, or (if there is a SIB) the
186 // SIB base
187 // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified
188 // scale amount
189 // 3. indexreg (register) x86_registerNONE, or (if there is a SIB)
190 // the index (which is multiplied by the
191 // scale amount)
192 // 4. displacement (immediate) 0, or the displacement if there is one
193 // 5. segmentreg (register) x86_registerNONE for now, but could be set
194 // if we have segment overrides
195
196 MCOperand baseReg;
197 MCOperand scaleAmount;
198 MCOperand indexReg;
199 MCOperand displacement;
200 MCOperand segmentReg;
201
202 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
203 if (insn.sibBase != SIB_BASE_NONE) {
204 switch (insn.sibBase) {
205 default:
206 llvm_unreachable("Unexpected sibBase");
207#define ENTRY(x) \
208 case SIB_BASE_##x: \
209 baseReg = MCOperand::CreateReg(X86::x); break;
210 ALL_SIB_BASES
211#undef ENTRY
212 }
213 } else {
214 baseReg = MCOperand::CreateReg(0);
215 }
216
217 if (insn.sibIndex != SIB_INDEX_NONE) {
218 switch (insn.sibIndex) {
219 default:
220 llvm_unreachable("Unexpected sibIndex");
221#define ENTRY(x) \
222 case SIB_INDEX_##x: \
223 indexReg = MCOperand::CreateReg(X86::x); break;
224 EA_BASES_32BIT
225 EA_BASES_64BIT
226#undef ENTRY
227 }
228 } else {
229 indexReg = MCOperand::CreateReg(0);
230 }
231
232 scaleAmount = MCOperand::CreateImm(insn.sibScale);
233 } else {
234 switch (insn.eaBase) {
235 case EA_BASE_NONE:
236 assert(insn.eaDisplacement != EA_DISP_NONE &&
237 "EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
238
239 if (insn.mode == MODE_64BIT)
240 baseReg = MCOperand::CreateReg(X86::RIP); // Section 2.2.1.6
241 else
242 baseReg = MCOperand::CreateReg(0);
243
244 indexReg = MCOperand::CreateReg(0);
245 break;
246 case EA_BASE_BX_SI:
247 baseReg = MCOperand::CreateReg(X86::BX);
248 indexReg = MCOperand::CreateReg(X86::SI);
249 break;
250 case EA_BASE_BX_DI:
251 baseReg = MCOperand::CreateReg(X86::BX);
252 indexReg = MCOperand::CreateReg(X86::DI);
253 break;
254 case EA_BASE_BP_SI:
255 baseReg = MCOperand::CreateReg(X86::BP);
256 indexReg = MCOperand::CreateReg(X86::SI);
257 break;
258 case EA_BASE_BP_DI:
259 baseReg = MCOperand::CreateReg(X86::BP);
260 indexReg = MCOperand::CreateReg(X86::DI);
261 break;
262 default:
263 indexReg = MCOperand::CreateReg(0);
264 switch (insn.eaBase) {
265 default:
266 llvm_unreachable("Unexpected eaBase");
267 break;
268 // Here, we will use the fill-ins defined above. However,
269 // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and
270 // sib and sib64 were handled in the top-level if, so they're only
271 // placeholders to keep the compiler happy.
272#define ENTRY(x) \
273 case EA_BASE_##x: \
274 baseReg = MCOperand::CreateReg(X86::x); break;
275 ALL_EA_BASES
276#undef ENTRY
277#define ENTRY(x) case EA_REG_##x:
278 ALL_REGS
279#undef ENTRY
280 llvm_unreachable("A R/M memory operand may not be a register; "
281 "the base field must be a base.");
282 break;
283 }
284 }
285 }
286
287 displacement = MCOperand::CreateImm(insn.displacement);
288
289 static const uint8_t segmentRegnums[SEG_OVERRIDE_max] = {
290 0, // SEG_OVERRIDE_NONE
291 X86::CS,
292 X86::SS,
293 X86::DS,
294 X86::ES,
295 X86::FS,
296 X86::GS
297 };
298
299 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
300
301 mcInst.addOperand(baseReg);
302 mcInst.addOperand(scaleAmount);
303 mcInst.addOperand(indexReg);
304 mcInst.addOperand(displacement);
305 mcInst.addOperand(segmentReg);
306}
307
308/// translateRM - Translates an operand stored in the R/M (and possibly SIB)
309/// byte of an instruction to LLVM form, and appends it to an MCInst.
310///
311/// @param mcInst - The MCInst to append to.
312/// @param operand - The operand, as stored in the descriptor table.
313/// @param insn - The instruction to extract Mod, R/M, and SIB fields
314/// from.
315static void translateRM(MCInst &mcInst,
316 OperandSpecifier &operand,
317 InternalInstruction &insn) {
318 switch (operand.type) {
319 default:
320 llvm_unreachable("Unexpected type for a R/M operand");
321 case TYPE_R8:
322 case TYPE_R16:
323 case TYPE_R32:
324 case TYPE_R64:
325 case TYPE_Rv:
326 case TYPE_MM:
327 case TYPE_MM32:
328 case TYPE_MM64:
329 case TYPE_XMM:
330 case TYPE_XMM32:
331 case TYPE_XMM64:
332 case TYPE_XMM128:
333 case TYPE_DEBUGREG:
334 case TYPE_CR32:
335 case TYPE_CR64:
336 translateRMRegister(mcInst, insn);
337 break;
338 case TYPE_M:
339 case TYPE_M8:
340 case TYPE_M16:
341 case TYPE_M32:
342 case TYPE_M64:
343 case TYPE_M128:
344 case TYPE_M512:
345 case TYPE_Mv:
346 case TYPE_M32FP:
347 case TYPE_M64FP:
348 case TYPE_M80FP:
349 case TYPE_M16INT:
350 case TYPE_M32INT:
351 case TYPE_M64INT:
352 case TYPE_M1616:
353 case TYPE_M1632:
354 case TYPE_M1664:
355 translateRMMemory(mcInst, insn);
356 break;
357 }
358}
359
360/// translateFPRegister - Translates a stack position on the FPU stack to its
361/// LLVM form, and appends it to an MCInst.
362///
363/// @param mcInst - The MCInst to append to.
364/// @param stackPos - The stack position to translate.
365static void translateFPRegister(MCInst &mcInst,
366 uint8_t stackPos) {
367 assert(stackPos < 8 && "Invalid FP stack position");
368
369 mcInst.addOperand(MCOperand::CreateReg(X86::ST0 + stackPos));
370}
371
372/// translateOperand - Translates an operand stored in an internal instruction
373/// to LLVM's format and appends it to an MCInst.
374///
375/// @param mcInst - The MCInst to append to.
376/// @param operand - The operand, as stored in the descriptor table.
377/// @param insn - The internal instruction.
378static void translateOperand(MCInst &mcInst,
379 OperandSpecifier &operand,
380 InternalInstruction &insn) {
381 switch (operand.encoding) {
382 default:
383 llvm_unreachable("Unhandled operand encoding during translation");
384 case ENCODING_REG:
385 translateRegister(mcInst, insn.reg);
386 break;
387 case ENCODING_RM:
388 translateRM(mcInst, operand, insn);
389 break;
390 case ENCODING_CB:
391 case ENCODING_CW:
392 case ENCODING_CD:
393 case ENCODING_CP:
394 case ENCODING_CO:
395 case ENCODING_CT:
396 llvm_unreachable("Translation of code offsets isn't supported.");
397 case ENCODING_IB:
398 case ENCODING_IW:
399 case ENCODING_ID:
400 case ENCODING_IO:
401 case ENCODING_Iv:
402 case ENCODING_Ia:
403 translateImmediate(mcInst,
404 insn.immediates[insn.numImmediatesTranslated++]);
405 break;
406 case ENCODING_RB:
407 case ENCODING_RW:
408 case ENCODING_RD:
409 case ENCODING_RO:
410 translateRegister(mcInst, insn.opcodeRegister);
411 break;
412 case ENCODING_I:
413 translateFPRegister(mcInst, insn.opcodeModifier);
414 break;
415 case ENCODING_Rv:
416 translateRegister(mcInst, insn.opcodeRegister);
417 break;
418 case ENCODING_DUP:
419 translateOperand(mcInst,
420 insn.spec->operands[operand.type - TYPE_DUP0],
421 insn);
422 break;
423 }
424}
425
426/// translateInstruction - Translates an internal instruction and all its
427/// operands to an MCInst.
428///
429/// @param mcInst - The MCInst to populate with the instruction's data.
430/// @param insn - The internal instruction.
431static void translateInstruction(MCInst &mcInst,
432 InternalInstruction &insn) {
433 assert(insn.spec);
434
435 mcInst.setOpcode(insn.instructionID);
436
437 int index;
438
439 insn.numImmediatesTranslated = 0;
440
441 for (index = 0; index < X86_MAX_OPERANDS; ++index) {
442 if (insn.spec->operands[index].encoding != ENCODING_NONE)
443 translateOperand(mcInst, insn.spec->operands[index], insn);
444 }
445}
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +0000446
447static const MCDisassembler *createX86_32Disassembler(const Target &T) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000448 return new X86Disassembler::X86_32Disassembler;
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +0000449}
450
451static const MCDisassembler *createX86_64Disassembler(const Target &T) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000452 return new X86Disassembler::X86_64Disassembler;
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +0000453}
454
455extern "C" void LLVMInitializeX86Disassembler() {
456 // Register the disassembler.
457 TargetRegistry::RegisterMCDisassembler(TheX86_32Target,
458 createX86_32Disassembler);
459 TargetRegistry::RegisterMCDisassembler(TheX86_64Target,
460 createX86_64Disassembler);
461}