blob: 709c7a52b9f9fda327e1904a6db234b19f0ac1a5 [file] [log] [blame]
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001//===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Parser for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
14#include "TGParser.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallVector.h"
Chris Lattnerf4127dd2007-11-22 20:49:04 +000016#include "llvm/ADT/StringExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/Support/CommandLine.h"
18#include "llvm/TableGen/Record.h"
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000019#include <algorithm>
20#include <sstream>
Chris Lattnerf4127dd2007-11-22 20:49:04 +000021using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24// Support Code for the Semantic Actions.
25//===----------------------------------------------------------------------===//
26
27namespace llvm {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000028struct SubClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000029 SMRange RefRange;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000030 Record *Rec;
David Greeneaf8ee2c2011-07-29 22:43:06 +000031 std::vector<Init*> TemplateArgs;
Craig Topper011817a2014-04-09 04:50:04 +000032 SubClassReference() : Rec(nullptr) {}
David Greene7049e792009-04-24 16:55:41 +000033
Craig Topper011817a2014-04-09 04:50:04 +000034 bool isInvalid() const { return Rec == nullptr; }
Chris Lattnerf4127dd2007-11-22 20:49:04 +000035};
David Greene753ed8f2009-04-22 16:42:54 +000036
37struct SubMultiClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000038 SMRange RefRange;
David Greene753ed8f2009-04-22 16:42:54 +000039 MultiClass *MC;
David Greeneaf8ee2c2011-07-29 22:43:06 +000040 std::vector<Init*> TemplateArgs;
Craig Topper011817a2014-04-09 04:50:04 +000041 SubMultiClassReference() : MC(nullptr) {}
Bob Wilson3d948162009-04-28 19:41:44 +000042
Craig Topper011817a2014-04-09 04:50:04 +000043 bool isInvalid() const { return MC == nullptr; }
David Greene7049e792009-04-24 16:55:41 +000044 void dump() const;
David Greene753ed8f2009-04-22 16:42:54 +000045};
David Greene7049e792009-04-24 16:55:41 +000046
47void SubMultiClassReference::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000048 errs() << "Multiclass:\n";
Bob Wilson7248f862009-11-22 04:24:42 +000049
David Greene7049e792009-04-24 16:55:41 +000050 MC->dump();
Bob Wilson7248f862009-11-22 04:24:42 +000051
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000052 errs() << "Template args:\n";
David Greeneaf8ee2c2011-07-29 22:43:06 +000053 for (std::vector<Init *>::const_iterator i = TemplateArgs.begin(),
David Greene7049e792009-04-24 16:55:41 +000054 iend = TemplateArgs.end();
55 i != iend;
56 ++i) {
57 (*i)->dump();
58 }
59}
60
Chris Lattnerf4127dd2007-11-22 20:49:04 +000061} // end namespace llvm
62
Chris Lattner526c8cb2009-06-21 03:39:35 +000063bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Craig Topper011817a2014-04-09 04:50:04 +000064 if (!CurRec)
Chris Lattnerf4127dd2007-11-22 20:49:04 +000065 CurRec = &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +000066
Jakob Stoklund Olesen9d1c5ee2012-01-13 03:16:35 +000067 if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000068 // The value already exists in the class, treat this as a set.
69 if (ERV->setValue(RV.getValue()))
70 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
71 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson7248f862009-11-22 04:24:42 +000072 "previous definition of type '" +
Chris Lattnerf4127dd2007-11-22 20:49:04 +000073 ERV->getType()->getAsString() + "'");
74 } else {
75 CurRec->addValue(RV);
76 }
77 return false;
78}
79
80/// SetValue -
81/// Return true on error, false on success.
David Greene3ca42122011-10-19 13:02:39 +000082bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
David Greeneaf8ee2c2011-07-29 22:43:06 +000083 const std::vector<unsigned> &BitList, Init *V) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000084 if (!V) return false;
85
Craig Topper011817a2014-04-09 04:50:04 +000086 if (!CurRec) CurRec = &CurMultiClass->Rec;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000087
88 RecordVal *RV = CurRec->getValue(ValName);
Craig Topper011817a2014-04-09 04:50:04 +000089 if (!RV)
David Greene3ca42122011-10-19 13:02:39 +000090 return Error(Loc, "Value '" + ValName->getAsUnquotedString()
91 + "' unknown!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +000092
93 // Do not allow assignments like 'X = X'. This will just cause infinite loops
94 // in the resolution machinery.
95 if (BitList.empty())
Sean Silvafb509ed2012-10-10 20:24:43 +000096 if (VarInit *VI = dyn_cast<VarInit>(V))
David Greene3ca42122011-10-19 13:02:39 +000097 if (VI->getNameInit() == ValName)
Chris Lattnerf4127dd2007-11-22 20:49:04 +000098 return false;
Bob Wilson7248f862009-11-22 04:24:42 +000099
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000100 // If we are assigning to a subset of the bits in the value... then we must be
101 // assigning to a field of BitsRecTy, which must have a BitsInit
102 // initializer.
103 //
104 if (!BitList.empty()) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000105 BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue());
Craig Topper011817a2014-04-09 04:50:04 +0000106 if (!CurVal)
David Greene3ca42122011-10-19 13:02:39 +0000107 return Error(Loc, "Value '" + ValName->getAsUnquotedString()
108 + "' is not a bits type");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000109
110 // Convert the incoming value to a bits type of the appropriate size...
David Greeneaf8ee2c2011-07-29 22:43:06 +0000111 Init *BI = V->convertInitializerTo(BitsRecTy::get(BitList.size()));
Craig Topper011817a2014-04-09 04:50:04 +0000112 if (!BI) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000113 return Error(Loc, "Initializer is not compatible with bit range");
114 }
Bob Wilson7248f862009-11-22 04:24:42 +0000115
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000116 // We should have a BitsInit type now.
Sean Silvafb509ed2012-10-10 20:24:43 +0000117 BitsInit *BInit = dyn_cast<BitsInit>(BI);
Craig Toppere73658d2014-04-28 04:05:08 +0000118 assert(BInit != nullptr);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000119
David Greeneaf8ee2c2011-07-29 22:43:06 +0000120 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000121
122 // Loop over bits, assigning values as appropriate.
123 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
124 unsigned Bit = BitList[i];
David Greeneb3da8122011-07-29 19:07:00 +0000125 if (NewBits[Bit])
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000126 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
David Greene3ca42122011-10-19 13:02:39 +0000127 ValName->getAsUnquotedString() + "' more than once");
David Greeneb3da8122011-07-29 19:07:00 +0000128 NewBits[Bit] = BInit->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000129 }
130
131 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
Craig Topper011817a2014-04-09 04:50:04 +0000132 if (!NewBits[i])
David Greeneb3da8122011-07-29 19:07:00 +0000133 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000134
David Greenee32ebf22011-07-29 19:07:07 +0000135 V = BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000136 }
137
Pete Cooper040c6a62014-07-31 01:43:57 +0000138 if (RV->setValue(V)) {
139 std::string InitType = "";
140 if (BitsInit *BI = dyn_cast<BitsInit>(V)) {
141 InitType = (Twine("' of type bit initializer with length ") +
142 Twine(BI->getNumBits())).str();
143 }
David Greene3ca42122011-10-19 13:02:39 +0000144 return Error(Loc, "Value '" + ValName->getAsUnquotedString() + "' of type '"
145 + RV->getType()->getAsString() +
146 "' is incompatible with initializer '" + V->getAsString()
Pete Cooper040c6a62014-07-31 01:43:57 +0000147 + InitType
David Greene3ca42122011-10-19 13:02:39 +0000148 + "'");
Pete Cooper040c6a62014-07-31 01:43:57 +0000149 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000150 return false;
151}
152
153/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
154/// args as SubClass's template arguments.
Cedric Venetd1e179d2009-02-14 16:06:42 +0000155bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000156 Record *SC = SubClass.Rec;
157 // Add all of the values in the subclass into the current class.
158 const std::vector<RecordVal> &Vals = SC->getValues();
159 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
Jordan Rosef12e8a92013-01-10 18:50:11 +0000160 if (AddValue(CurRec, SubClass.RefRange.Start, Vals[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000161 return true;
162
David Greenedb10e692011-10-19 13:02:42 +0000163 const std::vector<Init *> &TArgs = SC->getTemplateArgs();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000164
165 // Ensure that an appropriate number of template arguments are specified.
166 if (TArgs.size() < SubClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000167 return Error(SubClass.RefRange.Start,
168 "More template args specified than expected");
Bob Wilson7248f862009-11-22 04:24:42 +0000169
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000170 // Loop over all of the template arguments, setting them to the specified
171 // value or leaving them as the default if necessary.
172 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
173 if (i < SubClass.TemplateArgs.size()) {
174 // If a value is specified for this template arg, set it now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000175 if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
176 std::vector<unsigned>(), SubClass.TemplateArgs[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000177 return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000178
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000179 // Resolve it next.
180 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson7248f862009-11-22 04:24:42 +0000181
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000182 // Now remove it.
183 CurRec->removeValue(TArgs[i]);
184
185 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000186 return Error(SubClass.RefRange.Start,
187 "Value not specified for template argument #"
David Greenedb10e692011-10-19 13:02:42 +0000188 + utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
189 + ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000190 }
191 }
192
193 // Since everything went well, we can now set the "superclass" list for the
194 // current record.
195 const std::vector<Record*> &SCs = SC->getSuperClasses();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000196 ArrayRef<SMRange> SCRanges = SC->getSuperClassRanges();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000197 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
198 if (CurRec->isSubClassOf(SCs[i]))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000199 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000200 "Already subclass of '" + SCs[i]->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000201 CurRec->addSuperClass(SCs[i], SCRanges[i]);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000202 }
Bob Wilson7248f862009-11-22 04:24:42 +0000203
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000204 if (CurRec->isSubClassOf(SC))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000205 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000206 "Already subclass of '" + SC->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000207 CurRec->addSuperClass(SC, SubClass.RefRange);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000208 return false;
209}
210
David Greene753ed8f2009-04-22 16:42:54 +0000211/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilsonf71e6562009-04-30 18:26:19 +0000212/// CurMC, resolving its template args as SubMultiClass's
David Greene753ed8f2009-04-22 16:42:54 +0000213/// template arguments.
Bob Wilsonf71e6562009-04-30 18:26:19 +0000214bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson92ab8202009-04-30 17:46:20 +0000215 SubMultiClassReference &SubMultiClass) {
David Greene753ed8f2009-04-22 16:42:54 +0000216 MultiClass *SMC = SubMultiClass.MC;
Bob Wilsonf71e6562009-04-30 18:26:19 +0000217 Record *CurRec = &CurMC->Rec;
David Greene753ed8f2009-04-22 16:42:54 +0000218
Bob Wilsonf71e6562009-04-30 18:26:19 +0000219 const std::vector<RecordVal> &MCVals = CurRec->getValues();
David Greene753ed8f2009-04-22 16:42:54 +0000220
221 // Add all of the values in the subclass into the current class.
222 const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues();
223 for (unsigned i = 0, e = SMCVals.size(); i != e; ++i)
Jordan Rosef12e8a92013-01-10 18:50:11 +0000224 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVals[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000225 return true;
226
Bob Wilsonf71e6562009-04-30 18:26:19 +0000227 int newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000228
David Greene753ed8f2009-04-22 16:42:54 +0000229 // Add all of the defs in the subclass into the current multiclass.
230 for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(),
231 iend = SMC->DefPrototypes.end();
232 i != iend;
233 ++i) {
234 // Clone the def and add it to the current multiclass
235 Record *NewDef = new Record(**i);
236
237 // Add all of the values in the superclass into the current def.
238 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
Jordan Rosef12e8a92013-01-10 18:50:11 +0000239 if (AddValue(NewDef, SubMultiClass.RefRange.Start, MCVals[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000240 return true;
241
Bob Wilsonf71e6562009-04-30 18:26:19 +0000242 CurMC->DefPrototypes.push_back(NewDef);
David Greene753ed8f2009-04-22 16:42:54 +0000243 }
Bob Wilson3d948162009-04-28 19:41:44 +0000244
David Greenedb10e692011-10-19 13:02:42 +0000245 const std::vector<Init *> &SMCTArgs = SMC->Rec.getTemplateArgs();
David Greene753ed8f2009-04-22 16:42:54 +0000246
David Greene7049e792009-04-24 16:55:41 +0000247 // Ensure that an appropriate number of template arguments are
248 // specified.
David Greene753ed8f2009-04-22 16:42:54 +0000249 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000250 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000251 "More template args specified than expected");
Bob Wilson3d948162009-04-28 19:41:44 +0000252
David Greene753ed8f2009-04-22 16:42:54 +0000253 // Loop over all of the template arguments, setting them to the specified
254 // value or leaving them as the default if necessary.
255 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
256 if (i < SubMultiClass.TemplateArgs.size()) {
David Greene7049e792009-04-24 16:55:41 +0000257 // If a value is specified for this template arg, set it in the
258 // superclass now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000259 if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson3d948162009-04-28 19:41:44 +0000260 std::vector<unsigned>(),
David Greene753ed8f2009-04-22 16:42:54 +0000261 SubMultiClass.TemplateArgs[i]))
262 return true;
263
264 // Resolve it next.
265 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson3d948162009-04-28 19:41:44 +0000266
David Greene753ed8f2009-04-22 16:42:54 +0000267 // Now remove it.
268 CurRec->removeValue(SMCTArgs[i]);
269
David Greene7049e792009-04-24 16:55:41 +0000270 // If a value is specified for this template arg, set it in the
271 // new defs now.
272 for (MultiClass::RecordVector::iterator j =
Bob Wilsonf71e6562009-04-30 18:26:19 +0000273 CurMC->DefPrototypes.begin() + newDefStart,
274 jend = CurMC->DefPrototypes.end();
David Greene753ed8f2009-04-22 16:42:54 +0000275 j != jend;
276 ++j) {
277 Record *Def = *j;
278
Jordan Rosef12e8a92013-01-10 18:50:11 +0000279 if (SetValue(Def, SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson3d948162009-04-28 19:41:44 +0000280 std::vector<unsigned>(),
David Greene753ed8f2009-04-22 16:42:54 +0000281 SubMultiClass.TemplateArgs[i]))
282 return true;
283
284 // Resolve it next.
285 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
286
287 // Now remove it
288 Def->removeValue(SMCTArgs[i]);
289 }
290 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000291 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000292 "Value not specified for template argument #"
David Greenedb10e692011-10-19 13:02:42 +0000293 + utostr(i) + " (" + SMCTArgs[i]->getAsUnquotedString()
294 + ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000295 }
296 }
297
298 return false;
299}
300
David Greenefb927af2012-02-22 16:09:41 +0000301/// ProcessForeachDefs - Given a record, apply all of the variable
302/// values in all surrounding foreach loops, creating new records for
303/// each combination of values.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000304bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
305 if (Loops.empty())
306 return false;
307
David Greenefb927af2012-02-22 16:09:41 +0000308 // We want to instantiate a new copy of CurRec for each combination
309 // of nested loop iterator values. We don't want top instantiate
310 // any copies until we have values for each loop iterator.
311 IterSet IterVals;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000312 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000313}
314
315/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
316/// apply each of the variable values in this loop and then process
317/// subloops.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000318bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
319 // Recursively build a tuple of iterator values.
320 if (IterVals.size() != Loops.size()) {
321 assert(IterVals.size() < Loops.size());
322 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silvafb509ed2012-10-10 20:24:43 +0000323 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Craig Topper011817a2014-04-09 04:50:04 +0000324 if (!List) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000325 Error(Loc, "Loop list is not a list");
326 return true;
327 }
David Greenefb927af2012-02-22 16:09:41 +0000328
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000329 // Process each value.
330 for (int64_t i = 0; i < List->getSize(); ++i) {
Craig Topper011817a2014-04-09 04:50:04 +0000331 Init *ItemVal = List->resolveListElementReference(*CurRec, nullptr, i);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000332 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
333 if (ProcessForeachDefs(CurRec, Loc, IterVals))
334 return true;
335 IterVals.pop_back();
336 }
337 return false;
338 }
339
340 // This is the bottom of the recursion. We have all of the iterator values
341 // for this point in the iteration space. Instantiate a new record to
342 // reflect this combination of values.
343 Record *IterRec = new Record(*CurRec);
344
345 // Set the iterator values now.
346 for (unsigned i = 0, e = IterVals.size(); i != e; ++i) {
347 VarInit *IterVar = IterVals[i].IterVar;
Sean Silvafb509ed2012-10-10 20:24:43 +0000348 TypedInit *IVal = dyn_cast<TypedInit>(IterVals[i].IterValue);
Craig Topper011817a2014-04-09 04:50:04 +0000349 if (!IVal) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000350 Error(Loc, "foreach iterator value is untyped");
351 return true;
352 }
353
354 IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false));
355
356 if (SetValue(IterRec, Loc, IterVar->getName(),
357 std::vector<unsigned>(), IVal)) {
358 Error(Loc, "when instantiating this def");
359 return true;
360 }
361
362 // Resolve it next.
363 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName()));
364
365 // Remove it.
366 IterRec->removeValue(IterVar->getName());
367 }
368
369 if (Records.getDef(IterRec->getNameInitAsString())) {
Artyom Skrobov8b985322014-06-10 12:41:14 +0000370 // If this record is anonymous, it's no problem, just generate a new name
371 if (IterRec->isAnonymous())
372 IterRec->setName(GetNewAnonymousName());
373 else {
374 Error(Loc, "def already exists: " + IterRec->getNameInitAsString());
375 return true;
376 }
David Greenefb927af2012-02-22 16:09:41 +0000377 }
378
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000379 Records.addDef(IterRec);
380 IterRec->resolveReferences();
David Greenefb927af2012-02-22 16:09:41 +0000381 return false;
382}
383
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000384//===----------------------------------------------------------------------===//
385// Parser Code
386//===----------------------------------------------------------------------===//
387
388/// isObjectStart - Return true if this is a valid first token for an Object.
389static bool isObjectStart(tgtok::TokKind K) {
390 return K == tgtok::Class || K == tgtok::Def ||
David Greenefb927af2012-02-22 16:09:41 +0000391 K == tgtok::Defm || K == tgtok::Let ||
392 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000393}
394
Alp Tokerce91fe52013-12-21 18:51:00 +0000395/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
396/// an identifier.
397std::string TGParser::GetNewAnonymousName() {
Michael J. Spencer2bb7db92013-02-26 21:29:47 +0000398 unsigned Tmp = AnonCounter++; // MSVC2012 ICEs without this.
Alp Tokerce91fe52013-12-21 18:51:00 +0000399 return "anonymous_" + utostr(Tmp);
Chris Lattner7538ed82010-10-05 22:51:56 +0000400}
401
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000402/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000403/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000404/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000405/// ObjectName ::= /*empty*/
406///
David Greene2affd672011-10-19 13:04:29 +0000407Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
408 switch (Lex.getCode()) {
409 case tgtok::colon:
410 case tgtok::semi:
411 case tgtok::l_brace:
412 // These are all of the tokens that can begin an object body.
413 // Some of these can also begin values but we disallow those cases
414 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000415 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000416 default:
417 break;
418 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000419
Craig Topper011817a2014-04-09 04:50:04 +0000420 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000421 if (CurMultiClass)
422 CurRec = &CurMultiClass->Rec;
423
Craig Topper011817a2014-04-09 04:50:04 +0000424 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000425 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000426 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000427 if (!CurRecName) {
428 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000429 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000430 }
431 Type = CurRecName->getType();
432 }
433
434 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000435}
436
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000437/// ParseClassID - Parse and resolve a reference to a class name. This returns
438/// null on error.
439///
440/// ClassID ::= ID
441///
442Record *TGParser::ParseClassID() {
443 if (Lex.getCode() != tgtok::Id) {
444 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000445 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000446 }
Bob Wilson7248f862009-11-22 04:24:42 +0000447
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000448 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000449 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000450 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000451
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000452 Lex.Lex();
453 return Result;
454}
455
Bob Wilson3d948162009-04-28 19:41:44 +0000456/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
457/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000458///
459/// MultiClassID ::= ID
460///
461MultiClass *TGParser::ParseMultiClassID() {
462 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000463 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000464 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000465 }
Bob Wilson3d948162009-04-28 19:41:44 +0000466
David Greene753ed8f2009-04-22 16:42:54 +0000467 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
Craig Topper011817a2014-04-09 04:50:04 +0000468 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000469 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000470
David Greene753ed8f2009-04-22 16:42:54 +0000471 Lex.Lex();
472 return Result;
473}
474
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000475/// ParseSubClassReference - Parse a reference to a subclass or to a templated
476/// subclass. This returns a SubClassRefTy with a null Record* on error.
477///
478/// SubClassRef ::= ClassID
479/// SubClassRef ::= ClassID '<' ValueList '>'
480///
481SubClassReference TGParser::
482ParseSubClassReference(Record *CurRec, bool isDefm) {
483 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000484 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000485
Sean Silva0657b402013-01-09 02:17:14 +0000486 if (isDefm) {
487 if (MultiClass *MC = ParseMultiClassID())
488 Result.Rec = &MC->Rec;
489 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000490 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000491 }
Craig Topper011817a2014-04-09 04:50:04 +0000492 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000493
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000494 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000495 if (Lex.getCode() != tgtok::less) {
496 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000497 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000498 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000499 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000500
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000501 if (Lex.getCode() == tgtok::greater) {
502 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000503 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000504 return Result;
505 }
Bob Wilson7248f862009-11-22 04:24:42 +0000506
David Greene8618f952009-06-08 20:23:18 +0000507 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000508 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000509 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000510 return Result;
511 }
Bob Wilson7248f862009-11-22 04:24:42 +0000512
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000513 if (Lex.getCode() != tgtok::greater) {
514 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000515 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000516 return Result;
517 }
518 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000519 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000520
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000521 return Result;
522}
523
Bob Wilson3d948162009-04-28 19:41:44 +0000524/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
525/// templated submulticlass. This returns a SubMultiClassRefTy with a null
526/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000527///
528/// SubMultiClassRef ::= MultiClassID
529/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
530///
531SubMultiClassReference TGParser::
532ParseSubMultiClassReference(MultiClass *CurMC) {
533 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000534 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000535
David Greene753ed8f2009-04-22 16:42:54 +0000536 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000537 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000538
David Greene753ed8f2009-04-22 16:42:54 +0000539 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000540 if (Lex.getCode() != tgtok::less) {
541 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000542 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000543 }
David Greene753ed8f2009-04-22 16:42:54 +0000544 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000545
David Greene753ed8f2009-04-22 16:42:54 +0000546 if (Lex.getCode() == tgtok::greater) {
547 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000548 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000549 return Result;
550 }
Bob Wilson3d948162009-04-28 19:41:44 +0000551
David Greene8618f952009-06-08 20:23:18 +0000552 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000553 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000554 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000555 return Result;
556 }
Bob Wilson3d948162009-04-28 19:41:44 +0000557
David Greene753ed8f2009-04-22 16:42:54 +0000558 if (Lex.getCode() != tgtok::greater) {
559 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000560 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000561 return Result;
562 }
563 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000564 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000565
566 return Result;
567}
568
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000569/// ParseRangePiece - Parse a bit/value range.
570/// RangePiece ::= INTVAL
571/// RangePiece ::= INTVAL '-' INTVAL
572/// RangePiece ::= INTVAL INTVAL
573bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000574 if (Lex.getCode() != tgtok::IntVal) {
575 TokError("expected integer or bitrange");
576 return true;
577 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000578 int64_t Start = Lex.getCurIntVal();
579 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000580
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000581 if (Start < 0)
582 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000583
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000584 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000585 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000586 Ranges.push_back(Start);
587 return false;
588 case tgtok::minus:
589 if (Lex.Lex() != tgtok::IntVal) {
590 TokError("expected integer value as end of range");
591 return true;
592 }
593 End = Lex.getCurIntVal();
594 break;
595 case tgtok::IntVal:
596 End = -Lex.getCurIntVal();
597 break;
598 }
Bob Wilson7248f862009-11-22 04:24:42 +0000599 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000600 return TokError("invalid range, cannot be negative");
601 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000602
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000603 // Add to the range.
604 if (Start < End) {
605 for (; Start <= End; ++Start)
606 Ranges.push_back(Start);
607 } else {
608 for (; Start >= End; --Start)
609 Ranges.push_back(Start);
610 }
611 return false;
612}
613
614/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
615///
616/// RangeList ::= RangePiece (',' RangePiece)*
617///
618std::vector<unsigned> TGParser::ParseRangeList() {
619 std::vector<unsigned> Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000620
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000621 // Parse the first piece.
622 if (ParseRangePiece(Result))
623 return std::vector<unsigned>();
624 while (Lex.getCode() == tgtok::comma) {
625 Lex.Lex(); // Eat the comma.
626
627 // Parse the next range piece.
628 if (ParseRangePiece(Result))
629 return std::vector<unsigned>();
630 }
631 return Result;
632}
633
634/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
635/// OptionalRangeList ::= '<' RangeList '>'
636/// OptionalRangeList ::= /*empty*/
637bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
638 if (Lex.getCode() != tgtok::less)
639 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000640
Chris Lattner526c8cb2009-06-21 03:39:35 +0000641 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000642 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000643
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000644 // Parse the range list.
645 Ranges = ParseRangeList();
646 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000647
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000648 if (Lex.getCode() != tgtok::greater) {
649 TokError("expected '>' at end of range list");
650 return Error(StartLoc, "to match this '<'");
651 }
652 Lex.Lex(); // eat the '>'.
653 return false;
654}
655
656/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
657/// OptionalBitList ::= '{' RangeList '}'
658/// OptionalBitList ::= /*empty*/
659bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
660 if (Lex.getCode() != tgtok::l_brace)
661 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000662
Chris Lattner526c8cb2009-06-21 03:39:35 +0000663 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000664 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000665
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000666 // Parse the range list.
667 Ranges = ParseRangeList();
668 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000669
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000670 if (Lex.getCode() != tgtok::r_brace) {
671 TokError("expected '}' at end of bit list");
672 return Error(StartLoc, "to match this '{'");
673 }
674 Lex.Lex(); // eat the '}'.
675 return false;
676}
677
678
679/// ParseType - Parse and return a tblgen type. This returns null on error.
680///
681/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000682/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000683/// Type ::= BIT // bit type
684/// Type ::= BITS '<' INTVAL '>' // bits<x> type
685/// Type ::= INT // int type
686/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000687/// Type ::= DAG // dag type
688/// Type ::= ClassID // Record Type
689///
690RecTy *TGParser::ParseType() {
691 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000692 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000693 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000694 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000695 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
696 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000697 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000698 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000699 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000700 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000701 case tgtok::Bits: {
702 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
703 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000704 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000705 }
706 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
707 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000708 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000709 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000710 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000711 if (Lex.Lex() != tgtok::greater) { // Eat count.
712 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000713 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000714 }
715 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000716 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000717 }
718 case tgtok::List: {
719 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
720 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000721 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000722 }
723 Lex.Lex(); // Eat '<'
724 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000725 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000726
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000727 if (Lex.getCode() != tgtok::greater) {
728 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000729 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000730 }
731 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000732 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000733 }
Bob Wilson7248f862009-11-22 04:24:42 +0000734 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000735}
736
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000737/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
738/// has already been read.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000739Init *TGParser::ParseIDValue(Record *CurRec,
David Greened4263a62011-10-19 13:04:20 +0000740 const std::string &Name, SMLoc NameLoc,
741 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000742 if (CurRec) {
743 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000744 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000745
David Greenedb10e692011-10-19 13:02:42 +0000746 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
747
David Greene47a665e2011-10-05 22:42:54 +0000748 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +0000749 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
750 "::");
David Greene47a665e2011-10-05 22:42:54 +0000751
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000752 if (CurRec->isTemplateArg(TemplateArgName)) {
753 const RecordVal *RV = CurRec->getValue(TemplateArgName);
754 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000755 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000756 }
757 }
Bob Wilson7248f862009-11-22 04:24:42 +0000758
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000759 if (CurMultiClass) {
David Greenedb10e692011-10-19 13:02:42 +0000760 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
761 "::");
762
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000763 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
764 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
765 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000766 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000767 }
768 }
Bob Wilson7248f862009-11-22 04:24:42 +0000769
David Greenefb927af2012-02-22 16:09:41 +0000770 // If this is in a foreach loop, make sure it's not a loop iterator
771 for (LoopVector::iterator i = Loops.begin(), iend = Loops.end();
772 i != iend;
773 ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000774 VarInit *IterVar = dyn_cast<VarInit>(i->IterVar);
David Greenefb927af2012-02-22 16:09:41 +0000775 if (IterVar && IterVar->getName() == Name)
776 return IterVar;
777 }
778
David Greene232bd602011-10-19 13:04:21 +0000779 if (Mode == ParseNameMode)
780 return StringInit::get(Name);
781
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000782 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000783 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000784
David Greene232bd602011-10-19 13:04:21 +0000785 if (Mode == ParseValueMode) {
786 Error(NameLoc, "Variable not defined: '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000787 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000788 }
789
790 return StringInit::get(Name);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000791}
792
David Greene5d0c0512009-05-14 20:54:48 +0000793/// ParseOperation - Parse an operator. This returns null on error.
794///
795/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
796///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000797Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000798 switch (Lex.getCode()) {
799 default:
800 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000801 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000802 case tgtok::XHead:
803 case tgtok::XTail:
804 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000805 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
806 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000807 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000808
David Greenee8f3b272009-05-14 21:22:49 +0000809 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000810 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000811 case tgtok::XCast:
812 Lex.Lex(); // eat the operation
813 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000814
David Greenee8f3b272009-05-14 21:22:49 +0000815 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000816
Craig Topper011817a2014-04-09 04:50:04 +0000817 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000818 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000819 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000820 }
David Greene5d0c0512009-05-14 20:54:48 +0000821
David Greenee8f3b272009-05-14 21:22:49 +0000822 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000823 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000824 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000825 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000826 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000827 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000828 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000829 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000830 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000831 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000832 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000833 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000834 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000835 break;
David Greenee8f3b272009-05-14 21:22:49 +0000836 }
837 if (Lex.getCode() != tgtok::l_paren) {
838 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000839 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000840 }
841 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000842
David Greeneaf8ee2c2011-07-29 22:43:06 +0000843 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000844 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000845
David Greene2f7cf7f2011-01-07 17:05:37 +0000846 if (Code == UnOpInit::HEAD
847 || Code == UnOpInit::TAIL
848 || Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000849 ListInit *LHSl = dyn_cast<ListInit>(LHS);
850 StringInit *LHSs = dyn_cast<StringInit>(LHS);
851 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000852 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000853 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000854 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000855 }
856 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000857 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
858 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000859 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000860 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000861 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000862 }
863 }
864
David Greene2f7cf7f2011-01-07 17:05:37 +0000865 if (Code == UnOpInit::HEAD
866 || Code == UnOpInit::TAIL) {
Craig Topper011817a2014-04-09 04:50:04 +0000867 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000868 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000869 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000870 }
Bob Wilson7248f862009-11-22 04:24:42 +0000871
David Greened571b3c2009-05-14 22:38:31 +0000872 if (LHSl && LHSl->getSize() == 0) {
873 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000874 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000875 }
876 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000877 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000878 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000879 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000880 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000881 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000882 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000883 if (Code == UnOpInit::HEAD) {
David Greened571b3c2009-05-14 22:38:31 +0000884 Type = Itemt->getType();
Bob Wilson7248f862009-11-22 04:24:42 +0000885 } else {
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000886 Type = ListRecTy::get(Itemt->getType());
David Greened571b3c2009-05-14 22:38:31 +0000887 }
Bob Wilson7248f862009-11-22 04:24:42 +0000888 } else {
David Greened571b3c2009-05-14 22:38:31 +0000889 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000890 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000891 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000892 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000893 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000894 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000895 if (Code == UnOpInit::HEAD) {
David Greened571b3c2009-05-14 22:38:31 +0000896 Type = LType->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +0000897 } else {
David Greened571b3c2009-05-14 22:38:31 +0000898 Type = LType;
899 }
900 }
901 }
902 }
903
David Greenee8f3b272009-05-14 21:22:49 +0000904 if (Lex.getCode() != tgtok::r_paren) {
905 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000906 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000907 }
908 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000909 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000910 }
David Greene5d0c0512009-05-14 20:54:48 +0000911
912 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000913 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000914 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +0000915 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000916 case tgtok::XSRL:
917 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000918 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000919 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000920 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000921 tgtok::TokKind OpTok = Lex.getCode();
922 SMLoc OpLoc = Lex.getLoc();
923 Lex.Lex(); // eat the operation
924
David Greene5d0c0512009-05-14 20:54:48 +0000925 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000926 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000927
Chris Lattner61ea00b2010-10-05 23:58:18 +0000928 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000929 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000930 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000931 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000932 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000933 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
934 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
935 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
936 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000937 case tgtok::XListConcat:
938 Code = BinOpInit::LISTCONCAT;
939 // We don't know the list type until we parse the first argument
940 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000941 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000942 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000943 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000944 break;
David Greene5d0c0512009-05-14 20:54:48 +0000945 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000946
David Greene5d0c0512009-05-14 20:54:48 +0000947 if (Lex.getCode() != tgtok::l_paren) {
948 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000949 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000950 }
951 Lex.Lex(); // eat the '('
952
David Greeneaf8ee2c2011-07-29 22:43:06 +0000953 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000954
Chris Lattner61ea00b2010-10-05 23:58:18 +0000955 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000956 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000957
Chris Lattner61ea00b2010-10-05 23:58:18 +0000958 while (Lex.getCode() == tgtok::comma) {
959 Lex.Lex(); // eat the ','
960
961 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000962 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000963 }
David Greene5d0c0512009-05-14 20:54:48 +0000964
965 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000966 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000967 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000968 }
969 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000970
Daniel Sanders314e80e2014-05-07 10:13:19 +0000971 // If we are doing !listconcat, we should know the type by now
972 if (OpTok == tgtok::XListConcat) {
973 if (VarInit *Arg0 = dyn_cast<VarInit>(InitList[0]))
974 Type = Arg0->getType();
975 else if (ListInit *Arg0 = dyn_cast<ListInit>(InitList[0]))
976 Type = Arg0->getType();
977 else {
978 InitList[0]->dump();
979 Error(OpLoc, "expected a list");
980 return nullptr;
981 }
982 }
983
Chris Lattner61ea00b2010-10-05 23:58:18 +0000984 // We allow multiple operands to associative operators like !strconcat as
985 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000986 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000987 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000988 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000989 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
990 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000991 InitList.back() = RHS;
992 }
993 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000994
Chris Lattner61ea00b2010-10-05 23:58:18 +0000995 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000996 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000997 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000998
Chris Lattner61ea00b2010-10-05 23:58:18 +0000999 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +00001000 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001001 }
1002
David Greene3587eed2009-05-14 23:26:46 +00001003 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001004 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001005 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
1006 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +00001007 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001008
David Greene98ed3c72009-05-14 21:54:42 +00001009 tgtok::TokKind LexCode = Lex.getCode();
1010 Lex.Lex(); // eat the operation
1011 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001012 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001013 case tgtok::XIf:
1014 Code = TernOpInit::IF;
1015 break;
David Greenee917fff2009-05-14 22:23:47 +00001016 case tgtok::XForEach:
1017 Code = TernOpInit::FOREACH;
1018 break;
David Greene98ed3c72009-05-14 21:54:42 +00001019 case tgtok::XSubst:
1020 Code = TernOpInit::SUBST;
1021 break;
1022 }
1023 if (Lex.getCode() != tgtok::l_paren) {
1024 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001025 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001026 }
1027 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001028
David Greeneaf8ee2c2011-07-29 22:43:06 +00001029 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001030 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001031
David Greene98ed3c72009-05-14 21:54:42 +00001032 if (Lex.getCode() != tgtok::comma) {
1033 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001034 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001035 }
1036 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001037
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001038 Init *MHS = ParseValue(CurRec, ItemType);
1039 if (!MHS)
1040 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001041
David Greene98ed3c72009-05-14 21:54:42 +00001042 if (Lex.getCode() != tgtok::comma) {
1043 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001044 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001045 }
1046 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001047
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001048 Init *RHS = ParseValue(CurRec, ItemType);
1049 if (!RHS)
1050 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001051
David Greene98ed3c72009-05-14 21:54:42 +00001052 if (Lex.getCode() != tgtok::r_paren) {
1053 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001054 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001055 }
1056 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001057
David Greene98ed3c72009-05-14 21:54:42 +00001058 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001059 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001060 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001061 RecTy *MHSTy = nullptr;
1062 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001063
Sean Silvafb509ed2012-10-10 20:24:43 +00001064 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001065 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001066 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001067 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001068 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001069 MHSTy = BitRecTy::get();
1070
Sean Silvafb509ed2012-10-10 20:24:43 +00001071 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001072 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001073 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001074 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001075 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001076 RHSTy = BitRecTy::get();
1077
1078 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001079 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001080 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001081 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001082 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001083
1084 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001085 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001086 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001087 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001088
1089 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1090 Type = RHSTy;
1091 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1092 Type = MHSTy;
Bob Wilson7248f862009-11-22 04:24:42 +00001093 } else {
David Greene3587eed2009-05-14 23:26:46 +00001094 TokError("inconsistent types for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001095 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001096 }
1097 break;
1098 }
David Greenee917fff2009-05-14 22:23:47 +00001099 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001100 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
Craig Topper011817a2014-04-09 04:50:04 +00001101 if (!MHSt) {
David Greenee917fff2009-05-14 22:23:47 +00001102 TokError("could not get type for !foreach");
Craig Topper011817a2014-04-09 04:50:04 +00001103 return nullptr;
David Greenee917fff2009-05-14 22:23:47 +00001104 }
1105 Type = MHSt->getType();
1106 break;
1107 }
David Greene98ed3c72009-05-14 21:54:42 +00001108 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001109 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001110 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001111 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001112 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001113 }
1114 Type = RHSt->getType();
1115 break;
1116 }
1117 }
David Greenee32ebf22011-07-29 19:07:07 +00001118 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001119 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001120 }
David Greene5d0c0512009-05-14 20:54:48 +00001121 }
David Greene5d0c0512009-05-14 20:54:48 +00001122}
1123
1124/// ParseOperatorType - Parse a type for an operator. This returns
1125/// null on error.
1126///
1127/// OperatorType ::= '<' Type '>'
1128///
Dan Gohman1432ef82009-08-12 22:10:57 +00001129RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001130 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001131
1132 if (Lex.getCode() != tgtok::less) {
1133 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001134 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001135 }
1136 Lex.Lex(); // eat the <
1137
1138 Type = ParseType();
1139
Craig Topper011817a2014-04-09 04:50:04 +00001140 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001141 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001142 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001143 }
1144
1145 if (Lex.getCode() != tgtok::greater) {
1146 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001147 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001148 }
1149 Lex.Lex(); // eat the >
1150
1151 return Type;
1152}
1153
1154
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001155/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1156///
1157/// SimpleValue ::= IDValue
1158/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001159/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001160/// SimpleValue ::= CODEFRAGMENT
1161/// SimpleValue ::= '?'
1162/// SimpleValue ::= '{' ValueList '}'
1163/// SimpleValue ::= ID '<' ValueListNE '>'
1164/// SimpleValue ::= '[' ValueList ']'
1165/// SimpleValue ::= '(' IDValue DagArgList ')'
1166/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001167/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001168/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1169/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1170/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001171/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001172/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1173///
David Greened4263a62011-10-19 13:04:20 +00001174Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1175 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001176 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001177 switch (Lex.getCode()) {
1178 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001179 case tgtok::paste:
1180 // This is a leading paste operation. This is deprecated but
1181 // still exists in some .td files. Ignore it.
1182 Lex.Lex(); // Skip '#'.
1183 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001184 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001185 case tgtok::BinaryIntVal: {
1186 auto BinaryVal = Lex.getCurBinaryIntVal();
1187 SmallVector<Init*, 16> Bits(BinaryVal.second);
1188 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
1189 Bits[i] = BitInit::get(BinaryVal.first & (1 << i));
1190 R = BitsInit::get(Bits);
1191 Lex.Lex();
1192 break;
1193 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001194 case tgtok::StrVal: {
1195 std::string Val = Lex.getCurStrVal();
1196 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001197
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001198 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001199 while (Lex.getCode() == tgtok::StrVal) {
1200 Val += Lex.getCurStrVal();
1201 Lex.Lex();
1202 }
Bob Wilson7248f862009-11-22 04:24:42 +00001203
David Greenee32ebf22011-07-29 19:07:07 +00001204 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001205 break;
1206 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001207 case tgtok::CodeFragment:
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +00001208 R = StringInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001209 Lex.Lex();
1210 break;
1211 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001212 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001213 Lex.Lex();
1214 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001215 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001216 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001217 std::string Name = Lex.getCurStrVal();
1218 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001219 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001220
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001221 // Value ::= ID '<' ValueListNE '>'
1222 if (Lex.Lex() == tgtok::greater) {
1223 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001224 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001225 }
David Greene8618f952009-06-08 20:23:18 +00001226
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001227 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1228 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1229 // body.
1230 Record *Class = Records.getClass(Name);
1231 if (!Class) {
1232 Error(NameLoc, "Expected a class name, got '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001233 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001234 }
David Greene8618f952009-06-08 20:23:18 +00001235
David Greeneaf8ee2c2011-07-29 22:43:06 +00001236 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
Craig Topper011817a2014-04-09 04:50:04 +00001237 if (ValueList.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001238
David Greene8618f952009-06-08 20:23:18 +00001239 if (Lex.getCode() != tgtok::greater) {
1240 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001241 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001242 }
1243 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001244 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001245
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001246 // Create the new record, set it as CurRec temporarily.
Alp Tokerce91fe52013-12-21 18:51:00 +00001247 Record *NewRec = new Record(GetNewAnonymousName(), NameLoc, Records,
Jordan Roseabdd99b2013-01-10 18:50:05 +00001248 /*IsAnonymous=*/true);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001249 SubClassReference SCRef;
Jordan Rosef12e8a92013-01-10 18:50:11 +00001250 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001251 SCRef.Rec = Class;
1252 SCRef.TemplateArgs = ValueList;
1253 // Add info about the subclass to NewRec.
1254 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001255 return nullptr;
Hal Finkela8c1f462014-01-02 20:47:09 +00001256 if (!CurMultiClass) {
1257 NewRec->resolveReferences();
1258 Records.addDef(NewRec);
1259 } else {
1260 // Otherwise, we're inside a multiclass, add it to the multiclass.
1261 CurMultiClass->DefPrototypes.push_back(NewRec);
1262
1263 // Copy the template arguments for the multiclass into the def.
1264 const std::vector<Init *> &TArgs =
1265 CurMultiClass->Rec.getTemplateArgs();
1266
1267 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1268 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1269 assert(RV && "Template arg doesn't exist?");
1270 NewRec->addValue(*RV);
1271 }
1272
1273 // We can't return the prototype def here, instead return:
1274 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1275 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1276 assert(MCNameRV && "multiclass record must have a NAME");
1277
1278 return UnOpInit::get(UnOpInit::CAST,
1279 BinOpInit::get(BinOpInit::STRCONCAT,
1280 VarInit::get(MCNameRV->getName(),
1281 MCNameRV->getType()),
1282 NewRec->getNameInit(),
1283 StringRecTy::get()),
1284 Class->getDefInit()->getType());
1285 }
Bob Wilson7248f862009-11-22 04:24:42 +00001286
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001287 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001288 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001289 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001290 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001291 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001292 Lex.Lex(); // eat the '{'
David Greeneaf8ee2c2011-07-29 22:43:06 +00001293 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001294
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001295 if (Lex.getCode() != tgtok::r_brace) {
1296 Vals = ParseValueList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001297 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001298 }
1299 if (Lex.getCode() != tgtok::r_brace) {
1300 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001301 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001302 }
1303 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001304
David Greeneaf8ee2c2011-07-29 22:43:06 +00001305 SmallVector<Init *, 16> NewBits(Vals.size());
David Greeneb3da8122011-07-29 19:07:00 +00001306
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001307 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001308 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001309 if (!Bit) {
Chris Lattner52416952007-11-22 21:06:59 +00001310 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1311 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001312 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001313 }
David Greeneb3da8122011-07-29 19:07:00 +00001314 NewBits[Vals.size()-i-1] = Bit;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001315 }
David Greenee32ebf22011-07-29 19:07:07 +00001316 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001317 }
1318 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1319 Lex.Lex(); // eat the '['
David Greeneaf8ee2c2011-07-29 22:43:06 +00001320 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001321
Craig Topper011817a2014-04-09 04:50:04 +00001322 RecTy *DeducedEltTy = nullptr;
1323 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001324
Craig Topper011817a2014-04-09 04:50:04 +00001325 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001326 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001327 if (!ListType) {
Alp Tokere69170a2014-06-26 22:52:05 +00001328 std::string s;
1329 raw_string_ostream ss(s);
Reid Klecknerd78273f2013-08-06 22:51:21 +00001330 ss << "Type mismatch for list, expected list type, got "
1331 << ItemType->getAsString();
1332 TokError(ss.str());
Craig Topper011817a2014-04-09 04:50:04 +00001333 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001334 }
1335 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001336 }
David Greene8618f952009-06-08 20:23:18 +00001337
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001338 if (Lex.getCode() != tgtok::r_square) {
Craig Topper011817a2014-04-09 04:50:04 +00001339 Vals = ParseValueList(CurRec, nullptr,
1340 GivenListTy ? GivenListTy->getElementType() : nullptr);
1341 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001342 }
1343 if (Lex.getCode() != tgtok::r_square) {
1344 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001345 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001346 }
1347 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001348
Craig Topper011817a2014-04-09 04:50:04 +00001349 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001350 if (Lex.getCode() == tgtok::less) {
1351 // Optional list element type
1352 Lex.Lex(); // eat the '<'
1353
1354 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001355 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001356 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001357 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001358 }
1359
1360 if (Lex.getCode() != tgtok::greater) {
1361 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001362 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001363 }
1364 Lex.Lex(); // eat the '>'
1365 }
1366
1367 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001368 RecTy *EltTy = nullptr;
David Greeneaf8ee2c2011-07-29 22:43:06 +00001369 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greene8618f952009-06-08 20:23:18 +00001370 i != ie;
1371 ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +00001372 TypedInit *TArg = dyn_cast<TypedInit>(*i);
Craig Topper011817a2014-04-09 04:50:04 +00001373 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001374 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001375 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001376 }
Craig Topper011817a2014-04-09 04:50:04 +00001377 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001378 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001379 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001380 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001381 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001382 }
Bob Wilson7248f862009-11-22 04:24:42 +00001383 } else {
David Greene8618f952009-06-08 20:23:18 +00001384 EltTy = TArg->getType();
1385 }
1386 }
1387
Craig Topper011817a2014-04-09 04:50:04 +00001388 if (GivenEltTy) {
1389 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001390 // Verify consistency
1391 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1392 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001393 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001394 }
1395 }
1396 EltTy = GivenEltTy;
1397 }
1398
Craig Topper011817a2014-04-09 04:50:04 +00001399 if (!EltTy) {
1400 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001401 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001402 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001403 }
1404 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001405 } else {
David Greene8618f952009-06-08 20:23:18 +00001406 // Make sure the deduced type is compatible with the given type
1407 if (GivenListTy) {
1408 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1409 TokError("Element type mismatch for list");
Craig Topper011817a2014-04-09 04:50:04 +00001410 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001411 }
1412 }
1413 DeducedEltTy = EltTy;
1414 }
Bob Wilson7248f862009-11-22 04:24:42 +00001415
David Greenee32ebf22011-07-29 19:07:07 +00001416 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001417 }
1418 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1419 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001420 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001421 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001422 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001423 }
Bob Wilson7248f862009-11-22 04:24:42 +00001424
David Greeneaf8ee2c2011-07-29 22:43:06 +00001425 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001426 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001427
Nate Begemandbe3f772009-03-19 05:21:56 +00001428 // If the operator name is present, parse it.
1429 std::string OperatorName;
1430 if (Lex.getCode() == tgtok::colon) {
1431 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1432 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001433 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001434 }
1435 OperatorName = Lex.getCurStrVal();
1436 Lex.Lex(); // eat the VarName.
1437 }
Bob Wilson7248f862009-11-22 04:24:42 +00001438
David Greeneaf8ee2c2011-07-29 22:43:06 +00001439 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001440 if (Lex.getCode() != tgtok::r_paren) {
1441 DagArgs = ParseDagArgList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001442 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001443 }
Bob Wilson7248f862009-11-22 04:24:42 +00001444
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001445 if (Lex.getCode() != tgtok::r_paren) {
1446 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001447 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001448 }
1449 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001450
David Greenee32ebf22011-07-29 19:07:07 +00001451 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001452 }
Bob Wilson7248f862009-11-22 04:24:42 +00001453
David Greene2f7cf7f2011-01-07 17:05:37 +00001454 case tgtok::XHead:
1455 case tgtok::XTail:
1456 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001457 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001458 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001459 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001460 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +00001461 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001462 case tgtok::XSRL:
1463 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001464 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001465 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001466 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001467 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001468 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001469 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001470 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001471 }
1472 }
Bob Wilson7248f862009-11-22 04:24:42 +00001473
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001474 return R;
1475}
1476
1477/// ParseValue - Parse a tblgen value. This returns null on error.
1478///
1479/// Value ::= SimpleValue ValueSuffix*
1480/// ValueSuffix ::= '{' BitList '}'
1481/// ValueSuffix ::= '[' BitList ']'
1482/// ValueSuffix ::= '.' ID
1483///
David Greened4263a62011-10-19 13:04:20 +00001484Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1485 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001486 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001487
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001488 // Parse the suffixes now if present.
1489 while (1) {
1490 switch (Lex.getCode()) {
1491 default: return Result;
1492 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001493 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001494 // This is the beginning of the object body.
1495 return Result;
1496
Chris Lattner526c8cb2009-06-21 03:39:35 +00001497 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001498 Lex.Lex(); // eat the '{'
1499 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001500 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001501
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001502 // Reverse the bitlist.
1503 std::reverse(Ranges.begin(), Ranges.end());
1504 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001505 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001506 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001507 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001508 }
Bob Wilson7248f862009-11-22 04:24:42 +00001509
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001510 // Eat the '}'.
1511 if (Lex.getCode() != tgtok::r_brace) {
1512 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001513 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001514 }
1515 Lex.Lex();
1516 break;
1517 }
1518 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001519 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001520 Lex.Lex(); // eat the '['
1521 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001522 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001523
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001524 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001525 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001526 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001527 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001528 }
Bob Wilson7248f862009-11-22 04:24:42 +00001529
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001530 // Eat the ']'.
1531 if (Lex.getCode() != tgtok::r_square) {
1532 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001533 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001534 }
1535 Lex.Lex();
1536 break;
1537 }
1538 case tgtok::period:
1539 if (Lex.Lex() != tgtok::Id) { // eat the .
1540 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001541 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001542 }
1543 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001544 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001545 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001546 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001547 }
David Greenee32ebf22011-07-29 19:07:07 +00001548 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001549 Lex.Lex(); // eat field name
1550 break;
David Greene8e85b482011-10-19 13:04:43 +00001551
1552 case tgtok::paste:
1553 SMLoc PasteLoc = Lex.getLoc();
1554
1555 // Create a !strconcat() operation, first casting each operand to
1556 // a string if necessary.
1557
Sean Silvafb509ed2012-10-10 20:24:43 +00001558 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001559 if (!LHS) {
1560 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001561 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001562 }
1563
1564 if (LHS->getType() != StringRecTy::get()) {
1565 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1566 }
1567
Craig Topper011817a2014-04-09 04:50:04 +00001568 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001569
1570 Lex.Lex(); // Eat the '#'.
1571 switch (Lex.getCode()) {
1572 case tgtok::colon:
1573 case tgtok::semi:
1574 case tgtok::l_brace:
1575 // These are all of the tokens that can begin an object body.
1576 // Some of these can also begin values but we disallow those cases
1577 // because they are unlikely to be useful.
1578
1579 // Trailing paste, concat with an empty string.
1580 RHS = StringInit::get("");
1581 break;
1582
1583 default:
1584 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001585 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001586 if (!RHS) {
1587 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001588 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001589 }
1590
1591 if (RHS->getType() != StringRecTy::get()) {
1592 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1593 }
1594
1595 break;
1596 }
1597
1598 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1599 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1600 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001601 }
1602 }
1603}
1604
1605/// ParseDagArgList - Parse the argument list for a dag literal expression.
1606///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001607/// DagArg ::= Value (':' VARNAME)?
1608/// DagArg ::= VARNAME
1609/// DagArgList ::= DagArg
1610/// DagArgList ::= DagArgList ',' DagArg
David Greeneaf8ee2c2011-07-29 22:43:06 +00001611std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001612TGParser::ParseDagArgList(Record *CurRec) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001613 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson7248f862009-11-22 04:24:42 +00001614
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001615 while (1) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001616 // DagArg ::= VARNAME
1617 if (Lex.getCode() == tgtok::VarName) {
1618 // A missing value is treated like '?'.
1619 Result.push_back(std::make_pair(UnsetInit::get(), Lex.getCurStrVal()));
1620 Lex.Lex();
1621 } else {
1622 // DagArg ::= Value (':' VARNAME)?
1623 Init *Val = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001624 if (!Val)
David Greeneaf8ee2c2011-07-29 22:43:06 +00001625 return std::vector<std::pair<llvm::Init*, std::string> >();
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001626
1627 // If the variable name is present, add it.
1628 std::string VarName;
1629 if (Lex.getCode() == tgtok::colon) {
1630 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1631 TokError("expected variable name in dag literal");
1632 return std::vector<std::pair<llvm::Init*, std::string> >();
1633 }
1634 VarName = Lex.getCurStrVal();
1635 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001636 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001637
1638 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001639 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001640 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001641 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001642 }
Bob Wilson7248f862009-11-22 04:24:42 +00001643
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001644 return Result;
1645}
1646
1647
1648/// ParseValueList - Parse a comma separated list of values, returning them as a
1649/// vector. Note that this always expects to be able to parse at least one
1650/// value. It returns an empty list if this is not possible.
1651///
1652/// ValueList ::= Value (',' Value)
1653///
David Greeneaf8ee2c2011-07-29 22:43:06 +00001654std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopher71520a82011-07-11 23:06:52 +00001655 RecTy *EltTy) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001656 std::vector<Init*> Result;
David Greene8618f952009-06-08 20:23:18 +00001657 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001658 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001659 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001660 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001661 if (!TArgs.size()) {
1662 TokError("template argument provided to non-template class");
1663 return std::vector<Init*>();
1664 }
David Greene8618f952009-06-08 20:23:18 +00001665 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001666 if (!RV) {
1667 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1668 << ")\n";
1669 }
David Greene8618f952009-06-08 20:23:18 +00001670 assert(RV && "Template argument record not found??");
1671 ItemType = RV->getType();
1672 ++ArgN;
1673 }
1674 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001675 if (!Result.back()) return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001676
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001677 while (Lex.getCode() == tgtok::comma) {
1678 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001679
Craig Topper011817a2014-04-09 04:50:04 +00001680 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001681 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001682 if (ArgN >= TArgs.size()) {
1683 TokError("too many template arguments");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001684 return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001685 }
David Greene8618f952009-06-08 20:23:18 +00001686 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1687 assert(RV && "Template argument record not found??");
1688 ItemType = RV->getType();
1689 ++ArgN;
1690 }
1691 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001692 if (!Result.back()) return std::vector<Init*>();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001693 }
Bob Wilson7248f862009-11-22 04:24:42 +00001694
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001695 return Result;
1696}
1697
1698
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001699/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1700/// empty string on error. This can happen in a number of different context's,
1701/// including within a def or in the template args for a def (which which case
1702/// CurRec will be non-null) and within the template args for a multiclass (in
1703/// which case CurRec will be null, but CurMultiClass will be set). This can
1704/// also happen within a def that is within a multiclass, which will set both
1705/// CurRec and CurMultiClass.
1706///
1707/// Declaration ::= FIELD? Type ID ('=' Value)?
1708///
David Greenedb10e692011-10-19 13:02:42 +00001709Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001710 bool ParsingTemplateArgs) {
1711 // Read the field prefix if present.
1712 bool HasField = Lex.getCode() == tgtok::Field;
1713 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001714
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001715 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001716 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001717
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001718 if (Lex.getCode() != tgtok::Id) {
1719 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001720 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001721 }
Bob Wilson7248f862009-11-22 04:24:42 +00001722
Chris Lattner526c8cb2009-06-21 03:39:35 +00001723 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001724 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001725 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001726
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001727 if (ParsingTemplateArgs) {
1728 if (CurRec) {
David Greenedb10e692011-10-19 13:02:42 +00001729 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001730 } else {
1731 assert(CurMultiClass);
1732 }
1733 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001734 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1735 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001736 }
Bob Wilson7248f862009-11-22 04:24:42 +00001737
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001738 // Add the value.
1739 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001740 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001741
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001742 // If a value is present, parse it.
1743 if (Lex.getCode() == tgtok::equal) {
1744 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001745 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001746 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001747 if (!Val ||
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001748 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001749 // Return the name, even if an error is thrown. This is so that we can
1750 // continue to make some progress, even without the value having been
1751 // initialized.
1752 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001753 }
Bob Wilson7248f862009-11-22 04:24:42 +00001754
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001755 return DeclName;
1756}
1757
David Greenefb927af2012-02-22 16:09:41 +00001758/// ParseForeachDeclaration - Read a foreach declaration, returning
1759/// the name of the declared object or a NULL Init on error. Return
1760/// the name of the parsed initializer list through ForeachListName.
1761///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001762/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1763/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1764/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001765///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001766VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001767 if (Lex.getCode() != tgtok::Id) {
1768 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001769 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001770 }
1771
1772 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1773 Lex.Lex();
1774
1775 // If a value is present, parse it.
1776 if (Lex.getCode() != tgtok::equal) {
1777 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001778 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001779 }
1780 Lex.Lex(); // Eat the '='
1781
Craig Topper011817a2014-04-09 04:50:04 +00001782 RecTy *IterType = nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001783 std::vector<unsigned> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001784
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001785 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001786 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001787 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001788 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001789 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001790 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001791 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001792 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001793 }
1794 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001795 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001796 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001797 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001798 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001799 }
1800 IterType = ListType->getElementType();
1801 break;
David Greenefb927af2012-02-22 16:09:41 +00001802 }
1803
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001804 case tgtok::IntVal: { // RangePiece.
1805 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001806 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001807 break;
David Greenefb927af2012-02-22 16:09:41 +00001808 }
1809
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001810 case tgtok::l_brace: { // '{' RangeList '}'
1811 Lex.Lex(); // eat the '{'
1812 Ranges = ParseRangeList();
1813 if (Lex.getCode() != tgtok::r_brace) {
1814 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001815 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001816 }
1817 Lex.Lex();
1818 break;
1819 }
1820 }
David Greenefb927af2012-02-22 16:09:41 +00001821
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001822 if (!Ranges.empty()) {
1823 assert(!IterType && "Type already initialized?");
1824 IterType = IntRecTy::get();
1825 std::vector<Init*> Values;
1826 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
1827 Values.push_back(IntInit::get(Ranges[i]));
1828 ForeachListValue = ListInit::get(Values, IterType);
1829 }
1830
1831 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001832 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001833
1834 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001835}
1836
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001837/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1838/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1839/// template args for a def, which may or may not be in a multiclass. If null,
1840/// these are the template args for a multiclass.
1841///
1842/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001843///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001844bool TGParser::ParseTemplateArgList(Record *CurRec) {
1845 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1846 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001847
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001848 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001849
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001850 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001851 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001852 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001853 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001854
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001855 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001856
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001857 while (Lex.getCode() == tgtok::comma) {
1858 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001859
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001860 // Read the following declarations.
1861 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001862 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001863 return true;
1864 TheRecToAddTo->addTemplateArg(TemplArg);
1865 }
Bob Wilson7248f862009-11-22 04:24:42 +00001866
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001867 if (Lex.getCode() != tgtok::greater)
1868 return TokError("expected '>' at end of template argument list");
1869 Lex.Lex(); // eat the '>'.
1870 return false;
1871}
1872
1873
1874/// ParseBodyItem - Parse a single item at within the body of a def or class.
1875///
1876/// BodyItem ::= Declaration ';'
1877/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1878bool TGParser::ParseBodyItem(Record *CurRec) {
1879 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001880 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001881 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001882
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001883 if (Lex.getCode() != tgtok::semi)
1884 return TokError("expected ';' after declaration");
1885 Lex.Lex();
1886 return false;
1887 }
1888
1889 // LET ID OptionalRangeList '=' Value ';'
1890 if (Lex.Lex() != tgtok::Id)
1891 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001892
Chris Lattner526c8cb2009-06-21 03:39:35 +00001893 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001894 std::string FieldName = Lex.getCurStrVal();
1895 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001896
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001897 std::vector<unsigned> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001898 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001899 return true;
1900 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001901
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001902 if (Lex.getCode() != tgtok::equal)
1903 return TokError("expected '=' in let expression");
1904 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001905
David Greene8618f952009-06-08 20:23:18 +00001906 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001907 if (!Field)
David Greene8618f952009-06-08 20:23:18 +00001908 return TokError("Value '" + FieldName + "' unknown!");
1909
1910 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001911
David Greeneaf8ee2c2011-07-29 22:43:06 +00001912 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001913 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001914
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001915 if (Lex.getCode() != tgtok::semi)
1916 return TokError("expected ';' after let expression");
1917 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001918
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001919 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1920}
1921
1922/// ParseBody - Read the body of a class or def. Return true on error, false on
1923/// success.
1924///
1925/// Body ::= ';'
1926/// Body ::= '{' BodyList '}'
1927/// BodyList BodyItem*
1928///
1929bool TGParser::ParseBody(Record *CurRec) {
1930 // If this is a null definition, just eat the semi and return.
1931 if (Lex.getCode() == tgtok::semi) {
1932 Lex.Lex();
1933 return false;
1934 }
Bob Wilson7248f862009-11-22 04:24:42 +00001935
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001936 if (Lex.getCode() != tgtok::l_brace)
1937 return TokError("Expected ';' or '{' to start body");
1938 // Eat the '{'.
1939 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001940
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001941 while (Lex.getCode() != tgtok::r_brace)
1942 if (ParseBodyItem(CurRec))
1943 return true;
1944
1945 // Eat the '}'.
1946 Lex.Lex();
1947 return false;
1948}
1949
Sean Silvacb1a75e2013-01-09 04:49:14 +00001950/// \brief Apply the current let bindings to \a CurRec.
1951/// \returns true on error, false otherwise.
1952bool TGParser::ApplyLetStack(Record *CurRec) {
1953 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1954 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1955 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1956 LetStack[i][j].Bits, LetStack[i][j].Value))
1957 return true;
1958 return false;
1959}
1960
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001961/// ParseObjectBody - Parse the body of a def or class. This consists of an
1962/// optional ClassList followed by a Body. CurRec is the current def or class
1963/// that is being parsed.
1964///
1965/// ObjectBody ::= BaseClassList Body
1966/// BaseClassList ::= /*empty*/
1967/// BaseClassList ::= ':' BaseClassListNE
1968/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1969///
1970bool TGParser::ParseObjectBody(Record *CurRec) {
1971 // If there is a baseclass list, read it.
1972 if (Lex.getCode() == tgtok::colon) {
1973 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001974
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001975 // Read all of the subclasses.
1976 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1977 while (1) {
1978 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00001979 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001980
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001981 // Add it.
1982 if (AddSubClass(CurRec, SubClass))
1983 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001984
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001985 if (Lex.getCode() != tgtok::comma) break;
1986 Lex.Lex(); // eat ','.
1987 SubClass = ParseSubClassReference(CurRec, false);
1988 }
1989 }
1990
Sean Silvacb1a75e2013-01-09 04:49:14 +00001991 if (ApplyLetStack(CurRec))
1992 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001993
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001994 return ParseBody(CurRec);
1995}
1996
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001997/// ParseDef - Parse and return a top level or multiclass def, return the record
1998/// corresponding to it. This returns null on error.
1999///
2000/// DefInst ::= DEF ObjectName ObjectBody
2001///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002002bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00002003 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002004 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00002005 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002006
2007 // Parse ObjectName and make a record for it.
Jordan Roseabdd99b2013-01-10 18:50:05 +00002008 Record *CurRec;
2009 Init *Name = ParseObjectName(CurMultiClass);
2010 if (Name)
2011 CurRec = new Record(Name, DefLoc, Records);
2012 else
2013 CurRec = new Record(GetNewAnonymousName(), DefLoc, Records,
2014 /*IsAnonymous=*/true);
Bob Wilson7248f862009-11-22 04:24:42 +00002015
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002016 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002017 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002018
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002019 // Ensure redefinition doesn't happen.
David Greeneebb006f2012-01-28 00:03:24 +00002020 if (Records.getDef(CurRec->getNameInitAsString())) {
David Greene3a20f5a2011-10-19 13:03:45 +00002021 Error(DefLoc, "def '" + CurRec->getNameInitAsString()
2022 + "' already defined");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002023 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002024 }
2025 Records.addDef(CurRec);
Hal Finkela8c1f462014-01-02 20:47:09 +00002026
2027 if (ParseObjectBody(CurRec))
2028 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002029 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002030 // Parse the body before adding this prototype to the DefPrototypes vector.
2031 // That way implicit definitions will be added to the DefPrototypes vector
2032 // before this object, instantiated prior to defs derived from this object,
2033 // and this available for indirect name resolution when defs derived from
2034 // this object are instantiated.
2035 if (ParseObjectBody(CurRec))
2036 return true;
2037
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002038 // Otherwise, a def inside a multiclass, add it to the multiclass.
2039 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene07e055f2011-10-19 13:03:51 +00002040 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2041 == CurRec->getNameInit()) {
2042 Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002043 "' already defined in this multiclass!");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002044 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002045 }
2046 CurMultiClass->DefPrototypes.push_back(CurRec);
Hal Finkela8c1f462014-01-02 20:47:09 +00002047 } else if (ParseObjectBody(CurRec))
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002048 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002049
Craig Topper011817a2014-04-09 04:50:04 +00002050 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002051 // See Record::setName(). This resolve step will see any new name
2052 // for the def that might have been created when resolving
2053 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002054 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002055
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002056 // If ObjectBody has template arguments, it's an error.
2057 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002058
2059 if (CurMultiClass) {
2060 // Copy the template arguments for the multiclass into the def.
David Greenedb10e692011-10-19 13:02:42 +00002061 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002062 CurMultiClass->Rec.getTemplateArgs();
2063
2064 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2065 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
2066 assert(RV && "Template arg doesn't exist?");
2067 CurRec->addValue(*RV);
2068 }
2069 }
2070
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002071 if (ProcessForeachDefs(CurRec, DefLoc)) {
David Greenefb927af2012-02-22 16:09:41 +00002072 Error(DefLoc,
2073 "Could not process loops for def" + CurRec->getNameInitAsString());
2074 return true;
2075 }
2076
2077 return false;
2078}
2079
2080/// ParseForeach - Parse a for statement. Return the record corresponding
2081/// to it. This returns true on error.
2082///
2083/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2084/// Foreach ::= FOREACH Declaration IN Object
2085///
2086bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2087 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2088 Lex.Lex(); // Eat the 'for' token.
2089
2090 // Make a temporary object to record items associated with the for
2091 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002092 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002093 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002094 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002095 return TokError("expected declaration in for");
2096
2097 if (Lex.getCode() != tgtok::In)
2098 return TokError("Unknown tok");
2099 Lex.Lex(); // Eat the in
2100
2101 // Create a loop object and remember it.
2102 Loops.push_back(ForeachLoop(IterName, ListValue));
2103
2104 if (Lex.getCode() != tgtok::l_brace) {
2105 // FOREACH Declaration IN Object
2106 if (ParseObject(CurMultiClass))
2107 return true;
2108 }
2109 else {
2110 SMLoc BraceLoc = Lex.getLoc();
2111 // Otherwise, this is a group foreach.
2112 Lex.Lex(); // eat the '{'.
2113
2114 // Parse the object list.
2115 if (ParseObjectList(CurMultiClass))
2116 return true;
2117
2118 if (Lex.getCode() != tgtok::r_brace) {
2119 TokError("expected '}' at end of foreach command");
2120 return Error(BraceLoc, "to match this '{'");
2121 }
2122 Lex.Lex(); // Eat the }
2123 }
2124
2125 // We've processed everything in this loop.
2126 Loops.pop_back();
2127
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002128 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002129}
2130
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002131/// ParseClass - Parse a tblgen class definition.
2132///
2133/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2134///
2135bool TGParser::ParseClass() {
2136 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2137 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002138
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002139 if (Lex.getCode() != tgtok::Id)
2140 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002141
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002142 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2143 if (CurRec) {
2144 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002145 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002146 !CurRec->getSuperClasses().empty() ||
2147 !CurRec->getTemplateArgs().empty())
David Greene8eed9982011-10-19 13:03:58 +00002148 return TokError("Class '" + CurRec->getNameInitAsString()
2149 + "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002150 } else {
2151 // If this is the first reference to this class, create and add it.
Chris Lattner89dcb682010-12-15 04:48:22 +00002152 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002153 Records.addClass(CurRec);
2154 }
2155 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002156
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002157 // If there are template args, parse them.
2158 if (Lex.getCode() == tgtok::less)
2159 if (ParseTemplateArgList(CurRec))
2160 return true;
2161
2162 // Finally, parse the object body.
2163 return ParseObjectBody(CurRec);
2164}
2165
2166/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2167/// of LetRecords.
2168///
2169/// LetList ::= LetItem (',' LetItem)*
2170/// LetItem ::= ID OptionalRangeList '=' Value
2171///
2172std::vector<LetRecord> TGParser::ParseLetList() {
2173 std::vector<LetRecord> Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002174
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002175 while (1) {
2176 if (Lex.getCode() != tgtok::Id) {
2177 TokError("expected identifier in let definition");
2178 return std::vector<LetRecord>();
2179 }
2180 std::string Name = Lex.getCurStrVal();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002181 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002182 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002183
2184 // Check for an optional RangeList.
2185 std::vector<unsigned> Bits;
Bob Wilson7248f862009-11-22 04:24:42 +00002186 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002187 return std::vector<LetRecord>();
2188 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002189
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002190 if (Lex.getCode() != tgtok::equal) {
2191 TokError("expected '=' in let expression");
2192 return std::vector<LetRecord>();
2193 }
2194 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002195
Craig Topper011817a2014-04-09 04:50:04 +00002196 Init *Val = ParseValue(nullptr);
2197 if (!Val) return std::vector<LetRecord>();
Bob Wilson7248f862009-11-22 04:24:42 +00002198
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002199 // Now that we have everything, add the record.
2200 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson7248f862009-11-22 04:24:42 +00002201
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002202 if (Lex.getCode() != tgtok::comma)
2203 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002204 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002205 }
2206}
2207
2208/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002209/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002210///
2211/// Object ::= LET LetList IN '{' ObjectList '}'
2212/// Object ::= LET LetList IN Object
2213///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002214bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002215 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2216 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002217
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002218 // Add this entry to the let stack.
2219 std::vector<LetRecord> LetInfo = ParseLetList();
2220 if (LetInfo.empty()) return true;
2221 LetStack.push_back(LetInfo);
2222
2223 if (Lex.getCode() != tgtok::In)
2224 return TokError("expected 'in' at end of top-level 'let'");
2225 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002226
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002227 // If this is a scalar let, just handle it now
2228 if (Lex.getCode() != tgtok::l_brace) {
2229 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002230 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002231 return true;
2232 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002233 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002234 // Otherwise, this is a group let.
2235 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002236
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002237 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002238 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002239 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002240
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002241 if (Lex.getCode() != tgtok::r_brace) {
2242 TokError("expected '}' at end of top level let command");
2243 return Error(BraceLoc, "to match this '{'");
2244 }
2245 Lex.Lex();
2246 }
Bob Wilson7248f862009-11-22 04:24:42 +00002247
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002248 // Outside this let scope, this let block is not active.
2249 LetStack.pop_back();
2250 return false;
2251}
2252
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002253/// ParseMultiClass - Parse a multiclass definition.
2254///
Bob Wilson3d948162009-04-28 19:41:44 +00002255/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002256/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2257/// MultiClassObject ::= DefInst
2258/// MultiClassObject ::= MultiClassInst
2259/// MultiClassObject ::= DefMInst
2260/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2261/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002262///
2263bool TGParser::ParseMultiClass() {
2264 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2265 Lex.Lex(); // Eat the multiclass token.
2266
2267 if (Lex.getCode() != tgtok::Id)
2268 return TokError("expected identifier after multiclass for name");
2269 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002270
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002271 if (MultiClasses.count(Name))
2272 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002273
Chris Lattner77d369c2010-12-13 00:23:57 +00002274 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
2275 Lex.getLoc(), Records);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002276 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002277
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002278 // If there are template args, parse them.
2279 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002280 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002281 return true;
2282
David Greene7049e792009-04-24 16:55:41 +00002283 bool inherits = false;
2284
David Greene753ed8f2009-04-22 16:42:54 +00002285 // If there are submulticlasses, parse them.
2286 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002287 inherits = true;
2288
David Greene753ed8f2009-04-22 16:42:54 +00002289 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002290
David Greene753ed8f2009-04-22 16:42:54 +00002291 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002292 SubMultiClassReference SubMultiClass =
2293 ParseSubMultiClassReference(CurMultiClass);
David Greene753ed8f2009-04-22 16:42:54 +00002294 while (1) {
2295 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002296 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002297
David Greene753ed8f2009-04-22 16:42:54 +00002298 // Add it.
2299 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2300 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002301
David Greene753ed8f2009-04-22 16:42:54 +00002302 if (Lex.getCode() != tgtok::comma) break;
2303 Lex.Lex(); // eat ','.
2304 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2305 }
2306 }
2307
David Greene7049e792009-04-24 16:55:41 +00002308 if (Lex.getCode() != tgtok::l_brace) {
2309 if (!inherits)
2310 return TokError("expected '{' in multiclass definition");
Bob Wilson7248f862009-11-22 04:24:42 +00002311 else if (Lex.getCode() != tgtok::semi)
2312 return TokError("expected ';' in multiclass definition");
David Greene7049e792009-04-24 16:55:41 +00002313 else
Bob Wilson7248f862009-11-22 04:24:42 +00002314 Lex.Lex(); // eat the ';'.
2315 } else {
David Greene7049e792009-04-24 16:55:41 +00002316 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2317 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002318
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002319 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002320 switch (Lex.getCode()) {
2321 default:
David Greene33f61992011-10-07 18:25:05 +00002322 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002323 case tgtok::Let:
2324 case tgtok::Def:
2325 case tgtok::Defm:
David Greenefb927af2012-02-22 16:09:41 +00002326 case tgtok::Foreach:
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002327 if (ParseObject(CurMultiClass))
2328 return true;
2329 break;
2330 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002331 }
David Greene7049e792009-04-24 16:55:41 +00002332 Lex.Lex(); // eat the '}'.
2333 }
Bob Wilson7248f862009-11-22 04:24:42 +00002334
Craig Topper011817a2014-04-09 04:50:04 +00002335 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002336 return false;
2337}
2338
David Greenedb445972011-10-05 22:42:07 +00002339Record *TGParser::
2340InstantiateMulticlassDef(MultiClass &MC,
2341 Record *DefProto,
Hal Finkelf2a0b2b2014-01-02 19:35:33 +00002342 Init *&DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002343 SMRange DefmPrefixRange) {
David Greene5d5d88c2011-10-19 13:04:31 +00002344 // We need to preserve DefProto so it can be reused for later
2345 // instantiations, so create a new Record to inherit from it.
2346
David Greenedb445972011-10-05 22:42:07 +00002347 // Add in the defm name. If the defm prefix is empty, give each
2348 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2349 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2350 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002351
Jordan Roseabdd99b2013-01-10 18:50:05 +00002352 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002353 if (!DefmPrefix) {
David Greene5d5d88c2011-10-19 13:04:31 +00002354 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002355 IsAnonymous = true;
2356 }
David Greene5d5d88c2011-10-19 13:04:31 +00002357
2358 Init *DefName = DefProto->getNameInit();
2359
Sean Silvafb509ed2012-10-10 20:24:43 +00002360 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002361
Craig Topper011817a2014-04-09 04:50:04 +00002362 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002363 // We have a fully expanded string so there are no operators to
2364 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002365 DefName =
2366 BinOpInit::get(BinOpInit::STRCONCAT,
2367 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2368 StringRecTy::get())->Fold(DefProto, &MC),
2369 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2370 }
David Greene5d5d88c2011-10-19 13:04:31 +00002371
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002372 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002373 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002374 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002375 Record *CurRec = new Record(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002376
2377 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002378 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002379 Ref.Rec = DefProto;
2380 AddSubClass(CurRec, Ref);
2381
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002382 // Set the value for NAME. We don't resolve references to it 'til later,
2383 // though, so that uses in nested multiclass names don't get
2384 // confused.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002385 if (SetValue(CurRec, Ref.RefRange.Start, "NAME", std::vector<unsigned>(),
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002386 DefmPrefix)) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002387 Error(DefmPrefixRange.Start, "Could not resolve "
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002388 + CurRec->getNameInitAsString() + ":NAME to '"
2389 + DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002390 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002391 }
David Greene8bf0d722011-10-19 13:04:35 +00002392
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002393 // If the DefNameString didn't resolve, we probably have a reference to
2394 // NAME and need to replace it. We need to do at least this much greedily,
2395 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002396 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002397 RecordVal *DefNameRV = CurRec->getValue("NAME");
2398 CurRec->resolveReferencesTo(DefNameRV);
2399 }
2400
2401 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002402 // Now that we're at the top level, resolve all NAME references
2403 // in the resultant defs that weren't in the def names themselves.
2404 RecordVal *DefNameRV = CurRec->getValue("NAME");
2405 CurRec->resolveReferencesTo(DefNameRV);
2406
2407 // Now that NAME references are resolved and we're at the top level of
2408 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002409 // currently in a multiclass, it means this defm appears inside a
2410 // multiclass and its name won't be fully resolvable until we see
2411 // the top-level defm. Therefore, we don't add this to the
2412 // RecordKeeper at this point. If we did we could get duplicate
2413 // defs as more than one probably refers to NAME or some other
2414 // common internal placeholder.
2415
2416 // Ensure redefinition doesn't happen.
2417 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002418 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002419 "' already defined, instantiating defm with subdef '" +
2420 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002421 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002422 }
2423
2424 Records.addDef(CurRec);
2425 }
2426
David Greenedb445972011-10-05 22:42:07 +00002427 return CurRec;
2428}
2429
2430bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2431 Record *CurRec,
2432 SMLoc DefmPrefixLoc,
2433 SMLoc SubClassLoc,
David Greenedb10e692011-10-19 13:02:42 +00002434 const std::vector<Init *> &TArgs,
David Greenedb445972011-10-05 22:42:07 +00002435 std::vector<Init *> &TemplateVals,
2436 bool DeleteArgs) {
2437 // Loop over all of the template arguments, setting them to the specified
2438 // value or leaving them as the default if necessary.
2439 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2440 // Check if a value is specified for this temp-arg.
2441 if (i < TemplateVals.size()) {
2442 // Set it now.
2443 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2444 TemplateVals[i]))
2445 return true;
2446
2447 // Resolve it next.
2448 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2449
2450 if (DeleteArgs)
2451 // Now remove it.
2452 CurRec->removeValue(TArgs[i]);
2453
2454 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2455 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenedb10e692011-10-19 13:02:42 +00002456 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2457 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2458 + "'");
David Greenedb445972011-10-05 22:42:07 +00002459 }
2460 }
2461 return false;
2462}
2463
2464bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2465 Record *CurRec,
2466 Record *DefProto,
2467 SMLoc DefmPrefixLoc) {
2468 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002469 if (ApplyLetStack(CurRec))
2470 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002471
David Greenedb445972011-10-05 22:42:07 +00002472 // Don't create a top level definition for defm inside multiclasses,
2473 // instead, only update the prototypes and bind the template args
2474 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002475 if (!CurMultiClass)
2476 return false;
2477 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2478 i != e; ++i)
2479 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2480 == CurRec->getNameInit())
2481 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2482 "' already defined in this multiclass!");
2483 CurMultiClass->DefPrototypes.push_back(CurRec);
David Greenedb445972011-10-05 22:42:07 +00002484
Sean Silvacc951b22013-01-09 05:28:12 +00002485 // Copy the template arguments for the multiclass into the new def.
2486 const std::vector<Init *> &TA =
2487 CurMultiClass->Rec.getTemplateArgs();
David Greenedb445972011-10-05 22:42:07 +00002488
Sean Silvacc951b22013-01-09 05:28:12 +00002489 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2490 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2491 assert(RV && "Template arg doesn't exist?");
2492 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002493 }
2494
2495 return false;
2496}
2497
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002498/// ParseDefm - Parse the instantiation of a multiclass.
2499///
2500/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2501///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002502bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002503 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002504 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002505 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002506
Craig Topperb21afc62013-01-07 05:09:33 +00002507 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002508 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002509 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002510
Jordan Rosef12e8a92013-01-10 18:50:11 +00002511 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002512 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002513 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002514
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002515 // Keep track of the new generated record definitions.
2516 std::vector<Record*> NewRecDefs;
2517
2518 // This record also inherits from a regular class (non-multiclass)?
2519 bool InheritFromClass = false;
2520
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002521 // eat the colon.
2522 Lex.Lex();
2523
Chris Lattner526c8cb2009-06-21 03:39:35 +00002524 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002525 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002526
2527 while (1) {
Craig Topper011817a2014-04-09 04:50:04 +00002528 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002529
2530 // To instantiate a multiclass, we need to first get the multiclass, then
2531 // instantiate each def contained in the multiclass with the SubClassRef
2532 // template parameters.
2533 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2534 assert(MC && "Didn't lookup multiclass correctly?");
David Greeneaf8ee2c2011-07-29 22:43:06 +00002535 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002536
2537 // Verify that the correct number of template arguments were specified.
David Greenedb10e692011-10-19 13:02:42 +00002538 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002539 if (TArgs.size() < TemplateVals.size())
2540 return Error(SubClassLoc,
2541 "more template args specified than multiclass expects");
2542
2543 // Loop over all the def's in the multiclass, instantiating each one.
2544 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2545 Record *DefProto = MC->DefPrototypes[i];
2546
Jordan Rosef12e8a92013-01-10 18:50:11 +00002547 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix,
2548 SMRange(DefmLoc,
2549 DefmPrefixEndLoc));
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002550 if (!CurRec)
2551 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002552
Jordan Rosef12e8a92013-01-10 18:50:11 +00002553 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002554 TArgs, TemplateVals, true/*Delete args*/))
2555 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002556
Jordan Rosef12e8a92013-01-10 18:50:11 +00002557 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002558 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002559
2560 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002561 }
2562
David Greenedb445972011-10-05 22:42:07 +00002563
David Greenef00919a2009-04-22 22:17:51 +00002564 if (Lex.getCode() != tgtok::comma) break;
2565 Lex.Lex(); // eat ','.
2566
Craig Topper998a39a2013-08-20 04:22:09 +00002567 if (Lex.getCode() != tgtok::Id)
2568 return TokError("expected identifier");
2569
David Greenef00919a2009-04-22 22:17:51 +00002570 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002571
2572 // A defm can inherit from regular classes (non-multiclass) as
2573 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002574 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002575
2576 if (InheritFromClass)
2577 break;
2578
Craig Topper011817a2014-04-09 04:50:04 +00002579 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002580 }
2581
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002582 if (InheritFromClass) {
2583 // Process all the classes to inherit as if they were part of a
2584 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002585 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002586 while (1) {
2587 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002588 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002589
2590 // Get the expanded definition prototypes and teach them about
2591 // the record values the current class to inherit has
2592 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2593 Record *CurRec = NewRecDefs[i];
2594
2595 // Add it.
2596 if (AddSubClass(CurRec, SubClass))
2597 return true;
2598
Sean Silvacb1a75e2013-01-09 04:49:14 +00002599 if (ApplyLetStack(CurRec))
2600 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002601 }
2602
2603 if (Lex.getCode() != tgtok::comma) break;
2604 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002605 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002606 }
2607 }
2608
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002609 if (!CurMultiClass)
2610 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene50c09122011-08-10 18:27:46 +00002611 // See Record::setName(). This resolve step will see any new
2612 // name for the def that might have been created when resolving
2613 // inheritance, values and arguments above.
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002614 NewRecDefs[i]->resolveReferences();
2615
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002616 if (Lex.getCode() != tgtok::semi)
2617 return TokError("expected ';' at end of defm");
2618 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002619
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002620 return false;
2621}
2622
2623/// ParseObject
2624/// Object ::= ClassInst
2625/// Object ::= DefInst
2626/// Object ::= MultiClassInst
2627/// Object ::= DefMInst
2628/// Object ::= LETCommand '{' ObjectList '}'
2629/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002630bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002631 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002632 default:
2633 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002634 case tgtok::Let: return ParseTopLevelLet(MC);
2635 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002636 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002637 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002638 case tgtok::Class: return ParseClass();
2639 case tgtok::MultiClass: return ParseMultiClass();
2640 }
2641}
2642
2643/// ParseObjectList
2644/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002645bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002646 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002647 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002648 return true;
2649 }
2650 return false;
2651}
2652
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002653bool TGParser::ParseFile() {
2654 Lex.Lex(); // Prime the lexer.
2655 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002656
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002657 // If we have unread input at the end of the file, report it.
2658 if (Lex.getCode() == tgtok::Eof)
2659 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002660
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002661 return TokError("Unexpected input at top level");
2662}
2663