blob: a438cb6fc8f2c16bcc09d143a26508ba6b770671 [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
Craig Toppera4ea4b02014-11-30 00:24:32 +0000227 unsigned newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000228
David Greene753ed8f2009-04-22 16:42:54 +0000229 // Add all of the defs in the subclass into the current multiclass.
230 for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(),
231 iend = SMC->DefPrototypes.end();
232 i != iend;
233 ++i) {
234 // Clone the def and add it to the current multiclass
Anton Yartsev3fa65d42014-09-25 19:55:58 +0000235 auto NewDef = make_unique<Record>(**i);
David Greene753ed8f2009-04-22 16:42:54 +0000236
237 // Add all of the values in the superclass into the current def.
238 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
Anton Yartsev3fa65d42014-09-25 19:55:58 +0000239 if (AddValue(NewDef.get(), SubMultiClass.RefRange.Start, MCVals[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000240 return true;
241
Craig Topperc3504c42014-12-11 05:25:33 +0000242 CurMC->DefPrototypes.push_back(std::move(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.
Craig Topperc3504c42014-12-11 05:25:33 +0000272 for (const auto &Def :
273 makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
274 if (SetValue(Def.get(), SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson3d948162009-04-28 19:41:44 +0000275 std::vector<unsigned>(),
David Greene753ed8f2009-04-22 16:42:54 +0000276 SubMultiClass.TemplateArgs[i]))
277 return true;
278
279 // Resolve it next.
280 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
281
282 // Now remove it
283 Def->removeValue(SMCTArgs[i]);
284 }
285 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000286 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000287 "Value not specified for template argument #"
David Greenedb10e692011-10-19 13:02:42 +0000288 + utostr(i) + " (" + SMCTArgs[i]->getAsUnquotedString()
289 + ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000290 }
291 }
292
293 return false;
294}
295
David Greenefb927af2012-02-22 16:09:41 +0000296/// ProcessForeachDefs - Given a record, apply all of the variable
297/// values in all surrounding foreach loops, creating new records for
298/// each combination of values.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000299bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
300 if (Loops.empty())
301 return false;
302
David Greenefb927af2012-02-22 16:09:41 +0000303 // We want to instantiate a new copy of CurRec for each combination
304 // of nested loop iterator values. We don't want top instantiate
305 // any copies until we have values for each loop iterator.
306 IterSet IterVals;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000307 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000308}
309
310/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
311/// apply each of the variable values in this loop and then process
312/// subloops.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000313bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
314 // Recursively build a tuple of iterator values.
315 if (IterVals.size() != Loops.size()) {
316 assert(IterVals.size() < Loops.size());
317 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silvafb509ed2012-10-10 20:24:43 +0000318 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Craig Topper011817a2014-04-09 04:50:04 +0000319 if (!List) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000320 Error(Loc, "Loop list is not a list");
321 return true;
322 }
David Greenefb927af2012-02-22 16:09:41 +0000323
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000324 // Process each value.
325 for (int64_t i = 0; i < List->getSize(); ++i) {
Craig Topper011817a2014-04-09 04:50:04 +0000326 Init *ItemVal = List->resolveListElementReference(*CurRec, nullptr, i);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000327 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
328 if (ProcessForeachDefs(CurRec, Loc, IterVals))
329 return true;
330 IterVals.pop_back();
331 }
332 return false;
333 }
334
335 // This is the bottom of the recursion. We have all of the iterator values
336 // for this point in the iteration space. Instantiate a new record to
337 // reflect this combination of values.
Craig Topper84138712014-11-29 05:31:10 +0000338 auto IterRec = make_unique<Record>(*CurRec);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000339
340 // Set the iterator values now.
341 for (unsigned i = 0, e = IterVals.size(); i != e; ++i) {
342 VarInit *IterVar = IterVals[i].IterVar;
Sean Silvafb509ed2012-10-10 20:24:43 +0000343 TypedInit *IVal = dyn_cast<TypedInit>(IterVals[i].IterValue);
Craig Topper84138712014-11-29 05:31:10 +0000344 if (!IVal)
345 return Error(Loc, "foreach iterator value is untyped");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000346
347 IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false));
348
Craig Topper84138712014-11-29 05:31:10 +0000349 if (SetValue(IterRec.get(), Loc, IterVar->getName(),
350 std::vector<unsigned>(), IVal))
351 return Error(Loc, "when instantiating this def");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000352
353 // Resolve it next.
354 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName()));
355
356 // Remove it.
357 IterRec->removeValue(IterVar->getName());
358 }
359
360 if (Records.getDef(IterRec->getNameInitAsString())) {
Artyom Skrobov8b985322014-06-10 12:41:14 +0000361 // If this record is anonymous, it's no problem, just generate a new name
Craig Topper84138712014-11-29 05:31:10 +0000362 if (!IterRec->isAnonymous())
363 return Error(Loc, "def already exists: " +IterRec->getNameInitAsString());
364
365 IterRec->setName(GetNewAnonymousName());
David Greenefb927af2012-02-22 16:09:41 +0000366 }
367
Craig Topper84138712014-11-29 05:31:10 +0000368 Record *IterRecSave = IterRec.get(); // Keep a copy before release.
Craig Toppercdab2322014-11-29 05:52:51 +0000369 Records.addDef(std::move(IterRec));
Craig Topper84138712014-11-29 05:31:10 +0000370 IterRecSave->resolveReferences();
David Greenefb927af2012-02-22 16:09:41 +0000371 return false;
372}
373
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000374//===----------------------------------------------------------------------===//
375// Parser Code
376//===----------------------------------------------------------------------===//
377
378/// isObjectStart - Return true if this is a valid first token for an Object.
379static bool isObjectStart(tgtok::TokKind K) {
380 return K == tgtok::Class || K == tgtok::Def ||
David Greenefb927af2012-02-22 16:09:41 +0000381 K == tgtok::Defm || K == tgtok::Let ||
382 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000383}
384
Alp Tokerce91fe52013-12-21 18:51:00 +0000385/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
386/// an identifier.
387std::string TGParser::GetNewAnonymousName() {
Michael J. Spencer2bb7db92013-02-26 21:29:47 +0000388 unsigned Tmp = AnonCounter++; // MSVC2012 ICEs without this.
Alp Tokerce91fe52013-12-21 18:51:00 +0000389 return "anonymous_" + utostr(Tmp);
Chris Lattner7538ed82010-10-05 22:51:56 +0000390}
391
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000392/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000393/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000394/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000395/// ObjectName ::= /*empty*/
396///
David Greene2affd672011-10-19 13:04:29 +0000397Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
398 switch (Lex.getCode()) {
399 case tgtok::colon:
400 case tgtok::semi:
401 case tgtok::l_brace:
402 // These are all of the tokens that can begin an object body.
403 // Some of these can also begin values but we disallow those cases
404 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000405 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000406 default:
407 break;
408 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000409
Craig Topper011817a2014-04-09 04:50:04 +0000410 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000411 if (CurMultiClass)
412 CurRec = &CurMultiClass->Rec;
413
Craig Topper011817a2014-04-09 04:50:04 +0000414 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000415 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000416 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000417 if (!CurRecName) {
418 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000419 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000420 }
421 Type = CurRecName->getType();
422 }
423
424 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000425}
426
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000427/// ParseClassID - Parse and resolve a reference to a class name. This returns
428/// null on error.
429///
430/// ClassID ::= ID
431///
432Record *TGParser::ParseClassID() {
433 if (Lex.getCode() != tgtok::Id) {
434 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000435 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000436 }
Bob Wilson7248f862009-11-22 04:24:42 +0000437
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000438 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000439 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000440 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000441
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000442 Lex.Lex();
443 return Result;
444}
445
Bob Wilson3d948162009-04-28 19:41:44 +0000446/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
447/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000448///
449/// MultiClassID ::= ID
450///
451MultiClass *TGParser::ParseMultiClassID() {
452 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000453 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000454 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000455 }
Bob Wilson3d948162009-04-28 19:41:44 +0000456
Craig Topper7adf2bf2014-12-11 05:25:30 +0000457 MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
Craig Topper4ca40012014-11-30 01:20:17 +0000458 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000459 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000460
David Greene753ed8f2009-04-22 16:42:54 +0000461 Lex.Lex();
Craig Topper4ca40012014-11-30 01:20:17 +0000462 return Result;
David Greene753ed8f2009-04-22 16:42:54 +0000463}
464
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000465/// ParseSubClassReference - Parse a reference to a subclass or to a templated
466/// subclass. This returns a SubClassRefTy with a null Record* on error.
467///
468/// SubClassRef ::= ClassID
469/// SubClassRef ::= ClassID '<' ValueList '>'
470///
471SubClassReference TGParser::
472ParseSubClassReference(Record *CurRec, bool isDefm) {
473 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000474 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000475
Sean Silva0657b402013-01-09 02:17:14 +0000476 if (isDefm) {
477 if (MultiClass *MC = ParseMultiClassID())
478 Result.Rec = &MC->Rec;
479 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000480 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000481 }
Craig Topper011817a2014-04-09 04:50:04 +0000482 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000483
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000484 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000485 if (Lex.getCode() != tgtok::less) {
486 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000487 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000488 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000489 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000490
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000491 if (Lex.getCode() == tgtok::greater) {
492 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000493 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000494 return Result;
495 }
Bob Wilson7248f862009-11-22 04:24:42 +0000496
David Greene8618f952009-06-08 20:23:18 +0000497 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000498 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000499 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000500 return Result;
501 }
Bob Wilson7248f862009-11-22 04:24:42 +0000502
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000503 if (Lex.getCode() != tgtok::greater) {
504 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000505 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000506 return Result;
507 }
508 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000509 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000510
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000511 return Result;
512}
513
Bob Wilson3d948162009-04-28 19:41:44 +0000514/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
515/// templated submulticlass. This returns a SubMultiClassRefTy with a null
516/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000517///
518/// SubMultiClassRef ::= MultiClassID
519/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
520///
521SubMultiClassReference TGParser::
522ParseSubMultiClassReference(MultiClass *CurMC) {
523 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000524 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000525
David Greene753ed8f2009-04-22 16:42:54 +0000526 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000527 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000528
David Greene753ed8f2009-04-22 16:42:54 +0000529 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000530 if (Lex.getCode() != tgtok::less) {
531 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000532 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000533 }
David Greene753ed8f2009-04-22 16:42:54 +0000534 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000535
David Greene753ed8f2009-04-22 16:42:54 +0000536 if (Lex.getCode() == tgtok::greater) {
537 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000538 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000539 return Result;
540 }
Bob Wilson3d948162009-04-28 19:41:44 +0000541
David Greene8618f952009-06-08 20:23:18 +0000542 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000543 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000544 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000545 return Result;
546 }
Bob Wilson3d948162009-04-28 19:41:44 +0000547
David Greene753ed8f2009-04-22 16:42:54 +0000548 if (Lex.getCode() != tgtok::greater) {
549 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000550 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000551 return Result;
552 }
553 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000554 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000555
556 return Result;
557}
558
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000559/// ParseRangePiece - Parse a bit/value range.
560/// RangePiece ::= INTVAL
561/// RangePiece ::= INTVAL '-' INTVAL
562/// RangePiece ::= INTVAL INTVAL
563bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000564 if (Lex.getCode() != tgtok::IntVal) {
565 TokError("expected integer or bitrange");
566 return true;
567 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000568 int64_t Start = Lex.getCurIntVal();
569 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000570
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000571 if (Start < 0)
572 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000573
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000574 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000575 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000576 Ranges.push_back(Start);
577 return false;
578 case tgtok::minus:
579 if (Lex.Lex() != tgtok::IntVal) {
580 TokError("expected integer value as end of range");
581 return true;
582 }
583 End = Lex.getCurIntVal();
584 break;
585 case tgtok::IntVal:
586 End = -Lex.getCurIntVal();
587 break;
588 }
Bob Wilson7248f862009-11-22 04:24:42 +0000589 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000590 return TokError("invalid range, cannot be negative");
591 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000592
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000593 // Add to the range.
594 if (Start < End) {
595 for (; Start <= End; ++Start)
596 Ranges.push_back(Start);
597 } else {
598 for (; Start >= End; --Start)
599 Ranges.push_back(Start);
600 }
601 return false;
602}
603
604/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
605///
606/// RangeList ::= RangePiece (',' RangePiece)*
607///
608std::vector<unsigned> TGParser::ParseRangeList() {
609 std::vector<unsigned> Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000610
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000611 // Parse the first piece.
612 if (ParseRangePiece(Result))
613 return std::vector<unsigned>();
614 while (Lex.getCode() == tgtok::comma) {
615 Lex.Lex(); // Eat the comma.
616
617 // Parse the next range piece.
618 if (ParseRangePiece(Result))
619 return std::vector<unsigned>();
620 }
621 return Result;
622}
623
624/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
625/// OptionalRangeList ::= '<' RangeList '>'
626/// OptionalRangeList ::= /*empty*/
627bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
628 if (Lex.getCode() != tgtok::less)
629 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000630
Chris Lattner526c8cb2009-06-21 03:39:35 +0000631 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000632 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000633
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000634 // Parse the range list.
635 Ranges = ParseRangeList();
636 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000637
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000638 if (Lex.getCode() != tgtok::greater) {
639 TokError("expected '>' at end of range list");
640 return Error(StartLoc, "to match this '<'");
641 }
642 Lex.Lex(); // eat the '>'.
643 return false;
644}
645
646/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
647/// OptionalBitList ::= '{' RangeList '}'
648/// OptionalBitList ::= /*empty*/
649bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
650 if (Lex.getCode() != tgtok::l_brace)
651 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000652
Chris Lattner526c8cb2009-06-21 03:39:35 +0000653 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000654 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000655
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000656 // Parse the range list.
657 Ranges = ParseRangeList();
658 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000659
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000660 if (Lex.getCode() != tgtok::r_brace) {
661 TokError("expected '}' at end of bit list");
662 return Error(StartLoc, "to match this '{'");
663 }
664 Lex.Lex(); // eat the '}'.
665 return false;
666}
667
668
669/// ParseType - Parse and return a tblgen type. This returns null on error.
670///
671/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000672/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000673/// Type ::= BIT // bit type
674/// Type ::= BITS '<' INTVAL '>' // bits<x> type
675/// Type ::= INT // int type
676/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000677/// Type ::= DAG // dag type
678/// Type ::= ClassID // Record Type
679///
680RecTy *TGParser::ParseType() {
681 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000682 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000683 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000684 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000685 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
686 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000687 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000688 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000689 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000690 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000691 case tgtok::Bits: {
692 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
693 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000694 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000695 }
696 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
697 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000698 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000699 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000700 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000701 if (Lex.Lex() != tgtok::greater) { // Eat count.
702 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000703 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000704 }
705 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000706 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000707 }
708 case tgtok::List: {
709 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
710 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000711 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000712 }
713 Lex.Lex(); // Eat '<'
714 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000715 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000716
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000717 if (Lex.getCode() != tgtok::greater) {
718 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000719 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000720 }
721 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000722 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000723 }
Bob Wilson7248f862009-11-22 04:24:42 +0000724 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000725}
726
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000727/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
728/// has already been read.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000729Init *TGParser::ParseIDValue(Record *CurRec,
David Greened4263a62011-10-19 13:04:20 +0000730 const std::string &Name, SMLoc NameLoc,
731 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000732 if (CurRec) {
733 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000734 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000735
David Greenedb10e692011-10-19 13:02:42 +0000736 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
737
David Greene47a665e2011-10-05 22:42:54 +0000738 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +0000739 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
740 "::");
David Greene47a665e2011-10-05 22:42:54 +0000741
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000742 if (CurRec->isTemplateArg(TemplateArgName)) {
743 const RecordVal *RV = CurRec->getValue(TemplateArgName);
744 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000745 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000746 }
747 }
Bob Wilson7248f862009-11-22 04:24:42 +0000748
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000749 if (CurMultiClass) {
David Greenedb10e692011-10-19 13:02:42 +0000750 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
751 "::");
752
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000753 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
754 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
755 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000756 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000757 }
758 }
Bob Wilson7248f862009-11-22 04:24:42 +0000759
David Greenefb927af2012-02-22 16:09:41 +0000760 // If this is in a foreach loop, make sure it's not a loop iterator
761 for (LoopVector::iterator i = Loops.begin(), iend = Loops.end();
762 i != iend;
763 ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000764 VarInit *IterVar = dyn_cast<VarInit>(i->IterVar);
David Greenefb927af2012-02-22 16:09:41 +0000765 if (IterVar && IterVar->getName() == Name)
766 return IterVar;
767 }
768
David Greene232bd602011-10-19 13:04:21 +0000769 if (Mode == ParseNameMode)
770 return StringInit::get(Name);
771
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000772 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000773 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000774
David Greene232bd602011-10-19 13:04:21 +0000775 if (Mode == ParseValueMode) {
776 Error(NameLoc, "Variable not defined: '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000777 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000778 }
779
780 return StringInit::get(Name);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000781}
782
David Greene5d0c0512009-05-14 20:54:48 +0000783/// ParseOperation - Parse an operator. This returns null on error.
784///
785/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
786///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000787Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000788 switch (Lex.getCode()) {
789 default:
790 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000791 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000792 case tgtok::XHead:
793 case tgtok::XTail:
794 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000795 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
796 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000797 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000798
David Greenee8f3b272009-05-14 21:22:49 +0000799 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000800 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000801 case tgtok::XCast:
802 Lex.Lex(); // eat the operation
803 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000804
David Greenee8f3b272009-05-14 21:22:49 +0000805 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000806
Craig Topper011817a2014-04-09 04:50:04 +0000807 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000808 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000809 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000810 }
David Greene5d0c0512009-05-14 20:54:48 +0000811
David Greenee8f3b272009-05-14 21:22:49 +0000812 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000813 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000814 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000815 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000816 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000817 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000818 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000819 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000820 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000821 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000822 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000823 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000824 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000825 break;
David Greenee8f3b272009-05-14 21:22:49 +0000826 }
827 if (Lex.getCode() != tgtok::l_paren) {
828 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000829 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000830 }
831 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000832
David Greeneaf8ee2c2011-07-29 22:43:06 +0000833 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000834 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000835
David Greene2f7cf7f2011-01-07 17:05:37 +0000836 if (Code == UnOpInit::HEAD
837 || Code == UnOpInit::TAIL
838 || Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000839 ListInit *LHSl = dyn_cast<ListInit>(LHS);
840 StringInit *LHSs = dyn_cast<StringInit>(LHS);
841 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000842 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000843 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000844 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000845 }
846 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000847 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
848 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000849 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000850 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000851 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000852 }
853 }
854
David Greene2f7cf7f2011-01-07 17:05:37 +0000855 if (Code == UnOpInit::HEAD
856 || Code == UnOpInit::TAIL) {
Craig Topper011817a2014-04-09 04:50:04 +0000857 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000858 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000859 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000860 }
Bob Wilson7248f862009-11-22 04:24:42 +0000861
David Greened571b3c2009-05-14 22:38:31 +0000862 if (LHSl && LHSl->getSize() == 0) {
863 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000864 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000865 }
866 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000867 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000868 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000869 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000870 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000871 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000872 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000873 if (Code == UnOpInit::HEAD) {
David Greened571b3c2009-05-14 22:38:31 +0000874 Type = Itemt->getType();
Bob Wilson7248f862009-11-22 04:24:42 +0000875 } else {
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000876 Type = ListRecTy::get(Itemt->getType());
David Greened571b3c2009-05-14 22:38:31 +0000877 }
Bob Wilson7248f862009-11-22 04:24:42 +0000878 } else {
David Greened571b3c2009-05-14 22:38:31 +0000879 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000880 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000881 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000882 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000883 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000884 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000885 if (Code == UnOpInit::HEAD) {
David Greened571b3c2009-05-14 22:38:31 +0000886 Type = LType->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +0000887 } else {
David Greened571b3c2009-05-14 22:38:31 +0000888 Type = LType;
889 }
890 }
891 }
892 }
893
David Greenee8f3b272009-05-14 21:22:49 +0000894 if (Lex.getCode() != tgtok::r_paren) {
895 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000896 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000897 }
898 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000899 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000900 }
David Greene5d0c0512009-05-14 20:54:48 +0000901
902 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000903 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000904 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +0000905 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000906 case tgtok::XSRL:
907 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000908 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000909 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000910 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000911 tgtok::TokKind OpTok = Lex.getCode();
912 SMLoc OpLoc = Lex.getLoc();
913 Lex.Lex(); // eat the operation
914
David Greene5d0c0512009-05-14 20:54:48 +0000915 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000916 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000917
Chris Lattner61ea00b2010-10-05 23:58:18 +0000918 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000919 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000920 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000921 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000922 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000923 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
924 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
925 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
926 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000927 case tgtok::XListConcat:
928 Code = BinOpInit::LISTCONCAT;
929 // We don't know the list type until we parse the first argument
930 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000931 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000932 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000933 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000934 break;
David Greene5d0c0512009-05-14 20:54:48 +0000935 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000936
David Greene5d0c0512009-05-14 20:54:48 +0000937 if (Lex.getCode() != tgtok::l_paren) {
938 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000939 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000940 }
941 Lex.Lex(); // eat the '('
942
David Greeneaf8ee2c2011-07-29 22:43:06 +0000943 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000944
Chris Lattner61ea00b2010-10-05 23:58:18 +0000945 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000946 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000947
Chris Lattner61ea00b2010-10-05 23:58:18 +0000948 while (Lex.getCode() == tgtok::comma) {
949 Lex.Lex(); // eat the ','
950
951 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000952 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000953 }
David Greene5d0c0512009-05-14 20:54:48 +0000954
955 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000956 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000957 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000958 }
959 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000960
Daniel Sanders314e80e2014-05-07 10:13:19 +0000961 // If we are doing !listconcat, we should know the type by now
962 if (OpTok == tgtok::XListConcat) {
963 if (VarInit *Arg0 = dyn_cast<VarInit>(InitList[0]))
964 Type = Arg0->getType();
965 else if (ListInit *Arg0 = dyn_cast<ListInit>(InitList[0]))
966 Type = Arg0->getType();
967 else {
968 InitList[0]->dump();
969 Error(OpLoc, "expected a list");
970 return nullptr;
971 }
972 }
973
Chris Lattner61ea00b2010-10-05 23:58:18 +0000974 // We allow multiple operands to associative operators like !strconcat as
975 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000976 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000977 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000978 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000979 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
980 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000981 InitList.back() = RHS;
982 }
983 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000984
Chris Lattner61ea00b2010-10-05 23:58:18 +0000985 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000986 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000987 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000988
Chris Lattner61ea00b2010-10-05 23:58:18 +0000989 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +0000990 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000991 }
992
David Greene3587eed2009-05-14 23:26:46 +0000993 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +0000994 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +0000995 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
996 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000997 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000998
David Greene98ed3c72009-05-14 21:54:42 +0000999 tgtok::TokKind LexCode = Lex.getCode();
1000 Lex.Lex(); // eat the operation
1001 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001002 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001003 case tgtok::XIf:
1004 Code = TernOpInit::IF;
1005 break;
David Greenee917fff2009-05-14 22:23:47 +00001006 case tgtok::XForEach:
1007 Code = TernOpInit::FOREACH;
1008 break;
David Greene98ed3c72009-05-14 21:54:42 +00001009 case tgtok::XSubst:
1010 Code = TernOpInit::SUBST;
1011 break;
1012 }
1013 if (Lex.getCode() != tgtok::l_paren) {
1014 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001015 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001016 }
1017 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001018
David Greeneaf8ee2c2011-07-29 22:43:06 +00001019 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001020 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001021
David Greene98ed3c72009-05-14 21:54:42 +00001022 if (Lex.getCode() != tgtok::comma) {
1023 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001024 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001025 }
1026 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001027
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001028 Init *MHS = ParseValue(CurRec, ItemType);
1029 if (!MHS)
1030 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 *RHS = ParseValue(CurRec, ItemType);
1039 if (!RHS)
1040 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001041
David Greene98ed3c72009-05-14 21:54:42 +00001042 if (Lex.getCode() != tgtok::r_paren) {
1043 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001044 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001045 }
1046 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001047
David Greene98ed3c72009-05-14 21:54:42 +00001048 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001049 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001050 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001051 RecTy *MHSTy = nullptr;
1052 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001053
Sean Silvafb509ed2012-10-10 20:24:43 +00001054 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001055 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001056 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001057 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001058 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001059 MHSTy = BitRecTy::get();
1060
Sean Silvafb509ed2012-10-10 20:24:43 +00001061 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001062 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001063 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001064 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001065 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001066 RHSTy = BitRecTy::get();
1067
1068 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001069 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001070 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001071 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001072 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001073
1074 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001075 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001076 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001077 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001078
1079 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1080 Type = RHSTy;
1081 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1082 Type = MHSTy;
Bob Wilson7248f862009-11-22 04:24:42 +00001083 } else {
David Greene3587eed2009-05-14 23:26:46 +00001084 TokError("inconsistent types for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001085 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001086 }
1087 break;
1088 }
David Greenee917fff2009-05-14 22:23:47 +00001089 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001090 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
Craig Topper011817a2014-04-09 04:50:04 +00001091 if (!MHSt) {
David Greenee917fff2009-05-14 22:23:47 +00001092 TokError("could not get type for !foreach");
Craig Topper011817a2014-04-09 04:50:04 +00001093 return nullptr;
David Greenee917fff2009-05-14 22:23:47 +00001094 }
1095 Type = MHSt->getType();
1096 break;
1097 }
David Greene98ed3c72009-05-14 21:54:42 +00001098 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001099 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001100 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001101 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001102 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001103 }
1104 Type = RHSt->getType();
1105 break;
1106 }
1107 }
David Greenee32ebf22011-07-29 19:07:07 +00001108 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001109 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001110 }
David Greene5d0c0512009-05-14 20:54:48 +00001111 }
David Greene5d0c0512009-05-14 20:54:48 +00001112}
1113
1114/// ParseOperatorType - Parse a type for an operator. This returns
1115/// null on error.
1116///
1117/// OperatorType ::= '<' Type '>'
1118///
Dan Gohman1432ef82009-08-12 22:10:57 +00001119RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001120 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001121
1122 if (Lex.getCode() != tgtok::less) {
1123 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001124 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001125 }
1126 Lex.Lex(); // eat the <
1127
1128 Type = ParseType();
1129
Craig Topper011817a2014-04-09 04:50:04 +00001130 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001131 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001132 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001133 }
1134
1135 if (Lex.getCode() != tgtok::greater) {
1136 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001137 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001138 }
1139 Lex.Lex(); // eat the >
1140
1141 return Type;
1142}
1143
1144
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001145/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1146///
1147/// SimpleValue ::= IDValue
1148/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001149/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001150/// SimpleValue ::= CODEFRAGMENT
1151/// SimpleValue ::= '?'
1152/// SimpleValue ::= '{' ValueList '}'
1153/// SimpleValue ::= ID '<' ValueListNE '>'
1154/// SimpleValue ::= '[' ValueList ']'
1155/// SimpleValue ::= '(' IDValue DagArgList ')'
1156/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001157/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001158/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1159/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1160/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001161/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001162/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1163///
David Greened4263a62011-10-19 13:04:20 +00001164Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1165 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001166 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001167 switch (Lex.getCode()) {
1168 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001169 case tgtok::paste:
1170 // This is a leading paste operation. This is deprecated but
1171 // still exists in some .td files. Ignore it.
1172 Lex.Lex(); // Skip '#'.
1173 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001174 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001175 case tgtok::BinaryIntVal: {
1176 auto BinaryVal = Lex.getCurBinaryIntVal();
1177 SmallVector<Init*, 16> Bits(BinaryVal.second);
1178 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001179 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001180 R = BitsInit::get(Bits);
1181 Lex.Lex();
1182 break;
1183 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001184 case tgtok::StrVal: {
1185 std::string Val = Lex.getCurStrVal();
1186 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001187
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001188 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001189 while (Lex.getCode() == tgtok::StrVal) {
1190 Val += Lex.getCurStrVal();
1191 Lex.Lex();
1192 }
Bob Wilson7248f862009-11-22 04:24:42 +00001193
David Greenee32ebf22011-07-29 19:07:07 +00001194 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001195 break;
1196 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001197 case tgtok::CodeFragment:
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +00001198 R = StringInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001199 Lex.Lex();
1200 break;
1201 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001202 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001203 Lex.Lex();
1204 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001205 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001206 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001207 std::string Name = Lex.getCurStrVal();
1208 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001209 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001210
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001211 // Value ::= ID '<' ValueListNE '>'
1212 if (Lex.Lex() == tgtok::greater) {
1213 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001214 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001215 }
David Greene8618f952009-06-08 20:23:18 +00001216
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001217 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1218 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1219 // body.
1220 Record *Class = Records.getClass(Name);
1221 if (!Class) {
1222 Error(NameLoc, "Expected a class name, got '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001223 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001224 }
David Greene8618f952009-06-08 20:23:18 +00001225
David Greeneaf8ee2c2011-07-29 22:43:06 +00001226 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
Craig Topper011817a2014-04-09 04:50:04 +00001227 if (ValueList.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001228
David Greene8618f952009-06-08 20:23:18 +00001229 if (Lex.getCode() != tgtok::greater) {
1230 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001231 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001232 }
1233 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001234 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001235
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001236 // Create the new record, set it as CurRec temporarily.
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00001237 auto NewRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), NameLoc,
1238 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001239 Record *NewRec = NewRecOwner.get(); // Keep a copy since we may release.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001240 SubClassReference SCRef;
Jordan Rosef12e8a92013-01-10 18:50:11 +00001241 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001242 SCRef.Rec = Class;
1243 SCRef.TemplateArgs = ValueList;
1244 // Add info about the subclass to NewRec.
Craig Topper84138712014-11-29 05:31:10 +00001245 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001246 return nullptr;
Craig Topper84138712014-11-29 05:31:10 +00001247
Hal Finkela8c1f462014-01-02 20:47:09 +00001248 if (!CurMultiClass) {
1249 NewRec->resolveReferences();
Craig Toppercdab2322014-11-29 05:52:51 +00001250 Records.addDef(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001251 } else {
Adam Nemete5a07162014-09-16 17:14:13 +00001252 // This needs to get resolved once the multiclass template arguments are
1253 // known before any use.
1254 NewRec->setResolveFirst(true);
Hal Finkela8c1f462014-01-02 20:47:09 +00001255 // Otherwise, we're inside a multiclass, add it to the multiclass.
Craig Topperc3504c42014-12-11 05:25:33 +00001256 CurMultiClass->DefPrototypes.push_back(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001257
1258 // Copy the template arguments for the multiclass into the def.
1259 const std::vector<Init *> &TArgs =
1260 CurMultiClass->Rec.getTemplateArgs();
1261
1262 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1263 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1264 assert(RV && "Template arg doesn't exist?");
1265 NewRec->addValue(*RV);
1266 }
1267
1268 // We can't return the prototype def here, instead return:
1269 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1270 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1271 assert(MCNameRV && "multiclass record must have a NAME");
1272
1273 return UnOpInit::get(UnOpInit::CAST,
1274 BinOpInit::get(BinOpInit::STRCONCAT,
1275 VarInit::get(MCNameRV->getName(),
1276 MCNameRV->getType()),
1277 NewRec->getNameInit(),
1278 StringRecTy::get()),
1279 Class->getDefInit()->getType());
1280 }
Bob Wilson7248f862009-11-22 04:24:42 +00001281
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001282 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001283 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001284 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001285 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001286 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001287 Lex.Lex(); // eat the '{'
David Greeneaf8ee2c2011-07-29 22:43:06 +00001288 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001289
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001290 if (Lex.getCode() != tgtok::r_brace) {
1291 Vals = ParseValueList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001292 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001293 }
1294 if (Lex.getCode() != tgtok::r_brace) {
1295 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001296 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001297 }
1298 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001299
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001300 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001301
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001302 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1303 // first. We'll first read everything in to a vector, then we can reverse
1304 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001305 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001306 // FIXME: The following two loops would not be duplicated
1307 // if the API was a little more orthogonal.
1308
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001309 // bits<n> values are allowed to initialize n bits.
1310 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1311 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1312 NewBits.push_back(BI->getBit((e - i) - 1));
1313 continue;
1314 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001315 // bits<n> can also come from variable initializers.
1316 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1317 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1318 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1319 NewBits.push_back(VI->getBit((e - i) - 1));
1320 continue;
1321 }
1322 // Fallthrough to try convert this to a bit.
1323 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001324 // All other values must be convertible to just a single bit.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001325 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001326 if (!Bit) {
Chris Lattner52416952007-11-22 21:06:59 +00001327 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1328 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001329 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001330 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001331 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001332 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001333 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001334 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001335 }
1336 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1337 Lex.Lex(); // eat the '['
David Greeneaf8ee2c2011-07-29 22:43:06 +00001338 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001339
Craig Topper011817a2014-04-09 04:50:04 +00001340 RecTy *DeducedEltTy = nullptr;
1341 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001342
Craig Topper011817a2014-04-09 04:50:04 +00001343 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001344 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001345 if (!ListType) {
Alp Tokere69170a2014-06-26 22:52:05 +00001346 std::string s;
1347 raw_string_ostream ss(s);
Reid Klecknerd78273f2013-08-06 22:51:21 +00001348 ss << "Type mismatch for list, expected list type, got "
1349 << ItemType->getAsString();
1350 TokError(ss.str());
Craig Topper011817a2014-04-09 04:50:04 +00001351 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001352 }
1353 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001354 }
David Greene8618f952009-06-08 20:23:18 +00001355
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001356 if (Lex.getCode() != tgtok::r_square) {
Craig Topper011817a2014-04-09 04:50:04 +00001357 Vals = ParseValueList(CurRec, nullptr,
1358 GivenListTy ? GivenListTy->getElementType() : nullptr);
1359 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001360 }
1361 if (Lex.getCode() != tgtok::r_square) {
1362 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001363 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001364 }
1365 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001366
Craig Topper011817a2014-04-09 04:50:04 +00001367 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001368 if (Lex.getCode() == tgtok::less) {
1369 // Optional list element type
1370 Lex.Lex(); // eat the '<'
1371
1372 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001373 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001374 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001375 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001376 }
1377
1378 if (Lex.getCode() != tgtok::greater) {
1379 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001380 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001381 }
1382 Lex.Lex(); // eat the '>'
1383 }
1384
1385 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001386 RecTy *EltTy = nullptr;
David Greeneaf8ee2c2011-07-29 22:43:06 +00001387 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greene8618f952009-06-08 20:23:18 +00001388 i != ie;
1389 ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +00001390 TypedInit *TArg = dyn_cast<TypedInit>(*i);
Craig Topper011817a2014-04-09 04:50:04 +00001391 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001392 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001393 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001394 }
Craig Topper011817a2014-04-09 04:50:04 +00001395 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001396 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001397 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001398 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001399 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001400 }
Bob Wilson7248f862009-11-22 04:24:42 +00001401 } else {
David Greene8618f952009-06-08 20:23:18 +00001402 EltTy = TArg->getType();
1403 }
1404 }
1405
Craig Topper011817a2014-04-09 04:50:04 +00001406 if (GivenEltTy) {
1407 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001408 // Verify consistency
1409 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1410 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001411 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001412 }
1413 }
1414 EltTy = GivenEltTy;
1415 }
1416
Craig Topper011817a2014-04-09 04:50:04 +00001417 if (!EltTy) {
1418 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001419 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001420 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001421 }
1422 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001423 } else {
David Greene8618f952009-06-08 20:23:18 +00001424 // Make sure the deduced type is compatible with the given type
1425 if (GivenListTy) {
1426 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1427 TokError("Element type mismatch for list");
Craig Topper011817a2014-04-09 04:50:04 +00001428 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001429 }
1430 }
1431 DeducedEltTy = EltTy;
1432 }
Bob Wilson7248f862009-11-22 04:24:42 +00001433
David Greenee32ebf22011-07-29 19:07:07 +00001434 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001435 }
1436 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1437 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001438 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001439 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001440 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001441 }
Bob Wilson7248f862009-11-22 04:24:42 +00001442
David Greeneaf8ee2c2011-07-29 22:43:06 +00001443 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001444 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001445
Nate Begemandbe3f772009-03-19 05:21:56 +00001446 // If the operator name is present, parse it.
1447 std::string OperatorName;
1448 if (Lex.getCode() == tgtok::colon) {
1449 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1450 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001451 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001452 }
1453 OperatorName = Lex.getCurStrVal();
1454 Lex.Lex(); // eat the VarName.
1455 }
Bob Wilson7248f862009-11-22 04:24:42 +00001456
David Greeneaf8ee2c2011-07-29 22:43:06 +00001457 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001458 if (Lex.getCode() != tgtok::r_paren) {
1459 DagArgs = ParseDagArgList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001460 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001461 }
Bob Wilson7248f862009-11-22 04:24:42 +00001462
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001463 if (Lex.getCode() != tgtok::r_paren) {
1464 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001465 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001466 }
1467 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001468
David Greenee32ebf22011-07-29 19:07:07 +00001469 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001470 }
Bob Wilson7248f862009-11-22 04:24:42 +00001471
David Greene2f7cf7f2011-01-07 17:05:37 +00001472 case tgtok::XHead:
1473 case tgtok::XTail:
1474 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001475 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001476 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001477 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001478 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +00001479 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001480 case tgtok::XSRL:
1481 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001482 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001483 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001484 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001485 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001486 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001487 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001488 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001489 }
1490 }
Bob Wilson7248f862009-11-22 04:24:42 +00001491
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001492 return R;
1493}
1494
1495/// ParseValue - Parse a tblgen value. This returns null on error.
1496///
1497/// Value ::= SimpleValue ValueSuffix*
1498/// ValueSuffix ::= '{' BitList '}'
1499/// ValueSuffix ::= '[' BitList ']'
1500/// ValueSuffix ::= '.' ID
1501///
David Greened4263a62011-10-19 13:04:20 +00001502Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1503 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001504 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001505
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001506 // Parse the suffixes now if present.
1507 while (1) {
1508 switch (Lex.getCode()) {
1509 default: return Result;
1510 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001511 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001512 // This is the beginning of the object body.
1513 return Result;
1514
Chris Lattner526c8cb2009-06-21 03:39:35 +00001515 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001516 Lex.Lex(); // eat the '{'
1517 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001518 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001519
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001520 // Reverse the bitlist.
1521 std::reverse(Ranges.begin(), Ranges.end());
1522 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001523 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001524 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001525 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001526 }
Bob Wilson7248f862009-11-22 04:24:42 +00001527
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001528 // Eat the '}'.
1529 if (Lex.getCode() != tgtok::r_brace) {
1530 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001531 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001532 }
1533 Lex.Lex();
1534 break;
1535 }
1536 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001537 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001538 Lex.Lex(); // eat the '['
1539 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001540 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001541
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001542 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001543 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001544 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001545 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001546 }
Bob Wilson7248f862009-11-22 04:24:42 +00001547
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001548 // Eat the ']'.
1549 if (Lex.getCode() != tgtok::r_square) {
1550 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001551 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001552 }
1553 Lex.Lex();
1554 break;
1555 }
1556 case tgtok::period:
1557 if (Lex.Lex() != tgtok::Id) { // eat the .
1558 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001559 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001560 }
1561 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001562 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001563 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001564 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001565 }
David Greenee32ebf22011-07-29 19:07:07 +00001566 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001567 Lex.Lex(); // eat field name
1568 break;
David Greene8e85b482011-10-19 13:04:43 +00001569
1570 case tgtok::paste:
1571 SMLoc PasteLoc = Lex.getLoc();
1572
1573 // Create a !strconcat() operation, first casting each operand to
1574 // a string if necessary.
1575
Sean Silvafb509ed2012-10-10 20:24:43 +00001576 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001577 if (!LHS) {
1578 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001579 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001580 }
1581
1582 if (LHS->getType() != StringRecTy::get()) {
1583 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1584 }
1585
Craig Topper011817a2014-04-09 04:50:04 +00001586 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001587
1588 Lex.Lex(); // Eat the '#'.
1589 switch (Lex.getCode()) {
1590 case tgtok::colon:
1591 case tgtok::semi:
1592 case tgtok::l_brace:
1593 // These are all of the tokens that can begin an object body.
1594 // Some of these can also begin values but we disallow those cases
1595 // because they are unlikely to be useful.
1596
1597 // Trailing paste, concat with an empty string.
1598 RHS = StringInit::get("");
1599 break;
1600
1601 default:
1602 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001603 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001604 if (!RHS) {
1605 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001606 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001607 }
1608
1609 if (RHS->getType() != StringRecTy::get()) {
1610 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1611 }
1612
1613 break;
1614 }
1615
1616 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1617 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1618 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001619 }
1620 }
1621}
1622
1623/// ParseDagArgList - Parse the argument list for a dag literal expression.
1624///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001625/// DagArg ::= Value (':' VARNAME)?
1626/// DagArg ::= VARNAME
1627/// DagArgList ::= DagArg
1628/// DagArgList ::= DagArgList ',' DagArg
David Greeneaf8ee2c2011-07-29 22:43:06 +00001629std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001630TGParser::ParseDagArgList(Record *CurRec) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001631 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson7248f862009-11-22 04:24:42 +00001632
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001633 while (1) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001634 // DagArg ::= VARNAME
1635 if (Lex.getCode() == tgtok::VarName) {
1636 // A missing value is treated like '?'.
1637 Result.push_back(std::make_pair(UnsetInit::get(), Lex.getCurStrVal()));
1638 Lex.Lex();
1639 } else {
1640 // DagArg ::= Value (':' VARNAME)?
1641 Init *Val = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001642 if (!Val)
David Greeneaf8ee2c2011-07-29 22:43:06 +00001643 return std::vector<std::pair<llvm::Init*, std::string> >();
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001644
1645 // If the variable name is present, add it.
1646 std::string VarName;
1647 if (Lex.getCode() == tgtok::colon) {
1648 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1649 TokError("expected variable name in dag literal");
1650 return std::vector<std::pair<llvm::Init*, std::string> >();
1651 }
1652 VarName = Lex.getCurStrVal();
1653 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001654 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001655
1656 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001657 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001658 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001659 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001660 }
Bob Wilson7248f862009-11-22 04:24:42 +00001661
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001662 return Result;
1663}
1664
1665
1666/// ParseValueList - Parse a comma separated list of values, returning them as a
1667/// vector. Note that this always expects to be able to parse at least one
1668/// value. It returns an empty list if this is not possible.
1669///
1670/// ValueList ::= Value (',' Value)
1671///
David Greeneaf8ee2c2011-07-29 22:43:06 +00001672std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopher71520a82011-07-11 23:06:52 +00001673 RecTy *EltTy) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001674 std::vector<Init*> Result;
David Greene8618f952009-06-08 20:23:18 +00001675 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001676 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001677 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001678 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001679 if (!TArgs.size()) {
1680 TokError("template argument provided to non-template class");
1681 return std::vector<Init*>();
1682 }
David Greene8618f952009-06-08 20:23:18 +00001683 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001684 if (!RV) {
1685 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1686 << ")\n";
1687 }
David Greene8618f952009-06-08 20:23:18 +00001688 assert(RV && "Template argument record not found??");
1689 ItemType = RV->getType();
1690 ++ArgN;
1691 }
1692 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001693 if (!Result.back()) return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001694
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001695 while (Lex.getCode() == tgtok::comma) {
1696 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001697
Craig Topper011817a2014-04-09 04:50:04 +00001698 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001699 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001700 if (ArgN >= TArgs.size()) {
1701 TokError("too many template arguments");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001702 return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001703 }
David Greene8618f952009-06-08 20:23:18 +00001704 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1705 assert(RV && "Template argument record not found??");
1706 ItemType = RV->getType();
1707 ++ArgN;
1708 }
1709 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001710 if (!Result.back()) return std::vector<Init*>();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001711 }
Bob Wilson7248f862009-11-22 04:24:42 +00001712
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001713 return Result;
1714}
1715
1716
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001717/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1718/// empty string on error. This can happen in a number of different context's,
1719/// including within a def or in the template args for a def (which which case
1720/// CurRec will be non-null) and within the template args for a multiclass (in
1721/// which case CurRec will be null, but CurMultiClass will be set). This can
1722/// also happen within a def that is within a multiclass, which will set both
1723/// CurRec and CurMultiClass.
1724///
1725/// Declaration ::= FIELD? Type ID ('=' Value)?
1726///
David Greenedb10e692011-10-19 13:02:42 +00001727Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001728 bool ParsingTemplateArgs) {
1729 // Read the field prefix if present.
1730 bool HasField = Lex.getCode() == tgtok::Field;
1731 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001732
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001733 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001734 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001735
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001736 if (Lex.getCode() != tgtok::Id) {
1737 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001738 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001739 }
Bob Wilson7248f862009-11-22 04:24:42 +00001740
Chris Lattner526c8cb2009-06-21 03:39:35 +00001741 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001742 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001743 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001744
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001745 if (ParsingTemplateArgs) {
1746 if (CurRec) {
David Greenedb10e692011-10-19 13:02:42 +00001747 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001748 } else {
1749 assert(CurMultiClass);
1750 }
1751 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001752 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1753 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001754 }
Bob Wilson7248f862009-11-22 04:24:42 +00001755
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001756 // Add the value.
1757 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001758 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001759
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001760 // If a value is present, parse it.
1761 if (Lex.getCode() == tgtok::equal) {
1762 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001763 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001764 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001765 if (!Val ||
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001766 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001767 // Return the name, even if an error is thrown. This is so that we can
1768 // continue to make some progress, even without the value having been
1769 // initialized.
1770 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001771 }
Bob Wilson7248f862009-11-22 04:24:42 +00001772
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001773 return DeclName;
1774}
1775
David Greenefb927af2012-02-22 16:09:41 +00001776/// ParseForeachDeclaration - Read a foreach declaration, returning
1777/// the name of the declared object or a NULL Init on error. Return
1778/// the name of the parsed initializer list through ForeachListName.
1779///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001780/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1781/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1782/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001783///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001784VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001785 if (Lex.getCode() != tgtok::Id) {
1786 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001787 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001788 }
1789
1790 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1791 Lex.Lex();
1792
1793 // If a value is present, parse it.
1794 if (Lex.getCode() != tgtok::equal) {
1795 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001796 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001797 }
1798 Lex.Lex(); // Eat the '='
1799
Craig Topper011817a2014-04-09 04:50:04 +00001800 RecTy *IterType = nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001801 std::vector<unsigned> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001802
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001803 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001804 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001805 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001806 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001807 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001808 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001809 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001810 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001811 }
1812 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001813 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001814 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001815 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001816 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001817 }
1818 IterType = ListType->getElementType();
1819 break;
David Greenefb927af2012-02-22 16:09:41 +00001820 }
1821
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001822 case tgtok::IntVal: { // RangePiece.
1823 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001824 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001825 break;
David Greenefb927af2012-02-22 16:09:41 +00001826 }
1827
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001828 case tgtok::l_brace: { // '{' RangeList '}'
1829 Lex.Lex(); // eat the '{'
1830 Ranges = ParseRangeList();
1831 if (Lex.getCode() != tgtok::r_brace) {
1832 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001833 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001834 }
1835 Lex.Lex();
1836 break;
1837 }
1838 }
David Greenefb927af2012-02-22 16:09:41 +00001839
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001840 if (!Ranges.empty()) {
1841 assert(!IterType && "Type already initialized?");
1842 IterType = IntRecTy::get();
1843 std::vector<Init*> Values;
1844 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
1845 Values.push_back(IntInit::get(Ranges[i]));
1846 ForeachListValue = ListInit::get(Values, IterType);
1847 }
1848
1849 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001850 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001851
1852 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001853}
1854
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001855/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1856/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1857/// template args for a def, which may or may not be in a multiclass. If null,
1858/// these are the template args for a multiclass.
1859///
1860/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001861///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001862bool TGParser::ParseTemplateArgList(Record *CurRec) {
1863 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1864 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001865
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001866 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001867
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001868 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001869 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001870 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001871 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001872
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001873 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001874
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001875 while (Lex.getCode() == tgtok::comma) {
1876 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001877
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001878 // Read the following declarations.
1879 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001880 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001881 return true;
1882 TheRecToAddTo->addTemplateArg(TemplArg);
1883 }
Bob Wilson7248f862009-11-22 04:24:42 +00001884
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001885 if (Lex.getCode() != tgtok::greater)
1886 return TokError("expected '>' at end of template argument list");
1887 Lex.Lex(); // eat the '>'.
1888 return false;
1889}
1890
1891
1892/// ParseBodyItem - Parse a single item at within the body of a def or class.
1893///
1894/// BodyItem ::= Declaration ';'
1895/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1896bool TGParser::ParseBodyItem(Record *CurRec) {
1897 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001898 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001899 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001900
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001901 if (Lex.getCode() != tgtok::semi)
1902 return TokError("expected ';' after declaration");
1903 Lex.Lex();
1904 return false;
1905 }
1906
1907 // LET ID OptionalRangeList '=' Value ';'
1908 if (Lex.Lex() != tgtok::Id)
1909 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001910
Chris Lattner526c8cb2009-06-21 03:39:35 +00001911 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001912 std::string FieldName = Lex.getCurStrVal();
1913 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001914
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001915 std::vector<unsigned> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001916 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001917 return true;
1918 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001919
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001920 if (Lex.getCode() != tgtok::equal)
1921 return TokError("expected '=' in let expression");
1922 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001923
David Greene8618f952009-06-08 20:23:18 +00001924 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001925 if (!Field)
David Greene8618f952009-06-08 20:23:18 +00001926 return TokError("Value '" + FieldName + "' unknown!");
1927
1928 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001929
David Greeneaf8ee2c2011-07-29 22:43:06 +00001930 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001931 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001932
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001933 if (Lex.getCode() != tgtok::semi)
1934 return TokError("expected ';' after let expression");
1935 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001936
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001937 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1938}
1939
1940/// ParseBody - Read the body of a class or def. Return true on error, false on
1941/// success.
1942///
1943/// Body ::= ';'
1944/// Body ::= '{' BodyList '}'
1945/// BodyList BodyItem*
1946///
1947bool TGParser::ParseBody(Record *CurRec) {
1948 // If this is a null definition, just eat the semi and return.
1949 if (Lex.getCode() == tgtok::semi) {
1950 Lex.Lex();
1951 return false;
1952 }
Bob Wilson7248f862009-11-22 04:24:42 +00001953
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001954 if (Lex.getCode() != tgtok::l_brace)
1955 return TokError("Expected ';' or '{' to start body");
1956 // Eat the '{'.
1957 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001958
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001959 while (Lex.getCode() != tgtok::r_brace)
1960 if (ParseBodyItem(CurRec))
1961 return true;
1962
1963 // Eat the '}'.
1964 Lex.Lex();
1965 return false;
1966}
1967
Sean Silvacb1a75e2013-01-09 04:49:14 +00001968/// \brief Apply the current let bindings to \a CurRec.
1969/// \returns true on error, false otherwise.
1970bool TGParser::ApplyLetStack(Record *CurRec) {
1971 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1972 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1973 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1974 LetStack[i][j].Bits, LetStack[i][j].Value))
1975 return true;
1976 return false;
1977}
1978
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001979/// ParseObjectBody - Parse the body of a def or class. This consists of an
1980/// optional ClassList followed by a Body. CurRec is the current def or class
1981/// that is being parsed.
1982///
1983/// ObjectBody ::= BaseClassList Body
1984/// BaseClassList ::= /*empty*/
1985/// BaseClassList ::= ':' BaseClassListNE
1986/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1987///
1988bool TGParser::ParseObjectBody(Record *CurRec) {
1989 // If there is a baseclass list, read it.
1990 if (Lex.getCode() == tgtok::colon) {
1991 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001992
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001993 // Read all of the subclasses.
1994 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1995 while (1) {
1996 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00001997 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001998
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001999 // Add it.
2000 if (AddSubClass(CurRec, SubClass))
2001 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002002
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002003 if (Lex.getCode() != tgtok::comma) break;
2004 Lex.Lex(); // eat ','.
2005 SubClass = ParseSubClassReference(CurRec, false);
2006 }
2007 }
2008
Sean Silvacb1a75e2013-01-09 04:49:14 +00002009 if (ApplyLetStack(CurRec))
2010 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002011
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002012 return ParseBody(CurRec);
2013}
2014
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002015/// ParseDef - Parse and return a top level or multiclass def, return the record
2016/// corresponding to it. This returns null on error.
2017///
2018/// DefInst ::= DEF ObjectName ObjectBody
2019///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002020bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00002021 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002022 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00002023 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002024
2025 // Parse ObjectName and make a record for it.
Craig Topper84138712014-11-29 05:31:10 +00002026 std::unique_ptr<Record> CurRecOwner;
Jordan Roseabdd99b2013-01-10 18:50:05 +00002027 Init *Name = ParseObjectName(CurMultiClass);
2028 if (Name)
Craig Topper84138712014-11-29 05:31:10 +00002029 CurRecOwner = make_unique<Record>(Name, DefLoc, Records);
Jordan Roseabdd99b2013-01-10 18:50:05 +00002030 else
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00002031 CurRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), DefLoc,
2032 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00002033 Record *CurRec = CurRecOwner.get(); // Keep a copy since we may release.
Bob Wilson7248f862009-11-22 04:24:42 +00002034
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002035 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002036 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002037
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002038 // Ensure redefinition doesn't happen.
Craig Topper84138712014-11-29 05:31:10 +00002039 if (Records.getDef(CurRec->getNameInitAsString()))
2040 return Error(DefLoc, "def '" + CurRec->getNameInitAsString()+
2041 "' already defined");
Craig Toppercdab2322014-11-29 05:52:51 +00002042 Records.addDef(std::move(CurRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00002043
2044 if (ParseObjectBody(CurRec))
2045 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002046 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002047 // Parse the body before adding this prototype to the DefPrototypes vector.
2048 // That way implicit definitions will be added to the DefPrototypes vector
2049 // before this object, instantiated prior to defs derived from this object,
2050 // and this available for indirect name resolution when defs derived from
2051 // this object are instantiated.
Craig Topper84138712014-11-29 05:31:10 +00002052 if (ParseObjectBody(CurRec))
Hal Finkela8c1f462014-01-02 20:47:09 +00002053 return true;
2054
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002055 // Otherwise, a def inside a multiclass, add it to the multiclass.
2056 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene07e055f2011-10-19 13:03:51 +00002057 if (CurMultiClass->DefPrototypes[i]->getNameInit()
Craig Topper84138712014-11-29 05:31:10 +00002058 == CurRec->getNameInit())
2059 return Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
2060 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002061 CurMultiClass->DefPrototypes.push_back(std::move(CurRecOwner));
Anton Yartsev671dff12014-08-08 00:29:54 +00002062 } else if (ParseObjectBody(CurRec)) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002063 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002064 }
Bob Wilson7248f862009-11-22 04:24:42 +00002065
Craig Topper011817a2014-04-09 04:50:04 +00002066 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002067 // See Record::setName(). This resolve step will see any new name
2068 // for the def that might have been created when resolving
2069 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002070 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002071
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002072 // If ObjectBody has template arguments, it's an error.
2073 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002074
2075 if (CurMultiClass) {
2076 // Copy the template arguments for the multiclass into the def.
David Greenedb10e692011-10-19 13:02:42 +00002077 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002078 CurMultiClass->Rec.getTemplateArgs();
2079
2080 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2081 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
2082 assert(RV && "Template arg doesn't exist?");
2083 CurRec->addValue(*RV);
2084 }
2085 }
2086
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002087 if (ProcessForeachDefs(CurRec, DefLoc)) {
Craig Topper84138712014-11-29 05:31:10 +00002088 return Error(DefLoc, "Could not process loops for def" +
2089 CurRec->getNameInitAsString());
David Greenefb927af2012-02-22 16:09:41 +00002090 }
2091
2092 return false;
2093}
2094
2095/// ParseForeach - Parse a for statement. Return the record corresponding
2096/// to it. This returns true on error.
2097///
2098/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2099/// Foreach ::= FOREACH Declaration IN Object
2100///
2101bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2102 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2103 Lex.Lex(); // Eat the 'for' token.
2104
2105 // Make a temporary object to record items associated with the for
2106 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002107 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002108 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002109 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002110 return TokError("expected declaration in for");
2111
2112 if (Lex.getCode() != tgtok::In)
2113 return TokError("Unknown tok");
2114 Lex.Lex(); // Eat the in
2115
2116 // Create a loop object and remember it.
2117 Loops.push_back(ForeachLoop(IterName, ListValue));
2118
2119 if (Lex.getCode() != tgtok::l_brace) {
2120 // FOREACH Declaration IN Object
2121 if (ParseObject(CurMultiClass))
2122 return true;
2123 }
2124 else {
2125 SMLoc BraceLoc = Lex.getLoc();
2126 // Otherwise, this is a group foreach.
2127 Lex.Lex(); // eat the '{'.
2128
2129 // Parse the object list.
2130 if (ParseObjectList(CurMultiClass))
2131 return true;
2132
2133 if (Lex.getCode() != tgtok::r_brace) {
2134 TokError("expected '}' at end of foreach command");
2135 return Error(BraceLoc, "to match this '{'");
2136 }
2137 Lex.Lex(); // Eat the }
2138 }
2139
2140 // We've processed everything in this loop.
2141 Loops.pop_back();
2142
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002143 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002144}
2145
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002146/// ParseClass - Parse a tblgen class definition.
2147///
2148/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2149///
2150bool TGParser::ParseClass() {
2151 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2152 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002153
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002154 if (Lex.getCode() != tgtok::Id)
2155 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002156
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002157 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2158 if (CurRec) {
2159 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002160 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002161 !CurRec->getSuperClasses().empty() ||
2162 !CurRec->getTemplateArgs().empty())
David Greene8eed9982011-10-19 13:03:58 +00002163 return TokError("Class '" + CurRec->getNameInitAsString()
2164 + "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002165 } else {
2166 // If this is the first reference to this class, create and add it.
Hans Wennborgffbbd532014-11-30 00:31:49 +00002167 auto NewRec =
2168 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
Craig Toppercdab2322014-11-29 05:52:51 +00002169 CurRec = NewRec.get();
2170 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002171 }
2172 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002173
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002174 // If there are template args, parse them.
2175 if (Lex.getCode() == tgtok::less)
2176 if (ParseTemplateArgList(CurRec))
2177 return true;
2178
2179 // Finally, parse the object body.
2180 return ParseObjectBody(CurRec);
2181}
2182
2183/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2184/// of LetRecords.
2185///
2186/// LetList ::= LetItem (',' LetItem)*
2187/// LetItem ::= ID OptionalRangeList '=' Value
2188///
2189std::vector<LetRecord> TGParser::ParseLetList() {
2190 std::vector<LetRecord> Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002191
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002192 while (1) {
2193 if (Lex.getCode() != tgtok::Id) {
2194 TokError("expected identifier in let definition");
2195 return std::vector<LetRecord>();
2196 }
2197 std::string Name = Lex.getCurStrVal();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002198 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002199 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002200
2201 // Check for an optional RangeList.
2202 std::vector<unsigned> Bits;
Bob Wilson7248f862009-11-22 04:24:42 +00002203 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002204 return std::vector<LetRecord>();
2205 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002206
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002207 if (Lex.getCode() != tgtok::equal) {
2208 TokError("expected '=' in let expression");
2209 return std::vector<LetRecord>();
2210 }
2211 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002212
Craig Topper011817a2014-04-09 04:50:04 +00002213 Init *Val = ParseValue(nullptr);
2214 if (!Val) return std::vector<LetRecord>();
Bob Wilson7248f862009-11-22 04:24:42 +00002215
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002216 // Now that we have everything, add the record.
2217 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson7248f862009-11-22 04:24:42 +00002218
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002219 if (Lex.getCode() != tgtok::comma)
2220 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002221 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002222 }
2223}
2224
2225/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002226/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002227///
2228/// Object ::= LET LetList IN '{' ObjectList '}'
2229/// Object ::= LET LetList IN Object
2230///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002231bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002232 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2233 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002234
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002235 // Add this entry to the let stack.
2236 std::vector<LetRecord> LetInfo = ParseLetList();
2237 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002238 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002239
2240 if (Lex.getCode() != tgtok::In)
2241 return TokError("expected 'in' at end of top-level 'let'");
2242 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002243
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002244 // If this is a scalar let, just handle it now
2245 if (Lex.getCode() != tgtok::l_brace) {
2246 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002247 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002248 return true;
2249 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002250 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002251 // Otherwise, this is a group let.
2252 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002253
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002254 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002255 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002256 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002257
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002258 if (Lex.getCode() != tgtok::r_brace) {
2259 TokError("expected '}' at end of top level let command");
2260 return Error(BraceLoc, "to match this '{'");
2261 }
2262 Lex.Lex();
2263 }
Bob Wilson7248f862009-11-22 04:24:42 +00002264
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002265 // Outside this let scope, this let block is not active.
2266 LetStack.pop_back();
2267 return false;
2268}
2269
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002270/// ParseMultiClass - Parse a multiclass definition.
2271///
Bob Wilson3d948162009-04-28 19:41:44 +00002272/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002273/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2274/// MultiClassObject ::= DefInst
2275/// MultiClassObject ::= MultiClassInst
2276/// MultiClassObject ::= DefMInst
2277/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2278/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002279///
2280bool TGParser::ParseMultiClass() {
2281 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2282 Lex.Lex(); // Eat the multiclass token.
2283
2284 if (Lex.getCode() != tgtok::Id)
2285 return TokError("expected identifier after multiclass for name");
2286 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002287
Craig Topper7adf2bf2014-12-11 05:25:30 +00002288 auto Result =
2289 MultiClasses.insert(std::make_pair(Name,
2290 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2291
2292 if (!Result.second)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002293 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002294
Craig Topper7adf2bf2014-12-11 05:25:30 +00002295 CurMultiClass = Result.first->second.get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002296 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002297
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002298 // If there are template args, parse them.
2299 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002300 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002301 return true;
2302
David Greene7049e792009-04-24 16:55:41 +00002303 bool inherits = false;
2304
David Greene753ed8f2009-04-22 16:42:54 +00002305 // If there are submulticlasses, parse them.
2306 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002307 inherits = true;
2308
David Greene753ed8f2009-04-22 16:42:54 +00002309 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002310
David Greene753ed8f2009-04-22 16:42:54 +00002311 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002312 SubMultiClassReference SubMultiClass =
2313 ParseSubMultiClassReference(CurMultiClass);
David Greene753ed8f2009-04-22 16:42:54 +00002314 while (1) {
2315 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002316 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002317
David Greene753ed8f2009-04-22 16:42:54 +00002318 // Add it.
2319 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2320 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002321
David Greene753ed8f2009-04-22 16:42:54 +00002322 if (Lex.getCode() != tgtok::comma) break;
2323 Lex.Lex(); // eat ','.
2324 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2325 }
2326 }
2327
David Greene7049e792009-04-24 16:55:41 +00002328 if (Lex.getCode() != tgtok::l_brace) {
2329 if (!inherits)
2330 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002331 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002332 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002333 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002334 } else {
David Greene7049e792009-04-24 16:55:41 +00002335 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2336 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002337
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002338 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002339 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002340 default:
2341 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
2342 case tgtok::Let:
2343 case tgtok::Def:
2344 case tgtok::Defm:
2345 case tgtok::Foreach:
2346 if (ParseObject(CurMultiClass))
2347 return true;
2348 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002349 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002350 }
David Greene7049e792009-04-24 16:55:41 +00002351 Lex.Lex(); // eat the '}'.
2352 }
Bob Wilson7248f862009-11-22 04:24:42 +00002353
Craig Topper011817a2014-04-09 04:50:04 +00002354 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002355 return false;
2356}
2357
David Greenedb445972011-10-05 22:42:07 +00002358Record *TGParser::
2359InstantiateMulticlassDef(MultiClass &MC,
2360 Record *DefProto,
Hal Finkelf2a0b2b2014-01-02 19:35:33 +00002361 Init *&DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002362 SMRange DefmPrefixRange) {
David Greene5d5d88c2011-10-19 13:04:31 +00002363 // We need to preserve DefProto so it can be reused for later
2364 // instantiations, so create a new Record to inherit from it.
2365
David Greenedb445972011-10-05 22:42:07 +00002366 // Add in the defm name. If the defm prefix is empty, give each
2367 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2368 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2369 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002370
Jordan Roseabdd99b2013-01-10 18:50:05 +00002371 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002372 if (!DefmPrefix) {
David Greene5d5d88c2011-10-19 13:04:31 +00002373 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002374 IsAnonymous = true;
2375 }
David Greene5d5d88c2011-10-19 13:04:31 +00002376
2377 Init *DefName = DefProto->getNameInit();
2378
Sean Silvafb509ed2012-10-10 20:24:43 +00002379 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002380
Craig Topper011817a2014-04-09 04:50:04 +00002381 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002382 // We have a fully expanded string so there are no operators to
2383 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002384 DefName =
2385 BinOpInit::get(BinOpInit::STRCONCAT,
2386 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2387 StringRecTy::get())->Fold(DefProto, &MC),
2388 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2389 }
David Greene5d5d88c2011-10-19 13:04:31 +00002390
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002391 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002392 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002393 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Craig Topper84138712014-11-29 05:31:10 +00002394 auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002395
2396 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002397 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002398 Ref.Rec = DefProto;
Craig Topper84138712014-11-29 05:31:10 +00002399 AddSubClass(CurRec.get(), Ref);
David Greenedb445972011-10-05 22:42:07 +00002400
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002401 // Set the value for NAME. We don't resolve references to it 'til later,
2402 // though, so that uses in nested multiclass names don't get
2403 // confused.
Craig Topper84138712014-11-29 05:31:10 +00002404 if (SetValue(CurRec.get(), Ref.RefRange.Start, "NAME",
2405 std::vector<unsigned>(), DefmPrefix)) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002406 Error(DefmPrefixRange.Start, "Could not resolve "
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002407 + CurRec->getNameInitAsString() + ":NAME to '"
2408 + DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002409 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002410 }
David Greene8bf0d722011-10-19 13:04:35 +00002411
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002412 // If the DefNameString didn't resolve, we probably have a reference to
2413 // NAME and need to replace it. We need to do at least this much greedily,
2414 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002415 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002416 RecordVal *DefNameRV = CurRec->getValue("NAME");
2417 CurRec->resolveReferencesTo(DefNameRV);
2418 }
2419
2420 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002421 // Now that we're at the top level, resolve all NAME references
2422 // in the resultant defs that weren't in the def names themselves.
2423 RecordVal *DefNameRV = CurRec->getValue("NAME");
2424 CurRec->resolveReferencesTo(DefNameRV);
2425
2426 // Now that NAME references are resolved and we're at the top level of
2427 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002428 // currently in a multiclass, it means this defm appears inside a
2429 // multiclass and its name won't be fully resolvable until we see
2430 // the top-level defm. Therefore, we don't add this to the
2431 // RecordKeeper at this point. If we did we could get duplicate
2432 // defs as more than one probably refers to NAME or some other
2433 // common internal placeholder.
2434
2435 // Ensure redefinition doesn't happen.
2436 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002437 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002438 "' already defined, instantiating defm with subdef '" +
2439 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002440 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002441 }
2442
Craig Topper84138712014-11-29 05:31:10 +00002443 Record *CurRecSave = CurRec.get(); // Keep a copy before we release.
Craig Toppercdab2322014-11-29 05:52:51 +00002444 Records.addDef(std::move(CurRec));
Craig Topper84138712014-11-29 05:31:10 +00002445 return CurRecSave;
David Greene8bf0d722011-10-19 13:04:35 +00002446 }
2447
Craig Topper84138712014-11-29 05:31:10 +00002448 // FIXME This is bad but the ownership transfer to caller is pretty messy.
2449 // The unique_ptr in this function at least protects the exits above.
2450 return CurRec.release();
David Greenedb445972011-10-05 22:42:07 +00002451}
2452
2453bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2454 Record *CurRec,
2455 SMLoc DefmPrefixLoc,
2456 SMLoc SubClassLoc,
David Greenedb10e692011-10-19 13:02:42 +00002457 const std::vector<Init *> &TArgs,
David Greenedb445972011-10-05 22:42:07 +00002458 std::vector<Init *> &TemplateVals,
2459 bool DeleteArgs) {
2460 // Loop over all of the template arguments, setting them to the specified
2461 // value or leaving them as the default if necessary.
2462 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2463 // Check if a value is specified for this temp-arg.
2464 if (i < TemplateVals.size()) {
2465 // Set it now.
2466 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2467 TemplateVals[i]))
2468 return true;
2469
2470 // Resolve it next.
2471 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2472
2473 if (DeleteArgs)
2474 // Now remove it.
2475 CurRec->removeValue(TArgs[i]);
2476
2477 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2478 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenedb10e692011-10-19 13:02:42 +00002479 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2480 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2481 + "'");
David Greenedb445972011-10-05 22:42:07 +00002482 }
2483 }
2484 return false;
2485}
2486
2487bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2488 Record *CurRec,
2489 Record *DefProto,
2490 SMLoc DefmPrefixLoc) {
2491 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002492 if (ApplyLetStack(CurRec))
2493 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002494
David Greenedb445972011-10-05 22:42:07 +00002495 // Don't create a top level definition for defm inside multiclasses,
2496 // instead, only update the prototypes and bind the template args
2497 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002498 if (!CurMultiClass)
2499 return false;
2500 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2501 i != e; ++i)
2502 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2503 == CurRec->getNameInit())
2504 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2505 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002506 CurMultiClass->DefPrototypes.push_back(std::unique_ptr<Record>(CurRec));
David Greenedb445972011-10-05 22:42:07 +00002507
Sean Silvacc951b22013-01-09 05:28:12 +00002508 // Copy the template arguments for the multiclass into the new def.
2509 const std::vector<Init *> &TA =
2510 CurMultiClass->Rec.getTemplateArgs();
David Greenedb445972011-10-05 22:42:07 +00002511
Sean Silvacc951b22013-01-09 05:28:12 +00002512 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2513 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2514 assert(RV && "Template arg doesn't exist?");
2515 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002516 }
2517
2518 return false;
2519}
2520
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002521/// ParseDefm - Parse the instantiation of a multiclass.
2522///
2523/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2524///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002525bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002526 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002527 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002528 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002529
Craig Topperb21afc62013-01-07 05:09:33 +00002530 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002531 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002532 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002533
Jordan Rosef12e8a92013-01-10 18:50:11 +00002534 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002535 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002536 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002537
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002538 // Keep track of the new generated record definitions.
2539 std::vector<Record*> NewRecDefs;
2540
2541 // This record also inherits from a regular class (non-multiclass)?
2542 bool InheritFromClass = false;
2543
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002544 // eat the colon.
2545 Lex.Lex();
2546
Chris Lattner526c8cb2009-06-21 03:39:35 +00002547 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002548 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002549
2550 while (1) {
Craig Topper011817a2014-04-09 04:50:04 +00002551 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002552
2553 // To instantiate a multiclass, we need to first get the multiclass, then
2554 // instantiate each def contained in the multiclass with the SubClassRef
2555 // template parameters.
Craig Topper7adf2bf2014-12-11 05:25:30 +00002556 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper4ca40012014-11-30 01:20:17 +00002557 assert(MC && "Didn't lookup multiclass correctly?");
David Greeneaf8ee2c2011-07-29 22:43:06 +00002558 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002559
2560 // Verify that the correct number of template arguments were specified.
David Greenedb10e692011-10-19 13:02:42 +00002561 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002562 if (TArgs.size() < TemplateVals.size())
2563 return Error(SubClassLoc,
2564 "more template args specified than multiclass expects");
2565
2566 // Loop over all the def's in the multiclass, instantiating each one.
2567 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
Craig Topperc3504c42014-12-11 05:25:33 +00002568 Record *DefProto = MC->DefPrototypes[i].get();
David Greenef00919a2009-04-22 22:17:51 +00002569
Jordan Rosef12e8a92013-01-10 18:50:11 +00002570 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix,
2571 SMRange(DefmLoc,
2572 DefmPrefixEndLoc));
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002573 if (!CurRec)
2574 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002575
Jordan Rosef12e8a92013-01-10 18:50:11 +00002576 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002577 TArgs, TemplateVals, true/*Delete args*/))
2578 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002579
Jordan Rosef12e8a92013-01-10 18:50:11 +00002580 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002581 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002582
Adam Nemete5a07162014-09-16 17:14:13 +00002583 // Defs that can be used by other definitions should be fully resolved
2584 // before any use.
2585 if (DefProto->isResolveFirst() && !CurMultiClass) {
2586 CurRec->resolveReferences();
2587 CurRec->setResolveFirst(false);
2588 }
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002589 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002590 }
2591
David Greenedb445972011-10-05 22:42:07 +00002592
David Greenef00919a2009-04-22 22:17:51 +00002593 if (Lex.getCode() != tgtok::comma) break;
2594 Lex.Lex(); // eat ','.
2595
Craig Topper998a39a2013-08-20 04:22:09 +00002596 if (Lex.getCode() != tgtok::Id)
2597 return TokError("expected identifier");
2598
David Greenef00919a2009-04-22 22:17:51 +00002599 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002600
2601 // A defm can inherit from regular classes (non-multiclass) as
2602 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002603 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002604
2605 if (InheritFromClass)
2606 break;
2607
Craig Topper011817a2014-04-09 04:50:04 +00002608 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002609 }
2610
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002611 if (InheritFromClass) {
2612 // Process all the classes to inherit as if they were part of a
2613 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002614 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002615 while (1) {
2616 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002617 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002618
2619 // Get the expanded definition prototypes and teach them about
2620 // the record values the current class to inherit has
2621 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2622 Record *CurRec = NewRecDefs[i];
2623
2624 // Add it.
2625 if (AddSubClass(CurRec, SubClass))
2626 return true;
2627
Sean Silvacb1a75e2013-01-09 04:49:14 +00002628 if (ApplyLetStack(CurRec))
2629 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002630 }
2631
2632 if (Lex.getCode() != tgtok::comma) break;
2633 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002634 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002635 }
2636 }
2637
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002638 if (!CurMultiClass)
2639 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene50c09122011-08-10 18:27:46 +00002640 // See Record::setName(). This resolve step will see any new
2641 // name for the def that might have been created when resolving
2642 // inheritance, values and arguments above.
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002643 NewRecDefs[i]->resolveReferences();
2644
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002645 if (Lex.getCode() != tgtok::semi)
2646 return TokError("expected ';' at end of defm");
2647 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002648
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002649 return false;
2650}
2651
2652/// ParseObject
2653/// Object ::= ClassInst
2654/// Object ::= DefInst
2655/// Object ::= MultiClassInst
2656/// Object ::= DefMInst
2657/// Object ::= LETCommand '{' ObjectList '}'
2658/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002659bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002660 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002661 default:
2662 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002663 case tgtok::Let: return ParseTopLevelLet(MC);
2664 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002665 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002666 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002667 case tgtok::Class: return ParseClass();
2668 case tgtok::MultiClass: return ParseMultiClass();
2669 }
2670}
2671
2672/// ParseObjectList
2673/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002674bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002675 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002676 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002677 return true;
2678 }
2679 return false;
2680}
2681
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002682bool TGParser::ParseFile() {
2683 Lex.Lex(); // Prime the lexer.
2684 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002685
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002686 // If we have unread input at the end of the file, report it.
2687 if (Lex.getCode() == tgtok::Eof)
2688 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002689
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002690 return TokError("Unexpected input at top level");
2691}
2692