blob: f57075f105b85f6e4ee80af025bb9eb3b4a5a929 [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"
James Molloyb9505852011-09-07 17:24:38 +000024#include "llvm/MC/MCSubtargetInfo.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"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000027#include "llvm/Support/TargetRegistry.h"
Sean Callanan8ed9f512009-12-19 02:59:52 +000028#include "llvm/Support/raw_ostream.h"
Sean Callanan0122c902009-12-22 01:11:26 +000029
Evan Cheng73f50d92011-06-27 18:32:37 +000030#define GET_REGINFO_ENUM
31#include "X86GenRegisterInfo.inc"
Kevin Enderbyd5705fe2011-09-02 20:01:23 +000032#define GET_INSTRINFO_ENUM
33#include "X86GenInstrInfo.inc"
Sean Callanan9899f702010-04-13 21:21:57 +000034#include "X86GenEDInfo.inc"
Sean Callanan0122c902009-12-22 01:11:26 +000035
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +000036using namespace llvm;
Sean Callanan8ed9f512009-12-19 02:59:52 +000037using namespace llvm::X86Disassembler;
38
Sean Callanana144c3f2010-04-02 21:23:51 +000039void x86DisassemblerDebug(const char *file,
40 unsigned line,
41 const char *s) {
42 dbgs() << file << ":" << line << ": " << s;
43}
44
45#define debug(s) DEBUG(x86DisassemblerDebug(__FILE__, __LINE__, s));
46
Sean Callanan8ed9f512009-12-19 02:59:52 +000047namespace llvm {
48
49// Fill-ins to make the compiler happy. These constants are never actually
50// assigned; they are just filler to make an automatically-generated switch
51// statement work.
52namespace X86 {
53 enum {
54 BX_SI = 500,
55 BX_DI = 501,
56 BP_SI = 502,
57 BP_DI = 503,
58 sib = 504,
59 sib64 = 505
60 };
61}
62
Sean Callanan0122c902009-12-22 01:11:26 +000063extern Target TheX86_32Target, TheX86_64Target;
64
Sean Callanan8ed9f512009-12-19 02:59:52 +000065}
66
Sean Callanana144c3f2010-04-02 21:23:51 +000067static bool translateInstruction(MCInst &target,
68 InternalInstruction &source);
Sean Callanan8ed9f512009-12-19 02:59:52 +000069
James Molloyb9505852011-09-07 17:24:38 +000070X86GenericDisassembler::X86GenericDisassembler(const MCSubtargetInfo &STI, DisassemblerMode mode) :
71 MCDisassembler(STI),
Sean Callanan8ed9f512009-12-19 02:59:52 +000072 fMode(mode) {
73}
74
75X86GenericDisassembler::~X86GenericDisassembler() {
76}
77
Sean Callanan9899f702010-04-13 21:21:57 +000078EDInstInfo *X86GenericDisassembler::getEDInfo() const {
79 return instInfoX86;
80}
81
Sean Callanan8ed9f512009-12-19 02:59:52 +000082/// regionReader - a callback function that wraps the readByte method from
83/// MemoryObject.
84///
85/// @param arg - The generic callback parameter. In this case, this should
86/// be a pointer to a MemoryObject.
87/// @param byte - A pointer to the byte to be read.
88/// @param address - The address to be read.
89static int regionReader(void* arg, uint8_t* byte, uint64_t address) {
90 MemoryObject* region = static_cast<MemoryObject*>(arg);
91 return region->readByte(address, byte);
92}
93
94/// logger - a callback function that wraps the operator<< method from
95/// raw_ostream.
96///
97/// @param arg - The generic callback parameter. This should be a pointe
98/// to a raw_ostream.
99/// @param log - A string to be logged. logger() adds a newline.
100static void logger(void* arg, const char* log) {
101 if (!arg)
102 return;
103
104 raw_ostream &vStream = *(static_cast<raw_ostream*>(arg));
105 vStream << log << "\n";
106}
107
108//
109// Public interface for the disassembler
110//
111
Owen Anderson83e3f672011-08-17 17:44:15 +0000112MCDisassembler::DecodeStatus
113X86GenericDisassembler::getInstruction(MCInst &instr,
114 uint64_t &size,
Derek Schuff2ea93872012-02-06 22:30:29 +0000115 MemoryObject &region,
Owen Anderson83e3f672011-08-17 17:44:15 +0000116 uint64_t address,
Owen Anderson98c5dda2011-09-15 23:38:46 +0000117 raw_ostream &vStream,
118 raw_ostream &cStream) const {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000119 InternalInstruction internalInstr;
Benjamin Kramer15c9a1f2011-09-21 21:47:35 +0000120
121 dlog_t loggerFn = logger;
122 if (&vStream == &nulls())
123 loggerFn = 0; // Disable logging completely if it's going to nulls().
Sean Callanan8ed9f512009-12-19 02:59:52 +0000124
125 int ret = decodeInstruction(&internalInstr,
126 regionReader,
127 (void*)&region,
Benjamin Kramer15c9a1f2011-09-21 21:47:35 +0000128 loggerFn,
Sean Callanan8ed9f512009-12-19 02:59:52 +0000129 (void*)&vStream,
130 address,
131 fMode);
132
Sean Callanana144c3f2010-04-02 21:23:51 +0000133 if (ret) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000134 size = internalInstr.readerCursor - address;
Owen Anderson83e3f672011-08-17 17:44:15 +0000135 return Fail;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000136 }
137 else {
138 size = internalInstr.length;
Owen Anderson83e3f672011-08-17 17:44:15 +0000139 return (!translateInstruction(instr, internalInstr)) ? Success : Fail;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000140 }
141}
142
143//
144// Private code that translates from struct InternalInstructions to MCInsts.
145//
146
147/// translateRegister - Translates an internal register to the appropriate LLVM
148/// register, and appends it as an operand to an MCInst.
149///
150/// @param mcInst - The MCInst to append to.
151/// @param reg - The Reg to append.
152static void translateRegister(MCInst &mcInst, Reg reg) {
153#define ENTRY(x) X86::x,
154 uint8_t llvmRegnums[] = {
155 ALL_REGS
156 0
157 };
158#undef ENTRY
159
160 uint8_t llvmRegnum = llvmRegnums[reg];
161 mcInst.addOperand(MCOperand::CreateReg(llvmRegnum));
162}
163
164/// translateImmediate - Appends an immediate operand to an MCInst.
165///
166/// @param mcInst - The MCInst to append to.
167/// @param immediate - The immediate value to append.
Sean Callananbe192dd2010-05-05 22:47:27 +0000168/// @param operand - The operand, as stored in the descriptor table.
169/// @param insn - The internal instruction.
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000170static void translateImmediate(MCInst &mcInst, uint64_t immediate,
171 const OperandSpecifier &operand,
Sean Callananbe192dd2010-05-05 22:47:27 +0000172 InternalInstruction &insn) {
173 // Sign-extend the immediate if necessary.
174
175 OperandType type = operand.type;
176
177 if (type == TYPE_RELv) {
178 switch (insn.displacementSize) {
179 default:
180 break;
Sean Callanan89e59e62011-02-21 21:55:05 +0000181 case 1:
Sean Callananbe192dd2010-05-05 22:47:27 +0000182 type = TYPE_MOFFS8;
183 break;
Sean Callanan89e59e62011-02-21 21:55:05 +0000184 case 2:
Sean Callananbe192dd2010-05-05 22:47:27 +0000185 type = TYPE_MOFFS16;
186 break;
Sean Callanan89e59e62011-02-21 21:55:05 +0000187 case 4:
Sean Callananbe192dd2010-05-05 22:47:27 +0000188 type = TYPE_MOFFS32;
189 break;
Sean Callanan89e59e62011-02-21 21:55:05 +0000190 case 8:
Sean Callananbe192dd2010-05-05 22:47:27 +0000191 type = TYPE_MOFFS64;
192 break;
193 }
194 }
Kevin Enderbyd5705fe2011-09-02 20:01:23 +0000195 // By default sign-extend all X86 immediates based on their encoding.
196 else if (type == TYPE_IMM8 || type == TYPE_IMM16 || type == TYPE_IMM32 ||
197 type == TYPE_IMM64) {
198 uint32_t Opcode = mcInst.getOpcode();
199 switch (operand.encoding) {
200 default:
201 break;
202 case ENCODING_IB:
203 // Special case those X86 instructions that use the imm8 as a set of
204 // bits, bit count, etc. and are not sign-extend.
205 if (Opcode != X86::BLENDPSrri && Opcode != X86::BLENDPDrri &&
206 Opcode != X86::PBLENDWrri && Opcode != X86::MPSADBWrri &&
207 Opcode != X86::DPPSrri && Opcode != X86::DPPDrri &&
208 Opcode != X86::INSERTPSrr && Opcode != X86::VBLENDPSYrri &&
209 Opcode != X86::VBLENDPSYrmi && Opcode != X86::VBLENDPDYrri &&
210 Opcode != X86::VBLENDPDYrmi && Opcode != X86::VPBLENDWrri &&
211 Opcode != X86::VMPSADBWrri && Opcode != X86::VDPPSYrri &&
212 Opcode != X86::VDPPSYrmi && Opcode != X86::VDPPDrri &&
213 Opcode != X86::VINSERTPSrr)
214 type = TYPE_MOFFS8;
215 break;
216 case ENCODING_IW:
217 type = TYPE_MOFFS16;
218 break;
219 case ENCODING_ID:
220 type = TYPE_MOFFS32;
221 break;
222 case ENCODING_IO:
223 type = TYPE_MOFFS64;
224 break;
225 }
226 }
Sean Callananbe192dd2010-05-05 22:47:27 +0000227
228 switch (type) {
Craig Topper3bb43a82011-09-14 05:55:28 +0000229 case TYPE_XMM128:
230 mcInst.addOperand(MCOperand::CreateReg(X86::XMM0 + (immediate >> 4)));
231 return;
232 case TYPE_XMM256:
233 mcInst.addOperand(MCOperand::CreateReg(X86::YMM0 + (immediate >> 4)));
234 return;
Sean Callananbe192dd2010-05-05 22:47:27 +0000235 case TYPE_MOFFS8:
236 case TYPE_REL8:
237 if(immediate & 0x80)
238 immediate |= ~(0xffull);
239 break;
240 case TYPE_MOFFS16:
241 if(immediate & 0x8000)
242 immediate |= ~(0xffffull);
243 break;
244 case TYPE_MOFFS32:
245 case TYPE_REL32:
246 case TYPE_REL64:
247 if(immediate & 0x80000000)
248 immediate |= ~(0xffffffffull);
249 break;
250 case TYPE_MOFFS64:
251 default:
252 // operand is 64 bits wide. Do nothing.
253 break;
254 }
255
Sean Callanan8ed9f512009-12-19 02:59:52 +0000256 mcInst.addOperand(MCOperand::CreateImm(immediate));
257}
258
259/// translateRMRegister - Translates a register stored in the R/M field of the
260/// ModR/M byte to its LLVM equivalent and appends it to an MCInst.
261/// @param mcInst - The MCInst to append to.
262/// @param insn - The internal instruction to extract the R/M field
263/// from.
Sean Callanana144c3f2010-04-02 21:23:51 +0000264/// @return - 0 on success; -1 otherwise
265static bool translateRMRegister(MCInst &mcInst,
Sean Callanan8ed9f512009-12-19 02:59:52 +0000266 InternalInstruction &insn) {
Sean Callanana144c3f2010-04-02 21:23:51 +0000267 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
268 debug("A R/M register operand may not have a SIB byte");
269 return true;
270 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000271
272 switch (insn.eaBase) {
Sean Callanana144c3f2010-04-02 21:23:51 +0000273 default:
274 debug("Unexpected EA base register");
275 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000276 case EA_BASE_NONE:
Sean Callanana144c3f2010-04-02 21:23:51 +0000277 debug("EA_BASE_NONE for ModR/M base");
278 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000279#define ENTRY(x) case EA_BASE_##x:
280 ALL_EA_BASES
281#undef ENTRY
Sean Callanana144c3f2010-04-02 21:23:51 +0000282 debug("A R/M register operand may not have a base; "
283 "the operand must be a register.");
284 return true;
285#define ENTRY(x) \
Sean Callanan8ed9f512009-12-19 02:59:52 +0000286 case EA_REG_##x: \
287 mcInst.addOperand(MCOperand::CreateReg(X86::x)); break;
288 ALL_REGS
289#undef ENTRY
Sean Callanan8ed9f512009-12-19 02:59:52 +0000290 }
Sean Callanana144c3f2010-04-02 21:23:51 +0000291
292 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000293}
294
295/// translateRMMemory - Translates a memory operand stored in the Mod and R/M
296/// fields of an internal instruction (and possibly its SIB byte) to a memory
297/// operand in LLVM's format, and appends it to an MCInst.
298///
299/// @param mcInst - The MCInst to append to.
300/// @param insn - The instruction to extract Mod, R/M, and SIB fields
301/// from.
Sean Callanana144c3f2010-04-02 21:23:51 +0000302/// @return - 0 on success; nonzero otherwise
Chris Lattner37a746b2010-07-13 04:23:55 +0000303static bool translateRMMemory(MCInst &mcInst, InternalInstruction &insn) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000304 // Addresses in an MCInst are represented as five operands:
305 // 1. basereg (register) The R/M base, or (if there is a SIB) the
306 // SIB base
307 // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified
308 // scale amount
309 // 3. indexreg (register) x86_registerNONE, or (if there is a SIB)
310 // the index (which is multiplied by the
311 // scale amount)
312 // 4. displacement (immediate) 0, or the displacement if there is one
313 // 5. segmentreg (register) x86_registerNONE for now, but could be set
314 // if we have segment overrides
315
316 MCOperand baseReg;
317 MCOperand scaleAmount;
318 MCOperand indexReg;
319 MCOperand displacement;
320 MCOperand segmentReg;
321
322 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
323 if (insn.sibBase != SIB_BASE_NONE) {
324 switch (insn.sibBase) {
325 default:
Sean Callanana144c3f2010-04-02 21:23:51 +0000326 debug("Unexpected sibBase");
327 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000328#define ENTRY(x) \
Sean Callanan7fb35a22009-12-22 21:12:55 +0000329 case SIB_BASE_##x: \
Sean Callanan8ed9f512009-12-19 02:59:52 +0000330 baseReg = MCOperand::CreateReg(X86::x); break;
331 ALL_SIB_BASES
332#undef ENTRY
333 }
334 } else {
335 baseReg = MCOperand::CreateReg(0);
336 }
337
338 if (insn.sibIndex != SIB_INDEX_NONE) {
339 switch (insn.sibIndex) {
340 default:
Sean Callanana144c3f2010-04-02 21:23:51 +0000341 debug("Unexpected sibIndex");
342 return true;
Sean Callanan7fb35a22009-12-22 21:12:55 +0000343#define ENTRY(x) \
Sean Callanan8ed9f512009-12-19 02:59:52 +0000344 case SIB_INDEX_##x: \
345 indexReg = MCOperand::CreateReg(X86::x); break;
346 EA_BASES_32BIT
347 EA_BASES_64BIT
348#undef ENTRY
349 }
350 } else {
351 indexReg = MCOperand::CreateReg(0);
352 }
353
354 scaleAmount = MCOperand::CreateImm(insn.sibScale);
355 } else {
356 switch (insn.eaBase) {
357 case EA_BASE_NONE:
Sean Callanana144c3f2010-04-02 21:23:51 +0000358 if (insn.eaDisplacement == EA_DISP_NONE) {
359 debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
360 return true;
361 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000362 if (insn.mode == MODE_64BIT)
363 baseReg = MCOperand::CreateReg(X86::RIP); // Section 2.2.1.6
364 else
365 baseReg = MCOperand::CreateReg(0);
366
367 indexReg = MCOperand::CreateReg(0);
368 break;
369 case EA_BASE_BX_SI:
370 baseReg = MCOperand::CreateReg(X86::BX);
371 indexReg = MCOperand::CreateReg(X86::SI);
372 break;
373 case EA_BASE_BX_DI:
374 baseReg = MCOperand::CreateReg(X86::BX);
375 indexReg = MCOperand::CreateReg(X86::DI);
376 break;
377 case EA_BASE_BP_SI:
378 baseReg = MCOperand::CreateReg(X86::BP);
379 indexReg = MCOperand::CreateReg(X86::SI);
380 break;
381 case EA_BASE_BP_DI:
382 baseReg = MCOperand::CreateReg(X86::BP);
383 indexReg = MCOperand::CreateReg(X86::DI);
384 break;
385 default:
386 indexReg = MCOperand::CreateReg(0);
387 switch (insn.eaBase) {
388 default:
Sean Callanana144c3f2010-04-02 21:23:51 +0000389 debug("Unexpected eaBase");
390 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000391 // Here, we will use the fill-ins defined above. However,
392 // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and
393 // sib and sib64 were handled in the top-level if, so they're only
394 // placeholders to keep the compiler happy.
395#define ENTRY(x) \
396 case EA_BASE_##x: \
397 baseReg = MCOperand::CreateReg(X86::x); break;
398 ALL_EA_BASES
399#undef ENTRY
400#define ENTRY(x) case EA_REG_##x:
401 ALL_REGS
402#undef ENTRY
Sean Callanana144c3f2010-04-02 21:23:51 +0000403 debug("A R/M memory operand may not be a register; "
404 "the base field must be a base.");
405 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000406 }
407 }
Sean Callanan7fb35a22009-12-22 21:12:55 +0000408
409 scaleAmount = MCOperand::CreateImm(1);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000410 }
411
412 displacement = MCOperand::CreateImm(insn.displacement);
413
414 static const uint8_t segmentRegnums[SEG_OVERRIDE_max] = {
415 0, // SEG_OVERRIDE_NONE
416 X86::CS,
417 X86::SS,
418 X86::DS,
419 X86::ES,
420 X86::FS,
421 X86::GS
422 };
423
424 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
425
426 mcInst.addOperand(baseReg);
427 mcInst.addOperand(scaleAmount);
428 mcInst.addOperand(indexReg);
429 mcInst.addOperand(displacement);
Chris Lattner37a746b2010-07-13 04:23:55 +0000430 mcInst.addOperand(segmentReg);
Sean Callanana144c3f2010-04-02 21:23:51 +0000431 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000432}
433
434/// translateRM - Translates an operand stored in the R/M (and possibly SIB)
435/// byte of an instruction to LLVM form, and appends it to an MCInst.
436///
437/// @param mcInst - The MCInst to append to.
438/// @param operand - The operand, as stored in the descriptor table.
439/// @param insn - The instruction to extract Mod, R/M, and SIB fields
440/// from.
Sean Callanana144c3f2010-04-02 21:23:51 +0000441/// @return - 0 on success; nonzero otherwise
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000442static bool translateRM(MCInst &mcInst, const OperandSpecifier &operand,
443 InternalInstruction &insn) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000444 switch (operand.type) {
445 default:
Sean Callanana144c3f2010-04-02 21:23:51 +0000446 debug("Unexpected type for a R/M operand");
447 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000448 case TYPE_R8:
449 case TYPE_R16:
450 case TYPE_R32:
451 case TYPE_R64:
452 case TYPE_Rv:
453 case TYPE_MM:
454 case TYPE_MM32:
455 case TYPE_MM64:
456 case TYPE_XMM:
457 case TYPE_XMM32:
458 case TYPE_XMM64:
459 case TYPE_XMM128:
Sean Callanana21e2ea2011-03-15 01:23:15 +0000460 case TYPE_XMM256:
Sean Callanan8ed9f512009-12-19 02:59:52 +0000461 case TYPE_DEBUGREG:
Sean Callanan1a8b7892010-05-06 20:59:00 +0000462 case TYPE_CONTROLREG:
Sean Callanana144c3f2010-04-02 21:23:51 +0000463 return translateRMRegister(mcInst, insn);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000464 case TYPE_M:
465 case TYPE_M8:
466 case TYPE_M16:
467 case TYPE_M32:
468 case TYPE_M64:
469 case TYPE_M128:
Sean Callanana21e2ea2011-03-15 01:23:15 +0000470 case TYPE_M256:
Sean Callanan8ed9f512009-12-19 02:59:52 +0000471 case TYPE_M512:
472 case TYPE_Mv:
473 case TYPE_M32FP:
474 case TYPE_M64FP:
475 case TYPE_M80FP:
476 case TYPE_M16INT:
477 case TYPE_M32INT:
478 case TYPE_M64INT:
479 case TYPE_M1616:
480 case TYPE_M1632:
481 case TYPE_M1664:
Sean Callanan7fb35a22009-12-22 21:12:55 +0000482 case TYPE_LEA:
Chris Lattner37a746b2010-07-13 04:23:55 +0000483 return translateRMMemory(mcInst, insn);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000484 }
485}
486
487/// translateFPRegister - Translates a stack position on the FPU stack to its
488/// LLVM form, and appends it to an MCInst.
489///
490/// @param mcInst - The MCInst to append to.
491/// @param stackPos - The stack position to translate.
Sean Callanana144c3f2010-04-02 21:23:51 +0000492/// @return - 0 on success; nonzero otherwise.
493static bool translateFPRegister(MCInst &mcInst,
494 uint8_t stackPos) {
495 if (stackPos >= 8) {
496 debug("Invalid FP stack position");
497 return true;
498 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000499
500 mcInst.addOperand(MCOperand::CreateReg(X86::ST0 + stackPos));
Sean Callanana144c3f2010-04-02 21:23:51 +0000501
502 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000503}
504
505/// translateOperand - Translates an operand stored in an internal instruction
506/// to LLVM's format and appends it to an MCInst.
507///
508/// @param mcInst - The MCInst to append to.
509/// @param operand - The operand, as stored in the descriptor table.
510/// @param insn - The internal instruction.
Sean Callanana144c3f2010-04-02 21:23:51 +0000511/// @return - false on success; true otherwise.
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000512static bool translateOperand(MCInst &mcInst, const OperandSpecifier &operand,
513 InternalInstruction &insn) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000514 switch (operand.encoding) {
515 default:
Sean Callanana144c3f2010-04-02 21:23:51 +0000516 debug("Unhandled operand encoding during translation");
517 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000518 case ENCODING_REG:
519 translateRegister(mcInst, insn.reg);
Sean Callanana144c3f2010-04-02 21:23:51 +0000520 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000521 case ENCODING_RM:
Sean Callanana144c3f2010-04-02 21:23:51 +0000522 return translateRM(mcInst, operand, insn);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000523 case ENCODING_CB:
524 case ENCODING_CW:
525 case ENCODING_CD:
526 case ENCODING_CP:
527 case ENCODING_CO:
528 case ENCODING_CT:
Sean Callanana144c3f2010-04-02 21:23:51 +0000529 debug("Translation of code offsets isn't supported.");
530 return true;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000531 case ENCODING_IB:
532 case ENCODING_IW:
533 case ENCODING_ID:
534 case ENCODING_IO:
535 case ENCODING_Iv:
536 case ENCODING_Ia:
Sean Callananbe192dd2010-05-05 22:47:27 +0000537 translateImmediate(mcInst,
538 insn.immediates[insn.numImmediatesTranslated++],
539 operand,
540 insn);
Sean Callanana144c3f2010-04-02 21:23:51 +0000541 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000542 case ENCODING_RB:
543 case ENCODING_RW:
544 case ENCODING_RD:
545 case ENCODING_RO:
546 translateRegister(mcInst, insn.opcodeRegister);
Sean Callanana144c3f2010-04-02 21:23:51 +0000547 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000548 case ENCODING_I:
Sean Callanana144c3f2010-04-02 21:23:51 +0000549 return translateFPRegister(mcInst, insn.opcodeModifier);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000550 case ENCODING_Rv:
551 translateRegister(mcInst, insn.opcodeRegister);
Sean Callanana144c3f2010-04-02 21:23:51 +0000552 return false;
Sean Callanana21e2ea2011-03-15 01:23:15 +0000553 case ENCODING_VVVV:
554 translateRegister(mcInst, insn.vvvv);
555 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000556 case ENCODING_DUP:
Sean Callanana144c3f2010-04-02 21:23:51 +0000557 return translateOperand(mcInst,
558 insn.spec->operands[operand.type - TYPE_DUP0],
559 insn);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000560 }
561}
562
563/// translateInstruction - Translates an internal instruction and all its
564/// operands to an MCInst.
565///
566/// @param mcInst - The MCInst to populate with the instruction's data.
567/// @param insn - The internal instruction.
Sean Callanana144c3f2010-04-02 21:23:51 +0000568/// @return - false on success; true otherwise.
569static bool translateInstruction(MCInst &mcInst,
570 InternalInstruction &insn) {
571 if (!insn.spec) {
572 debug("Instruction has no specification");
573 return true;
574 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000575
576 mcInst.setOpcode(insn.instructionID);
577
578 int index;
579
580 insn.numImmediatesTranslated = 0;
581
582 for (index = 0; index < X86_MAX_OPERANDS; ++index) {
Sean Callanana144c3f2010-04-02 21:23:51 +0000583 if (insn.spec->operands[index].encoding != ENCODING_NONE) {
584 if (translateOperand(mcInst, insn.spec->operands[index], insn)) {
585 return true;
586 }
587 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000588 }
Sean Callanana144c3f2010-04-02 21:23:51 +0000589
590 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000591}
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +0000592
James Molloyb9505852011-09-07 17:24:38 +0000593static MCDisassembler *createX86_32Disassembler(const Target &T, const MCSubtargetInfo &STI) {
Craig Topper224c1b22011-12-21 08:06:52 +0000594 return new X86Disassembler::X86GenericDisassembler(STI, MODE_32BIT);
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +0000595}
596
James Molloyb9505852011-09-07 17:24:38 +0000597static MCDisassembler *createX86_64Disassembler(const Target &T, const MCSubtargetInfo &STI) {
Craig Topper224c1b22011-12-21 08:06:52 +0000598 return new X86Disassembler::X86GenericDisassembler(STI, MODE_64BIT);
Daniel Dunbar5f9b9ef2009-11-25 06:53:08 +0000599}
600
601extern "C" void LLVMInitializeX86Disassembler() {
602 // Register the disassembler.
603 TargetRegistry::RegisterMCDisassembler(TheX86_32Target,
604 createX86_32Disassembler);
605 TargetRegistry::RegisterMCDisassembler(TheX86_64Target,
606 createX86_64Disassembler);
607}