TGParser.cpp revision 226584
174298Ssos//===- TGParser.cpp - Parser for TableGen Files ---------------------------===// 2144330Ssos// 374298Ssos// The LLVM Compiler Infrastructure 474298Ssos// 574298Ssos// This file is distributed under the University of Illinois Open Source 674298Ssos// License. See LICENSE.TXT for details. 774298Ssos// 874298Ssos//===----------------------------------------------------------------------===// 974298Ssos// 1074298Ssos// Implement the Parser for TableGen. 1174298Ssos// 1274298Ssos//===----------------------------------------------------------------------===// 1374298Ssos 1474298Ssos#include "TGParser.h" 1574298Ssos#include "llvm/TableGen/Record.h" 1674298Ssos#include "llvm/ADT/StringExtras.h" 1774298Ssos#include <algorithm> 1874298Ssos#include <sstream> 1974298Ssos#include "llvm/ADT/SmallVector.h" 2074298Ssos#include "llvm/Support/CommandLine.h" 2174298Ssosusing namespace llvm; 2274298Ssos 2374298Ssos//===----------------------------------------------------------------------===// 2474298Ssos// Support Code for the Semantic Actions. 2574298Ssos//===----------------------------------------------------------------------===// 2674298Ssos 2774298Ssosnamespace llvm { 2874298Ssosstruct SubClassReference { 2974298Ssos SMLoc RefLoc; 3074298Ssos Record *Rec; 3174298Ssos std::vector<Init*> TemplateArgs; 3274298Ssos SubClassReference() : Rec(0) {} 3374298Ssos 3474298Ssos bool isInvalid() const { return Rec == 0; } 3574298Ssos}; 36119404Ssos 3793881Ssosstruct SubMultiClassReference { 38146266Ssos SMLoc RefLoc; 39146266Ssos MultiClass *MC; 40146266Ssos std::vector<Init*> TemplateArgs; 41146266Ssos SubMultiClassReference() : MC(0) {} 42146266Ssos 43146266Ssos bool isInvalid() const { return MC == 0; } 44146266Ssos void dump() const; 45146266Ssos}; 46146266Ssos 47146266Ssosvoid SubMultiClassReference::dump() const { 48146266Ssos errs() << "Multiclass:\n"; 49146266Ssos 50146266Ssos MC->dump(); 51146266Ssos 5293881Ssos errs() << "Template args:\n"; 53146266Ssos for (std::vector<Init *>::const_iterator i = TemplateArgs.begin(), 54146266Ssos iend = TemplateArgs.end(); 55146266Ssos i != iend; 56146266Ssos ++i) { 57146266Ssos (*i)->dump(); 58146266Ssos } 59146266Ssos} 60146266Ssos 61146266Ssos} // end namespace llvm 62146266Ssos 63146266Ssosbool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) { 64146266Ssos if (CurRec == 0) 65146266Ssos CurRec = &CurMultiClass->Rec; 66146266Ssos 67146266Ssos if (RecordVal *ERV = CurRec->getValue(RV.getName())) { 68146266Ssos // The value already exists in the class, treat this as a set. 69146266Ssos if (ERV->setValue(RV.getValue())) 70146266Ssos return Error(Loc, "New definition of '" + RV.getName() + "' of type '" + 71146266Ssos RV.getType()->getAsString() + "' is incompatible with " + 7293881Ssos "previous definition of type '" + 73146266Ssos ERV->getType()->getAsString() + "'"); 74146266Ssos } else { 75146266Ssos CurRec->addValue(RV); 7674298Ssos } 77146266Ssos return false; 78146266Ssos} 7974298Ssos 80146266Ssos/// SetValue - 81146266Ssos/// Return true on error, false on success. 82146266Ssosbool TGParser::SetValue(Record *CurRec, SMLoc Loc, const std::string &ValName, 83146266Ssos const std::vector<unsigned> &BitList, Init *V) { 8474298Ssos if (!V) return false; 85146266Ssos 86146266Ssos if (CurRec == 0) CurRec = &CurMultiClass->Rec; 87146266Ssos 88146266Ssos RecordVal *RV = CurRec->getValue(ValName); 89146266Ssos if (RV == 0) 90146266Ssos return Error(Loc, "Value '" + ValName + "' unknown!"); 91146266Ssos 9293881Ssos // Do not allow assignments like 'X = X'. This will just cause infinite loops 93146266Ssos // in the resolution machinery. 94146266Ssos if (BitList.empty()) 95146266Ssos if (VarInit *VI = dynamic_cast<VarInit*>(V)) 96146266Ssos if (VI->getName() == ValName) 97146266Ssos return false; 9874298Ssos 99146266Ssos // If we are assigning to a subset of the bits in the value... then we must be 100146266Ssos // assigning to a field of BitsRecTy, which must have a BitsInit 101146266Ssos // initializer. 102146266Ssos // 103146266Ssos if (!BitList.empty()) { 104146266Ssos BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue()); 105146266Ssos if (CurVal == 0) 106146266Ssos return Error(Loc, "Value '" + ValName + "' is not a bits type"); 107146266Ssos 108146266Ssos // Convert the incoming value to a bits type of the appropriate size... 109146266Ssos Init *BI = V->convertInitializerTo(BitsRecTy::get(BitList.size())); 110146266Ssos if (BI == 0) { 11174298Ssos V->convertInitializerTo(BitsRecTy::get(BitList.size())); 112146266Ssos return Error(Loc, "Initializer is not compatible with bit range"); 113146266Ssos } 114146266Ssos 115146266Ssos // We should have a BitsInit type now. 116146266Ssos BitsInit *BInit = dynamic_cast<BitsInit*>(BI); 117127021Ssos assert(BInit != 0); 118146266Ssos 119146266Ssos SmallVector<Init *, 16> NewBits(CurVal->getNumBits()); 120146266Ssos 121146266Ssos // Loop over bits, assigning values as appropriate. 122146266Ssos for (unsigned i = 0, e = BitList.size(); i != e; ++i) { 123146266Ssos unsigned Bit = BitList[i]; 124146266Ssos if (NewBits[Bit]) 125127021Ssos return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" + 126146266Ssos ValName + "' more than once"); 127146266Ssos NewBits[Bit] = BInit->getBit(i); 128119404Ssos } 12984584Ssos 130146266Ssos for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i) 131146266Ssos if (NewBits[i] == 0) 132146266Ssos NewBits[i] = CurVal->getBit(i); 133146266Ssos 134146266Ssos V = BitsInit::get(NewBits); 135146266Ssos } 136146266Ssos 137146266Ssos if (RV->setValue(V)) 138146266Ssos return Error(Loc, "Value '" + ValName + "' of type '" + 139146266Ssos RV->getType()->getAsString() + 140146266Ssos "' is incompatible with initializer '" + V->getAsString() +"'"); 141146266Ssos return false; 142146266Ssos} 143146266Ssos 144146266Ssos/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template 14584584Ssos/// args as SubClass's template arguments. 146146266Ssosbool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) { 147146266Ssos Record *SC = SubClass.Rec; 148146266Ssos // Add all of the values in the subclass into the current class. 149146266Ssos const std::vector<RecordVal> &Vals = SC->getValues(); 150146266Ssos for (unsigned i = 0, e = Vals.size(); i != e; ++i) 151146266Ssos if (AddValue(CurRec, SubClass.RefLoc, Vals[i])) 152146266Ssos return true; 153146266Ssos 154146266Ssos const std::vector<std::string> &TArgs = SC->getTemplateArgs(); 155146266Ssos 156146266Ssos // Ensure that an appropriate number of template arguments are specified. 157146266Ssos if (TArgs.size() < SubClass.TemplateArgs.size()) 158146266Ssos return Error(SubClass.RefLoc, "More template args specified than expected"); 159146266Ssos 16084584Ssos // Loop over all of the template arguments, setting them to the specified 161146266Ssos // value or leaving them as the default if necessary. 162144330Ssos for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { 16384584Ssos if (i < SubClass.TemplateArgs.size()) { 164146266Ssos // If a value is specified for this template arg, set it now. 165146266Ssos if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(), 166146266Ssos SubClass.TemplateArgs[i])) 167146266Ssos return true; 168146266Ssos 169146266Ssos // Resolve it next. 170146266Ssos CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i])); 17184584Ssos 172146266Ssos // Now remove it. 173146266Ssos CurRec->removeValue(TArgs[i]); 174146266Ssos 17584584Ssos } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) { 176146266Ssos return Error(SubClass.RefLoc,"Value not specified for template argument #" 177146266Ssos + utostr(i) + " (" + TArgs[i] + ") of subclass '" + 178146266Ssos SC->getName() + "'!"); 179146266Ssos } 180146266Ssos } 181146266Ssos 182146266Ssos // Since everything went well, we can now set the "superclass" list for the 183146266Ssos // current record. 184146266Ssos const std::vector<Record*> &SCs = SC->getSuperClasses(); 185146266Ssos for (unsigned i = 0, e = SCs.size(); i != e; ++i) { 186146266Ssos if (CurRec->isSubClassOf(SCs[i])) 187146266Ssos return Error(SubClass.RefLoc, 188146266Ssos "Already subclass of '" + SCs[i]->getName() + "'!\n"); 189146266Ssos CurRec->addSuperClass(SCs[i]); 190146266Ssos } 191146266Ssos 192146266Ssos if (CurRec->isSubClassOf(SC)) 193144330Ssos return Error(SubClass.RefLoc, 19474298Ssos "Already subclass of '" + SC->getName() + "'!\n"); 195146266Ssos CurRec->addSuperClass(SC); 196119404Ssos return false; 197146266Ssos} 198146266Ssos 199146266Ssos/// AddSubMultiClass - Add SubMultiClass as a subclass to 200146266Ssos/// CurMC, resolving its template args as SubMultiClass's 201146266Ssos/// template arguments. 202146266Ssosbool TGParser::AddSubMultiClass(MultiClass *CurMC, 203146266Ssos SubMultiClassReference &SubMultiClass) { 204146266Ssos MultiClass *SMC = SubMultiClass.MC; 205146266Ssos Record *CurRec = &CurMC->Rec; 206146266Ssos 207146266Ssos const std::vector<RecordVal> &MCVals = CurRec->getValues(); 208146266Ssos 209146266Ssos // Add all of the values in the subclass into the current class. 210146266Ssos const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues(); 211146266Ssos for (unsigned i = 0, e = SMCVals.size(); i != e; ++i) 212146266Ssos if (AddValue(CurRec, SubMultiClass.RefLoc, SMCVals[i])) 213146266Ssos return true; 214146266Ssos 215146266Ssos int newDefStart = CurMC->DefPrototypes.size(); 216146266Ssos 217146266Ssos // Add all of the defs in the subclass into the current multiclass. 218146266Ssos for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(), 21974298Ssos iend = SMC->DefPrototypes.end(); 220146266Ssos i != iend; 221119404Ssos ++i) { 222146266Ssos // Clone the def and add it to the current multiclass 223146266Ssos Record *NewDef = new Record(**i); 224146266Ssos 225146266Ssos // Add all of the values in the superclass into the current def. 226146266Ssos for (unsigned i = 0, e = MCVals.size(); i != e; ++i) 227146266Ssos if (AddValue(NewDef, SubMultiClass.RefLoc, MCVals[i])) 228146266Ssos return true; 229146266Ssos 230146266Ssos CurMC->DefPrototypes.push_back(NewDef); 231146266Ssos } 232146266Ssos 233146266Ssos const std::vector<std::string> &SMCTArgs = SMC->Rec.getTemplateArgs(); 234146266Ssos 235146266Ssos // Ensure that an appropriate number of template arguments are 236146266Ssos // specified. 237146266Ssos if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size()) 238146266Ssos return Error(SubMultiClass.RefLoc, 239146266Ssos "More template args specified than expected"); 240146266Ssos 241146266Ssos // Loop over all of the template arguments, setting them to the specified 242146266Ssos // value or leaving them as the default if necessary. 243146266Ssos for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) { 244146266Ssos if (i < SubMultiClass.TemplateArgs.size()) { 245146266Ssos // If a value is specified for this template arg, set it in the 246146266Ssos // superclass now. 247146266Ssos if (SetValue(CurRec, SubMultiClass.RefLoc, SMCTArgs[i], 248146266Ssos std::vector<unsigned>(), 249146266Ssos SubMultiClass.TemplateArgs[i])) 250146266Ssos return true; 251146266Ssos 252146266Ssos // Resolve it next. 253146266Ssos CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i])); 254146266Ssos 255146266Ssos // Now remove it. 256146266Ssos CurRec->removeValue(SMCTArgs[i]); 257146266Ssos 258146266Ssos // If a value is specified for this template arg, set it in the 259146266Ssos // new defs now. 260146266Ssos for (MultiClass::RecordVector::iterator j = 261146266Ssos CurMC->DefPrototypes.begin() + newDefStart, 262146266Ssos jend = CurMC->DefPrototypes.end(); 263146266Ssos j != jend; 264146266Ssos ++j) { 265146266Ssos Record *Def = *j; 266146266Ssos 267146266Ssos if (SetValue(Def, SubMultiClass.RefLoc, SMCTArgs[i], 268146266Ssos std::vector<unsigned>(), 269146266Ssos SubMultiClass.TemplateArgs[i])) 270146266Ssos return true; 271146266Ssos 272119404Ssos // Resolve it next. 273146266Ssos Def->resolveReferencesTo(Def->getValue(SMCTArgs[i])); 274119404Ssos 275146266Ssos // Now remove it 276146266Ssos Def->removeValue(SMCTArgs[i]); 277146266Ssos } 278146266Ssos } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) { 279146266Ssos return Error(SubMultiClass.RefLoc, 280146266Ssos "Value not specified for template argument #" 281146266Ssos + utostr(i) + " (" + SMCTArgs[i] + ") of subclass '" + 282146266Ssos SMC->Rec.getName() + "'!"); 283146266Ssos } 284146266Ssos } 285146266Ssos 286146266Ssos return false; 287146266Ssos} 288146266Ssos 289146266Ssos//===----------------------------------------------------------------------===// 290146266Ssos// Parser Code 291146266Ssos//===----------------------------------------------------------------------===// 292146266Ssos 293146266Ssos/// isObjectStart - Return true if this is a valid first token for an Object. 294146266Ssosstatic bool isObjectStart(tgtok::TokKind K) { 295146266Ssos return K == tgtok::Class || K == tgtok::Def || 296146266Ssos K == tgtok::Defm || K == tgtok::Let || K == tgtok::MultiClass; 297146266Ssos} 298146266Ssos 299146266Ssosstatic std::string GetNewAnonymousName() { 300146266Ssos static unsigned AnonCounter = 0; 301146266Ssos return "anonymous."+utostr(AnonCounter++); 302146266Ssos} 303146266Ssos 304146266Ssos/// ParseObjectName - If an object name is specified, return it. Otherwise, 305146266Ssos/// return an anonymous name. 306146266Ssos/// ObjectName ::= ID 307146266Ssos/// ObjectName ::= /*empty*/ 308146266Ssos/// 309146266Ssosstd::string TGParser::ParseObjectName() { 310146266Ssos if (Lex.getCode() != tgtok::Id) 311146266Ssos return GetNewAnonymousName(); 312146266Ssos 313146266Ssos std::string Ret = Lex.getCurStrVal(); 314146266Ssos Lex.Lex(); 315146266Ssos return Ret; 316146266Ssos} 317146266Ssos 318146266Ssos 319146266Ssos/// ParseClassID - Parse and resolve a reference to a class name. This returns 320146266Ssos/// null on error. 321146266Ssos/// 322146266Ssos/// ClassID ::= ID 323146266Ssos/// 324146266SsosRecord *TGParser::ParseClassID() { 325146266Ssos if (Lex.getCode() != tgtok::Id) { 326146266Ssos TokError("expected name for ClassID"); 327146266Ssos return 0; 328146266Ssos } 329146266Ssos 330146266Ssos Record *Result = Records.getClass(Lex.getCurStrVal()); 331119404Ssos if (Result == 0) 33274298Ssos TokError("Couldn't find class '" + Lex.getCurStrVal() + "'"); 333146266Ssos 334146266Ssos Lex.Lex(); 335146266Ssos return Result; 336146266Ssos} 337146266Ssos 338119404Ssos/// ParseMultiClassID - Parse and resolve a reference to a multiclass name. 339146266Ssos/// This returns null on error. 340146266Ssos/// 341146266Ssos/// MultiClassID ::= ID 342146266Ssos/// 343146266SsosMultiClass *TGParser::ParseMultiClassID() { 344146266Ssos if (Lex.getCode() != tgtok::Id) { 345119404Ssos TokError("expected name for ClassID"); 346146266Ssos return 0; 347146266Ssos } 348119404Ssos 349146266Ssos MultiClass *Result = MultiClasses[Lex.getCurStrVal()]; 350146266Ssos if (Result == 0) 351146266Ssos TokError("Couldn't find class '" + Lex.getCurStrVal() + "'"); 352146266Ssos 353146266Ssos Lex.Lex(); 354119404Ssos return Result; 355146266Ssos} 356146266Ssos 357146266SsosRecord *TGParser::ParseDefmID() { 358146266Ssos if (Lex.getCode() != tgtok::Id) { 359146266Ssos TokError("expected multiclass name"); 360146266Ssos return 0; 361146266Ssos } 362146266Ssos 363146266Ssos MultiClass *MC = MultiClasses[Lex.getCurStrVal()]; 364146266Ssos if (MC == 0) { 365119404Ssos TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'"); 366146266Ssos return 0; 367146266Ssos } 368146266Ssos 369119404Ssos Lex.Lex(); 370146266Ssos return &MC->Rec; 371146266Ssos} 372146266Ssos 373146266Ssos 374146266Ssos/// ParseSubClassReference - Parse a reference to a subclass or to a templated 37593662Ssos/// subclass. This returns a SubClassRefTy with a null Record* on error. 376119404Ssos/// 377146266Ssos/// SubClassRef ::= ClassID 378146266Ssos/// SubClassRef ::= ClassID '<' ValueList '>' 379146266Ssos/// 380146266SsosSubClassReference TGParser:: 381146266SsosParseSubClassReference(Record *CurRec, bool isDefm) { 382146266Ssos SubClassReference Result; 383146266Ssos Result.RefLoc = Lex.getLoc(); 384146266Ssos 385146266Ssos if (isDefm) 386146266Ssos Result.Rec = ParseDefmID(); 387146266Ssos else 38893662Ssos Result.Rec = ParseClassID(); 389146266Ssos if (Result.Rec == 0) return Result; 390146266Ssos 391146266Ssos // If there is no template arg list, we're done. 392146266Ssos if (Lex.getCode() != tgtok::less) 393146266Ssos return Result; 394119404Ssos Lex.Lex(); // Eat the '<' 395146266Ssos 396146266Ssos if (Lex.getCode() == tgtok::greater) { 397146266Ssos TokError("subclass reference requires a non-empty list of template values"); 39874298Ssos Result.Rec = 0; 39974298Ssos return Result; 400146266Ssos } 401148737Ssos 402146266Ssos Result.TemplateArgs = ParseValueList(CurRec, Result.Rec); 403148729Ssos if (Result.TemplateArgs.empty()) { 404146266Ssos Result.Rec = 0; // Error parsing value list. 405146266Ssos return Result; 40674298Ssos } 40774298Ssos 408 if (Lex.getCode() != tgtok::greater) { 409 TokError("expected '>' in template value list"); 410 Result.Rec = 0; 411 return Result; 412 } 413 Lex.Lex(); 414 415 return Result; 416} 417 418/// ParseSubMultiClassReference - Parse a reference to a subclass or to a 419/// templated submulticlass. This returns a SubMultiClassRefTy with a null 420/// Record* on error. 421/// 422/// SubMultiClassRef ::= MultiClassID 423/// SubMultiClassRef ::= MultiClassID '<' ValueList '>' 424/// 425SubMultiClassReference TGParser:: 426ParseSubMultiClassReference(MultiClass *CurMC) { 427 SubMultiClassReference Result; 428 Result.RefLoc = Lex.getLoc(); 429 430 Result.MC = ParseMultiClassID(); 431 if (Result.MC == 0) return Result; 432 433 // If there is no template arg list, we're done. 434 if (Lex.getCode() != tgtok::less) 435 return Result; 436 Lex.Lex(); // Eat the '<' 437 438 if (Lex.getCode() == tgtok::greater) { 439 TokError("subclass reference requires a non-empty list of template values"); 440 Result.MC = 0; 441 return Result; 442 } 443 444 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec); 445 if (Result.TemplateArgs.empty()) { 446 Result.MC = 0; // Error parsing value list. 447 return Result; 448 } 449 450 if (Lex.getCode() != tgtok::greater) { 451 TokError("expected '>' in template value list"); 452 Result.MC = 0; 453 return Result; 454 } 455 Lex.Lex(); 456 457 return Result; 458} 459 460/// ParseRangePiece - Parse a bit/value range. 461/// RangePiece ::= INTVAL 462/// RangePiece ::= INTVAL '-' INTVAL 463/// RangePiece ::= INTVAL INTVAL 464bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) { 465 if (Lex.getCode() != tgtok::IntVal) { 466 TokError("expected integer or bitrange"); 467 return true; 468 } 469 int64_t Start = Lex.getCurIntVal(); 470 int64_t End; 471 472 if (Start < 0) 473 return TokError("invalid range, cannot be negative"); 474 475 switch (Lex.Lex()) { // eat first character. 476 default: 477 Ranges.push_back(Start); 478 return false; 479 case tgtok::minus: 480 if (Lex.Lex() != tgtok::IntVal) { 481 TokError("expected integer value as end of range"); 482 return true; 483 } 484 End = Lex.getCurIntVal(); 485 break; 486 case tgtok::IntVal: 487 End = -Lex.getCurIntVal(); 488 break; 489 } 490 if (End < 0) 491 return TokError("invalid range, cannot be negative"); 492 Lex.Lex(); 493 494 // Add to the range. 495 if (Start < End) { 496 for (; Start <= End; ++Start) 497 Ranges.push_back(Start); 498 } else { 499 for (; Start >= End; --Start) 500 Ranges.push_back(Start); 501 } 502 return false; 503} 504 505/// ParseRangeList - Parse a list of scalars and ranges into scalar values. 506/// 507/// RangeList ::= RangePiece (',' RangePiece)* 508/// 509std::vector<unsigned> TGParser::ParseRangeList() { 510 std::vector<unsigned> Result; 511 512 // Parse the first piece. 513 if (ParseRangePiece(Result)) 514 return std::vector<unsigned>(); 515 while (Lex.getCode() == tgtok::comma) { 516 Lex.Lex(); // Eat the comma. 517 518 // Parse the next range piece. 519 if (ParseRangePiece(Result)) 520 return std::vector<unsigned>(); 521 } 522 return Result; 523} 524 525/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing. 526/// OptionalRangeList ::= '<' RangeList '>' 527/// OptionalRangeList ::= /*empty*/ 528bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) { 529 if (Lex.getCode() != tgtok::less) 530 return false; 531 532 SMLoc StartLoc = Lex.getLoc(); 533 Lex.Lex(); // eat the '<' 534 535 // Parse the range list. 536 Ranges = ParseRangeList(); 537 if (Ranges.empty()) return true; 538 539 if (Lex.getCode() != tgtok::greater) { 540 TokError("expected '>' at end of range list"); 541 return Error(StartLoc, "to match this '<'"); 542 } 543 Lex.Lex(); // eat the '>'. 544 return false; 545} 546 547/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing. 548/// OptionalBitList ::= '{' RangeList '}' 549/// OptionalBitList ::= /*empty*/ 550bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) { 551 if (Lex.getCode() != tgtok::l_brace) 552 return false; 553 554 SMLoc StartLoc = Lex.getLoc(); 555 Lex.Lex(); // eat the '{' 556 557 // Parse the range list. 558 Ranges = ParseRangeList(); 559 if (Ranges.empty()) return true; 560 561 if (Lex.getCode() != tgtok::r_brace) { 562 TokError("expected '}' at end of bit list"); 563 return Error(StartLoc, "to match this '{'"); 564 } 565 Lex.Lex(); // eat the '}'. 566 return false; 567} 568 569 570/// ParseType - Parse and return a tblgen type. This returns null on error. 571/// 572/// Type ::= STRING // string type 573/// Type ::= BIT // bit type 574/// Type ::= BITS '<' INTVAL '>' // bits<x> type 575/// Type ::= INT // int type 576/// Type ::= LIST '<' Type '>' // list<x> type 577/// Type ::= CODE // code type 578/// Type ::= DAG // dag type 579/// Type ::= ClassID // Record Type 580/// 581RecTy *TGParser::ParseType() { 582 switch (Lex.getCode()) { 583 default: TokError("Unknown token when expecting a type"); return 0; 584 case tgtok::String: Lex.Lex(); return StringRecTy::get(); 585 case tgtok::Bit: Lex.Lex(); return BitRecTy::get(); 586 case tgtok::Int: Lex.Lex(); return IntRecTy::get(); 587 case tgtok::Code: Lex.Lex(); return CodeRecTy::get(); 588 case tgtok::Dag: Lex.Lex(); return DagRecTy::get(); 589 case tgtok::Id: 590 if (Record *R = ParseClassID()) return RecordRecTy::get(R); 591 return 0; 592 case tgtok::Bits: { 593 if (Lex.Lex() != tgtok::less) { // Eat 'bits' 594 TokError("expected '<' after bits type"); 595 return 0; 596 } 597 if (Lex.Lex() != tgtok::IntVal) { // Eat '<' 598 TokError("expected integer in bits<n> type"); 599 return 0; 600 } 601 uint64_t Val = Lex.getCurIntVal(); 602 if (Lex.Lex() != tgtok::greater) { // Eat count. 603 TokError("expected '>' at end of bits<n> type"); 604 return 0; 605 } 606 Lex.Lex(); // Eat '>' 607 return BitsRecTy::get(Val); 608 } 609 case tgtok::List: { 610 if (Lex.Lex() != tgtok::less) { // Eat 'bits' 611 TokError("expected '<' after list type"); 612 return 0; 613 } 614 Lex.Lex(); // Eat '<' 615 RecTy *SubType = ParseType(); 616 if (SubType == 0) return 0; 617 618 if (Lex.getCode() != tgtok::greater) { 619 TokError("expected '>' at end of list<ty> type"); 620 return 0; 621 } 622 Lex.Lex(); // Eat '>' 623 return ListRecTy::get(SubType); 624 } 625 } 626} 627 628/// ParseIDValue - Parse an ID as a value and decode what it means. 629/// 630/// IDValue ::= ID [def local value] 631/// IDValue ::= ID [def template arg] 632/// IDValue ::= ID [multiclass local value] 633/// IDValue ::= ID [multiclass template argument] 634/// IDValue ::= ID [def name] 635/// 636Init *TGParser::ParseIDValue(Record *CurRec) { 637 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue"); 638 std::string Name = Lex.getCurStrVal(); 639 SMLoc Loc = Lex.getLoc(); 640 Lex.Lex(); 641 return ParseIDValue(CurRec, Name, Loc); 642} 643 644/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID 645/// has already been read. 646Init *TGParser::ParseIDValue(Record *CurRec, 647 const std::string &Name, SMLoc NameLoc) { 648 if (CurRec) { 649 if (const RecordVal *RV = CurRec->getValue(Name)) 650 return VarInit::get(Name, RV->getType()); 651 652 std::string TemplateArgName = CurRec->getName()+":"+Name; 653 if (CurMultiClass) 654 TemplateArgName = CurMultiClass->Rec.getName()+"::"+TemplateArgName; 655 656 if (CurRec->isTemplateArg(TemplateArgName)) { 657 const RecordVal *RV = CurRec->getValue(TemplateArgName); 658 assert(RV && "Template arg doesn't exist??"); 659 return VarInit::get(TemplateArgName, RV->getType()); 660 } 661 } 662 663 if (CurMultiClass) { 664 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name; 665 if (CurMultiClass->Rec.isTemplateArg(MCName)) { 666 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName); 667 assert(RV && "Template arg doesn't exist??"); 668 return VarInit::get(MCName, RV->getType()); 669 } 670 } 671 672 if (Record *D = Records.getDef(Name)) 673 return DefInit::get(D); 674 675 Error(NameLoc, "Variable not defined: '" + Name + "'"); 676 return 0; 677} 678 679/// ParseOperation - Parse an operator. This returns null on error. 680/// 681/// Operation ::= XOperator ['<' Type '>'] '(' Args ')' 682/// 683Init *TGParser::ParseOperation(Record *CurRec) { 684 switch (Lex.getCode()) { 685 default: 686 TokError("unknown operation"); 687 return 0; 688 break; 689 case tgtok::XHead: 690 case tgtok::XTail: 691 case tgtok::XEmpty: 692 case tgtok::XCast: { // Value ::= !unop '(' Value ')' 693 UnOpInit::UnaryOp Code; 694 RecTy *Type = 0; 695 696 switch (Lex.getCode()) { 697 default: assert(0 && "Unhandled code!"); 698 case tgtok::XCast: 699 Lex.Lex(); // eat the operation 700 Code = UnOpInit::CAST; 701 702 Type = ParseOperatorType(); 703 704 if (Type == 0) { 705 TokError("did not get type for unary operator"); 706 return 0; 707 } 708 709 break; 710 case tgtok::XHead: 711 Lex.Lex(); // eat the operation 712 Code = UnOpInit::HEAD; 713 break; 714 case tgtok::XTail: 715 Lex.Lex(); // eat the operation 716 Code = UnOpInit::TAIL; 717 break; 718 case tgtok::XEmpty: 719 Lex.Lex(); // eat the operation 720 Code = UnOpInit::EMPTY; 721 Type = IntRecTy::get(); 722 break; 723 } 724 if (Lex.getCode() != tgtok::l_paren) { 725 TokError("expected '(' after unary operator"); 726 return 0; 727 } 728 Lex.Lex(); // eat the '(' 729 730 Init *LHS = ParseValue(CurRec); 731 if (LHS == 0) return 0; 732 733 if (Code == UnOpInit::HEAD 734 || Code == UnOpInit::TAIL 735 || Code == UnOpInit::EMPTY) { 736 ListInit *LHSl = dynamic_cast<ListInit*>(LHS); 737 StringInit *LHSs = dynamic_cast<StringInit*>(LHS); 738 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS); 739 if (LHSl == 0 && LHSs == 0 && LHSt == 0) { 740 TokError("expected list or string type argument in unary operator"); 741 return 0; 742 } 743 if (LHSt) { 744 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType()); 745 StringRecTy *SType = dynamic_cast<StringRecTy*>(LHSt->getType()); 746 if (LType == 0 && SType == 0) { 747 TokError("expected list or string type argumnet in unary operator"); 748 return 0; 749 } 750 } 751 752 if (Code == UnOpInit::HEAD 753 || Code == UnOpInit::TAIL) { 754 if (LHSl == 0 && LHSt == 0) { 755 TokError("expected list type argumnet in unary operator"); 756 return 0; 757 } 758 759 if (LHSl && LHSl->getSize() == 0) { 760 TokError("empty list argument in unary operator"); 761 return 0; 762 } 763 if (LHSl) { 764 Init *Item = LHSl->getElement(0); 765 TypedInit *Itemt = dynamic_cast<TypedInit*>(Item); 766 if (Itemt == 0) { 767 TokError("untyped list element in unary operator"); 768 return 0; 769 } 770 if (Code == UnOpInit::HEAD) { 771 Type = Itemt->getType(); 772 } else { 773 Type = ListRecTy::get(Itemt->getType()); 774 } 775 } else { 776 assert(LHSt && "expected list type argument in unary operator"); 777 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType()); 778 if (LType == 0) { 779 TokError("expected list type argumnet in unary operator"); 780 return 0; 781 } 782 if (Code == UnOpInit::HEAD) { 783 Type = LType->getElementType(); 784 } else { 785 Type = LType; 786 } 787 } 788 } 789 } 790 791 if (Lex.getCode() != tgtok::r_paren) { 792 TokError("expected ')' in unary operator"); 793 return 0; 794 } 795 Lex.Lex(); // eat the ')' 796 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass); 797 } 798 799 case tgtok::XConcat: 800 case tgtok::XSRA: 801 case tgtok::XSRL: 802 case tgtok::XSHL: 803 case tgtok::XEq: 804 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')' 805 tgtok::TokKind OpTok = Lex.getCode(); 806 SMLoc OpLoc = Lex.getLoc(); 807 Lex.Lex(); // eat the operation 808 809 BinOpInit::BinaryOp Code; 810 RecTy *Type = 0; 811 812 switch (OpTok) { 813 default: assert(0 && "Unhandled code!"); 814 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break; 815 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break; 816 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break; 817 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break; 818 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break; 819 case tgtok::XStrConcat: 820 Code = BinOpInit::STRCONCAT; 821 Type = StringRecTy::get(); 822 break; 823 } 824 825 if (Lex.getCode() != tgtok::l_paren) { 826 TokError("expected '(' after binary operator"); 827 return 0; 828 } 829 Lex.Lex(); // eat the '(' 830 831 SmallVector<Init*, 2> InitList; 832 833 InitList.push_back(ParseValue(CurRec)); 834 if (InitList.back() == 0) return 0; 835 836 while (Lex.getCode() == tgtok::comma) { 837 Lex.Lex(); // eat the ',' 838 839 InitList.push_back(ParseValue(CurRec)); 840 if (InitList.back() == 0) return 0; 841 } 842 843 if (Lex.getCode() != tgtok::r_paren) { 844 TokError("expected ')' in operator"); 845 return 0; 846 } 847 Lex.Lex(); // eat the ')' 848 849 // We allow multiple operands to associative operators like !strconcat as 850 // shorthand for nesting them. 851 if (Code == BinOpInit::STRCONCAT) { 852 while (InitList.size() > 2) { 853 Init *RHS = InitList.pop_back_val(); 854 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type)) 855 ->Fold(CurRec, CurMultiClass); 856 InitList.back() = RHS; 857 } 858 } 859 860 if (InitList.size() == 2) 861 return (BinOpInit::get(Code, InitList[0], InitList[1], Type)) 862 ->Fold(CurRec, CurMultiClass); 863 864 Error(OpLoc, "expected two operands to operator"); 865 return 0; 866 } 867 868 case tgtok::XIf: 869 case tgtok::XForEach: 870 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')' 871 TernOpInit::TernaryOp Code; 872 RecTy *Type = 0; 873 874 tgtok::TokKind LexCode = Lex.getCode(); 875 Lex.Lex(); // eat the operation 876 switch (LexCode) { 877 default: assert(0 && "Unhandled code!"); 878 case tgtok::XIf: 879 Code = TernOpInit::IF; 880 break; 881 case tgtok::XForEach: 882 Code = TernOpInit::FOREACH; 883 break; 884 case tgtok::XSubst: 885 Code = TernOpInit::SUBST; 886 break; 887 } 888 if (Lex.getCode() != tgtok::l_paren) { 889 TokError("expected '(' after ternary operator"); 890 return 0; 891 } 892 Lex.Lex(); // eat the '(' 893 894 Init *LHS = ParseValue(CurRec); 895 if (LHS == 0) return 0; 896 897 if (Lex.getCode() != tgtok::comma) { 898 TokError("expected ',' in ternary operator"); 899 return 0; 900 } 901 Lex.Lex(); // eat the ',' 902 903 Init *MHS = ParseValue(CurRec); 904 if (MHS == 0) return 0; 905 906 if (Lex.getCode() != tgtok::comma) { 907 TokError("expected ',' in ternary operator"); 908 return 0; 909 } 910 Lex.Lex(); // eat the ',' 911 912 Init *RHS = ParseValue(CurRec); 913 if (RHS == 0) return 0; 914 915 if (Lex.getCode() != tgtok::r_paren) { 916 TokError("expected ')' in binary operator"); 917 return 0; 918 } 919 Lex.Lex(); // eat the ')' 920 921 switch (LexCode) { 922 default: assert(0 && "Unhandled code!"); 923 case tgtok::XIf: { 924 // FIXME: The `!if' operator doesn't handle non-TypedInit well at 925 // all. This can be made much more robust. 926 TypedInit *MHSt = dynamic_cast<TypedInit*>(MHS); 927 TypedInit *RHSt = dynamic_cast<TypedInit*>(RHS); 928 929 RecTy *MHSTy = 0; 930 RecTy *RHSTy = 0; 931 932 if (MHSt == 0 && RHSt == 0) { 933 BitsInit *MHSbits = dynamic_cast<BitsInit*>(MHS); 934 BitsInit *RHSbits = dynamic_cast<BitsInit*>(RHS); 935 936 if (MHSbits && RHSbits && 937 MHSbits->getNumBits() == RHSbits->getNumBits()) { 938 Type = BitRecTy::get(); 939 break; 940 } else { 941 BitInit *MHSbit = dynamic_cast<BitInit*>(MHS); 942 BitInit *RHSbit = dynamic_cast<BitInit*>(RHS); 943 944 if (MHSbit && RHSbit) { 945 Type = BitRecTy::get(); 946 break; 947 } 948 } 949 } else if (MHSt != 0 && RHSt != 0) { 950 MHSTy = MHSt->getType(); 951 RHSTy = RHSt->getType(); 952 } 953 954 if (!MHSTy || !RHSTy) { 955 TokError("could not get type for !if"); 956 return 0; 957 } 958 959 if (MHSTy->typeIsConvertibleTo(RHSTy)) { 960 Type = RHSTy; 961 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) { 962 Type = MHSTy; 963 } else { 964 TokError("inconsistent types for !if"); 965 return 0; 966 } 967 break; 968 } 969 case tgtok::XForEach: { 970 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS); 971 if (MHSt == 0) { 972 TokError("could not get type for !foreach"); 973 return 0; 974 } 975 Type = MHSt->getType(); 976 break; 977 } 978 case tgtok::XSubst: { 979 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS); 980 if (RHSt == 0) { 981 TokError("could not get type for !subst"); 982 return 0; 983 } 984 Type = RHSt->getType(); 985 break; 986 } 987 } 988 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec, 989 CurMultiClass); 990 } 991 } 992 TokError("could not parse operation"); 993 return 0; 994} 995 996/// ParseOperatorType - Parse a type for an operator. This returns 997/// null on error. 998/// 999/// OperatorType ::= '<' Type '>' 1000/// 1001RecTy *TGParser::ParseOperatorType() { 1002 RecTy *Type = 0; 1003 1004 if (Lex.getCode() != tgtok::less) { 1005 TokError("expected type name for operator"); 1006 return 0; 1007 } 1008 Lex.Lex(); // eat the < 1009 1010 Type = ParseType(); 1011 1012 if (Type == 0) { 1013 TokError("expected type name for operator"); 1014 return 0; 1015 } 1016 1017 if (Lex.getCode() != tgtok::greater) { 1018 TokError("expected type name for operator"); 1019 return 0; 1020 } 1021 Lex.Lex(); // eat the > 1022 1023 return Type; 1024} 1025 1026 1027/// ParseSimpleValue - Parse a tblgen value. This returns null on error. 1028/// 1029/// SimpleValue ::= IDValue 1030/// SimpleValue ::= INTVAL 1031/// SimpleValue ::= STRVAL+ 1032/// SimpleValue ::= CODEFRAGMENT 1033/// SimpleValue ::= '?' 1034/// SimpleValue ::= '{' ValueList '}' 1035/// SimpleValue ::= ID '<' ValueListNE '>' 1036/// SimpleValue ::= '[' ValueList ']' 1037/// SimpleValue ::= '(' IDValue DagArgList ')' 1038/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')' 1039/// SimpleValue ::= SHLTOK '(' Value ',' Value ')' 1040/// SimpleValue ::= SRATOK '(' Value ',' Value ')' 1041/// SimpleValue ::= SRLTOK '(' Value ',' Value ')' 1042/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')' 1043/// 1044Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType) { 1045 Init *R = 0; 1046 switch (Lex.getCode()) { 1047 default: TokError("Unknown token when parsing a value"); break; 1048 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break; 1049 case tgtok::StrVal: { 1050 std::string Val = Lex.getCurStrVal(); 1051 Lex.Lex(); 1052 1053 // Handle multiple consecutive concatenated strings. 1054 while (Lex.getCode() == tgtok::StrVal) { 1055 Val += Lex.getCurStrVal(); 1056 Lex.Lex(); 1057 } 1058 1059 R = StringInit::get(Val); 1060 break; 1061 } 1062 case tgtok::CodeFragment: 1063 R = CodeInit::get(Lex.getCurStrVal()); 1064 Lex.Lex(); 1065 break; 1066 case tgtok::question: 1067 R = UnsetInit::get(); 1068 Lex.Lex(); 1069 break; 1070 case tgtok::Id: { 1071 SMLoc NameLoc = Lex.getLoc(); 1072 std::string Name = Lex.getCurStrVal(); 1073 if (Lex.Lex() != tgtok::less) // consume the Id. 1074 return ParseIDValue(CurRec, Name, NameLoc); // Value ::= IDValue 1075 1076 // Value ::= ID '<' ValueListNE '>' 1077 if (Lex.Lex() == tgtok::greater) { 1078 TokError("expected non-empty value list"); 1079 return 0; 1080 } 1081 1082 // This is a CLASS<initvalslist> expression. This is supposed to synthesize 1083 // a new anonymous definition, deriving from CLASS<initvalslist> with no 1084 // body. 1085 Record *Class = Records.getClass(Name); 1086 if (!Class) { 1087 Error(NameLoc, "Expected a class name, got '" + Name + "'"); 1088 return 0; 1089 } 1090 1091 std::vector<Init*> ValueList = ParseValueList(CurRec, Class); 1092 if (ValueList.empty()) return 0; 1093 1094 if (Lex.getCode() != tgtok::greater) { 1095 TokError("expected '>' at end of value list"); 1096 return 0; 1097 } 1098 Lex.Lex(); // eat the '>' 1099 1100 // Create the new record, set it as CurRec temporarily. 1101 static unsigned AnonCounter = 0; 1102 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++), 1103 NameLoc, 1104 Records); 1105 SubClassReference SCRef; 1106 SCRef.RefLoc = NameLoc; 1107 SCRef.Rec = Class; 1108 SCRef.TemplateArgs = ValueList; 1109 // Add info about the subclass to NewRec. 1110 if (AddSubClass(NewRec, SCRef)) 1111 return 0; 1112 NewRec->resolveReferences(); 1113 Records.addDef(NewRec); 1114 1115 // The result of the expression is a reference to the new record. 1116 return DefInit::get(NewRec); 1117 } 1118 case tgtok::l_brace: { // Value ::= '{' ValueList '}' 1119 SMLoc BraceLoc = Lex.getLoc(); 1120 Lex.Lex(); // eat the '{' 1121 std::vector<Init*> Vals; 1122 1123 if (Lex.getCode() != tgtok::r_brace) { 1124 Vals = ParseValueList(CurRec); 1125 if (Vals.empty()) return 0; 1126 } 1127 if (Lex.getCode() != tgtok::r_brace) { 1128 TokError("expected '}' at end of bit list value"); 1129 return 0; 1130 } 1131 Lex.Lex(); // eat the '}' 1132 1133 SmallVector<Init *, 16> NewBits(Vals.size()); 1134 1135 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 1136 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get()); 1137 if (Bit == 0) { 1138 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+ 1139 ") is not convertable to a bit"); 1140 return 0; 1141 } 1142 NewBits[Vals.size()-i-1] = Bit; 1143 } 1144 return BitsInit::get(NewBits); 1145 } 1146 case tgtok::l_square: { // Value ::= '[' ValueList ']' 1147 Lex.Lex(); // eat the '[' 1148 std::vector<Init*> Vals; 1149 1150 RecTy *DeducedEltTy = 0; 1151 ListRecTy *GivenListTy = 0; 1152 1153 if (ItemType != 0) { 1154 ListRecTy *ListType = dynamic_cast<ListRecTy*>(ItemType); 1155 if (ListType == 0) { 1156 std::stringstream s; 1157 s << "Type mismatch for list, expected list type, got " 1158 << ItemType->getAsString(); 1159 TokError(s.str()); 1160 return 0; 1161 } 1162 GivenListTy = ListType; 1163 } 1164 1165 if (Lex.getCode() != tgtok::r_square) { 1166 Vals = ParseValueList(CurRec, 0, 1167 GivenListTy ? GivenListTy->getElementType() : 0); 1168 if (Vals.empty()) return 0; 1169 } 1170 if (Lex.getCode() != tgtok::r_square) { 1171 TokError("expected ']' at end of list value"); 1172 return 0; 1173 } 1174 Lex.Lex(); // eat the ']' 1175 1176 RecTy *GivenEltTy = 0; 1177 if (Lex.getCode() == tgtok::less) { 1178 // Optional list element type 1179 Lex.Lex(); // eat the '<' 1180 1181 GivenEltTy = ParseType(); 1182 if (GivenEltTy == 0) { 1183 // Couldn't parse element type 1184 return 0; 1185 } 1186 1187 if (Lex.getCode() != tgtok::greater) { 1188 TokError("expected '>' at end of list element type"); 1189 return 0; 1190 } 1191 Lex.Lex(); // eat the '>' 1192 } 1193 1194 // Check elements 1195 RecTy *EltTy = 0; 1196 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end(); 1197 i != ie; 1198 ++i) { 1199 TypedInit *TArg = dynamic_cast<TypedInit*>(*i); 1200 if (TArg == 0) { 1201 TokError("Untyped list element"); 1202 return 0; 1203 } 1204 if (EltTy != 0) { 1205 EltTy = resolveTypes(EltTy, TArg->getType()); 1206 if (EltTy == 0) { 1207 TokError("Incompatible types in list elements"); 1208 return 0; 1209 } 1210 } else { 1211 EltTy = TArg->getType(); 1212 } 1213 } 1214 1215 if (GivenEltTy != 0) { 1216 if (EltTy != 0) { 1217 // Verify consistency 1218 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) { 1219 TokError("Incompatible types in list elements"); 1220 return 0; 1221 } 1222 } 1223 EltTy = GivenEltTy; 1224 } 1225 1226 if (EltTy == 0) { 1227 if (ItemType == 0) { 1228 TokError("No type for list"); 1229 return 0; 1230 } 1231 DeducedEltTy = GivenListTy->getElementType(); 1232 } else { 1233 // Make sure the deduced type is compatible with the given type 1234 if (GivenListTy) { 1235 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) { 1236 TokError("Element type mismatch for list"); 1237 return 0; 1238 } 1239 } 1240 DeducedEltTy = EltTy; 1241 } 1242 1243 return ListInit::get(Vals, DeducedEltTy); 1244 } 1245 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')' 1246 Lex.Lex(); // eat the '(' 1247 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) { 1248 TokError("expected identifier in dag init"); 1249 return 0; 1250 } 1251 1252 Init *Operator = ParseValue(CurRec); 1253 if (Operator == 0) return 0; 1254 1255 // If the operator name is present, parse it. 1256 std::string OperatorName; 1257 if (Lex.getCode() == tgtok::colon) { 1258 if (Lex.Lex() != tgtok::VarName) { // eat the ':' 1259 TokError("expected variable name in dag operator"); 1260 return 0; 1261 } 1262 OperatorName = Lex.getCurStrVal(); 1263 Lex.Lex(); // eat the VarName. 1264 } 1265 1266 std::vector<std::pair<llvm::Init*, std::string> > DagArgs; 1267 if (Lex.getCode() != tgtok::r_paren) { 1268 DagArgs = ParseDagArgList(CurRec); 1269 if (DagArgs.empty()) return 0; 1270 } 1271 1272 if (Lex.getCode() != tgtok::r_paren) { 1273 TokError("expected ')' in dag init"); 1274 return 0; 1275 } 1276 Lex.Lex(); // eat the ')' 1277 1278 return DagInit::get(Operator, OperatorName, DagArgs); 1279 } 1280 1281 case tgtok::XHead: 1282 case tgtok::XTail: 1283 case tgtok::XEmpty: 1284 case tgtok::XCast: // Value ::= !unop '(' Value ')' 1285 case tgtok::XConcat: 1286 case tgtok::XSRA: 1287 case tgtok::XSRL: 1288 case tgtok::XSHL: 1289 case tgtok::XEq: 1290 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')' 1291 case tgtok::XIf: 1292 case tgtok::XForEach: 1293 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')' 1294 return ParseOperation(CurRec); 1295 } 1296 } 1297 1298 return R; 1299} 1300 1301/// ParseValue - Parse a tblgen value. This returns null on error. 1302/// 1303/// Value ::= SimpleValue ValueSuffix* 1304/// ValueSuffix ::= '{' BitList '}' 1305/// ValueSuffix ::= '[' BitList ']' 1306/// ValueSuffix ::= '.' ID 1307/// 1308Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType) { 1309 Init *Result = ParseSimpleValue(CurRec, ItemType); 1310 if (Result == 0) return 0; 1311 1312 // Parse the suffixes now if present. 1313 while (1) { 1314 switch (Lex.getCode()) { 1315 default: return Result; 1316 case tgtok::l_brace: { 1317 SMLoc CurlyLoc = Lex.getLoc(); 1318 Lex.Lex(); // eat the '{' 1319 std::vector<unsigned> Ranges = ParseRangeList(); 1320 if (Ranges.empty()) return 0; 1321 1322 // Reverse the bitlist. 1323 std::reverse(Ranges.begin(), Ranges.end()); 1324 Result = Result->convertInitializerBitRange(Ranges); 1325 if (Result == 0) { 1326 Error(CurlyLoc, "Invalid bit range for value"); 1327 return 0; 1328 } 1329 1330 // Eat the '}'. 1331 if (Lex.getCode() != tgtok::r_brace) { 1332 TokError("expected '}' at end of bit range list"); 1333 return 0; 1334 } 1335 Lex.Lex(); 1336 break; 1337 } 1338 case tgtok::l_square: { 1339 SMLoc SquareLoc = Lex.getLoc(); 1340 Lex.Lex(); // eat the '[' 1341 std::vector<unsigned> Ranges = ParseRangeList(); 1342 if (Ranges.empty()) return 0; 1343 1344 Result = Result->convertInitListSlice(Ranges); 1345 if (Result == 0) { 1346 Error(SquareLoc, "Invalid range for list slice"); 1347 return 0; 1348 } 1349 1350 // Eat the ']'. 1351 if (Lex.getCode() != tgtok::r_square) { 1352 TokError("expected ']' at end of list slice"); 1353 return 0; 1354 } 1355 Lex.Lex(); 1356 break; 1357 } 1358 case tgtok::period: 1359 if (Lex.Lex() != tgtok::Id) { // eat the . 1360 TokError("expected field identifier after '.'"); 1361 return 0; 1362 } 1363 if (!Result->getFieldType(Lex.getCurStrVal())) { 1364 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" + 1365 Result->getAsString() + "'"); 1366 return 0; 1367 } 1368 Result = FieldInit::get(Result, Lex.getCurStrVal()); 1369 Lex.Lex(); // eat field name 1370 break; 1371 } 1372 } 1373} 1374 1375/// ParseDagArgList - Parse the argument list for a dag literal expression. 1376/// 1377/// ParseDagArgList ::= Value (':' VARNAME)? 1378/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)? 1379std::vector<std::pair<llvm::Init*, std::string> > 1380TGParser::ParseDagArgList(Record *CurRec) { 1381 std::vector<std::pair<llvm::Init*, std::string> > Result; 1382 1383 while (1) { 1384 Init *Val = ParseValue(CurRec); 1385 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >(); 1386 1387 // If the variable name is present, add it. 1388 std::string VarName; 1389 if (Lex.getCode() == tgtok::colon) { 1390 if (Lex.Lex() != tgtok::VarName) { // eat the ':' 1391 TokError("expected variable name in dag literal"); 1392 return std::vector<std::pair<llvm::Init*, std::string> >(); 1393 } 1394 VarName = Lex.getCurStrVal(); 1395 Lex.Lex(); // eat the VarName. 1396 } 1397 1398 Result.push_back(std::make_pair(Val, VarName)); 1399 1400 if (Lex.getCode() != tgtok::comma) break; 1401 Lex.Lex(); // eat the ',' 1402 } 1403 1404 return Result; 1405} 1406 1407 1408/// ParseValueList - Parse a comma separated list of values, returning them as a 1409/// vector. Note that this always expects to be able to parse at least one 1410/// value. It returns an empty list if this is not possible. 1411/// 1412/// ValueList ::= Value (',' Value) 1413/// 1414std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec, 1415 RecTy *EltTy) { 1416 std::vector<Init*> Result; 1417 RecTy *ItemType = EltTy; 1418 unsigned int ArgN = 0; 1419 if (ArgsRec != 0 && EltTy == 0) { 1420 const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs(); 1421 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]); 1422 if (!RV) { 1423 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN] 1424 << ")\n"; 1425 } 1426 assert(RV && "Template argument record not found??"); 1427 ItemType = RV->getType(); 1428 ++ArgN; 1429 } 1430 Result.push_back(ParseValue(CurRec, ItemType)); 1431 if (Result.back() == 0) return std::vector<Init*>(); 1432 1433 while (Lex.getCode() == tgtok::comma) { 1434 Lex.Lex(); // Eat the comma 1435 1436 if (ArgsRec != 0 && EltTy == 0) { 1437 const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs(); 1438 if (ArgN >= TArgs.size()) { 1439 TokError("too many template arguments"); 1440 return std::vector<Init*>(); 1441 } 1442 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]); 1443 assert(RV && "Template argument record not found??"); 1444 ItemType = RV->getType(); 1445 ++ArgN; 1446 } 1447 Result.push_back(ParseValue(CurRec, ItemType)); 1448 if (Result.back() == 0) return std::vector<Init*>(); 1449 } 1450 1451 return Result; 1452} 1453 1454 1455/// ParseDeclaration - Read a declaration, returning the name of field ID, or an 1456/// empty string on error. This can happen in a number of different context's, 1457/// including within a def or in the template args for a def (which which case 1458/// CurRec will be non-null) and within the template args for a multiclass (in 1459/// which case CurRec will be null, but CurMultiClass will be set). This can 1460/// also happen within a def that is within a multiclass, which will set both 1461/// CurRec and CurMultiClass. 1462/// 1463/// Declaration ::= FIELD? Type ID ('=' Value)? 1464/// 1465std::string TGParser::ParseDeclaration(Record *CurRec, 1466 bool ParsingTemplateArgs) { 1467 // Read the field prefix if present. 1468 bool HasField = Lex.getCode() == tgtok::Field; 1469 if (HasField) Lex.Lex(); 1470 1471 RecTy *Type = ParseType(); 1472 if (Type == 0) return ""; 1473 1474 if (Lex.getCode() != tgtok::Id) { 1475 TokError("Expected identifier in declaration"); 1476 return ""; 1477 } 1478 1479 SMLoc IdLoc = Lex.getLoc(); 1480 std::string DeclName = Lex.getCurStrVal(); 1481 Lex.Lex(); 1482 1483 if (ParsingTemplateArgs) { 1484 if (CurRec) { 1485 DeclName = CurRec->getName() + ":" + DeclName; 1486 } else { 1487 assert(CurMultiClass); 1488 } 1489 if (CurMultiClass) 1490 DeclName = CurMultiClass->Rec.getName() + "::" + DeclName; 1491 } 1492 1493 // Add the value. 1494 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField))) 1495 return ""; 1496 1497 // If a value is present, parse it. 1498 if (Lex.getCode() == tgtok::equal) { 1499 Lex.Lex(); 1500 SMLoc ValLoc = Lex.getLoc(); 1501 Init *Val = ParseValue(CurRec, Type); 1502 if (Val == 0 || 1503 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val)) 1504 return ""; 1505 } 1506 1507 return DeclName; 1508} 1509 1510/// ParseTemplateArgList - Read a template argument list, which is a non-empty 1511/// sequence of template-declarations in <>'s. If CurRec is non-null, these are 1512/// template args for a def, which may or may not be in a multiclass. If null, 1513/// these are the template args for a multiclass. 1514/// 1515/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>' 1516/// 1517bool TGParser::ParseTemplateArgList(Record *CurRec) { 1518 assert(Lex.getCode() == tgtok::less && "Not a template arg list!"); 1519 Lex.Lex(); // eat the '<' 1520 1521 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec; 1522 1523 // Read the first declaration. 1524 std::string TemplArg = ParseDeclaration(CurRec, true/*templateargs*/); 1525 if (TemplArg.empty()) 1526 return true; 1527 1528 TheRecToAddTo->addTemplateArg(TemplArg); 1529 1530 while (Lex.getCode() == tgtok::comma) { 1531 Lex.Lex(); // eat the ',' 1532 1533 // Read the following declarations. 1534 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/); 1535 if (TemplArg.empty()) 1536 return true; 1537 TheRecToAddTo->addTemplateArg(TemplArg); 1538 } 1539 1540 if (Lex.getCode() != tgtok::greater) 1541 return TokError("expected '>' at end of template argument list"); 1542 Lex.Lex(); // eat the '>'. 1543 return false; 1544} 1545 1546 1547/// ParseBodyItem - Parse a single item at within the body of a def or class. 1548/// 1549/// BodyItem ::= Declaration ';' 1550/// BodyItem ::= LET ID OptionalBitList '=' Value ';' 1551bool TGParser::ParseBodyItem(Record *CurRec) { 1552 if (Lex.getCode() != tgtok::Let) { 1553 if (ParseDeclaration(CurRec, false).empty()) 1554 return true; 1555 1556 if (Lex.getCode() != tgtok::semi) 1557 return TokError("expected ';' after declaration"); 1558 Lex.Lex(); 1559 return false; 1560 } 1561 1562 // LET ID OptionalRangeList '=' Value ';' 1563 if (Lex.Lex() != tgtok::Id) 1564 return TokError("expected field identifier after let"); 1565 1566 SMLoc IdLoc = Lex.getLoc(); 1567 std::string FieldName = Lex.getCurStrVal(); 1568 Lex.Lex(); // eat the field name. 1569 1570 std::vector<unsigned> BitList; 1571 if (ParseOptionalBitList(BitList)) 1572 return true; 1573 std::reverse(BitList.begin(), BitList.end()); 1574 1575 if (Lex.getCode() != tgtok::equal) 1576 return TokError("expected '=' in let expression"); 1577 Lex.Lex(); // eat the '='. 1578 1579 RecordVal *Field = CurRec->getValue(FieldName); 1580 if (Field == 0) 1581 return TokError("Value '" + FieldName + "' unknown!"); 1582 1583 RecTy *Type = Field->getType(); 1584 1585 Init *Val = ParseValue(CurRec, Type); 1586 if (Val == 0) return true; 1587 1588 if (Lex.getCode() != tgtok::semi) 1589 return TokError("expected ';' after let expression"); 1590 Lex.Lex(); 1591 1592 return SetValue(CurRec, IdLoc, FieldName, BitList, Val); 1593} 1594 1595/// ParseBody - Read the body of a class or def. Return true on error, false on 1596/// success. 1597/// 1598/// Body ::= ';' 1599/// Body ::= '{' BodyList '}' 1600/// BodyList BodyItem* 1601/// 1602bool TGParser::ParseBody(Record *CurRec) { 1603 // If this is a null definition, just eat the semi and return. 1604 if (Lex.getCode() == tgtok::semi) { 1605 Lex.Lex(); 1606 return false; 1607 } 1608 1609 if (Lex.getCode() != tgtok::l_brace) 1610 return TokError("Expected ';' or '{' to start body"); 1611 // Eat the '{'. 1612 Lex.Lex(); 1613 1614 while (Lex.getCode() != tgtok::r_brace) 1615 if (ParseBodyItem(CurRec)) 1616 return true; 1617 1618 // Eat the '}'. 1619 Lex.Lex(); 1620 return false; 1621} 1622 1623/// ParseObjectBody - Parse the body of a def or class. This consists of an 1624/// optional ClassList followed by a Body. CurRec is the current def or class 1625/// that is being parsed. 1626/// 1627/// ObjectBody ::= BaseClassList Body 1628/// BaseClassList ::= /*empty*/ 1629/// BaseClassList ::= ':' BaseClassListNE 1630/// BaseClassListNE ::= SubClassRef (',' SubClassRef)* 1631/// 1632bool TGParser::ParseObjectBody(Record *CurRec) { 1633 // If there is a baseclass list, read it. 1634 if (Lex.getCode() == tgtok::colon) { 1635 Lex.Lex(); 1636 1637 // Read all of the subclasses. 1638 SubClassReference SubClass = ParseSubClassReference(CurRec, false); 1639 while (1) { 1640 // Check for error. 1641 if (SubClass.Rec == 0) return true; 1642 1643 // Add it. 1644 if (AddSubClass(CurRec, SubClass)) 1645 return true; 1646 1647 if (Lex.getCode() != tgtok::comma) break; 1648 Lex.Lex(); // eat ','. 1649 SubClass = ParseSubClassReference(CurRec, false); 1650 } 1651 } 1652 1653 // Process any variables on the let stack. 1654 for (unsigned i = 0, e = LetStack.size(); i != e; ++i) 1655 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j) 1656 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name, 1657 LetStack[i][j].Bits, LetStack[i][j].Value)) 1658 return true; 1659 1660 return ParseBody(CurRec); 1661} 1662 1663/// ParseDef - Parse and return a top level or multiclass def, return the record 1664/// corresponding to it. This returns null on error. 1665/// 1666/// DefInst ::= DEF ObjectName ObjectBody 1667/// 1668bool TGParser::ParseDef(MultiClass *CurMultiClass) { 1669 SMLoc DefLoc = Lex.getLoc(); 1670 assert(Lex.getCode() == tgtok::Def && "Unknown tok"); 1671 Lex.Lex(); // Eat the 'def' token. 1672 1673 // Parse ObjectName and make a record for it. 1674 Record *CurRec = new Record(ParseObjectName(), DefLoc, Records); 1675 1676 if (!CurMultiClass) { 1677 // Top-level def definition. 1678 1679 // Ensure redefinition doesn't happen. 1680 if (Records.getDef(CurRec->getName())) { 1681 Error(DefLoc, "def '" + CurRec->getName() + "' already defined"); 1682 return true; 1683 } 1684 Records.addDef(CurRec); 1685 } else { 1686 // Otherwise, a def inside a multiclass, add it to the multiclass. 1687 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i) 1688 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) { 1689 Error(DefLoc, "def '" + CurRec->getName() + 1690 "' already defined in this multiclass!"); 1691 return true; 1692 } 1693 CurMultiClass->DefPrototypes.push_back(CurRec); 1694 } 1695 1696 if (ParseObjectBody(CurRec)) 1697 return true; 1698 1699 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs. 1700 // See Record::setName(). This resolve step will see any new name 1701 // for the def that might have been created when resolving 1702 // inheritance, values and arguments above. 1703 CurRec->resolveReferences(); 1704 1705 // If ObjectBody has template arguments, it's an error. 1706 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?"); 1707 1708 if (CurMultiClass) { 1709 // Copy the template arguments for the multiclass into the def. 1710 const std::vector<std::string> &TArgs = 1711 CurMultiClass->Rec.getTemplateArgs(); 1712 1713 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { 1714 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]); 1715 assert(RV && "Template arg doesn't exist?"); 1716 CurRec->addValue(*RV); 1717 } 1718 } 1719 1720 return false; 1721} 1722 1723/// ParseClass - Parse a tblgen class definition. 1724/// 1725/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody 1726/// 1727bool TGParser::ParseClass() { 1728 assert(Lex.getCode() == tgtok::Class && "Unexpected token!"); 1729 Lex.Lex(); 1730 1731 if (Lex.getCode() != tgtok::Id) 1732 return TokError("expected class name after 'class' keyword"); 1733 1734 Record *CurRec = Records.getClass(Lex.getCurStrVal()); 1735 if (CurRec) { 1736 // If the body was previously defined, this is an error. 1737 if (!CurRec->getValues().empty() || 1738 !CurRec->getSuperClasses().empty() || 1739 !CurRec->getTemplateArgs().empty()) 1740 return TokError("Class '" + CurRec->getName() + "' already defined"); 1741 } else { 1742 // If this is the first reference to this class, create and add it. 1743 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records); 1744 Records.addClass(CurRec); 1745 } 1746 Lex.Lex(); // eat the name. 1747 1748 // If there are template args, parse them. 1749 if (Lex.getCode() == tgtok::less) 1750 if (ParseTemplateArgList(CurRec)) 1751 return true; 1752 1753 // Finally, parse the object body. 1754 return ParseObjectBody(CurRec); 1755} 1756 1757/// ParseLetList - Parse a non-empty list of assignment expressions into a list 1758/// of LetRecords. 1759/// 1760/// LetList ::= LetItem (',' LetItem)* 1761/// LetItem ::= ID OptionalRangeList '=' Value 1762/// 1763std::vector<LetRecord> TGParser::ParseLetList() { 1764 std::vector<LetRecord> Result; 1765 1766 while (1) { 1767 if (Lex.getCode() != tgtok::Id) { 1768 TokError("expected identifier in let definition"); 1769 return std::vector<LetRecord>(); 1770 } 1771 std::string Name = Lex.getCurStrVal(); 1772 SMLoc NameLoc = Lex.getLoc(); 1773 Lex.Lex(); // Eat the identifier. 1774 1775 // Check for an optional RangeList. 1776 std::vector<unsigned> Bits; 1777 if (ParseOptionalRangeList(Bits)) 1778 return std::vector<LetRecord>(); 1779 std::reverse(Bits.begin(), Bits.end()); 1780 1781 if (Lex.getCode() != tgtok::equal) { 1782 TokError("expected '=' in let expression"); 1783 return std::vector<LetRecord>(); 1784 } 1785 Lex.Lex(); // eat the '='. 1786 1787 Init *Val = ParseValue(0); 1788 if (Val == 0) return std::vector<LetRecord>(); 1789 1790 // Now that we have everything, add the record. 1791 Result.push_back(LetRecord(Name, Bits, Val, NameLoc)); 1792 1793 if (Lex.getCode() != tgtok::comma) 1794 return Result; 1795 Lex.Lex(); // eat the comma. 1796 } 1797} 1798 1799/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of 1800/// different related productions. This works inside multiclasses too. 1801/// 1802/// Object ::= LET LetList IN '{' ObjectList '}' 1803/// Object ::= LET LetList IN Object 1804/// 1805bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) { 1806 assert(Lex.getCode() == tgtok::Let && "Unexpected token"); 1807 Lex.Lex(); 1808 1809 // Add this entry to the let stack. 1810 std::vector<LetRecord> LetInfo = ParseLetList(); 1811 if (LetInfo.empty()) return true; 1812 LetStack.push_back(LetInfo); 1813 1814 if (Lex.getCode() != tgtok::In) 1815 return TokError("expected 'in' at end of top-level 'let'"); 1816 Lex.Lex(); 1817 1818 // If this is a scalar let, just handle it now 1819 if (Lex.getCode() != tgtok::l_brace) { 1820 // LET LetList IN Object 1821 if (ParseObject(CurMultiClass)) 1822 return true; 1823 } else { // Object ::= LETCommand '{' ObjectList '}' 1824 SMLoc BraceLoc = Lex.getLoc(); 1825 // Otherwise, this is a group let. 1826 Lex.Lex(); // eat the '{'. 1827 1828 // Parse the object list. 1829 if (ParseObjectList(CurMultiClass)) 1830 return true; 1831 1832 if (Lex.getCode() != tgtok::r_brace) { 1833 TokError("expected '}' at end of top level let command"); 1834 return Error(BraceLoc, "to match this '{'"); 1835 } 1836 Lex.Lex(); 1837 } 1838 1839 // Outside this let scope, this let block is not active. 1840 LetStack.pop_back(); 1841 return false; 1842} 1843 1844/// ParseMultiClass - Parse a multiclass definition. 1845/// 1846/// MultiClassInst ::= MULTICLASS ID TemplateArgList? 1847/// ':' BaseMultiClassList '{' MultiClassDef+ '}' 1848/// 1849bool TGParser::ParseMultiClass() { 1850 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token"); 1851 Lex.Lex(); // Eat the multiclass token. 1852 1853 if (Lex.getCode() != tgtok::Id) 1854 return TokError("expected identifier after multiclass for name"); 1855 std::string Name = Lex.getCurStrVal(); 1856 1857 if (MultiClasses.count(Name)) 1858 return TokError("multiclass '" + Name + "' already defined"); 1859 1860 CurMultiClass = MultiClasses[Name] = new MultiClass(Name, 1861 Lex.getLoc(), Records); 1862 Lex.Lex(); // Eat the identifier. 1863 1864 // If there are template args, parse them. 1865 if (Lex.getCode() == tgtok::less) 1866 if (ParseTemplateArgList(0)) 1867 return true; 1868 1869 bool inherits = false; 1870 1871 // If there are submulticlasses, parse them. 1872 if (Lex.getCode() == tgtok::colon) { 1873 inherits = true; 1874 1875 Lex.Lex(); 1876 1877 // Read all of the submulticlasses. 1878 SubMultiClassReference SubMultiClass = 1879 ParseSubMultiClassReference(CurMultiClass); 1880 while (1) { 1881 // Check for error. 1882 if (SubMultiClass.MC == 0) return true; 1883 1884 // Add it. 1885 if (AddSubMultiClass(CurMultiClass, SubMultiClass)) 1886 return true; 1887 1888 if (Lex.getCode() != tgtok::comma) break; 1889 Lex.Lex(); // eat ','. 1890 SubMultiClass = ParseSubMultiClassReference(CurMultiClass); 1891 } 1892 } 1893 1894 if (Lex.getCode() != tgtok::l_brace) { 1895 if (!inherits) 1896 return TokError("expected '{' in multiclass definition"); 1897 else if (Lex.getCode() != tgtok::semi) 1898 return TokError("expected ';' in multiclass definition"); 1899 else 1900 Lex.Lex(); // eat the ';'. 1901 } else { 1902 if (Lex.Lex() == tgtok::r_brace) // eat the '{'. 1903 return TokError("multiclass must contain at least one def"); 1904 1905 while (Lex.getCode() != tgtok::r_brace) { 1906 switch (Lex.getCode()) { 1907 default: 1908 return TokError("expected 'let', 'def' or 'defm' in multiclass body"); 1909 case tgtok::Let: 1910 case tgtok::Def: 1911 case tgtok::Defm: 1912 if (ParseObject(CurMultiClass)) 1913 return true; 1914 break; 1915 } 1916 } 1917 Lex.Lex(); // eat the '}'. 1918 } 1919 1920 CurMultiClass = 0; 1921 return false; 1922} 1923 1924Record *TGParser:: 1925InstantiateMulticlassDef(MultiClass &MC, 1926 Record *DefProto, 1927 const std::string &DefmPrefix, 1928 SMLoc DefmPrefixLoc) { 1929 // Add in the defm name. If the defm prefix is empty, give each 1930 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the 1931 // name, substitute the prefix for #NAME#. Otherwise, use the defm name 1932 // as a prefix. 1933 std::string DefName = DefProto->getName(); 1934 if (DefmPrefix.empty()) { 1935 DefName = GetNewAnonymousName(); 1936 } else { 1937 std::string::size_type idx = DefName.find("#NAME#"); 1938 if (idx != std::string::npos) { 1939 DefName.replace(idx, 6, DefmPrefix); 1940 } else { 1941 // Add the suffix to the defm name to get the new name. 1942 DefName = DefmPrefix + DefName; 1943 } 1944 } 1945 1946 Record *CurRec = new Record(DefName, DefmPrefixLoc, Records); 1947 1948 SubClassReference Ref; 1949 Ref.RefLoc = DefmPrefixLoc; 1950 Ref.Rec = DefProto; 1951 AddSubClass(CurRec, Ref); 1952 1953 return CurRec; 1954} 1955 1956bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC, 1957 Record *CurRec, 1958 SMLoc DefmPrefixLoc, 1959 SMLoc SubClassLoc, 1960 const std::vector<std::string> &TArgs, 1961 std::vector<Init *> &TemplateVals, 1962 bool DeleteArgs) { 1963 // Loop over all of the template arguments, setting them to the specified 1964 // value or leaving them as the default if necessary. 1965 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { 1966 // Check if a value is specified for this temp-arg. 1967 if (i < TemplateVals.size()) { 1968 // Set it now. 1969 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(), 1970 TemplateVals[i])) 1971 return true; 1972 1973 // Resolve it next. 1974 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i])); 1975 1976 if (DeleteArgs) 1977 // Now remove it. 1978 CurRec->removeValue(TArgs[i]); 1979 1980 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) { 1981 return Error(SubClassLoc, "value not specified for template argument #"+ 1982 utostr(i) + " (" + TArgs[i] + ") of multiclassclass '" + 1983 MC.Rec.getName() + "'"); 1984 } 1985 } 1986 return false; 1987} 1988 1989bool TGParser::ResolveMulticlassDef(MultiClass &MC, 1990 Record *CurRec, 1991 Record *DefProto, 1992 SMLoc DefmPrefixLoc) { 1993 // If the mdef is inside a 'let' expression, add to each def. 1994 for (unsigned i = 0, e = LetStack.size(); i != e; ++i) 1995 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j) 1996 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name, 1997 LetStack[i][j].Bits, LetStack[i][j].Value)) 1998 return Error(DefmPrefixLoc, "when instantiating this defm"); 1999 2000 // Ensure redefinition doesn't happen. 2001 if (Records.getDef(CurRec->getName())) 2002 return Error(DefmPrefixLoc, "def '" + CurRec->getName() + 2003 "' already defined, instantiating defm with subdef '" + 2004 DefProto->getName() + "'"); 2005 2006 // Don't create a top level definition for defm inside multiclasses, 2007 // instead, only update the prototypes and bind the template args 2008 // with the new created definition. 2009 if (CurMultiClass) { 2010 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); 2011 i != e; ++i) 2012 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) 2013 return Error(DefmPrefixLoc, "defm '" + CurRec->getName() + 2014 "' already defined in this multiclass!"); 2015 CurMultiClass->DefPrototypes.push_back(CurRec); 2016 2017 // Copy the template arguments for the multiclass into the new def. 2018 const std::vector<std::string> &TA = 2019 CurMultiClass->Rec.getTemplateArgs(); 2020 2021 for (unsigned i = 0, e = TA.size(); i != e; ++i) { 2022 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]); 2023 assert(RV && "Template arg doesn't exist?"); 2024 CurRec->addValue(*RV); 2025 } 2026 } else { 2027 Records.addDef(CurRec); 2028 } 2029 2030 return false; 2031} 2032 2033/// ParseDefm - Parse the instantiation of a multiclass. 2034/// 2035/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';' 2036/// 2037bool TGParser::ParseDefm(MultiClass *CurMultiClass) { 2038 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!"); 2039 2040 std::string DefmPrefix; 2041 if (Lex.Lex() == tgtok::Id) { // eat the defm. 2042 DefmPrefix = Lex.getCurStrVal(); 2043 Lex.Lex(); // Eat the defm prefix. 2044 } 2045 2046 SMLoc DefmPrefixLoc = Lex.getLoc(); 2047 if (Lex.getCode() != tgtok::colon) 2048 return TokError("expected ':' after defm identifier"); 2049 2050 // Keep track of the new generated record definitions. 2051 std::vector<Record*> NewRecDefs; 2052 2053 // This record also inherits from a regular class (non-multiclass)? 2054 bool InheritFromClass = false; 2055 2056 // eat the colon. 2057 Lex.Lex(); 2058 2059 SMLoc SubClassLoc = Lex.getLoc(); 2060 SubClassReference Ref = ParseSubClassReference(0, true); 2061 2062 while (1) { 2063 if (Ref.Rec == 0) return true; 2064 2065 // To instantiate a multiclass, we need to first get the multiclass, then 2066 // instantiate each def contained in the multiclass with the SubClassRef 2067 // template parameters. 2068 MultiClass *MC = MultiClasses[Ref.Rec->getName()]; 2069 assert(MC && "Didn't lookup multiclass correctly?"); 2070 std::vector<Init*> &TemplateVals = Ref.TemplateArgs; 2071 2072 // Verify that the correct number of template arguments were specified. 2073 const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs(); 2074 if (TArgs.size() < TemplateVals.size()) 2075 return Error(SubClassLoc, 2076 "more template args specified than multiclass expects"); 2077 2078 // Loop over all the def's in the multiclass, instantiating each one. 2079 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) { 2080 Record *DefProto = MC->DefPrototypes[i]; 2081 2082 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix, DefmPrefixLoc); 2083 2084 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmPrefixLoc, SubClassLoc, 2085 TArgs, TemplateVals, true/*Delete args*/)) 2086 return Error(SubClassLoc, "could not instantiate def"); 2087 2088 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmPrefixLoc)) 2089 return Error(SubClassLoc, "could not instantiate def"); 2090 2091 NewRecDefs.push_back(CurRec); 2092 } 2093 2094 2095 if (Lex.getCode() != tgtok::comma) break; 2096 Lex.Lex(); // eat ','. 2097 2098 SubClassLoc = Lex.getLoc(); 2099 2100 // A defm can inherit from regular classes (non-multiclass) as 2101 // long as they come in the end of the inheritance list. 2102 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0); 2103 2104 if (InheritFromClass) 2105 break; 2106 2107 Ref = ParseSubClassReference(0, true); 2108 } 2109 2110 if (InheritFromClass) { 2111 // Process all the classes to inherit as if they were part of a 2112 // regular 'def' and inherit all record values. 2113 SubClassReference SubClass = ParseSubClassReference(0, false); 2114 while (1) { 2115 // Check for error. 2116 if (SubClass.Rec == 0) return true; 2117 2118 // Get the expanded definition prototypes and teach them about 2119 // the record values the current class to inherit has 2120 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) { 2121 Record *CurRec = NewRecDefs[i]; 2122 2123 // Add it. 2124 if (AddSubClass(CurRec, SubClass)) 2125 return true; 2126 2127 // Process any variables on the let stack. 2128 for (unsigned i = 0, e = LetStack.size(); i != e; ++i) 2129 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j) 2130 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name, 2131 LetStack[i][j].Bits, LetStack[i][j].Value)) 2132 return true; 2133 } 2134 2135 if (Lex.getCode() != tgtok::comma) break; 2136 Lex.Lex(); // eat ','. 2137 SubClass = ParseSubClassReference(0, false); 2138 } 2139 } 2140 2141 if (!CurMultiClass) 2142 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) 2143 // See Record::setName(). This resolve step will see any new 2144 // name for the def that might have been created when resolving 2145 // inheritance, values and arguments above. 2146 NewRecDefs[i]->resolveReferences(); 2147 2148 if (Lex.getCode() != tgtok::semi) 2149 return TokError("expected ';' at end of defm"); 2150 Lex.Lex(); 2151 2152 return false; 2153} 2154 2155/// ParseObject 2156/// Object ::= ClassInst 2157/// Object ::= DefInst 2158/// Object ::= MultiClassInst 2159/// Object ::= DefMInst 2160/// Object ::= LETCommand '{' ObjectList '}' 2161/// Object ::= LETCommand Object 2162bool TGParser::ParseObject(MultiClass *MC) { 2163 switch (Lex.getCode()) { 2164 default: 2165 return TokError("Expected class, def, defm, multiclass or let definition"); 2166 case tgtok::Let: return ParseTopLevelLet(MC); 2167 case tgtok::Def: return ParseDef(MC); 2168 case tgtok::Defm: return ParseDefm(MC); 2169 case tgtok::Class: return ParseClass(); 2170 case tgtok::MultiClass: return ParseMultiClass(); 2171 } 2172} 2173 2174/// ParseObjectList 2175/// ObjectList :== Object* 2176bool TGParser::ParseObjectList(MultiClass *MC) { 2177 while (isObjectStart(Lex.getCode())) { 2178 if (ParseObject(MC)) 2179 return true; 2180 } 2181 return false; 2182} 2183 2184bool TGParser::ParseFile() { 2185 Lex.Lex(); // Prime the lexer. 2186 if (ParseObjectList()) return true; 2187 2188 // If we have unread input at the end of the file, report it. 2189 if (Lex.getCode() == tgtok::Eof) 2190 return false; 2191 2192 return TokError("Unexpected input at top level"); 2193} 2194 2195