blob: f337c75ed5e660085ad71cb7a1d67d4728ef46ed [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())) {
Artyom Skrobov8b985322014-06-10 12:41:14 +0000363 // If this record is anonymous, it's no problem, just generate a new name
364 if (IterRec->isAnonymous())
365 IterRec->setName(GetNewAnonymousName());
366 else {
367 Error(Loc, "def already exists: " + IterRec->getNameInitAsString());
368 return true;
369 }
David Greenefb927af2012-02-22 16:09:41 +0000370 }
371
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000372 Records.addDef(IterRec);
373 IterRec->resolveReferences();
David Greenefb927af2012-02-22 16:09:41 +0000374 return false;
375}
376
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000377//===----------------------------------------------------------------------===//
378// Parser Code
379//===----------------------------------------------------------------------===//
380
381/// isObjectStart - Return true if this is a valid first token for an Object.
382static bool isObjectStart(tgtok::TokKind K) {
383 return K == tgtok::Class || K == tgtok::Def ||
David Greenefb927af2012-02-22 16:09:41 +0000384 K == tgtok::Defm || K == tgtok::Let ||
385 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000386}
387
Alp Tokerce91fe52013-12-21 18:51:00 +0000388/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
389/// an identifier.
390std::string TGParser::GetNewAnonymousName() {
Michael J. Spencer2bb7db92013-02-26 21:29:47 +0000391 unsigned Tmp = AnonCounter++; // MSVC2012 ICEs without this.
Alp Tokerce91fe52013-12-21 18:51:00 +0000392 return "anonymous_" + utostr(Tmp);
Chris Lattner7538ed82010-10-05 22:51:56 +0000393}
394
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000395/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000396/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000397/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000398/// ObjectName ::= /*empty*/
399///
David Greene2affd672011-10-19 13:04:29 +0000400Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
401 switch (Lex.getCode()) {
402 case tgtok::colon:
403 case tgtok::semi:
404 case tgtok::l_brace:
405 // These are all of the tokens that can begin an object body.
406 // Some of these can also begin values but we disallow those cases
407 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000408 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000409 default:
410 break;
411 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000412
Craig Topper011817a2014-04-09 04:50:04 +0000413 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000414 if (CurMultiClass)
415 CurRec = &CurMultiClass->Rec;
416
Craig Topper011817a2014-04-09 04:50:04 +0000417 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000418 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000419 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000420 if (!CurRecName) {
421 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000422 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000423 }
424 Type = CurRecName->getType();
425 }
426
427 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000428}
429
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000430/// ParseClassID - Parse and resolve a reference to a class name. This returns
431/// null on error.
432///
433/// ClassID ::= ID
434///
435Record *TGParser::ParseClassID() {
436 if (Lex.getCode() != tgtok::Id) {
437 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000438 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000439 }
Bob Wilson7248f862009-11-22 04:24:42 +0000440
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000441 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000442 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000443 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000444
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000445 Lex.Lex();
446 return Result;
447}
448
Bob Wilson3d948162009-04-28 19:41:44 +0000449/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
450/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000451///
452/// MultiClassID ::= ID
453///
454MultiClass *TGParser::ParseMultiClassID() {
455 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000456 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000457 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000458 }
Bob Wilson3d948162009-04-28 19:41:44 +0000459
David Greene753ed8f2009-04-22 16:42:54 +0000460 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
Craig Topper011817a2014-04-09 04:50:04 +0000461 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000462 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000463
David Greene753ed8f2009-04-22 16:42:54 +0000464 Lex.Lex();
465 return Result;
466}
467
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000468/// ParseSubClassReference - Parse a reference to a subclass or to a templated
469/// subclass. This returns a SubClassRefTy with a null Record* on error.
470///
471/// SubClassRef ::= ClassID
472/// SubClassRef ::= ClassID '<' ValueList '>'
473///
474SubClassReference TGParser::
475ParseSubClassReference(Record *CurRec, bool isDefm) {
476 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000477 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000478
Sean Silva0657b402013-01-09 02:17:14 +0000479 if (isDefm) {
480 if (MultiClass *MC = ParseMultiClassID())
481 Result.Rec = &MC->Rec;
482 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000483 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000484 }
Craig Topper011817a2014-04-09 04:50:04 +0000485 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000486
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000487 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000488 if (Lex.getCode() != tgtok::less) {
489 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000490 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000491 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000492 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000493
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000494 if (Lex.getCode() == tgtok::greater) {
495 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000496 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000497 return Result;
498 }
Bob Wilson7248f862009-11-22 04:24:42 +0000499
David Greene8618f952009-06-08 20:23:18 +0000500 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000501 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000502 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000503 return Result;
504 }
Bob Wilson7248f862009-11-22 04:24:42 +0000505
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000506 if (Lex.getCode() != tgtok::greater) {
507 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000508 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000509 return Result;
510 }
511 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000512 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000513
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000514 return Result;
515}
516
Bob Wilson3d948162009-04-28 19:41:44 +0000517/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
518/// templated submulticlass. This returns a SubMultiClassRefTy with a null
519/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000520///
521/// SubMultiClassRef ::= MultiClassID
522/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
523///
524SubMultiClassReference TGParser::
525ParseSubMultiClassReference(MultiClass *CurMC) {
526 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000527 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000528
David Greene753ed8f2009-04-22 16:42:54 +0000529 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000530 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000531
David Greene753ed8f2009-04-22 16:42:54 +0000532 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000533 if (Lex.getCode() != tgtok::less) {
534 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000535 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000536 }
David Greene753ed8f2009-04-22 16:42:54 +0000537 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000538
David Greene753ed8f2009-04-22 16:42:54 +0000539 if (Lex.getCode() == tgtok::greater) {
540 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000541 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000542 return Result;
543 }
Bob Wilson3d948162009-04-28 19:41:44 +0000544
David Greene8618f952009-06-08 20:23:18 +0000545 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000546 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000547 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000548 return Result;
549 }
Bob Wilson3d948162009-04-28 19:41:44 +0000550
David Greene753ed8f2009-04-22 16:42:54 +0000551 if (Lex.getCode() != tgtok::greater) {
552 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000553 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000554 return Result;
555 }
556 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000557 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000558
559 return Result;
560}
561
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000562/// ParseRangePiece - Parse a bit/value range.
563/// RangePiece ::= INTVAL
564/// RangePiece ::= INTVAL '-' INTVAL
565/// RangePiece ::= INTVAL INTVAL
566bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000567 if (Lex.getCode() != tgtok::IntVal) {
568 TokError("expected integer or bitrange");
569 return true;
570 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000571 int64_t Start = Lex.getCurIntVal();
572 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000573
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000574 if (Start < 0)
575 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000576
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000577 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000578 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000579 Ranges.push_back(Start);
580 return false;
581 case tgtok::minus:
582 if (Lex.Lex() != tgtok::IntVal) {
583 TokError("expected integer value as end of range");
584 return true;
585 }
586 End = Lex.getCurIntVal();
587 break;
588 case tgtok::IntVal:
589 End = -Lex.getCurIntVal();
590 break;
591 }
Bob Wilson7248f862009-11-22 04:24:42 +0000592 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000593 return TokError("invalid range, cannot be negative");
594 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000595
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000596 // Add to the range.
597 if (Start < End) {
598 for (; Start <= End; ++Start)
599 Ranges.push_back(Start);
600 } else {
601 for (; Start >= End; --Start)
602 Ranges.push_back(Start);
603 }
604 return false;
605}
606
607/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
608///
609/// RangeList ::= RangePiece (',' RangePiece)*
610///
611std::vector<unsigned> TGParser::ParseRangeList() {
612 std::vector<unsigned> Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000613
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000614 // Parse the first piece.
615 if (ParseRangePiece(Result))
616 return std::vector<unsigned>();
617 while (Lex.getCode() == tgtok::comma) {
618 Lex.Lex(); // Eat the comma.
619
620 // Parse the next range piece.
621 if (ParseRangePiece(Result))
622 return std::vector<unsigned>();
623 }
624 return Result;
625}
626
627/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
628/// OptionalRangeList ::= '<' RangeList '>'
629/// OptionalRangeList ::= /*empty*/
630bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
631 if (Lex.getCode() != tgtok::less)
632 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000633
Chris Lattner526c8cb2009-06-21 03:39:35 +0000634 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000635 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000636
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000637 // Parse the range list.
638 Ranges = ParseRangeList();
639 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000640
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000641 if (Lex.getCode() != tgtok::greater) {
642 TokError("expected '>' at end of range list");
643 return Error(StartLoc, "to match this '<'");
644 }
645 Lex.Lex(); // eat the '>'.
646 return false;
647}
648
649/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
650/// OptionalBitList ::= '{' RangeList '}'
651/// OptionalBitList ::= /*empty*/
652bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
653 if (Lex.getCode() != tgtok::l_brace)
654 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000655
Chris Lattner526c8cb2009-06-21 03:39:35 +0000656 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000657 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000658
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000659 // Parse the range list.
660 Ranges = ParseRangeList();
661 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000662
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000663 if (Lex.getCode() != tgtok::r_brace) {
664 TokError("expected '}' at end of bit list");
665 return Error(StartLoc, "to match this '{'");
666 }
667 Lex.Lex(); // eat the '}'.
668 return false;
669}
670
671
672/// ParseType - Parse and return a tblgen type. This returns null on error.
673///
674/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000675/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000676/// Type ::= BIT // bit type
677/// Type ::= BITS '<' INTVAL '>' // bits<x> type
678/// Type ::= INT // int type
679/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000680/// Type ::= DAG // dag type
681/// Type ::= ClassID // Record Type
682///
683RecTy *TGParser::ParseType() {
684 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000685 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000686 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000687 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000688 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
689 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000690 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000691 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000692 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000693 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000694 case tgtok::Bits: {
695 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
696 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000697 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000698 }
699 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
700 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000701 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000702 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000703 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000704 if (Lex.Lex() != tgtok::greater) { // Eat count.
705 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000706 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000707 }
708 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000709 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000710 }
711 case tgtok::List: {
712 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
713 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000714 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000715 }
716 Lex.Lex(); // Eat '<'
717 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000718 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000719
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000720 if (Lex.getCode() != tgtok::greater) {
721 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000722 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000723 }
724 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000725 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000726 }
Bob Wilson7248f862009-11-22 04:24:42 +0000727 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000728}
729
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000730/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
731/// has already been read.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000732Init *TGParser::ParseIDValue(Record *CurRec,
David Greened4263a62011-10-19 13:04:20 +0000733 const std::string &Name, SMLoc NameLoc,
734 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000735 if (CurRec) {
736 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000737 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000738
David Greenedb10e692011-10-19 13:02:42 +0000739 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
740
David Greene47a665e2011-10-05 22:42:54 +0000741 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +0000742 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
743 "::");
David Greene47a665e2011-10-05 22:42:54 +0000744
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000745 if (CurRec->isTemplateArg(TemplateArgName)) {
746 const RecordVal *RV = CurRec->getValue(TemplateArgName);
747 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000748 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000749 }
750 }
Bob Wilson7248f862009-11-22 04:24:42 +0000751
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000752 if (CurMultiClass) {
David Greenedb10e692011-10-19 13:02:42 +0000753 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
754 "::");
755
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000756 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
757 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
758 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000759 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000760 }
761 }
Bob Wilson7248f862009-11-22 04:24:42 +0000762
David Greenefb927af2012-02-22 16:09:41 +0000763 // If this is in a foreach loop, make sure it's not a loop iterator
764 for (LoopVector::iterator i = Loops.begin(), iend = Loops.end();
765 i != iend;
766 ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000767 VarInit *IterVar = dyn_cast<VarInit>(i->IterVar);
David Greenefb927af2012-02-22 16:09:41 +0000768 if (IterVar && IterVar->getName() == Name)
769 return IterVar;
770 }
771
David Greene232bd602011-10-19 13:04:21 +0000772 if (Mode == ParseNameMode)
773 return StringInit::get(Name);
774
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000775 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000776 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000777
David Greene232bd602011-10-19 13:04:21 +0000778 if (Mode == ParseValueMode) {
779 Error(NameLoc, "Variable not defined: '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000780 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000781 }
782
783 return StringInit::get(Name);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000784}
785
David Greene5d0c0512009-05-14 20:54:48 +0000786/// ParseOperation - Parse an operator. This returns null on error.
787///
788/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
789///
David Greeneaf8ee2c2011-07-29 22:43:06 +0000790Init *TGParser::ParseOperation(Record *CurRec) {
David Greene5d0c0512009-05-14 20:54:48 +0000791 switch (Lex.getCode()) {
792 default:
793 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000794 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000795 case tgtok::XHead:
796 case tgtok::XTail:
797 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000798 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
799 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000800 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000801
David Greenee8f3b272009-05-14 21:22:49 +0000802 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000803 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000804 case tgtok::XCast:
805 Lex.Lex(); // eat the operation
806 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000807
David Greenee8f3b272009-05-14 21:22:49 +0000808 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000809
Craig Topper011817a2014-04-09 04:50:04 +0000810 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000811 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000812 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000813 }
David Greene5d0c0512009-05-14 20:54:48 +0000814
David Greenee8f3b272009-05-14 21:22:49 +0000815 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000816 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000817 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000818 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000819 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000820 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000821 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000822 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000823 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000824 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000825 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000826 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000827 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000828 break;
David Greenee8f3b272009-05-14 21:22:49 +0000829 }
830 if (Lex.getCode() != tgtok::l_paren) {
831 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000832 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000833 }
834 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000835
David Greeneaf8ee2c2011-07-29 22:43:06 +0000836 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000837 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000838
David Greene2f7cf7f2011-01-07 17:05:37 +0000839 if (Code == UnOpInit::HEAD
840 || Code == UnOpInit::TAIL
841 || Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000842 ListInit *LHSl = dyn_cast<ListInit>(LHS);
843 StringInit *LHSs = dyn_cast<StringInit>(LHS);
844 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000845 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000846 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000847 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000848 }
849 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000850 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
851 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000852 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000853 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000854 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000855 }
856 }
857
David Greene2f7cf7f2011-01-07 17:05:37 +0000858 if (Code == UnOpInit::HEAD
859 || Code == UnOpInit::TAIL) {
Craig Topper011817a2014-04-09 04:50:04 +0000860 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000861 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000862 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000863 }
Bob Wilson7248f862009-11-22 04:24:42 +0000864
David Greened571b3c2009-05-14 22:38:31 +0000865 if (LHSl && LHSl->getSize() == 0) {
866 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000867 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000868 }
869 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000870 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000871 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000872 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000873 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000874 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000875 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000876 if (Code == UnOpInit::HEAD) {
David Greened571b3c2009-05-14 22:38:31 +0000877 Type = Itemt->getType();
Bob Wilson7248f862009-11-22 04:24:42 +0000878 } else {
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000879 Type = ListRecTy::get(Itemt->getType());
David Greened571b3c2009-05-14 22:38:31 +0000880 }
Bob Wilson7248f862009-11-22 04:24:42 +0000881 } else {
David Greened571b3c2009-05-14 22:38:31 +0000882 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000883 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000884 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000885 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000886 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000887 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000888 if (Code == UnOpInit::HEAD) {
David Greened571b3c2009-05-14 22:38:31 +0000889 Type = LType->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +0000890 } else {
David Greened571b3c2009-05-14 22:38:31 +0000891 Type = LType;
892 }
893 }
894 }
895 }
896
David Greenee8f3b272009-05-14 21:22:49 +0000897 if (Lex.getCode() != tgtok::r_paren) {
898 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000899 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000900 }
901 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000902 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000903 }
David Greene5d0c0512009-05-14 20:54:48 +0000904
905 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000906 case tgtok::XADD:
Bob Wilson7248f862009-11-22 04:24:42 +0000907 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000908 case tgtok::XSRL:
909 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000910 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000911 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000912 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000913 tgtok::TokKind OpTok = Lex.getCode();
914 SMLoc OpLoc = Lex.getLoc();
915 Lex.Lex(); // eat the operation
916
David Greene5d0c0512009-05-14 20:54:48 +0000917 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000918 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000919
Chris Lattner61ea00b2010-10-05 23:58:18 +0000920 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000921 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000922 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000923 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000924 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
925 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
926 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
927 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000928 case tgtok::XListConcat:
929 Code = BinOpInit::LISTCONCAT;
930 // We don't know the list type until we parse the first argument
931 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000932 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000933 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000934 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000935 break;
David Greene5d0c0512009-05-14 20:54:48 +0000936 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000937
David Greene5d0c0512009-05-14 20:54:48 +0000938 if (Lex.getCode() != tgtok::l_paren) {
939 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000940 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000941 }
942 Lex.Lex(); // eat the '('
943
David Greeneaf8ee2c2011-07-29 22:43:06 +0000944 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000945
Chris Lattner61ea00b2010-10-05 23:58:18 +0000946 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000947 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000948
Chris Lattner61ea00b2010-10-05 23:58:18 +0000949 while (Lex.getCode() == tgtok::comma) {
950 Lex.Lex(); // eat the ','
951
952 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000953 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000954 }
David Greene5d0c0512009-05-14 20:54:48 +0000955
956 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000957 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000958 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000959 }
960 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000961
Daniel Sanders314e80e2014-05-07 10:13:19 +0000962 // If we are doing !listconcat, we should know the type by now
963 if (OpTok == tgtok::XListConcat) {
964 if (VarInit *Arg0 = dyn_cast<VarInit>(InitList[0]))
965 Type = Arg0->getType();
966 else if (ListInit *Arg0 = dyn_cast<ListInit>(InitList[0]))
967 Type = Arg0->getType();
968 else {
969 InitList[0]->dump();
970 Error(OpLoc, "expected a list");
971 return nullptr;
972 }
973 }
974
Chris Lattner61ea00b2010-10-05 23:58:18 +0000975 // We allow multiple operands to associative operators like !strconcat as
976 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000977 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000978 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000979 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000980 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
981 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000982 InitList.back() = RHS;
983 }
984 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000985
Chris Lattner61ea00b2010-10-05 23:58:18 +0000986 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000987 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000988 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000989
Chris Lattner61ea00b2010-10-05 23:58:18 +0000990 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +0000991 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000992 }
993
David Greene3587eed2009-05-14 23:26:46 +0000994 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +0000995 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +0000996 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
997 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000998 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000999
David Greene98ed3c72009-05-14 21:54:42 +00001000 tgtok::TokKind LexCode = Lex.getCode();
1001 Lex.Lex(); // eat the operation
1002 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001003 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001004 case tgtok::XIf:
1005 Code = TernOpInit::IF;
1006 break;
David Greenee917fff2009-05-14 22:23:47 +00001007 case tgtok::XForEach:
1008 Code = TernOpInit::FOREACH;
1009 break;
David Greene98ed3c72009-05-14 21:54:42 +00001010 case tgtok::XSubst:
1011 Code = TernOpInit::SUBST;
1012 break;
1013 }
1014 if (Lex.getCode() != tgtok::l_paren) {
1015 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001016 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001017 }
1018 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001019
David Greeneaf8ee2c2011-07-29 22:43:06 +00001020 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001021 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001022
David Greene98ed3c72009-05-14 21:54:42 +00001023 if (Lex.getCode() != tgtok::comma) {
1024 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001025 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001026 }
1027 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001028
David Greeneaf8ee2c2011-07-29 22:43:06 +00001029 Init *MHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001030 if (!MHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001031
David Greene98ed3c72009-05-14 21:54:42 +00001032 if (Lex.getCode() != tgtok::comma) {
1033 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001034 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001035 }
1036 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001037
David Greeneaf8ee2c2011-07-29 22:43:06 +00001038 Init *RHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001039 if (!RHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001040
David Greene98ed3c72009-05-14 21:54:42 +00001041 if (Lex.getCode() != tgtok::r_paren) {
1042 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001043 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001044 }
1045 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001046
David Greene98ed3c72009-05-14 21:54:42 +00001047 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001048 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001049 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001050 RecTy *MHSTy = nullptr;
1051 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001052
Sean Silvafb509ed2012-10-10 20:24:43 +00001053 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001054 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001055 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001056 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001057 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001058 MHSTy = BitRecTy::get();
1059
Sean Silvafb509ed2012-10-10 20:24:43 +00001060 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001061 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001062 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001063 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001064 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001065 RHSTy = BitRecTy::get();
1066
1067 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001068 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001069 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001070 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001071 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001072
1073 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001074 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001075 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001076 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001077
1078 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1079 Type = RHSTy;
1080 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1081 Type = MHSTy;
Bob Wilson7248f862009-11-22 04:24:42 +00001082 } else {
David Greene3587eed2009-05-14 23:26:46 +00001083 TokError("inconsistent types for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001084 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001085 }
1086 break;
1087 }
David Greenee917fff2009-05-14 22:23:47 +00001088 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001089 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
Craig Topper011817a2014-04-09 04:50:04 +00001090 if (!MHSt) {
David Greenee917fff2009-05-14 22:23:47 +00001091 TokError("could not get type for !foreach");
Craig Topper011817a2014-04-09 04:50:04 +00001092 return nullptr;
David Greenee917fff2009-05-14 22:23:47 +00001093 }
1094 Type = MHSt->getType();
1095 break;
1096 }
David Greene98ed3c72009-05-14 21:54:42 +00001097 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001098 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001099 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001100 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001101 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001102 }
1103 Type = RHSt->getType();
1104 break;
1105 }
1106 }
David Greenee32ebf22011-07-29 19:07:07 +00001107 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001108 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001109 }
David Greene5d0c0512009-05-14 20:54:48 +00001110 }
David Greene5d0c0512009-05-14 20:54:48 +00001111}
1112
1113/// ParseOperatorType - Parse a type for an operator. This returns
1114/// null on error.
1115///
1116/// OperatorType ::= '<' Type '>'
1117///
Dan Gohman1432ef82009-08-12 22:10:57 +00001118RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001119 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001120
1121 if (Lex.getCode() != tgtok::less) {
1122 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001123 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001124 }
1125 Lex.Lex(); // eat the <
1126
1127 Type = ParseType();
1128
Craig Topper011817a2014-04-09 04:50:04 +00001129 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001130 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
1134 if (Lex.getCode() != tgtok::greater) {
1135 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001136 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001137 }
1138 Lex.Lex(); // eat the >
1139
1140 return Type;
1141}
1142
1143
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001144/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1145///
1146/// SimpleValue ::= IDValue
1147/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001148/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001149/// SimpleValue ::= CODEFRAGMENT
1150/// SimpleValue ::= '?'
1151/// SimpleValue ::= '{' ValueList '}'
1152/// SimpleValue ::= ID '<' ValueListNE '>'
1153/// SimpleValue ::= '[' ValueList ']'
1154/// SimpleValue ::= '(' IDValue DagArgList ')'
1155/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001156/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001157/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1158/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1159/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001160/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001161/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1162///
David Greened4263a62011-10-19 13:04:20 +00001163Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1164 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001165 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001166 switch (Lex.getCode()) {
1167 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001168 case tgtok::paste:
1169 // This is a leading paste operation. This is deprecated but
1170 // still exists in some .td files. Ignore it.
1171 Lex.Lex(); // Skip '#'.
1172 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001173 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001174 case tgtok::StrVal: {
1175 std::string Val = Lex.getCurStrVal();
1176 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001177
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001178 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001179 while (Lex.getCode() == tgtok::StrVal) {
1180 Val += Lex.getCurStrVal();
1181 Lex.Lex();
1182 }
Bob Wilson7248f862009-11-22 04:24:42 +00001183
David Greenee32ebf22011-07-29 19:07:07 +00001184 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001185 break;
1186 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001187 case tgtok::CodeFragment:
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +00001188 R = StringInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001189 Lex.Lex();
1190 break;
1191 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001192 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001193 Lex.Lex();
1194 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001195 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001196 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001197 std::string Name = Lex.getCurStrVal();
1198 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001199 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001200
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001201 // Value ::= ID '<' ValueListNE '>'
1202 if (Lex.Lex() == tgtok::greater) {
1203 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001204 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001205 }
David Greene8618f952009-06-08 20:23:18 +00001206
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001207 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1208 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1209 // body.
1210 Record *Class = Records.getClass(Name);
1211 if (!Class) {
1212 Error(NameLoc, "Expected a class name, got '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001213 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001214 }
David Greene8618f952009-06-08 20:23:18 +00001215
David Greeneaf8ee2c2011-07-29 22:43:06 +00001216 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
Craig Topper011817a2014-04-09 04:50:04 +00001217 if (ValueList.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001218
David Greene8618f952009-06-08 20:23:18 +00001219 if (Lex.getCode() != tgtok::greater) {
1220 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001221 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001222 }
1223 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001224 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001225
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001226 // Create the new record, set it as CurRec temporarily.
Alp Tokerce91fe52013-12-21 18:51:00 +00001227 Record *NewRec = new Record(GetNewAnonymousName(), NameLoc, Records,
Jordan Roseabdd99b2013-01-10 18:50:05 +00001228 /*IsAnonymous=*/true);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001229 SubClassReference SCRef;
Jordan Rosef12e8a92013-01-10 18:50:11 +00001230 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001231 SCRef.Rec = Class;
1232 SCRef.TemplateArgs = ValueList;
1233 // Add info about the subclass to NewRec.
1234 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001235 return nullptr;
Hal Finkela8c1f462014-01-02 20:47:09 +00001236 if (!CurMultiClass) {
1237 NewRec->resolveReferences();
1238 Records.addDef(NewRec);
1239 } else {
1240 // Otherwise, we're inside a multiclass, add it to the multiclass.
1241 CurMultiClass->DefPrototypes.push_back(NewRec);
1242
1243 // Copy the template arguments for the multiclass into the def.
1244 const std::vector<Init *> &TArgs =
1245 CurMultiClass->Rec.getTemplateArgs();
1246
1247 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1248 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1249 assert(RV && "Template arg doesn't exist?");
1250 NewRec->addValue(*RV);
1251 }
1252
1253 // We can't return the prototype def here, instead return:
1254 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1255 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1256 assert(MCNameRV && "multiclass record must have a NAME");
1257
1258 return UnOpInit::get(UnOpInit::CAST,
1259 BinOpInit::get(BinOpInit::STRCONCAT,
1260 VarInit::get(MCNameRV->getName(),
1261 MCNameRV->getType()),
1262 NewRec->getNameInit(),
1263 StringRecTy::get()),
1264 Class->getDefInit()->getType());
1265 }
Bob Wilson7248f862009-11-22 04:24:42 +00001266
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001267 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001268 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001269 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001270 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001271 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001272 Lex.Lex(); // eat the '{'
David Greeneaf8ee2c2011-07-29 22:43:06 +00001273 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001274
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001275 if (Lex.getCode() != tgtok::r_brace) {
1276 Vals = ParseValueList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001277 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001278 }
1279 if (Lex.getCode() != tgtok::r_brace) {
1280 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001281 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001282 }
1283 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001284
David Greeneaf8ee2c2011-07-29 22:43:06 +00001285 SmallVector<Init *, 16> NewBits(Vals.size());
David Greeneb3da8122011-07-29 19:07:00 +00001286
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001287 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001288 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001289 if (!Bit) {
Chris Lattner52416952007-11-22 21:06:59 +00001290 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1291 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001292 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001293 }
David Greeneb3da8122011-07-29 19:07:00 +00001294 NewBits[Vals.size()-i-1] = Bit;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001295 }
David Greenee32ebf22011-07-29 19:07:07 +00001296 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001297 }
1298 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1299 Lex.Lex(); // eat the '['
David Greeneaf8ee2c2011-07-29 22:43:06 +00001300 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001301
Craig Topper011817a2014-04-09 04:50:04 +00001302 RecTy *DeducedEltTy = nullptr;
1303 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001304
Craig Topper011817a2014-04-09 04:50:04 +00001305 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001306 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001307 if (!ListType) {
Reid Klecknerd78273f2013-08-06 22:51:21 +00001308 std::string s;
1309 raw_string_ostream ss(s);
1310 ss << "Type mismatch for list, expected list type, got "
1311 << ItemType->getAsString();
1312 TokError(ss.str());
Craig Topper011817a2014-04-09 04:50:04 +00001313 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001314 }
1315 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001316 }
David Greene8618f952009-06-08 20:23:18 +00001317
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001318 if (Lex.getCode() != tgtok::r_square) {
Craig Topper011817a2014-04-09 04:50:04 +00001319 Vals = ParseValueList(CurRec, nullptr,
1320 GivenListTy ? GivenListTy->getElementType() : nullptr);
1321 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001322 }
1323 if (Lex.getCode() != tgtok::r_square) {
1324 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001325 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001326 }
1327 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001328
Craig Topper011817a2014-04-09 04:50:04 +00001329 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001330 if (Lex.getCode() == tgtok::less) {
1331 // Optional list element type
1332 Lex.Lex(); // eat the '<'
1333
1334 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001335 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001336 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001337 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001338 }
1339
1340 if (Lex.getCode() != tgtok::greater) {
1341 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001342 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001343 }
1344 Lex.Lex(); // eat the '>'
1345 }
1346
1347 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001348 RecTy *EltTy = nullptr;
David Greeneaf8ee2c2011-07-29 22:43:06 +00001349 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greene8618f952009-06-08 20:23:18 +00001350 i != ie;
1351 ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +00001352 TypedInit *TArg = dyn_cast<TypedInit>(*i);
Craig Topper011817a2014-04-09 04:50:04 +00001353 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001354 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001355 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001356 }
Craig Topper011817a2014-04-09 04:50:04 +00001357 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001358 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001359 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001360 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001361 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001362 }
Bob Wilson7248f862009-11-22 04:24:42 +00001363 } else {
David Greene8618f952009-06-08 20:23:18 +00001364 EltTy = TArg->getType();
1365 }
1366 }
1367
Craig Topper011817a2014-04-09 04:50:04 +00001368 if (GivenEltTy) {
1369 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001370 // Verify consistency
1371 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1372 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001373 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001374 }
1375 }
1376 EltTy = GivenEltTy;
1377 }
1378
Craig Topper011817a2014-04-09 04:50:04 +00001379 if (!EltTy) {
1380 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001381 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001382 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001383 }
1384 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001385 } else {
David Greene8618f952009-06-08 20:23:18 +00001386 // Make sure the deduced type is compatible with the given type
1387 if (GivenListTy) {
1388 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1389 TokError("Element type mismatch for list");
Craig Topper011817a2014-04-09 04:50:04 +00001390 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001391 }
1392 }
1393 DeducedEltTy = EltTy;
1394 }
Bob Wilson7248f862009-11-22 04:24:42 +00001395
David Greenee32ebf22011-07-29 19:07:07 +00001396 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001397 }
1398 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1399 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001400 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001401 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001402 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001403 }
Bob Wilson7248f862009-11-22 04:24:42 +00001404
David Greeneaf8ee2c2011-07-29 22:43:06 +00001405 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001406 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001407
Nate Begemandbe3f772009-03-19 05:21:56 +00001408 // If the operator name is present, parse it.
1409 std::string OperatorName;
1410 if (Lex.getCode() == tgtok::colon) {
1411 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1412 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001413 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001414 }
1415 OperatorName = Lex.getCurStrVal();
1416 Lex.Lex(); // eat the VarName.
1417 }
Bob Wilson7248f862009-11-22 04:24:42 +00001418
David Greeneaf8ee2c2011-07-29 22:43:06 +00001419 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001420 if (Lex.getCode() != tgtok::r_paren) {
1421 DagArgs = ParseDagArgList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001422 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001423 }
Bob Wilson7248f862009-11-22 04:24:42 +00001424
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001425 if (Lex.getCode() != tgtok::r_paren) {
1426 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001427 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001428 }
1429 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001430
David Greenee32ebf22011-07-29 19:07:07 +00001431 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001432 }
Bob Wilson7248f862009-11-22 04:24:42 +00001433
David Greene2f7cf7f2011-01-07 17:05:37 +00001434 case tgtok::XHead:
1435 case tgtok::XTail:
1436 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001437 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001438 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001439 case tgtok::XADD:
Bob Wilson7248f862009-11-22 04:24:42 +00001440 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001441 case tgtok::XSRL:
1442 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001443 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001444 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001445 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001446 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001447 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001448 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greene5d0c0512009-05-14 20:54:48 +00001449 return ParseOperation(CurRec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001450 }
1451 }
Bob Wilson7248f862009-11-22 04:24:42 +00001452
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001453 return R;
1454}
1455
1456/// ParseValue - Parse a tblgen value. This returns null on error.
1457///
1458/// Value ::= SimpleValue ValueSuffix*
1459/// ValueSuffix ::= '{' BitList '}'
1460/// ValueSuffix ::= '[' BitList ']'
1461/// ValueSuffix ::= '.' ID
1462///
David Greened4263a62011-10-19 13:04:20 +00001463Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1464 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001465 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001466
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001467 // Parse the suffixes now if present.
1468 while (1) {
1469 switch (Lex.getCode()) {
1470 default: return Result;
1471 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001472 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001473 // This is the beginning of the object body.
1474 return Result;
1475
Chris Lattner526c8cb2009-06-21 03:39:35 +00001476 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001477 Lex.Lex(); // eat the '{'
1478 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001479 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001480
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001481 // Reverse the bitlist.
1482 std::reverse(Ranges.begin(), Ranges.end());
1483 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001484 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001485 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001486 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001487 }
Bob Wilson7248f862009-11-22 04:24:42 +00001488
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001489 // Eat the '}'.
1490 if (Lex.getCode() != tgtok::r_brace) {
1491 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001492 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001493 }
1494 Lex.Lex();
1495 break;
1496 }
1497 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001498 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001499 Lex.Lex(); // eat the '['
1500 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001501 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001502
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001503 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001504 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001505 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001506 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001507 }
Bob Wilson7248f862009-11-22 04:24:42 +00001508
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001509 // Eat the ']'.
1510 if (Lex.getCode() != tgtok::r_square) {
1511 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001512 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001513 }
1514 Lex.Lex();
1515 break;
1516 }
1517 case tgtok::period:
1518 if (Lex.Lex() != tgtok::Id) { // eat the .
1519 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001520 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001521 }
1522 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001523 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001524 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001525 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001526 }
David Greenee32ebf22011-07-29 19:07:07 +00001527 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001528 Lex.Lex(); // eat field name
1529 break;
David Greene8e85b482011-10-19 13:04:43 +00001530
1531 case tgtok::paste:
1532 SMLoc PasteLoc = Lex.getLoc();
1533
1534 // Create a !strconcat() operation, first casting each operand to
1535 // a string if necessary.
1536
Sean Silvafb509ed2012-10-10 20:24:43 +00001537 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001538 if (!LHS) {
1539 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001540 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001541 }
1542
1543 if (LHS->getType() != StringRecTy::get()) {
1544 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1545 }
1546
Craig Topper011817a2014-04-09 04:50:04 +00001547 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001548
1549 Lex.Lex(); // Eat the '#'.
1550 switch (Lex.getCode()) {
1551 case tgtok::colon:
1552 case tgtok::semi:
1553 case tgtok::l_brace:
1554 // These are all of the tokens that can begin an object body.
1555 // Some of these can also begin values but we disallow those cases
1556 // because they are unlikely to be useful.
1557
1558 // Trailing paste, concat with an empty string.
1559 RHS = StringInit::get("");
1560 break;
1561
1562 default:
1563 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001564 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001565 if (!RHS) {
1566 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001567 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001568 }
1569
1570 if (RHS->getType() != StringRecTy::get()) {
1571 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1572 }
1573
1574 break;
1575 }
1576
1577 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1578 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1579 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001580 }
1581 }
1582}
1583
1584/// ParseDagArgList - Parse the argument list for a dag literal expression.
1585///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001586/// DagArg ::= Value (':' VARNAME)?
1587/// DagArg ::= VARNAME
1588/// DagArgList ::= DagArg
1589/// DagArgList ::= DagArgList ',' DagArg
David Greeneaf8ee2c2011-07-29 22:43:06 +00001590std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001591TGParser::ParseDagArgList(Record *CurRec) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001592 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson7248f862009-11-22 04:24:42 +00001593
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001594 while (1) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001595 // DagArg ::= VARNAME
1596 if (Lex.getCode() == tgtok::VarName) {
1597 // A missing value is treated like '?'.
1598 Result.push_back(std::make_pair(UnsetInit::get(), Lex.getCurStrVal()));
1599 Lex.Lex();
1600 } else {
1601 // DagArg ::= Value (':' VARNAME)?
1602 Init *Val = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001603 if (!Val)
David Greeneaf8ee2c2011-07-29 22:43:06 +00001604 return std::vector<std::pair<llvm::Init*, std::string> >();
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001605
1606 // If the variable name is present, add it.
1607 std::string VarName;
1608 if (Lex.getCode() == tgtok::colon) {
1609 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1610 TokError("expected variable name in dag literal");
1611 return std::vector<std::pair<llvm::Init*, std::string> >();
1612 }
1613 VarName = Lex.getCurStrVal();
1614 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001615 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001616
1617 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001618 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001619 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001620 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001621 }
Bob Wilson7248f862009-11-22 04:24:42 +00001622
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001623 return Result;
1624}
1625
1626
1627/// ParseValueList - Parse a comma separated list of values, returning them as a
1628/// vector. Note that this always expects to be able to parse at least one
1629/// value. It returns an empty list if this is not possible.
1630///
1631/// ValueList ::= Value (',' Value)
1632///
David Greeneaf8ee2c2011-07-29 22:43:06 +00001633std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopher71520a82011-07-11 23:06:52 +00001634 RecTy *EltTy) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001635 std::vector<Init*> Result;
David Greene8618f952009-06-08 20:23:18 +00001636 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001637 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001638 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001639 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001640 if (!TArgs.size()) {
1641 TokError("template argument provided to non-template class");
1642 return std::vector<Init*>();
1643 }
David Greene8618f952009-06-08 20:23:18 +00001644 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001645 if (!RV) {
1646 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1647 << ")\n";
1648 }
David Greene8618f952009-06-08 20:23:18 +00001649 assert(RV && "Template argument record not found??");
1650 ItemType = RV->getType();
1651 ++ArgN;
1652 }
1653 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001654 if (!Result.back()) return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001655
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001656 while (Lex.getCode() == tgtok::comma) {
1657 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001658
Craig Topper011817a2014-04-09 04:50:04 +00001659 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001660 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001661 if (ArgN >= TArgs.size()) {
1662 TokError("too many template arguments");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001663 return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001664 }
David Greene8618f952009-06-08 20:23:18 +00001665 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1666 assert(RV && "Template argument record not found??");
1667 ItemType = RV->getType();
1668 ++ArgN;
1669 }
1670 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001671 if (!Result.back()) return std::vector<Init*>();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001672 }
Bob Wilson7248f862009-11-22 04:24:42 +00001673
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001674 return Result;
1675}
1676
1677
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001678/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1679/// empty string on error. This can happen in a number of different context's,
1680/// including within a def or in the template args for a def (which which case
1681/// CurRec will be non-null) and within the template args for a multiclass (in
1682/// which case CurRec will be null, but CurMultiClass will be set). This can
1683/// also happen within a def that is within a multiclass, which will set both
1684/// CurRec and CurMultiClass.
1685///
1686/// Declaration ::= FIELD? Type ID ('=' Value)?
1687///
David Greenedb10e692011-10-19 13:02:42 +00001688Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001689 bool ParsingTemplateArgs) {
1690 // Read the field prefix if present.
1691 bool HasField = Lex.getCode() == tgtok::Field;
1692 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001693
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001694 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001695 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001696
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001697 if (Lex.getCode() != tgtok::Id) {
1698 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001699 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001700 }
Bob Wilson7248f862009-11-22 04:24:42 +00001701
Chris Lattner526c8cb2009-06-21 03:39:35 +00001702 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001703 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001704 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001705
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001706 if (ParsingTemplateArgs) {
1707 if (CurRec) {
David Greenedb10e692011-10-19 13:02:42 +00001708 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001709 } else {
1710 assert(CurMultiClass);
1711 }
1712 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001713 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1714 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001715 }
Bob Wilson7248f862009-11-22 04:24:42 +00001716
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001717 // Add the value.
1718 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001719 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001720
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001721 // If a value is present, parse it.
1722 if (Lex.getCode() == tgtok::equal) {
1723 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001724 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001725 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001726 if (!Val ||
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001727 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
Craig Topper011817a2014-04-09 04:50:04 +00001728 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001729 }
Bob Wilson7248f862009-11-22 04:24:42 +00001730
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001731 return DeclName;
1732}
1733
David Greenefb927af2012-02-22 16:09:41 +00001734/// ParseForeachDeclaration - Read a foreach declaration, returning
1735/// the name of the declared object or a NULL Init on error. Return
1736/// the name of the parsed initializer list through ForeachListName.
1737///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001738/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1739/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1740/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001741///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001742VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001743 if (Lex.getCode() != tgtok::Id) {
1744 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001745 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001746 }
1747
1748 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1749 Lex.Lex();
1750
1751 // If a value is present, parse it.
1752 if (Lex.getCode() != tgtok::equal) {
1753 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001754 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001755 }
1756 Lex.Lex(); // Eat the '='
1757
Craig Topper011817a2014-04-09 04:50:04 +00001758 RecTy *IterType = nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001759 std::vector<unsigned> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001760
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001761 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001762 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001763 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001764 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001765 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001766 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001767 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001768 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001769 }
1770 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001771 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001772 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001773 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001774 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001775 }
1776 IterType = ListType->getElementType();
1777 break;
David Greenefb927af2012-02-22 16:09:41 +00001778 }
1779
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001780 case tgtok::IntVal: { // RangePiece.
1781 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001782 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001783 break;
David Greenefb927af2012-02-22 16:09:41 +00001784 }
1785
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001786 case tgtok::l_brace: { // '{' RangeList '}'
1787 Lex.Lex(); // eat the '{'
1788 Ranges = ParseRangeList();
1789 if (Lex.getCode() != tgtok::r_brace) {
1790 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001791 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001792 }
1793 Lex.Lex();
1794 break;
1795 }
1796 }
David Greenefb927af2012-02-22 16:09:41 +00001797
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001798 if (!Ranges.empty()) {
1799 assert(!IterType && "Type already initialized?");
1800 IterType = IntRecTy::get();
1801 std::vector<Init*> Values;
1802 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
1803 Values.push_back(IntInit::get(Ranges[i]));
1804 ForeachListValue = ListInit::get(Values, IterType);
1805 }
1806
1807 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001808 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001809
1810 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001811}
1812
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001813/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1814/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1815/// template args for a def, which may or may not be in a multiclass. If null,
1816/// these are the template args for a multiclass.
1817///
1818/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001819///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001820bool TGParser::ParseTemplateArgList(Record *CurRec) {
1821 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1822 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001823
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001824 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001825
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001826 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001827 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001828 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001829 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001830
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001831 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001832
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001833 while (Lex.getCode() == tgtok::comma) {
1834 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001835
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001836 // Read the following declarations.
1837 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001838 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001839 return true;
1840 TheRecToAddTo->addTemplateArg(TemplArg);
1841 }
Bob Wilson7248f862009-11-22 04:24:42 +00001842
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001843 if (Lex.getCode() != tgtok::greater)
1844 return TokError("expected '>' at end of template argument list");
1845 Lex.Lex(); // eat the '>'.
1846 return false;
1847}
1848
1849
1850/// ParseBodyItem - Parse a single item at within the body of a def or class.
1851///
1852/// BodyItem ::= Declaration ';'
1853/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1854bool TGParser::ParseBodyItem(Record *CurRec) {
1855 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001856 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001857 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001858
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001859 if (Lex.getCode() != tgtok::semi)
1860 return TokError("expected ';' after declaration");
1861 Lex.Lex();
1862 return false;
1863 }
1864
1865 // LET ID OptionalRangeList '=' Value ';'
1866 if (Lex.Lex() != tgtok::Id)
1867 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001868
Chris Lattner526c8cb2009-06-21 03:39:35 +00001869 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001870 std::string FieldName = Lex.getCurStrVal();
1871 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001872
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001873 std::vector<unsigned> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001874 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001875 return true;
1876 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001877
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001878 if (Lex.getCode() != tgtok::equal)
1879 return TokError("expected '=' in let expression");
1880 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001881
David Greene8618f952009-06-08 20:23:18 +00001882 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001883 if (!Field)
David Greene8618f952009-06-08 20:23:18 +00001884 return TokError("Value '" + FieldName + "' unknown!");
1885
1886 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001887
David Greeneaf8ee2c2011-07-29 22:43:06 +00001888 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001889 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001890
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001891 if (Lex.getCode() != tgtok::semi)
1892 return TokError("expected ';' after let expression");
1893 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001894
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001895 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1896}
1897
1898/// ParseBody - Read the body of a class or def. Return true on error, false on
1899/// success.
1900///
1901/// Body ::= ';'
1902/// Body ::= '{' BodyList '}'
1903/// BodyList BodyItem*
1904///
1905bool TGParser::ParseBody(Record *CurRec) {
1906 // If this is a null definition, just eat the semi and return.
1907 if (Lex.getCode() == tgtok::semi) {
1908 Lex.Lex();
1909 return false;
1910 }
Bob Wilson7248f862009-11-22 04:24:42 +00001911
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001912 if (Lex.getCode() != tgtok::l_brace)
1913 return TokError("Expected ';' or '{' to start body");
1914 // Eat the '{'.
1915 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001916
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001917 while (Lex.getCode() != tgtok::r_brace)
1918 if (ParseBodyItem(CurRec))
1919 return true;
1920
1921 // Eat the '}'.
1922 Lex.Lex();
1923 return false;
1924}
1925
Sean Silvacb1a75e2013-01-09 04:49:14 +00001926/// \brief Apply the current let bindings to \a CurRec.
1927/// \returns true on error, false otherwise.
1928bool TGParser::ApplyLetStack(Record *CurRec) {
1929 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1930 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1931 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1932 LetStack[i][j].Bits, LetStack[i][j].Value))
1933 return true;
1934 return false;
1935}
1936
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001937/// ParseObjectBody - Parse the body of a def or class. This consists of an
1938/// optional ClassList followed by a Body. CurRec is the current def or class
1939/// that is being parsed.
1940///
1941/// ObjectBody ::= BaseClassList Body
1942/// BaseClassList ::= /*empty*/
1943/// BaseClassList ::= ':' BaseClassListNE
1944/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1945///
1946bool TGParser::ParseObjectBody(Record *CurRec) {
1947 // If there is a baseclass list, read it.
1948 if (Lex.getCode() == tgtok::colon) {
1949 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001950
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001951 // Read all of the subclasses.
1952 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1953 while (1) {
1954 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00001955 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001956
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001957 // Add it.
1958 if (AddSubClass(CurRec, SubClass))
1959 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001960
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001961 if (Lex.getCode() != tgtok::comma) break;
1962 Lex.Lex(); // eat ','.
1963 SubClass = ParseSubClassReference(CurRec, false);
1964 }
1965 }
1966
Sean Silvacb1a75e2013-01-09 04:49:14 +00001967 if (ApplyLetStack(CurRec))
1968 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001969
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001970 return ParseBody(CurRec);
1971}
1972
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001973/// ParseDef - Parse and return a top level or multiclass def, return the record
1974/// corresponding to it. This returns null on error.
1975///
1976/// DefInst ::= DEF ObjectName ObjectBody
1977///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00001978bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001979 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001980 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00001981 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001982
1983 // Parse ObjectName and make a record for it.
Jordan Roseabdd99b2013-01-10 18:50:05 +00001984 Record *CurRec;
1985 Init *Name = ParseObjectName(CurMultiClass);
1986 if (Name)
1987 CurRec = new Record(Name, DefLoc, Records);
1988 else
1989 CurRec = new Record(GetNewAnonymousName(), DefLoc, Records,
1990 /*IsAnonymous=*/true);
Bob Wilson7248f862009-11-22 04:24:42 +00001991
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00001992 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001993 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00001994
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001995 // Ensure redefinition doesn't happen.
David Greeneebb006f2012-01-28 00:03:24 +00001996 if (Records.getDef(CurRec->getNameInitAsString())) {
David Greene3a20f5a2011-10-19 13:03:45 +00001997 Error(DefLoc, "def '" + CurRec->getNameInitAsString()
1998 + "' already defined");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00001999 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002000 }
2001 Records.addDef(CurRec);
Hal Finkela8c1f462014-01-02 20:47:09 +00002002
2003 if (ParseObjectBody(CurRec))
2004 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002005 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002006 // Parse the body before adding this prototype to the DefPrototypes vector.
2007 // That way implicit definitions will be added to the DefPrototypes vector
2008 // before this object, instantiated prior to defs derived from this object,
2009 // and this available for indirect name resolution when defs derived from
2010 // this object are instantiated.
2011 if (ParseObjectBody(CurRec))
2012 return true;
2013
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002014 // Otherwise, a def inside a multiclass, add it to the multiclass.
2015 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene07e055f2011-10-19 13:03:51 +00002016 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2017 == CurRec->getNameInit()) {
2018 Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002019 "' already defined in this multiclass!");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002020 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002021 }
2022 CurMultiClass->DefPrototypes.push_back(CurRec);
Hal Finkela8c1f462014-01-02 20:47:09 +00002023 } else if (ParseObjectBody(CurRec))
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002024 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002025
Craig Topper011817a2014-04-09 04:50:04 +00002026 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002027 // See Record::setName(). This resolve step will see any new name
2028 // for the def that might have been created when resolving
2029 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002030 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002031
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002032 // If ObjectBody has template arguments, it's an error.
2033 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002034
2035 if (CurMultiClass) {
2036 // Copy the template arguments for the multiclass into the def.
David Greenedb10e692011-10-19 13:02:42 +00002037 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002038 CurMultiClass->Rec.getTemplateArgs();
2039
2040 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2041 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
2042 assert(RV && "Template arg doesn't exist?");
2043 CurRec->addValue(*RV);
2044 }
2045 }
2046
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002047 if (ProcessForeachDefs(CurRec, DefLoc)) {
David Greenefb927af2012-02-22 16:09:41 +00002048 Error(DefLoc,
2049 "Could not process loops for def" + CurRec->getNameInitAsString());
2050 return true;
2051 }
2052
2053 return false;
2054}
2055
2056/// ParseForeach - Parse a for statement. Return the record corresponding
2057/// to it. This returns true on error.
2058///
2059/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2060/// Foreach ::= FOREACH Declaration IN Object
2061///
2062bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2063 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2064 Lex.Lex(); // Eat the 'for' token.
2065
2066 // Make a temporary object to record items associated with the for
2067 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002068 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002069 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002070 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002071 return TokError("expected declaration in for");
2072
2073 if (Lex.getCode() != tgtok::In)
2074 return TokError("Unknown tok");
2075 Lex.Lex(); // Eat the in
2076
2077 // Create a loop object and remember it.
2078 Loops.push_back(ForeachLoop(IterName, ListValue));
2079
2080 if (Lex.getCode() != tgtok::l_brace) {
2081 // FOREACH Declaration IN Object
2082 if (ParseObject(CurMultiClass))
2083 return true;
2084 }
2085 else {
2086 SMLoc BraceLoc = Lex.getLoc();
2087 // Otherwise, this is a group foreach.
2088 Lex.Lex(); // eat the '{'.
2089
2090 // Parse the object list.
2091 if (ParseObjectList(CurMultiClass))
2092 return true;
2093
2094 if (Lex.getCode() != tgtok::r_brace) {
2095 TokError("expected '}' at end of foreach command");
2096 return Error(BraceLoc, "to match this '{'");
2097 }
2098 Lex.Lex(); // Eat the }
2099 }
2100
2101 // We've processed everything in this loop.
2102 Loops.pop_back();
2103
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002104 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002105}
2106
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002107/// ParseClass - Parse a tblgen class definition.
2108///
2109/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2110///
2111bool TGParser::ParseClass() {
2112 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2113 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002114
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002115 if (Lex.getCode() != tgtok::Id)
2116 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002117
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002118 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2119 if (CurRec) {
2120 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002121 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002122 !CurRec->getSuperClasses().empty() ||
2123 !CurRec->getTemplateArgs().empty())
David Greene8eed9982011-10-19 13:03:58 +00002124 return TokError("Class '" + CurRec->getNameInitAsString()
2125 + "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002126 } else {
2127 // If this is the first reference to this class, create and add it.
Chris Lattner89dcb682010-12-15 04:48:22 +00002128 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002129 Records.addClass(CurRec);
2130 }
2131 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002132
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002133 // If there are template args, parse them.
2134 if (Lex.getCode() == tgtok::less)
2135 if (ParseTemplateArgList(CurRec))
2136 return true;
2137
2138 // Finally, parse the object body.
2139 return ParseObjectBody(CurRec);
2140}
2141
2142/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2143/// of LetRecords.
2144///
2145/// LetList ::= LetItem (',' LetItem)*
2146/// LetItem ::= ID OptionalRangeList '=' Value
2147///
2148std::vector<LetRecord> TGParser::ParseLetList() {
2149 std::vector<LetRecord> Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002150
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002151 while (1) {
2152 if (Lex.getCode() != tgtok::Id) {
2153 TokError("expected identifier in let definition");
2154 return std::vector<LetRecord>();
2155 }
2156 std::string Name = Lex.getCurStrVal();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002157 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002158 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002159
2160 // Check for an optional RangeList.
2161 std::vector<unsigned> Bits;
Bob Wilson7248f862009-11-22 04:24:42 +00002162 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002163 return std::vector<LetRecord>();
2164 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002165
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002166 if (Lex.getCode() != tgtok::equal) {
2167 TokError("expected '=' in let expression");
2168 return std::vector<LetRecord>();
2169 }
2170 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002171
Craig Topper011817a2014-04-09 04:50:04 +00002172 Init *Val = ParseValue(nullptr);
2173 if (!Val) return std::vector<LetRecord>();
Bob Wilson7248f862009-11-22 04:24:42 +00002174
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002175 // Now that we have everything, add the record.
2176 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson7248f862009-11-22 04:24:42 +00002177
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002178 if (Lex.getCode() != tgtok::comma)
2179 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002180 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002181 }
2182}
2183
2184/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002185/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002186///
2187/// Object ::= LET LetList IN '{' ObjectList '}'
2188/// Object ::= LET LetList IN Object
2189///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002190bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002191 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2192 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002193
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002194 // Add this entry to the let stack.
2195 std::vector<LetRecord> LetInfo = ParseLetList();
2196 if (LetInfo.empty()) return true;
2197 LetStack.push_back(LetInfo);
2198
2199 if (Lex.getCode() != tgtok::In)
2200 return TokError("expected 'in' at end of top-level 'let'");
2201 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002202
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002203 // If this is a scalar let, just handle it now
2204 if (Lex.getCode() != tgtok::l_brace) {
2205 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002206 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002207 return true;
2208 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002209 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002210 // Otherwise, this is a group let.
2211 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002212
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002213 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002214 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002215 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002216
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002217 if (Lex.getCode() != tgtok::r_brace) {
2218 TokError("expected '}' at end of top level let command");
2219 return Error(BraceLoc, "to match this '{'");
2220 }
2221 Lex.Lex();
2222 }
Bob Wilson7248f862009-11-22 04:24:42 +00002223
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002224 // Outside this let scope, this let block is not active.
2225 LetStack.pop_back();
2226 return false;
2227}
2228
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002229/// ParseMultiClass - Parse a multiclass definition.
2230///
Bob Wilson3d948162009-04-28 19:41:44 +00002231/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002232/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2233/// MultiClassObject ::= DefInst
2234/// MultiClassObject ::= MultiClassInst
2235/// MultiClassObject ::= DefMInst
2236/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2237/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002238///
2239bool TGParser::ParseMultiClass() {
2240 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2241 Lex.Lex(); // Eat the multiclass token.
2242
2243 if (Lex.getCode() != tgtok::Id)
2244 return TokError("expected identifier after multiclass for name");
2245 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002246
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002247 if (MultiClasses.count(Name))
2248 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002249
Chris Lattner77d369c2010-12-13 00:23:57 +00002250 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
2251 Lex.getLoc(), Records);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002252 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002253
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002254 // If there are template args, parse them.
2255 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002256 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002257 return true;
2258
David Greene7049e792009-04-24 16:55:41 +00002259 bool inherits = false;
2260
David Greene753ed8f2009-04-22 16:42:54 +00002261 // If there are submulticlasses, parse them.
2262 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002263 inherits = true;
2264
David Greene753ed8f2009-04-22 16:42:54 +00002265 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002266
David Greene753ed8f2009-04-22 16:42:54 +00002267 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002268 SubMultiClassReference SubMultiClass =
2269 ParseSubMultiClassReference(CurMultiClass);
David Greene753ed8f2009-04-22 16:42:54 +00002270 while (1) {
2271 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002272 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002273
David Greene753ed8f2009-04-22 16:42:54 +00002274 // Add it.
2275 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2276 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002277
David Greene753ed8f2009-04-22 16:42:54 +00002278 if (Lex.getCode() != tgtok::comma) break;
2279 Lex.Lex(); // eat ','.
2280 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2281 }
2282 }
2283
David Greene7049e792009-04-24 16:55:41 +00002284 if (Lex.getCode() != tgtok::l_brace) {
2285 if (!inherits)
2286 return TokError("expected '{' in multiclass definition");
Bob Wilson7248f862009-11-22 04:24:42 +00002287 else if (Lex.getCode() != tgtok::semi)
2288 return TokError("expected ';' in multiclass definition");
David Greene7049e792009-04-24 16:55:41 +00002289 else
Bob Wilson7248f862009-11-22 04:24:42 +00002290 Lex.Lex(); // eat the ';'.
2291 } else {
David Greene7049e792009-04-24 16:55:41 +00002292 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2293 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002294
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002295 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002296 switch (Lex.getCode()) {
2297 default:
David Greene33f61992011-10-07 18:25:05 +00002298 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002299 case tgtok::Let:
2300 case tgtok::Def:
2301 case tgtok::Defm:
David Greenefb927af2012-02-22 16:09:41 +00002302 case tgtok::Foreach:
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002303 if (ParseObject(CurMultiClass))
2304 return true;
2305 break;
2306 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002307 }
David Greene7049e792009-04-24 16:55:41 +00002308 Lex.Lex(); // eat the '}'.
2309 }
Bob Wilson7248f862009-11-22 04:24:42 +00002310
Craig Topper011817a2014-04-09 04:50:04 +00002311 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002312 return false;
2313}
2314
David Greenedb445972011-10-05 22:42:07 +00002315Record *TGParser::
2316InstantiateMulticlassDef(MultiClass &MC,
2317 Record *DefProto,
Hal Finkelf2a0b2b2014-01-02 19:35:33 +00002318 Init *&DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002319 SMRange DefmPrefixRange) {
David Greene5d5d88c2011-10-19 13:04:31 +00002320 // We need to preserve DefProto so it can be reused for later
2321 // instantiations, so create a new Record to inherit from it.
2322
David Greenedb445972011-10-05 22:42:07 +00002323 // Add in the defm name. If the defm prefix is empty, give each
2324 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2325 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2326 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002327
Jordan Roseabdd99b2013-01-10 18:50:05 +00002328 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002329 if (!DefmPrefix) {
David Greene5d5d88c2011-10-19 13:04:31 +00002330 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002331 IsAnonymous = true;
2332 }
David Greene5d5d88c2011-10-19 13:04:31 +00002333
2334 Init *DefName = DefProto->getNameInit();
2335
Sean Silvafb509ed2012-10-10 20:24:43 +00002336 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002337
Craig Topper011817a2014-04-09 04:50:04 +00002338 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002339 // We have a fully expanded string so there are no operators to
2340 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002341 DefName =
2342 BinOpInit::get(BinOpInit::STRCONCAT,
2343 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2344 StringRecTy::get())->Fold(DefProto, &MC),
2345 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2346 }
David Greene5d5d88c2011-10-19 13:04:31 +00002347
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002348 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002349 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002350 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002351 Record *CurRec = new Record(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002352
2353 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002354 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002355 Ref.Rec = DefProto;
2356 AddSubClass(CurRec, Ref);
2357
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002358 // Set the value for NAME. We don't resolve references to it 'til later,
2359 // though, so that uses in nested multiclass names don't get
2360 // confused.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002361 if (SetValue(CurRec, Ref.RefRange.Start, "NAME", std::vector<unsigned>(),
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002362 DefmPrefix)) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002363 Error(DefmPrefixRange.Start, "Could not resolve "
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002364 + CurRec->getNameInitAsString() + ":NAME to '"
2365 + DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002366 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002367 }
David Greene8bf0d722011-10-19 13:04:35 +00002368
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002369 // If the DefNameString didn't resolve, we probably have a reference to
2370 // NAME and need to replace it. We need to do at least this much greedily,
2371 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002372 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002373 RecordVal *DefNameRV = CurRec->getValue("NAME");
2374 CurRec->resolveReferencesTo(DefNameRV);
2375 }
2376
2377 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002378 // Now that we're at the top level, resolve all NAME references
2379 // in the resultant defs that weren't in the def names themselves.
2380 RecordVal *DefNameRV = CurRec->getValue("NAME");
2381 CurRec->resolveReferencesTo(DefNameRV);
2382
2383 // Now that NAME references are resolved and we're at the top level of
2384 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002385 // currently in a multiclass, it means this defm appears inside a
2386 // multiclass and its name won't be fully resolvable until we see
2387 // the top-level defm. Therefore, we don't add this to the
2388 // RecordKeeper at this point. If we did we could get duplicate
2389 // defs as more than one probably refers to NAME or some other
2390 // common internal placeholder.
2391
2392 // Ensure redefinition doesn't happen.
2393 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002394 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002395 "' already defined, instantiating defm with subdef '" +
2396 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002397 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002398 }
2399
2400 Records.addDef(CurRec);
2401 }
2402
David Greenedb445972011-10-05 22:42:07 +00002403 return CurRec;
2404}
2405
2406bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2407 Record *CurRec,
2408 SMLoc DefmPrefixLoc,
2409 SMLoc SubClassLoc,
David Greenedb10e692011-10-19 13:02:42 +00002410 const std::vector<Init *> &TArgs,
David Greenedb445972011-10-05 22:42:07 +00002411 std::vector<Init *> &TemplateVals,
2412 bool DeleteArgs) {
2413 // Loop over all of the template arguments, setting them to the specified
2414 // value or leaving them as the default if necessary.
2415 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2416 // Check if a value is specified for this temp-arg.
2417 if (i < TemplateVals.size()) {
2418 // Set it now.
2419 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2420 TemplateVals[i]))
2421 return true;
2422
2423 // Resolve it next.
2424 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2425
2426 if (DeleteArgs)
2427 // Now remove it.
2428 CurRec->removeValue(TArgs[i]);
2429
2430 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2431 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenedb10e692011-10-19 13:02:42 +00002432 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2433 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2434 + "'");
David Greenedb445972011-10-05 22:42:07 +00002435 }
2436 }
2437 return false;
2438}
2439
2440bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2441 Record *CurRec,
2442 Record *DefProto,
2443 SMLoc DefmPrefixLoc) {
2444 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002445 if (ApplyLetStack(CurRec))
2446 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002447
David Greenedb445972011-10-05 22:42:07 +00002448 // Don't create a top level definition for defm inside multiclasses,
2449 // instead, only update the prototypes and bind the template args
2450 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002451 if (!CurMultiClass)
2452 return false;
2453 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2454 i != e; ++i)
2455 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2456 == CurRec->getNameInit())
2457 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2458 "' already defined in this multiclass!");
2459 CurMultiClass->DefPrototypes.push_back(CurRec);
David Greenedb445972011-10-05 22:42:07 +00002460
Sean Silvacc951b22013-01-09 05:28:12 +00002461 // Copy the template arguments for the multiclass into the new def.
2462 const std::vector<Init *> &TA =
2463 CurMultiClass->Rec.getTemplateArgs();
David Greenedb445972011-10-05 22:42:07 +00002464
Sean Silvacc951b22013-01-09 05:28:12 +00002465 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2466 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2467 assert(RV && "Template arg doesn't exist?");
2468 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002469 }
2470
2471 return false;
2472}
2473
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002474/// ParseDefm - Parse the instantiation of a multiclass.
2475///
2476/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2477///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002478bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002479 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002480 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002481 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002482
Craig Topperb21afc62013-01-07 05:09:33 +00002483 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002484 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002485 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002486
Jordan Rosef12e8a92013-01-10 18:50:11 +00002487 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002488 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002489 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002490
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002491 // Keep track of the new generated record definitions.
2492 std::vector<Record*> NewRecDefs;
2493
2494 // This record also inherits from a regular class (non-multiclass)?
2495 bool InheritFromClass = false;
2496
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002497 // eat the colon.
2498 Lex.Lex();
2499
Chris Lattner526c8cb2009-06-21 03:39:35 +00002500 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002501 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002502
2503 while (1) {
Craig Topper011817a2014-04-09 04:50:04 +00002504 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002505
2506 // To instantiate a multiclass, we need to first get the multiclass, then
2507 // instantiate each def contained in the multiclass with the SubClassRef
2508 // template parameters.
2509 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2510 assert(MC && "Didn't lookup multiclass correctly?");
David Greeneaf8ee2c2011-07-29 22:43:06 +00002511 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002512
2513 // Verify that the correct number of template arguments were specified.
David Greenedb10e692011-10-19 13:02:42 +00002514 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002515 if (TArgs.size() < TemplateVals.size())
2516 return Error(SubClassLoc,
2517 "more template args specified than multiclass expects");
2518
2519 // Loop over all the def's in the multiclass, instantiating each one.
2520 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2521 Record *DefProto = MC->DefPrototypes[i];
2522
Jordan Rosef12e8a92013-01-10 18:50:11 +00002523 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix,
2524 SMRange(DefmLoc,
2525 DefmPrefixEndLoc));
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002526 if (!CurRec)
2527 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002528
Jordan Rosef12e8a92013-01-10 18:50:11 +00002529 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002530 TArgs, TemplateVals, true/*Delete args*/))
2531 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002532
Jordan Rosef12e8a92013-01-10 18:50:11 +00002533 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002534 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002535
2536 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002537 }
2538
David Greenedb445972011-10-05 22:42:07 +00002539
David Greenef00919a2009-04-22 22:17:51 +00002540 if (Lex.getCode() != tgtok::comma) break;
2541 Lex.Lex(); // eat ','.
2542
Craig Topper998a39a2013-08-20 04:22:09 +00002543 if (Lex.getCode() != tgtok::Id)
2544 return TokError("expected identifier");
2545
David Greenef00919a2009-04-22 22:17:51 +00002546 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002547
2548 // A defm can inherit from regular classes (non-multiclass) as
2549 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002550 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002551
2552 if (InheritFromClass)
2553 break;
2554
Craig Topper011817a2014-04-09 04:50:04 +00002555 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002556 }
2557
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002558 if (InheritFromClass) {
2559 // Process all the classes to inherit as if they were part of a
2560 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002561 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002562 while (1) {
2563 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002564 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002565
2566 // Get the expanded definition prototypes and teach them about
2567 // the record values the current class to inherit has
2568 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2569 Record *CurRec = NewRecDefs[i];
2570
2571 // Add it.
2572 if (AddSubClass(CurRec, SubClass))
2573 return true;
2574
Sean Silvacb1a75e2013-01-09 04:49:14 +00002575 if (ApplyLetStack(CurRec))
2576 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002577 }
2578
2579 if (Lex.getCode() != tgtok::comma) break;
2580 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002581 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002582 }
2583 }
2584
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002585 if (!CurMultiClass)
2586 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene50c09122011-08-10 18:27:46 +00002587 // See Record::setName(). This resolve step will see any new
2588 // name for the def that might have been created when resolving
2589 // inheritance, values and arguments above.
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002590 NewRecDefs[i]->resolveReferences();
2591
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002592 if (Lex.getCode() != tgtok::semi)
2593 return TokError("expected ';' at end of defm");
2594 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002595
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002596 return false;
2597}
2598
2599/// ParseObject
2600/// Object ::= ClassInst
2601/// Object ::= DefInst
2602/// Object ::= MultiClassInst
2603/// Object ::= DefMInst
2604/// Object ::= LETCommand '{' ObjectList '}'
2605/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002606bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002607 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002608 default:
2609 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002610 case tgtok::Let: return ParseTopLevelLet(MC);
2611 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002612 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002613 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002614 case tgtok::Class: return ParseClass();
2615 case tgtok::MultiClass: return ParseMultiClass();
2616 }
2617}
2618
2619/// ParseObjectList
2620/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002621bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002622 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002623 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002624 return true;
2625 }
2626 return false;
2627}
2628
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002629bool TGParser::ParseFile() {
2630 Lex.Lex(); // Prime the lexer.
2631 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002632
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002633 // If we have unread input at the end of the file, report it.
2634 if (Lex.getCode() == tgtok::Eof)
2635 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002636
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002637 return TokError("Unexpected input at top level");
2638}
2639