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