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