Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1 | //===- TGParser.cpp - Parser for TableGen Files ---------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 3060910 | 2007-12-29 20:37:13 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Implement the Parser for TableGen. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chuck Rose III | aa91792 | 2007-11-26 23:19:59 +0000 | [diff] [blame] | 14 | #include <algorithm> |
| 15 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 16 | #include "TGParser.h" |
| 17 | #include "Record.h" |
| 18 | #include "llvm/ADT/StringExtras.h" |
| 19 | using namespace llvm; |
| 20 | |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | // Support Code for the Semantic Actions. |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | namespace llvm { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 26 | struct SubClassReference { |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 27 | TGLoc RefLoc; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 28 | Record *Rec; |
| 29 | std::vector<Init*> TemplateArgs; |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 30 | SubClassReference() : Rec(0) {} |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 31 | |
| 32 | bool isInvalid() const { return Rec == 0; } |
| 33 | }; |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 34 | |
| 35 | struct SubMultiClassReference { |
| 36 | TGLoc RefLoc; |
| 37 | MultiClass *MC; |
| 38 | std::vector<Init*> TemplateArgs; |
| 39 | SubMultiClassReference() : MC(0) {} |
| 40 | |
| 41 | bool isInvalid() const { return MC == 0; } |
| 42 | }; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 43 | |
| 44 | } // end namespace llvm |
| 45 | |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 46 | bool TGParser::AddValue(Record *CurRec, TGLoc Loc, const RecordVal &RV) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 47 | if (CurRec == 0) |
| 48 | CurRec = &CurMultiClass->Rec; |
| 49 | |
| 50 | if (RecordVal *ERV = CurRec->getValue(RV.getName())) { |
| 51 | // The value already exists in the class, treat this as a set. |
| 52 | if (ERV->setValue(RV.getValue())) |
| 53 | return Error(Loc, "New definition of '" + RV.getName() + "' of type '" + |
| 54 | RV.getType()->getAsString() + "' is incompatible with " + |
| 55 | "previous definition of type '" + |
| 56 | ERV->getType()->getAsString() + "'"); |
| 57 | } else { |
| 58 | CurRec->addValue(RV); |
| 59 | } |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | /// SetValue - |
| 64 | /// Return true on error, false on success. |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 65 | bool TGParser::SetValue(Record *CurRec, TGLoc Loc, const std::string &ValName, |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 66 | const std::vector<unsigned> &BitList, Init *V) { |
| 67 | if (!V) return false; |
| 68 | |
| 69 | if (CurRec == 0) CurRec = &CurMultiClass->Rec; |
| 70 | |
| 71 | RecordVal *RV = CurRec->getValue(ValName); |
| 72 | if (RV == 0) |
| 73 | return Error(Loc, "Value '" + ValName + "' unknown!"); |
| 74 | |
| 75 | // Do not allow assignments like 'X = X'. This will just cause infinite loops |
| 76 | // in the resolution machinery. |
| 77 | if (BitList.empty()) |
| 78 | if (VarInit *VI = dynamic_cast<VarInit*>(V)) |
| 79 | if (VI->getName() == ValName) |
| 80 | return false; |
| 81 | |
| 82 | // If we are assigning to a subset of the bits in the value... then we must be |
| 83 | // assigning to a field of BitsRecTy, which must have a BitsInit |
| 84 | // initializer. |
| 85 | // |
| 86 | if (!BitList.empty()) { |
| 87 | BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue()); |
| 88 | if (CurVal == 0) |
| 89 | return Error(Loc, "Value '" + ValName + "' is not a bits type"); |
| 90 | |
| 91 | // Convert the incoming value to a bits type of the appropriate size... |
| 92 | Init *BI = V->convertInitializerTo(new BitsRecTy(BitList.size())); |
| 93 | if (BI == 0) { |
| 94 | V->convertInitializerTo(new BitsRecTy(BitList.size())); |
| 95 | return Error(Loc, "Initializer is not compatible with bit range"); |
| 96 | } |
| 97 | |
| 98 | // We should have a BitsInit type now. |
| 99 | BitsInit *BInit = dynamic_cast<BitsInit*>(BI); |
| 100 | assert(BInit != 0); |
| 101 | |
| 102 | BitsInit *NewVal = new BitsInit(CurVal->getNumBits()); |
| 103 | |
| 104 | // Loop over bits, assigning values as appropriate. |
| 105 | for (unsigned i = 0, e = BitList.size(); i != e; ++i) { |
| 106 | unsigned Bit = BitList[i]; |
| 107 | if (NewVal->getBit(Bit)) |
| 108 | return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" + |
| 109 | ValName + "' more than once"); |
| 110 | NewVal->setBit(Bit, BInit->getBit(i)); |
| 111 | } |
| 112 | |
| 113 | for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i) |
| 114 | if (NewVal->getBit(i) == 0) |
| 115 | NewVal->setBit(i, CurVal->getBit(i)); |
| 116 | |
| 117 | V = NewVal; |
| 118 | } |
| 119 | |
| 120 | if (RV->setValue(V)) |
| 121 | return Error(Loc, "Value '" + ValName + "' of type '" + |
| 122 | RV->getType()->getAsString() + |
Chris Lattner | 5d81486 | 2007-11-22 21:06:59 +0000 | [diff] [blame] | 123 | "' is incompatible with initializer '" + V->getAsString() +"'"); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 124 | return false; |
| 125 | } |
| 126 | |
| 127 | /// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template |
| 128 | /// args as SubClass's template arguments. |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 129 | bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 130 | Record *SC = SubClass.Rec; |
| 131 | // Add all of the values in the subclass into the current class. |
| 132 | const std::vector<RecordVal> &Vals = SC->getValues(); |
| 133 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) |
| 134 | if (AddValue(CurRec, SubClass.RefLoc, Vals[i])) |
| 135 | return true; |
| 136 | |
| 137 | const std::vector<std::string> &TArgs = SC->getTemplateArgs(); |
| 138 | |
| 139 | // Ensure that an appropriate number of template arguments are specified. |
| 140 | if (TArgs.size() < SubClass.TemplateArgs.size()) |
| 141 | return Error(SubClass.RefLoc, "More template args specified than expected"); |
| 142 | |
| 143 | // Loop over all of the template arguments, setting them to the specified |
| 144 | // value or leaving them as the default if necessary. |
| 145 | for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { |
| 146 | if (i < SubClass.TemplateArgs.size()) { |
| 147 | // If a value is specified for this template arg, set it now. |
| 148 | if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(), |
| 149 | SubClass.TemplateArgs[i])) |
| 150 | return true; |
| 151 | |
| 152 | // Resolve it next. |
| 153 | CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i])); |
| 154 | |
| 155 | // Now remove it. |
| 156 | CurRec->removeValue(TArgs[i]); |
| 157 | |
| 158 | } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) { |
| 159 | return Error(SubClass.RefLoc,"Value not specified for template argument #" |
| 160 | + utostr(i) + " (" + TArgs[i] + ") of subclass '" + |
| 161 | SC->getName() + "'!"); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // Since everything went well, we can now set the "superclass" list for the |
| 166 | // current record. |
| 167 | const std::vector<Record*> &SCs = SC->getSuperClasses(); |
| 168 | for (unsigned i = 0, e = SCs.size(); i != e; ++i) { |
| 169 | if (CurRec->isSubClassOf(SCs[i])) |
| 170 | return Error(SubClass.RefLoc, |
| 171 | "Already subclass of '" + SCs[i]->getName() + "'!\n"); |
| 172 | CurRec->addSuperClass(SCs[i]); |
| 173 | } |
| 174 | |
| 175 | if (CurRec->isSubClassOf(SC)) |
| 176 | return Error(SubClass.RefLoc, |
| 177 | "Already subclass of '" + SC->getName() + "'!\n"); |
| 178 | CurRec->addSuperClass(SC); |
| 179 | return false; |
| 180 | } |
| 181 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 182 | /// AddSubMultiClass - Add SubMultiClass as a subclass to |
| 183 | /// CurMultiClass, resolving its template args as SubMultiClass's |
| 184 | /// template arguments. |
| 185 | bool TGParser::AddSubMultiClass(MultiClass *CurMultiClass, class SubMultiClassReference &SubMultiClass) { |
| 186 | MultiClass *SMC = SubMultiClass.MC; |
| 187 | Record *CurRec = &CurMultiClass->Rec; |
| 188 | |
| 189 | const std::vector<RecordVal> &MCVals = CurMultiClass->Rec.getValues(); |
| 190 | |
| 191 | // Add all of the values in the subclass into the current class. |
| 192 | const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues(); |
| 193 | for (unsigned i = 0, e = SMCVals.size(); i != e; ++i) |
| 194 | if (AddValue(CurRec, SubMultiClass.RefLoc, SMCVals[i])) |
| 195 | return true; |
| 196 | |
| 197 | // Add all of the defs in the subclass into the current multiclass. |
| 198 | for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(), |
| 199 | iend = SMC->DefPrototypes.end(); |
| 200 | i != iend; |
| 201 | ++i) { |
| 202 | // Clone the def and add it to the current multiclass |
| 203 | Record *NewDef = new Record(**i); |
| 204 | |
| 205 | // Add all of the values in the superclass into the current def. |
| 206 | for (unsigned i = 0, e = MCVals.size(); i != e; ++i) |
| 207 | if (AddValue(NewDef, SubMultiClass.RefLoc, MCVals[i])) |
| 208 | return true; |
| 209 | |
| 210 | CurMultiClass->DefPrototypes.push_back(NewDef); |
| 211 | } |
| 212 | |
| 213 | const std::vector<std::string> &SMCTArgs = SMC->Rec.getTemplateArgs(); |
| 214 | |
| 215 | // Ensure that an appropriate number of template arguments are specified. |
| 216 | if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size()) |
| 217 | return Error(SubMultiClass.RefLoc, "More template args specified than expected"); |
| 218 | |
| 219 | // Loop over all of the template arguments, setting them to the specified |
| 220 | // value or leaving them as the default if necessary. |
| 221 | for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) { |
| 222 | if (i < SubMultiClass.TemplateArgs.size()) { |
| 223 | // If a value is specified for this template arg, set it in the superclass now. |
| 224 | if (SetValue(CurRec, SubMultiClass.RefLoc, SMCTArgs[i], std::vector<unsigned>(), |
| 225 | SubMultiClass.TemplateArgs[i])) |
| 226 | return true; |
| 227 | |
| 228 | // Resolve it next. |
| 229 | CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i])); |
| 230 | |
| 231 | // Now remove it. |
| 232 | CurRec->removeValue(SMCTArgs[i]); |
| 233 | |
| 234 | // If a value is specified for this template arg, set it in the defs now. |
| 235 | for (MultiClass::RecordVector::iterator j = CurMultiClass->DefPrototypes.begin(), |
| 236 | jend = CurMultiClass->DefPrototypes.end(); |
| 237 | j != jend; |
| 238 | ++j) { |
| 239 | Record *Def = *j; |
| 240 | |
| 241 | if (SetValue(Def, SubMultiClass.RefLoc, SMCTArgs[i], std::vector<unsigned>(), |
| 242 | SubMultiClass.TemplateArgs[i])) |
| 243 | return true; |
| 244 | |
| 245 | // Resolve it next. |
| 246 | Def->resolveReferencesTo(Def->getValue(SMCTArgs[i])); |
| 247 | |
| 248 | // Now remove it |
| 249 | Def->removeValue(SMCTArgs[i]); |
| 250 | } |
| 251 | } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) { |
| 252 | return Error(SubMultiClass.RefLoc,"Value not specified for template argument #" |
| 253 | + utostr(i) + " (" + SMCTArgs[i] + ") of subclass '" + |
| 254 | SMC->Rec.getName() + "'!"); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return false; |
| 259 | } |
| 260 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 261 | //===----------------------------------------------------------------------===// |
| 262 | // Parser Code |
| 263 | //===----------------------------------------------------------------------===// |
| 264 | |
| 265 | /// isObjectStart - Return true if this is a valid first token for an Object. |
| 266 | static bool isObjectStart(tgtok::TokKind K) { |
| 267 | return K == tgtok::Class || K == tgtok::Def || |
| 268 | K == tgtok::Defm || K == tgtok::Let || K == tgtok::MultiClass; |
| 269 | } |
| 270 | |
| 271 | /// ParseObjectName - If an object name is specified, return it. Otherwise, |
| 272 | /// return an anonymous name. |
| 273 | /// ObjectName ::= ID |
| 274 | /// ObjectName ::= /*empty*/ |
| 275 | /// |
| 276 | std::string TGParser::ParseObjectName() { |
| 277 | if (Lex.getCode() == tgtok::Id) { |
| 278 | std::string Ret = Lex.getCurStrVal(); |
| 279 | Lex.Lex(); |
| 280 | return Ret; |
| 281 | } |
| 282 | |
| 283 | static unsigned AnonCounter = 0; |
| 284 | return "anonymous."+utostr(AnonCounter++); |
| 285 | } |
| 286 | |
| 287 | |
| 288 | /// ParseClassID - Parse and resolve a reference to a class name. This returns |
| 289 | /// null on error. |
| 290 | /// |
| 291 | /// ClassID ::= ID |
| 292 | /// |
| 293 | Record *TGParser::ParseClassID() { |
| 294 | if (Lex.getCode() != tgtok::Id) { |
| 295 | TokError("expected name for ClassID"); |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | Record *Result = Records.getClass(Lex.getCurStrVal()); |
| 300 | if (Result == 0) |
| 301 | TokError("Couldn't find class '" + Lex.getCurStrVal() + "'"); |
| 302 | |
| 303 | Lex.Lex(); |
| 304 | return Result; |
| 305 | } |
| 306 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 307 | /// ParseMultiClassID - Parse and resolve a reference to a multiclass name. This returns |
| 308 | /// null on error. |
| 309 | /// |
| 310 | /// MultiClassID ::= ID |
| 311 | /// |
| 312 | MultiClass *TGParser::ParseMultiClassID() { |
| 313 | if (Lex.getCode() != tgtok::Id) { |
| 314 | TokError("expected name for ClassID"); |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | MultiClass *Result = MultiClasses[Lex.getCurStrVal()]; |
| 319 | if (Result == 0) |
| 320 | TokError("Couldn't find class '" + Lex.getCurStrVal() + "'"); |
| 321 | |
| 322 | Lex.Lex(); |
| 323 | return Result; |
| 324 | } |
| 325 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 326 | Record *TGParser::ParseDefmID() { |
| 327 | if (Lex.getCode() != tgtok::Id) { |
| 328 | TokError("expected multiclass name"); |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | MultiClass *MC = MultiClasses[Lex.getCurStrVal()]; |
| 333 | if (MC == 0) { |
| 334 | TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'"); |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | Lex.Lex(); |
| 339 | return &MC->Rec; |
| 340 | } |
| 341 | |
| 342 | |
| 343 | |
| 344 | /// ParseSubClassReference - Parse a reference to a subclass or to a templated |
| 345 | /// subclass. This returns a SubClassRefTy with a null Record* on error. |
| 346 | /// |
| 347 | /// SubClassRef ::= ClassID |
| 348 | /// SubClassRef ::= ClassID '<' ValueList '>' |
| 349 | /// |
| 350 | SubClassReference TGParser:: |
| 351 | ParseSubClassReference(Record *CurRec, bool isDefm) { |
| 352 | SubClassReference Result; |
| 353 | Result.RefLoc = Lex.getLoc(); |
| 354 | |
| 355 | if (isDefm) |
| 356 | Result.Rec = ParseDefmID(); |
| 357 | else |
| 358 | Result.Rec = ParseClassID(); |
| 359 | if (Result.Rec == 0) return Result; |
| 360 | |
| 361 | // If there is no template arg list, we're done. |
| 362 | if (Lex.getCode() != tgtok::less) |
| 363 | return Result; |
| 364 | Lex.Lex(); // Eat the '<' |
| 365 | |
| 366 | if (Lex.getCode() == tgtok::greater) { |
| 367 | TokError("subclass reference requires a non-empty list of template values"); |
| 368 | Result.Rec = 0; |
| 369 | return Result; |
| 370 | } |
| 371 | |
| 372 | Result.TemplateArgs = ParseValueList(CurRec); |
| 373 | if (Result.TemplateArgs.empty()) { |
| 374 | Result.Rec = 0; // Error parsing value list. |
| 375 | return Result; |
| 376 | } |
| 377 | |
| 378 | if (Lex.getCode() != tgtok::greater) { |
| 379 | TokError("expected '>' in template value list"); |
| 380 | Result.Rec = 0; |
| 381 | return Result; |
| 382 | } |
| 383 | Lex.Lex(); |
| 384 | |
| 385 | return Result; |
| 386 | } |
| 387 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 388 | /// ParseSubMultiClassReference - Parse a reference to a subclass or to a templated |
| 389 | /// submulticlass. This returns a SubMultiClassRefTy with a null Record* on error. |
| 390 | /// |
| 391 | /// SubMultiClassRef ::= MultiClassID |
| 392 | /// SubMultiClassRef ::= MultiClassID '<' ValueList '>' |
| 393 | /// |
| 394 | SubMultiClassReference TGParser:: |
| 395 | ParseSubMultiClassReference(MultiClass *CurMC) { |
| 396 | SubMultiClassReference Result; |
| 397 | Result.RefLoc = Lex.getLoc(); |
| 398 | |
| 399 | Result.MC = ParseMultiClassID(); |
| 400 | if (Result.MC == 0) return Result; |
| 401 | |
| 402 | // If there is no template arg list, we're done. |
| 403 | if (Lex.getCode() != tgtok::less) |
| 404 | return Result; |
| 405 | Lex.Lex(); // Eat the '<' |
| 406 | |
| 407 | if (Lex.getCode() == tgtok::greater) { |
| 408 | TokError("subclass reference requires a non-empty list of template values"); |
| 409 | Result.MC = 0; |
| 410 | return Result; |
| 411 | } |
| 412 | |
| 413 | Result.TemplateArgs = ParseValueList(&CurMC->Rec); |
| 414 | if (Result.TemplateArgs.empty()) { |
| 415 | Result.MC = 0; // Error parsing value list. |
| 416 | return Result; |
| 417 | } |
| 418 | |
| 419 | if (Lex.getCode() != tgtok::greater) { |
| 420 | TokError("expected '>' in template value list"); |
| 421 | Result.MC = 0; |
| 422 | return Result; |
| 423 | } |
| 424 | Lex.Lex(); |
| 425 | |
| 426 | return Result; |
| 427 | } |
| 428 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 429 | /// ParseRangePiece - Parse a bit/value range. |
| 430 | /// RangePiece ::= INTVAL |
| 431 | /// RangePiece ::= INTVAL '-' INTVAL |
| 432 | /// RangePiece ::= INTVAL INTVAL |
| 433 | bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) { |
Chris Lattner | 811281e | 2008-01-10 07:01:53 +0000 | [diff] [blame] | 434 | if (Lex.getCode() != tgtok::IntVal) { |
| 435 | TokError("expected integer or bitrange"); |
| 436 | return true; |
| 437 | } |
Dan Gohman | 63f9720 | 2008-10-17 01:33:43 +0000 | [diff] [blame] | 438 | int64_t Start = Lex.getCurIntVal(); |
| 439 | int64_t End; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 440 | |
| 441 | if (Start < 0) |
| 442 | return TokError("invalid range, cannot be negative"); |
| 443 | |
| 444 | switch (Lex.Lex()) { // eat first character. |
| 445 | default: |
| 446 | Ranges.push_back(Start); |
| 447 | return false; |
| 448 | case tgtok::minus: |
| 449 | if (Lex.Lex() != tgtok::IntVal) { |
| 450 | TokError("expected integer value as end of range"); |
| 451 | return true; |
| 452 | } |
| 453 | End = Lex.getCurIntVal(); |
| 454 | break; |
| 455 | case tgtok::IntVal: |
| 456 | End = -Lex.getCurIntVal(); |
| 457 | break; |
| 458 | } |
| 459 | if (End < 0) |
| 460 | return TokError("invalid range, cannot be negative"); |
| 461 | Lex.Lex(); |
| 462 | |
| 463 | // Add to the range. |
| 464 | if (Start < End) { |
| 465 | for (; Start <= End; ++Start) |
| 466 | Ranges.push_back(Start); |
| 467 | } else { |
| 468 | for (; Start >= End; --Start) |
| 469 | Ranges.push_back(Start); |
| 470 | } |
| 471 | return false; |
| 472 | } |
| 473 | |
| 474 | /// ParseRangeList - Parse a list of scalars and ranges into scalar values. |
| 475 | /// |
| 476 | /// RangeList ::= RangePiece (',' RangePiece)* |
| 477 | /// |
| 478 | std::vector<unsigned> TGParser::ParseRangeList() { |
| 479 | std::vector<unsigned> Result; |
| 480 | |
| 481 | // Parse the first piece. |
| 482 | if (ParseRangePiece(Result)) |
| 483 | return std::vector<unsigned>(); |
| 484 | while (Lex.getCode() == tgtok::comma) { |
| 485 | Lex.Lex(); // Eat the comma. |
| 486 | |
| 487 | // Parse the next range piece. |
| 488 | if (ParseRangePiece(Result)) |
| 489 | return std::vector<unsigned>(); |
| 490 | } |
| 491 | return Result; |
| 492 | } |
| 493 | |
| 494 | /// ParseOptionalRangeList - Parse either a range list in <>'s or nothing. |
| 495 | /// OptionalRangeList ::= '<' RangeList '>' |
| 496 | /// OptionalRangeList ::= /*empty*/ |
| 497 | bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) { |
| 498 | if (Lex.getCode() != tgtok::less) |
| 499 | return false; |
| 500 | |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 501 | TGLoc StartLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 502 | Lex.Lex(); // eat the '<' |
| 503 | |
| 504 | // Parse the range list. |
| 505 | Ranges = ParseRangeList(); |
| 506 | if (Ranges.empty()) return true; |
| 507 | |
| 508 | if (Lex.getCode() != tgtok::greater) { |
| 509 | TokError("expected '>' at end of range list"); |
| 510 | return Error(StartLoc, "to match this '<'"); |
| 511 | } |
| 512 | Lex.Lex(); // eat the '>'. |
| 513 | return false; |
| 514 | } |
| 515 | |
| 516 | /// ParseOptionalBitList - Parse either a bit list in {}'s or nothing. |
| 517 | /// OptionalBitList ::= '{' RangeList '}' |
| 518 | /// OptionalBitList ::= /*empty*/ |
| 519 | bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) { |
| 520 | if (Lex.getCode() != tgtok::l_brace) |
| 521 | return false; |
| 522 | |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 523 | TGLoc StartLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 524 | Lex.Lex(); // eat the '{' |
| 525 | |
| 526 | // Parse the range list. |
| 527 | Ranges = ParseRangeList(); |
| 528 | if (Ranges.empty()) return true; |
| 529 | |
| 530 | if (Lex.getCode() != tgtok::r_brace) { |
| 531 | TokError("expected '}' at end of bit list"); |
| 532 | return Error(StartLoc, "to match this '{'"); |
| 533 | } |
| 534 | Lex.Lex(); // eat the '}'. |
| 535 | return false; |
| 536 | } |
| 537 | |
| 538 | |
| 539 | /// ParseType - Parse and return a tblgen type. This returns null on error. |
| 540 | /// |
| 541 | /// Type ::= STRING // string type |
| 542 | /// Type ::= BIT // bit type |
| 543 | /// Type ::= BITS '<' INTVAL '>' // bits<x> type |
| 544 | /// Type ::= INT // int type |
| 545 | /// Type ::= LIST '<' Type '>' // list<x> type |
| 546 | /// Type ::= CODE // code type |
| 547 | /// Type ::= DAG // dag type |
| 548 | /// Type ::= ClassID // Record Type |
| 549 | /// |
| 550 | RecTy *TGParser::ParseType() { |
| 551 | switch (Lex.getCode()) { |
| 552 | default: TokError("Unknown token when expecting a type"); return 0; |
| 553 | case tgtok::String: Lex.Lex(); return new StringRecTy(); |
| 554 | case tgtok::Bit: Lex.Lex(); return new BitRecTy(); |
| 555 | case tgtok::Int: Lex.Lex(); return new IntRecTy(); |
| 556 | case tgtok::Code: Lex.Lex(); return new CodeRecTy(); |
| 557 | case tgtok::Dag: Lex.Lex(); return new DagRecTy(); |
| 558 | case tgtok::Id: |
| 559 | if (Record *R = ParseClassID()) return new RecordRecTy(R); |
| 560 | return 0; |
| 561 | case tgtok::Bits: { |
| 562 | if (Lex.Lex() != tgtok::less) { // Eat 'bits' |
| 563 | TokError("expected '<' after bits type"); |
| 564 | return 0; |
| 565 | } |
| 566 | if (Lex.Lex() != tgtok::IntVal) { // Eat '<' |
| 567 | TokError("expected integer in bits<n> type"); |
| 568 | return 0; |
| 569 | } |
Dan Gohman | 63f9720 | 2008-10-17 01:33:43 +0000 | [diff] [blame] | 570 | uint64_t Val = Lex.getCurIntVal(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 571 | if (Lex.Lex() != tgtok::greater) { // Eat count. |
| 572 | TokError("expected '>' at end of bits<n> type"); |
| 573 | return 0; |
| 574 | } |
| 575 | Lex.Lex(); // Eat '>' |
| 576 | return new BitsRecTy(Val); |
| 577 | } |
| 578 | case tgtok::List: { |
| 579 | if (Lex.Lex() != tgtok::less) { // Eat 'bits' |
| 580 | TokError("expected '<' after list type"); |
| 581 | return 0; |
| 582 | } |
| 583 | Lex.Lex(); // Eat '<' |
| 584 | RecTy *SubType = ParseType(); |
| 585 | if (SubType == 0) return 0; |
| 586 | |
| 587 | if (Lex.getCode() != tgtok::greater) { |
| 588 | TokError("expected '>' at end of list<ty> type"); |
| 589 | return 0; |
| 590 | } |
| 591 | Lex.Lex(); // Eat '>' |
| 592 | return new ListRecTy(SubType); |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | /// ParseIDValue - Parse an ID as a value and decode what it means. |
| 598 | /// |
| 599 | /// IDValue ::= ID [def local value] |
| 600 | /// IDValue ::= ID [def template arg] |
| 601 | /// IDValue ::= ID [multiclass local value] |
| 602 | /// IDValue ::= ID [multiclass template argument] |
| 603 | /// IDValue ::= ID [def name] |
| 604 | /// |
| 605 | Init *TGParser::ParseIDValue(Record *CurRec) { |
| 606 | assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue"); |
| 607 | std::string Name = Lex.getCurStrVal(); |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 608 | TGLoc Loc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 609 | Lex.Lex(); |
| 610 | return ParseIDValue(CurRec, Name, Loc); |
| 611 | } |
| 612 | |
| 613 | /// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID |
| 614 | /// has already been read. |
| 615 | Init *TGParser::ParseIDValue(Record *CurRec, |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 616 | const std::string &Name, TGLoc NameLoc) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 617 | if (CurRec) { |
| 618 | if (const RecordVal *RV = CurRec->getValue(Name)) |
| 619 | return new VarInit(Name, RV->getType()); |
| 620 | |
| 621 | std::string TemplateArgName = CurRec->getName()+":"+Name; |
| 622 | if (CurRec->isTemplateArg(TemplateArgName)) { |
| 623 | const RecordVal *RV = CurRec->getValue(TemplateArgName); |
| 624 | assert(RV && "Template arg doesn't exist??"); |
| 625 | return new VarInit(TemplateArgName, RV->getType()); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | if (CurMultiClass) { |
| 630 | std::string MCName = CurMultiClass->Rec.getName()+"::"+Name; |
| 631 | if (CurMultiClass->Rec.isTemplateArg(MCName)) { |
| 632 | const RecordVal *RV = CurMultiClass->Rec.getValue(MCName); |
| 633 | assert(RV && "Template arg doesn't exist??"); |
| 634 | return new VarInit(MCName, RV->getType()); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | if (Record *D = Records.getDef(Name)) |
| 639 | return new DefInit(D); |
| 640 | |
| 641 | Error(NameLoc, "Variable not defined: '" + Name + "'"); |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | /// ParseSimpleValue - Parse a tblgen value. This returns null on error. |
| 646 | /// |
| 647 | /// SimpleValue ::= IDValue |
| 648 | /// SimpleValue ::= INTVAL |
Chris Lattner | d7a50cf | 2009-03-11 17:08:13 +0000 | [diff] [blame] | 649 | /// SimpleValue ::= STRVAL+ |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 650 | /// SimpleValue ::= CODEFRAGMENT |
| 651 | /// SimpleValue ::= '?' |
| 652 | /// SimpleValue ::= '{' ValueList '}' |
| 653 | /// SimpleValue ::= ID '<' ValueListNE '>' |
| 654 | /// SimpleValue ::= '[' ValueList ']' |
| 655 | /// SimpleValue ::= '(' IDValue DagArgList ')' |
| 656 | /// SimpleValue ::= CONCATTOK '(' Value ',' Value ')' |
| 657 | /// SimpleValue ::= SHLTOK '(' Value ',' Value ')' |
| 658 | /// SimpleValue ::= SRATOK '(' Value ',' Value ')' |
| 659 | /// SimpleValue ::= SRLTOK '(' Value ',' Value ')' |
| 660 | /// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')' |
| 661 | /// |
| 662 | Init *TGParser::ParseSimpleValue(Record *CurRec) { |
| 663 | Init *R = 0; |
| 664 | switch (Lex.getCode()) { |
| 665 | default: TokError("Unknown token when parsing a value"); break; |
| 666 | case tgtok::IntVal: R = new IntInit(Lex.getCurIntVal()); Lex.Lex(); break; |
Chris Lattner | d7a50cf | 2009-03-11 17:08:13 +0000 | [diff] [blame] | 667 | case tgtok::StrVal: { |
| 668 | std::string Val = Lex.getCurStrVal(); |
| 669 | Lex.Lex(); |
| 670 | |
Jim Grosbach | da4231f | 2009-03-26 16:17:51 +0000 | [diff] [blame] | 671 | // Handle multiple consecutive concatenated strings. |
Chris Lattner | d7a50cf | 2009-03-11 17:08:13 +0000 | [diff] [blame] | 672 | while (Lex.getCode() == tgtok::StrVal) { |
| 673 | Val += Lex.getCurStrVal(); |
| 674 | Lex.Lex(); |
| 675 | } |
| 676 | |
| 677 | R = new StringInit(Val); |
| 678 | break; |
| 679 | } |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 680 | case tgtok::CodeFragment: |
| 681 | R = new CodeInit(Lex.getCurStrVal()); Lex.Lex(); break; |
| 682 | case tgtok::question: R = new UnsetInit(); Lex.Lex(); break; |
| 683 | case tgtok::Id: { |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 684 | TGLoc NameLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 685 | std::string Name = Lex.getCurStrVal(); |
| 686 | if (Lex.Lex() != tgtok::less) // consume the Id. |
| 687 | return ParseIDValue(CurRec, Name, NameLoc); // Value ::= IDValue |
| 688 | |
| 689 | // Value ::= ID '<' ValueListNE '>' |
| 690 | if (Lex.Lex() == tgtok::greater) { |
| 691 | TokError("expected non-empty value list"); |
| 692 | return 0; |
| 693 | } |
| 694 | std::vector<Init*> ValueList = ParseValueList(CurRec); |
| 695 | if (ValueList.empty()) return 0; |
| 696 | |
| 697 | if (Lex.getCode() != tgtok::greater) { |
| 698 | TokError("expected '>' at end of value list"); |
| 699 | return 0; |
| 700 | } |
| 701 | Lex.Lex(); // eat the '>' |
| 702 | |
| 703 | // This is a CLASS<initvalslist> expression. This is supposed to synthesize |
| 704 | // a new anonymous definition, deriving from CLASS<initvalslist> with no |
| 705 | // body. |
| 706 | Record *Class = Records.getClass(Name); |
| 707 | if (!Class) { |
| 708 | Error(NameLoc, "Expected a class name, got '" + Name + "'"); |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | // Create the new record, set it as CurRec temporarily. |
| 713 | static unsigned AnonCounter = 0; |
Chris Lattner | 7b9ffe4 | 2009-03-13 16:09:24 +0000 | [diff] [blame] | 714 | Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),NameLoc); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 715 | SubClassReference SCRef; |
| 716 | SCRef.RefLoc = NameLoc; |
| 717 | SCRef.Rec = Class; |
| 718 | SCRef.TemplateArgs = ValueList; |
| 719 | // Add info about the subclass to NewRec. |
| 720 | if (AddSubClass(NewRec, SCRef)) |
| 721 | return 0; |
| 722 | NewRec->resolveReferences(); |
| 723 | Records.addDef(NewRec); |
| 724 | |
| 725 | // The result of the expression is a reference to the new record. |
| 726 | return new DefInit(NewRec); |
| 727 | } |
| 728 | case tgtok::l_brace: { // Value ::= '{' ValueList '}' |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 729 | TGLoc BraceLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 730 | Lex.Lex(); // eat the '{' |
| 731 | std::vector<Init*> Vals; |
| 732 | |
| 733 | if (Lex.getCode() != tgtok::r_brace) { |
| 734 | Vals = ParseValueList(CurRec); |
| 735 | if (Vals.empty()) return 0; |
| 736 | } |
| 737 | if (Lex.getCode() != tgtok::r_brace) { |
| 738 | TokError("expected '}' at end of bit list value"); |
| 739 | return 0; |
| 740 | } |
| 741 | Lex.Lex(); // eat the '}' |
| 742 | |
| 743 | BitsInit *Result = new BitsInit(Vals.size()); |
| 744 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) { |
| 745 | Init *Bit = Vals[i]->convertInitializerTo(new BitRecTy()); |
| 746 | if (Bit == 0) { |
Chris Lattner | 5d81486 | 2007-11-22 21:06:59 +0000 | [diff] [blame] | 747 | Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+ |
| 748 | ") is not convertable to a bit"); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 749 | return 0; |
| 750 | } |
| 751 | Result->setBit(Vals.size()-i-1, Bit); |
| 752 | } |
| 753 | return Result; |
| 754 | } |
| 755 | case tgtok::l_square: { // Value ::= '[' ValueList ']' |
| 756 | Lex.Lex(); // eat the '[' |
| 757 | std::vector<Init*> Vals; |
| 758 | |
| 759 | if (Lex.getCode() != tgtok::r_square) { |
| 760 | Vals = ParseValueList(CurRec); |
| 761 | if (Vals.empty()) return 0; |
| 762 | } |
| 763 | if (Lex.getCode() != tgtok::r_square) { |
| 764 | TokError("expected ']' at end of list value"); |
| 765 | return 0; |
| 766 | } |
| 767 | Lex.Lex(); // eat the ']' |
| 768 | return new ListInit(Vals); |
| 769 | } |
| 770 | case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')' |
| 771 | Lex.Lex(); // eat the '(' |
David Greene | c7cafcd | 2009-04-22 20:18:10 +0000 | [diff] [blame] | 772 | if (Lex.getCode() != tgtok::Id |
| 773 | && Lex.getCode() != tgtok::XNameConcat) { |
Chris Lattner | 3dc2e96 | 2008-04-10 04:48:34 +0000 | [diff] [blame] | 774 | TokError("expected identifier in dag init"); |
| 775 | return 0; |
| 776 | } |
| 777 | |
David Greene | c7cafcd | 2009-04-22 20:18:10 +0000 | [diff] [blame] | 778 | Init *Operator = 0; |
| 779 | if (Lex.getCode() == tgtok::Id) { |
| 780 | Operator = ParseIDValue(CurRec); |
| 781 | if (Operator == 0) return 0; |
| 782 | } |
| 783 | else { |
| 784 | BinOpInit::BinaryOp Code = BinOpInit::NAMECONCAT; |
| 785 | |
| 786 | Lex.Lex(); // eat the operation |
| 787 | if (Lex.getCode() != tgtok::l_paren) { |
| 788 | TokError("expected '(' after binary operator"); |
| 789 | return 0; |
| 790 | } |
| 791 | Lex.Lex(); // eat the '(' |
| 792 | |
| 793 | Init *LHS = ParseValue(CurRec); |
| 794 | if (LHS == 0) return 0; |
| 795 | |
| 796 | if (Lex.getCode() != tgtok::comma) { |
| 797 | TokError("expected ',' in binary operator"); |
| 798 | return 0; |
| 799 | } |
| 800 | Lex.Lex(); // eat the ',' |
| 801 | |
| 802 | Init *RHS = ParseValue(CurRec); |
| 803 | if (RHS == 0) return 0; |
| 804 | |
| 805 | if (Lex.getCode() != tgtok::r_paren) { |
| 806 | TokError("expected ')' in binary operator"); |
| 807 | return 0; |
| 808 | } |
| 809 | Lex.Lex(); // eat the ')' |
| 810 | Operator = (new BinOpInit(Code, LHS, RHS))->Fold(CurRec, CurMultiClass); |
| 811 | } |
| 812 | |
Nate Begeman | 7cee817 | 2009-03-19 05:21:56 +0000 | [diff] [blame] | 813 | // If the operator name is present, parse it. |
| 814 | std::string OperatorName; |
| 815 | if (Lex.getCode() == tgtok::colon) { |
| 816 | if (Lex.Lex() != tgtok::VarName) { // eat the ':' |
| 817 | TokError("expected variable name in dag operator"); |
| 818 | return 0; |
| 819 | } |
| 820 | OperatorName = Lex.getCurStrVal(); |
| 821 | Lex.Lex(); // eat the VarName. |
| 822 | } |
| 823 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 824 | std::vector<std::pair<llvm::Init*, std::string> > DagArgs; |
| 825 | if (Lex.getCode() != tgtok::r_paren) { |
| 826 | DagArgs = ParseDagArgList(CurRec); |
| 827 | if (DagArgs.empty()) return 0; |
| 828 | } |
| 829 | |
| 830 | if (Lex.getCode() != tgtok::r_paren) { |
| 831 | TokError("expected ')' in dag init"); |
| 832 | return 0; |
| 833 | } |
| 834 | Lex.Lex(); // eat the ')' |
| 835 | |
Nate Begeman | 7cee817 | 2009-03-19 05:21:56 +0000 | [diff] [blame] | 836 | return new DagInit(Operator, OperatorName, DagArgs); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 837 | } |
| 838 | case tgtok::XConcat: |
| 839 | case tgtok::XSRA: |
| 840 | case tgtok::XSRL: |
| 841 | case tgtok::XSHL: |
David Greene | c7cafcd | 2009-04-22 20:18:10 +0000 | [diff] [blame] | 842 | case tgtok::XStrConcat: |
| 843 | case tgtok::XNameConcat: { // Value ::= !binop '(' Value ',' Value ')' |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 844 | BinOpInit::BinaryOp Code; |
| 845 | switch (Lex.getCode()) { |
| 846 | default: assert(0 && "Unhandled code!"); |
David Greene | c7cafcd | 2009-04-22 20:18:10 +0000 | [diff] [blame] | 847 | case tgtok::XConcat: Code = BinOpInit::CONCAT; break; |
| 848 | case tgtok::XSRA: Code = BinOpInit::SRA; break; |
| 849 | case tgtok::XSRL: Code = BinOpInit::SRL; break; |
| 850 | case tgtok::XSHL: Code = BinOpInit::SHL; break; |
| 851 | case tgtok::XStrConcat: Code = BinOpInit::STRCONCAT; break; |
| 852 | case tgtok::XNameConcat: Code = BinOpInit::NAMECONCAT; break; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 853 | } |
| 854 | Lex.Lex(); // eat the operation |
| 855 | if (Lex.getCode() != tgtok::l_paren) { |
| 856 | TokError("expected '(' after binary operator"); |
| 857 | return 0; |
| 858 | } |
| 859 | Lex.Lex(); // eat the '(' |
David Greene | c7cafcd | 2009-04-22 20:18:10 +0000 | [diff] [blame] | 860 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 861 | Init *LHS = ParseValue(CurRec); |
| 862 | if (LHS == 0) return 0; |
| 863 | |
| 864 | if (Lex.getCode() != tgtok::comma) { |
| 865 | TokError("expected ',' in binary operator"); |
| 866 | return 0; |
| 867 | } |
| 868 | Lex.Lex(); // eat the ',' |
| 869 | |
| 870 | Init *RHS = ParseValue(CurRec); |
| 871 | if (RHS == 0) return 0; |
| 872 | |
| 873 | if (Lex.getCode() != tgtok::r_paren) { |
| 874 | TokError("expected ')' in binary operator"); |
| 875 | return 0; |
| 876 | } |
| 877 | Lex.Lex(); // eat the ')' |
David Greene | c7cafcd | 2009-04-22 20:18:10 +0000 | [diff] [blame] | 878 | return (new BinOpInit(Code, LHS, RHS))->Fold(CurRec, CurMultiClass); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
| 882 | return R; |
| 883 | } |
| 884 | |
| 885 | /// ParseValue - Parse a tblgen value. This returns null on error. |
| 886 | /// |
| 887 | /// Value ::= SimpleValue ValueSuffix* |
| 888 | /// ValueSuffix ::= '{' BitList '}' |
| 889 | /// ValueSuffix ::= '[' BitList ']' |
| 890 | /// ValueSuffix ::= '.' ID |
| 891 | /// |
| 892 | Init *TGParser::ParseValue(Record *CurRec) { |
| 893 | Init *Result = ParseSimpleValue(CurRec); |
| 894 | if (Result == 0) return 0; |
| 895 | |
| 896 | // Parse the suffixes now if present. |
| 897 | while (1) { |
| 898 | switch (Lex.getCode()) { |
| 899 | default: return Result; |
| 900 | case tgtok::l_brace: { |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 901 | TGLoc CurlyLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 902 | Lex.Lex(); // eat the '{' |
| 903 | std::vector<unsigned> Ranges = ParseRangeList(); |
| 904 | if (Ranges.empty()) return 0; |
| 905 | |
| 906 | // Reverse the bitlist. |
| 907 | std::reverse(Ranges.begin(), Ranges.end()); |
| 908 | Result = Result->convertInitializerBitRange(Ranges); |
| 909 | if (Result == 0) { |
| 910 | Error(CurlyLoc, "Invalid bit range for value"); |
| 911 | return 0; |
| 912 | } |
| 913 | |
| 914 | // Eat the '}'. |
| 915 | if (Lex.getCode() != tgtok::r_brace) { |
| 916 | TokError("expected '}' at end of bit range list"); |
| 917 | return 0; |
| 918 | } |
| 919 | Lex.Lex(); |
| 920 | break; |
| 921 | } |
| 922 | case tgtok::l_square: { |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 923 | TGLoc SquareLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 924 | Lex.Lex(); // eat the '[' |
| 925 | std::vector<unsigned> Ranges = ParseRangeList(); |
| 926 | if (Ranges.empty()) return 0; |
| 927 | |
| 928 | Result = Result->convertInitListSlice(Ranges); |
| 929 | if (Result == 0) { |
| 930 | Error(SquareLoc, "Invalid range for list slice"); |
| 931 | return 0; |
| 932 | } |
| 933 | |
| 934 | // Eat the ']'. |
| 935 | if (Lex.getCode() != tgtok::r_square) { |
| 936 | TokError("expected ']' at end of list slice"); |
| 937 | return 0; |
| 938 | } |
| 939 | Lex.Lex(); |
| 940 | break; |
| 941 | } |
| 942 | case tgtok::period: |
| 943 | if (Lex.Lex() != tgtok::Id) { // eat the . |
| 944 | TokError("expected field identifier after '.'"); |
| 945 | return 0; |
| 946 | } |
| 947 | if (!Result->getFieldType(Lex.getCurStrVal())) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 948 | TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" + |
Chris Lattner | 5d81486 | 2007-11-22 21:06:59 +0000 | [diff] [blame] | 949 | Result->getAsString() + "'"); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 950 | return 0; |
| 951 | } |
| 952 | Result = new FieldInit(Result, Lex.getCurStrVal()); |
| 953 | Lex.Lex(); // eat field name |
| 954 | break; |
| 955 | } |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | /// ParseDagArgList - Parse the argument list for a dag literal expression. |
| 960 | /// |
| 961 | /// ParseDagArgList ::= Value (':' VARNAME)? |
| 962 | /// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)? |
| 963 | std::vector<std::pair<llvm::Init*, std::string> > |
| 964 | TGParser::ParseDagArgList(Record *CurRec) { |
| 965 | std::vector<std::pair<llvm::Init*, std::string> > Result; |
| 966 | |
| 967 | while (1) { |
| 968 | Init *Val = ParseValue(CurRec); |
| 969 | if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >(); |
| 970 | |
| 971 | // If the variable name is present, add it. |
| 972 | std::string VarName; |
| 973 | if (Lex.getCode() == tgtok::colon) { |
| 974 | if (Lex.Lex() != tgtok::VarName) { // eat the ':' |
| 975 | TokError("expected variable name in dag literal"); |
| 976 | return std::vector<std::pair<llvm::Init*, std::string> >(); |
| 977 | } |
| 978 | VarName = Lex.getCurStrVal(); |
| 979 | Lex.Lex(); // eat the VarName. |
| 980 | } |
| 981 | |
| 982 | Result.push_back(std::make_pair(Val, VarName)); |
| 983 | |
| 984 | if (Lex.getCode() != tgtok::comma) break; |
| 985 | Lex.Lex(); // eat the ',' |
| 986 | } |
| 987 | |
| 988 | return Result; |
| 989 | } |
| 990 | |
| 991 | |
| 992 | /// ParseValueList - Parse a comma separated list of values, returning them as a |
| 993 | /// vector. Note that this always expects to be able to parse at least one |
| 994 | /// value. It returns an empty list if this is not possible. |
| 995 | /// |
| 996 | /// ValueList ::= Value (',' Value) |
| 997 | /// |
| 998 | std::vector<Init*> TGParser::ParseValueList(Record *CurRec) { |
| 999 | std::vector<Init*> Result; |
| 1000 | Result.push_back(ParseValue(CurRec)); |
| 1001 | if (Result.back() == 0) return std::vector<Init*>(); |
| 1002 | |
| 1003 | while (Lex.getCode() == tgtok::comma) { |
| 1004 | Lex.Lex(); // Eat the comma |
| 1005 | |
| 1006 | Result.push_back(ParseValue(CurRec)); |
| 1007 | if (Result.back() == 0) return std::vector<Init*>(); |
| 1008 | } |
| 1009 | |
| 1010 | return Result; |
| 1011 | } |
| 1012 | |
| 1013 | |
| 1014 | |
| 1015 | /// ParseDeclaration - Read a declaration, returning the name of field ID, or an |
| 1016 | /// empty string on error. This can happen in a number of different context's, |
| 1017 | /// including within a def or in the template args for a def (which which case |
| 1018 | /// CurRec will be non-null) and within the template args for a multiclass (in |
| 1019 | /// which case CurRec will be null, but CurMultiClass will be set). This can |
| 1020 | /// also happen within a def that is within a multiclass, which will set both |
| 1021 | /// CurRec and CurMultiClass. |
| 1022 | /// |
| 1023 | /// Declaration ::= FIELD? Type ID ('=' Value)? |
| 1024 | /// |
| 1025 | std::string TGParser::ParseDeclaration(Record *CurRec, |
| 1026 | bool ParsingTemplateArgs) { |
| 1027 | // Read the field prefix if present. |
| 1028 | bool HasField = Lex.getCode() == tgtok::Field; |
| 1029 | if (HasField) Lex.Lex(); |
| 1030 | |
| 1031 | RecTy *Type = ParseType(); |
| 1032 | if (Type == 0) return ""; |
| 1033 | |
| 1034 | if (Lex.getCode() != tgtok::Id) { |
| 1035 | TokError("Expected identifier in declaration"); |
| 1036 | return ""; |
| 1037 | } |
| 1038 | |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 1039 | TGLoc IdLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1040 | std::string DeclName = Lex.getCurStrVal(); |
| 1041 | Lex.Lex(); |
| 1042 | |
| 1043 | if (ParsingTemplateArgs) { |
| 1044 | if (CurRec) { |
| 1045 | DeclName = CurRec->getName() + ":" + DeclName; |
| 1046 | } else { |
| 1047 | assert(CurMultiClass); |
| 1048 | } |
| 1049 | if (CurMultiClass) |
| 1050 | DeclName = CurMultiClass->Rec.getName() + "::" + DeclName; |
| 1051 | } |
| 1052 | |
| 1053 | // Add the value. |
| 1054 | if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField))) |
| 1055 | return ""; |
| 1056 | |
| 1057 | // If a value is present, parse it. |
| 1058 | if (Lex.getCode() == tgtok::equal) { |
| 1059 | Lex.Lex(); |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 1060 | TGLoc ValLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1061 | Init *Val = ParseValue(CurRec); |
| 1062 | if (Val == 0 || |
| 1063 | SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val)) |
| 1064 | return ""; |
| 1065 | } |
| 1066 | |
| 1067 | return DeclName; |
| 1068 | } |
| 1069 | |
| 1070 | /// ParseTemplateArgList - Read a template argument list, which is a non-empty |
| 1071 | /// sequence of template-declarations in <>'s. If CurRec is non-null, these are |
| 1072 | /// template args for a def, which may or may not be in a multiclass. If null, |
| 1073 | /// these are the template args for a multiclass. |
| 1074 | /// |
| 1075 | /// TemplateArgList ::= '<' Declaration (',' Declaration)* '>' |
| 1076 | /// |
| 1077 | bool TGParser::ParseTemplateArgList(Record *CurRec) { |
| 1078 | assert(Lex.getCode() == tgtok::less && "Not a template arg list!"); |
| 1079 | Lex.Lex(); // eat the '<' |
| 1080 | |
| 1081 | Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec; |
| 1082 | |
| 1083 | // Read the first declaration. |
| 1084 | std::string TemplArg = ParseDeclaration(CurRec, true/*templateargs*/); |
| 1085 | if (TemplArg.empty()) |
| 1086 | return true; |
| 1087 | |
| 1088 | TheRecToAddTo->addTemplateArg(TemplArg); |
| 1089 | |
| 1090 | while (Lex.getCode() == tgtok::comma) { |
| 1091 | Lex.Lex(); // eat the ',' |
| 1092 | |
| 1093 | // Read the following declarations. |
| 1094 | TemplArg = ParseDeclaration(CurRec, true/*templateargs*/); |
| 1095 | if (TemplArg.empty()) |
| 1096 | return true; |
| 1097 | TheRecToAddTo->addTemplateArg(TemplArg); |
| 1098 | } |
| 1099 | |
| 1100 | if (Lex.getCode() != tgtok::greater) |
| 1101 | return TokError("expected '>' at end of template argument list"); |
| 1102 | Lex.Lex(); // eat the '>'. |
| 1103 | return false; |
| 1104 | } |
| 1105 | |
| 1106 | |
| 1107 | /// ParseBodyItem - Parse a single item at within the body of a def or class. |
| 1108 | /// |
| 1109 | /// BodyItem ::= Declaration ';' |
| 1110 | /// BodyItem ::= LET ID OptionalBitList '=' Value ';' |
| 1111 | bool TGParser::ParseBodyItem(Record *CurRec) { |
| 1112 | if (Lex.getCode() != tgtok::Let) { |
| 1113 | if (ParseDeclaration(CurRec, false).empty()) |
| 1114 | return true; |
| 1115 | |
| 1116 | if (Lex.getCode() != tgtok::semi) |
| 1117 | return TokError("expected ';' after declaration"); |
| 1118 | Lex.Lex(); |
| 1119 | return false; |
| 1120 | } |
| 1121 | |
| 1122 | // LET ID OptionalRangeList '=' Value ';' |
| 1123 | if (Lex.Lex() != tgtok::Id) |
| 1124 | return TokError("expected field identifier after let"); |
| 1125 | |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 1126 | TGLoc IdLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1127 | std::string FieldName = Lex.getCurStrVal(); |
| 1128 | Lex.Lex(); // eat the field name. |
| 1129 | |
| 1130 | std::vector<unsigned> BitList; |
| 1131 | if (ParseOptionalBitList(BitList)) |
| 1132 | return true; |
| 1133 | std::reverse(BitList.begin(), BitList.end()); |
| 1134 | |
| 1135 | if (Lex.getCode() != tgtok::equal) |
| 1136 | return TokError("expected '=' in let expression"); |
| 1137 | Lex.Lex(); // eat the '='. |
| 1138 | |
| 1139 | Init *Val = ParseValue(CurRec); |
| 1140 | if (Val == 0) return true; |
| 1141 | |
| 1142 | if (Lex.getCode() != tgtok::semi) |
| 1143 | return TokError("expected ';' after let expression"); |
| 1144 | Lex.Lex(); |
| 1145 | |
| 1146 | return SetValue(CurRec, IdLoc, FieldName, BitList, Val); |
| 1147 | } |
| 1148 | |
| 1149 | /// ParseBody - Read the body of a class or def. Return true on error, false on |
| 1150 | /// success. |
| 1151 | /// |
| 1152 | /// Body ::= ';' |
| 1153 | /// Body ::= '{' BodyList '}' |
| 1154 | /// BodyList BodyItem* |
| 1155 | /// |
| 1156 | bool TGParser::ParseBody(Record *CurRec) { |
| 1157 | // If this is a null definition, just eat the semi and return. |
| 1158 | if (Lex.getCode() == tgtok::semi) { |
| 1159 | Lex.Lex(); |
| 1160 | return false; |
| 1161 | } |
| 1162 | |
| 1163 | if (Lex.getCode() != tgtok::l_brace) |
| 1164 | return TokError("Expected ';' or '{' to start body"); |
| 1165 | // Eat the '{'. |
| 1166 | Lex.Lex(); |
| 1167 | |
| 1168 | while (Lex.getCode() != tgtok::r_brace) |
| 1169 | if (ParseBodyItem(CurRec)) |
| 1170 | return true; |
| 1171 | |
| 1172 | // Eat the '}'. |
| 1173 | Lex.Lex(); |
| 1174 | return false; |
| 1175 | } |
| 1176 | |
| 1177 | /// ParseObjectBody - Parse the body of a def or class. This consists of an |
| 1178 | /// optional ClassList followed by a Body. CurRec is the current def or class |
| 1179 | /// that is being parsed. |
| 1180 | /// |
| 1181 | /// ObjectBody ::= BaseClassList Body |
| 1182 | /// BaseClassList ::= /*empty*/ |
| 1183 | /// BaseClassList ::= ':' BaseClassListNE |
| 1184 | /// BaseClassListNE ::= SubClassRef (',' SubClassRef)* |
| 1185 | /// |
| 1186 | bool TGParser::ParseObjectBody(Record *CurRec) { |
| 1187 | // If there is a baseclass list, read it. |
| 1188 | if (Lex.getCode() == tgtok::colon) { |
| 1189 | Lex.Lex(); |
| 1190 | |
| 1191 | // Read all of the subclasses. |
| 1192 | SubClassReference SubClass = ParseSubClassReference(CurRec, false); |
| 1193 | while (1) { |
| 1194 | // Check for error. |
| 1195 | if (SubClass.Rec == 0) return true; |
| 1196 | |
| 1197 | // Add it. |
| 1198 | if (AddSubClass(CurRec, SubClass)) |
| 1199 | return true; |
| 1200 | |
| 1201 | if (Lex.getCode() != tgtok::comma) break; |
| 1202 | Lex.Lex(); // eat ','. |
| 1203 | SubClass = ParseSubClassReference(CurRec, false); |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | // Process any variables on the let stack. |
| 1208 | for (unsigned i = 0, e = LetStack.size(); i != e; ++i) |
| 1209 | for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j) |
| 1210 | if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name, |
| 1211 | LetStack[i][j].Bits, LetStack[i][j].Value)) |
| 1212 | return true; |
| 1213 | |
| 1214 | return ParseBody(CurRec); |
| 1215 | } |
| 1216 | |
| 1217 | |
| 1218 | /// ParseDef - Parse and return a top level or multiclass def, return the record |
| 1219 | /// corresponding to it. This returns null on error. |
| 1220 | /// |
| 1221 | /// DefInst ::= DEF ObjectName ObjectBody |
| 1222 | /// |
| 1223 | llvm::Record *TGParser::ParseDef(MultiClass *CurMultiClass) { |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 1224 | TGLoc DefLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1225 | assert(Lex.getCode() == tgtok::Def && "Unknown tok"); |
| 1226 | Lex.Lex(); // Eat the 'def' token. |
| 1227 | |
| 1228 | // Parse ObjectName and make a record for it. |
Chris Lattner | 7b9ffe4 | 2009-03-13 16:09:24 +0000 | [diff] [blame] | 1229 | Record *CurRec = new Record(ParseObjectName(), DefLoc); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1230 | |
| 1231 | if (!CurMultiClass) { |
| 1232 | // Top-level def definition. |
| 1233 | |
| 1234 | // Ensure redefinition doesn't happen. |
| 1235 | if (Records.getDef(CurRec->getName())) { |
| 1236 | Error(DefLoc, "def '" + CurRec->getName() + "' already defined"); |
| 1237 | return 0; |
| 1238 | } |
| 1239 | Records.addDef(CurRec); |
| 1240 | } else { |
| 1241 | // Otherwise, a def inside a multiclass, add it to the multiclass. |
| 1242 | for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i) |
| 1243 | if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) { |
| 1244 | Error(DefLoc, "def '" + CurRec->getName() + |
| 1245 | "' already defined in this multiclass!"); |
| 1246 | return 0; |
| 1247 | } |
| 1248 | CurMultiClass->DefPrototypes.push_back(CurRec); |
| 1249 | } |
| 1250 | |
| 1251 | if (ParseObjectBody(CurRec)) |
| 1252 | return 0; |
| 1253 | |
| 1254 | if (CurMultiClass == 0) // Def's in multiclasses aren't really defs. |
| 1255 | CurRec->resolveReferences(); |
| 1256 | |
| 1257 | // If ObjectBody has template arguments, it's an error. |
| 1258 | assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?"); |
| 1259 | return CurRec; |
| 1260 | } |
| 1261 | |
| 1262 | |
| 1263 | /// ParseClass - Parse a tblgen class definition. |
| 1264 | /// |
| 1265 | /// ClassInst ::= CLASS ID TemplateArgList? ObjectBody |
| 1266 | /// |
| 1267 | bool TGParser::ParseClass() { |
| 1268 | assert(Lex.getCode() == tgtok::Class && "Unexpected token!"); |
| 1269 | Lex.Lex(); |
| 1270 | |
| 1271 | if (Lex.getCode() != tgtok::Id) |
| 1272 | return TokError("expected class name after 'class' keyword"); |
| 1273 | |
| 1274 | Record *CurRec = Records.getClass(Lex.getCurStrVal()); |
| 1275 | if (CurRec) { |
| 1276 | // If the body was previously defined, this is an error. |
| 1277 | if (!CurRec->getValues().empty() || |
| 1278 | !CurRec->getSuperClasses().empty() || |
| 1279 | !CurRec->getTemplateArgs().empty()) |
| 1280 | return TokError("Class '" + CurRec->getName() + "' already defined"); |
| 1281 | } else { |
| 1282 | // If this is the first reference to this class, create and add it. |
Chris Lattner | 7b9ffe4 | 2009-03-13 16:09:24 +0000 | [diff] [blame] | 1283 | CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc()); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1284 | Records.addClass(CurRec); |
| 1285 | } |
| 1286 | Lex.Lex(); // eat the name. |
| 1287 | |
| 1288 | // If there are template args, parse them. |
| 1289 | if (Lex.getCode() == tgtok::less) |
| 1290 | if (ParseTemplateArgList(CurRec)) |
| 1291 | return true; |
| 1292 | |
| 1293 | // Finally, parse the object body. |
| 1294 | return ParseObjectBody(CurRec); |
| 1295 | } |
| 1296 | |
| 1297 | /// ParseLetList - Parse a non-empty list of assignment expressions into a list |
| 1298 | /// of LetRecords. |
| 1299 | /// |
| 1300 | /// LetList ::= LetItem (',' LetItem)* |
| 1301 | /// LetItem ::= ID OptionalRangeList '=' Value |
| 1302 | /// |
| 1303 | std::vector<LetRecord> TGParser::ParseLetList() { |
| 1304 | std::vector<LetRecord> Result; |
| 1305 | |
| 1306 | while (1) { |
| 1307 | if (Lex.getCode() != tgtok::Id) { |
| 1308 | TokError("expected identifier in let definition"); |
| 1309 | return std::vector<LetRecord>(); |
| 1310 | } |
| 1311 | std::string Name = Lex.getCurStrVal(); |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 1312 | TGLoc NameLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1313 | Lex.Lex(); // Eat the identifier. |
| 1314 | |
| 1315 | // Check for an optional RangeList. |
| 1316 | std::vector<unsigned> Bits; |
| 1317 | if (ParseOptionalRangeList(Bits)) |
| 1318 | return std::vector<LetRecord>(); |
| 1319 | std::reverse(Bits.begin(), Bits.end()); |
| 1320 | |
| 1321 | if (Lex.getCode() != tgtok::equal) { |
| 1322 | TokError("expected '=' in let expression"); |
| 1323 | return std::vector<LetRecord>(); |
| 1324 | } |
| 1325 | Lex.Lex(); // eat the '='. |
| 1326 | |
| 1327 | Init *Val = ParseValue(0); |
| 1328 | if (Val == 0) return std::vector<LetRecord>(); |
| 1329 | |
| 1330 | // Now that we have everything, add the record. |
| 1331 | Result.push_back(LetRecord(Name, Bits, Val, NameLoc)); |
| 1332 | |
| 1333 | if (Lex.getCode() != tgtok::comma) |
| 1334 | return Result; |
| 1335 | Lex.Lex(); // eat the comma. |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | /// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of |
| 1340 | /// different related productions. |
| 1341 | /// |
| 1342 | /// Object ::= LET LetList IN '{' ObjectList '}' |
| 1343 | /// Object ::= LET LetList IN Object |
| 1344 | /// |
| 1345 | bool TGParser::ParseTopLevelLet() { |
| 1346 | assert(Lex.getCode() == tgtok::Let && "Unexpected token"); |
| 1347 | Lex.Lex(); |
| 1348 | |
| 1349 | // Add this entry to the let stack. |
| 1350 | std::vector<LetRecord> LetInfo = ParseLetList(); |
| 1351 | if (LetInfo.empty()) return true; |
| 1352 | LetStack.push_back(LetInfo); |
| 1353 | |
| 1354 | if (Lex.getCode() != tgtok::In) |
| 1355 | return TokError("expected 'in' at end of top-level 'let'"); |
| 1356 | Lex.Lex(); |
| 1357 | |
| 1358 | // If this is a scalar let, just handle it now |
| 1359 | if (Lex.getCode() != tgtok::l_brace) { |
| 1360 | // LET LetList IN Object |
| 1361 | if (ParseObject()) |
| 1362 | return true; |
| 1363 | } else { // Object ::= LETCommand '{' ObjectList '}' |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 1364 | TGLoc BraceLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1365 | // Otherwise, this is a group let. |
| 1366 | Lex.Lex(); // eat the '{'. |
| 1367 | |
| 1368 | // Parse the object list. |
| 1369 | if (ParseObjectList()) |
| 1370 | return true; |
| 1371 | |
| 1372 | if (Lex.getCode() != tgtok::r_brace) { |
| 1373 | TokError("expected '}' at end of top level let command"); |
| 1374 | return Error(BraceLoc, "to match this '{'"); |
| 1375 | } |
| 1376 | Lex.Lex(); |
| 1377 | } |
| 1378 | |
| 1379 | // Outside this let scope, this let block is not active. |
| 1380 | LetStack.pop_back(); |
| 1381 | return false; |
| 1382 | } |
| 1383 | |
| 1384 | /// ParseMultiClassDef - Parse a def in a multiclass context. |
| 1385 | /// |
| 1386 | /// MultiClassDef ::= DefInst |
| 1387 | /// |
| 1388 | bool TGParser::ParseMultiClassDef(MultiClass *CurMC) { |
| 1389 | if (Lex.getCode() != tgtok::Def) |
| 1390 | return TokError("expected 'def' in multiclass body"); |
| 1391 | |
| 1392 | Record *D = ParseDef(CurMC); |
| 1393 | if (D == 0) return true; |
| 1394 | |
| 1395 | // Copy the template arguments for the multiclass into the def. |
| 1396 | const std::vector<std::string> &TArgs = CurMC->Rec.getTemplateArgs(); |
| 1397 | |
| 1398 | for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { |
| 1399 | const RecordVal *RV = CurMC->Rec.getValue(TArgs[i]); |
| 1400 | assert(RV && "Template arg doesn't exist?"); |
| 1401 | D->addValue(*RV); |
| 1402 | } |
| 1403 | |
| 1404 | return false; |
| 1405 | } |
| 1406 | |
| 1407 | /// ParseMultiClass - Parse a multiclass definition. |
| 1408 | /// |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 1409 | /// MultiClassInst ::= MULTICLASS ID TemplateArgList? ':' BaseMultiClassList '{' MultiClassDef+ '}' |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1410 | /// |
| 1411 | bool TGParser::ParseMultiClass() { |
| 1412 | assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token"); |
| 1413 | Lex.Lex(); // Eat the multiclass token. |
| 1414 | |
| 1415 | if (Lex.getCode() != tgtok::Id) |
| 1416 | return TokError("expected identifier after multiclass for name"); |
| 1417 | std::string Name = Lex.getCurStrVal(); |
| 1418 | |
| 1419 | if (MultiClasses.count(Name)) |
| 1420 | return TokError("multiclass '" + Name + "' already defined"); |
| 1421 | |
Chris Lattner | 7b9ffe4 | 2009-03-13 16:09:24 +0000 | [diff] [blame] | 1422 | CurMultiClass = MultiClasses[Name] = new MultiClass(Name, Lex.getLoc()); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1423 | Lex.Lex(); // Eat the identifier. |
| 1424 | |
| 1425 | // If there are template args, parse them. |
| 1426 | if (Lex.getCode() == tgtok::less) |
| 1427 | if (ParseTemplateArgList(0)) |
| 1428 | return true; |
| 1429 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 1430 | // If there are submulticlasses, parse them. |
| 1431 | if (Lex.getCode() == tgtok::colon) { |
| 1432 | Lex.Lex(); |
| 1433 | |
| 1434 | // Read all of the submulticlasses. |
| 1435 | SubMultiClassReference SubMultiClass = ParseSubMultiClassReference(CurMultiClass); |
| 1436 | while (1) { |
| 1437 | // Check for error. |
| 1438 | if (SubMultiClass.MC == 0) return true; |
| 1439 | |
| 1440 | // Add it. |
| 1441 | if (AddSubMultiClass(CurMultiClass, SubMultiClass)) |
| 1442 | return true; |
| 1443 | |
| 1444 | if (Lex.getCode() != tgtok::comma) break; |
| 1445 | Lex.Lex(); // eat ','. |
| 1446 | SubMultiClass = ParseSubMultiClassReference(CurMultiClass); |
| 1447 | } |
| 1448 | } |
| 1449 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1450 | if (Lex.getCode() != tgtok::l_brace) |
| 1451 | return TokError("expected '{' in multiclass definition"); |
| 1452 | |
| 1453 | if (Lex.Lex() == tgtok::r_brace) // eat the '{'. |
| 1454 | return TokError("multiclass must contain at least one def"); |
| 1455 | |
| 1456 | while (Lex.getCode() != tgtok::r_brace) |
| 1457 | if (ParseMultiClassDef(CurMultiClass)) |
| 1458 | return true; |
| 1459 | |
| 1460 | Lex.Lex(); // eat the '}'. |
| 1461 | |
| 1462 | CurMultiClass = 0; |
| 1463 | return false; |
| 1464 | } |
| 1465 | |
| 1466 | /// ParseDefm - Parse the instantiation of a multiclass. |
| 1467 | /// |
| 1468 | /// DefMInst ::= DEFM ID ':' DefmSubClassRef ';' |
| 1469 | /// |
| 1470 | bool TGParser::ParseDefm() { |
| 1471 | assert(Lex.getCode() == tgtok::Defm && "Unexpected token!"); |
| 1472 | if (Lex.Lex() != tgtok::Id) // eat the defm. |
| 1473 | return TokError("expected identifier after defm"); |
| 1474 | |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 1475 | TGLoc DefmPrefixLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1476 | std::string DefmPrefix = Lex.getCurStrVal(); |
| 1477 | if (Lex.Lex() != tgtok::colon) |
| 1478 | return TokError("expected ':' after defm identifier"); |
| 1479 | |
| 1480 | // eat the colon. |
| 1481 | Lex.Lex(); |
| 1482 | |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 1483 | TGLoc SubClassLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1484 | SubClassReference Ref = ParseSubClassReference(0, true); |
David Greene | 5654613 | 2009-04-22 22:17:51 +0000 | [diff] [blame^] | 1485 | |
| 1486 | while (1) { |
| 1487 | if (Ref.Rec == 0) return true; |
| 1488 | |
| 1489 | // To instantiate a multiclass, we need to first get the multiclass, then |
| 1490 | // instantiate each def contained in the multiclass with the SubClassRef |
| 1491 | // template parameters. |
| 1492 | MultiClass *MC = MultiClasses[Ref.Rec->getName()]; |
| 1493 | assert(MC && "Didn't lookup multiclass correctly?"); |
| 1494 | std::vector<Init*> &TemplateVals = Ref.TemplateArgs; |
| 1495 | |
| 1496 | // Verify that the correct number of template arguments were specified. |
| 1497 | const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs(); |
| 1498 | if (TArgs.size() < TemplateVals.size()) |
| 1499 | return Error(SubClassLoc, |
| 1500 | "more template args specified than multiclass expects"); |
| 1501 | |
| 1502 | // Loop over all the def's in the multiclass, instantiating each one. |
| 1503 | for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) { |
| 1504 | Record *DefProto = MC->DefPrototypes[i]; |
| 1505 | |
| 1506 | // Add the suffix to the defm name to get the new name. |
| 1507 | Record *CurRec = new Record(DefmPrefix + DefProto->getName(), DefmPrefixLoc); |
| 1508 | |
| 1509 | SubClassReference Ref; |
| 1510 | Ref.RefLoc = DefmPrefixLoc; |
| 1511 | Ref.Rec = DefProto; |
| 1512 | AddSubClass(CurRec, Ref); |
| 1513 | |
| 1514 | // Loop over all of the template arguments, setting them to the specified |
| 1515 | // value or leaving them as the default if necessary. |
| 1516 | for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { |
| 1517 | if (i < TemplateVals.size()) { // A value is specified for this temp-arg? |
| 1518 | // Set it now. |
| 1519 | if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(), |
| 1520 | TemplateVals[i])) |
| 1521 | return true; |
| 1522 | |
| 1523 | // Resolve it next. |
| 1524 | CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i])); |
| 1525 | |
| 1526 | // Now remove it. |
| 1527 | CurRec->removeValue(TArgs[i]); |
| 1528 | |
| 1529 | } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) { |
| 1530 | return Error(SubClassLoc, "value not specified for template argument #"+ |
| 1531 | utostr(i) + " (" + TArgs[i] + ") of multiclassclass '" + |
| 1532 | MC->Rec.getName() + "'"); |
| 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | // If the mdef is inside a 'let' expression, add to each def. |
| 1537 | for (unsigned i = 0, e = LetStack.size(); i != e; ++i) |
| 1538 | for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j) |
| 1539 | if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name, |
| 1540 | LetStack[i][j].Bits, LetStack[i][j].Value)) { |
| 1541 | Error(DefmPrefixLoc, "when instantiating this defm"); |
| 1542 | return true; |
| 1543 | } |
| 1544 | |
| 1545 | // Ensure redefinition doesn't happen. |
| 1546 | if (Records.getDef(CurRec->getName())) |
| 1547 | return Error(DefmPrefixLoc, "def '" + CurRec->getName() + |
| 1548 | "' already defined, instantiating defm with subdef '" + |
| 1549 | DefProto->getName() + "'"); |
| 1550 | Records.addDef(CurRec); |
| 1551 | CurRec->resolveReferences(); |
| 1552 | } |
| 1553 | |
| 1554 | if (Lex.getCode() != tgtok::comma) break; |
| 1555 | Lex.Lex(); // eat ','. |
| 1556 | |
| 1557 | SubClassLoc = Lex.getLoc(); |
| 1558 | Ref = ParseSubClassReference(0, true); |
| 1559 | } |
| 1560 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1561 | if (Lex.getCode() != tgtok::semi) |
| 1562 | return TokError("expected ';' at end of defm"); |
| 1563 | Lex.Lex(); |
| 1564 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1565 | return false; |
| 1566 | } |
| 1567 | |
| 1568 | /// ParseObject |
| 1569 | /// Object ::= ClassInst |
| 1570 | /// Object ::= DefInst |
| 1571 | /// Object ::= MultiClassInst |
| 1572 | /// Object ::= DefMInst |
| 1573 | /// Object ::= LETCommand '{' ObjectList '}' |
| 1574 | /// Object ::= LETCommand Object |
| 1575 | bool TGParser::ParseObject() { |
| 1576 | switch (Lex.getCode()) { |
| 1577 | default: assert(0 && "This is not an object"); |
| 1578 | case tgtok::Let: return ParseTopLevelLet(); |
| 1579 | case tgtok::Def: return ParseDef(0) == 0; |
| 1580 | case tgtok::Defm: return ParseDefm(); |
| 1581 | case tgtok::Class: return ParseClass(); |
| 1582 | case tgtok::MultiClass: return ParseMultiClass(); |
| 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | /// ParseObjectList |
| 1587 | /// ObjectList :== Object* |
| 1588 | bool TGParser::ParseObjectList() { |
| 1589 | while (isObjectStart(Lex.getCode())) { |
| 1590 | if (ParseObject()) |
| 1591 | return true; |
| 1592 | } |
| 1593 | return false; |
| 1594 | } |
| 1595 | |
| 1596 | |
| 1597 | bool TGParser::ParseFile() { |
| 1598 | Lex.Lex(); // Prime the lexer. |
| 1599 | if (ParseObjectList()) return true; |
| 1600 | |
| 1601 | // If we have unread input at the end of the file, report it. |
| 1602 | if (Lex.getCode() == tgtok::Eof) |
| 1603 | return false; |
| 1604 | |
| 1605 | return TokError("Unexpected input at top level"); |
| 1606 | } |
| 1607 | |