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