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