blob: 6681e77eb7925537c486e3a51f3bb27ebbddf26b [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,
79 std::unique_ptr<const MCInstrInfo> MII)
80 : MCDisassembler(STI), MII(std::move(MII)) {
David Woodhouse7dd21822014-01-20 12:02:31 +000081 switch (STI.getFeatureBits() &
82 (X86::Mode16Bit | X86::Mode32Bit | X86::Mode64Bit)) {
83 case X86::Mode16Bit:
84 fMode = MODE_16BIT;
85 break;
86 case X86::Mode32Bit:
87 fMode = MODE_32BIT;
88 break;
89 case X86::Mode64Bit:
90 fMode = MODE_64BIT;
91 break;
92 default:
93 llvm_unreachable("Invalid CPU mode");
94 }
95}
Sean Callanan04cc3072009-12-19 02:59:52 +000096
Sean Callanan04cc3072009-12-19 02:59:52 +000097/// regionReader - a callback function that wraps the readByte method from
98/// MemoryObject.
99///
100/// @param arg - The generic callback parameter. In this case, this should
101/// be a pointer to a MemoryObject.
102/// @param byte - A pointer to the byte to be read.
103/// @param address - The address to be read.
Roman Divacky67923802012-09-05 21:17:34 +0000104static int regionReader(const void* arg, uint8_t* byte, uint64_t address) {
105 const MemoryObject* region = static_cast<const MemoryObject*>(arg);
Sean Callanan04cc3072009-12-19 02:59:52 +0000106 return region->readByte(address, byte);
107}
108
109/// logger - a callback function that wraps the operator<< method from
110/// raw_ostream.
111///
112/// @param arg - The generic callback parameter. This should be a pointe
113/// to a raw_ostream.
114/// @param log - A string to be logged. logger() adds a newline.
115static void logger(void* arg, const char* log) {
116 if (!arg)
117 return;
118
119 raw_ostream &vStream = *(static_cast<raw_ostream*>(arg));
120 vStream << log << "\n";
121}
122
123//
124// Public interface for the disassembler
125//
126
Owen Andersona4043c42011-08-17 17:44:15 +0000127MCDisassembler::DecodeStatus
128X86GenericDisassembler::getInstruction(MCInst &instr,
129 uint64_t &size,
Derek Schuff56b662c2012-02-29 01:09:06 +0000130 const MemoryObject &region,
Owen Andersona4043c42011-08-17 17:44:15 +0000131 uint64_t address,
Owen Andersona0c3b972011-09-15 23:38:46 +0000132 raw_ostream &vStream,
133 raw_ostream &cStream) const {
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000134 CommentStream = &cStream;
135
Sean Callanan04cc3072009-12-19 02:59:52 +0000136 InternalInstruction internalInstr;
Benjamin Kramere5e189f2011-09-21 21:47:35 +0000137
138 dlog_t loggerFn = logger;
139 if (&vStream == &nulls())
140 loggerFn = 0; // Disable logging completely if it's going to nulls().
Sean Callanan04cc3072009-12-19 02:59:52 +0000141
142 int ret = decodeInstruction(&internalInstr,
143 regionReader,
Roman Divacky67923802012-09-05 21:17:34 +0000144 (const void*)&region,
Benjamin Kramere5e189f2011-09-21 21:47:35 +0000145 loggerFn,
Sean Callanan04cc3072009-12-19 02:59:52 +0000146 (void*)&vStream,
Lang Hames0563ca12014-04-13 04:09:16 +0000147 (const void*)MII.get(),
Sean Callanan04cc3072009-12-19 02:59:52 +0000148 address,
149 fMode);
150
Sean Callanan010b3732010-04-02 21:23:51 +0000151 if (ret) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000152 size = internalInstr.readerCursor - address;
Owen Andersona4043c42011-08-17 17:44:15 +0000153 return Fail;
Sean Callanan04cc3072009-12-19 02:59:52 +0000154 }
155 else {
156 size = internalInstr.length;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000157 return (!translateInstruction(instr, internalInstr, this)) ?
158 Success : Fail;
Sean Callanan04cc3072009-12-19 02:59:52 +0000159 }
160}
161
162//
163// Private code that translates from struct InternalInstructions to MCInsts.
164//
165
166/// translateRegister - Translates an internal register to the appropriate LLVM
167/// register, and appends it as an operand to an MCInst.
168///
169/// @param mcInst - The MCInst to append to.
170/// @param reg - The Reg to append.
171static void translateRegister(MCInst &mcInst, Reg reg) {
172#define ENTRY(x) X86::x,
173 uint8_t llvmRegnums[] = {
174 ALL_REGS
175 0
176 };
177#undef ENTRY
178
179 uint8_t llvmRegnum = llvmRegnums[reg];
180 mcInst.addOperand(MCOperand::CreateReg(llvmRegnum));
181}
182
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000183/// tryAddingSymbolicOperand - trys to add a symbolic operand in place of the
184/// immediate Value in the MCInst.
185///
186/// @param Value - The immediate Value, has had any PC adjustment made by
187/// the caller.
188/// @param isBranch - If the instruction is a branch instruction
189/// @param Address - The starting address of the instruction
190/// @param Offset - The byte offset to this immediate in the instruction
191/// @param Width - The byte width of this immediate in the instruction
192///
193/// If the getOpInfo() function was set when setupForSymbolicDisassembly() was
194/// called then that function is called to get any symbolic information for the
195/// immediate in the instruction using the Address, Offset and Width. If that
196/// returns non-zero then the symbolic information it returns is used to create
197/// an MCExpr and that is added as an operand to the MCInst. If getOpInfo()
198/// returns zero and isBranch is true then a symbol look up for immediate Value
199/// is done and if a symbol is found an MCExpr is created with that, else
200/// an MCExpr with the immediate Value is created. This function returns true
201/// if it adds an operand to the MCInst and false otherwise.
202static bool tryAddingSymbolicOperand(int64_t Value, bool isBranch,
203 uint64_t Address, uint64_t Offset,
204 uint64_t Width, MCInst &MI,
205 const MCDisassembler *Dis) {
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000206 return Dis->tryAddingSymbolicOperand(MI, Value, Address, isBranch,
207 Offset, Width);
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000208}
209
Kevin Enderbyb119c082012-02-29 22:58:34 +0000210/// tryAddingPcLoadReferenceComment - trys to add a comment as to what is being
211/// referenced by a load instruction with the base register that is the rip.
212/// These can often be addresses in a literal pool. The Address of the
213/// instruction and its immediate Value are used to determine the address
214/// being referenced in the literal pool entry. The SymbolLookUp call back will
215/// return a pointer to a literal 'C' string if the referenced address is an
216/// address into a section with 'C' string literals.
217static void tryAddingPcLoadReferenceComment(uint64_t Address, uint64_t Value,
218 const void *Decoder) {
219 const MCDisassembler *Dis = static_cast<const MCDisassembler*>(Decoder);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000220 Dis->tryAddingPcLoadReferenceComment(Value, Address);
Kevin Enderbyb119c082012-02-29 22:58:34 +0000221}
222
Craig Topper35da3d12014-01-16 07:36:58 +0000223static const uint8_t segmentRegnums[SEG_OVERRIDE_max] = {
224 0, // SEG_OVERRIDE_NONE
225 X86::CS,
226 X86::SS,
227 X86::DS,
228 X86::ES,
229 X86::FS,
230 X86::GS
231};
232
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000233/// translateSrcIndex - Appends a source index operand to an MCInst.
234///
235/// @param mcInst - The MCInst to append to.
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000236/// @param insn - The internal instruction.
237static bool translateSrcIndex(MCInst &mcInst, InternalInstruction &insn) {
238 unsigned baseRegNo;
239
240 if (insn.mode == MODE_64BIT)
241 baseRegNo = insn.prefixPresent[0x67] ? X86::ESI : X86::RSI;
242 else if (insn.mode == MODE_32BIT)
243 baseRegNo = insn.prefixPresent[0x67] ? X86::SI : X86::ESI;
David Woodhousefee418c2014-01-22 15:31:29 +0000244 else {
245 assert(insn.mode == MODE_16BIT);
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000246 baseRegNo = insn.prefixPresent[0x67] ? X86::ESI : X86::SI;
David Woodhousefee418c2014-01-22 15:31:29 +0000247 }
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000248 MCOperand baseReg = MCOperand::CreateReg(baseRegNo);
249 mcInst.addOperand(baseReg);
250
251 MCOperand segmentReg;
252 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
253 mcInst.addOperand(segmentReg);
254 return false;
255}
256
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000257/// translateDstIndex - Appends a destination index operand to an MCInst.
258///
259/// @param mcInst - The MCInst to append to.
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000260/// @param insn - The internal instruction.
261
262static bool translateDstIndex(MCInst &mcInst, InternalInstruction &insn) {
263 unsigned baseRegNo;
264
265 if (insn.mode == MODE_64BIT)
266 baseRegNo = insn.prefixPresent[0x67] ? X86::EDI : X86::RDI;
267 else if (insn.mode == MODE_32BIT)
268 baseRegNo = insn.prefixPresent[0x67] ? X86::DI : X86::EDI;
David Woodhousefee418c2014-01-22 15:31:29 +0000269 else {
270 assert(insn.mode == MODE_16BIT);
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000271 baseRegNo = insn.prefixPresent[0x67] ? X86::EDI : X86::DI;
David Woodhousefee418c2014-01-22 15:31:29 +0000272 }
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000273 MCOperand baseReg = MCOperand::CreateReg(baseRegNo);
274 mcInst.addOperand(baseReg);
275 return false;
276}
277
Sean Callanan04cc3072009-12-19 02:59:52 +0000278/// translateImmediate - Appends an immediate operand to an MCInst.
279///
280/// @param mcInst - The MCInst to append to.
281/// @param immediate - The immediate value to append.
Sean Callanan4cd930f2010-05-05 22:47:27 +0000282/// @param operand - The operand, as stored in the descriptor table.
283/// @param insn - The internal instruction.
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +0000284static void translateImmediate(MCInst &mcInst, uint64_t immediate,
285 const OperandSpecifier &operand,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000286 InternalInstruction &insn,
287 const MCDisassembler *Dis) {
Sean Callanan4cd930f2010-05-05 22:47:27 +0000288 // Sign-extend the immediate if necessary.
289
Craig Topper6dedbae2012-03-04 02:16:41 +0000290 OperandType type = (OperandType)operand.type;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000291
Kevin Enderbyec4bd312012-04-18 23:12:11 +0000292 bool isBranch = false;
293 uint64_t pcrel = 0;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000294 if (type == TYPE_RELv) {
Kevin Enderbyec4bd312012-04-18 23:12:11 +0000295 isBranch = true;
296 pcrel = insn.startLocation +
Kevin Enderby216ac312012-07-24 21:40:01 +0000297 insn.immediateOffset + insn.immediateSize;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000298 switch (insn.displacementSize) {
299 default:
300 break;
Sean Callanan5e8603d2011-02-21 21:55:05 +0000301 case 1:
Craig Topper18854172013-08-25 22:23:38 +0000302 if(immediate & 0x80)
303 immediate |= ~(0xffull);
Sean Callanan4cd930f2010-05-05 22:47:27 +0000304 break;
Sean Callanan5e8603d2011-02-21 21:55:05 +0000305 case 2:
Craig Topper18854172013-08-25 22:23:38 +0000306 if(immediate & 0x8000)
307 immediate |= ~(0xffffull);
Sean Callanan4cd930f2010-05-05 22:47:27 +0000308 break;
Sean Callanan5e8603d2011-02-21 21:55:05 +0000309 case 4:
Craig Topper18854172013-08-25 22:23:38 +0000310 if(immediate & 0x80000000)
311 immediate |= ~(0xffffffffull);
Sean Callanan4cd930f2010-05-05 22:47:27 +0000312 break;
Sean Callanan5e8603d2011-02-21 21:55:05 +0000313 case 8:
Sean Callanan4cd930f2010-05-05 22:47:27 +0000314 break;
315 }
316 }
Kevin Enderby5b03f722011-09-02 20:01:23 +0000317 // By default sign-extend all X86 immediates based on their encoding.
318 else if (type == TYPE_IMM8 || type == TYPE_IMM16 || type == TYPE_IMM32 ||
319 type == TYPE_IMM64) {
320 uint32_t Opcode = mcInst.getOpcode();
321 switch (operand.encoding) {
322 default:
323 break;
324 case ENCODING_IB:
325 // Special case those X86 instructions that use the imm8 as a set of
326 // bits, bit count, etc. and are not sign-extend.
327 if (Opcode != X86::BLENDPSrri && Opcode != X86::BLENDPDrri &&
Bill Wendlingea6397f2012-07-19 00:11:40 +0000328 Opcode != X86::PBLENDWrri && Opcode != X86::MPSADBWrri &&
329 Opcode != X86::DPPSrri && Opcode != X86::DPPDrri &&
330 Opcode != X86::INSERTPSrr && Opcode != X86::VBLENDPSYrri &&
331 Opcode != X86::VBLENDPSYrmi && Opcode != X86::VBLENDPDYrri &&
332 Opcode != X86::VBLENDPDYrmi && Opcode != X86::VPBLENDWrri &&
333 Opcode != X86::VMPSADBWrri && Opcode != X86::VDPPSYrri &&
334 Opcode != X86::VDPPSYrmi && Opcode != X86::VDPPDrri &&
335 Opcode != X86::VINSERTPSrr)
Craig Topper18854172013-08-25 22:23:38 +0000336 if(immediate & 0x80)
337 immediate |= ~(0xffull);
Kevin Enderby5b03f722011-09-02 20:01:23 +0000338 break;
339 case ENCODING_IW:
Craig Topper18854172013-08-25 22:23:38 +0000340 if(immediate & 0x8000)
341 immediate |= ~(0xffffull);
Kevin Enderby5b03f722011-09-02 20:01:23 +0000342 break;
343 case ENCODING_ID:
Craig Topper18854172013-08-25 22:23:38 +0000344 if(immediate & 0x80000000)
345 immediate |= ~(0xffffffffull);
Kevin Enderby5b03f722011-09-02 20:01:23 +0000346 break;
347 case ENCODING_IO:
Kevin Enderby5b03f722011-09-02 20:01:23 +0000348 break;
349 }
350 }
Sean Callanan4cd930f2010-05-05 22:47:27 +0000351
352 switch (type) {
Craig Topperc30fdbc2012-08-31 15:40:30 +0000353 case TYPE_XMM32:
354 case TYPE_XMM64:
Craig Topper96e00e52011-09-14 05:55:28 +0000355 case TYPE_XMM128:
356 mcInst.addOperand(MCOperand::CreateReg(X86::XMM0 + (immediate >> 4)));
357 return;
358 case TYPE_XMM256:
359 mcInst.addOperand(MCOperand::CreateReg(X86::YMM0 + (immediate >> 4)));
360 return;
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000361 case TYPE_XMM512:
362 mcInst.addOperand(MCOperand::CreateReg(X86::ZMM0 + (immediate >> 4)));
363 return;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000364 case TYPE_REL8:
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000365 isBranch = true;
366 pcrel = insn.startLocation + insn.immediateOffset + insn.immediateSize;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000367 if(immediate & 0x80)
368 immediate |= ~(0xffull);
369 break;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000370 case TYPE_REL32:
371 case TYPE_REL64:
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000372 isBranch = true;
373 pcrel = insn.startLocation + insn.immediateOffset + insn.immediateSize;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000374 if(immediate & 0x80000000)
375 immediate |= ~(0xffffffffull);
376 break;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000377 default:
378 // operand is 64 bits wide. Do nothing.
379 break;
380 }
Craig Topper092e2fe2013-08-24 19:50:11 +0000381
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000382 if(!tryAddingSymbolicOperand(immediate + pcrel, isBranch, insn.startLocation,
383 insn.immediateOffset, insn.immediateSize,
384 mcInst, Dis))
385 mcInst.addOperand(MCOperand::CreateImm(immediate));
Craig Topper35da3d12014-01-16 07:36:58 +0000386
387 if (type == TYPE_MOFFS8 || type == TYPE_MOFFS16 ||
388 type == TYPE_MOFFS32 || type == TYPE_MOFFS64) {
389 MCOperand segmentReg;
390 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
391 mcInst.addOperand(segmentReg);
392 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000393}
394
395/// translateRMRegister - Translates a register stored in the R/M field of the
396/// ModR/M byte to its LLVM equivalent and appends it to an MCInst.
397/// @param mcInst - The MCInst to append to.
398/// @param insn - The internal instruction to extract the R/M field
399/// from.
Sean Callanan010b3732010-04-02 21:23:51 +0000400/// @return - 0 on success; -1 otherwise
401static bool translateRMRegister(MCInst &mcInst,
Sean Callanan04cc3072009-12-19 02:59:52 +0000402 InternalInstruction &insn) {
Sean Callanan010b3732010-04-02 21:23:51 +0000403 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
404 debug("A R/M register operand may not have a SIB byte");
405 return true;
406 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000407
408 switch (insn.eaBase) {
Sean Callanan010b3732010-04-02 21:23:51 +0000409 default:
410 debug("Unexpected EA base register");
411 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000412 case EA_BASE_NONE:
Sean Callanan010b3732010-04-02 21:23:51 +0000413 debug("EA_BASE_NONE for ModR/M base");
414 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000415#define ENTRY(x) case EA_BASE_##x:
416 ALL_EA_BASES
417#undef ENTRY
Sean Callanan010b3732010-04-02 21:23:51 +0000418 debug("A R/M register operand may not have a base; "
419 "the operand must be a register.");
420 return true;
421#define ENTRY(x) \
Sean Callanan04cc3072009-12-19 02:59:52 +0000422 case EA_REG_##x: \
423 mcInst.addOperand(MCOperand::CreateReg(X86::x)); break;
424 ALL_REGS
425#undef ENTRY
Sean Callanan04cc3072009-12-19 02:59:52 +0000426 }
Sean Callanan010b3732010-04-02 21:23:51 +0000427
428 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000429}
430
431/// translateRMMemory - Translates a memory operand stored in the Mod and R/M
432/// fields of an internal instruction (and possibly its SIB byte) to a memory
433/// operand in LLVM's format, and appends it to an MCInst.
434///
435/// @param mcInst - The MCInst to append to.
436/// @param insn - The instruction to extract Mod, R/M, and SIB fields
437/// from.
Sean Callanan010b3732010-04-02 21:23:51 +0000438/// @return - 0 on success; nonzero otherwise
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000439static bool translateRMMemory(MCInst &mcInst, InternalInstruction &insn,
440 const MCDisassembler *Dis) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000441 // Addresses in an MCInst are represented as five operands:
442 // 1. basereg (register) The R/M base, or (if there is a SIB) the
443 // SIB base
444 // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified
445 // scale amount
446 // 3. indexreg (register) x86_registerNONE, or (if there is a SIB)
447 // the index (which is multiplied by the
448 // scale amount)
449 // 4. displacement (immediate) 0, or the displacement if there is one
450 // 5. segmentreg (register) x86_registerNONE for now, but could be set
451 // if we have segment overrides
452
453 MCOperand baseReg;
454 MCOperand scaleAmount;
455 MCOperand indexReg;
456 MCOperand displacement;
457 MCOperand segmentReg;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000458 uint64_t pcrel = 0;
Sean Callanan04cc3072009-12-19 02:59:52 +0000459
460 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
461 if (insn.sibBase != SIB_BASE_NONE) {
462 switch (insn.sibBase) {
463 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000464 debug("Unexpected sibBase");
465 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000466#define ENTRY(x) \
Sean Callanan36eab802009-12-22 21:12:55 +0000467 case SIB_BASE_##x: \
Sean Callanan04cc3072009-12-19 02:59:52 +0000468 baseReg = MCOperand::CreateReg(X86::x); break;
469 ALL_SIB_BASES
470#undef ENTRY
471 }
472 } else {
473 baseReg = MCOperand::CreateReg(0);
474 }
Manman Rena0982042012-06-26 19:47:59 +0000475
476 // Check whether we are handling VSIB addressing mode for GATHER.
477 // If sibIndex was set to SIB_INDEX_NONE, index offset is 4 and
478 // we should use SIB_INDEX_XMM4|YMM4 for VSIB.
479 // I don't see a way to get the correct IndexReg in readSIB:
480 // We can tell whether it is VSIB or SIB after instruction ID is decoded,
481 // but instruction ID may not be decoded yet when calling readSIB.
482 uint32_t Opcode = mcInst.getOpcode();
Manman Ren98a5bf22012-06-29 00:54:20 +0000483 bool IndexIs128 = (Opcode == X86::VGATHERDPDrm ||
484 Opcode == X86::VGATHERDPDYrm ||
485 Opcode == X86::VGATHERQPDrm ||
486 Opcode == X86::VGATHERDPSrm ||
487 Opcode == X86::VGATHERQPSrm ||
488 Opcode == X86::VPGATHERDQrm ||
489 Opcode == X86::VPGATHERDQYrm ||
490 Opcode == X86::VPGATHERQQrm ||
491 Opcode == X86::VPGATHERDDrm ||
492 Opcode == X86::VPGATHERQDrm);
493 bool IndexIs256 = (Opcode == X86::VGATHERQPDYrm ||
494 Opcode == X86::VGATHERDPSYrm ||
495 Opcode == X86::VGATHERQPSYrm ||
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000496 Opcode == X86::VGATHERDPDZrm ||
497 Opcode == X86::VPGATHERDQZrm ||
Manman Ren98a5bf22012-06-29 00:54:20 +0000498 Opcode == X86::VPGATHERQQYrm ||
499 Opcode == X86::VPGATHERDDYrm ||
500 Opcode == X86::VPGATHERQDYrm);
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000501 bool IndexIs512 = (Opcode == X86::VGATHERQPDZrm ||
502 Opcode == X86::VGATHERDPSZrm ||
503 Opcode == X86::VGATHERQPSZrm ||
504 Opcode == X86::VPGATHERQQZrm ||
505 Opcode == X86::VPGATHERDDZrm ||
506 Opcode == X86::VPGATHERQDZrm);
507 if (IndexIs128 || IndexIs256 || IndexIs512) {
Manman Rena0982042012-06-26 19:47:59 +0000508 unsigned IndexOffset = insn.sibIndex -
509 (insn.addressSize == 8 ? SIB_INDEX_RAX:SIB_INDEX_EAX);
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000510 SIBIndex IndexBase = IndexIs512 ? SIB_INDEX_ZMM0 :
511 IndexIs256 ? SIB_INDEX_YMM0 : SIB_INDEX_XMM0;
Manman Rena0982042012-06-26 19:47:59 +0000512 insn.sibIndex = (SIBIndex)(IndexBase +
513 (insn.sibIndex == SIB_INDEX_NONE ? 4 : IndexOffset));
514 }
515
Sean Callanan04cc3072009-12-19 02:59:52 +0000516 if (insn.sibIndex != SIB_INDEX_NONE) {
517 switch (insn.sibIndex) {
518 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000519 debug("Unexpected sibIndex");
520 return true;
Sean Callanan36eab802009-12-22 21:12:55 +0000521#define ENTRY(x) \
Sean Callanan04cc3072009-12-19 02:59:52 +0000522 case SIB_INDEX_##x: \
523 indexReg = MCOperand::CreateReg(X86::x); break;
524 EA_BASES_32BIT
525 EA_BASES_64BIT
Manman Rena0982042012-06-26 19:47:59 +0000526 REGS_XMM
527 REGS_YMM
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000528 REGS_ZMM
Sean Callanan04cc3072009-12-19 02:59:52 +0000529#undef ENTRY
530 }
531 } else {
532 indexReg = MCOperand::CreateReg(0);
533 }
534
535 scaleAmount = MCOperand::CreateImm(insn.sibScale);
536 } else {
537 switch (insn.eaBase) {
538 case EA_BASE_NONE:
Sean Callanan010b3732010-04-02 21:23:51 +0000539 if (insn.eaDisplacement == EA_DISP_NONE) {
540 debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
541 return true;
542 }
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000543 if (insn.mode == MODE_64BIT){
544 pcrel = insn.startLocation +
545 insn.displacementOffset + insn.displacementSize;
Kevin Enderbyb119c082012-02-29 22:58:34 +0000546 tryAddingPcLoadReferenceComment(insn.startLocation +
547 insn.displacementOffset,
548 insn.displacement + pcrel, Dis);
Sean Callanan04cc3072009-12-19 02:59:52 +0000549 baseReg = MCOperand::CreateReg(X86::RIP); // Section 2.2.1.6
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000550 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000551 else
552 baseReg = MCOperand::CreateReg(0);
553
554 indexReg = MCOperand::CreateReg(0);
555 break;
556 case EA_BASE_BX_SI:
557 baseReg = MCOperand::CreateReg(X86::BX);
558 indexReg = MCOperand::CreateReg(X86::SI);
559 break;
560 case EA_BASE_BX_DI:
561 baseReg = MCOperand::CreateReg(X86::BX);
562 indexReg = MCOperand::CreateReg(X86::DI);
563 break;
564 case EA_BASE_BP_SI:
565 baseReg = MCOperand::CreateReg(X86::BP);
566 indexReg = MCOperand::CreateReg(X86::SI);
567 break;
568 case EA_BASE_BP_DI:
569 baseReg = MCOperand::CreateReg(X86::BP);
570 indexReg = MCOperand::CreateReg(X86::DI);
571 break;
572 default:
573 indexReg = MCOperand::CreateReg(0);
574 switch (insn.eaBase) {
575 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000576 debug("Unexpected eaBase");
577 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000578 // Here, we will use the fill-ins defined above. However,
579 // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and
580 // sib and sib64 were handled in the top-level if, so they're only
581 // placeholders to keep the compiler happy.
582#define ENTRY(x) \
583 case EA_BASE_##x: \
584 baseReg = MCOperand::CreateReg(X86::x); break;
585 ALL_EA_BASES
586#undef ENTRY
587#define ENTRY(x) case EA_REG_##x:
588 ALL_REGS
589#undef ENTRY
Sean Callanan010b3732010-04-02 21:23:51 +0000590 debug("A R/M memory operand may not be a register; "
591 "the base field must be a base.");
592 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000593 }
594 }
Sean Callanan36eab802009-12-22 21:12:55 +0000595
596 scaleAmount = MCOperand::CreateImm(1);
Sean Callanan04cc3072009-12-19 02:59:52 +0000597 }
598
599 displacement = MCOperand::CreateImm(insn.displacement);
Craig Topper35da3d12014-01-16 07:36:58 +0000600
Sean Callanan04cc3072009-12-19 02:59:52 +0000601 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
602
603 mcInst.addOperand(baseReg);
604 mcInst.addOperand(scaleAmount);
605 mcInst.addOperand(indexReg);
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000606 if(!tryAddingSymbolicOperand(insn.displacement + pcrel, false,
607 insn.startLocation, insn.displacementOffset,
608 insn.displacementSize, mcInst, Dis))
609 mcInst.addOperand(displacement);
Chris Lattner55595fb2010-07-13 04:23:55 +0000610 mcInst.addOperand(segmentReg);
Sean Callanan010b3732010-04-02 21:23:51 +0000611 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000612}
613
614/// translateRM - Translates an operand stored in the R/M (and possibly SIB)
615/// byte of an instruction to LLVM form, and appends it to an MCInst.
616///
617/// @param mcInst - The MCInst to append to.
618/// @param operand - The operand, as stored in the descriptor table.
619/// @param insn - The instruction to extract Mod, R/M, and SIB fields
620/// from.
Sean Callanan010b3732010-04-02 21:23:51 +0000621/// @return - 0 on success; nonzero otherwise
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +0000622static bool translateRM(MCInst &mcInst, const OperandSpecifier &operand,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000623 InternalInstruction &insn, const MCDisassembler *Dis) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000624 switch (operand.type) {
625 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000626 debug("Unexpected type for a R/M operand");
627 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000628 case TYPE_R8:
629 case TYPE_R16:
630 case TYPE_R32:
631 case TYPE_R64:
632 case TYPE_Rv:
633 case TYPE_MM:
634 case TYPE_MM32:
635 case TYPE_MM64:
636 case TYPE_XMM:
637 case TYPE_XMM32:
638 case TYPE_XMM64:
639 case TYPE_XMM128:
Sean Callananc3fd5232011-03-15 01:23:15 +0000640 case TYPE_XMM256:
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000641 case TYPE_XMM512:
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000642 case TYPE_VK1:
643 case TYPE_VK8:
644 case TYPE_VK16:
Sean Callanan04cc3072009-12-19 02:59:52 +0000645 case TYPE_DEBUGREG:
Sean Callanane7e1cf92010-05-06 20:59:00 +0000646 case TYPE_CONTROLREG:
Sean Callanan010b3732010-04-02 21:23:51 +0000647 return translateRMRegister(mcInst, insn);
Sean Callanan04cc3072009-12-19 02:59:52 +0000648 case TYPE_M:
649 case TYPE_M8:
650 case TYPE_M16:
651 case TYPE_M32:
652 case TYPE_M64:
653 case TYPE_M128:
Sean Callananc3fd5232011-03-15 01:23:15 +0000654 case TYPE_M256:
Sean Callanan04cc3072009-12-19 02:59:52 +0000655 case TYPE_M512:
656 case TYPE_Mv:
657 case TYPE_M32FP:
658 case TYPE_M64FP:
659 case TYPE_M80FP:
660 case TYPE_M16INT:
661 case TYPE_M32INT:
662 case TYPE_M64INT:
663 case TYPE_M1616:
664 case TYPE_M1632:
665 case TYPE_M1664:
Sean Callanan36eab802009-12-22 21:12:55 +0000666 case TYPE_LEA:
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000667 return translateRMMemory(mcInst, insn, Dis);
Sean Callanan04cc3072009-12-19 02:59:52 +0000668 }
669}
670
671/// translateFPRegister - Translates a stack position on the FPU stack to its
672/// LLVM form, and appends it to an MCInst.
673///
674/// @param mcInst - The MCInst to append to.
675/// @param stackPos - The stack position to translate.
Craig Topper91551182014-01-01 15:29:32 +0000676static void translateFPRegister(MCInst &mcInst,
677 uint8_t stackPos) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000678 mcInst.addOperand(MCOperand::CreateReg(X86::ST0 + stackPos));
679}
680
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000681/// translateMaskRegister - Translates a 3-bit mask register number to
682/// LLVM form, and appends it to an MCInst.
683///
684/// @param mcInst - The MCInst to append to.
685/// @param maskRegNum - Number of mask register from 0 to 7.
686/// @return - false on success; true otherwise.
687static bool translateMaskRegister(MCInst &mcInst,
688 uint8_t maskRegNum) {
689 if (maskRegNum >= 8) {
690 debug("Invalid mask register number");
691 return true;
692 }
693
694 mcInst.addOperand(MCOperand::CreateReg(X86::K0 + maskRegNum));
695 return false;
696}
697
Sean Callanan04cc3072009-12-19 02:59:52 +0000698/// translateOperand - Translates an operand stored in an internal instruction
699/// to LLVM's format and appends it to an MCInst.
700///
701/// @param mcInst - The MCInst to append to.
702/// @param operand - The operand, as stored in the descriptor table.
703/// @param insn - The internal instruction.
Sean Callanan010b3732010-04-02 21:23:51 +0000704/// @return - false on success; true otherwise.
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +0000705static bool translateOperand(MCInst &mcInst, const OperandSpecifier &operand,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000706 InternalInstruction &insn,
707 const MCDisassembler *Dis) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000708 switch (operand.encoding) {
709 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000710 debug("Unhandled operand encoding during translation");
711 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000712 case ENCODING_REG:
713 translateRegister(mcInst, insn.reg);
Sean Callanan010b3732010-04-02 21:23:51 +0000714 return false;
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000715 case ENCODING_WRITEMASK:
716 return translateMaskRegister(mcInst, insn.writemask);
Sean Callanan04cc3072009-12-19 02:59:52 +0000717 case ENCODING_RM:
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000718 return translateRM(mcInst, operand, insn, Dis);
Sean Callanan04cc3072009-12-19 02:59:52 +0000719 case ENCODING_CB:
720 case ENCODING_CW:
721 case ENCODING_CD:
722 case ENCODING_CP:
723 case ENCODING_CO:
724 case ENCODING_CT:
Sean Callanan010b3732010-04-02 21:23:51 +0000725 debug("Translation of code offsets isn't supported.");
726 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000727 case ENCODING_IB:
728 case ENCODING_IW:
729 case ENCODING_ID:
730 case ENCODING_IO:
731 case ENCODING_Iv:
732 case ENCODING_Ia:
Sean Callanan4cd930f2010-05-05 22:47:27 +0000733 translateImmediate(mcInst,
734 insn.immediates[insn.numImmediatesTranslated++],
735 operand,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000736 insn,
737 Dis);
Sean Callanan010b3732010-04-02 21:23:51 +0000738 return false;
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000739 case ENCODING_SI:
740 return translateSrcIndex(mcInst, insn);
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000741 case ENCODING_DI:
742 return translateDstIndex(mcInst, insn);
Sean Callanan04cc3072009-12-19 02:59:52 +0000743 case ENCODING_RB:
744 case ENCODING_RW:
745 case ENCODING_RD:
746 case ENCODING_RO:
Craig Topper91551182014-01-01 15:29:32 +0000747 case ENCODING_Rv:
Sean Callanan04cc3072009-12-19 02:59:52 +0000748 translateRegister(mcInst, insn.opcodeRegister);
Sean Callanan010b3732010-04-02 21:23:51 +0000749 return false;
Craig Topper623b0d62014-01-01 14:22:37 +0000750 case ENCODING_FP:
Craig Topper91551182014-01-01 15:29:32 +0000751 translateFPRegister(mcInst, insn.modRM & 7);
Sean Callanan010b3732010-04-02 21:23:51 +0000752 return false;
Sean Callananc3fd5232011-03-15 01:23:15 +0000753 case ENCODING_VVVV:
754 translateRegister(mcInst, insn.vvvv);
755 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000756 case ENCODING_DUP:
Craig Topperb8aec082012-08-01 07:39:18 +0000757 return translateOperand(mcInst, insn.operands[operand.type - TYPE_DUP0],
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000758 insn, Dis);
Sean Callanan04cc3072009-12-19 02:59:52 +0000759 }
760}
761
762/// translateInstruction - Translates an internal instruction and all its
763/// operands to an MCInst.
764///
765/// @param mcInst - The MCInst to populate with the instruction's data.
766/// @param insn - The internal instruction.
Sean Callanan010b3732010-04-02 21:23:51 +0000767/// @return - false on success; true otherwise.
768static bool translateInstruction(MCInst &mcInst,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000769 InternalInstruction &insn,
770 const MCDisassembler *Dis) {
Sean Callanan010b3732010-04-02 21:23:51 +0000771 if (!insn.spec) {
772 debug("Instruction has no specification");
773 return true;
774 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000775
776 mcInst.setOpcode(insn.instructionID);
Kevin Enderby35fd7922013-06-20 22:32:18 +0000777 // If when reading the prefix bytes we determined the overlapping 0xf2 or 0xf3
778 // prefix bytes should be disassembled as xrelease and xacquire then set the
779 // opcode to those instead of the rep and repne opcodes.
780 if (insn.xAcquireRelease) {
781 if(mcInst.getOpcode() == X86::REP_PREFIX)
782 mcInst.setOpcode(X86::XRELEASE_PREFIX);
783 else if(mcInst.getOpcode() == X86::REPNE_PREFIX)
784 mcInst.setOpcode(X86::XACQUIRE_PREFIX);
785 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000786
787 int index;
788
789 insn.numImmediatesTranslated = 0;
790
791 for (index = 0; index < X86_MAX_OPERANDS; ++index) {
Craig Topperb8aec082012-08-01 07:39:18 +0000792 if (insn.operands[index].encoding != ENCODING_NONE) {
793 if (translateOperand(mcInst, insn.operands[index], insn, Dis)) {
Sean Callanan010b3732010-04-02 21:23:51 +0000794 return true;
795 }
796 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000797 }
Sean Callanan010b3732010-04-02 21:23:51 +0000798
799 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000800}
Daniel Dunbar900f2ce2009-11-25 06:53:08 +0000801
David Woodhouse7dd21822014-01-20 12:02:31 +0000802static MCDisassembler *createX86Disassembler(const Target &T,
803 const MCSubtargetInfo &STI) {
Lang Hames0563ca12014-04-13 04:09:16 +0000804 std::unique_ptr<const MCInstrInfo> MII(T.createMCInstrInfo());
805 return new X86Disassembler::X86GenericDisassembler(STI, std::move(MII));
Daniel Dunbar900f2ce2009-11-25 06:53:08 +0000806}
807
808extern "C" void LLVMInitializeX86Disassembler() {
809 // Register the disassembler.
810 TargetRegistry::RegisterMCDisassembler(TheX86_32Target,
David Woodhouse7dd21822014-01-20 12:02:31 +0000811 createX86Disassembler);
Daniel Dunbar900f2ce2009-11-25 06:53:08 +0000812 TargetRegistry::RegisterMCDisassembler(TheX86_64Target,
David Woodhouse7dd21822014-01-20 12:02:31 +0000813 createX86Disassembler);
Daniel Dunbar900f2ce2009-11-25 06:53:08 +0000814}