AsmWriterEmitter.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  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. // This function has some huge switch statements that causing excessive
  405. // compile time in LLVM profile instrumenation build. This print function
  406. // usually is not frequently called in compilation. Here we disable the
  407. // profile instrumenation for this function.
  408. O << "/// printInstruction - This method is automatically generated by "
  409. "tablegen\n"
  410. "/// from the instruction set description.\n"
  411. "LLVM_NO_PROFILE_INSTRUMENT_FUNCTION\n"
  412. "void "
  413. << Target.getName() << ClassName
  414. << "::printInstruction(const MCInst *MI, uint64_t Address, "
  415. << (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
  416. << "raw_ostream &O) {\n";
  417. // Emit the initial tab character.
  418. O << " O << \"\\t\";\n\n";
  419. // Emit the starting string.
  420. O << " auto MnemonicInfo = getMnemonic(MI);\n\n";
  421. O << " O << MnemonicInfo.first;\n\n";
  422. O << " uint" << ((BitsLeft < (OpcodeInfoBits - 32)) ? 64 : 32)
  423. << "_t Bits = MnemonicInfo.second;\n"
  424. << " assert(Bits != 0 && \"Cannot print this instruction.\");\n";
  425. // Output the table driven operand information.
  426. BitsLeft = OpcodeInfoBits-AsmStrBits;
  427. for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
  428. std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
  429. // Compute the number of bits we need to represent these cases, this is
  430. // ceil(log2(numentries)).
  431. unsigned NumBits = Log2_32_Ceil(Commands.size());
  432. assert(NumBits <= BitsLeft && "consistency error");
  433. // Emit code to extract this field from Bits.
  434. O << "\n // Fragment " << i << " encoded into " << NumBits
  435. << " bits for " << Commands.size() << " unique commands.\n";
  436. if (Commands.size() == 2) {
  437. // Emit two possibilitys with if/else.
  438. O << " if ((Bits >> "
  439. << (OpcodeInfoBits-BitsLeft) << ") & "
  440. << ((1 << NumBits)-1) << ") {\n"
  441. << Commands[1]
  442. << " } else {\n"
  443. << Commands[0]
  444. << " }\n\n";
  445. } else if (Commands.size() == 1) {
  446. // Emit a single possibility.
  447. O << Commands[0] << "\n\n";
  448. } else {
  449. O << " switch ((Bits >> "
  450. << (OpcodeInfoBits-BitsLeft) << ") & "
  451. << ((1 << NumBits)-1) << ") {\n"
  452. << " default: llvm_unreachable(\"Invalid command number.\");\n";
  453. // Print out all the cases.
  454. for (unsigned j = 0, e = Commands.size(); j != e; ++j) {
  455. O << " case " << j << ":\n";
  456. O << Commands[j];
  457. O << " break;\n";
  458. }
  459. O << " }\n\n";
  460. }
  461. BitsLeft -= NumBits;
  462. }
  463. // Okay, delete instructions with no operand info left.
  464. llvm::erase_if(Instructions,
  465. [](AsmWriterInst &Inst) { return Inst.Operands.empty(); });
  466. // Because this is a vector, we want to emit from the end. Reverse all of the
  467. // elements in the vector.
  468. std::reverse(Instructions.begin(), Instructions.end());
  469. // Now that we've emitted all of the operand info that fit into 64 bits, emit
  470. // information for those instructions that are left. This is a less dense
  471. // encoding, but we expect the main 64-bit table to handle the majority of
  472. // instructions.
  473. if (!Instructions.empty()) {
  474. // Find the opcode # of inline asm.
  475. O << " switch (MI->getOpcode()) {\n";
  476. O << " default: llvm_unreachable(\"Unexpected opcode.\");\n";
  477. while (!Instructions.empty())
  478. EmitInstructions(Instructions, O, PassSubtarget);
  479. O << " }\n";
  480. }
  481. O << "}\n";
  482. }
  483. static void
  484. emitRegisterNameString(raw_ostream &O, StringRef AltName,
  485. const std::deque<CodeGenRegister> &Registers) {
  486. SequenceToOffsetTable<std::string> StringTable;
  487. SmallVector<std::string, 4> AsmNames(Registers.size());
  488. unsigned i = 0;
  489. for (const auto &Reg : Registers) {
  490. std::string &AsmName = AsmNames[i++];
  491. // "NoRegAltName" is special. We don't need to do a lookup for that,
  492. // as it's just a reference to the default register name.
  493. if (AltName == "" || AltName == "NoRegAltName") {
  494. AsmName = std::string(Reg.TheDef->getValueAsString("AsmName"));
  495. if (AsmName.empty())
  496. AsmName = std::string(Reg.getName());
  497. } else {
  498. // Make sure the register has an alternate name for this index.
  499. std::vector<Record*> AltNameList =
  500. Reg.TheDef->getValueAsListOfDefs("RegAltNameIndices");
  501. unsigned Idx = 0, e;
  502. for (e = AltNameList.size();
  503. Idx < e && (AltNameList[Idx]->getName() != AltName);
  504. ++Idx)
  505. ;
  506. // If the register has an alternate name for this index, use it.
  507. // Otherwise, leave it empty as an error flag.
  508. if (Idx < e) {
  509. std::vector<StringRef> AltNames =
  510. Reg.TheDef->getValueAsListOfStrings("AltNames");
  511. if (AltNames.size() <= Idx)
  512. PrintFatalError(Reg.TheDef->getLoc(),
  513. "Register definition missing alt name for '" +
  514. AltName + "'.");
  515. AsmName = std::string(AltNames[Idx]);
  516. }
  517. }
  518. StringTable.add(AsmName);
  519. }
  520. StringTable.layout();
  521. StringTable.emitStringLiteralDef(O, Twine(" static const char AsmStrs") +
  522. AltName + "[]");
  523. O << " static const " << getMinimalTypeForRange(StringTable.size() - 1, 32)
  524. << " RegAsmOffset" << AltName << "[] = {";
  525. for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
  526. if ((i % 14) == 0)
  527. O << "\n ";
  528. O << StringTable.get(AsmNames[i]) << ", ";
  529. }
  530. O << "\n };\n"
  531. << "\n";
  532. }
  533. void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
  534. Record *AsmWriter = Target.getAsmWriter();
  535. StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
  536. const auto &Registers = Target.getRegBank().getRegisters();
  537. const std::vector<Record*> &AltNameIndices = Target.getRegAltNameIndices();
  538. bool hasAltNames = AltNameIndices.size() > 1;
  539. StringRef Namespace = Registers.front().TheDef->getValueAsString("Namespace");
  540. O <<
  541. "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
  542. "/// from the register set description. This returns the assembler name\n"
  543. "/// for the specified register.\n"
  544. "const char *" << Target.getName() << ClassName << "::";
  545. if (hasAltNames)
  546. O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx) {\n";
  547. else
  548. O << "getRegisterName(unsigned RegNo) {\n";
  549. O << " assert(RegNo && RegNo < " << (Registers.size()+1)
  550. << " && \"Invalid register number!\");\n"
  551. << "\n";
  552. if (hasAltNames) {
  553. for (const Record *R : AltNameIndices)
  554. emitRegisterNameString(O, R->getName(), Registers);
  555. } else
  556. emitRegisterNameString(O, "", Registers);
  557. if (hasAltNames) {
  558. O << " switch(AltIdx) {\n"
  559. << " default: llvm_unreachable(\"Invalid register alt name index!\");\n";
  560. for (const Record *R : AltNameIndices) {
  561. StringRef AltName = R->getName();
  562. O << " case ";
  563. if (!Namespace.empty())
  564. O << Namespace << "::";
  565. O << AltName << ":\n";
  566. if (R->isValueUnset("FallbackRegAltNameIndex"))
  567. O << " assert(*(AsmStrs" << AltName << "+RegAsmOffset" << AltName
  568. << "[RegNo-1]) &&\n"
  569. << " \"Invalid alt name index for register!\");\n";
  570. else {
  571. O << " if (!*(AsmStrs" << AltName << "+RegAsmOffset" << AltName
  572. << "[RegNo-1]))\n"
  573. << " return getRegisterName(RegNo, ";
  574. if (!Namespace.empty())
  575. O << Namespace << "::";
  576. O << R->getValueAsDef("FallbackRegAltNameIndex")->getName() << ");\n";
  577. }
  578. O << " return AsmStrs" << AltName << "+RegAsmOffset" << AltName
  579. << "[RegNo-1];\n";
  580. }
  581. O << " }\n";
  582. } else {
  583. O << " assert (*(AsmStrs+RegAsmOffset[RegNo-1]) &&\n"
  584. << " \"Invalid alt name index for register!\");\n"
  585. << " return AsmStrs+RegAsmOffset[RegNo-1];\n";
  586. }
  587. O << "}\n";
  588. }
  589. namespace {
  590. // IAPrinter - Holds information about an InstAlias. Two InstAliases match if
  591. // they both have the same conditionals. In which case, we cannot print out the
  592. // alias for that pattern.
  593. class IAPrinter {
  594. std::map<StringRef, std::pair<int, int>> OpMap;
  595. std::vector<std::string> Conds;
  596. std::string Result;
  597. std::string AsmString;
  598. unsigned NumMIOps;
  599. public:
  600. IAPrinter(std::string R, std::string AS, unsigned NumMIOps)
  601. : Result(std::move(R)), AsmString(std::move(AS)), NumMIOps(NumMIOps) {}
  602. void addCond(std::string C) { Conds.push_back(std::move(C)); }
  603. ArrayRef<std::string> getConds() const { return Conds; }
  604. size_t getCondCount() const { return Conds.size(); }
  605. void addOperand(StringRef Op, int OpIdx, int PrintMethodIdx = -1) {
  606. assert(OpIdx >= 0 && OpIdx < 0xFE && "Idx out of range");
  607. assert(PrintMethodIdx >= -1 && PrintMethodIdx < 0xFF &&
  608. "Idx out of range");
  609. OpMap[Op] = std::make_pair(OpIdx, PrintMethodIdx);
  610. }
  611. unsigned getNumMIOps() { return NumMIOps; }
  612. StringRef getResult() { return Result; }
  613. bool isOpMapped(StringRef Op) { return OpMap.find(Op) != OpMap.end(); }
  614. int getOpIndex(StringRef Op) { return OpMap[Op].first; }
  615. std::pair<int, int> &getOpData(StringRef Op) { return OpMap[Op]; }
  616. std::pair<StringRef, StringRef::iterator> parseName(StringRef::iterator Start,
  617. StringRef::iterator End) {
  618. StringRef::iterator I = Start;
  619. StringRef::iterator Next;
  620. if (*I == '{') {
  621. // ${some_name}
  622. Start = ++I;
  623. while (I != End && *I != '}')
  624. ++I;
  625. Next = I;
  626. // eat the final '}'
  627. if (Next != End)
  628. ++Next;
  629. } else {
  630. // $name, just eat the usual suspects.
  631. while (I != End && (isAlnum(*I) || *I == '_'))
  632. ++I;
  633. Next = I;
  634. }
  635. return std::make_pair(StringRef(Start, I - Start), Next);
  636. }
  637. std::string formatAliasString(uint32_t &UnescapedSize) {
  638. // Directly mangle mapped operands into the string. Each operand is
  639. // identified by a '$' sign followed by a byte identifying the number of the
  640. // operand. We add one to the index to avoid zero bytes.
  641. StringRef ASM(AsmString);
  642. std::string OutString;
  643. raw_string_ostream OS(OutString);
  644. for (StringRef::iterator I = ASM.begin(), E = ASM.end(); I != E;) {
  645. OS << *I;
  646. ++UnescapedSize;
  647. if (*I == '$') {
  648. StringRef Name;
  649. std::tie(Name, I) = parseName(++I, E);
  650. assert(isOpMapped(Name) && "Unmapped operand!");
  651. int OpIndex, PrintIndex;
  652. std::tie(OpIndex, PrintIndex) = getOpData(Name);
  653. if (PrintIndex == -1) {
  654. // Can use the default printOperand route.
  655. OS << format("\\x%02X", (unsigned char)OpIndex + 1);
  656. ++UnescapedSize;
  657. } else {
  658. // 3 bytes if a PrintMethod is needed: 0xFF, the MCInst operand
  659. // number, and which of our pre-detected Methods to call.
  660. OS << format("\\xFF\\x%02X\\x%02X", OpIndex + 1, PrintIndex + 1);
  661. UnescapedSize += 3;
  662. }
  663. } else {
  664. ++I;
  665. }
  666. }
  667. return OutString;
  668. }
  669. bool operator==(const IAPrinter &RHS) const {
  670. if (NumMIOps != RHS.NumMIOps)
  671. return false;
  672. if (Conds.size() != RHS.Conds.size())
  673. return false;
  674. unsigned Idx = 0;
  675. for (const auto &str : Conds)
  676. if (str != RHS.Conds[Idx++])
  677. return false;
  678. return true;
  679. }
  680. };
  681. } // end anonymous namespace
  682. static unsigned CountNumOperands(StringRef AsmString, unsigned Variant) {
  683. return AsmString.count(' ') + AsmString.count('\t');
  684. }
  685. namespace {
  686. struct AliasPriorityComparator {
  687. typedef std::pair<CodeGenInstAlias, int> ValueType;
  688. bool operator()(const ValueType &LHS, const ValueType &RHS) const {
  689. if (LHS.second == RHS.second) {
  690. // We don't actually care about the order, but for consistency it
  691. // shouldn't depend on pointer comparisons.
  692. return LessRecordByID()(LHS.first.TheDef, RHS.first.TheDef);
  693. }
  694. // Aliases with larger priorities should be considered first.
  695. return LHS.second > RHS.second;
  696. }
  697. };
  698. } // end anonymous namespace
  699. void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
  700. Record *AsmWriter = Target.getAsmWriter();
  701. O << "\n#ifdef PRINT_ALIAS_INSTR\n";
  702. O << "#undef PRINT_ALIAS_INSTR\n\n";
  703. //////////////////////////////
  704. // Gather information about aliases we need to print
  705. //////////////////////////////
  706. // Emit the method that prints the alias instruction.
  707. StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
  708. unsigned Variant = AsmWriter->getValueAsInt("Variant");
  709. bool PassSubtarget = AsmWriter->getValueAsInt("PassSubtarget");
  710. std::vector<Record*> AllInstAliases =
  711. Records.getAllDerivedDefinitions("InstAlias");
  712. // Create a map from the qualified name to a list of potential matches.
  713. typedef std::set<std::pair<CodeGenInstAlias, int>, AliasPriorityComparator>
  714. AliasWithPriority;
  715. std::map<std::string, AliasWithPriority> AliasMap;
  716. for (Record *R : AllInstAliases) {
  717. int Priority = R->getValueAsInt("EmitPriority");
  718. if (Priority < 1)
  719. continue; // Aliases with priority 0 are never emitted.
  720. const DagInit *DI = R->getValueAsDag("ResultInst");
  721. AliasMap[getQualifiedName(DI->getOperatorAsDef(R->getLoc()))].insert(
  722. std::make_pair(CodeGenInstAlias(R, Target), Priority));
  723. }
  724. // A map of which conditions need to be met for each instruction operand
  725. // before it can be matched to the mnemonic.
  726. std::map<std::string, std::vector<IAPrinter>> IAPrinterMap;
  727. std::vector<std::pair<std::string, bool>> PrintMethods;
  728. // A list of MCOperandPredicates for all operands in use, and the reverse map
  729. std::vector<const Record*> MCOpPredicates;
  730. DenseMap<const Record*, unsigned> MCOpPredicateMap;
  731. for (auto &Aliases : AliasMap) {
  732. // Collection of instruction alias rules. May contain ambiguous rules.
  733. std::vector<IAPrinter> IAPs;
  734. for (auto &Alias : Aliases.second) {
  735. const CodeGenInstAlias &CGA = Alias.first;
  736. unsigned LastOpNo = CGA.ResultInstOperandIndex.size();
  737. std::string FlatInstAsmString =
  738. CodeGenInstruction::FlattenAsmStringVariants(CGA.ResultInst->AsmString,
  739. Variant);
  740. unsigned NumResultOps = CountNumOperands(FlatInstAsmString, Variant);
  741. std::string FlatAliasAsmString =
  742. CodeGenInstruction::FlattenAsmStringVariants(CGA.AsmString, Variant);
  743. UnescapeAliasString(FlatAliasAsmString);
  744. // Don't emit the alias if it has more operands than what it's aliasing.
  745. if (NumResultOps < CountNumOperands(FlatAliasAsmString, Variant))
  746. continue;
  747. StringRef Namespace = Target.getName();
  748. unsigned NumMIOps = 0;
  749. for (auto &ResultInstOpnd : CGA.ResultInst->Operands)
  750. NumMIOps += ResultInstOpnd.MINumOperands;
  751. IAPrinter IAP(CGA.Result->getAsString(), FlatAliasAsmString, NumMIOps);
  752. bool CantHandle = false;
  753. unsigned MIOpNum = 0;
  754. for (unsigned i = 0, e = LastOpNo; i != e; ++i) {
  755. // Skip over tied operands as they're not part of an alias declaration.
  756. auto &Operands = CGA.ResultInst->Operands;
  757. while (true) {
  758. unsigned OpNum = Operands.getSubOperandNumber(MIOpNum).first;
  759. if (Operands[OpNum].MINumOperands == 1 &&
  760. Operands[OpNum].getTiedRegister() != -1) {
  761. // Tied operands of different RegisterClass should be explicit within
  762. // an instruction's syntax and so cannot be skipped.
  763. int TiedOpNum = Operands[OpNum].getTiedRegister();
  764. if (Operands[OpNum].Rec->getName() ==
  765. Operands[TiedOpNum].Rec->getName()) {
  766. ++MIOpNum;
  767. continue;
  768. }
  769. }
  770. break;
  771. }
  772. // Ignore unchecked result operands.
  773. while (IAP.getCondCount() < MIOpNum)
  774. IAP.addCond("AliasPatternCond::K_Ignore, 0");
  775. const CodeGenInstAlias::ResultOperand &RO = CGA.ResultOperands[i];
  776. switch (RO.Kind) {
  777. case CodeGenInstAlias::ResultOperand::K_Record: {
  778. const Record *Rec = RO.getRecord();
  779. StringRef ROName = RO.getName();
  780. int PrintMethodIdx = -1;
  781. // These two may have a PrintMethod, which we want to record (if it's
  782. // the first time we've seen it) and provide an index for the aliasing
  783. // code to use.
  784. if (Rec->isSubClassOf("RegisterOperand") ||
  785. Rec->isSubClassOf("Operand")) {
  786. StringRef PrintMethod = Rec->getValueAsString("PrintMethod");
  787. bool IsPCRel =
  788. Rec->getValueAsString("OperandType") == "OPERAND_PCREL";
  789. if (PrintMethod != "" && PrintMethod != "printOperand") {
  790. PrintMethodIdx = llvm::find_if(PrintMethods,
  791. [&](auto &X) {
  792. return X.first == PrintMethod;
  793. }) -
  794. PrintMethods.begin();
  795. if (static_cast<unsigned>(PrintMethodIdx) == PrintMethods.size())
  796. PrintMethods.emplace_back(std::string(PrintMethod), IsPCRel);
  797. }
  798. }
  799. if (Rec->isSubClassOf("RegisterOperand"))
  800. Rec = Rec->getValueAsDef("RegClass");
  801. if (Rec->isSubClassOf("RegisterClass")) {
  802. if (!IAP.isOpMapped(ROName)) {
  803. IAP.addOperand(ROName, MIOpNum, PrintMethodIdx);
  804. Record *R = CGA.ResultOperands[i].getRecord();
  805. if (R->isSubClassOf("RegisterOperand"))
  806. R = R->getValueAsDef("RegClass");
  807. IAP.addCond(std::string(
  808. formatv("AliasPatternCond::K_RegClass, {0}::{1}RegClassID",
  809. Namespace, R->getName())));
  810. } else {
  811. IAP.addCond(std::string(formatv(
  812. "AliasPatternCond::K_TiedReg, {0}", IAP.getOpIndex(ROName))));
  813. }
  814. } else {
  815. // Assume all printable operands are desired for now. This can be
  816. // overridden in the InstAlias instantiation if necessary.
  817. IAP.addOperand(ROName, MIOpNum, PrintMethodIdx);
  818. // There might be an additional predicate on the MCOperand
  819. unsigned Entry = MCOpPredicateMap[Rec];
  820. if (!Entry) {
  821. if (!Rec->isValueUnset("MCOperandPredicate")) {
  822. MCOpPredicates.push_back(Rec);
  823. Entry = MCOpPredicates.size();
  824. MCOpPredicateMap[Rec] = Entry;
  825. } else
  826. break; // No conditions on this operand at all
  827. }
  828. IAP.addCond(
  829. std::string(formatv("AliasPatternCond::K_Custom, {0}", Entry)));
  830. }
  831. break;
  832. }
  833. case CodeGenInstAlias::ResultOperand::K_Imm: {
  834. // Just because the alias has an immediate result, doesn't mean the
  835. // MCInst will. An MCExpr could be present, for example.
  836. auto Imm = CGA.ResultOperands[i].getImm();
  837. int32_t Imm32 = int32_t(Imm);
  838. if (Imm != Imm32)
  839. PrintFatalError("Matching an alias with an immediate out of the "
  840. "range of int32_t is not supported");
  841. IAP.addCond(std::string(
  842. formatv("AliasPatternCond::K_Imm, uint32_t({0})", Imm32)));
  843. break;
  844. }
  845. case CodeGenInstAlias::ResultOperand::K_Reg:
  846. // If this is zero_reg, something's playing tricks we're not
  847. // equipped to handle.
  848. if (!CGA.ResultOperands[i].getRegister()) {
  849. CantHandle = true;
  850. break;
  851. }
  852. StringRef Reg = CGA.ResultOperands[i].getRegister()->getName();
  853. IAP.addCond(std::string(
  854. formatv("AliasPatternCond::K_Reg, {0}::{1}", Namespace, Reg)));
  855. break;
  856. }
  857. MIOpNum += RO.getMINumOperands();
  858. }
  859. if (CantHandle) continue;
  860. std::vector<Record *> ReqFeatures;
  861. if (PassSubtarget) {
  862. // We only consider ReqFeatures predicates if PassSubtarget
  863. std::vector<Record *> RF =
  864. CGA.TheDef->getValueAsListOfDefs("Predicates");
  865. copy_if(RF, std::back_inserter(ReqFeatures), [](Record *R) {
  866. return R->getValueAsBit("AssemblerMatcherPredicate");
  867. });
  868. }
  869. for (Record *const R : ReqFeatures) {
  870. const DagInit *D = R->getValueAsDag("AssemblerCondDag");
  871. std::string CombineType = D->getOperator()->getAsString();
  872. if (CombineType != "any_of" && CombineType != "all_of")
  873. PrintFatalError(R->getLoc(), "Invalid AssemblerCondDag!");
  874. if (D->getNumArgs() == 0)
  875. PrintFatalError(R->getLoc(), "Invalid AssemblerCondDag!");
  876. bool IsOr = CombineType == "any_of";
  877. for (auto *Arg : D->getArgs()) {
  878. bool IsNeg = false;
  879. if (auto *NotArg = dyn_cast<DagInit>(Arg)) {
  880. if (NotArg->getOperator()->getAsString() != "not" ||
  881. NotArg->getNumArgs() != 1)
  882. PrintFatalError(R->getLoc(), "Invalid AssemblerCondDag!");
  883. Arg = NotArg->getArg(0);
  884. IsNeg = true;
  885. }
  886. if (!isa<DefInit>(Arg) ||
  887. !cast<DefInit>(Arg)->getDef()->isSubClassOf("SubtargetFeature"))
  888. PrintFatalError(R->getLoc(), "Invalid AssemblerCondDag!");
  889. IAP.addCond(std::string(formatv(
  890. "AliasPatternCond::K_{0}{1}Feature, {2}::{3}", IsOr ? "Or" : "",
  891. IsNeg ? "Neg" : "", Namespace, Arg->getAsString())));
  892. }
  893. // If an AssemblerPredicate with ors is used, note end of list should
  894. // these be combined.
  895. if (IsOr)
  896. IAP.addCond("AliasPatternCond::K_EndOrFeatures, 0");
  897. }
  898. IAPrinterMap[Aliases.first].push_back(std::move(IAP));
  899. }
  900. }
  901. //////////////////////////////
  902. // Write out the printAliasInstr function
  903. //////////////////////////////
  904. std::string Header;
  905. raw_string_ostream HeaderO(Header);
  906. HeaderO << "bool " << Target.getName() << ClassName
  907. << "::printAliasInstr(const MCInst"
  908. << " *MI, uint64_t Address, "
  909. << (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
  910. << "raw_ostream &OS) {\n";
  911. std::string PatternsForOpcode;
  912. raw_string_ostream OpcodeO(PatternsForOpcode);
  913. unsigned PatternCount = 0;
  914. std::string Patterns;
  915. raw_string_ostream PatternO(Patterns);
  916. unsigned CondCount = 0;
  917. std::string Conds;
  918. raw_string_ostream CondO(Conds);
  919. // All flattened alias strings.
  920. std::map<std::string, uint32_t> AsmStringOffsets;
  921. std::vector<std::pair<uint32_t, std::string>> AsmStrings;
  922. size_t AsmStringsSize = 0;
  923. // Iterate over the opcodes in enum order so they are sorted by opcode for
  924. // binary search.
  925. for (const CodeGenInstruction *Inst : NumberedInstructions) {
  926. auto It = IAPrinterMap.find(getQualifiedName(Inst->TheDef));
  927. if (It == IAPrinterMap.end())
  928. continue;
  929. std::vector<IAPrinter> &IAPs = It->second;
  930. std::vector<IAPrinter*> UniqueIAPs;
  931. // Remove any ambiguous alias rules.
  932. for (auto &LHS : IAPs) {
  933. bool IsDup = false;
  934. for (const auto &RHS : IAPs) {
  935. if (&LHS != &RHS && LHS == RHS) {
  936. IsDup = true;
  937. break;
  938. }
  939. }
  940. if (!IsDup)
  941. UniqueIAPs.push_back(&LHS);
  942. }
  943. if (UniqueIAPs.empty()) continue;
  944. unsigned PatternStart = PatternCount;
  945. // Insert the pattern start and opcode in the pattern list for debugging.
  946. PatternO << formatv(" // {0} - {1}\n", It->first, PatternStart);
  947. for (IAPrinter *IAP : UniqueIAPs) {
  948. // Start each condition list with a comment of the resulting pattern that
  949. // we're trying to match.
  950. unsigned CondStart = CondCount;
  951. CondO << formatv(" // {0} - {1}\n", IAP->getResult(), CondStart);
  952. for (const auto &Cond : IAP->getConds())
  953. CondO << " {" << Cond << "},\n";
  954. CondCount += IAP->getCondCount();
  955. // After operands have been examined, re-encode the alias string with
  956. // escapes indicating how operands should be printed.
  957. uint32_t UnescapedSize = 0;
  958. std::string EncodedAsmString = IAP->formatAliasString(UnescapedSize);
  959. auto Insertion =
  960. AsmStringOffsets.insert({EncodedAsmString, AsmStringsSize});
  961. if (Insertion.second) {
  962. // If the string is new, add it to the vector.
  963. AsmStrings.push_back({AsmStringsSize, EncodedAsmString});
  964. AsmStringsSize += UnescapedSize + 1;
  965. }
  966. unsigned AsmStrOffset = Insertion.first->second;
  967. PatternO << formatv(" {{{0}, {1}, {2}, {3} },\n", AsmStrOffset,
  968. CondStart, IAP->getNumMIOps(), IAP->getCondCount());
  969. ++PatternCount;
  970. }
  971. OpcodeO << formatv(" {{{0}, {1}, {2} },\n", It->first, PatternStart,
  972. PatternCount - PatternStart);
  973. }
  974. if (OpcodeO.str().empty()) {
  975. O << HeaderO.str();
  976. O << " return false;\n";
  977. O << "}\n\n";
  978. O << "#endif // PRINT_ALIAS_INSTR\n";
  979. return;
  980. }
  981. // Forward declare the validation method if needed.
  982. if (!MCOpPredicates.empty())
  983. O << "static bool " << Target.getName() << ClassName
  984. << "ValidateMCOperand(const MCOperand &MCOp,\n"
  985. << " const MCSubtargetInfo &STI,\n"
  986. << " unsigned PredicateIndex);\n";
  987. O << HeaderO.str();
  988. O.indent(2) << "static const PatternsForOpcode OpToPatterns[] = {\n";
  989. O << OpcodeO.str();
  990. O.indent(2) << "};\n\n";
  991. O.indent(2) << "static const AliasPattern Patterns[] = {\n";
  992. O << PatternO.str();
  993. O.indent(2) << "};\n\n";
  994. O.indent(2) << "static const AliasPatternCond Conds[] = {\n";
  995. O << CondO.str();
  996. O.indent(2) << "};\n\n";
  997. O.indent(2) << "static const char AsmStrings[] =\n";
  998. for (const auto &P : AsmStrings) {
  999. O.indent(4) << "/* " << P.first << " */ \"" << P.second << "\\0\"\n";
  1000. }
  1001. O.indent(2) << ";\n\n";
  1002. // Assert that the opcode table is sorted. Use a static local constructor to
  1003. // ensure that the check only happens once on first run.
  1004. O << "#ifndef NDEBUG\n";
  1005. O.indent(2) << "static struct SortCheck {\n";
  1006. O.indent(2) << " SortCheck(ArrayRef<PatternsForOpcode> OpToPatterns) {\n";
  1007. O.indent(2) << " assert(std::is_sorted(\n";
  1008. O.indent(2) << " OpToPatterns.begin(), OpToPatterns.end(),\n";
  1009. O.indent(2) << " [](const PatternsForOpcode &L, const "
  1010. "PatternsForOpcode &R) {\n";
  1011. O.indent(2) << " return L.Opcode < R.Opcode;\n";
  1012. O.indent(2) << " }) &&\n";
  1013. O.indent(2) << " \"tablegen failed to sort opcode patterns\");\n";
  1014. O.indent(2) << " }\n";
  1015. O.indent(2) << "} sortCheckVar(OpToPatterns);\n";
  1016. O << "#endif\n\n";
  1017. O.indent(2) << "AliasMatchingData M {\n";
  1018. O.indent(2) << " makeArrayRef(OpToPatterns),\n";
  1019. O.indent(2) << " makeArrayRef(Patterns),\n";
  1020. O.indent(2) << " makeArrayRef(Conds),\n";
  1021. O.indent(2) << " StringRef(AsmStrings, array_lengthof(AsmStrings)),\n";
  1022. if (MCOpPredicates.empty())
  1023. O.indent(2) << " nullptr,\n";
  1024. else
  1025. O.indent(2) << " &" << Target.getName() << ClassName << "ValidateMCOperand,\n";
  1026. O.indent(2) << "};\n";
  1027. O.indent(2) << "const char *AsmString = matchAliasPatterns(MI, "
  1028. << (PassSubtarget ? "&STI" : "nullptr") << ", M);\n";
  1029. O.indent(2) << "if (!AsmString) return false;\n\n";
  1030. // Code that prints the alias, replacing the operands with the ones from the
  1031. // MCInst.
  1032. O << " unsigned I = 0;\n";
  1033. O << " while (AsmString[I] != ' ' && AsmString[I] != '\\t' &&\n";
  1034. O << " AsmString[I] != '$' && AsmString[I] != '\\0')\n";
  1035. O << " ++I;\n";
  1036. O << " OS << '\\t' << StringRef(AsmString, I);\n";
  1037. O << " if (AsmString[I] != '\\0') {\n";
  1038. O << " if (AsmString[I] == ' ' || AsmString[I] == '\\t') {\n";
  1039. O << " OS << '\\t';\n";
  1040. O << " ++I;\n";
  1041. O << " }\n";
  1042. O << " do {\n";
  1043. O << " if (AsmString[I] == '$') {\n";
  1044. O << " ++I;\n";
  1045. O << " if (AsmString[I] == (char)0xff) {\n";
  1046. O << " ++I;\n";
  1047. O << " int OpIdx = AsmString[I++] - 1;\n";
  1048. O << " int PrintMethodIdx = AsmString[I++] - 1;\n";
  1049. O << " printCustomAliasOperand(MI, Address, OpIdx, PrintMethodIdx, ";
  1050. O << (PassSubtarget ? "STI, " : "");
  1051. O << "OS);\n";
  1052. O << " } else\n";
  1053. O << " printOperand(MI, unsigned(AsmString[I++]) - 1, ";
  1054. O << (PassSubtarget ? "STI, " : "");
  1055. O << "OS);\n";
  1056. O << " } else {\n";
  1057. O << " OS << AsmString[I++];\n";
  1058. O << " }\n";
  1059. O << " } while (AsmString[I] != '\\0');\n";
  1060. O << " }\n\n";
  1061. O << " return true;\n";
  1062. O << "}\n\n";
  1063. //////////////////////////////
  1064. // Write out the printCustomAliasOperand function
  1065. //////////////////////////////
  1066. O << "void " << Target.getName() << ClassName << "::"
  1067. << "printCustomAliasOperand(\n"
  1068. << " const MCInst *MI, uint64_t Address, unsigned OpIdx,\n"
  1069. << " unsigned PrintMethodIdx,\n"
  1070. << (PassSubtarget ? " const MCSubtargetInfo &STI,\n" : "")
  1071. << " raw_ostream &OS) {\n";
  1072. if (PrintMethods.empty())
  1073. O << " llvm_unreachable(\"Unknown PrintMethod kind\");\n";
  1074. else {
  1075. O << " switch (PrintMethodIdx) {\n"
  1076. << " default:\n"
  1077. << " llvm_unreachable(\"Unknown PrintMethod kind\");\n"
  1078. << " break;\n";
  1079. for (unsigned i = 0; i < PrintMethods.size(); ++i) {
  1080. O << " case " << i << ":\n"
  1081. << " " << PrintMethods[i].first << "(MI, "
  1082. << (PrintMethods[i].second ? "Address, " : "") << "OpIdx, "
  1083. << (PassSubtarget ? "STI, " : "") << "OS);\n"
  1084. << " break;\n";
  1085. }
  1086. O << " }\n";
  1087. }
  1088. O << "}\n\n";
  1089. if (!MCOpPredicates.empty()) {
  1090. O << "static bool " << Target.getName() << ClassName
  1091. << "ValidateMCOperand(const MCOperand &MCOp,\n"
  1092. << " const MCSubtargetInfo &STI,\n"
  1093. << " unsigned PredicateIndex) {\n"
  1094. << " switch (PredicateIndex) {\n"
  1095. << " default:\n"
  1096. << " llvm_unreachable(\"Unknown MCOperandPredicate kind\");\n"
  1097. << " break;\n";
  1098. for (unsigned i = 0; i < MCOpPredicates.size(); ++i) {
  1099. StringRef MCOpPred = MCOpPredicates[i]->getValueAsString("MCOperandPredicate");
  1100. O << " case " << i + 1 << ": {\n"
  1101. << MCOpPred.data() << "\n"
  1102. << " }\n";
  1103. }
  1104. O << " }\n"
  1105. << "}\n\n";
  1106. }
  1107. O << "#endif // PRINT_ALIAS_INSTR\n";
  1108. }
  1109. AsmWriterEmitter::AsmWriterEmitter(RecordKeeper &R) : Records(R), Target(R) {
  1110. Record *AsmWriter = Target.getAsmWriter();
  1111. unsigned Variant = AsmWriter->getValueAsInt("Variant");
  1112. // Get the instruction numbering.
  1113. NumberedInstructions = Target.getInstructionsByEnumValue();
  1114. for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
  1115. const CodeGenInstruction *I = NumberedInstructions[i];
  1116. if (!I->AsmString.empty() && I->TheDef->getName() != "PHI")
  1117. Instructions.emplace_back(*I, i, Variant);
  1118. }
  1119. }
  1120. void AsmWriterEmitter::run(raw_ostream &O) {
  1121. std::vector<std::vector<std::string>> TableDrivenOperandPrinters;
  1122. unsigned BitsLeft = 0;
  1123. unsigned AsmStrBits = 0;
  1124. EmitGetMnemonic(O, TableDrivenOperandPrinters, BitsLeft, AsmStrBits);
  1125. EmitPrintInstruction(O, TableDrivenOperandPrinters, BitsLeft, AsmStrBits);
  1126. EmitGetRegisterName(O);
  1127. EmitPrintAliasInstruction(O);
  1128. }
  1129. namespace llvm {
  1130. void EmitAsmWriter(RecordKeeper &RK, raw_ostream &OS) {
  1131. emitSourceFileHeader("Assembly Writer Source Fragment", OS);
  1132. AsmWriterEmitter(RK).run(OS);
  1133. }
  1134. } // end namespace llvm