blob: 66c6b85d696ab69a23122802e9b23e6d2329eaab [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)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001189 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001190 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
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001305 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001306
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001307 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1308 // first. We'll first read everything in to a vector, then we can reverse
1309 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001310 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001311 // bits<n> values are allowed to initialize n bits.
1312 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1313 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1314 NewBits.push_back(BI->getBit((e - i) - 1));
1315 continue;
1316 }
1317 // All other values must be convertible to just a single bit.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001318 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001319 if (!Bit) {
Chris Lattner52416952007-11-22 21:06:59 +00001320 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1321 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001322 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001323 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001324 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001325 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001326 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001327 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001328 }
1329 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1330 Lex.Lex(); // eat the '['
David Greeneaf8ee2c2011-07-29 22:43:06 +00001331 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001332
Craig Topper011817a2014-04-09 04:50:04 +00001333 RecTy *DeducedEltTy = nullptr;
1334 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001335
Craig Topper011817a2014-04-09 04:50:04 +00001336 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001337 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001338 if (!ListType) {
Alp Tokere69170a2014-06-26 22:52:05 +00001339 std::string s;
1340 raw_string_ostream ss(s);
Reid Klecknerd78273f2013-08-06 22:51:21 +00001341 ss << "Type mismatch for list, expected list type, got "
1342 << ItemType->getAsString();
1343 TokError(ss.str());
Craig Topper011817a2014-04-09 04:50:04 +00001344 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001345 }
1346 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001347 }
David Greene8618f952009-06-08 20:23:18 +00001348
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001349 if (Lex.getCode() != tgtok::r_square) {
Craig Topper011817a2014-04-09 04:50:04 +00001350 Vals = ParseValueList(CurRec, nullptr,
1351 GivenListTy ? GivenListTy->getElementType() : nullptr);
1352 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001353 }
1354 if (Lex.getCode() != tgtok::r_square) {
1355 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001356 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001357 }
1358 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001359
Craig Topper011817a2014-04-09 04:50:04 +00001360 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001361 if (Lex.getCode() == tgtok::less) {
1362 // Optional list element type
1363 Lex.Lex(); // eat the '<'
1364
1365 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001366 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001367 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001368 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001369 }
1370
1371 if (Lex.getCode() != tgtok::greater) {
1372 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001373 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001374 }
1375 Lex.Lex(); // eat the '>'
1376 }
1377
1378 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001379 RecTy *EltTy = nullptr;
David Greeneaf8ee2c2011-07-29 22:43:06 +00001380 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greene8618f952009-06-08 20:23:18 +00001381 i != ie;
1382 ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +00001383 TypedInit *TArg = dyn_cast<TypedInit>(*i);
Craig Topper011817a2014-04-09 04:50:04 +00001384 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001385 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001386 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001387 }
Craig Topper011817a2014-04-09 04:50:04 +00001388 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001389 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001390 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001391 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001392 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001393 }
Bob Wilson7248f862009-11-22 04:24:42 +00001394 } else {
David Greene8618f952009-06-08 20:23:18 +00001395 EltTy = TArg->getType();
1396 }
1397 }
1398
Craig Topper011817a2014-04-09 04:50:04 +00001399 if (GivenEltTy) {
1400 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001401 // Verify consistency
1402 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1403 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001404 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001405 }
1406 }
1407 EltTy = GivenEltTy;
1408 }
1409
Craig Topper011817a2014-04-09 04:50:04 +00001410 if (!EltTy) {
1411 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001412 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001413 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001414 }
1415 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001416 } else {
David Greene8618f952009-06-08 20:23:18 +00001417 // Make sure the deduced type is compatible with the given type
1418 if (GivenListTy) {
1419 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1420 TokError("Element type mismatch for list");
Craig Topper011817a2014-04-09 04:50:04 +00001421 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001422 }
1423 }
1424 DeducedEltTy = EltTy;
1425 }
Bob Wilson7248f862009-11-22 04:24:42 +00001426
David Greenee32ebf22011-07-29 19:07:07 +00001427 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001428 }
1429 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1430 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001431 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001432 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001433 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001434 }
Bob Wilson7248f862009-11-22 04:24:42 +00001435
David Greeneaf8ee2c2011-07-29 22:43:06 +00001436 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001437 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001438
Nate Begemandbe3f772009-03-19 05:21:56 +00001439 // If the operator name is present, parse it.
1440 std::string OperatorName;
1441 if (Lex.getCode() == tgtok::colon) {
1442 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1443 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001444 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001445 }
1446 OperatorName = Lex.getCurStrVal();
1447 Lex.Lex(); // eat the VarName.
1448 }
Bob Wilson7248f862009-11-22 04:24:42 +00001449
David Greeneaf8ee2c2011-07-29 22:43:06 +00001450 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001451 if (Lex.getCode() != tgtok::r_paren) {
1452 DagArgs = ParseDagArgList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001453 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001454 }
Bob Wilson7248f862009-11-22 04:24:42 +00001455
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001456 if (Lex.getCode() != tgtok::r_paren) {
1457 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001458 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001459 }
1460 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001461
David Greenee32ebf22011-07-29 19:07:07 +00001462 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001463 }
Bob Wilson7248f862009-11-22 04:24:42 +00001464
David Greene2f7cf7f2011-01-07 17:05:37 +00001465 case tgtok::XHead:
1466 case tgtok::XTail:
1467 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001468 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001469 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001470 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001471 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +00001472 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001473 case tgtok::XSRL:
1474 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001475 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001476 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001477 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001478 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001479 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001480 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001481 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001482 }
1483 }
Bob Wilson7248f862009-11-22 04:24:42 +00001484
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001485 return R;
1486}
1487
1488/// ParseValue - Parse a tblgen value. This returns null on error.
1489///
1490/// Value ::= SimpleValue ValueSuffix*
1491/// ValueSuffix ::= '{' BitList '}'
1492/// ValueSuffix ::= '[' BitList ']'
1493/// ValueSuffix ::= '.' ID
1494///
David Greened4263a62011-10-19 13:04:20 +00001495Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1496 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001497 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001498
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001499 // Parse the suffixes now if present.
1500 while (1) {
1501 switch (Lex.getCode()) {
1502 default: return Result;
1503 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001504 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001505 // This is the beginning of the object body.
1506 return Result;
1507
Chris Lattner526c8cb2009-06-21 03:39:35 +00001508 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001509 Lex.Lex(); // eat the '{'
1510 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001511 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001512
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001513 // Reverse the bitlist.
1514 std::reverse(Ranges.begin(), Ranges.end());
1515 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001516 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001517 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001518 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001519 }
Bob Wilson7248f862009-11-22 04:24:42 +00001520
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001521 // Eat the '}'.
1522 if (Lex.getCode() != tgtok::r_brace) {
1523 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001524 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001525 }
1526 Lex.Lex();
1527 break;
1528 }
1529 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001530 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001531 Lex.Lex(); // eat the '['
1532 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001533 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001534
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001535 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001536 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001537 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001538 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001539 }
Bob Wilson7248f862009-11-22 04:24:42 +00001540
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001541 // Eat the ']'.
1542 if (Lex.getCode() != tgtok::r_square) {
1543 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001544 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001545 }
1546 Lex.Lex();
1547 break;
1548 }
1549 case tgtok::period:
1550 if (Lex.Lex() != tgtok::Id) { // eat the .
1551 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001552 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001553 }
1554 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001555 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001556 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001557 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001558 }
David Greenee32ebf22011-07-29 19:07:07 +00001559 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001560 Lex.Lex(); // eat field name
1561 break;
David Greene8e85b482011-10-19 13:04:43 +00001562
1563 case tgtok::paste:
1564 SMLoc PasteLoc = Lex.getLoc();
1565
1566 // Create a !strconcat() operation, first casting each operand to
1567 // a string if necessary.
1568
Sean Silvafb509ed2012-10-10 20:24:43 +00001569 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001570 if (!LHS) {
1571 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001572 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001573 }
1574
1575 if (LHS->getType() != StringRecTy::get()) {
1576 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1577 }
1578
Craig Topper011817a2014-04-09 04:50:04 +00001579 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001580
1581 Lex.Lex(); // Eat the '#'.
1582 switch (Lex.getCode()) {
1583 case tgtok::colon:
1584 case tgtok::semi:
1585 case tgtok::l_brace:
1586 // These are all of the tokens that can begin an object body.
1587 // Some of these can also begin values but we disallow those cases
1588 // because they are unlikely to be useful.
1589
1590 // Trailing paste, concat with an empty string.
1591 RHS = StringInit::get("");
1592 break;
1593
1594 default:
1595 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001596 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001597 if (!RHS) {
1598 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001599 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001600 }
1601
1602 if (RHS->getType() != StringRecTy::get()) {
1603 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1604 }
1605
1606 break;
1607 }
1608
1609 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1610 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1611 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001612 }
1613 }
1614}
1615
1616/// ParseDagArgList - Parse the argument list for a dag literal expression.
1617///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001618/// DagArg ::= Value (':' VARNAME)?
1619/// DagArg ::= VARNAME
1620/// DagArgList ::= DagArg
1621/// DagArgList ::= DagArgList ',' DagArg
David Greeneaf8ee2c2011-07-29 22:43:06 +00001622std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001623TGParser::ParseDagArgList(Record *CurRec) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001624 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson7248f862009-11-22 04:24:42 +00001625
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001626 while (1) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001627 // DagArg ::= VARNAME
1628 if (Lex.getCode() == tgtok::VarName) {
1629 // A missing value is treated like '?'.
1630 Result.push_back(std::make_pair(UnsetInit::get(), Lex.getCurStrVal()));
1631 Lex.Lex();
1632 } else {
1633 // DagArg ::= Value (':' VARNAME)?
1634 Init *Val = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001635 if (!Val)
David Greeneaf8ee2c2011-07-29 22:43:06 +00001636 return std::vector<std::pair<llvm::Init*, std::string> >();
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001637
1638 // If the variable name is present, add it.
1639 std::string VarName;
1640 if (Lex.getCode() == tgtok::colon) {
1641 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1642 TokError("expected variable name in dag literal");
1643 return std::vector<std::pair<llvm::Init*, std::string> >();
1644 }
1645 VarName = Lex.getCurStrVal();
1646 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001647 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001648
1649 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001650 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001651 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001652 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001653 }
Bob Wilson7248f862009-11-22 04:24:42 +00001654
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001655 return Result;
1656}
1657
1658
1659/// ParseValueList - Parse a comma separated list of values, returning them as a
1660/// vector. Note that this always expects to be able to parse at least one
1661/// value. It returns an empty list if this is not possible.
1662///
1663/// ValueList ::= Value (',' Value)
1664///
David Greeneaf8ee2c2011-07-29 22:43:06 +00001665std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopher71520a82011-07-11 23:06:52 +00001666 RecTy *EltTy) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001667 std::vector<Init*> Result;
David Greene8618f952009-06-08 20:23:18 +00001668 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001669 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001670 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001671 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001672 if (!TArgs.size()) {
1673 TokError("template argument provided to non-template class");
1674 return std::vector<Init*>();
1675 }
David Greene8618f952009-06-08 20:23:18 +00001676 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001677 if (!RV) {
1678 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1679 << ")\n";
1680 }
David Greene8618f952009-06-08 20:23:18 +00001681 assert(RV && "Template argument record not found??");
1682 ItemType = RV->getType();
1683 ++ArgN;
1684 }
1685 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001686 if (!Result.back()) return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001687
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001688 while (Lex.getCode() == tgtok::comma) {
1689 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001690
Craig Topper011817a2014-04-09 04:50:04 +00001691 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001692 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001693 if (ArgN >= TArgs.size()) {
1694 TokError("too many template arguments");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001695 return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001696 }
David Greene8618f952009-06-08 20:23:18 +00001697 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1698 assert(RV && "Template argument record not found??");
1699 ItemType = RV->getType();
1700 ++ArgN;
1701 }
1702 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001703 if (!Result.back()) return std::vector<Init*>();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001704 }
Bob Wilson7248f862009-11-22 04:24:42 +00001705
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001706 return Result;
1707}
1708
1709
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001710/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1711/// empty string on error. This can happen in a number of different context's,
1712/// including within a def or in the template args for a def (which which case
1713/// CurRec will be non-null) and within the template args for a multiclass (in
1714/// which case CurRec will be null, but CurMultiClass will be set). This can
1715/// also happen within a def that is within a multiclass, which will set both
1716/// CurRec and CurMultiClass.
1717///
1718/// Declaration ::= FIELD? Type ID ('=' Value)?
1719///
David Greenedb10e692011-10-19 13:02:42 +00001720Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001721 bool ParsingTemplateArgs) {
1722 // Read the field prefix if present.
1723 bool HasField = Lex.getCode() == tgtok::Field;
1724 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001725
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001726 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001727 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001728
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001729 if (Lex.getCode() != tgtok::Id) {
1730 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001731 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001732 }
Bob Wilson7248f862009-11-22 04:24:42 +00001733
Chris Lattner526c8cb2009-06-21 03:39:35 +00001734 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001735 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001736 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001737
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001738 if (ParsingTemplateArgs) {
1739 if (CurRec) {
David Greenedb10e692011-10-19 13:02:42 +00001740 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001741 } else {
1742 assert(CurMultiClass);
1743 }
1744 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001745 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1746 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001747 }
Bob Wilson7248f862009-11-22 04:24:42 +00001748
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001749 // Add the value.
1750 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001751 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001752
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001753 // If a value is present, parse it.
1754 if (Lex.getCode() == tgtok::equal) {
1755 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001756 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001757 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001758 if (!Val ||
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001759 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001760 // Return the name, even if an error is thrown. This is so that we can
1761 // continue to make some progress, even without the value having been
1762 // initialized.
1763 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001764 }
Bob Wilson7248f862009-11-22 04:24:42 +00001765
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001766 return DeclName;
1767}
1768
David Greenefb927af2012-02-22 16:09:41 +00001769/// ParseForeachDeclaration - Read a foreach declaration, returning
1770/// the name of the declared object or a NULL Init on error. Return
1771/// the name of the parsed initializer list through ForeachListName.
1772///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001773/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1774/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1775/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001776///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001777VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001778 if (Lex.getCode() != tgtok::Id) {
1779 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001780 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001781 }
1782
1783 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1784 Lex.Lex();
1785
1786 // If a value is present, parse it.
1787 if (Lex.getCode() != tgtok::equal) {
1788 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001789 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001790 }
1791 Lex.Lex(); // Eat the '='
1792
Craig Topper011817a2014-04-09 04:50:04 +00001793 RecTy *IterType = nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001794 std::vector<unsigned> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001795
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001796 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001797 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001798 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001799 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001800 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001801 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001802 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001803 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001804 }
1805 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001806 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001807 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001808 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001809 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001810 }
1811 IterType = ListType->getElementType();
1812 break;
David Greenefb927af2012-02-22 16:09:41 +00001813 }
1814
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001815 case tgtok::IntVal: { // RangePiece.
1816 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001817 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001818 break;
David Greenefb927af2012-02-22 16:09:41 +00001819 }
1820
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001821 case tgtok::l_brace: { // '{' RangeList '}'
1822 Lex.Lex(); // eat the '{'
1823 Ranges = ParseRangeList();
1824 if (Lex.getCode() != tgtok::r_brace) {
1825 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001826 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001827 }
1828 Lex.Lex();
1829 break;
1830 }
1831 }
David Greenefb927af2012-02-22 16:09:41 +00001832
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001833 if (!Ranges.empty()) {
1834 assert(!IterType && "Type already initialized?");
1835 IterType = IntRecTy::get();
1836 std::vector<Init*> Values;
1837 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
1838 Values.push_back(IntInit::get(Ranges[i]));
1839 ForeachListValue = ListInit::get(Values, IterType);
1840 }
1841
1842 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001843 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001844
1845 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001846}
1847
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001848/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1849/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1850/// template args for a def, which may or may not be in a multiclass. If null,
1851/// these are the template args for a multiclass.
1852///
1853/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001854///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001855bool TGParser::ParseTemplateArgList(Record *CurRec) {
1856 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1857 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001858
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001859 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001860
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001861 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001862 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001863 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001864 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001865
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001866 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001867
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001868 while (Lex.getCode() == tgtok::comma) {
1869 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001870
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001871 // Read the following declarations.
1872 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001873 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001874 return true;
1875 TheRecToAddTo->addTemplateArg(TemplArg);
1876 }
Bob Wilson7248f862009-11-22 04:24:42 +00001877
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001878 if (Lex.getCode() != tgtok::greater)
1879 return TokError("expected '>' at end of template argument list");
1880 Lex.Lex(); // eat the '>'.
1881 return false;
1882}
1883
1884
1885/// ParseBodyItem - Parse a single item at within the body of a def or class.
1886///
1887/// BodyItem ::= Declaration ';'
1888/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1889bool TGParser::ParseBodyItem(Record *CurRec) {
1890 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001891 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001892 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001893
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001894 if (Lex.getCode() != tgtok::semi)
1895 return TokError("expected ';' after declaration");
1896 Lex.Lex();
1897 return false;
1898 }
1899
1900 // LET ID OptionalRangeList '=' Value ';'
1901 if (Lex.Lex() != tgtok::Id)
1902 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001903
Chris Lattner526c8cb2009-06-21 03:39:35 +00001904 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001905 std::string FieldName = Lex.getCurStrVal();
1906 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001907
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001908 std::vector<unsigned> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001909 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001910 return true;
1911 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001912
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001913 if (Lex.getCode() != tgtok::equal)
1914 return TokError("expected '=' in let expression");
1915 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001916
David Greene8618f952009-06-08 20:23:18 +00001917 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001918 if (!Field)
David Greene8618f952009-06-08 20:23:18 +00001919 return TokError("Value '" + FieldName + "' unknown!");
1920
1921 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001922
David Greeneaf8ee2c2011-07-29 22:43:06 +00001923 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001924 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001925
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001926 if (Lex.getCode() != tgtok::semi)
1927 return TokError("expected ';' after let expression");
1928 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001929
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001930 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1931}
1932
1933/// ParseBody - Read the body of a class or def. Return true on error, false on
1934/// success.
1935///
1936/// Body ::= ';'
1937/// Body ::= '{' BodyList '}'
1938/// BodyList BodyItem*
1939///
1940bool TGParser::ParseBody(Record *CurRec) {
1941 // If this is a null definition, just eat the semi and return.
1942 if (Lex.getCode() == tgtok::semi) {
1943 Lex.Lex();
1944 return false;
1945 }
Bob Wilson7248f862009-11-22 04:24:42 +00001946
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001947 if (Lex.getCode() != tgtok::l_brace)
1948 return TokError("Expected ';' or '{' to start body");
1949 // Eat the '{'.
1950 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001951
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001952 while (Lex.getCode() != tgtok::r_brace)
1953 if (ParseBodyItem(CurRec))
1954 return true;
1955
1956 // Eat the '}'.
1957 Lex.Lex();
1958 return false;
1959}
1960
Sean Silvacb1a75e2013-01-09 04:49:14 +00001961/// \brief Apply the current let bindings to \a CurRec.
1962/// \returns true on error, false otherwise.
1963bool TGParser::ApplyLetStack(Record *CurRec) {
1964 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1965 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1966 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1967 LetStack[i][j].Bits, LetStack[i][j].Value))
1968 return true;
1969 return false;
1970}
1971
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001972/// ParseObjectBody - Parse the body of a def or class. This consists of an
1973/// optional ClassList followed by a Body. CurRec is the current def or class
1974/// that is being parsed.
1975///
1976/// ObjectBody ::= BaseClassList Body
1977/// BaseClassList ::= /*empty*/
1978/// BaseClassList ::= ':' BaseClassListNE
1979/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1980///
1981bool TGParser::ParseObjectBody(Record *CurRec) {
1982 // If there is a baseclass list, read it.
1983 if (Lex.getCode() == tgtok::colon) {
1984 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001985
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001986 // Read all of the subclasses.
1987 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1988 while (1) {
1989 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00001990 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001991
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001992 // Add it.
1993 if (AddSubClass(CurRec, SubClass))
1994 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001995
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001996 if (Lex.getCode() != tgtok::comma) break;
1997 Lex.Lex(); // eat ','.
1998 SubClass = ParseSubClassReference(CurRec, false);
1999 }
2000 }
2001
Sean Silvacb1a75e2013-01-09 04:49:14 +00002002 if (ApplyLetStack(CurRec))
2003 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002004
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002005 return ParseBody(CurRec);
2006}
2007
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002008/// ParseDef - Parse and return a top level or multiclass def, return the record
2009/// corresponding to it. This returns null on error.
2010///
2011/// DefInst ::= DEF ObjectName ObjectBody
2012///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002013bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00002014 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002015 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00002016 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002017
2018 // Parse ObjectName and make a record for it.
Jordan Roseabdd99b2013-01-10 18:50:05 +00002019 Record *CurRec;
2020 Init *Name = ParseObjectName(CurMultiClass);
2021 if (Name)
2022 CurRec = new Record(Name, DefLoc, Records);
2023 else
2024 CurRec = new Record(GetNewAnonymousName(), DefLoc, Records,
2025 /*IsAnonymous=*/true);
Bob Wilson7248f862009-11-22 04:24:42 +00002026
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002027 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002028 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002029
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002030 // Ensure redefinition doesn't happen.
David Greeneebb006f2012-01-28 00:03:24 +00002031 if (Records.getDef(CurRec->getNameInitAsString())) {
David Greene3a20f5a2011-10-19 13:03:45 +00002032 Error(DefLoc, "def '" + CurRec->getNameInitAsString()
2033 + "' already defined");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002034 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002035 }
2036 Records.addDef(CurRec);
Hal Finkela8c1f462014-01-02 20:47:09 +00002037
2038 if (ParseObjectBody(CurRec))
2039 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002040 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002041 // Parse the body before adding this prototype to the DefPrototypes vector.
2042 // That way implicit definitions will be added to the DefPrototypes vector
2043 // before this object, instantiated prior to defs derived from this object,
2044 // and this available for indirect name resolution when defs derived from
2045 // this object are instantiated.
2046 if (ParseObjectBody(CurRec))
2047 return true;
2048
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002049 // Otherwise, a def inside a multiclass, add it to the multiclass.
2050 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene07e055f2011-10-19 13:03:51 +00002051 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2052 == CurRec->getNameInit()) {
2053 Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002054 "' already defined in this multiclass!");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002055 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002056 }
2057 CurMultiClass->DefPrototypes.push_back(CurRec);
Hal Finkela8c1f462014-01-02 20:47:09 +00002058 } else if (ParseObjectBody(CurRec))
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002059 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002060
Craig Topper011817a2014-04-09 04:50:04 +00002061 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002062 // See Record::setName(). This resolve step will see any new name
2063 // for the def that might have been created when resolving
2064 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002065 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002066
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002067 // If ObjectBody has template arguments, it's an error.
2068 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002069
2070 if (CurMultiClass) {
2071 // Copy the template arguments for the multiclass into the def.
David Greenedb10e692011-10-19 13:02:42 +00002072 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002073 CurMultiClass->Rec.getTemplateArgs();
2074
2075 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2076 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
2077 assert(RV && "Template arg doesn't exist?");
2078 CurRec->addValue(*RV);
2079 }
2080 }
2081
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002082 if (ProcessForeachDefs(CurRec, DefLoc)) {
David Greenefb927af2012-02-22 16:09:41 +00002083 Error(DefLoc,
2084 "Could not process loops for def" + CurRec->getNameInitAsString());
2085 return true;
2086 }
2087
2088 return false;
2089}
2090
2091/// ParseForeach - Parse a for statement. Return the record corresponding
2092/// to it. This returns true on error.
2093///
2094/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2095/// Foreach ::= FOREACH Declaration IN Object
2096///
2097bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2098 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2099 Lex.Lex(); // Eat the 'for' token.
2100
2101 // Make a temporary object to record items associated with the for
2102 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002103 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002104 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002105 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002106 return TokError("expected declaration in for");
2107
2108 if (Lex.getCode() != tgtok::In)
2109 return TokError("Unknown tok");
2110 Lex.Lex(); // Eat the in
2111
2112 // Create a loop object and remember it.
2113 Loops.push_back(ForeachLoop(IterName, ListValue));
2114
2115 if (Lex.getCode() != tgtok::l_brace) {
2116 // FOREACH Declaration IN Object
2117 if (ParseObject(CurMultiClass))
2118 return true;
2119 }
2120 else {
2121 SMLoc BraceLoc = Lex.getLoc();
2122 // Otherwise, this is a group foreach.
2123 Lex.Lex(); // eat the '{'.
2124
2125 // Parse the object list.
2126 if (ParseObjectList(CurMultiClass))
2127 return true;
2128
2129 if (Lex.getCode() != tgtok::r_brace) {
2130 TokError("expected '}' at end of foreach command");
2131 return Error(BraceLoc, "to match this '{'");
2132 }
2133 Lex.Lex(); // Eat the }
2134 }
2135
2136 // We've processed everything in this loop.
2137 Loops.pop_back();
2138
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002139 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002140}
2141
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002142/// ParseClass - Parse a tblgen class definition.
2143///
2144/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2145///
2146bool TGParser::ParseClass() {
2147 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2148 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002149
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002150 if (Lex.getCode() != tgtok::Id)
2151 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002152
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002153 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2154 if (CurRec) {
2155 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002156 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002157 !CurRec->getSuperClasses().empty() ||
2158 !CurRec->getTemplateArgs().empty())
David Greene8eed9982011-10-19 13:03:58 +00002159 return TokError("Class '" + CurRec->getNameInitAsString()
2160 + "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002161 } else {
2162 // If this is the first reference to this class, create and add it.
Chris Lattner89dcb682010-12-15 04:48:22 +00002163 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002164 Records.addClass(CurRec);
2165 }
2166 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002167
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002168 // If there are template args, parse them.
2169 if (Lex.getCode() == tgtok::less)
2170 if (ParseTemplateArgList(CurRec))
2171 return true;
2172
2173 // Finally, parse the object body.
2174 return ParseObjectBody(CurRec);
2175}
2176
2177/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2178/// of LetRecords.
2179///
2180/// LetList ::= LetItem (',' LetItem)*
2181/// LetItem ::= ID OptionalRangeList '=' Value
2182///
2183std::vector<LetRecord> TGParser::ParseLetList() {
2184 std::vector<LetRecord> Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002185
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002186 while (1) {
2187 if (Lex.getCode() != tgtok::Id) {
2188 TokError("expected identifier in let definition");
2189 return std::vector<LetRecord>();
2190 }
2191 std::string Name = Lex.getCurStrVal();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002192 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002193 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002194
2195 // Check for an optional RangeList.
2196 std::vector<unsigned> Bits;
Bob Wilson7248f862009-11-22 04:24:42 +00002197 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002198 return std::vector<LetRecord>();
2199 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002200
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002201 if (Lex.getCode() != tgtok::equal) {
2202 TokError("expected '=' in let expression");
2203 return std::vector<LetRecord>();
2204 }
2205 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002206
Craig Topper011817a2014-04-09 04:50:04 +00002207 Init *Val = ParseValue(nullptr);
2208 if (!Val) return std::vector<LetRecord>();
Bob Wilson7248f862009-11-22 04:24:42 +00002209
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002210 // Now that we have everything, add the record.
2211 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson7248f862009-11-22 04:24:42 +00002212
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002213 if (Lex.getCode() != tgtok::comma)
2214 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002215 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002216 }
2217}
2218
2219/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002220/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002221///
2222/// Object ::= LET LetList IN '{' ObjectList '}'
2223/// Object ::= LET LetList IN Object
2224///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002225bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002226 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2227 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002228
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002229 // Add this entry to the let stack.
2230 std::vector<LetRecord> LetInfo = ParseLetList();
2231 if (LetInfo.empty()) return true;
2232 LetStack.push_back(LetInfo);
2233
2234 if (Lex.getCode() != tgtok::In)
2235 return TokError("expected 'in' at end of top-level 'let'");
2236 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002237
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002238 // If this is a scalar let, just handle it now
2239 if (Lex.getCode() != tgtok::l_brace) {
2240 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002241 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002242 return true;
2243 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002244 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002245 // Otherwise, this is a group let.
2246 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002247
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002248 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002249 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002250 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002251
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002252 if (Lex.getCode() != tgtok::r_brace) {
2253 TokError("expected '}' at end of top level let command");
2254 return Error(BraceLoc, "to match this '{'");
2255 }
2256 Lex.Lex();
2257 }
Bob Wilson7248f862009-11-22 04:24:42 +00002258
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002259 // Outside this let scope, this let block is not active.
2260 LetStack.pop_back();
2261 return false;
2262}
2263
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002264/// ParseMultiClass - Parse a multiclass definition.
2265///
Bob Wilson3d948162009-04-28 19:41:44 +00002266/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002267/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2268/// MultiClassObject ::= DefInst
2269/// MultiClassObject ::= MultiClassInst
2270/// MultiClassObject ::= DefMInst
2271/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2272/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002273///
2274bool TGParser::ParseMultiClass() {
2275 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2276 Lex.Lex(); // Eat the multiclass token.
2277
2278 if (Lex.getCode() != tgtok::Id)
2279 return TokError("expected identifier after multiclass for name");
2280 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002281
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002282 if (MultiClasses.count(Name))
2283 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002284
Chris Lattner77d369c2010-12-13 00:23:57 +00002285 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
2286 Lex.getLoc(), Records);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002287 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002288
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002289 // If there are template args, parse them.
2290 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002291 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002292 return true;
2293
David Greene7049e792009-04-24 16:55:41 +00002294 bool inherits = false;
2295
David Greene753ed8f2009-04-22 16:42:54 +00002296 // If there are submulticlasses, parse them.
2297 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002298 inherits = true;
2299
David Greene753ed8f2009-04-22 16:42:54 +00002300 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002301
David Greene753ed8f2009-04-22 16:42:54 +00002302 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002303 SubMultiClassReference SubMultiClass =
2304 ParseSubMultiClassReference(CurMultiClass);
David Greene753ed8f2009-04-22 16:42:54 +00002305 while (1) {
2306 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002307 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002308
David Greene753ed8f2009-04-22 16:42:54 +00002309 // Add it.
2310 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2311 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002312
David Greene753ed8f2009-04-22 16:42:54 +00002313 if (Lex.getCode() != tgtok::comma) break;
2314 Lex.Lex(); // eat ','.
2315 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2316 }
2317 }
2318
David Greene7049e792009-04-24 16:55:41 +00002319 if (Lex.getCode() != tgtok::l_brace) {
2320 if (!inherits)
2321 return TokError("expected '{' in multiclass definition");
Bob Wilson7248f862009-11-22 04:24:42 +00002322 else if (Lex.getCode() != tgtok::semi)
2323 return TokError("expected ';' in multiclass definition");
David Greene7049e792009-04-24 16:55:41 +00002324 else
Bob Wilson7248f862009-11-22 04:24:42 +00002325 Lex.Lex(); // eat the ';'.
2326 } else {
David Greene7049e792009-04-24 16:55:41 +00002327 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2328 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002329
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002330 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002331 switch (Lex.getCode()) {
2332 default:
David Greene33f61992011-10-07 18:25:05 +00002333 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002334 case tgtok::Let:
2335 case tgtok::Def:
2336 case tgtok::Defm:
David Greenefb927af2012-02-22 16:09:41 +00002337 case tgtok::Foreach:
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002338 if (ParseObject(CurMultiClass))
2339 return true;
2340 break;
2341 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002342 }
David Greene7049e792009-04-24 16:55:41 +00002343 Lex.Lex(); // eat the '}'.
2344 }
Bob Wilson7248f862009-11-22 04:24:42 +00002345
Craig Topper011817a2014-04-09 04:50:04 +00002346 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002347 return false;
2348}
2349
David Greenedb445972011-10-05 22:42:07 +00002350Record *TGParser::
2351InstantiateMulticlassDef(MultiClass &MC,
2352 Record *DefProto,
Hal Finkelf2a0b2b2014-01-02 19:35:33 +00002353 Init *&DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002354 SMRange DefmPrefixRange) {
David Greene5d5d88c2011-10-19 13:04:31 +00002355 // We need to preserve DefProto so it can be reused for later
2356 // instantiations, so create a new Record to inherit from it.
2357
David Greenedb445972011-10-05 22:42:07 +00002358 // Add in the defm name. If the defm prefix is empty, give each
2359 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2360 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2361 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002362
Jordan Roseabdd99b2013-01-10 18:50:05 +00002363 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002364 if (!DefmPrefix) {
David Greene5d5d88c2011-10-19 13:04:31 +00002365 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002366 IsAnonymous = true;
2367 }
David Greene5d5d88c2011-10-19 13:04:31 +00002368
2369 Init *DefName = DefProto->getNameInit();
2370
Sean Silvafb509ed2012-10-10 20:24:43 +00002371 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002372
Craig Topper011817a2014-04-09 04:50:04 +00002373 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002374 // We have a fully expanded string so there are no operators to
2375 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002376 DefName =
2377 BinOpInit::get(BinOpInit::STRCONCAT,
2378 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2379 StringRecTy::get())->Fold(DefProto, &MC),
2380 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2381 }
David Greene5d5d88c2011-10-19 13:04:31 +00002382
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002383 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002384 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002385 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002386 Record *CurRec = new Record(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002387
2388 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002389 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002390 Ref.Rec = DefProto;
2391 AddSubClass(CurRec, Ref);
2392
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002393 // Set the value for NAME. We don't resolve references to it 'til later,
2394 // though, so that uses in nested multiclass names don't get
2395 // confused.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002396 if (SetValue(CurRec, Ref.RefRange.Start, "NAME", std::vector<unsigned>(),
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002397 DefmPrefix)) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002398 Error(DefmPrefixRange.Start, "Could not resolve "
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002399 + CurRec->getNameInitAsString() + ":NAME to '"
2400 + DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002401 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002402 }
David Greene8bf0d722011-10-19 13:04:35 +00002403
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002404 // If the DefNameString didn't resolve, we probably have a reference to
2405 // NAME and need to replace it. We need to do at least this much greedily,
2406 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002407 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002408 RecordVal *DefNameRV = CurRec->getValue("NAME");
2409 CurRec->resolveReferencesTo(DefNameRV);
2410 }
2411
2412 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002413 // Now that we're at the top level, resolve all NAME references
2414 // in the resultant defs that weren't in the def names themselves.
2415 RecordVal *DefNameRV = CurRec->getValue("NAME");
2416 CurRec->resolveReferencesTo(DefNameRV);
2417
2418 // Now that NAME references are resolved and we're at the top level of
2419 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002420 // currently in a multiclass, it means this defm appears inside a
2421 // multiclass and its name won't be fully resolvable until we see
2422 // the top-level defm. Therefore, we don't add this to the
2423 // RecordKeeper at this point. If we did we could get duplicate
2424 // defs as more than one probably refers to NAME or some other
2425 // common internal placeholder.
2426
2427 // Ensure redefinition doesn't happen.
2428 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002429 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002430 "' already defined, instantiating defm with subdef '" +
2431 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002432 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002433 }
2434
2435 Records.addDef(CurRec);
2436 }
2437
David Greenedb445972011-10-05 22:42:07 +00002438 return CurRec;
2439}
2440
2441bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2442 Record *CurRec,
2443 SMLoc DefmPrefixLoc,
2444 SMLoc SubClassLoc,
David Greenedb10e692011-10-19 13:02:42 +00002445 const std::vector<Init *> &TArgs,
David Greenedb445972011-10-05 22:42:07 +00002446 std::vector<Init *> &TemplateVals,
2447 bool DeleteArgs) {
2448 // Loop over all of the template arguments, setting them to the specified
2449 // value or leaving them as the default if necessary.
2450 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2451 // Check if a value is specified for this temp-arg.
2452 if (i < TemplateVals.size()) {
2453 // Set it now.
2454 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2455 TemplateVals[i]))
2456 return true;
2457
2458 // Resolve it next.
2459 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2460
2461 if (DeleteArgs)
2462 // Now remove it.
2463 CurRec->removeValue(TArgs[i]);
2464
2465 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2466 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenedb10e692011-10-19 13:02:42 +00002467 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2468 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2469 + "'");
David Greenedb445972011-10-05 22:42:07 +00002470 }
2471 }
2472 return false;
2473}
2474
2475bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2476 Record *CurRec,
2477 Record *DefProto,
2478 SMLoc DefmPrefixLoc) {
2479 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002480 if (ApplyLetStack(CurRec))
2481 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002482
David Greenedb445972011-10-05 22:42:07 +00002483 // Don't create a top level definition for defm inside multiclasses,
2484 // instead, only update the prototypes and bind the template args
2485 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002486 if (!CurMultiClass)
2487 return false;
2488 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2489 i != e; ++i)
2490 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2491 == CurRec->getNameInit())
2492 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2493 "' already defined in this multiclass!");
2494 CurMultiClass->DefPrototypes.push_back(CurRec);
David Greenedb445972011-10-05 22:42:07 +00002495
Sean Silvacc951b22013-01-09 05:28:12 +00002496 // Copy the template arguments for the multiclass into the new def.
2497 const std::vector<Init *> &TA =
2498 CurMultiClass->Rec.getTemplateArgs();
David Greenedb445972011-10-05 22:42:07 +00002499
Sean Silvacc951b22013-01-09 05:28:12 +00002500 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2501 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2502 assert(RV && "Template arg doesn't exist?");
2503 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002504 }
2505
2506 return false;
2507}
2508
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002509/// ParseDefm - Parse the instantiation of a multiclass.
2510///
2511/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2512///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002513bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002514 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002515 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002516 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002517
Craig Topperb21afc62013-01-07 05:09:33 +00002518 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002519 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002520 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002521
Jordan Rosef12e8a92013-01-10 18:50:11 +00002522 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002523 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002524 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002525
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002526 // Keep track of the new generated record definitions.
2527 std::vector<Record*> NewRecDefs;
2528
2529 // This record also inherits from a regular class (non-multiclass)?
2530 bool InheritFromClass = false;
2531
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002532 // eat the colon.
2533 Lex.Lex();
2534
Chris Lattner526c8cb2009-06-21 03:39:35 +00002535 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002536 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002537
2538 while (1) {
Craig Topper011817a2014-04-09 04:50:04 +00002539 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002540
2541 // To instantiate a multiclass, we need to first get the multiclass, then
2542 // instantiate each def contained in the multiclass with the SubClassRef
2543 // template parameters.
2544 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2545 assert(MC && "Didn't lookup multiclass correctly?");
David Greeneaf8ee2c2011-07-29 22:43:06 +00002546 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002547
2548 // Verify that the correct number of template arguments were specified.
David Greenedb10e692011-10-19 13:02:42 +00002549 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002550 if (TArgs.size() < TemplateVals.size())
2551 return Error(SubClassLoc,
2552 "more template args specified than multiclass expects");
2553
2554 // Loop over all the def's in the multiclass, instantiating each one.
2555 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2556 Record *DefProto = MC->DefPrototypes[i];
2557
Jordan Rosef12e8a92013-01-10 18:50:11 +00002558 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix,
2559 SMRange(DefmLoc,
2560 DefmPrefixEndLoc));
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002561 if (!CurRec)
2562 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002563
Jordan Rosef12e8a92013-01-10 18:50:11 +00002564 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002565 TArgs, TemplateVals, true/*Delete args*/))
2566 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002567
Jordan Rosef12e8a92013-01-10 18:50:11 +00002568 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002569 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002570
2571 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002572 }
2573
David Greenedb445972011-10-05 22:42:07 +00002574
David Greenef00919a2009-04-22 22:17:51 +00002575 if (Lex.getCode() != tgtok::comma) break;
2576 Lex.Lex(); // eat ','.
2577
Craig Topper998a39a2013-08-20 04:22:09 +00002578 if (Lex.getCode() != tgtok::Id)
2579 return TokError("expected identifier");
2580
David Greenef00919a2009-04-22 22:17:51 +00002581 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002582
2583 // A defm can inherit from regular classes (non-multiclass) as
2584 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002585 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002586
2587 if (InheritFromClass)
2588 break;
2589
Craig Topper011817a2014-04-09 04:50:04 +00002590 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002591 }
2592
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002593 if (InheritFromClass) {
2594 // Process all the classes to inherit as if they were part of a
2595 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002596 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002597 while (1) {
2598 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002599 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002600
2601 // Get the expanded definition prototypes and teach them about
2602 // the record values the current class to inherit has
2603 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2604 Record *CurRec = NewRecDefs[i];
2605
2606 // Add it.
2607 if (AddSubClass(CurRec, SubClass))
2608 return true;
2609
Sean Silvacb1a75e2013-01-09 04:49:14 +00002610 if (ApplyLetStack(CurRec))
2611 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002612 }
2613
2614 if (Lex.getCode() != tgtok::comma) break;
2615 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002616 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002617 }
2618 }
2619
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002620 if (!CurMultiClass)
2621 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene50c09122011-08-10 18:27:46 +00002622 // See Record::setName(). This resolve step will see any new
2623 // name for the def that might have been created when resolving
2624 // inheritance, values and arguments above.
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002625 NewRecDefs[i]->resolveReferences();
2626
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002627 if (Lex.getCode() != tgtok::semi)
2628 return TokError("expected ';' at end of defm");
2629 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002630
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002631 return false;
2632}
2633
2634/// ParseObject
2635/// Object ::= ClassInst
2636/// Object ::= DefInst
2637/// Object ::= MultiClassInst
2638/// Object ::= DefMInst
2639/// Object ::= LETCommand '{' ObjectList '}'
2640/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002641bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002642 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002643 default:
2644 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002645 case tgtok::Let: return ParseTopLevelLet(MC);
2646 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002647 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002648 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002649 case tgtok::Class: return ParseClass();
2650 case tgtok::MultiClass: return ParseMultiClass();
2651 }
2652}
2653
2654/// ParseObjectList
2655/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002656bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002657 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002658 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002659 return true;
2660 }
2661 return false;
2662}
2663
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002664bool TGParser::ParseFile() {
2665 Lex.Lex(); // Prime the lexer.
2666 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002667
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002668 // If we have unread input at the end of the file, report it.
2669 if (Lex.getCode() == tgtok::Eof)
2670 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002671
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002672 return TokError("Unexpected input at top level");
2673}
2674