xref: /freebsd/contrib/llvm-project/llvm/utils/TableGen/AsmMatcherEmitter.cpp (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
1 //===- AsmMatcherEmitter.cpp - Generate an assembly matcher ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This tablegen backend emits a target specifier matcher for converting parsed
10 // assembly operands in the MCInst structures. It also emits a matcher for
11 // custom operand parsing.
12 //
13 // Converting assembly operands into MCInst structures
14 // ---------------------------------------------------
15 //
16 // The input to the target specific matcher is a list of literal tokens and
17 // operands. The target specific parser should generally eliminate any syntax
18 // which is not relevant for matching; for example, comma tokens should have
19 // already been consumed and eliminated by the parser. Most instructions will
20 // end up with a single literal token (the instruction name) and some number of
21 // operands.
22 //
23 // Some example inputs, for X86:
24 //   'addl' (immediate ...) (register ...)
25 //   'add' (immediate ...) (memory ...)
26 //   'call' '*' %epc
27 //
28 // The assembly matcher is responsible for converting this input into a precise
29 // machine instruction (i.e., an instruction with a well defined encoding). This
30 // mapping has several properties which complicate matching:
31 //
32 //  - It may be ambiguous; many architectures can legally encode particular
33 //    variants of an instruction in different ways (for example, using a smaller
34 //    encoding for small immediates). Such ambiguities should never be
35 //    arbitrarily resolved by the assembler, the assembler is always responsible
36 //    for choosing the "best" available instruction.
37 //
38 //  - It may depend on the subtarget or the assembler context. Instructions
39 //    which are invalid for the current mode, but otherwise unambiguous (e.g.,
40 //    an SSE instruction in a file being assembled for i486) should be accepted
41 //    and rejected by the assembler front end. However, if the proper encoding
42 //    for an instruction is dependent on the assembler context then the matcher
43 //    is responsible for selecting the correct machine instruction for the
44 //    current mode.
45 //
46 // The core matching algorithm attempts to exploit the regularity in most
47 // instruction sets to quickly determine the set of possibly matching
48 // instructions, and the simplify the generated code. Additionally, this helps
49 // to ensure that the ambiguities are intentionally resolved by the user.
50 //
51 // The matching is divided into two distinct phases:
52 //
53 //   1. Classification: Each operand is mapped to the unique set which (a)
54 //      contains it, and (b) is the largest such subset for which a single
55 //      instruction could match all members.
56 //
57 //      For register classes, we can generate these subgroups automatically. For
58 //      arbitrary operands, we expect the user to define the classes and their
59 //      relations to one another (for example, 8-bit signed immediates as a
60 //      subset of 32-bit immediates).
61 //
62 //      By partitioning the operands in this way, we guarantee that for any
63 //      tuple of classes, any single instruction must match either all or none
64 //      of the sets of operands which could classify to that tuple.
65 //
66 //      In addition, the subset relation amongst classes induces a partial order
67 //      on such tuples, which we use to resolve ambiguities.
68 //
69 //   2. The input can now be treated as a tuple of classes (static tokens are
70 //      simple singleton sets). Each such tuple should generally map to a single
71 //      instruction (we currently ignore cases where this isn't true, whee!!!),
72 //      which we can emit a simple matcher for.
73 //
74 // Custom Operand Parsing
75 // ----------------------
76 //
77 //  Some targets need a custom way to parse operands, some specific instructions
78 //  can contain arguments that can represent processor flags and other kinds of
79 //  identifiers that need to be mapped to specific values in the final encoded
80 //  instructions. The target specific custom operand parsing works in the
81 //  following way:
82 //
83 //   1. A operand match table is built, each entry contains a mnemonic, an
84 //      operand class, a mask for all operand positions for that same
85 //      class/mnemonic and target features to be checked while trying to match.
86 //
87 //   2. The operand matcher will try every possible entry with the same
88 //      mnemonic and will check if the target feature for this mnemonic also
89 //      matches. After that, if the operand to be matched has its index
90 //      present in the mask, a successful match occurs. Otherwise, fallback
91 //      to the regular operand parsing.
92 //
93 //   3. For a match success, each operand class that has a 'ParserMethod'
94 //      becomes part of a switch from where the custom method is called.
95 //
96 //===----------------------------------------------------------------------===//
97 
98 #include "CodeGenInstAlias.h"
99 #include "CodeGenInstruction.h"
100 #include "CodeGenRegisters.h"
101 #include "CodeGenTarget.h"
102 #include "SubtargetFeatureInfo.h"
103 #include "Types.h"
104 #include "llvm/ADT/CachedHashString.h"
105 #include "llvm/ADT/PointerUnion.h"
106 #include "llvm/ADT/STLExtras.h"
107 #include "llvm/ADT/SmallPtrSet.h"
108 #include "llvm/ADT/SmallVector.h"
109 #include "llvm/ADT/StringExtras.h"
110 #include "llvm/Support/CommandLine.h"
111 #include "llvm/Support/Debug.h"
112 #include "llvm/Support/ErrorHandling.h"
113 #include "llvm/TableGen/Error.h"
114 #include "llvm/TableGen/Record.h"
115 #include "llvm/TableGen/StringMatcher.h"
116 #include "llvm/TableGen/StringToOffsetTable.h"
117 #include "llvm/TableGen/TableGenBackend.h"
118 #include <cassert>
119 #include <cctype>
120 #include <forward_list>
121 #include <map>
122 #include <set>
123 
124 using namespace llvm;
125 
126 #define DEBUG_TYPE "asm-matcher-emitter"
127 
128 cl::OptionCategory AsmMatcherEmitterCat("Options for -gen-asm-matcher");
129 
130 static cl::opt<std::string>
131     MatchPrefix("match-prefix", cl::init(""),
132                 cl::desc("Only match instructions with the given prefix"),
133                 cl::cat(AsmMatcherEmitterCat));
134 
135 namespace {
136 class AsmMatcherInfo;
137 
138 // Register sets are used as keys in some second-order sets TableGen creates
139 // when generating its data structures. This means that the order of two
140 // RegisterSets can be seen in the outputted AsmMatcher tables occasionally, and
141 // can even affect compiler output (at least seen in diagnostics produced when
142 // all matches fail). So we use a type that sorts them consistently.
143 typedef std::set<Record*, LessRecordByID> RegisterSet;
144 
145 class AsmMatcherEmitter {
146   RecordKeeper &Records;
147 public:
148   AsmMatcherEmitter(RecordKeeper &R) : Records(R) {}
149 
150   void run(raw_ostream &o);
151 };
152 
153 /// ClassInfo - Helper class for storing the information about a particular
154 /// class of operands which can be matched.
155 struct ClassInfo {
156   enum ClassInfoKind {
157     /// Invalid kind, for use as a sentinel value.
158     Invalid = 0,
159 
160     /// The class for a particular token.
161     Token,
162 
163     /// The (first) register class, subsequent register classes are
164     /// RegisterClass0+1, and so on.
165     RegisterClass0,
166 
167     /// The (first) user defined class, subsequent user defined classes are
168     /// UserClass0+1, and so on.
169     UserClass0 = 1<<16
170   };
171 
172   /// Kind - The class kind, which is either a predefined kind, or (UserClass0 +
173   /// N) for the Nth user defined class.
174   unsigned Kind;
175 
176   /// SuperClasses - The super classes of this class. Note that for simplicities
177   /// sake user operands only record their immediate super class, while register
178   /// operands include all superclasses.
179   std::vector<ClassInfo*> SuperClasses;
180 
181   /// Name - The full class name, suitable for use in an enum.
182   std::string Name;
183 
184   /// ClassName - The unadorned generic name for this class (e.g., Token).
185   std::string ClassName;
186 
187   /// ValueName - The name of the value this class represents; for a token this
188   /// is the literal token string, for an operand it is the TableGen class (or
189   /// empty if this is a derived class).
190   std::string ValueName;
191 
192   /// PredicateMethod - The name of the operand method to test whether the
193   /// operand matches this class; this is not valid for Token or register kinds.
194   std::string PredicateMethod;
195 
196   /// RenderMethod - The name of the operand method to add this operand to an
197   /// MCInst; this is not valid for Token or register kinds.
198   std::string RenderMethod;
199 
200   /// ParserMethod - The name of the operand method to do a target specific
201   /// parsing on the operand.
202   std::string ParserMethod;
203 
204   /// For register classes: the records for all the registers in this class.
205   RegisterSet Registers;
206 
207   /// For custom match classes: the diagnostic kind for when the predicate fails.
208   std::string DiagnosticType;
209 
210   /// For custom match classes: the diagnostic string for when the predicate fails.
211   std::string DiagnosticString;
212 
213   /// Is this operand optional and not always required.
214   bool IsOptional;
215 
216   /// DefaultMethod - The name of the method that returns the default operand
217   /// for optional operand
218   std::string DefaultMethod;
219 
220 public:
221   /// isRegisterClass() - Check if this is a register class.
222   bool isRegisterClass() const {
223     return Kind >= RegisterClass0 && Kind < UserClass0;
224   }
225 
226   /// isUserClass() - Check if this is a user defined class.
227   bool isUserClass() const {
228     return Kind >= UserClass0;
229   }
230 
231   /// isRelatedTo - Check whether this class is "related" to \p RHS. Classes
232   /// are related if they are in the same class hierarchy.
233   bool isRelatedTo(const ClassInfo &RHS) const {
234     // Tokens are only related to tokens.
235     if (Kind == Token || RHS.Kind == Token)
236       return Kind == Token && RHS.Kind == Token;
237 
238     // Registers classes are only related to registers classes, and only if
239     // their intersection is non-empty.
240     if (isRegisterClass() || RHS.isRegisterClass()) {
241       if (!isRegisterClass() || !RHS.isRegisterClass())
242         return false;
243 
244       RegisterSet Tmp;
245       std::insert_iterator<RegisterSet> II(Tmp, Tmp.begin());
246       std::set_intersection(Registers.begin(), Registers.end(),
247                             RHS.Registers.begin(), RHS.Registers.end(),
248                             II, LessRecordByID());
249 
250       return !Tmp.empty();
251     }
252 
253     // Otherwise we have two users operands; they are related if they are in the
254     // same class hierarchy.
255     //
256     // FIXME: This is an oversimplification, they should only be related if they
257     // intersect, however we don't have that information.
258     assert(isUserClass() && RHS.isUserClass() && "Unexpected class!");
259     const ClassInfo *Root = this;
260     while (!Root->SuperClasses.empty())
261       Root = Root->SuperClasses.front();
262 
263     const ClassInfo *RHSRoot = &RHS;
264     while (!RHSRoot->SuperClasses.empty())
265       RHSRoot = RHSRoot->SuperClasses.front();
266 
267     return Root == RHSRoot;
268   }
269 
270   /// isSubsetOf - Test whether this class is a subset of \p RHS.
271   bool isSubsetOf(const ClassInfo &RHS) const {
272     // This is a subset of RHS if it is the same class...
273     if (this == &RHS)
274       return true;
275 
276     // ... or if any of its super classes are a subset of RHS.
277     SmallVector<const ClassInfo *, 16> Worklist(SuperClasses.begin(),
278                                                 SuperClasses.end());
279     SmallPtrSet<const ClassInfo *, 16> Visited;
280     while (!Worklist.empty()) {
281       auto *CI = Worklist.pop_back_val();
282       if (CI == &RHS)
283         return true;
284       for (auto *Super : CI->SuperClasses)
285         if (Visited.insert(Super).second)
286           Worklist.push_back(Super);
287     }
288 
289     return false;
290   }
291 
292   int getTreeDepth() const {
293     int Depth = 0;
294     const ClassInfo *Root = this;
295     while (!Root->SuperClasses.empty()) {
296       Depth++;
297       Root = Root->SuperClasses.front();
298     }
299     return Depth;
300   }
301 
302   const ClassInfo *findRoot() const {
303     const ClassInfo *Root = this;
304     while (!Root->SuperClasses.empty())
305       Root = Root->SuperClasses.front();
306     return Root;
307   }
308 
309   /// Compare two classes. This does not produce a total ordering, but does
310   /// guarantee that subclasses are sorted before their parents, and that the
311   /// ordering is transitive.
312   bool operator<(const ClassInfo &RHS) const {
313     if (this == &RHS)
314       return false;
315 
316     // First, enforce the ordering between the three different types of class.
317     // Tokens sort before registers, which sort before user classes.
318     if (Kind == Token) {
319       if (RHS.Kind != Token)
320         return true;
321       assert(RHS.Kind == Token);
322     } else if (isRegisterClass()) {
323       if (RHS.Kind == Token)
324         return false;
325       else if (RHS.isUserClass())
326         return true;
327       assert(RHS.isRegisterClass());
328     } else if (isUserClass()) {
329       if (!RHS.isUserClass())
330         return false;
331       assert(RHS.isUserClass());
332     } else {
333       llvm_unreachable("Unknown ClassInfoKind");
334     }
335 
336     if (Kind == Token || isUserClass()) {
337       // Related tokens and user classes get sorted by depth in the inheritence
338       // tree (so that subclasses are before their parents).
339       if (isRelatedTo(RHS)) {
340         if (getTreeDepth() > RHS.getTreeDepth())
341           return true;
342         if (getTreeDepth() < RHS.getTreeDepth())
343           return false;
344       } else {
345         // Unrelated tokens and user classes are ordered by the name of their
346         // root nodes, so that there is a consistent ordering between
347         // unconnected trees.
348         return findRoot()->ValueName < RHS.findRoot()->ValueName;
349       }
350     } else if (isRegisterClass()) {
351       // For register sets, sort by number of registers. This guarantees that
352       // a set will always sort before all of it's strict supersets.
353       if (Registers.size() != RHS.Registers.size())
354         return Registers.size() < RHS.Registers.size();
355     } else {
356       llvm_unreachable("Unknown ClassInfoKind");
357     }
358 
359     // FIXME: We should be able to just return false here, as we only need a
360     // partial order (we use stable sorts, so this is deterministic) and the
361     // name of a class shouldn't be significant. However, some of the backends
362     // accidentally rely on this behaviour, so it will have to stay like this
363     // until they are fixed.
364     return ValueName < RHS.ValueName;
365   }
366 };
367 
368 class AsmVariantInfo {
369 public:
370   StringRef RegisterPrefix;
371   StringRef TokenizingCharacters;
372   StringRef SeparatorCharacters;
373   StringRef BreakCharacters;
374   StringRef Name;
375   int AsmVariantNo;
376 };
377 
378 /// MatchableInfo - Helper class for storing the necessary information for an
379 /// instruction or alias which is capable of being matched.
380 struct MatchableInfo {
381   struct AsmOperand {
382     /// Token - This is the token that the operand came from.
383     StringRef Token;
384 
385     /// The unique class instance this operand should match.
386     ClassInfo *Class;
387 
388     /// The operand name this is, if anything.
389     StringRef SrcOpName;
390 
391     /// The operand name this is, before renaming for tied operands.
392     StringRef OrigSrcOpName;
393 
394     /// The suboperand index within SrcOpName, or -1 for the entire operand.
395     int SubOpIdx;
396 
397     /// Whether the token is "isolated", i.e., it is preceded and followed
398     /// by separators.
399     bool IsIsolatedToken;
400 
401     /// Register record if this token is singleton register.
402     Record *SingletonReg;
403 
404     explicit AsmOperand(bool IsIsolatedToken, StringRef T)
405         : Token(T), Class(nullptr), SubOpIdx(-1),
406           IsIsolatedToken(IsIsolatedToken), SingletonReg(nullptr) {}
407   };
408 
409   /// ResOperand - This represents a single operand in the result instruction
410   /// generated by the match.  In cases (like addressing modes) where a single
411   /// assembler operand expands to multiple MCOperands, this represents the
412   /// single assembler operand, not the MCOperand.
413   struct ResOperand {
414     enum {
415       /// RenderAsmOperand - This represents an operand result that is
416       /// generated by calling the render method on the assembly operand.  The
417       /// corresponding AsmOperand is specified by AsmOperandNum.
418       RenderAsmOperand,
419 
420       /// TiedOperand - This represents a result operand that is a duplicate of
421       /// a previous result operand.
422       TiedOperand,
423 
424       /// ImmOperand - This represents an immediate value that is dumped into
425       /// the operand.
426       ImmOperand,
427 
428       /// RegOperand - This represents a fixed register that is dumped in.
429       RegOperand
430     } Kind;
431 
432     /// Tuple containing the index of the (earlier) result operand that should
433     /// be copied from, as well as the indices of the corresponding (parsed)
434     /// operands in the asm string.
435     struct TiedOperandsTuple {
436       unsigned ResOpnd;
437       unsigned SrcOpnd1Idx;
438       unsigned SrcOpnd2Idx;
439     };
440 
441     union {
442       /// This is the operand # in the AsmOperands list that this should be
443       /// copied from.
444       unsigned AsmOperandNum;
445 
446       /// Description of tied operands.
447       TiedOperandsTuple TiedOperands;
448 
449       /// ImmVal - This is the immediate value added to the instruction.
450       int64_t ImmVal;
451 
452       /// Register - This is the register record.
453       Record *Register;
454     };
455 
456     /// MINumOperands - The number of MCInst operands populated by this
457     /// operand.
458     unsigned MINumOperands;
459 
460     static ResOperand getRenderedOp(unsigned AsmOpNum, unsigned NumOperands) {
461       ResOperand X;
462       X.Kind = RenderAsmOperand;
463       X.AsmOperandNum = AsmOpNum;
464       X.MINumOperands = NumOperands;
465       return X;
466     }
467 
468     static ResOperand getTiedOp(unsigned TiedOperandNum, unsigned SrcOperand1,
469                                 unsigned SrcOperand2) {
470       ResOperand X;
471       X.Kind = TiedOperand;
472       X.TiedOperands = { TiedOperandNum, SrcOperand1, SrcOperand2 };
473       X.MINumOperands = 1;
474       return X;
475     }
476 
477     static ResOperand getImmOp(int64_t Val) {
478       ResOperand X;
479       X.Kind = ImmOperand;
480       X.ImmVal = Val;
481       X.MINumOperands = 1;
482       return X;
483     }
484 
485     static ResOperand getRegOp(Record *Reg) {
486       ResOperand X;
487       X.Kind = RegOperand;
488       X.Register = Reg;
489       X.MINumOperands = 1;
490       return X;
491     }
492   };
493 
494   /// AsmVariantID - Target's assembly syntax variant no.
495   int AsmVariantID;
496 
497   /// AsmString - The assembly string for this instruction (with variants
498   /// removed), e.g. "movsx $src, $dst".
499   std::string AsmString;
500 
501   /// TheDef - This is the definition of the instruction or InstAlias that this
502   /// matchable came from.
503   Record *const TheDef;
504 
505   /// DefRec - This is the definition that it came from.
506   PointerUnion<const CodeGenInstruction*, const CodeGenInstAlias*> DefRec;
507 
508   const CodeGenInstruction *getResultInst() const {
509     if (isa<const CodeGenInstruction *>(DefRec))
510       return cast<const CodeGenInstruction *>(DefRec);
511     return cast<const CodeGenInstAlias *>(DefRec)->ResultInst;
512   }
513 
514   /// ResOperands - This is the operand list that should be built for the result
515   /// MCInst.
516   SmallVector<ResOperand, 8> ResOperands;
517 
518   /// Mnemonic - This is the first token of the matched instruction, its
519   /// mnemonic.
520   StringRef Mnemonic;
521 
522   /// AsmOperands - The textual operands that this instruction matches,
523   /// annotated with a class and where in the OperandList they were defined.
524   /// This directly corresponds to the tokenized AsmString after the mnemonic is
525   /// removed.
526   SmallVector<AsmOperand, 8> AsmOperands;
527 
528   /// Predicates - The required subtarget features to match this instruction.
529   SmallVector<const SubtargetFeatureInfo *, 4> RequiredFeatures;
530 
531   /// ConversionFnKind - The enum value which is passed to the generated
532   /// convertToMCInst to convert parsed operands into an MCInst for this
533   /// function.
534   std::string ConversionFnKind;
535 
536   /// If this instruction is deprecated in some form.
537   bool HasDeprecation = false;
538 
539   /// If this is an alias, this is use to determine whether or not to using
540   /// the conversion function defined by the instruction's AsmMatchConverter
541   /// or to use the function generated by the alias.
542   bool UseInstAsmMatchConverter;
543 
544   MatchableInfo(const CodeGenInstruction &CGI)
545     : AsmVariantID(0), AsmString(CGI.AsmString), TheDef(CGI.TheDef), DefRec(&CGI),
546       UseInstAsmMatchConverter(true) {
547   }
548 
549   MatchableInfo(std::unique_ptr<const CodeGenInstAlias> Alias)
550     : AsmVariantID(0), AsmString(Alias->AsmString), TheDef(Alias->TheDef),
551       DefRec(Alias.release()),
552       UseInstAsmMatchConverter(
553         TheDef->getValueAsBit("UseInstAsmMatchConverter")) {
554   }
555 
556   // Could remove this and the dtor if PointerUnion supported unique_ptr
557   // elements with a dynamic failure/assertion (like the one below) in the case
558   // where it was copied while being in an owning state.
559   MatchableInfo(const MatchableInfo &RHS)
560       : AsmVariantID(RHS.AsmVariantID), AsmString(RHS.AsmString),
561         TheDef(RHS.TheDef), DefRec(RHS.DefRec), ResOperands(RHS.ResOperands),
562         Mnemonic(RHS.Mnemonic), AsmOperands(RHS.AsmOperands),
563         RequiredFeatures(RHS.RequiredFeatures),
564         ConversionFnKind(RHS.ConversionFnKind),
565         HasDeprecation(RHS.HasDeprecation),
566         UseInstAsmMatchConverter(RHS.UseInstAsmMatchConverter) {
567     assert(!isa<const CodeGenInstAlias *>(DefRec));
568   }
569 
570   ~MatchableInfo() {
571     delete dyn_cast_if_present<const CodeGenInstAlias *>(DefRec);
572   }
573 
574   // Two-operand aliases clone from the main matchable, but mark the second
575   // operand as a tied operand of the first for purposes of the assembler.
576   void formTwoOperandAlias(StringRef Constraint);
577 
578   void initialize(const AsmMatcherInfo &Info,
579                   SmallPtrSetImpl<Record*> &SingletonRegisters,
580                   AsmVariantInfo const &Variant,
581                   bool HasMnemonicFirst);
582 
583   /// validate - Return true if this matchable is a valid thing to match against
584   /// and perform a bunch of validity checking.
585   bool validate(StringRef CommentDelimiter, bool IsAlias) const;
586 
587   /// findAsmOperand - Find the AsmOperand with the specified name and
588   /// suboperand index.
589   int findAsmOperand(StringRef N, int SubOpIdx) const {
590     auto I = find_if(AsmOperands, [&](const AsmOperand &Op) {
591       return Op.SrcOpName == N && Op.SubOpIdx == SubOpIdx;
592     });
593     return (I != AsmOperands.end()) ? I - AsmOperands.begin() : -1;
594   }
595 
596   /// findAsmOperandNamed - Find the first AsmOperand with the specified name.
597   /// This does not check the suboperand index.
598   int findAsmOperandNamed(StringRef N, int LastIdx = -1) const {
599     auto I =
600         llvm::find_if(llvm::drop_begin(AsmOperands, LastIdx + 1),
601                       [&](const AsmOperand &Op) { return Op.SrcOpName == N; });
602     return (I != AsmOperands.end()) ? I - AsmOperands.begin() : -1;
603   }
604 
605   int findAsmOperandOriginallyNamed(StringRef N) const {
606     auto I =
607         find_if(AsmOperands,
608                 [&](const AsmOperand &Op) { return Op.OrigSrcOpName == N; });
609     return (I != AsmOperands.end()) ? I - AsmOperands.begin() : -1;
610   }
611 
612   void buildInstructionResultOperands();
613   void buildAliasResultOperands(bool AliasConstraintsAreChecked);
614 
615   /// operator< - Compare two matchables.
616   bool operator<(const MatchableInfo &RHS) const {
617     // The primary comparator is the instruction mnemonic.
618     if (int Cmp = Mnemonic.compare_insensitive(RHS.Mnemonic))
619       return Cmp == -1;
620 
621     if (AsmOperands.size() != RHS.AsmOperands.size())
622       return AsmOperands.size() < RHS.AsmOperands.size();
623 
624     // Compare lexicographically by operand. The matcher validates that other
625     // orderings wouldn't be ambiguous using \see couldMatchAmbiguouslyWith().
626     for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
627       if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
628         return true;
629       if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
630         return false;
631     }
632 
633     // For X86 AVX/AVX512 instructions, we prefer vex encoding because the
634     // vex encoding size is smaller. Since X86InstrSSE.td is included ahead
635     // of X86InstrAVX512.td, the AVX instruction ID is less than AVX512 ID.
636     // We use the ID to sort AVX instruction before AVX512 instruction in
637     // matching table.
638     if (TheDef->isSubClassOf("Instruction") &&
639         TheDef->getValueAsBit("HasPositionOrder") &&
640         RHS.TheDef->isSubClassOf("Instruction") &&
641         RHS.TheDef->getValueAsBit("HasPositionOrder"))
642       return TheDef->getID() < RHS.TheDef->getID();
643 
644     // Give matches that require more features higher precedence. This is useful
645     // because we cannot define AssemblerPredicates with the negation of
646     // processor features. For example, ARM v6 "nop" may be either a HINT or
647     // MOV. With v6, we want to match HINT. The assembler has no way to
648     // predicate MOV under "NoV6", but HINT will always match first because it
649     // requires V6 while MOV does not.
650     if (RequiredFeatures.size() != RHS.RequiredFeatures.size())
651       return RequiredFeatures.size() > RHS.RequiredFeatures.size();
652 
653     return false;
654   }
655 
656   /// couldMatchAmbiguouslyWith - Check whether this matchable could
657   /// ambiguously match the same set of operands as \p RHS (without being a
658   /// strictly superior match).
659   bool couldMatchAmbiguouslyWith(const MatchableInfo &RHS) const {
660     // The primary comparator is the instruction mnemonic.
661     if (Mnemonic != RHS.Mnemonic)
662       return false;
663 
664     // Different variants can't conflict.
665     if (AsmVariantID != RHS.AsmVariantID)
666       return false;
667 
668     // The number of operands is unambiguous.
669     if (AsmOperands.size() != RHS.AsmOperands.size())
670       return false;
671 
672     // Otherwise, make sure the ordering of the two instructions is unambiguous
673     // by checking that either (a) a token or operand kind discriminates them,
674     // or (b) the ordering among equivalent kinds is consistent.
675 
676     // Tokens and operand kinds are unambiguous (assuming a correct target
677     // specific parser).
678     for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
679       if (AsmOperands[i].Class->Kind != RHS.AsmOperands[i].Class->Kind ||
680           AsmOperands[i].Class->Kind == ClassInfo::Token)
681         if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class ||
682             *RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
683           return false;
684 
685     // Otherwise, this operand could commute if all operands are equivalent, or
686     // there is a pair of operands that compare less than and a pair that
687     // compare greater than.
688     bool HasLT = false, HasGT = false;
689     for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
690       if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
691         HasLT = true;
692       if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
693         HasGT = true;
694     }
695 
696     return HasLT == HasGT;
697   }
698 
699   void dump() const;
700 
701 private:
702   void tokenizeAsmString(AsmMatcherInfo const &Info,
703                          AsmVariantInfo const &Variant);
704   void addAsmOperand(StringRef Token, bool IsIsolatedToken = false);
705 };
706 
707 struct OperandMatchEntry {
708   unsigned OperandMask;
709   const MatchableInfo* MI;
710   ClassInfo *CI;
711 
712   static OperandMatchEntry create(const MatchableInfo *mi, ClassInfo *ci,
713                                   unsigned opMask) {
714     OperandMatchEntry X;
715     X.OperandMask = opMask;
716     X.CI = ci;
717     X.MI = mi;
718     return X;
719   }
720 };
721 
722 class AsmMatcherInfo {
723 public:
724   /// Tracked Records
725   RecordKeeper &Records;
726 
727   /// The tablegen AsmParser record.
728   Record *AsmParser;
729 
730   /// Target - The target information.
731   CodeGenTarget &Target;
732 
733   /// The classes which are needed for matching.
734   std::forward_list<ClassInfo> Classes;
735 
736   /// The information on the matchables to match.
737   std::vector<std::unique_ptr<MatchableInfo>> Matchables;
738 
739   /// Info for custom matching operands by user defined methods.
740   std::vector<OperandMatchEntry> OperandMatchInfo;
741 
742   /// Map of Register records to their class information.
743   typedef std::map<Record*, ClassInfo*, LessRecordByID> RegisterClassesTy;
744   RegisterClassesTy RegisterClasses;
745 
746   /// Map of Predicate records to their subtarget information.
747   std::map<Record *, SubtargetFeatureInfo, LessRecordByID> SubtargetFeatures;
748 
749   /// Map of AsmOperandClass records to their class information.
750   std::map<Record*, ClassInfo*> AsmOperandClasses;
751 
752   /// Map of RegisterClass records to their class information.
753   std::map<Record*, ClassInfo*> RegisterClassClasses;
754 
755 private:
756   /// Map of token to class information which has already been constructed.
757   std::map<std::string, ClassInfo*> TokenClasses;
758 
759 private:
760   /// getTokenClass - Lookup or create the class for the given token.
761   ClassInfo *getTokenClass(StringRef Token);
762 
763   /// getOperandClass - Lookup or create the class for the given operand.
764   ClassInfo *getOperandClass(const CGIOperandList::OperandInfo &OI,
765                              int SubOpIdx);
766   ClassInfo *getOperandClass(Record *Rec, int SubOpIdx);
767 
768   /// buildRegisterClasses - Build the ClassInfo* instances for register
769   /// classes.
770   void buildRegisterClasses(SmallPtrSetImpl<Record*> &SingletonRegisters);
771 
772   /// buildOperandClasses - Build the ClassInfo* instances for user defined
773   /// operand classes.
774   void buildOperandClasses();
775 
776   void buildInstructionOperandReference(MatchableInfo *II, StringRef OpName,
777                                         unsigned AsmOpIdx);
778   void buildAliasOperandReference(MatchableInfo *II, StringRef OpName,
779                                   MatchableInfo::AsmOperand &Op);
780 
781 public:
782   AsmMatcherInfo(Record *AsmParser,
783                  CodeGenTarget &Target,
784                  RecordKeeper &Records);
785 
786   /// Construct the various tables used during matching.
787   void buildInfo();
788 
789   /// buildOperandMatchInfo - Build the necessary information to handle user
790   /// defined operand parsing methods.
791   void buildOperandMatchInfo();
792 
793   /// getSubtargetFeature - Lookup or create the subtarget feature info for the
794   /// given operand.
795   const SubtargetFeatureInfo *getSubtargetFeature(Record *Def) const {
796     assert(Def->isSubClassOf("Predicate") && "Invalid predicate type!");
797     const auto &I = SubtargetFeatures.find(Def);
798     return I == SubtargetFeatures.end() ? nullptr : &I->second;
799   }
800 
801   RecordKeeper &getRecords() const {
802     return Records;
803   }
804 
805   bool hasOptionalOperands() const {
806     return any_of(Classes,
807                   [](const ClassInfo &Class) { return Class.IsOptional; });
808   }
809 };
810 
811 } // end anonymous namespace
812 
813 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
814 LLVM_DUMP_METHOD void MatchableInfo::dump() const {
815   errs() << TheDef->getName() << " -- " << "flattened:\"" << AsmString <<"\"\n";
816 
817   errs() << "  variant: " << AsmVariantID << "\n";
818 
819   for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
820     const AsmOperand &Op = AsmOperands[i];
821     errs() << "  op[" << i << "] = " << Op.Class->ClassName << " - ";
822     errs() << '\"' << Op.Token << "\"\n";
823   }
824 }
825 #endif
826 
827 static std::pair<StringRef, StringRef>
828 parseTwoOperandConstraint(StringRef S, ArrayRef<SMLoc> Loc) {
829   // Split via the '='.
830   std::pair<StringRef, StringRef> Ops = S.split('=');
831   if (Ops.second == "")
832     PrintFatalError(Loc, "missing '=' in two-operand alias constraint");
833   // Trim whitespace and the leading '$' on the operand names.
834   size_t start = Ops.first.find_first_of('$');
835   if (start == std::string::npos)
836     PrintFatalError(Loc, "expected '$' prefix on asm operand name");
837   Ops.first = Ops.first.slice(start + 1, std::string::npos);
838   size_t end = Ops.first.find_last_of(" \t");
839   Ops.first = Ops.first.slice(0, end);
840   // Now the second operand.
841   start = Ops.second.find_first_of('$');
842   if (start == std::string::npos)
843     PrintFatalError(Loc, "expected '$' prefix on asm operand name");
844   Ops.second = Ops.second.slice(start + 1, std::string::npos);
845   end = Ops.second.find_last_of(" \t");
846   Ops.first = Ops.first.slice(0, end);
847   return Ops;
848 }
849 
850 void MatchableInfo::formTwoOperandAlias(StringRef Constraint) {
851   // Figure out which operands are aliased and mark them as tied.
852   std::pair<StringRef, StringRef> Ops =
853     parseTwoOperandConstraint(Constraint, TheDef->getLoc());
854 
855   // Find the AsmOperands that refer to the operands we're aliasing.
856   int SrcAsmOperand = findAsmOperandNamed(Ops.first);
857   int DstAsmOperand = findAsmOperandNamed(Ops.second);
858   if (SrcAsmOperand == -1)
859     PrintFatalError(TheDef->getLoc(),
860                     "unknown source two-operand alias operand '" + Ops.first +
861                     "'.");
862   if (DstAsmOperand == -1)
863     PrintFatalError(TheDef->getLoc(),
864                     "unknown destination two-operand alias operand '" +
865                     Ops.second + "'.");
866 
867   // Find the ResOperand that refers to the operand we're aliasing away
868   // and update it to refer to the combined operand instead.
869   for (ResOperand &Op : ResOperands) {
870     if (Op.Kind == ResOperand::RenderAsmOperand &&
871         Op.AsmOperandNum == (unsigned)SrcAsmOperand) {
872       Op.AsmOperandNum = DstAsmOperand;
873       break;
874     }
875   }
876   // Remove the AsmOperand for the alias operand.
877   AsmOperands.erase(AsmOperands.begin() + SrcAsmOperand);
878   // Adjust the ResOperand references to any AsmOperands that followed
879   // the one we just deleted.
880   for (ResOperand &Op : ResOperands) {
881     switch(Op.Kind) {
882     default:
883       // Nothing to do for operands that don't reference AsmOperands.
884       break;
885     case ResOperand::RenderAsmOperand:
886       if (Op.AsmOperandNum > (unsigned)SrcAsmOperand)
887         --Op.AsmOperandNum;
888       break;
889     }
890   }
891 }
892 
893 /// extractSingletonRegisterForAsmOperand - Extract singleton register,
894 /// if present, from specified token.
895 static void
896 extractSingletonRegisterForAsmOperand(MatchableInfo::AsmOperand &Op,
897                                       const AsmMatcherInfo &Info,
898                                       StringRef RegisterPrefix) {
899   StringRef Tok = Op.Token;
900 
901   // If this token is not an isolated token, i.e., it isn't separated from
902   // other tokens (e.g. with whitespace), don't interpret it as a register name.
903   if (!Op.IsIsolatedToken)
904     return;
905 
906   if (RegisterPrefix.empty()) {
907     std::string LoweredTok = Tok.lower();
908     if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(LoweredTok))
909       Op.SingletonReg = Reg->TheDef;
910     return;
911   }
912 
913   if (!Tok.starts_with(RegisterPrefix))
914     return;
915 
916   StringRef RegName = Tok.substr(RegisterPrefix.size());
917   if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(RegName))
918     Op.SingletonReg = Reg->TheDef;
919 
920   // If there is no register prefix (i.e. "%" in "%eax"), then this may
921   // be some random non-register token, just ignore it.
922 }
923 
924 void MatchableInfo::initialize(const AsmMatcherInfo &Info,
925                                SmallPtrSetImpl<Record*> &SingletonRegisters,
926                                AsmVariantInfo const &Variant,
927                                bool HasMnemonicFirst) {
928   AsmVariantID = Variant.AsmVariantNo;
929   AsmString =
930     CodeGenInstruction::FlattenAsmStringVariants(AsmString,
931                                                  Variant.AsmVariantNo);
932 
933   tokenizeAsmString(Info, Variant);
934 
935   // The first token of the instruction is the mnemonic, which must be a
936   // simple string, not a $foo variable or a singleton register.
937   if (AsmOperands.empty())
938     PrintFatalError(TheDef->getLoc(),
939                   "Instruction '" + TheDef->getName() + "' has no tokens");
940 
941   assert(!AsmOperands[0].Token.empty());
942   if (HasMnemonicFirst) {
943     Mnemonic = AsmOperands[0].Token;
944     if (Mnemonic[0] == '$')
945       PrintFatalError(TheDef->getLoc(),
946                       "Invalid instruction mnemonic '" + Mnemonic + "'!");
947 
948     // Remove the first operand, it is tracked in the mnemonic field.
949     AsmOperands.erase(AsmOperands.begin());
950   } else if (AsmOperands[0].Token[0] != '$')
951     Mnemonic = AsmOperands[0].Token;
952 
953   // Compute the require features.
954   for (Record *Predicate : TheDef->getValueAsListOfDefs("Predicates"))
955     if (const SubtargetFeatureInfo *Feature =
956             Info.getSubtargetFeature(Predicate))
957       RequiredFeatures.push_back(Feature);
958 
959   // Collect singleton registers, if used.
960   for (MatchableInfo::AsmOperand &Op : AsmOperands) {
961     extractSingletonRegisterForAsmOperand(Op, Info, Variant.RegisterPrefix);
962     if (Record *Reg = Op.SingletonReg)
963       SingletonRegisters.insert(Reg);
964   }
965 
966   const RecordVal *DepMask = TheDef->getValue("DeprecatedFeatureMask");
967   if (!DepMask)
968     DepMask = TheDef->getValue("ComplexDeprecationPredicate");
969 
970   HasDeprecation =
971       DepMask ? !DepMask->getValue()->getAsUnquotedString().empty() : false;
972 }
973 
974 /// Append an AsmOperand for the given substring of AsmString.
975 void MatchableInfo::addAsmOperand(StringRef Token, bool IsIsolatedToken) {
976   AsmOperands.push_back(AsmOperand(IsIsolatedToken, Token));
977 }
978 
979 /// tokenizeAsmString - Tokenize a simplified assembly string.
980 void MatchableInfo::tokenizeAsmString(const AsmMatcherInfo &Info,
981                                       AsmVariantInfo const &Variant) {
982   StringRef String = AsmString;
983   size_t Prev = 0;
984   bool InTok = false;
985   bool IsIsolatedToken = true;
986   for (size_t i = 0, e = String.size(); i != e; ++i) {
987     char Char = String[i];
988     if (Variant.BreakCharacters.contains(Char)) {
989       if (InTok) {
990         addAsmOperand(String.slice(Prev, i), false);
991         Prev = i;
992         IsIsolatedToken = false;
993       }
994       InTok = true;
995       continue;
996     }
997     if (Variant.TokenizingCharacters.contains(Char)) {
998       if (InTok) {
999         addAsmOperand(String.slice(Prev, i), IsIsolatedToken);
1000         InTok = false;
1001         IsIsolatedToken = false;
1002       }
1003       addAsmOperand(String.slice(i, i + 1), IsIsolatedToken);
1004       Prev = i + 1;
1005       IsIsolatedToken = true;
1006       continue;
1007     }
1008     if (Variant.SeparatorCharacters.contains(Char)) {
1009       if (InTok) {
1010         addAsmOperand(String.slice(Prev, i), IsIsolatedToken);
1011         InTok = false;
1012       }
1013       Prev = i + 1;
1014       IsIsolatedToken = true;
1015       continue;
1016     }
1017 
1018     switch (Char) {
1019     case '\\':
1020       if (InTok) {
1021         addAsmOperand(String.slice(Prev, i), false);
1022         InTok = false;
1023         IsIsolatedToken = false;
1024       }
1025       ++i;
1026       assert(i != String.size() && "Invalid quoted character");
1027       addAsmOperand(String.slice(i, i + 1), IsIsolatedToken);
1028       Prev = i + 1;
1029       IsIsolatedToken = false;
1030       break;
1031 
1032     case '$': {
1033       if (InTok) {
1034         addAsmOperand(String.slice(Prev, i), IsIsolatedToken);
1035         InTok = false;
1036         IsIsolatedToken = false;
1037       }
1038 
1039       // If this isn't "${", start new identifier looking like "$xxx"
1040       if (i + 1 == String.size() || String[i + 1] != '{') {
1041         Prev = i;
1042         break;
1043       }
1044 
1045       size_t EndPos = String.find('}', i);
1046       assert(EndPos != StringRef::npos &&
1047              "Missing brace in operand reference!");
1048       addAsmOperand(String.slice(i, EndPos+1), IsIsolatedToken);
1049       Prev = EndPos + 1;
1050       i = EndPos;
1051       IsIsolatedToken = false;
1052       break;
1053     }
1054 
1055     default:
1056       InTok = true;
1057       break;
1058     }
1059   }
1060   if (InTok && Prev != String.size())
1061     addAsmOperand(String.substr(Prev), IsIsolatedToken);
1062 }
1063 
1064 bool MatchableInfo::validate(StringRef CommentDelimiter, bool IsAlias) const {
1065   // Reject matchables with no .s string.
1066   if (AsmString.empty())
1067     PrintFatalError(TheDef->getLoc(), "instruction with empty asm string");
1068 
1069   // Reject any matchables with a newline in them, they should be marked
1070   // isCodeGenOnly if they are pseudo instructions.
1071   if (AsmString.find('\n') != std::string::npos)
1072     PrintFatalError(TheDef->getLoc(),
1073                   "multiline instruction is not valid for the asmparser, "
1074                   "mark it isCodeGenOnly");
1075 
1076   // Remove comments from the asm string.  We know that the asmstring only
1077   // has one line.
1078   if (!CommentDelimiter.empty() &&
1079       StringRef(AsmString).contains(CommentDelimiter))
1080     PrintFatalError(TheDef->getLoc(),
1081                   "asmstring for instruction has comment character in it, "
1082                   "mark it isCodeGenOnly");
1083 
1084   // Reject matchables with operand modifiers, these aren't something we can
1085   // handle, the target should be refactored to use operands instead of
1086   // modifiers.
1087   //
1088   // Also, check for instructions which reference the operand multiple times,
1089   // if they don't define a custom AsmMatcher: this implies a constraint that
1090   // the built-in matching code would not honor.
1091   std::set<std::string> OperandNames;
1092   for (const AsmOperand &Op : AsmOperands) {
1093     StringRef Tok = Op.Token;
1094     if (Tok[0] == '$' && Tok.contains(':'))
1095       PrintFatalError(TheDef->getLoc(),
1096                       "matchable with operand modifier '" + Tok +
1097                       "' not supported by asm matcher.  Mark isCodeGenOnly!");
1098     // Verify that any operand is only mentioned once.
1099     // We reject aliases and ignore instructions for now.
1100     if (!IsAlias && TheDef->getValueAsString("AsmMatchConverter").empty() &&
1101         Tok[0] == '$' && !OperandNames.insert(std::string(Tok)).second) {
1102       LLVM_DEBUG({
1103         errs() << "warning: '" << TheDef->getName() << "': "
1104                << "ignoring instruction with tied operand '"
1105                << Tok << "'\n";
1106       });
1107       return false;
1108     }
1109   }
1110 
1111   return true;
1112 }
1113 
1114 static std::string getEnumNameForToken(StringRef Str) {
1115   std::string Res;
1116 
1117   for (char C : Str) {
1118     switch (C) {
1119     case '*': Res += "_STAR_"; break;
1120     case '%': Res += "_PCT_"; break;
1121     case ':': Res += "_COLON_"; break;
1122     case '!': Res += "_EXCLAIM_"; break;
1123     case '.': Res += "_DOT_"; break;
1124     case '<': Res += "_LT_"; break;
1125     case '>': Res += "_GT_"; break;
1126     case '-': Res += "_MINUS_"; break;
1127     case '#': Res += "_HASH_"; break;
1128     default:
1129       if (isAlnum(C))
1130         Res += C;
1131       else
1132         Res += "_" + utostr((unsigned)C) + "_";
1133     }
1134   }
1135 
1136   return Res;
1137 }
1138 
1139 ClassInfo *AsmMatcherInfo::getTokenClass(StringRef Token) {
1140   ClassInfo *&Entry = TokenClasses[std::string(Token)];
1141 
1142   if (!Entry) {
1143     Classes.emplace_front();
1144     Entry = &Classes.front();
1145     Entry->Kind = ClassInfo::Token;
1146     Entry->ClassName = "Token";
1147     Entry->Name = "MCK_" + getEnumNameForToken(Token);
1148     Entry->ValueName = std::string(Token);
1149     Entry->PredicateMethod = "<invalid>";
1150     Entry->RenderMethod = "<invalid>";
1151     Entry->ParserMethod = "";
1152     Entry->DiagnosticType = "";
1153     Entry->IsOptional = false;
1154     Entry->DefaultMethod = "<invalid>";
1155   }
1156 
1157   return Entry;
1158 }
1159 
1160 ClassInfo *
1161 AsmMatcherInfo::getOperandClass(const CGIOperandList::OperandInfo &OI,
1162                                 int SubOpIdx) {
1163   Record *Rec = OI.Rec;
1164   if (SubOpIdx != -1)
1165     Rec = cast<DefInit>(OI.MIOperandInfo->getArg(SubOpIdx))->getDef();
1166   return getOperandClass(Rec, SubOpIdx);
1167 }
1168 
1169 ClassInfo *
1170 AsmMatcherInfo::getOperandClass(Record *Rec, int SubOpIdx) {
1171   if (Rec->isSubClassOf("RegisterOperand")) {
1172     // RegisterOperand may have an associated ParserMatchClass. If it does,
1173     // use it, else just fall back to the underlying register class.
1174     const RecordVal *R = Rec->getValue("ParserMatchClass");
1175     if (!R || !R->getValue())
1176       PrintFatalError(Rec->getLoc(),
1177                       "Record `" + Rec->getName() +
1178                           "' does not have a ParserMatchClass!\n");
1179 
1180     if (DefInit *DI= dyn_cast<DefInit>(R->getValue())) {
1181       Record *MatchClass = DI->getDef();
1182       if (ClassInfo *CI = AsmOperandClasses[MatchClass])
1183         return CI;
1184     }
1185 
1186     // No custom match class. Just use the register class.
1187     Record *ClassRec = Rec->getValueAsDef("RegClass");
1188     if (!ClassRec)
1189       PrintFatalError(Rec->getLoc(), "RegisterOperand `" + Rec->getName() +
1190                     "' has no associated register class!\n");
1191     if (ClassInfo *CI = RegisterClassClasses[ClassRec])
1192       return CI;
1193     PrintFatalError(Rec->getLoc(), "register class has no class info!");
1194   }
1195 
1196   if (Rec->isSubClassOf("RegisterClass")) {
1197     if (ClassInfo *CI = RegisterClassClasses[Rec])
1198       return CI;
1199     PrintFatalError(Rec->getLoc(), "register class has no class info!");
1200   }
1201 
1202   if (!Rec->isSubClassOf("Operand"))
1203     PrintFatalError(Rec->getLoc(), "Operand `" + Rec->getName() +
1204                   "' does not derive from class Operand!\n");
1205   Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
1206   if (ClassInfo *CI = AsmOperandClasses[MatchClass])
1207     return CI;
1208 
1209   PrintFatalError(Rec->getLoc(), "operand has no match class!");
1210 }
1211 
1212 struct LessRegisterSet {
1213   bool operator() (const RegisterSet &LHS, const RegisterSet & RHS) const {
1214     // std::set<T> defines its own compariso "operator<", but it
1215     // performs a lexicographical comparison by T's innate comparison
1216     // for some reason. We don't want non-deterministic pointer
1217     // comparisons so use this instead.
1218     return std::lexicographical_compare(LHS.begin(), LHS.end(),
1219                                         RHS.begin(), RHS.end(),
1220                                         LessRecordByID());
1221   }
1222 };
1223 
1224 void AsmMatcherInfo::
1225 buildRegisterClasses(SmallPtrSetImpl<Record*> &SingletonRegisters) {
1226   const auto &Registers = Target.getRegBank().getRegisters();
1227   auto &RegClassList = Target.getRegBank().getRegClasses();
1228 
1229   typedef std::set<RegisterSet, LessRegisterSet> RegisterSetSet;
1230 
1231   // The register sets used for matching.
1232   RegisterSetSet RegisterSets;
1233 
1234   // Gather the defined sets.
1235   for (const CodeGenRegisterClass &RC : RegClassList)
1236     RegisterSets.insert(
1237         RegisterSet(RC.getOrder().begin(), RC.getOrder().end()));
1238 
1239   // Add any required singleton sets.
1240   for (Record *Rec : SingletonRegisters) {
1241     RegisterSets.insert(RegisterSet(&Rec, &Rec + 1));
1242   }
1243 
1244   // Introduce derived sets where necessary (when a register does not determine
1245   // a unique register set class), and build the mapping of registers to the set
1246   // they should classify to.
1247   std::map<Record*, RegisterSet> RegisterMap;
1248   for (const CodeGenRegister &CGR : Registers) {
1249     // Compute the intersection of all sets containing this register.
1250     RegisterSet ContainingSet;
1251 
1252     for (const RegisterSet &RS : RegisterSets) {
1253       if (!RS.count(CGR.TheDef))
1254         continue;
1255 
1256       if (ContainingSet.empty()) {
1257         ContainingSet = RS;
1258         continue;
1259       }
1260 
1261       RegisterSet Tmp;
1262       std::swap(Tmp, ContainingSet);
1263       std::insert_iterator<RegisterSet> II(ContainingSet,
1264                                            ContainingSet.begin());
1265       std::set_intersection(Tmp.begin(), Tmp.end(), RS.begin(), RS.end(), II,
1266                             LessRecordByID());
1267     }
1268 
1269     if (!ContainingSet.empty()) {
1270       RegisterSets.insert(ContainingSet);
1271       RegisterMap.insert(std::make_pair(CGR.TheDef, ContainingSet));
1272     }
1273   }
1274 
1275   // Construct the register classes.
1276   std::map<RegisterSet, ClassInfo*, LessRegisterSet> RegisterSetClasses;
1277   unsigned Index = 0;
1278   for (const RegisterSet &RS : RegisterSets) {
1279     Classes.emplace_front();
1280     ClassInfo *CI = &Classes.front();
1281     CI->Kind = ClassInfo::RegisterClass0 + Index;
1282     CI->ClassName = "Reg" + utostr(Index);
1283     CI->Name = "MCK_Reg" + utostr(Index);
1284     CI->ValueName = "";
1285     CI->PredicateMethod = ""; // unused
1286     CI->RenderMethod = "addRegOperands";
1287     CI->Registers = RS;
1288     // FIXME: diagnostic type.
1289     CI->DiagnosticType = "";
1290     CI->IsOptional = false;
1291     CI->DefaultMethod = ""; // unused
1292     RegisterSetClasses.insert(std::make_pair(RS, CI));
1293     ++Index;
1294   }
1295 
1296   // Find the superclasses; we could compute only the subgroup lattice edges,
1297   // but there isn't really a point.
1298   for (const RegisterSet &RS : RegisterSets) {
1299     ClassInfo *CI = RegisterSetClasses[RS];
1300     for (const RegisterSet &RS2 : RegisterSets)
1301       if (RS != RS2 &&
1302           std::includes(RS2.begin(), RS2.end(), RS.begin(), RS.end(),
1303                         LessRecordByID()))
1304         CI->SuperClasses.push_back(RegisterSetClasses[RS2]);
1305   }
1306 
1307   // Name the register classes which correspond to a user defined RegisterClass.
1308   for (const CodeGenRegisterClass &RC : RegClassList) {
1309     // Def will be NULL for non-user defined register classes.
1310     Record *Def = RC.getDef();
1311     if (!Def)
1312       continue;
1313     ClassInfo *CI = RegisterSetClasses[RegisterSet(RC.getOrder().begin(),
1314                                                    RC.getOrder().end())];
1315     if (CI->ValueName.empty()) {
1316       CI->ClassName = RC.getName();
1317       CI->Name = "MCK_" + RC.getName();
1318       CI->ValueName = RC.getName();
1319     } else
1320       CI->ValueName = CI->ValueName + "," + RC.getName();
1321 
1322     Init *DiagnosticType = Def->getValueInit("DiagnosticType");
1323     if (StringInit *SI = dyn_cast<StringInit>(DiagnosticType))
1324       CI->DiagnosticType = std::string(SI->getValue());
1325 
1326     Init *DiagnosticString = Def->getValueInit("DiagnosticString");
1327     if (StringInit *SI = dyn_cast<StringInit>(DiagnosticString))
1328       CI->DiagnosticString = std::string(SI->getValue());
1329 
1330     // If we have a diagnostic string but the diagnostic type is not specified
1331     // explicitly, create an anonymous diagnostic type.
1332     if (!CI->DiagnosticString.empty() && CI->DiagnosticType.empty())
1333       CI->DiagnosticType = RC.getName();
1334 
1335     RegisterClassClasses.insert(std::make_pair(Def, CI));
1336   }
1337 
1338   // Populate the map for individual registers.
1339   for (auto &It : RegisterMap)
1340     RegisterClasses[It.first] = RegisterSetClasses[It.second];
1341 
1342   // Name the register classes which correspond to singleton registers.
1343   for (Record *Rec : SingletonRegisters) {
1344     ClassInfo *CI = RegisterClasses[Rec];
1345     assert(CI && "Missing singleton register class info!");
1346 
1347     if (CI->ValueName.empty()) {
1348       CI->ClassName = std::string(Rec->getName());
1349       CI->Name = "MCK_" + Rec->getName().str();
1350       CI->ValueName = std::string(Rec->getName());
1351     } else
1352       CI->ValueName = CI->ValueName + "," + Rec->getName().str();
1353   }
1354 }
1355 
1356 void AsmMatcherInfo::buildOperandClasses() {
1357   std::vector<Record*> AsmOperands =
1358     Records.getAllDerivedDefinitions("AsmOperandClass");
1359 
1360   // Pre-populate AsmOperandClasses map.
1361   for (Record *Rec : AsmOperands) {
1362     Classes.emplace_front();
1363     AsmOperandClasses[Rec] = &Classes.front();
1364   }
1365 
1366   unsigned Index = 0;
1367   for (Record *Rec : AsmOperands) {
1368     ClassInfo *CI = AsmOperandClasses[Rec];
1369     CI->Kind = ClassInfo::UserClass0 + Index;
1370 
1371     ListInit *Supers = Rec->getValueAsListInit("SuperClasses");
1372     for (Init *I : Supers->getValues()) {
1373       DefInit *DI = dyn_cast<DefInit>(I);
1374       if (!DI) {
1375         PrintError(Rec->getLoc(), "Invalid super class reference!");
1376         continue;
1377       }
1378 
1379       ClassInfo *SC = AsmOperandClasses[DI->getDef()];
1380       if (!SC)
1381         PrintError(Rec->getLoc(), "Invalid super class reference!");
1382       else
1383         CI->SuperClasses.push_back(SC);
1384     }
1385     CI->ClassName = std::string(Rec->getValueAsString("Name"));
1386     CI->Name = "MCK_" + CI->ClassName;
1387     CI->ValueName = std::string(Rec->getName());
1388 
1389     // Get or construct the predicate method name.
1390     Init *PMName = Rec->getValueInit("PredicateMethod");
1391     if (StringInit *SI = dyn_cast<StringInit>(PMName)) {
1392       CI->PredicateMethod = std::string(SI->getValue());
1393     } else {
1394       assert(isa<UnsetInit>(PMName) && "Unexpected PredicateMethod field!");
1395       CI->PredicateMethod = "is" + CI->ClassName;
1396     }
1397 
1398     // Get or construct the render method name.
1399     Init *RMName = Rec->getValueInit("RenderMethod");
1400     if (StringInit *SI = dyn_cast<StringInit>(RMName)) {
1401       CI->RenderMethod = std::string(SI->getValue());
1402     } else {
1403       assert(isa<UnsetInit>(RMName) && "Unexpected RenderMethod field!");
1404       CI->RenderMethod = "add" + CI->ClassName + "Operands";
1405     }
1406 
1407     // Get the parse method name or leave it as empty.
1408     Init *PRMName = Rec->getValueInit("ParserMethod");
1409     if (StringInit *SI = dyn_cast<StringInit>(PRMName))
1410       CI->ParserMethod = std::string(SI->getValue());
1411 
1412     // Get the diagnostic type and string or leave them as empty.
1413     Init *DiagnosticType = Rec->getValueInit("DiagnosticType");
1414     if (StringInit *SI = dyn_cast<StringInit>(DiagnosticType))
1415       CI->DiagnosticType = std::string(SI->getValue());
1416     Init *DiagnosticString = Rec->getValueInit("DiagnosticString");
1417     if (StringInit *SI = dyn_cast<StringInit>(DiagnosticString))
1418       CI->DiagnosticString = std::string(SI->getValue());
1419     // If we have a DiagnosticString, we need a DiagnosticType for use within
1420     // the matcher.
1421     if (!CI->DiagnosticString.empty() && CI->DiagnosticType.empty())
1422       CI->DiagnosticType = CI->ClassName;
1423 
1424     Init *IsOptional = Rec->getValueInit("IsOptional");
1425     if (BitInit *BI = dyn_cast<BitInit>(IsOptional))
1426       CI->IsOptional = BI->getValue();
1427 
1428     // Get or construct the default method name.
1429     Init *DMName = Rec->getValueInit("DefaultMethod");
1430     if (StringInit *SI = dyn_cast<StringInit>(DMName)) {
1431       CI->DefaultMethod = std::string(SI->getValue());
1432     } else {
1433       assert(isa<UnsetInit>(DMName) && "Unexpected DefaultMethod field!");
1434       CI->DefaultMethod = "default" + CI->ClassName + "Operands";
1435     }
1436 
1437     ++Index;
1438   }
1439 }
1440 
1441 AsmMatcherInfo::AsmMatcherInfo(Record *asmParser,
1442                                CodeGenTarget &target,
1443                                RecordKeeper &records)
1444   : Records(records), AsmParser(asmParser), Target(target) {
1445 }
1446 
1447 /// buildOperandMatchInfo - Build the necessary information to handle user
1448 /// defined operand parsing methods.
1449 void AsmMatcherInfo::buildOperandMatchInfo() {
1450 
1451   /// Map containing a mask with all operands indices that can be found for
1452   /// that class inside a instruction.
1453   typedef std::map<ClassInfo *, unsigned, deref<std::less<>>> OpClassMaskTy;
1454   OpClassMaskTy OpClassMask;
1455 
1456   bool CallCustomParserForAllOperands =
1457       AsmParser->getValueAsBit("CallCustomParserForAllOperands");
1458   for (const auto &MI : Matchables) {
1459     OpClassMask.clear();
1460 
1461     // Keep track of all operands of this instructions which belong to the
1462     // same class.
1463     unsigned NumOptionalOps = 0;
1464     for (unsigned i = 0, e = MI->AsmOperands.size(); i != e; ++i) {
1465       const MatchableInfo::AsmOperand &Op = MI->AsmOperands[i];
1466       if (CallCustomParserForAllOperands || !Op.Class->ParserMethod.empty()) {
1467         unsigned &OperandMask = OpClassMask[Op.Class];
1468         OperandMask |= maskTrailingOnes<unsigned>(NumOptionalOps + 1)
1469                        << (i - NumOptionalOps);
1470       }
1471       if (Op.Class->IsOptional)
1472         ++NumOptionalOps;
1473     }
1474 
1475     // Generate operand match info for each mnemonic/operand class pair.
1476     for (const auto &OCM : OpClassMask) {
1477       unsigned OpMask = OCM.second;
1478       ClassInfo *CI = OCM.first;
1479       OperandMatchInfo.push_back(OperandMatchEntry::create(MI.get(), CI,
1480                                                            OpMask));
1481     }
1482   }
1483 }
1484 
1485 void AsmMatcherInfo::buildInfo() {
1486   // Build information about all of the AssemblerPredicates.
1487   const std::vector<std::pair<Record *, SubtargetFeatureInfo>>
1488       &SubtargetFeaturePairs = SubtargetFeatureInfo::getAll(Records);
1489   SubtargetFeatures.insert(SubtargetFeaturePairs.begin(),
1490                            SubtargetFeaturePairs.end());
1491 #ifndef NDEBUG
1492   for (const auto &Pair : SubtargetFeatures)
1493     LLVM_DEBUG(Pair.second.dump());
1494 #endif // NDEBUG
1495 
1496   bool HasMnemonicFirst = AsmParser->getValueAsBit("HasMnemonicFirst");
1497   bool ReportMultipleNearMisses =
1498       AsmParser->getValueAsBit("ReportMultipleNearMisses");
1499 
1500   // Parse the instructions; we need to do this first so that we can gather the
1501   // singleton register classes.
1502   SmallPtrSet<Record*, 16> SingletonRegisters;
1503   unsigned VariantCount = Target.getAsmParserVariantCount();
1504   for (unsigned VC = 0; VC != VariantCount; ++VC) {
1505     Record *AsmVariant = Target.getAsmParserVariant(VC);
1506     StringRef CommentDelimiter =
1507         AsmVariant->getValueAsString("CommentDelimiter");
1508     AsmVariantInfo Variant;
1509     Variant.RegisterPrefix = AsmVariant->getValueAsString("RegisterPrefix");
1510     Variant.TokenizingCharacters =
1511         AsmVariant->getValueAsString("TokenizingCharacters");
1512     Variant.SeparatorCharacters =
1513         AsmVariant->getValueAsString("SeparatorCharacters");
1514     Variant.BreakCharacters =
1515         AsmVariant->getValueAsString("BreakCharacters");
1516     Variant.Name = AsmVariant->getValueAsString("Name");
1517     Variant.AsmVariantNo = AsmVariant->getValueAsInt("Variant");
1518 
1519     for (const CodeGenInstruction *CGI : Target.getInstructionsByEnumValue()) {
1520 
1521       // If the tblgen -match-prefix option is specified (for tblgen hackers),
1522       // filter the set of instructions we consider.
1523       if (!StringRef(CGI->TheDef->getName()).starts_with(MatchPrefix))
1524         continue;
1525 
1526       // Ignore "codegen only" instructions.
1527       if (CGI->TheDef->getValueAsBit("isCodeGenOnly"))
1528         continue;
1529 
1530       // Ignore instructions for different instructions
1531       StringRef V = CGI->TheDef->getValueAsString("AsmVariantName");
1532       if (!V.empty() && V != Variant.Name)
1533         continue;
1534 
1535       auto II = std::make_unique<MatchableInfo>(*CGI);
1536 
1537       II->initialize(*this, SingletonRegisters, Variant, HasMnemonicFirst);
1538 
1539       // Ignore instructions which shouldn't be matched and diagnose invalid
1540       // instruction definitions with an error.
1541       if (!II->validate(CommentDelimiter, false))
1542         continue;
1543 
1544       Matchables.push_back(std::move(II));
1545     }
1546 
1547     // Parse all of the InstAlias definitions and stick them in the list of
1548     // matchables.
1549     std::vector<Record*> AllInstAliases =
1550       Records.getAllDerivedDefinitions("InstAlias");
1551     for (Record *InstAlias : AllInstAliases) {
1552       auto Alias = std::make_unique<CodeGenInstAlias>(InstAlias, Target);
1553 
1554       // If the tblgen -match-prefix option is specified (for tblgen hackers),
1555       // filter the set of instruction aliases we consider, based on the target
1556       // instruction.
1557       if (!StringRef(Alias->ResultInst->TheDef->getName())
1558                .starts_with(MatchPrefix))
1559         continue;
1560 
1561       StringRef V = Alias->TheDef->getValueAsString("AsmVariantName");
1562       if (!V.empty() && V != Variant.Name)
1563         continue;
1564 
1565       auto II = std::make_unique<MatchableInfo>(std::move(Alias));
1566 
1567       II->initialize(*this, SingletonRegisters, Variant, HasMnemonicFirst);
1568 
1569       // Validate the alias definitions.
1570       II->validate(CommentDelimiter, true);
1571 
1572       Matchables.push_back(std::move(II));
1573     }
1574   }
1575 
1576   // Build info for the register classes.
1577   buildRegisterClasses(SingletonRegisters);
1578 
1579   // Build info for the user defined assembly operand classes.
1580   buildOperandClasses();
1581 
1582   // Build the information about matchables, now that we have fully formed
1583   // classes.
1584   std::vector<std::unique_ptr<MatchableInfo>> NewMatchables;
1585   for (auto &II : Matchables) {
1586     // Parse the tokens after the mnemonic.
1587     // Note: buildInstructionOperandReference may insert new AsmOperands, so
1588     // don't precompute the loop bound.
1589     for (unsigned i = 0; i != II->AsmOperands.size(); ++i) {
1590       MatchableInfo::AsmOperand &Op = II->AsmOperands[i];
1591       StringRef Token = Op.Token;
1592 
1593       // Check for singleton registers.
1594       if (Record *RegRecord = Op.SingletonReg) {
1595         Op.Class = RegisterClasses[RegRecord];
1596         assert(Op.Class && Op.Class->Registers.size() == 1 &&
1597                "Unexpected class for singleton register");
1598         continue;
1599       }
1600 
1601       // Check for simple tokens.
1602       if (Token[0] != '$') {
1603         Op.Class = getTokenClass(Token);
1604         continue;
1605       }
1606 
1607       if (Token.size() > 1 && isdigit(Token[1])) {
1608         Op.Class = getTokenClass(Token);
1609         continue;
1610       }
1611 
1612       // Otherwise this is an operand reference.
1613       StringRef OperandName;
1614       if (Token[1] == '{')
1615         OperandName = Token.substr(2, Token.size() - 3);
1616       else
1617         OperandName = Token.substr(1);
1618 
1619       if (isa<const CodeGenInstruction *>(II->DefRec))
1620         buildInstructionOperandReference(II.get(), OperandName, i);
1621       else
1622         buildAliasOperandReference(II.get(), OperandName, Op);
1623     }
1624 
1625     if (isa<const CodeGenInstruction *>(II->DefRec)) {
1626       II->buildInstructionResultOperands();
1627       // If the instruction has a two-operand alias, build up the
1628       // matchable here. We'll add them in bulk at the end to avoid
1629       // confusing this loop.
1630       StringRef Constraint =
1631           II->TheDef->getValueAsString("TwoOperandAliasConstraint");
1632       if (Constraint != "") {
1633         // Start by making a copy of the original matchable.
1634         auto AliasII = std::make_unique<MatchableInfo>(*II);
1635 
1636         // Adjust it to be a two-operand alias.
1637         AliasII->formTwoOperandAlias(Constraint);
1638 
1639         // Add the alias to the matchables list.
1640         NewMatchables.push_back(std::move(AliasII));
1641       }
1642     } else
1643       // FIXME: The tied operands checking is not yet integrated with the
1644       // framework for reporting multiple near misses. To prevent invalid
1645       // formats from being matched with an alias if a tied-operands check
1646       // would otherwise have disallowed it, we just disallow such constructs
1647       // in TableGen completely.
1648       II->buildAliasResultOperands(!ReportMultipleNearMisses);
1649   }
1650   if (!NewMatchables.empty())
1651     Matchables.insert(Matchables.end(),
1652                       std::make_move_iterator(NewMatchables.begin()),
1653                       std::make_move_iterator(NewMatchables.end()));
1654 
1655   // Process token alias definitions and set up the associated superclass
1656   // information.
1657   std::vector<Record*> AllTokenAliases =
1658     Records.getAllDerivedDefinitions("TokenAlias");
1659   for (Record *Rec : AllTokenAliases) {
1660     ClassInfo *FromClass = getTokenClass(Rec->getValueAsString("FromToken"));
1661     ClassInfo *ToClass = getTokenClass(Rec->getValueAsString("ToToken"));
1662     if (FromClass == ToClass)
1663       PrintFatalError(Rec->getLoc(),
1664                     "error: Destination value identical to source value.");
1665     FromClass->SuperClasses.push_back(ToClass);
1666   }
1667 
1668   // Reorder classes so that classes precede super classes.
1669   Classes.sort();
1670 
1671 #ifdef EXPENSIVE_CHECKS
1672   // Verify that the table is sorted and operator < works transitively.
1673   for (auto I = Classes.begin(), E = Classes.end(); I != E; ++I) {
1674     for (auto J = I; J != E; ++J) {
1675       assert(!(*J < *I));
1676       assert(I == J || !J->isSubsetOf(*I));
1677     }
1678   }
1679 #endif
1680 }
1681 
1682 /// buildInstructionOperandReference - The specified operand is a reference to a
1683 /// named operand such as $src.  Resolve the Class and OperandInfo pointers.
1684 void AsmMatcherInfo::
1685 buildInstructionOperandReference(MatchableInfo *II,
1686                                  StringRef OperandName,
1687                                  unsigned AsmOpIdx) {
1688   const CodeGenInstruction &CGI = *cast<const CodeGenInstruction *>(II->DefRec);
1689   const CGIOperandList &Operands = CGI.Operands;
1690   MatchableInfo::AsmOperand *Op = &II->AsmOperands[AsmOpIdx];
1691 
1692   // Map this token to an operand.
1693   unsigned Idx;
1694   if (!Operands.hasOperandNamed(OperandName, Idx))
1695     PrintFatalError(II->TheDef->getLoc(),
1696                     "error: unable to find operand: '" + OperandName + "'");
1697 
1698   // If the instruction operand has multiple suboperands, but the parser
1699   // match class for the asm operand is still the default "ImmAsmOperand",
1700   // then handle each suboperand separately.
1701   if (Op->SubOpIdx == -1 && Operands[Idx].MINumOperands > 1) {
1702     Record *Rec = Operands[Idx].Rec;
1703     assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
1704     Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
1705     if (MatchClass && MatchClass->getValueAsString("Name") == "Imm") {
1706       // Insert remaining suboperands after AsmOpIdx in II->AsmOperands.
1707       StringRef Token = Op->Token; // save this in case Op gets moved
1708       for (unsigned SI = 1, SE = Operands[Idx].MINumOperands; SI != SE; ++SI) {
1709         MatchableInfo::AsmOperand NewAsmOp(/*IsIsolatedToken=*/true, Token);
1710         NewAsmOp.SubOpIdx = SI;
1711         II->AsmOperands.insert(II->AsmOperands.begin()+AsmOpIdx+SI, NewAsmOp);
1712       }
1713       // Replace Op with first suboperand.
1714       Op = &II->AsmOperands[AsmOpIdx]; // update the pointer in case it moved
1715       Op->SubOpIdx = 0;
1716     }
1717   }
1718 
1719   // Set up the operand class.
1720   Op->Class = getOperandClass(Operands[Idx], Op->SubOpIdx);
1721   Op->OrigSrcOpName = OperandName;
1722 
1723   // If the named operand is tied, canonicalize it to the untied operand.
1724   // For example, something like:
1725   //   (outs GPR:$dst), (ins GPR:$src)
1726   // with an asmstring of
1727   //   "inc $src"
1728   // we want to canonicalize to:
1729   //   "inc $dst"
1730   // so that we know how to provide the $dst operand when filling in the result.
1731   int OITied = -1;
1732   if (Operands[Idx].MINumOperands == 1)
1733     OITied = Operands[Idx].getTiedRegister();
1734   if (OITied != -1) {
1735     // The tied operand index is an MIOperand index, find the operand that
1736     // contains it.
1737     std::pair<unsigned, unsigned> Idx = Operands.getSubOperandNumber(OITied);
1738     OperandName = Operands[Idx.first].Name;
1739     Op->SubOpIdx = Idx.second;
1740   }
1741 
1742   Op->SrcOpName = OperandName;
1743 }
1744 
1745 /// buildAliasOperandReference - When parsing an operand reference out of the
1746 /// matching string (e.g. "movsx $src, $dst"), determine what the class of the
1747 /// operand reference is by looking it up in the result pattern definition.
1748 void AsmMatcherInfo::buildAliasOperandReference(MatchableInfo *II,
1749                                                 StringRef OperandName,
1750                                                 MatchableInfo::AsmOperand &Op) {
1751   const CodeGenInstAlias &CGA = *cast<const CodeGenInstAlias *>(II->DefRec);
1752 
1753   // Set up the operand class.
1754   for (unsigned i = 0, e = CGA.ResultOperands.size(); i != e; ++i)
1755     if (CGA.ResultOperands[i].isRecord() &&
1756         CGA.ResultOperands[i].getName() == OperandName) {
1757       // It's safe to go with the first one we find, because CodeGenInstAlias
1758       // validates that all operands with the same name have the same record.
1759       Op.SubOpIdx = CGA.ResultInstOperandIndex[i].second;
1760       // Use the match class from the Alias definition, not the
1761       // destination instruction, as we may have an immediate that's
1762       // being munged by the match class.
1763       Op.Class = getOperandClass(CGA.ResultOperands[i].getRecord(),
1764                                  Op.SubOpIdx);
1765       Op.SrcOpName = OperandName;
1766       Op.OrigSrcOpName = OperandName;
1767       return;
1768     }
1769 
1770   PrintFatalError(II->TheDef->getLoc(),
1771                   "error: unable to find operand: '" + OperandName + "'");
1772 }
1773 
1774 void MatchableInfo::buildInstructionResultOperands() {
1775   const CodeGenInstruction *ResultInst = getResultInst();
1776 
1777   // Loop over all operands of the result instruction, determining how to
1778   // populate them.
1779   for (const CGIOperandList::OperandInfo &OpInfo : ResultInst->Operands) {
1780     // If this is a tied operand, just copy from the previously handled operand.
1781     int TiedOp = -1;
1782     if (OpInfo.MINumOperands == 1)
1783       TiedOp = OpInfo.getTiedRegister();
1784     if (TiedOp != -1) {
1785       int TiedSrcOperand = findAsmOperandOriginallyNamed(OpInfo.Name);
1786       if (TiedSrcOperand != -1 &&
1787           ResOperands[TiedOp].Kind == ResOperand::RenderAsmOperand)
1788         ResOperands.push_back(ResOperand::getTiedOp(
1789             TiedOp, ResOperands[TiedOp].AsmOperandNum, TiedSrcOperand));
1790       else
1791         ResOperands.push_back(ResOperand::getTiedOp(TiedOp, 0, 0));
1792       continue;
1793     }
1794 
1795     int SrcOperand = findAsmOperandNamed(OpInfo.Name);
1796     if (OpInfo.Name.empty() || SrcOperand == -1) {
1797       // This may happen for operands that are tied to a suboperand of a
1798       // complex operand.  Simply use a dummy value here; nobody should
1799       // use this operand slot.
1800       // FIXME: The long term goal is for the MCOperand list to not contain
1801       // tied operands at all.
1802       ResOperands.push_back(ResOperand::getImmOp(0));
1803       continue;
1804     }
1805 
1806     // Check if the one AsmOperand populates the entire operand.
1807     unsigned NumOperands = OpInfo.MINumOperands;
1808     if (AsmOperands[SrcOperand].SubOpIdx == -1) {
1809       ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand, NumOperands));
1810       continue;
1811     }
1812 
1813     // Add a separate ResOperand for each suboperand.
1814     for (unsigned AI = 0; AI < NumOperands; ++AI) {
1815       assert(AsmOperands[SrcOperand+AI].SubOpIdx == (int)AI &&
1816              AsmOperands[SrcOperand+AI].SrcOpName == OpInfo.Name &&
1817              "unexpected AsmOperands for suboperands");
1818       ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand + AI, 1));
1819     }
1820   }
1821 }
1822 
1823 void MatchableInfo::buildAliasResultOperands(bool AliasConstraintsAreChecked) {
1824   const CodeGenInstAlias &CGA = *cast<const CodeGenInstAlias *>(DefRec);
1825   const CodeGenInstruction *ResultInst = getResultInst();
1826 
1827   // Map of:  $reg -> #lastref
1828   //   where $reg is the name of the operand in the asm string
1829   //   where #lastref is the last processed index where $reg was referenced in
1830   //   the asm string.
1831   SmallDenseMap<StringRef, int> OperandRefs;
1832 
1833   // Loop over all operands of the result instruction, determining how to
1834   // populate them.
1835   unsigned AliasOpNo = 0;
1836   unsigned LastOpNo = CGA.ResultInstOperandIndex.size();
1837   for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
1838     const CGIOperandList::OperandInfo *OpInfo = &ResultInst->Operands[i];
1839 
1840     // If this is a tied operand, just copy from the previously handled operand.
1841     int TiedOp = -1;
1842     if (OpInfo->MINumOperands == 1)
1843       TiedOp = OpInfo->getTiedRegister();
1844     if (TiedOp != -1) {
1845       unsigned SrcOp1 = 0;
1846       unsigned SrcOp2 = 0;
1847 
1848       // If an operand has been specified twice in the asm string,
1849       // add the two source operand's indices to the TiedOp so that
1850       // at runtime the 'tied' constraint is checked.
1851       if (ResOperands[TiedOp].Kind == ResOperand::RenderAsmOperand) {
1852         SrcOp1 = ResOperands[TiedOp].AsmOperandNum;
1853 
1854         // Find the next operand (similarly named operand) in the string.
1855         StringRef Name = AsmOperands[SrcOp1].SrcOpName;
1856         auto Insert = OperandRefs.try_emplace(Name, SrcOp1);
1857         SrcOp2 = findAsmOperandNamed(Name, Insert.first->second);
1858 
1859         // Not updating the record in OperandRefs will cause TableGen
1860         // to fail with an error at the end of this function.
1861         if (AliasConstraintsAreChecked)
1862           Insert.first->second = SrcOp2;
1863 
1864         // In case it only has one reference in the asm string,
1865         // it doesn't need to be checked for tied constraints.
1866         SrcOp2 = (SrcOp2 == (unsigned)-1) ? SrcOp1 : SrcOp2;
1867       }
1868 
1869       // If the alias operand is of a different operand class, we only want
1870       // to benefit from the tied-operands check and just match the operand
1871       // as a normal, but not copy the original (TiedOp) to the result
1872       // instruction. We do this by passing -1 as the tied operand to copy.
1873       if (ResultInst->Operands[i].Rec->getName() !=
1874           ResultInst->Operands[TiedOp].Rec->getName()) {
1875         SrcOp1 = ResOperands[TiedOp].AsmOperandNum;
1876         int SubIdx = CGA.ResultInstOperandIndex[AliasOpNo].second;
1877         StringRef Name = CGA.ResultOperands[AliasOpNo].getName();
1878         SrcOp2 = findAsmOperand(Name, SubIdx);
1879         ResOperands.push_back(
1880             ResOperand::getTiedOp((unsigned)-1, SrcOp1, SrcOp2));
1881       } else {
1882         ResOperands.push_back(ResOperand::getTiedOp(TiedOp, SrcOp1, SrcOp2));
1883         continue;
1884       }
1885     }
1886 
1887     // Handle all the suboperands for this operand.
1888     const std::string &OpName = OpInfo->Name;
1889     for ( ; AliasOpNo <  LastOpNo &&
1890             CGA.ResultInstOperandIndex[AliasOpNo].first == i; ++AliasOpNo) {
1891       int SubIdx = CGA.ResultInstOperandIndex[AliasOpNo].second;
1892 
1893       // Find out what operand from the asmparser that this MCInst operand
1894       // comes from.
1895       switch (CGA.ResultOperands[AliasOpNo].Kind) {
1896       case CodeGenInstAlias::ResultOperand::K_Record: {
1897         StringRef Name = CGA.ResultOperands[AliasOpNo].getName();
1898         int SrcOperand = findAsmOperand(Name, SubIdx);
1899         if (SrcOperand == -1)
1900           PrintFatalError(TheDef->getLoc(), "Instruction '" +
1901                         TheDef->getName() + "' has operand '" + OpName +
1902                         "' that doesn't appear in asm string!");
1903 
1904         // Add it to the operand references. If it is added a second time, the
1905         // record won't be updated and it will fail later on.
1906         OperandRefs.try_emplace(Name, SrcOperand);
1907 
1908         unsigned NumOperands = (SubIdx == -1 ? OpInfo->MINumOperands : 1);
1909         ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand,
1910                                                         NumOperands));
1911         break;
1912       }
1913       case CodeGenInstAlias::ResultOperand::K_Imm: {
1914         int64_t ImmVal = CGA.ResultOperands[AliasOpNo].getImm();
1915         ResOperands.push_back(ResOperand::getImmOp(ImmVal));
1916         break;
1917       }
1918       case CodeGenInstAlias::ResultOperand::K_Reg: {
1919         Record *Reg = CGA.ResultOperands[AliasOpNo].getRegister();
1920         ResOperands.push_back(ResOperand::getRegOp(Reg));
1921         break;
1922       }
1923       }
1924     }
1925   }
1926 
1927   // Check that operands are not repeated more times than is supported.
1928   for (auto &T : OperandRefs) {
1929     if (T.second != -1 && findAsmOperandNamed(T.first, T.second) != -1)
1930       PrintFatalError(TheDef->getLoc(),
1931                       "Operand '" + T.first + "' can never be matched");
1932   }
1933 }
1934 
1935 static unsigned
1936 getConverterOperandID(const std::string &Name,
1937                       SmallSetVector<CachedHashString, 16> &Table,
1938                       bool &IsNew) {
1939   IsNew = Table.insert(CachedHashString(Name));
1940 
1941   unsigned ID = IsNew ? Table.size() - 1 : find(Table, Name) - Table.begin();
1942 
1943   assert(ID < Table.size());
1944 
1945   return ID;
1946 }
1947 
1948 static unsigned
1949 emitConvertFuncs(CodeGenTarget &Target, StringRef ClassName,
1950                  std::vector<std::unique_ptr<MatchableInfo>> &Infos,
1951                  bool HasMnemonicFirst, bool HasOptionalOperands,
1952                  raw_ostream &OS) {
1953   SmallSetVector<CachedHashString, 16> OperandConversionKinds;
1954   SmallSetVector<CachedHashString, 16> InstructionConversionKinds;
1955   std::vector<std::vector<uint8_t> > ConversionTable;
1956   size_t MaxRowLength = 2; // minimum is custom converter plus terminator.
1957 
1958   // TargetOperandClass - This is the target's operand class, like X86Operand.
1959   std::string TargetOperandClass = Target.getName().str() + "Operand";
1960 
1961   // Write the convert function to a separate stream, so we can drop it after
1962   // the enum. We'll build up the conversion handlers for the individual
1963   // operand types opportunistically as we encounter them.
1964   std::string ConvertFnBody;
1965   raw_string_ostream CvtOS(ConvertFnBody);
1966   // Start the unified conversion function.
1967   if (HasOptionalOperands) {
1968     CvtOS << "void " << Target.getName() << ClassName << "::\n"
1969           << "convertToMCInst(unsigned Kind, MCInst &Inst, "
1970           << "unsigned Opcode,\n"
1971           << "                const OperandVector &Operands,\n"
1972           << "                const SmallBitVector &OptionalOperandsMask) {\n";
1973   } else {
1974     CvtOS << "void " << Target.getName() << ClassName << "::\n"
1975           << "convertToMCInst(unsigned Kind, MCInst &Inst, "
1976           << "unsigned Opcode,\n"
1977           << "                const OperandVector &Operands) {\n";
1978   }
1979   CvtOS << "  assert(Kind < CVT_NUM_SIGNATURES && \"Invalid signature!\");\n";
1980   CvtOS << "  const uint8_t *Converter = ConversionTable[Kind];\n";
1981   if (HasOptionalOperands) {
1982     size_t MaxNumOperands = 0;
1983     for (const auto &MI : Infos) {
1984       MaxNumOperands = std::max(MaxNumOperands, MI->AsmOperands.size());
1985     }
1986     CvtOS << "  unsigned DefaultsOffset[" << (MaxNumOperands + 1)
1987           << "] = { 0 };\n";
1988     CvtOS << "  assert(OptionalOperandsMask.size() == " << (MaxNumOperands)
1989           << ");\n";
1990     CvtOS << "  for (unsigned i = 0, NumDefaults = 0; i < " << (MaxNumOperands)
1991           << "; ++i) {\n";
1992     CvtOS << "    DefaultsOffset[i + 1] = NumDefaults;\n";
1993     CvtOS << "    NumDefaults += (OptionalOperandsMask[i] ? 1 : 0);\n";
1994     CvtOS << "  }\n";
1995   }
1996   CvtOS << "  unsigned OpIdx;\n";
1997   CvtOS << "  Inst.setOpcode(Opcode);\n";
1998   CvtOS << "  for (const uint8_t *p = Converter; *p; p += 2) {\n";
1999   if (HasOptionalOperands) {
2000     CvtOS << "    OpIdx = *(p + 1) - DefaultsOffset[*(p + 1)];\n";
2001   } else {
2002     CvtOS << "    OpIdx = *(p + 1);\n";
2003   }
2004   CvtOS << "    switch (*p) {\n";
2005   CvtOS << "    default: llvm_unreachable(\"invalid conversion entry!\");\n";
2006   CvtOS << "    case CVT_Reg:\n";
2007   CvtOS << "      static_cast<" << TargetOperandClass
2008         << " &>(*Operands[OpIdx]).addRegOperands(Inst, 1);\n";
2009   CvtOS << "      break;\n";
2010   CvtOS << "    case CVT_Tied: {\n";
2011   CvtOS << "      assert(OpIdx < (size_t)(std::end(TiedAsmOperandTable) -\n";
2012   CvtOS << "                              std::begin(TiedAsmOperandTable)) &&\n";
2013   CvtOS << "             \"Tied operand not found\");\n";
2014   CvtOS << "      unsigned TiedResOpnd = TiedAsmOperandTable[OpIdx][0];\n";
2015   CvtOS << "      if (TiedResOpnd != (uint8_t)-1)\n";
2016   CvtOS << "        Inst.addOperand(Inst.getOperand(TiedResOpnd));\n";
2017   CvtOS << "      break;\n";
2018   CvtOS << "    }\n";
2019 
2020   std::string OperandFnBody;
2021   raw_string_ostream OpOS(OperandFnBody);
2022   // Start the operand number lookup function.
2023   OpOS << "void " << Target.getName() << ClassName << "::\n"
2024        << "convertToMapAndConstraints(unsigned Kind,\n";
2025   OpOS.indent(27);
2026   OpOS << "const OperandVector &Operands) {\n"
2027        << "  assert(Kind < CVT_NUM_SIGNATURES && \"Invalid signature!\");\n"
2028        << "  unsigned NumMCOperands = 0;\n"
2029        << "  const uint8_t *Converter = ConversionTable[Kind];\n"
2030        << "  for (const uint8_t *p = Converter; *p; p += 2) {\n"
2031        << "    switch (*p) {\n"
2032        << "    default: llvm_unreachable(\"invalid conversion entry!\");\n"
2033        << "    case CVT_Reg:\n"
2034        << "      Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n"
2035        << "      Operands[*(p + 1)]->setConstraint(\"r\");\n"
2036        << "      ++NumMCOperands;\n"
2037        << "      break;\n"
2038        << "    case CVT_Tied:\n"
2039        << "      ++NumMCOperands;\n"
2040        << "      break;\n";
2041 
2042   // Pre-populate the operand conversion kinds with the standard always
2043   // available entries.
2044   OperandConversionKinds.insert(CachedHashString("CVT_Done"));
2045   OperandConversionKinds.insert(CachedHashString("CVT_Reg"));
2046   OperandConversionKinds.insert(CachedHashString("CVT_Tied"));
2047   enum { CVT_Done, CVT_Reg, CVT_Tied };
2048 
2049   // Map of e.g. <0, 2, 3> -> "Tie_0_2_3" enum label.
2050   std::map<std::tuple<uint8_t, uint8_t, uint8_t>, std::string>
2051   TiedOperandsEnumMap;
2052 
2053   for (auto &II : Infos) {
2054     // Check if we have a custom match function.
2055     StringRef AsmMatchConverter =
2056         II->getResultInst()->TheDef->getValueAsString("AsmMatchConverter");
2057     if (!AsmMatchConverter.empty() && II->UseInstAsmMatchConverter) {
2058       std::string Signature = ("ConvertCustom_" + AsmMatchConverter).str();
2059       II->ConversionFnKind = Signature;
2060 
2061       // Check if we have already generated this signature.
2062       if (!InstructionConversionKinds.insert(CachedHashString(Signature)))
2063         continue;
2064 
2065       // Remember this converter for the kind enum.
2066       unsigned KindID = OperandConversionKinds.size();
2067       OperandConversionKinds.insert(
2068           CachedHashString("CVT_" + getEnumNameForToken(AsmMatchConverter)));
2069 
2070       // Add the converter row for this instruction.
2071       ConversionTable.emplace_back();
2072       ConversionTable.back().push_back(KindID);
2073       ConversionTable.back().push_back(CVT_Done);
2074 
2075       // Add the handler to the conversion driver function.
2076       CvtOS << "    case CVT_"
2077             << getEnumNameForToken(AsmMatchConverter) << ":\n"
2078             << "      " << AsmMatchConverter << "(Inst, Operands);\n"
2079             << "      break;\n";
2080 
2081       // FIXME: Handle the operand number lookup for custom match functions.
2082       continue;
2083     }
2084 
2085     // Build the conversion function signature.
2086     std::string Signature = "Convert";
2087 
2088     std::vector<uint8_t> ConversionRow;
2089 
2090     // Compute the convert enum and the case body.
2091     MaxRowLength = std::max(MaxRowLength, II->ResOperands.size()*2 + 1 );
2092 
2093     for (unsigned i = 0, e = II->ResOperands.size(); i != e; ++i) {
2094       const MatchableInfo::ResOperand &OpInfo = II->ResOperands[i];
2095 
2096       // Generate code to populate each result operand.
2097       switch (OpInfo.Kind) {
2098       case MatchableInfo::ResOperand::RenderAsmOperand: {
2099         // This comes from something we parsed.
2100         const MatchableInfo::AsmOperand &Op =
2101           II->AsmOperands[OpInfo.AsmOperandNum];
2102 
2103         // Registers are always converted the same, don't duplicate the
2104         // conversion function based on them.
2105         Signature += "__";
2106         std::string Class;
2107         Class = Op.Class->isRegisterClass() ? "Reg" : Op.Class->ClassName;
2108         Signature += Class;
2109         Signature += utostr(OpInfo.MINumOperands);
2110         Signature += "_" + itostr(OpInfo.AsmOperandNum);
2111 
2112         // Add the conversion kind, if necessary, and get the associated ID
2113         // the index of its entry in the vector).
2114         std::string Name = "CVT_" + (Op.Class->isRegisterClass() ? "Reg" :
2115                                      Op.Class->RenderMethod);
2116         if (Op.Class->IsOptional) {
2117           // For optional operands we must also care about DefaultMethod
2118           assert(HasOptionalOperands);
2119           Name += "_" + Op.Class->DefaultMethod;
2120         }
2121         Name = getEnumNameForToken(Name);
2122 
2123         bool IsNewConverter = false;
2124         unsigned ID = getConverterOperandID(Name, OperandConversionKinds,
2125                                             IsNewConverter);
2126 
2127         // Add the operand entry to the instruction kind conversion row.
2128         ConversionRow.push_back(ID);
2129         ConversionRow.push_back(OpInfo.AsmOperandNum + HasMnemonicFirst);
2130 
2131         if (!IsNewConverter)
2132           break;
2133 
2134         // This is a new operand kind. Add a handler for it to the
2135         // converter driver.
2136         CvtOS << "    case " << Name << ":\n";
2137         if (Op.Class->IsOptional) {
2138           // If optional operand is not present in actual instruction then we
2139           // should call its DefaultMethod before RenderMethod
2140           assert(HasOptionalOperands);
2141           CvtOS << "      if (OptionalOperandsMask[*(p + 1) - 1]) {\n"
2142                 << "        " << Op.Class->DefaultMethod << "()"
2143                 << "->" << Op.Class->RenderMethod << "(Inst, "
2144                 << OpInfo.MINumOperands << ");\n"
2145                 << "      } else {\n"
2146                 << "        static_cast<" << TargetOperandClass
2147                 << " &>(*Operands[OpIdx])." << Op.Class->RenderMethod
2148                 << "(Inst, " << OpInfo.MINumOperands << ");\n"
2149                 << "      }\n";
2150         } else {
2151           CvtOS << "      static_cast<" << TargetOperandClass
2152                 << " &>(*Operands[OpIdx])." << Op.Class->RenderMethod
2153                 << "(Inst, " << OpInfo.MINumOperands << ");\n";
2154         }
2155         CvtOS << "      break;\n";
2156 
2157         // Add a handler for the operand number lookup.
2158         OpOS << "    case " << Name << ":\n"
2159              << "      Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n";
2160 
2161         if (Op.Class->isRegisterClass())
2162           OpOS << "      Operands[*(p + 1)]->setConstraint(\"r\");\n";
2163         else
2164           OpOS << "      Operands[*(p + 1)]->setConstraint(\"m\");\n";
2165         OpOS << "      NumMCOperands += " << OpInfo.MINumOperands << ";\n"
2166              << "      break;\n";
2167         break;
2168       }
2169       case MatchableInfo::ResOperand::TiedOperand: {
2170         // If this operand is tied to a previous one, just copy the MCInst
2171         // operand from the earlier one.We can only tie single MCOperand values.
2172         assert(OpInfo.MINumOperands == 1 && "Not a singular MCOperand");
2173         uint8_t TiedOp = OpInfo.TiedOperands.ResOpnd;
2174         uint8_t SrcOp1 =
2175             OpInfo.TiedOperands.SrcOpnd1Idx + HasMnemonicFirst;
2176         uint8_t SrcOp2 =
2177             OpInfo.TiedOperands.SrcOpnd2Idx + HasMnemonicFirst;
2178         assert((i > TiedOp || TiedOp == (uint8_t)-1) &&
2179                "Tied operand precedes its target!");
2180         auto TiedTupleName = std::string("Tie") + utostr(TiedOp) + '_' +
2181                              utostr(SrcOp1) + '_' + utostr(SrcOp2);
2182         Signature += "__" + TiedTupleName;
2183         ConversionRow.push_back(CVT_Tied);
2184         ConversionRow.push_back(TiedOp);
2185         ConversionRow.push_back(SrcOp1);
2186         ConversionRow.push_back(SrcOp2);
2187 
2188         // Also create an 'enum' for this combination of tied operands.
2189         auto Key = std::make_tuple(TiedOp, SrcOp1, SrcOp2);
2190         TiedOperandsEnumMap.emplace(Key, TiedTupleName);
2191         break;
2192       }
2193       case MatchableInfo::ResOperand::ImmOperand: {
2194         int64_t Val = OpInfo.ImmVal;
2195         std::string Ty = "imm_" + itostr(Val);
2196         Ty = getEnumNameForToken(Ty);
2197         Signature += "__" + Ty;
2198 
2199         std::string Name = "CVT_" + Ty;
2200         bool IsNewConverter = false;
2201         unsigned ID = getConverterOperandID(Name, OperandConversionKinds,
2202                                             IsNewConverter);
2203         // Add the operand entry to the instruction kind conversion row.
2204         ConversionRow.push_back(ID);
2205         ConversionRow.push_back(0);
2206 
2207         if (!IsNewConverter)
2208           break;
2209 
2210         CvtOS << "    case " << Name << ":\n"
2211               << "      Inst.addOperand(MCOperand::createImm(" << Val << "));\n"
2212               << "      break;\n";
2213 
2214         OpOS << "    case " << Name << ":\n"
2215              << "      Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n"
2216              << "      Operands[*(p + 1)]->setConstraint(\"\");\n"
2217              << "      ++NumMCOperands;\n"
2218              << "      break;\n";
2219         break;
2220       }
2221       case MatchableInfo::ResOperand::RegOperand: {
2222         std::string Reg, Name;
2223         if (!OpInfo.Register) {
2224           Name = "reg0";
2225           Reg = "0";
2226         } else {
2227           Reg = getQualifiedName(OpInfo.Register);
2228           Name = "reg" + OpInfo.Register->getName().str();
2229         }
2230         Signature += "__" + Name;
2231         Name = "CVT_" + Name;
2232         bool IsNewConverter = false;
2233         unsigned ID = getConverterOperandID(Name, OperandConversionKinds,
2234                                             IsNewConverter);
2235         // Add the operand entry to the instruction kind conversion row.
2236         ConversionRow.push_back(ID);
2237         ConversionRow.push_back(0);
2238 
2239         if (!IsNewConverter)
2240           break;
2241         CvtOS << "    case " << Name << ":\n"
2242               << "      Inst.addOperand(MCOperand::createReg(" << Reg << "));\n"
2243               << "      break;\n";
2244 
2245         OpOS << "    case " << Name << ":\n"
2246              << "      Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n"
2247              << "      Operands[*(p + 1)]->setConstraint(\"m\");\n"
2248              << "      ++NumMCOperands;\n"
2249              << "      break;\n";
2250       }
2251       }
2252     }
2253 
2254     // If there were no operands, add to the signature to that effect
2255     if (Signature == "Convert")
2256       Signature += "_NoOperands";
2257 
2258     II->ConversionFnKind = Signature;
2259 
2260     // Save the signature. If we already have it, don't add a new row
2261     // to the table.
2262     if (!InstructionConversionKinds.insert(CachedHashString(Signature)))
2263       continue;
2264 
2265     // Add the row to the table.
2266     ConversionTable.push_back(std::move(ConversionRow));
2267   }
2268 
2269   // Finish up the converter driver function.
2270   CvtOS << "    }\n  }\n}\n\n";
2271 
2272   // Finish up the operand number lookup function.
2273   OpOS << "    }\n  }\n}\n\n";
2274 
2275   // Output a static table for tied operands.
2276   if (TiedOperandsEnumMap.size()) {
2277     // The number of tied operand combinations will be small in practice,
2278     // but just add the assert to be sure.
2279     assert(TiedOperandsEnumMap.size() <= 254 &&
2280            "Too many tied-operand combinations to reference with "
2281            "an 8bit offset from the conversion table, where index "
2282            "'255' is reserved as operand not to be copied.");
2283 
2284     OS << "enum {\n";
2285     for (auto &KV : TiedOperandsEnumMap) {
2286       OS << "  " << KV.second << ",\n";
2287     }
2288     OS << "};\n\n";
2289 
2290     OS << "static const uint8_t TiedAsmOperandTable[][3] = {\n";
2291     for (auto &KV : TiedOperandsEnumMap) {
2292       OS << "  /* " << KV.second << " */ { "
2293          << utostr(std::get<0>(KV.first)) << ", "
2294          << utostr(std::get<1>(KV.first)) << ", "
2295          << utostr(std::get<2>(KV.first)) << " },\n";
2296     }
2297     OS << "};\n\n";
2298   } else
2299     OS << "static const uint8_t TiedAsmOperandTable[][3] = "
2300           "{ /* empty  */ {0, 0, 0} };\n\n";
2301 
2302   OS << "namespace {\n";
2303 
2304   // Output the operand conversion kind enum.
2305   OS << "enum OperatorConversionKind {\n";
2306   for (const auto &Converter : OperandConversionKinds)
2307     OS << "  " << Converter << ",\n";
2308   OS << "  CVT_NUM_CONVERTERS\n";
2309   OS << "};\n\n";
2310 
2311   // Output the instruction conversion kind enum.
2312   OS << "enum InstructionConversionKind {\n";
2313   for (const auto &Signature : InstructionConversionKinds)
2314     OS << "  " << Signature << ",\n";
2315   OS << "  CVT_NUM_SIGNATURES\n";
2316   OS << "};\n\n";
2317 
2318   OS << "} // end anonymous namespace\n\n";
2319 
2320   // Output the conversion table.
2321   OS << "static const uint8_t ConversionTable[CVT_NUM_SIGNATURES]["
2322      << MaxRowLength << "] = {\n";
2323 
2324   for (unsigned Row = 0, ERow = ConversionTable.size(); Row != ERow; ++Row) {
2325     assert(ConversionTable[Row].size() % 2 == 0 && "bad conversion row!");
2326     OS << "  // " << InstructionConversionKinds[Row] << "\n";
2327     OS << "  { ";
2328     for (unsigned i = 0, e = ConversionTable[Row].size(); i != e; i += 2) {
2329       OS << OperandConversionKinds[ConversionTable[Row][i]] << ", ";
2330       if (OperandConversionKinds[ConversionTable[Row][i]] !=
2331           CachedHashString("CVT_Tied")) {
2332         OS << (unsigned)(ConversionTable[Row][i + 1]) << ", ";
2333         continue;
2334       }
2335 
2336       // For a tied operand, emit a reference to the TiedAsmOperandTable
2337       // that contains the operand to copy, and the parsed operands to
2338       // check for their tied constraints.
2339       auto Key = std::make_tuple((uint8_t)ConversionTable[Row][i + 1],
2340                                  (uint8_t)ConversionTable[Row][i + 2],
2341                                  (uint8_t)ConversionTable[Row][i + 3]);
2342       auto TiedOpndEnum = TiedOperandsEnumMap.find(Key);
2343       assert(TiedOpndEnum != TiedOperandsEnumMap.end() &&
2344              "No record for tied operand pair");
2345       OS << TiedOpndEnum->second << ", ";
2346       i += 2;
2347     }
2348     OS << "CVT_Done },\n";
2349   }
2350 
2351   OS << "};\n\n";
2352 
2353   // Spit out the conversion driver function.
2354   OS << CvtOS.str();
2355 
2356   // Spit out the operand number lookup function.
2357   OS << OpOS.str();
2358 
2359   return ConversionTable.size();
2360 }
2361 
2362 /// emitMatchClassEnumeration - Emit the enumeration for match class kinds.
2363 static void emitMatchClassEnumeration(CodeGenTarget &Target,
2364                                       std::forward_list<ClassInfo> &Infos,
2365                                       raw_ostream &OS) {
2366   OS << "namespace {\n\n";
2367 
2368   OS << "/// MatchClassKind - The kinds of classes which participate in\n"
2369      << "/// instruction matching.\n";
2370   OS << "enum MatchClassKind {\n";
2371   OS << "  InvalidMatchClass = 0,\n";
2372   OS << "  OptionalMatchClass = 1,\n";
2373   ClassInfo::ClassInfoKind LastKind = ClassInfo::Token;
2374   StringRef LastName = "OptionalMatchClass";
2375   for (const auto &CI : Infos) {
2376     if (LastKind == ClassInfo::Token && CI.Kind != ClassInfo::Token) {
2377       OS << "  MCK_LAST_TOKEN = " << LastName << ",\n";
2378     } else if (LastKind < ClassInfo::UserClass0 &&
2379                CI.Kind >= ClassInfo::UserClass0) {
2380       OS << "  MCK_LAST_REGISTER = " << LastName << ",\n";
2381     }
2382     LastKind = (ClassInfo::ClassInfoKind)CI.Kind;
2383     LastName = CI.Name;
2384 
2385     OS << "  " << CI.Name << ", // ";
2386     if (CI.Kind == ClassInfo::Token) {
2387       OS << "'" << CI.ValueName << "'\n";
2388     } else if (CI.isRegisterClass()) {
2389       if (!CI.ValueName.empty())
2390         OS << "register class '" << CI.ValueName << "'\n";
2391       else
2392         OS << "derived register class\n";
2393     } else {
2394       OS << "user defined class '" << CI.ValueName << "'\n";
2395     }
2396   }
2397   OS << "  NumMatchClassKinds\n";
2398   OS << "};\n\n";
2399 
2400   OS << "} // end anonymous namespace\n\n";
2401 }
2402 
2403 /// emitMatchClassDiagStrings - Emit a function to get the diagnostic text to be
2404 /// used when an assembly operand does not match the expected operand class.
2405 static void emitOperandMatchErrorDiagStrings(AsmMatcherInfo &Info, raw_ostream &OS) {
2406   // If the target does not use DiagnosticString for any operands, don't emit
2407   // an unused function.
2408   if (llvm::all_of(Info.Classes, [](const ClassInfo &CI) {
2409         return CI.DiagnosticString.empty();
2410       }))
2411     return;
2412 
2413   OS << "static const char *getMatchKindDiag(" << Info.Target.getName()
2414      << "AsmParser::" << Info.Target.getName()
2415      << "MatchResultTy MatchResult) {\n";
2416   OS << "  switch (MatchResult) {\n";
2417 
2418   for (const auto &CI: Info.Classes) {
2419     if (!CI.DiagnosticString.empty()) {
2420       assert(!CI.DiagnosticType.empty() &&
2421              "DiagnosticString set without DiagnosticType");
2422       OS << "  case " << Info.Target.getName()
2423          << "AsmParser::Match_" << CI.DiagnosticType << ":\n";
2424       OS << "    return \"" << CI.DiagnosticString << "\";\n";
2425     }
2426   }
2427 
2428   OS << "  default:\n";
2429   OS << "    return nullptr;\n";
2430 
2431   OS << "  }\n";
2432   OS << "}\n\n";
2433 }
2434 
2435 static void emitRegisterMatchErrorFunc(AsmMatcherInfo &Info, raw_ostream &OS) {
2436   OS << "static unsigned getDiagKindFromRegisterClass(MatchClassKind "
2437         "RegisterClass) {\n";
2438   if (none_of(Info.Classes, [](const ClassInfo &CI) {
2439         return CI.isRegisterClass() && !CI.DiagnosticType.empty();
2440       })) {
2441     OS << "  return MCTargetAsmParser::Match_InvalidOperand;\n";
2442   } else {
2443     OS << "  switch (RegisterClass) {\n";
2444     for (const auto &CI: Info.Classes) {
2445       if (CI.isRegisterClass() && !CI.DiagnosticType.empty()) {
2446         OS << "  case " << CI.Name << ":\n";
2447         OS << "    return " << Info.Target.getName() << "AsmParser::Match_"
2448            << CI.DiagnosticType << ";\n";
2449       }
2450     }
2451 
2452     OS << "  default:\n";
2453     OS << "    return MCTargetAsmParser::Match_InvalidOperand;\n";
2454 
2455     OS << "  }\n";
2456   }
2457   OS << "}\n\n";
2458 }
2459 
2460 /// emitValidateOperandClass - Emit the function to validate an operand class.
2461 static void emitValidateOperandClass(AsmMatcherInfo &Info,
2462                                      raw_ostream &OS) {
2463   OS << "static unsigned validateOperandClass(MCParsedAsmOperand &GOp, "
2464      << "MatchClassKind Kind) {\n";
2465   OS << "  " << Info.Target.getName() << "Operand &Operand = ("
2466      << Info.Target.getName() << "Operand &)GOp;\n";
2467 
2468   // The InvalidMatchClass is not to match any operand.
2469   OS << "  if (Kind == InvalidMatchClass)\n";
2470   OS << "    return MCTargetAsmParser::Match_InvalidOperand;\n\n";
2471 
2472   // Check for Token operands first.
2473   // FIXME: Use a more specific diagnostic type.
2474   OS << "  if (Operand.isToken() && Kind <= MCK_LAST_TOKEN)\n";
2475   OS << "    return isSubclass(matchTokenString(Operand.getToken()), Kind) ?\n"
2476      << "             MCTargetAsmParser::Match_Success :\n"
2477      << "             MCTargetAsmParser::Match_InvalidOperand;\n\n";
2478 
2479   // Check the user classes. We don't care what order since we're only
2480   // actually matching against one of them.
2481   OS << "  switch (Kind) {\n"
2482         "  default: break;\n";
2483   for (const auto &CI : Info.Classes) {
2484     if (!CI.isUserClass())
2485       continue;
2486 
2487     OS << "  // '" << CI.ClassName << "' class\n";
2488     OS << "  case " << CI.Name << ": {\n";
2489     OS << "    DiagnosticPredicate DP(Operand." << CI.PredicateMethod
2490        << "());\n";
2491     OS << "    if (DP.isMatch())\n";
2492     OS << "      return MCTargetAsmParser::Match_Success;\n";
2493     if (!CI.DiagnosticType.empty()) {
2494       OS << "    if (DP.isNearMatch())\n";
2495       OS << "      return " << Info.Target.getName() << "AsmParser::Match_"
2496          << CI.DiagnosticType << ";\n";
2497       OS << "    break;\n";
2498     }
2499     else
2500       OS << "    break;\n";
2501     OS << "    }\n";
2502   }
2503   OS << "  } // end switch (Kind)\n\n";
2504 
2505   // Check for register operands, including sub-classes.
2506   OS << "  if (Operand.isReg()) {\n";
2507   OS << "    MatchClassKind OpKind;\n";
2508   OS << "    switch (Operand.getReg()) {\n";
2509   OS << "    default: OpKind = InvalidMatchClass; break;\n";
2510   for (const auto &RC : Info.RegisterClasses)
2511     OS << "    case " << RC.first->getValueAsString("Namespace") << "::"
2512        << RC.first->getName() << ": OpKind = " << RC.second->Name
2513        << "; break;\n";
2514   OS << "    }\n";
2515   OS << "    return isSubclass(OpKind, Kind) ? "
2516      << "(unsigned)MCTargetAsmParser::Match_Success :\n                     "
2517      << "                 getDiagKindFromRegisterClass(Kind);\n  }\n\n";
2518 
2519   // Expected operand is a register, but actual is not.
2520   OS << "  if (Kind > MCK_LAST_TOKEN && Kind <= MCK_LAST_REGISTER)\n";
2521   OS << "    return getDiagKindFromRegisterClass(Kind);\n\n";
2522 
2523   // Generic fallthrough match failure case for operands that don't have
2524   // specialized diagnostic types.
2525   OS << "  return MCTargetAsmParser::Match_InvalidOperand;\n";
2526   OS << "}\n\n";
2527 }
2528 
2529 /// emitIsSubclass - Emit the subclass predicate function.
2530 static void emitIsSubclass(CodeGenTarget &Target,
2531                            std::forward_list<ClassInfo> &Infos,
2532                            raw_ostream &OS) {
2533   OS << "/// isSubclass - Compute whether \\p A is a subclass of \\p B.\n";
2534   OS << "static bool isSubclass(MatchClassKind A, MatchClassKind B) {\n";
2535   OS << "  if (A == B)\n";
2536   OS << "    return true;\n\n";
2537 
2538   bool EmittedSwitch = false;
2539   for (const auto &A : Infos) {
2540     std::vector<StringRef> SuperClasses;
2541     if (A.IsOptional)
2542       SuperClasses.push_back("OptionalMatchClass");
2543     for (const auto &B : Infos) {
2544       if (&A != &B && A.isSubsetOf(B))
2545         SuperClasses.push_back(B.Name);
2546     }
2547 
2548     if (SuperClasses.empty())
2549       continue;
2550 
2551     // If this is the first SuperClass, emit the switch header.
2552     if (!EmittedSwitch) {
2553       OS << "  switch (A) {\n";
2554       OS << "  default:\n";
2555       OS << "    return false;\n";
2556       EmittedSwitch = true;
2557     }
2558 
2559     OS << "\n  case " << A.Name << ":\n";
2560 
2561     if (SuperClasses.size() == 1) {
2562       OS << "    return B == " << SuperClasses.back() << ";\n";
2563       continue;
2564     }
2565 
2566     if (!SuperClasses.empty()) {
2567       OS << "    switch (B) {\n";
2568       OS << "    default: return false;\n";
2569       for (StringRef SC : SuperClasses)
2570         OS << "    case " << SC << ": return true;\n";
2571       OS << "    }\n";
2572     } else {
2573       // No case statement to emit
2574       OS << "    return false;\n";
2575     }
2576   }
2577 
2578   // If there were case statements emitted into the string stream write the
2579   // default.
2580   if (EmittedSwitch)
2581     OS << "  }\n";
2582   else
2583     OS << "  return false;\n";
2584 
2585   OS << "}\n\n";
2586 }
2587 
2588 /// emitMatchTokenString - Emit the function to match a token string to the
2589 /// appropriate match class value.
2590 static void emitMatchTokenString(CodeGenTarget &Target,
2591                                  std::forward_list<ClassInfo> &Infos,
2592                                  raw_ostream &OS) {
2593   // Construct the match list.
2594   std::vector<StringMatcher::StringPair> Matches;
2595   for (const auto &CI : Infos) {
2596     if (CI.Kind == ClassInfo::Token)
2597       Matches.emplace_back(CI.ValueName, "return " + CI.Name + ";");
2598   }
2599 
2600   OS << "static MatchClassKind matchTokenString(StringRef Name) {\n";
2601 
2602   StringMatcher("Name", Matches, OS).Emit();
2603 
2604   OS << "  return InvalidMatchClass;\n";
2605   OS << "}\n\n";
2606 }
2607 
2608 /// emitMatchRegisterName - Emit the function to match a string to the target
2609 /// specific register enum.
2610 static void emitMatchRegisterName(CodeGenTarget &Target, Record *AsmParser,
2611                                   raw_ostream &OS) {
2612   // Construct the match list.
2613   std::vector<StringMatcher::StringPair> Matches;
2614   const auto &Regs = Target.getRegBank().getRegisters();
2615   for (const CodeGenRegister &Reg : Regs) {
2616     if (Reg.TheDef->getValueAsString("AsmName").empty())
2617       continue;
2618 
2619     Matches.emplace_back(std::string(Reg.TheDef->getValueAsString("AsmName")),
2620                          "return " + utostr(Reg.EnumValue) + ";");
2621   }
2622 
2623   OS << "static unsigned MatchRegisterName(StringRef Name) {\n";
2624 
2625   bool IgnoreDuplicates =
2626       AsmParser->getValueAsBit("AllowDuplicateRegisterNames");
2627   StringMatcher("Name", Matches, OS).Emit(0, IgnoreDuplicates);
2628 
2629   OS << "  return 0;\n";
2630   OS << "}\n\n";
2631 }
2632 
2633 /// Emit the function to match a string to the target
2634 /// specific register enum.
2635 static void emitMatchRegisterAltName(CodeGenTarget &Target, Record *AsmParser,
2636                                      raw_ostream &OS) {
2637   // Construct the match list.
2638   std::vector<StringMatcher::StringPair> Matches;
2639   const auto &Regs = Target.getRegBank().getRegisters();
2640   for (const CodeGenRegister &Reg : Regs) {
2641 
2642     auto AltNames = Reg.TheDef->getValueAsListOfStrings("AltNames");
2643 
2644     for (auto AltName : AltNames) {
2645       AltName = StringRef(AltName).trim();
2646 
2647       // don't handle empty alternative names
2648       if (AltName.empty())
2649         continue;
2650 
2651       Matches.emplace_back(std::string(AltName),
2652                            "return " + utostr(Reg.EnumValue) + ";");
2653     }
2654   }
2655 
2656   OS << "static unsigned MatchRegisterAltName(StringRef Name) {\n";
2657 
2658   bool IgnoreDuplicates =
2659       AsmParser->getValueAsBit("AllowDuplicateRegisterNames");
2660   StringMatcher("Name", Matches, OS).Emit(0, IgnoreDuplicates);
2661 
2662   OS << "  return 0;\n";
2663   OS << "}\n\n";
2664 }
2665 
2666 /// emitOperandDiagnosticTypes - Emit the operand matching diagnostic types.
2667 static void emitOperandDiagnosticTypes(AsmMatcherInfo &Info, raw_ostream &OS) {
2668   // Get the set of diagnostic types from all of the operand classes.
2669   std::set<StringRef> Types;
2670   for (const auto &OpClassEntry : Info.AsmOperandClasses) {
2671     if (!OpClassEntry.second->DiagnosticType.empty())
2672       Types.insert(OpClassEntry.second->DiagnosticType);
2673   }
2674   for (const auto &OpClassEntry : Info.RegisterClassClasses) {
2675     if (!OpClassEntry.second->DiagnosticType.empty())
2676       Types.insert(OpClassEntry.second->DiagnosticType);
2677   }
2678 
2679   if (Types.empty()) return;
2680 
2681   // Now emit the enum entries.
2682   for (StringRef Type : Types)
2683     OS << "  Match_" << Type << ",\n";
2684   OS << "  END_OPERAND_DIAGNOSTIC_TYPES\n";
2685 }
2686 
2687 /// emitGetSubtargetFeatureName - Emit the helper function to get the
2688 /// user-level name for a subtarget feature.
2689 static void emitGetSubtargetFeatureName(AsmMatcherInfo &Info, raw_ostream &OS) {
2690   OS << "// User-level names for subtarget features that participate in\n"
2691      << "// instruction matching.\n"
2692      << "static const char *getSubtargetFeatureName(uint64_t Val) {\n";
2693   if (!Info.SubtargetFeatures.empty()) {
2694     OS << "  switch(Val) {\n";
2695     for (const auto &SF : Info.SubtargetFeatures) {
2696       const SubtargetFeatureInfo &SFI = SF.second;
2697       // FIXME: Totally just a placeholder name to get the algorithm working.
2698       OS << "  case " << SFI.getEnumBitName() << ": return \""
2699          << SFI.TheDef->getValueAsString("PredicateName") << "\";\n";
2700     }
2701     OS << "  default: return \"(unknown)\";\n";
2702     OS << "  }\n";
2703   } else {
2704     // Nothing to emit, so skip the switch
2705     OS << "  return \"(unknown)\";\n";
2706   }
2707   OS << "}\n\n";
2708 }
2709 
2710 static std::string GetAliasRequiredFeatures(Record *R,
2711                                             const AsmMatcherInfo &Info) {
2712   std::vector<Record*> ReqFeatures = R->getValueAsListOfDefs("Predicates");
2713   std::string Result;
2714 
2715   if (ReqFeatures.empty())
2716     return Result;
2717 
2718   for (unsigned i = 0, e = ReqFeatures.size(); i != e; ++i) {
2719     const SubtargetFeatureInfo *F = Info.getSubtargetFeature(ReqFeatures[i]);
2720 
2721     if (!F)
2722       PrintFatalError(R->getLoc(), "Predicate '" + ReqFeatures[i]->getName() +
2723                     "' is not marked as an AssemblerPredicate!");
2724 
2725     if (i)
2726       Result += " && ";
2727 
2728     Result += "Features.test(" + F->getEnumBitName() + ')';
2729   }
2730 
2731   return Result;
2732 }
2733 
2734 static void emitMnemonicAliasVariant(raw_ostream &OS,const AsmMatcherInfo &Info,
2735                                      std::vector<Record*> &Aliases,
2736                                      unsigned Indent = 0,
2737                                   StringRef AsmParserVariantName = StringRef()){
2738   // Keep track of all the aliases from a mnemonic.  Use an std::map so that the
2739   // iteration order of the map is stable.
2740   std::map<std::string, std::vector<Record*> > AliasesFromMnemonic;
2741 
2742   for (Record *R : Aliases) {
2743     // FIXME: Allow AssemblerVariantName to be a comma separated list.
2744     StringRef AsmVariantName = R->getValueAsString("AsmVariantName");
2745     if (AsmVariantName != AsmParserVariantName)
2746       continue;
2747     AliasesFromMnemonic[R->getValueAsString("FromMnemonic").lower()]
2748         .push_back(R);
2749   }
2750   if (AliasesFromMnemonic.empty())
2751     return;
2752 
2753   // Process each alias a "from" mnemonic at a time, building the code executed
2754   // by the string remapper.
2755   std::vector<StringMatcher::StringPair> Cases;
2756   for (const auto &AliasEntry : AliasesFromMnemonic) {
2757     const std::vector<Record*> &ToVec = AliasEntry.second;
2758 
2759     // Loop through each alias and emit code that handles each case.  If there
2760     // are two instructions without predicates, emit an error.  If there is one,
2761     // emit it last.
2762     std::string MatchCode;
2763     int AliasWithNoPredicate = -1;
2764 
2765     for (unsigned i = 0, e = ToVec.size(); i != e; ++i) {
2766       Record *R = ToVec[i];
2767       std::string FeatureMask = GetAliasRequiredFeatures(R, Info);
2768 
2769       // If this unconditionally matches, remember it for later and diagnose
2770       // duplicates.
2771       if (FeatureMask.empty()) {
2772         if (AliasWithNoPredicate != -1 &&
2773             R->getValueAsString("ToMnemonic") !=
2774                 ToVec[AliasWithNoPredicate]->getValueAsString("ToMnemonic")) {
2775           // We can't have two different aliases from the same mnemonic with no
2776           // predicate.
2777           PrintError(
2778               ToVec[AliasWithNoPredicate]->getLoc(),
2779               "two different MnemonicAliases with the same 'from' mnemonic!");
2780           PrintFatalError(R->getLoc(), "this is the other MnemonicAlias.");
2781         }
2782 
2783         AliasWithNoPredicate = i;
2784         continue;
2785       }
2786       if (R->getValueAsString("ToMnemonic") == AliasEntry.first)
2787         PrintFatalError(R->getLoc(), "MnemonicAlias to the same string");
2788 
2789       if (!MatchCode.empty())
2790         MatchCode += "else ";
2791       MatchCode += "if (" + FeatureMask + ")\n";
2792       MatchCode += "  Mnemonic = \"";
2793       MatchCode += R->getValueAsString("ToMnemonic").lower();
2794       MatchCode += "\";\n";
2795     }
2796 
2797     if (AliasWithNoPredicate != -1) {
2798       Record *R = ToVec[AliasWithNoPredicate];
2799       if (!MatchCode.empty())
2800         MatchCode += "else\n  ";
2801       MatchCode += "Mnemonic = \"";
2802       MatchCode += R->getValueAsString("ToMnemonic").lower();
2803       MatchCode += "\";\n";
2804     }
2805 
2806     MatchCode += "return;";
2807 
2808     Cases.push_back(std::make_pair(AliasEntry.first, MatchCode));
2809   }
2810   StringMatcher("Mnemonic", Cases, OS).Emit(Indent);
2811 }
2812 
2813 /// emitMnemonicAliases - If the target has any MnemonicAlias<> definitions,
2814 /// emit a function for them and return true, otherwise return false.
2815 static bool emitMnemonicAliases(raw_ostream &OS, const AsmMatcherInfo &Info,
2816                                 CodeGenTarget &Target) {
2817   // Ignore aliases when match-prefix is set.
2818   if (!MatchPrefix.empty())
2819     return false;
2820 
2821   std::vector<Record*> Aliases =
2822     Info.getRecords().getAllDerivedDefinitions("MnemonicAlias");
2823   if (Aliases.empty()) return false;
2824 
2825   OS << "static void applyMnemonicAliases(StringRef &Mnemonic, "
2826     "const FeatureBitset &Features, unsigned VariantID) {\n";
2827   OS << "  switch (VariantID) {\n";
2828   unsigned VariantCount = Target.getAsmParserVariantCount();
2829   for (unsigned VC = 0; VC != VariantCount; ++VC) {
2830     Record *AsmVariant = Target.getAsmParserVariant(VC);
2831     int AsmParserVariantNo = AsmVariant->getValueAsInt("Variant");
2832     StringRef AsmParserVariantName = AsmVariant->getValueAsString("Name");
2833     OS << "  case " << AsmParserVariantNo << ":\n";
2834     emitMnemonicAliasVariant(OS, Info, Aliases, /*Indent=*/2,
2835                              AsmParserVariantName);
2836     OS << "    break;\n";
2837   }
2838   OS << "  }\n";
2839 
2840   // Emit aliases that apply to all variants.
2841   emitMnemonicAliasVariant(OS, Info, Aliases);
2842 
2843   OS << "}\n\n";
2844 
2845   return true;
2846 }
2847 
2848 static void
2849 emitCustomOperandParsing(raw_ostream &OS, CodeGenTarget &Target,
2850                          const AsmMatcherInfo &Info, StringRef ClassName,
2851                          StringToOffsetTable &StringTable,
2852                          unsigned MaxMnemonicIndex, unsigned MaxFeaturesIndex,
2853                          bool HasMnemonicFirst, const Record &AsmParser) {
2854   unsigned MaxMask = 0;
2855   for (const OperandMatchEntry &OMI : Info.OperandMatchInfo) {
2856     MaxMask |= OMI.OperandMask;
2857   }
2858 
2859   // Emit the static custom operand parsing table;
2860   OS << "namespace {\n";
2861   OS << "  struct OperandMatchEntry {\n";
2862   OS << "    " << getMinimalTypeForRange(MaxMnemonicIndex)
2863                << " Mnemonic;\n";
2864   OS << "    " << getMinimalTypeForRange(MaxMask)
2865                << " OperandMask;\n";
2866   OS << "    " << getMinimalTypeForRange(std::distance(
2867                       Info.Classes.begin(), Info.Classes.end())) << " Class;\n";
2868   OS << "    " << getMinimalTypeForRange(MaxFeaturesIndex)
2869                << " RequiredFeaturesIdx;\n\n";
2870   OS << "    StringRef getMnemonic() const {\n";
2871   OS << "      return StringRef(MnemonicTable + Mnemonic + 1,\n";
2872   OS << "                       MnemonicTable[Mnemonic]);\n";
2873   OS << "    }\n";
2874   OS << "  };\n\n";
2875 
2876   OS << "  // Predicate for searching for an opcode.\n";
2877   OS << "  struct LessOpcodeOperand {\n";
2878   OS << "    bool operator()(const OperandMatchEntry &LHS, StringRef RHS) {\n";
2879   OS << "      return LHS.getMnemonic()  < RHS;\n";
2880   OS << "    }\n";
2881   OS << "    bool operator()(StringRef LHS, const OperandMatchEntry &RHS) {\n";
2882   OS << "      return LHS < RHS.getMnemonic();\n";
2883   OS << "    }\n";
2884   OS << "    bool operator()(const OperandMatchEntry &LHS,";
2885   OS << " const OperandMatchEntry &RHS) {\n";
2886   OS << "      return LHS.getMnemonic() < RHS.getMnemonic();\n";
2887   OS << "    }\n";
2888   OS << "  };\n";
2889 
2890   OS << "} // end anonymous namespace\n\n";
2891 
2892   OS << "static const OperandMatchEntry OperandMatchTable["
2893      << Info.OperandMatchInfo.size() << "] = {\n";
2894 
2895   OS << "  /* Operand List Mnemonic, Mask, Operand Class, Features */\n";
2896   for (const OperandMatchEntry &OMI : Info.OperandMatchInfo) {
2897     const MatchableInfo &II = *OMI.MI;
2898 
2899     OS << "  { ";
2900 
2901     // Store a pascal-style length byte in the mnemonic.
2902     std::string LenMnemonic = char(II.Mnemonic.size()) + II.Mnemonic.lower();
2903     OS << StringTable.GetOrAddStringOffset(LenMnemonic, false)
2904        << " /* " << II.Mnemonic << " */, ";
2905 
2906     OS << OMI.OperandMask;
2907     OS << " /* ";
2908     ListSeparator LS;
2909     for (int i = 0, e = 31; i !=e; ++i)
2910       if (OMI.OperandMask & (1 << i))
2911         OS << LS << i;
2912     OS << " */, ";
2913 
2914     OS << OMI.CI->Name;
2915 
2916     // Write the required features mask.
2917     OS << ", AMFBS";
2918     if (II.RequiredFeatures.empty())
2919       OS << "_None";
2920     else
2921       for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i)
2922         OS << '_' << II.RequiredFeatures[i]->TheDef->getName();
2923 
2924     OS << " },\n";
2925   }
2926   OS << "};\n\n";
2927 
2928   // Emit the operand class switch to call the correct custom parser for
2929   // the found operand class.
2930   OS << "ParseStatus " << Target.getName() << ClassName << "::\n"
2931      << "tryCustomParseOperand(OperandVector"
2932      << " &Operands,\n                      unsigned MCK) {\n\n"
2933      << "  switch(MCK) {\n";
2934 
2935   for (const auto &CI : Info.Classes) {
2936     if (CI.ParserMethod.empty())
2937       continue;
2938     OS << "  case " << CI.Name << ":\n"
2939        << "    return " << CI.ParserMethod << "(Operands);\n";
2940   }
2941 
2942   OS << "  default:\n";
2943   OS << "    return ParseStatus::NoMatch;\n";
2944   OS << "  }\n";
2945   OS << "  return ParseStatus::NoMatch;\n";
2946   OS << "}\n\n";
2947 
2948   // Emit the static custom operand parser. This code is very similar with
2949   // the other matcher. Also use MatchResultTy here just in case we go for
2950   // a better error handling.
2951   OS << "ParseStatus " << Target.getName() << ClassName << "::\n"
2952      << "MatchOperandParserImpl(OperandVector"
2953      << " &Operands,\n                       StringRef Mnemonic,\n"
2954      << "                       bool ParseForAllFeatures) {\n";
2955 
2956   // Emit code to get the available features.
2957   OS << "  // Get the current feature set.\n";
2958   OS << "  const FeatureBitset &AvailableFeatures = getAvailableFeatures();\n\n";
2959 
2960   OS << "  // Get the next operand index.\n";
2961   OS << "  unsigned NextOpNum = Operands.size()"
2962      << (HasMnemonicFirst ? " - 1" : "") << ";\n";
2963 
2964   // Emit code to search the table.
2965   OS << "  // Search the table.\n";
2966   if (HasMnemonicFirst) {
2967     OS << "  auto MnemonicRange =\n";
2968     OS << "    std::equal_range(std::begin(OperandMatchTable), "
2969           "std::end(OperandMatchTable),\n";
2970     OS << "                     Mnemonic, LessOpcodeOperand());\n\n";
2971   } else {
2972     OS << "  auto MnemonicRange = std::make_pair(std::begin(OperandMatchTable),"
2973           " std::end(OperandMatchTable));\n";
2974     OS << "  if (!Mnemonic.empty())\n";
2975     OS << "    MnemonicRange =\n";
2976     OS << "      std::equal_range(std::begin(OperandMatchTable), "
2977           "std::end(OperandMatchTable),\n";
2978     OS << "                       Mnemonic, LessOpcodeOperand());\n\n";
2979   }
2980 
2981   OS << "  if (MnemonicRange.first == MnemonicRange.second)\n";
2982   OS << "    return ParseStatus::NoMatch;\n\n";
2983 
2984   OS << "  for (const OperandMatchEntry *it = MnemonicRange.first,\n"
2985      << "       *ie = MnemonicRange.second; it != ie; ++it) {\n";
2986 
2987   OS << "    // equal_range guarantees that instruction mnemonic matches.\n";
2988   OS << "    assert(Mnemonic == it->getMnemonic());\n\n";
2989 
2990   // Emit check that the required features are available.
2991   OS << "    // check if the available features match\n";
2992   OS << "    const FeatureBitset &RequiredFeatures = "
2993         "FeatureBitsets[it->RequiredFeaturesIdx];\n";
2994   OS << "    if (!ParseForAllFeatures && (AvailableFeatures & "
2995         "RequiredFeatures) != RequiredFeatures)\n";
2996   OS << "      continue;\n\n";
2997 
2998   // Emit check to ensure the operand number matches.
2999   OS << "    // check if the operand in question has a custom parser.\n";
3000   OS << "    if (!(it->OperandMask & (1 << NextOpNum)))\n";
3001   OS << "      continue;\n\n";
3002 
3003   // Emit call to the custom parser method
3004   StringRef ParserName = AsmParser.getValueAsString("OperandParserMethod");
3005   if (ParserName.empty())
3006     ParserName = "tryCustomParseOperand";
3007   OS << "    // call custom parse method to handle the operand\n";
3008   OS << "    ParseStatus Result = " << ParserName << "(Operands, it->Class);\n";
3009   OS << "    if (!Result.isNoMatch())\n";
3010   OS << "      return Result;\n";
3011   OS << "  }\n\n";
3012 
3013   OS << "  // Okay, we had no match.\n";
3014   OS << "  return ParseStatus::NoMatch;\n";
3015   OS << "}\n\n";
3016 }
3017 
3018 static void emitAsmTiedOperandConstraints(CodeGenTarget &Target,
3019                                           AsmMatcherInfo &Info,
3020                                           raw_ostream &OS) {
3021   std::string AsmParserName =
3022       std::string(Info.AsmParser->getValueAsString("AsmParserClassName"));
3023   OS << "static bool ";
3024   OS << "checkAsmTiedOperandConstraints(const " << Target.getName()
3025      << AsmParserName << "&AsmParser,\n";
3026   OS << "                               unsigned Kind,\n";
3027   OS << "                               const OperandVector &Operands,\n";
3028   OS << "                               uint64_t &ErrorInfo) {\n";
3029   OS << "  assert(Kind < CVT_NUM_SIGNATURES && \"Invalid signature!\");\n";
3030   OS << "  const uint8_t *Converter = ConversionTable[Kind];\n";
3031   OS << "  for (const uint8_t *p = Converter; *p; p += 2) {\n";
3032   OS << "    switch (*p) {\n";
3033   OS << "    case CVT_Tied: {\n";
3034   OS << "      unsigned OpIdx = *(p + 1);\n";
3035   OS << "      assert(OpIdx < (size_t)(std::end(TiedAsmOperandTable) -\n";
3036   OS << "                              std::begin(TiedAsmOperandTable)) &&\n";
3037   OS << "             \"Tied operand not found\");\n";
3038   OS << "      unsigned OpndNum1 = TiedAsmOperandTable[OpIdx][1];\n";
3039   OS << "      unsigned OpndNum2 = TiedAsmOperandTable[OpIdx][2];\n";
3040   OS << "      if (OpndNum1 != OpndNum2) {\n";
3041   OS << "        auto &SrcOp1 = Operands[OpndNum1];\n";
3042   OS << "        auto &SrcOp2 = Operands[OpndNum2];\n";
3043   OS << "        if (!AsmParser.areEqualRegs(*SrcOp1, *SrcOp2)) {\n";
3044   OS << "          ErrorInfo = OpndNum2;\n";
3045   OS << "          return false;\n";
3046   OS << "        }\n";
3047   OS << "      }\n";
3048   OS << "      break;\n";
3049   OS << "    }\n";
3050   OS << "    default:\n";
3051   OS << "      break;\n";
3052   OS << "    }\n";
3053   OS << "  }\n";
3054   OS << "  return true;\n";
3055   OS << "}\n\n";
3056 }
3057 
3058 static void emitMnemonicSpellChecker(raw_ostream &OS, CodeGenTarget &Target,
3059                                      unsigned VariantCount) {
3060   OS << "static std::string " << Target.getName()
3061      << "MnemonicSpellCheck(StringRef S, const FeatureBitset &FBS,"
3062      << " unsigned VariantID) {\n";
3063   if (!VariantCount)
3064     OS <<  "  return \"\";";
3065   else {
3066     OS << "  const unsigned MaxEditDist = 2;\n";
3067     OS << "  std::vector<StringRef> Candidates;\n";
3068     OS << "  StringRef Prev = \"\";\n\n";
3069 
3070     OS << "  // Find the appropriate table for this asm variant.\n";
3071     OS << "  const MatchEntry *Start, *End;\n";
3072     OS << "  switch (VariantID) {\n";
3073     OS << "  default: llvm_unreachable(\"invalid variant!\");\n";
3074     for (unsigned VC = 0; VC != VariantCount; ++VC) {
3075       Record *AsmVariant = Target.getAsmParserVariant(VC);
3076       int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
3077       OS << "  case " << AsmVariantNo << ": Start = std::begin(MatchTable" << VC
3078          << "); End = std::end(MatchTable" << VC << "); break;\n";
3079     }
3080     OS << "  }\n\n";
3081     OS << "  for (auto I = Start; I < End; I++) {\n";
3082     OS << "    // Ignore unsupported instructions.\n";
3083     OS << "    const FeatureBitset &RequiredFeatures = "
3084           "FeatureBitsets[I->RequiredFeaturesIdx];\n";
3085     OS << "    if ((FBS & RequiredFeatures) != RequiredFeatures)\n";
3086     OS << "      continue;\n";
3087     OS << "\n";
3088     OS << "    StringRef T = I->getMnemonic();\n";
3089     OS << "    // Avoid recomputing the edit distance for the same string.\n";
3090     OS << "    if (T.equals(Prev))\n";
3091     OS << "      continue;\n";
3092     OS << "\n";
3093     OS << "    Prev = T;\n";
3094     OS << "    unsigned Dist = S.edit_distance(T, false, MaxEditDist);\n";
3095     OS << "    if (Dist <= MaxEditDist)\n";
3096     OS << "      Candidates.push_back(T);\n";
3097     OS << "  }\n";
3098     OS << "\n";
3099     OS << "  if (Candidates.empty())\n";
3100     OS << "    return \"\";\n";
3101     OS << "\n";
3102     OS << "  std::string Res = \", did you mean: \";\n";
3103     OS << "  unsigned i = 0;\n";
3104     OS << "  for (; i < Candidates.size() - 1; i++)\n";
3105     OS << "    Res += Candidates[i].str() + \", \";\n";
3106     OS << "  return Res + Candidates[i].str() + \"?\";\n";
3107   }
3108   OS << "}\n";
3109   OS << "\n";
3110 }
3111 
3112 static void emitMnemonicChecker(raw_ostream &OS,
3113                                 CodeGenTarget &Target,
3114                                 unsigned VariantCount,
3115                                 bool HasMnemonicFirst,
3116                                 bool HasMnemonicAliases) {
3117   OS << "static bool " << Target.getName()
3118      << "CheckMnemonic(StringRef Mnemonic,\n";
3119   OS << "                                "
3120      << "const FeatureBitset &AvailableFeatures,\n";
3121   OS << "                                "
3122      << "unsigned VariantID) {\n";
3123 
3124   if (!VariantCount) {
3125     OS <<  "  return false;\n";
3126   } else {
3127     if (HasMnemonicAliases) {
3128       OS << "  // Process all MnemonicAliases to remap the mnemonic.\n";
3129       OS << "  applyMnemonicAliases(Mnemonic, AvailableFeatures, VariantID);";
3130       OS << "\n\n";
3131     }
3132     OS << "  // Find the appropriate table for this asm variant.\n";
3133     OS << "  const MatchEntry *Start, *End;\n";
3134     OS << "  switch (VariantID) {\n";
3135     OS << "  default: llvm_unreachable(\"invalid variant!\");\n";
3136     for (unsigned VC = 0; VC != VariantCount; ++VC) {
3137       Record *AsmVariant = Target.getAsmParserVariant(VC);
3138       int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
3139       OS << "  case " << AsmVariantNo << ": Start = std::begin(MatchTable" << VC
3140          << "); End = std::end(MatchTable" << VC << "); break;\n";
3141     }
3142     OS << "  }\n\n";
3143 
3144     OS << "  // Search the table.\n";
3145     if (HasMnemonicFirst) {
3146       OS << "  auto MnemonicRange = "
3147             "std::equal_range(Start, End, Mnemonic, LessOpcode());\n\n";
3148     } else {
3149       OS << "  auto MnemonicRange = std::make_pair(Start, End);\n";
3150       OS << "  unsigned SIndex = Mnemonic.empty() ? 0 : 1;\n";
3151       OS << "  if (!Mnemonic.empty())\n";
3152       OS << "    MnemonicRange = "
3153          << "std::equal_range(Start, End, Mnemonic.lower(), LessOpcode());\n\n";
3154     }
3155 
3156     OS << "  if (MnemonicRange.first == MnemonicRange.second)\n";
3157     OS << "    return false;\n\n";
3158 
3159     OS << "  for (const MatchEntry *it = MnemonicRange.first, "
3160        << "*ie = MnemonicRange.second;\n";
3161     OS << "       it != ie; ++it) {\n";
3162     OS << "    const FeatureBitset &RequiredFeatures =\n";
3163     OS << "      FeatureBitsets[it->RequiredFeaturesIdx];\n";
3164     OS << "    if ((AvailableFeatures & RequiredFeatures) == ";
3165     OS << "RequiredFeatures)\n";
3166     OS << "      return true;\n";
3167     OS << "  }\n";
3168     OS << "  return false;\n";
3169   }
3170   OS << "}\n";
3171   OS << "\n";
3172 }
3173 
3174 // Emit a function mapping match classes to strings, for debugging.
3175 static void emitMatchClassKindNames(std::forward_list<ClassInfo> &Infos,
3176                                     raw_ostream &OS) {
3177   OS << "#ifndef NDEBUG\n";
3178   OS << "const char *getMatchClassName(MatchClassKind Kind) {\n";
3179   OS << "  switch (Kind) {\n";
3180 
3181   OS << "  case InvalidMatchClass: return \"InvalidMatchClass\";\n";
3182   OS << "  case OptionalMatchClass: return \"OptionalMatchClass\";\n";
3183   for (const auto &CI : Infos) {
3184     OS << "  case " << CI.Name << ": return \"" << CI.Name << "\";\n";
3185   }
3186   OS << "  case NumMatchClassKinds: return \"NumMatchClassKinds\";\n";
3187 
3188   OS << "  }\n";
3189   OS << "  llvm_unreachable(\"unhandled MatchClassKind!\");\n";
3190   OS << "}\n\n";
3191   OS << "#endif // NDEBUG\n";
3192 }
3193 
3194 static std::string
3195 getNameForFeatureBitset(const std::vector<Record *> &FeatureBitset) {
3196   std::string Name = "AMFBS";
3197   for (const auto &Feature : FeatureBitset)
3198     Name += ("_" + Feature->getName()).str();
3199   return Name;
3200 }
3201 
3202 void AsmMatcherEmitter::run(raw_ostream &OS) {
3203   CodeGenTarget Target(Records);
3204   Record *AsmParser = Target.getAsmParser();
3205   StringRef ClassName = AsmParser->getValueAsString("AsmParserClassName");
3206 
3207   emitSourceFileHeader("Assembly Matcher Source Fragment", OS, Records);
3208 
3209   // Compute the information on the instructions to match.
3210   AsmMatcherInfo Info(AsmParser, Target, Records);
3211   Info.buildInfo();
3212 
3213   // Sort the instruction table using the partial order on classes. We use
3214   // stable_sort to ensure that ambiguous instructions are still
3215   // deterministically ordered.
3216   llvm::stable_sort(
3217       Info.Matchables,
3218       [](const std::unique_ptr<MatchableInfo> &a,
3219          const std::unique_ptr<MatchableInfo> &b) { return *a < *b; });
3220 
3221 #ifdef EXPENSIVE_CHECKS
3222   // Verify that the table is sorted and operator < works transitively.
3223   for (auto I = Info.Matchables.begin(), E = Info.Matchables.end(); I != E;
3224        ++I) {
3225     for (auto J = I; J != E; ++J) {
3226       assert(!(**J < **I));
3227     }
3228   }
3229 #endif
3230 
3231   DEBUG_WITH_TYPE("instruction_info", {
3232       for (const auto &MI : Info.Matchables)
3233         MI->dump();
3234     });
3235 
3236   // Check for ambiguous matchables.
3237   DEBUG_WITH_TYPE("ambiguous_instrs", {
3238     unsigned NumAmbiguous = 0;
3239     for (auto I = Info.Matchables.begin(), E = Info.Matchables.end(); I != E;
3240          ++I) {
3241       for (auto J = std::next(I); J != E; ++J) {
3242         const MatchableInfo &A = **I;
3243         const MatchableInfo &B = **J;
3244 
3245         if (A.couldMatchAmbiguouslyWith(B)) {
3246           errs() << "warning: ambiguous matchables:\n";
3247           A.dump();
3248           errs() << "\nis incomparable with:\n";
3249           B.dump();
3250           errs() << "\n\n";
3251           ++NumAmbiguous;
3252         }
3253       }
3254     }
3255     if (NumAmbiguous)
3256       errs() << "warning: " << NumAmbiguous
3257              << " ambiguous matchables!\n";
3258   });
3259 
3260   // Compute the information on the custom operand parsing.
3261   Info.buildOperandMatchInfo();
3262 
3263   bool HasMnemonicFirst = AsmParser->getValueAsBit("HasMnemonicFirst");
3264   bool HasOptionalOperands = Info.hasOptionalOperands();
3265   bool ReportMultipleNearMisses =
3266       AsmParser->getValueAsBit("ReportMultipleNearMisses");
3267 
3268   // Write the output.
3269 
3270   // Information for the class declaration.
3271   OS << "\n#ifdef GET_ASSEMBLER_HEADER\n";
3272   OS << "#undef GET_ASSEMBLER_HEADER\n";
3273   OS << "  // This should be included into the middle of the declaration of\n";
3274   OS << "  // your subclasses implementation of MCTargetAsmParser.\n";
3275   OS << "  FeatureBitset ComputeAvailableFeatures(const FeatureBitset &FB) const;\n";
3276   if (HasOptionalOperands) {
3277     OS << "  void convertToMCInst(unsigned Kind, MCInst &Inst, "
3278        << "unsigned Opcode,\n"
3279        << "                       const OperandVector &Operands,\n"
3280        << "                       const SmallBitVector &OptionalOperandsMask);\n";
3281   } else {
3282     OS << "  void convertToMCInst(unsigned Kind, MCInst &Inst, "
3283        << "unsigned Opcode,\n"
3284        << "                       const OperandVector &Operands);\n";
3285   }
3286   OS << "  void convertToMapAndConstraints(unsigned Kind,\n                ";
3287   OS << "           const OperandVector &Operands) override;\n";
3288   OS << "  unsigned MatchInstructionImpl(const OperandVector &Operands,\n"
3289      << "                                MCInst &Inst,\n";
3290   if (ReportMultipleNearMisses)
3291     OS << "                                SmallVectorImpl<NearMissInfo> *NearMisses,\n";
3292   else
3293     OS << "                                uint64_t &ErrorInfo,\n"
3294        << "                                FeatureBitset &MissingFeatures,\n";
3295   OS << "                                bool matchingInlineAsm,\n"
3296      << "                                unsigned VariantID = 0);\n";
3297   if (!ReportMultipleNearMisses)
3298     OS << "  unsigned MatchInstructionImpl(const OperandVector &Operands,\n"
3299        << "                                MCInst &Inst,\n"
3300        << "                                uint64_t &ErrorInfo,\n"
3301        << "                                bool matchingInlineAsm,\n"
3302        << "                                unsigned VariantID = 0) {\n"
3303        << "    FeatureBitset MissingFeatures;\n"
3304        << "    return MatchInstructionImpl(Operands, Inst, ErrorInfo, MissingFeatures,\n"
3305        << "                                matchingInlineAsm, VariantID);\n"
3306        << "  }\n\n";
3307 
3308 
3309   if (!Info.OperandMatchInfo.empty()) {
3310     OS << "  ParseStatus MatchOperandParserImpl(\n";
3311     OS << "    OperandVector &Operands,\n";
3312     OS << "    StringRef Mnemonic,\n";
3313     OS << "    bool ParseForAllFeatures = false);\n";
3314 
3315     OS << "  ParseStatus tryCustomParseOperand(\n";
3316     OS << "    OperandVector &Operands,\n";
3317     OS << "    unsigned MCK);\n\n";
3318   }
3319 
3320   OS << "#endif // GET_ASSEMBLER_HEADER\n\n";
3321 
3322   // Emit the operand match diagnostic enum names.
3323   OS << "\n#ifdef GET_OPERAND_DIAGNOSTIC_TYPES\n";
3324   OS << "#undef GET_OPERAND_DIAGNOSTIC_TYPES\n\n";
3325   emitOperandDiagnosticTypes(Info, OS);
3326   OS << "#endif // GET_OPERAND_DIAGNOSTIC_TYPES\n\n";
3327 
3328   OS << "\n#ifdef GET_REGISTER_MATCHER\n";
3329   OS << "#undef GET_REGISTER_MATCHER\n\n";
3330 
3331   // Emit the subtarget feature enumeration.
3332   SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(
3333       Info.SubtargetFeatures, OS);
3334 
3335   // Emit the function to match a register name to number.
3336   // This should be omitted for Mips target
3337   if (AsmParser->getValueAsBit("ShouldEmitMatchRegisterName"))
3338     emitMatchRegisterName(Target, AsmParser, OS);
3339 
3340   if (AsmParser->getValueAsBit("ShouldEmitMatchRegisterAltName"))
3341     emitMatchRegisterAltName(Target, AsmParser, OS);
3342 
3343   OS << "#endif // GET_REGISTER_MATCHER\n\n";
3344 
3345   OS << "\n#ifdef GET_SUBTARGET_FEATURE_NAME\n";
3346   OS << "#undef GET_SUBTARGET_FEATURE_NAME\n\n";
3347 
3348   // Generate the helper function to get the names for subtarget features.
3349   emitGetSubtargetFeatureName(Info, OS);
3350 
3351   OS << "#endif // GET_SUBTARGET_FEATURE_NAME\n\n";
3352 
3353   OS << "\n#ifdef GET_MATCHER_IMPLEMENTATION\n";
3354   OS << "#undef GET_MATCHER_IMPLEMENTATION\n\n";
3355 
3356   // Generate the function that remaps for mnemonic aliases.
3357   bool HasMnemonicAliases = emitMnemonicAliases(OS, Info, Target);
3358 
3359   // Generate the convertToMCInst function to convert operands into an MCInst.
3360   // Also, generate the convertToMapAndConstraints function for MS-style inline
3361   // assembly.  The latter doesn't actually generate a MCInst.
3362   unsigned NumConverters = emitConvertFuncs(Target, ClassName, Info.Matchables,
3363                                             HasMnemonicFirst,
3364                                             HasOptionalOperands, OS);
3365 
3366   // Emit the enumeration for classes which participate in matching.
3367   emitMatchClassEnumeration(Target, Info.Classes, OS);
3368 
3369   // Emit a function to get the user-visible string to describe an operand
3370   // match failure in diagnostics.
3371   emitOperandMatchErrorDiagStrings(Info, OS);
3372 
3373   // Emit a function to map register classes to operand match failure codes.
3374   emitRegisterMatchErrorFunc(Info, OS);
3375 
3376   // Emit the routine to match token strings to their match class.
3377   emitMatchTokenString(Target, Info.Classes, OS);
3378 
3379   // Emit the subclass predicate routine.
3380   emitIsSubclass(Target, Info.Classes, OS);
3381 
3382   // Emit the routine to validate an operand against a match class.
3383   emitValidateOperandClass(Info, OS);
3384 
3385   emitMatchClassKindNames(Info.Classes, OS);
3386 
3387   // Emit the available features compute function.
3388   SubtargetFeatureInfo::emitComputeAssemblerAvailableFeatures(
3389       Info.Target.getName(), ClassName, "ComputeAvailableFeatures",
3390       Info.SubtargetFeatures, OS);
3391 
3392   if (!ReportMultipleNearMisses)
3393     emitAsmTiedOperandConstraints(Target, Info, OS);
3394 
3395   StringToOffsetTable StringTable;
3396 
3397   size_t MaxNumOperands = 0;
3398   unsigned MaxMnemonicIndex = 0;
3399   bool HasDeprecation = false;
3400   for (const auto &MI : Info.Matchables) {
3401     MaxNumOperands = std::max(MaxNumOperands, MI->AsmOperands.size());
3402     HasDeprecation |= MI->HasDeprecation;
3403 
3404     // Store a pascal-style length byte in the mnemonic.
3405     std::string LenMnemonic = char(MI->Mnemonic.size()) + MI->Mnemonic.lower();
3406     MaxMnemonicIndex = std::max(MaxMnemonicIndex,
3407                         StringTable.GetOrAddStringOffset(LenMnemonic, false));
3408   }
3409 
3410   OS << "static const char MnemonicTable[] =\n";
3411   StringTable.EmitString(OS);
3412   OS << ";\n\n";
3413 
3414   std::vector<std::vector<Record *>> FeatureBitsets;
3415   for (const auto &MI : Info.Matchables) {
3416     if (MI->RequiredFeatures.empty())
3417       continue;
3418     FeatureBitsets.emplace_back();
3419     for (unsigned I = 0, E = MI->RequiredFeatures.size(); I != E; ++I)
3420       FeatureBitsets.back().push_back(MI->RequiredFeatures[I]->TheDef);
3421   }
3422 
3423   llvm::sort(FeatureBitsets, [&](const std::vector<Record *> &A,
3424                                  const std::vector<Record *> &B) {
3425     if (A.size() < B.size())
3426       return true;
3427     if (A.size() > B.size())
3428       return false;
3429     for (auto Pair : zip(A, B)) {
3430       if (std::get<0>(Pair)->getName() < std::get<1>(Pair)->getName())
3431         return true;
3432       if (std::get<0>(Pair)->getName() > std::get<1>(Pair)->getName())
3433         return false;
3434     }
3435     return false;
3436   });
3437   FeatureBitsets.erase(
3438       std::unique(FeatureBitsets.begin(), FeatureBitsets.end()),
3439       FeatureBitsets.end());
3440   OS << "// Feature bitsets.\n"
3441      << "enum : " << getMinimalTypeForRange(FeatureBitsets.size()) << " {\n"
3442      << "  AMFBS_None,\n";
3443   for (const auto &FeatureBitset : FeatureBitsets) {
3444     if (FeatureBitset.empty())
3445       continue;
3446     OS << "  " << getNameForFeatureBitset(FeatureBitset) << ",\n";
3447   }
3448   OS << "};\n\n"
3449      << "static constexpr FeatureBitset FeatureBitsets[] = {\n"
3450      << "  {}, // AMFBS_None\n";
3451   for (const auto &FeatureBitset : FeatureBitsets) {
3452     if (FeatureBitset.empty())
3453       continue;
3454     OS << "  {";
3455     for (const auto &Feature : FeatureBitset) {
3456       const auto &I = Info.SubtargetFeatures.find(Feature);
3457       assert(I != Info.SubtargetFeatures.end() && "Didn't import predicate?");
3458       OS << I->second.getEnumBitName() << ", ";
3459     }
3460     OS << "},\n";
3461   }
3462   OS << "};\n\n";
3463 
3464   // Emit the static match table; unused classes get initialized to 0 which is
3465   // guaranteed to be InvalidMatchClass.
3466   //
3467   // FIXME: We can reduce the size of this table very easily. First, we change
3468   // it so that store the kinds in separate bit-fields for each index, which
3469   // only needs to be the max width used for classes at that index (we also need
3470   // to reject based on this during classification). If we then make sure to
3471   // order the match kinds appropriately (putting mnemonics last), then we
3472   // should only end up using a few bits for each class, especially the ones
3473   // following the mnemonic.
3474   OS << "namespace {\n";
3475   OS << "  struct MatchEntry {\n";
3476   OS << "    " << getMinimalTypeForRange(MaxMnemonicIndex)
3477                << " Mnemonic;\n";
3478   OS << "    uint16_t Opcode;\n";
3479   OS << "    " << getMinimalTypeForRange(NumConverters)
3480                << " ConvertFn;\n";
3481   OS << "    " << getMinimalTypeForRange(FeatureBitsets.size())
3482                << " RequiredFeaturesIdx;\n";
3483   OS << "    " << getMinimalTypeForRange(
3484                       std::distance(Info.Classes.begin(), Info.Classes.end()))
3485      << " Classes[" << MaxNumOperands << "];\n";
3486   OS << "    StringRef getMnemonic() const {\n";
3487   OS << "      return StringRef(MnemonicTable + Mnemonic + 1,\n";
3488   OS << "                       MnemonicTable[Mnemonic]);\n";
3489   OS << "    }\n";
3490   OS << "  };\n\n";
3491 
3492   OS << "  // Predicate for searching for an opcode.\n";
3493   OS << "  struct LessOpcode {\n";
3494   OS << "    bool operator()(const MatchEntry &LHS, StringRef RHS) {\n";
3495   OS << "      return LHS.getMnemonic() < RHS;\n";
3496   OS << "    }\n";
3497   OS << "    bool operator()(StringRef LHS, const MatchEntry &RHS) {\n";
3498   OS << "      return LHS < RHS.getMnemonic();\n";
3499   OS << "    }\n";
3500   OS << "    bool operator()(const MatchEntry &LHS, const MatchEntry &RHS) {\n";
3501   OS << "      return LHS.getMnemonic() < RHS.getMnemonic();\n";
3502   OS << "    }\n";
3503   OS << "  };\n";
3504 
3505   OS << "} // end anonymous namespace\n\n";
3506 
3507   unsigned VariantCount = Target.getAsmParserVariantCount();
3508   for (unsigned VC = 0; VC != VariantCount; ++VC) {
3509     Record *AsmVariant = Target.getAsmParserVariant(VC);
3510     int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
3511 
3512     OS << "static const MatchEntry MatchTable" << VC << "[] = {\n";
3513 
3514     for (const auto &MI : Info.Matchables) {
3515       if (MI->AsmVariantID != AsmVariantNo)
3516         continue;
3517 
3518       // Store a pascal-style length byte in the mnemonic.
3519       std::string LenMnemonic =
3520           char(MI->Mnemonic.size()) + MI->Mnemonic.lower();
3521       OS << "  { " << StringTable.GetOrAddStringOffset(LenMnemonic, false)
3522          << " /* " << MI->Mnemonic << " */, "
3523          << Target.getInstNamespace() << "::"
3524          << MI->getResultInst()->TheDef->getName() << ", "
3525          << MI->ConversionFnKind << ", ";
3526 
3527       // Write the required features mask.
3528       OS << "AMFBS";
3529       if (MI->RequiredFeatures.empty())
3530         OS << "_None";
3531       else
3532         for (unsigned i = 0, e = MI->RequiredFeatures.size(); i != e; ++i)
3533           OS << '_' << MI->RequiredFeatures[i]->TheDef->getName();
3534 
3535       OS << ", { ";
3536       ListSeparator LS;
3537       for (const MatchableInfo::AsmOperand &Op : MI->AsmOperands)
3538         OS << LS << Op.Class->Name;
3539       OS << " }, },\n";
3540     }
3541 
3542     OS << "};\n\n";
3543   }
3544 
3545   OS << "#include \"llvm/Support/Debug.h\"\n";
3546   OS << "#include \"llvm/Support/Format.h\"\n\n";
3547 
3548   // Finally, build the match function.
3549   OS << "unsigned " << Target.getName() << ClassName << "::\n"
3550      << "MatchInstructionImpl(const OperandVector &Operands,\n";
3551   OS << "                     MCInst &Inst,\n";
3552   if (ReportMultipleNearMisses)
3553     OS << "                     SmallVectorImpl<NearMissInfo> *NearMisses,\n";
3554   else
3555     OS << "                     uint64_t &ErrorInfo,\n"
3556        << "                     FeatureBitset &MissingFeatures,\n";
3557   OS << "                     bool matchingInlineAsm, unsigned VariantID) {\n";
3558 
3559   if (!ReportMultipleNearMisses) {
3560     OS << "  // Eliminate obvious mismatches.\n";
3561     OS << "  if (Operands.size() > "
3562        << (MaxNumOperands + HasMnemonicFirst) << ") {\n";
3563     OS << "    ErrorInfo = "
3564        << (MaxNumOperands + HasMnemonicFirst) << ";\n";
3565     OS << "    return Match_InvalidOperand;\n";
3566     OS << "  }\n\n";
3567   }
3568 
3569   // Emit code to get the available features.
3570   OS << "  // Get the current feature set.\n";
3571   OS << "  const FeatureBitset &AvailableFeatures = getAvailableFeatures();\n\n";
3572 
3573   OS << "  // Get the instruction mnemonic, which is the first token.\n";
3574   if (HasMnemonicFirst) {
3575     OS << "  StringRef Mnemonic = ((" << Target.getName()
3576        << "Operand &)*Operands[0]).getToken();\n\n";
3577   } else {
3578     OS << "  StringRef Mnemonic;\n";
3579     OS << "  if (Operands[0]->isToken())\n";
3580     OS << "    Mnemonic = ((" << Target.getName()
3581        << "Operand &)*Operands[0]).getToken();\n\n";
3582   }
3583 
3584   if (HasMnemonicAliases) {
3585     OS << "  // Process all MnemonicAliases to remap the mnemonic.\n";
3586     OS << "  applyMnemonicAliases(Mnemonic, AvailableFeatures, VariantID);\n\n";
3587   }
3588 
3589   // Emit code to compute the class list for this operand vector.
3590   if (!ReportMultipleNearMisses) {
3591     OS << "  // Some state to try to produce better error messages.\n";
3592     OS << "  bool HadMatchOtherThanFeatures = false;\n";
3593     OS << "  bool HadMatchOtherThanPredicate = false;\n";
3594     OS << "  unsigned RetCode = Match_InvalidOperand;\n";
3595     OS << "  MissingFeatures.set();\n";
3596     OS << "  // Set ErrorInfo to the operand that mismatches if it is\n";
3597     OS << "  // wrong for all instances of the instruction.\n";
3598     OS << "  ErrorInfo = ~0ULL;\n";
3599   }
3600 
3601   if (HasOptionalOperands) {
3602     OS << "  SmallBitVector OptionalOperandsMask(" << MaxNumOperands << ");\n";
3603   }
3604 
3605   // Emit code to search the table.
3606   OS << "  // Find the appropriate table for this asm variant.\n";
3607   OS << "  const MatchEntry *Start, *End;\n";
3608   OS << "  switch (VariantID) {\n";
3609   OS << "  default: llvm_unreachable(\"invalid variant!\");\n";
3610   for (unsigned VC = 0; VC != VariantCount; ++VC) {
3611     Record *AsmVariant = Target.getAsmParserVariant(VC);
3612     int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
3613     OS << "  case " << AsmVariantNo << ": Start = std::begin(MatchTable" << VC
3614        << "); End = std::end(MatchTable" << VC << "); break;\n";
3615   }
3616   OS << "  }\n";
3617 
3618   OS << "  // Search the table.\n";
3619   if (HasMnemonicFirst) {
3620     OS << "  auto MnemonicRange = "
3621           "std::equal_range(Start, End, Mnemonic, LessOpcode());\n\n";
3622   } else {
3623     OS << "  auto MnemonicRange = std::make_pair(Start, End);\n";
3624     OS << "  unsigned SIndex = Mnemonic.empty() ? 0 : 1;\n";
3625     OS << "  if (!Mnemonic.empty())\n";
3626     OS << "    MnemonicRange = "
3627           "std::equal_range(Start, End, Mnemonic.lower(), LessOpcode());\n\n";
3628   }
3629 
3630   OS << "  DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"AsmMatcher: found \" <<\n"
3631      << "  std::distance(MnemonicRange.first, MnemonicRange.second) <<\n"
3632      << "  \" encodings with mnemonic '\" << Mnemonic << \"'\\n\");\n\n";
3633 
3634   OS << "  // Return a more specific error code if no mnemonics match.\n";
3635   OS << "  if (MnemonicRange.first == MnemonicRange.second)\n";
3636   OS << "    return Match_MnemonicFail;\n\n";
3637 
3638   OS << "  for (const MatchEntry *it = MnemonicRange.first, "
3639      << "*ie = MnemonicRange.second;\n";
3640   OS << "       it != ie; ++it) {\n";
3641   OS << "    const FeatureBitset &RequiredFeatures = "
3642         "FeatureBitsets[it->RequiredFeaturesIdx];\n";
3643   OS << "    bool HasRequiredFeatures =\n";
3644   OS << "      (AvailableFeatures & RequiredFeatures) == RequiredFeatures;\n";
3645   OS << "    DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"Trying to match opcode \"\n";
3646   OS << "                                          << MII.getName(it->Opcode) << \"\\n\");\n";
3647 
3648   if (ReportMultipleNearMisses) {
3649     OS << "    // Some state to record ways in which this instruction did not match.\n";
3650     OS << "    NearMissInfo OperandNearMiss = NearMissInfo::getSuccess();\n";
3651     OS << "    NearMissInfo FeaturesNearMiss = NearMissInfo::getSuccess();\n";
3652     OS << "    NearMissInfo EarlyPredicateNearMiss = NearMissInfo::getSuccess();\n";
3653     OS << "    NearMissInfo LatePredicateNearMiss = NearMissInfo::getSuccess();\n";
3654     OS << "    bool MultipleInvalidOperands = false;\n";
3655   }
3656 
3657   if (HasMnemonicFirst) {
3658     OS << "    // equal_range guarantees that instruction mnemonic matches.\n";
3659     OS << "    assert(Mnemonic == it->getMnemonic());\n";
3660   }
3661 
3662   // Emit check that the subclasses match.
3663   if (!ReportMultipleNearMisses)
3664     OS << "    bool OperandsValid = true;\n";
3665   if (HasOptionalOperands) {
3666     OS << "    OptionalOperandsMask.reset(0, " << MaxNumOperands << ");\n";
3667   }
3668   OS << "    for (unsigned FormalIdx = " << (HasMnemonicFirst ? "0" : "SIndex")
3669      << ", ActualIdx = " << (HasMnemonicFirst ? "1" : "SIndex")
3670      << "; FormalIdx != " << MaxNumOperands << "; ++FormalIdx) {\n";
3671   OS << "      auto Formal = "
3672      << "static_cast<MatchClassKind>(it->Classes[FormalIdx]);\n";
3673   OS << "      DEBUG_WITH_TYPE(\"asm-matcher\",\n";
3674   OS << "                      dbgs() << \"  Matching formal operand class \" << getMatchClassName(Formal)\n";
3675   OS << "                             << \" against actual operand at index \" << ActualIdx);\n";
3676   OS << "      if (ActualIdx < Operands.size())\n";
3677   OS << "        DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \" (\";\n";
3678   OS << "                        Operands[ActualIdx]->print(dbgs()); dbgs() << \"): \");\n";
3679   OS << "      else\n";
3680   OS << "        DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \": \");\n";
3681   OS << "      if (ActualIdx >= Operands.size()) {\n";
3682   OS << "        DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"actual operand "
3683         "index out of range\\n\");\n";
3684   if (ReportMultipleNearMisses) {
3685     OS << "        bool ThisOperandValid = (Formal == " <<"InvalidMatchClass) || "
3686                                    "isSubclass(Formal, OptionalMatchClass);\n";
3687     OS << "        if (!ThisOperandValid) {\n";
3688     OS << "          if (!OperandNearMiss) {\n";
3689     OS << "            // Record info about match failure for later use.\n";
3690     OS << "            DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"recording too-few-operands near miss\\n\");\n";
3691     OS << "            OperandNearMiss =\n";
3692     OS << "                NearMissInfo::getTooFewOperands(Formal, it->Opcode);\n";
3693     OS << "          } else if (OperandNearMiss.getKind() != NearMissInfo::NearMissTooFewOperands) {\n";
3694     OS << "            // If more than one operand is invalid, give up on this match entry.\n";
3695     OS << "            DEBUG_WITH_TYPE(\n";
3696     OS << "                \"asm-matcher\",\n";
3697     OS << "                dbgs() << \"second invalid operand, giving up on this opcode\\n\");\n";
3698     OS << "            MultipleInvalidOperands = true;\n";
3699     OS << "            break;\n";
3700     OS << "          }\n";
3701     OS << "        } else {\n";
3702     OS << "          DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"but formal "
3703           "operand not required\\n\");\n";
3704     OS << "        }\n";
3705     OS << "        continue;\n";
3706   } else {
3707     OS << "        if (Formal == InvalidMatchClass) {\n";
3708     if (HasOptionalOperands) {
3709       OS << "          OptionalOperandsMask.set(FormalIdx, " << MaxNumOperands
3710          << ");\n";
3711     }
3712     OS << "          break;\n";
3713     OS << "        }\n";
3714     OS << "        if (isSubclass(Formal, OptionalMatchClass)) {\n";
3715     if (HasOptionalOperands) {
3716       OS << "          OptionalOperandsMask.set(FormalIdx);\n";
3717     }
3718     OS << "          continue;\n";
3719     OS << "        }\n";
3720     OS << "        OperandsValid = false;\n";
3721     OS << "        ErrorInfo = ActualIdx;\n";
3722     OS << "        break;\n";
3723   }
3724   OS << "      }\n";
3725   OS << "      MCParsedAsmOperand &Actual = *Operands[ActualIdx];\n";
3726   OS << "      unsigned Diag = validateOperandClass(Actual, Formal);\n";
3727   OS << "      if (Diag == Match_Success) {\n";
3728   OS << "        DEBUG_WITH_TYPE(\"asm-matcher\",\n";
3729   OS << "                        dbgs() << \"match success using generic matcher\\n\");\n";
3730   OS << "        ++ActualIdx;\n";
3731   OS << "        continue;\n";
3732   OS << "      }\n";
3733   OS << "      // If the generic handler indicates an invalid operand\n";
3734   OS << "      // failure, check for a special case.\n";
3735   OS << "      if (Diag != Match_Success) {\n";
3736   OS << "        unsigned TargetDiag = validateTargetOperandClass(Actual, Formal);\n";
3737   OS << "        if (TargetDiag == Match_Success) {\n";
3738   OS << "          DEBUG_WITH_TYPE(\"asm-matcher\",\n";
3739   OS << "                          dbgs() << \"match success using target matcher\\n\");\n";
3740   OS << "          ++ActualIdx;\n";
3741   OS << "          continue;\n";
3742   OS << "        }\n";
3743   OS << "        // If the target matcher returned a specific error code use\n";
3744   OS << "        // that, else use the one from the generic matcher.\n";
3745   OS << "        if (TargetDiag != Match_InvalidOperand && "
3746         "HasRequiredFeatures)\n";
3747   OS << "          Diag = TargetDiag;\n";
3748   OS << "      }\n";
3749   OS << "      // If current formal operand wasn't matched and it is optional\n"
3750      << "      // then try to match next formal operand\n";
3751   OS << "      if (Diag == Match_InvalidOperand "
3752      << "&& isSubclass(Formal, OptionalMatchClass)) {\n";
3753   if (HasOptionalOperands) {
3754     OS << "        OptionalOperandsMask.set(FormalIdx);\n";
3755   }
3756     OS << "        DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"ignoring optional operand\\n\");\n";
3757   OS << "        continue;\n";
3758   OS << "      }\n";
3759 
3760   if (ReportMultipleNearMisses) {
3761     OS << "      if (!OperandNearMiss) {\n";
3762     OS << "        // If this is the first invalid operand we have seen, record some\n";
3763     OS << "        // information about it.\n";
3764     OS << "        DEBUG_WITH_TYPE(\n";
3765     OS << "            \"asm-matcher\",\n";
3766     OS << "            dbgs()\n";
3767     OS << "                << \"operand match failed, recording near-miss with diag code \"\n";
3768     OS << "                << Diag << \"\\n\");\n";
3769     OS << "        OperandNearMiss =\n";
3770     OS << "            NearMissInfo::getMissedOperand(Diag, Formal, it->Opcode, ActualIdx);\n";
3771     OS << "        ++ActualIdx;\n";
3772     OS << "      } else {\n";
3773     OS << "        // If more than one operand is invalid, give up on this match entry.\n";
3774     OS << "        DEBUG_WITH_TYPE(\n";
3775     OS << "            \"asm-matcher\",\n";
3776     OS << "            dbgs() << \"second operand mismatch, skipping this opcode\\n\");\n";
3777     OS << "        MultipleInvalidOperands = true;\n";
3778     OS << "        break;\n";
3779     OS << "      }\n";
3780     OS << "    }\n\n";
3781   } else {
3782     OS << "      // If this operand is broken for all of the instances of this\n";
3783     OS << "      // mnemonic, keep track of it so we can report loc info.\n";
3784     OS << "      // If we already had a match that only failed due to a\n";
3785     OS << "      // target predicate, that diagnostic is preferred.\n";
3786     OS << "      if (!HadMatchOtherThanPredicate &&\n";
3787     OS << "          (it == MnemonicRange.first || ErrorInfo <= ActualIdx)) {\n";
3788     OS << "        if (HasRequiredFeatures && (ErrorInfo != ActualIdx || Diag "
3789           "!= Match_InvalidOperand))\n";
3790     OS << "          RetCode = Diag;\n";
3791     OS << "        ErrorInfo = ActualIdx;\n";
3792     OS << "      }\n";
3793     OS << "      // Otherwise, just reject this instance of the mnemonic.\n";
3794     OS << "      OperandsValid = false;\n";
3795     OS << "      break;\n";
3796     OS << "    }\n\n";
3797   }
3798 
3799   if (ReportMultipleNearMisses)
3800     OS << "    if (MultipleInvalidOperands) {\n";
3801   else
3802     OS << "    if (!OperandsValid) {\n";
3803   OS << "      DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"Opcode result: multiple \"\n";
3804   OS << "                                               \"operand mismatches, ignoring \"\n";
3805   OS << "                                               \"this opcode\\n\");\n";
3806   OS << "      continue;\n";
3807   OS << "    }\n";
3808 
3809   // Emit check that the required features are available.
3810   OS << "    if (!HasRequiredFeatures) {\n";
3811   if (!ReportMultipleNearMisses)
3812     OS << "      HadMatchOtherThanFeatures = true;\n";
3813   OS << "      FeatureBitset NewMissingFeatures = RequiredFeatures & "
3814         "~AvailableFeatures;\n";
3815   OS << "      DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"Missing target features:\";\n";
3816   OS << "                      for (unsigned I = 0, E = NewMissingFeatures.size(); I != E; ++I)\n";
3817   OS << "                        if (NewMissingFeatures[I])\n";
3818   OS << "                          dbgs() << ' ' << I;\n";
3819   OS << "                      dbgs() << \"\\n\");\n";
3820   if (ReportMultipleNearMisses) {
3821     OS << "      FeaturesNearMiss = NearMissInfo::getMissedFeature(NewMissingFeatures);\n";
3822   } else {
3823     OS << "      if (NewMissingFeatures.count() <=\n"
3824           "          MissingFeatures.count())\n";
3825     OS << "        MissingFeatures = NewMissingFeatures;\n";
3826     OS << "      continue;\n";
3827   }
3828   OS << "    }\n";
3829   OS << "\n";
3830   OS << "    Inst.clear();\n\n";
3831   OS << "    Inst.setOpcode(it->Opcode);\n";
3832   // Verify the instruction with the target-specific match predicate function.
3833   OS << "    // We have a potential match but have not rendered the operands.\n"
3834      << "    // Check the target predicate to handle any context sensitive\n"
3835         "    // constraints.\n"
3836      << "    // For example, Ties that are referenced multiple times must be\n"
3837         "    // checked here to ensure the input is the same for each match\n"
3838         "    // constraints. If we leave it any later the ties will have been\n"
3839         "    // canonicalized\n"
3840      << "    unsigned MatchResult;\n"
3841      << "    if ((MatchResult = checkEarlyTargetMatchPredicate(Inst, "
3842         "Operands)) != Match_Success) {\n"
3843      << "      Inst.clear();\n";
3844   OS << "      DEBUG_WITH_TYPE(\n";
3845   OS << "          \"asm-matcher\",\n";
3846   OS << "          dbgs() << \"Early target match predicate failed with diag code \"\n";
3847   OS << "                 << MatchResult << \"\\n\");\n";
3848   if (ReportMultipleNearMisses) {
3849     OS << "      EarlyPredicateNearMiss = NearMissInfo::getMissedPredicate(MatchResult);\n";
3850   } else {
3851     OS << "      RetCode = MatchResult;\n"
3852        << "      HadMatchOtherThanPredicate = true;\n"
3853        << "      continue;\n";
3854   }
3855   OS << "    }\n\n";
3856 
3857   if (ReportMultipleNearMisses) {
3858     OS << "    // If we did not successfully match the operands, then we can't convert to\n";
3859     OS << "    // an MCInst, so bail out on this instruction variant now.\n";
3860     OS << "    if (OperandNearMiss) {\n";
3861     OS << "      // If the operand mismatch was the only problem, reprrt it as a near-miss.\n";
3862     OS << "      if (NearMisses && !FeaturesNearMiss && !EarlyPredicateNearMiss) {\n";
3863     OS << "        DEBUG_WITH_TYPE(\n";
3864     OS << "            \"asm-matcher\",\n";
3865     OS << "            dbgs()\n";
3866     OS << "                << \"Opcode result: one mismatched operand, adding near-miss\\n\");\n";
3867     OS << "        NearMisses->push_back(OperandNearMiss);\n";
3868     OS << "      } else {\n";
3869     OS << "        DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"Opcode result: multiple \"\n";
3870     OS << "                                                 \"types of mismatch, so not \"\n";
3871     OS << "                                                 \"reporting near-miss\\n\");\n";
3872     OS << "      }\n";
3873     OS << "      continue;\n";
3874     OS << "    }\n\n";
3875   }
3876 
3877   OS << "    if (matchingInlineAsm) {\n";
3878   OS << "      convertToMapAndConstraints(it->ConvertFn, Operands);\n";
3879   if (!ReportMultipleNearMisses) {
3880     OS << "      if (!checkAsmTiedOperandConstraints(*this, it->ConvertFn, "
3881           "Operands, ErrorInfo))\n";
3882     OS << "        return Match_InvalidTiedOperand;\n";
3883     OS << "\n";
3884   }
3885   OS << "      return Match_Success;\n";
3886   OS << "    }\n\n";
3887   OS << "    // We have selected a definite instruction, convert the parsed\n"
3888      << "    // operands into the appropriate MCInst.\n";
3889   if (HasOptionalOperands) {
3890     OS << "    convertToMCInst(it->ConvertFn, Inst, it->Opcode, Operands,\n"
3891        << "                    OptionalOperandsMask);\n";
3892   } else {
3893     OS << "    convertToMCInst(it->ConvertFn, Inst, it->Opcode, Operands);\n";
3894   }
3895   OS << "\n";
3896 
3897   // Verify the instruction with the target-specific match predicate function.
3898   OS << "    // We have a potential match. Check the target predicate to\n"
3899      << "    // handle any context sensitive constraints.\n"
3900      << "    if ((MatchResult = checkTargetMatchPredicate(Inst)) !="
3901      << " Match_Success) {\n"
3902      << "      DEBUG_WITH_TYPE(\"asm-matcher\",\n"
3903      << "                      dbgs() << \"Target match predicate failed with diag code \"\n"
3904      << "                             << MatchResult << \"\\n\");\n"
3905      << "      Inst.clear();\n";
3906   if (ReportMultipleNearMisses) {
3907     OS << "      LatePredicateNearMiss = NearMissInfo::getMissedPredicate(MatchResult);\n";
3908   } else {
3909     OS << "      RetCode = MatchResult;\n"
3910        << "      HadMatchOtherThanPredicate = true;\n"
3911        << "      continue;\n";
3912   }
3913   OS << "    }\n\n";
3914 
3915   if (ReportMultipleNearMisses) {
3916     OS << "    int NumNearMisses = ((int)(bool)OperandNearMiss +\n";
3917     OS << "                         (int)(bool)FeaturesNearMiss +\n";
3918     OS << "                         (int)(bool)EarlyPredicateNearMiss +\n";
3919     OS << "                         (int)(bool)LatePredicateNearMiss);\n";
3920     OS << "    if (NumNearMisses == 1) {\n";
3921     OS << "      // We had exactly one type of near-miss, so add that to the list.\n";
3922     OS << "      assert(!OperandNearMiss && \"OperandNearMiss was handled earlier\");\n";
3923     OS << "      DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"Opcode result: found one type of \"\n";
3924     OS << "                                            \"mismatch, so reporting a \"\n";
3925     OS << "                                            \"near-miss\\n\");\n";
3926     OS << "      if (NearMisses && FeaturesNearMiss)\n";
3927     OS << "        NearMisses->push_back(FeaturesNearMiss);\n";
3928     OS << "      else if (NearMisses && EarlyPredicateNearMiss)\n";
3929     OS << "        NearMisses->push_back(EarlyPredicateNearMiss);\n";
3930     OS << "      else if (NearMisses && LatePredicateNearMiss)\n";
3931     OS << "        NearMisses->push_back(LatePredicateNearMiss);\n";
3932     OS << "\n";
3933     OS << "      continue;\n";
3934     OS << "    } else if (NumNearMisses > 1) {\n";
3935     OS << "      // This instruction missed in more than one way, so ignore it.\n";
3936     OS << "      DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"Opcode result: multiple \"\n";
3937     OS << "                                               \"types of mismatch, so not \"\n";
3938     OS << "                                               \"reporting near-miss\\n\");\n";
3939     OS << "      continue;\n";
3940     OS << "    }\n";
3941   }
3942 
3943   // Call the post-processing function, if used.
3944   StringRef InsnCleanupFn = AsmParser->getValueAsString("AsmParserInstCleanup");
3945   if (!InsnCleanupFn.empty())
3946     OS << "    " << InsnCleanupFn << "(Inst);\n";
3947 
3948   if (HasDeprecation) {
3949     OS << "    std::string Info;\n";
3950     OS << "    if (!getParser().getTargetParser().getTargetOptions().MCNoDeprecatedWarn &&\n";
3951     OS << "        MII.getDeprecatedInfo(Inst, getSTI(), Info)) {\n";
3952     OS << "      SMLoc Loc = ((" << Target.getName()
3953        << "Operand &)*Operands[0]).getStartLoc();\n";
3954     OS << "      getParser().Warning(Loc, Info, std::nullopt);\n";
3955     OS << "    }\n";
3956   }
3957 
3958   if (!ReportMultipleNearMisses) {
3959     OS << "    if (!checkAsmTiedOperandConstraints(*this, it->ConvertFn, "
3960           "Operands, ErrorInfo))\n";
3961     OS << "      return Match_InvalidTiedOperand;\n";
3962     OS << "\n";
3963   }
3964 
3965   OS << "    DEBUG_WITH_TYPE(\n";
3966   OS << "        \"asm-matcher\",\n";
3967   OS << "        dbgs() << \"Opcode result: complete match, selecting this opcode\\n\");\n";
3968   OS << "    return Match_Success;\n";
3969   OS << "  }\n\n";
3970 
3971   if (ReportMultipleNearMisses) {
3972     OS << "  // No instruction variants matched exactly.\n";
3973     OS << "  return Match_NearMisses;\n";
3974   } else {
3975     OS << "  // Okay, we had no match.  Try to return a useful error code.\n";
3976     OS << "  if (HadMatchOtherThanPredicate || !HadMatchOtherThanFeatures)\n";
3977     OS << "    return RetCode;\n\n";
3978     OS << "  ErrorInfo = 0;\n";
3979     OS << "  return Match_MissingFeature;\n";
3980   }
3981   OS << "}\n\n";
3982 
3983   if (!Info.OperandMatchInfo.empty())
3984     emitCustomOperandParsing(OS, Target, Info, ClassName, StringTable,
3985                              MaxMnemonicIndex, FeatureBitsets.size(),
3986                              HasMnemonicFirst, *AsmParser);
3987 
3988   OS << "#endif // GET_MATCHER_IMPLEMENTATION\n\n";
3989 
3990   OS << "\n#ifdef GET_MNEMONIC_SPELL_CHECKER\n";
3991   OS << "#undef GET_MNEMONIC_SPELL_CHECKER\n\n";
3992 
3993   emitMnemonicSpellChecker(OS, Target, VariantCount);
3994 
3995   OS << "#endif // GET_MNEMONIC_SPELL_CHECKER\n\n";
3996 
3997   OS << "\n#ifdef GET_MNEMONIC_CHECKER\n";
3998   OS << "#undef GET_MNEMONIC_CHECKER\n\n";
3999 
4000   emitMnemonicChecker(OS, Target, VariantCount,
4001                       HasMnemonicFirst, HasMnemonicAliases);
4002 
4003   OS << "#endif // GET_MNEMONIC_CHECKER\n\n";
4004 }
4005 
4006 static TableGen::Emitter::OptClass<AsmMatcherEmitter>
4007     X("gen-asm-matcher", "Generate assembly instruction matcher");
4008