blob: b23f410013616b3603c0ce2f055f5df9a58c5c6b [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.
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000295bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
296 if (Loops.empty())
297 return false;
298
David Greenecebb4ee2012-02-22 16:09:41 +0000299 // We want to instantiate a new copy of CurRec for each combination
300 // of nested loop iterator values. We don't want top instantiate
301 // any copies until we have values for each loop iterator.
302 IterSet IterVals;
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000303 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenecebb4ee2012-02-22 16:09:41 +0000304}
305
306/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
307/// apply each of the variable values in this loop and then process
308/// subloops.
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000309bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
310 // Recursively build a tuple of iterator values.
311 if (IterVals.size() != Loops.size()) {
312 assert(IterVals.size() < Loops.size());
313 ForeachLoop &CurLoop = Loops[IterVals.size()];
314 ListInit *List = dynamic_cast<ListInit *>(CurLoop.ListValue);
315 if (List == 0) {
316 Error(Loc, "Loop list is not a list");
317 return true;
318 }
David Greenecebb4ee2012-02-22 16:09:41 +0000319
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000320 // Process each value.
321 for (int64_t i = 0; i < List->getSize(); ++i) {
322 Init *ItemVal = List->resolveListElementReference(*CurRec, 0, i);
323 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
324 if (ProcessForeachDefs(CurRec, Loc, IterVals))
325 return true;
326 IterVals.pop_back();
327 }
328 return false;
329 }
330
331 // This is the bottom of the recursion. We have all of the iterator values
332 // for this point in the iteration space. Instantiate a new record to
333 // reflect this combination of values.
334 Record *IterRec = new Record(*CurRec);
335
336 // Set the iterator values now.
337 for (unsigned i = 0, e = IterVals.size(); i != e; ++i) {
338 VarInit *IterVar = IterVals[i].IterVar;
339 TypedInit *IVal = dynamic_cast<TypedInit *>(IterVals[i].IterValue);
340 if (IVal == 0) {
341 Error(Loc, "foreach iterator value is untyped");
342 return true;
343 }
344
345 IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false));
346
347 if (SetValue(IterRec, Loc, IterVar->getName(),
348 std::vector<unsigned>(), IVal)) {
349 Error(Loc, "when instantiating this def");
350 return true;
351 }
352
353 // Resolve it next.
354 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName()));
355
356 // Remove it.
357 IterRec->removeValue(IterVar->getName());
358 }
359
360 if (Records.getDef(IterRec->getNameInitAsString())) {
361 Error(Loc, "def already exists: " + IterRec->getNameInitAsString());
David Greenecebb4ee2012-02-22 16:09:41 +0000362 return true;
363 }
364
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000365 Records.addDef(IterRec);
366 IterRec->resolveReferences();
David Greenecebb4ee2012-02-22 16:09:41 +0000367 return false;
368}
369
Chris Lattnerf4601652007-11-22 20:49:04 +0000370//===----------------------------------------------------------------------===//
371// Parser Code
372//===----------------------------------------------------------------------===//
373
374/// isObjectStart - Return true if this is a valid first token for an Object.
375static bool isObjectStart(tgtok::TokKind K) {
376 return K == tgtok::Class || K == tgtok::Def ||
David Greenecebb4ee2012-02-22 16:09:41 +0000377 K == tgtok::Defm || K == tgtok::Let ||
378 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4601652007-11-22 20:49:04 +0000379}
380
Chris Lattnerdf72eae2010-10-05 22:51:56 +0000381static std::string GetNewAnonymousName() {
382 static unsigned AnonCounter = 0;
383 return "anonymous."+utostr(AnonCounter++);
384}
385
Chris Lattnerf4601652007-11-22 20:49:04 +0000386/// ParseObjectName - If an object name is specified, return it. Otherwise,
387/// return an anonymous name.
David Greenea9e07dd2011-10-19 13:04:29 +0000388/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4601652007-11-22 20:49:04 +0000389/// ObjectName ::= /*empty*/
390///
David Greenea9e07dd2011-10-19 13:04:29 +0000391Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
392 switch (Lex.getCode()) {
393 case tgtok::colon:
394 case tgtok::semi:
395 case tgtok::l_brace:
396 // These are all of the tokens that can begin an object body.
397 // Some of these can also begin values but we disallow those cases
398 // because they are unlikely to be useful.
399 return StringInit::get(GetNewAnonymousName());
David Greenea9e07dd2011-10-19 13:04:29 +0000400 default:
401 break;
402 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000403
David Greenea9e07dd2011-10-19 13:04:29 +0000404 Record *CurRec = 0;
405 if (CurMultiClass)
406 CurRec = &CurMultiClass->Rec;
407
408 RecTy *Type = 0;
409 if (CurRec) {
410 const TypedInit *CurRecName =
411 dynamic_cast<const TypedInit *>(CurRec->getNameInit());
412 if (!CurRecName) {
413 TokError("Record name is not typed!");
414 return 0;
415 }
416 Type = CurRecName->getType();
417 }
418
419 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4601652007-11-22 20:49:04 +0000420}
421
Chris Lattnerf4601652007-11-22 20:49:04 +0000422/// ParseClassID - Parse and resolve a reference to a class name. This returns
423/// null on error.
424///
425/// ClassID ::= ID
426///
427Record *TGParser::ParseClassID() {
428 if (Lex.getCode() != tgtok::Id) {
429 TokError("expected name for ClassID");
430 return 0;
431 }
Bob Wilson21870412009-11-22 04:24:42 +0000432
Chris Lattnerf4601652007-11-22 20:49:04 +0000433 Record *Result = Records.getClass(Lex.getCurStrVal());
434 if (Result == 0)
435 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson21870412009-11-22 04:24:42 +0000436
Chris Lattnerf4601652007-11-22 20:49:04 +0000437 Lex.Lex();
438 return Result;
439}
440
Bob Wilson32558652009-04-28 19:41:44 +0000441/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
442/// This returns null on error.
David Greenede444af2009-04-22 16:42:54 +0000443///
444/// MultiClassID ::= ID
445///
446MultiClass *TGParser::ParseMultiClassID() {
447 if (Lex.getCode() != tgtok::Id) {
448 TokError("expected name for ClassID");
449 return 0;
450 }
Bob Wilson32558652009-04-28 19:41:44 +0000451
David Greenede444af2009-04-22 16:42:54 +0000452 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
453 if (Result == 0)
454 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson32558652009-04-28 19:41:44 +0000455
David Greenede444af2009-04-22 16:42:54 +0000456 Lex.Lex();
457 return Result;
458}
459
Chris Lattnerf4601652007-11-22 20:49:04 +0000460Record *TGParser::ParseDefmID() {
461 if (Lex.getCode() != tgtok::Id) {
462 TokError("expected multiclass name");
463 return 0;
464 }
Bob Wilson21870412009-11-22 04:24:42 +0000465
Chris Lattnerf4601652007-11-22 20:49:04 +0000466 MultiClass *MC = MultiClasses[Lex.getCurStrVal()];
467 if (MC == 0) {
468 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
469 return 0;
470 }
Bob Wilson21870412009-11-22 04:24:42 +0000471
Chris Lattnerf4601652007-11-22 20:49:04 +0000472 Lex.Lex();
473 return &MC->Rec;
Bob Wilson21870412009-11-22 04:24:42 +0000474}
Chris Lattnerf4601652007-11-22 20:49:04 +0000475
476
477/// ParseSubClassReference - Parse a reference to a subclass or to a templated
478/// subclass. This returns a SubClassRefTy with a null Record* on error.
479///
480/// SubClassRef ::= ClassID
481/// SubClassRef ::= ClassID '<' ValueList '>'
482///
483SubClassReference TGParser::
484ParseSubClassReference(Record *CurRec, bool isDefm) {
485 SubClassReference Result;
486 Result.RefLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +0000487
Chris Lattnerf4601652007-11-22 20:49:04 +0000488 if (isDefm)
489 Result.Rec = ParseDefmID();
490 else
491 Result.Rec = ParseClassID();
492 if (Result.Rec == 0) return Result;
Bob Wilson21870412009-11-22 04:24:42 +0000493
Chris Lattnerf4601652007-11-22 20:49:04 +0000494 // If there is no template arg list, we're done.
495 if (Lex.getCode() != tgtok::less)
496 return Result;
497 Lex.Lex(); // Eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000498
Chris Lattnerf4601652007-11-22 20:49:04 +0000499 if (Lex.getCode() == tgtok::greater) {
500 TokError("subclass reference requires a non-empty list of template values");
501 Result.Rec = 0;
502 return Result;
503 }
Bob Wilson21870412009-11-22 04:24:42 +0000504
David Greenee1b46912009-06-08 20:23:18 +0000505 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4601652007-11-22 20:49:04 +0000506 if (Result.TemplateArgs.empty()) {
507 Result.Rec = 0; // Error parsing value list.
508 return Result;
509 }
Bob Wilson21870412009-11-22 04:24:42 +0000510
Chris Lattnerf4601652007-11-22 20:49:04 +0000511 if (Lex.getCode() != tgtok::greater) {
512 TokError("expected '>' in template value list");
513 Result.Rec = 0;
514 return Result;
515 }
516 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000517
Chris Lattnerf4601652007-11-22 20:49:04 +0000518 return Result;
519}
520
Bob Wilson32558652009-04-28 19:41:44 +0000521/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
522/// templated submulticlass. This returns a SubMultiClassRefTy with a null
523/// Record* on error.
David Greenede444af2009-04-22 16:42:54 +0000524///
525/// SubMultiClassRef ::= MultiClassID
526/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
527///
528SubMultiClassReference TGParser::
529ParseSubMultiClassReference(MultiClass *CurMC) {
530 SubMultiClassReference Result;
531 Result.RefLoc = Lex.getLoc();
Bob Wilson32558652009-04-28 19:41:44 +0000532
David Greenede444af2009-04-22 16:42:54 +0000533 Result.MC = ParseMultiClassID();
534 if (Result.MC == 0) return Result;
Bob Wilson32558652009-04-28 19:41:44 +0000535
David Greenede444af2009-04-22 16:42:54 +0000536 // If there is no template arg list, we're done.
537 if (Lex.getCode() != tgtok::less)
538 return Result;
539 Lex.Lex(); // Eat the '<'
Bob Wilson32558652009-04-28 19:41:44 +0000540
David Greenede444af2009-04-22 16:42:54 +0000541 if (Lex.getCode() == tgtok::greater) {
542 TokError("subclass reference requires a non-empty list of template values");
543 Result.MC = 0;
544 return Result;
545 }
Bob Wilson32558652009-04-28 19:41:44 +0000546
David Greenee1b46912009-06-08 20:23:18 +0000547 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greenede444af2009-04-22 16:42:54 +0000548 if (Result.TemplateArgs.empty()) {
549 Result.MC = 0; // Error parsing value list.
550 return Result;
551 }
Bob Wilson32558652009-04-28 19:41:44 +0000552
David Greenede444af2009-04-22 16:42:54 +0000553 if (Lex.getCode() != tgtok::greater) {
554 TokError("expected '>' in template value list");
555 Result.MC = 0;
556 return Result;
557 }
558 Lex.Lex();
559
560 return Result;
561}
562
Chris Lattnerf4601652007-11-22 20:49:04 +0000563/// ParseRangePiece - Parse a bit/value range.
564/// RangePiece ::= INTVAL
565/// RangePiece ::= INTVAL '-' INTVAL
566/// RangePiece ::= INTVAL INTVAL
567bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner811281e2008-01-10 07:01:53 +0000568 if (Lex.getCode() != tgtok::IntVal) {
569 TokError("expected integer or bitrange");
570 return true;
571 }
Dan Gohman63f97202008-10-17 01:33:43 +0000572 int64_t Start = Lex.getCurIntVal();
573 int64_t End;
Bob Wilson21870412009-11-22 04:24:42 +0000574
Chris Lattnerf4601652007-11-22 20:49:04 +0000575 if (Start < 0)
576 return TokError("invalid range, cannot be negative");
Bob Wilson21870412009-11-22 04:24:42 +0000577
Chris Lattnerf4601652007-11-22 20:49:04 +0000578 switch (Lex.Lex()) { // eat first character.
Bob Wilson21870412009-11-22 04:24:42 +0000579 default:
Chris Lattnerf4601652007-11-22 20:49:04 +0000580 Ranges.push_back(Start);
581 return false;
582 case tgtok::minus:
583 if (Lex.Lex() != tgtok::IntVal) {
584 TokError("expected integer value as end of range");
585 return true;
586 }
587 End = Lex.getCurIntVal();
588 break;
589 case tgtok::IntVal:
590 End = -Lex.getCurIntVal();
591 break;
592 }
Bob Wilson21870412009-11-22 04:24:42 +0000593 if (End < 0)
Chris Lattnerf4601652007-11-22 20:49:04 +0000594 return TokError("invalid range, cannot be negative");
595 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000596
Chris Lattnerf4601652007-11-22 20:49:04 +0000597 // Add to the range.
598 if (Start < End) {
599 for (; Start <= End; ++Start)
600 Ranges.push_back(Start);
601 } else {
602 for (; Start >= End; --Start)
603 Ranges.push_back(Start);
604 }
605 return false;
606}
607
608/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
609///
610/// RangeList ::= RangePiece (',' RangePiece)*
611///
612std::vector<unsigned> TGParser::ParseRangeList() {
613 std::vector<unsigned> Result;
Bob Wilson21870412009-11-22 04:24:42 +0000614
Chris Lattnerf4601652007-11-22 20:49:04 +0000615 // Parse the first piece.
616 if (ParseRangePiece(Result))
617 return std::vector<unsigned>();
618 while (Lex.getCode() == tgtok::comma) {
619 Lex.Lex(); // Eat the comma.
620
621 // Parse the next range piece.
622 if (ParseRangePiece(Result))
623 return std::vector<unsigned>();
624 }
625 return Result;
626}
627
628/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
629/// OptionalRangeList ::= '<' RangeList '>'
630/// OptionalRangeList ::= /*empty*/
631bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
632 if (Lex.getCode() != tgtok::less)
633 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000634
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000635 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000636 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000637
Chris Lattnerf4601652007-11-22 20:49:04 +0000638 // Parse the range list.
639 Ranges = ParseRangeList();
640 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000641
Chris Lattnerf4601652007-11-22 20:49:04 +0000642 if (Lex.getCode() != tgtok::greater) {
643 TokError("expected '>' at end of range list");
644 return Error(StartLoc, "to match this '<'");
645 }
646 Lex.Lex(); // eat the '>'.
647 return false;
648}
649
650/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
651/// OptionalBitList ::= '{' RangeList '}'
652/// OptionalBitList ::= /*empty*/
653bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
654 if (Lex.getCode() != tgtok::l_brace)
655 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000656
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000657 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000658 Lex.Lex(); // eat the '{'
Bob Wilson21870412009-11-22 04:24:42 +0000659
Chris Lattnerf4601652007-11-22 20:49:04 +0000660 // Parse the range list.
661 Ranges = ParseRangeList();
662 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000663
Chris Lattnerf4601652007-11-22 20:49:04 +0000664 if (Lex.getCode() != tgtok::r_brace) {
665 TokError("expected '}' at end of bit list");
666 return Error(StartLoc, "to match this '{'");
667 }
668 Lex.Lex(); // eat the '}'.
669 return false;
670}
671
672
673/// ParseType - Parse and return a tblgen type. This returns null on error.
674///
675/// Type ::= STRING // string type
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +0000676/// Type ::= CODE // code type
Chris Lattnerf4601652007-11-22 20:49:04 +0000677/// Type ::= BIT // bit type
678/// Type ::= BITS '<' INTVAL '>' // bits<x> type
679/// Type ::= INT // int type
680/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4601652007-11-22 20:49:04 +0000681/// Type ::= DAG // dag type
682/// Type ::= ClassID // Record Type
683///
684RecTy *TGParser::ParseType() {
685 switch (Lex.getCode()) {
686 default: TokError("Unknown token when expecting a type"); return 0;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000687 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +0000688 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000689 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
690 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000691 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4601652007-11-22 20:49:04 +0000692 case tgtok::Id:
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000693 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Chris Lattnerf4601652007-11-22 20:49:04 +0000694 return 0;
695 case tgtok::Bits: {
696 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
697 TokError("expected '<' after bits type");
698 return 0;
699 }
700 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
701 TokError("expected integer in bits<n> type");
702 return 0;
703 }
Dan Gohman63f97202008-10-17 01:33:43 +0000704 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4601652007-11-22 20:49:04 +0000705 if (Lex.Lex() != tgtok::greater) { // Eat count.
706 TokError("expected '>' at end of bits<n> type");
707 return 0;
708 }
709 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000710 return BitsRecTy::get(Val);
Chris Lattnerf4601652007-11-22 20:49:04 +0000711 }
712 case tgtok::List: {
713 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
714 TokError("expected '<' after list type");
715 return 0;
716 }
717 Lex.Lex(); // Eat '<'
718 RecTy *SubType = ParseType();
719 if (SubType == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +0000720
Chris Lattnerf4601652007-11-22 20:49:04 +0000721 if (Lex.getCode() != tgtok::greater) {
722 TokError("expected '>' at end of list<ty> type");
723 return 0;
724 }
725 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000726 return ListRecTy::get(SubType);
Chris Lattnerf4601652007-11-22 20:49:04 +0000727 }
Bob Wilson21870412009-11-22 04:24:42 +0000728 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000729}
730
731/// ParseIDValue - Parse an ID as a value and decode what it means.
732///
733/// IDValue ::= ID [def local value]
734/// IDValue ::= ID [def template arg]
735/// IDValue ::= ID [multiclass local value]
736/// IDValue ::= ID [multiclass template argument]
737/// IDValue ::= ID [def name]
738///
David Greenef3744a02011-10-19 13:04:20 +0000739Init *TGParser::ParseIDValue(Record *CurRec, IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000740 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
741 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000742 SMLoc Loc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000743 Lex.Lex();
744 return ParseIDValue(CurRec, Name, Loc);
745}
746
747/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
748/// has already been read.
David Greene05bce0b2011-07-29 22:43:06 +0000749Init *TGParser::ParseIDValue(Record *CurRec,
David Greenef3744a02011-10-19 13:04:20 +0000750 const std::string &Name, SMLoc NameLoc,
751 IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000752 if (CurRec) {
753 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenedcd35c72011-07-29 19:07:07 +0000754 return VarInit::get(Name, RV->getType());
Bob Wilson21870412009-11-22 04:24:42 +0000755
David Greenee22b3212011-10-19 13:02:42 +0000756 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
757
David Greenecaa25c82011-10-05 22:42:54 +0000758 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +0000759 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
760 "::");
David Greenecaa25c82011-10-05 22:42:54 +0000761
Chris Lattnerf4601652007-11-22 20:49:04 +0000762 if (CurRec->isTemplateArg(TemplateArgName)) {
763 const RecordVal *RV = CurRec->getValue(TemplateArgName);
764 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000765 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000766 }
767 }
Bob Wilson21870412009-11-22 04:24:42 +0000768
Chris Lattnerf4601652007-11-22 20:49:04 +0000769 if (CurMultiClass) {
David Greenee22b3212011-10-19 13:02:42 +0000770 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
771 "::");
772
Chris Lattnerf4601652007-11-22 20:49:04 +0000773 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
774 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
775 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000776 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000777 }
778 }
Bob Wilson21870412009-11-22 04:24:42 +0000779
David Greenecebb4ee2012-02-22 16:09:41 +0000780 // If this is in a foreach loop, make sure it's not a loop iterator
781 for (LoopVector::iterator i = Loops.begin(), iend = Loops.end();
782 i != iend;
783 ++i) {
784 VarInit *IterVar = dynamic_cast<VarInit *>(i->IterVar);
785 if (IterVar && IterVar->getName() == Name)
786 return IterVar;
787 }
788
David Greenebbec2792011-10-19 13:04:21 +0000789 if (Mode == ParseNameMode)
790 return StringInit::get(Name);
791
Chris Lattnerf4601652007-11-22 20:49:04 +0000792 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000793 return DefInit::get(D);
Chris Lattnerf4601652007-11-22 20:49:04 +0000794
David Greenebbec2792011-10-19 13:04:21 +0000795 if (Mode == ParseValueMode) {
796 Error(NameLoc, "Variable not defined: '" + Name + "'");
797 return 0;
798 }
799
800 return StringInit::get(Name);
Chris Lattnerf4601652007-11-22 20:49:04 +0000801}
802
David Greened418c1b2009-05-14 20:54:48 +0000803/// ParseOperation - Parse an operator. This returns null on error.
804///
805/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
806///
David Greene05bce0b2011-07-29 22:43:06 +0000807Init *TGParser::ParseOperation(Record *CurRec) {
David Greened418c1b2009-05-14 20:54:48 +0000808 switch (Lex.getCode()) {
809 default:
810 TokError("unknown operation");
811 return 0;
David Greene1434f662011-01-07 17:05:37 +0000812 case tgtok::XHead:
813 case tgtok::XTail:
814 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +0000815 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
816 UnOpInit::UnaryOp Code;
817 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000818
David Greenee6c27de2009-05-14 21:22:49 +0000819 switch (Lex.getCode()) {
Craig Topper85814382012-02-07 05:05:23 +0000820 default: llvm_unreachable("Unhandled code!");
David Greenee6c27de2009-05-14 21:22:49 +0000821 case tgtok::XCast:
822 Lex.Lex(); // eat the operation
823 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000824
David Greenee6c27de2009-05-14 21:22:49 +0000825 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000826
David Greenee6c27de2009-05-14 21:22:49 +0000827 if (Type == 0) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000828 TokError("did not get type for unary operator");
David Greenee6c27de2009-05-14 21:22:49 +0000829 return 0;
830 }
David Greened418c1b2009-05-14 20:54:48 +0000831
David Greenee6c27de2009-05-14 21:22:49 +0000832 break;
David Greene1434f662011-01-07 17:05:37 +0000833 case tgtok::XHead:
David Greene5f9f9ba2009-05-14 22:38:31 +0000834 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000835 Code = UnOpInit::HEAD;
David Greene5f9f9ba2009-05-14 22:38:31 +0000836 break;
David Greene1434f662011-01-07 17:05:37 +0000837 case tgtok::XTail:
David Greene5f9f9ba2009-05-14 22:38:31 +0000838 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000839 Code = UnOpInit::TAIL;
David Greene5f9f9ba2009-05-14 22:38:31 +0000840 break;
David Greene1434f662011-01-07 17:05:37 +0000841 case tgtok::XEmpty:
David Greene5f9f9ba2009-05-14 22:38:31 +0000842 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000843 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000844 Type = IntRecTy::get();
David Greene5f9f9ba2009-05-14 22:38:31 +0000845 break;
David Greenee6c27de2009-05-14 21:22:49 +0000846 }
847 if (Lex.getCode() != tgtok::l_paren) {
848 TokError("expected '(' after unary operator");
849 return 0;
850 }
851 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000852
David Greene05bce0b2011-07-29 22:43:06 +0000853 Init *LHS = ParseValue(CurRec);
David Greenee6c27de2009-05-14 21:22:49 +0000854 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000855
David Greene1434f662011-01-07 17:05:37 +0000856 if (Code == UnOpInit::HEAD
857 || Code == UnOpInit::TAIL
858 || Code == UnOpInit::EMPTY) {
David Greene05bce0b2011-07-29 22:43:06 +0000859 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
860 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
861 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000862 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
863 TokError("expected list or string type argument in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000864 return 0;
865 }
866 if (LHSt) {
867 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
David Greenee1b46912009-06-08 20:23:18 +0000868 StringRecTy *SType = dynamic_cast<StringRecTy*>(LHSt->getType());
869 if (LType == 0 && SType == 0) {
870 TokError("expected list or string type argumnet in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000871 return 0;
872 }
873 }
874
David Greene1434f662011-01-07 17:05:37 +0000875 if (Code == UnOpInit::HEAD
876 || Code == UnOpInit::TAIL) {
David Greenee1b46912009-06-08 20:23:18 +0000877 if (LHSl == 0 && LHSt == 0) {
878 TokError("expected list type argumnet in unary operator");
879 return 0;
880 }
Bob Wilson21870412009-11-22 04:24:42 +0000881
David Greene5f9f9ba2009-05-14 22:38:31 +0000882 if (LHSl && LHSl->getSize() == 0) {
883 TokError("empty list argument in unary operator");
884 return 0;
885 }
886 if (LHSl) {
David Greene05bce0b2011-07-29 22:43:06 +0000887 Init *Item = LHSl->getElement(0);
888 TypedInit *Itemt = dynamic_cast<TypedInit*>(Item);
David Greene5f9f9ba2009-05-14 22:38:31 +0000889 if (Itemt == 0) {
890 TokError("untyped list element in unary operator");
891 return 0;
892 }
David Greene1434f662011-01-07 17:05:37 +0000893 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000894 Type = Itemt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000895 } else {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000896 Type = ListRecTy::get(Itemt->getType());
David Greene5f9f9ba2009-05-14 22:38:31 +0000897 }
Bob Wilson21870412009-11-22 04:24:42 +0000898 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000899 assert(LHSt && "expected list type argument in unary operator");
900 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
901 if (LType == 0) {
902 TokError("expected list type argumnet in unary operator");
903 return 0;
904 }
David Greene1434f662011-01-07 17:05:37 +0000905 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000906 Type = LType->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +0000907 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000908 Type = LType;
909 }
910 }
911 }
912 }
913
David Greenee6c27de2009-05-14 21:22:49 +0000914 if (Lex.getCode() != tgtok::r_paren) {
915 TokError("expected ')' in unary operator");
916 return 0;
917 }
918 Lex.Lex(); // eat the ')'
David Greenedcd35c72011-07-29 19:07:07 +0000919 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee6c27de2009-05-14 21:22:49 +0000920 }
David Greened418c1b2009-05-14 20:54:48 +0000921
922 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +0000923 case tgtok::XSRA:
David Greened418c1b2009-05-14 20:54:48 +0000924 case tgtok::XSRL:
925 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +0000926 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +0000927 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000928 tgtok::TokKind OpTok = Lex.getCode();
929 SMLoc OpLoc = Lex.getLoc();
930 Lex.Lex(); // eat the operation
931
David Greened418c1b2009-05-14 20:54:48 +0000932 BinOpInit::BinaryOp Code;
933 RecTy *Type = 0;
934
Chris Lattner8d978a72010-10-05 23:58:18 +0000935 switch (OpTok) {
Craig Topper85814382012-02-07 05:05:23 +0000936 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000937 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
938 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
939 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
940 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
941 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Bob Wilson21870412009-11-22 04:24:42 +0000942 case tgtok::XStrConcat:
David Greened418c1b2009-05-14 20:54:48 +0000943 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000944 Type = StringRecTy::get();
David Greened418c1b2009-05-14 20:54:48 +0000945 break;
David Greened418c1b2009-05-14 20:54:48 +0000946 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000947
David Greened418c1b2009-05-14 20:54:48 +0000948 if (Lex.getCode() != tgtok::l_paren) {
949 TokError("expected '(' after binary operator");
950 return 0;
951 }
952 Lex.Lex(); // eat the '('
953
David Greene05bce0b2011-07-29 22:43:06 +0000954 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000955
Chris Lattner8d978a72010-10-05 23:58:18 +0000956 InitList.push_back(ParseValue(CurRec));
957 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000958
Chris Lattner8d978a72010-10-05 23:58:18 +0000959 while (Lex.getCode() == tgtok::comma) {
960 Lex.Lex(); // eat the ','
961
962 InitList.push_back(ParseValue(CurRec));
963 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000964 }
David Greened418c1b2009-05-14 20:54:48 +0000965
966 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner8d978a72010-10-05 23:58:18 +0000967 TokError("expected ')' in operator");
David Greened418c1b2009-05-14 20:54:48 +0000968 return 0;
969 }
970 Lex.Lex(); // eat the ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000971
972 // We allow multiple operands to associative operators like !strconcat as
973 // shorthand for nesting them.
974 if (Code == BinOpInit::STRCONCAT) {
975 while (InitList.size() > 2) {
David Greene05bce0b2011-07-29 22:43:06 +0000976 Init *RHS = InitList.pop_back_val();
David Greenedcd35c72011-07-29 19:07:07 +0000977 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
978 ->Fold(CurRec, CurMultiClass);
Chris Lattner8d978a72010-10-05 23:58:18 +0000979 InitList.back() = RHS;
980 }
981 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000982
Chris Lattner8d978a72010-10-05 23:58:18 +0000983 if (InitList.size() == 2)
David Greenedcd35c72011-07-29 19:07:07 +0000984 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner8d978a72010-10-05 23:58:18 +0000985 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000986
Chris Lattner8d978a72010-10-05 23:58:18 +0000987 Error(OpLoc, "expected two operands to operator");
988 return 0;
David Greened418c1b2009-05-14 20:54:48 +0000989 }
990
David Greene9bea7c82009-05-14 23:26:46 +0000991 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +0000992 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +0000993 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
994 TernOpInit::TernaryOp Code;
995 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000996
David Greene4afc5092009-05-14 21:54:42 +0000997 tgtok::TokKind LexCode = Lex.getCode();
998 Lex.Lex(); // eat the operation
999 switch (LexCode) {
Craig Topper85814382012-02-07 05:05:23 +00001000 default: llvm_unreachable("Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +00001001 case tgtok::XIf:
1002 Code = TernOpInit::IF;
1003 break;
David Greenebeb31a52009-05-14 22:23:47 +00001004 case tgtok::XForEach:
1005 Code = TernOpInit::FOREACH;
1006 break;
David Greene4afc5092009-05-14 21:54:42 +00001007 case tgtok::XSubst:
1008 Code = TernOpInit::SUBST;
1009 break;
1010 }
1011 if (Lex.getCode() != tgtok::l_paren) {
1012 TokError("expected '(' after ternary operator");
1013 return 0;
1014 }
1015 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +00001016
David Greene05bce0b2011-07-29 22:43:06 +00001017 Init *LHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001018 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001019
David Greene4afc5092009-05-14 21:54:42 +00001020 if (Lex.getCode() != tgtok::comma) {
1021 TokError("expected ',' in ternary operator");
1022 return 0;
1023 }
1024 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001025
David Greene05bce0b2011-07-29 22:43:06 +00001026 Init *MHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001027 if (MHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001028
David Greene4afc5092009-05-14 21:54:42 +00001029 if (Lex.getCode() != tgtok::comma) {
1030 TokError("expected ',' in ternary operator");
1031 return 0;
1032 }
1033 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001034
David Greene05bce0b2011-07-29 22:43:06 +00001035 Init *RHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001036 if (RHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001037
David Greene4afc5092009-05-14 21:54:42 +00001038 if (Lex.getCode() != tgtok::r_paren) {
1039 TokError("expected ')' in binary operator");
1040 return 0;
1041 }
1042 Lex.Lex(); // eat the ')'
David Greened418c1b2009-05-14 20:54:48 +00001043
David Greene4afc5092009-05-14 21:54:42 +00001044 switch (LexCode) {
Craig Topper85814382012-02-07 05:05:23 +00001045 default: llvm_unreachable("Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +00001046 case tgtok::XIf: {
Bill Wendling548f5a02010-12-13 01:46:19 +00001047 // FIXME: The `!if' operator doesn't handle non-TypedInit well at
1048 // all. This can be made much more robust.
David Greene05bce0b2011-07-29 22:43:06 +00001049 TypedInit *MHSt = dynamic_cast<TypedInit*>(MHS);
1050 TypedInit *RHSt = dynamic_cast<TypedInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +00001051
1052 RecTy *MHSTy = 0;
1053 RecTy *RHSTy = 0;
1054
1055 if (MHSt == 0 && RHSt == 0) {
David Greene05bce0b2011-07-29 22:43:06 +00001056 BitsInit *MHSbits = dynamic_cast<BitsInit*>(MHS);
1057 BitsInit *RHSbits = dynamic_cast<BitsInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +00001058
1059 if (MHSbits && RHSbits &&
1060 MHSbits->getNumBits() == RHSbits->getNumBits()) {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +00001061 Type = BitRecTy::get();
Bill Wendling548f5a02010-12-13 01:46:19 +00001062 break;
1063 } else {
David Greene05bce0b2011-07-29 22:43:06 +00001064 BitInit *MHSbit = dynamic_cast<BitInit*>(MHS);
1065 BitInit *RHSbit = dynamic_cast<BitInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +00001066
1067 if (MHSbit && RHSbit) {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +00001068 Type = BitRecTy::get();
Bill Wendling548f5a02010-12-13 01:46:19 +00001069 break;
1070 }
1071 }
1072 } else if (MHSt != 0 && RHSt != 0) {
1073 MHSTy = MHSt->getType();
1074 RHSTy = RHSt->getType();
1075 }
1076
1077 if (!MHSTy || !RHSTy) {
David Greene9bea7c82009-05-14 23:26:46 +00001078 TokError("could not get type for !if");
1079 return 0;
1080 }
Bill Wendling548f5a02010-12-13 01:46:19 +00001081
1082 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1083 Type = RHSTy;
1084 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1085 Type = MHSTy;
Bob Wilson21870412009-11-22 04:24:42 +00001086 } else {
David Greene9bea7c82009-05-14 23:26:46 +00001087 TokError("inconsistent types for !if");
1088 return 0;
1089 }
1090 break;
1091 }
David Greenebeb31a52009-05-14 22:23:47 +00001092 case tgtok::XForEach: {
David Greene05bce0b2011-07-29 22:43:06 +00001093 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
David Greenebeb31a52009-05-14 22:23:47 +00001094 if (MHSt == 0) {
1095 TokError("could not get type for !foreach");
1096 return 0;
1097 }
1098 Type = MHSt->getType();
1099 break;
1100 }
David Greene4afc5092009-05-14 21:54:42 +00001101 case tgtok::XSubst: {
David Greene05bce0b2011-07-29 22:43:06 +00001102 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
David Greene4afc5092009-05-14 21:54:42 +00001103 if (RHSt == 0) {
1104 TokError("could not get type for !subst");
1105 return 0;
1106 }
1107 Type = RHSt->getType();
1108 break;
1109 }
1110 }
David Greenedcd35c72011-07-29 19:07:07 +00001111 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson21870412009-11-22 04:24:42 +00001112 CurMultiClass);
David Greene4afc5092009-05-14 21:54:42 +00001113 }
David Greened418c1b2009-05-14 20:54:48 +00001114 }
David Greened418c1b2009-05-14 20:54:48 +00001115}
1116
1117/// ParseOperatorType - Parse a type for an operator. This returns
1118/// null on error.
1119///
1120/// OperatorType ::= '<' Type '>'
1121///
Dan Gohmana9ad0412009-08-12 22:10:57 +00001122RecTy *TGParser::ParseOperatorType() {
David Greened418c1b2009-05-14 20:54:48 +00001123 RecTy *Type = 0;
1124
1125 if (Lex.getCode() != tgtok::less) {
1126 TokError("expected type name for operator");
1127 return 0;
1128 }
1129 Lex.Lex(); // eat the <
1130
1131 Type = ParseType();
1132
1133 if (Type == 0) {
1134 TokError("expected type name for operator");
1135 return 0;
1136 }
1137
1138 if (Lex.getCode() != tgtok::greater) {
1139 TokError("expected type name for operator");
1140 return 0;
1141 }
1142 Lex.Lex(); // eat the >
1143
1144 return Type;
1145}
1146
1147
Chris Lattnerf4601652007-11-22 20:49:04 +00001148/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1149///
1150/// SimpleValue ::= IDValue
1151/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001152/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +00001153/// SimpleValue ::= CODEFRAGMENT
1154/// SimpleValue ::= '?'
1155/// SimpleValue ::= '{' ValueList '}'
1156/// SimpleValue ::= ID '<' ValueListNE '>'
1157/// SimpleValue ::= '[' ValueList ']'
1158/// SimpleValue ::= '(' IDValue DagArgList ')'
1159/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
1160/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1161/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1162/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1163/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1164///
David Greenef3744a02011-10-19 13:04:20 +00001165Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1166 IDParseMode Mode) {
David Greene05bce0b2011-07-29 22:43:06 +00001167 Init *R = 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001168 switch (Lex.getCode()) {
1169 default: TokError("Unknown token when parsing a value"); break;
David Greened3d1cad2011-10-19 13:04:43 +00001170 case tgtok::paste:
1171 // This is a leading paste operation. This is deprecated but
1172 // still exists in some .td files. Ignore it.
1173 Lex.Lex(); // Skip '#'.
1174 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenedcd35c72011-07-29 19:07:07 +00001175 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001176 case tgtok::StrVal: {
1177 std::string Val = Lex.getCurStrVal();
1178 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001179
Jim Grosbachda4231f2009-03-26 16:17:51 +00001180 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001181 while (Lex.getCode() == tgtok::StrVal) {
1182 Val += Lex.getCurStrVal();
1183 Lex.Lex();
1184 }
Bob Wilson21870412009-11-22 04:24:42 +00001185
David Greenedcd35c72011-07-29 19:07:07 +00001186 R = StringInit::get(Val);
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001187 break;
1188 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001189 case tgtok::CodeFragment:
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +00001190 R = StringInit::get(Lex.getCurStrVal());
Chris Lattner578bcf02010-10-06 04:31:40 +00001191 Lex.Lex();
1192 break;
1193 case tgtok::question:
David Greenedcd35c72011-07-29 19:07:07 +00001194 R = UnsetInit::get();
Chris Lattner578bcf02010-10-06 04:31:40 +00001195 Lex.Lex();
1196 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001197 case tgtok::Id: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001198 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001199 std::string Name = Lex.getCurStrVal();
1200 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greenef3744a02011-10-19 13:04:20 +00001201 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson21870412009-11-22 04:24:42 +00001202
Chris Lattnerf4601652007-11-22 20:49:04 +00001203 // Value ::= ID '<' ValueListNE '>'
1204 if (Lex.Lex() == tgtok::greater) {
1205 TokError("expected non-empty value list");
1206 return 0;
1207 }
David Greenee1b46912009-06-08 20:23:18 +00001208
Chris Lattnerf4601652007-11-22 20:49:04 +00001209 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1210 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1211 // body.
1212 Record *Class = Records.getClass(Name);
1213 if (!Class) {
1214 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1215 return 0;
1216 }
David Greenee1b46912009-06-08 20:23:18 +00001217
David Greene05bce0b2011-07-29 22:43:06 +00001218 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
David Greenee1b46912009-06-08 20:23:18 +00001219 if (ValueList.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001220
David Greenee1b46912009-06-08 20:23:18 +00001221 if (Lex.getCode() != tgtok::greater) {
1222 TokError("expected '>' at end of value list");
1223 return 0;
1224 }
1225 Lex.Lex(); // eat the '>'
Bob Wilson21870412009-11-22 04:24:42 +00001226
Chris Lattnerf4601652007-11-22 20:49:04 +00001227 // Create the new record, set it as CurRec temporarily.
1228 static unsigned AnonCounter = 0;
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001229 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),
1230 NameLoc,
1231 Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001232 SubClassReference SCRef;
1233 SCRef.RefLoc = NameLoc;
1234 SCRef.Rec = Class;
1235 SCRef.TemplateArgs = ValueList;
1236 // Add info about the subclass to NewRec.
1237 if (AddSubClass(NewRec, SCRef))
1238 return 0;
1239 NewRec->resolveReferences();
1240 Records.addDef(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001241
Chris Lattnerf4601652007-11-22 20:49:04 +00001242 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +00001243 return DefInit::get(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001244 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001245 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001246 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001247 Lex.Lex(); // eat the '{'
David Greene05bce0b2011-07-29 22:43:06 +00001248 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001249
Chris Lattnerf4601652007-11-22 20:49:04 +00001250 if (Lex.getCode() != tgtok::r_brace) {
1251 Vals = ParseValueList(CurRec);
1252 if (Vals.empty()) return 0;
1253 }
1254 if (Lex.getCode() != tgtok::r_brace) {
1255 TokError("expected '}' at end of bit list value");
1256 return 0;
1257 }
1258 Lex.Lex(); // eat the '}'
Bob Wilson21870412009-11-22 04:24:42 +00001259
David Greene05bce0b2011-07-29 22:43:06 +00001260 SmallVector<Init *, 16> NewBits(Vals.size());
David Greeneca7fd3d2011-07-29 19:07:00 +00001261
Chris Lattnerf4601652007-11-22 20:49:04 +00001262 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001263 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Chris Lattnerf4601652007-11-22 20:49:04 +00001264 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +00001265 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1266 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +00001267 return 0;
1268 }
David Greeneca7fd3d2011-07-29 19:07:00 +00001269 NewBits[Vals.size()-i-1] = Bit;
Chris Lattnerf4601652007-11-22 20:49:04 +00001270 }
David Greenedcd35c72011-07-29 19:07:07 +00001271 return BitsInit::get(NewBits);
Chris Lattnerf4601652007-11-22 20:49:04 +00001272 }
1273 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1274 Lex.Lex(); // eat the '['
David Greene05bce0b2011-07-29 22:43:06 +00001275 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001276
David Greenee1b46912009-06-08 20:23:18 +00001277 RecTy *DeducedEltTy = 0;
1278 ListRecTy *GivenListTy = 0;
Bob Wilson21870412009-11-22 04:24:42 +00001279
David Greenee1b46912009-06-08 20:23:18 +00001280 if (ItemType != 0) {
1281 ListRecTy *ListType = dynamic_cast<ListRecTy*>(ItemType);
1282 if (ListType == 0) {
1283 std::stringstream s;
Bob Wilson21870412009-11-22 04:24:42 +00001284 s << "Type mismatch for list, expected list type, got "
David Greenee1b46912009-06-08 20:23:18 +00001285 << ItemType->getAsString();
1286 TokError(s.str());
Jim Grosbach6a44ada2011-03-11 19:52:52 +00001287 return 0;
David Greenee1b46912009-06-08 20:23:18 +00001288 }
1289 GivenListTy = ListType;
Bob Wilson21870412009-11-22 04:24:42 +00001290 }
David Greenee1b46912009-06-08 20:23:18 +00001291
Chris Lattnerf4601652007-11-22 20:49:04 +00001292 if (Lex.getCode() != tgtok::r_square) {
Bob Wilson21870412009-11-22 04:24:42 +00001293 Vals = ParseValueList(CurRec, 0,
1294 GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001295 if (Vals.empty()) return 0;
1296 }
1297 if (Lex.getCode() != tgtok::r_square) {
1298 TokError("expected ']' at end of list value");
1299 return 0;
1300 }
1301 Lex.Lex(); // eat the ']'
David Greenee1b46912009-06-08 20:23:18 +00001302
1303 RecTy *GivenEltTy = 0;
1304 if (Lex.getCode() == tgtok::less) {
1305 // Optional list element type
1306 Lex.Lex(); // eat the '<'
1307
1308 GivenEltTy = ParseType();
1309 if (GivenEltTy == 0) {
1310 // Couldn't parse element type
1311 return 0;
1312 }
1313
1314 if (Lex.getCode() != tgtok::greater) {
1315 TokError("expected '>' at end of list element type");
1316 return 0;
1317 }
1318 Lex.Lex(); // eat the '>'
1319 }
1320
1321 // Check elements
1322 RecTy *EltTy = 0;
David Greene05bce0b2011-07-29 22:43:06 +00001323 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greenee1b46912009-06-08 20:23:18 +00001324 i != ie;
1325 ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001326 TypedInit *TArg = dynamic_cast<TypedInit*>(*i);
David Greenee1b46912009-06-08 20:23:18 +00001327 if (TArg == 0) {
1328 TokError("Untyped list element");
1329 return 0;
1330 }
1331 if (EltTy != 0) {
1332 EltTy = resolveTypes(EltTy, TArg->getType());
1333 if (EltTy == 0) {
1334 TokError("Incompatible types in list elements");
1335 return 0;
1336 }
Bob Wilson21870412009-11-22 04:24:42 +00001337 } else {
David Greenee1b46912009-06-08 20:23:18 +00001338 EltTy = TArg->getType();
1339 }
1340 }
1341
1342 if (GivenEltTy != 0) {
1343 if (EltTy != 0) {
1344 // Verify consistency
1345 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1346 TokError("Incompatible types in list elements");
1347 return 0;
1348 }
1349 }
1350 EltTy = GivenEltTy;
1351 }
1352
1353 if (EltTy == 0) {
1354 if (ItemType == 0) {
1355 TokError("No type for list");
1356 return 0;
1357 }
1358 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +00001359 } else {
David Greenee1b46912009-06-08 20:23:18 +00001360 // Make sure the deduced type is compatible with the given type
1361 if (GivenListTy) {
1362 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1363 TokError("Element type mismatch for list");
1364 return 0;
1365 }
1366 }
1367 DeducedEltTy = EltTy;
1368 }
Bob Wilson21870412009-11-22 04:24:42 +00001369
David Greenedcd35c72011-07-29 19:07:07 +00001370 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4601652007-11-22 20:49:04 +00001371 }
1372 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1373 Lex.Lex(); // eat the '('
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001374 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001375 TokError("expected identifier in dag init");
1376 return 0;
1377 }
Bob Wilson21870412009-11-22 04:24:42 +00001378
David Greene05bce0b2011-07-29 22:43:06 +00001379 Init *Operator = ParseValue(CurRec);
Chris Lattner578bcf02010-10-06 04:31:40 +00001380 if (Operator == 0) return 0;
David Greenec7cafcd2009-04-22 20:18:10 +00001381
Nate Begeman7cee8172009-03-19 05:21:56 +00001382 // If the operator name is present, parse it.
1383 std::string OperatorName;
1384 if (Lex.getCode() == tgtok::colon) {
1385 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1386 TokError("expected variable name in dag operator");
1387 return 0;
1388 }
1389 OperatorName = Lex.getCurStrVal();
1390 Lex.Lex(); // eat the VarName.
1391 }
Bob Wilson21870412009-11-22 04:24:42 +00001392
David Greene05bce0b2011-07-29 22:43:06 +00001393 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4601652007-11-22 20:49:04 +00001394 if (Lex.getCode() != tgtok::r_paren) {
1395 DagArgs = ParseDagArgList(CurRec);
1396 if (DagArgs.empty()) return 0;
1397 }
Bob Wilson21870412009-11-22 04:24:42 +00001398
Chris Lattnerf4601652007-11-22 20:49:04 +00001399 if (Lex.getCode() != tgtok::r_paren) {
1400 TokError("expected ')' in dag init");
1401 return 0;
1402 }
1403 Lex.Lex(); // eat the ')'
Bob Wilson21870412009-11-22 04:24:42 +00001404
David Greenedcd35c72011-07-29 19:07:07 +00001405 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4601652007-11-22 20:49:04 +00001406 }
Bob Wilson21870412009-11-22 04:24:42 +00001407
David Greene1434f662011-01-07 17:05:37 +00001408 case tgtok::XHead:
1409 case tgtok::XTail:
1410 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +00001411 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001412 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +00001413 case tgtok::XSRA:
Chris Lattnerf4601652007-11-22 20:49:04 +00001414 case tgtok::XSRL:
1415 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +00001416 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001417 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene9bea7c82009-05-14 23:26:46 +00001418 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +00001419 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001420 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greened418c1b2009-05-14 20:54:48 +00001421 return ParseOperation(CurRec);
Chris Lattnerf4601652007-11-22 20:49:04 +00001422 }
1423 }
Bob Wilson21870412009-11-22 04:24:42 +00001424
Chris Lattnerf4601652007-11-22 20:49:04 +00001425 return R;
1426}
1427
1428/// ParseValue - Parse a tblgen value. This returns null on error.
1429///
1430/// Value ::= SimpleValue ValueSuffix*
1431/// ValueSuffix ::= '{' BitList '}'
1432/// ValueSuffix ::= '[' BitList ']'
1433/// ValueSuffix ::= '.' ID
1434///
David Greenef3744a02011-10-19 13:04:20 +00001435Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1436 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Chris Lattnerf4601652007-11-22 20:49:04 +00001437 if (Result == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001438
Chris Lattnerf4601652007-11-22 20:49:04 +00001439 // Parse the suffixes now if present.
1440 while (1) {
1441 switch (Lex.getCode()) {
1442 default: return Result;
1443 case tgtok::l_brace: {
David Greenecebb4ee2012-02-22 16:09:41 +00001444 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greene8592b2b2011-10-19 13:04:26 +00001445 // This is the beginning of the object body.
1446 return Result;
1447
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001448 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001449 Lex.Lex(); // eat the '{'
1450 std::vector<unsigned> Ranges = ParseRangeList();
1451 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001452
Chris Lattnerf4601652007-11-22 20:49:04 +00001453 // Reverse the bitlist.
1454 std::reverse(Ranges.begin(), Ranges.end());
1455 Result = Result->convertInitializerBitRange(Ranges);
1456 if (Result == 0) {
1457 Error(CurlyLoc, "Invalid bit range for value");
1458 return 0;
1459 }
Bob Wilson21870412009-11-22 04:24:42 +00001460
Chris Lattnerf4601652007-11-22 20:49:04 +00001461 // Eat the '}'.
1462 if (Lex.getCode() != tgtok::r_brace) {
1463 TokError("expected '}' at end of bit range list");
1464 return 0;
1465 }
1466 Lex.Lex();
1467 break;
1468 }
1469 case tgtok::l_square: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001470 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001471 Lex.Lex(); // eat the '['
1472 std::vector<unsigned> Ranges = ParseRangeList();
1473 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001474
Chris Lattnerf4601652007-11-22 20:49:04 +00001475 Result = Result->convertInitListSlice(Ranges);
1476 if (Result == 0) {
1477 Error(SquareLoc, "Invalid range for list slice");
1478 return 0;
1479 }
Bob Wilson21870412009-11-22 04:24:42 +00001480
Chris Lattnerf4601652007-11-22 20:49:04 +00001481 // Eat the ']'.
1482 if (Lex.getCode() != tgtok::r_square) {
1483 TokError("expected ']' at end of list slice");
1484 return 0;
1485 }
1486 Lex.Lex();
1487 break;
1488 }
1489 case tgtok::period:
1490 if (Lex.Lex() != tgtok::Id) { // eat the .
1491 TokError("expected field identifier after '.'");
1492 return 0;
1493 }
1494 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001495 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00001496 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +00001497 return 0;
1498 }
David Greenedcd35c72011-07-29 19:07:07 +00001499 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001500 Lex.Lex(); // eat field name
1501 break;
David Greened3d1cad2011-10-19 13:04:43 +00001502
1503 case tgtok::paste:
1504 SMLoc PasteLoc = Lex.getLoc();
1505
1506 // Create a !strconcat() operation, first casting each operand to
1507 // a string if necessary.
1508
1509 TypedInit *LHS = dynamic_cast<TypedInit *>(Result);
1510 if (!LHS) {
1511 Error(PasteLoc, "LHS of paste is not typed!");
1512 return 0;
1513 }
1514
1515 if (LHS->getType() != StringRecTy::get()) {
1516 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1517 }
1518
1519 TypedInit *RHS = 0;
1520
1521 Lex.Lex(); // Eat the '#'.
1522 switch (Lex.getCode()) {
1523 case tgtok::colon:
1524 case tgtok::semi:
1525 case tgtok::l_brace:
1526 // These are all of the tokens that can begin an object body.
1527 // Some of these can also begin values but we disallow those cases
1528 // because they are unlikely to be useful.
1529
1530 // Trailing paste, concat with an empty string.
1531 RHS = StringInit::get("");
1532 break;
1533
1534 default:
1535 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
1536 RHS = dynamic_cast<TypedInit *>(RHSResult);
1537 if (!RHS) {
1538 Error(PasteLoc, "RHS of paste is not typed!");
1539 return 0;
1540 }
1541
1542 if (RHS->getType() != StringRecTy::get()) {
1543 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1544 }
1545
1546 break;
1547 }
1548
1549 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1550 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1551 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001552 }
1553 }
1554}
1555
1556/// ParseDagArgList - Parse the argument list for a dag literal expression.
1557///
1558/// ParseDagArgList ::= Value (':' VARNAME)?
1559/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
David Greene05bce0b2011-07-29 22:43:06 +00001560std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4601652007-11-22 20:49:04 +00001561TGParser::ParseDagArgList(Record *CurRec) {
David Greene05bce0b2011-07-29 22:43:06 +00001562 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson21870412009-11-22 04:24:42 +00001563
Chris Lattnerf4601652007-11-22 20:49:04 +00001564 while (1) {
David Greene05bce0b2011-07-29 22:43:06 +00001565 Init *Val = ParseValue(CurRec);
1566 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
Bob Wilson21870412009-11-22 04:24:42 +00001567
Chris Lattnerf4601652007-11-22 20:49:04 +00001568 // If the variable name is present, add it.
1569 std::string VarName;
1570 if (Lex.getCode() == tgtok::colon) {
1571 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1572 TokError("expected variable name in dag literal");
David Greene05bce0b2011-07-29 22:43:06 +00001573 return std::vector<std::pair<llvm::Init*, std::string> >();
Chris Lattnerf4601652007-11-22 20:49:04 +00001574 }
1575 VarName = Lex.getCurStrVal();
1576 Lex.Lex(); // eat the VarName.
1577 }
Bob Wilson21870412009-11-22 04:24:42 +00001578
Chris Lattnerf4601652007-11-22 20:49:04 +00001579 Result.push_back(std::make_pair(Val, VarName));
Bob Wilson21870412009-11-22 04:24:42 +00001580
Chris Lattnerf4601652007-11-22 20:49:04 +00001581 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson21870412009-11-22 04:24:42 +00001582 Lex.Lex(); // eat the ','
Chris Lattnerf4601652007-11-22 20:49:04 +00001583 }
Bob Wilson21870412009-11-22 04:24:42 +00001584
Chris Lattnerf4601652007-11-22 20:49:04 +00001585 return Result;
1586}
1587
1588
1589/// ParseValueList - Parse a comma separated list of values, returning them as a
1590/// vector. Note that this always expects to be able to parse at least one
1591/// value. It returns an empty list if this is not possible.
1592///
1593/// ValueList ::= Value (',' Value)
1594///
David Greene05bce0b2011-07-29 22:43:06 +00001595std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopherd568b3f2011-07-11 23:06:52 +00001596 RecTy *EltTy) {
David Greene05bce0b2011-07-29 22:43:06 +00001597 std::vector<Init*> Result;
David Greenee1b46912009-06-08 20:23:18 +00001598 RecTy *ItemType = EltTy;
David Greene67acdf22009-06-29 19:59:52 +00001599 unsigned int ArgN = 0;
David Greenee1b46912009-06-08 20:23:18 +00001600 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001601 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Jim Grosbachb1320cb2012-01-20 20:02:39 +00001602 if (!TArgs.size()) {
1603 TokError("template argument provided to non-template class");
1604 return std::vector<Init*>();
1605 }
David Greenee1b46912009-06-08 20:23:18 +00001606 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greened9746fe2011-09-19 18:26:07 +00001607 if (!RV) {
1608 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1609 << ")\n";
1610 }
David Greenee1b46912009-06-08 20:23:18 +00001611 assert(RV && "Template argument record not found??");
1612 ItemType = RV->getType();
1613 ++ArgN;
1614 }
1615 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001616 if (Result.back() == 0) return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001617
Chris Lattnerf4601652007-11-22 20:49:04 +00001618 while (Lex.getCode() == tgtok::comma) {
1619 Lex.Lex(); // Eat the comma
Bob Wilson21870412009-11-22 04:24:42 +00001620
David Greenee1b46912009-06-08 20:23:18 +00001621 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001622 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greene67acdf22009-06-29 19:59:52 +00001623 if (ArgN >= TArgs.size()) {
1624 TokError("too many template arguments");
David Greene05bce0b2011-07-29 22:43:06 +00001625 return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001626 }
David Greenee1b46912009-06-08 20:23:18 +00001627 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1628 assert(RV && "Template argument record not found??");
1629 ItemType = RV->getType();
1630 ++ArgN;
1631 }
1632 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001633 if (Result.back() == 0) return std::vector<Init*>();
Chris Lattnerf4601652007-11-22 20:49:04 +00001634 }
Bob Wilson21870412009-11-22 04:24:42 +00001635
Chris Lattnerf4601652007-11-22 20:49:04 +00001636 return Result;
1637}
1638
1639
Chris Lattnerf4601652007-11-22 20:49:04 +00001640/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1641/// empty string on error. This can happen in a number of different context's,
1642/// including within a def or in the template args for a def (which which case
1643/// CurRec will be non-null) and within the template args for a multiclass (in
1644/// which case CurRec will be null, but CurMultiClass will be set). This can
1645/// also happen within a def that is within a multiclass, which will set both
1646/// CurRec and CurMultiClass.
1647///
1648/// Declaration ::= FIELD? Type ID ('=' Value)?
1649///
David Greenee22b3212011-10-19 13:02:42 +00001650Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4601652007-11-22 20:49:04 +00001651 bool ParsingTemplateArgs) {
1652 // Read the field prefix if present.
1653 bool HasField = Lex.getCode() == tgtok::Field;
1654 if (HasField) Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001655
Chris Lattnerf4601652007-11-22 20:49:04 +00001656 RecTy *Type = ParseType();
David Greenee22b3212011-10-19 13:02:42 +00001657 if (Type == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001658
Chris Lattnerf4601652007-11-22 20:49:04 +00001659 if (Lex.getCode() != tgtok::Id) {
1660 TokError("Expected identifier in declaration");
David Greenee22b3212011-10-19 13:02:42 +00001661 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001662 }
Bob Wilson21870412009-11-22 04:24:42 +00001663
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001664 SMLoc IdLoc = Lex.getLoc();
David Greenee22b3212011-10-19 13:02:42 +00001665 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001666 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001667
Chris Lattnerf4601652007-11-22 20:49:04 +00001668 if (ParsingTemplateArgs) {
1669 if (CurRec) {
David Greenee22b3212011-10-19 13:02:42 +00001670 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4601652007-11-22 20:49:04 +00001671 } else {
1672 assert(CurMultiClass);
1673 }
1674 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +00001675 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1676 "::");
Chris Lattnerf4601652007-11-22 20:49:04 +00001677 }
Bob Wilson21870412009-11-22 04:24:42 +00001678
Chris Lattnerf4601652007-11-22 20:49:04 +00001679 // Add the value.
1680 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
David Greenee22b3212011-10-19 13:02:42 +00001681 return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001682
Chris Lattnerf4601652007-11-22 20:49:04 +00001683 // If a value is present, parse it.
1684 if (Lex.getCode() == tgtok::equal) {
1685 Lex.Lex();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001686 SMLoc ValLoc = Lex.getLoc();
David Greene05bce0b2011-07-29 22:43:06 +00001687 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001688 if (Val == 0 ||
1689 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
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 Lattnerf4601652007-11-22 20:49:04 +00001693 return DeclName;
1694}
1695
David Greenecebb4ee2012-02-22 16:09:41 +00001696/// ParseForeachDeclaration - Read a foreach declaration, returning
1697/// the name of the declared object or a NULL Init on error. Return
1698/// the name of the parsed initializer list through ForeachListName.
1699///
1700/// ForeachDeclaration ::= ID '=' Value
1701///
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00001702VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenecebb4ee2012-02-22 16:09:41 +00001703 if (Lex.getCode() != tgtok::Id) {
1704 TokError("Expected identifier in foreach declaration");
1705 return 0;
1706 }
1707
1708 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1709 Lex.Lex();
1710
1711 // If a value is present, parse it.
1712 if (Lex.getCode() != tgtok::equal) {
1713 TokError("Expected '=' in foreach declaration");
1714 return 0;
1715 }
1716 Lex.Lex(); // Eat the '='
1717
1718 // Expect a list initializer.
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00001719 Init *List = ParseSimpleValue(0, 0, ParseForeachMode);
David Greenecebb4ee2012-02-22 16:09:41 +00001720
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00001721 ForeachListValue = dynamic_cast<ListInit*>(List);
1722 if (ForeachListValue == 0) {
1723 TokError("Expected a Value list");
David Greenecebb4ee2012-02-22 16:09:41 +00001724 return 0;
1725 }
1726
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00001727 RecTy *ValueType = ForeachListValue->getType();
David Greenecebb4ee2012-02-22 16:09:41 +00001728 ListRecTy *ListType = dynamic_cast<ListRecTy *>(ValueType);
1729 if (ListType == 0) {
1730 TokError("Value list is not of list type");
1731 return 0;
1732 }
1733
1734 RecTy *IterType = ListType->getElementType();
1735 VarInit *IterVar = VarInit::get(DeclName, IterType);
1736
1737 return IterVar;
1738}
1739
Chris Lattnerf4601652007-11-22 20:49:04 +00001740/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1741/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1742/// template args for a def, which may or may not be in a multiclass. If null,
1743/// these are the template args for a multiclass.
1744///
1745/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson21870412009-11-22 04:24:42 +00001746///
Chris Lattnerf4601652007-11-22 20:49:04 +00001747bool TGParser::ParseTemplateArgList(Record *CurRec) {
1748 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1749 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +00001750
Chris Lattnerf4601652007-11-22 20:49:04 +00001751 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +00001752
Chris Lattnerf4601652007-11-22 20:49:04 +00001753 // Read the first declaration.
David Greenee22b3212011-10-19 13:02:42 +00001754 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1755 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001756 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001757
Chris Lattnerf4601652007-11-22 20:49:04 +00001758 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson21870412009-11-22 04:24:42 +00001759
Chris Lattnerf4601652007-11-22 20:49:04 +00001760 while (Lex.getCode() == tgtok::comma) {
1761 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001762
Chris Lattnerf4601652007-11-22 20:49:04 +00001763 // Read the following declarations.
1764 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
David Greenee22b3212011-10-19 13:02:42 +00001765 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001766 return true;
1767 TheRecToAddTo->addTemplateArg(TemplArg);
1768 }
Bob Wilson21870412009-11-22 04:24:42 +00001769
Chris Lattnerf4601652007-11-22 20:49:04 +00001770 if (Lex.getCode() != tgtok::greater)
1771 return TokError("expected '>' at end of template argument list");
1772 Lex.Lex(); // eat the '>'.
1773 return false;
1774}
1775
1776
1777/// ParseBodyItem - Parse a single item at within the body of a def or class.
1778///
1779/// BodyItem ::= Declaration ';'
1780/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1781bool TGParser::ParseBodyItem(Record *CurRec) {
1782 if (Lex.getCode() != tgtok::Let) {
David Greenee22b3212011-10-19 13:02:42 +00001783 if (ParseDeclaration(CurRec, false) == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001784 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001785
Chris Lattnerf4601652007-11-22 20:49:04 +00001786 if (Lex.getCode() != tgtok::semi)
1787 return TokError("expected ';' after declaration");
1788 Lex.Lex();
1789 return false;
1790 }
1791
1792 // LET ID OptionalRangeList '=' Value ';'
1793 if (Lex.Lex() != tgtok::Id)
1794 return TokError("expected field identifier after let");
Bob Wilson21870412009-11-22 04:24:42 +00001795
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001796 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001797 std::string FieldName = Lex.getCurStrVal();
1798 Lex.Lex(); // eat the field name.
Bob Wilson21870412009-11-22 04:24:42 +00001799
Chris Lattnerf4601652007-11-22 20:49:04 +00001800 std::vector<unsigned> BitList;
Bob Wilson21870412009-11-22 04:24:42 +00001801 if (ParseOptionalBitList(BitList))
Chris Lattnerf4601652007-11-22 20:49:04 +00001802 return true;
1803 std::reverse(BitList.begin(), BitList.end());
Bob Wilson21870412009-11-22 04:24:42 +00001804
Chris Lattnerf4601652007-11-22 20:49:04 +00001805 if (Lex.getCode() != tgtok::equal)
1806 return TokError("expected '=' in let expression");
1807 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001808
David Greenee1b46912009-06-08 20:23:18 +00001809 RecordVal *Field = CurRec->getValue(FieldName);
1810 if (Field == 0)
1811 return TokError("Value '" + FieldName + "' unknown!");
1812
1813 RecTy *Type = Field->getType();
Bob Wilson21870412009-11-22 04:24:42 +00001814
David Greene05bce0b2011-07-29 22:43:06 +00001815 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001816 if (Val == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001817
Chris Lattnerf4601652007-11-22 20:49:04 +00001818 if (Lex.getCode() != tgtok::semi)
1819 return TokError("expected ';' after let expression");
1820 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001821
Chris Lattnerf4601652007-11-22 20:49:04 +00001822 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1823}
1824
1825/// ParseBody - Read the body of a class or def. Return true on error, false on
1826/// success.
1827///
1828/// Body ::= ';'
1829/// Body ::= '{' BodyList '}'
1830/// BodyList BodyItem*
1831///
1832bool TGParser::ParseBody(Record *CurRec) {
1833 // If this is a null definition, just eat the semi and return.
1834 if (Lex.getCode() == tgtok::semi) {
1835 Lex.Lex();
1836 return false;
1837 }
Bob Wilson21870412009-11-22 04:24:42 +00001838
Chris Lattnerf4601652007-11-22 20:49:04 +00001839 if (Lex.getCode() != tgtok::l_brace)
1840 return TokError("Expected ';' or '{' to start body");
1841 // Eat the '{'.
1842 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001843
Chris Lattnerf4601652007-11-22 20:49:04 +00001844 while (Lex.getCode() != tgtok::r_brace)
1845 if (ParseBodyItem(CurRec))
1846 return true;
1847
1848 // Eat the '}'.
1849 Lex.Lex();
1850 return false;
1851}
1852
1853/// ParseObjectBody - Parse the body of a def or class. This consists of an
1854/// optional ClassList followed by a Body. CurRec is the current def or class
1855/// that is being parsed.
1856///
1857/// ObjectBody ::= BaseClassList Body
1858/// BaseClassList ::= /*empty*/
1859/// BaseClassList ::= ':' BaseClassListNE
1860/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1861///
1862bool TGParser::ParseObjectBody(Record *CurRec) {
1863 // If there is a baseclass list, read it.
1864 if (Lex.getCode() == tgtok::colon) {
1865 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001866
Chris Lattnerf4601652007-11-22 20:49:04 +00001867 // Read all of the subclasses.
1868 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1869 while (1) {
1870 // Check for error.
1871 if (SubClass.Rec == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001872
Chris Lattnerf4601652007-11-22 20:49:04 +00001873 // Add it.
1874 if (AddSubClass(CurRec, SubClass))
1875 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001876
Chris Lattnerf4601652007-11-22 20:49:04 +00001877 if (Lex.getCode() != tgtok::comma) break;
1878 Lex.Lex(); // eat ','.
1879 SubClass = ParseSubClassReference(CurRec, false);
1880 }
1881 }
1882
1883 // Process any variables on the let stack.
1884 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1885 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1886 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1887 LetStack[i][j].Bits, LetStack[i][j].Value))
1888 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001889
Chris Lattnerf4601652007-11-22 20:49:04 +00001890 return ParseBody(CurRec);
1891}
1892
Chris Lattnerf4601652007-11-22 20:49:04 +00001893/// ParseDef - Parse and return a top level or multiclass def, return the record
1894/// corresponding to it. This returns null on error.
1895///
1896/// DefInst ::= DEF ObjectName ObjectBody
1897///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001898bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001899 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001900 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson21870412009-11-22 04:24:42 +00001901 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4601652007-11-22 20:49:04 +00001902
1903 // Parse ObjectName and make a record for it.
David Greenea9e07dd2011-10-19 13:04:29 +00001904 Record *CurRec = new Record(ParseObjectName(CurMultiClass), DefLoc, Records);
Bob Wilson21870412009-11-22 04:24:42 +00001905
Jakob Stoklund Olesen72cba6c2012-05-24 22:17:36 +00001906 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001907 // Top-level def definition.
Bob Wilson21870412009-11-22 04:24:42 +00001908
Chris Lattnerf4601652007-11-22 20:49:04 +00001909 // Ensure redefinition doesn't happen.
David Greene1d501392012-01-28 00:03:24 +00001910 if (Records.getDef(CurRec->getNameInitAsString())) {
David Greene2c49fbb2011-10-19 13:03:45 +00001911 Error(DefLoc, "def '" + CurRec->getNameInitAsString()
1912 + "' already defined");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001913 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001914 }
1915 Records.addDef(CurRec);
Jakob Stoklund Olesen72cba6c2012-05-24 22:17:36 +00001916 } else if (CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001917 // Otherwise, a def inside a multiclass, add it to the multiclass.
1918 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene91919cd2011-10-19 13:03:51 +00001919 if (CurMultiClass->DefPrototypes[i]->getNameInit()
1920 == CurRec->getNameInit()) {
1921 Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
Chris Lattnerf4601652007-11-22 20:49:04 +00001922 "' already defined in this multiclass!");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001923 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001924 }
1925 CurMultiClass->DefPrototypes.push_back(CurRec);
1926 }
Bob Wilson21870412009-11-22 04:24:42 +00001927
Chris Lattnerf4601652007-11-22 20:49:04 +00001928 if (ParseObjectBody(CurRec))
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001929 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001930
Chris Lattnerf4601652007-11-22 20:49:04 +00001931 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
David Greene0d886402011-08-10 18:27:46 +00001932 // See Record::setName(). This resolve step will see any new name
1933 // for the def that might have been created when resolving
1934 // inheritance, values and arguments above.
Chris Lattnerf4601652007-11-22 20:49:04 +00001935 CurRec->resolveReferences();
Bob Wilson21870412009-11-22 04:24:42 +00001936
Chris Lattnerf4601652007-11-22 20:49:04 +00001937 // If ObjectBody has template arguments, it's an error.
1938 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001939
1940 if (CurMultiClass) {
1941 // Copy the template arguments for the multiclass into the def.
David Greenee22b3212011-10-19 13:02:42 +00001942 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001943 CurMultiClass->Rec.getTemplateArgs();
1944
1945 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1946 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1947 assert(RV && "Template arg doesn't exist?");
1948 CurRec->addValue(*RV);
1949 }
1950 }
1951
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00001952 if (ProcessForeachDefs(CurRec, DefLoc)) {
David Greenecebb4ee2012-02-22 16:09:41 +00001953 Error(DefLoc,
1954 "Could not process loops for def" + CurRec->getNameInitAsString());
1955 return true;
1956 }
1957
1958 return false;
1959}
1960
1961/// ParseForeach - Parse a for statement. Return the record corresponding
1962/// to it. This returns true on error.
1963///
1964/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
1965/// Foreach ::= FOREACH Declaration IN Object
1966///
1967bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
1968 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
1969 Lex.Lex(); // Eat the 'for' token.
1970
1971 // Make a temporary object to record items associated with the for
1972 // loop.
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00001973 ListInit *ListValue = 0;
1974 VarInit *IterName = ParseForeachDeclaration(ListValue);
David Greenecebb4ee2012-02-22 16:09:41 +00001975 if (IterName == 0)
1976 return TokError("expected declaration in for");
1977
1978 if (Lex.getCode() != tgtok::In)
1979 return TokError("Unknown tok");
1980 Lex.Lex(); // Eat the in
1981
1982 // Create a loop object and remember it.
1983 Loops.push_back(ForeachLoop(IterName, ListValue));
1984
1985 if (Lex.getCode() != tgtok::l_brace) {
1986 // FOREACH Declaration IN Object
1987 if (ParseObject(CurMultiClass))
1988 return true;
1989 }
1990 else {
1991 SMLoc BraceLoc = Lex.getLoc();
1992 // Otherwise, this is a group foreach.
1993 Lex.Lex(); // eat the '{'.
1994
1995 // Parse the object list.
1996 if (ParseObjectList(CurMultiClass))
1997 return true;
1998
1999 if (Lex.getCode() != tgtok::r_brace) {
2000 TokError("expected '}' at end of foreach command");
2001 return Error(BraceLoc, "to match this '{'");
2002 }
2003 Lex.Lex(); // Eat the }
2004 }
2005
2006 // We've processed everything in this loop.
2007 Loops.pop_back();
2008
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002009 return false;
Chris Lattnerf4601652007-11-22 20:49:04 +00002010}
2011
Chris Lattnerf4601652007-11-22 20:49:04 +00002012/// ParseClass - Parse a tblgen class definition.
2013///
2014/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2015///
2016bool TGParser::ParseClass() {
2017 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2018 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002019
Chris Lattnerf4601652007-11-22 20:49:04 +00002020 if (Lex.getCode() != tgtok::Id)
2021 return TokError("expected class name after 'class' keyword");
Bob Wilson21870412009-11-22 04:24:42 +00002022
Chris Lattnerf4601652007-11-22 20:49:04 +00002023 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2024 if (CurRec) {
2025 // If the body was previously defined, this is an error.
David Greenee3385652011-10-19 13:04:13 +00002026 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4601652007-11-22 20:49:04 +00002027 !CurRec->getSuperClasses().empty() ||
2028 !CurRec->getTemplateArgs().empty())
David Greene69a23942011-10-19 13:03:58 +00002029 return TokError("Class '" + CurRec->getNameInitAsString()
2030 + "' already defined");
Chris Lattnerf4601652007-11-22 20:49:04 +00002031 } else {
2032 // If this is the first reference to this class, create and add it.
Chris Lattner9c6b60e2010-12-15 04:48:22 +00002033 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00002034 Records.addClass(CurRec);
2035 }
2036 Lex.Lex(); // eat the name.
Bob Wilson21870412009-11-22 04:24:42 +00002037
Chris Lattnerf4601652007-11-22 20:49:04 +00002038 // If there are template args, parse them.
2039 if (Lex.getCode() == tgtok::less)
2040 if (ParseTemplateArgList(CurRec))
2041 return true;
2042
2043 // Finally, parse the object body.
2044 return ParseObjectBody(CurRec);
2045}
2046
2047/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2048/// of LetRecords.
2049///
2050/// LetList ::= LetItem (',' LetItem)*
2051/// LetItem ::= ID OptionalRangeList '=' Value
2052///
2053std::vector<LetRecord> TGParser::ParseLetList() {
2054 std::vector<LetRecord> Result;
Bob Wilson21870412009-11-22 04:24:42 +00002055
Chris Lattnerf4601652007-11-22 20:49:04 +00002056 while (1) {
2057 if (Lex.getCode() != tgtok::Id) {
2058 TokError("expected identifier in let definition");
2059 return std::vector<LetRecord>();
2060 }
2061 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002062 SMLoc NameLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +00002063 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4601652007-11-22 20:49:04 +00002064
2065 // Check for an optional RangeList.
2066 std::vector<unsigned> Bits;
Bob Wilson21870412009-11-22 04:24:42 +00002067 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4601652007-11-22 20:49:04 +00002068 return std::vector<LetRecord>();
2069 std::reverse(Bits.begin(), Bits.end());
Bob Wilson21870412009-11-22 04:24:42 +00002070
Chris Lattnerf4601652007-11-22 20:49:04 +00002071 if (Lex.getCode() != tgtok::equal) {
2072 TokError("expected '=' in let expression");
2073 return std::vector<LetRecord>();
2074 }
2075 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00002076
David Greene05bce0b2011-07-29 22:43:06 +00002077 Init *Val = ParseValue(0);
Chris Lattnerf4601652007-11-22 20:49:04 +00002078 if (Val == 0) return std::vector<LetRecord>();
Bob Wilson21870412009-11-22 04:24:42 +00002079
Chris Lattnerf4601652007-11-22 20:49:04 +00002080 // Now that we have everything, add the record.
2081 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson21870412009-11-22 04:24:42 +00002082
Chris Lattnerf4601652007-11-22 20:49:04 +00002083 if (Lex.getCode() != tgtok::comma)
2084 return Result;
Bob Wilson21870412009-11-22 04:24:42 +00002085 Lex.Lex(); // eat the comma.
Chris Lattnerf4601652007-11-22 20:49:04 +00002086 }
2087}
2088
2089/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002090/// different related productions. This works inside multiclasses too.
Chris Lattnerf4601652007-11-22 20:49:04 +00002091///
2092/// Object ::= LET LetList IN '{' ObjectList '}'
2093/// Object ::= LET LetList IN Object
2094///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002095bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002096 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2097 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002098
Chris Lattnerf4601652007-11-22 20:49:04 +00002099 // Add this entry to the let stack.
2100 std::vector<LetRecord> LetInfo = ParseLetList();
2101 if (LetInfo.empty()) return true;
2102 LetStack.push_back(LetInfo);
2103
2104 if (Lex.getCode() != tgtok::In)
2105 return TokError("expected 'in' at end of top-level 'let'");
2106 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002107
Chris Lattnerf4601652007-11-22 20:49:04 +00002108 // If this is a scalar let, just handle it now
2109 if (Lex.getCode() != tgtok::l_brace) {
2110 // LET LetList IN Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002111 if (ParseObject(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00002112 return true;
2113 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002114 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002115 // Otherwise, this is a group let.
2116 Lex.Lex(); // eat the '{'.
Bob Wilson21870412009-11-22 04:24:42 +00002117
Chris Lattnerf4601652007-11-22 20:49:04 +00002118 // Parse the object list.
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002119 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00002120 return true;
Bob Wilson21870412009-11-22 04:24:42 +00002121
Chris Lattnerf4601652007-11-22 20:49:04 +00002122 if (Lex.getCode() != tgtok::r_brace) {
2123 TokError("expected '}' at end of top level let command");
2124 return Error(BraceLoc, "to match this '{'");
2125 }
2126 Lex.Lex();
2127 }
Bob Wilson21870412009-11-22 04:24:42 +00002128
Chris Lattnerf4601652007-11-22 20:49:04 +00002129 // Outside this let scope, this let block is not active.
2130 LetStack.pop_back();
2131 return false;
2132}
2133
Chris Lattnerf4601652007-11-22 20:49:04 +00002134/// ParseMultiClass - Parse a multiclass definition.
2135///
Bob Wilson32558652009-04-28 19:41:44 +00002136/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
2137/// ':' BaseMultiClassList '{' MultiClassDef+ '}'
Chris Lattnerf4601652007-11-22 20:49:04 +00002138///
2139bool TGParser::ParseMultiClass() {
2140 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2141 Lex.Lex(); // Eat the multiclass token.
2142
2143 if (Lex.getCode() != tgtok::Id)
2144 return TokError("expected identifier after multiclass for name");
2145 std::string Name = Lex.getCurStrVal();
Bob Wilson21870412009-11-22 04:24:42 +00002146
Chris Lattnerf4601652007-11-22 20:49:04 +00002147 if (MultiClasses.count(Name))
2148 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson21870412009-11-22 04:24:42 +00002149
Chris Lattner67db8832010-12-13 00:23:57 +00002150 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
2151 Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00002152 Lex.Lex(); // Eat the identifier.
Bob Wilson21870412009-11-22 04:24:42 +00002153
Chris Lattnerf4601652007-11-22 20:49:04 +00002154 // If there are template args, parse them.
2155 if (Lex.getCode() == tgtok::less)
2156 if (ParseTemplateArgList(0))
2157 return true;
2158
David Greened34a73b2009-04-24 16:55:41 +00002159 bool inherits = false;
2160
David Greenede444af2009-04-22 16:42:54 +00002161 // If there are submulticlasses, parse them.
2162 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00002163 inherits = true;
2164
David Greenede444af2009-04-22 16:42:54 +00002165 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00002166
David Greenede444af2009-04-22 16:42:54 +00002167 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00002168 SubMultiClassReference SubMultiClass =
2169 ParseSubMultiClassReference(CurMultiClass);
David Greenede444af2009-04-22 16:42:54 +00002170 while (1) {
2171 // Check for error.
2172 if (SubMultiClass.MC == 0) return true;
Bob Wilson32558652009-04-28 19:41:44 +00002173
David Greenede444af2009-04-22 16:42:54 +00002174 // Add it.
2175 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2176 return true;
Bob Wilson32558652009-04-28 19:41:44 +00002177
David Greenede444af2009-04-22 16:42:54 +00002178 if (Lex.getCode() != tgtok::comma) break;
2179 Lex.Lex(); // eat ','.
2180 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2181 }
2182 }
2183
David Greened34a73b2009-04-24 16:55:41 +00002184 if (Lex.getCode() != tgtok::l_brace) {
2185 if (!inherits)
2186 return TokError("expected '{' in multiclass definition");
Bob Wilson21870412009-11-22 04:24:42 +00002187 else if (Lex.getCode() != tgtok::semi)
2188 return TokError("expected ';' in multiclass definition");
David Greened34a73b2009-04-24 16:55:41 +00002189 else
Bob Wilson21870412009-11-22 04:24:42 +00002190 Lex.Lex(); // eat the ';'.
2191 } else {
David Greened34a73b2009-04-24 16:55:41 +00002192 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2193 return TokError("multiclass must contain at least one def");
Bob Wilson21870412009-11-22 04:24:42 +00002194
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002195 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002196 switch (Lex.getCode()) {
2197 default:
David Greenea1b1b792011-10-07 18:25:05 +00002198 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002199 case tgtok::Let:
2200 case tgtok::Def:
2201 case tgtok::Defm:
David Greenecebb4ee2012-02-22 16:09:41 +00002202 case tgtok::Foreach:
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002203 if (ParseObject(CurMultiClass))
2204 return true;
2205 break;
2206 }
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002207 }
David Greened34a73b2009-04-24 16:55:41 +00002208 Lex.Lex(); // eat the '}'.
2209 }
Bob Wilson21870412009-11-22 04:24:42 +00002210
Chris Lattnerf4601652007-11-22 20:49:04 +00002211 CurMultiClass = 0;
2212 return false;
2213}
2214
David Greenee499a2d2011-10-05 22:42:07 +00002215Record *TGParser::
2216InstantiateMulticlassDef(MultiClass &MC,
2217 Record *DefProto,
David Greene7be867e2011-10-19 13:04:31 +00002218 Init *DefmPrefix,
David Greenee499a2d2011-10-05 22:42:07 +00002219 SMLoc DefmPrefixLoc) {
David Greene7be867e2011-10-19 13:04:31 +00002220 // We need to preserve DefProto so it can be reused for later
2221 // instantiations, so create a new Record to inherit from it.
2222
David Greenee499a2d2011-10-05 22:42:07 +00002223 // Add in the defm name. If the defm prefix is empty, give each
2224 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2225 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2226 // as a prefix.
David Greenee499a2d2011-10-05 22:42:07 +00002227
David Greene7be867e2011-10-19 13:04:31 +00002228 if (DefmPrefix == 0)
2229 DefmPrefix = StringInit::get(GetNewAnonymousName());
2230
2231 Init *DefName = DefProto->getNameInit();
2232
David Greened3d1cad2011-10-19 13:04:43 +00002233 StringInit *DefNameString = dynamic_cast<StringInit *>(DefName);
David Greene7be867e2011-10-19 13:04:31 +00002234
David Greened3d1cad2011-10-19 13:04:43 +00002235 if (DefNameString != 0) {
2236 // We have a fully expanded string so there are no operators to
2237 // resolve. We should concatenate the given prefix and name.
David Greene7be867e2011-10-19 13:04:31 +00002238 DefName =
2239 BinOpInit::get(BinOpInit::STRCONCAT,
2240 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2241 StringRecTy::get())->Fold(DefProto, &MC),
2242 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2243 }
David Greene7be867e2011-10-19 13:04:31 +00002244
David Greenee499a2d2011-10-05 22:42:07 +00002245 Record *CurRec = new Record(DefName, DefmPrefixLoc, Records);
2246
2247 SubClassReference Ref;
2248 Ref.RefLoc = DefmPrefixLoc;
2249 Ref.Rec = DefProto;
2250 AddSubClass(CurRec, Ref);
2251
David Greenee5b252f2011-10-19 13:04:35 +00002252 if (DefNameString == 0) {
2253 // We must resolve references to NAME.
2254 if (SetValue(CurRec, Ref.RefLoc, "NAME", std::vector<unsigned>(),
2255 DefmPrefix)) {
2256 Error(DefmPrefixLoc, "Could not resolve "
2257 + CurRec->getNameInitAsString() + ":NAME to '"
2258 + DefmPrefix->getAsUnquotedString() + "'");
2259 return 0;
2260 }
2261
2262 RecordVal *DefNameRV = CurRec->getValue("NAME");
2263 CurRec->resolveReferencesTo(DefNameRV);
2264 }
2265
2266 if (!CurMultiClass) {
2267 // We do this after resolving NAME because before resolution, many
2268 // multiclass defs will have the same name expression. If we are
2269 // currently in a multiclass, it means this defm appears inside a
2270 // multiclass and its name won't be fully resolvable until we see
2271 // the top-level defm. Therefore, we don't add this to the
2272 // RecordKeeper at this point. If we did we could get duplicate
2273 // defs as more than one probably refers to NAME or some other
2274 // common internal placeholder.
2275
2276 // Ensure redefinition doesn't happen.
2277 if (Records.getDef(CurRec->getNameInitAsString())) {
2278 Error(DefmPrefixLoc, "def '" + CurRec->getNameInitAsString() +
2279 "' already defined, instantiating defm with subdef '" +
2280 DefProto->getNameInitAsString() + "'");
2281 return 0;
2282 }
2283
2284 Records.addDef(CurRec);
2285 }
2286
David Greenee499a2d2011-10-05 22:42:07 +00002287 return CurRec;
2288}
2289
2290bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2291 Record *CurRec,
2292 SMLoc DefmPrefixLoc,
2293 SMLoc SubClassLoc,
David Greenee22b3212011-10-19 13:02:42 +00002294 const std::vector<Init *> &TArgs,
David Greenee499a2d2011-10-05 22:42:07 +00002295 std::vector<Init *> &TemplateVals,
2296 bool DeleteArgs) {
2297 // Loop over all of the template arguments, setting them to the specified
2298 // value or leaving them as the default if necessary.
2299 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2300 // Check if a value is specified for this temp-arg.
2301 if (i < TemplateVals.size()) {
2302 // Set it now.
2303 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2304 TemplateVals[i]))
2305 return true;
2306
2307 // Resolve it next.
2308 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2309
2310 if (DeleteArgs)
2311 // Now remove it.
2312 CurRec->removeValue(TArgs[i]);
2313
2314 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2315 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenee22b3212011-10-19 13:02:42 +00002316 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2317 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2318 + "'");
David Greenee499a2d2011-10-05 22:42:07 +00002319 }
2320 }
2321 return false;
2322}
2323
2324bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2325 Record *CurRec,
2326 Record *DefProto,
2327 SMLoc DefmPrefixLoc) {
2328 // If the mdef is inside a 'let' expression, add to each def.
2329 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2330 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2331 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2332 LetStack[i][j].Bits, LetStack[i][j].Value))
2333 return Error(DefmPrefixLoc, "when instantiating this defm");
2334
David Greenee499a2d2011-10-05 22:42:07 +00002335 // Don't create a top level definition for defm inside multiclasses,
2336 // instead, only update the prototypes and bind the template args
2337 // with the new created definition.
2338 if (CurMultiClass) {
2339 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2340 i != e; ++i)
David Greene22dde7e2011-10-19 13:04:02 +00002341 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2342 == CurRec->getNameInit())
2343 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
David Greenee499a2d2011-10-05 22:42:07 +00002344 "' already defined in this multiclass!");
2345 CurMultiClass->DefPrototypes.push_back(CurRec);
2346
2347 // Copy the template arguments for the multiclass into the new def.
David Greenee22b3212011-10-19 13:02:42 +00002348 const std::vector<Init *> &TA =
David Greenee499a2d2011-10-05 22:42:07 +00002349 CurMultiClass->Rec.getTemplateArgs();
2350
2351 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2352 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2353 assert(RV && "Template arg doesn't exist?");
2354 CurRec->addValue(*RV);
2355 }
David Greenee499a2d2011-10-05 22:42:07 +00002356 }
2357
2358 return false;
2359}
2360
Chris Lattnerf4601652007-11-22 20:49:04 +00002361/// ParseDefm - Parse the instantiation of a multiclass.
2362///
2363/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2364///
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002365bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002366 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Bob Wilson21870412009-11-22 04:24:42 +00002367
David Greenea9e07dd2011-10-19 13:04:29 +00002368 Init *DefmPrefix = 0;
David Greenea9e07dd2011-10-19 13:04:29 +00002369
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002370 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greenea9e07dd2011-10-19 13:04:29 +00002371 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002372 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +00002373
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002374 SMLoc DefmPrefixLoc = Lex.getLoc();
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002375 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4601652007-11-22 20:49:04 +00002376 return TokError("expected ':' after defm identifier");
Bob Wilson21870412009-11-22 04:24:42 +00002377
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002378 // Keep track of the new generated record definitions.
2379 std::vector<Record*> NewRecDefs;
2380
2381 // This record also inherits from a regular class (non-multiclass)?
2382 bool InheritFromClass = false;
2383
Chris Lattnerf4601652007-11-22 20:49:04 +00002384 // eat the colon.
2385 Lex.Lex();
2386
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002387 SMLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002388 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene56546132009-04-22 22:17:51 +00002389
2390 while (1) {
2391 if (Ref.Rec == 0) return true;
2392
2393 // To instantiate a multiclass, we need to first get the multiclass, then
2394 // instantiate each def contained in the multiclass with the SubClassRef
2395 // template parameters.
2396 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2397 assert(MC && "Didn't lookup multiclass correctly?");
David Greene05bce0b2011-07-29 22:43:06 +00002398 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greene56546132009-04-22 22:17:51 +00002399
2400 // Verify that the correct number of template arguments were specified.
David Greenee22b3212011-10-19 13:02:42 +00002401 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greene56546132009-04-22 22:17:51 +00002402 if (TArgs.size() < TemplateVals.size())
2403 return Error(SubClassLoc,
2404 "more template args specified than multiclass expects");
2405
2406 // Loop over all the def's in the multiclass, instantiating each one.
2407 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2408 Record *DefProto = MC->DefPrototypes[i];
2409
David Greene7be867e2011-10-19 13:04:31 +00002410 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix, DefmPrefixLoc);
Jim Grosbach94f2dc92011-12-02 18:33:03 +00002411 if (!CurRec)
2412 return true;
David Greene065f2592009-05-05 16:28:25 +00002413
David Greenee499a2d2011-10-05 22:42:07 +00002414 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmPrefixLoc, SubClassLoc,
2415 TArgs, TemplateVals, true/*Delete args*/))
2416 return Error(SubClassLoc, "could not instantiate def");
David Greene56546132009-04-22 22:17:51 +00002417
David Greenee499a2d2011-10-05 22:42:07 +00002418 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmPrefixLoc))
2419 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002420
2421 NewRecDefs.push_back(CurRec);
David Greene56546132009-04-22 22:17:51 +00002422 }
2423
David Greenee499a2d2011-10-05 22:42:07 +00002424
David Greene56546132009-04-22 22:17:51 +00002425 if (Lex.getCode() != tgtok::comma) break;
2426 Lex.Lex(); // eat ','.
2427
2428 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002429
2430 // A defm can inherit from regular classes (non-multiclass) as
2431 // long as they come in the end of the inheritance list.
2432 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0);
2433
2434 if (InheritFromClass)
2435 break;
2436
David Greene56546132009-04-22 22:17:51 +00002437 Ref = ParseSubClassReference(0, true);
2438 }
2439
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002440 if (InheritFromClass) {
2441 // Process all the classes to inherit as if they were part of a
2442 // regular 'def' and inherit all record values.
2443 SubClassReference SubClass = ParseSubClassReference(0, false);
2444 while (1) {
2445 // Check for error.
2446 if (SubClass.Rec == 0) return true;
2447
2448 // Get the expanded definition prototypes and teach them about
2449 // the record values the current class to inherit has
2450 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2451 Record *CurRec = NewRecDefs[i];
2452
2453 // Add it.
2454 if (AddSubClass(CurRec, SubClass))
2455 return true;
2456
2457 // Process any variables on the let stack.
2458 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2459 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2460 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2461 LetStack[i][j].Bits, LetStack[i][j].Value))
2462 return true;
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002463 }
2464
2465 if (Lex.getCode() != tgtok::comma) break;
2466 Lex.Lex(); // eat ','.
2467 SubClass = ParseSubClassReference(0, false);
2468 }
2469 }
2470
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002471 if (!CurMultiClass)
2472 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene0d886402011-08-10 18:27:46 +00002473 // See Record::setName(). This resolve step will see any new
2474 // name for the def that might have been created when resolving
2475 // inheritance, values and arguments above.
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002476 NewRecDefs[i]->resolveReferences();
2477
Chris Lattnerf4601652007-11-22 20:49:04 +00002478 if (Lex.getCode() != tgtok::semi)
2479 return TokError("expected ';' at end of defm");
2480 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002481
Chris Lattnerf4601652007-11-22 20:49:04 +00002482 return false;
2483}
2484
2485/// ParseObject
2486/// Object ::= ClassInst
2487/// Object ::= DefInst
2488/// Object ::= MultiClassInst
2489/// Object ::= DefMInst
2490/// Object ::= LETCommand '{' ObjectList '}'
2491/// Object ::= LETCommand Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002492bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002493 switch (Lex.getCode()) {
Chris Lattnerd6d9dd92010-10-31 19:27:15 +00002494 default:
2495 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002496 case tgtok::Let: return ParseTopLevelLet(MC);
2497 case tgtok::Def: return ParseDef(MC);
David Greenecebb4ee2012-02-22 16:09:41 +00002498 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002499 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4601652007-11-22 20:49:04 +00002500 case tgtok::Class: return ParseClass();
2501 case tgtok::MultiClass: return ParseMultiClass();
2502 }
2503}
2504
2505/// ParseObjectList
2506/// ObjectList :== Object*
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002507bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002508 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002509 if (ParseObject(MC))
Chris Lattnerf4601652007-11-22 20:49:04 +00002510 return true;
2511 }
2512 return false;
2513}
2514
Chris Lattnerf4601652007-11-22 20:49:04 +00002515bool TGParser::ParseFile() {
2516 Lex.Lex(); // Prime the lexer.
2517 if (ParseObjectList()) return true;
Bob Wilson21870412009-11-22 04:24:42 +00002518
Chris Lattnerf4601652007-11-22 20:49:04 +00002519 // If we have unread input at the end of the file, report it.
2520 if (Lex.getCode() == tgtok::Eof)
2521 return false;
Bob Wilson21870412009-11-22 04:24:42 +00002522
Chris Lattnerf4601652007-11-22 20:49:04 +00002523 return TokError("Unexpected input at top level");
2524}
2525