blob: 54932487e8da9edf1fc4a1814f6db2ca4cdbefbe [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
235 Record *NewDef = new Record(**i);
236
237 // Add all of the values in the superclass into the current def.
238 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
Jordan Rosef12e8a92013-01-10 18:50:11 +0000239 if (AddValue(NewDef, SubMultiClass.RefRange.Start, MCVals[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000240 return true;
241
Bob Wilsonf71e6562009-04-30 18:26:19 +0000242 CurMC->DefPrototypes.push_back(NewDef);
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.
343 Record *IterRec = new Record(*CurRec);
344
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 Topper011817a2014-04-09 04:50:04 +0000349 if (!IVal) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000350 Error(Loc, "foreach iterator value is untyped");
351 return true;
352 }
353
354 IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false));
355
356 if (SetValue(IterRec, Loc, IterVar->getName(),
357 std::vector<unsigned>(), IVal)) {
358 Error(Loc, "when instantiating this def");
359 return true;
360 }
361
362 // Resolve it next.
363 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName()));
364
365 // Remove it.
366 IterRec->removeValue(IterVar->getName());
367 }
368
369 if (Records.getDef(IterRec->getNameInitAsString())) {
Artyom Skrobov8b985322014-06-10 12:41:14 +0000370 // If this record is anonymous, it's no problem, just generate a new name
371 if (IterRec->isAnonymous())
372 IterRec->setName(GetNewAnonymousName());
373 else {
374 Error(Loc, "def already exists: " + IterRec->getNameInitAsString());
375 return true;
376 }
David Greenefb927af2012-02-22 16:09:41 +0000377 }
378
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000379 Records.addDef(IterRec);
380 IterRec->resolveReferences();
David Greenefb927af2012-02-22 16:09:41 +0000381 return false;
382}
383
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000384//===----------------------------------------------------------------------===//
385// Parser Code
386//===----------------------------------------------------------------------===//
387
388/// isObjectStart - Return true if this is a valid first token for an Object.
389static bool isObjectStart(tgtok::TokKind K) {
390 return K == tgtok::Class || K == tgtok::Def ||
David Greenefb927af2012-02-22 16:09:41 +0000391 K == tgtok::Defm || K == tgtok::Let ||
392 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000393}
394
Alp Tokerce91fe52013-12-21 18:51:00 +0000395/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
396/// an identifier.
397std::string TGParser::GetNewAnonymousName() {
Michael J. Spencer2bb7db92013-02-26 21:29:47 +0000398 unsigned Tmp = AnonCounter++; // MSVC2012 ICEs without this.
Alp Tokerce91fe52013-12-21 18:51:00 +0000399 return "anonymous_" + utostr(Tmp);
Chris Lattner7538ed82010-10-05 22:51:56 +0000400}
401
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000402/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000403/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000404/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000405/// ObjectName ::= /*empty*/
406///
David Greene2affd672011-10-19 13:04:29 +0000407Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
408 switch (Lex.getCode()) {
409 case tgtok::colon:
410 case tgtok::semi:
411 case tgtok::l_brace:
412 // These are all of the tokens that can begin an object body.
413 // Some of these can also begin values but we disallow those cases
414 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000415 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000416 default:
417 break;
418 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000419
Craig Topper011817a2014-04-09 04:50:04 +0000420 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000421 if (CurMultiClass)
422 CurRec = &CurMultiClass->Rec;
423
Craig Topper011817a2014-04-09 04:50:04 +0000424 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000425 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000426 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000427 if (!CurRecName) {
428 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000429 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000430 }
431 Type = CurRecName->getType();
432 }
433
434 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000435}
436
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000437/// ParseClassID - Parse and resolve a reference to a class name. This returns
438/// null on error.
439///
440/// ClassID ::= ID
441///
442Record *TGParser::ParseClassID() {
443 if (Lex.getCode() != tgtok::Id) {
444 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000445 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000446 }
Bob Wilson7248f862009-11-22 04:24:42 +0000447
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000448 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000449 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000450 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000451
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000452 Lex.Lex();
453 return Result;
454}
455
Bob Wilson3d948162009-04-28 19:41:44 +0000456/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
457/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000458///
459/// MultiClassID ::= ID
460///
461MultiClass *TGParser::ParseMultiClassID() {
462 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000463 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000464 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000465 }
Bob Wilson3d948162009-04-28 19:41:44 +0000466
David Greene753ed8f2009-04-22 16:42:54 +0000467 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
Craig Topper011817a2014-04-09 04:50:04 +0000468 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000469 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000470
David Greene753ed8f2009-04-22 16:42:54 +0000471 Lex.Lex();
472 return Result;
473}
474
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000475/// ParseSubClassReference - Parse a reference to a subclass or to a templated
476/// subclass. This returns a SubClassRefTy with a null Record* on error.
477///
478/// SubClassRef ::= ClassID
479/// SubClassRef ::= ClassID '<' ValueList '>'
480///
481SubClassReference TGParser::
482ParseSubClassReference(Record *CurRec, bool isDefm) {
483 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000484 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000485
Sean Silva0657b402013-01-09 02:17:14 +0000486 if (isDefm) {
487 if (MultiClass *MC = ParseMultiClassID())
488 Result.Rec = &MC->Rec;
489 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000490 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000491 }
Craig Topper011817a2014-04-09 04:50:04 +0000492 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000493
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000494 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000495 if (Lex.getCode() != tgtok::less) {
496 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000497 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000498 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000499 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000500
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000501 if (Lex.getCode() == tgtok::greater) {
502 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000503 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000504 return Result;
505 }
Bob Wilson7248f862009-11-22 04:24:42 +0000506
David Greene8618f952009-06-08 20:23:18 +0000507 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000508 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000509 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000510 return Result;
511 }
Bob Wilson7248f862009-11-22 04:24:42 +0000512
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000513 if (Lex.getCode() != tgtok::greater) {
514 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000515 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000516 return Result;
517 }
518 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000519 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000520
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000521 return Result;
522}
523
Bob Wilson3d948162009-04-28 19:41:44 +0000524/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
525/// templated submulticlass. This returns a SubMultiClassRefTy with a null
526/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000527///
528/// SubMultiClassRef ::= MultiClassID
529/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
530///
531SubMultiClassReference TGParser::
532ParseSubMultiClassReference(MultiClass *CurMC) {
533 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000534 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000535
David Greene753ed8f2009-04-22 16:42:54 +0000536 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000537 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000538
David Greene753ed8f2009-04-22 16:42:54 +0000539 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000540 if (Lex.getCode() != tgtok::less) {
541 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000542 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000543 }
David Greene753ed8f2009-04-22 16:42:54 +0000544 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000545
David Greene753ed8f2009-04-22 16:42:54 +0000546 if (Lex.getCode() == tgtok::greater) {
547 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000548 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000549 return Result;
550 }
Bob Wilson3d948162009-04-28 19:41:44 +0000551
David Greene8618f952009-06-08 20:23:18 +0000552 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000553 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000554 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000555 return Result;
556 }
Bob Wilson3d948162009-04-28 19:41:44 +0000557
David Greene753ed8f2009-04-22 16:42:54 +0000558 if (Lex.getCode() != tgtok::greater) {
559 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000560 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000561 return Result;
562 }
563 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000564 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000565
566 return Result;
567}
568
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000569/// ParseRangePiece - Parse a bit/value range.
570/// RangePiece ::= INTVAL
571/// RangePiece ::= INTVAL '-' INTVAL
572/// RangePiece ::= INTVAL INTVAL
573bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000574 if (Lex.getCode() != tgtok::IntVal) {
575 TokError("expected integer or bitrange");
576 return true;
577 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000578 int64_t Start = Lex.getCurIntVal();
579 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000580
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000581 if (Start < 0)
582 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000583
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000584 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000585 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000586 Ranges.push_back(Start);
587 return false;
588 case tgtok::minus:
589 if (Lex.Lex() != tgtok::IntVal) {
590 TokError("expected integer value as end of range");
591 return true;
592 }
593 End = Lex.getCurIntVal();
594 break;
595 case tgtok::IntVal:
596 End = -Lex.getCurIntVal();
597 break;
598 }
Bob Wilson7248f862009-11-22 04:24:42 +0000599 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000600 return TokError("invalid range, cannot be negative");
601 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000602
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000603 // Add to the range.
604 if (Start < End) {
605 for (; Start <= End; ++Start)
606 Ranges.push_back(Start);
607 } else {
608 for (; Start >= End; --Start)
609 Ranges.push_back(Start);
610 }
611 return false;
612}
613
614/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
615///
616/// RangeList ::= RangePiece (',' RangePiece)*
617///
618std::vector<unsigned> TGParser::ParseRangeList() {
619 std::vector<unsigned> Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000620
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000621 // Parse the first piece.
622 if (ParseRangePiece(Result))
623 return std::vector<unsigned>();
624 while (Lex.getCode() == tgtok::comma) {
625 Lex.Lex(); // Eat the comma.
626
627 // Parse the next range piece.
628 if (ParseRangePiece(Result))
629 return std::vector<unsigned>();
630 }
631 return Result;
632}
633
634/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
635/// OptionalRangeList ::= '<' RangeList '>'
636/// OptionalRangeList ::= /*empty*/
637bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
638 if (Lex.getCode() != tgtok::less)
639 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000640
Chris Lattner526c8cb2009-06-21 03:39:35 +0000641 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000642 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000643
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000644 // Parse the range list.
645 Ranges = ParseRangeList();
646 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000647
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000648 if (Lex.getCode() != tgtok::greater) {
649 TokError("expected '>' at end of range list");
650 return Error(StartLoc, "to match this '<'");
651 }
652 Lex.Lex(); // eat the '>'.
653 return false;
654}
655
656/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
657/// OptionalBitList ::= '{' RangeList '}'
658/// OptionalBitList ::= /*empty*/
659bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
660 if (Lex.getCode() != tgtok::l_brace)
661 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000662
Chris Lattner526c8cb2009-06-21 03:39:35 +0000663 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000664 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000665
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000666 // Parse the range list.
667 Ranges = ParseRangeList();
668 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000669
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000670 if (Lex.getCode() != tgtok::r_brace) {
671 TokError("expected '}' at end of bit list");
672 return Error(StartLoc, "to match this '{'");
673 }
674 Lex.Lex(); // eat the '}'.
675 return false;
676}
677
678
679/// ParseType - Parse and return a tblgen type. This returns null on error.
680///
681/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000682/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000683/// Type ::= BIT // bit type
684/// Type ::= BITS '<' INTVAL '>' // bits<x> type
685/// Type ::= INT // int type
686/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000687/// Type ::= DAG // dag type
688/// Type ::= ClassID // Record Type
689///
690RecTy *TGParser::ParseType() {
691 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000692 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000693 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000694 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000695 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
696 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000697 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000698 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000699 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000700 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000701 case tgtok::Bits: {
702 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
703 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000704 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000705 }
706 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
707 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000708 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000709 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000710 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000711 if (Lex.Lex() != tgtok::greater) { // Eat count.
712 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000713 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000714 }
715 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000716 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000717 }
718 case tgtok::List: {
719 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
720 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000721 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000722 }
723 Lex.Lex(); // Eat '<'
724 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000725 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000726
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000727 if (Lex.getCode() != tgtok::greater) {
728 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000729 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000730 }
731 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000732 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000733 }
Bob Wilson7248f862009-11-22 04:24:42 +0000734 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000735}
736
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000737/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
738/// has already been read.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000739Init *TGParser::ParseIDValue(Record *CurRec,
David Greened4263a62011-10-19 13:04:20 +0000740 const std::string &Name, SMLoc NameLoc,
741 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000742 if (CurRec) {
743 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000744 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000745
David Greenedb10e692011-10-19 13:02:42 +0000746 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
747
David Greene47a665e2011-10-05 22:42:54 +0000748 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +0000749 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
750 "::");
David Greene47a665e2011-10-05 22:42:54 +0000751
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000752 if (CurRec->isTemplateArg(TemplateArgName)) {
753 const RecordVal *RV = CurRec->getValue(TemplateArgName);
754 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000755 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000756 }
757 }
Bob Wilson7248f862009-11-22 04:24:42 +0000758
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000759 if (CurMultiClass) {
David Greenedb10e692011-10-19 13:02:42 +0000760 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
761 "::");
762
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000763 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
764 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
765 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000766 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000767 }
768 }
Bob Wilson7248f862009-11-22 04:24:42 +0000769
David Greenefb927af2012-02-22 16:09:41 +0000770 // If this is in a foreach loop, make sure it's not a loop iterator
771 for (LoopVector::iterator i = Loops.begin(), iend = Loops.end();
772 i != iend;
773 ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000774 VarInit *IterVar = dyn_cast<VarInit>(i->IterVar);
David Greenefb927af2012-02-22 16:09:41 +0000775 if (IterVar && IterVar->getName() == Name)
776 return IterVar;
777 }
778
David Greene232bd602011-10-19 13:04:21 +0000779 if (Mode == ParseNameMode)
780 return StringInit::get(Name);
781
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000782 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000783 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000784
David Greene232bd602011-10-19 13:04:21 +0000785 if (Mode == ParseValueMode) {
786 Error(NameLoc, "Variable not defined: '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000787 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000788 }
789
790 return StringInit::get(Name);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000791}
792
David Greene5d0c0512009-05-14 20:54:48 +0000793/// ParseOperation - Parse an operator. This returns null on error.
794///
795/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
796///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000797Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000798 switch (Lex.getCode()) {
799 default:
800 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000801 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000802 case tgtok::XHead:
803 case tgtok::XTail:
804 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000805 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
806 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000807 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000808
David Greenee8f3b272009-05-14 21:22:49 +0000809 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000810 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000811 case tgtok::XCast:
812 Lex.Lex(); // eat the operation
813 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000814
David Greenee8f3b272009-05-14 21:22:49 +0000815 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000816
Craig Topper011817a2014-04-09 04:50:04 +0000817 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000818 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000819 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000820 }
David Greene5d0c0512009-05-14 20:54:48 +0000821
David Greenee8f3b272009-05-14 21:22:49 +0000822 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000823 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000824 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000825 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000826 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000827 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000828 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000829 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000830 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000831 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000832 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000833 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000834 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000835 break;
David Greenee8f3b272009-05-14 21:22:49 +0000836 }
837 if (Lex.getCode() != tgtok::l_paren) {
838 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000839 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000840 }
841 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000842
David Greeneaf8ee2c2011-07-29 22:43:06 +0000843 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000844 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000845
David Greene2f7cf7f2011-01-07 17:05:37 +0000846 if (Code == UnOpInit::HEAD
847 || Code == UnOpInit::TAIL
848 || Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000849 ListInit *LHSl = dyn_cast<ListInit>(LHS);
850 StringInit *LHSs = dyn_cast<StringInit>(LHS);
851 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000852 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000853 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000854 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000855 }
856 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000857 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
858 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000859 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000860 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000861 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000862 }
863 }
864
David Greene2f7cf7f2011-01-07 17:05:37 +0000865 if (Code == UnOpInit::HEAD
866 || Code == UnOpInit::TAIL) {
Craig Topper011817a2014-04-09 04:50:04 +0000867 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000868 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000869 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000870 }
Bob Wilson7248f862009-11-22 04:24:42 +0000871
David Greened571b3c2009-05-14 22:38:31 +0000872 if (LHSl && LHSl->getSize() == 0) {
873 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000874 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000875 }
876 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000877 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000878 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000879 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000880 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000881 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000882 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000883 if (Code == UnOpInit::HEAD) {
David Greened571b3c2009-05-14 22:38:31 +0000884 Type = Itemt->getType();
Bob Wilson7248f862009-11-22 04:24:42 +0000885 } else {
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000886 Type = ListRecTy::get(Itemt->getType());
David Greened571b3c2009-05-14 22:38:31 +0000887 }
Bob Wilson7248f862009-11-22 04:24:42 +0000888 } else {
David Greened571b3c2009-05-14 22:38:31 +0000889 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000890 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000891 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000892 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000893 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000894 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000895 if (Code == UnOpInit::HEAD) {
David Greened571b3c2009-05-14 22:38:31 +0000896 Type = LType->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +0000897 } else {
David Greened571b3c2009-05-14 22:38:31 +0000898 Type = LType;
899 }
900 }
901 }
902 }
903
David Greenee8f3b272009-05-14 21:22:49 +0000904 if (Lex.getCode() != tgtok::r_paren) {
905 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000906 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000907 }
908 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000909 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000910 }
David Greene5d0c0512009-05-14 20:54:48 +0000911
912 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000913 case tgtok::XADD:
Bob Wilson7248f862009-11-22 04:24:42 +0000914 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000915 case tgtok::XSRL:
916 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000917 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000918 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000919 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000920 tgtok::TokKind OpTok = Lex.getCode();
921 SMLoc OpLoc = Lex.getLoc();
922 Lex.Lex(); // eat the operation
923
David Greene5d0c0512009-05-14 20:54:48 +0000924 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000925 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000926
Chris Lattner61ea00b2010-10-05 23:58:18 +0000927 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000928 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000929 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000930 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000931 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
932 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
933 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
934 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000935 case tgtok::XListConcat:
936 Code = BinOpInit::LISTCONCAT;
937 // We don't know the list type until we parse the first argument
938 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000939 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000940 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000941 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000942 break;
David Greene5d0c0512009-05-14 20:54:48 +0000943 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000944
David Greene5d0c0512009-05-14 20:54:48 +0000945 if (Lex.getCode() != tgtok::l_paren) {
946 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000947 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000948 }
949 Lex.Lex(); // eat the '('
950
David Greeneaf8ee2c2011-07-29 22:43:06 +0000951 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000952
Chris Lattner61ea00b2010-10-05 23:58:18 +0000953 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000954 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000955
Chris Lattner61ea00b2010-10-05 23:58:18 +0000956 while (Lex.getCode() == tgtok::comma) {
957 Lex.Lex(); // eat the ','
958
959 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000960 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000961 }
David Greene5d0c0512009-05-14 20:54:48 +0000962
963 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000964 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000965 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000966 }
967 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000968
Daniel Sanders314e80e2014-05-07 10:13:19 +0000969 // If we are doing !listconcat, we should know the type by now
970 if (OpTok == tgtok::XListConcat) {
971 if (VarInit *Arg0 = dyn_cast<VarInit>(InitList[0]))
972 Type = Arg0->getType();
973 else if (ListInit *Arg0 = dyn_cast<ListInit>(InitList[0]))
974 Type = Arg0->getType();
975 else {
976 InitList[0]->dump();
977 Error(OpLoc, "expected a list");
978 return nullptr;
979 }
980 }
981
Chris Lattner61ea00b2010-10-05 23:58:18 +0000982 // We allow multiple operands to associative operators like !strconcat as
983 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000984 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000985 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000986 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000987 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
988 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000989 InitList.back() = RHS;
990 }
991 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000992
Chris Lattner61ea00b2010-10-05 23:58:18 +0000993 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000994 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000995 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000996
Chris Lattner61ea00b2010-10-05 23:58:18 +0000997 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +0000998 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000999 }
1000
David Greene3587eed2009-05-14 23:26:46 +00001001 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001002 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001003 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
1004 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +00001005 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001006
David Greene98ed3c72009-05-14 21:54:42 +00001007 tgtok::TokKind LexCode = Lex.getCode();
1008 Lex.Lex(); // eat the operation
1009 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001010 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001011 case tgtok::XIf:
1012 Code = TernOpInit::IF;
1013 break;
David Greenee917fff2009-05-14 22:23:47 +00001014 case tgtok::XForEach:
1015 Code = TernOpInit::FOREACH;
1016 break;
David Greene98ed3c72009-05-14 21:54:42 +00001017 case tgtok::XSubst:
1018 Code = TernOpInit::SUBST;
1019 break;
1020 }
1021 if (Lex.getCode() != tgtok::l_paren) {
1022 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001023 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001024 }
1025 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001026
David Greeneaf8ee2c2011-07-29 22:43:06 +00001027 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001028 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001029
David Greene98ed3c72009-05-14 21:54:42 +00001030 if (Lex.getCode() != tgtok::comma) {
1031 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001032 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001033 }
1034 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001035
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001036 Init *MHS = ParseValue(CurRec, ItemType);
1037 if (!MHS)
1038 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001039
David Greene98ed3c72009-05-14 21:54:42 +00001040 if (Lex.getCode() != tgtok::comma) {
1041 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001042 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001043 }
1044 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001045
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001046 Init *RHS = ParseValue(CurRec, ItemType);
1047 if (!RHS)
1048 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001049
David Greene98ed3c72009-05-14 21:54:42 +00001050 if (Lex.getCode() != tgtok::r_paren) {
1051 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001052 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001053 }
1054 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001055
David Greene98ed3c72009-05-14 21:54:42 +00001056 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001057 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001058 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001059 RecTy *MHSTy = nullptr;
1060 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001061
Sean Silvafb509ed2012-10-10 20:24:43 +00001062 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001063 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001064 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001065 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001066 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001067 MHSTy = BitRecTy::get();
1068
Sean Silvafb509ed2012-10-10 20:24:43 +00001069 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001070 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001071 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001072 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001073 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001074 RHSTy = BitRecTy::get();
1075
1076 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001077 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001078 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001079 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001080 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001081
1082 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001083 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001084 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001085 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001086
1087 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1088 Type = RHSTy;
1089 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1090 Type = MHSTy;
Bob Wilson7248f862009-11-22 04:24:42 +00001091 } else {
David Greene3587eed2009-05-14 23:26:46 +00001092 TokError("inconsistent types for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001093 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001094 }
1095 break;
1096 }
David Greenee917fff2009-05-14 22:23:47 +00001097 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001098 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
Craig Topper011817a2014-04-09 04:50:04 +00001099 if (!MHSt) {
David Greenee917fff2009-05-14 22:23:47 +00001100 TokError("could not get type for !foreach");
Craig Topper011817a2014-04-09 04:50:04 +00001101 return nullptr;
David Greenee917fff2009-05-14 22:23:47 +00001102 }
1103 Type = MHSt->getType();
1104 break;
1105 }
David Greene98ed3c72009-05-14 21:54:42 +00001106 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001107 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001108 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001109 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001110 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001111 }
1112 Type = RHSt->getType();
1113 break;
1114 }
1115 }
David Greenee32ebf22011-07-29 19:07:07 +00001116 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001117 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001118 }
David Greene5d0c0512009-05-14 20:54:48 +00001119 }
David Greene5d0c0512009-05-14 20:54:48 +00001120}
1121
1122/// ParseOperatorType - Parse a type for an operator. This returns
1123/// null on error.
1124///
1125/// OperatorType ::= '<' Type '>'
1126///
Dan Gohman1432ef82009-08-12 22:10:57 +00001127RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001128 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001129
1130 if (Lex.getCode() != tgtok::less) {
1131 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001132 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001133 }
1134 Lex.Lex(); // eat the <
1135
1136 Type = ParseType();
1137
Craig Topper011817a2014-04-09 04:50:04 +00001138 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001139 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001140 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001141 }
1142
1143 if (Lex.getCode() != tgtok::greater) {
1144 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001145 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001146 }
1147 Lex.Lex(); // eat the >
1148
1149 return Type;
1150}
1151
1152
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001153/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1154///
1155/// SimpleValue ::= IDValue
1156/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001157/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001158/// SimpleValue ::= CODEFRAGMENT
1159/// SimpleValue ::= '?'
1160/// SimpleValue ::= '{' ValueList '}'
1161/// SimpleValue ::= ID '<' ValueListNE '>'
1162/// SimpleValue ::= '[' ValueList ']'
1163/// SimpleValue ::= '(' IDValue DagArgList ')'
1164/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001165/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001166/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1167/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1168/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001169/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001170/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1171///
David Greened4263a62011-10-19 13:04:20 +00001172Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1173 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001174 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001175 switch (Lex.getCode()) {
1176 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001177 case tgtok::paste:
1178 // This is a leading paste operation. This is deprecated but
1179 // still exists in some .td files. Ignore it.
1180 Lex.Lex(); // Skip '#'.
1181 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001182 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001183 case tgtok::StrVal: {
1184 std::string Val = Lex.getCurStrVal();
1185 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001186
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001187 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001188 while (Lex.getCode() == tgtok::StrVal) {
1189 Val += Lex.getCurStrVal();
1190 Lex.Lex();
1191 }
Bob Wilson7248f862009-11-22 04:24:42 +00001192
David Greenee32ebf22011-07-29 19:07:07 +00001193 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001194 break;
1195 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001196 case tgtok::CodeFragment:
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +00001197 R = StringInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001198 Lex.Lex();
1199 break;
1200 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001201 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001202 Lex.Lex();
1203 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001204 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001205 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001206 std::string Name = Lex.getCurStrVal();
1207 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001208 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001209
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001210 // Value ::= ID '<' ValueListNE '>'
1211 if (Lex.Lex() == tgtok::greater) {
1212 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001213 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001214 }
David Greene8618f952009-06-08 20:23:18 +00001215
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001216 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1217 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1218 // body.
1219 Record *Class = Records.getClass(Name);
1220 if (!Class) {
1221 Error(NameLoc, "Expected a class name, got '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001222 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001223 }
David Greene8618f952009-06-08 20:23:18 +00001224
David Greeneaf8ee2c2011-07-29 22:43:06 +00001225 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
Craig Topper011817a2014-04-09 04:50:04 +00001226 if (ValueList.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001227
David Greene8618f952009-06-08 20:23:18 +00001228 if (Lex.getCode() != tgtok::greater) {
1229 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001230 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001231 }
1232 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001233 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001234
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001235 // Create the new record, set it as CurRec temporarily.
Alp Tokerce91fe52013-12-21 18:51:00 +00001236 Record *NewRec = new Record(GetNewAnonymousName(), NameLoc, Records,
Jordan Roseabdd99b2013-01-10 18:50:05 +00001237 /*IsAnonymous=*/true);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001238 SubClassReference SCRef;
Jordan Rosef12e8a92013-01-10 18:50:11 +00001239 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001240 SCRef.Rec = Class;
1241 SCRef.TemplateArgs = ValueList;
1242 // Add info about the subclass to NewRec.
1243 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001244 return nullptr;
Hal Finkela8c1f462014-01-02 20:47:09 +00001245 if (!CurMultiClass) {
1246 NewRec->resolveReferences();
1247 Records.addDef(NewRec);
1248 } else {
1249 // Otherwise, we're inside a multiclass, add it to the multiclass.
1250 CurMultiClass->DefPrototypes.push_back(NewRec);
1251
1252 // Copy the template arguments for the multiclass into the def.
1253 const std::vector<Init *> &TArgs =
1254 CurMultiClass->Rec.getTemplateArgs();
1255
1256 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1257 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1258 assert(RV && "Template arg doesn't exist?");
1259 NewRec->addValue(*RV);
1260 }
1261
1262 // We can't return the prototype def here, instead return:
1263 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1264 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1265 assert(MCNameRV && "multiclass record must have a NAME");
1266
1267 return UnOpInit::get(UnOpInit::CAST,
1268 BinOpInit::get(BinOpInit::STRCONCAT,
1269 VarInit::get(MCNameRV->getName(),
1270 MCNameRV->getType()),
1271 NewRec->getNameInit(),
1272 StringRecTy::get()),
1273 Class->getDefInit()->getType());
1274 }
Bob Wilson7248f862009-11-22 04:24:42 +00001275
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001276 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001277 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001278 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001279 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001280 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001281 Lex.Lex(); // eat the '{'
David Greeneaf8ee2c2011-07-29 22:43:06 +00001282 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001283
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001284 if (Lex.getCode() != tgtok::r_brace) {
1285 Vals = ParseValueList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001286 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001287 }
1288 if (Lex.getCode() != tgtok::r_brace) {
1289 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001290 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001291 }
1292 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001293
David Greeneaf8ee2c2011-07-29 22:43:06 +00001294 SmallVector<Init *, 16> NewBits(Vals.size());
David Greeneb3da8122011-07-29 19:07:00 +00001295
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001296 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001297 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001298 if (!Bit) {
Chris Lattner52416952007-11-22 21:06:59 +00001299 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1300 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001301 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001302 }
David Greeneb3da8122011-07-29 19:07:00 +00001303 NewBits[Vals.size()-i-1] = Bit;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001304 }
David Greenee32ebf22011-07-29 19:07:07 +00001305 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001306 }
1307 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1308 Lex.Lex(); // eat the '['
David Greeneaf8ee2c2011-07-29 22:43:06 +00001309 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001310
Craig Topper011817a2014-04-09 04:50:04 +00001311 RecTy *DeducedEltTy = nullptr;
1312 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001313
Craig Topper011817a2014-04-09 04:50:04 +00001314 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001315 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001316 if (!ListType) {
Alp Tokere69170a2014-06-26 22:52:05 +00001317 std::string s;
1318 raw_string_ostream ss(s);
Reid Klecknerd78273f2013-08-06 22:51:21 +00001319 ss << "Type mismatch for list, expected list type, got "
1320 << ItemType->getAsString();
1321 TokError(ss.str());
Craig Topper011817a2014-04-09 04:50:04 +00001322 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001323 }
1324 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001325 }
David Greene8618f952009-06-08 20:23:18 +00001326
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001327 if (Lex.getCode() != tgtok::r_square) {
Craig Topper011817a2014-04-09 04:50:04 +00001328 Vals = ParseValueList(CurRec, nullptr,
1329 GivenListTy ? GivenListTy->getElementType() : nullptr);
1330 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001331 }
1332 if (Lex.getCode() != tgtok::r_square) {
1333 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001334 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001335 }
1336 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001337
Craig Topper011817a2014-04-09 04:50:04 +00001338 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001339 if (Lex.getCode() == tgtok::less) {
1340 // Optional list element type
1341 Lex.Lex(); // eat the '<'
1342
1343 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001344 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001345 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001346 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001347 }
1348
1349 if (Lex.getCode() != tgtok::greater) {
1350 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001351 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001352 }
1353 Lex.Lex(); // eat the '>'
1354 }
1355
1356 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001357 RecTy *EltTy = nullptr;
David Greeneaf8ee2c2011-07-29 22:43:06 +00001358 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greene8618f952009-06-08 20:23:18 +00001359 i != ie;
1360 ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +00001361 TypedInit *TArg = dyn_cast<TypedInit>(*i);
Craig Topper011817a2014-04-09 04:50:04 +00001362 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001363 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001364 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001365 }
Craig Topper011817a2014-04-09 04:50:04 +00001366 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001367 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001368 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001369 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001370 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001371 }
Bob Wilson7248f862009-11-22 04:24:42 +00001372 } else {
David Greene8618f952009-06-08 20:23:18 +00001373 EltTy = TArg->getType();
1374 }
1375 }
1376
Craig Topper011817a2014-04-09 04:50:04 +00001377 if (GivenEltTy) {
1378 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001379 // Verify consistency
1380 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1381 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001382 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001383 }
1384 }
1385 EltTy = GivenEltTy;
1386 }
1387
Craig Topper011817a2014-04-09 04:50:04 +00001388 if (!EltTy) {
1389 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001390 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001391 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001392 }
1393 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001394 } else {
David Greene8618f952009-06-08 20:23:18 +00001395 // Make sure the deduced type is compatible with the given type
1396 if (GivenListTy) {
1397 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1398 TokError("Element type mismatch for list");
Craig Topper011817a2014-04-09 04:50:04 +00001399 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001400 }
1401 }
1402 DeducedEltTy = EltTy;
1403 }
Bob Wilson7248f862009-11-22 04:24:42 +00001404
David Greenee32ebf22011-07-29 19:07:07 +00001405 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001406 }
1407 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1408 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001409 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001410 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001411 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001412 }
Bob Wilson7248f862009-11-22 04:24:42 +00001413
David Greeneaf8ee2c2011-07-29 22:43:06 +00001414 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001415 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001416
Nate Begemandbe3f772009-03-19 05:21:56 +00001417 // If the operator name is present, parse it.
1418 std::string OperatorName;
1419 if (Lex.getCode() == tgtok::colon) {
1420 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1421 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001422 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001423 }
1424 OperatorName = Lex.getCurStrVal();
1425 Lex.Lex(); // eat the VarName.
1426 }
Bob Wilson7248f862009-11-22 04:24:42 +00001427
David Greeneaf8ee2c2011-07-29 22:43:06 +00001428 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001429 if (Lex.getCode() != tgtok::r_paren) {
1430 DagArgs = ParseDagArgList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001431 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001432 }
Bob Wilson7248f862009-11-22 04:24:42 +00001433
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001434 if (Lex.getCode() != tgtok::r_paren) {
1435 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001436 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001437 }
1438 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001439
David Greenee32ebf22011-07-29 19:07:07 +00001440 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001441 }
Bob Wilson7248f862009-11-22 04:24:42 +00001442
David Greene2f7cf7f2011-01-07 17:05:37 +00001443 case tgtok::XHead:
1444 case tgtok::XTail:
1445 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001446 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001447 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001448 case tgtok::XADD:
Bob Wilson7248f862009-11-22 04:24:42 +00001449 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001450 case tgtok::XSRL:
1451 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001452 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001453 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001454 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001455 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001456 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001457 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001458 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001459 }
1460 }
Bob Wilson7248f862009-11-22 04:24:42 +00001461
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001462 return R;
1463}
1464
1465/// ParseValue - Parse a tblgen value. This returns null on error.
1466///
1467/// Value ::= SimpleValue ValueSuffix*
1468/// ValueSuffix ::= '{' BitList '}'
1469/// ValueSuffix ::= '[' BitList ']'
1470/// ValueSuffix ::= '.' ID
1471///
David Greened4263a62011-10-19 13:04:20 +00001472Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1473 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001474 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001475
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001476 // Parse the suffixes now if present.
1477 while (1) {
1478 switch (Lex.getCode()) {
1479 default: return Result;
1480 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001481 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001482 // This is the beginning of the object body.
1483 return Result;
1484
Chris Lattner526c8cb2009-06-21 03:39:35 +00001485 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001486 Lex.Lex(); // eat the '{'
1487 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001488 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001489
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001490 // Reverse the bitlist.
1491 std::reverse(Ranges.begin(), Ranges.end());
1492 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001493 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001494 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001495 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001496 }
Bob Wilson7248f862009-11-22 04:24:42 +00001497
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001498 // Eat the '}'.
1499 if (Lex.getCode() != tgtok::r_brace) {
1500 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001501 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001502 }
1503 Lex.Lex();
1504 break;
1505 }
1506 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001507 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001508 Lex.Lex(); // eat the '['
1509 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001510 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001511
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001512 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001513 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001514 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001515 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001516 }
Bob Wilson7248f862009-11-22 04:24:42 +00001517
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001518 // Eat the ']'.
1519 if (Lex.getCode() != tgtok::r_square) {
1520 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001521 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001522 }
1523 Lex.Lex();
1524 break;
1525 }
1526 case tgtok::period:
1527 if (Lex.Lex() != tgtok::Id) { // eat the .
1528 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001529 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001530 }
1531 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001532 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001533 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001534 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001535 }
David Greenee32ebf22011-07-29 19:07:07 +00001536 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001537 Lex.Lex(); // eat field name
1538 break;
David Greene8e85b482011-10-19 13:04:43 +00001539
1540 case tgtok::paste:
1541 SMLoc PasteLoc = Lex.getLoc();
1542
1543 // Create a !strconcat() operation, first casting each operand to
1544 // a string if necessary.
1545
Sean Silvafb509ed2012-10-10 20:24:43 +00001546 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001547 if (!LHS) {
1548 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001549 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001550 }
1551
1552 if (LHS->getType() != StringRecTy::get()) {
1553 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1554 }
1555
Craig Topper011817a2014-04-09 04:50:04 +00001556 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001557
1558 Lex.Lex(); // Eat the '#'.
1559 switch (Lex.getCode()) {
1560 case tgtok::colon:
1561 case tgtok::semi:
1562 case tgtok::l_brace:
1563 // These are all of the tokens that can begin an object body.
1564 // Some of these can also begin values but we disallow those cases
1565 // because they are unlikely to be useful.
1566
1567 // Trailing paste, concat with an empty string.
1568 RHS = StringInit::get("");
1569 break;
1570
1571 default:
1572 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001573 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001574 if (!RHS) {
1575 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001576 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001577 }
1578
1579 if (RHS->getType() != StringRecTy::get()) {
1580 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1581 }
1582
1583 break;
1584 }
1585
1586 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1587 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1588 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001589 }
1590 }
1591}
1592
1593/// ParseDagArgList - Parse the argument list for a dag literal expression.
1594///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001595/// DagArg ::= Value (':' VARNAME)?
1596/// DagArg ::= VARNAME
1597/// DagArgList ::= DagArg
1598/// DagArgList ::= DagArgList ',' DagArg
David Greeneaf8ee2c2011-07-29 22:43:06 +00001599std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001600TGParser::ParseDagArgList(Record *CurRec) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001601 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson7248f862009-11-22 04:24:42 +00001602
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001603 while (1) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001604 // DagArg ::= VARNAME
1605 if (Lex.getCode() == tgtok::VarName) {
1606 // A missing value is treated like '?'.
1607 Result.push_back(std::make_pair(UnsetInit::get(), Lex.getCurStrVal()));
1608 Lex.Lex();
1609 } else {
1610 // DagArg ::= Value (':' VARNAME)?
1611 Init *Val = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001612 if (!Val)
David Greeneaf8ee2c2011-07-29 22:43:06 +00001613 return std::vector<std::pair<llvm::Init*, std::string> >();
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001614
1615 // If the variable name is present, add it.
1616 std::string VarName;
1617 if (Lex.getCode() == tgtok::colon) {
1618 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1619 TokError("expected variable name in dag literal");
1620 return std::vector<std::pair<llvm::Init*, std::string> >();
1621 }
1622 VarName = Lex.getCurStrVal();
1623 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001624 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001625
1626 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001627 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001628 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001629 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001630 }
Bob Wilson7248f862009-11-22 04:24:42 +00001631
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001632 return Result;
1633}
1634
1635
1636/// ParseValueList - Parse a comma separated list of values, returning them as a
1637/// vector. Note that this always expects to be able to parse at least one
1638/// value. It returns an empty list if this is not possible.
1639///
1640/// ValueList ::= Value (',' Value)
1641///
David Greeneaf8ee2c2011-07-29 22:43:06 +00001642std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopher71520a82011-07-11 23:06:52 +00001643 RecTy *EltTy) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001644 std::vector<Init*> Result;
David Greene8618f952009-06-08 20:23:18 +00001645 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001646 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001647 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001648 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001649 if (!TArgs.size()) {
1650 TokError("template argument provided to non-template class");
1651 return std::vector<Init*>();
1652 }
David Greene8618f952009-06-08 20:23:18 +00001653 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001654 if (!RV) {
1655 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1656 << ")\n";
1657 }
David Greene8618f952009-06-08 20:23:18 +00001658 assert(RV && "Template argument record not found??");
1659 ItemType = RV->getType();
1660 ++ArgN;
1661 }
1662 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001663 if (!Result.back()) return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001664
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001665 while (Lex.getCode() == tgtok::comma) {
1666 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001667
Craig Topper011817a2014-04-09 04:50:04 +00001668 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001669 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001670 if (ArgN >= TArgs.size()) {
1671 TokError("too many template arguments");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001672 return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001673 }
David Greene8618f952009-06-08 20:23:18 +00001674 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1675 assert(RV && "Template argument record not found??");
1676 ItemType = RV->getType();
1677 ++ArgN;
1678 }
1679 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001680 if (!Result.back()) return std::vector<Init*>();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001681 }
Bob Wilson7248f862009-11-22 04:24:42 +00001682
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001683 return Result;
1684}
1685
1686
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001687/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1688/// empty string on error. This can happen in a number of different context's,
1689/// including within a def or in the template args for a def (which which case
1690/// CurRec will be non-null) and within the template args for a multiclass (in
1691/// which case CurRec will be null, but CurMultiClass will be set). This can
1692/// also happen within a def that is within a multiclass, which will set both
1693/// CurRec and CurMultiClass.
1694///
1695/// Declaration ::= FIELD? Type ID ('=' Value)?
1696///
David Greenedb10e692011-10-19 13:02:42 +00001697Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001698 bool ParsingTemplateArgs) {
1699 // Read the field prefix if present.
1700 bool HasField = Lex.getCode() == tgtok::Field;
1701 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001702
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001703 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001704 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001705
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001706 if (Lex.getCode() != tgtok::Id) {
1707 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001708 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001709 }
Bob Wilson7248f862009-11-22 04:24:42 +00001710
Chris Lattner526c8cb2009-06-21 03:39:35 +00001711 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001712 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001713 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001714
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001715 if (ParsingTemplateArgs) {
1716 if (CurRec) {
David Greenedb10e692011-10-19 13:02:42 +00001717 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001718 } else {
1719 assert(CurMultiClass);
1720 }
1721 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001722 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1723 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001724 }
Bob Wilson7248f862009-11-22 04:24:42 +00001725
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001726 // Add the value.
1727 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001728 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001729
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001730 // If a value is present, parse it.
1731 if (Lex.getCode() == tgtok::equal) {
1732 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001733 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001734 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001735 if (!Val ||
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001736 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
Craig Topper011817a2014-04-09 04:50:04 +00001737 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001738 }
Bob Wilson7248f862009-11-22 04:24:42 +00001739
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001740 return DeclName;
1741}
1742
David Greenefb927af2012-02-22 16:09:41 +00001743/// ParseForeachDeclaration - Read a foreach declaration, returning
1744/// the name of the declared object or a NULL Init on error. Return
1745/// the name of the parsed initializer list through ForeachListName.
1746///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001747/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1748/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1749/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001750///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001751VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001752 if (Lex.getCode() != tgtok::Id) {
1753 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001754 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001755 }
1756
1757 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1758 Lex.Lex();
1759
1760 // If a value is present, parse it.
1761 if (Lex.getCode() != tgtok::equal) {
1762 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001763 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001764 }
1765 Lex.Lex(); // Eat the '='
1766
Craig Topper011817a2014-04-09 04:50:04 +00001767 RecTy *IterType = nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001768 std::vector<unsigned> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001769
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001770 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001771 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001772 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001773 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001774 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001775 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001776 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001777 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001778 }
1779 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001780 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001781 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001782 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001783 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001784 }
1785 IterType = ListType->getElementType();
1786 break;
David Greenefb927af2012-02-22 16:09:41 +00001787 }
1788
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001789 case tgtok::IntVal: { // RangePiece.
1790 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001791 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001792 break;
David Greenefb927af2012-02-22 16:09:41 +00001793 }
1794
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001795 case tgtok::l_brace: { // '{' RangeList '}'
1796 Lex.Lex(); // eat the '{'
1797 Ranges = ParseRangeList();
1798 if (Lex.getCode() != tgtok::r_brace) {
1799 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001800 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001801 }
1802 Lex.Lex();
1803 break;
1804 }
1805 }
David Greenefb927af2012-02-22 16:09:41 +00001806
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001807 if (!Ranges.empty()) {
1808 assert(!IterType && "Type already initialized?");
1809 IterType = IntRecTy::get();
1810 std::vector<Init*> Values;
1811 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
1812 Values.push_back(IntInit::get(Ranges[i]));
1813 ForeachListValue = ListInit::get(Values, IterType);
1814 }
1815
1816 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001817 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001818
1819 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001820}
1821
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001822/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1823/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1824/// template args for a def, which may or may not be in a multiclass. If null,
1825/// these are the template args for a multiclass.
1826///
1827/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001828///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001829bool TGParser::ParseTemplateArgList(Record *CurRec) {
1830 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1831 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001832
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001833 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001834
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001835 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001836 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001837 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001838 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001839
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001840 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001841
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001842 while (Lex.getCode() == tgtok::comma) {
1843 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001844
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001845 // Read the following declarations.
1846 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001847 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001848 return true;
1849 TheRecToAddTo->addTemplateArg(TemplArg);
1850 }
Bob Wilson7248f862009-11-22 04:24:42 +00001851
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001852 if (Lex.getCode() != tgtok::greater)
1853 return TokError("expected '>' at end of template argument list");
1854 Lex.Lex(); // eat the '>'.
1855 return false;
1856}
1857
1858
1859/// ParseBodyItem - Parse a single item at within the body of a def or class.
1860///
1861/// BodyItem ::= Declaration ';'
1862/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1863bool TGParser::ParseBodyItem(Record *CurRec) {
1864 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001865 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001866 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001867
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001868 if (Lex.getCode() != tgtok::semi)
1869 return TokError("expected ';' after declaration");
1870 Lex.Lex();
1871 return false;
1872 }
1873
1874 // LET ID OptionalRangeList '=' Value ';'
1875 if (Lex.Lex() != tgtok::Id)
1876 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001877
Chris Lattner526c8cb2009-06-21 03:39:35 +00001878 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001879 std::string FieldName = Lex.getCurStrVal();
1880 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001881
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001882 std::vector<unsigned> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001883 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001884 return true;
1885 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001886
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001887 if (Lex.getCode() != tgtok::equal)
1888 return TokError("expected '=' in let expression");
1889 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001890
David Greene8618f952009-06-08 20:23:18 +00001891 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001892 if (!Field)
David Greene8618f952009-06-08 20:23:18 +00001893 return TokError("Value '" + FieldName + "' unknown!");
1894
1895 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001896
David Greeneaf8ee2c2011-07-29 22:43:06 +00001897 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001898 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001899
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001900 if (Lex.getCode() != tgtok::semi)
1901 return TokError("expected ';' after let expression");
1902 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001903
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001904 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1905}
1906
1907/// ParseBody - Read the body of a class or def. Return true on error, false on
1908/// success.
1909///
1910/// Body ::= ';'
1911/// Body ::= '{' BodyList '}'
1912/// BodyList BodyItem*
1913///
1914bool TGParser::ParseBody(Record *CurRec) {
1915 // If this is a null definition, just eat the semi and return.
1916 if (Lex.getCode() == tgtok::semi) {
1917 Lex.Lex();
1918 return false;
1919 }
Bob Wilson7248f862009-11-22 04:24:42 +00001920
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001921 if (Lex.getCode() != tgtok::l_brace)
1922 return TokError("Expected ';' or '{' to start body");
1923 // Eat the '{'.
1924 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001925
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001926 while (Lex.getCode() != tgtok::r_brace)
1927 if (ParseBodyItem(CurRec))
1928 return true;
1929
1930 // Eat the '}'.
1931 Lex.Lex();
1932 return false;
1933}
1934
Sean Silvacb1a75e2013-01-09 04:49:14 +00001935/// \brief Apply the current let bindings to \a CurRec.
1936/// \returns true on error, false otherwise.
1937bool TGParser::ApplyLetStack(Record *CurRec) {
1938 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1939 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1940 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1941 LetStack[i][j].Bits, LetStack[i][j].Value))
1942 return true;
1943 return false;
1944}
1945
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001946/// ParseObjectBody - Parse the body of a def or class. This consists of an
1947/// optional ClassList followed by a Body. CurRec is the current def or class
1948/// that is being parsed.
1949///
1950/// ObjectBody ::= BaseClassList Body
1951/// BaseClassList ::= /*empty*/
1952/// BaseClassList ::= ':' BaseClassListNE
1953/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1954///
1955bool TGParser::ParseObjectBody(Record *CurRec) {
1956 // If there is a baseclass list, read it.
1957 if (Lex.getCode() == tgtok::colon) {
1958 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001959
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001960 // Read all of the subclasses.
1961 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1962 while (1) {
1963 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00001964 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001965
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001966 // Add it.
1967 if (AddSubClass(CurRec, SubClass))
1968 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001969
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001970 if (Lex.getCode() != tgtok::comma) break;
1971 Lex.Lex(); // eat ','.
1972 SubClass = ParseSubClassReference(CurRec, false);
1973 }
1974 }
1975
Sean Silvacb1a75e2013-01-09 04:49:14 +00001976 if (ApplyLetStack(CurRec))
1977 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001978
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001979 return ParseBody(CurRec);
1980}
1981
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001982/// ParseDef - Parse and return a top level or multiclass def, return the record
1983/// corresponding to it. This returns null on error.
1984///
1985/// DefInst ::= DEF ObjectName ObjectBody
1986///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00001987bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001988 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001989 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00001990 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001991
1992 // Parse ObjectName and make a record for it.
Jordan Roseabdd99b2013-01-10 18:50:05 +00001993 Record *CurRec;
1994 Init *Name = ParseObjectName(CurMultiClass);
1995 if (Name)
1996 CurRec = new Record(Name, DefLoc, Records);
1997 else
1998 CurRec = new Record(GetNewAnonymousName(), DefLoc, Records,
1999 /*IsAnonymous=*/true);
Bob Wilson7248f862009-11-22 04:24:42 +00002000
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002001 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002002 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002003
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002004 // Ensure redefinition doesn't happen.
David Greeneebb006f2012-01-28 00:03:24 +00002005 if (Records.getDef(CurRec->getNameInitAsString())) {
David Greene3a20f5a2011-10-19 13:03:45 +00002006 Error(DefLoc, "def '" + CurRec->getNameInitAsString()
2007 + "' already defined");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002008 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002009 }
2010 Records.addDef(CurRec);
Hal Finkela8c1f462014-01-02 20:47:09 +00002011
2012 if (ParseObjectBody(CurRec))
2013 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002014 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002015 // Parse the body before adding this prototype to the DefPrototypes vector.
2016 // That way implicit definitions will be added to the DefPrototypes vector
2017 // before this object, instantiated prior to defs derived from this object,
2018 // and this available for indirect name resolution when defs derived from
2019 // this object are instantiated.
2020 if (ParseObjectBody(CurRec))
2021 return true;
2022
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002023 // Otherwise, a def inside a multiclass, add it to the multiclass.
2024 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene07e055f2011-10-19 13:03:51 +00002025 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2026 == CurRec->getNameInit()) {
2027 Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002028 "' already defined in this multiclass!");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002029 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002030 }
2031 CurMultiClass->DefPrototypes.push_back(CurRec);
Hal Finkela8c1f462014-01-02 20:47:09 +00002032 } else if (ParseObjectBody(CurRec))
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002033 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002034
Craig Topper011817a2014-04-09 04:50:04 +00002035 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002036 // See Record::setName(). This resolve step will see any new name
2037 // for the def that might have been created when resolving
2038 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002039 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002040
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002041 // If ObjectBody has template arguments, it's an error.
2042 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002043
2044 if (CurMultiClass) {
2045 // Copy the template arguments for the multiclass into the def.
David Greenedb10e692011-10-19 13:02:42 +00002046 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002047 CurMultiClass->Rec.getTemplateArgs();
2048
2049 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2050 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
2051 assert(RV && "Template arg doesn't exist?");
2052 CurRec->addValue(*RV);
2053 }
2054 }
2055
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002056 if (ProcessForeachDefs(CurRec, DefLoc)) {
David Greenefb927af2012-02-22 16:09:41 +00002057 Error(DefLoc,
2058 "Could not process loops for def" + CurRec->getNameInitAsString());
2059 return true;
2060 }
2061
2062 return false;
2063}
2064
2065/// ParseForeach - Parse a for statement. Return the record corresponding
2066/// to it. This returns true on error.
2067///
2068/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2069/// Foreach ::= FOREACH Declaration IN Object
2070///
2071bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2072 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2073 Lex.Lex(); // Eat the 'for' token.
2074
2075 // Make a temporary object to record items associated with the for
2076 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002077 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002078 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002079 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002080 return TokError("expected declaration in for");
2081
2082 if (Lex.getCode() != tgtok::In)
2083 return TokError("Unknown tok");
2084 Lex.Lex(); // Eat the in
2085
2086 // Create a loop object and remember it.
2087 Loops.push_back(ForeachLoop(IterName, ListValue));
2088
2089 if (Lex.getCode() != tgtok::l_brace) {
2090 // FOREACH Declaration IN Object
2091 if (ParseObject(CurMultiClass))
2092 return true;
2093 }
2094 else {
2095 SMLoc BraceLoc = Lex.getLoc();
2096 // Otherwise, this is a group foreach.
2097 Lex.Lex(); // eat the '{'.
2098
2099 // Parse the object list.
2100 if (ParseObjectList(CurMultiClass))
2101 return true;
2102
2103 if (Lex.getCode() != tgtok::r_brace) {
2104 TokError("expected '}' at end of foreach command");
2105 return Error(BraceLoc, "to match this '{'");
2106 }
2107 Lex.Lex(); // Eat the }
2108 }
2109
2110 // We've processed everything in this loop.
2111 Loops.pop_back();
2112
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002113 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002114}
2115
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002116/// ParseClass - Parse a tblgen class definition.
2117///
2118/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2119///
2120bool TGParser::ParseClass() {
2121 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2122 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002123
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002124 if (Lex.getCode() != tgtok::Id)
2125 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002126
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002127 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2128 if (CurRec) {
2129 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002130 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002131 !CurRec->getSuperClasses().empty() ||
2132 !CurRec->getTemplateArgs().empty())
David Greene8eed9982011-10-19 13:03:58 +00002133 return TokError("Class '" + CurRec->getNameInitAsString()
2134 + "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002135 } else {
2136 // If this is the first reference to this class, create and add it.
Chris Lattner89dcb682010-12-15 04:48:22 +00002137 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002138 Records.addClass(CurRec);
2139 }
2140 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002141
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002142 // If there are template args, parse them.
2143 if (Lex.getCode() == tgtok::less)
2144 if (ParseTemplateArgList(CurRec))
2145 return true;
2146
2147 // Finally, parse the object body.
2148 return ParseObjectBody(CurRec);
2149}
2150
2151/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2152/// of LetRecords.
2153///
2154/// LetList ::= LetItem (',' LetItem)*
2155/// LetItem ::= ID OptionalRangeList '=' Value
2156///
2157std::vector<LetRecord> TGParser::ParseLetList() {
2158 std::vector<LetRecord> Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002159
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002160 while (1) {
2161 if (Lex.getCode() != tgtok::Id) {
2162 TokError("expected identifier in let definition");
2163 return std::vector<LetRecord>();
2164 }
2165 std::string Name = Lex.getCurStrVal();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002166 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002167 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002168
2169 // Check for an optional RangeList.
2170 std::vector<unsigned> Bits;
Bob Wilson7248f862009-11-22 04:24:42 +00002171 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002172 return std::vector<LetRecord>();
2173 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002174
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002175 if (Lex.getCode() != tgtok::equal) {
2176 TokError("expected '=' in let expression");
2177 return std::vector<LetRecord>();
2178 }
2179 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002180
Craig Topper011817a2014-04-09 04:50:04 +00002181 Init *Val = ParseValue(nullptr);
2182 if (!Val) return std::vector<LetRecord>();
Bob Wilson7248f862009-11-22 04:24:42 +00002183
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002184 // Now that we have everything, add the record.
2185 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson7248f862009-11-22 04:24:42 +00002186
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002187 if (Lex.getCode() != tgtok::comma)
2188 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002189 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002190 }
2191}
2192
2193/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002194/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002195///
2196/// Object ::= LET LetList IN '{' ObjectList '}'
2197/// Object ::= LET LetList IN Object
2198///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002199bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002200 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2201 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002202
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002203 // Add this entry to the let stack.
2204 std::vector<LetRecord> LetInfo = ParseLetList();
2205 if (LetInfo.empty()) return true;
2206 LetStack.push_back(LetInfo);
2207
2208 if (Lex.getCode() != tgtok::In)
2209 return TokError("expected 'in' at end of top-level 'let'");
2210 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002211
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002212 // If this is a scalar let, just handle it now
2213 if (Lex.getCode() != tgtok::l_brace) {
2214 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002215 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002216 return true;
2217 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002218 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002219 // Otherwise, this is a group let.
2220 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002221
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002222 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002223 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002224 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002225
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002226 if (Lex.getCode() != tgtok::r_brace) {
2227 TokError("expected '}' at end of top level let command");
2228 return Error(BraceLoc, "to match this '{'");
2229 }
2230 Lex.Lex();
2231 }
Bob Wilson7248f862009-11-22 04:24:42 +00002232
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002233 // Outside this let scope, this let block is not active.
2234 LetStack.pop_back();
2235 return false;
2236}
2237
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002238/// ParseMultiClass - Parse a multiclass definition.
2239///
Bob Wilson3d948162009-04-28 19:41:44 +00002240/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002241/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2242/// MultiClassObject ::= DefInst
2243/// MultiClassObject ::= MultiClassInst
2244/// MultiClassObject ::= DefMInst
2245/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2246/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002247///
2248bool TGParser::ParseMultiClass() {
2249 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2250 Lex.Lex(); // Eat the multiclass token.
2251
2252 if (Lex.getCode() != tgtok::Id)
2253 return TokError("expected identifier after multiclass for name");
2254 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002255
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002256 if (MultiClasses.count(Name))
2257 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002258
Chris Lattner77d369c2010-12-13 00:23:57 +00002259 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
2260 Lex.getLoc(), Records);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002261 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002262
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002263 // If there are template args, parse them.
2264 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002265 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002266 return true;
2267
David Greene7049e792009-04-24 16:55:41 +00002268 bool inherits = false;
2269
David Greene753ed8f2009-04-22 16:42:54 +00002270 // If there are submulticlasses, parse them.
2271 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002272 inherits = true;
2273
David Greene753ed8f2009-04-22 16:42:54 +00002274 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002275
David Greene753ed8f2009-04-22 16:42:54 +00002276 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002277 SubMultiClassReference SubMultiClass =
2278 ParseSubMultiClassReference(CurMultiClass);
David Greene753ed8f2009-04-22 16:42:54 +00002279 while (1) {
2280 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002281 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002282
David Greene753ed8f2009-04-22 16:42:54 +00002283 // Add it.
2284 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2285 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002286
David Greene753ed8f2009-04-22 16:42:54 +00002287 if (Lex.getCode() != tgtok::comma) break;
2288 Lex.Lex(); // eat ','.
2289 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2290 }
2291 }
2292
David Greene7049e792009-04-24 16:55:41 +00002293 if (Lex.getCode() != tgtok::l_brace) {
2294 if (!inherits)
2295 return TokError("expected '{' in multiclass definition");
Bob Wilson7248f862009-11-22 04:24:42 +00002296 else if (Lex.getCode() != tgtok::semi)
2297 return TokError("expected ';' in multiclass definition");
David Greene7049e792009-04-24 16:55:41 +00002298 else
Bob Wilson7248f862009-11-22 04:24:42 +00002299 Lex.Lex(); // eat the ';'.
2300 } else {
David Greene7049e792009-04-24 16:55:41 +00002301 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2302 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002303
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002304 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002305 switch (Lex.getCode()) {
2306 default:
David Greene33f61992011-10-07 18:25:05 +00002307 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002308 case tgtok::Let:
2309 case tgtok::Def:
2310 case tgtok::Defm:
David Greenefb927af2012-02-22 16:09:41 +00002311 case tgtok::Foreach:
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002312 if (ParseObject(CurMultiClass))
2313 return true;
2314 break;
2315 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002316 }
David Greene7049e792009-04-24 16:55:41 +00002317 Lex.Lex(); // eat the '}'.
2318 }
Bob Wilson7248f862009-11-22 04:24:42 +00002319
Craig Topper011817a2014-04-09 04:50:04 +00002320 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002321 return false;
2322}
2323
David Greenedb445972011-10-05 22:42:07 +00002324Record *TGParser::
2325InstantiateMulticlassDef(MultiClass &MC,
2326 Record *DefProto,
Hal Finkelf2a0b2b2014-01-02 19:35:33 +00002327 Init *&DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002328 SMRange DefmPrefixRange) {
David Greene5d5d88c2011-10-19 13:04:31 +00002329 // We need to preserve DefProto so it can be reused for later
2330 // instantiations, so create a new Record to inherit from it.
2331
David Greenedb445972011-10-05 22:42:07 +00002332 // Add in the defm name. If the defm prefix is empty, give each
2333 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2334 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2335 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002336
Jordan Roseabdd99b2013-01-10 18:50:05 +00002337 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002338 if (!DefmPrefix) {
David Greene5d5d88c2011-10-19 13:04:31 +00002339 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002340 IsAnonymous = true;
2341 }
David Greene5d5d88c2011-10-19 13:04:31 +00002342
2343 Init *DefName = DefProto->getNameInit();
2344
Sean Silvafb509ed2012-10-10 20:24:43 +00002345 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002346
Craig Topper011817a2014-04-09 04:50:04 +00002347 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002348 // We have a fully expanded string so there are no operators to
2349 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002350 DefName =
2351 BinOpInit::get(BinOpInit::STRCONCAT,
2352 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2353 StringRecTy::get())->Fold(DefProto, &MC),
2354 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2355 }
David Greene5d5d88c2011-10-19 13:04:31 +00002356
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002357 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002358 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002359 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002360 Record *CurRec = new Record(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002361
2362 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002363 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002364 Ref.Rec = DefProto;
2365 AddSubClass(CurRec, Ref);
2366
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002367 // Set the value for NAME. We don't resolve references to it 'til later,
2368 // though, so that uses in nested multiclass names don't get
2369 // confused.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002370 if (SetValue(CurRec, Ref.RefRange.Start, "NAME", std::vector<unsigned>(),
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002371 DefmPrefix)) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002372 Error(DefmPrefixRange.Start, "Could not resolve "
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002373 + CurRec->getNameInitAsString() + ":NAME to '"
2374 + DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002375 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002376 }
David Greene8bf0d722011-10-19 13:04:35 +00002377
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002378 // If the DefNameString didn't resolve, we probably have a reference to
2379 // NAME and need to replace it. We need to do at least this much greedily,
2380 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002381 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002382 RecordVal *DefNameRV = CurRec->getValue("NAME");
2383 CurRec->resolveReferencesTo(DefNameRV);
2384 }
2385
2386 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002387 // Now that we're at the top level, resolve all NAME references
2388 // in the resultant defs that weren't in the def names themselves.
2389 RecordVal *DefNameRV = CurRec->getValue("NAME");
2390 CurRec->resolveReferencesTo(DefNameRV);
2391
2392 // Now that NAME references are resolved and we're at the top level of
2393 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002394 // currently in a multiclass, it means this defm appears inside a
2395 // multiclass and its name won't be fully resolvable until we see
2396 // the top-level defm. Therefore, we don't add this to the
2397 // RecordKeeper at this point. If we did we could get duplicate
2398 // defs as more than one probably refers to NAME or some other
2399 // common internal placeholder.
2400
2401 // Ensure redefinition doesn't happen.
2402 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002403 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002404 "' already defined, instantiating defm with subdef '" +
2405 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002406 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002407 }
2408
2409 Records.addDef(CurRec);
2410 }
2411
David Greenedb445972011-10-05 22:42:07 +00002412 return CurRec;
2413}
2414
2415bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2416 Record *CurRec,
2417 SMLoc DefmPrefixLoc,
2418 SMLoc SubClassLoc,
David Greenedb10e692011-10-19 13:02:42 +00002419 const std::vector<Init *> &TArgs,
David Greenedb445972011-10-05 22:42:07 +00002420 std::vector<Init *> &TemplateVals,
2421 bool DeleteArgs) {
2422 // Loop over all of the template arguments, setting them to the specified
2423 // value or leaving them as the default if necessary.
2424 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2425 // Check if a value is specified for this temp-arg.
2426 if (i < TemplateVals.size()) {
2427 // Set it now.
2428 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2429 TemplateVals[i]))
2430 return true;
2431
2432 // Resolve it next.
2433 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2434
2435 if (DeleteArgs)
2436 // Now remove it.
2437 CurRec->removeValue(TArgs[i]);
2438
2439 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2440 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenedb10e692011-10-19 13:02:42 +00002441 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2442 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2443 + "'");
David Greenedb445972011-10-05 22:42:07 +00002444 }
2445 }
2446 return false;
2447}
2448
2449bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2450 Record *CurRec,
2451 Record *DefProto,
2452 SMLoc DefmPrefixLoc) {
2453 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002454 if (ApplyLetStack(CurRec))
2455 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002456
David Greenedb445972011-10-05 22:42:07 +00002457 // Don't create a top level definition for defm inside multiclasses,
2458 // instead, only update the prototypes and bind the template args
2459 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002460 if (!CurMultiClass)
2461 return false;
2462 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2463 i != e; ++i)
2464 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2465 == CurRec->getNameInit())
2466 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2467 "' already defined in this multiclass!");
2468 CurMultiClass->DefPrototypes.push_back(CurRec);
David Greenedb445972011-10-05 22:42:07 +00002469
Sean Silvacc951b22013-01-09 05:28:12 +00002470 // Copy the template arguments for the multiclass into the new def.
2471 const std::vector<Init *> &TA =
2472 CurMultiClass->Rec.getTemplateArgs();
David Greenedb445972011-10-05 22:42:07 +00002473
Sean Silvacc951b22013-01-09 05:28:12 +00002474 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2475 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2476 assert(RV && "Template arg doesn't exist?");
2477 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002478 }
2479
2480 return false;
2481}
2482
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002483/// ParseDefm - Parse the instantiation of a multiclass.
2484///
2485/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2486///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002487bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002488 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002489 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002490 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002491
Craig Topperb21afc62013-01-07 05:09:33 +00002492 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002493 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002494 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002495
Jordan Rosef12e8a92013-01-10 18:50:11 +00002496 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002497 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002498 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002499
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002500 // Keep track of the new generated record definitions.
2501 std::vector<Record*> NewRecDefs;
2502
2503 // This record also inherits from a regular class (non-multiclass)?
2504 bool InheritFromClass = false;
2505
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002506 // eat the colon.
2507 Lex.Lex();
2508
Chris Lattner526c8cb2009-06-21 03:39:35 +00002509 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002510 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002511
2512 while (1) {
Craig Topper011817a2014-04-09 04:50:04 +00002513 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002514
2515 // To instantiate a multiclass, we need to first get the multiclass, then
2516 // instantiate each def contained in the multiclass with the SubClassRef
2517 // template parameters.
2518 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2519 assert(MC && "Didn't lookup multiclass correctly?");
David Greeneaf8ee2c2011-07-29 22:43:06 +00002520 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002521
2522 // Verify that the correct number of template arguments were specified.
David Greenedb10e692011-10-19 13:02:42 +00002523 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002524 if (TArgs.size() < TemplateVals.size())
2525 return Error(SubClassLoc,
2526 "more template args specified than multiclass expects");
2527
2528 // Loop over all the def's in the multiclass, instantiating each one.
2529 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2530 Record *DefProto = MC->DefPrototypes[i];
2531
Jordan Rosef12e8a92013-01-10 18:50:11 +00002532 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix,
2533 SMRange(DefmLoc,
2534 DefmPrefixEndLoc));
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002535 if (!CurRec)
2536 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002537
Jordan Rosef12e8a92013-01-10 18:50:11 +00002538 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002539 TArgs, TemplateVals, true/*Delete args*/))
2540 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002541
Jordan Rosef12e8a92013-01-10 18:50:11 +00002542 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002543 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002544
2545 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002546 }
2547
David Greenedb445972011-10-05 22:42:07 +00002548
David Greenef00919a2009-04-22 22:17:51 +00002549 if (Lex.getCode() != tgtok::comma) break;
2550 Lex.Lex(); // eat ','.
2551
Craig Topper998a39a2013-08-20 04:22:09 +00002552 if (Lex.getCode() != tgtok::Id)
2553 return TokError("expected identifier");
2554
David Greenef00919a2009-04-22 22:17:51 +00002555 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002556
2557 // A defm can inherit from regular classes (non-multiclass) as
2558 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002559 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002560
2561 if (InheritFromClass)
2562 break;
2563
Craig Topper011817a2014-04-09 04:50:04 +00002564 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002565 }
2566
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002567 if (InheritFromClass) {
2568 // Process all the classes to inherit as if they were part of a
2569 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002570 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002571 while (1) {
2572 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002573 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002574
2575 // Get the expanded definition prototypes and teach them about
2576 // the record values the current class to inherit has
2577 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2578 Record *CurRec = NewRecDefs[i];
2579
2580 // Add it.
2581 if (AddSubClass(CurRec, SubClass))
2582 return true;
2583
Sean Silvacb1a75e2013-01-09 04:49:14 +00002584 if (ApplyLetStack(CurRec))
2585 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002586 }
2587
2588 if (Lex.getCode() != tgtok::comma) break;
2589 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002590 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002591 }
2592 }
2593
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002594 if (!CurMultiClass)
2595 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene50c09122011-08-10 18:27:46 +00002596 // See Record::setName(). This resolve step will see any new
2597 // name for the def that might have been created when resolving
2598 // inheritance, values and arguments above.
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002599 NewRecDefs[i]->resolveReferences();
2600
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002601 if (Lex.getCode() != tgtok::semi)
2602 return TokError("expected ';' at end of defm");
2603 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002604
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002605 return false;
2606}
2607
2608/// ParseObject
2609/// Object ::= ClassInst
2610/// Object ::= DefInst
2611/// Object ::= MultiClassInst
2612/// Object ::= DefMInst
2613/// Object ::= LETCommand '{' ObjectList '}'
2614/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002615bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002616 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002617 default:
2618 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002619 case tgtok::Let: return ParseTopLevelLet(MC);
2620 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002621 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002622 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002623 case tgtok::Class: return ParseClass();
2624 case tgtok::MultiClass: return ParseMultiClass();
2625 }
2626}
2627
2628/// ParseObjectList
2629/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002630bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002631 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002632 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002633 return true;
2634 }
2635 return false;
2636}
2637
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002638bool TGParser::ParseFile() {
2639 Lex.Lex(); // Prime the lexer.
2640 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002641
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002642 // If we have unread input at the end of the file, report it.
2643 if (Lex.getCode() == tgtok::Eof)
2644 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002645
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002646 return TokError("Unexpected input at top level");
2647}
2648