AsmWriterEmitter.cpp 48 KB

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