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