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