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