blob: d8a105e7e9d26f818f748a41f38b0919f5ff4dd8 [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
Douglas Gregor3dac3b72009-12-22 17:25:11 +000029#include "X86GenRegisterNames.inc"
Sean Callanan9899f702010-04-13 21:21:57 +000030#include "X86GenEDInfo.inc"
Sean Callanan0122c902009-12-22 01:11:26 +000031
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +000032using namespace llvm;
Sean Callanan8ed9f512009-12-19 02:59:52 +000033using namespace llvm::X86Disassembler;
34
Sean Callanana144c3f2010-04-02 21:23:51 +000035void x86DisassemblerDebug(const char *file,
36 unsigned line,
37 const char *s) {
38 dbgs() << file << ":" << line << ": " << s;
39}
40
41#define debug(s) DEBUG(x86DisassemblerDebug(__FILE__, __LINE__, s));
42
Sean Callanan8ed9f512009-12-19 02:59:52 +000043namespace llvm {
44
45// Fill-ins to make the compiler happy. These constants are never actually
46// assigned; they are just filler to make an automatically-generated switch
47// statement work.
48namespace X86 {
49 enum {
50 BX_SI = 500,
51 BX_DI = 501,
52 BP_SI = 502,
53 BP_DI = 503,
54 sib = 504,
55 sib64 = 505
56 };
57}
58
Sean Callanan0122c902009-12-22 01:11:26 +000059extern Target TheX86_32Target, TheX86_64Target;
60
Sean Callanan8ed9f512009-12-19 02:59:52 +000061}
62
Sean Callanana144c3f2010-04-02 21:23:51 +000063static bool translateInstruction(MCInst &target,
64 InternalInstruction &source);
Sean Callanan8ed9f512009-12-19 02:59:52 +000065
66X86GenericDisassembler::X86GenericDisassembler(DisassemblerMode mode) :
67 MCDisassembler(),
68 fMode(mode) {
69}
70
71X86GenericDisassembler::~X86GenericDisassembler() {
72}
73
Sean Callanan9899f702010-04-13 21:21:57 +000074EDInstInfo *X86GenericDisassembler::getEDInfo() const {
75 return instInfoX86;
76}
77
Sean Callanan8ed9f512009-12-19 02:59:52 +000078/// regionReader - a callback function that wraps the readByte method from
79/// MemoryObject.
80///
81/// @param arg - The generic callback parameter. In this case, this should
82/// be a pointer to a MemoryObject.
83/// @param byte - A pointer to the byte to be read.
84/// @param address - The address to be read.
85static int regionReader(void* arg, uint8_t* byte, uint64_t address) {
86 MemoryObject* region = static_cast<MemoryObject*>(arg);
87 return region->readByte(address, byte);
88}
89
90/// logger - a callback function that wraps the operator<< method from
91/// raw_ostream.
92///
93/// @param arg - The generic callback parameter. This should be a pointe
94/// to a raw_ostream.
95/// @param log - A string to be logged. logger() adds a newline.
96static void logger(void* arg, const char* log) {
97 if (!arg)
98 return;
99
100 raw_ostream &vStream = *(static_cast<raw_ostream*>(arg));
101 vStream << log << "\n";
102}
103
104//
105// Public interface for the disassembler
106//
107
108bool X86GenericDisassembler::getInstruction(MCInst &instr,
109 uint64_t &size,
110 const MemoryObject &region,
111 uint64_t address,
112 raw_ostream &vStream) const {
113 InternalInstruction internalInstr;
114
115 int ret = decodeInstruction(&internalInstr,
116 regionReader,
117 (void*)&region,
118 logger,
119 (void*)&vStream,
120 address,
121 fMode);
122
Sean Callanana144c3f2010-04-02 21:23:51 +0000123 if (ret) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000124 size = internalInstr.readerCursor - address;
125 return false;
126 }
127 else {
128 size = internalInstr.length;
Sean Callanana144c3f2010-04-02 21:23:51 +0000129 return !translateInstruction(instr, internalInstr);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000130 }
131}
132
133//
134// Private code that translates from struct InternalInstructions to MCInsts.
135//
136
137/// translateRegister - Translates an internal register to the appropriate LLVM
138/// register, and appends it as an operand to an MCInst.
139///
140/// @param mcInst - The MCInst to append to.
141/// @param reg - The Reg to append.
142static void translateRegister(MCInst &mcInst, Reg reg) {
143#define ENTRY(x) X86::x,
144 uint8_t llvmRegnums[] = {
145 ALL_REGS
146 0
147 };
148#undef ENTRY
149
150 uint8_t llvmRegnum = llvmRegnums[reg];
151 mcInst.addOperand(MCOperand::CreateReg(llvmRegnum));
152}
153
154/// translateImmediate - Appends an immediate operand to an MCInst.
155///
156/// @param mcInst - The MCInst to append to.
157/// @param immediate - The immediate value to append.
Sean Callananbe192dd2010-05-05 22:47:27 +0000158/// @param operand - The operand, as stored in the descriptor table.
159/// @param insn - The internal instruction.
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000160static void translateImmediate(MCInst &mcInst, uint64_t immediate,
161 const OperandSpecifier &operand,
Sean Callananbe192dd2010-05-05 22:47:27 +0000162 InternalInstruction &insn) {
163 // Sign-extend the immediate if necessary.
164
165 OperandType type = operand.type;
166
167 if (type == TYPE_RELv) {
168 switch (insn.displacementSize) {
169 default:
170 break;
Sean Callanan89e59e62011-02-21 21:55:05 +0000171 case 1:
Sean Callananbe192dd2010-05-05 22:47:27 +0000172 type = TYPE_MOFFS8;
173 break;
Sean Callanan89e59e62011-02-21 21:55:05 +0000174 case 2:
Sean Callananbe192dd2010-05-05 22:47:27 +0000175 type = TYPE_MOFFS16;
176 break;
Sean Callanan89e59e62011-02-21 21:55:05 +0000177 case 4:
Sean Callananbe192dd2010-05-05 22:47:27 +0000178 type = TYPE_MOFFS32;
179 break;
Sean Callanan89e59e62011-02-21 21:55:05 +0000180 case 8:
Sean Callananbe192dd2010-05-05 22:47:27 +0000181 type = TYPE_MOFFS64;
182 break;
183 }
184 }
185
186 switch (type) {
187 case TYPE_MOFFS8:
188 case TYPE_REL8:
189 if(immediate & 0x80)
190 immediate |= ~(0xffull);
191 break;
192 case TYPE_MOFFS16:
193 if(immediate & 0x8000)
194 immediate |= ~(0xffffull);
195 break;
196 case TYPE_MOFFS32:
197 case TYPE_REL32:
198 case TYPE_REL64:
199 if(immediate & 0x80000000)
200 immediate |= ~(0xffffffffull);
201 break;
202 case TYPE_MOFFS64:
203 default:
204 // operand is 64 bits wide. Do nothing.
205 break;
206 }
207
Sean Callanan8ed9f512009-12-19 02:59:52 +0000208 mcInst.addOperand(MCOperand::CreateImm(immediate));
209}
210
211/// translateRMRegister - Translates a register stored in the R/M field of the
212/// ModR/M byte to its LLVM equivalent and appends it to an MCInst.
213/// @param mcInst - The MCInst to append to.
214/// @param insn - The internal instruction to extract the R/M field
215/// from.
Sean Callanana144c3f2010-04-02 21:23:51 +0000216/// @return - 0 on success; -1 otherwise
217static bool translateRMRegister(MCInst &mcInst,
Sean Callanan8ed9f512009-12-19 02:59:52 +0000218 InternalInstruction &insn) {
Sean Callanana144c3f2010-04-02 21:23:51 +0000219 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
220 debug("A R/M register operand may not have a SIB byte");
221 return true;
222 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000223
224 switch (insn.eaBase) {
Sean Callanana144c3f2010-04-02 21:23:51 +0000225 default:
226 debug("Unexpected EA base register");
227 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000228 case EA_BASE_NONE:
Sean Callanana144c3f2010-04-02 21:23:51 +0000229 debug("EA_BASE_NONE for ModR/M base");
230 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000231#define ENTRY(x) case EA_BASE_##x:
232 ALL_EA_BASES
233#undef ENTRY
Sean Callanana144c3f2010-04-02 21:23:51 +0000234 debug("A R/M register operand may not have a base; "
235 "the operand must be a register.");
236 return true;
237#define ENTRY(x) \
Sean Callanan8ed9f512009-12-19 02:59:52 +0000238 case EA_REG_##x: \
239 mcInst.addOperand(MCOperand::CreateReg(X86::x)); break;
240 ALL_REGS
241#undef ENTRY
Sean Callanan8ed9f512009-12-19 02:59:52 +0000242 }
Sean Callanana144c3f2010-04-02 21:23:51 +0000243
244 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000245}
246
247/// translateRMMemory - Translates a memory operand stored in the Mod and R/M
248/// fields of an internal instruction (and possibly its SIB byte) to a memory
249/// operand in LLVM's format, and appends it to an MCInst.
250///
251/// @param mcInst - The MCInst to append to.
252/// @param insn - The instruction to extract Mod, R/M, and SIB fields
253/// from.
Sean Callanana144c3f2010-04-02 21:23:51 +0000254/// @return - 0 on success; nonzero otherwise
Chris Lattner37a746b2010-07-13 04:23:55 +0000255static bool translateRMMemory(MCInst &mcInst, InternalInstruction &insn) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000256 // Addresses in an MCInst are represented as five operands:
257 // 1. basereg (register) The R/M base, or (if there is a SIB) the
258 // SIB base
259 // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified
260 // scale amount
261 // 3. indexreg (register) x86_registerNONE, or (if there is a SIB)
262 // the index (which is multiplied by the
263 // scale amount)
264 // 4. displacement (immediate) 0, or the displacement if there is one
265 // 5. segmentreg (register) x86_registerNONE for now, but could be set
266 // if we have segment overrides
267
268 MCOperand baseReg;
269 MCOperand scaleAmount;
270 MCOperand indexReg;
271 MCOperand displacement;
272 MCOperand segmentReg;
273
274 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
275 if (insn.sibBase != SIB_BASE_NONE) {
276 switch (insn.sibBase) {
277 default:
Sean Callanana144c3f2010-04-02 21:23:51 +0000278 debug("Unexpected sibBase");
279 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000280#define ENTRY(x) \
Sean Callanan7fb35a22009-12-22 21:12:55 +0000281 case SIB_BASE_##x: \
Sean Callanan8ed9f512009-12-19 02:59:52 +0000282 baseReg = MCOperand::CreateReg(X86::x); break;
283 ALL_SIB_BASES
284#undef ENTRY
285 }
286 } else {
287 baseReg = MCOperand::CreateReg(0);
288 }
289
290 if (insn.sibIndex != SIB_INDEX_NONE) {
291 switch (insn.sibIndex) {
292 default:
Sean Callanana144c3f2010-04-02 21:23:51 +0000293 debug("Unexpected sibIndex");
294 return true;
Sean Callanan7fb35a22009-12-22 21:12:55 +0000295#define ENTRY(x) \
Sean Callanan8ed9f512009-12-19 02:59:52 +0000296 case SIB_INDEX_##x: \
297 indexReg = MCOperand::CreateReg(X86::x); break;
298 EA_BASES_32BIT
299 EA_BASES_64BIT
300#undef ENTRY
301 }
302 } else {
303 indexReg = MCOperand::CreateReg(0);
304 }
305
306 scaleAmount = MCOperand::CreateImm(insn.sibScale);
307 } else {
308 switch (insn.eaBase) {
309 case EA_BASE_NONE:
Sean Callanana144c3f2010-04-02 21:23:51 +0000310 if (insn.eaDisplacement == EA_DISP_NONE) {
311 debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
312 return true;
313 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000314 if (insn.mode == MODE_64BIT)
315 baseReg = MCOperand::CreateReg(X86::RIP); // Section 2.2.1.6
316 else
317 baseReg = MCOperand::CreateReg(0);
318
319 indexReg = MCOperand::CreateReg(0);
320 break;
321 case EA_BASE_BX_SI:
322 baseReg = MCOperand::CreateReg(X86::BX);
323 indexReg = MCOperand::CreateReg(X86::SI);
324 break;
325 case EA_BASE_BX_DI:
326 baseReg = MCOperand::CreateReg(X86::BX);
327 indexReg = MCOperand::CreateReg(X86::DI);
328 break;
329 case EA_BASE_BP_SI:
330 baseReg = MCOperand::CreateReg(X86::BP);
331 indexReg = MCOperand::CreateReg(X86::SI);
332 break;
333 case EA_BASE_BP_DI:
334 baseReg = MCOperand::CreateReg(X86::BP);
335 indexReg = MCOperand::CreateReg(X86::DI);
336 break;
337 default:
338 indexReg = MCOperand::CreateReg(0);
339 switch (insn.eaBase) {
340 default:
Sean Callanana144c3f2010-04-02 21:23:51 +0000341 debug("Unexpected eaBase");
342 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000343 // Here, we will use the fill-ins defined above. However,
344 // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and
345 // sib and sib64 were handled in the top-level if, so they're only
346 // placeholders to keep the compiler happy.
347#define ENTRY(x) \
348 case EA_BASE_##x: \
349 baseReg = MCOperand::CreateReg(X86::x); break;
350 ALL_EA_BASES
351#undef ENTRY
352#define ENTRY(x) case EA_REG_##x:
353 ALL_REGS
354#undef ENTRY
Sean Callanana144c3f2010-04-02 21:23:51 +0000355 debug("A R/M memory operand may not be a register; "
356 "the base field must be a base.");
357 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000358 }
359 }
Sean Callanan7fb35a22009-12-22 21:12:55 +0000360
361 scaleAmount = MCOperand::CreateImm(1);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000362 }
363
364 displacement = MCOperand::CreateImm(insn.displacement);
365
366 static const uint8_t segmentRegnums[SEG_OVERRIDE_max] = {
367 0, // SEG_OVERRIDE_NONE
368 X86::CS,
369 X86::SS,
370 X86::DS,
371 X86::ES,
372 X86::FS,
373 X86::GS
374 };
375
376 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
377
378 mcInst.addOperand(baseReg);
379 mcInst.addOperand(scaleAmount);
380 mcInst.addOperand(indexReg);
381 mcInst.addOperand(displacement);
Chris Lattner37a746b2010-07-13 04:23:55 +0000382 mcInst.addOperand(segmentReg);
Sean Callanana144c3f2010-04-02 21:23:51 +0000383 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000384}
385
386/// translateRM - Translates an operand stored in the R/M (and possibly SIB)
387/// byte of an instruction to LLVM form, and appends it to an MCInst.
388///
389/// @param mcInst - The MCInst to append to.
390/// @param operand - The operand, as stored in the descriptor table.
391/// @param insn - The instruction to extract Mod, R/M, and SIB fields
392/// from.
Sean Callanana144c3f2010-04-02 21:23:51 +0000393/// @return - 0 on success; nonzero otherwise
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000394static bool translateRM(MCInst &mcInst, const OperandSpecifier &operand,
395 InternalInstruction &insn) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000396 switch (operand.type) {
397 default:
Sean Callanana144c3f2010-04-02 21:23:51 +0000398 debug("Unexpected type for a R/M operand");
399 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000400 case TYPE_R8:
401 case TYPE_R16:
402 case TYPE_R32:
403 case TYPE_R64:
404 case TYPE_Rv:
405 case TYPE_MM:
406 case TYPE_MM32:
407 case TYPE_MM64:
408 case TYPE_XMM:
409 case TYPE_XMM32:
410 case TYPE_XMM64:
411 case TYPE_XMM128:
Sean Callanana21e2ea2011-03-15 01:23:15 +0000412 case TYPE_XMM256:
Sean Callanan8ed9f512009-12-19 02:59:52 +0000413 case TYPE_DEBUGREG:
Sean Callanan1a8b7892010-05-06 20:59:00 +0000414 case TYPE_CONTROLREG:
Sean Callanana144c3f2010-04-02 21:23:51 +0000415 return translateRMRegister(mcInst, insn);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000416 case TYPE_M:
417 case TYPE_M8:
418 case TYPE_M16:
419 case TYPE_M32:
420 case TYPE_M64:
421 case TYPE_M128:
Sean Callanana21e2ea2011-03-15 01:23:15 +0000422 case TYPE_M256:
Sean Callanan8ed9f512009-12-19 02:59:52 +0000423 case TYPE_M512:
424 case TYPE_Mv:
425 case TYPE_M32FP:
426 case TYPE_M64FP:
427 case TYPE_M80FP:
428 case TYPE_M16INT:
429 case TYPE_M32INT:
430 case TYPE_M64INT:
431 case TYPE_M1616:
432 case TYPE_M1632:
433 case TYPE_M1664:
Sean Callanan7fb35a22009-12-22 21:12:55 +0000434 case TYPE_LEA:
Chris Lattner37a746b2010-07-13 04:23:55 +0000435 return translateRMMemory(mcInst, insn);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000436 }
437}
438
439/// translateFPRegister - Translates a stack position on the FPU stack to its
440/// LLVM form, and appends it to an MCInst.
441///
442/// @param mcInst - The MCInst to append to.
443/// @param stackPos - The stack position to translate.
Sean Callanana144c3f2010-04-02 21:23:51 +0000444/// @return - 0 on success; nonzero otherwise.
445static bool translateFPRegister(MCInst &mcInst,
446 uint8_t stackPos) {
447 if (stackPos >= 8) {
448 debug("Invalid FP stack position");
449 return true;
450 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000451
452 mcInst.addOperand(MCOperand::CreateReg(X86::ST0 + stackPos));
Sean Callanana144c3f2010-04-02 21:23:51 +0000453
454 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000455}
456
457/// translateOperand - Translates an operand stored in an internal instruction
458/// to LLVM's format and appends it to an MCInst.
459///
460/// @param mcInst - The MCInst to append to.
461/// @param operand - The operand, as stored in the descriptor table.
462/// @param insn - The internal instruction.
Sean Callanana144c3f2010-04-02 21:23:51 +0000463/// @return - false on success; true otherwise.
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000464static bool translateOperand(MCInst &mcInst, const OperandSpecifier &operand,
465 InternalInstruction &insn) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000466 switch (operand.encoding) {
467 default:
Sean Callanana144c3f2010-04-02 21:23:51 +0000468 debug("Unhandled operand encoding during translation");
469 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000470 case ENCODING_REG:
471 translateRegister(mcInst, insn.reg);
Sean Callanana144c3f2010-04-02 21:23:51 +0000472 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000473 case ENCODING_RM:
Sean Callanana144c3f2010-04-02 21:23:51 +0000474 return translateRM(mcInst, operand, insn);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000475 case ENCODING_CB:
476 case ENCODING_CW:
477 case ENCODING_CD:
478 case ENCODING_CP:
479 case ENCODING_CO:
480 case ENCODING_CT:
Sean Callanana144c3f2010-04-02 21:23:51 +0000481 debug("Translation of code offsets isn't supported.");
482 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000483 case ENCODING_IB:
484 case ENCODING_IW:
485 case ENCODING_ID:
486 case ENCODING_IO:
487 case ENCODING_Iv:
488 case ENCODING_Ia:
Sean Callananbe192dd2010-05-05 22:47:27 +0000489 translateImmediate(mcInst,
490 insn.immediates[insn.numImmediatesTranslated++],
491 operand,
492 insn);
Sean Callanana144c3f2010-04-02 21:23:51 +0000493 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000494 case ENCODING_RB:
495 case ENCODING_RW:
496 case ENCODING_RD:
497 case ENCODING_RO:
498 translateRegister(mcInst, insn.opcodeRegister);
Sean Callanana144c3f2010-04-02 21:23:51 +0000499 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000500 case ENCODING_I:
Sean Callanana144c3f2010-04-02 21:23:51 +0000501 return translateFPRegister(mcInst, insn.opcodeModifier);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000502 case ENCODING_Rv:
503 translateRegister(mcInst, insn.opcodeRegister);
Sean Callanana144c3f2010-04-02 21:23:51 +0000504 return false;
Sean Callanana21e2ea2011-03-15 01:23:15 +0000505 case ENCODING_VVVV:
506 translateRegister(mcInst, insn.vvvv);
507 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000508 case ENCODING_DUP:
Sean Callanana144c3f2010-04-02 21:23:51 +0000509 return translateOperand(mcInst,
510 insn.spec->operands[operand.type - TYPE_DUP0],
511 insn);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000512 }
513}
514
515/// translateInstruction - Translates an internal instruction and all its
516/// operands to an MCInst.
517///
518/// @param mcInst - The MCInst to populate with the instruction's data.
519/// @param insn - The internal instruction.
Sean Callanana144c3f2010-04-02 21:23:51 +0000520/// @return - false on success; true otherwise.
521static bool translateInstruction(MCInst &mcInst,
522 InternalInstruction &insn) {
523 if (!insn.spec) {
524 debug("Instruction has no specification");
525 return true;
526 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000527
528 mcInst.setOpcode(insn.instructionID);
529
530 int index;
531
532 insn.numImmediatesTranslated = 0;
533
534 for (index = 0; index < X86_MAX_OPERANDS; ++index) {
Sean Callanana144c3f2010-04-02 21:23:51 +0000535 if (insn.spec->operands[index].encoding != ENCODING_NONE) {
536 if (translateOperand(mcInst, insn.spec->operands[index], insn)) {
537 return true;
538 }
539 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000540 }
Sean Callanana144c3f2010-04-02 21:23:51 +0000541
542 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000543}
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +0000544
Daniel Dunbar5d067fe2010-03-20 22:36:22 +0000545static MCDisassembler *createX86_32Disassembler(const Target &T) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000546 return new X86Disassembler::X86_32Disassembler;
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +0000547}
548
Daniel Dunbar5d067fe2010-03-20 22:36:22 +0000549static MCDisassembler *createX86_64Disassembler(const Target &T) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000550 return new X86Disassembler::X86_64Disassembler;
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +0000551}
552
553extern "C" void LLVMInitializeX86Disassembler() {
554 // Register the disassembler.
555 TargetRegistry::RegisterMCDisassembler(TheX86_32Target,
556 createX86_32Disassembler);
557 TargetRegistry::RegisterMCDisassembler(TheX86_64Target,
558 createX86_64Disassembler);
559}