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