LLVM  16.0.0git
TGParser.cpp
Go to the documentation of this file.
1 //===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
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 // Implement the Parser for TableGen.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "TGParser.h"
14 #include "llvm/ADT/DenseMapInfo.h"
15 #include "llvm/ADT/None.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Config/llvm-config.h"
20 #include "llvm/Support/Casting.h"
21 #include "llvm/Support/Compiler.h"
24 #include <algorithm>
25 #include <cassert>
26 #include <cstdint>
27 #include <limits>
28 
29 using namespace llvm;
30 
31 //===----------------------------------------------------------------------===//
32 // Support Code for the Semantic Actions.
33 //===----------------------------------------------------------------------===//
34 
35 namespace llvm {
36 
41 
42  SubClassReference() : Rec(nullptr) {}
43 
44  bool isInvalid() const { return Rec == nullptr; }
45 };
46 
51 
52  SubMultiClassReference() : MC(nullptr) {}
53 
54  bool isInvalid() const { return MC == nullptr; }
55  void dump() const;
56 };
57 
58 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
60  errs() << "Multiclass:\n";
61 
62  MC->dump();
63 
64  errs() << "Template args:\n";
65  for (Init *TA : TemplateArgs)
66  TA->dump();
67 }
68 #endif
69 
70 } // end namespace llvm
71 
72 static bool checkBitsConcrete(Record &R, const RecordVal &RV) {
73  BitsInit *BV = cast<BitsInit>(RV.getValue());
74  for (unsigned i = 0, e = BV->getNumBits(); i != e; ++i) {
75  Init *Bit = BV->getBit(i);
76  bool IsReference = false;
77  if (auto VBI = dyn_cast<VarBitInit>(Bit)) {
78  if (auto VI = dyn_cast<VarInit>(VBI->getBitVar())) {
79  if (R.getValue(VI->getName()))
80  IsReference = true;
81  }
82  } else if (isa<VarInit>(Bit)) {
83  IsReference = true;
84  }
85  if (!(IsReference || Bit->isConcrete()))
86  return false;
87  }
88  return true;
89 }
90 
91 static void checkConcrete(Record &R) {
92  for (const RecordVal &RV : R.getValues()) {
93  // HACK: Disable this check for variables declared with 'field'. This is
94  // done merely because existing targets have legitimate cases of
95  // non-concrete variables in helper defs. Ideally, we'd introduce a
96  // 'maybe' or 'optional' modifier instead of this.
97  if (RV.isNonconcreteOK())
98  continue;
99 
100  if (Init *V = RV.getValue()) {
101  bool Ok = isa<BitsInit>(V) ? checkBitsConcrete(R, RV) : V->isConcrete();
102  if (!Ok) {
103  PrintError(R.getLoc(),
104  Twine("Initializer of '") + RV.getNameInitAsString() +
105  "' in '" + R.getNameInitAsString() +
106  "' could not be fully resolved: " +
107  RV.getValue()->getAsString());
108  }
109  }
110  }
111 }
112 
113 /// Return an Init with a qualifier prefix referring
114 /// to CurRec's name.
115 static Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass, Init *Name,
116  StringRef Scoper) {
117  RecordKeeper &RK = CurRec.getRecords();
118  Init *NewName = BinOpInit::getStrConcat(CurRec.getNameInit(),
119  StringInit::get(RK, Scoper));
120  NewName = BinOpInit::getStrConcat(NewName, Name);
121  if (CurMultiClass && Scoper != "::") {
122  Init *Prefix = BinOpInit::getStrConcat(CurMultiClass->Rec.getNameInit(),
123  StringInit::get(RK, "::"));
124  NewName = BinOpInit::getStrConcat(Prefix, NewName);
125  }
126 
127  if (BinOpInit *BinOp = dyn_cast<BinOpInit>(NewName))
128  NewName = BinOp->Fold(&CurRec);
129  return NewName;
130 }
131 
132 /// Return the qualified version of the implicit 'NAME' template argument.
134  MultiClass *MC = nullptr) {
135  return QualifyName(Rec, MC, StringInit::get(Rec.getRecords(), "NAME"),
136  MC ? "::" : ":");
137 }
138 
140  return QualifiedNameOfImplicitName(MC->Rec, MC);
141 }
142 
143 bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
144  if (!CurRec)
145  CurRec = &CurMultiClass->Rec;
146 
147  if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
148  // The value already exists in the class, treat this as a set.
149  if (ERV->setValue(RV.getValue()))
150  return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
151  RV.getType()->getAsString() + "' is incompatible with " +
152  "previous definition of type '" +
153  ERV->getType()->getAsString() + "'");
154  } else {
155  CurRec->addValue(RV);
156  }
157  return false;
158 }
159 
160 /// SetValue -
161 /// Return true on error, false on success.
162 bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
163  ArrayRef<unsigned> BitList, Init *V,
164  bool AllowSelfAssignment, bool OverrideDefLoc) {
165  if (!V) return false;
166 
167  if (!CurRec) CurRec = &CurMultiClass->Rec;
168 
169  RecordVal *RV = CurRec->getValue(ValName);
170  if (!RV)
171  return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
172  "' unknown!");
173 
174  // Do not allow assignments like 'X = X'. This will just cause infinite loops
175  // in the resolution machinery.
176  if (BitList.empty())
177  if (VarInit *VI = dyn_cast<VarInit>(V))
178  if (VI->getNameInit() == ValName && !AllowSelfAssignment)
179  return Error(Loc, "Recursion / self-assignment forbidden");
180 
181  // If we are assigning to a subset of the bits in the value... then we must be
182  // assigning to a field of BitsRecTy, which must have a BitsInit
183  // initializer.
184  //
185  if (!BitList.empty()) {
186  BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue());
187  if (!CurVal)
188  return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
189  "' is not a bits type");
190 
191  // Convert the incoming value to a bits type of the appropriate size...
192  Init *BI = V->getCastTo(BitsRecTy::get(Records, BitList.size()));
193  if (!BI)
194  return Error(Loc, "Initializer is not compatible with bit range");
195 
196  SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
197 
198  // Loop over bits, assigning values as appropriate.
199  for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
200  unsigned Bit = BitList[i];
201  if (NewBits[Bit])
202  return Error(Loc, "Cannot set bit #" + Twine(Bit) + " of value '" +
203  ValName->getAsUnquotedString() + "' more than once");
204  NewBits[Bit] = BI->getBit(i);
205  }
206 
207  for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
208  if (!NewBits[i])
209  NewBits[i] = CurVal->getBit(i);
210 
211  V = BitsInit::get(Records, NewBits);
212  }
213 
214  if (OverrideDefLoc ? RV->setValue(V, Loc) : RV->setValue(V)) {
215  std::string InitType;
216  if (BitsInit *BI = dyn_cast<BitsInit>(V))
217  InitType = (Twine("' of type bit initializer with length ") +
218  Twine(BI->getNumBits())).str();
219  else if (TypedInit *TI = dyn_cast<TypedInit>(V))
220  InitType = (Twine("' of type '") + TI->getType()->getAsString()).str();
221  return Error(Loc, "Field '" + ValName->getAsUnquotedString() +
222  "' of type '" + RV->getType()->getAsString() +
223  "' is incompatible with value '" +
224  V->getAsString() + InitType + "'");
225  }
226  return false;
227 }
228 
229 /// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
230 /// args as SubClass's template arguments.
231 bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
232  Record *SC = SubClass.Rec;
233  MapResolver R(CurRec);
234 
235  // Loop over all the subclass record's fields. Add template arguments
236  // to the resolver map. Add regular fields to the new record.
237  for (const RecordVal &Field : SC->getValues()) {
238  if (Field.isTemplateArg()) {
239  R.set(Field.getNameInit(), Field.getValue());
240  } else {
241  if (AddValue(CurRec, SubClass.RefRange.Start, Field))
242  return true;
243  }
244  }
245 
246  ArrayRef<Init *> TArgs = SC->getTemplateArgs();
247  assert(SubClass.TemplateArgs.size() <= TArgs.size() &&
248  "Too many template arguments allowed");
249 
250  // Loop over the template argument names. If a value was specified,
251  // reset the map value. If not and there was no default, complain.
252  for (unsigned I = 0, E = TArgs.size(); I != E; ++I) {
253  if (I < SubClass.TemplateArgs.size())
254  R.set(TArgs[I], SubClass.TemplateArgs[I]);
255  else if (!R.isComplete(TArgs[I]))
256  return Error(SubClass.RefRange.Start,
257  "Value not specified for template argument '" +
258  TArgs[I]->getAsUnquotedString() + "' (#" + Twine(I) +
259  ") of parent class '" + SC->getNameInitAsString() + "'");
260  }
261 
262  // Copy the subclass record's assertions to the new record.
263  CurRec->appendAssertions(SC);
264 
265  Init *Name;
266  if (CurRec->isClass())
268  StringRecTy::get(Records));
269  else
270  Name = CurRec->getNameInit();
271  R.set(QualifiedNameOfImplicitName(*SC), Name);
272 
273  CurRec->resolveReferences(R);
274 
275  // Since everything went well, we can now set the "superclass" list for the
276  // current record.
277  ArrayRef<std::pair<Record *, SMRange>> SCs = SC->getSuperClasses();
278  for (const auto &SCPair : SCs) {
279  if (CurRec->isSubClassOf(SCPair.first))
280  return Error(SubClass.RefRange.Start,
281  "Already subclass of '" + SCPair.first->getName() + "'!\n");
282  CurRec->addSuperClass(SCPair.first, SCPair.second);
283  }
284 
285  if (CurRec->isSubClassOf(SC))
286  return Error(SubClass.RefRange.Start,
287  "Already subclass of '" + SC->getName() + "'!\n");
288  CurRec->addSuperClass(SC, SubClass.RefRange);
289  return false;
290 }
291 
292 bool TGParser::AddSubClass(RecordsEntry &Entry, SubClassReference &SubClass) {
293  if (Entry.Rec)
294  return AddSubClass(Entry.Rec.get(), SubClass);
295 
296  if (Entry.Assertion)
297  return false;
298 
299  for (auto &E : Entry.Loop->Entries) {
300  if (AddSubClass(E, SubClass))
301  return true;
302  }
303 
304  return false;
305 }
306 
307 /// AddSubMultiClass - Add SubMultiClass as a subclass to
308 /// CurMC, resolving its template args as SubMultiClass's
309 /// template arguments.
310 bool TGParser::AddSubMultiClass(MultiClass *CurMC,
311  SubMultiClassReference &SubMultiClass) {
312  MultiClass *SMC = SubMultiClass.MC;
313 
314  ArrayRef<Init *> SMCTArgs = SMC->Rec.getTemplateArgs();
315  if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
316  return Error(SubMultiClass.RefRange.Start,
317  "More template args specified than expected");
318 
319  // Prepare the mapping of template argument name to value, filling in default
320  // values if necessary.
321  SubstStack TemplateArgs;
322  for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
323  if (i < SubMultiClass.TemplateArgs.size()) {
324  TemplateArgs.emplace_back(SMCTArgs[i], SubMultiClass.TemplateArgs[i]);
325  } else {
326  Init *Default = SMC->Rec.getValue(SMCTArgs[i])->getValue();
327  if (!Default->isComplete()) {
328  return Error(SubMultiClass.RefRange.Start,
329  "value not specified for template argument #" + Twine(i) +
330  " (" + SMCTArgs[i]->getAsUnquotedString() +
331  ") of multiclass '" + SMC->Rec.getNameInitAsString() +
332  "'");
333  }
334  TemplateArgs.emplace_back(SMCTArgs[i], Default);
335  }
336  }
337 
338  TemplateArgs.emplace_back(QualifiedNameOfImplicitName(SMC),
340  StringRecTy::get(Records)));
341 
342  // Add all of the defs in the subclass into the current multiclass.
343  return resolve(SMC->Entries, TemplateArgs, false, &CurMC->Entries);
344 }
345 
346 /// Add a record, foreach loop, or assertion to the current context.
347 bool TGParser::addEntry(RecordsEntry E) {
348  assert((!!E.Rec + !!E.Loop + !!E.Assertion) == 1 &&
349  "RecordsEntry has invalid number of items");
350 
351  // If we are parsing a loop, add it to the loop's entries.
352  if (!Loops.empty()) {
353  Loops.back()->Entries.push_back(std::move(E));
354  return false;
355  }
356 
357  // If it is a loop, then resolve and perform the loop.
358  if (E.Loop) {
359  SubstStack Stack;
360  return resolve(*E.Loop, Stack, CurMultiClass == nullptr,
361  CurMultiClass ? &CurMultiClass->Entries : nullptr);
362  }
363 
364  // If we are parsing a multiclass, add it to the multiclass's entries.
365  if (CurMultiClass) {
366  CurMultiClass->Entries.push_back(std::move(E));
367  return false;
368  }
369 
370  // If it is an assertion, then it's a top-level one, so check it.
371  if (E.Assertion) {
372  CheckAssert(E.Assertion->Loc, E.Assertion->Condition, E.Assertion->Message);
373  return false;
374  }
375 
376  // It must be a record, so finish it off.
377  return addDefOne(std::move(E.Rec));
378 }
379 
380 /// Resolve the entries in \p Loop, going over inner loops recursively
381 /// and making the given subsitutions of (name, value) pairs.
382 ///
383 /// The resulting records are stored in \p Dest if non-null. Otherwise, they
384 /// are added to the global record keeper.
385 bool TGParser::resolve(const ForeachLoop &Loop, SubstStack &Substs,
386  bool Final, std::vector<RecordsEntry> *Dest,
387  SMLoc *Loc) {
388  MapResolver R;
389  for (const auto &S : Substs)
390  R.set(S.first, S.second);
391  Init *List = Loop.ListValue->resolveReferences(R);
392  auto LI = dyn_cast<ListInit>(List);
393  if (!LI) {
394  if (!Final) {
395  Dest->emplace_back(std::make_unique<ForeachLoop>(Loop.Loc, Loop.IterVar,
396  List));
397  return resolve(Loop.Entries, Substs, Final, &Dest->back().Loop->Entries,
398  Loc);
399  }
400 
401  PrintError(Loop.Loc, Twine("attempting to loop over '") +
402  List->getAsString() + "', expected a list");
403  return true;
404  }
405 
406  bool Error = false;
407  for (auto *Elt : *LI) {
408  if (Loop.IterVar)
409  Substs.emplace_back(Loop.IterVar->getNameInit(), Elt);
410  Error = resolve(Loop.Entries, Substs, Final, Dest);
411  if (Loop.IterVar)
412  Substs.pop_back();
413  if (Error)
414  break;
415  }
416  return Error;
417 }
418 
419 /// Resolve the entries in \p Source, going over loops recursively and
420 /// making the given substitutions of (name, value) pairs.
421 ///
422 /// The resulting records are stored in \p Dest if non-null. Otherwise, they
423 /// are added to the global record keeper.
424 bool TGParser::resolve(const std::vector<RecordsEntry> &Source,
425  SubstStack &Substs, bool Final,
426  std::vector<RecordsEntry> *Dest, SMLoc *Loc) {
427  bool Error = false;
428  for (auto &E : Source) {
429  if (E.Loop) {
430  Error = resolve(*E.Loop, Substs, Final, Dest);
431 
432  } else if (E.Assertion) {
433  MapResolver R;
434  for (const auto &S : Substs)
435  R.set(S.first, S.second);
436  Init *Condition = E.Assertion->Condition->resolveReferences(R);
437  Init *Message = E.Assertion->Message->resolveReferences(R);
438 
439  if (Dest)
440  Dest->push_back(std::make_unique<Record::AssertionInfo>(
441  E.Assertion->Loc, Condition, Message));
442  else
443  CheckAssert(E.Assertion->Loc, Condition, Message);
444 
445  } else {
446  auto Rec = std::make_unique<Record>(*E.Rec);
447  if (Loc)
448  Rec->appendLoc(*Loc);
449 
450  MapResolver R(Rec.get());
451  for (const auto &S : Substs)
452  R.set(S.first, S.second);
453  Rec->resolveReferences(R);
454 
455  if (Dest)
456  Dest->push_back(std::move(Rec));
457  else
458  Error = addDefOne(std::move(Rec));
459  }
460  if (Error)
461  break;
462  }
463  return Error;
464 }
465 
466 /// Resolve the record fully and add it to the record keeper.
467 bool TGParser::addDefOne(std::unique_ptr<Record> Rec) {
468  Init *NewName = nullptr;
469  if (Record *Prev = Records.getDef(Rec->getNameInitAsString())) {
470  if (!Rec->isAnonymous()) {
471  PrintError(Rec->getLoc(),
472  "def already exists: " + Rec->getNameInitAsString());
473  PrintNote(Prev->getLoc(), "location of previous definition");
474  return true;
475  }
476  NewName = Records.getNewAnonymousName();
477  }
478 
479  Rec->resolveReferences(NewName);
480  checkConcrete(*Rec);
481 
482  if (!isa<StringInit>(Rec->getNameInit())) {
483  PrintError(Rec->getLoc(), Twine("record name '") +
484  Rec->getNameInit()->getAsString() +
485  "' could not be fully resolved");
486  return true;
487  }
488 
489  // Check the assertions.
490  Rec->checkRecordAssertions();
491 
492  // If ObjectBody has template arguments, it's an error.
493  assert(Rec->getTemplateArgs().empty() && "How'd this get template args?");
494 
495  for (DefsetRecord *Defset : Defsets) {
496  DefInit *I = Rec->getDefInit();
497  if (!I->getType()->typeIsA(Defset->EltTy)) {
498  PrintError(Rec->getLoc(), Twine("adding record of incompatible type '") +
499  I->getType()->getAsString() +
500  "' to defset");
501  PrintNote(Defset->Loc, "location of defset declaration");
502  return true;
503  }
504  Defset->Elements.push_back(I);
505  }
506 
507  Records.addDef(std::move(Rec));
508  return false;
509 }
510 
511 //===----------------------------------------------------------------------===//
512 // Parser Code
513 //===----------------------------------------------------------------------===//
514 
515 /// isObjectStart - Return true if this is a valid first token for a statement.
517  return K == tgtok::Assert || K == tgtok::Class || K == tgtok::Def ||
518  K == tgtok::Defm || K == tgtok::Defset || K == tgtok::Defvar ||
519  K == tgtok::Foreach || K == tgtok::If || K == tgtok::Let ||
520  K == tgtok::MultiClass;
521 }
522 
523 bool TGParser::consume(tgtok::TokKind K) {
524  if (Lex.getCode() == K) {
525  Lex.Lex();
526  return true;
527  }
528  return false;
529 }
530 
531 /// ParseObjectName - If a valid object name is specified, return it. If no
532 /// name is specified, return the unset initializer. Return nullptr on parse
533 /// error.
534 /// ObjectName ::= Value [ '#' Value ]*
535 /// ObjectName ::= /*empty*/
536 ///
537 Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
538  switch (Lex.getCode()) {
539  case tgtok::colon:
540  case tgtok::semi:
541  case tgtok::l_brace:
542  // These are all of the tokens that can begin an object body.
543  // Some of these can also begin values but we disallow those cases
544  // because they are unlikely to be useful.
545  return UnsetInit::get(Records);
546  default:
547  break;
548  }
549 
550  Record *CurRec = nullptr;
551  if (CurMultiClass)
552  CurRec = &CurMultiClass->Rec;
553 
554  Init *Name = ParseValue(CurRec, StringRecTy::get(Records), ParseNameMode);
555  if (!Name)
556  return nullptr;
557 
558  if (CurMultiClass) {
559  Init *NameStr = QualifiedNameOfImplicitName(CurMultiClass);
560  HasReferenceResolver R(NameStr);
561  Name->resolveReferences(R);
562  if (!R.found())
564  VarInit::get(NameStr, StringRecTy::get(Records)), Name);
565  }
566 
567  return Name;
568 }
569 
570 /// ParseClassID - Parse and resolve a reference to a class name. This returns
571 /// null on error.
572 ///
573 /// ClassID ::= ID
574 ///
575 Record *TGParser::ParseClassID() {
576  if (Lex.getCode() != tgtok::Id) {
577  TokError("expected name for ClassID");
578  return nullptr;
579  }
580 
581  Record *Result = Records.getClass(Lex.getCurStrVal());
582  if (!Result) {
583  std::string Msg("Couldn't find class '" + Lex.getCurStrVal() + "'");
584  if (MultiClasses[Lex.getCurStrVal()].get())
585  TokError(Msg + ". Use 'defm' if you meant to use multiclass '" +
586  Lex.getCurStrVal() + "'");
587  else
588  TokError(Msg);
589  } else if (TrackReferenceLocs) {
590  Result->appendReferenceLoc(Lex.getLocRange());
591  }
592 
593  Lex.Lex();
594  return Result;
595 }
596 
597 /// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
598 /// This returns null on error.
599 ///
600 /// MultiClassID ::= ID
601 ///
602 MultiClass *TGParser::ParseMultiClassID() {
603  if (Lex.getCode() != tgtok::Id) {
604  TokError("expected name for MultiClassID");
605  return nullptr;
606  }
607 
608  MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
609  if (!Result)
610  TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
611 
612  Lex.Lex();
613  return Result;
614 }
615 
616 /// ParseSubClassReference - Parse a reference to a subclass or a
617 /// multiclass. This returns a SubClassRefTy with a null Record* on error.
618 ///
619 /// SubClassRef ::= ClassID
620 /// SubClassRef ::= ClassID '<' ValueList '>'
621 ///
622 SubClassReference TGParser::
623 ParseSubClassReference(Record *CurRec, bool isDefm) {
625  Result.RefRange.Start = Lex.getLoc();
626 
627  if (isDefm) {
628  if (MultiClass *MC = ParseMultiClassID())
629  Result.Rec = &MC->Rec;
630  } else {
631  Result.Rec = ParseClassID();
632  }
633  if (!Result.Rec) return Result;
634 
635  // If there is no template arg list, we're done.
636  if (!consume(tgtok::less)) {
637  Result.RefRange.End = Lex.getLoc();
638  return Result;
639  }
640 
641  if (ParseTemplateArgValueList(Result.TemplateArgs, CurRec, Result.Rec)) {
642  Result.Rec = nullptr; // Error parsing value list.
643  return Result;
644  }
645 
646  if (CheckTemplateArgValues(Result.TemplateArgs, Result.RefRange.Start,
647  Result.Rec)) {
648  Result.Rec = nullptr; // Error checking value list.
649  return Result;
650  }
651 
652  Result.RefRange.End = Lex.getLoc();
653  return Result;
654 }
655 
656 /// ParseSubMultiClassReference - Parse a reference to a subclass or to a
657 /// templated submulticlass. This returns a SubMultiClassRefTy with a null
658 /// Record* on error.
659 ///
660 /// SubMultiClassRef ::= MultiClassID
661 /// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
662 ///
663 SubMultiClassReference TGParser::
664 ParseSubMultiClassReference(MultiClass *CurMC) {
666  Result.RefRange.Start = Lex.getLoc();
667 
668  Result.MC = ParseMultiClassID();
669  if (!Result.MC) return Result;
670 
671  // If there is no template arg list, we're done.
672  if (!consume(tgtok::less)) {
673  Result.RefRange.End = Lex.getLoc();
674  return Result;
675  }
676 
677  if (ParseTemplateArgValueList(Result.TemplateArgs, &CurMC->Rec,
678  &Result.MC->Rec)) {
679  Result.MC = nullptr; // Error parsing value list.
680  return Result;
681  }
682 
683  Result.RefRange.End = Lex.getLoc();
684 
685  return Result;
686 }
687 
688 /// ParseRangePiece - Parse a bit/value range.
689 /// RangePiece ::= INTVAL
690 /// RangePiece ::= INTVAL '...' INTVAL
691 /// RangePiece ::= INTVAL '-' INTVAL
692 /// RangePiece ::= INTVAL INTVAL
693 // The last two forms are deprecated.
694 bool TGParser::ParseRangePiece(SmallVectorImpl<unsigned> &Ranges,
695  TypedInit *FirstItem) {
696  Init *CurVal = FirstItem;
697  if (!CurVal)
698  CurVal = ParseValue(nullptr);
699 
700  IntInit *II = dyn_cast_or_null<IntInit>(CurVal);
701  if (!II)
702  return TokError("expected integer or bitrange");
703 
704  int64_t Start = II->getValue();
705  int64_t End;
706 
707  if (Start < 0)
708  return TokError("invalid range, cannot be negative");
709 
710  switch (Lex.getCode()) {
711  default:
712  Ranges.push_back(Start);
713  return false;
714 
715  case tgtok::dotdotdot:
716  case tgtok::minus: {
717  Lex.Lex(); // eat
718 
719  Init *I_End = ParseValue(nullptr);
720  IntInit *II_End = dyn_cast_or_null<IntInit>(I_End);
721  if (!II_End) {
722  TokError("expected integer value as end of range");
723  return true;
724  }
725 
726  End = II_End->getValue();
727  break;
728  }
729  case tgtok::IntVal: {
730  End = -Lex.getCurIntVal();
731  Lex.Lex();
732  break;
733  }
734  }
735  if (End < 0)
736  return TokError("invalid range, cannot be negative");
737 
738  // Add to the range.
739  if (Start < End)
740  for (; Start <= End; ++Start)
741  Ranges.push_back(Start);
742  else
743  for (; Start >= End; --Start)
744  Ranges.push_back(Start);
745  return false;
746 }
747 
748 /// ParseRangeList - Parse a list of scalars and ranges into scalar values.
749 ///
750 /// RangeList ::= RangePiece (',' RangePiece)*
751 ///
752 void TGParser::ParseRangeList(SmallVectorImpl<unsigned> &Result) {
753  // Parse the first piece.
754  if (ParseRangePiece(Result)) {
755  Result.clear();
756  return;
757  }
758  while (consume(tgtok::comma))
759  // Parse the next range piece.
760  if (ParseRangePiece(Result)) {
761  Result.clear();
762  return;
763  }
764 }
765 
766 /// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
767 /// OptionalRangeList ::= '<' RangeList '>'
768 /// OptionalRangeList ::= /*empty*/
769 bool TGParser::ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges) {
770  SMLoc StartLoc = Lex.getLoc();
771  if (!consume(tgtok::less))
772  return false;
773 
774  // Parse the range list.
775  ParseRangeList(Ranges);
776  if (Ranges.empty()) return true;
777 
778  if (!consume(tgtok::greater)) {
779  TokError("expected '>' at end of range list");
780  return Error(StartLoc, "to match this '<'");
781  }
782  return false;
783 }
784 
785 /// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
786 /// OptionalBitList ::= '{' RangeList '}'
787 /// OptionalBitList ::= /*empty*/
788 bool TGParser::ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges) {
789  SMLoc StartLoc = Lex.getLoc();
790  if (!consume(tgtok::l_brace))
791  return false;
792 
793  // Parse the range list.
794  ParseRangeList(Ranges);
795  if (Ranges.empty()) return true;
796 
797  if (!consume(tgtok::r_brace)) {
798  TokError("expected '}' at end of bit list");
799  return Error(StartLoc, "to match this '{'");
800  }
801  return false;
802 }
803 
804 /// ParseType - Parse and return a tblgen type. This returns null on error.
805 ///
806 /// Type ::= STRING // string type
807 /// Type ::= CODE // code type
808 /// Type ::= BIT // bit type
809 /// Type ::= BITS '<' INTVAL '>' // bits<x> type
810 /// Type ::= INT // int type
811 /// Type ::= LIST '<' Type '>' // list<x> type
812 /// Type ::= DAG // dag type
813 /// Type ::= ClassID // Record Type
814 ///
815 RecTy *TGParser::ParseType() {
816  switch (Lex.getCode()) {
817  default: TokError("Unknown token when expecting a type"); return nullptr;
818  case tgtok::String:
819  case tgtok::Code:
820  Lex.Lex();
821  return StringRecTy::get(Records);
822  case tgtok::Bit:
823  Lex.Lex();
824  return BitRecTy::get(Records);
825  case tgtok::Int:
826  Lex.Lex();
827  return IntRecTy::get(Records);
828  case tgtok::Dag:
829  Lex.Lex();
830  return DagRecTy::get(Records);
831  case tgtok::Id:
832  if (Record *R = ParseClassID())
833  return RecordRecTy::get(R);
834  TokError("unknown class name");
835  return nullptr;
836  case tgtok::Bits: {
837  if (Lex.Lex() != tgtok::less) { // Eat 'bits'
838  TokError("expected '<' after bits type");
839  return nullptr;
840  }
841  if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
842  TokError("expected integer in bits<n> type");
843  return nullptr;
844  }
845  uint64_t Val = Lex.getCurIntVal();
846  if (Lex.Lex() != tgtok::greater) { // Eat count.
847  TokError("expected '>' at end of bits<n> type");
848  return nullptr;
849  }
850  Lex.Lex(); // Eat '>'
851  return BitsRecTy::get(Records, Val);
852  }
853  case tgtok::List: {
854  if (Lex.Lex() != tgtok::less) { // Eat 'bits'
855  TokError("expected '<' after list type");
856  return nullptr;
857  }
858  Lex.Lex(); // Eat '<'
859  RecTy *SubType = ParseType();
860  if (!SubType) return nullptr;
861 
862  if (!consume(tgtok::greater)) {
863  TokError("expected '>' at end of list<ty> type");
864  return nullptr;
865  }
866  return ListRecTy::get(SubType);
867  }
868  }
869 }
870 
871 /// ParseIDValue
872 Init *TGParser::ParseIDValue(Record *CurRec, StringInit *Name, SMRange NameLoc,
873  IDParseMode Mode) {
874  if (CurRec) {
875  if (RecordVal *RV = CurRec->getValue(Name)) {
876  if (TrackReferenceLocs)
877  RV->addReferenceLoc(NameLoc);
878  return VarInit::get(Name, RV->getType());
879  }
880  }
881 
882  if ((CurRec && CurRec->isClass()) || CurMultiClass) {
883  Init *TemplateArgName;
884  if (CurMultiClass) {
885  TemplateArgName =
886  QualifyName(CurMultiClass->Rec, CurMultiClass, Name, "::");
887  } else
888  TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
889 
890  Record *TemplateRec = CurMultiClass ? &CurMultiClass->Rec : CurRec;
891  if (TemplateRec->isTemplateArg(TemplateArgName)) {
892  RecordVal *RV = TemplateRec->getValue(TemplateArgName);
893  assert(RV && "Template arg doesn't exist??");
894  RV->setUsed(true);
895  if (TrackReferenceLocs)
896  RV->addReferenceLoc(NameLoc);
897  return VarInit::get(TemplateArgName, RV->getType());
898  } else if (Name->getValue() == "NAME") {
899  return VarInit::get(TemplateArgName, StringRecTy::get(Records));
900  }
901  }
902 
903  if (CurLocalScope)
904  if (Init *I = CurLocalScope->getVar(Name->getValue()))
905  return I;
906 
907  // If this is in a foreach loop, make sure it's not a loop iterator
908  for (const auto &L : Loops) {
909  if (L->IterVar) {
910  VarInit *IterVar = dyn_cast<VarInit>(L->IterVar);
911  if (IterVar && IterVar->getNameInit() == Name)
912  return IterVar;
913  }
914  }
915 
916  if (Mode == ParseNameMode)
917  return Name;
918 
919  if (Init *I = Records.getGlobal(Name->getValue())) {
920  // Add a reference to the global if it's a record.
921  if (TrackReferenceLocs) {
922  if (auto *Def = dyn_cast<DefInit>(I))
923  Def->getDef()->appendReferenceLoc(NameLoc);
924  }
925  return I;
926  }
927 
928  // Allow self-references of concrete defs, but delay the lookup so that we
929  // get the correct type.
930  if (CurRec && !CurRec->isClass() && !CurMultiClass &&
931  CurRec->getNameInit() == Name)
932  return UnOpInit::get(UnOpInit::CAST, Name, CurRec->getType());
933 
934  Error(NameLoc.Start, "Variable not defined: '" + Name->getValue() + "'");
935  return nullptr;
936 }
937 
938 /// ParseOperation - Parse an operator. This returns null on error.
939 ///
940 /// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
941 ///
942 Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
943  switch (Lex.getCode()) {
944  default:
945  TokError("unknown bang operator");
946  return nullptr;
947  case tgtok::XNOT:
948  case tgtok::XLOG2:
949  case tgtok::XHead:
950  case tgtok::XTail:
951  case tgtok::XSize:
952  case tgtok::XEmpty:
953  case tgtok::XCast:
954  case tgtok::XGetDagOp: { // Value ::= !unop '(' Value ')'
956  RecTy *Type = nullptr;
957 
958  switch (Lex.getCode()) {
959  default: llvm_unreachable("Unhandled code!");
960  case tgtok::XCast:
961  Lex.Lex(); // eat the operation
963 
964  Type = ParseOperatorType();
965 
966  if (!Type) {
967  TokError("did not get type for unary operator");
968  return nullptr;
969  }
970 
971  break;
972  case tgtok::XNOT:
973  Lex.Lex(); // eat the operation
975  Type = IntRecTy::get(Records);
976  break;
977  case tgtok::XLOG2:
978  Lex.Lex(); // eat the operation
980  Type = IntRecTy::get(Records);
981  break;
982  case tgtok::XHead:
983  Lex.Lex(); // eat the operation
985  break;
986  case tgtok::XTail:
987  Lex.Lex(); // eat the operation
989  break;
990  case tgtok::XSize:
991  Lex.Lex();
993  Type = IntRecTy::get(Records);
994  break;
995  case tgtok::XEmpty:
996  Lex.Lex(); // eat the operation
998  Type = IntRecTy::get(Records);
999  break;
1000  case tgtok::XGetDagOp:
1001  Lex.Lex(); // eat the operation
1002  if (Lex.getCode() == tgtok::less) {
1003  // Parse an optional type suffix, so that you can say
1004  // !getdagop<BaseClass>(someDag) as a shorthand for
1005  // !cast<BaseClass>(!getdagop(someDag)).
1006  Type = ParseOperatorType();
1007 
1008  if (!Type) {
1009  TokError("did not get type for unary operator");
1010  return nullptr;
1011  }
1012 
1013  if (!isa<RecordRecTy>(Type)) {
1014  TokError("type for !getdagop must be a record type");
1015  // but keep parsing, to consume the operand
1016  }
1017  } else {
1018  Type = RecordRecTy::get(Records, {});
1019  }
1021  break;
1022  }
1023  if (!consume(tgtok::l_paren)) {
1024  TokError("expected '(' after unary operator");
1025  return nullptr;
1026  }
1027 
1028  Init *LHS = ParseValue(CurRec);
1029  if (!LHS) return nullptr;
1030 
1031  if (Code == UnOpInit::EMPTY || Code == UnOpInit::SIZE) {
1032  ListInit *LHSl = dyn_cast<ListInit>(LHS);
1033  StringInit *LHSs = dyn_cast<StringInit>(LHS);
1034  DagInit *LHSd = dyn_cast<DagInit>(LHS);
1035  TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
1036  if (!LHSl && !LHSs && !LHSd && !LHSt) {
1037  TokError("expected string, list, or dag type argument in unary operator");
1038  return nullptr;
1039  }
1040  if (LHSt) {
1041  ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
1042  StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
1043  DagRecTy *DType = dyn_cast<DagRecTy>(LHSt->getType());
1044  if (!LType && !SType && !DType) {
1045  TokError("expected string, list, or dag type argument in unary operator");
1046  return nullptr;
1047  }
1048  }
1049  }
1050 
1051  if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
1052  ListInit *LHSl = dyn_cast<ListInit>(LHS);
1053  TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
1054  if (!LHSl && !LHSt) {
1055  TokError("expected list type argument in unary operator");
1056  return nullptr;
1057  }
1058  if (LHSt) {
1059  ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
1060  if (!LType) {
1061  TokError("expected list type argument in unary operator");
1062  return nullptr;
1063  }
1064  }
1065 
1066  if (LHSl && LHSl->empty()) {
1067  TokError("empty list argument in unary operator");
1068  return nullptr;
1069  }
1070  if (LHSl) {
1071  Init *Item = LHSl->getElement(0);
1072  TypedInit *Itemt = dyn_cast<TypedInit>(Item);
1073  if (!Itemt) {
1074  TokError("untyped list element in unary operator");
1075  return nullptr;
1076  }
1077  Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
1078  : ListRecTy::get(Itemt->getType());
1079  } else {
1080  assert(LHSt && "expected list type argument in unary operator");
1081  ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
1082  Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
1083  }
1084  }
1085 
1086  if (!consume(tgtok::r_paren)) {
1087  TokError("expected ')' in unary operator");
1088  return nullptr;
1089  }
1090  return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec);
1091  }
1092 
1093  case tgtok::XIsA: {
1094  // Value ::= !isa '<' Type '>' '(' Value ')'
1095  Lex.Lex(); // eat the operation
1096 
1097  RecTy *Type = ParseOperatorType();
1098  if (!Type)
1099  return nullptr;
1100 
1101  if (!consume(tgtok::l_paren)) {
1102  TokError("expected '(' after type of !isa");
1103  return nullptr;
1104  }
1105 
1106  Init *LHS = ParseValue(CurRec);
1107  if (!LHS)
1108  return nullptr;
1109 
1110  if (!consume(tgtok::r_paren)) {
1111  TokError("expected ')' in !isa");
1112  return nullptr;
1113  }
1114 
1115  return (IsAOpInit::get(Type, LHS))->Fold();
1116  }
1117 
1118  case tgtok::XExists: {
1119  // Value ::= !exists '<' Type '>' '(' Value ')'
1120  Lex.Lex(); // eat the operation
1121 
1122  RecTy *Type = ParseOperatorType();
1123  if (!Type)
1124  return nullptr;
1125 
1126  if (!consume(tgtok::l_paren)) {
1127  TokError("expected '(' after type of !exists");
1128  return nullptr;
1129  }
1130 
1131  SMLoc ExprLoc = Lex.getLoc();
1132  Init *Expr = ParseValue(CurRec);
1133  if (!Expr)
1134  return nullptr;
1135 
1136  TypedInit *ExprType = dyn_cast<TypedInit>(Expr);
1137  if (!ExprType) {
1138  Error(ExprLoc, "expected string type argument in !exists operator");
1139  return nullptr;
1140  }
1141 
1142  RecordRecTy *RecType = dyn_cast<RecordRecTy>(ExprType->getType());
1143  if (RecType) {
1144  Error(ExprLoc,
1145  "expected string type argument in !exists operator, please "
1146  "use !isa instead");
1147  return nullptr;
1148  }
1149 
1150  StringRecTy *SType = dyn_cast<StringRecTy>(ExprType->getType());
1151  if (!SType) {
1152  Error(ExprLoc, "expected string type argument in !exists operator");
1153  return nullptr;
1154  }
1155 
1156  if (!consume(tgtok::r_paren)) {
1157  TokError("expected ')' in !exists");
1158  return nullptr;
1159  }
1160 
1161  return (ExistsOpInit::get(Type, Expr))->Fold(CurRec);
1162  }
1163 
1164  case tgtok::XConcat:
1165  case tgtok::XADD:
1166  case tgtok::XSUB:
1167  case tgtok::XMUL:
1168  case tgtok::XDIV:
1169  case tgtok::XAND:
1170  case tgtok::XOR:
1171  case tgtok::XXOR:
1172  case tgtok::XSRA:
1173  case tgtok::XSRL:
1174  case tgtok::XSHL:
1175  case tgtok::XEq:
1176  case tgtok::XNe:
1177  case tgtok::XLe:
1178  case tgtok::XLt:
1179  case tgtok::XGe:
1180  case tgtok::XGt:
1181  case tgtok::XListConcat:
1182  case tgtok::XListSplat:
1183  case tgtok::XStrConcat:
1184  case tgtok::XInterleave:
1185  case tgtok::XSetDagOp: { // Value ::= !binop '(' Value ',' Value ')'
1186  tgtok::TokKind OpTok = Lex.getCode();
1187  SMLoc OpLoc = Lex.getLoc();
1188  Lex.Lex(); // eat the operation
1189 
1191  switch (OpTok) {
1192  default: llvm_unreachable("Unhandled code!");
1193  case tgtok::XConcat: Code = BinOpInit::CONCAT; break;
1194  case tgtok::XADD: Code = BinOpInit::ADD; break;
1195  case tgtok::XSUB: Code = BinOpInit::SUB; break;
1196  case tgtok::XMUL: Code = BinOpInit::MUL; break;
1197  case tgtok::XDIV: Code = BinOpInit::DIV; break;
1198  case tgtok::XAND: Code = BinOpInit::AND; break;
1199  case tgtok::XOR: Code = BinOpInit::OR; break;
1200  case tgtok::XXOR: Code = BinOpInit::XOR; break;
1201  case tgtok::XSRA: Code = BinOpInit::SRA; break;
1202  case tgtok::XSRL: Code = BinOpInit::SRL; break;
1203  case tgtok::XSHL: Code = BinOpInit::SHL; break;
1204  case tgtok::XEq: Code = BinOpInit::EQ; break;
1205  case tgtok::XNe: Code = BinOpInit::NE; break;
1206  case tgtok::XLe: Code = BinOpInit::LE; break;
1207  case tgtok::XLt: Code = BinOpInit::LT; break;
1208  case tgtok::XGe: Code = BinOpInit::GE; break;
1209  case tgtok::XGt: Code = BinOpInit::GT; break;
1214  case tgtok::XSetDagOp: Code = BinOpInit::SETDAGOP; break;
1215  }
1216 
1217  RecTy *Type = nullptr;
1218  RecTy *ArgType = nullptr;
1219  switch (OpTok) {
1220  default:
1221  llvm_unreachable("Unhandled code!");
1222  case tgtok::XConcat:
1223  case tgtok::XSetDagOp:
1224  Type = DagRecTy::get(Records);
1225  ArgType = DagRecTy::get(Records);
1226  break;
1227  case tgtok::XAND:
1228  case tgtok::XOR:
1229  case tgtok::XXOR:
1230  case tgtok::XSRA:
1231  case tgtok::XSRL:
1232  case tgtok::XSHL:
1233  case tgtok::XADD:
1234  case tgtok::XSUB:
1235  case tgtok::XMUL:
1236  case tgtok::XDIV:
1237  Type = IntRecTy::get(Records);
1238  ArgType = IntRecTy::get(Records);
1239  break;
1240  case tgtok::XEq:
1241  case tgtok::XNe:
1242  case tgtok::XLe:
1243  case tgtok::XLt:
1244  case tgtok::XGe:
1245  case tgtok::XGt:
1246  Type = BitRecTy::get(Records);
1247  // ArgType for the comparison operators is not yet known.
1248  break;
1249  case tgtok::XListConcat:
1250  // We don't know the list type until we parse the first argument
1251  ArgType = ItemType;
1252  break;
1253  case tgtok::XListSplat:
1254  // Can't do any typechecking until we parse the first argument.
1255  break;
1256  case tgtok::XStrConcat:
1257  Type = StringRecTy::get(Records);
1258  ArgType = StringRecTy::get(Records);
1259  break;
1260  case tgtok::XInterleave:
1261  Type = StringRecTy::get(Records);
1262  // The first argument type is not yet known.
1263  }
1264 
1265  if (Type && ItemType && !Type->typeIsConvertibleTo(ItemType)) {
1266  Error(OpLoc, Twine("expected value of type '") +
1267  ItemType->getAsString() + "', got '" +
1268  Type->getAsString() + "'");
1269  return nullptr;
1270  }
1271 
1272  if (!consume(tgtok::l_paren)) {
1273  TokError("expected '(' after binary operator");
1274  return nullptr;
1275  }
1276 
1277  SmallVector<Init*, 2> InitList;
1278 
1279  // Note that this loop consumes an arbitrary number of arguments.
1280  // The actual count is checked later.
1281  for (;;) {
1282  SMLoc InitLoc = Lex.getLoc();
1283  InitList.push_back(ParseValue(CurRec, ArgType));
1284  if (!InitList.back()) return nullptr;
1285 
1286  TypedInit *InitListBack = dyn_cast<TypedInit>(InitList.back());
1287  if (!InitListBack) {
1288  Error(OpLoc, Twine("expected value to be a typed value, got '" +
1289  InitList.back()->getAsString() + "'"));
1290  return nullptr;
1291  }
1292  RecTy *ListType = InitListBack->getType();
1293 
1294  if (!ArgType) {
1295  // Argument type must be determined from the argument itself.
1296  ArgType = ListType;
1297 
1298  switch (Code) {
1299  case BinOpInit::LISTCONCAT:
1300  if (!isa<ListRecTy>(ArgType)) {
1301  Error(InitLoc, Twine("expected a list, got value of type '") +
1302  ArgType->getAsString() + "'");
1303  return nullptr;
1304  }
1305  break;
1306  case BinOpInit::LISTSPLAT:
1307  if (ItemType && InitList.size() == 1) {
1308  if (!isa<ListRecTy>(ItemType)) {
1309  Error(OpLoc,
1310  Twine("expected output type to be a list, got type '") +
1311  ItemType->getAsString() + "'");
1312  return nullptr;
1313  }
1314  if (!ArgType->getListTy()->typeIsConvertibleTo(ItemType)) {
1315  Error(OpLoc, Twine("expected first arg type to be '") +
1316  ArgType->getAsString() +
1317  "', got value of type '" +
1318  cast<ListRecTy>(ItemType)
1319  ->getElementType()
1320  ->getAsString() +
1321  "'");
1322  return nullptr;
1323  }
1324  }
1325  if (InitList.size() == 2 && !isa<IntRecTy>(ArgType)) {
1326  Error(InitLoc, Twine("expected second parameter to be an int, got "
1327  "value of type '") +
1328  ArgType->getAsString() + "'");
1329  return nullptr;
1330  }
1331  ArgType = nullptr; // Broken invariant: types not identical.
1332  break;
1333  case BinOpInit::EQ:
1334  case BinOpInit::NE:
1335  if (!ArgType->typeIsConvertibleTo(IntRecTy::get(Records)) &&
1336  !ArgType->typeIsConvertibleTo(StringRecTy::get(Records)) &&
1337  !ArgType->typeIsConvertibleTo(RecordRecTy::get(Records, {}))) {
1338  Error(InitLoc, Twine("expected bit, bits, int, string, or record; "
1339  "got value of type '") + ArgType->getAsString() +
1340  "'");
1341  return nullptr;
1342  }
1343  break;
1344  case BinOpInit::LE:
1345  case BinOpInit::LT:
1346  case BinOpInit::GE:
1347  case BinOpInit::GT:
1348  if (!ArgType->typeIsConvertibleTo(IntRecTy::get(Records)) &&
1349  !ArgType->typeIsConvertibleTo(StringRecTy::get(Records))) {
1350  Error(InitLoc, Twine("expected bit, bits, int, or string; "
1351  "got value of type '") + ArgType->getAsString() +
1352  "'");
1353  return nullptr;
1354  }
1355  break;
1356  case BinOpInit::INTERLEAVE:
1357  switch (InitList.size()) {
1358  case 1: // First argument must be a list of strings or integers.
1359  if (ArgType != StringRecTy::get(Records)->getListTy() &&
1360  !ArgType->typeIsConvertibleTo(
1361  IntRecTy::get(Records)->getListTy())) {
1362  Error(InitLoc, Twine("expected list of string, int, bits, or bit; "
1363  "got value of type '") +
1364  ArgType->getAsString() + "'");
1365  return nullptr;
1366  }
1367  break;
1368  case 2: // Second argument must be a string.
1369  if (!isa<StringRecTy>(ArgType)) {
1370  Error(InitLoc, Twine("expected second argument to be a string, "
1371  "got value of type '") +
1372  ArgType->getAsString() + "'");
1373  return nullptr;
1374  }
1375  break;
1376  default: ;
1377  }
1378  ArgType = nullptr; // Broken invariant: types not identical.
1379  break;
1380  default: llvm_unreachable("other ops have fixed argument types");
1381  }
1382 
1383  } else {
1384  // Desired argument type is a known and in ArgType.
1385  RecTy *Resolved = resolveTypes(ArgType, ListType);
1386  if (!Resolved) {
1387  Error(InitLoc, Twine("expected value of type '") +
1388  ArgType->getAsString() + "', got '" +
1389  ListType->getAsString() + "'");
1390  return nullptr;
1391  }
1392  if (Code != BinOpInit::ADD && Code != BinOpInit::SUB &&
1393  Code != BinOpInit::AND && Code != BinOpInit::OR &&
1394  Code != BinOpInit::XOR && Code != BinOpInit::SRA &&
1395  Code != BinOpInit::SRL && Code != BinOpInit::SHL &&
1396  Code != BinOpInit::MUL && Code != BinOpInit::DIV)
1397  ArgType = Resolved;
1398  }
1399 
1400  // Deal with BinOps whose arguments have different types, by
1401  // rewriting ArgType in between them.
1402  switch (Code) {
1403  case BinOpInit::SETDAGOP:
1404  // After parsing the first dag argument, switch to expecting
1405  // a record, with no restriction on its superclasses.
1406  ArgType = RecordRecTy::get(Records, {});
1407  break;
1408  default:
1409  break;
1410  }
1411 
1412  if (!consume(tgtok::comma))
1413  break;
1414  }
1415 
1416  if (!consume(tgtok::r_paren)) {
1417  TokError("expected ')' in operator");
1418  return nullptr;
1419  }
1420 
1421  // listconcat returns a list with type of the argument.
1422  if (Code == BinOpInit::LISTCONCAT)
1423  Type = ArgType;
1424  // listsplat returns a list of type of the *first* argument.
1425  if (Code == BinOpInit::LISTSPLAT)
1426  Type = cast<TypedInit>(InitList.front())->getType()->getListTy();
1427 
1428  // We allow multiple operands to associative operators like !strconcat as
1429  // shorthand for nesting them.
1430  if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT ||
1431  Code == BinOpInit::CONCAT || Code == BinOpInit::ADD ||
1432  Code == BinOpInit::AND || Code == BinOpInit::OR ||
1433  Code == BinOpInit::XOR || Code == BinOpInit::MUL) {
1434  while (InitList.size() > 2) {
1435  Init *RHS = InitList.pop_back_val();
1436  RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))->Fold(CurRec);
1437  InitList.back() = RHS;
1438  }
1439  }
1440 
1441  if (InitList.size() == 2)
1442  return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
1443  ->Fold(CurRec);
1444 
1445  Error(OpLoc, "expected two operands to operator");
1446  return nullptr;
1447  }
1448 
1449  case tgtok::XForEach:
1450  case tgtok::XFilter: {
1451  return ParseOperationForEachFilter(CurRec, ItemType);
1452  }
1453 
1454  case tgtok::XDag:
1455  case tgtok::XIf:
1456  case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
1458  RecTy *Type = nullptr;
1459 
1460  tgtok::TokKind LexCode = Lex.getCode();
1461  Lex.Lex(); // eat the operation
1462  switch (LexCode) {
1463  default: llvm_unreachable("Unhandled code!");
1464  case tgtok::XDag:
1466  Type = DagRecTy::get(Records);
1467  ItemType = nullptr;
1468  break;
1469  case tgtok::XIf:
1470  Code = TernOpInit::IF;
1471  break;
1472  case tgtok::XSubst:
1474  break;
1475  }
1476  if (!consume(tgtok::l_paren)) {
1477  TokError("expected '(' after ternary operator");
1478  return nullptr;
1479  }
1480 
1481  Init *LHS = ParseValue(CurRec);
1482  if (!LHS) return nullptr;
1483 
1484  if (!consume(tgtok::comma)) {
1485  TokError("expected ',' in ternary operator");
1486  return nullptr;
1487  }
1488 
1489  SMLoc MHSLoc = Lex.getLoc();
1490  Init *MHS = ParseValue(CurRec, ItemType);
1491  if (!MHS)
1492  return nullptr;
1493 
1494  if (!consume(tgtok::comma)) {
1495  TokError("expected ',' in ternary operator");
1496  return nullptr;
1497  }
1498 
1499  SMLoc RHSLoc = Lex.getLoc();
1500  Init *RHS = ParseValue(CurRec, ItemType);
1501  if (!RHS)
1502  return nullptr;
1503 
1504  if (!consume(tgtok::r_paren)) {
1505  TokError("expected ')' in binary operator");
1506  return nullptr;
1507  }
1508 
1509  switch (LexCode) {
1510  default: llvm_unreachable("Unhandled code!");
1511  case tgtok::XDag: {
1512  TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1513  if (!MHSt && !isa<UnsetInit>(MHS)) {
1514  Error(MHSLoc, "could not determine type of the child list in !dag");
1515  return nullptr;
1516  }
1517  if (MHSt && !isa<ListRecTy>(MHSt->getType())) {
1518  Error(MHSLoc, Twine("expected list of children, got type '") +
1519  MHSt->getType()->getAsString() + "'");
1520  return nullptr;
1521  }
1522 
1523  TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1524  if (!RHSt && !isa<UnsetInit>(RHS)) {
1525  Error(RHSLoc, "could not determine type of the name list in !dag");
1526  return nullptr;
1527  }
1528  if (RHSt && StringRecTy::get(Records)->getListTy() != RHSt->getType()) {
1529  Error(RHSLoc, Twine("expected list<string>, got type '") +
1530  RHSt->getType()->getAsString() + "'");
1531  return nullptr;
1532  }
1533 
1534  if (!MHSt && !RHSt) {
1535  Error(MHSLoc,
1536  "cannot have both unset children and unset names in !dag");
1537  return nullptr;
1538  }
1539  break;
1540  }
1541  case tgtok::XIf: {
1542  RecTy *MHSTy = nullptr;
1543  RecTy *RHSTy = nullptr;
1544 
1545  if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
1546  MHSTy = MHSt->getType();
1547  if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
1548  MHSTy = BitsRecTy::get(Records, MHSbits->getNumBits());
1549  if (isa<BitInit>(MHS))
1550  MHSTy = BitRecTy::get(Records);
1551 
1552  if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
1553  RHSTy = RHSt->getType();
1554  if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
1555  RHSTy = BitsRecTy::get(Records, RHSbits->getNumBits());
1556  if (isa<BitInit>(RHS))
1557  RHSTy = BitRecTy::get(Records);
1558 
1559  // For UnsetInit, it's typed from the other hand.
1560  if (isa<UnsetInit>(MHS))
1561  MHSTy = RHSTy;
1562  if (isa<UnsetInit>(RHS))
1563  RHSTy = MHSTy;
1564 
1565  if (!MHSTy || !RHSTy) {
1566  TokError("could not get type for !if");
1567  return nullptr;
1568  }
1569 
1570  Type = resolveTypes(MHSTy, RHSTy);
1571  if (!Type) {
1572  TokError(Twine("inconsistent types '") + MHSTy->getAsString() +
1573  "' and '" + RHSTy->getAsString() + "' for !if");
1574  return nullptr;
1575  }
1576  break;
1577  }
1578  case tgtok::XSubst: {
1579  TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1580  if (!RHSt) {
1581  TokError("could not get type for !subst");
1582  return nullptr;
1583  }
1584  Type = RHSt->getType();
1585  break;
1586  }
1587  }
1588  return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec);
1589  }
1590 
1591  case tgtok::XSubstr:
1592  return ParseOperationSubstr(CurRec, ItemType);
1593 
1594  case tgtok::XFind:
1595  return ParseOperationFind(CurRec, ItemType);
1596 
1597  case tgtok::XCond:
1598  return ParseOperationCond(CurRec, ItemType);
1599 
1600  case tgtok::XFoldl: {
1601  // Value ::= !foldl '(' Value ',' Value ',' Id ',' Id ',' Expr ')'
1602  Lex.Lex(); // eat the operation
1603  if (!consume(tgtok::l_paren)) {
1604  TokError("expected '(' after !foldl");
1605  return nullptr;
1606  }
1607 
1608  Init *StartUntyped = ParseValue(CurRec);
1609  if (!StartUntyped)
1610  return nullptr;
1611 
1612  TypedInit *Start = dyn_cast<TypedInit>(StartUntyped);
1613  if (!Start) {
1614  TokError(Twine("could not get type of !foldl start: '") +
1615  StartUntyped->getAsString() + "'");
1616  return nullptr;
1617  }
1618 
1619  if (!consume(tgtok::comma)) {
1620  TokError("expected ',' in !foldl");
1621  return nullptr;
1622  }
1623 
1624  Init *ListUntyped = ParseValue(CurRec);
1625  if (!ListUntyped)
1626  return nullptr;
1627 
1628  TypedInit *List = dyn_cast<TypedInit>(ListUntyped);
1629  if (!List) {
1630  TokError(Twine("could not get type of !foldl list: '") +
1631  ListUntyped->getAsString() + "'");
1632  return nullptr;
1633  }
1634 
1635  ListRecTy *ListType = dyn_cast<ListRecTy>(List->getType());
1636  if (!ListType) {
1637  TokError(Twine("!foldl list must be a list, but is of type '") +
1638  List->getType()->getAsString());
1639  return nullptr;
1640  }
1641 
1642  if (Lex.getCode() != tgtok::comma) {
1643  TokError("expected ',' in !foldl");
1644  return nullptr;
1645  }
1646 
1647  if (Lex.Lex() != tgtok::Id) { // eat the ','
1648  TokError("third argument of !foldl must be an identifier");
1649  return nullptr;
1650  }
1651 
1652  Init *A = StringInit::get(Records, Lex.getCurStrVal());
1653  if (CurRec && CurRec->getValue(A)) {
1654  TokError((Twine("left !foldl variable '") + A->getAsString() +
1655  "' already defined")
1656  .str());
1657  return nullptr;
1658  }
1659 
1660  if (Lex.Lex() != tgtok::comma) { // eat the id
1661  TokError("expected ',' in !foldl");
1662  return nullptr;
1663  }
1664 
1665  if (Lex.Lex() != tgtok::Id) { // eat the ','
1666  TokError("fourth argument of !foldl must be an identifier");
1667  return nullptr;
1668  }
1669 
1670  Init *B = StringInit::get(Records, Lex.getCurStrVal());
1671  if (CurRec && CurRec->getValue(B)) {
1672  TokError((Twine("right !foldl variable '") + B->getAsString() +
1673  "' already defined")
1674  .str());
1675  return nullptr;
1676  }
1677 
1678  if (Lex.Lex() != tgtok::comma) { // eat the id
1679  TokError("expected ',' in !foldl");
1680  return nullptr;
1681  }
1682  Lex.Lex(); // eat the ','
1683 
1684  // We need to create a temporary record to provide a scope for the
1685  // two variables.
1686  std::unique_ptr<Record> ParseRecTmp;
1687  Record *ParseRec = CurRec;
1688  if (!ParseRec) {
1689  ParseRecTmp = std::make_unique<Record>(".parse", ArrayRef<SMLoc>{}, Records);
1690  ParseRec = ParseRecTmp.get();
1691  }
1692 
1693  ParseRec->addValue(RecordVal(A, Start->getType(), RecordVal::FK_Normal));
1694  ParseRec->addValue(RecordVal(B, ListType->getElementType(),
1696  Init *ExprUntyped = ParseValue(ParseRec);
1697  ParseRec->removeValue(A);
1698  ParseRec->removeValue(B);
1699  if (!ExprUntyped)
1700  return nullptr;
1701 
1702  TypedInit *Expr = dyn_cast<TypedInit>(ExprUntyped);
1703  if (!Expr) {
1704  TokError("could not get type of !foldl expression");
1705  return nullptr;
1706  }
1707 
1708  if (Expr->getType() != Start->getType()) {
1709  TokError(Twine("!foldl expression must be of same type as start (") +
1710  Start->getType()->getAsString() + "), but is of type " +
1711  Expr->getType()->getAsString());
1712  return nullptr;
1713  }
1714 
1715  if (!consume(tgtok::r_paren)) {
1716  TokError("expected ')' in fold operator");
1717  return nullptr;
1718  }
1719 
1720  return FoldOpInit::get(Start, List, A, B, Expr, Start->getType())
1721  ->Fold(CurRec);
1722  }
1723  }
1724 }
1725 
1726 /// ParseOperatorType - Parse a type for an operator. This returns
1727 /// null on error.
1728 ///
1729 /// OperatorType ::= '<' Type '>'
1730 ///
1731 RecTy *TGParser::ParseOperatorType() {
1732  RecTy *Type = nullptr;
1733 
1734  if (!consume(tgtok::less)) {
1735  TokError("expected type name for operator");
1736  return nullptr;
1737  }
1738 
1739  if (Lex.getCode() == tgtok::Code)
1740  TokError("the 'code' type is not allowed in bang operators; use 'string'");
1741 
1742  Type = ParseType();
1743 
1744  if (!Type) {
1745  TokError("expected type name for operator");
1746  return nullptr;
1747  }
1748 
1749  if (!consume(tgtok::greater)) {
1750  TokError("expected type name for operator");
1751  return nullptr;
1752  }
1753 
1754  return Type;
1755 }
1756 
1757 /// Parse the !substr operation. Return null on error.
1758 ///
1759 /// Substr ::= !substr(string, start-int [, length-int]) => string
1760 Init *TGParser::ParseOperationSubstr(Record *CurRec, RecTy *ItemType) {
1762  RecTy *Type = StringRecTy::get(Records);
1763 
1764  Lex.Lex(); // eat the operation
1765 
1766  if (!consume(tgtok::l_paren)) {
1767  TokError("expected '(' after !substr operator");
1768  return nullptr;
1769  }
1770 
1771  Init *LHS = ParseValue(CurRec);
1772  if (!LHS)
1773  return nullptr;
1774 
1775  if (!consume(tgtok::comma)) {
1776  TokError("expected ',' in !substr operator");
1777  return nullptr;
1778  }
1779 
1780  SMLoc MHSLoc = Lex.getLoc();
1781  Init *MHS = ParseValue(CurRec);
1782  if (!MHS)
1783  return nullptr;
1784 
1785  SMLoc RHSLoc = Lex.getLoc();
1786  Init *RHS;
1787  if (consume(tgtok::comma)) {
1788  RHSLoc = Lex.getLoc();
1789  RHS = ParseValue(CurRec);
1790  if (!RHS)
1791  return nullptr;
1792  } else {
1794  }
1795 
1796  if (!consume(tgtok::r_paren)) {
1797  TokError("expected ')' in !substr operator");
1798  return nullptr;
1799  }
1800 
1801  if (ItemType && !Type->typeIsConvertibleTo(ItemType)) {
1802  Error(RHSLoc, Twine("expected value of type '") +
1803  ItemType->getAsString() + "', got '" +
1804  Type->getAsString() + "'");
1805  }
1806 
1807  TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
1808  if (!LHSt && !isa<UnsetInit>(LHS)) {
1809  TokError("could not determine type of the string in !substr");
1810  return nullptr;
1811  }
1812  if (LHSt && !isa<StringRecTy>(LHSt->getType())) {
1813  TokError(Twine("expected string, got type '") +
1814  LHSt->getType()->getAsString() + "'");
1815  return nullptr;
1816  }
1817 
1818  TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1819  if (!MHSt && !isa<UnsetInit>(MHS)) {
1820  TokError("could not determine type of the start position in !substr");
1821  return nullptr;
1822  }
1823  if (MHSt && !isa<IntRecTy>(MHSt->getType())) {
1824  Error(MHSLoc, Twine("expected int, got type '") +
1825  MHSt->getType()->getAsString() + "'");
1826  return nullptr;
1827  }
1828 
1829  if (RHS) {
1830  TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1831  if (!RHSt && !isa<UnsetInit>(RHS)) {
1832  TokError("could not determine type of the length in !substr");
1833  return nullptr;
1834  }
1835  if (RHSt && !isa<IntRecTy>(RHSt->getType())) {
1836  TokError(Twine("expected int, got type '") +
1837  RHSt->getType()->getAsString() + "'");
1838  return nullptr;
1839  }
1840  }
1841 
1842  return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec);
1843 }
1844 
1845 /// Parse the !find operation. Return null on error.
1846 ///
1847 /// Substr ::= !find(string, string [, start-int]) => int
1848 Init *TGParser::ParseOperationFind(Record *CurRec, RecTy *ItemType) {
1850  RecTy *Type = IntRecTy::get(Records);
1851 
1852  Lex.Lex(); // eat the operation
1853 
1854  if (!consume(tgtok::l_paren)) {
1855  TokError("expected '(' after !find operator");
1856  return nullptr;
1857  }
1858 
1859  Init *LHS = ParseValue(CurRec);
1860  if (!LHS)
1861  return nullptr;
1862 
1863  if (!consume(tgtok::comma)) {
1864  TokError("expected ',' in !find operator");
1865  return nullptr;
1866  }
1867 
1868  SMLoc MHSLoc = Lex.getLoc();
1869  Init *MHS = ParseValue(CurRec);
1870  if (!MHS)
1871  return nullptr;
1872 
1873  SMLoc RHSLoc = Lex.getLoc();
1874  Init *RHS;
1875  if (consume(tgtok::comma)) {
1876  RHSLoc = Lex.getLoc();
1877  RHS = ParseValue(CurRec);
1878  if (!RHS)
1879  return nullptr;
1880  } else {
1881  RHS = IntInit::get(Records, 0);
1882  }
1883 
1884  if (!consume(tgtok::r_paren)) {
1885  TokError("expected ')' in !find operator");
1886  return nullptr;
1887  }
1888 
1889  if (ItemType && !Type->typeIsConvertibleTo(ItemType)) {
1890  Error(RHSLoc, Twine("expected value of type '") +
1891  ItemType->getAsString() + "', got '" +
1892  Type->getAsString() + "'");
1893  }
1894 
1895  TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
1896  if (!LHSt && !isa<UnsetInit>(LHS)) {
1897  TokError("could not determine type of the source string in !find");
1898  return nullptr;
1899  }
1900  if (LHSt && !isa<StringRecTy>(LHSt->getType())) {
1901  TokError(Twine("expected string, got type '") +
1902  LHSt->getType()->getAsString() + "'");
1903  return nullptr;
1904  }
1905 
1906  TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1907  if (!MHSt && !isa<UnsetInit>(MHS)) {
1908  TokError("could not determine type of the target string in !find");
1909  return nullptr;
1910  }
1911  if (MHSt && !isa<StringRecTy>(MHSt->getType())) {
1912  Error(MHSLoc, Twine("expected string, got type '") +
1913  MHSt->getType()->getAsString() + "'");
1914  return nullptr;
1915  }
1916 
1917  if (RHS) {
1918  TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1919  if (!RHSt && !isa<UnsetInit>(RHS)) {
1920  TokError("could not determine type of the start position in !find");
1921  return nullptr;
1922  }
1923  if (RHSt && !isa<IntRecTy>(RHSt->getType())) {
1924  TokError(Twine("expected int, got type '") +
1925  RHSt->getType()->getAsString() + "'");
1926  return nullptr;
1927  }
1928  }
1929 
1930  return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec);
1931 }
1932 
1933 /// Parse the !foreach and !filter operations. Return null on error.
1934 ///
1935 /// ForEach ::= !foreach(ID, list-or-dag, expr) => list<expr type>
1936 /// Filter ::= !foreach(ID, list, predicate) ==> list<list type>
1937 Init *TGParser::ParseOperationForEachFilter(Record *CurRec, RecTy *ItemType) {
1938  SMLoc OpLoc = Lex.getLoc();
1940  Lex.Lex(); // eat the operation
1941  if (Lex.getCode() != tgtok::l_paren) {
1942  TokError("expected '(' after !foreach/!filter");
1943  return nullptr;
1944  }
1945 
1946  if (Lex.Lex() != tgtok::Id) { // eat the '('
1947  TokError("first argument of !foreach/!filter must be an identifier");
1948  return nullptr;
1949  }
1950 
1951  Init *LHS = StringInit::get(Records, Lex.getCurStrVal());
1952  Lex.Lex(); // eat the ID.
1953 
1954  if (CurRec && CurRec->getValue(LHS)) {
1955  TokError((Twine("iteration variable '") + LHS->getAsString() +
1956  "' is already defined")
1957  .str());
1958  return nullptr;
1959  }
1960 
1961  if (!consume(tgtok::comma)) {
1962  TokError("expected ',' in !foreach/!filter");
1963  return nullptr;
1964  }
1965 
1966  Init *MHS = ParseValue(CurRec);
1967  if (!MHS)
1968  return nullptr;
1969 
1970  if (!consume(tgtok::comma)) {
1971  TokError("expected ',' in !foreach/!filter");
1972  return nullptr;
1973  }
1974 
1975  TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1976  if (!MHSt) {
1977  TokError("could not get type of !foreach/!filter list or dag");
1978  return nullptr;
1979  }
1980 
1981  RecTy *InEltType = nullptr;
1982  RecTy *ExprEltType = nullptr;
1983  bool IsDAG = false;
1984 
1985  if (ListRecTy *InListTy = dyn_cast<ListRecTy>(MHSt->getType())) {
1986  InEltType = InListTy->getElementType();
1987  if (ItemType) {
1988  if (ListRecTy *OutListTy = dyn_cast<ListRecTy>(ItemType)) {
1989  ExprEltType = (Operation == tgtok::XForEach)
1990  ? OutListTy->getElementType()
1991  : IntRecTy::get(Records);
1992  } else {
1993  Error(OpLoc,
1994  "expected value of type '" +
1995  Twine(ItemType->getAsString()) +
1996  "', but got list type");
1997  return nullptr;
1998  }
1999  }
2000  } else if (DagRecTy *InDagTy = dyn_cast<DagRecTy>(MHSt->getType())) {
2001  if (Operation == tgtok::XFilter) {
2002  TokError("!filter must have a list argument");
2003  return nullptr;
2004  }
2005  InEltType = InDagTy;
2006  if (ItemType && !isa<DagRecTy>(ItemType)) {
2007  Error(OpLoc,
2008  "expected value of type '" + Twine(ItemType->getAsString()) +
2009  "', but got dag type");
2010  return nullptr;
2011  }
2012  IsDAG = true;
2013  } else {
2014  if (Operation == tgtok::XForEach)
2015  TokError("!foreach must have a list or dag argument");
2016  else
2017  TokError("!filter must have a list argument");
2018  return nullptr;
2019  }
2020 
2021  // We need to create a temporary record to provide a scope for the
2022  // iteration variable.
2023  std::unique_ptr<Record> ParseRecTmp;
2024  Record *ParseRec = CurRec;
2025  if (!ParseRec) {
2026  ParseRecTmp =
2027  std::make_unique<Record>(".parse", ArrayRef<SMLoc>{}, Records);
2028  ParseRec = ParseRecTmp.get();
2029  }
2030 
2031  ParseRec->addValue(RecordVal(LHS, InEltType, RecordVal::FK_Normal));
2032  Init *RHS = ParseValue(ParseRec, ExprEltType);
2033  ParseRec->removeValue(LHS);
2034  if (!RHS)
2035  return nullptr;
2036 
2037  if (!consume(tgtok::r_paren)) {
2038  TokError("expected ')' in !foreach/!filter");
2039  return nullptr;
2040  }
2041 
2042  RecTy *OutType = InEltType;
2043  if (Operation == tgtok::XForEach && !IsDAG) {
2044  TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
2045  if (!RHSt) {
2046  TokError("could not get type of !foreach result expression");
2047  return nullptr;
2048  }
2049  OutType = RHSt->getType()->getListTy();
2050  } else if (Operation == tgtok::XFilter) {
2051  OutType = InEltType->getListTy();
2052  }
2053 
2056  LHS, MHS, RHS, OutType))
2057  ->Fold(CurRec);
2058 }
2059 
2060 Init *TGParser::ParseOperationCond(Record *CurRec, RecTy *ItemType) {
2061  Lex.Lex(); // eat the operation 'cond'
2062 
2063  if (!consume(tgtok::l_paren)) {
2064  TokError("expected '(' after !cond operator");
2065  return nullptr;
2066  }
2067 
2068  // Parse through '[Case: Val,]+'
2071  while (true) {
2072  if (consume(tgtok::r_paren))
2073  break;
2074 
2075  Init *V = ParseValue(CurRec);
2076  if (!V)
2077  return nullptr;
2078  Case.push_back(V);
2079 
2080  if (!consume(tgtok::colon)) {
2081  TokError("expected ':' following a condition in !cond operator");
2082  return nullptr;
2083  }
2084 
2085  V = ParseValue(CurRec, ItemType);
2086  if (!V)
2087  return nullptr;
2088  Val.push_back(V);
2089 
2090  if (consume(tgtok::r_paren))
2091  break;
2092 
2093  if (!consume(tgtok::comma)) {
2094  TokError("expected ',' or ')' following a value in !cond operator");
2095  return nullptr;
2096  }
2097  }
2098 
2099  if (Case.size() < 1) {
2100  TokError("there should be at least 1 'condition : value' in the !cond operator");
2101  return nullptr;
2102  }
2103 
2104  // resolve type
2105  RecTy *Type = nullptr;
2106  for (Init *V : Val) {
2107  RecTy *VTy = nullptr;
2108  if (TypedInit *Vt = dyn_cast<TypedInit>(V))
2109  VTy = Vt->getType();
2110  if (BitsInit *Vbits = dyn_cast<BitsInit>(V))
2111  VTy = BitsRecTy::get(Records, Vbits->getNumBits());
2112  if (isa<BitInit>(V))
2113  VTy = BitRecTy::get(Records);
2114 
2115  if (Type == nullptr) {
2116  if (!isa<UnsetInit>(V))
2117  Type = VTy;
2118  } else {
2119  if (!isa<UnsetInit>(V)) {
2120  RecTy *RType = resolveTypes(Type, VTy);
2121  if (!RType) {
2122  TokError(Twine("inconsistent types '") + Type->getAsString() +
2123  "' and '" + VTy->getAsString() + "' for !cond");
2124  return nullptr;
2125  }
2126  Type = RType;
2127  }
2128  }
2129  }
2130 
2131  if (!Type) {
2132  TokError("could not determine type for !cond from its arguments");
2133  return nullptr;
2134  }
2135  return CondOpInit::get(Case, Val, Type)->Fold(CurRec);
2136 }
2137 
2138 /// ParseSimpleValue - Parse a tblgen value. This returns null on error.
2139 ///
2140 /// SimpleValue ::= IDValue
2141 /// SimpleValue ::= INTVAL
2142 /// SimpleValue ::= STRVAL+
2143 /// SimpleValue ::= CODEFRAGMENT
2144 /// SimpleValue ::= '?'
2145 /// SimpleValue ::= '{' ValueList '}'
2146 /// SimpleValue ::= ID '<' ValueListNE '>'
2147 /// SimpleValue ::= '[' ValueList ']'
2148 /// SimpleValue ::= '(' IDValue DagArgList ')'
2149 /// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
2150 /// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
2151 /// SimpleValue ::= DIVTOK '(' Value ',' Value ')'
2152 /// SimpleValue ::= SUBTOK '(' Value ',' Value ')'
2153 /// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
2154 /// SimpleValue ::= SRATOK '(' Value ',' Value ')'
2155 /// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
2156 /// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
2157 /// SimpleValue ::= LISTSPLATTOK '(' Value ',' Value ')'
2158 /// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
2159 /// SimpleValue ::= COND '(' [Value ':' Value,]+ ')'
2160 ///
2161 Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
2162  IDParseMode Mode) {
2163  Init *R = nullptr;
2164  switch (Lex.getCode()) {
2165  default: TokError("Unknown or reserved token when parsing a value"); break;
2166 
2167  case tgtok::TrueVal:
2168  R = IntInit::get(Records, 1);
2169  Lex.Lex();
2170  break;
2171  case tgtok::FalseVal:
2172  R = IntInit::get(Records, 0);
2173  Lex.Lex();
2174  break;
2175  case tgtok::IntVal:
2176  R = IntInit::get(Records, Lex.getCurIntVal());
2177  Lex.Lex();
2178  break;
2179  case tgtok::BinaryIntVal: {
2180  auto BinaryVal = Lex.getCurBinaryIntVal();
2181  SmallVector<Init*, 16> Bits(BinaryVal.second);
2182  for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
2183  Bits[i] = BitInit::get(Records, BinaryVal.first & (1LL << i));
2184  R = BitsInit::get(Records, Bits);
2185  Lex.Lex();
2186  break;
2187  }
2188  case tgtok::StrVal: {
2189  std::string Val = Lex.getCurStrVal();
2190  Lex.Lex();
2191 
2192  // Handle multiple consecutive concatenated strings.
2193  while (Lex.getCode() == tgtok::StrVal) {
2194  Val += Lex.getCurStrVal();
2195  Lex.Lex();
2196  }
2197 
2198  R = StringInit::get(Records, Val);
2199  break;
2200  }
2201  case tgtok::CodeFragment:
2203  Lex.Lex();
2204  break;
2205  case tgtok::question:
2206  R = UnsetInit::get(Records);
2207  Lex.Lex();
2208  break;
2209  case tgtok::Id: {
2210  SMRange NameLoc = Lex.getLocRange();
2211  StringInit *Name = StringInit::get(Records, Lex.getCurStrVal());
2212  if (Lex.Lex() != tgtok::less) // consume the Id.
2213  return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
2214 
2215  // Value ::= CLASSID '<' ValueListNE '>' (CLASSID has been consumed)
2216  // This is supposed to synthesize a new anonymous definition, deriving
2217  // from the class with the template arguments, but no body.
2218  Record *Class = Records.getClass(Name->getValue());
2219  if (!Class) {
2220  Error(NameLoc.Start,
2221  "Expected a class name, got '" + Name->getValue() + "'");
2222  return nullptr;
2223  }
2224 
2226  Lex.Lex(); // consume the <
2227  if (ParseTemplateArgValueList(Args, CurRec, Class))
2228  return nullptr; // Error parsing value list.
2229 
2230  if (CheckTemplateArgValues(Args, NameLoc.Start, Class))
2231  return nullptr; // Error checking template argument values.
2232 
2233  // Loop through the arguments that were not specified and make sure
2234  // they have a complete value.
2235  ArrayRef<Init *> TArgs = Class->getTemplateArgs();
2236  for (unsigned I = Args.size(), E = TArgs.size(); I < E; ++I) {
2237  RecordVal *Arg = Class->getValue(TArgs[I]);
2238  if (!Arg->getValue()->isComplete()) {
2239  Error(NameLoc.Start, "Value not specified for template argument '" +
2240  TArgs[I]->getAsUnquotedString() + "' (#" +
2241  Twine(I) + ") of parent class '" +
2242  Class->getNameInitAsString() + "'");
2243  }
2244  }
2245 
2246  if (TrackReferenceLocs)
2247  Class->appendReferenceLoc(NameLoc);
2248  return VarDefInit::get(Class, Args)->Fold();
2249  }
2250  case tgtok::l_brace: { // Value ::= '{' ValueList '}'
2251  SMLoc BraceLoc = Lex.getLoc();
2252  Lex.Lex(); // eat the '{'
2254 
2255  if (Lex.getCode() != tgtok::r_brace) {
2256  ParseValueList(Vals, CurRec);
2257  if (Vals.empty()) return nullptr;
2258  }
2259  if (!consume(tgtok::r_brace)) {
2260  TokError("expected '}' at end of bit list value");
2261  return nullptr;
2262  }
2263 
2264  SmallVector<Init *, 16> NewBits;
2265 
2266  // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
2267  // first. We'll first read everything in to a vector, then we can reverse
2268  // it to get the bits in the correct order for the BitsInit value.
2269  for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
2270  // FIXME: The following two loops would not be duplicated
2271  // if the API was a little more orthogonal.
2272 
2273  // bits<n> values are allowed to initialize n bits.
2274  if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
2275  for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
2276  NewBits.push_back(BI->getBit((e - i) - 1));
2277  continue;
2278  }
2279  // bits<n> can also come from variable initializers.
2280  if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
2281  if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
2282  for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
2283  NewBits.push_back(VI->getBit((e - i) - 1));
2284  continue;
2285  }
2286  // Fallthrough to try convert this to a bit.
2287  }
2288  // All other values must be convertible to just a single bit.
2289  Init *Bit = Vals[i]->getCastTo(BitRecTy::get(Records));
2290  if (!Bit) {
2291  Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
2292  ") is not convertable to a bit");
2293  return nullptr;
2294  }
2295  NewBits.push_back(Bit);
2296  }
2297  std::reverse(NewBits.begin(), NewBits.end());
2298  return BitsInit::get(Records, NewBits);
2299  }
2300  case tgtok::l_square: { // Value ::= '[' ValueList ']'
2301  Lex.Lex(); // eat the '['
2303 
2304  RecTy *DeducedEltTy = nullptr;
2305  ListRecTy *GivenListTy = nullptr;
2306 
2307  if (ItemType) {
2308  ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
2309  if (!ListType) {
2310  TokError(Twine("Encountered a list when expecting a ") +
2311  ItemType->getAsString());
2312  return nullptr;
2313  }
2314  GivenListTy = ListType;
2315  }
2316 
2317  if (Lex.getCode() != tgtok::r_square) {
2318  ParseValueList(Vals, CurRec,
2319  GivenListTy ? GivenListTy->getElementType() : nullptr);
2320  if (Vals.empty()) return nullptr;
2321  }
2322  if (!consume(tgtok::r_square)) {
2323  TokError("expected ']' at end of list value");
2324  return nullptr;
2325  }
2326 
2327  RecTy *GivenEltTy = nullptr;
2328  if (consume(tgtok::less)) {
2329  // Optional list element type
2330  GivenEltTy = ParseType();
2331  if (!GivenEltTy) {
2332  // Couldn't parse element type
2333  return nullptr;
2334  }
2335 
2336  if (!consume(tgtok::greater)) {
2337  TokError("expected '>' at end of list element type");
2338  return nullptr;
2339  }
2340  }
2341 
2342  // Check elements
2343  RecTy *EltTy = nullptr;
2344  for (Init *V : Vals) {
2345  TypedInit *TArg = dyn_cast<TypedInit>(V);
2346  if (TArg) {
2347  if (EltTy) {
2348  EltTy = resolveTypes(EltTy, TArg->getType());
2349  if (!EltTy) {
2350  TokError("Incompatible types in list elements");
2351  return nullptr;
2352  }
2353  } else {
2354  EltTy = TArg->getType();
2355  }
2356  }
2357  }
2358 
2359  if (GivenEltTy) {
2360  if (EltTy) {
2361  // Verify consistency
2362  if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
2363  TokError("Incompatible types in list elements");
2364  return nullptr;
2365  }
2366  }
2367  EltTy = GivenEltTy;
2368  }
2369 
2370  if (!EltTy) {
2371  if (!ItemType) {
2372  TokError("No type for list");
2373  return nullptr;
2374  }
2375  DeducedEltTy = GivenListTy->getElementType();
2376  } else {
2377  // Make sure the deduced type is compatible with the given type
2378  if (GivenListTy) {
2379  if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
2380  TokError(Twine("Element type mismatch for list: element type '") +
2381  EltTy->getAsString() + "' not convertible to '" +
2382  GivenListTy->getElementType()->getAsString());
2383  return nullptr;
2384  }
2385  }
2386  DeducedEltTy = EltTy;
2387  }
2388 
2389  return ListInit::get(Vals, DeducedEltTy);
2390  }
2391  case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
2392  Lex.Lex(); // eat the '('
2393  if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast &&
2394  Lex.getCode() != tgtok::question && Lex.getCode() != tgtok::XGetDagOp) {
2395  TokError("expected identifier in dag init");
2396  return nullptr;
2397  }
2398 
2399  Init *Operator = ParseValue(CurRec);
2400  if (!Operator) return nullptr;
2401 
2402  // If the operator name is present, parse it.
2403  StringInit *OperatorName = nullptr;
2404  if (consume(tgtok::colon)) {
2405  if (Lex.getCode() != tgtok::VarName) { // eat the ':'
2406  TokError("expected variable name in dag operator");
2407  return nullptr;
2408  }
2409  OperatorName = StringInit::get(Records, Lex.getCurStrVal());
2410  Lex.Lex(); // eat the VarName.
2411  }
2412 
2414  if (Lex.getCode() != tgtok::r_paren) {
2415  ParseDagArgList(DagArgs, CurRec);
2416  if (DagArgs.empty()) return nullptr;
2417  }
2418 
2419  if (!consume(tgtok::r_paren)) {
2420  TokError("expected ')' in dag init");
2421  return nullptr;
2422  }
2423 
2424  return DagInit::get(Operator, OperatorName, DagArgs);
2425  }
2426 
2427  case tgtok::XHead:
2428  case tgtok::XTail:
2429  case tgtok::XSize:
2430  case tgtok::XEmpty:
2431  case tgtok::XCast:
2432  case tgtok::XGetDagOp: // Value ::= !unop '(' Value ')'
2433  case tgtok::XExists:
2434  case tgtok::XIsA:
2435  case tgtok::XConcat:
2436  case tgtok::XDag:
2437  case tgtok::XADD:
2438  case tgtok::XSUB:
2439  case tgtok::XMUL:
2440  case tgtok::XDIV:
2441  case tgtok::XNOT:
2442  case tgtok::XLOG2:
2443  case tgtok::XAND:
2444  case tgtok::XOR:
2445  case tgtok::XXOR:
2446  case tgtok::XSRA:
2447  case tgtok::XSRL:
2448  case tgtok::XSHL:
2449  case tgtok::XEq:
2450  case tgtok::XNe:
2451  case tgtok::XLe:
2452  case tgtok::XLt:
2453  case tgtok::XGe:
2454  case tgtok::XGt:
2455  case tgtok::XListConcat:
2456  case tgtok::XListSplat:
2457  case tgtok::XStrConcat:
2458  case tgtok::XInterleave:
2459  case tgtok::XSetDagOp: // Value ::= !binop '(' Value ',' Value ')'
2460  case tgtok::XIf:
2461  case tgtok::XCond:
2462  case tgtok::XFoldl:
2463  case tgtok::XForEach:
2464  case tgtok::XFilter:
2465  case tgtok::XSubst:
2466  case tgtok::XSubstr:
2467  case tgtok::XFind: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
2468  return ParseOperation(CurRec, ItemType);
2469  }
2470  }
2471 
2472  return R;
2473 }
2474 
2475 /// ParseValue - Parse a TableGen value. This returns null on error.
2476 ///
2477 /// Value ::= SimpleValue ValueSuffix*
2478 /// ValueSuffix ::= '{' BitList '}'
2479 /// ValueSuffix ::= '[' BitList ']'
2480 /// ValueSuffix ::= '.' ID
2481 ///
2482 Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
2483  Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
2484  if (!Result) return nullptr;
2485 
2486  // Parse the suffixes now if present.
2487  while (true) {
2488  switch (Lex.getCode()) {
2489  default: return Result;
2490  case tgtok::l_brace: {
2491  if (Mode == ParseNameMode)
2492  // This is the beginning of the object body.
2493  return Result;
2494 
2495  SMLoc CurlyLoc = Lex.getLoc();
2496  Lex.Lex(); // eat the '{'
2498  ParseRangeList(Ranges);
2499  if (Ranges.empty()) return nullptr;
2500 
2501  // Reverse the bitlist.
2502  std::reverse(Ranges.begin(), Ranges.end());
2503  Result = Result->convertInitializerBitRange(Ranges);
2504  if (!Result) {
2505  Error(CurlyLoc, "Invalid bit range for value");
2506  return nullptr;
2507  }
2508 
2509  // Eat the '}'.
2510  if (!consume(tgtok::r_brace)) {
2511  TokError("expected '}' at end of bit range list");
2512  return nullptr;
2513  }
2514  break;
2515  }
2516  case tgtok::l_square: {
2517  SMLoc SquareLoc = Lex.getLoc();
2518  Lex.Lex(); // eat the '['
2520  ParseRangeList(Ranges);
2521  if (Ranges.empty()) return nullptr;
2522 
2523  Result = Result->convertInitListSlice(Ranges);
2524  if (!Result) {
2525  Error(SquareLoc, "Invalid range for list slice");
2526  return nullptr;
2527  }
2528 
2529  // Eat the ']'.
2530  if (!consume(tgtok::r_square)) {
2531  TokError("expected ']' at end of list slice");
2532  return nullptr;
2533  }
2534  break;
2535  }
2536  case tgtok::dot: {
2537  if (Lex.Lex() != tgtok::Id) { // eat the .
2538  TokError("expected field identifier after '.'");
2539  return nullptr;
2540  }
2541  SMRange FieldNameLoc = Lex.getLocRange();
2542  StringInit *FieldName = StringInit::get(Records, Lex.getCurStrVal());
2543  if (!Result->getFieldType(FieldName)) {
2544  TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
2545  Result->getAsString() + "'");
2546  return nullptr;
2547  }
2548 
2549  // Add a reference to this field if we know the record class.
2550  if (TrackReferenceLocs) {
2551  if (auto *DI = dyn_cast<DefInit>(Result)) {
2552  DI->getDef()->getValue(FieldName)->addReferenceLoc(FieldNameLoc);
2553  } else if (auto *TI = dyn_cast<TypedInit>(Result)) {
2554  if (auto *RecTy = dyn_cast<RecordRecTy>(TI->getType())) {
2555  for (Record *R : RecTy->getClasses())
2556  if (auto *RV = R->getValue(FieldName))
2557  RV->addReferenceLoc(FieldNameLoc);
2558  }
2559  }
2560  }
2561 
2562  Result = FieldInit::get(Result, FieldName)->Fold(CurRec);
2563  Lex.Lex(); // eat field name
2564  break;
2565  }
2566 
2567  case tgtok::paste:
2568  SMLoc PasteLoc = Lex.getLoc();
2569  TypedInit *LHS = dyn_cast<TypedInit>(Result);
2570  if (!LHS) {
2571  Error(PasteLoc, "LHS of paste is not typed!");
2572  return nullptr;
2573  }
2574 
2575  // Check if it's a 'listA # listB'
2576  if (isa<ListRecTy>(LHS->getType())) {
2577  Lex.Lex(); // Eat the '#'.
2578 
2579  assert(Mode == ParseValueMode && "encountered paste of lists in name");
2580 
2581  switch (Lex.getCode()) {
2582  case tgtok::colon:
2583  case tgtok::semi:
2584  case tgtok::l_brace:
2585  Result = LHS; // trailing paste, ignore.
2586  break;
2587  default:
2588  Init *RHSResult = ParseValue(CurRec, ItemType, ParseValueMode);
2589  if (!RHSResult)
2590  return nullptr;
2591  Result = BinOpInit::getListConcat(LHS, RHSResult);
2592  break;
2593  }
2594  break;
2595  }
2596 
2597  // Create a !strconcat() operation, first casting each operand to
2598  // a string if necessary.
2599  if (LHS->getType() != StringRecTy::get(Records)) {
2600  auto CastLHS = dyn_cast<TypedInit>(
2602  ->Fold(CurRec));
2603  if (!CastLHS) {
2604  Error(PasteLoc,
2605  Twine("can't cast '") + LHS->getAsString() + "' to string");
2606  return nullptr;
2607  }
2608  LHS = CastLHS;
2609  }
2610 
2611  TypedInit *RHS = nullptr;
2612 
2613  Lex.Lex(); // Eat the '#'.
2614  switch (Lex.getCode()) {
2615  case tgtok::colon:
2616  case tgtok::semi:
2617  case tgtok::l_brace:
2618  // These are all of the tokens that can begin an object body.
2619  // Some of these can also begin values but we disallow those cases
2620  // because they are unlikely to be useful.
2621 
2622  // Trailing paste, concat with an empty string.
2623  RHS = StringInit::get(Records, "");
2624  break;
2625 
2626  default:
2627  Init *RHSResult = ParseValue(CurRec, nullptr, ParseNameMode);
2628  if (!RHSResult)
2629  return nullptr;
2630  RHS = dyn_cast<TypedInit>(RHSResult);
2631  if (!RHS) {
2632  Error(PasteLoc, "RHS of paste is not typed!");
2633  return nullptr;
2634  }
2635 
2636  if (RHS->getType() != StringRecTy::get(Records)) {
2637  auto CastRHS = dyn_cast<TypedInit>(
2639  ->Fold(CurRec));
2640  if (!CastRHS) {
2641  Error(PasteLoc,
2642  Twine("can't cast '") + RHS->getAsString() + "' to string");
2643  return nullptr;
2644  }
2645  RHS = CastRHS;
2646  }
2647 
2648  break;
2649  }
2650 
2652  break;
2653  }
2654  }
2655 }
2656 
2657 /// ParseDagArgList - Parse the argument list for a dag literal expression.
2658 ///
2659 /// DagArg ::= Value (':' VARNAME)?
2660 /// DagArg ::= VARNAME
2661 /// DagArgList ::= DagArg
2662 /// DagArgList ::= DagArgList ',' DagArg
2663 void TGParser::ParseDagArgList(
2664  SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
2665  Record *CurRec) {
2666 
2667  while (true) {
2668  // DagArg ::= VARNAME
2669  if (Lex.getCode() == tgtok::VarName) {
2670  // A missing value is treated like '?'.
2671  StringInit *VarName = StringInit::get(Records, Lex.getCurStrVal());
2672  Result.emplace_back(UnsetInit::get(Records), VarName);
2673  Lex.Lex();
2674  } else {
2675  // DagArg ::= Value (':' VARNAME)?
2676  Init *Val = ParseValue(CurRec);
2677  if (!Val) {
2678  Result.clear();
2679  return;
2680  }
2681 
2682  // If the variable name is present, add it.
2683  StringInit *VarName = nullptr;
2684  if (Lex.getCode() == tgtok::colon) {
2685  if (Lex.Lex() != tgtok::VarName) { // eat the ':'
2686  TokError("expected variable name in dag literal");
2687  Result.clear();
2688  return;
2689  }
2690  VarName = StringInit::get(Records, Lex.getCurStrVal());
2691  Lex.Lex(); // eat the VarName.
2692  }
2693 
2694  Result.push_back(std::make_pair(Val, VarName));
2695  }
2696  if (!consume(tgtok::comma))
2697  break;
2698  }
2699 }
2700 
2701 /// ParseValueList - Parse a comma separated list of values, returning them
2702 /// in a vector. Note that this always expects to be able to parse at least one
2703 /// value. It returns an empty list if this is not possible.
2704 ///
2705 /// ValueList ::= Value (',' Value)
2706 ///
2707 void TGParser::ParseValueList(SmallVectorImpl<Init *> &Result, Record *CurRec,
2708  RecTy *ItemType) {
2709 
2710  Result.push_back(ParseValue(CurRec, ItemType));
2711  if (!Result.back()) {
2712  Result.clear();
2713  return;
2714  }
2715 
2716  while (consume(tgtok::comma)) {
2717  // ignore trailing comma for lists
2718  if (Lex.getCode() == tgtok::r_square)
2719  return;
2720  Result.push_back(ParseValue(CurRec, ItemType));
2721  if (!Result.back()) {
2722  Result.clear();
2723  return;
2724  }
2725  }
2726 }
2727 
2728 // ParseTemplateArgValueList - Parse a template argument list with the syntax
2729 // shown, filling in the Result vector. The open angle has been consumed.
2730 // An empty argument list is allowed. Return false if okay, true if an
2731 // error was detected.
2732 //
2733 // TemplateArgList ::= '<' [Value {',' Value}*] '>'
2734 bool TGParser::ParseTemplateArgValueList(SmallVectorImpl<Init *> &Result,
2735  Record *CurRec, Record *ArgsRec) {
2736 
2737  assert(Result.empty() && "Result vector is not empty");
2738  ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
2739  unsigned ArgIndex = 0;
2740  RecTy *ItemType;
2741 
2742  if (consume(tgtok::greater)) // empty value list
2743  return false;
2744 
2745  while (true) {
2746  if (ArgIndex >= TArgs.size()) {
2747  TokError("Too many template arguments: " + utostr(ArgIndex + 1));
2748  return true;
2749  }
2750  const RecordVal *Arg = ArgsRec->getValue(TArgs[ArgIndex]);
2751  assert(Arg && "Template argument record not found");
2752 
2753  ItemType = Arg->getType();
2754  Init *Value = ParseValue(CurRec, ItemType);
2755  if (!Value)
2756  return true;
2757  Result.push_back(Value);
2758 
2759  if (consume(tgtok::greater)) // end of argument list?
2760  return false;
2761  if (!consume(tgtok::comma))
2762  return TokError("Expected comma before next argument");
2763  ++ArgIndex;
2764  }
2765 }
2766 
2767 /// ParseDeclaration - Read a declaration, returning the name of field ID, or an
2768 /// empty string on error. This can happen in a number of different contexts,
2769 /// including within a def or in the template args for a class (in which case
2770 /// CurRec will be non-null) and within the template args for a multiclass (in
2771 /// which case CurRec will be null, but CurMultiClass will be set). This can
2772 /// also happen within a def that is within a multiclass, which will set both
2773 /// CurRec and CurMultiClass.
2774 ///
2775 /// Declaration ::= FIELD? Type ID ('=' Value)?
2776 ///
2777 Init *TGParser::ParseDeclaration(Record *CurRec,
2778  bool ParsingTemplateArgs) {
2779  // Read the field prefix if present.
2780  bool HasField = consume(tgtok::Field);
2781 
2782  RecTy *Type = ParseType();
2783  if (!Type) return nullptr;
2784 
2785  if (Lex.getCode() != tgtok::Id) {
2786  TokError("Expected identifier in declaration");
2787  return nullptr;
2788  }
2789 
2790  std::string Str = Lex.getCurStrVal();
2791  if (Str == "NAME") {
2792  TokError("'" + Str + "' is a reserved variable name");
2793  return nullptr;
2794  }
2795 
2796  SMLoc IdLoc = Lex.getLoc();
2797  Init *DeclName = StringInit::get(Records, Str);
2798  Lex.Lex();
2799 
2800  bool BadField;
2801  if (!ParsingTemplateArgs) { // def, possibly in a multiclass
2802  BadField = AddValue(CurRec, IdLoc,
2803  RecordVal(DeclName, IdLoc, Type,
2804  HasField ? RecordVal::FK_NonconcreteOK
2806 
2807  } else if (CurRec) { // class template argument
2808  DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
2809  BadField = AddValue(CurRec, IdLoc, RecordVal(DeclName, IdLoc, Type,
2811 
2812  } else { // multiclass template argument
2813  assert(CurMultiClass && "invalid context for template argument");
2814  DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName, "::");
2815  BadField = AddValue(CurRec, IdLoc, RecordVal(DeclName, IdLoc, Type,
2817  }
2818  if (BadField)
2819  return nullptr;
2820 
2821  // If a value is present, parse it and set new field's value.
2822  if (consume(tgtok::equal)) {
2823  SMLoc ValLoc = Lex.getLoc();
2824  Init *Val = ParseValue(CurRec, Type);
2825  if (!Val ||
2826  SetValue(CurRec, ValLoc, DeclName, None, Val,
2827  /*AllowSelfAssignment=*/false, /*OverrideDefLoc=*/false)) {
2828  // Return the name, even if an error is thrown. This is so that we can
2829  // continue to make some progress, even without the value having been
2830  // initialized.
2831  return DeclName;
2832  }
2833  }
2834 
2835  return DeclName;
2836 }
2837 
2838 /// ParseForeachDeclaration - Read a foreach declaration, returning
2839 /// the name of the declared object or a NULL Init on error. Return
2840 /// the name of the parsed initializer list through ForeachListName.
2841 ///
2842 /// ForeachDeclaration ::= ID '=' '{' RangeList '}'
2843 /// ForeachDeclaration ::= ID '=' RangePiece
2844 /// ForeachDeclaration ::= ID '=' Value
2845 ///
2846 VarInit *TGParser::ParseForeachDeclaration(Init *&ForeachListValue) {
2847  if (Lex.getCode() != tgtok::Id) {
2848  TokError("Expected identifier in foreach declaration");
2849  return nullptr;
2850  }
2851 
2852  Init *DeclName = StringInit::get(Records, Lex.getCurStrVal());
2853  Lex.Lex();
2854 
2855  // If a value is present, parse it.
2856  if (!consume(tgtok::equal)) {
2857  TokError("Expected '=' in foreach declaration");
2858  return nullptr;
2859  }
2860 
2861  RecTy *IterType = nullptr;
2863 
2864  switch (Lex.getCode()) {
2865  case tgtok::l_brace: { // '{' RangeList '}'
2866  Lex.Lex(); // eat the '{'
2867  ParseRangeList(Ranges);
2868  if (!consume(tgtok::r_brace)) {
2869  TokError("expected '}' at end of bit range list");
2870  return nullptr;
2871  }
2872  break;
2873  }
2874 
2875  default: {
2876  SMLoc ValueLoc = Lex.getLoc();
2877  Init *I = ParseValue(nullptr);
2878  if (!I)
2879  return nullptr;
2880 
2881  TypedInit *TI = dyn_cast<TypedInit>(I);
2882  if (TI && isa<ListRecTy>(TI->getType())) {
2883  ForeachListValue = I;
2884  IterType = cast<ListRecTy>(TI->getType())->getElementType();
2885  break;
2886  }
2887 
2888  if (TI) {
2889  if (ParseRangePiece(Ranges, TI))
2890  return nullptr;
2891  break;
2892  }
2893 
2894  Error(ValueLoc, "expected a list, got '" + I->getAsString() + "'");
2895  if (CurMultiClass) {
2896  PrintNote({}, "references to multiclass template arguments cannot be "
2897  "resolved at this time");
2898  }
2899  return nullptr;
2900  }
2901  }
2902 
2903 
2904  if (!Ranges.empty()) {
2905  assert(!IterType && "Type already initialized?");
2906  IterType = IntRecTy::get(Records);
2907  std::vector<Init *> Values;
2908  for (unsigned R : Ranges)
2909  Values.push_back(IntInit::get(Records, R));
2910  ForeachListValue = ListInit::get(Values, IterType);
2911  }
2912 
2913  if (!IterType)
2914  return nullptr;
2915 
2916  return VarInit::get(DeclName, IterType);
2917 }
2918 
2919 /// ParseTemplateArgList - Read a template argument list, which is a non-empty
2920 /// sequence of template-declarations in <>'s. If CurRec is non-null, these are
2921 /// template args for a class. If null, these are the template args for a
2922 /// multiclass.
2923 ///
2924 /// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
2925 ///
2926 bool TGParser::ParseTemplateArgList(Record *CurRec) {
2927  assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
2928  Lex.Lex(); // eat the '<'
2929 
2930  Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
2931 
2932  // Read the first declaration.
2933  Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
2934  if (!TemplArg)
2935  return true;
2936 
2937  TheRecToAddTo->addTemplateArg(TemplArg);
2938 
2939  while (consume(tgtok::comma)) {
2940  // Read the following declarations.
2941  SMLoc Loc = Lex.getLoc();
2942  TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
2943  if (!TemplArg)
2944  return true;
2945 
2946  if (TheRecToAddTo->isTemplateArg(TemplArg))
2947  return Error(Loc, "template argument with the same name has already been "
2948  "defined");
2949 
2950  TheRecToAddTo->addTemplateArg(TemplArg);
2951  }
2952 
2953  if (!consume(tgtok::greater))
2954  return TokError("expected '>' at end of template argument list");
2955  return false;
2956 }
2957 
2958 /// ParseBodyItem - Parse a single item within the body of a def or class.
2959 ///
2960 /// BodyItem ::= Declaration ';'
2961 /// BodyItem ::= LET ID OptionalBitList '=' Value ';'
2962 /// BodyItem ::= Defvar
2963 /// BodyItem ::= Assert
2964 ///
2965 bool TGParser::ParseBodyItem(Record *CurRec) {
2966  if (Lex.getCode() == tgtok::Assert)
2967  return ParseAssert(nullptr, CurRec);
2968 
2969  if (Lex.getCode() == tgtok::Defvar)
2970  return ParseDefvar();
2971 
2972  if (Lex.getCode() != tgtok::Let) {
2973  if (!ParseDeclaration(CurRec, false))
2974  return true;
2975 
2976  if (!consume(tgtok::semi))
2977  return TokError("expected ';' after declaration");
2978  return false;
2979  }
2980 
2981  // LET ID OptionalRangeList '=' Value ';'
2982  if (Lex.Lex() != tgtok::Id)
2983  return TokError("expected field identifier after let");
2984 
2985  SMLoc IdLoc = Lex.getLoc();
2986  StringInit *FieldName = StringInit::get(Records, Lex.getCurStrVal());
2987  Lex.Lex(); // eat the field name.
2988 
2989  SmallVector<unsigned, 16> BitList;
2990  if (ParseOptionalBitList(BitList))
2991  return true;
2992  std::reverse(BitList.begin(), BitList.end());
2993 
2994  if (!consume(tgtok::equal))
2995  return TokError("expected '=' in let expression");
2996 
2997  RecordVal *Field = CurRec->getValue(FieldName);
2998  if (!Field)
2999  return TokError("Value '" + FieldName->getValue() + "' unknown!");
3000 
3001  RecTy *Type = Field->getType();
3002  if (!BitList.empty() && isa<BitsRecTy>(Type)) {
3003  // When assigning to a subset of a 'bits' object, expect the RHS to have
3004  // the type of that subset instead of the type of the whole object.
3005  Type = BitsRecTy::get(Records, BitList.size());
3006  }
3007 
3008  Init *Val = ParseValue(CurRec, Type);
3009  if (!Val) return true;
3010 
3011  if (!consume(tgtok::semi))
3012  return TokError("expected ';' after let expression");
3013 
3014  return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
3015 }
3016 
3017 /// ParseBody - Read the body of a class or def. Return true on error, false on
3018 /// success.
3019 ///
3020 /// Body ::= ';'
3021 /// Body ::= '{' BodyList '}'
3022 /// BodyList BodyItem*
3023 ///
3024 bool TGParser::ParseBody(Record *CurRec) {
3025  // If this is a null definition, just eat the semi and return.
3026  if (consume(tgtok::semi))
3027  return false;
3028 
3029  if (!consume(tgtok::l_brace))
3030  return TokError("Expected '{' to start body or ';' for declaration only");
3031 
3032  // An object body introduces a new scope for local variables.
3033  TGLocalVarScope *BodyScope = PushLocalScope();
3034 
3035  while (Lex.getCode() != tgtok::r_brace)
3036  if (ParseBodyItem(CurRec))
3037  return true;
3038 
3039  PopLocalScope(BodyScope);
3040 
3041  // Eat the '}'.
3042  Lex.Lex();
3043 
3044  // If we have a semicolon, print a gentle error.
3045  SMLoc SemiLoc = Lex.getLoc();
3046  if (consume(tgtok::semi)) {
3047  PrintError(SemiLoc, "A class or def body should not end with a semicolon");
3048  PrintNote("Semicolon ignored; remove to eliminate this error");
3049  }
3050 
3051  return false;
3052 }
3053 
3054 /// Apply the current let bindings to \a CurRec.
3055 /// \returns true on error, false otherwise.
3056 bool TGParser::ApplyLetStack(Record *CurRec) {
3057  for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
3058  for (LetRecord &LR : LetInfo)
3059  if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
3060  return true;
3061  return false;
3062 }
3063 
3064 /// Apply the current let bindings to the RecordsEntry.
3065 bool TGParser::ApplyLetStack(RecordsEntry &Entry) {
3066  if (Entry.Rec)
3067  return ApplyLetStack(Entry.Rec.get());
3068 
3069  // Let bindings are not applied to assertions.
3070  if (Entry.Assertion)
3071  return false;
3072 
3073  for (auto &E : Entry.Loop->Entries) {
3074  if (ApplyLetStack(E))
3075  return true;
3076  }
3077 
3078  return false;
3079 }
3080 
3081 /// ParseObjectBody - Parse the body of a def or class. This consists of an
3082 /// optional ClassList followed by a Body. CurRec is the current def or class
3083 /// that is being parsed.
3084 ///
3085 /// ObjectBody ::= BaseClassList Body
3086 /// BaseClassList ::= /*empty*/
3087 /// BaseClassList ::= ':' BaseClassListNE
3088 /// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
3089 ///
3090 bool TGParser::ParseObjectBody(Record *CurRec) {
3091  // If there is a baseclass list, read it.
3092  if (consume(tgtok::colon)) {
3093 
3094  // Read all of the subclasses.
3095  SubClassReference SubClass = ParseSubClassReference(CurRec, false);
3096  while (true) {
3097  // Check for error.
3098  if (!SubClass.Rec) return true;
3099 
3100  // Add it.
3101  if (AddSubClass(CurRec, SubClass))
3102  return true;
3103 
3104  if (!consume(tgtok::comma))
3105  break;
3106  SubClass = ParseSubClassReference(CurRec, false);
3107  }
3108  }
3109 
3110  if (ApplyLetStack(CurRec))
3111  return true;
3112 
3113  return ParseBody(CurRec);
3114 }
3115 
3116 /// ParseDef - Parse and return a top level or multiclass record definition.
3117 /// Return false if okay, true if error.
3118 ///
3119 /// DefInst ::= DEF ObjectName ObjectBody
3120 ///
3121 bool TGParser::ParseDef(MultiClass *CurMultiClass) {
3122  SMLoc DefLoc = Lex.getLoc();
3123  assert(Lex.getCode() == tgtok::Def && "Unknown tok");
3124  Lex.Lex(); // Eat the 'def' token.
3125 
3126  // If the name of the def is an Id token, use that for the location.
3127  // Otherwise, the name is more complex and we use the location of the 'def'
3128  // token.
3129  SMLoc NameLoc = Lex.getCode() == tgtok::Id ? Lex.getLoc() : DefLoc;
3130 
3131  // Parse ObjectName and make a record for it.
3132  std::unique_ptr<Record> CurRec;
3133  Init *Name = ParseObjectName(CurMultiClass);
3134  if (!Name)
3135  return true;
3136 
3137  if (isa<UnsetInit>(Name)) {
3138  CurRec =
3139  std::make_unique<Record>(Records.getNewAnonymousName(), DefLoc, Records,
3140  /*Anonymous=*/true);
3141  } else {
3142  CurRec = std::make_unique<Record>(Name, NameLoc, Records);
3143  }
3144 
3145  if (ParseObjectBody(CurRec.get()))
3146  return true;
3147 
3148  return addEntry(std::move(CurRec));
3149 }
3150 
3151 /// ParseDefset - Parse a defset statement.
3152 ///
3153 /// Defset ::= DEFSET Type Id '=' '{' ObjectList '}'
3154 ///
3155 bool TGParser::ParseDefset() {
3156  assert(Lex.getCode() == tgtok::Defset);
3157  Lex.Lex(); // Eat the 'defset' token
3158 
3160  Defset.Loc = Lex.getLoc();
3161  RecTy *Type = ParseType();
3162  if (!Type)
3163  return true;
3164  if (!isa<ListRecTy>(Type))
3165  return Error(Defset.Loc, "expected list type");
3166  Defset.EltTy = cast<ListRecTy>(Type)->getElementType();
3167 
3168  if (Lex.getCode() != tgtok::Id)
3169  return TokError("expected identifier");
3170  StringInit *DeclName = StringInit::get(Records, Lex.getCurStrVal());
3171  if (Records.getGlobal(DeclName->getValue()))
3172  return TokError("def or global variable of this name already exists");
3173 
3174  if (Lex.Lex() != tgtok::equal) // Eat the identifier
3175  return TokError("expected '='");
3176  if (Lex.Lex() != tgtok::l_brace) // Eat the '='
3177  return TokError("expected '{'");
3178  SMLoc BraceLoc = Lex.getLoc();
3179  Lex.Lex(); // Eat the '{'
3180 
3181  Defsets.push_back(&Defset);
3182  bool Err = ParseObjectList(nullptr);
3183  Defsets.pop_back();
3184  if (Err)
3185  return true;
3186 
3187  if (!consume(tgtok::r_brace)) {
3188  TokError("expected '}' at end of defset");
3189  return Error(BraceLoc, "to match this '{'");
3190  }
3191 
3192  Records.addExtraGlobal(DeclName->getValue(),
3193  ListInit::get(Defset.Elements, Defset.EltTy));
3194  return false;
3195 }
3196 
3197 /// ParseDefvar - Parse a defvar statement.
3198 ///
3199 /// Defvar ::= DEFVAR Id '=' Value ';'
3200 ///
3201 bool TGParser::ParseDefvar() {
3202  assert(Lex.getCode() == tgtok::Defvar);
3203  Lex.Lex(); // Eat the 'defvar' token
3204 
3205  if (Lex.getCode() != tgtok::Id)
3206  return TokError("expected identifier");
3207  StringInit *DeclName = StringInit::get(Records, Lex.getCurStrVal());
3208  if (CurLocalScope) {
3209  if (CurLocalScope->varAlreadyDefined(DeclName->getValue()))
3210  return TokError("local variable of this name already exists");
3211  } else {
3212  if (Records.getGlobal(DeclName->getValue()))
3213  return TokError("def or global variable of this name already exists");
3214  }
3215 
3216  Lex.Lex();
3217  if (!consume(tgtok::equal))
3218  return TokError("expected '='");
3219 
3220  Init *Value = ParseValue(nullptr);
3221  if (!Value)
3222  return true;
3223 
3224  if (!consume(tgtok::semi))
3225  return TokError("expected ';'");
3226 
3227  if (CurLocalScope)
3228  CurLocalScope->addVar(DeclName->getValue(), Value);
3229  else
3230  Records.addExtraGlobal(DeclName->getValue(), Value);
3231 
3232  return false;
3233 }
3234 
3235 /// ParseForeach - Parse a for statement. Return the record corresponding
3236 /// to it. This returns true on error.
3237 ///
3238 /// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
3239 /// Foreach ::= FOREACH Declaration IN Object
3240 ///
3241 bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
3242  SMLoc Loc = Lex.getLoc();
3243  assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
3244  Lex.Lex(); // Eat the 'for' token.
3245 
3246  // Make a temporary object to record items associated with the for
3247  // loop.
3248  Init *ListValue = nullptr;
3249  VarInit *IterName = ParseForeachDeclaration(ListValue);
3250  if (!IterName)
3251  return TokError("expected declaration in for");
3252 
3253  if (!consume(tgtok::In))
3254  return TokError("Unknown tok");
3255 
3256  // Create a loop object and remember it.
3257  Loops.push_back(std::make_unique<ForeachLoop>(Loc, IterName, ListValue));
3258 
3259  // A foreach loop introduces a new scope for local variables.
3260  TGLocalVarScope *ForeachScope = PushLocalScope();
3261 
3262  if (Lex.getCode() != tgtok::l_brace) {
3263  // FOREACH Declaration IN Object
3264  if (ParseObject(CurMultiClass))
3265  return true;
3266  } else {
3267  SMLoc BraceLoc = Lex.getLoc();
3268  // Otherwise, this is a group foreach.
3269  Lex.Lex(); // eat the '{'.
3270 
3271  // Parse the object list.
3272  if (ParseObjectList(CurMultiClass))
3273  return true;
3274 
3275  if (!consume(tgtok::r_brace)) {
3276  TokError("expected '}' at end of foreach command");
3277  return Error(BraceLoc, "to match this '{'");
3278  }
3279  }
3280 
3281  PopLocalScope(ForeachScope);
3282 
3283  // Resolve the loop or store it for later resolution.
3284  std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back());
3285  Loops.pop_back();
3286 
3287  return addEntry(std::move(Loop));
3288 }
3289 
3290 /// ParseIf - Parse an if statement.
3291 ///
3292 /// If ::= IF Value THEN IfBody
3293 /// If ::= IF Value THEN IfBody ELSE IfBody
3294 ///
3295 bool TGParser::ParseIf(MultiClass *CurMultiClass) {
3296  SMLoc Loc = Lex.getLoc();
3297  assert(Lex.getCode() == tgtok::If && "Unknown tok");
3298  Lex.Lex(); // Eat the 'if' token.
3299 
3300  // Make a temporary object to record items associated with the for
3301  // loop.
3302  Init *Condition = ParseValue(nullptr);
3303  if (!Condition)
3304  return true;
3305 
3306  if (!consume(tgtok::Then))
3307  return TokError("Unknown tok");
3308 
3309  // We have to be able to save if statements to execute later, and they have
3310  // to live on the same stack as foreach loops. The simplest implementation
3311  // technique is to convert each 'then' or 'else' clause *into* a foreach
3312  // loop, over a list of length 0 or 1 depending on the condition, and with no
3313  // iteration variable being assigned.
3314 
3315  ListInit *EmptyList = ListInit::get({}, BitRecTy::get(Records));
3316  ListInit *SingletonList =
3317  ListInit::get({BitInit::get(Records, true)}, BitRecTy::get(Records));
3318  RecTy *BitListTy = ListRecTy::get(BitRecTy::get(Records));
3319 
3320  // The foreach containing the then-clause selects SingletonList if
3321  // the condition is true.
3322  Init *ThenClauseList =
3323  TernOpInit::get(TernOpInit::IF, Condition, SingletonList, EmptyList,
3324  BitListTy)
3325  ->Fold(nullptr);
3326  Loops.push_back(std::make_unique<ForeachLoop>(Loc, nullptr, ThenClauseList));
3327 
3328  if (ParseIfBody(CurMultiClass, "then"))
3329  return true;
3330 
3331  std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back());
3332  Loops.pop_back();
3333 
3334  if (addEntry(std::move(Loop)))
3335  return true;
3336 
3337  // Now look for an optional else clause. The if-else syntax has the usual
3338  // dangling-else ambiguity, and by greedily matching an else here if we can,
3339  // we implement the usual resolution of pairing with the innermost unmatched
3340  // if.
3341  if (consume(tgtok::ElseKW)) {
3342  // The foreach containing the else-clause uses the same pair of lists as
3343  // above, but this time, selects SingletonList if the condition is *false*.
3344  Init *ElseClauseList =
3345  TernOpInit::get(TernOpInit::IF, Condition, EmptyList, SingletonList,
3346  BitListTy)
3347  ->Fold(nullptr);
3348  Loops.push_back(
3349  std::make_unique<ForeachLoop>(Loc, nullptr, ElseClauseList));
3350 
3351  if (ParseIfBody(CurMultiClass, "else"))
3352  return true;
3353 
3354  Loop = std::move(Loops.back());
3355  Loops.pop_back();
3356 
3357  if (addEntry(std::move(Loop)))
3358  return true;
3359  }
3360 
3361  return false;
3362 }
3363 
3364 /// ParseIfBody - Parse the then-clause or else-clause of an if statement.
3365 ///
3366 /// IfBody ::= Object
3367 /// IfBody ::= '{' ObjectList '}'
3368 ///
3369 bool TGParser::ParseIfBody(MultiClass *CurMultiClass, StringRef Kind) {
3370  TGLocalVarScope *BodyScope = PushLocalScope();
3371 
3372  if (Lex.getCode() != tgtok::l_brace) {
3373  // A single object.
3374  if (ParseObject(CurMultiClass))
3375  return true;
3376  } else {
3377  SMLoc BraceLoc = Lex.getLoc();
3378  // A braced block.
3379  Lex.Lex(); // eat the '{'.
3380 
3381  // Parse the object list.
3382  if (ParseObjectList(CurMultiClass))
3383  return true;
3384 
3385  if (!consume(tgtok::r_brace)) {
3386  TokError("expected '}' at end of '" + Kind + "' clause");
3387  return Error(BraceLoc, "to match this '{'");
3388  }
3389  }
3390 
3391  PopLocalScope(BodyScope);
3392  return false;
3393 }
3394 
3395 /// ParseAssert - Parse an assert statement.
3396 ///
3397 /// Assert ::= ASSERT condition , message ;
3398 bool TGParser::ParseAssert(MultiClass *CurMultiClass, Record *CurRec) {
3399  assert(Lex.getCode() == tgtok::Assert && "Unknown tok");
3400  Lex.Lex(); // Eat the 'assert' token.
3401 
3402  SMLoc ConditionLoc = Lex.getLoc();
3403  Init *Condition = ParseValue(CurRec);
3404  if (!Condition)
3405  return true;
3406 
3407  if (!consume(tgtok::comma)) {
3408  TokError("expected ',' in assert statement");
3409  return true;
3410  }
3411 
3412  Init *Message = ParseValue(CurRec);
3413  if (!Message)
3414  return true;
3415 
3416  if (!consume(tgtok::semi))
3417  return TokError("expected ';'");
3418 
3419  if (CurRec)
3420  CurRec->addAssertion(ConditionLoc, Condition, Message);
3421  else
3422  addEntry(std::make_unique<Record::AssertionInfo>(ConditionLoc, Condition,
3423  Message));
3424  return false;
3425 }
3426 
3427 /// ParseClass - Parse a tblgen class definition.
3428 ///
3429 /// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
3430 ///
3431 bool TGParser::ParseClass() {
3432  assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
3433  Lex.Lex();
3434 
3435  if (Lex.getCode() != tgtok::Id)
3436  return TokError("expected class name after 'class' keyword");
3437 
3438  Record *CurRec = Records.getClass(Lex.getCurStrVal());
3439  if (CurRec) {
3440  // If the body was previously defined, this is an error.
3441  if (!CurRec->getValues().empty() ||
3442  !CurRec->getSuperClasses().empty() ||
3443  !CurRec->getTemplateArgs().empty())
3444  return TokError("Class '" + CurRec->getNameInitAsString() +
3445  "' already defined");
3446 
3447  CurRec->updateClassLoc(Lex.getLoc());
3448  } else {
3449  // If this is the first reference to this class, create and add it.
3450  auto NewRec =
3451  std::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records,
3452  /*Class=*/true);
3453  CurRec = NewRec.get();
3454  Records.addClass(std::move(NewRec));
3455  }
3456  Lex.Lex(); // eat the name.
3457 
3458  // If there are template args, parse them.
3459  if (Lex.getCode() == tgtok::less)
3460  if (ParseTemplateArgList(CurRec))
3461  return true;
3462 
3463  if (ParseObjectBody(CurRec))
3464  return true;
3465 
3466  if (!NoWarnOnUnusedTemplateArgs)
3467  CurRec->checkUnusedTemplateArgs();
3468  return false;
3469 }
3470 
3471 /// ParseLetList - Parse a non-empty list of assignment expressions into a list
3472 /// of LetRecords.
3473 ///
3474 /// LetList ::= LetItem (',' LetItem)*
3475 /// LetItem ::= ID OptionalRangeList '=' Value
3476 ///
3477 void TGParser::ParseLetList(SmallVectorImpl<LetRecord> &Result) {
3478  do {
3479  if (Lex.getCode() != tgtok::Id) {
3480  TokError("expected identifier in let definition");
3481  Result.clear();
3482  return;
3483  }
3484 
3485  StringInit *Name = StringInit::get(Records, Lex.getCurStrVal());
3486  SMLoc NameLoc = Lex.getLoc();
3487  Lex.Lex(); // Eat the identifier.
3488 
3489  // Check for an optional RangeList.
3491  if (ParseOptionalRangeList(Bits)) {
3492  Result.clear();
3493  return;
3494  }
3495  std::reverse(Bits.begin(), Bits.end());
3496 
3497  if (!consume(tgtok::equal)) {
3498  TokError("expected '=' in let expression");
3499  Result.clear();
3500  return;
3501  }
3502 
3503  Init *Val = ParseValue(nullptr);
3504  if (!Val) {
3505  Result.clear();
3506  return;
3507  }
3508 
3509  // Now that we have everything, add the record.
3510  Result.emplace_back(Name, Bits, Val, NameLoc);
3511  } while (consume(tgtok::comma));
3512 }
3513 
3514 /// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
3515 /// different related productions. This works inside multiclasses too.
3516 ///
3517 /// Object ::= LET LetList IN '{' ObjectList '}'
3518 /// Object ::= LET LetList IN Object
3519 ///
3520 bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
3521  assert(Lex.getCode() == tgtok::Let && "Unexpected token");
3522  Lex.Lex();
3523 
3524  // Add this entry to the let stack.
3525  SmallVector<LetRecord, 8> LetInfo;
3526  ParseLetList(LetInfo);
3527  if (LetInfo.empty()) return true;
3528  LetStack.push_back(std::move(LetInfo));
3529 
3530  if (!consume(tgtok::In))
3531  return TokError("expected 'in' at end of top-level 'let'");
3532 
3533  TGLocalVarScope *LetScope = PushLocalScope();
3534 
3535  // If this is a scalar let, just handle it now
3536  if (Lex.getCode() != tgtok::l_brace) {
3537  // LET LetList IN Object
3538  if (ParseObject(CurMultiClass))
3539  return true;
3540  } else { // Object ::= LETCommand '{' ObjectList '}'
3541  SMLoc BraceLoc = Lex.getLoc();
3542  // Otherwise, this is a group let.
3543  Lex.Lex(); // eat the '{'.
3544 
3545  // Parse the object list.
3546  if (ParseObjectList(CurMultiClass))
3547  return true;
3548 
3549  if (!consume(tgtok::r_brace)) {
3550  TokError("expected '}' at end of top level let command");
3551  return Error(BraceLoc, "to match this '{'");
3552  }
3553  }
3554 
3555  PopLocalScope(LetScope);
3556 
3557  // Outside this let scope, this let block is not active.
3558  LetStack.pop_back();
3559  return false;
3560 }
3561 
3562 /// ParseMultiClass - Parse a multiclass definition.
3563 ///
3564 /// MultiClassInst ::= MULTICLASS ID TemplateArgList?
3565 /// ':' BaseMultiClassList '{' MultiClassObject+ '}'
3566 /// MultiClassObject ::= Assert
3567 /// MultiClassObject ::= DefInst
3568 /// MultiClassObject ::= DefMInst
3569 /// MultiClassObject ::= Defvar
3570 /// MultiClassObject ::= Foreach
3571 /// MultiClassObject ::= If
3572 /// MultiClassObject ::= LETCommand '{' ObjectList '}'
3573 /// MultiClassObject ::= LETCommand Object
3574 ///
3575 bool TGParser::ParseMultiClass() {
3576  assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
3577  Lex.Lex(); // Eat the multiclass token.
3578 
3579  if (Lex.getCode() != tgtok::Id)
3580  return TokError("expected identifier after multiclass for name");
3581  std::string Name = Lex.getCurStrVal();
3582 
3583  auto Result =
3584  MultiClasses.insert(std::make_pair(Name,
3585  std::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
3586 
3587  if (!Result.second)
3588  return TokError("multiclass '" + Name + "' already defined");
3589 
3590  CurMultiClass = Result.first->second.get();
3591  Lex.Lex(); // Eat the identifier.
3592 
3593  // If there are template args, parse them.
3594  if (Lex.getCode() == tgtok::less)
3595  if (ParseTemplateArgList(nullptr))
3596  return true;
3597 
3598  bool inherits = false;
3599 
3600  // If there are submulticlasses, parse them.
3601  if (consume(tgtok::colon)) {
3602  inherits = true;
3603 
3604  // Read all of the submulticlasses.
3605  SubMultiClassReference SubMultiClass =
3606  ParseSubMultiClassReference(CurMultiClass);
3607  while (true) {
3608  // Check for error.
3609  if (!SubMultiClass.MC) return true;
3610 
3611  // Add it.
3612  if (AddSubMultiClass(CurMultiClass, SubMultiClass))
3613  return true;
3614 
3615  if (!consume(tgtok::comma))
3616  break;
3617  SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
3618  }
3619  }
3620 
3621  if (Lex.getCode() != tgtok::l_brace) {
3622  if (!inherits)
3623  return TokError("expected '{' in multiclass definition");
3624  if (!consume(tgtok::semi))
3625  return TokError("expected ';' in multiclass definition");
3626  } else {
3627  if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
3628  return TokError("multiclass must contain at least one def");
3629 
3630  // A multiclass body introduces a new scope for local variables.
3631  TGLocalVarScope *MulticlassScope = PushLocalScope();
3632 
3633  while (Lex.getCode() != tgtok::r_brace) {
3634  switch (Lex.getCode()) {
3635  default:
3636  return TokError("expected 'assert', 'def', 'defm', 'defvar', "
3637  "'foreach', 'if', or 'let' in multiclass body");
3638 
3639  case tgtok::Assert:
3640  case tgtok::Def:
3641  case tgtok::Defm:
3642  case tgtok::Defvar:
3643  case tgtok::Foreach:
3644  case tgtok::If:
3645  case tgtok::Let:
3646  if (ParseObject(CurMultiClass))
3647  return true;
3648  break;
3649  }
3650  }
3651  Lex.Lex(); // eat the '}'.
3652 
3653  // If we have a semicolon, print a gentle error.
3654  SMLoc SemiLoc = Lex.getLoc();
3655  if (consume(tgtok::semi)) {
3656  PrintError(SemiLoc, "A multiclass body should not end with a semicolon");
3657  PrintNote("Semicolon ignored; remove to eliminate this error");
3658  }
3659 
3660  PopLocalScope(MulticlassScope);
3661  }
3662 
3663  if (!NoWarnOnUnusedTemplateArgs)
3664  CurMultiClass->Rec.checkUnusedTemplateArgs();
3665 
3666  CurMultiClass = nullptr;
3667  return false;
3668 }
3669 
3670 /// ParseDefm - Parse the instantiation of a multiclass.
3671 ///
3672 /// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
3673 ///
3674 bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
3675  assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
3676  Lex.Lex(); // eat the defm
3677 
3678  Init *DefmName = ParseObjectName(CurMultiClass);
3679  if (!DefmName)
3680  return true;
3681  if (isa<UnsetInit>(DefmName)) {
3682  DefmName = Records.getNewAnonymousName();
3683  if (CurMultiClass)
3684  DefmName = BinOpInit::getStrConcat(
3686  StringRecTy::get(Records)),
3687  DefmName);
3688  }
3689 
3690  if (Lex.getCode() != tgtok::colon)
3691  return TokError("expected ':' after defm identifier");
3692 
3693  // Keep track of the new generated record definitions.
3694  std::vector<RecordsEntry> NewEntries;
3695 
3696  // This record also inherits from a regular class (non-multiclass)?
3697  bool InheritFromClass = false;
3698 
3699  // eat the colon.
3700  Lex.Lex();
3701 
3702  SMLoc SubClassLoc = Lex.getLoc();
3703  SubClassReference Ref = ParseSubClassReference(nullptr, true);
3704 
3705  while (true) {
3706  if (!Ref.Rec) return true;
3707 
3708  // To instantiate a multiclass, we get the multiclass and then loop
3709  // through its template argument names. Substs contains a substitution
3710  // value for each argument, either the value specified or the default.
3711  // Then we can resolve the template arguments.
3712  MultiClass *MC = MultiClasses[std::string(Ref.Rec->getName())].get();
3713  assert(MC && "Didn't lookup multiclass correctly?");
3714 
3715  ArrayRef<Init *> TemplateVals = Ref.TemplateArgs;
3716  ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();
3717  SubstStack Substs;
3718 
3719  for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
3720  if (i < TemplateVals.size()) {
3721  Substs.emplace_back(TArgs[i], TemplateVals[i]);
3722  } else {
3723  Init *Default = MC->Rec.getValue(TArgs[i])->getValue();
3724  if (!Default->isComplete())
3725  return Error(SubClassLoc,
3726  "value not specified for template argument '" +
3727  TArgs[i]->getAsUnquotedString() + "' (#" +
3728  Twine(i) + ") of multiclass '" +
3729  MC->Rec.getNameInitAsString() + "'");
3730  Substs.emplace_back(TArgs[i], Default);
3731  }
3732  }
3733 
3734  Substs.emplace_back(QualifiedNameOfImplicitName(MC), DefmName);
3735 
3736  if (resolve(MC->Entries, Substs, !CurMultiClass && Loops.empty(),
3737  &NewEntries, &SubClassLoc))
3738  return true;
3739 
3740  if (!consume(tgtok::comma))
3741  break;
3742 
3743  if (Lex.getCode() != tgtok::Id)
3744  return TokError("expected identifier");
3745 
3746  SubClassLoc = Lex.getLoc();
3747 
3748  // A defm can inherit from regular classes (non-multiclasses) as
3749  // long as they come in the end of the inheritance list.
3750  InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
3751 
3752  if (InheritFromClass)
3753  break;
3754 
3755  Ref = ParseSubClassReference(nullptr, true);
3756  }
3757 
3758  if (InheritFromClass) {
3759  // Process all the classes to inherit as if they were part of a
3760  // regular 'def' and inherit all record values.
3761  SubClassReference SubClass = ParseSubClassReference(nullptr, false);
3762  while (true) {
3763  // Check for error.
3764  if (!SubClass.Rec) return true;
3765 
3766  // Get the expanded definition prototypes and teach them about
3767  // the record values the current class to inherit has
3768  for (auto &E : NewEntries) {
3769  // Add it.
3770  if (AddSubClass(E, SubClass))
3771  return true;
3772  }
3773 
3774  if (!consume(tgtok::comma))
3775  break;
3776  SubClass = ParseSubClassReference(nullptr, false);
3777  }
3778  }
3779 
3780  for (auto &E : NewEntries) {
3781  if (ApplyLetStack(E))
3782  return true;
3783 
3784  addEntry(std::move(E));
3785  }
3786 
3787  if (!consume(tgtok::semi))
3788  return TokError("expected ';' at end of defm");
3789 
3790  return false;
3791 }
3792 
3793 /// ParseObject
3794 /// Object ::= ClassInst
3795 /// Object ::= DefInst
3796 /// Object ::= MultiClassInst
3797 /// Object ::= DefMInst
3798 /// Object ::= LETCommand '{' ObjectList '}'
3799 /// Object ::= LETCommand Object
3800 /// Object ::= Defset
3801 /// Object ::= Defvar
3802 /// Object ::= Assert
3803 bool TGParser::ParseObject(MultiClass *MC) {
3804  switch (Lex.getCode()) {
3805  default:
3806  return TokError(
3807  "Expected assert, class, def, defm, defset, foreach, if, or let");
3808  case tgtok::Assert: return ParseAssert(MC);
3809  case tgtok::Def: return ParseDef(MC);
3810  case tgtok::Defm: return ParseDefm(MC);
3811  case tgtok::Defvar: return ParseDefvar();
3812  case tgtok::Foreach: return ParseForeach(MC);
3813  case tgtok::If: return ParseIf(MC);
3814  case tgtok::Let: return ParseTopLevelLet(MC);
3815  case tgtok::Defset:
3816  if (MC)
3817  return TokError("defset is not allowed inside multiclass");
3818  return ParseDefset();
3819  case tgtok::Class:
3820  if (MC)
3821  return TokError("class is not allowed inside multiclass");
3822  if (!Loops.empty())
3823  return TokError("class is not allowed inside foreach loop");
3824  return ParseClass();
3825  case tgtok::MultiClass:
3826  if (!Loops.empty())
3827  return TokError("multiclass is not allowed inside foreach loop");
3828  return ParseMultiClass();
3829  }
3830 }
3831 
3832 /// ParseObjectList
3833 /// ObjectList :== Object*
3834 bool TGParser::ParseObjectList(MultiClass *MC) {
3835  while (isObjectStart(Lex.getCode())) {
3836  if (ParseObject(MC))
3837  return true;
3838  }
3839  return false;
3840 }
3841 
3843  Lex.Lex(); // Prime the lexer.
3844  if (ParseObjectList()) return true;
3845 
3846  // If we have unread input at the end of the file, report it.
3847  if (Lex.getCode() == tgtok::Eof)
3848  return false;
3849 
3850  return TokError("Unexpected token at top level");
3851 }
3852 
3853 // Check the types of the template argument values for a class
3854 // inheritance, multiclass invocation, or anonymous class invocation.
3855 // If necessary, replace an argument with a cast to the required type.
3856 // The argument count has already been checked.
3857 bool TGParser::CheckTemplateArgValues(SmallVectorImpl<llvm::Init *> &Values,
3858  SMLoc Loc, Record *ArgsRec) {
3859 
3860  ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
3861 
3862  for (unsigned I = 0, E = Values.size(); I < E; ++I) {
3863  RecordVal *Arg = ArgsRec->getValue(TArgs[I]);
3864  RecTy *ArgType = Arg->getType();
3865  auto *Value = Values[I];
3866 
3867  if (TypedInit *ArgValue = dyn_cast<TypedInit>(Value)) {
3868  auto *CastValue = ArgValue->getCastTo(ArgType);
3869  if (CastValue) {
3870  assert((!isa<TypedInit>(CastValue) ||
3871  cast<TypedInit>(CastValue)->getType()->typeIsA(ArgType)) &&
3872  "result of template arg value cast has wrong type");
3873  Values[I] = CastValue;
3874  } else {
3875  PrintFatalError(Loc,
3876  "Value specified for template argument '" +
3877  Arg->getNameInitAsString() + "' (#" + Twine(I) +
3878  ") is of type " + ArgValue->getType()->getAsString() +
3879  "; expected type " + ArgType->getAsString() + ": " +
3880  ArgValue->getAsString());
3881  }
3882  }
3883  }
3884 
3885  return false;
3886 }
3887 
3888 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3890  if (Loop)
3891  Loop->dump();
3892  if (Rec)
3893  Rec->dump();
3894 }
3895 
3897  errs() << "foreach " << IterVar->getAsString() << " = "
3898  << ListValue->getAsString() << " in {\n";
3899 
3900  for (const auto &E : Entries)
3901  E.dump();
3902 
3903  errs() << "}\n";
3904 }
3905 
3907  errs() << "Record:\n";
3908  Rec.dump();
3909 
3910  errs() << "Defs:\n";
3911  for (const auto &E : Entries)
3912  E.dump();
3913 }
3914 #endif
llvm::tgtok::XFilter
@ XFilter
Definition: TGLexer.h:57
llvm::FieldInit::Fold
Init * Fold(Record *CurRec) const
Definition: Record.cpp:2099
i
i
Definition: README.txt:29
llvm::TernOpInit::IF
@ IF
Definition: Record.h:914
llvm::Record::isSubClassOf
bool isSubClassOf(const Record *R) const
Definition: Record.h:1754
llvm::BinOpInit::XOR
@ XOR
Definition: Record.h:843
llvm::SMRange::Start
SMLoc Start
Definition: SMLoc.h:50
llvm::ForeachLoop::dump
void dump() const
Definition: TGParser.cpp:3896
LLVM_DUMP_METHOD
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:492
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::tgtok::Def
@ Def
Definition: TGLexer.h:50
llvm::StringRecTy
'string' - Represent an string value
Definition: Record.h:167
llvm::RecordKeeper
Definition: Record.h:1888
llvm::Operator
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition: Operator.h:32
llvm::TGParser::TokError
bool TokError(const Twine &Msg) const
Definition: TGParser.h:182
llvm::RecordVal::FK_Normal
@ FK_Normal
Definition: Record.h:1490
llvm::Init::getCastTo
virtual Init * getCastTo(RecTy *Ty) const =0
If this value is convertible to type Ty, return a value whose type is Ty, generating a !...
llvm::TernOpInit::FOREACH
@ FOREACH
Definition: Record.h:914
llvm::cl::Prefix
@ Prefix
Definition: CommandLine.h:161
llvm::BinOpInit::get
static BinOpInit * get(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type)
Definition: Record.cpp:943
llvm::tgtok::XForEach
@ XForEach
Definition: TGLexer.h:57
llvm::Loop
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:547
llvm::tgtok::Field
@ Field
Definition: TGLexer.h:51
llvm::BinOpInit::AND
@ AND
Definition: Record.h:841
Loops
Hexagon Hardware Loops
Definition: HexagonHardwareLoops.cpp:374
llvm::MultiClass::Rec
Record Rec
Definition: TGParser.h:121
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1199
llvm::tgtok::Code
@ Code
Definition: TGLexer.h:50
llvm::tgtok::paste
@ paste
Definition: TGLexer.h:45
llvm::tgtok::XXOR
@ XXOR
Definition: TGLexer.h:55
checkConcrete
static void checkConcrete(Record &R)
Definition: TGParser.cpp:91
llvm::DagRecTy::get
static DagRecTy * get(RecordKeeper &RK)
Definition: Record.cpp:198
ErrorHandling.h
llvm::tgtok::l_brace
@ l_brace
Definition: TGLexer.h:39
llvm::Record::getSuperClasses
ArrayRef< std::pair< Record *, SMRange > > getSuperClasses() const
Definition: Record.h:1688
llvm::tgtok::XStrConcat
@ XStrConcat
Definition: TGLexer.h:56
llvm::tgtok::l_paren
@ l_paren
Definition: TGLexer.h:40
llvm::DagInit
(v a, b) - Represent a DAG tree value.
Definition: Record.h:1390
llvm::FieldInit::get
static FieldInit * get(Init *R, StringInit *FN)
Definition: Record.cpp:2078
llvm::tgtok::VarName
@ VarName
Definition: TGLexer.h:72
llvm::tgtok::Defm
@ Defm
Definition: TGLexer.h:50
llvm::tgtok::comma
@ comma
Definition: TGLexer.h:43
llvm::BinOpInit::DIV
@ DIV
Definition: Record.h:840
llvm::BinOpInit::OR
@ OR
Definition: Record.h:842
llvm::Record::addAssertion
void addAssertion(SMLoc Loc, Init *Condition, Init *Message)
Definition: Record.h:1743
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::BinOpInit
!op (X, Y) - Combine two inits.
Definition: Record.h:834
llvm::tgtok::Bits
@ Bits
Definition: TGLexer.h:50
llvm::BinOpInit::SRA
@ SRA
Definition: Record.h:845
llvm::tgtok::XGe
@ XGe
Definition: TGLexer.h:58
llvm::DagRecTy
'dag' - Represent a dag fragment
Definition: Record.h:210
llvm::tgtok::XSUB
@ XSUB
Definition: TGLexer.h:55
llvm::tgtok::l_square
@ l_square
Definition: TGLexer.h:38
llvm::SubMultiClassReference::TemplateArgs
SmallVector< Init *, 4 > TemplateArgs
Definition: TGParser.cpp:50
llvm::BinOpInit::GT
@ GT
Definition: Record.h:857
llvm::tgtok::FalseVal
@ FalseVal
Definition: TGLexer.h:62
llvm::TGParser::PushLocalScope
TGLocalVarScope * PushLocalScope()
Definition: TGParser.h:189
llvm::TernOpInit::SUBST
@ SUBST
Definition: Record.h:914
llvm::ListInit::get
static ListInit * get(ArrayRef< Init * > Range, RecTy *EltTy)
Definition: Record.cpp:624
llvm::max
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:337
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:891
llvm::Record::checkUnusedTemplateArgs
void checkUnusedTemplateArgs()
Definition: Record.cpp:2831
llvm::FoldOpInit::get
static FoldOpInit * get(Init *Start, Init *List, Init *A, Init *B, Init *Expr, RecTy *Type)
Definition: Record.cpp:1574
llvm::Init::getBit
virtual Init * getBit(unsigned Bit) const =0
Get the Init value of the specified bit.
RHS
Value * RHS
Definition: X86PartialReduction.cpp:76
llvm::tgtok::XHead
@ XHead
Definition: TGLexer.h:57
llvm::BinOpInit::getStrConcat
static Init * getStrConcat(Init *lhs, Init *rhs)
Definition: Record.cpp:1013
llvm::tgtok::BinaryIntVal
@ BinaryIntVal
Definition: TGLexer.h:69
llvm::TernOpInit::SUBSTR
@ SUBSTR
Definition: Record.h:914
llvm::VarInit
'Opcode' - Represent a reference to an entire variable object.
Definition: Record.h:1155
llvm::RecordVal::FK_NonconcreteOK
@ FK_NonconcreteOK
Definition: Record.h:1491
llvm::ArrayRef::empty
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:159
llvm::RecTy
Definition: Record.h:57
llvm::RISCVFenceField::R
@ R
Definition: RISCVBaseInfo.h:265
llvm::TernOpInit::Fold
Init * Fold(Record *CurRec) const
Definition: Record.cpp:1379
llvm::TGLexer::Lex
tgtok::TokKind Lex()
Definition: TGLexer.h:107
llvm::CondOpInit::Fold
Init * Fold(Record *CurRec) const
Definition: Record.cpp:2193
llvm::SubMultiClassReference::isInvalid
bool isInvalid() const
Definition: TGParser.cpp:54
llvm::BitsRecTy
'bits<n>' - Represent a fixed number of bits
Definition: Record.h:126
llvm::tgtok::XMUL
@ XMUL
Definition: TGLexer.h:55
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:187
llvm::Record::getTemplateArgs
ArrayRef< Init * > getTemplateArgs() const
Definition: Record.h:1680
llvm::tgtok::Dag
@ Dag
Definition: TGLexer.h:50
LHS
Value * LHS
Definition: X86PartialReduction.cpp:75
llvm::tgtok::XFind
@ XFind
Definition: TGLexer.h:56
llvm::tgtok::Then
@ Then
Definition: TGLexer.h:52
llvm::RecordVal::addReferenceLoc
void addReferenceLoc(SMRange Loc)
Add a reference to this record value.
Definition: Record.h:1552
llvm::Record::resolveReferences
void resolveReferences(Init *NewName=nullptr)
If there are any field references that refer to fields that have been filled in, we can propagate the...
Definition: Record.cpp:2576
llvm::SubMultiClassReference::RefRange
SMRange RefRange
Definition: TGParser.cpp:48
llvm::RecordsEntry::dump
void dump() const
Definition: TGParser.cpp:3889
llvm::tgtok::question
@ question
Definition: TGLexer.h:44
QualifyName
static Init * QualifyName(Record &CurRec, MultiClass *CurMultiClass, Init *Name, StringRef Scoper)
Return an Init with a qualifier prefix referring to CurRec's name.
Definition: TGParser.cpp:115
llvm::UnOpInit::GETDAGOP
@ GETDAGOP
Definition: Record.h:787
llvm::tgtok::XOR
@ XOR
Definition: TGLexer.h:55
llvm::MultiClass::dump
void dump() const
Definition: TGParser.cpp:3906
llvm::PPCISD::SC
@ SC
CHAIN = SC CHAIN, Imm128 - System call.
Definition: PPCISelLowering.h:419
llvm::SMLoc
Represents a location in source code.
Definition: SMLoc.h:23
llvm::ModRefInfo::Ref
@ Ref
The access may reference the value stored in memory.
llvm::BitInit::get
static BitInit * get(RecordKeeper &RK, bool V)
Definition: Record.cpp:372
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Operation
PowerPC Reduce CR logical Operation
Definition: PPCReduceCRLogicals.cpp:735
llvm::CheckAssert
void CheckAssert(SMLoc Loc, Init *Condition, Init *Message)
Definition: Error.cpp:159
llvm::UnOpInit::UnaryOp
UnaryOp
Definition: Record.h:787
llvm::RecordsEntry
RecordsEntry - Holds exactly one of a Record, ForeachLoop, or AssertionInfo.
Definition: TGParser.h:41
llvm::ARM_PROC::A
@ A
Definition: ARMBaseInfo.h:34
Twine.h
llvm::BinOpInit::BinaryOp
BinaryOp
Definition: Record.h:836
llvm::ListRecTy::getElementType
RecTy * getElementType() const
Definition: Record.h:200
llvm::TGParser::Error
bool Error(SMLoc L, const Twine &Msg) const
Definition: TGParser.h:178
llvm::RecordKeeper::addDef
void addDef(std::unique_ptr< Record > R)
Definition: Record.h:1942
llvm::tgtok::Assert
@ Assert
Definition: TGLexer.h:50
llvm::TGLexer::getCurBinaryIntVal
std::pair< int64_t, unsigned > getCurBinaryIntVal() const
Definition: TGLexer.h:127
llvm::tgtok::XIsA
@ XIsA
Definition: TGLexer.h:58
llvm::ms_demangle::QualifierMangleMode::Result
@ Result
llvm::UnOpInit::CAST
@ CAST
Definition: Record.h:787
llvm::tgtok::XCast
@ XCast
Definition: TGLexer.h:57
llvm::BinOpInit::ADD
@ ADD
Definition: Record.h:837
llvm::tgtok::XListConcat
@ XListConcat
Definition: TGLexer.h:56
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::Record::addTemplateArg
void addTemplateArg(Init *Name)
Definition: Record.h:1720
llvm::SubClassReference::SubClassReference
SubClassReference()
Definition: TGParser.cpp:42
llvm::TypedInit::getType
RecTy * getType() const
Get the type of the Init as a RecTy.
Definition: Record.h:438
llvm::PrintFatalError
void PrintFatalError(const Twine &Msg)
Definition: Error.cpp:125
llvm::Record::getNameInitAsString
std::string getNameInitAsString() const
Definition: Record.h:1650
llvm::BinOpInit::getListConcat
static Init * getListConcat(TypedInit *lhs, Init *rhs)
Definition: Record.cpp:1030
llvm::tgtok::If
@ If
Definition: TGLexer.h:51
llvm::tgtok::equal
@ equal
Definition: TGLexer.h:44
llvm::HasReferenceResolver
Do not resolve anything, but keep track of whether a given variable was referenced.
Definition: Record.h:2239
llvm::BitsInit::getBit
Init * getBit(unsigned Bit) const override
Get the Init value of the specified bit.
Definition: Record.h:571
llvm::TernOpInit::get
static TernOpInit * get(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs, RecTy *Type)
Definition: Record.cpp:1280
llvm::RecordVal::getValue
Init * getValue() const
Get the value of the field as an Init.
Definition: Record.h:1543
llvm::tgtok::XNOT
@ XNOT
Definition: TGLexer.h:55
llvm::CondOpInit::get
static CondOpInit * get(ArrayRef< Init * > C, ArrayRef< Init * > V, RecTy *Type)
Definition: Record.cpp:2145
llvm::SubMultiClassReference::dump
void dump() const
Definition: TGParser.cpp:59
llvm::SubClassReference::RefRange
SMRange RefRange
Definition: TGParser.cpp:38
llvm::tgtok::XGt
@ XGt
Definition: TGLexer.h:58
llvm::ForeachLoop
ForeachLoop - Record the iteration state associated with a for loop.
Definition: TGParser.h:62
llvm::TernOpInit::DAG
@ DAG
Definition: Record.h:914
llvm::Record::dump
void dump() const
Definition: Record.cpp:2584
llvm::tgtok::XSRA
@ XSRA
Definition: TGLexer.h:55
llvm::BinOpInit::LISTSPLAT
@ LISTSPLAT
Definition: Record.h:848
llvm::tgtok::Foreach
@ Foreach
Definition: TGLexer.h:51
llvm::StringInit::SF_Code
@ SF_Code
Definition: Record.h:642
llvm::RecordVal::setUsed
void setUsed(bool Used)
Whether this value is used.
Definition: Record.h:1559
llvm::TernOpInit::TernaryOp
TernaryOp
Definition: Record.h:914
llvm::SubMultiClassReference
Definition: TGParser.cpp:47
llvm::BinOpInit::NE
@ NE
Definition: Record.h:853
llvm::tgtok::XInterleave
@ XInterleave
Definition: TGLexer.h:56
llvm::tgtok::In
@ In
Definition: TGLexer.h:51
llvm::RecordVal::setValue
bool setValue(Init *V)
Set the value of the field from an Init.
Definition: Record.cpp:2397
llvm::tgtok::Defset
@ Defset
Definition: TGLexer.h:50
llvm::SubMultiClassReference::MC
MultiClass * MC
Definition: TGParser.cpp:49
VI
@ VI
Definition: SIInstrInfo.cpp:7967
llvm::tgtok::XIf
@ XIf
Definition: TGLexer.h:57
llvm::IsAOpInit::get
static IsAOpInit * get(RecTy *CheckType, Init *Expr)
Definition: Record.cpp:1639
llvm::SubClassReference::isInvalid
bool isInvalid() const
Definition: TGParser.cpp:44
llvm::IntInit
'7' - Represent an initialization by a literal integer value.
Definition: Record.h:578
llvm::ListInit
[AL, AH, CL] - Represent a list of defs
Definition: Record.h:693
uint64_t
llvm::tgtok::XEq
@ XEq
Definition: TGLexer.h:58
llvm::TGLexer::getCode
tgtok::TokKind getCode() const
Definition: TGLexer.h:115
llvm::Record::updateClassLoc
void updateClassLoc(SMLoc Loc)
Definition: Record.cpp:2459
llvm::tgtok::Int
@ Int
Definition: TGLexer.h:51
llvm::tgtok::XDIV
@ XDIV
Definition: TGLexer.h:55
llvm::MapResolver
Resolve arbitrary mappings.
Definition: Record.h:2161
llvm::BitsInit::get
static BitsInit * get(RecordKeeper &RK, ArrayRef< Init * > Range)
Definition: Record.cpp:400
llvm::tgtok::XExists
@ XExists
Definition: TGLexer.h:59
llvm::ForeachLoop::ListValue
Init * ListValue
Definition: TGParser.h:65
llvm::Record::getValues
ArrayRef< RecordVal > getValues() const
Definition: Record.h:1684
llvm::DefInit
AL - Represent a reference to a 'def' in the description.
Definition: Record.h:1262
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::numbers::e
constexpr double e
Definition: MathExtras.h:53
llvm::tgtok::ElseKW
@ ElseKW
Definition: TGLexer.h:50
llvm::VarInit::get
static VarInit * get(StringRef VN, RecTy *T)
Definition: Record.cpp:1842
llvm::RecordRecTy
'[classname]' - Type of record values that have zero or more superclasses.
Definition: Record.h:229
llvm::SubMultiClassReference::SubMultiClassReference
SubMultiClassReference()
Definition: TGParser.cpp:52
llvm::tgtok::r_square
@ r_square
Definition: TGLexer.h:38
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::Record::addValue
void addValue(const RecordVal &RV)
Definition: Record.h:1725
llvm::SubClassReference
Definition: TGParser.cpp:37
StringExtras.h
llvm::MultiClass
Definition: TGParser.h:120
llvm::tgtok::Defvar
@ Defvar
Definition: TGLexer.h:50
llvm::Record::isClass
bool isClass() const
Definition: Record.h:1678
llvm::IntInit::getValue
int64_t getValue() const
Definition: Record.h:594
llvm::SubClassReference::TemplateArgs
SmallVector< Init *, 4 > TemplateArgs
Definition: TGParser.cpp:40
llvm::tgtok::XSubstr
@ XSubstr
Definition: TGLexer.h:56
llvm::resolveTypes
RecTy * resolveTypes(RecTy *T1, RecTy *T2)
Find a common type that T1 and T2 convert to.
Definition: Record.cpp:318
llvm::BinOpInit::GE
@ GE
Definition: Record.h:856
llvm::pdb::PDB_MemoryType::Stack
@ Stack
llvm::BinOpInit::SHL
@ SHL
Definition: Record.h:844
llvm::UnOpInit::LOG2
@ LOG2
Definition: Record.h:787
llvm::ExistsOpInit::get
static ExistsOpInit * get(RecTy *CheckType, Init *Expr)
Definition: Record.cpp:1701
llvm::tgtok::r_paren
@ r_paren
Definition: TGLexer.h:40
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::ListRecTy
'list<Ty>' - Represent a list of element values, all of which must be of the specified type.
Definition: Record.h:186
llvm::tgtok::XConcat
@ XConcat
Definition: TGLexer.h:55
llvm::Init::resolveReferences
virtual Init * resolveReferences(Resolver &R) const
This function is used by classes that refer to other variables which may not be defined at the time t...
Definition: Record.h:407
llvm::BinOpInit::LISTCONCAT
@ LISTCONCAT
Definition: Record.h:847
llvm::Init::getAsUnquotedString
virtual std::string getAsUnquotedString() const
Convert this value to a literal form, without adding quotes around a string.
Definition: Record.h:364
llvm::BinOpInit::EQ
@ EQ
Definition: Record.h:852
llvm::TGLocalVarScope
Definition: TGParser.h:80
Mode
SI Whole Quad Mode
Definition: SIWholeQuadMode.cpp:264
llvm::IntInit::get
static IntInit * get(RecordKeeper &RK, int64_t V)
Definition: Record.cpp:517
llvm::Record
Definition: Record.h:1573
QualifiedNameOfImplicitName
static Init * QualifiedNameOfImplicitName(Record &Rec, MultiClass *MC=nullptr)
Return the qualified version of the implicit 'NAME' template argument.
Definition: TGParser.cpp:133
llvm::tgtok::XLe
@ XLe
Definition: TGLexer.h:58
llvm::RecordKeeper::getClass
Record * getClass(StringRef Name) const
Get the class with the specified name.
Definition: Record.h:1912
llvm::BinOpInit::SUB
@ SUB
Definition: Record.h:838
llvm::Init::getAsString
virtual std::string getAsString() const =0
Convert this value to a literal form.
llvm::UnOpInit::NOT
@ NOT
Definition: Record.h:787
llvm::VarInit::getAsString
std::string getAsString() const override
Convert this value to a literal form.
Definition: Record.h:1188
llvm::Record::getType
RecordRecTy * getType()
Definition: Record.cpp:2475
getType
static M68kRelType getType(unsigned Kind, MCSymbolRefExpr::VariantKind &Modifier, bool &IsPCRel)
Definition: M68kELFObjectWriter.cpp:48
llvm::RecordsEntry::Rec
std::unique_ptr< Record > Rec
Definition: TGParser.h:42
llvm::StringInit
"foo" - Represent an initialization by a string value.
Definition: Record.h:638
llvm::ForeachLoop::Entries
std::vector< RecordsEntry > Entries
Definition: TGParser.h:66
llvm::tgtok::XCond
@ XCond
Definition: TGLexer.h:58
llvm::BinOpInit::INTERLEAVE
@ INTERLEAVE
Definition: Record.h:850
llvm::Sched::Source
@ Source
Definition: TargetLowering.h:99
llvm::ArrayRef< unsigned >
llvm::Record::addSuperClass
void addSuperClass(Record *R, SMRange Range)
Definition: Record.h:1773
None.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::BitsInit::getNumBits
unsigned getNumBits() const
Definition: Record.h:549
llvm::tgtok::String
@ String
Definition: TGLexer.h:52
llvm::RecordKeeper::getGlobal
Init * getGlobal(StringRef Name) const
Get the Init value of the specified global variable.
Definition: Record.h:1924
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
llvm::tgtok::XADD
@ XADD
Definition: TGLexer.h:55
llvm::RecordVal::getNameInit
Init * getNameInit() const
Get the name of the field as an Init.
Definition: Record.h:1516
llvm::Record::getNameInit
Init * getNameInit() const
Definition: Record.h:1646
Compiler.h
llvm::ListRecTy::get
static ListRecTy * get(RecTy *T)
Definition: Record.h:199
llvm::tgtok::XSRL
@ XSRL
Definition: TGLexer.h:55
llvm::ListInit::empty
bool empty() const
Definition: Record.h:751
llvm::tgtok::XGetDagOp
@ XGetDagOp
Definition: TGLexer.h:58
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::LetRecord
Definition: TGParser.h:29
llvm::RecordVal::FK_TemplateArg
@ FK_TemplateArg
Definition: Record.h:1492
llvm::Init
Definition: Record.h:281
llvm::RecTy::getListTy
ListRecTy * getListTy()
Returns the type representing list<thistype>.
Definition: Record.cpp:108
llvm::RecordVal::getName
StringRef getName() const
Get the name of the field as a StringRef.
Definition: Record.cpp:2378
llvm::AMDGPU::SendMsg::Msg
const CustomOperand< const MCSubtargetInfo & > Msg[]
Definition: AMDGPUAsmUtils.cpp:39
llvm::tgtok::StrVal
@ StrVal
Definition: TGLexer.h:72
llvm::tgtok::dot
@ dot
Definition: TGLexer.h:43
llvm::tgtok::IntVal
@ IntVal
Definition: TGLexer.h:65
llvm::BitsRecTy::get
static BitsRecTy * get(RecordKeeper &RK, unsigned Sz)
Definition: Record.cpp:133
llvm::DagInit::get
static DagInit * get(Init *V, StringInit *VN, ArrayRef< Init * > ArgRange, ArrayRef< StringInit * > NameRange)
Definition: Record.cpp:2270
llvm::UnOpInit::SIZE
@ SIZE
Definition: Record.h:787
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
llvm::tgtok::r_brace
@ r_brace
Definition: TGLexer.h:39
llvm::RecordVal::getType
RecTy * getType() const
Get the type of the field value as a RecTy.
Definition: Record.h:1537
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
llvm::UnOpInit::get
static UnOpInit * get(UnaryOp opc, Init *lhs, RecTy *Type)
Definition: Record.cpp:760
llvm::None
constexpr std::nullopt_t None
Definition: None.h:27
llvm::tgtok::Eof
@ Eof
Definition: TGLexer.h:34
llvm::StringRecTy::get
static StringRecTy * get(RecordKeeper &RK)
Definition: Record.cpp:169
llvm::BinOpInit::SRL
@ SRL
Definition: Record.h:846
get
Should compile to something r4 addze r3 instead we get
Definition: README.txt:24
llvm::TernOpInit::FILTER
@ FILTER
Definition: Record.h:914
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::TGLexer::getCurIntVal
int64_t getCurIntVal() const
Definition: TGLexer.h:123
llvm::Record::getRecords
RecordKeeper & getRecords() const
Definition: Record.h:1794
llvm::VarInit::getNameInit
Init * getNameInit() const
Definition: Record.h:1173
llvm::Record::getValue
const RecordVal * getValue(const Init *Name) const
Definition: Record.h:1702
llvm::StringInit::getValue
StringRef getValue() const
Definition: Record.h:667
llvm::tgtok::XAND
@ XAND
Definition: TGLexer.h:55
Casting.h
llvm::tgtok::XListSplat
@ XListSplat
Definition: TGLexer.h:56
llvm::tgtok::XSHL
@ XSHL
Definition: TGLexer.h:56
llvm::TargetStackID::Default
@ Default
Definition: TargetFrameLowering.h:28
llvm::Loop::dump
void dump() const
Definition: LoopInfo.cpp:667
llvm::ForeachLoop::IterVar
VarInit * IterVar
Definition: TGParser.h:64
llvm::UnOpInit::TAIL
@ TAIL
Definition: Record.h:787
llvm::ListRecTy::typeIsConvertibleTo
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of 'this' type can be converted to the specified type.
Definition: Record.cpp:186
llvm::UnOpInit::HEAD
@ HEAD
Definition: Record.h:787
llvm::tgtok::XFoldl
@ XFoldl
Definition: TGLexer.h:57
TGParser.h
llvm::tgtok::XSubst
@ XSubst
Definition: TGLexer.h:57
llvm::tgtok::Class
@ Class
Definition: TGLexer.h:50
llvm::TGLexer::getCurStrVal
const std::string & getCurStrVal() const
Definition: TGLexer.h:117
llvm::tgtok::XDag
@ XDag
Definition: TGLexer.h:58
llvm::Record::isTemplateArg
bool isTemplateArg(Init *Name) const
Definition: Record.h:1698
llvm::tgtok::greater
@ greater
Definition: TGLexer.h:41
llvm::RecTy::typeIsConvertibleTo
virtual bool typeIsConvertibleTo(const RecTy *RHS) const
Return true if all values of 'this' type can be converted to the specified type.
Definition: Record.cpp:114
llvm::X86II::TA
@ TA
Definition: X86BaseInfo.h:813
isObjectStart
static bool isObjectStart(tgtok::TokKind K)
isObjectStart - Return true if this is a valid first token for a statement.
Definition: TGParser.cpp:516
llvm::BinOpInit::LT
@ LT
Definition: Record.h:855
llvm::TernOpInit::FIND
@ FIND
Definition: Record.h:914
llvm::RecTy::getAsString
virtual std::string getAsString() const =0
llvm::MultiClass::Entries
std::vector< RecordsEntry > Entries
Definition: TGParser.h:122
llvm::BinOpInit::SETDAGOP
@ SETDAGOP
Definition: Record.h:858
checkBitsConcrete
static bool checkBitsConcrete(Record &R, const RecordVal &RV)
Definition: TGParser.cpp:72
SmallVector.h
llvm::tgtok::CodeFragment
@ CodeFragment
Definition: TGLexer.h:72
llvm::RecordRecTy::get
static RecordRecTy * get(RecordKeeper &RK, ArrayRef< Record * > Classes)
Get the record type with the given non-redundant list of superclasses.
Definition: Record.cpp:213
llvm::tgtok::XSize
@ XSize
Definition: TGLexer.h:57
llvm::tgtok::XNe
@ XNe
Definition: TGLexer.h:58
llvm::PrintError
void PrintError(const Twine &Msg)
Definition: Error.cpp:101
llvm::tgtok::less
@ less
Definition: TGLexer.h:41
llvm::DefsetRecord
Definition: TGParser.h:74
llvm::FoldOpInit::Fold
Init * Fold(Record *CurRec) const
Definition: Record.cpp:1593
llvm::SMRange
Represents a range in source code.
Definition: SMLoc.h:48
List
const NodeList & List
Definition: RDFGraph.cpp:199
llvm::tgtok::MultiClass
@ MultiClass
Definition: TGLexer.h:51
llvm::Record::appendAssertions
void appendAssertions(const Record *Rec)
Definition: Record.h:1747
llvm::tgtok::TokKind
TokKind
Definition: TGLexer.h:32
llvm::SmallVectorImpl::pop_back_val
T pop_back_val()
Definition: SmallVector.h:677
llvm::BinOpInit::STRCONCAT
@ STRCONCAT
Definition: Record.h:849
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:164
llvm::RecordKeeper::getNewAnonymousName
Init * getNewAnonymousName()
GetNewAnonymousName - Generate a unique anonymous name that can be used as an identifier.
Definition: Record.cpp:2861
llvm::PrintNote
void PrintNote(const Twine &Msg)
Definition: Error.cpp:45
llvm::SubClassReference::Rec
Record * Rec
Definition: TGParser.cpp:39
llvm::tgtok::Bit
@ Bit
Definition: TGLexer.h:50
llvm::UnOpInit::EMPTY
@ EMPTY
Definition: Record.h:787
llvm::BinOpInit::MUL
@ MUL
Definition: Record.h:839
llvm::logicalview::LVWarningKind::Ranges
@ Ranges
llvm::TGLexer::getLocRange
SMRange getLocRange() const
Definition: TGLexer.cpp:66
llvm::SmallVectorImpl< unsigned >
llvm::reverse
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:485
DenseMapInfo.h
llvm::tgtok::XSetDagOp
@ XSetDagOp
Definition: TGLexer.h:58
llvm::BinOpInit::LE
@ LE
Definition: Record.h:854
llvm::RecordKeeper::addClass
void addClass(std::unique_ptr< Record > R)
Definition: Record.h:1935
llvm::orc::SymbolState::Resolved
@ Resolved
Queried, materialization begun.
llvm::tgtok::XLOG2
@ XLOG2
Definition: TGLexer.h:55
llvm::BinOpInit::CONCAT
@ CONCAT
Definition: Record.h:851
llvm::VarDefInit::get
static VarDefInit * get(Record *Class, ArrayRef< Init * > Args)
Definition: Record.cpp:1959
llvm::BitsInit
'{ a, b, c }' - Represents an initializer for a BitsRecTy value.
Definition: Record.h:527
llvm::TGLexer::getLoc
SMLoc getLoc() const
Definition: TGLexer.cpp:62
llvm::tgtok::List
@ List
Definition: TGLexer.h:51
TemplateArgs
Definition: ItaniumDemangle.h:1383
llvm::OptimizedStructLayoutField
A field in a structure.
Definition: OptimizedStructLayout.h:45
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:394
llvm::VarDefInit::Fold
Init * Fold() const
Definition: Record.cpp:2054
llvm::RecordKeeper::getDef
Record * getDef(StringRef Name) const
Get the concrete record with the specified name.
Definition: Record.h:1918
llvm::RecordKeeper::addExtraGlobal
void addExtraGlobal(StringRef Name, Init *I)
Definition: Record.h:1949
raw_ostream.h
llvm::RecordVal
This class represents a field in a record, including its name, type, value, and source location.
Definition: Record.h:1485
llvm::tgtok::XTail
@ XTail
Definition: TGLexer.h:57
llvm::tgtok::semi
@ semi
Definition: TGLexer.h:42
llvm::tgtok::TrueVal
@ TrueVal
Definition: TGLexer.h:62
llvm::BitRecTy::get
static BitRecTy * get(RecordKeeper &RK)
Definition: Record.cpp:121
llvm::tgtok::minus
@ minus
Definition: TGLexer.h:37
llvm::tgtok::XLt
@ XLt
Definition: TGLexer.h:58
llvm::IntRecTy::get
static IntRecTy * get(RecordKeeper &RK)
Definition: Record.cpp:160
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::tgtok::colon
@ colon
Definition: TGLexer.h:42
llvm::ListInit::getElement
Init * getElement(unsigned i) const
Definition: Record.h:718
llvm::TGParser::PopLocalScope
void PopLocalScope(TGLocalVarScope *ExpectedStackTop)
Definition: TGParser.h:196
llvm::StringInit::get
static StringInit * get(RecordKeeper &RK, StringRef, StringFormat Fmt=SF_String)
Definition: Record.cpp:597
llvm::tgtok::dotdotdot
@ dotdotdot
Definition: TGLexer.h:46
llvm::TGParser::ParseFile
bool ParseFile()
ParseFile - Main entrypoint for parsing a tblgen file.
Definition: TGParser.cpp:3842
llvm::tgtok::Id
@ Id
Definition: TGLexer.h:72
llvm::UnsetInit::get
static UnsetInit * get(RecordKeeper &RK)
Get the singleton unset Init.
Definition: Record.cpp:360
llvm::tgtok::XEmpty
@ XEmpty
Definition: TGLexer.h:57
llvm::tgtok::Let
@ Let
Definition: TGLexer.h:51
llvm::TypedInit
This is the common superclass of types that have a specific, explicit type, stored in ValueTy.
Definition: Record.h:421