blob: 08d6c07ae860ddcc25e1b28f208d22386b85e7ff [file] [log] [blame]
Jia Liu31d157a2012-02-18 12:03:15 +00001//===-- X86Disassembler.cpp - Disassembler for x86 and x86_64 -------------===//
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +00002//
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"
Benjamin Kramer953362c2012-02-11 14:50:54 +000024#include "llvm/MC/MCInstrInfo.h"
James Molloyb9505852011-09-07 17:24:38 +000025#include "llvm/MC/MCSubtargetInfo.h"
Sean Callanana144c3f2010-04-02 21:23:51 +000026#include "llvm/Support/Debug.h"
Sean Callanan8ed9f512009-12-19 02:59:52 +000027#include "llvm/Support/MemoryObject.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000028#include "llvm/Support/TargetRegistry.h"
Sean Callanan8ed9f512009-12-19 02:59:52 +000029#include "llvm/Support/raw_ostream.h"
Sean Callanan0122c902009-12-22 01:11:26 +000030
Evan Cheng73f50d92011-06-27 18:32:37 +000031#define GET_REGINFO_ENUM
32#include "X86GenRegisterInfo.inc"
Kevin Enderbyd5705fe2011-09-02 20:01:23 +000033#define GET_INSTRINFO_ENUM
34#include "X86GenInstrInfo.inc"
Sean Callanan9899f702010-04-13 21:21:57 +000035#include "X86GenEDInfo.inc"
Sean Callanan0122c902009-12-22 01:11:26 +000036
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +000037using namespace llvm;
Sean Callanan8ed9f512009-12-19 02:59:52 +000038using namespace llvm::X86Disassembler;
39
Sean Callanana144c3f2010-04-02 21:23:51 +000040void x86DisassemblerDebug(const char *file,
41 unsigned line,
42 const char *s) {
43 dbgs() << file << ":" << line << ": " << s;
44}
45
Benjamin Kramer953362c2012-02-11 14:50:54 +000046const char *x86DisassemblerGetInstrName(unsigned Opcode, void *mii) {
47 const MCInstrInfo *MII = static_cast<const MCInstrInfo *>(mii);
48 return MII->getName(Opcode);
49}
50
Sean Callanana144c3f2010-04-02 21:23:51 +000051#define debug(s) DEBUG(x86DisassemblerDebug(__FILE__, __LINE__, s));
52
Sean Callanan8ed9f512009-12-19 02:59:52 +000053namespace llvm {
54
55// Fill-ins to make the compiler happy. These constants are never actually
56// assigned; they are just filler to make an automatically-generated switch
57// statement work.
58namespace X86 {
59 enum {
60 BX_SI = 500,
61 BX_DI = 501,
62 BP_SI = 502,
63 BP_DI = 503,
64 sib = 504,
65 sib64 = 505
66 };
67}
68
Sean Callanan0122c902009-12-22 01:11:26 +000069extern Target TheX86_32Target, TheX86_64Target;
70
Sean Callanan8ed9f512009-12-19 02:59:52 +000071}
72
Sean Callanana144c3f2010-04-02 21:23:51 +000073static bool translateInstruction(MCInst &target,
74 InternalInstruction &source);
Sean Callanan8ed9f512009-12-19 02:59:52 +000075
Benjamin Kramer953362c2012-02-11 14:50:54 +000076X86GenericDisassembler::X86GenericDisassembler(const MCSubtargetInfo &STI,
77 DisassemblerMode mode,
78 const MCInstrInfo *MII)
79 : MCDisassembler(STI), MII(MII), fMode(mode) {}
Sean Callanan8ed9f512009-12-19 02:59:52 +000080
81X86GenericDisassembler::~X86GenericDisassembler() {
Benjamin Kramer953362c2012-02-11 14:50:54 +000082 delete MII;
Sean Callanan8ed9f512009-12-19 02:59:52 +000083}
84
Benjamin Kramer88b6fc02012-02-11 14:51:07 +000085const EDInstInfo *X86GenericDisassembler::getEDInfo() const {
Sean Callanan9899f702010-04-13 21:21:57 +000086 return instInfoX86;
87}
88
Sean Callanan8ed9f512009-12-19 02:59:52 +000089/// regionReader - a callback function that wraps the readByte method from
90/// MemoryObject.
91///
92/// @param arg - The generic callback parameter. In this case, this should
93/// be a pointer to a MemoryObject.
94/// @param byte - A pointer to the byte to be read.
95/// @param address - The address to be read.
96static int regionReader(void* arg, uint8_t* byte, uint64_t address) {
97 MemoryObject* region = static_cast<MemoryObject*>(arg);
98 return region->readByte(address, byte);
99}
100
101/// logger - a callback function that wraps the operator<< method from
102/// raw_ostream.
103///
104/// @param arg - The generic callback parameter. This should be a pointe
105/// to a raw_ostream.
106/// @param log - A string to be logged. logger() adds a newline.
107static void logger(void* arg, const char* log) {
108 if (!arg)
109 return;
110
111 raw_ostream &vStream = *(static_cast<raw_ostream*>(arg));
112 vStream << log << "\n";
113}
114
115//
116// Public interface for the disassembler
117//
118
Owen Anderson83e3f672011-08-17 17:44:15 +0000119MCDisassembler::DecodeStatus
120X86GenericDisassembler::getInstruction(MCInst &instr,
121 uint64_t &size,
Derek Schuff2ea93872012-02-06 22:30:29 +0000122 MemoryObject &region,
Owen Anderson83e3f672011-08-17 17:44:15 +0000123 uint64_t address,
Owen Anderson98c5dda2011-09-15 23:38:46 +0000124 raw_ostream &vStream,
125 raw_ostream &cStream) const {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000126 InternalInstruction internalInstr;
Benjamin Kramer15c9a1f2011-09-21 21:47:35 +0000127
128 dlog_t loggerFn = logger;
129 if (&vStream == &nulls())
130 loggerFn = 0; // Disable logging completely if it's going to nulls().
Sean Callanan8ed9f512009-12-19 02:59:52 +0000131
132 int ret = decodeInstruction(&internalInstr,
133 regionReader,
134 (void*)&region,
Benjamin Kramer15c9a1f2011-09-21 21:47:35 +0000135 loggerFn,
Sean Callanan8ed9f512009-12-19 02:59:52 +0000136 (void*)&vStream,
Benjamin Kramer953362c2012-02-11 14:50:54 +0000137 (void*)MII,
Sean Callanan8ed9f512009-12-19 02:59:52 +0000138 address,
139 fMode);
140
Sean Callanana144c3f2010-04-02 21:23:51 +0000141 if (ret) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000142 size = internalInstr.readerCursor - address;
Owen Anderson83e3f672011-08-17 17:44:15 +0000143 return Fail;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000144 }
145 else {
146 size = internalInstr.length;
Owen Anderson83e3f672011-08-17 17:44:15 +0000147 return (!translateInstruction(instr, internalInstr)) ? Success : Fail;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000148 }
149}
150
151//
152// Private code that translates from struct InternalInstructions to MCInsts.
153//
154
155/// translateRegister - Translates an internal register to the appropriate LLVM
156/// register, and appends it as an operand to an MCInst.
157///
158/// @param mcInst - The MCInst to append to.
159/// @param reg - The Reg to append.
160static void translateRegister(MCInst &mcInst, Reg reg) {
161#define ENTRY(x) X86::x,
162 uint8_t llvmRegnums[] = {
163 ALL_REGS
164 0
165 };
166#undef ENTRY
167
168 uint8_t llvmRegnum = llvmRegnums[reg];
169 mcInst.addOperand(MCOperand::CreateReg(llvmRegnum));
170}
171
172/// translateImmediate - Appends an immediate operand to an MCInst.
173///
174/// @param mcInst - The MCInst to append to.
175/// @param immediate - The immediate value to append.
Sean Callananbe192dd2010-05-05 22:47:27 +0000176/// @param operand - The operand, as stored in the descriptor table.
177/// @param insn - The internal instruction.
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000178static void translateImmediate(MCInst &mcInst, uint64_t immediate,
179 const OperandSpecifier &operand,
Sean Callananbe192dd2010-05-05 22:47:27 +0000180 InternalInstruction &insn) {
181 // Sign-extend the immediate if necessary.
182
183 OperandType type = operand.type;
184
185 if (type == TYPE_RELv) {
186 switch (insn.displacementSize) {
187 default:
188 break;
Sean Callanan89e59e62011-02-21 21:55:05 +0000189 case 1:
Sean Callananbe192dd2010-05-05 22:47:27 +0000190 type = TYPE_MOFFS8;
191 break;
Sean Callanan89e59e62011-02-21 21:55:05 +0000192 case 2:
Sean Callananbe192dd2010-05-05 22:47:27 +0000193 type = TYPE_MOFFS16;
194 break;
Sean Callanan89e59e62011-02-21 21:55:05 +0000195 case 4:
Sean Callananbe192dd2010-05-05 22:47:27 +0000196 type = TYPE_MOFFS32;
197 break;
Sean Callanan89e59e62011-02-21 21:55:05 +0000198 case 8:
Sean Callananbe192dd2010-05-05 22:47:27 +0000199 type = TYPE_MOFFS64;
200 break;
201 }
202 }
Kevin Enderbyd5705fe2011-09-02 20:01:23 +0000203 // By default sign-extend all X86 immediates based on their encoding.
204 else if (type == TYPE_IMM8 || type == TYPE_IMM16 || type == TYPE_IMM32 ||
205 type == TYPE_IMM64) {
206 uint32_t Opcode = mcInst.getOpcode();
207 switch (operand.encoding) {
208 default:
209 break;
210 case ENCODING_IB:
211 // Special case those X86 instructions that use the imm8 as a set of
212 // bits, bit count, etc. and are not sign-extend.
213 if (Opcode != X86::BLENDPSrri && Opcode != X86::BLENDPDrri &&
214 Opcode != X86::PBLENDWrri && Opcode != X86::MPSADBWrri &&
215 Opcode != X86::DPPSrri && Opcode != X86::DPPDrri &&
216 Opcode != X86::INSERTPSrr && Opcode != X86::VBLENDPSYrri &&
217 Opcode != X86::VBLENDPSYrmi && Opcode != X86::VBLENDPDYrri &&
218 Opcode != X86::VBLENDPDYrmi && Opcode != X86::VPBLENDWrri &&
219 Opcode != X86::VMPSADBWrri && Opcode != X86::VDPPSYrri &&
220 Opcode != X86::VDPPSYrmi && Opcode != X86::VDPPDrri &&
221 Opcode != X86::VINSERTPSrr)
222 type = TYPE_MOFFS8;
223 break;
224 case ENCODING_IW:
225 type = TYPE_MOFFS16;
226 break;
227 case ENCODING_ID:
228 type = TYPE_MOFFS32;
229 break;
230 case ENCODING_IO:
231 type = TYPE_MOFFS64;
232 break;
233 }
234 }
Sean Callananbe192dd2010-05-05 22:47:27 +0000235
236 switch (type) {
Craig Topper3bb43a82011-09-14 05:55:28 +0000237 case TYPE_XMM128:
238 mcInst.addOperand(MCOperand::CreateReg(X86::XMM0 + (immediate >> 4)));
239 return;
240 case TYPE_XMM256:
241 mcInst.addOperand(MCOperand::CreateReg(X86::YMM0 + (immediate >> 4)));
242 return;
Sean Callananbe192dd2010-05-05 22:47:27 +0000243 case TYPE_MOFFS8:
244 case TYPE_REL8:
245 if(immediate & 0x80)
246 immediate |= ~(0xffull);
247 break;
248 case TYPE_MOFFS16:
249 if(immediate & 0x8000)
250 immediate |= ~(0xffffull);
251 break;
252 case TYPE_MOFFS32:
253 case TYPE_REL32:
254 case TYPE_REL64:
255 if(immediate & 0x80000000)
256 immediate |= ~(0xffffffffull);
257 break;
258 case TYPE_MOFFS64:
259 default:
260 // operand is 64 bits wide. Do nothing.
261 break;
262 }
263
Sean Callanan8ed9f512009-12-19 02:59:52 +0000264 mcInst.addOperand(MCOperand::CreateImm(immediate));
265}
266
267/// translateRMRegister - Translates a register stored in the R/M field of the
268/// ModR/M byte to its LLVM equivalent and appends it to an MCInst.
269/// @param mcInst - The MCInst to append to.
270/// @param insn - The internal instruction to extract the R/M field
271/// from.
Sean Callanana144c3f2010-04-02 21:23:51 +0000272/// @return - 0 on success; -1 otherwise
273static bool translateRMRegister(MCInst &mcInst,
Sean Callanan8ed9f512009-12-19 02:59:52 +0000274 InternalInstruction &insn) {
Sean Callanana144c3f2010-04-02 21:23:51 +0000275 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
276 debug("A R/M register operand may not have a SIB byte");
277 return true;
278 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000279
280 switch (insn.eaBase) {
Sean Callanana144c3f2010-04-02 21:23:51 +0000281 default:
282 debug("Unexpected EA base register");
283 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000284 case EA_BASE_NONE:
Sean Callanana144c3f2010-04-02 21:23:51 +0000285 debug("EA_BASE_NONE for ModR/M base");
286 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000287#define ENTRY(x) case EA_BASE_##x:
288 ALL_EA_BASES
289#undef ENTRY
Sean Callanana144c3f2010-04-02 21:23:51 +0000290 debug("A R/M register operand may not have a base; "
291 "the operand must be a register.");
292 return true;
293#define ENTRY(x) \
Sean Callanan8ed9f512009-12-19 02:59:52 +0000294 case EA_REG_##x: \
295 mcInst.addOperand(MCOperand::CreateReg(X86::x)); break;
296 ALL_REGS
297#undef ENTRY
Sean Callanan8ed9f512009-12-19 02:59:52 +0000298 }
Sean Callanana144c3f2010-04-02 21:23:51 +0000299
300 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000301}
302
303/// translateRMMemory - Translates a memory operand stored in the Mod and R/M
304/// fields of an internal instruction (and possibly its SIB byte) to a memory
305/// operand in LLVM's format, and appends it to an MCInst.
306///
307/// @param mcInst - The MCInst to append to.
308/// @param insn - The instruction to extract Mod, R/M, and SIB fields
309/// from.
Sean Callanana144c3f2010-04-02 21:23:51 +0000310/// @return - 0 on success; nonzero otherwise
Chris Lattner37a746b2010-07-13 04:23:55 +0000311static bool translateRMMemory(MCInst &mcInst, InternalInstruction &insn) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000312 // Addresses in an MCInst are represented as five operands:
313 // 1. basereg (register) The R/M base, or (if there is a SIB) the
314 // SIB base
315 // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified
316 // scale amount
317 // 3. indexreg (register) x86_registerNONE, or (if there is a SIB)
318 // the index (which is multiplied by the
319 // scale amount)
320 // 4. displacement (immediate) 0, or the displacement if there is one
321 // 5. segmentreg (register) x86_registerNONE for now, but could be set
322 // if we have segment overrides
323
324 MCOperand baseReg;
325 MCOperand scaleAmount;
326 MCOperand indexReg;
327 MCOperand displacement;
328 MCOperand segmentReg;
329
330 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
331 if (insn.sibBase != SIB_BASE_NONE) {
332 switch (insn.sibBase) {
333 default:
Sean Callanana144c3f2010-04-02 21:23:51 +0000334 debug("Unexpected sibBase");
335 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000336#define ENTRY(x) \
Sean Callanan7fb35a22009-12-22 21:12:55 +0000337 case SIB_BASE_##x: \
Sean Callanan8ed9f512009-12-19 02:59:52 +0000338 baseReg = MCOperand::CreateReg(X86::x); break;
339 ALL_SIB_BASES
340#undef ENTRY
341 }
342 } else {
343 baseReg = MCOperand::CreateReg(0);
344 }
345
346 if (insn.sibIndex != SIB_INDEX_NONE) {
347 switch (insn.sibIndex) {
348 default:
Sean Callanana144c3f2010-04-02 21:23:51 +0000349 debug("Unexpected sibIndex");
350 return true;
Sean Callanan7fb35a22009-12-22 21:12:55 +0000351#define ENTRY(x) \
Sean Callanan8ed9f512009-12-19 02:59:52 +0000352 case SIB_INDEX_##x: \
353 indexReg = MCOperand::CreateReg(X86::x); break;
354 EA_BASES_32BIT
355 EA_BASES_64BIT
356#undef ENTRY
357 }
358 } else {
359 indexReg = MCOperand::CreateReg(0);
360 }
361
362 scaleAmount = MCOperand::CreateImm(insn.sibScale);
363 } else {
364 switch (insn.eaBase) {
365 case EA_BASE_NONE:
Sean Callanana144c3f2010-04-02 21:23:51 +0000366 if (insn.eaDisplacement == EA_DISP_NONE) {
367 debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
368 return true;
369 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000370 if (insn.mode == MODE_64BIT)
371 baseReg = MCOperand::CreateReg(X86::RIP); // Section 2.2.1.6
372 else
373 baseReg = MCOperand::CreateReg(0);
374
375 indexReg = MCOperand::CreateReg(0);
376 break;
377 case EA_BASE_BX_SI:
378 baseReg = MCOperand::CreateReg(X86::BX);
379 indexReg = MCOperand::CreateReg(X86::SI);
380 break;
381 case EA_BASE_BX_DI:
382 baseReg = MCOperand::CreateReg(X86::BX);
383 indexReg = MCOperand::CreateReg(X86::DI);
384 break;
385 case EA_BASE_BP_SI:
386 baseReg = MCOperand::CreateReg(X86::BP);
387 indexReg = MCOperand::CreateReg(X86::SI);
388 break;
389 case EA_BASE_BP_DI:
390 baseReg = MCOperand::CreateReg(X86::BP);
391 indexReg = MCOperand::CreateReg(X86::DI);
392 break;
393 default:
394 indexReg = MCOperand::CreateReg(0);
395 switch (insn.eaBase) {
396 default:
Sean Callanana144c3f2010-04-02 21:23:51 +0000397 debug("Unexpected eaBase");
398 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000399 // Here, we will use the fill-ins defined above. However,
400 // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and
401 // sib and sib64 were handled in the top-level if, so they're only
402 // placeholders to keep the compiler happy.
403#define ENTRY(x) \
404 case EA_BASE_##x: \
405 baseReg = MCOperand::CreateReg(X86::x); break;
406 ALL_EA_BASES
407#undef ENTRY
408#define ENTRY(x) case EA_REG_##x:
409 ALL_REGS
410#undef ENTRY
Sean Callanana144c3f2010-04-02 21:23:51 +0000411 debug("A R/M memory operand may not be a register; "
412 "the base field must be a base.");
413 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000414 }
415 }
Sean Callanan7fb35a22009-12-22 21:12:55 +0000416
417 scaleAmount = MCOperand::CreateImm(1);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000418 }
419
420 displacement = MCOperand::CreateImm(insn.displacement);
421
422 static const uint8_t segmentRegnums[SEG_OVERRIDE_max] = {
423 0, // SEG_OVERRIDE_NONE
424 X86::CS,
425 X86::SS,
426 X86::DS,
427 X86::ES,
428 X86::FS,
429 X86::GS
430 };
431
432 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
433
434 mcInst.addOperand(baseReg);
435 mcInst.addOperand(scaleAmount);
436 mcInst.addOperand(indexReg);
437 mcInst.addOperand(displacement);
Chris Lattner37a746b2010-07-13 04:23:55 +0000438 mcInst.addOperand(segmentReg);
Sean Callanana144c3f2010-04-02 21:23:51 +0000439 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000440}
441
442/// translateRM - Translates an operand stored in the R/M (and possibly SIB)
443/// byte of an instruction to LLVM form, and appends it to an MCInst.
444///
445/// @param mcInst - The MCInst to append to.
446/// @param operand - The operand, as stored in the descriptor table.
447/// @param insn - The instruction to extract Mod, R/M, and SIB fields
448/// from.
Sean Callanana144c3f2010-04-02 21:23:51 +0000449/// @return - 0 on success; nonzero otherwise
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000450static bool translateRM(MCInst &mcInst, const OperandSpecifier &operand,
451 InternalInstruction &insn) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000452 switch (operand.type) {
453 default:
Sean Callanana144c3f2010-04-02 21:23:51 +0000454 debug("Unexpected type for a R/M operand");
455 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000456 case TYPE_R8:
457 case TYPE_R16:
458 case TYPE_R32:
459 case TYPE_R64:
460 case TYPE_Rv:
461 case TYPE_MM:
462 case TYPE_MM32:
463 case TYPE_MM64:
464 case TYPE_XMM:
465 case TYPE_XMM32:
466 case TYPE_XMM64:
467 case TYPE_XMM128:
Sean Callanana21e2ea2011-03-15 01:23:15 +0000468 case TYPE_XMM256:
Sean Callanan8ed9f512009-12-19 02:59:52 +0000469 case TYPE_DEBUGREG:
Sean Callanan1a8b7892010-05-06 20:59:00 +0000470 case TYPE_CONTROLREG:
Sean Callanana144c3f2010-04-02 21:23:51 +0000471 return translateRMRegister(mcInst, insn);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000472 case TYPE_M:
473 case TYPE_M8:
474 case TYPE_M16:
475 case TYPE_M32:
476 case TYPE_M64:
477 case TYPE_M128:
Sean Callanana21e2ea2011-03-15 01:23:15 +0000478 case TYPE_M256:
Sean Callanan8ed9f512009-12-19 02:59:52 +0000479 case TYPE_M512:
480 case TYPE_Mv:
481 case TYPE_M32FP:
482 case TYPE_M64FP:
483 case TYPE_M80FP:
484 case TYPE_M16INT:
485 case TYPE_M32INT:
486 case TYPE_M64INT:
487 case TYPE_M1616:
488 case TYPE_M1632:
489 case TYPE_M1664:
Sean Callanan7fb35a22009-12-22 21:12:55 +0000490 case TYPE_LEA:
Chris Lattner37a746b2010-07-13 04:23:55 +0000491 return translateRMMemory(mcInst, insn);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000492 }
493}
494
495/// translateFPRegister - Translates a stack position on the FPU stack to its
496/// LLVM form, and appends it to an MCInst.
497///
498/// @param mcInst - The MCInst to append to.
499/// @param stackPos - The stack position to translate.
Sean Callanana144c3f2010-04-02 21:23:51 +0000500/// @return - 0 on success; nonzero otherwise.
501static bool translateFPRegister(MCInst &mcInst,
502 uint8_t stackPos) {
503 if (stackPos >= 8) {
504 debug("Invalid FP stack position");
505 return true;
506 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000507
508 mcInst.addOperand(MCOperand::CreateReg(X86::ST0 + stackPos));
Sean Callanana144c3f2010-04-02 21:23:51 +0000509
510 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000511}
512
513/// translateOperand - Translates an operand stored in an internal instruction
514/// to LLVM's format and appends it to an MCInst.
515///
516/// @param mcInst - The MCInst to append to.
517/// @param operand - The operand, as stored in the descriptor table.
518/// @param insn - The internal instruction.
Sean Callanana144c3f2010-04-02 21:23:51 +0000519/// @return - false on success; true otherwise.
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000520static bool translateOperand(MCInst &mcInst, const OperandSpecifier &operand,
521 InternalInstruction &insn) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000522 switch (operand.encoding) {
523 default:
Sean Callanana144c3f2010-04-02 21:23:51 +0000524 debug("Unhandled operand encoding during translation");
525 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000526 case ENCODING_REG:
527 translateRegister(mcInst, insn.reg);
Sean Callanana144c3f2010-04-02 21:23:51 +0000528 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000529 case ENCODING_RM:
Sean Callanana144c3f2010-04-02 21:23:51 +0000530 return translateRM(mcInst, operand, insn);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000531 case ENCODING_CB:
532 case ENCODING_CW:
533 case ENCODING_CD:
534 case ENCODING_CP:
535 case ENCODING_CO:
536 case ENCODING_CT:
Sean Callanana144c3f2010-04-02 21:23:51 +0000537 debug("Translation of code offsets isn't supported.");
538 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000539 case ENCODING_IB:
540 case ENCODING_IW:
541 case ENCODING_ID:
542 case ENCODING_IO:
543 case ENCODING_Iv:
544 case ENCODING_Ia:
Sean Callananbe192dd2010-05-05 22:47:27 +0000545 translateImmediate(mcInst,
546 insn.immediates[insn.numImmediatesTranslated++],
547 operand,
548 insn);
Sean Callanana144c3f2010-04-02 21:23:51 +0000549 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000550 case ENCODING_RB:
551 case ENCODING_RW:
552 case ENCODING_RD:
553 case ENCODING_RO:
554 translateRegister(mcInst, insn.opcodeRegister);
Sean Callanana144c3f2010-04-02 21:23:51 +0000555 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000556 case ENCODING_I:
Sean Callanana144c3f2010-04-02 21:23:51 +0000557 return translateFPRegister(mcInst, insn.opcodeModifier);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000558 case ENCODING_Rv:
559 translateRegister(mcInst, insn.opcodeRegister);
Sean Callanana144c3f2010-04-02 21:23:51 +0000560 return false;
Sean Callanana21e2ea2011-03-15 01:23:15 +0000561 case ENCODING_VVVV:
562 translateRegister(mcInst, insn.vvvv);
563 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000564 case ENCODING_DUP:
Sean Callanana144c3f2010-04-02 21:23:51 +0000565 return translateOperand(mcInst,
566 insn.spec->operands[operand.type - TYPE_DUP0],
567 insn);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000568 }
569}
570
571/// translateInstruction - Translates an internal instruction and all its
572/// operands to an MCInst.
573///
574/// @param mcInst - The MCInst to populate with the instruction's data.
575/// @param insn - The internal instruction.
Sean Callanana144c3f2010-04-02 21:23:51 +0000576/// @return - false on success; true otherwise.
577static bool translateInstruction(MCInst &mcInst,
578 InternalInstruction &insn) {
579 if (!insn.spec) {
580 debug("Instruction has no specification");
581 return true;
582 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000583
584 mcInst.setOpcode(insn.instructionID);
585
586 int index;
587
588 insn.numImmediatesTranslated = 0;
589
590 for (index = 0; index < X86_MAX_OPERANDS; ++index) {
Sean Callanana144c3f2010-04-02 21:23:51 +0000591 if (insn.spec->operands[index].encoding != ENCODING_NONE) {
592 if (translateOperand(mcInst, insn.spec->operands[index], insn)) {
593 return true;
594 }
595 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000596 }
Sean Callanana144c3f2010-04-02 21:23:51 +0000597
598 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000599}
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +0000600
Benjamin Kramer953362c2012-02-11 14:50:54 +0000601static MCDisassembler *createX86_32Disassembler(const Target &T,
602 const MCSubtargetInfo &STI) {
603 return new X86Disassembler::X86GenericDisassembler(STI, MODE_32BIT,
604 T.createMCInstrInfo());
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +0000605}
606
Benjamin Kramer953362c2012-02-11 14:50:54 +0000607static MCDisassembler *createX86_64Disassembler(const Target &T,
608 const MCSubtargetInfo &STI) {
609 return new X86Disassembler::X86GenericDisassembler(STI, MODE_64BIT,
610 T.createMCInstrInfo());
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +0000611}
612
613extern "C" void LLVMInitializeX86Disassembler() {
614 // Register the disassembler.
615 TargetRegistry::RegisterMCDisassembler(TheX86_32Target,
616 createX86_32Disassembler);
617 TargetRegistry::RegisterMCDisassembler(TheX86_64Target,
618 createX86_64Disassembler);
619}