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