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