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