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