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" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/Support/CommandLine.h" |
| 18 | #include "llvm/TableGen/Record.h" |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | #include <sstream> |
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; |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 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; |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 40 | std::vector<Init*> TemplateArgs; |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 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 | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 53 | for (std::vector<Init *>::const_iterator i = TemplateArgs.begin(), |
David Greene | d34a73b | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 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 | |
Jakob Stoklund Olesen | ebaf92c | 2012-01-13 03:16:35 +0000 | [diff] [blame] | 67 | if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 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. |
David Greene | 917924d | 2011-10-19 13:02:39 +0000 | [diff] [blame] | 82 | bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName, |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 83 | const std::vector<unsigned> &BitList, Init *V) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 84 | if (!V) return false; |
| 85 | |
| 86 | if (CurRec == 0) CurRec = &CurMultiClass->Rec; |
| 87 | |
| 88 | RecordVal *RV = CurRec->getValue(ValName); |
| 89 | if (RV == 0) |
David Greene | 917924d | 2011-10-19 13:02:39 +0000 | [diff] [blame] | 90 | return Error(Loc, "Value '" + ValName->getAsUnquotedString() |
| 91 | + "' unknown!"); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 92 | |
| 93 | // Do not allow assignments like 'X = X'. This will just cause infinite loops |
| 94 | // in the resolution machinery. |
| 95 | if (BitList.empty()) |
Sean Silva | 6cfc806 | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 96 | if (VarInit *VI = dyn_cast<VarInit>(V)) |
David Greene | 917924d | 2011-10-19 13:02:39 +0000 | [diff] [blame] | 97 | if (VI->getNameInit() == ValName) |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 98 | return false; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 99 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 100 | // If we are assigning to a subset of the bits in the value... then we must be |
| 101 | // assigning to a field of BitsRecTy, which must have a BitsInit |
| 102 | // initializer. |
| 103 | // |
| 104 | if (!BitList.empty()) { |
Sean Silva | 6cfc806 | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 105 | BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue()); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 106 | if (CurVal == 0) |
David Greene | 917924d | 2011-10-19 13:02:39 +0000 | [diff] [blame] | 107 | return Error(Loc, "Value '" + ValName->getAsUnquotedString() |
| 108 | + "' is not a bits type"); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 109 | |
| 110 | // Convert the incoming value to a bits type of the appropriate size... |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 111 | Init *BI = V->convertInitializerTo(BitsRecTy::get(BitList.size())); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 112 | if (BI == 0) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 113 | return Error(Loc, "Initializer is not compatible with bit range"); |
| 114 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 115 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 116 | // We should have a BitsInit type now. |
Sean Silva | 6cfc806 | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 117 | BitsInit *BInit = dyn_cast<BitsInit>(BI); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 118 | assert(BInit != 0); |
| 119 | |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 120 | SmallVector<Init *, 16> NewBits(CurVal->getNumBits()); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 121 | |
| 122 | // Loop over bits, assigning values as appropriate. |
| 123 | for (unsigned i = 0, e = BitList.size(); i != e; ++i) { |
| 124 | unsigned Bit = BitList[i]; |
David Greene | ca7fd3d | 2011-07-29 19:07:00 +0000 | [diff] [blame] | 125 | if (NewBits[Bit]) |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 126 | return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" + |
David Greene | 917924d | 2011-10-19 13:02:39 +0000 | [diff] [blame] | 127 | ValName->getAsUnquotedString() + "' more than once"); |
David Greene | ca7fd3d | 2011-07-29 19:07:00 +0000 | [diff] [blame] | 128 | NewBits[Bit] = BInit->getBit(i); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i) |
David Greene | ca7fd3d | 2011-07-29 19:07:00 +0000 | [diff] [blame] | 132 | if (NewBits[i] == 0) |
| 133 | NewBits[i] = CurVal->getBit(i); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 134 | |
David Greene | dcd35c7 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 135 | V = BitsInit::get(NewBits); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | if (RV->setValue(V)) |
David Greene | 917924d | 2011-10-19 13:02:39 +0000 | [diff] [blame] | 139 | return Error(Loc, "Value '" + ValName->getAsUnquotedString() + "' of type '" |
| 140 | + RV->getType()->getAsString() + |
| 141 | "' is incompatible with initializer '" + V->getAsString() |
| 142 | + "'"); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 143 | return false; |
| 144 | } |
| 145 | |
| 146 | /// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template |
| 147 | /// args as SubClass's template arguments. |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 148 | bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 149 | Record *SC = SubClass.Rec; |
| 150 | // Add all of the values in the subclass into the current class. |
| 151 | const std::vector<RecordVal> &Vals = SC->getValues(); |
| 152 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) |
| 153 | if (AddValue(CurRec, SubClass.RefLoc, Vals[i])) |
| 154 | return true; |
| 155 | |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 156 | const std::vector<Init *> &TArgs = SC->getTemplateArgs(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 157 | |
| 158 | // Ensure that an appropriate number of template arguments are specified. |
| 159 | if (TArgs.size() < SubClass.TemplateArgs.size()) |
| 160 | return Error(SubClass.RefLoc, "More template args specified than expected"); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 161 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 162 | // Loop over all of the template arguments, setting them to the specified |
| 163 | // value or leaving them as the default if necessary. |
| 164 | for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { |
| 165 | if (i < SubClass.TemplateArgs.size()) { |
| 166 | // If a value is specified for this template arg, set it now. |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 167 | if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(), |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 168 | SubClass.TemplateArgs[i])) |
| 169 | return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 170 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 171 | // Resolve it next. |
| 172 | CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i])); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 173 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 174 | // Now remove it. |
| 175 | CurRec->removeValue(TArgs[i]); |
| 176 | |
| 177 | } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) { |
| 178 | return Error(SubClass.RefLoc,"Value not specified for template argument #" |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 179 | + utostr(i) + " (" + TArgs[i]->getAsUnquotedString() |
| 180 | + ") of subclass '" + SC->getNameInitAsString() + "'!"); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | // Since everything went well, we can now set the "superclass" list for the |
| 185 | // current record. |
| 186 | const std::vector<Record*> &SCs = SC->getSuperClasses(); |
| 187 | for (unsigned i = 0, e = SCs.size(); i != e; ++i) { |
| 188 | if (CurRec->isSubClassOf(SCs[i])) |
| 189 | return Error(SubClass.RefLoc, |
| 190 | "Already subclass of '" + SCs[i]->getName() + "'!\n"); |
| 191 | CurRec->addSuperClass(SCs[i]); |
| 192 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 193 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 194 | if (CurRec->isSubClassOf(SC)) |
| 195 | return Error(SubClass.RefLoc, |
| 196 | "Already subclass of '" + SC->getName() + "'!\n"); |
| 197 | CurRec->addSuperClass(SC); |
| 198 | return false; |
| 199 | } |
| 200 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 201 | /// AddSubMultiClass - Add SubMultiClass as a subclass to |
Bob Wilson | 440548d | 2009-04-30 18:26:19 +0000 | [diff] [blame] | 202 | /// CurMC, resolving its template args as SubMultiClass's |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 203 | /// template arguments. |
Bob Wilson | 440548d | 2009-04-30 18:26:19 +0000 | [diff] [blame] | 204 | bool TGParser::AddSubMultiClass(MultiClass *CurMC, |
Bob Wilson | 1d512df | 2009-04-30 17:46:20 +0000 | [diff] [blame] | 205 | SubMultiClassReference &SubMultiClass) { |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 206 | MultiClass *SMC = SubMultiClass.MC; |
Bob Wilson | 440548d | 2009-04-30 18:26:19 +0000 | [diff] [blame] | 207 | Record *CurRec = &CurMC->Rec; |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 208 | |
Bob Wilson | 440548d | 2009-04-30 18:26:19 +0000 | [diff] [blame] | 209 | const std::vector<RecordVal> &MCVals = CurRec->getValues(); |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 210 | |
| 211 | // Add all of the values in the subclass into the current class. |
| 212 | const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues(); |
| 213 | for (unsigned i = 0, e = SMCVals.size(); i != e; ++i) |
| 214 | if (AddValue(CurRec, SubMultiClass.RefLoc, SMCVals[i])) |
| 215 | return true; |
| 216 | |
Bob Wilson | 440548d | 2009-04-30 18:26:19 +0000 | [diff] [blame] | 217 | int newDefStart = CurMC->DefPrototypes.size(); |
David Greene | d34a73b | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 218 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 219 | // Add all of the defs in the subclass into the current multiclass. |
| 220 | for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(), |
| 221 | iend = SMC->DefPrototypes.end(); |
| 222 | i != iend; |
| 223 | ++i) { |
| 224 | // Clone the def and add it to the current multiclass |
| 225 | Record *NewDef = new Record(**i); |
| 226 | |
| 227 | // Add all of the values in the superclass into the current def. |
| 228 | for (unsigned i = 0, e = MCVals.size(); i != e; ++i) |
| 229 | if (AddValue(NewDef, SubMultiClass.RefLoc, MCVals[i])) |
| 230 | return true; |
| 231 | |
Bob Wilson | 440548d | 2009-04-30 18:26:19 +0000 | [diff] [blame] | 232 | CurMC->DefPrototypes.push_back(NewDef); |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 233 | } |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 234 | |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 235 | const std::vector<Init *> &SMCTArgs = SMC->Rec.getTemplateArgs(); |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 236 | |
David Greene | d34a73b | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 237 | // Ensure that an appropriate number of template arguments are |
| 238 | // specified. |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 239 | if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size()) |
David Greene | d34a73b | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 240 | return Error(SubMultiClass.RefLoc, |
| 241 | "More template args specified than expected"); |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 242 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 243 | // Loop over all of the template arguments, setting them to the specified |
| 244 | // value or leaving them as the default if necessary. |
| 245 | for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) { |
| 246 | if (i < SubMultiClass.TemplateArgs.size()) { |
David Greene | d34a73b | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 247 | // If a value is specified for this template arg, set it in the |
| 248 | // superclass now. |
| 249 | if (SetValue(CurRec, SubMultiClass.RefLoc, SMCTArgs[i], |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 250 | std::vector<unsigned>(), |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 251 | SubMultiClass.TemplateArgs[i])) |
| 252 | return true; |
| 253 | |
| 254 | // Resolve it next. |
| 255 | CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i])); |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 256 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 257 | // Now remove it. |
| 258 | CurRec->removeValue(SMCTArgs[i]); |
| 259 | |
David Greene | d34a73b | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 260 | // If a value is specified for this template arg, set it in the |
| 261 | // new defs now. |
| 262 | for (MultiClass::RecordVector::iterator j = |
Bob Wilson | 440548d | 2009-04-30 18:26:19 +0000 | [diff] [blame] | 263 | CurMC->DefPrototypes.begin() + newDefStart, |
| 264 | jend = CurMC->DefPrototypes.end(); |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 265 | j != jend; |
| 266 | ++j) { |
| 267 | Record *Def = *j; |
| 268 | |
David Greene | d34a73b | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 269 | if (SetValue(Def, SubMultiClass.RefLoc, SMCTArgs[i], |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 270 | std::vector<unsigned>(), |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 271 | SubMultiClass.TemplateArgs[i])) |
| 272 | return true; |
| 273 | |
| 274 | // Resolve it next. |
| 275 | Def->resolveReferencesTo(Def->getValue(SMCTArgs[i])); |
| 276 | |
| 277 | // Now remove it |
| 278 | Def->removeValue(SMCTArgs[i]); |
| 279 | } |
| 280 | } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) { |
David Greene | d34a73b | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 281 | return Error(SubMultiClass.RefLoc, |
| 282 | "Value not specified for template argument #" |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 283 | + utostr(i) + " (" + SMCTArgs[i]->getAsUnquotedString() |
| 284 | + ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!"); |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | return false; |
| 289 | } |
| 290 | |
David Greene | cebb4ee | 2012-02-22 16:09:41 +0000 | [diff] [blame] | 291 | /// ProcessForeachDefs - Given a record, apply all of the variable |
| 292 | /// values in all surrounding foreach loops, creating new records for |
| 293 | /// each combination of values. |
Jakob Stoklund Olesen | 8e5286e | 2012-05-24 22:17:33 +0000 | [diff] [blame] | 294 | bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) { |
| 295 | if (Loops.empty()) |
| 296 | return false; |
| 297 | |
David Greene | cebb4ee | 2012-02-22 16:09:41 +0000 | [diff] [blame] | 298 | // We want to instantiate a new copy of CurRec for each combination |
| 299 | // of nested loop iterator values. We don't want top instantiate |
| 300 | // any copies until we have values for each loop iterator. |
| 301 | IterSet IterVals; |
Jakob Stoklund Olesen | 8e5286e | 2012-05-24 22:17:33 +0000 | [diff] [blame] | 302 | return ProcessForeachDefs(CurRec, Loc, IterVals); |
David Greene | cebb4ee | 2012-02-22 16:09:41 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /// ProcessForeachDefs - Given a record, a loop and a loop iterator, |
| 306 | /// apply each of the variable values in this loop and then process |
| 307 | /// subloops. |
Jakob Stoklund Olesen | 8e5286e | 2012-05-24 22:17:33 +0000 | [diff] [blame] | 308 | bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){ |
| 309 | // Recursively build a tuple of iterator values. |
| 310 | if (IterVals.size() != Loops.size()) { |
| 311 | assert(IterVals.size() < Loops.size()); |
| 312 | ForeachLoop &CurLoop = Loops[IterVals.size()]; |
Sean Silva | 6cfc806 | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 313 | ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue); |
Jakob Stoklund Olesen | 8e5286e | 2012-05-24 22:17:33 +0000 | [diff] [blame] | 314 | if (List == 0) { |
| 315 | Error(Loc, "Loop list is not a list"); |
| 316 | return true; |
| 317 | } |
David Greene | cebb4ee | 2012-02-22 16:09:41 +0000 | [diff] [blame] | 318 | |
Jakob Stoklund Olesen | 8e5286e | 2012-05-24 22:17:33 +0000 | [diff] [blame] | 319 | // Process each value. |
| 320 | for (int64_t i = 0; i < List->getSize(); ++i) { |
| 321 | Init *ItemVal = List->resolveListElementReference(*CurRec, 0, i); |
| 322 | IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal)); |
| 323 | if (ProcessForeachDefs(CurRec, Loc, IterVals)) |
| 324 | return true; |
| 325 | IterVals.pop_back(); |
| 326 | } |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | // This is the bottom of the recursion. We have all of the iterator values |
| 331 | // for this point in the iteration space. Instantiate a new record to |
| 332 | // reflect this combination of values. |
| 333 | Record *IterRec = new Record(*CurRec); |
| 334 | |
| 335 | // Set the iterator values now. |
| 336 | for (unsigned i = 0, e = IterVals.size(); i != e; ++i) { |
| 337 | VarInit *IterVar = IterVals[i].IterVar; |
Sean Silva | 6cfc806 | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 338 | TypedInit *IVal = dyn_cast<TypedInit>(IterVals[i].IterValue); |
Jakob Stoklund Olesen | 8e5286e | 2012-05-24 22:17:33 +0000 | [diff] [blame] | 339 | if (IVal == 0) { |
| 340 | Error(Loc, "foreach iterator value is untyped"); |
| 341 | return true; |
| 342 | } |
| 343 | |
| 344 | IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false)); |
| 345 | |
| 346 | if (SetValue(IterRec, Loc, IterVar->getName(), |
| 347 | std::vector<unsigned>(), IVal)) { |
| 348 | Error(Loc, "when instantiating this def"); |
| 349 | return true; |
| 350 | } |
| 351 | |
| 352 | // Resolve it next. |
| 353 | IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName())); |
| 354 | |
| 355 | // Remove it. |
| 356 | IterRec->removeValue(IterVar->getName()); |
| 357 | } |
| 358 | |
| 359 | if (Records.getDef(IterRec->getNameInitAsString())) { |
| 360 | Error(Loc, "def already exists: " + IterRec->getNameInitAsString()); |
David Greene | cebb4ee | 2012-02-22 16:09:41 +0000 | [diff] [blame] | 361 | return true; |
| 362 | } |
| 363 | |
Jakob Stoklund Olesen | 8e5286e | 2012-05-24 22:17:33 +0000 | [diff] [blame] | 364 | Records.addDef(IterRec); |
| 365 | IterRec->resolveReferences(); |
David Greene | cebb4ee | 2012-02-22 16:09:41 +0000 | [diff] [blame] | 366 | return false; |
| 367 | } |
| 368 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 369 | //===----------------------------------------------------------------------===// |
| 370 | // Parser Code |
| 371 | //===----------------------------------------------------------------------===// |
| 372 | |
| 373 | /// isObjectStart - Return true if this is a valid first token for an Object. |
| 374 | static bool isObjectStart(tgtok::TokKind K) { |
| 375 | return K == tgtok::Class || K == tgtok::Def || |
David Greene | cebb4ee | 2012-02-22 16:09:41 +0000 | [diff] [blame] | 376 | K == tgtok::Defm || K == tgtok::Let || |
| 377 | K == tgtok::MultiClass || K == tgtok::Foreach; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Chris Lattner | df72eae | 2010-10-05 22:51:56 +0000 | [diff] [blame] | 380 | static std::string GetNewAnonymousName() { |
| 381 | static unsigned AnonCounter = 0; |
| 382 | return "anonymous."+utostr(AnonCounter++); |
| 383 | } |
| 384 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 385 | /// ParseObjectName - If an object name is specified, return it. Otherwise, |
| 386 | /// return an anonymous name. |
David Greene | a9e07dd | 2011-10-19 13:04:29 +0000 | [diff] [blame] | 387 | /// ObjectName ::= Value [ '#' Value ]* |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 388 | /// ObjectName ::= /*empty*/ |
| 389 | /// |
David Greene | a9e07dd | 2011-10-19 13:04:29 +0000 | [diff] [blame] | 390 | Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) { |
| 391 | switch (Lex.getCode()) { |
| 392 | case tgtok::colon: |
| 393 | case tgtok::semi: |
| 394 | case tgtok::l_brace: |
| 395 | // These are all of the tokens that can begin an object body. |
| 396 | // Some of these can also begin values but we disallow those cases |
| 397 | // because they are unlikely to be useful. |
| 398 | return StringInit::get(GetNewAnonymousName()); |
David Greene | a9e07dd | 2011-10-19 13:04:29 +0000 | [diff] [blame] | 399 | default: |
| 400 | break; |
| 401 | } |
Mikhail Glushenkov | c761f7d | 2010-10-23 07:32:37 +0000 | [diff] [blame] | 402 | |
David Greene | a9e07dd | 2011-10-19 13:04:29 +0000 | [diff] [blame] | 403 | Record *CurRec = 0; |
| 404 | if (CurMultiClass) |
| 405 | CurRec = &CurMultiClass->Rec; |
| 406 | |
| 407 | RecTy *Type = 0; |
| 408 | if (CurRec) { |
Sean Silva | 3f7b7f8 | 2012-10-10 20:24:47 +0000 | [diff] [blame] | 409 | const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit()); |
David Greene | a9e07dd | 2011-10-19 13:04:29 +0000 | [diff] [blame] | 410 | if (!CurRecName) { |
| 411 | TokError("Record name is not typed!"); |
| 412 | return 0; |
| 413 | } |
| 414 | Type = CurRecName->getType(); |
| 415 | } |
| 416 | |
| 417 | return ParseValue(CurRec, Type, ParseNameMode); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 420 | /// ParseClassID - Parse and resolve a reference to a class name. This returns |
| 421 | /// null on error. |
| 422 | /// |
| 423 | /// ClassID ::= ID |
| 424 | /// |
| 425 | Record *TGParser::ParseClassID() { |
| 426 | if (Lex.getCode() != tgtok::Id) { |
| 427 | TokError("expected name for ClassID"); |
| 428 | return 0; |
| 429 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 430 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 431 | Record *Result = Records.getClass(Lex.getCurStrVal()); |
| 432 | if (Result == 0) |
| 433 | TokError("Couldn't find class '" + Lex.getCurStrVal() + "'"); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 434 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 435 | Lex.Lex(); |
| 436 | return Result; |
| 437 | } |
| 438 | |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 439 | /// ParseMultiClassID - Parse and resolve a reference to a multiclass name. |
| 440 | /// This returns null on error. |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 441 | /// |
| 442 | /// MultiClassID ::= ID |
| 443 | /// |
| 444 | MultiClass *TGParser::ParseMultiClassID() { |
| 445 | if (Lex.getCode() != tgtok::Id) { |
Sean Silva | 36febfd | 2013-01-09 02:11:57 +0000 | [diff] [blame] | 446 | TokError("expected name for MultiClassID"); |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 447 | return 0; |
| 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 | MultiClass *Result = MultiClasses[Lex.getCurStrVal()]; |
| 451 | if (Result == 0) |
Sean Silva | 36febfd | 2013-01-09 02:11:57 +0000 | [diff] [blame] | 452 | TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'"); |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 453 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 454 | Lex.Lex(); |
| 455 | return Result; |
| 456 | } |
| 457 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 458 | /// ParseSubClassReference - Parse a reference to a subclass or to a templated |
| 459 | /// subclass. This returns a SubClassRefTy with a null Record* on error. |
| 460 | /// |
| 461 | /// SubClassRef ::= ClassID |
| 462 | /// SubClassRef ::= ClassID '<' ValueList '>' |
| 463 | /// |
| 464 | SubClassReference TGParser:: |
| 465 | ParseSubClassReference(Record *CurRec, bool isDefm) { |
| 466 | SubClassReference Result; |
| 467 | Result.RefLoc = Lex.getLoc(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 468 | |
Sean Silva | 7be9021 | 2013-01-09 02:17:14 +0000 | [diff] [blame] | 469 | if (isDefm) { |
| 470 | if (MultiClass *MC = ParseMultiClassID()) |
| 471 | Result.Rec = &MC->Rec; |
| 472 | } else { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 473 | Result.Rec = ParseClassID(); |
Sean Silva | 7be9021 | 2013-01-09 02:17:14 +0000 | [diff] [blame] | 474 | } |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 475 | if (Result.Rec == 0) return Result; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 476 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 477 | // If there is no template arg list, we're done. |
| 478 | if (Lex.getCode() != tgtok::less) |
| 479 | return Result; |
| 480 | Lex.Lex(); // Eat the '<' |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 481 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 482 | if (Lex.getCode() == tgtok::greater) { |
| 483 | TokError("subclass reference requires a non-empty list of template values"); |
| 484 | Result.Rec = 0; |
| 485 | return Result; |
| 486 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 487 | |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 488 | Result.TemplateArgs = ParseValueList(CurRec, Result.Rec); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 489 | if (Result.TemplateArgs.empty()) { |
| 490 | Result.Rec = 0; // Error parsing value list. |
| 491 | return Result; |
| 492 | } |
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 | if (Lex.getCode() != tgtok::greater) { |
| 495 | TokError("expected '>' in template value list"); |
| 496 | Result.Rec = 0; |
| 497 | return Result; |
| 498 | } |
| 499 | Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 500 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 501 | return Result; |
| 502 | } |
| 503 | |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 504 | /// ParseSubMultiClassReference - Parse a reference to a subclass or to a |
| 505 | /// templated submulticlass. This returns a SubMultiClassRefTy with a null |
| 506 | /// Record* on error. |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 507 | /// |
| 508 | /// SubMultiClassRef ::= MultiClassID |
| 509 | /// SubMultiClassRef ::= MultiClassID '<' ValueList '>' |
| 510 | /// |
| 511 | SubMultiClassReference TGParser:: |
| 512 | ParseSubMultiClassReference(MultiClass *CurMC) { |
| 513 | SubMultiClassReference Result; |
| 514 | Result.RefLoc = Lex.getLoc(); |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 515 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 516 | Result.MC = ParseMultiClassID(); |
| 517 | if (Result.MC == 0) return Result; |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 518 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 519 | // If there is no template arg list, we're done. |
| 520 | if (Lex.getCode() != tgtok::less) |
| 521 | return Result; |
| 522 | Lex.Lex(); // Eat the '<' |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 523 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 524 | if (Lex.getCode() == tgtok::greater) { |
| 525 | TokError("subclass reference requires a non-empty list of template values"); |
| 526 | Result.MC = 0; |
| 527 | return Result; |
| 528 | } |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 529 | |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 530 | Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec); |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 531 | if (Result.TemplateArgs.empty()) { |
| 532 | Result.MC = 0; // Error parsing value list. |
| 533 | return Result; |
| 534 | } |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 535 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 536 | if (Lex.getCode() != tgtok::greater) { |
| 537 | TokError("expected '>' in template value list"); |
| 538 | Result.MC = 0; |
| 539 | return Result; |
| 540 | } |
| 541 | Lex.Lex(); |
| 542 | |
| 543 | return Result; |
| 544 | } |
| 545 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 546 | /// ParseRangePiece - Parse a bit/value range. |
| 547 | /// RangePiece ::= INTVAL |
| 548 | /// RangePiece ::= INTVAL '-' INTVAL |
| 549 | /// RangePiece ::= INTVAL INTVAL |
| 550 | bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) { |
Chris Lattner | 811281e | 2008-01-10 07:01:53 +0000 | [diff] [blame] | 551 | if (Lex.getCode() != tgtok::IntVal) { |
| 552 | TokError("expected integer or bitrange"); |
| 553 | return true; |
| 554 | } |
Dan Gohman | 63f9720 | 2008-10-17 01:33:43 +0000 | [diff] [blame] | 555 | int64_t Start = Lex.getCurIntVal(); |
| 556 | int64_t End; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 557 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 558 | if (Start < 0) |
| 559 | return TokError("invalid range, cannot be negative"); |
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 | switch (Lex.Lex()) { // eat first character. |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 562 | default: |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 563 | Ranges.push_back(Start); |
| 564 | return false; |
| 565 | case tgtok::minus: |
| 566 | if (Lex.Lex() != tgtok::IntVal) { |
| 567 | TokError("expected integer value as end of range"); |
| 568 | return true; |
| 569 | } |
| 570 | End = Lex.getCurIntVal(); |
| 571 | break; |
| 572 | case tgtok::IntVal: |
| 573 | End = -Lex.getCurIntVal(); |
| 574 | break; |
| 575 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 576 | if (End < 0) |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 577 | return TokError("invalid range, cannot be negative"); |
| 578 | Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 579 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 580 | // Add to the range. |
| 581 | if (Start < End) { |
| 582 | for (; Start <= End; ++Start) |
| 583 | Ranges.push_back(Start); |
| 584 | } else { |
| 585 | for (; Start >= End; --Start) |
| 586 | Ranges.push_back(Start); |
| 587 | } |
| 588 | return false; |
| 589 | } |
| 590 | |
| 591 | /// ParseRangeList - Parse a list of scalars and ranges into scalar values. |
| 592 | /// |
| 593 | /// RangeList ::= RangePiece (',' RangePiece)* |
| 594 | /// |
| 595 | std::vector<unsigned> TGParser::ParseRangeList() { |
| 596 | std::vector<unsigned> Result; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 597 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 598 | // Parse the first piece. |
| 599 | if (ParseRangePiece(Result)) |
| 600 | return std::vector<unsigned>(); |
| 601 | while (Lex.getCode() == tgtok::comma) { |
| 602 | Lex.Lex(); // Eat the comma. |
| 603 | |
| 604 | // Parse the next range piece. |
| 605 | if (ParseRangePiece(Result)) |
| 606 | return std::vector<unsigned>(); |
| 607 | } |
| 608 | return Result; |
| 609 | } |
| 610 | |
| 611 | /// ParseOptionalRangeList - Parse either a range list in <>'s or nothing. |
| 612 | /// OptionalRangeList ::= '<' RangeList '>' |
| 613 | /// OptionalRangeList ::= /*empty*/ |
| 614 | bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) { |
| 615 | if (Lex.getCode() != tgtok::less) |
| 616 | return false; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 617 | |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 618 | SMLoc StartLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 619 | Lex.Lex(); // eat the '<' |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 620 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 621 | // Parse the range list. |
| 622 | Ranges = ParseRangeList(); |
| 623 | if (Ranges.empty()) return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 624 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 625 | if (Lex.getCode() != tgtok::greater) { |
| 626 | TokError("expected '>' at end of range list"); |
| 627 | return Error(StartLoc, "to match this '<'"); |
| 628 | } |
| 629 | Lex.Lex(); // eat the '>'. |
| 630 | return false; |
| 631 | } |
| 632 | |
| 633 | /// ParseOptionalBitList - Parse either a bit list in {}'s or nothing. |
| 634 | /// OptionalBitList ::= '{' RangeList '}' |
| 635 | /// OptionalBitList ::= /*empty*/ |
| 636 | bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) { |
| 637 | if (Lex.getCode() != tgtok::l_brace) |
| 638 | return false; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 639 | |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 640 | SMLoc StartLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 641 | Lex.Lex(); // eat the '{' |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 642 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 643 | // Parse the range list. |
| 644 | Ranges = ParseRangeList(); |
| 645 | if (Ranges.empty()) return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 646 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 647 | if (Lex.getCode() != tgtok::r_brace) { |
| 648 | TokError("expected '}' at end of bit list"); |
| 649 | return Error(StartLoc, "to match this '{'"); |
| 650 | } |
| 651 | Lex.Lex(); // eat the '}'. |
| 652 | return false; |
| 653 | } |
| 654 | |
| 655 | |
| 656 | /// ParseType - Parse and return a tblgen type. This returns null on error. |
| 657 | /// |
| 658 | /// Type ::= STRING // string type |
Jakob Stoklund Olesen | 8dd6f0c | 2012-01-13 03:38:34 +0000 | [diff] [blame] | 659 | /// Type ::= CODE // code type |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 660 | /// Type ::= BIT // bit type |
| 661 | /// Type ::= BITS '<' INTVAL '>' // bits<x> type |
| 662 | /// Type ::= INT // int type |
| 663 | /// Type ::= LIST '<' Type '>' // list<x> type |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 664 | /// Type ::= DAG // dag type |
| 665 | /// Type ::= ClassID // Record Type |
| 666 | /// |
| 667 | RecTy *TGParser::ParseType() { |
| 668 | switch (Lex.getCode()) { |
| 669 | default: TokError("Unknown token when expecting a type"); return 0; |
Jakob Stoklund Olesen | 77f8274 | 2011-07-18 17:02:57 +0000 | [diff] [blame] | 670 | case tgtok::String: Lex.Lex(); return StringRecTy::get(); |
Jakob Stoklund Olesen | 8dd6f0c | 2012-01-13 03:38:34 +0000 | [diff] [blame] | 671 | case tgtok::Code: Lex.Lex(); return StringRecTy::get(); |
Jakob Stoklund Olesen | 77f8274 | 2011-07-18 17:02:57 +0000 | [diff] [blame] | 672 | case tgtok::Bit: Lex.Lex(); return BitRecTy::get(); |
| 673 | case tgtok::Int: Lex.Lex(); return IntRecTy::get(); |
Jakob Stoklund Olesen | 77f8274 | 2011-07-18 17:02:57 +0000 | [diff] [blame] | 674 | case tgtok::Dag: Lex.Lex(); return DagRecTy::get(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 675 | case tgtok::Id: |
Jakob Stoklund Olesen | 77f8274 | 2011-07-18 17:02:57 +0000 | [diff] [blame] | 676 | if (Record *R = ParseClassID()) return RecordRecTy::get(R); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 677 | return 0; |
| 678 | case tgtok::Bits: { |
| 679 | if (Lex.Lex() != tgtok::less) { // Eat 'bits' |
| 680 | TokError("expected '<' after bits type"); |
| 681 | return 0; |
| 682 | } |
| 683 | if (Lex.Lex() != tgtok::IntVal) { // Eat '<' |
| 684 | TokError("expected integer in bits<n> type"); |
| 685 | return 0; |
| 686 | } |
Dan Gohman | 63f9720 | 2008-10-17 01:33:43 +0000 | [diff] [blame] | 687 | uint64_t Val = Lex.getCurIntVal(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 688 | if (Lex.Lex() != tgtok::greater) { // Eat count. |
| 689 | TokError("expected '>' at end of bits<n> type"); |
| 690 | return 0; |
| 691 | } |
| 692 | Lex.Lex(); // Eat '>' |
Jakob Stoklund Olesen | 77f8274 | 2011-07-18 17:02:57 +0000 | [diff] [blame] | 693 | return BitsRecTy::get(Val); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 694 | } |
| 695 | case tgtok::List: { |
| 696 | if (Lex.Lex() != tgtok::less) { // Eat 'bits' |
| 697 | TokError("expected '<' after list type"); |
| 698 | return 0; |
| 699 | } |
| 700 | Lex.Lex(); // Eat '<' |
| 701 | RecTy *SubType = ParseType(); |
| 702 | if (SubType == 0) return 0; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 703 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 704 | if (Lex.getCode() != tgtok::greater) { |
| 705 | TokError("expected '>' at end of list<ty> type"); |
| 706 | return 0; |
| 707 | } |
| 708 | Lex.Lex(); // Eat '>' |
Jakob Stoklund Olesen | 77f8274 | 2011-07-18 17:02:57 +0000 | [diff] [blame] | 709 | return ListRecTy::get(SubType); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 710 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 711 | } |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | /// ParseIDValue - Parse an ID as a value and decode what it means. |
| 715 | /// |
| 716 | /// IDValue ::= ID [def local value] |
| 717 | /// IDValue ::= ID [def template arg] |
| 718 | /// IDValue ::= ID [multiclass local value] |
| 719 | /// IDValue ::= ID [multiclass template argument] |
| 720 | /// IDValue ::= ID [def name] |
| 721 | /// |
David Greene | f3744a0 | 2011-10-19 13:04:20 +0000 | [diff] [blame] | 722 | Init *TGParser::ParseIDValue(Record *CurRec, IDParseMode Mode) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 723 | assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue"); |
| 724 | std::string Name = Lex.getCurStrVal(); |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 725 | SMLoc Loc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 726 | Lex.Lex(); |
| 727 | return ParseIDValue(CurRec, Name, Loc); |
| 728 | } |
| 729 | |
| 730 | /// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID |
| 731 | /// has already been read. |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 732 | Init *TGParser::ParseIDValue(Record *CurRec, |
David Greene | f3744a0 | 2011-10-19 13:04:20 +0000 | [diff] [blame] | 733 | const std::string &Name, SMLoc NameLoc, |
| 734 | IDParseMode Mode) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 735 | if (CurRec) { |
| 736 | if (const RecordVal *RV = CurRec->getValue(Name)) |
David Greene | dcd35c7 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 737 | return VarInit::get(Name, RV->getType()); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 738 | |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 739 | Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":"); |
| 740 | |
David Greene | caa25c8 | 2011-10-05 22:42:54 +0000 | [diff] [blame] | 741 | if (CurMultiClass) |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 742 | TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name, |
| 743 | "::"); |
David Greene | caa25c8 | 2011-10-05 22:42:54 +0000 | [diff] [blame] | 744 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 745 | if (CurRec->isTemplateArg(TemplateArgName)) { |
| 746 | const RecordVal *RV = CurRec->getValue(TemplateArgName); |
| 747 | assert(RV && "Template arg doesn't exist??"); |
David Greene | dcd35c7 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 748 | return VarInit::get(TemplateArgName, RV->getType()); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 749 | } |
| 750 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 751 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 752 | if (CurMultiClass) { |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 753 | Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name, |
| 754 | "::"); |
| 755 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 756 | if (CurMultiClass->Rec.isTemplateArg(MCName)) { |
| 757 | const RecordVal *RV = CurMultiClass->Rec.getValue(MCName); |
| 758 | assert(RV && "Template arg doesn't exist??"); |
David Greene | dcd35c7 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 759 | return VarInit::get(MCName, RV->getType()); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 760 | } |
| 761 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 762 | |
David Greene | cebb4ee | 2012-02-22 16:09:41 +0000 | [diff] [blame] | 763 | // If this is in a foreach loop, make sure it's not a loop iterator |
| 764 | for (LoopVector::iterator i = Loops.begin(), iend = Loops.end(); |
| 765 | i != iend; |
| 766 | ++i) { |
Sean Silva | 6cfc806 | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 767 | VarInit *IterVar = dyn_cast<VarInit>(i->IterVar); |
David Greene | cebb4ee | 2012-02-22 16:09:41 +0000 | [diff] [blame] | 768 | if (IterVar && IterVar->getName() == Name) |
| 769 | return IterVar; |
| 770 | } |
| 771 | |
David Greene | bbec279 | 2011-10-19 13:04:21 +0000 | [diff] [blame] | 772 | if (Mode == ParseNameMode) |
| 773 | return StringInit::get(Name); |
| 774 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 775 | if (Record *D = Records.getDef(Name)) |
Jakob Stoklund Olesen | 77f8274 | 2011-07-18 17:02:57 +0000 | [diff] [blame] | 776 | return DefInit::get(D); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 777 | |
David Greene | bbec279 | 2011-10-19 13:04:21 +0000 | [diff] [blame] | 778 | if (Mode == ParseValueMode) { |
| 779 | Error(NameLoc, "Variable not defined: '" + Name + "'"); |
| 780 | return 0; |
| 781 | } |
| 782 | |
| 783 | return StringInit::get(Name); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 784 | } |
| 785 | |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 786 | /// ParseOperation - Parse an operator. This returns null on error. |
| 787 | /// |
| 788 | /// Operation ::= XOperator ['<' Type '>'] '(' Args ')' |
| 789 | /// |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 790 | Init *TGParser::ParseOperation(Record *CurRec) { |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 791 | switch (Lex.getCode()) { |
| 792 | default: |
| 793 | TokError("unknown operation"); |
| 794 | return 0; |
David Greene | 1434f66 | 2011-01-07 17:05:37 +0000 | [diff] [blame] | 795 | case tgtok::XHead: |
| 796 | case tgtok::XTail: |
| 797 | case tgtok::XEmpty: |
David Greene | e6c27de | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 798 | case tgtok::XCast: { // Value ::= !unop '(' Value ')' |
| 799 | UnOpInit::UnaryOp Code; |
| 800 | RecTy *Type = 0; |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 801 | |
David Greene | e6c27de | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 802 | switch (Lex.getCode()) { |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 803 | default: llvm_unreachable("Unhandled code!"); |
David Greene | e6c27de | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 804 | case tgtok::XCast: |
| 805 | Lex.Lex(); // eat the operation |
| 806 | Code = UnOpInit::CAST; |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 807 | |
David Greene | e6c27de | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 808 | Type = ParseOperatorType(); |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 809 | |
David Greene | e6c27de | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 810 | if (Type == 0) { |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 811 | TokError("did not get type for unary operator"); |
David Greene | e6c27de | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 812 | return 0; |
| 813 | } |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 814 | |
David Greene | e6c27de | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 815 | break; |
David Greene | 1434f66 | 2011-01-07 17:05:37 +0000 | [diff] [blame] | 816 | case tgtok::XHead: |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 817 | Lex.Lex(); // eat the operation |
David Greene | 1434f66 | 2011-01-07 17:05:37 +0000 | [diff] [blame] | 818 | Code = UnOpInit::HEAD; |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 819 | break; |
David Greene | 1434f66 | 2011-01-07 17:05:37 +0000 | [diff] [blame] | 820 | case tgtok::XTail: |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 821 | Lex.Lex(); // eat the operation |
David Greene | 1434f66 | 2011-01-07 17:05:37 +0000 | [diff] [blame] | 822 | Code = UnOpInit::TAIL; |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 823 | break; |
David Greene | 1434f66 | 2011-01-07 17:05:37 +0000 | [diff] [blame] | 824 | case tgtok::XEmpty: |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 825 | Lex.Lex(); // eat the operation |
David Greene | 1434f66 | 2011-01-07 17:05:37 +0000 | [diff] [blame] | 826 | Code = UnOpInit::EMPTY; |
Jakob Stoklund Olesen | 77f8274 | 2011-07-18 17:02:57 +0000 | [diff] [blame] | 827 | Type = IntRecTy::get(); |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 828 | break; |
David Greene | e6c27de | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 829 | } |
| 830 | if (Lex.getCode() != tgtok::l_paren) { |
| 831 | TokError("expected '(' after unary operator"); |
| 832 | return 0; |
| 833 | } |
| 834 | Lex.Lex(); // eat the '(' |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 835 | |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 836 | Init *LHS = ParseValue(CurRec); |
David Greene | e6c27de | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 837 | if (LHS == 0) return 0; |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 838 | |
David Greene | 1434f66 | 2011-01-07 17:05:37 +0000 | [diff] [blame] | 839 | if (Code == UnOpInit::HEAD |
| 840 | || Code == UnOpInit::TAIL |
| 841 | || Code == UnOpInit::EMPTY) { |
Sean Silva | 6cfc806 | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 842 | ListInit *LHSl = dyn_cast<ListInit>(LHS); |
| 843 | StringInit *LHSs = dyn_cast<StringInit>(LHS); |
| 844 | TypedInit *LHSt = dyn_cast<TypedInit>(LHS); |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 845 | if (LHSl == 0 && LHSs == 0 && LHSt == 0) { |
| 846 | TokError("expected list or string type argument in unary operator"); |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 847 | return 0; |
| 848 | } |
| 849 | if (LHSt) { |
Sean Silva | 736ceac | 2012-10-05 03:31:58 +0000 | [diff] [blame] | 850 | ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType()); |
| 851 | StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType()); |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 852 | if (LType == 0 && SType == 0) { |
| 853 | TokError("expected list or string type argumnet in unary operator"); |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 854 | return 0; |
| 855 | } |
| 856 | } |
| 857 | |
David Greene | 1434f66 | 2011-01-07 17:05:37 +0000 | [diff] [blame] | 858 | if (Code == UnOpInit::HEAD |
| 859 | || Code == UnOpInit::TAIL) { |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 860 | if (LHSl == 0 && LHSt == 0) { |
| 861 | TokError("expected list type argumnet in unary operator"); |
| 862 | return 0; |
| 863 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 864 | |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 865 | if (LHSl && LHSl->getSize() == 0) { |
| 866 | TokError("empty list argument in unary operator"); |
| 867 | return 0; |
| 868 | } |
| 869 | if (LHSl) { |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 870 | Init *Item = LHSl->getElement(0); |
Sean Silva | 6cfc806 | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 871 | TypedInit *Itemt = dyn_cast<TypedInit>(Item); |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 872 | if (Itemt == 0) { |
| 873 | TokError("untyped list element in unary operator"); |
| 874 | return 0; |
| 875 | } |
David Greene | 1434f66 | 2011-01-07 17:05:37 +0000 | [diff] [blame] | 876 | if (Code == UnOpInit::HEAD) { |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 877 | Type = Itemt->getType(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 878 | } else { |
Jakob Stoklund Olesen | 77f8274 | 2011-07-18 17:02:57 +0000 | [diff] [blame] | 879 | Type = ListRecTy::get(Itemt->getType()); |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 880 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 881 | } else { |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 882 | assert(LHSt && "expected list type argument in unary operator"); |
Sean Silva | 736ceac | 2012-10-05 03:31:58 +0000 | [diff] [blame] | 883 | ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType()); |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 884 | if (LType == 0) { |
| 885 | TokError("expected list type argumnet in unary operator"); |
| 886 | return 0; |
| 887 | } |
David Greene | 1434f66 | 2011-01-07 17:05:37 +0000 | [diff] [blame] | 888 | if (Code == UnOpInit::HEAD) { |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 889 | Type = LType->getElementType(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 890 | } else { |
David Greene | 5f9f9ba | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 891 | Type = LType; |
| 892 | } |
| 893 | } |
| 894 | } |
| 895 | } |
| 896 | |
David Greene | e6c27de | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 897 | if (Lex.getCode() != tgtok::r_paren) { |
| 898 | TokError("expected ')' in unary operator"); |
| 899 | return 0; |
| 900 | } |
| 901 | Lex.Lex(); // eat the ')' |
David Greene | dcd35c7 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 902 | return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass); |
David Greene | e6c27de | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 903 | } |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 904 | |
| 905 | case tgtok::XConcat: |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 906 | case tgtok::XSRA: |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 907 | case tgtok::XSRL: |
| 908 | case tgtok::XSHL: |
David Greene | 6786d5e | 2010-01-05 19:11:42 +0000 | [diff] [blame] | 909 | case tgtok::XEq: |
Chris Lattner | c7252ce | 2010-10-06 00:19:21 +0000 | [diff] [blame] | 910 | case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')' |
Chris Lattner | 8d978a7 | 2010-10-05 23:58:18 +0000 | [diff] [blame] | 911 | tgtok::TokKind OpTok = Lex.getCode(); |
| 912 | SMLoc OpLoc = Lex.getLoc(); |
| 913 | Lex.Lex(); // eat the operation |
| 914 | |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 915 | BinOpInit::BinaryOp Code; |
| 916 | RecTy *Type = 0; |
| 917 | |
Chris Lattner | 8d978a7 | 2010-10-05 23:58:18 +0000 | [diff] [blame] | 918 | switch (OpTok) { |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 919 | default: llvm_unreachable("Unhandled code!"); |
Jakob Stoklund Olesen | 77f8274 | 2011-07-18 17:02:57 +0000 | [diff] [blame] | 920 | case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break; |
| 921 | case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break; |
| 922 | case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break; |
| 923 | case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break; |
| 924 | case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 925 | case tgtok::XStrConcat: |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 926 | Code = BinOpInit::STRCONCAT; |
Jakob Stoklund Olesen | 77f8274 | 2011-07-18 17:02:57 +0000 | [diff] [blame] | 927 | Type = StringRecTy::get(); |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 928 | break; |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 929 | } |
Mikhail Glushenkov | c761f7d | 2010-10-23 07:32:37 +0000 | [diff] [blame] | 930 | |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 931 | if (Lex.getCode() != tgtok::l_paren) { |
| 932 | TokError("expected '(' after binary operator"); |
| 933 | return 0; |
| 934 | } |
| 935 | Lex.Lex(); // eat the '(' |
| 936 | |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 937 | SmallVector<Init*, 2> InitList; |
Mikhail Glushenkov | c761f7d | 2010-10-23 07:32:37 +0000 | [diff] [blame] | 938 | |
Chris Lattner | 8d978a7 | 2010-10-05 23:58:18 +0000 | [diff] [blame] | 939 | InitList.push_back(ParseValue(CurRec)); |
| 940 | if (InitList.back() == 0) return 0; |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 941 | |
Chris Lattner | 8d978a7 | 2010-10-05 23:58:18 +0000 | [diff] [blame] | 942 | while (Lex.getCode() == tgtok::comma) { |
| 943 | Lex.Lex(); // eat the ',' |
| 944 | |
| 945 | InitList.push_back(ParseValue(CurRec)); |
| 946 | if (InitList.back() == 0) return 0; |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 947 | } |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 948 | |
| 949 | if (Lex.getCode() != tgtok::r_paren) { |
Chris Lattner | 8d978a7 | 2010-10-05 23:58:18 +0000 | [diff] [blame] | 950 | TokError("expected ')' in operator"); |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 951 | return 0; |
| 952 | } |
| 953 | Lex.Lex(); // eat the ')' |
Chris Lattner | 8d978a7 | 2010-10-05 23:58:18 +0000 | [diff] [blame] | 954 | |
| 955 | // We allow multiple operands to associative operators like !strconcat as |
| 956 | // shorthand for nesting them. |
| 957 | if (Code == BinOpInit::STRCONCAT) { |
| 958 | while (InitList.size() > 2) { |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 959 | Init *RHS = InitList.pop_back_val(); |
David Greene | dcd35c7 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 960 | RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type)) |
| 961 | ->Fold(CurRec, CurMultiClass); |
Chris Lattner | 8d978a7 | 2010-10-05 23:58:18 +0000 | [diff] [blame] | 962 | InitList.back() = RHS; |
| 963 | } |
| 964 | } |
Mikhail Glushenkov | c761f7d | 2010-10-23 07:32:37 +0000 | [diff] [blame] | 965 | |
Chris Lattner | 8d978a7 | 2010-10-05 23:58:18 +0000 | [diff] [blame] | 966 | if (InitList.size() == 2) |
David Greene | dcd35c7 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 967 | return (BinOpInit::get(Code, InitList[0], InitList[1], Type)) |
Chris Lattner | 8d978a7 | 2010-10-05 23:58:18 +0000 | [diff] [blame] | 968 | ->Fold(CurRec, CurMultiClass); |
Mikhail Glushenkov | c761f7d | 2010-10-23 07:32:37 +0000 | [diff] [blame] | 969 | |
Chris Lattner | 8d978a7 | 2010-10-05 23:58:18 +0000 | [diff] [blame] | 970 | Error(OpLoc, "expected two operands to operator"); |
| 971 | return 0; |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 972 | } |
| 973 | |
David Greene | 9bea7c8 | 2009-05-14 23:26:46 +0000 | [diff] [blame] | 974 | case tgtok::XIf: |
David Greene | beb31a5 | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 975 | case tgtok::XForEach: |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 976 | case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')' |
| 977 | TernOpInit::TernaryOp Code; |
| 978 | RecTy *Type = 0; |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 979 | |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 980 | tgtok::TokKind LexCode = Lex.getCode(); |
| 981 | Lex.Lex(); // eat the operation |
| 982 | switch (LexCode) { |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 983 | default: llvm_unreachable("Unhandled code!"); |
David Greene | 9bea7c8 | 2009-05-14 23:26:46 +0000 | [diff] [blame] | 984 | case tgtok::XIf: |
| 985 | Code = TernOpInit::IF; |
| 986 | break; |
David Greene | beb31a5 | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 987 | case tgtok::XForEach: |
| 988 | Code = TernOpInit::FOREACH; |
| 989 | break; |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 990 | case tgtok::XSubst: |
| 991 | Code = TernOpInit::SUBST; |
| 992 | break; |
| 993 | } |
| 994 | if (Lex.getCode() != tgtok::l_paren) { |
| 995 | TokError("expected '(' after ternary operator"); |
| 996 | return 0; |
| 997 | } |
| 998 | Lex.Lex(); // eat the '(' |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 999 | |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1000 | Init *LHS = ParseValue(CurRec); |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1001 | if (LHS == 0) return 0; |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 1002 | |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1003 | if (Lex.getCode() != tgtok::comma) { |
| 1004 | TokError("expected ',' in ternary operator"); |
| 1005 | return 0; |
| 1006 | } |
| 1007 | Lex.Lex(); // eat the ',' |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1008 | |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1009 | Init *MHS = ParseValue(CurRec); |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1010 | if (MHS == 0) return 0; |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 1011 | |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1012 | if (Lex.getCode() != tgtok::comma) { |
| 1013 | TokError("expected ',' in ternary operator"); |
| 1014 | return 0; |
| 1015 | } |
| 1016 | Lex.Lex(); // eat the ',' |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1017 | |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1018 | Init *RHS = ParseValue(CurRec); |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1019 | if (RHS == 0) return 0; |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 1020 | |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1021 | if (Lex.getCode() != tgtok::r_paren) { |
| 1022 | TokError("expected ')' in binary operator"); |
| 1023 | return 0; |
| 1024 | } |
| 1025 | Lex.Lex(); // eat the ')' |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 1026 | |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1027 | switch (LexCode) { |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 1028 | default: llvm_unreachable("Unhandled code!"); |
David Greene | 9bea7c8 | 2009-05-14 23:26:46 +0000 | [diff] [blame] | 1029 | case tgtok::XIf: { |
Bill Wendling | 548f5a0 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 1030 | RecTy *MHSTy = 0; |
| 1031 | RecTy *RHSTy = 0; |
| 1032 | |
Sean Silva | 6cfc806 | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 1033 | if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS)) |
Bill Wendling | 548f5a0 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 1034 | MHSTy = MHSt->getType(); |
Sean Silva | 6cfc806 | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 1035 | if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS)) |
Michael Liao | 307525c | 2012-09-06 23:32:48 +0000 | [diff] [blame] | 1036 | MHSTy = BitsRecTy::get(MHSbits->getNumBits()); |
Sean Silva | 3f7b7f8 | 2012-10-10 20:24:47 +0000 | [diff] [blame] | 1037 | if (isa<BitInit>(MHS)) |
Michael Liao | 307525c | 2012-09-06 23:32:48 +0000 | [diff] [blame] | 1038 | MHSTy = BitRecTy::get(); |
| 1039 | |
Sean Silva | 6cfc806 | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 1040 | if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS)) |
Bill Wendling | 548f5a0 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 1041 | RHSTy = RHSt->getType(); |
Sean Silva | 6cfc806 | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 1042 | if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS)) |
Michael Liao | 307525c | 2012-09-06 23:32:48 +0000 | [diff] [blame] | 1043 | RHSTy = BitsRecTy::get(RHSbits->getNumBits()); |
Sean Silva | 3f7b7f8 | 2012-10-10 20:24:47 +0000 | [diff] [blame] | 1044 | if (isa<BitInit>(RHS)) |
Michael Liao | 307525c | 2012-09-06 23:32:48 +0000 | [diff] [blame] | 1045 | RHSTy = BitRecTy::get(); |
| 1046 | |
| 1047 | // For UnsetInit, it's typed from the other hand. |
Sean Silva | 3f7b7f8 | 2012-10-10 20:24:47 +0000 | [diff] [blame] | 1048 | if (isa<UnsetInit>(MHS)) |
Michael Liao | 307525c | 2012-09-06 23:32:48 +0000 | [diff] [blame] | 1049 | MHSTy = RHSTy; |
Sean Silva | 3f7b7f8 | 2012-10-10 20:24:47 +0000 | [diff] [blame] | 1050 | if (isa<UnsetInit>(RHS)) |
Michael Liao | 307525c | 2012-09-06 23:32:48 +0000 | [diff] [blame] | 1051 | RHSTy = MHSTy; |
Bill Wendling | 548f5a0 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 1052 | |
| 1053 | if (!MHSTy || !RHSTy) { |
David Greene | 9bea7c8 | 2009-05-14 23:26:46 +0000 | [diff] [blame] | 1054 | TokError("could not get type for !if"); |
| 1055 | return 0; |
| 1056 | } |
Bill Wendling | 548f5a0 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 1057 | |
| 1058 | if (MHSTy->typeIsConvertibleTo(RHSTy)) { |
| 1059 | Type = RHSTy; |
| 1060 | } else if (RHSTy->typeIsConvertibleTo(MHSTy)) { |
| 1061 | Type = MHSTy; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1062 | } else { |
David Greene | 9bea7c8 | 2009-05-14 23:26:46 +0000 | [diff] [blame] | 1063 | TokError("inconsistent types for !if"); |
| 1064 | return 0; |
| 1065 | } |
| 1066 | break; |
| 1067 | } |
David Greene | beb31a5 | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1068 | case tgtok::XForEach: { |
Sean Silva | 6cfc806 | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 1069 | TypedInit *MHSt = dyn_cast<TypedInit>(MHS); |
David Greene | beb31a5 | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1070 | if (MHSt == 0) { |
| 1071 | TokError("could not get type for !foreach"); |
| 1072 | return 0; |
| 1073 | } |
| 1074 | Type = MHSt->getType(); |
| 1075 | break; |
| 1076 | } |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1077 | case tgtok::XSubst: { |
Sean Silva | 6cfc806 | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 1078 | TypedInit *RHSt = dyn_cast<TypedInit>(RHS); |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1079 | if (RHSt == 0) { |
| 1080 | TokError("could not get type for !subst"); |
| 1081 | return 0; |
| 1082 | } |
| 1083 | Type = RHSt->getType(); |
| 1084 | break; |
| 1085 | } |
| 1086 | } |
David Greene | dcd35c7 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1087 | return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec, |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1088 | CurMultiClass); |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1089 | } |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 1090 | } |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | /// ParseOperatorType - Parse a type for an operator. This returns |
| 1094 | /// null on error. |
| 1095 | /// |
| 1096 | /// OperatorType ::= '<' Type '>' |
| 1097 | /// |
Dan Gohman | a9ad041 | 2009-08-12 22:10:57 +0000 | [diff] [blame] | 1098 | RecTy *TGParser::ParseOperatorType() { |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 1099 | RecTy *Type = 0; |
| 1100 | |
| 1101 | if (Lex.getCode() != tgtok::less) { |
| 1102 | TokError("expected type name for operator"); |
| 1103 | return 0; |
| 1104 | } |
| 1105 | Lex.Lex(); // eat the < |
| 1106 | |
| 1107 | Type = ParseType(); |
| 1108 | |
| 1109 | if (Type == 0) { |
| 1110 | TokError("expected type name for operator"); |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
| 1114 | if (Lex.getCode() != tgtok::greater) { |
| 1115 | TokError("expected type name for operator"); |
| 1116 | return 0; |
| 1117 | } |
| 1118 | Lex.Lex(); // eat the > |
| 1119 | |
| 1120 | return Type; |
| 1121 | } |
| 1122 | |
| 1123 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1124 | /// ParseSimpleValue - Parse a tblgen value. This returns null on error. |
| 1125 | /// |
| 1126 | /// SimpleValue ::= IDValue |
| 1127 | /// SimpleValue ::= INTVAL |
Chris Lattner | d7a50cf | 2009-03-11 17:08:13 +0000 | [diff] [blame] | 1128 | /// SimpleValue ::= STRVAL+ |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1129 | /// SimpleValue ::= CODEFRAGMENT |
| 1130 | /// SimpleValue ::= '?' |
| 1131 | /// SimpleValue ::= '{' ValueList '}' |
| 1132 | /// SimpleValue ::= ID '<' ValueListNE '>' |
| 1133 | /// SimpleValue ::= '[' ValueList ']' |
| 1134 | /// SimpleValue ::= '(' IDValue DagArgList ')' |
| 1135 | /// SimpleValue ::= CONCATTOK '(' Value ',' Value ')' |
| 1136 | /// SimpleValue ::= SHLTOK '(' Value ',' Value ')' |
| 1137 | /// SimpleValue ::= SRATOK '(' Value ',' Value ')' |
| 1138 | /// SimpleValue ::= SRLTOK '(' Value ',' Value ')' |
| 1139 | /// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')' |
| 1140 | /// |
David Greene | f3744a0 | 2011-10-19 13:04:20 +0000 | [diff] [blame] | 1141 | Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType, |
| 1142 | IDParseMode Mode) { |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1143 | Init *R = 0; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1144 | switch (Lex.getCode()) { |
| 1145 | default: TokError("Unknown token when parsing a value"); break; |
David Greene | d3d1cad | 2011-10-19 13:04:43 +0000 | [diff] [blame] | 1146 | case tgtok::paste: |
| 1147 | // This is a leading paste operation. This is deprecated but |
| 1148 | // still exists in some .td files. Ignore it. |
| 1149 | Lex.Lex(); // Skip '#'. |
| 1150 | return ParseSimpleValue(CurRec, ItemType, Mode); |
David Greene | dcd35c7 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1151 | case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break; |
Chris Lattner | d7a50cf | 2009-03-11 17:08:13 +0000 | [diff] [blame] | 1152 | case tgtok::StrVal: { |
| 1153 | std::string Val = Lex.getCurStrVal(); |
| 1154 | Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1155 | |
Jim Grosbach | da4231f | 2009-03-26 16:17:51 +0000 | [diff] [blame] | 1156 | // Handle multiple consecutive concatenated strings. |
Chris Lattner | d7a50cf | 2009-03-11 17:08:13 +0000 | [diff] [blame] | 1157 | while (Lex.getCode() == tgtok::StrVal) { |
| 1158 | Val += Lex.getCurStrVal(); |
| 1159 | Lex.Lex(); |
| 1160 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1161 | |
David Greene | dcd35c7 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1162 | R = StringInit::get(Val); |
Chris Lattner | d7a50cf | 2009-03-11 17:08:13 +0000 | [diff] [blame] | 1163 | break; |
| 1164 | } |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1165 | case tgtok::CodeFragment: |
Jakob Stoklund Olesen | 8dd6f0c | 2012-01-13 03:38:34 +0000 | [diff] [blame] | 1166 | R = StringInit::get(Lex.getCurStrVal()); |
Chris Lattner | 578bcf0 | 2010-10-06 04:31:40 +0000 | [diff] [blame] | 1167 | Lex.Lex(); |
| 1168 | break; |
| 1169 | case tgtok::question: |
David Greene | dcd35c7 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1170 | R = UnsetInit::get(); |
Chris Lattner | 578bcf0 | 2010-10-06 04:31:40 +0000 | [diff] [blame] | 1171 | Lex.Lex(); |
| 1172 | break; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1173 | case tgtok::Id: { |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 1174 | SMLoc NameLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1175 | std::string Name = Lex.getCurStrVal(); |
| 1176 | if (Lex.Lex() != tgtok::less) // consume the Id. |
David Greene | f3744a0 | 2011-10-19 13:04:20 +0000 | [diff] [blame] | 1177 | return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1178 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1179 | // Value ::= ID '<' ValueListNE '>' |
| 1180 | if (Lex.Lex() == tgtok::greater) { |
| 1181 | TokError("expected non-empty value list"); |
| 1182 | return 0; |
| 1183 | } |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1184 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1185 | // This is a CLASS<initvalslist> expression. This is supposed to synthesize |
| 1186 | // a new anonymous definition, deriving from CLASS<initvalslist> with no |
| 1187 | // body. |
| 1188 | Record *Class = Records.getClass(Name); |
| 1189 | if (!Class) { |
| 1190 | Error(NameLoc, "Expected a class name, got '" + Name + "'"); |
| 1191 | return 0; |
| 1192 | } |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1193 | |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1194 | std::vector<Init*> ValueList = ParseValueList(CurRec, Class); |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1195 | if (ValueList.empty()) return 0; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1196 | |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1197 | if (Lex.getCode() != tgtok::greater) { |
| 1198 | TokError("expected '>' at end of value list"); |
| 1199 | return 0; |
| 1200 | } |
| 1201 | Lex.Lex(); // eat the '>' |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1202 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1203 | // Create the new record, set it as CurRec temporarily. |
| 1204 | static unsigned AnonCounter = 0; |
Chris Lattner | 9c6b60e | 2010-12-15 04:48:22 +0000 | [diff] [blame] | 1205 | Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++), |
| 1206 | NameLoc, |
| 1207 | Records); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1208 | SubClassReference SCRef; |
| 1209 | SCRef.RefLoc = NameLoc; |
| 1210 | SCRef.Rec = Class; |
| 1211 | SCRef.TemplateArgs = ValueList; |
| 1212 | // Add info about the subclass to NewRec. |
| 1213 | if (AddSubClass(NewRec, SCRef)) |
| 1214 | return 0; |
| 1215 | NewRec->resolveReferences(); |
| 1216 | Records.addDef(NewRec); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1217 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1218 | // The result of the expression is a reference to the new record. |
Jakob Stoklund Olesen | 77f8274 | 2011-07-18 17:02:57 +0000 | [diff] [blame] | 1219 | return DefInit::get(NewRec); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1220 | } |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1221 | case tgtok::l_brace: { // Value ::= '{' ValueList '}' |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 1222 | SMLoc BraceLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1223 | Lex.Lex(); // eat the '{' |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1224 | std::vector<Init*> Vals; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1225 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1226 | if (Lex.getCode() != tgtok::r_brace) { |
| 1227 | Vals = ParseValueList(CurRec); |
| 1228 | if (Vals.empty()) return 0; |
| 1229 | } |
| 1230 | if (Lex.getCode() != tgtok::r_brace) { |
| 1231 | TokError("expected '}' at end of bit list value"); |
| 1232 | return 0; |
| 1233 | } |
| 1234 | Lex.Lex(); // eat the '}' |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1235 | |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1236 | SmallVector<Init *, 16> NewBits(Vals.size()); |
David Greene | ca7fd3d | 2011-07-29 19:07:00 +0000 | [diff] [blame] | 1237 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1238 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) { |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1239 | Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get()); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1240 | if (Bit == 0) { |
Chris Lattner | 5d81486 | 2007-11-22 21:06:59 +0000 | [diff] [blame] | 1241 | Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+ |
| 1242 | ") is not convertable to a bit"); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1243 | return 0; |
| 1244 | } |
David Greene | ca7fd3d | 2011-07-29 19:07:00 +0000 | [diff] [blame] | 1245 | NewBits[Vals.size()-i-1] = Bit; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1246 | } |
David Greene | dcd35c7 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1247 | return BitsInit::get(NewBits); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1248 | } |
| 1249 | case tgtok::l_square: { // Value ::= '[' ValueList ']' |
| 1250 | Lex.Lex(); // eat the '[' |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1251 | std::vector<Init*> Vals; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1252 | |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1253 | RecTy *DeducedEltTy = 0; |
| 1254 | ListRecTy *GivenListTy = 0; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1255 | |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1256 | if (ItemType != 0) { |
Sean Silva | 736ceac | 2012-10-05 03:31:58 +0000 | [diff] [blame] | 1257 | ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType); |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1258 | if (ListType == 0) { |
| 1259 | std::stringstream s; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1260 | s << "Type mismatch for list, expected list type, got " |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1261 | << ItemType->getAsString(); |
| 1262 | TokError(s.str()); |
Jim Grosbach | 6a44ada | 2011-03-11 19:52:52 +0000 | [diff] [blame] | 1263 | return 0; |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1264 | } |
| 1265 | GivenListTy = ListType; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1266 | } |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1267 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1268 | if (Lex.getCode() != tgtok::r_square) { |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1269 | Vals = ParseValueList(CurRec, 0, |
| 1270 | GivenListTy ? GivenListTy->getElementType() : 0); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1271 | if (Vals.empty()) return 0; |
| 1272 | } |
| 1273 | if (Lex.getCode() != tgtok::r_square) { |
| 1274 | TokError("expected ']' at end of list value"); |
| 1275 | return 0; |
| 1276 | } |
| 1277 | Lex.Lex(); // eat the ']' |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1278 | |
| 1279 | RecTy *GivenEltTy = 0; |
| 1280 | if (Lex.getCode() == tgtok::less) { |
| 1281 | // Optional list element type |
| 1282 | Lex.Lex(); // eat the '<' |
| 1283 | |
| 1284 | GivenEltTy = ParseType(); |
| 1285 | if (GivenEltTy == 0) { |
| 1286 | // Couldn't parse element type |
| 1287 | return 0; |
| 1288 | } |
| 1289 | |
| 1290 | if (Lex.getCode() != tgtok::greater) { |
| 1291 | TokError("expected '>' at end of list element type"); |
| 1292 | return 0; |
| 1293 | } |
| 1294 | Lex.Lex(); // eat the '>' |
| 1295 | } |
| 1296 | |
| 1297 | // Check elements |
| 1298 | RecTy *EltTy = 0; |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1299 | for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end(); |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1300 | i != ie; |
| 1301 | ++i) { |
Sean Silva | 6cfc806 | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 1302 | TypedInit *TArg = dyn_cast<TypedInit>(*i); |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1303 | if (TArg == 0) { |
| 1304 | TokError("Untyped list element"); |
| 1305 | return 0; |
| 1306 | } |
| 1307 | if (EltTy != 0) { |
| 1308 | EltTy = resolveTypes(EltTy, TArg->getType()); |
| 1309 | if (EltTy == 0) { |
| 1310 | TokError("Incompatible types in list elements"); |
| 1311 | return 0; |
| 1312 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1313 | } else { |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1314 | EltTy = TArg->getType(); |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | if (GivenEltTy != 0) { |
| 1319 | if (EltTy != 0) { |
| 1320 | // Verify consistency |
| 1321 | if (!EltTy->typeIsConvertibleTo(GivenEltTy)) { |
| 1322 | TokError("Incompatible types in list elements"); |
| 1323 | return 0; |
| 1324 | } |
| 1325 | } |
| 1326 | EltTy = GivenEltTy; |
| 1327 | } |
| 1328 | |
| 1329 | if (EltTy == 0) { |
| 1330 | if (ItemType == 0) { |
| 1331 | TokError("No type for list"); |
| 1332 | return 0; |
| 1333 | } |
| 1334 | DeducedEltTy = GivenListTy->getElementType(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1335 | } else { |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1336 | // Make sure the deduced type is compatible with the given type |
| 1337 | if (GivenListTy) { |
| 1338 | if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) { |
| 1339 | TokError("Element type mismatch for list"); |
| 1340 | return 0; |
| 1341 | } |
| 1342 | } |
| 1343 | DeducedEltTy = EltTy; |
| 1344 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1345 | |
David Greene | dcd35c7 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1346 | return ListInit::get(Vals, DeducedEltTy); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1347 | } |
| 1348 | case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')' |
| 1349 | Lex.Lex(); // eat the '(' |
Chris Lattner | c7252ce | 2010-10-06 00:19:21 +0000 | [diff] [blame] | 1350 | if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) { |
Chris Lattner | 3dc2e96 | 2008-04-10 04:48:34 +0000 | [diff] [blame] | 1351 | TokError("expected identifier in dag init"); |
| 1352 | return 0; |
| 1353 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1354 | |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1355 | Init *Operator = ParseValue(CurRec); |
Chris Lattner | 578bcf0 | 2010-10-06 04:31:40 +0000 | [diff] [blame] | 1356 | if (Operator == 0) return 0; |
David Greene | c7cafcd | 2009-04-22 20:18:10 +0000 | [diff] [blame] | 1357 | |
Nate Begeman | 7cee817 | 2009-03-19 05:21:56 +0000 | [diff] [blame] | 1358 | // If the operator name is present, parse it. |
| 1359 | std::string OperatorName; |
| 1360 | if (Lex.getCode() == tgtok::colon) { |
| 1361 | if (Lex.Lex() != tgtok::VarName) { // eat the ':' |
| 1362 | TokError("expected variable name in dag operator"); |
| 1363 | return 0; |
| 1364 | } |
| 1365 | OperatorName = Lex.getCurStrVal(); |
| 1366 | Lex.Lex(); // eat the VarName. |
| 1367 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1368 | |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1369 | std::vector<std::pair<llvm::Init*, std::string> > DagArgs; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1370 | if (Lex.getCode() != tgtok::r_paren) { |
| 1371 | DagArgs = ParseDagArgList(CurRec); |
| 1372 | if (DagArgs.empty()) return 0; |
| 1373 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1374 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1375 | if (Lex.getCode() != tgtok::r_paren) { |
| 1376 | TokError("expected ')' in dag init"); |
| 1377 | return 0; |
| 1378 | } |
| 1379 | Lex.Lex(); // eat the ')' |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1380 | |
David Greene | dcd35c7 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1381 | return DagInit::get(Operator, OperatorName, DagArgs); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1382 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1383 | |
David Greene | 1434f66 | 2011-01-07 17:05:37 +0000 | [diff] [blame] | 1384 | case tgtok::XHead: |
| 1385 | case tgtok::XTail: |
| 1386 | case tgtok::XEmpty: |
David Greene | e6c27de | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 1387 | case tgtok::XCast: // Value ::= !unop '(' Value ')' |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1388 | case tgtok::XConcat: |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1389 | case tgtok::XSRA: |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1390 | case tgtok::XSRL: |
| 1391 | case tgtok::XSHL: |
David Greene | 6786d5e | 2010-01-05 19:11:42 +0000 | [diff] [blame] | 1392 | case tgtok::XEq: |
Chris Lattner | c7252ce | 2010-10-06 00:19:21 +0000 | [diff] [blame] | 1393 | case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')' |
David Greene | 9bea7c8 | 2009-05-14 23:26:46 +0000 | [diff] [blame] | 1394 | case tgtok::XIf: |
David Greene | beb31a5 | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1395 | case tgtok::XForEach: |
David Greene | 4afc509 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1396 | case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')' |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 1397 | return ParseOperation(CurRec); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1398 | } |
| 1399 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1400 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1401 | return R; |
| 1402 | } |
| 1403 | |
| 1404 | /// ParseValue - Parse a tblgen value. This returns null on error. |
| 1405 | /// |
| 1406 | /// Value ::= SimpleValue ValueSuffix* |
| 1407 | /// ValueSuffix ::= '{' BitList '}' |
| 1408 | /// ValueSuffix ::= '[' BitList ']' |
| 1409 | /// ValueSuffix ::= '.' ID |
| 1410 | /// |
David Greene | f3744a0 | 2011-10-19 13:04:20 +0000 | [diff] [blame] | 1411 | Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) { |
| 1412 | Init *Result = ParseSimpleValue(CurRec, ItemType, Mode); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1413 | if (Result == 0) return 0; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1414 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1415 | // Parse the suffixes now if present. |
| 1416 | while (1) { |
| 1417 | switch (Lex.getCode()) { |
| 1418 | default: return Result; |
| 1419 | case tgtok::l_brace: { |
David Greene | cebb4ee | 2012-02-22 16:09:41 +0000 | [diff] [blame] | 1420 | if (Mode == ParseNameMode || Mode == ParseForeachMode) |
David Greene | 8592b2b | 2011-10-19 13:04:26 +0000 | [diff] [blame] | 1421 | // This is the beginning of the object body. |
| 1422 | return Result; |
| 1423 | |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 1424 | SMLoc CurlyLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1425 | Lex.Lex(); // eat the '{' |
| 1426 | std::vector<unsigned> Ranges = ParseRangeList(); |
| 1427 | if (Ranges.empty()) return 0; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1428 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1429 | // Reverse the bitlist. |
| 1430 | std::reverse(Ranges.begin(), Ranges.end()); |
| 1431 | Result = Result->convertInitializerBitRange(Ranges); |
| 1432 | if (Result == 0) { |
| 1433 | Error(CurlyLoc, "Invalid bit range for value"); |
| 1434 | return 0; |
| 1435 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1436 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1437 | // Eat the '}'. |
| 1438 | if (Lex.getCode() != tgtok::r_brace) { |
| 1439 | TokError("expected '}' at end of bit range list"); |
| 1440 | return 0; |
| 1441 | } |
| 1442 | Lex.Lex(); |
| 1443 | break; |
| 1444 | } |
| 1445 | case tgtok::l_square: { |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 1446 | SMLoc SquareLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1447 | Lex.Lex(); // eat the '[' |
| 1448 | std::vector<unsigned> Ranges = ParseRangeList(); |
| 1449 | if (Ranges.empty()) return 0; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1450 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1451 | Result = Result->convertInitListSlice(Ranges); |
| 1452 | if (Result == 0) { |
| 1453 | Error(SquareLoc, "Invalid range for list slice"); |
| 1454 | return 0; |
| 1455 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1456 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1457 | // Eat the ']'. |
| 1458 | if (Lex.getCode() != tgtok::r_square) { |
| 1459 | TokError("expected ']' at end of list slice"); |
| 1460 | return 0; |
| 1461 | } |
| 1462 | Lex.Lex(); |
| 1463 | break; |
| 1464 | } |
| 1465 | case tgtok::period: |
| 1466 | if (Lex.Lex() != tgtok::Id) { // eat the . |
| 1467 | TokError("expected field identifier after '.'"); |
| 1468 | return 0; |
| 1469 | } |
| 1470 | if (!Result->getFieldType(Lex.getCurStrVal())) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1471 | TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" + |
Chris Lattner | 5d81486 | 2007-11-22 21:06:59 +0000 | [diff] [blame] | 1472 | Result->getAsString() + "'"); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1473 | return 0; |
| 1474 | } |
David Greene | dcd35c7 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1475 | Result = FieldInit::get(Result, Lex.getCurStrVal()); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1476 | Lex.Lex(); // eat field name |
| 1477 | break; |
David Greene | d3d1cad | 2011-10-19 13:04:43 +0000 | [diff] [blame] | 1478 | |
| 1479 | case tgtok::paste: |
| 1480 | SMLoc PasteLoc = Lex.getLoc(); |
| 1481 | |
| 1482 | // Create a !strconcat() operation, first casting each operand to |
| 1483 | // a string if necessary. |
| 1484 | |
Sean Silva | 6cfc806 | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 1485 | TypedInit *LHS = dyn_cast<TypedInit>(Result); |
David Greene | d3d1cad | 2011-10-19 13:04:43 +0000 | [diff] [blame] | 1486 | if (!LHS) { |
| 1487 | Error(PasteLoc, "LHS of paste is not typed!"); |
| 1488 | return 0; |
| 1489 | } |
| 1490 | |
| 1491 | if (LHS->getType() != StringRecTy::get()) { |
| 1492 | LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get()); |
| 1493 | } |
| 1494 | |
| 1495 | TypedInit *RHS = 0; |
| 1496 | |
| 1497 | Lex.Lex(); // Eat the '#'. |
| 1498 | switch (Lex.getCode()) { |
| 1499 | case tgtok::colon: |
| 1500 | case tgtok::semi: |
| 1501 | case tgtok::l_brace: |
| 1502 | // These are all of the tokens that can begin an object body. |
| 1503 | // Some of these can also begin values but we disallow those cases |
| 1504 | // because they are unlikely to be useful. |
| 1505 | |
| 1506 | // Trailing paste, concat with an empty string. |
| 1507 | RHS = StringInit::get(""); |
| 1508 | break; |
| 1509 | |
| 1510 | default: |
| 1511 | Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode); |
Sean Silva | 6cfc806 | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 1512 | RHS = dyn_cast<TypedInit>(RHSResult); |
David Greene | d3d1cad | 2011-10-19 13:04:43 +0000 | [diff] [blame] | 1513 | if (!RHS) { |
| 1514 | Error(PasteLoc, "RHS of paste is not typed!"); |
| 1515 | return 0; |
| 1516 | } |
| 1517 | |
| 1518 | if (RHS->getType() != StringRecTy::get()) { |
| 1519 | RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get()); |
| 1520 | } |
| 1521 | |
| 1522 | break; |
| 1523 | } |
| 1524 | |
| 1525 | Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS, |
| 1526 | StringRecTy::get())->Fold(CurRec, CurMultiClass); |
| 1527 | break; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1528 | } |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | /// ParseDagArgList - Parse the argument list for a dag literal expression. |
| 1533 | /// |
| 1534 | /// ParseDagArgList ::= Value (':' VARNAME)? |
| 1535 | /// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)? |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1536 | std::vector<std::pair<llvm::Init*, std::string> > |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1537 | TGParser::ParseDagArgList(Record *CurRec) { |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1538 | std::vector<std::pair<llvm::Init*, std::string> > Result; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1539 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1540 | while (1) { |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1541 | Init *Val = ParseValue(CurRec); |
| 1542 | if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1543 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1544 | // If the variable name is present, add it. |
| 1545 | std::string VarName; |
| 1546 | if (Lex.getCode() == tgtok::colon) { |
| 1547 | if (Lex.Lex() != tgtok::VarName) { // eat the ':' |
| 1548 | TokError("expected variable name in dag literal"); |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1549 | return std::vector<std::pair<llvm::Init*, std::string> >(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1550 | } |
| 1551 | VarName = Lex.getCurStrVal(); |
| 1552 | Lex.Lex(); // eat the VarName. |
| 1553 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1554 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1555 | Result.push_back(std::make_pair(Val, VarName)); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1556 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1557 | if (Lex.getCode() != tgtok::comma) break; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1558 | Lex.Lex(); // eat the ',' |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1559 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1560 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1561 | return Result; |
| 1562 | } |
| 1563 | |
| 1564 | |
| 1565 | /// ParseValueList - Parse a comma separated list of values, returning them as a |
| 1566 | /// vector. Note that this always expects to be able to parse at least one |
| 1567 | /// value. It returns an empty list if this is not possible. |
| 1568 | /// |
| 1569 | /// ValueList ::= Value (',' Value) |
| 1570 | /// |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1571 | std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec, |
Eric Christopher | d568b3f | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1572 | RecTy *EltTy) { |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1573 | std::vector<Init*> Result; |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1574 | RecTy *ItemType = EltTy; |
David Greene | 67acdf2 | 2009-06-29 19:59:52 +0000 | [diff] [blame] | 1575 | unsigned int ArgN = 0; |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1576 | if (ArgsRec != 0 && EltTy == 0) { |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 1577 | const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs(); |
Jim Grosbach | b1320cb | 2012-01-20 20:02:39 +0000 | [diff] [blame] | 1578 | if (!TArgs.size()) { |
| 1579 | TokError("template argument provided to non-template class"); |
| 1580 | return std::vector<Init*>(); |
| 1581 | } |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1582 | const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]); |
David Greene | d9746fe | 2011-09-19 18:26:07 +0000 | [diff] [blame] | 1583 | if (!RV) { |
| 1584 | errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN] |
| 1585 | << ")\n"; |
| 1586 | } |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1587 | assert(RV && "Template argument record not found??"); |
| 1588 | ItemType = RV->getType(); |
| 1589 | ++ArgN; |
| 1590 | } |
| 1591 | Result.push_back(ParseValue(CurRec, ItemType)); |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1592 | if (Result.back() == 0) return std::vector<Init*>(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1593 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1594 | while (Lex.getCode() == tgtok::comma) { |
| 1595 | Lex.Lex(); // Eat the comma |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1596 | |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1597 | if (ArgsRec != 0 && EltTy == 0) { |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 1598 | const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs(); |
David Greene | 67acdf2 | 2009-06-29 19:59:52 +0000 | [diff] [blame] | 1599 | if (ArgN >= TArgs.size()) { |
| 1600 | TokError("too many template arguments"); |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1601 | return std::vector<Init*>(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1602 | } |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1603 | const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]); |
| 1604 | assert(RV && "Template argument record not found??"); |
| 1605 | ItemType = RV->getType(); |
| 1606 | ++ArgN; |
| 1607 | } |
| 1608 | Result.push_back(ParseValue(CurRec, ItemType)); |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1609 | if (Result.back() == 0) return std::vector<Init*>(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1610 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1611 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1612 | return Result; |
| 1613 | } |
| 1614 | |
| 1615 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1616 | /// ParseDeclaration - Read a declaration, returning the name of field ID, or an |
| 1617 | /// empty string on error. This can happen in a number of different context's, |
| 1618 | /// including within a def or in the template args for a def (which which case |
| 1619 | /// CurRec will be non-null) and within the template args for a multiclass (in |
| 1620 | /// which case CurRec will be null, but CurMultiClass will be set). This can |
| 1621 | /// also happen within a def that is within a multiclass, which will set both |
| 1622 | /// CurRec and CurMultiClass. |
| 1623 | /// |
| 1624 | /// Declaration ::= FIELD? Type ID ('=' Value)? |
| 1625 | /// |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 1626 | Init *TGParser::ParseDeclaration(Record *CurRec, |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1627 | bool ParsingTemplateArgs) { |
| 1628 | // Read the field prefix if present. |
| 1629 | bool HasField = Lex.getCode() == tgtok::Field; |
| 1630 | if (HasField) Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1631 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1632 | RecTy *Type = ParseType(); |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 1633 | if (Type == 0) return 0; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1634 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1635 | if (Lex.getCode() != tgtok::Id) { |
| 1636 | TokError("Expected identifier in declaration"); |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 1637 | return 0; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1638 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1639 | |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 1640 | SMLoc IdLoc = Lex.getLoc(); |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 1641 | Init *DeclName = StringInit::get(Lex.getCurStrVal()); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1642 | Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1643 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1644 | if (ParsingTemplateArgs) { |
| 1645 | if (CurRec) { |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 1646 | DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":"); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1647 | } else { |
| 1648 | assert(CurMultiClass); |
| 1649 | } |
| 1650 | if (CurMultiClass) |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 1651 | DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName, |
| 1652 | "::"); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1653 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1654 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1655 | // Add the value. |
| 1656 | if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField))) |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 1657 | return 0; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1658 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1659 | // If a value is present, parse it. |
| 1660 | if (Lex.getCode() == tgtok::equal) { |
| 1661 | Lex.Lex(); |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 1662 | SMLoc ValLoc = Lex.getLoc(); |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1663 | Init *Val = ParseValue(CurRec, Type); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1664 | if (Val == 0 || |
| 1665 | SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val)) |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 1666 | return 0; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1667 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1668 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1669 | return DeclName; |
| 1670 | } |
| 1671 | |
David Greene | cebb4ee | 2012-02-22 16:09:41 +0000 | [diff] [blame] | 1672 | /// ParseForeachDeclaration - Read a foreach declaration, returning |
| 1673 | /// the name of the declared object or a NULL Init on error. Return |
| 1674 | /// the name of the parsed initializer list through ForeachListName. |
| 1675 | /// |
Jakob Stoklund Olesen | fae8b1d | 2012-05-24 22:17:39 +0000 | [diff] [blame] | 1676 | /// ForeachDeclaration ::= ID '=' '[' ValueList ']' |
| 1677 | /// ForeachDeclaration ::= ID '=' '{' RangeList '}' |
| 1678 | /// ForeachDeclaration ::= ID '=' RangePiece |
David Greene | cebb4ee | 2012-02-22 16:09:41 +0000 | [diff] [blame] | 1679 | /// |
Jakob Stoklund Olesen | 8e5286e | 2012-05-24 22:17:33 +0000 | [diff] [blame] | 1680 | VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) { |
David Greene | cebb4ee | 2012-02-22 16:09:41 +0000 | [diff] [blame] | 1681 | if (Lex.getCode() != tgtok::Id) { |
| 1682 | TokError("Expected identifier in foreach declaration"); |
| 1683 | return 0; |
| 1684 | } |
| 1685 | |
| 1686 | Init *DeclName = StringInit::get(Lex.getCurStrVal()); |
| 1687 | Lex.Lex(); |
| 1688 | |
| 1689 | // If a value is present, parse it. |
| 1690 | if (Lex.getCode() != tgtok::equal) { |
| 1691 | TokError("Expected '=' in foreach declaration"); |
| 1692 | return 0; |
| 1693 | } |
| 1694 | Lex.Lex(); // Eat the '=' |
| 1695 | |
Jakob Stoklund Olesen | fae8b1d | 2012-05-24 22:17:39 +0000 | [diff] [blame] | 1696 | RecTy *IterType = 0; |
| 1697 | std::vector<unsigned> Ranges; |
David Greene | cebb4ee | 2012-02-22 16:09:41 +0000 | [diff] [blame] | 1698 | |
Jakob Stoklund Olesen | fae8b1d | 2012-05-24 22:17:39 +0000 | [diff] [blame] | 1699 | switch (Lex.getCode()) { |
| 1700 | default: TokError("Unknown token when expecting a range list"); return 0; |
| 1701 | case tgtok::l_square: { // '[' ValueList ']' |
| 1702 | Init *List = ParseSimpleValue(0, 0, ParseForeachMode); |
Sean Silva | 6cfc806 | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 1703 | ForeachListValue = dyn_cast<ListInit>(List); |
Jakob Stoklund Olesen | fae8b1d | 2012-05-24 22:17:39 +0000 | [diff] [blame] | 1704 | if (ForeachListValue == 0) { |
| 1705 | TokError("Expected a Value list"); |
| 1706 | return 0; |
| 1707 | } |
| 1708 | RecTy *ValueType = ForeachListValue->getType(); |
Sean Silva | 736ceac | 2012-10-05 03:31:58 +0000 | [diff] [blame] | 1709 | ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType); |
Jakob Stoklund Olesen | fae8b1d | 2012-05-24 22:17:39 +0000 | [diff] [blame] | 1710 | if (ListType == 0) { |
| 1711 | TokError("Value list is not of list type"); |
| 1712 | return 0; |
| 1713 | } |
| 1714 | IterType = ListType->getElementType(); |
| 1715 | break; |
David Greene | cebb4ee | 2012-02-22 16:09:41 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
Jakob Stoklund Olesen | fae8b1d | 2012-05-24 22:17:39 +0000 | [diff] [blame] | 1718 | case tgtok::IntVal: { // RangePiece. |
| 1719 | if (ParseRangePiece(Ranges)) |
| 1720 | return 0; |
| 1721 | break; |
David Greene | cebb4ee | 2012-02-22 16:09:41 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
Jakob Stoklund Olesen | fae8b1d | 2012-05-24 22:17:39 +0000 | [diff] [blame] | 1724 | case tgtok::l_brace: { // '{' RangeList '}' |
| 1725 | Lex.Lex(); // eat the '{' |
| 1726 | Ranges = ParseRangeList(); |
| 1727 | if (Lex.getCode() != tgtok::r_brace) { |
| 1728 | TokError("expected '}' at end of bit range list"); |
| 1729 | return 0; |
| 1730 | } |
| 1731 | Lex.Lex(); |
| 1732 | break; |
| 1733 | } |
| 1734 | } |
David Greene | cebb4ee | 2012-02-22 16:09:41 +0000 | [diff] [blame] | 1735 | |
Jakob Stoklund Olesen | fae8b1d | 2012-05-24 22:17:39 +0000 | [diff] [blame] | 1736 | if (!Ranges.empty()) { |
| 1737 | assert(!IterType && "Type already initialized?"); |
| 1738 | IterType = IntRecTy::get(); |
| 1739 | std::vector<Init*> Values; |
| 1740 | for (unsigned i = 0, e = Ranges.size(); i != e; ++i) |
| 1741 | Values.push_back(IntInit::get(Ranges[i])); |
| 1742 | ForeachListValue = ListInit::get(Values, IterType); |
| 1743 | } |
| 1744 | |
| 1745 | if (!IterType) |
| 1746 | return 0; |
| 1747 | |
| 1748 | return VarInit::get(DeclName, IterType); |
David Greene | cebb4ee | 2012-02-22 16:09:41 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1751 | /// ParseTemplateArgList - Read a template argument list, which is a non-empty |
| 1752 | /// sequence of template-declarations in <>'s. If CurRec is non-null, these are |
| 1753 | /// template args for a def, which may or may not be in a multiclass. If null, |
| 1754 | /// these are the template args for a multiclass. |
| 1755 | /// |
| 1756 | /// TemplateArgList ::= '<' Declaration (',' Declaration)* '>' |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1757 | /// |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1758 | bool TGParser::ParseTemplateArgList(Record *CurRec) { |
| 1759 | assert(Lex.getCode() == tgtok::less && "Not a template arg list!"); |
| 1760 | Lex.Lex(); // eat the '<' |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1761 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1762 | Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1763 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1764 | // Read the first declaration. |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 1765 | Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/); |
| 1766 | if (TemplArg == 0) |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1767 | return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1768 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1769 | TheRecToAddTo->addTemplateArg(TemplArg); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1770 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1771 | while (Lex.getCode() == tgtok::comma) { |
| 1772 | Lex.Lex(); // eat the ',' |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1773 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1774 | // Read the following declarations. |
| 1775 | TemplArg = ParseDeclaration(CurRec, true/*templateargs*/); |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 1776 | if (TemplArg == 0) |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1777 | return true; |
| 1778 | TheRecToAddTo->addTemplateArg(TemplArg); |
| 1779 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1780 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1781 | if (Lex.getCode() != tgtok::greater) |
| 1782 | return TokError("expected '>' at end of template argument list"); |
| 1783 | Lex.Lex(); // eat the '>'. |
| 1784 | return false; |
| 1785 | } |
| 1786 | |
| 1787 | |
| 1788 | /// ParseBodyItem - Parse a single item at within the body of a def or class. |
| 1789 | /// |
| 1790 | /// BodyItem ::= Declaration ';' |
| 1791 | /// BodyItem ::= LET ID OptionalBitList '=' Value ';' |
| 1792 | bool TGParser::ParseBodyItem(Record *CurRec) { |
| 1793 | if (Lex.getCode() != tgtok::Let) { |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 1794 | if (ParseDeclaration(CurRec, false) == 0) |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1795 | return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1796 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1797 | if (Lex.getCode() != tgtok::semi) |
| 1798 | return TokError("expected ';' after declaration"); |
| 1799 | Lex.Lex(); |
| 1800 | return false; |
| 1801 | } |
| 1802 | |
| 1803 | // LET ID OptionalRangeList '=' Value ';' |
| 1804 | if (Lex.Lex() != tgtok::Id) |
| 1805 | return TokError("expected field identifier after let"); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1806 | |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 1807 | SMLoc IdLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1808 | std::string FieldName = Lex.getCurStrVal(); |
| 1809 | Lex.Lex(); // eat the field name. |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1810 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1811 | std::vector<unsigned> BitList; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1812 | if (ParseOptionalBitList(BitList)) |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1813 | return true; |
| 1814 | std::reverse(BitList.begin(), BitList.end()); |
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 (Lex.getCode() != tgtok::equal) |
| 1817 | return TokError("expected '=' in let expression"); |
| 1818 | Lex.Lex(); // eat the '='. |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1819 | |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 1820 | RecordVal *Field = CurRec->getValue(FieldName); |
| 1821 | if (Field == 0) |
| 1822 | return TokError("Value '" + FieldName + "' unknown!"); |
| 1823 | |
| 1824 | RecTy *Type = Field->getType(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1825 | |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1826 | Init *Val = ParseValue(CurRec, Type); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1827 | if (Val == 0) return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1828 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1829 | if (Lex.getCode() != tgtok::semi) |
| 1830 | return TokError("expected ';' after let expression"); |
| 1831 | Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1832 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1833 | return SetValue(CurRec, IdLoc, FieldName, BitList, Val); |
| 1834 | } |
| 1835 | |
| 1836 | /// ParseBody - Read the body of a class or def. Return true on error, false on |
| 1837 | /// success. |
| 1838 | /// |
| 1839 | /// Body ::= ';' |
| 1840 | /// Body ::= '{' BodyList '}' |
| 1841 | /// BodyList BodyItem* |
| 1842 | /// |
| 1843 | bool TGParser::ParseBody(Record *CurRec) { |
| 1844 | // If this is a null definition, just eat the semi and return. |
| 1845 | if (Lex.getCode() == tgtok::semi) { |
| 1846 | Lex.Lex(); |
| 1847 | return false; |
| 1848 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1849 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1850 | if (Lex.getCode() != tgtok::l_brace) |
| 1851 | return TokError("Expected ';' or '{' to start body"); |
| 1852 | // Eat the '{'. |
| 1853 | Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1854 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1855 | while (Lex.getCode() != tgtok::r_brace) |
| 1856 | if (ParseBodyItem(CurRec)) |
| 1857 | return true; |
| 1858 | |
| 1859 | // Eat the '}'. |
| 1860 | Lex.Lex(); |
| 1861 | return false; |
| 1862 | } |
| 1863 | |
Sean Silva | 9cceede | 2013-01-09 04:49:14 +0000 | [diff] [blame^] | 1864 | /// \brief Apply the current let bindings to \a CurRec. |
| 1865 | /// \returns true on error, false otherwise. |
| 1866 | bool TGParser::ApplyLetStack(Record *CurRec) { |
| 1867 | for (unsigned i = 0, e = LetStack.size(); i != e; ++i) |
| 1868 | for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j) |
| 1869 | if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name, |
| 1870 | LetStack[i][j].Bits, LetStack[i][j].Value)) |
| 1871 | return true; |
| 1872 | return false; |
| 1873 | } |
| 1874 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1875 | /// ParseObjectBody - Parse the body of a def or class. This consists of an |
| 1876 | /// optional ClassList followed by a Body. CurRec is the current def or class |
| 1877 | /// that is being parsed. |
| 1878 | /// |
| 1879 | /// ObjectBody ::= BaseClassList Body |
| 1880 | /// BaseClassList ::= /*empty*/ |
| 1881 | /// BaseClassList ::= ':' BaseClassListNE |
| 1882 | /// BaseClassListNE ::= SubClassRef (',' SubClassRef)* |
| 1883 | /// |
| 1884 | bool TGParser::ParseObjectBody(Record *CurRec) { |
| 1885 | // If there is a baseclass list, read it. |
| 1886 | if (Lex.getCode() == tgtok::colon) { |
| 1887 | Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1888 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1889 | // Read all of the subclasses. |
| 1890 | SubClassReference SubClass = ParseSubClassReference(CurRec, false); |
| 1891 | while (1) { |
| 1892 | // Check for error. |
| 1893 | if (SubClass.Rec == 0) return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1894 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1895 | // Add it. |
| 1896 | if (AddSubClass(CurRec, SubClass)) |
| 1897 | return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1898 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1899 | if (Lex.getCode() != tgtok::comma) break; |
| 1900 | Lex.Lex(); // eat ','. |
| 1901 | SubClass = ParseSubClassReference(CurRec, false); |
| 1902 | } |
| 1903 | } |
| 1904 | |
Sean Silva | 9cceede | 2013-01-09 04:49:14 +0000 | [diff] [blame^] | 1905 | if (ApplyLetStack(CurRec)) |
| 1906 | return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1907 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1908 | return ParseBody(CurRec); |
| 1909 | } |
| 1910 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1911 | /// ParseDef - Parse and return a top level or multiclass def, return the record |
| 1912 | /// corresponding to it. This returns null on error. |
| 1913 | /// |
| 1914 | /// DefInst ::= DEF ObjectName ObjectBody |
| 1915 | /// |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 1916 | bool TGParser::ParseDef(MultiClass *CurMultiClass) { |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 1917 | SMLoc DefLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1918 | assert(Lex.getCode() == tgtok::Def && "Unknown tok"); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1919 | Lex.Lex(); // Eat the 'def' token. |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1920 | |
| 1921 | // Parse ObjectName and make a record for it. |
David Greene | a9e07dd | 2011-10-19 13:04:29 +0000 | [diff] [blame] | 1922 | Record *CurRec = new Record(ParseObjectName(CurMultiClass), DefLoc, Records); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1923 | |
Jakob Stoklund Olesen | 72cba6c | 2012-05-24 22:17:36 +0000 | [diff] [blame] | 1924 | if (!CurMultiClass && Loops.empty()) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1925 | // Top-level def definition. |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1926 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1927 | // Ensure redefinition doesn't happen. |
David Greene | 1d50139 | 2012-01-28 00:03:24 +0000 | [diff] [blame] | 1928 | if (Records.getDef(CurRec->getNameInitAsString())) { |
David Greene | 2c49fbb | 2011-10-19 13:03:45 +0000 | [diff] [blame] | 1929 | Error(DefLoc, "def '" + CurRec->getNameInitAsString() |
| 1930 | + "' already defined"); |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 1931 | return true; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1932 | } |
| 1933 | Records.addDef(CurRec); |
Jakob Stoklund Olesen | 72cba6c | 2012-05-24 22:17:36 +0000 | [diff] [blame] | 1934 | } else if (CurMultiClass) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1935 | // Otherwise, a def inside a multiclass, add it to the multiclass. |
| 1936 | for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i) |
David Greene | 91919cd | 2011-10-19 13:03:51 +0000 | [diff] [blame] | 1937 | if (CurMultiClass->DefPrototypes[i]->getNameInit() |
| 1938 | == CurRec->getNameInit()) { |
| 1939 | Error(DefLoc, "def '" + CurRec->getNameInitAsString() + |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1940 | "' already defined in this multiclass!"); |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 1941 | return true; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1942 | } |
| 1943 | CurMultiClass->DefPrototypes.push_back(CurRec); |
| 1944 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1945 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1946 | if (ParseObjectBody(CurRec)) |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 1947 | return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1948 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1949 | if (CurMultiClass == 0) // Def's in multiclasses aren't really defs. |
David Greene | 0d88640 | 2011-08-10 18:27:46 +0000 | [diff] [blame] | 1950 | // See Record::setName(). This resolve step will see any new name |
| 1951 | // for the def that might have been created when resolving |
| 1952 | // inheritance, values and arguments above. |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1953 | CurRec->resolveReferences(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1954 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1955 | // If ObjectBody has template arguments, it's an error. |
| 1956 | assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?"); |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 1957 | |
| 1958 | if (CurMultiClass) { |
| 1959 | // Copy the template arguments for the multiclass into the def. |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 1960 | const std::vector<Init *> &TArgs = |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 1961 | CurMultiClass->Rec.getTemplateArgs(); |
| 1962 | |
| 1963 | for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { |
| 1964 | const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]); |
| 1965 | assert(RV && "Template arg doesn't exist?"); |
| 1966 | CurRec->addValue(*RV); |
| 1967 | } |
| 1968 | } |
| 1969 | |
Jakob Stoklund Olesen | 8e5286e | 2012-05-24 22:17:33 +0000 | [diff] [blame] | 1970 | if (ProcessForeachDefs(CurRec, DefLoc)) { |
David Greene | cebb4ee | 2012-02-22 16:09:41 +0000 | [diff] [blame] | 1971 | Error(DefLoc, |
| 1972 | "Could not process loops for def" + CurRec->getNameInitAsString()); |
| 1973 | return true; |
| 1974 | } |
| 1975 | |
| 1976 | return false; |
| 1977 | } |
| 1978 | |
| 1979 | /// ParseForeach - Parse a for statement. Return the record corresponding |
| 1980 | /// to it. This returns true on error. |
| 1981 | /// |
| 1982 | /// Foreach ::= FOREACH Declaration IN '{ ObjectList '}' |
| 1983 | /// Foreach ::= FOREACH Declaration IN Object |
| 1984 | /// |
| 1985 | bool TGParser::ParseForeach(MultiClass *CurMultiClass) { |
| 1986 | assert(Lex.getCode() == tgtok::Foreach && "Unknown tok"); |
| 1987 | Lex.Lex(); // Eat the 'for' token. |
| 1988 | |
| 1989 | // Make a temporary object to record items associated with the for |
| 1990 | // loop. |
Jakob Stoklund Olesen | 8e5286e | 2012-05-24 22:17:33 +0000 | [diff] [blame] | 1991 | ListInit *ListValue = 0; |
| 1992 | VarInit *IterName = ParseForeachDeclaration(ListValue); |
David Greene | cebb4ee | 2012-02-22 16:09:41 +0000 | [diff] [blame] | 1993 | if (IterName == 0) |
| 1994 | return TokError("expected declaration in for"); |
| 1995 | |
| 1996 | if (Lex.getCode() != tgtok::In) |
| 1997 | return TokError("Unknown tok"); |
| 1998 | Lex.Lex(); // Eat the in |
| 1999 | |
| 2000 | // Create a loop object and remember it. |
| 2001 | Loops.push_back(ForeachLoop(IterName, ListValue)); |
| 2002 | |
| 2003 | if (Lex.getCode() != tgtok::l_brace) { |
| 2004 | // FOREACH Declaration IN Object |
| 2005 | if (ParseObject(CurMultiClass)) |
| 2006 | return true; |
| 2007 | } |
| 2008 | else { |
| 2009 | SMLoc BraceLoc = Lex.getLoc(); |
| 2010 | // Otherwise, this is a group foreach. |
| 2011 | Lex.Lex(); // eat the '{'. |
| 2012 | |
| 2013 | // Parse the object list. |
| 2014 | if (ParseObjectList(CurMultiClass)) |
| 2015 | return true; |
| 2016 | |
| 2017 | if (Lex.getCode() != tgtok::r_brace) { |
| 2018 | TokError("expected '}' at end of foreach command"); |
| 2019 | return Error(BraceLoc, "to match this '{'"); |
| 2020 | } |
| 2021 | Lex.Lex(); // Eat the } |
| 2022 | } |
| 2023 | |
| 2024 | // We've processed everything in this loop. |
| 2025 | Loops.pop_back(); |
| 2026 | |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 2027 | return false; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2030 | /// ParseClass - Parse a tblgen class definition. |
| 2031 | /// |
| 2032 | /// ClassInst ::= CLASS ID TemplateArgList? ObjectBody |
| 2033 | /// |
| 2034 | bool TGParser::ParseClass() { |
| 2035 | assert(Lex.getCode() == tgtok::Class && "Unexpected token!"); |
| 2036 | Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2037 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2038 | if (Lex.getCode() != tgtok::Id) |
| 2039 | return TokError("expected class name after 'class' keyword"); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2040 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2041 | Record *CurRec = Records.getClass(Lex.getCurStrVal()); |
| 2042 | if (CurRec) { |
| 2043 | // If the body was previously defined, this is an error. |
David Greene | e338565 | 2011-10-19 13:04:13 +0000 | [diff] [blame] | 2044 | if (CurRec->getValues().size() > 1 || // Account for NAME. |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2045 | !CurRec->getSuperClasses().empty() || |
| 2046 | !CurRec->getTemplateArgs().empty()) |
David Greene | 69a2394 | 2011-10-19 13:03:58 +0000 | [diff] [blame] | 2047 | return TokError("Class '" + CurRec->getNameInitAsString() |
| 2048 | + "' already defined"); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2049 | } else { |
| 2050 | // If this is the first reference to this class, create and add it. |
Chris Lattner | 9c6b60e | 2010-12-15 04:48:22 +0000 | [diff] [blame] | 2051 | CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2052 | Records.addClass(CurRec); |
| 2053 | } |
| 2054 | Lex.Lex(); // eat the name. |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2055 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2056 | // If there are template args, parse them. |
| 2057 | if (Lex.getCode() == tgtok::less) |
| 2058 | if (ParseTemplateArgList(CurRec)) |
| 2059 | return true; |
| 2060 | |
| 2061 | // Finally, parse the object body. |
| 2062 | return ParseObjectBody(CurRec); |
| 2063 | } |
| 2064 | |
| 2065 | /// ParseLetList - Parse a non-empty list of assignment expressions into a list |
| 2066 | /// of LetRecords. |
| 2067 | /// |
| 2068 | /// LetList ::= LetItem (',' LetItem)* |
| 2069 | /// LetItem ::= ID OptionalRangeList '=' Value |
| 2070 | /// |
| 2071 | std::vector<LetRecord> TGParser::ParseLetList() { |
| 2072 | std::vector<LetRecord> Result; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2073 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2074 | while (1) { |
| 2075 | if (Lex.getCode() != tgtok::Id) { |
| 2076 | TokError("expected identifier in let definition"); |
| 2077 | return std::vector<LetRecord>(); |
| 2078 | } |
| 2079 | std::string Name = Lex.getCurStrVal(); |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 2080 | SMLoc NameLoc = Lex.getLoc(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2081 | Lex.Lex(); // Eat the identifier. |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2082 | |
| 2083 | // Check for an optional RangeList. |
| 2084 | std::vector<unsigned> Bits; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2085 | if (ParseOptionalRangeList(Bits)) |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2086 | return std::vector<LetRecord>(); |
| 2087 | std::reverse(Bits.begin(), Bits.end()); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2088 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2089 | if (Lex.getCode() != tgtok::equal) { |
| 2090 | TokError("expected '=' in let expression"); |
| 2091 | return std::vector<LetRecord>(); |
| 2092 | } |
| 2093 | Lex.Lex(); // eat the '='. |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2094 | |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 2095 | Init *Val = ParseValue(0); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2096 | if (Val == 0) return std::vector<LetRecord>(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2097 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2098 | // Now that we have everything, add the record. |
| 2099 | Result.push_back(LetRecord(Name, Bits, Val, NameLoc)); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2100 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2101 | if (Lex.getCode() != tgtok::comma) |
| 2102 | return Result; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2103 | Lex.Lex(); // eat the comma. |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | /// 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] | 2108 | /// different related productions. This works inside multiclasses too. |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2109 | /// |
| 2110 | /// Object ::= LET LetList IN '{' ObjectList '}' |
| 2111 | /// Object ::= LET LetList IN Object |
| 2112 | /// |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 2113 | bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2114 | assert(Lex.getCode() == tgtok::Let && "Unexpected token"); |
| 2115 | Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2116 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2117 | // Add this entry to the let stack. |
| 2118 | std::vector<LetRecord> LetInfo = ParseLetList(); |
| 2119 | if (LetInfo.empty()) return true; |
| 2120 | LetStack.push_back(LetInfo); |
| 2121 | |
| 2122 | if (Lex.getCode() != tgtok::In) |
| 2123 | return TokError("expected 'in' at end of top-level 'let'"); |
| 2124 | Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2125 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2126 | // If this is a scalar let, just handle it now |
| 2127 | if (Lex.getCode() != tgtok::l_brace) { |
| 2128 | // LET LetList IN Object |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 2129 | if (ParseObject(CurMultiClass)) |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2130 | return true; |
| 2131 | } else { // Object ::= LETCommand '{' ObjectList '}' |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 2132 | SMLoc BraceLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2133 | // Otherwise, this is a group let. |
| 2134 | Lex.Lex(); // eat the '{'. |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2135 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2136 | // Parse the object list. |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 2137 | if (ParseObjectList(CurMultiClass)) |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2138 | return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2139 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2140 | if (Lex.getCode() != tgtok::r_brace) { |
| 2141 | TokError("expected '}' at end of top level let command"); |
| 2142 | return Error(BraceLoc, "to match this '{'"); |
| 2143 | } |
| 2144 | Lex.Lex(); |
| 2145 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2146 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2147 | // Outside this let scope, this let block is not active. |
| 2148 | LetStack.pop_back(); |
| 2149 | return false; |
| 2150 | } |
| 2151 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2152 | /// ParseMultiClass - Parse a multiclass definition. |
| 2153 | /// |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 2154 | /// MultiClassInst ::= MULTICLASS ID TemplateArgList? |
Sean Silva | 9302dcc | 2013-01-09 02:11:55 +0000 | [diff] [blame] | 2155 | /// ':' BaseMultiClassList '{' MultiClassObject+ '}' |
| 2156 | /// MultiClassObject ::= DefInst |
| 2157 | /// MultiClassObject ::= MultiClassInst |
| 2158 | /// MultiClassObject ::= DefMInst |
| 2159 | /// MultiClassObject ::= LETCommand '{' ObjectList '}' |
| 2160 | /// MultiClassObject ::= LETCommand Object |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2161 | /// |
| 2162 | bool TGParser::ParseMultiClass() { |
| 2163 | assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token"); |
| 2164 | Lex.Lex(); // Eat the multiclass token. |
| 2165 | |
| 2166 | if (Lex.getCode() != tgtok::Id) |
| 2167 | return TokError("expected identifier after multiclass for name"); |
| 2168 | std::string Name = Lex.getCurStrVal(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2169 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2170 | if (MultiClasses.count(Name)) |
| 2171 | return TokError("multiclass '" + Name + "' already defined"); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2172 | |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 2173 | CurMultiClass = MultiClasses[Name] = new MultiClass(Name, |
| 2174 | Lex.getLoc(), Records); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2175 | Lex.Lex(); // Eat the identifier. |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2176 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2177 | // If there are template args, parse them. |
| 2178 | if (Lex.getCode() == tgtok::less) |
| 2179 | if (ParseTemplateArgList(0)) |
| 2180 | return true; |
| 2181 | |
David Greene | d34a73b | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 2182 | bool inherits = false; |
| 2183 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 2184 | // If there are submulticlasses, parse them. |
| 2185 | if (Lex.getCode() == tgtok::colon) { |
David Greene | d34a73b | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 2186 | inherits = true; |
| 2187 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 2188 | Lex.Lex(); |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 2189 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 2190 | // Read all of the submulticlasses. |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 2191 | SubMultiClassReference SubMultiClass = |
| 2192 | ParseSubMultiClassReference(CurMultiClass); |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 2193 | while (1) { |
| 2194 | // Check for error. |
| 2195 | if (SubMultiClass.MC == 0) return true; |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 2196 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 2197 | // Add it. |
| 2198 | if (AddSubMultiClass(CurMultiClass, SubMultiClass)) |
| 2199 | return true; |
Bob Wilson | 3255865 | 2009-04-28 19:41:44 +0000 | [diff] [blame] | 2200 | |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 2201 | if (Lex.getCode() != tgtok::comma) break; |
| 2202 | Lex.Lex(); // eat ','. |
| 2203 | SubMultiClass = ParseSubMultiClassReference(CurMultiClass); |
| 2204 | } |
| 2205 | } |
| 2206 | |
David Greene | d34a73b | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 2207 | if (Lex.getCode() != tgtok::l_brace) { |
| 2208 | if (!inherits) |
| 2209 | return TokError("expected '{' in multiclass definition"); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2210 | else if (Lex.getCode() != tgtok::semi) |
| 2211 | return TokError("expected ';' in multiclass definition"); |
David Greene | d34a73b | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 2212 | else |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2213 | Lex.Lex(); // eat the ';'. |
| 2214 | } else { |
David Greene | d34a73b | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 2215 | if (Lex.Lex() == tgtok::r_brace) // eat the '{'. |
| 2216 | return TokError("multiclass must contain at least one def"); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2217 | |
Bruno Cardoso Lopes | 270562b | 2010-06-05 02:11:52 +0000 | [diff] [blame] | 2218 | while (Lex.getCode() != tgtok::r_brace) { |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 2219 | switch (Lex.getCode()) { |
| 2220 | default: |
David Greene | a1b1b79 | 2011-10-07 18:25:05 +0000 | [diff] [blame] | 2221 | return TokError("expected 'let', 'def' or 'defm' in multiclass body"); |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 2222 | case tgtok::Let: |
| 2223 | case tgtok::Def: |
| 2224 | case tgtok::Defm: |
David Greene | cebb4ee | 2012-02-22 16:09:41 +0000 | [diff] [blame] | 2225 | case tgtok::Foreach: |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 2226 | if (ParseObject(CurMultiClass)) |
| 2227 | return true; |
| 2228 | break; |
| 2229 | } |
Bruno Cardoso Lopes | 270562b | 2010-06-05 02:11:52 +0000 | [diff] [blame] | 2230 | } |
David Greene | d34a73b | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 2231 | Lex.Lex(); // eat the '}'. |
| 2232 | } |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2233 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2234 | CurMultiClass = 0; |
| 2235 | return false; |
| 2236 | } |
| 2237 | |
David Greene | e499a2d | 2011-10-05 22:42:07 +0000 | [diff] [blame] | 2238 | Record *TGParser:: |
| 2239 | InstantiateMulticlassDef(MultiClass &MC, |
| 2240 | Record *DefProto, |
David Greene | 7be867e | 2011-10-19 13:04:31 +0000 | [diff] [blame] | 2241 | Init *DefmPrefix, |
David Greene | e499a2d | 2011-10-05 22:42:07 +0000 | [diff] [blame] | 2242 | SMLoc DefmPrefixLoc) { |
David Greene | 7be867e | 2011-10-19 13:04:31 +0000 | [diff] [blame] | 2243 | // We need to preserve DefProto so it can be reused for later |
| 2244 | // instantiations, so create a new Record to inherit from it. |
| 2245 | |
David Greene | e499a2d | 2011-10-05 22:42:07 +0000 | [diff] [blame] | 2246 | // Add in the defm name. If the defm prefix is empty, give each |
| 2247 | // instantiated def a unique name. Otherwise, if "#NAME#" exists in the |
| 2248 | // name, substitute the prefix for #NAME#. Otherwise, use the defm name |
| 2249 | // as a prefix. |
David Greene | e499a2d | 2011-10-05 22:42:07 +0000 | [diff] [blame] | 2250 | |
David Greene | 7be867e | 2011-10-19 13:04:31 +0000 | [diff] [blame] | 2251 | if (DefmPrefix == 0) |
| 2252 | DefmPrefix = StringInit::get(GetNewAnonymousName()); |
| 2253 | |
| 2254 | Init *DefName = DefProto->getNameInit(); |
| 2255 | |
Sean Silva | 6cfc806 | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 2256 | StringInit *DefNameString = dyn_cast<StringInit>(DefName); |
David Greene | 7be867e | 2011-10-19 13:04:31 +0000 | [diff] [blame] | 2257 | |
David Greene | d3d1cad | 2011-10-19 13:04:43 +0000 | [diff] [blame] | 2258 | if (DefNameString != 0) { |
| 2259 | // We have a fully expanded string so there are no operators to |
| 2260 | // resolve. We should concatenate the given prefix and name. |
David Greene | 7be867e | 2011-10-19 13:04:31 +0000 | [diff] [blame] | 2261 | DefName = |
| 2262 | BinOpInit::get(BinOpInit::STRCONCAT, |
| 2263 | UnOpInit::get(UnOpInit::CAST, DefmPrefix, |
| 2264 | StringRecTy::get())->Fold(DefProto, &MC), |
| 2265 | DefName, StringRecTy::get())->Fold(DefProto, &MC); |
| 2266 | } |
David Greene | 7be867e | 2011-10-19 13:04:31 +0000 | [diff] [blame] | 2267 | |
Jakob Stoklund Olesen | 376a8a7 | 2012-08-22 23:33:58 +0000 | [diff] [blame] | 2268 | // Make a trail of SMLocs from the multiclass instantiations. |
| 2269 | SmallVector<SMLoc, 4> Locs(1, DefmPrefixLoc); |
| 2270 | Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end()); |
| 2271 | Record *CurRec = new Record(DefName, Locs, Records); |
David Greene | e499a2d | 2011-10-05 22:42:07 +0000 | [diff] [blame] | 2272 | |
| 2273 | SubClassReference Ref; |
| 2274 | Ref.RefLoc = DefmPrefixLoc; |
| 2275 | Ref.Rec = DefProto; |
| 2276 | AddSubClass(CurRec, Ref); |
| 2277 | |
Jim Grosbach | cfbda4a | 2012-08-02 18:46:42 +0000 | [diff] [blame] | 2278 | // Set the value for NAME. We don't resolve references to it 'til later, |
| 2279 | // though, so that uses in nested multiclass names don't get |
| 2280 | // confused. |
| 2281 | if (SetValue(CurRec, Ref.RefLoc, "NAME", std::vector<unsigned>(), |
| 2282 | DefmPrefix)) { |
| 2283 | Error(DefmPrefixLoc, "Could not resolve " |
| 2284 | + CurRec->getNameInitAsString() + ":NAME to '" |
| 2285 | + DefmPrefix->getAsUnquotedString() + "'"); |
| 2286 | return 0; |
| 2287 | } |
David Greene | e5b252f | 2011-10-19 13:04:35 +0000 | [diff] [blame] | 2288 | |
Jim Grosbach | cfbda4a | 2012-08-02 18:46:42 +0000 | [diff] [blame] | 2289 | // If the DefNameString didn't resolve, we probably have a reference to |
| 2290 | // NAME and need to replace it. We need to do at least this much greedily, |
| 2291 | // otherwise nested multiclasses will end up with incorrect NAME expansions. |
| 2292 | if (DefNameString == 0) { |
David Greene | e5b252f | 2011-10-19 13:04:35 +0000 | [diff] [blame] | 2293 | RecordVal *DefNameRV = CurRec->getValue("NAME"); |
| 2294 | CurRec->resolveReferencesTo(DefNameRV); |
| 2295 | } |
| 2296 | |
| 2297 | if (!CurMultiClass) { |
Jim Grosbach | cfbda4a | 2012-08-02 18:46:42 +0000 | [diff] [blame] | 2298 | // Now that we're at the top level, resolve all NAME references |
| 2299 | // in the resultant defs that weren't in the def names themselves. |
| 2300 | RecordVal *DefNameRV = CurRec->getValue("NAME"); |
| 2301 | CurRec->resolveReferencesTo(DefNameRV); |
| 2302 | |
| 2303 | // Now that NAME references are resolved and we're at the top level of |
| 2304 | // any multiclass expansions, add the record to the RecordKeeper. If we are |
David Greene | e5b252f | 2011-10-19 13:04:35 +0000 | [diff] [blame] | 2305 | // currently in a multiclass, it means this defm appears inside a |
| 2306 | // multiclass and its name won't be fully resolvable until we see |
| 2307 | // the top-level defm. Therefore, we don't add this to the |
| 2308 | // RecordKeeper at this point. If we did we could get duplicate |
| 2309 | // defs as more than one probably refers to NAME or some other |
| 2310 | // common internal placeholder. |
| 2311 | |
| 2312 | // Ensure redefinition doesn't happen. |
| 2313 | if (Records.getDef(CurRec->getNameInitAsString())) { |
| 2314 | Error(DefmPrefixLoc, "def '" + CurRec->getNameInitAsString() + |
| 2315 | "' already defined, instantiating defm with subdef '" + |
| 2316 | DefProto->getNameInitAsString() + "'"); |
| 2317 | return 0; |
| 2318 | } |
| 2319 | |
| 2320 | Records.addDef(CurRec); |
| 2321 | } |
| 2322 | |
David Greene | e499a2d | 2011-10-05 22:42:07 +0000 | [diff] [blame] | 2323 | return CurRec; |
| 2324 | } |
| 2325 | |
| 2326 | bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC, |
| 2327 | Record *CurRec, |
| 2328 | SMLoc DefmPrefixLoc, |
| 2329 | SMLoc SubClassLoc, |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 2330 | const std::vector<Init *> &TArgs, |
David Greene | e499a2d | 2011-10-05 22:42:07 +0000 | [diff] [blame] | 2331 | std::vector<Init *> &TemplateVals, |
| 2332 | bool DeleteArgs) { |
| 2333 | // Loop over all of the template arguments, setting them to the specified |
| 2334 | // value or leaving them as the default if necessary. |
| 2335 | for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { |
| 2336 | // Check if a value is specified for this temp-arg. |
| 2337 | if (i < TemplateVals.size()) { |
| 2338 | // Set it now. |
| 2339 | if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(), |
| 2340 | TemplateVals[i])) |
| 2341 | return true; |
| 2342 | |
| 2343 | // Resolve it next. |
| 2344 | CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i])); |
| 2345 | |
| 2346 | if (DeleteArgs) |
| 2347 | // Now remove it. |
| 2348 | CurRec->removeValue(TArgs[i]); |
| 2349 | |
| 2350 | } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) { |
| 2351 | return Error(SubClassLoc, "value not specified for template argument #"+ |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 2352 | utostr(i) + " (" + TArgs[i]->getAsUnquotedString() |
| 2353 | + ") of multiclassclass '" + MC.Rec.getNameInitAsString() |
| 2354 | + "'"); |
David Greene | e499a2d | 2011-10-05 22:42:07 +0000 | [diff] [blame] | 2355 | } |
| 2356 | } |
| 2357 | return false; |
| 2358 | } |
| 2359 | |
| 2360 | bool TGParser::ResolveMulticlassDef(MultiClass &MC, |
| 2361 | Record *CurRec, |
| 2362 | Record *DefProto, |
| 2363 | SMLoc DefmPrefixLoc) { |
| 2364 | // If the mdef is inside a 'let' expression, add to each def. |
Sean Silva | 9cceede | 2013-01-09 04:49:14 +0000 | [diff] [blame^] | 2365 | if (ApplyLetStack(CurRec)) |
| 2366 | return Error(DefmPrefixLoc, "when instantiating this defm"); |
David Greene | e499a2d | 2011-10-05 22:42:07 +0000 | [diff] [blame] | 2367 | |
David Greene | e499a2d | 2011-10-05 22:42:07 +0000 | [diff] [blame] | 2368 | // Don't create a top level definition for defm inside multiclasses, |
| 2369 | // instead, only update the prototypes and bind the template args |
| 2370 | // with the new created definition. |
| 2371 | if (CurMultiClass) { |
| 2372 | for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); |
| 2373 | i != e; ++i) |
David Greene | 22dde7e | 2011-10-19 13:04:02 +0000 | [diff] [blame] | 2374 | if (CurMultiClass->DefPrototypes[i]->getNameInit() |
| 2375 | == CurRec->getNameInit()) |
| 2376 | return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() + |
David Greene | e499a2d | 2011-10-05 22:42:07 +0000 | [diff] [blame] | 2377 | "' already defined in this multiclass!"); |
| 2378 | CurMultiClass->DefPrototypes.push_back(CurRec); |
| 2379 | |
| 2380 | // Copy the template arguments for the multiclass into the new def. |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 2381 | const std::vector<Init *> &TA = |
David Greene | e499a2d | 2011-10-05 22:42:07 +0000 | [diff] [blame] | 2382 | CurMultiClass->Rec.getTemplateArgs(); |
| 2383 | |
| 2384 | for (unsigned i = 0, e = TA.size(); i != e; ++i) { |
| 2385 | const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]); |
| 2386 | assert(RV && "Template arg doesn't exist?"); |
| 2387 | CurRec->addValue(*RV); |
| 2388 | } |
David Greene | e499a2d | 2011-10-05 22:42:07 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | return false; |
| 2392 | } |
| 2393 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2394 | /// ParseDefm - Parse the instantiation of a multiclass. |
| 2395 | /// |
| 2396 | /// DefMInst ::= DEFM ID ':' DefmSubClassRef ';' |
| 2397 | /// |
Bruno Cardoso Lopes | 270562b | 2010-06-05 02:11:52 +0000 | [diff] [blame] | 2398 | bool TGParser::ParseDefm(MultiClass *CurMultiClass) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2399 | assert(Lex.getCode() == tgtok::Defm && "Unexpected token!"); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2400 | |
David Greene | a9e07dd | 2011-10-19 13:04:29 +0000 | [diff] [blame] | 2401 | Init *DefmPrefix = 0; |
David Greene | a9e07dd | 2011-10-19 13:04:29 +0000 | [diff] [blame] | 2402 | |
Craig Topper | 6a59f5a | 2013-01-07 05:09:33 +0000 | [diff] [blame] | 2403 | if (Lex.Lex() == tgtok::Id) { // eat the defm. |
David Greene | a9e07dd | 2011-10-19 13:04:29 +0000 | [diff] [blame] | 2404 | DefmPrefix = ParseObjectName(CurMultiClass); |
Chris Lattner | df72eae | 2010-10-05 22:51:56 +0000 | [diff] [blame] | 2405 | } |
Mikhail Glushenkov | c761f7d | 2010-10-23 07:32:37 +0000 | [diff] [blame] | 2406 | |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 2407 | SMLoc DefmPrefixLoc = Lex.getLoc(); |
Chris Lattner | df72eae | 2010-10-05 22:51:56 +0000 | [diff] [blame] | 2408 | if (Lex.getCode() != tgtok::colon) |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2409 | return TokError("expected ':' after defm identifier"); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2410 | |
Bruno Cardoso Lopes | 6e0a99a | 2010-06-18 19:53:41 +0000 | [diff] [blame] | 2411 | // Keep track of the new generated record definitions. |
| 2412 | std::vector<Record*> NewRecDefs; |
| 2413 | |
| 2414 | // This record also inherits from a regular class (non-multiclass)? |
| 2415 | bool InheritFromClass = false; |
| 2416 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2417 | // eat the colon. |
| 2418 | Lex.Lex(); |
| 2419 | |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 2420 | SMLoc SubClassLoc = Lex.getLoc(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2421 | SubClassReference Ref = ParseSubClassReference(0, true); |
David Greene | 5654613 | 2009-04-22 22:17:51 +0000 | [diff] [blame] | 2422 | |
| 2423 | while (1) { |
| 2424 | if (Ref.Rec == 0) return true; |
| 2425 | |
| 2426 | // To instantiate a multiclass, we need to first get the multiclass, then |
| 2427 | // instantiate each def contained in the multiclass with the SubClassRef |
| 2428 | // template parameters. |
| 2429 | MultiClass *MC = MultiClasses[Ref.Rec->getName()]; |
| 2430 | assert(MC && "Didn't lookup multiclass correctly?"); |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 2431 | std::vector<Init*> &TemplateVals = Ref.TemplateArgs; |
David Greene | 5654613 | 2009-04-22 22:17:51 +0000 | [diff] [blame] | 2432 | |
| 2433 | // Verify that the correct number of template arguments were specified. |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 2434 | const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs(); |
David Greene | 5654613 | 2009-04-22 22:17:51 +0000 | [diff] [blame] | 2435 | if (TArgs.size() < TemplateVals.size()) |
| 2436 | return Error(SubClassLoc, |
| 2437 | "more template args specified than multiclass expects"); |
| 2438 | |
| 2439 | // Loop over all the def's in the multiclass, instantiating each one. |
| 2440 | for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) { |
| 2441 | Record *DefProto = MC->DefPrototypes[i]; |
| 2442 | |
David Greene | 7be867e | 2011-10-19 13:04:31 +0000 | [diff] [blame] | 2443 | Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix, DefmPrefixLoc); |
Jim Grosbach | 94f2dc9 | 2011-12-02 18:33:03 +0000 | [diff] [blame] | 2444 | if (!CurRec) |
| 2445 | return true; |
David Greene | 065f259 | 2009-05-05 16:28:25 +0000 | [diff] [blame] | 2446 | |
David Greene | e499a2d | 2011-10-05 22:42:07 +0000 | [diff] [blame] | 2447 | if (ResolveMulticlassDefArgs(*MC, CurRec, DefmPrefixLoc, SubClassLoc, |
| 2448 | TArgs, TemplateVals, true/*Delete args*/)) |
| 2449 | return Error(SubClassLoc, "could not instantiate def"); |
David Greene | 5654613 | 2009-04-22 22:17:51 +0000 | [diff] [blame] | 2450 | |
David Greene | e499a2d | 2011-10-05 22:42:07 +0000 | [diff] [blame] | 2451 | if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmPrefixLoc)) |
| 2452 | return Error(SubClassLoc, "could not instantiate def"); |
Bruno Cardoso Lopes | 6e0a99a | 2010-06-18 19:53:41 +0000 | [diff] [blame] | 2453 | |
| 2454 | NewRecDefs.push_back(CurRec); |
David Greene | 5654613 | 2009-04-22 22:17:51 +0000 | [diff] [blame] | 2455 | } |
| 2456 | |
David Greene | e499a2d | 2011-10-05 22:42:07 +0000 | [diff] [blame] | 2457 | |
David Greene | 5654613 | 2009-04-22 22:17:51 +0000 | [diff] [blame] | 2458 | if (Lex.getCode() != tgtok::comma) break; |
| 2459 | Lex.Lex(); // eat ','. |
| 2460 | |
| 2461 | SubClassLoc = Lex.getLoc(); |
Bruno Cardoso Lopes | 6e0a99a | 2010-06-18 19:53:41 +0000 | [diff] [blame] | 2462 | |
| 2463 | // A defm can inherit from regular classes (non-multiclass) as |
| 2464 | // long as they come in the end of the inheritance list. |
| 2465 | InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0); |
| 2466 | |
| 2467 | if (InheritFromClass) |
| 2468 | break; |
| 2469 | |
David Greene | 5654613 | 2009-04-22 22:17:51 +0000 | [diff] [blame] | 2470 | Ref = ParseSubClassReference(0, true); |
| 2471 | } |
| 2472 | |
Bruno Cardoso Lopes | 6e0a99a | 2010-06-18 19:53:41 +0000 | [diff] [blame] | 2473 | if (InheritFromClass) { |
| 2474 | // Process all the classes to inherit as if they were part of a |
| 2475 | // regular 'def' and inherit all record values. |
| 2476 | SubClassReference SubClass = ParseSubClassReference(0, false); |
| 2477 | while (1) { |
| 2478 | // Check for error. |
| 2479 | if (SubClass.Rec == 0) return true; |
| 2480 | |
| 2481 | // Get the expanded definition prototypes and teach them about |
| 2482 | // the record values the current class to inherit has |
| 2483 | for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) { |
| 2484 | Record *CurRec = NewRecDefs[i]; |
| 2485 | |
| 2486 | // Add it. |
| 2487 | if (AddSubClass(CurRec, SubClass)) |
| 2488 | return true; |
| 2489 | |
Sean Silva | 9cceede | 2013-01-09 04:49:14 +0000 | [diff] [blame^] | 2490 | if (ApplyLetStack(CurRec)) |
| 2491 | return true; |
Bruno Cardoso Lopes | 6e0a99a | 2010-06-18 19:53:41 +0000 | [diff] [blame] | 2492 | } |
| 2493 | |
| 2494 | if (Lex.getCode() != tgtok::comma) break; |
| 2495 | Lex.Lex(); // eat ','. |
| 2496 | SubClass = ParseSubClassReference(0, false); |
| 2497 | } |
| 2498 | } |
| 2499 | |
Bruno Cardoso Lopes | e5104ac | 2010-06-22 20:30:50 +0000 | [diff] [blame] | 2500 | if (!CurMultiClass) |
| 2501 | for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) |
David Greene | 0d88640 | 2011-08-10 18:27:46 +0000 | [diff] [blame] | 2502 | // See Record::setName(). This resolve step will see any new |
| 2503 | // name for the def that might have been created when resolving |
| 2504 | // inheritance, values and arguments above. |
Bruno Cardoso Lopes | e5104ac | 2010-06-22 20:30:50 +0000 | [diff] [blame] | 2505 | NewRecDefs[i]->resolveReferences(); |
| 2506 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2507 | if (Lex.getCode() != tgtok::semi) |
| 2508 | return TokError("expected ';' at end of defm"); |
| 2509 | Lex.Lex(); |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2510 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2511 | return false; |
| 2512 | } |
| 2513 | |
| 2514 | /// ParseObject |
| 2515 | /// Object ::= ClassInst |
| 2516 | /// Object ::= DefInst |
| 2517 | /// Object ::= MultiClassInst |
| 2518 | /// Object ::= DefMInst |
| 2519 | /// Object ::= LETCommand '{' ObjectList '}' |
| 2520 | /// Object ::= LETCommand Object |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 2521 | bool TGParser::ParseObject(MultiClass *MC) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2522 | switch (Lex.getCode()) { |
Chris Lattner | d6d9dd9 | 2010-10-31 19:27:15 +0000 | [diff] [blame] | 2523 | default: |
| 2524 | return TokError("Expected class, def, defm, multiclass or let definition"); |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 2525 | case tgtok::Let: return ParseTopLevelLet(MC); |
| 2526 | case tgtok::Def: return ParseDef(MC); |
David Greene | cebb4ee | 2012-02-22 16:09:41 +0000 | [diff] [blame] | 2527 | case tgtok::Foreach: return ParseForeach(MC); |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 2528 | case tgtok::Defm: return ParseDefm(MC); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2529 | case tgtok::Class: return ParseClass(); |
| 2530 | case tgtok::MultiClass: return ParseMultiClass(); |
| 2531 | } |
| 2532 | } |
| 2533 | |
| 2534 | /// ParseObjectList |
| 2535 | /// ObjectList :== Object* |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 2536 | bool TGParser::ParseObjectList(MultiClass *MC) { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2537 | while (isObjectStart(Lex.getCode())) { |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 2538 | if (ParseObject(MC)) |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2539 | return true; |
| 2540 | } |
| 2541 | return false; |
| 2542 | } |
| 2543 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2544 | bool TGParser::ParseFile() { |
| 2545 | Lex.Lex(); // Prime the lexer. |
| 2546 | if (ParseObjectList()) return true; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2547 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2548 | // If we have unread input at the end of the file, report it. |
| 2549 | if (Lex.getCode() == tgtok::Eof) |
| 2550 | return false; |
Bob Wilson | 2187041 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 2551 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 2552 | return TokError("Unexpected input at top level"); |
| 2553 | } |
| 2554 | |