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 | |
| 14 | #include "TGParser.h" |
| 15 | #include "Record.h" |
| 16 | #include "llvm/ADT/StringExtras.h" |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 17 | #include <algorithm> |
| 18 | #include <sstream> |
Chris Lattner | 8d978a7 | 2010-10-05 23:58:18 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallVector.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 | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 28 | SMLoc 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 { |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 37 | SMLoc RefLoc; |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 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 { |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 47 | errs() << "Multiclass:\n"; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 48 | |
David Greene | d34a73b | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 49 | MC->dump(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 50 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 51 | errs() << "Template args:\n"; |
David Greene | d34a73b | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 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 | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 62 | bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 63 | if (CurRec == 0) |
| 64 | CurRec = &CurMultiClass->Rec; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 65 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 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 " + |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 71 | "previous definition of type '" + |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 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. |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 81 | bool TGParser::SetValue(Record *CurRec, SMLoc 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; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 97 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 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 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 113 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 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)) |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 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"); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 158 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 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. |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 164 | if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(), |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 165 | SubClass.TemplateArgs[i])) |
| 166 | return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 167 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 168 | // Resolve it next. |
| 169 | CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i])); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 170 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 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 #" |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 176 | + utostr(i) + " (" + TArgs[i] + ") of subclass '" + |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 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 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 190 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 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 || |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 295 | K == tgtok::Defm || K == tgtok::Let || K == tgtok::MultiClass; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Chris Lattner | df72eae | 2010-10-05 22:51:56 +0000 | [diff] [blame] | 298 | static std::string GetNewAnonymousName() { |
| 299 | static unsigned AnonCounter = 0; |
| 300 | return "anonymous."+utostr(AnonCounter++); |
| 301 | } |
| 302 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 303 | /// ParseObjectName - If an object name is specified, return it. Otherwise, |
| 304 | /// return an anonymous name. |
| 305 | /// ObjectName ::= ID |
| 306 | /// ObjectName ::= /*empty*/ |
| 307 | /// |
| 308 | std::string TGParser::ParseObjectName() { |
Chris Lattner | df72eae | 2010-10-05 22:51:56 +0000 | [diff] [blame] | 309 | if (Lex.getCode() != tgtok::Id) |
| 310 | return GetNewAnonymousName(); |
| 311 | |
| 312 | std::string Ret = Lex.getCurStrVal(); |
| 313 | Lex.Lex(); |
| 314 | return Ret; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | |
| 318 | /// ParseClassID - Parse and resolve a reference to a class name. This returns |
| 319 | /// null on error. |
| 320 | /// |
| 321 | /// ClassID ::= ID |
| 322 | /// |
| 323 | Record *TGParser::ParseClassID() { |
| 324 | if (Lex.getCode() != tgtok::Id) { |
| 325 | TokError("expected name for ClassID"); |
| 326 | return 0; |
| 327 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 328 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 329 | Record *Result = Records.getClass(Lex.getCurStrVal()); |
| 330 | if (Result == 0) |
| 331 | TokError("Couldn't find class '" + Lex.getCurStrVal() + "'"); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 332 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 333 | Lex.Lex(); |
| 334 | return Result; |
| 335 | } |
| 336 | |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 337 | /// ParseMultiClassID - Parse and resolve a reference to a multiclass name. |
| 338 | /// This returns null on error. |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 339 | /// |
| 340 | /// MultiClassID ::= ID |
| 341 | /// |
| 342 | MultiClass *TGParser::ParseMultiClassID() { |
| 343 | if (Lex.getCode() != tgtok::Id) { |
| 344 | TokError("expected name for ClassID"); |
| 345 | return 0; |
| 346 | } |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 347 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 348 | MultiClass *Result = MultiClasses[Lex.getCurStrVal()]; |
| 349 | if (Result == 0) |
| 350 | TokError("Couldn't find class '" + Lex.getCurStrVal() + "'"); |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 351 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 352 | Lex.Lex(); |
| 353 | return Result; |
| 354 | } |
| 355 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 356 | Record *TGParser::ParseDefmID() { |
| 357 | if (Lex.getCode() != tgtok::Id) { |
| 358 | TokError("expected multiclass name"); |
| 359 | return 0; |
| 360 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 361 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 362 | MultiClass *MC = MultiClasses[Lex.getCurStrVal()]; |
| 363 | if (MC == 0) { |
| 364 | TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'"); |
| 365 | return 0; |
| 366 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 367 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 368 | Lex.Lex(); |
| 369 | return &MC->Rec; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 370 | } |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 371 | |
| 372 | |
| 373 | /// ParseSubClassReference - Parse a reference to a subclass or to a templated |
| 374 | /// subclass. This returns a SubClassRefTy with a null Record* on error. |
| 375 | /// |
| 376 | /// SubClassRef ::= ClassID |
| 377 | /// SubClassRef ::= ClassID '<' ValueList '>' |
| 378 | /// |
| 379 | SubClassReference TGParser:: |
| 380 | ParseSubClassReference(Record *CurRec, bool isDefm) { |
| 381 | SubClassReference Result; |
| 382 | Result.RefLoc = Lex.getLoc(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 383 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 384 | if (isDefm) |
| 385 | Result.Rec = ParseDefmID(); |
| 386 | else |
| 387 | Result.Rec = ParseClassID(); |
| 388 | if (Result.Rec == 0) return Result; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 389 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 390 | // If there is no template arg list, we're done. |
| 391 | if (Lex.getCode() != tgtok::less) |
| 392 | return Result; |
| 393 | Lex.Lex(); // Eat the '<' |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 394 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 395 | if (Lex.getCode() == tgtok::greater) { |
| 396 | TokError("subclass reference requires a non-empty list of template values"); |
| 397 | Result.Rec = 0; |
| 398 | return Result; |
| 399 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 400 | |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 401 | Result.TemplateArgs = ParseValueList(CurRec, Result.Rec); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 402 | if (Result.TemplateArgs.empty()) { |
| 403 | Result.Rec = 0; // Error parsing value list. |
| 404 | return Result; |
| 405 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 406 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 407 | if (Lex.getCode() != tgtok::greater) { |
| 408 | TokError("expected '>' in template value list"); |
| 409 | Result.Rec = 0; |
| 410 | return Result; |
| 411 | } |
| 412 | Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 413 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 414 | return Result; |
| 415 | } |
| 416 | |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 417 | /// ParseSubMultiClassReference - Parse a reference to a subclass or to a |
| 418 | /// templated submulticlass. This returns a SubMultiClassRefTy with a null |
| 419 | /// Record* on error. |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 420 | /// |
| 421 | /// SubMultiClassRef ::= MultiClassID |
| 422 | /// SubMultiClassRef ::= MultiClassID '<' ValueList '>' |
| 423 | /// |
| 424 | SubMultiClassReference TGParser:: |
| 425 | ParseSubMultiClassReference(MultiClass *CurMC) { |
| 426 | SubMultiClassReference Result; |
| 427 | Result.RefLoc = Lex.getLoc(); |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 428 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 429 | Result.MC = ParseMultiClassID(); |
| 430 | if (Result.MC == 0) return Result; |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 431 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 432 | // If there is no template arg list, we're done. |
| 433 | if (Lex.getCode() != tgtok::less) |
| 434 | return Result; |
| 435 | Lex.Lex(); // Eat the '<' |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 436 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 437 | if (Lex.getCode() == tgtok::greater) { |
| 438 | TokError("subclass reference requires a non-empty list of template values"); |
| 439 | Result.MC = 0; |
| 440 | return Result; |
| 441 | } |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 442 | |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 443 | Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec); |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 444 | if (Result.TemplateArgs.empty()) { |
| 445 | Result.MC = 0; // Error parsing value list. |
| 446 | return Result; |
| 447 | } |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 448 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 449 | if (Lex.getCode() != tgtok::greater) { |
| 450 | TokError("expected '>' in template value list"); |
| 451 | Result.MC = 0; |
| 452 | return Result; |
| 453 | } |
| 454 | Lex.Lex(); |
| 455 | |
| 456 | return Result; |
| 457 | } |
| 458 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 459 | /// ParseRangePiece - Parse a bit/value range. |
| 460 | /// RangePiece ::= INTVAL |
| 461 | /// RangePiece ::= INTVAL '-' INTVAL |
| 462 | /// RangePiece ::= INTVAL INTVAL |
| 463 | bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) { |
Chris Lattner | 811281e | 2008-01-10 07:01:53 +0000 | [diff] [blame] | 464 | if (Lex.getCode() != tgtok::IntVal) { |
| 465 | TokError("expected integer or bitrange"); |
| 466 | return true; |
| 467 | } |
Dan Gohman | 63f9720 | 2008-10-17 01:33:43 +0000 | [diff] [blame] | 468 | int64_t Start = Lex.getCurIntVal(); |
| 469 | int64_t End; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 470 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 471 | if (Start < 0) |
| 472 | return TokError("invalid range, cannot be negative"); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 473 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 474 | switch (Lex.Lex()) { // eat first character. |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 475 | default: |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 476 | Ranges.push_back(Start); |
| 477 | return false; |
| 478 | case tgtok::minus: |
| 479 | if (Lex.Lex() != tgtok::IntVal) { |
| 480 | TokError("expected integer value as end of range"); |
| 481 | return true; |
| 482 | } |
| 483 | End = Lex.getCurIntVal(); |
| 484 | break; |
| 485 | case tgtok::IntVal: |
| 486 | End = -Lex.getCurIntVal(); |
| 487 | break; |
| 488 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 489 | if (End < 0) |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 490 | return TokError("invalid range, cannot be negative"); |
| 491 | Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 492 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 493 | // Add to the range. |
| 494 | if (Start < End) { |
| 495 | for (; Start <= End; ++Start) |
| 496 | Ranges.push_back(Start); |
| 497 | } else { |
| 498 | for (; Start >= End; --Start) |
| 499 | Ranges.push_back(Start); |
| 500 | } |
| 501 | return false; |
| 502 | } |
| 503 | |
| 504 | /// ParseRangeList - Parse a list of scalars and ranges into scalar values. |
| 505 | /// |
| 506 | /// RangeList ::= RangePiece (',' RangePiece)* |
| 507 | /// |
| 508 | std::vector<unsigned> TGParser::ParseRangeList() { |
| 509 | std::vector<unsigned> Result; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 510 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 511 | // Parse the first piece. |
| 512 | if (ParseRangePiece(Result)) |
| 513 | return std::vector<unsigned>(); |
| 514 | while (Lex.getCode() == tgtok::comma) { |
| 515 | Lex.Lex(); // Eat the comma. |
| 516 | |
| 517 | // Parse the next range piece. |
| 518 | if (ParseRangePiece(Result)) |
| 519 | return std::vector<unsigned>(); |
| 520 | } |
| 521 | return Result; |
| 522 | } |
| 523 | |
| 524 | /// ParseOptionalRangeList - Parse either a range list in <>'s or nothing. |
| 525 | /// OptionalRangeList ::= '<' RangeList '>' |
| 526 | /// OptionalRangeList ::= /*empty*/ |
| 527 | bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) { |
| 528 | if (Lex.getCode() != tgtok::less) |
| 529 | return false; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 530 | |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 531 | SMLoc StartLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 532 | Lex.Lex(); // eat the '<' |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 533 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 534 | // Parse the range list. |
| 535 | Ranges = ParseRangeList(); |
| 536 | if (Ranges.empty()) return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 537 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 538 | if (Lex.getCode() != tgtok::greater) { |
| 539 | TokError("expected '>' at end of range list"); |
| 540 | return Error(StartLoc, "to match this '<'"); |
| 541 | } |
| 542 | Lex.Lex(); // eat the '>'. |
| 543 | return false; |
| 544 | } |
| 545 | |
| 546 | /// ParseOptionalBitList - Parse either a bit list in {}'s or nothing. |
| 547 | /// OptionalBitList ::= '{' RangeList '}' |
| 548 | /// OptionalBitList ::= /*empty*/ |
| 549 | bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) { |
| 550 | if (Lex.getCode() != tgtok::l_brace) |
| 551 | return false; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 552 | |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 553 | SMLoc StartLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 554 | Lex.Lex(); // eat the '{' |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 555 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 556 | // Parse the range list. |
| 557 | Ranges = ParseRangeList(); |
| 558 | if (Ranges.empty()) return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 559 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 560 | if (Lex.getCode() != tgtok::r_brace) { |
| 561 | TokError("expected '}' at end of bit list"); |
| 562 | return Error(StartLoc, "to match this '{'"); |
| 563 | } |
| 564 | Lex.Lex(); // eat the '}'. |
| 565 | return false; |
| 566 | } |
| 567 | |
| 568 | |
| 569 | /// ParseType - Parse and return a tblgen type. This returns null on error. |
| 570 | /// |
| 571 | /// Type ::= STRING // string type |
| 572 | /// Type ::= BIT // bit type |
| 573 | /// Type ::= BITS '<' INTVAL '>' // bits<x> type |
| 574 | /// Type ::= INT // int type |
| 575 | /// Type ::= LIST '<' Type '>' // list<x> type |
| 576 | /// Type ::= CODE // code type |
| 577 | /// Type ::= DAG // dag type |
| 578 | /// Type ::= ClassID // Record Type |
| 579 | /// |
| 580 | RecTy *TGParser::ParseType() { |
| 581 | switch (Lex.getCode()) { |
| 582 | default: TokError("Unknown token when expecting a type"); return 0; |
| 583 | case tgtok::String: Lex.Lex(); return new StringRecTy(); |
| 584 | case tgtok::Bit: Lex.Lex(); return new BitRecTy(); |
| 585 | case tgtok::Int: Lex.Lex(); return new IntRecTy(); |
| 586 | case tgtok::Code: Lex.Lex(); return new CodeRecTy(); |
| 587 | case tgtok::Dag: Lex.Lex(); return new DagRecTy(); |
| 588 | case tgtok::Id: |
| 589 | if (Record *R = ParseClassID()) return new RecordRecTy(R); |
| 590 | return 0; |
| 591 | case tgtok::Bits: { |
| 592 | if (Lex.Lex() != tgtok::less) { // Eat 'bits' |
| 593 | TokError("expected '<' after bits type"); |
| 594 | return 0; |
| 595 | } |
| 596 | if (Lex.Lex() != tgtok::IntVal) { // Eat '<' |
| 597 | TokError("expected integer in bits<n> type"); |
| 598 | return 0; |
| 599 | } |
Dan Gohman | 63f9720 | 2008-10-17 01:33:43 +0000 | [diff] [blame] | 600 | uint64_t Val = Lex.getCurIntVal(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 601 | if (Lex.Lex() != tgtok::greater) { // Eat count. |
| 602 | TokError("expected '>' at end of bits<n> type"); |
| 603 | return 0; |
| 604 | } |
| 605 | Lex.Lex(); // Eat '>' |
| 606 | return new BitsRecTy(Val); |
| 607 | } |
| 608 | case tgtok::List: { |
| 609 | if (Lex.Lex() != tgtok::less) { // Eat 'bits' |
| 610 | TokError("expected '<' after list type"); |
| 611 | return 0; |
| 612 | } |
| 613 | Lex.Lex(); // Eat '<' |
| 614 | RecTy *SubType = ParseType(); |
| 615 | if (SubType == 0) return 0; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 616 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 617 | if (Lex.getCode() != tgtok::greater) { |
| 618 | TokError("expected '>' at end of list<ty> type"); |
| 619 | return 0; |
| 620 | } |
| 621 | Lex.Lex(); // Eat '>' |
| 622 | return new ListRecTy(SubType); |
| 623 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 624 | } |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | /// ParseIDValue - Parse an ID as a value and decode what it means. |
| 628 | /// |
| 629 | /// IDValue ::= ID [def local value] |
| 630 | /// IDValue ::= ID [def template arg] |
| 631 | /// IDValue ::= ID [multiclass local value] |
| 632 | /// IDValue ::= ID [multiclass template argument] |
| 633 | /// IDValue ::= ID [def name] |
| 634 | /// |
| 635 | Init *TGParser::ParseIDValue(Record *CurRec) { |
| 636 | assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue"); |
| 637 | std::string Name = Lex.getCurStrVal(); |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 638 | SMLoc Loc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 639 | Lex.Lex(); |
| 640 | return ParseIDValue(CurRec, Name, Loc); |
| 641 | } |
| 642 | |
| 643 | /// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID |
| 644 | /// has already been read. |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 645 | Init *TGParser::ParseIDValue(Record *CurRec, |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 646 | const std::string &Name, SMLoc NameLoc) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 647 | if (CurRec) { |
| 648 | if (const RecordVal *RV = CurRec->getValue(Name)) |
| 649 | return new VarInit(Name, RV->getType()); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 650 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 651 | std::string TemplateArgName = CurRec->getName()+":"+Name; |
| 652 | if (CurRec->isTemplateArg(TemplateArgName)) { |
| 653 | const RecordVal *RV = CurRec->getValue(TemplateArgName); |
| 654 | assert(RV && "Template arg doesn't exist??"); |
| 655 | return new VarInit(TemplateArgName, RV->getType()); |
| 656 | } |
| 657 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 658 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 659 | if (CurMultiClass) { |
| 660 | std::string MCName = CurMultiClass->Rec.getName()+"::"+Name; |
| 661 | if (CurMultiClass->Rec.isTemplateArg(MCName)) { |
| 662 | const RecordVal *RV = CurMultiClass->Rec.getValue(MCName); |
| 663 | assert(RV && "Template arg doesn't exist??"); |
| 664 | return new VarInit(MCName, RV->getType()); |
| 665 | } |
| 666 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 667 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 668 | if (Record *D = Records.getDef(Name)) |
| 669 | return new DefInit(D); |
| 670 | |
| 671 | Error(NameLoc, "Variable not defined: '" + Name + "'"); |
| 672 | return 0; |
| 673 | } |
| 674 | |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 675 | /// ParseOperation - Parse an operator. This returns null on error. |
| 676 | /// |
| 677 | /// Operation ::= XOperator ['<' Type '>'] '(' Args ')' |
| 678 | /// |
| 679 | Init *TGParser::ParseOperation(Record *CurRec) { |
| 680 | switch (Lex.getCode()) { |
| 681 | default: |
| 682 | TokError("unknown operation"); |
| 683 | return 0; |
| 684 | break; |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 685 | case tgtok::XCar: |
| 686 | case tgtok::XCdr: |
| 687 | case tgtok::XNull: |
David Greene | e6c27de | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 688 | case tgtok::XCast: { // Value ::= !unop '(' Value ')' |
| 689 | UnOpInit::UnaryOp Code; |
| 690 | RecTy *Type = 0; |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 691 | |
David Greene | e6c27de | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 692 | switch (Lex.getCode()) { |
| 693 | default: assert(0 && "Unhandled code!"); |
| 694 | case tgtok::XCast: |
| 695 | Lex.Lex(); // eat the operation |
| 696 | Code = UnOpInit::CAST; |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 697 | |
David Greene | e6c27de | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 698 | Type = ParseOperatorType(); |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 699 | |
David Greene | e6c27de | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 700 | if (Type == 0) { |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 701 | TokError("did not get type for unary operator"); |
David Greene | e6c27de | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 702 | return 0; |
| 703 | } |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 704 | |
David Greene | e6c27de | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 705 | break; |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 706 | case tgtok::XCar: |
| 707 | Lex.Lex(); // eat the operation |
| 708 | Code = UnOpInit::CAR; |
| 709 | break; |
| 710 | case tgtok::XCdr: |
| 711 | Lex.Lex(); // eat the operation |
| 712 | Code = UnOpInit::CDR; |
| 713 | break; |
| 714 | case tgtok::XNull: |
| 715 | Lex.Lex(); // eat the operation |
| 716 | Code = UnOpInit::LNULL; |
| 717 | Type = new IntRecTy; |
| 718 | break; |
David Greene | e6c27de | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 719 | } |
| 720 | if (Lex.getCode() != tgtok::l_paren) { |
| 721 | TokError("expected '(' after unary operator"); |
| 722 | return 0; |
| 723 | } |
| 724 | Lex.Lex(); // eat the '(' |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 725 | |
David Greene | e6c27de | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 726 | Init *LHS = ParseValue(CurRec); |
| 727 | if (LHS == 0) return 0; |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 728 | |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 729 | if (Code == UnOpInit::CAR |
| 730 | || Code == UnOpInit::CDR |
| 731 | || Code == UnOpInit::LNULL) { |
| 732 | ListInit *LHSl = dynamic_cast<ListInit*>(LHS); |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 733 | StringInit *LHSs = dynamic_cast<StringInit*>(LHS); |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 734 | TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS); |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 735 | if (LHSl == 0 && LHSs == 0 && LHSt == 0) { |
| 736 | TokError("expected list or string type argument in unary operator"); |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 737 | return 0; |
| 738 | } |
| 739 | if (LHSt) { |
| 740 | ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType()); |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 741 | StringRecTy *SType = dynamic_cast<StringRecTy*>(LHSt->getType()); |
| 742 | if (LType == 0 && SType == 0) { |
| 743 | TokError("expected list or string type argumnet in unary operator"); |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 744 | return 0; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | if (Code == UnOpInit::CAR |
| 749 | || Code == UnOpInit::CDR) { |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 750 | if (LHSl == 0 && LHSt == 0) { |
| 751 | TokError("expected list type argumnet in unary operator"); |
| 752 | return 0; |
| 753 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 754 | |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 755 | if (LHSl && LHSl->getSize() == 0) { |
| 756 | TokError("empty list argument in unary operator"); |
| 757 | return 0; |
| 758 | } |
| 759 | if (LHSl) { |
| 760 | Init *Item = LHSl->getElement(0); |
| 761 | TypedInit *Itemt = dynamic_cast<TypedInit*>(Item); |
| 762 | if (Itemt == 0) { |
| 763 | TokError("untyped list element in unary operator"); |
| 764 | return 0; |
| 765 | } |
| 766 | if (Code == UnOpInit::CAR) { |
| 767 | Type = Itemt->getType(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 768 | } else { |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 769 | Type = new ListRecTy(Itemt->getType()); |
| 770 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 771 | } else { |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 772 | assert(LHSt && "expected list type argument in unary operator"); |
| 773 | ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType()); |
| 774 | if (LType == 0) { |
| 775 | TokError("expected list type argumnet in unary operator"); |
| 776 | return 0; |
| 777 | } |
| 778 | if (Code == UnOpInit::CAR) { |
| 779 | Type = LType->getElementType(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 780 | } else { |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 781 | Type = LType; |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | |
David Greene | e6c27de | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 787 | if (Lex.getCode() != tgtok::r_paren) { |
| 788 | TokError("expected ')' in unary operator"); |
| 789 | return 0; |
| 790 | } |
| 791 | Lex.Lex(); // eat the ')' |
| 792 | return (new UnOpInit(Code, LHS, Type))->Fold(CurRec, CurMultiClass); |
| 793 | } |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 794 | |
| 795 | case tgtok::XConcat: |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 796 | case tgtok::XSRA: |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 797 | case tgtok::XSRL: |
| 798 | case tgtok::XSHL: |
David Greene | 6786d5e | 2010-01-05 19:11:42 +0000 | [diff] [blame] | 799 | case tgtok::XEq: |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 800 | case tgtok::XStrConcat: |
| 801 | case tgtok::XNameConcat: { // Value ::= !binop '(' Value ',' Value ')' |
Chris Lattner | 8d978a7 | 2010-10-05 23:58:18 +0000 | [diff] [blame] | 802 | tgtok::TokKind OpTok = Lex.getCode(); |
| 803 | SMLoc OpLoc = Lex.getLoc(); |
| 804 | Lex.Lex(); // eat the operation |
| 805 | |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 806 | BinOpInit::BinaryOp Code; |
| 807 | RecTy *Type = 0; |
| 808 | |
Chris Lattner | 8d978a7 | 2010-10-05 23:58:18 +0000 | [diff] [blame] | 809 | switch (OpTok) { |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 810 | default: assert(0 && "Unhandled code!"); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 811 | case tgtok::XConcat: |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 812 | Code = BinOpInit::CONCAT; |
| 813 | Type = new DagRecTy(); |
| 814 | break; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 815 | case tgtok::XSRA: |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 816 | Code = BinOpInit::SRA; |
| 817 | Type = new IntRecTy(); |
| 818 | break; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 819 | case tgtok::XSRL: |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 820 | Code = BinOpInit::SRL; |
| 821 | Type = new IntRecTy(); |
| 822 | break; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 823 | case tgtok::XSHL: |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 824 | Code = BinOpInit::SHL; |
| 825 | Type = new IntRecTy(); |
| 826 | break; |
David Greene | 6786d5e | 2010-01-05 19:11:42 +0000 | [diff] [blame] | 827 | case tgtok::XEq: |
David Greene | 6786d5e | 2010-01-05 19:11:42 +0000 | [diff] [blame] | 828 | Code = BinOpInit::EQ; |
| 829 | Type = new IntRecTy(); |
| 830 | break; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 831 | case tgtok::XStrConcat: |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 832 | Code = BinOpInit::STRCONCAT; |
| 833 | Type = new StringRecTy(); |
| 834 | break; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 835 | case tgtok::XNameConcat: |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 836 | Code = BinOpInit::NAMECONCAT; |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 837 | Type = ParseOperatorType(); |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 838 | if (Type == 0) { |
| 839 | TokError("did not get type for binary operator"); |
| 840 | return 0; |
| 841 | } |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 842 | break; |
| 843 | } |
| 844 | if (Lex.getCode() != tgtok::l_paren) { |
| 845 | TokError("expected '(' after binary operator"); |
| 846 | return 0; |
| 847 | } |
| 848 | Lex.Lex(); // eat the '(' |
| 849 | |
Chris Lattner | 8d978a7 | 2010-10-05 23:58:18 +0000 | [diff] [blame] | 850 | SmallVector<Init*, 2> InitList; |
| 851 | |
| 852 | InitList.push_back(ParseValue(CurRec)); |
| 853 | if (InitList.back() == 0) return 0; |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 854 | |
Chris Lattner | 8d978a7 | 2010-10-05 23:58:18 +0000 | [diff] [blame] | 855 | while (Lex.getCode() == tgtok::comma) { |
| 856 | Lex.Lex(); // eat the ',' |
| 857 | |
| 858 | InitList.push_back(ParseValue(CurRec)); |
| 859 | if (InitList.back() == 0) return 0; |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 860 | } |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 861 | |
| 862 | if (Lex.getCode() != tgtok::r_paren) { |
Chris Lattner | 8d978a7 | 2010-10-05 23:58:18 +0000 | [diff] [blame] | 863 | TokError("expected ')' in operator"); |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 864 | return 0; |
| 865 | } |
| 866 | Lex.Lex(); // eat the ')' |
Chris Lattner | 8d978a7 | 2010-10-05 23:58:18 +0000 | [diff] [blame] | 867 | |
| 868 | // We allow multiple operands to associative operators like !strconcat as |
| 869 | // shorthand for nesting them. |
| 870 | if (Code == BinOpInit::STRCONCAT) { |
| 871 | while (InitList.size() > 2) { |
| 872 | Init *RHS = InitList.pop_back_val(); |
| 873 | RHS = (new BinOpInit(Code, InitList.back(), RHS, Type)) |
| 874 | ->Fold(CurRec, CurMultiClass); |
| 875 | InitList.back() = RHS; |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | if (InitList.size() == 2) |
| 880 | return (new BinOpInit(Code, InitList[0], InitList[1], Type)) |
| 881 | ->Fold(CurRec, CurMultiClass); |
| 882 | |
| 883 | Error(OpLoc, "expected two operands to operator"); |
| 884 | return 0; |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 885 | } |
| 886 | |
David Greene | 9bea7c8 | 2009-05-14 23:26:46 +0000 | [diff] [blame] | 887 | case tgtok::XIf: |
David Greene | beb31a5 | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 888 | case tgtok::XForEach: |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 889 | case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')' |
| 890 | TernOpInit::TernaryOp Code; |
| 891 | RecTy *Type = 0; |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 892 | |
| 893 | |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 894 | tgtok::TokKind LexCode = Lex.getCode(); |
| 895 | Lex.Lex(); // eat the operation |
| 896 | switch (LexCode) { |
| 897 | default: assert(0 && "Unhandled code!"); |
David Greene | 9bea7c8 | 2009-05-14 23:26:46 +0000 | [diff] [blame] | 898 | case tgtok::XIf: |
| 899 | Code = TernOpInit::IF; |
| 900 | break; |
David Greene | beb31a5 | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 901 | case tgtok::XForEach: |
| 902 | Code = TernOpInit::FOREACH; |
| 903 | break; |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 904 | case tgtok::XSubst: |
| 905 | Code = TernOpInit::SUBST; |
| 906 | break; |
| 907 | } |
| 908 | if (Lex.getCode() != tgtok::l_paren) { |
| 909 | TokError("expected '(' after ternary operator"); |
| 910 | return 0; |
| 911 | } |
| 912 | Lex.Lex(); // eat the '(' |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 913 | |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 914 | Init *LHS = ParseValue(CurRec); |
| 915 | if (LHS == 0) return 0; |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 916 | |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 917 | if (Lex.getCode() != tgtok::comma) { |
| 918 | TokError("expected ',' in ternary operator"); |
| 919 | return 0; |
| 920 | } |
| 921 | Lex.Lex(); // eat the ',' |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 922 | |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 923 | Init *MHS = ParseValue(CurRec); |
| 924 | if (MHS == 0) return 0; |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 925 | |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 926 | if (Lex.getCode() != tgtok::comma) { |
| 927 | TokError("expected ',' in ternary operator"); |
| 928 | return 0; |
| 929 | } |
| 930 | Lex.Lex(); // eat the ',' |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 931 | |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 932 | Init *RHS = ParseValue(CurRec); |
| 933 | if (RHS == 0) return 0; |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 934 | |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 935 | if (Lex.getCode() != tgtok::r_paren) { |
| 936 | TokError("expected ')' in binary operator"); |
| 937 | return 0; |
| 938 | } |
| 939 | Lex.Lex(); // eat the ')' |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 940 | |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 941 | switch (LexCode) { |
| 942 | default: assert(0 && "Unhandled code!"); |
David Greene | 9bea7c8 | 2009-05-14 23:26:46 +0000 | [diff] [blame] | 943 | case tgtok::XIf: { |
| 944 | TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS); |
| 945 | TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS); |
| 946 | if (MHSt == 0 || RHSt == 0) { |
| 947 | TokError("could not get type for !if"); |
| 948 | return 0; |
| 949 | } |
| 950 | if (MHSt->getType()->typeIsConvertibleTo(RHSt->getType())) { |
| 951 | Type = RHSt->getType(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 952 | } else if (RHSt->getType()->typeIsConvertibleTo(MHSt->getType())) { |
David Greene | 9bea7c8 | 2009-05-14 23:26:46 +0000 | [diff] [blame] | 953 | Type = MHSt->getType(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 954 | } else { |
David Greene | 9bea7c8 | 2009-05-14 23:26:46 +0000 | [diff] [blame] | 955 | TokError("inconsistent types for !if"); |
| 956 | return 0; |
| 957 | } |
| 958 | break; |
| 959 | } |
David Greene | beb31a5 | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 960 | case tgtok::XForEach: { |
| 961 | TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS); |
| 962 | if (MHSt == 0) { |
| 963 | TokError("could not get type for !foreach"); |
| 964 | return 0; |
| 965 | } |
| 966 | Type = MHSt->getType(); |
| 967 | break; |
| 968 | } |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 969 | case tgtok::XSubst: { |
| 970 | TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS); |
| 971 | if (RHSt == 0) { |
| 972 | TokError("could not get type for !subst"); |
| 973 | return 0; |
| 974 | } |
| 975 | Type = RHSt->getType(); |
| 976 | break; |
| 977 | } |
| 978 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 979 | return (new TernOpInit(Code, LHS, MHS, RHS, Type))->Fold(CurRec, |
| 980 | CurMultiClass); |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 981 | } |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 982 | } |
| 983 | TokError("could not parse operation"); |
| 984 | return 0; |
| 985 | } |
| 986 | |
| 987 | /// ParseOperatorType - Parse a type for an operator. This returns |
| 988 | /// null on error. |
| 989 | /// |
| 990 | /// OperatorType ::= '<' Type '>' |
| 991 | /// |
Dan Gohman | a9ad041 | 2009-08-12 22:10:57 +0000 | [diff] [blame] | 992 | RecTy *TGParser::ParseOperatorType() { |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 993 | RecTy *Type = 0; |
| 994 | |
| 995 | if (Lex.getCode() != tgtok::less) { |
| 996 | TokError("expected type name for operator"); |
| 997 | return 0; |
| 998 | } |
| 999 | Lex.Lex(); // eat the < |
| 1000 | |
| 1001 | Type = ParseType(); |
| 1002 | |
| 1003 | if (Type == 0) { |
| 1004 | TokError("expected type name for operator"); |
| 1005 | return 0; |
| 1006 | } |
| 1007 | |
| 1008 | if (Lex.getCode() != tgtok::greater) { |
| 1009 | TokError("expected type name for operator"); |
| 1010 | return 0; |
| 1011 | } |
| 1012 | Lex.Lex(); // eat the > |
| 1013 | |
| 1014 | return Type; |
| 1015 | } |
| 1016 | |
| 1017 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1018 | /// ParseSimpleValue - Parse a tblgen value. This returns null on error. |
| 1019 | /// |
| 1020 | /// SimpleValue ::= IDValue |
| 1021 | /// SimpleValue ::= INTVAL |
Chris Lattner | d7a50cf | 2009-03-11 17:08:13 +0000 | [diff] [blame] | 1022 | /// SimpleValue ::= STRVAL+ |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1023 | /// SimpleValue ::= CODEFRAGMENT |
| 1024 | /// SimpleValue ::= '?' |
| 1025 | /// SimpleValue ::= '{' ValueList '}' |
| 1026 | /// SimpleValue ::= ID '<' ValueListNE '>' |
| 1027 | /// SimpleValue ::= '[' ValueList ']' |
| 1028 | /// SimpleValue ::= '(' IDValue DagArgList ')' |
| 1029 | /// SimpleValue ::= CONCATTOK '(' Value ',' Value ')' |
| 1030 | /// SimpleValue ::= SHLTOK '(' Value ',' Value ')' |
| 1031 | /// SimpleValue ::= SRATOK '(' Value ',' Value ')' |
| 1032 | /// SimpleValue ::= SRLTOK '(' Value ',' Value ')' |
| 1033 | /// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')' |
| 1034 | /// |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1035 | Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1036 | Init *R = 0; |
| 1037 | switch (Lex.getCode()) { |
| 1038 | default: TokError("Unknown token when parsing a value"); break; |
| 1039 | case tgtok::IntVal: R = new IntInit(Lex.getCurIntVal()); Lex.Lex(); break; |
Chris Lattner | d7a50cf | 2009-03-11 17:08:13 +0000 | [diff] [blame] | 1040 | case tgtok::StrVal: { |
| 1041 | std::string Val = Lex.getCurStrVal(); |
| 1042 | Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1043 | |
Jim Grosbach | da4231f | 2009-03-26 16:17:51 +0000 | [diff] [blame] | 1044 | // Handle multiple consecutive concatenated strings. |
Chris Lattner | d7a50cf | 2009-03-11 17:08:13 +0000 | [diff] [blame] | 1045 | while (Lex.getCode() == tgtok::StrVal) { |
| 1046 | Val += Lex.getCurStrVal(); |
| 1047 | Lex.Lex(); |
| 1048 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1049 | |
Chris Lattner | d7a50cf | 2009-03-11 17:08:13 +0000 | [diff] [blame] | 1050 | R = new StringInit(Val); |
| 1051 | break; |
| 1052 | } |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1053 | case tgtok::CodeFragment: |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1054 | R = new CodeInit(Lex.getCurStrVal()); Lex.Lex(); break; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1055 | case tgtok::question: R = new UnsetInit(); Lex.Lex(); break; |
| 1056 | case tgtok::Id: { |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 1057 | SMLoc NameLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1058 | std::string Name = Lex.getCurStrVal(); |
| 1059 | if (Lex.Lex() != tgtok::less) // consume the Id. |
| 1060 | return ParseIDValue(CurRec, Name, NameLoc); // Value ::= IDValue |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1061 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1062 | // Value ::= ID '<' ValueListNE '>' |
| 1063 | if (Lex.Lex() == tgtok::greater) { |
| 1064 | TokError("expected non-empty value list"); |
| 1065 | return 0; |
| 1066 | } |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1067 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1068 | // This is a CLASS<initvalslist> expression. This is supposed to synthesize |
| 1069 | // a new anonymous definition, deriving from CLASS<initvalslist> with no |
| 1070 | // body. |
| 1071 | Record *Class = Records.getClass(Name); |
| 1072 | if (!Class) { |
| 1073 | Error(NameLoc, "Expected a class name, got '" + Name + "'"); |
| 1074 | return 0; |
| 1075 | } |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1076 | |
| 1077 | std::vector<Init*> ValueList = ParseValueList(CurRec, Class); |
| 1078 | if (ValueList.empty()) return 0; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1079 | |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1080 | if (Lex.getCode() != tgtok::greater) { |
| 1081 | TokError("expected '>' at end of value list"); |
| 1082 | return 0; |
| 1083 | } |
| 1084 | Lex.Lex(); // eat the '>' |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1085 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1086 | // Create the new record, set it as CurRec temporarily. |
| 1087 | static unsigned AnonCounter = 0; |
Chris Lattner | 7b9ffe4 | 2009-03-13 16:09:24 +0000 | [diff] [blame] | 1088 | Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),NameLoc); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1089 | SubClassReference SCRef; |
| 1090 | SCRef.RefLoc = NameLoc; |
| 1091 | SCRef.Rec = Class; |
| 1092 | SCRef.TemplateArgs = ValueList; |
| 1093 | // Add info about the subclass to NewRec. |
| 1094 | if (AddSubClass(NewRec, SCRef)) |
| 1095 | return 0; |
| 1096 | NewRec->resolveReferences(); |
| 1097 | Records.addDef(NewRec); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1098 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1099 | // The result of the expression is a reference to the new record. |
| 1100 | return new DefInit(NewRec); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1101 | } |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1102 | case tgtok::l_brace: { // Value ::= '{' ValueList '}' |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 1103 | SMLoc BraceLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1104 | Lex.Lex(); // eat the '{' |
| 1105 | std::vector<Init*> Vals; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1106 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1107 | if (Lex.getCode() != tgtok::r_brace) { |
| 1108 | Vals = ParseValueList(CurRec); |
| 1109 | if (Vals.empty()) return 0; |
| 1110 | } |
| 1111 | if (Lex.getCode() != tgtok::r_brace) { |
| 1112 | TokError("expected '}' at end of bit list value"); |
| 1113 | return 0; |
| 1114 | } |
| 1115 | Lex.Lex(); // eat the '}' |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1116 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1117 | BitsInit *Result = new BitsInit(Vals.size()); |
| 1118 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) { |
| 1119 | Init *Bit = Vals[i]->convertInitializerTo(new BitRecTy()); |
| 1120 | if (Bit == 0) { |
Chris Lattner | 5d81486 | 2007-11-22 21:06:59 +0000 | [diff] [blame] | 1121 | Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+ |
| 1122 | ") is not convertable to a bit"); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1123 | return 0; |
| 1124 | } |
| 1125 | Result->setBit(Vals.size()-i-1, Bit); |
| 1126 | } |
| 1127 | return Result; |
| 1128 | } |
| 1129 | case tgtok::l_square: { // Value ::= '[' ValueList ']' |
| 1130 | Lex.Lex(); // eat the '[' |
| 1131 | std::vector<Init*> Vals; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1132 | |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1133 | RecTy *DeducedEltTy = 0; |
| 1134 | ListRecTy *GivenListTy = 0; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1135 | |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1136 | if (ItemType != 0) { |
| 1137 | ListRecTy *ListType = dynamic_cast<ListRecTy*>(ItemType); |
| 1138 | if (ListType == 0) { |
| 1139 | std::stringstream s; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1140 | s << "Type mismatch for list, expected list type, got " |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1141 | << ItemType->getAsString(); |
| 1142 | TokError(s.str()); |
| 1143 | } |
| 1144 | GivenListTy = ListType; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1145 | } |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1146 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1147 | if (Lex.getCode() != tgtok::r_square) { |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1148 | Vals = ParseValueList(CurRec, 0, |
| 1149 | GivenListTy ? GivenListTy->getElementType() : 0); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1150 | if (Vals.empty()) return 0; |
| 1151 | } |
| 1152 | if (Lex.getCode() != tgtok::r_square) { |
| 1153 | TokError("expected ']' at end of list value"); |
| 1154 | return 0; |
| 1155 | } |
| 1156 | Lex.Lex(); // eat the ']' |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1157 | |
| 1158 | RecTy *GivenEltTy = 0; |
| 1159 | if (Lex.getCode() == tgtok::less) { |
| 1160 | // Optional list element type |
| 1161 | Lex.Lex(); // eat the '<' |
| 1162 | |
| 1163 | GivenEltTy = ParseType(); |
| 1164 | if (GivenEltTy == 0) { |
| 1165 | // Couldn't parse element type |
| 1166 | return 0; |
| 1167 | } |
| 1168 | |
| 1169 | if (Lex.getCode() != tgtok::greater) { |
| 1170 | TokError("expected '>' at end of list element type"); |
| 1171 | return 0; |
| 1172 | } |
| 1173 | Lex.Lex(); // eat the '>' |
| 1174 | } |
| 1175 | |
| 1176 | // Check elements |
| 1177 | RecTy *EltTy = 0; |
| 1178 | for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end(); |
| 1179 | i != ie; |
| 1180 | ++i) { |
| 1181 | TypedInit *TArg = dynamic_cast<TypedInit*>(*i); |
| 1182 | if (TArg == 0) { |
| 1183 | TokError("Untyped list element"); |
| 1184 | return 0; |
| 1185 | } |
| 1186 | if (EltTy != 0) { |
| 1187 | EltTy = resolveTypes(EltTy, TArg->getType()); |
| 1188 | if (EltTy == 0) { |
| 1189 | TokError("Incompatible types in list elements"); |
| 1190 | return 0; |
| 1191 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1192 | } else { |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1193 | EltTy = TArg->getType(); |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | if (GivenEltTy != 0) { |
| 1198 | if (EltTy != 0) { |
| 1199 | // Verify consistency |
| 1200 | if (!EltTy->typeIsConvertibleTo(GivenEltTy)) { |
| 1201 | TokError("Incompatible types in list elements"); |
| 1202 | return 0; |
| 1203 | } |
| 1204 | } |
| 1205 | EltTy = GivenEltTy; |
| 1206 | } |
| 1207 | |
| 1208 | if (EltTy == 0) { |
| 1209 | if (ItemType == 0) { |
| 1210 | TokError("No type for list"); |
| 1211 | return 0; |
| 1212 | } |
| 1213 | DeducedEltTy = GivenListTy->getElementType(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1214 | } else { |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1215 | // Make sure the deduced type is compatible with the given type |
| 1216 | if (GivenListTy) { |
| 1217 | if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) { |
| 1218 | TokError("Element type mismatch for list"); |
| 1219 | return 0; |
| 1220 | } |
| 1221 | } |
| 1222 | DeducedEltTy = EltTy; |
| 1223 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1224 | |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1225 | return new ListInit(Vals, DeducedEltTy); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1226 | } |
| 1227 | case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')' |
| 1228 | Lex.Lex(); // eat the '(' |
David Greene | c7cafcd | 2009-04-22 20:18:10 +0000 | [diff] [blame] | 1229 | if (Lex.getCode() != tgtok::Id |
David Greene | e6c27de | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 1230 | && Lex.getCode() != tgtok::XCast |
David Greene | c7cafcd | 2009-04-22 20:18:10 +0000 | [diff] [blame] | 1231 | && Lex.getCode() != tgtok::XNameConcat) { |
Chris Lattner | 3dc2e96 | 2008-04-10 04:48:34 +0000 | [diff] [blame] | 1232 | TokError("expected identifier in dag init"); |
| 1233 | return 0; |
| 1234 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1235 | |
David Greene | c7cafcd | 2009-04-22 20:18:10 +0000 | [diff] [blame] | 1236 | Init *Operator = 0; |
| 1237 | if (Lex.getCode() == tgtok::Id) { |
| 1238 | Operator = ParseIDValue(CurRec); |
| 1239 | if (Operator == 0) return 0; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1240 | } else { |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 1241 | Operator = ParseOperation(CurRec); |
| 1242 | if (Operator == 0) return 0; |
David Greene | c7cafcd | 2009-04-22 20:18:10 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Nate Begeman | 7cee817 | 2009-03-19 05:21:56 +0000 | [diff] [blame] | 1245 | // If the operator name is present, parse it. |
| 1246 | std::string OperatorName; |
| 1247 | if (Lex.getCode() == tgtok::colon) { |
| 1248 | if (Lex.Lex() != tgtok::VarName) { // eat the ':' |
| 1249 | TokError("expected variable name in dag operator"); |
| 1250 | return 0; |
| 1251 | } |
| 1252 | OperatorName = Lex.getCurStrVal(); |
| 1253 | Lex.Lex(); // eat the VarName. |
| 1254 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1255 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1256 | std::vector<std::pair<llvm::Init*, std::string> > DagArgs; |
| 1257 | if (Lex.getCode() != tgtok::r_paren) { |
| 1258 | DagArgs = ParseDagArgList(CurRec); |
| 1259 | if (DagArgs.empty()) return 0; |
| 1260 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1261 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1262 | if (Lex.getCode() != tgtok::r_paren) { |
| 1263 | TokError("expected ')' in dag init"); |
| 1264 | return 0; |
| 1265 | } |
| 1266 | Lex.Lex(); // eat the ')' |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1267 | |
Nate Begeman | 7cee817 | 2009-03-19 05:21:56 +0000 | [diff] [blame] | 1268 | return new DagInit(Operator, OperatorName, DagArgs); |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 1269 | break; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1270 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1271 | |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 1272 | case tgtok::XCar: |
| 1273 | case tgtok::XCdr: |
| 1274 | case tgtok::XNull: |
David Greene | e6c27de | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 1275 | case tgtok::XCast: // Value ::= !unop '(' Value ')' |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1276 | case tgtok::XConcat: |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1277 | case tgtok::XSRA: |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1278 | case tgtok::XSRL: |
| 1279 | case tgtok::XSHL: |
David Greene | 6786d5e | 2010-01-05 19:11:42 +0000 | [diff] [blame] | 1280 | case tgtok::XEq: |
David Greene | c7cafcd | 2009-04-22 20:18:10 +0000 | [diff] [blame] | 1281 | case tgtok::XStrConcat: |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1282 | case tgtok::XNameConcat: // Value ::= !binop '(' Value ',' Value ')' |
David Greene | 9bea7c8 | 2009-05-14 23:26:46 +0000 | [diff] [blame] | 1283 | case tgtok::XIf: |
David Greene | beb31a5 | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1284 | case tgtok::XForEach: |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1285 | case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')' |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 1286 | return ParseOperation(CurRec); |
| 1287 | break; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1288 | } |
| 1289 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1290 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1291 | return R; |
| 1292 | } |
| 1293 | |
| 1294 | /// ParseValue - Parse a tblgen value. This returns null on error. |
| 1295 | /// |
| 1296 | /// Value ::= SimpleValue ValueSuffix* |
| 1297 | /// ValueSuffix ::= '{' BitList '}' |
| 1298 | /// ValueSuffix ::= '[' BitList ']' |
| 1299 | /// ValueSuffix ::= '.' ID |
| 1300 | /// |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1301 | Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType) { |
| 1302 | Init *Result = ParseSimpleValue(CurRec, ItemType); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1303 | if (Result == 0) return 0; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1304 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1305 | // Parse the suffixes now if present. |
| 1306 | while (1) { |
| 1307 | switch (Lex.getCode()) { |
| 1308 | default: return Result; |
| 1309 | case tgtok::l_brace: { |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 1310 | SMLoc CurlyLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1311 | Lex.Lex(); // eat the '{' |
| 1312 | std::vector<unsigned> Ranges = ParseRangeList(); |
| 1313 | if (Ranges.empty()) return 0; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1314 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1315 | // Reverse the bitlist. |
| 1316 | std::reverse(Ranges.begin(), Ranges.end()); |
| 1317 | Result = Result->convertInitializerBitRange(Ranges); |
| 1318 | if (Result == 0) { |
| 1319 | Error(CurlyLoc, "Invalid bit range for value"); |
| 1320 | return 0; |
| 1321 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1322 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1323 | // Eat the '}'. |
| 1324 | if (Lex.getCode() != tgtok::r_brace) { |
| 1325 | TokError("expected '}' at end of bit range list"); |
| 1326 | return 0; |
| 1327 | } |
| 1328 | Lex.Lex(); |
| 1329 | break; |
| 1330 | } |
| 1331 | case tgtok::l_square: { |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 1332 | SMLoc SquareLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1333 | Lex.Lex(); // eat the '[' |
| 1334 | std::vector<unsigned> Ranges = ParseRangeList(); |
| 1335 | if (Ranges.empty()) return 0; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1336 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1337 | Result = Result->convertInitListSlice(Ranges); |
| 1338 | if (Result == 0) { |
| 1339 | Error(SquareLoc, "Invalid range for list slice"); |
| 1340 | return 0; |
| 1341 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1342 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1343 | // Eat the ']'. |
| 1344 | if (Lex.getCode() != tgtok::r_square) { |
| 1345 | TokError("expected ']' at end of list slice"); |
| 1346 | return 0; |
| 1347 | } |
| 1348 | Lex.Lex(); |
| 1349 | break; |
| 1350 | } |
| 1351 | case tgtok::period: |
| 1352 | if (Lex.Lex() != tgtok::Id) { // eat the . |
| 1353 | TokError("expected field identifier after '.'"); |
| 1354 | return 0; |
| 1355 | } |
| 1356 | if (!Result->getFieldType(Lex.getCurStrVal())) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1357 | TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" + |
Chris Lattner | 5d81486 | 2007-11-22 21:06:59 +0000 | [diff] [blame] | 1358 | Result->getAsString() + "'"); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1359 | return 0; |
| 1360 | } |
| 1361 | Result = new FieldInit(Result, Lex.getCurStrVal()); |
| 1362 | Lex.Lex(); // eat field name |
| 1363 | break; |
| 1364 | } |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | /// ParseDagArgList - Parse the argument list for a dag literal expression. |
| 1369 | /// |
| 1370 | /// ParseDagArgList ::= Value (':' VARNAME)? |
| 1371 | /// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)? |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1372 | std::vector<std::pair<llvm::Init*, std::string> > |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1373 | TGParser::ParseDagArgList(Record *CurRec) { |
| 1374 | std::vector<std::pair<llvm::Init*, std::string> > Result; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1375 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1376 | while (1) { |
| 1377 | Init *Val = ParseValue(CurRec); |
| 1378 | if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1379 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1380 | // If the variable name is present, add it. |
| 1381 | std::string VarName; |
| 1382 | if (Lex.getCode() == tgtok::colon) { |
| 1383 | if (Lex.Lex() != tgtok::VarName) { // eat the ':' |
| 1384 | TokError("expected variable name in dag literal"); |
| 1385 | return std::vector<std::pair<llvm::Init*, std::string> >(); |
| 1386 | } |
| 1387 | VarName = Lex.getCurStrVal(); |
| 1388 | Lex.Lex(); // eat the VarName. |
| 1389 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1390 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1391 | Result.push_back(std::make_pair(Val, VarName)); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1392 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1393 | if (Lex.getCode() != tgtok::comma) break; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1394 | Lex.Lex(); // eat the ',' |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1395 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1396 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1397 | return Result; |
| 1398 | } |
| 1399 | |
| 1400 | |
| 1401 | /// ParseValueList - Parse a comma separated list of values, returning them as a |
| 1402 | /// vector. Note that this always expects to be able to parse at least one |
| 1403 | /// value. It returns an empty list if this is not possible. |
| 1404 | /// |
| 1405 | /// ValueList ::= Value (',' Value) |
| 1406 | /// |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1407 | std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec, |
| 1408 | RecTy *EltTy) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1409 | std::vector<Init*> Result; |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1410 | RecTy *ItemType = EltTy; |
David Greene | 67acdf2 | 2009-06-29 19:59:52 +0000 | [diff] [blame] | 1411 | unsigned int ArgN = 0; |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1412 | if (ArgsRec != 0 && EltTy == 0) { |
| 1413 | const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs(); |
| 1414 | const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]); |
| 1415 | assert(RV && "Template argument record not found??"); |
| 1416 | ItemType = RV->getType(); |
| 1417 | ++ArgN; |
| 1418 | } |
| 1419 | Result.push_back(ParseValue(CurRec, ItemType)); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1420 | if (Result.back() == 0) return std::vector<Init*>(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1421 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1422 | while (Lex.getCode() == tgtok::comma) { |
| 1423 | Lex.Lex(); // Eat the comma |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1424 | |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1425 | if (ArgsRec != 0 && EltTy == 0) { |
| 1426 | const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs(); |
David Greene | 67acdf2 | 2009-06-29 19:59:52 +0000 | [diff] [blame] | 1427 | if (ArgN >= TArgs.size()) { |
| 1428 | TokError("too many template arguments"); |
| 1429 | return std::vector<Init*>(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1430 | } |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1431 | const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]); |
| 1432 | assert(RV && "Template argument record not found??"); |
| 1433 | ItemType = RV->getType(); |
| 1434 | ++ArgN; |
| 1435 | } |
| 1436 | Result.push_back(ParseValue(CurRec, ItemType)); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1437 | if (Result.back() == 0) return std::vector<Init*>(); |
| 1438 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1439 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1440 | return Result; |
| 1441 | } |
| 1442 | |
| 1443 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1444 | /// ParseDeclaration - Read a declaration, returning the name of field ID, or an |
| 1445 | /// empty string on error. This can happen in a number of different context's, |
| 1446 | /// including within a def or in the template args for a def (which which case |
| 1447 | /// CurRec will be non-null) and within the template args for a multiclass (in |
| 1448 | /// which case CurRec will be null, but CurMultiClass will be set). This can |
| 1449 | /// also happen within a def that is within a multiclass, which will set both |
| 1450 | /// CurRec and CurMultiClass. |
| 1451 | /// |
| 1452 | /// Declaration ::= FIELD? Type ID ('=' Value)? |
| 1453 | /// |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1454 | std::string TGParser::ParseDeclaration(Record *CurRec, |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1455 | bool ParsingTemplateArgs) { |
| 1456 | // Read the field prefix if present. |
| 1457 | bool HasField = Lex.getCode() == tgtok::Field; |
| 1458 | if (HasField) Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1459 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1460 | RecTy *Type = ParseType(); |
| 1461 | if (Type == 0) return ""; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1462 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1463 | if (Lex.getCode() != tgtok::Id) { |
| 1464 | TokError("Expected identifier in declaration"); |
| 1465 | return ""; |
| 1466 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1467 | |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 1468 | SMLoc IdLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1469 | std::string DeclName = Lex.getCurStrVal(); |
| 1470 | Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1471 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1472 | if (ParsingTemplateArgs) { |
| 1473 | if (CurRec) { |
| 1474 | DeclName = CurRec->getName() + ":" + DeclName; |
| 1475 | } else { |
| 1476 | assert(CurMultiClass); |
| 1477 | } |
| 1478 | if (CurMultiClass) |
| 1479 | DeclName = CurMultiClass->Rec.getName() + "::" + DeclName; |
| 1480 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1481 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1482 | // Add the value. |
| 1483 | if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField))) |
| 1484 | return ""; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1485 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1486 | // If a value is present, parse it. |
| 1487 | if (Lex.getCode() == tgtok::equal) { |
| 1488 | Lex.Lex(); |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 1489 | SMLoc ValLoc = Lex.getLoc(); |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1490 | Init *Val = ParseValue(CurRec, Type); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1491 | if (Val == 0 || |
| 1492 | SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val)) |
| 1493 | return ""; |
| 1494 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1495 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1496 | return DeclName; |
| 1497 | } |
| 1498 | |
| 1499 | /// ParseTemplateArgList - Read a template argument list, which is a non-empty |
| 1500 | /// sequence of template-declarations in <>'s. If CurRec is non-null, these are |
| 1501 | /// template args for a def, which may or may not be in a multiclass. If null, |
| 1502 | /// these are the template args for a multiclass. |
| 1503 | /// |
| 1504 | /// TemplateArgList ::= '<' Declaration (',' Declaration)* '>' |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1505 | /// |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1506 | bool TGParser::ParseTemplateArgList(Record *CurRec) { |
| 1507 | assert(Lex.getCode() == tgtok::less && "Not a template arg list!"); |
| 1508 | Lex.Lex(); // eat the '<' |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1509 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1510 | Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1511 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1512 | // Read the first declaration. |
| 1513 | std::string TemplArg = ParseDeclaration(CurRec, true/*templateargs*/); |
| 1514 | if (TemplArg.empty()) |
| 1515 | return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1516 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1517 | TheRecToAddTo->addTemplateArg(TemplArg); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1518 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1519 | while (Lex.getCode() == tgtok::comma) { |
| 1520 | Lex.Lex(); // eat the ',' |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1521 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1522 | // Read the following declarations. |
| 1523 | TemplArg = ParseDeclaration(CurRec, true/*templateargs*/); |
| 1524 | if (TemplArg.empty()) |
| 1525 | return true; |
| 1526 | TheRecToAddTo->addTemplateArg(TemplArg); |
| 1527 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1528 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1529 | if (Lex.getCode() != tgtok::greater) |
| 1530 | return TokError("expected '>' at end of template argument list"); |
| 1531 | Lex.Lex(); // eat the '>'. |
| 1532 | return false; |
| 1533 | } |
| 1534 | |
| 1535 | |
| 1536 | /// ParseBodyItem - Parse a single item at within the body of a def or class. |
| 1537 | /// |
| 1538 | /// BodyItem ::= Declaration ';' |
| 1539 | /// BodyItem ::= LET ID OptionalBitList '=' Value ';' |
| 1540 | bool TGParser::ParseBodyItem(Record *CurRec) { |
| 1541 | if (Lex.getCode() != tgtok::Let) { |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1542 | if (ParseDeclaration(CurRec, false).empty()) |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1543 | return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1544 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1545 | if (Lex.getCode() != tgtok::semi) |
| 1546 | return TokError("expected ';' after declaration"); |
| 1547 | Lex.Lex(); |
| 1548 | return false; |
| 1549 | } |
| 1550 | |
| 1551 | // LET ID OptionalRangeList '=' Value ';' |
| 1552 | if (Lex.Lex() != tgtok::Id) |
| 1553 | return TokError("expected field identifier after let"); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1554 | |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 1555 | SMLoc IdLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1556 | std::string FieldName = Lex.getCurStrVal(); |
| 1557 | Lex.Lex(); // eat the field name. |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1558 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1559 | std::vector<unsigned> BitList; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1560 | if (ParseOptionalBitList(BitList)) |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1561 | return true; |
| 1562 | std::reverse(BitList.begin(), BitList.end()); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1563 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1564 | if (Lex.getCode() != tgtok::equal) |
| 1565 | return TokError("expected '=' in let expression"); |
| 1566 | Lex.Lex(); // eat the '='. |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1567 | |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1568 | RecordVal *Field = CurRec->getValue(FieldName); |
| 1569 | if (Field == 0) |
| 1570 | return TokError("Value '" + FieldName + "' unknown!"); |
| 1571 | |
| 1572 | RecTy *Type = Field->getType(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1573 | |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1574 | Init *Val = ParseValue(CurRec, Type); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1575 | if (Val == 0) return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1576 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1577 | if (Lex.getCode() != tgtok::semi) |
| 1578 | return TokError("expected ';' after let expression"); |
| 1579 | Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1580 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1581 | return SetValue(CurRec, IdLoc, FieldName, BitList, Val); |
| 1582 | } |
| 1583 | |
| 1584 | /// ParseBody - Read the body of a class or def. Return true on error, false on |
| 1585 | /// success. |
| 1586 | /// |
| 1587 | /// Body ::= ';' |
| 1588 | /// Body ::= '{' BodyList '}' |
| 1589 | /// BodyList BodyItem* |
| 1590 | /// |
| 1591 | bool TGParser::ParseBody(Record *CurRec) { |
| 1592 | // If this is a null definition, just eat the semi and return. |
| 1593 | if (Lex.getCode() == tgtok::semi) { |
| 1594 | Lex.Lex(); |
| 1595 | return false; |
| 1596 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1597 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1598 | if (Lex.getCode() != tgtok::l_brace) |
| 1599 | return TokError("Expected ';' or '{' to start body"); |
| 1600 | // Eat the '{'. |
| 1601 | Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1602 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1603 | while (Lex.getCode() != tgtok::r_brace) |
| 1604 | if (ParseBodyItem(CurRec)) |
| 1605 | return true; |
| 1606 | |
| 1607 | // Eat the '}'. |
| 1608 | Lex.Lex(); |
| 1609 | return false; |
| 1610 | } |
| 1611 | |
| 1612 | /// ParseObjectBody - Parse the body of a def or class. This consists of an |
| 1613 | /// optional ClassList followed by a Body. CurRec is the current def or class |
| 1614 | /// that is being parsed. |
| 1615 | /// |
| 1616 | /// ObjectBody ::= BaseClassList Body |
| 1617 | /// BaseClassList ::= /*empty*/ |
| 1618 | /// BaseClassList ::= ':' BaseClassListNE |
| 1619 | /// BaseClassListNE ::= SubClassRef (',' SubClassRef)* |
| 1620 | /// |
| 1621 | bool TGParser::ParseObjectBody(Record *CurRec) { |
| 1622 | // If there is a baseclass list, read it. |
| 1623 | if (Lex.getCode() == tgtok::colon) { |
| 1624 | Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1625 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1626 | // Read all of the subclasses. |
| 1627 | SubClassReference SubClass = ParseSubClassReference(CurRec, false); |
| 1628 | while (1) { |
| 1629 | // Check for error. |
| 1630 | if (SubClass.Rec == 0) return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1631 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1632 | // Add it. |
| 1633 | if (AddSubClass(CurRec, SubClass)) |
| 1634 | return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1635 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1636 | if (Lex.getCode() != tgtok::comma) break; |
| 1637 | Lex.Lex(); // eat ','. |
| 1638 | SubClass = ParseSubClassReference(CurRec, false); |
| 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | // Process any variables on the let stack. |
| 1643 | for (unsigned i = 0, e = LetStack.size(); i != e; ++i) |
| 1644 | for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j) |
| 1645 | if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name, |
| 1646 | LetStack[i][j].Bits, LetStack[i][j].Value)) |
| 1647 | return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1648 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1649 | return ParseBody(CurRec); |
| 1650 | } |
| 1651 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1652 | /// ParseDef - Parse and return a top level or multiclass def, return the record |
| 1653 | /// corresponding to it. This returns null on error. |
| 1654 | /// |
| 1655 | /// DefInst ::= DEF ObjectName ObjectBody |
| 1656 | /// |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 1657 | bool TGParser::ParseDef(MultiClass *CurMultiClass) { |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 1658 | SMLoc DefLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1659 | assert(Lex.getCode() == tgtok::Def && "Unknown tok"); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1660 | Lex.Lex(); // Eat the 'def' token. |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1661 | |
| 1662 | // Parse ObjectName and make a record for it. |
Chris Lattner | 7b9ffe4 | 2009-03-13 16:09:24 +0000 | [diff] [blame] | 1663 | Record *CurRec = new Record(ParseObjectName(), DefLoc); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1664 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1665 | if (!CurMultiClass) { |
| 1666 | // Top-level def definition. |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1667 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1668 | // Ensure redefinition doesn't happen. |
| 1669 | if (Records.getDef(CurRec->getName())) { |
| 1670 | Error(DefLoc, "def '" + CurRec->getName() + "' already defined"); |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 1671 | return true; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1672 | } |
| 1673 | Records.addDef(CurRec); |
| 1674 | } else { |
| 1675 | // Otherwise, a def inside a multiclass, add it to the multiclass. |
| 1676 | for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i) |
| 1677 | if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) { |
| 1678 | Error(DefLoc, "def '" + CurRec->getName() + |
| 1679 | "' already defined in this multiclass!"); |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 1680 | return true; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1681 | } |
| 1682 | CurMultiClass->DefPrototypes.push_back(CurRec); |
| 1683 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1684 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1685 | if (ParseObjectBody(CurRec)) |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 1686 | return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1687 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1688 | if (CurMultiClass == 0) // Def's in multiclasses aren't really defs. |
| 1689 | CurRec->resolveReferences(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1690 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1691 | // If ObjectBody has template arguments, it's an error. |
| 1692 | assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?"); |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 1693 | |
| 1694 | if (CurMultiClass) { |
| 1695 | // Copy the template arguments for the multiclass into the def. |
| 1696 | const std::vector<std::string> &TArgs = |
| 1697 | CurMultiClass->Rec.getTemplateArgs(); |
| 1698 | |
| 1699 | for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { |
| 1700 | const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]); |
| 1701 | assert(RV && "Template arg doesn't exist?"); |
| 1702 | CurRec->addValue(*RV); |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | return false; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | |
| 1710 | /// ParseClass - Parse a tblgen class definition. |
| 1711 | /// |
| 1712 | /// ClassInst ::= CLASS ID TemplateArgList? ObjectBody |
| 1713 | /// |
| 1714 | bool TGParser::ParseClass() { |
| 1715 | assert(Lex.getCode() == tgtok::Class && "Unexpected token!"); |
| 1716 | Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1717 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1718 | if (Lex.getCode() != tgtok::Id) |
| 1719 | return TokError("expected class name after 'class' keyword"); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1720 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1721 | Record *CurRec = Records.getClass(Lex.getCurStrVal()); |
| 1722 | if (CurRec) { |
| 1723 | // If the body was previously defined, this is an error. |
| 1724 | if (!CurRec->getValues().empty() || |
| 1725 | !CurRec->getSuperClasses().empty() || |
| 1726 | !CurRec->getTemplateArgs().empty()) |
| 1727 | return TokError("Class '" + CurRec->getName() + "' already defined"); |
| 1728 | } else { |
| 1729 | // 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] | 1730 | CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc()); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1731 | Records.addClass(CurRec); |
| 1732 | } |
| 1733 | Lex.Lex(); // eat the name. |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1734 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1735 | // If there are template args, parse them. |
| 1736 | if (Lex.getCode() == tgtok::less) |
| 1737 | if (ParseTemplateArgList(CurRec)) |
| 1738 | return true; |
| 1739 | |
| 1740 | // Finally, parse the object body. |
| 1741 | return ParseObjectBody(CurRec); |
| 1742 | } |
| 1743 | |
| 1744 | /// ParseLetList - Parse a non-empty list of assignment expressions into a list |
| 1745 | /// of LetRecords. |
| 1746 | /// |
| 1747 | /// LetList ::= LetItem (',' LetItem)* |
| 1748 | /// LetItem ::= ID OptionalRangeList '=' Value |
| 1749 | /// |
| 1750 | std::vector<LetRecord> TGParser::ParseLetList() { |
| 1751 | std::vector<LetRecord> Result; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1752 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1753 | while (1) { |
| 1754 | if (Lex.getCode() != tgtok::Id) { |
| 1755 | TokError("expected identifier in let definition"); |
| 1756 | return std::vector<LetRecord>(); |
| 1757 | } |
| 1758 | std::string Name = Lex.getCurStrVal(); |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 1759 | SMLoc NameLoc = Lex.getLoc(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1760 | Lex.Lex(); // Eat the identifier. |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1761 | |
| 1762 | // Check for an optional RangeList. |
| 1763 | std::vector<unsigned> Bits; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1764 | if (ParseOptionalRangeList(Bits)) |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1765 | return std::vector<LetRecord>(); |
| 1766 | std::reverse(Bits.begin(), Bits.end()); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1767 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1768 | if (Lex.getCode() != tgtok::equal) { |
| 1769 | TokError("expected '=' in let expression"); |
| 1770 | return std::vector<LetRecord>(); |
| 1771 | } |
| 1772 | Lex.Lex(); // eat the '='. |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1773 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1774 | Init *Val = ParseValue(0); |
| 1775 | if (Val == 0) return std::vector<LetRecord>(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1776 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1777 | // Now that we have everything, add the record. |
| 1778 | Result.push_back(LetRecord(Name, Bits, Val, NameLoc)); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1779 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1780 | if (Lex.getCode() != tgtok::comma) |
| 1781 | return Result; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1782 | Lex.Lex(); // eat the comma. |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1783 | } |
| 1784 | } |
| 1785 | |
| 1786 | /// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 1787 | /// different related productions. This works inside multiclasses too. |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1788 | /// |
| 1789 | /// Object ::= LET LetList IN '{' ObjectList '}' |
| 1790 | /// Object ::= LET LetList IN Object |
| 1791 | /// |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 1792 | bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1793 | assert(Lex.getCode() == tgtok::Let && "Unexpected token"); |
| 1794 | Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1795 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1796 | // Add this entry to the let stack. |
| 1797 | std::vector<LetRecord> LetInfo = ParseLetList(); |
| 1798 | if (LetInfo.empty()) return true; |
| 1799 | LetStack.push_back(LetInfo); |
| 1800 | |
| 1801 | if (Lex.getCode() != tgtok::In) |
| 1802 | return TokError("expected 'in' at end of top-level 'let'"); |
| 1803 | Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1804 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1805 | // If this is a scalar let, just handle it now |
| 1806 | if (Lex.getCode() != tgtok::l_brace) { |
| 1807 | // LET LetList IN Object |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 1808 | if (ParseObject(CurMultiClass)) |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1809 | return true; |
| 1810 | } else { // Object ::= LETCommand '{' ObjectList '}' |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 1811 | SMLoc BraceLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1812 | // Otherwise, this is a group let. |
| 1813 | Lex.Lex(); // eat the '{'. |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1814 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1815 | // Parse the object list. |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 1816 | if (ParseObjectList(CurMultiClass)) |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1817 | return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1818 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1819 | if (Lex.getCode() != tgtok::r_brace) { |
| 1820 | TokError("expected '}' at end of top level let command"); |
| 1821 | return Error(BraceLoc, "to match this '{'"); |
| 1822 | } |
| 1823 | Lex.Lex(); |
| 1824 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1825 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1826 | // Outside this let scope, this let block is not active. |
| 1827 | LetStack.pop_back(); |
| 1828 | return false; |
| 1829 | } |
| 1830 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1831 | /// ParseMultiClass - Parse a multiclass definition. |
| 1832 | /// |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 1833 | /// MultiClassInst ::= MULTICLASS ID TemplateArgList? |
| 1834 | /// ':' BaseMultiClassList '{' MultiClassDef+ '}' |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1835 | /// |
| 1836 | bool TGParser::ParseMultiClass() { |
| 1837 | assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token"); |
| 1838 | Lex.Lex(); // Eat the multiclass token. |
| 1839 | |
| 1840 | if (Lex.getCode() != tgtok::Id) |
| 1841 | return TokError("expected identifier after multiclass for name"); |
| 1842 | std::string Name = Lex.getCurStrVal(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1843 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1844 | if (MultiClasses.count(Name)) |
| 1845 | return TokError("multiclass '" + Name + "' already defined"); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1846 | |
Chris Lattner | 7b9ffe4 | 2009-03-13 16:09:24 +0000 | [diff] [blame] | 1847 | CurMultiClass = MultiClasses[Name] = new MultiClass(Name, Lex.getLoc()); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1848 | Lex.Lex(); // Eat the identifier. |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1849 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1850 | // If there are template args, parse them. |
| 1851 | if (Lex.getCode() == tgtok::less) |
| 1852 | if (ParseTemplateArgList(0)) |
| 1853 | return true; |
| 1854 | |
David Greene | d34a73b | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 1855 | bool inherits = false; |
| 1856 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 1857 | // If there are submulticlasses, parse them. |
| 1858 | if (Lex.getCode() == tgtok::colon) { |
David Greene | d34a73b | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 1859 | inherits = true; |
| 1860 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 1861 | Lex.Lex(); |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 1862 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 1863 | // Read all of the submulticlasses. |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 1864 | SubMultiClassReference SubMultiClass = |
| 1865 | ParseSubMultiClassReference(CurMultiClass); |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 1866 | while (1) { |
| 1867 | // Check for error. |
| 1868 | if (SubMultiClass.MC == 0) return true; |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 1869 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 1870 | // Add it. |
| 1871 | if (AddSubMultiClass(CurMultiClass, SubMultiClass)) |
| 1872 | return true; |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 1873 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 1874 | if (Lex.getCode() != tgtok::comma) break; |
| 1875 | Lex.Lex(); // eat ','. |
| 1876 | SubMultiClass = ParseSubMultiClassReference(CurMultiClass); |
| 1877 | } |
| 1878 | } |
| 1879 | |
David Greene | d34a73b | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 1880 | if (Lex.getCode() != tgtok::l_brace) { |
| 1881 | if (!inherits) |
| 1882 | return TokError("expected '{' in multiclass definition"); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1883 | else if (Lex.getCode() != tgtok::semi) |
| 1884 | return TokError("expected ';' in multiclass definition"); |
David Greene | d34a73b | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 1885 | else |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1886 | Lex.Lex(); // eat the ';'. |
| 1887 | } else { |
David Greene | d34a73b | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 1888 | if (Lex.Lex() == tgtok::r_brace) // eat the '{'. |
| 1889 | return TokError("multiclass must contain at least one def"); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1890 | |
Bruno Cardoso Lopes | 270562b | 2010-06-05 02:11:52 +0000 | [diff] [blame] | 1891 | while (Lex.getCode() != tgtok::r_brace) { |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 1892 | switch (Lex.getCode()) { |
| 1893 | default: |
| 1894 | return TokError("expected 'let', 'def' or 'defm' in multiclass body"); |
| 1895 | case tgtok::Let: |
| 1896 | case tgtok::Def: |
| 1897 | case tgtok::Defm: |
| 1898 | if (ParseObject(CurMultiClass)) |
| 1899 | return true; |
| 1900 | break; |
| 1901 | } |
Bruno Cardoso Lopes | 270562b | 2010-06-05 02:11:52 +0000 | [diff] [blame] | 1902 | } |
David Greene | d34a73b | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 1903 | Lex.Lex(); // eat the '}'. |
| 1904 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1905 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1906 | CurMultiClass = 0; |
| 1907 | return false; |
| 1908 | } |
| 1909 | |
| 1910 | /// ParseDefm - Parse the instantiation of a multiclass. |
| 1911 | /// |
| 1912 | /// DefMInst ::= DEFM ID ':' DefmSubClassRef ';' |
| 1913 | /// |
Bruno Cardoso Lopes | 270562b | 2010-06-05 02:11:52 +0000 | [diff] [blame] | 1914 | bool TGParser::ParseDefm(MultiClass *CurMultiClass) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1915 | assert(Lex.getCode() == tgtok::Defm && "Unexpected token!"); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1916 | |
Chris Lattner | df72eae | 2010-10-05 22:51:56 +0000 | [diff] [blame] | 1917 | std::string DefmPrefix; |
| 1918 | if (Lex.Lex() == tgtok::Id) { // eat the defm. |
| 1919 | DefmPrefix = Lex.getCurStrVal(); |
| 1920 | Lex.Lex(); // Eat the defm prefix. |
| 1921 | } |
| 1922 | |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 1923 | SMLoc DefmPrefixLoc = Lex.getLoc(); |
Chris Lattner | df72eae | 2010-10-05 22:51:56 +0000 | [diff] [blame] | 1924 | if (Lex.getCode() != tgtok::colon) |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1925 | return TokError("expected ':' after defm identifier"); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1926 | |
Bruno Cardoso Lopes | 6e0a99a | 2010-06-18 19:53:41 +0000 | [diff] [blame] | 1927 | // Keep track of the new generated record definitions. |
| 1928 | std::vector<Record*> NewRecDefs; |
| 1929 | |
| 1930 | // This record also inherits from a regular class (non-multiclass)? |
| 1931 | bool InheritFromClass = false; |
| 1932 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1933 | // eat the colon. |
| 1934 | Lex.Lex(); |
| 1935 | |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 1936 | SMLoc SubClassLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1937 | SubClassReference Ref = ParseSubClassReference(0, true); |
David Greene | 5654613 | 2009-04-22 22:17:51 +0000 | [diff] [blame] | 1938 | |
| 1939 | while (1) { |
| 1940 | if (Ref.Rec == 0) return true; |
| 1941 | |
| 1942 | // To instantiate a multiclass, we need to first get the multiclass, then |
| 1943 | // instantiate each def contained in the multiclass with the SubClassRef |
| 1944 | // template parameters. |
| 1945 | MultiClass *MC = MultiClasses[Ref.Rec->getName()]; |
| 1946 | assert(MC && "Didn't lookup multiclass correctly?"); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1947 | std::vector<Init*> &TemplateVals = Ref.TemplateArgs; |
David Greene | 5654613 | 2009-04-22 22:17:51 +0000 | [diff] [blame] | 1948 | |
| 1949 | // Verify that the correct number of template arguments were specified. |
| 1950 | const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs(); |
| 1951 | if (TArgs.size() < TemplateVals.size()) |
| 1952 | return Error(SubClassLoc, |
| 1953 | "more template args specified than multiclass expects"); |
| 1954 | |
| 1955 | // Loop over all the def's in the multiclass, instantiating each one. |
| 1956 | for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) { |
| 1957 | Record *DefProto = MC->DefPrototypes[i]; |
| 1958 | |
Chris Lattner | df72eae | 2010-10-05 22:51:56 +0000 | [diff] [blame] | 1959 | // Add in the defm name. If the defm prefix is empty, give each |
| 1960 | // instantiated def a unique name. Otherwise, if "#NAME#" exists in the |
| 1961 | // name, substitute the prefix for #NAME#. Otherwise, use the defm name |
| 1962 | // as a prefix. |
David Greene | 065f259 | 2009-05-05 16:28:25 +0000 | [diff] [blame] | 1963 | std::string DefName = DefProto->getName(); |
Chris Lattner | df72eae | 2010-10-05 22:51:56 +0000 | [diff] [blame] | 1964 | if (DefmPrefix.empty()) { |
| 1965 | DefName = GetNewAnonymousName(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1966 | } else { |
Chris Lattner | df72eae | 2010-10-05 22:51:56 +0000 | [diff] [blame] | 1967 | std::string::size_type idx = DefName.find("#NAME#"); |
| 1968 | if (idx != std::string::npos) { |
| 1969 | DefName.replace(idx, 6, DefmPrefix); |
| 1970 | } else { |
| 1971 | // Add the suffix to the defm name to get the new name. |
| 1972 | DefName = DefmPrefix + DefName; |
| 1973 | } |
David Greene | 065f259 | 2009-05-05 16:28:25 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | Record *CurRec = new Record(DefName, DefmPrefixLoc); |
David Greene | 5654613 | 2009-04-22 22:17:51 +0000 | [diff] [blame] | 1977 | |
| 1978 | SubClassReference Ref; |
| 1979 | Ref.RefLoc = DefmPrefixLoc; |
| 1980 | Ref.Rec = DefProto; |
| 1981 | AddSubClass(CurRec, Ref); |
| 1982 | |
| 1983 | // Loop over all of the template arguments, setting them to the specified |
| 1984 | // value or leaving them as the default if necessary. |
| 1985 | for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 1986 | // Check if a value is specified for this temp-arg. |
| 1987 | if (i < TemplateVals.size()) { |
David Greene | 5654613 | 2009-04-22 22:17:51 +0000 | [diff] [blame] | 1988 | // Set it now. |
| 1989 | if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(), |
| 1990 | TemplateVals[i])) |
| 1991 | return true; |
| 1992 | |
| 1993 | // Resolve it next. |
| 1994 | CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i])); |
| 1995 | |
| 1996 | // Now remove it. |
| 1997 | CurRec->removeValue(TArgs[i]); |
| 1998 | |
| 1999 | } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) { |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 2000 | return Error(SubClassLoc, |
| 2001 | "value not specified for template argument #"+ |
David Greene | 5654613 | 2009-04-22 22:17:51 +0000 | [diff] [blame] | 2002 | utostr(i) + " (" + TArgs[i] + ") of multiclassclass '" + |
| 2003 | MC->Rec.getName() + "'"); |
| 2004 | } |
| 2005 | } |
| 2006 | |
| 2007 | // If the mdef is inside a 'let' expression, add to each def. |
| 2008 | for (unsigned i = 0, e = LetStack.size(); i != e; ++i) |
| 2009 | for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j) |
| 2010 | if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name, |
| 2011 | LetStack[i][j].Bits, LetStack[i][j].Value)) { |
| 2012 | Error(DefmPrefixLoc, "when instantiating this defm"); |
| 2013 | return true; |
| 2014 | } |
| 2015 | |
| 2016 | // Ensure redefinition doesn't happen. |
| 2017 | if (Records.getDef(CurRec->getName())) |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2018 | return Error(DefmPrefixLoc, "def '" + CurRec->getName() + |
| 2019 | "' already defined, instantiating defm with subdef '" + |
David Greene | 5654613 | 2009-04-22 22:17:51 +0000 | [diff] [blame] | 2020 | DefProto->getName() + "'"); |
Bruno Cardoso Lopes | 270562b | 2010-06-05 02:11:52 +0000 | [diff] [blame] | 2021 | |
| 2022 | // Don't create a top level definition for defm inside multiclasses, |
| 2023 | // instead, only update the prototypes and bind the template args |
| 2024 | // with the new created definition. |
| 2025 | if (CurMultiClass) { |
| 2026 | for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); |
| 2027 | i != e; ++i) { |
| 2028 | if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) { |
| 2029 | Error(DefmPrefixLoc, "defm '" + CurRec->getName() + |
| 2030 | "' already defined in this multiclass!"); |
| 2031 | return 0; |
| 2032 | } |
| 2033 | } |
| 2034 | CurMultiClass->DefPrototypes.push_back(CurRec); |
| 2035 | |
| 2036 | // Copy the template arguments for the multiclass into the new def. |
| 2037 | const std::vector<std::string> &TA = |
| 2038 | CurMultiClass->Rec.getTemplateArgs(); |
| 2039 | |
| 2040 | for (unsigned i = 0, e = TA.size(); i != e; ++i) { |
| 2041 | const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]); |
| 2042 | assert(RV && "Template arg doesn't exist?"); |
| 2043 | CurRec->addValue(*RV); |
| 2044 | } |
| 2045 | } else { |
| 2046 | Records.addDef(CurRec); |
Bruno Cardoso Lopes | 270562b | 2010-06-05 02:11:52 +0000 | [diff] [blame] | 2047 | } |
Bruno Cardoso Lopes | 6e0a99a | 2010-06-18 19:53:41 +0000 | [diff] [blame] | 2048 | |
| 2049 | NewRecDefs.push_back(CurRec); |
David Greene | 5654613 | 2009-04-22 22:17:51 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | if (Lex.getCode() != tgtok::comma) break; |
| 2053 | Lex.Lex(); // eat ','. |
| 2054 | |
| 2055 | SubClassLoc = Lex.getLoc(); |
Bruno Cardoso Lopes | 6e0a99a | 2010-06-18 19:53:41 +0000 | [diff] [blame] | 2056 | |
| 2057 | // A defm can inherit from regular classes (non-multiclass) as |
| 2058 | // long as they come in the end of the inheritance list. |
| 2059 | InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0); |
| 2060 | |
| 2061 | if (InheritFromClass) |
| 2062 | break; |
| 2063 | |
David Greene | 5654613 | 2009-04-22 22:17:51 +0000 | [diff] [blame] | 2064 | Ref = ParseSubClassReference(0, true); |
| 2065 | } |
| 2066 | |
Bruno Cardoso Lopes | 6e0a99a | 2010-06-18 19:53:41 +0000 | [diff] [blame] | 2067 | if (InheritFromClass) { |
| 2068 | // Process all the classes to inherit as if they were part of a |
| 2069 | // regular 'def' and inherit all record values. |
| 2070 | SubClassReference SubClass = ParseSubClassReference(0, false); |
| 2071 | while (1) { |
| 2072 | // Check for error. |
| 2073 | if (SubClass.Rec == 0) return true; |
| 2074 | |
| 2075 | // Get the expanded definition prototypes and teach them about |
| 2076 | // the record values the current class to inherit has |
| 2077 | for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) { |
| 2078 | Record *CurRec = NewRecDefs[i]; |
| 2079 | |
| 2080 | // Add it. |
| 2081 | if (AddSubClass(CurRec, SubClass)) |
| 2082 | return true; |
| 2083 | |
| 2084 | // Process any variables on the let stack. |
| 2085 | for (unsigned i = 0, e = LetStack.size(); i != e; ++i) |
| 2086 | for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j) |
| 2087 | if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name, |
| 2088 | LetStack[i][j].Bits, LetStack[i][j].Value)) |
| 2089 | return true; |
Bruno Cardoso Lopes | 6e0a99a | 2010-06-18 19:53:41 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | if (Lex.getCode() != tgtok::comma) break; |
| 2093 | Lex.Lex(); // eat ','. |
| 2094 | SubClass = ParseSubClassReference(0, false); |
| 2095 | } |
| 2096 | } |
| 2097 | |
Bruno Cardoso Lopes | e5104ac | 2010-06-22 20:30:50 +0000 | [diff] [blame] | 2098 | if (!CurMultiClass) |
| 2099 | for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) |
| 2100 | NewRecDefs[i]->resolveReferences(); |
| 2101 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2102 | if (Lex.getCode() != tgtok::semi) |
| 2103 | return TokError("expected ';' at end of defm"); |
| 2104 | Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2105 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2106 | return false; |
| 2107 | } |
| 2108 | |
| 2109 | /// ParseObject |
| 2110 | /// Object ::= ClassInst |
| 2111 | /// Object ::= DefInst |
| 2112 | /// Object ::= MultiClassInst |
| 2113 | /// Object ::= DefMInst |
| 2114 | /// Object ::= LETCommand '{' ObjectList '}' |
| 2115 | /// Object ::= LETCommand Object |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 2116 | bool TGParser::ParseObject(MultiClass *MC) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2117 | switch (Lex.getCode()) { |
| 2118 | default: assert(0 && "This is not an object"); |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 2119 | case tgtok::Let: return ParseTopLevelLet(MC); |
| 2120 | case tgtok::Def: return ParseDef(MC); |
| 2121 | case tgtok::Defm: return ParseDefm(MC); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2122 | case tgtok::Class: return ParseClass(); |
| 2123 | case tgtok::MultiClass: return ParseMultiClass(); |
| 2124 | } |
| 2125 | } |
| 2126 | |
| 2127 | /// ParseObjectList |
| 2128 | /// ObjectList :== Object* |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 2129 | bool TGParser::ParseObjectList(MultiClass *MC) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2130 | while (isObjectStart(Lex.getCode())) { |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 2131 | if (ParseObject(MC)) |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2132 | return true; |
| 2133 | } |
| 2134 | return false; |
| 2135 | } |
| 2136 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2137 | bool TGParser::ParseFile() { |
| 2138 | Lex.Lex(); // Prime the lexer. |
| 2139 | if (ParseObjectList()) return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2140 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2141 | // If we have unread input at the end of the file, report it. |
| 2142 | if (Lex.getCode() == tgtok::Eof) |
| 2143 | return false; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2144 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2145 | return TokError("Unexpected input at top level"); |
| 2146 | } |
| 2147 | |