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 <vector>
31 using namespace llvm;
32 
33 #define DEBUG_TYPE "asm-writer-emitter"
34 
35 namespace {
36 class AsmWriterEmitter {
37   RecordKeeper &Records;
38   CodeGenTarget Target;
39   ArrayRef<const CodeGenInstruction *> NumberedInstructions;
40   std::vector<AsmWriterInst> Instructions;
41 public:
42   AsmWriterEmitter(RecordKeeper &R);
43 
44   void run(raw_ostream &o);
45 
46 private:
47   void EmitPrintInstruction(raw_ostream &o);
48   void EmitGetRegisterName(raw_ostream &o);
49   void EmitPrintAliasInstruction(raw_ostream &O);
50 
51   void FindUniqueOperandCommands(std::vector<std::string> &UOC,
52                                  std::vector<std::vector<unsigned>> &InstIdxs,
53                                  std::vector<unsigned> &InstOpsUsed,
54                                  bool PassSubtarget) const;
55 };
56 } // end anonymous namespace
57 
58 static void PrintCases(std::vector<std::pair<std::string,
59                        AsmWriterOperand> > &OpsToPrint, raw_ostream &O,
60                        bool PassSubtarget) {
61   O << "    case " << OpsToPrint.back().first << ":";
62   AsmWriterOperand TheOp = OpsToPrint.back().second;
63   OpsToPrint.pop_back();
64 
65   // Check to see if any other operands are identical in this list, and if so,
66   // emit a case label for them.
67   for (unsigned i = OpsToPrint.size(); i != 0; --i)
68     if (OpsToPrint[i-1].second == TheOp) {
69       O << "\n    case " << OpsToPrint[i-1].first << ":";
70       OpsToPrint.erase(OpsToPrint.begin()+i-1);
71     }
72 
73   // Finally, emit the code.
74   O << "\n      " << TheOp.getCode(PassSubtarget);
75   O << "\n      break;\n";
76 }
77 
78 
79 /// EmitInstructions - Emit the last instruction in the vector and any other
80 /// instructions that are suitably similar to it.
81 static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
82                              raw_ostream &O, bool PassSubtarget) {
83   AsmWriterInst FirstInst = Insts.back();
84   Insts.pop_back();
85 
86   std::vector<AsmWriterInst> SimilarInsts;
87   unsigned DifferingOperand = ~0;
88   for (unsigned i = Insts.size(); i != 0; --i) {
89     unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst);
90     if (DiffOp != ~1U) {
91       if (DifferingOperand == ~0U)  // First match!
92         DifferingOperand = DiffOp;
93 
94       // If this differs in the same operand as the rest of the instructions in
95       // this class, move it to the SimilarInsts list.
96       if (DifferingOperand == DiffOp || DiffOp == ~0U) {
97         SimilarInsts.push_back(Insts[i-1]);
98         Insts.erase(Insts.begin()+i-1);
99       }
100     }
101   }
102 
103   O << "  case " << FirstInst.CGI->Namespace << "::"
104     << FirstInst.CGI->TheDef->getName() << ":\n";
105   for (const AsmWriterInst &AWI : SimilarInsts)
106     O << "  case " << AWI.CGI->Namespace << "::"
107       << AWI.CGI->TheDef->getName() << ":\n";
108   for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
109     if (i != DifferingOperand) {
110       // If the operand is the same for all instructions, just print it.
111       O << "    " << FirstInst.Operands[i].getCode(PassSubtarget);
112     } else {
113       // If this is the operand that varies between all of the instructions,
114       // emit a switch for just this operand now.
115       O << "    switch (MI->getOpcode()) {\n";
116       O << "    default: llvm_unreachable(\"Unexpected opcode.\");\n";
117       std::vector<std::pair<std::string, AsmWriterOperand> > OpsToPrint;
118       OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace + "::" +
119                                           FirstInst.CGI->TheDef->getName(),
120                                           FirstInst.Operands[i]));
121 
122       for (const AsmWriterInst &AWI : SimilarInsts) {
123         OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace+"::"+
124                                             AWI.CGI->TheDef->getName(),
125                                             AWI.Operands[i]));
126       }
127       std::reverse(OpsToPrint.begin(), OpsToPrint.end());
128       while (!OpsToPrint.empty())
129         PrintCases(OpsToPrint, O, PassSubtarget);
130       O << "    }";
131     }
132     O << "\n";
133   }
134   O << "    break;\n";
135 }
136 
137 void AsmWriterEmitter::
138 FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
139                           std::vector<std::vector<unsigned>> &InstIdxs,
140                           std::vector<unsigned> &InstOpsUsed,
141                           bool PassSubtarget) const {
142 
143   // This vector parallels UniqueOperandCommands, keeping track of which
144   // instructions each case are used for.  It is a comma separated string of
145   // enums.
146   std::vector<std::string> InstrsForCase;
147   InstrsForCase.resize(UniqueOperandCommands.size());
148   InstOpsUsed.assign(UniqueOperandCommands.size(), 0);
149 
150   for (size_t i = 0, e = Instructions.size(); i != e; ++i) {
151     const AsmWriterInst &Inst = Instructions[i];
152     if (Inst.Operands.empty())
153       continue;   // Instruction already done.
154 
155     std::string Command = "    "+Inst.Operands[0].getCode(PassSubtarget)+"\n";
156 
157     // Check to see if we already have 'Command' in UniqueOperandCommands.
158     // If not, add it.
159     auto I = std::find(UniqueOperandCommands.begin(),
160                        UniqueOperandCommands.end(), 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 = std::remove_if(Instructions.begin(), Instructions.end(),
454                           [](AsmWriterInst &Inst) {
455                             return Inst.Operands.empty();
456                           });
457   Instructions.erase(I, Instructions.end());
458 
459 
460   // Because this is a vector, we want to emit from the end.  Reverse all of the
461   // elements in the vector.
462   std::reverse(Instructions.begin(), Instructions.end());
463 
464 
465   // Now that we've emitted all of the operand info that fit into 64 bits, emit
466   // information for those instructions that are left.  This is a less dense
467   // encoding, but we expect the main 64-bit table to handle the majority of
468   // instructions.
469   if (!Instructions.empty()) {
470     // Find the opcode # of inline asm.
471     O << "  switch (MI->getOpcode()) {\n";
472     O << "  default: llvm_unreachable(\"Unexpected opcode.\");\n";
473     while (!Instructions.empty())
474       EmitInstructions(Instructions, O, PassSubtarget);
475 
476     O << "  }\n";
477   }
478 
479   O << "}\n";
480 }
481 
482 static const char *getMinimalTypeForRange(uint64_t Range) {
483   assert(Range < 0xFFFFFFFFULL && "Enum too large");
484   if (Range > 0xFFFF)
485     return "uint32_t";
486   if (Range > 0xFF)
487     return "uint16_t";
488   return "uint8_t";
489 }
490 
491 static void
492 emitRegisterNameString(raw_ostream &O, StringRef AltName,
493                        const std::deque<CodeGenRegister> &Registers) {
494   SequenceToOffsetTable<std::string> StringTable;
495   SmallVector<std::string, 4> AsmNames(Registers.size());
496   unsigned i = 0;
497   for (const auto &Reg : Registers) {
498     std::string &AsmName = AsmNames[i++];
499 
500     // "NoRegAltName" is special. We don't need to do a lookup for that,
501     // as it's just a reference to the default register name.
502     if (AltName == "" || AltName == "NoRegAltName") {
503       AsmName = Reg.TheDef->getValueAsString("AsmName");
504       if (AsmName.empty())
505         AsmName = Reg.getName();
506     } else {
507       // Make sure the register has an alternate name for this index.
508       std::vector<Record*> AltNameList =
509         Reg.TheDef->getValueAsListOfDefs("RegAltNameIndices");
510       unsigned Idx = 0, e;
511       for (e = AltNameList.size();
512            Idx < e && (AltNameList[Idx]->getName() != AltName);
513            ++Idx)
514         ;
515       // If the register has an alternate name for this index, use it.
516       // Otherwise, leave it empty as an error flag.
517       if (Idx < e) {
518         std::vector<std::string> AltNames =
519           Reg.TheDef->getValueAsListOfStrings("AltNames");
520         if (AltNames.size() <= Idx)
521           PrintFatalError(Reg.TheDef->getLoc(),
522                           "Register definition missing alt name for '" +
523                           AltName + "'.");
524         AsmName = AltNames[Idx];
525       }
526     }
527     StringTable.add(AsmName);
528   }
529 
530   StringTable.layout();
531   O << "  static const char AsmStrs" << AltName << "[] = {\n";
532   StringTable.emit(O, printChar);
533   O << "  };\n\n";
534 
535   O << "  static const " << getMinimalTypeForRange(StringTable.size()-1)
536     << " RegAsmOffset" << AltName << "[] = {";
537   for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
538     if ((i % 14) == 0)
539       O << "\n    ";
540     O << StringTable.get(AsmNames[i]) << ", ";
541   }
542   O << "\n  };\n"
543     << "\n";
544 }
545 
546 void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
547   Record *AsmWriter = Target.getAsmWriter();
548   std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
549   const auto &Registers = Target.getRegBank().getRegisters();
550   const std::vector<Record*> &AltNameIndices = Target.getRegAltNameIndices();
551   bool hasAltNames = AltNameIndices.size() > 1;
552   std::string Namespace =
553       Registers.front().TheDef->getValueAsString("Namespace");
554 
555   O <<
556   "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
557   "/// from the register set description.  This returns the assembler name\n"
558   "/// for the specified register.\n"
559   "const char *" << Target.getName() << ClassName << "::";
560   if (hasAltNames)
561     O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx) {\n";
562   else
563     O << "getRegisterName(unsigned RegNo) {\n";
564   O << "  assert(RegNo && RegNo < " << (Registers.size()+1)
565     << " && \"Invalid register number!\");\n"
566     << "\n";
567 
568   if (hasAltNames) {
569     for (const Record *R : AltNameIndices)
570       emitRegisterNameString(O, R->getName(), Registers);
571   } else
572     emitRegisterNameString(O, "", Registers);
573 
574   if (hasAltNames) {
575     O << "  switch(AltIdx) {\n"
576       << "  default: llvm_unreachable(\"Invalid register alt name index!\");\n";
577     for (const Record *R : AltNameIndices) {
578       std::string AltName(R->getName());
579       std::string Prefix = !Namespace.empty() ? Namespace + "::" : "";
580       O << "  case " << Prefix << AltName << ":\n"
581         << "    assert(*(AsmStrs" << AltName << "+RegAsmOffset"
582         << AltName << "[RegNo-1]) &&\n"
583         << "           \"Invalid alt name index for register!\");\n"
584         << "    return AsmStrs" << AltName << "+RegAsmOffset"
585         << AltName << "[RegNo-1];\n";
586     }
587     O << "  }\n";
588   } else {
589     O << "  assert (*(AsmStrs+RegAsmOffset[RegNo-1]) &&\n"
590       << "          \"Invalid alt name index for register!\");\n"
591       << "  return AsmStrs+RegAsmOffset[RegNo-1];\n";
592   }
593   O << "}\n";
594 }
595 
596 namespace {
597 // IAPrinter - Holds information about an InstAlias. Two InstAliases match if
598 // they both have the same conditionals. In which case, we cannot print out the
599 // alias for that pattern.
600 class IAPrinter {
601   std::vector<std::string> Conds;
602   std::map<StringRef, std::pair<int, int>> OpMap;
603   SmallVector<Record*, 4> ReqFeatures;
604 
605   std::string Result;
606   std::string AsmString;
607 public:
608   IAPrinter(std::string R, std::string AS) : Result(R), AsmString(AS) {}
609 
610   void addCond(const std::string &C) { Conds.push_back(C); }
611 
612   void addOperand(StringRef Op, int OpIdx, int PrintMethodIdx = -1) {
613     assert(OpIdx >= 0 && OpIdx < 0xFE && "Idx out of range");
614     assert(PrintMethodIdx >= -1 && PrintMethodIdx < 0xFF &&
615            "Idx out of range");
616     OpMap[Op] = std::make_pair(OpIdx, PrintMethodIdx);
617   }
618 
619   bool isOpMapped(StringRef Op) { return OpMap.find(Op) != OpMap.end(); }
620   int getOpIndex(StringRef Op) { return OpMap[Op].first; }
621   std::pair<int, int> &getOpData(StringRef Op) { return OpMap[Op]; }
622 
623   std::pair<StringRef, StringRef::iterator> parseName(StringRef::iterator Start,
624                                                       StringRef::iterator End) {
625     StringRef::iterator I = Start;
626     StringRef::iterator Next;
627     if (*I == '{') {
628       // ${some_name}
629       Start = ++I;
630       while (I != End && *I != '}')
631         ++I;
632       Next = I;
633       // eat the final '}'
634       if (Next != End)
635         ++Next;
636     } else {
637       // $name, just eat the usual suspects.
638       while (I != End &&
639              ((*I >= 'a' && *I <= 'z') || (*I >= 'A' && *I <= 'Z') ||
640               (*I >= '0' && *I <= '9') || *I == '_'))
641         ++I;
642       Next = I;
643     }
644 
645     return std::make_pair(StringRef(Start, I - Start), Next);
646   }
647 
648   void print(raw_ostream &O) {
649     if (Conds.empty() && ReqFeatures.empty()) {
650       O.indent(6) << "return true;\n";
651       return;
652     }
653 
654     O << "if (";
655 
656     for (std::vector<std::string>::iterator
657            I = Conds.begin(), E = Conds.end(); I != E; ++I) {
658       if (I != Conds.begin()) {
659         O << " &&\n";
660         O.indent(8);
661       }
662 
663       O << *I;
664     }
665 
666     O << ") {\n";
667     O.indent(6) << "// " << Result << "\n";
668 
669     // Directly mangle mapped operands into the string. Each operand is
670     // identified by a '$' sign followed by a byte identifying the number of the
671     // operand. We add one to the index to avoid zero bytes.
672     StringRef ASM(AsmString);
673     SmallString<128> OutString;
674     raw_svector_ostream OS(OutString);
675     for (StringRef::iterator I = ASM.begin(), E = ASM.end(); I != E;) {
676       OS << *I;
677       if (*I == '$') {
678         StringRef Name;
679         std::tie(Name, I) = parseName(++I, E);
680         assert(isOpMapped(Name) && "Unmapped operand!");
681 
682         int OpIndex, PrintIndex;
683         std::tie(OpIndex, PrintIndex) = getOpData(Name);
684         if (PrintIndex == -1) {
685           // Can use the default printOperand route.
686           OS << format("\\x%02X", (unsigned char)OpIndex + 1);
687         } else
688           // 3 bytes if a PrintMethod is needed: 0xFF, the MCInst operand
689           // number, and which of our pre-detected Methods to call.
690           OS << format("\\xFF\\x%02X\\x%02X", OpIndex + 1, PrintIndex + 1);
691       } else {
692         ++I;
693       }
694     }
695 
696     // Emit the string.
697     O.indent(6) << "AsmString = \"" << OutString << "\";\n";
698 
699     O.indent(6) << "break;\n";
700     O.indent(4) << '}';
701   }
702 
703   bool operator==(const IAPrinter &RHS) const {
704     if (Conds.size() != RHS.Conds.size())
705       return false;
706 
707     unsigned Idx = 0;
708     for (const auto &str : Conds)
709       if (str != RHS.Conds[Idx++])
710         return false;
711 
712     return true;
713   }
714 };
715 
716 } // end anonymous namespace
717 
718 static unsigned CountNumOperands(StringRef AsmString, unsigned Variant) {
719   std::string FlatAsmString =
720       CodeGenInstruction::FlattenAsmStringVariants(AsmString, Variant);
721   AsmString = FlatAsmString;
722 
723   return AsmString.count(' ') + AsmString.count('\t');
724 }
725 
726 namespace {
727 struct AliasPriorityComparator {
728   typedef std::pair<CodeGenInstAlias, int> ValueType;
729   bool operator()(const ValueType &LHS, const ValueType &RHS) {
730     if (LHS.second ==  RHS.second) {
731       // We don't actually care about the order, but for consistency it
732       // shouldn't depend on pointer comparisons.
733       return LHS.first.TheDef->getName() < RHS.first.TheDef->getName();
734     }
735 
736     // Aliases with larger priorities should be considered first.
737     return LHS.second > RHS.second;
738   }
739 };
740 }
741 
742 
743 void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
744   Record *AsmWriter = Target.getAsmWriter();
745 
746   O << "\n#ifdef PRINT_ALIAS_INSTR\n";
747   O << "#undef PRINT_ALIAS_INSTR\n\n";
748 
749   //////////////////////////////
750   // Gather information about aliases we need to print
751   //////////////////////////////
752 
753   // Emit the method that prints the alias instruction.
754   std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
755   unsigned Variant = AsmWriter->getValueAsInt("Variant");
756   bool PassSubtarget = AsmWriter->getValueAsInt("PassSubtarget");
757 
758   std::vector<Record*> AllInstAliases =
759     Records.getAllDerivedDefinitions("InstAlias");
760 
761   // Create a map from the qualified name to a list of potential matches.
762   typedef std::set<std::pair<CodeGenInstAlias, int>, AliasPriorityComparator>
763       AliasWithPriority;
764   std::map<std::string, AliasWithPriority> AliasMap;
765   for (Record *R : AllInstAliases) {
766     int Priority = R->getValueAsInt("EmitPriority");
767     if (Priority < 1)
768       continue; // Aliases with priority 0 are never emitted.
769 
770     const DagInit *DI = R->getValueAsDag("ResultInst");
771     const DefInit *Op = cast<DefInit>(DI->getOperator());
772     AliasMap[getQualifiedName(Op->getDef())].insert(
773         std::make_pair(CodeGenInstAlias(R, Variant, Target), Priority));
774   }
775 
776   // A map of which conditions need to be met for each instruction operand
777   // before it can be matched to the mnemonic.
778   std::map<std::string, std::vector<IAPrinter>> IAPrinterMap;
779 
780   std::vector<std::string> PrintMethods;
781 
782   // A list of MCOperandPredicates for all operands in use, and the reverse map
783   std::vector<const Record*> MCOpPredicates;
784   DenseMap<const Record*, unsigned> MCOpPredicateMap;
785 
786   for (auto &Aliases : AliasMap) {
787     for (auto &Alias : Aliases.second) {
788       const CodeGenInstAlias &CGA = Alias.first;
789       unsigned LastOpNo = CGA.ResultInstOperandIndex.size();
790       unsigned NumResultOps =
791           CountNumOperands(CGA.ResultInst->AsmString, Variant);
792 
793       // Don't emit the alias if it has more operands than what it's aliasing.
794       if (NumResultOps < CountNumOperands(CGA.AsmString, Variant))
795         continue;
796 
797       IAPrinter IAP(CGA.Result->getAsString(), CGA.AsmString);
798 
799       unsigned NumMIOps = 0;
800       for (auto &Operand : CGA.ResultOperands)
801         NumMIOps += Operand.getMINumOperands();
802 
803       std::string Cond;
804       Cond = std::string("MI->getNumOperands() == ") + llvm::utostr(NumMIOps);
805       IAP.addCond(Cond);
806 
807       bool CantHandle = false;
808 
809       unsigned MIOpNum = 0;
810       for (unsigned i = 0, e = LastOpNo; i != e; ++i) {
811         std::string Op = "MI->getOperand(" + llvm::utostr(MIOpNum) + ")";
812 
813         const CodeGenInstAlias::ResultOperand &RO = CGA.ResultOperands[i];
814 
815         switch (RO.Kind) {
816         case CodeGenInstAlias::ResultOperand::K_Record: {
817           const Record *Rec = RO.getRecord();
818           StringRef ROName = RO.getName();
819           int PrintMethodIdx = -1;
820 
821           // These two may have a PrintMethod, which we want to record (if it's
822           // the first time we've seen it) and provide an index for the aliasing
823           // code to use.
824           if (Rec->isSubClassOf("RegisterOperand") ||
825               Rec->isSubClassOf("Operand")) {
826             std::string PrintMethod = Rec->getValueAsString("PrintMethod");
827             if (PrintMethod != "" && PrintMethod != "printOperand") {
828               PrintMethodIdx = std::find(PrintMethods.begin(),
829                                          PrintMethods.end(), PrintMethod) -
830                                PrintMethods.begin();
831               if (static_cast<unsigned>(PrintMethodIdx) == PrintMethods.size())
832                 PrintMethods.push_back(PrintMethod);
833             }
834           }
835 
836           if (Rec->isSubClassOf("RegisterOperand"))
837             Rec = Rec->getValueAsDef("RegClass");
838           if (Rec->isSubClassOf("RegisterClass")) {
839             IAP.addCond(Op + ".isReg()");
840 
841             if (!IAP.isOpMapped(ROName)) {
842               IAP.addOperand(ROName, MIOpNum, PrintMethodIdx);
843               Record *R = CGA.ResultOperands[i].getRecord();
844               if (R->isSubClassOf("RegisterOperand"))
845                 R = R->getValueAsDef("RegClass");
846               Cond = std::string("MRI.getRegClass(") + Target.getName() + "::" +
847                      R->getName() + "RegClassID)"
848                                     ".contains(" + Op + ".getReg())";
849             } else {
850               Cond = Op + ".getReg() == MI->getOperand(" +
851                      llvm::utostr(IAP.getOpIndex(ROName)) + ").getReg()";
852             }
853           } else {
854             // Assume all printable operands are desired for now. This can be
855             // overridden in the InstAlias instantiation if necessary.
856             IAP.addOperand(ROName, MIOpNum, PrintMethodIdx);
857 
858             // There might be an additional predicate on the MCOperand
859             unsigned Entry = MCOpPredicateMap[Rec];
860             if (!Entry) {
861               if (!Rec->isValueUnset("MCOperandPredicate")) {
862                 MCOpPredicates.push_back(Rec);
863                 Entry = MCOpPredicates.size();
864                 MCOpPredicateMap[Rec] = Entry;
865               } else
866                 break; // No conditions on this operand at all
867             }
868             Cond = Target.getName() + ClassName + "ValidateMCOperand(" +
869                    Op + ", STI, " + llvm::utostr(Entry) + ")";
870           }
871           // for all subcases of ResultOperand::K_Record:
872           IAP.addCond(Cond);
873           break;
874         }
875         case CodeGenInstAlias::ResultOperand::K_Imm: {
876           // Just because the alias has an immediate result, doesn't mean the
877           // MCInst will. An MCExpr could be present, for example.
878           IAP.addCond(Op + ".isImm()");
879 
880           Cond = Op + ".getImm() == " +
881                  llvm::utostr(CGA.ResultOperands[i].getImm());
882           IAP.addCond(Cond);
883           break;
884         }
885         case CodeGenInstAlias::ResultOperand::K_Reg:
886           // If this is zero_reg, something's playing tricks we're not
887           // equipped to handle.
888           if (!CGA.ResultOperands[i].getRegister()) {
889             CantHandle = true;
890             break;
891           }
892 
893           Cond = Op + ".getReg() == " + Target.getName() + "::" +
894                  CGA.ResultOperands[i].getRegister()->getName();
895           IAP.addCond(Cond);
896           break;
897         }
898 
899         MIOpNum += RO.getMINumOperands();
900       }
901 
902       if (CantHandle) continue;
903       IAPrinterMap[Aliases.first].push_back(std::move(IAP));
904     }
905   }
906 
907   //////////////////////////////
908   // Write out the printAliasInstr function
909   //////////////////////////////
910 
911   std::string Header;
912   raw_string_ostream HeaderO(Header);
913 
914   HeaderO << "bool " << Target.getName() << ClassName
915           << "::printAliasInstr(const MCInst"
916           << " *MI, " << (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
917           << "raw_ostream &OS) {\n";
918 
919   std::string Cases;
920   raw_string_ostream CasesO(Cases);
921 
922   for (auto &Entry : IAPrinterMap) {
923     std::vector<IAPrinter> &IAPs = Entry.second;
924     std::vector<IAPrinter*> UniqueIAPs;
925 
926     for (auto &LHS : IAPs) {
927       bool IsDup = false;
928       for (const auto &RHS : IAPs) {
929         if (&LHS != &RHS && LHS == RHS) {
930           IsDup = true;
931           break;
932         }
933       }
934 
935       if (!IsDup)
936         UniqueIAPs.push_back(&LHS);
937     }
938 
939     if (UniqueIAPs.empty()) continue;
940 
941     CasesO.indent(2) << "case " << Entry.first << ":\n";
942 
943     for (IAPrinter *IAP : UniqueIAPs) {
944       CasesO.indent(4);
945       IAP->print(CasesO);
946       CasesO << '\n';
947     }
948 
949     CasesO.indent(4) << "return false;\n";
950   }
951 
952   if (CasesO.str().empty()) {
953     O << HeaderO.str();
954     O << "  return false;\n";
955     O << "}\n\n";
956     O << "#endif // PRINT_ALIAS_INSTR\n";
957     return;
958   }
959 
960   if (!MCOpPredicates.empty())
961     O << "static bool " << Target.getName() << ClassName
962       << "ValidateMCOperand(const MCOperand &MCOp,\n"
963       << "                  const MCSubtargetInfo &STI,\n"
964       << "                  unsigned PredicateIndex);\n";
965 
966   O << HeaderO.str();
967   O.indent(2) << "const char *AsmString;\n";
968   O.indent(2) << "switch (MI->getOpcode()) {\n";
969   O.indent(2) << "default: return false;\n";
970   O << CasesO.str();
971   O.indent(2) << "}\n\n";
972 
973   // Code that prints the alias, replacing the operands with the ones from the
974   // MCInst.
975   O << "  unsigned I = 0;\n";
976   O << "  while (AsmString[I] != ' ' && AsmString[I] != '\t' &&\n";
977   O << "         AsmString[I] != '\\0')\n";
978   O << "    ++I;\n";
979   O << "  OS << '\\t' << StringRef(AsmString, I);\n";
980 
981   O << "  if (AsmString[I] != '\\0') {\n";
982   O << "    OS << '\\t';\n";
983   O << "    do {\n";
984   O << "      if (AsmString[I] == '$') {\n";
985   O << "        ++I;\n";
986   O << "        if (AsmString[I] == (char)0xff) {\n";
987   O << "          ++I;\n";
988   O << "          int OpIdx = AsmString[I++] - 1;\n";
989   O << "          int PrintMethodIdx = AsmString[I++] - 1;\n";
990   O << "          printCustomAliasOperand(MI, OpIdx, PrintMethodIdx, ";
991   O << (PassSubtarget ? "STI, " : "");
992   O << "OS);\n";
993   O << "        } else\n";
994   O << "          printOperand(MI, unsigned(AsmString[I++]) - 1, ";
995   O << (PassSubtarget ? "STI, " : "");
996   O << "OS);\n";
997   O << "      } else {\n";
998   O << "        OS << AsmString[I++];\n";
999   O << "      }\n";
1000   O << "    } while (AsmString[I] != '\\0');\n";
1001   O << "  }\n\n";
1002 
1003   O << "  return true;\n";
1004   O << "}\n\n";
1005 
1006   //////////////////////////////
1007   // Write out the printCustomAliasOperand function
1008   //////////////////////////////
1009 
1010   O << "void " << Target.getName() << ClassName << "::"
1011     << "printCustomAliasOperand(\n"
1012     << "         const MCInst *MI, unsigned OpIdx,\n"
1013     << "         unsigned PrintMethodIdx,\n"
1014     << (PassSubtarget ? "         const MCSubtargetInfo &STI,\n" : "")
1015     << "         raw_ostream &OS) {\n";
1016   if (PrintMethods.empty())
1017     O << "  llvm_unreachable(\"Unknown PrintMethod kind\");\n";
1018   else {
1019     O << "  switch (PrintMethodIdx) {\n"
1020       << "  default:\n"
1021       << "    llvm_unreachable(\"Unknown PrintMethod kind\");\n"
1022       << "    break;\n";
1023 
1024     for (unsigned i = 0; i < PrintMethods.size(); ++i) {
1025       O << "  case " << i << ":\n"
1026         << "    " << PrintMethods[i] << "(MI, OpIdx, "
1027         << (PassSubtarget ? "STI, " : "") << "OS);\n"
1028         << "    break;\n";
1029     }
1030     O << "  }\n";
1031   }
1032   O << "}\n\n";
1033 
1034   if (!MCOpPredicates.empty()) {
1035     O << "static bool " << Target.getName() << ClassName
1036       << "ValidateMCOperand(const MCOperand &MCOp,\n"
1037       << "                  const MCSubtargetInfo &STI,\n"
1038       << "                  unsigned PredicateIndex) {\n"
1039       << "  switch (PredicateIndex) {\n"
1040       << "  default:\n"
1041       << "    llvm_unreachable(\"Unknown MCOperandPredicate kind\");\n"
1042       << "    break;\n";
1043 
1044     for (unsigned i = 0; i < MCOpPredicates.size(); ++i) {
1045       Init *MCOpPred = MCOpPredicates[i]->getValueInit("MCOperandPredicate");
1046       if (StringInit *SI = dyn_cast<StringInit>(MCOpPred)) {
1047         O << "  case " << i + 1 << ": {\n"
1048           << SI->getValue() << "\n"
1049           << "    }\n";
1050       } else
1051         llvm_unreachable("Unexpected MCOperandPredicate field!");
1052     }
1053     O << "  }\n"
1054       << "}\n\n";
1055   }
1056 
1057   O << "#endif // PRINT_ALIAS_INSTR\n";
1058 }
1059 
1060 AsmWriterEmitter::AsmWriterEmitter(RecordKeeper &R) : Records(R), Target(R) {
1061   Record *AsmWriter = Target.getAsmWriter();
1062   unsigned Variant = AsmWriter->getValueAsInt("Variant");
1063 
1064   // Get the instruction numbering.
1065   NumberedInstructions = Target.getInstructionsByEnumValue();
1066 
1067   for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
1068     const CodeGenInstruction *I = NumberedInstructions[i];
1069     if (!I->AsmString.empty() && I->TheDef->getName() != "PHI")
1070       Instructions.emplace_back(*I, i, Variant);
1071   }
1072 }
1073 
1074 void AsmWriterEmitter::run(raw_ostream &O) {
1075   EmitPrintInstruction(O);
1076   EmitGetRegisterName(O);
1077   EmitPrintAliasInstruction(O);
1078 }
1079 
1080 
1081 namespace llvm {
1082 
1083 void EmitAsmWriter(RecordKeeper &RK, raw_ostream &OS) {
1084   emitSourceFileHeader("Assembly Writer Source Fragment", OS);
1085   AsmWriterEmitter(RK).run(OS);
1086 }
1087 
1088 } // End llvm namespace
1089