blob: 4152c165c60e46da4d7bd5d0f7469d6497c76069 [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
Chris Lattnerf4601652007-11-22 20:49:04 +000067 if (RecordVal *ERV = CurRec->getValue(RV.getName())) {
68 // 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
157 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
158
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 #"
Bob Wilson21870412009-11-22 04:24:42 +0000180 + utostr(i) + " (" + TArgs[i] + ") of subclass '" +
Chris Lattnerf4601652007-11-22 20:49:04 +0000181 SC->getName() + "'!");
182 }
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 Greenede444af2009-04-22 16:42:54 +0000236 const std::vector<std::string> &SMCTArgs = SMC->Rec.getTemplateArgs();
237
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 #"
Bob Wilson32558652009-04-28 19:41:44 +0000284 + utostr(i) + " (" + SMCTArgs[i] + ") of subclass '" +
David Greenede444af2009-04-22 16:42:54 +0000285 SMC->Rec.getName() + "'!");
286 }
287 }
288
289 return false;
290}
291
Chris Lattnerf4601652007-11-22 20:49:04 +0000292//===----------------------------------------------------------------------===//
293// Parser Code
294//===----------------------------------------------------------------------===//
295
296/// isObjectStart - Return true if this is a valid first token for an Object.
297static bool isObjectStart(tgtok::TokKind K) {
298 return K == tgtok::Class || K == tgtok::Def ||
Bob Wilson21870412009-11-22 04:24:42 +0000299 K == tgtok::Defm || K == tgtok::Let || K == tgtok::MultiClass;
Chris Lattnerf4601652007-11-22 20:49:04 +0000300}
301
Chris Lattnerdf72eae2010-10-05 22:51:56 +0000302static std::string GetNewAnonymousName() {
303 static unsigned AnonCounter = 0;
304 return "anonymous."+utostr(AnonCounter++);
305}
306
Chris Lattnerf4601652007-11-22 20:49:04 +0000307/// ParseObjectName - If an object name is specified, return it. Otherwise,
308/// return an anonymous name.
309/// ObjectName ::= ID
310/// ObjectName ::= /*empty*/
311///
312std::string TGParser::ParseObjectName() {
Chris Lattnerdf72eae2010-10-05 22:51:56 +0000313 if (Lex.getCode() != tgtok::Id)
314 return GetNewAnonymousName();
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000315
Chris Lattnerdf72eae2010-10-05 22:51:56 +0000316 std::string Ret = Lex.getCurStrVal();
317 Lex.Lex();
318 return Ret;
Chris Lattnerf4601652007-11-22 20:49:04 +0000319}
320
321
322/// ParseClassID - Parse and resolve a reference to a class name. This returns
323/// null on error.
324///
325/// ClassID ::= ID
326///
327Record *TGParser::ParseClassID() {
328 if (Lex.getCode() != tgtok::Id) {
329 TokError("expected name for ClassID");
330 return 0;
331 }
Bob Wilson21870412009-11-22 04:24:42 +0000332
Chris Lattnerf4601652007-11-22 20:49:04 +0000333 Record *Result = Records.getClass(Lex.getCurStrVal());
334 if (Result == 0)
335 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson21870412009-11-22 04:24:42 +0000336
Chris Lattnerf4601652007-11-22 20:49:04 +0000337 Lex.Lex();
338 return Result;
339}
340
Bob Wilson32558652009-04-28 19:41:44 +0000341/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
342/// This returns null on error.
David Greenede444af2009-04-22 16:42:54 +0000343///
344/// MultiClassID ::= ID
345///
346MultiClass *TGParser::ParseMultiClassID() {
347 if (Lex.getCode() != tgtok::Id) {
348 TokError("expected name for ClassID");
349 return 0;
350 }
Bob Wilson32558652009-04-28 19:41:44 +0000351
David Greenede444af2009-04-22 16:42:54 +0000352 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
353 if (Result == 0)
354 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson32558652009-04-28 19:41:44 +0000355
David Greenede444af2009-04-22 16:42:54 +0000356 Lex.Lex();
357 return Result;
358}
359
Chris Lattnerf4601652007-11-22 20:49:04 +0000360Record *TGParser::ParseDefmID() {
361 if (Lex.getCode() != tgtok::Id) {
362 TokError("expected multiclass name");
363 return 0;
364 }
Bob Wilson21870412009-11-22 04:24:42 +0000365
Chris Lattnerf4601652007-11-22 20:49:04 +0000366 MultiClass *MC = MultiClasses[Lex.getCurStrVal()];
367 if (MC == 0) {
368 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
369 return 0;
370 }
Bob Wilson21870412009-11-22 04:24:42 +0000371
Chris Lattnerf4601652007-11-22 20:49:04 +0000372 Lex.Lex();
373 return &MC->Rec;
Bob Wilson21870412009-11-22 04:24:42 +0000374}
Chris Lattnerf4601652007-11-22 20:49:04 +0000375
376
377/// ParseSubClassReference - Parse a reference to a subclass or to a templated
378/// subclass. This returns a SubClassRefTy with a null Record* on error.
379///
380/// SubClassRef ::= ClassID
381/// SubClassRef ::= ClassID '<' ValueList '>'
382///
383SubClassReference TGParser::
384ParseSubClassReference(Record *CurRec, bool isDefm) {
385 SubClassReference Result;
386 Result.RefLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +0000387
Chris Lattnerf4601652007-11-22 20:49:04 +0000388 if (isDefm)
389 Result.Rec = ParseDefmID();
390 else
391 Result.Rec = ParseClassID();
392 if (Result.Rec == 0) return Result;
Bob Wilson21870412009-11-22 04:24:42 +0000393
Chris Lattnerf4601652007-11-22 20:49:04 +0000394 // If there is no template arg list, we're done.
395 if (Lex.getCode() != tgtok::less)
396 return Result;
397 Lex.Lex(); // Eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000398
Chris Lattnerf4601652007-11-22 20:49:04 +0000399 if (Lex.getCode() == tgtok::greater) {
400 TokError("subclass reference requires a non-empty list of template values");
401 Result.Rec = 0;
402 return Result;
403 }
Bob Wilson21870412009-11-22 04:24:42 +0000404
David Greenee1b46912009-06-08 20:23:18 +0000405 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4601652007-11-22 20:49:04 +0000406 if (Result.TemplateArgs.empty()) {
407 Result.Rec = 0; // Error parsing value list.
408 return Result;
409 }
Bob Wilson21870412009-11-22 04:24:42 +0000410
Chris Lattnerf4601652007-11-22 20:49:04 +0000411 if (Lex.getCode() != tgtok::greater) {
412 TokError("expected '>' in template value list");
413 Result.Rec = 0;
414 return Result;
415 }
416 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000417
Chris Lattnerf4601652007-11-22 20:49:04 +0000418 return Result;
419}
420
Bob Wilson32558652009-04-28 19:41:44 +0000421/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
422/// templated submulticlass. This returns a SubMultiClassRefTy with a null
423/// Record* on error.
David Greenede444af2009-04-22 16:42:54 +0000424///
425/// SubMultiClassRef ::= MultiClassID
426/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
427///
428SubMultiClassReference TGParser::
429ParseSubMultiClassReference(MultiClass *CurMC) {
430 SubMultiClassReference Result;
431 Result.RefLoc = Lex.getLoc();
Bob Wilson32558652009-04-28 19:41:44 +0000432
David Greenede444af2009-04-22 16:42:54 +0000433 Result.MC = ParseMultiClassID();
434 if (Result.MC == 0) return Result;
Bob Wilson32558652009-04-28 19:41:44 +0000435
David Greenede444af2009-04-22 16:42:54 +0000436 // If there is no template arg list, we're done.
437 if (Lex.getCode() != tgtok::less)
438 return Result;
439 Lex.Lex(); // Eat the '<'
Bob Wilson32558652009-04-28 19:41:44 +0000440
David Greenede444af2009-04-22 16:42:54 +0000441 if (Lex.getCode() == tgtok::greater) {
442 TokError("subclass reference requires a non-empty list of template values");
443 Result.MC = 0;
444 return Result;
445 }
Bob Wilson32558652009-04-28 19:41:44 +0000446
David Greenee1b46912009-06-08 20:23:18 +0000447 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greenede444af2009-04-22 16:42:54 +0000448 if (Result.TemplateArgs.empty()) {
449 Result.MC = 0; // Error parsing value list.
450 return Result;
451 }
Bob Wilson32558652009-04-28 19:41:44 +0000452
David Greenede444af2009-04-22 16:42:54 +0000453 if (Lex.getCode() != tgtok::greater) {
454 TokError("expected '>' in template value list");
455 Result.MC = 0;
456 return Result;
457 }
458 Lex.Lex();
459
460 return Result;
461}
462
Chris Lattnerf4601652007-11-22 20:49:04 +0000463/// ParseRangePiece - Parse a bit/value range.
464/// RangePiece ::= INTVAL
465/// RangePiece ::= INTVAL '-' INTVAL
466/// RangePiece ::= INTVAL INTVAL
467bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner811281e2008-01-10 07:01:53 +0000468 if (Lex.getCode() != tgtok::IntVal) {
469 TokError("expected integer or bitrange");
470 return true;
471 }
Dan Gohman63f97202008-10-17 01:33:43 +0000472 int64_t Start = Lex.getCurIntVal();
473 int64_t End;
Bob Wilson21870412009-11-22 04:24:42 +0000474
Chris Lattnerf4601652007-11-22 20:49:04 +0000475 if (Start < 0)
476 return TokError("invalid range, cannot be negative");
Bob Wilson21870412009-11-22 04:24:42 +0000477
Chris Lattnerf4601652007-11-22 20:49:04 +0000478 switch (Lex.Lex()) { // eat first character.
Bob Wilson21870412009-11-22 04:24:42 +0000479 default:
Chris Lattnerf4601652007-11-22 20:49:04 +0000480 Ranges.push_back(Start);
481 return false;
482 case tgtok::minus:
483 if (Lex.Lex() != tgtok::IntVal) {
484 TokError("expected integer value as end of range");
485 return true;
486 }
487 End = Lex.getCurIntVal();
488 break;
489 case tgtok::IntVal:
490 End = -Lex.getCurIntVal();
491 break;
492 }
Bob Wilson21870412009-11-22 04:24:42 +0000493 if (End < 0)
Chris Lattnerf4601652007-11-22 20:49:04 +0000494 return TokError("invalid range, cannot be negative");
495 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000496
Chris Lattnerf4601652007-11-22 20:49:04 +0000497 // Add to the range.
498 if (Start < End) {
499 for (; Start <= End; ++Start)
500 Ranges.push_back(Start);
501 } else {
502 for (; Start >= End; --Start)
503 Ranges.push_back(Start);
504 }
505 return false;
506}
507
508/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
509///
510/// RangeList ::= RangePiece (',' RangePiece)*
511///
512std::vector<unsigned> TGParser::ParseRangeList() {
513 std::vector<unsigned> Result;
Bob Wilson21870412009-11-22 04:24:42 +0000514
Chris Lattnerf4601652007-11-22 20:49:04 +0000515 // Parse the first piece.
516 if (ParseRangePiece(Result))
517 return std::vector<unsigned>();
518 while (Lex.getCode() == tgtok::comma) {
519 Lex.Lex(); // Eat the comma.
520
521 // Parse the next range piece.
522 if (ParseRangePiece(Result))
523 return std::vector<unsigned>();
524 }
525 return Result;
526}
527
528/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
529/// OptionalRangeList ::= '<' RangeList '>'
530/// OptionalRangeList ::= /*empty*/
531bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
532 if (Lex.getCode() != tgtok::less)
533 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000534
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000535 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000536 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000537
Chris Lattnerf4601652007-11-22 20:49:04 +0000538 // Parse the range list.
539 Ranges = ParseRangeList();
540 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000541
Chris Lattnerf4601652007-11-22 20:49:04 +0000542 if (Lex.getCode() != tgtok::greater) {
543 TokError("expected '>' at end of range list");
544 return Error(StartLoc, "to match this '<'");
545 }
546 Lex.Lex(); // eat the '>'.
547 return false;
548}
549
550/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
551/// OptionalBitList ::= '{' RangeList '}'
552/// OptionalBitList ::= /*empty*/
553bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
554 if (Lex.getCode() != tgtok::l_brace)
555 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000556
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000557 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000558 Lex.Lex(); // eat the '{'
Bob Wilson21870412009-11-22 04:24:42 +0000559
Chris Lattnerf4601652007-11-22 20:49:04 +0000560 // Parse the range list.
561 Ranges = ParseRangeList();
562 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000563
Chris Lattnerf4601652007-11-22 20:49:04 +0000564 if (Lex.getCode() != tgtok::r_brace) {
565 TokError("expected '}' at end of bit list");
566 return Error(StartLoc, "to match this '{'");
567 }
568 Lex.Lex(); // eat the '}'.
569 return false;
570}
571
572
573/// ParseType - Parse and return a tblgen type. This returns null on error.
574///
575/// Type ::= STRING // string type
576/// Type ::= BIT // bit type
577/// Type ::= BITS '<' INTVAL '>' // bits<x> type
578/// Type ::= INT // int type
579/// Type ::= LIST '<' Type '>' // list<x> type
580/// Type ::= CODE // code type
581/// Type ::= DAG // dag type
582/// Type ::= ClassID // Record Type
583///
584RecTy *TGParser::ParseType() {
585 switch (Lex.getCode()) {
586 default: TokError("Unknown token when expecting a type"); return 0;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000587 case tgtok::String: Lex.Lex(); return StringRecTy::get();
588 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
589 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
590 case tgtok::Code: Lex.Lex(); return CodeRecTy::get();
591 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4601652007-11-22 20:49:04 +0000592 case tgtok::Id:
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000593 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Chris Lattnerf4601652007-11-22 20:49:04 +0000594 return 0;
595 case tgtok::Bits: {
596 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
597 TokError("expected '<' after bits type");
598 return 0;
599 }
600 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
601 TokError("expected integer in bits<n> type");
602 return 0;
603 }
Dan Gohman63f97202008-10-17 01:33:43 +0000604 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4601652007-11-22 20:49:04 +0000605 if (Lex.Lex() != tgtok::greater) { // Eat count.
606 TokError("expected '>' at end of bits<n> type");
607 return 0;
608 }
609 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000610 return BitsRecTy::get(Val);
Chris Lattnerf4601652007-11-22 20:49:04 +0000611 }
612 case tgtok::List: {
613 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
614 TokError("expected '<' after list type");
615 return 0;
616 }
617 Lex.Lex(); // Eat '<'
618 RecTy *SubType = ParseType();
619 if (SubType == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +0000620
Chris Lattnerf4601652007-11-22 20:49:04 +0000621 if (Lex.getCode() != tgtok::greater) {
622 TokError("expected '>' at end of list<ty> type");
623 return 0;
624 }
625 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000626 return ListRecTy::get(SubType);
Chris Lattnerf4601652007-11-22 20:49:04 +0000627 }
Bob Wilson21870412009-11-22 04:24:42 +0000628 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000629}
630
631/// ParseIDValue - Parse an ID as a value and decode what it means.
632///
633/// IDValue ::= ID [def local value]
634/// IDValue ::= ID [def template arg]
635/// IDValue ::= ID [multiclass local value]
636/// IDValue ::= ID [multiclass template argument]
637/// IDValue ::= ID [def name]
638///
David Greene05bce0b2011-07-29 22:43:06 +0000639Init *TGParser::ParseIDValue(Record *CurRec) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000640 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
641 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000642 SMLoc Loc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000643 Lex.Lex();
644 return ParseIDValue(CurRec, Name, Loc);
645}
646
647/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
648/// has already been read.
David Greene05bce0b2011-07-29 22:43:06 +0000649Init *TGParser::ParseIDValue(Record *CurRec,
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000650 const std::string &Name, SMLoc NameLoc) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000651 if (CurRec) {
652 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenedcd35c72011-07-29 19:07:07 +0000653 return VarInit::get(Name, RV->getType());
Bob Wilson21870412009-11-22 04:24:42 +0000654
Chris Lattnerf4601652007-11-22 20:49:04 +0000655 std::string TemplateArgName = CurRec->getName()+":"+Name;
David Greenecaa25c82011-10-05 22:42:54 +0000656 if (CurMultiClass)
657 TemplateArgName = CurMultiClass->Rec.getName()+"::"+TemplateArgName;
658
Chris Lattnerf4601652007-11-22 20:49:04 +0000659 if (CurRec->isTemplateArg(TemplateArgName)) {
660 const RecordVal *RV = CurRec->getValue(TemplateArgName);
661 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000662 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000663 }
664 }
Bob Wilson21870412009-11-22 04:24:42 +0000665
Chris Lattnerf4601652007-11-22 20:49:04 +0000666 if (CurMultiClass) {
667 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
668 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
669 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
670 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000671 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000672 }
673 }
Bob Wilson21870412009-11-22 04:24:42 +0000674
Chris Lattnerf4601652007-11-22 20:49:04 +0000675 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000676 return DefInit::get(D);
Chris Lattnerf4601652007-11-22 20:49:04 +0000677
678 Error(NameLoc, "Variable not defined: '" + Name + "'");
679 return 0;
680}
681
David Greened418c1b2009-05-14 20:54:48 +0000682/// ParseOperation - Parse an operator. This returns null on error.
683///
684/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
685///
David Greene05bce0b2011-07-29 22:43:06 +0000686Init *TGParser::ParseOperation(Record *CurRec) {
David Greened418c1b2009-05-14 20:54:48 +0000687 switch (Lex.getCode()) {
688 default:
689 TokError("unknown operation");
690 return 0;
691 break;
David Greene1434f662011-01-07 17:05:37 +0000692 case tgtok::XHead:
693 case tgtok::XTail:
694 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +0000695 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
696 UnOpInit::UnaryOp Code;
697 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000698
David Greenee6c27de2009-05-14 21:22:49 +0000699 switch (Lex.getCode()) {
700 default: assert(0 && "Unhandled code!");
701 case tgtok::XCast:
702 Lex.Lex(); // eat the operation
703 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000704
David Greenee6c27de2009-05-14 21:22:49 +0000705 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000706
David Greenee6c27de2009-05-14 21:22:49 +0000707 if (Type == 0) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000708 TokError("did not get type for unary operator");
David Greenee6c27de2009-05-14 21:22:49 +0000709 return 0;
710 }
David Greened418c1b2009-05-14 20:54:48 +0000711
David Greenee6c27de2009-05-14 21:22:49 +0000712 break;
David Greene1434f662011-01-07 17:05:37 +0000713 case tgtok::XHead:
David Greene5f9f9ba2009-05-14 22:38:31 +0000714 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000715 Code = UnOpInit::HEAD;
David Greene5f9f9ba2009-05-14 22:38:31 +0000716 break;
David Greene1434f662011-01-07 17:05:37 +0000717 case tgtok::XTail:
David Greene5f9f9ba2009-05-14 22:38:31 +0000718 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000719 Code = UnOpInit::TAIL;
David Greene5f9f9ba2009-05-14 22:38:31 +0000720 break;
David Greene1434f662011-01-07 17:05:37 +0000721 case tgtok::XEmpty:
David Greene5f9f9ba2009-05-14 22:38:31 +0000722 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000723 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000724 Type = IntRecTy::get();
David Greene5f9f9ba2009-05-14 22:38:31 +0000725 break;
David Greenee6c27de2009-05-14 21:22:49 +0000726 }
727 if (Lex.getCode() != tgtok::l_paren) {
728 TokError("expected '(' after unary operator");
729 return 0;
730 }
731 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000732
David Greene05bce0b2011-07-29 22:43:06 +0000733 Init *LHS = ParseValue(CurRec);
David Greenee6c27de2009-05-14 21:22:49 +0000734 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000735
David Greene1434f662011-01-07 17:05:37 +0000736 if (Code == UnOpInit::HEAD
737 || Code == UnOpInit::TAIL
738 || Code == UnOpInit::EMPTY) {
David Greene05bce0b2011-07-29 22:43:06 +0000739 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
740 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
741 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000742 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
743 TokError("expected list or string type argument in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000744 return 0;
745 }
746 if (LHSt) {
747 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
David Greenee1b46912009-06-08 20:23:18 +0000748 StringRecTy *SType = dynamic_cast<StringRecTy*>(LHSt->getType());
749 if (LType == 0 && SType == 0) {
750 TokError("expected list or string type argumnet in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000751 return 0;
752 }
753 }
754
David Greene1434f662011-01-07 17:05:37 +0000755 if (Code == UnOpInit::HEAD
756 || Code == UnOpInit::TAIL) {
David Greenee1b46912009-06-08 20:23:18 +0000757 if (LHSl == 0 && LHSt == 0) {
758 TokError("expected list type argumnet in unary operator");
759 return 0;
760 }
Bob Wilson21870412009-11-22 04:24:42 +0000761
David Greene5f9f9ba2009-05-14 22:38:31 +0000762 if (LHSl && LHSl->getSize() == 0) {
763 TokError("empty list argument in unary operator");
764 return 0;
765 }
766 if (LHSl) {
David Greene05bce0b2011-07-29 22:43:06 +0000767 Init *Item = LHSl->getElement(0);
768 TypedInit *Itemt = dynamic_cast<TypedInit*>(Item);
David Greene5f9f9ba2009-05-14 22:38:31 +0000769 if (Itemt == 0) {
770 TokError("untyped list element in unary operator");
771 return 0;
772 }
David Greene1434f662011-01-07 17:05:37 +0000773 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000774 Type = Itemt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000775 } else {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000776 Type = ListRecTy::get(Itemt->getType());
David Greene5f9f9ba2009-05-14 22:38:31 +0000777 }
Bob Wilson21870412009-11-22 04:24:42 +0000778 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000779 assert(LHSt && "expected list type argument in unary operator");
780 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
781 if (LType == 0) {
782 TokError("expected list type argumnet in unary operator");
783 return 0;
784 }
David Greene1434f662011-01-07 17:05:37 +0000785 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000786 Type = LType->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +0000787 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000788 Type = LType;
789 }
790 }
791 }
792 }
793
David Greenee6c27de2009-05-14 21:22:49 +0000794 if (Lex.getCode() != tgtok::r_paren) {
795 TokError("expected ')' in unary operator");
796 return 0;
797 }
798 Lex.Lex(); // eat the ')'
David Greenedcd35c72011-07-29 19:07:07 +0000799 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee6c27de2009-05-14 21:22:49 +0000800 }
David Greened418c1b2009-05-14 20:54:48 +0000801
802 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +0000803 case tgtok::XSRA:
David Greened418c1b2009-05-14 20:54:48 +0000804 case tgtok::XSRL:
805 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +0000806 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +0000807 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000808 tgtok::TokKind OpTok = Lex.getCode();
809 SMLoc OpLoc = Lex.getLoc();
810 Lex.Lex(); // eat the operation
811
David Greened418c1b2009-05-14 20:54:48 +0000812 BinOpInit::BinaryOp Code;
813 RecTy *Type = 0;
814
Chris Lattner8d978a72010-10-05 23:58:18 +0000815 switch (OpTok) {
David Greened418c1b2009-05-14 20:54:48 +0000816 default: assert(0 && "Unhandled code!");
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000817 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
818 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
819 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
820 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
821 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Bob Wilson21870412009-11-22 04:24:42 +0000822 case tgtok::XStrConcat:
David Greened418c1b2009-05-14 20:54:48 +0000823 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000824 Type = StringRecTy::get();
David Greened418c1b2009-05-14 20:54:48 +0000825 break;
David Greened418c1b2009-05-14 20:54:48 +0000826 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000827
David Greened418c1b2009-05-14 20:54:48 +0000828 if (Lex.getCode() != tgtok::l_paren) {
829 TokError("expected '(' after binary operator");
830 return 0;
831 }
832 Lex.Lex(); // eat the '('
833
David Greene05bce0b2011-07-29 22:43:06 +0000834 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000835
Chris Lattner8d978a72010-10-05 23:58:18 +0000836 InitList.push_back(ParseValue(CurRec));
837 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000838
Chris Lattner8d978a72010-10-05 23:58:18 +0000839 while (Lex.getCode() == tgtok::comma) {
840 Lex.Lex(); // eat the ','
841
842 InitList.push_back(ParseValue(CurRec));
843 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000844 }
David Greened418c1b2009-05-14 20:54:48 +0000845
846 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner8d978a72010-10-05 23:58:18 +0000847 TokError("expected ')' in operator");
David Greened418c1b2009-05-14 20:54:48 +0000848 return 0;
849 }
850 Lex.Lex(); // eat the ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000851
852 // We allow multiple operands to associative operators like !strconcat as
853 // shorthand for nesting them.
854 if (Code == BinOpInit::STRCONCAT) {
855 while (InitList.size() > 2) {
David Greene05bce0b2011-07-29 22:43:06 +0000856 Init *RHS = InitList.pop_back_val();
David Greenedcd35c72011-07-29 19:07:07 +0000857 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
858 ->Fold(CurRec, CurMultiClass);
Chris Lattner8d978a72010-10-05 23:58:18 +0000859 InitList.back() = RHS;
860 }
861 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000862
Chris Lattner8d978a72010-10-05 23:58:18 +0000863 if (InitList.size() == 2)
David Greenedcd35c72011-07-29 19:07:07 +0000864 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner8d978a72010-10-05 23:58:18 +0000865 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000866
Chris Lattner8d978a72010-10-05 23:58:18 +0000867 Error(OpLoc, "expected two operands to operator");
868 return 0;
David Greened418c1b2009-05-14 20:54:48 +0000869 }
870
David Greene9bea7c82009-05-14 23:26:46 +0000871 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +0000872 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +0000873 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
874 TernOpInit::TernaryOp Code;
875 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000876
David Greene4afc5092009-05-14 21:54:42 +0000877 tgtok::TokKind LexCode = Lex.getCode();
878 Lex.Lex(); // eat the operation
879 switch (LexCode) {
880 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000881 case tgtok::XIf:
882 Code = TernOpInit::IF;
883 break;
David Greenebeb31a52009-05-14 22:23:47 +0000884 case tgtok::XForEach:
885 Code = TernOpInit::FOREACH;
886 break;
David Greene4afc5092009-05-14 21:54:42 +0000887 case tgtok::XSubst:
888 Code = TernOpInit::SUBST;
889 break;
890 }
891 if (Lex.getCode() != tgtok::l_paren) {
892 TokError("expected '(' after ternary operator");
893 return 0;
894 }
895 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000896
David Greene05bce0b2011-07-29 22:43:06 +0000897 Init *LHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +0000898 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000899
David Greene4afc5092009-05-14 21:54:42 +0000900 if (Lex.getCode() != tgtok::comma) {
901 TokError("expected ',' in ternary operator");
902 return 0;
903 }
904 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000905
David Greene05bce0b2011-07-29 22:43:06 +0000906 Init *MHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +0000907 if (MHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000908
David Greene4afc5092009-05-14 21:54:42 +0000909 if (Lex.getCode() != tgtok::comma) {
910 TokError("expected ',' in ternary operator");
911 return 0;
912 }
913 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000914
David Greene05bce0b2011-07-29 22:43:06 +0000915 Init *RHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +0000916 if (RHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000917
David Greene4afc5092009-05-14 21:54:42 +0000918 if (Lex.getCode() != tgtok::r_paren) {
919 TokError("expected ')' in binary operator");
920 return 0;
921 }
922 Lex.Lex(); // eat the ')'
David Greened418c1b2009-05-14 20:54:48 +0000923
David Greene4afc5092009-05-14 21:54:42 +0000924 switch (LexCode) {
925 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000926 case tgtok::XIf: {
Bill Wendling548f5a02010-12-13 01:46:19 +0000927 // FIXME: The `!if' operator doesn't handle non-TypedInit well at
928 // all. This can be made much more robust.
David Greene05bce0b2011-07-29 22:43:06 +0000929 TypedInit *MHSt = dynamic_cast<TypedInit*>(MHS);
930 TypedInit *RHSt = dynamic_cast<TypedInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +0000931
932 RecTy *MHSTy = 0;
933 RecTy *RHSTy = 0;
934
935 if (MHSt == 0 && RHSt == 0) {
David Greene05bce0b2011-07-29 22:43:06 +0000936 BitsInit *MHSbits = dynamic_cast<BitsInit*>(MHS);
937 BitsInit *RHSbits = dynamic_cast<BitsInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +0000938
939 if (MHSbits && RHSbits &&
940 MHSbits->getNumBits() == RHSbits->getNumBits()) {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000941 Type = BitRecTy::get();
Bill Wendling548f5a02010-12-13 01:46:19 +0000942 break;
943 } else {
David Greene05bce0b2011-07-29 22:43:06 +0000944 BitInit *MHSbit = dynamic_cast<BitInit*>(MHS);
945 BitInit *RHSbit = dynamic_cast<BitInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +0000946
947 if (MHSbit && RHSbit) {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000948 Type = BitRecTy::get();
Bill Wendling548f5a02010-12-13 01:46:19 +0000949 break;
950 }
951 }
952 } else if (MHSt != 0 && RHSt != 0) {
953 MHSTy = MHSt->getType();
954 RHSTy = RHSt->getType();
955 }
956
957 if (!MHSTy || !RHSTy) {
David Greene9bea7c82009-05-14 23:26:46 +0000958 TokError("could not get type for !if");
959 return 0;
960 }
Bill Wendling548f5a02010-12-13 01:46:19 +0000961
962 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
963 Type = RHSTy;
964 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
965 Type = MHSTy;
Bob Wilson21870412009-11-22 04:24:42 +0000966 } else {
David Greene9bea7c82009-05-14 23:26:46 +0000967 TokError("inconsistent types for !if");
968 return 0;
969 }
970 break;
971 }
David Greenebeb31a52009-05-14 22:23:47 +0000972 case tgtok::XForEach: {
David Greene05bce0b2011-07-29 22:43:06 +0000973 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
David Greenebeb31a52009-05-14 22:23:47 +0000974 if (MHSt == 0) {
975 TokError("could not get type for !foreach");
976 return 0;
977 }
978 Type = MHSt->getType();
979 break;
980 }
David Greene4afc5092009-05-14 21:54:42 +0000981 case tgtok::XSubst: {
David Greene05bce0b2011-07-29 22:43:06 +0000982 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
David Greene4afc5092009-05-14 21:54:42 +0000983 if (RHSt == 0) {
984 TokError("could not get type for !subst");
985 return 0;
986 }
987 Type = RHSt->getType();
988 break;
989 }
990 }
David Greenedcd35c72011-07-29 19:07:07 +0000991 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson21870412009-11-22 04:24:42 +0000992 CurMultiClass);
David Greene4afc5092009-05-14 21:54:42 +0000993 }
David Greened418c1b2009-05-14 20:54:48 +0000994 }
995 TokError("could not parse operation");
996 return 0;
997}
998
999/// ParseOperatorType - Parse a type for an operator. This returns
1000/// null on error.
1001///
1002/// OperatorType ::= '<' Type '>'
1003///
Dan Gohmana9ad0412009-08-12 22:10:57 +00001004RecTy *TGParser::ParseOperatorType() {
David Greened418c1b2009-05-14 20:54:48 +00001005 RecTy *Type = 0;
1006
1007 if (Lex.getCode() != tgtok::less) {
1008 TokError("expected type name for operator");
1009 return 0;
1010 }
1011 Lex.Lex(); // eat the <
1012
1013 Type = ParseType();
1014
1015 if (Type == 0) {
1016 TokError("expected type name for operator");
1017 return 0;
1018 }
1019
1020 if (Lex.getCode() != tgtok::greater) {
1021 TokError("expected type name for operator");
1022 return 0;
1023 }
1024 Lex.Lex(); // eat the >
1025
1026 return Type;
1027}
1028
1029
Chris Lattnerf4601652007-11-22 20:49:04 +00001030/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1031///
1032/// SimpleValue ::= IDValue
1033/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001034/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +00001035/// SimpleValue ::= CODEFRAGMENT
1036/// SimpleValue ::= '?'
1037/// SimpleValue ::= '{' ValueList '}'
1038/// SimpleValue ::= ID '<' ValueListNE '>'
1039/// SimpleValue ::= '[' ValueList ']'
1040/// SimpleValue ::= '(' IDValue DagArgList ')'
1041/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
1042/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1043/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1044/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1045/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1046///
David Greene05bce0b2011-07-29 22:43:06 +00001047Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType) {
1048 Init *R = 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001049 switch (Lex.getCode()) {
1050 default: TokError("Unknown token when parsing a value"); break;
David Greenedcd35c72011-07-29 19:07:07 +00001051 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001052 case tgtok::StrVal: {
1053 std::string Val = Lex.getCurStrVal();
1054 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001055
Jim Grosbachda4231f2009-03-26 16:17:51 +00001056 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001057 while (Lex.getCode() == tgtok::StrVal) {
1058 Val += Lex.getCurStrVal();
1059 Lex.Lex();
1060 }
Bob Wilson21870412009-11-22 04:24:42 +00001061
David Greenedcd35c72011-07-29 19:07:07 +00001062 R = StringInit::get(Val);
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001063 break;
1064 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001065 case tgtok::CodeFragment:
David Greenedcd35c72011-07-29 19:07:07 +00001066 R = CodeInit::get(Lex.getCurStrVal());
Chris Lattner578bcf02010-10-06 04:31:40 +00001067 Lex.Lex();
1068 break;
1069 case tgtok::question:
David Greenedcd35c72011-07-29 19:07:07 +00001070 R = UnsetInit::get();
Chris Lattner578bcf02010-10-06 04:31:40 +00001071 Lex.Lex();
1072 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001073 case tgtok::Id: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001074 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001075 std::string Name = Lex.getCurStrVal();
1076 if (Lex.Lex() != tgtok::less) // consume the Id.
1077 return ParseIDValue(CurRec, Name, NameLoc); // Value ::= IDValue
Bob Wilson21870412009-11-22 04:24:42 +00001078
Chris Lattnerf4601652007-11-22 20:49:04 +00001079 // Value ::= ID '<' ValueListNE '>'
1080 if (Lex.Lex() == tgtok::greater) {
1081 TokError("expected non-empty value list");
1082 return 0;
1083 }
David Greenee1b46912009-06-08 20:23:18 +00001084
Chris Lattnerf4601652007-11-22 20:49:04 +00001085 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1086 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1087 // body.
1088 Record *Class = Records.getClass(Name);
1089 if (!Class) {
1090 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1091 return 0;
1092 }
David Greenee1b46912009-06-08 20:23:18 +00001093
David Greene05bce0b2011-07-29 22:43:06 +00001094 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
David Greenee1b46912009-06-08 20:23:18 +00001095 if (ValueList.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001096
David Greenee1b46912009-06-08 20:23:18 +00001097 if (Lex.getCode() != tgtok::greater) {
1098 TokError("expected '>' at end of value list");
1099 return 0;
1100 }
1101 Lex.Lex(); // eat the '>'
Bob Wilson21870412009-11-22 04:24:42 +00001102
Chris Lattnerf4601652007-11-22 20:49:04 +00001103 // Create the new record, set it as CurRec temporarily.
1104 static unsigned AnonCounter = 0;
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001105 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),
1106 NameLoc,
1107 Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001108 SubClassReference SCRef;
1109 SCRef.RefLoc = NameLoc;
1110 SCRef.Rec = Class;
1111 SCRef.TemplateArgs = ValueList;
1112 // Add info about the subclass to NewRec.
1113 if (AddSubClass(NewRec, SCRef))
1114 return 0;
1115 NewRec->resolveReferences();
1116 Records.addDef(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001117
Chris Lattnerf4601652007-11-22 20:49:04 +00001118 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +00001119 return DefInit::get(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001120 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001121 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001122 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001123 Lex.Lex(); // eat the '{'
David Greene05bce0b2011-07-29 22:43:06 +00001124 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001125
Chris Lattnerf4601652007-11-22 20:49:04 +00001126 if (Lex.getCode() != tgtok::r_brace) {
1127 Vals = ParseValueList(CurRec);
1128 if (Vals.empty()) return 0;
1129 }
1130 if (Lex.getCode() != tgtok::r_brace) {
1131 TokError("expected '}' at end of bit list value");
1132 return 0;
1133 }
1134 Lex.Lex(); // eat the '}'
Bob Wilson21870412009-11-22 04:24:42 +00001135
David Greene05bce0b2011-07-29 22:43:06 +00001136 SmallVector<Init *, 16> NewBits(Vals.size());
David Greeneca7fd3d2011-07-29 19:07:00 +00001137
Chris Lattnerf4601652007-11-22 20:49:04 +00001138 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001139 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Chris Lattnerf4601652007-11-22 20:49:04 +00001140 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +00001141 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1142 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +00001143 return 0;
1144 }
David Greeneca7fd3d2011-07-29 19:07:00 +00001145 NewBits[Vals.size()-i-1] = Bit;
Chris Lattnerf4601652007-11-22 20:49:04 +00001146 }
David Greenedcd35c72011-07-29 19:07:07 +00001147 return BitsInit::get(NewBits);
Chris Lattnerf4601652007-11-22 20:49:04 +00001148 }
1149 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1150 Lex.Lex(); // eat the '['
David Greene05bce0b2011-07-29 22:43:06 +00001151 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001152
David Greenee1b46912009-06-08 20:23:18 +00001153 RecTy *DeducedEltTy = 0;
1154 ListRecTy *GivenListTy = 0;
Bob Wilson21870412009-11-22 04:24:42 +00001155
David Greenee1b46912009-06-08 20:23:18 +00001156 if (ItemType != 0) {
1157 ListRecTy *ListType = dynamic_cast<ListRecTy*>(ItemType);
1158 if (ListType == 0) {
1159 std::stringstream s;
Bob Wilson21870412009-11-22 04:24:42 +00001160 s << "Type mismatch for list, expected list type, got "
David Greenee1b46912009-06-08 20:23:18 +00001161 << ItemType->getAsString();
1162 TokError(s.str());
Jim Grosbach6a44ada2011-03-11 19:52:52 +00001163 return 0;
David Greenee1b46912009-06-08 20:23:18 +00001164 }
1165 GivenListTy = ListType;
Bob Wilson21870412009-11-22 04:24:42 +00001166 }
David Greenee1b46912009-06-08 20:23:18 +00001167
Chris Lattnerf4601652007-11-22 20:49:04 +00001168 if (Lex.getCode() != tgtok::r_square) {
Bob Wilson21870412009-11-22 04:24:42 +00001169 Vals = ParseValueList(CurRec, 0,
1170 GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001171 if (Vals.empty()) return 0;
1172 }
1173 if (Lex.getCode() != tgtok::r_square) {
1174 TokError("expected ']' at end of list value");
1175 return 0;
1176 }
1177 Lex.Lex(); // eat the ']'
David Greenee1b46912009-06-08 20:23:18 +00001178
1179 RecTy *GivenEltTy = 0;
1180 if (Lex.getCode() == tgtok::less) {
1181 // Optional list element type
1182 Lex.Lex(); // eat the '<'
1183
1184 GivenEltTy = ParseType();
1185 if (GivenEltTy == 0) {
1186 // Couldn't parse element type
1187 return 0;
1188 }
1189
1190 if (Lex.getCode() != tgtok::greater) {
1191 TokError("expected '>' at end of list element type");
1192 return 0;
1193 }
1194 Lex.Lex(); // eat the '>'
1195 }
1196
1197 // Check elements
1198 RecTy *EltTy = 0;
David Greene05bce0b2011-07-29 22:43:06 +00001199 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greenee1b46912009-06-08 20:23:18 +00001200 i != ie;
1201 ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001202 TypedInit *TArg = dynamic_cast<TypedInit*>(*i);
David Greenee1b46912009-06-08 20:23:18 +00001203 if (TArg == 0) {
1204 TokError("Untyped list element");
1205 return 0;
1206 }
1207 if (EltTy != 0) {
1208 EltTy = resolveTypes(EltTy, TArg->getType());
1209 if (EltTy == 0) {
1210 TokError("Incompatible types in list elements");
1211 return 0;
1212 }
Bob Wilson21870412009-11-22 04:24:42 +00001213 } else {
David Greenee1b46912009-06-08 20:23:18 +00001214 EltTy = TArg->getType();
1215 }
1216 }
1217
1218 if (GivenEltTy != 0) {
1219 if (EltTy != 0) {
1220 // Verify consistency
1221 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1222 TokError("Incompatible types in list elements");
1223 return 0;
1224 }
1225 }
1226 EltTy = GivenEltTy;
1227 }
1228
1229 if (EltTy == 0) {
1230 if (ItemType == 0) {
1231 TokError("No type for list");
1232 return 0;
1233 }
1234 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +00001235 } else {
David Greenee1b46912009-06-08 20:23:18 +00001236 // Make sure the deduced type is compatible with the given type
1237 if (GivenListTy) {
1238 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1239 TokError("Element type mismatch for list");
1240 return 0;
1241 }
1242 }
1243 DeducedEltTy = EltTy;
1244 }
Bob Wilson21870412009-11-22 04:24:42 +00001245
David Greenedcd35c72011-07-29 19:07:07 +00001246 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4601652007-11-22 20:49:04 +00001247 }
1248 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1249 Lex.Lex(); // eat the '('
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001250 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001251 TokError("expected identifier in dag init");
1252 return 0;
1253 }
Bob Wilson21870412009-11-22 04:24:42 +00001254
David Greene05bce0b2011-07-29 22:43:06 +00001255 Init *Operator = ParseValue(CurRec);
Chris Lattner578bcf02010-10-06 04:31:40 +00001256 if (Operator == 0) return 0;
David Greenec7cafcd2009-04-22 20:18:10 +00001257
Nate Begeman7cee8172009-03-19 05:21:56 +00001258 // If the operator name is present, parse it.
1259 std::string OperatorName;
1260 if (Lex.getCode() == tgtok::colon) {
1261 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1262 TokError("expected variable name in dag operator");
1263 return 0;
1264 }
1265 OperatorName = Lex.getCurStrVal();
1266 Lex.Lex(); // eat the VarName.
1267 }
Bob Wilson21870412009-11-22 04:24:42 +00001268
David Greene05bce0b2011-07-29 22:43:06 +00001269 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4601652007-11-22 20:49:04 +00001270 if (Lex.getCode() != tgtok::r_paren) {
1271 DagArgs = ParseDagArgList(CurRec);
1272 if (DagArgs.empty()) return 0;
1273 }
Bob Wilson21870412009-11-22 04:24:42 +00001274
Chris Lattnerf4601652007-11-22 20:49:04 +00001275 if (Lex.getCode() != tgtok::r_paren) {
1276 TokError("expected ')' in dag init");
1277 return 0;
1278 }
1279 Lex.Lex(); // eat the ')'
Bob Wilson21870412009-11-22 04:24:42 +00001280
David Greenedcd35c72011-07-29 19:07:07 +00001281 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4601652007-11-22 20:49:04 +00001282 }
Bob Wilson21870412009-11-22 04:24:42 +00001283
David Greene1434f662011-01-07 17:05:37 +00001284 case tgtok::XHead:
1285 case tgtok::XTail:
1286 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +00001287 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001288 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +00001289 case tgtok::XSRA:
Chris Lattnerf4601652007-11-22 20:49:04 +00001290 case tgtok::XSRL:
1291 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +00001292 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001293 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene9bea7c82009-05-14 23:26:46 +00001294 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +00001295 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001296 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greened418c1b2009-05-14 20:54:48 +00001297 return ParseOperation(CurRec);
Chris Lattnerf4601652007-11-22 20:49:04 +00001298 }
1299 }
Bob Wilson21870412009-11-22 04:24:42 +00001300
Chris Lattnerf4601652007-11-22 20:49:04 +00001301 return R;
1302}
1303
1304/// ParseValue - Parse a tblgen value. This returns null on error.
1305///
1306/// Value ::= SimpleValue ValueSuffix*
1307/// ValueSuffix ::= '{' BitList '}'
1308/// ValueSuffix ::= '[' BitList ']'
1309/// ValueSuffix ::= '.' ID
1310///
David Greene05bce0b2011-07-29 22:43:06 +00001311Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType) {
1312 Init *Result = ParseSimpleValue(CurRec, ItemType);
Chris Lattnerf4601652007-11-22 20:49:04 +00001313 if (Result == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001314
Chris Lattnerf4601652007-11-22 20:49:04 +00001315 // Parse the suffixes now if present.
1316 while (1) {
1317 switch (Lex.getCode()) {
1318 default: return Result;
1319 case tgtok::l_brace: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001320 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001321 Lex.Lex(); // eat the '{'
1322 std::vector<unsigned> Ranges = ParseRangeList();
1323 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001324
Chris Lattnerf4601652007-11-22 20:49:04 +00001325 // Reverse the bitlist.
1326 std::reverse(Ranges.begin(), Ranges.end());
1327 Result = Result->convertInitializerBitRange(Ranges);
1328 if (Result == 0) {
1329 Error(CurlyLoc, "Invalid bit range for value");
1330 return 0;
1331 }
Bob Wilson21870412009-11-22 04:24:42 +00001332
Chris Lattnerf4601652007-11-22 20:49:04 +00001333 // Eat the '}'.
1334 if (Lex.getCode() != tgtok::r_brace) {
1335 TokError("expected '}' at end of bit range list");
1336 return 0;
1337 }
1338 Lex.Lex();
1339 break;
1340 }
1341 case tgtok::l_square: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001342 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001343 Lex.Lex(); // eat the '['
1344 std::vector<unsigned> Ranges = ParseRangeList();
1345 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001346
Chris Lattnerf4601652007-11-22 20:49:04 +00001347 Result = Result->convertInitListSlice(Ranges);
1348 if (Result == 0) {
1349 Error(SquareLoc, "Invalid range for list slice");
1350 return 0;
1351 }
Bob Wilson21870412009-11-22 04:24:42 +00001352
Chris Lattnerf4601652007-11-22 20:49:04 +00001353 // Eat the ']'.
1354 if (Lex.getCode() != tgtok::r_square) {
1355 TokError("expected ']' at end of list slice");
1356 return 0;
1357 }
1358 Lex.Lex();
1359 break;
1360 }
1361 case tgtok::period:
1362 if (Lex.Lex() != tgtok::Id) { // eat the .
1363 TokError("expected field identifier after '.'");
1364 return 0;
1365 }
1366 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001367 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00001368 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +00001369 return 0;
1370 }
David Greenedcd35c72011-07-29 19:07:07 +00001371 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001372 Lex.Lex(); // eat field name
1373 break;
1374 }
1375 }
1376}
1377
1378/// ParseDagArgList - Parse the argument list for a dag literal expression.
1379///
1380/// ParseDagArgList ::= Value (':' VARNAME)?
1381/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
David Greene05bce0b2011-07-29 22:43:06 +00001382std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4601652007-11-22 20:49:04 +00001383TGParser::ParseDagArgList(Record *CurRec) {
David Greene05bce0b2011-07-29 22:43:06 +00001384 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson21870412009-11-22 04:24:42 +00001385
Chris Lattnerf4601652007-11-22 20:49:04 +00001386 while (1) {
David Greene05bce0b2011-07-29 22:43:06 +00001387 Init *Val = ParseValue(CurRec);
1388 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
Bob Wilson21870412009-11-22 04:24:42 +00001389
Chris Lattnerf4601652007-11-22 20:49:04 +00001390 // If the variable name is present, add it.
1391 std::string VarName;
1392 if (Lex.getCode() == tgtok::colon) {
1393 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1394 TokError("expected variable name in dag literal");
David Greene05bce0b2011-07-29 22:43:06 +00001395 return std::vector<std::pair<llvm::Init*, std::string> >();
Chris Lattnerf4601652007-11-22 20:49:04 +00001396 }
1397 VarName = Lex.getCurStrVal();
1398 Lex.Lex(); // eat the VarName.
1399 }
Bob Wilson21870412009-11-22 04:24:42 +00001400
Chris Lattnerf4601652007-11-22 20:49:04 +00001401 Result.push_back(std::make_pair(Val, VarName));
Bob Wilson21870412009-11-22 04:24:42 +00001402
Chris Lattnerf4601652007-11-22 20:49:04 +00001403 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson21870412009-11-22 04:24:42 +00001404 Lex.Lex(); // eat the ','
Chris Lattnerf4601652007-11-22 20:49:04 +00001405 }
Bob Wilson21870412009-11-22 04:24:42 +00001406
Chris Lattnerf4601652007-11-22 20:49:04 +00001407 return Result;
1408}
1409
1410
1411/// ParseValueList - Parse a comma separated list of values, returning them as a
1412/// vector. Note that this always expects to be able to parse at least one
1413/// value. It returns an empty list if this is not possible.
1414///
1415/// ValueList ::= Value (',' Value)
1416///
David Greene05bce0b2011-07-29 22:43:06 +00001417std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopherd568b3f2011-07-11 23:06:52 +00001418 RecTy *EltTy) {
David Greene05bce0b2011-07-29 22:43:06 +00001419 std::vector<Init*> Result;
David Greenee1b46912009-06-08 20:23:18 +00001420 RecTy *ItemType = EltTy;
David Greene67acdf22009-06-29 19:59:52 +00001421 unsigned int ArgN = 0;
David Greenee1b46912009-06-08 20:23:18 +00001422 if (ArgsRec != 0 && EltTy == 0) {
1423 const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs();
1424 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greened9746fe2011-09-19 18:26:07 +00001425 if (!RV) {
1426 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1427 << ")\n";
1428 }
David Greenee1b46912009-06-08 20:23:18 +00001429 assert(RV && "Template argument record not found??");
1430 ItemType = RV->getType();
1431 ++ArgN;
1432 }
1433 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001434 if (Result.back() == 0) return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001435
Chris Lattnerf4601652007-11-22 20:49:04 +00001436 while (Lex.getCode() == tgtok::comma) {
1437 Lex.Lex(); // Eat the comma
Bob Wilson21870412009-11-22 04:24:42 +00001438
David Greenee1b46912009-06-08 20:23:18 +00001439 if (ArgsRec != 0 && EltTy == 0) {
1440 const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs();
David Greene67acdf22009-06-29 19:59:52 +00001441 if (ArgN >= TArgs.size()) {
1442 TokError("too many template arguments");
David Greene05bce0b2011-07-29 22:43:06 +00001443 return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001444 }
David Greenee1b46912009-06-08 20:23:18 +00001445 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1446 assert(RV && "Template argument record not found??");
1447 ItemType = RV->getType();
1448 ++ArgN;
1449 }
1450 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001451 if (Result.back() == 0) return std::vector<Init*>();
Chris Lattnerf4601652007-11-22 20:49:04 +00001452 }
Bob Wilson21870412009-11-22 04:24:42 +00001453
Chris Lattnerf4601652007-11-22 20:49:04 +00001454 return Result;
1455}
1456
1457
Chris Lattnerf4601652007-11-22 20:49:04 +00001458/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1459/// empty string on error. This can happen in a number of different context's,
1460/// including within a def or in the template args for a def (which which case
1461/// CurRec will be non-null) and within the template args for a multiclass (in
1462/// which case CurRec will be null, but CurMultiClass will be set). This can
1463/// also happen within a def that is within a multiclass, which will set both
1464/// CurRec and CurMultiClass.
1465///
1466/// Declaration ::= FIELD? Type ID ('=' Value)?
1467///
Bob Wilson21870412009-11-22 04:24:42 +00001468std::string TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4601652007-11-22 20:49:04 +00001469 bool ParsingTemplateArgs) {
1470 // Read the field prefix if present.
1471 bool HasField = Lex.getCode() == tgtok::Field;
1472 if (HasField) Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001473
Chris Lattnerf4601652007-11-22 20:49:04 +00001474 RecTy *Type = ParseType();
1475 if (Type == 0) return "";
Bob Wilson21870412009-11-22 04:24:42 +00001476
Chris Lattnerf4601652007-11-22 20:49:04 +00001477 if (Lex.getCode() != tgtok::Id) {
1478 TokError("Expected identifier in declaration");
1479 return "";
1480 }
Bob Wilson21870412009-11-22 04:24:42 +00001481
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001482 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001483 std::string DeclName = Lex.getCurStrVal();
1484 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001485
Chris Lattnerf4601652007-11-22 20:49:04 +00001486 if (ParsingTemplateArgs) {
1487 if (CurRec) {
1488 DeclName = CurRec->getName() + ":" + DeclName;
1489 } else {
1490 assert(CurMultiClass);
1491 }
1492 if (CurMultiClass)
1493 DeclName = CurMultiClass->Rec.getName() + "::" + DeclName;
1494 }
Bob Wilson21870412009-11-22 04:24:42 +00001495
Chris Lattnerf4601652007-11-22 20:49:04 +00001496 // Add the value.
1497 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
1498 return "";
Bob Wilson21870412009-11-22 04:24:42 +00001499
Chris Lattnerf4601652007-11-22 20:49:04 +00001500 // If a value is present, parse it.
1501 if (Lex.getCode() == tgtok::equal) {
1502 Lex.Lex();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001503 SMLoc ValLoc = Lex.getLoc();
David Greene05bce0b2011-07-29 22:43:06 +00001504 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001505 if (Val == 0 ||
1506 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
1507 return "";
1508 }
Bob Wilson21870412009-11-22 04:24:42 +00001509
Chris Lattnerf4601652007-11-22 20:49:04 +00001510 return DeclName;
1511}
1512
1513/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1514/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1515/// template args for a def, which may or may not be in a multiclass. If null,
1516/// these are the template args for a multiclass.
1517///
1518/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson21870412009-11-22 04:24:42 +00001519///
Chris Lattnerf4601652007-11-22 20:49:04 +00001520bool TGParser::ParseTemplateArgList(Record *CurRec) {
1521 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1522 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +00001523
Chris Lattnerf4601652007-11-22 20:49:04 +00001524 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +00001525
Chris Lattnerf4601652007-11-22 20:49:04 +00001526 // Read the first declaration.
1527 std::string TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1528 if (TemplArg.empty())
1529 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001530
Chris Lattnerf4601652007-11-22 20:49:04 +00001531 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson21870412009-11-22 04:24:42 +00001532
Chris Lattnerf4601652007-11-22 20:49:04 +00001533 while (Lex.getCode() == tgtok::comma) {
1534 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001535
Chris Lattnerf4601652007-11-22 20:49:04 +00001536 // Read the following declarations.
1537 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1538 if (TemplArg.empty())
1539 return true;
1540 TheRecToAddTo->addTemplateArg(TemplArg);
1541 }
Bob Wilson21870412009-11-22 04:24:42 +00001542
Chris Lattnerf4601652007-11-22 20:49:04 +00001543 if (Lex.getCode() != tgtok::greater)
1544 return TokError("expected '>' at end of template argument list");
1545 Lex.Lex(); // eat the '>'.
1546 return false;
1547}
1548
1549
1550/// ParseBodyItem - Parse a single item at within the body of a def or class.
1551///
1552/// BodyItem ::= Declaration ';'
1553/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1554bool TGParser::ParseBodyItem(Record *CurRec) {
1555 if (Lex.getCode() != tgtok::Let) {
Bob Wilson21870412009-11-22 04:24:42 +00001556 if (ParseDeclaration(CurRec, false).empty())
Chris Lattnerf4601652007-11-22 20:49:04 +00001557 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001558
Chris Lattnerf4601652007-11-22 20:49:04 +00001559 if (Lex.getCode() != tgtok::semi)
1560 return TokError("expected ';' after declaration");
1561 Lex.Lex();
1562 return false;
1563 }
1564
1565 // LET ID OptionalRangeList '=' Value ';'
1566 if (Lex.Lex() != tgtok::Id)
1567 return TokError("expected field identifier after let");
Bob Wilson21870412009-11-22 04:24:42 +00001568
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001569 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001570 std::string FieldName = Lex.getCurStrVal();
1571 Lex.Lex(); // eat the field name.
Bob Wilson21870412009-11-22 04:24:42 +00001572
Chris Lattnerf4601652007-11-22 20:49:04 +00001573 std::vector<unsigned> BitList;
Bob Wilson21870412009-11-22 04:24:42 +00001574 if (ParseOptionalBitList(BitList))
Chris Lattnerf4601652007-11-22 20:49:04 +00001575 return true;
1576 std::reverse(BitList.begin(), BitList.end());
Bob Wilson21870412009-11-22 04:24:42 +00001577
Chris Lattnerf4601652007-11-22 20:49:04 +00001578 if (Lex.getCode() != tgtok::equal)
1579 return TokError("expected '=' in let expression");
1580 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001581
David Greenee1b46912009-06-08 20:23:18 +00001582 RecordVal *Field = CurRec->getValue(FieldName);
1583 if (Field == 0)
1584 return TokError("Value '" + FieldName + "' unknown!");
1585
1586 RecTy *Type = Field->getType();
Bob Wilson21870412009-11-22 04:24:42 +00001587
David Greene05bce0b2011-07-29 22:43:06 +00001588 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001589 if (Val == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001590
Chris Lattnerf4601652007-11-22 20:49:04 +00001591 if (Lex.getCode() != tgtok::semi)
1592 return TokError("expected ';' after let expression");
1593 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001594
Chris Lattnerf4601652007-11-22 20:49:04 +00001595 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1596}
1597
1598/// ParseBody - Read the body of a class or def. Return true on error, false on
1599/// success.
1600///
1601/// Body ::= ';'
1602/// Body ::= '{' BodyList '}'
1603/// BodyList BodyItem*
1604///
1605bool TGParser::ParseBody(Record *CurRec) {
1606 // If this is a null definition, just eat the semi and return.
1607 if (Lex.getCode() == tgtok::semi) {
1608 Lex.Lex();
1609 return false;
1610 }
Bob Wilson21870412009-11-22 04:24:42 +00001611
Chris Lattnerf4601652007-11-22 20:49:04 +00001612 if (Lex.getCode() != tgtok::l_brace)
1613 return TokError("Expected ';' or '{' to start body");
1614 // Eat the '{'.
1615 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001616
Chris Lattnerf4601652007-11-22 20:49:04 +00001617 while (Lex.getCode() != tgtok::r_brace)
1618 if (ParseBodyItem(CurRec))
1619 return true;
1620
1621 // Eat the '}'.
1622 Lex.Lex();
1623 return false;
1624}
1625
1626/// ParseObjectBody - Parse the body of a def or class. This consists of an
1627/// optional ClassList followed by a Body. CurRec is the current def or class
1628/// that is being parsed.
1629///
1630/// ObjectBody ::= BaseClassList Body
1631/// BaseClassList ::= /*empty*/
1632/// BaseClassList ::= ':' BaseClassListNE
1633/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1634///
1635bool TGParser::ParseObjectBody(Record *CurRec) {
1636 // If there is a baseclass list, read it.
1637 if (Lex.getCode() == tgtok::colon) {
1638 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001639
Chris Lattnerf4601652007-11-22 20:49:04 +00001640 // Read all of the subclasses.
1641 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1642 while (1) {
1643 // Check for error.
1644 if (SubClass.Rec == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001645
Chris Lattnerf4601652007-11-22 20:49:04 +00001646 // Add it.
1647 if (AddSubClass(CurRec, SubClass))
1648 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001649
Chris Lattnerf4601652007-11-22 20:49:04 +00001650 if (Lex.getCode() != tgtok::comma) break;
1651 Lex.Lex(); // eat ','.
1652 SubClass = ParseSubClassReference(CurRec, false);
1653 }
1654 }
1655
1656 // Process any variables on the let stack.
1657 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1658 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1659 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1660 LetStack[i][j].Bits, LetStack[i][j].Value))
1661 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001662
Chris Lattnerf4601652007-11-22 20:49:04 +00001663 return ParseBody(CurRec);
1664}
1665
Chris Lattnerf4601652007-11-22 20:49:04 +00001666/// ParseDef - Parse and return a top level or multiclass def, return the record
1667/// corresponding to it. This returns null on error.
1668///
1669/// DefInst ::= DEF ObjectName ObjectBody
1670///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001671bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001672 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001673 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson21870412009-11-22 04:24:42 +00001674 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4601652007-11-22 20:49:04 +00001675
1676 // Parse ObjectName and make a record for it.
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001677 Record *CurRec = new Record(ParseObjectName(), DefLoc, Records);
Bob Wilson21870412009-11-22 04:24:42 +00001678
Chris Lattnerf4601652007-11-22 20:49:04 +00001679 if (!CurMultiClass) {
1680 // Top-level def definition.
Bob Wilson21870412009-11-22 04:24:42 +00001681
Chris Lattnerf4601652007-11-22 20:49:04 +00001682 // Ensure redefinition doesn't happen.
1683 if (Records.getDef(CurRec->getName())) {
1684 Error(DefLoc, "def '" + CurRec->getName() + "' already defined");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001685 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001686 }
1687 Records.addDef(CurRec);
1688 } else {
1689 // Otherwise, a def inside a multiclass, add it to the multiclass.
1690 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
1691 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
1692 Error(DefLoc, "def '" + CurRec->getName() +
1693 "' already defined in this multiclass!");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001694 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001695 }
1696 CurMultiClass->DefPrototypes.push_back(CurRec);
1697 }
Bob Wilson21870412009-11-22 04:24:42 +00001698
Chris Lattnerf4601652007-11-22 20:49:04 +00001699 if (ParseObjectBody(CurRec))
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001700 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001701
Chris Lattnerf4601652007-11-22 20:49:04 +00001702 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
David Greene0d886402011-08-10 18:27:46 +00001703 // See Record::setName(). This resolve step will see any new name
1704 // for the def that might have been created when resolving
1705 // inheritance, values and arguments above.
Chris Lattnerf4601652007-11-22 20:49:04 +00001706 CurRec->resolveReferences();
Bob Wilson21870412009-11-22 04:24:42 +00001707
Chris Lattnerf4601652007-11-22 20:49:04 +00001708 // If ObjectBody has template arguments, it's an error.
1709 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001710
1711 if (CurMultiClass) {
1712 // Copy the template arguments for the multiclass into the def.
1713 const std::vector<std::string> &TArgs =
1714 CurMultiClass->Rec.getTemplateArgs();
1715
1716 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1717 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1718 assert(RV && "Template arg doesn't exist?");
1719 CurRec->addValue(*RV);
1720 }
1721 }
1722
1723 return false;
Chris Lattnerf4601652007-11-22 20:49:04 +00001724}
1725
Chris Lattnerf4601652007-11-22 20:49:04 +00001726/// ParseClass - Parse a tblgen class definition.
1727///
1728/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
1729///
1730bool TGParser::ParseClass() {
1731 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
1732 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001733
Chris Lattnerf4601652007-11-22 20:49:04 +00001734 if (Lex.getCode() != tgtok::Id)
1735 return TokError("expected class name after 'class' keyword");
Bob Wilson21870412009-11-22 04:24:42 +00001736
Chris Lattnerf4601652007-11-22 20:49:04 +00001737 Record *CurRec = Records.getClass(Lex.getCurStrVal());
1738 if (CurRec) {
1739 // If the body was previously defined, this is an error.
1740 if (!CurRec->getValues().empty() ||
1741 !CurRec->getSuperClasses().empty() ||
1742 !CurRec->getTemplateArgs().empty())
1743 return TokError("Class '" + CurRec->getName() + "' already defined");
1744 } else {
1745 // If this is the first reference to this class, create and add it.
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001746 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001747 Records.addClass(CurRec);
1748 }
1749 Lex.Lex(); // eat the name.
Bob Wilson21870412009-11-22 04:24:42 +00001750
Chris Lattnerf4601652007-11-22 20:49:04 +00001751 // If there are template args, parse them.
1752 if (Lex.getCode() == tgtok::less)
1753 if (ParseTemplateArgList(CurRec))
1754 return true;
1755
1756 // Finally, parse the object body.
1757 return ParseObjectBody(CurRec);
1758}
1759
1760/// ParseLetList - Parse a non-empty list of assignment expressions into a list
1761/// of LetRecords.
1762///
1763/// LetList ::= LetItem (',' LetItem)*
1764/// LetItem ::= ID OptionalRangeList '=' Value
1765///
1766std::vector<LetRecord> TGParser::ParseLetList() {
1767 std::vector<LetRecord> Result;
Bob Wilson21870412009-11-22 04:24:42 +00001768
Chris Lattnerf4601652007-11-22 20:49:04 +00001769 while (1) {
1770 if (Lex.getCode() != tgtok::Id) {
1771 TokError("expected identifier in let definition");
1772 return std::vector<LetRecord>();
1773 }
1774 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001775 SMLoc NameLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +00001776 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4601652007-11-22 20:49:04 +00001777
1778 // Check for an optional RangeList.
1779 std::vector<unsigned> Bits;
Bob Wilson21870412009-11-22 04:24:42 +00001780 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4601652007-11-22 20:49:04 +00001781 return std::vector<LetRecord>();
1782 std::reverse(Bits.begin(), Bits.end());
Bob Wilson21870412009-11-22 04:24:42 +00001783
Chris Lattnerf4601652007-11-22 20:49:04 +00001784 if (Lex.getCode() != tgtok::equal) {
1785 TokError("expected '=' in let expression");
1786 return std::vector<LetRecord>();
1787 }
1788 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001789
David Greene05bce0b2011-07-29 22:43:06 +00001790 Init *Val = ParseValue(0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001791 if (Val == 0) return std::vector<LetRecord>();
Bob Wilson21870412009-11-22 04:24:42 +00001792
Chris Lattnerf4601652007-11-22 20:49:04 +00001793 // Now that we have everything, add the record.
1794 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson21870412009-11-22 04:24:42 +00001795
Chris Lattnerf4601652007-11-22 20:49:04 +00001796 if (Lex.getCode() != tgtok::comma)
1797 return Result;
Bob Wilson21870412009-11-22 04:24:42 +00001798 Lex.Lex(); // eat the comma.
Chris Lattnerf4601652007-11-22 20:49:04 +00001799 }
1800}
1801
1802/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001803/// different related productions. This works inside multiclasses too.
Chris Lattnerf4601652007-11-22 20:49:04 +00001804///
1805/// Object ::= LET LetList IN '{' ObjectList '}'
1806/// Object ::= LET LetList IN Object
1807///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001808bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001809 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
1810 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001811
Chris Lattnerf4601652007-11-22 20:49:04 +00001812 // Add this entry to the let stack.
1813 std::vector<LetRecord> LetInfo = ParseLetList();
1814 if (LetInfo.empty()) return true;
1815 LetStack.push_back(LetInfo);
1816
1817 if (Lex.getCode() != tgtok::In)
1818 return TokError("expected 'in' at end of top-level 'let'");
1819 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001820
Chris Lattnerf4601652007-11-22 20:49:04 +00001821 // If this is a scalar let, just handle it now
1822 if (Lex.getCode() != tgtok::l_brace) {
1823 // LET LetList IN Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001824 if (ParseObject(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00001825 return true;
1826 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001827 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001828 // Otherwise, this is a group let.
1829 Lex.Lex(); // eat the '{'.
Bob Wilson21870412009-11-22 04:24:42 +00001830
Chris Lattnerf4601652007-11-22 20:49:04 +00001831 // Parse the object list.
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001832 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00001833 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001834
Chris Lattnerf4601652007-11-22 20:49:04 +00001835 if (Lex.getCode() != tgtok::r_brace) {
1836 TokError("expected '}' at end of top level let command");
1837 return Error(BraceLoc, "to match this '{'");
1838 }
1839 Lex.Lex();
1840 }
Bob Wilson21870412009-11-22 04:24:42 +00001841
Chris Lattnerf4601652007-11-22 20:49:04 +00001842 // Outside this let scope, this let block is not active.
1843 LetStack.pop_back();
1844 return false;
1845}
1846
Chris Lattnerf4601652007-11-22 20:49:04 +00001847/// ParseMultiClass - Parse a multiclass definition.
1848///
Bob Wilson32558652009-04-28 19:41:44 +00001849/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
1850/// ':' BaseMultiClassList '{' MultiClassDef+ '}'
Chris Lattnerf4601652007-11-22 20:49:04 +00001851///
1852bool TGParser::ParseMultiClass() {
1853 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
1854 Lex.Lex(); // Eat the multiclass token.
1855
1856 if (Lex.getCode() != tgtok::Id)
1857 return TokError("expected identifier after multiclass for name");
1858 std::string Name = Lex.getCurStrVal();
Bob Wilson21870412009-11-22 04:24:42 +00001859
Chris Lattnerf4601652007-11-22 20:49:04 +00001860 if (MultiClasses.count(Name))
1861 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson21870412009-11-22 04:24:42 +00001862
Chris Lattner67db8832010-12-13 00:23:57 +00001863 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
1864 Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001865 Lex.Lex(); // Eat the identifier.
Bob Wilson21870412009-11-22 04:24:42 +00001866
Chris Lattnerf4601652007-11-22 20:49:04 +00001867 // If there are template args, parse them.
1868 if (Lex.getCode() == tgtok::less)
1869 if (ParseTemplateArgList(0))
1870 return true;
1871
David Greened34a73b2009-04-24 16:55:41 +00001872 bool inherits = false;
1873
David Greenede444af2009-04-22 16:42:54 +00001874 // If there are submulticlasses, parse them.
1875 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00001876 inherits = true;
1877
David Greenede444af2009-04-22 16:42:54 +00001878 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00001879
David Greenede444af2009-04-22 16:42:54 +00001880 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00001881 SubMultiClassReference SubMultiClass =
1882 ParseSubMultiClassReference(CurMultiClass);
David Greenede444af2009-04-22 16:42:54 +00001883 while (1) {
1884 // Check for error.
1885 if (SubMultiClass.MC == 0) return true;
Bob Wilson32558652009-04-28 19:41:44 +00001886
David Greenede444af2009-04-22 16:42:54 +00001887 // Add it.
1888 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
1889 return true;
Bob Wilson32558652009-04-28 19:41:44 +00001890
David Greenede444af2009-04-22 16:42:54 +00001891 if (Lex.getCode() != tgtok::comma) break;
1892 Lex.Lex(); // eat ','.
1893 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
1894 }
1895 }
1896
David Greened34a73b2009-04-24 16:55:41 +00001897 if (Lex.getCode() != tgtok::l_brace) {
1898 if (!inherits)
1899 return TokError("expected '{' in multiclass definition");
Bob Wilson21870412009-11-22 04:24:42 +00001900 else if (Lex.getCode() != tgtok::semi)
1901 return TokError("expected ';' in multiclass definition");
David Greened34a73b2009-04-24 16:55:41 +00001902 else
Bob Wilson21870412009-11-22 04:24:42 +00001903 Lex.Lex(); // eat the ';'.
1904 } else {
David Greened34a73b2009-04-24 16:55:41 +00001905 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
1906 return TokError("multiclass must contain at least one def");
Bob Wilson21870412009-11-22 04:24:42 +00001907
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001908 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001909 switch (Lex.getCode()) {
1910 default:
David Greenea1b1b792011-10-07 18:25:05 +00001911 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001912 case tgtok::Let:
1913 case tgtok::Def:
1914 case tgtok::Defm:
1915 if (ParseObject(CurMultiClass))
1916 return true;
1917 break;
1918 }
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001919 }
David Greened34a73b2009-04-24 16:55:41 +00001920 Lex.Lex(); // eat the '}'.
1921 }
Bob Wilson21870412009-11-22 04:24:42 +00001922
Chris Lattnerf4601652007-11-22 20:49:04 +00001923 CurMultiClass = 0;
1924 return false;
1925}
1926
David Greenee499a2d2011-10-05 22:42:07 +00001927Record *TGParser::
1928InstantiateMulticlassDef(MultiClass &MC,
1929 Record *DefProto,
1930 const std::string &DefmPrefix,
1931 SMLoc DefmPrefixLoc) {
1932 // Add in the defm name. If the defm prefix is empty, give each
1933 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
1934 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
1935 // as a prefix.
1936 std::string DefName = DefProto->getName();
1937 if (DefmPrefix.empty()) {
1938 DefName = GetNewAnonymousName();
1939 } else {
1940 std::string::size_type idx = DefName.find("#NAME#");
1941 if (idx != std::string::npos) {
1942 DefName.replace(idx, 6, DefmPrefix);
1943 } else {
1944 // Add the suffix to the defm name to get the new name.
1945 DefName = DefmPrefix + DefName;
1946 }
1947 }
1948
1949 Record *CurRec = new Record(DefName, DefmPrefixLoc, Records);
1950
1951 SubClassReference Ref;
1952 Ref.RefLoc = DefmPrefixLoc;
1953 Ref.Rec = DefProto;
1954 AddSubClass(CurRec, Ref);
1955
1956 return CurRec;
1957}
1958
1959bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
1960 Record *CurRec,
1961 SMLoc DefmPrefixLoc,
1962 SMLoc SubClassLoc,
1963 const std::vector<std::string> &TArgs,
1964 std::vector<Init *> &TemplateVals,
1965 bool DeleteArgs) {
1966 // Loop over all of the template arguments, setting them to the specified
1967 // value or leaving them as the default if necessary.
1968 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1969 // Check if a value is specified for this temp-arg.
1970 if (i < TemplateVals.size()) {
1971 // Set it now.
1972 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
1973 TemplateVals[i]))
1974 return true;
1975
1976 // Resolve it next.
1977 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
1978
1979 if (DeleteArgs)
1980 // Now remove it.
1981 CurRec->removeValue(TArgs[i]);
1982
1983 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
1984 return Error(SubClassLoc, "value not specified for template argument #"+
1985 utostr(i) + " (" + TArgs[i] + ") of multiclassclass '" +
1986 MC.Rec.getName() + "'");
1987 }
1988 }
1989 return false;
1990}
1991
1992bool TGParser::ResolveMulticlassDef(MultiClass &MC,
1993 Record *CurRec,
1994 Record *DefProto,
1995 SMLoc DefmPrefixLoc) {
1996 // If the mdef is inside a 'let' expression, add to each def.
1997 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1998 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1999 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2000 LetStack[i][j].Bits, LetStack[i][j].Value))
2001 return Error(DefmPrefixLoc, "when instantiating this defm");
2002
2003 // Ensure redefinition doesn't happen.
2004 if (Records.getDef(CurRec->getName()))
2005 return Error(DefmPrefixLoc, "def '" + CurRec->getName() +
2006 "' already defined, instantiating defm with subdef '" +
2007 DefProto->getName() + "'");
2008
2009 // Don't create a top level definition for defm inside multiclasses,
2010 // instead, only update the prototypes and bind the template args
2011 // with the new created definition.
2012 if (CurMultiClass) {
2013 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2014 i != e; ++i)
2015 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName())
2016 return Error(DefmPrefixLoc, "defm '" + CurRec->getName() +
2017 "' already defined in this multiclass!");
2018 CurMultiClass->DefPrototypes.push_back(CurRec);
2019
2020 // Copy the template arguments for the multiclass into the new def.
2021 const std::vector<std::string> &TA =
2022 CurMultiClass->Rec.getTemplateArgs();
2023
2024 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2025 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2026 assert(RV && "Template arg doesn't exist?");
2027 CurRec->addValue(*RV);
2028 }
2029 } else {
2030 Records.addDef(CurRec);
2031 }
2032
2033 return false;
2034}
2035
Chris Lattnerf4601652007-11-22 20:49:04 +00002036/// ParseDefm - Parse the instantiation of a multiclass.
2037///
2038/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2039///
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002040bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002041 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Bob Wilson21870412009-11-22 04:24:42 +00002042
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002043 std::string DefmPrefix;
2044 if (Lex.Lex() == tgtok::Id) { // eat the defm.
2045 DefmPrefix = Lex.getCurStrVal();
2046 Lex.Lex(); // Eat the defm prefix.
2047 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +00002048
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002049 SMLoc DefmPrefixLoc = Lex.getLoc();
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002050 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4601652007-11-22 20:49:04 +00002051 return TokError("expected ':' after defm identifier");
Bob Wilson21870412009-11-22 04:24:42 +00002052
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002053 // Keep track of the new generated record definitions.
2054 std::vector<Record*> NewRecDefs;
2055
2056 // This record also inherits from a regular class (non-multiclass)?
2057 bool InheritFromClass = false;
2058
Chris Lattnerf4601652007-11-22 20:49:04 +00002059 // eat the colon.
2060 Lex.Lex();
2061
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002062 SMLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002063 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene56546132009-04-22 22:17:51 +00002064
2065 while (1) {
2066 if (Ref.Rec == 0) return true;
2067
2068 // To instantiate a multiclass, we need to first get the multiclass, then
2069 // instantiate each def contained in the multiclass with the SubClassRef
2070 // template parameters.
2071 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2072 assert(MC && "Didn't lookup multiclass correctly?");
David Greene05bce0b2011-07-29 22:43:06 +00002073 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greene56546132009-04-22 22:17:51 +00002074
2075 // Verify that the correct number of template arguments were specified.
2076 const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs();
2077 if (TArgs.size() < TemplateVals.size())
2078 return Error(SubClassLoc,
2079 "more template args specified than multiclass expects");
2080
2081 // Loop over all the def's in the multiclass, instantiating each one.
2082 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2083 Record *DefProto = MC->DefPrototypes[i];
2084
David Greenee499a2d2011-10-05 22:42:07 +00002085 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix, DefmPrefixLoc);
David Greene065f2592009-05-05 16:28:25 +00002086
David Greenee499a2d2011-10-05 22:42:07 +00002087 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmPrefixLoc, SubClassLoc,
2088 TArgs, TemplateVals, true/*Delete args*/))
2089 return Error(SubClassLoc, "could not instantiate def");
David Greene56546132009-04-22 22:17:51 +00002090
David Greenee499a2d2011-10-05 22:42:07 +00002091 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmPrefixLoc))
2092 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002093
2094 NewRecDefs.push_back(CurRec);
David Greene56546132009-04-22 22:17:51 +00002095 }
2096
David Greenee499a2d2011-10-05 22:42:07 +00002097
David Greene56546132009-04-22 22:17:51 +00002098 if (Lex.getCode() != tgtok::comma) break;
2099 Lex.Lex(); // eat ','.
2100
2101 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002102
2103 // A defm can inherit from regular classes (non-multiclass) as
2104 // long as they come in the end of the inheritance list.
2105 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0);
2106
2107 if (InheritFromClass)
2108 break;
2109
David Greene56546132009-04-22 22:17:51 +00002110 Ref = ParseSubClassReference(0, true);
2111 }
2112
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002113 if (InheritFromClass) {
2114 // Process all the classes to inherit as if they were part of a
2115 // regular 'def' and inherit all record values.
2116 SubClassReference SubClass = ParseSubClassReference(0, false);
2117 while (1) {
2118 // Check for error.
2119 if (SubClass.Rec == 0) return true;
2120
2121 // Get the expanded definition prototypes and teach them about
2122 // the record values the current class to inherit has
2123 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2124 Record *CurRec = NewRecDefs[i];
2125
2126 // Add it.
2127 if (AddSubClass(CurRec, SubClass))
2128 return true;
2129
2130 // Process any variables on the let stack.
2131 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2132 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2133 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2134 LetStack[i][j].Bits, LetStack[i][j].Value))
2135 return true;
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002136 }
2137
2138 if (Lex.getCode() != tgtok::comma) break;
2139 Lex.Lex(); // eat ','.
2140 SubClass = ParseSubClassReference(0, false);
2141 }
2142 }
2143
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002144 if (!CurMultiClass)
2145 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene0d886402011-08-10 18:27:46 +00002146 // See Record::setName(). This resolve step will see any new
2147 // name for the def that might have been created when resolving
2148 // inheritance, values and arguments above.
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002149 NewRecDefs[i]->resolveReferences();
2150
Chris Lattnerf4601652007-11-22 20:49:04 +00002151 if (Lex.getCode() != tgtok::semi)
2152 return TokError("expected ';' at end of defm");
2153 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002154
Chris Lattnerf4601652007-11-22 20:49:04 +00002155 return false;
2156}
2157
2158/// ParseObject
2159/// Object ::= ClassInst
2160/// Object ::= DefInst
2161/// Object ::= MultiClassInst
2162/// Object ::= DefMInst
2163/// Object ::= LETCommand '{' ObjectList '}'
2164/// Object ::= LETCommand Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002165bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002166 switch (Lex.getCode()) {
Chris Lattnerd6d9dd92010-10-31 19:27:15 +00002167 default:
2168 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002169 case tgtok::Let: return ParseTopLevelLet(MC);
2170 case tgtok::Def: return ParseDef(MC);
2171 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4601652007-11-22 20:49:04 +00002172 case tgtok::Class: return ParseClass();
2173 case tgtok::MultiClass: return ParseMultiClass();
2174 }
2175}
2176
2177/// ParseObjectList
2178/// ObjectList :== Object*
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002179bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002180 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002181 if (ParseObject(MC))
Chris Lattnerf4601652007-11-22 20:49:04 +00002182 return true;
2183 }
2184 return false;
2185}
2186
Chris Lattnerf4601652007-11-22 20:49:04 +00002187bool TGParser::ParseFile() {
2188 Lex.Lex(); // Prime the lexer.
2189 if (ParseObjectList()) return true;
Bob Wilson21870412009-11-22 04:24:42 +00002190
Chris Lattnerf4601652007-11-22 20:49:04 +00002191 // If we have unread input at the end of the file, report it.
2192 if (Lex.getCode() == tgtok::Eof)
2193 return false;
Bob Wilson21870412009-11-22 04:24:42 +00002194
Chris Lattnerf4601652007-11-22 20:49:04 +00002195 return TokError("Unexpected input at top level");
2196}
2197