Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1 | //===- X86RecognizableInstr.cpp - Disassembler instruction spec --*- 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 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is part of the X86 Disassembler Emitter. |
| 11 | // It contains the implementation of a single recognizable instruction. |
| 12 | // Documentation for the disassembler emitter in general can be found in |
| 13 | // X86DisasemblerEmitter.h. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "X86DisassemblerShared.h" |
| 18 | #include "X86RecognizableInstr.h" |
| 19 | #include "X86ModRMFilters.h" |
| 20 | |
| 21 | #include "llvm/Support/ErrorHandling.h" |
| 22 | |
| 23 | #include <string> |
| 24 | |
| 25 | using namespace llvm; |
| 26 | |
Sean Callanan | 9492be8 | 2010-02-12 23:39:46 +0000 | [diff] [blame] | 27 | #define MRM_MAPPING \ |
| 28 | MAP(C1, 33) \ |
Chris Lattner | a599de2 | 2010-02-13 00:41:14 +0000 | [diff] [blame] | 29 | MAP(C2, 34) \ |
| 30 | MAP(C3, 35) \ |
| 31 | MAP(C4, 36) \ |
| 32 | MAP(C8, 37) \ |
| 33 | MAP(C9, 38) \ |
| 34 | MAP(E8, 39) \ |
| 35 | MAP(F0, 40) \ |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 36 | MAP(F8, 41) \ |
Rafael Espindola | 87ca0e0 | 2011-02-22 00:35:18 +0000 | [diff] [blame] | 37 | MAP(F9, 42) \ |
| 38 | MAP(D0, 45) \ |
| 39 | MAP(D1, 46) |
Sean Callanan | 9492be8 | 2010-02-12 23:39:46 +0000 | [diff] [blame] | 40 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 41 | // A clone of X86 since we can't depend on something that is generated. |
| 42 | namespace X86Local { |
| 43 | enum { |
| 44 | Pseudo = 0, |
| 45 | RawFrm = 1, |
| 46 | AddRegFrm = 2, |
| 47 | MRMDestReg = 3, |
| 48 | MRMDestMem = 4, |
| 49 | MRMSrcReg = 5, |
| 50 | MRMSrcMem = 6, |
| 51 | MRM0r = 16, MRM1r = 17, MRM2r = 18, MRM3r = 19, |
| 52 | MRM4r = 20, MRM5r = 21, MRM6r = 22, MRM7r = 23, |
| 53 | MRM0m = 24, MRM1m = 25, MRM2m = 26, MRM3m = 27, |
| 54 | MRM4m = 28, MRM5m = 29, MRM6m = 30, MRM7m = 31, |
Sean Callanan | 9492be8 | 2010-02-12 23:39:46 +0000 | [diff] [blame] | 55 | MRMInitReg = 32, |
Sean Callanan | 9492be8 | 2010-02-12 23:39:46 +0000 | [diff] [blame] | 56 | #define MAP(from, to) MRM_##from = to, |
| 57 | MRM_MAPPING |
| 58 | #undef MAP |
Sean Callanan | 6aeb2e3 | 2010-10-04 22:45:51 +0000 | [diff] [blame] | 59 | RawFrmImm8 = 43, |
| 60 | RawFrmImm16 = 44, |
Sean Callanan | 9492be8 | 2010-02-12 23:39:46 +0000 | [diff] [blame] | 61 | lastMRM |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | enum { |
| 65 | TB = 1, |
| 66 | REP = 2, |
| 67 | D8 = 3, D9 = 4, DA = 5, DB = 6, |
| 68 | DC = 7, DD = 8, DE = 9, DF = 10, |
| 69 | XD = 11, XS = 12, |
Chris Lattner | 0d8db8e | 2010-02-12 02:06:33 +0000 | [diff] [blame] | 70 | T8 = 13, P_TA = 14, |
Kevin Enderby | fff64ca | 2011-08-29 22:06:28 +0000 | [diff] [blame] | 71 | A6 = 15, A7 = 16, TF = 17 |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 72 | }; |
| 73 | } |
Sean Callanan | 9492be8 | 2010-02-12 23:39:46 +0000 | [diff] [blame] | 74 | |
| 75 | // If rows are added to the opcode extension tables, then corresponding entries |
| 76 | // must be added here. |
| 77 | // |
| 78 | // If the row corresponds to a single byte (i.e., 8f), then add an entry for |
| 79 | // that byte to ONE_BYTE_EXTENSION_TABLES. |
| 80 | // |
| 81 | // If the row corresponds to two bytes where the first is 0f, add an entry for |
| 82 | // the second byte to TWO_BYTE_EXTENSION_TABLES. |
| 83 | // |
| 84 | // If the row corresponds to some other set of bytes, you will need to modify |
| 85 | // the code in RecognizableInstr::emitDecodePath() as well, and add new prefixes |
| 86 | // to the X86 TD files, except in two cases: if the first two bytes of such a |
| 87 | // new combination are 0f 38 or 0f 3a, you just have to add maps called |
| 88 | // THREE_BYTE_38_EXTENSION_TABLES and THREE_BYTE_3A_EXTENSION_TABLES and add a |
| 89 | // switch(Opcode) just below the case X86Local::T8: or case X86Local::TA: line |
| 90 | // in RecognizableInstr::emitDecodePath(). |
| 91 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 92 | #define ONE_BYTE_EXTENSION_TABLES \ |
| 93 | EXTENSION_TABLE(80) \ |
| 94 | EXTENSION_TABLE(81) \ |
| 95 | EXTENSION_TABLE(82) \ |
| 96 | EXTENSION_TABLE(83) \ |
| 97 | EXTENSION_TABLE(8f) \ |
| 98 | EXTENSION_TABLE(c0) \ |
| 99 | EXTENSION_TABLE(c1) \ |
| 100 | EXTENSION_TABLE(c6) \ |
| 101 | EXTENSION_TABLE(c7) \ |
| 102 | EXTENSION_TABLE(d0) \ |
| 103 | EXTENSION_TABLE(d1) \ |
| 104 | EXTENSION_TABLE(d2) \ |
| 105 | EXTENSION_TABLE(d3) \ |
| 106 | EXTENSION_TABLE(f6) \ |
| 107 | EXTENSION_TABLE(f7) \ |
| 108 | EXTENSION_TABLE(fe) \ |
| 109 | EXTENSION_TABLE(ff) |
| 110 | |
| 111 | #define TWO_BYTE_EXTENSION_TABLES \ |
| 112 | EXTENSION_TABLE(00) \ |
| 113 | EXTENSION_TABLE(01) \ |
| 114 | EXTENSION_TABLE(18) \ |
| 115 | EXTENSION_TABLE(71) \ |
| 116 | EXTENSION_TABLE(72) \ |
| 117 | EXTENSION_TABLE(73) \ |
| 118 | EXTENSION_TABLE(ae) \ |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 119 | EXTENSION_TABLE(ba) \ |
| 120 | EXTENSION_TABLE(c7) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 121 | |
Craig Topper | 566f233 | 2011-10-15 20:46:47 +0000 | [diff] [blame] | 122 | #define THREE_BYTE_38_EXTENSION_TABLES \ |
| 123 | EXTENSION_TABLE(F3) |
| 124 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 125 | using namespace X86Disassembler; |
| 126 | |
| 127 | /// needsModRMForDecode - Indicates whether a particular instruction requires a |
| 128 | /// ModR/M byte for the instruction to be properly decoded. For example, a |
| 129 | /// MRMDestReg instruction needs the Mod field in the ModR/M byte to be set to |
| 130 | /// 0b11. |
| 131 | /// |
| 132 | /// @param form - The form of the instruction. |
| 133 | /// @return - true if the form implies that a ModR/M byte is required, false |
| 134 | /// otherwise. |
| 135 | static bool needsModRMForDecode(uint8_t form) { |
| 136 | if (form == X86Local::MRMDestReg || |
| 137 | form == X86Local::MRMDestMem || |
| 138 | form == X86Local::MRMSrcReg || |
| 139 | form == X86Local::MRMSrcMem || |
| 140 | (form >= X86Local::MRM0r && form <= X86Local::MRM7r) || |
| 141 | (form >= X86Local::MRM0m && form <= X86Local::MRM7m)) |
| 142 | return true; |
| 143 | else |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | /// isRegFormat - Indicates whether a particular form requires the Mod field of |
| 148 | /// the ModR/M byte to be 0b11. |
| 149 | /// |
| 150 | /// @param form - The form of the instruction. |
| 151 | /// @return - true if the form implies that Mod must be 0b11, false |
| 152 | /// otherwise. |
| 153 | static bool isRegFormat(uint8_t form) { |
| 154 | if (form == X86Local::MRMDestReg || |
| 155 | form == X86Local::MRMSrcReg || |
| 156 | (form >= X86Local::MRM0r && form <= X86Local::MRM7r)) |
| 157 | return true; |
| 158 | else |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | /// byteFromBitsInit - Extracts a value at most 8 bits in width from a BitsInit. |
| 163 | /// Useful for switch statements and the like. |
| 164 | /// |
| 165 | /// @param init - A reference to the BitsInit to be decoded. |
| 166 | /// @return - The field, with the first bit in the BitsInit as the lowest |
| 167 | /// order bit. |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 168 | static uint8_t byteFromBitsInit(BitsInit &init) { |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 169 | int width = init.getNumBits(); |
| 170 | |
| 171 | assert(width <= 8 && "Field is too large for uint8_t!"); |
| 172 | |
| 173 | int index; |
| 174 | uint8_t mask = 0x01; |
| 175 | |
| 176 | uint8_t ret = 0; |
| 177 | |
| 178 | for (index = 0; index < width; index++) { |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 179 | if (static_cast<BitInit*>(init.getBit(index))->getValue()) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 180 | ret |= mask; |
| 181 | |
| 182 | mask <<= 1; |
| 183 | } |
| 184 | |
| 185 | return ret; |
| 186 | } |
| 187 | |
| 188 | /// byteFromRec - Extract a value at most 8 bits in with from a Record given the |
| 189 | /// name of the field. |
| 190 | /// |
| 191 | /// @param rec - The record from which to extract the value. |
| 192 | /// @param name - The name of the field in the record. |
| 193 | /// @return - The field, as translated by byteFromBitsInit(). |
| 194 | static uint8_t byteFromRec(const Record* rec, const std::string &name) { |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 195 | BitsInit* bits = rec->getValueAsBitsInit(name); |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 196 | return byteFromBitsInit(*bits); |
| 197 | } |
| 198 | |
| 199 | RecognizableInstr::RecognizableInstr(DisassemblerTables &tables, |
| 200 | const CodeGenInstruction &insn, |
| 201 | InstrUID uid) { |
| 202 | UID = uid; |
| 203 | |
| 204 | Rec = insn.TheDef; |
| 205 | Name = Rec->getName(); |
| 206 | Spec = &tables.specForUID(UID); |
| 207 | |
| 208 | if (!Rec->isSubClassOf("X86Inst")) { |
| 209 | ShouldBeEmitted = false; |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | Prefix = byteFromRec(Rec, "Prefix"); |
| 214 | Opcode = byteFromRec(Rec, "Opcode"); |
| 215 | Form = byteFromRec(Rec, "FormBits"); |
| 216 | SegOvr = byteFromRec(Rec, "SegOvrBits"); |
| 217 | |
| 218 | HasOpSizePrefix = Rec->getValueAsBit("hasOpSizePrefix"); |
| 219 | HasREX_WPrefix = Rec->getValueAsBit("hasREX_WPrefix"); |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 220 | HasVEXPrefix = Rec->getValueAsBit("hasVEXPrefix"); |
Bruno Cardoso Lopes | 99405df | 2010-06-08 22:51:23 +0000 | [diff] [blame] | 221 | HasVEX_4VPrefix = Rec->getValueAsBit("hasVEX_4VPrefix"); |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 222 | HasVEX_WPrefix = Rec->getValueAsBit("hasVEX_WPrefix"); |
Craig Topper | 6744a17 | 2011-10-04 06:30:42 +0000 | [diff] [blame] | 223 | IgnoresVEX_L = Rec->getValueAsBit("ignoresVEX_L"); |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 224 | HasLockPrefix = Rec->getValueAsBit("hasLockPrefix"); |
| 225 | IsCodeGenOnly = Rec->getValueAsBit("isCodeGenOnly"); |
| 226 | |
| 227 | Name = Rec->getName(); |
| 228 | AsmString = Rec->getValueAsString("AsmString"); |
| 229 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 230 | Operands = &insn.Operands.OperandList; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 231 | |
Kevin Enderby | 98f213c | 2011-09-02 18:03:03 +0000 | [diff] [blame] | 232 | IsSSE = (HasOpSizePrefix && (Name.find("16") == Name.npos)) || |
| 233 | (Name.find("CRC32") != Name.npos); |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 234 | HasFROperands = hasFROperands(); |
| 235 | HasVEX_LPrefix = has256BitOperands() || Rec->getValueAsBit("hasVEX_L"); |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 236 | |
Eli Friedman | 7105259 | 2011-07-16 02:41:28 +0000 | [diff] [blame] | 237 | // Check for 64-bit inst which does not require REX |
Craig Topper | 4da632e | 2011-09-23 06:57:25 +0000 | [diff] [blame] | 238 | Is32Bit = false; |
Eli Friedman | 7105259 | 2011-07-16 02:41:28 +0000 | [diff] [blame] | 239 | Is64Bit = false; |
| 240 | // FIXME: Is there some better way to check for In64BitMode? |
| 241 | std::vector<Record*> Predicates = Rec->getValueAsListOfDefs("Predicates"); |
| 242 | for (unsigned i = 0, e = Predicates.size(); i != e; ++i) { |
Craig Topper | 4da632e | 2011-09-23 06:57:25 +0000 | [diff] [blame] | 243 | if (Predicates[i]->getName().find("32Bit") != Name.npos) { |
| 244 | Is32Bit = true; |
| 245 | break; |
| 246 | } |
Eli Friedman | 7105259 | 2011-07-16 02:41:28 +0000 | [diff] [blame] | 247 | if (Predicates[i]->getName().find("64Bit") != Name.npos) { |
| 248 | Is64Bit = true; |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | // FIXME: These instructions aren't marked as 64-bit in any way |
| 253 | Is64Bit |= Rec->getName() == "JMP64pcrel32" || |
| 254 | Rec->getName() == "MASKMOVDQU64" || |
| 255 | Rec->getName() == "POPFS64" || |
| 256 | Rec->getName() == "POPGS64" || |
| 257 | Rec->getName() == "PUSHFS64" || |
| 258 | Rec->getName() == "PUSHGS64" || |
| 259 | Rec->getName() == "REX64_PREFIX" || |
| 260 | Rec->getName().find("VMREAD64") != Name.npos || |
| 261 | Rec->getName().find("VMWRITE64") != Name.npos || |
Craig Topper | 846a2dc | 2011-10-01 21:20:14 +0000 | [diff] [blame] | 262 | Rec->getName().find("INVEPT64") != Name.npos || |
| 263 | Rec->getName().find("INVVPID64") != Name.npos || |
Eli Friedman | 7105259 | 2011-07-16 02:41:28 +0000 | [diff] [blame] | 264 | Rec->getName().find("MOV64") != Name.npos || |
| 265 | Rec->getName().find("PUSH64") != Name.npos || |
| 266 | Rec->getName().find("POP64") != Name.npos; |
| 267 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 268 | ShouldBeEmitted = true; |
| 269 | } |
| 270 | |
| 271 | void RecognizableInstr::processInstr(DisassemblerTables &tables, |
Kevin Enderby | fff64ca | 2011-08-29 22:06:28 +0000 | [diff] [blame] | 272 | const CodeGenInstruction &insn, |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 273 | InstrUID uid) |
| 274 | { |
Daniel Dunbar | 4072886 | 2010-05-20 20:20:32 +0000 | [diff] [blame] | 275 | // Ignore "asm parser only" instructions. |
| 276 | if (insn.TheDef->getValueAsBit("isAsmParserOnly")) |
| 277 | return; |
| 278 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 279 | RecognizableInstr recogInstr(tables, insn, uid); |
| 280 | |
| 281 | recogInstr.emitInstructionSpecifier(tables); |
| 282 | |
| 283 | if (recogInstr.shouldBeEmitted()) |
| 284 | recogInstr.emitDecodePath(tables); |
| 285 | } |
| 286 | |
| 287 | InstructionContext RecognizableInstr::insnContext() const { |
| 288 | InstructionContext insnContext; |
| 289 | |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 290 | if (HasVEX_4VPrefix || HasVEXPrefix) { |
Craig Topper | 6744a17 | 2011-10-04 06:30:42 +0000 | [diff] [blame] | 291 | if (HasVEX_LPrefix && HasVEX_WPrefix) |
| 292 | llvm_unreachable("Don't support VEX.L and VEX.W together"); |
| 293 | else if (HasOpSizePrefix && HasVEX_LPrefix) |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 294 | insnContext = IC_VEX_L_OPSIZE; |
| 295 | else if (HasOpSizePrefix && HasVEX_WPrefix) |
| 296 | insnContext = IC_VEX_W_OPSIZE; |
| 297 | else if (HasOpSizePrefix) |
| 298 | insnContext = IC_VEX_OPSIZE; |
| 299 | else if (HasVEX_LPrefix && Prefix == X86Local::XS) |
| 300 | insnContext = IC_VEX_L_XS; |
| 301 | else if (HasVEX_LPrefix && Prefix == X86Local::XD) |
| 302 | insnContext = IC_VEX_L_XD; |
| 303 | else if (HasVEX_WPrefix && Prefix == X86Local::XS) |
| 304 | insnContext = IC_VEX_W_XS; |
| 305 | else if (HasVEX_WPrefix && Prefix == X86Local::XD) |
| 306 | insnContext = IC_VEX_W_XD; |
| 307 | else if (HasVEX_WPrefix) |
| 308 | insnContext = IC_VEX_W; |
| 309 | else if (HasVEX_LPrefix) |
| 310 | insnContext = IC_VEX_L; |
| 311 | else if (Prefix == X86Local::XD) |
| 312 | insnContext = IC_VEX_XD; |
| 313 | else if (Prefix == X86Local::XS) |
| 314 | insnContext = IC_VEX_XS; |
| 315 | else |
| 316 | insnContext = IC_VEX; |
Eli Friedman | 7105259 | 2011-07-16 02:41:28 +0000 | [diff] [blame] | 317 | } else if (Is64Bit || HasREX_WPrefix) { |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 318 | if (HasREX_WPrefix && HasOpSizePrefix) |
| 319 | insnContext = IC_64BIT_REXW_OPSIZE; |
Craig Topper | 29480fd | 2011-10-11 04:34:23 +0000 | [diff] [blame] | 320 | else if (HasOpSizePrefix && |
| 321 | (Prefix == X86Local::XD || Prefix == X86Local::TF)) |
Craig Topper | e1b4a1a | 2011-10-01 19:54:56 +0000 | [diff] [blame] | 322 | insnContext = IC_64BIT_XD_OPSIZE; |
Craig Topper | 29480fd | 2011-10-11 04:34:23 +0000 | [diff] [blame] | 323 | else if (HasOpSizePrefix && Prefix == X86Local::XS) |
| 324 | insnContext = IC_64BIT_XS_OPSIZE; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 325 | else if (HasOpSizePrefix) |
| 326 | insnContext = IC_64BIT_OPSIZE; |
| 327 | else if (HasREX_WPrefix && Prefix == X86Local::XS) |
| 328 | insnContext = IC_64BIT_REXW_XS; |
Craig Topper | 29480fd | 2011-10-11 04:34:23 +0000 | [diff] [blame] | 329 | else if (HasREX_WPrefix && |
| 330 | (Prefix == X86Local::XD || Prefix == X86Local::TF)) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 331 | insnContext = IC_64BIT_REXW_XD; |
Craig Topper | e1b4a1a | 2011-10-01 19:54:56 +0000 | [diff] [blame] | 332 | else if (Prefix == X86Local::XD || Prefix == X86Local::TF) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 333 | insnContext = IC_64BIT_XD; |
| 334 | else if (Prefix == X86Local::XS) |
| 335 | insnContext = IC_64BIT_XS; |
| 336 | else if (HasREX_WPrefix) |
| 337 | insnContext = IC_64BIT_REXW; |
| 338 | else |
| 339 | insnContext = IC_64BIT; |
| 340 | } else { |
Craig Topper | e1b4a1a | 2011-10-01 19:54:56 +0000 | [diff] [blame] | 341 | if (HasOpSizePrefix && |
| 342 | (Prefix == X86Local::XD || Prefix == X86Local::TF)) |
| 343 | insnContext = IC_XD_OPSIZE; |
Craig Topper | 29480fd | 2011-10-11 04:34:23 +0000 | [diff] [blame] | 344 | else if (HasOpSizePrefix && Prefix == X86Local::XS) |
| 345 | insnContext = IC_XS_OPSIZE; |
Kevin Enderby | 98f213c | 2011-09-02 18:03:03 +0000 | [diff] [blame] | 346 | else if (HasOpSizePrefix) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 347 | insnContext = IC_OPSIZE; |
Craig Topper | e1b4a1a | 2011-10-01 19:54:56 +0000 | [diff] [blame] | 348 | else if (Prefix == X86Local::XD || Prefix == X86Local::TF) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 349 | insnContext = IC_XD; |
Craig Topper | 842f58f | 2011-09-11 20:23:20 +0000 | [diff] [blame] | 350 | else if (Prefix == X86Local::XS || Prefix == X86Local::REP) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 351 | insnContext = IC_XS; |
| 352 | else |
| 353 | insnContext = IC; |
| 354 | } |
| 355 | |
| 356 | return insnContext; |
| 357 | } |
| 358 | |
| 359 | RecognizableInstr::filter_ret RecognizableInstr::filter() const { |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 360 | /////////////////// |
| 361 | // FILTER_STRONG |
| 362 | // |
| 363 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 364 | // Filter out intrinsics |
| 365 | |
| 366 | if (!Rec->isSubClassOf("X86Inst")) |
| 367 | return FILTER_STRONG; |
| 368 | |
| 369 | if (Form == X86Local::Pseudo || |
Craig Topper | 0381979 | 2011-09-11 21:41:45 +0000 | [diff] [blame] | 370 | (IsCodeGenOnly && Name.find("_REV") == Name.npos)) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 371 | return FILTER_STRONG; |
| 372 | |
Sean Callanan | 80443f9 | 2010-02-24 02:56:25 +0000 | [diff] [blame] | 373 | if (Form == X86Local::MRMInitReg) |
| 374 | return FILTER_STRONG; |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 375 | |
| 376 | |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 377 | // Filter out artificial instructions |
| 378 | |
| 379 | if (Name.find("TAILJMP") != Name.npos || |
| 380 | Name.find("_Int") != Name.npos || |
| 381 | Name.find("_int") != Name.npos || |
| 382 | Name.find("Int_") != Name.npos || |
| 383 | Name.find("_NOREX") != Name.npos || |
| 384 | Name.find("_TC") != Name.npos || |
| 385 | Name.find("EH_RETURN") != Name.npos || |
| 386 | Name.find("V_SET") != Name.npos || |
| 387 | Name.find("LOCK_") != Name.npos || |
| 388 | Name.find("WIN") != Name.npos || |
| 389 | Name.find("_AVX") != Name.npos || |
| 390 | Name.find("2SDL") != Name.npos) |
| 391 | return FILTER_STRONG; |
| 392 | |
| 393 | // Filter out instructions with segment override prefixes. |
| 394 | // They're too messy to handle now and we'll special case them if needed. |
| 395 | |
| 396 | if (SegOvr) |
| 397 | return FILTER_STRONG; |
| 398 | |
| 399 | // Filter out instructions that can't be printed. |
| 400 | |
| 401 | if (AsmString.size() == 0) |
| 402 | return FILTER_STRONG; |
| 403 | |
| 404 | // Filter out instructions with subreg operands. |
| 405 | |
| 406 | if (AsmString.find("subreg") != AsmString.npos) |
| 407 | return FILTER_STRONG; |
| 408 | |
| 409 | ///////////////// |
| 410 | // FILTER_WEAK |
| 411 | // |
| 412 | |
| 413 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 414 | // Filter out instructions with a LOCK prefix; |
| 415 | // prefer forms that do not have the prefix |
| 416 | if (HasLockPrefix) |
| 417 | return FILTER_WEAK; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 418 | |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 419 | // Filter out alternate forms of AVX instructions |
| 420 | if (Name.find("_alt") != Name.npos || |
| 421 | Name.find("XrYr") != Name.npos || |
Craig Topper | e1b4a1a | 2011-10-01 19:54:56 +0000 | [diff] [blame] | 422 | (Name.find("r64r") != Name.npos && Name.find("r64r64") == Name.npos) || |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 423 | Name.find("_64mr") != Name.npos || |
| 424 | Name.find("Xrr") != Name.npos || |
| 425 | Name.find("rr64") != Name.npos) |
| 426 | return FILTER_WEAK; |
| 427 | |
| 428 | if (Name == "VMASKMOVDQU64" || |
| 429 | Name == "VEXTRACTPSrr64" || |
| 430 | Name == "VMOVQd64rr" || |
| 431 | Name == "VMOVQs64rr") |
| 432 | return FILTER_WEAK; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 433 | |
| 434 | // Special cases. |
Dale Johannesen | 86097c3 | 2010-09-07 18:10:56 +0000 | [diff] [blame] | 435 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 436 | if (Name.find("PCMPISTRI") != Name.npos && Name != "PCMPISTRI") |
| 437 | return FILTER_WEAK; |
| 438 | if (Name.find("PCMPESTRI") != Name.npos && Name != "PCMPESTRI") |
| 439 | return FILTER_WEAK; |
| 440 | |
| 441 | if (Name.find("MOV") != Name.npos && Name.find("r0") != Name.npos) |
| 442 | return FILTER_WEAK; |
| 443 | if (Name.find("MOVZ") != Name.npos && Name.find("MOVZX") == Name.npos) |
| 444 | return FILTER_WEAK; |
| 445 | if (Name.find("Fs") != Name.npos) |
| 446 | return FILTER_WEAK; |
| 447 | if (Name == "MOVLPDrr" || |
| 448 | Name == "MOVLPSrr" || |
| 449 | Name == "PUSHFQ" || |
| 450 | Name == "BSF16rr" || |
| 451 | Name == "BSF16rm" || |
| 452 | Name == "BSR16rr" || |
| 453 | Name == "BSR16rm" || |
| 454 | Name == "MOVSX16rm8" || |
| 455 | Name == "MOVSX16rr8" || |
| 456 | Name == "MOVZX16rm8" || |
| 457 | Name == "MOVZX16rr8" || |
| 458 | Name == "PUSH32i16" || |
| 459 | Name == "PUSH64i16" || |
| 460 | Name == "MOVPQI2QImr" || |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 461 | Name == "VMOVPQI2QImr" || |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 462 | Name == "MOVSDmr" || |
| 463 | Name == "MOVSDrm" || |
| 464 | Name == "MOVSSmr" || |
| 465 | Name == "MOVSSrm" || |
| 466 | Name == "MMX_MOVD64rrv164" || |
| 467 | Name == "CRC32m16" || |
| 468 | Name == "MOV64ri64i32" || |
| 469 | Name == "CRC32r16") |
| 470 | return FILTER_WEAK; |
| 471 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 472 | if (HasFROperands && Name.find("MOV") != Name.npos && |
| 473 | ((Name.find("2") != Name.npos && Name.find("32") == Name.npos) || |
| 474 | (Name.find("to") != Name.npos))) |
| 475 | return FILTER_WEAK; |
| 476 | |
| 477 | return FILTER_NORMAL; |
| 478 | } |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 479 | |
| 480 | bool RecognizableInstr::hasFROperands() const { |
| 481 | const std::vector<CGIOperandList::OperandInfo> &OperandList = *Operands; |
| 482 | unsigned numOperands = OperandList.size(); |
| 483 | |
| 484 | for (unsigned operandIndex = 0; operandIndex < numOperands; ++operandIndex) { |
| 485 | const std::string &recName = OperandList[operandIndex].Rec->getName(); |
| 486 | |
| 487 | if (recName.find("FR") != recName.npos) |
| 488 | return true; |
| 489 | } |
| 490 | return false; |
| 491 | } |
| 492 | |
| 493 | bool RecognizableInstr::has256BitOperands() const { |
| 494 | const std::vector<CGIOperandList::OperandInfo> &OperandList = *Operands; |
| 495 | unsigned numOperands = OperandList.size(); |
| 496 | |
| 497 | for (unsigned operandIndex = 0; operandIndex < numOperands; ++operandIndex) { |
| 498 | const std::string &recName = OperandList[operandIndex].Rec->getName(); |
| 499 | |
| 500 | if (!recName.compare("VR256") || !recName.compare("f256mem")) { |
| 501 | return true; |
| 502 | } |
| 503 | } |
| 504 | return false; |
| 505 | } |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 506 | |
| 507 | void RecognizableInstr::handleOperand( |
| 508 | bool optional, |
| 509 | unsigned &operandIndex, |
| 510 | unsigned &physicalOperandIndex, |
| 511 | unsigned &numPhysicalOperands, |
| 512 | unsigned *operandMapping, |
| 513 | OperandEncoding (*encodingFromString)(const std::string&, bool hasOpSizePrefix)) { |
| 514 | if (optional) { |
| 515 | if (physicalOperandIndex >= numPhysicalOperands) |
| 516 | return; |
| 517 | } else { |
| 518 | assert(physicalOperandIndex < numPhysicalOperands); |
| 519 | } |
| 520 | |
| 521 | while (operandMapping[operandIndex] != operandIndex) { |
| 522 | Spec->operands[operandIndex].encoding = ENCODING_DUP; |
| 523 | Spec->operands[operandIndex].type = |
| 524 | (OperandType)(TYPE_DUP0 + operandMapping[operandIndex]); |
| 525 | ++operandIndex; |
| 526 | } |
| 527 | |
| 528 | const std::string &typeName = (*Operands)[operandIndex].Rec->getName(); |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 529 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 530 | Spec->operands[operandIndex].encoding = encodingFromString(typeName, |
| 531 | HasOpSizePrefix); |
| 532 | Spec->operands[operandIndex].type = typeFromString(typeName, |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 533 | IsSSE, |
| 534 | HasREX_WPrefix, |
| 535 | HasOpSizePrefix); |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 536 | |
| 537 | ++operandIndex; |
| 538 | ++physicalOperandIndex; |
| 539 | } |
| 540 | |
| 541 | void RecognizableInstr::emitInstructionSpecifier(DisassemblerTables &tables) { |
| 542 | Spec->name = Name; |
| 543 | |
| 544 | if (!Rec->isSubClassOf("X86Inst")) |
| 545 | return; |
| 546 | |
| 547 | switch (filter()) { |
| 548 | case FILTER_WEAK: |
| 549 | Spec->filtered = true; |
| 550 | break; |
| 551 | case FILTER_STRONG: |
| 552 | ShouldBeEmitted = false; |
| 553 | return; |
| 554 | case FILTER_NORMAL: |
| 555 | break; |
| 556 | } |
| 557 | |
| 558 | Spec->insnContext = insnContext(); |
| 559 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 560 | const std::vector<CGIOperandList::OperandInfo> &OperandList = *Operands; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 561 | |
| 562 | unsigned operandIndex; |
| 563 | unsigned numOperands = OperandList.size(); |
| 564 | unsigned numPhysicalOperands = 0; |
| 565 | |
| 566 | // operandMapping maps from operands in OperandList to their originals. |
| 567 | // If operandMapping[i] != i, then the entry is a duplicate. |
| 568 | unsigned operandMapping[X86_MAX_OPERANDS]; |
| 569 | |
| 570 | bool hasFROperands = false; |
| 571 | |
| 572 | assert(numOperands < X86_MAX_OPERANDS && "X86_MAX_OPERANDS is not large enough"); |
| 573 | |
| 574 | for (operandIndex = 0; operandIndex < numOperands; ++operandIndex) { |
| 575 | if (OperandList[operandIndex].Constraints.size()) { |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 576 | const CGIOperandList::ConstraintInfo &Constraint = |
Chris Lattner | a7d479c | 2010-02-10 01:45:28 +0000 | [diff] [blame] | 577 | OperandList[operandIndex].Constraints[0]; |
| 578 | if (Constraint.isTied()) { |
| 579 | operandMapping[operandIndex] = Constraint.getTiedOperand(); |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 580 | } else { |
| 581 | ++numPhysicalOperands; |
| 582 | operandMapping[operandIndex] = operandIndex; |
| 583 | } |
| 584 | } else { |
| 585 | ++numPhysicalOperands; |
| 586 | operandMapping[operandIndex] = operandIndex; |
| 587 | } |
| 588 | |
| 589 | const std::string &recName = OperandList[operandIndex].Rec->getName(); |
| 590 | |
| 591 | if (recName.find("FR") != recName.npos) |
| 592 | hasFROperands = true; |
| 593 | } |
| 594 | |
| 595 | if (hasFROperands && Name.find("MOV") != Name.npos && |
| 596 | ((Name.find("2") != Name.npos && Name.find("32") == Name.npos) || |
| 597 | (Name.find("to") != Name.npos))) |
| 598 | ShouldBeEmitted = false; |
| 599 | |
| 600 | if (!ShouldBeEmitted) |
| 601 | return; |
| 602 | |
| 603 | #define HANDLE_OPERAND(class) \ |
| 604 | handleOperand(false, \ |
| 605 | operandIndex, \ |
| 606 | physicalOperandIndex, \ |
| 607 | numPhysicalOperands, \ |
| 608 | operandMapping, \ |
| 609 | class##EncodingFromString); |
| 610 | |
| 611 | #define HANDLE_OPTIONAL(class) \ |
| 612 | handleOperand(true, \ |
| 613 | operandIndex, \ |
| 614 | physicalOperandIndex, \ |
| 615 | numPhysicalOperands, \ |
| 616 | operandMapping, \ |
| 617 | class##EncodingFromString); |
| 618 | |
| 619 | // operandIndex should always be < numOperands |
| 620 | operandIndex = 0; |
| 621 | // physicalOperandIndex should always be < numPhysicalOperands |
| 622 | unsigned physicalOperandIndex = 0; |
| 623 | |
| 624 | switch (Form) { |
| 625 | case X86Local::RawFrm: |
| 626 | // Operand 1 (optional) is an address or immediate. |
| 627 | // Operand 2 (optional) is an immediate. |
| 628 | assert(numPhysicalOperands <= 2 && |
| 629 | "Unexpected number of operands for RawFrm"); |
| 630 | HANDLE_OPTIONAL(relocation) |
| 631 | HANDLE_OPTIONAL(immediate) |
| 632 | break; |
| 633 | case X86Local::AddRegFrm: |
| 634 | // Operand 1 is added to the opcode. |
| 635 | // Operand 2 (optional) is an address. |
| 636 | assert(numPhysicalOperands >= 1 && numPhysicalOperands <= 2 && |
| 637 | "Unexpected number of operands for AddRegFrm"); |
| 638 | HANDLE_OPERAND(opcodeModifier) |
| 639 | HANDLE_OPTIONAL(relocation) |
| 640 | break; |
| 641 | case X86Local::MRMDestReg: |
| 642 | // Operand 1 is a register operand in the R/M field. |
| 643 | // Operand 2 is a register operand in the Reg/Opcode field. |
Craig Topper | 3daa5c2 | 2011-08-30 07:09:35 +0000 | [diff] [blame] | 644 | // - In AVX, there is a register operand in the VEX.vvvv field here - |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 645 | // Operand 3 (optional) is an immediate. |
Craig Topper | 3daa5c2 | 2011-08-30 07:09:35 +0000 | [diff] [blame] | 646 | if (HasVEX_4VPrefix) |
| 647 | assert(numPhysicalOperands >= 3 && numPhysicalOperands <= 4 && |
| 648 | "Unexpected number of operands for MRMDestRegFrm with VEX_4V"); |
| 649 | else |
| 650 | assert(numPhysicalOperands >= 2 && numPhysicalOperands <= 3 && |
| 651 | "Unexpected number of operands for MRMDestRegFrm"); |
| 652 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 653 | HANDLE_OPERAND(rmRegister) |
Craig Topper | 3daa5c2 | 2011-08-30 07:09:35 +0000 | [diff] [blame] | 654 | |
| 655 | if (HasVEX_4VPrefix) |
| 656 | // FIXME: In AVX, the register below becomes the one encoded |
| 657 | // in ModRMVEX and the one above the one in the VEX.VVVV field |
| 658 | HANDLE_OPERAND(vvvvRegister) |
| 659 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 660 | HANDLE_OPERAND(roRegister) |
| 661 | HANDLE_OPTIONAL(immediate) |
| 662 | break; |
| 663 | case X86Local::MRMDestMem: |
| 664 | // Operand 1 is a memory operand (possibly SIB-extended) |
| 665 | // Operand 2 is a register operand in the Reg/Opcode field. |
Craig Topper | 3daa5c2 | 2011-08-30 07:09:35 +0000 | [diff] [blame] | 666 | // - In AVX, there is a register operand in the VEX.vvvv field here - |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 667 | // Operand 3 (optional) is an immediate. |
Craig Topper | 3daa5c2 | 2011-08-30 07:09:35 +0000 | [diff] [blame] | 668 | if (HasVEX_4VPrefix) |
| 669 | assert(numPhysicalOperands >= 3 && numPhysicalOperands <= 4 && |
| 670 | "Unexpected number of operands for MRMDestMemFrm with VEX_4V"); |
| 671 | else |
| 672 | assert(numPhysicalOperands >= 2 && numPhysicalOperands <= 3 && |
| 673 | "Unexpected number of operands for MRMDestMemFrm"); |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 674 | HANDLE_OPERAND(memory) |
Craig Topper | 3daa5c2 | 2011-08-30 07:09:35 +0000 | [diff] [blame] | 675 | |
| 676 | if (HasVEX_4VPrefix) |
| 677 | // FIXME: In AVX, the register below becomes the one encoded |
| 678 | // in ModRMVEX and the one above the one in the VEX.VVVV field |
| 679 | HANDLE_OPERAND(vvvvRegister) |
| 680 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 681 | HANDLE_OPERAND(roRegister) |
| 682 | HANDLE_OPTIONAL(immediate) |
| 683 | break; |
| 684 | case X86Local::MRMSrcReg: |
| 685 | // Operand 1 is a register operand in the Reg/Opcode field. |
| 686 | // Operand 2 is a register operand in the R/M field. |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 687 | // - In AVX, there is a register operand in the VEX.vvvv field here - |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 688 | // Operand 3 (optional) is an immediate. |
Bruno Cardoso Lopes | 99405df | 2010-06-08 22:51:23 +0000 | [diff] [blame] | 689 | |
| 690 | if (HasVEX_4VPrefix) |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 691 | assert(numPhysicalOperands >= 3 && numPhysicalOperands <= 4 && |
| 692 | "Unexpected number of operands for MRMSrcRegFrm with VEX_4V"); |
| 693 | else |
| 694 | assert(numPhysicalOperands >= 2 && numPhysicalOperands <= 3 && |
| 695 | "Unexpected number of operands for MRMSrcRegFrm"); |
| 696 | |
| 697 | HANDLE_OPERAND(roRegister) |
| 698 | |
| 699 | if (HasVEX_4VPrefix) |
Bruno Cardoso Lopes | c902a59 | 2010-06-11 23:50:47 +0000 | [diff] [blame] | 700 | // FIXME: In AVX, the register below becomes the one encoded |
| 701 | // in ModRMVEX and the one above the one in the VEX.VVVV field |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 702 | HANDLE_OPERAND(vvvvRegister) |
| 703 | |
| 704 | HANDLE_OPERAND(rmRegister) |
| 705 | HANDLE_OPTIONAL(immediate) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 706 | break; |
| 707 | case X86Local::MRMSrcMem: |
| 708 | // Operand 1 is a register operand in the Reg/Opcode field. |
| 709 | // Operand 2 is a memory operand (possibly SIB-extended) |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 710 | // - In AVX, there is a register operand in the VEX.vvvv field here - |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 711 | // Operand 3 (optional) is an immediate. |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 712 | |
| 713 | if (HasVEX_4VPrefix) |
| 714 | assert(numPhysicalOperands >= 3 && numPhysicalOperands <= 4 && |
| 715 | "Unexpected number of operands for MRMSrcMemFrm with VEX_4V"); |
| 716 | else |
| 717 | assert(numPhysicalOperands >= 2 && numPhysicalOperands <= 3 && |
| 718 | "Unexpected number of operands for MRMSrcMemFrm"); |
| 719 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 720 | HANDLE_OPERAND(roRegister) |
Bruno Cardoso Lopes | c902a59 | 2010-06-11 23:50:47 +0000 | [diff] [blame] | 721 | |
| 722 | if (HasVEX_4VPrefix) |
| 723 | // FIXME: In AVX, the register below becomes the one encoded |
| 724 | // in ModRMVEX and the one above the one in the VEX.VVVV field |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 725 | HANDLE_OPERAND(vvvvRegister) |
Bruno Cardoso Lopes | c902a59 | 2010-06-11 23:50:47 +0000 | [diff] [blame] | 726 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 727 | HANDLE_OPERAND(memory) |
| 728 | HANDLE_OPTIONAL(immediate) |
| 729 | break; |
| 730 | case X86Local::MRM0r: |
| 731 | case X86Local::MRM1r: |
| 732 | case X86Local::MRM2r: |
| 733 | case X86Local::MRM3r: |
| 734 | case X86Local::MRM4r: |
| 735 | case X86Local::MRM5r: |
| 736 | case X86Local::MRM6r: |
| 737 | case X86Local::MRM7r: |
| 738 | // Operand 1 is a register operand in the R/M field. |
| 739 | // Operand 2 (optional) is an immediate or relocation. |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 740 | if (HasVEX_4VPrefix) |
| 741 | assert(numPhysicalOperands <= 3 && |
Craig Topper | 566f233 | 2011-10-15 20:46:47 +0000 | [diff] [blame] | 742 | "Unexpected number of operands for MRMnRFrm with VEX_4V"); |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 743 | else |
| 744 | assert(numPhysicalOperands <= 2 && |
| 745 | "Unexpected number of operands for MRMnRFrm"); |
| 746 | if (HasVEX_4VPrefix) |
Craig Topper | 566f233 | 2011-10-15 20:46:47 +0000 | [diff] [blame] | 747 | HANDLE_OPERAND(vvvvRegister) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 748 | HANDLE_OPTIONAL(rmRegister) |
| 749 | HANDLE_OPTIONAL(relocation) |
| 750 | break; |
| 751 | case X86Local::MRM0m: |
| 752 | case X86Local::MRM1m: |
| 753 | case X86Local::MRM2m: |
| 754 | case X86Local::MRM3m: |
| 755 | case X86Local::MRM4m: |
| 756 | case X86Local::MRM5m: |
| 757 | case X86Local::MRM6m: |
| 758 | case X86Local::MRM7m: |
| 759 | // Operand 1 is a memory operand (possibly SIB-extended) |
| 760 | // Operand 2 (optional) is an immediate or relocation. |
Craig Topper | 566f233 | 2011-10-15 20:46:47 +0000 | [diff] [blame] | 761 | if (HasVEX_4VPrefix) |
| 762 | assert(numPhysicalOperands >= 2 && numPhysicalOperands <= 3 && |
| 763 | "Unexpected number of operands for MRMnMFrm"); |
| 764 | else |
| 765 | assert(numPhysicalOperands >= 1 && numPhysicalOperands <= 2 && |
| 766 | "Unexpected number of operands for MRMnMFrm"); |
| 767 | if (HasVEX_4VPrefix) |
| 768 | HANDLE_OPERAND(vvvvRegister) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 769 | HANDLE_OPERAND(memory) |
| 770 | HANDLE_OPTIONAL(relocation) |
| 771 | break; |
Sean Callanan | 6aeb2e3 | 2010-10-04 22:45:51 +0000 | [diff] [blame] | 772 | case X86Local::RawFrmImm8: |
| 773 | // operand 1 is a 16-bit immediate |
| 774 | // operand 2 is an 8-bit immediate |
| 775 | assert(numPhysicalOperands == 2 && |
| 776 | "Unexpected number of operands for X86Local::RawFrmImm8"); |
| 777 | HANDLE_OPERAND(immediate) |
| 778 | HANDLE_OPERAND(immediate) |
| 779 | break; |
| 780 | case X86Local::RawFrmImm16: |
| 781 | // operand 1 is a 16-bit immediate |
| 782 | // operand 2 is a 16-bit immediate |
| 783 | HANDLE_OPERAND(immediate) |
| 784 | HANDLE_OPERAND(immediate) |
| 785 | break; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 786 | case X86Local::MRMInitReg: |
| 787 | // Ignored. |
| 788 | break; |
| 789 | } |
| 790 | |
| 791 | #undef HANDLE_OPERAND |
| 792 | #undef HANDLE_OPTIONAL |
| 793 | } |
| 794 | |
| 795 | void RecognizableInstr::emitDecodePath(DisassemblerTables &tables) const { |
| 796 | // Special cases where the LLVM tables are not complete |
| 797 | |
Sean Callanan | 9492be8 | 2010-02-12 23:39:46 +0000 | [diff] [blame] | 798 | #define MAP(from, to) \ |
| 799 | case X86Local::MRM_##from: \ |
| 800 | filter = new ExactFilter(0x##from); \ |
| 801 | break; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 802 | |
| 803 | OpcodeType opcodeType = (OpcodeType)-1; |
| 804 | |
| 805 | ModRMFilter* filter = NULL; |
| 806 | uint8_t opcodeToSet = 0; |
| 807 | |
| 808 | switch (Prefix) { |
| 809 | // Extended two-byte opcodes can start with f2 0f, f3 0f, or 0f |
| 810 | case X86Local::XD: |
| 811 | case X86Local::XS: |
| 812 | case X86Local::TB: |
| 813 | opcodeType = TWOBYTE; |
| 814 | |
| 815 | switch (Opcode) { |
Sean Callanan | 95a5a7d | 2010-02-13 01:48:34 +0000 | [diff] [blame] | 816 | default: |
| 817 | if (needsModRMForDecode(Form)) |
| 818 | filter = new ModFilter(isRegFormat(Form)); |
| 819 | else |
| 820 | filter = new DumbFilter(); |
| 821 | break; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 822 | #define EXTENSION_TABLE(n) case 0x##n: |
| 823 | TWO_BYTE_EXTENSION_TABLES |
| 824 | #undef EXTENSION_TABLE |
| 825 | switch (Form) { |
| 826 | default: |
| 827 | llvm_unreachable("Unhandled two-byte extended opcode"); |
| 828 | case X86Local::MRM0r: |
| 829 | case X86Local::MRM1r: |
| 830 | case X86Local::MRM2r: |
| 831 | case X86Local::MRM3r: |
| 832 | case X86Local::MRM4r: |
| 833 | case X86Local::MRM5r: |
| 834 | case X86Local::MRM6r: |
| 835 | case X86Local::MRM7r: |
| 836 | filter = new ExtendedFilter(true, Form - X86Local::MRM0r); |
| 837 | break; |
| 838 | case X86Local::MRM0m: |
| 839 | case X86Local::MRM1m: |
| 840 | case X86Local::MRM2m: |
| 841 | case X86Local::MRM3m: |
| 842 | case X86Local::MRM4m: |
| 843 | case X86Local::MRM5m: |
| 844 | case X86Local::MRM6m: |
| 845 | case X86Local::MRM7m: |
| 846 | filter = new ExtendedFilter(false, Form - X86Local::MRM0m); |
| 847 | break; |
Sean Callanan | 9492be8 | 2010-02-12 23:39:46 +0000 | [diff] [blame] | 848 | MRM_MAPPING |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 849 | } // switch (Form) |
| 850 | break; |
Sean Callanan | 95a5a7d | 2010-02-13 01:48:34 +0000 | [diff] [blame] | 851 | } // switch (Opcode) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 852 | opcodeToSet = Opcode; |
| 853 | break; |
| 854 | case X86Local::T8: |
Kevin Enderby | fff64ca | 2011-08-29 22:06:28 +0000 | [diff] [blame] | 855 | case X86Local::TF: |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 856 | opcodeType = THREEBYTE_38; |
Craig Topper | 566f233 | 2011-10-15 20:46:47 +0000 | [diff] [blame] | 857 | switch (Opcode) { |
| 858 | default: |
| 859 | if (needsModRMForDecode(Form)) |
| 860 | filter = new ModFilter(isRegFormat(Form)); |
| 861 | else |
| 862 | filter = new DumbFilter(); |
| 863 | break; |
| 864 | #define EXTENSION_TABLE(n) case 0x##n: |
| 865 | THREE_BYTE_38_EXTENSION_TABLES |
| 866 | #undef EXTENSION_TABLE |
| 867 | switch (Form) { |
| 868 | default: |
| 869 | llvm_unreachable("Unhandled two-byte extended opcode"); |
| 870 | case X86Local::MRM0r: |
| 871 | case X86Local::MRM1r: |
| 872 | case X86Local::MRM2r: |
| 873 | case X86Local::MRM3r: |
| 874 | case X86Local::MRM4r: |
| 875 | case X86Local::MRM5r: |
| 876 | case X86Local::MRM6r: |
| 877 | case X86Local::MRM7r: |
| 878 | filter = new ExtendedFilter(true, Form - X86Local::MRM0r); |
| 879 | break; |
| 880 | case X86Local::MRM0m: |
| 881 | case X86Local::MRM1m: |
| 882 | case X86Local::MRM2m: |
| 883 | case X86Local::MRM3m: |
| 884 | case X86Local::MRM4m: |
| 885 | case X86Local::MRM5m: |
| 886 | case X86Local::MRM6m: |
| 887 | case X86Local::MRM7m: |
| 888 | filter = new ExtendedFilter(false, Form - X86Local::MRM0m); |
| 889 | break; |
| 890 | MRM_MAPPING |
| 891 | } // switch (Form) |
| 892 | break; |
| 893 | } // switch (Opcode) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 894 | opcodeToSet = Opcode; |
| 895 | break; |
Chris Lattner | 0d8db8e | 2010-02-12 02:06:33 +0000 | [diff] [blame] | 896 | case X86Local::P_TA: |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 897 | opcodeType = THREEBYTE_3A; |
| 898 | if (needsModRMForDecode(Form)) |
| 899 | filter = new ModFilter(isRegFormat(Form)); |
| 900 | else |
| 901 | filter = new DumbFilter(); |
| 902 | opcodeToSet = Opcode; |
| 903 | break; |
Joerg Sonnenberger | 4a8ac8d | 2011-04-04 16:58:13 +0000 | [diff] [blame] | 904 | case X86Local::A6: |
| 905 | opcodeType = THREEBYTE_A6; |
| 906 | if (needsModRMForDecode(Form)) |
| 907 | filter = new ModFilter(isRegFormat(Form)); |
| 908 | else |
| 909 | filter = new DumbFilter(); |
| 910 | opcodeToSet = Opcode; |
| 911 | break; |
| 912 | case X86Local::A7: |
| 913 | opcodeType = THREEBYTE_A7; |
| 914 | if (needsModRMForDecode(Form)) |
| 915 | filter = new ModFilter(isRegFormat(Form)); |
| 916 | else |
| 917 | filter = new DumbFilter(); |
| 918 | opcodeToSet = Opcode; |
| 919 | break; |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 920 | case X86Local::D8: |
| 921 | case X86Local::D9: |
| 922 | case X86Local::DA: |
| 923 | case X86Local::DB: |
| 924 | case X86Local::DC: |
| 925 | case X86Local::DD: |
| 926 | case X86Local::DE: |
| 927 | case X86Local::DF: |
| 928 | assert(Opcode >= 0xc0 && "Unexpected opcode for an escape opcode"); |
| 929 | opcodeType = ONEBYTE; |
| 930 | if (Form == X86Local::AddRegFrm) { |
| 931 | Spec->modifierType = MODIFIER_MODRM; |
| 932 | Spec->modifierBase = Opcode; |
| 933 | filter = new AddRegEscapeFilter(Opcode); |
| 934 | } else { |
| 935 | filter = new EscapeFilter(true, Opcode); |
| 936 | } |
| 937 | opcodeToSet = 0xd8 + (Prefix - X86Local::D8); |
| 938 | break; |
Craig Topper | 842f58f | 2011-09-11 20:23:20 +0000 | [diff] [blame] | 939 | case X86Local::REP: |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 940 | default: |
| 941 | opcodeType = ONEBYTE; |
| 942 | switch (Opcode) { |
| 943 | #define EXTENSION_TABLE(n) case 0x##n: |
| 944 | ONE_BYTE_EXTENSION_TABLES |
| 945 | #undef EXTENSION_TABLE |
| 946 | switch (Form) { |
| 947 | default: |
| 948 | llvm_unreachable("Fell through the cracks of a single-byte " |
| 949 | "extended opcode"); |
| 950 | case X86Local::MRM0r: |
| 951 | case X86Local::MRM1r: |
| 952 | case X86Local::MRM2r: |
| 953 | case X86Local::MRM3r: |
| 954 | case X86Local::MRM4r: |
| 955 | case X86Local::MRM5r: |
| 956 | case X86Local::MRM6r: |
| 957 | case X86Local::MRM7r: |
| 958 | filter = new ExtendedFilter(true, Form - X86Local::MRM0r); |
| 959 | break; |
| 960 | case X86Local::MRM0m: |
| 961 | case X86Local::MRM1m: |
| 962 | case X86Local::MRM2m: |
| 963 | case X86Local::MRM3m: |
| 964 | case X86Local::MRM4m: |
| 965 | case X86Local::MRM5m: |
| 966 | case X86Local::MRM6m: |
| 967 | case X86Local::MRM7m: |
| 968 | filter = new ExtendedFilter(false, Form - X86Local::MRM0m); |
| 969 | break; |
Sean Callanan | 9492be8 | 2010-02-12 23:39:46 +0000 | [diff] [blame] | 970 | MRM_MAPPING |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 971 | } // switch (Form) |
| 972 | break; |
| 973 | case 0xd8: |
| 974 | case 0xd9: |
| 975 | case 0xda: |
| 976 | case 0xdb: |
| 977 | case 0xdc: |
| 978 | case 0xdd: |
| 979 | case 0xde: |
| 980 | case 0xdf: |
| 981 | filter = new EscapeFilter(false, Form - X86Local::MRM0m); |
| 982 | break; |
| 983 | default: |
| 984 | if (needsModRMForDecode(Form)) |
| 985 | filter = new ModFilter(isRegFormat(Form)); |
| 986 | else |
| 987 | filter = new DumbFilter(); |
| 988 | break; |
| 989 | } // switch (Opcode) |
| 990 | opcodeToSet = Opcode; |
| 991 | } // switch (Prefix) |
| 992 | |
| 993 | assert(opcodeType != (OpcodeType)-1 && |
| 994 | "Opcode type not set"); |
| 995 | assert(filter && "Filter not set"); |
| 996 | |
| 997 | if (Form == X86Local::AddRegFrm) { |
| 998 | if(Spec->modifierType != MODIFIER_MODRM) { |
| 999 | assert(opcodeToSet < 0xf9 && |
| 1000 | "Not enough room for all ADDREG_FRM operands"); |
| 1001 | |
| 1002 | uint8_t currentOpcode; |
| 1003 | |
| 1004 | for (currentOpcode = opcodeToSet; |
| 1005 | currentOpcode < opcodeToSet + 8; |
| 1006 | ++currentOpcode) |
| 1007 | tables.setTableFields(opcodeType, |
| 1008 | insnContext(), |
| 1009 | currentOpcode, |
| 1010 | *filter, |
Craig Topper | 6744a17 | 2011-10-04 06:30:42 +0000 | [diff] [blame] | 1011 | UID, Is32Bit, IgnoresVEX_L); |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1012 | |
| 1013 | Spec->modifierType = MODIFIER_OPCODE; |
| 1014 | Spec->modifierBase = opcodeToSet; |
| 1015 | } else { |
| 1016 | // modifierBase was set where MODIFIER_MODRM was set |
| 1017 | tables.setTableFields(opcodeType, |
| 1018 | insnContext(), |
| 1019 | opcodeToSet, |
| 1020 | *filter, |
Craig Topper | 6744a17 | 2011-10-04 06:30:42 +0000 | [diff] [blame] | 1021 | UID, Is32Bit, IgnoresVEX_L); |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1022 | } |
| 1023 | } else { |
| 1024 | tables.setTableFields(opcodeType, |
| 1025 | insnContext(), |
| 1026 | opcodeToSet, |
| 1027 | *filter, |
Craig Topper | 6744a17 | 2011-10-04 06:30:42 +0000 | [diff] [blame] | 1028 | UID, Is32Bit, IgnoresVEX_L); |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1029 | |
| 1030 | Spec->modifierType = MODIFIER_NONE; |
| 1031 | Spec->modifierBase = opcodeToSet; |
| 1032 | } |
| 1033 | |
| 1034 | delete filter; |
Sean Callanan | 9492be8 | 2010-02-12 23:39:46 +0000 | [diff] [blame] | 1035 | |
| 1036 | #undef MAP |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | #define TYPE(str, type) if (s == str) return type; |
| 1040 | OperandType RecognizableInstr::typeFromString(const std::string &s, |
| 1041 | bool isSSE, |
| 1042 | bool hasREX_WPrefix, |
| 1043 | bool hasOpSizePrefix) { |
| 1044 | if (isSSE) { |
| 1045 | // For SSE instructions, we ignore the OpSize prefix and force operand |
| 1046 | // sizes. |
| 1047 | TYPE("GR16", TYPE_R16) |
| 1048 | TYPE("GR32", TYPE_R32) |
| 1049 | TYPE("GR64", TYPE_R64) |
| 1050 | } |
| 1051 | if(hasREX_WPrefix) { |
| 1052 | // For instructions with a REX_W prefix, a declared 32-bit register encoding |
| 1053 | // is special. |
| 1054 | TYPE("GR32", TYPE_R32) |
| 1055 | } |
| 1056 | if(!hasOpSizePrefix) { |
| 1057 | // For instructions without an OpSize prefix, a declared 16-bit register or |
| 1058 | // immediate encoding is special. |
| 1059 | TYPE("GR16", TYPE_R16) |
| 1060 | TYPE("i16imm", TYPE_IMM16) |
| 1061 | } |
| 1062 | TYPE("i16mem", TYPE_Mv) |
| 1063 | TYPE("i16imm", TYPE_IMMv) |
| 1064 | TYPE("i16i8imm", TYPE_IMMv) |
| 1065 | TYPE("GR16", TYPE_Rv) |
| 1066 | TYPE("i32mem", TYPE_Mv) |
| 1067 | TYPE("i32imm", TYPE_IMMv) |
| 1068 | TYPE("i32i8imm", TYPE_IMM32) |
Kevin Enderby | c37d4bb | 2011-07-27 23:01:50 +0000 | [diff] [blame] | 1069 | TYPE("u32u8imm", TYPE_IMM32) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1070 | TYPE("GR32", TYPE_Rv) |
| 1071 | TYPE("i64mem", TYPE_Mv) |
| 1072 | TYPE("i64i32imm", TYPE_IMM64) |
| 1073 | TYPE("i64i8imm", TYPE_IMM64) |
| 1074 | TYPE("GR64", TYPE_R64) |
| 1075 | TYPE("i8mem", TYPE_M8) |
| 1076 | TYPE("i8imm", TYPE_IMM8) |
| 1077 | TYPE("GR8", TYPE_R8) |
| 1078 | TYPE("VR128", TYPE_XMM128) |
| 1079 | TYPE("f128mem", TYPE_M128) |
Chris Lattner | b2ef4c1 | 2010-09-29 02:57:56 +0000 | [diff] [blame] | 1080 | TYPE("f256mem", TYPE_M256) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1081 | TYPE("FR64", TYPE_XMM64) |
| 1082 | TYPE("f64mem", TYPE_M64FP) |
Chris Lattner | b2ef4c1 | 2010-09-29 02:57:56 +0000 | [diff] [blame] | 1083 | TYPE("sdmem", TYPE_M64FP) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1084 | TYPE("FR32", TYPE_XMM32) |
| 1085 | TYPE("f32mem", TYPE_M32FP) |
Chris Lattner | b2ef4c1 | 2010-09-29 02:57:56 +0000 | [diff] [blame] | 1086 | TYPE("ssmem", TYPE_M32FP) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1087 | TYPE("RST", TYPE_ST) |
| 1088 | TYPE("i128mem", TYPE_M128) |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1089 | TYPE("i256mem", TYPE_M256) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1090 | TYPE("i64i32imm_pcrel", TYPE_REL64) |
Chris Lattner | 9fc0522 | 2010-07-07 22:27:31 +0000 | [diff] [blame] | 1091 | TYPE("i16imm_pcrel", TYPE_REL16) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1092 | TYPE("i32imm_pcrel", TYPE_REL32) |
Sean Callanan | 5edca81 | 2010-04-07 21:42:19 +0000 | [diff] [blame] | 1093 | TYPE("SSECC", TYPE_IMM3) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1094 | TYPE("brtarget", TYPE_RELv) |
Owen Anderson | c266600 | 2010-12-13 19:31:11 +0000 | [diff] [blame] | 1095 | TYPE("uncondbrtarget", TYPE_RELv) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1096 | TYPE("brtarget8", TYPE_REL8) |
| 1097 | TYPE("f80mem", TYPE_M80FP) |
Sean Callanan | 7fb35a2 | 2009-12-22 21:12:55 +0000 | [diff] [blame] | 1098 | TYPE("lea32mem", TYPE_LEA) |
| 1099 | TYPE("lea64_32mem", TYPE_LEA) |
| 1100 | TYPE("lea64mem", TYPE_LEA) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1101 | TYPE("VR64", TYPE_MM64) |
| 1102 | TYPE("i64imm", TYPE_IMMv) |
| 1103 | TYPE("opaque32mem", TYPE_M1616) |
| 1104 | TYPE("opaque48mem", TYPE_M1632) |
| 1105 | TYPE("opaque80mem", TYPE_M1664) |
| 1106 | TYPE("opaque512mem", TYPE_M512) |
| 1107 | TYPE("SEGMENT_REG", TYPE_SEGMENTREG) |
| 1108 | TYPE("DEBUG_REG", TYPE_DEBUGREG) |
Sean Callanan | 1a8b789 | 2010-05-06 20:59:00 +0000 | [diff] [blame] | 1109 | TYPE("CONTROL_REG", TYPE_CONTROLREG) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1110 | TYPE("offset8", TYPE_MOFFS8) |
| 1111 | TYPE("offset16", TYPE_MOFFS16) |
| 1112 | TYPE("offset32", TYPE_MOFFS32) |
| 1113 | TYPE("offset64", TYPE_MOFFS64) |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1114 | TYPE("VR256", TYPE_XMM256) |
Craig Topper | 7ea16b0 | 2011-10-06 06:44:41 +0000 | [diff] [blame] | 1115 | TYPE("GR16_NOAX", TYPE_Rv) |
| 1116 | TYPE("GR32_NOAX", TYPE_Rv) |
| 1117 | TYPE("GR64_NOAX", TYPE_R64) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1118 | errs() << "Unhandled type string " << s << "\n"; |
| 1119 | llvm_unreachable("Unhandled type string"); |
| 1120 | } |
| 1121 | #undef TYPE |
| 1122 | |
| 1123 | #define ENCODING(str, encoding) if (s == str) return encoding; |
| 1124 | OperandEncoding RecognizableInstr::immediateEncodingFromString |
| 1125 | (const std::string &s, |
| 1126 | bool hasOpSizePrefix) { |
| 1127 | if(!hasOpSizePrefix) { |
| 1128 | // For instructions without an OpSize prefix, a declared 16-bit register or |
| 1129 | // immediate encoding is special. |
| 1130 | ENCODING("i16imm", ENCODING_IW) |
| 1131 | } |
| 1132 | ENCODING("i32i8imm", ENCODING_IB) |
Kevin Enderby | c37d4bb | 2011-07-27 23:01:50 +0000 | [diff] [blame] | 1133 | ENCODING("u32u8imm", ENCODING_IB) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1134 | ENCODING("SSECC", ENCODING_IB) |
| 1135 | ENCODING("i16imm", ENCODING_Iv) |
| 1136 | ENCODING("i16i8imm", ENCODING_IB) |
| 1137 | ENCODING("i32imm", ENCODING_Iv) |
| 1138 | ENCODING("i64i32imm", ENCODING_ID) |
| 1139 | ENCODING("i64i8imm", ENCODING_IB) |
| 1140 | ENCODING("i8imm", ENCODING_IB) |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1141 | // This is not a typo. Instructions like BLENDVPD put |
| 1142 | // register IDs in 8-bit immediates nowadays. |
| 1143 | ENCODING("VR256", ENCODING_IB) |
| 1144 | ENCODING("VR128", ENCODING_IB) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1145 | errs() << "Unhandled immediate encoding " << s << "\n"; |
| 1146 | llvm_unreachable("Unhandled immediate encoding"); |
| 1147 | } |
| 1148 | |
| 1149 | OperandEncoding RecognizableInstr::rmRegisterEncodingFromString |
| 1150 | (const std::string &s, |
| 1151 | bool hasOpSizePrefix) { |
| 1152 | ENCODING("GR16", ENCODING_RM) |
| 1153 | ENCODING("GR32", ENCODING_RM) |
| 1154 | ENCODING("GR64", ENCODING_RM) |
| 1155 | ENCODING("GR8", ENCODING_RM) |
| 1156 | ENCODING("VR128", ENCODING_RM) |
| 1157 | ENCODING("FR64", ENCODING_RM) |
| 1158 | ENCODING("FR32", ENCODING_RM) |
| 1159 | ENCODING("VR64", ENCODING_RM) |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1160 | ENCODING("VR256", ENCODING_RM) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1161 | errs() << "Unhandled R/M register encoding " << s << "\n"; |
| 1162 | llvm_unreachable("Unhandled R/M register encoding"); |
| 1163 | } |
| 1164 | |
| 1165 | OperandEncoding RecognizableInstr::roRegisterEncodingFromString |
| 1166 | (const std::string &s, |
| 1167 | bool hasOpSizePrefix) { |
| 1168 | ENCODING("GR16", ENCODING_REG) |
| 1169 | ENCODING("GR32", ENCODING_REG) |
| 1170 | ENCODING("GR64", ENCODING_REG) |
| 1171 | ENCODING("GR8", ENCODING_REG) |
| 1172 | ENCODING("VR128", ENCODING_REG) |
| 1173 | ENCODING("FR64", ENCODING_REG) |
| 1174 | ENCODING("FR32", ENCODING_REG) |
| 1175 | ENCODING("VR64", ENCODING_REG) |
| 1176 | ENCODING("SEGMENT_REG", ENCODING_REG) |
| 1177 | ENCODING("DEBUG_REG", ENCODING_REG) |
Sean Callanan | 1a8b789 | 2010-05-06 20:59:00 +0000 | [diff] [blame] | 1178 | ENCODING("CONTROL_REG", ENCODING_REG) |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1179 | ENCODING("VR256", ENCODING_REG) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1180 | errs() << "Unhandled reg/opcode register encoding " << s << "\n"; |
| 1181 | llvm_unreachable("Unhandled reg/opcode register encoding"); |
| 1182 | } |
| 1183 | |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1184 | OperandEncoding RecognizableInstr::vvvvRegisterEncodingFromString |
| 1185 | (const std::string &s, |
| 1186 | bool hasOpSizePrefix) { |
Craig Topper | 54a1117 | 2011-10-14 07:06:56 +0000 | [diff] [blame] | 1187 | ENCODING("GR32", ENCODING_VVVV) |
| 1188 | ENCODING("GR64", ENCODING_VVVV) |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1189 | ENCODING("FR32", ENCODING_VVVV) |
| 1190 | ENCODING("FR64", ENCODING_VVVV) |
| 1191 | ENCODING("VR128", ENCODING_VVVV) |
| 1192 | ENCODING("VR256", ENCODING_VVVV) |
| 1193 | errs() << "Unhandled VEX.vvvv register encoding " << s << "\n"; |
| 1194 | llvm_unreachable("Unhandled VEX.vvvv register encoding"); |
| 1195 | } |
| 1196 | |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1197 | OperandEncoding RecognizableInstr::memoryEncodingFromString |
| 1198 | (const std::string &s, |
| 1199 | bool hasOpSizePrefix) { |
| 1200 | ENCODING("i16mem", ENCODING_RM) |
| 1201 | ENCODING("i32mem", ENCODING_RM) |
| 1202 | ENCODING("i64mem", ENCODING_RM) |
| 1203 | ENCODING("i8mem", ENCODING_RM) |
Chris Lattner | b2ef4c1 | 2010-09-29 02:57:56 +0000 | [diff] [blame] | 1204 | ENCODING("ssmem", ENCODING_RM) |
| 1205 | ENCODING("sdmem", ENCODING_RM) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1206 | ENCODING("f128mem", ENCODING_RM) |
Chris Lattner | b2ef4c1 | 2010-09-29 02:57:56 +0000 | [diff] [blame] | 1207 | ENCODING("f256mem", ENCODING_RM) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1208 | ENCODING("f64mem", ENCODING_RM) |
| 1209 | ENCODING("f32mem", ENCODING_RM) |
| 1210 | ENCODING("i128mem", ENCODING_RM) |
Sean Callanan | a21e2ea | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1211 | ENCODING("i256mem", ENCODING_RM) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1212 | ENCODING("f80mem", ENCODING_RM) |
| 1213 | ENCODING("lea32mem", ENCODING_RM) |
| 1214 | ENCODING("lea64_32mem", ENCODING_RM) |
| 1215 | ENCODING("lea64mem", ENCODING_RM) |
| 1216 | ENCODING("opaque32mem", ENCODING_RM) |
| 1217 | ENCODING("opaque48mem", ENCODING_RM) |
| 1218 | ENCODING("opaque80mem", ENCODING_RM) |
| 1219 | ENCODING("opaque512mem", ENCODING_RM) |
| 1220 | errs() << "Unhandled memory encoding " << s << "\n"; |
| 1221 | llvm_unreachable("Unhandled memory encoding"); |
| 1222 | } |
| 1223 | |
| 1224 | OperandEncoding RecognizableInstr::relocationEncodingFromString |
| 1225 | (const std::string &s, |
| 1226 | bool hasOpSizePrefix) { |
| 1227 | if(!hasOpSizePrefix) { |
| 1228 | // For instructions without an OpSize prefix, a declared 16-bit register or |
| 1229 | // immediate encoding is special. |
| 1230 | ENCODING("i16imm", ENCODING_IW) |
| 1231 | } |
| 1232 | ENCODING("i16imm", ENCODING_Iv) |
| 1233 | ENCODING("i16i8imm", ENCODING_IB) |
| 1234 | ENCODING("i32imm", ENCODING_Iv) |
| 1235 | ENCODING("i32i8imm", ENCODING_IB) |
| 1236 | ENCODING("i64i32imm", ENCODING_ID) |
| 1237 | ENCODING("i64i8imm", ENCODING_IB) |
| 1238 | ENCODING("i8imm", ENCODING_IB) |
| 1239 | ENCODING("i64i32imm_pcrel", ENCODING_ID) |
Chris Lattner | 9fc0522 | 2010-07-07 22:27:31 +0000 | [diff] [blame] | 1240 | ENCODING("i16imm_pcrel", ENCODING_IW) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1241 | ENCODING("i32imm_pcrel", ENCODING_ID) |
| 1242 | ENCODING("brtarget", ENCODING_Iv) |
| 1243 | ENCODING("brtarget8", ENCODING_IB) |
| 1244 | ENCODING("i64imm", ENCODING_IO) |
| 1245 | ENCODING("offset8", ENCODING_Ia) |
| 1246 | ENCODING("offset16", ENCODING_Ia) |
| 1247 | ENCODING("offset32", ENCODING_Ia) |
| 1248 | ENCODING("offset64", ENCODING_Ia) |
| 1249 | errs() << "Unhandled relocation encoding " << s << "\n"; |
| 1250 | llvm_unreachable("Unhandled relocation encoding"); |
| 1251 | } |
| 1252 | |
| 1253 | OperandEncoding RecognizableInstr::opcodeModifierEncodingFromString |
| 1254 | (const std::string &s, |
| 1255 | bool hasOpSizePrefix) { |
| 1256 | ENCODING("RST", ENCODING_I) |
| 1257 | ENCODING("GR32", ENCODING_Rv) |
| 1258 | ENCODING("GR64", ENCODING_RO) |
| 1259 | ENCODING("GR16", ENCODING_Rv) |
| 1260 | ENCODING("GR8", ENCODING_RB) |
Craig Topper | 7ea16b0 | 2011-10-06 06:44:41 +0000 | [diff] [blame] | 1261 | ENCODING("GR16_NOAX", ENCODING_Rv) |
| 1262 | ENCODING("GR32_NOAX", ENCODING_Rv) |
| 1263 | ENCODING("GR64_NOAX", ENCODING_RO) |
Sean Callanan | 8ed9f51 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1264 | errs() << "Unhandled opcode modifier encoding " << s << "\n"; |
| 1265 | llvm_unreachable("Unhandled opcode modifier encoding"); |
| 1266 | } |
Daniel Dunbar | 9e6d1d1 | 2009-12-19 04:16:48 +0000 | [diff] [blame] | 1267 | #undef ENCODING |