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