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