tblgen: Twinify PrintFatalError.
[oota-llvm.git] / utils / TableGen / AsmWriterEmitter.cpp
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 is 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/StringExtras.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/Format.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/TableGen/Error.h"
24 #include "llvm/TableGen/Record.h"
25 #include "llvm/TableGen/TableGenBackend.h"
26 #include <algorithm>
27 #include <cassert>
28 #include <map>
29 #include <vector>
30 using namespace llvm;
31
32 namespace {
33 class AsmWriterEmitter {
34   RecordKeeper &Records;
35   CodeGenTarget Target;
36   std::map<const CodeGenInstruction*, AsmWriterInst*> CGIAWIMap;
37   const std::vector<const CodeGenInstruction*> *NumberedInstructions;
38   std::vector<AsmWriterInst> Instructions;
39 public:
40   AsmWriterEmitter(RecordKeeper &R);
41
42   void run(raw_ostream &o);
43
44 private:
45   void EmitPrintInstruction(raw_ostream &o);
46   void EmitGetRegisterName(raw_ostream &o);
47   void EmitPrintAliasInstruction(raw_ostream &O);
48
49   AsmWriterInst *getAsmWriterInstByID(unsigned ID) const {
50     assert(ID < NumberedInstructions->size());
51     std::map<const CodeGenInstruction*, AsmWriterInst*>::const_iterator I =
52       CGIAWIMap.find(NumberedInstructions->at(ID));
53     assert(I != CGIAWIMap.end() && "Didn't find inst!");
54     return I->second;
55   }
56   void FindUniqueOperandCommands(std::vector<std::string> &UOC,
57                                  std::vector<unsigned> &InstIdxs,
58                                  std::vector<unsigned> &InstOpsUsed) const;
59 };
60 } // end anonymous namespace
61
62 static void PrintCases(std::vector<std::pair<std::string,
63                        AsmWriterOperand> > &OpsToPrint, raw_ostream &O) {
64   O << "    case " << OpsToPrint.back().first << ": ";
65   AsmWriterOperand TheOp = OpsToPrint.back().second;
66   OpsToPrint.pop_back();
67
68   // Check to see if any other operands are identical in this list, and if so,
69   // emit a case label for them.
70   for (unsigned i = OpsToPrint.size(); i != 0; --i)
71     if (OpsToPrint[i-1].second == TheOp) {
72       O << "\n    case " << OpsToPrint[i-1].first << ": ";
73       OpsToPrint.erase(OpsToPrint.begin()+i-1);
74     }
75
76   // Finally, emit the code.
77   O << TheOp.getCode();
78   O << "break;\n";
79 }
80
81
82 /// EmitInstructions - Emit the last instruction in the vector and any other
83 /// instructions that are suitably similar to it.
84 static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
85                              raw_ostream &O) {
86   AsmWriterInst FirstInst = Insts.back();
87   Insts.pop_back();
88
89   std::vector<AsmWriterInst> SimilarInsts;
90   unsigned DifferingOperand = ~0;
91   for (unsigned i = Insts.size(); i != 0; --i) {
92     unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst);
93     if (DiffOp != ~1U) {
94       if (DifferingOperand == ~0U)  // First match!
95         DifferingOperand = DiffOp;
96
97       // If this differs in the same operand as the rest of the instructions in
98       // this class, move it to the SimilarInsts list.
99       if (DifferingOperand == DiffOp || DiffOp == ~0U) {
100         SimilarInsts.push_back(Insts[i-1]);
101         Insts.erase(Insts.begin()+i-1);
102       }
103     }
104   }
105
106   O << "  case " << FirstInst.CGI->Namespace << "::"
107     << FirstInst.CGI->TheDef->getName() << ":\n";
108   for (unsigned i = 0, e = SimilarInsts.size(); i != e; ++i)
109     O << "  case " << SimilarInsts[i].CGI->Namespace << "::"
110       << SimilarInsts[i].CGI->TheDef->getName() << ":\n";
111   for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
112     if (i != DifferingOperand) {
113       // If the operand is the same for all instructions, just print it.
114       O << "    " << FirstInst.Operands[i].getCode();
115     } else {
116       // If this is the operand that varies between all of the instructions,
117       // emit a switch for just this operand now.
118       O << "    switch (MI->getOpcode()) {\n";
119       std::vector<std::pair<std::string, AsmWriterOperand> > OpsToPrint;
120       OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace + "::" +
121                                           FirstInst.CGI->TheDef->getName(),
122                                           FirstInst.Operands[i]));
123
124       for (unsigned si = 0, e = SimilarInsts.size(); si != e; ++si) {
125         AsmWriterInst &AWI = SimilarInsts[si];
126         OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace+"::"+
127                                             AWI.CGI->TheDef->getName(),
128                                             AWI.Operands[i]));
129       }
130       std::reverse(OpsToPrint.begin(), OpsToPrint.end());
131       while (!OpsToPrint.empty())
132         PrintCases(OpsToPrint, O);
133       O << "    }";
134     }
135     O << "\n";
136   }
137   O << "    break;\n";
138 }
139
140 void AsmWriterEmitter::
141 FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
142                           std::vector<unsigned> &InstIdxs,
143                           std::vector<unsigned> &InstOpsUsed) const {
144   InstIdxs.assign(NumberedInstructions->size(), ~0U);
145
146   // This vector parallels UniqueOperandCommands, keeping track of which
147   // instructions each case are used for.  It is a comma separated string of
148   // enums.
149   std::vector<std::string> InstrsForCase;
150   InstrsForCase.resize(UniqueOperandCommands.size());
151   InstOpsUsed.assign(UniqueOperandCommands.size(), 0);
152
153   for (unsigned i = 0, e = NumberedInstructions->size(); i != e; ++i) {
154     const AsmWriterInst *Inst = getAsmWriterInstByID(i);
155     if (Inst == 0)
156       continue; // PHI, INLINEASM, CFI_INSTRUCTION, etc.
157
158     std::string Command;
159     if (Inst->Operands.empty())
160       continue;   // Instruction already done.
161
162     Command = "    " + Inst->Operands[0].getCode() + "\n";
163
164     // Check to see if we already have 'Command' in UniqueOperandCommands.
165     // If not, add it.
166     bool FoundIt = false;
167     for (unsigned idx = 0, e = UniqueOperandCommands.size(); idx != e; ++idx)
168       if (UniqueOperandCommands[idx] == Command) {
169         InstIdxs[i] = idx;
170         InstrsForCase[idx] += ", ";
171         InstrsForCase[idx] += Inst->CGI->TheDef->getName();
172         FoundIt = true;
173         break;
174       }
175     if (!FoundIt) {
176       InstIdxs[i] = UniqueOperandCommands.size();
177       UniqueOperandCommands.push_back(Command);
178       InstrsForCase.push_back(Inst->CGI->TheDef->getName());
179
180       // This command matches one operand so far.
181       InstOpsUsed.push_back(1);
182     }
183   }
184
185   // For each entry of UniqueOperandCommands, there is a set of instructions
186   // that uses it.  If the next command of all instructions in the set are
187   // identical, fold it into the command.
188   for (unsigned CommandIdx = 0, e = UniqueOperandCommands.size();
189        CommandIdx != e; ++CommandIdx) {
190
191     for (unsigned Op = 1; ; ++Op) {
192       // Scan for the first instruction in the set.
193       std::vector<unsigned>::iterator NIT =
194         std::find(InstIdxs.begin(), InstIdxs.end(), CommandIdx);
195       if (NIT == InstIdxs.end()) break;  // No commonality.
196
197       // If this instruction has no more operands, we isn't anything to merge
198       // into this command.
199       const AsmWriterInst *FirstInst =
200         getAsmWriterInstByID(NIT-InstIdxs.begin());
201       if (!FirstInst || FirstInst->Operands.size() == Op)
202         break;
203
204       // Otherwise, scan to see if all of the other instructions in this command
205       // set share the operand.
206       bool AllSame = true;
207       // Keep track of the maximum, number of operands or any
208       // instruction we see in the group.
209       size_t MaxSize = FirstInst->Operands.size();
210
211       for (NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx);
212            NIT != InstIdxs.end();
213            NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx)) {
214         // Okay, found another instruction in this command set.  If the operand
215         // matches, we're ok, otherwise bail out.
216         const AsmWriterInst *OtherInst =
217           getAsmWriterInstByID(NIT-InstIdxs.begin());
218
219         if (OtherInst &&
220             OtherInst->Operands.size() > FirstInst->Operands.size())
221           MaxSize = std::max(MaxSize, OtherInst->Operands.size());
222
223         if (!OtherInst || OtherInst->Operands.size() == Op ||
224             OtherInst->Operands[Op] != FirstInst->Operands[Op]) {
225           AllSame = false;
226           break;
227         }
228       }
229       if (!AllSame) break;
230
231       // Okay, everything in this command set has the same next operand.  Add it
232       // to UniqueOperandCommands and remember that it was consumed.
233       std::string Command = "    " + FirstInst->Operands[Op].getCode() + "\n";
234
235       UniqueOperandCommands[CommandIdx] += Command;
236       InstOpsUsed[CommandIdx]++;
237     }
238   }
239
240   // Prepend some of the instructions each case is used for onto the case val.
241   for (unsigned i = 0, e = InstrsForCase.size(); i != e; ++i) {
242     std::string Instrs = InstrsForCase[i];
243     if (Instrs.size() > 70) {
244       Instrs.erase(Instrs.begin()+70, Instrs.end());
245       Instrs += "...";
246     }
247
248     if (!Instrs.empty())
249       UniqueOperandCommands[i] = "    // " + Instrs + "\n" +
250         UniqueOperandCommands[i];
251   }
252 }
253
254
255 static void UnescapeString(std::string &Str) {
256   for (unsigned i = 0; i != Str.size(); ++i) {
257     if (Str[i] == '\\' && i != Str.size()-1) {
258       switch (Str[i+1]) {
259       default: continue;  // Don't execute the code after the switch.
260       case 'a': Str[i] = '\a'; break;
261       case 'b': Str[i] = '\b'; break;
262       case 'e': Str[i] = 27; break;
263       case 'f': Str[i] = '\f'; break;
264       case 'n': Str[i] = '\n'; break;
265       case 'r': Str[i] = '\r'; break;
266       case 't': Str[i] = '\t'; break;
267       case 'v': Str[i] = '\v'; break;
268       case '"': Str[i] = '\"'; break;
269       case '\'': Str[i] = '\''; break;
270       case '\\': Str[i] = '\\'; break;
271       }
272       // Nuke the second character.
273       Str.erase(Str.begin()+i+1);
274     }
275   }
276 }
277
278 /// EmitPrintInstruction - Generate the code for the "printInstruction" method
279 /// implementation. Destroys all instances of AsmWriterInst information, by
280 /// clearing the Instructions vector.
281 void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
282   Record *AsmWriter = Target.getAsmWriter();
283   std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
284
285   O <<
286   "/// printInstruction - This method is automatically generated by tablegen\n"
287   "/// from the instruction set description.\n"
288     "void " << Target.getName() << ClassName
289             << "::printInstruction(const MCInst *MI, raw_ostream &O) {\n";
290
291   // Build an aggregate string, and build a table of offsets into it.
292   SequenceToOffsetTable<std::string> StringTable;
293
294   /// OpcodeInfo - This encodes the index of the string to use for the first
295   /// chunk of the output as well as indices used for operand printing.
296   /// To reduce the number of unhandled cases, we expand the size from 32-bit
297   /// to 32+16 = 48-bit.
298   std::vector<uint64_t> OpcodeInfo;
299
300   // Add all strings to the string table upfront so it can generate an optimized
301   // representation.
302   for (unsigned i = 0, e = NumberedInstructions->size(); i != e; ++i) {
303     AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions->at(i)];
304     if (AWI != 0 &&
305         AWI->Operands[0].OperandType ==
306                  AsmWriterOperand::isLiteralTextOperand &&
307         !AWI->Operands[0].Str.empty()) {
308       std::string Str = AWI->Operands[0].Str;
309       UnescapeString(Str);
310       StringTable.add(Str);
311     }
312   }
313
314   StringTable.layout();
315
316   unsigned MaxStringIdx = 0;
317   for (unsigned i = 0, e = NumberedInstructions->size(); i != e; ++i) {
318     AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions->at(i)];
319     unsigned Idx;
320     if (AWI == 0) {
321       // Something not handled by the asmwriter printer.
322       Idx = ~0U;
323     } else if (AWI->Operands[0].OperandType !=
324                         AsmWriterOperand::isLiteralTextOperand ||
325                AWI->Operands[0].Str.empty()) {
326       // Something handled by the asmwriter printer, but with no leading string.
327       Idx = StringTable.get("");
328     } else {
329       std::string Str = AWI->Operands[0].Str;
330       UnescapeString(Str);
331       Idx = StringTable.get(Str);
332       MaxStringIdx = std::max(MaxStringIdx, Idx);
333
334       // Nuke the string from the operand list.  It is now handled!
335       AWI->Operands.erase(AWI->Operands.begin());
336     }
337
338     // Bias offset by one since we want 0 as a sentinel.
339     OpcodeInfo.push_back(Idx+1);
340   }
341
342   // Figure out how many bits we used for the string index.
343   unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+2);
344
345   // To reduce code size, we compactify common instructions into a few bits
346   // in the opcode-indexed table.
347   unsigned BitsLeft = 64-AsmStrBits;
348
349   std::vector<std::vector<std::string> > TableDrivenOperandPrinters;
350
351   while (1) {
352     std::vector<std::string> UniqueOperandCommands;
353     std::vector<unsigned> InstIdxs;
354     std::vector<unsigned> NumInstOpsHandled;
355     FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
356                               NumInstOpsHandled);
357
358     // If we ran out of operands to print, we're done.
359     if (UniqueOperandCommands.empty()) break;
360
361     // Compute the number of bits we need to represent these cases, this is
362     // ceil(log2(numentries)).
363     unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size());
364
365     // If we don't have enough bits for this operand, don't include it.
366     if (NumBits > BitsLeft) {
367       DEBUG(errs() << "Not enough bits to densely encode " << NumBits
368                    << " more bits\n");
369       break;
370     }
371
372     // Otherwise, we can include this in the initial lookup table.  Add it in.
373     for (unsigned i = 0, e = InstIdxs.size(); i != e; ++i)
374       if (InstIdxs[i] != ~0U) {
375         OpcodeInfo[i] |= (uint64_t)InstIdxs[i] << (64-BitsLeft);
376       }
377     BitsLeft -= NumBits;
378
379     // Remove the info about this operand.
380     for (unsigned i = 0, e = NumberedInstructions->size(); i != e; ++i) {
381       if (AsmWriterInst *Inst = getAsmWriterInstByID(i))
382         if (!Inst->Operands.empty()) {
383           unsigned NumOps = NumInstOpsHandled[InstIdxs[i]];
384           assert(NumOps <= Inst->Operands.size() &&
385                  "Can't remove this many ops!");
386           Inst->Operands.erase(Inst->Operands.begin(),
387                                Inst->Operands.begin()+NumOps);
388         }
389     }
390
391     // Remember the handlers for this set of operands.
392     TableDrivenOperandPrinters.push_back(UniqueOperandCommands);
393   }
394
395
396   // We always emit at least one 32-bit table. A second table is emitted if
397   // more bits are needed.
398   O<<"  static const uint32_t OpInfo[] = {\n";
399   for (unsigned i = 0, e = NumberedInstructions->size(); i != e; ++i) {
400     O << "    " << (OpcodeInfo[i] & 0xffffffff) << "U,\t// "
401       << NumberedInstructions->at(i)->TheDef->getName() << "\n";
402   }
403   // Add a dummy entry so the array init doesn't end with a comma.
404   O << "    0U\n";
405   O << "  };\n\n";
406
407   if (BitsLeft < 32) {
408     // Add a second OpInfo table only when it is necessary.
409     // Adjust the type of the second table based on the number of bits needed.
410     O << "  static const uint"
411       << ((BitsLeft < 16) ? "32" : (BitsLeft < 24) ? "16" : "8")
412       << "_t OpInfo2[] = {\n";
413     for (unsigned i = 0, e = NumberedInstructions->size(); i != e; ++i) {
414       O << "    " << (OpcodeInfo[i] >> 32) << "U,\t// "
415         << NumberedInstructions->at(i)->TheDef->getName() << "\n";
416     }
417     // Add a dummy entry so the array init doesn't end with a comma.
418     O << "    0U\n";
419     O << "  };\n\n";
420   }
421
422   // Emit the string itself.
423   O << "  const char AsmStrs[] = {\n";
424   StringTable.emit(O, printChar);
425   O << "  };\n\n";
426
427   O << "  O << \"\\t\";\n\n";
428
429   O << "  // Emit the opcode for the instruction.\n";
430   if (BitsLeft < 32) {
431     // If we have two tables then we need to perform two lookups and combine
432     // the results into a single 64-bit value.
433     O << "  uint64_t Bits1 = OpInfo[MI->getOpcode()];\n"
434       << "  uint64_t Bits2 = OpInfo2[MI->getOpcode()];\n"
435       << "  uint64_t Bits = (Bits2 << 32) | Bits1;\n";
436   } else {
437     // If only one table is used we just need to perform a single lookup.
438     O << "  uint32_t Bits = OpInfo[MI->getOpcode()];\n";
439   }
440   O << "  assert(Bits != 0 && \"Cannot print this instruction.\");\n"
441     << "  O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n";
442
443   // Output the table driven operand information.
444   BitsLeft = 64-AsmStrBits;
445   for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
446     std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
447
448     // Compute the number of bits we need to represent these cases, this is
449     // ceil(log2(numentries)).
450     unsigned NumBits = Log2_32_Ceil(Commands.size());
451     assert(NumBits <= BitsLeft && "consistency error");
452
453     // Emit code to extract this field from Bits.
454     O << "\n  // Fragment " << i << " encoded into " << NumBits
455       << " bits for " << Commands.size() << " unique commands.\n";
456
457     if (Commands.size() == 2) {
458       // Emit two possibilitys with if/else.
459       O << "  if ((Bits >> "
460         << (64-BitsLeft) << ") & "
461         << ((1 << NumBits)-1) << ") {\n"
462         << Commands[1]
463         << "  } else {\n"
464         << Commands[0]
465         << "  }\n\n";
466     } else if (Commands.size() == 1) {
467       // Emit a single possibility.
468       O << Commands[0] << "\n\n";
469     } else {
470       O << "  switch ((Bits >> "
471         << (64-BitsLeft) << ") & "
472         << ((1 << NumBits)-1) << ") {\n"
473         << "  default:   // unreachable.\n";
474
475       // Print out all the cases.
476       for (unsigned i = 0, e = Commands.size(); i != e; ++i) {
477         O << "  case " << i << ":\n";
478         O << Commands[i];
479         O << "    break;\n";
480       }
481       O << "  }\n\n";
482     }
483     BitsLeft -= NumBits;
484   }
485
486   // Okay, delete instructions with no operand info left.
487   for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
488     // Entire instruction has been emitted?
489     AsmWriterInst &Inst = Instructions[i];
490     if (Inst.Operands.empty()) {
491       Instructions.erase(Instructions.begin()+i);
492       --i; --e;
493     }
494   }
495
496
497   // Because this is a vector, we want to emit from the end.  Reverse all of the
498   // elements in the vector.
499   std::reverse(Instructions.begin(), Instructions.end());
500
501
502   // Now that we've emitted all of the operand info that fit into 32 bits, emit
503   // information for those instructions that are left.  This is a less dense
504   // encoding, but we expect the main 32-bit table to handle the majority of
505   // instructions.
506   if (!Instructions.empty()) {
507     // Find the opcode # of inline asm.
508     O << "  switch (MI->getOpcode()) {\n";
509     while (!Instructions.empty())
510       EmitInstructions(Instructions, O);
511
512     O << "  }\n";
513     O << "  return;\n";
514   }
515
516   O << "}\n";
517 }
518
519 static void
520 emitRegisterNameString(raw_ostream &O, StringRef AltName,
521                        const std::vector<CodeGenRegister*> &Registers) {
522   SequenceToOffsetTable<std::string> StringTable;
523   SmallVector<std::string, 4> AsmNames(Registers.size());
524   for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
525     const CodeGenRegister &Reg = *Registers[i];
526     std::string &AsmName = AsmNames[i];
527
528     // "NoRegAltName" is special. We don't need to do a lookup for that,
529     // as it's just a reference to the default register name.
530     if (AltName == "" || AltName == "NoRegAltName") {
531       AsmName = Reg.TheDef->getValueAsString("AsmName");
532       if (AsmName.empty())
533         AsmName = Reg.getName();
534     } else {
535       // Make sure the register has an alternate name for this index.
536       std::vector<Record*> AltNameList =
537         Reg.TheDef->getValueAsListOfDefs("RegAltNameIndices");
538       unsigned Idx = 0, e;
539       for (e = AltNameList.size();
540            Idx < e && (AltNameList[Idx]->getName() != AltName);
541            ++Idx)
542         ;
543       // If the register has an alternate name for this index, use it.
544       // Otherwise, leave it empty as an error flag.
545       if (Idx < e) {
546         std::vector<std::string> AltNames =
547           Reg.TheDef->getValueAsListOfStrings("AltNames");
548         if (AltNames.size() <= Idx)
549           PrintFatalError(Reg.TheDef->getLoc(),
550                           "Register definition missing alt name for '" +
551                           AltName + "'.");
552         AsmName = AltNames[Idx];
553       }
554     }
555     StringTable.add(AsmName);
556   }
557
558   StringTable.layout();
559   O << "  static const char AsmStrs" << AltName << "[] = {\n";
560   StringTable.emit(O, printChar);
561   O << "  };\n\n";
562
563   O << "  static const uint32_t RegAsmOffset" << AltName << "[] = {";
564   for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
565     if ((i % 14) == 0)
566       O << "\n    ";
567     O << StringTable.get(AsmNames[i]) << ", ";
568   }
569   O << "\n  };\n"
570     << "\n";
571 }
572
573 void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
574   Record *AsmWriter = Target.getAsmWriter();
575   std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
576   const std::vector<CodeGenRegister*> &Registers =
577     Target.getRegBank().getRegisters();
578   std::vector<Record*> AltNameIndices = Target.getRegAltNameIndices();
579   bool hasAltNames = AltNameIndices.size() > 1;
580
581   O <<
582   "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
583   "/// from the register set description.  This returns the assembler name\n"
584   "/// for the specified register.\n"
585   "const char *" << Target.getName() << ClassName << "::";
586   if (hasAltNames)
587     O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx) {\n";
588   else
589     O << "getRegisterName(unsigned RegNo) {\n";
590   O << "  assert(RegNo && RegNo < " << (Registers.size()+1)
591     << " && \"Invalid register number!\");\n"
592     << "\n";
593
594   if (hasAltNames) {
595     for (unsigned i = 0, e = AltNameIndices.size(); i < e; ++i)
596       emitRegisterNameString(O, AltNameIndices[i]->getName(), Registers);
597   } else
598     emitRegisterNameString(O, "", Registers);
599
600   if (hasAltNames) {
601     O << "  const uint32_t *RegAsmOffset;\n"
602       << "  const char *AsmStrs;\n"
603       << "  switch(AltIdx) {\n"
604       << "  default: llvm_unreachable(\"Invalid register alt name index!\");\n";
605     for (unsigned i = 0, e = AltNameIndices.size(); i < e; ++i) {
606       std::string Namespace = AltNameIndices[1]->getValueAsString("Namespace");
607       std::string AltName(AltNameIndices[i]->getName());
608       O << "  case " << Namespace << "::" << AltName
609         << ":\n"
610         << "    AsmStrs = AsmStrs" << AltName  << ";\n"
611         << "    RegAsmOffset = RegAsmOffset" << AltName << ";\n"
612         << "    break;\n";
613     }
614     O << "}\n";
615   }
616
617   O << "  assert (*(AsmStrs+RegAsmOffset[RegNo-1]) &&\n"
618     << "          \"Invalid alt name index for register!\");\n"
619     << "  return AsmStrs+RegAsmOffset[RegNo-1];\n"
620     << "}\n";
621 }
622
623 namespace {
624 // IAPrinter - Holds information about an InstAlias. Two InstAliases match if
625 // they both have the same conditionals. In which case, we cannot print out the
626 // alias for that pattern.
627 class IAPrinter {
628   std::vector<std::string> Conds;
629   std::map<StringRef, unsigned> OpMap;
630   std::string Result;
631   std::string AsmString;
632   SmallVector<Record*, 4> ReqFeatures;
633 public:
634   IAPrinter(std::string R, std::string AS)
635     : Result(R), AsmString(AS) {}
636
637   void addCond(const std::string &C) { Conds.push_back(C); }
638
639   void addOperand(StringRef Op, unsigned Idx) {
640     assert(Idx < 0xFF && "Index too large!");
641     OpMap[Op] = Idx;
642   }
643   unsigned getOpIndex(StringRef Op) { return OpMap[Op]; }
644   bool isOpMapped(StringRef Op) { return OpMap.find(Op) != OpMap.end(); }
645
646   void print(raw_ostream &O) {
647     if (Conds.empty() && ReqFeatures.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     std::pair<StringRef, StringRef> ASM = StringRef(AsmString).split(' ');
671     SmallString<128> OutString = ASM.first;
672     if (!ASM.second.empty()) {
673       raw_svector_ostream OS(OutString);
674       OS << ' ';
675       for (StringRef::iterator I = ASM.second.begin(), E = ASM.second.end();
676            I != E;) {
677         OS << *I;
678         if (*I == '$') {
679           StringRef::iterator Start = ++I;
680           while (I != E &&
681                  ((*I >= 'a' && *I <= 'z') || (*I >= 'A' && *I <= 'Z') ||
682                   (*I >= '0' && *I <= '9') || *I == '_'))
683             ++I;
684           StringRef Name(Start, I - Start);
685           assert(isOpMapped(Name) && "Unmapped operand!");
686           OS << format("\\x%02X", (unsigned char)getOpIndex(Name) + 1);
687         } else {
688           ++I;
689         }
690       }
691     }
692
693     // Emit the string.
694     O.indent(6) << "AsmString = \"" << OutString.str() << "\";\n";
695
696     O.indent(6) << "break;\n";
697     O.indent(4) << '}';
698   }
699
700   bool operator==(const IAPrinter &RHS) {
701     if (Conds.size() != RHS.Conds.size())
702       return false;
703
704     unsigned Idx = 0;
705     for (std::vector<std::string>::iterator
706            I = Conds.begin(), E = Conds.end(); I != E; ++I)
707       if (*I != RHS.Conds[Idx++])
708         return false;
709
710     return true;
711   }
712
713   bool operator()(const IAPrinter &RHS) {
714     if (Conds.size() < RHS.Conds.size())
715       return true;
716
717     unsigned Idx = 0;
718     for (std::vector<std::string>::iterator
719            I = Conds.begin(), E = Conds.end(); I != E; ++I)
720       if (*I != RHS.Conds[Idx++])
721         return *I < RHS.Conds[Idx++];
722
723     return false;
724   }
725 };
726
727 } // end anonymous namespace
728
729 static unsigned CountNumOperands(StringRef AsmString) {
730   unsigned NumOps = 0;
731   std::pair<StringRef, StringRef> ASM = AsmString.split(' ');
732
733   while (!ASM.second.empty()) {
734     ++NumOps;
735     ASM = ASM.second.split(' ');
736   }
737
738   return NumOps;
739 }
740
741 static unsigned CountResultNumOperands(StringRef AsmString) {
742   unsigned NumOps = 0;
743   std::pair<StringRef, StringRef> ASM = AsmString.split('\t');
744
745   if (!ASM.second.empty()) {
746     size_t I = ASM.second.find('{');
747     StringRef Str = ASM.second;
748     if (I != StringRef::npos)
749       Str = ASM.second.substr(I, ASM.second.find('|', I));
750
751     ASM = Str.split(' ');
752
753     do {
754       ++NumOps;
755       ASM = ASM.second.split(' ');
756     } while (!ASM.second.empty());
757   }
758
759   return NumOps;
760 }
761
762 void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
763   Record *AsmWriter = Target.getAsmWriter();
764
765   O << "\n#ifdef PRINT_ALIAS_INSTR\n";
766   O << "#undef PRINT_ALIAS_INSTR\n\n";
767
768   // Emit the method that prints the alias instruction.
769   std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
770
771   std::vector<Record*> AllInstAliases =
772     Records.getAllDerivedDefinitions("InstAlias");
773
774   // Create a map from the qualified name to a list of potential matches.
775   std::map<std::string, std::vector<CodeGenInstAlias*> > AliasMap;
776   for (std::vector<Record*>::iterator
777          I = AllInstAliases.begin(), E = AllInstAliases.end(); I != E; ++I) {
778     CodeGenInstAlias *Alias = new CodeGenInstAlias(*I, Target);
779     const Record *R = *I;
780     if (!R->getValueAsBit("EmitAlias"))
781       continue; // We were told not to emit the alias, but to emit the aliasee.
782     const DagInit *DI = R->getValueAsDag("ResultInst");
783     const DefInit *Op = cast<DefInit>(DI->getOperator());
784     AliasMap[getQualifiedName(Op->getDef())].push_back(Alias);
785   }
786
787   // A map of which conditions need to be met for each instruction operand
788   // before it can be matched to the mnemonic.
789   std::map<std::string, std::vector<IAPrinter*> > IAPrinterMap;
790
791   for (std::map<std::string, std::vector<CodeGenInstAlias*> >::iterator
792          I = AliasMap.begin(), E = AliasMap.end(); I != E; ++I) {
793     std::vector<CodeGenInstAlias*> &Aliases = I->second;
794
795     for (std::vector<CodeGenInstAlias*>::iterator
796            II = Aliases.begin(), IE = Aliases.end(); II != IE; ++II) {
797       const CodeGenInstAlias *CGA = *II;
798       unsigned LastOpNo = CGA->ResultInstOperandIndex.size();
799       unsigned NumResultOps =
800         CountResultNumOperands(CGA->ResultInst->AsmString);
801
802       // Don't emit the alias if it has more operands than what it's aliasing.
803       if (NumResultOps < CountNumOperands(CGA->AsmString))
804         continue;
805
806       IAPrinter *IAP = new IAPrinter(CGA->Result->getAsString(),
807                                      CGA->AsmString);
808
809       std::string Cond;
810       Cond = std::string("MI->getNumOperands() == ") + llvm::utostr(LastOpNo);
811       IAP->addCond(Cond);
812
813       bool CantHandle = false;
814
815       for (unsigned i = 0, e = LastOpNo; i != e; ++i) {
816         const CodeGenInstAlias::ResultOperand &RO = CGA->ResultOperands[i];
817
818         switch (RO.Kind) {
819         case CodeGenInstAlias::ResultOperand::K_Record: {
820           const Record *Rec = RO.getRecord();
821           StringRef ROName = RO.getName();
822
823
824           if (Rec->isSubClassOf("RegisterOperand"))
825             Rec = Rec->getValueAsDef("RegClass");
826           if (Rec->isSubClassOf("RegisterClass")) {
827             Cond = std::string("MI->getOperand(")+llvm::utostr(i)+").isReg()";
828             IAP->addCond(Cond);
829
830             if (!IAP->isOpMapped(ROName)) {
831               IAP->addOperand(ROName, i);
832               Record *R = CGA->ResultOperands[i].getRecord();
833               if (R->isSubClassOf("RegisterOperand"))
834                 R = R->getValueAsDef("RegClass");
835               Cond = std::string("MRI.getRegClass(") + Target.getName() + "::" +
836                 R->getName() + "RegClassID)"
837                 ".contains(MI->getOperand(" + llvm::utostr(i) + ").getReg())";
838               IAP->addCond(Cond);
839             } else {
840               Cond = std::string("MI->getOperand(") +
841                 llvm::utostr(i) + ").getReg() == MI->getOperand(" +
842                 llvm::utostr(IAP->getOpIndex(ROName)) + ").getReg()";
843               IAP->addCond(Cond);
844             }
845           } else {
846             assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
847             // FIXME: We may need to handle these situations.
848             delete IAP;
849             IAP = 0;
850             CantHandle = true;
851             break;
852           }
853
854           break;
855         }
856         case CodeGenInstAlias::ResultOperand::K_Imm: {
857           std::string Op = "MI->getOperand(" + llvm::utostr(i) + ")";
858
859           // Just because the alias has an immediate result, doesn't mean the
860           // MCInst will. An MCExpr could be present, for example.
861           IAP->addCond(Op + ".isImm()");
862
863           Cond = Op + ".getImm() == "
864             + llvm::utostr(CGA->ResultOperands[i].getImm());
865           IAP->addCond(Cond);
866           break;
867         }
868         case CodeGenInstAlias::ResultOperand::K_Reg:
869           // If this is zero_reg, something's playing tricks we're not
870           // equipped to handle.
871           if (!CGA->ResultOperands[i].getRegister()) {
872             CantHandle = true;
873             break;
874           }
875
876           Cond = std::string("MI->getOperand(") +
877             llvm::utostr(i) + ").getReg() == " + Target.getName() +
878             "::" + CGA->ResultOperands[i].getRegister()->getName();
879           IAP->addCond(Cond);
880           break;
881         }
882
883         if (!IAP) break;
884       }
885
886       if (CantHandle) continue;
887       IAPrinterMap[I->first].push_back(IAP);
888     }
889   }
890
891   std::string Header;
892   raw_string_ostream HeaderO(Header);
893
894   HeaderO << "bool " << Target.getName() << ClassName
895           << "::printAliasInstr(const MCInst"
896           << " *MI, raw_ostream &OS) {\n";
897
898   std::string Cases;
899   raw_string_ostream CasesO(Cases);
900
901   for (std::map<std::string, std::vector<IAPrinter*> >::iterator
902          I = IAPrinterMap.begin(), E = IAPrinterMap.end(); I != E; ++I) {
903     std::vector<IAPrinter*> &IAPs = I->second;
904     std::vector<IAPrinter*> UniqueIAPs;
905
906     for (std::vector<IAPrinter*>::iterator
907            II = IAPs.begin(), IE = IAPs.end(); II != IE; ++II) {
908       IAPrinter *LHS = *II;
909       bool IsDup = false;
910       for (std::vector<IAPrinter*>::iterator
911              III = IAPs.begin(), IIE = IAPs.end(); III != IIE; ++III) {
912         IAPrinter *RHS = *III;
913         if (LHS != RHS && *LHS == *RHS) {
914           IsDup = true;
915           break;
916         }
917       }
918
919       if (!IsDup) UniqueIAPs.push_back(LHS);
920     }
921
922     if (UniqueIAPs.empty()) continue;
923
924     CasesO.indent(2) << "case " << I->first << ":\n";
925
926     for (std::vector<IAPrinter*>::iterator
927            II = UniqueIAPs.begin(), IE = UniqueIAPs.end(); II != IE; ++II) {
928       IAPrinter *IAP = *II;
929       CasesO.indent(4);
930       IAP->print(CasesO);
931       CasesO << '\n';
932     }
933
934     CasesO.indent(4) << "return false;\n";
935   }
936
937   if (CasesO.str().empty()) {
938     O << HeaderO.str();
939     O << "  return false;\n";
940     O << "}\n\n";
941     O << "#endif // PRINT_ALIAS_INSTR\n";
942     return;
943   }
944
945   O << HeaderO.str();
946   O.indent(2) << "const char *AsmString;\n";
947   O.indent(2) << "switch (MI->getOpcode()) {\n";
948   O.indent(2) << "default: return false;\n";
949   O << CasesO.str();
950   O.indent(2) << "}\n\n";
951
952   // Code that prints the alias, replacing the operands with the ones from the
953   // MCInst.
954   O << "  unsigned I = 0;\n";
955   O << "  while (AsmString[I] != ' ' && AsmString[I] != '\\0')\n";
956   O << "    ++I;\n";
957   O << "  OS << '\\t' << StringRef(AsmString, I);\n";
958
959   O << "  if (AsmString[I] != '\\0') {\n";
960   O << "    OS << '\\t';\n";
961   O << "    do {\n";
962   O << "      if (AsmString[I] == '$') {\n";
963   O << "        ++I;\n";
964   O << "        printOperand(MI, unsigned(AsmString[I++]) - 1, OS);\n";
965   O << "      } else {\n";
966   O << "        OS << AsmString[I++];\n";
967   O << "      }\n";
968   O << "    } while (AsmString[I] != '\\0');\n";
969   O << "  }\n\n";
970
971   O << "  return true;\n";
972   O << "}\n\n";
973
974   O << "#endif // PRINT_ALIAS_INSTR\n";
975 }
976
977 AsmWriterEmitter::AsmWriterEmitter(RecordKeeper &R) : Records(R), Target(R) {
978   Record *AsmWriter = Target.getAsmWriter();
979   for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
980                                     E = Target.inst_end();
981        I != E; ++I)
982     if (!(*I)->AsmString.empty() && (*I)->TheDef->getName() != "PHI")
983       Instructions.push_back(
984           AsmWriterInst(**I, AsmWriter->getValueAsInt("Variant"),
985                         AsmWriter->getValueAsInt("OperandSpacing")));
986
987   // Get the instruction numbering.
988   NumberedInstructions = &Target.getInstructionsByEnumValue();
989
990   // Compute the CodeGenInstruction -> AsmWriterInst mapping.  Note that not
991   // all machine instructions are necessarily being printed, so there may be
992   // target instructions not in this map.
993   for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
994     CGIAWIMap.insert(std::make_pair(Instructions[i].CGI, &Instructions[i]));
995 }
996
997 void AsmWriterEmitter::run(raw_ostream &O) {
998   EmitPrintInstruction(O);
999   EmitGetRegisterName(O);
1000   EmitPrintAliasInstruction(O);
1001 }
1002
1003
1004 namespace llvm {
1005
1006 void EmitAsmWriter(RecordKeeper &RK, raw_ostream &OS) {
1007   emitSourceFileHeader("Assembly Writer Source Fragment", OS);
1008   AsmWriterEmitter(RK).run(OS);
1009 }
1010
1011 } // End llvm namespace