blob: 99fb1aba8413b3832519377660384bb39b4da81c [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"
Evan Cheng2bb40352011-08-24 18:08:43 +000026#include "llvm/Support/TargetRegistry.h"
Sean Callanan04cc3072009-12-19 02:59:52 +000027#include "llvm/Support/raw_ostream.h"
Sean Callanan5c8f4cd2009-12-22 01:11:26 +000028
Chandler Carruthd174b722014-04-22 02:03:14 +000029using namespace llvm;
30using namespace llvm::X86Disassembler;
31
32#define DEBUG_TYPE "x86-disassembler"
33
Evan Chengd9997ac2011-06-27 18:32:37 +000034#define GET_REGINFO_ENUM
35#include "X86GenRegisterInfo.inc"
Kevin Enderby5b03f722011-09-02 20:01:23 +000036#define GET_INSTRINFO_ENUM
37#include "X86GenInstrInfo.inc"
David Woodhouse7dd21822014-01-20 12:02:31 +000038#define GET_SUBTARGETINFO_ENUM
39#include "X86GenSubtargetInfo.inc"
Sean Callanan5c8f4cd2009-12-22 01:11:26 +000040
Richard Smith89ee75d2014-04-20 21:07:34 +000041void llvm::X86Disassembler::Debug(const char *file, unsigned line,
42 const char *s) {
Sean Callanan010b3732010-04-02 21:23:51 +000043 dbgs() << file << ":" << line << ": " << s;
44}
45
Richard Smith89ee75d2014-04-20 21:07:34 +000046const char *llvm::X86Disassembler::GetInstrName(unsigned Opcode,
47 const void *mii) {
Benjamin Kramer478e8de2012-02-11 14:50:54 +000048 const MCInstrInfo *MII = static_cast<const MCInstrInfo *>(mii);
49 return MII->getName(Opcode);
50}
51
Richard Smith89ee75d2014-04-20 21:07:34 +000052#define debug(s) DEBUG(Debug(__FILE__, __LINE__, s));
Sean Callanan010b3732010-04-02 21:23:51 +000053
Michael Liao5bf95782014-12-04 05:20:33 +000054namespace llvm {
55
Sean Callanan04cc3072009-12-19 02:59:52 +000056// Fill-ins to make the compiler happy. These constants are never actually
57// assigned; they are just filler to make an automatically-generated switch
58// statement work.
59namespace X86 {
60 enum {
61 BX_SI = 500,
62 BX_DI = 501,
63 BP_SI = 502,
64 BP_DI = 503,
65 sib = 504,
66 sib64 = 505
67 };
68}
69
Sean Callanan5c8f4cd2009-12-22 01:11:26 +000070extern Target TheX86_32Target, TheX86_64Target;
71
Sean Callanan04cc3072009-12-19 02:59:52 +000072}
73
Sean Callanan010b3732010-04-02 21:23:51 +000074static bool translateInstruction(MCInst &target,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +000075 InternalInstruction &source,
76 const MCDisassembler *Dis);
Sean Callanan04cc3072009-12-19 02:59:52 +000077
Lang Hames0563ca12014-04-13 04:09:16 +000078X86GenericDisassembler::X86GenericDisassembler(
79 const MCSubtargetInfo &STI,
Lang Hamesa1bc0f52014-04-15 04:40:56 +000080 MCContext &Ctx,
Lang Hames0563ca12014-04-13 04:09:16 +000081 std::unique_ptr<const MCInstrInfo> MII)
Lang Hamesa1bc0f52014-04-15 04:40:56 +000082 : MCDisassembler(STI, Ctx), MII(std::move(MII)) {
Michael Kupersteinefd7a962015-02-19 11:38:11 +000083 switch (STI.getFeatureBits() &
84 (X86::Mode16Bit | X86::Mode32Bit | X86::Mode64Bit)) {
85 case X86::Mode16Bit:
David Woodhouse7dd21822014-01-20 12:02:31 +000086 fMode = MODE_16BIT;
Michael Kupersteinefd7a962015-02-19 11:38:11 +000087 break;
88 case X86::Mode32Bit:
David Woodhouse7dd21822014-01-20 12:02:31 +000089 fMode = MODE_32BIT;
Michael Kupersteinefd7a962015-02-19 11:38:11 +000090 break;
91 case X86::Mode64Bit:
David Woodhouse7dd21822014-01-20 12:02:31 +000092 fMode = MODE_64BIT;
Michael Kupersteinefd7a962015-02-19 11:38:11 +000093 break;
94 default:
95 llvm_unreachable("Invalid CPU mode");
David Woodhouse7dd21822014-01-20 12:02:31 +000096 }
97}
Sean Callanan04cc3072009-12-19 02:59:52 +000098
Rafael Espindola7fc5b872014-11-12 02:04:27 +000099struct Region {
100 ArrayRef<uint8_t> Bytes;
101 uint64_t Base;
102 Region(ArrayRef<uint8_t> Bytes, uint64_t Base) : Bytes(Bytes), Base(Base) {}
103};
104
105/// A callback function that wraps the readByte method from Region.
Sean Callanan04cc3072009-12-19 02:59:52 +0000106///
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000107/// @param Arg - The generic callback parameter. In this case, this should
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000108/// be a pointer to a Region.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000109/// @param Byte - A pointer to the byte to be read.
110/// @param Address - The address to be read.
111static int regionReader(const void *Arg, uint8_t *Byte, uint64_t Address) {
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000112 auto *R = static_cast<const Region *>(Arg);
113 ArrayRef<uint8_t> Bytes = R->Bytes;
114 unsigned Index = Address - R->Base;
115 if (Bytes.size() <= Index)
116 return -1;
117 *Byte = Bytes[Index];
118 return 0;
Sean Callanan04cc3072009-12-19 02:59:52 +0000119}
120
121/// logger - a callback function that wraps the operator<< method from
122/// raw_ostream.
123///
124/// @param arg - The generic callback parameter. This should be a pointe
125/// to a raw_ostream.
126/// @param log - A string to be logged. logger() adds a newline.
127static void logger(void* arg, const char* log) {
128 if (!arg)
129 return;
Michael Liao5bf95782014-12-04 05:20:33 +0000130
Sean Callanan04cc3072009-12-19 02:59:52 +0000131 raw_ostream &vStream = *(static_cast<raw_ostream*>(arg));
132 vStream << log << "\n";
Michael Liao5bf95782014-12-04 05:20:33 +0000133}
134
Sean Callanan04cc3072009-12-19 02:59:52 +0000135//
136// Public interface for the disassembler
137//
138
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000139MCDisassembler::DecodeStatus X86GenericDisassembler::getInstruction(
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000140 MCInst &Instr, uint64_t &Size, ArrayRef<uint8_t> Bytes, uint64_t Address,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000141 raw_ostream &VStream, raw_ostream &CStream) const {
142 CommentStream = &CStream;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000143
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000144 InternalInstruction InternalInstr;
Benjamin Kramere5e189f2011-09-21 21:47:35 +0000145
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000146 dlog_t LoggerFn = logger;
147 if (&VStream == &nulls())
148 LoggerFn = nullptr; // Disable logging completely if it's going to nulls().
Sean Callanan04cc3072009-12-19 02:59:52 +0000149
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000150 Region R(Bytes, Address);
151
152 int Ret = decodeInstruction(&InternalInstr, regionReader, (const void *)&R,
153 LoggerFn, (void *)&VStream,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000154 (const void *)MII.get(), Address, fMode);
155
156 if (Ret) {
157 Size = InternalInstr.readerCursor - Address;
Owen Andersona4043c42011-08-17 17:44:15 +0000158 return Fail;
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000159 } else {
160 Size = InternalInstr.length;
161 return (!translateInstruction(Instr, InternalInstr, this)) ? 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
Michael Liao5bf95782014-12-04 05:20:33 +0000187/// immediate Value in the MCInst.
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000188///
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
Michael Liao5bf95782014-12-04 05:20:33 +0000199/// returns non-zero then the symbolic information it returns is used to create
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000200/// 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,
Michael Liao5bf95782014-12-04 05:20:33 +0000207 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
Michael Liao5bf95782014-12-04 05:20:33 +0000218/// return a pointer to a literal 'C' string if the referenced address is an
Kevin Enderbyb119c082012-02-29 22:58:34 +0000219/// 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,
Michael Liao5bf95782014-12-04 05:20:33 +0000290 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 switch (operand.encoding) {
324 default:
325 break;
326 case ENCODING_IB:
Craig Topper620b50c2015-01-21 08:15:54 +0000327 if(immediate & 0x80)
328 immediate |= ~(0xffull);
Kevin Enderby5b03f722011-09-02 20:01:23 +0000329 break;
330 case ENCODING_IW:
Craig Topper18854172013-08-25 22:23:38 +0000331 if(immediate & 0x8000)
332 immediate |= ~(0xffffull);
Kevin Enderby5b03f722011-09-02 20:01:23 +0000333 break;
334 case ENCODING_ID:
Craig Topper18854172013-08-25 22:23:38 +0000335 if(immediate & 0x80000000)
336 immediate |= ~(0xffffffffull);
Kevin Enderby5b03f722011-09-02 20:01:23 +0000337 break;
338 case ENCODING_IO:
Kevin Enderby5b03f722011-09-02 20:01:23 +0000339 break;
340 }
Craig Topperee9eef22014-12-26 06:36:28 +0000341 } else if (type == TYPE_IMM3) {
342 // Check for immediates that printSSECC can't handle.
343 if (immediate >= 8) {
344 unsigned NewOpc;
345 switch (mcInst.getOpcode()) {
346 default: llvm_unreachable("unexpected opcode");
Craig Topper916708f2015-02-13 07:42:25 +0000347 case X86::CMPPDrmi: NewOpc = X86::CMPPDrmi_alt; break;
348 case X86::CMPPDrri: NewOpc = X86::CMPPDrri_alt; break;
349 case X86::CMPPSrmi: NewOpc = X86::CMPPSrmi_alt; break;
350 case X86::CMPPSrri: NewOpc = X86::CMPPSrri_alt; break;
351 case X86::CMPSDrm: NewOpc = X86::CMPSDrm_alt; break;
352 case X86::CMPSDrr: NewOpc = X86::CMPSDrr_alt; break;
353 case X86::CMPSSrm: NewOpc = X86::CMPSSrm_alt; break;
354 case X86::CMPSSrr: NewOpc = X86::CMPSSrr_alt; break;
355 case X86::VPCOMBri: NewOpc = X86::VPCOMBri_alt; break;
356 case X86::VPCOMBmi: NewOpc = X86::VPCOMBmi_alt; break;
357 case X86::VPCOMWri: NewOpc = X86::VPCOMWri_alt; break;
358 case X86::VPCOMWmi: NewOpc = X86::VPCOMWmi_alt; break;
359 case X86::VPCOMDri: NewOpc = X86::VPCOMDri_alt; break;
360 case X86::VPCOMDmi: NewOpc = X86::VPCOMDmi_alt; break;
361 case X86::VPCOMQri: NewOpc = X86::VPCOMQri_alt; break;
362 case X86::VPCOMQmi: NewOpc = X86::VPCOMQmi_alt; break;
363 case X86::VPCOMUBri: NewOpc = X86::VPCOMUBri_alt; break;
364 case X86::VPCOMUBmi: NewOpc = X86::VPCOMUBmi_alt; break;
365 case X86::VPCOMUWri: NewOpc = X86::VPCOMUWri_alt; break;
366 case X86::VPCOMUWmi: NewOpc = X86::VPCOMUWmi_alt; break;
367 case X86::VPCOMUDri: NewOpc = X86::VPCOMUDri_alt; break;
368 case X86::VPCOMUDmi: NewOpc = X86::VPCOMUDmi_alt; break;
369 case X86::VPCOMUQri: NewOpc = X86::VPCOMUQri_alt; break;
370 case X86::VPCOMUQmi: NewOpc = X86::VPCOMUQmi_alt; break;
Craig Topperee9eef22014-12-26 06:36:28 +0000371 }
372 // Switch opcode to the one that doesn't get special printing.
373 mcInst.setOpcode(NewOpc);
374 }
375 } else if (type == TYPE_IMM5) {
376 // Check for immediates that printAVXCC can't handle.
377 if (immediate >= 32) {
378 unsigned NewOpc;
379 switch (mcInst.getOpcode()) {
380 default: llvm_unreachable("unexpected opcode");
381 case X86::VCMPPDrmi: NewOpc = X86::VCMPPDrmi_alt; break;
382 case X86::VCMPPDrri: NewOpc = X86::VCMPPDrri_alt; break;
383 case X86::VCMPPSrmi: NewOpc = X86::VCMPPSrmi_alt; break;
384 case X86::VCMPPSrri: NewOpc = X86::VCMPPSrri_alt; break;
385 case X86::VCMPSDrm: NewOpc = X86::VCMPSDrm_alt; break;
386 case X86::VCMPSDrr: NewOpc = X86::VCMPSDrr_alt; break;
387 case X86::VCMPSSrm: NewOpc = X86::VCMPSSrm_alt; break;
388 case X86::VCMPSSrr: NewOpc = X86::VCMPSSrr_alt; break;
389 case X86::VCMPPDYrmi: NewOpc = X86::VCMPPDYrmi_alt; break;
390 case X86::VCMPPDYrri: NewOpc = X86::VCMPPDYrri_alt; break;
391 case X86::VCMPPSYrmi: NewOpc = X86::VCMPPSYrmi_alt; break;
392 case X86::VCMPPSYrri: NewOpc = X86::VCMPPSYrri_alt; break;
393 case X86::VCMPPDZrmi: NewOpc = X86::VCMPPDZrmi_alt; break;
394 case X86::VCMPPDZrri: NewOpc = X86::VCMPPDZrri_alt; break;
395 case X86::VCMPPSZrmi: NewOpc = X86::VCMPPSZrmi_alt; break;
396 case X86::VCMPPSZrri: NewOpc = X86::VCMPPSZrri_alt; break;
397 case X86::VCMPSDZrm: NewOpc = X86::VCMPSDZrmi_alt; break;
398 case X86::VCMPSDZrr: NewOpc = X86::VCMPSDZrri_alt; break;
399 case X86::VCMPSSZrm: NewOpc = X86::VCMPSSZrmi_alt; break;
400 case X86::VCMPSSZrr: NewOpc = X86::VCMPSSZrri_alt; break;
401 }
402 // Switch opcode to the one that doesn't get special printing.
403 mcInst.setOpcode(NewOpc);
404 }
Craig Topper7d3c6d32015-01-28 10:09:56 +0000405 } else if (type == TYPE_AVX512ICC) {
406 if (immediate >= 8 || ((immediate & 0x3) == 3)) {
407 unsigned NewOpc;
408 switch (mcInst.getOpcode()) {
409 default: llvm_unreachable("unexpected opcode");
410 case X86::VPCMPBZ128rmi: NewOpc = X86::VPCMPBZ128rmi_alt; break;
411 case X86::VPCMPBZ128rmik: NewOpc = X86::VPCMPBZ128rmik_alt; break;
412 case X86::VPCMPBZ128rri: NewOpc = X86::VPCMPBZ128rri_alt; break;
413 case X86::VPCMPBZ128rrik: NewOpc = X86::VPCMPBZ128rrik_alt; break;
414 case X86::VPCMPBZ256rmi: NewOpc = X86::VPCMPBZ256rmi_alt; break;
415 case X86::VPCMPBZ256rmik: NewOpc = X86::VPCMPBZ256rmik_alt; break;
416 case X86::VPCMPBZ256rri: NewOpc = X86::VPCMPBZ256rri_alt; break;
417 case X86::VPCMPBZ256rrik: NewOpc = X86::VPCMPBZ256rrik_alt; break;
418 case X86::VPCMPBZrmi: NewOpc = X86::VPCMPBZrmi_alt; break;
419 case X86::VPCMPBZrmik: NewOpc = X86::VPCMPBZrmik_alt; break;
420 case X86::VPCMPBZrri: NewOpc = X86::VPCMPBZrri_alt; break;
421 case X86::VPCMPBZrrik: NewOpc = X86::VPCMPBZrrik_alt; break;
422 case X86::VPCMPDZ128rmi: NewOpc = X86::VPCMPDZ128rmi_alt; break;
423 case X86::VPCMPDZ128rmib: NewOpc = X86::VPCMPDZ128rmib_alt; break;
424 case X86::VPCMPDZ128rmibk: NewOpc = X86::VPCMPDZ128rmibk_alt; break;
425 case X86::VPCMPDZ128rmik: NewOpc = X86::VPCMPDZ128rmik_alt; break;
426 case X86::VPCMPDZ128rri: NewOpc = X86::VPCMPDZ128rri_alt; break;
427 case X86::VPCMPDZ128rrik: NewOpc = X86::VPCMPDZ128rrik_alt; break;
428 case X86::VPCMPDZ256rmi: NewOpc = X86::VPCMPDZ256rmi_alt; break;
429 case X86::VPCMPDZ256rmib: NewOpc = X86::VPCMPDZ256rmib_alt; break;
430 case X86::VPCMPDZ256rmibk: NewOpc = X86::VPCMPDZ256rmibk_alt; break;
431 case X86::VPCMPDZ256rmik: NewOpc = X86::VPCMPDZ256rmik_alt; break;
432 case X86::VPCMPDZ256rri: NewOpc = X86::VPCMPDZ256rri_alt; break;
433 case X86::VPCMPDZ256rrik: NewOpc = X86::VPCMPDZ256rrik_alt; break;
434 case X86::VPCMPDZrmi: NewOpc = X86::VPCMPDZrmi_alt; break;
435 case X86::VPCMPDZrmib: NewOpc = X86::VPCMPDZrmib_alt; break;
436 case X86::VPCMPDZrmibk: NewOpc = X86::VPCMPDZrmibk_alt; break;
437 case X86::VPCMPDZrmik: NewOpc = X86::VPCMPDZrmik_alt; break;
438 case X86::VPCMPDZrri: NewOpc = X86::VPCMPDZrri_alt; break;
439 case X86::VPCMPDZrrik: NewOpc = X86::VPCMPDZrrik_alt; break;
440 case X86::VPCMPQZ128rmi: NewOpc = X86::VPCMPQZ128rmi_alt; break;
441 case X86::VPCMPQZ128rmib: NewOpc = X86::VPCMPQZ128rmib_alt; break;
442 case X86::VPCMPQZ128rmibk: NewOpc = X86::VPCMPQZ128rmibk_alt; break;
443 case X86::VPCMPQZ128rmik: NewOpc = X86::VPCMPQZ128rmik_alt; break;
444 case X86::VPCMPQZ128rri: NewOpc = X86::VPCMPQZ128rri_alt; break;
445 case X86::VPCMPQZ128rrik: NewOpc = X86::VPCMPQZ128rrik_alt; break;
446 case X86::VPCMPQZ256rmi: NewOpc = X86::VPCMPQZ256rmi_alt; break;
447 case X86::VPCMPQZ256rmib: NewOpc = X86::VPCMPQZ256rmib_alt; break;
448 case X86::VPCMPQZ256rmibk: NewOpc = X86::VPCMPQZ256rmibk_alt; break;
449 case X86::VPCMPQZ256rmik: NewOpc = X86::VPCMPQZ256rmik_alt; break;
450 case X86::VPCMPQZ256rri: NewOpc = X86::VPCMPQZ256rri_alt; break;
451 case X86::VPCMPQZ256rrik: NewOpc = X86::VPCMPQZ256rrik_alt; break;
452 case X86::VPCMPQZrmi: NewOpc = X86::VPCMPQZrmi_alt; break;
453 case X86::VPCMPQZrmib: NewOpc = X86::VPCMPQZrmib_alt; break;
454 case X86::VPCMPQZrmibk: NewOpc = X86::VPCMPQZrmibk_alt; break;
455 case X86::VPCMPQZrmik: NewOpc = X86::VPCMPQZrmik_alt; break;
456 case X86::VPCMPQZrri: NewOpc = X86::VPCMPQZrri_alt; break;
457 case X86::VPCMPQZrrik: NewOpc = X86::VPCMPQZrrik_alt; break;
458 case X86::VPCMPUBZ128rmi: NewOpc = X86::VPCMPUBZ128rmi_alt; break;
459 case X86::VPCMPUBZ128rmik: NewOpc = X86::VPCMPUBZ128rmik_alt; break;
460 case X86::VPCMPUBZ128rri: NewOpc = X86::VPCMPUBZ128rri_alt; break;
461 case X86::VPCMPUBZ128rrik: NewOpc = X86::VPCMPUBZ128rrik_alt; break;
462 case X86::VPCMPUBZ256rmi: NewOpc = X86::VPCMPUBZ256rmi_alt; break;
463 case X86::VPCMPUBZ256rmik: NewOpc = X86::VPCMPUBZ256rmik_alt; break;
464 case X86::VPCMPUBZ256rri: NewOpc = X86::VPCMPUBZ256rri_alt; break;
465 case X86::VPCMPUBZ256rrik: NewOpc = X86::VPCMPUBZ256rrik_alt; break;
466 case X86::VPCMPUBZrmi: NewOpc = X86::VPCMPUBZrmi_alt; break;
467 case X86::VPCMPUBZrmik: NewOpc = X86::VPCMPUBZrmik_alt; break;
468 case X86::VPCMPUBZrri: NewOpc = X86::VPCMPUBZrri_alt; break;
469 case X86::VPCMPUBZrrik: NewOpc = X86::VPCMPUBZrrik_alt; break;
470 case X86::VPCMPUDZ128rmi: NewOpc = X86::VPCMPUDZ128rmi_alt; break;
471 case X86::VPCMPUDZ128rmib: NewOpc = X86::VPCMPUDZ128rmib_alt; break;
472 case X86::VPCMPUDZ128rmibk: NewOpc = X86::VPCMPUDZ128rmibk_alt; break;
473 case X86::VPCMPUDZ128rmik: NewOpc = X86::VPCMPUDZ128rmik_alt; break;
474 case X86::VPCMPUDZ128rri: NewOpc = X86::VPCMPUDZ128rri_alt; break;
475 case X86::VPCMPUDZ128rrik: NewOpc = X86::VPCMPUDZ128rrik_alt; break;
476 case X86::VPCMPUDZ256rmi: NewOpc = X86::VPCMPUDZ256rmi_alt; break;
477 case X86::VPCMPUDZ256rmib: NewOpc = X86::VPCMPUDZ256rmib_alt; break;
478 case X86::VPCMPUDZ256rmibk: NewOpc = X86::VPCMPUDZ256rmibk_alt; break;
479 case X86::VPCMPUDZ256rmik: NewOpc = X86::VPCMPUDZ256rmik_alt; break;
480 case X86::VPCMPUDZ256rri: NewOpc = X86::VPCMPUDZ256rri_alt; break;
481 case X86::VPCMPUDZ256rrik: NewOpc = X86::VPCMPUDZ256rrik_alt; break;
482 case X86::VPCMPUDZrmi: NewOpc = X86::VPCMPUDZrmi_alt; break;
483 case X86::VPCMPUDZrmib: NewOpc = X86::VPCMPUDZrmib_alt; break;
484 case X86::VPCMPUDZrmibk: NewOpc = X86::VPCMPUDZrmibk_alt; break;
485 case X86::VPCMPUDZrmik: NewOpc = X86::VPCMPUDZrmik_alt; break;
486 case X86::VPCMPUDZrri: NewOpc = X86::VPCMPUDZrri_alt; break;
487 case X86::VPCMPUDZrrik: NewOpc = X86::VPCMPUDZrrik_alt; break;
488 case X86::VPCMPUQZ128rmi: NewOpc = X86::VPCMPUQZ128rmi_alt; break;
489 case X86::VPCMPUQZ128rmib: NewOpc = X86::VPCMPUQZ128rmib_alt; break;
490 case X86::VPCMPUQZ128rmibk: NewOpc = X86::VPCMPUQZ128rmibk_alt; break;
491 case X86::VPCMPUQZ128rmik: NewOpc = X86::VPCMPUQZ128rmik_alt; break;
492 case X86::VPCMPUQZ128rri: NewOpc = X86::VPCMPUQZ128rri_alt; break;
493 case X86::VPCMPUQZ128rrik: NewOpc = X86::VPCMPUQZ128rrik_alt; break;
494 case X86::VPCMPUQZ256rmi: NewOpc = X86::VPCMPUQZ256rmi_alt; break;
495 case X86::VPCMPUQZ256rmib: NewOpc = X86::VPCMPUQZ256rmib_alt; break;
496 case X86::VPCMPUQZ256rmibk: NewOpc = X86::VPCMPUQZ256rmibk_alt; break;
497 case X86::VPCMPUQZ256rmik: NewOpc = X86::VPCMPUQZ256rmik_alt; break;
498 case X86::VPCMPUQZ256rri: NewOpc = X86::VPCMPUQZ256rri_alt; break;
499 case X86::VPCMPUQZ256rrik: NewOpc = X86::VPCMPUQZ256rrik_alt; break;
500 case X86::VPCMPUQZrmi: NewOpc = X86::VPCMPUQZrmi_alt; break;
501 case X86::VPCMPUQZrmib: NewOpc = X86::VPCMPUQZrmib_alt; break;
502 case X86::VPCMPUQZrmibk: NewOpc = X86::VPCMPUQZrmibk_alt; break;
503 case X86::VPCMPUQZrmik: NewOpc = X86::VPCMPUQZrmik_alt; break;
504 case X86::VPCMPUQZrri: NewOpc = X86::VPCMPUQZrri_alt; break;
505 case X86::VPCMPUQZrrik: NewOpc = X86::VPCMPUQZrrik_alt; break;
506 case X86::VPCMPUWZ128rmi: NewOpc = X86::VPCMPUWZ128rmi_alt; break;
507 case X86::VPCMPUWZ128rmik: NewOpc = X86::VPCMPUWZ128rmik_alt; break;
508 case X86::VPCMPUWZ128rri: NewOpc = X86::VPCMPUWZ128rri_alt; break;
509 case X86::VPCMPUWZ128rrik: NewOpc = X86::VPCMPUWZ128rrik_alt; break;
510 case X86::VPCMPUWZ256rmi: NewOpc = X86::VPCMPUWZ256rmi_alt; break;
511 case X86::VPCMPUWZ256rmik: NewOpc = X86::VPCMPUWZ256rmik_alt; break;
512 case X86::VPCMPUWZ256rri: NewOpc = X86::VPCMPUWZ256rri_alt; break;
513 case X86::VPCMPUWZ256rrik: NewOpc = X86::VPCMPUWZ256rrik_alt; break;
514 case X86::VPCMPUWZrmi: NewOpc = X86::VPCMPUWZrmi_alt; break;
515 case X86::VPCMPUWZrmik: NewOpc = X86::VPCMPUWZrmik_alt; break;
516 case X86::VPCMPUWZrri: NewOpc = X86::VPCMPUWZrri_alt; break;
517 case X86::VPCMPUWZrrik: NewOpc = X86::VPCMPUWZrrik_alt; break;
518 case X86::VPCMPWZ128rmi: NewOpc = X86::VPCMPWZ128rmi_alt; break;
519 case X86::VPCMPWZ128rmik: NewOpc = X86::VPCMPWZ128rmik_alt; break;
520 case X86::VPCMPWZ128rri: NewOpc = X86::VPCMPWZ128rri_alt; break;
521 case X86::VPCMPWZ128rrik: NewOpc = X86::VPCMPWZ128rrik_alt; break;
522 case X86::VPCMPWZ256rmi: NewOpc = X86::VPCMPWZ256rmi_alt; break;
523 case X86::VPCMPWZ256rmik: NewOpc = X86::VPCMPWZ256rmik_alt; break;
524 case X86::VPCMPWZ256rri: NewOpc = X86::VPCMPWZ256rri_alt; break;
525 case X86::VPCMPWZ256rrik: NewOpc = X86::VPCMPWZ256rrik_alt; break;
526 case X86::VPCMPWZrmi: NewOpc = X86::VPCMPWZrmi_alt; break;
527 case X86::VPCMPWZrmik: NewOpc = X86::VPCMPWZrmik_alt; break;
528 case X86::VPCMPWZrri: NewOpc = X86::VPCMPWZrri_alt; break;
529 case X86::VPCMPWZrrik: NewOpc = X86::VPCMPWZrrik_alt; break;
530 }
531 // Switch opcode to the one that doesn't get special printing.
532 mcInst.setOpcode(NewOpc);
533 }
Kevin Enderby5b03f722011-09-02 20:01:23 +0000534 }
Sean Callanan4cd930f2010-05-05 22:47:27 +0000535
536 switch (type) {
Craig Topperc30fdbc2012-08-31 15:40:30 +0000537 case TYPE_XMM32:
538 case TYPE_XMM64:
Craig Topper96e00e52011-09-14 05:55:28 +0000539 case TYPE_XMM128:
540 mcInst.addOperand(MCOperand::CreateReg(X86::XMM0 + (immediate >> 4)));
541 return;
542 case TYPE_XMM256:
543 mcInst.addOperand(MCOperand::CreateReg(X86::YMM0 + (immediate >> 4)));
544 return;
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000545 case TYPE_XMM512:
546 mcInst.addOperand(MCOperand::CreateReg(X86::ZMM0 + (immediate >> 4)));
547 return;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000548 case TYPE_REL8:
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000549 isBranch = true;
550 pcrel = insn.startLocation + insn.immediateOffset + insn.immediateSize;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000551 if(immediate & 0x80)
552 immediate |= ~(0xffull);
553 break;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000554 case TYPE_REL32:
555 case TYPE_REL64:
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000556 isBranch = true;
557 pcrel = insn.startLocation + insn.immediateOffset + insn.immediateSize;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000558 if(immediate & 0x80000000)
559 immediate |= ~(0xffffffffull);
560 break;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000561 default:
562 // operand is 64 bits wide. Do nothing.
563 break;
564 }
Craig Topper092e2fe2013-08-24 19:50:11 +0000565
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000566 if(!tryAddingSymbolicOperand(immediate + pcrel, isBranch, insn.startLocation,
567 insn.immediateOffset, insn.immediateSize,
568 mcInst, Dis))
569 mcInst.addOperand(MCOperand::CreateImm(immediate));
Craig Topper35da3d12014-01-16 07:36:58 +0000570
571 if (type == TYPE_MOFFS8 || type == TYPE_MOFFS16 ||
572 type == TYPE_MOFFS32 || type == TYPE_MOFFS64) {
573 MCOperand segmentReg;
574 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
575 mcInst.addOperand(segmentReg);
576 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000577}
578
579/// translateRMRegister - Translates a register stored in the R/M field of the
580/// ModR/M byte to its LLVM equivalent and appends it to an MCInst.
581/// @param mcInst - The MCInst to append to.
582/// @param insn - The internal instruction to extract the R/M field
583/// from.
Sean Callanan010b3732010-04-02 21:23:51 +0000584/// @return - 0 on success; -1 otherwise
585static bool translateRMRegister(MCInst &mcInst,
Sean Callanan04cc3072009-12-19 02:59:52 +0000586 InternalInstruction &insn) {
Sean Callanan010b3732010-04-02 21:23:51 +0000587 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
588 debug("A R/M register operand may not have a SIB byte");
589 return true;
590 }
Michael Liao5bf95782014-12-04 05:20:33 +0000591
Sean Callanan04cc3072009-12-19 02:59:52 +0000592 switch (insn.eaBase) {
Sean Callanan010b3732010-04-02 21:23:51 +0000593 default:
594 debug("Unexpected EA base register");
595 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000596 case EA_BASE_NONE:
Sean Callanan010b3732010-04-02 21:23:51 +0000597 debug("EA_BASE_NONE for ModR/M base");
598 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000599#define ENTRY(x) case EA_BASE_##x:
600 ALL_EA_BASES
601#undef ENTRY
Sean Callanan010b3732010-04-02 21:23:51 +0000602 debug("A R/M register operand may not have a base; "
603 "the operand must be a register.");
604 return true;
605#define ENTRY(x) \
Sean Callanan04cc3072009-12-19 02:59:52 +0000606 case EA_REG_##x: \
607 mcInst.addOperand(MCOperand::CreateReg(X86::x)); break;
608 ALL_REGS
609#undef ENTRY
Sean Callanan04cc3072009-12-19 02:59:52 +0000610 }
Michael Liao5bf95782014-12-04 05:20:33 +0000611
Sean Callanan010b3732010-04-02 21:23:51 +0000612 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000613}
614
615/// translateRMMemory - Translates a memory operand stored in the Mod and R/M
616/// fields of an internal instruction (and possibly its SIB byte) to a memory
617/// operand in LLVM's format, and appends it to an MCInst.
618///
619/// @param mcInst - The MCInst to append to.
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
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000623static bool translateRMMemory(MCInst &mcInst, InternalInstruction &insn,
Michael Liao5bf95782014-12-04 05:20:33 +0000624 const MCDisassembler *Dis) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000625 // Addresses in an MCInst are represented as five operands:
Michael Liao5bf95782014-12-04 05:20:33 +0000626 // 1. basereg (register) The R/M base, or (if there is a SIB) the
Sean Callanan04cc3072009-12-19 02:59:52 +0000627 // SIB base
Michael Liao5bf95782014-12-04 05:20:33 +0000628 // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified
Sean Callanan04cc3072009-12-19 02:59:52 +0000629 // scale amount
630 // 3. indexreg (register) x86_registerNONE, or (if there is a SIB)
Michael Liao5bf95782014-12-04 05:20:33 +0000631 // the index (which is multiplied by the
Sean Callanan04cc3072009-12-19 02:59:52 +0000632 // scale amount)
633 // 4. displacement (immediate) 0, or the displacement if there is one
634 // 5. segmentreg (register) x86_registerNONE for now, but could be set
635 // if we have segment overrides
Michael Liao5bf95782014-12-04 05:20:33 +0000636
Sean Callanan04cc3072009-12-19 02:59:52 +0000637 MCOperand baseReg;
638 MCOperand scaleAmount;
639 MCOperand indexReg;
640 MCOperand displacement;
641 MCOperand segmentReg;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000642 uint64_t pcrel = 0;
Michael Liao5bf95782014-12-04 05:20:33 +0000643
Sean Callanan04cc3072009-12-19 02:59:52 +0000644 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
645 if (insn.sibBase != SIB_BASE_NONE) {
646 switch (insn.sibBase) {
647 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000648 debug("Unexpected sibBase");
649 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000650#define ENTRY(x) \
Sean Callanan36eab802009-12-22 21:12:55 +0000651 case SIB_BASE_##x: \
Sean Callanan04cc3072009-12-19 02:59:52 +0000652 baseReg = MCOperand::CreateReg(X86::x); break;
653 ALL_SIB_BASES
654#undef ENTRY
655 }
656 } else {
657 baseReg = MCOperand::CreateReg(0);
658 }
Manman Rena0982042012-06-26 19:47:59 +0000659
660 // Check whether we are handling VSIB addressing mode for GATHER.
661 // If sibIndex was set to SIB_INDEX_NONE, index offset is 4 and
662 // we should use SIB_INDEX_XMM4|YMM4 for VSIB.
663 // I don't see a way to get the correct IndexReg in readSIB:
664 // We can tell whether it is VSIB or SIB after instruction ID is decoded,
665 // but instruction ID may not be decoded yet when calling readSIB.
666 uint32_t Opcode = mcInst.getOpcode();
Manman Ren98a5bf22012-06-29 00:54:20 +0000667 bool IndexIs128 = (Opcode == X86::VGATHERDPDrm ||
668 Opcode == X86::VGATHERDPDYrm ||
669 Opcode == X86::VGATHERQPDrm ||
670 Opcode == X86::VGATHERDPSrm ||
671 Opcode == X86::VGATHERQPSrm ||
672 Opcode == X86::VPGATHERDQrm ||
673 Opcode == X86::VPGATHERDQYrm ||
674 Opcode == X86::VPGATHERQQrm ||
675 Opcode == X86::VPGATHERDDrm ||
676 Opcode == X86::VPGATHERQDrm);
677 bool IndexIs256 = (Opcode == X86::VGATHERQPDYrm ||
678 Opcode == X86::VGATHERDPSYrm ||
679 Opcode == X86::VGATHERQPSYrm ||
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000680 Opcode == X86::VGATHERDPDZrm ||
681 Opcode == X86::VPGATHERDQZrm ||
Manman Ren98a5bf22012-06-29 00:54:20 +0000682 Opcode == X86::VPGATHERQQYrm ||
683 Opcode == X86::VPGATHERDDYrm ||
684 Opcode == X86::VPGATHERQDYrm);
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000685 bool IndexIs512 = (Opcode == X86::VGATHERQPDZrm ||
686 Opcode == X86::VGATHERDPSZrm ||
687 Opcode == X86::VGATHERQPSZrm ||
688 Opcode == X86::VPGATHERQQZrm ||
689 Opcode == X86::VPGATHERDDZrm ||
690 Opcode == X86::VPGATHERQDZrm);
691 if (IndexIs128 || IndexIs256 || IndexIs512) {
Manman Rena0982042012-06-26 19:47:59 +0000692 unsigned IndexOffset = insn.sibIndex -
693 (insn.addressSize == 8 ? SIB_INDEX_RAX:SIB_INDEX_EAX);
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000694 SIBIndex IndexBase = IndexIs512 ? SIB_INDEX_ZMM0 :
695 IndexIs256 ? SIB_INDEX_YMM0 : SIB_INDEX_XMM0;
Michael Liao5bf95782014-12-04 05:20:33 +0000696 insn.sibIndex = (SIBIndex)(IndexBase +
Manman Rena0982042012-06-26 19:47:59 +0000697 (insn.sibIndex == SIB_INDEX_NONE ? 4 : IndexOffset));
698 }
699
Sean Callanan04cc3072009-12-19 02:59:52 +0000700 if (insn.sibIndex != SIB_INDEX_NONE) {
701 switch (insn.sibIndex) {
702 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000703 debug("Unexpected sibIndex");
704 return true;
Sean Callanan36eab802009-12-22 21:12:55 +0000705#define ENTRY(x) \
Sean Callanan04cc3072009-12-19 02:59:52 +0000706 case SIB_INDEX_##x: \
707 indexReg = MCOperand::CreateReg(X86::x); break;
708 EA_BASES_32BIT
709 EA_BASES_64BIT
Manman Rena0982042012-06-26 19:47:59 +0000710 REGS_XMM
711 REGS_YMM
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000712 REGS_ZMM
Sean Callanan04cc3072009-12-19 02:59:52 +0000713#undef ENTRY
714 }
715 } else {
716 indexReg = MCOperand::CreateReg(0);
717 }
Michael Liao5bf95782014-12-04 05:20:33 +0000718
Sean Callanan04cc3072009-12-19 02:59:52 +0000719 scaleAmount = MCOperand::CreateImm(insn.sibScale);
720 } else {
721 switch (insn.eaBase) {
722 case EA_BASE_NONE:
Sean Callanan010b3732010-04-02 21:23:51 +0000723 if (insn.eaDisplacement == EA_DISP_NONE) {
724 debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
725 return true;
726 }
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000727 if (insn.mode == MODE_64BIT){
728 pcrel = insn.startLocation +
729 insn.displacementOffset + insn.displacementSize;
Kevin Enderbyb119c082012-02-29 22:58:34 +0000730 tryAddingPcLoadReferenceComment(insn.startLocation +
731 insn.displacementOffset,
732 insn.displacement + pcrel, Dis);
Sean Callanan04cc3072009-12-19 02:59:52 +0000733 baseReg = MCOperand::CreateReg(X86::RIP); // Section 2.2.1.6
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000734 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000735 else
736 baseReg = MCOperand::CreateReg(0);
Michael Liao5bf95782014-12-04 05:20:33 +0000737
Sean Callanan04cc3072009-12-19 02:59:52 +0000738 indexReg = MCOperand::CreateReg(0);
739 break;
740 case EA_BASE_BX_SI:
741 baseReg = MCOperand::CreateReg(X86::BX);
742 indexReg = MCOperand::CreateReg(X86::SI);
743 break;
744 case EA_BASE_BX_DI:
745 baseReg = MCOperand::CreateReg(X86::BX);
746 indexReg = MCOperand::CreateReg(X86::DI);
747 break;
748 case EA_BASE_BP_SI:
749 baseReg = MCOperand::CreateReg(X86::BP);
750 indexReg = MCOperand::CreateReg(X86::SI);
751 break;
752 case EA_BASE_BP_DI:
753 baseReg = MCOperand::CreateReg(X86::BP);
754 indexReg = MCOperand::CreateReg(X86::DI);
755 break;
756 default:
757 indexReg = MCOperand::CreateReg(0);
758 switch (insn.eaBase) {
759 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000760 debug("Unexpected eaBase");
761 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000762 // Here, we will use the fill-ins defined above. However,
763 // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and
764 // sib and sib64 were handled in the top-level if, so they're only
765 // placeholders to keep the compiler happy.
766#define ENTRY(x) \
767 case EA_BASE_##x: \
Michael Liao5bf95782014-12-04 05:20:33 +0000768 baseReg = MCOperand::CreateReg(X86::x); break;
Sean Callanan04cc3072009-12-19 02:59:52 +0000769 ALL_EA_BASES
770#undef ENTRY
771#define ENTRY(x) case EA_REG_##x:
772 ALL_REGS
773#undef ENTRY
Sean Callanan010b3732010-04-02 21:23:51 +0000774 debug("A R/M memory operand may not be a register; "
775 "the base field must be a base.");
776 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000777 }
778 }
Michael Liao5bf95782014-12-04 05:20:33 +0000779
Sean Callanan36eab802009-12-22 21:12:55 +0000780 scaleAmount = MCOperand::CreateImm(1);
Sean Callanan04cc3072009-12-19 02:59:52 +0000781 }
Michael Liao5bf95782014-12-04 05:20:33 +0000782
Sean Callanan04cc3072009-12-19 02:59:52 +0000783 displacement = MCOperand::CreateImm(insn.displacement);
Craig Topper35da3d12014-01-16 07:36:58 +0000784
Sean Callanan04cc3072009-12-19 02:59:52 +0000785 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
Michael Liao5bf95782014-12-04 05:20:33 +0000786
Sean Callanan04cc3072009-12-19 02:59:52 +0000787 mcInst.addOperand(baseReg);
788 mcInst.addOperand(scaleAmount);
789 mcInst.addOperand(indexReg);
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000790 if(!tryAddingSymbolicOperand(insn.displacement + pcrel, false,
791 insn.startLocation, insn.displacementOffset,
792 insn.displacementSize, mcInst, Dis))
793 mcInst.addOperand(displacement);
Chris Lattner55595fb2010-07-13 04:23:55 +0000794 mcInst.addOperand(segmentReg);
Sean Callanan010b3732010-04-02 21:23:51 +0000795 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000796}
797
798/// translateRM - Translates an operand stored in the R/M (and possibly SIB)
799/// byte of an instruction to LLVM form, and appends it to an MCInst.
800///
801/// @param mcInst - The MCInst to append to.
802/// @param operand - The operand, as stored in the descriptor table.
803/// @param insn - The instruction to extract Mod, R/M, and SIB fields
804/// from.
Sean Callanan010b3732010-04-02 21:23:51 +0000805/// @return - 0 on success; nonzero otherwise
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +0000806static bool translateRM(MCInst &mcInst, const OperandSpecifier &operand,
Michael Liao5bf95782014-12-04 05:20:33 +0000807 InternalInstruction &insn, const MCDisassembler *Dis) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000808 switch (operand.type) {
809 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000810 debug("Unexpected type for a R/M operand");
811 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000812 case TYPE_R8:
813 case TYPE_R16:
814 case TYPE_R32:
815 case TYPE_R64:
816 case TYPE_Rv:
Sean Callanan04cc3072009-12-19 02:59:52 +0000817 case TYPE_MM64:
818 case TYPE_XMM:
819 case TYPE_XMM32:
820 case TYPE_XMM64:
821 case TYPE_XMM128:
Sean Callananc3fd5232011-03-15 01:23:15 +0000822 case TYPE_XMM256:
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000823 case TYPE_XMM512:
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000824 case TYPE_VK1:
825 case TYPE_VK8:
826 case TYPE_VK16:
Sean Callanan04cc3072009-12-19 02:59:52 +0000827 case TYPE_DEBUGREG:
Sean Callanane7e1cf92010-05-06 20:59:00 +0000828 case TYPE_CONTROLREG:
Sean Callanan010b3732010-04-02 21:23:51 +0000829 return translateRMRegister(mcInst, insn);
Sean Callanan04cc3072009-12-19 02:59:52 +0000830 case TYPE_M:
831 case TYPE_M8:
832 case TYPE_M16:
833 case TYPE_M32:
834 case TYPE_M64:
835 case TYPE_M128:
Sean Callananc3fd5232011-03-15 01:23:15 +0000836 case TYPE_M256:
Sean Callanan04cc3072009-12-19 02:59:52 +0000837 case TYPE_M512:
838 case TYPE_Mv:
839 case TYPE_M32FP:
840 case TYPE_M64FP:
841 case TYPE_M80FP:
Sean Callanan04cc3072009-12-19 02:59:52 +0000842 case TYPE_M1616:
843 case TYPE_M1632:
844 case TYPE_M1664:
Sean Callanan36eab802009-12-22 21:12:55 +0000845 case TYPE_LEA:
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000846 return translateRMMemory(mcInst, insn, Dis);
Sean Callanan04cc3072009-12-19 02:59:52 +0000847 }
848}
Michael Liao5bf95782014-12-04 05:20:33 +0000849
Sean Callanan04cc3072009-12-19 02:59:52 +0000850/// translateFPRegister - Translates a stack position on the FPU stack to its
851/// LLVM form, and appends it to an MCInst.
852///
853/// @param mcInst - The MCInst to append to.
854/// @param stackPos - The stack position to translate.
Craig Topper91551182014-01-01 15:29:32 +0000855static void translateFPRegister(MCInst &mcInst,
856 uint8_t stackPos) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000857 mcInst.addOperand(MCOperand::CreateReg(X86::ST0 + stackPos));
858}
859
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000860/// translateMaskRegister - Translates a 3-bit mask register number to
861/// LLVM form, and appends it to an MCInst.
862///
863/// @param mcInst - The MCInst to append to.
864/// @param maskRegNum - Number of mask register from 0 to 7.
865/// @return - false on success; true otherwise.
866static bool translateMaskRegister(MCInst &mcInst,
867 uint8_t maskRegNum) {
868 if (maskRegNum >= 8) {
869 debug("Invalid mask register number");
870 return true;
871 }
872
873 mcInst.addOperand(MCOperand::CreateReg(X86::K0 + maskRegNum));
874 return false;
875}
876
Michael Liao5bf95782014-12-04 05:20:33 +0000877/// translateOperand - Translates an operand stored in an internal instruction
Sean Callanan04cc3072009-12-19 02:59:52 +0000878/// to LLVM's format and appends it to an MCInst.
879///
880/// @param mcInst - The MCInst to append to.
881/// @param operand - The operand, as stored in the descriptor table.
882/// @param insn - The internal instruction.
Sean Callanan010b3732010-04-02 21:23:51 +0000883/// @return - false on success; true otherwise.
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +0000884static bool translateOperand(MCInst &mcInst, const OperandSpecifier &operand,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000885 InternalInstruction &insn,
Michael Liao5bf95782014-12-04 05:20:33 +0000886 const MCDisassembler *Dis) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000887 switch (operand.encoding) {
888 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000889 debug("Unhandled operand encoding during translation");
890 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000891 case ENCODING_REG:
892 translateRegister(mcInst, insn.reg);
Sean Callanan010b3732010-04-02 21:23:51 +0000893 return false;
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000894 case ENCODING_WRITEMASK:
895 return translateMaskRegister(mcInst, insn.writemask);
Adam Nemet5933c2f2014-07-17 17:04:56 +0000896 CASE_ENCODING_RM:
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000897 return translateRM(mcInst, operand, insn, Dis);
Sean Callanan04cc3072009-12-19 02:59:52 +0000898 case ENCODING_CB:
899 case ENCODING_CW:
900 case ENCODING_CD:
901 case ENCODING_CP:
902 case ENCODING_CO:
903 case ENCODING_CT:
Sean Callanan010b3732010-04-02 21:23:51 +0000904 debug("Translation of code offsets isn't supported.");
905 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000906 case ENCODING_IB:
907 case ENCODING_IW:
908 case ENCODING_ID:
909 case ENCODING_IO:
910 case ENCODING_Iv:
911 case ENCODING_Ia:
Sean Callanan4cd930f2010-05-05 22:47:27 +0000912 translateImmediate(mcInst,
913 insn.immediates[insn.numImmediatesTranslated++],
914 operand,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000915 insn,
916 Dis);
Sean Callanan010b3732010-04-02 21:23:51 +0000917 return false;
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000918 case ENCODING_SI:
919 return translateSrcIndex(mcInst, insn);
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000920 case ENCODING_DI:
921 return translateDstIndex(mcInst, insn);
Sean Callanan04cc3072009-12-19 02:59:52 +0000922 case ENCODING_RB:
923 case ENCODING_RW:
924 case ENCODING_RD:
925 case ENCODING_RO:
Craig Topper91551182014-01-01 15:29:32 +0000926 case ENCODING_Rv:
Sean Callanan04cc3072009-12-19 02:59:52 +0000927 translateRegister(mcInst, insn.opcodeRegister);
Sean Callanan010b3732010-04-02 21:23:51 +0000928 return false;
Craig Topper623b0d62014-01-01 14:22:37 +0000929 case ENCODING_FP:
Craig Topper91551182014-01-01 15:29:32 +0000930 translateFPRegister(mcInst, insn.modRM & 7);
Sean Callanan010b3732010-04-02 21:23:51 +0000931 return false;
Sean Callananc3fd5232011-03-15 01:23:15 +0000932 case ENCODING_VVVV:
933 translateRegister(mcInst, insn.vvvv);
934 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000935 case ENCODING_DUP:
Craig Topperb8aec082012-08-01 07:39:18 +0000936 return translateOperand(mcInst, insn.operands[operand.type - TYPE_DUP0],
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000937 insn, Dis);
Sean Callanan04cc3072009-12-19 02:59:52 +0000938 }
939}
Michael Liao5bf95782014-12-04 05:20:33 +0000940
Sean Callanan04cc3072009-12-19 02:59:52 +0000941/// translateInstruction - Translates an internal instruction and all its
942/// operands to an MCInst.
943///
944/// @param mcInst - The MCInst to populate with the instruction's data.
945/// @param insn - The internal instruction.
Sean Callanan010b3732010-04-02 21:23:51 +0000946/// @return - false on success; true otherwise.
947static bool translateInstruction(MCInst &mcInst,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000948 InternalInstruction &insn,
Michael Liao5bf95782014-12-04 05:20:33 +0000949 const MCDisassembler *Dis) {
Sean Callanan010b3732010-04-02 21:23:51 +0000950 if (!insn.spec) {
951 debug("Instruction has no specification");
952 return true;
953 }
Michael Liao5bf95782014-12-04 05:20:33 +0000954
Sean Callanan04cc3072009-12-19 02:59:52 +0000955 mcInst.setOpcode(insn.instructionID);
Kevin Enderby35fd7922013-06-20 22:32:18 +0000956 // If when reading the prefix bytes we determined the overlapping 0xf2 or 0xf3
957 // prefix bytes should be disassembled as xrelease and xacquire then set the
958 // opcode to those instead of the rep and repne opcodes.
959 if (insn.xAcquireRelease) {
960 if(mcInst.getOpcode() == X86::REP_PREFIX)
961 mcInst.setOpcode(X86::XRELEASE_PREFIX);
962 else if(mcInst.getOpcode() == X86::REPNE_PREFIX)
963 mcInst.setOpcode(X86::XACQUIRE_PREFIX);
964 }
Michael Liao5bf95782014-12-04 05:20:33 +0000965
Sean Callanan04cc3072009-12-19 02:59:52 +0000966 insn.numImmediatesTranslated = 0;
Michael Liao5bf95782014-12-04 05:20:33 +0000967
Patrik Hagglund31998382014-04-28 12:12:27 +0000968 for (const auto &Op : insn.operands) {
969 if (Op.encoding != ENCODING_NONE) {
970 if (translateOperand(mcInst, Op, insn, Dis)) {
Sean Callanan010b3732010-04-02 21:23:51 +0000971 return true;
972 }
973 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000974 }
Michael Liao5bf95782014-12-04 05:20:33 +0000975
Sean Callanan010b3732010-04-02 21:23:51 +0000976 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000977}
Daniel Dunbar900f2ce2009-11-25 06:53:08 +0000978
David Woodhouse7dd21822014-01-20 12:02:31 +0000979static MCDisassembler *createX86Disassembler(const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000980 const MCSubtargetInfo &STI,
981 MCContext &Ctx) {
Lang Hames0563ca12014-04-13 04:09:16 +0000982 std::unique_ptr<const MCInstrInfo> MII(T.createMCInstrInfo());
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000983 return new X86Disassembler::X86GenericDisassembler(STI, Ctx, std::move(MII));
Daniel Dunbar900f2ce2009-11-25 06:53:08 +0000984}
985
Michael Liao5bf95782014-12-04 05:20:33 +0000986extern "C" void LLVMInitializeX86Disassembler() {
Daniel Dunbar900f2ce2009-11-25 06:53:08 +0000987 // Register the disassembler.
Michael Liao5bf95782014-12-04 05:20:33 +0000988 TargetRegistry::RegisterMCDisassembler(TheX86_32Target,
David Woodhouse7dd21822014-01-20 12:02:31 +0000989 createX86Disassembler);
Daniel Dunbar900f2ce2009-11-25 06:53:08 +0000990 TargetRegistry::RegisterMCDisassembler(TheX86_64Target,
David Woodhouse7dd21822014-01-20 12:02:31 +0000991 createX86Disassembler);
Daniel Dunbar900f2ce2009-11-25 06:53:08 +0000992}