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