blob: f03ed13d13c0c311e9692dd24d24000523135a0d [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- X86Disassembler.cpp - Disassembler for x86 and x86_64 -------------===//
Daniel Dunbar900f2ce2009-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 Callanan04cc3072009-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"
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +000019#include "llvm/MC/MCContext.h"
Sean Callanan04cc3072009-12-19 02:59:52 +000020#include "llvm/MC/MCDisassembler.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/MC/MCExpr.h"
Sean Callanan04cc3072009-12-19 02:59:52 +000022#include "llvm/MC/MCInst.h"
Benjamin Kramer478e8de2012-02-11 14:50:54 +000023#include "llvm/MC/MCInstrInfo.h"
James Molloy4c493e82011-09-07 17:24:38 +000024#include "llvm/MC/MCSubtargetInfo.h"
Sean Callanan010b3732010-04-02 21:23:51 +000025#include "llvm/Support/Debug.h"
Sean Callanan04cc3072009-12-19 02:59:52 +000026#include "llvm/Support/MemoryObject.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000027#include "llvm/Support/TargetRegistry.h"
Sean Callanan04cc3072009-12-19 02:59:52 +000028#include "llvm/Support/raw_ostream.h"
Sean Callanan5c8f4cd2009-12-22 01:11:26 +000029
Evan Chengd9997ac2011-06-27 18:32:37 +000030#define GET_REGINFO_ENUM
31#include "X86GenRegisterInfo.inc"
Kevin Enderby5b03f722011-09-02 20:01:23 +000032#define GET_INSTRINFO_ENUM
33#include "X86GenInstrInfo.inc"
David Woodhouse7dd21822014-01-20 12:02:31 +000034#define GET_SUBTARGETINFO_ENUM
35#include "X86GenSubtargetInfo.inc"
Sean Callanan5c8f4cd2009-12-22 01:11:26 +000036
Daniel Dunbar900f2ce2009-11-25 06:53:08 +000037using namespace llvm;
Sean Callanan04cc3072009-12-19 02:59:52 +000038using namespace llvm::X86Disassembler;
39
Sean Callanan010b3732010-04-02 21:23:51 +000040void x86DisassemblerDebug(const char *file,
41 unsigned line,
42 const char *s) {
43 dbgs() << file << ":" << line << ": " << s;
44}
45
Roman Divacky67923802012-09-05 21:17:34 +000046const char *x86DisassemblerGetInstrName(unsigned Opcode, const void *mii) {
Benjamin Kramer478e8de2012-02-11 14:50:54 +000047 const MCInstrInfo *MII = static_cast<const MCInstrInfo *>(mii);
48 return MII->getName(Opcode);
49}
50
Sean Callanan010b3732010-04-02 21:23:51 +000051#define debug(s) DEBUG(x86DisassemblerDebug(__FILE__, __LINE__, s));
52
Sean Callanan04cc3072009-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 Callanan5c8f4cd2009-12-22 01:11:26 +000069extern Target TheX86_32Target, TheX86_64Target;
70
Sean Callanan04cc3072009-12-19 02:59:52 +000071}
72
Sean Callanan010b3732010-04-02 21:23:51 +000073static bool translateInstruction(MCInst &target,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +000074 InternalInstruction &source,
75 const MCDisassembler *Dis);
Sean Callanan04cc3072009-12-19 02:59:52 +000076
Lang Hames0563ca12014-04-13 04:09:16 +000077X86GenericDisassembler::X86GenericDisassembler(
78 const MCSubtargetInfo &STI,
Lang Hamesa1bc0f52014-04-15 04:40:56 +000079 MCContext &Ctx,
Lang Hames0563ca12014-04-13 04:09:16 +000080 std::unique_ptr<const MCInstrInfo> MII)
Lang Hamesa1bc0f52014-04-15 04:40:56 +000081 : MCDisassembler(STI, Ctx), MII(std::move(MII)) {
David Woodhouse7dd21822014-01-20 12:02:31 +000082 switch (STI.getFeatureBits() &
83 (X86::Mode16Bit | X86::Mode32Bit | X86::Mode64Bit)) {
84 case X86::Mode16Bit:
85 fMode = MODE_16BIT;
86 break;
87 case X86::Mode32Bit:
88 fMode = MODE_32BIT;
89 break;
90 case X86::Mode64Bit:
91 fMode = MODE_64BIT;
92 break;
93 default:
94 llvm_unreachable("Invalid CPU mode");
95 }
96}
Sean Callanan04cc3072009-12-19 02:59:52 +000097
Sean Callanan04cc3072009-12-19 02:59:52 +000098/// regionReader - a callback function that wraps the readByte method from
99/// MemoryObject.
100///
101/// @param arg - The generic callback parameter. In this case, this should
102/// be a pointer to a MemoryObject.
103/// @param byte - A pointer to the byte to be read.
104/// @param address - The address to be read.
Roman Divacky67923802012-09-05 21:17:34 +0000105static int regionReader(const void* arg, uint8_t* byte, uint64_t address) {
106 const MemoryObject* region = static_cast<const MemoryObject*>(arg);
Sean Callanan04cc3072009-12-19 02:59:52 +0000107 return region->readByte(address, byte);
108}
109
110/// logger - a callback function that wraps the operator<< method from
111/// raw_ostream.
112///
113/// @param arg - The generic callback parameter. This should be a pointe
114/// to a raw_ostream.
115/// @param log - A string to be logged. logger() adds a newline.
116static void logger(void* arg, const char* log) {
117 if (!arg)
118 return;
119
120 raw_ostream &vStream = *(static_cast<raw_ostream*>(arg));
121 vStream << log << "\n";
122}
123
124//
125// Public interface for the disassembler
126//
127
Owen Andersona4043c42011-08-17 17:44:15 +0000128MCDisassembler::DecodeStatus
129X86GenericDisassembler::getInstruction(MCInst &instr,
130 uint64_t &size,
Derek Schuff56b662c2012-02-29 01:09:06 +0000131 const MemoryObject &region,
Owen Andersona4043c42011-08-17 17:44:15 +0000132 uint64_t address,
Owen Andersona0c3b972011-09-15 23:38:46 +0000133 raw_ostream &vStream,
134 raw_ostream &cStream) const {
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000135 CommentStream = &cStream;
136
Sean Callanan04cc3072009-12-19 02:59:52 +0000137 InternalInstruction internalInstr;
Benjamin Kramere5e189f2011-09-21 21:47:35 +0000138
139 dlog_t loggerFn = logger;
140 if (&vStream == &nulls())
141 loggerFn = 0; // Disable logging completely if it's going to nulls().
Sean Callanan04cc3072009-12-19 02:59:52 +0000142
143 int ret = decodeInstruction(&internalInstr,
144 regionReader,
Roman Divacky67923802012-09-05 21:17:34 +0000145 (const void*)&region,
Benjamin Kramere5e189f2011-09-21 21:47:35 +0000146 loggerFn,
Sean Callanan04cc3072009-12-19 02:59:52 +0000147 (void*)&vStream,
Lang Hames0563ca12014-04-13 04:09:16 +0000148 (const void*)MII.get(),
Sean Callanan04cc3072009-12-19 02:59:52 +0000149 address,
150 fMode);
151
Sean Callanan010b3732010-04-02 21:23:51 +0000152 if (ret) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000153 size = internalInstr.readerCursor - address;
Owen Andersona4043c42011-08-17 17:44:15 +0000154 return Fail;
Sean Callanan04cc3072009-12-19 02:59:52 +0000155 }
156 else {
157 size = internalInstr.length;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000158 return (!translateInstruction(instr, internalInstr, this)) ?
159 Success : Fail;
Sean Callanan04cc3072009-12-19 02:59:52 +0000160 }
161}
162
163//
164// Private code that translates from struct InternalInstructions to MCInsts.
165//
166
167/// translateRegister - Translates an internal register to the appropriate LLVM
168/// register, and appends it as an operand to an MCInst.
169///
170/// @param mcInst - The MCInst to append to.
171/// @param reg - The Reg to append.
172static void translateRegister(MCInst &mcInst, Reg reg) {
173#define ENTRY(x) X86::x,
174 uint8_t llvmRegnums[] = {
175 ALL_REGS
176 0
177 };
178#undef ENTRY
179
180 uint8_t llvmRegnum = llvmRegnums[reg];
181 mcInst.addOperand(MCOperand::CreateReg(llvmRegnum));
182}
183
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000184/// tryAddingSymbolicOperand - trys to add a symbolic operand in place of the
185/// immediate Value in the MCInst.
186///
187/// @param Value - The immediate Value, has had any PC adjustment made by
188/// the caller.
189/// @param isBranch - If the instruction is a branch instruction
190/// @param Address - The starting address of the instruction
191/// @param Offset - The byte offset to this immediate in the instruction
192/// @param Width - The byte width of this immediate in the instruction
193///
194/// If the getOpInfo() function was set when setupForSymbolicDisassembly() was
195/// called then that function is called to get any symbolic information for the
196/// immediate in the instruction using the Address, Offset and Width. If that
197/// returns non-zero then the symbolic information it returns is used to create
198/// an MCExpr and that is added as an operand to the MCInst. If getOpInfo()
199/// returns zero and isBranch is true then a symbol look up for immediate Value
200/// is done and if a symbol is found an MCExpr is created with that, else
201/// an MCExpr with the immediate Value is created. This function returns true
202/// if it adds an operand to the MCInst and false otherwise.
203static bool tryAddingSymbolicOperand(int64_t Value, bool isBranch,
204 uint64_t Address, uint64_t Offset,
205 uint64_t Width, MCInst &MI,
206 const MCDisassembler *Dis) {
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000207 return Dis->tryAddingSymbolicOperand(MI, Value, Address, isBranch,
208 Offset, Width);
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000209}
210
Kevin Enderbyb119c082012-02-29 22:58:34 +0000211/// tryAddingPcLoadReferenceComment - trys to add a comment as to what is being
212/// referenced by a load instruction with the base register that is the rip.
213/// These can often be addresses in a literal pool. The Address of the
214/// instruction and its immediate Value are used to determine the address
215/// being referenced in the literal pool entry. The SymbolLookUp call back will
216/// return a pointer to a literal 'C' string if the referenced address is an
217/// address into a section with 'C' string literals.
218static void tryAddingPcLoadReferenceComment(uint64_t Address, uint64_t Value,
219 const void *Decoder) {
220 const MCDisassembler *Dis = static_cast<const MCDisassembler*>(Decoder);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000221 Dis->tryAddingPcLoadReferenceComment(Value, Address);
Kevin Enderbyb119c082012-02-29 22:58:34 +0000222}
223
Craig Topper35da3d12014-01-16 07:36:58 +0000224static const uint8_t segmentRegnums[SEG_OVERRIDE_max] = {
225 0, // SEG_OVERRIDE_NONE
226 X86::CS,
227 X86::SS,
228 X86::DS,
229 X86::ES,
230 X86::FS,
231 X86::GS
232};
233
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000234/// translateSrcIndex - Appends a source index operand to an MCInst.
235///
236/// @param mcInst - The MCInst to append to.
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000237/// @param insn - The internal instruction.
238static bool translateSrcIndex(MCInst &mcInst, InternalInstruction &insn) {
239 unsigned baseRegNo;
240
241 if (insn.mode == MODE_64BIT)
242 baseRegNo = insn.prefixPresent[0x67] ? X86::ESI : X86::RSI;
243 else if (insn.mode == MODE_32BIT)
244 baseRegNo = insn.prefixPresent[0x67] ? X86::SI : X86::ESI;
David Woodhousefee418c2014-01-22 15:31:29 +0000245 else {
246 assert(insn.mode == MODE_16BIT);
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000247 baseRegNo = insn.prefixPresent[0x67] ? X86::ESI : X86::SI;
David Woodhousefee418c2014-01-22 15:31:29 +0000248 }
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000249 MCOperand baseReg = MCOperand::CreateReg(baseRegNo);
250 mcInst.addOperand(baseReg);
251
252 MCOperand segmentReg;
253 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
254 mcInst.addOperand(segmentReg);
255 return false;
256}
257
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000258/// translateDstIndex - Appends a destination index operand to an MCInst.
259///
260/// @param mcInst - The MCInst to append to.
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000261/// @param insn - The internal instruction.
262
263static bool translateDstIndex(MCInst &mcInst, InternalInstruction &insn) {
264 unsigned baseRegNo;
265
266 if (insn.mode == MODE_64BIT)
267 baseRegNo = insn.prefixPresent[0x67] ? X86::EDI : X86::RDI;
268 else if (insn.mode == MODE_32BIT)
269 baseRegNo = insn.prefixPresent[0x67] ? X86::DI : X86::EDI;
David Woodhousefee418c2014-01-22 15:31:29 +0000270 else {
271 assert(insn.mode == MODE_16BIT);
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000272 baseRegNo = insn.prefixPresent[0x67] ? X86::EDI : X86::DI;
David Woodhousefee418c2014-01-22 15:31:29 +0000273 }
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000274 MCOperand baseReg = MCOperand::CreateReg(baseRegNo);
275 mcInst.addOperand(baseReg);
276 return false;
277}
278
Sean Callanan04cc3072009-12-19 02:59:52 +0000279/// translateImmediate - Appends an immediate operand to an MCInst.
280///
281/// @param mcInst - The MCInst to append to.
282/// @param immediate - The immediate value to append.
Sean Callanan4cd930f2010-05-05 22:47:27 +0000283/// @param operand - The operand, as stored in the descriptor table.
284/// @param insn - The internal instruction.
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +0000285static void translateImmediate(MCInst &mcInst, uint64_t immediate,
286 const OperandSpecifier &operand,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000287 InternalInstruction &insn,
288 const MCDisassembler *Dis) {
Sean Callanan4cd930f2010-05-05 22:47:27 +0000289 // Sign-extend the immediate if necessary.
290
Craig Topper6dedbae2012-03-04 02:16:41 +0000291 OperandType type = (OperandType)operand.type;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000292
Kevin Enderbyec4bd312012-04-18 23:12:11 +0000293 bool isBranch = false;
294 uint64_t pcrel = 0;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000295 if (type == TYPE_RELv) {
Kevin Enderbyec4bd312012-04-18 23:12:11 +0000296 isBranch = true;
297 pcrel = insn.startLocation +
Kevin Enderby216ac312012-07-24 21:40:01 +0000298 insn.immediateOffset + insn.immediateSize;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000299 switch (insn.displacementSize) {
300 default:
301 break;
Sean Callanan5e8603d2011-02-21 21:55:05 +0000302 case 1:
Craig Topper18854172013-08-25 22:23:38 +0000303 if(immediate & 0x80)
304 immediate |= ~(0xffull);
Sean Callanan4cd930f2010-05-05 22:47:27 +0000305 break;
Sean Callanan5e8603d2011-02-21 21:55:05 +0000306 case 2:
Craig Topper18854172013-08-25 22:23:38 +0000307 if(immediate & 0x8000)
308 immediate |= ~(0xffffull);
Sean Callanan4cd930f2010-05-05 22:47:27 +0000309 break;
Sean Callanan5e8603d2011-02-21 21:55:05 +0000310 case 4:
Craig Topper18854172013-08-25 22:23:38 +0000311 if(immediate & 0x80000000)
312 immediate |= ~(0xffffffffull);
Sean Callanan4cd930f2010-05-05 22:47:27 +0000313 break;
Sean Callanan5e8603d2011-02-21 21:55:05 +0000314 case 8:
Sean Callanan4cd930f2010-05-05 22:47:27 +0000315 break;
316 }
317 }
Kevin Enderby5b03f722011-09-02 20:01:23 +0000318 // By default sign-extend all X86 immediates based on their encoding.
319 else if (type == TYPE_IMM8 || type == TYPE_IMM16 || type == TYPE_IMM32 ||
320 type == TYPE_IMM64) {
321 uint32_t Opcode = mcInst.getOpcode();
322 switch (operand.encoding) {
323 default:
324 break;
325 case ENCODING_IB:
326 // Special case those X86 instructions that use the imm8 as a set of
327 // bits, bit count, etc. and are not sign-extend.
328 if (Opcode != X86::BLENDPSrri && Opcode != X86::BLENDPDrri &&
Bill Wendlingea6397f2012-07-19 00:11:40 +0000329 Opcode != X86::PBLENDWrri && Opcode != X86::MPSADBWrri &&
330 Opcode != X86::DPPSrri && Opcode != X86::DPPDrri &&
331 Opcode != X86::INSERTPSrr && Opcode != X86::VBLENDPSYrri &&
332 Opcode != X86::VBLENDPSYrmi && Opcode != X86::VBLENDPDYrri &&
333 Opcode != X86::VBLENDPDYrmi && Opcode != X86::VPBLENDWrri &&
334 Opcode != X86::VMPSADBWrri && Opcode != X86::VDPPSYrri &&
335 Opcode != X86::VDPPSYrmi && Opcode != X86::VDPPDrri &&
336 Opcode != X86::VINSERTPSrr)
Craig Topper18854172013-08-25 22:23:38 +0000337 if(immediate & 0x80)
338 immediate |= ~(0xffull);
Kevin Enderby5b03f722011-09-02 20:01:23 +0000339 break;
340 case ENCODING_IW:
Craig Topper18854172013-08-25 22:23:38 +0000341 if(immediate & 0x8000)
342 immediate |= ~(0xffffull);
Kevin Enderby5b03f722011-09-02 20:01:23 +0000343 break;
344 case ENCODING_ID:
Craig Topper18854172013-08-25 22:23:38 +0000345 if(immediate & 0x80000000)
346 immediate |= ~(0xffffffffull);
Kevin Enderby5b03f722011-09-02 20:01:23 +0000347 break;
348 case ENCODING_IO:
Kevin Enderby5b03f722011-09-02 20:01:23 +0000349 break;
350 }
351 }
Sean Callanan4cd930f2010-05-05 22:47:27 +0000352
353 switch (type) {
Craig Topperc30fdbc2012-08-31 15:40:30 +0000354 case TYPE_XMM32:
355 case TYPE_XMM64:
Craig Topper96e00e52011-09-14 05:55:28 +0000356 case TYPE_XMM128:
357 mcInst.addOperand(MCOperand::CreateReg(X86::XMM0 + (immediate >> 4)));
358 return;
359 case TYPE_XMM256:
360 mcInst.addOperand(MCOperand::CreateReg(X86::YMM0 + (immediate >> 4)));
361 return;
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000362 case TYPE_XMM512:
363 mcInst.addOperand(MCOperand::CreateReg(X86::ZMM0 + (immediate >> 4)));
364 return;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000365 case TYPE_REL8:
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000366 isBranch = true;
367 pcrel = insn.startLocation + insn.immediateOffset + insn.immediateSize;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000368 if(immediate & 0x80)
369 immediate |= ~(0xffull);
370 break;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000371 case TYPE_REL32:
372 case TYPE_REL64:
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000373 isBranch = true;
374 pcrel = insn.startLocation + insn.immediateOffset + insn.immediateSize;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000375 if(immediate & 0x80000000)
376 immediate |= ~(0xffffffffull);
377 break;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000378 default:
379 // operand is 64 bits wide. Do nothing.
380 break;
381 }
Craig Topper092e2fe2013-08-24 19:50:11 +0000382
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000383 if(!tryAddingSymbolicOperand(immediate + pcrel, isBranch, insn.startLocation,
384 insn.immediateOffset, insn.immediateSize,
385 mcInst, Dis))
386 mcInst.addOperand(MCOperand::CreateImm(immediate));
Craig Topper35da3d12014-01-16 07:36:58 +0000387
388 if (type == TYPE_MOFFS8 || type == TYPE_MOFFS16 ||
389 type == TYPE_MOFFS32 || type == TYPE_MOFFS64) {
390 MCOperand segmentReg;
391 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
392 mcInst.addOperand(segmentReg);
393 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000394}
395
396/// translateRMRegister - Translates a register stored in the R/M field of the
397/// ModR/M byte to its LLVM equivalent and appends it to an MCInst.
398/// @param mcInst - The MCInst to append to.
399/// @param insn - The internal instruction to extract the R/M field
400/// from.
Sean Callanan010b3732010-04-02 21:23:51 +0000401/// @return - 0 on success; -1 otherwise
402static bool translateRMRegister(MCInst &mcInst,
Sean Callanan04cc3072009-12-19 02:59:52 +0000403 InternalInstruction &insn) {
Sean Callanan010b3732010-04-02 21:23:51 +0000404 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
405 debug("A R/M register operand may not have a SIB byte");
406 return true;
407 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000408
409 switch (insn.eaBase) {
Sean Callanan010b3732010-04-02 21:23:51 +0000410 default:
411 debug("Unexpected EA base register");
412 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000413 case EA_BASE_NONE:
Sean Callanan010b3732010-04-02 21:23:51 +0000414 debug("EA_BASE_NONE for ModR/M base");
415 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000416#define ENTRY(x) case EA_BASE_##x:
417 ALL_EA_BASES
418#undef ENTRY
Sean Callanan010b3732010-04-02 21:23:51 +0000419 debug("A R/M register operand may not have a base; "
420 "the operand must be a register.");
421 return true;
422#define ENTRY(x) \
Sean Callanan04cc3072009-12-19 02:59:52 +0000423 case EA_REG_##x: \
424 mcInst.addOperand(MCOperand::CreateReg(X86::x)); break;
425 ALL_REGS
426#undef ENTRY
Sean Callanan04cc3072009-12-19 02:59:52 +0000427 }
Sean Callanan010b3732010-04-02 21:23:51 +0000428
429 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000430}
431
432/// translateRMMemory - Translates a memory operand stored in the Mod and R/M
433/// fields of an internal instruction (and possibly its SIB byte) to a memory
434/// operand in LLVM's format, and appends it to an MCInst.
435///
436/// @param mcInst - The MCInst to append to.
437/// @param insn - The instruction to extract Mod, R/M, and SIB fields
438/// from.
Sean Callanan010b3732010-04-02 21:23:51 +0000439/// @return - 0 on success; nonzero otherwise
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000440static bool translateRMMemory(MCInst &mcInst, InternalInstruction &insn,
441 const MCDisassembler *Dis) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000442 // Addresses in an MCInst are represented as five operands:
443 // 1. basereg (register) The R/M base, or (if there is a SIB) the
444 // SIB base
445 // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified
446 // scale amount
447 // 3. indexreg (register) x86_registerNONE, or (if there is a SIB)
448 // the index (which is multiplied by the
449 // scale amount)
450 // 4. displacement (immediate) 0, or the displacement if there is one
451 // 5. segmentreg (register) x86_registerNONE for now, but could be set
452 // if we have segment overrides
453
454 MCOperand baseReg;
455 MCOperand scaleAmount;
456 MCOperand indexReg;
457 MCOperand displacement;
458 MCOperand segmentReg;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000459 uint64_t pcrel = 0;
Sean Callanan04cc3072009-12-19 02:59:52 +0000460
461 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
462 if (insn.sibBase != SIB_BASE_NONE) {
463 switch (insn.sibBase) {
464 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000465 debug("Unexpected sibBase");
466 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000467#define ENTRY(x) \
Sean Callanan36eab802009-12-22 21:12:55 +0000468 case SIB_BASE_##x: \
Sean Callanan04cc3072009-12-19 02:59:52 +0000469 baseReg = MCOperand::CreateReg(X86::x); break;
470 ALL_SIB_BASES
471#undef ENTRY
472 }
473 } else {
474 baseReg = MCOperand::CreateReg(0);
475 }
Manman Rena0982042012-06-26 19:47:59 +0000476
477 // Check whether we are handling VSIB addressing mode for GATHER.
478 // If sibIndex was set to SIB_INDEX_NONE, index offset is 4 and
479 // we should use SIB_INDEX_XMM4|YMM4 for VSIB.
480 // I don't see a way to get the correct IndexReg in readSIB:
481 // We can tell whether it is VSIB or SIB after instruction ID is decoded,
482 // but instruction ID may not be decoded yet when calling readSIB.
483 uint32_t Opcode = mcInst.getOpcode();
Manman Ren98a5bf22012-06-29 00:54:20 +0000484 bool IndexIs128 = (Opcode == X86::VGATHERDPDrm ||
485 Opcode == X86::VGATHERDPDYrm ||
486 Opcode == X86::VGATHERQPDrm ||
487 Opcode == X86::VGATHERDPSrm ||
488 Opcode == X86::VGATHERQPSrm ||
489 Opcode == X86::VPGATHERDQrm ||
490 Opcode == X86::VPGATHERDQYrm ||
491 Opcode == X86::VPGATHERQQrm ||
492 Opcode == X86::VPGATHERDDrm ||
493 Opcode == X86::VPGATHERQDrm);
494 bool IndexIs256 = (Opcode == X86::VGATHERQPDYrm ||
495 Opcode == X86::VGATHERDPSYrm ||
496 Opcode == X86::VGATHERQPSYrm ||
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000497 Opcode == X86::VGATHERDPDZrm ||
498 Opcode == X86::VPGATHERDQZrm ||
Manman Ren98a5bf22012-06-29 00:54:20 +0000499 Opcode == X86::VPGATHERQQYrm ||
500 Opcode == X86::VPGATHERDDYrm ||
501 Opcode == X86::VPGATHERQDYrm);
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000502 bool IndexIs512 = (Opcode == X86::VGATHERQPDZrm ||
503 Opcode == X86::VGATHERDPSZrm ||
504 Opcode == X86::VGATHERQPSZrm ||
505 Opcode == X86::VPGATHERQQZrm ||
506 Opcode == X86::VPGATHERDDZrm ||
507 Opcode == X86::VPGATHERQDZrm);
508 if (IndexIs128 || IndexIs256 || IndexIs512) {
Manman Rena0982042012-06-26 19:47:59 +0000509 unsigned IndexOffset = insn.sibIndex -
510 (insn.addressSize == 8 ? SIB_INDEX_RAX:SIB_INDEX_EAX);
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000511 SIBIndex IndexBase = IndexIs512 ? SIB_INDEX_ZMM0 :
512 IndexIs256 ? SIB_INDEX_YMM0 : SIB_INDEX_XMM0;
Manman Rena0982042012-06-26 19:47:59 +0000513 insn.sibIndex = (SIBIndex)(IndexBase +
514 (insn.sibIndex == SIB_INDEX_NONE ? 4 : IndexOffset));
515 }
516
Sean Callanan04cc3072009-12-19 02:59:52 +0000517 if (insn.sibIndex != SIB_INDEX_NONE) {
518 switch (insn.sibIndex) {
519 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000520 debug("Unexpected sibIndex");
521 return true;
Sean Callanan36eab802009-12-22 21:12:55 +0000522#define ENTRY(x) \
Sean Callanan04cc3072009-12-19 02:59:52 +0000523 case SIB_INDEX_##x: \
524 indexReg = MCOperand::CreateReg(X86::x); break;
525 EA_BASES_32BIT
526 EA_BASES_64BIT
Manman Rena0982042012-06-26 19:47:59 +0000527 REGS_XMM
528 REGS_YMM
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000529 REGS_ZMM
Sean Callanan04cc3072009-12-19 02:59:52 +0000530#undef ENTRY
531 }
532 } else {
533 indexReg = MCOperand::CreateReg(0);
534 }
535
536 scaleAmount = MCOperand::CreateImm(insn.sibScale);
537 } else {
538 switch (insn.eaBase) {
539 case EA_BASE_NONE:
Sean Callanan010b3732010-04-02 21:23:51 +0000540 if (insn.eaDisplacement == EA_DISP_NONE) {
541 debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
542 return true;
543 }
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000544 if (insn.mode == MODE_64BIT){
545 pcrel = insn.startLocation +
546 insn.displacementOffset + insn.displacementSize;
Kevin Enderbyb119c082012-02-29 22:58:34 +0000547 tryAddingPcLoadReferenceComment(insn.startLocation +
548 insn.displacementOffset,
549 insn.displacement + pcrel, Dis);
Sean Callanan04cc3072009-12-19 02:59:52 +0000550 baseReg = MCOperand::CreateReg(X86::RIP); // Section 2.2.1.6
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000551 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000552 else
553 baseReg = MCOperand::CreateReg(0);
554
555 indexReg = MCOperand::CreateReg(0);
556 break;
557 case EA_BASE_BX_SI:
558 baseReg = MCOperand::CreateReg(X86::BX);
559 indexReg = MCOperand::CreateReg(X86::SI);
560 break;
561 case EA_BASE_BX_DI:
562 baseReg = MCOperand::CreateReg(X86::BX);
563 indexReg = MCOperand::CreateReg(X86::DI);
564 break;
565 case EA_BASE_BP_SI:
566 baseReg = MCOperand::CreateReg(X86::BP);
567 indexReg = MCOperand::CreateReg(X86::SI);
568 break;
569 case EA_BASE_BP_DI:
570 baseReg = MCOperand::CreateReg(X86::BP);
571 indexReg = MCOperand::CreateReg(X86::DI);
572 break;
573 default:
574 indexReg = MCOperand::CreateReg(0);
575 switch (insn.eaBase) {
576 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000577 debug("Unexpected eaBase");
578 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000579 // Here, we will use the fill-ins defined above. However,
580 // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and
581 // sib and sib64 were handled in the top-level if, so they're only
582 // placeholders to keep the compiler happy.
583#define ENTRY(x) \
584 case EA_BASE_##x: \
585 baseReg = MCOperand::CreateReg(X86::x); break;
586 ALL_EA_BASES
587#undef ENTRY
588#define ENTRY(x) case EA_REG_##x:
589 ALL_REGS
590#undef ENTRY
Sean Callanan010b3732010-04-02 21:23:51 +0000591 debug("A R/M memory operand may not be a register; "
592 "the base field must be a base.");
593 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000594 }
595 }
Sean Callanan36eab802009-12-22 21:12:55 +0000596
597 scaleAmount = MCOperand::CreateImm(1);
Sean Callanan04cc3072009-12-19 02:59:52 +0000598 }
599
600 displacement = MCOperand::CreateImm(insn.displacement);
Craig Topper35da3d12014-01-16 07:36:58 +0000601
Sean Callanan04cc3072009-12-19 02:59:52 +0000602 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
603
604 mcInst.addOperand(baseReg);
605 mcInst.addOperand(scaleAmount);
606 mcInst.addOperand(indexReg);
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000607 if(!tryAddingSymbolicOperand(insn.displacement + pcrel, false,
608 insn.startLocation, insn.displacementOffset,
609 insn.displacementSize, mcInst, Dis))
610 mcInst.addOperand(displacement);
Chris Lattner55595fb2010-07-13 04:23:55 +0000611 mcInst.addOperand(segmentReg);
Sean Callanan010b3732010-04-02 21:23:51 +0000612 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000613}
614
615/// translateRM - Translates an operand stored in the R/M (and possibly SIB)
616/// byte of an instruction to LLVM form, and appends it to an MCInst.
617///
618/// @param mcInst - The MCInst to append to.
619/// @param operand - The operand, as stored in the descriptor table.
620/// @param insn - The instruction to extract Mod, R/M, and SIB fields
621/// from.
Sean Callanan010b3732010-04-02 21:23:51 +0000622/// @return - 0 on success; nonzero otherwise
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +0000623static bool translateRM(MCInst &mcInst, const OperandSpecifier &operand,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000624 InternalInstruction &insn, const MCDisassembler *Dis) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000625 switch (operand.type) {
626 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000627 debug("Unexpected type for a R/M operand");
628 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000629 case TYPE_R8:
630 case TYPE_R16:
631 case TYPE_R32:
632 case TYPE_R64:
633 case TYPE_Rv:
634 case TYPE_MM:
635 case TYPE_MM32:
636 case TYPE_MM64:
637 case TYPE_XMM:
638 case TYPE_XMM32:
639 case TYPE_XMM64:
640 case TYPE_XMM128:
Sean Callananc3fd5232011-03-15 01:23:15 +0000641 case TYPE_XMM256:
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000642 case TYPE_XMM512:
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000643 case TYPE_VK1:
644 case TYPE_VK8:
645 case TYPE_VK16:
Sean Callanan04cc3072009-12-19 02:59:52 +0000646 case TYPE_DEBUGREG:
Sean Callanane7e1cf92010-05-06 20:59:00 +0000647 case TYPE_CONTROLREG:
Sean Callanan010b3732010-04-02 21:23:51 +0000648 return translateRMRegister(mcInst, insn);
Sean Callanan04cc3072009-12-19 02:59:52 +0000649 case TYPE_M:
650 case TYPE_M8:
651 case TYPE_M16:
652 case TYPE_M32:
653 case TYPE_M64:
654 case TYPE_M128:
Sean Callananc3fd5232011-03-15 01:23:15 +0000655 case TYPE_M256:
Sean Callanan04cc3072009-12-19 02:59:52 +0000656 case TYPE_M512:
657 case TYPE_Mv:
658 case TYPE_M32FP:
659 case TYPE_M64FP:
660 case TYPE_M80FP:
661 case TYPE_M16INT:
662 case TYPE_M32INT:
663 case TYPE_M64INT:
664 case TYPE_M1616:
665 case TYPE_M1632:
666 case TYPE_M1664:
Sean Callanan36eab802009-12-22 21:12:55 +0000667 case TYPE_LEA:
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000668 return translateRMMemory(mcInst, insn, Dis);
Sean Callanan04cc3072009-12-19 02:59:52 +0000669 }
670}
671
672/// translateFPRegister - Translates a stack position on the FPU stack to its
673/// LLVM form, and appends it to an MCInst.
674///
675/// @param mcInst - The MCInst to append to.
676/// @param stackPos - The stack position to translate.
Craig Topper91551182014-01-01 15:29:32 +0000677static void translateFPRegister(MCInst &mcInst,
678 uint8_t stackPos) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000679 mcInst.addOperand(MCOperand::CreateReg(X86::ST0 + stackPos));
680}
681
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000682/// translateMaskRegister - Translates a 3-bit mask register number to
683/// LLVM form, and appends it to an MCInst.
684///
685/// @param mcInst - The MCInst to append to.
686/// @param maskRegNum - Number of mask register from 0 to 7.
687/// @return - false on success; true otherwise.
688static bool translateMaskRegister(MCInst &mcInst,
689 uint8_t maskRegNum) {
690 if (maskRegNum >= 8) {
691 debug("Invalid mask register number");
692 return true;
693 }
694
695 mcInst.addOperand(MCOperand::CreateReg(X86::K0 + maskRegNum));
696 return false;
697}
698
Sean Callanan04cc3072009-12-19 02:59:52 +0000699/// translateOperand - Translates an operand stored in an internal instruction
700/// to LLVM's format and appends it to an MCInst.
701///
702/// @param mcInst - The MCInst to append to.
703/// @param operand - The operand, as stored in the descriptor table.
704/// @param insn - The internal instruction.
Sean Callanan010b3732010-04-02 21:23:51 +0000705/// @return - false on success; true otherwise.
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +0000706static bool translateOperand(MCInst &mcInst, const OperandSpecifier &operand,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000707 InternalInstruction &insn,
708 const MCDisassembler *Dis) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000709 switch (operand.encoding) {
710 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000711 debug("Unhandled operand encoding during translation");
712 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000713 case ENCODING_REG:
714 translateRegister(mcInst, insn.reg);
Sean Callanan010b3732010-04-02 21:23:51 +0000715 return false;
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000716 case ENCODING_WRITEMASK:
717 return translateMaskRegister(mcInst, insn.writemask);
Sean Callanan04cc3072009-12-19 02:59:52 +0000718 case ENCODING_RM:
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000719 return translateRM(mcInst, operand, insn, Dis);
Sean Callanan04cc3072009-12-19 02:59:52 +0000720 case ENCODING_CB:
721 case ENCODING_CW:
722 case ENCODING_CD:
723 case ENCODING_CP:
724 case ENCODING_CO:
725 case ENCODING_CT:
Sean Callanan010b3732010-04-02 21:23:51 +0000726 debug("Translation of code offsets isn't supported.");
727 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000728 case ENCODING_IB:
729 case ENCODING_IW:
730 case ENCODING_ID:
731 case ENCODING_IO:
732 case ENCODING_Iv:
733 case ENCODING_Ia:
Sean Callanan4cd930f2010-05-05 22:47:27 +0000734 translateImmediate(mcInst,
735 insn.immediates[insn.numImmediatesTranslated++],
736 operand,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000737 insn,
738 Dis);
Sean Callanan010b3732010-04-02 21:23:51 +0000739 return false;
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000740 case ENCODING_SI:
741 return translateSrcIndex(mcInst, insn);
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000742 case ENCODING_DI:
743 return translateDstIndex(mcInst, insn);
Sean Callanan04cc3072009-12-19 02:59:52 +0000744 case ENCODING_RB:
745 case ENCODING_RW:
746 case ENCODING_RD:
747 case ENCODING_RO:
Craig Topper91551182014-01-01 15:29:32 +0000748 case ENCODING_Rv:
Sean Callanan04cc3072009-12-19 02:59:52 +0000749 translateRegister(mcInst, insn.opcodeRegister);
Sean Callanan010b3732010-04-02 21:23:51 +0000750 return false;
Craig Topper623b0d62014-01-01 14:22:37 +0000751 case ENCODING_FP:
Craig Topper91551182014-01-01 15:29:32 +0000752 translateFPRegister(mcInst, insn.modRM & 7);
Sean Callanan010b3732010-04-02 21:23:51 +0000753 return false;
Sean Callananc3fd5232011-03-15 01:23:15 +0000754 case ENCODING_VVVV:
755 translateRegister(mcInst, insn.vvvv);
756 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000757 case ENCODING_DUP:
Craig Topperb8aec082012-08-01 07:39:18 +0000758 return translateOperand(mcInst, insn.operands[operand.type - TYPE_DUP0],
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000759 insn, Dis);
Sean Callanan04cc3072009-12-19 02:59:52 +0000760 }
761}
762
763/// translateInstruction - Translates an internal instruction and all its
764/// operands to an MCInst.
765///
766/// @param mcInst - The MCInst to populate with the instruction's data.
767/// @param insn - The internal instruction.
Sean Callanan010b3732010-04-02 21:23:51 +0000768/// @return - false on success; true otherwise.
769static bool translateInstruction(MCInst &mcInst,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000770 InternalInstruction &insn,
771 const MCDisassembler *Dis) {
Sean Callanan010b3732010-04-02 21:23:51 +0000772 if (!insn.spec) {
773 debug("Instruction has no specification");
774 return true;
775 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000776
777 mcInst.setOpcode(insn.instructionID);
Kevin Enderby35fd7922013-06-20 22:32:18 +0000778 // If when reading the prefix bytes we determined the overlapping 0xf2 or 0xf3
779 // prefix bytes should be disassembled as xrelease and xacquire then set the
780 // opcode to those instead of the rep and repne opcodes.
781 if (insn.xAcquireRelease) {
782 if(mcInst.getOpcode() == X86::REP_PREFIX)
783 mcInst.setOpcode(X86::XRELEASE_PREFIX);
784 else if(mcInst.getOpcode() == X86::REPNE_PREFIX)
785 mcInst.setOpcode(X86::XACQUIRE_PREFIX);
786 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000787
788 int index;
789
790 insn.numImmediatesTranslated = 0;
791
792 for (index = 0; index < X86_MAX_OPERANDS; ++index) {
Craig Topperb8aec082012-08-01 07:39:18 +0000793 if (insn.operands[index].encoding != ENCODING_NONE) {
794 if (translateOperand(mcInst, insn.operands[index], insn, Dis)) {
Sean Callanan010b3732010-04-02 21:23:51 +0000795 return true;
796 }
797 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000798 }
Sean Callanan010b3732010-04-02 21:23:51 +0000799
800 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000801}
Daniel Dunbar900f2ce2009-11-25 06:53:08 +0000802
David Woodhouse7dd21822014-01-20 12:02:31 +0000803static MCDisassembler *createX86Disassembler(const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000804 const MCSubtargetInfo &STI,
805 MCContext &Ctx) {
Lang Hames0563ca12014-04-13 04:09:16 +0000806 std::unique_ptr<const MCInstrInfo> MII(T.createMCInstrInfo());
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000807 return new X86Disassembler::X86GenericDisassembler(STI, Ctx, std::move(MII));
Daniel Dunbar900f2ce2009-11-25 06:53:08 +0000808}
809
810extern "C" void LLVMInitializeX86Disassembler() {
811 // Register the disassembler.
812 TargetRegistry::RegisterMCDisassembler(TheX86_32Target,
David Woodhouse7dd21822014-01-20 12:02:31 +0000813 createX86Disassembler);
Daniel Dunbar900f2ce2009-11-25 06:53:08 +0000814 TargetRegistry::RegisterMCDisassembler(TheX86_64Target,
David Woodhouse7dd21822014-01-20 12:02:31 +0000815 createX86Disassembler);
Daniel Dunbar900f2ce2009-11-25 06:53:08 +0000816}