X86RecognizableInstr.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. //===- X86RecognizableInstr.cpp - Disassembler instruction spec --*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is part of the X86 Disassembler Emitter.
  10. // It contains the implementation of a single recognizable instruction.
  11. // Documentation for the disassembler emitter in general can be found in
  12. // X86DisassemblerEmitter.h.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "X86RecognizableInstr.h"
  16. #include "X86DisassemblerShared.h"
  17. #include "X86DisassemblerTables.h"
  18. #include "X86ModRMFilters.h"
  19. #include "llvm/Support/ErrorHandling.h"
  20. #include "llvm/TableGen/Record.h"
  21. #include <string>
  22. using namespace llvm;
  23. using namespace X86Disassembler;
  24. /// byteFromBitsInit - Extracts a value at most 8 bits in width from a BitsInit.
  25. /// Useful for switch statements and the like.
  26. ///
  27. /// @param init - A reference to the BitsInit to be decoded.
  28. /// @return - The field, with the first bit in the BitsInit as the lowest
  29. /// order bit.
  30. static uint8_t byteFromBitsInit(BitsInit &init) {
  31. int width = init.getNumBits();
  32. assert(width <= 8 && "Field is too large for uint8_t!");
  33. int index;
  34. uint8_t mask = 0x01;
  35. uint8_t ret = 0;
  36. for (index = 0; index < width; index++) {
  37. if (cast<BitInit>(init.getBit(index))->getValue())
  38. ret |= mask;
  39. mask <<= 1;
  40. }
  41. return ret;
  42. }
  43. /// byteFromRec - Extract a value at most 8 bits in with from a Record given the
  44. /// name of the field.
  45. ///
  46. /// @param rec - The record from which to extract the value.
  47. /// @param name - The name of the field in the record.
  48. /// @return - The field, as translated by byteFromBitsInit().
  49. static uint8_t byteFromRec(const Record* rec, StringRef name) {
  50. BitsInit* bits = rec->getValueAsBitsInit(name);
  51. return byteFromBitsInit(*bits);
  52. }
  53. RecognizableInstr::RecognizableInstr(DisassemblerTables &tables,
  54. const CodeGenInstruction &insn,
  55. InstrUID uid) {
  56. UID = uid;
  57. Rec = insn.TheDef;
  58. Name = std::string(Rec->getName());
  59. Spec = &tables.specForUID(UID);
  60. if (!Rec->isSubClassOf("X86Inst")) {
  61. ShouldBeEmitted = false;
  62. return;
  63. }
  64. OpPrefix = byteFromRec(Rec, "OpPrefixBits");
  65. OpMap = byteFromRec(Rec, "OpMapBits");
  66. Opcode = byteFromRec(Rec, "Opcode");
  67. Form = byteFromRec(Rec, "FormBits");
  68. Encoding = byteFromRec(Rec, "OpEncBits");
  69. OpSize = byteFromRec(Rec, "OpSizeBits");
  70. AdSize = byteFromRec(Rec, "AdSizeBits");
  71. HasREX_WPrefix = Rec->getValueAsBit("hasREX_WPrefix");
  72. HasVEX_4V = Rec->getValueAsBit("hasVEX_4V");
  73. HasVEX_W = Rec->getValueAsBit("HasVEX_W");
  74. IgnoresVEX_W = Rec->getValueAsBit("IgnoresVEX_W");
  75. IgnoresVEX_L = Rec->getValueAsBit("ignoresVEX_L");
  76. HasEVEX_L2Prefix = Rec->getValueAsBit("hasEVEX_L2");
  77. HasEVEX_K = Rec->getValueAsBit("hasEVEX_K");
  78. HasEVEX_KZ = Rec->getValueAsBit("hasEVEX_Z");
  79. HasEVEX_B = Rec->getValueAsBit("hasEVEX_B");
  80. IsCodeGenOnly = Rec->getValueAsBit("isCodeGenOnly");
  81. ForceDisassemble = Rec->getValueAsBit("ForceDisassemble");
  82. CD8_Scale = byteFromRec(Rec, "CD8_Scale");
  83. Name = std::string(Rec->getName());
  84. Operands = &insn.Operands.OperandList;
  85. HasVEX_LPrefix = Rec->getValueAsBit("hasVEX_L");
  86. EncodeRC = HasEVEX_B &&
  87. (Form == X86Local::MRMDestReg || Form == X86Local::MRMSrcReg);
  88. // Check for 64-bit inst which does not require REX
  89. Is32Bit = false;
  90. Is64Bit = false;
  91. // FIXME: Is there some better way to check for In64BitMode?
  92. std::vector<Record*> Predicates = Rec->getValueAsListOfDefs("Predicates");
  93. for (unsigned i = 0, e = Predicates.size(); i != e; ++i) {
  94. if (Predicates[i]->getName().contains("Not64Bit") ||
  95. Predicates[i]->getName().contains("In32Bit")) {
  96. Is32Bit = true;
  97. break;
  98. }
  99. if (Predicates[i]->getName().contains("In64Bit")) {
  100. Is64Bit = true;
  101. break;
  102. }
  103. }
  104. if (Form == X86Local::Pseudo || (IsCodeGenOnly && !ForceDisassemble)) {
  105. ShouldBeEmitted = false;
  106. return;
  107. }
  108. ShouldBeEmitted = true;
  109. }
  110. void RecognizableInstr::processInstr(DisassemblerTables &tables,
  111. const CodeGenInstruction &insn,
  112. InstrUID uid)
  113. {
  114. // Ignore "asm parser only" instructions.
  115. if (insn.TheDef->getValueAsBit("isAsmParserOnly"))
  116. return;
  117. RecognizableInstr recogInstr(tables, insn, uid);
  118. if (recogInstr.shouldBeEmitted()) {
  119. recogInstr.emitInstructionSpecifier();
  120. recogInstr.emitDecodePath(tables);
  121. }
  122. }
  123. #define EVEX_KB(n) (HasEVEX_KZ && HasEVEX_B ? n##_KZ_B : \
  124. (HasEVEX_K && HasEVEX_B ? n##_K_B : \
  125. (HasEVEX_KZ ? n##_KZ : \
  126. (HasEVEX_K? n##_K : (HasEVEX_B ? n##_B : n)))))
  127. InstructionContext RecognizableInstr::insnContext() const {
  128. InstructionContext insnContext;
  129. if (Encoding == X86Local::EVEX) {
  130. if (HasVEX_LPrefix && HasEVEX_L2Prefix) {
  131. errs() << "Don't support VEX.L if EVEX_L2 is enabled: " << Name << "\n";
  132. llvm_unreachable("Don't support VEX.L if EVEX_L2 is enabled");
  133. }
  134. // VEX_L & VEX_W
  135. if (!EncodeRC && HasVEX_LPrefix && HasVEX_W) {
  136. if (OpPrefix == X86Local::PD)
  137. insnContext = EVEX_KB(IC_EVEX_L_W_OPSIZE);
  138. else if (OpPrefix == X86Local::XS)
  139. insnContext = EVEX_KB(IC_EVEX_L_W_XS);
  140. else if (OpPrefix == X86Local::XD)
  141. insnContext = EVEX_KB(IC_EVEX_L_W_XD);
  142. else if (OpPrefix == X86Local::PS)
  143. insnContext = EVEX_KB(IC_EVEX_L_W);
  144. else {
  145. errs() << "Instruction does not use a prefix: " << Name << "\n";
  146. llvm_unreachable("Invalid prefix");
  147. }
  148. } else if (!EncodeRC && HasVEX_LPrefix) {
  149. // VEX_L
  150. if (OpPrefix == X86Local::PD)
  151. insnContext = EVEX_KB(IC_EVEX_L_OPSIZE);
  152. else if (OpPrefix == X86Local::XS)
  153. insnContext = EVEX_KB(IC_EVEX_L_XS);
  154. else if (OpPrefix == X86Local::XD)
  155. insnContext = EVEX_KB(IC_EVEX_L_XD);
  156. else if (OpPrefix == X86Local::PS)
  157. insnContext = EVEX_KB(IC_EVEX_L);
  158. else {
  159. errs() << "Instruction does not use a prefix: " << Name << "\n";
  160. llvm_unreachable("Invalid prefix");
  161. }
  162. } else if (!EncodeRC && HasEVEX_L2Prefix && HasVEX_W) {
  163. // EVEX_L2 & VEX_W
  164. if (OpPrefix == X86Local::PD)
  165. insnContext = EVEX_KB(IC_EVEX_L2_W_OPSIZE);
  166. else if (OpPrefix == X86Local::XS)
  167. insnContext = EVEX_KB(IC_EVEX_L2_W_XS);
  168. else if (OpPrefix == X86Local::XD)
  169. insnContext = EVEX_KB(IC_EVEX_L2_W_XD);
  170. else if (OpPrefix == X86Local::PS)
  171. insnContext = EVEX_KB(IC_EVEX_L2_W);
  172. else {
  173. errs() << "Instruction does not use a prefix: " << Name << "\n";
  174. llvm_unreachable("Invalid prefix");
  175. }
  176. } else if (!EncodeRC && HasEVEX_L2Prefix) {
  177. // EVEX_L2
  178. if (OpPrefix == X86Local::PD)
  179. insnContext = EVEX_KB(IC_EVEX_L2_OPSIZE);
  180. else if (OpPrefix == X86Local::XD)
  181. insnContext = EVEX_KB(IC_EVEX_L2_XD);
  182. else if (OpPrefix == X86Local::XS)
  183. insnContext = EVEX_KB(IC_EVEX_L2_XS);
  184. else if (OpPrefix == X86Local::PS)
  185. insnContext = EVEX_KB(IC_EVEX_L2);
  186. else {
  187. errs() << "Instruction does not use a prefix: " << Name << "\n";
  188. llvm_unreachable("Invalid prefix");
  189. }
  190. }
  191. else if (HasVEX_W) {
  192. // VEX_W
  193. if (OpPrefix == X86Local::PD)
  194. insnContext = EVEX_KB(IC_EVEX_W_OPSIZE);
  195. else if (OpPrefix == X86Local::XS)
  196. insnContext = EVEX_KB(IC_EVEX_W_XS);
  197. else if (OpPrefix == X86Local::XD)
  198. insnContext = EVEX_KB(IC_EVEX_W_XD);
  199. else if (OpPrefix == X86Local::PS)
  200. insnContext = EVEX_KB(IC_EVEX_W);
  201. else {
  202. errs() << "Instruction does not use a prefix: " << Name << "\n";
  203. llvm_unreachable("Invalid prefix");
  204. }
  205. }
  206. // No L, no W
  207. else if (OpPrefix == X86Local::PD)
  208. insnContext = EVEX_KB(IC_EVEX_OPSIZE);
  209. else if (OpPrefix == X86Local::XD)
  210. insnContext = EVEX_KB(IC_EVEX_XD);
  211. else if (OpPrefix == X86Local::XS)
  212. insnContext = EVEX_KB(IC_EVEX_XS);
  213. else if (OpPrefix == X86Local::PS)
  214. insnContext = EVEX_KB(IC_EVEX);
  215. else {
  216. errs() << "Instruction does not use a prefix: " << Name << "\n";
  217. llvm_unreachable("Invalid prefix");
  218. }
  219. /// eof EVEX
  220. } else if (Encoding == X86Local::VEX || Encoding == X86Local::XOP) {
  221. if (HasVEX_LPrefix && HasVEX_W) {
  222. if (OpPrefix == X86Local::PD)
  223. insnContext = IC_VEX_L_W_OPSIZE;
  224. else if (OpPrefix == X86Local::XS)
  225. insnContext = IC_VEX_L_W_XS;
  226. else if (OpPrefix == X86Local::XD)
  227. insnContext = IC_VEX_L_W_XD;
  228. else if (OpPrefix == X86Local::PS)
  229. insnContext = IC_VEX_L_W;
  230. else {
  231. errs() << "Instruction does not use a prefix: " << Name << "\n";
  232. llvm_unreachable("Invalid prefix");
  233. }
  234. } else if (OpPrefix == X86Local::PD && HasVEX_LPrefix)
  235. insnContext = IC_VEX_L_OPSIZE;
  236. else if (OpPrefix == X86Local::PD && HasVEX_W)
  237. insnContext = IC_VEX_W_OPSIZE;
  238. else if (OpPrefix == X86Local::PD && Is64Bit &&
  239. AdSize == X86Local::AdSize32)
  240. insnContext = IC_64BIT_VEX_OPSIZE_ADSIZE;
  241. else if (OpPrefix == X86Local::PD && Is64Bit)
  242. insnContext = IC_64BIT_VEX_OPSIZE;
  243. else if (OpPrefix == X86Local::PD)
  244. insnContext = IC_VEX_OPSIZE;
  245. else if (HasVEX_LPrefix && OpPrefix == X86Local::XS)
  246. insnContext = IC_VEX_L_XS;
  247. else if (HasVEX_LPrefix && OpPrefix == X86Local::XD)
  248. insnContext = IC_VEX_L_XD;
  249. else if (HasVEX_W && OpPrefix == X86Local::XS)
  250. insnContext = IC_VEX_W_XS;
  251. else if (HasVEX_W && OpPrefix == X86Local::XD)
  252. insnContext = IC_VEX_W_XD;
  253. else if (HasVEX_W && OpPrefix == X86Local::PS)
  254. insnContext = IC_VEX_W;
  255. else if (HasVEX_LPrefix && OpPrefix == X86Local::PS)
  256. insnContext = IC_VEX_L;
  257. else if (OpPrefix == X86Local::XD)
  258. insnContext = IC_VEX_XD;
  259. else if (OpPrefix == X86Local::XS)
  260. insnContext = IC_VEX_XS;
  261. else if (OpPrefix == X86Local::PS)
  262. insnContext = IC_VEX;
  263. else {
  264. errs() << "Instruction does not use a prefix: " << Name << "\n";
  265. llvm_unreachable("Invalid prefix");
  266. }
  267. } else if (Is64Bit || HasREX_WPrefix || AdSize == X86Local::AdSize64) {
  268. if (HasREX_WPrefix && (OpSize == X86Local::OpSize16 || OpPrefix == X86Local::PD))
  269. insnContext = IC_64BIT_REXW_OPSIZE;
  270. else if (HasREX_WPrefix && AdSize == X86Local::AdSize32)
  271. insnContext = IC_64BIT_REXW_ADSIZE;
  272. else if (OpSize == X86Local::OpSize16 && OpPrefix == X86Local::XD)
  273. insnContext = IC_64BIT_XD_OPSIZE;
  274. else if (OpSize == X86Local::OpSize16 && OpPrefix == X86Local::XS)
  275. insnContext = IC_64BIT_XS_OPSIZE;
  276. else if (AdSize == X86Local::AdSize32 && OpPrefix == X86Local::PD)
  277. insnContext = IC_64BIT_OPSIZE_ADSIZE;
  278. else if (OpSize == X86Local::OpSize16 && AdSize == X86Local::AdSize32)
  279. insnContext = IC_64BIT_OPSIZE_ADSIZE;
  280. else if (OpSize == X86Local::OpSize16 || OpPrefix == X86Local::PD)
  281. insnContext = IC_64BIT_OPSIZE;
  282. else if (AdSize == X86Local::AdSize32)
  283. insnContext = IC_64BIT_ADSIZE;
  284. else if (HasREX_WPrefix && OpPrefix == X86Local::XS)
  285. insnContext = IC_64BIT_REXW_XS;
  286. else if (HasREX_WPrefix && OpPrefix == X86Local::XD)
  287. insnContext = IC_64BIT_REXW_XD;
  288. else if (OpPrefix == X86Local::XD)
  289. insnContext = IC_64BIT_XD;
  290. else if (OpPrefix == X86Local::XS)
  291. insnContext = IC_64BIT_XS;
  292. else if (HasREX_WPrefix)
  293. insnContext = IC_64BIT_REXW;
  294. else
  295. insnContext = IC_64BIT;
  296. } else {
  297. if (OpSize == X86Local::OpSize16 && OpPrefix == X86Local::XD)
  298. insnContext = IC_XD_OPSIZE;
  299. else if (OpSize == X86Local::OpSize16 && OpPrefix == X86Local::XS)
  300. insnContext = IC_XS_OPSIZE;
  301. else if (AdSize == X86Local::AdSize16 && OpPrefix == X86Local::XD)
  302. insnContext = IC_XD_ADSIZE;
  303. else if (AdSize == X86Local::AdSize16 && OpPrefix == X86Local::XS)
  304. insnContext = IC_XS_ADSIZE;
  305. else if (AdSize == X86Local::AdSize16 && OpPrefix == X86Local::PD)
  306. insnContext = IC_OPSIZE_ADSIZE;
  307. else if (OpSize == X86Local::OpSize16 && AdSize == X86Local::AdSize16)
  308. insnContext = IC_OPSIZE_ADSIZE;
  309. else if (OpSize == X86Local::OpSize16 || OpPrefix == X86Local::PD)
  310. insnContext = IC_OPSIZE;
  311. else if (AdSize == X86Local::AdSize16)
  312. insnContext = IC_ADSIZE;
  313. else if (OpPrefix == X86Local::XD)
  314. insnContext = IC_XD;
  315. else if (OpPrefix == X86Local::XS)
  316. insnContext = IC_XS;
  317. else
  318. insnContext = IC;
  319. }
  320. return insnContext;
  321. }
  322. void RecognizableInstr::adjustOperandEncoding(OperandEncoding &encoding) {
  323. // The scaling factor for AVX512 compressed displacement encoding is an
  324. // instruction attribute. Adjust the ModRM encoding type to include the
  325. // scale for compressed displacement.
  326. if ((encoding != ENCODING_RM &&
  327. encoding != ENCODING_VSIB &&
  328. encoding != ENCODING_SIB) ||CD8_Scale == 0)
  329. return;
  330. encoding = (OperandEncoding)(encoding + Log2_32(CD8_Scale));
  331. assert(((encoding >= ENCODING_RM && encoding <= ENCODING_RM_CD64) ||
  332. (encoding == ENCODING_SIB) ||
  333. (encoding >= ENCODING_VSIB && encoding <= ENCODING_VSIB_CD64)) &&
  334. "Invalid CDisp scaling");
  335. }
  336. void RecognizableInstr::handleOperand(bool optional, unsigned &operandIndex,
  337. unsigned &physicalOperandIndex,
  338. unsigned numPhysicalOperands,
  339. const unsigned *operandMapping,
  340. OperandEncoding (*encodingFromString)
  341. (const std::string&,
  342. uint8_t OpSize)) {
  343. if (optional) {
  344. if (physicalOperandIndex >= numPhysicalOperands)
  345. return;
  346. } else {
  347. assert(physicalOperandIndex < numPhysicalOperands);
  348. }
  349. while (operandMapping[operandIndex] != operandIndex) {
  350. Spec->operands[operandIndex].encoding = ENCODING_DUP;
  351. Spec->operands[operandIndex].type =
  352. (OperandType)(TYPE_DUP0 + operandMapping[operandIndex]);
  353. ++operandIndex;
  354. }
  355. StringRef typeName = (*Operands)[operandIndex].Rec->getName();
  356. OperandEncoding encoding = encodingFromString(std::string(typeName), OpSize);
  357. // Adjust the encoding type for an operand based on the instruction.
  358. adjustOperandEncoding(encoding);
  359. Spec->operands[operandIndex].encoding = encoding;
  360. Spec->operands[operandIndex].type =
  361. typeFromString(std::string(typeName), HasREX_WPrefix, OpSize);
  362. ++operandIndex;
  363. ++physicalOperandIndex;
  364. }
  365. void RecognizableInstr::emitInstructionSpecifier() {
  366. Spec->name = Name;
  367. Spec->insnContext = insnContext();
  368. const std::vector<CGIOperandList::OperandInfo> &OperandList = *Operands;
  369. unsigned numOperands = OperandList.size();
  370. unsigned numPhysicalOperands = 0;
  371. // operandMapping maps from operands in OperandList to their originals.
  372. // If operandMapping[i] != i, then the entry is a duplicate.
  373. unsigned operandMapping[X86_MAX_OPERANDS];
  374. assert(numOperands <= X86_MAX_OPERANDS && "X86_MAX_OPERANDS is not large enough");
  375. for (unsigned operandIndex = 0; operandIndex < numOperands; ++operandIndex) {
  376. if (!OperandList[operandIndex].Constraints.empty()) {
  377. const CGIOperandList::ConstraintInfo &Constraint =
  378. OperandList[operandIndex].Constraints[0];
  379. if (Constraint.isTied()) {
  380. operandMapping[operandIndex] = operandIndex;
  381. operandMapping[Constraint.getTiedOperand()] = operandIndex;
  382. } else {
  383. ++numPhysicalOperands;
  384. operandMapping[operandIndex] = operandIndex;
  385. }
  386. } else {
  387. ++numPhysicalOperands;
  388. operandMapping[operandIndex] = operandIndex;
  389. }
  390. }
  391. #define HANDLE_OPERAND(class) \
  392. handleOperand(false, \
  393. operandIndex, \
  394. physicalOperandIndex, \
  395. numPhysicalOperands, \
  396. operandMapping, \
  397. class##EncodingFromString);
  398. #define HANDLE_OPTIONAL(class) \
  399. handleOperand(true, \
  400. operandIndex, \
  401. physicalOperandIndex, \
  402. numPhysicalOperands, \
  403. operandMapping, \
  404. class##EncodingFromString);
  405. // operandIndex should always be < numOperands
  406. unsigned operandIndex = 0;
  407. // physicalOperandIndex should always be < numPhysicalOperands
  408. unsigned physicalOperandIndex = 0;
  409. #ifndef NDEBUG
  410. // Given the set of prefix bits, how many additional operands does the
  411. // instruction have?
  412. unsigned additionalOperands = 0;
  413. if (HasVEX_4V)
  414. ++additionalOperands;
  415. if (HasEVEX_K)
  416. ++additionalOperands;
  417. #endif
  418. switch (Form) {
  419. default: llvm_unreachable("Unhandled form");
  420. case X86Local::PrefixByte:
  421. return;
  422. case X86Local::RawFrmSrc:
  423. HANDLE_OPERAND(relocation);
  424. return;
  425. case X86Local::RawFrmDst:
  426. HANDLE_OPERAND(relocation);
  427. return;
  428. case X86Local::RawFrmDstSrc:
  429. HANDLE_OPERAND(relocation);
  430. HANDLE_OPERAND(relocation);
  431. return;
  432. case X86Local::RawFrm:
  433. // Operand 1 (optional) is an address or immediate.
  434. assert(numPhysicalOperands <= 1 &&
  435. "Unexpected number of operands for RawFrm");
  436. HANDLE_OPTIONAL(relocation)
  437. break;
  438. case X86Local::RawFrmMemOffs:
  439. // Operand 1 is an address.
  440. HANDLE_OPERAND(relocation);
  441. break;
  442. case X86Local::AddRegFrm:
  443. // Operand 1 is added to the opcode.
  444. // Operand 2 (optional) is an address.
  445. assert(numPhysicalOperands >= 1 && numPhysicalOperands <= 2 &&
  446. "Unexpected number of operands for AddRegFrm");
  447. HANDLE_OPERAND(opcodeModifier)
  448. HANDLE_OPTIONAL(relocation)
  449. break;
  450. case X86Local::AddCCFrm:
  451. // Operand 1 (optional) is an address or immediate.
  452. assert(numPhysicalOperands == 2 &&
  453. "Unexpected number of operands for AddCCFrm");
  454. HANDLE_OPERAND(relocation)
  455. HANDLE_OPERAND(opcodeModifier)
  456. break;
  457. case X86Local::MRMDestReg:
  458. // Operand 1 is a register operand in the R/M field.
  459. // - In AVX512 there may be a mask operand here -
  460. // Operand 2 is a register operand in the Reg/Opcode field.
  461. // - In AVX, there is a register operand in the VEX.vvvv field here -
  462. // Operand 3 (optional) is an immediate.
  463. assert(numPhysicalOperands >= 2 + additionalOperands &&
  464. numPhysicalOperands <= 3 + additionalOperands &&
  465. "Unexpected number of operands for MRMDestRegFrm");
  466. HANDLE_OPERAND(rmRegister)
  467. if (HasEVEX_K)
  468. HANDLE_OPERAND(writemaskRegister)
  469. if (HasVEX_4V)
  470. // FIXME: In AVX, the register below becomes the one encoded
  471. // in ModRMVEX and the one above the one in the VEX.VVVV field
  472. HANDLE_OPERAND(vvvvRegister)
  473. HANDLE_OPERAND(roRegister)
  474. HANDLE_OPTIONAL(immediate)
  475. break;
  476. case X86Local::MRMDestMem:
  477. case X86Local::MRMDestMemFSIB:
  478. // Operand 1 is a memory operand (possibly SIB-extended)
  479. // Operand 2 is a register operand in the Reg/Opcode field.
  480. // - In AVX, there is a register operand in the VEX.vvvv field here -
  481. // Operand 3 (optional) is an immediate.
  482. assert(numPhysicalOperands >= 2 + additionalOperands &&
  483. numPhysicalOperands <= 3 + additionalOperands &&
  484. "Unexpected number of operands for MRMDestMemFrm with VEX_4V");
  485. HANDLE_OPERAND(memory)
  486. if (HasEVEX_K)
  487. HANDLE_OPERAND(writemaskRegister)
  488. if (HasVEX_4V)
  489. // FIXME: In AVX, the register below becomes the one encoded
  490. // in ModRMVEX and the one above the one in the VEX.VVVV field
  491. HANDLE_OPERAND(vvvvRegister)
  492. HANDLE_OPERAND(roRegister)
  493. HANDLE_OPTIONAL(immediate)
  494. break;
  495. case X86Local::MRMSrcReg:
  496. // Operand 1 is a register operand in the Reg/Opcode field.
  497. // Operand 2 is a register operand in the R/M field.
  498. // - In AVX, there is a register operand in the VEX.vvvv field here -
  499. // Operand 3 (optional) is an immediate.
  500. // Operand 4 (optional) is an immediate.
  501. assert(numPhysicalOperands >= 2 + additionalOperands &&
  502. numPhysicalOperands <= 4 + additionalOperands &&
  503. "Unexpected number of operands for MRMSrcRegFrm");
  504. HANDLE_OPERAND(roRegister)
  505. if (HasEVEX_K)
  506. HANDLE_OPERAND(writemaskRegister)
  507. if (HasVEX_4V)
  508. // FIXME: In AVX, the register below becomes the one encoded
  509. // in ModRMVEX and the one above the one in the VEX.VVVV field
  510. HANDLE_OPERAND(vvvvRegister)
  511. HANDLE_OPERAND(rmRegister)
  512. HANDLE_OPTIONAL(immediate)
  513. HANDLE_OPTIONAL(immediate) // above might be a register in 7:4
  514. break;
  515. case X86Local::MRMSrcReg4VOp3:
  516. assert(numPhysicalOperands == 3 &&
  517. "Unexpected number of operands for MRMSrcReg4VOp3Frm");
  518. HANDLE_OPERAND(roRegister)
  519. HANDLE_OPERAND(rmRegister)
  520. HANDLE_OPERAND(vvvvRegister)
  521. break;
  522. case X86Local::MRMSrcRegOp4:
  523. assert(numPhysicalOperands >= 4 && numPhysicalOperands <= 5 &&
  524. "Unexpected number of operands for MRMSrcRegOp4Frm");
  525. HANDLE_OPERAND(roRegister)
  526. HANDLE_OPERAND(vvvvRegister)
  527. HANDLE_OPERAND(immediate) // Register in imm[7:4]
  528. HANDLE_OPERAND(rmRegister)
  529. HANDLE_OPTIONAL(immediate)
  530. break;
  531. case X86Local::MRMSrcRegCC:
  532. assert(numPhysicalOperands == 3 &&
  533. "Unexpected number of operands for MRMSrcRegCC");
  534. HANDLE_OPERAND(roRegister)
  535. HANDLE_OPERAND(rmRegister)
  536. HANDLE_OPERAND(opcodeModifier)
  537. break;
  538. case X86Local::MRMSrcMem:
  539. case X86Local::MRMSrcMemFSIB:
  540. // Operand 1 is a register operand in the Reg/Opcode field.
  541. // Operand 2 is a memory operand (possibly SIB-extended)
  542. // - In AVX, there is a register operand in the VEX.vvvv field here -
  543. // Operand 3 (optional) is an immediate.
  544. assert(numPhysicalOperands >= 2 + additionalOperands &&
  545. numPhysicalOperands <= 4 + additionalOperands &&
  546. "Unexpected number of operands for MRMSrcMemFrm");
  547. HANDLE_OPERAND(roRegister)
  548. if (HasEVEX_K)
  549. HANDLE_OPERAND(writemaskRegister)
  550. if (HasVEX_4V)
  551. // FIXME: In AVX, the register below becomes the one encoded
  552. // in ModRMVEX and the one above the one in the VEX.VVVV field
  553. HANDLE_OPERAND(vvvvRegister)
  554. HANDLE_OPERAND(memory)
  555. HANDLE_OPTIONAL(immediate)
  556. HANDLE_OPTIONAL(immediate) // above might be a register in 7:4
  557. break;
  558. case X86Local::MRMSrcMem4VOp3:
  559. assert(numPhysicalOperands == 3 &&
  560. "Unexpected number of operands for MRMSrcMem4VOp3Frm");
  561. HANDLE_OPERAND(roRegister)
  562. HANDLE_OPERAND(memory)
  563. HANDLE_OPERAND(vvvvRegister)
  564. break;
  565. case X86Local::MRMSrcMemOp4:
  566. assert(numPhysicalOperands >= 4 && numPhysicalOperands <= 5 &&
  567. "Unexpected number of operands for MRMSrcMemOp4Frm");
  568. HANDLE_OPERAND(roRegister)
  569. HANDLE_OPERAND(vvvvRegister)
  570. HANDLE_OPERAND(immediate) // Register in imm[7:4]
  571. HANDLE_OPERAND(memory)
  572. HANDLE_OPTIONAL(immediate)
  573. break;
  574. case X86Local::MRMSrcMemCC:
  575. assert(numPhysicalOperands == 3 &&
  576. "Unexpected number of operands for MRMSrcMemCC");
  577. HANDLE_OPERAND(roRegister)
  578. HANDLE_OPERAND(memory)
  579. HANDLE_OPERAND(opcodeModifier)
  580. break;
  581. case X86Local::MRMXrCC:
  582. assert(numPhysicalOperands == 2 &&
  583. "Unexpected number of operands for MRMXrCC");
  584. HANDLE_OPERAND(rmRegister)
  585. HANDLE_OPERAND(opcodeModifier)
  586. break;
  587. case X86Local::MRMr0:
  588. // Operand 1 is a register operand in the R/M field.
  589. HANDLE_OPERAND(roRegister)
  590. break;
  591. case X86Local::MRMXr:
  592. case X86Local::MRM0r:
  593. case X86Local::MRM1r:
  594. case X86Local::MRM2r:
  595. case X86Local::MRM3r:
  596. case X86Local::MRM4r:
  597. case X86Local::MRM5r:
  598. case X86Local::MRM6r:
  599. case X86Local::MRM7r:
  600. // Operand 1 is a register operand in the R/M field.
  601. // Operand 2 (optional) is an immediate or relocation.
  602. // Operand 3 (optional) is an immediate.
  603. assert(numPhysicalOperands >= 0 + additionalOperands &&
  604. numPhysicalOperands <= 3 + additionalOperands &&
  605. "Unexpected number of operands for MRMnr");
  606. if (HasVEX_4V)
  607. HANDLE_OPERAND(vvvvRegister)
  608. if (HasEVEX_K)
  609. HANDLE_OPERAND(writemaskRegister)
  610. HANDLE_OPTIONAL(rmRegister)
  611. HANDLE_OPTIONAL(relocation)
  612. HANDLE_OPTIONAL(immediate)
  613. break;
  614. case X86Local::MRMXmCC:
  615. assert(numPhysicalOperands == 2 &&
  616. "Unexpected number of operands for MRMXm");
  617. HANDLE_OPERAND(memory)
  618. HANDLE_OPERAND(opcodeModifier)
  619. break;
  620. case X86Local::MRMXm:
  621. case X86Local::MRM0m:
  622. case X86Local::MRM1m:
  623. case X86Local::MRM2m:
  624. case X86Local::MRM3m:
  625. case X86Local::MRM4m:
  626. case X86Local::MRM5m:
  627. case X86Local::MRM6m:
  628. case X86Local::MRM7m:
  629. // Operand 1 is a memory operand (possibly SIB-extended)
  630. // Operand 2 (optional) is an immediate or relocation.
  631. assert(numPhysicalOperands >= 1 + additionalOperands &&
  632. numPhysicalOperands <= 2 + additionalOperands &&
  633. "Unexpected number of operands for MRMnm");
  634. if (HasVEX_4V)
  635. HANDLE_OPERAND(vvvvRegister)
  636. if (HasEVEX_K)
  637. HANDLE_OPERAND(writemaskRegister)
  638. HANDLE_OPERAND(memory)
  639. HANDLE_OPTIONAL(relocation)
  640. break;
  641. case X86Local::RawFrmImm8:
  642. // operand 1 is a 16-bit immediate
  643. // operand 2 is an 8-bit immediate
  644. assert(numPhysicalOperands == 2 &&
  645. "Unexpected number of operands for X86Local::RawFrmImm8");
  646. HANDLE_OPERAND(immediate)
  647. HANDLE_OPERAND(immediate)
  648. break;
  649. case X86Local::RawFrmImm16:
  650. // operand 1 is a 16-bit immediate
  651. // operand 2 is a 16-bit immediate
  652. HANDLE_OPERAND(immediate)
  653. HANDLE_OPERAND(immediate)
  654. break;
  655. case X86Local::MRM0X:
  656. case X86Local::MRM1X:
  657. case X86Local::MRM2X:
  658. case X86Local::MRM3X:
  659. case X86Local::MRM4X:
  660. case X86Local::MRM5X:
  661. case X86Local::MRM6X:
  662. case X86Local::MRM7X:
  663. #define MAP(from, to) case X86Local::MRM_##from:
  664. X86_INSTR_MRM_MAPPING
  665. #undef MAP
  666. HANDLE_OPTIONAL(relocation)
  667. break;
  668. }
  669. #undef HANDLE_OPERAND
  670. #undef HANDLE_OPTIONAL
  671. }
  672. void RecognizableInstr::emitDecodePath(DisassemblerTables &tables) const {
  673. // Special cases where the LLVM tables are not complete
  674. #define MAP(from, to) \
  675. case X86Local::MRM_##from:
  676. llvm::Optional<OpcodeType> opcodeType;
  677. switch (OpMap) {
  678. default: llvm_unreachable("Invalid map!");
  679. case X86Local::OB: opcodeType = ONEBYTE; break;
  680. case X86Local::TB: opcodeType = TWOBYTE; break;
  681. case X86Local::T8: opcodeType = THREEBYTE_38; break;
  682. case X86Local::TA: opcodeType = THREEBYTE_3A; break;
  683. case X86Local::XOP8: opcodeType = XOP8_MAP; break;
  684. case X86Local::XOP9: opcodeType = XOP9_MAP; break;
  685. case X86Local::XOPA: opcodeType = XOPA_MAP; break;
  686. case X86Local::ThreeDNow: opcodeType = THREEDNOW_MAP; break;
  687. case X86Local::T_MAP5: opcodeType = MAP5; break;
  688. case X86Local::T_MAP6: opcodeType = MAP6; break;
  689. }
  690. std::unique_ptr<ModRMFilter> filter;
  691. switch (Form) {
  692. default: llvm_unreachable("Invalid form!");
  693. case X86Local::Pseudo: llvm_unreachable("Pseudo should not be emitted!");
  694. case X86Local::RawFrm:
  695. case X86Local::AddRegFrm:
  696. case X86Local::RawFrmMemOffs:
  697. case X86Local::RawFrmSrc:
  698. case X86Local::RawFrmDst:
  699. case X86Local::RawFrmDstSrc:
  700. case X86Local::RawFrmImm8:
  701. case X86Local::RawFrmImm16:
  702. case X86Local::AddCCFrm:
  703. case X86Local::PrefixByte:
  704. filter = std::make_unique<DumbFilter>();
  705. break;
  706. case X86Local::MRMDestReg:
  707. case X86Local::MRMSrcReg:
  708. case X86Local::MRMSrcReg4VOp3:
  709. case X86Local::MRMSrcRegOp4:
  710. case X86Local::MRMSrcRegCC:
  711. case X86Local::MRMXrCC:
  712. case X86Local::MRMXr:
  713. filter = std::make_unique<ModFilter>(true);
  714. break;
  715. case X86Local::MRMDestMem:
  716. case X86Local::MRMDestMemFSIB:
  717. case X86Local::MRMSrcMem:
  718. case X86Local::MRMSrcMemFSIB:
  719. case X86Local::MRMSrcMem4VOp3:
  720. case X86Local::MRMSrcMemOp4:
  721. case X86Local::MRMSrcMemCC:
  722. case X86Local::MRMXmCC:
  723. case X86Local::MRMXm:
  724. filter = std::make_unique<ModFilter>(false);
  725. break;
  726. case X86Local::MRM0r: case X86Local::MRM1r:
  727. case X86Local::MRM2r: case X86Local::MRM3r:
  728. case X86Local::MRM4r: case X86Local::MRM5r:
  729. case X86Local::MRM6r: case X86Local::MRM7r:
  730. filter = std::make_unique<ExtendedFilter>(true, Form - X86Local::MRM0r);
  731. break;
  732. case X86Local::MRM0X: case X86Local::MRM1X:
  733. case X86Local::MRM2X: case X86Local::MRM3X:
  734. case X86Local::MRM4X: case X86Local::MRM5X:
  735. case X86Local::MRM6X: case X86Local::MRM7X:
  736. filter = std::make_unique<ExtendedFilter>(true, Form - X86Local::MRM0X);
  737. break;
  738. case X86Local::MRMr0:
  739. filter = std::make_unique<ExtendedRMFilter>(true, Form - X86Local::MRMr0);
  740. break;
  741. case X86Local::MRM0m: case X86Local::MRM1m:
  742. case X86Local::MRM2m: case X86Local::MRM3m:
  743. case X86Local::MRM4m: case X86Local::MRM5m:
  744. case X86Local::MRM6m: case X86Local::MRM7m:
  745. filter = std::make_unique<ExtendedFilter>(false, Form - X86Local::MRM0m);
  746. break;
  747. X86_INSTR_MRM_MAPPING
  748. filter = std::make_unique<ExactFilter>(0xC0 + Form - X86Local::MRM_C0);
  749. break;
  750. } // switch (Form)
  751. uint8_t opcodeToSet = Opcode;
  752. unsigned AddressSize = 0;
  753. switch (AdSize) {
  754. case X86Local::AdSize16: AddressSize = 16; break;
  755. case X86Local::AdSize32: AddressSize = 32; break;
  756. case X86Local::AdSize64: AddressSize = 64; break;
  757. }
  758. assert(opcodeType && "Opcode type not set");
  759. assert(filter && "Filter not set");
  760. if (Form == X86Local::AddRegFrm || Form == X86Local::MRMSrcRegCC ||
  761. Form == X86Local::MRMSrcMemCC || Form == X86Local::MRMXrCC ||
  762. Form == X86Local::MRMXmCC || Form == X86Local::AddCCFrm) {
  763. unsigned Count = Form == X86Local::AddRegFrm ? 8 : 16;
  764. assert(((opcodeToSet % Count) == 0) && "ADDREG_FRM opcode not aligned");
  765. uint8_t currentOpcode;
  766. for (currentOpcode = opcodeToSet; currentOpcode < opcodeToSet + Count;
  767. ++currentOpcode)
  768. tables.setTableFields(*opcodeType, insnContext(), currentOpcode, *filter,
  769. UID, Is32Bit, OpPrefix == 0,
  770. IgnoresVEX_L || EncodeRC,
  771. IgnoresVEX_W, AddressSize);
  772. } else {
  773. tables.setTableFields(*opcodeType, insnContext(), opcodeToSet, *filter, UID,
  774. Is32Bit, OpPrefix == 0, IgnoresVEX_L || EncodeRC,
  775. IgnoresVEX_W, AddressSize);
  776. }
  777. #undef MAP
  778. }
  779. #define TYPE(str, type) if (s == str) return type;
  780. OperandType RecognizableInstr::typeFromString(const std::string &s,
  781. bool hasREX_WPrefix,
  782. uint8_t OpSize) {
  783. if(hasREX_WPrefix) {
  784. // For instructions with a REX_W prefix, a declared 32-bit register encoding
  785. // is special.
  786. TYPE("GR32", TYPE_R32)
  787. }
  788. if(OpSize == X86Local::OpSize16) {
  789. // For OpSize16 instructions, a declared 16-bit register or
  790. // immediate encoding is special.
  791. TYPE("GR16", TYPE_Rv)
  792. } else if(OpSize == X86Local::OpSize32) {
  793. // For OpSize32 instructions, a declared 32-bit register or
  794. // immediate encoding is special.
  795. TYPE("GR32", TYPE_Rv)
  796. }
  797. TYPE("i16mem", TYPE_M)
  798. TYPE("i16imm", TYPE_IMM)
  799. TYPE("i16i8imm", TYPE_IMM)
  800. TYPE("GR16", TYPE_R16)
  801. TYPE("GR16orGR32orGR64", TYPE_R16)
  802. TYPE("i32mem", TYPE_M)
  803. TYPE("i32imm", TYPE_IMM)
  804. TYPE("i32i8imm", TYPE_IMM)
  805. TYPE("GR32", TYPE_R32)
  806. TYPE("GR32orGR64", TYPE_R32)
  807. TYPE("i64mem", TYPE_M)
  808. TYPE("i64i32imm", TYPE_IMM)
  809. TYPE("i64i8imm", TYPE_IMM)
  810. TYPE("GR64", TYPE_R64)
  811. TYPE("i8mem", TYPE_M)
  812. TYPE("i8imm", TYPE_IMM)
  813. TYPE("u4imm", TYPE_UIMM8)
  814. TYPE("u8imm", TYPE_UIMM8)
  815. TYPE("i16u8imm", TYPE_UIMM8)
  816. TYPE("i32u8imm", TYPE_UIMM8)
  817. TYPE("i64u8imm", TYPE_UIMM8)
  818. TYPE("GR8", TYPE_R8)
  819. TYPE("VR128", TYPE_XMM)
  820. TYPE("VR128X", TYPE_XMM)
  821. TYPE("f128mem", TYPE_M)
  822. TYPE("f256mem", TYPE_M)
  823. TYPE("f512mem", TYPE_M)
  824. TYPE("FR128", TYPE_XMM)
  825. TYPE("FR64", TYPE_XMM)
  826. TYPE("FR64X", TYPE_XMM)
  827. TYPE("f64mem", TYPE_M)
  828. TYPE("sdmem", TYPE_M)
  829. TYPE("FR16X", TYPE_XMM)
  830. TYPE("FR32", TYPE_XMM)
  831. TYPE("FR32X", TYPE_XMM)
  832. TYPE("f32mem", TYPE_M)
  833. TYPE("f16mem", TYPE_M)
  834. TYPE("ssmem", TYPE_M)
  835. TYPE("shmem", TYPE_M)
  836. TYPE("RST", TYPE_ST)
  837. TYPE("RSTi", TYPE_ST)
  838. TYPE("i128mem", TYPE_M)
  839. TYPE("i256mem", TYPE_M)
  840. TYPE("i512mem", TYPE_M)
  841. TYPE("i64i32imm_brtarget", TYPE_REL)
  842. TYPE("i16imm_brtarget", TYPE_REL)
  843. TYPE("i32imm_brtarget", TYPE_REL)
  844. TYPE("ccode", TYPE_IMM)
  845. TYPE("AVX512RC", TYPE_IMM)
  846. TYPE("brtarget32", TYPE_REL)
  847. TYPE("brtarget16", TYPE_REL)
  848. TYPE("brtarget8", TYPE_REL)
  849. TYPE("f80mem", TYPE_M)
  850. TYPE("lea64_32mem", TYPE_M)
  851. TYPE("lea64mem", TYPE_M)
  852. TYPE("VR64", TYPE_MM64)
  853. TYPE("i64imm", TYPE_IMM)
  854. TYPE("anymem", TYPE_M)
  855. TYPE("opaquemem", TYPE_M)
  856. TYPE("sibmem", TYPE_MSIB)
  857. TYPE("SEGMENT_REG", TYPE_SEGMENTREG)
  858. TYPE("DEBUG_REG", TYPE_DEBUGREG)
  859. TYPE("CONTROL_REG", TYPE_CONTROLREG)
  860. TYPE("srcidx8", TYPE_SRCIDX)
  861. TYPE("srcidx16", TYPE_SRCIDX)
  862. TYPE("srcidx32", TYPE_SRCIDX)
  863. TYPE("srcidx64", TYPE_SRCIDX)
  864. TYPE("dstidx8", TYPE_DSTIDX)
  865. TYPE("dstidx16", TYPE_DSTIDX)
  866. TYPE("dstidx32", TYPE_DSTIDX)
  867. TYPE("dstidx64", TYPE_DSTIDX)
  868. TYPE("offset16_8", TYPE_MOFFS)
  869. TYPE("offset16_16", TYPE_MOFFS)
  870. TYPE("offset16_32", TYPE_MOFFS)
  871. TYPE("offset32_8", TYPE_MOFFS)
  872. TYPE("offset32_16", TYPE_MOFFS)
  873. TYPE("offset32_32", TYPE_MOFFS)
  874. TYPE("offset32_64", TYPE_MOFFS)
  875. TYPE("offset64_8", TYPE_MOFFS)
  876. TYPE("offset64_16", TYPE_MOFFS)
  877. TYPE("offset64_32", TYPE_MOFFS)
  878. TYPE("offset64_64", TYPE_MOFFS)
  879. TYPE("VR256", TYPE_YMM)
  880. TYPE("VR256X", TYPE_YMM)
  881. TYPE("VR512", TYPE_ZMM)
  882. TYPE("VK1", TYPE_VK)
  883. TYPE("VK1WM", TYPE_VK)
  884. TYPE("VK2", TYPE_VK)
  885. TYPE("VK2WM", TYPE_VK)
  886. TYPE("VK4", TYPE_VK)
  887. TYPE("VK4WM", TYPE_VK)
  888. TYPE("VK8", TYPE_VK)
  889. TYPE("VK8WM", TYPE_VK)
  890. TYPE("VK16", TYPE_VK)
  891. TYPE("VK16WM", TYPE_VK)
  892. TYPE("VK32", TYPE_VK)
  893. TYPE("VK32WM", TYPE_VK)
  894. TYPE("VK64", TYPE_VK)
  895. TYPE("VK64WM", TYPE_VK)
  896. TYPE("VK1Pair", TYPE_VK_PAIR)
  897. TYPE("VK2Pair", TYPE_VK_PAIR)
  898. TYPE("VK4Pair", TYPE_VK_PAIR)
  899. TYPE("VK8Pair", TYPE_VK_PAIR)
  900. TYPE("VK16Pair", TYPE_VK_PAIR)
  901. TYPE("vx64mem", TYPE_MVSIBX)
  902. TYPE("vx128mem", TYPE_MVSIBX)
  903. TYPE("vx256mem", TYPE_MVSIBX)
  904. TYPE("vy128mem", TYPE_MVSIBY)
  905. TYPE("vy256mem", TYPE_MVSIBY)
  906. TYPE("vx64xmem", TYPE_MVSIBX)
  907. TYPE("vx128xmem", TYPE_MVSIBX)
  908. TYPE("vx256xmem", TYPE_MVSIBX)
  909. TYPE("vy128xmem", TYPE_MVSIBY)
  910. TYPE("vy256xmem", TYPE_MVSIBY)
  911. TYPE("vy512xmem", TYPE_MVSIBY)
  912. TYPE("vz256mem", TYPE_MVSIBZ)
  913. TYPE("vz512mem", TYPE_MVSIBZ)
  914. TYPE("BNDR", TYPE_BNDR)
  915. TYPE("TILE", TYPE_TMM)
  916. errs() << "Unhandled type string " << s << "\n";
  917. llvm_unreachable("Unhandled type string");
  918. }
  919. #undef TYPE
  920. #define ENCODING(str, encoding) if (s == str) return encoding;
  921. OperandEncoding
  922. RecognizableInstr::immediateEncodingFromString(const std::string &s,
  923. uint8_t OpSize) {
  924. if(OpSize != X86Local::OpSize16) {
  925. // For instructions without an OpSize prefix, a declared 16-bit register or
  926. // immediate encoding is special.
  927. ENCODING("i16imm", ENCODING_IW)
  928. }
  929. ENCODING("i32i8imm", ENCODING_IB)
  930. ENCODING("AVX512RC", ENCODING_IRC)
  931. ENCODING("i16imm", ENCODING_Iv)
  932. ENCODING("i16i8imm", ENCODING_IB)
  933. ENCODING("i32imm", ENCODING_Iv)
  934. ENCODING("i64i32imm", ENCODING_ID)
  935. ENCODING("i64i8imm", ENCODING_IB)
  936. ENCODING("i8imm", ENCODING_IB)
  937. ENCODING("u4imm", ENCODING_IB)
  938. ENCODING("u8imm", ENCODING_IB)
  939. ENCODING("i16u8imm", ENCODING_IB)
  940. ENCODING("i32u8imm", ENCODING_IB)
  941. ENCODING("i64u8imm", ENCODING_IB)
  942. // This is not a typo. Instructions like BLENDVPD put
  943. // register IDs in 8-bit immediates nowadays.
  944. ENCODING("FR32", ENCODING_IB)
  945. ENCODING("FR64", ENCODING_IB)
  946. ENCODING("FR128", ENCODING_IB)
  947. ENCODING("VR128", ENCODING_IB)
  948. ENCODING("VR256", ENCODING_IB)
  949. ENCODING("FR16X", ENCODING_IB)
  950. ENCODING("FR32X", ENCODING_IB)
  951. ENCODING("FR64X", ENCODING_IB)
  952. ENCODING("VR128X", ENCODING_IB)
  953. ENCODING("VR256X", ENCODING_IB)
  954. ENCODING("VR512", ENCODING_IB)
  955. ENCODING("TILE", ENCODING_IB)
  956. errs() << "Unhandled immediate encoding " << s << "\n";
  957. llvm_unreachable("Unhandled immediate encoding");
  958. }
  959. OperandEncoding
  960. RecognizableInstr::rmRegisterEncodingFromString(const std::string &s,
  961. uint8_t OpSize) {
  962. ENCODING("RST", ENCODING_FP)
  963. ENCODING("RSTi", ENCODING_FP)
  964. ENCODING("GR16", ENCODING_RM)
  965. ENCODING("GR16orGR32orGR64",ENCODING_RM)
  966. ENCODING("GR32", ENCODING_RM)
  967. ENCODING("GR32orGR64", ENCODING_RM)
  968. ENCODING("GR64", ENCODING_RM)
  969. ENCODING("GR8", ENCODING_RM)
  970. ENCODING("VR128", ENCODING_RM)
  971. ENCODING("VR128X", ENCODING_RM)
  972. ENCODING("FR128", ENCODING_RM)
  973. ENCODING("FR64", ENCODING_RM)
  974. ENCODING("FR32", ENCODING_RM)
  975. ENCODING("FR64X", ENCODING_RM)
  976. ENCODING("FR32X", ENCODING_RM)
  977. ENCODING("FR16X", ENCODING_RM)
  978. ENCODING("VR64", ENCODING_RM)
  979. ENCODING("VR256", ENCODING_RM)
  980. ENCODING("VR256X", ENCODING_RM)
  981. ENCODING("VR512", ENCODING_RM)
  982. ENCODING("VK1", ENCODING_RM)
  983. ENCODING("VK2", ENCODING_RM)
  984. ENCODING("VK4", ENCODING_RM)
  985. ENCODING("VK8", ENCODING_RM)
  986. ENCODING("VK16", ENCODING_RM)
  987. ENCODING("VK32", ENCODING_RM)
  988. ENCODING("VK64", ENCODING_RM)
  989. ENCODING("BNDR", ENCODING_RM)
  990. ENCODING("TILE", ENCODING_RM)
  991. errs() << "Unhandled R/M register encoding " << s << "\n";
  992. llvm_unreachable("Unhandled R/M register encoding");
  993. }
  994. OperandEncoding
  995. RecognizableInstr::roRegisterEncodingFromString(const std::string &s,
  996. uint8_t OpSize) {
  997. ENCODING("GR16", ENCODING_REG)
  998. ENCODING("GR16orGR32orGR64",ENCODING_REG)
  999. ENCODING("GR32", ENCODING_REG)
  1000. ENCODING("GR32orGR64", ENCODING_REG)
  1001. ENCODING("GR64", ENCODING_REG)
  1002. ENCODING("GR8", ENCODING_REG)
  1003. ENCODING("VR128", ENCODING_REG)
  1004. ENCODING("FR128", ENCODING_REG)
  1005. ENCODING("FR64", ENCODING_REG)
  1006. ENCODING("FR32", ENCODING_REG)
  1007. ENCODING("VR64", ENCODING_REG)
  1008. ENCODING("SEGMENT_REG", ENCODING_REG)
  1009. ENCODING("DEBUG_REG", ENCODING_REG)
  1010. ENCODING("CONTROL_REG", ENCODING_REG)
  1011. ENCODING("VR256", ENCODING_REG)
  1012. ENCODING("VR256X", ENCODING_REG)
  1013. ENCODING("VR128X", ENCODING_REG)
  1014. ENCODING("FR64X", ENCODING_REG)
  1015. ENCODING("FR32X", ENCODING_REG)
  1016. ENCODING("FR16X", ENCODING_REG)
  1017. ENCODING("VR512", ENCODING_REG)
  1018. ENCODING("VK1", ENCODING_REG)
  1019. ENCODING("VK2", ENCODING_REG)
  1020. ENCODING("VK4", ENCODING_REG)
  1021. ENCODING("VK8", ENCODING_REG)
  1022. ENCODING("VK16", ENCODING_REG)
  1023. ENCODING("VK32", ENCODING_REG)
  1024. ENCODING("VK64", ENCODING_REG)
  1025. ENCODING("VK1Pair", ENCODING_REG)
  1026. ENCODING("VK2Pair", ENCODING_REG)
  1027. ENCODING("VK4Pair", ENCODING_REG)
  1028. ENCODING("VK8Pair", ENCODING_REG)
  1029. ENCODING("VK16Pair", ENCODING_REG)
  1030. ENCODING("VK1WM", ENCODING_REG)
  1031. ENCODING("VK2WM", ENCODING_REG)
  1032. ENCODING("VK4WM", ENCODING_REG)
  1033. ENCODING("VK8WM", ENCODING_REG)
  1034. ENCODING("VK16WM", ENCODING_REG)
  1035. ENCODING("VK32WM", ENCODING_REG)
  1036. ENCODING("VK64WM", ENCODING_REG)
  1037. ENCODING("BNDR", ENCODING_REG)
  1038. ENCODING("TILE", ENCODING_REG)
  1039. errs() << "Unhandled reg/opcode register encoding " << s << "\n";
  1040. llvm_unreachable("Unhandled reg/opcode register encoding");
  1041. }
  1042. OperandEncoding
  1043. RecognizableInstr::vvvvRegisterEncodingFromString(const std::string &s,
  1044. uint8_t OpSize) {
  1045. ENCODING("GR32", ENCODING_VVVV)
  1046. ENCODING("GR64", ENCODING_VVVV)
  1047. ENCODING("FR32", ENCODING_VVVV)
  1048. ENCODING("FR128", ENCODING_VVVV)
  1049. ENCODING("FR64", ENCODING_VVVV)
  1050. ENCODING("VR128", ENCODING_VVVV)
  1051. ENCODING("VR256", ENCODING_VVVV)
  1052. ENCODING("FR16X", ENCODING_VVVV)
  1053. ENCODING("FR32X", ENCODING_VVVV)
  1054. ENCODING("FR64X", ENCODING_VVVV)
  1055. ENCODING("VR128X", ENCODING_VVVV)
  1056. ENCODING("VR256X", ENCODING_VVVV)
  1057. ENCODING("VR512", ENCODING_VVVV)
  1058. ENCODING("VK1", ENCODING_VVVV)
  1059. ENCODING("VK2", ENCODING_VVVV)
  1060. ENCODING("VK4", ENCODING_VVVV)
  1061. ENCODING("VK8", ENCODING_VVVV)
  1062. ENCODING("VK16", ENCODING_VVVV)
  1063. ENCODING("VK32", ENCODING_VVVV)
  1064. ENCODING("VK64", ENCODING_VVVV)
  1065. ENCODING("TILE", ENCODING_VVVV)
  1066. errs() << "Unhandled VEX.vvvv register encoding " << s << "\n";
  1067. llvm_unreachable("Unhandled VEX.vvvv register encoding");
  1068. }
  1069. OperandEncoding
  1070. RecognizableInstr::writemaskRegisterEncodingFromString(const std::string &s,
  1071. uint8_t OpSize) {
  1072. ENCODING("VK1WM", ENCODING_WRITEMASK)
  1073. ENCODING("VK2WM", ENCODING_WRITEMASK)
  1074. ENCODING("VK4WM", ENCODING_WRITEMASK)
  1075. ENCODING("VK8WM", ENCODING_WRITEMASK)
  1076. ENCODING("VK16WM", ENCODING_WRITEMASK)
  1077. ENCODING("VK32WM", ENCODING_WRITEMASK)
  1078. ENCODING("VK64WM", ENCODING_WRITEMASK)
  1079. errs() << "Unhandled mask register encoding " << s << "\n";
  1080. llvm_unreachable("Unhandled mask register encoding");
  1081. }
  1082. OperandEncoding
  1083. RecognizableInstr::memoryEncodingFromString(const std::string &s,
  1084. uint8_t OpSize) {
  1085. ENCODING("i16mem", ENCODING_RM)
  1086. ENCODING("i32mem", ENCODING_RM)
  1087. ENCODING("i64mem", ENCODING_RM)
  1088. ENCODING("i8mem", ENCODING_RM)
  1089. ENCODING("shmem", ENCODING_RM)
  1090. ENCODING("ssmem", ENCODING_RM)
  1091. ENCODING("sdmem", ENCODING_RM)
  1092. ENCODING("f128mem", ENCODING_RM)
  1093. ENCODING("f256mem", ENCODING_RM)
  1094. ENCODING("f512mem", ENCODING_RM)
  1095. ENCODING("f64mem", ENCODING_RM)
  1096. ENCODING("f32mem", ENCODING_RM)
  1097. ENCODING("f16mem", ENCODING_RM)
  1098. ENCODING("i128mem", ENCODING_RM)
  1099. ENCODING("i256mem", ENCODING_RM)
  1100. ENCODING("i512mem", ENCODING_RM)
  1101. ENCODING("f80mem", ENCODING_RM)
  1102. ENCODING("lea64_32mem", ENCODING_RM)
  1103. ENCODING("lea64mem", ENCODING_RM)
  1104. ENCODING("anymem", ENCODING_RM)
  1105. ENCODING("opaquemem", ENCODING_RM)
  1106. ENCODING("sibmem", ENCODING_SIB)
  1107. ENCODING("vx64mem", ENCODING_VSIB)
  1108. ENCODING("vx128mem", ENCODING_VSIB)
  1109. ENCODING("vx256mem", ENCODING_VSIB)
  1110. ENCODING("vy128mem", ENCODING_VSIB)
  1111. ENCODING("vy256mem", ENCODING_VSIB)
  1112. ENCODING("vx64xmem", ENCODING_VSIB)
  1113. ENCODING("vx128xmem", ENCODING_VSIB)
  1114. ENCODING("vx256xmem", ENCODING_VSIB)
  1115. ENCODING("vy128xmem", ENCODING_VSIB)
  1116. ENCODING("vy256xmem", ENCODING_VSIB)
  1117. ENCODING("vy512xmem", ENCODING_VSIB)
  1118. ENCODING("vz256mem", ENCODING_VSIB)
  1119. ENCODING("vz512mem", ENCODING_VSIB)
  1120. errs() << "Unhandled memory encoding " << s << "\n";
  1121. llvm_unreachable("Unhandled memory encoding");
  1122. }
  1123. OperandEncoding
  1124. RecognizableInstr::relocationEncodingFromString(const std::string &s,
  1125. uint8_t OpSize) {
  1126. if(OpSize != X86Local::OpSize16) {
  1127. // For instructions without an OpSize prefix, a declared 16-bit register or
  1128. // immediate encoding is special.
  1129. ENCODING("i16imm", ENCODING_IW)
  1130. }
  1131. ENCODING("i16imm", ENCODING_Iv)
  1132. ENCODING("i16i8imm", ENCODING_IB)
  1133. ENCODING("i32imm", ENCODING_Iv)
  1134. ENCODING("i32i8imm", ENCODING_IB)
  1135. ENCODING("i64i32imm", ENCODING_ID)
  1136. ENCODING("i64i8imm", ENCODING_IB)
  1137. ENCODING("i8imm", ENCODING_IB)
  1138. ENCODING("u8imm", ENCODING_IB)
  1139. ENCODING("i16u8imm", ENCODING_IB)
  1140. ENCODING("i32u8imm", ENCODING_IB)
  1141. ENCODING("i64u8imm", ENCODING_IB)
  1142. ENCODING("i64i32imm_brtarget", ENCODING_ID)
  1143. ENCODING("i16imm_brtarget", ENCODING_IW)
  1144. ENCODING("i32imm_brtarget", ENCODING_ID)
  1145. ENCODING("brtarget32", ENCODING_ID)
  1146. ENCODING("brtarget16", ENCODING_IW)
  1147. ENCODING("brtarget8", ENCODING_IB)
  1148. ENCODING("i64imm", ENCODING_IO)
  1149. ENCODING("offset16_8", ENCODING_Ia)
  1150. ENCODING("offset16_16", ENCODING_Ia)
  1151. ENCODING("offset16_32", ENCODING_Ia)
  1152. ENCODING("offset32_8", ENCODING_Ia)
  1153. ENCODING("offset32_16", ENCODING_Ia)
  1154. ENCODING("offset32_32", ENCODING_Ia)
  1155. ENCODING("offset32_64", ENCODING_Ia)
  1156. ENCODING("offset64_8", ENCODING_Ia)
  1157. ENCODING("offset64_16", ENCODING_Ia)
  1158. ENCODING("offset64_32", ENCODING_Ia)
  1159. ENCODING("offset64_64", ENCODING_Ia)
  1160. ENCODING("srcidx8", ENCODING_SI)
  1161. ENCODING("srcidx16", ENCODING_SI)
  1162. ENCODING("srcidx32", ENCODING_SI)
  1163. ENCODING("srcidx64", ENCODING_SI)
  1164. ENCODING("dstidx8", ENCODING_DI)
  1165. ENCODING("dstidx16", ENCODING_DI)
  1166. ENCODING("dstidx32", ENCODING_DI)
  1167. ENCODING("dstidx64", ENCODING_DI)
  1168. errs() << "Unhandled relocation encoding " << s << "\n";
  1169. llvm_unreachable("Unhandled relocation encoding");
  1170. }
  1171. OperandEncoding
  1172. RecognizableInstr::opcodeModifierEncodingFromString(const std::string &s,
  1173. uint8_t OpSize) {
  1174. ENCODING("GR32", ENCODING_Rv)
  1175. ENCODING("GR64", ENCODING_RO)
  1176. ENCODING("GR16", ENCODING_Rv)
  1177. ENCODING("GR8", ENCODING_RB)
  1178. ENCODING("ccode", ENCODING_CC)
  1179. errs() << "Unhandled opcode modifier encoding " << s << "\n";
  1180. llvm_unreachable("Unhandled opcode modifier encoding");
  1181. }
  1182. #undef ENCODING