Jia Liu | 31d157a | 2012-02-18 12:03:15 +0000 | [diff] [blame^] | 1 | //===-- X86Disassembler.cpp - Disassembler for x86 and x86_64 -------------===// |
Daniel Dunbar | 5f9b9ef | 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 | 8ed9f51 | 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 | 5f9b9ef | 2009-11-25 06:53:08 +0000 | [diff] [blame] | 19 | |
Sean Callanan | 9899f70 | 2010-04-13 21:21:57 +0000 | [diff] [blame] | 20 | #include "llvm/MC/EDInstInfo.h" |
Daniel Dunbar | 5f9b9ef | 2009-11-25 06:53:08 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCDisassembler.h" |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCDisassembler.h" |
| 23 | #include "llvm/MC/MCInst.h" |
Benjamin Kramer | 953362c | 2012-02-11 14:50:54 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCInstrInfo.h" |
James Molloy | b950585 | 2011-09-07 17:24:38 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCSubtargetInfo.h" |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 27 | #include "llvm/Support/MemoryObject.h" |
Evan Cheng | 3e74d6f | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 28 | #include "llvm/Support/TargetRegistry.h" |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Sean Callanan | 0122c90 | 2009-12-22 01:11:26 +0000 | [diff] [blame] | 30 | |
Evan Cheng | 73f50d9 | 2011-06-27 18:32:37 +0000 | [diff] [blame] | 31 | #define GET_REGINFO_ENUM |
| 32 | #include "X86GenRegisterInfo.inc" |
Kevin Enderby | d5705fe | 2011-09-02 20:01:23 +0000 | [diff] [blame] | 33 | #define GET_INSTRINFO_ENUM |
| 34 | #include "X86GenInstrInfo.inc" |
Sean Callanan | 9899f70 | 2010-04-13 21:21:57 +0000 | [diff] [blame] | 35 | #include "X86GenEDInfo.inc" |
Sean Callanan | 0122c90 | 2009-12-22 01:11:26 +0000 | [diff] [blame] | 36 | |
Daniel Dunbar | 5f9b9ef | 2009-11-25 06:53:08 +0000 | [diff] [blame] | 37 | using namespace llvm; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 38 | using namespace llvm::X86Disassembler; |
| 39 | |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 40 | void x86DisassemblerDebug(const char *file, |
| 41 | unsigned line, |
| 42 | const char *s) { |
| 43 | dbgs() << file << ":" << line << ": " << s; |
| 44 | } |
| 45 | |
Benjamin Kramer | 953362c | 2012-02-11 14:50:54 +0000 | [diff] [blame] | 46 | const char *x86DisassemblerGetInstrName(unsigned Opcode, void *mii) { |
| 47 | const MCInstrInfo *MII = static_cast<const MCInstrInfo *>(mii); |
| 48 | return MII->getName(Opcode); |
| 49 | } |
| 50 | |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 51 | #define debug(s) DEBUG(x86DisassemblerDebug(__FILE__, __LINE__, s)); |
| 52 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 53 | namespace llvm { |
| 54 | |
| 55 | // Fill-ins to make the compiler happy. These constants are never actually |
| 56 | // assigned; they are just filler to make an automatically-generated switch |
| 57 | // statement work. |
| 58 | namespace X86 { |
| 59 | enum { |
| 60 | BX_SI = 500, |
| 61 | BX_DI = 501, |
| 62 | BP_SI = 502, |
| 63 | BP_DI = 503, |
| 64 | sib = 504, |
| 65 | sib64 = 505 |
| 66 | }; |
| 67 | } |
| 68 | |
Sean Callanan | 0122c90 | 2009-12-22 01:11:26 +0000 | [diff] [blame] | 69 | extern Target TheX86_32Target, TheX86_64Target; |
| 70 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 73 | static bool translateInstruction(MCInst &target, |
| 74 | InternalInstruction &source); |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 75 | |
Benjamin Kramer | 953362c | 2012-02-11 14:50:54 +0000 | [diff] [blame] | 76 | X86GenericDisassembler::X86GenericDisassembler(const MCSubtargetInfo &STI, |
| 77 | DisassemblerMode mode, |
| 78 | const MCInstrInfo *MII) |
| 79 | : MCDisassembler(STI), MII(MII), fMode(mode) {} |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 80 | |
| 81 | X86GenericDisassembler::~X86GenericDisassembler() { |
Benjamin Kramer | 953362c | 2012-02-11 14:50:54 +0000 | [diff] [blame] | 82 | delete MII; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Benjamin Kramer | 88b6fc0 | 2012-02-11 14:51:07 +0000 | [diff] [blame] | 85 | const EDInstInfo *X86GenericDisassembler::getEDInfo() const { |
Sean Callanan | 9899f70 | 2010-04-13 21:21:57 +0000 | [diff] [blame] | 86 | return instInfoX86; |
| 87 | } |
| 88 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 89 | /// regionReader - a callback function that wraps the readByte method from |
| 90 | /// MemoryObject. |
| 91 | /// |
| 92 | /// @param arg - The generic callback parameter. In this case, this should |
| 93 | /// be a pointer to a MemoryObject. |
| 94 | /// @param byte - A pointer to the byte to be read. |
| 95 | /// @param address - The address to be read. |
| 96 | static int regionReader(void* arg, uint8_t* byte, uint64_t address) { |
| 97 | MemoryObject* region = static_cast<MemoryObject*>(arg); |
| 98 | return region->readByte(address, byte); |
| 99 | } |
| 100 | |
| 101 | /// logger - a callback function that wraps the operator<< method from |
| 102 | /// raw_ostream. |
| 103 | /// |
| 104 | /// @param arg - The generic callback parameter. This should be a pointe |
| 105 | /// to a raw_ostream. |
| 106 | /// @param log - A string to be logged. logger() adds a newline. |
| 107 | static void logger(void* arg, const char* log) { |
| 108 | if (!arg) |
| 109 | return; |
| 110 | |
| 111 | raw_ostream &vStream = *(static_cast<raw_ostream*>(arg)); |
| 112 | vStream << log << "\n"; |
| 113 | } |
| 114 | |
| 115 | // |
| 116 | // Public interface for the disassembler |
| 117 | // |
| 118 | |
Owen Anderson | 83e3f67 | 2011-08-17 17:44:15 +0000 | [diff] [blame] | 119 | MCDisassembler::DecodeStatus |
| 120 | X86GenericDisassembler::getInstruction(MCInst &instr, |
| 121 | uint64_t &size, |
Derek Schuff | 2ea9387 | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 122 | MemoryObject ®ion, |
Owen Anderson | 83e3f67 | 2011-08-17 17:44:15 +0000 | [diff] [blame] | 123 | uint64_t address, |
Owen Anderson | 98c5dda | 2011-09-15 23:38:46 +0000 | [diff] [blame] | 124 | raw_ostream &vStream, |
| 125 | raw_ostream &cStream) const { |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 126 | InternalInstruction internalInstr; |
Benjamin Kramer | 15c9a1f | 2011-09-21 21:47:35 +0000 | [diff] [blame] | 127 | |
| 128 | dlog_t loggerFn = logger; |
| 129 | if (&vStream == &nulls()) |
| 130 | loggerFn = 0; // Disable logging completely if it's going to nulls(). |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 131 | |
| 132 | int ret = decodeInstruction(&internalInstr, |
| 133 | regionReader, |
| 134 | (void*)®ion, |
Benjamin Kramer | 15c9a1f | 2011-09-21 21:47:35 +0000 | [diff] [blame] | 135 | loggerFn, |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 136 | (void*)&vStream, |
Benjamin Kramer | 953362c | 2012-02-11 14:50:54 +0000 | [diff] [blame] | 137 | (void*)MII, |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 138 | address, |
| 139 | fMode); |
| 140 | |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 141 | if (ret) { |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 142 | size = internalInstr.readerCursor - address; |
Owen Anderson | 83e3f67 | 2011-08-17 17:44:15 +0000 | [diff] [blame] | 143 | return Fail; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 144 | } |
| 145 | else { |
| 146 | size = internalInstr.length; |
Owen Anderson | 83e3f67 | 2011-08-17 17:44:15 +0000 | [diff] [blame] | 147 | return (!translateInstruction(instr, internalInstr)) ? Success : Fail; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
| 151 | // |
| 152 | // Private code that translates from struct InternalInstructions to MCInsts. |
| 153 | // |
| 154 | |
| 155 | /// translateRegister - Translates an internal register to the appropriate LLVM |
| 156 | /// register, and appends it as an operand to an MCInst. |
| 157 | /// |
| 158 | /// @param mcInst - The MCInst to append to. |
| 159 | /// @param reg - The Reg to append. |
| 160 | static void translateRegister(MCInst &mcInst, Reg reg) { |
| 161 | #define ENTRY(x) X86::x, |
| 162 | uint8_t llvmRegnums[] = { |
| 163 | ALL_REGS |
| 164 | 0 |
| 165 | }; |
| 166 | #undef ENTRY |
| 167 | |
| 168 | uint8_t llvmRegnum = llvmRegnums[reg]; |
| 169 | mcInst.addOperand(MCOperand::CreateReg(llvmRegnum)); |
| 170 | } |
| 171 | |
| 172 | /// translateImmediate - Appends an immediate operand to an MCInst. |
| 173 | /// |
| 174 | /// @param mcInst - The MCInst to append to. |
| 175 | /// @param immediate - The immediate value to append. |
Sean Callanan | be192dd | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 176 | /// @param operand - The operand, as stored in the descriptor table. |
| 177 | /// @param insn - The internal instruction. |
Benjamin Kramer | 4d1dca9 | 2010-10-23 09:10:44 +0000 | [diff] [blame] | 178 | static void translateImmediate(MCInst &mcInst, uint64_t immediate, |
| 179 | const OperandSpecifier &operand, |
Sean Callanan | be192dd | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 180 | InternalInstruction &insn) { |
| 181 | // Sign-extend the immediate if necessary. |
| 182 | |
| 183 | OperandType type = operand.type; |
| 184 | |
| 185 | if (type == TYPE_RELv) { |
| 186 | switch (insn.displacementSize) { |
| 187 | default: |
| 188 | break; |
Sean Callanan | 89e59e6 | 2011-02-21 21:55:05 +0000 | [diff] [blame] | 189 | case 1: |
Sean Callanan | be192dd | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 190 | type = TYPE_MOFFS8; |
| 191 | break; |
Sean Callanan | 89e59e6 | 2011-02-21 21:55:05 +0000 | [diff] [blame] | 192 | case 2: |
Sean Callanan | be192dd | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 193 | type = TYPE_MOFFS16; |
| 194 | break; |
Sean Callanan | 89e59e6 | 2011-02-21 21:55:05 +0000 | [diff] [blame] | 195 | case 4: |
Sean Callanan | be192dd | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 196 | type = TYPE_MOFFS32; |
| 197 | break; |
Sean Callanan | 89e59e6 | 2011-02-21 21:55:05 +0000 | [diff] [blame] | 198 | case 8: |
Sean Callanan | be192dd | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 199 | type = TYPE_MOFFS64; |
| 200 | break; |
| 201 | } |
| 202 | } |
Kevin Enderby | d5705fe | 2011-09-02 20:01:23 +0000 | [diff] [blame] | 203 | // By default sign-extend all X86 immediates based on their encoding. |
| 204 | else if (type == TYPE_IMM8 || type == TYPE_IMM16 || type == TYPE_IMM32 || |
| 205 | type == TYPE_IMM64) { |
| 206 | uint32_t Opcode = mcInst.getOpcode(); |
| 207 | switch (operand.encoding) { |
| 208 | default: |
| 209 | break; |
| 210 | case ENCODING_IB: |
| 211 | // Special case those X86 instructions that use the imm8 as a set of |
| 212 | // bits, bit count, etc. and are not sign-extend. |
| 213 | if (Opcode != X86::BLENDPSrri && Opcode != X86::BLENDPDrri && |
| 214 | Opcode != X86::PBLENDWrri && Opcode != X86::MPSADBWrri && |
| 215 | Opcode != X86::DPPSrri && Opcode != X86::DPPDrri && |
| 216 | Opcode != X86::INSERTPSrr && Opcode != X86::VBLENDPSYrri && |
| 217 | Opcode != X86::VBLENDPSYrmi && Opcode != X86::VBLENDPDYrri && |
| 218 | Opcode != X86::VBLENDPDYrmi && Opcode != X86::VPBLENDWrri && |
| 219 | Opcode != X86::VMPSADBWrri && Opcode != X86::VDPPSYrri && |
| 220 | Opcode != X86::VDPPSYrmi && Opcode != X86::VDPPDrri && |
| 221 | Opcode != X86::VINSERTPSrr) |
| 222 | type = TYPE_MOFFS8; |
| 223 | break; |
| 224 | case ENCODING_IW: |
| 225 | type = TYPE_MOFFS16; |
| 226 | break; |
| 227 | case ENCODING_ID: |
| 228 | type = TYPE_MOFFS32; |
| 229 | break; |
| 230 | case ENCODING_IO: |
| 231 | type = TYPE_MOFFS64; |
| 232 | break; |
| 233 | } |
| 234 | } |
Sean Callanan | be192dd | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 235 | |
| 236 | switch (type) { |
Craig Topper | 3bb43a8 | 2011-09-14 05:55:28 +0000 | [diff] [blame] | 237 | case TYPE_XMM128: |
| 238 | mcInst.addOperand(MCOperand::CreateReg(X86::XMM0 + (immediate >> 4))); |
| 239 | return; |
| 240 | case TYPE_XMM256: |
| 241 | mcInst.addOperand(MCOperand::CreateReg(X86::YMM0 + (immediate >> 4))); |
| 242 | return; |
Sean Callanan | be192dd | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 243 | case TYPE_MOFFS8: |
| 244 | case TYPE_REL8: |
| 245 | if(immediate & 0x80) |
| 246 | immediate |= ~(0xffull); |
| 247 | break; |
| 248 | case TYPE_MOFFS16: |
| 249 | if(immediate & 0x8000) |
| 250 | immediate |= ~(0xffffull); |
| 251 | break; |
| 252 | case TYPE_MOFFS32: |
| 253 | case TYPE_REL32: |
| 254 | case TYPE_REL64: |
| 255 | if(immediate & 0x80000000) |
| 256 | immediate |= ~(0xffffffffull); |
| 257 | break; |
| 258 | case TYPE_MOFFS64: |
| 259 | default: |
| 260 | // operand is 64 bits wide. Do nothing. |
| 261 | break; |
| 262 | } |
| 263 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 264 | mcInst.addOperand(MCOperand::CreateImm(immediate)); |
| 265 | } |
| 266 | |
| 267 | /// translateRMRegister - Translates a register stored in the R/M field of the |
| 268 | /// ModR/M byte to its LLVM equivalent and appends it to an MCInst. |
| 269 | /// @param mcInst - The MCInst to append to. |
| 270 | /// @param insn - The internal instruction to extract the R/M field |
| 271 | /// from. |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 272 | /// @return - 0 on success; -1 otherwise |
| 273 | static bool translateRMRegister(MCInst &mcInst, |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 274 | InternalInstruction &insn) { |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 275 | if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) { |
| 276 | debug("A R/M register operand may not have a SIB byte"); |
| 277 | return true; |
| 278 | } |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 279 | |
| 280 | switch (insn.eaBase) { |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 281 | default: |
| 282 | debug("Unexpected EA base register"); |
| 283 | return true; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 284 | case EA_BASE_NONE: |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 285 | debug("EA_BASE_NONE for ModR/M base"); |
| 286 | return true; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 287 | #define ENTRY(x) case EA_BASE_##x: |
| 288 | ALL_EA_BASES |
| 289 | #undef ENTRY |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 290 | debug("A R/M register operand may not have a base; " |
| 291 | "the operand must be a register."); |
| 292 | return true; |
| 293 | #define ENTRY(x) \ |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 294 | case EA_REG_##x: \ |
| 295 | mcInst.addOperand(MCOperand::CreateReg(X86::x)); break; |
| 296 | ALL_REGS |
| 297 | #undef ENTRY |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 298 | } |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 299 | |
| 300 | return false; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /// translateRMMemory - Translates a memory operand stored in the Mod and R/M |
| 304 | /// fields of an internal instruction (and possibly its SIB byte) to a memory |
| 305 | /// operand in LLVM's format, and appends it to an MCInst. |
| 306 | /// |
| 307 | /// @param mcInst - The MCInst to append to. |
| 308 | /// @param insn - The instruction to extract Mod, R/M, and SIB fields |
| 309 | /// from. |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 310 | /// @return - 0 on success; nonzero otherwise |
Chris Lattner | 37a746b | 2010-07-13 04:23:55 +0000 | [diff] [blame] | 311 | static bool translateRMMemory(MCInst &mcInst, InternalInstruction &insn) { |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 312 | // Addresses in an MCInst are represented as five operands: |
| 313 | // 1. basereg (register) The R/M base, or (if there is a SIB) the |
| 314 | // SIB base |
| 315 | // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified |
| 316 | // scale amount |
| 317 | // 3. indexreg (register) x86_registerNONE, or (if there is a SIB) |
| 318 | // the index (which is multiplied by the |
| 319 | // scale amount) |
| 320 | // 4. displacement (immediate) 0, or the displacement if there is one |
| 321 | // 5. segmentreg (register) x86_registerNONE for now, but could be set |
| 322 | // if we have segment overrides |
| 323 | |
| 324 | MCOperand baseReg; |
| 325 | MCOperand scaleAmount; |
| 326 | MCOperand indexReg; |
| 327 | MCOperand displacement; |
| 328 | MCOperand segmentReg; |
| 329 | |
| 330 | if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) { |
| 331 | if (insn.sibBase != SIB_BASE_NONE) { |
| 332 | switch (insn.sibBase) { |
| 333 | default: |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 334 | debug("Unexpected sibBase"); |
| 335 | return true; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 336 | #define ENTRY(x) \ |
Sean Callanan | 7fb35a2 | 2009-12-22 21:12:55 +0000 | [diff] [blame] | 337 | case SIB_BASE_##x: \ |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 338 | baseReg = MCOperand::CreateReg(X86::x); break; |
| 339 | ALL_SIB_BASES |
| 340 | #undef ENTRY |
| 341 | } |
| 342 | } else { |
| 343 | baseReg = MCOperand::CreateReg(0); |
| 344 | } |
| 345 | |
| 346 | if (insn.sibIndex != SIB_INDEX_NONE) { |
| 347 | switch (insn.sibIndex) { |
| 348 | default: |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 349 | debug("Unexpected sibIndex"); |
| 350 | return true; |
Sean Callanan | 7fb35a2 | 2009-12-22 21:12:55 +0000 | [diff] [blame] | 351 | #define ENTRY(x) \ |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 352 | case SIB_INDEX_##x: \ |
| 353 | indexReg = MCOperand::CreateReg(X86::x); break; |
| 354 | EA_BASES_32BIT |
| 355 | EA_BASES_64BIT |
| 356 | #undef ENTRY |
| 357 | } |
| 358 | } else { |
| 359 | indexReg = MCOperand::CreateReg(0); |
| 360 | } |
| 361 | |
| 362 | scaleAmount = MCOperand::CreateImm(insn.sibScale); |
| 363 | } else { |
| 364 | switch (insn.eaBase) { |
| 365 | case EA_BASE_NONE: |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 366 | if (insn.eaDisplacement == EA_DISP_NONE) { |
| 367 | debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base"); |
| 368 | return true; |
| 369 | } |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 370 | if (insn.mode == MODE_64BIT) |
| 371 | baseReg = MCOperand::CreateReg(X86::RIP); // Section 2.2.1.6 |
| 372 | else |
| 373 | baseReg = MCOperand::CreateReg(0); |
| 374 | |
| 375 | indexReg = MCOperand::CreateReg(0); |
| 376 | break; |
| 377 | case EA_BASE_BX_SI: |
| 378 | baseReg = MCOperand::CreateReg(X86::BX); |
| 379 | indexReg = MCOperand::CreateReg(X86::SI); |
| 380 | break; |
| 381 | case EA_BASE_BX_DI: |
| 382 | baseReg = MCOperand::CreateReg(X86::BX); |
| 383 | indexReg = MCOperand::CreateReg(X86::DI); |
| 384 | break; |
| 385 | case EA_BASE_BP_SI: |
| 386 | baseReg = MCOperand::CreateReg(X86::BP); |
| 387 | indexReg = MCOperand::CreateReg(X86::SI); |
| 388 | break; |
| 389 | case EA_BASE_BP_DI: |
| 390 | baseReg = MCOperand::CreateReg(X86::BP); |
| 391 | indexReg = MCOperand::CreateReg(X86::DI); |
| 392 | break; |
| 393 | default: |
| 394 | indexReg = MCOperand::CreateReg(0); |
| 395 | switch (insn.eaBase) { |
| 396 | default: |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 397 | debug("Unexpected eaBase"); |
| 398 | return true; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 399 | // Here, we will use the fill-ins defined above. However, |
| 400 | // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and |
| 401 | // sib and sib64 were handled in the top-level if, so they're only |
| 402 | // placeholders to keep the compiler happy. |
| 403 | #define ENTRY(x) \ |
| 404 | case EA_BASE_##x: \ |
| 405 | baseReg = MCOperand::CreateReg(X86::x); break; |
| 406 | ALL_EA_BASES |
| 407 | #undef ENTRY |
| 408 | #define ENTRY(x) case EA_REG_##x: |
| 409 | ALL_REGS |
| 410 | #undef ENTRY |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 411 | debug("A R/M memory operand may not be a register; " |
| 412 | "the base field must be a base."); |
| 413 | return true; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 414 | } |
| 415 | } |
Sean Callanan | 7fb35a2 | 2009-12-22 21:12:55 +0000 | [diff] [blame] | 416 | |
| 417 | scaleAmount = MCOperand::CreateImm(1); |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | displacement = MCOperand::CreateImm(insn.displacement); |
| 421 | |
| 422 | static const uint8_t segmentRegnums[SEG_OVERRIDE_max] = { |
| 423 | 0, // SEG_OVERRIDE_NONE |
| 424 | X86::CS, |
| 425 | X86::SS, |
| 426 | X86::DS, |
| 427 | X86::ES, |
| 428 | X86::FS, |
| 429 | X86::GS |
| 430 | }; |
| 431 | |
| 432 | segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]); |
| 433 | |
| 434 | mcInst.addOperand(baseReg); |
| 435 | mcInst.addOperand(scaleAmount); |
| 436 | mcInst.addOperand(indexReg); |
| 437 | mcInst.addOperand(displacement); |
Chris Lattner | 37a746b | 2010-07-13 04:23:55 +0000 | [diff] [blame] | 438 | mcInst.addOperand(segmentReg); |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 439 | return false; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /// translateRM - Translates an operand stored in the R/M (and possibly SIB) |
| 443 | /// byte of an instruction to LLVM form, and appends it to an MCInst. |
| 444 | /// |
| 445 | /// @param mcInst - The MCInst to append to. |
| 446 | /// @param operand - The operand, as stored in the descriptor table. |
| 447 | /// @param insn - The instruction to extract Mod, R/M, and SIB fields |
| 448 | /// from. |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 449 | /// @return - 0 on success; nonzero otherwise |
Benjamin Kramer | 4d1dca9 | 2010-10-23 09:10:44 +0000 | [diff] [blame] | 450 | static bool translateRM(MCInst &mcInst, const OperandSpecifier &operand, |
| 451 | InternalInstruction &insn) { |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 452 | switch (operand.type) { |
| 453 | default: |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 454 | debug("Unexpected type for a R/M operand"); |
| 455 | return true; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 456 | case TYPE_R8: |
| 457 | case TYPE_R16: |
| 458 | case TYPE_R32: |
| 459 | case TYPE_R64: |
| 460 | case TYPE_Rv: |
| 461 | case TYPE_MM: |
| 462 | case TYPE_MM32: |
| 463 | case TYPE_MM64: |
| 464 | case TYPE_XMM: |
| 465 | case TYPE_XMM32: |
| 466 | case TYPE_XMM64: |
| 467 | case TYPE_XMM128: |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 468 | case TYPE_XMM256: |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 469 | case TYPE_DEBUGREG: |
Sean Callanan | 1a8b789 | 2010-05-06 20:59:00 +0000 | [diff] [blame] | 470 | case TYPE_CONTROLREG: |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 471 | return translateRMRegister(mcInst, insn); |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 472 | case TYPE_M: |
| 473 | case TYPE_M8: |
| 474 | case TYPE_M16: |
| 475 | case TYPE_M32: |
| 476 | case TYPE_M64: |
| 477 | case TYPE_M128: |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 478 | case TYPE_M256: |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 479 | case TYPE_M512: |
| 480 | case TYPE_Mv: |
| 481 | case TYPE_M32FP: |
| 482 | case TYPE_M64FP: |
| 483 | case TYPE_M80FP: |
| 484 | case TYPE_M16INT: |
| 485 | case TYPE_M32INT: |
| 486 | case TYPE_M64INT: |
| 487 | case TYPE_M1616: |
| 488 | case TYPE_M1632: |
| 489 | case TYPE_M1664: |
Sean Callanan | 7fb35a2 | 2009-12-22 21:12:55 +0000 | [diff] [blame] | 490 | case TYPE_LEA: |
Chris Lattner | 37a746b | 2010-07-13 04:23:55 +0000 | [diff] [blame] | 491 | return translateRMMemory(mcInst, insn); |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
| 495 | /// translateFPRegister - Translates a stack position on the FPU stack to its |
| 496 | /// LLVM form, and appends it to an MCInst. |
| 497 | /// |
| 498 | /// @param mcInst - The MCInst to append to. |
| 499 | /// @param stackPos - The stack position to translate. |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 500 | /// @return - 0 on success; nonzero otherwise. |
| 501 | static bool translateFPRegister(MCInst &mcInst, |
| 502 | uint8_t stackPos) { |
| 503 | if (stackPos >= 8) { |
| 504 | debug("Invalid FP stack position"); |
| 505 | return true; |
| 506 | } |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 507 | |
| 508 | mcInst.addOperand(MCOperand::CreateReg(X86::ST0 + stackPos)); |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 509 | |
| 510 | return false; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /// translateOperand - Translates an operand stored in an internal instruction |
| 514 | /// to LLVM's format and appends it to an MCInst. |
| 515 | /// |
| 516 | /// @param mcInst - The MCInst to append to. |
| 517 | /// @param operand - The operand, as stored in the descriptor table. |
| 518 | /// @param insn - The internal instruction. |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 519 | /// @return - false on success; true otherwise. |
Benjamin Kramer | 4d1dca9 | 2010-10-23 09:10:44 +0000 | [diff] [blame] | 520 | static bool translateOperand(MCInst &mcInst, const OperandSpecifier &operand, |
| 521 | InternalInstruction &insn) { |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 522 | switch (operand.encoding) { |
| 523 | default: |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 524 | debug("Unhandled operand encoding during translation"); |
| 525 | return true; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 526 | case ENCODING_REG: |
| 527 | translateRegister(mcInst, insn.reg); |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 528 | return false; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 529 | case ENCODING_RM: |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 530 | return translateRM(mcInst, operand, insn); |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 531 | case ENCODING_CB: |
| 532 | case ENCODING_CW: |
| 533 | case ENCODING_CD: |
| 534 | case ENCODING_CP: |
| 535 | case ENCODING_CO: |
| 536 | case ENCODING_CT: |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 537 | debug("Translation of code offsets isn't supported."); |
| 538 | return true; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 539 | case ENCODING_IB: |
| 540 | case ENCODING_IW: |
| 541 | case ENCODING_ID: |
| 542 | case ENCODING_IO: |
| 543 | case ENCODING_Iv: |
| 544 | case ENCODING_Ia: |
Sean Callanan | be192dd | 2010-05-05 22:47:27 +0000 | [diff] [blame] | 545 | translateImmediate(mcInst, |
| 546 | insn.immediates[insn.numImmediatesTranslated++], |
| 547 | operand, |
| 548 | insn); |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 549 | return false; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 550 | case ENCODING_RB: |
| 551 | case ENCODING_RW: |
| 552 | case ENCODING_RD: |
| 553 | case ENCODING_RO: |
| 554 | translateRegister(mcInst, insn.opcodeRegister); |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 555 | return false; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 556 | case ENCODING_I: |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 557 | return translateFPRegister(mcInst, insn.opcodeModifier); |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 558 | case ENCODING_Rv: |
| 559 | translateRegister(mcInst, insn.opcodeRegister); |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 560 | return false; |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 561 | case ENCODING_VVVV: |
| 562 | translateRegister(mcInst, insn.vvvv); |
| 563 | return false; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 564 | case ENCODING_DUP: |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 565 | return translateOperand(mcInst, |
| 566 | insn.spec->operands[operand.type - TYPE_DUP0], |
| 567 | insn); |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
| 571 | /// translateInstruction - Translates an internal instruction and all its |
| 572 | /// operands to an MCInst. |
| 573 | /// |
| 574 | /// @param mcInst - The MCInst to populate with the instruction's data. |
| 575 | /// @param insn - The internal instruction. |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 576 | /// @return - false on success; true otherwise. |
| 577 | static bool translateInstruction(MCInst &mcInst, |
| 578 | InternalInstruction &insn) { |
| 579 | if (!insn.spec) { |
| 580 | debug("Instruction has no specification"); |
| 581 | return true; |
| 582 | } |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 583 | |
| 584 | mcInst.setOpcode(insn.instructionID); |
| 585 | |
| 586 | int index; |
| 587 | |
| 588 | insn.numImmediatesTranslated = 0; |
| 589 | |
| 590 | for (index = 0; index < X86_MAX_OPERANDS; ++index) { |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 591 | if (insn.spec->operands[index].encoding != ENCODING_NONE) { |
| 592 | if (translateOperand(mcInst, insn.spec->operands[index], insn)) { |
| 593 | return true; |
| 594 | } |
| 595 | } |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 596 | } |
Sean Callanan | a144c3f | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 597 | |
| 598 | return false; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 599 | } |
Daniel Dunbar | 5f9b9ef | 2009-11-25 06:53:08 +0000 | [diff] [blame] | 600 | |
Benjamin Kramer | 953362c | 2012-02-11 14:50:54 +0000 | [diff] [blame] | 601 | static MCDisassembler *createX86_32Disassembler(const Target &T, |
| 602 | const MCSubtargetInfo &STI) { |
| 603 | return new X86Disassembler::X86GenericDisassembler(STI, MODE_32BIT, |
| 604 | T.createMCInstrInfo()); |
Daniel Dunbar | 5f9b9ef | 2009-11-25 06:53:08 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Benjamin Kramer | 953362c | 2012-02-11 14:50:54 +0000 | [diff] [blame] | 607 | static MCDisassembler *createX86_64Disassembler(const Target &T, |
| 608 | const MCSubtargetInfo &STI) { |
| 609 | return new X86Disassembler::X86GenericDisassembler(STI, MODE_64BIT, |
| 610 | T.createMCInstrInfo()); |
Daniel Dunbar | 5f9b9ef | 2009-11-25 06:53:08 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | extern "C" void LLVMInitializeX86Disassembler() { |
| 614 | // Register the disassembler. |
| 615 | TargetRegistry::RegisterMCDisassembler(TheX86_32Target, |
| 616 | createX86_32Disassembler); |
| 617 | TargetRegistry::RegisterMCDisassembler(TheX86_64Target, |
| 618 | createX86_64Disassembler); |
| 619 | } |