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