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