blob: 1d1f3603c83c9aa81d2324b56cc75bd779ebeb3d [file] [log] [blame]
Chris Lattnerf4601652007-11-22 20:49:04 +00001//===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf4601652007-11-22 20:49:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Parser for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
14#include "TGParser.h"
Eugene Zelenko9feaa972016-08-23 17:14:32 +000015#include "llvm/ADT/None.h"
Benjamin Kramerd59c5f92015-03-01 21:28:53 +000016#include "llvm/ADT/STLExtras.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/ADT/SmallVector.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000018#include "llvm/ADT/StringExtras.h"
Nico Weber0f38c602018-04-30 14:59:11 +000019#include "llvm/Config/llvm-config.h"
Eugene Zelenko9feaa972016-08-23 17:14:32 +000020#include "llvm/Support/Casting.h"
21#include "llvm/Support/Compiler.h"
22#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/TableGen/Record.h"
Daniel Dunbar1a551802009-07-03 00:10:29 +000025#include <algorithm>
Eugene Zelenko9feaa972016-08-23 17:14:32 +000026#include <cassert>
27#include <cstdint>
28
Chris Lattnerf4601652007-11-22 20:49:04 +000029using namespace llvm;
30
31//===----------------------------------------------------------------------===//
32// Support Code for the Semantic Actions.
33//===----------------------------------------------------------------------===//
34
35namespace llvm {
Eugene Zelenko9feaa972016-08-23 17:14:32 +000036
Chris Lattnerf4601652007-11-22 20:49:04 +000037struct SubClassReference {
Jordan Roseb50df4a2013-01-10 18:50:11 +000038 SMRange RefRange;
Chris Lattnerf4601652007-11-22 20:49:04 +000039 Record *Rec;
Matthias Brauna3f0e482016-12-05 06:41:54 +000040 SmallVector<Init*, 4> TemplateArgs;
Eugene Zelenko9feaa972016-08-23 17:14:32 +000041
Craig Topper8a0d1c82014-04-09 04:50:04 +000042 SubClassReference() : Rec(nullptr) {}
David Greened34a73b2009-04-24 16:55:41 +000043
Craig Topper8a0d1c82014-04-09 04:50:04 +000044 bool isInvalid() const { return Rec == nullptr; }
Chris Lattnerf4601652007-11-22 20:49:04 +000045};
David Greenede444af2009-04-22 16:42:54 +000046
47struct SubMultiClassReference {
Jordan Roseb50df4a2013-01-10 18:50:11 +000048 SMRange RefRange;
David Greenede444af2009-04-22 16:42:54 +000049 MultiClass *MC;
Matthias Brauna3f0e482016-12-05 06:41:54 +000050 SmallVector<Init*, 4> TemplateArgs;
Eugene Zelenko9feaa972016-08-23 17:14:32 +000051
Craig Topper8a0d1c82014-04-09 04:50:04 +000052 SubMultiClassReference() : MC(nullptr) {}
Bob Wilson32558652009-04-28 19:41:44 +000053
Craig Topper8a0d1c82014-04-09 04:50:04 +000054 bool isInvalid() const { return MC == nullptr; }
David Greened34a73b2009-04-24 16:55:41 +000055 void dump() const;
David Greenede444af2009-04-22 16:42:54 +000056};
David Greened34a73b2009-04-24 16:55:41 +000057
Aaron Ballman1d03d382017-10-15 14:32:27 +000058#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Keren55307982016-01-29 20:50:44 +000059LLVM_DUMP_METHOD void SubMultiClassReference::dump() const {
Daniel Dunbar1a551802009-07-03 00:10:29 +000060 errs() << "Multiclass:\n";
Bob Wilson21870412009-11-22 04:24:42 +000061
David Greened34a73b2009-04-24 16:55:41 +000062 MC->dump();
Bob Wilson21870412009-11-22 04:24:42 +000063
Daniel Dunbar1a551802009-07-03 00:10:29 +000064 errs() << "Template args:\n";
Craig Topperdc0b6f22015-05-04 01:35:39 +000065 for (Init *TA : TemplateArgs)
Craig Toppered5293c2015-04-29 04:43:36 +000066 TA->dump();
David Greened34a73b2009-04-24 16:55:41 +000067}
Matthias Braund6da5db2017-01-28 02:47:46 +000068#endif
David Greened34a73b2009-04-24 16:55:41 +000069
Chris Lattnerf4601652007-11-22 20:49:04 +000070} // end namespace llvm
71
Nicolai Haehnle19ebaa72018-03-06 13:48:47 +000072static bool checkBitsConcrete(Record &R, const RecordVal &RV) {
73 BitsInit *BV = cast<BitsInit>(RV.getValue());
74 for (unsigned i = 0, e = BV->getNumBits(); i != e; ++i) {
75 Init *Bit = BV->getBit(i);
76 bool IsReference = false;
77 if (auto VBI = dyn_cast<VarBitInit>(Bit)) {
78 if (auto VI = dyn_cast<VarInit>(VBI->getBitVar())) {
79 if (R.getValue(VI->getName()))
80 IsReference = true;
81 }
82 } else if (isa<VarInit>(Bit)) {
83 IsReference = true;
84 }
85 if (!(IsReference || Bit->isConcrete()))
86 return false;
87 }
88 return true;
89}
90
91static void checkConcrete(Record &R) {
92 for (const RecordVal &RV : R.getValues()) {
93 // HACK: Disable this check for variables declared with 'field'. This is
94 // done merely because existing targets have legitimate cases of
95 // non-concrete variables in helper defs. Ideally, we'd introduce a
96 // 'maybe' or 'optional' modifier instead of this.
97 if (RV.getPrefix())
98 continue;
99
100 if (Init *V = RV.getValue()) {
101 bool Ok = isa<BitsInit>(V) ? checkBitsConcrete(R, RV) : V->isConcrete();
102 if (!Ok) {
103 PrintError(R.getLoc(),
104 Twine("Initializer of '") + RV.getNameInitAsString() +
105 "' in '" + R.getNameInitAsString() +
106 "' could not be fully resolved: " +
107 RV.getValue()->getAsString());
108 }
109 }
110 }
111}
112
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +0000113/// Return an Init with a qualifier prefix referring
114/// to CurRec's name.
115static Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
116 Init *Name, StringRef Scoper) {
117 Init *NewName =
118 BinOpInit::getStrConcat(CurRec.getNameInit(), StringInit::get(Scoper));
119 NewName = BinOpInit::getStrConcat(NewName, Name);
120 if (CurMultiClass && Scoper != "::") {
121 Init *Prefix = BinOpInit::getStrConcat(CurMultiClass->Rec.getNameInit(),
122 StringInit::get("::"));
123 NewName = BinOpInit::getStrConcat(Prefix, NewName);
124 }
125
126 if (BinOpInit *BinOp = dyn_cast<BinOpInit>(NewName))
127 NewName = BinOp->Fold(&CurRec);
128 return NewName;
129}
130
Nicolai Haehnle26db53e2018-06-04 14:26:05 +0000131/// Return the qualified version of the implicit 'NAME' template argument.
132static Init *QualifiedNameOfImplicitName(Record &Rec,
133 MultiClass *MC = nullptr) {
134 return QualifyName(Rec, MC, StringInit::get("NAME"), MC ? "::" : ":");
135}
136
137static Init *QualifiedNameOfImplicitName(MultiClass *MC) {
138 return QualifiedNameOfImplicitName(MC->Rec, MC);
139}
140
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000141bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Craig Topper8a0d1c82014-04-09 04:50:04 +0000142 if (!CurRec)
Chris Lattnerf4601652007-11-22 20:49:04 +0000143 CurRec = &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +0000144
Jakob Stoklund Olesenebaf92c2012-01-13 03:16:35 +0000145 if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000146 // The value already exists in the class, treat this as a set.
147 if (ERV->setValue(RV.getValue()))
148 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
149 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson21870412009-11-22 04:24:42 +0000150 "previous definition of type '" +
Chris Lattnerf4601652007-11-22 20:49:04 +0000151 ERV->getType()->getAsString() + "'");
152 } else {
153 CurRec->addValue(RV);
154 }
155 return false;
156}
157
158/// SetValue -
159/// Return true on error, false on success.
David Greene917924d2011-10-19 13:02:39 +0000160bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
Craig Topper2507b002016-01-04 03:15:08 +0000161 ArrayRef<unsigned> BitList, Init *V,
Craig Topperae626362016-01-04 03:05:14 +0000162 bool AllowSelfAssignment) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000163 if (!V) return false;
164
Craig Topper8a0d1c82014-04-09 04:50:04 +0000165 if (!CurRec) CurRec = &CurMultiClass->Rec;
Chris Lattnerf4601652007-11-22 20:49:04 +0000166
167 RecordVal *RV = CurRec->getValue(ValName);
Craig Topper8a0d1c82014-04-09 04:50:04 +0000168 if (!RV)
Craig Topper21092d82015-04-30 05:54:22 +0000169 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
170 "' unknown!");
Chris Lattnerf4601652007-11-22 20:49:04 +0000171
172 // Do not allow assignments like 'X = X'. This will just cause infinite loops
173 // in the resolution machinery.
174 if (BitList.empty())
Sean Silva6cfc8062012-10-10 20:24:43 +0000175 if (VarInit *VI = dyn_cast<VarInit>(V))
Craig Topperae626362016-01-04 03:05:14 +0000176 if (VI->getNameInit() == ValName && !AllowSelfAssignment)
Nicolai Haehnlea61d1a52018-02-22 15:26:21 +0000177 return Error(Loc, "Recursion / self-assignment forbidden");
Bob Wilson21870412009-11-22 04:24:42 +0000178
Chris Lattnerf4601652007-11-22 20:49:04 +0000179 // If we are assigning to a subset of the bits in the value... then we must be
180 // assigning to a field of BitsRecTy, which must have a BitsInit
181 // initializer.
182 //
183 if (!BitList.empty()) {
Sean Silva6cfc8062012-10-10 20:24:43 +0000184 BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue());
Craig Topper8a0d1c82014-04-09 04:50:04 +0000185 if (!CurVal)
Craig Topper21092d82015-04-30 05:54:22 +0000186 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
187 "' is not a bits type");
Chris Lattnerf4601652007-11-22 20:49:04 +0000188
189 // Convert the incoming value to a bits type of the appropriate size...
Nicolai Haehnlea9e8c1d2018-03-06 13:48:39 +0000190 Init *BI = V->getCastTo(BitsRecTy::get(BitList.size()));
Craig Topperdc0b6f22015-05-04 01:35:39 +0000191 if (!BI)
Chris Lattnerf4601652007-11-22 20:49:04 +0000192 return Error(Loc, "Initializer is not compatible with bit range");
Bob Wilson21870412009-11-22 04:24:42 +0000193
David Greene05bce0b2011-07-29 22:43:06 +0000194 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4601652007-11-22 20:49:04 +0000195
196 // Loop over bits, assigning values as appropriate.
197 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
198 unsigned Bit = BitList[i];
David Greeneca7fd3d2011-07-29 19:07:00 +0000199 if (NewBits[Bit])
Benjamin Kramer24bccaf2015-05-28 11:24:24 +0000200 return Error(Loc, "Cannot set bit #" + Twine(Bit) + " of value '" +
David Greene917924d2011-10-19 13:02:39 +0000201 ValName->getAsUnquotedString() + "' more than once");
Nicolai Haehnlea9e8c1d2018-03-06 13:48:39 +0000202 NewBits[Bit] = BI->getBit(i);
Chris Lattnerf4601652007-11-22 20:49:04 +0000203 }
204
205 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
Craig Topper8a0d1c82014-04-09 04:50:04 +0000206 if (!NewBits[i])
David Greeneca7fd3d2011-07-29 19:07:00 +0000207 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4601652007-11-22 20:49:04 +0000208
David Greenedcd35c72011-07-29 19:07:07 +0000209 V = BitsInit::get(NewBits);
Chris Lattnerf4601652007-11-22 20:49:04 +0000210 }
211
Pete Cooperb71c54f2014-07-31 01:43:57 +0000212 if (RV->setValue(V)) {
Eugene Zelenko9feaa972016-08-23 17:14:32 +0000213 std::string InitType;
Craig Topperdc0b6f22015-05-04 01:35:39 +0000214 if (BitsInit *BI = dyn_cast<BitsInit>(V))
Pete Cooperb71c54f2014-07-31 01:43:57 +0000215 InitType = (Twine("' of type bit initializer with length ") +
216 Twine(BI->getNumBits())).str();
Nicolai Haehnlea61d1a52018-02-22 15:26:21 +0000217 else if (TypedInit *TI = dyn_cast<TypedInit>(V))
218 InitType = (Twine("' of type '") + TI->getType()->getAsString()).str();
Craig Topper21092d82015-04-30 05:54:22 +0000219 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
Nicolai Haehnlea61d1a52018-02-22 15:26:21 +0000220 "' of type '" + RV->getType()->getAsString() +
221 "' is incompatible with initializer '" +
222 V->getAsString() + InitType + "'");
Pete Cooperb71c54f2014-07-31 01:43:57 +0000223 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000224 return false;
225}
226
227/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
228/// args as SubClass's template arguments.
Cedric Venetaff9c272009-02-14 16:06:42 +0000229bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000230 Record *SC = SubClass.Rec;
231 // Add all of the values in the subclass into the current class.
Craig Toppercaa40ec2015-06-02 06:19:28 +0000232 for (const RecordVal &Val : SC->getValues())
233 if (AddValue(CurRec, SubClass.RefRange.Start, Val))
Chris Lattnerf4601652007-11-22 20:49:04 +0000234 return true;
235
Benjamin Kramer55cb91e2015-10-24 12:46:45 +0000236 ArrayRef<Init *> TArgs = SC->getTemplateArgs();
Chris Lattnerf4601652007-11-22 20:49:04 +0000237
238 // Ensure that an appropriate number of template arguments are specified.
239 if (TArgs.size() < SubClass.TemplateArgs.size())
Jordan Roseb50df4a2013-01-10 18:50:11 +0000240 return Error(SubClass.RefRange.Start,
241 "More template args specified than expected");
Bob Wilson21870412009-11-22 04:24:42 +0000242
Chris Lattnerf4601652007-11-22 20:49:04 +0000243 // Loop over all of the template arguments, setting them to the specified
244 // value or leaving them as the default if necessary.
Nicolai Haehnle73eed0f2018-03-05 15:21:11 +0000245 MapResolver R(CurRec);
246
Chris Lattnerf4601652007-11-22 20:49:04 +0000247 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
248 if (i < SubClass.TemplateArgs.size()) {
249 // If a value is specified for this template arg, set it now.
Jordan Roseb50df4a2013-01-10 18:50:11 +0000250 if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
Craig Topper2507b002016-01-04 03:15:08 +0000251 None, SubClass.TemplateArgs[i]))
Chris Lattnerf4601652007-11-22 20:49:04 +0000252 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +0000253 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Jordan Roseb50df4a2013-01-10 18:50:11 +0000254 return Error(SubClass.RefRange.Start,
Craig Topper21092d82015-04-30 05:54:22 +0000255 "Value not specified for template argument #" +
Benjamin Kramer24bccaf2015-05-28 11:24:24 +0000256 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper21092d82015-04-30 05:54:22 +0000257 ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4601652007-11-22 20:49:04 +0000258 }
Nicolai Haehnle73eed0f2018-03-05 15:21:11 +0000259
260 R.set(TArgs[i], CurRec->getValue(TArgs[i])->getValue());
261
262 CurRec->removeValue(TArgs[i]);
Chris Lattnerf4601652007-11-22 20:49:04 +0000263 }
264
Nicolai Haehnle26db53e2018-06-04 14:26:05 +0000265 Init *Name;
266 if (CurRec->isClass())
267 Name =
268 VarInit::get(QualifiedNameOfImplicitName(*CurRec), StringRecTy::get());
269 else
270 Name = CurRec->getNameInit();
271 R.set(QualifiedNameOfImplicitName(*SC), Name);
272
Nicolai Haehnle73eed0f2018-03-05 15:21:11 +0000273 CurRec->resolveReferences(R);
274
Chris Lattnerf4601652007-11-22 20:49:04 +0000275 // Since everything went well, we can now set the "superclass" list for the
276 // current record.
Craig Topper401ee722016-01-18 19:52:37 +0000277 ArrayRef<std::pair<Record *, SMRange>> SCs = SC->getSuperClasses();
278 for (const auto &SCPair : SCs) {
279 if (CurRec->isSubClassOf(SCPair.first))
Jordan Roseb50df4a2013-01-10 18:50:11 +0000280 return Error(SubClass.RefRange.Start,
Craig Topper401ee722016-01-18 19:52:37 +0000281 "Already subclass of '" + SCPair.first->getName() + "'!\n");
282 CurRec->addSuperClass(SCPair.first, SCPair.second);
Chris Lattnerf4601652007-11-22 20:49:04 +0000283 }
Nicolai Haehnleaba588d2018-02-23 11:31:49 +0000284
285 if (CurRec->isSubClassOf(SC))
286 return Error(SubClass.RefRange.Start,
287 "Already subclass of '" + SC->getName() + "'!\n");
288 CurRec->addSuperClass(SC, SubClass.RefRange);
Chris Lattnerf4601652007-11-22 20:49:04 +0000289 return false;
290}
291
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +0000292bool TGParser::AddSubClass(RecordsEntry &Entry, SubClassReference &SubClass) {
293 if (Entry.Rec)
294 return AddSubClass(Entry.Rec.get(), SubClass);
295
296 for (auto &E : Entry.Loop->Entries) {
297 if (AddSubClass(E, SubClass))
298 return true;
299 }
300
301 return false;
302}
303
David Greenede444af2009-04-22 16:42:54 +0000304/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilson440548d2009-04-30 18:26:19 +0000305/// CurMC, resolving its template args as SubMultiClass's
David Greenede444af2009-04-22 16:42:54 +0000306/// template arguments.
Bob Wilson440548d2009-04-30 18:26:19 +0000307bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson1d512df2009-04-30 17:46:20 +0000308 SubMultiClassReference &SubMultiClass) {
David Greenede444af2009-04-22 16:42:54 +0000309 MultiClass *SMC = SubMultiClass.MC;
Bob Wilson32558652009-04-28 19:41:44 +0000310
Benjamin Kramer55cb91e2015-10-24 12:46:45 +0000311 ArrayRef<Init *> SMCTArgs = SMC->Rec.getTemplateArgs();
David Greenede444af2009-04-22 16:42:54 +0000312 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Roseb50df4a2013-01-10 18:50:11 +0000313 return Error(SubMultiClass.RefRange.Start,
David Greened34a73b2009-04-24 16:55:41 +0000314 "More template args specified than expected");
Bob Wilson32558652009-04-28 19:41:44 +0000315
Nicolai Haehnle26db53e2018-06-04 14:26:05 +0000316 // Prepare the mapping of template argument name to value, filling in default
317 // values if necessary.
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +0000318 SubstStack TemplateArgs;
David Greenede444af2009-04-22 16:42:54 +0000319 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
320 if (i < SubMultiClass.TemplateArgs.size()) {
Nicolai Haehnle26db53e2018-06-04 14:26:05 +0000321 TemplateArgs.emplace_back(SMCTArgs[i], SubMultiClass.TemplateArgs[i]);
322 } else {
323 Init *Default = SMC->Rec.getValue(SMCTArgs[i])->getValue();
324 if (!Default->isComplete()) {
325 return Error(SubMultiClass.RefRange.Start,
326 "value not specified for template argument #" + Twine(i) +
327 " (" + SMCTArgs[i]->getAsUnquotedString() +
328 ") of multiclass '" + SMC->Rec.getNameInitAsString() +
329 "'");
David Greenede444af2009-04-22 16:42:54 +0000330 }
Nicolai Haehnle26db53e2018-06-04 14:26:05 +0000331 TemplateArgs.emplace_back(SMCTArgs[i], Default);
David Greenede444af2009-04-22 16:42:54 +0000332 }
Nicolai Haehnle83f929b2018-03-05 15:21:15 +0000333 }
334
Nicolai Haehnle26db53e2018-06-04 14:26:05 +0000335 TemplateArgs.emplace_back(
336 QualifiedNameOfImplicitName(SMC),
337 VarInit::get(QualifiedNameOfImplicitName(CurMC), StringRecTy::get()));
Nicolai Haehnle83f929b2018-03-05 15:21:15 +0000338
Nicolai Haehnle26db53e2018-06-04 14:26:05 +0000339 // Add all of the defs in the subclass into the current multiclass.
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +0000340 return resolve(SMC->Entries, TemplateArgs, false, &CurMC->Entries);
David Greenede444af2009-04-22 16:42:54 +0000341}
342
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +0000343/// Add a record or foreach loop to the current context (global record keeper,
344/// current inner-most foreach loop, or multiclass).
345bool TGParser::addEntry(RecordsEntry E) {
346 assert(!E.Rec || !E.Loop);
Nicolai Haehnle02dea262018-03-21 17:12:53 +0000347
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +0000348 if (!Loops.empty()) {
349 Loops.back()->Entries.push_back(std::move(E));
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000350 return false;
351 }
352
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +0000353 if (E.Loop) {
354 SubstStack Stack;
355 return resolve(*E.Loop, Stack, CurMultiClass == nullptr,
356 CurMultiClass ? &CurMultiClass->Entries : nullptr);
357 }
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000358
Nicolai Haehnle02dea262018-03-21 17:12:53 +0000359 if (CurMultiClass) {
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +0000360 CurMultiClass->Entries.push_back(std::move(E));
Nicolai Haehnle02dea262018-03-21 17:12:53 +0000361 return false;
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000362 }
363
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +0000364 return addDefOne(std::move(E.Rec));
365}
366
367/// Resolve the entries in \p Loop, going over inner loops recursively
368/// and making the given subsitutions of (name, value) pairs.
369///
370/// The resulting records are stored in \p Dest if non-null. Otherwise, they
371/// are added to the global record keeper.
372bool TGParser::resolve(const ForeachLoop &Loop, SubstStack &Substs,
373 bool Final, std::vector<RecordsEntry> *Dest,
374 SMLoc *Loc) {
375 MapResolver R;
376 for (const auto &S : Substs)
377 R.set(S.first, S.second);
378 Init *List = Loop.ListValue->resolveReferences(R);
379 auto LI = dyn_cast<ListInit>(List);
380 if (!LI) {
381 if (!Final) {
382 Dest->emplace_back(make_unique<ForeachLoop>(Loop.Loc, Loop.IterVar,
383 List));
384 return resolve(Loop.Entries, Substs, Final, &Dest->back().Loop->Entries,
385 Loc);
386 }
387
388 PrintError(Loop.Loc, Twine("attempting to loop over '") +
389 List->getAsString() + "', expected a list");
390 return true;
391 }
392
393 bool Error = false;
394 for (auto Elt : *LI) {
395 Substs.emplace_back(Loop.IterVar->getNameInit(), Elt);
396 Error = resolve(Loop.Entries, Substs, Final, Dest);
397 Substs.pop_back();
398 if (Error)
399 break;
400 }
401 return Error;
402}
403
404/// Resolve the entries in \p Source, going over loops recursively and
405/// making the given substitutions of (name, value) pairs.
406///
407/// The resulting records are stored in \p Dest if non-null. Otherwise, they
408/// are added to the global record keeper.
409bool TGParser::resolve(const std::vector<RecordsEntry> &Source,
410 SubstStack &Substs, bool Final,
411 std::vector<RecordsEntry> *Dest, SMLoc *Loc) {
412 bool Error = false;
413 for (auto &E : Source) {
414 if (E.Loop) {
415 Error = resolve(*E.Loop, Substs, Final, Dest);
416 } else {
417 auto Rec = make_unique<Record>(*E.Rec);
418 if (Loc)
419 Rec->appendLoc(*Loc);
420
421 MapResolver R(Rec.get());
422 for (const auto &S : Substs)
423 R.set(S.first, S.second);
424 Rec->resolveReferences(R);
425
426 if (Dest)
427 Dest->push_back(std::move(Rec));
428 else
429 Error = addDefOne(std::move(Rec));
430 }
431 if (Error)
432 break;
433 }
434 return Error;
435}
436
437/// Resolve the record fully and add it to the record keeper.
438bool TGParser::addDefOne(std::unique_ptr<Record> Rec) {
Nicolai Haehnle02dea262018-03-21 17:12:53 +0000439 if (Record *Prev = Records.getDef(Rec->getNameInitAsString())) {
440 if (!Rec->isAnonymous()) {
441 PrintError(Rec->getLoc(),
442 "def already exists: " + Rec->getNameInitAsString());
443 PrintNote(Prev->getLoc(), "location of previous definition");
444 return true;
445 }
446 Rec->setName(Records.getNewAnonymousName());
447 }
448
449 Rec->resolveReferences();
450 checkConcrete(*Rec);
451
Nicolai Haehnle5aba3742018-05-29 17:12:20 +0000452 if (!isa<StringInit>(Rec->getNameInit())) {
453 PrintError(Rec->getLoc(), Twine("record name '") +
454 Rec->getNameInit()->getAsString() +
455 "' could not be fully resolved");
456 return true;
457 }
458
Nicolai Haehnle02dea262018-03-21 17:12:53 +0000459 // If ObjectBody has template arguments, it's an error.
460 assert(Rec->getTemplateArgs().empty() && "How'd this get template args?");
461
462 for (DefsetRecord *Defset : Defsets) {
463 DefInit *I = Rec->getDefInit();
464 if (!I->getType()->typeIsA(Defset->EltTy)) {
465 PrintError(Rec->getLoc(), Twine("adding record of incompatible type '") +
466 I->getType()->getAsString() +
467 "' to defset");
468 PrintNote(Defset->Loc, "location of defset declaration");
469 return true;
470 }
471 Defset->Elements.push_back(I);
472 }
473
474 Records.addDef(std::move(Rec));
David Greenecebb4ee2012-02-22 16:09:41 +0000475 return false;
476}
477
Chris Lattnerf4601652007-11-22 20:49:04 +0000478//===----------------------------------------------------------------------===//
479// Parser Code
480//===----------------------------------------------------------------------===//
481
482/// isObjectStart - Return true if this is a valid first token for an Object.
483static bool isObjectStart(tgtok::TokKind K) {
Nicolai Haehnled66fa2a2018-03-09 12:24:42 +0000484 return K == tgtok::Class || K == tgtok::Def || K == tgtok::Defm ||
485 K == tgtok::Let || K == tgtok::MultiClass || K == tgtok::Foreach ||
486 K == tgtok::Defset;
Chris Lattnerf4601652007-11-22 20:49:04 +0000487}
488
Nicolai Haehnle02dea262018-03-21 17:12:53 +0000489/// ParseObjectName - If a valid object name is specified, return it. If no
490/// name is specified, return the unset initializer. Return nullptr on parse
491/// error.
David Greenea9e07dd2011-10-19 13:04:29 +0000492/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4601652007-11-22 20:49:04 +0000493/// ObjectName ::= /*empty*/
494///
David Greenea9e07dd2011-10-19 13:04:29 +0000495Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
496 switch (Lex.getCode()) {
497 case tgtok::colon:
498 case tgtok::semi:
499 case tgtok::l_brace:
500 // These are all of the tokens that can begin an object body.
501 // Some of these can also begin values but we disallow those cases
502 // because they are unlikely to be useful.
Nicolai Haehnle02dea262018-03-21 17:12:53 +0000503 return UnsetInit::get();
David Greenea9e07dd2011-10-19 13:04:29 +0000504 default:
505 break;
506 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000507
Craig Topper8a0d1c82014-04-09 04:50:04 +0000508 Record *CurRec = nullptr;
David Greenea9e07dd2011-10-19 13:04:29 +0000509 if (CurMultiClass)
510 CurRec = &CurMultiClass->Rec;
511
Nicolai Haehnle26db53e2018-06-04 14:26:05 +0000512 Init *Name = ParseValue(CurRec, StringRecTy::get(), ParseNameMode);
513 if (!Name)
514 return nullptr;
515
516 if (CurMultiClass) {
517 Init *NameStr = QualifiedNameOfImplicitName(CurMultiClass);
518 HasReferenceResolver R(NameStr);
519 Name->resolveReferences(R);
520 if (!R.found())
521 Name = BinOpInit::getStrConcat(VarInit::get(NameStr, StringRecTy::get()),
522 Name);
523 }
524
525 return Name;
Chris Lattnerf4601652007-11-22 20:49:04 +0000526}
527
Chris Lattnerf4601652007-11-22 20:49:04 +0000528/// ParseClassID - Parse and resolve a reference to a class name. This returns
529/// null on error.
530///
531/// ClassID ::= ID
532///
533Record *TGParser::ParseClassID() {
534 if (Lex.getCode() != tgtok::Id) {
535 TokError("expected name for ClassID");
Craig Topper8a0d1c82014-04-09 04:50:04 +0000536 return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000537 }
Bob Wilson21870412009-11-22 04:24:42 +0000538
Chris Lattnerf4601652007-11-22 20:49:04 +0000539 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper8a0d1c82014-04-09 04:50:04 +0000540 if (!Result)
Chris Lattnerf4601652007-11-22 20:49:04 +0000541 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson21870412009-11-22 04:24:42 +0000542
Chris Lattnerf4601652007-11-22 20:49:04 +0000543 Lex.Lex();
544 return Result;
545}
546
Bob Wilson32558652009-04-28 19:41:44 +0000547/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
548/// This returns null on error.
David Greenede444af2009-04-22 16:42:54 +0000549///
550/// MultiClassID ::= ID
551///
552MultiClass *TGParser::ParseMultiClassID() {
553 if (Lex.getCode() != tgtok::Id) {
Sean Silva36febfd2013-01-09 02:11:57 +0000554 TokError("expected name for MultiClassID");
Craig Topper8a0d1c82014-04-09 04:50:04 +0000555 return nullptr;
David Greenede444af2009-04-22 16:42:54 +0000556 }
Bob Wilson32558652009-04-28 19:41:44 +0000557
Craig Topper50083142014-12-11 05:25:30 +0000558 MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
Craig Topper73642a82014-11-30 01:20:17 +0000559 if (!Result)
Sean Silva36febfd2013-01-09 02:11:57 +0000560 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson32558652009-04-28 19:41:44 +0000561
David Greenede444af2009-04-22 16:42:54 +0000562 Lex.Lex();
Craig Topper73642a82014-11-30 01:20:17 +0000563 return Result;
David Greenede444af2009-04-22 16:42:54 +0000564}
565
Chris Lattnerf4601652007-11-22 20:49:04 +0000566/// ParseSubClassReference - Parse a reference to a subclass or to a templated
567/// subclass. This returns a SubClassRefTy with a null Record* on error.
568///
569/// SubClassRef ::= ClassID
570/// SubClassRef ::= ClassID '<' ValueList '>'
571///
572SubClassReference TGParser::
573ParseSubClassReference(Record *CurRec, bool isDefm) {
574 SubClassReference Result;
Jordan Roseb50df4a2013-01-10 18:50:11 +0000575 Result.RefRange.Start = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +0000576
Sean Silva7be90212013-01-09 02:17:14 +0000577 if (isDefm) {
578 if (MultiClass *MC = ParseMultiClassID())
579 Result.Rec = &MC->Rec;
580 } else {
Chris Lattnerf4601652007-11-22 20:49:04 +0000581 Result.Rec = ParseClassID();
Sean Silva7be90212013-01-09 02:17:14 +0000582 }
Craig Topper8a0d1c82014-04-09 04:50:04 +0000583 if (!Result.Rec) return Result;
Bob Wilson21870412009-11-22 04:24:42 +0000584
Chris Lattnerf4601652007-11-22 20:49:04 +0000585 // If there is no template arg list, we're done.
Jordan Roseb50df4a2013-01-10 18:50:11 +0000586 if (Lex.getCode() != tgtok::less) {
587 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000588 return Result;
Jordan Roseb50df4a2013-01-10 18:50:11 +0000589 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000590 Lex.Lex(); // Eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000591
Chris Lattnerf4601652007-11-22 20:49:04 +0000592 if (Lex.getCode() == tgtok::greater) {
593 TokError("subclass reference requires a non-empty list of template values");
Craig Topper8a0d1c82014-04-09 04:50:04 +0000594 Result.Rec = nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000595 return Result;
596 }
Bob Wilson21870412009-11-22 04:24:42 +0000597
Matthias Brauna3f0e482016-12-05 06:41:54 +0000598 ParseValueList(Result.TemplateArgs, CurRec, Result.Rec);
Chris Lattnerf4601652007-11-22 20:49:04 +0000599 if (Result.TemplateArgs.empty()) {
Craig Topper8a0d1c82014-04-09 04:50:04 +0000600 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4601652007-11-22 20:49:04 +0000601 return Result;
602 }
Bob Wilson21870412009-11-22 04:24:42 +0000603
Chris Lattnerf4601652007-11-22 20:49:04 +0000604 if (Lex.getCode() != tgtok::greater) {
605 TokError("expected '>' in template value list");
Craig Topper8a0d1c82014-04-09 04:50:04 +0000606 Result.Rec = nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000607 return Result;
608 }
609 Lex.Lex();
Jordan Roseb50df4a2013-01-10 18:50:11 +0000610 Result.RefRange.End = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +0000611
Chris Lattnerf4601652007-11-22 20:49:04 +0000612 return Result;
613}
614
Bob Wilson32558652009-04-28 19:41:44 +0000615/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
616/// templated submulticlass. This returns a SubMultiClassRefTy with a null
617/// Record* on error.
David Greenede444af2009-04-22 16:42:54 +0000618///
619/// SubMultiClassRef ::= MultiClassID
620/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
621///
622SubMultiClassReference TGParser::
623ParseSubMultiClassReference(MultiClass *CurMC) {
624 SubMultiClassReference Result;
Jordan Roseb50df4a2013-01-10 18:50:11 +0000625 Result.RefRange.Start = Lex.getLoc();
Bob Wilson32558652009-04-28 19:41:44 +0000626
David Greenede444af2009-04-22 16:42:54 +0000627 Result.MC = ParseMultiClassID();
Craig Topper8a0d1c82014-04-09 04:50:04 +0000628 if (!Result.MC) return Result;
Bob Wilson32558652009-04-28 19:41:44 +0000629
David Greenede444af2009-04-22 16:42:54 +0000630 // If there is no template arg list, we're done.
Jordan Roseb50df4a2013-01-10 18:50:11 +0000631 if (Lex.getCode() != tgtok::less) {
632 Result.RefRange.End = Lex.getLoc();
David Greenede444af2009-04-22 16:42:54 +0000633 return Result;
Jordan Roseb50df4a2013-01-10 18:50:11 +0000634 }
David Greenede444af2009-04-22 16:42:54 +0000635 Lex.Lex(); // Eat the '<'
Bob Wilson32558652009-04-28 19:41:44 +0000636
David Greenede444af2009-04-22 16:42:54 +0000637 if (Lex.getCode() == tgtok::greater) {
638 TokError("subclass reference requires a non-empty list of template values");
Craig Topper8a0d1c82014-04-09 04:50:04 +0000639 Result.MC = nullptr;
David Greenede444af2009-04-22 16:42:54 +0000640 return Result;
641 }
Bob Wilson32558652009-04-28 19:41:44 +0000642
Matthias Brauna3f0e482016-12-05 06:41:54 +0000643 ParseValueList(Result.TemplateArgs, &CurMC->Rec, &Result.MC->Rec);
David Greenede444af2009-04-22 16:42:54 +0000644 if (Result.TemplateArgs.empty()) {
Craig Topper8a0d1c82014-04-09 04:50:04 +0000645 Result.MC = nullptr; // Error parsing value list.
David Greenede444af2009-04-22 16:42:54 +0000646 return Result;
647 }
Bob Wilson32558652009-04-28 19:41:44 +0000648
David Greenede444af2009-04-22 16:42:54 +0000649 if (Lex.getCode() != tgtok::greater) {
650 TokError("expected '>' in template value list");
Craig Topper8a0d1c82014-04-09 04:50:04 +0000651 Result.MC = nullptr;
David Greenede444af2009-04-22 16:42:54 +0000652 return Result;
653 }
654 Lex.Lex();
Jordan Roseb50df4a2013-01-10 18:50:11 +0000655 Result.RefRange.End = Lex.getLoc();
David Greenede444af2009-04-22 16:42:54 +0000656
657 return Result;
658}
659
Chris Lattnerf4601652007-11-22 20:49:04 +0000660/// ParseRangePiece - Parse a bit/value range.
661/// RangePiece ::= INTVAL
662/// RangePiece ::= INTVAL '-' INTVAL
663/// RangePiece ::= INTVAL INTVAL
Matthias Brauna3f0e482016-12-05 06:41:54 +0000664bool TGParser::ParseRangePiece(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattner811281e2008-01-10 07:01:53 +0000665 if (Lex.getCode() != tgtok::IntVal) {
666 TokError("expected integer or bitrange");
667 return true;
668 }
Dan Gohman63f97202008-10-17 01:33:43 +0000669 int64_t Start = Lex.getCurIntVal();
670 int64_t End;
Bob Wilson21870412009-11-22 04:24:42 +0000671
Chris Lattnerf4601652007-11-22 20:49:04 +0000672 if (Start < 0)
673 return TokError("invalid range, cannot be negative");
Bob Wilson21870412009-11-22 04:24:42 +0000674
Chris Lattnerf4601652007-11-22 20:49:04 +0000675 switch (Lex.Lex()) { // eat first character.
Bob Wilson21870412009-11-22 04:24:42 +0000676 default:
Chris Lattnerf4601652007-11-22 20:49:04 +0000677 Ranges.push_back(Start);
678 return false;
679 case tgtok::minus:
680 if (Lex.Lex() != tgtok::IntVal) {
681 TokError("expected integer value as end of range");
682 return true;
683 }
684 End = Lex.getCurIntVal();
685 break;
686 case tgtok::IntVal:
687 End = -Lex.getCurIntVal();
688 break;
689 }
Bob Wilson21870412009-11-22 04:24:42 +0000690 if (End < 0)
Chris Lattnerf4601652007-11-22 20:49:04 +0000691 return TokError("invalid range, cannot be negative");
692 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000693
Chris Lattnerf4601652007-11-22 20:49:04 +0000694 // Add to the range.
Craig Topperdc0b6f22015-05-04 01:35:39 +0000695 if (Start < End)
Chris Lattnerf4601652007-11-22 20:49:04 +0000696 for (; Start <= End; ++Start)
697 Ranges.push_back(Start);
Craig Topperdc0b6f22015-05-04 01:35:39 +0000698 else
Chris Lattnerf4601652007-11-22 20:49:04 +0000699 for (; Start >= End; --Start)
700 Ranges.push_back(Start);
Chris Lattnerf4601652007-11-22 20:49:04 +0000701 return false;
702}
703
704/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
705///
706/// RangeList ::= RangePiece (',' RangePiece)*
707///
Matthias Brauna3f0e482016-12-05 06:41:54 +0000708void TGParser::ParseRangeList(SmallVectorImpl<unsigned> &Result) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000709 // Parse the first piece.
Matthias Brauna3f0e482016-12-05 06:41:54 +0000710 if (ParseRangePiece(Result)) {
711 Result.clear();
712 return;
713 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000714 while (Lex.getCode() == tgtok::comma) {
715 Lex.Lex(); // Eat the comma.
716
717 // Parse the next range piece.
Matthias Brauna3f0e482016-12-05 06:41:54 +0000718 if (ParseRangePiece(Result)) {
719 Result.clear();
720 return;
721 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000722 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000723}
724
725/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
726/// OptionalRangeList ::= '<' RangeList '>'
727/// OptionalRangeList ::= /*empty*/
Matthias Brauna3f0e482016-12-05 06:41:54 +0000728bool TGParser::ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000729 if (Lex.getCode() != tgtok::less)
730 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000731
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000732 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000733 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000734
Chris Lattnerf4601652007-11-22 20:49:04 +0000735 // Parse the range list.
Matthias Brauna3f0e482016-12-05 06:41:54 +0000736 ParseRangeList(Ranges);
Chris Lattnerf4601652007-11-22 20:49:04 +0000737 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000738
Chris Lattnerf4601652007-11-22 20:49:04 +0000739 if (Lex.getCode() != tgtok::greater) {
740 TokError("expected '>' at end of range list");
741 return Error(StartLoc, "to match this '<'");
742 }
743 Lex.Lex(); // eat the '>'.
744 return false;
745}
746
747/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
748/// OptionalBitList ::= '{' RangeList '}'
749/// OptionalBitList ::= /*empty*/
Matthias Brauna3f0e482016-12-05 06:41:54 +0000750bool TGParser::ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000751 if (Lex.getCode() != tgtok::l_brace)
752 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000753
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000754 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000755 Lex.Lex(); // eat the '{'
Bob Wilson21870412009-11-22 04:24:42 +0000756
Chris Lattnerf4601652007-11-22 20:49:04 +0000757 // Parse the range list.
Matthias Brauna3f0e482016-12-05 06:41:54 +0000758 ParseRangeList(Ranges);
Chris Lattnerf4601652007-11-22 20:49:04 +0000759 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000760
Chris Lattnerf4601652007-11-22 20:49:04 +0000761 if (Lex.getCode() != tgtok::r_brace) {
762 TokError("expected '}' at end of bit list");
763 return Error(StartLoc, "to match this '{'");
764 }
765 Lex.Lex(); // eat the '}'.
766 return false;
767}
768
Chris Lattnerf4601652007-11-22 20:49:04 +0000769/// ParseType - Parse and return a tblgen type. This returns null on error.
770///
771/// Type ::= STRING // string type
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +0000772/// Type ::= CODE // code type
Chris Lattnerf4601652007-11-22 20:49:04 +0000773/// Type ::= BIT // bit type
774/// Type ::= BITS '<' INTVAL '>' // bits<x> type
775/// Type ::= INT // int type
776/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4601652007-11-22 20:49:04 +0000777/// Type ::= DAG // dag type
778/// Type ::= ClassID // Record Type
779///
780RecTy *TGParser::ParseType() {
781 switch (Lex.getCode()) {
Craig Topper8a0d1c82014-04-09 04:50:04 +0000782 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000783 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Tim Northoveredff0682016-07-05 21:22:55 +0000784 case tgtok::Code: Lex.Lex(); return CodeRecTy::get();
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000785 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
786 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000787 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4601652007-11-22 20:49:04 +0000788 case tgtok::Id:
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000789 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Nicolai Haehnled66fa2a2018-03-09 12:24:42 +0000790 TokError("unknown class name");
Craig Topper8a0d1c82014-04-09 04:50:04 +0000791 return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000792 case tgtok::Bits: {
793 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
794 TokError("expected '<' after bits type");
Craig Topper8a0d1c82014-04-09 04:50:04 +0000795 return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000796 }
797 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
798 TokError("expected integer in bits<n> type");
Craig Topper8a0d1c82014-04-09 04:50:04 +0000799 return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000800 }
Dan Gohman63f97202008-10-17 01:33:43 +0000801 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4601652007-11-22 20:49:04 +0000802 if (Lex.Lex() != tgtok::greater) { // Eat count.
803 TokError("expected '>' at end of bits<n> type");
Craig Topper8a0d1c82014-04-09 04:50:04 +0000804 return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000805 }
806 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000807 return BitsRecTy::get(Val);
Chris Lattnerf4601652007-11-22 20:49:04 +0000808 }
809 case tgtok::List: {
810 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
811 TokError("expected '<' after list type");
Craig Topper8a0d1c82014-04-09 04:50:04 +0000812 return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000813 }
814 Lex.Lex(); // Eat '<'
815 RecTy *SubType = ParseType();
Craig Topper8a0d1c82014-04-09 04:50:04 +0000816 if (!SubType) return nullptr;
Bob Wilson21870412009-11-22 04:24:42 +0000817
Chris Lattnerf4601652007-11-22 20:49:04 +0000818 if (Lex.getCode() != tgtok::greater) {
819 TokError("expected '>' at end of list<ty> type");
Craig Topper8a0d1c82014-04-09 04:50:04 +0000820 return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000821 }
822 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000823 return ListRecTy::get(SubType);
Chris Lattnerf4601652007-11-22 20:49:04 +0000824 }
Bob Wilson21870412009-11-22 04:24:42 +0000825 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000826}
827
Chris Lattnerf4601652007-11-22 20:49:04 +0000828/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
829/// has already been read.
Matthias Braun2b86a872016-12-05 07:35:13 +0000830Init *TGParser::ParseIDValue(Record *CurRec, StringInit *Name, SMLoc NameLoc,
David Greenef3744a02011-10-19 13:04:20 +0000831 IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000832 if (CurRec) {
833 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenedcd35c72011-07-29 19:07:07 +0000834 return VarInit::get(Name, RV->getType());
Matthias Braun2b86a872016-12-05 07:35:13 +0000835 }
Bob Wilson21870412009-11-22 04:24:42 +0000836
Nicolai Haehnle26db53e2018-06-04 14:26:05 +0000837 if ((CurRec && CurRec->isClass()) || CurMultiClass) {
838 Init *TemplateArgName;
839 if (CurMultiClass) {
840 TemplateArgName =
841 QualifyName(CurMultiClass->Rec, CurMultiClass, Name, "::");
842 } else
843 TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
Nicolai Haehnlef3b54ef2018-03-05 14:01:38 +0000844
Nicolai Haehnle26db53e2018-06-04 14:26:05 +0000845 Record *TemplateRec = CurMultiClass ? &CurMultiClass->Rec : CurRec;
846 if (TemplateRec->isTemplateArg(TemplateArgName)) {
847 const RecordVal *RV = TemplateRec->getValue(TemplateArgName);
Chris Lattnerf4601652007-11-22 20:49:04 +0000848 assert(RV && "Template arg doesn't exist??");
Nicolai Haehnle26db53e2018-06-04 14:26:05 +0000849 return VarInit::get(TemplateArgName, RV->getType());
850 } else if (Name->getValue() == "NAME") {
851 return VarInit::get(TemplateArgName, StringRecTy::get());
Chris Lattnerf4601652007-11-22 20:49:04 +0000852 }
853 }
Bob Wilson21870412009-11-22 04:24:42 +0000854
David Greenecebb4ee2012-02-22 16:09:41 +0000855 // If this is in a foreach loop, make sure it's not a loop iterator
Craig Toppered5293c2015-04-29 04:43:36 +0000856 for (const auto &L : Loops) {
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +0000857 VarInit *IterVar = dyn_cast<VarInit>(L->IterVar);
Matthias Braun2b86a872016-12-05 07:35:13 +0000858 if (IterVar && IterVar->getNameInit() == Name)
David Greenecebb4ee2012-02-22 16:09:41 +0000859 return IterVar;
860 }
861
David Greenebbec2792011-10-19 13:04:21 +0000862 if (Mode == ParseNameMode)
Matthias Braun2b86a872016-12-05 07:35:13 +0000863 return Name;
David Greenebbec2792011-10-19 13:04:21 +0000864
Nicolai Haehnled66fa2a2018-03-09 12:24:42 +0000865 if (Init *I = Records.getGlobal(Name->getValue()))
866 return I;
Chris Lattnerf4601652007-11-22 20:49:04 +0000867
Nicolai Haehnle02dea262018-03-21 17:12:53 +0000868 // Allow self-references of concrete defs, but delay the lookup so that we
869 // get the correct type.
Nicolai Haehnle26db53e2018-06-04 14:26:05 +0000870 if (CurRec && !CurRec->isClass() && !CurMultiClass &&
871 CurRec->getNameInit() == Name)
Nicolai Haehnle02dea262018-03-21 17:12:53 +0000872 return UnOpInit::get(UnOpInit::CAST, Name, CurRec->getType());
873
Nicolai Haehnle26db53e2018-06-04 14:26:05 +0000874 Error(NameLoc, "Variable not defined: '" + Name->getValue() + "'");
875 return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000876}
877
David Greened418c1b2009-05-14 20:54:48 +0000878/// ParseOperation - Parse an operator. This returns null on error.
879///
880/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
881///
Matt Arsenault258e8222014-06-10 20:10:08 +0000882Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greened418c1b2009-05-14 20:54:48 +0000883 switch (Lex.getCode()) {
884 default:
885 TokError("unknown operation");
Craig Topper8a0d1c82014-04-09 04:50:04 +0000886 return nullptr;
David Greene1434f662011-01-07 17:05:37 +0000887 case tgtok::XHead:
888 case tgtok::XTail:
Nicolai Haehnlec3435022018-02-23 10:46:07 +0000889 case tgtok::XSize:
David Greene1434f662011-01-07 17:05:37 +0000890 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +0000891 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
892 UnOpInit::UnaryOp Code;
Craig Topper8a0d1c82014-04-09 04:50:04 +0000893 RecTy *Type = nullptr;
David Greened418c1b2009-05-14 20:54:48 +0000894
David Greenee6c27de2009-05-14 21:22:49 +0000895 switch (Lex.getCode()) {
Craig Topper85814382012-02-07 05:05:23 +0000896 default: llvm_unreachable("Unhandled code!");
David Greenee6c27de2009-05-14 21:22:49 +0000897 case tgtok::XCast:
898 Lex.Lex(); // eat the operation
899 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000900
David Greenee6c27de2009-05-14 21:22:49 +0000901 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000902
Craig Topper8a0d1c82014-04-09 04:50:04 +0000903 if (!Type) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000904 TokError("did not get type for unary operator");
Craig Topper8a0d1c82014-04-09 04:50:04 +0000905 return nullptr;
David Greenee6c27de2009-05-14 21:22:49 +0000906 }
David Greened418c1b2009-05-14 20:54:48 +0000907
David Greenee6c27de2009-05-14 21:22:49 +0000908 break;
David Greene1434f662011-01-07 17:05:37 +0000909 case tgtok::XHead:
David Greene5f9f9ba2009-05-14 22:38:31 +0000910 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000911 Code = UnOpInit::HEAD;
David Greene5f9f9ba2009-05-14 22:38:31 +0000912 break;
David Greene1434f662011-01-07 17:05:37 +0000913 case tgtok::XTail:
David Greene5f9f9ba2009-05-14 22:38:31 +0000914 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000915 Code = UnOpInit::TAIL;
David Greene5f9f9ba2009-05-14 22:38:31 +0000916 break;
Nicolai Haehnlec3435022018-02-23 10:46:07 +0000917 case tgtok::XSize:
918 Lex.Lex();
919 Code = UnOpInit::SIZE;
920 Type = IntRecTy::get();
921 break;
David Greene1434f662011-01-07 17:05:37 +0000922 case tgtok::XEmpty:
David Greene5f9f9ba2009-05-14 22:38:31 +0000923 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000924 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000925 Type = IntRecTy::get();
David Greene5f9f9ba2009-05-14 22:38:31 +0000926 break;
David Greenee6c27de2009-05-14 21:22:49 +0000927 }
928 if (Lex.getCode() != tgtok::l_paren) {
929 TokError("expected '(' after unary operator");
Craig Topper8a0d1c82014-04-09 04:50:04 +0000930 return nullptr;
David Greenee6c27de2009-05-14 21:22:49 +0000931 }
932 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000933
David Greene05bce0b2011-07-29 22:43:06 +0000934 Init *LHS = ParseValue(CurRec);
Craig Topper8a0d1c82014-04-09 04:50:04 +0000935 if (!LHS) return nullptr;
David Greened418c1b2009-05-14 20:54:48 +0000936
Craig Topper21092d82015-04-30 05:54:22 +0000937 if (Code == UnOpInit::HEAD ||
938 Code == UnOpInit::TAIL ||
939 Code == UnOpInit::EMPTY) {
Sean Silva6cfc8062012-10-10 20:24:43 +0000940 ListInit *LHSl = dyn_cast<ListInit>(LHS);
941 StringInit *LHSs = dyn_cast<StringInit>(LHS);
942 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper8a0d1c82014-04-09 04:50:04 +0000943 if (!LHSl && !LHSs && !LHSt) {
David Greenee1b46912009-06-08 20:23:18 +0000944 TokError("expected list or string type argument in unary operator");
Craig Topper8a0d1c82014-04-09 04:50:04 +0000945 return nullptr;
David Greene5f9f9ba2009-05-14 22:38:31 +0000946 }
947 if (LHSt) {
Sean Silva736ceac2012-10-05 03:31:58 +0000948 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
949 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper8a0d1c82014-04-09 04:50:04 +0000950 if (!LType && !SType) {
Matt Arsenault090da8f2014-05-31 05:18:52 +0000951 TokError("expected list or string type argument in unary operator");
Craig Topper8a0d1c82014-04-09 04:50:04 +0000952 return nullptr;
David Greene5f9f9ba2009-05-14 22:38:31 +0000953 }
954 }
955
Nicolai Haehnlec3435022018-02-23 10:46:07 +0000956 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL ||
957 Code == UnOpInit::SIZE) {
Craig Topper8a0d1c82014-04-09 04:50:04 +0000958 if (!LHSl && !LHSt) {
Matt Arsenault090da8f2014-05-31 05:18:52 +0000959 TokError("expected list type argument in unary operator");
Craig Topper8a0d1c82014-04-09 04:50:04 +0000960 return nullptr;
David Greenee1b46912009-06-08 20:23:18 +0000961 }
Nicolai Haehnlec3435022018-02-23 10:46:07 +0000962 }
Bob Wilson21870412009-11-22 04:24:42 +0000963
Nicolai Haehnlec3435022018-02-23 10:46:07 +0000964 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
Craig Topperbbf57b32015-05-14 05:53:53 +0000965 if (LHSl && LHSl->empty()) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000966 TokError("empty list argument in unary operator");
Craig Topper8a0d1c82014-04-09 04:50:04 +0000967 return nullptr;
David Greene5f9f9ba2009-05-14 22:38:31 +0000968 }
969 if (LHSl) {
David Greene05bce0b2011-07-29 22:43:06 +0000970 Init *Item = LHSl->getElement(0);
Sean Silva6cfc8062012-10-10 20:24:43 +0000971 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper8a0d1c82014-04-09 04:50:04 +0000972 if (!Itemt) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000973 TokError("untyped list element in unary operator");
Craig Topper8a0d1c82014-04-09 04:50:04 +0000974 return nullptr;
David Greene5f9f9ba2009-05-14 22:38:31 +0000975 }
Craig Topperdc0b6f22015-05-04 01:35:39 +0000976 Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
977 : ListRecTy::get(Itemt->getType());
Bob Wilson21870412009-11-22 04:24:42 +0000978 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000979 assert(LHSt && "expected list type argument in unary operator");
Sean Silva736ceac2012-10-05 03:31:58 +0000980 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper8a0d1c82014-04-09 04:50:04 +0000981 if (!LType) {
Matt Arsenault090da8f2014-05-31 05:18:52 +0000982 TokError("expected list type argument in unary operator");
Craig Topper8a0d1c82014-04-09 04:50:04 +0000983 return nullptr;
David Greene5f9f9ba2009-05-14 22:38:31 +0000984 }
Craig Topperdc0b6f22015-05-04 01:35:39 +0000985 Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
David Greene5f9f9ba2009-05-14 22:38:31 +0000986 }
987 }
988 }
989
David Greenee6c27de2009-05-14 21:22:49 +0000990 if (Lex.getCode() != tgtok::r_paren) {
991 TokError("expected ')' in unary operator");
Craig Topper8a0d1c82014-04-09 04:50:04 +0000992 return nullptr;
David Greenee6c27de2009-05-14 21:22:49 +0000993 }
994 Lex.Lex(); // eat the ')'
Nicolai Haehnlece04a4a2018-03-19 14:13:37 +0000995 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec);
David Greenee6c27de2009-05-14 21:22:49 +0000996 }
David Greened418c1b2009-05-14 20:54:48 +0000997
Nicolai Haehnlea2472db2018-03-09 12:24:06 +0000998 case tgtok::XIsA: {
999 // Value ::= !isa '<' Type '>' '(' Value ')'
1000 Lex.Lex(); // eat the operation
1001
1002 RecTy *Type = ParseOperatorType();
1003 if (!Type)
1004 return nullptr;
1005
1006 if (Lex.getCode() != tgtok::l_paren) {
1007 TokError("expected '(' after type of !isa");
1008 return nullptr;
1009 }
1010 Lex.Lex(); // eat the '('
1011
1012 Init *LHS = ParseValue(CurRec);
1013 if (!LHS)
1014 return nullptr;
1015
1016 if (Lex.getCode() != tgtok::r_paren) {
1017 TokError("expected ')' in !isa");
1018 return nullptr;
1019 }
1020 Lex.Lex(); // eat the ')'
1021
1022 return (IsAOpInit::get(Type, LHS))->Fold();
1023 }
1024
David Greened418c1b2009-05-14 20:54:48 +00001025 case tgtok::XConcat:
Hal Finkeld23a41c2013-01-25 14:49:08 +00001026 case tgtok::XADD:
Joerg Sonnenbergerc754b572014-08-05 09:43:25 +00001027 case tgtok::XAND:
Matt Arsenaultee233182016-11-15 06:49:28 +00001028 case tgtok::XOR:
Bob Wilson21870412009-11-22 04:24:42 +00001029 case tgtok::XSRA:
David Greened418c1b2009-05-14 20:54:48 +00001030 case tgtok::XSRL:
1031 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +00001032 case tgtok::XEq:
Nicolai Haehnleaf0de502018-03-14 11:00:57 +00001033 case tgtok::XNe:
1034 case tgtok::XLe:
1035 case tgtok::XLt:
1036 case tgtok::XGe:
1037 case tgtok::XGt:
Daniel Sandersd80222a2014-05-07 10:13:19 +00001038 case tgtok::XListConcat:
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001039 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner8d978a72010-10-05 23:58:18 +00001040 tgtok::TokKind OpTok = Lex.getCode();
1041 SMLoc OpLoc = Lex.getLoc();
1042 Lex.Lex(); // eat the operation
1043
David Greened418c1b2009-05-14 20:54:48 +00001044 BinOpInit::BinaryOp Code;
Chris Lattner8d978a72010-10-05 23:58:18 +00001045 switch (OpTok) {
Craig Topper85814382012-02-07 05:05:23 +00001046 default: llvm_unreachable("Unhandled code!");
Nicolai Haehnle8ed1fd42018-03-14 11:00:43 +00001047 case tgtok::XConcat: Code = BinOpInit::CONCAT; break;
1048 case tgtok::XADD: Code = BinOpInit::ADD; break;
1049 case tgtok::XAND: Code = BinOpInit::AND; break;
1050 case tgtok::XOR: Code = BinOpInit::OR; break;
1051 case tgtok::XSRA: Code = BinOpInit::SRA; break;
1052 case tgtok::XSRL: Code = BinOpInit::SRL; break;
1053 case tgtok::XSHL: Code = BinOpInit::SHL; break;
1054 case tgtok::XEq: Code = BinOpInit::EQ; break;
Nicolai Haehnleaf0de502018-03-14 11:00:57 +00001055 case tgtok::XNe: Code = BinOpInit::NE; break;
1056 case tgtok::XLe: Code = BinOpInit::LE; break;
1057 case tgtok::XLt: Code = BinOpInit::LT; break;
1058 case tgtok::XGe: Code = BinOpInit::GE; break;
1059 case tgtok::XGt: Code = BinOpInit::GT; break;
Nicolai Haehnle8ed1fd42018-03-14 11:00:43 +00001060 case tgtok::XListConcat: Code = BinOpInit::LISTCONCAT; break;
1061 case tgtok::XStrConcat: Code = BinOpInit::STRCONCAT; break;
1062 }
1063
1064 RecTy *Type = nullptr;
1065 RecTy *ArgType = nullptr;
1066 switch (OpTok) {
1067 default:
1068 llvm_unreachable("Unhandled code!");
1069 case tgtok::XConcat:
1070 Type = DagRecTy::get();
1071 ArgType = DagRecTy::get();
1072 break;
1073 case tgtok::XAND:
1074 case tgtok::XOR:
1075 case tgtok::XSRA:
1076 case tgtok::XSRL:
1077 case tgtok::XSHL:
1078 case tgtok::XADD:
1079 Type = IntRecTy::get();
1080 ArgType = IntRecTy::get();
1081 break;
1082 case tgtok::XEq:
Nicolai Haehnleaf0de502018-03-14 11:00:57 +00001083 case tgtok::XNe:
Nicolai Haehnle8ed1fd42018-03-14 11:00:43 +00001084 Type = BitRecTy::get();
Nicolai Haehnleaf0de502018-03-14 11:00:57 +00001085 // ArgType for Eq / Ne is not known at this point
1086 break;
1087 case tgtok::XLe:
1088 case tgtok::XLt:
1089 case tgtok::XGe:
1090 case tgtok::XGt:
1091 Type = BitRecTy::get();
1092 ArgType = IntRecTy::get();
Nicolai Haehnle8ed1fd42018-03-14 11:00:43 +00001093 break;
Daniel Sandersd80222a2014-05-07 10:13:19 +00001094 case tgtok::XListConcat:
Daniel Sandersd80222a2014-05-07 10:13:19 +00001095 // We don't know the list type until we parse the first argument
Nicolai Haehnle8ed1fd42018-03-14 11:00:43 +00001096 ArgType = ItemType;
Daniel Sandersd80222a2014-05-07 10:13:19 +00001097 break;
Bob Wilson21870412009-11-22 04:24:42 +00001098 case tgtok::XStrConcat:
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +00001099 Type = StringRecTy::get();
Nicolai Haehnle8ed1fd42018-03-14 11:00:43 +00001100 ArgType = StringRecTy::get();
David Greened418c1b2009-05-14 20:54:48 +00001101 break;
David Greened418c1b2009-05-14 20:54:48 +00001102 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +00001103
Nicolai Haehnle8ed1fd42018-03-14 11:00:43 +00001104 if (Type && ItemType && !Type->typeIsConvertibleTo(ItemType)) {
1105 Error(OpLoc, Twine("expected value of type '") +
1106 ItemType->getAsString() + "', got '" +
1107 Type->getAsString() + "'");
1108 return nullptr;
1109 }
1110
David Greened418c1b2009-05-14 20:54:48 +00001111 if (Lex.getCode() != tgtok::l_paren) {
1112 TokError("expected '(' after binary operator");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001113 return nullptr;
David Greened418c1b2009-05-14 20:54:48 +00001114 }
1115 Lex.Lex(); // eat the '('
1116
David Greene05bce0b2011-07-29 22:43:06 +00001117 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +00001118
Nicolai Haehnle8ed1fd42018-03-14 11:00:43 +00001119 for (;;) {
1120 SMLoc InitLoc = Lex.getLoc();
1121 InitList.push_back(ParseValue(CurRec, ArgType));
Craig Topper8a0d1c82014-04-09 04:50:04 +00001122 if (!InitList.back()) return nullptr;
Nicolai Haehnle8ed1fd42018-03-14 11:00:43 +00001123
1124 // All BinOps require their arguments to be of compatible types.
1125 TypedInit *TI = dyn_cast<TypedInit>(InitList.back());
1126 if (!ArgType) {
1127 ArgType = TI->getType();
1128
1129 switch (Code) {
1130 case BinOpInit::LISTCONCAT:
1131 if (!isa<ListRecTy>(ArgType)) {
1132 Error(InitLoc, Twine("expected a list, got value of type '") +
1133 ArgType->getAsString() + "'");
1134 return nullptr;
1135 }
1136 break;
1137 case BinOpInit::EQ:
Nicolai Haehnleaf0de502018-03-14 11:00:57 +00001138 case BinOpInit::NE:
Nicolai Haehnle8ed1fd42018-03-14 11:00:43 +00001139 if (!ArgType->typeIsConvertibleTo(IntRecTy::get()) &&
1140 !ArgType->typeIsConvertibleTo(StringRecTy::get())) {
1141 Error(InitLoc, Twine("expected int, bits, or string; got value of "
1142 "type '") + ArgType->getAsString() + "'");
1143 return nullptr;
1144 }
1145 break;
1146 default: llvm_unreachable("other ops have fixed argument types");
1147 }
1148 } else {
1149 RecTy *Resolved = resolveTypes(ArgType, TI->getType());
1150 if (!Resolved) {
1151 Error(InitLoc, Twine("expected value of type '") +
1152 ArgType->getAsString() + "', got '" +
1153 TI->getType()->getAsString() + "'");
1154 return nullptr;
1155 }
1156 if (Code != BinOpInit::ADD && Code != BinOpInit::AND &&
1157 Code != BinOpInit::OR && Code != BinOpInit::SRA &&
1158 Code != BinOpInit::SRL && Code != BinOpInit::SHL)
1159 ArgType = Resolved;
1160 }
1161
1162 if (Lex.getCode() != tgtok::comma)
1163 break;
1164 Lex.Lex(); // eat the ','
David Greened418c1b2009-05-14 20:54:48 +00001165 }
David Greened418c1b2009-05-14 20:54:48 +00001166
1167 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner8d978a72010-10-05 23:58:18 +00001168 TokError("expected ')' in operator");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001169 return nullptr;
David Greened418c1b2009-05-14 20:54:48 +00001170 }
1171 Lex.Lex(); // eat the ')'
Chris Lattner8d978a72010-10-05 23:58:18 +00001172
Nicolai Haehnle8ed1fd42018-03-14 11:00:43 +00001173 if (Code == BinOpInit::LISTCONCAT)
1174 Type = ArgType;
Daniel Sandersd80222a2014-05-07 10:13:19 +00001175
Chris Lattner8d978a72010-10-05 23:58:18 +00001176 // We allow multiple operands to associative operators like !strconcat as
1177 // shorthand for nesting them.
Nicolai Haehnle8ed1fd42018-03-14 11:00:43 +00001178 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT ||
1179 Code == BinOpInit::CONCAT || Code == BinOpInit::ADD ||
1180 Code == BinOpInit::AND || Code == BinOpInit::OR) {
Chris Lattner8d978a72010-10-05 23:58:18 +00001181 while (InitList.size() > 2) {
David Greene05bce0b2011-07-29 22:43:06 +00001182 Init *RHS = InitList.pop_back_val();
Nicolai Haehnlece04a4a2018-03-19 14:13:37 +00001183 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))->Fold(CurRec);
Chris Lattner8d978a72010-10-05 23:58:18 +00001184 InitList.back() = RHS;
1185 }
1186 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +00001187
Chris Lattner8d978a72010-10-05 23:58:18 +00001188 if (InitList.size() == 2)
David Greenedcd35c72011-07-29 19:07:07 +00001189 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Nicolai Haehnlece04a4a2018-03-19 14:13:37 +00001190 ->Fold(CurRec);
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +00001191
Chris Lattner8d978a72010-10-05 23:58:18 +00001192 Error(OpLoc, "expected two operands to operator");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001193 return nullptr;
David Greened418c1b2009-05-14 20:54:48 +00001194 }
1195
Nicolai Haehnled5cc0e02018-03-05 15:21:04 +00001196 case tgtok::XForEach: { // Value ::= !foreach '(' Id ',' Value ',' Value ')'
1197 SMLoc OpLoc = Lex.getLoc();
1198 Lex.Lex(); // eat the operation
1199 if (Lex.getCode() != tgtok::l_paren) {
1200 TokError("expected '(' after !foreach");
1201 return nullptr;
1202 }
1203
1204 if (Lex.Lex() != tgtok::Id) { // eat the '('
1205 TokError("first argument of !foreach must be an identifier");
1206 return nullptr;
1207 }
1208
1209 Init *LHS = StringInit::get(Lex.getCurStrVal());
1210
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00001211 if (CurRec && CurRec->getValue(LHS)) {
Nicolai Haehnled5cc0e02018-03-05 15:21:04 +00001212 TokError((Twine("iteration variable '") + LHS->getAsString() +
1213 "' already defined")
1214 .str());
1215 return nullptr;
1216 }
1217
1218 if (Lex.Lex() != tgtok::comma) { // eat the id
1219 TokError("expected ',' in ternary operator");
1220 return nullptr;
1221 }
1222 Lex.Lex(); // eat the ','
1223
1224 Init *MHS = ParseValue(CurRec);
1225 if (!MHS)
1226 return nullptr;
1227
1228 if (Lex.getCode() != tgtok::comma) {
1229 TokError("expected ',' in ternary operator");
1230 return nullptr;
1231 }
1232 Lex.Lex(); // eat the ','
1233
1234 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1235 if (!MHSt) {
1236 TokError("could not get type of !foreach input");
1237 return nullptr;
1238 }
1239
1240 RecTy *InEltType = nullptr;
1241 RecTy *OutEltType = nullptr;
1242 bool IsDAG = false;
1243
1244 if (ListRecTy *InListTy = dyn_cast<ListRecTy>(MHSt->getType())) {
1245 InEltType = InListTy->getElementType();
1246 if (ItemType) {
1247 if (ListRecTy *OutListTy = dyn_cast<ListRecTy>(ItemType)) {
1248 OutEltType = OutListTy->getElementType();
1249 } else {
1250 Error(OpLoc,
1251 "expected value of type '" + Twine(ItemType->getAsString()) +
1252 "', but got !foreach of list type");
1253 return nullptr;
1254 }
1255 }
1256 } else if (DagRecTy *InDagTy = dyn_cast<DagRecTy>(MHSt->getType())) {
1257 InEltType = InDagTy;
1258 if (ItemType && !isa<DagRecTy>(ItemType)) {
1259 Error(OpLoc,
1260 "expected value of type '" + Twine(ItemType->getAsString()) +
1261 "', but got !foreach of dag type");
1262 return nullptr;
1263 }
1264 IsDAG = true;
1265 } else {
1266 TokError("!foreach must have list or dag input");
1267 return nullptr;
1268 }
1269
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00001270 // We need to create a temporary record to provide a scope for the iteration
1271 // variable while parsing top-level foreach's.
1272 std::unique_ptr<Record> ParseRecTmp;
1273 Record *ParseRec = CurRec;
1274 if (!ParseRec) {
1275 ParseRecTmp = make_unique<Record>(".parse", ArrayRef<SMLoc>{}, Records);
1276 ParseRec = ParseRecTmp.get();
1277 }
1278
1279 ParseRec->addValue(RecordVal(LHS, InEltType, false));
1280 Init *RHS = ParseValue(ParseRec, OutEltType);
1281 ParseRec->removeValue(LHS);
Nicolai Haehnled5cc0e02018-03-05 15:21:04 +00001282 if (!RHS)
1283 return nullptr;
1284
1285 if (Lex.getCode() != tgtok::r_paren) {
1286 TokError("expected ')' in binary operator");
1287 return nullptr;
1288 }
1289 Lex.Lex(); // eat the ')'
1290
1291 RecTy *OutType;
1292 if (IsDAG) {
1293 OutType = InEltType;
1294 } else {
1295 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1296 if (!RHSt) {
1297 TokError("could not get type of !foreach result");
1298 return nullptr;
1299 }
1300 OutType = RHSt->getType()->getListTy();
1301 }
1302
1303 return (TernOpInit::get(TernOpInit::FOREACH, LHS, MHS, RHS, OutType))
Nicolai Haehnlece04a4a2018-03-19 14:13:37 +00001304 ->Fold(CurRec);
Nicolai Haehnled5cc0e02018-03-05 15:21:04 +00001305 }
1306
Nicolai Haehnle23187642018-03-14 11:00:26 +00001307 case tgtok::XDag:
David Greene9bea7c82009-05-14 23:26:46 +00001308 case tgtok::XIf:
David Greene4afc5092009-05-14 21:54:42 +00001309 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
1310 TernOpInit::TernaryOp Code;
Craig Topper8a0d1c82014-04-09 04:50:04 +00001311 RecTy *Type = nullptr;
David Greened418c1b2009-05-14 20:54:48 +00001312
David Greene4afc5092009-05-14 21:54:42 +00001313 tgtok::TokKind LexCode = Lex.getCode();
1314 Lex.Lex(); // eat the operation
1315 switch (LexCode) {
Craig Topper85814382012-02-07 05:05:23 +00001316 default: llvm_unreachable("Unhandled code!");
Nicolai Haehnle23187642018-03-14 11:00:26 +00001317 case tgtok::XDag:
1318 Code = TernOpInit::DAG;
1319 Type = DagRecTy::get();
1320 ItemType = nullptr;
1321 break;
David Greene9bea7c82009-05-14 23:26:46 +00001322 case tgtok::XIf:
1323 Code = TernOpInit::IF;
1324 break;
David Greene4afc5092009-05-14 21:54:42 +00001325 case tgtok::XSubst:
1326 Code = TernOpInit::SUBST;
1327 break;
1328 }
1329 if (Lex.getCode() != tgtok::l_paren) {
1330 TokError("expected '(' after ternary operator");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001331 return nullptr;
David Greene4afc5092009-05-14 21:54:42 +00001332 }
1333 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +00001334
David Greene05bce0b2011-07-29 22:43:06 +00001335 Init *LHS = ParseValue(CurRec);
Craig Topper8a0d1c82014-04-09 04:50:04 +00001336 if (!LHS) return nullptr;
David Greened418c1b2009-05-14 20:54:48 +00001337
David Greene4afc5092009-05-14 21:54:42 +00001338 if (Lex.getCode() != tgtok::comma) {
1339 TokError("expected ',' in ternary operator");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001340 return nullptr;
David Greene4afc5092009-05-14 21:54:42 +00001341 }
1342 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001343
Nicolai Haehnle23187642018-03-14 11:00:26 +00001344 SMLoc MHSLoc = Lex.getLoc();
Matt Arsenault258e8222014-06-10 20:10:08 +00001345 Init *MHS = ParseValue(CurRec, ItemType);
1346 if (!MHS)
1347 return nullptr;
David Greened418c1b2009-05-14 20:54:48 +00001348
David Greene4afc5092009-05-14 21:54:42 +00001349 if (Lex.getCode() != tgtok::comma) {
1350 TokError("expected ',' in ternary operator");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001351 return nullptr;
David Greene4afc5092009-05-14 21:54:42 +00001352 }
1353 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001354
Nicolai Haehnle23187642018-03-14 11:00:26 +00001355 SMLoc RHSLoc = Lex.getLoc();
Matt Arsenault258e8222014-06-10 20:10:08 +00001356 Init *RHS = ParseValue(CurRec, ItemType);
1357 if (!RHS)
1358 return nullptr;
David Greened418c1b2009-05-14 20:54:48 +00001359
David Greene4afc5092009-05-14 21:54:42 +00001360 if (Lex.getCode() != tgtok::r_paren) {
1361 TokError("expected ')' in binary operator");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001362 return nullptr;
David Greene4afc5092009-05-14 21:54:42 +00001363 }
1364 Lex.Lex(); // eat the ')'
David Greened418c1b2009-05-14 20:54:48 +00001365
David Greene4afc5092009-05-14 21:54:42 +00001366 switch (LexCode) {
Craig Topper85814382012-02-07 05:05:23 +00001367 default: llvm_unreachable("Unhandled code!");
Nicolai Haehnle23187642018-03-14 11:00:26 +00001368 case tgtok::XDag: {
1369 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1370 if (!MHSt && !isa<UnsetInit>(MHS)) {
1371 Error(MHSLoc, "could not determine type of the child list in !dag");
1372 return nullptr;
1373 }
1374 if (MHSt && !isa<ListRecTy>(MHSt->getType())) {
1375 Error(MHSLoc, Twine("expected list of children, got type '") +
1376 MHSt->getType()->getAsString() + "'");
1377 return nullptr;
1378 }
1379
1380 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1381 if (!RHSt && !isa<UnsetInit>(RHS)) {
1382 Error(RHSLoc, "could not determine type of the name list in !dag");
1383 return nullptr;
1384 }
1385 if (RHSt && StringRecTy::get()->getListTy() != RHSt->getType()) {
1386 Error(RHSLoc, Twine("expected list<string>, got type '") +
1387 RHSt->getType()->getAsString() + "'");
1388 return nullptr;
1389 }
1390
1391 if (!MHSt && !RHSt) {
1392 Error(MHSLoc,
1393 "cannot have both unset children and unset names in !dag");
1394 return nullptr;
1395 }
1396 break;
1397 }
David Greene9bea7c82009-05-14 23:26:46 +00001398 case tgtok::XIf: {
Craig Topper8a0d1c82014-04-09 04:50:04 +00001399 RecTy *MHSTy = nullptr;
1400 RecTy *RHSTy = nullptr;
Bill Wendling548f5a0b2010-12-13 01:46:19 +00001401
Sean Silva6cfc8062012-10-10 20:24:43 +00001402 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling548f5a0b2010-12-13 01:46:19 +00001403 MHSTy = MHSt->getType();
Sean Silva6cfc8062012-10-10 20:24:43 +00001404 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao307525c2012-09-06 23:32:48 +00001405 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva3f7b7f82012-10-10 20:24:47 +00001406 if (isa<BitInit>(MHS))
Michael Liao307525c2012-09-06 23:32:48 +00001407 MHSTy = BitRecTy::get();
1408
Sean Silva6cfc8062012-10-10 20:24:43 +00001409 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling548f5a0b2010-12-13 01:46:19 +00001410 RHSTy = RHSt->getType();
Sean Silva6cfc8062012-10-10 20:24:43 +00001411 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao307525c2012-09-06 23:32:48 +00001412 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva3f7b7f82012-10-10 20:24:47 +00001413 if (isa<BitInit>(RHS))
Michael Liao307525c2012-09-06 23:32:48 +00001414 RHSTy = BitRecTy::get();
1415
1416 // For UnsetInit, it's typed from the other hand.
Sean Silva3f7b7f82012-10-10 20:24:47 +00001417 if (isa<UnsetInit>(MHS))
Michael Liao307525c2012-09-06 23:32:48 +00001418 MHSTy = RHSTy;
Sean Silva3f7b7f82012-10-10 20:24:47 +00001419 if (isa<UnsetInit>(RHS))
Michael Liao307525c2012-09-06 23:32:48 +00001420 RHSTy = MHSTy;
Bill Wendling548f5a0b2010-12-13 01:46:19 +00001421
1422 if (!MHSTy || !RHSTy) {
David Greene9bea7c82009-05-14 23:26:46 +00001423 TokError("could not get type for !if");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001424 return nullptr;
David Greene9bea7c82009-05-14 23:26:46 +00001425 }
Bill Wendling548f5a0b2010-12-13 01:46:19 +00001426
Nicolai Haehnlec1dad062018-03-06 13:48:20 +00001427 Type = resolveTypes(MHSTy, RHSTy);
1428 if (!Type) {
1429 TokError(Twine("inconsistent types '") + MHSTy->getAsString() +
1430 "' and '" + RHSTy->getAsString() + "' for !if");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001431 return nullptr;
David Greene9bea7c82009-05-14 23:26:46 +00001432 }
1433 break;
1434 }
David Greene4afc5092009-05-14 21:54:42 +00001435 case tgtok::XSubst: {
Sean Silva6cfc8062012-10-10 20:24:43 +00001436 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper8a0d1c82014-04-09 04:50:04 +00001437 if (!RHSt) {
David Greene4afc5092009-05-14 21:54:42 +00001438 TokError("could not get type for !subst");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001439 return nullptr;
David Greene4afc5092009-05-14 21:54:42 +00001440 }
1441 Type = RHSt->getType();
1442 break;
1443 }
1444 }
Nicolai Haehnlece04a4a2018-03-19 14:13:37 +00001445 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001446 }
Nicolai Haehnle8498a492018-03-06 13:49:16 +00001447
1448 case tgtok::XFoldl: {
1449 // Value ::= !foldl '(' Id ',' Id ',' Value ',' Value ',' Value ')'
1450 Lex.Lex(); // eat the operation
1451 if (Lex.getCode() != tgtok::l_paren) {
1452 TokError("expected '(' after !foldl");
1453 return nullptr;
1454 }
1455 Lex.Lex(); // eat the '('
1456
1457 Init *StartUntyped = ParseValue(CurRec);
1458 if (!StartUntyped)
1459 return nullptr;
1460
1461 TypedInit *Start = dyn_cast<TypedInit>(StartUntyped);
1462 if (!Start) {
1463 TokError(Twine("could not get type of !foldl start: '") +
1464 StartUntyped->getAsString() + "'");
1465 return nullptr;
1466 }
1467
1468 if (Lex.getCode() != tgtok::comma) {
1469 TokError("expected ',' in !foldl");
1470 return nullptr;
1471 }
1472 Lex.Lex(); // eat the ','
1473
1474 Init *ListUntyped = ParseValue(CurRec);
1475 if (!ListUntyped)
1476 return nullptr;
1477
1478 TypedInit *List = dyn_cast<TypedInit>(ListUntyped);
1479 if (!List) {
1480 TokError(Twine("could not get type of !foldl list: '") +
1481 ListUntyped->getAsString() + "'");
1482 return nullptr;
1483 }
1484
1485 ListRecTy *ListType = dyn_cast<ListRecTy>(List->getType());
1486 if (!ListType) {
1487 TokError(Twine("!foldl list must be a list, but is of type '") +
1488 List->getType()->getAsString());
1489 return nullptr;
1490 }
1491
1492 if (Lex.getCode() != tgtok::comma) {
1493 TokError("expected ',' in !foldl");
1494 return nullptr;
1495 }
1496
1497 if (Lex.Lex() != tgtok::Id) { // eat the ','
1498 TokError("third argument of !foldl must be an identifier");
1499 return nullptr;
1500 }
1501
1502 Init *A = StringInit::get(Lex.getCurStrVal());
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00001503 if (CurRec && CurRec->getValue(A)) {
Nicolai Haehnle8498a492018-03-06 13:49:16 +00001504 TokError((Twine("left !foldl variable '") + A->getAsString() +
1505 "' already defined")
1506 .str());
1507 return nullptr;
1508 }
1509
1510 if (Lex.Lex() != tgtok::comma) { // eat the id
1511 TokError("expected ',' in !foldl");
1512 return nullptr;
1513 }
1514
1515 if (Lex.Lex() != tgtok::Id) { // eat the ','
1516 TokError("fourth argument of !foldl must be an identifier");
1517 return nullptr;
1518 }
1519
1520 Init *B = StringInit::get(Lex.getCurStrVal());
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00001521 if (CurRec && CurRec->getValue(B)) {
Nicolai Haehnle8498a492018-03-06 13:49:16 +00001522 TokError((Twine("right !foldl variable '") + B->getAsString() +
1523 "' already defined")
1524 .str());
1525 return nullptr;
1526 }
1527
1528 if (Lex.Lex() != tgtok::comma) { // eat the id
1529 TokError("expected ',' in !foldl");
1530 return nullptr;
1531 }
1532 Lex.Lex(); // eat the ','
1533
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00001534 // We need to create a temporary record to provide a scope for the iteration
1535 // variable while parsing top-level foreach's.
1536 std::unique_ptr<Record> ParseRecTmp;
1537 Record *ParseRec = CurRec;
1538 if (!ParseRec) {
1539 ParseRecTmp = make_unique<Record>(".parse", ArrayRef<SMLoc>{}, Records);
1540 ParseRec = ParseRecTmp.get();
1541 }
1542
1543 ParseRec->addValue(RecordVal(A, Start->getType(), false));
1544 ParseRec->addValue(RecordVal(B, ListType->getElementType(), false));
1545 Init *ExprUntyped = ParseValue(ParseRec);
1546 ParseRec->removeValue(A);
1547 ParseRec->removeValue(B);
Nicolai Haehnle8498a492018-03-06 13:49:16 +00001548 if (!ExprUntyped)
1549 return nullptr;
1550
1551 TypedInit *Expr = dyn_cast<TypedInit>(ExprUntyped);
1552 if (!Expr) {
1553 TokError("could not get type of !foldl expression");
1554 return nullptr;
1555 }
1556
1557 if (Expr->getType() != Start->getType()) {
1558 TokError(Twine("!foldl expression must be of same type as start (") +
1559 Start->getType()->getAsString() + "), but is of type " +
1560 Expr->getType()->getAsString());
1561 return nullptr;
1562 }
1563
1564 if (Lex.getCode() != tgtok::r_paren) {
1565 TokError("expected ')' in fold operator");
1566 return nullptr;
1567 }
1568 Lex.Lex(); // eat the ')'
1569
1570 return FoldOpInit::get(Start, List, A, B, Expr, Start->getType())
1571 ->Fold(CurRec);
1572 }
David Greened418c1b2009-05-14 20:54:48 +00001573 }
David Greened418c1b2009-05-14 20:54:48 +00001574}
1575
1576/// ParseOperatorType - Parse a type for an operator. This returns
1577/// null on error.
1578///
1579/// OperatorType ::= '<' Type '>'
1580///
Dan Gohmana9ad0412009-08-12 22:10:57 +00001581RecTy *TGParser::ParseOperatorType() {
Craig Topper8a0d1c82014-04-09 04:50:04 +00001582 RecTy *Type = nullptr;
David Greened418c1b2009-05-14 20:54:48 +00001583
1584 if (Lex.getCode() != tgtok::less) {
1585 TokError("expected type name for operator");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001586 return nullptr;
David Greened418c1b2009-05-14 20:54:48 +00001587 }
1588 Lex.Lex(); // eat the <
1589
1590 Type = ParseType();
1591
Craig Topper8a0d1c82014-04-09 04:50:04 +00001592 if (!Type) {
David Greened418c1b2009-05-14 20:54:48 +00001593 TokError("expected type name for operator");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001594 return nullptr;
David Greened418c1b2009-05-14 20:54:48 +00001595 }
1596
1597 if (Lex.getCode() != tgtok::greater) {
1598 TokError("expected type name for operator");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001599 return nullptr;
David Greened418c1b2009-05-14 20:54:48 +00001600 }
1601 Lex.Lex(); // eat the >
1602
1603 return Type;
1604}
1605
Chris Lattnerf4601652007-11-22 20:49:04 +00001606/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1607///
1608/// SimpleValue ::= IDValue
1609/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001610/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +00001611/// SimpleValue ::= CODEFRAGMENT
1612/// SimpleValue ::= '?'
1613/// SimpleValue ::= '{' ValueList '}'
1614/// SimpleValue ::= ID '<' ValueListNE '>'
1615/// SimpleValue ::= '[' ValueList ']'
1616/// SimpleValue ::= '(' IDValue DagArgList ')'
1617/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkeld23a41c2013-01-25 14:49:08 +00001618/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001619/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1620/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1621/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sandersd80222a2014-05-07 10:13:19 +00001622/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001623/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1624///
David Greenef3744a02011-10-19 13:04:20 +00001625Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1626 IDParseMode Mode) {
Craig Topper8a0d1c82014-04-09 04:50:04 +00001627 Init *R = nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +00001628 switch (Lex.getCode()) {
1629 default: TokError("Unknown token when parsing a value"); break;
David Greened3d1cad2011-10-19 13:04:43 +00001630 case tgtok::paste:
1631 // This is a leading paste operation. This is deprecated but
1632 // still exists in some .td files. Ignore it.
1633 Lex.Lex(); // Skip '#'.
1634 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenedcd35c72011-07-29 19:07:07 +00001635 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper42c12272014-08-07 05:47:00 +00001636 case tgtok::BinaryIntVal: {
1637 auto BinaryVal = Lex.getCurBinaryIntVal();
1638 SmallVector<Init*, 16> Bits(BinaryVal.second);
1639 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballman6a85b882014-08-07 12:07:33 +00001640 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper42c12272014-08-07 05:47:00 +00001641 R = BitsInit::get(Bits);
1642 Lex.Lex();
1643 break;
1644 }
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001645 case tgtok::StrVal: {
1646 std::string Val = Lex.getCurStrVal();
1647 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001648
Jim Grosbachda4231f2009-03-26 16:17:51 +00001649 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001650 while (Lex.getCode() == tgtok::StrVal) {
1651 Val += Lex.getCurStrVal();
1652 Lex.Lex();
1653 }
Bob Wilson21870412009-11-22 04:24:42 +00001654
David Greenedcd35c72011-07-29 19:07:07 +00001655 R = StringInit::get(Val);
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001656 break;
1657 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001658 case tgtok::CodeFragment:
Tim Northoveredff0682016-07-05 21:22:55 +00001659 R = CodeInit::get(Lex.getCurStrVal());
Chris Lattner578bcf02010-10-06 04:31:40 +00001660 Lex.Lex();
1661 break;
1662 case tgtok::question:
David Greenedcd35c72011-07-29 19:07:07 +00001663 R = UnsetInit::get();
Chris Lattner578bcf02010-10-06 04:31:40 +00001664 Lex.Lex();
1665 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001666 case tgtok::Id: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001667 SMLoc NameLoc = Lex.getLoc();
Matthias Braun2b86a872016-12-05 07:35:13 +00001668 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001669 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greenef3744a02011-10-19 13:04:20 +00001670 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson21870412009-11-22 04:24:42 +00001671
Chris Lattnerf4601652007-11-22 20:49:04 +00001672 // Value ::= ID '<' ValueListNE '>'
1673 if (Lex.Lex() == tgtok::greater) {
1674 TokError("expected non-empty value list");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001675 return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +00001676 }
David Greenee1b46912009-06-08 20:23:18 +00001677
Chris Lattnerf4601652007-11-22 20:49:04 +00001678 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1679 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1680 // body.
Matthias Braun2b86a872016-12-05 07:35:13 +00001681 Record *Class = Records.getClass(Name->getValue());
Chris Lattnerf4601652007-11-22 20:49:04 +00001682 if (!Class) {
Matthias Braun2b86a872016-12-05 07:35:13 +00001683 Error(NameLoc, "Expected a class name, got '" + Name->getValue() + "'");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001684 return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +00001685 }
David Greenee1b46912009-06-08 20:23:18 +00001686
Nicolai Haehnle1f3e2332018-03-06 13:49:01 +00001687 SmallVector<Init *, 8> Args;
1688 ParseValueList(Args, CurRec, Class);
1689 if (Args.empty()) return nullptr;
Bob Wilson21870412009-11-22 04:24:42 +00001690
David Greenee1b46912009-06-08 20:23:18 +00001691 if (Lex.getCode() != tgtok::greater) {
1692 TokError("expected '>' at end of value list");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001693 return nullptr;
David Greenee1b46912009-06-08 20:23:18 +00001694 }
1695 Lex.Lex(); // eat the '>'
Bob Wilson21870412009-11-22 04:24:42 +00001696
Nicolai Haehnle1f3e2332018-03-06 13:49:01 +00001697 // Typecheck the template arguments list
1698 ArrayRef<Init *> ExpectedArgs = Class->getTemplateArgs();
1699 if (ExpectedArgs.size() < Args.size()) {
1700 Error(NameLoc,
1701 "More template args specified than expected");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001702 return nullptr;
Hal Finkel0a3368c2014-01-02 20:47:09 +00001703 }
Bob Wilson21870412009-11-22 04:24:42 +00001704
Nicolai Haehnle1f3e2332018-03-06 13:49:01 +00001705 for (unsigned i = 0, e = ExpectedArgs.size(); i != e; ++i) {
1706 RecordVal *ExpectedArg = Class->getValue(ExpectedArgs[i]);
1707 if (i < Args.size()) {
1708 if (TypedInit *TI = dyn_cast<TypedInit>(Args[i])) {
1709 RecTy *ExpectedType = ExpectedArg->getType();
1710 if (!TI->getType()->typeIsConvertibleTo(ExpectedType)) {
1711 Error(NameLoc,
1712 "Value specified for template argument #" + Twine(i) + " (" +
1713 ExpectedArg->getNameInitAsString() + ") is of type '" +
1714 TI->getType()->getAsString() + "', expected '" +
1715 ExpectedType->getAsString() + "': " + TI->getAsString());
1716 return nullptr;
1717 }
1718 continue;
1719 }
1720 } else if (ExpectedArg->getValue()->isComplete())
1721 continue;
1722
1723 Error(NameLoc,
1724 "Value not specified for template argument #" + Twine(i) + " (" +
1725 ExpectedArgs[i]->getAsUnquotedString() + ")");
1726 return nullptr;
1727 }
1728
1729 return VarDefInit::get(Class, Args)->Fold();
Bob Wilson21870412009-11-22 04:24:42 +00001730 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001731 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001732 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001733 Lex.Lex(); // eat the '{'
Matthias Brauna3f0e482016-12-05 06:41:54 +00001734 SmallVector<Init*, 16> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001735
Chris Lattnerf4601652007-11-22 20:49:04 +00001736 if (Lex.getCode() != tgtok::r_brace) {
Matthias Brauna3f0e482016-12-05 06:41:54 +00001737 ParseValueList(Vals, CurRec);
Craig Topper8a0d1c82014-04-09 04:50:04 +00001738 if (Vals.empty()) return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +00001739 }
1740 if (Lex.getCode() != tgtok::r_brace) {
1741 TokError("expected '}' at end of bit list value");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001742 return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +00001743 }
1744 Lex.Lex(); // eat the '}'
Bob Wilson21870412009-11-22 04:24:42 +00001745
Pete Cooper64a334d2014-08-07 05:47:07 +00001746 SmallVector<Init *, 16> NewBits;
David Greeneca7fd3d2011-07-29 19:07:00 +00001747
Pete Cooper64a334d2014-08-07 05:47:07 +00001748 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1749 // first. We'll first read everything in to a vector, then we can reverse
1750 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4601652007-11-22 20:49:04 +00001751 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat794003c2014-08-29 22:43:30 +00001752 // FIXME: The following two loops would not be duplicated
1753 // if the API was a little more orthogonal.
1754
Pete Cooper64a334d2014-08-07 05:47:07 +00001755 // bits<n> values are allowed to initialize n bits.
1756 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1757 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1758 NewBits.push_back(BI->getBit((e - i) - 1));
1759 continue;
1760 }
Jean-Luc Duprat1da48622014-08-29 19:41:04 +00001761 // bits<n> can also come from variable initializers.
1762 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1763 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1764 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1765 NewBits.push_back(VI->getBit((e - i) - 1));
1766 continue;
1767 }
1768 // Fallthrough to try convert this to a bit.
1769 }
Pete Cooper64a334d2014-08-07 05:47:07 +00001770 // All other values must be convertible to just a single bit.
Nicolai Haehnlea9e8c1d2018-03-06 13:48:39 +00001771 Init *Bit = Vals[i]->getCastTo(BitRecTy::get());
Craig Topper8a0d1c82014-04-09 04:50:04 +00001772 if (!Bit) {
Benjamin Kramer24bccaf2015-05-28 11:24:24 +00001773 Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
Chris Lattner5d814862007-11-22 21:06:59 +00001774 ") is not convertable to a bit");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001775 return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +00001776 }
Pete Cooper64a334d2014-08-07 05:47:07 +00001777 NewBits.push_back(Bit);
Chris Lattnerf4601652007-11-22 20:49:04 +00001778 }
Pete Cooper64a334d2014-08-07 05:47:07 +00001779 std::reverse(NewBits.begin(), NewBits.end());
David Greenedcd35c72011-07-29 19:07:07 +00001780 return BitsInit::get(NewBits);
Chris Lattnerf4601652007-11-22 20:49:04 +00001781 }
1782 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1783 Lex.Lex(); // eat the '['
Matthias Brauna3f0e482016-12-05 06:41:54 +00001784 SmallVector<Init*, 16> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001785
Craig Topper8a0d1c82014-04-09 04:50:04 +00001786 RecTy *DeducedEltTy = nullptr;
1787 ListRecTy *GivenListTy = nullptr;
Bob Wilson21870412009-11-22 04:24:42 +00001788
Craig Topper8a0d1c82014-04-09 04:50:04 +00001789 if (ItemType) {
Sean Silva736ceac2012-10-05 03:31:58 +00001790 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper8a0d1c82014-04-09 04:50:04 +00001791 if (!ListType) {
Benjamin Kramer24bccaf2015-05-28 11:24:24 +00001792 TokError(Twine("Type mismatch for list, expected list type, got ") +
1793 ItemType->getAsString());
Craig Topper8a0d1c82014-04-09 04:50:04 +00001794 return nullptr;
David Greenee1b46912009-06-08 20:23:18 +00001795 }
1796 GivenListTy = ListType;
Bob Wilson21870412009-11-22 04:24:42 +00001797 }
David Greenee1b46912009-06-08 20:23:18 +00001798
Chris Lattnerf4601652007-11-22 20:49:04 +00001799 if (Lex.getCode() != tgtok::r_square) {
Matthias Brauna3f0e482016-12-05 06:41:54 +00001800 ParseValueList(Vals, CurRec, nullptr,
1801 GivenListTy ? GivenListTy->getElementType() : nullptr);
Craig Topper8a0d1c82014-04-09 04:50:04 +00001802 if (Vals.empty()) return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +00001803 }
1804 if (Lex.getCode() != tgtok::r_square) {
1805 TokError("expected ']' at end of list value");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001806 return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +00001807 }
1808 Lex.Lex(); // eat the ']'
David Greenee1b46912009-06-08 20:23:18 +00001809
Craig Topper8a0d1c82014-04-09 04:50:04 +00001810 RecTy *GivenEltTy = nullptr;
David Greenee1b46912009-06-08 20:23:18 +00001811 if (Lex.getCode() == tgtok::less) {
1812 // Optional list element type
1813 Lex.Lex(); // eat the '<'
1814
1815 GivenEltTy = ParseType();
Craig Topper8a0d1c82014-04-09 04:50:04 +00001816 if (!GivenEltTy) {
David Greenee1b46912009-06-08 20:23:18 +00001817 // Couldn't parse element type
Craig Topper8a0d1c82014-04-09 04:50:04 +00001818 return nullptr;
David Greenee1b46912009-06-08 20:23:18 +00001819 }
1820
1821 if (Lex.getCode() != tgtok::greater) {
1822 TokError("expected '>' at end of list element type");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001823 return nullptr;
David Greenee1b46912009-06-08 20:23:18 +00001824 }
1825 Lex.Lex(); // eat the '>'
1826 }
1827
1828 // Check elements
Craig Topper8a0d1c82014-04-09 04:50:04 +00001829 RecTy *EltTy = nullptr;
Craig Toppered5293c2015-04-29 04:43:36 +00001830 for (Init *V : Vals) {
1831 TypedInit *TArg = dyn_cast<TypedInit>(V);
Nicolai Haehnle14236eb2018-03-14 11:00:33 +00001832 if (TArg) {
1833 if (EltTy) {
1834 EltTy = resolveTypes(EltTy, TArg->getType());
1835 if (!EltTy) {
1836 TokError("Incompatible types in list elements");
1837 return nullptr;
1838 }
1839 } else {
1840 EltTy = TArg->getType();
David Greenee1b46912009-06-08 20:23:18 +00001841 }
David Greenee1b46912009-06-08 20:23:18 +00001842 }
1843 }
1844
Craig Topper8a0d1c82014-04-09 04:50:04 +00001845 if (GivenEltTy) {
1846 if (EltTy) {
David Greenee1b46912009-06-08 20:23:18 +00001847 // Verify consistency
1848 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1849 TokError("Incompatible types in list elements");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001850 return nullptr;
David Greenee1b46912009-06-08 20:23:18 +00001851 }
1852 }
1853 EltTy = GivenEltTy;
1854 }
1855
Craig Topper8a0d1c82014-04-09 04:50:04 +00001856 if (!EltTy) {
1857 if (!ItemType) {
David Greenee1b46912009-06-08 20:23:18 +00001858 TokError("No type for list");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001859 return nullptr;
David Greenee1b46912009-06-08 20:23:18 +00001860 }
1861 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +00001862 } else {
David Greenee1b46912009-06-08 20:23:18 +00001863 // Make sure the deduced type is compatible with the given type
1864 if (GivenListTy) {
1865 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
Nicolai Haehnlea61d1a52018-02-22 15:26:21 +00001866 TokError(Twine("Element type mismatch for list: element type '") +
1867 EltTy->getAsString() + "' not convertible to '" +
1868 GivenListTy->getElementType()->getAsString());
Craig Topper8a0d1c82014-04-09 04:50:04 +00001869 return nullptr;
David Greenee1b46912009-06-08 20:23:18 +00001870 }
1871 }
1872 DeducedEltTy = EltTy;
1873 }
Bob Wilson21870412009-11-22 04:24:42 +00001874
David Greenedcd35c72011-07-29 19:07:07 +00001875 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4601652007-11-22 20:49:04 +00001876 }
1877 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1878 Lex.Lex(); // eat the '('
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001879 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001880 TokError("expected identifier in dag init");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001881 return nullptr;
Chris Lattner3dc2e962008-04-10 04:48:34 +00001882 }
Bob Wilson21870412009-11-22 04:24:42 +00001883
David Greene05bce0b2011-07-29 22:43:06 +00001884 Init *Operator = ParseValue(CurRec);
Craig Topper8a0d1c82014-04-09 04:50:04 +00001885 if (!Operator) return nullptr;
David Greenec7cafcd2009-04-22 20:18:10 +00001886
Nate Begeman7cee8172009-03-19 05:21:56 +00001887 // If the operator name is present, parse it.
Matthias Braun205e95012016-12-05 06:00:41 +00001888 StringInit *OperatorName = nullptr;
Nate Begeman7cee8172009-03-19 05:21:56 +00001889 if (Lex.getCode() == tgtok::colon) {
1890 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1891 TokError("expected variable name in dag operator");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001892 return nullptr;
Nate Begeman7cee8172009-03-19 05:21:56 +00001893 }
Matthias Braun205e95012016-12-05 06:00:41 +00001894 OperatorName = StringInit::get(Lex.getCurStrVal());
Nate Begeman7cee8172009-03-19 05:21:56 +00001895 Lex.Lex(); // eat the VarName.
1896 }
Bob Wilson21870412009-11-22 04:24:42 +00001897
Matthias Braun5a87cb22016-12-05 06:41:51 +00001898 SmallVector<std::pair<llvm::Init*, StringInit*>, 8> DagArgs;
Chris Lattnerf4601652007-11-22 20:49:04 +00001899 if (Lex.getCode() != tgtok::r_paren) {
Matthias Braun5a87cb22016-12-05 06:41:51 +00001900 ParseDagArgList(DagArgs, CurRec);
Craig Topper8a0d1c82014-04-09 04:50:04 +00001901 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +00001902 }
Bob Wilson21870412009-11-22 04:24:42 +00001903
Chris Lattnerf4601652007-11-22 20:49:04 +00001904 if (Lex.getCode() != tgtok::r_paren) {
1905 TokError("expected ')' in dag init");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001906 return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +00001907 }
1908 Lex.Lex(); // eat the ')'
Bob Wilson21870412009-11-22 04:24:42 +00001909
David Greenedcd35c72011-07-29 19:07:07 +00001910 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4601652007-11-22 20:49:04 +00001911 }
Bob Wilson21870412009-11-22 04:24:42 +00001912
David Greene1434f662011-01-07 17:05:37 +00001913 case tgtok::XHead:
1914 case tgtok::XTail:
Nicolai Haehnlec3435022018-02-23 10:46:07 +00001915 case tgtok::XSize:
David Greene1434f662011-01-07 17:05:37 +00001916 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +00001917 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Nicolai Haehnlea2472db2018-03-09 12:24:06 +00001918 case tgtok::XIsA:
Chris Lattnerf4601652007-11-22 20:49:04 +00001919 case tgtok::XConcat:
Nicolai Haehnle23187642018-03-14 11:00:26 +00001920 case tgtok::XDag:
Hal Finkeld23a41c2013-01-25 14:49:08 +00001921 case tgtok::XADD:
Joerg Sonnenbergerc754b572014-08-05 09:43:25 +00001922 case tgtok::XAND:
Matt Arsenaultee233182016-11-15 06:49:28 +00001923 case tgtok::XOR:
Bob Wilson21870412009-11-22 04:24:42 +00001924 case tgtok::XSRA:
Chris Lattnerf4601652007-11-22 20:49:04 +00001925 case tgtok::XSRL:
1926 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +00001927 case tgtok::XEq:
Nicolai Haehnleaf0de502018-03-14 11:00:57 +00001928 case tgtok::XNe:
1929 case tgtok::XLe:
1930 case tgtok::XLt:
1931 case tgtok::XGe:
1932 case tgtok::XGt:
Daniel Sandersd80222a2014-05-07 10:13:19 +00001933 case tgtok::XListConcat:
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001934 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene9bea7c82009-05-14 23:26:46 +00001935 case tgtok::XIf:
Nicolai Haehnle8498a492018-03-06 13:49:16 +00001936 case tgtok::XFoldl:
David Greenebeb31a52009-05-14 22:23:47 +00001937 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001938 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenault258e8222014-06-10 20:10:08 +00001939 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4601652007-11-22 20:49:04 +00001940 }
1941 }
Bob Wilson21870412009-11-22 04:24:42 +00001942
Chris Lattnerf4601652007-11-22 20:49:04 +00001943 return R;
1944}
1945
1946/// ParseValue - Parse a tblgen value. This returns null on error.
1947///
1948/// Value ::= SimpleValue ValueSuffix*
1949/// ValueSuffix ::= '{' BitList '}'
1950/// ValueSuffix ::= '[' BitList ']'
1951/// ValueSuffix ::= '.' ID
1952///
David Greenef3744a02011-10-19 13:04:20 +00001953Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1954 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper8a0d1c82014-04-09 04:50:04 +00001955 if (!Result) return nullptr;
Bob Wilson21870412009-11-22 04:24:42 +00001956
Chris Lattnerf4601652007-11-22 20:49:04 +00001957 // Parse the suffixes now if present.
Eugene Zelenko9feaa972016-08-23 17:14:32 +00001958 while (true) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001959 switch (Lex.getCode()) {
1960 default: return Result;
1961 case tgtok::l_brace: {
Nicolai Haehnle691987f2018-03-09 12:24:20 +00001962 if (Mode == ParseNameMode)
David Greene8592b2b2011-10-19 13:04:26 +00001963 // This is the beginning of the object body.
1964 return Result;
1965
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001966 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001967 Lex.Lex(); // eat the '{'
Matthias Brauna3f0e482016-12-05 06:41:54 +00001968 SmallVector<unsigned, 16> Ranges;
1969 ParseRangeList(Ranges);
Craig Topper8a0d1c82014-04-09 04:50:04 +00001970 if (Ranges.empty()) return nullptr;
Bob Wilson21870412009-11-22 04:24:42 +00001971
Chris Lattnerf4601652007-11-22 20:49:04 +00001972 // Reverse the bitlist.
1973 std::reverse(Ranges.begin(), Ranges.end());
1974 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper8a0d1c82014-04-09 04:50:04 +00001975 if (!Result) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001976 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001977 return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +00001978 }
Bob Wilson21870412009-11-22 04:24:42 +00001979
Chris Lattnerf4601652007-11-22 20:49:04 +00001980 // Eat the '}'.
1981 if (Lex.getCode() != tgtok::r_brace) {
1982 TokError("expected '}' at end of bit range list");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001983 return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +00001984 }
1985 Lex.Lex();
1986 break;
1987 }
1988 case tgtok::l_square: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001989 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001990 Lex.Lex(); // eat the '['
Matthias Brauna3f0e482016-12-05 06:41:54 +00001991 SmallVector<unsigned, 16> Ranges;
1992 ParseRangeList(Ranges);
Craig Topper8a0d1c82014-04-09 04:50:04 +00001993 if (Ranges.empty()) return nullptr;
Bob Wilson21870412009-11-22 04:24:42 +00001994
Chris Lattnerf4601652007-11-22 20:49:04 +00001995 Result = Result->convertInitListSlice(Ranges);
Craig Topper8a0d1c82014-04-09 04:50:04 +00001996 if (!Result) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001997 Error(SquareLoc, "Invalid range for list slice");
Craig Topper8a0d1c82014-04-09 04:50:04 +00001998 return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +00001999 }
Bob Wilson21870412009-11-22 04:24:42 +00002000
Chris Lattnerf4601652007-11-22 20:49:04 +00002001 // Eat the ']'.
2002 if (Lex.getCode() != tgtok::r_square) {
2003 TokError("expected ']' at end of list slice");
Craig Topper8a0d1c82014-04-09 04:50:04 +00002004 return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +00002005 }
2006 Lex.Lex();
2007 break;
2008 }
Matthias Braun1ecbef92016-12-05 06:00:36 +00002009 case tgtok::period: {
Chris Lattnerf4601652007-11-22 20:49:04 +00002010 if (Lex.Lex() != tgtok::Id) { // eat the .
2011 TokError("expected field identifier after '.'");
Craig Topper8a0d1c82014-04-09 04:50:04 +00002012 return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +00002013 }
Matthias Braun1ecbef92016-12-05 06:00:36 +00002014 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
2015 if (!Result->getFieldType(FieldName)) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002016 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00002017 Result->getAsString() + "'");
Craig Topper8a0d1c82014-04-09 04:50:04 +00002018 return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +00002019 }
Nicolai Haehnle6bd41df2018-03-19 14:14:28 +00002020 Result = FieldInit::get(Result, FieldName)->Fold(CurRec);
Chris Lattnerf4601652007-11-22 20:49:04 +00002021 Lex.Lex(); // eat field name
2022 break;
Matthias Braun1ecbef92016-12-05 06:00:36 +00002023 }
David Greened3d1cad2011-10-19 13:04:43 +00002024
2025 case tgtok::paste:
2026 SMLoc PasteLoc = Lex.getLoc();
2027
2028 // Create a !strconcat() operation, first casting each operand to
2029 // a string if necessary.
2030
Sean Silva6cfc8062012-10-10 20:24:43 +00002031 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greened3d1cad2011-10-19 13:04:43 +00002032 if (!LHS) {
2033 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper8a0d1c82014-04-09 04:50:04 +00002034 return nullptr;
David Greened3d1cad2011-10-19 13:04:43 +00002035 }
Craig Topperdc0b6f22015-05-04 01:35:39 +00002036
David Greened3d1cad2011-10-19 13:04:43 +00002037 if (LHS->getType() != StringRecTy::get()) {
Nicolai Haehnlee0e26552018-03-19 14:14:04 +00002038 LHS = dyn_cast<TypedInit>(
2039 UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get())
2040 ->Fold(CurRec));
2041 if (!LHS) {
2042 Error(PasteLoc, Twine("can't cast '") + LHS->getAsString() +
2043 "' to string");
2044 return nullptr;
2045 }
David Greened3d1cad2011-10-19 13:04:43 +00002046 }
2047
Craig Topper8a0d1c82014-04-09 04:50:04 +00002048 TypedInit *RHS = nullptr;
David Greened3d1cad2011-10-19 13:04:43 +00002049
2050 Lex.Lex(); // Eat the '#'.
Nicolai Haehnle8498a492018-03-06 13:49:16 +00002051 switch (Lex.getCode()) {
David Greened3d1cad2011-10-19 13:04:43 +00002052 case tgtok::colon:
2053 case tgtok::semi:
2054 case tgtok::l_brace:
2055 // These are all of the tokens that can begin an object body.
2056 // Some of these can also begin values but we disallow those cases
2057 // because they are unlikely to be useful.
Craig Topperdc0b6f22015-05-04 01:35:39 +00002058
David Greened3d1cad2011-10-19 13:04:43 +00002059 // Trailing paste, concat with an empty string.
2060 RHS = StringInit::get("");
2061 break;
2062
2063 default:
Nicolai Haehnle8ed1fd42018-03-14 11:00:43 +00002064 Init *RHSResult = ParseValue(CurRec, nullptr, ParseNameMode);
Sean Silva6cfc8062012-10-10 20:24:43 +00002065 RHS = dyn_cast<TypedInit>(RHSResult);
David Greened3d1cad2011-10-19 13:04:43 +00002066 if (!RHS) {
2067 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper8a0d1c82014-04-09 04:50:04 +00002068 return nullptr;
David Greened3d1cad2011-10-19 13:04:43 +00002069 }
2070
2071 if (RHS->getType() != StringRecTy::get()) {
Nicolai Haehnlee0e26552018-03-19 14:14:04 +00002072 RHS = dyn_cast<TypedInit>(
2073 UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get())
2074 ->Fold(CurRec));
2075 if (!RHS) {
2076 Error(PasteLoc, Twine("can't cast '") + RHS->getAsString() +
2077 "' to string");
2078 return nullptr;
2079 }
David Greened3d1cad2011-10-19 13:04:43 +00002080 }
Craig Topperdc0b6f22015-05-04 01:35:39 +00002081
David Greened3d1cad2011-10-19 13:04:43 +00002082 break;
2083 }
2084
Nicolai Haehnleb50f5592018-03-19 14:13:54 +00002085 Result = BinOpInit::getStrConcat(LHS, RHS);
David Greened3d1cad2011-10-19 13:04:43 +00002086 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00002087 }
2088 }
2089}
2090
2091/// ParseDagArgList - Parse the argument list for a dag literal expression.
2092///
Jakob Stoklund Olesen4717fd42013-03-24 19:36:51 +00002093/// DagArg ::= Value (':' VARNAME)?
2094/// DagArg ::= VARNAME
2095/// DagArgList ::= DagArg
2096/// DagArgList ::= DagArgList ',' DagArg
Matthias Braun5a87cb22016-12-05 06:41:51 +00002097void TGParser::ParseDagArgList(
2098 SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
2099 Record *CurRec) {
Bob Wilson21870412009-11-22 04:24:42 +00002100
Eugene Zelenko9feaa972016-08-23 17:14:32 +00002101 while (true) {
Jakob Stoklund Olesen4717fd42013-03-24 19:36:51 +00002102 // DagArg ::= VARNAME
2103 if (Lex.getCode() == tgtok::VarName) {
2104 // A missing value is treated like '?'.
Matthias Braunddbd6db2016-12-05 06:00:46 +00002105 StringInit *VarName = StringInit::get(Lex.getCurStrVal());
2106 Result.emplace_back(UnsetInit::get(), VarName);
Jakob Stoklund Olesen4717fd42013-03-24 19:36:51 +00002107 Lex.Lex();
2108 } else {
2109 // DagArg ::= Value (':' VARNAME)?
2110 Init *Val = ParseValue(CurRec);
Matthias Braun5a87cb22016-12-05 06:41:51 +00002111 if (!Val) {
2112 Result.clear();
2113 return;
2114 }
Jakob Stoklund Olesen4717fd42013-03-24 19:36:51 +00002115
2116 // If the variable name is present, add it.
Matthias Braunddbd6db2016-12-05 06:00:46 +00002117 StringInit *VarName = nullptr;
Jakob Stoklund Olesen4717fd42013-03-24 19:36:51 +00002118 if (Lex.getCode() == tgtok::colon) {
2119 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
2120 TokError("expected variable name in dag literal");
Matthias Braun5a87cb22016-12-05 06:41:51 +00002121 Result.clear();
2122 return;
Jakob Stoklund Olesen4717fd42013-03-24 19:36:51 +00002123 }
Matthias Braunddbd6db2016-12-05 06:00:46 +00002124 VarName = StringInit::get(Lex.getCurStrVal());
Jakob Stoklund Olesen4717fd42013-03-24 19:36:51 +00002125 Lex.Lex(); // eat the VarName.
Chris Lattnerf4601652007-11-22 20:49:04 +00002126 }
Jakob Stoklund Olesen4717fd42013-03-24 19:36:51 +00002127
2128 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4601652007-11-22 20:49:04 +00002129 }
Chris Lattnerf4601652007-11-22 20:49:04 +00002130 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson21870412009-11-22 04:24:42 +00002131 Lex.Lex(); // eat the ','
Chris Lattnerf4601652007-11-22 20:49:04 +00002132 }
Chris Lattnerf4601652007-11-22 20:49:04 +00002133}
2134
Chris Lattnerf4601652007-11-22 20:49:04 +00002135/// ParseValueList - Parse a comma separated list of values, returning them as a
2136/// vector. Note that this always expects to be able to parse at least one
2137/// value. It returns an empty list if this is not possible.
2138///
2139/// ValueList ::= Value (',' Value)
2140///
Matthias Brauna3f0e482016-12-05 06:41:54 +00002141void TGParser::ParseValueList(SmallVectorImpl<Init*> &Result, Record *CurRec,
2142 Record *ArgsRec, RecTy *EltTy) {
David Greenee1b46912009-06-08 20:23:18 +00002143 RecTy *ItemType = EltTy;
David Greene67acdf22009-06-29 19:59:52 +00002144 unsigned int ArgN = 0;
Craig Topper8a0d1c82014-04-09 04:50:04 +00002145 if (ArgsRec && !EltTy) {
Benjamin Kramer55cb91e2015-10-24 12:46:45 +00002146 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
Alexander Kornienkob4c62672015-01-15 11:41:30 +00002147 if (TArgs.empty()) {
Jim Grosbachb1320cb2012-01-20 20:02:39 +00002148 TokError("template argument provided to non-template class");
Matthias Brauna3f0e482016-12-05 06:41:54 +00002149 Result.clear();
2150 return;
Jim Grosbachb1320cb2012-01-20 20:02:39 +00002151 }
David Greenee1b46912009-06-08 20:23:18 +00002152 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greened9746fe2011-09-19 18:26:07 +00002153 if (!RV) {
2154 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
2155 << ")\n";
2156 }
David Greenee1b46912009-06-08 20:23:18 +00002157 assert(RV && "Template argument record not found??");
2158 ItemType = RV->getType();
2159 ++ArgN;
2160 }
2161 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Brauna3f0e482016-12-05 06:41:54 +00002162 if (!Result.back()) {
2163 Result.clear();
2164 return;
2165 }
Bob Wilson21870412009-11-22 04:24:42 +00002166
Chris Lattnerf4601652007-11-22 20:49:04 +00002167 while (Lex.getCode() == tgtok::comma) {
2168 Lex.Lex(); // Eat the comma
Bob Wilson21870412009-11-22 04:24:42 +00002169
Craig Topper8a0d1c82014-04-09 04:50:04 +00002170 if (ArgsRec && !EltTy) {
Benjamin Kramer55cb91e2015-10-24 12:46:45 +00002171 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
David Greene67acdf22009-06-29 19:59:52 +00002172 if (ArgN >= TArgs.size()) {
2173 TokError("too many template arguments");
Matthias Brauna3f0e482016-12-05 06:41:54 +00002174 Result.clear();
2175 return;
Bob Wilson21870412009-11-22 04:24:42 +00002176 }
David Greenee1b46912009-06-08 20:23:18 +00002177 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
2178 assert(RV && "Template argument record not found??");
2179 ItemType = RV->getType();
2180 ++ArgN;
2181 }
2182 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Brauna3f0e482016-12-05 06:41:54 +00002183 if (!Result.back()) {
2184 Result.clear();
2185 return;
2186 }
Chris Lattnerf4601652007-11-22 20:49:04 +00002187 }
Chris Lattnerf4601652007-11-22 20:49:04 +00002188}
2189
Chris Lattnerf4601652007-11-22 20:49:04 +00002190/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
2191/// empty string on error. This can happen in a number of different context's,
2192/// including within a def or in the template args for a def (which which case
2193/// CurRec will be non-null) and within the template args for a multiclass (in
2194/// which case CurRec will be null, but CurMultiClass will be set). This can
2195/// also happen within a def that is within a multiclass, which will set both
2196/// CurRec and CurMultiClass.
2197///
2198/// Declaration ::= FIELD? Type ID ('=' Value)?
2199///
David Greenee22b3212011-10-19 13:02:42 +00002200Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4601652007-11-22 20:49:04 +00002201 bool ParsingTemplateArgs) {
2202 // Read the field prefix if present.
2203 bool HasField = Lex.getCode() == tgtok::Field;
2204 if (HasField) Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002205
Chris Lattnerf4601652007-11-22 20:49:04 +00002206 RecTy *Type = ParseType();
Craig Topper8a0d1c82014-04-09 04:50:04 +00002207 if (!Type) return nullptr;
Bob Wilson21870412009-11-22 04:24:42 +00002208
Chris Lattnerf4601652007-11-22 20:49:04 +00002209 if (Lex.getCode() != tgtok::Id) {
2210 TokError("Expected identifier in declaration");
Craig Topper8a0d1c82014-04-09 04:50:04 +00002211 return nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +00002212 }
Bob Wilson21870412009-11-22 04:24:42 +00002213
Nicolai Haehnle26db53e2018-06-04 14:26:05 +00002214 std::string Str = Lex.getCurStrVal();
2215 if (Str == "NAME") {
2216 TokError("'" + Str + "' is a reserved variable name");
2217 return nullptr;
2218 }
2219
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002220 SMLoc IdLoc = Lex.getLoc();
Nicolai Haehnle26db53e2018-06-04 14:26:05 +00002221 Init *DeclName = StringInit::get(Str);
Chris Lattnerf4601652007-11-22 20:49:04 +00002222 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002223
Chris Lattnerf4601652007-11-22 20:49:04 +00002224 if (ParsingTemplateArgs) {
Craig Topperdc0b6f22015-05-04 01:35:39 +00002225 if (CurRec)
David Greenee22b3212011-10-19 13:02:42 +00002226 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Craig Topperdc0b6f22015-05-04 01:35:39 +00002227 else
Chris Lattnerf4601652007-11-22 20:49:04 +00002228 assert(CurMultiClass);
Chris Lattnerf4601652007-11-22 20:49:04 +00002229 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +00002230 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
2231 "::");
Chris Lattnerf4601652007-11-22 20:49:04 +00002232 }
Bob Wilson21870412009-11-22 04:24:42 +00002233
Chris Lattnerf4601652007-11-22 20:49:04 +00002234 // Add the value.
2235 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper8a0d1c82014-04-09 04:50:04 +00002236 return nullptr;
Bob Wilson21870412009-11-22 04:24:42 +00002237
Chris Lattnerf4601652007-11-22 20:49:04 +00002238 // If a value is present, parse it.
2239 if (Lex.getCode() == tgtok::equal) {
2240 Lex.Lex();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002241 SMLoc ValLoc = Lex.getLoc();
David Greene05bce0b2011-07-29 22:43:06 +00002242 Init *Val = ParseValue(CurRec, Type);
Craig Topper8a0d1c82014-04-09 04:50:04 +00002243 if (!Val ||
Craig Topper2507b002016-01-04 03:15:08 +00002244 SetValue(CurRec, ValLoc, DeclName, None, Val))
Pete Cooper0a8cc452014-07-31 01:44:00 +00002245 // Return the name, even if an error is thrown. This is so that we can
2246 // continue to make some progress, even without the value having been
2247 // initialized.
2248 return DeclName;
Chris Lattnerf4601652007-11-22 20:49:04 +00002249 }
Bob Wilson21870412009-11-22 04:24:42 +00002250
Chris Lattnerf4601652007-11-22 20:49:04 +00002251 return DeclName;
2252}
2253
David Greenecebb4ee2012-02-22 16:09:41 +00002254/// ParseForeachDeclaration - Read a foreach declaration, returning
2255/// the name of the declared object or a NULL Init on error. Return
2256/// the name of the parsed initializer list through ForeachListName.
2257///
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00002258/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
2259/// ForeachDeclaration ::= ID '=' RangePiece
Nicolai Haehnlead64c882018-03-09 12:24:30 +00002260/// ForeachDeclaration ::= ID '=' Value
David Greenecebb4ee2012-02-22 16:09:41 +00002261///
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00002262VarInit *TGParser::ParseForeachDeclaration(Init *&ForeachListValue) {
David Greenecebb4ee2012-02-22 16:09:41 +00002263 if (Lex.getCode() != tgtok::Id) {
2264 TokError("Expected identifier in foreach declaration");
Craig Topper8a0d1c82014-04-09 04:50:04 +00002265 return nullptr;
David Greenecebb4ee2012-02-22 16:09:41 +00002266 }
2267
2268 Init *DeclName = StringInit::get(Lex.getCurStrVal());
2269 Lex.Lex();
2270
2271 // If a value is present, parse it.
2272 if (Lex.getCode() != tgtok::equal) {
2273 TokError("Expected '=' in foreach declaration");
Craig Topper8a0d1c82014-04-09 04:50:04 +00002274 return nullptr;
David Greenecebb4ee2012-02-22 16:09:41 +00002275 }
2276 Lex.Lex(); // Eat the '='
2277
Craig Topper8a0d1c82014-04-09 04:50:04 +00002278 RecTy *IterType = nullptr;
Matthias Brauna3f0e482016-12-05 06:41:54 +00002279 SmallVector<unsigned, 16> Ranges;
David Greenecebb4ee2012-02-22 16:09:41 +00002280
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00002281 switch (Lex.getCode()) {
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00002282 case tgtok::IntVal: { // RangePiece.
2283 if (ParseRangePiece(Ranges))
Craig Topper8a0d1c82014-04-09 04:50:04 +00002284 return nullptr;
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00002285 break;
David Greenecebb4ee2012-02-22 16:09:41 +00002286 }
2287
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00002288 case tgtok::l_brace: { // '{' RangeList '}'
2289 Lex.Lex(); // eat the '{'
Matthias Brauna3f0e482016-12-05 06:41:54 +00002290 ParseRangeList(Ranges);
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00002291 if (Lex.getCode() != tgtok::r_brace) {
2292 TokError("expected '}' at end of bit range list");
Craig Topper8a0d1c82014-04-09 04:50:04 +00002293 return nullptr;
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00002294 }
2295 Lex.Lex();
2296 break;
2297 }
Nicolai Haehnlead64c882018-03-09 12:24:30 +00002298
2299 default: {
2300 SMLoc ValueLoc = Lex.getLoc();
2301 Init *I = ParseValue(nullptr);
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00002302 TypedInit *TI = dyn_cast<TypedInit>(I);
2303 if (!TI || !isa<ListRecTy>(TI->getType())) {
Nicolai Haehnlead64c882018-03-09 12:24:30 +00002304 std::string Type;
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00002305 if (TI)
Nicolai Haehnlead64c882018-03-09 12:24:30 +00002306 Type = (Twine("' of type '") + TI->getType()->getAsString()).str();
2307 Error(ValueLoc, "expected a list, got '" + I->getAsString() + Type + "'");
Nicolai Haehnle02dea262018-03-21 17:12:53 +00002308 if (CurMultiClass)
2309 PrintNote({}, "references to multiclass template arguments cannot be "
2310 "resolved at this time");
Nicolai Haehnlead64c882018-03-09 12:24:30 +00002311 return nullptr;
2312 }
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00002313 ForeachListValue = I;
2314 IterType = cast<ListRecTy>(TI->getType())->getElementType();
Nicolai Haehnlead64c882018-03-09 12:24:30 +00002315 break;
2316 }
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00002317 }
David Greenecebb4ee2012-02-22 16:09:41 +00002318
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00002319 if (!Ranges.empty()) {
2320 assert(!IterType && "Type already initialized?");
2321 IterType = IntRecTy::get();
2322 std::vector<Init*> Values;
Craig Toppercaa40ec2015-06-02 06:19:28 +00002323 for (unsigned R : Ranges)
2324 Values.push_back(IntInit::get(R));
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00002325 ForeachListValue = ListInit::get(Values, IterType);
2326 }
2327
2328 if (!IterType)
Craig Topper8a0d1c82014-04-09 04:50:04 +00002329 return nullptr;
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00002330
2331 return VarInit::get(DeclName, IterType);
David Greenecebb4ee2012-02-22 16:09:41 +00002332}
2333
Chris Lattnerf4601652007-11-22 20:49:04 +00002334/// ParseTemplateArgList - Read a template argument list, which is a non-empty
2335/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
2336/// template args for a def, which may or may not be in a multiclass. If null,
2337/// these are the template args for a multiclass.
2338///
2339/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson21870412009-11-22 04:24:42 +00002340///
Chris Lattnerf4601652007-11-22 20:49:04 +00002341bool TGParser::ParseTemplateArgList(Record *CurRec) {
2342 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
2343 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +00002344
Chris Lattnerf4601652007-11-22 20:49:04 +00002345 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +00002346
Chris Lattnerf4601652007-11-22 20:49:04 +00002347 // Read the first declaration.
David Greenee22b3212011-10-19 13:02:42 +00002348 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper8a0d1c82014-04-09 04:50:04 +00002349 if (!TemplArg)
Chris Lattnerf4601652007-11-22 20:49:04 +00002350 return true;
Bob Wilson21870412009-11-22 04:24:42 +00002351
Chris Lattnerf4601652007-11-22 20:49:04 +00002352 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson21870412009-11-22 04:24:42 +00002353
Chris Lattnerf4601652007-11-22 20:49:04 +00002354 while (Lex.getCode() == tgtok::comma) {
2355 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00002356
Chris Lattnerf4601652007-11-22 20:49:04 +00002357 // Read the following declarations.
Nicolai Haehnle5aba3742018-05-29 17:12:20 +00002358 SMLoc Loc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002359 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper8a0d1c82014-04-09 04:50:04 +00002360 if (!TemplArg)
Chris Lattnerf4601652007-11-22 20:49:04 +00002361 return true;
Nicolai Haehnle5aba3742018-05-29 17:12:20 +00002362
2363 if (TheRecToAddTo->isTemplateArg(TemplArg))
2364 return Error(Loc, "template argument with the same name has already been "
2365 "defined");
2366
Chris Lattnerf4601652007-11-22 20:49:04 +00002367 TheRecToAddTo->addTemplateArg(TemplArg);
2368 }
Bob Wilson21870412009-11-22 04:24:42 +00002369
Chris Lattnerf4601652007-11-22 20:49:04 +00002370 if (Lex.getCode() != tgtok::greater)
2371 return TokError("expected '>' at end of template argument list");
2372 Lex.Lex(); // eat the '>'.
2373 return false;
2374}
2375
Chris Lattnerf4601652007-11-22 20:49:04 +00002376/// ParseBodyItem - Parse a single item at within the body of a def or class.
2377///
2378/// BodyItem ::= Declaration ';'
2379/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
2380bool TGParser::ParseBodyItem(Record *CurRec) {
2381 if (Lex.getCode() != tgtok::Let) {
Craig Topper8a0d1c82014-04-09 04:50:04 +00002382 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4601652007-11-22 20:49:04 +00002383 return true;
Bob Wilson21870412009-11-22 04:24:42 +00002384
Chris Lattnerf4601652007-11-22 20:49:04 +00002385 if (Lex.getCode() != tgtok::semi)
2386 return TokError("expected ';' after declaration");
2387 Lex.Lex();
2388 return false;
2389 }
2390
2391 // LET ID OptionalRangeList '=' Value ';'
2392 if (Lex.Lex() != tgtok::Id)
2393 return TokError("expected field identifier after let");
Bob Wilson21870412009-11-22 04:24:42 +00002394
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002395 SMLoc IdLoc = Lex.getLoc();
Matthias Braun2b86a872016-12-05 07:35:13 +00002396 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00002397 Lex.Lex(); // eat the field name.
Bob Wilson21870412009-11-22 04:24:42 +00002398
Matthias Brauna3f0e482016-12-05 06:41:54 +00002399 SmallVector<unsigned, 16> BitList;
Bob Wilson21870412009-11-22 04:24:42 +00002400 if (ParseOptionalBitList(BitList))
Chris Lattnerf4601652007-11-22 20:49:04 +00002401 return true;
2402 std::reverse(BitList.begin(), BitList.end());
Bob Wilson21870412009-11-22 04:24:42 +00002403
Chris Lattnerf4601652007-11-22 20:49:04 +00002404 if (Lex.getCode() != tgtok::equal)
2405 return TokError("expected '=' in let expression");
2406 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00002407
David Greenee1b46912009-06-08 20:23:18 +00002408 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper8a0d1c82014-04-09 04:50:04 +00002409 if (!Field)
Matthias Braun2b86a872016-12-05 07:35:13 +00002410 return TokError("Value '" + FieldName->getValue() + "' unknown!");
David Greenee1b46912009-06-08 20:23:18 +00002411
2412 RecTy *Type = Field->getType();
Bob Wilson21870412009-11-22 04:24:42 +00002413
David Greene05bce0b2011-07-29 22:43:06 +00002414 Init *Val = ParseValue(CurRec, Type);
Craig Topper8a0d1c82014-04-09 04:50:04 +00002415 if (!Val) return true;
Bob Wilson21870412009-11-22 04:24:42 +00002416
Chris Lattnerf4601652007-11-22 20:49:04 +00002417 if (Lex.getCode() != tgtok::semi)
2418 return TokError("expected ';' after let expression");
2419 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002420
Chris Lattnerf4601652007-11-22 20:49:04 +00002421 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
2422}
2423
2424/// ParseBody - Read the body of a class or def. Return true on error, false on
2425/// success.
2426///
2427/// Body ::= ';'
2428/// Body ::= '{' BodyList '}'
2429/// BodyList BodyItem*
2430///
2431bool TGParser::ParseBody(Record *CurRec) {
2432 // If this is a null definition, just eat the semi and return.
2433 if (Lex.getCode() == tgtok::semi) {
2434 Lex.Lex();
2435 return false;
2436 }
Bob Wilson21870412009-11-22 04:24:42 +00002437
Chris Lattnerf4601652007-11-22 20:49:04 +00002438 if (Lex.getCode() != tgtok::l_brace)
2439 return TokError("Expected ';' or '{' to start body");
2440 // Eat the '{'.
2441 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002442
Chris Lattnerf4601652007-11-22 20:49:04 +00002443 while (Lex.getCode() != tgtok::r_brace)
2444 if (ParseBodyItem(CurRec))
2445 return true;
2446
2447 // Eat the '}'.
2448 Lex.Lex();
2449 return false;
2450}
2451
Adrian Prantl26b584c2018-05-01 15:54:18 +00002452/// Apply the current let bindings to \a CurRec.
Sean Silva9cceede2013-01-09 04:49:14 +00002453/// \returns true on error, false otherwise.
2454bool TGParser::ApplyLetStack(Record *CurRec) {
Matthias Brauna3f0e482016-12-05 06:41:54 +00002455 for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
Craig Toppercaa40ec2015-06-02 06:19:28 +00002456 for (LetRecord &LR : LetInfo)
2457 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
Sean Silva9cceede2013-01-09 04:49:14 +00002458 return true;
2459 return false;
2460}
2461
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00002462bool TGParser::ApplyLetStack(RecordsEntry &Entry) {
2463 if (Entry.Rec)
2464 return ApplyLetStack(Entry.Rec.get());
2465
2466 for (auto &E : Entry.Loop->Entries) {
2467 if (ApplyLetStack(E))
2468 return true;
2469 }
2470
2471 return false;
2472}
2473
Chris Lattnerf4601652007-11-22 20:49:04 +00002474/// ParseObjectBody - Parse the body of a def or class. This consists of an
2475/// optional ClassList followed by a Body. CurRec is the current def or class
2476/// that is being parsed.
2477///
2478/// ObjectBody ::= BaseClassList Body
2479/// BaseClassList ::= /*empty*/
2480/// BaseClassList ::= ':' BaseClassListNE
2481/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
2482///
2483bool TGParser::ParseObjectBody(Record *CurRec) {
2484 // If there is a baseclass list, read it.
2485 if (Lex.getCode() == tgtok::colon) {
2486 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002487
Chris Lattnerf4601652007-11-22 20:49:04 +00002488 // Read all of the subclasses.
2489 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
Eugene Zelenko9feaa972016-08-23 17:14:32 +00002490 while (true) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002491 // Check for error.
Craig Topper8a0d1c82014-04-09 04:50:04 +00002492 if (!SubClass.Rec) return true;
Bob Wilson21870412009-11-22 04:24:42 +00002493
Chris Lattnerf4601652007-11-22 20:49:04 +00002494 // Add it.
2495 if (AddSubClass(CurRec, SubClass))
2496 return true;
Bob Wilson21870412009-11-22 04:24:42 +00002497
Chris Lattnerf4601652007-11-22 20:49:04 +00002498 if (Lex.getCode() != tgtok::comma) break;
2499 Lex.Lex(); // eat ','.
2500 SubClass = ParseSubClassReference(CurRec, false);
2501 }
2502 }
2503
Sean Silva9cceede2013-01-09 04:49:14 +00002504 if (ApplyLetStack(CurRec))
2505 return true;
Bob Wilson21870412009-11-22 04:24:42 +00002506
Chris Lattnerf4601652007-11-22 20:49:04 +00002507 return ParseBody(CurRec);
2508}
2509
Chris Lattnerf4601652007-11-22 20:49:04 +00002510/// ParseDef - Parse and return a top level or multiclass def, return the record
2511/// corresponding to it. This returns null on error.
2512///
2513/// DefInst ::= DEF ObjectName ObjectBody
2514///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002515bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002516 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002517 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson21870412009-11-22 04:24:42 +00002518 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4601652007-11-22 20:49:04 +00002519
2520 // Parse ObjectName and make a record for it.
Nicolai Haehnle02dea262018-03-21 17:12:53 +00002521 std::unique_ptr<Record> CurRec;
Jordan Rosed1220092013-01-10 18:50:05 +00002522 Init *Name = ParseObjectName(CurMultiClass);
Nicolai Haehnle02dea262018-03-21 17:12:53 +00002523 if (!Name)
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002524 return true;
Bob Wilson21870412009-11-22 04:24:42 +00002525
Nicolai Haehnle02dea262018-03-21 17:12:53 +00002526 if (isa<UnsetInit>(Name))
2527 CurRec = make_unique<Record>(Records.getNewAnonymousName(), DefLoc, Records,
2528 /*Anonymous=*/true);
2529 else
2530 CurRec = make_unique<Record>(Name, DefLoc, Records);
Bob Wilson21870412009-11-22 04:24:42 +00002531
Nicolai Haehnle02dea262018-03-21 17:12:53 +00002532 if (ParseObjectBody(CurRec.get()))
2533 return true;
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002534
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00002535 return addEntry(std::move(CurRec));
Nicolai Haehnled66fa2a2018-03-09 12:24:42 +00002536}
2537
2538/// ParseDefset - Parse a defset statement.
2539///
2540/// Defset ::= DEFSET Type Id '=' '{' ObjectList '}'
2541///
2542bool TGParser::ParseDefset() {
2543 assert(Lex.getCode() == tgtok::Defset);
2544 Lex.Lex(); // Eat the 'defset' token
2545
2546 DefsetRecord Defset;
2547 Defset.Loc = Lex.getLoc();
2548 RecTy *Type = ParseType();
2549 if (!Type)
2550 return true;
2551 if (!isa<ListRecTy>(Type))
2552 return Error(Defset.Loc, "expected list type");
2553 Defset.EltTy = cast<ListRecTy>(Type)->getElementType();
2554
2555 if (Lex.getCode() != tgtok::Id)
2556 return TokError("expected identifier");
2557 StringInit *DeclName = StringInit::get(Lex.getCurStrVal());
2558 if (Records.getGlobal(DeclName->getValue()))
2559 return TokError("def or global variable of this name already exists");
2560
2561 if (Lex.Lex() != tgtok::equal) // Eat the identifier
2562 return TokError("expected '='");
2563 if (Lex.Lex() != tgtok::l_brace) // Eat the '='
2564 return TokError("expected '{'");
2565 SMLoc BraceLoc = Lex.getLoc();
2566 Lex.Lex(); // Eat the '{'
2567
2568 Defsets.push_back(&Defset);
2569 bool Err = ParseObjectList(nullptr);
2570 Defsets.pop_back();
2571 if (Err)
2572 return true;
2573
2574 if (Lex.getCode() != tgtok::r_brace) {
2575 TokError("expected '}' at end of defset");
2576 return Error(BraceLoc, "to match this '{'");
2577 }
2578 Lex.Lex(); // Eat the '}'
2579
2580 Records.addExtraGlobal(DeclName->getValue(),
2581 ListInit::get(Defset.Elements, Defset.EltTy));
2582 return false;
2583}
2584
David Greenecebb4ee2012-02-22 16:09:41 +00002585/// ParseForeach - Parse a for statement. Return the record corresponding
2586/// to it. This returns true on error.
2587///
2588/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2589/// Foreach ::= FOREACH Declaration IN Object
2590///
2591bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00002592 SMLoc Loc = Lex.getLoc();
David Greenecebb4ee2012-02-22 16:09:41 +00002593 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2594 Lex.Lex(); // Eat the 'for' token.
2595
2596 // Make a temporary object to record items associated with the for
2597 // loop.
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00002598 Init *ListValue = nullptr;
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00002599 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper8a0d1c82014-04-09 04:50:04 +00002600 if (!IterName)
David Greenecebb4ee2012-02-22 16:09:41 +00002601 return TokError("expected declaration in for");
2602
2603 if (Lex.getCode() != tgtok::In)
2604 return TokError("Unknown tok");
2605 Lex.Lex(); // Eat the in
2606
2607 // Create a loop object and remember it.
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00002608 Loops.push_back(llvm::make_unique<ForeachLoop>(Loc, IterName, ListValue));
David Greenecebb4ee2012-02-22 16:09:41 +00002609
2610 if (Lex.getCode() != tgtok::l_brace) {
2611 // FOREACH Declaration IN Object
2612 if (ParseObject(CurMultiClass))
2613 return true;
Craig Topperdc0b6f22015-05-04 01:35:39 +00002614 } else {
David Greenecebb4ee2012-02-22 16:09:41 +00002615 SMLoc BraceLoc = Lex.getLoc();
2616 // Otherwise, this is a group foreach.
2617 Lex.Lex(); // eat the '{'.
2618
2619 // Parse the object list.
2620 if (ParseObjectList(CurMultiClass))
2621 return true;
2622
2623 if (Lex.getCode() != tgtok::r_brace) {
2624 TokError("expected '}' at end of foreach command");
2625 return Error(BraceLoc, "to match this '{'");
2626 }
2627 Lex.Lex(); // Eat the }
2628 }
2629
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00002630 // Resolve the loop or store it for later resolution.
2631 std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back());
David Greenecebb4ee2012-02-22 16:09:41 +00002632 Loops.pop_back();
2633
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00002634 return addEntry(std::move(Loop));
Chris Lattnerf4601652007-11-22 20:49:04 +00002635}
2636
Chris Lattnerf4601652007-11-22 20:49:04 +00002637/// ParseClass - Parse a tblgen class definition.
2638///
2639/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2640///
2641bool TGParser::ParseClass() {
2642 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2643 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002644
Chris Lattnerf4601652007-11-22 20:49:04 +00002645 if (Lex.getCode() != tgtok::Id)
2646 return TokError("expected class name after 'class' keyword");
Bob Wilson21870412009-11-22 04:24:42 +00002647
Chris Lattnerf4601652007-11-22 20:49:04 +00002648 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2649 if (CurRec) {
2650 // If the body was previously defined, this is an error.
Nicolai Haehnle26db53e2018-06-04 14:26:05 +00002651 if (!CurRec->getValues().empty() ||
Chris Lattnerf4601652007-11-22 20:49:04 +00002652 !CurRec->getSuperClasses().empty() ||
2653 !CurRec->getTemplateArgs().empty())
Craig Topper21092d82015-04-30 05:54:22 +00002654 return TokError("Class '" + CurRec->getNameInitAsString() +
2655 "' already defined");
Chris Lattnerf4601652007-11-22 20:49:04 +00002656 } else {
2657 // If this is the first reference to this class, create and add it.
Hans Wennborge1e53432014-11-30 00:31:49 +00002658 auto NewRec =
Nicolai Haehnle26db53e2018-06-04 14:26:05 +00002659 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records,
2660 /*Class=*/true);
Craig Topper2935bbc2014-11-29 05:52:51 +00002661 CurRec = NewRec.get();
2662 Records.addClass(std::move(NewRec));
Chris Lattnerf4601652007-11-22 20:49:04 +00002663 }
2664 Lex.Lex(); // eat the name.
Bob Wilson21870412009-11-22 04:24:42 +00002665
Chris Lattnerf4601652007-11-22 20:49:04 +00002666 // If there are template args, parse them.
2667 if (Lex.getCode() == tgtok::less)
2668 if (ParseTemplateArgList(CurRec))
2669 return true;
2670
Chris Lattnerf4601652007-11-22 20:49:04 +00002671 return ParseObjectBody(CurRec);
2672}
2673
2674/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2675/// of LetRecords.
2676///
2677/// LetList ::= LetItem (',' LetItem)*
2678/// LetItem ::= ID OptionalRangeList '=' Value
2679///
Matthias Brauna3f0e482016-12-05 06:41:54 +00002680void TGParser::ParseLetList(SmallVectorImpl<LetRecord> &Result) {
Eugene Zelenko9feaa972016-08-23 17:14:32 +00002681 while (true) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002682 if (Lex.getCode() != tgtok::Id) {
2683 TokError("expected identifier in let definition");
Matthias Brauna3f0e482016-12-05 06:41:54 +00002684 Result.clear();
2685 return;
Chris Lattnerf4601652007-11-22 20:49:04 +00002686 }
Matthias Brauna3f0e482016-12-05 06:41:54 +00002687
Matthias Braun2b86a872016-12-05 07:35:13 +00002688 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002689 SMLoc NameLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +00002690 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4601652007-11-22 20:49:04 +00002691
2692 // Check for an optional RangeList.
Matthias Brauna3f0e482016-12-05 06:41:54 +00002693 SmallVector<unsigned, 16> Bits;
2694 if (ParseOptionalRangeList(Bits)) {
2695 Result.clear();
2696 return;
2697 }
Chris Lattnerf4601652007-11-22 20:49:04 +00002698 std::reverse(Bits.begin(), Bits.end());
Bob Wilson21870412009-11-22 04:24:42 +00002699
Chris Lattnerf4601652007-11-22 20:49:04 +00002700 if (Lex.getCode() != tgtok::equal) {
2701 TokError("expected '=' in let expression");
Matthias Brauna3f0e482016-12-05 06:41:54 +00002702 Result.clear();
2703 return;
Chris Lattnerf4601652007-11-22 20:49:04 +00002704 }
2705 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00002706
Craig Topper8a0d1c82014-04-09 04:50:04 +00002707 Init *Val = ParseValue(nullptr);
Matthias Brauna3f0e482016-12-05 06:41:54 +00002708 if (!Val) {
2709 Result.clear();
2710 return;
2711 }
Bob Wilson21870412009-11-22 04:24:42 +00002712
Chris Lattnerf4601652007-11-22 20:49:04 +00002713 // Now that we have everything, add the record.
Matthias Braun2b86a872016-12-05 07:35:13 +00002714 Result.emplace_back(Name, Bits, Val, NameLoc);
Bob Wilson21870412009-11-22 04:24:42 +00002715
Chris Lattnerf4601652007-11-22 20:49:04 +00002716 if (Lex.getCode() != tgtok::comma)
Matthias Brauna3f0e482016-12-05 06:41:54 +00002717 return;
Bob Wilson21870412009-11-22 04:24:42 +00002718 Lex.Lex(); // eat the comma.
Chris Lattnerf4601652007-11-22 20:49:04 +00002719 }
2720}
2721
2722/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002723/// different related productions. This works inside multiclasses too.
Chris Lattnerf4601652007-11-22 20:49:04 +00002724///
2725/// Object ::= LET LetList IN '{' ObjectList '}'
2726/// Object ::= LET LetList IN Object
2727///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002728bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002729 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2730 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002731
Chris Lattnerf4601652007-11-22 20:49:04 +00002732 // Add this entry to the let stack.
Matthias Brauna3f0e482016-12-05 06:41:54 +00002733 SmallVector<LetRecord, 8> LetInfo;
2734 ParseLetList(LetInfo);
Chris Lattnerf4601652007-11-22 20:49:04 +00002735 if (LetInfo.empty()) return true;
Benjamin Kramer63688e62014-10-03 18:33:16 +00002736 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4601652007-11-22 20:49:04 +00002737
2738 if (Lex.getCode() != tgtok::In)
2739 return TokError("expected 'in' at end of top-level 'let'");
2740 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002741
Chris Lattnerf4601652007-11-22 20:49:04 +00002742 // If this is a scalar let, just handle it now
2743 if (Lex.getCode() != tgtok::l_brace) {
2744 // LET LetList IN Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002745 if (ParseObject(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00002746 return true;
2747 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002748 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002749 // Otherwise, this is a group let.
2750 Lex.Lex(); // eat the '{'.
Bob Wilson21870412009-11-22 04:24:42 +00002751
Chris Lattnerf4601652007-11-22 20:49:04 +00002752 // Parse the object list.
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002753 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00002754 return true;
Bob Wilson21870412009-11-22 04:24:42 +00002755
Chris Lattnerf4601652007-11-22 20:49:04 +00002756 if (Lex.getCode() != tgtok::r_brace) {
2757 TokError("expected '}' at end of top level let command");
2758 return Error(BraceLoc, "to match this '{'");
2759 }
2760 Lex.Lex();
2761 }
Bob Wilson21870412009-11-22 04:24:42 +00002762
Chris Lattnerf4601652007-11-22 20:49:04 +00002763 // Outside this let scope, this let block is not active.
2764 LetStack.pop_back();
2765 return false;
2766}
2767
Chris Lattnerf4601652007-11-22 20:49:04 +00002768/// ParseMultiClass - Parse a multiclass definition.
2769///
Bob Wilson32558652009-04-28 19:41:44 +00002770/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silva9302dcc2013-01-09 02:11:55 +00002771/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2772/// MultiClassObject ::= DefInst
2773/// MultiClassObject ::= MultiClassInst
2774/// MultiClassObject ::= DefMInst
2775/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2776/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4601652007-11-22 20:49:04 +00002777///
2778bool TGParser::ParseMultiClass() {
2779 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2780 Lex.Lex(); // Eat the multiclass token.
2781
2782 if (Lex.getCode() != tgtok::Id)
2783 return TokError("expected identifier after multiclass for name");
2784 std::string Name = Lex.getCurStrVal();
Bob Wilson21870412009-11-22 04:24:42 +00002785
Craig Topper50083142014-12-11 05:25:30 +00002786 auto Result =
2787 MultiClasses.insert(std::make_pair(Name,
2788 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2789
2790 if (!Result.second)
Chris Lattnerf4601652007-11-22 20:49:04 +00002791 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson21870412009-11-22 04:24:42 +00002792
Craig Topper50083142014-12-11 05:25:30 +00002793 CurMultiClass = Result.first->second.get();
Chris Lattnerf4601652007-11-22 20:49:04 +00002794 Lex.Lex(); // Eat the identifier.
Bob Wilson21870412009-11-22 04:24:42 +00002795
Chris Lattnerf4601652007-11-22 20:49:04 +00002796 // If there are template args, parse them.
2797 if (Lex.getCode() == tgtok::less)
Craig Topper8a0d1c82014-04-09 04:50:04 +00002798 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4601652007-11-22 20:49:04 +00002799 return true;
2800
David Greened34a73b2009-04-24 16:55:41 +00002801 bool inherits = false;
2802
David Greenede444af2009-04-22 16:42:54 +00002803 // If there are submulticlasses, parse them.
2804 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00002805 inherits = true;
2806
David Greenede444af2009-04-22 16:42:54 +00002807 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00002808
David Greenede444af2009-04-22 16:42:54 +00002809 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00002810 SubMultiClassReference SubMultiClass =
2811 ParseSubMultiClassReference(CurMultiClass);
Eugene Zelenko9feaa972016-08-23 17:14:32 +00002812 while (true) {
David Greenede444af2009-04-22 16:42:54 +00002813 // Check for error.
Craig Topper8a0d1c82014-04-09 04:50:04 +00002814 if (!SubMultiClass.MC) return true;
Bob Wilson32558652009-04-28 19:41:44 +00002815
David Greenede444af2009-04-22 16:42:54 +00002816 // Add it.
2817 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2818 return true;
Bob Wilson32558652009-04-28 19:41:44 +00002819
David Greenede444af2009-04-22 16:42:54 +00002820 if (Lex.getCode() != tgtok::comma) break;
2821 Lex.Lex(); // eat ','.
2822 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2823 }
2824 }
2825
David Greened34a73b2009-04-24 16:55:41 +00002826 if (Lex.getCode() != tgtok::l_brace) {
2827 if (!inherits)
2828 return TokError("expected '{' in multiclass definition");
Craig Topper355d0692014-11-29 16:05:27 +00002829 if (Lex.getCode() != tgtok::semi)
Bob Wilson21870412009-11-22 04:24:42 +00002830 return TokError("expected ';' in multiclass definition");
Craig Topper355d0692014-11-29 16:05:27 +00002831 Lex.Lex(); // eat the ';'.
Bob Wilson21870412009-11-22 04:24:42 +00002832 } else {
David Greened34a73b2009-04-24 16:55:41 +00002833 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2834 return TokError("multiclass must contain at least one def");
Bob Wilson21870412009-11-22 04:24:42 +00002835
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002836 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002837 switch (Lex.getCode()) {
Craig Topper355d0692014-11-29 16:05:27 +00002838 default:
Nicolai Haehnled66fa2a2018-03-09 12:24:42 +00002839 return TokError("expected 'let', 'def', 'defm' or 'foreach' in "
2840 "multiclass body");
Craig Topper355d0692014-11-29 16:05:27 +00002841 case tgtok::Let:
2842 case tgtok::Def:
2843 case tgtok::Defm:
2844 case tgtok::Foreach:
2845 if (ParseObject(CurMultiClass))
2846 return true;
2847 break;
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002848 }
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002849 }
David Greened34a73b2009-04-24 16:55:41 +00002850 Lex.Lex(); // eat the '}'.
2851 }
Bob Wilson21870412009-11-22 04:24:42 +00002852
Craig Topper8a0d1c82014-04-09 04:50:04 +00002853 CurMultiClass = nullptr;
Chris Lattnerf4601652007-11-22 20:49:04 +00002854 return false;
2855}
2856
2857/// ParseDefm - Parse the instantiation of a multiclass.
2858///
2859/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2860///
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002861bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002862 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Nicolai Haehnle02dea262018-03-21 17:12:53 +00002863 Lex.Lex(); // eat the defm
David Greenea9e07dd2011-10-19 13:04:29 +00002864
Nicolai Haehnle02dea262018-03-21 17:12:53 +00002865 Init *DefmName = ParseObjectName(CurMultiClass);
2866 if (!DefmName)
2867 return true;
Nicolai Haehnle26db53e2018-06-04 14:26:05 +00002868 if (isa<UnsetInit>(DefmName)) {
Nicolai Haehnle02dea262018-03-21 17:12:53 +00002869 DefmName = Records.getNewAnonymousName();
Nicolai Haehnle26db53e2018-06-04 14:26:05 +00002870 if (CurMultiClass)
2871 DefmName = BinOpInit::getStrConcat(
2872 VarInit::get(QualifiedNameOfImplicitName(CurMultiClass),
2873 StringRecTy::get()),
2874 DefmName);
2875 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +00002876
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002877 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4601652007-11-22 20:49:04 +00002878 return TokError("expected ':' after defm identifier");
Bob Wilson21870412009-11-22 04:24:42 +00002879
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002880 // Keep track of the new generated record definitions.
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00002881 std::vector<RecordsEntry> NewEntries;
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002882
2883 // This record also inherits from a regular class (non-multiclass)?
2884 bool InheritFromClass = false;
2885
Chris Lattnerf4601652007-11-22 20:49:04 +00002886 // eat the colon.
2887 Lex.Lex();
2888
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002889 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper8a0d1c82014-04-09 04:50:04 +00002890 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greene56546132009-04-22 22:17:51 +00002891
Eugene Zelenko9feaa972016-08-23 17:14:32 +00002892 while (true) {
Craig Topper8a0d1c82014-04-09 04:50:04 +00002893 if (!Ref.Rec) return true;
David Greene56546132009-04-22 22:17:51 +00002894
2895 // To instantiate a multiclass, we need to first get the multiclass, then
2896 // instantiate each def contained in the multiclass with the SubClassRef
2897 // template parameters.
Craig Topper50083142014-12-11 05:25:30 +00002898 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper73642a82014-11-30 01:20:17 +00002899 assert(MC && "Didn't lookup multiclass correctly?");
Matthias Brauna3f0e482016-12-05 06:41:54 +00002900 ArrayRef<Init*> TemplateVals = Ref.TemplateArgs;
David Greene56546132009-04-22 22:17:51 +00002901
2902 // Verify that the correct number of template arguments were specified.
Benjamin Kramer55cb91e2015-10-24 12:46:45 +00002903 ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();
David Greene56546132009-04-22 22:17:51 +00002904 if (TArgs.size() < TemplateVals.size())
2905 return Error(SubClassLoc,
2906 "more template args specified than multiclass expects");
2907
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00002908 SubstStack Substs;
Nicolai Haehnle02dea262018-03-21 17:12:53 +00002909 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2910 if (i < TemplateVals.size()) {
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00002911 Substs.emplace_back(TArgs[i], TemplateVals[i]);
Nicolai Haehnle02dea262018-03-21 17:12:53 +00002912 } else {
2913 Init *Default = MC->Rec.getValue(TArgs[i])->getValue();
2914 if (!Default->isComplete()) {
2915 return Error(SubClassLoc,
2916 "value not specified for template argument #" +
2917 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
2918 ") of multiclass '" + MC->Rec.getNameInitAsString() +
2919 "'");
2920 }
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00002921 Substs.emplace_back(TArgs[i], Default);
Nicolai Haehnle02dea262018-03-21 17:12:53 +00002922 }
David Greene56546132009-04-22 22:17:51 +00002923 }
2924
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00002925 Substs.emplace_back(QualifiedNameOfImplicitName(MC), DefmName);
Nicolai Haehnle26db53e2018-06-04 14:26:05 +00002926
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00002927 if (resolve(MC->Entries, Substs, CurMultiClass == nullptr, &NewEntries,
2928 &SubClassLoc))
2929 return true;
David Greenee499a2d2011-10-05 22:42:07 +00002930
David Greene56546132009-04-22 22:17:51 +00002931 if (Lex.getCode() != tgtok::comma) break;
2932 Lex.Lex(); // eat ','.
2933
Craig Topper5cc16a92013-08-20 04:22:09 +00002934 if (Lex.getCode() != tgtok::Id)
2935 return TokError("expected identifier");
2936
David Greene56546132009-04-22 22:17:51 +00002937 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002938
2939 // A defm can inherit from regular classes (non-multiclass) as
2940 // long as they come in the end of the inheritance list.
Craig Topper8a0d1c82014-04-09 04:50:04 +00002941 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002942
2943 if (InheritFromClass)
2944 break;
2945
Craig Topper8a0d1c82014-04-09 04:50:04 +00002946 Ref = ParseSubClassReference(nullptr, true);
David Greene56546132009-04-22 22:17:51 +00002947 }
2948
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002949 if (InheritFromClass) {
2950 // Process all the classes to inherit as if they were part of a
2951 // regular 'def' and inherit all record values.
Craig Topper8a0d1c82014-04-09 04:50:04 +00002952 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Eugene Zelenko9feaa972016-08-23 17:14:32 +00002953 while (true) {
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002954 // Check for error.
Craig Topper8a0d1c82014-04-09 04:50:04 +00002955 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002956
2957 // Get the expanded definition prototypes and teach them about
2958 // the record values the current class to inherit has
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00002959 for (auto &E : NewEntries) {
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002960 // Add it.
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00002961 if (AddSubClass(E, SubClass))
Sean Silva9cceede2013-01-09 04:49:14 +00002962 return true;
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002963 }
2964
2965 if (Lex.getCode() != tgtok::comma) break;
2966 Lex.Lex(); // eat ','.
Craig Topper8a0d1c82014-04-09 04:50:04 +00002967 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002968 }
2969 }
2970
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00002971 for (auto &E : NewEntries) {
2972 if (ApplyLetStack(E))
Nicolai Haehnle02dea262018-03-21 17:12:53 +00002973 return true;
2974
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00002975 addEntry(std::move(E));
Nicolai Haehnle19ebaa72018-03-06 13:48:47 +00002976 }
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002977
Chris Lattnerf4601652007-11-22 20:49:04 +00002978 if (Lex.getCode() != tgtok::semi)
2979 return TokError("expected ';' at end of defm");
2980 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002981
Chris Lattnerf4601652007-11-22 20:49:04 +00002982 return false;
2983}
2984
2985/// ParseObject
2986/// Object ::= ClassInst
2987/// Object ::= DefInst
2988/// Object ::= MultiClassInst
2989/// Object ::= DefMInst
2990/// Object ::= LETCommand '{' ObjectList '}'
2991/// Object ::= LETCommand Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002992bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002993 switch (Lex.getCode()) {
Chris Lattnerd6d9dd92010-10-31 19:27:15 +00002994 default:
Nicolai Haehnled66fa2a2018-03-09 12:24:42 +00002995 return TokError("Expected class, def, defm, defset, multiclass, let or "
2996 "foreach");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002997 case tgtok::Let: return ParseTopLevelLet(MC);
2998 case tgtok::Def: return ParseDef(MC);
David Greenecebb4ee2012-02-22 16:09:41 +00002999 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00003000 case tgtok::Defm: return ParseDefm(MC);
Nicolai Haehnled66fa2a2018-03-09 12:24:42 +00003001 case tgtok::Defset:
3002 if (MC)
3003 return TokError("defset is not allowed inside multiclass");
3004 return ParseDefset();
Nicolai Haehnle3e7114a2018-03-14 11:01:01 +00003005 case tgtok::Class:
3006 if (MC)
3007 return TokError("class is not allowed inside multiclass");
3008 if (!Loops.empty())
3009 return TokError("class is not allowed inside foreach loop");
3010 return ParseClass();
3011 case tgtok::MultiClass:
3012 if (!Loops.empty())
3013 return TokError("multiclass is not allowed inside foreach loop");
3014 return ParseMultiClass();
Chris Lattnerf4601652007-11-22 20:49:04 +00003015 }
3016}
3017
3018/// ParseObjectList
3019/// ObjectList :== Object*
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00003020bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00003021 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00003022 if (ParseObject(MC))
Chris Lattnerf4601652007-11-22 20:49:04 +00003023 return true;
3024 }
3025 return false;
3026}
3027
Chris Lattnerf4601652007-11-22 20:49:04 +00003028bool TGParser::ParseFile() {
3029 Lex.Lex(); // Prime the lexer.
3030 if (ParseObjectList()) return true;
Bob Wilson21870412009-11-22 04:24:42 +00003031
Chris Lattnerf4601652007-11-22 20:49:04 +00003032 // If we have unread input at the end of the file, report it.
3033 if (Lex.getCode() == tgtok::Eof)
3034 return false;
Bob Wilson21870412009-11-22 04:24:42 +00003035
Chris Lattnerf4601652007-11-22 20:49:04 +00003036 return TokError("Unexpected input at top level");
3037}
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +00003038
3039#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3040LLVM_DUMP_METHOD void RecordsEntry::dump() const {
3041 if (Loop)
3042 Loop->dump();
3043 if (Rec)
3044 Rec->dump();
3045}
3046
3047LLVM_DUMP_METHOD void ForeachLoop::dump() const {
3048 errs() << "foreach " << IterVar->getAsString() << " = "
3049 << ListValue->getAsString() << " in {\n";
3050
3051 for (const auto &E : Entries)
3052 E.dump();
3053
3054 errs() << "}\n";
3055}
3056
3057LLVM_DUMP_METHOD void MultiClass::dump() const {
3058 errs() << "Record:\n";
3059 Rec.dump();
3060
3061 errs() << "Defs:\n";
3062 for (const auto &E : Entries)
3063 E.dump();
3064}
3065#endif