blob: 3ea3add1d16d7d9902c85c6abcc79c4cc7f9ff10 [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"
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +000019
Sean Callanan9899f702010-04-13 21:21:57 +000020#include "llvm/MC/EDInstInfo.h"
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +000021#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 Callanana144c3f2010-04-02 21:23:51 +000025#include "llvm/Support/Debug.h"
Sean Callanan8ed9f512009-12-19 02:59:52 +000026#include "llvm/Support/MemoryObject.h"
Sean Callanan8ed9f512009-12-19 02:59:52 +000027#include "llvm/Support/raw_ostream.h"
Sean Callanan0122c902009-12-22 01:11:26 +000028
Evan Cheng73f50d92011-06-27 18:32:37 +000029#define GET_REGINFO_ENUM
30#include "X86GenRegisterInfo.inc"
Sean Callanan9899f702010-04-13 21:21:57 +000031#include "X86GenEDInfo.inc"
Sean Callanan0122c902009-12-22 01:11:26 +000032
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +000033using namespace llvm;
Sean Callanan8ed9f512009-12-19 02:59:52 +000034using namespace llvm::X86Disassembler;
35
Sean Callanana144c3f2010-04-02 21:23:51 +000036void x86DisassemblerDebug(const char *file,
37 unsigned line,
38 const char *s) {
39 dbgs() << file << ":" << line << ": " << s;
40}
41
42#define debug(s) DEBUG(x86DisassemblerDebug(__FILE__, __LINE__, s));
43
Sean Callanan8ed9f512009-12-19 02:59:52 +000044namespace llvm {
45
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
48// statement work.
49namespace X86 {
50 enum {
51 BX_SI = 500,
52 BX_DI = 501,
53 BP_SI = 502,
54 BP_DI = 503,
55 sib = 504,
56 sib64 = 505
57 };
58}
59
Sean Callanan0122c902009-12-22 01:11:26 +000060extern Target TheX86_32Target, TheX86_64Target;
61
Sean Callanan8ed9f512009-12-19 02:59:52 +000062}
63
Sean Callanana144c3f2010-04-02 21:23:51 +000064static bool translateInstruction(MCInst &target,
65 InternalInstruction &source);
Sean Callanan8ed9f512009-12-19 02:59:52 +000066
67X86GenericDisassembler::X86GenericDisassembler(DisassemblerMode mode) :
68 MCDisassembler(),
69 fMode(mode) {
70}
71
72X86GenericDisassembler::~X86GenericDisassembler() {
73}
74
Sean Callanan9899f702010-04-13 21:21:57 +000075EDInstInfo *X86GenericDisassembler::getEDInfo() const {
76 return instInfoX86;
77}
78
Sean Callanan8ed9f512009-12-19 02:59:52 +000079/// regionReader - a callback function that wraps the readByte method from
80/// MemoryObject.
81///
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.
86static int regionReader(void* arg, uint8_t* byte, uint64_t address) {
87 MemoryObject* region = static_cast<MemoryObject*>(arg);
88 return region->readByte(address, byte);
89}
90
91/// logger - a callback function that wraps the operator<< method from
92/// raw_ostream.
93///
94/// @param arg - The generic callback parameter. This should be a pointe
95/// to a raw_ostream.
96/// @param log - A string to be logged. logger() adds a newline.
97static void logger(void* arg, const char* log) {
98 if (!arg)
99 return;
100
101 raw_ostream &vStream = *(static_cast<raw_ostream*>(arg));
102 vStream << log << "\n";
103}
104
105//
106// Public interface for the disassembler
107//
108
Owen Anderson83e3f672011-08-17 17:44:15 +0000109MCDisassembler::DecodeStatus
110X86GenericDisassembler::getInstruction(MCInst &instr,
111 uint64_t &size,
112 const MemoryObject &region,
113 uint64_t address,
114 raw_ostream &vStream) const {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000115 InternalInstruction internalInstr;
116
117 int ret = decodeInstruction(&internalInstr,
118 regionReader,
119 (void*)&region,
120 logger,
121 (void*)&vStream,
122 address,
123 fMode);
124
Sean Callanana144c3f2010-04-02 21:23:51 +0000125 if (ret) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000126 size = internalInstr.readerCursor - address;
Owen Anderson83e3f672011-08-17 17:44:15 +0000127 return Fail;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000128 }
129 else {
130 size = internalInstr.length;
Owen Anderson83e3f672011-08-17 17:44:15 +0000131 return (!translateInstruction(instr, internalInstr)) ? Success : Fail;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000132 }
133}
134
135//
136// Private code that translates from struct InternalInstructions to MCInsts.
137//
138
139/// translateRegister - Translates an internal register to the appropriate LLVM
140/// register, and appends it as an operand to an MCInst.
141///
142/// @param mcInst - The MCInst to append to.
143/// @param reg - The Reg to append.
144static void translateRegister(MCInst &mcInst, Reg reg) {
145#define ENTRY(x) X86::x,
146 uint8_t llvmRegnums[] = {
147 ALL_REGS
148 0
149 };
150#undef ENTRY
151
152 uint8_t llvmRegnum = llvmRegnums[reg];
153 mcInst.addOperand(MCOperand::CreateReg(llvmRegnum));
154}
155
156/// translateImmediate - Appends an immediate operand to an MCInst.
157///
158/// @param mcInst - The MCInst to append to.
159/// @param immediate - The immediate value to append.
Sean Callananbe192dd2010-05-05 22:47:27 +0000160/// @param operand - The operand, as stored in the descriptor table.
161/// @param insn - The internal instruction.
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000162static void translateImmediate(MCInst &mcInst, uint64_t immediate,
163 const OperandSpecifier &operand,
Sean Callananbe192dd2010-05-05 22:47:27 +0000164 InternalInstruction &insn) {
165 // Sign-extend the immediate if necessary.
166
167 OperandType type = operand.type;
168
169 if (type == TYPE_RELv) {
170 switch (insn.displacementSize) {
171 default:
172 break;
Sean Callanan89e59e62011-02-21 21:55:05 +0000173 case 1:
Sean Callananbe192dd2010-05-05 22:47:27 +0000174 type = TYPE_MOFFS8;
175 break;
Sean Callanan89e59e62011-02-21 21:55:05 +0000176 case 2:
Sean Callananbe192dd2010-05-05 22:47:27 +0000177 type = TYPE_MOFFS16;
178 break;
Sean Callanan89e59e62011-02-21 21:55:05 +0000179 case 4:
Sean Callananbe192dd2010-05-05 22:47:27 +0000180 type = TYPE_MOFFS32;
181 break;
Sean Callanan89e59e62011-02-21 21:55:05 +0000182 case 8:
Sean Callananbe192dd2010-05-05 22:47:27 +0000183 type = TYPE_MOFFS64;
184 break;
185 }
186 }
187
188 switch (type) {
189 case TYPE_MOFFS8:
190 case TYPE_REL8:
191 if(immediate & 0x80)
192 immediate |= ~(0xffull);
193 break;
194 case TYPE_MOFFS16:
195 if(immediate & 0x8000)
196 immediate |= ~(0xffffull);
197 break;
198 case TYPE_MOFFS32:
199 case TYPE_REL32:
200 case TYPE_REL64:
201 if(immediate & 0x80000000)
202 immediate |= ~(0xffffffffull);
203 break;
204 case TYPE_MOFFS64:
205 default:
206 // operand is 64 bits wide. Do nothing.
207 break;
208 }
209
Sean Callanan8ed9f512009-12-19 02:59:52 +0000210 mcInst.addOperand(MCOperand::CreateImm(immediate));
211}
212
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
217/// from.
Sean Callanana144c3f2010-04-02 21:23:51 +0000218/// @return - 0 on success; -1 otherwise
219static bool translateRMRegister(MCInst &mcInst,
Sean Callanan8ed9f512009-12-19 02:59:52 +0000220 InternalInstruction &insn) {
Sean Callanana144c3f2010-04-02 21:23:51 +0000221 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
222 debug("A R/M register operand may not have a SIB byte");
223 return true;
224 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000225
226 switch (insn.eaBase) {
Sean Callanana144c3f2010-04-02 21:23:51 +0000227 default:
228 debug("Unexpected EA base register");
229 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000230 case EA_BASE_NONE:
Sean Callanana144c3f2010-04-02 21:23:51 +0000231 debug("EA_BASE_NONE for ModR/M base");
232 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000233#define ENTRY(x) case EA_BASE_##x:
234 ALL_EA_BASES
235#undef ENTRY
Sean Callanana144c3f2010-04-02 21:23:51 +0000236 debug("A R/M register operand may not have a base; "
237 "the operand must be a register.");
238 return true;
239#define ENTRY(x) \
Sean Callanan8ed9f512009-12-19 02:59:52 +0000240 case EA_REG_##x: \
241 mcInst.addOperand(MCOperand::CreateReg(X86::x)); break;
242 ALL_REGS
243#undef ENTRY
Sean Callanan8ed9f512009-12-19 02:59:52 +0000244 }
Sean Callanana144c3f2010-04-02 21:23:51 +0000245
246 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000247}
248
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.
252///
253/// @param mcInst - The MCInst to append to.
254/// @param insn - The instruction to extract Mod, R/M, and SIB fields
255/// from.
Sean Callanana144c3f2010-04-02 21:23:51 +0000256/// @return - 0 on success; nonzero otherwise
Chris Lattner37a746b2010-07-13 04:23:55 +0000257static bool translateRMMemory(MCInst &mcInst, InternalInstruction &insn) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000258 // Addresses in an MCInst are represented as five operands:
259 // 1. basereg (register) The R/M base, or (if there is a SIB) the
260 // SIB base
261 // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified
262 // scale amount
263 // 3. indexreg (register) x86_registerNONE, or (if there is a SIB)
264 // the index (which is multiplied by the
265 // scale amount)
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
269
270 MCOperand baseReg;
271 MCOperand scaleAmount;
272 MCOperand indexReg;
273 MCOperand displacement;
274 MCOperand segmentReg;
275
276 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
277 if (insn.sibBase != SIB_BASE_NONE) {
278 switch (insn.sibBase) {
279 default:
Sean Callanana144c3f2010-04-02 21:23:51 +0000280 debug("Unexpected sibBase");
281 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000282#define ENTRY(x) \
Sean Callanan7fb35a22009-12-22 21:12:55 +0000283 case SIB_BASE_##x: \
Sean Callanan8ed9f512009-12-19 02:59:52 +0000284 baseReg = MCOperand::CreateReg(X86::x); break;
285 ALL_SIB_BASES
286#undef ENTRY
287 }
288 } else {
289 baseReg = MCOperand::CreateReg(0);
290 }
291
292 if (insn.sibIndex != SIB_INDEX_NONE) {
293 switch (insn.sibIndex) {
294 default:
Sean Callanana144c3f2010-04-02 21:23:51 +0000295 debug("Unexpected sibIndex");
296 return true;
Sean Callanan7fb35a22009-12-22 21:12:55 +0000297#define ENTRY(x) \
Sean Callanan8ed9f512009-12-19 02:59:52 +0000298 case SIB_INDEX_##x: \
299 indexReg = MCOperand::CreateReg(X86::x); break;
300 EA_BASES_32BIT
301 EA_BASES_64BIT
302#undef ENTRY
303 }
304 } else {
305 indexReg = MCOperand::CreateReg(0);
306 }
307
308 scaleAmount = MCOperand::CreateImm(insn.sibScale);
309 } else {
310 switch (insn.eaBase) {
311 case EA_BASE_NONE:
Sean Callanana144c3f2010-04-02 21:23:51 +0000312 if (insn.eaDisplacement == EA_DISP_NONE) {
313 debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
314 return true;
315 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000316 if (insn.mode == MODE_64BIT)
317 baseReg = MCOperand::CreateReg(X86::RIP); // Section 2.2.1.6
318 else
319 baseReg = MCOperand::CreateReg(0);
320
321 indexReg = MCOperand::CreateReg(0);
322 break;
323 case EA_BASE_BX_SI:
324 baseReg = MCOperand::CreateReg(X86::BX);
325 indexReg = MCOperand::CreateReg(X86::SI);
326 break;
327 case EA_BASE_BX_DI:
328 baseReg = MCOperand::CreateReg(X86::BX);
329 indexReg = MCOperand::CreateReg(X86::DI);
330 break;
331 case EA_BASE_BP_SI:
332 baseReg = MCOperand::CreateReg(X86::BP);
333 indexReg = MCOperand::CreateReg(X86::SI);
334 break;
335 case EA_BASE_BP_DI:
336 baseReg = MCOperand::CreateReg(X86::BP);
337 indexReg = MCOperand::CreateReg(X86::DI);
338 break;
339 default:
340 indexReg = MCOperand::CreateReg(0);
341 switch (insn.eaBase) {
342 default:
Sean Callanana144c3f2010-04-02 21:23:51 +0000343 debug("Unexpected eaBase");
344 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000345 // 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.
349#define ENTRY(x) \
350 case EA_BASE_##x: \
351 baseReg = MCOperand::CreateReg(X86::x); break;
352 ALL_EA_BASES
353#undef ENTRY
354#define ENTRY(x) case EA_REG_##x:
355 ALL_REGS
356#undef ENTRY
Sean Callanana144c3f2010-04-02 21:23:51 +0000357 debug("A R/M memory operand may not be a register; "
358 "the base field must be a base.");
359 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000360 }
361 }
Sean Callanan7fb35a22009-12-22 21:12:55 +0000362
363 scaleAmount = MCOperand::CreateImm(1);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000364 }
365
366 displacement = MCOperand::CreateImm(insn.displacement);
367
368 static const uint8_t segmentRegnums[SEG_OVERRIDE_max] = {
369 0, // SEG_OVERRIDE_NONE
370 X86::CS,
371 X86::SS,
372 X86::DS,
373 X86::ES,
374 X86::FS,
375 X86::GS
376 };
377
378 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
379
380 mcInst.addOperand(baseReg);
381 mcInst.addOperand(scaleAmount);
382 mcInst.addOperand(indexReg);
383 mcInst.addOperand(displacement);
Chris Lattner37a746b2010-07-13 04:23:55 +0000384 mcInst.addOperand(segmentReg);
Sean Callanana144c3f2010-04-02 21:23:51 +0000385 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000386}
387
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.
390///
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
394/// from.
Sean Callanana144c3f2010-04-02 21:23:51 +0000395/// @return - 0 on success; nonzero otherwise
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000396static bool translateRM(MCInst &mcInst, const OperandSpecifier &operand,
397 InternalInstruction &insn) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000398 switch (operand.type) {
399 default:
Sean Callanana144c3f2010-04-02 21:23:51 +0000400 debug("Unexpected type for a R/M operand");
401 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000402 case TYPE_R8:
403 case TYPE_R16:
404 case TYPE_R32:
405 case TYPE_R64:
406 case TYPE_Rv:
407 case TYPE_MM:
408 case TYPE_MM32:
409 case TYPE_MM64:
410 case TYPE_XMM:
411 case TYPE_XMM32:
412 case TYPE_XMM64:
413 case TYPE_XMM128:
Sean Callanana21e2ea2011-03-15 01:23:15 +0000414 case TYPE_XMM256:
Sean Callanan8ed9f512009-12-19 02:59:52 +0000415 case TYPE_DEBUGREG:
Sean Callanan1a8b7892010-05-06 20:59:00 +0000416 case TYPE_CONTROLREG:
Sean Callanana144c3f2010-04-02 21:23:51 +0000417 return translateRMRegister(mcInst, insn);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000418 case TYPE_M:
419 case TYPE_M8:
420 case TYPE_M16:
421 case TYPE_M32:
422 case TYPE_M64:
423 case TYPE_M128:
Sean Callanana21e2ea2011-03-15 01:23:15 +0000424 case TYPE_M256:
Sean Callanan8ed9f512009-12-19 02:59:52 +0000425 case TYPE_M512:
426 case TYPE_Mv:
427 case TYPE_M32FP:
428 case TYPE_M64FP:
429 case TYPE_M80FP:
430 case TYPE_M16INT:
431 case TYPE_M32INT:
432 case TYPE_M64INT:
433 case TYPE_M1616:
434 case TYPE_M1632:
435 case TYPE_M1664:
Sean Callanan7fb35a22009-12-22 21:12:55 +0000436 case TYPE_LEA:
Chris Lattner37a746b2010-07-13 04:23:55 +0000437 return translateRMMemory(mcInst, insn);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000438 }
439}
440
441/// translateFPRegister - Translates a stack position on the FPU stack to its
442/// LLVM form, and appends it to an MCInst.
443///
444/// @param mcInst - The MCInst to append to.
445/// @param stackPos - The stack position to translate.
Sean Callanana144c3f2010-04-02 21:23:51 +0000446/// @return - 0 on success; nonzero otherwise.
447static bool translateFPRegister(MCInst &mcInst,
448 uint8_t stackPos) {
449 if (stackPos >= 8) {
450 debug("Invalid FP stack position");
451 return true;
452 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000453
454 mcInst.addOperand(MCOperand::CreateReg(X86::ST0 + stackPos));
Sean Callanana144c3f2010-04-02 21:23:51 +0000455
456 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000457}
458
459/// translateOperand - Translates an operand stored in an internal instruction
460/// to LLVM's format and appends it to an MCInst.
461///
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.
Sean Callanana144c3f2010-04-02 21:23:51 +0000465/// @return - false on success; true otherwise.
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000466static bool translateOperand(MCInst &mcInst, const OperandSpecifier &operand,
467 InternalInstruction &insn) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000468 switch (operand.encoding) {
469 default:
Sean Callanana144c3f2010-04-02 21:23:51 +0000470 debug("Unhandled operand encoding during translation");
471 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000472 case ENCODING_REG:
473 translateRegister(mcInst, insn.reg);
Sean Callanana144c3f2010-04-02 21:23:51 +0000474 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000475 case ENCODING_RM:
Sean Callanana144c3f2010-04-02 21:23:51 +0000476 return translateRM(mcInst, operand, insn);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000477 case ENCODING_CB:
478 case ENCODING_CW:
479 case ENCODING_CD:
480 case ENCODING_CP:
481 case ENCODING_CO:
482 case ENCODING_CT:
Sean Callanana144c3f2010-04-02 21:23:51 +0000483 debug("Translation of code offsets isn't supported.");
484 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000485 case ENCODING_IB:
486 case ENCODING_IW:
487 case ENCODING_ID:
488 case ENCODING_IO:
489 case ENCODING_Iv:
490 case ENCODING_Ia:
Sean Callananbe192dd2010-05-05 22:47:27 +0000491 translateImmediate(mcInst,
492 insn.immediates[insn.numImmediatesTranslated++],
493 operand,
494 insn);
Sean Callanana144c3f2010-04-02 21:23:51 +0000495 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000496 case ENCODING_RB:
497 case ENCODING_RW:
498 case ENCODING_RD:
499 case ENCODING_RO:
500 translateRegister(mcInst, insn.opcodeRegister);
Sean Callanana144c3f2010-04-02 21:23:51 +0000501 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000502 case ENCODING_I:
Sean Callanana144c3f2010-04-02 21:23:51 +0000503 return translateFPRegister(mcInst, insn.opcodeModifier);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000504 case ENCODING_Rv:
505 translateRegister(mcInst, insn.opcodeRegister);
Sean Callanana144c3f2010-04-02 21:23:51 +0000506 return false;
Sean Callanana21e2ea2011-03-15 01:23:15 +0000507 case ENCODING_VVVV:
508 translateRegister(mcInst, insn.vvvv);
509 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000510 case ENCODING_DUP:
Sean Callanana144c3f2010-04-02 21:23:51 +0000511 return translateOperand(mcInst,
512 insn.spec->operands[operand.type - TYPE_DUP0],
513 insn);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000514 }
515}
516
517/// translateInstruction - Translates an internal instruction and all its
518/// operands to an MCInst.
519///
520/// @param mcInst - The MCInst to populate with the instruction's data.
521/// @param insn - The internal instruction.
Sean Callanana144c3f2010-04-02 21:23:51 +0000522/// @return - false on success; true otherwise.
523static bool translateInstruction(MCInst &mcInst,
524 InternalInstruction &insn) {
525 if (!insn.spec) {
526 debug("Instruction has no specification");
527 return true;
528 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000529
530 mcInst.setOpcode(insn.instructionID);
531
532 int index;
533
534 insn.numImmediatesTranslated = 0;
535
536 for (index = 0; index < X86_MAX_OPERANDS; ++index) {
Sean Callanana144c3f2010-04-02 21:23:51 +0000537 if (insn.spec->operands[index].encoding != ENCODING_NONE) {
538 if (translateOperand(mcInst, insn.spec->operands[index], insn)) {
539 return true;
540 }
541 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000542 }
Sean Callanana144c3f2010-04-02 21:23:51 +0000543
544 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000545}
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +0000546
Daniel Dunbar5d067fe2010-03-20 22:36:22 +0000547static MCDisassembler *createX86_32Disassembler(const Target &T) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000548 return new X86Disassembler::X86_32Disassembler;
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +0000549}
550
Daniel Dunbar5d067fe2010-03-20 22:36:22 +0000551static MCDisassembler *createX86_64Disassembler(const Target &T) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000552 return new X86Disassembler::X86_64Disassembler;
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +0000553}
554
555extern "C" void LLVMInitializeX86Disassembler() {
556 // Register the disassembler.
557 TargetRegistry::RegisterMCDisassembler(TheX86_32Target,
558 createX86_32Disassembler);
559 TargetRegistry::RegisterMCDisassembler(TheX86_64Target,
560 createX86_64Disassembler);
561}