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