blob: d864b4f69d8ba79b806cb5d313bbfc024288e16b [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
David Greenee22b3212011-10-19 13:02:42 +0000157 const std::vector<Init *> &TArgs = SC->getTemplateArgs();
Chris Lattnerf4601652007-11-22 20:49:04 +0000158
159 // Ensure that an appropriate number of template arguments are specified.
160 if (TArgs.size() < SubClass.TemplateArgs.size())
161 return Error(SubClass.RefLoc, "More template args specified than expected");
Bob Wilson21870412009-11-22 04:24:42 +0000162
Chris Lattnerf4601652007-11-22 20:49:04 +0000163 // Loop over all of the template arguments, setting them to the specified
164 // value or leaving them as the default if necessary.
165 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
166 if (i < SubClass.TemplateArgs.size()) {
167 // If a value is specified for this template arg, set it now.
Bob Wilson21870412009-11-22 04:24:42 +0000168 if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(),
Chris Lattnerf4601652007-11-22 20:49:04 +0000169 SubClass.TemplateArgs[i]))
170 return true;
Bob Wilson21870412009-11-22 04:24:42 +0000171
Chris Lattnerf4601652007-11-22 20:49:04 +0000172 // Resolve it next.
173 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson21870412009-11-22 04:24:42 +0000174
Chris Lattnerf4601652007-11-22 20:49:04 +0000175 // Now remove it.
176 CurRec->removeValue(TArgs[i]);
177
178 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
179 return Error(SubClass.RefLoc,"Value not specified for template argument #"
David Greenee22b3212011-10-19 13:02:42 +0000180 + utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
181 + ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4601652007-11-22 20:49:04 +0000182 }
183 }
184
185 // Since everything went well, we can now set the "superclass" list for the
186 // current record.
187 const std::vector<Record*> &SCs = SC->getSuperClasses();
188 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
189 if (CurRec->isSubClassOf(SCs[i]))
190 return Error(SubClass.RefLoc,
191 "Already subclass of '" + SCs[i]->getName() + "'!\n");
192 CurRec->addSuperClass(SCs[i]);
193 }
Bob Wilson21870412009-11-22 04:24:42 +0000194
Chris Lattnerf4601652007-11-22 20:49:04 +0000195 if (CurRec->isSubClassOf(SC))
196 return Error(SubClass.RefLoc,
197 "Already subclass of '" + SC->getName() + "'!\n");
198 CurRec->addSuperClass(SC);
199 return false;
200}
201
David Greenede444af2009-04-22 16:42:54 +0000202/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilson440548d2009-04-30 18:26:19 +0000203/// CurMC, resolving its template args as SubMultiClass's
David Greenede444af2009-04-22 16:42:54 +0000204/// template arguments.
Bob Wilson440548d2009-04-30 18:26:19 +0000205bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson1d512df2009-04-30 17:46:20 +0000206 SubMultiClassReference &SubMultiClass) {
David Greenede444af2009-04-22 16:42:54 +0000207 MultiClass *SMC = SubMultiClass.MC;
Bob Wilson440548d2009-04-30 18:26:19 +0000208 Record *CurRec = &CurMC->Rec;
David Greenede444af2009-04-22 16:42:54 +0000209
Bob Wilson440548d2009-04-30 18:26:19 +0000210 const std::vector<RecordVal> &MCVals = CurRec->getValues();
David Greenede444af2009-04-22 16:42:54 +0000211
212 // Add all of the values in the subclass into the current class.
213 const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues();
214 for (unsigned i = 0, e = SMCVals.size(); i != e; ++i)
215 if (AddValue(CurRec, SubMultiClass.RefLoc, SMCVals[i]))
216 return true;
217
Bob Wilson440548d2009-04-30 18:26:19 +0000218 int newDefStart = CurMC->DefPrototypes.size();
David Greened34a73b2009-04-24 16:55:41 +0000219
David Greenede444af2009-04-22 16:42:54 +0000220 // Add all of the defs in the subclass into the current multiclass.
221 for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(),
222 iend = SMC->DefPrototypes.end();
223 i != iend;
224 ++i) {
225 // Clone the def and add it to the current multiclass
226 Record *NewDef = new Record(**i);
227
228 // Add all of the values in the superclass into the current def.
229 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
230 if (AddValue(NewDef, SubMultiClass.RefLoc, MCVals[i]))
231 return true;
232
Bob Wilson440548d2009-04-30 18:26:19 +0000233 CurMC->DefPrototypes.push_back(NewDef);
David Greenede444af2009-04-22 16:42:54 +0000234 }
Bob Wilson32558652009-04-28 19:41:44 +0000235
David Greenee22b3212011-10-19 13:02:42 +0000236 const std::vector<Init *> &SMCTArgs = SMC->Rec.getTemplateArgs();
David Greenede444af2009-04-22 16:42:54 +0000237
David Greened34a73b2009-04-24 16:55:41 +0000238 // Ensure that an appropriate number of template arguments are
239 // specified.
David Greenede444af2009-04-22 16:42:54 +0000240 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
David Greened34a73b2009-04-24 16:55:41 +0000241 return Error(SubMultiClass.RefLoc,
242 "More template args specified than expected");
Bob Wilson32558652009-04-28 19:41:44 +0000243
David Greenede444af2009-04-22 16:42:54 +0000244 // Loop over all of the template arguments, setting them to the specified
245 // value or leaving them as the default if necessary.
246 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
247 if (i < SubMultiClass.TemplateArgs.size()) {
David Greened34a73b2009-04-24 16:55:41 +0000248 // If a value is specified for this template arg, set it in the
249 // superclass now.
250 if (SetValue(CurRec, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000251 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000252 SubMultiClass.TemplateArgs[i]))
253 return true;
254
255 // Resolve it next.
256 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson32558652009-04-28 19:41:44 +0000257
David Greenede444af2009-04-22 16:42:54 +0000258 // Now remove it.
259 CurRec->removeValue(SMCTArgs[i]);
260
David Greened34a73b2009-04-24 16:55:41 +0000261 // If a value is specified for this template arg, set it in the
262 // new defs now.
263 for (MultiClass::RecordVector::iterator j =
Bob Wilson440548d2009-04-30 18:26:19 +0000264 CurMC->DefPrototypes.begin() + newDefStart,
265 jend = CurMC->DefPrototypes.end();
David Greenede444af2009-04-22 16:42:54 +0000266 j != jend;
267 ++j) {
268 Record *Def = *j;
269
David Greened34a73b2009-04-24 16:55:41 +0000270 if (SetValue(Def, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000271 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000272 SubMultiClass.TemplateArgs[i]))
273 return true;
274
275 // Resolve it next.
276 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
277
278 // Now remove it
279 Def->removeValue(SMCTArgs[i]);
280 }
281 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
David Greened34a73b2009-04-24 16:55:41 +0000282 return Error(SubMultiClass.RefLoc,
283 "Value not specified for template argument #"
David Greenee22b3212011-10-19 13:02:42 +0000284 + utostr(i) + " (" + SMCTArgs[i]->getAsUnquotedString()
285 + ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greenede444af2009-04-22 16:42:54 +0000286 }
287 }
288
289 return false;
290}
291
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 Greenef3744a02011-10-19 13:04:20 +0000639Init *TGParser::ParseIDValue(Record *CurRec, IDParseMode Mode) {
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,
David Greenef3744a02011-10-19 13:04:20 +0000650 const std::string &Name, SMLoc NameLoc,
651 IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000652 if (CurRec) {
653 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenedcd35c72011-07-29 19:07:07 +0000654 return VarInit::get(Name, RV->getType());
Bob Wilson21870412009-11-22 04:24:42 +0000655
David Greenee22b3212011-10-19 13:02:42 +0000656 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
657
David Greenecaa25c82011-10-05 22:42:54 +0000658 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +0000659 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
660 "::");
David Greenecaa25c82011-10-05 22:42:54 +0000661
Chris Lattnerf4601652007-11-22 20:49:04 +0000662 if (CurRec->isTemplateArg(TemplateArgName)) {
663 const RecordVal *RV = CurRec->getValue(TemplateArgName);
664 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000665 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000666 }
667 }
Bob Wilson21870412009-11-22 04:24:42 +0000668
Chris Lattnerf4601652007-11-22 20:49:04 +0000669 if (CurMultiClass) {
David Greenee22b3212011-10-19 13:02:42 +0000670 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
671 "::");
672
Chris Lattnerf4601652007-11-22 20:49:04 +0000673 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
674 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
675 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000676 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000677 }
678 }
Bob Wilson21870412009-11-22 04:24:42 +0000679
Chris Lattnerf4601652007-11-22 20:49:04 +0000680 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000681 return DefInit::get(D);
Chris Lattnerf4601652007-11-22 20:49:04 +0000682
683 Error(NameLoc, "Variable not defined: '" + Name + "'");
684 return 0;
685}
686
David Greened418c1b2009-05-14 20:54:48 +0000687/// ParseOperation - Parse an operator. This returns null on error.
688///
689/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
690///
David Greene05bce0b2011-07-29 22:43:06 +0000691Init *TGParser::ParseOperation(Record *CurRec) {
David Greened418c1b2009-05-14 20:54:48 +0000692 switch (Lex.getCode()) {
693 default:
694 TokError("unknown operation");
695 return 0;
696 break;
David Greene1434f662011-01-07 17:05:37 +0000697 case tgtok::XHead:
698 case tgtok::XTail:
699 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +0000700 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
701 UnOpInit::UnaryOp Code;
702 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000703
David Greenee6c27de2009-05-14 21:22:49 +0000704 switch (Lex.getCode()) {
705 default: assert(0 && "Unhandled code!");
706 case tgtok::XCast:
707 Lex.Lex(); // eat the operation
708 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000709
David Greenee6c27de2009-05-14 21:22:49 +0000710 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000711
David Greenee6c27de2009-05-14 21:22:49 +0000712 if (Type == 0) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000713 TokError("did not get type for unary operator");
David Greenee6c27de2009-05-14 21:22:49 +0000714 return 0;
715 }
David Greened418c1b2009-05-14 20:54:48 +0000716
David Greenee6c27de2009-05-14 21:22:49 +0000717 break;
David Greene1434f662011-01-07 17:05:37 +0000718 case tgtok::XHead:
David Greene5f9f9ba2009-05-14 22:38:31 +0000719 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000720 Code = UnOpInit::HEAD;
David Greene5f9f9ba2009-05-14 22:38:31 +0000721 break;
David Greene1434f662011-01-07 17:05:37 +0000722 case tgtok::XTail:
David Greene5f9f9ba2009-05-14 22:38:31 +0000723 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000724 Code = UnOpInit::TAIL;
David Greene5f9f9ba2009-05-14 22:38:31 +0000725 break;
David Greene1434f662011-01-07 17:05:37 +0000726 case tgtok::XEmpty:
David Greene5f9f9ba2009-05-14 22:38:31 +0000727 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000728 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000729 Type = IntRecTy::get();
David Greene5f9f9ba2009-05-14 22:38:31 +0000730 break;
David Greenee6c27de2009-05-14 21:22:49 +0000731 }
732 if (Lex.getCode() != tgtok::l_paren) {
733 TokError("expected '(' after unary operator");
734 return 0;
735 }
736 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000737
David Greene05bce0b2011-07-29 22:43:06 +0000738 Init *LHS = ParseValue(CurRec);
David Greenee6c27de2009-05-14 21:22:49 +0000739 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000740
David Greene1434f662011-01-07 17:05:37 +0000741 if (Code == UnOpInit::HEAD
742 || Code == UnOpInit::TAIL
743 || Code == UnOpInit::EMPTY) {
David Greene05bce0b2011-07-29 22:43:06 +0000744 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
745 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
746 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000747 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
748 TokError("expected list or string type argument in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000749 return 0;
750 }
751 if (LHSt) {
752 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
David Greenee1b46912009-06-08 20:23:18 +0000753 StringRecTy *SType = dynamic_cast<StringRecTy*>(LHSt->getType());
754 if (LType == 0 && SType == 0) {
755 TokError("expected list or string type argumnet in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000756 return 0;
757 }
758 }
759
David Greene1434f662011-01-07 17:05:37 +0000760 if (Code == UnOpInit::HEAD
761 || Code == UnOpInit::TAIL) {
David Greenee1b46912009-06-08 20:23:18 +0000762 if (LHSl == 0 && LHSt == 0) {
763 TokError("expected list type argumnet in unary operator");
764 return 0;
765 }
Bob Wilson21870412009-11-22 04:24:42 +0000766
David Greene5f9f9ba2009-05-14 22:38:31 +0000767 if (LHSl && LHSl->getSize() == 0) {
768 TokError("empty list argument in unary operator");
769 return 0;
770 }
771 if (LHSl) {
David Greene05bce0b2011-07-29 22:43:06 +0000772 Init *Item = LHSl->getElement(0);
773 TypedInit *Itemt = dynamic_cast<TypedInit*>(Item);
David Greene5f9f9ba2009-05-14 22:38:31 +0000774 if (Itemt == 0) {
775 TokError("untyped list element in unary operator");
776 return 0;
777 }
David Greene1434f662011-01-07 17:05:37 +0000778 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000779 Type = Itemt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000780 } else {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000781 Type = ListRecTy::get(Itemt->getType());
David Greene5f9f9ba2009-05-14 22:38:31 +0000782 }
Bob Wilson21870412009-11-22 04:24:42 +0000783 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000784 assert(LHSt && "expected list type argument in unary operator");
785 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
786 if (LType == 0) {
787 TokError("expected list type argumnet in unary operator");
788 return 0;
789 }
David Greene1434f662011-01-07 17:05:37 +0000790 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000791 Type = LType->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +0000792 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000793 Type = LType;
794 }
795 }
796 }
797 }
798
David Greenee6c27de2009-05-14 21:22:49 +0000799 if (Lex.getCode() != tgtok::r_paren) {
800 TokError("expected ')' in unary operator");
801 return 0;
802 }
803 Lex.Lex(); // eat the ')'
David Greenedcd35c72011-07-29 19:07:07 +0000804 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee6c27de2009-05-14 21:22:49 +0000805 }
David Greened418c1b2009-05-14 20:54:48 +0000806
807 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +0000808 case tgtok::XSRA:
David Greened418c1b2009-05-14 20:54:48 +0000809 case tgtok::XSRL:
810 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +0000811 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +0000812 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000813 tgtok::TokKind OpTok = Lex.getCode();
814 SMLoc OpLoc = Lex.getLoc();
815 Lex.Lex(); // eat the operation
816
David Greened418c1b2009-05-14 20:54:48 +0000817 BinOpInit::BinaryOp Code;
818 RecTy *Type = 0;
819
Chris Lattner8d978a72010-10-05 23:58:18 +0000820 switch (OpTok) {
David Greened418c1b2009-05-14 20:54:48 +0000821 default: assert(0 && "Unhandled code!");
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000822 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
823 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
824 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
825 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
826 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Bob Wilson21870412009-11-22 04:24:42 +0000827 case tgtok::XStrConcat:
David Greened418c1b2009-05-14 20:54:48 +0000828 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000829 Type = StringRecTy::get();
David Greened418c1b2009-05-14 20:54:48 +0000830 break;
David Greened418c1b2009-05-14 20:54:48 +0000831 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000832
David Greened418c1b2009-05-14 20:54:48 +0000833 if (Lex.getCode() != tgtok::l_paren) {
834 TokError("expected '(' after binary operator");
835 return 0;
836 }
837 Lex.Lex(); // eat the '('
838
David Greene05bce0b2011-07-29 22:43:06 +0000839 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000840
Chris Lattner8d978a72010-10-05 23:58:18 +0000841 InitList.push_back(ParseValue(CurRec));
842 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000843
Chris Lattner8d978a72010-10-05 23:58:18 +0000844 while (Lex.getCode() == tgtok::comma) {
845 Lex.Lex(); // eat the ','
846
847 InitList.push_back(ParseValue(CurRec));
848 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000849 }
David Greened418c1b2009-05-14 20:54:48 +0000850
851 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner8d978a72010-10-05 23:58:18 +0000852 TokError("expected ')' in operator");
David Greened418c1b2009-05-14 20:54:48 +0000853 return 0;
854 }
855 Lex.Lex(); // eat the ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000856
857 // We allow multiple operands to associative operators like !strconcat as
858 // shorthand for nesting them.
859 if (Code == BinOpInit::STRCONCAT) {
860 while (InitList.size() > 2) {
David Greene05bce0b2011-07-29 22:43:06 +0000861 Init *RHS = InitList.pop_back_val();
David Greenedcd35c72011-07-29 19:07:07 +0000862 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
863 ->Fold(CurRec, CurMultiClass);
Chris Lattner8d978a72010-10-05 23:58:18 +0000864 InitList.back() = RHS;
865 }
866 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000867
Chris Lattner8d978a72010-10-05 23:58:18 +0000868 if (InitList.size() == 2)
David Greenedcd35c72011-07-29 19:07:07 +0000869 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner8d978a72010-10-05 23:58:18 +0000870 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000871
Chris Lattner8d978a72010-10-05 23:58:18 +0000872 Error(OpLoc, "expected two operands to operator");
873 return 0;
David Greened418c1b2009-05-14 20:54:48 +0000874 }
875
David Greene9bea7c82009-05-14 23:26:46 +0000876 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +0000877 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +0000878 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
879 TernOpInit::TernaryOp Code;
880 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000881
David Greene4afc5092009-05-14 21:54:42 +0000882 tgtok::TokKind LexCode = Lex.getCode();
883 Lex.Lex(); // eat the operation
884 switch (LexCode) {
885 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000886 case tgtok::XIf:
887 Code = TernOpInit::IF;
888 break;
David Greenebeb31a52009-05-14 22:23:47 +0000889 case tgtok::XForEach:
890 Code = TernOpInit::FOREACH;
891 break;
David Greene4afc5092009-05-14 21:54:42 +0000892 case tgtok::XSubst:
893 Code = TernOpInit::SUBST;
894 break;
895 }
896 if (Lex.getCode() != tgtok::l_paren) {
897 TokError("expected '(' after ternary operator");
898 return 0;
899 }
900 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000901
David Greene05bce0b2011-07-29 22:43:06 +0000902 Init *LHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +0000903 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000904
David Greene4afc5092009-05-14 21:54:42 +0000905 if (Lex.getCode() != tgtok::comma) {
906 TokError("expected ',' in ternary operator");
907 return 0;
908 }
909 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000910
David Greene05bce0b2011-07-29 22:43:06 +0000911 Init *MHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +0000912 if (MHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000913
David Greene4afc5092009-05-14 21:54:42 +0000914 if (Lex.getCode() != tgtok::comma) {
915 TokError("expected ',' in ternary operator");
916 return 0;
917 }
918 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000919
David Greene05bce0b2011-07-29 22:43:06 +0000920 Init *RHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +0000921 if (RHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000922
David Greene4afc5092009-05-14 21:54:42 +0000923 if (Lex.getCode() != tgtok::r_paren) {
924 TokError("expected ')' in binary operator");
925 return 0;
926 }
927 Lex.Lex(); // eat the ')'
David Greened418c1b2009-05-14 20:54:48 +0000928
David Greene4afc5092009-05-14 21:54:42 +0000929 switch (LexCode) {
930 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000931 case tgtok::XIf: {
Bill Wendling548f5a02010-12-13 01:46:19 +0000932 // FIXME: The `!if' operator doesn't handle non-TypedInit well at
933 // all. This can be made much more robust.
David Greene05bce0b2011-07-29 22:43:06 +0000934 TypedInit *MHSt = dynamic_cast<TypedInit*>(MHS);
935 TypedInit *RHSt = dynamic_cast<TypedInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +0000936
937 RecTy *MHSTy = 0;
938 RecTy *RHSTy = 0;
939
940 if (MHSt == 0 && RHSt == 0) {
David Greene05bce0b2011-07-29 22:43:06 +0000941 BitsInit *MHSbits = dynamic_cast<BitsInit*>(MHS);
942 BitsInit *RHSbits = dynamic_cast<BitsInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +0000943
944 if (MHSbits && RHSbits &&
945 MHSbits->getNumBits() == RHSbits->getNumBits()) {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000946 Type = BitRecTy::get();
Bill Wendling548f5a02010-12-13 01:46:19 +0000947 break;
948 } else {
David Greene05bce0b2011-07-29 22:43:06 +0000949 BitInit *MHSbit = dynamic_cast<BitInit*>(MHS);
950 BitInit *RHSbit = dynamic_cast<BitInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +0000951
952 if (MHSbit && RHSbit) {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000953 Type = BitRecTy::get();
Bill Wendling548f5a02010-12-13 01:46:19 +0000954 break;
955 }
956 }
957 } else if (MHSt != 0 && RHSt != 0) {
958 MHSTy = MHSt->getType();
959 RHSTy = RHSt->getType();
960 }
961
962 if (!MHSTy || !RHSTy) {
David Greene9bea7c82009-05-14 23:26:46 +0000963 TokError("could not get type for !if");
964 return 0;
965 }
Bill Wendling548f5a02010-12-13 01:46:19 +0000966
967 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
968 Type = RHSTy;
969 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
970 Type = MHSTy;
Bob Wilson21870412009-11-22 04:24:42 +0000971 } else {
David Greene9bea7c82009-05-14 23:26:46 +0000972 TokError("inconsistent types for !if");
973 return 0;
974 }
975 break;
976 }
David Greenebeb31a52009-05-14 22:23:47 +0000977 case tgtok::XForEach: {
David Greene05bce0b2011-07-29 22:43:06 +0000978 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
David Greenebeb31a52009-05-14 22:23:47 +0000979 if (MHSt == 0) {
980 TokError("could not get type for !foreach");
981 return 0;
982 }
983 Type = MHSt->getType();
984 break;
985 }
David Greene4afc5092009-05-14 21:54:42 +0000986 case tgtok::XSubst: {
David Greene05bce0b2011-07-29 22:43:06 +0000987 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
David Greene4afc5092009-05-14 21:54:42 +0000988 if (RHSt == 0) {
989 TokError("could not get type for !subst");
990 return 0;
991 }
992 Type = RHSt->getType();
993 break;
994 }
995 }
David Greenedcd35c72011-07-29 19:07:07 +0000996 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson21870412009-11-22 04:24:42 +0000997 CurMultiClass);
David Greene4afc5092009-05-14 21:54:42 +0000998 }
David Greened418c1b2009-05-14 20:54:48 +0000999 }
1000 TokError("could not parse operation");
1001 return 0;
1002}
1003
1004/// ParseOperatorType - Parse a type for an operator. This returns
1005/// null on error.
1006///
1007/// OperatorType ::= '<' Type '>'
1008///
Dan Gohmana9ad0412009-08-12 22:10:57 +00001009RecTy *TGParser::ParseOperatorType() {
David Greened418c1b2009-05-14 20:54:48 +00001010 RecTy *Type = 0;
1011
1012 if (Lex.getCode() != tgtok::less) {
1013 TokError("expected type name for operator");
1014 return 0;
1015 }
1016 Lex.Lex(); // eat the <
1017
1018 Type = ParseType();
1019
1020 if (Type == 0) {
1021 TokError("expected type name for operator");
1022 return 0;
1023 }
1024
1025 if (Lex.getCode() != tgtok::greater) {
1026 TokError("expected type name for operator");
1027 return 0;
1028 }
1029 Lex.Lex(); // eat the >
1030
1031 return Type;
1032}
1033
1034
Chris Lattnerf4601652007-11-22 20:49:04 +00001035/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1036///
1037/// SimpleValue ::= IDValue
1038/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001039/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +00001040/// SimpleValue ::= CODEFRAGMENT
1041/// SimpleValue ::= '?'
1042/// SimpleValue ::= '{' ValueList '}'
1043/// SimpleValue ::= ID '<' ValueListNE '>'
1044/// SimpleValue ::= '[' ValueList ']'
1045/// SimpleValue ::= '(' IDValue DagArgList ')'
1046/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
1047/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1048/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1049/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1050/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1051///
David Greenef3744a02011-10-19 13:04:20 +00001052Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1053 IDParseMode Mode) {
David Greene05bce0b2011-07-29 22:43:06 +00001054 Init *R = 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001055 switch (Lex.getCode()) {
1056 default: TokError("Unknown token when parsing a value"); break;
David Greenedcd35c72011-07-29 19:07:07 +00001057 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001058 case tgtok::StrVal: {
1059 std::string Val = Lex.getCurStrVal();
1060 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001061
Jim Grosbachda4231f2009-03-26 16:17:51 +00001062 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001063 while (Lex.getCode() == tgtok::StrVal) {
1064 Val += Lex.getCurStrVal();
1065 Lex.Lex();
1066 }
Bob Wilson21870412009-11-22 04:24:42 +00001067
David Greenedcd35c72011-07-29 19:07:07 +00001068 R = StringInit::get(Val);
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001069 break;
1070 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001071 case tgtok::CodeFragment:
David Greenedcd35c72011-07-29 19:07:07 +00001072 R = CodeInit::get(Lex.getCurStrVal());
Chris Lattner578bcf02010-10-06 04:31:40 +00001073 Lex.Lex();
1074 break;
1075 case tgtok::question:
David Greenedcd35c72011-07-29 19:07:07 +00001076 R = UnsetInit::get();
Chris Lattner578bcf02010-10-06 04:31:40 +00001077 Lex.Lex();
1078 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001079 case tgtok::Id: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001080 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001081 std::string Name = Lex.getCurStrVal();
1082 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greenef3744a02011-10-19 13:04:20 +00001083 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson21870412009-11-22 04:24:42 +00001084
Chris Lattnerf4601652007-11-22 20:49:04 +00001085 // Value ::= ID '<' ValueListNE '>'
1086 if (Lex.Lex() == tgtok::greater) {
1087 TokError("expected non-empty value list");
1088 return 0;
1089 }
David Greenee1b46912009-06-08 20:23:18 +00001090
Chris Lattnerf4601652007-11-22 20:49:04 +00001091 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1092 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1093 // body.
1094 Record *Class = Records.getClass(Name);
1095 if (!Class) {
1096 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1097 return 0;
1098 }
David Greenee1b46912009-06-08 20:23:18 +00001099
David Greene05bce0b2011-07-29 22:43:06 +00001100 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
David Greenee1b46912009-06-08 20:23:18 +00001101 if (ValueList.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001102
David Greenee1b46912009-06-08 20:23:18 +00001103 if (Lex.getCode() != tgtok::greater) {
1104 TokError("expected '>' at end of value list");
1105 return 0;
1106 }
1107 Lex.Lex(); // eat the '>'
Bob Wilson21870412009-11-22 04:24:42 +00001108
Chris Lattnerf4601652007-11-22 20:49:04 +00001109 // Create the new record, set it as CurRec temporarily.
1110 static unsigned AnonCounter = 0;
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001111 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),
1112 NameLoc,
1113 Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001114 SubClassReference SCRef;
1115 SCRef.RefLoc = NameLoc;
1116 SCRef.Rec = Class;
1117 SCRef.TemplateArgs = ValueList;
1118 // Add info about the subclass to NewRec.
1119 if (AddSubClass(NewRec, SCRef))
1120 return 0;
1121 NewRec->resolveReferences();
1122 Records.addDef(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001123
Chris Lattnerf4601652007-11-22 20:49:04 +00001124 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +00001125 return DefInit::get(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001126 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001127 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001128 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001129 Lex.Lex(); // eat the '{'
David Greene05bce0b2011-07-29 22:43:06 +00001130 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001131
Chris Lattnerf4601652007-11-22 20:49:04 +00001132 if (Lex.getCode() != tgtok::r_brace) {
1133 Vals = ParseValueList(CurRec);
1134 if (Vals.empty()) return 0;
1135 }
1136 if (Lex.getCode() != tgtok::r_brace) {
1137 TokError("expected '}' at end of bit list value");
1138 return 0;
1139 }
1140 Lex.Lex(); // eat the '}'
Bob Wilson21870412009-11-22 04:24:42 +00001141
David Greene05bce0b2011-07-29 22:43:06 +00001142 SmallVector<Init *, 16> NewBits(Vals.size());
David Greeneca7fd3d2011-07-29 19:07:00 +00001143
Chris Lattnerf4601652007-11-22 20:49:04 +00001144 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001145 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Chris Lattnerf4601652007-11-22 20:49:04 +00001146 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +00001147 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1148 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +00001149 return 0;
1150 }
David Greeneca7fd3d2011-07-29 19:07:00 +00001151 NewBits[Vals.size()-i-1] = Bit;
Chris Lattnerf4601652007-11-22 20:49:04 +00001152 }
David Greenedcd35c72011-07-29 19:07:07 +00001153 return BitsInit::get(NewBits);
Chris Lattnerf4601652007-11-22 20:49:04 +00001154 }
1155 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1156 Lex.Lex(); // eat the '['
David Greene05bce0b2011-07-29 22:43:06 +00001157 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001158
David Greenee1b46912009-06-08 20:23:18 +00001159 RecTy *DeducedEltTy = 0;
1160 ListRecTy *GivenListTy = 0;
Bob Wilson21870412009-11-22 04:24:42 +00001161
David Greenee1b46912009-06-08 20:23:18 +00001162 if (ItemType != 0) {
1163 ListRecTy *ListType = dynamic_cast<ListRecTy*>(ItemType);
1164 if (ListType == 0) {
1165 std::stringstream s;
Bob Wilson21870412009-11-22 04:24:42 +00001166 s << "Type mismatch for list, expected list type, got "
David Greenee1b46912009-06-08 20:23:18 +00001167 << ItemType->getAsString();
1168 TokError(s.str());
Jim Grosbach6a44ada2011-03-11 19:52:52 +00001169 return 0;
David Greenee1b46912009-06-08 20:23:18 +00001170 }
1171 GivenListTy = ListType;
Bob Wilson21870412009-11-22 04:24:42 +00001172 }
David Greenee1b46912009-06-08 20:23:18 +00001173
Chris Lattnerf4601652007-11-22 20:49:04 +00001174 if (Lex.getCode() != tgtok::r_square) {
Bob Wilson21870412009-11-22 04:24:42 +00001175 Vals = ParseValueList(CurRec, 0,
1176 GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001177 if (Vals.empty()) return 0;
1178 }
1179 if (Lex.getCode() != tgtok::r_square) {
1180 TokError("expected ']' at end of list value");
1181 return 0;
1182 }
1183 Lex.Lex(); // eat the ']'
David Greenee1b46912009-06-08 20:23:18 +00001184
1185 RecTy *GivenEltTy = 0;
1186 if (Lex.getCode() == tgtok::less) {
1187 // Optional list element type
1188 Lex.Lex(); // eat the '<'
1189
1190 GivenEltTy = ParseType();
1191 if (GivenEltTy == 0) {
1192 // Couldn't parse element type
1193 return 0;
1194 }
1195
1196 if (Lex.getCode() != tgtok::greater) {
1197 TokError("expected '>' at end of list element type");
1198 return 0;
1199 }
1200 Lex.Lex(); // eat the '>'
1201 }
1202
1203 // Check elements
1204 RecTy *EltTy = 0;
David Greene05bce0b2011-07-29 22:43:06 +00001205 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greenee1b46912009-06-08 20:23:18 +00001206 i != ie;
1207 ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001208 TypedInit *TArg = dynamic_cast<TypedInit*>(*i);
David Greenee1b46912009-06-08 20:23:18 +00001209 if (TArg == 0) {
1210 TokError("Untyped list element");
1211 return 0;
1212 }
1213 if (EltTy != 0) {
1214 EltTy = resolveTypes(EltTy, TArg->getType());
1215 if (EltTy == 0) {
1216 TokError("Incompatible types in list elements");
1217 return 0;
1218 }
Bob Wilson21870412009-11-22 04:24:42 +00001219 } else {
David Greenee1b46912009-06-08 20:23:18 +00001220 EltTy = TArg->getType();
1221 }
1222 }
1223
1224 if (GivenEltTy != 0) {
1225 if (EltTy != 0) {
1226 // Verify consistency
1227 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1228 TokError("Incompatible types in list elements");
1229 return 0;
1230 }
1231 }
1232 EltTy = GivenEltTy;
1233 }
1234
1235 if (EltTy == 0) {
1236 if (ItemType == 0) {
1237 TokError("No type for list");
1238 return 0;
1239 }
1240 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +00001241 } else {
David Greenee1b46912009-06-08 20:23:18 +00001242 // Make sure the deduced type is compatible with the given type
1243 if (GivenListTy) {
1244 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1245 TokError("Element type mismatch for list");
1246 return 0;
1247 }
1248 }
1249 DeducedEltTy = EltTy;
1250 }
Bob Wilson21870412009-11-22 04:24:42 +00001251
David Greenedcd35c72011-07-29 19:07:07 +00001252 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4601652007-11-22 20:49:04 +00001253 }
1254 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1255 Lex.Lex(); // eat the '('
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001256 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001257 TokError("expected identifier in dag init");
1258 return 0;
1259 }
Bob Wilson21870412009-11-22 04:24:42 +00001260
David Greene05bce0b2011-07-29 22:43:06 +00001261 Init *Operator = ParseValue(CurRec);
Chris Lattner578bcf02010-10-06 04:31:40 +00001262 if (Operator == 0) return 0;
David Greenec7cafcd2009-04-22 20:18:10 +00001263
Nate Begeman7cee8172009-03-19 05:21:56 +00001264 // If the operator name is present, parse it.
1265 std::string OperatorName;
1266 if (Lex.getCode() == tgtok::colon) {
1267 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1268 TokError("expected variable name in dag operator");
1269 return 0;
1270 }
1271 OperatorName = Lex.getCurStrVal();
1272 Lex.Lex(); // eat the VarName.
1273 }
Bob Wilson21870412009-11-22 04:24:42 +00001274
David Greene05bce0b2011-07-29 22:43:06 +00001275 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4601652007-11-22 20:49:04 +00001276 if (Lex.getCode() != tgtok::r_paren) {
1277 DagArgs = ParseDagArgList(CurRec);
1278 if (DagArgs.empty()) return 0;
1279 }
Bob Wilson21870412009-11-22 04:24:42 +00001280
Chris Lattnerf4601652007-11-22 20:49:04 +00001281 if (Lex.getCode() != tgtok::r_paren) {
1282 TokError("expected ')' in dag init");
1283 return 0;
1284 }
1285 Lex.Lex(); // eat the ')'
Bob Wilson21870412009-11-22 04:24:42 +00001286
David Greenedcd35c72011-07-29 19:07:07 +00001287 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4601652007-11-22 20:49:04 +00001288 }
Bob Wilson21870412009-11-22 04:24:42 +00001289
David Greene1434f662011-01-07 17:05:37 +00001290 case tgtok::XHead:
1291 case tgtok::XTail:
1292 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +00001293 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001294 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +00001295 case tgtok::XSRA:
Chris Lattnerf4601652007-11-22 20:49:04 +00001296 case tgtok::XSRL:
1297 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +00001298 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001299 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene9bea7c82009-05-14 23:26:46 +00001300 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +00001301 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001302 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greened418c1b2009-05-14 20:54:48 +00001303 return ParseOperation(CurRec);
Chris Lattnerf4601652007-11-22 20:49:04 +00001304 }
1305 }
Bob Wilson21870412009-11-22 04:24:42 +00001306
Chris Lattnerf4601652007-11-22 20:49:04 +00001307 return R;
1308}
1309
1310/// ParseValue - Parse a tblgen value. This returns null on error.
1311///
1312/// Value ::= SimpleValue ValueSuffix*
1313/// ValueSuffix ::= '{' BitList '}'
1314/// ValueSuffix ::= '[' BitList ']'
1315/// ValueSuffix ::= '.' ID
1316///
David Greenef3744a02011-10-19 13:04:20 +00001317Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1318 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Chris Lattnerf4601652007-11-22 20:49:04 +00001319 if (Result == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001320
Chris Lattnerf4601652007-11-22 20:49:04 +00001321 // Parse the suffixes now if present.
1322 while (1) {
1323 switch (Lex.getCode()) {
1324 default: return Result;
1325 case tgtok::l_brace: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001326 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001327 Lex.Lex(); // eat the '{'
1328 std::vector<unsigned> Ranges = ParseRangeList();
1329 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001330
Chris Lattnerf4601652007-11-22 20:49:04 +00001331 // Reverse the bitlist.
1332 std::reverse(Ranges.begin(), Ranges.end());
1333 Result = Result->convertInitializerBitRange(Ranges);
1334 if (Result == 0) {
1335 Error(CurlyLoc, "Invalid bit range for value");
1336 return 0;
1337 }
Bob Wilson21870412009-11-22 04:24:42 +00001338
Chris Lattnerf4601652007-11-22 20:49:04 +00001339 // Eat the '}'.
1340 if (Lex.getCode() != tgtok::r_brace) {
1341 TokError("expected '}' at end of bit range list");
1342 return 0;
1343 }
1344 Lex.Lex();
1345 break;
1346 }
1347 case tgtok::l_square: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001348 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001349 Lex.Lex(); // eat the '['
1350 std::vector<unsigned> Ranges = ParseRangeList();
1351 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001352
Chris Lattnerf4601652007-11-22 20:49:04 +00001353 Result = Result->convertInitListSlice(Ranges);
1354 if (Result == 0) {
1355 Error(SquareLoc, "Invalid range for list slice");
1356 return 0;
1357 }
Bob Wilson21870412009-11-22 04:24:42 +00001358
Chris Lattnerf4601652007-11-22 20:49:04 +00001359 // Eat the ']'.
1360 if (Lex.getCode() != tgtok::r_square) {
1361 TokError("expected ']' at end of list slice");
1362 return 0;
1363 }
1364 Lex.Lex();
1365 break;
1366 }
1367 case tgtok::period:
1368 if (Lex.Lex() != tgtok::Id) { // eat the .
1369 TokError("expected field identifier after '.'");
1370 return 0;
1371 }
1372 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001373 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00001374 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +00001375 return 0;
1376 }
David Greenedcd35c72011-07-29 19:07:07 +00001377 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001378 Lex.Lex(); // eat field name
1379 break;
1380 }
1381 }
1382}
1383
1384/// ParseDagArgList - Parse the argument list for a dag literal expression.
1385///
1386/// ParseDagArgList ::= Value (':' VARNAME)?
1387/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
David Greene05bce0b2011-07-29 22:43:06 +00001388std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4601652007-11-22 20:49:04 +00001389TGParser::ParseDagArgList(Record *CurRec) {
David Greene05bce0b2011-07-29 22:43:06 +00001390 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson21870412009-11-22 04:24:42 +00001391
Chris Lattnerf4601652007-11-22 20:49:04 +00001392 while (1) {
David Greene05bce0b2011-07-29 22:43:06 +00001393 Init *Val = ParseValue(CurRec);
1394 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
Bob Wilson21870412009-11-22 04:24:42 +00001395
Chris Lattnerf4601652007-11-22 20:49:04 +00001396 // If the variable name is present, add it.
1397 std::string VarName;
1398 if (Lex.getCode() == tgtok::colon) {
1399 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1400 TokError("expected variable name in dag literal");
David Greene05bce0b2011-07-29 22:43:06 +00001401 return std::vector<std::pair<llvm::Init*, std::string> >();
Chris Lattnerf4601652007-11-22 20:49:04 +00001402 }
1403 VarName = Lex.getCurStrVal();
1404 Lex.Lex(); // eat the VarName.
1405 }
Bob Wilson21870412009-11-22 04:24:42 +00001406
Chris Lattnerf4601652007-11-22 20:49:04 +00001407 Result.push_back(std::make_pair(Val, VarName));
Bob Wilson21870412009-11-22 04:24:42 +00001408
Chris Lattnerf4601652007-11-22 20:49:04 +00001409 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson21870412009-11-22 04:24:42 +00001410 Lex.Lex(); // eat the ','
Chris Lattnerf4601652007-11-22 20:49:04 +00001411 }
Bob Wilson21870412009-11-22 04:24:42 +00001412
Chris Lattnerf4601652007-11-22 20:49:04 +00001413 return Result;
1414}
1415
1416
1417/// ParseValueList - Parse a comma separated list of values, returning them as a
1418/// vector. Note that this always expects to be able to parse at least one
1419/// value. It returns an empty list if this is not possible.
1420///
1421/// ValueList ::= Value (',' Value)
1422///
David Greene05bce0b2011-07-29 22:43:06 +00001423std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopherd568b3f2011-07-11 23:06:52 +00001424 RecTy *EltTy) {
David Greene05bce0b2011-07-29 22:43:06 +00001425 std::vector<Init*> Result;
David Greenee1b46912009-06-08 20:23:18 +00001426 RecTy *ItemType = EltTy;
David Greene67acdf22009-06-29 19:59:52 +00001427 unsigned int ArgN = 0;
David Greenee1b46912009-06-08 20:23:18 +00001428 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001429 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greenee1b46912009-06-08 20:23:18 +00001430 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greened9746fe2011-09-19 18:26:07 +00001431 if (!RV) {
1432 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1433 << ")\n";
1434 }
David Greenee1b46912009-06-08 20:23:18 +00001435 assert(RV && "Template argument record not found??");
1436 ItemType = RV->getType();
1437 ++ArgN;
1438 }
1439 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001440 if (Result.back() == 0) return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001441
Chris Lattnerf4601652007-11-22 20:49:04 +00001442 while (Lex.getCode() == tgtok::comma) {
1443 Lex.Lex(); // Eat the comma
Bob Wilson21870412009-11-22 04:24:42 +00001444
David Greenee1b46912009-06-08 20:23:18 +00001445 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001446 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greene67acdf22009-06-29 19:59:52 +00001447 if (ArgN >= TArgs.size()) {
1448 TokError("too many template arguments");
David Greene05bce0b2011-07-29 22:43:06 +00001449 return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001450 }
David Greenee1b46912009-06-08 20:23:18 +00001451 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1452 assert(RV && "Template argument record not found??");
1453 ItemType = RV->getType();
1454 ++ArgN;
1455 }
1456 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001457 if (Result.back() == 0) return std::vector<Init*>();
Chris Lattnerf4601652007-11-22 20:49:04 +00001458 }
Bob Wilson21870412009-11-22 04:24:42 +00001459
Chris Lattnerf4601652007-11-22 20:49:04 +00001460 return Result;
1461}
1462
1463
Chris Lattnerf4601652007-11-22 20:49:04 +00001464/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1465/// empty string on error. This can happen in a number of different context's,
1466/// including within a def or in the template args for a def (which which case
1467/// CurRec will be non-null) and within the template args for a multiclass (in
1468/// which case CurRec will be null, but CurMultiClass will be set). This can
1469/// also happen within a def that is within a multiclass, which will set both
1470/// CurRec and CurMultiClass.
1471///
1472/// Declaration ::= FIELD? Type ID ('=' Value)?
1473///
David Greenee22b3212011-10-19 13:02:42 +00001474Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4601652007-11-22 20:49:04 +00001475 bool ParsingTemplateArgs) {
1476 // Read the field prefix if present.
1477 bool HasField = Lex.getCode() == tgtok::Field;
1478 if (HasField) Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001479
Chris Lattnerf4601652007-11-22 20:49:04 +00001480 RecTy *Type = ParseType();
David Greenee22b3212011-10-19 13:02:42 +00001481 if (Type == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001482
Chris Lattnerf4601652007-11-22 20:49:04 +00001483 if (Lex.getCode() != tgtok::Id) {
1484 TokError("Expected identifier in declaration");
David Greenee22b3212011-10-19 13:02:42 +00001485 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001486 }
Bob Wilson21870412009-11-22 04:24:42 +00001487
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001488 SMLoc IdLoc = Lex.getLoc();
David Greenee22b3212011-10-19 13:02:42 +00001489 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001490 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001491
Chris Lattnerf4601652007-11-22 20:49:04 +00001492 if (ParsingTemplateArgs) {
1493 if (CurRec) {
David Greenee22b3212011-10-19 13:02:42 +00001494 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4601652007-11-22 20:49:04 +00001495 } else {
1496 assert(CurMultiClass);
1497 }
1498 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +00001499 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1500 "::");
Chris Lattnerf4601652007-11-22 20:49:04 +00001501 }
Bob Wilson21870412009-11-22 04:24:42 +00001502
Chris Lattnerf4601652007-11-22 20:49:04 +00001503 // Add the value.
1504 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
David Greenee22b3212011-10-19 13:02:42 +00001505 return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001506
Chris Lattnerf4601652007-11-22 20:49:04 +00001507 // If a value is present, parse it.
1508 if (Lex.getCode() == tgtok::equal) {
1509 Lex.Lex();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001510 SMLoc ValLoc = Lex.getLoc();
David Greene05bce0b2011-07-29 22:43:06 +00001511 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001512 if (Val == 0 ||
1513 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
David Greenee22b3212011-10-19 13:02:42 +00001514 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001515 }
Bob Wilson21870412009-11-22 04:24:42 +00001516
Chris Lattnerf4601652007-11-22 20:49:04 +00001517 return DeclName;
1518}
1519
1520/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1521/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1522/// template args for a def, which may or may not be in a multiclass. If null,
1523/// these are the template args for a multiclass.
1524///
1525/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson21870412009-11-22 04:24:42 +00001526///
Chris Lattnerf4601652007-11-22 20:49:04 +00001527bool TGParser::ParseTemplateArgList(Record *CurRec) {
1528 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1529 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +00001530
Chris Lattnerf4601652007-11-22 20:49:04 +00001531 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +00001532
Chris Lattnerf4601652007-11-22 20:49:04 +00001533 // Read the first declaration.
David Greenee22b3212011-10-19 13:02:42 +00001534 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1535 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001536 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001537
Chris Lattnerf4601652007-11-22 20:49:04 +00001538 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson21870412009-11-22 04:24:42 +00001539
Chris Lattnerf4601652007-11-22 20:49:04 +00001540 while (Lex.getCode() == tgtok::comma) {
1541 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001542
Chris Lattnerf4601652007-11-22 20:49:04 +00001543 // Read the following declarations.
1544 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
David Greenee22b3212011-10-19 13:02:42 +00001545 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001546 return true;
1547 TheRecToAddTo->addTemplateArg(TemplArg);
1548 }
Bob Wilson21870412009-11-22 04:24:42 +00001549
Chris Lattnerf4601652007-11-22 20:49:04 +00001550 if (Lex.getCode() != tgtok::greater)
1551 return TokError("expected '>' at end of template argument list");
1552 Lex.Lex(); // eat the '>'.
1553 return false;
1554}
1555
1556
1557/// ParseBodyItem - Parse a single item at within the body of a def or class.
1558///
1559/// BodyItem ::= Declaration ';'
1560/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1561bool TGParser::ParseBodyItem(Record *CurRec) {
1562 if (Lex.getCode() != tgtok::Let) {
David Greenee22b3212011-10-19 13:02:42 +00001563 if (ParseDeclaration(CurRec, false) == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001564 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001565
Chris Lattnerf4601652007-11-22 20:49:04 +00001566 if (Lex.getCode() != tgtok::semi)
1567 return TokError("expected ';' after declaration");
1568 Lex.Lex();
1569 return false;
1570 }
1571
1572 // LET ID OptionalRangeList '=' Value ';'
1573 if (Lex.Lex() != tgtok::Id)
1574 return TokError("expected field identifier after let");
Bob Wilson21870412009-11-22 04:24:42 +00001575
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001576 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001577 std::string FieldName = Lex.getCurStrVal();
1578 Lex.Lex(); // eat the field name.
Bob Wilson21870412009-11-22 04:24:42 +00001579
Chris Lattnerf4601652007-11-22 20:49:04 +00001580 std::vector<unsigned> BitList;
Bob Wilson21870412009-11-22 04:24:42 +00001581 if (ParseOptionalBitList(BitList))
Chris Lattnerf4601652007-11-22 20:49:04 +00001582 return true;
1583 std::reverse(BitList.begin(), BitList.end());
Bob Wilson21870412009-11-22 04:24:42 +00001584
Chris Lattnerf4601652007-11-22 20:49:04 +00001585 if (Lex.getCode() != tgtok::equal)
1586 return TokError("expected '=' in let expression");
1587 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001588
David Greenee1b46912009-06-08 20:23:18 +00001589 RecordVal *Field = CurRec->getValue(FieldName);
1590 if (Field == 0)
1591 return TokError("Value '" + FieldName + "' unknown!");
1592
1593 RecTy *Type = Field->getType();
Bob Wilson21870412009-11-22 04:24:42 +00001594
David Greene05bce0b2011-07-29 22:43:06 +00001595 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001596 if (Val == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001597
Chris Lattnerf4601652007-11-22 20:49:04 +00001598 if (Lex.getCode() != tgtok::semi)
1599 return TokError("expected ';' after let expression");
1600 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001601
Chris Lattnerf4601652007-11-22 20:49:04 +00001602 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1603}
1604
1605/// ParseBody - Read the body of a class or def. Return true on error, false on
1606/// success.
1607///
1608/// Body ::= ';'
1609/// Body ::= '{' BodyList '}'
1610/// BodyList BodyItem*
1611///
1612bool TGParser::ParseBody(Record *CurRec) {
1613 // If this is a null definition, just eat the semi and return.
1614 if (Lex.getCode() == tgtok::semi) {
1615 Lex.Lex();
1616 return false;
1617 }
Bob Wilson21870412009-11-22 04:24:42 +00001618
Chris Lattnerf4601652007-11-22 20:49:04 +00001619 if (Lex.getCode() != tgtok::l_brace)
1620 return TokError("Expected ';' or '{' to start body");
1621 // Eat the '{'.
1622 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001623
Chris Lattnerf4601652007-11-22 20:49:04 +00001624 while (Lex.getCode() != tgtok::r_brace)
1625 if (ParseBodyItem(CurRec))
1626 return true;
1627
1628 // Eat the '}'.
1629 Lex.Lex();
1630 return false;
1631}
1632
1633/// ParseObjectBody - Parse the body of a def or class. This consists of an
1634/// optional ClassList followed by a Body. CurRec is the current def or class
1635/// that is being parsed.
1636///
1637/// ObjectBody ::= BaseClassList Body
1638/// BaseClassList ::= /*empty*/
1639/// BaseClassList ::= ':' BaseClassListNE
1640/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1641///
1642bool TGParser::ParseObjectBody(Record *CurRec) {
1643 // If there is a baseclass list, read it.
1644 if (Lex.getCode() == tgtok::colon) {
1645 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001646
Chris Lattnerf4601652007-11-22 20:49:04 +00001647 // Read all of the subclasses.
1648 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1649 while (1) {
1650 // Check for error.
1651 if (SubClass.Rec == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001652
Chris Lattnerf4601652007-11-22 20:49:04 +00001653 // Add it.
1654 if (AddSubClass(CurRec, SubClass))
1655 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001656
Chris Lattnerf4601652007-11-22 20:49:04 +00001657 if (Lex.getCode() != tgtok::comma) break;
1658 Lex.Lex(); // eat ','.
1659 SubClass = ParseSubClassReference(CurRec, false);
1660 }
1661 }
1662
1663 // Process any variables on the let stack.
1664 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1665 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1666 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1667 LetStack[i][j].Bits, LetStack[i][j].Value))
1668 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001669
Chris Lattnerf4601652007-11-22 20:49:04 +00001670 return ParseBody(CurRec);
1671}
1672
Chris Lattnerf4601652007-11-22 20:49:04 +00001673/// ParseDef - Parse and return a top level or multiclass def, return the record
1674/// corresponding to it. This returns null on error.
1675///
1676/// DefInst ::= DEF ObjectName ObjectBody
1677///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001678bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001679 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001680 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson21870412009-11-22 04:24:42 +00001681 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4601652007-11-22 20:49:04 +00001682
1683 // Parse ObjectName and make a record for it.
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001684 Record *CurRec = new Record(ParseObjectName(), DefLoc, Records);
Bob Wilson21870412009-11-22 04:24:42 +00001685
Chris Lattnerf4601652007-11-22 20:49:04 +00001686 if (!CurMultiClass) {
1687 // Top-level def definition.
Bob Wilson21870412009-11-22 04:24:42 +00001688
Chris Lattnerf4601652007-11-22 20:49:04 +00001689 // Ensure redefinition doesn't happen.
1690 if (Records.getDef(CurRec->getName())) {
David Greene2c49fbb2011-10-19 13:03:45 +00001691 Error(DefLoc, "def '" + CurRec->getNameInitAsString()
1692 + "' already defined");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001693 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001694 }
1695 Records.addDef(CurRec);
1696 } else {
1697 // Otherwise, a def inside a multiclass, add it to the multiclass.
1698 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene91919cd2011-10-19 13:03:51 +00001699 if (CurMultiClass->DefPrototypes[i]->getNameInit()
1700 == CurRec->getNameInit()) {
1701 Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
Chris Lattnerf4601652007-11-22 20:49:04 +00001702 "' already defined in this multiclass!");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001703 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001704 }
1705 CurMultiClass->DefPrototypes.push_back(CurRec);
1706 }
Bob Wilson21870412009-11-22 04:24:42 +00001707
Chris Lattnerf4601652007-11-22 20:49:04 +00001708 if (ParseObjectBody(CurRec))
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001709 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001710
Chris Lattnerf4601652007-11-22 20:49:04 +00001711 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
David Greene0d886402011-08-10 18:27:46 +00001712 // See Record::setName(). This resolve step will see any new name
1713 // for the def that might have been created when resolving
1714 // inheritance, values and arguments above.
Chris Lattnerf4601652007-11-22 20:49:04 +00001715 CurRec->resolveReferences();
Bob Wilson21870412009-11-22 04:24:42 +00001716
Chris Lattnerf4601652007-11-22 20:49:04 +00001717 // If ObjectBody has template arguments, it's an error.
1718 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001719
1720 if (CurMultiClass) {
1721 // Copy the template arguments for the multiclass into the def.
David Greenee22b3212011-10-19 13:02:42 +00001722 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001723 CurMultiClass->Rec.getTemplateArgs();
1724
1725 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1726 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1727 assert(RV && "Template arg doesn't exist?");
1728 CurRec->addValue(*RV);
1729 }
1730 }
1731
1732 return false;
Chris Lattnerf4601652007-11-22 20:49:04 +00001733}
1734
Chris Lattnerf4601652007-11-22 20:49:04 +00001735/// ParseClass - Parse a tblgen class definition.
1736///
1737/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
1738///
1739bool TGParser::ParseClass() {
1740 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
1741 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001742
Chris Lattnerf4601652007-11-22 20:49:04 +00001743 if (Lex.getCode() != tgtok::Id)
1744 return TokError("expected class name after 'class' keyword");
Bob Wilson21870412009-11-22 04:24:42 +00001745
Chris Lattnerf4601652007-11-22 20:49:04 +00001746 Record *CurRec = Records.getClass(Lex.getCurStrVal());
1747 if (CurRec) {
1748 // If the body was previously defined, this is an error.
David Greenee3385652011-10-19 13:04:13 +00001749 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4601652007-11-22 20:49:04 +00001750 !CurRec->getSuperClasses().empty() ||
1751 !CurRec->getTemplateArgs().empty())
David Greene69a23942011-10-19 13:03:58 +00001752 return TokError("Class '" + CurRec->getNameInitAsString()
1753 + "' already defined");
Chris Lattnerf4601652007-11-22 20:49:04 +00001754 } else {
1755 // If this is the first reference to this class, create and add it.
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001756 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001757 Records.addClass(CurRec);
1758 }
1759 Lex.Lex(); // eat the name.
Bob Wilson21870412009-11-22 04:24:42 +00001760
Chris Lattnerf4601652007-11-22 20:49:04 +00001761 // If there are template args, parse them.
1762 if (Lex.getCode() == tgtok::less)
1763 if (ParseTemplateArgList(CurRec))
1764 return true;
1765
1766 // Finally, parse the object body.
1767 return ParseObjectBody(CurRec);
1768}
1769
1770/// ParseLetList - Parse a non-empty list of assignment expressions into a list
1771/// of LetRecords.
1772///
1773/// LetList ::= LetItem (',' LetItem)*
1774/// LetItem ::= ID OptionalRangeList '=' Value
1775///
1776std::vector<LetRecord> TGParser::ParseLetList() {
1777 std::vector<LetRecord> Result;
Bob Wilson21870412009-11-22 04:24:42 +00001778
Chris Lattnerf4601652007-11-22 20:49:04 +00001779 while (1) {
1780 if (Lex.getCode() != tgtok::Id) {
1781 TokError("expected identifier in let definition");
1782 return std::vector<LetRecord>();
1783 }
1784 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001785 SMLoc NameLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +00001786 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4601652007-11-22 20:49:04 +00001787
1788 // Check for an optional RangeList.
1789 std::vector<unsigned> Bits;
Bob Wilson21870412009-11-22 04:24:42 +00001790 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4601652007-11-22 20:49:04 +00001791 return std::vector<LetRecord>();
1792 std::reverse(Bits.begin(), Bits.end());
Bob Wilson21870412009-11-22 04:24:42 +00001793
Chris Lattnerf4601652007-11-22 20:49:04 +00001794 if (Lex.getCode() != tgtok::equal) {
1795 TokError("expected '=' in let expression");
1796 return std::vector<LetRecord>();
1797 }
1798 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001799
David Greene05bce0b2011-07-29 22:43:06 +00001800 Init *Val = ParseValue(0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001801 if (Val == 0) return std::vector<LetRecord>();
Bob Wilson21870412009-11-22 04:24:42 +00001802
Chris Lattnerf4601652007-11-22 20:49:04 +00001803 // Now that we have everything, add the record.
1804 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson21870412009-11-22 04:24:42 +00001805
Chris Lattnerf4601652007-11-22 20:49:04 +00001806 if (Lex.getCode() != tgtok::comma)
1807 return Result;
Bob Wilson21870412009-11-22 04:24:42 +00001808 Lex.Lex(); // eat the comma.
Chris Lattnerf4601652007-11-22 20:49:04 +00001809 }
1810}
1811
1812/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001813/// different related productions. This works inside multiclasses too.
Chris Lattnerf4601652007-11-22 20:49:04 +00001814///
1815/// Object ::= LET LetList IN '{' ObjectList '}'
1816/// Object ::= LET LetList IN Object
1817///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001818bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001819 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
1820 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001821
Chris Lattnerf4601652007-11-22 20:49:04 +00001822 // Add this entry to the let stack.
1823 std::vector<LetRecord> LetInfo = ParseLetList();
1824 if (LetInfo.empty()) return true;
1825 LetStack.push_back(LetInfo);
1826
1827 if (Lex.getCode() != tgtok::In)
1828 return TokError("expected 'in' at end of top-level 'let'");
1829 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001830
Chris Lattnerf4601652007-11-22 20:49:04 +00001831 // If this is a scalar let, just handle it now
1832 if (Lex.getCode() != tgtok::l_brace) {
1833 // LET LetList IN Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001834 if (ParseObject(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00001835 return true;
1836 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001837 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001838 // Otherwise, this is a group let.
1839 Lex.Lex(); // eat the '{'.
Bob Wilson21870412009-11-22 04:24:42 +00001840
Chris Lattnerf4601652007-11-22 20:49:04 +00001841 // Parse the object list.
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001842 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00001843 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001844
Chris Lattnerf4601652007-11-22 20:49:04 +00001845 if (Lex.getCode() != tgtok::r_brace) {
1846 TokError("expected '}' at end of top level let command");
1847 return Error(BraceLoc, "to match this '{'");
1848 }
1849 Lex.Lex();
1850 }
Bob Wilson21870412009-11-22 04:24:42 +00001851
Chris Lattnerf4601652007-11-22 20:49:04 +00001852 // Outside this let scope, this let block is not active.
1853 LetStack.pop_back();
1854 return false;
1855}
1856
Chris Lattnerf4601652007-11-22 20:49:04 +00001857/// ParseMultiClass - Parse a multiclass definition.
1858///
Bob Wilson32558652009-04-28 19:41:44 +00001859/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
1860/// ':' BaseMultiClassList '{' MultiClassDef+ '}'
Chris Lattnerf4601652007-11-22 20:49:04 +00001861///
1862bool TGParser::ParseMultiClass() {
1863 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
1864 Lex.Lex(); // Eat the multiclass token.
1865
1866 if (Lex.getCode() != tgtok::Id)
1867 return TokError("expected identifier after multiclass for name");
1868 std::string Name = Lex.getCurStrVal();
Bob Wilson21870412009-11-22 04:24:42 +00001869
Chris Lattnerf4601652007-11-22 20:49:04 +00001870 if (MultiClasses.count(Name))
1871 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson21870412009-11-22 04:24:42 +00001872
Chris Lattner67db8832010-12-13 00:23:57 +00001873 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
1874 Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001875 Lex.Lex(); // Eat the identifier.
Bob Wilson21870412009-11-22 04:24:42 +00001876
Chris Lattnerf4601652007-11-22 20:49:04 +00001877 // If there are template args, parse them.
1878 if (Lex.getCode() == tgtok::less)
1879 if (ParseTemplateArgList(0))
1880 return true;
1881
David Greened34a73b2009-04-24 16:55:41 +00001882 bool inherits = false;
1883
David Greenede444af2009-04-22 16:42:54 +00001884 // If there are submulticlasses, parse them.
1885 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00001886 inherits = true;
1887
David Greenede444af2009-04-22 16:42:54 +00001888 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00001889
David Greenede444af2009-04-22 16:42:54 +00001890 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00001891 SubMultiClassReference SubMultiClass =
1892 ParseSubMultiClassReference(CurMultiClass);
David Greenede444af2009-04-22 16:42:54 +00001893 while (1) {
1894 // Check for error.
1895 if (SubMultiClass.MC == 0) return true;
Bob Wilson32558652009-04-28 19:41:44 +00001896
David Greenede444af2009-04-22 16:42:54 +00001897 // Add it.
1898 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
1899 return true;
Bob Wilson32558652009-04-28 19:41:44 +00001900
David Greenede444af2009-04-22 16:42:54 +00001901 if (Lex.getCode() != tgtok::comma) break;
1902 Lex.Lex(); // eat ','.
1903 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
1904 }
1905 }
1906
David Greened34a73b2009-04-24 16:55:41 +00001907 if (Lex.getCode() != tgtok::l_brace) {
1908 if (!inherits)
1909 return TokError("expected '{' in multiclass definition");
Bob Wilson21870412009-11-22 04:24:42 +00001910 else if (Lex.getCode() != tgtok::semi)
1911 return TokError("expected ';' in multiclass definition");
David Greened34a73b2009-04-24 16:55:41 +00001912 else
Bob Wilson21870412009-11-22 04:24:42 +00001913 Lex.Lex(); // eat the ';'.
1914 } else {
David Greened34a73b2009-04-24 16:55:41 +00001915 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
1916 return TokError("multiclass must contain at least one def");
Bob Wilson21870412009-11-22 04:24:42 +00001917
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001918 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001919 switch (Lex.getCode()) {
1920 default:
David Greenea1b1b792011-10-07 18:25:05 +00001921 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001922 case tgtok::Let:
1923 case tgtok::Def:
1924 case tgtok::Defm:
1925 if (ParseObject(CurMultiClass))
1926 return true;
1927 break;
1928 }
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001929 }
David Greened34a73b2009-04-24 16:55:41 +00001930 Lex.Lex(); // eat the '}'.
1931 }
Bob Wilson21870412009-11-22 04:24:42 +00001932
Chris Lattnerf4601652007-11-22 20:49:04 +00001933 CurMultiClass = 0;
1934 return false;
1935}
1936
David Greenee499a2d2011-10-05 22:42:07 +00001937Record *TGParser::
1938InstantiateMulticlassDef(MultiClass &MC,
1939 Record *DefProto,
1940 const std::string &DefmPrefix,
1941 SMLoc DefmPrefixLoc) {
1942 // Add in the defm name. If the defm prefix is empty, give each
1943 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
1944 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
1945 // as a prefix.
1946 std::string DefName = DefProto->getName();
1947 if (DefmPrefix.empty()) {
1948 DefName = GetNewAnonymousName();
1949 } else {
1950 std::string::size_type idx = DefName.find("#NAME#");
1951 if (idx != std::string::npos) {
1952 DefName.replace(idx, 6, DefmPrefix);
1953 } else {
1954 // Add the suffix to the defm name to get the new name.
1955 DefName = DefmPrefix + DefName;
1956 }
1957 }
1958
1959 Record *CurRec = new Record(DefName, DefmPrefixLoc, Records);
1960
1961 SubClassReference Ref;
1962 Ref.RefLoc = DefmPrefixLoc;
1963 Ref.Rec = DefProto;
1964 AddSubClass(CurRec, Ref);
1965
1966 return CurRec;
1967}
1968
1969bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
1970 Record *CurRec,
1971 SMLoc DefmPrefixLoc,
1972 SMLoc SubClassLoc,
David Greenee22b3212011-10-19 13:02:42 +00001973 const std::vector<Init *> &TArgs,
David Greenee499a2d2011-10-05 22:42:07 +00001974 std::vector<Init *> &TemplateVals,
1975 bool DeleteArgs) {
1976 // Loop over all of the template arguments, setting them to the specified
1977 // value or leaving them as the default if necessary.
1978 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1979 // Check if a value is specified for this temp-arg.
1980 if (i < TemplateVals.size()) {
1981 // Set it now.
1982 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
1983 TemplateVals[i]))
1984 return true;
1985
1986 // Resolve it next.
1987 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
1988
1989 if (DeleteArgs)
1990 // Now remove it.
1991 CurRec->removeValue(TArgs[i]);
1992
1993 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
1994 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenee22b3212011-10-19 13:02:42 +00001995 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
1996 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
1997 + "'");
David Greenee499a2d2011-10-05 22:42:07 +00001998 }
1999 }
2000 return false;
2001}
2002
2003bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2004 Record *CurRec,
2005 Record *DefProto,
2006 SMLoc DefmPrefixLoc) {
2007 // If the mdef is inside a 'let' expression, add to each def.
2008 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2009 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2010 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2011 LetStack[i][j].Bits, LetStack[i][j].Value))
2012 return Error(DefmPrefixLoc, "when instantiating this defm");
2013
2014 // Ensure redefinition doesn't happen.
2015 if (Records.getDef(CurRec->getName()))
2016 return Error(DefmPrefixLoc, "def '" + CurRec->getName() +
2017 "' already defined, instantiating defm with subdef '" +
2018 DefProto->getName() + "'");
2019
2020 // Don't create a top level definition for defm inside multiclasses,
2021 // instead, only update the prototypes and bind the template args
2022 // with the new created definition.
2023 if (CurMultiClass) {
2024 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2025 i != e; ++i)
David Greene22dde7e2011-10-19 13:04:02 +00002026 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2027 == CurRec->getNameInit())
2028 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
David Greenee499a2d2011-10-05 22:42:07 +00002029 "' already defined in this multiclass!");
2030 CurMultiClass->DefPrototypes.push_back(CurRec);
2031
2032 // Copy the template arguments for the multiclass into the new def.
David Greenee22b3212011-10-19 13:02:42 +00002033 const std::vector<Init *> &TA =
David Greenee499a2d2011-10-05 22:42:07 +00002034 CurMultiClass->Rec.getTemplateArgs();
2035
2036 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2037 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2038 assert(RV && "Template arg doesn't exist?");
2039 CurRec->addValue(*RV);
2040 }
2041 } else {
2042 Records.addDef(CurRec);
2043 }
2044
2045 return false;
2046}
2047
Chris Lattnerf4601652007-11-22 20:49:04 +00002048/// ParseDefm - Parse the instantiation of a multiclass.
2049///
2050/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2051///
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002052bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002053 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Bob Wilson21870412009-11-22 04:24:42 +00002054
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002055 std::string DefmPrefix;
2056 if (Lex.Lex() == tgtok::Id) { // eat the defm.
2057 DefmPrefix = Lex.getCurStrVal();
2058 Lex.Lex(); // Eat the defm prefix.
2059 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +00002060
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002061 SMLoc DefmPrefixLoc = Lex.getLoc();
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002062 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4601652007-11-22 20:49:04 +00002063 return TokError("expected ':' after defm identifier");
Bob Wilson21870412009-11-22 04:24:42 +00002064
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002065 // Keep track of the new generated record definitions.
2066 std::vector<Record*> NewRecDefs;
2067
2068 // This record also inherits from a regular class (non-multiclass)?
2069 bool InheritFromClass = false;
2070
Chris Lattnerf4601652007-11-22 20:49:04 +00002071 // eat the colon.
2072 Lex.Lex();
2073
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002074 SMLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002075 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene56546132009-04-22 22:17:51 +00002076
2077 while (1) {
2078 if (Ref.Rec == 0) return true;
2079
2080 // To instantiate a multiclass, we need to first get the multiclass, then
2081 // instantiate each def contained in the multiclass with the SubClassRef
2082 // template parameters.
2083 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2084 assert(MC && "Didn't lookup multiclass correctly?");
David Greene05bce0b2011-07-29 22:43:06 +00002085 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greene56546132009-04-22 22:17:51 +00002086
2087 // Verify that the correct number of template arguments were specified.
David Greenee22b3212011-10-19 13:02:42 +00002088 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greene56546132009-04-22 22:17:51 +00002089 if (TArgs.size() < TemplateVals.size())
2090 return Error(SubClassLoc,
2091 "more template args specified than multiclass expects");
2092
2093 // Loop over all the def's in the multiclass, instantiating each one.
2094 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2095 Record *DefProto = MC->DefPrototypes[i];
2096
David Greenee499a2d2011-10-05 22:42:07 +00002097 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix, DefmPrefixLoc);
David Greene065f2592009-05-05 16:28:25 +00002098
David Greenee499a2d2011-10-05 22:42:07 +00002099 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmPrefixLoc, SubClassLoc,
2100 TArgs, TemplateVals, true/*Delete args*/))
2101 return Error(SubClassLoc, "could not instantiate def");
David Greene56546132009-04-22 22:17:51 +00002102
David Greenee499a2d2011-10-05 22:42:07 +00002103 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmPrefixLoc))
2104 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002105
2106 NewRecDefs.push_back(CurRec);
David Greene56546132009-04-22 22:17:51 +00002107 }
2108
David Greenee499a2d2011-10-05 22:42:07 +00002109
David Greene56546132009-04-22 22:17:51 +00002110 if (Lex.getCode() != tgtok::comma) break;
2111 Lex.Lex(); // eat ','.
2112
2113 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002114
2115 // A defm can inherit from regular classes (non-multiclass) as
2116 // long as they come in the end of the inheritance list.
2117 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0);
2118
2119 if (InheritFromClass)
2120 break;
2121
David Greene56546132009-04-22 22:17:51 +00002122 Ref = ParseSubClassReference(0, true);
2123 }
2124
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002125 if (InheritFromClass) {
2126 // Process all the classes to inherit as if they were part of a
2127 // regular 'def' and inherit all record values.
2128 SubClassReference SubClass = ParseSubClassReference(0, false);
2129 while (1) {
2130 // Check for error.
2131 if (SubClass.Rec == 0) return true;
2132
2133 // Get the expanded definition prototypes and teach them about
2134 // the record values the current class to inherit has
2135 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2136 Record *CurRec = NewRecDefs[i];
2137
2138 // Add it.
2139 if (AddSubClass(CurRec, SubClass))
2140 return true;
2141
2142 // Process any variables on the let stack.
2143 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2144 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2145 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2146 LetStack[i][j].Bits, LetStack[i][j].Value))
2147 return true;
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002148 }
2149
2150 if (Lex.getCode() != tgtok::comma) break;
2151 Lex.Lex(); // eat ','.
2152 SubClass = ParseSubClassReference(0, false);
2153 }
2154 }
2155
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002156 if (!CurMultiClass)
2157 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene0d886402011-08-10 18:27:46 +00002158 // See Record::setName(). This resolve step will see any new
2159 // name for the def that might have been created when resolving
2160 // inheritance, values and arguments above.
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002161 NewRecDefs[i]->resolveReferences();
2162
Chris Lattnerf4601652007-11-22 20:49:04 +00002163 if (Lex.getCode() != tgtok::semi)
2164 return TokError("expected ';' at end of defm");
2165 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002166
Chris Lattnerf4601652007-11-22 20:49:04 +00002167 return false;
2168}
2169
2170/// ParseObject
2171/// Object ::= ClassInst
2172/// Object ::= DefInst
2173/// Object ::= MultiClassInst
2174/// Object ::= DefMInst
2175/// Object ::= LETCommand '{' ObjectList '}'
2176/// Object ::= LETCommand Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002177bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002178 switch (Lex.getCode()) {
Chris Lattnerd6d9dd92010-10-31 19:27:15 +00002179 default:
2180 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002181 case tgtok::Let: return ParseTopLevelLet(MC);
2182 case tgtok::Def: return ParseDef(MC);
2183 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4601652007-11-22 20:49:04 +00002184 case tgtok::Class: return ParseClass();
2185 case tgtok::MultiClass: return ParseMultiClass();
2186 }
2187}
2188
2189/// ParseObjectList
2190/// ObjectList :== Object*
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002191bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002192 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002193 if (ParseObject(MC))
Chris Lattnerf4601652007-11-22 20:49:04 +00002194 return true;
2195 }
2196 return false;
2197}
2198
Chris Lattnerf4601652007-11-22 20:49:04 +00002199bool TGParser::ParseFile() {
2200 Lex.Lex(); // Prime the lexer.
2201 if (ParseObjectList()) return true;
Bob Wilson21870412009-11-22 04:24:42 +00002202
Chris Lattnerf4601652007-11-22 20:49:04 +00002203 // If we have unread input at the end of the file, report it.
2204 if (Lex.getCode() == tgtok::Eof)
2205 return false;
Bob Wilson21870412009-11-22 04:24:42 +00002206
Chris Lattnerf4601652007-11-22 20:49:04 +00002207 return TokError("Unexpected input at top level");
2208}
2209