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