blob: 8761726d12e3e9ac72a76167afa14c643ca7eccf [file] [log] [blame]
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001//===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-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 Lattnerf4127dd2007-11-22 20:49:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Parser for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
14#include "TGParser.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallVector.h"
Chris Lattnerf4127dd2007-11-22 20:49:04 +000016#include "llvm/ADT/StringExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/Support/CommandLine.h"
18#include "llvm/TableGen/Record.h"
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000019#include <algorithm>
20#include <sstream>
Chris Lattnerf4127dd2007-11-22 20:49:04 +000021using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24// Support Code for the Semantic Actions.
25//===----------------------------------------------------------------------===//
26
27namespace llvm {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000028struct SubClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000029 SMRange RefRange;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000030 Record *Rec;
David Greeneaf8ee2c2011-07-29 22:43:06 +000031 std::vector<Init*> TemplateArgs;
Craig Topper011817a2014-04-09 04:50:04 +000032 SubClassReference() : Rec(nullptr) {}
David Greene7049e792009-04-24 16:55:41 +000033
Craig Topper011817a2014-04-09 04:50:04 +000034 bool isInvalid() const { return Rec == nullptr; }
Chris Lattnerf4127dd2007-11-22 20:49:04 +000035};
David Greene753ed8f2009-04-22 16:42:54 +000036
37struct SubMultiClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000038 SMRange RefRange;
David Greene753ed8f2009-04-22 16:42:54 +000039 MultiClass *MC;
David Greeneaf8ee2c2011-07-29 22:43:06 +000040 std::vector<Init*> TemplateArgs;
Craig Topper011817a2014-04-09 04:50:04 +000041 SubMultiClassReference() : MC(nullptr) {}
Bob Wilson3d948162009-04-28 19:41:44 +000042
Craig Topper011817a2014-04-09 04:50:04 +000043 bool isInvalid() const { return MC == nullptr; }
David Greene7049e792009-04-24 16:55:41 +000044 void dump() const;
David Greene753ed8f2009-04-22 16:42:54 +000045};
David Greene7049e792009-04-24 16:55:41 +000046
47void SubMultiClassReference::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000048 errs() << "Multiclass:\n";
Bob Wilson7248f862009-11-22 04:24:42 +000049
David Greene7049e792009-04-24 16:55:41 +000050 MC->dump();
Bob Wilson7248f862009-11-22 04:24:42 +000051
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000052 errs() << "Template args:\n";
David Greeneaf8ee2c2011-07-29 22:43:06 +000053 for (std::vector<Init *>::const_iterator i = TemplateArgs.begin(),
David Greene7049e792009-04-24 16:55:41 +000054 iend = TemplateArgs.end();
55 i != iend;
56 ++i) {
57 (*i)->dump();
58 }
59}
60
Chris Lattnerf4127dd2007-11-22 20:49:04 +000061} // end namespace llvm
62
Chris Lattner526c8cb2009-06-21 03:39:35 +000063bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Craig Topper011817a2014-04-09 04:50:04 +000064 if (!CurRec)
Chris Lattnerf4127dd2007-11-22 20:49:04 +000065 CurRec = &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +000066
Jakob Stoklund Olesen9d1c5ee2012-01-13 03:16:35 +000067 if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000068 // The value already exists in the class, treat this as a set.
69 if (ERV->setValue(RV.getValue()))
70 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
71 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson7248f862009-11-22 04:24:42 +000072 "previous definition of type '" +
Chris Lattnerf4127dd2007-11-22 20:49:04 +000073 ERV->getType()->getAsString() + "'");
74 } else {
75 CurRec->addValue(RV);
76 }
77 return false;
78}
79
80/// SetValue -
81/// Return true on error, false on success.
David Greene3ca42122011-10-19 13:02:39 +000082bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
David Greeneaf8ee2c2011-07-29 22:43:06 +000083 const std::vector<unsigned> &BitList, Init *V) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000084 if (!V) return false;
85
Craig Topper011817a2014-04-09 04:50:04 +000086 if (!CurRec) CurRec = &CurMultiClass->Rec;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000087
88 RecordVal *RV = CurRec->getValue(ValName);
Craig Topper011817a2014-04-09 04:50:04 +000089 if (!RV)
David Greene3ca42122011-10-19 13:02:39 +000090 return Error(Loc, "Value '" + ValName->getAsUnquotedString()
91 + "' unknown!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +000092
93 // Do not allow assignments like 'X = X'. This will just cause infinite loops
94 // in the resolution machinery.
95 if (BitList.empty())
Sean Silvafb509ed2012-10-10 20:24:43 +000096 if (VarInit *VI = dyn_cast<VarInit>(V))
David Greene3ca42122011-10-19 13:02:39 +000097 if (VI->getNameInit() == ValName)
Chris Lattnerf4127dd2007-11-22 20:49:04 +000098 return false;
Bob Wilson7248f862009-11-22 04:24:42 +000099
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000100 // If we are assigning to a subset of the bits in the value... then we must be
101 // assigning to a field of BitsRecTy, which must have a BitsInit
102 // initializer.
103 //
104 if (!BitList.empty()) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000105 BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue());
Craig Topper011817a2014-04-09 04:50:04 +0000106 if (!CurVal)
David Greene3ca42122011-10-19 13:02:39 +0000107 return Error(Loc, "Value '" + ValName->getAsUnquotedString()
108 + "' is not a bits type");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000109
110 // Convert the incoming value to a bits type of the appropriate size...
David Greeneaf8ee2c2011-07-29 22:43:06 +0000111 Init *BI = V->convertInitializerTo(BitsRecTy::get(BitList.size()));
Craig Topper011817a2014-04-09 04:50:04 +0000112 if (!BI) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000113 return Error(Loc, "Initializer is not compatible with bit range");
114 }
Bob Wilson7248f862009-11-22 04:24:42 +0000115
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000116 // We should have a BitsInit type now.
Sean Silvafb509ed2012-10-10 20:24:43 +0000117 BitsInit *BInit = dyn_cast<BitsInit>(BI);
Craig Toppere73658d2014-04-28 04:05:08 +0000118 assert(BInit != nullptr);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000119
David Greeneaf8ee2c2011-07-29 22:43:06 +0000120 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000121
122 // Loop over bits, assigning values as appropriate.
123 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
124 unsigned Bit = BitList[i];
David Greeneb3da8122011-07-29 19:07:00 +0000125 if (NewBits[Bit])
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000126 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
David Greene3ca42122011-10-19 13:02:39 +0000127 ValName->getAsUnquotedString() + "' more than once");
David Greeneb3da8122011-07-29 19:07:00 +0000128 NewBits[Bit] = BInit->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000129 }
130
131 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
Craig Topper011817a2014-04-09 04:50:04 +0000132 if (!NewBits[i])
David Greeneb3da8122011-07-29 19:07:00 +0000133 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000134
David Greenee32ebf22011-07-29 19:07:07 +0000135 V = BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000136 }
137
Pete Cooper040c6a62014-07-31 01:43:57 +0000138 if (RV->setValue(V)) {
139 std::string InitType = "";
140 if (BitsInit *BI = dyn_cast<BitsInit>(V)) {
141 InitType = (Twine("' of type bit initializer with length ") +
142 Twine(BI->getNumBits())).str();
143 }
David Greene3ca42122011-10-19 13:02:39 +0000144 return Error(Loc, "Value '" + ValName->getAsUnquotedString() + "' of type '"
145 + RV->getType()->getAsString() +
146 "' is incompatible with initializer '" + V->getAsString()
Pete Cooper040c6a62014-07-31 01:43:57 +0000147 + InitType
David Greene3ca42122011-10-19 13:02:39 +0000148 + "'");
Pete Cooper040c6a62014-07-31 01:43:57 +0000149 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000150 return false;
151}
152
153/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
154/// args as SubClass's template arguments.
Cedric Venetd1e179d2009-02-14 16:06:42 +0000155bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000156 Record *SC = SubClass.Rec;
157 // Add all of the values in the subclass into the current class.
158 const std::vector<RecordVal> &Vals = SC->getValues();
159 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
Jordan Rosef12e8a92013-01-10 18:50:11 +0000160 if (AddValue(CurRec, SubClass.RefRange.Start, Vals[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000161 return true;
162
David Greenedb10e692011-10-19 13:02:42 +0000163 const std::vector<Init *> &TArgs = SC->getTemplateArgs();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000164
165 // Ensure that an appropriate number of template arguments are specified.
166 if (TArgs.size() < SubClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000167 return Error(SubClass.RefRange.Start,
168 "More template args specified than expected");
Bob Wilson7248f862009-11-22 04:24:42 +0000169
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000170 // Loop over all of the template arguments, setting them to the specified
171 // value or leaving them as the default if necessary.
172 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
173 if (i < SubClass.TemplateArgs.size()) {
174 // If a value is specified for this template arg, set it now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000175 if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
176 std::vector<unsigned>(), SubClass.TemplateArgs[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000177 return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000178
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000179 // Resolve it next.
180 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson7248f862009-11-22 04:24:42 +0000181
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000182 // Now remove it.
183 CurRec->removeValue(TArgs[i]);
184
185 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000186 return Error(SubClass.RefRange.Start,
187 "Value not specified for template argument #"
David Greenedb10e692011-10-19 13:02:42 +0000188 + utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
189 + ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000190 }
191 }
192
193 // Since everything went well, we can now set the "superclass" list for the
194 // current record.
195 const std::vector<Record*> &SCs = SC->getSuperClasses();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000196 ArrayRef<SMRange> SCRanges = SC->getSuperClassRanges();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000197 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
198 if (CurRec->isSubClassOf(SCs[i]))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000199 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000200 "Already subclass of '" + SCs[i]->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000201 CurRec->addSuperClass(SCs[i], SCRanges[i]);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000202 }
Bob Wilson7248f862009-11-22 04:24:42 +0000203
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000204 if (CurRec->isSubClassOf(SC))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000205 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000206 "Already subclass of '" + SC->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000207 CurRec->addSuperClass(SC, SubClass.RefRange);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000208 return false;
209}
210
David Greene753ed8f2009-04-22 16:42:54 +0000211/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilsonf71e6562009-04-30 18:26:19 +0000212/// CurMC, resolving its template args as SubMultiClass's
David Greene753ed8f2009-04-22 16:42:54 +0000213/// template arguments.
Bob Wilsonf71e6562009-04-30 18:26:19 +0000214bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson92ab8202009-04-30 17:46:20 +0000215 SubMultiClassReference &SubMultiClass) {
David Greene753ed8f2009-04-22 16:42:54 +0000216 MultiClass *SMC = SubMultiClass.MC;
Bob Wilsonf71e6562009-04-30 18:26:19 +0000217 Record *CurRec = &CurMC->Rec;
David Greene753ed8f2009-04-22 16:42:54 +0000218
Bob Wilsonf71e6562009-04-30 18:26:19 +0000219 const std::vector<RecordVal> &MCVals = CurRec->getValues();
David Greene753ed8f2009-04-22 16:42:54 +0000220
221 // Add all of the values in the subclass into the current class.
222 const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues();
223 for (unsigned i = 0, e = SMCVals.size(); i != e; ++i)
Jordan Rosef12e8a92013-01-10 18:50:11 +0000224 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVals[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000225 return true;
226
Bob Wilsonf71e6562009-04-30 18:26:19 +0000227 int newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000228
David Greene753ed8f2009-04-22 16:42:54 +0000229 // Add all of the defs in the subclass into the current multiclass.
230 for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(),
231 iend = SMC->DefPrototypes.end();
232 i != iend;
233 ++i) {
234 // Clone the def and add it to the current multiclass
Anton Yartsev3fa65d42014-09-25 19:55:58 +0000235 auto NewDef = make_unique<Record>(**i);
David Greene753ed8f2009-04-22 16:42:54 +0000236
237 // Add all of the values in the superclass into the current def.
238 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
Anton Yartsev3fa65d42014-09-25 19:55:58 +0000239 if (AddValue(NewDef.get(), SubMultiClass.RefRange.Start, MCVals[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000240 return true;
241
Anton Yartsev3fa65d42014-09-25 19:55:58 +0000242 CurMC->DefPrototypes.push_back(NewDef.release());
David Greene753ed8f2009-04-22 16:42:54 +0000243 }
Bob Wilson3d948162009-04-28 19:41:44 +0000244
David Greenedb10e692011-10-19 13:02:42 +0000245 const std::vector<Init *> &SMCTArgs = SMC->Rec.getTemplateArgs();
David Greene753ed8f2009-04-22 16:42:54 +0000246
David Greene7049e792009-04-24 16:55:41 +0000247 // Ensure that an appropriate number of template arguments are
248 // specified.
David Greene753ed8f2009-04-22 16:42:54 +0000249 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000250 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000251 "More template args specified than expected");
Bob Wilson3d948162009-04-28 19:41:44 +0000252
David Greene753ed8f2009-04-22 16:42:54 +0000253 // Loop over all of the template arguments, setting them to the specified
254 // value or leaving them as the default if necessary.
255 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
256 if (i < SubMultiClass.TemplateArgs.size()) {
David Greene7049e792009-04-24 16:55:41 +0000257 // If a value is specified for this template arg, set it in the
258 // superclass now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000259 if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson3d948162009-04-28 19:41:44 +0000260 std::vector<unsigned>(),
David Greene753ed8f2009-04-22 16:42:54 +0000261 SubMultiClass.TemplateArgs[i]))
262 return true;
263
264 // Resolve it next.
265 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson3d948162009-04-28 19:41:44 +0000266
David Greene753ed8f2009-04-22 16:42:54 +0000267 // Now remove it.
268 CurRec->removeValue(SMCTArgs[i]);
269
David Greene7049e792009-04-24 16:55:41 +0000270 // If a value is specified for this template arg, set it in the
271 // new defs now.
272 for (MultiClass::RecordVector::iterator j =
Bob Wilsonf71e6562009-04-30 18:26:19 +0000273 CurMC->DefPrototypes.begin() + newDefStart,
274 jend = CurMC->DefPrototypes.end();
David Greene753ed8f2009-04-22 16:42:54 +0000275 j != jend;
276 ++j) {
277 Record *Def = *j;
278
Jordan Rosef12e8a92013-01-10 18:50:11 +0000279 if (SetValue(Def, SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson3d948162009-04-28 19:41:44 +0000280 std::vector<unsigned>(),
David Greene753ed8f2009-04-22 16:42:54 +0000281 SubMultiClass.TemplateArgs[i]))
282 return true;
283
284 // Resolve it next.
285 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
286
287 // Now remove it
288 Def->removeValue(SMCTArgs[i]);
289 }
290 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000291 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000292 "Value not specified for template argument #"
David Greenedb10e692011-10-19 13:02:42 +0000293 + utostr(i) + " (" + SMCTArgs[i]->getAsUnquotedString()
294 + ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000295 }
296 }
297
298 return false;
299}
300
David Greenefb927af2012-02-22 16:09:41 +0000301/// ProcessForeachDefs - Given a record, apply all of the variable
302/// values in all surrounding foreach loops, creating new records for
303/// each combination of values.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000304bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
305 if (Loops.empty())
306 return false;
307
David Greenefb927af2012-02-22 16:09:41 +0000308 // We want to instantiate a new copy of CurRec for each combination
309 // of nested loop iterator values. We don't want top instantiate
310 // any copies until we have values for each loop iterator.
311 IterSet IterVals;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000312 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000313}
314
315/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
316/// apply each of the variable values in this loop and then process
317/// subloops.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000318bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
319 // Recursively build a tuple of iterator values.
320 if (IterVals.size() != Loops.size()) {
321 assert(IterVals.size() < Loops.size());
322 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silvafb509ed2012-10-10 20:24:43 +0000323 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Craig Topper011817a2014-04-09 04:50:04 +0000324 if (!List) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000325 Error(Loc, "Loop list is not a list");
326 return true;
327 }
David Greenefb927af2012-02-22 16:09:41 +0000328
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000329 // Process each value.
330 for (int64_t i = 0; i < List->getSize(); ++i) {
Craig Topper011817a2014-04-09 04:50:04 +0000331 Init *ItemVal = List->resolveListElementReference(*CurRec, nullptr, i);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000332 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
333 if (ProcessForeachDefs(CurRec, Loc, IterVals))
334 return true;
335 IterVals.pop_back();
336 }
337 return false;
338 }
339
340 // This is the bottom of the recursion. We have all of the iterator values
341 // for this point in the iteration space. Instantiate a new record to
342 // reflect this combination of values.
Craig Topper84138712014-11-29 05:31:10 +0000343 auto IterRec = make_unique<Record>(*CurRec);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000344
345 // Set the iterator values now.
346 for (unsigned i = 0, e = IterVals.size(); i != e; ++i) {
347 VarInit *IterVar = IterVals[i].IterVar;
Sean Silvafb509ed2012-10-10 20:24:43 +0000348 TypedInit *IVal = dyn_cast<TypedInit>(IterVals[i].IterValue);
Craig Topper84138712014-11-29 05:31:10 +0000349 if (!IVal)
350 return Error(Loc, "foreach iterator value is untyped");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000351
352 IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false));
353
Craig Topper84138712014-11-29 05:31:10 +0000354 if (SetValue(IterRec.get(), Loc, IterVar->getName(),
355 std::vector<unsigned>(), IVal))
356 return Error(Loc, "when instantiating this def");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000357
358 // Resolve it next.
359 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName()));
360
361 // Remove it.
362 IterRec->removeValue(IterVar->getName());
363 }
364
365 if (Records.getDef(IterRec->getNameInitAsString())) {
Artyom Skrobov8b985322014-06-10 12:41:14 +0000366 // If this record is anonymous, it's no problem, just generate a new name
Craig Topper84138712014-11-29 05:31:10 +0000367 if (!IterRec->isAnonymous())
368 return Error(Loc, "def already exists: " +IterRec->getNameInitAsString());
369
370 IterRec->setName(GetNewAnonymousName());
David Greenefb927af2012-02-22 16:09:41 +0000371 }
372
Craig Topper84138712014-11-29 05:31:10 +0000373 Record *IterRecSave = IterRec.get(); // Keep a copy before release.
Craig Toppercdab2322014-11-29 05:52:51 +0000374 Records.addDef(std::move(IterRec));
Craig Topper84138712014-11-29 05:31:10 +0000375 IterRecSave->resolveReferences();
David Greenefb927af2012-02-22 16:09:41 +0000376 return false;
377}
378
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000379//===----------------------------------------------------------------------===//
380// Parser Code
381//===----------------------------------------------------------------------===//
382
383/// isObjectStart - Return true if this is a valid first token for an Object.
384static bool isObjectStart(tgtok::TokKind K) {
385 return K == tgtok::Class || K == tgtok::Def ||
David Greenefb927af2012-02-22 16:09:41 +0000386 K == tgtok::Defm || K == tgtok::Let ||
387 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000388}
389
Alp Tokerce91fe52013-12-21 18:51:00 +0000390/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
391/// an identifier.
392std::string TGParser::GetNewAnonymousName() {
Michael J. Spencer2bb7db92013-02-26 21:29:47 +0000393 unsigned Tmp = AnonCounter++; // MSVC2012 ICEs without this.
Alp Tokerce91fe52013-12-21 18:51:00 +0000394 return "anonymous_" + utostr(Tmp);
Chris Lattner7538ed82010-10-05 22:51:56 +0000395}
396
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000397/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000398/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000399/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000400/// ObjectName ::= /*empty*/
401///
David Greene2affd672011-10-19 13:04:29 +0000402Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
403 switch (Lex.getCode()) {
404 case tgtok::colon:
405 case tgtok::semi:
406 case tgtok::l_brace:
407 // These are all of the tokens that can begin an object body.
408 // Some of these can also begin values but we disallow those cases
409 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000410 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000411 default:
412 break;
413 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000414
Craig Topper011817a2014-04-09 04:50:04 +0000415 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000416 if (CurMultiClass)
417 CurRec = &CurMultiClass->Rec;
418
Craig Topper011817a2014-04-09 04:50:04 +0000419 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000420 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000421 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000422 if (!CurRecName) {
423 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000424 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000425 }
426 Type = CurRecName->getType();
427 }
428
429 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000430}
431
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000432/// ParseClassID - Parse and resolve a reference to a class name. This returns
433/// null on error.
434///
435/// ClassID ::= ID
436///
437Record *TGParser::ParseClassID() {
438 if (Lex.getCode() != tgtok::Id) {
439 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000440 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000441 }
Bob Wilson7248f862009-11-22 04:24:42 +0000442
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000443 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000444 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000445 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000446
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000447 Lex.Lex();
448 return Result;
449}
450
Bob Wilson3d948162009-04-28 19:41:44 +0000451/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
452/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000453///
454/// MultiClassID ::= ID
455///
456MultiClass *TGParser::ParseMultiClassID() {
457 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000458 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000459 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000460 }
Bob Wilson3d948162009-04-28 19:41:44 +0000461
David Greene753ed8f2009-04-22 16:42:54 +0000462 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
Craig Topper011817a2014-04-09 04:50:04 +0000463 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000464 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000465
David Greene753ed8f2009-04-22 16:42:54 +0000466 Lex.Lex();
467 return Result;
468}
469
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000470/// ParseSubClassReference - Parse a reference to a subclass or to a templated
471/// subclass. This returns a SubClassRefTy with a null Record* on error.
472///
473/// SubClassRef ::= ClassID
474/// SubClassRef ::= ClassID '<' ValueList '>'
475///
476SubClassReference TGParser::
477ParseSubClassReference(Record *CurRec, bool isDefm) {
478 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000479 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000480
Sean Silva0657b402013-01-09 02:17:14 +0000481 if (isDefm) {
482 if (MultiClass *MC = ParseMultiClassID())
483 Result.Rec = &MC->Rec;
484 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000485 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000486 }
Craig Topper011817a2014-04-09 04:50:04 +0000487 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000488
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000489 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000490 if (Lex.getCode() != tgtok::less) {
491 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000492 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000493 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000494 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000495
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000496 if (Lex.getCode() == tgtok::greater) {
497 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000498 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000499 return Result;
500 }
Bob Wilson7248f862009-11-22 04:24:42 +0000501
David Greene8618f952009-06-08 20:23:18 +0000502 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000503 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000504 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000505 return Result;
506 }
Bob Wilson7248f862009-11-22 04:24:42 +0000507
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000508 if (Lex.getCode() != tgtok::greater) {
509 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000510 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000511 return Result;
512 }
513 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000514 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000515
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000516 return Result;
517}
518
Bob Wilson3d948162009-04-28 19:41:44 +0000519/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
520/// templated submulticlass. This returns a SubMultiClassRefTy with a null
521/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000522///
523/// SubMultiClassRef ::= MultiClassID
524/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
525///
526SubMultiClassReference TGParser::
527ParseSubMultiClassReference(MultiClass *CurMC) {
528 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000529 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000530
David Greene753ed8f2009-04-22 16:42:54 +0000531 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000532 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000533
David Greene753ed8f2009-04-22 16:42:54 +0000534 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000535 if (Lex.getCode() != tgtok::less) {
536 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000537 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000538 }
David Greene753ed8f2009-04-22 16:42:54 +0000539 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000540
David Greene753ed8f2009-04-22 16:42:54 +0000541 if (Lex.getCode() == tgtok::greater) {
542 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000543 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000544 return Result;
545 }
Bob Wilson3d948162009-04-28 19:41:44 +0000546
David Greene8618f952009-06-08 20:23:18 +0000547 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000548 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000549 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000550 return Result;
551 }
Bob Wilson3d948162009-04-28 19:41:44 +0000552
David Greene753ed8f2009-04-22 16:42:54 +0000553 if (Lex.getCode() != tgtok::greater) {
554 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000555 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000556 return Result;
557 }
558 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000559 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000560
561 return Result;
562}
563
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000564/// ParseRangePiece - Parse a bit/value range.
565/// RangePiece ::= INTVAL
566/// RangePiece ::= INTVAL '-' INTVAL
567/// RangePiece ::= INTVAL INTVAL
568bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000569 if (Lex.getCode() != tgtok::IntVal) {
570 TokError("expected integer or bitrange");
571 return true;
572 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000573 int64_t Start = Lex.getCurIntVal();
574 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000575
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000576 if (Start < 0)
577 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000578
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000579 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000580 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000581 Ranges.push_back(Start);
582 return false;
583 case tgtok::minus:
584 if (Lex.Lex() != tgtok::IntVal) {
585 TokError("expected integer value as end of range");
586 return true;
587 }
588 End = Lex.getCurIntVal();
589 break;
590 case tgtok::IntVal:
591 End = -Lex.getCurIntVal();
592 break;
593 }
Bob Wilson7248f862009-11-22 04:24:42 +0000594 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000595 return TokError("invalid range, cannot be negative");
596 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000597
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000598 // Add to the range.
599 if (Start < End) {
600 for (; Start <= End; ++Start)
601 Ranges.push_back(Start);
602 } else {
603 for (; Start >= End; --Start)
604 Ranges.push_back(Start);
605 }
606 return false;
607}
608
609/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
610///
611/// RangeList ::= RangePiece (',' RangePiece)*
612///
613std::vector<unsigned> TGParser::ParseRangeList() {
614 std::vector<unsigned> Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000615
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000616 // Parse the first piece.
617 if (ParseRangePiece(Result))
618 return std::vector<unsigned>();
619 while (Lex.getCode() == tgtok::comma) {
620 Lex.Lex(); // Eat the comma.
621
622 // Parse the next range piece.
623 if (ParseRangePiece(Result))
624 return std::vector<unsigned>();
625 }
626 return Result;
627}
628
629/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
630/// OptionalRangeList ::= '<' RangeList '>'
631/// OptionalRangeList ::= /*empty*/
632bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
633 if (Lex.getCode() != tgtok::less)
634 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000635
Chris Lattner526c8cb2009-06-21 03:39:35 +0000636 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000637 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000638
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000639 // Parse the range list.
640 Ranges = ParseRangeList();
641 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000642
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000643 if (Lex.getCode() != tgtok::greater) {
644 TokError("expected '>' at end of range list");
645 return Error(StartLoc, "to match this '<'");
646 }
647 Lex.Lex(); // eat the '>'.
648 return false;
649}
650
651/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
652/// OptionalBitList ::= '{' RangeList '}'
653/// OptionalBitList ::= /*empty*/
654bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
655 if (Lex.getCode() != tgtok::l_brace)
656 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000657
Chris Lattner526c8cb2009-06-21 03:39:35 +0000658 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000659 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000660
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000661 // Parse the range list.
662 Ranges = ParseRangeList();
663 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000664
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000665 if (Lex.getCode() != tgtok::r_brace) {
666 TokError("expected '}' at end of bit list");
667 return Error(StartLoc, "to match this '{'");
668 }
669 Lex.Lex(); // eat the '}'.
670 return false;
671}
672
673
674/// ParseType - Parse and return a tblgen type. This returns null on error.
675///
676/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000677/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000678/// Type ::= BIT // bit type
679/// Type ::= BITS '<' INTVAL '>' // bits<x> type
680/// Type ::= INT // int type
681/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000682/// Type ::= DAG // dag type
683/// Type ::= ClassID // Record Type
684///
685RecTy *TGParser::ParseType() {
686 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000687 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000688 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000689 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000690 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
691 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000692 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000693 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000694 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000695 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000696 case tgtok::Bits: {
697 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
698 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000699 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000700 }
701 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
702 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000703 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000704 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000705 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000706 if (Lex.Lex() != tgtok::greater) { // Eat count.
707 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000708 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000709 }
710 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000711 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000712 }
713 case tgtok::List: {
714 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
715 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000716 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000717 }
718 Lex.Lex(); // Eat '<'
719 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000720 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000721
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000722 if (Lex.getCode() != tgtok::greater) {
723 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000724 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000725 }
726 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000727 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000728 }
Bob Wilson7248f862009-11-22 04:24:42 +0000729 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000730}
731
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000732/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
733/// has already been read.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000734Init *TGParser::ParseIDValue(Record *CurRec,
David Greened4263a62011-10-19 13:04:20 +0000735 const std::string &Name, SMLoc NameLoc,
736 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000737 if (CurRec) {
738 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000739 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000740
David Greenedb10e692011-10-19 13:02:42 +0000741 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
742
David Greene47a665e2011-10-05 22:42:54 +0000743 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +0000744 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
745 "::");
David Greene47a665e2011-10-05 22:42:54 +0000746
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000747 if (CurRec->isTemplateArg(TemplateArgName)) {
748 const RecordVal *RV = CurRec->getValue(TemplateArgName);
749 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000750 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000751 }
752 }
Bob Wilson7248f862009-11-22 04:24:42 +0000753
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000754 if (CurMultiClass) {
David Greenedb10e692011-10-19 13:02:42 +0000755 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
756 "::");
757
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000758 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
759 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
760 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000761 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000762 }
763 }
Bob Wilson7248f862009-11-22 04:24:42 +0000764
David Greenefb927af2012-02-22 16:09:41 +0000765 // If this is in a foreach loop, make sure it's not a loop iterator
766 for (LoopVector::iterator i = Loops.begin(), iend = Loops.end();
767 i != iend;
768 ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000769 VarInit *IterVar = dyn_cast<VarInit>(i->IterVar);
David Greenefb927af2012-02-22 16:09:41 +0000770 if (IterVar && IterVar->getName() == Name)
771 return IterVar;
772 }
773
David Greene232bd602011-10-19 13:04:21 +0000774 if (Mode == ParseNameMode)
775 return StringInit::get(Name);
776
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000777 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000778 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000779
David Greene232bd602011-10-19 13:04:21 +0000780 if (Mode == ParseValueMode) {
781 Error(NameLoc, "Variable not defined: '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000782 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000783 }
784
785 return StringInit::get(Name);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000786}
787
David Greene5d0c0512009-05-14 20:54:48 +0000788/// ParseOperation - Parse an operator. This returns null on error.
789///
790/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
791///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000792Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000793 switch (Lex.getCode()) {
794 default:
795 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000796 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000797 case tgtok::XHead:
798 case tgtok::XTail:
799 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000800 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
801 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000802 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000803
David Greenee8f3b272009-05-14 21:22:49 +0000804 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000805 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000806 case tgtok::XCast:
807 Lex.Lex(); // eat the operation
808 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000809
David Greenee8f3b272009-05-14 21:22:49 +0000810 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000811
Craig Topper011817a2014-04-09 04:50:04 +0000812 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000813 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000814 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000815 }
David Greene5d0c0512009-05-14 20:54:48 +0000816
David Greenee8f3b272009-05-14 21:22:49 +0000817 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000818 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000819 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000820 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000821 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000822 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000823 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000824 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000825 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000826 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000827 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000828 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000829 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000830 break;
David Greenee8f3b272009-05-14 21:22:49 +0000831 }
832 if (Lex.getCode() != tgtok::l_paren) {
833 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000834 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000835 }
836 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000837
David Greeneaf8ee2c2011-07-29 22:43:06 +0000838 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000839 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000840
David Greene2f7cf7f2011-01-07 17:05:37 +0000841 if (Code == UnOpInit::HEAD
842 || Code == UnOpInit::TAIL
843 || Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000844 ListInit *LHSl = dyn_cast<ListInit>(LHS);
845 StringInit *LHSs = dyn_cast<StringInit>(LHS);
846 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000847 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000848 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000849 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000850 }
851 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000852 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
853 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000854 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000855 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000856 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000857 }
858 }
859
David Greene2f7cf7f2011-01-07 17:05:37 +0000860 if (Code == UnOpInit::HEAD
861 || Code == UnOpInit::TAIL) {
Craig Topper011817a2014-04-09 04:50:04 +0000862 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000863 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000864 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000865 }
Bob Wilson7248f862009-11-22 04:24:42 +0000866
David Greened571b3c2009-05-14 22:38:31 +0000867 if (LHSl && LHSl->getSize() == 0) {
868 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000869 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000870 }
871 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000872 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000873 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000874 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000875 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000876 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000877 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000878 if (Code == UnOpInit::HEAD) {
David Greened571b3c2009-05-14 22:38:31 +0000879 Type = Itemt->getType();
Bob Wilson7248f862009-11-22 04:24:42 +0000880 } else {
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000881 Type = ListRecTy::get(Itemt->getType());
David Greened571b3c2009-05-14 22:38:31 +0000882 }
Bob Wilson7248f862009-11-22 04:24:42 +0000883 } else {
David Greened571b3c2009-05-14 22:38:31 +0000884 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000885 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000886 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000887 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000888 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000889 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000890 if (Code == UnOpInit::HEAD) {
David Greened571b3c2009-05-14 22:38:31 +0000891 Type = LType->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +0000892 } else {
David Greened571b3c2009-05-14 22:38:31 +0000893 Type = LType;
894 }
895 }
896 }
897 }
898
David Greenee8f3b272009-05-14 21:22:49 +0000899 if (Lex.getCode() != tgtok::r_paren) {
900 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000901 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000902 }
903 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000904 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000905 }
David Greene5d0c0512009-05-14 20:54:48 +0000906
907 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000908 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000909 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +0000910 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000911 case tgtok::XSRL:
912 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000913 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000914 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000915 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000916 tgtok::TokKind OpTok = Lex.getCode();
917 SMLoc OpLoc = Lex.getLoc();
918 Lex.Lex(); // eat the operation
919
David Greene5d0c0512009-05-14 20:54:48 +0000920 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000921 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000922
Chris Lattner61ea00b2010-10-05 23:58:18 +0000923 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000924 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000925 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000926 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000927 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000928 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
929 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
930 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
931 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000932 case tgtok::XListConcat:
933 Code = BinOpInit::LISTCONCAT;
934 // We don't know the list type until we parse the first argument
935 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000936 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000937 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000938 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000939 break;
David Greene5d0c0512009-05-14 20:54:48 +0000940 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000941
David Greene5d0c0512009-05-14 20:54:48 +0000942 if (Lex.getCode() != tgtok::l_paren) {
943 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000944 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000945 }
946 Lex.Lex(); // eat the '('
947
David Greeneaf8ee2c2011-07-29 22:43:06 +0000948 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000949
Chris Lattner61ea00b2010-10-05 23:58:18 +0000950 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000951 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000952
Chris Lattner61ea00b2010-10-05 23:58:18 +0000953 while (Lex.getCode() == tgtok::comma) {
954 Lex.Lex(); // eat the ','
955
956 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000957 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000958 }
David Greene5d0c0512009-05-14 20:54:48 +0000959
960 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000961 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000962 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000963 }
964 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000965
Daniel Sanders314e80e2014-05-07 10:13:19 +0000966 // If we are doing !listconcat, we should know the type by now
967 if (OpTok == tgtok::XListConcat) {
968 if (VarInit *Arg0 = dyn_cast<VarInit>(InitList[0]))
969 Type = Arg0->getType();
970 else if (ListInit *Arg0 = dyn_cast<ListInit>(InitList[0]))
971 Type = Arg0->getType();
972 else {
973 InitList[0]->dump();
974 Error(OpLoc, "expected a list");
975 return nullptr;
976 }
977 }
978
Chris Lattner61ea00b2010-10-05 23:58:18 +0000979 // We allow multiple operands to associative operators like !strconcat as
980 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000981 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000982 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000983 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000984 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
985 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000986 InitList.back() = RHS;
987 }
988 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000989
Chris Lattner61ea00b2010-10-05 23:58:18 +0000990 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000991 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000992 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000993
Chris Lattner61ea00b2010-10-05 23:58:18 +0000994 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +0000995 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000996 }
997
David Greene3587eed2009-05-14 23:26:46 +0000998 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +0000999 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001000 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
1001 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +00001002 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001003
David Greene98ed3c72009-05-14 21:54:42 +00001004 tgtok::TokKind LexCode = Lex.getCode();
1005 Lex.Lex(); // eat the operation
1006 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001007 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001008 case tgtok::XIf:
1009 Code = TernOpInit::IF;
1010 break;
David Greenee917fff2009-05-14 22:23:47 +00001011 case tgtok::XForEach:
1012 Code = TernOpInit::FOREACH;
1013 break;
David Greene98ed3c72009-05-14 21:54:42 +00001014 case tgtok::XSubst:
1015 Code = TernOpInit::SUBST;
1016 break;
1017 }
1018 if (Lex.getCode() != tgtok::l_paren) {
1019 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001020 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001021 }
1022 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001023
David Greeneaf8ee2c2011-07-29 22:43:06 +00001024 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001025 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001026
David Greene98ed3c72009-05-14 21:54:42 +00001027 if (Lex.getCode() != tgtok::comma) {
1028 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001029 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001030 }
1031 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001032
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001033 Init *MHS = ParseValue(CurRec, ItemType);
1034 if (!MHS)
1035 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001036
David Greene98ed3c72009-05-14 21:54:42 +00001037 if (Lex.getCode() != tgtok::comma) {
1038 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001039 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001040 }
1041 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001042
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001043 Init *RHS = ParseValue(CurRec, ItemType);
1044 if (!RHS)
1045 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001046
David Greene98ed3c72009-05-14 21:54:42 +00001047 if (Lex.getCode() != tgtok::r_paren) {
1048 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001049 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001050 }
1051 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001052
David Greene98ed3c72009-05-14 21:54:42 +00001053 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001054 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001055 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001056 RecTy *MHSTy = nullptr;
1057 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001058
Sean Silvafb509ed2012-10-10 20:24:43 +00001059 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001060 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001061 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001062 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001063 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001064 MHSTy = BitRecTy::get();
1065
Sean Silvafb509ed2012-10-10 20:24:43 +00001066 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001067 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001068 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001069 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001070 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001071 RHSTy = BitRecTy::get();
1072
1073 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001074 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001075 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001076 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001077 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001078
1079 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001080 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001081 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001082 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001083
1084 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1085 Type = RHSTy;
1086 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1087 Type = MHSTy;
Bob Wilson7248f862009-11-22 04:24:42 +00001088 } else {
David Greene3587eed2009-05-14 23:26:46 +00001089 TokError("inconsistent types for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001090 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001091 }
1092 break;
1093 }
David Greenee917fff2009-05-14 22:23:47 +00001094 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001095 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
Craig Topper011817a2014-04-09 04:50:04 +00001096 if (!MHSt) {
David Greenee917fff2009-05-14 22:23:47 +00001097 TokError("could not get type for !foreach");
Craig Topper011817a2014-04-09 04:50:04 +00001098 return nullptr;
David Greenee917fff2009-05-14 22:23:47 +00001099 }
1100 Type = MHSt->getType();
1101 break;
1102 }
David Greene98ed3c72009-05-14 21:54:42 +00001103 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001104 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001105 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001106 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001107 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001108 }
1109 Type = RHSt->getType();
1110 break;
1111 }
1112 }
David Greenee32ebf22011-07-29 19:07:07 +00001113 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001114 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001115 }
David Greene5d0c0512009-05-14 20:54:48 +00001116 }
David Greene5d0c0512009-05-14 20:54:48 +00001117}
1118
1119/// ParseOperatorType - Parse a type for an operator. This returns
1120/// null on error.
1121///
1122/// OperatorType ::= '<' Type '>'
1123///
Dan Gohman1432ef82009-08-12 22:10:57 +00001124RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001125 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001126
1127 if (Lex.getCode() != tgtok::less) {
1128 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001129 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001130 }
1131 Lex.Lex(); // eat the <
1132
1133 Type = ParseType();
1134
Craig Topper011817a2014-04-09 04:50:04 +00001135 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001136 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001137 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001138 }
1139
1140 if (Lex.getCode() != tgtok::greater) {
1141 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001142 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001143 }
1144 Lex.Lex(); // eat the >
1145
1146 return Type;
1147}
1148
1149
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001150/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1151///
1152/// SimpleValue ::= IDValue
1153/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001154/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001155/// SimpleValue ::= CODEFRAGMENT
1156/// SimpleValue ::= '?'
1157/// SimpleValue ::= '{' ValueList '}'
1158/// SimpleValue ::= ID '<' ValueListNE '>'
1159/// SimpleValue ::= '[' ValueList ']'
1160/// SimpleValue ::= '(' IDValue DagArgList ')'
1161/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001162/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001163/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1164/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1165/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001166/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001167/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1168///
David Greened4263a62011-10-19 13:04:20 +00001169Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1170 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001171 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001172 switch (Lex.getCode()) {
1173 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001174 case tgtok::paste:
1175 // This is a leading paste operation. This is deprecated but
1176 // still exists in some .td files. Ignore it.
1177 Lex.Lex(); // Skip '#'.
1178 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001179 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001180 case tgtok::BinaryIntVal: {
1181 auto BinaryVal = Lex.getCurBinaryIntVal();
1182 SmallVector<Init*, 16> Bits(BinaryVal.second);
1183 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001184 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001185 R = BitsInit::get(Bits);
1186 Lex.Lex();
1187 break;
1188 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001189 case tgtok::StrVal: {
1190 std::string Val = Lex.getCurStrVal();
1191 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001192
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001193 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001194 while (Lex.getCode() == tgtok::StrVal) {
1195 Val += Lex.getCurStrVal();
1196 Lex.Lex();
1197 }
Bob Wilson7248f862009-11-22 04:24:42 +00001198
David Greenee32ebf22011-07-29 19:07:07 +00001199 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001200 break;
1201 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001202 case tgtok::CodeFragment:
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +00001203 R = StringInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001204 Lex.Lex();
1205 break;
1206 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001207 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001208 Lex.Lex();
1209 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001210 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001211 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001212 std::string Name = Lex.getCurStrVal();
1213 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001214 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001215
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001216 // Value ::= ID '<' ValueListNE '>'
1217 if (Lex.Lex() == tgtok::greater) {
1218 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001219 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001220 }
David Greene8618f952009-06-08 20:23:18 +00001221
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001222 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1223 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1224 // body.
1225 Record *Class = Records.getClass(Name);
1226 if (!Class) {
1227 Error(NameLoc, "Expected a class name, got '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001228 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001229 }
David Greene8618f952009-06-08 20:23:18 +00001230
David Greeneaf8ee2c2011-07-29 22:43:06 +00001231 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
Craig Topper011817a2014-04-09 04:50:04 +00001232 if (ValueList.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001233
David Greene8618f952009-06-08 20:23:18 +00001234 if (Lex.getCode() != tgtok::greater) {
1235 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001236 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001237 }
1238 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001239 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001240
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001241 // Create the new record, set it as CurRec temporarily.
Craig Topper84138712014-11-29 05:31:10 +00001242 auto NewRecOwner = make_unique<Record>(GetNewAnonymousName(), NameLoc,
1243 Records, /*IsAnonymous=*/true);
1244 Record *NewRec = NewRecOwner.get(); // Keep a copy since we may release.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001245 SubClassReference SCRef;
Jordan Rosef12e8a92013-01-10 18:50:11 +00001246 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001247 SCRef.Rec = Class;
1248 SCRef.TemplateArgs = ValueList;
1249 // Add info about the subclass to NewRec.
Craig Topper84138712014-11-29 05:31:10 +00001250 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001251 return nullptr;
Craig Topper84138712014-11-29 05:31:10 +00001252
Hal Finkela8c1f462014-01-02 20:47:09 +00001253 if (!CurMultiClass) {
1254 NewRec->resolveReferences();
Craig Toppercdab2322014-11-29 05:52:51 +00001255 Records.addDef(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001256 } else {
Adam Nemete5a07162014-09-16 17:14:13 +00001257 // This needs to get resolved once the multiclass template arguments are
1258 // known before any use.
1259 NewRec->setResolveFirst(true);
Hal Finkela8c1f462014-01-02 20:47:09 +00001260 // Otherwise, we're inside a multiclass, add it to the multiclass.
Craig Topper84138712014-11-29 05:31:10 +00001261 CurMultiClass->DefPrototypes.push_back(NewRecOwner.release());
Hal Finkela8c1f462014-01-02 20:47:09 +00001262
1263 // Copy the template arguments for the multiclass into the def.
1264 const std::vector<Init *> &TArgs =
1265 CurMultiClass->Rec.getTemplateArgs();
1266
1267 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1268 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1269 assert(RV && "Template arg doesn't exist?");
1270 NewRec->addValue(*RV);
1271 }
1272
1273 // We can't return the prototype def here, instead return:
1274 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1275 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1276 assert(MCNameRV && "multiclass record must have a NAME");
1277
1278 return UnOpInit::get(UnOpInit::CAST,
1279 BinOpInit::get(BinOpInit::STRCONCAT,
1280 VarInit::get(MCNameRV->getName(),
1281 MCNameRV->getType()),
1282 NewRec->getNameInit(),
1283 StringRecTy::get()),
1284 Class->getDefInit()->getType());
1285 }
Bob Wilson7248f862009-11-22 04:24:42 +00001286
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001287 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001288 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001289 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001290 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001291 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001292 Lex.Lex(); // eat the '{'
David Greeneaf8ee2c2011-07-29 22:43:06 +00001293 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001294
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001295 if (Lex.getCode() != tgtok::r_brace) {
1296 Vals = ParseValueList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001297 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001298 }
1299 if (Lex.getCode() != tgtok::r_brace) {
1300 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001301 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001302 }
1303 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001304
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001305 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001306
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001307 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1308 // first. We'll first read everything in to a vector, then we can reverse
1309 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001310 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001311 // FIXME: The following two loops would not be duplicated
1312 // if the API was a little more orthogonal.
1313
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001314 // bits<n> values are allowed to initialize n bits.
1315 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1316 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1317 NewBits.push_back(BI->getBit((e - i) - 1));
1318 continue;
1319 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001320 // bits<n> can also come from variable initializers.
1321 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1322 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1323 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1324 NewBits.push_back(VI->getBit((e - i) - 1));
1325 continue;
1326 }
1327 // Fallthrough to try convert this to a bit.
1328 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001329 // All other values must be convertible to just a single bit.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001330 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001331 if (!Bit) {
Chris Lattner52416952007-11-22 21:06:59 +00001332 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1333 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001334 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001335 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001336 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001337 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001338 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001339 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001340 }
1341 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1342 Lex.Lex(); // eat the '['
David Greeneaf8ee2c2011-07-29 22:43:06 +00001343 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001344
Craig Topper011817a2014-04-09 04:50:04 +00001345 RecTy *DeducedEltTy = nullptr;
1346 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001347
Craig Topper011817a2014-04-09 04:50:04 +00001348 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001349 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001350 if (!ListType) {
Alp Tokere69170a2014-06-26 22:52:05 +00001351 std::string s;
1352 raw_string_ostream ss(s);
Reid Klecknerd78273f2013-08-06 22:51:21 +00001353 ss << "Type mismatch for list, expected list type, got "
1354 << ItemType->getAsString();
1355 TokError(ss.str());
Craig Topper011817a2014-04-09 04:50:04 +00001356 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001357 }
1358 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001359 }
David Greene8618f952009-06-08 20:23:18 +00001360
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001361 if (Lex.getCode() != tgtok::r_square) {
Craig Topper011817a2014-04-09 04:50:04 +00001362 Vals = ParseValueList(CurRec, nullptr,
1363 GivenListTy ? GivenListTy->getElementType() : nullptr);
1364 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001365 }
1366 if (Lex.getCode() != tgtok::r_square) {
1367 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001368 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001369 }
1370 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001371
Craig Topper011817a2014-04-09 04:50:04 +00001372 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001373 if (Lex.getCode() == tgtok::less) {
1374 // Optional list element type
1375 Lex.Lex(); // eat the '<'
1376
1377 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001378 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001379 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001380 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001381 }
1382
1383 if (Lex.getCode() != tgtok::greater) {
1384 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001385 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001386 }
1387 Lex.Lex(); // eat the '>'
1388 }
1389
1390 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001391 RecTy *EltTy = nullptr;
David Greeneaf8ee2c2011-07-29 22:43:06 +00001392 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greene8618f952009-06-08 20:23:18 +00001393 i != ie;
1394 ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +00001395 TypedInit *TArg = dyn_cast<TypedInit>(*i);
Craig Topper011817a2014-04-09 04:50:04 +00001396 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001397 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001398 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001399 }
Craig Topper011817a2014-04-09 04:50:04 +00001400 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001401 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001402 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001403 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001404 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001405 }
Bob Wilson7248f862009-11-22 04:24:42 +00001406 } else {
David Greene8618f952009-06-08 20:23:18 +00001407 EltTy = TArg->getType();
1408 }
1409 }
1410
Craig Topper011817a2014-04-09 04:50:04 +00001411 if (GivenEltTy) {
1412 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001413 // Verify consistency
1414 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1415 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001416 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001417 }
1418 }
1419 EltTy = GivenEltTy;
1420 }
1421
Craig Topper011817a2014-04-09 04:50:04 +00001422 if (!EltTy) {
1423 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001424 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001425 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001426 }
1427 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001428 } else {
David Greene8618f952009-06-08 20:23:18 +00001429 // Make sure the deduced type is compatible with the given type
1430 if (GivenListTy) {
1431 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1432 TokError("Element type mismatch for list");
Craig Topper011817a2014-04-09 04:50:04 +00001433 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001434 }
1435 }
1436 DeducedEltTy = EltTy;
1437 }
Bob Wilson7248f862009-11-22 04:24:42 +00001438
David Greenee32ebf22011-07-29 19:07:07 +00001439 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001440 }
1441 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1442 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001443 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001444 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001445 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001446 }
Bob Wilson7248f862009-11-22 04:24:42 +00001447
David Greeneaf8ee2c2011-07-29 22:43:06 +00001448 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001449 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001450
Nate Begemandbe3f772009-03-19 05:21:56 +00001451 // If the operator name is present, parse it.
1452 std::string OperatorName;
1453 if (Lex.getCode() == tgtok::colon) {
1454 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1455 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001456 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001457 }
1458 OperatorName = Lex.getCurStrVal();
1459 Lex.Lex(); // eat the VarName.
1460 }
Bob Wilson7248f862009-11-22 04:24:42 +00001461
David Greeneaf8ee2c2011-07-29 22:43:06 +00001462 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001463 if (Lex.getCode() != tgtok::r_paren) {
1464 DagArgs = ParseDagArgList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001465 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001466 }
Bob Wilson7248f862009-11-22 04:24:42 +00001467
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001468 if (Lex.getCode() != tgtok::r_paren) {
1469 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001470 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001471 }
1472 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001473
David Greenee32ebf22011-07-29 19:07:07 +00001474 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001475 }
Bob Wilson7248f862009-11-22 04:24:42 +00001476
David Greene2f7cf7f2011-01-07 17:05:37 +00001477 case tgtok::XHead:
1478 case tgtok::XTail:
1479 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001480 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001481 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001482 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001483 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +00001484 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001485 case tgtok::XSRL:
1486 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001487 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001488 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001489 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001490 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001491 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001492 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001493 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001494 }
1495 }
Bob Wilson7248f862009-11-22 04:24:42 +00001496
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001497 return R;
1498}
1499
1500/// ParseValue - Parse a tblgen value. This returns null on error.
1501///
1502/// Value ::= SimpleValue ValueSuffix*
1503/// ValueSuffix ::= '{' BitList '}'
1504/// ValueSuffix ::= '[' BitList ']'
1505/// ValueSuffix ::= '.' ID
1506///
David Greened4263a62011-10-19 13:04:20 +00001507Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1508 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001509 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001510
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001511 // Parse the suffixes now if present.
1512 while (1) {
1513 switch (Lex.getCode()) {
1514 default: return Result;
1515 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001516 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001517 // This is the beginning of the object body.
1518 return Result;
1519
Chris Lattner526c8cb2009-06-21 03:39:35 +00001520 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001521 Lex.Lex(); // eat the '{'
1522 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001523 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001524
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001525 // Reverse the bitlist.
1526 std::reverse(Ranges.begin(), Ranges.end());
1527 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001528 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001529 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001530 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001531 }
Bob Wilson7248f862009-11-22 04:24:42 +00001532
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001533 // Eat the '}'.
1534 if (Lex.getCode() != tgtok::r_brace) {
1535 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001536 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001537 }
1538 Lex.Lex();
1539 break;
1540 }
1541 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001542 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001543 Lex.Lex(); // eat the '['
1544 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001545 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001546
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001547 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001548 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001549 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001550 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001551 }
Bob Wilson7248f862009-11-22 04:24:42 +00001552
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001553 // Eat the ']'.
1554 if (Lex.getCode() != tgtok::r_square) {
1555 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001556 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001557 }
1558 Lex.Lex();
1559 break;
1560 }
1561 case tgtok::period:
1562 if (Lex.Lex() != tgtok::Id) { // eat the .
1563 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001564 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001565 }
1566 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001567 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001568 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001569 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001570 }
David Greenee32ebf22011-07-29 19:07:07 +00001571 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001572 Lex.Lex(); // eat field name
1573 break;
David Greene8e85b482011-10-19 13:04:43 +00001574
1575 case tgtok::paste:
1576 SMLoc PasteLoc = Lex.getLoc();
1577
1578 // Create a !strconcat() operation, first casting each operand to
1579 // a string if necessary.
1580
Sean Silvafb509ed2012-10-10 20:24:43 +00001581 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001582 if (!LHS) {
1583 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001584 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001585 }
1586
1587 if (LHS->getType() != StringRecTy::get()) {
1588 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1589 }
1590
Craig Topper011817a2014-04-09 04:50:04 +00001591 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001592
1593 Lex.Lex(); // Eat the '#'.
1594 switch (Lex.getCode()) {
1595 case tgtok::colon:
1596 case tgtok::semi:
1597 case tgtok::l_brace:
1598 // These are all of the tokens that can begin an object body.
1599 // Some of these can also begin values but we disallow those cases
1600 // because they are unlikely to be useful.
1601
1602 // Trailing paste, concat with an empty string.
1603 RHS = StringInit::get("");
1604 break;
1605
1606 default:
1607 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001608 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001609 if (!RHS) {
1610 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001611 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001612 }
1613
1614 if (RHS->getType() != StringRecTy::get()) {
1615 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1616 }
1617
1618 break;
1619 }
1620
1621 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1622 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1623 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001624 }
1625 }
1626}
1627
1628/// ParseDagArgList - Parse the argument list for a dag literal expression.
1629///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001630/// DagArg ::= Value (':' VARNAME)?
1631/// DagArg ::= VARNAME
1632/// DagArgList ::= DagArg
1633/// DagArgList ::= DagArgList ',' DagArg
David Greeneaf8ee2c2011-07-29 22:43:06 +00001634std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001635TGParser::ParseDagArgList(Record *CurRec) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001636 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson7248f862009-11-22 04:24:42 +00001637
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001638 while (1) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001639 // DagArg ::= VARNAME
1640 if (Lex.getCode() == tgtok::VarName) {
1641 // A missing value is treated like '?'.
1642 Result.push_back(std::make_pair(UnsetInit::get(), Lex.getCurStrVal()));
1643 Lex.Lex();
1644 } else {
1645 // DagArg ::= Value (':' VARNAME)?
1646 Init *Val = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001647 if (!Val)
David Greeneaf8ee2c2011-07-29 22:43:06 +00001648 return std::vector<std::pair<llvm::Init*, std::string> >();
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001649
1650 // If the variable name is present, add it.
1651 std::string VarName;
1652 if (Lex.getCode() == tgtok::colon) {
1653 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1654 TokError("expected variable name in dag literal");
1655 return std::vector<std::pair<llvm::Init*, std::string> >();
1656 }
1657 VarName = Lex.getCurStrVal();
1658 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001659 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001660
1661 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001662 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001663 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001664 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001665 }
Bob Wilson7248f862009-11-22 04:24:42 +00001666
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001667 return Result;
1668}
1669
1670
1671/// ParseValueList - Parse a comma separated list of values, returning them as a
1672/// vector. Note that this always expects to be able to parse at least one
1673/// value. It returns an empty list if this is not possible.
1674///
1675/// ValueList ::= Value (',' Value)
1676///
David Greeneaf8ee2c2011-07-29 22:43:06 +00001677std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopher71520a82011-07-11 23:06:52 +00001678 RecTy *EltTy) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001679 std::vector<Init*> Result;
David Greene8618f952009-06-08 20:23:18 +00001680 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001681 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001682 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001683 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001684 if (!TArgs.size()) {
1685 TokError("template argument provided to non-template class");
1686 return std::vector<Init*>();
1687 }
David Greene8618f952009-06-08 20:23:18 +00001688 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001689 if (!RV) {
1690 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1691 << ")\n";
1692 }
David Greene8618f952009-06-08 20:23:18 +00001693 assert(RV && "Template argument record not found??");
1694 ItemType = RV->getType();
1695 ++ArgN;
1696 }
1697 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001698 if (!Result.back()) return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001699
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001700 while (Lex.getCode() == tgtok::comma) {
1701 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001702
Craig Topper011817a2014-04-09 04:50:04 +00001703 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001704 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001705 if (ArgN >= TArgs.size()) {
1706 TokError("too many template arguments");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001707 return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001708 }
David Greene8618f952009-06-08 20:23:18 +00001709 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1710 assert(RV && "Template argument record not found??");
1711 ItemType = RV->getType();
1712 ++ArgN;
1713 }
1714 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001715 if (!Result.back()) return std::vector<Init*>();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001716 }
Bob Wilson7248f862009-11-22 04:24:42 +00001717
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001718 return Result;
1719}
1720
1721
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001722/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1723/// empty string on error. This can happen in a number of different context's,
1724/// including within a def or in the template args for a def (which which case
1725/// CurRec will be non-null) and within the template args for a multiclass (in
1726/// which case CurRec will be null, but CurMultiClass will be set). This can
1727/// also happen within a def that is within a multiclass, which will set both
1728/// CurRec and CurMultiClass.
1729///
1730/// Declaration ::= FIELD? Type ID ('=' Value)?
1731///
David Greenedb10e692011-10-19 13:02:42 +00001732Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001733 bool ParsingTemplateArgs) {
1734 // Read the field prefix if present.
1735 bool HasField = Lex.getCode() == tgtok::Field;
1736 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001737
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001738 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001739 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001740
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001741 if (Lex.getCode() != tgtok::Id) {
1742 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001743 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001744 }
Bob Wilson7248f862009-11-22 04:24:42 +00001745
Chris Lattner526c8cb2009-06-21 03:39:35 +00001746 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001747 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001748 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001749
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001750 if (ParsingTemplateArgs) {
1751 if (CurRec) {
David Greenedb10e692011-10-19 13:02:42 +00001752 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001753 } else {
1754 assert(CurMultiClass);
1755 }
1756 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001757 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1758 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001759 }
Bob Wilson7248f862009-11-22 04:24:42 +00001760
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001761 // Add the value.
1762 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001763 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001764
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001765 // If a value is present, parse it.
1766 if (Lex.getCode() == tgtok::equal) {
1767 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001768 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001769 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001770 if (!Val ||
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001771 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001772 // Return the name, even if an error is thrown. This is so that we can
1773 // continue to make some progress, even without the value having been
1774 // initialized.
1775 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001776 }
Bob Wilson7248f862009-11-22 04:24:42 +00001777
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001778 return DeclName;
1779}
1780
David Greenefb927af2012-02-22 16:09:41 +00001781/// ParseForeachDeclaration - Read a foreach declaration, returning
1782/// the name of the declared object or a NULL Init on error. Return
1783/// the name of the parsed initializer list through ForeachListName.
1784///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001785/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1786/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1787/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001788///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001789VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001790 if (Lex.getCode() != tgtok::Id) {
1791 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001792 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001793 }
1794
1795 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1796 Lex.Lex();
1797
1798 // If a value is present, parse it.
1799 if (Lex.getCode() != tgtok::equal) {
1800 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001801 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001802 }
1803 Lex.Lex(); // Eat the '='
1804
Craig Topper011817a2014-04-09 04:50:04 +00001805 RecTy *IterType = nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001806 std::vector<unsigned> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001807
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001808 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001809 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001810 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001811 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001812 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001813 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001814 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001815 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001816 }
1817 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001818 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001819 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001820 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001821 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001822 }
1823 IterType = ListType->getElementType();
1824 break;
David Greenefb927af2012-02-22 16:09:41 +00001825 }
1826
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001827 case tgtok::IntVal: { // RangePiece.
1828 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001829 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001830 break;
David Greenefb927af2012-02-22 16:09:41 +00001831 }
1832
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001833 case tgtok::l_brace: { // '{' RangeList '}'
1834 Lex.Lex(); // eat the '{'
1835 Ranges = ParseRangeList();
1836 if (Lex.getCode() != tgtok::r_brace) {
1837 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001838 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001839 }
1840 Lex.Lex();
1841 break;
1842 }
1843 }
David Greenefb927af2012-02-22 16:09:41 +00001844
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001845 if (!Ranges.empty()) {
1846 assert(!IterType && "Type already initialized?");
1847 IterType = IntRecTy::get();
1848 std::vector<Init*> Values;
1849 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
1850 Values.push_back(IntInit::get(Ranges[i]));
1851 ForeachListValue = ListInit::get(Values, IterType);
1852 }
1853
1854 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001855 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001856
1857 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001858}
1859
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001860/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1861/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1862/// template args for a def, which may or may not be in a multiclass. If null,
1863/// these are the template args for a multiclass.
1864///
1865/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001866///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001867bool TGParser::ParseTemplateArgList(Record *CurRec) {
1868 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1869 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001870
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001871 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001872
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001873 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001874 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001875 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001876 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001877
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001878 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001879
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001880 while (Lex.getCode() == tgtok::comma) {
1881 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001882
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001883 // Read the following declarations.
1884 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001885 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001886 return true;
1887 TheRecToAddTo->addTemplateArg(TemplArg);
1888 }
Bob Wilson7248f862009-11-22 04:24:42 +00001889
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001890 if (Lex.getCode() != tgtok::greater)
1891 return TokError("expected '>' at end of template argument list");
1892 Lex.Lex(); // eat the '>'.
1893 return false;
1894}
1895
1896
1897/// ParseBodyItem - Parse a single item at within the body of a def or class.
1898///
1899/// BodyItem ::= Declaration ';'
1900/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1901bool TGParser::ParseBodyItem(Record *CurRec) {
1902 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001903 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001904 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001905
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001906 if (Lex.getCode() != tgtok::semi)
1907 return TokError("expected ';' after declaration");
1908 Lex.Lex();
1909 return false;
1910 }
1911
1912 // LET ID OptionalRangeList '=' Value ';'
1913 if (Lex.Lex() != tgtok::Id)
1914 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001915
Chris Lattner526c8cb2009-06-21 03:39:35 +00001916 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001917 std::string FieldName = Lex.getCurStrVal();
1918 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001919
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001920 std::vector<unsigned> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001921 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001922 return true;
1923 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001924
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001925 if (Lex.getCode() != tgtok::equal)
1926 return TokError("expected '=' in let expression");
1927 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001928
David Greene8618f952009-06-08 20:23:18 +00001929 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001930 if (!Field)
David Greene8618f952009-06-08 20:23:18 +00001931 return TokError("Value '" + FieldName + "' unknown!");
1932
1933 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001934
David Greeneaf8ee2c2011-07-29 22:43:06 +00001935 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001936 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001937
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001938 if (Lex.getCode() != tgtok::semi)
1939 return TokError("expected ';' after let expression");
1940 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001941
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001942 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1943}
1944
1945/// ParseBody - Read the body of a class or def. Return true on error, false on
1946/// success.
1947///
1948/// Body ::= ';'
1949/// Body ::= '{' BodyList '}'
1950/// BodyList BodyItem*
1951///
1952bool TGParser::ParseBody(Record *CurRec) {
1953 // If this is a null definition, just eat the semi and return.
1954 if (Lex.getCode() == tgtok::semi) {
1955 Lex.Lex();
1956 return false;
1957 }
Bob Wilson7248f862009-11-22 04:24:42 +00001958
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001959 if (Lex.getCode() != tgtok::l_brace)
1960 return TokError("Expected ';' or '{' to start body");
1961 // Eat the '{'.
1962 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001963
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001964 while (Lex.getCode() != tgtok::r_brace)
1965 if (ParseBodyItem(CurRec))
1966 return true;
1967
1968 // Eat the '}'.
1969 Lex.Lex();
1970 return false;
1971}
1972
Sean Silvacb1a75e2013-01-09 04:49:14 +00001973/// \brief Apply the current let bindings to \a CurRec.
1974/// \returns true on error, false otherwise.
1975bool TGParser::ApplyLetStack(Record *CurRec) {
1976 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1977 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1978 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1979 LetStack[i][j].Bits, LetStack[i][j].Value))
1980 return true;
1981 return false;
1982}
1983
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001984/// ParseObjectBody - Parse the body of a def or class. This consists of an
1985/// optional ClassList followed by a Body. CurRec is the current def or class
1986/// that is being parsed.
1987///
1988/// ObjectBody ::= BaseClassList Body
1989/// BaseClassList ::= /*empty*/
1990/// BaseClassList ::= ':' BaseClassListNE
1991/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1992///
1993bool TGParser::ParseObjectBody(Record *CurRec) {
1994 // If there is a baseclass list, read it.
1995 if (Lex.getCode() == tgtok::colon) {
1996 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001997
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001998 // Read all of the subclasses.
1999 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
2000 while (1) {
2001 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002002 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002003
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002004 // Add it.
2005 if (AddSubClass(CurRec, SubClass))
2006 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002007
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002008 if (Lex.getCode() != tgtok::comma) break;
2009 Lex.Lex(); // eat ','.
2010 SubClass = ParseSubClassReference(CurRec, false);
2011 }
2012 }
2013
Sean Silvacb1a75e2013-01-09 04:49:14 +00002014 if (ApplyLetStack(CurRec))
2015 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002016
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002017 return ParseBody(CurRec);
2018}
2019
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002020/// ParseDef - Parse and return a top level or multiclass def, return the record
2021/// corresponding to it. This returns null on error.
2022///
2023/// DefInst ::= DEF ObjectName ObjectBody
2024///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002025bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00002026 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002027 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00002028 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002029
2030 // Parse ObjectName and make a record for it.
Craig Topper84138712014-11-29 05:31:10 +00002031 std::unique_ptr<Record> CurRecOwner;
Jordan Roseabdd99b2013-01-10 18:50:05 +00002032 Init *Name = ParseObjectName(CurMultiClass);
2033 if (Name)
Craig Topper84138712014-11-29 05:31:10 +00002034 CurRecOwner = make_unique<Record>(Name, DefLoc, Records);
Jordan Roseabdd99b2013-01-10 18:50:05 +00002035 else
Craig Topper84138712014-11-29 05:31:10 +00002036 CurRecOwner = make_unique<Record>(GetNewAnonymousName(), DefLoc, Records,
2037 /*IsAnonymous=*/true);
2038 Record *CurRec = CurRecOwner.get(); // Keep a copy since we may release.
Bob Wilson7248f862009-11-22 04:24:42 +00002039
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002040 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002041 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002042
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002043 // Ensure redefinition doesn't happen.
Craig Topper84138712014-11-29 05:31:10 +00002044 if (Records.getDef(CurRec->getNameInitAsString()))
2045 return Error(DefLoc, "def '" + CurRec->getNameInitAsString()+
2046 "' already defined");
Craig Toppercdab2322014-11-29 05:52:51 +00002047 Records.addDef(std::move(CurRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00002048
2049 if (ParseObjectBody(CurRec))
2050 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002051 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002052 // Parse the body before adding this prototype to the DefPrototypes vector.
2053 // That way implicit definitions will be added to the DefPrototypes vector
2054 // before this object, instantiated prior to defs derived from this object,
2055 // and this available for indirect name resolution when defs derived from
2056 // this object are instantiated.
Craig Topper84138712014-11-29 05:31:10 +00002057 if (ParseObjectBody(CurRec))
Hal Finkela8c1f462014-01-02 20:47:09 +00002058 return true;
2059
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002060 // Otherwise, a def inside a multiclass, add it to the multiclass.
2061 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene07e055f2011-10-19 13:03:51 +00002062 if (CurMultiClass->DefPrototypes[i]->getNameInit()
Craig Topper84138712014-11-29 05:31:10 +00002063 == CurRec->getNameInit())
2064 return Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
2065 "' already defined in this multiclass!");
2066 CurMultiClass->DefPrototypes.push_back(CurRecOwner.release());
Anton Yartsev671dff12014-08-08 00:29:54 +00002067 } else if (ParseObjectBody(CurRec)) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002068 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002069 }
Bob Wilson7248f862009-11-22 04:24:42 +00002070
Craig Topper011817a2014-04-09 04:50:04 +00002071 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002072 // See Record::setName(). This resolve step will see any new name
2073 // for the def that might have been created when resolving
2074 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002075 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002076
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002077 // If ObjectBody has template arguments, it's an error.
2078 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002079
2080 if (CurMultiClass) {
2081 // Copy the template arguments for the multiclass into the def.
David Greenedb10e692011-10-19 13:02:42 +00002082 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002083 CurMultiClass->Rec.getTemplateArgs();
2084
2085 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2086 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
2087 assert(RV && "Template arg doesn't exist?");
2088 CurRec->addValue(*RV);
2089 }
2090 }
2091
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002092 if (ProcessForeachDefs(CurRec, DefLoc)) {
Craig Topper84138712014-11-29 05:31:10 +00002093 return Error(DefLoc, "Could not process loops for def" +
2094 CurRec->getNameInitAsString());
David Greenefb927af2012-02-22 16:09:41 +00002095 }
2096
2097 return false;
2098}
2099
2100/// ParseForeach - Parse a for statement. Return the record corresponding
2101/// to it. This returns true on error.
2102///
2103/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2104/// Foreach ::= FOREACH Declaration IN Object
2105///
2106bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2107 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2108 Lex.Lex(); // Eat the 'for' token.
2109
2110 // Make a temporary object to record items associated with the for
2111 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002112 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002113 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002114 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002115 return TokError("expected declaration in for");
2116
2117 if (Lex.getCode() != tgtok::In)
2118 return TokError("Unknown tok");
2119 Lex.Lex(); // Eat the in
2120
2121 // Create a loop object and remember it.
2122 Loops.push_back(ForeachLoop(IterName, ListValue));
2123
2124 if (Lex.getCode() != tgtok::l_brace) {
2125 // FOREACH Declaration IN Object
2126 if (ParseObject(CurMultiClass))
2127 return true;
2128 }
2129 else {
2130 SMLoc BraceLoc = Lex.getLoc();
2131 // Otherwise, this is a group foreach.
2132 Lex.Lex(); // eat the '{'.
2133
2134 // Parse the object list.
2135 if (ParseObjectList(CurMultiClass))
2136 return true;
2137
2138 if (Lex.getCode() != tgtok::r_brace) {
2139 TokError("expected '}' at end of foreach command");
2140 return Error(BraceLoc, "to match this '{'");
2141 }
2142 Lex.Lex(); // Eat the }
2143 }
2144
2145 // We've processed everything in this loop.
2146 Loops.pop_back();
2147
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002148 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002149}
2150
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002151/// ParseClass - Parse a tblgen class definition.
2152///
2153/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2154///
2155bool TGParser::ParseClass() {
2156 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2157 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002158
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002159 if (Lex.getCode() != tgtok::Id)
2160 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002161
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002162 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2163 if (CurRec) {
2164 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002165 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002166 !CurRec->getSuperClasses().empty() ||
2167 !CurRec->getTemplateArgs().empty())
David Greene8eed9982011-10-19 13:03:58 +00002168 return TokError("Class '" + CurRec->getNameInitAsString()
2169 + "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002170 } else {
2171 // If this is the first reference to this class, create and add it.
Craig Toppercdab2322014-11-29 05:52:51 +00002172 auto NewRec = make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(),
2173 Records);
2174 CurRec = NewRec.get();
2175 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002176 }
2177 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002178
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002179 // If there are template args, parse them.
2180 if (Lex.getCode() == tgtok::less)
2181 if (ParseTemplateArgList(CurRec))
2182 return true;
2183
2184 // Finally, parse the object body.
2185 return ParseObjectBody(CurRec);
2186}
2187
2188/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2189/// of LetRecords.
2190///
2191/// LetList ::= LetItem (',' LetItem)*
2192/// LetItem ::= ID OptionalRangeList '=' Value
2193///
2194std::vector<LetRecord> TGParser::ParseLetList() {
2195 std::vector<LetRecord> Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002196
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002197 while (1) {
2198 if (Lex.getCode() != tgtok::Id) {
2199 TokError("expected identifier in let definition");
2200 return std::vector<LetRecord>();
2201 }
2202 std::string Name = Lex.getCurStrVal();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002203 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002204 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002205
2206 // Check for an optional RangeList.
2207 std::vector<unsigned> Bits;
Bob Wilson7248f862009-11-22 04:24:42 +00002208 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002209 return std::vector<LetRecord>();
2210 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002211
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002212 if (Lex.getCode() != tgtok::equal) {
2213 TokError("expected '=' in let expression");
2214 return std::vector<LetRecord>();
2215 }
2216 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002217
Craig Topper011817a2014-04-09 04:50:04 +00002218 Init *Val = ParseValue(nullptr);
2219 if (!Val) return std::vector<LetRecord>();
Bob Wilson7248f862009-11-22 04:24:42 +00002220
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002221 // Now that we have everything, add the record.
2222 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson7248f862009-11-22 04:24:42 +00002223
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002224 if (Lex.getCode() != tgtok::comma)
2225 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002226 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002227 }
2228}
2229
2230/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002231/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002232///
2233/// Object ::= LET LetList IN '{' ObjectList '}'
2234/// Object ::= LET LetList IN Object
2235///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002236bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002237 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2238 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002239
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002240 // Add this entry to the let stack.
2241 std::vector<LetRecord> LetInfo = ParseLetList();
2242 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002243 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002244
2245 if (Lex.getCode() != tgtok::In)
2246 return TokError("expected 'in' at end of top-level 'let'");
2247 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002248
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002249 // If this is a scalar let, just handle it now
2250 if (Lex.getCode() != tgtok::l_brace) {
2251 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002252 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002253 return true;
2254 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002255 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002256 // Otherwise, this is a group let.
2257 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002258
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002259 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002260 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002261 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002262
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002263 if (Lex.getCode() != tgtok::r_brace) {
2264 TokError("expected '}' at end of top level let command");
2265 return Error(BraceLoc, "to match this '{'");
2266 }
2267 Lex.Lex();
2268 }
Bob Wilson7248f862009-11-22 04:24:42 +00002269
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002270 // Outside this let scope, this let block is not active.
2271 LetStack.pop_back();
2272 return false;
2273}
2274
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002275/// ParseMultiClass - Parse a multiclass definition.
2276///
Bob Wilson3d948162009-04-28 19:41:44 +00002277/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002278/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2279/// MultiClassObject ::= DefInst
2280/// MultiClassObject ::= MultiClassInst
2281/// MultiClassObject ::= DefMInst
2282/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2283/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002284///
2285bool TGParser::ParseMultiClass() {
2286 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2287 Lex.Lex(); // Eat the multiclass token.
2288
2289 if (Lex.getCode() != tgtok::Id)
2290 return TokError("expected identifier after multiclass for name");
2291 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002292
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002293 if (MultiClasses.count(Name))
2294 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002295
Chris Lattner77d369c2010-12-13 00:23:57 +00002296 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
2297 Lex.getLoc(), Records);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002298 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002299
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002300 // If there are template args, parse them.
2301 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002302 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002303 return true;
2304
David Greene7049e792009-04-24 16:55:41 +00002305 bool inherits = false;
2306
David Greene753ed8f2009-04-22 16:42:54 +00002307 // If there are submulticlasses, parse them.
2308 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002309 inherits = true;
2310
David Greene753ed8f2009-04-22 16:42:54 +00002311 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002312
David Greene753ed8f2009-04-22 16:42:54 +00002313 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002314 SubMultiClassReference SubMultiClass =
2315 ParseSubMultiClassReference(CurMultiClass);
David Greene753ed8f2009-04-22 16:42:54 +00002316 while (1) {
2317 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002318 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002319
David Greene753ed8f2009-04-22 16:42:54 +00002320 // Add it.
2321 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2322 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002323
David Greene753ed8f2009-04-22 16:42:54 +00002324 if (Lex.getCode() != tgtok::comma) break;
2325 Lex.Lex(); // eat ','.
2326 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2327 }
2328 }
2329
David Greene7049e792009-04-24 16:55:41 +00002330 if (Lex.getCode() != tgtok::l_brace) {
2331 if (!inherits)
2332 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002333 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002334 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002335 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002336 } else {
David Greene7049e792009-04-24 16:55:41 +00002337 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2338 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002339
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002340 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002341 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002342 default:
2343 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
2344 case tgtok::Let:
2345 case tgtok::Def:
2346 case tgtok::Defm:
2347 case tgtok::Foreach:
2348 if (ParseObject(CurMultiClass))
2349 return true;
2350 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002351 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002352 }
David Greene7049e792009-04-24 16:55:41 +00002353 Lex.Lex(); // eat the '}'.
2354 }
Bob Wilson7248f862009-11-22 04:24:42 +00002355
Craig Topper011817a2014-04-09 04:50:04 +00002356 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002357 return false;
2358}
2359
David Greenedb445972011-10-05 22:42:07 +00002360Record *TGParser::
2361InstantiateMulticlassDef(MultiClass &MC,
2362 Record *DefProto,
Hal Finkelf2a0b2b2014-01-02 19:35:33 +00002363 Init *&DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002364 SMRange DefmPrefixRange) {
David Greene5d5d88c2011-10-19 13:04:31 +00002365 // We need to preserve DefProto so it can be reused for later
2366 // instantiations, so create a new Record to inherit from it.
2367
David Greenedb445972011-10-05 22:42:07 +00002368 // Add in the defm name. If the defm prefix is empty, give each
2369 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2370 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2371 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002372
Jordan Roseabdd99b2013-01-10 18:50:05 +00002373 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002374 if (!DefmPrefix) {
David Greene5d5d88c2011-10-19 13:04:31 +00002375 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002376 IsAnonymous = true;
2377 }
David Greene5d5d88c2011-10-19 13:04:31 +00002378
2379 Init *DefName = DefProto->getNameInit();
2380
Sean Silvafb509ed2012-10-10 20:24:43 +00002381 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002382
Craig Topper011817a2014-04-09 04:50:04 +00002383 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002384 // We have a fully expanded string so there are no operators to
2385 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002386 DefName =
2387 BinOpInit::get(BinOpInit::STRCONCAT,
2388 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2389 StringRecTy::get())->Fold(DefProto, &MC),
2390 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2391 }
David Greene5d5d88c2011-10-19 13:04:31 +00002392
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002393 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002394 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002395 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Craig Topper84138712014-11-29 05:31:10 +00002396 auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002397
2398 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002399 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002400 Ref.Rec = DefProto;
Craig Topper84138712014-11-29 05:31:10 +00002401 AddSubClass(CurRec.get(), Ref);
David Greenedb445972011-10-05 22:42:07 +00002402
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002403 // Set the value for NAME. We don't resolve references to it 'til later,
2404 // though, so that uses in nested multiclass names don't get
2405 // confused.
Craig Topper84138712014-11-29 05:31:10 +00002406 if (SetValue(CurRec.get(), Ref.RefRange.Start, "NAME",
2407 std::vector<unsigned>(), DefmPrefix)) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002408 Error(DefmPrefixRange.Start, "Could not resolve "
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002409 + CurRec->getNameInitAsString() + ":NAME to '"
2410 + DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002411 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002412 }
David Greene8bf0d722011-10-19 13:04:35 +00002413
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002414 // If the DefNameString didn't resolve, we probably have a reference to
2415 // NAME and need to replace it. We need to do at least this much greedily,
2416 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002417 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002418 RecordVal *DefNameRV = CurRec->getValue("NAME");
2419 CurRec->resolveReferencesTo(DefNameRV);
2420 }
2421
2422 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002423 // Now that we're at the top level, resolve all NAME references
2424 // in the resultant defs that weren't in the def names themselves.
2425 RecordVal *DefNameRV = CurRec->getValue("NAME");
2426 CurRec->resolveReferencesTo(DefNameRV);
2427
2428 // Now that NAME references are resolved and we're at the top level of
2429 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002430 // currently in a multiclass, it means this defm appears inside a
2431 // multiclass and its name won't be fully resolvable until we see
2432 // the top-level defm. Therefore, we don't add this to the
2433 // RecordKeeper at this point. If we did we could get duplicate
2434 // defs as more than one probably refers to NAME or some other
2435 // common internal placeholder.
2436
2437 // Ensure redefinition doesn't happen.
2438 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002439 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002440 "' already defined, instantiating defm with subdef '" +
2441 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002442 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002443 }
2444
Craig Topper84138712014-11-29 05:31:10 +00002445 Record *CurRecSave = CurRec.get(); // Keep a copy before we release.
Craig Toppercdab2322014-11-29 05:52:51 +00002446 Records.addDef(std::move(CurRec));
Craig Topper84138712014-11-29 05:31:10 +00002447 return CurRecSave;
David Greene8bf0d722011-10-19 13:04:35 +00002448 }
2449
Craig Topper84138712014-11-29 05:31:10 +00002450 // FIXME This is bad but the ownership transfer to caller is pretty messy.
2451 // The unique_ptr in this function at least protects the exits above.
2452 return CurRec.release();
David Greenedb445972011-10-05 22:42:07 +00002453}
2454
2455bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2456 Record *CurRec,
2457 SMLoc DefmPrefixLoc,
2458 SMLoc SubClassLoc,
David Greenedb10e692011-10-19 13:02:42 +00002459 const std::vector<Init *> &TArgs,
David Greenedb445972011-10-05 22:42:07 +00002460 std::vector<Init *> &TemplateVals,
2461 bool DeleteArgs) {
2462 // Loop over all of the template arguments, setting them to the specified
2463 // value or leaving them as the default if necessary.
2464 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2465 // Check if a value is specified for this temp-arg.
2466 if (i < TemplateVals.size()) {
2467 // Set it now.
2468 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2469 TemplateVals[i]))
2470 return true;
2471
2472 // Resolve it next.
2473 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2474
2475 if (DeleteArgs)
2476 // Now remove it.
2477 CurRec->removeValue(TArgs[i]);
2478
2479 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2480 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenedb10e692011-10-19 13:02:42 +00002481 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2482 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2483 + "'");
David Greenedb445972011-10-05 22:42:07 +00002484 }
2485 }
2486 return false;
2487}
2488
2489bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2490 Record *CurRec,
2491 Record *DefProto,
2492 SMLoc DefmPrefixLoc) {
2493 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002494 if (ApplyLetStack(CurRec))
2495 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002496
David Greenedb445972011-10-05 22:42:07 +00002497 // Don't create a top level definition for defm inside multiclasses,
2498 // instead, only update the prototypes and bind the template args
2499 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002500 if (!CurMultiClass)
2501 return false;
2502 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2503 i != e; ++i)
2504 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2505 == CurRec->getNameInit())
2506 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2507 "' already defined in this multiclass!");
2508 CurMultiClass->DefPrototypes.push_back(CurRec);
David Greenedb445972011-10-05 22:42:07 +00002509
Sean Silvacc951b22013-01-09 05:28:12 +00002510 // Copy the template arguments for the multiclass into the new def.
2511 const std::vector<Init *> &TA =
2512 CurMultiClass->Rec.getTemplateArgs();
David Greenedb445972011-10-05 22:42:07 +00002513
Sean Silvacc951b22013-01-09 05:28:12 +00002514 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2515 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2516 assert(RV && "Template arg doesn't exist?");
2517 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002518 }
2519
2520 return false;
2521}
2522
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002523/// ParseDefm - Parse the instantiation of a multiclass.
2524///
2525/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2526///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002527bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002528 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002529 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002530 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002531
Craig Topperb21afc62013-01-07 05:09:33 +00002532 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002533 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002534 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002535
Jordan Rosef12e8a92013-01-10 18:50:11 +00002536 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002537 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002538 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002539
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002540 // Keep track of the new generated record definitions.
2541 std::vector<Record*> NewRecDefs;
2542
2543 // This record also inherits from a regular class (non-multiclass)?
2544 bool InheritFromClass = false;
2545
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002546 // eat the colon.
2547 Lex.Lex();
2548
Chris Lattner526c8cb2009-06-21 03:39:35 +00002549 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002550 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002551
2552 while (1) {
Craig Topper011817a2014-04-09 04:50:04 +00002553 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002554
2555 // To instantiate a multiclass, we need to first get the multiclass, then
2556 // instantiate each def contained in the multiclass with the SubClassRef
2557 // template parameters.
2558 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2559 assert(MC && "Didn't lookup multiclass correctly?");
David Greeneaf8ee2c2011-07-29 22:43:06 +00002560 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002561
2562 // Verify that the correct number of template arguments were specified.
David Greenedb10e692011-10-19 13:02:42 +00002563 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002564 if (TArgs.size() < TemplateVals.size())
2565 return Error(SubClassLoc,
2566 "more template args specified than multiclass expects");
2567
2568 // Loop over all the def's in the multiclass, instantiating each one.
2569 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2570 Record *DefProto = MC->DefPrototypes[i];
2571
Jordan Rosef12e8a92013-01-10 18:50:11 +00002572 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix,
2573 SMRange(DefmLoc,
2574 DefmPrefixEndLoc));
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002575 if (!CurRec)
2576 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002577
Jordan Rosef12e8a92013-01-10 18:50:11 +00002578 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002579 TArgs, TemplateVals, true/*Delete args*/))
2580 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002581
Jordan Rosef12e8a92013-01-10 18:50:11 +00002582 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002583 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002584
Adam Nemete5a07162014-09-16 17:14:13 +00002585 // Defs that can be used by other definitions should be fully resolved
2586 // before any use.
2587 if (DefProto->isResolveFirst() && !CurMultiClass) {
2588 CurRec->resolveReferences();
2589 CurRec->setResolveFirst(false);
2590 }
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002591 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002592 }
2593
David Greenedb445972011-10-05 22:42:07 +00002594
David Greenef00919a2009-04-22 22:17:51 +00002595 if (Lex.getCode() != tgtok::comma) break;
2596 Lex.Lex(); // eat ','.
2597
Craig Topper998a39a2013-08-20 04:22:09 +00002598 if (Lex.getCode() != tgtok::Id)
2599 return TokError("expected identifier");
2600
David Greenef00919a2009-04-22 22:17:51 +00002601 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002602
2603 // A defm can inherit from regular classes (non-multiclass) as
2604 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002605 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002606
2607 if (InheritFromClass)
2608 break;
2609
Craig Topper011817a2014-04-09 04:50:04 +00002610 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002611 }
2612
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002613 if (InheritFromClass) {
2614 // Process all the classes to inherit as if they were part of a
2615 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002616 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002617 while (1) {
2618 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002619 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002620
2621 // Get the expanded definition prototypes and teach them about
2622 // the record values the current class to inherit has
2623 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2624 Record *CurRec = NewRecDefs[i];
2625
2626 // Add it.
2627 if (AddSubClass(CurRec, SubClass))
2628 return true;
2629
Sean Silvacb1a75e2013-01-09 04:49:14 +00002630 if (ApplyLetStack(CurRec))
2631 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002632 }
2633
2634 if (Lex.getCode() != tgtok::comma) break;
2635 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002636 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002637 }
2638 }
2639
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002640 if (!CurMultiClass)
2641 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene50c09122011-08-10 18:27:46 +00002642 // See Record::setName(). This resolve step will see any new
2643 // name for the def that might have been created when resolving
2644 // inheritance, values and arguments above.
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002645 NewRecDefs[i]->resolveReferences();
2646
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002647 if (Lex.getCode() != tgtok::semi)
2648 return TokError("expected ';' at end of defm");
2649 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002650
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002651 return false;
2652}
2653
2654/// ParseObject
2655/// Object ::= ClassInst
2656/// Object ::= DefInst
2657/// Object ::= MultiClassInst
2658/// Object ::= DefMInst
2659/// Object ::= LETCommand '{' ObjectList '}'
2660/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002661bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002662 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002663 default:
2664 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002665 case tgtok::Let: return ParseTopLevelLet(MC);
2666 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002667 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002668 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002669 case tgtok::Class: return ParseClass();
2670 case tgtok::MultiClass: return ParseMultiClass();
2671 }
2672}
2673
2674/// ParseObjectList
2675/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002676bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002677 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002678 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002679 return true;
2680 }
2681 return false;
2682}
2683
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002684bool TGParser::ParseFile() {
2685 Lex.Lex(); // Prime the lexer.
2686 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002687
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002688 // If we have unread input at the end of the file, report it.
2689 if (Lex.getCode() == tgtok::Eof)
2690 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002691
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002692 return TokError("Unexpected input at top level");
2693}
2694