blob: f2910019ed96dfb289ebfde95dc9b10f77dfb587 [file] [log] [blame]
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001//===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Parser for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
14#include "TGParser.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallVector.h"
Chris Lattnerf4127dd2007-11-22 20:49:04 +000016#include "llvm/ADT/StringExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/Support/CommandLine.h"
18#include "llvm/TableGen/Record.h"
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000019#include <algorithm>
20#include <sstream>
Chris Lattnerf4127dd2007-11-22 20:49:04 +000021using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24// Support Code for the Semantic Actions.
25//===----------------------------------------------------------------------===//
26
27namespace llvm {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000028struct SubClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000029 SMRange RefRange;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000030 Record *Rec;
David Greeneaf8ee2c2011-07-29 22:43:06 +000031 std::vector<Init*> TemplateArgs;
Craig Topper011817a2014-04-09 04:50:04 +000032 SubClassReference() : Rec(nullptr) {}
David Greene7049e792009-04-24 16:55:41 +000033
Craig Topper011817a2014-04-09 04:50:04 +000034 bool isInvalid() const { return Rec == nullptr; }
Chris Lattnerf4127dd2007-11-22 20:49:04 +000035};
David Greene753ed8f2009-04-22 16:42:54 +000036
37struct SubMultiClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000038 SMRange RefRange;
David Greene753ed8f2009-04-22 16:42:54 +000039 MultiClass *MC;
David Greeneaf8ee2c2011-07-29 22:43:06 +000040 std::vector<Init*> TemplateArgs;
Craig Topper011817a2014-04-09 04:50:04 +000041 SubMultiClassReference() : MC(nullptr) {}
Bob Wilson3d948162009-04-28 19:41:44 +000042
Craig Topper011817a2014-04-09 04:50:04 +000043 bool isInvalid() const { return MC == nullptr; }
David Greene7049e792009-04-24 16:55:41 +000044 void dump() const;
David Greene753ed8f2009-04-22 16:42:54 +000045};
David Greene7049e792009-04-24 16:55:41 +000046
47void SubMultiClassReference::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000048 errs() << "Multiclass:\n";
Bob Wilson7248f862009-11-22 04:24:42 +000049
David Greene7049e792009-04-24 16:55:41 +000050 MC->dump();
Bob Wilson7248f862009-11-22 04:24:42 +000051
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000052 errs() << "Template args:\n";
David Greeneaf8ee2c2011-07-29 22:43:06 +000053 for (std::vector<Init *>::const_iterator i = TemplateArgs.begin(),
David Greene7049e792009-04-24 16:55:41 +000054 iend = TemplateArgs.end();
55 i != iend;
56 ++i) {
57 (*i)->dump();
58 }
59}
60
Chris Lattnerf4127dd2007-11-22 20:49:04 +000061} // end namespace llvm
62
Chris Lattner526c8cb2009-06-21 03:39:35 +000063bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Craig Topper011817a2014-04-09 04:50:04 +000064 if (!CurRec)
Chris Lattnerf4127dd2007-11-22 20:49:04 +000065 CurRec = &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +000066
Jakob Stoklund Olesen9d1c5ee2012-01-13 03:16:35 +000067 if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000068 // The value already exists in the class, treat this as a set.
69 if (ERV->setValue(RV.getValue()))
70 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
71 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson7248f862009-11-22 04:24:42 +000072 "previous definition of type '" +
Chris Lattnerf4127dd2007-11-22 20:49:04 +000073 ERV->getType()->getAsString() + "'");
74 } else {
75 CurRec->addValue(RV);
76 }
77 return false;
78}
79
80/// SetValue -
81/// Return true on error, false on success.
David Greene3ca42122011-10-19 13:02:39 +000082bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
David Greeneaf8ee2c2011-07-29 22:43:06 +000083 const std::vector<unsigned> &BitList, Init *V) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000084 if (!V) return false;
85
Craig Topper011817a2014-04-09 04:50:04 +000086 if (!CurRec) CurRec = &CurMultiClass->Rec;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000087
88 RecordVal *RV = CurRec->getValue(ValName);
Craig Topper011817a2014-04-09 04:50:04 +000089 if (!RV)
David Greene3ca42122011-10-19 13:02:39 +000090 return Error(Loc, "Value '" + ValName->getAsUnquotedString()
91 + "' unknown!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +000092
93 // Do not allow assignments like 'X = X'. This will just cause infinite loops
94 // in the resolution machinery.
95 if (BitList.empty())
Sean Silvafb509ed2012-10-10 20:24:43 +000096 if (VarInit *VI = dyn_cast<VarInit>(V))
David Greene3ca42122011-10-19 13:02:39 +000097 if (VI->getNameInit() == ValName)
Chris Lattnerf4127dd2007-11-22 20:49:04 +000098 return false;
Bob Wilson7248f862009-11-22 04:24:42 +000099
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000100 // If we are assigning to a subset of the bits in the value... then we must be
101 // assigning to a field of BitsRecTy, which must have a BitsInit
102 // initializer.
103 //
104 if (!BitList.empty()) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000105 BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue());
Craig Topper011817a2014-04-09 04:50:04 +0000106 if (!CurVal)
David Greene3ca42122011-10-19 13:02:39 +0000107 return Error(Loc, "Value '" + ValName->getAsUnquotedString()
108 + "' is not a bits type");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000109
110 // Convert the incoming value to a bits type of the appropriate size...
David Greeneaf8ee2c2011-07-29 22:43:06 +0000111 Init *BI = V->convertInitializerTo(BitsRecTy::get(BitList.size()));
Craig Topper011817a2014-04-09 04:50:04 +0000112 if (!BI) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000113 return Error(Loc, "Initializer is not compatible with bit range");
114 }
Bob Wilson7248f862009-11-22 04:24:42 +0000115
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000116 // We should have a BitsInit type now.
Sean Silvafb509ed2012-10-10 20:24:43 +0000117 BitsInit *BInit = dyn_cast<BitsInit>(BI);
Craig Toppere73658d2014-04-28 04:05:08 +0000118 assert(BInit != nullptr);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000119
David Greeneaf8ee2c2011-07-29 22:43:06 +0000120 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000121
122 // Loop over bits, assigning values as appropriate.
123 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
124 unsigned Bit = BitList[i];
David Greeneb3da8122011-07-29 19:07:00 +0000125 if (NewBits[Bit])
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000126 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
David Greene3ca42122011-10-19 13:02:39 +0000127 ValName->getAsUnquotedString() + "' more than once");
David Greeneb3da8122011-07-29 19:07:00 +0000128 NewBits[Bit] = BInit->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000129 }
130
131 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
Craig Topper011817a2014-04-09 04:50:04 +0000132 if (!NewBits[i])
David Greeneb3da8122011-07-29 19:07:00 +0000133 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000134
David Greenee32ebf22011-07-29 19:07:07 +0000135 V = BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000136 }
137
Pete Cooper040c6a62014-07-31 01:43:57 +0000138 if (RV->setValue(V)) {
139 std::string InitType = "";
140 if (BitsInit *BI = dyn_cast<BitsInit>(V)) {
141 InitType = (Twine("' of type bit initializer with length ") +
142 Twine(BI->getNumBits())).str();
143 }
David Greene3ca42122011-10-19 13:02:39 +0000144 return Error(Loc, "Value '" + ValName->getAsUnquotedString() + "' of type '"
145 + RV->getType()->getAsString() +
146 "' is incompatible with initializer '" + V->getAsString()
Pete Cooper040c6a62014-07-31 01:43:57 +0000147 + InitType
David Greene3ca42122011-10-19 13:02:39 +0000148 + "'");
Pete Cooper040c6a62014-07-31 01:43:57 +0000149 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000150 return false;
151}
152
153/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
154/// args as SubClass's template arguments.
Cedric Venetd1e179d2009-02-14 16:06:42 +0000155bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000156 Record *SC = SubClass.Rec;
157 // Add all of the values in the subclass into the current class.
158 const std::vector<RecordVal> &Vals = SC->getValues();
159 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
Jordan Rosef12e8a92013-01-10 18:50:11 +0000160 if (AddValue(CurRec, SubClass.RefRange.Start, Vals[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000161 return true;
162
David Greenedb10e692011-10-19 13:02:42 +0000163 const std::vector<Init *> &TArgs = SC->getTemplateArgs();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000164
165 // Ensure that an appropriate number of template arguments are specified.
166 if (TArgs.size() < SubClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000167 return Error(SubClass.RefRange.Start,
168 "More template args specified than expected");
Bob Wilson7248f862009-11-22 04:24:42 +0000169
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000170 // Loop over all of the template arguments, setting them to the specified
171 // value or leaving them as the default if necessary.
172 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
173 if (i < SubClass.TemplateArgs.size()) {
174 // If a value is specified for this template arg, set it now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000175 if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
176 std::vector<unsigned>(), SubClass.TemplateArgs[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000177 return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000178
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000179 // Resolve it next.
180 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson7248f862009-11-22 04:24:42 +0000181
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000182 // Now remove it.
183 CurRec->removeValue(TArgs[i]);
184
185 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000186 return Error(SubClass.RefRange.Start,
187 "Value not specified for template argument #"
David Greenedb10e692011-10-19 13:02:42 +0000188 + utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
189 + ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000190 }
191 }
192
193 // Since everything went well, we can now set the "superclass" list for the
194 // current record.
195 const std::vector<Record*> &SCs = SC->getSuperClasses();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000196 ArrayRef<SMRange> SCRanges = SC->getSuperClassRanges();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000197 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
198 if (CurRec->isSubClassOf(SCs[i]))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000199 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000200 "Already subclass of '" + SCs[i]->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000201 CurRec->addSuperClass(SCs[i], SCRanges[i]);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000202 }
Bob Wilson7248f862009-11-22 04:24:42 +0000203
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000204 if (CurRec->isSubClassOf(SC))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000205 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000206 "Already subclass of '" + SC->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000207 CurRec->addSuperClass(SC, SubClass.RefRange);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000208 return false;
209}
210
David Greene753ed8f2009-04-22 16:42:54 +0000211/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilsonf71e6562009-04-30 18:26:19 +0000212/// CurMC, resolving its template args as SubMultiClass's
David Greene753ed8f2009-04-22 16:42:54 +0000213/// template arguments.
Bob Wilsonf71e6562009-04-30 18:26:19 +0000214bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson92ab8202009-04-30 17:46:20 +0000215 SubMultiClassReference &SubMultiClass) {
David Greene753ed8f2009-04-22 16:42:54 +0000216 MultiClass *SMC = SubMultiClass.MC;
Bob Wilsonf71e6562009-04-30 18:26:19 +0000217 Record *CurRec = &CurMC->Rec;
David Greene753ed8f2009-04-22 16:42:54 +0000218
Bob Wilsonf71e6562009-04-30 18:26:19 +0000219 const std::vector<RecordVal> &MCVals = CurRec->getValues();
David Greene753ed8f2009-04-22 16:42:54 +0000220
221 // Add all of the values in the subclass into the current class.
222 const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues();
223 for (unsigned i = 0, e = SMCVals.size(); i != e; ++i)
Jordan Rosef12e8a92013-01-10 18:50:11 +0000224 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVals[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000225 return true;
226
Bob Wilsonf71e6562009-04-30 18:26:19 +0000227 int newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000228
David Greene753ed8f2009-04-22 16:42:54 +0000229 // Add all of the defs in the subclass into the current multiclass.
230 for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(),
231 iend = SMC->DefPrototypes.end();
232 i != iend;
233 ++i) {
234 // Clone the def and add it to the current multiclass
235 Record *NewDef = new Record(**i);
236
237 // Add all of the values in the superclass into the current def.
238 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
Anton Yartsev671dff12014-08-08 00:29:54 +0000239 if (AddValue(NewDef, SubMultiClass.RefRange.Start, MCVals[i])) {
240 delete NewDef;
David Greene753ed8f2009-04-22 16:42:54 +0000241 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +0000242 }
David Greene753ed8f2009-04-22 16:42:54 +0000243
Bob Wilsonf71e6562009-04-30 18:26:19 +0000244 CurMC->DefPrototypes.push_back(NewDef);
David Greene753ed8f2009-04-22 16:42:54 +0000245 }
Bob Wilson3d948162009-04-28 19:41:44 +0000246
David Greenedb10e692011-10-19 13:02:42 +0000247 const std::vector<Init *> &SMCTArgs = SMC->Rec.getTemplateArgs();
David Greene753ed8f2009-04-22 16:42:54 +0000248
David Greene7049e792009-04-24 16:55:41 +0000249 // Ensure that an appropriate number of template arguments are
250 // specified.
David Greene753ed8f2009-04-22 16:42:54 +0000251 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000252 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000253 "More template args specified than expected");
Bob Wilson3d948162009-04-28 19:41:44 +0000254
David Greene753ed8f2009-04-22 16:42:54 +0000255 // Loop over all of the template arguments, setting them to the specified
256 // value or leaving them as the default if necessary.
257 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
258 if (i < SubMultiClass.TemplateArgs.size()) {
David Greene7049e792009-04-24 16:55:41 +0000259 // If a value is specified for this template arg, set it in the
260 // superclass now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000261 if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson3d948162009-04-28 19:41:44 +0000262 std::vector<unsigned>(),
David Greene753ed8f2009-04-22 16:42:54 +0000263 SubMultiClass.TemplateArgs[i]))
264 return true;
265
266 // Resolve it next.
267 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson3d948162009-04-28 19:41:44 +0000268
David Greene753ed8f2009-04-22 16:42:54 +0000269 // Now remove it.
270 CurRec->removeValue(SMCTArgs[i]);
271
David Greene7049e792009-04-24 16:55:41 +0000272 // If a value is specified for this template arg, set it in the
273 // new defs now.
274 for (MultiClass::RecordVector::iterator j =
Bob Wilsonf71e6562009-04-30 18:26:19 +0000275 CurMC->DefPrototypes.begin() + newDefStart,
276 jend = CurMC->DefPrototypes.end();
David Greene753ed8f2009-04-22 16:42:54 +0000277 j != jend;
278 ++j) {
279 Record *Def = *j;
280
Jordan Rosef12e8a92013-01-10 18:50:11 +0000281 if (SetValue(Def, SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson3d948162009-04-28 19:41:44 +0000282 std::vector<unsigned>(),
David Greene753ed8f2009-04-22 16:42:54 +0000283 SubMultiClass.TemplateArgs[i]))
284 return true;
285
286 // Resolve it next.
287 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
288
289 // Now remove it
290 Def->removeValue(SMCTArgs[i]);
291 }
292 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000293 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000294 "Value not specified for template argument #"
David Greenedb10e692011-10-19 13:02:42 +0000295 + utostr(i) + " (" + SMCTArgs[i]->getAsUnquotedString()
296 + ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000297 }
298 }
299
300 return false;
301}
302
David Greenefb927af2012-02-22 16:09:41 +0000303/// ProcessForeachDefs - Given a record, apply all of the variable
304/// values in all surrounding foreach loops, creating new records for
305/// each combination of values.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000306bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
307 if (Loops.empty())
308 return false;
309
David Greenefb927af2012-02-22 16:09:41 +0000310 // We want to instantiate a new copy of CurRec for each combination
311 // of nested loop iterator values. We don't want top instantiate
312 // any copies until we have values for each loop iterator.
313 IterSet IterVals;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000314 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000315}
316
317/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
318/// apply each of the variable values in this loop and then process
319/// subloops.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000320bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
321 // Recursively build a tuple of iterator values.
322 if (IterVals.size() != Loops.size()) {
323 assert(IterVals.size() < Loops.size());
324 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silvafb509ed2012-10-10 20:24:43 +0000325 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Craig Topper011817a2014-04-09 04:50:04 +0000326 if (!List) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000327 Error(Loc, "Loop list is not a list");
328 return true;
329 }
David Greenefb927af2012-02-22 16:09:41 +0000330
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000331 // Process each value.
332 for (int64_t i = 0; i < List->getSize(); ++i) {
Craig Topper011817a2014-04-09 04:50:04 +0000333 Init *ItemVal = List->resolveListElementReference(*CurRec, nullptr, i);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000334 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
335 if (ProcessForeachDefs(CurRec, Loc, IterVals))
336 return true;
337 IterVals.pop_back();
338 }
339 return false;
340 }
341
342 // This is the bottom of the recursion. We have all of the iterator values
343 // for this point in the iteration space. Instantiate a new record to
344 // reflect this combination of values.
345 Record *IterRec = new Record(*CurRec);
346
347 // Set the iterator values now.
348 for (unsigned i = 0, e = IterVals.size(); i != e; ++i) {
349 VarInit *IterVar = IterVals[i].IterVar;
Sean Silvafb509ed2012-10-10 20:24:43 +0000350 TypedInit *IVal = dyn_cast<TypedInit>(IterVals[i].IterValue);
Craig Topper011817a2014-04-09 04:50:04 +0000351 if (!IVal) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000352 Error(Loc, "foreach iterator value is untyped");
Anton Yartsev671dff12014-08-08 00:29:54 +0000353 delete IterRec;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000354 return true;
355 }
356
357 IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false));
358
359 if (SetValue(IterRec, Loc, IterVar->getName(),
360 std::vector<unsigned>(), IVal)) {
361 Error(Loc, "when instantiating this def");
Anton Yartsev671dff12014-08-08 00:29:54 +0000362 delete IterRec;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000363 return true;
364 }
365
366 // Resolve it next.
367 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName()));
368
369 // Remove it.
370 IterRec->removeValue(IterVar->getName());
371 }
372
373 if (Records.getDef(IterRec->getNameInitAsString())) {
Artyom Skrobov8b985322014-06-10 12:41:14 +0000374 // If this record is anonymous, it's no problem, just generate a new name
375 if (IterRec->isAnonymous())
376 IterRec->setName(GetNewAnonymousName());
377 else {
378 Error(Loc, "def already exists: " + IterRec->getNameInitAsString());
Anton Yartsev671dff12014-08-08 00:29:54 +0000379 delete IterRec;
Artyom Skrobov8b985322014-06-10 12:41:14 +0000380 return true;
381 }
David Greenefb927af2012-02-22 16:09:41 +0000382 }
383
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000384 Records.addDef(IterRec);
385 IterRec->resolveReferences();
David Greenefb927af2012-02-22 16:09:41 +0000386 return false;
387}
388
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000389//===----------------------------------------------------------------------===//
390// Parser Code
391//===----------------------------------------------------------------------===//
392
393/// isObjectStart - Return true if this is a valid first token for an Object.
394static bool isObjectStart(tgtok::TokKind K) {
395 return K == tgtok::Class || K == tgtok::Def ||
David Greenefb927af2012-02-22 16:09:41 +0000396 K == tgtok::Defm || K == tgtok::Let ||
397 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000398}
399
Alp Tokerce91fe52013-12-21 18:51:00 +0000400/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
401/// an identifier.
402std::string TGParser::GetNewAnonymousName() {
Michael J. Spencer2bb7db92013-02-26 21:29:47 +0000403 unsigned Tmp = AnonCounter++; // MSVC2012 ICEs without this.
Alp Tokerce91fe52013-12-21 18:51:00 +0000404 return "anonymous_" + utostr(Tmp);
Chris Lattner7538ed82010-10-05 22:51:56 +0000405}
406
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000407/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000408/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000409/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000410/// ObjectName ::= /*empty*/
411///
David Greene2affd672011-10-19 13:04:29 +0000412Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
413 switch (Lex.getCode()) {
414 case tgtok::colon:
415 case tgtok::semi:
416 case tgtok::l_brace:
417 // These are all of the tokens that can begin an object body.
418 // Some of these can also begin values but we disallow those cases
419 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000420 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000421 default:
422 break;
423 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000424
Craig Topper011817a2014-04-09 04:50:04 +0000425 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000426 if (CurMultiClass)
427 CurRec = &CurMultiClass->Rec;
428
Craig Topper011817a2014-04-09 04:50:04 +0000429 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000430 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000431 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000432 if (!CurRecName) {
433 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000434 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000435 }
436 Type = CurRecName->getType();
437 }
438
439 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000440}
441
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000442/// ParseClassID - Parse and resolve a reference to a class name. This returns
443/// null on error.
444///
445/// ClassID ::= ID
446///
447Record *TGParser::ParseClassID() {
448 if (Lex.getCode() != tgtok::Id) {
449 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000450 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000451 }
Bob Wilson7248f862009-11-22 04:24:42 +0000452
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000453 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000454 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000455 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000456
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000457 Lex.Lex();
458 return Result;
459}
460
Bob Wilson3d948162009-04-28 19:41:44 +0000461/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
462/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000463///
464/// MultiClassID ::= ID
465///
466MultiClass *TGParser::ParseMultiClassID() {
467 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000468 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000469 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000470 }
Bob Wilson3d948162009-04-28 19:41:44 +0000471
David Greene753ed8f2009-04-22 16:42:54 +0000472 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
Craig Topper011817a2014-04-09 04:50:04 +0000473 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000474 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000475
David Greene753ed8f2009-04-22 16:42:54 +0000476 Lex.Lex();
477 return Result;
478}
479
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000480/// ParseSubClassReference - Parse a reference to a subclass or to a templated
481/// subclass. This returns a SubClassRefTy with a null Record* on error.
482///
483/// SubClassRef ::= ClassID
484/// SubClassRef ::= ClassID '<' ValueList '>'
485///
486SubClassReference TGParser::
487ParseSubClassReference(Record *CurRec, bool isDefm) {
488 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000489 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000490
Sean Silva0657b402013-01-09 02:17:14 +0000491 if (isDefm) {
492 if (MultiClass *MC = ParseMultiClassID())
493 Result.Rec = &MC->Rec;
494 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000495 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000496 }
Craig Topper011817a2014-04-09 04:50:04 +0000497 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000498
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000499 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000500 if (Lex.getCode() != tgtok::less) {
501 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000502 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000503 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000504 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000505
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000506 if (Lex.getCode() == tgtok::greater) {
507 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000508 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000509 return Result;
510 }
Bob Wilson7248f862009-11-22 04:24:42 +0000511
David Greene8618f952009-06-08 20:23:18 +0000512 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000513 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000514 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000515 return Result;
516 }
Bob Wilson7248f862009-11-22 04:24:42 +0000517
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000518 if (Lex.getCode() != tgtok::greater) {
519 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000520 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000521 return Result;
522 }
523 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000524 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000525
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000526 return Result;
527}
528
Bob Wilson3d948162009-04-28 19:41:44 +0000529/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
530/// templated submulticlass. This returns a SubMultiClassRefTy with a null
531/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000532///
533/// SubMultiClassRef ::= MultiClassID
534/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
535///
536SubMultiClassReference TGParser::
537ParseSubMultiClassReference(MultiClass *CurMC) {
538 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000539 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000540
David Greene753ed8f2009-04-22 16:42:54 +0000541 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000542 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000543
David Greene753ed8f2009-04-22 16:42:54 +0000544 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000545 if (Lex.getCode() != tgtok::less) {
546 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000547 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000548 }
David Greene753ed8f2009-04-22 16:42:54 +0000549 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000550
David Greene753ed8f2009-04-22 16:42:54 +0000551 if (Lex.getCode() == tgtok::greater) {
552 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000553 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000554 return Result;
555 }
Bob Wilson3d948162009-04-28 19:41:44 +0000556
David Greene8618f952009-06-08 20:23:18 +0000557 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000558 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000559 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000560 return Result;
561 }
Bob Wilson3d948162009-04-28 19:41:44 +0000562
David Greene753ed8f2009-04-22 16:42:54 +0000563 if (Lex.getCode() != tgtok::greater) {
564 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000565 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000566 return Result;
567 }
568 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000569 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000570
571 return Result;
572}
573
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000574/// ParseRangePiece - Parse a bit/value range.
575/// RangePiece ::= INTVAL
576/// RangePiece ::= INTVAL '-' INTVAL
577/// RangePiece ::= INTVAL INTVAL
578bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000579 if (Lex.getCode() != tgtok::IntVal) {
580 TokError("expected integer or bitrange");
581 return true;
582 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000583 int64_t Start = Lex.getCurIntVal();
584 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000585
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000586 if (Start < 0)
587 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000588
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000589 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000590 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000591 Ranges.push_back(Start);
592 return false;
593 case tgtok::minus:
594 if (Lex.Lex() != tgtok::IntVal) {
595 TokError("expected integer value as end of range");
596 return true;
597 }
598 End = Lex.getCurIntVal();
599 break;
600 case tgtok::IntVal:
601 End = -Lex.getCurIntVal();
602 break;
603 }
Bob Wilson7248f862009-11-22 04:24:42 +0000604 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000605 return TokError("invalid range, cannot be negative");
606 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000607
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000608 // Add to the range.
609 if (Start < End) {
610 for (; Start <= End; ++Start)
611 Ranges.push_back(Start);
612 } else {
613 for (; Start >= End; --Start)
614 Ranges.push_back(Start);
615 }
616 return false;
617}
618
619/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
620///
621/// RangeList ::= RangePiece (',' RangePiece)*
622///
623std::vector<unsigned> TGParser::ParseRangeList() {
624 std::vector<unsigned> Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000625
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000626 // Parse the first piece.
627 if (ParseRangePiece(Result))
628 return std::vector<unsigned>();
629 while (Lex.getCode() == tgtok::comma) {
630 Lex.Lex(); // Eat the comma.
631
632 // Parse the next range piece.
633 if (ParseRangePiece(Result))
634 return std::vector<unsigned>();
635 }
636 return Result;
637}
638
639/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
640/// OptionalRangeList ::= '<' RangeList '>'
641/// OptionalRangeList ::= /*empty*/
642bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
643 if (Lex.getCode() != tgtok::less)
644 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000645
Chris Lattner526c8cb2009-06-21 03:39:35 +0000646 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000647 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000648
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000649 // Parse the range list.
650 Ranges = ParseRangeList();
651 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000652
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000653 if (Lex.getCode() != tgtok::greater) {
654 TokError("expected '>' at end of range list");
655 return Error(StartLoc, "to match this '<'");
656 }
657 Lex.Lex(); // eat the '>'.
658 return false;
659}
660
661/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
662/// OptionalBitList ::= '{' RangeList '}'
663/// OptionalBitList ::= /*empty*/
664bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
665 if (Lex.getCode() != tgtok::l_brace)
666 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000667
Chris Lattner526c8cb2009-06-21 03:39:35 +0000668 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000669 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000670
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000671 // Parse the range list.
672 Ranges = ParseRangeList();
673 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000674
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000675 if (Lex.getCode() != tgtok::r_brace) {
676 TokError("expected '}' at end of bit list");
677 return Error(StartLoc, "to match this '{'");
678 }
679 Lex.Lex(); // eat the '}'.
680 return false;
681}
682
683
684/// ParseType - Parse and return a tblgen type. This returns null on error.
685///
686/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000687/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000688/// Type ::= BIT // bit type
689/// Type ::= BITS '<' INTVAL '>' // bits<x> type
690/// Type ::= INT // int type
691/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000692/// Type ::= DAG // dag type
693/// Type ::= ClassID // Record Type
694///
695RecTy *TGParser::ParseType() {
696 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000697 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000698 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000699 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000700 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
701 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000702 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000703 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000704 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000705 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000706 case tgtok::Bits: {
707 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
708 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000709 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000710 }
711 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
712 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000713 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000714 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000715 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000716 if (Lex.Lex() != tgtok::greater) { // Eat count.
717 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000718 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000719 }
720 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000721 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000722 }
723 case tgtok::List: {
724 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
725 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000726 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000727 }
728 Lex.Lex(); // Eat '<'
729 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000730 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000731
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000732 if (Lex.getCode() != tgtok::greater) {
733 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000734 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000735 }
736 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000737 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000738 }
Bob Wilson7248f862009-11-22 04:24:42 +0000739 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000740}
741
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000742/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
743/// has already been read.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000744Init *TGParser::ParseIDValue(Record *CurRec,
David Greened4263a62011-10-19 13:04:20 +0000745 const std::string &Name, SMLoc NameLoc,
746 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000747 if (CurRec) {
748 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000749 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000750
David Greenedb10e692011-10-19 13:02:42 +0000751 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
752
David Greene47a665e2011-10-05 22:42:54 +0000753 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +0000754 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
755 "::");
David Greene47a665e2011-10-05 22:42:54 +0000756
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000757 if (CurRec->isTemplateArg(TemplateArgName)) {
758 const RecordVal *RV = CurRec->getValue(TemplateArgName);
759 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000760 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000761 }
762 }
Bob Wilson7248f862009-11-22 04:24:42 +0000763
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000764 if (CurMultiClass) {
David Greenedb10e692011-10-19 13:02:42 +0000765 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
766 "::");
767
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000768 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
769 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
770 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000771 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000772 }
773 }
Bob Wilson7248f862009-11-22 04:24:42 +0000774
David Greenefb927af2012-02-22 16:09:41 +0000775 // If this is in a foreach loop, make sure it's not a loop iterator
776 for (LoopVector::iterator i = Loops.begin(), iend = Loops.end();
777 i != iend;
778 ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000779 VarInit *IterVar = dyn_cast<VarInit>(i->IterVar);
David Greenefb927af2012-02-22 16:09:41 +0000780 if (IterVar && IterVar->getName() == Name)
781 return IterVar;
782 }
783
David Greene232bd602011-10-19 13:04:21 +0000784 if (Mode == ParseNameMode)
785 return StringInit::get(Name);
786
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000787 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000788 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000789
David Greene232bd602011-10-19 13:04:21 +0000790 if (Mode == ParseValueMode) {
791 Error(NameLoc, "Variable not defined: '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000792 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000793 }
794
795 return StringInit::get(Name);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000796}
797
David Greene5d0c0512009-05-14 20:54:48 +0000798/// ParseOperation - Parse an operator. This returns null on error.
799///
800/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
801///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000802Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000803 switch (Lex.getCode()) {
804 default:
805 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000806 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000807 case tgtok::XHead:
808 case tgtok::XTail:
809 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000810 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
811 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000812 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000813
David Greenee8f3b272009-05-14 21:22:49 +0000814 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000815 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000816 case tgtok::XCast:
817 Lex.Lex(); // eat the operation
818 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000819
David Greenee8f3b272009-05-14 21:22:49 +0000820 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000821
Craig Topper011817a2014-04-09 04:50:04 +0000822 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000823 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000824 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000825 }
David Greene5d0c0512009-05-14 20:54:48 +0000826
David Greenee8f3b272009-05-14 21:22:49 +0000827 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000828 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000829 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000830 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000831 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000832 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000833 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000834 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000835 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000836 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000837 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000838 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000839 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000840 break;
David Greenee8f3b272009-05-14 21:22:49 +0000841 }
842 if (Lex.getCode() != tgtok::l_paren) {
843 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000844 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000845 }
846 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000847
David Greeneaf8ee2c2011-07-29 22:43:06 +0000848 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000849 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000850
David Greene2f7cf7f2011-01-07 17:05:37 +0000851 if (Code == UnOpInit::HEAD
852 || Code == UnOpInit::TAIL
853 || Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000854 ListInit *LHSl = dyn_cast<ListInit>(LHS);
855 StringInit *LHSs = dyn_cast<StringInit>(LHS);
856 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000857 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000858 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000859 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000860 }
861 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000862 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
863 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000864 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000865 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000866 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000867 }
868 }
869
David Greene2f7cf7f2011-01-07 17:05:37 +0000870 if (Code == UnOpInit::HEAD
871 || Code == UnOpInit::TAIL) {
Craig Topper011817a2014-04-09 04:50:04 +0000872 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000873 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000874 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000875 }
Bob Wilson7248f862009-11-22 04:24:42 +0000876
David Greened571b3c2009-05-14 22:38:31 +0000877 if (LHSl && LHSl->getSize() == 0) {
878 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000879 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000880 }
881 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000882 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000883 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000884 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000885 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000886 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000887 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000888 if (Code == UnOpInit::HEAD) {
David Greened571b3c2009-05-14 22:38:31 +0000889 Type = Itemt->getType();
Bob Wilson7248f862009-11-22 04:24:42 +0000890 } else {
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000891 Type = ListRecTy::get(Itemt->getType());
David Greened571b3c2009-05-14 22:38:31 +0000892 }
Bob Wilson7248f862009-11-22 04:24:42 +0000893 } else {
David Greened571b3c2009-05-14 22:38:31 +0000894 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000895 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000896 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000897 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000898 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000899 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000900 if (Code == UnOpInit::HEAD) {
David Greened571b3c2009-05-14 22:38:31 +0000901 Type = LType->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +0000902 } else {
David Greened571b3c2009-05-14 22:38:31 +0000903 Type = LType;
904 }
905 }
906 }
907 }
908
David Greenee8f3b272009-05-14 21:22:49 +0000909 if (Lex.getCode() != tgtok::r_paren) {
910 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000911 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000912 }
913 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000914 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000915 }
David Greene5d0c0512009-05-14 20:54:48 +0000916
917 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000918 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000919 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +0000920 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000921 case tgtok::XSRL:
922 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000923 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000924 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000925 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000926 tgtok::TokKind OpTok = Lex.getCode();
927 SMLoc OpLoc = Lex.getLoc();
928 Lex.Lex(); // eat the operation
929
David Greene5d0c0512009-05-14 20:54:48 +0000930 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000931 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000932
Chris Lattner61ea00b2010-10-05 23:58:18 +0000933 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000934 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000935 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000936 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000937 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000938 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
939 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
940 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
941 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000942 case tgtok::XListConcat:
943 Code = BinOpInit::LISTCONCAT;
944 // We don't know the list type until we parse the first argument
945 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000946 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000947 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000948 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000949 break;
David Greene5d0c0512009-05-14 20:54:48 +0000950 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000951
David Greene5d0c0512009-05-14 20:54:48 +0000952 if (Lex.getCode() != tgtok::l_paren) {
953 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000954 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000955 }
956 Lex.Lex(); // eat the '('
957
David Greeneaf8ee2c2011-07-29 22:43:06 +0000958 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000959
Chris Lattner61ea00b2010-10-05 23:58:18 +0000960 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000961 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000962
Chris Lattner61ea00b2010-10-05 23:58:18 +0000963 while (Lex.getCode() == tgtok::comma) {
964 Lex.Lex(); // eat the ','
965
966 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000967 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000968 }
David Greene5d0c0512009-05-14 20:54:48 +0000969
970 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000971 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000972 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000973 }
974 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000975
Daniel Sanders314e80e2014-05-07 10:13:19 +0000976 // If we are doing !listconcat, we should know the type by now
977 if (OpTok == tgtok::XListConcat) {
978 if (VarInit *Arg0 = dyn_cast<VarInit>(InitList[0]))
979 Type = Arg0->getType();
980 else if (ListInit *Arg0 = dyn_cast<ListInit>(InitList[0]))
981 Type = Arg0->getType();
982 else {
983 InitList[0]->dump();
984 Error(OpLoc, "expected a list");
985 return nullptr;
986 }
987 }
988
Chris Lattner61ea00b2010-10-05 23:58:18 +0000989 // We allow multiple operands to associative operators like !strconcat as
990 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000991 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000992 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000993 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000994 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
995 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000996 InitList.back() = RHS;
997 }
998 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000999
Chris Lattner61ea00b2010-10-05 23:58:18 +00001000 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +00001001 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +00001002 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +00001003
Chris Lattner61ea00b2010-10-05 23:58:18 +00001004 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +00001005 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001006 }
1007
David Greene3587eed2009-05-14 23:26:46 +00001008 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001009 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001010 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
1011 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +00001012 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001013
David Greene98ed3c72009-05-14 21:54:42 +00001014 tgtok::TokKind LexCode = Lex.getCode();
1015 Lex.Lex(); // eat the operation
1016 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001017 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001018 case tgtok::XIf:
1019 Code = TernOpInit::IF;
1020 break;
David Greenee917fff2009-05-14 22:23:47 +00001021 case tgtok::XForEach:
1022 Code = TernOpInit::FOREACH;
1023 break;
David Greene98ed3c72009-05-14 21:54:42 +00001024 case tgtok::XSubst:
1025 Code = TernOpInit::SUBST;
1026 break;
1027 }
1028 if (Lex.getCode() != tgtok::l_paren) {
1029 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001030 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001031 }
1032 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001033
David Greeneaf8ee2c2011-07-29 22:43:06 +00001034 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001035 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001036
David Greene98ed3c72009-05-14 21:54:42 +00001037 if (Lex.getCode() != tgtok::comma) {
1038 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001039 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001040 }
1041 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001042
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001043 Init *MHS = ParseValue(CurRec, ItemType);
1044 if (!MHS)
1045 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001046
David Greene98ed3c72009-05-14 21:54:42 +00001047 if (Lex.getCode() != tgtok::comma) {
1048 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001049 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001050 }
1051 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001052
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001053 Init *RHS = ParseValue(CurRec, ItemType);
1054 if (!RHS)
1055 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001056
David Greene98ed3c72009-05-14 21:54:42 +00001057 if (Lex.getCode() != tgtok::r_paren) {
1058 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001059 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001060 }
1061 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001062
David Greene98ed3c72009-05-14 21:54:42 +00001063 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001064 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001065 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001066 RecTy *MHSTy = nullptr;
1067 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001068
Sean Silvafb509ed2012-10-10 20:24:43 +00001069 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001070 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001071 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001072 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001073 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001074 MHSTy = BitRecTy::get();
1075
Sean Silvafb509ed2012-10-10 20:24:43 +00001076 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001077 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001078 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001079 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001080 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001081 RHSTy = BitRecTy::get();
1082
1083 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001084 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001085 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001086 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001087 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001088
1089 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001090 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001091 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001092 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001093
1094 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1095 Type = RHSTy;
1096 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1097 Type = MHSTy;
Bob Wilson7248f862009-11-22 04:24:42 +00001098 } else {
David Greene3587eed2009-05-14 23:26:46 +00001099 TokError("inconsistent types for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001100 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001101 }
1102 break;
1103 }
David Greenee917fff2009-05-14 22:23:47 +00001104 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001105 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
Craig Topper011817a2014-04-09 04:50:04 +00001106 if (!MHSt) {
David Greenee917fff2009-05-14 22:23:47 +00001107 TokError("could not get type for !foreach");
Craig Topper011817a2014-04-09 04:50:04 +00001108 return nullptr;
David Greenee917fff2009-05-14 22:23:47 +00001109 }
1110 Type = MHSt->getType();
1111 break;
1112 }
David Greene98ed3c72009-05-14 21:54:42 +00001113 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001114 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001115 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001116 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001117 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001118 }
1119 Type = RHSt->getType();
1120 break;
1121 }
1122 }
David Greenee32ebf22011-07-29 19:07:07 +00001123 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001124 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001125 }
David Greene5d0c0512009-05-14 20:54:48 +00001126 }
David Greene5d0c0512009-05-14 20:54:48 +00001127}
1128
1129/// ParseOperatorType - Parse a type for an operator. This returns
1130/// null on error.
1131///
1132/// OperatorType ::= '<' Type '>'
1133///
Dan Gohman1432ef82009-08-12 22:10:57 +00001134RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001135 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001136
1137 if (Lex.getCode() != tgtok::less) {
1138 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001139 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001140 }
1141 Lex.Lex(); // eat the <
1142
1143 Type = ParseType();
1144
Craig Topper011817a2014-04-09 04:50:04 +00001145 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001146 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001147 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001148 }
1149
1150 if (Lex.getCode() != tgtok::greater) {
1151 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001152 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001153 }
1154 Lex.Lex(); // eat the >
1155
1156 return Type;
1157}
1158
1159
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001160/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1161///
1162/// SimpleValue ::= IDValue
1163/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001164/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001165/// SimpleValue ::= CODEFRAGMENT
1166/// SimpleValue ::= '?'
1167/// SimpleValue ::= '{' ValueList '}'
1168/// SimpleValue ::= ID '<' ValueListNE '>'
1169/// SimpleValue ::= '[' ValueList ']'
1170/// SimpleValue ::= '(' IDValue DagArgList ')'
1171/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001172/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001173/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1174/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1175/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001176/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001177/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1178///
David Greened4263a62011-10-19 13:04:20 +00001179Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1180 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001181 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001182 switch (Lex.getCode()) {
1183 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001184 case tgtok::paste:
1185 // This is a leading paste operation. This is deprecated but
1186 // still exists in some .td files. Ignore it.
1187 Lex.Lex(); // Skip '#'.
1188 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001189 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001190 case tgtok::BinaryIntVal: {
1191 auto BinaryVal = Lex.getCurBinaryIntVal();
1192 SmallVector<Init*, 16> Bits(BinaryVal.second);
1193 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001194 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001195 R = BitsInit::get(Bits);
1196 Lex.Lex();
1197 break;
1198 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001199 case tgtok::StrVal: {
1200 std::string Val = Lex.getCurStrVal();
1201 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001202
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001203 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001204 while (Lex.getCode() == tgtok::StrVal) {
1205 Val += Lex.getCurStrVal();
1206 Lex.Lex();
1207 }
Bob Wilson7248f862009-11-22 04:24:42 +00001208
David Greenee32ebf22011-07-29 19:07:07 +00001209 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001210 break;
1211 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001212 case tgtok::CodeFragment:
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +00001213 R = StringInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001214 Lex.Lex();
1215 break;
1216 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001217 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001218 Lex.Lex();
1219 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001220 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001221 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001222 std::string Name = Lex.getCurStrVal();
1223 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001224 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001225
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001226 // Value ::= ID '<' ValueListNE '>'
1227 if (Lex.Lex() == tgtok::greater) {
1228 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001229 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001230 }
David Greene8618f952009-06-08 20:23:18 +00001231
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001232 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1233 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1234 // body.
1235 Record *Class = Records.getClass(Name);
1236 if (!Class) {
1237 Error(NameLoc, "Expected a class name, got '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001238 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001239 }
David Greene8618f952009-06-08 20:23:18 +00001240
David Greeneaf8ee2c2011-07-29 22:43:06 +00001241 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
Craig Topper011817a2014-04-09 04:50:04 +00001242 if (ValueList.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001243
David Greene8618f952009-06-08 20:23:18 +00001244 if (Lex.getCode() != tgtok::greater) {
1245 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001246 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001247 }
1248 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001249 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001250
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001251 // Create the new record, set it as CurRec temporarily.
Alp Tokerce91fe52013-12-21 18:51:00 +00001252 Record *NewRec = new Record(GetNewAnonymousName(), NameLoc, Records,
Jordan Roseabdd99b2013-01-10 18:50:05 +00001253 /*IsAnonymous=*/true);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001254 SubClassReference SCRef;
Jordan Rosef12e8a92013-01-10 18:50:11 +00001255 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001256 SCRef.Rec = Class;
1257 SCRef.TemplateArgs = ValueList;
1258 // Add info about the subclass to NewRec.
Anton Yartsev671dff12014-08-08 00:29:54 +00001259 if (AddSubClass(NewRec, SCRef)) {
1260 delete NewRec;
Craig Topper011817a2014-04-09 04:50:04 +00001261 return nullptr;
Anton Yartsev671dff12014-08-08 00:29:54 +00001262 }
Hal Finkela8c1f462014-01-02 20:47:09 +00001263 if (!CurMultiClass) {
1264 NewRec->resolveReferences();
1265 Records.addDef(NewRec);
1266 } else {
Adam Nemete5a07162014-09-16 17:14:13 +00001267 // This needs to get resolved once the multiclass template arguments are
1268 // known before any use.
1269 NewRec->setResolveFirst(true);
Hal Finkela8c1f462014-01-02 20:47:09 +00001270 // Otherwise, we're inside a multiclass, add it to the multiclass.
1271 CurMultiClass->DefPrototypes.push_back(NewRec);
1272
1273 // Copy the template arguments for the multiclass into the def.
1274 const std::vector<Init *> &TArgs =
1275 CurMultiClass->Rec.getTemplateArgs();
1276
1277 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1278 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1279 assert(RV && "Template arg doesn't exist?");
1280 NewRec->addValue(*RV);
1281 }
1282
1283 // We can't return the prototype def here, instead return:
1284 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1285 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1286 assert(MCNameRV && "multiclass record must have a NAME");
1287
1288 return UnOpInit::get(UnOpInit::CAST,
1289 BinOpInit::get(BinOpInit::STRCONCAT,
1290 VarInit::get(MCNameRV->getName(),
1291 MCNameRV->getType()),
1292 NewRec->getNameInit(),
1293 StringRecTy::get()),
1294 Class->getDefInit()->getType());
1295 }
Bob Wilson7248f862009-11-22 04:24:42 +00001296
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001297 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001298 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001299 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001300 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001301 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001302 Lex.Lex(); // eat the '{'
David Greeneaf8ee2c2011-07-29 22:43:06 +00001303 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001304
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001305 if (Lex.getCode() != tgtok::r_brace) {
1306 Vals = ParseValueList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001307 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001308 }
1309 if (Lex.getCode() != tgtok::r_brace) {
1310 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001311 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001312 }
1313 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001314
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001315 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001316
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001317 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1318 // first. We'll first read everything in to a vector, then we can reverse
1319 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001320 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001321 // FIXME: The following two loops would not be duplicated
1322 // if the API was a little more orthogonal.
1323
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001324 // bits<n> values are allowed to initialize n bits.
1325 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1326 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1327 NewBits.push_back(BI->getBit((e - i) - 1));
1328 continue;
1329 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001330 // bits<n> can also come from variable initializers.
1331 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1332 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1333 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1334 NewBits.push_back(VI->getBit((e - i) - 1));
1335 continue;
1336 }
1337 // Fallthrough to try convert this to a bit.
1338 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001339 // All other values must be convertible to just a single bit.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001340 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001341 if (!Bit) {
Chris Lattner52416952007-11-22 21:06:59 +00001342 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1343 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001344 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001345 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001346 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001347 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001348 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001349 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001350 }
1351 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1352 Lex.Lex(); // eat the '['
David Greeneaf8ee2c2011-07-29 22:43:06 +00001353 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001354
Craig Topper011817a2014-04-09 04:50:04 +00001355 RecTy *DeducedEltTy = nullptr;
1356 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001357
Craig Topper011817a2014-04-09 04:50:04 +00001358 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001359 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001360 if (!ListType) {
Alp Tokere69170a2014-06-26 22:52:05 +00001361 std::string s;
1362 raw_string_ostream ss(s);
Reid Klecknerd78273f2013-08-06 22:51:21 +00001363 ss << "Type mismatch for list, expected list type, got "
1364 << ItemType->getAsString();
1365 TokError(ss.str());
Craig Topper011817a2014-04-09 04:50:04 +00001366 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001367 }
1368 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001369 }
David Greene8618f952009-06-08 20:23:18 +00001370
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001371 if (Lex.getCode() != tgtok::r_square) {
Craig Topper011817a2014-04-09 04:50:04 +00001372 Vals = ParseValueList(CurRec, nullptr,
1373 GivenListTy ? GivenListTy->getElementType() : nullptr);
1374 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001375 }
1376 if (Lex.getCode() != tgtok::r_square) {
1377 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001378 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001379 }
1380 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001381
Craig Topper011817a2014-04-09 04:50:04 +00001382 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001383 if (Lex.getCode() == tgtok::less) {
1384 // Optional list element type
1385 Lex.Lex(); // eat the '<'
1386
1387 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001388 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001389 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001390 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001391 }
1392
1393 if (Lex.getCode() != tgtok::greater) {
1394 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001395 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001396 }
1397 Lex.Lex(); // eat the '>'
1398 }
1399
1400 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001401 RecTy *EltTy = nullptr;
David Greeneaf8ee2c2011-07-29 22:43:06 +00001402 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greene8618f952009-06-08 20:23:18 +00001403 i != ie;
1404 ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +00001405 TypedInit *TArg = dyn_cast<TypedInit>(*i);
Craig Topper011817a2014-04-09 04:50:04 +00001406 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001407 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001408 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001409 }
Craig Topper011817a2014-04-09 04:50:04 +00001410 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001411 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001412 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001413 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001414 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001415 }
Bob Wilson7248f862009-11-22 04:24:42 +00001416 } else {
David Greene8618f952009-06-08 20:23:18 +00001417 EltTy = TArg->getType();
1418 }
1419 }
1420
Craig Topper011817a2014-04-09 04:50:04 +00001421 if (GivenEltTy) {
1422 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001423 // Verify consistency
1424 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1425 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001426 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001427 }
1428 }
1429 EltTy = GivenEltTy;
1430 }
1431
Craig Topper011817a2014-04-09 04:50:04 +00001432 if (!EltTy) {
1433 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001434 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001435 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001436 }
1437 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001438 } else {
David Greene8618f952009-06-08 20:23:18 +00001439 // Make sure the deduced type is compatible with the given type
1440 if (GivenListTy) {
1441 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1442 TokError("Element type mismatch for list");
Craig Topper011817a2014-04-09 04:50:04 +00001443 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001444 }
1445 }
1446 DeducedEltTy = EltTy;
1447 }
Bob Wilson7248f862009-11-22 04:24:42 +00001448
David Greenee32ebf22011-07-29 19:07:07 +00001449 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001450 }
1451 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1452 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001453 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001454 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001455 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001456 }
Bob Wilson7248f862009-11-22 04:24:42 +00001457
David Greeneaf8ee2c2011-07-29 22:43:06 +00001458 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001459 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001460
Nate Begemandbe3f772009-03-19 05:21:56 +00001461 // If the operator name is present, parse it.
1462 std::string OperatorName;
1463 if (Lex.getCode() == tgtok::colon) {
1464 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1465 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001466 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001467 }
1468 OperatorName = Lex.getCurStrVal();
1469 Lex.Lex(); // eat the VarName.
1470 }
Bob Wilson7248f862009-11-22 04:24:42 +00001471
David Greeneaf8ee2c2011-07-29 22:43:06 +00001472 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001473 if (Lex.getCode() != tgtok::r_paren) {
1474 DagArgs = ParseDagArgList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001475 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001476 }
Bob Wilson7248f862009-11-22 04:24:42 +00001477
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001478 if (Lex.getCode() != tgtok::r_paren) {
1479 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001480 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001481 }
1482 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001483
David Greenee32ebf22011-07-29 19:07:07 +00001484 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001485 }
Bob Wilson7248f862009-11-22 04:24:42 +00001486
David Greene2f7cf7f2011-01-07 17:05:37 +00001487 case tgtok::XHead:
1488 case tgtok::XTail:
1489 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001490 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001491 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001492 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001493 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +00001494 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001495 case tgtok::XSRL:
1496 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001497 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001498 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001499 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001500 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001501 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001502 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001503 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001504 }
1505 }
Bob Wilson7248f862009-11-22 04:24:42 +00001506
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001507 return R;
1508}
1509
1510/// ParseValue - Parse a tblgen value. This returns null on error.
1511///
1512/// Value ::= SimpleValue ValueSuffix*
1513/// ValueSuffix ::= '{' BitList '}'
1514/// ValueSuffix ::= '[' BitList ']'
1515/// ValueSuffix ::= '.' ID
1516///
David Greened4263a62011-10-19 13:04:20 +00001517Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1518 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001519 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001520
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001521 // Parse the suffixes now if present.
1522 while (1) {
1523 switch (Lex.getCode()) {
1524 default: return Result;
1525 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001526 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001527 // This is the beginning of the object body.
1528 return Result;
1529
Chris Lattner526c8cb2009-06-21 03:39:35 +00001530 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001531 Lex.Lex(); // eat the '{'
1532 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001533 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001534
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001535 // Reverse the bitlist.
1536 std::reverse(Ranges.begin(), Ranges.end());
1537 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001538 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001539 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001540 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001541 }
Bob Wilson7248f862009-11-22 04:24:42 +00001542
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001543 // Eat the '}'.
1544 if (Lex.getCode() != tgtok::r_brace) {
1545 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001546 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001547 }
1548 Lex.Lex();
1549 break;
1550 }
1551 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001552 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001553 Lex.Lex(); // eat the '['
1554 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001555 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001556
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001557 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001558 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001559 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001560 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001561 }
Bob Wilson7248f862009-11-22 04:24:42 +00001562
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001563 // Eat the ']'.
1564 if (Lex.getCode() != tgtok::r_square) {
1565 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001566 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001567 }
1568 Lex.Lex();
1569 break;
1570 }
1571 case tgtok::period:
1572 if (Lex.Lex() != tgtok::Id) { // eat the .
1573 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001574 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001575 }
1576 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001577 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001578 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001579 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001580 }
David Greenee32ebf22011-07-29 19:07:07 +00001581 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001582 Lex.Lex(); // eat field name
1583 break;
David Greene8e85b482011-10-19 13:04:43 +00001584
1585 case tgtok::paste:
1586 SMLoc PasteLoc = Lex.getLoc();
1587
1588 // Create a !strconcat() operation, first casting each operand to
1589 // a string if necessary.
1590
Sean Silvafb509ed2012-10-10 20:24:43 +00001591 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001592 if (!LHS) {
1593 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001594 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001595 }
1596
1597 if (LHS->getType() != StringRecTy::get()) {
1598 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1599 }
1600
Craig Topper011817a2014-04-09 04:50:04 +00001601 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001602
1603 Lex.Lex(); // Eat the '#'.
1604 switch (Lex.getCode()) {
1605 case tgtok::colon:
1606 case tgtok::semi:
1607 case tgtok::l_brace:
1608 // These are all of the tokens that can begin an object body.
1609 // Some of these can also begin values but we disallow those cases
1610 // because they are unlikely to be useful.
1611
1612 // Trailing paste, concat with an empty string.
1613 RHS = StringInit::get("");
1614 break;
1615
1616 default:
1617 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001618 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001619 if (!RHS) {
1620 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001621 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001622 }
1623
1624 if (RHS->getType() != StringRecTy::get()) {
1625 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1626 }
1627
1628 break;
1629 }
1630
1631 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1632 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1633 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001634 }
1635 }
1636}
1637
1638/// ParseDagArgList - Parse the argument list for a dag literal expression.
1639///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001640/// DagArg ::= Value (':' VARNAME)?
1641/// DagArg ::= VARNAME
1642/// DagArgList ::= DagArg
1643/// DagArgList ::= DagArgList ',' DagArg
David Greeneaf8ee2c2011-07-29 22:43:06 +00001644std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001645TGParser::ParseDagArgList(Record *CurRec) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001646 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson7248f862009-11-22 04:24:42 +00001647
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001648 while (1) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001649 // DagArg ::= VARNAME
1650 if (Lex.getCode() == tgtok::VarName) {
1651 // A missing value is treated like '?'.
1652 Result.push_back(std::make_pair(UnsetInit::get(), Lex.getCurStrVal()));
1653 Lex.Lex();
1654 } else {
1655 // DagArg ::= Value (':' VARNAME)?
1656 Init *Val = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001657 if (!Val)
David Greeneaf8ee2c2011-07-29 22:43:06 +00001658 return std::vector<std::pair<llvm::Init*, std::string> >();
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001659
1660 // If the variable name is present, add it.
1661 std::string VarName;
1662 if (Lex.getCode() == tgtok::colon) {
1663 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1664 TokError("expected variable name in dag literal");
1665 return std::vector<std::pair<llvm::Init*, std::string> >();
1666 }
1667 VarName = Lex.getCurStrVal();
1668 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001669 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001670
1671 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001672 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001673 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001674 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001675 }
Bob Wilson7248f862009-11-22 04:24:42 +00001676
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001677 return Result;
1678}
1679
1680
1681/// ParseValueList - Parse a comma separated list of values, returning them as a
1682/// vector. Note that this always expects to be able to parse at least one
1683/// value. It returns an empty list if this is not possible.
1684///
1685/// ValueList ::= Value (',' Value)
1686///
David Greeneaf8ee2c2011-07-29 22:43:06 +00001687std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopher71520a82011-07-11 23:06:52 +00001688 RecTy *EltTy) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001689 std::vector<Init*> Result;
David Greene8618f952009-06-08 20:23:18 +00001690 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001691 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001692 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001693 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001694 if (!TArgs.size()) {
1695 TokError("template argument provided to non-template class");
1696 return std::vector<Init*>();
1697 }
David Greene8618f952009-06-08 20:23:18 +00001698 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001699 if (!RV) {
1700 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1701 << ")\n";
1702 }
David Greene8618f952009-06-08 20:23:18 +00001703 assert(RV && "Template argument record not found??");
1704 ItemType = RV->getType();
1705 ++ArgN;
1706 }
1707 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001708 if (!Result.back()) return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001709
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001710 while (Lex.getCode() == tgtok::comma) {
1711 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001712
Craig Topper011817a2014-04-09 04:50:04 +00001713 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001714 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001715 if (ArgN >= TArgs.size()) {
1716 TokError("too many template arguments");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001717 return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001718 }
David Greene8618f952009-06-08 20:23:18 +00001719 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1720 assert(RV && "Template argument record not found??");
1721 ItemType = RV->getType();
1722 ++ArgN;
1723 }
1724 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001725 if (!Result.back()) return std::vector<Init*>();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001726 }
Bob Wilson7248f862009-11-22 04:24:42 +00001727
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001728 return Result;
1729}
1730
1731
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001732/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1733/// empty string on error. This can happen in a number of different context's,
1734/// including within a def or in the template args for a def (which which case
1735/// CurRec will be non-null) and within the template args for a multiclass (in
1736/// which case CurRec will be null, but CurMultiClass will be set). This can
1737/// also happen within a def that is within a multiclass, which will set both
1738/// CurRec and CurMultiClass.
1739///
1740/// Declaration ::= FIELD? Type ID ('=' Value)?
1741///
David Greenedb10e692011-10-19 13:02:42 +00001742Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001743 bool ParsingTemplateArgs) {
1744 // Read the field prefix if present.
1745 bool HasField = Lex.getCode() == tgtok::Field;
1746 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001747
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001748 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001749 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001750
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001751 if (Lex.getCode() != tgtok::Id) {
1752 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001753 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001754 }
Bob Wilson7248f862009-11-22 04:24:42 +00001755
Chris Lattner526c8cb2009-06-21 03:39:35 +00001756 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001757 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001758 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001759
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001760 if (ParsingTemplateArgs) {
1761 if (CurRec) {
David Greenedb10e692011-10-19 13:02:42 +00001762 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001763 } else {
1764 assert(CurMultiClass);
1765 }
1766 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001767 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1768 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001769 }
Bob Wilson7248f862009-11-22 04:24:42 +00001770
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001771 // Add the value.
1772 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001773 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001774
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001775 // If a value is present, parse it.
1776 if (Lex.getCode() == tgtok::equal) {
1777 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001778 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001779 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001780 if (!Val ||
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001781 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001782 // Return the name, even if an error is thrown. This is so that we can
1783 // continue to make some progress, even without the value having been
1784 // initialized.
1785 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001786 }
Bob Wilson7248f862009-11-22 04:24:42 +00001787
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001788 return DeclName;
1789}
1790
David Greenefb927af2012-02-22 16:09:41 +00001791/// ParseForeachDeclaration - Read a foreach declaration, returning
1792/// the name of the declared object or a NULL Init on error. Return
1793/// the name of the parsed initializer list through ForeachListName.
1794///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001795/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1796/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1797/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001798///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001799VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001800 if (Lex.getCode() != tgtok::Id) {
1801 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001802 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001803 }
1804
1805 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1806 Lex.Lex();
1807
1808 // If a value is present, parse it.
1809 if (Lex.getCode() != tgtok::equal) {
1810 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001811 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001812 }
1813 Lex.Lex(); // Eat the '='
1814
Craig Topper011817a2014-04-09 04:50:04 +00001815 RecTy *IterType = nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001816 std::vector<unsigned> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001817
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001818 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001819 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001820 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001821 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001822 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001823 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001824 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001825 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001826 }
1827 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001828 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001829 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001830 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001831 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001832 }
1833 IterType = ListType->getElementType();
1834 break;
David Greenefb927af2012-02-22 16:09:41 +00001835 }
1836
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001837 case tgtok::IntVal: { // RangePiece.
1838 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001839 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001840 break;
David Greenefb927af2012-02-22 16:09:41 +00001841 }
1842
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001843 case tgtok::l_brace: { // '{' RangeList '}'
1844 Lex.Lex(); // eat the '{'
1845 Ranges = ParseRangeList();
1846 if (Lex.getCode() != tgtok::r_brace) {
1847 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001848 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001849 }
1850 Lex.Lex();
1851 break;
1852 }
1853 }
David Greenefb927af2012-02-22 16:09:41 +00001854
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001855 if (!Ranges.empty()) {
1856 assert(!IterType && "Type already initialized?");
1857 IterType = IntRecTy::get();
1858 std::vector<Init*> Values;
1859 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
1860 Values.push_back(IntInit::get(Ranges[i]));
1861 ForeachListValue = ListInit::get(Values, IterType);
1862 }
1863
1864 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001865 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001866
1867 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001868}
1869
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001870/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1871/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1872/// template args for a def, which may or may not be in a multiclass. If null,
1873/// these are the template args for a multiclass.
1874///
1875/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001876///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001877bool TGParser::ParseTemplateArgList(Record *CurRec) {
1878 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1879 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001880
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001881 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001882
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001883 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001884 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001885 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001886 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001887
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001888 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001889
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001890 while (Lex.getCode() == tgtok::comma) {
1891 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001892
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001893 // Read the following declarations.
1894 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001895 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001896 return true;
1897 TheRecToAddTo->addTemplateArg(TemplArg);
1898 }
Bob Wilson7248f862009-11-22 04:24:42 +00001899
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001900 if (Lex.getCode() != tgtok::greater)
1901 return TokError("expected '>' at end of template argument list");
1902 Lex.Lex(); // eat the '>'.
1903 return false;
1904}
1905
1906
1907/// ParseBodyItem - Parse a single item at within the body of a def or class.
1908///
1909/// BodyItem ::= Declaration ';'
1910/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1911bool TGParser::ParseBodyItem(Record *CurRec) {
1912 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001913 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001914 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001915
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001916 if (Lex.getCode() != tgtok::semi)
1917 return TokError("expected ';' after declaration");
1918 Lex.Lex();
1919 return false;
1920 }
1921
1922 // LET ID OptionalRangeList '=' Value ';'
1923 if (Lex.Lex() != tgtok::Id)
1924 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001925
Chris Lattner526c8cb2009-06-21 03:39:35 +00001926 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001927 std::string FieldName = Lex.getCurStrVal();
1928 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001929
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001930 std::vector<unsigned> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001931 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001932 return true;
1933 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001934
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001935 if (Lex.getCode() != tgtok::equal)
1936 return TokError("expected '=' in let expression");
1937 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001938
David Greene8618f952009-06-08 20:23:18 +00001939 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001940 if (!Field)
David Greene8618f952009-06-08 20:23:18 +00001941 return TokError("Value '" + FieldName + "' unknown!");
1942
1943 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001944
David Greeneaf8ee2c2011-07-29 22:43:06 +00001945 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001946 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001947
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001948 if (Lex.getCode() != tgtok::semi)
1949 return TokError("expected ';' after let expression");
1950 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001951
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001952 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1953}
1954
1955/// ParseBody - Read the body of a class or def. Return true on error, false on
1956/// success.
1957///
1958/// Body ::= ';'
1959/// Body ::= '{' BodyList '}'
1960/// BodyList BodyItem*
1961///
1962bool TGParser::ParseBody(Record *CurRec) {
1963 // If this is a null definition, just eat the semi and return.
1964 if (Lex.getCode() == tgtok::semi) {
1965 Lex.Lex();
1966 return false;
1967 }
Bob Wilson7248f862009-11-22 04:24:42 +00001968
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001969 if (Lex.getCode() != tgtok::l_brace)
1970 return TokError("Expected ';' or '{' to start body");
1971 // Eat the '{'.
1972 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001973
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001974 while (Lex.getCode() != tgtok::r_brace)
1975 if (ParseBodyItem(CurRec))
1976 return true;
1977
1978 // Eat the '}'.
1979 Lex.Lex();
1980 return false;
1981}
1982
Sean Silvacb1a75e2013-01-09 04:49:14 +00001983/// \brief Apply the current let bindings to \a CurRec.
1984/// \returns true on error, false otherwise.
1985bool TGParser::ApplyLetStack(Record *CurRec) {
1986 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1987 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1988 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1989 LetStack[i][j].Bits, LetStack[i][j].Value))
1990 return true;
1991 return false;
1992}
1993
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001994/// ParseObjectBody - Parse the body of a def or class. This consists of an
1995/// optional ClassList followed by a Body. CurRec is the current def or class
1996/// that is being parsed.
1997///
1998/// ObjectBody ::= BaseClassList Body
1999/// BaseClassList ::= /*empty*/
2000/// BaseClassList ::= ':' BaseClassListNE
2001/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
2002///
2003bool TGParser::ParseObjectBody(Record *CurRec) {
2004 // If there is a baseclass list, read it.
2005 if (Lex.getCode() == tgtok::colon) {
2006 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002007
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002008 // Read all of the subclasses.
2009 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
2010 while (1) {
2011 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002012 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002013
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002014 // Add it.
2015 if (AddSubClass(CurRec, SubClass))
2016 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002017
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002018 if (Lex.getCode() != tgtok::comma) break;
2019 Lex.Lex(); // eat ','.
2020 SubClass = ParseSubClassReference(CurRec, false);
2021 }
2022 }
2023
Sean Silvacb1a75e2013-01-09 04:49:14 +00002024 if (ApplyLetStack(CurRec))
2025 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002026
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002027 return ParseBody(CurRec);
2028}
2029
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002030/// ParseDef - Parse and return a top level or multiclass def, return the record
2031/// corresponding to it. This returns null on error.
2032///
2033/// DefInst ::= DEF ObjectName ObjectBody
2034///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002035bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00002036 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002037 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00002038 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002039
2040 // Parse ObjectName and make a record for it.
Jordan Roseabdd99b2013-01-10 18:50:05 +00002041 Record *CurRec;
Anton Yartsev671dff12014-08-08 00:29:54 +00002042 bool CurRecOwnershipTransferred = false;
Jordan Roseabdd99b2013-01-10 18:50:05 +00002043 Init *Name = ParseObjectName(CurMultiClass);
2044 if (Name)
2045 CurRec = new Record(Name, DefLoc, Records);
2046 else
2047 CurRec = new Record(GetNewAnonymousName(), DefLoc, Records,
2048 /*IsAnonymous=*/true);
Bob Wilson7248f862009-11-22 04:24:42 +00002049
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002050 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002051 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002052
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002053 // Ensure redefinition doesn't happen.
David Greeneebb006f2012-01-28 00:03:24 +00002054 if (Records.getDef(CurRec->getNameInitAsString())) {
David Greene3a20f5a2011-10-19 13:03:45 +00002055 Error(DefLoc, "def '" + CurRec->getNameInitAsString()
2056 + "' already defined");
Anton Yartsev671dff12014-08-08 00:29:54 +00002057 delete CurRec;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002058 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002059 }
2060 Records.addDef(CurRec);
Anton Yartsev671dff12014-08-08 00:29:54 +00002061 CurRecOwnershipTransferred = true;
Hal Finkela8c1f462014-01-02 20:47:09 +00002062
2063 if (ParseObjectBody(CurRec))
2064 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002065 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002066 // Parse the body before adding this prototype to the DefPrototypes vector.
2067 // That way implicit definitions will be added to the DefPrototypes vector
2068 // before this object, instantiated prior to defs derived from this object,
2069 // and this available for indirect name resolution when defs derived from
2070 // this object are instantiated.
Anton Yartsev671dff12014-08-08 00:29:54 +00002071 if (ParseObjectBody(CurRec)) {
2072 delete CurRec;
Hal Finkela8c1f462014-01-02 20:47:09 +00002073 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002074 }
Hal Finkela8c1f462014-01-02 20:47:09 +00002075
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002076 // Otherwise, a def inside a multiclass, add it to the multiclass.
2077 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene07e055f2011-10-19 13:03:51 +00002078 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2079 == CurRec->getNameInit()) {
2080 Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002081 "' already defined in this multiclass!");
Anton Yartsev671dff12014-08-08 00:29:54 +00002082 delete CurRec;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002083 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002084 }
2085 CurMultiClass->DefPrototypes.push_back(CurRec);
Anton Yartsev671dff12014-08-08 00:29:54 +00002086 CurRecOwnershipTransferred = true;
2087 } else if (ParseObjectBody(CurRec)) {
2088 delete CurRec;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002089 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002090 }
Bob Wilson7248f862009-11-22 04:24:42 +00002091
Craig Topper011817a2014-04-09 04:50:04 +00002092 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002093 // See Record::setName(). This resolve step will see any new name
2094 // for the def that might have been created when resolving
2095 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002096 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002097
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002098 // If ObjectBody has template arguments, it's an error.
2099 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002100
2101 if (CurMultiClass) {
2102 // Copy the template arguments for the multiclass into the def.
David Greenedb10e692011-10-19 13:02:42 +00002103 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002104 CurMultiClass->Rec.getTemplateArgs();
2105
2106 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2107 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
2108 assert(RV && "Template arg doesn't exist?");
2109 CurRec->addValue(*RV);
2110 }
2111 }
2112
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002113 if (ProcessForeachDefs(CurRec, DefLoc)) {
David Greenefb927af2012-02-22 16:09:41 +00002114 Error(DefLoc,
2115 "Could not process loops for def" + CurRec->getNameInitAsString());
Anton Yartsev671dff12014-08-08 00:29:54 +00002116 if (!CurRecOwnershipTransferred)
2117 delete CurRec;
David Greenefb927af2012-02-22 16:09:41 +00002118 return true;
2119 }
2120
Anton Yartsev671dff12014-08-08 00:29:54 +00002121 if (!CurRecOwnershipTransferred)
2122 delete CurRec;
David Greenefb927af2012-02-22 16:09:41 +00002123 return false;
2124}
2125
2126/// ParseForeach - Parse a for statement. Return the record corresponding
2127/// to it. This returns true on error.
2128///
2129/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2130/// Foreach ::= FOREACH Declaration IN Object
2131///
2132bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2133 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2134 Lex.Lex(); // Eat the 'for' token.
2135
2136 // Make a temporary object to record items associated with the for
2137 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002138 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002139 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002140 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002141 return TokError("expected declaration in for");
2142
2143 if (Lex.getCode() != tgtok::In)
2144 return TokError("Unknown tok");
2145 Lex.Lex(); // Eat the in
2146
2147 // Create a loop object and remember it.
2148 Loops.push_back(ForeachLoop(IterName, ListValue));
2149
2150 if (Lex.getCode() != tgtok::l_brace) {
2151 // FOREACH Declaration IN Object
2152 if (ParseObject(CurMultiClass))
2153 return true;
2154 }
2155 else {
2156 SMLoc BraceLoc = Lex.getLoc();
2157 // Otherwise, this is a group foreach.
2158 Lex.Lex(); // eat the '{'.
2159
2160 // Parse the object list.
2161 if (ParseObjectList(CurMultiClass))
2162 return true;
2163
2164 if (Lex.getCode() != tgtok::r_brace) {
2165 TokError("expected '}' at end of foreach command");
2166 return Error(BraceLoc, "to match this '{'");
2167 }
2168 Lex.Lex(); // Eat the }
2169 }
2170
2171 // We've processed everything in this loop.
2172 Loops.pop_back();
2173
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002174 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002175}
2176
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002177/// ParseClass - Parse a tblgen class definition.
2178///
2179/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2180///
2181bool TGParser::ParseClass() {
2182 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2183 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002184
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002185 if (Lex.getCode() != tgtok::Id)
2186 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002187
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002188 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2189 if (CurRec) {
2190 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002191 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002192 !CurRec->getSuperClasses().empty() ||
2193 !CurRec->getTemplateArgs().empty())
David Greene8eed9982011-10-19 13:03:58 +00002194 return TokError("Class '" + CurRec->getNameInitAsString()
2195 + "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002196 } else {
2197 // If this is the first reference to this class, create and add it.
Chris Lattner89dcb682010-12-15 04:48:22 +00002198 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002199 Records.addClass(CurRec);
2200 }
2201 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002202
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002203 // If there are template args, parse them.
2204 if (Lex.getCode() == tgtok::less)
2205 if (ParseTemplateArgList(CurRec))
2206 return true;
2207
2208 // Finally, parse the object body.
2209 return ParseObjectBody(CurRec);
2210}
2211
2212/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2213/// of LetRecords.
2214///
2215/// LetList ::= LetItem (',' LetItem)*
2216/// LetItem ::= ID OptionalRangeList '=' Value
2217///
2218std::vector<LetRecord> TGParser::ParseLetList() {
2219 std::vector<LetRecord> Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002220
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002221 while (1) {
2222 if (Lex.getCode() != tgtok::Id) {
2223 TokError("expected identifier in let definition");
2224 return std::vector<LetRecord>();
2225 }
2226 std::string Name = Lex.getCurStrVal();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002227 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002228 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002229
2230 // Check for an optional RangeList.
2231 std::vector<unsigned> Bits;
Bob Wilson7248f862009-11-22 04:24:42 +00002232 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002233 return std::vector<LetRecord>();
2234 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002235
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002236 if (Lex.getCode() != tgtok::equal) {
2237 TokError("expected '=' in let expression");
2238 return std::vector<LetRecord>();
2239 }
2240 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002241
Craig Topper011817a2014-04-09 04:50:04 +00002242 Init *Val = ParseValue(nullptr);
2243 if (!Val) return std::vector<LetRecord>();
Bob Wilson7248f862009-11-22 04:24:42 +00002244
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002245 // Now that we have everything, add the record.
2246 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson7248f862009-11-22 04:24:42 +00002247
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002248 if (Lex.getCode() != tgtok::comma)
2249 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002250 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002251 }
2252}
2253
2254/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002255/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002256///
2257/// Object ::= LET LetList IN '{' ObjectList '}'
2258/// Object ::= LET LetList IN Object
2259///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002260bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002261 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2262 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002263
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002264 // Add this entry to the let stack.
2265 std::vector<LetRecord> LetInfo = ParseLetList();
2266 if (LetInfo.empty()) return true;
2267 LetStack.push_back(LetInfo);
2268
2269 if (Lex.getCode() != tgtok::In)
2270 return TokError("expected 'in' at end of top-level 'let'");
2271 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002272
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002273 // If this is a scalar let, just handle it now
2274 if (Lex.getCode() != tgtok::l_brace) {
2275 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002276 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002277 return true;
2278 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002279 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002280 // Otherwise, this is a group let.
2281 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002282
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002283 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002284 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002285 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002286
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002287 if (Lex.getCode() != tgtok::r_brace) {
2288 TokError("expected '}' at end of top level let command");
2289 return Error(BraceLoc, "to match this '{'");
2290 }
2291 Lex.Lex();
2292 }
Bob Wilson7248f862009-11-22 04:24:42 +00002293
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002294 // Outside this let scope, this let block is not active.
2295 LetStack.pop_back();
2296 return false;
2297}
2298
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002299/// ParseMultiClass - Parse a multiclass definition.
2300///
Bob Wilson3d948162009-04-28 19:41:44 +00002301/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002302/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2303/// MultiClassObject ::= DefInst
2304/// MultiClassObject ::= MultiClassInst
2305/// MultiClassObject ::= DefMInst
2306/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2307/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002308///
2309bool TGParser::ParseMultiClass() {
2310 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2311 Lex.Lex(); // Eat the multiclass token.
2312
2313 if (Lex.getCode() != tgtok::Id)
2314 return TokError("expected identifier after multiclass for name");
2315 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002316
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002317 if (MultiClasses.count(Name))
2318 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002319
Chris Lattner77d369c2010-12-13 00:23:57 +00002320 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
2321 Lex.getLoc(), Records);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002322 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002323
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002324 // If there are template args, parse them.
2325 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002326 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002327 return true;
2328
David Greene7049e792009-04-24 16:55:41 +00002329 bool inherits = false;
2330
David Greene753ed8f2009-04-22 16:42:54 +00002331 // If there are submulticlasses, parse them.
2332 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002333 inherits = true;
2334
David Greene753ed8f2009-04-22 16:42:54 +00002335 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002336
David Greene753ed8f2009-04-22 16:42:54 +00002337 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002338 SubMultiClassReference SubMultiClass =
2339 ParseSubMultiClassReference(CurMultiClass);
David Greene753ed8f2009-04-22 16:42:54 +00002340 while (1) {
2341 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002342 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002343
David Greene753ed8f2009-04-22 16:42:54 +00002344 // Add it.
2345 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2346 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002347
David Greene753ed8f2009-04-22 16:42:54 +00002348 if (Lex.getCode() != tgtok::comma) break;
2349 Lex.Lex(); // eat ','.
2350 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2351 }
2352 }
2353
David Greene7049e792009-04-24 16:55:41 +00002354 if (Lex.getCode() != tgtok::l_brace) {
2355 if (!inherits)
2356 return TokError("expected '{' in multiclass definition");
Bob Wilson7248f862009-11-22 04:24:42 +00002357 else if (Lex.getCode() != tgtok::semi)
2358 return TokError("expected ';' in multiclass definition");
David Greene7049e792009-04-24 16:55:41 +00002359 else
Bob Wilson7248f862009-11-22 04:24:42 +00002360 Lex.Lex(); // eat the ';'.
2361 } else {
David Greene7049e792009-04-24 16:55:41 +00002362 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2363 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002364
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002365 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002366 switch (Lex.getCode()) {
2367 default:
David Greene33f61992011-10-07 18:25:05 +00002368 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002369 case tgtok::Let:
2370 case tgtok::Def:
2371 case tgtok::Defm:
David Greenefb927af2012-02-22 16:09:41 +00002372 case tgtok::Foreach:
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002373 if (ParseObject(CurMultiClass))
2374 return true;
2375 break;
2376 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002377 }
David Greene7049e792009-04-24 16:55:41 +00002378 Lex.Lex(); // eat the '}'.
2379 }
Bob Wilson7248f862009-11-22 04:24:42 +00002380
Craig Topper011817a2014-04-09 04:50:04 +00002381 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002382 return false;
2383}
2384
David Greenedb445972011-10-05 22:42:07 +00002385Record *TGParser::
2386InstantiateMulticlassDef(MultiClass &MC,
2387 Record *DefProto,
Hal Finkelf2a0b2b2014-01-02 19:35:33 +00002388 Init *&DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002389 SMRange DefmPrefixRange) {
David Greene5d5d88c2011-10-19 13:04:31 +00002390 // We need to preserve DefProto so it can be reused for later
2391 // instantiations, so create a new Record to inherit from it.
2392
David Greenedb445972011-10-05 22:42:07 +00002393 // Add in the defm name. If the defm prefix is empty, give each
2394 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2395 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2396 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002397
Jordan Roseabdd99b2013-01-10 18:50:05 +00002398 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002399 if (!DefmPrefix) {
David Greene5d5d88c2011-10-19 13:04:31 +00002400 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002401 IsAnonymous = true;
2402 }
David Greene5d5d88c2011-10-19 13:04:31 +00002403
2404 Init *DefName = DefProto->getNameInit();
2405
Sean Silvafb509ed2012-10-10 20:24:43 +00002406 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002407
Craig Topper011817a2014-04-09 04:50:04 +00002408 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002409 // We have a fully expanded string so there are no operators to
2410 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002411 DefName =
2412 BinOpInit::get(BinOpInit::STRCONCAT,
2413 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2414 StringRecTy::get())->Fold(DefProto, &MC),
2415 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2416 }
David Greene5d5d88c2011-10-19 13:04:31 +00002417
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002418 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002419 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002420 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002421 Record *CurRec = new Record(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002422
2423 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002424 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002425 Ref.Rec = DefProto;
2426 AddSubClass(CurRec, Ref);
2427
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002428 // Set the value for NAME. We don't resolve references to it 'til later,
2429 // though, so that uses in nested multiclass names don't get
2430 // confused.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002431 if (SetValue(CurRec, Ref.RefRange.Start, "NAME", std::vector<unsigned>(),
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002432 DefmPrefix)) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002433 Error(DefmPrefixRange.Start, "Could not resolve "
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002434 + CurRec->getNameInitAsString() + ":NAME to '"
2435 + DefmPrefix->getAsUnquotedString() + "'");
Anton Yartsev671dff12014-08-08 00:29:54 +00002436 delete CurRec;
Craig Topper011817a2014-04-09 04:50:04 +00002437 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002438 }
David Greene8bf0d722011-10-19 13:04:35 +00002439
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002440 // If the DefNameString didn't resolve, we probably have a reference to
2441 // NAME and need to replace it. We need to do at least this much greedily,
2442 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002443 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002444 RecordVal *DefNameRV = CurRec->getValue("NAME");
2445 CurRec->resolveReferencesTo(DefNameRV);
2446 }
2447
2448 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002449 // Now that we're at the top level, resolve all NAME references
2450 // in the resultant defs that weren't in the def names themselves.
2451 RecordVal *DefNameRV = CurRec->getValue("NAME");
2452 CurRec->resolveReferencesTo(DefNameRV);
2453
2454 // Now that NAME references are resolved and we're at the top level of
2455 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002456 // currently in a multiclass, it means this defm appears inside a
2457 // multiclass and its name won't be fully resolvable until we see
2458 // the top-level defm. Therefore, we don't add this to the
2459 // RecordKeeper at this point. If we did we could get duplicate
2460 // defs as more than one probably refers to NAME or some other
2461 // common internal placeholder.
2462
2463 // Ensure redefinition doesn't happen.
2464 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002465 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002466 "' already defined, instantiating defm with subdef '" +
2467 DefProto->getNameInitAsString() + "'");
Anton Yartsev671dff12014-08-08 00:29:54 +00002468 delete CurRec;
Craig Topper011817a2014-04-09 04:50:04 +00002469 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002470 }
2471
2472 Records.addDef(CurRec);
2473 }
2474
David Greenedb445972011-10-05 22:42:07 +00002475 return CurRec;
2476}
2477
2478bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2479 Record *CurRec,
2480 SMLoc DefmPrefixLoc,
2481 SMLoc SubClassLoc,
David Greenedb10e692011-10-19 13:02:42 +00002482 const std::vector<Init *> &TArgs,
David Greenedb445972011-10-05 22:42:07 +00002483 std::vector<Init *> &TemplateVals,
2484 bool DeleteArgs) {
2485 // Loop over all of the template arguments, setting them to the specified
2486 // value or leaving them as the default if necessary.
2487 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2488 // Check if a value is specified for this temp-arg.
2489 if (i < TemplateVals.size()) {
2490 // Set it now.
2491 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2492 TemplateVals[i]))
2493 return true;
2494
2495 // Resolve it next.
2496 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2497
2498 if (DeleteArgs)
2499 // Now remove it.
2500 CurRec->removeValue(TArgs[i]);
2501
2502 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2503 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenedb10e692011-10-19 13:02:42 +00002504 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2505 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2506 + "'");
David Greenedb445972011-10-05 22:42:07 +00002507 }
2508 }
2509 return false;
2510}
2511
2512bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2513 Record *CurRec,
2514 Record *DefProto,
2515 SMLoc DefmPrefixLoc) {
2516 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002517 if (ApplyLetStack(CurRec))
2518 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002519
David Greenedb445972011-10-05 22:42:07 +00002520 // Don't create a top level definition for defm inside multiclasses,
2521 // instead, only update the prototypes and bind the template args
2522 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002523 if (!CurMultiClass)
2524 return false;
2525 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2526 i != e; ++i)
2527 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2528 == CurRec->getNameInit())
2529 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2530 "' already defined in this multiclass!");
2531 CurMultiClass->DefPrototypes.push_back(CurRec);
David Greenedb445972011-10-05 22:42:07 +00002532
Sean Silvacc951b22013-01-09 05:28:12 +00002533 // Copy the template arguments for the multiclass into the new def.
2534 const std::vector<Init *> &TA =
2535 CurMultiClass->Rec.getTemplateArgs();
David Greenedb445972011-10-05 22:42:07 +00002536
Sean Silvacc951b22013-01-09 05:28:12 +00002537 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2538 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2539 assert(RV && "Template arg doesn't exist?");
2540 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002541 }
2542
2543 return false;
2544}
2545
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002546/// ParseDefm - Parse the instantiation of a multiclass.
2547///
2548/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2549///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002550bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002551 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002552 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002553 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002554
Craig Topperb21afc62013-01-07 05:09:33 +00002555 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002556 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002557 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002558
Jordan Rosef12e8a92013-01-10 18:50:11 +00002559 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002560 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002561 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002562
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002563 // Keep track of the new generated record definitions.
2564 std::vector<Record*> NewRecDefs;
2565
2566 // This record also inherits from a regular class (non-multiclass)?
2567 bool InheritFromClass = false;
2568
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002569 // eat the colon.
2570 Lex.Lex();
2571
Chris Lattner526c8cb2009-06-21 03:39:35 +00002572 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002573 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002574
2575 while (1) {
Craig Topper011817a2014-04-09 04:50:04 +00002576 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002577
2578 // To instantiate a multiclass, we need to first get the multiclass, then
2579 // instantiate each def contained in the multiclass with the SubClassRef
2580 // template parameters.
2581 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2582 assert(MC && "Didn't lookup multiclass correctly?");
David Greeneaf8ee2c2011-07-29 22:43:06 +00002583 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002584
2585 // Verify that the correct number of template arguments were specified.
David Greenedb10e692011-10-19 13:02:42 +00002586 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002587 if (TArgs.size() < TemplateVals.size())
2588 return Error(SubClassLoc,
2589 "more template args specified than multiclass expects");
2590
2591 // Loop over all the def's in the multiclass, instantiating each one.
2592 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2593 Record *DefProto = MC->DefPrototypes[i];
2594
Jordan Rosef12e8a92013-01-10 18:50:11 +00002595 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix,
2596 SMRange(DefmLoc,
2597 DefmPrefixEndLoc));
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002598 if (!CurRec)
2599 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002600
Jordan Rosef12e8a92013-01-10 18:50:11 +00002601 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002602 TArgs, TemplateVals, true/*Delete args*/))
2603 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002604
Jordan Rosef12e8a92013-01-10 18:50:11 +00002605 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002606 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002607
Adam Nemete5a07162014-09-16 17:14:13 +00002608 // Defs that can be used by other definitions should be fully resolved
2609 // before any use.
2610 if (DefProto->isResolveFirst() && !CurMultiClass) {
2611 CurRec->resolveReferences();
2612 CurRec->setResolveFirst(false);
2613 }
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002614 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002615 }
2616
David Greenedb445972011-10-05 22:42:07 +00002617
David Greenef00919a2009-04-22 22:17:51 +00002618 if (Lex.getCode() != tgtok::comma) break;
2619 Lex.Lex(); // eat ','.
2620
Craig Topper998a39a2013-08-20 04:22:09 +00002621 if (Lex.getCode() != tgtok::Id)
2622 return TokError("expected identifier");
2623
David Greenef00919a2009-04-22 22:17:51 +00002624 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002625
2626 // A defm can inherit from regular classes (non-multiclass) as
2627 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002628 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002629
2630 if (InheritFromClass)
2631 break;
2632
Craig Topper011817a2014-04-09 04:50:04 +00002633 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002634 }
2635
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002636 if (InheritFromClass) {
2637 // Process all the classes to inherit as if they were part of a
2638 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002639 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002640 while (1) {
2641 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002642 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002643
2644 // Get the expanded definition prototypes and teach them about
2645 // the record values the current class to inherit has
2646 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2647 Record *CurRec = NewRecDefs[i];
2648
2649 // Add it.
2650 if (AddSubClass(CurRec, SubClass))
2651 return true;
2652
Sean Silvacb1a75e2013-01-09 04:49:14 +00002653 if (ApplyLetStack(CurRec))
2654 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002655 }
2656
2657 if (Lex.getCode() != tgtok::comma) break;
2658 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002659 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002660 }
2661 }
2662
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002663 if (!CurMultiClass)
2664 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene50c09122011-08-10 18:27:46 +00002665 // See Record::setName(). This resolve step will see any new
2666 // name for the def that might have been created when resolving
2667 // inheritance, values and arguments above.
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002668 NewRecDefs[i]->resolveReferences();
2669
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002670 if (Lex.getCode() != tgtok::semi)
2671 return TokError("expected ';' at end of defm");
2672 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002673
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002674 return false;
2675}
2676
2677/// ParseObject
2678/// Object ::= ClassInst
2679/// Object ::= DefInst
2680/// Object ::= MultiClassInst
2681/// Object ::= DefMInst
2682/// Object ::= LETCommand '{' ObjectList '}'
2683/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002684bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002685 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002686 default:
2687 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002688 case tgtok::Let: return ParseTopLevelLet(MC);
2689 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002690 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002691 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002692 case tgtok::Class: return ParseClass();
2693 case tgtok::MultiClass: return ParseMultiClass();
2694 }
2695}
2696
2697/// ParseObjectList
2698/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002699bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002700 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002701 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002702 return true;
2703 }
2704 return false;
2705}
2706
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002707bool TGParser::ParseFile() {
2708 Lex.Lex(); // Prime the lexer.
2709 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002710
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002711 // If we have unread input at the end of the file, report it.
2712 if (Lex.getCode() == tgtok::Eof)
2713 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002714
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002715 return TokError("Unexpected input at top level");
2716}
2717