blob: 4d4bbe989d467ba55bb2ba91723f23acfb8405f6 [file] [log] [blame]
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001//===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Parser for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
14#include "TGParser.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallVector.h"
Chris Lattnerf4127dd2007-11-22 20:49:04 +000016#include "llvm/ADT/StringExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/Support/CommandLine.h"
18#include "llvm/TableGen/Record.h"
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000019#include <algorithm>
20#include <sstream>
Chris Lattnerf4127dd2007-11-22 20:49:04 +000021using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24// Support Code for the Semantic Actions.
25//===----------------------------------------------------------------------===//
26
27namespace llvm {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000028struct SubClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000029 SMRange RefRange;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000030 Record *Rec;
David Greeneaf8ee2c2011-07-29 22:43:06 +000031 std::vector<Init*> TemplateArgs;
Craig Topper011817a2014-04-09 04:50:04 +000032 SubClassReference() : Rec(nullptr) {}
David Greene7049e792009-04-24 16:55:41 +000033
Craig Topper011817a2014-04-09 04:50:04 +000034 bool isInvalid() const { return Rec == nullptr; }
Chris Lattnerf4127dd2007-11-22 20:49:04 +000035};
David Greene753ed8f2009-04-22 16:42:54 +000036
37struct SubMultiClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000038 SMRange RefRange;
David Greene753ed8f2009-04-22 16:42:54 +000039 MultiClass *MC;
David Greeneaf8ee2c2011-07-29 22:43:06 +000040 std::vector<Init*> TemplateArgs;
Craig Topper011817a2014-04-09 04:50:04 +000041 SubMultiClassReference() : MC(nullptr) {}
Bob Wilson3d948162009-04-28 19:41:44 +000042
Craig Topper011817a2014-04-09 04:50:04 +000043 bool isInvalid() const { return MC == nullptr; }
David Greene7049e792009-04-24 16:55:41 +000044 void dump() const;
David Greene753ed8f2009-04-22 16:42:54 +000045};
David Greene7049e792009-04-24 16:55:41 +000046
47void SubMultiClassReference::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000048 errs() << "Multiclass:\n";
Bob Wilson7248f862009-11-22 04:24:42 +000049
David Greene7049e792009-04-24 16:55:41 +000050 MC->dump();
Bob Wilson7248f862009-11-22 04:24:42 +000051
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000052 errs() << "Template args:\n";
David Greeneaf8ee2c2011-07-29 22:43:06 +000053 for (std::vector<Init *>::const_iterator i = TemplateArgs.begin(),
David Greene7049e792009-04-24 16:55:41 +000054 iend = TemplateArgs.end();
55 i != iend;
56 ++i) {
57 (*i)->dump();
58 }
59}
60
Chris Lattnerf4127dd2007-11-22 20:49:04 +000061} // end namespace llvm
62
Chris Lattner526c8cb2009-06-21 03:39:35 +000063bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Craig Topper011817a2014-04-09 04:50:04 +000064 if (!CurRec)
Chris Lattnerf4127dd2007-11-22 20:49:04 +000065 CurRec = &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +000066
Jakob Stoklund Olesen9d1c5ee2012-01-13 03:16:35 +000067 if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000068 // The value already exists in the class, treat this as a set.
69 if (ERV->setValue(RV.getValue()))
70 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
71 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson7248f862009-11-22 04:24:42 +000072 "previous definition of type '" +
Chris Lattnerf4127dd2007-11-22 20:49:04 +000073 ERV->getType()->getAsString() + "'");
74 } else {
75 CurRec->addValue(RV);
76 }
77 return false;
78}
79
80/// SetValue -
81/// Return true on error, false on success.
David Greene3ca42122011-10-19 13:02:39 +000082bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
David Greeneaf8ee2c2011-07-29 22:43:06 +000083 const std::vector<unsigned> &BitList, Init *V) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000084 if (!V) return false;
85
Craig Topper011817a2014-04-09 04:50:04 +000086 if (!CurRec) CurRec = &CurMultiClass->Rec;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000087
88 RecordVal *RV = CurRec->getValue(ValName);
Craig Topper011817a2014-04-09 04:50:04 +000089 if (!RV)
David Greene3ca42122011-10-19 13:02:39 +000090 return Error(Loc, "Value '" + ValName->getAsUnquotedString()
91 + "' unknown!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +000092
93 // Do not allow assignments like 'X = X'. This will just cause infinite loops
94 // in the resolution machinery.
95 if (BitList.empty())
Sean Silvafb509ed2012-10-10 20:24:43 +000096 if (VarInit *VI = dyn_cast<VarInit>(V))
David Greene3ca42122011-10-19 13:02:39 +000097 if (VI->getNameInit() == ValName)
Chris Lattnerf4127dd2007-11-22 20:49:04 +000098 return false;
Bob Wilson7248f862009-11-22 04:24:42 +000099
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000100 // If we are assigning to a subset of the bits in the value... then we must be
101 // assigning to a field of BitsRecTy, which must have a BitsInit
102 // initializer.
103 //
104 if (!BitList.empty()) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000105 BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue());
Craig Topper011817a2014-04-09 04:50:04 +0000106 if (!CurVal)
David Greene3ca42122011-10-19 13:02:39 +0000107 return Error(Loc, "Value '" + ValName->getAsUnquotedString()
108 + "' is not a bits type");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000109
110 // Convert the incoming value to a bits type of the appropriate size...
David Greeneaf8ee2c2011-07-29 22:43:06 +0000111 Init *BI = V->convertInitializerTo(BitsRecTy::get(BitList.size()));
Craig Topper011817a2014-04-09 04:50:04 +0000112 if (!BI) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000113 return Error(Loc, "Initializer is not compatible with bit range");
114 }
Bob Wilson7248f862009-11-22 04:24:42 +0000115
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000116 // We should have a BitsInit type now.
Sean Silvafb509ed2012-10-10 20:24:43 +0000117 BitsInit *BInit = dyn_cast<BitsInit>(BI);
Craig Toppere73658d2014-04-28 04:05:08 +0000118 assert(BInit != nullptr);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000119
David Greeneaf8ee2c2011-07-29 22:43:06 +0000120 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000121
122 // Loop over bits, assigning values as appropriate.
123 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
124 unsigned Bit = BitList[i];
David Greeneb3da8122011-07-29 19:07:00 +0000125 if (NewBits[Bit])
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000126 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
David Greene3ca42122011-10-19 13:02:39 +0000127 ValName->getAsUnquotedString() + "' more than once");
David Greeneb3da8122011-07-29 19:07:00 +0000128 NewBits[Bit] = BInit->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000129 }
130
131 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
Craig Topper011817a2014-04-09 04:50:04 +0000132 if (!NewBits[i])
David Greeneb3da8122011-07-29 19:07:00 +0000133 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000134
David Greenee32ebf22011-07-29 19:07:07 +0000135 V = BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000136 }
137
Pete Cooper040c6a62014-07-31 01:43:57 +0000138 if (RV->setValue(V)) {
139 std::string InitType = "";
140 if (BitsInit *BI = dyn_cast<BitsInit>(V)) {
141 InitType = (Twine("' of type bit initializer with length ") +
142 Twine(BI->getNumBits())).str();
143 }
David Greene3ca42122011-10-19 13:02:39 +0000144 return Error(Loc, "Value '" + ValName->getAsUnquotedString() + "' of type '"
145 + RV->getType()->getAsString() +
146 "' is incompatible with initializer '" + V->getAsString()
Pete Cooper040c6a62014-07-31 01:43:57 +0000147 + InitType
David Greene3ca42122011-10-19 13:02:39 +0000148 + "'");
Pete Cooper040c6a62014-07-31 01:43:57 +0000149 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000150 return false;
151}
152
153/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
154/// args as SubClass's template arguments.
Cedric Venetd1e179d2009-02-14 16:06:42 +0000155bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000156 Record *SC = SubClass.Rec;
157 // Add all of the values in the subclass into the current class.
158 const std::vector<RecordVal> &Vals = SC->getValues();
159 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
Jordan Rosef12e8a92013-01-10 18:50:11 +0000160 if (AddValue(CurRec, SubClass.RefRange.Start, Vals[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000161 return true;
162
David Greenedb10e692011-10-19 13:02:42 +0000163 const std::vector<Init *> &TArgs = SC->getTemplateArgs();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000164
165 // Ensure that an appropriate number of template arguments are specified.
166 if (TArgs.size() < SubClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000167 return Error(SubClass.RefRange.Start,
168 "More template args specified than expected");
Bob Wilson7248f862009-11-22 04:24:42 +0000169
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000170 // Loop over all of the template arguments, setting them to the specified
171 // value or leaving them as the default if necessary.
172 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
173 if (i < SubClass.TemplateArgs.size()) {
174 // If a value is specified for this template arg, set it now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000175 if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
176 std::vector<unsigned>(), SubClass.TemplateArgs[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000177 return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000178
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000179 // Resolve it next.
180 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson7248f862009-11-22 04:24:42 +0000181
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000182 // Now remove it.
183 CurRec->removeValue(TArgs[i]);
184
185 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000186 return Error(SubClass.RefRange.Start,
187 "Value not specified for template argument #"
David Greenedb10e692011-10-19 13:02:42 +0000188 + utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
189 + ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000190 }
191 }
192
193 // Since everything went well, we can now set the "superclass" list for the
194 // current record.
195 const std::vector<Record*> &SCs = SC->getSuperClasses();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000196 ArrayRef<SMRange> SCRanges = SC->getSuperClassRanges();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000197 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
198 if (CurRec->isSubClassOf(SCs[i]))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000199 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000200 "Already subclass of '" + SCs[i]->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000201 CurRec->addSuperClass(SCs[i], SCRanges[i]);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000202 }
Bob Wilson7248f862009-11-22 04:24:42 +0000203
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000204 if (CurRec->isSubClassOf(SC))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000205 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000206 "Already subclass of '" + SC->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000207 CurRec->addSuperClass(SC, SubClass.RefRange);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000208 return false;
209}
210
David Greene753ed8f2009-04-22 16:42:54 +0000211/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilsonf71e6562009-04-30 18:26:19 +0000212/// CurMC, resolving its template args as SubMultiClass's
David Greene753ed8f2009-04-22 16:42:54 +0000213/// template arguments.
Bob Wilsonf71e6562009-04-30 18:26:19 +0000214bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson92ab8202009-04-30 17:46:20 +0000215 SubMultiClassReference &SubMultiClass) {
David Greene753ed8f2009-04-22 16:42:54 +0000216 MultiClass *SMC = SubMultiClass.MC;
Bob Wilsonf71e6562009-04-30 18:26:19 +0000217 Record *CurRec = &CurMC->Rec;
David Greene753ed8f2009-04-22 16:42:54 +0000218
Bob Wilsonf71e6562009-04-30 18:26:19 +0000219 const std::vector<RecordVal> &MCVals = CurRec->getValues();
David Greene753ed8f2009-04-22 16:42:54 +0000220
221 // Add all of the values in the subclass into the current class.
222 const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues();
223 for (unsigned i = 0, e = SMCVals.size(); i != e; ++i)
Jordan Rosef12e8a92013-01-10 18:50:11 +0000224 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVals[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000225 return true;
226
Bob Wilsonf71e6562009-04-30 18:26:19 +0000227 int newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000228
David Greene753ed8f2009-04-22 16:42:54 +0000229 // Add all of the defs in the subclass into the current multiclass.
230 for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(),
231 iend = SMC->DefPrototypes.end();
232 i != iend;
233 ++i) {
234 // Clone the def and add it to the current multiclass
Anton Yartsev3fa65d42014-09-25 19:55:58 +0000235 auto NewDef = make_unique<Record>(**i);
David Greene753ed8f2009-04-22 16:42:54 +0000236
237 // Add all of the values in the superclass into the current def.
238 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
Anton Yartsev3fa65d42014-09-25 19:55:58 +0000239 if (AddValue(NewDef.get(), SubMultiClass.RefRange.Start, MCVals[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000240 return true;
241
Anton Yartsev3fa65d42014-09-25 19:55:58 +0000242 CurMC->DefPrototypes.push_back(NewDef.release());
David Greene753ed8f2009-04-22 16:42:54 +0000243 }
Bob Wilson3d948162009-04-28 19:41:44 +0000244
David Greenedb10e692011-10-19 13:02:42 +0000245 const std::vector<Init *> &SMCTArgs = SMC->Rec.getTemplateArgs();
David Greene753ed8f2009-04-22 16:42:54 +0000246
David Greene7049e792009-04-24 16:55:41 +0000247 // Ensure that an appropriate number of template arguments are
248 // specified.
David Greene753ed8f2009-04-22 16:42:54 +0000249 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000250 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000251 "More template args specified than expected");
Bob Wilson3d948162009-04-28 19:41:44 +0000252
David Greene753ed8f2009-04-22 16:42:54 +0000253 // Loop over all of the template arguments, setting them to the specified
254 // value or leaving them as the default if necessary.
255 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
256 if (i < SubMultiClass.TemplateArgs.size()) {
David Greene7049e792009-04-24 16:55:41 +0000257 // If a value is specified for this template arg, set it in the
258 // superclass now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000259 if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson3d948162009-04-28 19:41:44 +0000260 std::vector<unsigned>(),
David Greene753ed8f2009-04-22 16:42:54 +0000261 SubMultiClass.TemplateArgs[i]))
262 return true;
263
264 // Resolve it next.
265 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson3d948162009-04-28 19:41:44 +0000266
David Greene753ed8f2009-04-22 16:42:54 +0000267 // Now remove it.
268 CurRec->removeValue(SMCTArgs[i]);
269
David Greene7049e792009-04-24 16:55:41 +0000270 // If a value is specified for this template arg, set it in the
271 // new defs now.
272 for (MultiClass::RecordVector::iterator j =
Bob Wilsonf71e6562009-04-30 18:26:19 +0000273 CurMC->DefPrototypes.begin() + newDefStart,
274 jend = CurMC->DefPrototypes.end();
David Greene753ed8f2009-04-22 16:42:54 +0000275 j != jend;
276 ++j) {
277 Record *Def = *j;
278
Jordan Rosef12e8a92013-01-10 18:50:11 +0000279 if (SetValue(Def, SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson3d948162009-04-28 19:41:44 +0000280 std::vector<unsigned>(),
David Greene753ed8f2009-04-22 16:42:54 +0000281 SubMultiClass.TemplateArgs[i]))
282 return true;
283
284 // Resolve it next.
285 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
286
287 // Now remove it
288 Def->removeValue(SMCTArgs[i]);
289 }
290 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000291 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000292 "Value not specified for template argument #"
David Greenedb10e692011-10-19 13:02:42 +0000293 + utostr(i) + " (" + SMCTArgs[i]->getAsUnquotedString()
294 + ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000295 }
296 }
297
298 return false;
299}
300
David Greenefb927af2012-02-22 16:09:41 +0000301/// ProcessForeachDefs - Given a record, apply all of the variable
302/// values in all surrounding foreach loops, creating new records for
303/// each combination of values.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000304bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
305 if (Loops.empty())
306 return false;
307
David Greenefb927af2012-02-22 16:09:41 +0000308 // We want to instantiate a new copy of CurRec for each combination
309 // of nested loop iterator values. We don't want top instantiate
310 // any copies until we have values for each loop iterator.
311 IterSet IterVals;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000312 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000313}
314
315/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
316/// apply each of the variable values in this loop and then process
317/// subloops.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000318bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
319 // Recursively build a tuple of iterator values.
320 if (IterVals.size() != Loops.size()) {
321 assert(IterVals.size() < Loops.size());
322 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silvafb509ed2012-10-10 20:24:43 +0000323 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Craig Topper011817a2014-04-09 04:50:04 +0000324 if (!List) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000325 Error(Loc, "Loop list is not a list");
326 return true;
327 }
David Greenefb927af2012-02-22 16:09:41 +0000328
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000329 // Process each value.
330 for (int64_t i = 0; i < List->getSize(); ++i) {
Craig Topper011817a2014-04-09 04:50:04 +0000331 Init *ItemVal = List->resolveListElementReference(*CurRec, nullptr, i);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000332 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
333 if (ProcessForeachDefs(CurRec, Loc, IterVals))
334 return true;
335 IterVals.pop_back();
336 }
337 return false;
338 }
339
340 // This is the bottom of the recursion. We have all of the iterator values
341 // for this point in the iteration space. Instantiate a new record to
342 // reflect this combination of values.
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");
Anton Yartsev671dff12014-08-08 00:29:54 +0000351 delete IterRec;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000352 return true;
353 }
354
355 IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false));
356
357 if (SetValue(IterRec, Loc, IterVar->getName(),
358 std::vector<unsigned>(), IVal)) {
359 Error(Loc, "when instantiating this def");
Anton Yartsev671dff12014-08-08 00:29:54 +0000360 delete IterRec;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000361 return true;
362 }
363
364 // Resolve it next.
365 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName()));
366
367 // Remove it.
368 IterRec->removeValue(IterVar->getName());
369 }
370
371 if (Records.getDef(IterRec->getNameInitAsString())) {
Artyom Skrobov8b985322014-06-10 12:41:14 +0000372 // If this record is anonymous, it's no problem, just generate a new name
373 if (IterRec->isAnonymous())
374 IterRec->setName(GetNewAnonymousName());
375 else {
376 Error(Loc, "def already exists: " + IterRec->getNameInitAsString());
Anton Yartsev671dff12014-08-08 00:29:54 +0000377 delete IterRec;
Artyom Skrobov8b985322014-06-10 12:41:14 +0000378 return true;
379 }
David Greenefb927af2012-02-22 16:09:41 +0000380 }
381
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000382 Records.addDef(IterRec);
383 IterRec->resolveReferences();
David Greenefb927af2012-02-22 16:09:41 +0000384 return false;
385}
386
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000387//===----------------------------------------------------------------------===//
388// Parser Code
389//===----------------------------------------------------------------------===//
390
391/// isObjectStart - Return true if this is a valid first token for an Object.
392static bool isObjectStart(tgtok::TokKind K) {
393 return K == tgtok::Class || K == tgtok::Def ||
David Greenefb927af2012-02-22 16:09:41 +0000394 K == tgtok::Defm || K == tgtok::Let ||
395 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000396}
397
Alp Tokerce91fe52013-12-21 18:51:00 +0000398/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
399/// an identifier.
400std::string TGParser::GetNewAnonymousName() {
Michael J. Spencer2bb7db92013-02-26 21:29:47 +0000401 unsigned Tmp = AnonCounter++; // MSVC2012 ICEs without this.
Alp Tokerce91fe52013-12-21 18:51:00 +0000402 return "anonymous_" + utostr(Tmp);
Chris Lattner7538ed82010-10-05 22:51:56 +0000403}
404
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000405/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000406/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000407/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000408/// ObjectName ::= /*empty*/
409///
David Greene2affd672011-10-19 13:04:29 +0000410Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
411 switch (Lex.getCode()) {
412 case tgtok::colon:
413 case tgtok::semi:
414 case tgtok::l_brace:
415 // These are all of the tokens that can begin an object body.
416 // Some of these can also begin values but we disallow those cases
417 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000418 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000419 default:
420 break;
421 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000422
Craig Topper011817a2014-04-09 04:50:04 +0000423 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000424 if (CurMultiClass)
425 CurRec = &CurMultiClass->Rec;
426
Craig Topper011817a2014-04-09 04:50:04 +0000427 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000428 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000429 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000430 if (!CurRecName) {
431 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000432 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000433 }
434 Type = CurRecName->getType();
435 }
436
437 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000438}
439
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000440/// ParseClassID - Parse and resolve a reference to a class name. This returns
441/// null on error.
442///
443/// ClassID ::= ID
444///
445Record *TGParser::ParseClassID() {
446 if (Lex.getCode() != tgtok::Id) {
447 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000448 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000449 }
Bob Wilson7248f862009-11-22 04:24:42 +0000450
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000451 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000452 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000453 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000454
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000455 Lex.Lex();
456 return Result;
457}
458
Bob Wilson3d948162009-04-28 19:41:44 +0000459/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
460/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000461///
462/// MultiClassID ::= ID
463///
464MultiClass *TGParser::ParseMultiClassID() {
465 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000466 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000467 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000468 }
Bob Wilson3d948162009-04-28 19:41:44 +0000469
David Greene753ed8f2009-04-22 16:42:54 +0000470 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
Craig Topper011817a2014-04-09 04:50:04 +0000471 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000472 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000473
David Greene753ed8f2009-04-22 16:42:54 +0000474 Lex.Lex();
475 return Result;
476}
477
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000478/// ParseSubClassReference - Parse a reference to a subclass or to a templated
479/// subclass. This returns a SubClassRefTy with a null Record* on error.
480///
481/// SubClassRef ::= ClassID
482/// SubClassRef ::= ClassID '<' ValueList '>'
483///
484SubClassReference TGParser::
485ParseSubClassReference(Record *CurRec, bool isDefm) {
486 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000487 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000488
Sean Silva0657b402013-01-09 02:17:14 +0000489 if (isDefm) {
490 if (MultiClass *MC = ParseMultiClassID())
491 Result.Rec = &MC->Rec;
492 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000493 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000494 }
Craig Topper011817a2014-04-09 04:50:04 +0000495 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000496
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000497 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000498 if (Lex.getCode() != tgtok::less) {
499 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000500 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000501 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000502 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000503
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000504 if (Lex.getCode() == tgtok::greater) {
505 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000506 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000507 return Result;
508 }
Bob Wilson7248f862009-11-22 04:24:42 +0000509
David Greene8618f952009-06-08 20:23:18 +0000510 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000511 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000512 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000513 return Result;
514 }
Bob Wilson7248f862009-11-22 04:24:42 +0000515
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000516 if (Lex.getCode() != tgtok::greater) {
517 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000518 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000519 return Result;
520 }
521 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000522 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000523
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000524 return Result;
525}
526
Bob Wilson3d948162009-04-28 19:41:44 +0000527/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
528/// templated submulticlass. This returns a SubMultiClassRefTy with a null
529/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000530///
531/// SubMultiClassRef ::= MultiClassID
532/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
533///
534SubMultiClassReference TGParser::
535ParseSubMultiClassReference(MultiClass *CurMC) {
536 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000537 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000538
David Greene753ed8f2009-04-22 16:42:54 +0000539 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000540 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000541
David Greene753ed8f2009-04-22 16:42:54 +0000542 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000543 if (Lex.getCode() != tgtok::less) {
544 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000545 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000546 }
David Greene753ed8f2009-04-22 16:42:54 +0000547 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000548
David Greene753ed8f2009-04-22 16:42:54 +0000549 if (Lex.getCode() == tgtok::greater) {
550 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000551 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000552 return Result;
553 }
Bob Wilson3d948162009-04-28 19:41:44 +0000554
David Greene8618f952009-06-08 20:23:18 +0000555 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000556 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000557 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000558 return Result;
559 }
Bob Wilson3d948162009-04-28 19:41:44 +0000560
David Greene753ed8f2009-04-22 16:42:54 +0000561 if (Lex.getCode() != tgtok::greater) {
562 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000563 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000564 return Result;
565 }
566 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000567 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000568
569 return Result;
570}
571
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000572/// ParseRangePiece - Parse a bit/value range.
573/// RangePiece ::= INTVAL
574/// RangePiece ::= INTVAL '-' INTVAL
575/// RangePiece ::= INTVAL INTVAL
576bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000577 if (Lex.getCode() != tgtok::IntVal) {
578 TokError("expected integer or bitrange");
579 return true;
580 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000581 int64_t Start = Lex.getCurIntVal();
582 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000583
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000584 if (Start < 0)
585 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000586
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000587 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000588 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000589 Ranges.push_back(Start);
590 return false;
591 case tgtok::minus:
592 if (Lex.Lex() != tgtok::IntVal) {
593 TokError("expected integer value as end of range");
594 return true;
595 }
596 End = Lex.getCurIntVal();
597 break;
598 case tgtok::IntVal:
599 End = -Lex.getCurIntVal();
600 break;
601 }
Bob Wilson7248f862009-11-22 04:24:42 +0000602 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000603 return TokError("invalid range, cannot be negative");
604 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000605
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000606 // Add to the range.
607 if (Start < End) {
608 for (; Start <= End; ++Start)
609 Ranges.push_back(Start);
610 } else {
611 for (; Start >= End; --Start)
612 Ranges.push_back(Start);
613 }
614 return false;
615}
616
617/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
618///
619/// RangeList ::= RangePiece (',' RangePiece)*
620///
621std::vector<unsigned> TGParser::ParseRangeList() {
622 std::vector<unsigned> Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000623
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000624 // Parse the first piece.
625 if (ParseRangePiece(Result))
626 return std::vector<unsigned>();
627 while (Lex.getCode() == tgtok::comma) {
628 Lex.Lex(); // Eat the comma.
629
630 // Parse the next range piece.
631 if (ParseRangePiece(Result))
632 return std::vector<unsigned>();
633 }
634 return Result;
635}
636
637/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
638/// OptionalRangeList ::= '<' RangeList '>'
639/// OptionalRangeList ::= /*empty*/
640bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
641 if (Lex.getCode() != tgtok::less)
642 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000643
Chris Lattner526c8cb2009-06-21 03:39:35 +0000644 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000645 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000646
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000647 // Parse the range list.
648 Ranges = ParseRangeList();
649 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000650
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000651 if (Lex.getCode() != tgtok::greater) {
652 TokError("expected '>' at end of range list");
653 return Error(StartLoc, "to match this '<'");
654 }
655 Lex.Lex(); // eat the '>'.
656 return false;
657}
658
659/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
660/// OptionalBitList ::= '{' RangeList '}'
661/// OptionalBitList ::= /*empty*/
662bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
663 if (Lex.getCode() != tgtok::l_brace)
664 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000665
Chris Lattner526c8cb2009-06-21 03:39:35 +0000666 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000667 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000668
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000669 // Parse the range list.
670 Ranges = ParseRangeList();
671 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000672
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000673 if (Lex.getCode() != tgtok::r_brace) {
674 TokError("expected '}' at end of bit list");
675 return Error(StartLoc, "to match this '{'");
676 }
677 Lex.Lex(); // eat the '}'.
678 return false;
679}
680
681
682/// ParseType - Parse and return a tblgen type. This returns null on error.
683///
684/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000685/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000686/// Type ::= BIT // bit type
687/// Type ::= BITS '<' INTVAL '>' // bits<x> type
688/// Type ::= INT // int type
689/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000690/// Type ::= DAG // dag type
691/// Type ::= ClassID // Record Type
692///
693RecTy *TGParser::ParseType() {
694 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000695 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000696 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000697 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000698 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
699 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000700 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000701 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000702 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000703 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000704 case tgtok::Bits: {
705 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
706 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000707 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000708 }
709 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
710 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000711 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000712 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000713 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000714 if (Lex.Lex() != tgtok::greater) { // Eat count.
715 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000716 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000717 }
718 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000719 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000720 }
721 case tgtok::List: {
722 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
723 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000724 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000725 }
726 Lex.Lex(); // Eat '<'
727 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000728 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000729
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000730 if (Lex.getCode() != tgtok::greater) {
731 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000732 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000733 }
734 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000735 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000736 }
Bob Wilson7248f862009-11-22 04:24:42 +0000737 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000738}
739
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000740/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
741/// has already been read.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000742Init *TGParser::ParseIDValue(Record *CurRec,
David Greened4263a62011-10-19 13:04:20 +0000743 const std::string &Name, SMLoc NameLoc,
744 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000745 if (CurRec) {
746 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000747 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000748
David Greenedb10e692011-10-19 13:02:42 +0000749 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
750
David Greene47a665e2011-10-05 22:42:54 +0000751 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +0000752 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
753 "::");
David Greene47a665e2011-10-05 22:42:54 +0000754
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000755 if (CurRec->isTemplateArg(TemplateArgName)) {
756 const RecordVal *RV = CurRec->getValue(TemplateArgName);
757 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000758 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000759 }
760 }
Bob Wilson7248f862009-11-22 04:24:42 +0000761
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000762 if (CurMultiClass) {
David Greenedb10e692011-10-19 13:02:42 +0000763 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
764 "::");
765
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000766 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
767 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
768 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000769 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000770 }
771 }
Bob Wilson7248f862009-11-22 04:24:42 +0000772
David Greenefb927af2012-02-22 16:09:41 +0000773 // If this is in a foreach loop, make sure it's not a loop iterator
774 for (LoopVector::iterator i = Loops.begin(), iend = Loops.end();
775 i != iend;
776 ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000777 VarInit *IterVar = dyn_cast<VarInit>(i->IterVar);
David Greenefb927af2012-02-22 16:09:41 +0000778 if (IterVar && IterVar->getName() == Name)
779 return IterVar;
780 }
781
David Greene232bd602011-10-19 13:04:21 +0000782 if (Mode == ParseNameMode)
783 return StringInit::get(Name);
784
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000785 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000786 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000787
David Greene232bd602011-10-19 13:04:21 +0000788 if (Mode == ParseValueMode) {
789 Error(NameLoc, "Variable not defined: '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000790 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000791 }
792
793 return StringInit::get(Name);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000794}
795
David Greene5d0c0512009-05-14 20:54:48 +0000796/// ParseOperation - Parse an operator. This returns null on error.
797///
798/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
799///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000800Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000801 switch (Lex.getCode()) {
802 default:
803 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000804 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000805 case tgtok::XHead:
806 case tgtok::XTail:
807 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000808 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
809 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000810 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000811
David Greenee8f3b272009-05-14 21:22:49 +0000812 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000813 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000814 case tgtok::XCast:
815 Lex.Lex(); // eat the operation
816 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000817
David Greenee8f3b272009-05-14 21:22:49 +0000818 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000819
Craig Topper011817a2014-04-09 04:50:04 +0000820 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000821 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000822 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000823 }
David Greene5d0c0512009-05-14 20:54:48 +0000824
David Greenee8f3b272009-05-14 21:22:49 +0000825 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000826 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000827 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000828 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000829 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000830 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000831 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000832 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000833 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000834 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000835 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000836 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000837 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000838 break;
David Greenee8f3b272009-05-14 21:22:49 +0000839 }
840 if (Lex.getCode() != tgtok::l_paren) {
841 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000842 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000843 }
844 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000845
David Greeneaf8ee2c2011-07-29 22:43:06 +0000846 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000847 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000848
David Greene2f7cf7f2011-01-07 17:05:37 +0000849 if (Code == UnOpInit::HEAD
850 || Code == UnOpInit::TAIL
851 || Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000852 ListInit *LHSl = dyn_cast<ListInit>(LHS);
853 StringInit *LHSs = dyn_cast<StringInit>(LHS);
854 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000855 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000856 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000857 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000858 }
859 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000860 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
861 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000862 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000863 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000864 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000865 }
866 }
867
David Greene2f7cf7f2011-01-07 17:05:37 +0000868 if (Code == UnOpInit::HEAD
869 || Code == UnOpInit::TAIL) {
Craig Topper011817a2014-04-09 04:50:04 +0000870 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000871 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000872 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000873 }
Bob Wilson7248f862009-11-22 04:24:42 +0000874
David Greened571b3c2009-05-14 22:38:31 +0000875 if (LHSl && LHSl->getSize() == 0) {
876 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000877 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000878 }
879 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000880 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000881 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000882 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000883 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000884 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000885 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000886 if (Code == UnOpInit::HEAD) {
David Greened571b3c2009-05-14 22:38:31 +0000887 Type = Itemt->getType();
Bob Wilson7248f862009-11-22 04:24:42 +0000888 } else {
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000889 Type = ListRecTy::get(Itemt->getType());
David Greened571b3c2009-05-14 22:38:31 +0000890 }
Bob Wilson7248f862009-11-22 04:24:42 +0000891 } else {
David Greened571b3c2009-05-14 22:38:31 +0000892 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000893 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000894 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000895 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000896 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000897 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000898 if (Code == UnOpInit::HEAD) {
David Greened571b3c2009-05-14 22:38:31 +0000899 Type = LType->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +0000900 } else {
David Greened571b3c2009-05-14 22:38:31 +0000901 Type = LType;
902 }
903 }
904 }
905 }
906
David Greenee8f3b272009-05-14 21:22:49 +0000907 if (Lex.getCode() != tgtok::r_paren) {
908 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000909 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000910 }
911 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000912 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000913 }
David Greene5d0c0512009-05-14 20:54:48 +0000914
915 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000916 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000917 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +0000918 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000919 case tgtok::XSRL:
920 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000921 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000922 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000923 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000924 tgtok::TokKind OpTok = Lex.getCode();
925 SMLoc OpLoc = Lex.getLoc();
926 Lex.Lex(); // eat the operation
927
David Greene5d0c0512009-05-14 20:54:48 +0000928 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000929 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000930
Chris Lattner61ea00b2010-10-05 23:58:18 +0000931 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000932 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000933 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000934 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000935 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000936 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
937 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
938 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
939 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000940 case tgtok::XListConcat:
941 Code = BinOpInit::LISTCONCAT;
942 // We don't know the list type until we parse the first argument
943 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000944 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000945 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000946 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000947 break;
David Greene5d0c0512009-05-14 20:54:48 +0000948 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000949
David Greene5d0c0512009-05-14 20:54:48 +0000950 if (Lex.getCode() != tgtok::l_paren) {
951 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000952 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000953 }
954 Lex.Lex(); // eat the '('
955
David Greeneaf8ee2c2011-07-29 22:43:06 +0000956 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000957
Chris Lattner61ea00b2010-10-05 23:58:18 +0000958 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000959 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000960
Chris Lattner61ea00b2010-10-05 23:58:18 +0000961 while (Lex.getCode() == tgtok::comma) {
962 Lex.Lex(); // eat the ','
963
964 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000965 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000966 }
David Greene5d0c0512009-05-14 20:54:48 +0000967
968 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000969 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000970 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000971 }
972 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000973
Daniel Sanders314e80e2014-05-07 10:13:19 +0000974 // If we are doing !listconcat, we should know the type by now
975 if (OpTok == tgtok::XListConcat) {
976 if (VarInit *Arg0 = dyn_cast<VarInit>(InitList[0]))
977 Type = Arg0->getType();
978 else if (ListInit *Arg0 = dyn_cast<ListInit>(InitList[0]))
979 Type = Arg0->getType();
980 else {
981 InitList[0]->dump();
982 Error(OpLoc, "expected a list");
983 return nullptr;
984 }
985 }
986
Chris Lattner61ea00b2010-10-05 23:58:18 +0000987 // We allow multiple operands to associative operators like !strconcat as
988 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000989 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000990 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000991 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000992 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
993 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000994 InitList.back() = RHS;
995 }
996 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000997
Chris Lattner61ea00b2010-10-05 23:58:18 +0000998 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000999 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +00001000 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +00001001
Chris Lattner61ea00b2010-10-05 23:58:18 +00001002 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +00001003 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001004 }
1005
David Greene3587eed2009-05-14 23:26:46 +00001006 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001007 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001008 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
1009 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +00001010 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001011
David Greene98ed3c72009-05-14 21:54:42 +00001012 tgtok::TokKind LexCode = Lex.getCode();
1013 Lex.Lex(); // eat the operation
1014 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001015 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001016 case tgtok::XIf:
1017 Code = TernOpInit::IF;
1018 break;
David Greenee917fff2009-05-14 22:23:47 +00001019 case tgtok::XForEach:
1020 Code = TernOpInit::FOREACH;
1021 break;
David Greene98ed3c72009-05-14 21:54:42 +00001022 case tgtok::XSubst:
1023 Code = TernOpInit::SUBST;
1024 break;
1025 }
1026 if (Lex.getCode() != tgtok::l_paren) {
1027 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001028 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001029 }
1030 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001031
David Greeneaf8ee2c2011-07-29 22:43:06 +00001032 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001033 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001034
David Greene98ed3c72009-05-14 21:54:42 +00001035 if (Lex.getCode() != tgtok::comma) {
1036 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001037 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001038 }
1039 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001040
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001041 Init *MHS = ParseValue(CurRec, ItemType);
1042 if (!MHS)
1043 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001044
David Greene98ed3c72009-05-14 21:54:42 +00001045 if (Lex.getCode() != tgtok::comma) {
1046 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001047 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001048 }
1049 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001050
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001051 Init *RHS = ParseValue(CurRec, ItemType);
1052 if (!RHS)
1053 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001054
David Greene98ed3c72009-05-14 21:54:42 +00001055 if (Lex.getCode() != tgtok::r_paren) {
1056 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001057 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001058 }
1059 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001060
David Greene98ed3c72009-05-14 21:54:42 +00001061 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001062 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001063 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001064 RecTy *MHSTy = nullptr;
1065 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001066
Sean Silvafb509ed2012-10-10 20:24:43 +00001067 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001068 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001069 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001070 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001071 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001072 MHSTy = BitRecTy::get();
1073
Sean Silvafb509ed2012-10-10 20:24:43 +00001074 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001075 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001076 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001077 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001078 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001079 RHSTy = BitRecTy::get();
1080
1081 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001082 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001083 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001084 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001085 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001086
1087 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001088 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001089 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001090 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001091
1092 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1093 Type = RHSTy;
1094 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1095 Type = MHSTy;
Bob Wilson7248f862009-11-22 04:24:42 +00001096 } else {
David Greene3587eed2009-05-14 23:26:46 +00001097 TokError("inconsistent types for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001098 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001099 }
1100 break;
1101 }
David Greenee917fff2009-05-14 22:23:47 +00001102 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001103 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
Craig Topper011817a2014-04-09 04:50:04 +00001104 if (!MHSt) {
David Greenee917fff2009-05-14 22:23:47 +00001105 TokError("could not get type for !foreach");
Craig Topper011817a2014-04-09 04:50:04 +00001106 return nullptr;
David Greenee917fff2009-05-14 22:23:47 +00001107 }
1108 Type = MHSt->getType();
1109 break;
1110 }
David Greene98ed3c72009-05-14 21:54:42 +00001111 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001112 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001113 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001114 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001115 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001116 }
1117 Type = RHSt->getType();
1118 break;
1119 }
1120 }
David Greenee32ebf22011-07-29 19:07:07 +00001121 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001122 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001123 }
David Greene5d0c0512009-05-14 20:54:48 +00001124 }
David Greene5d0c0512009-05-14 20:54:48 +00001125}
1126
1127/// ParseOperatorType - Parse a type for an operator. This returns
1128/// null on error.
1129///
1130/// OperatorType ::= '<' Type '>'
1131///
Dan Gohman1432ef82009-08-12 22:10:57 +00001132RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001133 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001134
1135 if (Lex.getCode() != tgtok::less) {
1136 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001137 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001138 }
1139 Lex.Lex(); // eat the <
1140
1141 Type = ParseType();
1142
Craig Topper011817a2014-04-09 04:50:04 +00001143 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001144 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001145 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001146 }
1147
1148 if (Lex.getCode() != tgtok::greater) {
1149 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001150 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001151 }
1152 Lex.Lex(); // eat the >
1153
1154 return Type;
1155}
1156
1157
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001158/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1159///
1160/// SimpleValue ::= IDValue
1161/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001162/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001163/// SimpleValue ::= CODEFRAGMENT
1164/// SimpleValue ::= '?'
1165/// SimpleValue ::= '{' ValueList '}'
1166/// SimpleValue ::= ID '<' ValueListNE '>'
1167/// SimpleValue ::= '[' ValueList ']'
1168/// SimpleValue ::= '(' IDValue DagArgList ')'
1169/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001170/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001171/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1172/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1173/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001174/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001175/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1176///
David Greened4263a62011-10-19 13:04:20 +00001177Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1178 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001179 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001180 switch (Lex.getCode()) {
1181 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001182 case tgtok::paste:
1183 // This is a leading paste operation. This is deprecated but
1184 // still exists in some .td files. Ignore it.
1185 Lex.Lex(); // Skip '#'.
1186 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001187 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001188 case tgtok::BinaryIntVal: {
1189 auto BinaryVal = Lex.getCurBinaryIntVal();
1190 SmallVector<Init*, 16> Bits(BinaryVal.second);
1191 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001192 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001193 R = BitsInit::get(Bits);
1194 Lex.Lex();
1195 break;
1196 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001197 case tgtok::StrVal: {
1198 std::string Val = Lex.getCurStrVal();
1199 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001200
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001201 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001202 while (Lex.getCode() == tgtok::StrVal) {
1203 Val += Lex.getCurStrVal();
1204 Lex.Lex();
1205 }
Bob Wilson7248f862009-11-22 04:24:42 +00001206
David Greenee32ebf22011-07-29 19:07:07 +00001207 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001208 break;
1209 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001210 case tgtok::CodeFragment:
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +00001211 R = StringInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001212 Lex.Lex();
1213 break;
1214 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001215 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001216 Lex.Lex();
1217 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001218 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001219 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001220 std::string Name = Lex.getCurStrVal();
1221 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001222 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001223
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001224 // Value ::= ID '<' ValueListNE '>'
1225 if (Lex.Lex() == tgtok::greater) {
1226 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001227 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001228 }
David Greene8618f952009-06-08 20:23:18 +00001229
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001230 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1231 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1232 // body.
1233 Record *Class = Records.getClass(Name);
1234 if (!Class) {
1235 Error(NameLoc, "Expected a class name, got '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001236 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001237 }
David Greene8618f952009-06-08 20:23:18 +00001238
David Greeneaf8ee2c2011-07-29 22:43:06 +00001239 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
Craig Topper011817a2014-04-09 04:50:04 +00001240 if (ValueList.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001241
David Greene8618f952009-06-08 20:23:18 +00001242 if (Lex.getCode() != tgtok::greater) {
1243 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001244 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001245 }
1246 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001247 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001248
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001249 // Create the new record, set it as CurRec temporarily.
Alp Tokerce91fe52013-12-21 18:51:00 +00001250 Record *NewRec = new Record(GetNewAnonymousName(), NameLoc, Records,
Jordan Roseabdd99b2013-01-10 18:50:05 +00001251 /*IsAnonymous=*/true);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001252 SubClassReference SCRef;
Jordan Rosef12e8a92013-01-10 18:50:11 +00001253 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001254 SCRef.Rec = Class;
1255 SCRef.TemplateArgs = ValueList;
1256 // Add info about the subclass to NewRec.
Anton Yartsev671dff12014-08-08 00:29:54 +00001257 if (AddSubClass(NewRec, SCRef)) {
1258 delete NewRec;
Craig Topper011817a2014-04-09 04:50:04 +00001259 return nullptr;
Anton Yartsev671dff12014-08-08 00:29:54 +00001260 }
Hal Finkela8c1f462014-01-02 20:47:09 +00001261 if (!CurMultiClass) {
1262 NewRec->resolveReferences();
1263 Records.addDef(NewRec);
1264 } else {
Adam Nemete5a07162014-09-16 17:14:13 +00001265 // This needs to get resolved once the multiclass template arguments are
1266 // known before any use.
1267 NewRec->setResolveFirst(true);
Hal Finkela8c1f462014-01-02 20:47:09 +00001268 // Otherwise, we're inside a multiclass, add it to the multiclass.
1269 CurMultiClass->DefPrototypes.push_back(NewRec);
1270
1271 // Copy the template arguments for the multiclass into the def.
1272 const std::vector<Init *> &TArgs =
1273 CurMultiClass->Rec.getTemplateArgs();
1274
1275 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1276 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1277 assert(RV && "Template arg doesn't exist?");
1278 NewRec->addValue(*RV);
1279 }
1280
1281 // We can't return the prototype def here, instead return:
1282 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1283 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1284 assert(MCNameRV && "multiclass record must have a NAME");
1285
1286 return UnOpInit::get(UnOpInit::CAST,
1287 BinOpInit::get(BinOpInit::STRCONCAT,
1288 VarInit::get(MCNameRV->getName(),
1289 MCNameRV->getType()),
1290 NewRec->getNameInit(),
1291 StringRecTy::get()),
1292 Class->getDefInit()->getType());
1293 }
Bob Wilson7248f862009-11-22 04:24:42 +00001294
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001295 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001296 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001297 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001298 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001299 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001300 Lex.Lex(); // eat the '{'
David Greeneaf8ee2c2011-07-29 22:43:06 +00001301 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001302
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001303 if (Lex.getCode() != tgtok::r_brace) {
1304 Vals = ParseValueList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001305 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001306 }
1307 if (Lex.getCode() != tgtok::r_brace) {
1308 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001309 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001310 }
1311 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001312
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001313 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001314
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001315 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1316 // first. We'll first read everything in to a vector, then we can reverse
1317 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001318 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001319 // FIXME: The following two loops would not be duplicated
1320 // if the API was a little more orthogonal.
1321
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001322 // bits<n> values are allowed to initialize n bits.
1323 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1324 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1325 NewBits.push_back(BI->getBit((e - i) - 1));
1326 continue;
1327 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001328 // bits<n> can also come from variable initializers.
1329 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1330 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1331 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1332 NewBits.push_back(VI->getBit((e - i) - 1));
1333 continue;
1334 }
1335 // Fallthrough to try convert this to a bit.
1336 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001337 // All other values must be convertible to just a single bit.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001338 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001339 if (!Bit) {
Chris Lattner52416952007-11-22 21:06:59 +00001340 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1341 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001342 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001343 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001344 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001345 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001346 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001347 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001348 }
1349 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1350 Lex.Lex(); // eat the '['
David Greeneaf8ee2c2011-07-29 22:43:06 +00001351 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001352
Craig Topper011817a2014-04-09 04:50:04 +00001353 RecTy *DeducedEltTy = nullptr;
1354 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001355
Craig Topper011817a2014-04-09 04:50:04 +00001356 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001357 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001358 if (!ListType) {
Alp Tokere69170a2014-06-26 22:52:05 +00001359 std::string s;
1360 raw_string_ostream ss(s);
Reid Klecknerd78273f2013-08-06 22:51:21 +00001361 ss << "Type mismatch for list, expected list type, got "
1362 << ItemType->getAsString();
1363 TokError(ss.str());
Craig Topper011817a2014-04-09 04:50:04 +00001364 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001365 }
1366 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001367 }
David Greene8618f952009-06-08 20:23:18 +00001368
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001369 if (Lex.getCode() != tgtok::r_square) {
Craig Topper011817a2014-04-09 04:50:04 +00001370 Vals = ParseValueList(CurRec, nullptr,
1371 GivenListTy ? GivenListTy->getElementType() : nullptr);
1372 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001373 }
1374 if (Lex.getCode() != tgtok::r_square) {
1375 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001376 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001377 }
1378 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001379
Craig Topper011817a2014-04-09 04:50:04 +00001380 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001381 if (Lex.getCode() == tgtok::less) {
1382 // Optional list element type
1383 Lex.Lex(); // eat the '<'
1384
1385 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001386 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001387 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001388 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001389 }
1390
1391 if (Lex.getCode() != tgtok::greater) {
1392 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001393 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001394 }
1395 Lex.Lex(); // eat the '>'
1396 }
1397
1398 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001399 RecTy *EltTy = nullptr;
David Greeneaf8ee2c2011-07-29 22:43:06 +00001400 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greene8618f952009-06-08 20:23:18 +00001401 i != ie;
1402 ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +00001403 TypedInit *TArg = dyn_cast<TypedInit>(*i);
Craig Topper011817a2014-04-09 04:50:04 +00001404 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001405 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001406 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001407 }
Craig Topper011817a2014-04-09 04:50:04 +00001408 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001409 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001410 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001411 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001412 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001413 }
Bob Wilson7248f862009-11-22 04:24:42 +00001414 } else {
David Greene8618f952009-06-08 20:23:18 +00001415 EltTy = TArg->getType();
1416 }
1417 }
1418
Craig Topper011817a2014-04-09 04:50:04 +00001419 if (GivenEltTy) {
1420 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001421 // Verify consistency
1422 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1423 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001424 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001425 }
1426 }
1427 EltTy = GivenEltTy;
1428 }
1429
Craig Topper011817a2014-04-09 04:50:04 +00001430 if (!EltTy) {
1431 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001432 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001433 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001434 }
1435 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001436 } else {
David Greene8618f952009-06-08 20:23:18 +00001437 // Make sure the deduced type is compatible with the given type
1438 if (GivenListTy) {
1439 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1440 TokError("Element type mismatch for list");
Craig Topper011817a2014-04-09 04:50:04 +00001441 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001442 }
1443 }
1444 DeducedEltTy = EltTy;
1445 }
Bob Wilson7248f862009-11-22 04:24:42 +00001446
David Greenee32ebf22011-07-29 19:07:07 +00001447 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001448 }
1449 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1450 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001451 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001452 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001453 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001454 }
Bob Wilson7248f862009-11-22 04:24:42 +00001455
David Greeneaf8ee2c2011-07-29 22:43:06 +00001456 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001457 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001458
Nate Begemandbe3f772009-03-19 05:21:56 +00001459 // If the operator name is present, parse it.
1460 std::string OperatorName;
1461 if (Lex.getCode() == tgtok::colon) {
1462 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1463 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001464 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001465 }
1466 OperatorName = Lex.getCurStrVal();
1467 Lex.Lex(); // eat the VarName.
1468 }
Bob Wilson7248f862009-11-22 04:24:42 +00001469
David Greeneaf8ee2c2011-07-29 22:43:06 +00001470 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001471 if (Lex.getCode() != tgtok::r_paren) {
1472 DagArgs = ParseDagArgList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001473 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001474 }
Bob Wilson7248f862009-11-22 04:24:42 +00001475
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001476 if (Lex.getCode() != tgtok::r_paren) {
1477 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001478 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001479 }
1480 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001481
David Greenee32ebf22011-07-29 19:07:07 +00001482 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001483 }
Bob Wilson7248f862009-11-22 04:24:42 +00001484
David Greene2f7cf7f2011-01-07 17:05:37 +00001485 case tgtok::XHead:
1486 case tgtok::XTail:
1487 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001488 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001489 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001490 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001491 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +00001492 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001493 case tgtok::XSRL:
1494 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001495 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001496 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001497 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001498 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001499 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001500 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001501 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001502 }
1503 }
Bob Wilson7248f862009-11-22 04:24:42 +00001504
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001505 return R;
1506}
1507
1508/// ParseValue - Parse a tblgen value. This returns null on error.
1509///
1510/// Value ::= SimpleValue ValueSuffix*
1511/// ValueSuffix ::= '{' BitList '}'
1512/// ValueSuffix ::= '[' BitList ']'
1513/// ValueSuffix ::= '.' ID
1514///
David Greened4263a62011-10-19 13:04:20 +00001515Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1516 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001517 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001518
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001519 // Parse the suffixes now if present.
1520 while (1) {
1521 switch (Lex.getCode()) {
1522 default: return Result;
1523 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001524 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001525 // This is the beginning of the object body.
1526 return Result;
1527
Chris Lattner526c8cb2009-06-21 03:39:35 +00001528 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001529 Lex.Lex(); // eat the '{'
1530 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001531 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001532
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001533 // Reverse the bitlist.
1534 std::reverse(Ranges.begin(), Ranges.end());
1535 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001536 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001537 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001538 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001539 }
Bob Wilson7248f862009-11-22 04:24:42 +00001540
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001541 // Eat the '}'.
1542 if (Lex.getCode() != tgtok::r_brace) {
1543 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001544 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001545 }
1546 Lex.Lex();
1547 break;
1548 }
1549 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001550 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001551 Lex.Lex(); // eat the '['
1552 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001553 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001554
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001555 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001556 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001557 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001558 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001559 }
Bob Wilson7248f862009-11-22 04:24:42 +00001560
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001561 // Eat the ']'.
1562 if (Lex.getCode() != tgtok::r_square) {
1563 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001564 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001565 }
1566 Lex.Lex();
1567 break;
1568 }
1569 case tgtok::period:
1570 if (Lex.Lex() != tgtok::Id) { // eat the .
1571 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001572 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001573 }
1574 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001575 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001576 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001577 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001578 }
David Greenee32ebf22011-07-29 19:07:07 +00001579 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001580 Lex.Lex(); // eat field name
1581 break;
David Greene8e85b482011-10-19 13:04:43 +00001582
1583 case tgtok::paste:
1584 SMLoc PasteLoc = Lex.getLoc();
1585
1586 // Create a !strconcat() operation, first casting each operand to
1587 // a string if necessary.
1588
Sean Silvafb509ed2012-10-10 20:24:43 +00001589 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001590 if (!LHS) {
1591 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001592 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001593 }
1594
1595 if (LHS->getType() != StringRecTy::get()) {
1596 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1597 }
1598
Craig Topper011817a2014-04-09 04:50:04 +00001599 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001600
1601 Lex.Lex(); // Eat the '#'.
1602 switch (Lex.getCode()) {
1603 case tgtok::colon:
1604 case tgtok::semi:
1605 case tgtok::l_brace:
1606 // These are all of the tokens that can begin an object body.
1607 // Some of these can also begin values but we disallow those cases
1608 // because they are unlikely to be useful.
1609
1610 // Trailing paste, concat with an empty string.
1611 RHS = StringInit::get("");
1612 break;
1613
1614 default:
1615 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001616 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001617 if (!RHS) {
1618 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001619 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001620 }
1621
1622 if (RHS->getType() != StringRecTy::get()) {
1623 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1624 }
1625
1626 break;
1627 }
1628
1629 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1630 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1631 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001632 }
1633 }
1634}
1635
1636/// ParseDagArgList - Parse the argument list for a dag literal expression.
1637///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001638/// DagArg ::= Value (':' VARNAME)?
1639/// DagArg ::= VARNAME
1640/// DagArgList ::= DagArg
1641/// DagArgList ::= DagArgList ',' DagArg
David Greeneaf8ee2c2011-07-29 22:43:06 +00001642std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001643TGParser::ParseDagArgList(Record *CurRec) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001644 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson7248f862009-11-22 04:24:42 +00001645
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001646 while (1) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001647 // DagArg ::= VARNAME
1648 if (Lex.getCode() == tgtok::VarName) {
1649 // A missing value is treated like '?'.
1650 Result.push_back(std::make_pair(UnsetInit::get(), Lex.getCurStrVal()));
1651 Lex.Lex();
1652 } else {
1653 // DagArg ::= Value (':' VARNAME)?
1654 Init *Val = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001655 if (!Val)
David Greeneaf8ee2c2011-07-29 22:43:06 +00001656 return std::vector<std::pair<llvm::Init*, std::string> >();
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001657
1658 // If the variable name is present, add it.
1659 std::string VarName;
1660 if (Lex.getCode() == tgtok::colon) {
1661 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1662 TokError("expected variable name in dag literal");
1663 return std::vector<std::pair<llvm::Init*, std::string> >();
1664 }
1665 VarName = Lex.getCurStrVal();
1666 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001667 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001668
1669 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001670 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001671 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001672 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001673 }
Bob Wilson7248f862009-11-22 04:24:42 +00001674
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001675 return Result;
1676}
1677
1678
1679/// ParseValueList - Parse a comma separated list of values, returning them as a
1680/// vector. Note that this always expects to be able to parse at least one
1681/// value. It returns an empty list if this is not possible.
1682///
1683/// ValueList ::= Value (',' Value)
1684///
David Greeneaf8ee2c2011-07-29 22:43:06 +00001685std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopher71520a82011-07-11 23:06:52 +00001686 RecTy *EltTy) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001687 std::vector<Init*> Result;
David Greene8618f952009-06-08 20:23:18 +00001688 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001689 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001690 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001691 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001692 if (!TArgs.size()) {
1693 TokError("template argument provided to non-template class");
1694 return std::vector<Init*>();
1695 }
David Greene8618f952009-06-08 20:23:18 +00001696 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001697 if (!RV) {
1698 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1699 << ")\n";
1700 }
David Greene8618f952009-06-08 20:23:18 +00001701 assert(RV && "Template argument record not found??");
1702 ItemType = RV->getType();
1703 ++ArgN;
1704 }
1705 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001706 if (!Result.back()) return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001707
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001708 while (Lex.getCode() == tgtok::comma) {
1709 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001710
Craig Topper011817a2014-04-09 04:50:04 +00001711 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001712 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001713 if (ArgN >= TArgs.size()) {
1714 TokError("too many template arguments");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001715 return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001716 }
David Greene8618f952009-06-08 20:23:18 +00001717 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1718 assert(RV && "Template argument record not found??");
1719 ItemType = RV->getType();
1720 ++ArgN;
1721 }
1722 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001723 if (!Result.back()) return std::vector<Init*>();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001724 }
Bob Wilson7248f862009-11-22 04:24:42 +00001725
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001726 return Result;
1727}
1728
1729
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001730/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1731/// empty string on error. This can happen in a number of different context's,
1732/// including within a def or in the template args for a def (which which case
1733/// CurRec will be non-null) and within the template args for a multiclass (in
1734/// which case CurRec will be null, but CurMultiClass will be set). This can
1735/// also happen within a def that is within a multiclass, which will set both
1736/// CurRec and CurMultiClass.
1737///
1738/// Declaration ::= FIELD? Type ID ('=' Value)?
1739///
David Greenedb10e692011-10-19 13:02:42 +00001740Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001741 bool ParsingTemplateArgs) {
1742 // Read the field prefix if present.
1743 bool HasField = Lex.getCode() == tgtok::Field;
1744 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001745
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001746 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001747 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001748
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001749 if (Lex.getCode() != tgtok::Id) {
1750 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001751 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001752 }
Bob Wilson7248f862009-11-22 04:24:42 +00001753
Chris Lattner526c8cb2009-06-21 03:39:35 +00001754 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001755 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001756 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001757
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001758 if (ParsingTemplateArgs) {
1759 if (CurRec) {
David Greenedb10e692011-10-19 13:02:42 +00001760 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001761 } else {
1762 assert(CurMultiClass);
1763 }
1764 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001765 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1766 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001767 }
Bob Wilson7248f862009-11-22 04:24:42 +00001768
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001769 // Add the value.
1770 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001771 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001772
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001773 // If a value is present, parse it.
1774 if (Lex.getCode() == tgtok::equal) {
1775 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001776 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001777 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001778 if (!Val ||
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001779 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001780 // Return the name, even if an error is thrown. This is so that we can
1781 // continue to make some progress, even without the value having been
1782 // initialized.
1783 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001784 }
Bob Wilson7248f862009-11-22 04:24:42 +00001785
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001786 return DeclName;
1787}
1788
David Greenefb927af2012-02-22 16:09:41 +00001789/// ParseForeachDeclaration - Read a foreach declaration, returning
1790/// the name of the declared object or a NULL Init on error. Return
1791/// the name of the parsed initializer list through ForeachListName.
1792///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001793/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1794/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1795/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001796///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001797VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001798 if (Lex.getCode() != tgtok::Id) {
1799 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001800 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001801 }
1802
1803 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1804 Lex.Lex();
1805
1806 // If a value is present, parse it.
1807 if (Lex.getCode() != tgtok::equal) {
1808 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001809 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001810 }
1811 Lex.Lex(); // Eat the '='
1812
Craig Topper011817a2014-04-09 04:50:04 +00001813 RecTy *IterType = nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001814 std::vector<unsigned> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001815
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001816 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001817 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001818 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001819 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001820 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001821 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001822 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001823 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001824 }
1825 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001826 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001827 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001828 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001829 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001830 }
1831 IterType = ListType->getElementType();
1832 break;
David Greenefb927af2012-02-22 16:09:41 +00001833 }
1834
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001835 case tgtok::IntVal: { // RangePiece.
1836 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001837 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001838 break;
David Greenefb927af2012-02-22 16:09:41 +00001839 }
1840
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001841 case tgtok::l_brace: { // '{' RangeList '}'
1842 Lex.Lex(); // eat the '{'
1843 Ranges = ParseRangeList();
1844 if (Lex.getCode() != tgtok::r_brace) {
1845 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001846 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001847 }
1848 Lex.Lex();
1849 break;
1850 }
1851 }
David Greenefb927af2012-02-22 16:09:41 +00001852
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001853 if (!Ranges.empty()) {
1854 assert(!IterType && "Type already initialized?");
1855 IterType = IntRecTy::get();
1856 std::vector<Init*> Values;
1857 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
1858 Values.push_back(IntInit::get(Ranges[i]));
1859 ForeachListValue = ListInit::get(Values, IterType);
1860 }
1861
1862 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001863 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001864
1865 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001866}
1867
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001868/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1869/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1870/// template args for a def, which may or may not be in a multiclass. If null,
1871/// these are the template args for a multiclass.
1872///
1873/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001874///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001875bool TGParser::ParseTemplateArgList(Record *CurRec) {
1876 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1877 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001878
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001879 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001880
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001881 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001882 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001883 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001884 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001885
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001886 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001887
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001888 while (Lex.getCode() == tgtok::comma) {
1889 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001890
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001891 // Read the following declarations.
1892 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001893 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001894 return true;
1895 TheRecToAddTo->addTemplateArg(TemplArg);
1896 }
Bob Wilson7248f862009-11-22 04:24:42 +00001897
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001898 if (Lex.getCode() != tgtok::greater)
1899 return TokError("expected '>' at end of template argument list");
1900 Lex.Lex(); // eat the '>'.
1901 return false;
1902}
1903
1904
1905/// ParseBodyItem - Parse a single item at within the body of a def or class.
1906///
1907/// BodyItem ::= Declaration ';'
1908/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1909bool TGParser::ParseBodyItem(Record *CurRec) {
1910 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001911 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001912 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001913
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001914 if (Lex.getCode() != tgtok::semi)
1915 return TokError("expected ';' after declaration");
1916 Lex.Lex();
1917 return false;
1918 }
1919
1920 // LET ID OptionalRangeList '=' Value ';'
1921 if (Lex.Lex() != tgtok::Id)
1922 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001923
Chris Lattner526c8cb2009-06-21 03:39:35 +00001924 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001925 std::string FieldName = Lex.getCurStrVal();
1926 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001927
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001928 std::vector<unsigned> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001929 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001930 return true;
1931 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001932
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001933 if (Lex.getCode() != tgtok::equal)
1934 return TokError("expected '=' in let expression");
1935 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001936
David Greene8618f952009-06-08 20:23:18 +00001937 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001938 if (!Field)
David Greene8618f952009-06-08 20:23:18 +00001939 return TokError("Value '" + FieldName + "' unknown!");
1940
1941 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001942
David Greeneaf8ee2c2011-07-29 22:43:06 +00001943 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001944 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001945
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001946 if (Lex.getCode() != tgtok::semi)
1947 return TokError("expected ';' after let expression");
1948 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001949
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001950 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1951}
1952
1953/// ParseBody - Read the body of a class or def. Return true on error, false on
1954/// success.
1955///
1956/// Body ::= ';'
1957/// Body ::= '{' BodyList '}'
1958/// BodyList BodyItem*
1959///
1960bool TGParser::ParseBody(Record *CurRec) {
1961 // If this is a null definition, just eat the semi and return.
1962 if (Lex.getCode() == tgtok::semi) {
1963 Lex.Lex();
1964 return false;
1965 }
Bob Wilson7248f862009-11-22 04:24:42 +00001966
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001967 if (Lex.getCode() != tgtok::l_brace)
1968 return TokError("Expected ';' or '{' to start body");
1969 // Eat the '{'.
1970 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001971
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001972 while (Lex.getCode() != tgtok::r_brace)
1973 if (ParseBodyItem(CurRec))
1974 return true;
1975
1976 // Eat the '}'.
1977 Lex.Lex();
1978 return false;
1979}
1980
Sean Silvacb1a75e2013-01-09 04:49:14 +00001981/// \brief Apply the current let bindings to \a CurRec.
1982/// \returns true on error, false otherwise.
1983bool TGParser::ApplyLetStack(Record *CurRec) {
1984 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1985 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1986 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1987 LetStack[i][j].Bits, LetStack[i][j].Value))
1988 return true;
1989 return false;
1990}
1991
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001992/// ParseObjectBody - Parse the body of a def or class. This consists of an
1993/// optional ClassList followed by a Body. CurRec is the current def or class
1994/// that is being parsed.
1995///
1996/// ObjectBody ::= BaseClassList Body
1997/// BaseClassList ::= /*empty*/
1998/// BaseClassList ::= ':' BaseClassListNE
1999/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
2000///
2001bool TGParser::ParseObjectBody(Record *CurRec) {
2002 // If there is a baseclass list, read it.
2003 if (Lex.getCode() == tgtok::colon) {
2004 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002005
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002006 // Read all of the subclasses.
2007 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
2008 while (1) {
2009 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002010 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002011
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002012 // Add it.
2013 if (AddSubClass(CurRec, SubClass))
2014 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002015
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002016 if (Lex.getCode() != tgtok::comma) break;
2017 Lex.Lex(); // eat ','.
2018 SubClass = ParseSubClassReference(CurRec, false);
2019 }
2020 }
2021
Sean Silvacb1a75e2013-01-09 04:49:14 +00002022 if (ApplyLetStack(CurRec))
2023 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002024
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002025 return ParseBody(CurRec);
2026}
2027
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002028/// ParseDef - Parse and return a top level or multiclass def, return the record
2029/// corresponding to it. This returns null on error.
2030///
2031/// DefInst ::= DEF ObjectName ObjectBody
2032///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002033bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00002034 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002035 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00002036 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002037
2038 // Parse ObjectName and make a record for it.
Jordan Roseabdd99b2013-01-10 18:50:05 +00002039 Record *CurRec;
Anton Yartsev671dff12014-08-08 00:29:54 +00002040 bool CurRecOwnershipTransferred = false;
Jordan Roseabdd99b2013-01-10 18:50:05 +00002041 Init *Name = ParseObjectName(CurMultiClass);
2042 if (Name)
2043 CurRec = new Record(Name, DefLoc, Records);
2044 else
2045 CurRec = new Record(GetNewAnonymousName(), DefLoc, Records,
2046 /*IsAnonymous=*/true);
Bob Wilson7248f862009-11-22 04:24:42 +00002047
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002048 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002049 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002050
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002051 // Ensure redefinition doesn't happen.
David Greeneebb006f2012-01-28 00:03:24 +00002052 if (Records.getDef(CurRec->getNameInitAsString())) {
David Greene3a20f5a2011-10-19 13:03:45 +00002053 Error(DefLoc, "def '" + CurRec->getNameInitAsString()
2054 + "' already defined");
Anton Yartsev671dff12014-08-08 00:29:54 +00002055 delete CurRec;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002056 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002057 }
2058 Records.addDef(CurRec);
Anton Yartsev671dff12014-08-08 00:29:54 +00002059 CurRecOwnershipTransferred = true;
Hal Finkela8c1f462014-01-02 20:47:09 +00002060
2061 if (ParseObjectBody(CurRec))
2062 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002063 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002064 // Parse the body before adding this prototype to the DefPrototypes vector.
2065 // That way implicit definitions will be added to the DefPrototypes vector
2066 // before this object, instantiated prior to defs derived from this object,
2067 // and this available for indirect name resolution when defs derived from
2068 // this object are instantiated.
Anton Yartsev671dff12014-08-08 00:29:54 +00002069 if (ParseObjectBody(CurRec)) {
2070 delete CurRec;
Hal Finkela8c1f462014-01-02 20:47:09 +00002071 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002072 }
Hal Finkela8c1f462014-01-02 20:47:09 +00002073
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002074 // Otherwise, a def inside a multiclass, add it to the multiclass.
2075 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene07e055f2011-10-19 13:03:51 +00002076 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2077 == CurRec->getNameInit()) {
2078 Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002079 "' already defined in this multiclass!");
Anton Yartsev671dff12014-08-08 00:29:54 +00002080 delete CurRec;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002081 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002082 }
2083 CurMultiClass->DefPrototypes.push_back(CurRec);
Anton Yartsev671dff12014-08-08 00:29:54 +00002084 CurRecOwnershipTransferred = true;
2085 } else if (ParseObjectBody(CurRec)) {
2086 delete CurRec;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002087 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002088 }
Bob Wilson7248f862009-11-22 04:24:42 +00002089
Craig Topper011817a2014-04-09 04:50:04 +00002090 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002091 // See Record::setName(). This resolve step will see any new name
2092 // for the def that might have been created when resolving
2093 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002094 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002095
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002096 // If ObjectBody has template arguments, it's an error.
2097 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002098
2099 if (CurMultiClass) {
2100 // Copy the template arguments for the multiclass into the def.
David Greenedb10e692011-10-19 13:02:42 +00002101 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002102 CurMultiClass->Rec.getTemplateArgs();
2103
2104 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2105 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
2106 assert(RV && "Template arg doesn't exist?");
2107 CurRec->addValue(*RV);
2108 }
2109 }
2110
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002111 if (ProcessForeachDefs(CurRec, DefLoc)) {
David Greenefb927af2012-02-22 16:09:41 +00002112 Error(DefLoc,
2113 "Could not process loops for def" + CurRec->getNameInitAsString());
Anton Yartsev671dff12014-08-08 00:29:54 +00002114 if (!CurRecOwnershipTransferred)
2115 delete CurRec;
David Greenefb927af2012-02-22 16:09:41 +00002116 return true;
2117 }
2118
Anton Yartsev671dff12014-08-08 00:29:54 +00002119 if (!CurRecOwnershipTransferred)
2120 delete CurRec;
David Greenefb927af2012-02-22 16:09:41 +00002121 return false;
2122}
2123
2124/// ParseForeach - Parse a for statement. Return the record corresponding
2125/// to it. This returns true on error.
2126///
2127/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2128/// Foreach ::= FOREACH Declaration IN Object
2129///
2130bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2131 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2132 Lex.Lex(); // Eat the 'for' token.
2133
2134 // Make a temporary object to record items associated with the for
2135 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002136 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002137 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002138 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002139 return TokError("expected declaration in for");
2140
2141 if (Lex.getCode() != tgtok::In)
2142 return TokError("Unknown tok");
2143 Lex.Lex(); // Eat the in
2144
2145 // Create a loop object and remember it.
2146 Loops.push_back(ForeachLoop(IterName, ListValue));
2147
2148 if (Lex.getCode() != tgtok::l_brace) {
2149 // FOREACH Declaration IN Object
2150 if (ParseObject(CurMultiClass))
2151 return true;
2152 }
2153 else {
2154 SMLoc BraceLoc = Lex.getLoc();
2155 // Otherwise, this is a group foreach.
2156 Lex.Lex(); // eat the '{'.
2157
2158 // Parse the object list.
2159 if (ParseObjectList(CurMultiClass))
2160 return true;
2161
2162 if (Lex.getCode() != tgtok::r_brace) {
2163 TokError("expected '}' at end of foreach command");
2164 return Error(BraceLoc, "to match this '{'");
2165 }
2166 Lex.Lex(); // Eat the }
2167 }
2168
2169 // We've processed everything in this loop.
2170 Loops.pop_back();
2171
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002172 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002173}
2174
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002175/// ParseClass - Parse a tblgen class definition.
2176///
2177/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2178///
2179bool TGParser::ParseClass() {
2180 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2181 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002182
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002183 if (Lex.getCode() != tgtok::Id)
2184 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002185
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002186 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2187 if (CurRec) {
2188 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002189 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002190 !CurRec->getSuperClasses().empty() ||
2191 !CurRec->getTemplateArgs().empty())
David Greene8eed9982011-10-19 13:03:58 +00002192 return TokError("Class '" + CurRec->getNameInitAsString()
2193 + "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002194 } else {
2195 // If this is the first reference to this class, create and add it.
Chris Lattner89dcb682010-12-15 04:48:22 +00002196 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002197 Records.addClass(CurRec);
2198 }
2199 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002200
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002201 // If there are template args, parse them.
2202 if (Lex.getCode() == tgtok::less)
2203 if (ParseTemplateArgList(CurRec))
2204 return true;
2205
2206 // Finally, parse the object body.
2207 return ParseObjectBody(CurRec);
2208}
2209
2210/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2211/// of LetRecords.
2212///
2213/// LetList ::= LetItem (',' LetItem)*
2214/// LetItem ::= ID OptionalRangeList '=' Value
2215///
2216std::vector<LetRecord> TGParser::ParseLetList() {
2217 std::vector<LetRecord> Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002218
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002219 while (1) {
2220 if (Lex.getCode() != tgtok::Id) {
2221 TokError("expected identifier in let definition");
2222 return std::vector<LetRecord>();
2223 }
2224 std::string Name = Lex.getCurStrVal();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002225 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002226 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002227
2228 // Check for an optional RangeList.
2229 std::vector<unsigned> Bits;
Bob Wilson7248f862009-11-22 04:24:42 +00002230 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002231 return std::vector<LetRecord>();
2232 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002233
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002234 if (Lex.getCode() != tgtok::equal) {
2235 TokError("expected '=' in let expression");
2236 return std::vector<LetRecord>();
2237 }
2238 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002239
Craig Topper011817a2014-04-09 04:50:04 +00002240 Init *Val = ParseValue(nullptr);
2241 if (!Val) return std::vector<LetRecord>();
Bob Wilson7248f862009-11-22 04:24:42 +00002242
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002243 // Now that we have everything, add the record.
2244 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson7248f862009-11-22 04:24:42 +00002245
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002246 if (Lex.getCode() != tgtok::comma)
2247 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002248 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002249 }
2250}
2251
2252/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002253/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002254///
2255/// Object ::= LET LetList IN '{' ObjectList '}'
2256/// Object ::= LET LetList IN Object
2257///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002258bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002259 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2260 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002261
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002262 // Add this entry to the let stack.
2263 std::vector<LetRecord> LetInfo = ParseLetList();
2264 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002265 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002266
2267 if (Lex.getCode() != tgtok::In)
2268 return TokError("expected 'in' at end of top-level 'let'");
2269 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002270
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002271 // If this is a scalar let, just handle it now
2272 if (Lex.getCode() != tgtok::l_brace) {
2273 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002274 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002275 return true;
2276 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002277 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002278 // Otherwise, this is a group let.
2279 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002280
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002281 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002282 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002283 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002284
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002285 if (Lex.getCode() != tgtok::r_brace) {
2286 TokError("expected '}' at end of top level let command");
2287 return Error(BraceLoc, "to match this '{'");
2288 }
2289 Lex.Lex();
2290 }
Bob Wilson7248f862009-11-22 04:24:42 +00002291
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002292 // Outside this let scope, this let block is not active.
2293 LetStack.pop_back();
2294 return false;
2295}
2296
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002297/// ParseMultiClass - Parse a multiclass definition.
2298///
Bob Wilson3d948162009-04-28 19:41:44 +00002299/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002300/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2301/// MultiClassObject ::= DefInst
2302/// MultiClassObject ::= MultiClassInst
2303/// MultiClassObject ::= DefMInst
2304/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2305/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002306///
2307bool TGParser::ParseMultiClass() {
2308 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2309 Lex.Lex(); // Eat the multiclass token.
2310
2311 if (Lex.getCode() != tgtok::Id)
2312 return TokError("expected identifier after multiclass for name");
2313 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002314
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002315 if (MultiClasses.count(Name))
2316 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002317
Chris Lattner77d369c2010-12-13 00:23:57 +00002318 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
2319 Lex.getLoc(), Records);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002320 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002321
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002322 // If there are template args, parse them.
2323 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002324 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002325 return true;
2326
David Greene7049e792009-04-24 16:55:41 +00002327 bool inherits = false;
2328
David Greene753ed8f2009-04-22 16:42:54 +00002329 // If there are submulticlasses, parse them.
2330 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002331 inherits = true;
2332
David Greene753ed8f2009-04-22 16:42:54 +00002333 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002334
David Greene753ed8f2009-04-22 16:42:54 +00002335 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002336 SubMultiClassReference SubMultiClass =
2337 ParseSubMultiClassReference(CurMultiClass);
David Greene753ed8f2009-04-22 16:42:54 +00002338 while (1) {
2339 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002340 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002341
David Greene753ed8f2009-04-22 16:42:54 +00002342 // Add it.
2343 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2344 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002345
David Greene753ed8f2009-04-22 16:42:54 +00002346 if (Lex.getCode() != tgtok::comma) break;
2347 Lex.Lex(); // eat ','.
2348 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2349 }
2350 }
2351
David Greene7049e792009-04-24 16:55:41 +00002352 if (Lex.getCode() != tgtok::l_brace) {
2353 if (!inherits)
2354 return TokError("expected '{' in multiclass definition");
Bob Wilson7248f862009-11-22 04:24:42 +00002355 else if (Lex.getCode() != tgtok::semi)
2356 return TokError("expected ';' in multiclass definition");
David Greene7049e792009-04-24 16:55:41 +00002357 else
Bob Wilson7248f862009-11-22 04:24:42 +00002358 Lex.Lex(); // eat the ';'.
2359 } else {
David Greene7049e792009-04-24 16:55:41 +00002360 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2361 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002362
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002363 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002364 switch (Lex.getCode()) {
2365 default:
David Greene33f61992011-10-07 18:25:05 +00002366 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002367 case tgtok::Let:
2368 case tgtok::Def:
2369 case tgtok::Defm:
David Greenefb927af2012-02-22 16:09:41 +00002370 case tgtok::Foreach:
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002371 if (ParseObject(CurMultiClass))
2372 return true;
2373 break;
2374 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002375 }
David Greene7049e792009-04-24 16:55:41 +00002376 Lex.Lex(); // eat the '}'.
2377 }
Bob Wilson7248f862009-11-22 04:24:42 +00002378
Craig Topper011817a2014-04-09 04:50:04 +00002379 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002380 return false;
2381}
2382
David Greenedb445972011-10-05 22:42:07 +00002383Record *TGParser::
2384InstantiateMulticlassDef(MultiClass &MC,
2385 Record *DefProto,
Hal Finkelf2a0b2b2014-01-02 19:35:33 +00002386 Init *&DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002387 SMRange DefmPrefixRange) {
David Greene5d5d88c2011-10-19 13:04:31 +00002388 // We need to preserve DefProto so it can be reused for later
2389 // instantiations, so create a new Record to inherit from it.
2390
David Greenedb445972011-10-05 22:42:07 +00002391 // Add in the defm name. If the defm prefix is empty, give each
2392 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2393 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2394 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002395
Jordan Roseabdd99b2013-01-10 18:50:05 +00002396 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002397 if (!DefmPrefix) {
David Greene5d5d88c2011-10-19 13:04:31 +00002398 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002399 IsAnonymous = true;
2400 }
David Greene5d5d88c2011-10-19 13:04:31 +00002401
2402 Init *DefName = DefProto->getNameInit();
2403
Sean Silvafb509ed2012-10-10 20:24:43 +00002404 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002405
Craig Topper011817a2014-04-09 04:50:04 +00002406 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002407 // We have a fully expanded string so there are no operators to
2408 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002409 DefName =
2410 BinOpInit::get(BinOpInit::STRCONCAT,
2411 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2412 StringRecTy::get())->Fold(DefProto, &MC),
2413 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2414 }
David Greene5d5d88c2011-10-19 13:04:31 +00002415
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002416 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002417 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002418 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002419 Record *CurRec = new Record(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002420
2421 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002422 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002423 Ref.Rec = DefProto;
2424 AddSubClass(CurRec, Ref);
2425
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002426 // Set the value for NAME. We don't resolve references to it 'til later,
2427 // though, so that uses in nested multiclass names don't get
2428 // confused.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002429 if (SetValue(CurRec, Ref.RefRange.Start, "NAME", std::vector<unsigned>(),
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002430 DefmPrefix)) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002431 Error(DefmPrefixRange.Start, "Could not resolve "
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002432 + CurRec->getNameInitAsString() + ":NAME to '"
2433 + DefmPrefix->getAsUnquotedString() + "'");
Anton Yartsev671dff12014-08-08 00:29:54 +00002434 delete CurRec;
Craig Topper011817a2014-04-09 04:50:04 +00002435 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002436 }
David Greene8bf0d722011-10-19 13:04:35 +00002437
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002438 // If the DefNameString didn't resolve, we probably have a reference to
2439 // NAME and need to replace it. We need to do at least this much greedily,
2440 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002441 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002442 RecordVal *DefNameRV = CurRec->getValue("NAME");
2443 CurRec->resolveReferencesTo(DefNameRV);
2444 }
2445
2446 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002447 // Now that we're at the top level, resolve all NAME references
2448 // in the resultant defs that weren't in the def names themselves.
2449 RecordVal *DefNameRV = CurRec->getValue("NAME");
2450 CurRec->resolveReferencesTo(DefNameRV);
2451
2452 // Now that NAME references are resolved and we're at the top level of
2453 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002454 // currently in a multiclass, it means this defm appears inside a
2455 // multiclass and its name won't be fully resolvable until we see
2456 // the top-level defm. Therefore, we don't add this to the
2457 // RecordKeeper at this point. If we did we could get duplicate
2458 // defs as more than one probably refers to NAME or some other
2459 // common internal placeholder.
2460
2461 // Ensure redefinition doesn't happen.
2462 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002463 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002464 "' already defined, instantiating defm with subdef '" +
2465 DefProto->getNameInitAsString() + "'");
Anton Yartsev671dff12014-08-08 00:29:54 +00002466 delete CurRec;
Craig Topper011817a2014-04-09 04:50:04 +00002467 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002468 }
2469
2470 Records.addDef(CurRec);
2471 }
2472
David Greenedb445972011-10-05 22:42:07 +00002473 return CurRec;
2474}
2475
2476bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2477 Record *CurRec,
2478 SMLoc DefmPrefixLoc,
2479 SMLoc SubClassLoc,
David Greenedb10e692011-10-19 13:02:42 +00002480 const std::vector<Init *> &TArgs,
David Greenedb445972011-10-05 22:42:07 +00002481 std::vector<Init *> &TemplateVals,
2482 bool DeleteArgs) {
2483 // Loop over all of the template arguments, setting them to the specified
2484 // value or leaving them as the default if necessary.
2485 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2486 // Check if a value is specified for this temp-arg.
2487 if (i < TemplateVals.size()) {
2488 // Set it now.
2489 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2490 TemplateVals[i]))
2491 return true;
2492
2493 // Resolve it next.
2494 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2495
2496 if (DeleteArgs)
2497 // Now remove it.
2498 CurRec->removeValue(TArgs[i]);
2499
2500 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2501 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenedb10e692011-10-19 13:02:42 +00002502 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2503 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2504 + "'");
David Greenedb445972011-10-05 22:42:07 +00002505 }
2506 }
2507 return false;
2508}
2509
2510bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2511 Record *CurRec,
2512 Record *DefProto,
2513 SMLoc DefmPrefixLoc) {
2514 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002515 if (ApplyLetStack(CurRec))
2516 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002517
David Greenedb445972011-10-05 22:42:07 +00002518 // Don't create a top level definition for defm inside multiclasses,
2519 // instead, only update the prototypes and bind the template args
2520 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002521 if (!CurMultiClass)
2522 return false;
2523 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2524 i != e; ++i)
2525 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2526 == CurRec->getNameInit())
2527 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2528 "' already defined in this multiclass!");
2529 CurMultiClass->DefPrototypes.push_back(CurRec);
David Greenedb445972011-10-05 22:42:07 +00002530
Sean Silvacc951b22013-01-09 05:28:12 +00002531 // Copy the template arguments for the multiclass into the new def.
2532 const std::vector<Init *> &TA =
2533 CurMultiClass->Rec.getTemplateArgs();
David Greenedb445972011-10-05 22:42:07 +00002534
Sean Silvacc951b22013-01-09 05:28:12 +00002535 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2536 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2537 assert(RV && "Template arg doesn't exist?");
2538 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002539 }
2540
2541 return false;
2542}
2543
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002544/// ParseDefm - Parse the instantiation of a multiclass.
2545///
2546/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2547///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002548bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002549 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002550 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002551 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002552
Craig Topperb21afc62013-01-07 05:09:33 +00002553 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002554 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002555 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002556
Jordan Rosef12e8a92013-01-10 18:50:11 +00002557 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002558 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002559 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002560
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002561 // Keep track of the new generated record definitions.
2562 std::vector<Record*> NewRecDefs;
2563
2564 // This record also inherits from a regular class (non-multiclass)?
2565 bool InheritFromClass = false;
2566
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002567 // eat the colon.
2568 Lex.Lex();
2569
Chris Lattner526c8cb2009-06-21 03:39:35 +00002570 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002571 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002572
2573 while (1) {
Craig Topper011817a2014-04-09 04:50:04 +00002574 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002575
2576 // To instantiate a multiclass, we need to first get the multiclass, then
2577 // instantiate each def contained in the multiclass with the SubClassRef
2578 // template parameters.
2579 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2580 assert(MC && "Didn't lookup multiclass correctly?");
David Greeneaf8ee2c2011-07-29 22:43:06 +00002581 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002582
2583 // Verify that the correct number of template arguments were specified.
David Greenedb10e692011-10-19 13:02:42 +00002584 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002585 if (TArgs.size() < TemplateVals.size())
2586 return Error(SubClassLoc,
2587 "more template args specified than multiclass expects");
2588
2589 // Loop over all the def's in the multiclass, instantiating each one.
2590 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2591 Record *DefProto = MC->DefPrototypes[i];
2592
Jordan Rosef12e8a92013-01-10 18:50:11 +00002593 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix,
2594 SMRange(DefmLoc,
2595 DefmPrefixEndLoc));
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002596 if (!CurRec)
2597 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002598
Jordan Rosef12e8a92013-01-10 18:50:11 +00002599 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002600 TArgs, TemplateVals, true/*Delete args*/))
2601 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002602
Jordan Rosef12e8a92013-01-10 18:50:11 +00002603 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002604 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002605
Adam Nemete5a07162014-09-16 17:14:13 +00002606 // Defs that can be used by other definitions should be fully resolved
2607 // before any use.
2608 if (DefProto->isResolveFirst() && !CurMultiClass) {
2609 CurRec->resolveReferences();
2610 CurRec->setResolveFirst(false);
2611 }
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002612 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002613 }
2614
David Greenedb445972011-10-05 22:42:07 +00002615
David Greenef00919a2009-04-22 22:17:51 +00002616 if (Lex.getCode() != tgtok::comma) break;
2617 Lex.Lex(); // eat ','.
2618
Craig Topper998a39a2013-08-20 04:22:09 +00002619 if (Lex.getCode() != tgtok::Id)
2620 return TokError("expected identifier");
2621
David Greenef00919a2009-04-22 22:17:51 +00002622 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002623
2624 // A defm can inherit from regular classes (non-multiclass) as
2625 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002626 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002627
2628 if (InheritFromClass)
2629 break;
2630
Craig Topper011817a2014-04-09 04:50:04 +00002631 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002632 }
2633
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002634 if (InheritFromClass) {
2635 // Process all the classes to inherit as if they were part of a
2636 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002637 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002638 while (1) {
2639 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002640 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002641
2642 // Get the expanded definition prototypes and teach them about
2643 // the record values the current class to inherit has
2644 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2645 Record *CurRec = NewRecDefs[i];
2646
2647 // Add it.
2648 if (AddSubClass(CurRec, SubClass))
2649 return true;
2650
Sean Silvacb1a75e2013-01-09 04:49:14 +00002651 if (ApplyLetStack(CurRec))
2652 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002653 }
2654
2655 if (Lex.getCode() != tgtok::comma) break;
2656 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002657 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002658 }
2659 }
2660
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002661 if (!CurMultiClass)
2662 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene50c09122011-08-10 18:27:46 +00002663 // See Record::setName(). This resolve step will see any new
2664 // name for the def that might have been created when resolving
2665 // inheritance, values and arguments above.
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002666 NewRecDefs[i]->resolveReferences();
2667
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002668 if (Lex.getCode() != tgtok::semi)
2669 return TokError("expected ';' at end of defm");
2670 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002671
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002672 return false;
2673}
2674
2675/// ParseObject
2676/// Object ::= ClassInst
2677/// Object ::= DefInst
2678/// Object ::= MultiClassInst
2679/// Object ::= DefMInst
2680/// Object ::= LETCommand '{' ObjectList '}'
2681/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002682bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002683 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002684 default:
2685 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002686 case tgtok::Let: return ParseTopLevelLet(MC);
2687 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002688 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002689 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002690 case tgtok::Class: return ParseClass();
2691 case tgtok::MultiClass: return ParseMultiClass();
2692 }
2693}
2694
2695/// ParseObjectList
2696/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002697bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002698 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002699 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002700 return true;
2701 }
2702 return false;
2703}
2704
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002705bool TGParser::ParseFile() {
2706 Lex.Lex(); // Prime the lexer.
2707 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002708
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002709 // If we have unread input at the end of the file, report it.
2710 if (Lex.getCode() == tgtok::Eof)
2711 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002712
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002713 return TokError("Unexpected input at top level");
2714}
2715