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