AsmWriterEmitter.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  1. //===- AsmWriterEmitter.cpp - Generate an assembly writer -----------------===//
  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 tablegen backend emits an assembly printer for the current target.
  10. // Note that this is currently fairly skeletal, but will grow over time.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "AsmWriterInst.h"
  14. #include "CodeGenInstruction.h"
  15. #include "CodeGenRegisters.h"
  16. #include "CodeGenTarget.h"
  17. #include "SequenceToOffsetTable.h"
  18. #include "Types.h"
  19. #include "llvm/ADT/ArrayRef.h"
  20. #include "llvm/ADT/DenseMap.h"
  21. #include "llvm/ADT/SmallString.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/ADT/STLExtras.h"
  24. #include "llvm/ADT/StringExtras.h"
  25. #include "llvm/ADT/StringRef.h"
  26. #include "llvm/ADT/Twine.h"
  27. #include "llvm/Support/Casting.h"
  28. #include "llvm/Support/Debug.h"
  29. #include "llvm/Support/ErrorHandling.h"
  30. #include "llvm/Support/Format.h"
  31. #include "llvm/Support/FormatVariadic.h"
  32. #include "llvm/Support/MathExtras.h"
  33. #include "llvm/Support/raw_ostream.h"
  34. #include "llvm/TableGen/Error.h"
  35. #include "llvm/TableGen/Record.h"
  36. #include "llvm/TableGen/TableGenBackend.h"
  37. #include <algorithm>
  38. #include <cassert>
  39. #include <cstddef>
  40. #include <cstdint>
  41. #include <deque>
  42. #include <iterator>
  43. #include <map>
  44. #include <set>
  45. #include <string>
  46. #include <tuple>
  47. #include <utility>
  48. #include <vector>
  49. using namespace llvm;
  50. #define DEBUG_TYPE "asm-writer-emitter"
  51. namespace {
  52. class AsmWriterEmitter {
  53. RecordKeeper &Records;
  54. CodeGenTarget Target;
  55. ArrayRef<const CodeGenInstruction *> NumberedInstructions;
  56. std::vector<AsmWriterInst> Instructions;
  57. public:
  58. AsmWriterEmitter(RecordKeeper &R);
  59. void run(raw_ostream &o);
  60. private:
  61. void EmitGetMnemonic(
  62. raw_ostream &o,
  63. std::vector<std::vector<std::string>> &TableDrivenOperandPrinters,
  64. unsigned &BitsLeft, unsigned &AsmStrBits);
  65. void EmitPrintInstruction(
  66. raw_ostream &o,
  67. std::vector<std::vector<std::string>> &TableDrivenOperandPrinters,
  68. unsigned &BitsLeft, unsigned &AsmStrBits);
  69. void EmitGetRegisterName(raw_ostream &o);
  70. void EmitPrintAliasInstruction(raw_ostream &O);
  71. void FindUniqueOperandCommands(std::vector<std::string> &UOC,
  72. std::vector<std::vector<unsigned>> &InstIdxs,
  73. std::vector<unsigned> &InstOpsUsed,
  74. bool PassSubtarget) const;
  75. };
  76. } // end anonymous namespace
  77. static void PrintCases(std::vector<std::pair<std::string,
  78. AsmWriterOperand>> &OpsToPrint, raw_ostream &O,
  79. bool PassSubtarget) {
  80. O << " case " << OpsToPrint.back().first << ":";
  81. AsmWriterOperand TheOp = OpsToPrint.back().second;
  82. OpsToPrint.pop_back();
  83. // Check to see if any other operands are identical in this list, and if so,
  84. // emit a case label for them.
  85. for (unsigned i = OpsToPrint.size(); i != 0; --i)
  86. if (OpsToPrint[i-1].second == TheOp) {
  87. O << "\n case " << OpsToPrint[i-1].first << ":";
  88. OpsToPrint.erase(OpsToPrint.begin()+i-1);
  89. }
  90. // Finally, emit the code.
  91. O << "\n " << TheOp.getCode(PassSubtarget);
  92. O << "\n break;\n";
  93. }
  94. /// EmitInstructions - Emit the last instruction in the vector and any other
  95. /// instructions that are suitably similar to it.
  96. static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
  97. raw_ostream &O, bool PassSubtarget) {
  98. AsmWriterInst FirstInst = Insts.back();
  99. Insts.pop_back();
  100. std::vector<AsmWriterInst> SimilarInsts;
  101. unsigned DifferingOperand = ~0;
  102. for (unsigned i = Insts.size(); i != 0; --i) {
  103. unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst);
  104. if (DiffOp != ~1U) {
  105. if (DifferingOperand == ~0U) // First match!
  106. DifferingOperand = DiffOp;
  107. // If this differs in the same operand as the rest of the instructions in
  108. // this class, move it to the SimilarInsts list.
  109. if (DifferingOperand == DiffOp || DiffOp == ~0U) {
  110. SimilarInsts.push_back(Insts[i-1]);
  111. Insts.erase(Insts.begin()+i-1);
  112. }
  113. }
  114. }
  115. O << " case " << FirstInst.CGI->Namespace << "::"
  116. << FirstInst.CGI->TheDef->getName() << ":\n";
  117. for (const AsmWriterInst &AWI : SimilarInsts)
  118. O << " case " << AWI.CGI->Namespace << "::"
  119. << AWI.CGI->TheDef->getName() << ":\n";
  120. for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
  121. if (i != DifferingOperand) {
  122. // If the operand is the same for all instructions, just print it.
  123. O << " " << FirstInst.Operands[i].getCode(PassSubtarget);
  124. } else {
  125. // If this is the operand that varies between all of the instructions,
  126. // emit a switch for just this operand now.
  127. O << " switch (MI->getOpcode()) {\n";
  128. O << " default: llvm_unreachable(\"Unexpected opcode.\");\n";
  129. std::vector<std::pair<std::string, AsmWriterOperand>> OpsToPrint;
  130. OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace.str() + "::" +
  131. FirstInst.CGI->TheDef->getName().str(),
  132. FirstInst.Operands[i]));
  133. for (const AsmWriterInst &AWI : SimilarInsts) {
  134. OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace.str()+"::" +
  135. AWI.CGI->TheDef->getName().str(),
  136. AWI.Operands[i]));
  137. }
  138. std::reverse(OpsToPrint.begin(), OpsToPrint.end());
  139. while (!OpsToPrint.empty())
  140. PrintCases(OpsToPrint, O, PassSubtarget);
  141. O << " }";
  142. }
  143. O << "\n";
  144. }
  145. O << " break;\n";
  146. }
  147. void AsmWriterEmitter::
  148. FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
  149. std::vector<std::vector<unsigned>> &InstIdxs,
  150. std::vector<unsigned> &InstOpsUsed,
  151. bool PassSubtarget) const {
  152. // This vector parallels UniqueOperandCommands, keeping track of which
  153. // instructions each case are used for. It is a comma separated string of
  154. // enums.
  155. std::vector<std::string> InstrsForCase;
  156. InstrsForCase.resize(UniqueOperandCommands.size());
  157. InstOpsUsed.assign(UniqueOperandCommands.size(), 0);
  158. for (size_t i = 0, e = Instructions.size(); i != e; ++i) {
  159. const AsmWriterInst &Inst = Instructions[i];
  160. if (Inst.Operands.empty())
  161. continue; // Instruction already done.
  162. std::string Command = " "+Inst.Operands[0].getCode(PassSubtarget)+"\n";
  163. // Check to see if we already have 'Command' in UniqueOperandCommands.
  164. // If not, add it.
  165. auto I = llvm::find(UniqueOperandCommands, Command);
  166. if (I != UniqueOperandCommands.end()) {
  167. size_t idx = I - UniqueOperandCommands.begin();
  168. InstrsForCase[idx] += ", ";
  169. InstrsForCase[idx] += Inst.CGI->TheDef->getName();
  170. InstIdxs[idx].push_back(i);
  171. } else {
  172. UniqueOperandCommands.push_back(std::move(Command));
  173. InstrsForCase.push_back(std::string(Inst.CGI->TheDef->getName()));
  174. InstIdxs.emplace_back();
  175. InstIdxs.back().push_back(i);
  176. // This command matches one operand so far.
  177. InstOpsUsed.push_back(1);
  178. }
  179. }
  180. // For each entry of UniqueOperandCommands, there is a set of instructions
  181. // that uses it. If the next command of all instructions in the set are
  182. // identical, fold it into the command.
  183. for (size_t CommandIdx = 0, e = UniqueOperandCommands.size();
  184. CommandIdx != e; ++CommandIdx) {
  185. const auto &Idxs = InstIdxs[CommandIdx];
  186. for (unsigned Op = 1; ; ++Op) {
  187. // Find the first instruction in the set.
  188. const AsmWriterInst &FirstInst = Instructions[Idxs.front()];
  189. // If this instruction has no more operands, we isn't anything to merge
  190. // into this command.
  191. if (FirstInst.Operands.size() == Op)
  192. break;
  193. // Otherwise, scan to see if all of the other instructions in this command
  194. // set share the operand.
  195. if (any_of(drop_begin(Idxs), [&](unsigned Idx) {
  196. const AsmWriterInst &OtherInst = Instructions[Idx];
  197. return OtherInst.Operands.size() == Op ||
  198. OtherInst.Operands[Op] != FirstInst.Operands[Op];
  199. }))
  200. break;
  201. // Okay, everything in this command set has the same next operand. Add it
  202. // to UniqueOperandCommands and remember that it was consumed.
  203. std::string Command = " " +
  204. FirstInst.Operands[Op].getCode(PassSubtarget) + "\n";
  205. UniqueOperandCommands[CommandIdx] += Command;
  206. InstOpsUsed[CommandIdx]++;
  207. }
  208. }
  209. // Prepend some of the instructions each case is used for onto the case val.
  210. for (unsigned i = 0, e = InstrsForCase.size(); i != e; ++i) {
  211. std::string Instrs = InstrsForCase[i];
  212. if (Instrs.size() > 70) {
  213. Instrs.erase(Instrs.begin()+70, Instrs.end());
  214. Instrs += "...";
  215. }
  216. if (!Instrs.empty())
  217. UniqueOperandCommands[i] = " // " + Instrs + "\n" +
  218. UniqueOperandCommands[i];
  219. }
  220. }
  221. static void UnescapeString(std::string &Str) {
  222. for (unsigned i = 0; i != Str.size(); ++i) {
  223. if (Str[i] == '\\' && i != Str.size()-1) {
  224. switch (Str[i+1]) {
  225. default: continue; // Don't execute the code after the switch.
  226. case 'a': Str[i] = '\a'; break;
  227. case 'b': Str[i] = '\b'; break;
  228. case 'e': Str[i] = 27; break;
  229. case 'f': Str[i] = '\f'; break;
  230. case 'n': Str[i] = '\n'; break;
  231. case 'r': Str[i] = '\r'; break;
  232. case 't': Str[i] = '\t'; break;
  233. case 'v': Str[i] = '\v'; break;
  234. case '"': Str[i] = '\"'; break;
  235. case '\'': Str[i] = '\''; break;
  236. case '\\': Str[i] = '\\'; break;
  237. }
  238. // Nuke the second character.
  239. Str.erase(Str.begin()+i+1);
  240. }
  241. }
  242. }
  243. /// UnescapeAliasString - Supports literal braces in InstAlias asm string which
  244. /// are escaped with '\\' to avoid being interpreted as variants. Braces must
  245. /// be unescaped before c++ code is generated as (e.g.):
  246. ///
  247. /// AsmString = "foo \{$\x01\}";
  248. ///
  249. /// causes non-standard escape character warnings.
  250. static void UnescapeAliasString(std::string &Str) {
  251. for (unsigned i = 0; i != Str.size(); ++i) {
  252. if (Str[i] == '\\' && i != Str.size()-1) {
  253. switch (Str[i+1]) {
  254. default: continue; // Don't execute the code after the switch.
  255. case '{': Str[i] = '{'; break;
  256. case '}': Str[i] = '}'; break;
  257. }
  258. // Nuke the second character.
  259. Str.erase(Str.begin()+i+1);
  260. }
  261. }
  262. }
  263. void AsmWriterEmitter::EmitGetMnemonic(
  264. raw_ostream &O,
  265. std::vector<std::vector<std::string>> &TableDrivenOperandPrinters,
  266. unsigned &BitsLeft, unsigned &AsmStrBits) {
  267. Record *AsmWriter = Target.getAsmWriter();
  268. StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
  269. bool PassSubtarget = AsmWriter->getValueAsInt("PassSubtarget");
  270. O << "/// getMnemonic - This method is automatically generated by "
  271. "tablegen\n"
  272. "/// from the instruction set description.\n"
  273. "std::pair<const char *, uint64_t> "
  274. << Target.getName() << ClassName << "::getMnemonic(const MCInst *MI) {\n";
  275. // Build an aggregate string, and build a table of offsets into it.
  276. SequenceToOffsetTable<std::string> StringTable;
  277. /// OpcodeInfo - This encodes the index of the string to use for the first
  278. /// chunk of the output as well as indices used for operand printing.
  279. std::vector<uint64_t> OpcodeInfo(NumberedInstructions.size());
  280. const unsigned OpcodeInfoBits = 64;
  281. // Add all strings to the string table upfront so it can generate an optimized
  282. // representation.
  283. for (AsmWriterInst &AWI : Instructions) {
  284. if (AWI.Operands[0].OperandType ==
  285. AsmWriterOperand::isLiteralTextOperand &&
  286. !AWI.Operands[0].Str.empty()) {
  287. std::string Str = AWI.Operands[0].Str;
  288. UnescapeString(Str);
  289. StringTable.add(Str);
  290. }
  291. }
  292. StringTable.layout();
  293. unsigned MaxStringIdx = 0;
  294. for (AsmWriterInst &AWI : Instructions) {
  295. unsigned Idx;
  296. if (AWI.Operands[0].OperandType != AsmWriterOperand::isLiteralTextOperand ||
  297. AWI.Operands[0].Str.empty()) {
  298. // Something handled by the asmwriter printer, but with no leading string.
  299. Idx = StringTable.get("");
  300. } else {
  301. std::string Str = AWI.Operands[0].Str;
  302. UnescapeString(Str);
  303. Idx = StringTable.get(Str);
  304. MaxStringIdx = std::max(MaxStringIdx, Idx);
  305. // Nuke the string from the operand list. It is now handled!
  306. AWI.Operands.erase(AWI.Operands.begin());
  307. }
  308. // Bias offset by one since we want 0 as a sentinel.
  309. OpcodeInfo[AWI.CGIIndex] = Idx+1;
  310. }
  311. // Figure out how many bits we used for the string index.
  312. AsmStrBits = Log2_32_Ceil(MaxStringIdx + 2);
  313. // To reduce code size, we compactify common instructions into a few bits
  314. // in the opcode-indexed table.
  315. BitsLeft = OpcodeInfoBits - AsmStrBits;
  316. while (true) {
  317. std::vector<std::string> UniqueOperandCommands;
  318. std::vector<std::vector<unsigned>> InstIdxs;
  319. std::vector<unsigned> NumInstOpsHandled;
  320. FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
  321. NumInstOpsHandled, PassSubtarget);
  322. // If we ran out of operands to print, we're done.
  323. if (UniqueOperandCommands.empty()) break;
  324. // Compute the number of bits we need to represent these cases, this is
  325. // ceil(log2(numentries)).
  326. unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size());
  327. // If we don't have enough bits for this operand, don't include it.
  328. if (NumBits > BitsLeft) {
  329. LLVM_DEBUG(errs() << "Not enough bits to densely encode " << NumBits
  330. << " more bits\n");
  331. break;
  332. }
  333. // Otherwise, we can include this in the initial lookup table. Add it in.
  334. for (size_t i = 0, e = InstIdxs.size(); i != e; ++i) {
  335. unsigned NumOps = NumInstOpsHandled[i];
  336. for (unsigned Idx : InstIdxs[i]) {
  337. OpcodeInfo[Instructions[Idx].CGIIndex] |=
  338. (uint64_t)i << (OpcodeInfoBits-BitsLeft);
  339. // Remove the info about this operand from the instruction.
  340. AsmWriterInst &Inst = Instructions[Idx];
  341. if (!Inst.Operands.empty()) {
  342. assert(NumOps <= Inst.Operands.size() &&
  343. "Can't remove this many ops!");
  344. Inst.Operands.erase(Inst.Operands.begin(),
  345. Inst.Operands.begin()+NumOps);
  346. }
  347. }
  348. }
  349. BitsLeft -= NumBits;
  350. // Remember the handlers for this set of operands.
  351. TableDrivenOperandPrinters.push_back(std::move(UniqueOperandCommands));
  352. }
  353. // Emit the string table itself.
  354. StringTable.emitStringLiteralDef(O, " static const char AsmStrs[]");
  355. // Emit the lookup tables in pieces to minimize wasted bytes.
  356. unsigned BytesNeeded = ((OpcodeInfoBits - BitsLeft) + 7) / 8;
  357. unsigned Table = 0, Shift = 0;
  358. SmallString<128> BitsString;
  359. raw_svector_ostream BitsOS(BitsString);
  360. // If the total bits is more than 32-bits we need to use a 64-bit type.
  361. BitsOS << " uint" << ((BitsLeft < (OpcodeInfoBits - 32)) ? 64 : 32)
  362. << "_t Bits = 0;\n";
  363. while (BytesNeeded != 0) {
  364. // Figure out how big this table section needs to be, but no bigger than 4.
  365. unsigned TableSize = std::min(1 << Log2_32(BytesNeeded), 4);
  366. BytesNeeded -= TableSize;
  367. TableSize *= 8; // Convert to bits;
  368. uint64_t Mask = (1ULL << TableSize) - 1;
  369. O << " static const uint" << TableSize << "_t OpInfo" << Table
  370. << "[] = {\n";
  371. for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
  372. O << " " << ((OpcodeInfo[i] >> Shift) & Mask) << "U,\t// "
  373. << NumberedInstructions[i]->TheDef->getName() << "\n";
  374. }
  375. O << " };\n\n";
  376. // Emit string to combine the individual table lookups.
  377. BitsOS << " Bits |= ";
  378. // If the total bits is more than 32-bits we need to use a 64-bit type.
  379. if (BitsLeft < (OpcodeInfoBits - 32))
  380. BitsOS << "(uint64_t)";
  381. BitsOS << "OpInfo" << Table << "[MI->getOpcode()] << " << Shift << ";\n";
  382. // Prepare the shift for the next iteration and increment the table count.
  383. Shift += TableSize;
  384. ++Table;
  385. }
  386. O << " // Emit the opcode for the instruction.\n";
  387. O << BitsString;
  388. // Return mnemonic string and bits.
  389. O << " return {AsmStrs+(Bits & " << (1 << AsmStrBits) - 1
  390. << ")-1, Bits};\n\n";
  391. O << "}\n";
  392. }
  393. /// EmitPrintInstruction - Generate the code for the "printInstruction" method
  394. /// implementation. Destroys all instances of AsmWriterInst information, by
  395. /// clearing the Instructions vector.
  396. void AsmWriterEmitter::EmitPrintInstruction(
  397. raw_ostream &O,
  398. std::vector<std::vector<std::string>> &TableDrivenOperandPrinters,
  399. unsigned &BitsLeft, unsigned &AsmStrBits) {
  400. const unsigned OpcodeInfoBits = 64;
  401. Record *AsmWriter = Target.getAsmWriter();
  402. StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
  403. bool PassSubtarget = AsmWriter->getValueAsInt("PassSubtarget");
  404. O << "/// printInstruction - This method is automatically generated by "
  405. "tablegen\n"
  406. "/// from the instruction set description.\n"
  407. "void "
  408. << Target.getName() << ClassName
  409. << "::printInstruction(const MCInst *MI, uint64_t Address, "
  410. << (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
  411. << "raw_ostream &O) {\n";
  412. // Emit the initial tab character.
  413. O << " O << \"\\t\";\n\n";
  414. // Emit the starting string.
  415. O << " auto MnemonicInfo = getMnemonic(MI);\n\n";
  416. O << " O << MnemonicInfo.first;\n\n";
  417. O << " uint" << ((BitsLeft < (OpcodeInfoBits - 32)) ? 64 : 32)
  418. << "_t Bits = MnemonicInfo.second;\n"
  419. << " assert(Bits != 0 && \"Cannot print this instruction.\");\n";
  420. // Output the table driven operand information.
  421. BitsLeft = OpcodeInfoBits-AsmStrBits;
  422. for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
  423. std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
  424. // Compute the number of bits we need to represent these cases, this is
  425. // ceil(log2(numentries)).
  426. unsigned NumBits = Log2_32_Ceil(Commands.size());
  427. assert(NumBits <= BitsLeft && "consistency error");
  428. // Emit code to extract this field from Bits.
  429. O << "\n // Fragment " << i << " encoded into " << NumBits
  430. << " bits for " << Commands.size() << " unique commands.\n";
  431. if (Commands.size() == 2) {
  432. // Emit two possibilitys with if/else.
  433. O << " if ((Bits >> "
  434. << (OpcodeInfoBits-BitsLeft) << ") & "
  435. << ((1 << NumBits)-1) << ") {\n"
  436. << Commands[1]
  437. << " } else {\n"
  438. << Commands[0]
  439. << " }\n\n";
  440. } else if (Commands.size() == 1) {
  441. // Emit a single possibility.
  442. O << Commands[0] << "\n\n";
  443. } else {
  444. O << " switch ((Bits >> "
  445. << (OpcodeInfoBits-BitsLeft) << ") & "
  446. << ((1 << NumBits)-1) << ") {\n"
  447. << " default: llvm_unreachable(\"Invalid command number.\");\n";
  448. // Print out all the cases.
  449. for (unsigned j = 0, e = Commands.size(); j != e; ++j) {
  450. O << " case " << j << ":\n";
  451. O << Commands[j];
  452. O << " break;\n";
  453. }
  454. O << " }\n\n";
  455. }
  456. BitsLeft -= NumBits;
  457. }
  458. // Okay, delete instructions with no operand info left.
  459. llvm::erase_if(Instructions,
  460. [](AsmWriterInst &Inst) { return Inst.Operands.empty(); });
  461. // Because this is a vector, we want to emit from the end. Reverse all of the
  462. // elements in the vector.
  463. std::reverse(Instructions.begin(), Instructions.end());
  464. // Now that we've emitted all of the operand info that fit into 64 bits, emit
  465. // information for those instructions that are left. This is a less dense
  466. // encoding, but we expect the main 64-bit table to handle the majority of
  467. // instructions.
  468. if (!Instructions.empty()) {
  469. // Find the opcode # of inline asm.
  470. O << " switch (MI->getOpcode()) {\n";
  471. O << " default: llvm_unreachable(\"Unexpected opcode.\");\n";
  472. while (!Instructions.empty())
  473. EmitInstructions(Instructions, O, PassSubtarget);
  474. O << " }\n";
  475. }
  476. O << "}\n";
  477. }
  478. static void
  479. emitRegisterNameString(raw_ostream &O, StringRef AltName,
  480. const std::deque<CodeGenRegister> &Registers) {
  481. SequenceToOffsetTable<std::string> StringTable;
  482. SmallVector<std::string, 4> AsmNames(Registers.size());
  483. unsigned i = 0;
  484. for (const auto &Reg : Registers) {
  485. std::string &AsmName = AsmNames[i++];
  486. // "NoRegAltName" is special. We don't need to do a lookup for that,
  487. // as it's just a reference to the default register name.
  488. if (AltName == "" || AltName == "NoRegAltName") {
  489. AsmName = std::string(Reg.TheDef->getValueAsString("AsmName"));
  490. if (AsmName.empty())
  491. AsmName = std::string(Reg.getName());
  492. } else {
  493. // Make sure the register has an alternate name for this index.
  494. std::vector<Record*> AltNameList =
  495. Reg.TheDef->getValueAsListOfDefs("RegAltNameIndices");
  496. unsigned Idx = 0, e;
  497. for (e = AltNameList.size();
  498. Idx < e && (AltNameList[Idx]->getName() != AltName);
  499. ++Idx)
  500. ;
  501. // If the register has an alternate name for this index, use it.
  502. // Otherwise, leave it empty as an error flag.
  503. if (Idx < e) {
  504. std::vector<StringRef> AltNames =
  505. Reg.TheDef->getValueAsListOfStrings("AltNames");
  506. if (AltNames.size() <= Idx)
  507. PrintFatalError(Reg.TheDef->getLoc(),
  508. "Register definition missing alt name for '" +
  509. AltName + "'.");
  510. AsmName = std::string(AltNames[Idx]);
  511. }
  512. }
  513. StringTable.add(AsmName);
  514. }
  515. StringTable.layout();
  516. StringTable.emitStringLiteralDef(O, Twine(" static const char AsmStrs") +
  517. AltName + "[]");
  518. O << " static const " << getMinimalTypeForRange(StringTable.size() - 1, 32)
  519. << " RegAsmOffset" << AltName << "[] = {";
  520. for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
  521. if ((i % 14) == 0)
  522. O << "\n ";
  523. O << StringTable.get(AsmNames[i]) << ", ";
  524. }
  525. O << "\n };\n"
  526. << "\n";
  527. }
  528. void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
  529. Record *AsmWriter = Target.getAsmWriter();
  530. StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
  531. const auto &Registers = Target.getRegBank().getRegisters();
  532. const std::vector<Record*> &AltNameIndices = Target.getRegAltNameIndices();
  533. bool hasAltNames = AltNameIndices.size() > 1;
  534. StringRef Namespace = Registers.front().TheDef->getValueAsString("Namespace");
  535. O <<
  536. "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
  537. "/// from the register set description. This returns the assembler name\n"
  538. "/// for the specified register.\n"
  539. "const char *" << Target.getName() << ClassName << "::";
  540. if (hasAltNames)
  541. O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx) {\n";
  542. else
  543. O << "getRegisterName(unsigned RegNo) {\n";
  544. O << " assert(RegNo && RegNo < " << (Registers.size()+1)
  545. << " && \"Invalid register number!\");\n"
  546. << "\n";
  547. if (hasAltNames) {
  548. for (const Record *R : AltNameIndices)
  549. emitRegisterNameString(O, R->getName(), Registers);
  550. } else
  551. emitRegisterNameString(O, "", Registers);
  552. if (hasAltNames) {
  553. O << " switch(AltIdx) {\n"
  554. << " default: llvm_unreachable(\"Invalid register alt name index!\");\n";
  555. for (const Record *R : AltNameIndices) {
  556. StringRef AltName = R->getName();
  557. O << " case ";
  558. if (!Namespace.empty())
  559. O << Namespace << "::";
  560. O << AltName << ":\n";
  561. if (R->isValueUnset("FallbackRegAltNameIndex"))
  562. O << " assert(*(AsmStrs" << AltName << "+RegAsmOffset" << AltName
  563. << "[RegNo-1]) &&\n"
  564. << " \"Invalid alt name index for register!\");\n";
  565. else {
  566. O << " if (!*(AsmStrs" << AltName << "+RegAsmOffset" << AltName
  567. << "[RegNo-1]))\n"
  568. << " return getRegisterName(RegNo, ";
  569. if (!Namespace.empty())
  570. O << Namespace << "::";
  571. O << R->getValueAsDef("FallbackRegAltNameIndex")->getName() << ");\n";
  572. }
  573. O << " return AsmStrs" << AltName << "+RegAsmOffset" << AltName
  574. << "[RegNo-1];\n";
  575. }
  576. O << " }\n";
  577. } else {
  578. O << " assert (*(AsmStrs+RegAsmOffset[RegNo-1]) &&\n"
  579. << " \"Invalid alt name index for register!\");\n"
  580. << " return AsmStrs+RegAsmOffset[RegNo-1];\n";
  581. }
  582. O << "}\n";
  583. }
  584. namespace {
  585. // IAPrinter - Holds information about an InstAlias. Two InstAliases match if
  586. // they both have the same conditionals. In which case, we cannot print out the
  587. // alias for that pattern.
  588. class IAPrinter {
  589. std::map<StringRef, std::pair<int, int>> OpMap;
  590. std::vector<std::string> Conds;
  591. std::string Result;
  592. std::string AsmString;
  593. unsigned NumMIOps;
  594. public:
  595. IAPrinter(std::string R, std::string AS, unsigned NumMIOps)
  596. : Result(std::move(R)), AsmString(std::move(AS)), NumMIOps(NumMIOps) {}
  597. void addCond(std::string C) { Conds.push_back(std::move(C)); }
  598. ArrayRef<std::string> getConds() const { return Conds; }
  599. size_t getCondCount() const { return Conds.size(); }
  600. void addOperand(StringRef Op, int OpIdx, int PrintMethodIdx = -1) {
  601. assert(OpIdx >= 0 && OpIdx < 0xFE && "Idx out of range");
  602. assert(PrintMethodIdx >= -1 && PrintMethodIdx < 0xFF &&
  603. "Idx out of range");
  604. OpMap[Op] = std::make_pair(OpIdx, PrintMethodIdx);
  605. }
  606. unsigned getNumMIOps() { return NumMIOps; }
  607. StringRef getResult() { return Result; }
  608. bool isOpMapped(StringRef Op) { return OpMap.find(Op) != OpMap.end(); }
  609. int getOpIndex(StringRef Op) { return OpMap[Op].first; }
  610. std::pair<int, int> &getOpData(StringRef Op) { return OpMap[Op]; }
  611. std::pair<StringRef, StringRef::iterator> parseName(StringRef::iterator Start,
  612. StringRef::iterator End) {
  613. StringRef::iterator I = Start;
  614. StringRef::iterator Next;
  615. if (*I == '{') {
  616. // ${some_name}
  617. Start = ++I;
  618. while (I != End && *I != '}')
  619. ++I;
  620. Next = I;
  621. // eat the final '}'
  622. if (Next != End)
  623. ++Next;
  624. } else {
  625. // $name, just eat the usual suspects.
  626. while (I != End && (isAlnum(*I) || *I == '_'))
  627. ++I;
  628. Next = I;
  629. }
  630. return std::make_pair(StringRef(Start, I - Start), Next);
  631. }
  632. std::string formatAliasString(uint32_t &UnescapedSize) {
  633. // Directly mangle mapped operands into the string. Each operand is
  634. // identified by a '$' sign followed by a byte identifying the number of the
  635. // operand. We add one to the index to avoid zero bytes.
  636. StringRef ASM(AsmString);
  637. std::string OutString;
  638. raw_string_ostream OS(OutString);
  639. for (StringRef::iterator I = ASM.begin(), E = ASM.end(); I != E;) {
  640. OS << *I;
  641. ++UnescapedSize;
  642. if (*I == '$') {
  643. StringRef Name;
  644. std::tie(Name, I) = parseName(++I, E);
  645. assert(isOpMapped(Name) && "Unmapped operand!");
  646. int OpIndex, PrintIndex;
  647. std::tie(OpIndex, PrintIndex) = getOpData(Name);
  648. if (PrintIndex == -1) {
  649. // Can use the default printOperand route.
  650. OS << format("\\x%02X", (unsigned char)OpIndex + 1);
  651. ++UnescapedSize;
  652. } else {
  653. // 3 bytes if a PrintMethod is needed: 0xFF, the MCInst operand
  654. // number, and which of our pre-detected Methods to call.
  655. OS << format("\\xFF\\x%02X\\x%02X", OpIndex + 1, PrintIndex + 1);
  656. UnescapedSize += 3;
  657. }
  658. } else {
  659. ++I;
  660. }
  661. }
  662. OS.flush();
  663. return OutString;
  664. }
  665. bool operator==(const IAPrinter &RHS) const {
  666. if (NumMIOps != RHS.NumMIOps)
  667. return false;
  668. if (Conds.size() != RHS.Conds.size())
  669. return false;
  670. unsigned Idx = 0;
  671. for (const auto &str : Conds)
  672. if (str != RHS.Conds[Idx++])
  673. return false;
  674. return true;
  675. }
  676. };
  677. } // end anonymous namespace
  678. static unsigned CountNumOperands(StringRef AsmString, unsigned Variant) {
  679. return AsmString.count(' ') + AsmString.count('\t');
  680. }
  681. namespace {
  682. struct AliasPriorityComparator {
  683. typedef std::pair<CodeGenInstAlias, int> ValueType;
  684. bool operator()(const ValueType &LHS, const ValueType &RHS) const {
  685. if (LHS.second == RHS.second) {
  686. // We don't actually care about the order, but for consistency it
  687. // shouldn't depend on pointer comparisons.
  688. return LessRecordByID()(LHS.first.TheDef, RHS.first.TheDef);
  689. }
  690. // Aliases with larger priorities should be considered first.
  691. return LHS.second > RHS.second;
  692. }
  693. };
  694. } // end anonymous namespace
  695. void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
  696. Record *AsmWriter = Target.getAsmWriter();
  697. O << "\n#ifdef PRINT_ALIAS_INSTR\n";
  698. O << "#undef PRINT_ALIAS_INSTR\n\n";
  699. //////////////////////////////
  700. // Gather information about aliases we need to print
  701. //////////////////////////////
  702. // Emit the method that prints the alias instruction.
  703. StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
  704. unsigned Variant = AsmWriter->getValueAsInt("Variant");
  705. bool PassSubtarget = AsmWriter->getValueAsInt("PassSubtarget");
  706. std::vector<Record*> AllInstAliases =
  707. Records.getAllDerivedDefinitions("InstAlias");
  708. // Create a map from the qualified name to a list of potential matches.
  709. typedef std::set<std::pair<CodeGenInstAlias, int>, AliasPriorityComparator>
  710. AliasWithPriority;
  711. std::map<std::string, AliasWithPriority> AliasMap;
  712. for (Record *R : AllInstAliases) {
  713. int Priority = R->getValueAsInt("EmitPriority");
  714. if (Priority < 1)
  715. continue; // Aliases with priority 0 are never emitted.
  716. const DagInit *DI = R->getValueAsDag("ResultInst");
  717. AliasMap[getQualifiedName(DI->getOperatorAsDef(R->getLoc()))].insert(
  718. std::make_pair(CodeGenInstAlias(R, Target), Priority));
  719. }
  720. // A map of which conditions need to be met for each instruction operand
  721. // before it can be matched to the mnemonic.
  722. std::map<std::string, std::vector<IAPrinter>> IAPrinterMap;
  723. std::vector<std::pair<std::string, bool>> PrintMethods;
  724. // A list of MCOperandPredicates for all operands in use, and the reverse map
  725. std::vector<const Record*> MCOpPredicates;
  726. DenseMap<const Record*, unsigned> MCOpPredicateMap;
  727. for (auto &Aliases : AliasMap) {
  728. // Collection of instruction alias rules. May contain ambiguous rules.
  729. std::vector<IAPrinter> IAPs;
  730. for (auto &Alias : Aliases.second) {
  731. const CodeGenInstAlias &CGA = Alias.first;
  732. unsigned LastOpNo = CGA.ResultInstOperandIndex.size();
  733. std::string FlatInstAsmString =
  734. CodeGenInstruction::FlattenAsmStringVariants(CGA.ResultInst->AsmString,
  735. Variant);
  736. unsigned NumResultOps = CountNumOperands(FlatInstAsmString, Variant);
  737. std::string FlatAliasAsmString =
  738. CodeGenInstruction::FlattenAsmStringVariants(CGA.AsmString, Variant);
  739. UnescapeAliasString(FlatAliasAsmString);
  740. // Don't emit the alias if it has more operands than what it's aliasing.
  741. if (NumResultOps < CountNumOperands(FlatAliasAsmString, Variant))
  742. continue;
  743. StringRef Namespace = Target.getName();
  744. unsigned NumMIOps = 0;
  745. for (auto &ResultInstOpnd : CGA.ResultInst->Operands)
  746. NumMIOps += ResultInstOpnd.MINumOperands;
  747. IAPrinter IAP(CGA.Result->getAsString(), FlatAliasAsmString, NumMIOps);
  748. bool CantHandle = false;
  749. unsigned MIOpNum = 0;
  750. for (unsigned i = 0, e = LastOpNo; i != e; ++i) {
  751. // Skip over tied operands as they're not part of an alias declaration.
  752. auto &Operands = CGA.ResultInst->Operands;
  753. while (true) {
  754. unsigned OpNum = Operands.getSubOperandNumber(MIOpNum).first;
  755. if (Operands[OpNum].MINumOperands == 1 &&
  756. Operands[OpNum].getTiedRegister() != -1) {
  757. // Tied operands of different RegisterClass should be explicit within
  758. // an instruction's syntax and so cannot be skipped.
  759. int TiedOpNum = Operands[OpNum].getTiedRegister();
  760. if (Operands[OpNum].Rec->getName() ==
  761. Operands[TiedOpNum].Rec->getName()) {
  762. ++MIOpNum;
  763. continue;
  764. }
  765. }
  766. break;
  767. }
  768. // Ignore unchecked result operands.
  769. while (IAP.getCondCount() < MIOpNum)
  770. IAP.addCond("AliasPatternCond::K_Ignore, 0");
  771. const CodeGenInstAlias::ResultOperand &RO = CGA.ResultOperands[i];
  772. switch (RO.Kind) {
  773. case CodeGenInstAlias::ResultOperand::K_Record: {
  774. const Record *Rec = RO.getRecord();
  775. StringRef ROName = RO.getName();
  776. int PrintMethodIdx = -1;
  777. // These two may have a PrintMethod, which we want to record (if it's
  778. // the first time we've seen it) and provide an index for the aliasing
  779. // code to use.
  780. if (Rec->isSubClassOf("RegisterOperand") ||
  781. Rec->isSubClassOf("Operand")) {
  782. StringRef PrintMethod = Rec->getValueAsString("PrintMethod");
  783. bool IsPCRel =
  784. Rec->getValueAsString("OperandType") == "OPERAND_PCREL";
  785. if (PrintMethod != "" && PrintMethod != "printOperand") {
  786. PrintMethodIdx = llvm::find_if(PrintMethods,
  787. [&](auto &X) {
  788. return X.first == PrintMethod;
  789. }) -
  790. PrintMethods.begin();
  791. if (static_cast<unsigned>(PrintMethodIdx) == PrintMethods.size())
  792. PrintMethods.emplace_back(std::string(PrintMethod), IsPCRel);
  793. }
  794. }
  795. if (Rec->isSubClassOf("RegisterOperand"))
  796. Rec = Rec->getValueAsDef("RegClass");
  797. if (Rec->isSubClassOf("RegisterClass")) {
  798. if (!IAP.isOpMapped(ROName)) {
  799. IAP.addOperand(ROName, MIOpNum, PrintMethodIdx);
  800. Record *R = CGA.ResultOperands[i].getRecord();
  801. if (R->isSubClassOf("RegisterOperand"))
  802. R = R->getValueAsDef("RegClass");
  803. IAP.addCond(std::string(
  804. formatv("AliasPatternCond::K_RegClass, {0}::{1}RegClassID",
  805. Namespace, R->getName())));
  806. } else {
  807. IAP.addCond(std::string(formatv(
  808. "AliasPatternCond::K_TiedReg, {0}", IAP.getOpIndex(ROName))));
  809. }
  810. } else {
  811. // Assume all printable operands are desired for now. This can be
  812. // overridden in the InstAlias instantiation if necessary.
  813. IAP.addOperand(ROName, MIOpNum, PrintMethodIdx);
  814. // There might be an additional predicate on the MCOperand
  815. unsigned Entry = MCOpPredicateMap[Rec];
  816. if (!Entry) {
  817. if (!Rec->isValueUnset("MCOperandPredicate")) {
  818. MCOpPredicates.push_back(Rec);
  819. Entry = MCOpPredicates.size();
  820. MCOpPredicateMap[Rec] = Entry;
  821. } else
  822. break; // No conditions on this operand at all
  823. }
  824. IAP.addCond(
  825. std::string(formatv("AliasPatternCond::K_Custom, {0}", Entry)));
  826. }
  827. break;
  828. }
  829. case CodeGenInstAlias::ResultOperand::K_Imm: {
  830. // Just because the alias has an immediate result, doesn't mean the
  831. // MCInst will. An MCExpr could be present, for example.
  832. auto Imm = CGA.ResultOperands[i].getImm();
  833. int32_t Imm32 = int32_t(Imm);
  834. if (Imm != Imm32)
  835. PrintFatalError("Matching an alias with an immediate out of the "
  836. "range of int32_t is not supported");
  837. IAP.addCond(std::string(
  838. formatv("AliasPatternCond::K_Imm, uint32_t({0})", Imm32)));
  839. break;
  840. }
  841. case CodeGenInstAlias::ResultOperand::K_Reg:
  842. // If this is zero_reg, something's playing tricks we're not
  843. // equipped to handle.
  844. if (!CGA.ResultOperands[i].getRegister()) {
  845. CantHandle = true;
  846. break;
  847. }
  848. StringRef Reg = CGA.ResultOperands[i].getRegister()->getName();
  849. IAP.addCond(std::string(
  850. formatv("AliasPatternCond::K_Reg, {0}::{1}", Namespace, Reg)));
  851. break;
  852. }
  853. MIOpNum += RO.getMINumOperands();
  854. }
  855. if (CantHandle) continue;
  856. std::vector<Record *> ReqFeatures;
  857. if (PassSubtarget) {
  858. // We only consider ReqFeatures predicates if PassSubtarget
  859. std::vector<Record *> RF =
  860. CGA.TheDef->getValueAsListOfDefs("Predicates");
  861. copy_if(RF, std::back_inserter(ReqFeatures), [](Record *R) {
  862. return R->getValueAsBit("AssemblerMatcherPredicate");
  863. });
  864. }
  865. for (auto I = ReqFeatures.cbegin(); I != ReqFeatures.cend(); I++) {
  866. Record *R = *I;
  867. const DagInit *D = R->getValueAsDag("AssemblerCondDag");
  868. std::string CombineType = D->getOperator()->getAsString();
  869. if (CombineType != "any_of" && CombineType != "all_of")
  870. PrintFatalError(R->getLoc(), "Invalid AssemblerCondDag!");
  871. if (D->getNumArgs() == 0)
  872. PrintFatalError(R->getLoc(), "Invalid AssemblerCondDag!");
  873. bool IsOr = CombineType == "any_of";
  874. for (auto *Arg : D->getArgs()) {
  875. bool IsNeg = false;
  876. if (auto *NotArg = dyn_cast<DagInit>(Arg)) {
  877. if (NotArg->getOperator()->getAsString() != "not" ||
  878. NotArg->getNumArgs() != 1)
  879. PrintFatalError(R->getLoc(), "Invalid AssemblerCondDag!");
  880. Arg = NotArg->getArg(0);
  881. IsNeg = true;
  882. }
  883. if (!isa<DefInit>(Arg) ||
  884. !cast<DefInit>(Arg)->getDef()->isSubClassOf("SubtargetFeature"))
  885. PrintFatalError(R->getLoc(), "Invalid AssemblerCondDag!");
  886. IAP.addCond(std::string(formatv(
  887. "AliasPatternCond::K_{0}{1}Feature, {2}::{3}", IsOr ? "Or" : "",
  888. IsNeg ? "Neg" : "", Namespace, Arg->getAsString())));
  889. }
  890. // If an AssemblerPredicate with ors is used, note end of list should
  891. // these be combined.
  892. if (IsOr)
  893. IAP.addCond("AliasPatternCond::K_EndOrFeatures, 0");
  894. }
  895. IAPrinterMap[Aliases.first].push_back(std::move(IAP));
  896. }
  897. }
  898. //////////////////////////////
  899. // Write out the printAliasInstr function
  900. //////////////////////////////
  901. std::string Header;
  902. raw_string_ostream HeaderO(Header);
  903. HeaderO << "bool " << Target.getName() << ClassName
  904. << "::printAliasInstr(const MCInst"
  905. << " *MI, uint64_t Address, "
  906. << (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
  907. << "raw_ostream &OS) {\n";
  908. std::string PatternsForOpcode;
  909. raw_string_ostream OpcodeO(PatternsForOpcode);
  910. unsigned PatternCount = 0;
  911. std::string Patterns;
  912. raw_string_ostream PatternO(Patterns);
  913. unsigned CondCount = 0;
  914. std::string Conds;
  915. raw_string_ostream CondO(Conds);
  916. // All flattened alias strings.
  917. std::map<std::string, uint32_t> AsmStringOffsets;
  918. std::vector<std::pair<uint32_t, std::string>> AsmStrings;
  919. size_t AsmStringsSize = 0;
  920. // Iterate over the opcodes in enum order so they are sorted by opcode for
  921. // binary search.
  922. for (const CodeGenInstruction *Inst : NumberedInstructions) {
  923. auto It = IAPrinterMap.find(getQualifiedName(Inst->TheDef));
  924. if (It == IAPrinterMap.end())
  925. continue;
  926. std::vector<IAPrinter> &IAPs = It->second;
  927. std::vector<IAPrinter*> UniqueIAPs;
  928. // Remove any ambiguous alias rules.
  929. for (auto &LHS : IAPs) {
  930. bool IsDup = false;
  931. for (const auto &RHS : IAPs) {
  932. if (&LHS != &RHS && LHS == RHS) {
  933. IsDup = true;
  934. break;
  935. }
  936. }
  937. if (!IsDup)
  938. UniqueIAPs.push_back(&LHS);
  939. }
  940. if (UniqueIAPs.empty()) continue;
  941. unsigned PatternStart = PatternCount;
  942. // Insert the pattern start and opcode in the pattern list for debugging.
  943. PatternO << formatv(" // {0} - {1}\n", It->first, PatternStart);
  944. for (IAPrinter *IAP : UniqueIAPs) {
  945. // Start each condition list with a comment of the resulting pattern that
  946. // we're trying to match.
  947. unsigned CondStart = CondCount;
  948. CondO << formatv(" // {0} - {1}\n", IAP->getResult(), CondStart);
  949. for (const auto &Cond : IAP->getConds())
  950. CondO << " {" << Cond << "},\n";
  951. CondCount += IAP->getCondCount();
  952. // After operands have been examined, re-encode the alias string with
  953. // escapes indicating how operands should be printed.
  954. uint32_t UnescapedSize = 0;
  955. std::string EncodedAsmString = IAP->formatAliasString(UnescapedSize);
  956. auto Insertion =
  957. AsmStringOffsets.insert({EncodedAsmString, AsmStringsSize});
  958. if (Insertion.second) {
  959. // If the string is new, add it to the vector.
  960. AsmStrings.push_back({AsmStringsSize, EncodedAsmString});
  961. AsmStringsSize += UnescapedSize + 1;
  962. }
  963. unsigned AsmStrOffset = Insertion.first->second;
  964. PatternO << formatv(" {{{0}, {1}, {2}, {3} },\n", AsmStrOffset,
  965. CondStart, IAP->getNumMIOps(), IAP->getCondCount());
  966. ++PatternCount;
  967. }
  968. OpcodeO << formatv(" {{{0}, {1}, {2} },\n", It->first, PatternStart,
  969. PatternCount - PatternStart);
  970. }
  971. if (OpcodeO.str().empty()) {
  972. O << HeaderO.str();
  973. O << " return false;\n";
  974. O << "}\n\n";
  975. O << "#endif // PRINT_ALIAS_INSTR\n";
  976. return;
  977. }
  978. // Forward declare the validation method if needed.
  979. if (!MCOpPredicates.empty())
  980. O << "static bool " << Target.getName() << ClassName
  981. << "ValidateMCOperand(const MCOperand &MCOp,\n"
  982. << " const MCSubtargetInfo &STI,\n"
  983. << " unsigned PredicateIndex);\n";
  984. O << HeaderO.str();
  985. O.indent(2) << "static const PatternsForOpcode OpToPatterns[] = {\n";
  986. O << OpcodeO.str();
  987. O.indent(2) << "};\n\n";
  988. O.indent(2) << "static const AliasPattern Patterns[] = {\n";
  989. O << PatternO.str();
  990. O.indent(2) << "};\n\n";
  991. O.indent(2) << "static const AliasPatternCond Conds[] = {\n";
  992. O << CondO.str();
  993. O.indent(2) << "};\n\n";
  994. O.indent(2) << "static const char AsmStrings[] =\n";
  995. for (const auto &P : AsmStrings) {
  996. O.indent(4) << "/* " << P.first << " */ \"" << P.second << "\\0\"\n";
  997. }
  998. O.indent(2) << ";\n\n";
  999. // Assert that the opcode table is sorted. Use a static local constructor to
  1000. // ensure that the check only happens once on first run.
  1001. O << "#ifndef NDEBUG\n";
  1002. O.indent(2) << "static struct SortCheck {\n";
  1003. O.indent(2) << " SortCheck(ArrayRef<PatternsForOpcode> OpToPatterns) {\n";
  1004. O.indent(2) << " assert(std::is_sorted(\n";
  1005. O.indent(2) << " OpToPatterns.begin(), OpToPatterns.end(),\n";
  1006. O.indent(2) << " [](const PatternsForOpcode &L, const "
  1007. "PatternsForOpcode &R) {\n";
  1008. O.indent(2) << " return L.Opcode < R.Opcode;\n";
  1009. O.indent(2) << " }) &&\n";
  1010. O.indent(2) << " \"tablegen failed to sort opcode patterns\");\n";
  1011. O.indent(2) << " }\n";
  1012. O.indent(2) << "} sortCheckVar(OpToPatterns);\n";
  1013. O << "#endif\n\n";
  1014. O.indent(2) << "AliasMatchingData M {\n";
  1015. O.indent(2) << " makeArrayRef(OpToPatterns),\n";
  1016. O.indent(2) << " makeArrayRef(Patterns),\n";
  1017. O.indent(2) << " makeArrayRef(Conds),\n";
  1018. O.indent(2) << " StringRef(AsmStrings, array_lengthof(AsmStrings)),\n";
  1019. if (MCOpPredicates.empty())
  1020. O.indent(2) << " nullptr,\n";
  1021. else
  1022. O.indent(2) << " &" << Target.getName() << ClassName << "ValidateMCOperand,\n";
  1023. O.indent(2) << "};\n";
  1024. O.indent(2) << "const char *AsmString = matchAliasPatterns(MI, "
  1025. << (PassSubtarget ? "&STI" : "nullptr") << ", M);\n";
  1026. O.indent(2) << "if (!AsmString) return false;\n\n";
  1027. // Code that prints the alias, replacing the operands with the ones from the
  1028. // MCInst.
  1029. O << " unsigned I = 0;\n";
  1030. O << " while (AsmString[I] != ' ' && AsmString[I] != '\\t' &&\n";
  1031. O << " AsmString[I] != '$' && AsmString[I] != '\\0')\n";
  1032. O << " ++I;\n";
  1033. O << " OS << '\\t' << StringRef(AsmString, I);\n";
  1034. O << " if (AsmString[I] != '\\0') {\n";
  1035. O << " if (AsmString[I] == ' ' || AsmString[I] == '\\t') {\n";
  1036. O << " OS << '\\t';\n";
  1037. O << " ++I;\n";
  1038. O << " }\n";
  1039. O << " do {\n";
  1040. O << " if (AsmString[I] == '$') {\n";
  1041. O << " ++I;\n";
  1042. O << " if (AsmString[I] == (char)0xff) {\n";
  1043. O << " ++I;\n";
  1044. O << " int OpIdx = AsmString[I++] - 1;\n";
  1045. O << " int PrintMethodIdx = AsmString[I++] - 1;\n";
  1046. O << " printCustomAliasOperand(MI, Address, OpIdx, PrintMethodIdx, ";
  1047. O << (PassSubtarget ? "STI, " : "");
  1048. O << "OS);\n";
  1049. O << " } else\n";
  1050. O << " printOperand(MI, unsigned(AsmString[I++]) - 1, ";
  1051. O << (PassSubtarget ? "STI, " : "");
  1052. O << "OS);\n";
  1053. O << " } else {\n";
  1054. O << " OS << AsmString[I++];\n";
  1055. O << " }\n";
  1056. O << " } while (AsmString[I] != '\\0');\n";
  1057. O << " }\n\n";
  1058. O << " return true;\n";
  1059. O << "}\n\n";
  1060. //////////////////////////////
  1061. // Write out the printCustomAliasOperand function
  1062. //////////////////////////////
  1063. O << "void " << Target.getName() << ClassName << "::"
  1064. << "printCustomAliasOperand(\n"
  1065. << " const MCInst *MI, uint64_t Address, unsigned OpIdx,\n"
  1066. << " unsigned PrintMethodIdx,\n"
  1067. << (PassSubtarget ? " const MCSubtargetInfo &STI,\n" : "")
  1068. << " raw_ostream &OS) {\n";
  1069. if (PrintMethods.empty())
  1070. O << " llvm_unreachable(\"Unknown PrintMethod kind\");\n";
  1071. else {
  1072. O << " switch (PrintMethodIdx) {\n"
  1073. << " default:\n"
  1074. << " llvm_unreachable(\"Unknown PrintMethod kind\");\n"
  1075. << " break;\n";
  1076. for (unsigned i = 0; i < PrintMethods.size(); ++i) {
  1077. O << " case " << i << ":\n"
  1078. << " " << PrintMethods[i].first << "(MI, "
  1079. << (PrintMethods[i].second ? "Address, " : "") << "OpIdx, "
  1080. << (PassSubtarget ? "STI, " : "") << "OS);\n"
  1081. << " break;\n";
  1082. }
  1083. O << " }\n";
  1084. }
  1085. O << "}\n\n";
  1086. if (!MCOpPredicates.empty()) {
  1087. O << "static bool " << Target.getName() << ClassName
  1088. << "ValidateMCOperand(const MCOperand &MCOp,\n"
  1089. << " const MCSubtargetInfo &STI,\n"
  1090. << " unsigned PredicateIndex) {\n"
  1091. << " switch (PredicateIndex) {\n"
  1092. << " default:\n"
  1093. << " llvm_unreachable(\"Unknown MCOperandPredicate kind\");\n"
  1094. << " break;\n";
  1095. for (unsigned i = 0; i < MCOpPredicates.size(); ++i) {
  1096. StringRef MCOpPred = MCOpPredicates[i]->getValueAsString("MCOperandPredicate");
  1097. O << " case " << i + 1 << ": {\n"
  1098. << MCOpPred.data() << "\n"
  1099. << " }\n";
  1100. }
  1101. O << " }\n"
  1102. << "}\n\n";
  1103. }
  1104. O << "#endif // PRINT_ALIAS_INSTR\n";
  1105. }
  1106. AsmWriterEmitter::AsmWriterEmitter(RecordKeeper &R) : Records(R), Target(R) {
  1107. Record *AsmWriter = Target.getAsmWriter();
  1108. unsigned Variant = AsmWriter->getValueAsInt("Variant");
  1109. // Get the instruction numbering.
  1110. NumberedInstructions = Target.getInstructionsByEnumValue();
  1111. for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
  1112. const CodeGenInstruction *I = NumberedInstructions[i];
  1113. if (!I->AsmString.empty() && I->TheDef->getName() != "PHI")
  1114. Instructions.emplace_back(*I, i, Variant);
  1115. }
  1116. }
  1117. void AsmWriterEmitter::run(raw_ostream &O) {
  1118. std::vector<std::vector<std::string>> TableDrivenOperandPrinters;
  1119. unsigned BitsLeft = 0;
  1120. unsigned AsmStrBits = 0;
  1121. EmitGetMnemonic(O, TableDrivenOperandPrinters, BitsLeft, AsmStrBits);
  1122. EmitPrintInstruction(O, TableDrivenOperandPrinters, BitsLeft, AsmStrBits);
  1123. EmitGetRegisterName(O);
  1124. EmitPrintAliasInstruction(O);
  1125. }
  1126. namespace llvm {
  1127. void EmitAsmWriter(RecordKeeper &RK, raw_ostream &OS) {
  1128. emitSourceFileHeader("Assembly Writer Source Fragment", OS);
  1129. AsmWriterEmitter(RK).run(OS);
  1130. }
  1131. } // end namespace llvm