blob: 04c4fc158ff7c1ec2effeb4fb495a478a4f92fe3 [file] [log] [blame]
Chris Lattnerf4601652007-11-22 20:49:04 +00001//===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-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 Lattnerf4601652007-11-22 20:49:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Parser for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
14#include "TGParser.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000015#include "llvm/TableGen/Record.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000016#include "llvm/ADT/StringExtras.h"
Daniel Dunbar1a551802009-07-03 00:10:29 +000017#include <algorithm>
18#include <sstream>
Chris Lattner8d978a72010-10-05 23:58:18 +000019#include "llvm/ADT/SmallVector.h"
Chris Lattner46f55522010-10-06 04:55:48 +000020#include "llvm/Support/CommandLine.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000021using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24// Support Code for the Semantic Actions.
25//===----------------------------------------------------------------------===//
26
27namespace llvm {
Chris Lattnerf4601652007-11-22 20:49:04 +000028struct SubClassReference {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000029 SMLoc RefLoc;
Chris Lattnerf4601652007-11-22 20:49:04 +000030 Record *Rec;
David Greene05bce0b2011-07-29 22:43:06 +000031 std::vector<Init*> TemplateArgs;
Chris Lattner1c8ae592009-03-13 16:01:53 +000032 SubClassReference() : Rec(0) {}
David Greened34a73b2009-04-24 16:55:41 +000033
Chris Lattnerf4601652007-11-22 20:49:04 +000034 bool isInvalid() const { return Rec == 0; }
35};
David Greenede444af2009-04-22 16:42:54 +000036
37struct SubMultiClassReference {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000038 SMLoc RefLoc;
David Greenede444af2009-04-22 16:42:54 +000039 MultiClass *MC;
David Greene05bce0b2011-07-29 22:43:06 +000040 std::vector<Init*> TemplateArgs;
David Greenede444af2009-04-22 16:42:54 +000041 SubMultiClassReference() : MC(0) {}
Bob Wilson32558652009-04-28 19:41:44 +000042
David Greenede444af2009-04-22 16:42:54 +000043 bool isInvalid() const { return MC == 0; }
David Greened34a73b2009-04-24 16:55:41 +000044 void dump() const;
David Greenede444af2009-04-22 16:42:54 +000045};
David Greened34a73b2009-04-24 16:55:41 +000046
47void SubMultiClassReference::dump() const {
Daniel Dunbar1a551802009-07-03 00:10:29 +000048 errs() << "Multiclass:\n";
Bob Wilson21870412009-11-22 04:24:42 +000049
David Greened34a73b2009-04-24 16:55:41 +000050 MC->dump();
Bob Wilson21870412009-11-22 04:24:42 +000051
Daniel Dunbar1a551802009-07-03 00:10:29 +000052 errs() << "Template args:\n";
David Greene05bce0b2011-07-29 22:43:06 +000053 for (std::vector<Init *>::const_iterator i = TemplateArgs.begin(),
David Greened34a73b2009-04-24 16:55:41 +000054 iend = TemplateArgs.end();
55 i != iend;
56 ++i) {
57 (*i)->dump();
58 }
59}
60
Chris Lattnerf4601652007-11-22 20:49:04 +000061} // end namespace llvm
62
Chris Lattner1e3a8a42009-06-21 03:39:35 +000063bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Chris Lattnerf4601652007-11-22 20:49:04 +000064 if (CurRec == 0)
65 CurRec = &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +000066
Jakob Stoklund Olesenebaf92c2012-01-13 03:16:35 +000067 if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
Chris Lattnerf4601652007-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 Wilson21870412009-11-22 04:24:42 +000072 "previous definition of type '" +
Chris Lattnerf4601652007-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 Greene917924d2011-10-19 13:02:39 +000082bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
David Greene05bce0b2011-07-29 22:43:06 +000083 const std::vector<unsigned> &BitList, Init *V) {
Chris Lattnerf4601652007-11-22 20:49:04 +000084 if (!V) return false;
85
86 if (CurRec == 0) CurRec = &CurMultiClass->Rec;
87
88 RecordVal *RV = CurRec->getValue(ValName);
89 if (RV == 0)
David Greene917924d2011-10-19 13:02:39 +000090 return Error(Loc, "Value '" + ValName->getAsUnquotedString()
91 + "' unknown!");
Chris Lattnerf4601652007-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())
David Greene05bce0b2011-07-29 22:43:06 +000096 if (VarInit *VI = dynamic_cast<VarInit*>(V))
David Greene917924d2011-10-19 13:02:39 +000097 if (VI->getNameInit() == ValName)
Chris Lattnerf4601652007-11-22 20:49:04 +000098 return false;
Bob Wilson21870412009-11-22 04:24:42 +000099
Chris Lattnerf4601652007-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()) {
David Greene05bce0b2011-07-29 22:43:06 +0000105 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
Chris Lattnerf4601652007-11-22 20:49:04 +0000106 if (CurVal == 0)
David Greene917924d2011-10-19 13:02:39 +0000107 return Error(Loc, "Value '" + ValName->getAsUnquotedString()
108 + "' is not a bits type");
Chris Lattnerf4601652007-11-22 20:49:04 +0000109
110 // Convert the incoming value to a bits type of the appropriate size...
David Greene05bce0b2011-07-29 22:43:06 +0000111 Init *BI = V->convertInitializerTo(BitsRecTy::get(BitList.size()));
Chris Lattnerf4601652007-11-22 20:49:04 +0000112 if (BI == 0) {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000113 V->convertInitializerTo(BitsRecTy::get(BitList.size()));
Chris Lattnerf4601652007-11-22 20:49:04 +0000114 return Error(Loc, "Initializer is not compatible with bit range");
115 }
Bob Wilson21870412009-11-22 04:24:42 +0000116
Chris Lattnerf4601652007-11-22 20:49:04 +0000117 // We should have a BitsInit type now.
David Greene05bce0b2011-07-29 22:43:06 +0000118 BitsInit *BInit = dynamic_cast<BitsInit*>(BI);
Chris Lattnerf4601652007-11-22 20:49:04 +0000119 assert(BInit != 0);
120
David Greene05bce0b2011-07-29 22:43:06 +0000121 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4601652007-11-22 20:49:04 +0000122
123 // Loop over bits, assigning values as appropriate.
124 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
125 unsigned Bit = BitList[i];
David Greeneca7fd3d2011-07-29 19:07:00 +0000126 if (NewBits[Bit])
Chris Lattnerf4601652007-11-22 20:49:04 +0000127 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
David Greene917924d2011-10-19 13:02:39 +0000128 ValName->getAsUnquotedString() + "' more than once");
David Greeneca7fd3d2011-07-29 19:07:00 +0000129 NewBits[Bit] = BInit->getBit(i);
Chris Lattnerf4601652007-11-22 20:49:04 +0000130 }
131
132 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
David Greeneca7fd3d2011-07-29 19:07:00 +0000133 if (NewBits[i] == 0)
134 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4601652007-11-22 20:49:04 +0000135
David Greenedcd35c72011-07-29 19:07:07 +0000136 V = BitsInit::get(NewBits);
Chris Lattnerf4601652007-11-22 20:49:04 +0000137 }
138
139 if (RV->setValue(V))
David Greene917924d2011-10-19 13:02:39 +0000140 return Error(Loc, "Value '" + ValName->getAsUnquotedString() + "' of type '"
141 + RV->getType()->getAsString() +
142 "' is incompatible with initializer '" + V->getAsString()
143 + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +0000144 return false;
145}
146
147/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
148/// args as SubClass's template arguments.
Cedric Venetaff9c272009-02-14 16:06:42 +0000149bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000150 Record *SC = SubClass.Rec;
151 // Add all of the values in the subclass into the current class.
152 const std::vector<RecordVal> &Vals = SC->getValues();
153 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
154 if (AddValue(CurRec, SubClass.RefLoc, Vals[i]))
155 return true;
156
David Greenee22b3212011-10-19 13:02:42 +0000157 const std::vector<Init *> &TArgs = SC->getTemplateArgs();
Chris Lattnerf4601652007-11-22 20:49:04 +0000158
159 // Ensure that an appropriate number of template arguments are specified.
160 if (TArgs.size() < SubClass.TemplateArgs.size())
161 return Error(SubClass.RefLoc, "More template args specified than expected");
Bob Wilson21870412009-11-22 04:24:42 +0000162
Chris Lattnerf4601652007-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.
Bob Wilson21870412009-11-22 04:24:42 +0000168 if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(),
Chris Lattnerf4601652007-11-22 20:49:04 +0000169 SubClass.TemplateArgs[i]))
170 return true;
Bob Wilson21870412009-11-22 04:24:42 +0000171
Chris Lattnerf4601652007-11-22 20:49:04 +0000172 // Resolve it next.
173 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson21870412009-11-22 04:24:42 +0000174
Chris Lattnerf4601652007-11-22 20:49:04 +0000175 // Now remove it.
176 CurRec->removeValue(TArgs[i]);
177
178 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
179 return Error(SubClass.RefLoc,"Value not specified for template argument #"
David Greenee22b3212011-10-19 13:02:42 +0000180 + utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
181 + ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4601652007-11-22 20:49:04 +0000182 }
183 }
184
185 // Since everything went well, we can now set the "superclass" list for the
186 // current record.
187 const std::vector<Record*> &SCs = SC->getSuperClasses();
188 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
189 if (CurRec->isSubClassOf(SCs[i]))
190 return Error(SubClass.RefLoc,
191 "Already subclass of '" + SCs[i]->getName() + "'!\n");
192 CurRec->addSuperClass(SCs[i]);
193 }
Bob Wilson21870412009-11-22 04:24:42 +0000194
Chris Lattnerf4601652007-11-22 20:49:04 +0000195 if (CurRec->isSubClassOf(SC))
196 return Error(SubClass.RefLoc,
197 "Already subclass of '" + SC->getName() + "'!\n");
198 CurRec->addSuperClass(SC);
199 return false;
200}
201
David Greenede444af2009-04-22 16:42:54 +0000202/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilson440548d2009-04-30 18:26:19 +0000203/// CurMC, resolving its template args as SubMultiClass's
David Greenede444af2009-04-22 16:42:54 +0000204/// template arguments.
Bob Wilson440548d2009-04-30 18:26:19 +0000205bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson1d512df2009-04-30 17:46:20 +0000206 SubMultiClassReference &SubMultiClass) {
David Greenede444af2009-04-22 16:42:54 +0000207 MultiClass *SMC = SubMultiClass.MC;
Bob Wilson440548d2009-04-30 18:26:19 +0000208 Record *CurRec = &CurMC->Rec;
David Greenede444af2009-04-22 16:42:54 +0000209
Bob Wilson440548d2009-04-30 18:26:19 +0000210 const std::vector<RecordVal> &MCVals = CurRec->getValues();
David Greenede444af2009-04-22 16:42:54 +0000211
212 // Add all of the values in the subclass into the current class.
213 const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues();
214 for (unsigned i = 0, e = SMCVals.size(); i != e; ++i)
215 if (AddValue(CurRec, SubMultiClass.RefLoc, SMCVals[i]))
216 return true;
217
Bob Wilson440548d2009-04-30 18:26:19 +0000218 int newDefStart = CurMC->DefPrototypes.size();
David Greened34a73b2009-04-24 16:55:41 +0000219
David Greenede444af2009-04-22 16:42:54 +0000220 // Add all of the defs in the subclass into the current multiclass.
221 for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(),
222 iend = SMC->DefPrototypes.end();
223 i != iend;
224 ++i) {
225 // Clone the def and add it to the current multiclass
226 Record *NewDef = new Record(**i);
227
228 // Add all of the values in the superclass into the current def.
229 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
230 if (AddValue(NewDef, SubMultiClass.RefLoc, MCVals[i]))
231 return true;
232
Bob Wilson440548d2009-04-30 18:26:19 +0000233 CurMC->DefPrototypes.push_back(NewDef);
David Greenede444af2009-04-22 16:42:54 +0000234 }
Bob Wilson32558652009-04-28 19:41:44 +0000235
David Greenee22b3212011-10-19 13:02:42 +0000236 const std::vector<Init *> &SMCTArgs = SMC->Rec.getTemplateArgs();
David Greenede444af2009-04-22 16:42:54 +0000237
David Greened34a73b2009-04-24 16:55:41 +0000238 // Ensure that an appropriate number of template arguments are
239 // specified.
David Greenede444af2009-04-22 16:42:54 +0000240 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
David Greened34a73b2009-04-24 16:55:41 +0000241 return Error(SubMultiClass.RefLoc,
242 "More template args specified than expected");
Bob Wilson32558652009-04-28 19:41:44 +0000243
David Greenede444af2009-04-22 16:42:54 +0000244 // Loop over all of the template arguments, setting them to the specified
245 // value or leaving them as the default if necessary.
246 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
247 if (i < SubMultiClass.TemplateArgs.size()) {
David Greened34a73b2009-04-24 16:55:41 +0000248 // If a value is specified for this template arg, set it in the
249 // superclass now.
250 if (SetValue(CurRec, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000251 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000252 SubMultiClass.TemplateArgs[i]))
253 return true;
254
255 // Resolve it next.
256 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson32558652009-04-28 19:41:44 +0000257
David Greenede444af2009-04-22 16:42:54 +0000258 // Now remove it.
259 CurRec->removeValue(SMCTArgs[i]);
260
David Greened34a73b2009-04-24 16:55:41 +0000261 // If a value is specified for this template arg, set it in the
262 // new defs now.
263 for (MultiClass::RecordVector::iterator j =
Bob Wilson440548d2009-04-30 18:26:19 +0000264 CurMC->DefPrototypes.begin() + newDefStart,
265 jend = CurMC->DefPrototypes.end();
David Greenede444af2009-04-22 16:42:54 +0000266 j != jend;
267 ++j) {
268 Record *Def = *j;
269
David Greened34a73b2009-04-24 16:55:41 +0000270 if (SetValue(Def, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000271 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000272 SubMultiClass.TemplateArgs[i]))
273 return true;
274
275 // Resolve it next.
276 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
277
278 // Now remove it
279 Def->removeValue(SMCTArgs[i]);
280 }
281 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
David Greened34a73b2009-04-24 16:55:41 +0000282 return Error(SubMultiClass.RefLoc,
283 "Value not specified for template argument #"
David Greenee22b3212011-10-19 13:02:42 +0000284 + utostr(i) + " (" + SMCTArgs[i]->getAsUnquotedString()
285 + ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greenede444af2009-04-22 16:42:54 +0000286 }
287 }
288
289 return false;
290}
291
David Greenecebb4ee2012-02-22 16:09:41 +0000292/// ProcessForeachDefs - Given a record, apply all of the variable
293/// values in all surrounding foreach loops, creating new records for
294/// each combination of values.
295bool TGParser::ProcessForeachDefs(Record *CurRec, MultiClass *CurMultiClass,
296 SMLoc Loc) {
297 // We want to instantiate a new copy of CurRec for each combination
298 // of nested loop iterator values. We don't want top instantiate
299 // any copies until we have values for each loop iterator.
300 IterSet IterVals;
301 for (LoopVector::iterator Loop = Loops.begin(), LoopEnd = Loops.end();
302 Loop != LoopEnd;
303 ++Loop) {
304 // Process this loop.
305 if (ProcessForeachDefs(CurRec, CurMultiClass, Loc,
306 IterVals, *Loop, Loop+1)) {
307 Error(Loc,
308 "Could not process loops for def " + CurRec->getNameInitAsString());
309 return true;
310 }
311 }
312
313 return false;
314}
315
316/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
317/// apply each of the variable values in this loop and then process
318/// subloops.
319bool TGParser::ProcessForeachDefs(Record *CurRec, MultiClass *CurMultiClass,
320 SMLoc Loc, IterSet &IterVals,
321 ForeachLoop &CurLoop,
322 LoopVector::iterator NextLoop) {
323 Init *IterVar = CurLoop.IterVar;
324 ListInit *List = dynamic_cast<ListInit *>(CurLoop.ListValue);
325
326 if (List == 0) {
327 Error(Loc, "Loop list is not a list");
328 return true;
329 }
330
331 // Process each value.
332 for (int64_t i = 0; i < List->getSize(); ++i) {
333 Init *ItemVal = List->resolveListElementReference(*CurRec, 0, i);
334 IterVals.push_back(IterRecord(IterVar, ItemVal));
335
336 if (IterVals.size() == Loops.size()) {
337 // Ok, we have all of the iterator values for this point in the
338 // iteration space. Instantiate a new record to reflect this
339 // combination of values.
340 Record *IterRec = new Record(*CurRec);
341
342 // Set the iterator values now.
343 for (IterSet::iterator i = IterVals.begin(), iend = IterVals.end();
344 i != iend;
345 ++i) {
346 VarInit *IterVar = dynamic_cast<VarInit *>(i->IterVar);
347 if (IterVar == 0) {
348 Error(Loc, "foreach iterator is unresolved");
349 return true;
350 }
351
352 TypedInit *IVal = dynamic_cast<TypedInit *>(i->IterValue);
353 if (IVal == 0) {
354 Error(Loc, "foreach iterator value is untyped");
355 return true;
356 }
357
358 IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false));
359
360 if (SetValue(IterRec, Loc, IterVar->getName(),
361 std::vector<unsigned>(), IVal)) {
362 Error(Loc, "when instantiating this def");
363 return true;
364 }
365
366 // Resolve it next.
367 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName()));
368
369 // Remove it.
370 IterRec->removeValue(IterVar->getName());
371 }
372
373 if (Records.getDef(IterRec->getNameInitAsString())) {
374 Error(Loc, "def already exists: " + IterRec->getNameInitAsString());
375 return true;
376 }
377
378 Records.addDef(IterRec);
379 IterRec->resolveReferences();
380 }
381
382 if (NextLoop != Loops.end()) {
383 // Process nested loops.
384 if (ProcessForeachDefs(CurRec, CurMultiClass, Loc, IterVals, *NextLoop,
385 NextLoop+1)) {
386 Error(Loc,
387 "Could not process loops for def " +
388 CurRec->getNameInitAsString());
389 return true;
390 }
391 }
392
393 // We're done with this iterator.
394 IterVals.pop_back();
395 }
396 return false;
397}
398
Chris Lattnerf4601652007-11-22 20:49:04 +0000399//===----------------------------------------------------------------------===//
400// Parser Code
401//===----------------------------------------------------------------------===//
402
403/// isObjectStart - Return true if this is a valid first token for an Object.
404static bool isObjectStart(tgtok::TokKind K) {
405 return K == tgtok::Class || K == tgtok::Def ||
David Greenecebb4ee2012-02-22 16:09:41 +0000406 K == tgtok::Defm || K == tgtok::Let ||
407 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4601652007-11-22 20:49:04 +0000408}
409
Chris Lattnerdf72eae2010-10-05 22:51:56 +0000410static std::string GetNewAnonymousName() {
411 static unsigned AnonCounter = 0;
412 return "anonymous."+utostr(AnonCounter++);
413}
414
Chris Lattnerf4601652007-11-22 20:49:04 +0000415/// ParseObjectName - If an object name is specified, return it. Otherwise,
416/// return an anonymous name.
David Greenea9e07dd2011-10-19 13:04:29 +0000417/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4601652007-11-22 20:49:04 +0000418/// ObjectName ::= /*empty*/
419///
David Greenea9e07dd2011-10-19 13:04:29 +0000420Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
421 switch (Lex.getCode()) {
422 case tgtok::colon:
423 case tgtok::semi:
424 case tgtok::l_brace:
425 // These are all of the tokens that can begin an object body.
426 // Some of these can also begin values but we disallow those cases
427 // because they are unlikely to be useful.
428 return StringInit::get(GetNewAnonymousName());
David Greenea9e07dd2011-10-19 13:04:29 +0000429 default:
430 break;
431 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000432
David Greenea9e07dd2011-10-19 13:04:29 +0000433 Record *CurRec = 0;
434 if (CurMultiClass)
435 CurRec = &CurMultiClass->Rec;
436
437 RecTy *Type = 0;
438 if (CurRec) {
439 const TypedInit *CurRecName =
440 dynamic_cast<const TypedInit *>(CurRec->getNameInit());
441 if (!CurRecName) {
442 TokError("Record name is not typed!");
443 return 0;
444 }
445 Type = CurRecName->getType();
446 }
447
448 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4601652007-11-22 20:49:04 +0000449}
450
Chris Lattnerf4601652007-11-22 20:49:04 +0000451/// ParseClassID - Parse and resolve a reference to a class name. This returns
452/// null on error.
453///
454/// ClassID ::= ID
455///
456Record *TGParser::ParseClassID() {
457 if (Lex.getCode() != tgtok::Id) {
458 TokError("expected name for ClassID");
459 return 0;
460 }
Bob Wilson21870412009-11-22 04:24:42 +0000461
Chris Lattnerf4601652007-11-22 20:49:04 +0000462 Record *Result = Records.getClass(Lex.getCurStrVal());
463 if (Result == 0)
464 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson21870412009-11-22 04:24:42 +0000465
Chris Lattnerf4601652007-11-22 20:49:04 +0000466 Lex.Lex();
467 return Result;
468}
469
Bob Wilson32558652009-04-28 19:41:44 +0000470/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
471/// This returns null on error.
David Greenede444af2009-04-22 16:42:54 +0000472///
473/// MultiClassID ::= ID
474///
475MultiClass *TGParser::ParseMultiClassID() {
476 if (Lex.getCode() != tgtok::Id) {
477 TokError("expected name for ClassID");
478 return 0;
479 }
Bob Wilson32558652009-04-28 19:41:44 +0000480
David Greenede444af2009-04-22 16:42:54 +0000481 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
482 if (Result == 0)
483 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson32558652009-04-28 19:41:44 +0000484
David Greenede444af2009-04-22 16:42:54 +0000485 Lex.Lex();
486 return Result;
487}
488
Chris Lattnerf4601652007-11-22 20:49:04 +0000489Record *TGParser::ParseDefmID() {
490 if (Lex.getCode() != tgtok::Id) {
491 TokError("expected multiclass name");
492 return 0;
493 }
Bob Wilson21870412009-11-22 04:24:42 +0000494
Chris Lattnerf4601652007-11-22 20:49:04 +0000495 MultiClass *MC = MultiClasses[Lex.getCurStrVal()];
496 if (MC == 0) {
497 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
498 return 0;
499 }
Bob Wilson21870412009-11-22 04:24:42 +0000500
Chris Lattnerf4601652007-11-22 20:49:04 +0000501 Lex.Lex();
502 return &MC->Rec;
Bob Wilson21870412009-11-22 04:24:42 +0000503}
Chris Lattnerf4601652007-11-22 20:49:04 +0000504
505
506/// ParseSubClassReference - Parse a reference to a subclass or to a templated
507/// subclass. This returns a SubClassRefTy with a null Record* on error.
508///
509/// SubClassRef ::= ClassID
510/// SubClassRef ::= ClassID '<' ValueList '>'
511///
512SubClassReference TGParser::
513ParseSubClassReference(Record *CurRec, bool isDefm) {
514 SubClassReference Result;
515 Result.RefLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +0000516
Chris Lattnerf4601652007-11-22 20:49:04 +0000517 if (isDefm)
518 Result.Rec = ParseDefmID();
519 else
520 Result.Rec = ParseClassID();
521 if (Result.Rec == 0) return Result;
Bob Wilson21870412009-11-22 04:24:42 +0000522
Chris Lattnerf4601652007-11-22 20:49:04 +0000523 // If there is no template arg list, we're done.
524 if (Lex.getCode() != tgtok::less)
525 return Result;
526 Lex.Lex(); // Eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000527
Chris Lattnerf4601652007-11-22 20:49:04 +0000528 if (Lex.getCode() == tgtok::greater) {
529 TokError("subclass reference requires a non-empty list of template values");
530 Result.Rec = 0;
531 return Result;
532 }
Bob Wilson21870412009-11-22 04:24:42 +0000533
David Greenee1b46912009-06-08 20:23:18 +0000534 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4601652007-11-22 20:49:04 +0000535 if (Result.TemplateArgs.empty()) {
536 Result.Rec = 0; // Error parsing value list.
537 return Result;
538 }
Bob Wilson21870412009-11-22 04:24:42 +0000539
Chris Lattnerf4601652007-11-22 20:49:04 +0000540 if (Lex.getCode() != tgtok::greater) {
541 TokError("expected '>' in template value list");
542 Result.Rec = 0;
543 return Result;
544 }
545 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000546
Chris Lattnerf4601652007-11-22 20:49:04 +0000547 return Result;
548}
549
Bob Wilson32558652009-04-28 19:41:44 +0000550/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
551/// templated submulticlass. This returns a SubMultiClassRefTy with a null
552/// Record* on error.
David Greenede444af2009-04-22 16:42:54 +0000553///
554/// SubMultiClassRef ::= MultiClassID
555/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
556///
557SubMultiClassReference TGParser::
558ParseSubMultiClassReference(MultiClass *CurMC) {
559 SubMultiClassReference Result;
560 Result.RefLoc = Lex.getLoc();
Bob Wilson32558652009-04-28 19:41:44 +0000561
David Greenede444af2009-04-22 16:42:54 +0000562 Result.MC = ParseMultiClassID();
563 if (Result.MC == 0) return Result;
Bob Wilson32558652009-04-28 19:41:44 +0000564
David Greenede444af2009-04-22 16:42:54 +0000565 // If there is no template arg list, we're done.
566 if (Lex.getCode() != tgtok::less)
567 return Result;
568 Lex.Lex(); // Eat the '<'
Bob Wilson32558652009-04-28 19:41:44 +0000569
David Greenede444af2009-04-22 16:42:54 +0000570 if (Lex.getCode() == tgtok::greater) {
571 TokError("subclass reference requires a non-empty list of template values");
572 Result.MC = 0;
573 return Result;
574 }
Bob Wilson32558652009-04-28 19:41:44 +0000575
David Greenee1b46912009-06-08 20:23:18 +0000576 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greenede444af2009-04-22 16:42:54 +0000577 if (Result.TemplateArgs.empty()) {
578 Result.MC = 0; // Error parsing value list.
579 return Result;
580 }
Bob Wilson32558652009-04-28 19:41:44 +0000581
David Greenede444af2009-04-22 16:42:54 +0000582 if (Lex.getCode() != tgtok::greater) {
583 TokError("expected '>' in template value list");
584 Result.MC = 0;
585 return Result;
586 }
587 Lex.Lex();
588
589 return Result;
590}
591
Chris Lattnerf4601652007-11-22 20:49:04 +0000592/// ParseRangePiece - Parse a bit/value range.
593/// RangePiece ::= INTVAL
594/// RangePiece ::= INTVAL '-' INTVAL
595/// RangePiece ::= INTVAL INTVAL
596bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner811281e2008-01-10 07:01:53 +0000597 if (Lex.getCode() != tgtok::IntVal) {
598 TokError("expected integer or bitrange");
599 return true;
600 }
Dan Gohman63f97202008-10-17 01:33:43 +0000601 int64_t Start = Lex.getCurIntVal();
602 int64_t End;
Bob Wilson21870412009-11-22 04:24:42 +0000603
Chris Lattnerf4601652007-11-22 20:49:04 +0000604 if (Start < 0)
605 return TokError("invalid range, cannot be negative");
Bob Wilson21870412009-11-22 04:24:42 +0000606
Chris Lattnerf4601652007-11-22 20:49:04 +0000607 switch (Lex.Lex()) { // eat first character.
Bob Wilson21870412009-11-22 04:24:42 +0000608 default:
Chris Lattnerf4601652007-11-22 20:49:04 +0000609 Ranges.push_back(Start);
610 return false;
611 case tgtok::minus:
612 if (Lex.Lex() != tgtok::IntVal) {
613 TokError("expected integer value as end of range");
614 return true;
615 }
616 End = Lex.getCurIntVal();
617 break;
618 case tgtok::IntVal:
619 End = -Lex.getCurIntVal();
620 break;
621 }
Bob Wilson21870412009-11-22 04:24:42 +0000622 if (End < 0)
Chris Lattnerf4601652007-11-22 20:49:04 +0000623 return TokError("invalid range, cannot be negative");
624 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000625
Chris Lattnerf4601652007-11-22 20:49:04 +0000626 // Add to the range.
627 if (Start < End) {
628 for (; Start <= End; ++Start)
629 Ranges.push_back(Start);
630 } else {
631 for (; Start >= End; --Start)
632 Ranges.push_back(Start);
633 }
634 return false;
635}
636
637/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
638///
639/// RangeList ::= RangePiece (',' RangePiece)*
640///
641std::vector<unsigned> TGParser::ParseRangeList() {
642 std::vector<unsigned> Result;
Bob Wilson21870412009-11-22 04:24:42 +0000643
Chris Lattnerf4601652007-11-22 20:49:04 +0000644 // Parse the first piece.
645 if (ParseRangePiece(Result))
646 return std::vector<unsigned>();
647 while (Lex.getCode() == tgtok::comma) {
648 Lex.Lex(); // Eat the comma.
649
650 // Parse the next range piece.
651 if (ParseRangePiece(Result))
652 return std::vector<unsigned>();
653 }
654 return Result;
655}
656
657/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
658/// OptionalRangeList ::= '<' RangeList '>'
659/// OptionalRangeList ::= /*empty*/
660bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
661 if (Lex.getCode() != tgtok::less)
662 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000663
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000664 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000665 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000666
Chris Lattnerf4601652007-11-22 20:49:04 +0000667 // Parse the range list.
668 Ranges = ParseRangeList();
669 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000670
Chris Lattnerf4601652007-11-22 20:49:04 +0000671 if (Lex.getCode() != tgtok::greater) {
672 TokError("expected '>' at end of range list");
673 return Error(StartLoc, "to match this '<'");
674 }
675 Lex.Lex(); // eat the '>'.
676 return false;
677}
678
679/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
680/// OptionalBitList ::= '{' RangeList '}'
681/// OptionalBitList ::= /*empty*/
682bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
683 if (Lex.getCode() != tgtok::l_brace)
684 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000685
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000686 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000687 Lex.Lex(); // eat the '{'
Bob Wilson21870412009-11-22 04:24:42 +0000688
Chris Lattnerf4601652007-11-22 20:49:04 +0000689 // Parse the range list.
690 Ranges = ParseRangeList();
691 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000692
Chris Lattnerf4601652007-11-22 20:49:04 +0000693 if (Lex.getCode() != tgtok::r_brace) {
694 TokError("expected '}' at end of bit list");
695 return Error(StartLoc, "to match this '{'");
696 }
697 Lex.Lex(); // eat the '}'.
698 return false;
699}
700
701
702/// ParseType - Parse and return a tblgen type. This returns null on error.
703///
704/// Type ::= STRING // string type
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +0000705/// Type ::= CODE // code type
Chris Lattnerf4601652007-11-22 20:49:04 +0000706/// Type ::= BIT // bit type
707/// Type ::= BITS '<' INTVAL '>' // bits<x> type
708/// Type ::= INT // int type
709/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4601652007-11-22 20:49:04 +0000710/// Type ::= DAG // dag type
711/// Type ::= ClassID // Record Type
712///
713RecTy *TGParser::ParseType() {
714 switch (Lex.getCode()) {
715 default: TokError("Unknown token when expecting a type"); return 0;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000716 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +0000717 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000718 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
719 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000720 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4601652007-11-22 20:49:04 +0000721 case tgtok::Id:
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000722 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Chris Lattnerf4601652007-11-22 20:49:04 +0000723 return 0;
724 case tgtok::Bits: {
725 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
726 TokError("expected '<' after bits type");
727 return 0;
728 }
729 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
730 TokError("expected integer in bits<n> type");
731 return 0;
732 }
Dan Gohman63f97202008-10-17 01:33:43 +0000733 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4601652007-11-22 20:49:04 +0000734 if (Lex.Lex() != tgtok::greater) { // Eat count.
735 TokError("expected '>' at end of bits<n> type");
736 return 0;
737 }
738 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000739 return BitsRecTy::get(Val);
Chris Lattnerf4601652007-11-22 20:49:04 +0000740 }
741 case tgtok::List: {
742 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
743 TokError("expected '<' after list type");
744 return 0;
745 }
746 Lex.Lex(); // Eat '<'
747 RecTy *SubType = ParseType();
748 if (SubType == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +0000749
Chris Lattnerf4601652007-11-22 20:49:04 +0000750 if (Lex.getCode() != tgtok::greater) {
751 TokError("expected '>' at end of list<ty> type");
752 return 0;
753 }
754 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000755 return ListRecTy::get(SubType);
Chris Lattnerf4601652007-11-22 20:49:04 +0000756 }
Bob Wilson21870412009-11-22 04:24:42 +0000757 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000758}
759
760/// ParseIDValue - Parse an ID as a value and decode what it means.
761///
762/// IDValue ::= ID [def local value]
763/// IDValue ::= ID [def template arg]
764/// IDValue ::= ID [multiclass local value]
765/// IDValue ::= ID [multiclass template argument]
766/// IDValue ::= ID [def name]
767///
David Greenef3744a02011-10-19 13:04:20 +0000768Init *TGParser::ParseIDValue(Record *CurRec, IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000769 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
770 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000771 SMLoc Loc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000772 Lex.Lex();
773 return ParseIDValue(CurRec, Name, Loc);
774}
775
776/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
777/// has already been read.
David Greene05bce0b2011-07-29 22:43:06 +0000778Init *TGParser::ParseIDValue(Record *CurRec,
David Greenef3744a02011-10-19 13:04:20 +0000779 const std::string &Name, SMLoc NameLoc,
780 IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000781 if (CurRec) {
782 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenedcd35c72011-07-29 19:07:07 +0000783 return VarInit::get(Name, RV->getType());
Bob Wilson21870412009-11-22 04:24:42 +0000784
David Greenee22b3212011-10-19 13:02:42 +0000785 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
786
David Greenecaa25c82011-10-05 22:42:54 +0000787 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +0000788 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
789 "::");
David Greenecaa25c82011-10-05 22:42:54 +0000790
Chris Lattnerf4601652007-11-22 20:49:04 +0000791 if (CurRec->isTemplateArg(TemplateArgName)) {
792 const RecordVal *RV = CurRec->getValue(TemplateArgName);
793 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000794 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000795 }
796 }
Bob Wilson21870412009-11-22 04:24:42 +0000797
Chris Lattnerf4601652007-11-22 20:49:04 +0000798 if (CurMultiClass) {
David Greenee22b3212011-10-19 13:02:42 +0000799 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
800 "::");
801
Chris Lattnerf4601652007-11-22 20:49:04 +0000802 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
803 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
804 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000805 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000806 }
807 }
Bob Wilson21870412009-11-22 04:24:42 +0000808
David Greenecebb4ee2012-02-22 16:09:41 +0000809 // If this is in a foreach loop, make sure it's not a loop iterator
810 for (LoopVector::iterator i = Loops.begin(), iend = Loops.end();
811 i != iend;
812 ++i) {
813 VarInit *IterVar = dynamic_cast<VarInit *>(i->IterVar);
814 if (IterVar && IterVar->getName() == Name)
815 return IterVar;
816 }
817
David Greenebbec2792011-10-19 13:04:21 +0000818 if (Mode == ParseNameMode)
819 return StringInit::get(Name);
820
Chris Lattnerf4601652007-11-22 20:49:04 +0000821 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000822 return DefInit::get(D);
Chris Lattnerf4601652007-11-22 20:49:04 +0000823
David Greenebbec2792011-10-19 13:04:21 +0000824 if (Mode == ParseValueMode) {
825 Error(NameLoc, "Variable not defined: '" + Name + "'");
826 return 0;
827 }
828
829 return StringInit::get(Name);
Chris Lattnerf4601652007-11-22 20:49:04 +0000830}
831
David Greened418c1b2009-05-14 20:54:48 +0000832/// ParseOperation - Parse an operator. This returns null on error.
833///
834/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
835///
David Greene05bce0b2011-07-29 22:43:06 +0000836Init *TGParser::ParseOperation(Record *CurRec) {
David Greened418c1b2009-05-14 20:54:48 +0000837 switch (Lex.getCode()) {
838 default:
839 TokError("unknown operation");
840 return 0;
David Greene1434f662011-01-07 17:05:37 +0000841 case tgtok::XHead:
842 case tgtok::XTail:
843 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +0000844 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
845 UnOpInit::UnaryOp Code;
846 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000847
David Greenee6c27de2009-05-14 21:22:49 +0000848 switch (Lex.getCode()) {
Craig Topper85814382012-02-07 05:05:23 +0000849 default: llvm_unreachable("Unhandled code!");
David Greenee6c27de2009-05-14 21:22:49 +0000850 case tgtok::XCast:
851 Lex.Lex(); // eat the operation
852 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000853
David Greenee6c27de2009-05-14 21:22:49 +0000854 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000855
David Greenee6c27de2009-05-14 21:22:49 +0000856 if (Type == 0) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000857 TokError("did not get type for unary operator");
David Greenee6c27de2009-05-14 21:22:49 +0000858 return 0;
859 }
David Greened418c1b2009-05-14 20:54:48 +0000860
David Greenee6c27de2009-05-14 21:22:49 +0000861 break;
David Greene1434f662011-01-07 17:05:37 +0000862 case tgtok::XHead:
David Greene5f9f9ba2009-05-14 22:38:31 +0000863 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000864 Code = UnOpInit::HEAD;
David Greene5f9f9ba2009-05-14 22:38:31 +0000865 break;
David Greene1434f662011-01-07 17:05:37 +0000866 case tgtok::XTail:
David Greene5f9f9ba2009-05-14 22:38:31 +0000867 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000868 Code = UnOpInit::TAIL;
David Greene5f9f9ba2009-05-14 22:38:31 +0000869 break;
David Greene1434f662011-01-07 17:05:37 +0000870 case tgtok::XEmpty:
David Greene5f9f9ba2009-05-14 22:38:31 +0000871 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000872 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000873 Type = IntRecTy::get();
David Greene5f9f9ba2009-05-14 22:38:31 +0000874 break;
David Greenee6c27de2009-05-14 21:22:49 +0000875 }
876 if (Lex.getCode() != tgtok::l_paren) {
877 TokError("expected '(' after unary operator");
878 return 0;
879 }
880 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000881
David Greene05bce0b2011-07-29 22:43:06 +0000882 Init *LHS = ParseValue(CurRec);
David Greenee6c27de2009-05-14 21:22:49 +0000883 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000884
David Greene1434f662011-01-07 17:05:37 +0000885 if (Code == UnOpInit::HEAD
886 || Code == UnOpInit::TAIL
887 || Code == UnOpInit::EMPTY) {
David Greene05bce0b2011-07-29 22:43:06 +0000888 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
889 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
890 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000891 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
892 TokError("expected list or string type argument in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000893 return 0;
894 }
895 if (LHSt) {
896 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
David Greenee1b46912009-06-08 20:23:18 +0000897 StringRecTy *SType = dynamic_cast<StringRecTy*>(LHSt->getType());
898 if (LType == 0 && SType == 0) {
899 TokError("expected list or string type argumnet in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000900 return 0;
901 }
902 }
903
David Greene1434f662011-01-07 17:05:37 +0000904 if (Code == UnOpInit::HEAD
905 || Code == UnOpInit::TAIL) {
David Greenee1b46912009-06-08 20:23:18 +0000906 if (LHSl == 0 && LHSt == 0) {
907 TokError("expected list type argumnet in unary operator");
908 return 0;
909 }
Bob Wilson21870412009-11-22 04:24:42 +0000910
David Greene5f9f9ba2009-05-14 22:38:31 +0000911 if (LHSl && LHSl->getSize() == 0) {
912 TokError("empty list argument in unary operator");
913 return 0;
914 }
915 if (LHSl) {
David Greene05bce0b2011-07-29 22:43:06 +0000916 Init *Item = LHSl->getElement(0);
917 TypedInit *Itemt = dynamic_cast<TypedInit*>(Item);
David Greene5f9f9ba2009-05-14 22:38:31 +0000918 if (Itemt == 0) {
919 TokError("untyped list element in unary operator");
920 return 0;
921 }
David Greene1434f662011-01-07 17:05:37 +0000922 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000923 Type = Itemt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000924 } else {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000925 Type = ListRecTy::get(Itemt->getType());
David Greene5f9f9ba2009-05-14 22:38:31 +0000926 }
Bob Wilson21870412009-11-22 04:24:42 +0000927 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000928 assert(LHSt && "expected list type argument in unary operator");
929 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
930 if (LType == 0) {
931 TokError("expected list type argumnet in unary operator");
932 return 0;
933 }
David Greene1434f662011-01-07 17:05:37 +0000934 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000935 Type = LType->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +0000936 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000937 Type = LType;
938 }
939 }
940 }
941 }
942
David Greenee6c27de2009-05-14 21:22:49 +0000943 if (Lex.getCode() != tgtok::r_paren) {
944 TokError("expected ')' in unary operator");
945 return 0;
946 }
947 Lex.Lex(); // eat the ')'
David Greenedcd35c72011-07-29 19:07:07 +0000948 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee6c27de2009-05-14 21:22:49 +0000949 }
David Greened418c1b2009-05-14 20:54:48 +0000950
951 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +0000952 case tgtok::XSRA:
David Greened418c1b2009-05-14 20:54:48 +0000953 case tgtok::XSRL:
954 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +0000955 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +0000956 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000957 tgtok::TokKind OpTok = Lex.getCode();
958 SMLoc OpLoc = Lex.getLoc();
959 Lex.Lex(); // eat the operation
960
David Greened418c1b2009-05-14 20:54:48 +0000961 BinOpInit::BinaryOp Code;
962 RecTy *Type = 0;
963
Chris Lattner8d978a72010-10-05 23:58:18 +0000964 switch (OpTok) {
Craig Topper85814382012-02-07 05:05:23 +0000965 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000966 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
967 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
968 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
969 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
970 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Bob Wilson21870412009-11-22 04:24:42 +0000971 case tgtok::XStrConcat:
David Greened418c1b2009-05-14 20:54:48 +0000972 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000973 Type = StringRecTy::get();
David Greened418c1b2009-05-14 20:54:48 +0000974 break;
David Greened418c1b2009-05-14 20:54:48 +0000975 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000976
David Greened418c1b2009-05-14 20:54:48 +0000977 if (Lex.getCode() != tgtok::l_paren) {
978 TokError("expected '(' after binary operator");
979 return 0;
980 }
981 Lex.Lex(); // eat the '('
982
David Greene05bce0b2011-07-29 22:43:06 +0000983 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000984
Chris Lattner8d978a72010-10-05 23:58:18 +0000985 InitList.push_back(ParseValue(CurRec));
986 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000987
Chris Lattner8d978a72010-10-05 23:58:18 +0000988 while (Lex.getCode() == tgtok::comma) {
989 Lex.Lex(); // eat the ','
990
991 InitList.push_back(ParseValue(CurRec));
992 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000993 }
David Greened418c1b2009-05-14 20:54:48 +0000994
995 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner8d978a72010-10-05 23:58:18 +0000996 TokError("expected ')' in operator");
David Greened418c1b2009-05-14 20:54:48 +0000997 return 0;
998 }
999 Lex.Lex(); // eat the ')'
Chris Lattner8d978a72010-10-05 23:58:18 +00001000
1001 // We allow multiple operands to associative operators like !strconcat as
1002 // shorthand for nesting them.
1003 if (Code == BinOpInit::STRCONCAT) {
1004 while (InitList.size() > 2) {
David Greene05bce0b2011-07-29 22:43:06 +00001005 Init *RHS = InitList.pop_back_val();
David Greenedcd35c72011-07-29 19:07:07 +00001006 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
1007 ->Fold(CurRec, CurMultiClass);
Chris Lattner8d978a72010-10-05 23:58:18 +00001008 InitList.back() = RHS;
1009 }
1010 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +00001011
Chris Lattner8d978a72010-10-05 23:58:18 +00001012 if (InitList.size() == 2)
David Greenedcd35c72011-07-29 19:07:07 +00001013 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner8d978a72010-10-05 23:58:18 +00001014 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +00001015
Chris Lattner8d978a72010-10-05 23:58:18 +00001016 Error(OpLoc, "expected two operands to operator");
1017 return 0;
David Greened418c1b2009-05-14 20:54:48 +00001018 }
1019
David Greene9bea7c82009-05-14 23:26:46 +00001020 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +00001021 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001022 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
1023 TernOpInit::TernaryOp Code;
1024 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +00001025
David Greene4afc5092009-05-14 21:54:42 +00001026 tgtok::TokKind LexCode = Lex.getCode();
1027 Lex.Lex(); // eat the operation
1028 switch (LexCode) {
Craig Topper85814382012-02-07 05:05:23 +00001029 default: llvm_unreachable("Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +00001030 case tgtok::XIf:
1031 Code = TernOpInit::IF;
1032 break;
David Greenebeb31a52009-05-14 22:23:47 +00001033 case tgtok::XForEach:
1034 Code = TernOpInit::FOREACH;
1035 break;
David Greene4afc5092009-05-14 21:54:42 +00001036 case tgtok::XSubst:
1037 Code = TernOpInit::SUBST;
1038 break;
1039 }
1040 if (Lex.getCode() != tgtok::l_paren) {
1041 TokError("expected '(' after ternary operator");
1042 return 0;
1043 }
1044 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +00001045
David Greene05bce0b2011-07-29 22:43:06 +00001046 Init *LHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001047 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001048
David Greene4afc5092009-05-14 21:54:42 +00001049 if (Lex.getCode() != tgtok::comma) {
1050 TokError("expected ',' in ternary operator");
1051 return 0;
1052 }
1053 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001054
David Greene05bce0b2011-07-29 22:43:06 +00001055 Init *MHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001056 if (MHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001057
David Greene4afc5092009-05-14 21:54:42 +00001058 if (Lex.getCode() != tgtok::comma) {
1059 TokError("expected ',' in ternary operator");
1060 return 0;
1061 }
1062 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001063
David Greene05bce0b2011-07-29 22:43:06 +00001064 Init *RHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001065 if (RHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001066
David Greene4afc5092009-05-14 21:54:42 +00001067 if (Lex.getCode() != tgtok::r_paren) {
1068 TokError("expected ')' in binary operator");
1069 return 0;
1070 }
1071 Lex.Lex(); // eat the ')'
David Greened418c1b2009-05-14 20:54:48 +00001072
David Greene4afc5092009-05-14 21:54:42 +00001073 switch (LexCode) {
Craig Topper85814382012-02-07 05:05:23 +00001074 default: llvm_unreachable("Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +00001075 case tgtok::XIf: {
Bill Wendling548f5a02010-12-13 01:46:19 +00001076 // FIXME: The `!if' operator doesn't handle non-TypedInit well at
1077 // all. This can be made much more robust.
David Greene05bce0b2011-07-29 22:43:06 +00001078 TypedInit *MHSt = dynamic_cast<TypedInit*>(MHS);
1079 TypedInit *RHSt = dynamic_cast<TypedInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +00001080
1081 RecTy *MHSTy = 0;
1082 RecTy *RHSTy = 0;
1083
1084 if (MHSt == 0 && RHSt == 0) {
David Greene05bce0b2011-07-29 22:43:06 +00001085 BitsInit *MHSbits = dynamic_cast<BitsInit*>(MHS);
1086 BitsInit *RHSbits = dynamic_cast<BitsInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +00001087
1088 if (MHSbits && RHSbits &&
1089 MHSbits->getNumBits() == RHSbits->getNumBits()) {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +00001090 Type = BitRecTy::get();
Bill Wendling548f5a02010-12-13 01:46:19 +00001091 break;
1092 } else {
David Greene05bce0b2011-07-29 22:43:06 +00001093 BitInit *MHSbit = dynamic_cast<BitInit*>(MHS);
1094 BitInit *RHSbit = dynamic_cast<BitInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +00001095
1096 if (MHSbit && RHSbit) {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +00001097 Type = BitRecTy::get();
Bill Wendling548f5a02010-12-13 01:46:19 +00001098 break;
1099 }
1100 }
1101 } else if (MHSt != 0 && RHSt != 0) {
1102 MHSTy = MHSt->getType();
1103 RHSTy = RHSt->getType();
1104 }
1105
1106 if (!MHSTy || !RHSTy) {
David Greene9bea7c82009-05-14 23:26:46 +00001107 TokError("could not get type for !if");
1108 return 0;
1109 }
Bill Wendling548f5a02010-12-13 01:46:19 +00001110
1111 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1112 Type = RHSTy;
1113 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1114 Type = MHSTy;
Bob Wilson21870412009-11-22 04:24:42 +00001115 } else {
David Greene9bea7c82009-05-14 23:26:46 +00001116 TokError("inconsistent types for !if");
1117 return 0;
1118 }
1119 break;
1120 }
David Greenebeb31a52009-05-14 22:23:47 +00001121 case tgtok::XForEach: {
David Greene05bce0b2011-07-29 22:43:06 +00001122 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
David Greenebeb31a52009-05-14 22:23:47 +00001123 if (MHSt == 0) {
1124 TokError("could not get type for !foreach");
1125 return 0;
1126 }
1127 Type = MHSt->getType();
1128 break;
1129 }
David Greene4afc5092009-05-14 21:54:42 +00001130 case tgtok::XSubst: {
David Greene05bce0b2011-07-29 22:43:06 +00001131 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
David Greene4afc5092009-05-14 21:54:42 +00001132 if (RHSt == 0) {
1133 TokError("could not get type for !subst");
1134 return 0;
1135 }
1136 Type = RHSt->getType();
1137 break;
1138 }
1139 }
David Greenedcd35c72011-07-29 19:07:07 +00001140 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson21870412009-11-22 04:24:42 +00001141 CurMultiClass);
David Greene4afc5092009-05-14 21:54:42 +00001142 }
David Greened418c1b2009-05-14 20:54:48 +00001143 }
David Greened418c1b2009-05-14 20:54:48 +00001144}
1145
1146/// ParseOperatorType - Parse a type for an operator. This returns
1147/// null on error.
1148///
1149/// OperatorType ::= '<' Type '>'
1150///
Dan Gohmana9ad0412009-08-12 22:10:57 +00001151RecTy *TGParser::ParseOperatorType() {
David Greened418c1b2009-05-14 20:54:48 +00001152 RecTy *Type = 0;
1153
1154 if (Lex.getCode() != tgtok::less) {
1155 TokError("expected type name for operator");
1156 return 0;
1157 }
1158 Lex.Lex(); // eat the <
1159
1160 Type = ParseType();
1161
1162 if (Type == 0) {
1163 TokError("expected type name for operator");
1164 return 0;
1165 }
1166
1167 if (Lex.getCode() != tgtok::greater) {
1168 TokError("expected type name for operator");
1169 return 0;
1170 }
1171 Lex.Lex(); // eat the >
1172
1173 return Type;
1174}
1175
1176
Chris Lattnerf4601652007-11-22 20:49:04 +00001177/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1178///
1179/// SimpleValue ::= IDValue
1180/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001181/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +00001182/// SimpleValue ::= CODEFRAGMENT
1183/// SimpleValue ::= '?'
1184/// SimpleValue ::= '{' ValueList '}'
1185/// SimpleValue ::= ID '<' ValueListNE '>'
1186/// SimpleValue ::= '[' ValueList ']'
1187/// SimpleValue ::= '(' IDValue DagArgList ')'
1188/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
1189/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1190/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1191/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1192/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1193///
David Greenef3744a02011-10-19 13:04:20 +00001194Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1195 IDParseMode Mode) {
David Greene05bce0b2011-07-29 22:43:06 +00001196 Init *R = 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001197 switch (Lex.getCode()) {
1198 default: TokError("Unknown token when parsing a value"); break;
David Greened3d1cad2011-10-19 13:04:43 +00001199 case tgtok::paste:
1200 // This is a leading paste operation. This is deprecated but
1201 // still exists in some .td files. Ignore it.
1202 Lex.Lex(); // Skip '#'.
1203 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenedcd35c72011-07-29 19:07:07 +00001204 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001205 case tgtok::StrVal: {
1206 std::string Val = Lex.getCurStrVal();
1207 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001208
Jim Grosbachda4231f2009-03-26 16:17:51 +00001209 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001210 while (Lex.getCode() == tgtok::StrVal) {
1211 Val += Lex.getCurStrVal();
1212 Lex.Lex();
1213 }
Bob Wilson21870412009-11-22 04:24:42 +00001214
David Greenedcd35c72011-07-29 19:07:07 +00001215 R = StringInit::get(Val);
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001216 break;
1217 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001218 case tgtok::CodeFragment:
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +00001219 R = StringInit::get(Lex.getCurStrVal());
Chris Lattner578bcf02010-10-06 04:31:40 +00001220 Lex.Lex();
1221 break;
1222 case tgtok::question:
David Greenedcd35c72011-07-29 19:07:07 +00001223 R = UnsetInit::get();
Chris Lattner578bcf02010-10-06 04:31:40 +00001224 Lex.Lex();
1225 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001226 case tgtok::Id: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001227 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001228 std::string Name = Lex.getCurStrVal();
1229 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greenef3744a02011-10-19 13:04:20 +00001230 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson21870412009-11-22 04:24:42 +00001231
Chris Lattnerf4601652007-11-22 20:49:04 +00001232 // Value ::= ID '<' ValueListNE '>'
1233 if (Lex.Lex() == tgtok::greater) {
1234 TokError("expected non-empty value list");
1235 return 0;
1236 }
David Greenee1b46912009-06-08 20:23:18 +00001237
Chris Lattnerf4601652007-11-22 20:49:04 +00001238 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1239 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1240 // body.
1241 Record *Class = Records.getClass(Name);
1242 if (!Class) {
1243 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1244 return 0;
1245 }
David Greenee1b46912009-06-08 20:23:18 +00001246
David Greene05bce0b2011-07-29 22:43:06 +00001247 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
David Greenee1b46912009-06-08 20:23:18 +00001248 if (ValueList.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001249
David Greenee1b46912009-06-08 20:23:18 +00001250 if (Lex.getCode() != tgtok::greater) {
1251 TokError("expected '>' at end of value list");
1252 return 0;
1253 }
1254 Lex.Lex(); // eat the '>'
Bob Wilson21870412009-11-22 04:24:42 +00001255
Chris Lattnerf4601652007-11-22 20:49:04 +00001256 // Create the new record, set it as CurRec temporarily.
1257 static unsigned AnonCounter = 0;
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001258 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),
1259 NameLoc,
1260 Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001261 SubClassReference SCRef;
1262 SCRef.RefLoc = NameLoc;
1263 SCRef.Rec = Class;
1264 SCRef.TemplateArgs = ValueList;
1265 // Add info about the subclass to NewRec.
1266 if (AddSubClass(NewRec, SCRef))
1267 return 0;
1268 NewRec->resolveReferences();
1269 Records.addDef(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001270
Chris Lattnerf4601652007-11-22 20:49:04 +00001271 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +00001272 return DefInit::get(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001273 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001274 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001275 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001276 Lex.Lex(); // eat the '{'
David Greene05bce0b2011-07-29 22:43:06 +00001277 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001278
Chris Lattnerf4601652007-11-22 20:49:04 +00001279 if (Lex.getCode() != tgtok::r_brace) {
1280 Vals = ParseValueList(CurRec);
1281 if (Vals.empty()) return 0;
1282 }
1283 if (Lex.getCode() != tgtok::r_brace) {
1284 TokError("expected '}' at end of bit list value");
1285 return 0;
1286 }
1287 Lex.Lex(); // eat the '}'
Bob Wilson21870412009-11-22 04:24:42 +00001288
David Greene05bce0b2011-07-29 22:43:06 +00001289 SmallVector<Init *, 16> NewBits(Vals.size());
David Greeneca7fd3d2011-07-29 19:07:00 +00001290
Chris Lattnerf4601652007-11-22 20:49:04 +00001291 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001292 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Chris Lattnerf4601652007-11-22 20:49:04 +00001293 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +00001294 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1295 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +00001296 return 0;
1297 }
David Greeneca7fd3d2011-07-29 19:07:00 +00001298 NewBits[Vals.size()-i-1] = Bit;
Chris Lattnerf4601652007-11-22 20:49:04 +00001299 }
David Greenedcd35c72011-07-29 19:07:07 +00001300 return BitsInit::get(NewBits);
Chris Lattnerf4601652007-11-22 20:49:04 +00001301 }
1302 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1303 Lex.Lex(); // eat the '['
David Greene05bce0b2011-07-29 22:43:06 +00001304 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001305
David Greenee1b46912009-06-08 20:23:18 +00001306 RecTy *DeducedEltTy = 0;
1307 ListRecTy *GivenListTy = 0;
Bob Wilson21870412009-11-22 04:24:42 +00001308
David Greenee1b46912009-06-08 20:23:18 +00001309 if (ItemType != 0) {
1310 ListRecTy *ListType = dynamic_cast<ListRecTy*>(ItemType);
1311 if (ListType == 0) {
1312 std::stringstream s;
Bob Wilson21870412009-11-22 04:24:42 +00001313 s << "Type mismatch for list, expected list type, got "
David Greenee1b46912009-06-08 20:23:18 +00001314 << ItemType->getAsString();
1315 TokError(s.str());
Jim Grosbach6a44ada2011-03-11 19:52:52 +00001316 return 0;
David Greenee1b46912009-06-08 20:23:18 +00001317 }
1318 GivenListTy = ListType;
Bob Wilson21870412009-11-22 04:24:42 +00001319 }
David Greenee1b46912009-06-08 20:23:18 +00001320
Chris Lattnerf4601652007-11-22 20:49:04 +00001321 if (Lex.getCode() != tgtok::r_square) {
Bob Wilson21870412009-11-22 04:24:42 +00001322 Vals = ParseValueList(CurRec, 0,
1323 GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001324 if (Vals.empty()) return 0;
1325 }
1326 if (Lex.getCode() != tgtok::r_square) {
1327 TokError("expected ']' at end of list value");
1328 return 0;
1329 }
1330 Lex.Lex(); // eat the ']'
David Greenee1b46912009-06-08 20:23:18 +00001331
1332 RecTy *GivenEltTy = 0;
1333 if (Lex.getCode() == tgtok::less) {
1334 // Optional list element type
1335 Lex.Lex(); // eat the '<'
1336
1337 GivenEltTy = ParseType();
1338 if (GivenEltTy == 0) {
1339 // Couldn't parse element type
1340 return 0;
1341 }
1342
1343 if (Lex.getCode() != tgtok::greater) {
1344 TokError("expected '>' at end of list element type");
1345 return 0;
1346 }
1347 Lex.Lex(); // eat the '>'
1348 }
1349
1350 // Check elements
1351 RecTy *EltTy = 0;
David Greene05bce0b2011-07-29 22:43:06 +00001352 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greenee1b46912009-06-08 20:23:18 +00001353 i != ie;
1354 ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001355 TypedInit *TArg = dynamic_cast<TypedInit*>(*i);
David Greenee1b46912009-06-08 20:23:18 +00001356 if (TArg == 0) {
1357 TokError("Untyped list element");
1358 return 0;
1359 }
1360 if (EltTy != 0) {
1361 EltTy = resolveTypes(EltTy, TArg->getType());
1362 if (EltTy == 0) {
1363 TokError("Incompatible types in list elements");
1364 return 0;
1365 }
Bob Wilson21870412009-11-22 04:24:42 +00001366 } else {
David Greenee1b46912009-06-08 20:23:18 +00001367 EltTy = TArg->getType();
1368 }
1369 }
1370
1371 if (GivenEltTy != 0) {
1372 if (EltTy != 0) {
1373 // Verify consistency
1374 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1375 TokError("Incompatible types in list elements");
1376 return 0;
1377 }
1378 }
1379 EltTy = GivenEltTy;
1380 }
1381
1382 if (EltTy == 0) {
1383 if (ItemType == 0) {
1384 TokError("No type for list");
1385 return 0;
1386 }
1387 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +00001388 } else {
David Greenee1b46912009-06-08 20:23:18 +00001389 // Make sure the deduced type is compatible with the given type
1390 if (GivenListTy) {
1391 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1392 TokError("Element type mismatch for list");
1393 return 0;
1394 }
1395 }
1396 DeducedEltTy = EltTy;
1397 }
Bob Wilson21870412009-11-22 04:24:42 +00001398
David Greenedcd35c72011-07-29 19:07:07 +00001399 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4601652007-11-22 20:49:04 +00001400 }
1401 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1402 Lex.Lex(); // eat the '('
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001403 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001404 TokError("expected identifier in dag init");
1405 return 0;
1406 }
Bob Wilson21870412009-11-22 04:24:42 +00001407
David Greene05bce0b2011-07-29 22:43:06 +00001408 Init *Operator = ParseValue(CurRec);
Chris Lattner578bcf02010-10-06 04:31:40 +00001409 if (Operator == 0) return 0;
David Greenec7cafcd2009-04-22 20:18:10 +00001410
Nate Begeman7cee8172009-03-19 05:21:56 +00001411 // If the operator name is present, parse it.
1412 std::string OperatorName;
1413 if (Lex.getCode() == tgtok::colon) {
1414 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1415 TokError("expected variable name in dag operator");
1416 return 0;
1417 }
1418 OperatorName = Lex.getCurStrVal();
1419 Lex.Lex(); // eat the VarName.
1420 }
Bob Wilson21870412009-11-22 04:24:42 +00001421
David Greene05bce0b2011-07-29 22:43:06 +00001422 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4601652007-11-22 20:49:04 +00001423 if (Lex.getCode() != tgtok::r_paren) {
1424 DagArgs = ParseDagArgList(CurRec);
1425 if (DagArgs.empty()) return 0;
1426 }
Bob Wilson21870412009-11-22 04:24:42 +00001427
Chris Lattnerf4601652007-11-22 20:49:04 +00001428 if (Lex.getCode() != tgtok::r_paren) {
1429 TokError("expected ')' in dag init");
1430 return 0;
1431 }
1432 Lex.Lex(); // eat the ')'
Bob Wilson21870412009-11-22 04:24:42 +00001433
David Greenedcd35c72011-07-29 19:07:07 +00001434 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4601652007-11-22 20:49:04 +00001435 }
Bob Wilson21870412009-11-22 04:24:42 +00001436
David Greene1434f662011-01-07 17:05:37 +00001437 case tgtok::XHead:
1438 case tgtok::XTail:
1439 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +00001440 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001441 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +00001442 case tgtok::XSRA:
Chris Lattnerf4601652007-11-22 20:49:04 +00001443 case tgtok::XSRL:
1444 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +00001445 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001446 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene9bea7c82009-05-14 23:26:46 +00001447 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +00001448 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001449 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greened418c1b2009-05-14 20:54:48 +00001450 return ParseOperation(CurRec);
Chris Lattnerf4601652007-11-22 20:49:04 +00001451 }
1452 }
Bob Wilson21870412009-11-22 04:24:42 +00001453
Chris Lattnerf4601652007-11-22 20:49:04 +00001454 return R;
1455}
1456
1457/// ParseValue - Parse a tblgen value. This returns null on error.
1458///
1459/// Value ::= SimpleValue ValueSuffix*
1460/// ValueSuffix ::= '{' BitList '}'
1461/// ValueSuffix ::= '[' BitList ']'
1462/// ValueSuffix ::= '.' ID
1463///
David Greenef3744a02011-10-19 13:04:20 +00001464Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1465 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Chris Lattnerf4601652007-11-22 20:49:04 +00001466 if (Result == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001467
Chris Lattnerf4601652007-11-22 20:49:04 +00001468 // Parse the suffixes now if present.
1469 while (1) {
1470 switch (Lex.getCode()) {
1471 default: return Result;
1472 case tgtok::l_brace: {
David Greenecebb4ee2012-02-22 16:09:41 +00001473 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greene8592b2b2011-10-19 13:04:26 +00001474 // This is the beginning of the object body.
1475 return Result;
1476
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001477 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001478 Lex.Lex(); // eat the '{'
1479 std::vector<unsigned> Ranges = ParseRangeList();
1480 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001481
Chris Lattnerf4601652007-11-22 20:49:04 +00001482 // Reverse the bitlist.
1483 std::reverse(Ranges.begin(), Ranges.end());
1484 Result = Result->convertInitializerBitRange(Ranges);
1485 if (Result == 0) {
1486 Error(CurlyLoc, "Invalid bit range for value");
1487 return 0;
1488 }
Bob Wilson21870412009-11-22 04:24:42 +00001489
Chris Lattnerf4601652007-11-22 20:49:04 +00001490 // Eat the '}'.
1491 if (Lex.getCode() != tgtok::r_brace) {
1492 TokError("expected '}' at end of bit range list");
1493 return 0;
1494 }
1495 Lex.Lex();
1496 break;
1497 }
1498 case tgtok::l_square: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001499 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001500 Lex.Lex(); // eat the '['
1501 std::vector<unsigned> Ranges = ParseRangeList();
1502 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001503
Chris Lattnerf4601652007-11-22 20:49:04 +00001504 Result = Result->convertInitListSlice(Ranges);
1505 if (Result == 0) {
1506 Error(SquareLoc, "Invalid range for list slice");
1507 return 0;
1508 }
Bob Wilson21870412009-11-22 04:24:42 +00001509
Chris Lattnerf4601652007-11-22 20:49:04 +00001510 // Eat the ']'.
1511 if (Lex.getCode() != tgtok::r_square) {
1512 TokError("expected ']' at end of list slice");
1513 return 0;
1514 }
1515 Lex.Lex();
1516 break;
1517 }
1518 case tgtok::period:
1519 if (Lex.Lex() != tgtok::Id) { // eat the .
1520 TokError("expected field identifier after '.'");
1521 return 0;
1522 }
1523 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001524 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00001525 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +00001526 return 0;
1527 }
David Greenedcd35c72011-07-29 19:07:07 +00001528 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001529 Lex.Lex(); // eat field name
1530 break;
David Greened3d1cad2011-10-19 13:04:43 +00001531
1532 case tgtok::paste:
1533 SMLoc PasteLoc = Lex.getLoc();
1534
1535 // Create a !strconcat() operation, first casting each operand to
1536 // a string if necessary.
1537
1538 TypedInit *LHS = dynamic_cast<TypedInit *>(Result);
1539 if (!LHS) {
1540 Error(PasteLoc, "LHS of paste is not typed!");
1541 return 0;
1542 }
1543
1544 if (LHS->getType() != StringRecTy::get()) {
1545 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1546 }
1547
1548 TypedInit *RHS = 0;
1549
1550 Lex.Lex(); // Eat the '#'.
1551 switch (Lex.getCode()) {
1552 case tgtok::colon:
1553 case tgtok::semi:
1554 case tgtok::l_brace:
1555 // These are all of the tokens that can begin an object body.
1556 // Some of these can also begin values but we disallow those cases
1557 // because they are unlikely to be useful.
1558
1559 // Trailing paste, concat with an empty string.
1560 RHS = StringInit::get("");
1561 break;
1562
1563 default:
1564 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
1565 RHS = dynamic_cast<TypedInit *>(RHSResult);
1566 if (!RHS) {
1567 Error(PasteLoc, "RHS of paste is not typed!");
1568 return 0;
1569 }
1570
1571 if (RHS->getType() != StringRecTy::get()) {
1572 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1573 }
1574
1575 break;
1576 }
1577
1578 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1579 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1580 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001581 }
1582 }
1583}
1584
1585/// ParseDagArgList - Parse the argument list for a dag literal expression.
1586///
1587/// ParseDagArgList ::= Value (':' VARNAME)?
1588/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
David Greene05bce0b2011-07-29 22:43:06 +00001589std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4601652007-11-22 20:49:04 +00001590TGParser::ParseDagArgList(Record *CurRec) {
David Greene05bce0b2011-07-29 22:43:06 +00001591 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson21870412009-11-22 04:24:42 +00001592
Chris Lattnerf4601652007-11-22 20:49:04 +00001593 while (1) {
David Greene05bce0b2011-07-29 22:43:06 +00001594 Init *Val = ParseValue(CurRec);
1595 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
Bob Wilson21870412009-11-22 04:24:42 +00001596
Chris Lattnerf4601652007-11-22 20:49:04 +00001597 // If the variable name is present, add it.
1598 std::string VarName;
1599 if (Lex.getCode() == tgtok::colon) {
1600 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1601 TokError("expected variable name in dag literal");
David Greene05bce0b2011-07-29 22:43:06 +00001602 return std::vector<std::pair<llvm::Init*, std::string> >();
Chris Lattnerf4601652007-11-22 20:49:04 +00001603 }
1604 VarName = Lex.getCurStrVal();
1605 Lex.Lex(); // eat the VarName.
1606 }
Bob Wilson21870412009-11-22 04:24:42 +00001607
Chris Lattnerf4601652007-11-22 20:49:04 +00001608 Result.push_back(std::make_pair(Val, VarName));
Bob Wilson21870412009-11-22 04:24:42 +00001609
Chris Lattnerf4601652007-11-22 20:49:04 +00001610 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson21870412009-11-22 04:24:42 +00001611 Lex.Lex(); // eat the ','
Chris Lattnerf4601652007-11-22 20:49:04 +00001612 }
Bob Wilson21870412009-11-22 04:24:42 +00001613
Chris Lattnerf4601652007-11-22 20:49:04 +00001614 return Result;
1615}
1616
1617
1618/// ParseValueList - Parse a comma separated list of values, returning them as a
1619/// vector. Note that this always expects to be able to parse at least one
1620/// value. It returns an empty list if this is not possible.
1621///
1622/// ValueList ::= Value (',' Value)
1623///
David Greene05bce0b2011-07-29 22:43:06 +00001624std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopherd568b3f2011-07-11 23:06:52 +00001625 RecTy *EltTy) {
David Greene05bce0b2011-07-29 22:43:06 +00001626 std::vector<Init*> Result;
David Greenee1b46912009-06-08 20:23:18 +00001627 RecTy *ItemType = EltTy;
David Greene67acdf22009-06-29 19:59:52 +00001628 unsigned int ArgN = 0;
David Greenee1b46912009-06-08 20:23:18 +00001629 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001630 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Jim Grosbachb1320cb2012-01-20 20:02:39 +00001631 if (!TArgs.size()) {
1632 TokError("template argument provided to non-template class");
1633 return std::vector<Init*>();
1634 }
David Greenee1b46912009-06-08 20:23:18 +00001635 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greened9746fe2011-09-19 18:26:07 +00001636 if (!RV) {
1637 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1638 << ")\n";
1639 }
David Greenee1b46912009-06-08 20:23:18 +00001640 assert(RV && "Template argument record not found??");
1641 ItemType = RV->getType();
1642 ++ArgN;
1643 }
1644 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001645 if (Result.back() == 0) return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001646
Chris Lattnerf4601652007-11-22 20:49:04 +00001647 while (Lex.getCode() == tgtok::comma) {
1648 Lex.Lex(); // Eat the comma
Bob Wilson21870412009-11-22 04:24:42 +00001649
David Greenee1b46912009-06-08 20:23:18 +00001650 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001651 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greene67acdf22009-06-29 19:59:52 +00001652 if (ArgN >= TArgs.size()) {
1653 TokError("too many template arguments");
David Greene05bce0b2011-07-29 22:43:06 +00001654 return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001655 }
David Greenee1b46912009-06-08 20:23:18 +00001656 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1657 assert(RV && "Template argument record not found??");
1658 ItemType = RV->getType();
1659 ++ArgN;
1660 }
1661 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001662 if (Result.back() == 0) return std::vector<Init*>();
Chris Lattnerf4601652007-11-22 20:49:04 +00001663 }
Bob Wilson21870412009-11-22 04:24:42 +00001664
Chris Lattnerf4601652007-11-22 20:49:04 +00001665 return Result;
1666}
1667
1668
Chris Lattnerf4601652007-11-22 20:49:04 +00001669/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1670/// empty string on error. This can happen in a number of different context's,
1671/// including within a def or in the template args for a def (which which case
1672/// CurRec will be non-null) and within the template args for a multiclass (in
1673/// which case CurRec will be null, but CurMultiClass will be set). This can
1674/// also happen within a def that is within a multiclass, which will set both
1675/// CurRec and CurMultiClass.
1676///
1677/// Declaration ::= FIELD? Type ID ('=' Value)?
1678///
David Greenee22b3212011-10-19 13:02:42 +00001679Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4601652007-11-22 20:49:04 +00001680 bool ParsingTemplateArgs) {
1681 // Read the field prefix if present.
1682 bool HasField = Lex.getCode() == tgtok::Field;
1683 if (HasField) Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001684
Chris Lattnerf4601652007-11-22 20:49:04 +00001685 RecTy *Type = ParseType();
David Greenee22b3212011-10-19 13:02:42 +00001686 if (Type == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001687
Chris Lattnerf4601652007-11-22 20:49:04 +00001688 if (Lex.getCode() != tgtok::Id) {
1689 TokError("Expected identifier in declaration");
David Greenee22b3212011-10-19 13:02:42 +00001690 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001691 }
Bob Wilson21870412009-11-22 04:24:42 +00001692
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001693 SMLoc IdLoc = Lex.getLoc();
David Greenee22b3212011-10-19 13:02:42 +00001694 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001695 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001696
Chris Lattnerf4601652007-11-22 20:49:04 +00001697 if (ParsingTemplateArgs) {
1698 if (CurRec) {
David Greenee22b3212011-10-19 13:02:42 +00001699 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4601652007-11-22 20:49:04 +00001700 } else {
1701 assert(CurMultiClass);
1702 }
1703 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +00001704 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1705 "::");
Chris Lattnerf4601652007-11-22 20:49:04 +00001706 }
Bob Wilson21870412009-11-22 04:24:42 +00001707
Chris Lattnerf4601652007-11-22 20:49:04 +00001708 // Add the value.
1709 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
David Greenee22b3212011-10-19 13:02:42 +00001710 return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001711
Chris Lattnerf4601652007-11-22 20:49:04 +00001712 // If a value is present, parse it.
1713 if (Lex.getCode() == tgtok::equal) {
1714 Lex.Lex();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001715 SMLoc ValLoc = Lex.getLoc();
David Greene05bce0b2011-07-29 22:43:06 +00001716 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001717 if (Val == 0 ||
1718 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
David Greenee22b3212011-10-19 13:02:42 +00001719 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001720 }
Bob Wilson21870412009-11-22 04:24:42 +00001721
Chris Lattnerf4601652007-11-22 20:49:04 +00001722 return DeclName;
1723}
1724
David Greenecebb4ee2012-02-22 16:09:41 +00001725/// ParseForeachDeclaration - Read a foreach declaration, returning
1726/// the name of the declared object or a NULL Init on error. Return
1727/// the name of the parsed initializer list through ForeachListName.
1728///
1729/// ForeachDeclaration ::= ID '=' Value
1730///
1731Init *TGParser::ParseForeachDeclaration(Init *&ForeachListValue) {
1732 if (Lex.getCode() != tgtok::Id) {
1733 TokError("Expected identifier in foreach declaration");
1734 return 0;
1735 }
1736
1737 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1738 Lex.Lex();
1739
1740 // If a value is present, parse it.
1741 if (Lex.getCode() != tgtok::equal) {
1742 TokError("Expected '=' in foreach declaration");
1743 return 0;
1744 }
1745 Lex.Lex(); // Eat the '='
1746
1747 // Expect a list initializer.
1748 ForeachListValue = ParseValue(0, 0, ParseForeachMode);
1749
1750 TypedInit *TypedList = dynamic_cast<TypedInit *>(ForeachListValue);
1751 if (TypedList == 0) {
1752 TokError("Value list is untyped");
1753 return 0;
1754 }
1755
1756 RecTy *ValueType = TypedList->getType();
1757 ListRecTy *ListType = dynamic_cast<ListRecTy *>(ValueType);
1758 if (ListType == 0) {
1759 TokError("Value list is not of list type");
1760 return 0;
1761 }
1762
1763 RecTy *IterType = ListType->getElementType();
1764 VarInit *IterVar = VarInit::get(DeclName, IterType);
1765
1766 return IterVar;
1767}
1768
Chris Lattnerf4601652007-11-22 20:49:04 +00001769/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1770/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1771/// template args for a def, which may or may not be in a multiclass. If null,
1772/// these are the template args for a multiclass.
1773///
1774/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson21870412009-11-22 04:24:42 +00001775///
Chris Lattnerf4601652007-11-22 20:49:04 +00001776bool TGParser::ParseTemplateArgList(Record *CurRec) {
1777 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1778 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +00001779
Chris Lattnerf4601652007-11-22 20:49:04 +00001780 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +00001781
Chris Lattnerf4601652007-11-22 20:49:04 +00001782 // Read the first declaration.
David Greenee22b3212011-10-19 13:02:42 +00001783 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1784 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001785 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001786
Chris Lattnerf4601652007-11-22 20:49:04 +00001787 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson21870412009-11-22 04:24:42 +00001788
Chris Lattnerf4601652007-11-22 20:49:04 +00001789 while (Lex.getCode() == tgtok::comma) {
1790 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001791
Chris Lattnerf4601652007-11-22 20:49:04 +00001792 // Read the following declarations.
1793 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
David Greenee22b3212011-10-19 13:02:42 +00001794 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001795 return true;
1796 TheRecToAddTo->addTemplateArg(TemplArg);
1797 }
Bob Wilson21870412009-11-22 04:24:42 +00001798
Chris Lattnerf4601652007-11-22 20:49:04 +00001799 if (Lex.getCode() != tgtok::greater)
1800 return TokError("expected '>' at end of template argument list");
1801 Lex.Lex(); // eat the '>'.
1802 return false;
1803}
1804
1805
1806/// ParseBodyItem - Parse a single item at within the body of a def or class.
1807///
1808/// BodyItem ::= Declaration ';'
1809/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1810bool TGParser::ParseBodyItem(Record *CurRec) {
1811 if (Lex.getCode() != tgtok::Let) {
David Greenee22b3212011-10-19 13:02:42 +00001812 if (ParseDeclaration(CurRec, false) == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001813 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001814
Chris Lattnerf4601652007-11-22 20:49:04 +00001815 if (Lex.getCode() != tgtok::semi)
1816 return TokError("expected ';' after declaration");
1817 Lex.Lex();
1818 return false;
1819 }
1820
1821 // LET ID OptionalRangeList '=' Value ';'
1822 if (Lex.Lex() != tgtok::Id)
1823 return TokError("expected field identifier after let");
Bob Wilson21870412009-11-22 04:24:42 +00001824
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001825 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001826 std::string FieldName = Lex.getCurStrVal();
1827 Lex.Lex(); // eat the field name.
Bob Wilson21870412009-11-22 04:24:42 +00001828
Chris Lattnerf4601652007-11-22 20:49:04 +00001829 std::vector<unsigned> BitList;
Bob Wilson21870412009-11-22 04:24:42 +00001830 if (ParseOptionalBitList(BitList))
Chris Lattnerf4601652007-11-22 20:49:04 +00001831 return true;
1832 std::reverse(BitList.begin(), BitList.end());
Bob Wilson21870412009-11-22 04:24:42 +00001833
Chris Lattnerf4601652007-11-22 20:49:04 +00001834 if (Lex.getCode() != tgtok::equal)
1835 return TokError("expected '=' in let expression");
1836 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001837
David Greenee1b46912009-06-08 20:23:18 +00001838 RecordVal *Field = CurRec->getValue(FieldName);
1839 if (Field == 0)
1840 return TokError("Value '" + FieldName + "' unknown!");
1841
1842 RecTy *Type = Field->getType();
Bob Wilson21870412009-11-22 04:24:42 +00001843
David Greene05bce0b2011-07-29 22:43:06 +00001844 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001845 if (Val == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001846
Chris Lattnerf4601652007-11-22 20:49:04 +00001847 if (Lex.getCode() != tgtok::semi)
1848 return TokError("expected ';' after let expression");
1849 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001850
Chris Lattnerf4601652007-11-22 20:49:04 +00001851 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1852}
1853
1854/// ParseBody - Read the body of a class or def. Return true on error, false on
1855/// success.
1856///
1857/// Body ::= ';'
1858/// Body ::= '{' BodyList '}'
1859/// BodyList BodyItem*
1860///
1861bool TGParser::ParseBody(Record *CurRec) {
1862 // If this is a null definition, just eat the semi and return.
1863 if (Lex.getCode() == tgtok::semi) {
1864 Lex.Lex();
1865 return false;
1866 }
Bob Wilson21870412009-11-22 04:24:42 +00001867
Chris Lattnerf4601652007-11-22 20:49:04 +00001868 if (Lex.getCode() != tgtok::l_brace)
1869 return TokError("Expected ';' or '{' to start body");
1870 // Eat the '{'.
1871 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001872
Chris Lattnerf4601652007-11-22 20:49:04 +00001873 while (Lex.getCode() != tgtok::r_brace)
1874 if (ParseBodyItem(CurRec))
1875 return true;
1876
1877 // Eat the '}'.
1878 Lex.Lex();
1879 return false;
1880}
1881
1882/// ParseObjectBody - Parse the body of a def or class. This consists of an
1883/// optional ClassList followed by a Body. CurRec is the current def or class
1884/// that is being parsed.
1885///
1886/// ObjectBody ::= BaseClassList Body
1887/// BaseClassList ::= /*empty*/
1888/// BaseClassList ::= ':' BaseClassListNE
1889/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1890///
1891bool TGParser::ParseObjectBody(Record *CurRec) {
1892 // If there is a baseclass list, read it.
1893 if (Lex.getCode() == tgtok::colon) {
1894 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001895
Chris Lattnerf4601652007-11-22 20:49:04 +00001896 // Read all of the subclasses.
1897 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1898 while (1) {
1899 // Check for error.
1900 if (SubClass.Rec == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001901
Chris Lattnerf4601652007-11-22 20:49:04 +00001902 // Add it.
1903 if (AddSubClass(CurRec, SubClass))
1904 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001905
Chris Lattnerf4601652007-11-22 20:49:04 +00001906 if (Lex.getCode() != tgtok::comma) break;
1907 Lex.Lex(); // eat ','.
1908 SubClass = ParseSubClassReference(CurRec, false);
1909 }
1910 }
1911
1912 // Process any variables on the let stack.
1913 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1914 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1915 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1916 LetStack[i][j].Bits, LetStack[i][j].Value))
1917 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001918
Chris Lattnerf4601652007-11-22 20:49:04 +00001919 return ParseBody(CurRec);
1920}
1921
Chris Lattnerf4601652007-11-22 20:49:04 +00001922/// ParseDef - Parse and return a top level or multiclass def, return the record
1923/// corresponding to it. This returns null on error.
1924///
1925/// DefInst ::= DEF ObjectName ObjectBody
1926///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001927bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001928 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001929 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson21870412009-11-22 04:24:42 +00001930 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4601652007-11-22 20:49:04 +00001931
1932 // Parse ObjectName and make a record for it.
David Greenea9e07dd2011-10-19 13:04:29 +00001933 Record *CurRec = new Record(ParseObjectName(CurMultiClass), DefLoc, Records);
Bob Wilson21870412009-11-22 04:24:42 +00001934
Chris Lattnerf4601652007-11-22 20:49:04 +00001935 if (!CurMultiClass) {
1936 // Top-level def definition.
Bob Wilson21870412009-11-22 04:24:42 +00001937
Chris Lattnerf4601652007-11-22 20:49:04 +00001938 // Ensure redefinition doesn't happen.
David Greene1d501392012-01-28 00:03:24 +00001939 if (Records.getDef(CurRec->getNameInitAsString())) {
David Greene2c49fbb2011-10-19 13:03:45 +00001940 Error(DefLoc, "def '" + CurRec->getNameInitAsString()
1941 + "' already defined");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001942 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001943 }
1944 Records.addDef(CurRec);
1945 } else {
1946 // Otherwise, a def inside a multiclass, add it to the multiclass.
1947 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene91919cd2011-10-19 13:03:51 +00001948 if (CurMultiClass->DefPrototypes[i]->getNameInit()
1949 == CurRec->getNameInit()) {
1950 Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
Chris Lattnerf4601652007-11-22 20:49:04 +00001951 "' already defined in this multiclass!");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001952 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001953 }
1954 CurMultiClass->DefPrototypes.push_back(CurRec);
1955 }
Bob Wilson21870412009-11-22 04:24:42 +00001956
Chris Lattnerf4601652007-11-22 20:49:04 +00001957 if (ParseObjectBody(CurRec))
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001958 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001959
Chris Lattnerf4601652007-11-22 20:49:04 +00001960 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
David Greene0d886402011-08-10 18:27:46 +00001961 // See Record::setName(). This resolve step will see any new name
1962 // for the def that might have been created when resolving
1963 // inheritance, values and arguments above.
Chris Lattnerf4601652007-11-22 20:49:04 +00001964 CurRec->resolveReferences();
Bob Wilson21870412009-11-22 04:24:42 +00001965
Chris Lattnerf4601652007-11-22 20:49:04 +00001966 // If ObjectBody has template arguments, it's an error.
1967 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001968
1969 if (CurMultiClass) {
1970 // Copy the template arguments for the multiclass into the def.
David Greenee22b3212011-10-19 13:02:42 +00001971 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001972 CurMultiClass->Rec.getTemplateArgs();
1973
1974 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1975 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1976 assert(RV && "Template arg doesn't exist?");
1977 CurRec->addValue(*RV);
1978 }
1979 }
1980
David Greenecebb4ee2012-02-22 16:09:41 +00001981 if (ProcessForeachDefs(CurRec, CurMultiClass, DefLoc)) {
1982 Error(DefLoc,
1983 "Could not process loops for def" + CurRec->getNameInitAsString());
1984 return true;
1985 }
1986
1987 return false;
1988}
1989
1990/// ParseForeach - Parse a for statement. Return the record corresponding
1991/// to it. This returns true on error.
1992///
1993/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
1994/// Foreach ::= FOREACH Declaration IN Object
1995///
1996bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
1997 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
1998 Lex.Lex(); // Eat the 'for' token.
1999
2000 // Make a temporary object to record items associated with the for
2001 // loop.
2002 Init *ListValue = 0;
2003 Init *IterName = ParseForeachDeclaration(ListValue);
2004 if (IterName == 0)
2005 return TokError("expected declaration in for");
2006
2007 if (Lex.getCode() != tgtok::In)
2008 return TokError("Unknown tok");
2009 Lex.Lex(); // Eat the in
2010
2011 // Create a loop object and remember it.
2012 Loops.push_back(ForeachLoop(IterName, ListValue));
2013
2014 if (Lex.getCode() != tgtok::l_brace) {
2015 // FOREACH Declaration IN Object
2016 if (ParseObject(CurMultiClass))
2017 return true;
2018 }
2019 else {
2020 SMLoc BraceLoc = Lex.getLoc();
2021 // Otherwise, this is a group foreach.
2022 Lex.Lex(); // eat the '{'.
2023
2024 // Parse the object list.
2025 if (ParseObjectList(CurMultiClass))
2026 return true;
2027
2028 if (Lex.getCode() != tgtok::r_brace) {
2029 TokError("expected '}' at end of foreach command");
2030 return Error(BraceLoc, "to match this '{'");
2031 }
2032 Lex.Lex(); // Eat the }
2033 }
2034
2035 // We've processed everything in this loop.
2036 Loops.pop_back();
2037
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002038 return false;
Chris Lattnerf4601652007-11-22 20:49:04 +00002039}
2040
Chris Lattnerf4601652007-11-22 20:49:04 +00002041/// ParseClass - Parse a tblgen class definition.
2042///
2043/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2044///
2045bool TGParser::ParseClass() {
2046 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2047 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002048
Chris Lattnerf4601652007-11-22 20:49:04 +00002049 if (Lex.getCode() != tgtok::Id)
2050 return TokError("expected class name after 'class' keyword");
Bob Wilson21870412009-11-22 04:24:42 +00002051
Chris Lattnerf4601652007-11-22 20:49:04 +00002052 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2053 if (CurRec) {
2054 // If the body was previously defined, this is an error.
David Greenee3385652011-10-19 13:04:13 +00002055 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4601652007-11-22 20:49:04 +00002056 !CurRec->getSuperClasses().empty() ||
2057 !CurRec->getTemplateArgs().empty())
David Greene69a23942011-10-19 13:03:58 +00002058 return TokError("Class '" + CurRec->getNameInitAsString()
2059 + "' already defined");
Chris Lattnerf4601652007-11-22 20:49:04 +00002060 } else {
2061 // If this is the first reference to this class, create and add it.
Chris Lattner9c6b60e2010-12-15 04:48:22 +00002062 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00002063 Records.addClass(CurRec);
2064 }
2065 Lex.Lex(); // eat the name.
Bob Wilson21870412009-11-22 04:24:42 +00002066
Chris Lattnerf4601652007-11-22 20:49:04 +00002067 // If there are template args, parse them.
2068 if (Lex.getCode() == tgtok::less)
2069 if (ParseTemplateArgList(CurRec))
2070 return true;
2071
2072 // Finally, parse the object body.
2073 return ParseObjectBody(CurRec);
2074}
2075
2076/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2077/// of LetRecords.
2078///
2079/// LetList ::= LetItem (',' LetItem)*
2080/// LetItem ::= ID OptionalRangeList '=' Value
2081///
2082std::vector<LetRecord> TGParser::ParseLetList() {
2083 std::vector<LetRecord> Result;
Bob Wilson21870412009-11-22 04:24:42 +00002084
Chris Lattnerf4601652007-11-22 20:49:04 +00002085 while (1) {
2086 if (Lex.getCode() != tgtok::Id) {
2087 TokError("expected identifier in let definition");
2088 return std::vector<LetRecord>();
2089 }
2090 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002091 SMLoc NameLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +00002092 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4601652007-11-22 20:49:04 +00002093
2094 // Check for an optional RangeList.
2095 std::vector<unsigned> Bits;
Bob Wilson21870412009-11-22 04:24:42 +00002096 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4601652007-11-22 20:49:04 +00002097 return std::vector<LetRecord>();
2098 std::reverse(Bits.begin(), Bits.end());
Bob Wilson21870412009-11-22 04:24:42 +00002099
Chris Lattnerf4601652007-11-22 20:49:04 +00002100 if (Lex.getCode() != tgtok::equal) {
2101 TokError("expected '=' in let expression");
2102 return std::vector<LetRecord>();
2103 }
2104 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00002105
David Greene05bce0b2011-07-29 22:43:06 +00002106 Init *Val = ParseValue(0);
Chris Lattnerf4601652007-11-22 20:49:04 +00002107 if (Val == 0) return std::vector<LetRecord>();
Bob Wilson21870412009-11-22 04:24:42 +00002108
Chris Lattnerf4601652007-11-22 20:49:04 +00002109 // Now that we have everything, add the record.
2110 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson21870412009-11-22 04:24:42 +00002111
Chris Lattnerf4601652007-11-22 20:49:04 +00002112 if (Lex.getCode() != tgtok::comma)
2113 return Result;
Bob Wilson21870412009-11-22 04:24:42 +00002114 Lex.Lex(); // eat the comma.
Chris Lattnerf4601652007-11-22 20:49:04 +00002115 }
2116}
2117
2118/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002119/// different related productions. This works inside multiclasses too.
Chris Lattnerf4601652007-11-22 20:49:04 +00002120///
2121/// Object ::= LET LetList IN '{' ObjectList '}'
2122/// Object ::= LET LetList IN Object
2123///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002124bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002125 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2126 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002127
Chris Lattnerf4601652007-11-22 20:49:04 +00002128 // Add this entry to the let stack.
2129 std::vector<LetRecord> LetInfo = ParseLetList();
2130 if (LetInfo.empty()) return true;
2131 LetStack.push_back(LetInfo);
2132
2133 if (Lex.getCode() != tgtok::In)
2134 return TokError("expected 'in' at end of top-level 'let'");
2135 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002136
Chris Lattnerf4601652007-11-22 20:49:04 +00002137 // If this is a scalar let, just handle it now
2138 if (Lex.getCode() != tgtok::l_brace) {
2139 // LET LetList IN Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002140 if (ParseObject(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00002141 return true;
2142 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002143 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002144 // Otherwise, this is a group let.
2145 Lex.Lex(); // eat the '{'.
Bob Wilson21870412009-11-22 04:24:42 +00002146
Chris Lattnerf4601652007-11-22 20:49:04 +00002147 // Parse the object list.
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002148 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00002149 return true;
Bob Wilson21870412009-11-22 04:24:42 +00002150
Chris Lattnerf4601652007-11-22 20:49:04 +00002151 if (Lex.getCode() != tgtok::r_brace) {
2152 TokError("expected '}' at end of top level let command");
2153 return Error(BraceLoc, "to match this '{'");
2154 }
2155 Lex.Lex();
2156 }
Bob Wilson21870412009-11-22 04:24:42 +00002157
Chris Lattnerf4601652007-11-22 20:49:04 +00002158 // Outside this let scope, this let block is not active.
2159 LetStack.pop_back();
2160 return false;
2161}
2162
Chris Lattnerf4601652007-11-22 20:49:04 +00002163/// ParseMultiClass - Parse a multiclass definition.
2164///
Bob Wilson32558652009-04-28 19:41:44 +00002165/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
2166/// ':' BaseMultiClassList '{' MultiClassDef+ '}'
Chris Lattnerf4601652007-11-22 20:49:04 +00002167///
2168bool TGParser::ParseMultiClass() {
2169 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2170 Lex.Lex(); // Eat the multiclass token.
2171
2172 if (Lex.getCode() != tgtok::Id)
2173 return TokError("expected identifier after multiclass for name");
2174 std::string Name = Lex.getCurStrVal();
Bob Wilson21870412009-11-22 04:24:42 +00002175
Chris Lattnerf4601652007-11-22 20:49:04 +00002176 if (MultiClasses.count(Name))
2177 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson21870412009-11-22 04:24:42 +00002178
Chris Lattner67db8832010-12-13 00:23:57 +00002179 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
2180 Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00002181 Lex.Lex(); // Eat the identifier.
Bob Wilson21870412009-11-22 04:24:42 +00002182
Chris Lattnerf4601652007-11-22 20:49:04 +00002183 // If there are template args, parse them.
2184 if (Lex.getCode() == tgtok::less)
2185 if (ParseTemplateArgList(0))
2186 return true;
2187
David Greened34a73b2009-04-24 16:55:41 +00002188 bool inherits = false;
2189
David Greenede444af2009-04-22 16:42:54 +00002190 // If there are submulticlasses, parse them.
2191 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00002192 inherits = true;
2193
David Greenede444af2009-04-22 16:42:54 +00002194 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00002195
David Greenede444af2009-04-22 16:42:54 +00002196 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00002197 SubMultiClassReference SubMultiClass =
2198 ParseSubMultiClassReference(CurMultiClass);
David Greenede444af2009-04-22 16:42:54 +00002199 while (1) {
2200 // Check for error.
2201 if (SubMultiClass.MC == 0) return true;
Bob Wilson32558652009-04-28 19:41:44 +00002202
David Greenede444af2009-04-22 16:42:54 +00002203 // Add it.
2204 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2205 return true;
Bob Wilson32558652009-04-28 19:41:44 +00002206
David Greenede444af2009-04-22 16:42:54 +00002207 if (Lex.getCode() != tgtok::comma) break;
2208 Lex.Lex(); // eat ','.
2209 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2210 }
2211 }
2212
David Greened34a73b2009-04-24 16:55:41 +00002213 if (Lex.getCode() != tgtok::l_brace) {
2214 if (!inherits)
2215 return TokError("expected '{' in multiclass definition");
Bob Wilson21870412009-11-22 04:24:42 +00002216 else if (Lex.getCode() != tgtok::semi)
2217 return TokError("expected ';' in multiclass definition");
David Greened34a73b2009-04-24 16:55:41 +00002218 else
Bob Wilson21870412009-11-22 04:24:42 +00002219 Lex.Lex(); // eat the ';'.
2220 } else {
David Greened34a73b2009-04-24 16:55:41 +00002221 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2222 return TokError("multiclass must contain at least one def");
Bob Wilson21870412009-11-22 04:24:42 +00002223
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002224 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002225 switch (Lex.getCode()) {
2226 default:
David Greenea1b1b792011-10-07 18:25:05 +00002227 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002228 case tgtok::Let:
2229 case tgtok::Def:
2230 case tgtok::Defm:
David Greenecebb4ee2012-02-22 16:09:41 +00002231 case tgtok::Foreach:
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002232 if (ParseObject(CurMultiClass))
2233 return true;
2234 break;
2235 }
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002236 }
David Greened34a73b2009-04-24 16:55:41 +00002237 Lex.Lex(); // eat the '}'.
2238 }
Bob Wilson21870412009-11-22 04:24:42 +00002239
Chris Lattnerf4601652007-11-22 20:49:04 +00002240 CurMultiClass = 0;
2241 return false;
2242}
2243
David Greenee499a2d2011-10-05 22:42:07 +00002244Record *TGParser::
2245InstantiateMulticlassDef(MultiClass &MC,
2246 Record *DefProto,
David Greene7be867e2011-10-19 13:04:31 +00002247 Init *DefmPrefix,
David Greenee499a2d2011-10-05 22:42:07 +00002248 SMLoc DefmPrefixLoc) {
David Greene7be867e2011-10-19 13:04:31 +00002249 // We need to preserve DefProto so it can be reused for later
2250 // instantiations, so create a new Record to inherit from it.
2251
David Greenee499a2d2011-10-05 22:42:07 +00002252 // Add in the defm name. If the defm prefix is empty, give each
2253 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2254 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2255 // as a prefix.
David Greenee499a2d2011-10-05 22:42:07 +00002256
David Greene7be867e2011-10-19 13:04:31 +00002257 if (DefmPrefix == 0)
2258 DefmPrefix = StringInit::get(GetNewAnonymousName());
2259
2260 Init *DefName = DefProto->getNameInit();
2261
David Greened3d1cad2011-10-19 13:04:43 +00002262 StringInit *DefNameString = dynamic_cast<StringInit *>(DefName);
David Greene7be867e2011-10-19 13:04:31 +00002263
David Greened3d1cad2011-10-19 13:04:43 +00002264 if (DefNameString != 0) {
2265 // We have a fully expanded string so there are no operators to
2266 // resolve. We should concatenate the given prefix and name.
David Greene7be867e2011-10-19 13:04:31 +00002267 DefName =
2268 BinOpInit::get(BinOpInit::STRCONCAT,
2269 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2270 StringRecTy::get())->Fold(DefProto, &MC),
2271 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2272 }
David Greene7be867e2011-10-19 13:04:31 +00002273
David Greenee499a2d2011-10-05 22:42:07 +00002274 Record *CurRec = new Record(DefName, DefmPrefixLoc, Records);
2275
2276 SubClassReference Ref;
2277 Ref.RefLoc = DefmPrefixLoc;
2278 Ref.Rec = DefProto;
2279 AddSubClass(CurRec, Ref);
2280
David Greenee5b252f2011-10-19 13:04:35 +00002281 if (DefNameString == 0) {
2282 // We must resolve references to NAME.
2283 if (SetValue(CurRec, Ref.RefLoc, "NAME", std::vector<unsigned>(),
2284 DefmPrefix)) {
2285 Error(DefmPrefixLoc, "Could not resolve "
2286 + CurRec->getNameInitAsString() + ":NAME to '"
2287 + DefmPrefix->getAsUnquotedString() + "'");
2288 return 0;
2289 }
2290
2291 RecordVal *DefNameRV = CurRec->getValue("NAME");
2292 CurRec->resolveReferencesTo(DefNameRV);
2293 }
2294
2295 if (!CurMultiClass) {
2296 // We do this after resolving NAME because before resolution, many
2297 // multiclass defs will have the same name expression. If we are
2298 // currently in a multiclass, it means this defm appears inside a
2299 // multiclass and its name won't be fully resolvable until we see
2300 // the top-level defm. Therefore, we don't add this to the
2301 // RecordKeeper at this point. If we did we could get duplicate
2302 // defs as more than one probably refers to NAME or some other
2303 // common internal placeholder.
2304
2305 // Ensure redefinition doesn't happen.
2306 if (Records.getDef(CurRec->getNameInitAsString())) {
2307 Error(DefmPrefixLoc, "def '" + CurRec->getNameInitAsString() +
2308 "' already defined, instantiating defm with subdef '" +
2309 DefProto->getNameInitAsString() + "'");
2310 return 0;
2311 }
2312
2313 Records.addDef(CurRec);
2314 }
2315
David Greenee499a2d2011-10-05 22:42:07 +00002316 return CurRec;
2317}
2318
2319bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2320 Record *CurRec,
2321 SMLoc DefmPrefixLoc,
2322 SMLoc SubClassLoc,
David Greenee22b3212011-10-19 13:02:42 +00002323 const std::vector<Init *> &TArgs,
David Greenee499a2d2011-10-05 22:42:07 +00002324 std::vector<Init *> &TemplateVals,
2325 bool DeleteArgs) {
2326 // Loop over all of the template arguments, setting them to the specified
2327 // value or leaving them as the default if necessary.
2328 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2329 // Check if a value is specified for this temp-arg.
2330 if (i < TemplateVals.size()) {
2331 // Set it now.
2332 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2333 TemplateVals[i]))
2334 return true;
2335
2336 // Resolve it next.
2337 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2338
2339 if (DeleteArgs)
2340 // Now remove it.
2341 CurRec->removeValue(TArgs[i]);
2342
2343 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2344 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenee22b3212011-10-19 13:02:42 +00002345 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2346 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2347 + "'");
David Greenee499a2d2011-10-05 22:42:07 +00002348 }
2349 }
2350 return false;
2351}
2352
2353bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2354 Record *CurRec,
2355 Record *DefProto,
2356 SMLoc DefmPrefixLoc) {
2357 // If the mdef is inside a 'let' expression, add to each def.
2358 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2359 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2360 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2361 LetStack[i][j].Bits, LetStack[i][j].Value))
2362 return Error(DefmPrefixLoc, "when instantiating this defm");
2363
David Greenee499a2d2011-10-05 22:42:07 +00002364 // Don't create a top level definition for defm inside multiclasses,
2365 // instead, only update the prototypes and bind the template args
2366 // with the new created definition.
2367 if (CurMultiClass) {
2368 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2369 i != e; ++i)
David Greene22dde7e2011-10-19 13:04:02 +00002370 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2371 == CurRec->getNameInit())
2372 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
David Greenee499a2d2011-10-05 22:42:07 +00002373 "' already defined in this multiclass!");
2374 CurMultiClass->DefPrototypes.push_back(CurRec);
2375
2376 // Copy the template arguments for the multiclass into the new def.
David Greenee22b3212011-10-19 13:02:42 +00002377 const std::vector<Init *> &TA =
David Greenee499a2d2011-10-05 22:42:07 +00002378 CurMultiClass->Rec.getTemplateArgs();
2379
2380 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2381 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2382 assert(RV && "Template arg doesn't exist?");
2383 CurRec->addValue(*RV);
2384 }
David Greenee499a2d2011-10-05 22:42:07 +00002385 }
2386
2387 return false;
2388}
2389
Chris Lattnerf4601652007-11-22 20:49:04 +00002390/// ParseDefm - Parse the instantiation of a multiclass.
2391///
2392/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2393///
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002394bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002395 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Bob Wilson21870412009-11-22 04:24:42 +00002396
David Greenea9e07dd2011-10-19 13:04:29 +00002397 Init *DefmPrefix = 0;
David Greenea9e07dd2011-10-19 13:04:29 +00002398
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002399 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greenea9e07dd2011-10-19 13:04:29 +00002400 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002401 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +00002402
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002403 SMLoc DefmPrefixLoc = Lex.getLoc();
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002404 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4601652007-11-22 20:49:04 +00002405 return TokError("expected ':' after defm identifier");
Bob Wilson21870412009-11-22 04:24:42 +00002406
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002407 // Keep track of the new generated record definitions.
2408 std::vector<Record*> NewRecDefs;
2409
2410 // This record also inherits from a regular class (non-multiclass)?
2411 bool InheritFromClass = false;
2412
Chris Lattnerf4601652007-11-22 20:49:04 +00002413 // eat the colon.
2414 Lex.Lex();
2415
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002416 SMLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002417 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene56546132009-04-22 22:17:51 +00002418
2419 while (1) {
2420 if (Ref.Rec == 0) return true;
2421
2422 // To instantiate a multiclass, we need to first get the multiclass, then
2423 // instantiate each def contained in the multiclass with the SubClassRef
2424 // template parameters.
2425 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2426 assert(MC && "Didn't lookup multiclass correctly?");
David Greene05bce0b2011-07-29 22:43:06 +00002427 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greene56546132009-04-22 22:17:51 +00002428
2429 // Verify that the correct number of template arguments were specified.
David Greenee22b3212011-10-19 13:02:42 +00002430 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greene56546132009-04-22 22:17:51 +00002431 if (TArgs.size() < TemplateVals.size())
2432 return Error(SubClassLoc,
2433 "more template args specified than multiclass expects");
2434
2435 // Loop over all the def's in the multiclass, instantiating each one.
2436 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2437 Record *DefProto = MC->DefPrototypes[i];
2438
David Greene7be867e2011-10-19 13:04:31 +00002439 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix, DefmPrefixLoc);
Jim Grosbach94f2dc92011-12-02 18:33:03 +00002440 if (!CurRec)
2441 return true;
David Greene065f2592009-05-05 16:28:25 +00002442
David Greenee499a2d2011-10-05 22:42:07 +00002443 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmPrefixLoc, SubClassLoc,
2444 TArgs, TemplateVals, true/*Delete args*/))
2445 return Error(SubClassLoc, "could not instantiate def");
David Greene56546132009-04-22 22:17:51 +00002446
David Greenee499a2d2011-10-05 22:42:07 +00002447 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmPrefixLoc))
2448 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002449
2450 NewRecDefs.push_back(CurRec);
David Greene56546132009-04-22 22:17:51 +00002451 }
2452
David Greenee499a2d2011-10-05 22:42:07 +00002453
David Greene56546132009-04-22 22:17:51 +00002454 if (Lex.getCode() != tgtok::comma) break;
2455 Lex.Lex(); // eat ','.
2456
2457 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002458
2459 // A defm can inherit from regular classes (non-multiclass) as
2460 // long as they come in the end of the inheritance list.
2461 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0);
2462
2463 if (InheritFromClass)
2464 break;
2465
David Greene56546132009-04-22 22:17:51 +00002466 Ref = ParseSubClassReference(0, true);
2467 }
2468
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002469 if (InheritFromClass) {
2470 // Process all the classes to inherit as if they were part of a
2471 // regular 'def' and inherit all record values.
2472 SubClassReference SubClass = ParseSubClassReference(0, false);
2473 while (1) {
2474 // Check for error.
2475 if (SubClass.Rec == 0) return true;
2476
2477 // Get the expanded definition prototypes and teach them about
2478 // the record values the current class to inherit has
2479 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2480 Record *CurRec = NewRecDefs[i];
2481
2482 // Add it.
2483 if (AddSubClass(CurRec, SubClass))
2484 return true;
2485
2486 // Process any variables on the let stack.
2487 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2488 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2489 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2490 LetStack[i][j].Bits, LetStack[i][j].Value))
2491 return true;
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002492 }
2493
2494 if (Lex.getCode() != tgtok::comma) break;
2495 Lex.Lex(); // eat ','.
2496 SubClass = ParseSubClassReference(0, false);
2497 }
2498 }
2499
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002500 if (!CurMultiClass)
2501 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene0d886402011-08-10 18:27:46 +00002502 // See Record::setName(). This resolve step will see any new
2503 // name for the def that might have been created when resolving
2504 // inheritance, values and arguments above.
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002505 NewRecDefs[i]->resolveReferences();
2506
Chris Lattnerf4601652007-11-22 20:49:04 +00002507 if (Lex.getCode() != tgtok::semi)
2508 return TokError("expected ';' at end of defm");
2509 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002510
Chris Lattnerf4601652007-11-22 20:49:04 +00002511 return false;
2512}
2513
2514/// ParseObject
2515/// Object ::= ClassInst
2516/// Object ::= DefInst
2517/// Object ::= MultiClassInst
2518/// Object ::= DefMInst
2519/// Object ::= LETCommand '{' ObjectList '}'
2520/// Object ::= LETCommand Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002521bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002522 switch (Lex.getCode()) {
Chris Lattnerd6d9dd92010-10-31 19:27:15 +00002523 default:
2524 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002525 case tgtok::Let: return ParseTopLevelLet(MC);
2526 case tgtok::Def: return ParseDef(MC);
David Greenecebb4ee2012-02-22 16:09:41 +00002527 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002528 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4601652007-11-22 20:49:04 +00002529 case tgtok::Class: return ParseClass();
2530 case tgtok::MultiClass: return ParseMultiClass();
2531 }
2532}
2533
2534/// ParseObjectList
2535/// ObjectList :== Object*
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002536bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002537 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002538 if (ParseObject(MC))
Chris Lattnerf4601652007-11-22 20:49:04 +00002539 return true;
2540 }
2541 return false;
2542}
2543
Chris Lattnerf4601652007-11-22 20:49:04 +00002544bool TGParser::ParseFile() {
2545 Lex.Lex(); // Prime the lexer.
2546 if (ParseObjectList()) return true;
Bob Wilson21870412009-11-22 04:24:42 +00002547
Chris Lattnerf4601652007-11-22 20:49:04 +00002548 // If we have unread input at the end of the file, report it.
2549 if (Lex.getCode() == tgtok::Eof)
2550 return false;
Bob Wilson21870412009-11-22 04:24:42 +00002551
Chris Lattnerf4601652007-11-22 20:49:04 +00002552 return TokError("Unexpected input at top level");
2553}
2554