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. |
Craig Topper | b805723 | 2016-04-29 04:22:28 +0000 | [diff] [blame] | 13 | // |
| 14 | // |
| 15 | // The X86 disassembler is a table-driven disassembler for the 16-, 32-, and |
| 16 | // 64-bit X86 instruction sets. The main decode sequence for an assembly |
| 17 | // instruction in this disassembler is: |
| 18 | // |
| 19 | // 1. Read the prefix bytes and determine the attributes of the instruction. |
| 20 | // These attributes, recorded in enum attributeBits |
| 21 | // (X86DisassemblerDecoderCommon.h), form a bitmask. The table CONTEXTS_SYM |
| 22 | // provides a mapping from bitmasks to contexts, which are represented by |
| 23 | // enum InstructionContext (ibid.). |
| 24 | // |
| 25 | // 2. Read the opcode, and determine what kind of opcode it is. The |
| 26 | // disassembler distinguishes four kinds of opcodes, which are enumerated in |
| 27 | // OpcodeType (X86DisassemblerDecoderCommon.h): one-byte (0xnn), two-byte |
| 28 | // (0x0f 0xnn), three-byte-38 (0x0f 0x38 0xnn), or three-byte-3a |
| 29 | // (0x0f 0x3a 0xnn). Mandatory prefixes are treated as part of the context. |
| 30 | // |
| 31 | // 3. Depending on the opcode type, look in one of four ClassDecision structures |
| 32 | // (X86DisassemblerDecoderCommon.h). Use the opcode class to determine which |
| 33 | // OpcodeDecision (ibid.) to look the opcode in. Look up the opcode, to get |
| 34 | // a ModRMDecision (ibid.). |
| 35 | // |
| 36 | // 4. Some instructions, such as escape opcodes or extended opcodes, or even |
| 37 | // instructions that have ModRM*Reg / ModRM*Mem forms in LLVM, need the |
| 38 | // ModR/M byte to complete decode. The ModRMDecision's type is an entry from |
| 39 | // ModRMDecisionType (X86DisassemblerDecoderCommon.h) that indicates if the |
| 40 | // ModR/M byte is required and how to interpret it. |
| 41 | // |
| 42 | // 5. After resolving the ModRMDecision, the disassembler has a unique ID |
| 43 | // of type InstrUID (X86DisassemblerDecoderCommon.h). Looking this ID up in |
| 44 | // INSTRUCTIONS_SYM yields the name of the instruction and the encodings and |
| 45 | // meanings of its operands. |
| 46 | // |
| 47 | // 6. For each operand, its encoding is an entry from OperandEncoding |
| 48 | // (X86DisassemblerDecoderCommon.h) and its type is an entry from |
| 49 | // OperandType (ibid.). The encoding indicates how to read it from the |
| 50 | // instruction; the type indicates how to interpret the value once it has |
| 51 | // been read. For example, a register operand could be stored in the R/M |
| 52 | // field of the ModR/M byte, the REG field of the ModR/M byte, or added to |
| 53 | // the main opcode. This is orthogonal from its meaning (an GPR or an XMM |
| 54 | // register, for instance). Given this information, the operands can be |
| 55 | // extracted and interpreted. |
| 56 | // |
| 57 | // 7. As the last step, the disassembler translates the instruction information |
| 58 | // and operands into a format understandable by the client - in this case, an |
| 59 | // MCInst for use by the MC infrastructure. |
| 60 | // |
| 61 | // The disassembler is broken broadly into two parts: the table emitter that |
| 62 | // emits the instruction decode tables discussed above during compilation, and |
| 63 | // the disassembler itself. The table emitter is documented in more detail in |
| 64 | // utils/TableGen/X86DisassemblerEmitter.h. |
| 65 | // |
| 66 | // X86Disassembler.cpp contains the code responsible for step 7, and for |
| 67 | // invoking the decoder to execute steps 1-6. |
| 68 | // X86DisassemblerDecoderCommon.h contains the definitions needed by both the |
| 69 | // table emitter and the disassembler. |
| 70 | // X86DisassemblerDecoder.h contains the public interface of the decoder, |
| 71 | // factored out into C for possible use by other projects. |
| 72 | // X86DisassemblerDecoder.c contains the source code of the decoder, which is |
| 73 | // responsible for steps 1-6. |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 74 | // |
| 75 | //===----------------------------------------------------------------------===// |
| 76 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 77 | #include "X86DisassemblerDecoder.h" |
Craig Topper | e7c1cd1 | 2016-04-29 04:22:26 +0000 | [diff] [blame] | 78 | #include "MCTargetDesc/X86MCTargetDesc.h" |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 79 | #include "llvm/MC/MCContext.h" |
Benjamin Kramer | f57c197 | 2016-01-26 16:44:37 +0000 | [diff] [blame] | 80 | #include "llvm/MC/MCDisassembler/MCDisassembler.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 81 | #include "llvm/MC/MCExpr.h" |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 82 | #include "llvm/MC/MCInst.h" |
Benjamin Kramer | 478e8de | 2012-02-11 14:50:54 +0000 | [diff] [blame] | 83 | #include "llvm/MC/MCInstrInfo.h" |
James Molloy | 4c493e8 | 2011-09-07 17:24:38 +0000 | [diff] [blame] | 84 | #include "llvm/MC/MCSubtargetInfo.h" |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 85 | #include "llvm/Support/Debug.h" |
Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 86 | #include "llvm/Support/TargetRegistry.h" |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 87 | #include "llvm/Support/raw_ostream.h" |
Sean Callanan | 5c8f4cd | 2009-12-22 01:11:26 +0000 | [diff] [blame] | 88 | |
Chandler Carruth | d174b72 | 2014-04-22 02:03:14 +0000 | [diff] [blame] | 89 | using namespace llvm; |
| 90 | using namespace llvm::X86Disassembler; |
| 91 | |
| 92 | #define DEBUG_TYPE "x86-disassembler" |
| 93 | |
Richard Smith | 89ee75d | 2014-04-20 21:07:34 +0000 | [diff] [blame] | 94 | void llvm::X86Disassembler::Debug(const char *file, unsigned line, |
| 95 | const char *s) { |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 96 | dbgs() << file << ":" << line << ": " << s; |
| 97 | } |
| 98 | |
Mehdi Amini | 36d33fc | 2016-10-01 06:46:33 +0000 | [diff] [blame] | 99 | StringRef llvm::X86Disassembler::GetInstrName(unsigned Opcode, |
Richard Smith | 89ee75d | 2014-04-20 21:07:34 +0000 | [diff] [blame] | 100 | const void *mii) { |
Benjamin Kramer | 478e8de | 2012-02-11 14:50:54 +0000 | [diff] [blame] | 101 | const MCInstrInfo *MII = static_cast<const MCInstrInfo *>(mii); |
| 102 | return MII->getName(Opcode); |
| 103 | } |
| 104 | |
Richard Smith | 89ee75d | 2014-04-20 21:07:34 +0000 | [diff] [blame] | 105 | #define debug(s) DEBUG(Debug(__FILE__, __LINE__, s)); |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 106 | |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 107 | namespace llvm { |
| 108 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 109 | // Fill-ins to make the compiler happy. These constants are never actually |
| 110 | // assigned; they are just filler to make an automatically-generated switch |
| 111 | // statement work. |
| 112 | namespace X86 { |
| 113 | enum { |
| 114 | BX_SI = 500, |
| 115 | BX_DI = 501, |
| 116 | BP_SI = 502, |
| 117 | BP_DI = 503, |
| 118 | sib = 504, |
| 119 | sib64 = 505 |
| 120 | }; |
| 121 | } |
| 122 | |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 123 | } |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 124 | |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 125 | static bool translateInstruction(MCInst &target, |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 126 | InternalInstruction &source, |
| 127 | const MCDisassembler *Dis); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 128 | |
Craig Topper | b805723 | 2016-04-29 04:22:28 +0000 | [diff] [blame] | 129 | namespace { |
| 130 | |
| 131 | /// Generic disassembler for all X86 platforms. All each platform class should |
| 132 | /// have to do is subclass the constructor, and provide a different |
| 133 | /// disassemblerMode value. |
| 134 | class X86GenericDisassembler : public MCDisassembler { |
| 135 | std::unique_ptr<const MCInstrInfo> MII; |
| 136 | public: |
| 137 | X86GenericDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx, |
| 138 | std::unique_ptr<const MCInstrInfo> MII); |
| 139 | public: |
| 140 | DecodeStatus getInstruction(MCInst &instr, uint64_t &size, |
| 141 | ArrayRef<uint8_t> Bytes, uint64_t Address, |
| 142 | raw_ostream &vStream, |
| 143 | raw_ostream &cStream) const override; |
| 144 | |
| 145 | private: |
| 146 | DisassemblerMode fMode; |
| 147 | }; |
| 148 | |
| 149 | } |
| 150 | |
Lang Hames | 0563ca1 | 2014-04-13 04:09:16 +0000 | [diff] [blame] | 151 | X86GenericDisassembler::X86GenericDisassembler( |
| 152 | const MCSubtargetInfo &STI, |
Lang Hames | a1bc0f5 | 2014-04-15 04:40:56 +0000 | [diff] [blame] | 153 | MCContext &Ctx, |
Lang Hames | 0563ca1 | 2014-04-13 04:09:16 +0000 | [diff] [blame] | 154 | std::unique_ptr<const MCInstrInfo> MII) |
Lang Hames | a1bc0f5 | 2014-04-15 04:40:56 +0000 | [diff] [blame] | 155 | : MCDisassembler(STI, Ctx), MII(std::move(MII)) { |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 156 | const FeatureBitset &FB = STI.getFeatureBits(); |
| 157 | if (FB[X86::Mode16Bit]) { |
David Woodhouse | 7dd2182 | 2014-01-20 12:02:31 +0000 | [diff] [blame] | 158 | fMode = MODE_16BIT; |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 159 | return; |
| 160 | } else if (FB[X86::Mode32Bit]) { |
David Woodhouse | 7dd2182 | 2014-01-20 12:02:31 +0000 | [diff] [blame] | 161 | fMode = MODE_32BIT; |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 162 | return; |
| 163 | } else if (FB[X86::Mode64Bit]) { |
David Woodhouse | 7dd2182 | 2014-01-20 12:02:31 +0000 | [diff] [blame] | 164 | fMode = MODE_64BIT; |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 165 | return; |
David Woodhouse | 7dd2182 | 2014-01-20 12:02:31 +0000 | [diff] [blame] | 166 | } |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 167 | |
| 168 | llvm_unreachable("Invalid CPU mode"); |
David Woodhouse | 7dd2182 | 2014-01-20 12:02:31 +0000 | [diff] [blame] | 169 | } |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 170 | |
Benjamin Kramer | 039b104 | 2015-10-28 13:54:36 +0000 | [diff] [blame] | 171 | namespace { |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 172 | struct Region { |
| 173 | ArrayRef<uint8_t> Bytes; |
| 174 | uint64_t Base; |
| 175 | Region(ArrayRef<uint8_t> Bytes, uint64_t Base) : Bytes(Bytes), Base(Base) {} |
| 176 | }; |
Benjamin Kramer | 039b104 | 2015-10-28 13:54:36 +0000 | [diff] [blame] | 177 | } // end anonymous namespace |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 178 | |
| 179 | /// A callback function that wraps the readByte method from Region. |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 180 | /// |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 181 | /// @param Arg - The generic callback parameter. In this case, this should |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 182 | /// be a pointer to a Region. |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 183 | /// @param Byte - A pointer to the byte to be read. |
| 184 | /// @param Address - The address to be read. |
| 185 | static int regionReader(const void *Arg, uint8_t *Byte, uint64_t Address) { |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 186 | auto *R = static_cast<const Region *>(Arg); |
| 187 | ArrayRef<uint8_t> Bytes = R->Bytes; |
| 188 | unsigned Index = Address - R->Base; |
| 189 | if (Bytes.size() <= Index) |
| 190 | return -1; |
| 191 | *Byte = Bytes[Index]; |
| 192 | return 0; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /// logger - a callback function that wraps the operator<< method from |
| 196 | /// raw_ostream. |
| 197 | /// |
| 198 | /// @param arg - The generic callback parameter. This should be a pointe |
| 199 | /// to a raw_ostream. |
| 200 | /// @param log - A string to be logged. logger() adds a newline. |
| 201 | static void logger(void* arg, const char* log) { |
| 202 | if (!arg) |
| 203 | return; |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 204 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 205 | raw_ostream &vStream = *(static_cast<raw_ostream*>(arg)); |
| 206 | vStream << log << "\n"; |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 209 | // |
| 210 | // Public interface for the disassembler |
| 211 | // |
| 212 | |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 213 | MCDisassembler::DecodeStatus X86GenericDisassembler::getInstruction( |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 214 | MCInst &Instr, uint64_t &Size, ArrayRef<uint8_t> Bytes, uint64_t Address, |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 215 | raw_ostream &VStream, raw_ostream &CStream) const { |
| 216 | CommentStream = &CStream; |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 217 | |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 218 | InternalInstruction InternalInstr; |
Benjamin Kramer | e5e189f | 2011-09-21 21:47:35 +0000 | [diff] [blame] | 219 | |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 220 | dlog_t LoggerFn = logger; |
| 221 | if (&VStream == &nulls()) |
| 222 | LoggerFn = nullptr; // Disable logging completely if it's going to nulls(). |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 223 | |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 224 | Region R(Bytes, Address); |
| 225 | |
| 226 | int Ret = decodeInstruction(&InternalInstr, regionReader, (const void *)&R, |
| 227 | LoggerFn, (void *)&VStream, |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 228 | (const void *)MII.get(), Address, fMode); |
| 229 | |
| 230 | if (Ret) { |
| 231 | Size = InternalInstr.readerCursor - Address; |
Owen Anderson | a4043c4 | 2011-08-17 17:44:15 +0000 | [diff] [blame] | 232 | return Fail; |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 233 | } else { |
| 234 | Size = InternalInstr.length; |
| 235 | return (!translateInstruction(Instr, InternalInstr, this)) ? Success : Fail; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
| 239 | // |
| 240 | // Private code that translates from struct InternalInstructions to MCInsts. |
| 241 | // |
| 242 | |
| 243 | /// translateRegister - Translates an internal register to the appropriate LLVM |
| 244 | /// register, and appends it as an operand to an MCInst. |
| 245 | /// |
| 246 | /// @param mcInst - The MCInst to append to. |
| 247 | /// @param reg - The Reg to append. |
| 248 | static void translateRegister(MCInst &mcInst, Reg reg) { |
| 249 | #define ENTRY(x) X86::x, |
| 250 | uint8_t llvmRegnums[] = { |
| 251 | ALL_REGS |
| 252 | 0 |
| 253 | }; |
| 254 | #undef ENTRY |
| 255 | |
| 256 | uint8_t llvmRegnum = llvmRegnums[reg]; |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 257 | mcInst.addOperand(MCOperand::createReg(llvmRegnum)); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 260 | /// tryAddingSymbolicOperand - trys to add a symbolic operand in place of the |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 261 | /// immediate Value in the MCInst. |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 262 | /// |
| 263 | /// @param Value - The immediate Value, has had any PC adjustment made by |
| 264 | /// the caller. |
| 265 | /// @param isBranch - If the instruction is a branch instruction |
| 266 | /// @param Address - The starting address of the instruction |
| 267 | /// @param Offset - The byte offset to this immediate in the instruction |
| 268 | /// @param Width - The byte width of this immediate in the instruction |
| 269 | /// |
| 270 | /// If the getOpInfo() function was set when setupForSymbolicDisassembly() was |
| 271 | /// called then that function is called to get any symbolic information for the |
| 272 | /// immediate in the instruction using the Address, Offset and Width. If that |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 273 | /// returns non-zero then the symbolic information it returns is used to create |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 274 | /// an MCExpr and that is added as an operand to the MCInst. If getOpInfo() |
| 275 | /// returns zero and isBranch is true then a symbol look up for immediate Value |
| 276 | /// is done and if a symbol is found an MCExpr is created with that, else |
| 277 | /// an MCExpr with the immediate Value is created. This function returns true |
| 278 | /// if it adds an operand to the MCInst and false otherwise. |
| 279 | static bool tryAddingSymbolicOperand(int64_t Value, bool isBranch, |
| 280 | uint64_t Address, uint64_t Offset, |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 281 | uint64_t Width, MCInst &MI, |
| 282 | const MCDisassembler *Dis) { |
Ahmed Bougacha | ad1084d | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 283 | return Dis->tryAddingSymbolicOperand(MI, Value, Address, isBranch, |
| 284 | Offset, Width); |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Kevin Enderby | b119c08 | 2012-02-29 22:58:34 +0000 | [diff] [blame] | 287 | /// tryAddingPcLoadReferenceComment - trys to add a comment as to what is being |
| 288 | /// referenced by a load instruction with the base register that is the rip. |
| 289 | /// These can often be addresses in a literal pool. The Address of the |
| 290 | /// instruction and its immediate Value are used to determine the address |
| 291 | /// being referenced in the literal pool entry. The SymbolLookUp call back will |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 292 | /// return a pointer to a literal 'C' string if the referenced address is an |
Kevin Enderby | b119c08 | 2012-02-29 22:58:34 +0000 | [diff] [blame] | 293 | /// address into a section with 'C' string literals. |
| 294 | static void tryAddingPcLoadReferenceComment(uint64_t Address, uint64_t Value, |
| 295 | const void *Decoder) { |
| 296 | const MCDisassembler *Dis = static_cast<const MCDisassembler*>(Decoder); |
Ahmed Bougacha | ad1084d | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 297 | Dis->tryAddingPcLoadReferenceComment(Value, Address); |
Kevin Enderby | b119c08 | 2012-02-29 22:58:34 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Craig Topper | 35da3d1 | 2014-01-16 07:36:58 +0000 | [diff] [blame] | 300 | static const uint8_t segmentRegnums[SEG_OVERRIDE_max] = { |
| 301 | 0, // SEG_OVERRIDE_NONE |
| 302 | X86::CS, |
| 303 | X86::SS, |
| 304 | X86::DS, |
| 305 | X86::ES, |
| 306 | X86::FS, |
| 307 | X86::GS |
| 308 | }; |
| 309 | |
David Woodhouse | 2ef8d9c | 2014-01-22 15:08:08 +0000 | [diff] [blame] | 310 | /// translateSrcIndex - Appends a source index operand to an MCInst. |
| 311 | /// |
| 312 | /// @param mcInst - The MCInst to append to. |
David Woodhouse | 2ef8d9c | 2014-01-22 15:08:08 +0000 | [diff] [blame] | 313 | /// @param insn - The internal instruction. |
| 314 | static bool translateSrcIndex(MCInst &mcInst, InternalInstruction &insn) { |
| 315 | unsigned baseRegNo; |
| 316 | |
| 317 | if (insn.mode == MODE_64BIT) |
| 318 | baseRegNo = insn.prefixPresent[0x67] ? X86::ESI : X86::RSI; |
| 319 | else if (insn.mode == MODE_32BIT) |
| 320 | baseRegNo = insn.prefixPresent[0x67] ? X86::SI : X86::ESI; |
David Woodhouse | fee418c | 2014-01-22 15:31:29 +0000 | [diff] [blame] | 321 | else { |
| 322 | assert(insn.mode == MODE_16BIT); |
David Woodhouse | 2ef8d9c | 2014-01-22 15:08:08 +0000 | [diff] [blame] | 323 | baseRegNo = insn.prefixPresent[0x67] ? X86::ESI : X86::SI; |
David Woodhouse | fee418c | 2014-01-22 15:31:29 +0000 | [diff] [blame] | 324 | } |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 325 | MCOperand baseReg = MCOperand::createReg(baseRegNo); |
David Woodhouse | 2ef8d9c | 2014-01-22 15:08:08 +0000 | [diff] [blame] | 326 | mcInst.addOperand(baseReg); |
| 327 | |
| 328 | MCOperand segmentReg; |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 329 | segmentReg = MCOperand::createReg(segmentRegnums[insn.segmentOverride]); |
David Woodhouse | 2ef8d9c | 2014-01-22 15:08:08 +0000 | [diff] [blame] | 330 | mcInst.addOperand(segmentReg); |
| 331 | return false; |
| 332 | } |
| 333 | |
David Woodhouse | b33c2ef | 2014-01-22 15:08:21 +0000 | [diff] [blame] | 334 | /// translateDstIndex - Appends a destination index operand to an MCInst. |
| 335 | /// |
| 336 | /// @param mcInst - The MCInst to append to. |
David Woodhouse | b33c2ef | 2014-01-22 15:08:21 +0000 | [diff] [blame] | 337 | /// @param insn - The internal instruction. |
| 338 | |
| 339 | static bool translateDstIndex(MCInst &mcInst, InternalInstruction &insn) { |
| 340 | unsigned baseRegNo; |
| 341 | |
| 342 | if (insn.mode == MODE_64BIT) |
| 343 | baseRegNo = insn.prefixPresent[0x67] ? X86::EDI : X86::RDI; |
| 344 | else if (insn.mode == MODE_32BIT) |
| 345 | baseRegNo = insn.prefixPresent[0x67] ? X86::DI : X86::EDI; |
David Woodhouse | fee418c | 2014-01-22 15:31:29 +0000 | [diff] [blame] | 346 | else { |
| 347 | assert(insn.mode == MODE_16BIT); |
David Woodhouse | b33c2ef | 2014-01-22 15:08:21 +0000 | [diff] [blame] | 348 | baseRegNo = insn.prefixPresent[0x67] ? X86::EDI : X86::DI; |
David Woodhouse | fee418c | 2014-01-22 15:31:29 +0000 | [diff] [blame] | 349 | } |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 350 | MCOperand baseReg = MCOperand::createReg(baseRegNo); |
David Woodhouse | b33c2ef | 2014-01-22 15:08:21 +0000 | [diff] [blame] | 351 | mcInst.addOperand(baseReg); |
| 352 | return false; |
| 353 | } |
| 354 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 355 | /// translateImmediate - Appends an immediate operand to an MCInst. |
| 356 | /// |
| 357 | /// @param mcInst - The MCInst to append to. |
| 358 | /// @param immediate - The immediate value to append. |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 359 | /// @param operand - The operand, as stored in the descriptor table. |
| 360 | /// @param insn - The internal instruction. |
Benjamin Kramer | de0a4fb | 2010-10-23 09:10:44 +0000 | [diff] [blame] | 361 | static void translateImmediate(MCInst &mcInst, uint64_t immediate, |
| 362 | const OperandSpecifier &operand, |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 363 | InternalInstruction &insn, |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 364 | const MCDisassembler *Dis) { |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 365 | // Sign-extend the immediate if necessary. |
| 366 | |
Craig Topper | 6dedbae | 2012-03-04 02:16:41 +0000 | [diff] [blame] | 367 | OperandType type = (OperandType)operand.type; |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 368 | |
Kevin Enderby | ec4bd31 | 2012-04-18 23:12:11 +0000 | [diff] [blame] | 369 | bool isBranch = false; |
| 370 | uint64_t pcrel = 0; |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 371 | if (type == TYPE_RELv) { |
Kevin Enderby | ec4bd31 | 2012-04-18 23:12:11 +0000 | [diff] [blame] | 372 | isBranch = true; |
| 373 | pcrel = insn.startLocation + |
Kevin Enderby | 216ac31 | 2012-07-24 21:40:01 +0000 | [diff] [blame] | 374 | insn.immediateOffset + insn.immediateSize; |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 375 | switch (insn.displacementSize) { |
| 376 | default: |
| 377 | break; |
Sean Callanan | 5e8603d | 2011-02-21 21:55:05 +0000 | [diff] [blame] | 378 | case 1: |
Craig Topper | 1885417 | 2013-08-25 22:23:38 +0000 | [diff] [blame] | 379 | if(immediate & 0x80) |
| 380 | immediate |= ~(0xffull); |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 381 | break; |
Sean Callanan | 5e8603d | 2011-02-21 21:55:05 +0000 | [diff] [blame] | 382 | case 2: |
Craig Topper | 1885417 | 2013-08-25 22:23:38 +0000 | [diff] [blame] | 383 | if(immediate & 0x8000) |
| 384 | immediate |= ~(0xffffull); |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 385 | break; |
Sean Callanan | 5e8603d | 2011-02-21 21:55:05 +0000 | [diff] [blame] | 386 | case 4: |
Craig Topper | 1885417 | 2013-08-25 22:23:38 +0000 | [diff] [blame] | 387 | if(immediate & 0x80000000) |
| 388 | immediate |= ~(0xffffffffull); |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 389 | break; |
Sean Callanan | 5e8603d | 2011-02-21 21:55:05 +0000 | [diff] [blame] | 390 | case 8: |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 391 | break; |
| 392 | } |
| 393 | } |
Kevin Enderby | 5b03f72 | 2011-09-02 20:01:23 +0000 | [diff] [blame] | 394 | // By default sign-extend all X86 immediates based on their encoding. |
| 395 | else if (type == TYPE_IMM8 || type == TYPE_IMM16 || type == TYPE_IMM32 || |
Elena Demikhovsky | 8ac0bf9 | 2014-04-23 07:21:04 +0000 | [diff] [blame] | 396 | type == TYPE_IMM64 || type == TYPE_IMMv) { |
Kevin Enderby | 5b03f72 | 2011-09-02 20:01:23 +0000 | [diff] [blame] | 397 | switch (operand.encoding) { |
| 398 | default: |
| 399 | break; |
| 400 | case ENCODING_IB: |
Craig Topper | 620b50c | 2015-01-21 08:15:54 +0000 | [diff] [blame] | 401 | if(immediate & 0x80) |
| 402 | immediate |= ~(0xffull); |
Kevin Enderby | 5b03f72 | 2011-09-02 20:01:23 +0000 | [diff] [blame] | 403 | break; |
| 404 | case ENCODING_IW: |
Craig Topper | 1885417 | 2013-08-25 22:23:38 +0000 | [diff] [blame] | 405 | if(immediate & 0x8000) |
| 406 | immediate |= ~(0xffffull); |
Kevin Enderby | 5b03f72 | 2011-09-02 20:01:23 +0000 | [diff] [blame] | 407 | break; |
| 408 | case ENCODING_ID: |
Craig Topper | 1885417 | 2013-08-25 22:23:38 +0000 | [diff] [blame] | 409 | if(immediate & 0x80000000) |
| 410 | immediate |= ~(0xffffffffull); |
Kevin Enderby | 5b03f72 | 2011-09-02 20:01:23 +0000 | [diff] [blame] | 411 | break; |
| 412 | case ENCODING_IO: |
Kevin Enderby | 5b03f72 | 2011-09-02 20:01:23 +0000 | [diff] [blame] | 413 | break; |
| 414 | } |
Craig Topper | ee9eef2 | 2014-12-26 06:36:28 +0000 | [diff] [blame] | 415 | } else if (type == TYPE_IMM3) { |
| 416 | // Check for immediates that printSSECC can't handle. |
| 417 | if (immediate >= 8) { |
| 418 | unsigned NewOpc; |
| 419 | switch (mcInst.getOpcode()) { |
| 420 | default: llvm_unreachable("unexpected opcode"); |
Craig Topper | 916708f | 2015-02-13 07:42:25 +0000 | [diff] [blame] | 421 | case X86::CMPPDrmi: NewOpc = X86::CMPPDrmi_alt; break; |
| 422 | case X86::CMPPDrri: NewOpc = X86::CMPPDrri_alt; break; |
| 423 | case X86::CMPPSrmi: NewOpc = X86::CMPPSrmi_alt; break; |
| 424 | case X86::CMPPSrri: NewOpc = X86::CMPPSrri_alt; break; |
| 425 | case X86::CMPSDrm: NewOpc = X86::CMPSDrm_alt; break; |
| 426 | case X86::CMPSDrr: NewOpc = X86::CMPSDrr_alt; break; |
| 427 | case X86::CMPSSrm: NewOpc = X86::CMPSSrm_alt; break; |
| 428 | case X86::CMPSSrr: NewOpc = X86::CMPSSrr_alt; break; |
| 429 | case X86::VPCOMBri: NewOpc = X86::VPCOMBri_alt; break; |
| 430 | case X86::VPCOMBmi: NewOpc = X86::VPCOMBmi_alt; break; |
| 431 | case X86::VPCOMWri: NewOpc = X86::VPCOMWri_alt; break; |
| 432 | case X86::VPCOMWmi: NewOpc = X86::VPCOMWmi_alt; break; |
| 433 | case X86::VPCOMDri: NewOpc = X86::VPCOMDri_alt; break; |
| 434 | case X86::VPCOMDmi: NewOpc = X86::VPCOMDmi_alt; break; |
| 435 | case X86::VPCOMQri: NewOpc = X86::VPCOMQri_alt; break; |
| 436 | case X86::VPCOMQmi: NewOpc = X86::VPCOMQmi_alt; break; |
| 437 | case X86::VPCOMUBri: NewOpc = X86::VPCOMUBri_alt; break; |
| 438 | case X86::VPCOMUBmi: NewOpc = X86::VPCOMUBmi_alt; break; |
| 439 | case X86::VPCOMUWri: NewOpc = X86::VPCOMUWri_alt; break; |
| 440 | case X86::VPCOMUWmi: NewOpc = X86::VPCOMUWmi_alt; break; |
| 441 | case X86::VPCOMUDri: NewOpc = X86::VPCOMUDri_alt; break; |
| 442 | case X86::VPCOMUDmi: NewOpc = X86::VPCOMUDmi_alt; break; |
| 443 | case X86::VPCOMUQri: NewOpc = X86::VPCOMUQri_alt; break; |
| 444 | case X86::VPCOMUQmi: NewOpc = X86::VPCOMUQmi_alt; break; |
Craig Topper | ee9eef2 | 2014-12-26 06:36:28 +0000 | [diff] [blame] | 445 | } |
| 446 | // Switch opcode to the one that doesn't get special printing. |
| 447 | mcInst.setOpcode(NewOpc); |
| 448 | } |
| 449 | } else if (type == TYPE_IMM5) { |
| 450 | // Check for immediates that printAVXCC can't handle. |
| 451 | if (immediate >= 32) { |
| 452 | unsigned NewOpc; |
| 453 | switch (mcInst.getOpcode()) { |
| 454 | default: llvm_unreachable("unexpected opcode"); |
Craig Topper | 09b27e7 | 2015-03-02 00:22:29 +0000 | [diff] [blame] | 455 | case X86::VCMPPDrmi: NewOpc = X86::VCMPPDrmi_alt; break; |
| 456 | case X86::VCMPPDrri: NewOpc = X86::VCMPPDrri_alt; break; |
| 457 | case X86::VCMPPSrmi: NewOpc = X86::VCMPPSrmi_alt; break; |
| 458 | case X86::VCMPPSrri: NewOpc = X86::VCMPPSrri_alt; break; |
| 459 | case X86::VCMPSDrm: NewOpc = X86::VCMPSDrm_alt; break; |
| 460 | case X86::VCMPSDrr: NewOpc = X86::VCMPSDrr_alt; break; |
| 461 | case X86::VCMPSSrm: NewOpc = X86::VCMPSSrm_alt; break; |
| 462 | case X86::VCMPSSrr: NewOpc = X86::VCMPSSrr_alt; break; |
| 463 | case X86::VCMPPDYrmi: NewOpc = X86::VCMPPDYrmi_alt; break; |
| 464 | case X86::VCMPPDYrri: NewOpc = X86::VCMPPDYrri_alt; break; |
| 465 | case X86::VCMPPSYrmi: NewOpc = X86::VCMPPSYrmi_alt; break; |
| 466 | case X86::VCMPPSYrri: NewOpc = X86::VCMPPSYrri_alt; break; |
| 467 | case X86::VCMPPDZrmi: NewOpc = X86::VCMPPDZrmi_alt; break; |
| 468 | case X86::VCMPPDZrri: NewOpc = X86::VCMPPDZrri_alt; break; |
| 469 | case X86::VCMPPDZrrib: NewOpc = X86::VCMPPDZrrib_alt; break; |
| 470 | case X86::VCMPPSZrmi: NewOpc = X86::VCMPPSZrmi_alt; break; |
| 471 | case X86::VCMPPSZrri: NewOpc = X86::VCMPPSZrri_alt; break; |
| 472 | case X86::VCMPPSZrrib: NewOpc = X86::VCMPPSZrrib_alt; break; |
| 473 | case X86::VCMPSDZrm: NewOpc = X86::VCMPSDZrmi_alt; break; |
| 474 | case X86::VCMPSDZrr: NewOpc = X86::VCMPSDZrri_alt; break; |
| 475 | case X86::VCMPSSZrm: NewOpc = X86::VCMPSSZrmi_alt; break; |
| 476 | case X86::VCMPSSZrr: NewOpc = X86::VCMPSSZrri_alt; break; |
Craig Topper | ee9eef2 | 2014-12-26 06:36:28 +0000 | [diff] [blame] | 477 | } |
| 478 | // Switch opcode to the one that doesn't get special printing. |
| 479 | mcInst.setOpcode(NewOpc); |
| 480 | } |
Craig Topper | 7d3c6d3 | 2015-01-28 10:09:56 +0000 | [diff] [blame] | 481 | } else if (type == TYPE_AVX512ICC) { |
| 482 | if (immediate >= 8 || ((immediate & 0x3) == 3)) { |
| 483 | unsigned NewOpc; |
| 484 | switch (mcInst.getOpcode()) { |
| 485 | default: llvm_unreachable("unexpected opcode"); |
| 486 | case X86::VPCMPBZ128rmi: NewOpc = X86::VPCMPBZ128rmi_alt; break; |
| 487 | case X86::VPCMPBZ128rmik: NewOpc = X86::VPCMPBZ128rmik_alt; break; |
| 488 | case X86::VPCMPBZ128rri: NewOpc = X86::VPCMPBZ128rri_alt; break; |
| 489 | case X86::VPCMPBZ128rrik: NewOpc = X86::VPCMPBZ128rrik_alt; break; |
| 490 | case X86::VPCMPBZ256rmi: NewOpc = X86::VPCMPBZ256rmi_alt; break; |
| 491 | case X86::VPCMPBZ256rmik: NewOpc = X86::VPCMPBZ256rmik_alt; break; |
| 492 | case X86::VPCMPBZ256rri: NewOpc = X86::VPCMPBZ256rri_alt; break; |
| 493 | case X86::VPCMPBZ256rrik: NewOpc = X86::VPCMPBZ256rrik_alt; break; |
| 494 | case X86::VPCMPBZrmi: NewOpc = X86::VPCMPBZrmi_alt; break; |
| 495 | case X86::VPCMPBZrmik: NewOpc = X86::VPCMPBZrmik_alt; break; |
| 496 | case X86::VPCMPBZrri: NewOpc = X86::VPCMPBZrri_alt; break; |
| 497 | case X86::VPCMPBZrrik: NewOpc = X86::VPCMPBZrrik_alt; break; |
| 498 | case X86::VPCMPDZ128rmi: NewOpc = X86::VPCMPDZ128rmi_alt; break; |
| 499 | case X86::VPCMPDZ128rmib: NewOpc = X86::VPCMPDZ128rmib_alt; break; |
| 500 | case X86::VPCMPDZ128rmibk: NewOpc = X86::VPCMPDZ128rmibk_alt; break; |
| 501 | case X86::VPCMPDZ128rmik: NewOpc = X86::VPCMPDZ128rmik_alt; break; |
| 502 | case X86::VPCMPDZ128rri: NewOpc = X86::VPCMPDZ128rri_alt; break; |
| 503 | case X86::VPCMPDZ128rrik: NewOpc = X86::VPCMPDZ128rrik_alt; break; |
| 504 | case X86::VPCMPDZ256rmi: NewOpc = X86::VPCMPDZ256rmi_alt; break; |
| 505 | case X86::VPCMPDZ256rmib: NewOpc = X86::VPCMPDZ256rmib_alt; break; |
| 506 | case X86::VPCMPDZ256rmibk: NewOpc = X86::VPCMPDZ256rmibk_alt; break; |
| 507 | case X86::VPCMPDZ256rmik: NewOpc = X86::VPCMPDZ256rmik_alt; break; |
| 508 | case X86::VPCMPDZ256rri: NewOpc = X86::VPCMPDZ256rri_alt; break; |
| 509 | case X86::VPCMPDZ256rrik: NewOpc = X86::VPCMPDZ256rrik_alt; break; |
| 510 | case X86::VPCMPDZrmi: NewOpc = X86::VPCMPDZrmi_alt; break; |
| 511 | case X86::VPCMPDZrmib: NewOpc = X86::VPCMPDZrmib_alt; break; |
| 512 | case X86::VPCMPDZrmibk: NewOpc = X86::VPCMPDZrmibk_alt; break; |
| 513 | case X86::VPCMPDZrmik: NewOpc = X86::VPCMPDZrmik_alt; break; |
| 514 | case X86::VPCMPDZrri: NewOpc = X86::VPCMPDZrri_alt; break; |
| 515 | case X86::VPCMPDZrrik: NewOpc = X86::VPCMPDZrrik_alt; break; |
| 516 | case X86::VPCMPQZ128rmi: NewOpc = X86::VPCMPQZ128rmi_alt; break; |
| 517 | case X86::VPCMPQZ128rmib: NewOpc = X86::VPCMPQZ128rmib_alt; break; |
| 518 | case X86::VPCMPQZ128rmibk: NewOpc = X86::VPCMPQZ128rmibk_alt; break; |
| 519 | case X86::VPCMPQZ128rmik: NewOpc = X86::VPCMPQZ128rmik_alt; break; |
| 520 | case X86::VPCMPQZ128rri: NewOpc = X86::VPCMPQZ128rri_alt; break; |
| 521 | case X86::VPCMPQZ128rrik: NewOpc = X86::VPCMPQZ128rrik_alt; break; |
| 522 | case X86::VPCMPQZ256rmi: NewOpc = X86::VPCMPQZ256rmi_alt; break; |
| 523 | case X86::VPCMPQZ256rmib: NewOpc = X86::VPCMPQZ256rmib_alt; break; |
| 524 | case X86::VPCMPQZ256rmibk: NewOpc = X86::VPCMPQZ256rmibk_alt; break; |
| 525 | case X86::VPCMPQZ256rmik: NewOpc = X86::VPCMPQZ256rmik_alt; break; |
| 526 | case X86::VPCMPQZ256rri: NewOpc = X86::VPCMPQZ256rri_alt; break; |
| 527 | case X86::VPCMPQZ256rrik: NewOpc = X86::VPCMPQZ256rrik_alt; break; |
| 528 | case X86::VPCMPQZrmi: NewOpc = X86::VPCMPQZrmi_alt; break; |
| 529 | case X86::VPCMPQZrmib: NewOpc = X86::VPCMPQZrmib_alt; break; |
| 530 | case X86::VPCMPQZrmibk: NewOpc = X86::VPCMPQZrmibk_alt; break; |
| 531 | case X86::VPCMPQZrmik: NewOpc = X86::VPCMPQZrmik_alt; break; |
| 532 | case X86::VPCMPQZrri: NewOpc = X86::VPCMPQZrri_alt; break; |
| 533 | case X86::VPCMPQZrrik: NewOpc = X86::VPCMPQZrrik_alt; break; |
| 534 | case X86::VPCMPUBZ128rmi: NewOpc = X86::VPCMPUBZ128rmi_alt; break; |
| 535 | case X86::VPCMPUBZ128rmik: NewOpc = X86::VPCMPUBZ128rmik_alt; break; |
| 536 | case X86::VPCMPUBZ128rri: NewOpc = X86::VPCMPUBZ128rri_alt; break; |
| 537 | case X86::VPCMPUBZ128rrik: NewOpc = X86::VPCMPUBZ128rrik_alt; break; |
| 538 | case X86::VPCMPUBZ256rmi: NewOpc = X86::VPCMPUBZ256rmi_alt; break; |
| 539 | case X86::VPCMPUBZ256rmik: NewOpc = X86::VPCMPUBZ256rmik_alt; break; |
| 540 | case X86::VPCMPUBZ256rri: NewOpc = X86::VPCMPUBZ256rri_alt; break; |
| 541 | case X86::VPCMPUBZ256rrik: NewOpc = X86::VPCMPUBZ256rrik_alt; break; |
| 542 | case X86::VPCMPUBZrmi: NewOpc = X86::VPCMPUBZrmi_alt; break; |
| 543 | case X86::VPCMPUBZrmik: NewOpc = X86::VPCMPUBZrmik_alt; break; |
| 544 | case X86::VPCMPUBZrri: NewOpc = X86::VPCMPUBZrri_alt; break; |
| 545 | case X86::VPCMPUBZrrik: NewOpc = X86::VPCMPUBZrrik_alt; break; |
| 546 | case X86::VPCMPUDZ128rmi: NewOpc = X86::VPCMPUDZ128rmi_alt; break; |
| 547 | case X86::VPCMPUDZ128rmib: NewOpc = X86::VPCMPUDZ128rmib_alt; break; |
| 548 | case X86::VPCMPUDZ128rmibk: NewOpc = X86::VPCMPUDZ128rmibk_alt; break; |
| 549 | case X86::VPCMPUDZ128rmik: NewOpc = X86::VPCMPUDZ128rmik_alt; break; |
| 550 | case X86::VPCMPUDZ128rri: NewOpc = X86::VPCMPUDZ128rri_alt; break; |
| 551 | case X86::VPCMPUDZ128rrik: NewOpc = X86::VPCMPUDZ128rrik_alt; break; |
| 552 | case X86::VPCMPUDZ256rmi: NewOpc = X86::VPCMPUDZ256rmi_alt; break; |
| 553 | case X86::VPCMPUDZ256rmib: NewOpc = X86::VPCMPUDZ256rmib_alt; break; |
| 554 | case X86::VPCMPUDZ256rmibk: NewOpc = X86::VPCMPUDZ256rmibk_alt; break; |
| 555 | case X86::VPCMPUDZ256rmik: NewOpc = X86::VPCMPUDZ256rmik_alt; break; |
| 556 | case X86::VPCMPUDZ256rri: NewOpc = X86::VPCMPUDZ256rri_alt; break; |
| 557 | case X86::VPCMPUDZ256rrik: NewOpc = X86::VPCMPUDZ256rrik_alt; break; |
| 558 | case X86::VPCMPUDZrmi: NewOpc = X86::VPCMPUDZrmi_alt; break; |
| 559 | case X86::VPCMPUDZrmib: NewOpc = X86::VPCMPUDZrmib_alt; break; |
| 560 | case X86::VPCMPUDZrmibk: NewOpc = X86::VPCMPUDZrmibk_alt; break; |
| 561 | case X86::VPCMPUDZrmik: NewOpc = X86::VPCMPUDZrmik_alt; break; |
| 562 | case X86::VPCMPUDZrri: NewOpc = X86::VPCMPUDZrri_alt; break; |
| 563 | case X86::VPCMPUDZrrik: NewOpc = X86::VPCMPUDZrrik_alt; break; |
| 564 | case X86::VPCMPUQZ128rmi: NewOpc = X86::VPCMPUQZ128rmi_alt; break; |
| 565 | case X86::VPCMPUQZ128rmib: NewOpc = X86::VPCMPUQZ128rmib_alt; break; |
| 566 | case X86::VPCMPUQZ128rmibk: NewOpc = X86::VPCMPUQZ128rmibk_alt; break; |
| 567 | case X86::VPCMPUQZ128rmik: NewOpc = X86::VPCMPUQZ128rmik_alt; break; |
| 568 | case X86::VPCMPUQZ128rri: NewOpc = X86::VPCMPUQZ128rri_alt; break; |
| 569 | case X86::VPCMPUQZ128rrik: NewOpc = X86::VPCMPUQZ128rrik_alt; break; |
| 570 | case X86::VPCMPUQZ256rmi: NewOpc = X86::VPCMPUQZ256rmi_alt; break; |
| 571 | case X86::VPCMPUQZ256rmib: NewOpc = X86::VPCMPUQZ256rmib_alt; break; |
| 572 | case X86::VPCMPUQZ256rmibk: NewOpc = X86::VPCMPUQZ256rmibk_alt; break; |
| 573 | case X86::VPCMPUQZ256rmik: NewOpc = X86::VPCMPUQZ256rmik_alt; break; |
| 574 | case X86::VPCMPUQZ256rri: NewOpc = X86::VPCMPUQZ256rri_alt; break; |
| 575 | case X86::VPCMPUQZ256rrik: NewOpc = X86::VPCMPUQZ256rrik_alt; break; |
| 576 | case X86::VPCMPUQZrmi: NewOpc = X86::VPCMPUQZrmi_alt; break; |
| 577 | case X86::VPCMPUQZrmib: NewOpc = X86::VPCMPUQZrmib_alt; break; |
| 578 | case X86::VPCMPUQZrmibk: NewOpc = X86::VPCMPUQZrmibk_alt; break; |
| 579 | case X86::VPCMPUQZrmik: NewOpc = X86::VPCMPUQZrmik_alt; break; |
| 580 | case X86::VPCMPUQZrri: NewOpc = X86::VPCMPUQZrri_alt; break; |
| 581 | case X86::VPCMPUQZrrik: NewOpc = X86::VPCMPUQZrrik_alt; break; |
| 582 | case X86::VPCMPUWZ128rmi: NewOpc = X86::VPCMPUWZ128rmi_alt; break; |
| 583 | case X86::VPCMPUWZ128rmik: NewOpc = X86::VPCMPUWZ128rmik_alt; break; |
| 584 | case X86::VPCMPUWZ128rri: NewOpc = X86::VPCMPUWZ128rri_alt; break; |
| 585 | case X86::VPCMPUWZ128rrik: NewOpc = X86::VPCMPUWZ128rrik_alt; break; |
| 586 | case X86::VPCMPUWZ256rmi: NewOpc = X86::VPCMPUWZ256rmi_alt; break; |
| 587 | case X86::VPCMPUWZ256rmik: NewOpc = X86::VPCMPUWZ256rmik_alt; break; |
| 588 | case X86::VPCMPUWZ256rri: NewOpc = X86::VPCMPUWZ256rri_alt; break; |
| 589 | case X86::VPCMPUWZ256rrik: NewOpc = X86::VPCMPUWZ256rrik_alt; break; |
| 590 | case X86::VPCMPUWZrmi: NewOpc = X86::VPCMPUWZrmi_alt; break; |
| 591 | case X86::VPCMPUWZrmik: NewOpc = X86::VPCMPUWZrmik_alt; break; |
| 592 | case X86::VPCMPUWZrri: NewOpc = X86::VPCMPUWZrri_alt; break; |
| 593 | case X86::VPCMPUWZrrik: NewOpc = X86::VPCMPUWZrrik_alt; break; |
| 594 | case X86::VPCMPWZ128rmi: NewOpc = X86::VPCMPWZ128rmi_alt; break; |
| 595 | case X86::VPCMPWZ128rmik: NewOpc = X86::VPCMPWZ128rmik_alt; break; |
| 596 | case X86::VPCMPWZ128rri: NewOpc = X86::VPCMPWZ128rri_alt; break; |
| 597 | case X86::VPCMPWZ128rrik: NewOpc = X86::VPCMPWZ128rrik_alt; break; |
| 598 | case X86::VPCMPWZ256rmi: NewOpc = X86::VPCMPWZ256rmi_alt; break; |
| 599 | case X86::VPCMPWZ256rmik: NewOpc = X86::VPCMPWZ256rmik_alt; break; |
| 600 | case X86::VPCMPWZ256rri: NewOpc = X86::VPCMPWZ256rri_alt; break; |
| 601 | case X86::VPCMPWZ256rrik: NewOpc = X86::VPCMPWZ256rrik_alt; break; |
| 602 | case X86::VPCMPWZrmi: NewOpc = X86::VPCMPWZrmi_alt; break; |
| 603 | case X86::VPCMPWZrmik: NewOpc = X86::VPCMPWZrmik_alt; break; |
| 604 | case X86::VPCMPWZrri: NewOpc = X86::VPCMPWZrri_alt; break; |
| 605 | case X86::VPCMPWZrrik: NewOpc = X86::VPCMPWZrrik_alt; break; |
| 606 | } |
| 607 | // Switch opcode to the one that doesn't get special printing. |
| 608 | mcInst.setOpcode(NewOpc); |
| 609 | } |
Kevin Enderby | 5b03f72 | 2011-09-02 20:01:23 +0000 | [diff] [blame] | 610 | } |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 611 | |
| 612 | switch (type) { |
Craig Topper | c30fdbc | 2012-08-31 15:40:30 +0000 | [diff] [blame] | 613 | case TYPE_XMM32: |
| 614 | case TYPE_XMM64: |
Craig Topper | 96e00e5 | 2011-09-14 05:55:28 +0000 | [diff] [blame] | 615 | case TYPE_XMM128: |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 616 | mcInst.addOperand(MCOperand::createReg(X86::XMM0 + (immediate >> 4))); |
Craig Topper | 96e00e5 | 2011-09-14 05:55:28 +0000 | [diff] [blame] | 617 | return; |
| 618 | case TYPE_XMM256: |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 619 | mcInst.addOperand(MCOperand::createReg(X86::YMM0 + (immediate >> 4))); |
Craig Topper | 96e00e5 | 2011-09-14 05:55:28 +0000 | [diff] [blame] | 620 | return; |
Elena Demikhovsky | 003e7d7 | 2013-07-28 08:28:38 +0000 | [diff] [blame] | 621 | case TYPE_XMM512: |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 622 | mcInst.addOperand(MCOperand::createReg(X86::ZMM0 + (immediate >> 4))); |
Elena Demikhovsky | 003e7d7 | 2013-07-28 08:28:38 +0000 | [diff] [blame] | 623 | return; |
Elena Demikhovsky | 6b62b65 | 2015-06-09 13:02:10 +0000 | [diff] [blame] | 624 | case TYPE_BNDR: |
| 625 | mcInst.addOperand(MCOperand::createReg(X86::BND0 + (immediate >> 4))); |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 626 | case TYPE_REL8: |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 627 | isBranch = true; |
| 628 | pcrel = insn.startLocation + insn.immediateOffset + insn.immediateSize; |
Douglas Katzman | 289ec85 | 2015-06-26 16:58:59 +0000 | [diff] [blame] | 629 | if (immediate & 0x80) |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 630 | immediate |= ~(0xffull); |
| 631 | break; |
Douglas Katzman | 289ec85 | 2015-06-26 16:58:59 +0000 | [diff] [blame] | 632 | case TYPE_REL16: |
| 633 | isBranch = true; |
| 634 | pcrel = insn.startLocation + insn.immediateOffset + insn.immediateSize; |
| 635 | if (immediate & 0x8000) |
| 636 | immediate |= ~(0xffffull); |
| 637 | break; |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 638 | case TYPE_REL32: |
| 639 | case TYPE_REL64: |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 640 | isBranch = true; |
| 641 | pcrel = insn.startLocation + insn.immediateOffset + insn.immediateSize; |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 642 | if(immediate & 0x80000000) |
| 643 | immediate |= ~(0xffffffffull); |
| 644 | break; |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 645 | default: |
| 646 | // operand is 64 bits wide. Do nothing. |
| 647 | break; |
| 648 | } |
Craig Topper | 092e2fe | 2013-08-24 19:50:11 +0000 | [diff] [blame] | 649 | |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 650 | if(!tryAddingSymbolicOperand(immediate + pcrel, isBranch, insn.startLocation, |
| 651 | insn.immediateOffset, insn.immediateSize, |
| 652 | mcInst, Dis)) |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 653 | mcInst.addOperand(MCOperand::createImm(immediate)); |
Craig Topper | 35da3d1 | 2014-01-16 07:36:58 +0000 | [diff] [blame] | 654 | |
| 655 | if (type == TYPE_MOFFS8 || type == TYPE_MOFFS16 || |
| 656 | type == TYPE_MOFFS32 || type == TYPE_MOFFS64) { |
| 657 | MCOperand segmentReg; |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 658 | segmentReg = MCOperand::createReg(segmentRegnums[insn.segmentOverride]); |
Craig Topper | 35da3d1 | 2014-01-16 07:36:58 +0000 | [diff] [blame] | 659 | mcInst.addOperand(segmentReg); |
| 660 | } |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | /// translateRMRegister - Translates a register stored in the R/M field of the |
| 664 | /// ModR/M byte to its LLVM equivalent and appends it to an MCInst. |
| 665 | /// @param mcInst - The MCInst to append to. |
| 666 | /// @param insn - The internal instruction to extract the R/M field |
| 667 | /// from. |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 668 | /// @return - 0 on success; -1 otherwise |
| 669 | static bool translateRMRegister(MCInst &mcInst, |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 670 | InternalInstruction &insn) { |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 671 | if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) { |
| 672 | debug("A R/M register operand may not have a SIB byte"); |
| 673 | return true; |
| 674 | } |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 675 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 676 | switch (insn.eaBase) { |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 677 | default: |
| 678 | debug("Unexpected EA base register"); |
| 679 | return true; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 680 | case EA_BASE_NONE: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 681 | debug("EA_BASE_NONE for ModR/M base"); |
| 682 | return true; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 683 | #define ENTRY(x) case EA_BASE_##x: |
| 684 | ALL_EA_BASES |
| 685 | #undef ENTRY |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 686 | debug("A R/M register operand may not have a base; " |
| 687 | "the operand must be a register."); |
| 688 | return true; |
| 689 | #define ENTRY(x) \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 690 | case EA_REG_##x: \ |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 691 | mcInst.addOperand(MCOperand::createReg(X86::x)); break; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 692 | ALL_REGS |
| 693 | #undef ENTRY |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 694 | } |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 695 | |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 696 | return false; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | /// translateRMMemory - Translates a memory operand stored in the Mod and R/M |
| 700 | /// fields of an internal instruction (and possibly its SIB byte) to a memory |
| 701 | /// operand in LLVM's format, and appends it to an MCInst. |
| 702 | /// |
| 703 | /// @param mcInst - The MCInst to append to. |
| 704 | /// @param insn - The instruction to extract Mod, R/M, and SIB fields |
| 705 | /// from. |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 706 | /// @return - 0 on success; nonzero otherwise |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 707 | static bool translateRMMemory(MCInst &mcInst, InternalInstruction &insn, |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 708 | const MCDisassembler *Dis) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 709 | // Addresses in an MCInst are represented as five operands: |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 710 | // 1. basereg (register) The R/M base, or (if there is a SIB) the |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 711 | // SIB base |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 712 | // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 713 | // scale amount |
| 714 | // 3. indexreg (register) x86_registerNONE, or (if there is a SIB) |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 715 | // the index (which is multiplied by the |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 716 | // scale amount) |
| 717 | // 4. displacement (immediate) 0, or the displacement if there is one |
| 718 | // 5. segmentreg (register) x86_registerNONE for now, but could be set |
| 719 | // if we have segment overrides |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 720 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 721 | MCOperand baseReg; |
| 722 | MCOperand scaleAmount; |
| 723 | MCOperand indexReg; |
| 724 | MCOperand displacement; |
| 725 | MCOperand segmentReg; |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 726 | uint64_t pcrel = 0; |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 727 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 728 | if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) { |
| 729 | if (insn.sibBase != SIB_BASE_NONE) { |
| 730 | switch (insn.sibBase) { |
| 731 | default: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 732 | debug("Unexpected sibBase"); |
| 733 | return true; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 734 | #define ENTRY(x) \ |
Sean Callanan | 36eab80 | 2009-12-22 21:12:55 +0000 | [diff] [blame] | 735 | case SIB_BASE_##x: \ |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 736 | baseReg = MCOperand::createReg(X86::x); break; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 737 | ALL_SIB_BASES |
| 738 | #undef ENTRY |
| 739 | } |
| 740 | } else { |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 741 | baseReg = MCOperand::createReg(0); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 742 | } |
Manman Ren | a098204 | 2012-06-26 19:47:59 +0000 | [diff] [blame] | 743 | |
| 744 | // Check whether we are handling VSIB addressing mode for GATHER. |
| 745 | // If sibIndex was set to SIB_INDEX_NONE, index offset is 4 and |
| 746 | // we should use SIB_INDEX_XMM4|YMM4 for VSIB. |
| 747 | // I don't see a way to get the correct IndexReg in readSIB: |
| 748 | // We can tell whether it is VSIB or SIB after instruction ID is decoded, |
| 749 | // but instruction ID may not be decoded yet when calling readSIB. |
| 750 | uint32_t Opcode = mcInst.getOpcode(); |
Manman Ren | 98a5bf2 | 2012-06-29 00:54:20 +0000 | [diff] [blame] | 751 | bool IndexIs128 = (Opcode == X86::VGATHERDPDrm || |
| 752 | Opcode == X86::VGATHERDPDYrm || |
| 753 | Opcode == X86::VGATHERQPDrm || |
| 754 | Opcode == X86::VGATHERDPSrm || |
| 755 | Opcode == X86::VGATHERQPSrm || |
| 756 | Opcode == X86::VPGATHERDQrm || |
| 757 | Opcode == X86::VPGATHERDQYrm || |
| 758 | Opcode == X86::VPGATHERQQrm || |
| 759 | Opcode == X86::VPGATHERDDrm || |
| 760 | Opcode == X86::VPGATHERQDrm); |
| 761 | bool IndexIs256 = (Opcode == X86::VGATHERQPDYrm || |
| 762 | Opcode == X86::VGATHERDPSYrm || |
| 763 | Opcode == X86::VGATHERQPSYrm || |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 764 | Opcode == X86::VGATHERDPDZrm || |
| 765 | Opcode == X86::VPGATHERDQZrm || |
Manman Ren | 98a5bf2 | 2012-06-29 00:54:20 +0000 | [diff] [blame] | 766 | Opcode == X86::VPGATHERQQYrm || |
| 767 | Opcode == X86::VPGATHERDDYrm || |
| 768 | Opcode == X86::VPGATHERQDYrm); |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 769 | bool IndexIs512 = (Opcode == X86::VGATHERQPDZrm || |
| 770 | Opcode == X86::VGATHERDPSZrm || |
| 771 | Opcode == X86::VGATHERQPSZrm || |
| 772 | Opcode == X86::VPGATHERQQZrm || |
| 773 | Opcode == X86::VPGATHERDDZrm || |
| 774 | Opcode == X86::VPGATHERQDZrm); |
| 775 | if (IndexIs128 || IndexIs256 || IndexIs512) { |
Manman Ren | a098204 | 2012-06-26 19:47:59 +0000 | [diff] [blame] | 776 | unsigned IndexOffset = insn.sibIndex - |
| 777 | (insn.addressSize == 8 ? SIB_INDEX_RAX:SIB_INDEX_EAX); |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 778 | SIBIndex IndexBase = IndexIs512 ? SIB_INDEX_ZMM0 : |
| 779 | IndexIs256 ? SIB_INDEX_YMM0 : SIB_INDEX_XMM0; |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 780 | insn.sibIndex = (SIBIndex)(IndexBase + |
Manman Ren | a098204 | 2012-06-26 19:47:59 +0000 | [diff] [blame] | 781 | (insn.sibIndex == SIB_INDEX_NONE ? 4 : IndexOffset)); |
| 782 | } |
| 783 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 784 | if (insn.sibIndex != SIB_INDEX_NONE) { |
| 785 | switch (insn.sibIndex) { |
| 786 | default: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 787 | debug("Unexpected sibIndex"); |
| 788 | return true; |
Sean Callanan | 36eab80 | 2009-12-22 21:12:55 +0000 | [diff] [blame] | 789 | #define ENTRY(x) \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 790 | case SIB_INDEX_##x: \ |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 791 | indexReg = MCOperand::createReg(X86::x); break; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 792 | EA_BASES_32BIT |
| 793 | EA_BASES_64BIT |
Manman Ren | a098204 | 2012-06-26 19:47:59 +0000 | [diff] [blame] | 794 | REGS_XMM |
| 795 | REGS_YMM |
Elena Demikhovsky | 003e7d7 | 2013-07-28 08:28:38 +0000 | [diff] [blame] | 796 | REGS_ZMM |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 797 | #undef ENTRY |
| 798 | } |
| 799 | } else { |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 800 | indexReg = MCOperand::createReg(0); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 801 | } |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 802 | |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 803 | scaleAmount = MCOperand::createImm(insn.sibScale); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 804 | } else { |
| 805 | switch (insn.eaBase) { |
| 806 | case EA_BASE_NONE: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 807 | if (insn.eaDisplacement == EA_DISP_NONE) { |
| 808 | debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base"); |
| 809 | return true; |
| 810 | } |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 811 | if (insn.mode == MODE_64BIT){ |
| 812 | pcrel = insn.startLocation + |
| 813 | insn.displacementOffset + insn.displacementSize; |
Kevin Enderby | b119c08 | 2012-02-29 22:58:34 +0000 | [diff] [blame] | 814 | tryAddingPcLoadReferenceComment(insn.startLocation + |
| 815 | insn.displacementOffset, |
| 816 | insn.displacement + pcrel, Dis); |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 817 | baseReg = MCOperand::createReg(X86::RIP); // Section 2.2.1.6 |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 818 | } |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 819 | else |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 820 | baseReg = MCOperand::createReg(0); |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 821 | |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 822 | indexReg = MCOperand::createReg(0); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 823 | break; |
| 824 | case EA_BASE_BX_SI: |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 825 | baseReg = MCOperand::createReg(X86::BX); |
| 826 | indexReg = MCOperand::createReg(X86::SI); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 827 | break; |
| 828 | case EA_BASE_BX_DI: |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 829 | baseReg = MCOperand::createReg(X86::BX); |
| 830 | indexReg = MCOperand::createReg(X86::DI); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 831 | break; |
| 832 | case EA_BASE_BP_SI: |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 833 | baseReg = MCOperand::createReg(X86::BP); |
| 834 | indexReg = MCOperand::createReg(X86::SI); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 835 | break; |
| 836 | case EA_BASE_BP_DI: |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 837 | baseReg = MCOperand::createReg(X86::BP); |
| 838 | indexReg = MCOperand::createReg(X86::DI); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 839 | break; |
| 840 | default: |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 841 | indexReg = MCOperand::createReg(0); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 842 | switch (insn.eaBase) { |
| 843 | default: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 844 | debug("Unexpected eaBase"); |
| 845 | return true; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 846 | // Here, we will use the fill-ins defined above. However, |
| 847 | // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and |
| 848 | // sib and sib64 were handled in the top-level if, so they're only |
| 849 | // placeholders to keep the compiler happy. |
| 850 | #define ENTRY(x) \ |
| 851 | case EA_BASE_##x: \ |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 852 | baseReg = MCOperand::createReg(X86::x); break; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 853 | ALL_EA_BASES |
| 854 | #undef ENTRY |
| 855 | #define ENTRY(x) case EA_REG_##x: |
| 856 | ALL_REGS |
| 857 | #undef ENTRY |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 858 | debug("A R/M memory operand may not be a register; " |
| 859 | "the base field must be a base."); |
| 860 | return true; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 861 | } |
| 862 | } |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 863 | |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 864 | scaleAmount = MCOperand::createImm(1); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 865 | } |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 866 | |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 867 | displacement = MCOperand::createImm(insn.displacement); |
Craig Topper | 35da3d1 | 2014-01-16 07:36:58 +0000 | [diff] [blame] | 868 | |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 869 | segmentReg = MCOperand::createReg(segmentRegnums[insn.segmentOverride]); |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 870 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 871 | mcInst.addOperand(baseReg); |
| 872 | mcInst.addOperand(scaleAmount); |
| 873 | mcInst.addOperand(indexReg); |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 874 | if(!tryAddingSymbolicOperand(insn.displacement + pcrel, false, |
| 875 | insn.startLocation, insn.displacementOffset, |
| 876 | insn.displacementSize, mcInst, Dis)) |
| 877 | mcInst.addOperand(displacement); |
Chris Lattner | 55595fb | 2010-07-13 04:23:55 +0000 | [diff] [blame] | 878 | mcInst.addOperand(segmentReg); |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 879 | return false; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | /// translateRM - Translates an operand stored in the R/M (and possibly SIB) |
| 883 | /// byte of an instruction to LLVM form, and appends it to an MCInst. |
| 884 | /// |
| 885 | /// @param mcInst - The MCInst to append to. |
| 886 | /// @param operand - The operand, as stored in the descriptor table. |
| 887 | /// @param insn - The instruction to extract Mod, R/M, and SIB fields |
| 888 | /// from. |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 889 | /// @return - 0 on success; nonzero otherwise |
Benjamin Kramer | de0a4fb | 2010-10-23 09:10:44 +0000 | [diff] [blame] | 890 | static bool translateRM(MCInst &mcInst, const OperandSpecifier &operand, |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 891 | InternalInstruction &insn, const MCDisassembler *Dis) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 892 | switch (operand.type) { |
| 893 | default: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 894 | debug("Unexpected type for a R/M operand"); |
| 895 | return true; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 896 | case TYPE_R8: |
| 897 | case TYPE_R16: |
| 898 | case TYPE_R32: |
| 899 | case TYPE_R64: |
| 900 | case TYPE_Rv: |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 901 | case TYPE_MM64: |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 902 | case TYPE_XMM32: |
| 903 | case TYPE_XMM64: |
| 904 | case TYPE_XMM128: |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 905 | case TYPE_XMM256: |
Elena Demikhovsky | 003e7d7 | 2013-07-28 08:28:38 +0000 | [diff] [blame] | 906 | case TYPE_XMM512: |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 907 | case TYPE_VK1: |
Asaf Badouh | 0d957b8 | 2015-11-18 09:42:45 +0000 | [diff] [blame] | 908 | case TYPE_VK2: |
| 909 | case TYPE_VK4: |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 910 | case TYPE_VK8: |
| 911 | case TYPE_VK16: |
Asaf Badouh | 0d957b8 | 2015-11-18 09:42:45 +0000 | [diff] [blame] | 912 | case TYPE_VK32: |
| 913 | case TYPE_VK64: |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 914 | case TYPE_DEBUGREG: |
Sean Callanan | e7e1cf9 | 2010-05-06 20:59:00 +0000 | [diff] [blame] | 915 | case TYPE_CONTROLREG: |
Elena Demikhovsky | 6b62b65 | 2015-06-09 13:02:10 +0000 | [diff] [blame] | 916 | case TYPE_BNDR: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 917 | return translateRMRegister(mcInst, insn); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 918 | case TYPE_M: |
| 919 | case TYPE_M8: |
| 920 | case TYPE_M16: |
| 921 | case TYPE_M32: |
| 922 | case TYPE_M64: |
| 923 | case TYPE_M128: |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 924 | case TYPE_M256: |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 925 | case TYPE_M512: |
| 926 | case TYPE_Mv: |
| 927 | case TYPE_M32FP: |
| 928 | case TYPE_M64FP: |
| 929 | case TYPE_M80FP: |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 930 | case TYPE_M1616: |
| 931 | case TYPE_M1632: |
| 932 | case TYPE_M1664: |
Sean Callanan | 36eab80 | 2009-12-22 21:12:55 +0000 | [diff] [blame] | 933 | case TYPE_LEA: |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 934 | return translateRMMemory(mcInst, insn, Dis); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 935 | } |
| 936 | } |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 937 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 938 | /// translateFPRegister - Translates a stack position on the FPU stack to its |
| 939 | /// LLVM form, and appends it to an MCInst. |
| 940 | /// |
| 941 | /// @param mcInst - The MCInst to append to. |
| 942 | /// @param stackPos - The stack position to translate. |
Craig Topper | 9155118 | 2014-01-01 15:29:32 +0000 | [diff] [blame] | 943 | static void translateFPRegister(MCInst &mcInst, |
| 944 | uint8_t stackPos) { |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 945 | mcInst.addOperand(MCOperand::createReg(X86::ST0 + stackPos)); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 948 | /// translateMaskRegister - Translates a 3-bit mask register number to |
| 949 | /// LLVM form, and appends it to an MCInst. |
| 950 | /// |
| 951 | /// @param mcInst - The MCInst to append to. |
| 952 | /// @param maskRegNum - Number of mask register from 0 to 7. |
| 953 | /// @return - false on success; true otherwise. |
| 954 | static bool translateMaskRegister(MCInst &mcInst, |
| 955 | uint8_t maskRegNum) { |
| 956 | if (maskRegNum >= 8) { |
| 957 | debug("Invalid mask register number"); |
| 958 | return true; |
| 959 | } |
| 960 | |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 961 | mcInst.addOperand(MCOperand::createReg(X86::K0 + maskRegNum)); |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 962 | return false; |
| 963 | } |
| 964 | |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 965 | /// translateOperand - Translates an operand stored in an internal instruction |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 966 | /// to LLVM's format and appends it to an MCInst. |
| 967 | /// |
| 968 | /// @param mcInst - The MCInst to append to. |
| 969 | /// @param operand - The operand, as stored in the descriptor table. |
| 970 | /// @param insn - The internal instruction. |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 971 | /// @return - false on success; true otherwise. |
Benjamin Kramer | de0a4fb | 2010-10-23 09:10:44 +0000 | [diff] [blame] | 972 | static bool translateOperand(MCInst &mcInst, const OperandSpecifier &operand, |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 973 | InternalInstruction &insn, |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 974 | const MCDisassembler *Dis) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 975 | switch (operand.encoding) { |
| 976 | default: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 977 | debug("Unhandled operand encoding during translation"); |
| 978 | return true; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 979 | case ENCODING_REG: |
| 980 | translateRegister(mcInst, insn.reg); |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 981 | return false; |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 982 | case ENCODING_WRITEMASK: |
| 983 | return translateMaskRegister(mcInst, insn.writemask); |
Adam Nemet | 5933c2f | 2014-07-17 17:04:56 +0000 | [diff] [blame] | 984 | CASE_ENCODING_RM: |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 985 | return translateRM(mcInst, operand, insn, Dis); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 986 | case ENCODING_IB: |
| 987 | case ENCODING_IW: |
| 988 | case ENCODING_ID: |
| 989 | case ENCODING_IO: |
| 990 | case ENCODING_Iv: |
| 991 | case ENCODING_Ia: |
Sean Callanan | 4cd930f | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 992 | translateImmediate(mcInst, |
| 993 | insn.immediates[insn.numImmediatesTranslated++], |
| 994 | operand, |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 995 | insn, |
| 996 | Dis); |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 997 | return false; |
David Woodhouse | 2ef8d9c | 2014-01-22 15:08:08 +0000 | [diff] [blame] | 998 | case ENCODING_SI: |
| 999 | return translateSrcIndex(mcInst, insn); |
David Woodhouse | b33c2ef | 2014-01-22 15:08:21 +0000 | [diff] [blame] | 1000 | case ENCODING_DI: |
| 1001 | return translateDstIndex(mcInst, insn); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1002 | case ENCODING_RB: |
| 1003 | case ENCODING_RW: |
| 1004 | case ENCODING_RD: |
| 1005 | case ENCODING_RO: |
Craig Topper | 9155118 | 2014-01-01 15:29:32 +0000 | [diff] [blame] | 1006 | case ENCODING_Rv: |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1007 | translateRegister(mcInst, insn.opcodeRegister); |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1008 | return false; |
Craig Topper | 623b0d6 | 2014-01-01 14:22:37 +0000 | [diff] [blame] | 1009 | case ENCODING_FP: |
Craig Topper | 9155118 | 2014-01-01 15:29:32 +0000 | [diff] [blame] | 1010 | translateFPRegister(mcInst, insn.modRM & 7); |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1011 | return false; |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1012 | case ENCODING_VVVV: |
| 1013 | translateRegister(mcInst, insn.vvvv); |
| 1014 | return false; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1015 | case ENCODING_DUP: |
Craig Topper | b8aec08 | 2012-08-01 07:39:18 +0000 | [diff] [blame] | 1016 | return translateOperand(mcInst, insn.operands[operand.type - TYPE_DUP0], |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 1017 | insn, Dis); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1018 | } |
| 1019 | } |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 1020 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1021 | /// translateInstruction - Translates an internal instruction and all its |
| 1022 | /// operands to an MCInst. |
| 1023 | /// |
| 1024 | /// @param mcInst - The MCInst to populate with the instruction's data. |
| 1025 | /// @param insn - The internal instruction. |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1026 | /// @return - false on success; true otherwise. |
| 1027 | static bool translateInstruction(MCInst &mcInst, |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 1028 | InternalInstruction &insn, |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 1029 | const MCDisassembler *Dis) { |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1030 | if (!insn.spec) { |
| 1031 | debug("Instruction has no specification"); |
| 1032 | return true; |
| 1033 | } |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 1034 | |
Cameron Esfahani | f97999d | 2015-08-11 01:15:07 +0000 | [diff] [blame] | 1035 | mcInst.clear(); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1036 | mcInst.setOpcode(insn.instructionID); |
Kevin Enderby | 35fd792 | 2013-06-20 22:32:18 +0000 | [diff] [blame] | 1037 | // If when reading the prefix bytes we determined the overlapping 0xf2 or 0xf3 |
| 1038 | // prefix bytes should be disassembled as xrelease and xacquire then set the |
| 1039 | // opcode to those instead of the rep and repne opcodes. |
| 1040 | if (insn.xAcquireRelease) { |
| 1041 | if(mcInst.getOpcode() == X86::REP_PREFIX) |
| 1042 | mcInst.setOpcode(X86::XRELEASE_PREFIX); |
| 1043 | else if(mcInst.getOpcode() == X86::REPNE_PREFIX) |
| 1044 | mcInst.setOpcode(X86::XACQUIRE_PREFIX); |
| 1045 | } |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 1046 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1047 | insn.numImmediatesTranslated = 0; |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 1048 | |
Patrik Hagglund | 3199838 | 2014-04-28 12:12:27 +0000 | [diff] [blame] | 1049 | for (const auto &Op : insn.operands) { |
| 1050 | if (Op.encoding != ENCODING_NONE) { |
| 1051 | if (translateOperand(mcInst, Op, insn, Dis)) { |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1052 | return true; |
| 1053 | } |
| 1054 | } |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1055 | } |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 1056 | |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1057 | return false; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1058 | } |
Daniel Dunbar | 900f2ce | 2009-11-25 06:53:08 +0000 | [diff] [blame] | 1059 | |
David Woodhouse | 7dd2182 | 2014-01-20 12:02:31 +0000 | [diff] [blame] | 1060 | static MCDisassembler *createX86Disassembler(const Target &T, |
Lang Hames | a1bc0f5 | 2014-04-15 04:40:56 +0000 | [diff] [blame] | 1061 | const MCSubtargetInfo &STI, |
| 1062 | MCContext &Ctx) { |
Lang Hames | 0563ca1 | 2014-04-13 04:09:16 +0000 | [diff] [blame] | 1063 | std::unique_ptr<const MCInstrInfo> MII(T.createMCInstrInfo()); |
Craig Topper | b805723 | 2016-04-29 04:22:28 +0000 | [diff] [blame] | 1064 | return new X86GenericDisassembler(STI, Ctx, std::move(MII)); |
Daniel Dunbar | 900f2ce | 2009-11-25 06:53:08 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 1067 | extern "C" void LLVMInitializeX86Disassembler() { |
Daniel Dunbar | 900f2ce | 2009-11-25 06:53:08 +0000 | [diff] [blame] | 1068 | // Register the disassembler. |
Mehdi Amini | f42454b | 2016-10-09 23:00:34 +0000 | [diff] [blame^] | 1069 | TargetRegistry::RegisterMCDisassembler(getTheX86_32Target(), |
David Woodhouse | 7dd2182 | 2014-01-20 12:02:31 +0000 | [diff] [blame] | 1070 | createX86Disassembler); |
Mehdi Amini | f42454b | 2016-10-09 23:00:34 +0000 | [diff] [blame^] | 1071 | TargetRegistry::RegisterMCDisassembler(getTheX86_64Target(), |
David Woodhouse | 7dd2182 | 2014-01-20 12:02:31 +0000 | [diff] [blame] | 1072 | createX86Disassembler); |
Daniel Dunbar | 900f2ce | 2009-11-25 06:53:08 +0000 | [diff] [blame] | 1073 | } |