blob: 2c6a46cb0bc2e90d5db3d91699882ca61371fa18 [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
138 if (RV->setValue(V))
David Greene3ca42122011-10-19 13:02:39 +0000139 return Error(Loc, "Value '" + ValName->getAsUnquotedString() + "' of type '"
140 + RV->getType()->getAsString() +
141 "' is incompatible with initializer '" + V->getAsString()
142 + "'");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000143 return false;
144}
145
146/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
147/// args as SubClass's template arguments.
Cedric Venetd1e179d2009-02-14 16:06:42 +0000148bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000149 Record *SC = SubClass.Rec;
150 // Add all of the values in the subclass into the current class.
151 const std::vector<RecordVal> &Vals = SC->getValues();
152 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
Jordan Rosef12e8a92013-01-10 18:50:11 +0000153 if (AddValue(CurRec, SubClass.RefRange.Start, Vals[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000154 return true;
155
David Greenedb10e692011-10-19 13:02:42 +0000156 const std::vector<Init *> &TArgs = SC->getTemplateArgs();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000157
158 // Ensure that an appropriate number of template arguments are specified.
159 if (TArgs.size() < SubClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000160 return Error(SubClass.RefRange.Start,
161 "More template args specified than expected");
Bob Wilson7248f862009-11-22 04:24:42 +0000162
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000163 // Loop over all of the template arguments, setting them to the specified
164 // value or leaving them as the default if necessary.
165 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
166 if (i < SubClass.TemplateArgs.size()) {
167 // If a value is specified for this template arg, set it now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000168 if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
169 std::vector<unsigned>(), SubClass.TemplateArgs[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000170 return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000171
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000172 // Resolve it next.
173 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson7248f862009-11-22 04:24:42 +0000174
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000175 // Now remove it.
176 CurRec->removeValue(TArgs[i]);
177
178 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000179 return Error(SubClass.RefRange.Start,
180 "Value not specified for template argument #"
David Greenedb10e692011-10-19 13:02:42 +0000181 + utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
182 + ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000183 }
184 }
185
186 // Since everything went well, we can now set the "superclass" list for the
187 // current record.
188 const std::vector<Record*> &SCs = SC->getSuperClasses();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000189 ArrayRef<SMRange> SCRanges = SC->getSuperClassRanges();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000190 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
191 if (CurRec->isSubClassOf(SCs[i]))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000192 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000193 "Already subclass of '" + SCs[i]->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000194 CurRec->addSuperClass(SCs[i], SCRanges[i]);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000195 }
Bob Wilson7248f862009-11-22 04:24:42 +0000196
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000197 if (CurRec->isSubClassOf(SC))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000198 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000199 "Already subclass of '" + SC->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000200 CurRec->addSuperClass(SC, SubClass.RefRange);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000201 return false;
202}
203
David Greene753ed8f2009-04-22 16:42:54 +0000204/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilsonf71e6562009-04-30 18:26:19 +0000205/// CurMC, resolving its template args as SubMultiClass's
David Greene753ed8f2009-04-22 16:42:54 +0000206/// template arguments.
Bob Wilsonf71e6562009-04-30 18:26:19 +0000207bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson92ab8202009-04-30 17:46:20 +0000208 SubMultiClassReference &SubMultiClass) {
David Greene753ed8f2009-04-22 16:42:54 +0000209 MultiClass *SMC = SubMultiClass.MC;
Bob Wilsonf71e6562009-04-30 18:26:19 +0000210 Record *CurRec = &CurMC->Rec;
David Greene753ed8f2009-04-22 16:42:54 +0000211
Bob Wilsonf71e6562009-04-30 18:26:19 +0000212 const std::vector<RecordVal> &MCVals = CurRec->getValues();
David Greene753ed8f2009-04-22 16:42:54 +0000213
214 // Add all of the values in the subclass into the current class.
215 const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues();
216 for (unsigned i = 0, e = SMCVals.size(); i != e; ++i)
Jordan Rosef12e8a92013-01-10 18:50:11 +0000217 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVals[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000218 return true;
219
Bob Wilsonf71e6562009-04-30 18:26:19 +0000220 int newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000221
David Greene753ed8f2009-04-22 16:42:54 +0000222 // Add all of the defs in the subclass into the current multiclass.
223 for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(),
224 iend = SMC->DefPrototypes.end();
225 i != iend;
226 ++i) {
227 // Clone the def and add it to the current multiclass
228 Record *NewDef = new Record(**i);
229
230 // Add all of the values in the superclass into the current def.
231 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
Jordan Rosef12e8a92013-01-10 18:50:11 +0000232 if (AddValue(NewDef, SubMultiClass.RefRange.Start, MCVals[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000233 return true;
234
Bob Wilsonf71e6562009-04-30 18:26:19 +0000235 CurMC->DefPrototypes.push_back(NewDef);
David Greene753ed8f2009-04-22 16:42:54 +0000236 }
Bob Wilson3d948162009-04-28 19:41:44 +0000237
David Greenedb10e692011-10-19 13:02:42 +0000238 const std::vector<Init *> &SMCTArgs = SMC->Rec.getTemplateArgs();
David Greene753ed8f2009-04-22 16:42:54 +0000239
David Greene7049e792009-04-24 16:55:41 +0000240 // Ensure that an appropriate number of template arguments are
241 // specified.
David Greene753ed8f2009-04-22 16:42:54 +0000242 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000243 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000244 "More template args specified than expected");
Bob Wilson3d948162009-04-28 19:41:44 +0000245
David Greene753ed8f2009-04-22 16:42:54 +0000246 // Loop over all of the template arguments, setting them to the specified
247 // value or leaving them as the default if necessary.
248 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
249 if (i < SubMultiClass.TemplateArgs.size()) {
David Greene7049e792009-04-24 16:55:41 +0000250 // If a value is specified for this template arg, set it in the
251 // superclass now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000252 if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson3d948162009-04-28 19:41:44 +0000253 std::vector<unsigned>(),
David Greene753ed8f2009-04-22 16:42:54 +0000254 SubMultiClass.TemplateArgs[i]))
255 return true;
256
257 // Resolve it next.
258 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson3d948162009-04-28 19:41:44 +0000259
David Greene753ed8f2009-04-22 16:42:54 +0000260 // Now remove it.
261 CurRec->removeValue(SMCTArgs[i]);
262
David Greene7049e792009-04-24 16:55:41 +0000263 // If a value is specified for this template arg, set it in the
264 // new defs now.
265 for (MultiClass::RecordVector::iterator j =
Bob Wilsonf71e6562009-04-30 18:26:19 +0000266 CurMC->DefPrototypes.begin() + newDefStart,
267 jend = CurMC->DefPrototypes.end();
David Greene753ed8f2009-04-22 16:42:54 +0000268 j != jend;
269 ++j) {
270 Record *Def = *j;
271
Jordan Rosef12e8a92013-01-10 18:50:11 +0000272 if (SetValue(Def, SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson3d948162009-04-28 19:41:44 +0000273 std::vector<unsigned>(),
David Greene753ed8f2009-04-22 16:42:54 +0000274 SubMultiClass.TemplateArgs[i]))
275 return true;
276
277 // Resolve it next.
278 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
279
280 // Now remove it
281 Def->removeValue(SMCTArgs[i]);
282 }
283 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000284 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000285 "Value not specified for template argument #"
David Greenedb10e692011-10-19 13:02:42 +0000286 + utostr(i) + " (" + SMCTArgs[i]->getAsUnquotedString()
287 + ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000288 }
289 }
290
291 return false;
292}
293
David Greenefb927af2012-02-22 16:09:41 +0000294/// ProcessForeachDefs - Given a record, apply all of the variable
295/// values in all surrounding foreach loops, creating new records for
296/// each combination of values.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000297bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
298 if (Loops.empty())
299 return false;
300
David Greenefb927af2012-02-22 16:09:41 +0000301 // We want to instantiate a new copy of CurRec for each combination
302 // of nested loop iterator values. We don't want top instantiate
303 // any copies until we have values for each loop iterator.
304 IterSet IterVals;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000305 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000306}
307
308/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
309/// apply each of the variable values in this loop and then process
310/// subloops.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000311bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
312 // Recursively build a tuple of iterator values.
313 if (IterVals.size() != Loops.size()) {
314 assert(IterVals.size() < Loops.size());
315 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silvafb509ed2012-10-10 20:24:43 +0000316 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Craig Topper011817a2014-04-09 04:50:04 +0000317 if (!List) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000318 Error(Loc, "Loop list is not a list");
319 return true;
320 }
David Greenefb927af2012-02-22 16:09:41 +0000321
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000322 // Process each value.
323 for (int64_t i = 0; i < List->getSize(); ++i) {
Craig Topper011817a2014-04-09 04:50:04 +0000324 Init *ItemVal = List->resolveListElementReference(*CurRec, nullptr, i);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000325 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
326 if (ProcessForeachDefs(CurRec, Loc, IterVals))
327 return true;
328 IterVals.pop_back();
329 }
330 return false;
331 }
332
333 // This is the bottom of the recursion. We have all of the iterator values
334 // for this point in the iteration space. Instantiate a new record to
335 // reflect this combination of values.
336 Record *IterRec = new Record(*CurRec);
337
338 // Set the iterator values now.
339 for (unsigned i = 0, e = IterVals.size(); i != e; ++i) {
340 VarInit *IterVar = IterVals[i].IterVar;
Sean Silvafb509ed2012-10-10 20:24:43 +0000341 TypedInit *IVal = dyn_cast<TypedInit>(IterVals[i].IterValue);
Craig Topper011817a2014-04-09 04:50:04 +0000342 if (!IVal) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000343 Error(Loc, "foreach iterator value is untyped");
344 return true;
345 }
346
347 IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false));
348
349 if (SetValue(IterRec, Loc, IterVar->getName(),
350 std::vector<unsigned>(), IVal)) {
351 Error(Loc, "when instantiating this def");
352 return true;
353 }
354
355 // Resolve it next.
356 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName()));
357
358 // Remove it.
359 IterRec->removeValue(IterVar->getName());
360 }
361
362 if (Records.getDef(IterRec->getNameInitAsString())) {
363 Error(Loc, "def already exists: " + IterRec->getNameInitAsString());
David Greenefb927af2012-02-22 16:09:41 +0000364 return true;
365 }
366
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000367 Records.addDef(IterRec);
368 IterRec->resolveReferences();
David Greenefb927af2012-02-22 16:09:41 +0000369 return false;
370}
371
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000372//===----------------------------------------------------------------------===//
373// Parser Code
374//===----------------------------------------------------------------------===//
375
376/// isObjectStart - Return true if this is a valid first token for an Object.
377static bool isObjectStart(tgtok::TokKind K) {
378 return K == tgtok::Class || K == tgtok::Def ||
David Greenefb927af2012-02-22 16:09:41 +0000379 K == tgtok::Defm || K == tgtok::Let ||
380 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000381}
382
Alp Tokerce91fe52013-12-21 18:51:00 +0000383/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
384/// an identifier.
385std::string TGParser::GetNewAnonymousName() {
Michael J. Spencer2bb7db92013-02-26 21:29:47 +0000386 unsigned Tmp = AnonCounter++; // MSVC2012 ICEs without this.
Alp Tokerce91fe52013-12-21 18:51:00 +0000387 return "anonymous_" + utostr(Tmp);
Chris Lattner7538ed82010-10-05 22:51:56 +0000388}
389
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000390/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000391/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000392/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000393/// ObjectName ::= /*empty*/
394///
David Greene2affd672011-10-19 13:04:29 +0000395Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
396 switch (Lex.getCode()) {
397 case tgtok::colon:
398 case tgtok::semi:
399 case tgtok::l_brace:
400 // These are all of the tokens that can begin an object body.
401 // Some of these can also begin values but we disallow those cases
402 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000403 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000404 default:
405 break;
406 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000407
Craig Topper011817a2014-04-09 04:50:04 +0000408 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000409 if (CurMultiClass)
410 CurRec = &CurMultiClass->Rec;
411
Craig Topper011817a2014-04-09 04:50:04 +0000412 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000413 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000414 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000415 if (!CurRecName) {
416 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000417 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000418 }
419 Type = CurRecName->getType();
420 }
421
422 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000423}
424
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000425/// ParseClassID - Parse and resolve a reference to a class name. This returns
426/// null on error.
427///
428/// ClassID ::= ID
429///
430Record *TGParser::ParseClassID() {
431 if (Lex.getCode() != tgtok::Id) {
432 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000433 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000434 }
Bob Wilson7248f862009-11-22 04:24:42 +0000435
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000436 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000437 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000438 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000439
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000440 Lex.Lex();
441 return Result;
442}
443
Bob Wilson3d948162009-04-28 19:41:44 +0000444/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
445/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000446///
447/// MultiClassID ::= ID
448///
449MultiClass *TGParser::ParseMultiClassID() {
450 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000451 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000452 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000453 }
Bob Wilson3d948162009-04-28 19:41:44 +0000454
David Greene753ed8f2009-04-22 16:42:54 +0000455 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
Craig Topper011817a2014-04-09 04:50:04 +0000456 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000457 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000458
David Greene753ed8f2009-04-22 16:42:54 +0000459 Lex.Lex();
460 return Result;
461}
462
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000463/// ParseSubClassReference - Parse a reference to a subclass or to a templated
464/// subclass. This returns a SubClassRefTy with a null Record* on error.
465///
466/// SubClassRef ::= ClassID
467/// SubClassRef ::= ClassID '<' ValueList '>'
468///
469SubClassReference TGParser::
470ParseSubClassReference(Record *CurRec, bool isDefm) {
471 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000472 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000473
Sean Silva0657b402013-01-09 02:17:14 +0000474 if (isDefm) {
475 if (MultiClass *MC = ParseMultiClassID())
476 Result.Rec = &MC->Rec;
477 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000478 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000479 }
Craig Topper011817a2014-04-09 04:50:04 +0000480 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000481
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000482 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000483 if (Lex.getCode() != tgtok::less) {
484 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000485 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000486 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000487 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000488
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000489 if (Lex.getCode() == tgtok::greater) {
490 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000491 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000492 return Result;
493 }
Bob Wilson7248f862009-11-22 04:24:42 +0000494
David Greene8618f952009-06-08 20:23:18 +0000495 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000496 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000497 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000498 return Result;
499 }
Bob Wilson7248f862009-11-22 04:24:42 +0000500
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000501 if (Lex.getCode() != tgtok::greater) {
502 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000503 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000504 return Result;
505 }
506 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000507 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000508
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000509 return Result;
510}
511
Bob Wilson3d948162009-04-28 19:41:44 +0000512/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
513/// templated submulticlass. This returns a SubMultiClassRefTy with a null
514/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000515///
516/// SubMultiClassRef ::= MultiClassID
517/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
518///
519SubMultiClassReference TGParser::
520ParseSubMultiClassReference(MultiClass *CurMC) {
521 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000522 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000523
David Greene753ed8f2009-04-22 16:42:54 +0000524 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000525 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000526
David Greene753ed8f2009-04-22 16:42:54 +0000527 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000528 if (Lex.getCode() != tgtok::less) {
529 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000530 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000531 }
David Greene753ed8f2009-04-22 16:42:54 +0000532 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000533
David Greene753ed8f2009-04-22 16:42:54 +0000534 if (Lex.getCode() == tgtok::greater) {
535 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000536 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000537 return Result;
538 }
Bob Wilson3d948162009-04-28 19:41:44 +0000539
David Greene8618f952009-06-08 20:23:18 +0000540 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000541 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000542 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000543 return Result;
544 }
Bob Wilson3d948162009-04-28 19:41:44 +0000545
David Greene753ed8f2009-04-22 16:42:54 +0000546 if (Lex.getCode() != tgtok::greater) {
547 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000548 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000549 return Result;
550 }
551 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000552 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000553
554 return Result;
555}
556
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000557/// ParseRangePiece - Parse a bit/value range.
558/// RangePiece ::= INTVAL
559/// RangePiece ::= INTVAL '-' INTVAL
560/// RangePiece ::= INTVAL INTVAL
561bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000562 if (Lex.getCode() != tgtok::IntVal) {
563 TokError("expected integer or bitrange");
564 return true;
565 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000566 int64_t Start = Lex.getCurIntVal();
567 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000568
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000569 if (Start < 0)
570 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000571
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000572 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000573 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000574 Ranges.push_back(Start);
575 return false;
576 case tgtok::minus:
577 if (Lex.Lex() != tgtok::IntVal) {
578 TokError("expected integer value as end of range");
579 return true;
580 }
581 End = Lex.getCurIntVal();
582 break;
583 case tgtok::IntVal:
584 End = -Lex.getCurIntVal();
585 break;
586 }
Bob Wilson7248f862009-11-22 04:24:42 +0000587 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000588 return TokError("invalid range, cannot be negative");
589 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000590
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000591 // Add to the range.
592 if (Start < End) {
593 for (; Start <= End; ++Start)
594 Ranges.push_back(Start);
595 } else {
596 for (; Start >= End; --Start)
597 Ranges.push_back(Start);
598 }
599 return false;
600}
601
602/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
603///
604/// RangeList ::= RangePiece (',' RangePiece)*
605///
606std::vector<unsigned> TGParser::ParseRangeList() {
607 std::vector<unsigned> Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000608
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000609 // Parse the first piece.
610 if (ParseRangePiece(Result))
611 return std::vector<unsigned>();
612 while (Lex.getCode() == tgtok::comma) {
613 Lex.Lex(); // Eat the comma.
614
615 // Parse the next range piece.
616 if (ParseRangePiece(Result))
617 return std::vector<unsigned>();
618 }
619 return Result;
620}
621
622/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
623/// OptionalRangeList ::= '<' RangeList '>'
624/// OptionalRangeList ::= /*empty*/
625bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
626 if (Lex.getCode() != tgtok::less)
627 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000628
Chris Lattner526c8cb2009-06-21 03:39:35 +0000629 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000630 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000631
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000632 // Parse the range list.
633 Ranges = ParseRangeList();
634 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000635
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000636 if (Lex.getCode() != tgtok::greater) {
637 TokError("expected '>' at end of range list");
638 return Error(StartLoc, "to match this '<'");
639 }
640 Lex.Lex(); // eat the '>'.
641 return false;
642}
643
644/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
645/// OptionalBitList ::= '{' RangeList '}'
646/// OptionalBitList ::= /*empty*/
647bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
648 if (Lex.getCode() != tgtok::l_brace)
649 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000650
Chris Lattner526c8cb2009-06-21 03:39:35 +0000651 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000652 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000653
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000654 // Parse the range list.
655 Ranges = ParseRangeList();
656 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000657
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000658 if (Lex.getCode() != tgtok::r_brace) {
659 TokError("expected '}' at end of bit list");
660 return Error(StartLoc, "to match this '{'");
661 }
662 Lex.Lex(); // eat the '}'.
663 return false;
664}
665
666
667/// ParseType - Parse and return a tblgen type. This returns null on error.
668///
669/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000670/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000671/// Type ::= BIT // bit type
672/// Type ::= BITS '<' INTVAL '>' // bits<x> type
673/// Type ::= INT // int type
674/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000675/// Type ::= DAG // dag type
676/// Type ::= ClassID // Record Type
677///
678RecTy *TGParser::ParseType() {
679 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000680 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000681 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000682 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000683 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
684 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000685 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000686 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000687 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000688 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000689 case tgtok::Bits: {
690 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
691 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000692 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000693 }
694 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
695 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000696 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000697 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000698 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000699 if (Lex.Lex() != tgtok::greater) { // Eat count.
700 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000701 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000702 }
703 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000704 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000705 }
706 case tgtok::List: {
707 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
708 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000709 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000710 }
711 Lex.Lex(); // Eat '<'
712 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000713 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000714
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000715 if (Lex.getCode() != tgtok::greater) {
716 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000717 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000718 }
719 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000720 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000721 }
Bob Wilson7248f862009-11-22 04:24:42 +0000722 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000723}
724
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000725/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
726/// has already been read.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000727Init *TGParser::ParseIDValue(Record *CurRec,
David Greened4263a62011-10-19 13:04:20 +0000728 const std::string &Name, SMLoc NameLoc,
729 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000730 if (CurRec) {
731 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000732 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000733
David Greenedb10e692011-10-19 13:02:42 +0000734 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
735
David Greene47a665e2011-10-05 22:42:54 +0000736 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +0000737 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
738 "::");
David Greene47a665e2011-10-05 22:42:54 +0000739
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000740 if (CurRec->isTemplateArg(TemplateArgName)) {
741 const RecordVal *RV = CurRec->getValue(TemplateArgName);
742 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000743 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000744 }
745 }
Bob Wilson7248f862009-11-22 04:24:42 +0000746
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000747 if (CurMultiClass) {
David Greenedb10e692011-10-19 13:02:42 +0000748 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
749 "::");
750
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000751 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
752 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
753 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000754 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000755 }
756 }
Bob Wilson7248f862009-11-22 04:24:42 +0000757
David Greenefb927af2012-02-22 16:09:41 +0000758 // If this is in a foreach loop, make sure it's not a loop iterator
759 for (LoopVector::iterator i = Loops.begin(), iend = Loops.end();
760 i != iend;
761 ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000762 VarInit *IterVar = dyn_cast<VarInit>(i->IterVar);
David Greenefb927af2012-02-22 16:09:41 +0000763 if (IterVar && IterVar->getName() == Name)
764 return IterVar;
765 }
766
David Greene232bd602011-10-19 13:04:21 +0000767 if (Mode == ParseNameMode)
768 return StringInit::get(Name);
769
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000770 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000771 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000772
David Greene232bd602011-10-19 13:04:21 +0000773 if (Mode == ParseValueMode) {
774 Error(NameLoc, "Variable not defined: '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000775 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000776 }
777
778 return StringInit::get(Name);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000779}
780
David Greene5d0c0512009-05-14 20:54:48 +0000781/// ParseOperation - Parse an operator. This returns null on error.
782///
783/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
784///
David Greeneaf8ee2c2011-07-29 22:43:06 +0000785Init *TGParser::ParseOperation(Record *CurRec) {
David Greene5d0c0512009-05-14 20:54:48 +0000786 switch (Lex.getCode()) {
787 default:
788 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000789 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000790 case tgtok::XHead:
791 case tgtok::XTail:
792 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000793 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
794 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000795 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000796
David Greenee8f3b272009-05-14 21:22:49 +0000797 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000798 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000799 case tgtok::XCast:
800 Lex.Lex(); // eat the operation
801 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000802
David Greenee8f3b272009-05-14 21:22:49 +0000803 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000804
Craig Topper011817a2014-04-09 04:50:04 +0000805 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000806 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000807 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000808 }
David Greene5d0c0512009-05-14 20:54:48 +0000809
David Greenee8f3b272009-05-14 21:22:49 +0000810 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000811 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000812 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000813 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000814 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000815 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000816 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000817 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000818 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000819 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000820 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000821 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000822 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000823 break;
David Greenee8f3b272009-05-14 21:22:49 +0000824 }
825 if (Lex.getCode() != tgtok::l_paren) {
826 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000827 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000828 }
829 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000830
David Greeneaf8ee2c2011-07-29 22:43:06 +0000831 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000832 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000833
David Greene2f7cf7f2011-01-07 17:05:37 +0000834 if (Code == UnOpInit::HEAD
835 || Code == UnOpInit::TAIL
836 || Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000837 ListInit *LHSl = dyn_cast<ListInit>(LHS);
838 StringInit *LHSs = dyn_cast<StringInit>(LHS);
839 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000840 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000841 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000842 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000843 }
844 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000845 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
846 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000847 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000848 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000849 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000850 }
851 }
852
David Greene2f7cf7f2011-01-07 17:05:37 +0000853 if (Code == UnOpInit::HEAD
854 || Code == UnOpInit::TAIL) {
Craig Topper011817a2014-04-09 04:50:04 +0000855 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000856 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000857 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000858 }
Bob Wilson7248f862009-11-22 04:24:42 +0000859
David Greened571b3c2009-05-14 22:38:31 +0000860 if (LHSl && LHSl->getSize() == 0) {
861 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000862 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000863 }
864 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000865 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000866 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000867 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000868 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000869 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000870 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000871 if (Code == UnOpInit::HEAD) {
David Greened571b3c2009-05-14 22:38:31 +0000872 Type = Itemt->getType();
Bob Wilson7248f862009-11-22 04:24:42 +0000873 } else {
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000874 Type = ListRecTy::get(Itemt->getType());
David Greened571b3c2009-05-14 22:38:31 +0000875 }
Bob Wilson7248f862009-11-22 04:24:42 +0000876 } else {
David Greened571b3c2009-05-14 22:38:31 +0000877 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000878 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000879 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000880 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000881 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000882 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000883 if (Code == UnOpInit::HEAD) {
David Greened571b3c2009-05-14 22:38:31 +0000884 Type = LType->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +0000885 } else {
David Greened571b3c2009-05-14 22:38:31 +0000886 Type = LType;
887 }
888 }
889 }
890 }
891
David Greenee8f3b272009-05-14 21:22:49 +0000892 if (Lex.getCode() != tgtok::r_paren) {
893 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000894 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000895 }
896 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000897 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000898 }
David Greene5d0c0512009-05-14 20:54:48 +0000899
900 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000901 case tgtok::XADD:
Bob Wilson7248f862009-11-22 04:24:42 +0000902 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000903 case tgtok::XSRL:
904 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000905 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000906 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000907 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000908 tgtok::TokKind OpTok = Lex.getCode();
909 SMLoc OpLoc = Lex.getLoc();
910 Lex.Lex(); // eat the operation
911
David Greene5d0c0512009-05-14 20:54:48 +0000912 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000913 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000914
Chris Lattner61ea00b2010-10-05 23:58:18 +0000915 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000916 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000917 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000918 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000919 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
920 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
921 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
922 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000923 case tgtok::XListConcat:
924 Code = BinOpInit::LISTCONCAT;
925 // We don't know the list type until we parse the first argument
926 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000927 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000928 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000929 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000930 break;
David Greene5d0c0512009-05-14 20:54:48 +0000931 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000932
David Greene5d0c0512009-05-14 20:54:48 +0000933 if (Lex.getCode() != tgtok::l_paren) {
934 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000935 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000936 }
937 Lex.Lex(); // eat the '('
938
David Greeneaf8ee2c2011-07-29 22:43:06 +0000939 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000940
Chris Lattner61ea00b2010-10-05 23:58:18 +0000941 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000942 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000943
Chris Lattner61ea00b2010-10-05 23:58:18 +0000944 while (Lex.getCode() == tgtok::comma) {
945 Lex.Lex(); // eat the ','
946
947 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000948 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000949 }
David Greene5d0c0512009-05-14 20:54:48 +0000950
951 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000952 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000953 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000954 }
955 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000956
Daniel Sanders314e80e2014-05-07 10:13:19 +0000957 // If we are doing !listconcat, we should know the type by now
958 if (OpTok == tgtok::XListConcat) {
959 if (VarInit *Arg0 = dyn_cast<VarInit>(InitList[0]))
960 Type = Arg0->getType();
961 else if (ListInit *Arg0 = dyn_cast<ListInit>(InitList[0]))
962 Type = Arg0->getType();
963 else {
964 InitList[0]->dump();
965 Error(OpLoc, "expected a list");
966 return nullptr;
967 }
968 }
969
Chris Lattner61ea00b2010-10-05 23:58:18 +0000970 // We allow multiple operands to associative operators like !strconcat as
971 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000972 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000973 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000974 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000975 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
976 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000977 InitList.back() = RHS;
978 }
979 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000980
Chris Lattner61ea00b2010-10-05 23:58:18 +0000981 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000982 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000983 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000984
Chris Lattner61ea00b2010-10-05 23:58:18 +0000985 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +0000986 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000987 }
988
David Greene3587eed2009-05-14 23:26:46 +0000989 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +0000990 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +0000991 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
992 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000993 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000994
David Greene98ed3c72009-05-14 21:54:42 +0000995 tgtok::TokKind LexCode = Lex.getCode();
996 Lex.Lex(); // eat the operation
997 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +0000998 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +0000999 case tgtok::XIf:
1000 Code = TernOpInit::IF;
1001 break;
David Greenee917fff2009-05-14 22:23:47 +00001002 case tgtok::XForEach:
1003 Code = TernOpInit::FOREACH;
1004 break;
David Greene98ed3c72009-05-14 21:54:42 +00001005 case tgtok::XSubst:
1006 Code = TernOpInit::SUBST;
1007 break;
1008 }
1009 if (Lex.getCode() != tgtok::l_paren) {
1010 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001011 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001012 }
1013 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001014
David Greeneaf8ee2c2011-07-29 22:43:06 +00001015 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001016 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001017
David Greene98ed3c72009-05-14 21:54:42 +00001018 if (Lex.getCode() != tgtok::comma) {
1019 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001020 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001021 }
1022 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001023
David Greeneaf8ee2c2011-07-29 22:43:06 +00001024 Init *MHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001025 if (!MHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001026
David Greene98ed3c72009-05-14 21:54:42 +00001027 if (Lex.getCode() != tgtok::comma) {
1028 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001029 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001030 }
1031 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001032
David Greeneaf8ee2c2011-07-29 22:43:06 +00001033 Init *RHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001034 if (!RHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001035
David Greene98ed3c72009-05-14 21:54:42 +00001036 if (Lex.getCode() != tgtok::r_paren) {
1037 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001038 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001039 }
1040 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001041
David Greene98ed3c72009-05-14 21:54:42 +00001042 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001043 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001044 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001045 RecTy *MHSTy = nullptr;
1046 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001047
Sean Silvafb509ed2012-10-10 20:24:43 +00001048 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001049 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001050 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001051 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001052 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001053 MHSTy = BitRecTy::get();
1054
Sean Silvafb509ed2012-10-10 20:24:43 +00001055 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001056 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001057 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001058 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001059 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001060 RHSTy = BitRecTy::get();
1061
1062 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001063 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001064 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001065 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001066 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001067
1068 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001069 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001070 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001071 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001072
1073 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1074 Type = RHSTy;
1075 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1076 Type = MHSTy;
Bob Wilson7248f862009-11-22 04:24:42 +00001077 } else {
David Greene3587eed2009-05-14 23:26:46 +00001078 TokError("inconsistent types for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001079 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001080 }
1081 break;
1082 }
David Greenee917fff2009-05-14 22:23:47 +00001083 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001084 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
Craig Topper011817a2014-04-09 04:50:04 +00001085 if (!MHSt) {
David Greenee917fff2009-05-14 22:23:47 +00001086 TokError("could not get type for !foreach");
Craig Topper011817a2014-04-09 04:50:04 +00001087 return nullptr;
David Greenee917fff2009-05-14 22:23:47 +00001088 }
1089 Type = MHSt->getType();
1090 break;
1091 }
David Greene98ed3c72009-05-14 21:54:42 +00001092 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001093 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001094 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001095 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001096 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001097 }
1098 Type = RHSt->getType();
1099 break;
1100 }
1101 }
David Greenee32ebf22011-07-29 19:07:07 +00001102 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001103 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001104 }
David Greene5d0c0512009-05-14 20:54:48 +00001105 }
David Greene5d0c0512009-05-14 20:54:48 +00001106}
1107
1108/// ParseOperatorType - Parse a type for an operator. This returns
1109/// null on error.
1110///
1111/// OperatorType ::= '<' Type '>'
1112///
Dan Gohman1432ef82009-08-12 22:10:57 +00001113RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001114 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001115
1116 if (Lex.getCode() != tgtok::less) {
1117 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001118 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001119 }
1120 Lex.Lex(); // eat the <
1121
1122 Type = ParseType();
1123
Craig Topper011817a2014-04-09 04:50:04 +00001124 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001125 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001126 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001127 }
1128
1129 if (Lex.getCode() != tgtok::greater) {
1130 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001131 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001132 }
1133 Lex.Lex(); // eat the >
1134
1135 return Type;
1136}
1137
1138
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001139/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1140///
1141/// SimpleValue ::= IDValue
1142/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001143/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001144/// SimpleValue ::= CODEFRAGMENT
1145/// SimpleValue ::= '?'
1146/// SimpleValue ::= '{' ValueList '}'
1147/// SimpleValue ::= ID '<' ValueListNE '>'
1148/// SimpleValue ::= '[' ValueList ']'
1149/// SimpleValue ::= '(' IDValue DagArgList ')'
1150/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001151/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001152/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1153/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1154/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001155/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001156/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1157///
David Greened4263a62011-10-19 13:04:20 +00001158Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1159 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001160 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001161 switch (Lex.getCode()) {
1162 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001163 case tgtok::paste:
1164 // This is a leading paste operation. This is deprecated but
1165 // still exists in some .td files. Ignore it.
1166 Lex.Lex(); // Skip '#'.
1167 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001168 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001169 case tgtok::StrVal: {
1170 std::string Val = Lex.getCurStrVal();
1171 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001172
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001173 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001174 while (Lex.getCode() == tgtok::StrVal) {
1175 Val += Lex.getCurStrVal();
1176 Lex.Lex();
1177 }
Bob Wilson7248f862009-11-22 04:24:42 +00001178
David Greenee32ebf22011-07-29 19:07:07 +00001179 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001180 break;
1181 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001182 case tgtok::CodeFragment:
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +00001183 R = StringInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001184 Lex.Lex();
1185 break;
1186 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001187 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001188 Lex.Lex();
1189 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001190 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001191 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001192 std::string Name = Lex.getCurStrVal();
1193 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001194 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001195
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001196 // Value ::= ID '<' ValueListNE '>'
1197 if (Lex.Lex() == tgtok::greater) {
1198 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001199 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001200 }
David Greene8618f952009-06-08 20:23:18 +00001201
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001202 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1203 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1204 // body.
1205 Record *Class = Records.getClass(Name);
1206 if (!Class) {
1207 Error(NameLoc, "Expected a class name, got '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001208 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001209 }
David Greene8618f952009-06-08 20:23:18 +00001210
David Greeneaf8ee2c2011-07-29 22:43:06 +00001211 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
Craig Topper011817a2014-04-09 04:50:04 +00001212 if (ValueList.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001213
David Greene8618f952009-06-08 20:23:18 +00001214 if (Lex.getCode() != tgtok::greater) {
1215 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001216 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001217 }
1218 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001219 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001220
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001221 // Create the new record, set it as CurRec temporarily.
Alp Tokerce91fe52013-12-21 18:51:00 +00001222 Record *NewRec = new Record(GetNewAnonymousName(), NameLoc, Records,
Jordan Roseabdd99b2013-01-10 18:50:05 +00001223 /*IsAnonymous=*/true);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001224 SubClassReference SCRef;
Jordan Rosef12e8a92013-01-10 18:50:11 +00001225 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001226 SCRef.Rec = Class;
1227 SCRef.TemplateArgs = ValueList;
1228 // Add info about the subclass to NewRec.
1229 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001230 return nullptr;
Hal Finkela8c1f462014-01-02 20:47:09 +00001231 if (!CurMultiClass) {
1232 NewRec->resolveReferences();
1233 Records.addDef(NewRec);
1234 } else {
1235 // Otherwise, we're inside a multiclass, add it to the multiclass.
1236 CurMultiClass->DefPrototypes.push_back(NewRec);
1237
1238 // Copy the template arguments for the multiclass into the def.
1239 const std::vector<Init *> &TArgs =
1240 CurMultiClass->Rec.getTemplateArgs();
1241
1242 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1243 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1244 assert(RV && "Template arg doesn't exist?");
1245 NewRec->addValue(*RV);
1246 }
1247
1248 // We can't return the prototype def here, instead return:
1249 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1250 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1251 assert(MCNameRV && "multiclass record must have a NAME");
1252
1253 return UnOpInit::get(UnOpInit::CAST,
1254 BinOpInit::get(BinOpInit::STRCONCAT,
1255 VarInit::get(MCNameRV->getName(),
1256 MCNameRV->getType()),
1257 NewRec->getNameInit(),
1258 StringRecTy::get()),
1259 Class->getDefInit()->getType());
1260 }
Bob Wilson7248f862009-11-22 04:24:42 +00001261
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001262 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001263 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001264 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001265 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001266 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001267 Lex.Lex(); // eat the '{'
David Greeneaf8ee2c2011-07-29 22:43:06 +00001268 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001269
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001270 if (Lex.getCode() != tgtok::r_brace) {
1271 Vals = ParseValueList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001272 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001273 }
1274 if (Lex.getCode() != tgtok::r_brace) {
1275 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001276 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001277 }
1278 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001279
David Greeneaf8ee2c2011-07-29 22:43:06 +00001280 SmallVector<Init *, 16> NewBits(Vals.size());
David Greeneb3da8122011-07-29 19:07:00 +00001281
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001282 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001283 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001284 if (!Bit) {
Chris Lattner52416952007-11-22 21:06:59 +00001285 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1286 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001287 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001288 }
David Greeneb3da8122011-07-29 19:07:00 +00001289 NewBits[Vals.size()-i-1] = Bit;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001290 }
David Greenee32ebf22011-07-29 19:07:07 +00001291 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001292 }
1293 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1294 Lex.Lex(); // eat the '['
David Greeneaf8ee2c2011-07-29 22:43:06 +00001295 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001296
Craig Topper011817a2014-04-09 04:50:04 +00001297 RecTy *DeducedEltTy = nullptr;
1298 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001299
Craig Topper011817a2014-04-09 04:50:04 +00001300 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001301 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001302 if (!ListType) {
Reid Klecknerd78273f2013-08-06 22:51:21 +00001303 std::string s;
1304 raw_string_ostream ss(s);
1305 ss << "Type mismatch for list, expected list type, got "
1306 << ItemType->getAsString();
1307 TokError(ss.str());
Craig Topper011817a2014-04-09 04:50:04 +00001308 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001309 }
1310 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001311 }
David Greene8618f952009-06-08 20:23:18 +00001312
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001313 if (Lex.getCode() != tgtok::r_square) {
Craig Topper011817a2014-04-09 04:50:04 +00001314 Vals = ParseValueList(CurRec, nullptr,
1315 GivenListTy ? GivenListTy->getElementType() : nullptr);
1316 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001317 }
1318 if (Lex.getCode() != tgtok::r_square) {
1319 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001320 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001321 }
1322 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001323
Craig Topper011817a2014-04-09 04:50:04 +00001324 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001325 if (Lex.getCode() == tgtok::less) {
1326 // Optional list element type
1327 Lex.Lex(); // eat the '<'
1328
1329 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001330 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001331 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001332 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001333 }
1334
1335 if (Lex.getCode() != tgtok::greater) {
1336 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001337 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001338 }
1339 Lex.Lex(); // eat the '>'
1340 }
1341
1342 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001343 RecTy *EltTy = nullptr;
David Greeneaf8ee2c2011-07-29 22:43:06 +00001344 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greene8618f952009-06-08 20:23:18 +00001345 i != ie;
1346 ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +00001347 TypedInit *TArg = dyn_cast<TypedInit>(*i);
Craig Topper011817a2014-04-09 04:50:04 +00001348 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001349 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001350 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001351 }
Craig Topper011817a2014-04-09 04:50:04 +00001352 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001353 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001354 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001355 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001356 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001357 }
Bob Wilson7248f862009-11-22 04:24:42 +00001358 } else {
David Greene8618f952009-06-08 20:23:18 +00001359 EltTy = TArg->getType();
1360 }
1361 }
1362
Craig Topper011817a2014-04-09 04:50:04 +00001363 if (GivenEltTy) {
1364 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001365 // Verify consistency
1366 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1367 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001368 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001369 }
1370 }
1371 EltTy = GivenEltTy;
1372 }
1373
Craig Topper011817a2014-04-09 04:50:04 +00001374 if (!EltTy) {
1375 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001376 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001377 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001378 }
1379 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001380 } else {
David Greene8618f952009-06-08 20:23:18 +00001381 // Make sure the deduced type is compatible with the given type
1382 if (GivenListTy) {
1383 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1384 TokError("Element type mismatch for list");
Craig Topper011817a2014-04-09 04:50:04 +00001385 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001386 }
1387 }
1388 DeducedEltTy = EltTy;
1389 }
Bob Wilson7248f862009-11-22 04:24:42 +00001390
David Greenee32ebf22011-07-29 19:07:07 +00001391 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001392 }
1393 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1394 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001395 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001396 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001397 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001398 }
Bob Wilson7248f862009-11-22 04:24:42 +00001399
David Greeneaf8ee2c2011-07-29 22:43:06 +00001400 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001401 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001402
Nate Begemandbe3f772009-03-19 05:21:56 +00001403 // If the operator name is present, parse it.
1404 std::string OperatorName;
1405 if (Lex.getCode() == tgtok::colon) {
1406 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1407 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001408 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001409 }
1410 OperatorName = Lex.getCurStrVal();
1411 Lex.Lex(); // eat the VarName.
1412 }
Bob Wilson7248f862009-11-22 04:24:42 +00001413
David Greeneaf8ee2c2011-07-29 22:43:06 +00001414 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001415 if (Lex.getCode() != tgtok::r_paren) {
1416 DagArgs = ParseDagArgList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001417 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001418 }
Bob Wilson7248f862009-11-22 04:24:42 +00001419
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001420 if (Lex.getCode() != tgtok::r_paren) {
1421 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001422 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001423 }
1424 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001425
David Greenee32ebf22011-07-29 19:07:07 +00001426 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001427 }
Bob Wilson7248f862009-11-22 04:24:42 +00001428
David Greene2f7cf7f2011-01-07 17:05:37 +00001429 case tgtok::XHead:
1430 case tgtok::XTail:
1431 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001432 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001433 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001434 case tgtok::XADD:
Bob Wilson7248f862009-11-22 04:24:42 +00001435 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001436 case tgtok::XSRL:
1437 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001438 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001439 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001440 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001441 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001442 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001443 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greene5d0c0512009-05-14 20:54:48 +00001444 return ParseOperation(CurRec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001445 }
1446 }
Bob Wilson7248f862009-11-22 04:24:42 +00001447
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001448 return R;
1449}
1450
1451/// ParseValue - Parse a tblgen value. This returns null on error.
1452///
1453/// Value ::= SimpleValue ValueSuffix*
1454/// ValueSuffix ::= '{' BitList '}'
1455/// ValueSuffix ::= '[' BitList ']'
1456/// ValueSuffix ::= '.' ID
1457///
David Greened4263a62011-10-19 13:04:20 +00001458Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1459 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001460 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001461
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001462 // Parse the suffixes now if present.
1463 while (1) {
1464 switch (Lex.getCode()) {
1465 default: return Result;
1466 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001467 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001468 // This is the beginning of the object body.
1469 return Result;
1470
Chris Lattner526c8cb2009-06-21 03:39:35 +00001471 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001472 Lex.Lex(); // eat the '{'
1473 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001474 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001475
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001476 // Reverse the bitlist.
1477 std::reverse(Ranges.begin(), Ranges.end());
1478 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001479 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001480 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001481 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001482 }
Bob Wilson7248f862009-11-22 04:24:42 +00001483
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001484 // Eat the '}'.
1485 if (Lex.getCode() != tgtok::r_brace) {
1486 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001487 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001488 }
1489 Lex.Lex();
1490 break;
1491 }
1492 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001493 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001494 Lex.Lex(); // eat the '['
1495 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001496 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001497
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001498 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001499 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001500 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001501 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001502 }
Bob Wilson7248f862009-11-22 04:24:42 +00001503
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001504 // Eat the ']'.
1505 if (Lex.getCode() != tgtok::r_square) {
1506 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001507 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001508 }
1509 Lex.Lex();
1510 break;
1511 }
1512 case tgtok::period:
1513 if (Lex.Lex() != tgtok::Id) { // eat the .
1514 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001515 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001516 }
1517 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001518 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001519 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001520 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001521 }
David Greenee32ebf22011-07-29 19:07:07 +00001522 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001523 Lex.Lex(); // eat field name
1524 break;
David Greene8e85b482011-10-19 13:04:43 +00001525
1526 case tgtok::paste:
1527 SMLoc PasteLoc = Lex.getLoc();
1528
1529 // Create a !strconcat() operation, first casting each operand to
1530 // a string if necessary.
1531
Sean Silvafb509ed2012-10-10 20:24:43 +00001532 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001533 if (!LHS) {
1534 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001535 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001536 }
1537
1538 if (LHS->getType() != StringRecTy::get()) {
1539 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1540 }
1541
Craig Topper011817a2014-04-09 04:50:04 +00001542 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001543
1544 Lex.Lex(); // Eat the '#'.
1545 switch (Lex.getCode()) {
1546 case tgtok::colon:
1547 case tgtok::semi:
1548 case tgtok::l_brace:
1549 // These are all of the tokens that can begin an object body.
1550 // Some of these can also begin values but we disallow those cases
1551 // because they are unlikely to be useful.
1552
1553 // Trailing paste, concat with an empty string.
1554 RHS = StringInit::get("");
1555 break;
1556
1557 default:
1558 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001559 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001560 if (!RHS) {
1561 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001562 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001563 }
1564
1565 if (RHS->getType() != StringRecTy::get()) {
1566 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1567 }
1568
1569 break;
1570 }
1571
1572 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1573 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1574 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001575 }
1576 }
1577}
1578
1579/// ParseDagArgList - Parse the argument list for a dag literal expression.
1580///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001581/// DagArg ::= Value (':' VARNAME)?
1582/// DagArg ::= VARNAME
1583/// DagArgList ::= DagArg
1584/// DagArgList ::= DagArgList ',' DagArg
David Greeneaf8ee2c2011-07-29 22:43:06 +00001585std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001586TGParser::ParseDagArgList(Record *CurRec) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001587 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson7248f862009-11-22 04:24:42 +00001588
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001589 while (1) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001590 // DagArg ::= VARNAME
1591 if (Lex.getCode() == tgtok::VarName) {
1592 // A missing value is treated like '?'.
1593 Result.push_back(std::make_pair(UnsetInit::get(), Lex.getCurStrVal()));
1594 Lex.Lex();
1595 } else {
1596 // DagArg ::= Value (':' VARNAME)?
1597 Init *Val = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001598 if (!Val)
David Greeneaf8ee2c2011-07-29 22:43:06 +00001599 return std::vector<std::pair<llvm::Init*, std::string> >();
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001600
1601 // If the variable name is present, add it.
1602 std::string VarName;
1603 if (Lex.getCode() == tgtok::colon) {
1604 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1605 TokError("expected variable name in dag literal");
1606 return std::vector<std::pair<llvm::Init*, std::string> >();
1607 }
1608 VarName = Lex.getCurStrVal();
1609 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001610 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001611
1612 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001613 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001614 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001615 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001616 }
Bob Wilson7248f862009-11-22 04:24:42 +00001617
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001618 return Result;
1619}
1620
1621
1622/// ParseValueList - Parse a comma separated list of values, returning them as a
1623/// vector. Note that this always expects to be able to parse at least one
1624/// value. It returns an empty list if this is not possible.
1625///
1626/// ValueList ::= Value (',' Value)
1627///
David Greeneaf8ee2c2011-07-29 22:43:06 +00001628std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopher71520a82011-07-11 23:06:52 +00001629 RecTy *EltTy) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001630 std::vector<Init*> Result;
David Greene8618f952009-06-08 20:23:18 +00001631 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001632 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001633 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001634 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001635 if (!TArgs.size()) {
1636 TokError("template argument provided to non-template class");
1637 return std::vector<Init*>();
1638 }
David Greene8618f952009-06-08 20:23:18 +00001639 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001640 if (!RV) {
1641 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1642 << ")\n";
1643 }
David Greene8618f952009-06-08 20:23:18 +00001644 assert(RV && "Template argument record not found??");
1645 ItemType = RV->getType();
1646 ++ArgN;
1647 }
1648 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001649 if (!Result.back()) return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001650
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001651 while (Lex.getCode() == tgtok::comma) {
1652 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001653
Craig Topper011817a2014-04-09 04:50:04 +00001654 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001655 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001656 if (ArgN >= TArgs.size()) {
1657 TokError("too many template arguments");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001658 return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001659 }
David Greene8618f952009-06-08 20:23:18 +00001660 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1661 assert(RV && "Template argument record not found??");
1662 ItemType = RV->getType();
1663 ++ArgN;
1664 }
1665 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001666 if (!Result.back()) return std::vector<Init*>();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001667 }
Bob Wilson7248f862009-11-22 04:24:42 +00001668
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001669 return Result;
1670}
1671
1672
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001673/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1674/// empty string on error. This can happen in a number of different context's,
1675/// including within a def or in the template args for a def (which which case
1676/// CurRec will be non-null) and within the template args for a multiclass (in
1677/// which case CurRec will be null, but CurMultiClass will be set). This can
1678/// also happen within a def that is within a multiclass, which will set both
1679/// CurRec and CurMultiClass.
1680///
1681/// Declaration ::= FIELD? Type ID ('=' Value)?
1682///
David Greenedb10e692011-10-19 13:02:42 +00001683Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001684 bool ParsingTemplateArgs) {
1685 // Read the field prefix if present.
1686 bool HasField = Lex.getCode() == tgtok::Field;
1687 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001688
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001689 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001690 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001691
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001692 if (Lex.getCode() != tgtok::Id) {
1693 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001694 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001695 }
Bob Wilson7248f862009-11-22 04:24:42 +00001696
Chris Lattner526c8cb2009-06-21 03:39:35 +00001697 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001698 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001699 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001700
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001701 if (ParsingTemplateArgs) {
1702 if (CurRec) {
David Greenedb10e692011-10-19 13:02:42 +00001703 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001704 } else {
1705 assert(CurMultiClass);
1706 }
1707 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001708 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1709 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001710 }
Bob Wilson7248f862009-11-22 04:24:42 +00001711
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001712 // Add the value.
1713 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001714 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001715
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001716 // If a value is present, parse it.
1717 if (Lex.getCode() == tgtok::equal) {
1718 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001719 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001720 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001721 if (!Val ||
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001722 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
Craig Topper011817a2014-04-09 04:50:04 +00001723 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001724 }
Bob Wilson7248f862009-11-22 04:24:42 +00001725
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001726 return DeclName;
1727}
1728
David Greenefb927af2012-02-22 16:09:41 +00001729/// ParseForeachDeclaration - Read a foreach declaration, returning
1730/// the name of the declared object or a NULL Init on error. Return
1731/// the name of the parsed initializer list through ForeachListName.
1732///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001733/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1734/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1735/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001736///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001737VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001738 if (Lex.getCode() != tgtok::Id) {
1739 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001740 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001741 }
1742
1743 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1744 Lex.Lex();
1745
1746 // If a value is present, parse it.
1747 if (Lex.getCode() != tgtok::equal) {
1748 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001749 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001750 }
1751 Lex.Lex(); // Eat the '='
1752
Craig Topper011817a2014-04-09 04:50:04 +00001753 RecTy *IterType = nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001754 std::vector<unsigned> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001755
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001756 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001757 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001758 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001759 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001760 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001761 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001762 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001763 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001764 }
1765 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001766 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001767 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001768 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001769 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001770 }
1771 IterType = ListType->getElementType();
1772 break;
David Greenefb927af2012-02-22 16:09:41 +00001773 }
1774
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001775 case tgtok::IntVal: { // RangePiece.
1776 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001777 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001778 break;
David Greenefb927af2012-02-22 16:09:41 +00001779 }
1780
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001781 case tgtok::l_brace: { // '{' RangeList '}'
1782 Lex.Lex(); // eat the '{'
1783 Ranges = ParseRangeList();
1784 if (Lex.getCode() != tgtok::r_brace) {
1785 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001786 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001787 }
1788 Lex.Lex();
1789 break;
1790 }
1791 }
David Greenefb927af2012-02-22 16:09:41 +00001792
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001793 if (!Ranges.empty()) {
1794 assert(!IterType && "Type already initialized?");
1795 IterType = IntRecTy::get();
1796 std::vector<Init*> Values;
1797 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
1798 Values.push_back(IntInit::get(Ranges[i]));
1799 ForeachListValue = ListInit::get(Values, IterType);
1800 }
1801
1802 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001803 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001804
1805 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001806}
1807
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001808/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1809/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1810/// template args for a def, which may or may not be in a multiclass. If null,
1811/// these are the template args for a multiclass.
1812///
1813/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001814///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001815bool TGParser::ParseTemplateArgList(Record *CurRec) {
1816 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1817 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001818
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001819 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001820
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001821 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001822 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001823 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001824 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001825
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001826 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001827
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001828 while (Lex.getCode() == tgtok::comma) {
1829 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001830
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001831 // Read the following declarations.
1832 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001833 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001834 return true;
1835 TheRecToAddTo->addTemplateArg(TemplArg);
1836 }
Bob Wilson7248f862009-11-22 04:24:42 +00001837
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001838 if (Lex.getCode() != tgtok::greater)
1839 return TokError("expected '>' at end of template argument list");
1840 Lex.Lex(); // eat the '>'.
1841 return false;
1842}
1843
1844
1845/// ParseBodyItem - Parse a single item at within the body of a def or class.
1846///
1847/// BodyItem ::= Declaration ';'
1848/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1849bool TGParser::ParseBodyItem(Record *CurRec) {
1850 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001851 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001852 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001853
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001854 if (Lex.getCode() != tgtok::semi)
1855 return TokError("expected ';' after declaration");
1856 Lex.Lex();
1857 return false;
1858 }
1859
1860 // LET ID OptionalRangeList '=' Value ';'
1861 if (Lex.Lex() != tgtok::Id)
1862 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001863
Chris Lattner526c8cb2009-06-21 03:39:35 +00001864 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001865 std::string FieldName = Lex.getCurStrVal();
1866 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001867
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001868 std::vector<unsigned> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001869 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001870 return true;
1871 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001872
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001873 if (Lex.getCode() != tgtok::equal)
1874 return TokError("expected '=' in let expression");
1875 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001876
David Greene8618f952009-06-08 20:23:18 +00001877 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001878 if (!Field)
David Greene8618f952009-06-08 20:23:18 +00001879 return TokError("Value '" + FieldName + "' unknown!");
1880
1881 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001882
David Greeneaf8ee2c2011-07-29 22:43:06 +00001883 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001884 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001885
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001886 if (Lex.getCode() != tgtok::semi)
1887 return TokError("expected ';' after let expression");
1888 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001889
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001890 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1891}
1892
1893/// ParseBody - Read the body of a class or def. Return true on error, false on
1894/// success.
1895///
1896/// Body ::= ';'
1897/// Body ::= '{' BodyList '}'
1898/// BodyList BodyItem*
1899///
1900bool TGParser::ParseBody(Record *CurRec) {
1901 // If this is a null definition, just eat the semi and return.
1902 if (Lex.getCode() == tgtok::semi) {
1903 Lex.Lex();
1904 return false;
1905 }
Bob Wilson7248f862009-11-22 04:24:42 +00001906
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001907 if (Lex.getCode() != tgtok::l_brace)
1908 return TokError("Expected ';' or '{' to start body");
1909 // Eat the '{'.
1910 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001911
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001912 while (Lex.getCode() != tgtok::r_brace)
1913 if (ParseBodyItem(CurRec))
1914 return true;
1915
1916 // Eat the '}'.
1917 Lex.Lex();
1918 return false;
1919}
1920
Sean Silvacb1a75e2013-01-09 04:49:14 +00001921/// \brief Apply the current let bindings to \a CurRec.
1922/// \returns true on error, false otherwise.
1923bool TGParser::ApplyLetStack(Record *CurRec) {
1924 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1925 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1926 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1927 LetStack[i][j].Bits, LetStack[i][j].Value))
1928 return true;
1929 return false;
1930}
1931
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001932/// ParseObjectBody - Parse the body of a def or class. This consists of an
1933/// optional ClassList followed by a Body. CurRec is the current def or class
1934/// that is being parsed.
1935///
1936/// ObjectBody ::= BaseClassList Body
1937/// BaseClassList ::= /*empty*/
1938/// BaseClassList ::= ':' BaseClassListNE
1939/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1940///
1941bool TGParser::ParseObjectBody(Record *CurRec) {
1942 // If there is a baseclass list, read it.
1943 if (Lex.getCode() == tgtok::colon) {
1944 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001945
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001946 // Read all of the subclasses.
1947 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1948 while (1) {
1949 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00001950 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001951
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001952 // Add it.
1953 if (AddSubClass(CurRec, SubClass))
1954 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001955
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001956 if (Lex.getCode() != tgtok::comma) break;
1957 Lex.Lex(); // eat ','.
1958 SubClass = ParseSubClassReference(CurRec, false);
1959 }
1960 }
1961
Sean Silvacb1a75e2013-01-09 04:49:14 +00001962 if (ApplyLetStack(CurRec))
1963 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001964
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001965 return ParseBody(CurRec);
1966}
1967
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001968/// ParseDef - Parse and return a top level or multiclass def, return the record
1969/// corresponding to it. This returns null on error.
1970///
1971/// DefInst ::= DEF ObjectName ObjectBody
1972///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00001973bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001974 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001975 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00001976 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001977
1978 // Parse ObjectName and make a record for it.
Jordan Roseabdd99b2013-01-10 18:50:05 +00001979 Record *CurRec;
1980 Init *Name = ParseObjectName(CurMultiClass);
1981 if (Name)
1982 CurRec = new Record(Name, DefLoc, Records);
1983 else
1984 CurRec = new Record(GetNewAnonymousName(), DefLoc, Records,
1985 /*IsAnonymous=*/true);
Bob Wilson7248f862009-11-22 04:24:42 +00001986
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00001987 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001988 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00001989
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001990 // Ensure redefinition doesn't happen.
David Greeneebb006f2012-01-28 00:03:24 +00001991 if (Records.getDef(CurRec->getNameInitAsString())) {
David Greene3a20f5a2011-10-19 13:03:45 +00001992 Error(DefLoc, "def '" + CurRec->getNameInitAsString()
1993 + "' already defined");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00001994 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001995 }
1996 Records.addDef(CurRec);
Hal Finkela8c1f462014-01-02 20:47:09 +00001997
1998 if (ParseObjectBody(CurRec))
1999 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002000 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002001 // Parse the body before adding this prototype to the DefPrototypes vector.
2002 // That way implicit definitions will be added to the DefPrototypes vector
2003 // before this object, instantiated prior to defs derived from this object,
2004 // and this available for indirect name resolution when defs derived from
2005 // this object are instantiated.
2006 if (ParseObjectBody(CurRec))
2007 return true;
2008
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002009 // Otherwise, a def inside a multiclass, add it to the multiclass.
2010 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene07e055f2011-10-19 13:03:51 +00002011 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2012 == CurRec->getNameInit()) {
2013 Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002014 "' already defined in this multiclass!");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002015 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002016 }
2017 CurMultiClass->DefPrototypes.push_back(CurRec);
Hal Finkela8c1f462014-01-02 20:47:09 +00002018 } else if (ParseObjectBody(CurRec))
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002019 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002020
Craig Topper011817a2014-04-09 04:50:04 +00002021 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002022 // See Record::setName(). This resolve step will see any new name
2023 // for the def that might have been created when resolving
2024 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002025 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002026
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002027 // If ObjectBody has template arguments, it's an error.
2028 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002029
2030 if (CurMultiClass) {
2031 // Copy the template arguments for the multiclass into the def.
David Greenedb10e692011-10-19 13:02:42 +00002032 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002033 CurMultiClass->Rec.getTemplateArgs();
2034
2035 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2036 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
2037 assert(RV && "Template arg doesn't exist?");
2038 CurRec->addValue(*RV);
2039 }
2040 }
2041
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002042 if (ProcessForeachDefs(CurRec, DefLoc)) {
David Greenefb927af2012-02-22 16:09:41 +00002043 Error(DefLoc,
2044 "Could not process loops for def" + CurRec->getNameInitAsString());
2045 return true;
2046 }
2047
2048 return false;
2049}
2050
2051/// ParseForeach - Parse a for statement. Return the record corresponding
2052/// to it. This returns true on error.
2053///
2054/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2055/// Foreach ::= FOREACH Declaration IN Object
2056///
2057bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2058 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2059 Lex.Lex(); // Eat the 'for' token.
2060
2061 // Make a temporary object to record items associated with the for
2062 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002063 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002064 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002065 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002066 return TokError("expected declaration in for");
2067
2068 if (Lex.getCode() != tgtok::In)
2069 return TokError("Unknown tok");
2070 Lex.Lex(); // Eat the in
2071
2072 // Create a loop object and remember it.
2073 Loops.push_back(ForeachLoop(IterName, ListValue));
2074
2075 if (Lex.getCode() != tgtok::l_brace) {
2076 // FOREACH Declaration IN Object
2077 if (ParseObject(CurMultiClass))
2078 return true;
2079 }
2080 else {
2081 SMLoc BraceLoc = Lex.getLoc();
2082 // Otherwise, this is a group foreach.
2083 Lex.Lex(); // eat the '{'.
2084
2085 // Parse the object list.
2086 if (ParseObjectList(CurMultiClass))
2087 return true;
2088
2089 if (Lex.getCode() != tgtok::r_brace) {
2090 TokError("expected '}' at end of foreach command");
2091 return Error(BraceLoc, "to match this '{'");
2092 }
2093 Lex.Lex(); // Eat the }
2094 }
2095
2096 // We've processed everything in this loop.
2097 Loops.pop_back();
2098
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002099 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002100}
2101
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002102/// ParseClass - Parse a tblgen class definition.
2103///
2104/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2105///
2106bool TGParser::ParseClass() {
2107 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2108 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002109
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002110 if (Lex.getCode() != tgtok::Id)
2111 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002112
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002113 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2114 if (CurRec) {
2115 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002116 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002117 !CurRec->getSuperClasses().empty() ||
2118 !CurRec->getTemplateArgs().empty())
David Greene8eed9982011-10-19 13:03:58 +00002119 return TokError("Class '" + CurRec->getNameInitAsString()
2120 + "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002121 } else {
2122 // If this is the first reference to this class, create and add it.
Chris Lattner89dcb682010-12-15 04:48:22 +00002123 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002124 Records.addClass(CurRec);
2125 }
2126 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002127
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002128 // If there are template args, parse them.
2129 if (Lex.getCode() == tgtok::less)
2130 if (ParseTemplateArgList(CurRec))
2131 return true;
2132
2133 // Finally, parse the object body.
2134 return ParseObjectBody(CurRec);
2135}
2136
2137/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2138/// of LetRecords.
2139///
2140/// LetList ::= LetItem (',' LetItem)*
2141/// LetItem ::= ID OptionalRangeList '=' Value
2142///
2143std::vector<LetRecord> TGParser::ParseLetList() {
2144 std::vector<LetRecord> Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002145
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002146 while (1) {
2147 if (Lex.getCode() != tgtok::Id) {
2148 TokError("expected identifier in let definition");
2149 return std::vector<LetRecord>();
2150 }
2151 std::string Name = Lex.getCurStrVal();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002152 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002153 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002154
2155 // Check for an optional RangeList.
2156 std::vector<unsigned> Bits;
Bob Wilson7248f862009-11-22 04:24:42 +00002157 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002158 return std::vector<LetRecord>();
2159 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002160
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002161 if (Lex.getCode() != tgtok::equal) {
2162 TokError("expected '=' in let expression");
2163 return std::vector<LetRecord>();
2164 }
2165 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002166
Craig Topper011817a2014-04-09 04:50:04 +00002167 Init *Val = ParseValue(nullptr);
2168 if (!Val) return std::vector<LetRecord>();
Bob Wilson7248f862009-11-22 04:24:42 +00002169
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002170 // Now that we have everything, add the record.
2171 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson7248f862009-11-22 04:24:42 +00002172
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002173 if (Lex.getCode() != tgtok::comma)
2174 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002175 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002176 }
2177}
2178
2179/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002180/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002181///
2182/// Object ::= LET LetList IN '{' ObjectList '}'
2183/// Object ::= LET LetList IN Object
2184///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002185bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002186 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2187 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002188
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002189 // Add this entry to the let stack.
2190 std::vector<LetRecord> LetInfo = ParseLetList();
2191 if (LetInfo.empty()) return true;
2192 LetStack.push_back(LetInfo);
2193
2194 if (Lex.getCode() != tgtok::In)
2195 return TokError("expected 'in' at end of top-level 'let'");
2196 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002197
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002198 // If this is a scalar let, just handle it now
2199 if (Lex.getCode() != tgtok::l_brace) {
2200 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002201 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002202 return true;
2203 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002204 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002205 // Otherwise, this is a group let.
2206 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002207
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002208 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002209 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002210 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002211
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002212 if (Lex.getCode() != tgtok::r_brace) {
2213 TokError("expected '}' at end of top level let command");
2214 return Error(BraceLoc, "to match this '{'");
2215 }
2216 Lex.Lex();
2217 }
Bob Wilson7248f862009-11-22 04:24:42 +00002218
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002219 // Outside this let scope, this let block is not active.
2220 LetStack.pop_back();
2221 return false;
2222}
2223
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002224/// ParseMultiClass - Parse a multiclass definition.
2225///
Bob Wilson3d948162009-04-28 19:41:44 +00002226/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002227/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2228/// MultiClassObject ::= DefInst
2229/// MultiClassObject ::= MultiClassInst
2230/// MultiClassObject ::= DefMInst
2231/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2232/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002233///
2234bool TGParser::ParseMultiClass() {
2235 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2236 Lex.Lex(); // Eat the multiclass token.
2237
2238 if (Lex.getCode() != tgtok::Id)
2239 return TokError("expected identifier after multiclass for name");
2240 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002241
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002242 if (MultiClasses.count(Name))
2243 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002244
Chris Lattner77d369c2010-12-13 00:23:57 +00002245 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
2246 Lex.getLoc(), Records);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002247 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002248
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002249 // If there are template args, parse them.
2250 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002251 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002252 return true;
2253
David Greene7049e792009-04-24 16:55:41 +00002254 bool inherits = false;
2255
David Greene753ed8f2009-04-22 16:42:54 +00002256 // If there are submulticlasses, parse them.
2257 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002258 inherits = true;
2259
David Greene753ed8f2009-04-22 16:42:54 +00002260 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002261
David Greene753ed8f2009-04-22 16:42:54 +00002262 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002263 SubMultiClassReference SubMultiClass =
2264 ParseSubMultiClassReference(CurMultiClass);
David Greene753ed8f2009-04-22 16:42:54 +00002265 while (1) {
2266 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002267 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002268
David Greene753ed8f2009-04-22 16:42:54 +00002269 // Add it.
2270 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2271 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002272
David Greene753ed8f2009-04-22 16:42:54 +00002273 if (Lex.getCode() != tgtok::comma) break;
2274 Lex.Lex(); // eat ','.
2275 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2276 }
2277 }
2278
David Greene7049e792009-04-24 16:55:41 +00002279 if (Lex.getCode() != tgtok::l_brace) {
2280 if (!inherits)
2281 return TokError("expected '{' in multiclass definition");
Bob Wilson7248f862009-11-22 04:24:42 +00002282 else if (Lex.getCode() != tgtok::semi)
2283 return TokError("expected ';' in multiclass definition");
David Greene7049e792009-04-24 16:55:41 +00002284 else
Bob Wilson7248f862009-11-22 04:24:42 +00002285 Lex.Lex(); // eat the ';'.
2286 } else {
David Greene7049e792009-04-24 16:55:41 +00002287 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2288 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002289
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002290 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002291 switch (Lex.getCode()) {
2292 default:
David Greene33f61992011-10-07 18:25:05 +00002293 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002294 case tgtok::Let:
2295 case tgtok::Def:
2296 case tgtok::Defm:
David Greenefb927af2012-02-22 16:09:41 +00002297 case tgtok::Foreach:
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002298 if (ParseObject(CurMultiClass))
2299 return true;
2300 break;
2301 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002302 }
David Greene7049e792009-04-24 16:55:41 +00002303 Lex.Lex(); // eat the '}'.
2304 }
Bob Wilson7248f862009-11-22 04:24:42 +00002305
Craig Topper011817a2014-04-09 04:50:04 +00002306 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002307 return false;
2308}
2309
David Greenedb445972011-10-05 22:42:07 +00002310Record *TGParser::
2311InstantiateMulticlassDef(MultiClass &MC,
2312 Record *DefProto,
Hal Finkelf2a0b2b2014-01-02 19:35:33 +00002313 Init *&DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002314 SMRange DefmPrefixRange) {
David Greene5d5d88c2011-10-19 13:04:31 +00002315 // We need to preserve DefProto so it can be reused for later
2316 // instantiations, so create a new Record to inherit from it.
2317
David Greenedb445972011-10-05 22:42:07 +00002318 // Add in the defm name. If the defm prefix is empty, give each
2319 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2320 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2321 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002322
Jordan Roseabdd99b2013-01-10 18:50:05 +00002323 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002324 if (!DefmPrefix) {
David Greene5d5d88c2011-10-19 13:04:31 +00002325 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002326 IsAnonymous = true;
2327 }
David Greene5d5d88c2011-10-19 13:04:31 +00002328
2329 Init *DefName = DefProto->getNameInit();
2330
Sean Silvafb509ed2012-10-10 20:24:43 +00002331 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002332
Craig Topper011817a2014-04-09 04:50:04 +00002333 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002334 // We have a fully expanded string so there are no operators to
2335 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002336 DefName =
2337 BinOpInit::get(BinOpInit::STRCONCAT,
2338 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2339 StringRecTy::get())->Fold(DefProto, &MC),
2340 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2341 }
David Greene5d5d88c2011-10-19 13:04:31 +00002342
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002343 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002344 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002345 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002346 Record *CurRec = new Record(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002347
2348 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002349 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002350 Ref.Rec = DefProto;
2351 AddSubClass(CurRec, Ref);
2352
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002353 // Set the value for NAME. We don't resolve references to it 'til later,
2354 // though, so that uses in nested multiclass names don't get
2355 // confused.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002356 if (SetValue(CurRec, Ref.RefRange.Start, "NAME", std::vector<unsigned>(),
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002357 DefmPrefix)) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002358 Error(DefmPrefixRange.Start, "Could not resolve "
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002359 + CurRec->getNameInitAsString() + ":NAME to '"
2360 + DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002361 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002362 }
David Greene8bf0d722011-10-19 13:04:35 +00002363
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002364 // If the DefNameString didn't resolve, we probably have a reference to
2365 // NAME and need to replace it. We need to do at least this much greedily,
2366 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002367 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002368 RecordVal *DefNameRV = CurRec->getValue("NAME");
2369 CurRec->resolveReferencesTo(DefNameRV);
2370 }
2371
2372 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002373 // Now that we're at the top level, resolve all NAME references
2374 // in the resultant defs that weren't in the def names themselves.
2375 RecordVal *DefNameRV = CurRec->getValue("NAME");
2376 CurRec->resolveReferencesTo(DefNameRV);
2377
2378 // Now that NAME references are resolved and we're at the top level of
2379 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002380 // currently in a multiclass, it means this defm appears inside a
2381 // multiclass and its name won't be fully resolvable until we see
2382 // the top-level defm. Therefore, we don't add this to the
2383 // RecordKeeper at this point. If we did we could get duplicate
2384 // defs as more than one probably refers to NAME or some other
2385 // common internal placeholder.
2386
2387 // Ensure redefinition doesn't happen.
2388 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002389 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002390 "' already defined, instantiating defm with subdef '" +
2391 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002392 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002393 }
2394
2395 Records.addDef(CurRec);
2396 }
2397
David Greenedb445972011-10-05 22:42:07 +00002398 return CurRec;
2399}
2400
2401bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2402 Record *CurRec,
2403 SMLoc DefmPrefixLoc,
2404 SMLoc SubClassLoc,
David Greenedb10e692011-10-19 13:02:42 +00002405 const std::vector<Init *> &TArgs,
David Greenedb445972011-10-05 22:42:07 +00002406 std::vector<Init *> &TemplateVals,
2407 bool DeleteArgs) {
2408 // Loop over all of the template arguments, setting them to the specified
2409 // value or leaving them as the default if necessary.
2410 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2411 // Check if a value is specified for this temp-arg.
2412 if (i < TemplateVals.size()) {
2413 // Set it now.
2414 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2415 TemplateVals[i]))
2416 return true;
2417
2418 // Resolve it next.
2419 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2420
2421 if (DeleteArgs)
2422 // Now remove it.
2423 CurRec->removeValue(TArgs[i]);
2424
2425 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2426 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenedb10e692011-10-19 13:02:42 +00002427 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2428 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2429 + "'");
David Greenedb445972011-10-05 22:42:07 +00002430 }
2431 }
2432 return false;
2433}
2434
2435bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2436 Record *CurRec,
2437 Record *DefProto,
2438 SMLoc DefmPrefixLoc) {
2439 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002440 if (ApplyLetStack(CurRec))
2441 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002442
David Greenedb445972011-10-05 22:42:07 +00002443 // Don't create a top level definition for defm inside multiclasses,
2444 // instead, only update the prototypes and bind the template args
2445 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002446 if (!CurMultiClass)
2447 return false;
2448 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2449 i != e; ++i)
2450 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2451 == CurRec->getNameInit())
2452 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2453 "' already defined in this multiclass!");
2454 CurMultiClass->DefPrototypes.push_back(CurRec);
David Greenedb445972011-10-05 22:42:07 +00002455
Sean Silvacc951b22013-01-09 05:28:12 +00002456 // Copy the template arguments for the multiclass into the new def.
2457 const std::vector<Init *> &TA =
2458 CurMultiClass->Rec.getTemplateArgs();
David Greenedb445972011-10-05 22:42:07 +00002459
Sean Silvacc951b22013-01-09 05:28:12 +00002460 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2461 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2462 assert(RV && "Template arg doesn't exist?");
2463 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002464 }
2465
2466 return false;
2467}
2468
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002469/// ParseDefm - Parse the instantiation of a multiclass.
2470///
2471/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2472///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002473bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002474 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002475 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002476 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002477
Craig Topperb21afc62013-01-07 05:09:33 +00002478 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002479 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002480 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002481
Jordan Rosef12e8a92013-01-10 18:50:11 +00002482 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002483 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002484 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002485
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002486 // Keep track of the new generated record definitions.
2487 std::vector<Record*> NewRecDefs;
2488
2489 // This record also inherits from a regular class (non-multiclass)?
2490 bool InheritFromClass = false;
2491
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002492 // eat the colon.
2493 Lex.Lex();
2494
Chris Lattner526c8cb2009-06-21 03:39:35 +00002495 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002496 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002497
2498 while (1) {
Craig Topper011817a2014-04-09 04:50:04 +00002499 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002500
2501 // To instantiate a multiclass, we need to first get the multiclass, then
2502 // instantiate each def contained in the multiclass with the SubClassRef
2503 // template parameters.
2504 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2505 assert(MC && "Didn't lookup multiclass correctly?");
David Greeneaf8ee2c2011-07-29 22:43:06 +00002506 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002507
2508 // Verify that the correct number of template arguments were specified.
David Greenedb10e692011-10-19 13:02:42 +00002509 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002510 if (TArgs.size() < TemplateVals.size())
2511 return Error(SubClassLoc,
2512 "more template args specified than multiclass expects");
2513
2514 // Loop over all the def's in the multiclass, instantiating each one.
2515 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2516 Record *DefProto = MC->DefPrototypes[i];
2517
Jordan Rosef12e8a92013-01-10 18:50:11 +00002518 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix,
2519 SMRange(DefmLoc,
2520 DefmPrefixEndLoc));
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002521 if (!CurRec)
2522 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002523
Jordan Rosef12e8a92013-01-10 18:50:11 +00002524 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002525 TArgs, TemplateVals, true/*Delete args*/))
2526 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002527
Jordan Rosef12e8a92013-01-10 18:50:11 +00002528 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002529 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002530
2531 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002532 }
2533
David Greenedb445972011-10-05 22:42:07 +00002534
David Greenef00919a2009-04-22 22:17:51 +00002535 if (Lex.getCode() != tgtok::comma) break;
2536 Lex.Lex(); // eat ','.
2537
Craig Topper998a39a2013-08-20 04:22:09 +00002538 if (Lex.getCode() != tgtok::Id)
2539 return TokError("expected identifier");
2540
David Greenef00919a2009-04-22 22:17:51 +00002541 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002542
2543 // A defm can inherit from regular classes (non-multiclass) as
2544 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002545 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002546
2547 if (InheritFromClass)
2548 break;
2549
Craig Topper011817a2014-04-09 04:50:04 +00002550 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002551 }
2552
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002553 if (InheritFromClass) {
2554 // Process all the classes to inherit as if they were part of a
2555 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002556 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002557 while (1) {
2558 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002559 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002560
2561 // Get the expanded definition prototypes and teach them about
2562 // the record values the current class to inherit has
2563 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2564 Record *CurRec = NewRecDefs[i];
2565
2566 // Add it.
2567 if (AddSubClass(CurRec, SubClass))
2568 return true;
2569
Sean Silvacb1a75e2013-01-09 04:49:14 +00002570 if (ApplyLetStack(CurRec))
2571 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002572 }
2573
2574 if (Lex.getCode() != tgtok::comma) break;
2575 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002576 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002577 }
2578 }
2579
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002580 if (!CurMultiClass)
2581 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene50c09122011-08-10 18:27:46 +00002582 // See Record::setName(). This resolve step will see any new
2583 // name for the def that might have been created when resolving
2584 // inheritance, values and arguments above.
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002585 NewRecDefs[i]->resolveReferences();
2586
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002587 if (Lex.getCode() != tgtok::semi)
2588 return TokError("expected ';' at end of defm");
2589 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002590
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002591 return false;
2592}
2593
2594/// ParseObject
2595/// Object ::= ClassInst
2596/// Object ::= DefInst
2597/// Object ::= MultiClassInst
2598/// Object ::= DefMInst
2599/// Object ::= LETCommand '{' ObjectList '}'
2600/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002601bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002602 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002603 default:
2604 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002605 case tgtok::Let: return ParseTopLevelLet(MC);
2606 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002607 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002608 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002609 case tgtok::Class: return ParseClass();
2610 case tgtok::MultiClass: return ParseMultiClass();
2611 }
2612}
2613
2614/// ParseObjectList
2615/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002616bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002617 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002618 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002619 return true;
2620 }
2621 return false;
2622}
2623
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002624bool TGParser::ParseFile() {
2625 Lex.Lex(); // Prime the lexer.
2626 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002627
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002628 // If we have unread input at the end of the file, report it.
2629 if (Lex.getCode() == tgtok::Eof)
2630 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002631
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002632 return TokError("Unexpected input at top level");
2633}
2634