X86RecognizableInstr.cpp 49 KB

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