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