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->TheDef->getValueAsBit(FieldName: "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 size_t MaxRowLength = 2; // minimum is custom converter plus terminator.
2014
2015 // TargetOperandClass - This is the target's operand class, like X86Operand.
2016 std::string TargetOperandClass = Target.getName().str() + "Operand";
2017
2018 // Write the convert function to a separate stream, so we can drop it after
2019 // the enum. We'll build up the conversion handlers for the individual
2020 // operand types opportunistically as we encounter them.
2021 std::string ConvertFnBody;
2022 raw_string_ostream CvtOS(ConvertFnBody);
2023 // Start the unified conversion function.
2024 if (HasOptionalOperands) {
2025 CvtOS << "void " << Target.getName() << ClassName << "::\n"
2026 << "convertToMCInst(unsigned Kind, MCInst &Inst, "
2027 << "unsigned Opcode,\n"
2028 << " const OperandVector &Operands,\n"
2029 << " const SmallBitVector &OptionalOperandsMask,\n"
2030 << " ArrayRef<unsigned> DefaultsOffset) {\n";
2031 } else {
2032 CvtOS << "void " << Target.getName() << ClassName << "::\n"
2033 << "convertToMCInst(unsigned Kind, MCInst &Inst, "
2034 << "unsigned Opcode,\n"
2035 << " const OperandVector &Operands) {\n";
2036 }
2037 CvtOS << " assert(Kind < CVT_NUM_SIGNATURES && \"Invalid signature!\");\n";
2038 CvtOS << " const uint8_t *Converter = ConversionTable[Kind];\n";
2039 CvtOS << " Inst.setOpcode(Opcode);\n";
2040 CvtOS << " for (const uint8_t *p = Converter; *p; p += 2) {\n";
2041 if (HasOptionalOperands) {
2042 // When optional operands are involved, formal and actual operand indices
2043 // may differ. Map the former to the latter by subtracting the number of
2044 // absent optional operands.
2045 // FIXME: This is not an operand index in the CVT_Tied case
2046 CvtOS << " unsigned OpIdx = *(p + 1) - DefaultsOffset[*(p + 1)];\n";
2047 } else {
2048 CvtOS << " unsigned OpIdx = *(p + 1);\n";
2049 }
2050 CvtOS << " switch (*p) {\n";
2051 CvtOS << " default: llvm_unreachable(\"invalid conversion entry!\");\n";
2052 CvtOS << " case CVT_Reg:\n";
2053 CvtOS << " static_cast<" << TargetOperandClass
2054 << " &>(*Operands[OpIdx]).addRegOperands(Inst, 1);\n";
2055 CvtOS << " break;\n";
2056 CvtOS << " case CVT_Tied: {\n";
2057 CvtOS << " assert(*(p + 1) < (size_t)(std::end(TiedAsmOperandTable) -\n";
2058 CvtOS
2059 << " std::begin(TiedAsmOperandTable)) &&\n";
2060 CvtOS << " \"Tied operand not found\");\n";
2061 CvtOS << " unsigned TiedResOpnd = TiedAsmOperandTable[*(p + 1)][0];\n";
2062 CvtOS << " if (TiedResOpnd != (uint8_t)-1)\n";
2063 CvtOS << " Inst.addOperand(Inst.getOperand(TiedResOpnd));\n";
2064 CvtOS << " break;\n";
2065 CvtOS << " }\n";
2066
2067 std::string OperandFnBody;
2068 raw_string_ostream OpOS(OperandFnBody);
2069 // Start the operand number lookup function.
2070 OpOS << "void " << Target.getName() << ClassName << "::\n"
2071 << "convertToMapAndConstraints(unsigned Kind,\n";
2072 OpOS.indent(NumSpaces: 27);
2073 OpOS << "const OperandVector &Operands) {\n"
2074 << " assert(Kind < CVT_NUM_SIGNATURES && \"Invalid signature!\");\n"
2075 << " unsigned NumMCOperands = 0;\n"
2076 << " const uint8_t *Converter = ConversionTable[Kind];\n"
2077 << " for (const uint8_t *p = Converter; *p; p += 2) {\n"
2078 << " switch (*p) {\n"
2079 << " default: llvm_unreachable(\"invalid conversion entry!\");\n"
2080 << " case CVT_Reg:\n"
2081 << " Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n"
2082 << " Operands[*(p + 1)]->setConstraint(\"r\");\n"
2083 << " ++NumMCOperands;\n"
2084 << " break;\n"
2085 << " case CVT_Tied:\n"
2086 << " ++NumMCOperands;\n"
2087 << " break;\n";
2088
2089 // Pre-populate the operand conversion kinds with the standard always
2090 // available entries.
2091 OperandConversionKinds.insert(X: CachedHashString("CVT_Done"));
2092 OperandConversionKinds.insert(X: CachedHashString("CVT_Reg"));
2093 OperandConversionKinds.insert(X: CachedHashString("CVT_Tied"));
2094 enum { CVT_Done, CVT_Reg, CVT_Tied };
2095
2096 // Map of e.g. <0, 2, 3> -> "Tie_0_2_3" enum label.
2097 std::map<std::tuple<uint8_t, uint8_t, uint8_t>, std::string>
2098 TiedOperandsEnumMap;
2099
2100 for (auto &II : Infos) {
2101 // Check if we have a custom match function.
2102 StringRef AsmMatchConverter =
2103 II->getResultInst()->TheDef->getValueAsString(FieldName: "AsmMatchConverter");
2104 if (!AsmMatchConverter.empty() && II->UseInstAsmMatchConverter) {
2105 std::string Signature = ("ConvertCustom_" + AsmMatchConverter).str();
2106 II->ConversionFnKind = Signature;
2107
2108 // Check if we have already generated this signature.
2109 if (!InstructionConversionKinds.insert(X: CachedHashString(Signature)))
2110 continue;
2111
2112 // Remember this converter for the kind enum.
2113 unsigned KindID = OperandConversionKinds.size();
2114 OperandConversionKinds.insert(
2115 X: CachedHashString("CVT_" + getEnumNameForToken(Str: AsmMatchConverter)));
2116
2117 // Add the converter row for this instruction.
2118 ConversionTable.emplace_back();
2119 ConversionTable.back().push_back(x: KindID);
2120 ConversionTable.back().push_back(x: CVT_Done);
2121
2122 // Add the handler to the conversion driver function.
2123 CvtOS << " case CVT_" << getEnumNameForToken(Str: AsmMatchConverter)
2124 << ":\n"
2125 << " " << AsmMatchConverter << "(Inst, Operands);\n"
2126 << " break;\n";
2127
2128 // FIXME: Handle the operand number lookup for custom match functions.
2129 continue;
2130 }
2131
2132 // Build the conversion function signature.
2133 std::string Signature = "Convert";
2134
2135 std::vector<uint8_t> ConversionRow;
2136
2137 // Compute the convert enum and the case body.
2138 MaxRowLength = std::max(a: MaxRowLength, b: II->ResOperands.size() * 2 + 1);
2139
2140 for (const auto &[Idx, OpInfo] : enumerate(First&: II->ResOperands)) {
2141 // Generate code to populate each result operand.
2142 switch (OpInfo.Kind) {
2143 case MatchableInfo::ResOperand::RenderAsmOperand: {
2144 // This comes from something we parsed.
2145 const MatchableInfo::AsmOperand &Op =
2146 II->AsmOperands[OpInfo.AsmOperandNum];
2147
2148 // Registers are always converted the same, don't duplicate the
2149 // conversion function based on them.
2150 Signature += "__";
2151 std::string Class;
2152 Class = Op.Class->isRegisterClass() ? "Reg" : Op.Class->ClassName;
2153 Signature += Class;
2154 Signature += utostr(X: OpInfo.MINumOperands);
2155 Signature += "_" + itostr(X: OpInfo.AsmOperandNum);
2156
2157 // Add the conversion kind, if necessary, and get the associated ID
2158 // the index of its entry in the vector).
2159 std::string Name =
2160 "CVT_" +
2161 (Op.Class->isRegisterClass() ? "Reg" : Op.Class->RenderMethod);
2162 if (Op.Class->IsOptional) {
2163 // For optional operands we must also care about DefaultMethod
2164 assert(HasOptionalOperands);
2165 Name += "_" + Op.Class->DefaultMethod;
2166 }
2167 Name = getEnumNameForToken(Str: Name);
2168
2169 bool IsNewConverter = false;
2170 unsigned ID =
2171 getConverterOperandID(Name, Table&: OperandConversionKinds, IsNew&: IsNewConverter);
2172
2173 // Add the operand entry to the instruction kind conversion row.
2174 ConversionRow.push_back(x: ID);
2175 ConversionRow.push_back(x: OpInfo.AsmOperandNum + HasMnemonicFirst);
2176
2177 if (!IsNewConverter)
2178 break;
2179
2180 // This is a new operand kind. Add a handler for it to the
2181 // converter driver.
2182 CvtOS << " case " << Name << ":\n";
2183 if (Op.Class->IsOptional) {
2184 // If optional operand is not present in actual instruction then we
2185 // should call its DefaultMethod before RenderMethod
2186 assert(HasOptionalOperands);
2187 CvtOS << " if (OptionalOperandsMask[*(p + 1)]) {\n"
2188 << " " << Op.Class->DefaultMethod << "()"
2189 << "->" << Op.Class->RenderMethod << "(Inst, "
2190 << OpInfo.MINumOperands << ");\n"
2191 << " } else {\n"
2192 << " static_cast<" << TargetOperandClass
2193 << " &>(*Operands[OpIdx])." << Op.Class->RenderMethod
2194 << "(Inst, " << OpInfo.MINumOperands << ");\n"
2195 << " }\n";
2196 } else {
2197 CvtOS << " static_cast<" << TargetOperandClass
2198 << " &>(*Operands[OpIdx])." << Op.Class->RenderMethod
2199 << "(Inst, " << OpInfo.MINumOperands << ");\n";
2200 }
2201 CvtOS << " break;\n";
2202
2203 // Add a handler for the operand number lookup.
2204 OpOS << " case " << Name << ":\n"
2205 << " Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n";
2206
2207 if (Op.Class->isRegisterClass())
2208 OpOS << " Operands[*(p + 1)]->setConstraint(\"r\");\n";
2209 else
2210 OpOS << " Operands[*(p + 1)]->setConstraint(\"m\");\n";
2211 OpOS << " NumMCOperands += " << OpInfo.MINumOperands << ";\n"
2212 << " break;\n";
2213 break;
2214 }
2215 case MatchableInfo::ResOperand::TiedOperand: {
2216 // If this operand is tied to a previous one, just copy the MCInst
2217 // operand from the earlier one.We can only tie single MCOperand values.
2218 assert(OpInfo.MINumOperands == 1 && "Not a singular MCOperand");
2219 uint8_t TiedOp = OpInfo.TiedOperands.ResOpnd;
2220 uint8_t SrcOp1 = OpInfo.TiedOperands.SrcOpnd1Idx + HasMnemonicFirst;
2221 uint8_t SrcOp2 = OpInfo.TiedOperands.SrcOpnd2Idx + HasMnemonicFirst;
2222 assert((Idx > TiedOp || TiedOp == (uint8_t)-1) &&
2223 "Tied operand precedes its target!");
2224 auto TiedTupleName = std::string("Tie") + utostr(X: TiedOp) + '_' +
2225 utostr(X: SrcOp1) + '_' + utostr(X: SrcOp2);
2226 Signature += "__" + TiedTupleName;
2227 ConversionRow.push_back(x: CVT_Tied);
2228 ConversionRow.push_back(x: TiedOp);
2229 ConversionRow.push_back(x: SrcOp1);
2230 ConversionRow.push_back(x: SrcOp2);
2231
2232 // Also create an 'enum' for this combination of tied operands.
2233 auto Key = std::tuple(TiedOp, SrcOp1, SrcOp2);
2234 TiedOperandsEnumMap.emplace(args&: Key, args&: TiedTupleName);
2235 break;
2236 }
2237 case MatchableInfo::ResOperand::ImmOperand: {
2238 int64_t Val = OpInfo.ImmVal;
2239 std::string Ty = "imm_" + itostr(X: Val);
2240 Ty = getEnumNameForToken(Str: Ty);
2241 Signature += "__" + Ty;
2242
2243 std::string Name = "CVT_" + Ty;
2244 bool IsNewConverter = false;
2245 unsigned ID =
2246 getConverterOperandID(Name, Table&: OperandConversionKinds, IsNew&: IsNewConverter);
2247 // Add the operand entry to the instruction kind conversion row.
2248 ConversionRow.push_back(x: ID);
2249 ConversionRow.push_back(x: 0);
2250
2251 if (!IsNewConverter)
2252 break;
2253
2254 CvtOS << " case " << Name << ":\n"
2255 << " Inst.addOperand(MCOperand::createImm(" << Val << "));\n"
2256 << " break;\n";
2257
2258 OpOS << " case " << Name << ":\n"
2259 << " Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n"
2260 << " Operands[*(p + 1)]->setConstraint(\"\");\n"
2261 << " ++NumMCOperands;\n"
2262 << " break;\n";
2263 break;
2264 }
2265 case MatchableInfo::ResOperand::RegOperand: {
2266 std::string Reg, Name;
2267 bool IsRegByHwMode = false;
2268 if (!OpInfo.Register) {
2269 Name = "reg0";
2270 Reg = "0";
2271 } else {
2272 Reg = getQualifiedName(R: OpInfo.Register);
2273 Name = "reg" + OpInfo.Register->getName().str();
2274 IsRegByHwMode = OpInfo.Register->isSubClassOf(Name: "RegisterByHwMode");
2275 }
2276 Signature += "__" + Name;
2277 Name = "CVT_" + Name;
2278 bool IsNewConverter = false;
2279 unsigned ID =
2280 getConverterOperandID(Name, Table&: OperandConversionKinds, IsNew&: IsNewConverter);
2281 // Add the operand entry to the instruction kind conversion row.
2282 ConversionRow.push_back(x: ID);
2283 ConversionRow.push_back(x: 0);
2284
2285 if (!IsNewConverter)
2286 break;
2287
2288 CvtOS << indent(4) << "case " << Name << ":\n"
2289 << indent(6) << "Inst.addOperand(MCOperand::createReg(";
2290 if (IsRegByHwMode) {
2291 RegisterByHwMode(OpInfo.Register, Target.getRegBank())
2292 .emitResolverCall(
2293 OS&: CvtOS, HwMode: "STI->getHwMode(MCSubtargetInfo::HwMode_RegInfo)");
2294 } else {
2295 CvtOS << Reg;
2296 }
2297 CvtOS << "));\n" << indent(6) << "break;\n";
2298 OpOS << " case " << Name << ":\n"
2299 << " Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n"
2300 << " Operands[*(p + 1)]->setConstraint(\"m\");\n"
2301 << " ++NumMCOperands;\n"
2302 << " break;\n";
2303 }
2304 }
2305 }
2306
2307 // If there were no operands, add to the signature to that effect
2308 if (Signature == "Convert")
2309 Signature += "_NoOperands";
2310
2311 II->ConversionFnKind = Signature;
2312
2313 // Save the signature. If we already have it, don't add a new row
2314 // to the table.
2315 if (!InstructionConversionKinds.insert(X: CachedHashString(Signature)))
2316 continue;
2317
2318 // Add the row to the table.
2319 ConversionTable.push_back(x: std::move(ConversionRow));
2320 }
2321
2322 // Finish up the converter driver function.
2323 CvtOS << " }\n }\n}\n\n";
2324
2325 // Finish up the operand number lookup function.
2326 OpOS << " }\n }\n}\n\n";
2327
2328 // Output a static table for tied operands.
2329 if (TiedOperandsEnumMap.size()) {
2330 // The number of tied operand combinations will be small in practice,
2331 // but just add the assert to be sure.
2332 assert(TiedOperandsEnumMap.size() <= 254 &&
2333 "Too many tied-operand combinations to reference with "
2334 "an 8bit offset from the conversion table, where index "
2335 "'255' is reserved as operand not to be copied.");
2336
2337 OS << "enum {\n";
2338 for (auto &KV : TiedOperandsEnumMap) {
2339 OS << " " << KV.second << ",\n";
2340 }
2341 OS << "};\n\n";
2342
2343 OS << "static const uint8_t TiedAsmOperandTable[][3] = {\n";
2344 for (auto &KV : TiedOperandsEnumMap) {
2345 OS << " /* " << KV.second << " */ { " << utostr(X: std::get<0>(t: KV.first))
2346 << ", " << utostr(X: std::get<1>(t: KV.first)) << ", "
2347 << utostr(X: std::get<2>(t: KV.first)) << " },\n";
2348 }
2349 OS << "};\n\n";
2350 } else {
2351 OS << "static const uint8_t TiedAsmOperandTable[][3] = "
2352 "{ /* empty */ {0, 0, 0} };\n\n";
2353 }
2354
2355 OS << "namespace {\n";
2356
2357 // Output the operand conversion kind enum.
2358 OS << "enum OperatorConversionKind {\n";
2359 for (const auto &Converter : OperandConversionKinds)
2360 OS << " " << Converter << ",\n";
2361 OS << " CVT_NUM_CONVERTERS\n";
2362 OS << "};\n\n";
2363
2364 // Output the instruction conversion kind enum.
2365 OS << "enum InstructionConversionKind {\n";
2366 for (const auto &Signature : InstructionConversionKinds)
2367 OS << " " << Signature << ",\n";
2368 OS << " CVT_NUM_SIGNATURES\n";
2369 OS << "};\n\n";
2370
2371 OS << "} // end anonymous namespace\n\n";
2372
2373 // Output the conversion table.
2374 OS << "static const uint8_t ConversionTable[CVT_NUM_SIGNATURES]["
2375 << MaxRowLength << "] = {\n";
2376
2377 for (unsigned Row = 0, ERow = ConversionTable.size(); Row != ERow; ++Row) {
2378 assert(ConversionTable[Row].size() % 2 == 0 && "bad conversion row!");
2379 OS << " // " << InstructionConversionKinds[Row] << "\n";
2380 OS << " { ";
2381 for (unsigned i = 0, e = ConversionTable[Row].size(); i != e; i += 2) {
2382 const auto &OCK = OperandConversionKinds[ConversionTable[Row][i]];
2383 OS << OCK << ", ";
2384 if (OCK != CachedHashString("CVT_Tied")) {
2385 OS << (unsigned)(ConversionTable[Row][i + 1]) << ", ";
2386 continue;
2387 }
2388
2389 // For a tied operand, emit a reference to the TiedAsmOperandTable
2390 // that contains the operand to copy, and the parsed operands to
2391 // check for their tied constraints.
2392 auto Key = std::tuple((uint8_t)ConversionTable[Row][i + 1],
2393 (uint8_t)ConversionTable[Row][i + 2],
2394 (uint8_t)ConversionTable[Row][i + 3]);
2395 auto TiedOpndEnum = TiedOperandsEnumMap.find(x: Key);
2396 assert(TiedOpndEnum != TiedOperandsEnumMap.end() &&
2397 "No record for tied operand pair");
2398 OS << TiedOpndEnum->second << ", ";
2399 i += 2;
2400 }
2401 OS << "CVT_Done },\n";
2402 }
2403
2404 OS << "};\n\n";
2405
2406 // Spit out the conversion driver function.
2407 OS << ConvertFnBody;
2408
2409 // Spit out the operand number lookup function.
2410 OS << OperandFnBody;
2411
2412 return ConversionTable.size();
2413}
2414
2415/// emitMatchClassEnumeration - Emit the enumeration for match class kinds.
2416static void emitMatchClassEnumeration(CodeGenTarget &Target,
2417 std::forward_list<ClassInfo> &Infos,
2418 raw_ostream &OS) {
2419 OS << "namespace {\n\n";
2420
2421 OS << "/// MatchClassKind - The kinds of classes which participate in\n"
2422 << "/// instruction matching.\n";
2423 OS << "enum MatchClassKind {\n";
2424 OS << " InvalidMatchClass = 0,\n";
2425 OS << " OptionalMatchClass = 1,\n";
2426 ClassInfo::ClassInfoKind LastKind = ClassInfo::Token;
2427 StringRef LastName = "OptionalMatchClass";
2428 for (const auto &CI : Infos) {
2429 if (LastKind == ClassInfo::Token && CI.Kind != ClassInfo::Token) {
2430 OS << " MCK_LAST_TOKEN = " << LastName << ",\n";
2431 } else if (LastKind < ClassInfo::RegisterClassByHwMode0 &&
2432 CI.Kind >= ClassInfo::RegisterClassByHwMode0) {
2433 OS << " MCK_LAST_REGISTER = " << LastName << ",\n";
2434 } else if (LastKind < ClassInfo::UserClass0 &&
2435 CI.Kind >= ClassInfo::UserClass0) {
2436 OS << " MCK_LAST_REGCLASS_BY_HWMODE = " << LastName << ",\n";
2437 }
2438
2439 LastKind = (ClassInfo::ClassInfoKind)CI.Kind;
2440 LastName = CI.Name;
2441
2442 OS << " " << CI.Name << ", // ";
2443 if (CI.Kind == ClassInfo::Token) {
2444 OS << "'" << CI.ValueName << "'\n";
2445 } else if (CI.isRegisterClass()) {
2446 if (!CI.ValueName.empty())
2447 OS << "register class '" << CI.ValueName << "'\n";
2448 else
2449 OS << "derived register class\n";
2450 } else if (CI.isRegisterClassByHwMode()) {
2451 OS << "register class by hwmode\n";
2452 } else {
2453 OS << "user defined class '" << CI.ValueName << "'\n";
2454 }
2455 }
2456 OS << " NumMatchClassKinds\n";
2457 OS << "};\n\n";
2458
2459 OS << "} // end anonymous namespace\n\n";
2460}
2461
2462/// emitMatchClassDiagStrings - Emit a function to get the diagnostic text to be
2463/// used when an assembly operand does not match the expected operand class.
2464static void emitOperandMatchErrorDiagStrings(AsmMatcherInfo &Info,
2465 raw_ostream &OS) {
2466 // If the target does not use DiagnosticString for any operands, don't emit
2467 // an unused function.
2468 if (llvm::all_of(Range&: Info.Classes, P: [](const ClassInfo &CI) {
2469 return CI.DiagnosticString.empty();
2470 }))
2471 return;
2472
2473 OS << "static const char *getMatchKindDiag(" << Info.Target.getName()
2474 << "AsmParser::" << Info.Target.getName()
2475 << "MatchResultTy MatchResult) {\n";
2476 OS << " switch (MatchResult) {\n";
2477
2478 for (const auto &CI : Info.Classes) {
2479 if (!CI.DiagnosticString.empty()) {
2480 assert(!CI.DiagnosticType.empty() &&
2481 "DiagnosticString set without DiagnosticType");
2482 OS << " case " << Info.Target.getName() << "AsmParser::Match_"
2483 << CI.DiagnosticType << ":\n";
2484 OS << " return \"" << CI.DiagnosticString << "\";\n";
2485 }
2486 }
2487
2488 OS << " default:\n";
2489 OS << " return nullptr;\n";
2490
2491 OS << " }\n";
2492 OS << "}\n\n";
2493}
2494
2495static void emitRegisterMatchErrorFunc(AsmMatcherInfo &Info, raw_ostream &OS) {
2496 OS << "static unsigned getDiagKindFromRegisterClass(MatchClassKind "
2497 "RegisterClass) {\n";
2498 if (none_of(Range&: Info.Classes, P: [](const ClassInfo &CI) {
2499 return CI.isRegisterClass() && !CI.DiagnosticType.empty();
2500 })) {
2501 OS << " return MCTargetAsmParser::Match_InvalidOperand;\n";
2502 } else {
2503 OS << " switch (RegisterClass) {\n";
2504 for (const auto &CI : Info.Classes) {
2505 if (CI.isRegisterClass() && !CI.DiagnosticType.empty()) {
2506 OS << " case " << CI.Name << ":\n";
2507 OS << " return " << Info.Target.getName() << "AsmParser::Match_"
2508 << CI.DiagnosticType << ";\n";
2509 }
2510 }
2511
2512 OS << " default:\n";
2513 OS << " return MCTargetAsmParser::Match_InvalidOperand;\n";
2514
2515 OS << " }\n";
2516 }
2517 OS << "}\n\n";
2518}
2519
2520/// emitValidateOperandClass - Emit the function to validate an operand class.
2521static void emitValidateOperandClass(const CodeGenTarget &Target,
2522 AsmMatcherInfo &Info, raw_ostream &OS) {
2523 OS << "static unsigned validateOperandClass(MCParsedAsmOperand &GOp, "
2524 << "MatchClassKind Kind, const MCSubtargetInfo &STI) {\n";
2525 OS << " " << Info.Target.getName() << "Operand &Operand = ("
2526 << Info.Target.getName() << "Operand &)GOp;\n";
2527
2528 // The InvalidMatchClass is not to match any operand.
2529 OS << " if (Kind == InvalidMatchClass)\n";
2530 OS << " return MCTargetAsmParser::Match_InvalidOperand;\n\n";
2531
2532 // Check for Token operands first.
2533 // FIXME: Use a more specific diagnostic type.
2534 OS << " if (Operand.isToken() && Kind <= MCK_LAST_TOKEN)\n";
2535 OS << " return isSubclass(matchTokenString(Operand.getToken()), Kind) ?\n"
2536 << " MCTargetAsmParser::Match_Success :\n"
2537 << " MCTargetAsmParser::Match_InvalidOperand;\n\n";
2538
2539 // Check the user classes. We don't care what order since we're only
2540 // actually matching against one of them.
2541 OS << " switch (Kind) {\n"
2542 " default: break;\n";
2543 for (const auto &CI : Info.Classes) {
2544 if (!CI.isUserClass())
2545 continue;
2546
2547 OS << " case " << CI.Name << ": {\n";
2548 OS << " DiagnosticPredicate DP(Operand." << CI.PredicateMethod
2549 << "());\n";
2550 OS << " if (DP.isMatch())\n";
2551 OS << " return MCTargetAsmParser::Match_Success;\n";
2552 if (!CI.DiagnosticType.empty()) {
2553 OS << " if (DP.isNearMatch())\n";
2554 OS << " return " << Info.Target.getName() << "AsmParser::Match_"
2555 << CI.DiagnosticType << ";\n";
2556 OS << " break;\n";
2557 } else {
2558 OS << " break;\n";
2559 }
2560 OS << " }\n";
2561 }
2562 OS << " } // end switch (Kind)\n\n";
2563
2564 const CodeGenRegBank &RegBank = Target.getRegBank();
2565 ArrayRef<const Record *> RegClassesByHwMode = Target.getAllRegClassByHwMode();
2566 unsigned NumClassesByHwMode = RegClassesByHwMode.size();
2567
2568 if (!RegClassesByHwMode.empty()) {
2569 OS << " if (Operand.isReg() && Kind > MCK_LAST_REGISTER &&"
2570 " Kind <= MCK_LAST_REGCLASS_BY_HWMODE) {\n";
2571
2572 const CodeGenHwModes &CGH = Target.getHwModes();
2573 unsigned NumModes = CGH.getNumModeIds();
2574
2575 OS << indent(4)
2576 << "static constexpr MatchClassKind RegClassByHwModeMatchTable["
2577 << NumModes << "][" << RegClassesByHwMode.size() << "] = {\n";
2578
2579 // TODO: If the instruction predicates can statically resolve which hwmode,
2580 // directly match the register class
2581 for (unsigned M = 0; M < NumModes; ++M) {
2582 OS << indent(6) << "{ // " << CGH.getModeName(Id: M, /*IncludeDefault=*/true)
2583 << '\n';
2584 for (unsigned I = 0; I != NumClassesByHwMode; ++I) {
2585 const Record *Class = RegClassesByHwMode[I];
2586 const HwModeSelect &ModeSelect = CGH.getHwModeSelect(R: Class);
2587
2588 auto FoundMode =
2589 find_if(Range: ModeSelect.Items, P: [=](const HwModeSelect::PairType P) {
2590 return P.first == M;
2591 });
2592
2593 if (FoundMode == ModeSelect.Items.end()) {
2594 OS << indent(8) << "InvalidMatchClass, // Missing mode entry for "
2595 << Class->getName() << "\n";
2596 } else {
2597 const CodeGenRegisterClass *RegClass =
2598 RegBank.getRegClass(FoundMode->second);
2599 const ClassInfo *CI =
2600 Info.RegisterClassClasses.at(k: RegClass->getDef());
2601 OS << indent(8) << CI->Name << ", // " << Class->getName() << "\n";
2602 }
2603 }
2604
2605 OS << indent(6) << "},\n";
2606 }
2607
2608 OS << indent(4) << "};\n\n";
2609
2610 OS << indent(4)
2611 << "static_assert(MCK_LAST_REGCLASS_BY_HWMODE - MCK_LAST_REGISTER == "
2612 << NumClassesByHwMode << ");\n";
2613
2614 OS << indent(4)
2615 << "const unsigned HwMode = "
2616 "STI.getHwMode(MCSubtargetInfo::HwMode_RegInfo);\n"
2617 "Kind = RegClassByHwModeMatchTable[HwMode][Kind - (MCK_LAST_REGISTER "
2618 "+ 1)];\n"
2619 " }\n\n";
2620 }
2621
2622 // Check for register operands, including sub-classes.
2623 const auto &Regs = RegBank.getRegisters();
2624 StringRef Namespace = Regs.front().TheDef->getValueAsString(FieldName: "Namespace");
2625 SmallVector<StringRef> Table(1 + Regs.size(), "InvalidMatchClass");
2626 for (const auto &RC : Info.RegisterClasses) {
2627 const auto &Reg = Target.getRegBank().getReg(RC.first);
2628 Table[Reg->EnumValue] = RC.second->Name;
2629 }
2630 OS << " if (Operand.isReg()) {\n";
2631 OS << " static constexpr uint16_t Table[" << Namespace
2632 << "::NUM_TARGET_REGS] = {\n";
2633 for (auto &MatchClassName : Table)
2634 OS << " " << MatchClassName << ",\n";
2635 OS << " };\n\n";
2636 OS << " MCRegister Reg = Operand.getReg();\n";
2637 OS << " MatchClassKind OpKind = Reg.isPhysical() ? "
2638 "(MatchClassKind)Table[Reg.id()] : InvalidMatchClass;\n";
2639 OS << " return isSubclass(OpKind, Kind) ? "
2640 << "(unsigned)MCTargetAsmParser::Match_Success :\n "
2641 << " getDiagKindFromRegisterClass(Kind);\n }\n\n";
2642
2643 // Expected operand is a register, but actual is not.
2644 OS << " if (Kind > MCK_LAST_TOKEN && Kind <= MCK_LAST_REGISTER)\n";
2645 OS << " return getDiagKindFromRegisterClass(Kind);\n\n";
2646
2647 // Generic fallthrough match failure case for operands that don't have
2648 // specialized diagnostic types.
2649 OS << " return MCTargetAsmParser::Match_InvalidOperand;\n";
2650 OS << "}\n\n";
2651}
2652
2653/// emitIsSubclass - Emit the subclass predicate function.
2654static void emitIsSubclass(CodeGenTarget &Target,
2655 std::forward_list<ClassInfo> &Infos,
2656 raw_ostream &OS) {
2657 OS << "/// isSubclass - Compute whether \\p A is a subclass of \\p B.\n";
2658 OS << "static bool isSubclass(MatchClassKind A, MatchClassKind B) {\n";
2659 OS << " if (A == B)\n";
2660 OS << " return true;\n\n";
2661
2662 // TODO: Use something like SequenceToOffsetTable to allow sequences to
2663 // overlap in this table.
2664 SmallVector<bool> SuperClassData;
2665
2666 OS << " [[maybe_unused]] static constexpr struct {\n";
2667 OS << " uint32_t Offset;\n";
2668 OS << " uint16_t Start;\n";
2669 OS << " uint16_t Length;\n";
2670 OS << " } Table[] = {\n";
2671 OS << " {0, 0, 0},\n"; // InvalidMatchClass
2672 OS << " {0, 0, 0},\n"; // OptionalMatchClass
2673 for (const auto &A : Infos) {
2674 SmallVector<bool> SuperClasses;
2675 SuperClasses.push_back(Elt: false); // InvalidMatchClass
2676 SuperClasses.push_back(Elt: A.IsOptional); // OptionalMatchClass
2677 for (const auto &B : Infos)
2678 SuperClasses.push_back(Elt: &A != &B && A.isSubsetOf(RHS: B));
2679
2680 // Trim leading and trailing zeros.
2681 auto End = find_if(Range: reverse(C&: SuperClasses), P: [](bool B) { return B; }).base();
2682 auto Start =
2683 std::find_if(first: SuperClasses.begin(), last: End, pred: [](bool B) { return B; });
2684
2685 unsigned Offset = SuperClassData.size();
2686 SuperClassData.append(in_start: Start, in_end: End);
2687
2688 OS << " {" << Offset << ", " << (Start - SuperClasses.begin()) << ", "
2689 << (End - Start) << "},\n";
2690 }
2691 OS << " };\n\n";
2692
2693 if (SuperClassData.empty()) {
2694 OS << " return false;\n";
2695 } else {
2696 // Dump the boolean data packed into bytes.
2697 SuperClassData.append(NumInputs: -SuperClassData.size() % 8, Elt: false);
2698 OS << " static constexpr uint8_t Data[] = {\n";
2699 for (unsigned I = 0, E = SuperClassData.size(); I < E; I += 8) {
2700 unsigned Byte = 0;
2701 for (unsigned J = 0; J < 8; ++J)
2702 Byte |= (unsigned)SuperClassData[I + J] << J;
2703 OS << formatv(Fmt: " {:X2},\n", Vals&: Byte);
2704 }
2705 OS << " };\n\n";
2706
2707 OS << " auto &Entry = Table[A];\n";
2708 OS << " unsigned Idx = B - Entry.Start;\n";
2709 OS << " if (Idx >= Entry.Length)\n";
2710 OS << " return false;\n";
2711 OS << " Idx += Entry.Offset;\n";
2712 OS << " return (Data[Idx / 8] >> (Idx % 8)) & 1;\n";
2713 }
2714 OS << "}\n\n";
2715}
2716
2717/// emitMatchTokenString - Emit the function to match a token string to the
2718/// appropriate match class value.
2719static void emitMatchTokenString(CodeGenTarget &Target,
2720 std::forward_list<ClassInfo> &Infos,
2721 raw_ostream &OS) {
2722 // Construct the match list.
2723 std::vector<StringMatcher::StringPair> Matches;
2724 for (const auto &CI : Infos) {
2725 if (CI.Kind == ClassInfo::Token)
2726 Matches.emplace_back(args: CI.ValueName, args: "return " + CI.Name + ";");
2727 }
2728
2729 OS << "static MatchClassKind matchTokenString(StringRef Name) {\n";
2730
2731 StringMatcher("Name", Matches, OS).Emit();
2732
2733 OS << " return InvalidMatchClass;\n";
2734 OS << "}\n\n";
2735}
2736
2737/// emitMatchRegisterName - Emit the function to match a string to the target
2738/// specific register enum.
2739static void emitMatchRegisterName(const CodeGenTarget &Target,
2740 const Record *AsmParser, raw_ostream &OS) {
2741 // Construct the match list.
2742 std::vector<StringMatcher::StringPair> Matches;
2743 const auto &Regs = Target.getRegBank().getRegisters();
2744 std::string Namespace =
2745 Regs.front().TheDef->getValueAsString(FieldName: "Namespace").str();
2746 for (const CodeGenRegister &Reg : Regs) {
2747 StringRef AsmName = Reg.TheDef->getValueAsString(FieldName: "AsmName");
2748 if (AsmName.empty())
2749 continue;
2750
2751 Matches.emplace_back(args: AsmName.str(), args: "return " + Namespace +
2752 "::" + Reg.getName().str() + ';');
2753 }
2754
2755 OS << "static MCRegister MatchRegisterName(StringRef Name) {\n";
2756
2757 bool IgnoreDuplicates =
2758 AsmParser->getValueAsBit(FieldName: "AllowDuplicateRegisterNames");
2759 StringMatcher("Name", Matches, OS).Emit(Indent: 0, IgnoreDuplicates);
2760
2761 OS << " return " << Namespace << "::NoRegister;\n";
2762 OS << "}\n\n";
2763}
2764
2765/// Emit the function to match a string to the target
2766/// specific register enum.
2767static void emitMatchRegisterAltName(const CodeGenTarget &Target,
2768 const Record *AsmParser, raw_ostream &OS) {
2769 // Construct the match list.
2770 std::vector<StringMatcher::StringPair> Matches;
2771 const auto &Regs = Target.getRegBank().getRegisters();
2772 std::string Namespace =
2773 Regs.front().TheDef->getValueAsString(FieldName: "Namespace").str();
2774 for (const CodeGenRegister &Reg : Regs) {
2775 for (StringRef AltName : Reg.TheDef->getValueAsListOfStrings(FieldName: "AltNames")) {
2776 AltName = AltName.trim();
2777
2778 // don't handle empty alternative names
2779 if (AltName.empty())
2780 continue;
2781
2782 Matches.emplace_back(args: AltName.str(), args: "return " + Namespace +
2783 "::" + Reg.getName().str() + ';');
2784 }
2785 }
2786
2787 OS << "static MCRegister MatchRegisterAltName(StringRef Name) {\n";
2788
2789 bool IgnoreDuplicates =
2790 AsmParser->getValueAsBit(FieldName: "AllowDuplicateRegisterNames");
2791 StringMatcher("Name", Matches, OS).Emit(Indent: 0, IgnoreDuplicates);
2792
2793 OS << " return " << Namespace << "::NoRegister;\n";
2794 OS << "}\n\n";
2795}
2796
2797/// emitOperandDiagnosticTypes - Emit the operand matching diagnostic types.
2798static void emitOperandDiagnosticTypes(AsmMatcherInfo &Info, raw_ostream &OS) {
2799 // Get the set of diagnostic types from all of the operand classes.
2800 std::set<StringRef> Types;
2801 for (const auto &OpClassEntry : Info.AsmOperandClasses) {
2802 if (!OpClassEntry.second->DiagnosticType.empty())
2803 Types.insert(x: OpClassEntry.second->DiagnosticType);
2804 }
2805 for (const auto &OpClassEntry : Info.RegisterClassClasses) {
2806 if (!OpClassEntry.second->DiagnosticType.empty())
2807 Types.insert(x: OpClassEntry.second->DiagnosticType);
2808 }
2809
2810 if (Types.empty())
2811 return;
2812
2813 // Now emit the enum entries.
2814 for (StringRef Type : Types)
2815 OS << " Match_" << Type << ",\n";
2816 OS << " END_OPERAND_DIAGNOSTIC_TYPES\n";
2817}
2818
2819/// emitGetSubtargetFeatureName - Emit the helper function to get the
2820/// user-level name for a subtarget feature.
2821static void emitGetSubtargetFeatureName(AsmMatcherInfo &Info, raw_ostream &OS) {
2822 OS << "// User-level names for subtarget features that participate in\n"
2823 << "// instruction matching.\n"
2824 << "static const char *getSubtargetFeatureName(uint64_t Val) {\n";
2825 if (!Info.SubtargetFeatures.empty()) {
2826 OS << " switch(Val) {\n";
2827 for (const SubtargetFeatureInfo &SFI :
2828 make_second_range(c&: Info.SubtargetFeatures)) {
2829 // FIXME: Totally just a placeholder name to get the algorithm working.
2830 OS << " case " << SFI.getEnumBitName() << ": return \""
2831 << SFI.TheDef->getValueAsString(FieldName: "PredicateName") << "\";\n";
2832 }
2833 OS << " default: return \"(unknown)\";\n";
2834 OS << " }\n";
2835 } else {
2836 // Nothing to emit, so skip the switch
2837 OS << " return \"(unknown)\";\n";
2838 }
2839 OS << "}\n\n";
2840}
2841
2842static std::string GetAliasRequiredFeatures(const Record *R,
2843 const AsmMatcherInfo &Info) {
2844 std::string Result;
2845
2846 ListSeparator LS(" && ");
2847 for (const Record *RF : R->getValueAsListOfDefs(FieldName: "Predicates")) {
2848 const SubtargetFeatureInfo *F = Info.getSubtargetFeature(Def: RF);
2849 if (!F)
2850 PrintFatalError(ErrorLoc: R->getLoc(),
2851 Msg: "Predicate '" + RF->getName() +
2852 "' is not marked as an AssemblerPredicate!");
2853 Result += LS;
2854 Result += "Features.test(" + F->getEnumBitName() + ')';
2855 }
2856
2857 return Result;
2858}
2859
2860static void
2861emitMnemonicAliasVariant(raw_ostream &OS, const AsmMatcherInfo &Info,
2862 ArrayRef<const Record *> Aliases, unsigned Indent = 0,
2863 StringRef AsmParserVariantName = StringRef()) {
2864 // Keep track of all the aliases from a mnemonic. Use an std::map so that the
2865 // iteration order of the map is stable.
2866 std::map<std::string, std::vector<const Record *>> AliasesFromMnemonic;
2867
2868 for (const Record *R : Aliases) {
2869 // FIXME: Allow AssemblerVariantName to be a comma separated list.
2870 StringRef AsmVariantName = R->getValueAsString(FieldName: "AsmVariantName");
2871 if (AsmVariantName != AsmParserVariantName)
2872 continue;
2873 AliasesFromMnemonic[R->getValueAsString(FieldName: "FromMnemonic").lower()].push_back(
2874 x: R);
2875 }
2876 if (AliasesFromMnemonic.empty())
2877 return;
2878
2879 // Process each alias a "from" mnemonic at a time, building the code executed
2880 // by the string remapper.
2881 std::vector<StringMatcher::StringPair> Cases;
2882 for (const auto &AliasEntry : AliasesFromMnemonic) {
2883 // Loop through each alias and emit code that handles each case. If there
2884 // are two instructions without predicates, emit an error. If there is one,
2885 // emit it last.
2886 std::string MatchCode;
2887 int AliasWithNoPredicate = -1;
2888
2889 ArrayRef<const Record *> ToVec = AliasEntry.second;
2890 for (const auto &[Idx, R] : enumerate(First&: ToVec)) {
2891 std::string FeatureMask = GetAliasRequiredFeatures(R, Info);
2892
2893 // If this unconditionally matches, remember it for later and diagnose
2894 // duplicates.
2895 if (FeatureMask.empty()) {
2896 if (AliasWithNoPredicate != -1 &&
2897 R->getValueAsString(FieldName: "ToMnemonic") !=
2898 ToVec[AliasWithNoPredicate]->getValueAsString(FieldName: "ToMnemonic")) {
2899 // We can't have two different aliases from the same mnemonic with no
2900 // predicate.
2901 PrintError(
2902 ErrorLoc: ToVec[AliasWithNoPredicate]->getLoc(),
2903 Msg: "two different MnemonicAliases with the same 'from' mnemonic!");
2904 PrintFatalError(ErrorLoc: R->getLoc(), Msg: "this is the other MnemonicAlias.");
2905 }
2906
2907 AliasWithNoPredicate = Idx;
2908 continue;
2909 }
2910 if (R->getValueAsString(FieldName: "ToMnemonic") == AliasEntry.first)
2911 PrintFatalError(ErrorLoc: R->getLoc(), Msg: "MnemonicAlias to the same string");
2912
2913 if (!MatchCode.empty())
2914 MatchCode += "else ";
2915 MatchCode += "if (" + FeatureMask + ")\n";
2916 MatchCode += " Mnemonic = \"";
2917 MatchCode += R->getValueAsString(FieldName: "ToMnemonic").lower();
2918 MatchCode += "\";\n";
2919 }
2920
2921 if (AliasWithNoPredicate != -1) {
2922 const Record *R = ToVec[AliasWithNoPredicate];
2923 if (!MatchCode.empty())
2924 MatchCode += "else\n ";
2925 MatchCode += "Mnemonic = \"";
2926 MatchCode += R->getValueAsString(FieldName: "ToMnemonic").lower();
2927 MatchCode += "\";\n";
2928 }
2929
2930 MatchCode += "return;";
2931
2932 Cases.emplace_back(args: AliasEntry.first, args&: MatchCode);
2933 }
2934 StringMatcher("Mnemonic", Cases, OS).Emit(Indent);
2935}
2936
2937/// emitMnemonicAliases - If the target has any MnemonicAlias<> definitions,
2938/// emit a function for them and return true, otherwise return false.
2939static bool emitMnemonicAliases(raw_ostream &OS, const AsmMatcherInfo &Info,
2940 CodeGenTarget &Target) {
2941 // Ignore aliases when match-prefix is set.
2942 if (!MatchPrefix.empty())
2943 return false;
2944
2945 ArrayRef<const Record *> Aliases =
2946 Info.getRecords().getAllDerivedDefinitions(ClassName: "MnemonicAlias");
2947 if (Aliases.empty())
2948 return false;
2949
2950 OS << "static void applyMnemonicAliases(StringRef &Mnemonic, "
2951 "const FeatureBitset &Features, unsigned VariantID) {\n";
2952 unsigned VariantCount = Target.getAsmParserVariantCount();
2953 for (unsigned VC = 0; VC != VariantCount; ++VC) {
2954 const Record *AsmVariant = Target.getAsmParserVariant(i: VC);
2955 int AsmParserVariantNo = AsmVariant->getValueAsInt(FieldName: "Variant");
2956 StringRef AsmParserVariantName = AsmVariant->getValueAsString(FieldName: "Name");
2957
2958 // If the variant doesn't have a name, defer to the emitMnemonicAliasVariant
2959 // call after the loop.
2960 if (AsmParserVariantName.empty()) {
2961 assert(VariantCount == 1 && "Multiple variants should each be named");
2962 continue;
2963 }
2964
2965 if (VC == 0)
2966 OS << " switch (VariantID) {\n";
2967 OS << " case " << AsmParserVariantNo << ":\n";
2968 emitMnemonicAliasVariant(OS, Info, Aliases, /*Indent=*/2,
2969 AsmParserVariantName);
2970 OS << " break;\n";
2971
2972 if (VC == VariantCount - 1)
2973 OS << " }\n";
2974 }
2975
2976 // Emit aliases that apply to all variants.
2977 emitMnemonicAliasVariant(OS, Info, Aliases);
2978
2979 OS << "}\n\n";
2980
2981 return true;
2982}
2983
2984static void
2985emitCustomOperandParsing(raw_ostream &OS, CodeGenTarget &Target,
2986 const AsmMatcherInfo &Info, StringRef ClassName,
2987 const StringToOffsetTable &StringTable,
2988 unsigned MaxMnemonicIndex, unsigned MaxFeaturesIndex,
2989 bool HasMnemonicFirst, const Record &AsmParser) {
2990 unsigned MaxMask = 0;
2991 for (const OperandMatchEntry &OMI : Info.OperandMatchInfo) {
2992 MaxMask |= OMI.OperandMask;
2993 }
2994
2995 // Emit the static custom operand parsing table;
2996 OS << "namespace {\n";
2997 OS << " struct OperandMatchEntry {\n";
2998 OS << " " << getMinimalTypeForRange(Range: MaxMnemonicIndex) << " Mnemonic;\n";
2999 OS << " " << getMinimalTypeForRange(Range: MaxMask) << " OperandMask;\n";
3000 OS << " "
3001 << getMinimalTypeForRange(
3002 Range: std::distance(first: Info.Classes.begin(), last: Info.Classes.end()) +
3003 2 /* Include 'InvalidMatchClass' and 'OptionalMatchClass' */)
3004 << " Class;\n";
3005 OS << " " << getMinimalTypeForRange(Range: MaxFeaturesIndex)
3006 << " RequiredFeaturesIdx;\n\n";
3007 OS << " StringRef getMnemonic() const {\n";
3008 OS << " return StringRef(MnemonicTable + Mnemonic + 1,\n";
3009 OS << " MnemonicTable[Mnemonic]);\n";
3010 OS << " }\n";
3011 OS << " };\n\n";
3012
3013 OS << " // Predicate for searching for an opcode.\n";
3014 OS << " struct LessOpcodeOperand {\n";
3015 OS << " bool operator()(const OperandMatchEntry &LHS, StringRef RHS) {\n";
3016 OS << " return LHS.getMnemonic() < RHS;\n";
3017 OS << " }\n";
3018 OS << " bool operator()(StringRef LHS, const OperandMatchEntry &RHS) {\n";
3019 OS << " return LHS < RHS.getMnemonic();\n";
3020 OS << " }\n";
3021 OS << " bool operator()(const OperandMatchEntry &LHS,";
3022 OS << " const OperandMatchEntry &RHS) {\n";
3023 OS << " return LHS.getMnemonic() < RHS.getMnemonic();\n";
3024 OS << " }\n";
3025 OS << " };\n";
3026
3027 OS << "} // end anonymous namespace\n\n";
3028
3029 OS << "static const OperandMatchEntry OperandMatchTable["
3030 << Info.OperandMatchInfo.size() << "] = {\n";
3031
3032 OS << " /* Operand List Mnemonic, Mask, Operand Class, Features */\n";
3033 for (const OperandMatchEntry &OMI : Info.OperandMatchInfo) {
3034 const MatchableInfo &II = *OMI.MI;
3035
3036 OS << " { ";
3037
3038 // Store a pascal-style length byte in the mnemonic.
3039 std::string LenMnemonic = char(II.Mnemonic.size()) + II.Mnemonic.lower();
3040 OS << *StringTable.GetStringOffset(Str: LenMnemonic) << " /* " << II.Mnemonic
3041 << " */, ";
3042
3043 OS << OMI.OperandMask;
3044 OS << " /* ";
3045 ListSeparator LS;
3046 for (int i = 0, e = 31; i != e; ++i)
3047 if (OMI.OperandMask & (1 << i))
3048 OS << LS << i;
3049 OS << " */, ";
3050
3051 OS << OMI.CI->Name;
3052
3053 // Write the required features mask.
3054 OS << ", AMFBS";
3055 if (II.RequiredFeatures.empty())
3056 OS << "_None";
3057 else
3058 for (const auto &F : II.RequiredFeatures)
3059 OS << '_' << F->TheDef->getName();
3060
3061 OS << " },\n";
3062 }
3063 OS << "};\n\n";
3064
3065 // Emit the operand class switch to call the correct custom parser for
3066 // the found operand class.
3067 OS << "ParseStatus " << Target.getName() << ClassName << "::\n"
3068 << "tryCustomParseOperand(OperandVector"
3069 << " &Operands,\n unsigned MCK) {\n\n"
3070 << " switch(MCK) {\n";
3071
3072 for (const auto &CI : Info.Classes) {
3073 if (CI.ParserMethod.empty())
3074 continue;
3075 OS << " case " << CI.Name << ":\n"
3076 << " return " << CI.ParserMethod << "(Operands);\n";
3077 }
3078
3079 OS << " default:\n";
3080 OS << " return ParseStatus::NoMatch;\n";
3081 OS << " }\n";
3082 OS << " return ParseStatus::NoMatch;\n";
3083 OS << "}\n\n";
3084
3085 // Emit the static custom operand parser. This code is very similar with
3086 // the other matcher. Also use MatchResultTy here just in case we go for
3087 // a better error handling.
3088 OS << "ParseStatus " << Target.getName() << ClassName << "::\n"
3089 << "MatchOperandParserImpl(OperandVector"
3090 << " &Operands,\n StringRef Mnemonic,\n"
3091 << " bool ParseForAllFeatures) {\n";
3092
3093 // Emit code to get the available features.
3094 OS << " // Get the current feature set.\n";
3095 OS << " const FeatureBitset &AvailableFeatures = "
3096 "getAvailableFeatures();\n\n";
3097
3098 OS << " // Get the next operand index.\n";
3099 OS << " unsigned NextOpNum = Operands.size()"
3100 << (HasMnemonicFirst ? " - 1" : "") << ";\n";
3101
3102 // Emit code to search the table.
3103 OS << " // Search the table.\n";
3104 if (HasMnemonicFirst) {
3105 OS << " auto MnemonicRange =\n";
3106 OS << " std::equal_range(std::begin(OperandMatchTable), "
3107 "std::end(OperandMatchTable),\n";
3108 OS << " Mnemonic, LessOpcodeOperand());\n\n";
3109 } else {
3110 OS << " auto MnemonicRange = std::pair(std::begin(OperandMatchTable),"
3111 " std::end(OperandMatchTable));\n";
3112 OS << " if (!Mnemonic.empty())\n";
3113 OS << " MnemonicRange =\n";
3114 OS << " std::equal_range(std::begin(OperandMatchTable), "
3115 "std::end(OperandMatchTable),\n";
3116 OS << " Mnemonic, LessOpcodeOperand());\n\n";
3117 }
3118
3119 OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
3120 OS << " return ParseStatus::NoMatch;\n\n";
3121
3122 OS << " for (const OperandMatchEntry *it = MnemonicRange.first,\n"
3123 << " *ie = MnemonicRange.second; it != ie; ++it) {\n";
3124
3125 OS << " // equal_range guarantees that instruction mnemonic matches.\n";
3126 OS << " assert(Mnemonic == it->getMnemonic());\n\n";
3127
3128 // Emit check that the required features are available.
3129 OS << " // check if the available features match\n";
3130 OS << " const FeatureBitset &RequiredFeatures = "
3131 "FeatureBitsets[it->RequiredFeaturesIdx];\n";
3132 OS << " if (!ParseForAllFeatures && (AvailableFeatures & "
3133 "RequiredFeatures) != RequiredFeatures)\n";
3134 OS << " continue;\n\n";
3135
3136 // Emit check to ensure the operand number matches.
3137 OS << " // check if the operand in question has a custom parser.\n";
3138 OS << " if (!(it->OperandMask & (1 << NextOpNum)))\n";
3139 OS << " continue;\n\n";
3140
3141 // Emit call to the custom parser method
3142 StringRef ParserName = AsmParser.getValueAsString(FieldName: "OperandParserMethod");
3143 if (ParserName.empty())
3144 ParserName = "tryCustomParseOperand";
3145 OS << " // call custom parse method to handle the operand\n";
3146 OS << " ParseStatus Result = " << ParserName << "(Operands, it->Class);\n";
3147 OS << " if (!Result.isNoMatch())\n";
3148 OS << " return Result;\n";
3149 OS << " }\n\n";
3150
3151 OS << " // Okay, we had no match.\n";
3152 OS << " return ParseStatus::NoMatch;\n";
3153 OS << "}\n\n";
3154}
3155
3156static void emitAsmTiedOperandConstraints(CodeGenTarget &Target,
3157 AsmMatcherInfo &Info, raw_ostream &OS,
3158 bool HasOptionalOperands) {
3159 std::string AsmParserName =
3160 Info.AsmParser->getValueAsString(FieldName: "AsmParserClassName").str();
3161 OS << "static bool ";
3162 OS << "checkAsmTiedOperandConstraints(const " << Target.getName()
3163 << AsmParserName << "&AsmParser,\n";
3164 OS << " unsigned Kind, const OperandVector "
3165 "&Operands,\n";
3166 if (HasOptionalOperands)
3167 OS << " ArrayRef<unsigned> DefaultsOffset,\n";
3168 OS << " uint64_t &ErrorInfo) {\n";
3169 OS << " assert(Kind < CVT_NUM_SIGNATURES && \"Invalid signature!\");\n";
3170 OS << " const uint8_t *Converter = ConversionTable[Kind];\n";
3171 OS << " for (const uint8_t *p = Converter; *p; p += 2) {\n";
3172 OS << " switch (*p) {\n";
3173 OS << " case CVT_Tied: {\n";
3174 OS << " unsigned OpIdx = *(p + 1);\n";
3175 OS << " assert(OpIdx < (size_t)(std::end(TiedAsmOperandTable) -\n";
3176 OS << " std::begin(TiedAsmOperandTable)) &&\n";
3177 OS << " \"Tied operand not found\");\n";
3178 OS << " unsigned OpndNum1 = TiedAsmOperandTable[OpIdx][1];\n";
3179 OS << " unsigned OpndNum2 = TiedAsmOperandTable[OpIdx][2];\n";
3180 if (HasOptionalOperands) {
3181 // When optional operands are involved, formal and actual operand indices
3182 // may differ. Map the former to the latter by subtracting the number of
3183 // absent optional operands.
3184 OS << " OpndNum1 = OpndNum1 - DefaultsOffset[OpndNum1];\n";
3185 OS << " OpndNum2 = OpndNum2 - DefaultsOffset[OpndNum2];\n";
3186 }
3187 OS << " if (OpndNum1 != OpndNum2) {\n";
3188 OS << " auto &SrcOp1 = Operands[OpndNum1];\n";
3189 OS << " auto &SrcOp2 = Operands[OpndNum2];\n";
3190 OS << " if (!AsmParser.areEqualRegs(*SrcOp1, *SrcOp2)) {\n";
3191 OS << " ErrorInfo = OpndNum2;\n";
3192 OS << " return false;\n";
3193 OS << " }\n";
3194 OS << " }\n";
3195 OS << " break;\n";
3196 OS << " }\n";
3197 OS << " default:\n";
3198 OS << " break;\n";
3199 OS << " }\n";
3200 OS << " }\n";
3201 OS << " return true;\n";
3202 OS << "}\n\n";
3203}
3204
3205static void emitMnemonicSpellChecker(raw_ostream &OS, CodeGenTarget &Target,
3206 unsigned VariantCount) {
3207 OS << "static std::string " << Target.getName()
3208 << "MnemonicSpellCheck(StringRef S, const FeatureBitset &FBS,"
3209 << " unsigned VariantID) {\n";
3210 if (!VariantCount)
3211 OS << " return \"\";";
3212 else {
3213 OS << " const unsigned MaxEditDist = 2;\n";
3214 OS << " std::vector<StringRef> Candidates;\n";
3215 OS << " StringRef Prev = \"\";\n\n";
3216
3217 OS << " // Find the appropriate table for this asm variant.\n";
3218 OS << " const MatchEntry *Start, *End;\n";
3219 OS << " switch (VariantID) {\n";
3220 OS << " default: llvm_unreachable(\"invalid variant!\");\n";
3221 for (unsigned VC = 0; VC != VariantCount; ++VC) {
3222 const Record *AsmVariant = Target.getAsmParserVariant(i: VC);
3223 int AsmVariantNo = AsmVariant->getValueAsInt(FieldName: "Variant");
3224 OS << " case " << AsmVariantNo << ": Start = std::begin(MatchTable" << VC
3225 << "); End = std::end(MatchTable" << VC << "); break;\n";
3226 }
3227 OS << " }\n\n";
3228 OS << " for (auto I = Start; I < End; I++) {\n";
3229 OS << " // Ignore unsupported instructions.\n";
3230 OS << " const FeatureBitset &RequiredFeatures = "
3231 "FeatureBitsets[I->RequiredFeaturesIdx];\n";
3232 OS << " if ((FBS & RequiredFeatures) != RequiredFeatures)\n";
3233 OS << " continue;\n";
3234 OS << "\n";
3235 OS << " StringRef T = I->getMnemonic();\n";
3236 OS << " // Avoid recomputing the edit distance for the same string.\n";
3237 OS << " if (T == Prev)\n";
3238 OS << " continue;\n";
3239 OS << "\n";
3240 OS << " Prev = T;\n";
3241 OS << " unsigned Dist = S.edit_distance(T, false, MaxEditDist);\n";
3242 OS << " if (Dist <= MaxEditDist)\n";
3243 OS << " Candidates.push_back(T);\n";
3244 OS << " }\n";
3245 OS << "\n";
3246 OS << " if (Candidates.empty())\n";
3247 OS << " return \"\";\n";
3248 OS << "\n";
3249 OS << " std::string Res = \", did you mean: \";\n";
3250 OS << " unsigned i = 0;\n";
3251 OS << " for (; i < Candidates.size() - 1; i++)\n";
3252 OS << " Res += Candidates[i].str() + \", \";\n";
3253 OS << " return Res + Candidates[i].str() + \"?\";\n";
3254 }
3255 OS << "}\n";
3256 OS << "\n";
3257}
3258
3259static void emitMnemonicChecker(raw_ostream &OS, CodeGenTarget &Target,
3260 unsigned VariantCount, bool HasMnemonicFirst,
3261 bool HasMnemonicAliases) {
3262 OS << "static bool " << Target.getName()
3263 << "CheckMnemonic(StringRef Mnemonic,\n";
3264 OS << " "
3265 << "const FeatureBitset &AvailableFeatures,\n";
3266 OS << " "
3267 << "unsigned VariantID) {\n";
3268
3269 if (!VariantCount) {
3270 OS << " return false;\n";
3271 } else {
3272 if (HasMnemonicAliases) {
3273 OS << " // Process all MnemonicAliases to remap the mnemonic.\n";
3274 OS << " applyMnemonicAliases(Mnemonic, AvailableFeatures, VariantID);";
3275 OS << "\n\n";
3276 }
3277 OS << " // Find the appropriate table for this asm variant.\n";
3278 OS << " const MatchEntry *Start, *End;\n";
3279 OS << " switch (VariantID) {\n";
3280 OS << " default: llvm_unreachable(\"invalid variant!\");\n";
3281 for (unsigned VC = 0; VC != VariantCount; ++VC) {
3282 const Record *AsmVariant = Target.getAsmParserVariant(i: VC);
3283 int AsmVariantNo = AsmVariant->getValueAsInt(FieldName: "Variant");
3284 OS << " case " << AsmVariantNo << ": Start = std::begin(MatchTable" << VC
3285 << "); End = std::end(MatchTable" << VC << "); break;\n";
3286 }
3287 OS << " }\n\n";
3288
3289 OS << " // Search the table.\n";
3290 if (HasMnemonicFirst) {
3291 OS << " auto MnemonicRange = "
3292 "std::equal_range(Start, End, Mnemonic, LessOpcode());\n\n";
3293 } else {
3294 OS << " auto MnemonicRange = std::pair(Start, End);\n";
3295 OS << " unsigned SIndex = Mnemonic.empty() ? 0 : 1;\n";
3296 OS << " if (!Mnemonic.empty())\n";
3297 OS << " MnemonicRange = "
3298 << "std::equal_range(Start, End, Mnemonic.lower(), LessOpcode());\n\n";
3299 }
3300
3301 OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
3302 OS << " return false;\n\n";
3303
3304 OS << " for (const MatchEntry *it = MnemonicRange.first, "
3305 << "*ie = MnemonicRange.second;\n";
3306 OS << " it != ie; ++it) {\n";
3307 OS << " const FeatureBitset &RequiredFeatures =\n";
3308 OS << " FeatureBitsets[it->RequiredFeaturesIdx];\n";
3309 OS << " if ((AvailableFeatures & RequiredFeatures) == ";
3310 OS << "RequiredFeatures)\n";
3311 OS << " return true;\n";
3312 OS << " }\n";
3313 OS << " return false;\n";
3314 }
3315 OS << "}\n";
3316 OS << "\n";
3317}
3318
3319// Emit a function mapping match classes to strings, for debugging.
3320static void emitMatchClassKindNames(std::forward_list<ClassInfo> &Infos,
3321 raw_ostream &OS) {
3322 OS << "#ifndef NDEBUG\n";
3323 OS << "const char *getMatchClassName(MatchClassKind Kind) {\n";
3324 OS << " switch (Kind) {\n";
3325
3326 OS << " case InvalidMatchClass: return \"InvalidMatchClass\";\n";
3327 OS << " case OptionalMatchClass: return \"OptionalMatchClass\";\n";
3328 for (const auto &CI : Infos) {
3329 OS << " case " << CI.Name << ": return \"" << CI.Name << "\";\n";
3330 }
3331 OS << " case NumMatchClassKinds: return \"NumMatchClassKinds\";\n";
3332
3333 OS << " }\n";
3334 OS << " llvm_unreachable(\"unhandled MatchClassKind!\");\n";
3335 OS << "}\n\n";
3336 OS << "#endif // NDEBUG\n";
3337}
3338
3339static std::string
3340getNameForFeatureBitset(ArrayRef<const Record *> FeatureBitset) {
3341 std::string Name = "AMFBS";
3342 for (const Record *Feature : FeatureBitset)
3343 Name += ("_" + Feature->getName()).str();
3344 return Name;
3345}
3346
3347void AsmMatcherEmitter::run(raw_ostream &OS) {
3348 CodeGenTarget Target(Records);
3349 const Record *AsmParser = Target.getAsmParser();
3350 StringRef ClassName = AsmParser->getValueAsString(FieldName: "AsmParserClassName");
3351
3352 emitSourceFileHeader(Desc: "Assembly Matcher Source Fragment", OS, Record: Records);
3353
3354 // Compute the information on the instructions to match.
3355 AsmMatcherInfo Info(AsmParser, Target, Records);
3356 Info.buildInfo();
3357
3358 bool PreferSmallerInstructions = getPreferSmallerInstructions(Target);
3359 // Sort the instruction table using the partial order on classes. We use
3360 // stable_sort to ensure that ambiguous instructions are still
3361 // deterministically ordered.
3362 llvm::stable_sort(
3363 Range&: Info.Matchables,
3364 C: [PreferSmallerInstructions](const std::unique_ptr<MatchableInfo> &A,
3365 const std::unique_ptr<MatchableInfo> &B) {
3366 return A->shouldBeMatchedBefore(RHS: *B, PreferSmallerInstructions);
3367 });
3368
3369#ifdef EXPENSIVE_CHECKS
3370 // Verify that the table is sorted and operator < works transitively.
3371 for (auto I = Info.Matchables.begin(), E = Info.Matchables.end(); I != E;
3372 ++I) {
3373 for (auto J = I; J != E; ++J) {
3374 assert(!(*J)->shouldBeMatchedBefore(**I, PreferSmallerInstructions));
3375 }
3376 }
3377#endif
3378
3379 DEBUG_WITH_TYPE("instruction_info", {
3380 for (const auto &MI : Info.Matchables)
3381 MI->dump();
3382 });
3383
3384 // Check for ambiguous matchables.
3385 DEBUG_WITH_TYPE("ambiguous_instrs", {
3386 unsigned NumAmbiguous = 0;
3387 for (auto I = Info.Matchables.begin(), E = Info.Matchables.end(); I != E;
3388 ++I) {
3389 for (auto J = std::next(I); J != E; ++J) {
3390 const MatchableInfo &A = **I;
3391 const MatchableInfo &B = **J;
3392
3393 if (A.couldMatchAmbiguouslyWith(B, PreferSmallerInstructions)) {
3394 errs() << "warning: ambiguous matchables:\n";
3395 A.dump();
3396 errs() << "\nis incomparable with:\n";
3397 B.dump();
3398 errs() << "\n\n";
3399 ++NumAmbiguous;
3400 }
3401 }
3402 }
3403 if (NumAmbiguous)
3404 errs() << "warning: " << NumAmbiguous << " ambiguous matchables!\n";
3405 });
3406
3407 // Compute the information on the custom operand parsing.
3408 Info.buildOperandMatchInfo();
3409
3410 bool HasMnemonicFirst = AsmParser->getValueAsBit(FieldName: "HasMnemonicFirst");
3411 bool HasOptionalOperands = Info.hasOptionalOperands();
3412 bool ReportMultipleNearMisses =
3413 AsmParser->getValueAsBit(FieldName: "ReportMultipleNearMisses");
3414
3415 // Write the output.
3416
3417 // Information for the class declaration.
3418 OS << "\n#ifdef GET_ASSEMBLER_HEADER\n";
3419 OS << "#undef GET_ASSEMBLER_HEADER\n";
3420 OS << " // This should be included into the middle of the declaration of\n";
3421 OS << " // your subclasses implementation of MCTargetAsmParser.\n";
3422 OS << " FeatureBitset ComputeAvailableFeatures(const FeatureBitset &FB) "
3423 "const;\n";
3424 if (HasOptionalOperands) {
3425 OS << " void convertToMCInst(unsigned Kind, MCInst &Inst, "
3426 << "unsigned Opcode,\n"
3427 << " const OperandVector &Operands,\n"
3428 << " const SmallBitVector "
3429 "&OptionalOperandsMask,\n"
3430 << " ArrayRef<unsigned> DefaultsOffset);\n";
3431 } else {
3432 OS << " void convertToMCInst(unsigned Kind, MCInst &Inst, "
3433 << "unsigned Opcode,\n"
3434 << " const OperandVector &Operands);\n";
3435 }
3436 OS << " void convertToMapAndConstraints(unsigned Kind,\n ";
3437 OS << " const OperandVector &Operands) override;\n";
3438 OS << " unsigned MatchInstructionImpl(const OperandVector &Operands,\n"
3439 << " MCInst &Inst,\n";
3440 if (ReportMultipleNearMisses)
3441 OS << " SmallVectorImpl<NearMissInfo> "
3442 "*NearMisses,\n";
3443 else
3444 OS << " uint64_t &ErrorInfo,\n"
3445 << " FeatureBitset &MissingFeatures,\n";
3446 OS << " bool matchingInlineAsm,\n"
3447 << " unsigned VariantID = 0);\n";
3448 if (!ReportMultipleNearMisses)
3449 OS << " unsigned MatchInstructionImpl(const OperandVector &Operands,\n"
3450 << " MCInst &Inst,\n"
3451 << " uint64_t &ErrorInfo,\n"
3452 << " bool matchingInlineAsm,\n"
3453 << " unsigned VariantID = 0) {\n"
3454 << " FeatureBitset MissingFeatures;\n"
3455 << " return MatchInstructionImpl(Operands, Inst, ErrorInfo, "
3456 "MissingFeatures,\n"
3457 << " matchingInlineAsm, VariantID);\n"
3458 << " }\n\n";
3459
3460 if (!Info.OperandMatchInfo.empty()) {
3461 OS << " ParseStatus MatchOperandParserImpl(\n";
3462 OS << " OperandVector &Operands,\n";
3463 OS << " StringRef Mnemonic,\n";
3464 OS << " bool ParseForAllFeatures = false);\n";
3465
3466 OS << " ParseStatus tryCustomParseOperand(\n";
3467 OS << " OperandVector &Operands,\n";
3468 OS << " unsigned MCK);\n\n";
3469 }
3470
3471 OS << "#endif // GET_ASSEMBLER_HEADER\n\n";
3472
3473 // Emit the operand match diagnostic enum names.
3474 OS << "\n#ifdef GET_OPERAND_DIAGNOSTIC_TYPES\n";
3475 OS << "#undef GET_OPERAND_DIAGNOSTIC_TYPES\n\n";
3476 emitOperandDiagnosticTypes(Info, OS);
3477 OS << "#endif // GET_OPERAND_DIAGNOSTIC_TYPES\n\n";
3478
3479 OS << "\n#ifdef GET_REGISTER_MATCHER\n";
3480 OS << "#undef GET_REGISTER_MATCHER\n\n";
3481
3482 // Emit the subtarget feature enumeration.
3483 SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(
3484 SubtargetFeatures: Info.SubtargetFeatures, OS);
3485
3486 // Emit the function to match a register name to number.
3487 // This should be omitted for Mips target
3488 if (AsmParser->getValueAsBit(FieldName: "ShouldEmitMatchRegisterName"))
3489 emitMatchRegisterName(Target, AsmParser, OS);
3490
3491 if (AsmParser->getValueAsBit(FieldName: "ShouldEmitMatchRegisterAltName"))
3492 emitMatchRegisterAltName(Target, AsmParser, OS);
3493
3494 OS << "#endif // GET_REGISTER_MATCHER\n\n";
3495
3496 OS << "\n#ifdef GET_SUBTARGET_FEATURE_NAME\n";
3497 OS << "#undef GET_SUBTARGET_FEATURE_NAME\n\n";
3498
3499 // Generate the helper function to get the names for subtarget features.
3500 emitGetSubtargetFeatureName(Info, OS);
3501
3502 OS << "#endif // GET_SUBTARGET_FEATURE_NAME\n\n";
3503
3504 OS << "\n#ifdef GET_MATCHER_IMPLEMENTATION\n";
3505 OS << "#undef GET_MATCHER_IMPLEMENTATION\n\n";
3506
3507 // Generate the function that remaps for mnemonic aliases.
3508 bool HasMnemonicAliases = emitMnemonicAliases(OS, Info, Target);
3509
3510 // Generate the convertToMCInst function to convert operands into an MCInst.
3511 // Also, generate the convertToMapAndConstraints function for MS-style inline
3512 // assembly. The latter doesn't actually generate a MCInst.
3513 unsigned NumConverters =
3514 emitConvertFuncs(Target, ClassName, Infos&: Info.Matchables, HasMnemonicFirst,
3515 HasOptionalOperands, OS);
3516
3517 // Emit the enumeration for classes which participate in matching.
3518 emitMatchClassEnumeration(Target, Infos&: Info.Classes, OS);
3519
3520 // Emit a function to get the user-visible string to describe an operand
3521 // match failure in diagnostics.
3522 emitOperandMatchErrorDiagStrings(Info, OS);
3523
3524 // Emit a function to map register classes to operand match failure codes.
3525 emitRegisterMatchErrorFunc(Info, OS);
3526
3527 // Emit the routine to match token strings to their match class.
3528 emitMatchTokenString(Target, Infos&: Info.Classes, OS);
3529
3530 // Emit the subclass predicate routine.
3531 emitIsSubclass(Target, Infos&: Info.Classes, OS);
3532
3533 // Emit the routine to validate an operand against a match class.
3534 emitValidateOperandClass(Target, Info, OS);
3535
3536 emitMatchClassKindNames(Infos&: Info.Classes, OS);
3537
3538 // Emit the available features compute function.
3539 SubtargetFeatureInfo::emitComputeAssemblerAvailableFeatures(
3540 TargetName: Info.Target.getName(), ClassName, FuncName: "ComputeAvailableFeatures",
3541 SubtargetFeatures&: Info.SubtargetFeatures, OS);
3542
3543 if (!ReportMultipleNearMisses)
3544 emitAsmTiedOperandConstraints(Target, Info, OS, HasOptionalOperands);
3545
3546 StringToOffsetTable StringTable(/*AppendZero=*/false);
3547
3548 size_t MaxNumOperands = 0;
3549 unsigned MaxMnemonicIndex = 0;
3550 bool HasDeprecation = false;
3551 for (const auto &MI : Info.Matchables) {
3552 MaxNumOperands = std::max(a: MaxNumOperands, b: MI->AsmOperands.size());
3553 HasDeprecation |= MI->HasDeprecation;
3554
3555 // Store a pascal-style length byte in the mnemonic.
3556 std::string LenMnemonic = char(MI->Mnemonic.size()) + MI->Mnemonic.lower();
3557 MaxMnemonicIndex = std::max(a: MaxMnemonicIndex,
3558 b: StringTable.GetOrAddStringOffset(Str: LenMnemonic));
3559 }
3560
3561 OS << "static const char MnemonicTable[] =\n";
3562 StringTable.EmitString(O&: OS);
3563 OS << ";\n\n";
3564
3565 std::vector<std::vector<const Record *>> FeatureBitsets;
3566 for (const auto &MI : Info.Matchables) {
3567 if (MI->RequiredFeatures.empty())
3568 continue;
3569 FeatureBitsets.emplace_back();
3570 for (const auto *F : MI->RequiredFeatures)
3571 FeatureBitsets.back().push_back(x: F->TheDef);
3572 }
3573
3574 llvm::sort(C&: FeatureBitsets,
3575 Comp: [&](ArrayRef<const Record *> A, ArrayRef<const Record *> B) {
3576 if (A.size() != B.size())
3577 return A.size() < B.size();
3578 for (const auto [ARec, BRec] : zip_equal(t&: A, u&: B)) {
3579 if (ARec->getName() != BRec->getName())
3580 return ARec->getName() < BRec->getName();
3581 }
3582 return false;
3583 });
3584 FeatureBitsets.erase(first: llvm::unique(R&: FeatureBitsets), last: FeatureBitsets.end());
3585 OS << "// Feature bitsets.\n"
3586 << "enum : " << getMinimalTypeForRange(Range: FeatureBitsets.size()) << " {\n"
3587 << " AMFBS_None,\n";
3588 for (const auto &FeatureBitset : FeatureBitsets) {
3589 if (FeatureBitset.empty())
3590 continue;
3591 OS << " " << getNameForFeatureBitset(FeatureBitset) << ",\n";
3592 }
3593 OS << "};\n\n"
3594 << "static constexpr FeatureBitset FeatureBitsets[] = {\n"
3595 << " {}, // AMFBS_None\n";
3596 for (const auto &FeatureBitset : FeatureBitsets) {
3597 if (FeatureBitset.empty())
3598 continue;
3599 OS << " {";
3600 for (const auto &Feature : FeatureBitset) {
3601 const auto &I = Info.SubtargetFeatures.find(x: Feature);
3602 assert(I != Info.SubtargetFeatures.end() && "Didn't import predicate?");
3603 OS << I->second.getEnumBitName() << ", ";
3604 }
3605 OS << "},\n";
3606 }
3607 OS << "};\n\n";
3608
3609 // Emit the static match table; unused classes get initialized to 0 which is
3610 // guaranteed to be InvalidMatchClass.
3611 //
3612 // FIXME: We can reduce the size of this table very easily. First, we change
3613 // it so that store the kinds in separate bit-fields for each index, which
3614 // only needs to be the max width used for classes at that index (we also need
3615 // to reject based on this during classification). If we then make sure to
3616 // order the match kinds appropriately (putting mnemonics last), then we
3617 // should only end up using a few bits for each class, especially the ones
3618 // following the mnemonic.
3619 OS << "namespace {\n";
3620 OS << " struct MatchEntry {\n";
3621 OS << " " << getMinimalTypeForRange(Range: MaxMnemonicIndex) << " Mnemonic;\n";
3622 OS << " uint32_t Opcode;\n";
3623 OS << " " << getMinimalTypeForRange(Range: NumConverters) << " ConvertFn;\n";
3624 OS << " " << getMinimalTypeForRange(Range: FeatureBitsets.size())
3625 << " RequiredFeaturesIdx;\n";
3626 OS << " "
3627 << getMinimalTypeForRange(
3628 Range: std::distance(first: Info.Classes.begin(), last: Info.Classes.end()) +
3629 2 /* Include 'InvalidMatchClass' and 'OptionalMatchClass' */)
3630 << " Classes[" << MaxNumOperands << "];\n";
3631 OS << " StringRef getMnemonic() const {\n";
3632 OS << " return StringRef(MnemonicTable + Mnemonic + 1,\n";
3633 OS << " MnemonicTable[Mnemonic]);\n";
3634 OS << " }\n";
3635 OS << " };\n\n";
3636
3637 OS << " // Predicate for searching for an opcode.\n";
3638 OS << " struct LessOpcode {\n";
3639 OS << " bool operator()(const MatchEntry &LHS, StringRef RHS) {\n";
3640 OS << " return LHS.getMnemonic() < RHS;\n";
3641 OS << " }\n";
3642 OS << " bool operator()(StringRef LHS, const MatchEntry &RHS) {\n";
3643 OS << " return LHS < RHS.getMnemonic();\n";
3644 OS << " }\n";
3645 OS << " bool operator()(const MatchEntry &LHS, const MatchEntry &RHS) {\n";
3646 OS << " return LHS.getMnemonic() < RHS.getMnemonic();\n";
3647 OS << " }\n";
3648 OS << " };\n";
3649
3650 OS << "} // end anonymous namespace\n\n";
3651
3652 unsigned VariantCount = Target.getAsmParserVariantCount();
3653 for (unsigned VC = 0; VC != VariantCount; ++VC) {
3654 const Record *AsmVariant = Target.getAsmParserVariant(i: VC);
3655 int AsmVariantNo = AsmVariant->getValueAsInt(FieldName: "Variant");
3656
3657 OS << "static const MatchEntry MatchTable" << VC << "[] = {\n";
3658
3659 for (const auto &MI : Info.Matchables) {
3660 if (MI->AsmVariantID != AsmVariantNo)
3661 continue;
3662
3663 // Store a pascal-style length byte in the mnemonic.
3664 std::string LenMnemonic =
3665 char(MI->Mnemonic.size()) + MI->Mnemonic.lower();
3666 OS << " { " << *StringTable.GetStringOffset(Str: LenMnemonic) << " /* "
3667 << MI->Mnemonic << " */, " << Target.getInstNamespace()
3668 << "::" << MI->getResultInst()->getName() << ", "
3669 << MI->ConversionFnKind << ", ";
3670
3671 // Write the required features mask.
3672 OS << "AMFBS";
3673 if (MI->RequiredFeatures.empty())
3674 OS << "_None";
3675 else
3676 for (const auto &F : MI->RequiredFeatures)
3677 OS << '_' << F->TheDef->getName();
3678
3679 OS << ", { ";
3680 ListSeparator LS;
3681 for (const MatchableInfo::AsmOperand &Op : MI->AsmOperands)
3682 OS << LS << Op.Class->Name;
3683 OS << " }, },\n";
3684 }
3685
3686 OS << "};\n\n";
3687 }
3688
3689 OS << "#include \"llvm/Support/Debug.h\"\n";
3690 OS << "#include \"llvm/Support/Format.h\"\n\n";
3691
3692 // Finally, build the match function.
3693 OS << "unsigned " << Target.getName() << ClassName << "::\n"
3694 << "MatchInstructionImpl(const OperandVector &Operands,\n";
3695 OS << " MCInst &Inst,\n";
3696 if (ReportMultipleNearMisses)
3697 OS << " SmallVectorImpl<NearMissInfo> *NearMisses,\n";
3698 else
3699 OS << " uint64_t &ErrorInfo,\n"
3700 << " FeatureBitset &MissingFeatures,\n";
3701 OS << " bool matchingInlineAsm, unsigned VariantID) {\n";
3702
3703 if (!ReportMultipleNearMisses) {
3704 OS << " // Eliminate obvious mismatches.\n";
3705 OS << " if (Operands.size() > " << (MaxNumOperands + HasMnemonicFirst)
3706 << ") {\n";
3707 OS << " ErrorInfo = " << (MaxNumOperands + HasMnemonicFirst) << ";\n";
3708 OS << " return Match_InvalidOperand;\n";
3709 OS << " }\n\n";
3710 }
3711
3712 // Emit code to get the available features.
3713 OS << " // Get the current feature set.\n";
3714 OS << " const FeatureBitset &AvailableFeatures = "
3715 "getAvailableFeatures();\n\n";
3716
3717 OS << " // Get the instruction mnemonic, which is the first token.\n";
3718 if (HasMnemonicFirst) {
3719 OS << " StringRef Mnemonic = ((" << Target.getName()
3720 << "Operand &)*Operands[0]).getToken();\n\n";
3721 } else {
3722 OS << " StringRef Mnemonic;\n";
3723 OS << " if (Operands[0]->isToken())\n";
3724 OS << " Mnemonic = ((" << Target.getName()
3725 << "Operand &)*Operands[0]).getToken();\n\n";
3726 }
3727
3728 if (HasMnemonicAliases) {
3729 OS << " // Process all MnemonicAliases to remap the mnemonic.\n";
3730 OS << " applyMnemonicAliases(Mnemonic, AvailableFeatures, VariantID);\n\n";
3731 }
3732
3733 // Emit code to compute the class list for this operand vector.
3734 if (!ReportMultipleNearMisses) {
3735 OS << " // Some state to try to produce better error messages.\n";
3736 OS << " bool HadMatchOtherThanFeatures = false;\n";
3737 OS << " bool HadMatchOtherThanPredicate = false;\n";
3738 OS << " unsigned RetCode = Match_InvalidOperand;\n";
3739 OS << " MissingFeatures.set();\n";
3740 OS << " // Set ErrorInfo to the operand that mismatches if it is\n";
3741 OS << " // wrong for all instances of the instruction.\n";
3742 OS << " ErrorInfo = ~0ULL;\n";
3743 }
3744
3745 if (HasOptionalOperands)
3746 OS << " SmallBitVector OptionalOperandsMask("
3747 << MaxNumOperands + HasMnemonicFirst << ");\n";
3748
3749 // Emit code to search the table.
3750 OS << " // Find the appropriate table for this asm variant.\n";
3751 OS << " const MatchEntry *Start, *End;\n";
3752 OS << " switch (VariantID) {\n";
3753 OS << " default: llvm_unreachable(\"invalid variant!\");\n";
3754 for (unsigned VC = 0; VC != VariantCount; ++VC) {
3755 const Record *AsmVariant = Target.getAsmParserVariant(i: VC);
3756 int AsmVariantNo = AsmVariant->getValueAsInt(FieldName: "Variant");
3757 OS << " case " << AsmVariantNo << ": Start = std::begin(MatchTable" << VC
3758 << "); End = std::end(MatchTable" << VC << "); break;\n";
3759 }
3760 OS << " }\n";
3761
3762 OS << " // Search the table.\n";
3763 if (HasMnemonicFirst) {
3764 OS << " auto MnemonicRange = "
3765 "std::equal_range(Start, End, Mnemonic, LessOpcode());\n\n";
3766 } else {
3767 OS << " auto MnemonicRange = std::pair(Start, End);\n";
3768 OS << " unsigned SIndex = Mnemonic.empty() ? 0 : 1;\n";
3769 OS << " if (!Mnemonic.empty())\n";
3770 OS << " MnemonicRange = "
3771 "std::equal_range(Start, End, Mnemonic.lower(), LessOpcode());\n\n";
3772 }
3773
3774 OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"AsmMatcher: found \" "
3775 "<<\n"
3776 << " std::distance(MnemonicRange.first, MnemonicRange.second) <<\n"
3777 << " \" encodings with mnemonic '\" << Mnemonic << \"'\\n\");\n\n";
3778
3779 OS << " // Return a more specific error code if no mnemonics match.\n";
3780 OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
3781 OS << " return Match_MnemonicFail;\n\n";
3782
3783 OS << " for (const MatchEntry *it = MnemonicRange.first, "
3784 << "*ie = MnemonicRange.second;\n";
3785 OS << " it != ie; ++it) {\n";
3786 OS << " const FeatureBitset &RequiredFeatures = "
3787 "FeatureBitsets[it->RequiredFeaturesIdx];\n";
3788 OS << " bool HasRequiredFeatures =\n";
3789 OS << " (AvailableFeatures & RequiredFeatures) == RequiredFeatures;\n";
3790 OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"Trying to match "
3791 "opcode \"\n";
3792 OS << " << MII.getName(it->Opcode) "
3793 "<< \"\\n\");\n";
3794
3795 if (ReportMultipleNearMisses) {
3796 OS << " // Some state to record ways in which this instruction did not "
3797 "match.\n";
3798 OS << " NearMissInfo OperandNearMiss = NearMissInfo::getSuccess();\n";
3799 OS << " NearMissInfo FeaturesNearMiss = NearMissInfo::getSuccess();\n";
3800 OS << " NearMissInfo EarlyPredicateNearMiss = "
3801 "NearMissInfo::getSuccess();\n";
3802 OS << " NearMissInfo LatePredicateNearMiss = "
3803 "NearMissInfo::getSuccess();\n";
3804 OS << " bool MultipleInvalidOperands = false;\n";
3805 }
3806
3807 if (HasMnemonicFirst) {
3808 OS << " // equal_range guarantees that instruction mnemonic matches.\n";
3809 OS << " assert(Mnemonic == it->getMnemonic());\n";
3810 }
3811
3812 // Emit check that the subclasses match.
3813 if (!ReportMultipleNearMisses)
3814 OS << " bool OperandsValid = true;\n";
3815 if (HasOptionalOperands)
3816 OS << " OptionalOperandsMask.reset(0, "
3817 << MaxNumOperands + HasMnemonicFirst << ");\n";
3818 OS << " for (unsigned FormalIdx = " << (HasMnemonicFirst ? "0" : "SIndex")
3819 << ", ActualIdx = " << (HasMnemonicFirst ? "1" : "SIndex")
3820 << "; FormalIdx != " << MaxNumOperands << "; ++FormalIdx) {\n";
3821 OS << " auto Formal = "
3822 << "static_cast<MatchClassKind>(it->Classes[FormalIdx]);\n";
3823 OS << " DEBUG_WITH_TYPE(\"asm-matcher\",\n";
3824 OS << " dbgs() << \" Matching formal operand class \" "
3825 "<< getMatchClassName(Formal)\n";
3826 OS << " << \" against actual operand at index \" "
3827 "<< ActualIdx);\n";
3828 OS << " if (ActualIdx < Operands.size())\n";
3829 OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \" (\";\n";
3830 OS << " Operands[ActualIdx]->print(dbgs(), "
3831 "*getContext().getAsmInfo()); dbgs() << "
3832 "\"): \");\n";
3833 OS << " else\n";
3834 OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \": \");\n";
3835 OS << " if (ActualIdx >= Operands.size()) {\n";
3836 OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"actual operand "
3837 "index out of range\\n\");\n";
3838 if (ReportMultipleNearMisses) {
3839 OS << " bool ThisOperandValid = (Formal == "
3840 << "InvalidMatchClass) || "
3841 "isSubclass(Formal, OptionalMatchClass);\n";
3842 OS << " if (!ThisOperandValid) {\n";
3843 OS << " if (!OperandNearMiss) {\n";
3844 OS << " // Record info about match failure for later use.\n";
3845 OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"recording "
3846 "too-few-operands near miss\\n\");\n";
3847 OS << " OperandNearMiss =\n";
3848 OS << " NearMissInfo::getTooFewOperands(Formal, "
3849 "it->Opcode);\n";
3850 OS << " } else if (OperandNearMiss.getKind() != "
3851 "NearMissInfo::NearMissTooFewOperands) {\n";
3852 OS << " // If more than one operand is invalid, give up on this "
3853 "match entry.\n";
3854 OS << " DEBUG_WITH_TYPE(\n";
3855 OS << " \"asm-matcher\",\n";
3856 OS << " dbgs() << \"second invalid operand, giving up on "
3857 "this opcode\\n\");\n";
3858 OS << " MultipleInvalidOperands = true;\n";
3859 OS << " break;\n";
3860 OS << " }\n";
3861 OS << " } else {\n";
3862 OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"but formal "
3863 "operand not required\\n\");\n";
3864 OS << " if (isSubclass(Formal, OptionalMatchClass)) {\n";
3865 OS << " OptionalOperandsMask.set("
3866 << (HasMnemonicFirst ? "FormalIdx + 1" : "FormalIdx") << ");\n";
3867 OS << " }\n";
3868 OS << " }\n";
3869 OS << " continue;\n";
3870 } else {
3871 OS << " if (Formal == InvalidMatchClass) {\n";
3872 if (HasOptionalOperands) {
3873 OS << " OptionalOperandsMask.set("
3874 << (HasMnemonicFirst ? "FormalIdx + 1, " : "FormalIdx, ")
3875 << MaxNumOperands + HasMnemonicFirst << ");\n";
3876 }
3877 OS << " break;\n";
3878 OS << " }\n";
3879 OS << " if (isSubclass(Formal, OptionalMatchClass)) {\n";
3880 if (HasOptionalOperands)
3881 OS << " OptionalOperandsMask.set("
3882 << (HasMnemonicFirst ? "FormalIdx + 1" : "FormalIdx") << ");\n";
3883 OS << " continue;\n";
3884 OS << " }\n";
3885 OS << " OperandsValid = false;\n";
3886 OS << " ErrorInfo = ActualIdx;\n";
3887 OS << " break;\n";
3888 }
3889 OS << " }\n";
3890 OS << " MCParsedAsmOperand &Actual = *Operands[ActualIdx];\n";
3891 OS << " unsigned Diag = validateOperandClass(Actual, Formal, *STI);\n";
3892 OS << " if (Diag == Match_Success) {\n";
3893 OS << " DEBUG_WITH_TYPE(\"asm-matcher\",\n";
3894 OS << " dbgs() << \"match success using generic "
3895 "matcher\\n\");\n";
3896 OS << " ++ActualIdx;\n";
3897 OS << " continue;\n";
3898 OS << " }\n";
3899 OS << " // If the generic handler indicates an invalid operand\n";
3900 OS << " // failure, check for a special case.\n";
3901 OS << " if (Diag != Match_Success) {\n";
3902 OS << " unsigned TargetDiag = validateTargetOperandClass(Actual, "
3903 "Formal);\n";
3904 OS << " if (TargetDiag == Match_Success) {\n";
3905 OS << " DEBUG_WITH_TYPE(\"asm-matcher\",\n";
3906 OS << " dbgs() << \"match success using target "
3907 "matcher\\n\");\n";
3908 OS << " ++ActualIdx;\n";
3909 OS << " continue;\n";
3910 OS << " }\n";
3911 OS << " // If the target matcher returned a specific error code use\n";
3912 OS << " // that, else use the one from the generic matcher.\n";
3913 OS << " if (TargetDiag != Match_InvalidOperand && "
3914 "HasRequiredFeatures)\n";
3915 OS << " Diag = TargetDiag;\n";
3916 OS << " }\n";
3917 OS << " // If current formal operand wasn't matched and it is optional\n"
3918 << " // then try to match next formal operand\n";
3919 OS << " if (Diag == Match_InvalidOperand "
3920 << "&& isSubclass(Formal, OptionalMatchClass)) {\n";
3921 if (HasOptionalOperands)
3922 OS << " OptionalOperandsMask.set("
3923 << (HasMnemonicFirst ? "FormalIdx + 1" : "FormalIdx") << ");\n";
3924 OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"ignoring "
3925 "optional operand\\n\");\n";
3926 OS << " continue;\n";
3927 OS << " }\n";
3928
3929 if (ReportMultipleNearMisses) {
3930 OS << " if (!OperandNearMiss) {\n";
3931 OS << " // If this is the first invalid operand we have seen, "
3932 "record some\n";
3933 OS << " // information about it.\n";
3934 OS << " DEBUG_WITH_TYPE(\n";
3935 OS << " \"asm-matcher\",\n";
3936 OS << " dbgs()\n";
3937 OS << " << \"operand match failed, recording near-miss with "
3938 "diag code \"\n";
3939 OS << " << Diag << \"\\n\");\n";
3940 OS << " OperandNearMiss =\n";
3941 OS << " NearMissInfo::getMissedOperand(Diag, Formal, "
3942 "it->Opcode, ActualIdx);\n";
3943 OS << " ++ActualIdx;\n";
3944 OS << " } else {\n";
3945 OS << " // If more than one operand is invalid, give up on this "
3946 "match entry.\n";
3947 OS << " DEBUG_WITH_TYPE(\n";
3948 OS << " \"asm-matcher\",\n";
3949 OS << " dbgs() << \"second operand mismatch, skipping this "
3950 "opcode\\n\");\n";
3951 OS << " MultipleInvalidOperands = true;\n";
3952 OS << " break;\n";
3953 OS << " }\n";
3954 OS << " }\n\n";
3955 } else {
3956 OS << " // If this operand is broken for all of the instances of "
3957 "this\n";
3958 OS << " // mnemonic, keep track of it so we can report loc info.\n";
3959 OS << " // If we already had a match that only failed due to a\n";
3960 OS << " // target predicate, that diagnostic is preferred.\n";
3961 OS << " if (!HadMatchOtherThanPredicate &&\n";
3962 OS << " (it == MnemonicRange.first || ErrorInfo <= ActualIdx)) "
3963 "{\n";
3964 OS << " if (HasRequiredFeatures && (ErrorInfo != ActualIdx || Diag "
3965 "!= Match_InvalidOperand))\n";
3966 OS << " RetCode = Diag;\n";
3967 OS << " ErrorInfo = ActualIdx;\n";
3968 OS << " }\n";
3969 OS << " // Otherwise, just reject this instance of the mnemonic.\n";
3970 OS << " OperandsValid = false;\n";
3971 OS << " break;\n";
3972 OS << " }\n\n";
3973 }
3974
3975 if (ReportMultipleNearMisses)
3976 OS << " if (MultipleInvalidOperands) {\n";
3977 else
3978 OS << " if (!OperandsValid) {\n";
3979 OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"Opcode result: "
3980 "multiple \"\n";
3981 OS << " \"operand mismatches, "
3982 "ignoring \"\n";
3983 OS << " \"this opcode\\n\");\n";
3984 OS << " continue;\n";
3985 OS << " }\n";
3986
3987 // Emit check that the required features are available.
3988 OS << " if (!HasRequiredFeatures) {\n";
3989 if (!ReportMultipleNearMisses)
3990 OS << " HadMatchOtherThanFeatures = true;\n";
3991 OS << " FeatureBitset NewMissingFeatures = RequiredFeatures & "
3992 "~AvailableFeatures;\n";
3993 OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"Missing target "
3994 "features:\";\n";
3995 OS << " for (unsigned I = 0, E = "
3996 "NewMissingFeatures.size(); I != E; ++I)\n";
3997 OS << " if (NewMissingFeatures[I])\n";
3998 OS << " dbgs() << ' ' << I;\n";
3999 OS << " dbgs() << \"\\n\");\n";
4000 if (ReportMultipleNearMisses) {
4001 OS << " FeaturesNearMiss = "
4002 "NearMissInfo::getMissedFeature(NewMissingFeatures);\n";
4003 } else {
4004 OS << " if (NewMissingFeatures.count() <=\n"
4005 " MissingFeatures.count())\n";
4006 OS << " MissingFeatures = NewMissingFeatures;\n";
4007 OS << " continue;\n";
4008 }
4009 OS << " }\n";
4010 OS << "\n";
4011 OS << " Inst.clear();\n\n";
4012 OS << " Inst.setOpcode(it->Opcode);\n";
4013 // Verify the instruction with the target-specific match predicate function.
4014 OS << " // We have a potential match but have not rendered the operands.\n"
4015 << " // Check the target predicate to handle any context sensitive\n"
4016 " // constraints.\n"
4017 << " // For example, Ties that are referenced multiple times must be\n"
4018 " // checked here to ensure the input is the same for each match\n"
4019 " // constraints. If we leave it any later the ties will have been\n"
4020 " // canonicalized\n"
4021 << " unsigned MatchResult;\n"
4022 << " if ((MatchResult = checkEarlyTargetMatchPredicate(Inst, "
4023 "Operands)) != Match_Success) {\n"
4024 << " Inst.clear();\n";
4025 OS << " DEBUG_WITH_TYPE(\n";
4026 OS << " \"asm-matcher\",\n";
4027 OS << " dbgs() << \"Early target match predicate failed with diag "
4028 "code \"\n";
4029 OS << " << MatchResult << \"\\n\");\n";
4030 if (ReportMultipleNearMisses) {
4031 OS << " EarlyPredicateNearMiss = "
4032 "NearMissInfo::getMissedPredicate(MatchResult);\n";
4033 } else {
4034 OS << " RetCode = MatchResult;\n"
4035 << " HadMatchOtherThanPredicate = true;\n"
4036 << " continue;\n";
4037 }
4038 OS << " }\n\n";
4039
4040 if (ReportMultipleNearMisses) {
4041 OS << " // If we did not successfully match the operands, then we can't "
4042 "convert to\n";
4043 OS << " // an MCInst, so bail out on this instruction variant now.\n";
4044 OS << " if (OperandNearMiss) {\n";
4045 OS << " // If the operand mismatch was the only problem, report it as "
4046 "a near-miss.\n";
4047 OS << " if (NearMisses && !FeaturesNearMiss && "
4048 "!EarlyPredicateNearMiss) {\n";
4049 OS << " DEBUG_WITH_TYPE(\n";
4050 OS << " \"asm-matcher\",\n";
4051 OS << " dbgs()\n";
4052 OS << " << \"Opcode result: one mismatched operand, adding "
4053 "near-miss\\n\");\n";
4054 OS << " NearMisses->push_back(OperandNearMiss);\n";
4055 OS << " } else {\n";
4056 OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"Opcode result: "
4057 "multiple \"\n";
4058 OS << " \"types of "
4059 "mismatch, so not \"\n";
4060 OS << " \"reporting "
4061 "near-miss\\n\");\n";
4062 OS << " }\n";
4063 OS << " continue;\n";
4064 OS << " }\n\n";
4065 }
4066
4067 // When converting parsed operands to MCInst we need to know whether optional
4068 // operands were parsed or not so that we can choose the correct converter
4069 // function. We also need to know this when checking tied operand constraints.
4070 // DefaultsOffset is an array of deltas between the formal (MCInst) and the
4071 // actual (parsed operand array) operand indices. When all optional operands
4072 // are present, all elements of the array are zeros. If some of the optional
4073 // operands are absent, the array might look like '0, 0, 1, 1, 1, 2, 2, 3',
4074 // where each increment in value reflects the absence of an optional operand.
4075 if (HasOptionalOperands) {
4076 OS << " unsigned DefaultsOffset[" << (MaxNumOperands + 1)
4077 << "] = { 0 };\n";
4078 OS << " assert(OptionalOperandsMask.size() == "
4079 << (MaxNumOperands + HasMnemonicFirst) << ");\n";
4080 OS << " for (unsigned i = 0, NumDefaults = 0; i < " << (MaxNumOperands)
4081 << "; ++i) {\n";
4082 OS << " NumDefaults += (OptionalOperandsMask[i] ? 1 : 0);\n";
4083 OS << " DefaultsOffset[i + 1] = NumDefaults;\n";
4084 OS << " }\n\n";
4085 }
4086
4087 OS << " if (matchingInlineAsm) {\n";
4088 OS << " convertToMapAndConstraints(it->ConvertFn, Operands);\n";
4089 if (!ReportMultipleNearMisses) {
4090 if (HasOptionalOperands) {
4091 OS << " if (!checkAsmTiedOperandConstraints(*this, it->ConvertFn, "
4092 "Operands,\n";
4093 OS << " DefaultsOffset, "
4094 "ErrorInfo))\n";
4095 } else {
4096 OS << " if (!checkAsmTiedOperandConstraints(*this, it->ConvertFn, "
4097 "Operands,\n";
4098 OS << " ErrorInfo))\n";
4099 }
4100 OS << " return Match_InvalidTiedOperand;\n";
4101 OS << "\n";
4102 }
4103 OS << " return Match_Success;\n";
4104 OS << " }\n\n";
4105 OS << " // We have selected a definite instruction, convert the parsed\n"
4106 << " // operands into the appropriate MCInst.\n";
4107 if (HasOptionalOperands) {
4108 OS << " convertToMCInst(it->ConvertFn, Inst, it->Opcode, Operands,\n"
4109 << " OptionalOperandsMask, DefaultsOffset);\n";
4110 } else {
4111 OS << " convertToMCInst(it->ConvertFn, Inst, it->Opcode, Operands);\n";
4112 }
4113 OS << "\n";
4114
4115 // Verify the instruction with the target-specific match predicate function.
4116 OS << " // We have a potential match. Check the target predicate to\n"
4117 << " // handle any context sensitive constraints.\n"
4118 << " if ((MatchResult = checkTargetMatchPredicate(Inst)) !="
4119 << " Match_Success) {\n"
4120 << " DEBUG_WITH_TYPE(\"asm-matcher\",\n"
4121 << " dbgs() << \"Target match predicate failed with "
4122 "diag code \"\n"
4123 << " << MatchResult << \"\\n\");\n"
4124 << " Inst.clear();\n";
4125 if (ReportMultipleNearMisses) {
4126 OS << " LatePredicateNearMiss = "
4127 "NearMissInfo::getMissedPredicate(MatchResult);\n";
4128 } else {
4129 OS << " RetCode = MatchResult;\n"
4130 << " HadMatchOtherThanPredicate = true;\n"
4131 << " continue;\n";
4132 }
4133 OS << " }\n\n";
4134
4135 if (ReportMultipleNearMisses) {
4136 OS << " int NumNearMisses = ((int)(bool)OperandNearMiss +\n";
4137 OS << " (int)(bool)FeaturesNearMiss +\n";
4138 OS << " (int)(bool)EarlyPredicateNearMiss +\n";
4139 OS << " (int)(bool)LatePredicateNearMiss);\n";
4140 OS << " if (NumNearMisses == 1) {\n";
4141 OS << " // We had exactly one type of near-miss, so add that to the "
4142 "list.\n";
4143 OS << " assert(!OperandNearMiss && \"OperandNearMiss was handled "
4144 "earlier\");\n";
4145 OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"Opcode result: "
4146 "found one type of \"\n";
4147 OS << " \"mismatch, so "
4148 "reporting a \"\n";
4149 OS << " \"near-miss\\n\");\n";
4150 OS << " if (NearMisses && FeaturesNearMiss)\n";
4151 OS << " NearMisses->push_back(FeaturesNearMiss);\n";
4152 OS << " else if (NearMisses && EarlyPredicateNearMiss)\n";
4153 OS << " NearMisses->push_back(EarlyPredicateNearMiss);\n";
4154 OS << " else if (NearMisses && LatePredicateNearMiss)\n";
4155 OS << " NearMisses->push_back(LatePredicateNearMiss);\n";
4156 OS << "\n";
4157 OS << " continue;\n";
4158 OS << " } else if (NumNearMisses > 1) {\n";
4159 OS << " // This instruction missed in more than one way, so ignore "
4160 "it.\n";
4161 OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"Opcode result: "
4162 "multiple \"\n";
4163 OS << " \"types of mismatch, "
4164 "so not \"\n";
4165 OS << " \"reporting "
4166 "near-miss\\n\");\n";
4167 OS << " continue;\n";
4168 OS << " }\n";
4169 }
4170
4171 // Call the post-processing function, if used.
4172 StringRef InsnCleanupFn = AsmParser->getValueAsString(FieldName: "AsmParserInstCleanup");
4173 if (!InsnCleanupFn.empty())
4174 OS << " " << InsnCleanupFn << "(Inst);\n";
4175
4176 if (HasDeprecation) {
4177 OS << " std::string Info;\n";
4178 OS << " if "
4179 "(!getParser().getTargetParser().getTargetOptions()."
4180 "MCNoDeprecatedWarn &&\n";
4181 OS << " MII.getDeprecatedInfo(Inst, getSTI(), Info)) {\n";
4182 OS << " SMLoc Loc = ((" << Target.getName()
4183 << "Operand &)*Operands[0]).getStartLoc();\n";
4184 OS << " getParser().Warning(Loc, Info, {});\n";
4185 OS << " }\n";
4186 }
4187
4188 if (!ReportMultipleNearMisses) {
4189 if (HasOptionalOperands) {
4190 OS << " if (!checkAsmTiedOperandConstraints(*this, it->ConvertFn, "
4191 "Operands,\n";
4192 OS << " DefaultsOffset, "
4193 "ErrorInfo))\n";
4194 } else {
4195 OS << " if (!checkAsmTiedOperandConstraints(*this, it->ConvertFn, "
4196 "Operands,\n";
4197 OS << " ErrorInfo))\n";
4198 }
4199 OS << " return Match_InvalidTiedOperand;\n";
4200 OS << "\n";
4201 }
4202
4203 OS << " DEBUG_WITH_TYPE(\n";
4204 OS << " \"asm-matcher\",\n";
4205 OS << " dbgs() << \"Opcode result: complete match, selecting this "
4206 "opcode\\n\");\n";
4207 OS << " return Match_Success;\n";
4208 OS << " }\n\n";
4209
4210 if (ReportMultipleNearMisses) {
4211 OS << " // No instruction variants matched exactly.\n";
4212 OS << " return Match_NearMisses;\n";
4213 } else {
4214 OS << " // Okay, we had no match. Try to return a useful error code.\n";
4215 OS << " if (HadMatchOtherThanPredicate || !HadMatchOtherThanFeatures)\n";
4216 OS << " return RetCode;\n\n";
4217 OS << " ErrorInfo = 0;\n";
4218 OS << " return Match_MissingFeature;\n";
4219 }
4220 OS << "}\n\n";
4221
4222 if (!Info.OperandMatchInfo.empty())
4223 emitCustomOperandParsing(OS, Target, Info, ClassName, StringTable,
4224 MaxMnemonicIndex, MaxFeaturesIndex: FeatureBitsets.size(),
4225 HasMnemonicFirst, AsmParser: *AsmParser);
4226
4227 OS << "#endif // GET_MATCHER_IMPLEMENTATION\n\n";
4228
4229 OS << "\n#ifdef GET_MNEMONIC_SPELL_CHECKER\n";
4230 OS << "#undef GET_MNEMONIC_SPELL_CHECKER\n\n";
4231
4232 emitMnemonicSpellChecker(OS, Target, VariantCount);
4233
4234 OS << "#endif // GET_MNEMONIC_SPELL_CHECKER\n\n";
4235
4236 OS << "\n#ifdef GET_MNEMONIC_CHECKER\n";
4237 OS << "#undef GET_MNEMONIC_CHECKER\n\n";
4238
4239 emitMnemonicChecker(OS, Target, VariantCount, HasMnemonicFirst,
4240 HasMnemonicAliases);
4241
4242 OS << "#endif // GET_MNEMONIC_CHECKER\n\n";
4243}
4244
4245static TableGen::Emitter::OptClass<AsmMatcherEmitter>
4246 X("gen-asm-matcher", "Generate assembly instruction matcher");
4247