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