blob: 521bd21b81c6ebbc4bbaca4227a31ccb814a3d93 [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
Chandler Carruthd174b722014-04-22 02:03:14 +000030using namespace llvm;
31using namespace llvm::X86Disassembler;
32
33#define DEBUG_TYPE "x86-disassembler"
34
Evan Chengd9997ac2011-06-27 18:32:37 +000035#define GET_REGINFO_ENUM
36#include "X86GenRegisterInfo.inc"
Kevin Enderby5b03f722011-09-02 20:01:23 +000037#define GET_INSTRINFO_ENUM
38#include "X86GenInstrInfo.inc"
David Woodhouse7dd21822014-01-20 12:02:31 +000039#define GET_SUBTARGETINFO_ENUM
40#include "X86GenSubtargetInfo.inc"
Sean Callanan5c8f4cd2009-12-22 01:11:26 +000041
Richard Smith89ee75d2014-04-20 21:07:34 +000042void llvm::X86Disassembler::Debug(const char *file, unsigned line,
43 const char *s) {
Sean Callanan010b3732010-04-02 21:23:51 +000044 dbgs() << file << ":" << line << ": " << s;
45}
46
Richard Smith89ee75d2014-04-20 21:07:34 +000047const char *llvm::X86Disassembler::GetInstrName(unsigned Opcode,
48 const void *mii) {
Benjamin Kramer478e8de2012-02-11 14:50:54 +000049 const MCInstrInfo *MII = static_cast<const MCInstrInfo *>(mii);
50 return MII->getName(Opcode);
51}
52
Richard Smith89ee75d2014-04-20 21:07:34 +000053#define debug(s) DEBUG(Debug(__FILE__, __LINE__, s));
Sean Callanan010b3732010-04-02 21:23:51 +000054
Sean Callanan04cc3072009-12-19 02:59:52 +000055namespace llvm {
56
57// Fill-ins to make the compiler happy. These constants are never actually
58// assigned; they are just filler to make an automatically-generated switch
59// statement work.
60namespace X86 {
61 enum {
62 BX_SI = 500,
63 BX_DI = 501,
64 BP_SI = 502,
65 BP_DI = 503,
66 sib = 504,
67 sib64 = 505
68 };
69}
70
Sean Callanan5c8f4cd2009-12-22 01:11:26 +000071extern Target TheX86_32Target, TheX86_64Target;
72
Sean Callanan04cc3072009-12-19 02:59:52 +000073}
74
Sean Callanan010b3732010-04-02 21:23:51 +000075static bool translateInstruction(MCInst &target,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +000076 InternalInstruction &source,
77 const MCDisassembler *Dis);
Sean Callanan04cc3072009-12-19 02:59:52 +000078
Lang Hames0563ca12014-04-13 04:09:16 +000079X86GenericDisassembler::X86GenericDisassembler(
80 const MCSubtargetInfo &STI,
Lang Hamesa1bc0f52014-04-15 04:40:56 +000081 MCContext &Ctx,
Lang Hames0563ca12014-04-13 04:09:16 +000082 std::unique_ptr<const MCInstrInfo> MII)
Lang Hamesa1bc0f52014-04-15 04:40:56 +000083 : MCDisassembler(STI, Ctx), MII(std::move(MII)) {
David Woodhouse7dd21822014-01-20 12:02:31 +000084 switch (STI.getFeatureBits() &
85 (X86::Mode16Bit | X86::Mode32Bit | X86::Mode64Bit)) {
86 case X86::Mode16Bit:
87 fMode = MODE_16BIT;
88 break;
89 case X86::Mode32Bit:
90 fMode = MODE_32BIT;
91 break;
92 case X86::Mode64Bit:
93 fMode = MODE_64BIT;
94 break;
95 default:
96 llvm_unreachable("Invalid CPU mode");
97 }
98}
Sean Callanan04cc3072009-12-19 02:59:52 +000099
Sean Callanan04cc3072009-12-19 02:59:52 +0000100/// regionReader - a callback function that wraps the readByte method from
101/// MemoryObject.
102///
103/// @param arg - The generic callback parameter. In this case, this should
104/// be a pointer to a MemoryObject.
105/// @param byte - A pointer to the byte to be read.
106/// @param address - The address to be read.
Roman Divacky67923802012-09-05 21:17:34 +0000107static int regionReader(const void* arg, uint8_t* byte, uint64_t address) {
108 const MemoryObject* region = static_cast<const MemoryObject*>(arg);
Sean Callanan04cc3072009-12-19 02:59:52 +0000109 return region->readByte(address, byte);
110}
111
112/// logger - a callback function that wraps the operator<< method from
113/// raw_ostream.
114///
115/// @param arg - The generic callback parameter. This should be a pointe
116/// to a raw_ostream.
117/// @param log - A string to be logged. logger() adds a newline.
118static void logger(void* arg, const char* log) {
119 if (!arg)
120 return;
121
122 raw_ostream &vStream = *(static_cast<raw_ostream*>(arg));
123 vStream << log << "\n";
124}
125
126//
127// Public interface for the disassembler
128//
129
Owen Andersona4043c42011-08-17 17:44:15 +0000130MCDisassembler::DecodeStatus
131X86GenericDisassembler::getInstruction(MCInst &instr,
132 uint64_t &size,
Derek Schuff56b662c2012-02-29 01:09:06 +0000133 const MemoryObject &region,
Owen Andersona4043c42011-08-17 17:44:15 +0000134 uint64_t address,
Owen Andersona0c3b972011-09-15 23:38:46 +0000135 raw_ostream &vStream,
136 raw_ostream &cStream) const {
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000137 CommentStream = &cStream;
138
Sean Callanan04cc3072009-12-19 02:59:52 +0000139 InternalInstruction internalInstr;
Benjamin Kramere5e189f2011-09-21 21:47:35 +0000140
141 dlog_t loggerFn = logger;
142 if (&vStream == &nulls())
Craig Topper062a2ba2014-04-25 05:30:21 +0000143 loggerFn = nullptr; // Disable logging completely if it's going to nulls().
Sean Callanan04cc3072009-12-19 02:59:52 +0000144
145 int ret = decodeInstruction(&internalInstr,
146 regionReader,
Roman Divacky67923802012-09-05 21:17:34 +0000147 (const void*)&region,
Benjamin Kramere5e189f2011-09-21 21:47:35 +0000148 loggerFn,
Sean Callanan04cc3072009-12-19 02:59:52 +0000149 (void*)&vStream,
Lang Hames0563ca12014-04-13 04:09:16 +0000150 (const void*)MII.get(),
Sean Callanan04cc3072009-12-19 02:59:52 +0000151 address,
152 fMode);
153
Sean Callanan010b3732010-04-02 21:23:51 +0000154 if (ret) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000155 size = internalInstr.readerCursor - address;
Owen Andersona4043c42011-08-17 17:44:15 +0000156 return Fail;
Sean Callanan04cc3072009-12-19 02:59:52 +0000157 }
158 else {
159 size = internalInstr.length;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000160 return (!translateInstruction(instr, internalInstr, this)) ?
161 Success : Fail;
Sean Callanan04cc3072009-12-19 02:59:52 +0000162 }
163}
164
165//
166// Private code that translates from struct InternalInstructions to MCInsts.
167//
168
169/// translateRegister - Translates an internal register to the appropriate LLVM
170/// register, and appends it as an operand to an MCInst.
171///
172/// @param mcInst - The MCInst to append to.
173/// @param reg - The Reg to append.
174static void translateRegister(MCInst &mcInst, Reg reg) {
175#define ENTRY(x) X86::x,
176 uint8_t llvmRegnums[] = {
177 ALL_REGS
178 0
179 };
180#undef ENTRY
181
182 uint8_t llvmRegnum = llvmRegnums[reg];
183 mcInst.addOperand(MCOperand::CreateReg(llvmRegnum));
184}
185
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000186/// tryAddingSymbolicOperand - trys to add a symbolic operand in place of the
187/// immediate Value in the MCInst.
188///
189/// @param Value - The immediate Value, has had any PC adjustment made by
190/// the caller.
191/// @param isBranch - If the instruction is a branch instruction
192/// @param Address - The starting address of the instruction
193/// @param Offset - The byte offset to this immediate in the instruction
194/// @param Width - The byte width of this immediate in the instruction
195///
196/// If the getOpInfo() function was set when setupForSymbolicDisassembly() was
197/// called then that function is called to get any symbolic information for the
198/// immediate in the instruction using the Address, Offset and Width. If that
199/// returns non-zero then the symbolic information it returns is used to create
200/// an MCExpr and that is added as an operand to the MCInst. If getOpInfo()
201/// returns zero and isBranch is true then a symbol look up for immediate Value
202/// is done and if a symbol is found an MCExpr is created with that, else
203/// an MCExpr with the immediate Value is created. This function returns true
204/// if it adds an operand to the MCInst and false otherwise.
205static bool tryAddingSymbolicOperand(int64_t Value, bool isBranch,
206 uint64_t Address, uint64_t Offset,
207 uint64_t Width, MCInst &MI,
208 const MCDisassembler *Dis) {
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000209 return Dis->tryAddingSymbolicOperand(MI, Value, Address, isBranch,
210 Offset, Width);
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000211}
212
Kevin Enderbyb119c082012-02-29 22:58:34 +0000213/// tryAddingPcLoadReferenceComment - trys to add a comment as to what is being
214/// referenced by a load instruction with the base register that is the rip.
215/// These can often be addresses in a literal pool. The Address of the
216/// instruction and its immediate Value are used to determine the address
217/// being referenced in the literal pool entry. The SymbolLookUp call back will
218/// return a pointer to a literal 'C' string if the referenced address is an
219/// address into a section with 'C' string literals.
220static void tryAddingPcLoadReferenceComment(uint64_t Address, uint64_t Value,
221 const void *Decoder) {
222 const MCDisassembler *Dis = static_cast<const MCDisassembler*>(Decoder);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000223 Dis->tryAddingPcLoadReferenceComment(Value, Address);
Kevin Enderbyb119c082012-02-29 22:58:34 +0000224}
225
Craig Topper35da3d12014-01-16 07:36:58 +0000226static const uint8_t segmentRegnums[SEG_OVERRIDE_max] = {
227 0, // SEG_OVERRIDE_NONE
228 X86::CS,
229 X86::SS,
230 X86::DS,
231 X86::ES,
232 X86::FS,
233 X86::GS
234};
235
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000236/// translateSrcIndex - Appends a source index operand to an MCInst.
237///
238/// @param mcInst - The MCInst to append to.
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000239/// @param insn - The internal instruction.
240static bool translateSrcIndex(MCInst &mcInst, InternalInstruction &insn) {
241 unsigned baseRegNo;
242
243 if (insn.mode == MODE_64BIT)
244 baseRegNo = insn.prefixPresent[0x67] ? X86::ESI : X86::RSI;
245 else if (insn.mode == MODE_32BIT)
246 baseRegNo = insn.prefixPresent[0x67] ? X86::SI : X86::ESI;
David Woodhousefee418c2014-01-22 15:31:29 +0000247 else {
248 assert(insn.mode == MODE_16BIT);
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000249 baseRegNo = insn.prefixPresent[0x67] ? X86::ESI : X86::SI;
David Woodhousefee418c2014-01-22 15:31:29 +0000250 }
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000251 MCOperand baseReg = MCOperand::CreateReg(baseRegNo);
252 mcInst.addOperand(baseReg);
253
254 MCOperand segmentReg;
255 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
256 mcInst.addOperand(segmentReg);
257 return false;
258}
259
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000260/// translateDstIndex - Appends a destination index operand to an MCInst.
261///
262/// @param mcInst - The MCInst to append to.
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000263/// @param insn - The internal instruction.
264
265static bool translateDstIndex(MCInst &mcInst, InternalInstruction &insn) {
266 unsigned baseRegNo;
267
268 if (insn.mode == MODE_64BIT)
269 baseRegNo = insn.prefixPresent[0x67] ? X86::EDI : X86::RDI;
270 else if (insn.mode == MODE_32BIT)
271 baseRegNo = insn.prefixPresent[0x67] ? X86::DI : X86::EDI;
David Woodhousefee418c2014-01-22 15:31:29 +0000272 else {
273 assert(insn.mode == MODE_16BIT);
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000274 baseRegNo = insn.prefixPresent[0x67] ? X86::EDI : X86::DI;
David Woodhousefee418c2014-01-22 15:31:29 +0000275 }
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000276 MCOperand baseReg = MCOperand::CreateReg(baseRegNo);
277 mcInst.addOperand(baseReg);
278 return false;
279}
280
Sean Callanan04cc3072009-12-19 02:59:52 +0000281/// translateImmediate - Appends an immediate operand to an MCInst.
282///
283/// @param mcInst - The MCInst to append to.
284/// @param immediate - The immediate value to append.
Sean Callanan4cd930f2010-05-05 22:47:27 +0000285/// @param operand - The operand, as stored in the descriptor table.
286/// @param insn - The internal instruction.
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +0000287static void translateImmediate(MCInst &mcInst, uint64_t immediate,
288 const OperandSpecifier &operand,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000289 InternalInstruction &insn,
290 const MCDisassembler *Dis) {
Sean Callanan4cd930f2010-05-05 22:47:27 +0000291 // Sign-extend the immediate if necessary.
292
Craig Topper6dedbae2012-03-04 02:16:41 +0000293 OperandType type = (OperandType)operand.type;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000294
Kevin Enderbyec4bd312012-04-18 23:12:11 +0000295 bool isBranch = false;
296 uint64_t pcrel = 0;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000297 if (type == TYPE_RELv) {
Kevin Enderbyec4bd312012-04-18 23:12:11 +0000298 isBranch = true;
299 pcrel = insn.startLocation +
Kevin Enderby216ac312012-07-24 21:40:01 +0000300 insn.immediateOffset + insn.immediateSize;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000301 switch (insn.displacementSize) {
302 default:
303 break;
Sean Callanan5e8603d2011-02-21 21:55:05 +0000304 case 1:
Craig Topper18854172013-08-25 22:23:38 +0000305 if(immediate & 0x80)
306 immediate |= ~(0xffull);
Sean Callanan4cd930f2010-05-05 22:47:27 +0000307 break;
Sean Callanan5e8603d2011-02-21 21:55:05 +0000308 case 2:
Craig Topper18854172013-08-25 22:23:38 +0000309 if(immediate & 0x8000)
310 immediate |= ~(0xffffull);
Sean Callanan4cd930f2010-05-05 22:47:27 +0000311 break;
Sean Callanan5e8603d2011-02-21 21:55:05 +0000312 case 4:
Craig Topper18854172013-08-25 22:23:38 +0000313 if(immediate & 0x80000000)
314 immediate |= ~(0xffffffffull);
Sean Callanan4cd930f2010-05-05 22:47:27 +0000315 break;
Sean Callanan5e8603d2011-02-21 21:55:05 +0000316 case 8:
Sean Callanan4cd930f2010-05-05 22:47:27 +0000317 break;
318 }
319 }
Kevin Enderby5b03f722011-09-02 20:01:23 +0000320 // By default sign-extend all X86 immediates based on their encoding.
321 else if (type == TYPE_IMM8 || type == TYPE_IMM16 || type == TYPE_IMM32 ||
Elena Demikhovsky8ac0bf92014-04-23 07:21:04 +0000322 type == TYPE_IMM64 || type == TYPE_IMMv) {
Kevin Enderby5b03f722011-09-02 20:01:23 +0000323 uint32_t Opcode = mcInst.getOpcode();
324 switch (operand.encoding) {
325 default:
326 break;
327 case ENCODING_IB:
328 // Special case those X86 instructions that use the imm8 as a set of
329 // bits, bit count, etc. and are not sign-extend.
330 if (Opcode != X86::BLENDPSrri && Opcode != X86::BLENDPDrri &&
Bill Wendlingea6397f2012-07-19 00:11:40 +0000331 Opcode != X86::PBLENDWrri && Opcode != X86::MPSADBWrri &&
332 Opcode != X86::DPPSrri && Opcode != X86::DPPDrri &&
333 Opcode != X86::INSERTPSrr && Opcode != X86::VBLENDPSYrri &&
334 Opcode != X86::VBLENDPSYrmi && Opcode != X86::VBLENDPDYrri &&
335 Opcode != X86::VBLENDPDYrmi && Opcode != X86::VPBLENDWrri &&
336 Opcode != X86::VMPSADBWrri && Opcode != X86::VDPPSYrri &&
337 Opcode != X86::VDPPSYrmi && Opcode != X86::VDPPDrri &&
338 Opcode != X86::VINSERTPSrr)
Craig Topper18854172013-08-25 22:23:38 +0000339 if(immediate & 0x80)
340 immediate |= ~(0xffull);
Kevin Enderby5b03f722011-09-02 20:01:23 +0000341 break;
342 case ENCODING_IW:
Craig Topper18854172013-08-25 22:23:38 +0000343 if(immediate & 0x8000)
344 immediate |= ~(0xffffull);
Kevin Enderby5b03f722011-09-02 20:01:23 +0000345 break;
346 case ENCODING_ID:
Craig Topper18854172013-08-25 22:23:38 +0000347 if(immediate & 0x80000000)
348 immediate |= ~(0xffffffffull);
Kevin Enderby5b03f722011-09-02 20:01:23 +0000349 break;
350 case ENCODING_IO:
Kevin Enderby5b03f722011-09-02 20:01:23 +0000351 break;
352 }
353 }
Sean Callanan4cd930f2010-05-05 22:47:27 +0000354
355 switch (type) {
Craig Topperc30fdbc2012-08-31 15:40:30 +0000356 case TYPE_XMM32:
357 case TYPE_XMM64:
Craig Topper96e00e52011-09-14 05:55:28 +0000358 case TYPE_XMM128:
359 mcInst.addOperand(MCOperand::CreateReg(X86::XMM0 + (immediate >> 4)));
360 return;
361 case TYPE_XMM256:
362 mcInst.addOperand(MCOperand::CreateReg(X86::YMM0 + (immediate >> 4)));
363 return;
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000364 case TYPE_XMM512:
365 mcInst.addOperand(MCOperand::CreateReg(X86::ZMM0 + (immediate >> 4)));
366 return;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000367 case TYPE_REL8:
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000368 isBranch = true;
369 pcrel = insn.startLocation + insn.immediateOffset + insn.immediateSize;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000370 if(immediate & 0x80)
371 immediate |= ~(0xffull);
372 break;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000373 case TYPE_REL32:
374 case TYPE_REL64:
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000375 isBranch = true;
376 pcrel = insn.startLocation + insn.immediateOffset + insn.immediateSize;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000377 if(immediate & 0x80000000)
378 immediate |= ~(0xffffffffull);
379 break;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000380 default:
381 // operand is 64 bits wide. Do nothing.
382 break;
383 }
Craig Topper092e2fe2013-08-24 19:50:11 +0000384
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000385 if(!tryAddingSymbolicOperand(immediate + pcrel, isBranch, insn.startLocation,
386 insn.immediateOffset, insn.immediateSize,
387 mcInst, Dis))
388 mcInst.addOperand(MCOperand::CreateImm(immediate));
Craig Topper35da3d12014-01-16 07:36:58 +0000389
390 if (type == TYPE_MOFFS8 || type == TYPE_MOFFS16 ||
391 type == TYPE_MOFFS32 || type == TYPE_MOFFS64) {
392 MCOperand segmentReg;
393 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
394 mcInst.addOperand(segmentReg);
395 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000396}
397
398/// translateRMRegister - Translates a register stored in the R/M field of the
399/// ModR/M byte to its LLVM equivalent and appends it to an MCInst.
400/// @param mcInst - The MCInst to append to.
401/// @param insn - The internal instruction to extract the R/M field
402/// from.
Sean Callanan010b3732010-04-02 21:23:51 +0000403/// @return - 0 on success; -1 otherwise
404static bool translateRMRegister(MCInst &mcInst,
Sean Callanan04cc3072009-12-19 02:59:52 +0000405 InternalInstruction &insn) {
Sean Callanan010b3732010-04-02 21:23:51 +0000406 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
407 debug("A R/M register operand may not have a SIB byte");
408 return true;
409 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000410
411 switch (insn.eaBase) {
Sean Callanan010b3732010-04-02 21:23:51 +0000412 default:
413 debug("Unexpected EA base register");
414 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000415 case EA_BASE_NONE:
Sean Callanan010b3732010-04-02 21:23:51 +0000416 debug("EA_BASE_NONE for ModR/M base");
417 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000418#define ENTRY(x) case EA_BASE_##x:
419 ALL_EA_BASES
420#undef ENTRY
Sean Callanan010b3732010-04-02 21:23:51 +0000421 debug("A R/M register operand may not have a base; "
422 "the operand must be a register.");
423 return true;
424#define ENTRY(x) \
Sean Callanan04cc3072009-12-19 02:59:52 +0000425 case EA_REG_##x: \
426 mcInst.addOperand(MCOperand::CreateReg(X86::x)); break;
427 ALL_REGS
428#undef ENTRY
Sean Callanan04cc3072009-12-19 02:59:52 +0000429 }
Sean Callanan010b3732010-04-02 21:23:51 +0000430
431 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000432}
433
434/// translateRMMemory - Translates a memory operand stored in the Mod and R/M
435/// fields of an internal instruction (and possibly its SIB byte) to a memory
436/// operand in LLVM's format, and appends it to an MCInst.
437///
438/// @param mcInst - The MCInst to append to.
439/// @param insn - The instruction to extract Mod, R/M, and SIB fields
440/// from.
Sean Callanan010b3732010-04-02 21:23:51 +0000441/// @return - 0 on success; nonzero otherwise
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000442static bool translateRMMemory(MCInst &mcInst, InternalInstruction &insn,
443 const MCDisassembler *Dis) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000444 // Addresses in an MCInst are represented as five operands:
445 // 1. basereg (register) The R/M base, or (if there is a SIB) the
446 // SIB base
447 // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified
448 // scale amount
449 // 3. indexreg (register) x86_registerNONE, or (if there is a SIB)
450 // the index (which is multiplied by the
451 // scale amount)
452 // 4. displacement (immediate) 0, or the displacement if there is one
453 // 5. segmentreg (register) x86_registerNONE for now, but could be set
454 // if we have segment overrides
455
456 MCOperand baseReg;
457 MCOperand scaleAmount;
458 MCOperand indexReg;
459 MCOperand displacement;
460 MCOperand segmentReg;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000461 uint64_t pcrel = 0;
Sean Callanan04cc3072009-12-19 02:59:52 +0000462
463 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
464 if (insn.sibBase != SIB_BASE_NONE) {
465 switch (insn.sibBase) {
466 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000467 debug("Unexpected sibBase");
468 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000469#define ENTRY(x) \
Sean Callanan36eab802009-12-22 21:12:55 +0000470 case SIB_BASE_##x: \
Sean Callanan04cc3072009-12-19 02:59:52 +0000471 baseReg = MCOperand::CreateReg(X86::x); break;
472 ALL_SIB_BASES
473#undef ENTRY
474 }
475 } else {
476 baseReg = MCOperand::CreateReg(0);
477 }
Manman Rena0982042012-06-26 19:47:59 +0000478
479 // Check whether we are handling VSIB addressing mode for GATHER.
480 // If sibIndex was set to SIB_INDEX_NONE, index offset is 4 and
481 // we should use SIB_INDEX_XMM4|YMM4 for VSIB.
482 // I don't see a way to get the correct IndexReg in readSIB:
483 // We can tell whether it is VSIB or SIB after instruction ID is decoded,
484 // but instruction ID may not be decoded yet when calling readSIB.
485 uint32_t Opcode = mcInst.getOpcode();
Manman Ren98a5bf22012-06-29 00:54:20 +0000486 bool IndexIs128 = (Opcode == X86::VGATHERDPDrm ||
487 Opcode == X86::VGATHERDPDYrm ||
488 Opcode == X86::VGATHERQPDrm ||
489 Opcode == X86::VGATHERDPSrm ||
490 Opcode == X86::VGATHERQPSrm ||
491 Opcode == X86::VPGATHERDQrm ||
492 Opcode == X86::VPGATHERDQYrm ||
493 Opcode == X86::VPGATHERQQrm ||
494 Opcode == X86::VPGATHERDDrm ||
495 Opcode == X86::VPGATHERQDrm);
496 bool IndexIs256 = (Opcode == X86::VGATHERQPDYrm ||
497 Opcode == X86::VGATHERDPSYrm ||
498 Opcode == X86::VGATHERQPSYrm ||
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000499 Opcode == X86::VGATHERDPDZrm ||
500 Opcode == X86::VPGATHERDQZrm ||
Manman Ren98a5bf22012-06-29 00:54:20 +0000501 Opcode == X86::VPGATHERQQYrm ||
502 Opcode == X86::VPGATHERDDYrm ||
503 Opcode == X86::VPGATHERQDYrm);
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000504 bool IndexIs512 = (Opcode == X86::VGATHERQPDZrm ||
505 Opcode == X86::VGATHERDPSZrm ||
506 Opcode == X86::VGATHERQPSZrm ||
507 Opcode == X86::VPGATHERQQZrm ||
508 Opcode == X86::VPGATHERDDZrm ||
509 Opcode == X86::VPGATHERQDZrm);
510 if (IndexIs128 || IndexIs256 || IndexIs512) {
Manman Rena0982042012-06-26 19:47:59 +0000511 unsigned IndexOffset = insn.sibIndex -
512 (insn.addressSize == 8 ? SIB_INDEX_RAX:SIB_INDEX_EAX);
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000513 SIBIndex IndexBase = IndexIs512 ? SIB_INDEX_ZMM0 :
514 IndexIs256 ? SIB_INDEX_YMM0 : SIB_INDEX_XMM0;
Manman Rena0982042012-06-26 19:47:59 +0000515 insn.sibIndex = (SIBIndex)(IndexBase +
516 (insn.sibIndex == SIB_INDEX_NONE ? 4 : IndexOffset));
517 }
518
Sean Callanan04cc3072009-12-19 02:59:52 +0000519 if (insn.sibIndex != SIB_INDEX_NONE) {
520 switch (insn.sibIndex) {
521 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000522 debug("Unexpected sibIndex");
523 return true;
Sean Callanan36eab802009-12-22 21:12:55 +0000524#define ENTRY(x) \
Sean Callanan04cc3072009-12-19 02:59:52 +0000525 case SIB_INDEX_##x: \
526 indexReg = MCOperand::CreateReg(X86::x); break;
527 EA_BASES_32BIT
528 EA_BASES_64BIT
Manman Rena0982042012-06-26 19:47:59 +0000529 REGS_XMM
530 REGS_YMM
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000531 REGS_ZMM
Sean Callanan04cc3072009-12-19 02:59:52 +0000532#undef ENTRY
533 }
534 } else {
535 indexReg = MCOperand::CreateReg(0);
536 }
537
538 scaleAmount = MCOperand::CreateImm(insn.sibScale);
539 } else {
540 switch (insn.eaBase) {
541 case EA_BASE_NONE:
Sean Callanan010b3732010-04-02 21:23:51 +0000542 if (insn.eaDisplacement == EA_DISP_NONE) {
543 debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
544 return true;
545 }
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000546 if (insn.mode == MODE_64BIT){
547 pcrel = insn.startLocation +
548 insn.displacementOffset + insn.displacementSize;
Kevin Enderbyb119c082012-02-29 22:58:34 +0000549 tryAddingPcLoadReferenceComment(insn.startLocation +
550 insn.displacementOffset,
551 insn.displacement + pcrel, Dis);
Sean Callanan04cc3072009-12-19 02:59:52 +0000552 baseReg = MCOperand::CreateReg(X86::RIP); // Section 2.2.1.6
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000553 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000554 else
555 baseReg = MCOperand::CreateReg(0);
556
557 indexReg = MCOperand::CreateReg(0);
558 break;
559 case EA_BASE_BX_SI:
560 baseReg = MCOperand::CreateReg(X86::BX);
561 indexReg = MCOperand::CreateReg(X86::SI);
562 break;
563 case EA_BASE_BX_DI:
564 baseReg = MCOperand::CreateReg(X86::BX);
565 indexReg = MCOperand::CreateReg(X86::DI);
566 break;
567 case EA_BASE_BP_SI:
568 baseReg = MCOperand::CreateReg(X86::BP);
569 indexReg = MCOperand::CreateReg(X86::SI);
570 break;
571 case EA_BASE_BP_DI:
572 baseReg = MCOperand::CreateReg(X86::BP);
573 indexReg = MCOperand::CreateReg(X86::DI);
574 break;
575 default:
576 indexReg = MCOperand::CreateReg(0);
577 switch (insn.eaBase) {
578 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000579 debug("Unexpected eaBase");
580 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000581 // Here, we will use the fill-ins defined above. However,
582 // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and
583 // sib and sib64 were handled in the top-level if, so they're only
584 // placeholders to keep the compiler happy.
585#define ENTRY(x) \
586 case EA_BASE_##x: \
587 baseReg = MCOperand::CreateReg(X86::x); break;
588 ALL_EA_BASES
589#undef ENTRY
590#define ENTRY(x) case EA_REG_##x:
591 ALL_REGS
592#undef ENTRY
Sean Callanan010b3732010-04-02 21:23:51 +0000593 debug("A R/M memory operand may not be a register; "
594 "the base field must be a base.");
595 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000596 }
597 }
Sean Callanan36eab802009-12-22 21:12:55 +0000598
599 scaleAmount = MCOperand::CreateImm(1);
Sean Callanan04cc3072009-12-19 02:59:52 +0000600 }
601
602 displacement = MCOperand::CreateImm(insn.displacement);
Craig Topper35da3d12014-01-16 07:36:58 +0000603
Sean Callanan04cc3072009-12-19 02:59:52 +0000604 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
605
606 mcInst.addOperand(baseReg);
607 mcInst.addOperand(scaleAmount);
608 mcInst.addOperand(indexReg);
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000609 if(!tryAddingSymbolicOperand(insn.displacement + pcrel, false,
610 insn.startLocation, insn.displacementOffset,
611 insn.displacementSize, mcInst, Dis))
612 mcInst.addOperand(displacement);
Chris Lattner55595fb2010-07-13 04:23:55 +0000613 mcInst.addOperand(segmentReg);
Sean Callanan010b3732010-04-02 21:23:51 +0000614 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000615}
616
617/// translateRM - Translates an operand stored in the R/M (and possibly SIB)
618/// byte of an instruction to LLVM form, and appends it to an MCInst.
619///
620/// @param mcInst - The MCInst to append to.
621/// @param operand - The operand, as stored in the descriptor table.
622/// @param insn - The instruction to extract Mod, R/M, and SIB fields
623/// from.
Sean Callanan010b3732010-04-02 21:23:51 +0000624/// @return - 0 on success; nonzero otherwise
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +0000625static bool translateRM(MCInst &mcInst, const OperandSpecifier &operand,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000626 InternalInstruction &insn, const MCDisassembler *Dis) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000627 switch (operand.type) {
628 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000629 debug("Unexpected type for a R/M operand");
630 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000631 case TYPE_R8:
632 case TYPE_R16:
633 case TYPE_R32:
634 case TYPE_R64:
635 case TYPE_Rv:
636 case TYPE_MM:
637 case TYPE_MM32:
638 case TYPE_MM64:
639 case TYPE_XMM:
640 case TYPE_XMM32:
641 case TYPE_XMM64:
642 case TYPE_XMM128:
Sean Callananc3fd5232011-03-15 01:23:15 +0000643 case TYPE_XMM256:
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000644 case TYPE_XMM512:
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000645 case TYPE_VK1:
646 case TYPE_VK8:
647 case TYPE_VK16:
Sean Callanan04cc3072009-12-19 02:59:52 +0000648 case TYPE_DEBUGREG:
Sean Callanane7e1cf92010-05-06 20:59:00 +0000649 case TYPE_CONTROLREG:
Sean Callanan010b3732010-04-02 21:23:51 +0000650 return translateRMRegister(mcInst, insn);
Sean Callanan04cc3072009-12-19 02:59:52 +0000651 case TYPE_M:
652 case TYPE_M8:
653 case TYPE_M16:
654 case TYPE_M32:
655 case TYPE_M64:
656 case TYPE_M128:
Sean Callananc3fd5232011-03-15 01:23:15 +0000657 case TYPE_M256:
Sean Callanan04cc3072009-12-19 02:59:52 +0000658 case TYPE_M512:
659 case TYPE_Mv:
660 case TYPE_M32FP:
661 case TYPE_M64FP:
662 case TYPE_M80FP:
663 case TYPE_M16INT:
664 case TYPE_M32INT:
665 case TYPE_M64INT:
666 case TYPE_M1616:
667 case TYPE_M1632:
668 case TYPE_M1664:
Sean Callanan36eab802009-12-22 21:12:55 +0000669 case TYPE_LEA:
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000670 return translateRMMemory(mcInst, insn, Dis);
Sean Callanan04cc3072009-12-19 02:59:52 +0000671 }
672}
673
674/// translateFPRegister - Translates a stack position on the FPU stack to its
675/// LLVM form, and appends it to an MCInst.
676///
677/// @param mcInst - The MCInst to append to.
678/// @param stackPos - The stack position to translate.
Craig Topper91551182014-01-01 15:29:32 +0000679static void translateFPRegister(MCInst &mcInst,
680 uint8_t stackPos) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000681 mcInst.addOperand(MCOperand::CreateReg(X86::ST0 + stackPos));
682}
683
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000684/// translateMaskRegister - Translates a 3-bit mask register number to
685/// LLVM form, and appends it to an MCInst.
686///
687/// @param mcInst - The MCInst to append to.
688/// @param maskRegNum - Number of mask register from 0 to 7.
689/// @return - false on success; true otherwise.
690static bool translateMaskRegister(MCInst &mcInst,
691 uint8_t maskRegNum) {
692 if (maskRegNum >= 8) {
693 debug("Invalid mask register number");
694 return true;
695 }
696
697 mcInst.addOperand(MCOperand::CreateReg(X86::K0 + maskRegNum));
698 return false;
699}
700
Sean Callanan04cc3072009-12-19 02:59:52 +0000701/// translateOperand - Translates an operand stored in an internal instruction
702/// to LLVM's format and appends it to an MCInst.
703///
704/// @param mcInst - The MCInst to append to.
705/// @param operand - The operand, as stored in the descriptor table.
706/// @param insn - The internal instruction.
Sean Callanan010b3732010-04-02 21:23:51 +0000707/// @return - false on success; true otherwise.
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +0000708static bool translateOperand(MCInst &mcInst, const OperandSpecifier &operand,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000709 InternalInstruction &insn,
710 const MCDisassembler *Dis) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000711 switch (operand.encoding) {
712 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000713 debug("Unhandled operand encoding during translation");
714 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000715 case ENCODING_REG:
716 translateRegister(mcInst, insn.reg);
Sean Callanan010b3732010-04-02 21:23:51 +0000717 return false;
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000718 case ENCODING_WRITEMASK:
719 return translateMaskRegister(mcInst, insn.writemask);
Adam Nemet5933c2f2014-07-17 17:04:56 +0000720 CASE_ENCODING_RM:
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000721 return translateRM(mcInst, operand, insn, Dis);
Sean Callanan04cc3072009-12-19 02:59:52 +0000722 case ENCODING_CB:
723 case ENCODING_CW:
724 case ENCODING_CD:
725 case ENCODING_CP:
726 case ENCODING_CO:
727 case ENCODING_CT:
Sean Callanan010b3732010-04-02 21:23:51 +0000728 debug("Translation of code offsets isn't supported.");
729 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000730 case ENCODING_IB:
731 case ENCODING_IW:
732 case ENCODING_ID:
733 case ENCODING_IO:
734 case ENCODING_Iv:
735 case ENCODING_Ia:
Sean Callanan4cd930f2010-05-05 22:47:27 +0000736 translateImmediate(mcInst,
737 insn.immediates[insn.numImmediatesTranslated++],
738 operand,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000739 insn,
740 Dis);
Sean Callanan010b3732010-04-02 21:23:51 +0000741 return false;
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000742 case ENCODING_SI:
743 return translateSrcIndex(mcInst, insn);
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000744 case ENCODING_DI:
745 return translateDstIndex(mcInst, insn);
Sean Callanan04cc3072009-12-19 02:59:52 +0000746 case ENCODING_RB:
747 case ENCODING_RW:
748 case ENCODING_RD:
749 case ENCODING_RO:
Craig Topper91551182014-01-01 15:29:32 +0000750 case ENCODING_Rv:
Sean Callanan04cc3072009-12-19 02:59:52 +0000751 translateRegister(mcInst, insn.opcodeRegister);
Sean Callanan010b3732010-04-02 21:23:51 +0000752 return false;
Craig Topper623b0d62014-01-01 14:22:37 +0000753 case ENCODING_FP:
Craig Topper91551182014-01-01 15:29:32 +0000754 translateFPRegister(mcInst, insn.modRM & 7);
Sean Callanan010b3732010-04-02 21:23:51 +0000755 return false;
Sean Callananc3fd5232011-03-15 01:23:15 +0000756 case ENCODING_VVVV:
757 translateRegister(mcInst, insn.vvvv);
758 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000759 case ENCODING_DUP:
Craig Topperb8aec082012-08-01 07:39:18 +0000760 return translateOperand(mcInst, insn.operands[operand.type - TYPE_DUP0],
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000761 insn, Dis);
Sean Callanan04cc3072009-12-19 02:59:52 +0000762 }
763}
764
765/// translateInstruction - Translates an internal instruction and all its
766/// operands to an MCInst.
767///
768/// @param mcInst - The MCInst to populate with the instruction's data.
769/// @param insn - The internal instruction.
Sean Callanan010b3732010-04-02 21:23:51 +0000770/// @return - false on success; true otherwise.
771static bool translateInstruction(MCInst &mcInst,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000772 InternalInstruction &insn,
773 const MCDisassembler *Dis) {
Sean Callanan010b3732010-04-02 21:23:51 +0000774 if (!insn.spec) {
775 debug("Instruction has no specification");
776 return true;
777 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000778
779 mcInst.setOpcode(insn.instructionID);
Kevin Enderby35fd7922013-06-20 22:32:18 +0000780 // If when reading the prefix bytes we determined the overlapping 0xf2 or 0xf3
781 // prefix bytes should be disassembled as xrelease and xacquire then set the
782 // opcode to those instead of the rep and repne opcodes.
783 if (insn.xAcquireRelease) {
784 if(mcInst.getOpcode() == X86::REP_PREFIX)
785 mcInst.setOpcode(X86::XRELEASE_PREFIX);
786 else if(mcInst.getOpcode() == X86::REPNE_PREFIX)
787 mcInst.setOpcode(X86::XACQUIRE_PREFIX);
788 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000789
Sean Callanan04cc3072009-12-19 02:59:52 +0000790 insn.numImmediatesTranslated = 0;
791
Patrik Hagglund31998382014-04-28 12:12:27 +0000792 for (const auto &Op : insn.operands) {
793 if (Op.encoding != ENCODING_NONE) {
794 if (translateOperand(mcInst, Op, 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}