blob: 9c061a8ae4cbc7c22f12ebfb55c5666184448fa0 [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 {
1267 // Otherwise, we're inside a multiclass, add it to the multiclass.
1268 CurMultiClass->DefPrototypes.push_back(NewRec);
1269
1270 // Copy the template arguments for the multiclass into the def.
1271 const std::vector<Init *> &TArgs =
1272 CurMultiClass->Rec.getTemplateArgs();
1273
1274 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1275 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1276 assert(RV && "Template arg doesn't exist?");
1277 NewRec->addValue(*RV);
1278 }
1279
1280 // We can't return the prototype def here, instead return:
1281 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1282 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1283 assert(MCNameRV && "multiclass record must have a NAME");
1284
1285 return UnOpInit::get(UnOpInit::CAST,
1286 BinOpInit::get(BinOpInit::STRCONCAT,
1287 VarInit::get(MCNameRV->getName(),
1288 MCNameRV->getType()),
1289 NewRec->getNameInit(),
1290 StringRecTy::get()),
1291 Class->getDefInit()->getType());
1292 }
Bob Wilson7248f862009-11-22 04:24:42 +00001293
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001294 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001295 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001296 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001297 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001298 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001299 Lex.Lex(); // eat the '{'
David Greeneaf8ee2c2011-07-29 22:43:06 +00001300 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001301
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001302 if (Lex.getCode() != tgtok::r_brace) {
1303 Vals = ParseValueList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001304 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001305 }
1306 if (Lex.getCode() != tgtok::r_brace) {
1307 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001308 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001309 }
1310 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001311
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001312 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001313
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001314 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1315 // first. We'll first read everything in to a vector, then we can reverse
1316 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001317 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001318 // bits<n> values are allowed to initialize n bits.
1319 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1320 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1321 NewBits.push_back(BI->getBit((e - i) - 1));
1322 continue;
1323 }
1324 // All other values must be convertible to just a single bit.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001325 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001326 if (!Bit) {
Chris Lattner52416952007-11-22 21:06:59 +00001327 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1328 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001329 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001330 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001331 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001332 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001333 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001334 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001335 }
1336 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1337 Lex.Lex(); // eat the '['
David Greeneaf8ee2c2011-07-29 22:43:06 +00001338 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001339
Craig Topper011817a2014-04-09 04:50:04 +00001340 RecTy *DeducedEltTy = nullptr;
1341 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001342
Craig Topper011817a2014-04-09 04:50:04 +00001343 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001344 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001345 if (!ListType) {
Alp Tokere69170a2014-06-26 22:52:05 +00001346 std::string s;
1347 raw_string_ostream ss(s);
Reid Klecknerd78273f2013-08-06 22:51:21 +00001348 ss << "Type mismatch for list, expected list type, got "
1349 << ItemType->getAsString();
1350 TokError(ss.str());
Craig Topper011817a2014-04-09 04:50:04 +00001351 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001352 }
1353 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001354 }
David Greene8618f952009-06-08 20:23:18 +00001355
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001356 if (Lex.getCode() != tgtok::r_square) {
Craig Topper011817a2014-04-09 04:50:04 +00001357 Vals = ParseValueList(CurRec, nullptr,
1358 GivenListTy ? GivenListTy->getElementType() : nullptr);
1359 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001360 }
1361 if (Lex.getCode() != tgtok::r_square) {
1362 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001363 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001364 }
1365 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001366
Craig Topper011817a2014-04-09 04:50:04 +00001367 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001368 if (Lex.getCode() == tgtok::less) {
1369 // Optional list element type
1370 Lex.Lex(); // eat the '<'
1371
1372 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001373 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001374 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001375 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001376 }
1377
1378 if (Lex.getCode() != tgtok::greater) {
1379 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001380 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001381 }
1382 Lex.Lex(); // eat the '>'
1383 }
1384
1385 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001386 RecTy *EltTy = nullptr;
David Greeneaf8ee2c2011-07-29 22:43:06 +00001387 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greene8618f952009-06-08 20:23:18 +00001388 i != ie;
1389 ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +00001390 TypedInit *TArg = dyn_cast<TypedInit>(*i);
Craig Topper011817a2014-04-09 04:50:04 +00001391 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001392 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001393 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001394 }
Craig Topper011817a2014-04-09 04:50:04 +00001395 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001396 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001397 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001398 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001399 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001400 }
Bob Wilson7248f862009-11-22 04:24:42 +00001401 } else {
David Greene8618f952009-06-08 20:23:18 +00001402 EltTy = TArg->getType();
1403 }
1404 }
1405
Craig Topper011817a2014-04-09 04:50:04 +00001406 if (GivenEltTy) {
1407 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001408 // Verify consistency
1409 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1410 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001411 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001412 }
1413 }
1414 EltTy = GivenEltTy;
1415 }
1416
Craig Topper011817a2014-04-09 04:50:04 +00001417 if (!EltTy) {
1418 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001419 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001420 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001421 }
1422 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001423 } else {
David Greene8618f952009-06-08 20:23:18 +00001424 // Make sure the deduced type is compatible with the given type
1425 if (GivenListTy) {
1426 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1427 TokError("Element type mismatch for list");
Craig Topper011817a2014-04-09 04:50:04 +00001428 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001429 }
1430 }
1431 DeducedEltTy = EltTy;
1432 }
Bob Wilson7248f862009-11-22 04:24:42 +00001433
David Greenee32ebf22011-07-29 19:07:07 +00001434 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001435 }
1436 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1437 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001438 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001439 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001440 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001441 }
Bob Wilson7248f862009-11-22 04:24:42 +00001442
David Greeneaf8ee2c2011-07-29 22:43:06 +00001443 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001444 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001445
Nate Begemandbe3f772009-03-19 05:21:56 +00001446 // If the operator name is present, parse it.
1447 std::string OperatorName;
1448 if (Lex.getCode() == tgtok::colon) {
1449 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1450 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001451 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001452 }
1453 OperatorName = Lex.getCurStrVal();
1454 Lex.Lex(); // eat the VarName.
1455 }
Bob Wilson7248f862009-11-22 04:24:42 +00001456
David Greeneaf8ee2c2011-07-29 22:43:06 +00001457 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001458 if (Lex.getCode() != tgtok::r_paren) {
1459 DagArgs = ParseDagArgList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001460 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001461 }
Bob Wilson7248f862009-11-22 04:24:42 +00001462
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001463 if (Lex.getCode() != tgtok::r_paren) {
1464 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001465 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001466 }
1467 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001468
David Greenee32ebf22011-07-29 19:07:07 +00001469 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001470 }
Bob Wilson7248f862009-11-22 04:24:42 +00001471
David Greene2f7cf7f2011-01-07 17:05:37 +00001472 case tgtok::XHead:
1473 case tgtok::XTail:
1474 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001475 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001476 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001477 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001478 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +00001479 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001480 case tgtok::XSRL:
1481 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001482 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001483 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001484 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001485 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001486 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001487 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001488 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001489 }
1490 }
Bob Wilson7248f862009-11-22 04:24:42 +00001491
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001492 return R;
1493}
1494
1495/// ParseValue - Parse a tblgen value. This returns null on error.
1496///
1497/// Value ::= SimpleValue ValueSuffix*
1498/// ValueSuffix ::= '{' BitList '}'
1499/// ValueSuffix ::= '[' BitList ']'
1500/// ValueSuffix ::= '.' ID
1501///
David Greened4263a62011-10-19 13:04:20 +00001502Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1503 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001504 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001505
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001506 // Parse the suffixes now if present.
1507 while (1) {
1508 switch (Lex.getCode()) {
1509 default: return Result;
1510 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001511 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001512 // This is the beginning of the object body.
1513 return Result;
1514
Chris Lattner526c8cb2009-06-21 03:39:35 +00001515 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001516 Lex.Lex(); // eat the '{'
1517 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001518 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001519
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001520 // Reverse the bitlist.
1521 std::reverse(Ranges.begin(), Ranges.end());
1522 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001523 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001524 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001525 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001526 }
Bob Wilson7248f862009-11-22 04:24:42 +00001527
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001528 // Eat the '}'.
1529 if (Lex.getCode() != tgtok::r_brace) {
1530 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001531 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001532 }
1533 Lex.Lex();
1534 break;
1535 }
1536 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001537 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001538 Lex.Lex(); // eat the '['
1539 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001540 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001541
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001542 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001543 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001544 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001545 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001546 }
Bob Wilson7248f862009-11-22 04:24:42 +00001547
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001548 // Eat the ']'.
1549 if (Lex.getCode() != tgtok::r_square) {
1550 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001551 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001552 }
1553 Lex.Lex();
1554 break;
1555 }
1556 case tgtok::period:
1557 if (Lex.Lex() != tgtok::Id) { // eat the .
1558 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001559 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001560 }
1561 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001562 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001563 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001564 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001565 }
David Greenee32ebf22011-07-29 19:07:07 +00001566 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001567 Lex.Lex(); // eat field name
1568 break;
David Greene8e85b482011-10-19 13:04:43 +00001569
1570 case tgtok::paste:
1571 SMLoc PasteLoc = Lex.getLoc();
1572
1573 // Create a !strconcat() operation, first casting each operand to
1574 // a string if necessary.
1575
Sean Silvafb509ed2012-10-10 20:24:43 +00001576 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001577 if (!LHS) {
1578 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001579 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001580 }
1581
1582 if (LHS->getType() != StringRecTy::get()) {
1583 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1584 }
1585
Craig Topper011817a2014-04-09 04:50:04 +00001586 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001587
1588 Lex.Lex(); // Eat the '#'.
1589 switch (Lex.getCode()) {
1590 case tgtok::colon:
1591 case tgtok::semi:
1592 case tgtok::l_brace:
1593 // These are all of the tokens that can begin an object body.
1594 // Some of these can also begin values but we disallow those cases
1595 // because they are unlikely to be useful.
1596
1597 // Trailing paste, concat with an empty string.
1598 RHS = StringInit::get("");
1599 break;
1600
1601 default:
1602 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001603 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001604 if (!RHS) {
1605 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001606 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001607 }
1608
1609 if (RHS->getType() != StringRecTy::get()) {
1610 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1611 }
1612
1613 break;
1614 }
1615
1616 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1617 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1618 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001619 }
1620 }
1621}
1622
1623/// ParseDagArgList - Parse the argument list for a dag literal expression.
1624///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001625/// DagArg ::= Value (':' VARNAME)?
1626/// DagArg ::= VARNAME
1627/// DagArgList ::= DagArg
1628/// DagArgList ::= DagArgList ',' DagArg
David Greeneaf8ee2c2011-07-29 22:43:06 +00001629std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001630TGParser::ParseDagArgList(Record *CurRec) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001631 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson7248f862009-11-22 04:24:42 +00001632
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001633 while (1) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001634 // DagArg ::= VARNAME
1635 if (Lex.getCode() == tgtok::VarName) {
1636 // A missing value is treated like '?'.
1637 Result.push_back(std::make_pair(UnsetInit::get(), Lex.getCurStrVal()));
1638 Lex.Lex();
1639 } else {
1640 // DagArg ::= Value (':' VARNAME)?
1641 Init *Val = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001642 if (!Val)
David Greeneaf8ee2c2011-07-29 22:43:06 +00001643 return std::vector<std::pair<llvm::Init*, std::string> >();
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001644
1645 // If the variable name is present, add it.
1646 std::string VarName;
1647 if (Lex.getCode() == tgtok::colon) {
1648 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1649 TokError("expected variable name in dag literal");
1650 return std::vector<std::pair<llvm::Init*, std::string> >();
1651 }
1652 VarName = Lex.getCurStrVal();
1653 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001654 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001655
1656 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001657 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001658 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001659 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001660 }
Bob Wilson7248f862009-11-22 04:24:42 +00001661
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001662 return Result;
1663}
1664
1665
1666/// ParseValueList - Parse a comma separated list of values, returning them as a
1667/// vector. Note that this always expects to be able to parse at least one
1668/// value. It returns an empty list if this is not possible.
1669///
1670/// ValueList ::= Value (',' Value)
1671///
David Greeneaf8ee2c2011-07-29 22:43:06 +00001672std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopher71520a82011-07-11 23:06:52 +00001673 RecTy *EltTy) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001674 std::vector<Init*> Result;
David Greene8618f952009-06-08 20:23:18 +00001675 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001676 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001677 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001678 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001679 if (!TArgs.size()) {
1680 TokError("template argument provided to non-template class");
1681 return std::vector<Init*>();
1682 }
David Greene8618f952009-06-08 20:23:18 +00001683 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001684 if (!RV) {
1685 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1686 << ")\n";
1687 }
David Greene8618f952009-06-08 20:23:18 +00001688 assert(RV && "Template argument record not found??");
1689 ItemType = RV->getType();
1690 ++ArgN;
1691 }
1692 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001693 if (!Result.back()) return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001694
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001695 while (Lex.getCode() == tgtok::comma) {
1696 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001697
Craig Topper011817a2014-04-09 04:50:04 +00001698 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001699 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001700 if (ArgN >= TArgs.size()) {
1701 TokError("too many template arguments");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001702 return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001703 }
David Greene8618f952009-06-08 20:23:18 +00001704 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1705 assert(RV && "Template argument record not found??");
1706 ItemType = RV->getType();
1707 ++ArgN;
1708 }
1709 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001710 if (!Result.back()) return std::vector<Init*>();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001711 }
Bob Wilson7248f862009-11-22 04:24:42 +00001712
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001713 return Result;
1714}
1715
1716
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001717/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1718/// empty string on error. This can happen in a number of different context's,
1719/// including within a def or in the template args for a def (which which case
1720/// CurRec will be non-null) and within the template args for a multiclass (in
1721/// which case CurRec will be null, but CurMultiClass will be set). This can
1722/// also happen within a def that is within a multiclass, which will set both
1723/// CurRec and CurMultiClass.
1724///
1725/// Declaration ::= FIELD? Type ID ('=' Value)?
1726///
David Greenedb10e692011-10-19 13:02:42 +00001727Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001728 bool ParsingTemplateArgs) {
1729 // Read the field prefix if present.
1730 bool HasField = Lex.getCode() == tgtok::Field;
1731 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001732
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001733 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001734 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001735
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001736 if (Lex.getCode() != tgtok::Id) {
1737 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001738 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001739 }
Bob Wilson7248f862009-11-22 04:24:42 +00001740
Chris Lattner526c8cb2009-06-21 03:39:35 +00001741 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001742 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001743 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001744
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001745 if (ParsingTemplateArgs) {
1746 if (CurRec) {
David Greenedb10e692011-10-19 13:02:42 +00001747 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001748 } else {
1749 assert(CurMultiClass);
1750 }
1751 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001752 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1753 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001754 }
Bob Wilson7248f862009-11-22 04:24:42 +00001755
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001756 // Add the value.
1757 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001758 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001759
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001760 // If a value is present, parse it.
1761 if (Lex.getCode() == tgtok::equal) {
1762 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001763 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001764 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001765 if (!Val ||
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001766 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001767 // Return the name, even if an error is thrown. This is so that we can
1768 // continue to make some progress, even without the value having been
1769 // initialized.
1770 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001771 }
Bob Wilson7248f862009-11-22 04:24:42 +00001772
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001773 return DeclName;
1774}
1775
David Greenefb927af2012-02-22 16:09:41 +00001776/// ParseForeachDeclaration - Read a foreach declaration, returning
1777/// the name of the declared object or a NULL Init on error. Return
1778/// the name of the parsed initializer list through ForeachListName.
1779///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001780/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1781/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1782/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001783///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001784VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001785 if (Lex.getCode() != tgtok::Id) {
1786 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001787 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001788 }
1789
1790 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1791 Lex.Lex();
1792
1793 // If a value is present, parse it.
1794 if (Lex.getCode() != tgtok::equal) {
1795 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001796 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001797 }
1798 Lex.Lex(); // Eat the '='
1799
Craig Topper011817a2014-04-09 04:50:04 +00001800 RecTy *IterType = nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001801 std::vector<unsigned> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001802
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001803 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001804 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001805 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001806 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001807 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001808 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001809 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001810 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001811 }
1812 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001813 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001814 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001815 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001816 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001817 }
1818 IterType = ListType->getElementType();
1819 break;
David Greenefb927af2012-02-22 16:09:41 +00001820 }
1821
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001822 case tgtok::IntVal: { // RangePiece.
1823 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001824 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001825 break;
David Greenefb927af2012-02-22 16:09:41 +00001826 }
1827
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001828 case tgtok::l_brace: { // '{' RangeList '}'
1829 Lex.Lex(); // eat the '{'
1830 Ranges = ParseRangeList();
1831 if (Lex.getCode() != tgtok::r_brace) {
1832 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001833 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001834 }
1835 Lex.Lex();
1836 break;
1837 }
1838 }
David Greenefb927af2012-02-22 16:09:41 +00001839
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001840 if (!Ranges.empty()) {
1841 assert(!IterType && "Type already initialized?");
1842 IterType = IntRecTy::get();
1843 std::vector<Init*> Values;
1844 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
1845 Values.push_back(IntInit::get(Ranges[i]));
1846 ForeachListValue = ListInit::get(Values, IterType);
1847 }
1848
1849 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001850 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001851
1852 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001853}
1854
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001855/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1856/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1857/// template args for a def, which may or may not be in a multiclass. If null,
1858/// these are the template args for a multiclass.
1859///
1860/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001861///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001862bool TGParser::ParseTemplateArgList(Record *CurRec) {
1863 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1864 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001865
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001866 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001867
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001868 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001869 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001870 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001871 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001872
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001873 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001874
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001875 while (Lex.getCode() == tgtok::comma) {
1876 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001877
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001878 // Read the following declarations.
1879 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001880 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001881 return true;
1882 TheRecToAddTo->addTemplateArg(TemplArg);
1883 }
Bob Wilson7248f862009-11-22 04:24:42 +00001884
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001885 if (Lex.getCode() != tgtok::greater)
1886 return TokError("expected '>' at end of template argument list");
1887 Lex.Lex(); // eat the '>'.
1888 return false;
1889}
1890
1891
1892/// ParseBodyItem - Parse a single item at within the body of a def or class.
1893///
1894/// BodyItem ::= Declaration ';'
1895/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1896bool TGParser::ParseBodyItem(Record *CurRec) {
1897 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001898 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001899 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001900
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001901 if (Lex.getCode() != tgtok::semi)
1902 return TokError("expected ';' after declaration");
1903 Lex.Lex();
1904 return false;
1905 }
1906
1907 // LET ID OptionalRangeList '=' Value ';'
1908 if (Lex.Lex() != tgtok::Id)
1909 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001910
Chris Lattner526c8cb2009-06-21 03:39:35 +00001911 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001912 std::string FieldName = Lex.getCurStrVal();
1913 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001914
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001915 std::vector<unsigned> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001916 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001917 return true;
1918 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001919
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001920 if (Lex.getCode() != tgtok::equal)
1921 return TokError("expected '=' in let expression");
1922 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001923
David Greene8618f952009-06-08 20:23:18 +00001924 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001925 if (!Field)
David Greene8618f952009-06-08 20:23:18 +00001926 return TokError("Value '" + FieldName + "' unknown!");
1927
1928 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001929
David Greeneaf8ee2c2011-07-29 22:43:06 +00001930 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001931 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001932
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001933 if (Lex.getCode() != tgtok::semi)
1934 return TokError("expected ';' after let expression");
1935 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001936
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001937 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1938}
1939
1940/// ParseBody - Read the body of a class or def. Return true on error, false on
1941/// success.
1942///
1943/// Body ::= ';'
1944/// Body ::= '{' BodyList '}'
1945/// BodyList BodyItem*
1946///
1947bool TGParser::ParseBody(Record *CurRec) {
1948 // If this is a null definition, just eat the semi and return.
1949 if (Lex.getCode() == tgtok::semi) {
1950 Lex.Lex();
1951 return false;
1952 }
Bob Wilson7248f862009-11-22 04:24:42 +00001953
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001954 if (Lex.getCode() != tgtok::l_brace)
1955 return TokError("Expected ';' or '{' to start body");
1956 // Eat the '{'.
1957 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001958
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001959 while (Lex.getCode() != tgtok::r_brace)
1960 if (ParseBodyItem(CurRec))
1961 return true;
1962
1963 // Eat the '}'.
1964 Lex.Lex();
1965 return false;
1966}
1967
Sean Silvacb1a75e2013-01-09 04:49:14 +00001968/// \brief Apply the current let bindings to \a CurRec.
1969/// \returns true on error, false otherwise.
1970bool TGParser::ApplyLetStack(Record *CurRec) {
1971 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1972 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1973 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1974 LetStack[i][j].Bits, LetStack[i][j].Value))
1975 return true;
1976 return false;
1977}
1978
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001979/// ParseObjectBody - Parse the body of a def or class. This consists of an
1980/// optional ClassList followed by a Body. CurRec is the current def or class
1981/// that is being parsed.
1982///
1983/// ObjectBody ::= BaseClassList Body
1984/// BaseClassList ::= /*empty*/
1985/// BaseClassList ::= ':' BaseClassListNE
1986/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1987///
1988bool TGParser::ParseObjectBody(Record *CurRec) {
1989 // If there is a baseclass list, read it.
1990 if (Lex.getCode() == tgtok::colon) {
1991 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001992
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001993 // Read all of the subclasses.
1994 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1995 while (1) {
1996 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00001997 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001998
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001999 // Add it.
2000 if (AddSubClass(CurRec, SubClass))
2001 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002002
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002003 if (Lex.getCode() != tgtok::comma) break;
2004 Lex.Lex(); // eat ','.
2005 SubClass = ParseSubClassReference(CurRec, false);
2006 }
2007 }
2008
Sean Silvacb1a75e2013-01-09 04:49:14 +00002009 if (ApplyLetStack(CurRec))
2010 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002011
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002012 return ParseBody(CurRec);
2013}
2014
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002015/// ParseDef - Parse and return a top level or multiclass def, return the record
2016/// corresponding to it. This returns null on error.
2017///
2018/// DefInst ::= DEF ObjectName ObjectBody
2019///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002020bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00002021 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002022 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00002023 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002024
2025 // Parse ObjectName and make a record for it.
Jordan Roseabdd99b2013-01-10 18:50:05 +00002026 Record *CurRec;
Anton Yartsev671dff12014-08-08 00:29:54 +00002027 bool CurRecOwnershipTransferred = false;
Jordan Roseabdd99b2013-01-10 18:50:05 +00002028 Init *Name = ParseObjectName(CurMultiClass);
2029 if (Name)
2030 CurRec = new Record(Name, DefLoc, Records);
2031 else
2032 CurRec = new Record(GetNewAnonymousName(), DefLoc, Records,
2033 /*IsAnonymous=*/true);
Bob Wilson7248f862009-11-22 04:24:42 +00002034
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002035 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002036 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002037
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002038 // Ensure redefinition doesn't happen.
David Greeneebb006f2012-01-28 00:03:24 +00002039 if (Records.getDef(CurRec->getNameInitAsString())) {
David Greene3a20f5a2011-10-19 13:03:45 +00002040 Error(DefLoc, "def '" + CurRec->getNameInitAsString()
2041 + "' already defined");
Anton Yartsev671dff12014-08-08 00:29:54 +00002042 delete CurRec;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002043 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002044 }
2045 Records.addDef(CurRec);
Anton Yartsev671dff12014-08-08 00:29:54 +00002046 CurRecOwnershipTransferred = true;
Hal Finkela8c1f462014-01-02 20:47:09 +00002047
2048 if (ParseObjectBody(CurRec))
2049 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002050 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002051 // Parse the body before adding this prototype to the DefPrototypes vector.
2052 // That way implicit definitions will be added to the DefPrototypes vector
2053 // before this object, instantiated prior to defs derived from this object,
2054 // and this available for indirect name resolution when defs derived from
2055 // this object are instantiated.
Anton Yartsev671dff12014-08-08 00:29:54 +00002056 if (ParseObjectBody(CurRec)) {
2057 delete CurRec;
Hal Finkela8c1f462014-01-02 20:47:09 +00002058 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002059 }
Hal Finkela8c1f462014-01-02 20:47:09 +00002060
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002061 // Otherwise, a def inside a multiclass, add it to the multiclass.
2062 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene07e055f2011-10-19 13:03:51 +00002063 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2064 == CurRec->getNameInit()) {
2065 Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002066 "' already defined in this multiclass!");
Anton Yartsev671dff12014-08-08 00:29:54 +00002067 delete CurRec;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002068 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002069 }
2070 CurMultiClass->DefPrototypes.push_back(CurRec);
Anton Yartsev671dff12014-08-08 00:29:54 +00002071 CurRecOwnershipTransferred = true;
2072 } else if (ParseObjectBody(CurRec)) {
2073 delete CurRec;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002074 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002075 }
Bob Wilson7248f862009-11-22 04:24:42 +00002076
Craig Topper011817a2014-04-09 04:50:04 +00002077 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002078 // See Record::setName(). This resolve step will see any new name
2079 // for the def that might have been created when resolving
2080 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002081 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002082
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002083 // If ObjectBody has template arguments, it's an error.
2084 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002085
2086 if (CurMultiClass) {
2087 // Copy the template arguments for the multiclass into the def.
David Greenedb10e692011-10-19 13:02:42 +00002088 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002089 CurMultiClass->Rec.getTemplateArgs();
2090
2091 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2092 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
2093 assert(RV && "Template arg doesn't exist?");
2094 CurRec->addValue(*RV);
2095 }
2096 }
2097
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002098 if (ProcessForeachDefs(CurRec, DefLoc)) {
David Greenefb927af2012-02-22 16:09:41 +00002099 Error(DefLoc,
2100 "Could not process loops for def" + CurRec->getNameInitAsString());
Anton Yartsev671dff12014-08-08 00:29:54 +00002101 if (!CurRecOwnershipTransferred)
2102 delete CurRec;
David Greenefb927af2012-02-22 16:09:41 +00002103 return true;
2104 }
2105
Anton Yartsev671dff12014-08-08 00:29:54 +00002106 if (!CurRecOwnershipTransferred)
2107 delete CurRec;
David Greenefb927af2012-02-22 16:09:41 +00002108 return false;
2109}
2110
2111/// ParseForeach - Parse a for statement. Return the record corresponding
2112/// to it. This returns true on error.
2113///
2114/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2115/// Foreach ::= FOREACH Declaration IN Object
2116///
2117bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2118 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2119 Lex.Lex(); // Eat the 'for' token.
2120
2121 // Make a temporary object to record items associated with the for
2122 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002123 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002124 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002125 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002126 return TokError("expected declaration in for");
2127
2128 if (Lex.getCode() != tgtok::In)
2129 return TokError("Unknown tok");
2130 Lex.Lex(); // Eat the in
2131
2132 // Create a loop object and remember it.
2133 Loops.push_back(ForeachLoop(IterName, ListValue));
2134
2135 if (Lex.getCode() != tgtok::l_brace) {
2136 // FOREACH Declaration IN Object
2137 if (ParseObject(CurMultiClass))
2138 return true;
2139 }
2140 else {
2141 SMLoc BraceLoc = Lex.getLoc();
2142 // Otherwise, this is a group foreach.
2143 Lex.Lex(); // eat the '{'.
2144
2145 // Parse the object list.
2146 if (ParseObjectList(CurMultiClass))
2147 return true;
2148
2149 if (Lex.getCode() != tgtok::r_brace) {
2150 TokError("expected '}' at end of foreach command");
2151 return Error(BraceLoc, "to match this '{'");
2152 }
2153 Lex.Lex(); // Eat the }
2154 }
2155
2156 // We've processed everything in this loop.
2157 Loops.pop_back();
2158
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002159 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002160}
2161
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002162/// ParseClass - Parse a tblgen class definition.
2163///
2164/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2165///
2166bool TGParser::ParseClass() {
2167 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2168 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002169
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002170 if (Lex.getCode() != tgtok::Id)
2171 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002172
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002173 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2174 if (CurRec) {
2175 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002176 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002177 !CurRec->getSuperClasses().empty() ||
2178 !CurRec->getTemplateArgs().empty())
David Greene8eed9982011-10-19 13:03:58 +00002179 return TokError("Class '" + CurRec->getNameInitAsString()
2180 + "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002181 } else {
2182 // If this is the first reference to this class, create and add it.
Chris Lattner89dcb682010-12-15 04:48:22 +00002183 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002184 Records.addClass(CurRec);
2185 }
2186 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002187
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002188 // If there are template args, parse them.
2189 if (Lex.getCode() == tgtok::less)
2190 if (ParseTemplateArgList(CurRec))
2191 return true;
2192
2193 // Finally, parse the object body.
2194 return ParseObjectBody(CurRec);
2195}
2196
2197/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2198/// of LetRecords.
2199///
2200/// LetList ::= LetItem (',' LetItem)*
2201/// LetItem ::= ID OptionalRangeList '=' Value
2202///
2203std::vector<LetRecord> TGParser::ParseLetList() {
2204 std::vector<LetRecord> Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002205
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002206 while (1) {
2207 if (Lex.getCode() != tgtok::Id) {
2208 TokError("expected identifier in let definition");
2209 return std::vector<LetRecord>();
2210 }
2211 std::string Name = Lex.getCurStrVal();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002212 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002213 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002214
2215 // Check for an optional RangeList.
2216 std::vector<unsigned> Bits;
Bob Wilson7248f862009-11-22 04:24:42 +00002217 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002218 return std::vector<LetRecord>();
2219 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002220
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002221 if (Lex.getCode() != tgtok::equal) {
2222 TokError("expected '=' in let expression");
2223 return std::vector<LetRecord>();
2224 }
2225 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002226
Craig Topper011817a2014-04-09 04:50:04 +00002227 Init *Val = ParseValue(nullptr);
2228 if (!Val) return std::vector<LetRecord>();
Bob Wilson7248f862009-11-22 04:24:42 +00002229
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002230 // Now that we have everything, add the record.
2231 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson7248f862009-11-22 04:24:42 +00002232
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002233 if (Lex.getCode() != tgtok::comma)
2234 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002235 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002236 }
2237}
2238
2239/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002240/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002241///
2242/// Object ::= LET LetList IN '{' ObjectList '}'
2243/// Object ::= LET LetList IN Object
2244///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002245bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002246 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2247 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002248
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002249 // Add this entry to the let stack.
2250 std::vector<LetRecord> LetInfo = ParseLetList();
2251 if (LetInfo.empty()) return true;
2252 LetStack.push_back(LetInfo);
2253
2254 if (Lex.getCode() != tgtok::In)
2255 return TokError("expected 'in' at end of top-level 'let'");
2256 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002257
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002258 // If this is a scalar let, just handle it now
2259 if (Lex.getCode() != tgtok::l_brace) {
2260 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002261 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002262 return true;
2263 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002264 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002265 // Otherwise, this is a group let.
2266 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002267
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002268 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002269 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002270 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002271
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002272 if (Lex.getCode() != tgtok::r_brace) {
2273 TokError("expected '}' at end of top level let command");
2274 return Error(BraceLoc, "to match this '{'");
2275 }
2276 Lex.Lex();
2277 }
Bob Wilson7248f862009-11-22 04:24:42 +00002278
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002279 // Outside this let scope, this let block is not active.
2280 LetStack.pop_back();
2281 return false;
2282}
2283
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002284/// ParseMultiClass - Parse a multiclass definition.
2285///
Bob Wilson3d948162009-04-28 19:41:44 +00002286/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002287/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2288/// MultiClassObject ::= DefInst
2289/// MultiClassObject ::= MultiClassInst
2290/// MultiClassObject ::= DefMInst
2291/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2292/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002293///
2294bool TGParser::ParseMultiClass() {
2295 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2296 Lex.Lex(); // Eat the multiclass token.
2297
2298 if (Lex.getCode() != tgtok::Id)
2299 return TokError("expected identifier after multiclass for name");
2300 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002301
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002302 if (MultiClasses.count(Name))
2303 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002304
Chris Lattner77d369c2010-12-13 00:23:57 +00002305 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
2306 Lex.getLoc(), Records);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002307 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002308
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002309 // If there are template args, parse them.
2310 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002311 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002312 return true;
2313
David Greene7049e792009-04-24 16:55:41 +00002314 bool inherits = false;
2315
David Greene753ed8f2009-04-22 16:42:54 +00002316 // If there are submulticlasses, parse them.
2317 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002318 inherits = true;
2319
David Greene753ed8f2009-04-22 16:42:54 +00002320 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002321
David Greene753ed8f2009-04-22 16:42:54 +00002322 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002323 SubMultiClassReference SubMultiClass =
2324 ParseSubMultiClassReference(CurMultiClass);
David Greene753ed8f2009-04-22 16:42:54 +00002325 while (1) {
2326 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002327 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002328
David Greene753ed8f2009-04-22 16:42:54 +00002329 // Add it.
2330 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2331 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002332
David Greene753ed8f2009-04-22 16:42:54 +00002333 if (Lex.getCode() != tgtok::comma) break;
2334 Lex.Lex(); // eat ','.
2335 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2336 }
2337 }
2338
David Greene7049e792009-04-24 16:55:41 +00002339 if (Lex.getCode() != tgtok::l_brace) {
2340 if (!inherits)
2341 return TokError("expected '{' in multiclass definition");
Bob Wilson7248f862009-11-22 04:24:42 +00002342 else if (Lex.getCode() != tgtok::semi)
2343 return TokError("expected ';' in multiclass definition");
David Greene7049e792009-04-24 16:55:41 +00002344 else
Bob Wilson7248f862009-11-22 04:24:42 +00002345 Lex.Lex(); // eat the ';'.
2346 } else {
David Greene7049e792009-04-24 16:55:41 +00002347 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2348 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002349
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002350 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002351 switch (Lex.getCode()) {
2352 default:
David Greene33f61992011-10-07 18:25:05 +00002353 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002354 case tgtok::Let:
2355 case tgtok::Def:
2356 case tgtok::Defm:
David Greenefb927af2012-02-22 16:09:41 +00002357 case tgtok::Foreach:
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002358 if (ParseObject(CurMultiClass))
2359 return true;
2360 break;
2361 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002362 }
David Greene7049e792009-04-24 16:55:41 +00002363 Lex.Lex(); // eat the '}'.
2364 }
Bob Wilson7248f862009-11-22 04:24:42 +00002365
Craig Topper011817a2014-04-09 04:50:04 +00002366 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002367 return false;
2368}
2369
David Greenedb445972011-10-05 22:42:07 +00002370Record *TGParser::
2371InstantiateMulticlassDef(MultiClass &MC,
2372 Record *DefProto,
Hal Finkelf2a0b2b2014-01-02 19:35:33 +00002373 Init *&DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002374 SMRange DefmPrefixRange) {
David Greene5d5d88c2011-10-19 13:04:31 +00002375 // We need to preserve DefProto so it can be reused for later
2376 // instantiations, so create a new Record to inherit from it.
2377
David Greenedb445972011-10-05 22:42:07 +00002378 // Add in the defm name. If the defm prefix is empty, give each
2379 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2380 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2381 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002382
Jordan Roseabdd99b2013-01-10 18:50:05 +00002383 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002384 if (!DefmPrefix) {
David Greene5d5d88c2011-10-19 13:04:31 +00002385 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002386 IsAnonymous = true;
2387 }
David Greene5d5d88c2011-10-19 13:04:31 +00002388
2389 Init *DefName = DefProto->getNameInit();
2390
Sean Silvafb509ed2012-10-10 20:24:43 +00002391 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002392
Craig Topper011817a2014-04-09 04:50:04 +00002393 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002394 // We have a fully expanded string so there are no operators to
2395 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002396 DefName =
2397 BinOpInit::get(BinOpInit::STRCONCAT,
2398 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2399 StringRecTy::get())->Fold(DefProto, &MC),
2400 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2401 }
David Greene5d5d88c2011-10-19 13:04:31 +00002402
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002403 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002404 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002405 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002406 Record *CurRec = new Record(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002407
2408 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002409 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002410 Ref.Rec = DefProto;
2411 AddSubClass(CurRec, Ref);
2412
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002413 // Set the value for NAME. We don't resolve references to it 'til later,
2414 // though, so that uses in nested multiclass names don't get
2415 // confused.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002416 if (SetValue(CurRec, Ref.RefRange.Start, "NAME", std::vector<unsigned>(),
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002417 DefmPrefix)) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002418 Error(DefmPrefixRange.Start, "Could not resolve "
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002419 + CurRec->getNameInitAsString() + ":NAME to '"
2420 + DefmPrefix->getAsUnquotedString() + "'");
Anton Yartsev671dff12014-08-08 00:29:54 +00002421 delete CurRec;
Craig Topper011817a2014-04-09 04:50:04 +00002422 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002423 }
David Greene8bf0d722011-10-19 13:04:35 +00002424
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002425 // If the DefNameString didn't resolve, we probably have a reference to
2426 // NAME and need to replace it. We need to do at least this much greedily,
2427 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002428 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002429 RecordVal *DefNameRV = CurRec->getValue("NAME");
2430 CurRec->resolveReferencesTo(DefNameRV);
2431 }
2432
2433 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002434 // Now that we're at the top level, resolve all NAME references
2435 // in the resultant defs that weren't in the def names themselves.
2436 RecordVal *DefNameRV = CurRec->getValue("NAME");
2437 CurRec->resolveReferencesTo(DefNameRV);
2438
2439 // Now that NAME references are resolved and we're at the top level of
2440 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002441 // currently in a multiclass, it means this defm appears inside a
2442 // multiclass and its name won't be fully resolvable until we see
2443 // the top-level defm. Therefore, we don't add this to the
2444 // RecordKeeper at this point. If we did we could get duplicate
2445 // defs as more than one probably refers to NAME or some other
2446 // common internal placeholder.
2447
2448 // Ensure redefinition doesn't happen.
2449 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002450 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002451 "' already defined, instantiating defm with subdef '" +
2452 DefProto->getNameInitAsString() + "'");
Anton Yartsev671dff12014-08-08 00:29:54 +00002453 delete CurRec;
Craig Topper011817a2014-04-09 04:50:04 +00002454 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002455 }
2456
2457 Records.addDef(CurRec);
2458 }
2459
David Greenedb445972011-10-05 22:42:07 +00002460 return CurRec;
2461}
2462
2463bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2464 Record *CurRec,
2465 SMLoc DefmPrefixLoc,
2466 SMLoc SubClassLoc,
David Greenedb10e692011-10-19 13:02:42 +00002467 const std::vector<Init *> &TArgs,
David Greenedb445972011-10-05 22:42:07 +00002468 std::vector<Init *> &TemplateVals,
2469 bool DeleteArgs) {
2470 // Loop over all of the template arguments, setting them to the specified
2471 // value or leaving them as the default if necessary.
2472 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2473 // Check if a value is specified for this temp-arg.
2474 if (i < TemplateVals.size()) {
2475 // Set it now.
2476 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2477 TemplateVals[i]))
2478 return true;
2479
2480 // Resolve it next.
2481 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2482
2483 if (DeleteArgs)
2484 // Now remove it.
2485 CurRec->removeValue(TArgs[i]);
2486
2487 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2488 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenedb10e692011-10-19 13:02:42 +00002489 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2490 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2491 + "'");
David Greenedb445972011-10-05 22:42:07 +00002492 }
2493 }
2494 return false;
2495}
2496
2497bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2498 Record *CurRec,
2499 Record *DefProto,
2500 SMLoc DefmPrefixLoc) {
2501 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002502 if (ApplyLetStack(CurRec))
2503 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002504
David Greenedb445972011-10-05 22:42:07 +00002505 // Don't create a top level definition for defm inside multiclasses,
2506 // instead, only update the prototypes and bind the template args
2507 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002508 if (!CurMultiClass)
2509 return false;
2510 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2511 i != e; ++i)
2512 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2513 == CurRec->getNameInit())
2514 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2515 "' already defined in this multiclass!");
2516 CurMultiClass->DefPrototypes.push_back(CurRec);
David Greenedb445972011-10-05 22:42:07 +00002517
Sean Silvacc951b22013-01-09 05:28:12 +00002518 // Copy the template arguments for the multiclass into the new def.
2519 const std::vector<Init *> &TA =
2520 CurMultiClass->Rec.getTemplateArgs();
David Greenedb445972011-10-05 22:42:07 +00002521
Sean Silvacc951b22013-01-09 05:28:12 +00002522 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2523 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2524 assert(RV && "Template arg doesn't exist?");
2525 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002526 }
2527
2528 return false;
2529}
2530
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002531/// ParseDefm - Parse the instantiation of a multiclass.
2532///
2533/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2534///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002535bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002536 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002537 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002538 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002539
Craig Topperb21afc62013-01-07 05:09:33 +00002540 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002541 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002542 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002543
Jordan Rosef12e8a92013-01-10 18:50:11 +00002544 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002545 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002546 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002547
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002548 // Keep track of the new generated record definitions.
2549 std::vector<Record*> NewRecDefs;
2550
2551 // This record also inherits from a regular class (non-multiclass)?
2552 bool InheritFromClass = false;
2553
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002554 // eat the colon.
2555 Lex.Lex();
2556
Chris Lattner526c8cb2009-06-21 03:39:35 +00002557 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002558 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002559
2560 while (1) {
Craig Topper011817a2014-04-09 04:50:04 +00002561 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002562
2563 // To instantiate a multiclass, we need to first get the multiclass, then
2564 // instantiate each def contained in the multiclass with the SubClassRef
2565 // template parameters.
2566 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2567 assert(MC && "Didn't lookup multiclass correctly?");
David Greeneaf8ee2c2011-07-29 22:43:06 +00002568 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002569
2570 // Verify that the correct number of template arguments were specified.
David Greenedb10e692011-10-19 13:02:42 +00002571 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002572 if (TArgs.size() < TemplateVals.size())
2573 return Error(SubClassLoc,
2574 "more template args specified than multiclass expects");
2575
2576 // Loop over all the def's in the multiclass, instantiating each one.
2577 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2578 Record *DefProto = MC->DefPrototypes[i];
2579
Jordan Rosef12e8a92013-01-10 18:50:11 +00002580 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix,
2581 SMRange(DefmLoc,
2582 DefmPrefixEndLoc));
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002583 if (!CurRec)
2584 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002585
Jordan Rosef12e8a92013-01-10 18:50:11 +00002586 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002587 TArgs, TemplateVals, true/*Delete args*/))
2588 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002589
Jordan Rosef12e8a92013-01-10 18:50:11 +00002590 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002591 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002592
2593 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002594 }
2595
David Greenedb445972011-10-05 22:42:07 +00002596
David Greenef00919a2009-04-22 22:17:51 +00002597 if (Lex.getCode() != tgtok::comma) break;
2598 Lex.Lex(); // eat ','.
2599
Craig Topper998a39a2013-08-20 04:22:09 +00002600 if (Lex.getCode() != tgtok::Id)
2601 return TokError("expected identifier");
2602
David Greenef00919a2009-04-22 22:17:51 +00002603 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002604
2605 // A defm can inherit from regular classes (non-multiclass) as
2606 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002607 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002608
2609 if (InheritFromClass)
2610 break;
2611
Craig Topper011817a2014-04-09 04:50:04 +00002612 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002613 }
2614
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002615 if (InheritFromClass) {
2616 // Process all the classes to inherit as if they were part of a
2617 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002618 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002619 while (1) {
2620 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002621 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002622
2623 // Get the expanded definition prototypes and teach them about
2624 // the record values the current class to inherit has
2625 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2626 Record *CurRec = NewRecDefs[i];
2627
2628 // Add it.
2629 if (AddSubClass(CurRec, SubClass))
2630 return true;
2631
Sean Silvacb1a75e2013-01-09 04:49:14 +00002632 if (ApplyLetStack(CurRec))
2633 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002634 }
2635
2636 if (Lex.getCode() != tgtok::comma) break;
2637 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002638 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002639 }
2640 }
2641
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002642 if (!CurMultiClass)
2643 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene50c09122011-08-10 18:27:46 +00002644 // See Record::setName(). This resolve step will see any new
2645 // name for the def that might have been created when resolving
2646 // inheritance, values and arguments above.
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002647 NewRecDefs[i]->resolveReferences();
2648
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002649 if (Lex.getCode() != tgtok::semi)
2650 return TokError("expected ';' at end of defm");
2651 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002652
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002653 return false;
2654}
2655
2656/// ParseObject
2657/// Object ::= ClassInst
2658/// Object ::= DefInst
2659/// Object ::= MultiClassInst
2660/// Object ::= DefMInst
2661/// Object ::= LETCommand '{' ObjectList '}'
2662/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002663bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002664 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002665 default:
2666 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002667 case tgtok::Let: return ParseTopLevelLet(MC);
2668 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002669 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002670 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002671 case tgtok::Class: return ParseClass();
2672 case tgtok::MultiClass: return ParseMultiClass();
2673 }
2674}
2675
2676/// ParseObjectList
2677/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002678bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002679 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002680 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002681 return true;
2682 }
2683 return false;
2684}
2685
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002686bool TGParser::ParseFile() {
2687 Lex.Lex(); // Prime the lexer.
2688 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002689
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002690 // If we have unread input at the end of the file, report it.
2691 if (Lex.getCode() == tgtok::Eof)
2692 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002693
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002694 return TokError("Unexpected input at top level");
2695}
2696