Jia Liu | b22310f | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | //===-- X86Disassembler.cpp - Disassembler for x86 and x86_64 -------------===// |
Daniel Dunbar | 900f2ce | 2009-11-25 06:53:08 +0000 | [diff] [blame] | 2 | // |
| 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 Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 9 | // |
| 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 Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCContext.h" |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCDisassembler.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCExpr.h" |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCInst.h" |
Benjamin Kramer | 478e8de | 2012-02-11 14:50:54 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCInstrInfo.h" |
James Molloy | 4c493e8 | 2011-09-07 17:24:38 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCSubtargetInfo.h" |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 26 | #include "llvm/Support/MemoryObject.h" |
Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 27 | #include "llvm/Support/TargetRegistry.h" |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Sean Callanan | 5c8f4cd | 2009-12-22 01:11:26 +0000 | [diff] [blame] | 29 | |
Evan Cheng | d9997ac | 2011-06-27 18:32:37 +0000 | [diff] [blame] | 30 | #define GET_REGINFO_ENUM |
| 31 | #include "X86GenRegisterInfo.inc" |
Kevin Enderby | 5b03f72 | 2011-09-02 20:01:23 +0000 | [diff] [blame] | 32 | #define GET_INSTRINFO_ENUM |
| 33 | #include "X86GenInstrInfo.inc" |
David Woodhouse | 7dd2182 | 2014-01-20 12:02:31 +0000 | [diff] [blame] | 34 | #define GET_SUBTARGETINFO_ENUM |
| 35 | #include "X86GenSubtargetInfo.inc" |
Sean Callanan | 5c8f4cd | 2009-12-22 01:11:26 +0000 | [diff] [blame] | 36 | |
Daniel Dunbar | 900f2ce | 2009-11-25 06:53:08 +0000 | [diff] [blame] | 37 | using namespace llvm; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 38 | using namespace llvm::X86Disassembler; |
| 39 | |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 40 | void x86DisassemblerDebug(const char *file, |
| 41 | unsigned line, |
| 42 | const char *s) { |
| 43 | dbgs() << file << ":" << line << ": " << s; |
| 44 | } |
| 45 | |
Roman Divacky | 6792380 | 2012-09-05 21:17:34 +0000 | [diff] [blame] | 46 | const char *x86DisassemblerGetInstrName(unsigned Opcode, const void *mii) { |
Benjamin Kramer | 478e8de | 2012-02-11 14:50:54 +0000 | [diff] [blame] | 47 | const MCInstrInfo *MII = static_cast<const MCInstrInfo *>(mii); |
| 48 | return MII->getName(Opcode); |
| 49 | } |
| 50 | |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 51 | #define debug(s) DEBUG(x86DisassemblerDebug(__FILE__, __LINE__, s)); |
| 52 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 53 | namespace 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. |
| 58 | namespace 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 Callanan | 5c8f4cd | 2009-12-22 01:11:26 +0000 | [diff] [blame] | 69 | extern Target TheX86_32Target, TheX86_64Target; |
| 70 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 73 | static bool translateInstruction(MCInst &target, |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 74 | InternalInstruction &source, |
| 75 | const MCDisassembler *Dis); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 76 | |
Lang Hames | 0563ca1 | 2014-04-13 04:09:16 +0000 | [diff] [blame] | 77 | X86GenericDisassembler::X86GenericDisassembler( |
| 78 | const MCSubtargetInfo &STI, |
Lang Hames | a1bc0f5 | 2014-04-15 04:40:56 +0000 | [diff] [blame^] | 79 | MCContext &Ctx, |
Lang Hames | 0563ca1 | 2014-04-13 04:09:16 +0000 | [diff] [blame] | 80 | std::unique_ptr<const MCInstrInfo> MII) |
Lang Hames | a1bc0f5 | 2014-04-15 04:40:56 +0000 | [diff] [blame^] | 81 | : MCDisassembler(STI, Ctx), MII(std::move(MII)) { |
David Woodhouse | 7dd2182 | 2014-01-20 12:02:31 +0000 | [diff] [blame] | 82 | switch (STI.getFeatureBits() & |
| 83 | (X86::Mode16Bit | X86::Mode32Bit | X86::Mode64Bit)) { |
| 84 | case X86::Mode16Bit: |
| 85 | fMode = MODE_16BIT; |
| 86 | break; |
| 87 | case X86::Mode32Bit: |
| 88 | fMode = MODE_32BIT; |
| 89 | break; |
| 90 | case X86::Mode64Bit: |
| 91 | fMode = MODE_64BIT; |
| 92 | break; |
| 93 | default: |
| 94 | llvm_unreachable("Invalid CPU mode"); |
| 95 | } |
| 96 | } |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 97 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 98 | /// regionReader - a callback function that wraps the readByte method from |
| 99 | /// MemoryObject. |
| 100 | /// |
| 101 | /// @param arg - The generic callback parameter. In this case, this should |
| 102 | /// be a pointer to a MemoryObject. |
| 103 | /// @param byte - A pointer to the byte to be read. |
| 104 | /// @param address - The address to be read. |
Roman Divacky | 6792380 | 2012-09-05 21:17:34 +0000 | [diff] [blame] | 105 | static int regionReader(const void* arg, uint8_t* byte, uint64_t address) { |
| 106 | const MemoryObject* region = static_cast<const MemoryObject*>(arg); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 107 | return region->readByte(address, byte); |
| 108 | } |
| 109 | |
| 110 | /// logger - a callback function that wraps the operator<< method from |
| 111 | /// raw_ostream. |
| 112 | /// |
| 113 | /// @param arg - The generic callback parameter. This should be a pointe |
| 114 | /// to a raw_ostream. |
| 115 | /// @param log - A string to be logged. logger() adds a newline. |
| 116 | static void logger(void* arg, const char* log) { |
| 117 | if (!arg) |
| 118 | return; |
| 119 | |
| 120 | raw_ostream &vStream = *(static_cast<raw_ostream*>(arg)); |
| 121 | vStream << log << "\n"; |
| 122 | } |
| 123 | |
| 124 | // |
| 125 | // Public interface for the disassembler |
| 126 | // |
| 127 | |
Owen Anderson | a4043c4 | 2011-08-17 17:44:15 +0000 | [diff] [blame] | 128 | MCDisassembler::DecodeStatus |
| 129 | X86GenericDisassembler::getInstruction(MCInst &instr, |
| 130 | uint64_t &size, |
Derek Schuff | 56b662c | 2012-02-29 01:09:06 +0000 | [diff] [blame] | 131 | const MemoryObject ®ion, |
Owen Anderson | a4043c4 | 2011-08-17 17:44:15 +0000 | [diff] [blame] | 132 | uint64_t address, |
Owen Anderson | a0c3b97 | 2011-09-15 23:38:46 +0000 | [diff] [blame] | 133 | raw_ostream &vStream, |
| 134 | raw_ostream &cStream) const { |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 135 | CommentStream = &cStream; |
| 136 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 137 | InternalInstruction internalInstr; |
Benjamin Kramer | e5e189f | 2011-09-21 21:47:35 +0000 | [diff] [blame] | 138 | |
| 139 | dlog_t loggerFn = logger; |
| 140 | if (&vStream == &nulls()) |
| 141 | loggerFn = 0; // Disable logging completely if it's going to nulls(). |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 142 | |
| 143 | int ret = decodeInstruction(&internalInstr, |
| 144 | regionReader, |
Roman Divacky | 6792380 | 2012-09-05 21:17:34 +0000 | [diff] [blame] | 145 | (const void*)®ion, |
Benjamin Kramer | e5e189f | 2011-09-21 21:47:35 +0000 | [diff] [blame] | 146 | loggerFn, |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 147 | (void*)&vStream, |
Lang Hames | 0563ca1 | 2014-04-13 04:09:16 +0000 | [diff] [blame] | 148 | (const void*)MII.get(), |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 149 | address, |
| 150 | fMode); |
| 151 | |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 152 | if (ret) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 153 | size = internalInstr.readerCursor - address; |
Owen Anderson | a4043c4 | 2011-08-17 17:44:15 +0000 | [diff] [blame] | 154 | return Fail; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 155 | } |
| 156 | else { |
| 157 | size = internalInstr.length; |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 158 | return (!translateInstruction(instr, internalInstr, this)) ? |
| 159 | Success : Fail; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | // |
| 164 | // Private code that translates from struct InternalInstructions to MCInsts. |
| 165 | // |
| 166 | |
| 167 | /// translateRegister - Translates an internal register to the appropriate LLVM |
| 168 | /// register, and appends it as an operand to an MCInst. |
| 169 | /// |
| 170 | /// @param mcInst - The MCInst to append to. |
| 171 | /// @param reg - The Reg to append. |
| 172 | static void translateRegister(MCInst &mcInst, Reg reg) { |
| 173 | #define ENTRY(x) X86::x, |
| 174 | uint8_t llvmRegnums[] = { |
| 175 | ALL_REGS |
| 176 | 0 |
| 177 | }; |
| 178 | #undef ENTRY |
| 179 | |
| 180 | uint8_t llvmRegnum = llvmRegnums[reg]; |
| 181 | mcInst.addOperand(MCOperand::CreateReg(llvmRegnum)); |
| 182 | } |
| 183 | |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 184 | /// tryAddingSymbolicOperand - trys to add a symbolic operand in place of the |
| 185 | /// immediate Value in the MCInst. |
| 186 | /// |
| 187 | /// @param Value - The immediate Value, has had any PC adjustment made by |
| 188 | /// the caller. |
| 189 | /// @param isBranch - If the instruction is a branch instruction |
| 190 | /// @param Address - The starting address of the instruction |
| 191 | /// @param Offset - The byte offset to this immediate in the instruction |
| 192 | /// @param Width - The byte width of this immediate in the instruction |
| 193 | /// |
| 194 | /// If the getOpInfo() function was set when setupForSymbolicDisassembly() was |
| 195 | /// called then that function is called to get any symbolic information for the |
| 196 | /// immediate in the instruction using the Address, Offset and Width. If that |
| 197 | /// returns non-zero then the symbolic information it returns is used to create |
| 198 | /// an MCExpr and that is added as an operand to the MCInst. If getOpInfo() |
| 199 | /// returns zero and isBranch is true then a symbol look up for immediate Value |
| 200 | /// is done and if a symbol is found an MCExpr is created with that, else |
| 201 | /// an MCExpr with the immediate Value is created. This function returns true |
| 202 | /// if it adds an operand to the MCInst and false otherwise. |
| 203 | static bool tryAddingSymbolicOperand(int64_t Value, bool isBranch, |
| 204 | uint64_t Address, uint64_t Offset, |
| 205 | uint64_t Width, MCInst &MI, |
| 206 | const MCDisassembler *Dis) { |
Ahmed Bougacha | ad1084d | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 207 | return Dis->tryAddingSymbolicOperand(MI, Value, Address, isBranch, |
| 208 | Offset, Width); |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Kevin Enderby | b119c08 | 2012-02-29 22:58:34 +0000 | [diff] [blame] | 211 | /// tryAddingPcLoadReferenceComment - trys to add a comment as to what is being |
| 212 | /// referenced by a load instruction with the base register that is the rip. |
| 213 | /// These can often be addresses in a literal pool. The Address of the |
| 214 | /// instruction and its immediate Value are used to determine the address |
| 215 | /// being referenced in the literal pool entry. The SymbolLookUp call back will |
| 216 | /// return a pointer to a literal 'C' string if the referenced address is an |
| 217 | /// address into a section with 'C' string literals. |
| 218 | static void tryAddingPcLoadReferenceComment(uint64_t Address, uint64_t Value, |
| 219 | const void *Decoder) { |
| 220 | const MCDisassembler *Dis = static_cast<const MCDisassembler*>(Decoder); |
Ahmed Bougacha | ad1084d | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 221 | Dis->tryAddingPcLoadReferenceComment(Value, Address); |
Kevin Enderby | b119c08 | 2012-02-29 22:58:34 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Craig Topper | 35da3d1 | 2014-01-16 07:36:58 +0000 | [diff] [blame] | 224 | static const uint8_t segmentRegnums[SEG_OVERRIDE_max] = { |
| 225 | 0, // SEG_OVERRIDE_NONE |
| 226 | X86::CS, |
| 227 | X86::SS, |
| 228 | X86::DS, |
| 229 | X86::ES, |
| 230 | X86::FS, |
| 231 | X86::GS |
| 232 | }; |
| 233 | |
David Woodhouse | 2ef8d9c | 2014-01-22 15:08:08 +0000 | [diff] [blame] | 234 | /// translateSrcIndex - Appends a source index operand to an MCInst. |
| 235 | /// |
| 236 | /// @param mcInst - The MCInst to append to. |
David Woodhouse | 2ef8d9c | 2014-01-22 15:08:08 +0000 | [diff] [blame] | 237 | /// @param insn - The internal instruction. |
| 238 | static bool translateSrcIndex(MCInst &mcInst, InternalInstruction &insn) { |
| 239 | unsigned baseRegNo; |
| 240 | |
| 241 | if (insn.mode == MODE_64BIT) |
| 242 | baseRegNo = insn.prefixPresent[0x67] ? X86::ESI : X86::RSI; |
| 243 | else if (insn.mode == MODE_32BIT) |
| 244 | baseRegNo = insn.prefixPresent[0x67] ? X86::SI : X86::ESI; |
David Woodhouse | fee418c | 2014-01-22 15:31:29 +0000 | [diff] [blame] | 245 | else { |
| 246 | assert(insn.mode == MODE_16BIT); |
David Woodhouse | 2ef8d9c | 2014-01-22 15:08:08 +0000 | [diff] [blame] | 247 | baseRegNo = insn.prefixPresent[0x67] ? X86::ESI : X86::SI; |
David Woodhouse | fee418c | 2014-01-22 15:31:29 +0000 | [diff] [blame] | 248 | } |
David Woodhouse | 2ef8d9c | 2014-01-22 15:08:08 +0000 | [diff] [blame] | 249 | MCOperand baseReg = MCOperand::CreateReg(baseRegNo); |
| 250 | mcInst.addOperand(baseReg); |
| 251 | |
| 252 | MCOperand segmentReg; |
| 253 | segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]); |
| 254 | mcInst.addOperand(segmentReg); |
| 255 | return false; |
| 256 | } |
| 257 | |
David Woodhouse | b33c2ef | 2014-01-22 15:08:21 +0000 | [diff] [blame] | 258 | /// translateDstIndex - Appends a destination index operand to an MCInst. |
| 259 | /// |
| 260 | /// @param mcInst - The MCInst to append to. |
David Woodhouse | b33c2ef | 2014-01-22 15:08:21 +0000 | [diff] [blame] | 261 | /// @param insn - The internal instruction. |
| 262 | |
| 263 | static bool translateDstIndex(MCInst &mcInst, InternalInstruction &insn) { |
| 264 | unsigned baseRegNo; |
| 265 | |
| 266 | if (insn.mode == MODE_64BIT) |
| 267 | baseRegNo = insn.prefixPresent[0x67] ? X86::EDI : X86::RDI; |
| 268 | else if (insn.mode == MODE_32BIT) |
| 269 | baseRegNo = insn.prefixPresent[0x67] ? X86::DI : X86::EDI; |
David Woodhouse | fee418c | 2014-01-22 15:31:29 +0000 | [diff] [blame] | 270 | else { |
| 271 | assert(insn.mode == MODE_16BIT); |
David Woodhouse | b33c2ef | 2014-01-22 15:08:21 +0000 | [diff] [blame] | 272 | baseRegNo = insn.prefixPresent[0x67] ? X86::EDI : X86::DI; |
David Woodhouse | fee418c | 2014-01-22 15:31:29 +0000 | [diff] [blame] | 273 | } |
David Woodhouse | b33c2ef | 2014-01-22 15:08:21 +0000 | [diff] [blame] | 274 | MCOperand baseReg = MCOperand::CreateReg(baseRegNo); |
| 275 | mcInst.addOperand(baseReg); |
| 276 | return false; |
| 277 | } |
| 278 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 279 | /// translateImmediate - Appends an immediate operand to an MCInst. |
| 280 | /// |
| 281 | /// @param mcInst - The MCInst to append to. |
| 282 | /// @param immediate - The immediate value to append. |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 283 | /// @param operand - The operand, as stored in the descriptor table. |
| 284 | /// @param insn - The internal instruction. |
Benjamin Kramer | de0a4fb | 2010-10-23 09:10:44 +0000 | [diff] [blame] | 285 | static void translateImmediate(MCInst &mcInst, uint64_t immediate, |
| 286 | const OperandSpecifier &operand, |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 287 | InternalInstruction &insn, |
| 288 | const MCDisassembler *Dis) { |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 289 | // Sign-extend the immediate if necessary. |
| 290 | |
Craig Topper | 6dedbae | 2012-03-04 02:16:41 +0000 | [diff] [blame] | 291 | OperandType type = (OperandType)operand.type; |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 292 | |
Kevin Enderby | ec4bd31 | 2012-04-18 23:12:11 +0000 | [diff] [blame] | 293 | bool isBranch = false; |
| 294 | uint64_t pcrel = 0; |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 295 | if (type == TYPE_RELv) { |
Kevin Enderby | ec4bd31 | 2012-04-18 23:12:11 +0000 | [diff] [blame] | 296 | isBranch = true; |
| 297 | pcrel = insn.startLocation + |
Kevin Enderby | 216ac31 | 2012-07-24 21:40:01 +0000 | [diff] [blame] | 298 | insn.immediateOffset + insn.immediateSize; |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 299 | switch (insn.displacementSize) { |
| 300 | default: |
| 301 | break; |
Sean Callanan | 5e8603d | 2011-02-21 21:55:05 +0000 | [diff] [blame] | 302 | case 1: |
Craig Topper | 1885417 | 2013-08-25 22:23:38 +0000 | [diff] [blame] | 303 | if(immediate & 0x80) |
| 304 | immediate |= ~(0xffull); |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 305 | break; |
Sean Callanan | 5e8603d | 2011-02-21 21:55:05 +0000 | [diff] [blame] | 306 | case 2: |
Craig Topper | 1885417 | 2013-08-25 22:23:38 +0000 | [diff] [blame] | 307 | if(immediate & 0x8000) |
| 308 | immediate |= ~(0xffffull); |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 309 | break; |
Sean Callanan | 5e8603d | 2011-02-21 21:55:05 +0000 | [diff] [blame] | 310 | case 4: |
Craig Topper | 1885417 | 2013-08-25 22:23:38 +0000 | [diff] [blame] | 311 | if(immediate & 0x80000000) |
| 312 | immediate |= ~(0xffffffffull); |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 313 | break; |
Sean Callanan | 5e8603d | 2011-02-21 21:55:05 +0000 | [diff] [blame] | 314 | case 8: |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 315 | break; |
| 316 | } |
| 317 | } |
Kevin Enderby | 5b03f72 | 2011-09-02 20:01:23 +0000 | [diff] [blame] | 318 | // By default sign-extend all X86 immediates based on their encoding. |
| 319 | else if (type == TYPE_IMM8 || type == TYPE_IMM16 || type == TYPE_IMM32 || |
| 320 | type == TYPE_IMM64) { |
| 321 | uint32_t Opcode = mcInst.getOpcode(); |
| 322 | switch (operand.encoding) { |
| 323 | default: |
| 324 | break; |
| 325 | case ENCODING_IB: |
| 326 | // Special case those X86 instructions that use the imm8 as a set of |
| 327 | // bits, bit count, etc. and are not sign-extend. |
| 328 | if (Opcode != X86::BLENDPSrri && Opcode != X86::BLENDPDrri && |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 329 | Opcode != X86::PBLENDWrri && Opcode != X86::MPSADBWrri && |
| 330 | Opcode != X86::DPPSrri && Opcode != X86::DPPDrri && |
| 331 | Opcode != X86::INSERTPSrr && Opcode != X86::VBLENDPSYrri && |
| 332 | Opcode != X86::VBLENDPSYrmi && Opcode != X86::VBLENDPDYrri && |
| 333 | Opcode != X86::VBLENDPDYrmi && Opcode != X86::VPBLENDWrri && |
| 334 | Opcode != X86::VMPSADBWrri && Opcode != X86::VDPPSYrri && |
| 335 | Opcode != X86::VDPPSYrmi && Opcode != X86::VDPPDrri && |
| 336 | Opcode != X86::VINSERTPSrr) |
Craig Topper | 1885417 | 2013-08-25 22:23:38 +0000 | [diff] [blame] | 337 | if(immediate & 0x80) |
| 338 | immediate |= ~(0xffull); |
Kevin Enderby | 5b03f72 | 2011-09-02 20:01:23 +0000 | [diff] [blame] | 339 | break; |
| 340 | case ENCODING_IW: |
Craig Topper | 1885417 | 2013-08-25 22:23:38 +0000 | [diff] [blame] | 341 | if(immediate & 0x8000) |
| 342 | immediate |= ~(0xffffull); |
Kevin Enderby | 5b03f72 | 2011-09-02 20:01:23 +0000 | [diff] [blame] | 343 | break; |
| 344 | case ENCODING_ID: |
Craig Topper | 1885417 | 2013-08-25 22:23:38 +0000 | [diff] [blame] | 345 | if(immediate & 0x80000000) |
| 346 | immediate |= ~(0xffffffffull); |
Kevin Enderby | 5b03f72 | 2011-09-02 20:01:23 +0000 | [diff] [blame] | 347 | break; |
| 348 | case ENCODING_IO: |
Kevin Enderby | 5b03f72 | 2011-09-02 20:01:23 +0000 | [diff] [blame] | 349 | break; |
| 350 | } |
| 351 | } |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 352 | |
| 353 | switch (type) { |
Craig Topper | c30fdbc | 2012-08-31 15:40:30 +0000 | [diff] [blame] | 354 | case TYPE_XMM32: |
| 355 | case TYPE_XMM64: |
Craig Topper | 96e00e5 | 2011-09-14 05:55:28 +0000 | [diff] [blame] | 356 | case TYPE_XMM128: |
| 357 | mcInst.addOperand(MCOperand::CreateReg(X86::XMM0 + (immediate >> 4))); |
| 358 | return; |
| 359 | case TYPE_XMM256: |
| 360 | mcInst.addOperand(MCOperand::CreateReg(X86::YMM0 + (immediate >> 4))); |
| 361 | return; |
Elena Demikhovsky | 003e7d7 | 2013-07-28 08:28:38 +0000 | [diff] [blame] | 362 | case TYPE_XMM512: |
| 363 | mcInst.addOperand(MCOperand::CreateReg(X86::ZMM0 + (immediate >> 4))); |
| 364 | return; |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 365 | case TYPE_REL8: |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 366 | isBranch = true; |
| 367 | pcrel = insn.startLocation + insn.immediateOffset + insn.immediateSize; |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 368 | if(immediate & 0x80) |
| 369 | immediate |= ~(0xffull); |
| 370 | break; |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 371 | case TYPE_REL32: |
| 372 | case TYPE_REL64: |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 373 | isBranch = true; |
| 374 | pcrel = insn.startLocation + insn.immediateOffset + insn.immediateSize; |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 375 | if(immediate & 0x80000000) |
| 376 | immediate |= ~(0xffffffffull); |
| 377 | break; |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 378 | default: |
| 379 | // operand is 64 bits wide. Do nothing. |
| 380 | break; |
| 381 | } |
Craig Topper | 092e2fe | 2013-08-24 19:50:11 +0000 | [diff] [blame] | 382 | |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 383 | if(!tryAddingSymbolicOperand(immediate + pcrel, isBranch, insn.startLocation, |
| 384 | insn.immediateOffset, insn.immediateSize, |
| 385 | mcInst, Dis)) |
| 386 | mcInst.addOperand(MCOperand::CreateImm(immediate)); |
Craig Topper | 35da3d1 | 2014-01-16 07:36:58 +0000 | [diff] [blame] | 387 | |
| 388 | if (type == TYPE_MOFFS8 || type == TYPE_MOFFS16 || |
| 389 | type == TYPE_MOFFS32 || type == TYPE_MOFFS64) { |
| 390 | MCOperand segmentReg; |
| 391 | segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]); |
| 392 | mcInst.addOperand(segmentReg); |
| 393 | } |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /// translateRMRegister - Translates a register stored in the R/M field of the |
| 397 | /// ModR/M byte to its LLVM equivalent and appends it to an MCInst. |
| 398 | /// @param mcInst - The MCInst to append to. |
| 399 | /// @param insn - The internal instruction to extract the R/M field |
| 400 | /// from. |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 401 | /// @return - 0 on success; -1 otherwise |
| 402 | static bool translateRMRegister(MCInst &mcInst, |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 403 | InternalInstruction &insn) { |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 404 | if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) { |
| 405 | debug("A R/M register operand may not have a SIB byte"); |
| 406 | return true; |
| 407 | } |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 408 | |
| 409 | switch (insn.eaBase) { |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 410 | default: |
| 411 | debug("Unexpected EA base register"); |
| 412 | return true; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 413 | case EA_BASE_NONE: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 414 | debug("EA_BASE_NONE for ModR/M base"); |
| 415 | return true; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 416 | #define ENTRY(x) case EA_BASE_##x: |
| 417 | ALL_EA_BASES |
| 418 | #undef ENTRY |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 419 | debug("A R/M register operand may not have a base; " |
| 420 | "the operand must be a register."); |
| 421 | return true; |
| 422 | #define ENTRY(x) \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 423 | case EA_REG_##x: \ |
| 424 | mcInst.addOperand(MCOperand::CreateReg(X86::x)); break; |
| 425 | ALL_REGS |
| 426 | #undef ENTRY |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 427 | } |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 428 | |
| 429 | return false; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /// translateRMMemory - Translates a memory operand stored in the Mod and R/M |
| 433 | /// fields of an internal instruction (and possibly its SIB byte) to a memory |
| 434 | /// operand in LLVM's format, and appends it to an MCInst. |
| 435 | /// |
| 436 | /// @param mcInst - The MCInst to append to. |
| 437 | /// @param insn - The instruction to extract Mod, R/M, and SIB fields |
| 438 | /// from. |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 439 | /// @return - 0 on success; nonzero otherwise |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 440 | static bool translateRMMemory(MCInst &mcInst, InternalInstruction &insn, |
| 441 | const MCDisassembler *Dis) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 442 | // Addresses in an MCInst are represented as five operands: |
| 443 | // 1. basereg (register) The R/M base, or (if there is a SIB) the |
| 444 | // SIB base |
| 445 | // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified |
| 446 | // scale amount |
| 447 | // 3. indexreg (register) x86_registerNONE, or (if there is a SIB) |
| 448 | // the index (which is multiplied by the |
| 449 | // scale amount) |
| 450 | // 4. displacement (immediate) 0, or the displacement if there is one |
| 451 | // 5. segmentreg (register) x86_registerNONE for now, but could be set |
| 452 | // if we have segment overrides |
| 453 | |
| 454 | MCOperand baseReg; |
| 455 | MCOperand scaleAmount; |
| 456 | MCOperand indexReg; |
| 457 | MCOperand displacement; |
| 458 | MCOperand segmentReg; |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 459 | uint64_t pcrel = 0; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 460 | |
| 461 | if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) { |
| 462 | if (insn.sibBase != SIB_BASE_NONE) { |
| 463 | switch (insn.sibBase) { |
| 464 | default: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 465 | debug("Unexpected sibBase"); |
| 466 | return true; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 467 | #define ENTRY(x) \ |
Sean Callanan | 36eab80 | 2009-12-22 21:12:55 +0000 | [diff] [blame] | 468 | case SIB_BASE_##x: \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 469 | baseReg = MCOperand::CreateReg(X86::x); break; |
| 470 | ALL_SIB_BASES |
| 471 | #undef ENTRY |
| 472 | } |
| 473 | } else { |
| 474 | baseReg = MCOperand::CreateReg(0); |
| 475 | } |
Manman Ren | a098204 | 2012-06-26 19:47:59 +0000 | [diff] [blame] | 476 | |
| 477 | // Check whether we are handling VSIB addressing mode for GATHER. |
| 478 | // If sibIndex was set to SIB_INDEX_NONE, index offset is 4 and |
| 479 | // we should use SIB_INDEX_XMM4|YMM4 for VSIB. |
| 480 | // I don't see a way to get the correct IndexReg in readSIB: |
| 481 | // We can tell whether it is VSIB or SIB after instruction ID is decoded, |
| 482 | // but instruction ID may not be decoded yet when calling readSIB. |
| 483 | uint32_t Opcode = mcInst.getOpcode(); |
Manman Ren | 98a5bf2 | 2012-06-29 00:54:20 +0000 | [diff] [blame] | 484 | bool IndexIs128 = (Opcode == X86::VGATHERDPDrm || |
| 485 | Opcode == X86::VGATHERDPDYrm || |
| 486 | Opcode == X86::VGATHERQPDrm || |
| 487 | Opcode == X86::VGATHERDPSrm || |
| 488 | Opcode == X86::VGATHERQPSrm || |
| 489 | Opcode == X86::VPGATHERDQrm || |
| 490 | Opcode == X86::VPGATHERDQYrm || |
| 491 | Opcode == X86::VPGATHERQQrm || |
| 492 | Opcode == X86::VPGATHERDDrm || |
| 493 | Opcode == X86::VPGATHERQDrm); |
| 494 | bool IndexIs256 = (Opcode == X86::VGATHERQPDYrm || |
| 495 | Opcode == X86::VGATHERDPSYrm || |
| 496 | Opcode == X86::VGATHERQPSYrm || |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 497 | Opcode == X86::VGATHERDPDZrm || |
| 498 | Opcode == X86::VPGATHERDQZrm || |
Manman Ren | 98a5bf2 | 2012-06-29 00:54:20 +0000 | [diff] [blame] | 499 | Opcode == X86::VPGATHERQQYrm || |
| 500 | Opcode == X86::VPGATHERDDYrm || |
| 501 | Opcode == X86::VPGATHERQDYrm); |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 502 | bool IndexIs512 = (Opcode == X86::VGATHERQPDZrm || |
| 503 | Opcode == X86::VGATHERDPSZrm || |
| 504 | Opcode == X86::VGATHERQPSZrm || |
| 505 | Opcode == X86::VPGATHERQQZrm || |
| 506 | Opcode == X86::VPGATHERDDZrm || |
| 507 | Opcode == X86::VPGATHERQDZrm); |
| 508 | if (IndexIs128 || IndexIs256 || IndexIs512) { |
Manman Ren | a098204 | 2012-06-26 19:47:59 +0000 | [diff] [blame] | 509 | unsigned IndexOffset = insn.sibIndex - |
| 510 | (insn.addressSize == 8 ? SIB_INDEX_RAX:SIB_INDEX_EAX); |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 511 | SIBIndex IndexBase = IndexIs512 ? SIB_INDEX_ZMM0 : |
| 512 | IndexIs256 ? SIB_INDEX_YMM0 : SIB_INDEX_XMM0; |
Manman Ren | a098204 | 2012-06-26 19:47:59 +0000 | [diff] [blame] | 513 | insn.sibIndex = (SIBIndex)(IndexBase + |
| 514 | (insn.sibIndex == SIB_INDEX_NONE ? 4 : IndexOffset)); |
| 515 | } |
| 516 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 517 | if (insn.sibIndex != SIB_INDEX_NONE) { |
| 518 | switch (insn.sibIndex) { |
| 519 | default: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 520 | debug("Unexpected sibIndex"); |
| 521 | return true; |
Sean Callanan | 36eab80 | 2009-12-22 21:12:55 +0000 | [diff] [blame] | 522 | #define ENTRY(x) \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 523 | case SIB_INDEX_##x: \ |
| 524 | indexReg = MCOperand::CreateReg(X86::x); break; |
| 525 | EA_BASES_32BIT |
| 526 | EA_BASES_64BIT |
Manman Ren | a098204 | 2012-06-26 19:47:59 +0000 | [diff] [blame] | 527 | REGS_XMM |
| 528 | REGS_YMM |
Elena Demikhovsky | 003e7d7 | 2013-07-28 08:28:38 +0000 | [diff] [blame] | 529 | REGS_ZMM |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 530 | #undef ENTRY |
| 531 | } |
| 532 | } else { |
| 533 | indexReg = MCOperand::CreateReg(0); |
| 534 | } |
| 535 | |
| 536 | scaleAmount = MCOperand::CreateImm(insn.sibScale); |
| 537 | } else { |
| 538 | switch (insn.eaBase) { |
| 539 | case EA_BASE_NONE: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 540 | if (insn.eaDisplacement == EA_DISP_NONE) { |
| 541 | debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base"); |
| 542 | return true; |
| 543 | } |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 544 | if (insn.mode == MODE_64BIT){ |
| 545 | pcrel = insn.startLocation + |
| 546 | insn.displacementOffset + insn.displacementSize; |
Kevin Enderby | b119c08 | 2012-02-29 22:58:34 +0000 | [diff] [blame] | 547 | tryAddingPcLoadReferenceComment(insn.startLocation + |
| 548 | insn.displacementOffset, |
| 549 | insn.displacement + pcrel, Dis); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 550 | baseReg = MCOperand::CreateReg(X86::RIP); // Section 2.2.1.6 |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 551 | } |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 552 | else |
| 553 | baseReg = MCOperand::CreateReg(0); |
| 554 | |
| 555 | indexReg = MCOperand::CreateReg(0); |
| 556 | break; |
| 557 | case EA_BASE_BX_SI: |
| 558 | baseReg = MCOperand::CreateReg(X86::BX); |
| 559 | indexReg = MCOperand::CreateReg(X86::SI); |
| 560 | break; |
| 561 | case EA_BASE_BX_DI: |
| 562 | baseReg = MCOperand::CreateReg(X86::BX); |
| 563 | indexReg = MCOperand::CreateReg(X86::DI); |
| 564 | break; |
| 565 | case EA_BASE_BP_SI: |
| 566 | baseReg = MCOperand::CreateReg(X86::BP); |
| 567 | indexReg = MCOperand::CreateReg(X86::SI); |
| 568 | break; |
| 569 | case EA_BASE_BP_DI: |
| 570 | baseReg = MCOperand::CreateReg(X86::BP); |
| 571 | indexReg = MCOperand::CreateReg(X86::DI); |
| 572 | break; |
| 573 | default: |
| 574 | indexReg = MCOperand::CreateReg(0); |
| 575 | switch (insn.eaBase) { |
| 576 | default: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 577 | debug("Unexpected eaBase"); |
| 578 | return true; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 579 | // Here, we will use the fill-ins defined above. However, |
| 580 | // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and |
| 581 | // sib and sib64 were handled in the top-level if, so they're only |
| 582 | // placeholders to keep the compiler happy. |
| 583 | #define ENTRY(x) \ |
| 584 | case EA_BASE_##x: \ |
| 585 | baseReg = MCOperand::CreateReg(X86::x); break; |
| 586 | ALL_EA_BASES |
| 587 | #undef ENTRY |
| 588 | #define ENTRY(x) case EA_REG_##x: |
| 589 | ALL_REGS |
| 590 | #undef ENTRY |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 591 | debug("A R/M memory operand may not be a register; " |
| 592 | "the base field must be a base."); |
| 593 | return true; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 594 | } |
| 595 | } |
Sean Callanan | 36eab80 | 2009-12-22 21:12:55 +0000 | [diff] [blame] | 596 | |
| 597 | scaleAmount = MCOperand::CreateImm(1); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | displacement = MCOperand::CreateImm(insn.displacement); |
Craig Topper | 35da3d1 | 2014-01-16 07:36:58 +0000 | [diff] [blame] | 601 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 602 | segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]); |
| 603 | |
| 604 | mcInst.addOperand(baseReg); |
| 605 | mcInst.addOperand(scaleAmount); |
| 606 | mcInst.addOperand(indexReg); |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 607 | if(!tryAddingSymbolicOperand(insn.displacement + pcrel, false, |
| 608 | insn.startLocation, insn.displacementOffset, |
| 609 | insn.displacementSize, mcInst, Dis)) |
| 610 | mcInst.addOperand(displacement); |
Chris Lattner | 55595fb | 2010-07-13 04:23:55 +0000 | [diff] [blame] | 611 | mcInst.addOperand(segmentReg); |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 612 | return false; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | /// translateRM - Translates an operand stored in the R/M (and possibly SIB) |
| 616 | /// byte of an instruction to LLVM form, and appends it to an MCInst. |
| 617 | /// |
| 618 | /// @param mcInst - The MCInst to append to. |
| 619 | /// @param operand - The operand, as stored in the descriptor table. |
| 620 | /// @param insn - The instruction to extract Mod, R/M, and SIB fields |
| 621 | /// from. |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 622 | /// @return - 0 on success; nonzero otherwise |
Benjamin Kramer | de0a4fb | 2010-10-23 09:10:44 +0000 | [diff] [blame] | 623 | static bool translateRM(MCInst &mcInst, const OperandSpecifier &operand, |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 624 | InternalInstruction &insn, const MCDisassembler *Dis) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 625 | switch (operand.type) { |
| 626 | default: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 627 | debug("Unexpected type for a R/M operand"); |
| 628 | return true; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 629 | case TYPE_R8: |
| 630 | case TYPE_R16: |
| 631 | case TYPE_R32: |
| 632 | case TYPE_R64: |
| 633 | case TYPE_Rv: |
| 634 | case TYPE_MM: |
| 635 | case TYPE_MM32: |
| 636 | case TYPE_MM64: |
| 637 | case TYPE_XMM: |
| 638 | case TYPE_XMM32: |
| 639 | case TYPE_XMM64: |
| 640 | case TYPE_XMM128: |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 641 | case TYPE_XMM256: |
Elena Demikhovsky | 003e7d7 | 2013-07-28 08:28:38 +0000 | [diff] [blame] | 642 | case TYPE_XMM512: |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 643 | case TYPE_VK1: |
| 644 | case TYPE_VK8: |
| 645 | case TYPE_VK16: |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 646 | case TYPE_DEBUGREG: |
Sean Callanan | e7e1cf9 | 2010-05-06 20:59:00 +0000 | [diff] [blame] | 647 | case TYPE_CONTROLREG: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 648 | return translateRMRegister(mcInst, insn); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 649 | case TYPE_M: |
| 650 | case TYPE_M8: |
| 651 | case TYPE_M16: |
| 652 | case TYPE_M32: |
| 653 | case TYPE_M64: |
| 654 | case TYPE_M128: |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 655 | case TYPE_M256: |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 656 | case TYPE_M512: |
| 657 | case TYPE_Mv: |
| 658 | case TYPE_M32FP: |
| 659 | case TYPE_M64FP: |
| 660 | case TYPE_M80FP: |
| 661 | case TYPE_M16INT: |
| 662 | case TYPE_M32INT: |
| 663 | case TYPE_M64INT: |
| 664 | case TYPE_M1616: |
| 665 | case TYPE_M1632: |
| 666 | case TYPE_M1664: |
Sean Callanan | 36eab80 | 2009-12-22 21:12:55 +0000 | [diff] [blame] | 667 | case TYPE_LEA: |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 668 | return translateRMMemory(mcInst, insn, Dis); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 669 | } |
| 670 | } |
| 671 | |
| 672 | /// translateFPRegister - Translates a stack position on the FPU stack to its |
| 673 | /// LLVM form, and appends it to an MCInst. |
| 674 | /// |
| 675 | /// @param mcInst - The MCInst to append to. |
| 676 | /// @param stackPos - The stack position to translate. |
Craig Topper | 9155118 | 2014-01-01 15:29:32 +0000 | [diff] [blame] | 677 | static void translateFPRegister(MCInst &mcInst, |
| 678 | uint8_t stackPos) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 679 | mcInst.addOperand(MCOperand::CreateReg(X86::ST0 + stackPos)); |
| 680 | } |
| 681 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 682 | /// translateMaskRegister - Translates a 3-bit mask register number to |
| 683 | /// LLVM form, and appends it to an MCInst. |
| 684 | /// |
| 685 | /// @param mcInst - The MCInst to append to. |
| 686 | /// @param maskRegNum - Number of mask register from 0 to 7. |
| 687 | /// @return - false on success; true otherwise. |
| 688 | static bool translateMaskRegister(MCInst &mcInst, |
| 689 | uint8_t maskRegNum) { |
| 690 | if (maskRegNum >= 8) { |
| 691 | debug("Invalid mask register number"); |
| 692 | return true; |
| 693 | } |
| 694 | |
| 695 | mcInst.addOperand(MCOperand::CreateReg(X86::K0 + maskRegNum)); |
| 696 | return false; |
| 697 | } |
| 698 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 699 | /// translateOperand - Translates an operand stored in an internal instruction |
| 700 | /// to LLVM's format and appends it to an MCInst. |
| 701 | /// |
| 702 | /// @param mcInst - The MCInst to append to. |
| 703 | /// @param operand - The operand, as stored in the descriptor table. |
| 704 | /// @param insn - The internal instruction. |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 705 | /// @return - false on success; true otherwise. |
Benjamin Kramer | de0a4fb | 2010-10-23 09:10:44 +0000 | [diff] [blame] | 706 | static bool translateOperand(MCInst &mcInst, const OperandSpecifier &operand, |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 707 | InternalInstruction &insn, |
| 708 | const MCDisassembler *Dis) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 709 | switch (operand.encoding) { |
| 710 | default: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 711 | debug("Unhandled operand encoding during translation"); |
| 712 | return true; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 713 | case ENCODING_REG: |
| 714 | translateRegister(mcInst, insn.reg); |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 715 | return false; |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 716 | case ENCODING_WRITEMASK: |
| 717 | return translateMaskRegister(mcInst, insn.writemask); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 718 | case ENCODING_RM: |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 719 | return translateRM(mcInst, operand, insn, Dis); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 720 | case ENCODING_CB: |
| 721 | case ENCODING_CW: |
| 722 | case ENCODING_CD: |
| 723 | case ENCODING_CP: |
| 724 | case ENCODING_CO: |
| 725 | case ENCODING_CT: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 726 | debug("Translation of code offsets isn't supported."); |
| 727 | return true; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 728 | case ENCODING_IB: |
| 729 | case ENCODING_IW: |
| 730 | case ENCODING_ID: |
| 731 | case ENCODING_IO: |
| 732 | case ENCODING_Iv: |
| 733 | case ENCODING_Ia: |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 734 | translateImmediate(mcInst, |
| 735 | insn.immediates[insn.numImmediatesTranslated++], |
| 736 | operand, |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 737 | insn, |
| 738 | Dis); |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 739 | return false; |
David Woodhouse | 2ef8d9c | 2014-01-22 15:08:08 +0000 | [diff] [blame] | 740 | case ENCODING_SI: |
| 741 | return translateSrcIndex(mcInst, insn); |
David Woodhouse | b33c2ef | 2014-01-22 15:08:21 +0000 | [diff] [blame] | 742 | case ENCODING_DI: |
| 743 | return translateDstIndex(mcInst, insn); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 744 | case ENCODING_RB: |
| 745 | case ENCODING_RW: |
| 746 | case ENCODING_RD: |
| 747 | case ENCODING_RO: |
Craig Topper | 9155118 | 2014-01-01 15:29:32 +0000 | [diff] [blame] | 748 | case ENCODING_Rv: |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 749 | translateRegister(mcInst, insn.opcodeRegister); |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 750 | return false; |
Craig Topper | 623b0d6 | 2014-01-01 14:22:37 +0000 | [diff] [blame] | 751 | case ENCODING_FP: |
Craig Topper | 9155118 | 2014-01-01 15:29:32 +0000 | [diff] [blame] | 752 | translateFPRegister(mcInst, insn.modRM & 7); |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 753 | return false; |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 754 | case ENCODING_VVVV: |
| 755 | translateRegister(mcInst, insn.vvvv); |
| 756 | return false; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 757 | case ENCODING_DUP: |
Craig Topper | b8aec08 | 2012-08-01 07:39:18 +0000 | [diff] [blame] | 758 | return translateOperand(mcInst, insn.operands[operand.type - TYPE_DUP0], |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 759 | insn, Dis); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
| 763 | /// translateInstruction - Translates an internal instruction and all its |
| 764 | /// operands to an MCInst. |
| 765 | /// |
| 766 | /// @param mcInst - The MCInst to populate with the instruction's data. |
| 767 | /// @param insn - The internal instruction. |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 768 | /// @return - false on success; true otherwise. |
| 769 | static bool translateInstruction(MCInst &mcInst, |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 770 | InternalInstruction &insn, |
| 771 | const MCDisassembler *Dis) { |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 772 | if (!insn.spec) { |
| 773 | debug("Instruction has no specification"); |
| 774 | return true; |
| 775 | } |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 776 | |
| 777 | mcInst.setOpcode(insn.instructionID); |
Kevin Enderby | 35fd792 | 2013-06-20 22:32:18 +0000 | [diff] [blame] | 778 | // If when reading the prefix bytes we determined the overlapping 0xf2 or 0xf3 |
| 779 | // prefix bytes should be disassembled as xrelease and xacquire then set the |
| 780 | // opcode to those instead of the rep and repne opcodes. |
| 781 | if (insn.xAcquireRelease) { |
| 782 | if(mcInst.getOpcode() == X86::REP_PREFIX) |
| 783 | mcInst.setOpcode(X86::XRELEASE_PREFIX); |
| 784 | else if(mcInst.getOpcode() == X86::REPNE_PREFIX) |
| 785 | mcInst.setOpcode(X86::XACQUIRE_PREFIX); |
| 786 | } |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 787 | |
| 788 | int index; |
| 789 | |
| 790 | insn.numImmediatesTranslated = 0; |
| 791 | |
| 792 | for (index = 0; index < X86_MAX_OPERANDS; ++index) { |
Craig Topper | b8aec08 | 2012-08-01 07:39:18 +0000 | [diff] [blame] | 793 | if (insn.operands[index].encoding != ENCODING_NONE) { |
| 794 | if (translateOperand(mcInst, insn.operands[index], insn, Dis)) { |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 795 | return true; |
| 796 | } |
| 797 | } |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 798 | } |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 799 | |
| 800 | return false; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 801 | } |
Daniel Dunbar | 900f2ce | 2009-11-25 06:53:08 +0000 | [diff] [blame] | 802 | |
David Woodhouse | 7dd2182 | 2014-01-20 12:02:31 +0000 | [diff] [blame] | 803 | static MCDisassembler *createX86Disassembler(const Target &T, |
Lang Hames | a1bc0f5 | 2014-04-15 04:40:56 +0000 | [diff] [blame^] | 804 | const MCSubtargetInfo &STI, |
| 805 | MCContext &Ctx) { |
Lang Hames | 0563ca1 | 2014-04-13 04:09:16 +0000 | [diff] [blame] | 806 | std::unique_ptr<const MCInstrInfo> MII(T.createMCInstrInfo()); |
Lang Hames | a1bc0f5 | 2014-04-15 04:40:56 +0000 | [diff] [blame^] | 807 | return new X86Disassembler::X86GenericDisassembler(STI, Ctx, std::move(MII)); |
Daniel Dunbar | 900f2ce | 2009-11-25 06:53:08 +0000 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | extern "C" void LLVMInitializeX86Disassembler() { |
| 811 | // Register the disassembler. |
| 812 | TargetRegistry::RegisterMCDisassembler(TheX86_32Target, |
David Woodhouse | 7dd2182 | 2014-01-20 12:02:31 +0000 | [diff] [blame] | 813 | createX86Disassembler); |
Daniel Dunbar | 900f2ce | 2009-11-25 06:53:08 +0000 | [diff] [blame] | 814 | TargetRegistry::RegisterMCDisassembler(TheX86_64Target, |
David Woodhouse | 7dd2182 | 2014-01-20 12:02:31 +0000 | [diff] [blame] | 815 | createX86Disassembler); |
Daniel Dunbar | 900f2ce | 2009-11-25 06:53:08 +0000 | [diff] [blame] | 816 | } |