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