blob: d020a8bdc1a3f3ecfabd798083089f1cef841918 [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:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000914 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +0000915 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000916 case tgtok::XSRL:
917 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000918 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000919 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000920 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000921 tgtok::TokKind OpTok = Lex.getCode();
922 SMLoc OpLoc = Lex.getLoc();
923 Lex.Lex(); // eat the operation
924
David Greene5d0c0512009-05-14 20:54:48 +0000925 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000926 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000927
Chris Lattner61ea00b2010-10-05 23:58:18 +0000928 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000929 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000930 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000931 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000932 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000933 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
934 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
935 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
936 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000937 case tgtok::XListConcat:
938 Code = BinOpInit::LISTCONCAT;
939 // We don't know the list type until we parse the first argument
940 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000941 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000942 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000943 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000944 break;
David Greene5d0c0512009-05-14 20:54:48 +0000945 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000946
David Greene5d0c0512009-05-14 20:54:48 +0000947 if (Lex.getCode() != tgtok::l_paren) {
948 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000949 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000950 }
951 Lex.Lex(); // eat the '('
952
David Greeneaf8ee2c2011-07-29 22:43:06 +0000953 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000954
Chris Lattner61ea00b2010-10-05 23:58:18 +0000955 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000956 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000957
Chris Lattner61ea00b2010-10-05 23:58:18 +0000958 while (Lex.getCode() == tgtok::comma) {
959 Lex.Lex(); // eat the ','
960
961 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000962 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000963 }
David Greene5d0c0512009-05-14 20:54:48 +0000964
965 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000966 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000967 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000968 }
969 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000970
Daniel Sanders314e80e2014-05-07 10:13:19 +0000971 // If we are doing !listconcat, we should know the type by now
972 if (OpTok == tgtok::XListConcat) {
973 if (VarInit *Arg0 = dyn_cast<VarInit>(InitList[0]))
974 Type = Arg0->getType();
975 else if (ListInit *Arg0 = dyn_cast<ListInit>(InitList[0]))
976 Type = Arg0->getType();
977 else {
978 InitList[0]->dump();
979 Error(OpLoc, "expected a list");
980 return nullptr;
981 }
982 }
983
Chris Lattner61ea00b2010-10-05 23:58:18 +0000984 // We allow multiple operands to associative operators like !strconcat as
985 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000986 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000987 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000988 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000989 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
990 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000991 InitList.back() = RHS;
992 }
993 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000994
Chris Lattner61ea00b2010-10-05 23:58:18 +0000995 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000996 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000997 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000998
Chris Lattner61ea00b2010-10-05 23:58:18 +0000999 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +00001000 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001001 }
1002
David Greene3587eed2009-05-14 23:26:46 +00001003 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001004 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001005 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
1006 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +00001007 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001008
David Greene98ed3c72009-05-14 21:54:42 +00001009 tgtok::TokKind LexCode = Lex.getCode();
1010 Lex.Lex(); // eat the operation
1011 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001012 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001013 case tgtok::XIf:
1014 Code = TernOpInit::IF;
1015 break;
David Greenee917fff2009-05-14 22:23:47 +00001016 case tgtok::XForEach:
1017 Code = TernOpInit::FOREACH;
1018 break;
David Greene98ed3c72009-05-14 21:54:42 +00001019 case tgtok::XSubst:
1020 Code = TernOpInit::SUBST;
1021 break;
1022 }
1023 if (Lex.getCode() != tgtok::l_paren) {
1024 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001025 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001026 }
1027 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001028
David Greeneaf8ee2c2011-07-29 22:43:06 +00001029 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001030 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001031
David Greene98ed3c72009-05-14 21:54:42 +00001032 if (Lex.getCode() != tgtok::comma) {
1033 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001034 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001035 }
1036 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001037
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001038 Init *MHS = ParseValue(CurRec, ItemType);
1039 if (!MHS)
1040 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001041
David Greene98ed3c72009-05-14 21:54:42 +00001042 if (Lex.getCode() != tgtok::comma) {
1043 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001044 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001045 }
1046 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001047
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001048 Init *RHS = ParseValue(CurRec, ItemType);
1049 if (!RHS)
1050 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001051
David Greene98ed3c72009-05-14 21:54:42 +00001052 if (Lex.getCode() != tgtok::r_paren) {
1053 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001054 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001055 }
1056 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001057
David Greene98ed3c72009-05-14 21:54:42 +00001058 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001059 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001060 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001061 RecTy *MHSTy = nullptr;
1062 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001063
Sean Silvafb509ed2012-10-10 20:24:43 +00001064 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001065 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001066 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001067 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001068 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001069 MHSTy = BitRecTy::get();
1070
Sean Silvafb509ed2012-10-10 20:24:43 +00001071 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001072 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001073 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001074 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001075 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001076 RHSTy = BitRecTy::get();
1077
1078 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001079 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001080 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001081 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001082 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001083
1084 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001085 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001086 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001087 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001088
1089 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1090 Type = RHSTy;
1091 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1092 Type = MHSTy;
Bob Wilson7248f862009-11-22 04:24:42 +00001093 } else {
David Greene3587eed2009-05-14 23:26:46 +00001094 TokError("inconsistent types for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001095 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001096 }
1097 break;
1098 }
David Greenee917fff2009-05-14 22:23:47 +00001099 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001100 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
Craig Topper011817a2014-04-09 04:50:04 +00001101 if (!MHSt) {
David Greenee917fff2009-05-14 22:23:47 +00001102 TokError("could not get type for !foreach");
Craig Topper011817a2014-04-09 04:50:04 +00001103 return nullptr;
David Greenee917fff2009-05-14 22:23:47 +00001104 }
1105 Type = MHSt->getType();
1106 break;
1107 }
David Greene98ed3c72009-05-14 21:54:42 +00001108 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001109 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001110 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001111 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001112 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001113 }
1114 Type = RHSt->getType();
1115 break;
1116 }
1117 }
David Greenee32ebf22011-07-29 19:07:07 +00001118 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001119 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001120 }
David Greene5d0c0512009-05-14 20:54:48 +00001121 }
David Greene5d0c0512009-05-14 20:54:48 +00001122}
1123
1124/// ParseOperatorType - Parse a type for an operator. This returns
1125/// null on error.
1126///
1127/// OperatorType ::= '<' Type '>'
1128///
Dan Gohman1432ef82009-08-12 22:10:57 +00001129RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001130 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001131
1132 if (Lex.getCode() != tgtok::less) {
1133 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001134 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001135 }
1136 Lex.Lex(); // eat the <
1137
1138 Type = ParseType();
1139
Craig Topper011817a2014-04-09 04:50:04 +00001140 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001141 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001142 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001143 }
1144
1145 if (Lex.getCode() != tgtok::greater) {
1146 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001147 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001148 }
1149 Lex.Lex(); // eat the >
1150
1151 return Type;
1152}
1153
1154
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001155/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1156///
1157/// SimpleValue ::= IDValue
1158/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001159/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001160/// SimpleValue ::= CODEFRAGMENT
1161/// SimpleValue ::= '?'
1162/// SimpleValue ::= '{' ValueList '}'
1163/// SimpleValue ::= ID '<' ValueListNE '>'
1164/// SimpleValue ::= '[' ValueList ']'
1165/// SimpleValue ::= '(' IDValue DagArgList ')'
1166/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001167/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001168/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1169/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1170/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001171/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001172/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1173///
David Greened4263a62011-10-19 13:04:20 +00001174Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1175 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001176 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001177 switch (Lex.getCode()) {
1178 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001179 case tgtok::paste:
1180 // This is a leading paste operation. This is deprecated but
1181 // still exists in some .td files. Ignore it.
1182 Lex.Lex(); // Skip '#'.
1183 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001184 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001185 case tgtok::StrVal: {
1186 std::string Val = Lex.getCurStrVal();
1187 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001188
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001189 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001190 while (Lex.getCode() == tgtok::StrVal) {
1191 Val += Lex.getCurStrVal();
1192 Lex.Lex();
1193 }
Bob Wilson7248f862009-11-22 04:24:42 +00001194
David Greenee32ebf22011-07-29 19:07:07 +00001195 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001196 break;
1197 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001198 case tgtok::CodeFragment:
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +00001199 R = StringInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001200 Lex.Lex();
1201 break;
1202 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001203 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001204 Lex.Lex();
1205 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001206 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001207 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001208 std::string Name = Lex.getCurStrVal();
1209 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001210 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001211
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001212 // Value ::= ID '<' ValueListNE '>'
1213 if (Lex.Lex() == tgtok::greater) {
1214 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001215 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001216 }
David Greene8618f952009-06-08 20:23:18 +00001217
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001218 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1219 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1220 // body.
1221 Record *Class = Records.getClass(Name);
1222 if (!Class) {
1223 Error(NameLoc, "Expected a class name, got '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001224 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001225 }
David Greene8618f952009-06-08 20:23:18 +00001226
David Greeneaf8ee2c2011-07-29 22:43:06 +00001227 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
Craig Topper011817a2014-04-09 04:50:04 +00001228 if (ValueList.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001229
David Greene8618f952009-06-08 20:23:18 +00001230 if (Lex.getCode() != tgtok::greater) {
1231 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001232 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001233 }
1234 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001235 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001236
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001237 // Create the new record, set it as CurRec temporarily.
Alp Tokerce91fe52013-12-21 18:51:00 +00001238 Record *NewRec = new Record(GetNewAnonymousName(), NameLoc, Records,
Jordan Roseabdd99b2013-01-10 18:50:05 +00001239 /*IsAnonymous=*/true);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001240 SubClassReference SCRef;
Jordan Rosef12e8a92013-01-10 18:50:11 +00001241 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001242 SCRef.Rec = Class;
1243 SCRef.TemplateArgs = ValueList;
1244 // Add info about the subclass to NewRec.
1245 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001246 return nullptr;
Hal Finkela8c1f462014-01-02 20:47:09 +00001247 if (!CurMultiClass) {
1248 NewRec->resolveReferences();
1249 Records.addDef(NewRec);
1250 } else {
1251 // Otherwise, we're inside a multiclass, add it to the multiclass.
1252 CurMultiClass->DefPrototypes.push_back(NewRec);
1253
1254 // Copy the template arguments for the multiclass into the def.
1255 const std::vector<Init *> &TArgs =
1256 CurMultiClass->Rec.getTemplateArgs();
1257
1258 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1259 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1260 assert(RV && "Template arg doesn't exist?");
1261 NewRec->addValue(*RV);
1262 }
1263
1264 // We can't return the prototype def here, instead return:
1265 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1266 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1267 assert(MCNameRV && "multiclass record must have a NAME");
1268
1269 return UnOpInit::get(UnOpInit::CAST,
1270 BinOpInit::get(BinOpInit::STRCONCAT,
1271 VarInit::get(MCNameRV->getName(),
1272 MCNameRV->getType()),
1273 NewRec->getNameInit(),
1274 StringRecTy::get()),
1275 Class->getDefInit()->getType());
1276 }
Bob Wilson7248f862009-11-22 04:24:42 +00001277
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001278 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001279 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001280 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001281 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001282 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001283 Lex.Lex(); // eat the '{'
David Greeneaf8ee2c2011-07-29 22:43:06 +00001284 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001285
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001286 if (Lex.getCode() != tgtok::r_brace) {
1287 Vals = ParseValueList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001288 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001289 }
1290 if (Lex.getCode() != tgtok::r_brace) {
1291 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001292 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001293 }
1294 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001295
David Greeneaf8ee2c2011-07-29 22:43:06 +00001296 SmallVector<Init *, 16> NewBits(Vals.size());
David Greeneb3da8122011-07-29 19:07:00 +00001297
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001298 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001299 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001300 if (!Bit) {
Chris Lattner52416952007-11-22 21:06:59 +00001301 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1302 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001303 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001304 }
David Greeneb3da8122011-07-29 19:07:00 +00001305 NewBits[Vals.size()-i-1] = Bit;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001306 }
David Greenee32ebf22011-07-29 19:07:07 +00001307 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001308 }
1309 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1310 Lex.Lex(); // eat the '['
David Greeneaf8ee2c2011-07-29 22:43:06 +00001311 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001312
Craig Topper011817a2014-04-09 04:50:04 +00001313 RecTy *DeducedEltTy = nullptr;
1314 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001315
Craig Topper011817a2014-04-09 04:50:04 +00001316 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001317 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001318 if (!ListType) {
Alp Tokere69170a2014-06-26 22:52:05 +00001319 std::string s;
1320 raw_string_ostream ss(s);
Reid Klecknerd78273f2013-08-06 22:51:21 +00001321 ss << "Type mismatch for list, expected list type, got "
1322 << ItemType->getAsString();
1323 TokError(ss.str());
Craig Topper011817a2014-04-09 04:50:04 +00001324 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001325 }
1326 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001327 }
David Greene8618f952009-06-08 20:23:18 +00001328
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001329 if (Lex.getCode() != tgtok::r_square) {
Craig Topper011817a2014-04-09 04:50:04 +00001330 Vals = ParseValueList(CurRec, nullptr,
1331 GivenListTy ? GivenListTy->getElementType() : nullptr);
1332 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001333 }
1334 if (Lex.getCode() != tgtok::r_square) {
1335 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001336 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001337 }
1338 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001339
Craig Topper011817a2014-04-09 04:50:04 +00001340 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001341 if (Lex.getCode() == tgtok::less) {
1342 // Optional list element type
1343 Lex.Lex(); // eat the '<'
1344
1345 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001346 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001347 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001348 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001349 }
1350
1351 if (Lex.getCode() != tgtok::greater) {
1352 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001353 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001354 }
1355 Lex.Lex(); // eat the '>'
1356 }
1357
1358 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001359 RecTy *EltTy = nullptr;
David Greeneaf8ee2c2011-07-29 22:43:06 +00001360 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greene8618f952009-06-08 20:23:18 +00001361 i != ie;
1362 ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +00001363 TypedInit *TArg = dyn_cast<TypedInit>(*i);
Craig Topper011817a2014-04-09 04:50:04 +00001364 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001365 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001366 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001367 }
Craig Topper011817a2014-04-09 04:50:04 +00001368 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001369 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001370 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001371 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001372 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001373 }
Bob Wilson7248f862009-11-22 04:24:42 +00001374 } else {
David Greene8618f952009-06-08 20:23:18 +00001375 EltTy = TArg->getType();
1376 }
1377 }
1378
Craig Topper011817a2014-04-09 04:50:04 +00001379 if (GivenEltTy) {
1380 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001381 // Verify consistency
1382 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1383 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001384 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001385 }
1386 }
1387 EltTy = GivenEltTy;
1388 }
1389
Craig Topper011817a2014-04-09 04:50:04 +00001390 if (!EltTy) {
1391 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001392 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001393 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001394 }
1395 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001396 } else {
David Greene8618f952009-06-08 20:23:18 +00001397 // Make sure the deduced type is compatible with the given type
1398 if (GivenListTy) {
1399 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1400 TokError("Element type mismatch for list");
Craig Topper011817a2014-04-09 04:50:04 +00001401 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001402 }
1403 }
1404 DeducedEltTy = EltTy;
1405 }
Bob Wilson7248f862009-11-22 04:24:42 +00001406
David Greenee32ebf22011-07-29 19:07:07 +00001407 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001408 }
1409 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1410 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001411 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001412 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001413 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001414 }
Bob Wilson7248f862009-11-22 04:24:42 +00001415
David Greeneaf8ee2c2011-07-29 22:43:06 +00001416 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001417 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001418
Nate Begemandbe3f772009-03-19 05:21:56 +00001419 // If the operator name is present, parse it.
1420 std::string OperatorName;
1421 if (Lex.getCode() == tgtok::colon) {
1422 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1423 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001424 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001425 }
1426 OperatorName = Lex.getCurStrVal();
1427 Lex.Lex(); // eat the VarName.
1428 }
Bob Wilson7248f862009-11-22 04:24:42 +00001429
David Greeneaf8ee2c2011-07-29 22:43:06 +00001430 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001431 if (Lex.getCode() != tgtok::r_paren) {
1432 DagArgs = ParseDagArgList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001433 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001434 }
Bob Wilson7248f862009-11-22 04:24:42 +00001435
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001436 if (Lex.getCode() != tgtok::r_paren) {
1437 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001438 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001439 }
1440 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001441
David Greenee32ebf22011-07-29 19:07:07 +00001442 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001443 }
Bob Wilson7248f862009-11-22 04:24:42 +00001444
David Greene2f7cf7f2011-01-07 17:05:37 +00001445 case tgtok::XHead:
1446 case tgtok::XTail:
1447 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001448 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001449 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001450 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001451 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +00001452 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001453 case tgtok::XSRL:
1454 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001455 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001456 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001457 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001458 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001459 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001460 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001461 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001462 }
1463 }
Bob Wilson7248f862009-11-22 04:24:42 +00001464
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001465 return R;
1466}
1467
1468/// ParseValue - Parse a tblgen value. This returns null on error.
1469///
1470/// Value ::= SimpleValue ValueSuffix*
1471/// ValueSuffix ::= '{' BitList '}'
1472/// ValueSuffix ::= '[' BitList ']'
1473/// ValueSuffix ::= '.' ID
1474///
David Greened4263a62011-10-19 13:04:20 +00001475Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1476 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001477 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001478
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001479 // Parse the suffixes now if present.
1480 while (1) {
1481 switch (Lex.getCode()) {
1482 default: return Result;
1483 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001484 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001485 // This is the beginning of the object body.
1486 return Result;
1487
Chris Lattner526c8cb2009-06-21 03:39:35 +00001488 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001489 Lex.Lex(); // eat the '{'
1490 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001491 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001492
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001493 // Reverse the bitlist.
1494 std::reverse(Ranges.begin(), Ranges.end());
1495 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001496 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001497 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001498 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001499 }
Bob Wilson7248f862009-11-22 04:24:42 +00001500
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001501 // Eat the '}'.
1502 if (Lex.getCode() != tgtok::r_brace) {
1503 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001504 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001505 }
1506 Lex.Lex();
1507 break;
1508 }
1509 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001510 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001511 Lex.Lex(); // eat the '['
1512 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001513 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001514
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001515 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001516 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001517 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001518 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001519 }
Bob Wilson7248f862009-11-22 04:24:42 +00001520
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001521 // Eat the ']'.
1522 if (Lex.getCode() != tgtok::r_square) {
1523 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001524 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001525 }
1526 Lex.Lex();
1527 break;
1528 }
1529 case tgtok::period:
1530 if (Lex.Lex() != tgtok::Id) { // eat the .
1531 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001532 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001533 }
1534 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001535 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001536 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001537 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001538 }
David Greenee32ebf22011-07-29 19:07:07 +00001539 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001540 Lex.Lex(); // eat field name
1541 break;
David Greene8e85b482011-10-19 13:04:43 +00001542
1543 case tgtok::paste:
1544 SMLoc PasteLoc = Lex.getLoc();
1545
1546 // Create a !strconcat() operation, first casting each operand to
1547 // a string if necessary.
1548
Sean Silvafb509ed2012-10-10 20:24:43 +00001549 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001550 if (!LHS) {
1551 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001552 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001553 }
1554
1555 if (LHS->getType() != StringRecTy::get()) {
1556 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1557 }
1558
Craig Topper011817a2014-04-09 04:50:04 +00001559 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001560
1561 Lex.Lex(); // Eat the '#'.
1562 switch (Lex.getCode()) {
1563 case tgtok::colon:
1564 case tgtok::semi:
1565 case tgtok::l_brace:
1566 // These are all of the tokens that can begin an object body.
1567 // Some of these can also begin values but we disallow those cases
1568 // because they are unlikely to be useful.
1569
1570 // Trailing paste, concat with an empty string.
1571 RHS = StringInit::get("");
1572 break;
1573
1574 default:
1575 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001576 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001577 if (!RHS) {
1578 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001579 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001580 }
1581
1582 if (RHS->getType() != StringRecTy::get()) {
1583 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1584 }
1585
1586 break;
1587 }
1588
1589 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1590 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1591 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001592 }
1593 }
1594}
1595
1596/// ParseDagArgList - Parse the argument list for a dag literal expression.
1597///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001598/// DagArg ::= Value (':' VARNAME)?
1599/// DagArg ::= VARNAME
1600/// DagArgList ::= DagArg
1601/// DagArgList ::= DagArgList ',' DagArg
David Greeneaf8ee2c2011-07-29 22:43:06 +00001602std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001603TGParser::ParseDagArgList(Record *CurRec) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001604 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson7248f862009-11-22 04:24:42 +00001605
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001606 while (1) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001607 // DagArg ::= VARNAME
1608 if (Lex.getCode() == tgtok::VarName) {
1609 // A missing value is treated like '?'.
1610 Result.push_back(std::make_pair(UnsetInit::get(), Lex.getCurStrVal()));
1611 Lex.Lex();
1612 } else {
1613 // DagArg ::= Value (':' VARNAME)?
1614 Init *Val = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001615 if (!Val)
David Greeneaf8ee2c2011-07-29 22:43:06 +00001616 return std::vector<std::pair<llvm::Init*, std::string> >();
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001617
1618 // If the variable name is present, add it.
1619 std::string VarName;
1620 if (Lex.getCode() == tgtok::colon) {
1621 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1622 TokError("expected variable name in dag literal");
1623 return std::vector<std::pair<llvm::Init*, std::string> >();
1624 }
1625 VarName = Lex.getCurStrVal();
1626 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001627 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001628
1629 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001630 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001631 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001632 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001633 }
Bob Wilson7248f862009-11-22 04:24:42 +00001634
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001635 return Result;
1636}
1637
1638
1639/// ParseValueList - Parse a comma separated list of values, returning them as a
1640/// vector. Note that this always expects to be able to parse at least one
1641/// value. It returns an empty list if this is not possible.
1642///
1643/// ValueList ::= Value (',' Value)
1644///
David Greeneaf8ee2c2011-07-29 22:43:06 +00001645std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopher71520a82011-07-11 23:06:52 +00001646 RecTy *EltTy) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001647 std::vector<Init*> Result;
David Greene8618f952009-06-08 20:23:18 +00001648 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001649 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001650 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001651 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001652 if (!TArgs.size()) {
1653 TokError("template argument provided to non-template class");
1654 return std::vector<Init*>();
1655 }
David Greene8618f952009-06-08 20:23:18 +00001656 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001657 if (!RV) {
1658 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1659 << ")\n";
1660 }
David Greene8618f952009-06-08 20:23:18 +00001661 assert(RV && "Template argument record not found??");
1662 ItemType = RV->getType();
1663 ++ArgN;
1664 }
1665 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001666 if (!Result.back()) return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001667
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001668 while (Lex.getCode() == tgtok::comma) {
1669 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001670
Craig Topper011817a2014-04-09 04:50:04 +00001671 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001672 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001673 if (ArgN >= TArgs.size()) {
1674 TokError("too many template arguments");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001675 return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001676 }
David Greene8618f952009-06-08 20:23:18 +00001677 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1678 assert(RV && "Template argument record not found??");
1679 ItemType = RV->getType();
1680 ++ArgN;
1681 }
1682 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001683 if (!Result.back()) return std::vector<Init*>();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001684 }
Bob Wilson7248f862009-11-22 04:24:42 +00001685
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001686 return Result;
1687}
1688
1689
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001690/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1691/// empty string on error. This can happen in a number of different context's,
1692/// including within a def or in the template args for a def (which which case
1693/// CurRec will be non-null) and within the template args for a multiclass (in
1694/// which case CurRec will be null, but CurMultiClass will be set). This can
1695/// also happen within a def that is within a multiclass, which will set both
1696/// CurRec and CurMultiClass.
1697///
1698/// Declaration ::= FIELD? Type ID ('=' Value)?
1699///
David Greenedb10e692011-10-19 13:02:42 +00001700Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001701 bool ParsingTemplateArgs) {
1702 // Read the field prefix if present.
1703 bool HasField = Lex.getCode() == tgtok::Field;
1704 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001705
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001706 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001707 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001708
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001709 if (Lex.getCode() != tgtok::Id) {
1710 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001711 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001712 }
Bob Wilson7248f862009-11-22 04:24:42 +00001713
Chris Lattner526c8cb2009-06-21 03:39:35 +00001714 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001715 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001716 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001717
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001718 if (ParsingTemplateArgs) {
1719 if (CurRec) {
David Greenedb10e692011-10-19 13:02:42 +00001720 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001721 } else {
1722 assert(CurMultiClass);
1723 }
1724 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001725 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1726 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001727 }
Bob Wilson7248f862009-11-22 04:24:42 +00001728
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001729 // Add the value.
1730 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001731 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001732
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001733 // If a value is present, parse it.
1734 if (Lex.getCode() == tgtok::equal) {
1735 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001736 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001737 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001738 if (!Val ||
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001739 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001740 // Return the name, even if an error is thrown. This is so that we can
1741 // continue to make some progress, even without the value having been
1742 // initialized.
1743 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001744 }
Bob Wilson7248f862009-11-22 04:24:42 +00001745
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001746 return DeclName;
1747}
1748
David Greenefb927af2012-02-22 16:09:41 +00001749/// ParseForeachDeclaration - Read a foreach declaration, returning
1750/// the name of the declared object or a NULL Init on error. Return
1751/// the name of the parsed initializer list through ForeachListName.
1752///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001753/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1754/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1755/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001756///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001757VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001758 if (Lex.getCode() != tgtok::Id) {
1759 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001760 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001761 }
1762
1763 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1764 Lex.Lex();
1765
1766 // If a value is present, parse it.
1767 if (Lex.getCode() != tgtok::equal) {
1768 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001769 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001770 }
1771 Lex.Lex(); // Eat the '='
1772
Craig Topper011817a2014-04-09 04:50:04 +00001773 RecTy *IterType = nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001774 std::vector<unsigned> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001775
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001776 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001777 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001778 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001779 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001780 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001781 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001782 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001783 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001784 }
1785 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001786 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001787 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001788 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001789 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001790 }
1791 IterType = ListType->getElementType();
1792 break;
David Greenefb927af2012-02-22 16:09:41 +00001793 }
1794
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001795 case tgtok::IntVal: { // RangePiece.
1796 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001797 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001798 break;
David Greenefb927af2012-02-22 16:09:41 +00001799 }
1800
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001801 case tgtok::l_brace: { // '{' RangeList '}'
1802 Lex.Lex(); // eat the '{'
1803 Ranges = ParseRangeList();
1804 if (Lex.getCode() != tgtok::r_brace) {
1805 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001806 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001807 }
1808 Lex.Lex();
1809 break;
1810 }
1811 }
David Greenefb927af2012-02-22 16:09:41 +00001812
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001813 if (!Ranges.empty()) {
1814 assert(!IterType && "Type already initialized?");
1815 IterType = IntRecTy::get();
1816 std::vector<Init*> Values;
1817 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
1818 Values.push_back(IntInit::get(Ranges[i]));
1819 ForeachListValue = ListInit::get(Values, IterType);
1820 }
1821
1822 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001823 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001824
1825 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001826}
1827
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001828/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1829/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1830/// template args for a def, which may or may not be in a multiclass. If null,
1831/// these are the template args for a multiclass.
1832///
1833/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001834///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001835bool TGParser::ParseTemplateArgList(Record *CurRec) {
1836 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1837 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001838
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001839 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001840
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001841 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001842 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001843 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001844 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001845
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001846 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001847
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001848 while (Lex.getCode() == tgtok::comma) {
1849 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001850
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001851 // Read the following declarations.
1852 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001853 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001854 return true;
1855 TheRecToAddTo->addTemplateArg(TemplArg);
1856 }
Bob Wilson7248f862009-11-22 04:24:42 +00001857
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001858 if (Lex.getCode() != tgtok::greater)
1859 return TokError("expected '>' at end of template argument list");
1860 Lex.Lex(); // eat the '>'.
1861 return false;
1862}
1863
1864
1865/// ParseBodyItem - Parse a single item at within the body of a def or class.
1866///
1867/// BodyItem ::= Declaration ';'
1868/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1869bool TGParser::ParseBodyItem(Record *CurRec) {
1870 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001871 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001872 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001873
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001874 if (Lex.getCode() != tgtok::semi)
1875 return TokError("expected ';' after declaration");
1876 Lex.Lex();
1877 return false;
1878 }
1879
1880 // LET ID OptionalRangeList '=' Value ';'
1881 if (Lex.Lex() != tgtok::Id)
1882 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001883
Chris Lattner526c8cb2009-06-21 03:39:35 +00001884 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001885 std::string FieldName = Lex.getCurStrVal();
1886 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001887
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001888 std::vector<unsigned> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001889 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001890 return true;
1891 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001892
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001893 if (Lex.getCode() != tgtok::equal)
1894 return TokError("expected '=' in let expression");
1895 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001896
David Greene8618f952009-06-08 20:23:18 +00001897 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001898 if (!Field)
David Greene8618f952009-06-08 20:23:18 +00001899 return TokError("Value '" + FieldName + "' unknown!");
1900
1901 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001902
David Greeneaf8ee2c2011-07-29 22:43:06 +00001903 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001904 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001905
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001906 if (Lex.getCode() != tgtok::semi)
1907 return TokError("expected ';' after let expression");
1908 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001909
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001910 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1911}
1912
1913/// ParseBody - Read the body of a class or def. Return true on error, false on
1914/// success.
1915///
1916/// Body ::= ';'
1917/// Body ::= '{' BodyList '}'
1918/// BodyList BodyItem*
1919///
1920bool TGParser::ParseBody(Record *CurRec) {
1921 // If this is a null definition, just eat the semi and return.
1922 if (Lex.getCode() == tgtok::semi) {
1923 Lex.Lex();
1924 return false;
1925 }
Bob Wilson7248f862009-11-22 04:24:42 +00001926
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001927 if (Lex.getCode() != tgtok::l_brace)
1928 return TokError("Expected ';' or '{' to start body");
1929 // Eat the '{'.
1930 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001931
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001932 while (Lex.getCode() != tgtok::r_brace)
1933 if (ParseBodyItem(CurRec))
1934 return true;
1935
1936 // Eat the '}'.
1937 Lex.Lex();
1938 return false;
1939}
1940
Sean Silvacb1a75e2013-01-09 04:49:14 +00001941/// \brief Apply the current let bindings to \a CurRec.
1942/// \returns true on error, false otherwise.
1943bool TGParser::ApplyLetStack(Record *CurRec) {
1944 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1945 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1946 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1947 LetStack[i][j].Bits, LetStack[i][j].Value))
1948 return true;
1949 return false;
1950}
1951
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001952/// ParseObjectBody - Parse the body of a def or class. This consists of an
1953/// optional ClassList followed by a Body. CurRec is the current def or class
1954/// that is being parsed.
1955///
1956/// ObjectBody ::= BaseClassList Body
1957/// BaseClassList ::= /*empty*/
1958/// BaseClassList ::= ':' BaseClassListNE
1959/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1960///
1961bool TGParser::ParseObjectBody(Record *CurRec) {
1962 // If there is a baseclass list, read it.
1963 if (Lex.getCode() == tgtok::colon) {
1964 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001965
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001966 // Read all of the subclasses.
1967 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1968 while (1) {
1969 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00001970 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001971
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001972 // Add it.
1973 if (AddSubClass(CurRec, SubClass))
1974 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001975
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001976 if (Lex.getCode() != tgtok::comma) break;
1977 Lex.Lex(); // eat ','.
1978 SubClass = ParseSubClassReference(CurRec, false);
1979 }
1980 }
1981
Sean Silvacb1a75e2013-01-09 04:49:14 +00001982 if (ApplyLetStack(CurRec))
1983 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001984
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001985 return ParseBody(CurRec);
1986}
1987
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001988/// ParseDef - Parse and return a top level or multiclass def, return the record
1989/// corresponding to it. This returns null on error.
1990///
1991/// DefInst ::= DEF ObjectName ObjectBody
1992///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00001993bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001994 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001995 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00001996 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001997
1998 // Parse ObjectName and make a record for it.
Jordan Roseabdd99b2013-01-10 18:50:05 +00001999 Record *CurRec;
2000 Init *Name = ParseObjectName(CurMultiClass);
2001 if (Name)
2002 CurRec = new Record(Name, DefLoc, Records);
2003 else
2004 CurRec = new Record(GetNewAnonymousName(), DefLoc, Records,
2005 /*IsAnonymous=*/true);
Bob Wilson7248f862009-11-22 04:24:42 +00002006
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002007 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002008 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002009
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002010 // Ensure redefinition doesn't happen.
David Greeneebb006f2012-01-28 00:03:24 +00002011 if (Records.getDef(CurRec->getNameInitAsString())) {
David Greene3a20f5a2011-10-19 13:03:45 +00002012 Error(DefLoc, "def '" + CurRec->getNameInitAsString()
2013 + "' already defined");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002014 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002015 }
2016 Records.addDef(CurRec);
Hal Finkela8c1f462014-01-02 20:47:09 +00002017
2018 if (ParseObjectBody(CurRec))
2019 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002020 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002021 // Parse the body before adding this prototype to the DefPrototypes vector.
2022 // That way implicit definitions will be added to the DefPrototypes vector
2023 // before this object, instantiated prior to defs derived from this object,
2024 // and this available for indirect name resolution when defs derived from
2025 // this object are instantiated.
2026 if (ParseObjectBody(CurRec))
2027 return true;
2028
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002029 // Otherwise, a def inside a multiclass, add it to the multiclass.
2030 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene07e055f2011-10-19 13:03:51 +00002031 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2032 == CurRec->getNameInit()) {
2033 Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002034 "' already defined in this multiclass!");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002035 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002036 }
2037 CurMultiClass->DefPrototypes.push_back(CurRec);
Hal Finkela8c1f462014-01-02 20:47:09 +00002038 } else if (ParseObjectBody(CurRec))
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002039 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002040
Craig Topper011817a2014-04-09 04:50:04 +00002041 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002042 // See Record::setName(). This resolve step will see any new name
2043 // for the def that might have been created when resolving
2044 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002045 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002046
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002047 // If ObjectBody has template arguments, it's an error.
2048 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002049
2050 if (CurMultiClass) {
2051 // Copy the template arguments for the multiclass into the def.
David Greenedb10e692011-10-19 13:02:42 +00002052 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002053 CurMultiClass->Rec.getTemplateArgs();
2054
2055 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2056 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
2057 assert(RV && "Template arg doesn't exist?");
2058 CurRec->addValue(*RV);
2059 }
2060 }
2061
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002062 if (ProcessForeachDefs(CurRec, DefLoc)) {
David Greenefb927af2012-02-22 16:09:41 +00002063 Error(DefLoc,
2064 "Could not process loops for def" + CurRec->getNameInitAsString());
2065 return true;
2066 }
2067
2068 return false;
2069}
2070
2071/// ParseForeach - Parse a for statement. Return the record corresponding
2072/// to it. This returns true on error.
2073///
2074/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2075/// Foreach ::= FOREACH Declaration IN Object
2076///
2077bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2078 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2079 Lex.Lex(); // Eat the 'for' token.
2080
2081 // Make a temporary object to record items associated with the for
2082 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002083 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002084 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002085 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002086 return TokError("expected declaration in for");
2087
2088 if (Lex.getCode() != tgtok::In)
2089 return TokError("Unknown tok");
2090 Lex.Lex(); // Eat the in
2091
2092 // Create a loop object and remember it.
2093 Loops.push_back(ForeachLoop(IterName, ListValue));
2094
2095 if (Lex.getCode() != tgtok::l_brace) {
2096 // FOREACH Declaration IN Object
2097 if (ParseObject(CurMultiClass))
2098 return true;
2099 }
2100 else {
2101 SMLoc BraceLoc = Lex.getLoc();
2102 // Otherwise, this is a group foreach.
2103 Lex.Lex(); // eat the '{'.
2104
2105 // Parse the object list.
2106 if (ParseObjectList(CurMultiClass))
2107 return true;
2108
2109 if (Lex.getCode() != tgtok::r_brace) {
2110 TokError("expected '}' at end of foreach command");
2111 return Error(BraceLoc, "to match this '{'");
2112 }
2113 Lex.Lex(); // Eat the }
2114 }
2115
2116 // We've processed everything in this loop.
2117 Loops.pop_back();
2118
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002119 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002120}
2121
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002122/// ParseClass - Parse a tblgen class definition.
2123///
2124/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2125///
2126bool TGParser::ParseClass() {
2127 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2128 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002129
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002130 if (Lex.getCode() != tgtok::Id)
2131 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002132
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002133 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2134 if (CurRec) {
2135 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002136 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002137 !CurRec->getSuperClasses().empty() ||
2138 !CurRec->getTemplateArgs().empty())
David Greene8eed9982011-10-19 13:03:58 +00002139 return TokError("Class '" + CurRec->getNameInitAsString()
2140 + "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002141 } else {
2142 // If this is the first reference to this class, create and add it.
Chris Lattner89dcb682010-12-15 04:48:22 +00002143 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002144 Records.addClass(CurRec);
2145 }
2146 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002147
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002148 // If there are template args, parse them.
2149 if (Lex.getCode() == tgtok::less)
2150 if (ParseTemplateArgList(CurRec))
2151 return true;
2152
2153 // Finally, parse the object body.
2154 return ParseObjectBody(CurRec);
2155}
2156
2157/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2158/// of LetRecords.
2159///
2160/// LetList ::= LetItem (',' LetItem)*
2161/// LetItem ::= ID OptionalRangeList '=' Value
2162///
2163std::vector<LetRecord> TGParser::ParseLetList() {
2164 std::vector<LetRecord> Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002165
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002166 while (1) {
2167 if (Lex.getCode() != tgtok::Id) {
2168 TokError("expected identifier in let definition");
2169 return std::vector<LetRecord>();
2170 }
2171 std::string Name = Lex.getCurStrVal();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002172 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002173 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002174
2175 // Check for an optional RangeList.
2176 std::vector<unsigned> Bits;
Bob Wilson7248f862009-11-22 04:24:42 +00002177 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002178 return std::vector<LetRecord>();
2179 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002180
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002181 if (Lex.getCode() != tgtok::equal) {
2182 TokError("expected '=' in let expression");
2183 return std::vector<LetRecord>();
2184 }
2185 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002186
Craig Topper011817a2014-04-09 04:50:04 +00002187 Init *Val = ParseValue(nullptr);
2188 if (!Val) return std::vector<LetRecord>();
Bob Wilson7248f862009-11-22 04:24:42 +00002189
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002190 // Now that we have everything, add the record.
2191 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson7248f862009-11-22 04:24:42 +00002192
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002193 if (Lex.getCode() != tgtok::comma)
2194 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002195 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002196 }
2197}
2198
2199/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002200/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002201///
2202/// Object ::= LET LetList IN '{' ObjectList '}'
2203/// Object ::= LET LetList IN Object
2204///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002205bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002206 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2207 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002208
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002209 // Add this entry to the let stack.
2210 std::vector<LetRecord> LetInfo = ParseLetList();
2211 if (LetInfo.empty()) return true;
2212 LetStack.push_back(LetInfo);
2213
2214 if (Lex.getCode() != tgtok::In)
2215 return TokError("expected 'in' at end of top-level 'let'");
2216 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002217
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002218 // If this is a scalar let, just handle it now
2219 if (Lex.getCode() != tgtok::l_brace) {
2220 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002221 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002222 return true;
2223 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002224 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002225 // Otherwise, this is a group let.
2226 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002227
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002228 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002229 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002230 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002231
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002232 if (Lex.getCode() != tgtok::r_brace) {
2233 TokError("expected '}' at end of top level let command");
2234 return Error(BraceLoc, "to match this '{'");
2235 }
2236 Lex.Lex();
2237 }
Bob Wilson7248f862009-11-22 04:24:42 +00002238
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002239 // Outside this let scope, this let block is not active.
2240 LetStack.pop_back();
2241 return false;
2242}
2243
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002244/// ParseMultiClass - Parse a multiclass definition.
2245///
Bob Wilson3d948162009-04-28 19:41:44 +00002246/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002247/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2248/// MultiClassObject ::= DefInst
2249/// MultiClassObject ::= MultiClassInst
2250/// MultiClassObject ::= DefMInst
2251/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2252/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002253///
2254bool TGParser::ParseMultiClass() {
2255 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2256 Lex.Lex(); // Eat the multiclass token.
2257
2258 if (Lex.getCode() != tgtok::Id)
2259 return TokError("expected identifier after multiclass for name");
2260 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002261
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002262 if (MultiClasses.count(Name))
2263 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002264
Chris Lattner77d369c2010-12-13 00:23:57 +00002265 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
2266 Lex.getLoc(), Records);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002267 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002268
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002269 // If there are template args, parse them.
2270 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002271 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002272 return true;
2273
David Greene7049e792009-04-24 16:55:41 +00002274 bool inherits = false;
2275
David Greene753ed8f2009-04-22 16:42:54 +00002276 // If there are submulticlasses, parse them.
2277 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002278 inherits = true;
2279
David Greene753ed8f2009-04-22 16:42:54 +00002280 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002281
David Greene753ed8f2009-04-22 16:42:54 +00002282 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002283 SubMultiClassReference SubMultiClass =
2284 ParseSubMultiClassReference(CurMultiClass);
David Greene753ed8f2009-04-22 16:42:54 +00002285 while (1) {
2286 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002287 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002288
David Greene753ed8f2009-04-22 16:42:54 +00002289 // Add it.
2290 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2291 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002292
David Greene753ed8f2009-04-22 16:42:54 +00002293 if (Lex.getCode() != tgtok::comma) break;
2294 Lex.Lex(); // eat ','.
2295 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2296 }
2297 }
2298
David Greene7049e792009-04-24 16:55:41 +00002299 if (Lex.getCode() != tgtok::l_brace) {
2300 if (!inherits)
2301 return TokError("expected '{' in multiclass definition");
Bob Wilson7248f862009-11-22 04:24:42 +00002302 else if (Lex.getCode() != tgtok::semi)
2303 return TokError("expected ';' in multiclass definition");
David Greene7049e792009-04-24 16:55:41 +00002304 else
Bob Wilson7248f862009-11-22 04:24:42 +00002305 Lex.Lex(); // eat the ';'.
2306 } else {
David Greene7049e792009-04-24 16:55:41 +00002307 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2308 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002309
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002310 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002311 switch (Lex.getCode()) {
2312 default:
David Greene33f61992011-10-07 18:25:05 +00002313 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002314 case tgtok::Let:
2315 case tgtok::Def:
2316 case tgtok::Defm:
David Greenefb927af2012-02-22 16:09:41 +00002317 case tgtok::Foreach:
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002318 if (ParseObject(CurMultiClass))
2319 return true;
2320 break;
2321 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002322 }
David Greene7049e792009-04-24 16:55:41 +00002323 Lex.Lex(); // eat the '}'.
2324 }
Bob Wilson7248f862009-11-22 04:24:42 +00002325
Craig Topper011817a2014-04-09 04:50:04 +00002326 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002327 return false;
2328}
2329
David Greenedb445972011-10-05 22:42:07 +00002330Record *TGParser::
2331InstantiateMulticlassDef(MultiClass &MC,
2332 Record *DefProto,
Hal Finkelf2a0b2b2014-01-02 19:35:33 +00002333 Init *&DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002334 SMRange DefmPrefixRange) {
David Greene5d5d88c2011-10-19 13:04:31 +00002335 // We need to preserve DefProto so it can be reused for later
2336 // instantiations, so create a new Record to inherit from it.
2337
David Greenedb445972011-10-05 22:42:07 +00002338 // Add in the defm name. If the defm prefix is empty, give each
2339 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2340 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2341 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002342
Jordan Roseabdd99b2013-01-10 18:50:05 +00002343 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002344 if (!DefmPrefix) {
David Greene5d5d88c2011-10-19 13:04:31 +00002345 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002346 IsAnonymous = true;
2347 }
David Greene5d5d88c2011-10-19 13:04:31 +00002348
2349 Init *DefName = DefProto->getNameInit();
2350
Sean Silvafb509ed2012-10-10 20:24:43 +00002351 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002352
Craig Topper011817a2014-04-09 04:50:04 +00002353 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002354 // We have a fully expanded string so there are no operators to
2355 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002356 DefName =
2357 BinOpInit::get(BinOpInit::STRCONCAT,
2358 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2359 StringRecTy::get())->Fold(DefProto, &MC),
2360 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2361 }
David Greene5d5d88c2011-10-19 13:04:31 +00002362
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002363 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002364 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002365 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002366 Record *CurRec = new Record(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002367
2368 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002369 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002370 Ref.Rec = DefProto;
2371 AddSubClass(CurRec, Ref);
2372
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002373 // Set the value for NAME. We don't resolve references to it 'til later,
2374 // though, so that uses in nested multiclass names don't get
2375 // confused.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002376 if (SetValue(CurRec, Ref.RefRange.Start, "NAME", std::vector<unsigned>(),
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002377 DefmPrefix)) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002378 Error(DefmPrefixRange.Start, "Could not resolve "
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002379 + CurRec->getNameInitAsString() + ":NAME to '"
2380 + DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002381 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002382 }
David Greene8bf0d722011-10-19 13:04:35 +00002383
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002384 // If the DefNameString didn't resolve, we probably have a reference to
2385 // NAME and need to replace it. We need to do at least this much greedily,
2386 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002387 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002388 RecordVal *DefNameRV = CurRec->getValue("NAME");
2389 CurRec->resolveReferencesTo(DefNameRV);
2390 }
2391
2392 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002393 // Now that we're at the top level, resolve all NAME references
2394 // in the resultant defs that weren't in the def names themselves.
2395 RecordVal *DefNameRV = CurRec->getValue("NAME");
2396 CurRec->resolveReferencesTo(DefNameRV);
2397
2398 // Now that NAME references are resolved and we're at the top level of
2399 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002400 // currently in a multiclass, it means this defm appears inside a
2401 // multiclass and its name won't be fully resolvable until we see
2402 // the top-level defm. Therefore, we don't add this to the
2403 // RecordKeeper at this point. If we did we could get duplicate
2404 // defs as more than one probably refers to NAME or some other
2405 // common internal placeholder.
2406
2407 // Ensure redefinition doesn't happen.
2408 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002409 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002410 "' already defined, instantiating defm with subdef '" +
2411 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002412 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002413 }
2414
2415 Records.addDef(CurRec);
2416 }
2417
David Greenedb445972011-10-05 22:42:07 +00002418 return CurRec;
2419}
2420
2421bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2422 Record *CurRec,
2423 SMLoc DefmPrefixLoc,
2424 SMLoc SubClassLoc,
David Greenedb10e692011-10-19 13:02:42 +00002425 const std::vector<Init *> &TArgs,
David Greenedb445972011-10-05 22:42:07 +00002426 std::vector<Init *> &TemplateVals,
2427 bool DeleteArgs) {
2428 // Loop over all of the template arguments, setting them to the specified
2429 // value or leaving them as the default if necessary.
2430 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2431 // Check if a value is specified for this temp-arg.
2432 if (i < TemplateVals.size()) {
2433 // Set it now.
2434 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2435 TemplateVals[i]))
2436 return true;
2437
2438 // Resolve it next.
2439 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2440
2441 if (DeleteArgs)
2442 // Now remove it.
2443 CurRec->removeValue(TArgs[i]);
2444
2445 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2446 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenedb10e692011-10-19 13:02:42 +00002447 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2448 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2449 + "'");
David Greenedb445972011-10-05 22:42:07 +00002450 }
2451 }
2452 return false;
2453}
2454
2455bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2456 Record *CurRec,
2457 Record *DefProto,
2458 SMLoc DefmPrefixLoc) {
2459 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002460 if (ApplyLetStack(CurRec))
2461 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002462
David Greenedb445972011-10-05 22:42:07 +00002463 // Don't create a top level definition for defm inside multiclasses,
2464 // instead, only update the prototypes and bind the template args
2465 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002466 if (!CurMultiClass)
2467 return false;
2468 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2469 i != e; ++i)
2470 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2471 == CurRec->getNameInit())
2472 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2473 "' already defined in this multiclass!");
2474 CurMultiClass->DefPrototypes.push_back(CurRec);
David Greenedb445972011-10-05 22:42:07 +00002475
Sean Silvacc951b22013-01-09 05:28:12 +00002476 // Copy the template arguments for the multiclass into the new def.
2477 const std::vector<Init *> &TA =
2478 CurMultiClass->Rec.getTemplateArgs();
David Greenedb445972011-10-05 22:42:07 +00002479
Sean Silvacc951b22013-01-09 05:28:12 +00002480 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2481 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2482 assert(RV && "Template arg doesn't exist?");
2483 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002484 }
2485
2486 return false;
2487}
2488
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002489/// ParseDefm - Parse the instantiation of a multiclass.
2490///
2491/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2492///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002493bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002494 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002495 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002496 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002497
Craig Topperb21afc62013-01-07 05:09:33 +00002498 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002499 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002500 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002501
Jordan Rosef12e8a92013-01-10 18:50:11 +00002502 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002503 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002504 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002505
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002506 // Keep track of the new generated record definitions.
2507 std::vector<Record*> NewRecDefs;
2508
2509 // This record also inherits from a regular class (non-multiclass)?
2510 bool InheritFromClass = false;
2511
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002512 // eat the colon.
2513 Lex.Lex();
2514
Chris Lattner526c8cb2009-06-21 03:39:35 +00002515 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002516 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002517
2518 while (1) {
Craig Topper011817a2014-04-09 04:50:04 +00002519 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002520
2521 // To instantiate a multiclass, we need to first get the multiclass, then
2522 // instantiate each def contained in the multiclass with the SubClassRef
2523 // template parameters.
2524 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2525 assert(MC && "Didn't lookup multiclass correctly?");
David Greeneaf8ee2c2011-07-29 22:43:06 +00002526 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002527
2528 // Verify that the correct number of template arguments were specified.
David Greenedb10e692011-10-19 13:02:42 +00002529 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002530 if (TArgs.size() < TemplateVals.size())
2531 return Error(SubClassLoc,
2532 "more template args specified than multiclass expects");
2533
2534 // Loop over all the def's in the multiclass, instantiating each one.
2535 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2536 Record *DefProto = MC->DefPrototypes[i];
2537
Jordan Rosef12e8a92013-01-10 18:50:11 +00002538 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix,
2539 SMRange(DefmLoc,
2540 DefmPrefixEndLoc));
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002541 if (!CurRec)
2542 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002543
Jordan Rosef12e8a92013-01-10 18:50:11 +00002544 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002545 TArgs, TemplateVals, true/*Delete args*/))
2546 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002547
Jordan Rosef12e8a92013-01-10 18:50:11 +00002548 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002549 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002550
2551 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002552 }
2553
David Greenedb445972011-10-05 22:42:07 +00002554
David Greenef00919a2009-04-22 22:17:51 +00002555 if (Lex.getCode() != tgtok::comma) break;
2556 Lex.Lex(); // eat ','.
2557
Craig Topper998a39a2013-08-20 04:22:09 +00002558 if (Lex.getCode() != tgtok::Id)
2559 return TokError("expected identifier");
2560
David Greenef00919a2009-04-22 22:17:51 +00002561 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002562
2563 // A defm can inherit from regular classes (non-multiclass) as
2564 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002565 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002566
2567 if (InheritFromClass)
2568 break;
2569
Craig Topper011817a2014-04-09 04:50:04 +00002570 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002571 }
2572
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002573 if (InheritFromClass) {
2574 // Process all the classes to inherit as if they were part of a
2575 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002576 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002577 while (1) {
2578 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002579 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002580
2581 // Get the expanded definition prototypes and teach them about
2582 // the record values the current class to inherit has
2583 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2584 Record *CurRec = NewRecDefs[i];
2585
2586 // Add it.
2587 if (AddSubClass(CurRec, SubClass))
2588 return true;
2589
Sean Silvacb1a75e2013-01-09 04:49:14 +00002590 if (ApplyLetStack(CurRec))
2591 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002592 }
2593
2594 if (Lex.getCode() != tgtok::comma) break;
2595 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002596 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002597 }
2598 }
2599
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002600 if (!CurMultiClass)
2601 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene50c09122011-08-10 18:27:46 +00002602 // See Record::setName(). This resolve step will see any new
2603 // name for the def that might have been created when resolving
2604 // inheritance, values and arguments above.
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002605 NewRecDefs[i]->resolveReferences();
2606
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002607 if (Lex.getCode() != tgtok::semi)
2608 return TokError("expected ';' at end of defm");
2609 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002610
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002611 return false;
2612}
2613
2614/// ParseObject
2615/// Object ::= ClassInst
2616/// Object ::= DefInst
2617/// Object ::= MultiClassInst
2618/// Object ::= DefMInst
2619/// Object ::= LETCommand '{' ObjectList '}'
2620/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002621bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002622 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002623 default:
2624 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002625 case tgtok::Let: return ParseTopLevelLet(MC);
2626 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002627 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002628 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002629 case tgtok::Class: return ParseClass();
2630 case tgtok::MultiClass: return ParseMultiClass();
2631 }
2632}
2633
2634/// ParseObjectList
2635/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002636bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002637 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002638 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002639 return true;
2640 }
2641 return false;
2642}
2643
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002644bool TGParser::ParseFile() {
2645 Lex.Lex(); // Prime the lexer.
2646 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002647
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002648 // If we have unread input at the end of the file, report it.
2649 if (Lex.getCode() == tgtok::Eof)
2650 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002651
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002652 return TokError("Unexpected input at top level");
2653}
2654