LCOV - code coverage report
Current view: top level - lib/TableGen - TGParser.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 927 1330 69.7 %
Date: 2018-10-20 13:21:21 Functions: 49 49 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // Implement the Parser for TableGen.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "TGParser.h"
      15             : #include "llvm/ADT/None.h"
      16             : #include "llvm/ADT/STLExtras.h"
      17             : #include "llvm/ADT/SmallVector.h"
      18             : #include "llvm/ADT/StringExtras.h"
      19             : #include "llvm/Config/llvm-config.h"
      20             : #include "llvm/Support/Casting.h"
      21             : #include "llvm/Support/Compiler.h"
      22             : #include "llvm/Support/ErrorHandling.h"
      23             : #include "llvm/Support/raw_ostream.h"
      24             : #include "llvm/TableGen/Record.h"
      25             : #include <algorithm>
      26             : #include <cassert>
      27             : #include <cstdint>
      28             : 
      29             : using namespace llvm;
      30             : 
      31             : //===----------------------------------------------------------------------===//
      32             : // Support Code for the Semantic Actions.
      33             : //===----------------------------------------------------------------------===//
      34             : 
      35             : namespace llvm {
      36             : 
      37      806348 : struct SubClassReference {
      38             :   SMRange RefRange;
      39             :   Record *Rec;
      40             :   SmallVector<Init*, 4> TemplateArgs;
      41             : 
      42     3259066 :   SubClassReference() : Rec(nullptr) {}
      43             : 
      44             :   bool isInvalid() const { return Rec == nullptr; }
      45             : };
      46             : 
      47          91 : struct SubMultiClassReference {
      48             :   SMRange RefRange;
      49             :   MultiClass *MC;
      50             :   SmallVector<Init*, 4> TemplateArgs;
      51             : 
      52        2195 :   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)
      59             : LLVM_DUMP_METHOD void SubMultiClassReference::dump() const {
      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     6039114 : static bool checkBitsConcrete(Record &R, const RecordVal &RV) {
      73     6039114 :   BitsInit *BV = cast<BitsInit>(RV.getValue());
      74    66589365 :   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      139871 :       if (auto VI = dyn_cast<VarInit>(VBI->getBitVar())) {
      79      279740 :         if (R.getValue(VI->getName()))
      80             :           IsReference = true;
      81             :       }
      82    60410382 :     } else if (isa<VarInit>(Bit)) {
      83             :       IsReference = true;
      84             :     }
      85    60410381 :     if (!(IsReference || Bit->isConcrete()))
      86             :       return false;
      87             :   }
      88             :   return true;
      89             : }
      90             : 
      91     2944419 : static void checkConcrete(Record &R) {
      92    82311753 :   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    79367334 :     if (RV.getPrefix())
      98             :       continue;
      99             : 
     100    71912173 :     if (Init *V = RV.getValue()) {
     101    71912165 :       bool Ok = isa<BitsInit>(V) ? checkBitsConcrete(R, RV) : V->isConcrete();
     102    71912165 :       if (!Ok) {
     103           2 :         PrintError(R.getLoc(),
     104           4 :                    Twine("Initializer of '") + RV.getNameInitAsString() +
     105           2 :                    "' in '" + R.getNameInitAsString() +
     106           2 :                    "' could not be fully resolved: " +
     107           6 :                    RV.getValue()->getAsString());
     108             :       }
     109             :     }
     110             :   }
     111     2944419 : }
     112             : 
     113             : /// Return an Init with a qualifier prefix referring
     114             : /// to CurRec's name.
     115     6016955 : static Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
     116             :                         Init *Name, StringRef Scoper) {
     117             :   Init *NewName =
     118     6016955 :       BinOpInit::getStrConcat(CurRec.getNameInit(), StringInit::get(Scoper));
     119     6016955 :   NewName = BinOpInit::getStrConcat(NewName, Name);
     120     6016955 :   if (CurMultiClass && Scoper != "::") {
     121           0 :     Init *Prefix = BinOpInit::getStrConcat(CurMultiClass->Rec.getNameInit(),
     122           0 :                                            StringInit::get("::"));
     123           0 :     NewName = BinOpInit::getStrConcat(Prefix, NewName);
     124             :   }
     125             : 
     126             :   if (BinOpInit *BinOp = dyn_cast<BinOpInit>(NewName))
     127           0 :     NewName = BinOp->Fold(&CurRec);
     128     6016955 :   return NewName;
     129             : }
     130             : 
     131             : /// Return the qualified version of the implicit 'NAME' template argument.
     132     3792349 : static Init *QualifiedNameOfImplicitName(Record &Rec,
     133             :                                          MultiClass *MC = nullptr) {
     134     7311967 :   return QualifyName(Rec, MC, StringInit::get("NAME"), MC ? "::" : ":");
     135             : }
     136             : 
     137             : static Init *QualifiedNameOfImplicitName(MultiClass *MC) {
     138      270536 :   return QualifiedNameOfImplicitName(MC->Rec, MC);
     139             : }
     140             : 
     141    48594930 : bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
     142    48594930 :   if (!CurRec)
     143      109067 :     CurRec = &CurMultiClass->Rec;
     144             : 
     145    97189860 :   if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
     146             :     // The value already exists in the class, treat this as a set.
     147      776758 :     if (ERV->setValue(RV.getValue()))
     148           0 :       return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
     149           0 :                    RV.getType()->getAsString() + "' is incompatible with " +
     150           0 :                    "previous definition of type '" +
     151           0 :                    ERV->getType()->getAsString() + "'");
     152             :   } else {
     153             :     CurRec->addValue(RV);
     154             :   }
     155             :   return false;
     156             : }
     157             : 
     158             : /// SetValue -
     159             : /// Return true on error, false on success.
     160     9065759 : bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
     161             :                         ArrayRef<unsigned> BitList, Init *V,
     162             :                         bool AllowSelfAssignment) {
     163     9065759 :   if (!V) return false;
     164             : 
     165     9065759 :   if (!CurRec) CurRec = &CurMultiClass->Rec;
     166             : 
     167             :   RecordVal *RV = CurRec->getValue(ValName);
     168     9065759 :   if (!RV)
     169           0 :     return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
     170             :                  "' unknown!");
     171             : 
     172             :   // Do not allow assignments like 'X = X'.  This will just cause infinite loops
     173             :   // in the resolution machinery.
     174     9065759 :   if (BitList.empty())
     175             :     if (VarInit *VI = dyn_cast<VarInit>(V))
     176      337204 :       if (VI->getNameInit() == ValName && !AllowSelfAssignment)
     177           0 :         return Error(Loc, "Recursion / self-assignment forbidden");
     178             : 
     179             :   // If we are assigning to a subset of the bits in the value... then we must be
     180             :   // assigning to a field of BitsRecTy, which must have a BitsInit
     181             :   // initializer.
     182             :   //
     183     9065759 :   if (!BitList.empty()) {
     184      205562 :     BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue());
     185             :     if (!CurVal)
     186           0 :       return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
     187             :                    "' is not a bits type");
     188             : 
     189             :     // Convert the incoming value to a bits type of the appropriate size...
     190      205562 :     Init *BI = V->getCastTo(BitsRecTy::get(BitList.size()));
     191      205562 :     if (!BI)
     192           0 :       return Error(Loc, "Initializer is not compatible with bit range");
     193             : 
     194      205562 :     SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
     195             : 
     196             :     // Loop over bits, assigning values as appropriate.
     197     1149876 :     for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
     198      944314 :       unsigned Bit = BitList[i];
     199     1888628 :       if (NewBits[Bit])
     200           0 :         return Error(Loc, "Cannot set bit #" + Twine(Bit) + " of value '" +
     201           0 :                      ValName->getAsUnquotedString() + "' more than once");
     202      944314 :       NewBits[Bit] = BI->getBit(i);
     203             :     }
     204             : 
     205     7015304 :     for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
     206    13619484 :       if (!NewBits[i])
     207     5865428 :         NewBits[i] = CurVal->getBit(i);
     208             : 
     209      205562 :     V = BitsInit::get(NewBits);
     210             :   }
     211             : 
     212     9065759 :   if (RV->setValue(V)) {
     213             :     std::string InitType;
     214             :     if (BitsInit *BI = dyn_cast<BitsInit>(V))
     215           8 :       InitType = (Twine("' of type bit initializer with length ") +
     216          16 :                   Twine(BI->getNumBits())).str();
     217             :     else if (TypedInit *TI = dyn_cast<TypedInit>(V))
     218           6 :       InitType = (Twine("' of type '") + TI->getType()->getAsString()).str();
     219          30 :     return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
     220          40 :                           "' of type '" + RV->getType()->getAsString() +
     221          20 :                           "' is incompatible with initializer '" +
     222          40 :                           V->getAsString() + InitType + "'");
     223             :   }
     224             :   return false;
     225             : }
     226             : 
     227             : /// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
     228             : /// args as SubClass's template arguments.
     229     3321864 : bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
     230     3321864 :   Record *SC = SubClass.Rec;
     231             :   // Add all of the values in the subclass into the current class.
     232    51299926 :   for (const RecordVal &Val : SC->getValues())
     233    47978062 :     if (AddValue(CurRec, SubClass.RefRange.Start, Val))
     234             :       return true;
     235             : 
     236             :   ArrayRef<Init *> TArgs = SC->getTemplateArgs();
     237             : 
     238             :   // Ensure that an appropriate number of template arguments are specified.
     239     3321864 :   if (TArgs.size() < SubClass.TemplateArgs.size())
     240           0 :     return Error(SubClass.RefRange.Start,
     241             :                  "More template args specified than expected");
     242             : 
     243             :   // Loop over all of the template arguments, setting them to the specified
     244             :   // value or leaving them as the default if necessary.
     245             :   MapResolver R(CurRec);
     246             : 
     247    11231718 :   for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
     248     7909855 :     if (i < SubClass.TemplateArgs.size()) {
     249             :       // If a value is specified for this template arg, set it now.
     250    12616346 :       if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
     251             :                    None, SubClass.TemplateArgs[i]))
     252             :         return true;
     253     3203364 :     } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
     254           0 :       return Error(SubClass.RefRange.Start,
     255           0 :                    "Value not specified for template argument #" +
     256           0 :                    Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
     257           0 :                    ") of subclass '" + SC->getNameInitAsString() + "'!");
     258             :     }
     259             : 
     260    15819708 :     R.set(TArgs[i], CurRec->getValue(TArgs[i])->getValue());
     261             : 
     262     7909854 :     CurRec->removeValue(TArgs[i]);
     263             :   }
     264             : 
     265             :   Init *Name;
     266     3321863 :   if (CurRec->isClass())
     267             :     Name =
     268      197755 :         VarInit::get(QualifiedNameOfImplicitName(*CurRec), StringRecTy::get());
     269             :   else
     270     3124108 :     Name = CurRec->getNameInit();
     271     3321863 :   R.set(QualifiedNameOfImplicitName(*SC), Name);
     272             : 
     273     3321863 :   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     7918096 :   for (const auto &SCPair : SCs) {
     279     9192468 :     if (CurRec->isSubClassOf(SCPair.first))
     280           0 :       return Error(SubClass.RefRange.Start,
     281           0 :                    "Already subclass of '" + SCPair.first->getName() + "'!\n");
     282             :     CurRec->addSuperClass(SCPair.first, SCPair.second);
     283             :   }
     284             : 
     285     3321862 :   if (CurRec->isSubClassOf(SC))
     286           0 :     return Error(SubClass.RefRange.Start,
     287           0 :                  "Already subclass of '" + SC->getName() + "'!\n");
     288             :   CurRec->addSuperClass(SC, SubClass.RefRange);
     289     3321862 :   return false;
     290             : }
     291             : 
     292      290162 : bool TGParser::AddSubClass(RecordsEntry &Entry, SubClassReference &SubClass) {
     293      290162 :   if (Entry.Rec)
     294      290162 :     return AddSubClass(Entry.Rec.get(), SubClass);
     295             : 
     296           0 :   for (auto &E : Entry.Loop->Entries) {
     297           0 :     if (AddSubClass(E, SubClass))
     298             :       return true;
     299             :   }
     300             : 
     301             :   return false;
     302             : }
     303             : 
     304             : /// AddSubMultiClass - Add SubMultiClass as a subclass to
     305             : /// CurMC, resolving its template args as SubMultiClass's
     306             : /// template arguments.
     307        2195 : bool TGParser::AddSubMultiClass(MultiClass *CurMC,
     308             :                                 SubMultiClassReference &SubMultiClass) {
     309        2195 :   MultiClass *SMC = SubMultiClass.MC;
     310             : 
     311             :   ArrayRef<Init *> SMCTArgs = SMC->Rec.getTemplateArgs();
     312        2195 :   if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
     313           0 :     return Error(SubMultiClass.RefRange.Start,
     314             :                  "More template args specified than expected");
     315             : 
     316             :   // Prepare the mapping of template argument name to value, filling in default
     317             :   // values if necessary.
     318             :   SubstStack TemplateArgs;
     319       11175 :   for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
     320        8980 :     if (i < SubMultiClass.TemplateArgs.size()) {
     321        8744 :       TemplateArgs.emplace_back(SMCTArgs[i], SubMultiClass.TemplateArgs[i]);
     322             :     } else {
     323         236 :       Init *Default = SMC->Rec.getValue(SMCTArgs[i])->getValue();
     324         236 :       if (!Default->isComplete()) {
     325           0 :         return Error(SubMultiClass.RefRange.Start,
     326           0 :                      "value not specified for template argument #" + Twine(i) +
     327           0 :                          " (" + SMCTArgs[i]->getAsUnquotedString() +
     328           0 :                          ") of multiclass '" + SMC->Rec.getNameInitAsString() +
     329           0 :                          "'");
     330             :       }
     331         236 :       TemplateArgs.emplace_back(SMCTArgs[i], Default);
     332             :     }
     333             :   }
     334             : 
     335        2195 :   TemplateArgs.emplace_back(
     336        2195 :       QualifiedNameOfImplicitName(SMC),
     337        2195 :       VarInit::get(QualifiedNameOfImplicitName(CurMC), StringRecTy::get()));
     338             : 
     339             :   // Add all of the defs in the subclass into the current multiclass.
     340        2195 :   return resolve(SMC->Entries, TemplateArgs, false, &CurMC->Entries);
     341             : }
     342             : 
     343             : /// Add a record or foreach loop to the current context (global record keeper,
     344             : /// current inner-most foreach loop, or multiclass).
     345     3051802 : bool TGParser::addEntry(RecordsEntry E) {
     346             :   assert(!E.Rec || !E.Loop);
     347             : 
     348     3051802 :   if (!Loops.empty()) {
     349        6414 :     Loops.back()->Entries.push_back(std::move(E));
     350        6414 :     return false;
     351             :   }
     352             : 
     353     3045388 :   if (E.Loop) {
     354             :     SubstStack Stack;
     355        3248 :     return resolve(*E.Loop, Stack, CurMultiClass == nullptr,
     356        3248 :                    CurMultiClass ? &CurMultiClass->Entries : nullptr);
     357             :   }
     358             : 
     359     3042140 :   if (CurMultiClass) {
     360      230652 :     CurMultiClass->Entries.push_back(std::move(E));
     361      230652 :     return false;
     362             :   }
     363             : 
     364     2811488 :   return addDefOne(std::move(E.Rec));
     365             : }
     366             : 
     367             : /// Resolve the entries in \p Loop, going over inner loops recursively
     368             : /// and making the given subsitutions of (name, value) pairs.
     369             : ///
     370             : /// The resulting records are stored in \p Dest if non-null. Otherwise, they
     371             : /// are added to the global record keeper.
     372       12958 : bool TGParser::resolve(const ForeachLoop &Loop, SubstStack &Substs,
     373             :                        bool Final, std::vector<RecordsEntry> *Dest,
     374             :                        SMLoc *Loc) {
     375             :   MapResolver R;
     376       35013 :   for (const auto &S : Substs)
     377       22055 :     R.set(S.first, S.second);
     378       12958 :   Init *List = Loop.ListValue->resolveReferences(R);
     379             :   auto LI = dyn_cast<ListInit>(List);
     380             :   if (!LI) {
     381         291 :     if (!Final) {
     382         291 :       Dest->emplace_back(make_unique<ForeachLoop>(Loop.Loc, Loop.IterVar,
     383             :                                                   List));
     384         291 :       return resolve(Loop.Entries, Substs, Final, &Dest->back().Loop->Entries,
     385         291 :                      Loc);
     386             :     }
     387             : 
     388           0 :     PrintError(Loop.Loc, Twine("attempting to loop over '") +
     389           0 :                               List->getAsString() + "', expected a list");
     390           0 :     return true;
     391             :   }
     392             : 
     393             :   bool Error = false;
     394      108924 :   for (auto Elt : *LI) {
     395       96257 :     Substs.emplace_back(Loop.IterVar->getNameInit(), Elt);
     396       96257 :     Error = resolve(Loop.Entries, Substs, Final, Dest);
     397             :     Substs.pop_back();
     398       96257 :     if (Error)
     399             :       break;
     400             :   }
     401       12667 :   return Error;
     402             : }
     403             : 
     404             : /// Resolve the entries in \p Source, going over loops recursively and
     405             : /// making the given substitutions of (name, value) pairs.
     406             : ///
     407             : /// The resulting records are stored in \p Dest if non-null. Otherwise, they
     408             : /// are added to the global record keeper.
     409      286836 : bool TGParser::resolve(const std::vector<RecordsEntry> &Source,
     410             :                        SubstStack &Substs, bool Final,
     411             :                        std::vector<RecordsEntry> *Dest, SMLoc *Loc) {
     412             :   bool Error = false;
     413     1419685 :   for (auto &E : Source) {
     414     1132849 :     if (E.Loop) {
     415        9710 :       Error = resolve(*E.Loop, Substs, Final, Dest);
     416             :     } else {
     417     1123139 :       auto Rec = make_unique<Record>(*E.Rec);
     418     1123139 :       if (Loc)
     419      962984 :         Rec->appendLoc(*Loc);
     420             : 
     421             :       MapResolver R(Rec.get());
     422     5957442 :       for (const auto &S : Substs)
     423     4834303 :         R.set(S.first, S.second);
     424     1123139 :       Rec->resolveReferences(R);
     425             : 
     426     1123139 :       if (Dest)
     427      990206 :         Dest->push_back(std::move(Rec));
     428             :       else
     429      132933 :         Error = addDefOne(std::move(Rec));
     430             :     }
     431     1132849 :     if (Error)
     432             :       break;
     433             :   }
     434      286836 :   return Error;
     435             : }
     436             : 
     437             : /// Resolve the record fully and add it to the record keeper.
     438     2944421 : bool TGParser::addDefOne(std::unique_ptr<Record> Rec) {
     439     4542086 :   if (Record *Prev = Records.getDef(Rec->getNameInitAsString())) {
     440      216404 :     if (!Rec->isAnonymous()) {
     441           0 :       PrintError(Rec->getLoc(),
     442           0 :                  "def already exists: " + Rec->getNameInitAsString());
     443           0 :       PrintNote(Prev->getLoc(), "location of previous definition");
     444           0 :       return true;
     445             :     }
     446      216404 :     Rec->setName(Records.getNewAnonymousName());
     447             :   }
     448             : 
     449     2944421 :   Rec->resolveReferences();
     450     2944419 :   checkConcrete(*Rec);
     451             : 
     452     5888838 :   if (!isa<StringInit>(Rec->getNameInit())) {
     453           0 :     PrintError(Rec->getLoc(), Twine("record name '") +
     454           0 :                                   Rec->getNameInit()->getAsString() +
     455           0 :                                   "' could not be fully resolved");
     456           0 :     return true;
     457             :   }
     458             : 
     459             :   // If ObjectBody has template arguments, it's an error.
     460             :   assert(Rec->getTemplateArgs().empty() && "How'd this get template args?");
     461             : 
     462     3054642 :   for (DefsetRecord *Defset : Defsets) {
     463      110224 :     DefInit *I = Rec->getDefInit();
     464      110224 :     if (!I->getType()->typeIsA(Defset->EltTy)) {
     465           1 :       PrintError(Rec->getLoc(), Twine("adding record of incompatible type '") +
     466           2 :                                     I->getType()->getAsString() +
     467           1 :                                      "' to defset");
     468           2 :       PrintNote(Defset->Loc, "location of defset declaration");
     469           1 :       return true;
     470             :     }
     471      110223 :     Defset->Elements.push_back(I);
     472             :   }
     473             : 
     474     5888836 :   Records.addDef(std::move(Rec));
     475     2944418 :   return false;
     476             : }
     477             : 
     478             : //===----------------------------------------------------------------------===//
     479             : // Parser Code
     480             : //===----------------------------------------------------------------------===//
     481             : 
     482             : /// isObjectStart - Return true if this is a valid first token for an Object.
     483             : static bool isObjectStart(tgtok::TokKind K) {
     484     3292125 :   return K == tgtok::Class || K == tgtok::Def || K == tgtok::Defm ||
     485      366146 :          K == tgtok::Let || K == tgtok::MultiClass || K == tgtok::Foreach ||
     486             :          K == tgtok::Defset;
     487             : }
     488             : 
     489             : /// ParseObjectName - If a valid object name is specified, return it. If no
     490             : /// name is specified, return the unset initializer. Return nullptr on parse
     491             : /// error.
     492             : ///   ObjectName ::= Value [ '#' Value ]*
     493             : ///   ObjectName ::= /*empty*/
     494             : ///
     495     2260385 : Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
     496     2260385 :   switch (Lex.getCode()) {
     497      349379 :   case tgtok::colon:
     498             :   case tgtok::semi:
     499             :   case tgtok::l_brace:
     500             :     // These are all of the tokens that can begin an object body.
     501             :     // Some of these can also begin values but we disallow those cases
     502             :     // because they are unlikely to be useful.
     503      349379 :     return UnsetInit::get();
     504             :   default:
     505             :     break;
     506             :   }
     507             : 
     508             :   Record *CurRec = nullptr;
     509     1911006 :   if (CurMultiClass)
     510       79166 :     CurRec = &CurMultiClass->Rec;
     511             : 
     512     1911006 :   Init *Name = ParseValue(CurRec, StringRecTy::get(), ParseNameMode);
     513     1911006 :   if (!Name)
     514             :     return nullptr;
     515             : 
     516     1911006 :   if (CurMultiClass) {
     517             :     Init *NameStr = QualifiedNameOfImplicitName(CurMultiClass);
     518             :     HasReferenceResolver R(NameStr);
     519       79166 :     Name->resolveReferences(R);
     520       79166 :     if (!R.found())
     521       66576 :       Name = BinOpInit::getStrConcat(VarInit::get(NameStr, StringRecTy::get()),
     522             :                                      Name);
     523             :   }
     524             : 
     525             :   return Name;
     526             : }
     527             : 
     528             : /// ParseClassID - Parse and resolve a reference to a class name.  This returns
     529             : /// null on error.
     530             : ///
     531             : ///    ClassID ::= ID
     532             : ///
     533     3285930 : Record *TGParser::ParseClassID() {
     534     3285930 :   if (Lex.getCode() != tgtok::Id) {
     535           0 :     TokError("expected name for ClassID");
     536           0 :     return nullptr;
     537             :   }
     538             : 
     539     6571860 :   Record *Result = Records.getClass(Lex.getCurStrVal());
     540     3285930 :   if (!Result)
     541           0 :     TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
     542             : 
     543     3285930 :   Lex.Lex();
     544     3285930 :   return Result;
     545             : }
     546             : 
     547             : /// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
     548             : /// This returns null on error.
     549             : ///
     550             : ///    MultiClassID ::= ID
     551             : ///
     552      190288 : MultiClass *TGParser::ParseMultiClassID() {
     553      190288 :   if (Lex.getCode() != tgtok::Id) {
     554           0 :     TokError("expected name for MultiClassID");
     555           0 :     return nullptr;
     556             :   }
     557             : 
     558      190288 :   MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
     559      190288 :   if (!Result)
     560           0 :     TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
     561             : 
     562      190288 :   Lex.Lex();
     563      190288 :   return Result;
     564             : }
     565             : 
     566             : /// ParseSubClassReference - Parse a reference to a subclass or to a templated
     567             : /// subclass.  This returns a SubClassRefTy with a null Record* on error.
     568             : ///
     569             : ///  SubClassRef ::= ClassID
     570             : ///  SubClassRef ::= ClassID '<' ValueList '>'
     571             : ///
     572     3259066 : SubClassReference TGParser::
     573             : ParseSubClassReference(Record *CurRec, bool isDefm) {
     574             :   SubClassReference Result;
     575     3259066 :   Result.RefRange.Start = Lex.getLoc();
     576             : 
     577     3259066 :   if (isDefm) {
     578      188093 :     if (MultiClass *MC = ParseMultiClassID())
     579      188093 :       Result.Rec = &MC->Rec;
     580             :   } else {
     581     3070973 :     Result.Rec = ParseClassID();
     582             :   }
     583     3259066 :   if (!Result.Rec) return Result;
     584             : 
     585             :   // If there is no template arg list, we're done.
     586     3259066 :   if (Lex.getCode() != tgtok::less) {
     587      339456 :     Result.RefRange.End = Lex.getLoc();
     588      339456 :     return Result;
     589             :   }
     590             :   Lex.Lex();  // Eat the '<'
     591             : 
     592     2919610 :   if (Lex.getCode() == tgtok::greater) {
     593           0 :     TokError("subclass reference requires a non-empty list of template values");
     594           0 :     Result.Rec = nullptr;
     595           0 :     return Result;
     596             :   }
     597             : 
     598     2919610 :   ParseValueList(Result.TemplateArgs, CurRec, Result.Rec);
     599     2919610 :   if (Result.TemplateArgs.empty()) {
     600           0 :     Result.Rec = nullptr;   // Error parsing value list.
     601           0 :     return Result;
     602             :   }
     603             : 
     604     2919610 :   if (Lex.getCode() != tgtok::greater) {
     605           0 :     TokError("expected '>' in template value list");
     606           0 :     Result.Rec = nullptr;
     607           0 :     return Result;
     608             :   }
     609             :   Lex.Lex();
     610     2919610 :   Result.RefRange.End = Lex.getLoc();
     611             : 
     612     2919610 :   return Result;
     613             : }
     614             : 
     615             : /// ParseSubMultiClassReference - Parse a reference to a subclass or to a
     616             : /// templated submulticlass.  This returns a SubMultiClassRefTy with a null
     617             : /// Record* on error.
     618             : ///
     619             : ///  SubMultiClassRef ::= MultiClassID
     620             : ///  SubMultiClassRef ::= MultiClassID '<' ValueList '>'
     621             : ///
     622        2195 : SubMultiClassReference TGParser::
     623             : ParseSubMultiClassReference(MultiClass *CurMC) {
     624             :   SubMultiClassReference Result;
     625        2195 :   Result.RefRange.Start = Lex.getLoc();
     626             : 
     627        2195 :   Result.MC = ParseMultiClassID();
     628        2195 :   if (!Result.MC) return Result;
     629             : 
     630             :   // If there is no template arg list, we're done.
     631        2195 :   if (Lex.getCode() != tgtok::less) {
     632           0 :     Result.RefRange.End = Lex.getLoc();
     633           0 :     return Result;
     634             :   }
     635             :   Lex.Lex();  // Eat the '<'
     636             : 
     637        2195 :   if (Lex.getCode() == tgtok::greater) {
     638           0 :     TokError("subclass reference requires a non-empty list of template values");
     639           0 :     Result.MC = nullptr;
     640           0 :     return Result;
     641             :   }
     642             : 
     643        2195 :   ParseValueList(Result.TemplateArgs, &CurMC->Rec, &Result.MC->Rec);
     644        2195 :   if (Result.TemplateArgs.empty()) {
     645           0 :     Result.MC = nullptr;   // Error parsing value list.
     646           0 :     return Result;
     647             :   }
     648             : 
     649        2195 :   if (Lex.getCode() != tgtok::greater) {
     650           0 :     TokError("expected '>' in template value list");
     651           0 :     Result.MC = nullptr;
     652           0 :     return Result;
     653             :   }
     654             :   Lex.Lex();
     655        2195 :   Result.RefRange.End = Lex.getLoc();
     656             : 
     657        2195 :   return Result;
     658             : }
     659             : 
     660             : /// ParseRangePiece - Parse a bit/value range.
     661             : ///   RangePiece ::= INTVAL
     662             : ///   RangePiece ::= INTVAL '-' INTVAL
     663             : ///   RangePiece ::= INTVAL INTVAL
     664      255203 : bool TGParser::ParseRangePiece(SmallVectorImpl<unsigned> &Ranges) {
     665      255203 :   if (Lex.getCode() != tgtok::IntVal) {
     666           0 :     TokError("expected integer or bitrange");
     667           0 :     return true;
     668             :   }
     669      255203 :   int64_t Start = Lex.getCurIntVal();
     670             :   int64_t End;
     671             : 
     672      255203 :   if (Start < 0)
     673           0 :     return TokError("invalid range, cannot be negative");
     674             : 
     675      255203 :   switch (Lex.Lex()) {  // eat first character.
     676       49846 :   default:
     677       49846 :     Ranges.push_back(Start);
     678       49846 :     return false;
     679         468 :   case tgtok::minus:
     680         468 :     if (Lex.Lex() != tgtok::IntVal) {
     681           0 :       TokError("expected integer value as end of range");
     682           0 :       return true;
     683             :     }
     684         468 :     End = Lex.getCurIntVal();
     685         468 :     break;
     686      204889 :   case tgtok::IntVal:
     687      204889 :     End = -Lex.getCurIntVal();
     688      204889 :     break;
     689             :   }
     690      205357 :   if (End < 0)
     691           0 :     return TokError("invalid range, cannot be negative");
     692             :   Lex.Lex();
     693             : 
     694             :   // Add to the range.
     695      205357 :   if (Start < End)
     696       70136 :     for (; Start <= End; ++Start)
     697       63388 :       Ranges.push_back(Start);
     698             :   else
     699     1232508 :     for (; Start >= End; --Start)
     700     1033899 :       Ranges.push_back(Start);
     701             :   return false;
     702             : }
     703             : 
     704             : /// ParseRangeList - Parse a list of scalars and ranges into scalar values.
     705             : ///
     706             : ///   RangeList ::= RangePiece (',' RangePiece)*
     707             : ///
     708      253139 : void TGParser::ParseRangeList(SmallVectorImpl<unsigned> &Result) {
     709             :   // Parse the first piece.
     710      253139 :   if (ParseRangePiece(Result)) {
     711             :     Result.clear();
     712           0 :     return;
     713             :   }
     714      254141 :   while (Lex.getCode() == tgtok::comma) {
     715        1002 :     Lex.Lex();  // Eat the comma.
     716             : 
     717             :     // Parse the next range piece.
     718        1002 :     if (ParseRangePiece(Result)) {
     719             :       Result.clear();
     720           0 :       return;
     721             :     }
     722             :   }
     723             : }
     724             : 
     725             : /// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
     726             : ///   OptionalRangeList ::= '<' RangeList '>'
     727             : ///   OptionalRangeList ::= /*empty*/
     728      128665 : bool TGParser::ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges) {
     729      128665 :   if (Lex.getCode() != tgtok::less)
     730             :     return false;
     731             : 
     732           0 :   SMLoc StartLoc = Lex.getLoc();
     733             :   Lex.Lex(); // eat the '<'
     734             : 
     735             :   // Parse the range list.
     736           0 :   ParseRangeList(Ranges);
     737           0 :   if (Ranges.empty()) return true;
     738             : 
     739           0 :   if (Lex.getCode() != tgtok::greater) {
     740           0 :     TokError("expected '>' at end of range list");
     741           0 :     return Error(StartLoc, "to match this '<'");
     742             :   }
     743             :   Lex.Lex();   // eat the '>'.
     744           0 :   return false;
     745             : }
     746             : 
     747             : /// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
     748             : ///   OptionalBitList ::= '{' RangeList '}'
     749             : ///   OptionalBitList ::= /*empty*/
     750      689348 : bool TGParser::ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges) {
     751      689348 :   if (Lex.getCode() != tgtok::l_brace)
     752             :     return false;
     753             : 
     754      205562 :   SMLoc StartLoc = Lex.getLoc();
     755             :   Lex.Lex(); // eat the '{'
     756             : 
     757             :   // Parse the range list.
     758      205562 :   ParseRangeList(Ranges);
     759      205562 :   if (Ranges.empty()) return true;
     760             : 
     761      205562 :   if (Lex.getCode() != tgtok::r_brace) {
     762           0 :     TokError("expected '}' at end of bit list");
     763           0 :     return Error(StartLoc, "to match this '{'");
     764             :   }
     765             :   Lex.Lex();   // eat the '}'.
     766      205562 :   return false;
     767             : }
     768             : 
     769             : /// ParseType - Parse and return a tblgen type.  This returns null on error.
     770             : ///
     771             : ///   Type ::= STRING                       // string type
     772             : ///   Type ::= CODE                         // code type
     773             : ///   Type ::= BIT                          // bit type
     774             : ///   Type ::= BITS '<' INTVAL '>'          // bits<x> type
     775             : ///   Type ::= INT                          // int type
     776             : ///   Type ::= LIST '<' Type '>'            // list<x> type
     777             : ///   Type ::= DAG                          // dag type
     778             : ///   Type ::= ClassID                      // Record Type
     779             : ///
     780      708201 : RecTy *TGParser::ParseType() {
     781      708201 :   switch (Lex.getCode()) {
     782           0 :   default: TokError("Unknown token when expecting a type"); return nullptr;
     783      163815 :   case tgtok::String: Lex.Lex(); return StringRecTy::get();
     784        4028 :   case tgtok::Code:   Lex.Lex(); return CodeRecTy::get();
     785       47807 :   case tgtok::Bit:    Lex.Lex(); return BitRecTy::get();
     786       39097 :   case tgtok::Int:    Lex.Lex(); return IntRecTy::get();
     787       54507 :   case tgtok::Dag:    Lex.Lex(); return DagRecTy::get();
     788      214957 :   case tgtok::Id:
     789      214957 :     if (Record *R = ParseClassID()) return RecordRecTy::get(R);
     790           0 :     TokError("unknown class name");
     791           0 :     return nullptr;
     792      120273 :   case tgtok::Bits: {
     793      120273 :     if (Lex.Lex() != tgtok::less) { // Eat 'bits'
     794           0 :       TokError("expected '<' after bits type");
     795           0 :       return nullptr;
     796             :     }
     797      120273 :     if (Lex.Lex() != tgtok::IntVal) {  // Eat '<'
     798           0 :       TokError("expected integer in bits<n> type");
     799           0 :       return nullptr;
     800             :     }
     801      120273 :     uint64_t Val = Lex.getCurIntVal();
     802      120273 :     if (Lex.Lex() != tgtok::greater) {  // Eat count.
     803           0 :       TokError("expected '>' at end of bits<n> type");
     804           0 :       return nullptr;
     805             :     }
     806             :     Lex.Lex();  // Eat '>'
     807      120273 :     return BitsRecTy::get(Val);
     808             :   }
     809       63717 :   case tgtok::List: {
     810       63717 :     if (Lex.Lex() != tgtok::less) { // Eat 'bits'
     811           0 :       TokError("expected '<' after list type");
     812           0 :       return nullptr;
     813             :     }
     814             :     Lex.Lex();  // Eat '<'
     815       63717 :     RecTy *SubType = ParseType();
     816       63717 :     if (!SubType) return nullptr;
     817             : 
     818       63717 :     if (Lex.getCode() != tgtok::greater) {
     819           0 :       TokError("expected '>' at end of list<ty> type");
     820           0 :       return nullptr;
     821             :     }
     822             :     Lex.Lex();  // Eat '>'
     823       63717 :     return ListRecTy::get(SubType);
     824             :   }
     825             :   }
     826             : }
     827             : 
     828             : /// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
     829             : /// has already been read.
     830    10046155 : Init *TGParser::ParseIDValue(Record *CurRec, StringInit *Name, SMLoc NameLoc,
     831             :                              IDParseMode Mode) {
     832    10046155 :   if (CurRec) {
     833     7691818 :     if (const RecordVal *RV = CurRec->getValue(Name))
     834      101870 :       return VarInit::get(Name, RV->getType());
     835             :   }
     836             : 
     837     9944285 :   if ((CurRec && CurRec->isClass()) || CurMultiClass) {
     838             :     Init *TemplateArgName;
     839     1806154 :     if (CurMultiClass) {
     840             :       TemplateArgName =
     841      852188 :           QualifyName(CurMultiClass->Rec, CurMultiClass, Name, "::");
     842             :     } else
     843      953966 :       TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
     844             : 
     845     1806154 :     Record *TemplateRec = CurMultiClass ? &CurMultiClass->Rec : CurRec;
     846     1806154 :     if (TemplateRec->isTemplateArg(TemplateArgName)) {
     847             :       const RecordVal *RV = TemplateRec->getValue(TemplateArgName);
     848             :       assert(RV && "Template arg doesn't exist??");
     849      737895 :       return VarInit::get(TemplateArgName, RV->getType());
     850             :     } else if (Name->getValue() == "NAME") {
     851       26213 :       return VarInit::get(TemplateArgName, StringRecTy::get());
     852             :     }
     853             :   }
     854             : 
     855             :   // If this is in a foreach loop, make sure it's not a loop iterator
     856     9195310 :   for (const auto &L : Loops) {
     857       28538 :     VarInit *IterVar = dyn_cast<VarInit>(L->IterVar);
     858       28538 :     if (IterVar && IterVar->getNameInit() == Name)
     859             :       return IterVar;
     860             :   }
     861             : 
     862     9166772 :   if (Mode == ParseNameMode)
     863             :     return Name;
     864             : 
     865     7254203 :   if (Init *I = Records.getGlobal(Name->getValue()))
     866             :     return I;
     867             : 
     868             :   // Allow self-references of concrete defs, but delay the lookup so that we
     869             :   // get the correct type.
     870         510 :   if (CurRec && !CurRec->isClass() && !CurMultiClass &&
     871         510 :       CurRec->getNameInit() == Name)
     872         510 :     return UnOpInit::get(UnOpInit::CAST, Name, CurRec->getType());
     873             : 
     874           0 :   Error(NameLoc, "Variable not defined: '" + Name->getValue() + "'");
     875           0 :   return nullptr;
     876             : }
     877             : 
     878             : /// ParseOperation - Parse an operator.  This returns null on error.
     879             : ///
     880             : /// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
     881             : ///
     882       86508 : Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
     883       86508 :   switch (Lex.getCode()) {
     884             :   default:
     885           0 :     TokError("unknown operation");
     886           0 :     return nullptr;
     887       27151 :   case tgtok::XHead:
     888             :   case tgtok::XTail:
     889             :   case tgtok::XSize:
     890             :   case tgtok::XEmpty:
     891             :   case tgtok::XCast: {  // Value ::= !unop '(' Value ')'
     892             :     UnOpInit::UnaryOp Code;
     893             :     RecTy *Type = nullptr;
     894             : 
     895             :     switch (Lex.getCode()) {
     896           0 :     default: llvm_unreachable("Unhandled code!");
     897       25576 :     case tgtok::XCast:
     898       25576 :       Lex.Lex();  // eat the operation
     899             :       Code = UnOpInit::CAST;
     900             : 
     901       25576 :       Type = ParseOperatorType();
     902             : 
     903       25576 :       if (!Type) {
     904           0 :         TokError("did not get type for unary operator");
     905           0 :         return nullptr;
     906             :       }
     907             : 
     908             :       break;
     909         239 :     case tgtok::XHead:
     910         239 :       Lex.Lex();  // eat the operation
     911             :       Code = UnOpInit::HEAD;
     912         239 :       break;
     913         259 :     case tgtok::XTail:
     914         259 :       Lex.Lex();  // eat the operation
     915             :       Code = UnOpInit::TAIL;
     916         259 :       break;
     917         800 :     case tgtok::XSize:
     918         800 :       Lex.Lex();
     919             :       Code = UnOpInit::SIZE;
     920             :       Type = IntRecTy::get();
     921         800 :       break;
     922         277 :     case tgtok::XEmpty:
     923         277 :       Lex.Lex();  // eat the operation
     924             :       Code = UnOpInit::EMPTY;
     925             :       Type = IntRecTy::get();
     926         277 :       break;
     927             :     }
     928       27151 :     if (Lex.getCode() != tgtok::l_paren) {
     929           0 :       TokError("expected '(' after unary operator");
     930           0 :       return nullptr;
     931             :     }
     932       27151 :     Lex.Lex();  // eat the '('
     933             : 
     934       27151 :     Init *LHS = ParseValue(CurRec);
     935       27151 :     if (!LHS) return nullptr;
     936             : 
     937       54302 :     if (Code == UnOpInit::HEAD ||
     938       27151 :         Code == UnOpInit::TAIL ||
     939       27151 :         Code == UnOpInit::EMPTY) {
     940             :       ListInit *LHSl = dyn_cast<ListInit>(LHS);
     941             :       StringInit *LHSs = dyn_cast<StringInit>(LHS);
     942             :       TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
     943         775 :       if (!LHSl && !LHSs && !LHSt) {
     944           0 :         TokError("expected list or string type argument in unary operator");
     945           0 :         return nullptr;
     946             :       }
     947         775 :       if (LHSt) {
     948         775 :         ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
     949             :         StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
     950         775 :         if (!LType && !SType) {
     951           0 :           TokError("expected list or string type argument in unary operator");
     952           0 :           return nullptr;
     953             :         }
     954             :       }
     955             : 
     956         775 :       if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL ||
     957             :           Code == UnOpInit::SIZE) {
     958         498 :         if (!LHSl && !LHSt) {
     959           0 :           TokError("expected list type argument in unary operator");
     960           0 :           return nullptr;
     961             :         }
     962             :       }
     963             : 
     964         775 :       if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
     965         498 :         if (LHSl && LHSl->empty()) {
     966           0 :           TokError("empty list argument in unary operator");
     967           0 :           return nullptr;
     968             :         }
     969         498 :         if (LHSl) {
     970             :           Init *Item = LHSl->getElement(0);
     971             :           TypedInit *Itemt = dyn_cast<TypedInit>(Item);
     972             :           if (!Itemt) {
     973           0 :             TokError("untyped list element in unary operator");
     974           0 :             return nullptr;
     975             :           }
     976           0 :           Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
     977           0 :                                           : ListRecTy::get(Itemt->getType());
     978             :         } else {
     979             :           assert(LHSt && "expected list type argument in unary operator");
     980         498 :           ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
     981             :           if (!LType) {
     982           0 :             TokError("expected list type argument in unary operator");
     983           0 :             return nullptr;
     984             :           }
     985         498 :           Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
     986             :         }
     987             :       }
     988             :     }
     989             : 
     990       27151 :     if (Lex.getCode() != tgtok::r_paren) {
     991           0 :       TokError("expected ')' in unary operator");
     992           0 :       return nullptr;
     993             :     }
     994             :     Lex.Lex();  // eat the ')'
     995       27151 :     return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec);
     996             :   }
     997             : 
     998         593 :   case tgtok::XIsA: {
     999             :     // Value ::= !isa '<' Type '>' '(' Value ')'
    1000         593 :     Lex.Lex(); // eat the operation
    1001             : 
    1002         593 :     RecTy *Type = ParseOperatorType();
    1003         593 :     if (!Type)
    1004             :       return nullptr;
    1005             : 
    1006         593 :     if (Lex.getCode() != tgtok::l_paren) {
    1007           0 :       TokError("expected '(' after type of !isa");
    1008           0 :       return nullptr;
    1009             :     }
    1010             :     Lex.Lex(); // eat the '('
    1011             : 
    1012         593 :     Init *LHS = ParseValue(CurRec);
    1013         593 :     if (!LHS)
    1014             :       return nullptr;
    1015             : 
    1016         593 :     if (Lex.getCode() != tgtok::r_paren) {
    1017           0 :       TokError("expected ')' in !isa");
    1018           0 :       return nullptr;
    1019             :     }
    1020             :     Lex.Lex(); // eat the ')'
    1021             : 
    1022         593 :     return (IsAOpInit::get(Type, LHS))->Fold();
    1023             :   }
    1024             : 
    1025       38768 :   case tgtok::XConcat:
    1026             :   case tgtok::XADD:
    1027             :   case tgtok::XAND:
    1028             :   case tgtok::XOR:
    1029             :   case tgtok::XSRA:
    1030             :   case tgtok::XSRL:
    1031             :   case tgtok::XSHL:
    1032             :   case tgtok::XEq:
    1033             :   case tgtok::XNe:
    1034             :   case tgtok::XLe:
    1035             :   case tgtok::XLt:
    1036             :   case tgtok::XGe:
    1037             :   case tgtok::XGt:
    1038             :   case tgtok::XListConcat:
    1039             :   case tgtok::XStrConcat: {  // Value ::= !binop '(' Value ',' Value ')'
    1040             :     tgtok::TokKind OpTok = Lex.getCode();
    1041       38768 :     SMLoc OpLoc = Lex.getLoc();
    1042             :     Lex.Lex();  // eat the operation
    1043             : 
    1044             :     BinOpInit::BinaryOp Code;
    1045             :     switch (OpTok) {
    1046           0 :     default: llvm_unreachable("Unhandled code!");
    1047             :     case tgtok::XConcat: Code = BinOpInit::CONCAT; break;
    1048        2826 :     case tgtok::XADD:    Code = BinOpInit::ADD; break;
    1049         281 :     case tgtok::XAND:    Code = BinOpInit::AND; break;
    1050         503 :     case tgtok::XOR:     Code = BinOpInit::OR; break;
    1051           1 :     case tgtok::XSRA:    Code = BinOpInit::SRA; break;
    1052          96 :     case tgtok::XSRL:    Code = BinOpInit::SRL; break;
    1053         228 :     case tgtok::XSHL:    Code = BinOpInit::SHL; break;
    1054        6721 :     case tgtok::XEq:     Code = BinOpInit::EQ; break;
    1055          34 :     case tgtok::XNe:     Code = BinOpInit::NE; break;
    1056          76 :     case tgtok::XLe:     Code = BinOpInit::LE; break;
    1057           2 :     case tgtok::XLt:     Code = BinOpInit::LT; break;
    1058           1 :     case tgtok::XGe:     Code = BinOpInit::GE; break;
    1059           1 :     case tgtok::XGt:     Code = BinOpInit::GT; break;
    1060        3017 :     case tgtok::XListConcat: Code = BinOpInit::LISTCONCAT; break;
    1061       24186 :     case tgtok::XStrConcat: Code = BinOpInit::STRCONCAT; break;
    1062             :     }
    1063             : 
    1064             :     RecTy *Type = nullptr;
    1065             :     RecTy *ArgType = nullptr;
    1066             :     switch (OpTok) {
    1067           0 :     default:
    1068           0 :       llvm_unreachable("Unhandled code!");
    1069             :     case tgtok::XConcat:
    1070             :       Type = DagRecTy::get();
    1071             :       ArgType = DagRecTy::get();
    1072             :       break;
    1073             :     case tgtok::XAND:
    1074             :     case tgtok::XOR:
    1075             :     case tgtok::XSRA:
    1076             :     case tgtok::XSRL:
    1077             :     case tgtok::XSHL:
    1078             :     case tgtok::XADD:
    1079             :       Type = IntRecTy::get();
    1080             :       ArgType = IntRecTy::get();
    1081        3935 :       break;
    1082             :     case tgtok::XEq:
    1083             :     case tgtok::XNe:
    1084             :       Type = BitRecTy::get();
    1085             :       // ArgType for Eq / Ne is not known at this point
    1086        6755 :       break;
    1087             :     case tgtok::XLe:
    1088             :     case tgtok::XLt:
    1089             :     case tgtok::XGe:
    1090             :     case tgtok::XGt:
    1091             :       Type = BitRecTy::get();
    1092             :       ArgType = IntRecTy::get();
    1093          80 :       break;
    1094             :     case tgtok::XListConcat:
    1095             :       // We don't know the list type until we parse the first argument
    1096             :       ArgType = ItemType;
    1097             :       break;
    1098             :     case tgtok::XStrConcat:
    1099             :       Type = StringRecTy::get();
    1100             :       ArgType = StringRecTy::get();
    1101       24186 :       break;
    1102             :     }
    1103             : 
    1104       35751 :     if (Type && ItemType && !Type->typeIsConvertibleTo(ItemType)) {
    1105           0 :       Error(OpLoc, Twine("expected value of type '") +
    1106           0 :                    ItemType->getAsString() + "', got '" +
    1107           0 :                    Type->getAsString() + "'");
    1108           0 :       return nullptr;
    1109             :     }
    1110             : 
    1111       38768 :     if (Lex.getCode() != tgtok::l_paren) {
    1112           0 :       TokError("expected '(' after binary operator");
    1113           0 :       return nullptr;
    1114             :     }
    1115             :     Lex.Lex();  // eat the '('
    1116             : 
    1117             :     SmallVector<Init*, 2> InitList;
    1118             : 
    1119             :     for (;;) {
    1120       83499 :       SMLoc InitLoc = Lex.getLoc();
    1121       83499 :       InitList.push_back(ParseValue(CurRec, ArgType));
    1122       83499 :       if (!InitList.back()) return nullptr;
    1123             : 
    1124             :       // All BinOps require their arguments to be of compatible types.
    1125             :       TypedInit *TI = dyn_cast<TypedInit>(InitList.back());
    1126       83499 :       if (!ArgType) {
    1127        7182 :         ArgType = TI->getType();
    1128             : 
    1129             :         switch (Code) {
    1130         427 :         case BinOpInit::LISTCONCAT:
    1131         427 :           if (!isa<ListRecTy>(ArgType)) {
    1132           0 :             Error(InitLoc, Twine("expected a list, got value of type '") +
    1133           0 :                            ArgType->getAsString() + "'");
    1134           0 :             return nullptr;
    1135             :           }
    1136             :           break;
    1137        6755 :         case BinOpInit::EQ:
    1138             :         case BinOpInit::NE:
    1139        8530 :           if (!ArgType->typeIsConvertibleTo(IntRecTy::get()) &&
    1140        1775 :               !ArgType->typeIsConvertibleTo(StringRecTy::get())) {
    1141           0 :             Error(InitLoc, Twine("expected int, bits, or string; got value of "
    1142           0 :                                  "type '") + ArgType->getAsString() + "'");
    1143           0 :             return nullptr;
    1144             :           }
    1145             :           break;
    1146           0 :         default: llvm_unreachable("other ops have fixed argument types");
    1147             :         }
    1148             :       } else {
    1149       76317 :         RecTy *Resolved = resolveTypes(ArgType, TI->getType());
    1150       76317 :         if (!Resolved) {
    1151           0 :           Error(InitLoc, Twine("expected value of type '") +
    1152           0 :                          ArgType->getAsString() + "', got '" +
    1153           0 :                          TI->getType()->getAsString() + "'");
    1154           0 :           return nullptr;
    1155             :         }
    1156      152634 :         if (Code != BinOpInit::ADD && Code != BinOpInit::AND &&
    1157       76317 :             Code != BinOpInit::OR && Code != BinOpInit::SRA &&
    1158       68900 :             Code != BinOpInit::SRL && Code != BinOpInit::SHL)
    1159             :           ArgType = Resolved;
    1160             :       }
    1161             : 
    1162       83499 :       if (Lex.getCode() != tgtok::comma)
    1163             :         break;
    1164             :       Lex.Lex();  // eat the ','
    1165       44731 :     }
    1166             : 
    1167       38768 :     if (Lex.getCode() != tgtok::r_paren) {
    1168           0 :       TokError("expected ')' in operator");
    1169           0 :       return nullptr;
    1170             :     }
    1171             :     Lex.Lex();  // eat the ')'
    1172             : 
    1173       38768 :     if (Code == BinOpInit::LISTCONCAT)
    1174             :       Type = ArgType;
    1175             : 
    1176             :     // We allow multiple operands to associative operators like !strconcat as
    1177             :     // shorthand for nesting them.
    1178       77536 :     if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT ||
    1179       38768 :         Code == BinOpInit::CONCAT || Code == BinOpInit::ADD ||
    1180        7944 :         Code == BinOpInit::AND || Code == BinOpInit::OR) {
    1181       75142 :       while (InitList.size() > 2) {
    1182             :         Init *RHS = InitList.pop_back_val();
    1183        5963 :         RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))->Fold(CurRec);
    1184        5963 :         InitList.back() = RHS;
    1185             :       }
    1186             :     }
    1187             : 
    1188       38768 :     if (InitList.size() == 2)
    1189             :       return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
    1190       38768 :           ->Fold(CurRec);
    1191             : 
    1192           0 :     Error(OpLoc, "expected two operands to operator");
    1193           0 :     return nullptr;
    1194             :   }
    1195             : 
    1196        1835 :   case tgtok::XForEach: { // Value ::= !foreach '(' Id ',' Value ',' Value ')'
    1197        1835 :     SMLoc OpLoc = Lex.getLoc();
    1198             :     Lex.Lex(); // eat the operation
    1199        1835 :     if (Lex.getCode() != tgtok::l_paren) {
    1200           0 :       TokError("expected '(' after !foreach");
    1201           0 :       return nullptr;
    1202             :     }
    1203             : 
    1204        1835 :     if (Lex.Lex() != tgtok::Id) { // eat the '('
    1205           0 :       TokError("first argument of !foreach must be an identifier");
    1206           0 :       return nullptr;
    1207             :     }
    1208             : 
    1209        1835 :     Init *LHS = StringInit::get(Lex.getCurStrVal());
    1210             : 
    1211        3669 :     if (CurRec && CurRec->getValue(LHS)) {
    1212           0 :       TokError((Twine("iteration variable '") + LHS->getAsString() +
    1213           0 :                 "' already defined")
    1214           0 :                    .str());
    1215           0 :       return nullptr;
    1216             :     }
    1217             : 
    1218        1835 :     if (Lex.Lex() != tgtok::comma) { // eat the id
    1219           0 :       TokError("expected ',' in ternary operator");
    1220           0 :       return nullptr;
    1221             :     }
    1222             :     Lex.Lex();  // eat the ','
    1223             : 
    1224        1835 :     Init *MHS = ParseValue(CurRec);
    1225        1835 :     if (!MHS)
    1226             :       return nullptr;
    1227             : 
    1228        1835 :     if (Lex.getCode() != tgtok::comma) {
    1229           0 :       TokError("expected ',' in ternary operator");
    1230           0 :       return nullptr;
    1231             :     }
    1232             :     Lex.Lex();  // eat the ','
    1233             : 
    1234             :     TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
    1235             :     if (!MHSt) {
    1236           0 :       TokError("could not get type of !foreach input");
    1237           0 :       return nullptr;
    1238             :     }
    1239             : 
    1240             :     RecTy *InEltType = nullptr;
    1241             :     RecTy *OutEltType = nullptr;
    1242             :     bool IsDAG = false;
    1243             : 
    1244        1835 :     if (ListRecTy *InListTy = dyn_cast<ListRecTy>(MHSt->getType())) {
    1245        1762 :       InEltType = InListTy->getElementType();
    1246        1762 :       if (ItemType) {
    1247             :         if (ListRecTy *OutListTy = dyn_cast<ListRecTy>(ItemType)) {
    1248        1761 :           OutEltType = OutListTy->getElementType();
    1249             :         } else {
    1250           0 :           Error(OpLoc,
    1251           0 :                 "expected value of type '" + Twine(ItemType->getAsString()) +
    1252           0 :                 "', but got !foreach of list type");
    1253           0 :           return nullptr;
    1254             :         }
    1255             :       }
    1256             :     } else if (DagRecTy *InDagTy = dyn_cast<DagRecTy>(MHSt->getType())) {
    1257             :       InEltType = InDagTy;
    1258          73 :       if (ItemType && !isa<DagRecTy>(ItemType)) {
    1259           0 :         Error(OpLoc,
    1260           0 :               "expected value of type '" + Twine(ItemType->getAsString()) +
    1261           0 :               "', but got !foreach of dag type");
    1262           0 :         return nullptr;
    1263             :       }
    1264             :       IsDAG = true;
    1265             :     } else {
    1266           0 :       TokError("!foreach must have list or dag input");
    1267           0 :       return nullptr;
    1268             :     }
    1269             : 
    1270             :     // We need to create a temporary record to provide a scope for the iteration
    1271             :     // variable while parsing top-level foreach's.
    1272        1835 :     std::unique_ptr<Record> ParseRecTmp;
    1273             :     Record *ParseRec = CurRec;
    1274        1835 :     if (!ParseRec) {
    1275           1 :       ParseRecTmp = make_unique<Record>(".parse", ArrayRef<SMLoc>{}, Records);
    1276             :       ParseRec = ParseRecTmp.get();
    1277             :     }
    1278             : 
    1279        1835 :     ParseRec->addValue(RecordVal(LHS, InEltType, false));
    1280        1835 :     Init *RHS = ParseValue(ParseRec, OutEltType);
    1281        1835 :     ParseRec->removeValue(LHS);
    1282        1835 :     if (!RHS)
    1283             :       return nullptr;
    1284             : 
    1285        1835 :     if (Lex.getCode() != tgtok::r_paren) {
    1286           0 :       TokError("expected ')' in binary operator");
    1287           0 :       return nullptr;
    1288             :     }
    1289             :     Lex.Lex();  // eat the ')'
    1290             : 
    1291             :     RecTy *OutType;
    1292        1835 :     if (IsDAG) {
    1293             :       OutType = InEltType;
    1294             :     } else {
    1295             :       TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
    1296             :       if (!RHSt) {
    1297           0 :         TokError("could not get type of !foreach result");
    1298           0 :         return nullptr;
    1299             :       }
    1300        1762 :       OutType = RHSt->getType()->getListTy();
    1301             :     }
    1302             : 
    1303             :     return (TernOpInit::get(TernOpInit::FOREACH, LHS, MHS, RHS, OutType))
    1304        1835 :         ->Fold(CurRec);
    1305             :   }
    1306             : 
    1307       17509 :   case tgtok::XDag:
    1308             :   case tgtok::XIf:
    1309             :   case tgtok::XSubst: {  // Value ::= !ternop '(' Value ',' Value ',' Value ')'
    1310             :     TernOpInit::TernaryOp Code;
    1311             :     RecTy *Type = nullptr;
    1312             : 
    1313             :     tgtok::TokKind LexCode = Lex.getCode();
    1314       17509 :     Lex.Lex();  // eat the operation
    1315             :     switch (LexCode) {
    1316           0 :     default: llvm_unreachable("Unhandled code!");
    1317             :     case tgtok::XDag:
    1318             :       Code = TernOpInit::DAG;
    1319             :       Type = DagRecTy::get();
    1320             :       ItemType = nullptr;
    1321             :       break;
    1322       17177 :     case tgtok::XIf:
    1323             :       Code = TernOpInit::IF;
    1324       17177 :       break;
    1325         325 :     case tgtok::XSubst:
    1326             :       Code = TernOpInit::SUBST;
    1327         325 :       break;
    1328             :     }
    1329       17509 :     if (Lex.getCode() != tgtok::l_paren) {
    1330           0 :       TokError("expected '(' after ternary operator");
    1331           0 :       return nullptr;
    1332             :     }
    1333             :     Lex.Lex();  // eat the '('
    1334             : 
    1335       17509 :     Init *LHS = ParseValue(CurRec);
    1336       17509 :     if (!LHS) return nullptr;
    1337             : 
    1338       17509 :     if (Lex.getCode() != tgtok::comma) {
    1339           0 :       TokError("expected ',' in ternary operator");
    1340           0 :       return nullptr;
    1341             :     }
    1342             :     Lex.Lex();  // eat the ','
    1343             : 
    1344       17509 :     SMLoc MHSLoc = Lex.getLoc();
    1345       17509 :     Init *MHS = ParseValue(CurRec, ItemType);
    1346       17509 :     if (!MHS)
    1347             :       return nullptr;
    1348             : 
    1349       17509 :     if (Lex.getCode() != tgtok::comma) {
    1350           0 :       TokError("expected ',' in ternary operator");
    1351           0 :       return nullptr;
    1352             :     }
    1353             :     Lex.Lex();  // eat the ','
    1354             : 
    1355       17509 :     SMLoc RHSLoc = Lex.getLoc();
    1356       17509 :     Init *RHS = ParseValue(CurRec, ItemType);
    1357       17509 :     if (!RHS)
    1358             :       return nullptr;
    1359             : 
    1360       17509 :     if (Lex.getCode() != tgtok::r_paren) {
    1361           0 :       TokError("expected ')' in binary operator");
    1362           0 :       return nullptr;
    1363             :     }
    1364             :     Lex.Lex();  // eat the ')'
    1365             : 
    1366             :     switch (LexCode) {
    1367           0 :     default: llvm_unreachable("Unhandled code!");
    1368           7 :     case tgtok::XDag: {
    1369             :       TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
    1370           1 :       if (!MHSt && !isa<UnsetInit>(MHS)) {
    1371           0 :         Error(MHSLoc, "could not determine type of the child list in !dag");
    1372           0 :         return nullptr;
    1373             :       }
    1374           7 :       if (MHSt && !isa<ListRecTy>(MHSt->getType())) {
    1375           0 :         Error(MHSLoc, Twine("expected list of children, got type '") +
    1376           0 :                           MHSt->getType()->getAsString() + "'");
    1377           0 :         return nullptr;
    1378             :       }
    1379             : 
    1380             :       TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
    1381           2 :       if (!RHSt && !isa<UnsetInit>(RHS)) {
    1382           0 :         Error(RHSLoc, "could not determine type of the name list in !dag");
    1383           0 :         return nullptr;
    1384             :       }
    1385           7 :       if (RHSt && StringRecTy::get()->getListTy() != RHSt->getType()) {
    1386           0 :         Error(RHSLoc, Twine("expected list<string>, got type '") +
    1387           0 :                           RHSt->getType()->getAsString() + "'");
    1388           0 :         return nullptr;
    1389             :       }
    1390             : 
    1391           7 :       if (!MHSt && !RHSt) {
    1392           0 :         Error(MHSLoc,
    1393             :               "cannot have both unset children and unset names in !dag");
    1394           0 :         return nullptr;
    1395             :       }
    1396             :       break;
    1397             :     }
    1398       17177 :     case tgtok::XIf: {
    1399             :       RecTy *MHSTy = nullptr;
    1400             :       RecTy *RHSTy = nullptr;
    1401             : 
    1402             :       if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
    1403       17042 :         MHSTy = MHSt->getType();
    1404             :       if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
    1405        1502 :         MHSTy = BitsRecTy::get(MHSbits->getNumBits());
    1406       17177 :       if (isa<BitInit>(MHS))
    1407             :         MHSTy = BitRecTy::get();
    1408             : 
    1409             :       if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
    1410       16147 :         RHSTy = RHSt->getType();
    1411             :       if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
    1412         146 :         RHSTy = BitsRecTy::get(RHSbits->getNumBits());
    1413       17177 :       if (isa<BitInit>(RHS))
    1414             :         RHSTy = BitRecTy::get();
    1415             : 
    1416             :       // For UnsetInit, it's typed from the other hand.
    1417       17177 :       if (isa<UnsetInit>(MHS))
    1418             :         MHSTy = RHSTy;
    1419       17177 :       if (isa<UnsetInit>(RHS))
    1420             :         RHSTy = MHSTy;
    1421             : 
    1422       17177 :       if (!MHSTy || !RHSTy) {
    1423           0 :         TokError("could not get type for !if");
    1424           0 :         return nullptr;
    1425             :       }
    1426             : 
    1427       17177 :       Type = resolveTypes(MHSTy, RHSTy);
    1428       17177 :       if (!Type) {
    1429           0 :         TokError(Twine("inconsistent types '") + MHSTy->getAsString() +
    1430           0 :                  "' and '" + RHSTy->getAsString() + "' for !if");
    1431           0 :         return nullptr;
    1432             :       }
    1433             :       break;
    1434             :     }
    1435         325 :     case tgtok::XSubst: {
    1436             :       TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
    1437             :       if (!RHSt) {
    1438           0 :         TokError("could not get type for !subst");
    1439           0 :         return nullptr;
    1440             :       }
    1441         325 :       Type = RHSt->getType();
    1442         325 :       break;
    1443             :     }
    1444             :     }
    1445       17509 :     return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec);
    1446             :   }
    1447             : 
    1448         652 :   case tgtok::XFoldl: {
    1449             :     // Value ::= !foldl '(' Id ',' Id ',' Value ',' Value ',' Value ')'
    1450         652 :     Lex.Lex(); // eat the operation
    1451         652 :     if (Lex.getCode() != tgtok::l_paren) {
    1452           0 :       TokError("expected '(' after !foldl");
    1453           0 :       return nullptr;
    1454             :     }
    1455             :     Lex.Lex(); // eat the '('
    1456             : 
    1457         652 :     Init *StartUntyped = ParseValue(CurRec);
    1458         652 :     if (!StartUntyped)
    1459             :       return nullptr;
    1460             : 
    1461             :     TypedInit *Start = dyn_cast<TypedInit>(StartUntyped);
    1462             :     if (!Start) {
    1463           0 :       TokError(Twine("could not get type of !foldl start: '") +
    1464           0 :                StartUntyped->getAsString() + "'");
    1465           0 :       return nullptr;
    1466             :     }
    1467             : 
    1468         652 :     if (Lex.getCode() != tgtok::comma) {
    1469           0 :       TokError("expected ',' in !foldl");
    1470           0 :       return nullptr;
    1471             :     }
    1472             :     Lex.Lex(); // eat the ','
    1473             : 
    1474         652 :     Init *ListUntyped = ParseValue(CurRec);
    1475         652 :     if (!ListUntyped)
    1476             :       return nullptr;
    1477             : 
    1478             :     TypedInit *List = dyn_cast<TypedInit>(ListUntyped);
    1479             :     if (!List) {
    1480           0 :       TokError(Twine("could not get type of !foldl list: '") +
    1481           0 :                ListUntyped->getAsString() + "'");
    1482           0 :       return nullptr;
    1483             :     }
    1484             : 
    1485         652 :     ListRecTy *ListType = dyn_cast<ListRecTy>(List->getType());
    1486             :     if (!ListType) {
    1487           0 :       TokError(Twine("!foldl list must be a list, but is of type '") +
    1488           0 :                List->getType()->getAsString());
    1489           0 :       return nullptr;
    1490             :     }
    1491             : 
    1492         652 :     if (Lex.getCode() != tgtok::comma) {
    1493           0 :       TokError("expected ',' in !foldl");
    1494           0 :       return nullptr;
    1495             :     }
    1496             : 
    1497         652 :     if (Lex.Lex() != tgtok::Id) { // eat the ','
    1498           0 :       TokError("third argument of !foldl must be an identifier");
    1499           0 :       return nullptr;
    1500             :     }
    1501             : 
    1502         652 :     Init *A = StringInit::get(Lex.getCurStrVal());
    1503        1293 :     if (CurRec && CurRec->getValue(A)) {
    1504           0 :       TokError((Twine("left !foldl variable '") + A->getAsString() +
    1505           0 :                 "' already defined")
    1506           0 :                    .str());
    1507           0 :       return nullptr;
    1508             :     }
    1509             : 
    1510         652 :     if (Lex.Lex() != tgtok::comma) { // eat the id
    1511           0 :       TokError("expected ',' in !foldl");
    1512           0 :       return nullptr;
    1513             :     }
    1514             : 
    1515         652 :     if (Lex.Lex() != tgtok::Id) { // eat the ','
    1516           0 :       TokError("fourth argument of !foldl must be an identifier");
    1517           0 :       return nullptr;
    1518             :     }
    1519             : 
    1520         652 :     Init *B = StringInit::get(Lex.getCurStrVal());
    1521        1293 :     if (CurRec && CurRec->getValue(B)) {
    1522           0 :       TokError((Twine("right !foldl variable '") + B->getAsString() +
    1523           0 :                 "' already defined")
    1524           0 :                    .str());
    1525           0 :       return nullptr;
    1526             :     }
    1527             : 
    1528         652 :     if (Lex.Lex() != tgtok::comma) { // eat the id
    1529           0 :       TokError("expected ',' in !foldl");
    1530           0 :       return nullptr;
    1531             :     }
    1532             :     Lex.Lex(); // eat the ','
    1533             : 
    1534             :     // We need to create a temporary record to provide a scope for the iteration
    1535             :     // variable while parsing top-level foreach's.
    1536         652 :     std::unique_ptr<Record> ParseRecTmp;
    1537             :     Record *ParseRec = CurRec;
    1538         652 :     if (!ParseRec) {
    1539          11 :       ParseRecTmp = make_unique<Record>(".parse", ArrayRef<SMLoc>{}, Records);
    1540             :       ParseRec = ParseRecTmp.get();
    1541             :     }
    1542             : 
    1543         652 :     ParseRec->addValue(RecordVal(A, Start->getType(), false));
    1544         652 :     ParseRec->addValue(RecordVal(B, ListType->getElementType(), false));
    1545         652 :     Init *ExprUntyped = ParseValue(ParseRec);
    1546         652 :     ParseRec->removeValue(A);
    1547         652 :     ParseRec->removeValue(B);
    1548         652 :     if (!ExprUntyped)
    1549             :       return nullptr;
    1550             : 
    1551             :     TypedInit *Expr = dyn_cast<TypedInit>(ExprUntyped);
    1552             :     if (!Expr) {
    1553           0 :       TokError("could not get type of !foldl expression");
    1554           0 :       return nullptr;
    1555             :     }
    1556             : 
    1557         652 :     if (Expr->getType() != Start->getType()) {
    1558           0 :       TokError(Twine("!foldl expression must be of same type as start (") +
    1559           0 :                Start->getType()->getAsString() + "), but is of type " +
    1560           0 :                Expr->getType()->getAsString());
    1561           0 :       return nullptr;
    1562             :     }
    1563             : 
    1564         652 :     if (Lex.getCode() != tgtok::r_paren) {
    1565           0 :       TokError("expected ')' in fold operator");
    1566           0 :       return nullptr;
    1567             :     }
    1568             :     Lex.Lex(); // eat the ')'
    1569             : 
    1570             :     return FoldOpInit::get(Start, List, A, B, Expr, Start->getType())
    1571         652 :         ->Fold(CurRec);
    1572             :   }
    1573             :   }
    1574             : }
    1575             : 
    1576             : /// ParseOperatorType - Parse a type for an operator.  This returns
    1577             : /// null on error.
    1578             : ///
    1579             : /// OperatorType ::= '<' Type '>'
    1580             : ///
    1581       26169 : RecTy *TGParser::ParseOperatorType() {
    1582             :   RecTy *Type = nullptr;
    1583             : 
    1584       26169 :   if (Lex.getCode() != tgtok::less) {
    1585           0 :     TokError("expected type name for operator");
    1586           0 :     return nullptr;
    1587             :   }
    1588       26169 :   Lex.Lex();  // eat the <
    1589             : 
    1590       26169 :   Type = ParseType();
    1591             : 
    1592       26169 :   if (!Type) {
    1593           0 :     TokError("expected type name for operator");
    1594           0 :     return nullptr;
    1595             :   }
    1596             : 
    1597       26169 :   if (Lex.getCode() != tgtok::greater) {
    1598           0 :     TokError("expected type name for operator");
    1599           0 :     return nullptr;
    1600             :   }
    1601             :   Lex.Lex();  // eat the >
    1602             : 
    1603       26169 :   return Type;
    1604             : }
    1605             : 
    1606             : /// ParseSimpleValue - Parse a tblgen value.  This returns null on error.
    1607             : ///
    1608             : ///   SimpleValue ::= IDValue
    1609             : ///   SimpleValue ::= INTVAL
    1610             : ///   SimpleValue ::= STRVAL+
    1611             : ///   SimpleValue ::= CODEFRAGMENT
    1612             : ///   SimpleValue ::= '?'
    1613             : ///   SimpleValue ::= '{' ValueList '}'
    1614             : ///   SimpleValue ::= ID '<' ValueListNE '>'
    1615             : ///   SimpleValue ::= '[' ValueList ']'
    1616             : ///   SimpleValue ::= '(' IDValue DagArgList ')'
    1617             : ///   SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
    1618             : ///   SimpleValue ::= ADDTOK '(' Value ',' Value ')'
    1619             : ///   SimpleValue ::= SHLTOK '(' Value ',' Value ')'
    1620             : ///   SimpleValue ::= SRATOK '(' Value ',' Value ')'
    1621             : ///   SimpleValue ::= SRLTOK '(' Value ',' Value ')'
    1622             : ///   SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
    1623             : ///   SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
    1624             : ///
    1625    18153440 : Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
    1626             :                                  IDParseMode Mode) {
    1627             :   Init *R = nullptr;
    1628    18153440 :   switch (Lex.getCode()) {
    1629           0 :   default: TokError("Unknown token when parsing a value"); break;
    1630       18034 :   case tgtok::paste:
    1631             :     // This is a leading paste operation.  This is deprecated but
    1632             :     // still exists in some .td files.  Ignore it.
    1633       18034 :     Lex.Lex();  // Skip '#'.
    1634       18034 :     return ParseSimpleValue(CurRec, ItemType, Mode);
    1635     1279630 :   case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
    1636      261719 :   case tgtok::BinaryIntVal: {
    1637             :     auto BinaryVal = Lex.getCurBinaryIntVal();
    1638      261719 :     SmallVector<Init*, 16> Bits(BinaryVal.second);
    1639     1337245 :     for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
    1640     2151052 :       Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
    1641      261719 :     R = BitsInit::get(Bits);
    1642      261719 :     Lex.Lex();
    1643             :     break;
    1644             :   }
    1645     1983727 :   case tgtok::StrVal: {
    1646             :     std::string Val = Lex.getCurStrVal();
    1647     1983727 :     Lex.Lex();
    1648             : 
    1649             :     // Handle multiple consecutive concatenated strings.
    1650     2015819 :     while (Lex.getCode() == tgtok::StrVal) {
    1651             :       Val += Lex.getCurStrVal();
    1652             :       Lex.Lex();
    1653             :     }
    1654             : 
    1655     1983727 :     R = StringInit::get(Val);
    1656             :     break;
    1657             :   }
    1658       20730 :   case tgtok::CodeFragment:
    1659       20730 :     R = CodeInit::get(Lex.getCurStrVal());
    1660       20730 :     Lex.Lex();
    1661             :     break;
    1662       24561 :   case tgtok::question:
    1663       24561 :     R = UnsetInit::get();
    1664       24561 :     Lex.Lex();
    1665             :     break;
    1666    10357572 :   case tgtok::Id: {
    1667    10357572 :     SMLoc NameLoc = Lex.getLoc();
    1668    10357572 :     StringInit *Name = StringInit::get(Lex.getCurStrVal());
    1669    10357572 :     if (Lex.Lex() != tgtok::less)  // consume the Id.
    1670    10046155 :       return ParseIDValue(CurRec, Name, NameLoc, Mode);    // Value ::= IDValue
    1671             : 
    1672             :     // Value ::= ID '<' ValueListNE '>'
    1673      311417 :     if (Lex.Lex() == tgtok::greater) {
    1674           0 :       TokError("expected non-empty value list");
    1675           0 :       return nullptr;
    1676             :     }
    1677             : 
    1678             :     // This is a CLASS<initvalslist> expression.  This is supposed to synthesize
    1679             :     // a new anonymous definition, deriving from CLASS<initvalslist> with no
    1680             :     // body.
    1681      311417 :     Record *Class = Records.getClass(Name->getValue());
    1682      311417 :     if (!Class) {
    1683           0 :       Error(NameLoc, "Expected a class name, got '" + Name->getValue() + "'");
    1684           0 :       return nullptr;
    1685             :     }
    1686             : 
    1687             :     SmallVector<Init *, 8> Args;
    1688      311417 :     ParseValueList(Args, CurRec, Class);
    1689      311417 :     if (Args.empty()) return nullptr;
    1690             : 
    1691      311417 :     if (Lex.getCode() != tgtok::greater) {
    1692           0 :       TokError("expected '>' at end of value list");
    1693           0 :       return nullptr;
    1694             :     }
    1695             :     Lex.Lex();  // eat the '>'
    1696             : 
    1697             :     // Typecheck the template arguments list
    1698             :     ArrayRef<Init *> ExpectedArgs = Class->getTemplateArgs();
    1699      311417 :     if (ExpectedArgs.size() < Args.size()) {
    1700           0 :       Error(NameLoc,
    1701             :             "More template args specified than expected");
    1702           0 :       return nullptr;
    1703             :     }
    1704             : 
    1705     1033295 :     for (unsigned i = 0, e = ExpectedArgs.size(); i != e; ++i) {
    1706     1443756 :       RecordVal *ExpectedArg = Class->getValue(ExpectedArgs[i]);
    1707      721878 :       if (i < Args.size()) {
    1708      545190 :         if (TypedInit *TI = dyn_cast<TypedInit>(Args[i])) {
    1709             :           RecTy *ExpectedType = ExpectedArg->getType();
    1710      545190 :           if (!TI->getType()->typeIsConvertibleTo(ExpectedType)) {
    1711           0 :             Error(NameLoc,
    1712           0 :                   "Value specified for template argument #" + Twine(i) + " (" +
    1713           0 :                   ExpectedArg->getNameInitAsString() + ") is of type '" +
    1714           0 :                   TI->getType()->getAsString() + "', expected '" +
    1715           0 :                   ExpectedType->getAsString() + "': " + TI->getAsString());
    1716           0 :             return nullptr;
    1717             :           }
    1718             :           continue;
    1719             :         }
    1720      176688 :       } else if (ExpectedArg->getValue()->isComplete())
    1721             :         continue;
    1722             : 
    1723           0 :       Error(NameLoc,
    1724           0 :             "Value not specified for template argument #" + Twine(i) + " (" +
    1725           0 :             ExpectedArgs[i]->getAsUnquotedString() + ")");
    1726           0 :       return nullptr;
    1727             :     }
    1728             : 
    1729      311417 :     return VarDefInit::get(Class, Args)->Fold();
    1730             :   }
    1731        8942 :   case tgtok::l_brace: {           // Value ::= '{' ValueList '}'
    1732        8942 :     SMLoc BraceLoc = Lex.getLoc();
    1733             :     Lex.Lex(); // eat the '{'
    1734             :     SmallVector<Init*, 16> Vals;
    1735             : 
    1736        8942 :     if (Lex.getCode() != tgtok::r_brace) {
    1737        8942 :       ParseValueList(Vals, CurRec);
    1738        8942 :       if (Vals.empty()) return nullptr;
    1739             :     }
    1740        8942 :     if (Lex.getCode() != tgtok::r_brace) {
    1741           0 :       TokError("expected '}' at end of bit list value");
    1742           0 :       return nullptr;
    1743             :     }
    1744             :     Lex.Lex();  // eat the '}'
    1745             : 
    1746             :     SmallVector<Init *, 16> NewBits;
    1747             : 
    1748             :     // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
    1749             :     // first.  We'll first read everything in to a vector, then we can reverse
    1750             :     // it to get the bits in the correct order for the BitsInit value.
    1751       45281 :     for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
    1752             :       // FIXME: The following two loops would not be duplicated
    1753             :       //        if the API was a little more orthogonal.
    1754             : 
    1755             :       // bits<n> values are allowed to initialize n bits.
    1756       72678 :       if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
    1757        6343 :         for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
    1758        8016 :           NewBits.push_back(BI->getBit((e - i) - 1));
    1759        3270 :         continue;
    1760             :       }
    1761             :       // bits<n> can also come from variable initializers.
    1762             :       if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
    1763        2070 :         if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
    1764        3731 :           for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
    1765        2796 :             NewBits.push_back(VI->getBit((e - i) - 1));
    1766             :           continue;
    1767             :         }
    1768             :         // Fallthrough to try convert this to a bit.
    1769             :       }
    1770             :       // All other values must be convertible to just a single bit.
    1771       33069 :       Init *Bit = Vals[i]->getCastTo(BitRecTy::get());
    1772       33069 :       if (!Bit) {
    1773           0 :         Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
    1774           0 :               ") is not convertable to a bit");
    1775           0 :         return nullptr;
    1776             :       }
    1777       33069 :       NewBits.push_back(Bit);
    1778             :     }
    1779             :     std::reverse(NewBits.begin(), NewBits.end());
    1780        8942 :     return BitsInit::get(NewBits);
    1781             :   }
    1782     3004519 :   case tgtok::l_square: {          // Value ::= '[' ValueList ']'
    1783     3004519 :     Lex.Lex(); // eat the '['
    1784             :     SmallVector<Init*, 16> Vals;
    1785             : 
    1786             :     RecTy *DeducedEltTy = nullptr;
    1787             :     ListRecTy *GivenListTy = nullptr;
    1788             : 
    1789     3004519 :     if (ItemType) {
    1790             :       ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
    1791             :       if (!ListType) {
    1792           0 :         TokError(Twine("Type mismatch for list, expected list type, got ") +
    1793           0 :                  ItemType->getAsString());
    1794           0 :         return nullptr;
    1795             :       }
    1796             :       GivenListTy = ListType;
    1797             :     }
    1798             : 
    1799     3004519 :     if (Lex.getCode() != tgtok::r_square) {
    1800     5204751 :       ParseValueList(Vals, CurRec, nullptr,
    1801     2583468 :                      GivenListTy ? GivenListTy->getElementType() : nullptr);
    1802     2621283 :       if (Vals.empty()) return nullptr;
    1803             :     }
    1804     3004519 :     if (Lex.getCode() != tgtok::r_square) {
    1805           0 :       TokError("expected ']' at end of list value");
    1806           0 :       return nullptr;
    1807             :     }
    1808             :     Lex.Lex();  // eat the ']'
    1809             : 
    1810             :     RecTy *GivenEltTy = nullptr;
    1811     3004519 :     if (Lex.getCode() == tgtok::less) {
    1812             :       // Optional list element type
    1813             :       Lex.Lex();  // eat the '<'
    1814             : 
    1815         484 :       GivenEltTy = ParseType();
    1816         484 :       if (!GivenEltTy) {
    1817             :         // Couldn't parse element type
    1818             :         return nullptr;
    1819             :       }
    1820             : 
    1821         484 :       if (Lex.getCode() != tgtok::greater) {
    1822           0 :         TokError("expected '>' at end of list element type");
    1823           0 :         return nullptr;
    1824             :       }
    1825             :       Lex.Lex();  // eat the '>'
    1826             :     }
    1827             : 
    1828             :     // Check elements
    1829             :     RecTy *EltTy = nullptr;
    1830     7708046 :     for (Init *V : Vals) {
    1831             :       TypedInit *TArg = dyn_cast<TypedInit>(V);
    1832             :       if (TArg) {
    1833     4703525 :         if (EltTy) {
    1834     2082242 :           EltTy = resolveTypes(EltTy, TArg->getType());
    1835     2082242 :           if (!EltTy) {
    1836           0 :             TokError("Incompatible types in list elements");
    1837           0 :             return nullptr;
    1838             :           }
    1839             :         } else {
    1840     2621283 :           EltTy = TArg->getType();
    1841             :         }
    1842             :       }
    1843             :     }
    1844             : 
    1845     3004519 :     if (GivenEltTy) {
    1846         484 :       if (EltTy) {
    1847             :         // Verify consistency
    1848           1 :         if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
    1849           0 :           TokError("Incompatible types in list elements");
    1850           0 :           return nullptr;
    1851             :         }
    1852             :       }
    1853             :       EltTy = GivenEltTy;
    1854             :     }
    1855             : 
    1856     3004519 :     if (!EltTy) {
    1857      382753 :       if (!ItemType) {
    1858           0 :         TokError("No type for list");
    1859           0 :         return nullptr;
    1860             :       }
    1861      382753 :       DeducedEltTy = GivenListTy->getElementType();
    1862             :     } else {
    1863             :       // Make sure the deduced type is compatible with the given type
    1864     2621766 :       if (GivenListTy) {
    1865     2583669 :         if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
    1866           0 :           TokError(Twine("Element type mismatch for list: element type '") +
    1867           0 :                    EltTy->getAsString() + "' not convertible to '" +
    1868           0 :                    GivenListTy->getElementType()->getAsString());
    1869           0 :           return nullptr;
    1870             :         }
    1871             :       }
    1872             :       DeducedEltTy = EltTy;
    1873             :     }
    1874             : 
    1875     3004519 :     return ListInit::get(Vals, DeducedEltTy);
    1876             :   }
    1877     1107498 :   case tgtok::l_paren: {         // Value ::= '(' IDValue DagArgList ')'
    1878     1107498 :     Lex.Lex();   // eat the '('
    1879     1107498 :     if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
    1880           0 :       TokError("expected identifier in dag init");
    1881           0 :       return nullptr;
    1882             :     }
    1883             : 
    1884     1107498 :     Init *Operator = ParseValue(CurRec);
    1885     1107498 :     if (!Operator) return nullptr;
    1886             : 
    1887             :     // If the operator name is present, parse it.
    1888             :     StringInit *OperatorName = nullptr;
    1889     1107498 :     if (Lex.getCode() == tgtok::colon) {
    1890        1301 :       if (Lex.Lex() != tgtok::VarName) { // eat the ':'
    1891           0 :         TokError("expected variable name in dag operator");
    1892           0 :         return nullptr;
    1893             :       }
    1894        1301 :       OperatorName = StringInit::get(Lex.getCurStrVal());
    1895             :       Lex.Lex();  // eat the VarName.
    1896             :     }
    1897             : 
    1898             :     SmallVector<std::pair<llvm::Init*, StringInit*>, 8> DagArgs;
    1899     1107498 :     if (Lex.getCode() != tgtok::r_paren) {
    1900     1039385 :       ParseDagArgList(DagArgs, CurRec);
    1901     1039385 :       if (DagArgs.empty()) return nullptr;
    1902             :     }
    1903             : 
    1904     1107498 :     if (Lex.getCode() != tgtok::r_paren) {
    1905           0 :       TokError("expected ')' in dag init");
    1906           0 :       return nullptr;
    1907             :     }
    1908             :     Lex.Lex();  // eat the ')'
    1909             : 
    1910     1107498 :     return DagInit::get(Operator, OperatorName, DagArgs);
    1911             :   }
    1912             : 
    1913       86508 :   case tgtok::XHead:
    1914             :   case tgtok::XTail:
    1915             :   case tgtok::XSize:
    1916             :   case tgtok::XEmpty:
    1917             :   case tgtok::XCast:  // Value ::= !unop '(' Value ')'
    1918             :   case tgtok::XIsA:
    1919             :   case tgtok::XConcat:
    1920             :   case tgtok::XDag:
    1921             :   case tgtok::XADD:
    1922             :   case tgtok::XAND:
    1923             :   case tgtok::XOR:
    1924             :   case tgtok::XSRA:
    1925             :   case tgtok::XSRL:
    1926             :   case tgtok::XSHL:
    1927             :   case tgtok::XEq:
    1928             :   case tgtok::XNe:
    1929             :   case tgtok::XLe:
    1930             :   case tgtok::XLt:
    1931             :   case tgtok::XGe:
    1932             :   case tgtok::XGt:
    1933             :   case tgtok::XListConcat:
    1934             :   case tgtok::XStrConcat:   // Value ::= !binop '(' Value ',' Value ')'
    1935             :   case tgtok::XIf:
    1936             :   case tgtok::XFoldl:
    1937             :   case tgtok::XForEach:
    1938             :   case tgtok::XSubst: {  // Value ::= !ternop '(' Value ',' Value ',' Value ')'
    1939       86508 :     return ParseOperation(CurRec, ItemType);
    1940             :   }
    1941             :   }
    1942             : 
    1943             :   return R;
    1944             : }
    1945             : 
    1946             : /// ParseValue - Parse a tblgen value.  This returns null on error.
    1947             : ///
    1948             : ///   Value       ::= SimpleValue ValueSuffix*
    1949             : ///   ValueSuffix ::= '{' BitList '}'
    1950             : ///   ValueSuffix ::= '[' BitList ']'
    1951             : ///   ValueSuffix ::= '.' ID
    1952             : ///
    1953    18135406 : Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
    1954    18135406 :   Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
    1955    18135406 :   if (!Result) return nullptr;
    1956             : 
    1957             :   // Parse the suffixes now if present.
    1958             :   while (true) {
    1959    18400441 :     switch (Lex.getCode()) {
    1960    18135406 :     default: return Result;
    1961       46808 :     case tgtok::l_brace: {
    1962       46808 :       if (Mode == ParseNameMode)
    1963             :         // This is the beginning of the object body.
    1964         945 :         return Result;
    1965             : 
    1966       45863 :       SMLoc CurlyLoc = Lex.getLoc();
    1967             :       Lex.Lex(); // eat the '{'
    1968             :       SmallVector<unsigned, 16> Ranges;
    1969       45863 :       ParseRangeList(Ranges);
    1970       45863 :       if (Ranges.empty()) return nullptr;
    1971             : 
    1972             :       // Reverse the bitlist.
    1973             :       std::reverse(Ranges.begin(), Ranges.end());
    1974       91726 :       Result = Result->convertInitializerBitRange(Ranges);
    1975       45863 :       if (!Result) {
    1976           0 :         Error(CurlyLoc, "Invalid bit range for value");
    1977           0 :         return nullptr;
    1978             :       }
    1979             : 
    1980             :       // Eat the '}'.
    1981       45863 :       if (Lex.getCode() != tgtok::r_brace) {
    1982           0 :         TokError("expected '}' at end of bit range list");
    1983           0 :         return nullptr;
    1984             :       }
    1985             :       Lex.Lex();
    1986             :       break;
    1987             :     }
    1988        1467 :     case tgtok::l_square: {
    1989        1467 :       SMLoc SquareLoc = Lex.getLoc();
    1990             :       Lex.Lex(); // eat the '['
    1991             :       SmallVector<unsigned, 16> Ranges;
    1992        1467 :       ParseRangeList(Ranges);
    1993        1467 :       if (Ranges.empty()) return nullptr;
    1994             : 
    1995        2934 :       Result = Result->convertInitListSlice(Ranges);
    1996        1467 :       if (!Result) {
    1997           0 :         Error(SquareLoc, "Invalid range for list slice");
    1998           0 :         return nullptr;
    1999             :       }
    2000             : 
    2001             :       // Eat the ']'.
    2002        1467 :       if (Lex.getCode() != tgtok::r_square) {
    2003           0 :         TokError("expected ']' at end of list slice");
    2004           0 :         return nullptr;
    2005             :       }
    2006             :       Lex.Lex();
    2007             :       break;
    2008             :     }
    2009      123623 :     case tgtok::period: {
    2010      123623 :       if (Lex.Lex() != tgtok::Id) {  // eat the .
    2011           0 :         TokError("expected field identifier after '.'");
    2012           0 :         return nullptr;
    2013             :       }
    2014      123623 :       StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
    2015      123623 :       if (!Result->getFieldType(FieldName)) {
    2016           0 :         TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
    2017           0 :                  Result->getAsString() + "'");
    2018           0 :         return nullptr;
    2019             :       }
    2020      123623 :       Result = FieldInit::get(Result, FieldName)->Fold(CurRec);
    2021             :       Lex.Lex();  // eat field name
    2022             :       break;
    2023             :     }
    2024             : 
    2025       94082 :     case tgtok::paste:
    2026       94082 :       SMLoc PasteLoc = Lex.getLoc();
    2027             : 
    2028             :       // Create a !strconcat() operation, first casting each operand to
    2029             :       // a string if necessary.
    2030             : 
    2031             :       TypedInit *LHS = dyn_cast<TypedInit>(Result);
    2032             :       if (!LHS) {
    2033           0 :         Error(PasteLoc, "LHS of paste is not typed!");
    2034           0 :         return nullptr;
    2035             :       }
    2036             : 
    2037       94082 :       if (LHS->getType() != StringRecTy::get()) {
    2038        5708 :         LHS = dyn_cast<TypedInit>(
    2039             :             UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get())
    2040             :                 ->Fold(CurRec));
    2041             :         if (!LHS) {
    2042           0 :           Error(PasteLoc, Twine("can't cast '") + LHS->getAsString() +
    2043           0 :                               "' to string");
    2044           0 :           return nullptr;
    2045             :         }
    2046             :       }
    2047             : 
    2048             :       TypedInit *RHS = nullptr;
    2049             : 
    2050             :       Lex.Lex();  // Eat the '#'.
    2051             :       switch (Lex.getCode()) {
    2052             :       case tgtok::colon:
    2053             :       case tgtok::semi:
    2054             :       case tgtok::l_brace:
    2055             :         // These are all of the tokens that can begin an object body.
    2056             :         // Some of these can also begin values but we disallow those cases
    2057             :         // because they are unlikely to be useful.
    2058             : 
    2059             :         // Trailing paste, concat with an empty string.
    2060          20 :         RHS = StringInit::get("");
    2061          20 :         break;
    2062             : 
    2063       94062 :       default:
    2064       94062 :         Init *RHSResult = ParseValue(CurRec, nullptr, ParseNameMode);
    2065             :         RHS = dyn_cast<TypedInit>(RHSResult);
    2066             :         if (!RHS) {
    2067           0 :           Error(PasteLoc, "RHS of paste is not typed!");
    2068           0 :           return nullptr;
    2069             :         }
    2070             : 
    2071       94062 :         if (RHS->getType() != StringRecTy::get()) {
    2072        5472 :           RHS = dyn_cast<TypedInit>(
    2073             :               UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get())
    2074             :                   ->Fold(CurRec));
    2075             :           if (!RHS) {
    2076           0 :             Error(PasteLoc, Twine("can't cast '") + RHS->getAsString() +
    2077           0 :                                 "' to string");
    2078           0 :             return nullptr;
    2079             :           }
    2080             :         }
    2081             : 
    2082             :         break;
    2083             :       }
    2084             : 
    2085       94082 :       Result = BinOpInit::getStrConcat(LHS, RHS);
    2086       94082 :       break;
    2087             :     }
    2088      265035 :   }
    2089             : }
    2090             : 
    2091             : /// ParseDagArgList - Parse the argument list for a dag literal expression.
    2092             : ///
    2093             : ///    DagArg     ::= Value (':' VARNAME)?
    2094             : ///    DagArg     ::= VARNAME
    2095             : ///    DagArgList ::= DagArg
    2096             : ///    DagArgList ::= DagArgList ',' DagArg
    2097     1039385 : void TGParser::ParseDagArgList(
    2098             :     SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
    2099             :     Record *CurRec) {
    2100             : 
    2101             :   while (true) {
    2102             :     // DagArg ::= VARNAME
    2103     1764953 :     if (Lex.getCode() == tgtok::VarName) {
    2104             :       // A missing value is treated like '?'.
    2105       38741 :       StringInit *VarName = StringInit::get(Lex.getCurStrVal());
    2106       38741 :       Result.emplace_back(UnsetInit::get(), VarName);
    2107       38741 :       Lex.Lex();
    2108             :     } else {
    2109             :       // DagArg ::= Value (':' VARNAME)?
    2110     1726212 :       Init *Val = ParseValue(CurRec);
    2111     1726212 :       if (!Val) {
    2112             :         Result.clear();
    2113           0 :         return;
    2114             :       }
    2115             : 
    2116             :       // If the variable name is present, add it.
    2117             :       StringInit *VarName = nullptr;
    2118     1726212 :       if (Lex.getCode() == tgtok::colon) {
    2119     1045397 :         if (Lex.Lex() != tgtok::VarName) { // eat the ':'
    2120           0 :           TokError("expected variable name in dag literal");
    2121             :           Result.clear();
    2122           0 :           return;
    2123             :         }
    2124     1045397 :         VarName = StringInit::get(Lex.getCurStrVal());
    2125             :         Lex.Lex();  // eat the VarName.
    2126             :       }
    2127             : 
    2128     1726212 :       Result.push_back(std::make_pair(Val, VarName));
    2129             :     }
    2130     1764953 :     if (Lex.getCode() != tgtok::comma) break;
    2131      725568 :     Lex.Lex(); // eat the ','
    2132             :   }
    2133             : }
    2134             : 
    2135             : /// ParseValueList - Parse a comma separated list of values, returning them as a
    2136             : /// vector.  Note that this always expects to be able to parse at least one
    2137             : /// value.  It returns an empty list if this is not possible.
    2138             : ///
    2139             : ///   ValueList ::= Value (',' Value)
    2140             : ///
    2141     5863447 : void TGParser::ParseValueList(SmallVectorImpl<Init*> &Result, Record *CurRec,
    2142             :                               Record *ArgsRec, RecTy *EltTy) {
    2143             :   RecTy *ItemType = EltTy;
    2144             :   unsigned int ArgN = 0;
    2145     5863447 :   if (ArgsRec && !EltTy) {
    2146             :     ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
    2147     3233222 :     if (TArgs.empty()) {
    2148           0 :       TokError("template argument provided to non-template class");
    2149             :       Result.clear();
    2150             :       return;
    2151             :     }
    2152     3233222 :     const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
    2153     3233222 :     if (!RV) {
    2154           0 :       errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
    2155           0 :         << ")\n";
    2156             :     }
    2157             :     assert(RV && "Template argument record not found??");
    2158             :     ItemType = RV->getType();
    2159             :     ++ArgN;
    2160             :   }
    2161     5863447 :   Result.push_back(ParseValue(CurRec, ItemType));
    2162     5863447 :   if (!Result.back()) {
    2163             :     Result.clear();
    2164           0 :     return;
    2165             :   }
    2166             : 
    2167    12153145 :   while (Lex.getCode() == tgtok::comma) {
    2168     6289698 :     Lex.Lex();  // Eat the comma
    2169             : 
    2170     6289698 :     if (ArgsRec && !EltTy) {
    2171             :       ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
    2172     4180057 :       if (ArgN >= TArgs.size()) {
    2173           0 :         TokError("too many template arguments");
    2174             :         Result.clear();
    2175             :         return;
    2176             :       }
    2177     4180057 :       const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
    2178             :       assert(RV && "Template argument record not found??");
    2179             :       ItemType = RV->getType();
    2180     4180057 :       ++ArgN;
    2181             :     }
    2182     6289698 :     Result.push_back(ParseValue(CurRec, ItemType));
    2183     6289698 :     if (!Result.back()) {
    2184             :       Result.clear();
    2185           0 :       return;
    2186             :     }
    2187             :   }
    2188             : }
    2189             : 
    2190             : /// ParseDeclaration - Read a declaration, returning the name of field ID, or an
    2191             : /// empty string on error.  This can happen in a number of different context's,
    2192             : /// including within a def or in the template args for a def (which which case
    2193             : /// CurRec will be non-null) and within the template args for a multiclass (in
    2194             : /// which case CurRec will be null, but CurMultiClass will be set).  This can
    2195             : /// also happen within a def that is within a multiclass, which will set both
    2196             : /// CurRec and CurMultiClass.
    2197             : ///
    2198             : ///  Declaration ::= FIELD? Type ID ('=' Value)?
    2199             : ///
    2200      616868 : Init *TGParser::ParseDeclaration(Record *CurRec,
    2201             :                                        bool ParsingTemplateArgs) {
    2202             :   // Read the field prefix if present.
    2203      616868 :   bool HasField = Lex.getCode() == tgtok::Field;
    2204      616868 :   if (HasField) Lex.Lex();
    2205             : 
    2206      616868 :   RecTy *Type = ParseType();
    2207      616868 :   if (!Type) return nullptr;
    2208             : 
    2209      616868 :   if (Lex.getCode() != tgtok::Id) {
    2210           0 :     TokError("Expected identifier in declaration");
    2211           0 :     return nullptr;
    2212             :   }
    2213             : 
    2214             :   std::string Str = Lex.getCurStrVal();
    2215      616868 :   if (Str == "NAME") {
    2216           0 :     TokError("'" + Str + "' is a reserved variable name");
    2217           0 :     return nullptr;
    2218             :   }
    2219             : 
    2220      616868 :   SMLoc IdLoc = Lex.getLoc();
    2221      616868 :   Init *DeclName = StringInit::get(Str);
    2222             :   Lex.Lex();
    2223             : 
    2224      616868 :   if (ParsingTemplateArgs) {
    2225      418452 :     if (CurRec)
    2226      309385 :       DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
    2227             :     else
    2228             :       assert(CurMultiClass);
    2229      418452 :     if (CurMultiClass)
    2230      109067 :       DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
    2231             :                              "::");
    2232             :   }
    2233             : 
    2234             :   // Add the value.
    2235      616868 :   if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
    2236             :     return nullptr;
    2237             : 
    2238             :   // If a value is present, parse it.
    2239      616868 :   if (Lex.getCode() == tgtok::equal) {
    2240             :     Lex.Lex();
    2241      153948 :     SMLoc ValLoc = Lex.getLoc();
    2242      153948 :     Init *Val = ParseValue(CurRec, Type);
    2243      307896 :     if (!Val ||
    2244      307887 :         SetValue(CurRec, ValLoc, DeclName, None, Val))
    2245             :       // Return the name, even if an error is thrown.  This is so that we can
    2246             :       // continue to make some progress, even without the value having been
    2247             :       // initialized.
    2248           9 :       return DeclName;
    2249             :   }
    2250             : 
    2251             :   return DeclName;
    2252             : }
    2253             : 
    2254             : /// ParseForeachDeclaration - Read a foreach declaration, returning
    2255             : /// the name of the declared object or a NULL Init on error.  Return
    2256             : /// the name of the parsed initializer list through ForeachListName.
    2257             : ///
    2258             : ///  ForeachDeclaration ::= ID '=' '{' RangeList '}'
    2259             : ///  ForeachDeclaration ::= ID '=' RangePiece
    2260             : ///  ForeachDeclaration ::= ID '=' Value
    2261             : ///
    2262        3435 : VarInit *TGParser::ParseForeachDeclaration(Init *&ForeachListValue) {
    2263        3435 :   if (Lex.getCode() != tgtok::Id) {
    2264           0 :     TokError("Expected identifier in foreach declaration");
    2265           0 :     return nullptr;
    2266             :   }
    2267             : 
    2268        3435 :   Init *DeclName = StringInit::get(Lex.getCurStrVal());
    2269        3435 :   Lex.Lex();
    2270             : 
    2271             :   // If a value is present, parse it.
    2272        3435 :   if (Lex.getCode() != tgtok::equal) {
    2273           0 :     TokError("Expected '=' in foreach declaration");
    2274           0 :     return nullptr;
    2275             :   }
    2276             :   Lex.Lex();  // Eat the '='
    2277             : 
    2278             :   RecTy *IterType = nullptr;
    2279             :   SmallVector<unsigned, 16> Ranges;
    2280             : 
    2281        3435 :   switch (Lex.getCode()) {
    2282        1062 :   case tgtok::IntVal: { // RangePiece.
    2283        1062 :     if (ParseRangePiece(Ranges))
    2284             :       return nullptr;
    2285             :     break;
    2286             :   }
    2287             : 
    2288         247 :   case tgtok::l_brace: { // '{' RangeList '}'
    2289             :     Lex.Lex(); // eat the '{'
    2290         247 :     ParseRangeList(Ranges);
    2291         247 :     if (Lex.getCode() != tgtok::r_brace) {
    2292           0 :       TokError("expected '}' at end of bit range list");
    2293           0 :       return nullptr;
    2294             :     }
    2295             :     Lex.Lex();
    2296             :     break;
    2297             :   }
    2298             : 
    2299        2126 :   default: {
    2300        2126 :     SMLoc ValueLoc = Lex.getLoc();
    2301        2126 :     Init *I = ParseValue(nullptr);
    2302             :     TypedInit *TI = dyn_cast<TypedInit>(I);
    2303        4252 :     if (!TI || !isa<ListRecTy>(TI->getType())) {
    2304             :       std::string Type;
    2305           0 :       if (TI)
    2306           0 :         Type = (Twine("' of type '") + TI->getType()->getAsString()).str();
    2307           0 :       Error(ValueLoc, "expected a list, got '" + I->getAsString() + Type + "'");
    2308           0 :       if (CurMultiClass)
    2309           0 :         PrintNote({}, "references to multiclass template arguments cannot be "
    2310           0 :                       "resolved at this time");
    2311             :       return nullptr;
    2312             :     }
    2313        2126 :     ForeachListValue = I;
    2314        2126 :     IterType = cast<ListRecTy>(TI->getType())->getElementType();
    2315        2126 :     break;
    2316             :   }
    2317             :   }
    2318             : 
    2319        3435 :   if (!Ranges.empty()) {
    2320             :     assert(!IterType && "Type already initialized?");
    2321             :     IterType = IntRecTy::get();
    2322             :     std::vector<Init*> Values;
    2323       27990 :     for (unsigned R : Ranges)
    2324       26681 :       Values.push_back(IntInit::get(R));
    2325        1309 :     ForeachListValue = ListInit::get(Values, IterType);
    2326             :   }
    2327             : 
    2328        3435 :   if (!IterType)
    2329             :     return nullptr;
    2330             : 
    2331        3435 :   return VarInit::get(DeclName, IterType);
    2332             : }
    2333             : 
    2334             : /// ParseTemplateArgList - Read a template argument list, which is a non-empty
    2335             : /// sequence of template-declarations in <>'s.  If CurRec is non-null, these are
    2336             : /// template args for a def, which may or may not be in a multiclass.  If null,
    2337             : /// these are the template args for a multiclass.
    2338             : ///
    2339             : ///    TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
    2340             : ///
    2341      165881 : bool TGParser::ParseTemplateArgList(Record *CurRec) {
    2342             :   assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
    2343      165881 :   Lex.Lex(); // eat the '<'
    2344             : 
    2345      165881 :   Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
    2346             : 
    2347             :   // Read the first declaration.
    2348      165881 :   Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
    2349      165881 :   if (!TemplArg)
    2350             :     return true;
    2351             : 
    2352      165881 :   TheRecToAddTo->addTemplateArg(TemplArg);
    2353             : 
    2354      418452 :   while (Lex.getCode() == tgtok::comma) {
    2355             :     Lex.Lex(); // eat the ','
    2356             : 
    2357             :     // Read the following declarations.
    2358      252571 :     SMLoc Loc = Lex.getLoc();
    2359      252571 :     TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
    2360      252571 :     if (!TemplArg)
    2361           0 :       return true;
    2362             : 
    2363      252571 :     if (TheRecToAddTo->isTemplateArg(TemplArg))
    2364           0 :       return Error(Loc, "template argument with the same name has already been "
    2365             :                         "defined");
    2366             : 
    2367      252571 :     TheRecToAddTo->addTemplateArg(TemplArg);
    2368             :   }
    2369             : 
    2370      165881 :   if (Lex.getCode() != tgtok::greater)
    2371           0 :     return TokError("expected '>' at end of template argument list");
    2372             :   Lex.Lex(); // eat the '>'.
    2373      165881 :   return false;
    2374             : }
    2375             : 
    2376             : /// ParseBodyItem - Parse a single item at within the body of a def or class.
    2377             : ///
    2378             : ///   BodyItem ::= Declaration ';'
    2379             : ///   BodyItem ::= LET ID OptionalBitList '=' Value ';'
    2380      887764 : bool TGParser::ParseBodyItem(Record *CurRec) {
    2381      887764 :   if (Lex.getCode() != tgtok::Let) {
    2382      198416 :     if (!ParseDeclaration(CurRec, false))
    2383             :       return true;
    2384             : 
    2385      198416 :     if (Lex.getCode() != tgtok::semi)
    2386           0 :       return TokError("expected ';' after declaration");
    2387      198416 :     Lex.Lex();
    2388      198416 :     return false;
    2389             :   }
    2390             : 
    2391             :   // LET ID OptionalRangeList '=' Value ';'
    2392      689348 :   if (Lex.Lex() != tgtok::Id)
    2393           0 :     return TokError("expected field identifier after let");
    2394             : 
    2395      689348 :   SMLoc IdLoc = Lex.getLoc();
    2396      689348 :   StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
    2397             :   Lex.Lex();  // eat the field name.
    2398             : 
    2399             :   SmallVector<unsigned, 16> BitList;
    2400      689348 :   if (ParseOptionalBitList(BitList))
    2401             :     return true;
    2402             :   std::reverse(BitList.begin(), BitList.end());
    2403             : 
    2404      689348 :   if (Lex.getCode() != tgtok::equal)
    2405           0 :     return TokError("expected '=' in let expression");
    2406             :   Lex.Lex();  // eat the '='.
    2407             : 
    2408             :   RecordVal *Field = CurRec->getValue(FieldName);
    2409      689348 :   if (!Field)
    2410           0 :     return TokError("Value '" + FieldName->getValue() + "' unknown!");
    2411             : 
    2412             :   RecTy *Type = Field->getType();
    2413             : 
    2414      689348 :   Init *Val = ParseValue(CurRec, Type);
    2415      689348 :   if (!Val) return true;
    2416             : 
    2417      689348 :   if (Lex.getCode() != tgtok::semi)
    2418           0 :     return TokError("expected ';' after let expression");
    2419             :   Lex.Lex();
    2420             : 
    2421      689348 :   return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
    2422             : }
    2423             : 
    2424             : /// ParseBody - Read the body of a class or def.  Return true on error, false on
    2425             : /// success.
    2426             : ///
    2427             : ///   Body     ::= ';'
    2428             : ///   Body     ::= '{' BodyList '}'
    2429             : ///   BodyList BodyItem*
    2430             : ///
    2431     2277671 : bool TGParser::ParseBody(Record *CurRec) {
    2432             :   // If this is a null definition, just eat the semi and return.
    2433     2277671 :   if (Lex.getCode() == tgtok::semi) {
    2434     2053136 :     Lex.Lex();
    2435     2053136 :     return false;
    2436             :   }
    2437             : 
    2438      224535 :   if (Lex.getCode() != tgtok::l_brace)
    2439           0 :     return TokError("Expected ';' or '{' to start body");
    2440             :   // Eat the '{'.
    2441      224535 :   Lex.Lex();
    2442             : 
    2443     1112299 :   while (Lex.getCode() != tgtok::r_brace)
    2444      887764 :     if (ParseBodyItem(CurRec))
    2445             :       return true;
    2446             : 
    2447             :   // Eat the '}'.
    2448             :   Lex.Lex();
    2449      224535 :   return false;
    2450             : }
    2451             : 
    2452             : /// Apply the current let bindings to \a CurRec.
    2453             : /// \returns true on error, false otherwise.
    2454     3251948 : bool TGParser::ApplyLetStack(Record *CurRec) {
    2455     5009091 :   for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
    2456     3671433 :     for (LetRecord &LR : LetInfo)
    2457     3828580 :       if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
    2458             :         return true;
    2459             :   return false;
    2460             : }
    2461             : 
    2462      974367 : bool TGParser::ApplyLetStack(RecordsEntry &Entry) {
    2463      974367 :   if (Entry.Rec)
    2464      974277 :     return ApplyLetStack(Entry.Rec.get());
    2465             : 
    2466         180 :   for (auto &E : Entry.Loop->Entries) {
    2467          90 :     if (ApplyLetStack(E))
    2468             :       return true;
    2469             :   }
    2470             : 
    2471             :   return false;
    2472             : }
    2473             : 
    2474             : /// ParseObjectBody - Parse the body of a def or class.  This consists of an
    2475             : /// optional ClassList followed by a Body.  CurRec is the current def or class
    2476             : /// that is being parsed.
    2477             : ///
    2478             : ///   ObjectBody      ::= BaseClassList Body
    2479             : ///   BaseClassList   ::= /*empty*/
    2480             : ///   BaseClassList   ::= ':' BaseClassListNE
    2481             : ///   BaseClassListNE ::= SubClassRef (',' SubClassRef)*
    2482             : ///
    2483     2277673 : bool TGParser::ParseObjectBody(Record *CurRec) {
    2484             :   // If there is a baseclass list, read it.
    2485     2277673 :   if (Lex.getCode() == tgtok::colon) {
    2486     2246266 :     Lex.Lex();
    2487             : 
    2488             :     // Read all of the subclasses.
    2489     2246266 :     SubClassReference SubClass = ParseSubClassReference(CurRec, false);
    2490             :     while (true) {
    2491             :       // Check for error.
    2492     3031702 :       if (!SubClass.Rec) return true;
    2493             : 
    2494             :       // Add it.
    2495     3031702 :       if (AddSubClass(CurRec, SubClass))
    2496             :         return true;
    2497             : 
    2498     3031700 :       if (Lex.getCode() != tgtok::comma) break;
    2499             :       Lex.Lex(); // eat ','.
    2500     1570872 :       SubClass = ParseSubClassReference(CurRec, false);
    2501             :     }
    2502             :   }
    2503             : 
    2504     2277671 :   if (ApplyLetStack(CurRec))
    2505             :     return true;
    2506             : 
    2507     2277671 :   return ParseBody(CurRec);
    2508             : }
    2509             : 
    2510             : /// ParseDef - Parse and return a top level or multiclass def, return the record
    2511             : /// corresponding to it.  This returns null on error.
    2512             : ///
    2513             : ///   DefInst ::= DEF ObjectName ObjectBody
    2514             : ///
    2515     2074092 : bool TGParser::ParseDef(MultiClass *CurMultiClass) {
    2516     2074092 :   SMLoc DefLoc = Lex.getLoc();
    2517             :   assert(Lex.getCode() == tgtok::Def && "Unknown tok");
    2518             :   Lex.Lex();  // Eat the 'def' token.
    2519             : 
    2520             :   // Parse ObjectName and make a record for it.
    2521     2074090 :   std::unique_ptr<Record> CurRec;
    2522     2074092 :   Init *Name = ParseObjectName(CurMultiClass);
    2523     2074092 :   if (!Name)
    2524             :     return true;
    2525             : 
    2526     2074092 :   if (isa<UnsetInit>(Name))
    2527      587898 :     CurRec = make_unique<Record>(Records.getNewAnonymousName(), DefLoc, Records,
    2528      293949 :                                  /*Anonymous=*/true);
    2529             :   else
    2530     1780143 :     CurRec = make_unique<Record>(Name, DefLoc, Records);
    2531             : 
    2532     2074092 :   if (ParseObjectBody(CurRec.get()))
    2533             :     return true;
    2534             : 
    2535     4148179 :   return addEntry(std::move(CurRec));
    2536             : }
    2537             : 
    2538             : /// ParseDefset - Parse a defset statement.
    2539             : ///
    2540             : ///   Defset ::= DEFSET Type Id '=' '{' ObjectList '}'
    2541             : ///
    2542         963 : bool TGParser::ParseDefset() {
    2543             :   assert(Lex.getCode() == tgtok::Defset);
    2544         963 :   Lex.Lex(); // Eat the 'defset' token
    2545             : 
    2546             :   DefsetRecord Defset;
    2547         963 :   Defset.Loc = Lex.getLoc();
    2548         963 :   RecTy *Type = ParseType();
    2549         963 :   if (!Type)
    2550             :     return true;
    2551         963 :   if (!isa<ListRecTy>(Type))
    2552           0 :     return Error(Defset.Loc, "expected list type");
    2553         963 :   Defset.EltTy = cast<ListRecTy>(Type)->getElementType();
    2554             : 
    2555         963 :   if (Lex.getCode() != tgtok::Id)
    2556           0 :     return TokError("expected identifier");
    2557         963 :   StringInit *DeclName = StringInit::get(Lex.getCurStrVal());
    2558         963 :   if (Records.getGlobal(DeclName->getValue()))
    2559           0 :     return TokError("def or global variable of this name already exists");
    2560             : 
    2561         963 :   if (Lex.Lex() != tgtok::equal) // Eat the identifier
    2562           0 :     return TokError("expected '='");
    2563         963 :   if (Lex.Lex() != tgtok::l_brace) // Eat the '='
    2564           0 :     return TokError("expected '{'");
    2565         963 :   SMLoc BraceLoc = Lex.getLoc();
    2566             :   Lex.Lex(); // Eat the '{'
    2567             : 
    2568         963 :   Defsets.push_back(&Defset);
    2569         963 :   bool Err = ParseObjectList(nullptr);
    2570             :   Defsets.pop_back();
    2571         963 :   if (Err)
    2572             :     return true;
    2573             : 
    2574         962 :   if (Lex.getCode() != tgtok::r_brace) {
    2575           0 :     TokError("expected '}' at end of defset");
    2576           0 :     return Error(BraceLoc, "to match this '{'");
    2577             :   }
    2578             :   Lex.Lex();  // Eat the '}'
    2579             : 
    2580         962 :   Records.addExtraGlobal(DeclName->getValue(),
    2581        1924 :                          ListInit::get(Defset.Elements, Defset.EltTy));
    2582         962 :   return false;
    2583             : }
    2584             : 
    2585             : /// ParseForeach - Parse a for statement.  Return the record corresponding
    2586             : /// to it.  This returns true on error.
    2587             : ///
    2588             : ///   Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
    2589             : ///   Foreach ::= FOREACH Declaration IN Object
    2590             : ///
    2591        3435 : bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
    2592        3435 :   SMLoc Loc = Lex.getLoc();
    2593             :   assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
    2594             :   Lex.Lex();  // Eat the 'for' token.
    2595             : 
    2596             :   // Make a temporary object to record items associated with the for
    2597             :   // loop.
    2598        3435 :   Init *ListValue = nullptr;
    2599        3435 :   VarInit *IterName = ParseForeachDeclaration(ListValue);
    2600        3435 :   if (!IterName)
    2601           0 :     return TokError("expected declaration in for");
    2602             : 
    2603        3435 :   if (Lex.getCode() != tgtok::In)
    2604           0 :     return TokError("Unknown tok");
    2605             :   Lex.Lex();  // Eat the in
    2606             : 
    2607             :   // Create a loop object and remember it.
    2608        6870 :   Loops.push_back(llvm::make_unique<ForeachLoop>(Loc, IterName, ListValue));
    2609             : 
    2610        3435 :   if (Lex.getCode() != tgtok::l_brace) {
    2611             :     // FOREACH Declaration IN Object
    2612         619 :     if (ParseObject(CurMultiClass))
    2613             :       return true;
    2614             :   } else {
    2615        2816 :     SMLoc BraceLoc = Lex.getLoc();
    2616             :     // Otherwise, this is a group foreach.
    2617             :     Lex.Lex();  // eat the '{'.
    2618             : 
    2619             :     // Parse the object list.
    2620        2816 :     if (ParseObjectList(CurMultiClass))
    2621           0 :       return true;
    2622             : 
    2623        2816 :     if (Lex.getCode() != tgtok::r_brace) {
    2624           0 :       TokError("expected '}' at end of foreach command");
    2625           0 :       return Error(BraceLoc, "to match this '{'");
    2626             :     }
    2627             :     Lex.Lex();  // Eat the }
    2628             :   }
    2629             : 
    2630             :   // Resolve the loop or store it for later resolution.
    2631        3435 :   std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back());
    2632             :   Loops.pop_back();
    2633             : 
    2634        6870 :   return addEntry(std::move(Loop));
    2635             : }
    2636             : 
    2637             : /// ParseClass - Parse a tblgen class definition.
    2638             : ///
    2639             : ///   ClassInst ::= CLASS ID TemplateArgList? ObjectBody
    2640             : ///
    2641      203581 : bool TGParser::ParseClass() {
    2642             :   assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
    2643      203581 :   Lex.Lex();
    2644             : 
    2645      203581 :   if (Lex.getCode() != tgtok::Id)
    2646           0 :     return TokError("expected class name after 'class' keyword");
    2647             : 
    2648      407162 :   Record *CurRec = Records.getClass(Lex.getCurStrVal());
    2649      203581 :   if (CurRec) {
    2650             :     // If the body was previously defined, this is an error.
    2651        1359 :     if (!CurRec->getValues().empty() ||
    2652        2718 :         !CurRec->getSuperClasses().empty() ||
    2653             :         !CurRec->getTemplateArgs().empty())
    2654           0 :       return TokError("Class '" + CurRec->getNameInitAsString() +
    2655             :                       "' already defined");
    2656             :   } else {
    2657             :     // If this is the first reference to this class, create and add it.
    2658             :     auto NewRec =
    2659      202222 :         llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records,
    2660      202222 :                                   /*Class=*/true);
    2661             :     CurRec = NewRec.get();
    2662      404444 :     Records.addClass(std::move(NewRec));
    2663             :   }
    2664             :   Lex.Lex(); // eat the name.
    2665             : 
    2666             :   // If there are template args, parse them.
    2667      203581 :   if (Lex.getCode() == tgtok::less)
    2668      136009 :     if (ParseTemplateArgList(CurRec))
    2669             :       return true;
    2670             : 
    2671      203581 :   return ParseObjectBody(CurRec);
    2672             : }
    2673             : 
    2674             : /// ParseLetList - Parse a non-empty list of assignment expressions into a list
    2675             : /// of LetRecords.
    2676             : ///
    2677             : ///   LetList ::= LetItem (',' LetItem)*
    2678             : ///   LetItem ::= ID OptionalRangeList '=' Value
    2679             : ///
    2680       95557 : void TGParser::ParseLetList(SmallVectorImpl<LetRecord> &Result) {
    2681             :   while (true) {
    2682      128665 :     if (Lex.getCode() != tgtok::Id) {
    2683           0 :       TokError("expected identifier in let definition");
    2684           0 :       Result.clear();
    2685           0 :       return;
    2686             :     }
    2687             : 
    2688      128665 :     StringInit *Name = StringInit::get(Lex.getCurStrVal());
    2689      128665 :     SMLoc NameLoc = Lex.getLoc();
    2690             :     Lex.Lex();  // Eat the identifier.
    2691             : 
    2692             :     // Check for an optional RangeList.
    2693             :     SmallVector<unsigned, 16> Bits;
    2694      128665 :     if (ParseOptionalRangeList(Bits)) {
    2695           0 :       Result.clear();
    2696           0 :       return;
    2697             :     }
    2698             :     std::reverse(Bits.begin(), Bits.end());
    2699             : 
    2700      128665 :     if (Lex.getCode() != tgtok::equal) {
    2701           0 :       TokError("expected '=' in let expression");
    2702           0 :       Result.clear();
    2703           0 :       return;
    2704             :     }
    2705             :     Lex.Lex();  // eat the '='.
    2706             : 
    2707      128665 :     Init *Val = ParseValue(nullptr);
    2708      128665 :     if (!Val) {
    2709           0 :       Result.clear();
    2710           0 :       return;
    2711             :     }
    2712             : 
    2713             :     // Now that we have everything, add the record.
    2714      128665 :     Result.emplace_back(Name, Bits, Val, NameLoc);
    2715             : 
    2716      128665 :     if (Lex.getCode() != tgtok::comma)
    2717             :       return;
    2718             :     Lex.Lex();  // eat the comma.
    2719             :   }
    2720             : }
    2721             : 
    2722             : /// ParseTopLevelLet - Parse a 'let' at top level.  This can be a couple of
    2723             : /// different related productions. This works inside multiclasses too.
    2724             : ///
    2725             : ///   Object ::= LET LetList IN '{' ObjectList '}'
    2726             : ///   Object ::= LET LetList IN Object
    2727             : ///
    2728       95557 : bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
    2729             :   assert(Lex.getCode() == tgtok::Let && "Unexpected token");
    2730       95557 :   Lex.Lex();
    2731             : 
    2732             :   // Add this entry to the let stack.
    2733       95557 :   SmallVector<LetRecord, 8> LetInfo;
    2734       95557 :   ParseLetList(LetInfo);
    2735       95557 :   if (LetInfo.empty()) return true;
    2736      191114 :   LetStack.push_back(std::move(LetInfo));
    2737             : 
    2738       95557 :   if (Lex.getCode() != tgtok::In)
    2739           0 :     return TokError("expected 'in' at end of top-level 'let'");
    2740             :   Lex.Lex();
    2741             : 
    2742             :   // If this is a scalar let, just handle it now
    2743       95557 :   if (Lex.getCode() != tgtok::l_brace) {
    2744             :     // LET LetList IN Object
    2745       22455 :     if (ParseObject(CurMultiClass))
    2746             :       return true;
    2747             :   } else {   // Object ::= LETCommand '{' ObjectList '}'
    2748       73102 :     SMLoc BraceLoc = Lex.getLoc();
    2749             :     // Otherwise, this is a group let.
    2750             :     Lex.Lex();  // eat the '{'.
    2751             : 
    2752             :     // Parse the object list.
    2753       73102 :     if (ParseObjectList(CurMultiClass))
    2754           0 :       return true;
    2755             : 
    2756       73102 :     if (Lex.getCode() != tgtok::r_brace) {
    2757           0 :       TokError("expected '}' at end of top level let command");
    2758           0 :       return Error(BraceLoc, "to match this '{'");
    2759             :     }
    2760             :     Lex.Lex();
    2761             :   }
    2762             : 
    2763             :   // Outside this let scope, this let block is not active.
    2764             :   LetStack.pop_back();
    2765       95557 :   return false;
    2766             : }
    2767             : 
    2768             : /// ParseMultiClass - Parse a multiclass definition.
    2769             : ///
    2770             : ///  MultiClassInst ::= MULTICLASS ID TemplateArgList?
    2771             : ///                     ':' BaseMultiClassList '{' MultiClassObject+ '}'
    2772             : ///  MultiClassObject ::= DefInst
    2773             : ///  MultiClassObject ::= MultiClassInst
    2774             : ///  MultiClassObject ::= DefMInst
    2775             : ///  MultiClassObject ::= LETCommand '{' ObjectList '}'
    2776             : ///  MultiClassObject ::= LETCommand Object
    2777             : ///
    2778       31896 : bool TGParser::ParseMultiClass() {
    2779             :   assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
    2780       31896 :   Lex.Lex();  // Eat the multiclass token.
    2781             : 
    2782       31896 :   if (Lex.getCode() != tgtok::Id)
    2783           0 :     return TokError("expected identifier after multiclass for name");
    2784             :   std::string Name = Lex.getCurStrVal();
    2785             : 
    2786             :   auto Result =
    2787       31896 :     MultiClasses.insert(std::make_pair(Name,
    2788       31896 :                     llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
    2789             : 
    2790       31896 :   if (!Result.second)
    2791           0 :     return TokError("multiclass '" + Name + "' already defined");
    2792             : 
    2793       31896 :   CurMultiClass = Result.first->second.get();
    2794             :   Lex.Lex();  // Eat the identifier.
    2795             : 
    2796             :   // If there are template args, parse them.
    2797       31896 :   if (Lex.getCode() == tgtok::less)
    2798       29872 :     if (ParseTemplateArgList(nullptr))
    2799             :       return true;
    2800             : 
    2801             :   bool inherits = false;
    2802             : 
    2803             :   // If there are submulticlasses, parse them.
    2804       31896 :   if (Lex.getCode() == tgtok::colon) {
    2805             :     inherits = true;
    2806             : 
    2807             :     Lex.Lex();
    2808             : 
    2809             :     // Read all of the submulticlasses.
    2810             :     SubMultiClassReference SubMultiClass =
    2811        2104 :       ParseSubMultiClassReference(CurMultiClass);
    2812             :     while (true) {
    2813             :       // Check for error.
    2814        2195 :       if (!SubMultiClass.MC) return true;
    2815             : 
    2816             :       // Add it.
    2817        2195 :       if (AddSubMultiClass(CurMultiClass, SubMultiClass))
    2818             :         return true;
    2819             : 
    2820        2195 :       if (Lex.getCode() != tgtok::comma) break;
    2821             :       Lex.Lex(); // eat ','.
    2822         182 :       SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
    2823             :     }
    2824             :   }
    2825             : 
    2826       31896 :   if (Lex.getCode() != tgtok::l_brace) {
    2827         658 :     if (!inherits)
    2828           0 :       return TokError("expected '{' in multiclass definition");
    2829         658 :     if (Lex.getCode() != tgtok::semi)
    2830           0 :       return TokError("expected ';' in multiclass definition");
    2831             :     Lex.Lex();  // eat the ';'.
    2832             :   } else {
    2833       31238 :     if (Lex.Lex() == tgtok::r_brace)  // eat the '{'.
    2834           0 :       return TokError("multiclass must contain at least one def");
    2835             : 
    2836      121375 :     while (Lex.getCode() != tgtok::r_brace) {
    2837             :       switch (Lex.getCode()) {
    2838             :       default:
    2839           0 :         return TokError("expected 'let', 'def', 'defm' or 'foreach' in "
    2840             :                         "multiclass body");
    2841       90137 :       case tgtok::Let:
    2842             :       case tgtok::Def:
    2843             :       case tgtok::Defm:
    2844             :       case tgtok::Foreach:
    2845       90137 :         if (ParseObject(CurMultiClass))
    2846             :           return true;
    2847             :         break;
    2848             :       }
    2849             :     }
    2850             :     Lex.Lex();  // eat the '}'.
    2851             :   }
    2852             : 
    2853       31896 :   CurMultiClass = nullptr;
    2854       31896 :   return false;
    2855             : }
    2856             : 
    2857             : /// ParseDefm - Parse the instantiation of a multiclass.
    2858             : ///
    2859             : ///   DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
    2860             : ///
    2861      186293 : bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
    2862             :   assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
    2863      186293 :   Lex.Lex(); // eat the defm
    2864             : 
    2865      186293 :   Init *DefmName = ParseObjectName(CurMultiClass);
    2866      186293 :   if (!DefmName)
    2867             :     return true;
    2868      186293 :   if (isa<UnsetInit>(DefmName)) {
    2869       55430 :     DefmName = Records.getNewAnonymousName();
    2870       55430 :     if (CurMultiClass)
    2871        1082 :       DefmName = BinOpInit::getStrConcat(
    2872        1082 :           VarInit::get(QualifiedNameOfImplicitName(CurMultiClass),
    2873             :                        StringRecTy::get()),
    2874             :           DefmName);
    2875             :   }
    2876             : 
    2877      186293 :   if (Lex.getCode() != tgtok::colon)
    2878           0 :     return TokError("expected ':' after defm identifier");
    2879             : 
    2880             :   // Keep track of the new generated record definitions.
    2881      186292 :   std::vector<RecordsEntry> NewEntries;
    2882             : 
    2883             :   // This record also inherits from a regular class (non-multiclass)?
    2884             :   bool InheritFromClass = false;
    2885             : 
    2886             :   // eat the colon.
    2887             :   Lex.Lex();
    2888             : 
    2889      186293 :   SMLoc SubClassLoc = Lex.getLoc();
    2890      186293 :   SubClassReference Ref = ParseSubClassReference(nullptr, true);
    2891             : 
    2892             :   while (true) {
    2893      188093 :     if (!Ref.Rec) return true;
    2894             : 
    2895             :     // To instantiate a multiclass, we need to first get the multiclass, then
    2896             :     // instantiate each def contained in the multiclass with the SubClassRef
    2897             :     // template parameters.
    2898      564279 :     MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
    2899             :     assert(MC && "Didn't lookup multiclass correctly?");
    2900             :     ArrayRef<Init*> TemplateVals = Ref.TemplateArgs;
    2901             : 
    2902             :     // Verify that the correct number of template arguments were specified.
    2903             :     ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();
    2904      188093 :     if (TArgs.size() < TemplateVals.size())
    2905           0 :       return Error(SubClassLoc,
    2906             :                    "more template args specified than multiclass expects");
    2907             : 
    2908             :     SubstStack Substs;
    2909      908826 :     for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
    2910      720733 :       if (i < TemplateVals.size()) {
    2911      667062 :         Substs.emplace_back(TArgs[i], TemplateVals[i]);
    2912             :       } else {
    2913       53671 :         Init *Default = MC->Rec.getValue(TArgs[i])->getValue();
    2914       53671 :         if (!Default->isComplete()) {
    2915           0 :           return Error(SubClassLoc,
    2916           0 :                        "value not specified for template argument #" +
    2917           0 :                            Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
    2918           0 :                            ") of multiclass '" + MC->Rec.getNameInitAsString() +
    2919           0 :                            "'");
    2920             :         }
    2921       53671 :         Substs.emplace_back(TArgs[i], Default);
    2922             :       }
    2923             :     }
    2924             : 
    2925      188093 :     Substs.emplace_back(QualifiedNameOfImplicitName(MC), DefmName);
    2926             : 
    2927      188093 :     if (resolve(MC->Entries, Substs, CurMultiClass == nullptr, &NewEntries,
    2928             :                 &SubClassLoc))
    2929             :       return true;
    2930             : 
    2931      188093 :     if (Lex.getCode() != tgtok::comma) break;
    2932             :     Lex.Lex(); // eat ','.
    2933             : 
    2934       21959 :     if (Lex.getCode() != tgtok::Id)
    2935           0 :       return TokError("expected identifier");
    2936             : 
    2937       21959 :     SubClassLoc = Lex.getLoc();
    2938             : 
    2939             :     // A defm can inherit from regular classes (non-multiclass) as
    2940             :     // long as they come in the end of the inheritance list.
    2941       43918 :     InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
    2942             : 
    2943       21959 :     if (InheritFromClass)
    2944             :       break;
    2945             : 
    2946        3600 :     Ref = ParseSubClassReference(nullptr, true);
    2947             :   }
    2948             : 
    2949      186293 :   if (InheritFromClass) {
    2950             :     // Process all the classes to inherit as if they were part of a
    2951             :     // regular 'def' and inherit all record values.
    2952       20159 :     SubClassReference SubClass = ParseSubClassReference(nullptr, false);
    2953             :     while (true) {
    2954             :       // Check for error.
    2955       39271 :       if (!SubClass.Rec) return true;
    2956             : 
    2957             :       // Get the expanded definition prototypes and teach them about
    2958             :       // the record values the current class to inherit has
    2959      329433 :       for (auto &E : NewEntries) {
    2960             :         // Add it.
    2961      290162 :         if (AddSubClass(E, SubClass))
    2962             :           return true;
    2963             :       }
    2964             : 
    2965       39271 :       if (Lex.getCode() != tgtok::comma) break;
    2966             :       Lex.Lex(); // eat ','.
    2967       19112 :       SubClass = ParseSubClassReference(nullptr, false);
    2968       19112 :     }
    2969             :   }
    2970             : 
    2971     1160569 :   for (auto &E : NewEntries) {
    2972      974277 :     if (ApplyLetStack(E))
    2973             :       return true;
    2974             : 
    2975     1948553 :     addEntry(std::move(E));
    2976             :   }
    2977             : 
    2978      186292 :   if (Lex.getCode() != tgtok::semi)
    2979           0 :     return TokError("expected ';' at end of defm");
    2980             :   Lex.Lex();
    2981             : 
    2982      186292 :   return false;
    2983             : }
    2984             : 
    2985             : /// ParseObject
    2986             : ///   Object ::= ClassInst
    2987             : ///   Object ::= DefInst
    2988             : ///   Object ::= MultiClassInst
    2989             : ///   Object ::= DefMInst
    2990             : ///   Object ::= LETCommand '{' ObjectList '}'
    2991             : ///   Object ::= LETCommand Object
    2992     2595817 : bool TGParser::ParseObject(MultiClass *MC) {
    2993     2595817 :   switch (Lex.getCode()) {
    2994             :   default:
    2995           0 :     return TokError("Expected class, def, defm, defset, multiclass, let or "
    2996             :                     "foreach");
    2997       95557 :   case tgtok::Let:   return ParseTopLevelLet(MC);
    2998     2074092 :   case tgtok::Def:   return ParseDef(MC);
    2999        3435 :   case tgtok::Foreach:   return ParseForeach(MC);
    3000      186293 :   case tgtok::Defm:  return ParseDefm(MC);
    3001         963 :   case tgtok::Defset:
    3002         963 :     if (MC)
    3003           0 :       return TokError("defset is not allowed inside multiclass");
    3004         963 :     return ParseDefset();
    3005      203581 :   case tgtok::Class:
    3006      203581 :     if (MC)
    3007           0 :       return TokError("class is not allowed inside multiclass");
    3008      203581 :     if (!Loops.empty())
    3009           0 :       return TokError("class is not allowed inside foreach loop");
    3010      203581 :     return ParseClass();
    3011       31896 :   case tgtok::MultiClass:
    3012       31896 :     if (!Loops.empty())
    3013           0 :       return TokError("multiclass is not allowed inside foreach loop");
    3014       31896 :     return ParseMultiClass();
    3015             :   }
    3016             : }
    3017             : 
    3018             : /// ParseObjectList
    3019             : ///   ObjectList :== Object*
    3020       77233 : bool TGParser::ParseObjectList(MultiClass *MC) {
    3021     2559833 :   while (isObjectStart(Lex.getCode())) {
    3022     2482606 :     if (ParseObject(MC))
    3023             :       return true;
    3024             :   }
    3025             :   return false;
    3026             : }
    3027             : 
    3028         352 : bool TGParser::ParseFile() {
    3029         352 :   Lex.Lex(); // Prime the lexer.
    3030         352 :   if (ParseObjectList()) return true;
    3031             : 
    3032             :   // If we have unread input at the end of the file, report it.
    3033         347 :   if (Lex.getCode() == tgtok::Eof)
    3034             :     return false;
    3035             : 
    3036           1 :   return TokError("Unexpected input at top level");
    3037             : }
    3038             : 
    3039             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
    3040             : LLVM_DUMP_METHOD void RecordsEntry::dump() const {
    3041             :   if (Loop)
    3042             :     Loop->dump();
    3043             :   if (Rec)
    3044             :     Rec->dump();
    3045             : }
    3046             : 
    3047             : LLVM_DUMP_METHOD void ForeachLoop::dump() const {
    3048             :   errs() << "foreach " << IterVar->getAsString() << " = "
    3049             :          << ListValue->getAsString() << " in {\n";
    3050             : 
    3051             :   for (const auto &E : Entries)
    3052             :     E.dump();
    3053             : 
    3054             :   errs() << "}\n";
    3055             : }
    3056             : 
    3057             : LLVM_DUMP_METHOD void MultiClass::dump() const {
    3058             :   errs() << "Record:\n";
    3059             :   Rec.dump();
    3060             : 
    3061             :   errs() << "Defs:\n";
    3062             :   for (const auto &E : Entries)
    3063             :     E.dump();
    3064             : }
    3065             : #endif

Generated by: LCOV version 1.13