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