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