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