blob: dae1345f7086a4dbd6e68a6210b53cddb8118ae3 [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
Benjamin Kramer478e8de2012-02-11 14:50:54 +000077X86GenericDisassembler::X86GenericDisassembler(const MCSubtargetInfo &STI,
Benjamin Kramer478e8de2012-02-11 14:50:54 +000078 const MCInstrInfo *MII)
David Woodhouse7dd21822014-01-20 12:02:31 +000079 : MCDisassembler(STI), MII(MII) {
80 switch (STI.getFeatureBits() &
81 (X86::Mode16Bit | X86::Mode32Bit | X86::Mode64Bit)) {
82 case X86::Mode16Bit:
83 fMode = MODE_16BIT;
84 break;
85 case X86::Mode32Bit:
86 fMode = MODE_32BIT;
87 break;
88 case X86::Mode64Bit:
89 fMode = MODE_64BIT;
90 break;
91 default:
92 llvm_unreachable("Invalid CPU mode");
93 }
94}
Sean Callanan04cc3072009-12-19 02:59:52 +000095
96X86GenericDisassembler::~X86GenericDisassembler() {
Benjamin Kramer478e8de2012-02-11 14:50:54 +000097 delete MII;
Sean Callanan04cc3072009-12-19 02:59:52 +000098}
99
100/// 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())
143 loggerFn = 0; // 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,
Roman Divacky67923802012-09-05 21:17:34 +0000150 (const void*)MII,
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.
239/// @param operand - The operand, as stored in the descriptor table.
240/// @param insn - The internal instruction.
241static bool translateSrcIndex(MCInst &mcInst, InternalInstruction &insn) {
242 unsigned baseRegNo;
243
244 if (insn.mode == MODE_64BIT)
245 baseRegNo = insn.prefixPresent[0x67] ? X86::ESI : X86::RSI;
246 else if (insn.mode == MODE_32BIT)
247 baseRegNo = insn.prefixPresent[0x67] ? X86::SI : X86::ESI;
248 else if (insn.mode == MODE_16BIT)
249 baseRegNo = insn.prefixPresent[0x67] ? X86::ESI : X86::SI;
250 MCOperand baseReg = MCOperand::CreateReg(baseRegNo);
251 mcInst.addOperand(baseReg);
252
253 MCOperand segmentReg;
254 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
255 mcInst.addOperand(segmentReg);
256 return false;
257}
258
Sean Callanan04cc3072009-12-19 02:59:52 +0000259/// translateImmediate - Appends an immediate operand to an MCInst.
260///
261/// @param mcInst - The MCInst to append to.
262/// @param immediate - The immediate value to append.
Sean Callanan4cd930f2010-05-05 22:47:27 +0000263/// @param operand - The operand, as stored in the descriptor table.
264/// @param insn - The internal instruction.
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +0000265static void translateImmediate(MCInst &mcInst, uint64_t immediate,
266 const OperandSpecifier &operand,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000267 InternalInstruction &insn,
268 const MCDisassembler *Dis) {
Sean Callanan4cd930f2010-05-05 22:47:27 +0000269 // Sign-extend the immediate if necessary.
270
Craig Topper6dedbae2012-03-04 02:16:41 +0000271 OperandType type = (OperandType)operand.type;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000272
Kevin Enderbyec4bd312012-04-18 23:12:11 +0000273 bool isBranch = false;
274 uint64_t pcrel = 0;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000275 if (type == TYPE_RELv) {
Kevin Enderbyec4bd312012-04-18 23:12:11 +0000276 isBranch = true;
277 pcrel = insn.startLocation +
Kevin Enderby216ac312012-07-24 21:40:01 +0000278 insn.immediateOffset + insn.immediateSize;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000279 switch (insn.displacementSize) {
280 default:
281 break;
Sean Callanan5e8603d2011-02-21 21:55:05 +0000282 case 1:
Craig Topper18854172013-08-25 22:23:38 +0000283 if(immediate & 0x80)
284 immediate |= ~(0xffull);
Sean Callanan4cd930f2010-05-05 22:47:27 +0000285 break;
Sean Callanan5e8603d2011-02-21 21:55:05 +0000286 case 2:
Craig Topper18854172013-08-25 22:23:38 +0000287 if(immediate & 0x8000)
288 immediate |= ~(0xffffull);
Sean Callanan4cd930f2010-05-05 22:47:27 +0000289 break;
Sean Callanan5e8603d2011-02-21 21:55:05 +0000290 case 4:
Craig Topper18854172013-08-25 22:23:38 +0000291 if(immediate & 0x80000000)
292 immediate |= ~(0xffffffffull);
Sean Callanan4cd930f2010-05-05 22:47:27 +0000293 break;
Sean Callanan5e8603d2011-02-21 21:55:05 +0000294 case 8:
Sean Callanan4cd930f2010-05-05 22:47:27 +0000295 break;
296 }
297 }
Kevin Enderby5b03f722011-09-02 20:01:23 +0000298 // By default sign-extend all X86 immediates based on their encoding.
299 else if (type == TYPE_IMM8 || type == TYPE_IMM16 || type == TYPE_IMM32 ||
300 type == TYPE_IMM64) {
301 uint32_t Opcode = mcInst.getOpcode();
302 switch (operand.encoding) {
303 default:
304 break;
305 case ENCODING_IB:
306 // Special case those X86 instructions that use the imm8 as a set of
307 // bits, bit count, etc. and are not sign-extend.
308 if (Opcode != X86::BLENDPSrri && Opcode != X86::BLENDPDrri &&
Bill Wendlingea6397f2012-07-19 00:11:40 +0000309 Opcode != X86::PBLENDWrri && Opcode != X86::MPSADBWrri &&
310 Opcode != X86::DPPSrri && Opcode != X86::DPPDrri &&
311 Opcode != X86::INSERTPSrr && Opcode != X86::VBLENDPSYrri &&
312 Opcode != X86::VBLENDPSYrmi && Opcode != X86::VBLENDPDYrri &&
313 Opcode != X86::VBLENDPDYrmi && Opcode != X86::VPBLENDWrri &&
314 Opcode != X86::VMPSADBWrri && Opcode != X86::VDPPSYrri &&
315 Opcode != X86::VDPPSYrmi && Opcode != X86::VDPPDrri &&
316 Opcode != X86::VINSERTPSrr)
Craig Topper18854172013-08-25 22:23:38 +0000317 if(immediate & 0x80)
318 immediate |= ~(0xffull);
Kevin Enderby5b03f722011-09-02 20:01:23 +0000319 break;
320 case ENCODING_IW:
Craig Topper18854172013-08-25 22:23:38 +0000321 if(immediate & 0x8000)
322 immediate |= ~(0xffffull);
Kevin Enderby5b03f722011-09-02 20:01:23 +0000323 break;
324 case ENCODING_ID:
Craig Topper18854172013-08-25 22:23:38 +0000325 if(immediate & 0x80000000)
326 immediate |= ~(0xffffffffull);
Kevin Enderby5b03f722011-09-02 20:01:23 +0000327 break;
328 case ENCODING_IO:
Kevin Enderby5b03f722011-09-02 20:01:23 +0000329 break;
330 }
331 }
Sean Callanan4cd930f2010-05-05 22:47:27 +0000332
333 switch (type) {
Craig Topperc30fdbc2012-08-31 15:40:30 +0000334 case TYPE_XMM32:
335 case TYPE_XMM64:
Craig Topper96e00e52011-09-14 05:55:28 +0000336 case TYPE_XMM128:
337 mcInst.addOperand(MCOperand::CreateReg(X86::XMM0 + (immediate >> 4)));
338 return;
339 case TYPE_XMM256:
340 mcInst.addOperand(MCOperand::CreateReg(X86::YMM0 + (immediate >> 4)));
341 return;
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000342 case TYPE_XMM512:
343 mcInst.addOperand(MCOperand::CreateReg(X86::ZMM0 + (immediate >> 4)));
344 return;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000345 case TYPE_REL8:
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000346 isBranch = true;
347 pcrel = insn.startLocation + insn.immediateOffset + insn.immediateSize;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000348 if(immediate & 0x80)
349 immediate |= ~(0xffull);
350 break;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000351 case TYPE_REL32:
352 case TYPE_REL64:
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000353 isBranch = true;
354 pcrel = insn.startLocation + insn.immediateOffset + insn.immediateSize;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000355 if(immediate & 0x80000000)
356 immediate |= ~(0xffffffffull);
357 break;
Sean Callanan4cd930f2010-05-05 22:47:27 +0000358 default:
359 // operand is 64 bits wide. Do nothing.
360 break;
361 }
Craig Topper092e2fe2013-08-24 19:50:11 +0000362
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000363 if(!tryAddingSymbolicOperand(immediate + pcrel, isBranch, insn.startLocation,
364 insn.immediateOffset, insn.immediateSize,
365 mcInst, Dis))
366 mcInst.addOperand(MCOperand::CreateImm(immediate));
Craig Topper35da3d12014-01-16 07:36:58 +0000367
368 if (type == TYPE_MOFFS8 || type == TYPE_MOFFS16 ||
369 type == TYPE_MOFFS32 || type == TYPE_MOFFS64) {
370 MCOperand segmentReg;
371 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
372 mcInst.addOperand(segmentReg);
373 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000374}
375
376/// translateRMRegister - Translates a register stored in the R/M field of the
377/// ModR/M byte to its LLVM equivalent and appends it to an MCInst.
378/// @param mcInst - The MCInst to append to.
379/// @param insn - The internal instruction to extract the R/M field
380/// from.
Sean Callanan010b3732010-04-02 21:23:51 +0000381/// @return - 0 on success; -1 otherwise
382static bool translateRMRegister(MCInst &mcInst,
Sean Callanan04cc3072009-12-19 02:59:52 +0000383 InternalInstruction &insn) {
Sean Callanan010b3732010-04-02 21:23:51 +0000384 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
385 debug("A R/M register operand may not have a SIB byte");
386 return true;
387 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000388
389 switch (insn.eaBase) {
Sean Callanan010b3732010-04-02 21:23:51 +0000390 default:
391 debug("Unexpected EA base register");
392 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000393 case EA_BASE_NONE:
Sean Callanan010b3732010-04-02 21:23:51 +0000394 debug("EA_BASE_NONE for ModR/M base");
395 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000396#define ENTRY(x) case EA_BASE_##x:
397 ALL_EA_BASES
398#undef ENTRY
Sean Callanan010b3732010-04-02 21:23:51 +0000399 debug("A R/M register operand may not have a base; "
400 "the operand must be a register.");
401 return true;
402#define ENTRY(x) \
Sean Callanan04cc3072009-12-19 02:59:52 +0000403 case EA_REG_##x: \
404 mcInst.addOperand(MCOperand::CreateReg(X86::x)); break;
405 ALL_REGS
406#undef ENTRY
Sean Callanan04cc3072009-12-19 02:59:52 +0000407 }
Sean Callanan010b3732010-04-02 21:23:51 +0000408
409 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000410}
411
412/// translateRMMemory - Translates a memory operand stored in the Mod and R/M
413/// fields of an internal instruction (and possibly its SIB byte) to a memory
414/// operand in LLVM's format, and appends it to an MCInst.
415///
416/// @param mcInst - The MCInst to append to.
417/// @param insn - The instruction to extract Mod, R/M, and SIB fields
418/// from.
Sean Callanan010b3732010-04-02 21:23:51 +0000419/// @return - 0 on success; nonzero otherwise
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000420static bool translateRMMemory(MCInst &mcInst, InternalInstruction &insn,
421 const MCDisassembler *Dis) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000422 // Addresses in an MCInst are represented as five operands:
423 // 1. basereg (register) The R/M base, or (if there is a SIB) the
424 // SIB base
425 // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified
426 // scale amount
427 // 3. indexreg (register) x86_registerNONE, or (if there is a SIB)
428 // the index (which is multiplied by the
429 // scale amount)
430 // 4. displacement (immediate) 0, or the displacement if there is one
431 // 5. segmentreg (register) x86_registerNONE for now, but could be set
432 // if we have segment overrides
433
434 MCOperand baseReg;
435 MCOperand scaleAmount;
436 MCOperand indexReg;
437 MCOperand displacement;
438 MCOperand segmentReg;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000439 uint64_t pcrel = 0;
Sean Callanan04cc3072009-12-19 02:59:52 +0000440
441 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
442 if (insn.sibBase != SIB_BASE_NONE) {
443 switch (insn.sibBase) {
444 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000445 debug("Unexpected sibBase");
446 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000447#define ENTRY(x) \
Sean Callanan36eab802009-12-22 21:12:55 +0000448 case SIB_BASE_##x: \
Sean Callanan04cc3072009-12-19 02:59:52 +0000449 baseReg = MCOperand::CreateReg(X86::x); break;
450 ALL_SIB_BASES
451#undef ENTRY
452 }
453 } else {
454 baseReg = MCOperand::CreateReg(0);
455 }
Manman Rena0982042012-06-26 19:47:59 +0000456
457 // Check whether we are handling VSIB addressing mode for GATHER.
458 // If sibIndex was set to SIB_INDEX_NONE, index offset is 4 and
459 // we should use SIB_INDEX_XMM4|YMM4 for VSIB.
460 // I don't see a way to get the correct IndexReg in readSIB:
461 // We can tell whether it is VSIB or SIB after instruction ID is decoded,
462 // but instruction ID may not be decoded yet when calling readSIB.
463 uint32_t Opcode = mcInst.getOpcode();
Manman Ren98a5bf22012-06-29 00:54:20 +0000464 bool IndexIs128 = (Opcode == X86::VGATHERDPDrm ||
465 Opcode == X86::VGATHERDPDYrm ||
466 Opcode == X86::VGATHERQPDrm ||
467 Opcode == X86::VGATHERDPSrm ||
468 Opcode == X86::VGATHERQPSrm ||
469 Opcode == X86::VPGATHERDQrm ||
470 Opcode == X86::VPGATHERDQYrm ||
471 Opcode == X86::VPGATHERQQrm ||
472 Opcode == X86::VPGATHERDDrm ||
473 Opcode == X86::VPGATHERQDrm);
474 bool IndexIs256 = (Opcode == X86::VGATHERQPDYrm ||
475 Opcode == X86::VGATHERDPSYrm ||
476 Opcode == X86::VGATHERQPSYrm ||
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000477 Opcode == X86::VGATHERDPDZrm ||
478 Opcode == X86::VPGATHERDQZrm ||
Manman Ren98a5bf22012-06-29 00:54:20 +0000479 Opcode == X86::VPGATHERQQYrm ||
480 Opcode == X86::VPGATHERDDYrm ||
481 Opcode == X86::VPGATHERQDYrm);
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000482 bool IndexIs512 = (Opcode == X86::VGATHERQPDZrm ||
483 Opcode == X86::VGATHERDPSZrm ||
484 Opcode == X86::VGATHERQPSZrm ||
485 Opcode == X86::VPGATHERQQZrm ||
486 Opcode == X86::VPGATHERDDZrm ||
487 Opcode == X86::VPGATHERQDZrm);
488 if (IndexIs128 || IndexIs256 || IndexIs512) {
Manman Rena0982042012-06-26 19:47:59 +0000489 unsigned IndexOffset = insn.sibIndex -
490 (insn.addressSize == 8 ? SIB_INDEX_RAX:SIB_INDEX_EAX);
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000491 SIBIndex IndexBase = IndexIs512 ? SIB_INDEX_ZMM0 :
492 IndexIs256 ? SIB_INDEX_YMM0 : SIB_INDEX_XMM0;
Manman Rena0982042012-06-26 19:47:59 +0000493 insn.sibIndex = (SIBIndex)(IndexBase +
494 (insn.sibIndex == SIB_INDEX_NONE ? 4 : IndexOffset));
495 }
496
Sean Callanan04cc3072009-12-19 02:59:52 +0000497 if (insn.sibIndex != SIB_INDEX_NONE) {
498 switch (insn.sibIndex) {
499 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000500 debug("Unexpected sibIndex");
501 return true;
Sean Callanan36eab802009-12-22 21:12:55 +0000502#define ENTRY(x) \
Sean Callanan04cc3072009-12-19 02:59:52 +0000503 case SIB_INDEX_##x: \
504 indexReg = MCOperand::CreateReg(X86::x); break;
505 EA_BASES_32BIT
506 EA_BASES_64BIT
Manman Rena0982042012-06-26 19:47:59 +0000507 REGS_XMM
508 REGS_YMM
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000509 REGS_ZMM
Sean Callanan04cc3072009-12-19 02:59:52 +0000510#undef ENTRY
511 }
512 } else {
513 indexReg = MCOperand::CreateReg(0);
514 }
515
516 scaleAmount = MCOperand::CreateImm(insn.sibScale);
517 } else {
518 switch (insn.eaBase) {
519 case EA_BASE_NONE:
Sean Callanan010b3732010-04-02 21:23:51 +0000520 if (insn.eaDisplacement == EA_DISP_NONE) {
521 debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
522 return true;
523 }
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000524 if (insn.mode == MODE_64BIT){
525 pcrel = insn.startLocation +
526 insn.displacementOffset + insn.displacementSize;
Kevin Enderbyb119c082012-02-29 22:58:34 +0000527 tryAddingPcLoadReferenceComment(insn.startLocation +
528 insn.displacementOffset,
529 insn.displacement + pcrel, Dis);
Sean Callanan04cc3072009-12-19 02:59:52 +0000530 baseReg = MCOperand::CreateReg(X86::RIP); // Section 2.2.1.6
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000531 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000532 else
533 baseReg = MCOperand::CreateReg(0);
534
535 indexReg = MCOperand::CreateReg(0);
536 break;
537 case EA_BASE_BX_SI:
538 baseReg = MCOperand::CreateReg(X86::BX);
539 indexReg = MCOperand::CreateReg(X86::SI);
540 break;
541 case EA_BASE_BX_DI:
542 baseReg = MCOperand::CreateReg(X86::BX);
543 indexReg = MCOperand::CreateReg(X86::DI);
544 break;
545 case EA_BASE_BP_SI:
546 baseReg = MCOperand::CreateReg(X86::BP);
547 indexReg = MCOperand::CreateReg(X86::SI);
548 break;
549 case EA_BASE_BP_DI:
550 baseReg = MCOperand::CreateReg(X86::BP);
551 indexReg = MCOperand::CreateReg(X86::DI);
552 break;
553 default:
554 indexReg = MCOperand::CreateReg(0);
555 switch (insn.eaBase) {
556 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000557 debug("Unexpected eaBase");
558 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000559 // Here, we will use the fill-ins defined above. However,
560 // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and
561 // sib and sib64 were handled in the top-level if, so they're only
562 // placeholders to keep the compiler happy.
563#define ENTRY(x) \
564 case EA_BASE_##x: \
565 baseReg = MCOperand::CreateReg(X86::x); break;
566 ALL_EA_BASES
567#undef ENTRY
568#define ENTRY(x) case EA_REG_##x:
569 ALL_REGS
570#undef ENTRY
Sean Callanan010b3732010-04-02 21:23:51 +0000571 debug("A R/M memory operand may not be a register; "
572 "the base field must be a base.");
573 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000574 }
575 }
Sean Callanan36eab802009-12-22 21:12:55 +0000576
577 scaleAmount = MCOperand::CreateImm(1);
Sean Callanan04cc3072009-12-19 02:59:52 +0000578 }
579
580 displacement = MCOperand::CreateImm(insn.displacement);
Craig Topper35da3d12014-01-16 07:36:58 +0000581
Sean Callanan04cc3072009-12-19 02:59:52 +0000582 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]);
583
584 mcInst.addOperand(baseReg);
585 mcInst.addOperand(scaleAmount);
586 mcInst.addOperand(indexReg);
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000587 if(!tryAddingSymbolicOperand(insn.displacement + pcrel, false,
588 insn.startLocation, insn.displacementOffset,
589 insn.displacementSize, mcInst, Dis))
590 mcInst.addOperand(displacement);
Chris Lattner55595fb2010-07-13 04:23:55 +0000591 mcInst.addOperand(segmentReg);
Sean Callanan010b3732010-04-02 21:23:51 +0000592 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000593}
594
595/// translateRM - Translates an operand stored in the R/M (and possibly SIB)
596/// byte of an instruction to LLVM form, and appends it to an MCInst.
597///
598/// @param mcInst - The MCInst to append to.
599/// @param operand - The operand, as stored in the descriptor table.
600/// @param insn - The instruction to extract Mod, R/M, and SIB fields
601/// from.
Sean Callanan010b3732010-04-02 21:23:51 +0000602/// @return - 0 on success; nonzero otherwise
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +0000603static bool translateRM(MCInst &mcInst, const OperandSpecifier &operand,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000604 InternalInstruction &insn, const MCDisassembler *Dis) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000605 switch (operand.type) {
606 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000607 debug("Unexpected type for a R/M operand");
608 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000609 case TYPE_R8:
610 case TYPE_R16:
611 case TYPE_R32:
612 case TYPE_R64:
613 case TYPE_Rv:
614 case TYPE_MM:
615 case TYPE_MM32:
616 case TYPE_MM64:
617 case TYPE_XMM:
618 case TYPE_XMM32:
619 case TYPE_XMM64:
620 case TYPE_XMM128:
Sean Callananc3fd5232011-03-15 01:23:15 +0000621 case TYPE_XMM256:
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000622 case TYPE_XMM512:
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000623 case TYPE_VK1:
624 case TYPE_VK8:
625 case TYPE_VK16:
Sean Callanan04cc3072009-12-19 02:59:52 +0000626 case TYPE_DEBUGREG:
Sean Callanane7e1cf92010-05-06 20:59:00 +0000627 case TYPE_CONTROLREG:
Sean Callanan010b3732010-04-02 21:23:51 +0000628 return translateRMRegister(mcInst, insn);
Sean Callanan04cc3072009-12-19 02:59:52 +0000629 case TYPE_M:
630 case TYPE_M8:
631 case TYPE_M16:
632 case TYPE_M32:
633 case TYPE_M64:
634 case TYPE_M128:
Sean Callananc3fd5232011-03-15 01:23:15 +0000635 case TYPE_M256:
Sean Callanan04cc3072009-12-19 02:59:52 +0000636 case TYPE_M512:
637 case TYPE_Mv:
638 case TYPE_M32FP:
639 case TYPE_M64FP:
640 case TYPE_M80FP:
641 case TYPE_M16INT:
642 case TYPE_M32INT:
643 case TYPE_M64INT:
644 case TYPE_M1616:
645 case TYPE_M1632:
646 case TYPE_M1664:
Sean Callanan36eab802009-12-22 21:12:55 +0000647 case TYPE_LEA:
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000648 return translateRMMemory(mcInst, insn, Dis);
Sean Callanan04cc3072009-12-19 02:59:52 +0000649 }
650}
651
652/// translateFPRegister - Translates a stack position on the FPU stack to its
653/// LLVM form, and appends it to an MCInst.
654///
655/// @param mcInst - The MCInst to append to.
656/// @param stackPos - The stack position to translate.
Craig Topper91551182014-01-01 15:29:32 +0000657static void translateFPRegister(MCInst &mcInst,
658 uint8_t stackPos) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000659 mcInst.addOperand(MCOperand::CreateReg(X86::ST0 + stackPos));
660}
661
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000662/// translateMaskRegister - Translates a 3-bit mask register number to
663/// LLVM form, and appends it to an MCInst.
664///
665/// @param mcInst - The MCInst to append to.
666/// @param maskRegNum - Number of mask register from 0 to 7.
667/// @return - false on success; true otherwise.
668static bool translateMaskRegister(MCInst &mcInst,
669 uint8_t maskRegNum) {
670 if (maskRegNum >= 8) {
671 debug("Invalid mask register number");
672 return true;
673 }
674
675 mcInst.addOperand(MCOperand::CreateReg(X86::K0 + maskRegNum));
676 return false;
677}
678
Sean Callanan04cc3072009-12-19 02:59:52 +0000679/// translateOperand - Translates an operand stored in an internal instruction
680/// to LLVM's format and appends it to an MCInst.
681///
682/// @param mcInst - The MCInst to append to.
683/// @param operand - The operand, as stored in the descriptor table.
684/// @param insn - The internal instruction.
Sean Callanan010b3732010-04-02 21:23:51 +0000685/// @return - false on success; true otherwise.
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +0000686static bool translateOperand(MCInst &mcInst, const OperandSpecifier &operand,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000687 InternalInstruction &insn,
688 const MCDisassembler *Dis) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000689 switch (operand.encoding) {
690 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000691 debug("Unhandled operand encoding during translation");
692 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000693 case ENCODING_REG:
694 translateRegister(mcInst, insn.reg);
Sean Callanan010b3732010-04-02 21:23:51 +0000695 return false;
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000696 case ENCODING_WRITEMASK:
697 return translateMaskRegister(mcInst, insn.writemask);
Sean Callanan04cc3072009-12-19 02:59:52 +0000698 case ENCODING_RM:
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000699 return translateRM(mcInst, operand, insn, Dis);
Sean Callanan04cc3072009-12-19 02:59:52 +0000700 case ENCODING_CB:
701 case ENCODING_CW:
702 case ENCODING_CD:
703 case ENCODING_CP:
704 case ENCODING_CO:
705 case ENCODING_CT:
Sean Callanan010b3732010-04-02 21:23:51 +0000706 debug("Translation of code offsets isn't supported.");
707 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000708 case ENCODING_IB:
709 case ENCODING_IW:
710 case ENCODING_ID:
711 case ENCODING_IO:
712 case ENCODING_Iv:
713 case ENCODING_Ia:
Sean Callanan4cd930f2010-05-05 22:47:27 +0000714 translateImmediate(mcInst,
715 insn.immediates[insn.numImmediatesTranslated++],
716 operand,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000717 insn,
718 Dis);
Sean Callanan010b3732010-04-02 21:23:51 +0000719 return false;
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000720 case ENCODING_SI:
721 return translateSrcIndex(mcInst, insn);
Sean Callanan04cc3072009-12-19 02:59:52 +0000722 case ENCODING_RB:
723 case ENCODING_RW:
724 case ENCODING_RD:
725 case ENCODING_RO:
Craig Topper91551182014-01-01 15:29:32 +0000726 case ENCODING_Rv:
Sean Callanan04cc3072009-12-19 02:59:52 +0000727 translateRegister(mcInst, insn.opcodeRegister);
Sean Callanan010b3732010-04-02 21:23:51 +0000728 return false;
Craig Topper623b0d62014-01-01 14:22:37 +0000729 case ENCODING_FP:
Craig Topper91551182014-01-01 15:29:32 +0000730 translateFPRegister(mcInst, insn.modRM & 7);
Sean Callanan010b3732010-04-02 21:23:51 +0000731 return false;
Sean Callananc3fd5232011-03-15 01:23:15 +0000732 case ENCODING_VVVV:
733 translateRegister(mcInst, insn.vvvv);
734 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000735 case ENCODING_DUP:
Craig Topperb8aec082012-08-01 07:39:18 +0000736 return translateOperand(mcInst, insn.operands[operand.type - TYPE_DUP0],
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000737 insn, Dis);
Sean Callanan04cc3072009-12-19 02:59:52 +0000738 }
739}
740
741/// translateInstruction - Translates an internal instruction and all its
742/// operands to an MCInst.
743///
744/// @param mcInst - The MCInst to populate with the instruction's data.
745/// @param insn - The internal instruction.
Sean Callanan010b3732010-04-02 21:23:51 +0000746/// @return - false on success; true otherwise.
747static bool translateInstruction(MCInst &mcInst,
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000748 InternalInstruction &insn,
749 const MCDisassembler *Dis) {
Sean Callanan010b3732010-04-02 21:23:51 +0000750 if (!insn.spec) {
751 debug("Instruction has no specification");
752 return true;
753 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000754
755 mcInst.setOpcode(insn.instructionID);
Kevin Enderby35fd7922013-06-20 22:32:18 +0000756 // If when reading the prefix bytes we determined the overlapping 0xf2 or 0xf3
757 // prefix bytes should be disassembled as xrelease and xacquire then set the
758 // opcode to those instead of the rep and repne opcodes.
759 if (insn.xAcquireRelease) {
760 if(mcInst.getOpcode() == X86::REP_PREFIX)
761 mcInst.setOpcode(X86::XRELEASE_PREFIX);
762 else if(mcInst.getOpcode() == X86::REPNE_PREFIX)
763 mcInst.setOpcode(X86::XACQUIRE_PREFIX);
764 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000765
766 int index;
767
768 insn.numImmediatesTranslated = 0;
769
770 for (index = 0; index < X86_MAX_OPERANDS; ++index) {
Craig Topperb8aec082012-08-01 07:39:18 +0000771 if (insn.operands[index].encoding != ENCODING_NONE) {
772 if (translateOperand(mcInst, insn.operands[index], insn, Dis)) {
Sean Callanan010b3732010-04-02 21:23:51 +0000773 return true;
774 }
775 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000776 }
Sean Callanan010b3732010-04-02 21:23:51 +0000777
778 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000779}
Daniel Dunbar900f2ce2009-11-25 06:53:08 +0000780
David Woodhouse7dd21822014-01-20 12:02:31 +0000781static MCDisassembler *createX86Disassembler(const Target &T,
782 const MCSubtargetInfo &STI) {
783 return new X86Disassembler::X86GenericDisassembler(STI,
Benjamin Kramer478e8de2012-02-11 14:50:54 +0000784 T.createMCInstrInfo());
Daniel Dunbar900f2ce2009-11-25 06:53:08 +0000785}
786
787extern "C" void LLVMInitializeX86Disassembler() {
788 // Register the disassembler.
789 TargetRegistry::RegisterMCDisassembler(TheX86_32Target,
David Woodhouse7dd21822014-01-20 12:02:31 +0000790 createX86Disassembler);
Daniel Dunbar900f2ce2009-11-25 06:53:08 +0000791 TargetRegistry::RegisterMCDisassembler(TheX86_64Target,
David Woodhouse7dd21822014-01-20 12:02:31 +0000792 createX86Disassembler);
Daniel Dunbar900f2ce2009-11-25 06:53:08 +0000793}