blob: e913bce705ab840ee461e7afa81e4dda01ffef75 [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 Greene05bce0b2011-07-29 22:43:06 +0000639Init *TGParser::ParseIDValue(Record *CurRec) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000640 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
641 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000642 SMLoc Loc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000643 Lex.Lex();
644 return ParseIDValue(CurRec, Name, Loc);
645}
646
647/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
648/// has already been read.
David Greene05bce0b2011-07-29 22:43:06 +0000649Init *TGParser::ParseIDValue(Record *CurRec,
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000650 const std::string &Name, SMLoc NameLoc) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000651 if (CurRec) {
652 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenedcd35c72011-07-29 19:07:07 +0000653 return VarInit::get(Name, RV->getType());
Bob Wilson21870412009-11-22 04:24:42 +0000654
David Greenee22b3212011-10-19 13:02:42 +0000655 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
656
David Greenecaa25c82011-10-05 22:42:54 +0000657 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +0000658 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
659 "::");
David Greenecaa25c82011-10-05 22:42:54 +0000660
Chris Lattnerf4601652007-11-22 20:49:04 +0000661 if (CurRec->isTemplateArg(TemplateArgName)) {
662 const RecordVal *RV = CurRec->getValue(TemplateArgName);
663 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000664 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000665 }
666 }
Bob Wilson21870412009-11-22 04:24:42 +0000667
Chris Lattnerf4601652007-11-22 20:49:04 +0000668 if (CurMultiClass) {
David Greenee22b3212011-10-19 13:02:42 +0000669 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
670 "::");
671
Chris Lattnerf4601652007-11-22 20:49:04 +0000672 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
673 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
674 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000675 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000676 }
677 }
Bob Wilson21870412009-11-22 04:24:42 +0000678
Chris Lattnerf4601652007-11-22 20:49:04 +0000679 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000680 return DefInit::get(D);
Chris Lattnerf4601652007-11-22 20:49:04 +0000681
682 Error(NameLoc, "Variable not defined: '" + Name + "'");
683 return 0;
684}
685
David Greened418c1b2009-05-14 20:54:48 +0000686/// ParseOperation - Parse an operator. This returns null on error.
687///
688/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
689///
David Greene05bce0b2011-07-29 22:43:06 +0000690Init *TGParser::ParseOperation(Record *CurRec) {
David Greened418c1b2009-05-14 20:54:48 +0000691 switch (Lex.getCode()) {
692 default:
693 TokError("unknown operation");
694 return 0;
695 break;
David Greene1434f662011-01-07 17:05:37 +0000696 case tgtok::XHead:
697 case tgtok::XTail:
698 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +0000699 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
700 UnOpInit::UnaryOp Code;
701 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000702
David Greenee6c27de2009-05-14 21:22:49 +0000703 switch (Lex.getCode()) {
704 default: assert(0 && "Unhandled code!");
705 case tgtok::XCast:
706 Lex.Lex(); // eat the operation
707 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000708
David Greenee6c27de2009-05-14 21:22:49 +0000709 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000710
David Greenee6c27de2009-05-14 21:22:49 +0000711 if (Type == 0) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000712 TokError("did not get type for unary operator");
David Greenee6c27de2009-05-14 21:22:49 +0000713 return 0;
714 }
David Greened418c1b2009-05-14 20:54:48 +0000715
David Greenee6c27de2009-05-14 21:22:49 +0000716 break;
David Greene1434f662011-01-07 17:05:37 +0000717 case tgtok::XHead:
David Greene5f9f9ba2009-05-14 22:38:31 +0000718 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000719 Code = UnOpInit::HEAD;
David Greene5f9f9ba2009-05-14 22:38:31 +0000720 break;
David Greene1434f662011-01-07 17:05:37 +0000721 case tgtok::XTail:
David Greene5f9f9ba2009-05-14 22:38:31 +0000722 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000723 Code = UnOpInit::TAIL;
David Greene5f9f9ba2009-05-14 22:38:31 +0000724 break;
David Greene1434f662011-01-07 17:05:37 +0000725 case tgtok::XEmpty:
David Greene5f9f9ba2009-05-14 22:38:31 +0000726 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000727 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000728 Type = IntRecTy::get();
David Greene5f9f9ba2009-05-14 22:38:31 +0000729 break;
David Greenee6c27de2009-05-14 21:22:49 +0000730 }
731 if (Lex.getCode() != tgtok::l_paren) {
732 TokError("expected '(' after unary operator");
733 return 0;
734 }
735 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000736
David Greene05bce0b2011-07-29 22:43:06 +0000737 Init *LHS = ParseValue(CurRec);
David Greenee6c27de2009-05-14 21:22:49 +0000738 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000739
David Greene1434f662011-01-07 17:05:37 +0000740 if (Code == UnOpInit::HEAD
741 || Code == UnOpInit::TAIL
742 || Code == UnOpInit::EMPTY) {
David Greene05bce0b2011-07-29 22:43:06 +0000743 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
744 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
745 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000746 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
747 TokError("expected list or string type argument in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000748 return 0;
749 }
750 if (LHSt) {
751 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
David Greenee1b46912009-06-08 20:23:18 +0000752 StringRecTy *SType = dynamic_cast<StringRecTy*>(LHSt->getType());
753 if (LType == 0 && SType == 0) {
754 TokError("expected list or string type argumnet in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000755 return 0;
756 }
757 }
758
David Greene1434f662011-01-07 17:05:37 +0000759 if (Code == UnOpInit::HEAD
760 || Code == UnOpInit::TAIL) {
David Greenee1b46912009-06-08 20:23:18 +0000761 if (LHSl == 0 && LHSt == 0) {
762 TokError("expected list type argumnet in unary operator");
763 return 0;
764 }
Bob Wilson21870412009-11-22 04:24:42 +0000765
David Greene5f9f9ba2009-05-14 22:38:31 +0000766 if (LHSl && LHSl->getSize() == 0) {
767 TokError("empty list argument in unary operator");
768 return 0;
769 }
770 if (LHSl) {
David Greene05bce0b2011-07-29 22:43:06 +0000771 Init *Item = LHSl->getElement(0);
772 TypedInit *Itemt = dynamic_cast<TypedInit*>(Item);
David Greene5f9f9ba2009-05-14 22:38:31 +0000773 if (Itemt == 0) {
774 TokError("untyped list element in unary operator");
775 return 0;
776 }
David Greene1434f662011-01-07 17:05:37 +0000777 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000778 Type = Itemt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000779 } else {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000780 Type = ListRecTy::get(Itemt->getType());
David Greene5f9f9ba2009-05-14 22:38:31 +0000781 }
Bob Wilson21870412009-11-22 04:24:42 +0000782 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000783 assert(LHSt && "expected list type argument in unary operator");
784 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
785 if (LType == 0) {
786 TokError("expected list type argumnet in unary operator");
787 return 0;
788 }
David Greene1434f662011-01-07 17:05:37 +0000789 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000790 Type = LType->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +0000791 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000792 Type = LType;
793 }
794 }
795 }
796 }
797
David Greenee6c27de2009-05-14 21:22:49 +0000798 if (Lex.getCode() != tgtok::r_paren) {
799 TokError("expected ')' in unary operator");
800 return 0;
801 }
802 Lex.Lex(); // eat the ')'
David Greenedcd35c72011-07-29 19:07:07 +0000803 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee6c27de2009-05-14 21:22:49 +0000804 }
David Greened418c1b2009-05-14 20:54:48 +0000805
806 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +0000807 case tgtok::XSRA:
David Greened418c1b2009-05-14 20:54:48 +0000808 case tgtok::XSRL:
809 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +0000810 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +0000811 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000812 tgtok::TokKind OpTok = Lex.getCode();
813 SMLoc OpLoc = Lex.getLoc();
814 Lex.Lex(); // eat the operation
815
David Greened418c1b2009-05-14 20:54:48 +0000816 BinOpInit::BinaryOp Code;
817 RecTy *Type = 0;
818
Chris Lattner8d978a72010-10-05 23:58:18 +0000819 switch (OpTok) {
David Greened418c1b2009-05-14 20:54:48 +0000820 default: assert(0 && "Unhandled code!");
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000821 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
822 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
823 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
824 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
825 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Bob Wilson21870412009-11-22 04:24:42 +0000826 case tgtok::XStrConcat:
David Greened418c1b2009-05-14 20:54:48 +0000827 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000828 Type = StringRecTy::get();
David Greened418c1b2009-05-14 20:54:48 +0000829 break;
David Greened418c1b2009-05-14 20:54:48 +0000830 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000831
David Greened418c1b2009-05-14 20:54:48 +0000832 if (Lex.getCode() != tgtok::l_paren) {
833 TokError("expected '(' after binary operator");
834 return 0;
835 }
836 Lex.Lex(); // eat the '('
837
David Greene05bce0b2011-07-29 22:43:06 +0000838 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000839
Chris Lattner8d978a72010-10-05 23:58:18 +0000840 InitList.push_back(ParseValue(CurRec));
841 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000842
Chris Lattner8d978a72010-10-05 23:58:18 +0000843 while (Lex.getCode() == tgtok::comma) {
844 Lex.Lex(); // eat the ','
845
846 InitList.push_back(ParseValue(CurRec));
847 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000848 }
David Greened418c1b2009-05-14 20:54:48 +0000849
850 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner8d978a72010-10-05 23:58:18 +0000851 TokError("expected ')' in operator");
David Greened418c1b2009-05-14 20:54:48 +0000852 return 0;
853 }
854 Lex.Lex(); // eat the ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000855
856 // We allow multiple operands to associative operators like !strconcat as
857 // shorthand for nesting them.
858 if (Code == BinOpInit::STRCONCAT) {
859 while (InitList.size() > 2) {
David Greene05bce0b2011-07-29 22:43:06 +0000860 Init *RHS = InitList.pop_back_val();
David Greenedcd35c72011-07-29 19:07:07 +0000861 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
862 ->Fold(CurRec, CurMultiClass);
Chris Lattner8d978a72010-10-05 23:58:18 +0000863 InitList.back() = RHS;
864 }
865 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000866
Chris Lattner8d978a72010-10-05 23:58:18 +0000867 if (InitList.size() == 2)
David Greenedcd35c72011-07-29 19:07:07 +0000868 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner8d978a72010-10-05 23:58:18 +0000869 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000870
Chris Lattner8d978a72010-10-05 23:58:18 +0000871 Error(OpLoc, "expected two operands to operator");
872 return 0;
David Greened418c1b2009-05-14 20:54:48 +0000873 }
874
David Greene9bea7c82009-05-14 23:26:46 +0000875 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +0000876 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +0000877 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
878 TernOpInit::TernaryOp Code;
879 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000880
David Greene4afc5092009-05-14 21:54:42 +0000881 tgtok::TokKind LexCode = Lex.getCode();
882 Lex.Lex(); // eat the operation
883 switch (LexCode) {
884 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000885 case tgtok::XIf:
886 Code = TernOpInit::IF;
887 break;
David Greenebeb31a52009-05-14 22:23:47 +0000888 case tgtok::XForEach:
889 Code = TernOpInit::FOREACH;
890 break;
David Greene4afc5092009-05-14 21:54:42 +0000891 case tgtok::XSubst:
892 Code = TernOpInit::SUBST;
893 break;
894 }
895 if (Lex.getCode() != tgtok::l_paren) {
896 TokError("expected '(' after ternary operator");
897 return 0;
898 }
899 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000900
David Greene05bce0b2011-07-29 22:43:06 +0000901 Init *LHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +0000902 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000903
David Greene4afc5092009-05-14 21:54:42 +0000904 if (Lex.getCode() != tgtok::comma) {
905 TokError("expected ',' in ternary operator");
906 return 0;
907 }
908 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000909
David Greene05bce0b2011-07-29 22:43:06 +0000910 Init *MHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +0000911 if (MHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000912
David Greene4afc5092009-05-14 21:54:42 +0000913 if (Lex.getCode() != tgtok::comma) {
914 TokError("expected ',' in ternary operator");
915 return 0;
916 }
917 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000918
David Greene05bce0b2011-07-29 22:43:06 +0000919 Init *RHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +0000920 if (RHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000921
David Greene4afc5092009-05-14 21:54:42 +0000922 if (Lex.getCode() != tgtok::r_paren) {
923 TokError("expected ')' in binary operator");
924 return 0;
925 }
926 Lex.Lex(); // eat the ')'
David Greened418c1b2009-05-14 20:54:48 +0000927
David Greene4afc5092009-05-14 21:54:42 +0000928 switch (LexCode) {
929 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000930 case tgtok::XIf: {
Bill Wendling548f5a02010-12-13 01:46:19 +0000931 // FIXME: The `!if' operator doesn't handle non-TypedInit well at
932 // all. This can be made much more robust.
David Greene05bce0b2011-07-29 22:43:06 +0000933 TypedInit *MHSt = dynamic_cast<TypedInit*>(MHS);
934 TypedInit *RHSt = dynamic_cast<TypedInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +0000935
936 RecTy *MHSTy = 0;
937 RecTy *RHSTy = 0;
938
939 if (MHSt == 0 && RHSt == 0) {
David Greene05bce0b2011-07-29 22:43:06 +0000940 BitsInit *MHSbits = dynamic_cast<BitsInit*>(MHS);
941 BitsInit *RHSbits = dynamic_cast<BitsInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +0000942
943 if (MHSbits && RHSbits &&
944 MHSbits->getNumBits() == RHSbits->getNumBits()) {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000945 Type = BitRecTy::get();
Bill Wendling548f5a02010-12-13 01:46:19 +0000946 break;
947 } else {
David Greene05bce0b2011-07-29 22:43:06 +0000948 BitInit *MHSbit = dynamic_cast<BitInit*>(MHS);
949 BitInit *RHSbit = dynamic_cast<BitInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +0000950
951 if (MHSbit && RHSbit) {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000952 Type = BitRecTy::get();
Bill Wendling548f5a02010-12-13 01:46:19 +0000953 break;
954 }
955 }
956 } else if (MHSt != 0 && RHSt != 0) {
957 MHSTy = MHSt->getType();
958 RHSTy = RHSt->getType();
959 }
960
961 if (!MHSTy || !RHSTy) {
David Greene9bea7c82009-05-14 23:26:46 +0000962 TokError("could not get type for !if");
963 return 0;
964 }
Bill Wendling548f5a02010-12-13 01:46:19 +0000965
966 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
967 Type = RHSTy;
968 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
969 Type = MHSTy;
Bob Wilson21870412009-11-22 04:24:42 +0000970 } else {
David Greene9bea7c82009-05-14 23:26:46 +0000971 TokError("inconsistent types for !if");
972 return 0;
973 }
974 break;
975 }
David Greenebeb31a52009-05-14 22:23:47 +0000976 case tgtok::XForEach: {
David Greene05bce0b2011-07-29 22:43:06 +0000977 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
David Greenebeb31a52009-05-14 22:23:47 +0000978 if (MHSt == 0) {
979 TokError("could not get type for !foreach");
980 return 0;
981 }
982 Type = MHSt->getType();
983 break;
984 }
David Greene4afc5092009-05-14 21:54:42 +0000985 case tgtok::XSubst: {
David Greene05bce0b2011-07-29 22:43:06 +0000986 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
David Greene4afc5092009-05-14 21:54:42 +0000987 if (RHSt == 0) {
988 TokError("could not get type for !subst");
989 return 0;
990 }
991 Type = RHSt->getType();
992 break;
993 }
994 }
David Greenedcd35c72011-07-29 19:07:07 +0000995 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson21870412009-11-22 04:24:42 +0000996 CurMultiClass);
David Greene4afc5092009-05-14 21:54:42 +0000997 }
David Greened418c1b2009-05-14 20:54:48 +0000998 }
999 TokError("could not parse operation");
1000 return 0;
1001}
1002
1003/// ParseOperatorType - Parse a type for an operator. This returns
1004/// null on error.
1005///
1006/// OperatorType ::= '<' Type '>'
1007///
Dan Gohmana9ad0412009-08-12 22:10:57 +00001008RecTy *TGParser::ParseOperatorType() {
David Greened418c1b2009-05-14 20:54:48 +00001009 RecTy *Type = 0;
1010
1011 if (Lex.getCode() != tgtok::less) {
1012 TokError("expected type name for operator");
1013 return 0;
1014 }
1015 Lex.Lex(); // eat the <
1016
1017 Type = ParseType();
1018
1019 if (Type == 0) {
1020 TokError("expected type name for operator");
1021 return 0;
1022 }
1023
1024 if (Lex.getCode() != tgtok::greater) {
1025 TokError("expected type name for operator");
1026 return 0;
1027 }
1028 Lex.Lex(); // eat the >
1029
1030 return Type;
1031}
1032
1033
Chris Lattnerf4601652007-11-22 20:49:04 +00001034/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1035///
1036/// SimpleValue ::= IDValue
1037/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001038/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +00001039/// SimpleValue ::= CODEFRAGMENT
1040/// SimpleValue ::= '?'
1041/// SimpleValue ::= '{' ValueList '}'
1042/// SimpleValue ::= ID '<' ValueListNE '>'
1043/// SimpleValue ::= '[' ValueList ']'
1044/// SimpleValue ::= '(' IDValue DagArgList ')'
1045/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
1046/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1047/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1048/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1049/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1050///
David Greene05bce0b2011-07-29 22:43:06 +00001051Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType) {
1052 Init *R = 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001053 switch (Lex.getCode()) {
1054 default: TokError("Unknown token when parsing a value"); break;
David Greenedcd35c72011-07-29 19:07:07 +00001055 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001056 case tgtok::StrVal: {
1057 std::string Val = Lex.getCurStrVal();
1058 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001059
Jim Grosbachda4231f2009-03-26 16:17:51 +00001060 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001061 while (Lex.getCode() == tgtok::StrVal) {
1062 Val += Lex.getCurStrVal();
1063 Lex.Lex();
1064 }
Bob Wilson21870412009-11-22 04:24:42 +00001065
David Greenedcd35c72011-07-29 19:07:07 +00001066 R = StringInit::get(Val);
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001067 break;
1068 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001069 case tgtok::CodeFragment:
David Greenedcd35c72011-07-29 19:07:07 +00001070 R = CodeInit::get(Lex.getCurStrVal());
Chris Lattner578bcf02010-10-06 04:31:40 +00001071 Lex.Lex();
1072 break;
1073 case tgtok::question:
David Greenedcd35c72011-07-29 19:07:07 +00001074 R = UnsetInit::get();
Chris Lattner578bcf02010-10-06 04:31:40 +00001075 Lex.Lex();
1076 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001077 case tgtok::Id: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001078 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001079 std::string Name = Lex.getCurStrVal();
1080 if (Lex.Lex() != tgtok::less) // consume the Id.
1081 return ParseIDValue(CurRec, Name, NameLoc); // Value ::= IDValue
Bob Wilson21870412009-11-22 04:24:42 +00001082
Chris Lattnerf4601652007-11-22 20:49:04 +00001083 // Value ::= ID '<' ValueListNE '>'
1084 if (Lex.Lex() == tgtok::greater) {
1085 TokError("expected non-empty value list");
1086 return 0;
1087 }
David Greenee1b46912009-06-08 20:23:18 +00001088
Chris Lattnerf4601652007-11-22 20:49:04 +00001089 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1090 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1091 // body.
1092 Record *Class = Records.getClass(Name);
1093 if (!Class) {
1094 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1095 return 0;
1096 }
David Greenee1b46912009-06-08 20:23:18 +00001097
David Greene05bce0b2011-07-29 22:43:06 +00001098 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
David Greenee1b46912009-06-08 20:23:18 +00001099 if (ValueList.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001100
David Greenee1b46912009-06-08 20:23:18 +00001101 if (Lex.getCode() != tgtok::greater) {
1102 TokError("expected '>' at end of value list");
1103 return 0;
1104 }
1105 Lex.Lex(); // eat the '>'
Bob Wilson21870412009-11-22 04:24:42 +00001106
Chris Lattnerf4601652007-11-22 20:49:04 +00001107 // Create the new record, set it as CurRec temporarily.
1108 static unsigned AnonCounter = 0;
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001109 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),
1110 NameLoc,
1111 Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001112 SubClassReference SCRef;
1113 SCRef.RefLoc = NameLoc;
1114 SCRef.Rec = Class;
1115 SCRef.TemplateArgs = ValueList;
1116 // Add info about the subclass to NewRec.
1117 if (AddSubClass(NewRec, SCRef))
1118 return 0;
1119 NewRec->resolveReferences();
1120 Records.addDef(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001121
Chris Lattnerf4601652007-11-22 20:49:04 +00001122 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +00001123 return DefInit::get(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001124 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001125 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001126 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001127 Lex.Lex(); // eat the '{'
David Greene05bce0b2011-07-29 22:43:06 +00001128 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001129
Chris Lattnerf4601652007-11-22 20:49:04 +00001130 if (Lex.getCode() != tgtok::r_brace) {
1131 Vals = ParseValueList(CurRec);
1132 if (Vals.empty()) return 0;
1133 }
1134 if (Lex.getCode() != tgtok::r_brace) {
1135 TokError("expected '}' at end of bit list value");
1136 return 0;
1137 }
1138 Lex.Lex(); // eat the '}'
Bob Wilson21870412009-11-22 04:24:42 +00001139
David Greene05bce0b2011-07-29 22:43:06 +00001140 SmallVector<Init *, 16> NewBits(Vals.size());
David Greeneca7fd3d2011-07-29 19:07:00 +00001141
Chris Lattnerf4601652007-11-22 20:49:04 +00001142 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001143 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Chris Lattnerf4601652007-11-22 20:49:04 +00001144 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +00001145 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1146 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +00001147 return 0;
1148 }
David Greeneca7fd3d2011-07-29 19:07:00 +00001149 NewBits[Vals.size()-i-1] = Bit;
Chris Lattnerf4601652007-11-22 20:49:04 +00001150 }
David Greenedcd35c72011-07-29 19:07:07 +00001151 return BitsInit::get(NewBits);
Chris Lattnerf4601652007-11-22 20:49:04 +00001152 }
1153 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1154 Lex.Lex(); // eat the '['
David Greene05bce0b2011-07-29 22:43:06 +00001155 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001156
David Greenee1b46912009-06-08 20:23:18 +00001157 RecTy *DeducedEltTy = 0;
1158 ListRecTy *GivenListTy = 0;
Bob Wilson21870412009-11-22 04:24:42 +00001159
David Greenee1b46912009-06-08 20:23:18 +00001160 if (ItemType != 0) {
1161 ListRecTy *ListType = dynamic_cast<ListRecTy*>(ItemType);
1162 if (ListType == 0) {
1163 std::stringstream s;
Bob Wilson21870412009-11-22 04:24:42 +00001164 s << "Type mismatch for list, expected list type, got "
David Greenee1b46912009-06-08 20:23:18 +00001165 << ItemType->getAsString();
1166 TokError(s.str());
Jim Grosbach6a44ada2011-03-11 19:52:52 +00001167 return 0;
David Greenee1b46912009-06-08 20:23:18 +00001168 }
1169 GivenListTy = ListType;
Bob Wilson21870412009-11-22 04:24:42 +00001170 }
David Greenee1b46912009-06-08 20:23:18 +00001171
Chris Lattnerf4601652007-11-22 20:49:04 +00001172 if (Lex.getCode() != tgtok::r_square) {
Bob Wilson21870412009-11-22 04:24:42 +00001173 Vals = ParseValueList(CurRec, 0,
1174 GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001175 if (Vals.empty()) return 0;
1176 }
1177 if (Lex.getCode() != tgtok::r_square) {
1178 TokError("expected ']' at end of list value");
1179 return 0;
1180 }
1181 Lex.Lex(); // eat the ']'
David Greenee1b46912009-06-08 20:23:18 +00001182
1183 RecTy *GivenEltTy = 0;
1184 if (Lex.getCode() == tgtok::less) {
1185 // Optional list element type
1186 Lex.Lex(); // eat the '<'
1187
1188 GivenEltTy = ParseType();
1189 if (GivenEltTy == 0) {
1190 // Couldn't parse element type
1191 return 0;
1192 }
1193
1194 if (Lex.getCode() != tgtok::greater) {
1195 TokError("expected '>' at end of list element type");
1196 return 0;
1197 }
1198 Lex.Lex(); // eat the '>'
1199 }
1200
1201 // Check elements
1202 RecTy *EltTy = 0;
David Greene05bce0b2011-07-29 22:43:06 +00001203 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greenee1b46912009-06-08 20:23:18 +00001204 i != ie;
1205 ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001206 TypedInit *TArg = dynamic_cast<TypedInit*>(*i);
David Greenee1b46912009-06-08 20:23:18 +00001207 if (TArg == 0) {
1208 TokError("Untyped list element");
1209 return 0;
1210 }
1211 if (EltTy != 0) {
1212 EltTy = resolveTypes(EltTy, TArg->getType());
1213 if (EltTy == 0) {
1214 TokError("Incompatible types in list elements");
1215 return 0;
1216 }
Bob Wilson21870412009-11-22 04:24:42 +00001217 } else {
David Greenee1b46912009-06-08 20:23:18 +00001218 EltTy = TArg->getType();
1219 }
1220 }
1221
1222 if (GivenEltTy != 0) {
1223 if (EltTy != 0) {
1224 // Verify consistency
1225 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1226 TokError("Incompatible types in list elements");
1227 return 0;
1228 }
1229 }
1230 EltTy = GivenEltTy;
1231 }
1232
1233 if (EltTy == 0) {
1234 if (ItemType == 0) {
1235 TokError("No type for list");
1236 return 0;
1237 }
1238 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +00001239 } else {
David Greenee1b46912009-06-08 20:23:18 +00001240 // Make sure the deduced type is compatible with the given type
1241 if (GivenListTy) {
1242 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1243 TokError("Element type mismatch for list");
1244 return 0;
1245 }
1246 }
1247 DeducedEltTy = EltTy;
1248 }
Bob Wilson21870412009-11-22 04:24:42 +00001249
David Greenedcd35c72011-07-29 19:07:07 +00001250 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4601652007-11-22 20:49:04 +00001251 }
1252 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1253 Lex.Lex(); // eat the '('
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001254 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001255 TokError("expected identifier in dag init");
1256 return 0;
1257 }
Bob Wilson21870412009-11-22 04:24:42 +00001258
David Greene05bce0b2011-07-29 22:43:06 +00001259 Init *Operator = ParseValue(CurRec);
Chris Lattner578bcf02010-10-06 04:31:40 +00001260 if (Operator == 0) return 0;
David Greenec7cafcd2009-04-22 20:18:10 +00001261
Nate Begeman7cee8172009-03-19 05:21:56 +00001262 // If the operator name is present, parse it.
1263 std::string OperatorName;
1264 if (Lex.getCode() == tgtok::colon) {
1265 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1266 TokError("expected variable name in dag operator");
1267 return 0;
1268 }
1269 OperatorName = Lex.getCurStrVal();
1270 Lex.Lex(); // eat the VarName.
1271 }
Bob Wilson21870412009-11-22 04:24:42 +00001272
David Greene05bce0b2011-07-29 22:43:06 +00001273 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4601652007-11-22 20:49:04 +00001274 if (Lex.getCode() != tgtok::r_paren) {
1275 DagArgs = ParseDagArgList(CurRec);
1276 if (DagArgs.empty()) return 0;
1277 }
Bob Wilson21870412009-11-22 04:24:42 +00001278
Chris Lattnerf4601652007-11-22 20:49:04 +00001279 if (Lex.getCode() != tgtok::r_paren) {
1280 TokError("expected ')' in dag init");
1281 return 0;
1282 }
1283 Lex.Lex(); // eat the ')'
Bob Wilson21870412009-11-22 04:24:42 +00001284
David Greenedcd35c72011-07-29 19:07:07 +00001285 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4601652007-11-22 20:49:04 +00001286 }
Bob Wilson21870412009-11-22 04:24:42 +00001287
David Greene1434f662011-01-07 17:05:37 +00001288 case tgtok::XHead:
1289 case tgtok::XTail:
1290 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +00001291 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001292 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +00001293 case tgtok::XSRA:
Chris Lattnerf4601652007-11-22 20:49:04 +00001294 case tgtok::XSRL:
1295 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +00001296 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001297 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene9bea7c82009-05-14 23:26:46 +00001298 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +00001299 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001300 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greened418c1b2009-05-14 20:54:48 +00001301 return ParseOperation(CurRec);
Chris Lattnerf4601652007-11-22 20:49:04 +00001302 }
1303 }
Bob Wilson21870412009-11-22 04:24:42 +00001304
Chris Lattnerf4601652007-11-22 20:49:04 +00001305 return R;
1306}
1307
1308/// ParseValue - Parse a tblgen value. This returns null on error.
1309///
1310/// Value ::= SimpleValue ValueSuffix*
1311/// ValueSuffix ::= '{' BitList '}'
1312/// ValueSuffix ::= '[' BitList ']'
1313/// ValueSuffix ::= '.' ID
1314///
David Greene05bce0b2011-07-29 22:43:06 +00001315Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType) {
1316 Init *Result = ParseSimpleValue(CurRec, ItemType);
Chris Lattnerf4601652007-11-22 20:49:04 +00001317 if (Result == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001318
Chris Lattnerf4601652007-11-22 20:49:04 +00001319 // Parse the suffixes now if present.
1320 while (1) {
1321 switch (Lex.getCode()) {
1322 default: return Result;
1323 case tgtok::l_brace: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001324 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001325 Lex.Lex(); // eat the '{'
1326 std::vector<unsigned> Ranges = ParseRangeList();
1327 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001328
Chris Lattnerf4601652007-11-22 20:49:04 +00001329 // Reverse the bitlist.
1330 std::reverse(Ranges.begin(), Ranges.end());
1331 Result = Result->convertInitializerBitRange(Ranges);
1332 if (Result == 0) {
1333 Error(CurlyLoc, "Invalid bit range for value");
1334 return 0;
1335 }
Bob Wilson21870412009-11-22 04:24:42 +00001336
Chris Lattnerf4601652007-11-22 20:49:04 +00001337 // Eat the '}'.
1338 if (Lex.getCode() != tgtok::r_brace) {
1339 TokError("expected '}' at end of bit range list");
1340 return 0;
1341 }
1342 Lex.Lex();
1343 break;
1344 }
1345 case tgtok::l_square: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001346 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001347 Lex.Lex(); // eat the '['
1348 std::vector<unsigned> Ranges = ParseRangeList();
1349 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001350
Chris Lattnerf4601652007-11-22 20:49:04 +00001351 Result = Result->convertInitListSlice(Ranges);
1352 if (Result == 0) {
1353 Error(SquareLoc, "Invalid range for list slice");
1354 return 0;
1355 }
Bob Wilson21870412009-11-22 04:24:42 +00001356
Chris Lattnerf4601652007-11-22 20:49:04 +00001357 // Eat the ']'.
1358 if (Lex.getCode() != tgtok::r_square) {
1359 TokError("expected ']' at end of list slice");
1360 return 0;
1361 }
1362 Lex.Lex();
1363 break;
1364 }
1365 case tgtok::period:
1366 if (Lex.Lex() != tgtok::Id) { // eat the .
1367 TokError("expected field identifier after '.'");
1368 return 0;
1369 }
1370 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001371 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00001372 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +00001373 return 0;
1374 }
David Greenedcd35c72011-07-29 19:07:07 +00001375 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001376 Lex.Lex(); // eat field name
1377 break;
1378 }
1379 }
1380}
1381
1382/// ParseDagArgList - Parse the argument list for a dag literal expression.
1383///
1384/// ParseDagArgList ::= Value (':' VARNAME)?
1385/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
David Greene05bce0b2011-07-29 22:43:06 +00001386std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4601652007-11-22 20:49:04 +00001387TGParser::ParseDagArgList(Record *CurRec) {
David Greene05bce0b2011-07-29 22:43:06 +00001388 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson21870412009-11-22 04:24:42 +00001389
Chris Lattnerf4601652007-11-22 20:49:04 +00001390 while (1) {
David Greene05bce0b2011-07-29 22:43:06 +00001391 Init *Val = ParseValue(CurRec);
1392 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
Bob Wilson21870412009-11-22 04:24:42 +00001393
Chris Lattnerf4601652007-11-22 20:49:04 +00001394 // If the variable name is present, add it.
1395 std::string VarName;
1396 if (Lex.getCode() == tgtok::colon) {
1397 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1398 TokError("expected variable name in dag literal");
David Greene05bce0b2011-07-29 22:43:06 +00001399 return std::vector<std::pair<llvm::Init*, std::string> >();
Chris Lattnerf4601652007-11-22 20:49:04 +00001400 }
1401 VarName = Lex.getCurStrVal();
1402 Lex.Lex(); // eat the VarName.
1403 }
Bob Wilson21870412009-11-22 04:24:42 +00001404
Chris Lattnerf4601652007-11-22 20:49:04 +00001405 Result.push_back(std::make_pair(Val, VarName));
Bob Wilson21870412009-11-22 04:24:42 +00001406
Chris Lattnerf4601652007-11-22 20:49:04 +00001407 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson21870412009-11-22 04:24:42 +00001408 Lex.Lex(); // eat the ','
Chris Lattnerf4601652007-11-22 20:49:04 +00001409 }
Bob Wilson21870412009-11-22 04:24:42 +00001410
Chris Lattnerf4601652007-11-22 20:49:04 +00001411 return Result;
1412}
1413
1414
1415/// ParseValueList - Parse a comma separated list of values, returning them as a
1416/// vector. Note that this always expects to be able to parse at least one
1417/// value. It returns an empty list if this is not possible.
1418///
1419/// ValueList ::= Value (',' Value)
1420///
David Greene05bce0b2011-07-29 22:43:06 +00001421std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopherd568b3f2011-07-11 23:06:52 +00001422 RecTy *EltTy) {
David Greene05bce0b2011-07-29 22:43:06 +00001423 std::vector<Init*> Result;
David Greenee1b46912009-06-08 20:23:18 +00001424 RecTy *ItemType = EltTy;
David Greene67acdf22009-06-29 19:59:52 +00001425 unsigned int ArgN = 0;
David Greenee1b46912009-06-08 20:23:18 +00001426 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001427 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greenee1b46912009-06-08 20:23:18 +00001428 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greened9746fe2011-09-19 18:26:07 +00001429 if (!RV) {
1430 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1431 << ")\n";
1432 }
David Greenee1b46912009-06-08 20:23:18 +00001433 assert(RV && "Template argument record not found??");
1434 ItemType = RV->getType();
1435 ++ArgN;
1436 }
1437 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001438 if (Result.back() == 0) return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001439
Chris Lattnerf4601652007-11-22 20:49:04 +00001440 while (Lex.getCode() == tgtok::comma) {
1441 Lex.Lex(); // Eat the comma
Bob Wilson21870412009-11-22 04:24:42 +00001442
David Greenee1b46912009-06-08 20:23:18 +00001443 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001444 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greene67acdf22009-06-29 19:59:52 +00001445 if (ArgN >= TArgs.size()) {
1446 TokError("too many template arguments");
David Greene05bce0b2011-07-29 22:43:06 +00001447 return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001448 }
David Greenee1b46912009-06-08 20:23:18 +00001449 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1450 assert(RV && "Template argument record not found??");
1451 ItemType = RV->getType();
1452 ++ArgN;
1453 }
1454 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001455 if (Result.back() == 0) return std::vector<Init*>();
Chris Lattnerf4601652007-11-22 20:49:04 +00001456 }
Bob Wilson21870412009-11-22 04:24:42 +00001457
Chris Lattnerf4601652007-11-22 20:49:04 +00001458 return Result;
1459}
1460
1461
Chris Lattnerf4601652007-11-22 20:49:04 +00001462/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1463/// empty string on error. This can happen in a number of different context's,
1464/// including within a def or in the template args for a def (which which case
1465/// CurRec will be non-null) and within the template args for a multiclass (in
1466/// which case CurRec will be null, but CurMultiClass will be set). This can
1467/// also happen within a def that is within a multiclass, which will set both
1468/// CurRec and CurMultiClass.
1469///
1470/// Declaration ::= FIELD? Type ID ('=' Value)?
1471///
David Greenee22b3212011-10-19 13:02:42 +00001472Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4601652007-11-22 20:49:04 +00001473 bool ParsingTemplateArgs) {
1474 // Read the field prefix if present.
1475 bool HasField = Lex.getCode() == tgtok::Field;
1476 if (HasField) Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001477
Chris Lattnerf4601652007-11-22 20:49:04 +00001478 RecTy *Type = ParseType();
David Greenee22b3212011-10-19 13:02:42 +00001479 if (Type == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001480
Chris Lattnerf4601652007-11-22 20:49:04 +00001481 if (Lex.getCode() != tgtok::Id) {
1482 TokError("Expected identifier in declaration");
David Greenee22b3212011-10-19 13:02:42 +00001483 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001484 }
Bob Wilson21870412009-11-22 04:24:42 +00001485
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001486 SMLoc IdLoc = Lex.getLoc();
David Greenee22b3212011-10-19 13:02:42 +00001487 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001488 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001489
Chris Lattnerf4601652007-11-22 20:49:04 +00001490 if (ParsingTemplateArgs) {
1491 if (CurRec) {
David Greenee22b3212011-10-19 13:02:42 +00001492 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4601652007-11-22 20:49:04 +00001493 } else {
1494 assert(CurMultiClass);
1495 }
1496 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +00001497 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1498 "::");
Chris Lattnerf4601652007-11-22 20:49:04 +00001499 }
Bob Wilson21870412009-11-22 04:24:42 +00001500
Chris Lattnerf4601652007-11-22 20:49:04 +00001501 // Add the value.
1502 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
David Greenee22b3212011-10-19 13:02:42 +00001503 return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001504
Chris Lattnerf4601652007-11-22 20:49:04 +00001505 // If a value is present, parse it.
1506 if (Lex.getCode() == tgtok::equal) {
1507 Lex.Lex();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001508 SMLoc ValLoc = Lex.getLoc();
David Greene05bce0b2011-07-29 22:43:06 +00001509 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001510 if (Val == 0 ||
1511 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
David Greenee22b3212011-10-19 13:02:42 +00001512 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001513 }
Bob Wilson21870412009-11-22 04:24:42 +00001514
Chris Lattnerf4601652007-11-22 20:49:04 +00001515 return DeclName;
1516}
1517
1518/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1519/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1520/// template args for a def, which may or may not be in a multiclass. If null,
1521/// these are the template args for a multiclass.
1522///
1523/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson21870412009-11-22 04:24:42 +00001524///
Chris Lattnerf4601652007-11-22 20:49:04 +00001525bool TGParser::ParseTemplateArgList(Record *CurRec) {
1526 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1527 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +00001528
Chris Lattnerf4601652007-11-22 20:49:04 +00001529 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +00001530
Chris Lattnerf4601652007-11-22 20:49:04 +00001531 // Read the first declaration.
David Greenee22b3212011-10-19 13:02:42 +00001532 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1533 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001534 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001535
Chris Lattnerf4601652007-11-22 20:49:04 +00001536 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson21870412009-11-22 04:24:42 +00001537
Chris Lattnerf4601652007-11-22 20:49:04 +00001538 while (Lex.getCode() == tgtok::comma) {
1539 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001540
Chris Lattnerf4601652007-11-22 20:49:04 +00001541 // Read the following declarations.
1542 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
David Greenee22b3212011-10-19 13:02:42 +00001543 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001544 return true;
1545 TheRecToAddTo->addTemplateArg(TemplArg);
1546 }
Bob Wilson21870412009-11-22 04:24:42 +00001547
Chris Lattnerf4601652007-11-22 20:49:04 +00001548 if (Lex.getCode() != tgtok::greater)
1549 return TokError("expected '>' at end of template argument list");
1550 Lex.Lex(); // eat the '>'.
1551 return false;
1552}
1553
1554
1555/// ParseBodyItem - Parse a single item at within the body of a def or class.
1556///
1557/// BodyItem ::= Declaration ';'
1558/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1559bool TGParser::ParseBodyItem(Record *CurRec) {
1560 if (Lex.getCode() != tgtok::Let) {
David Greenee22b3212011-10-19 13:02:42 +00001561 if (ParseDeclaration(CurRec, false) == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001562 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001563
Chris Lattnerf4601652007-11-22 20:49:04 +00001564 if (Lex.getCode() != tgtok::semi)
1565 return TokError("expected ';' after declaration");
1566 Lex.Lex();
1567 return false;
1568 }
1569
1570 // LET ID OptionalRangeList '=' Value ';'
1571 if (Lex.Lex() != tgtok::Id)
1572 return TokError("expected field identifier after let");
Bob Wilson21870412009-11-22 04:24:42 +00001573
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001574 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001575 std::string FieldName = Lex.getCurStrVal();
1576 Lex.Lex(); // eat the field name.
Bob Wilson21870412009-11-22 04:24:42 +00001577
Chris Lattnerf4601652007-11-22 20:49:04 +00001578 std::vector<unsigned> BitList;
Bob Wilson21870412009-11-22 04:24:42 +00001579 if (ParseOptionalBitList(BitList))
Chris Lattnerf4601652007-11-22 20:49:04 +00001580 return true;
1581 std::reverse(BitList.begin(), BitList.end());
Bob Wilson21870412009-11-22 04:24:42 +00001582
Chris Lattnerf4601652007-11-22 20:49:04 +00001583 if (Lex.getCode() != tgtok::equal)
1584 return TokError("expected '=' in let expression");
1585 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001586
David Greenee1b46912009-06-08 20:23:18 +00001587 RecordVal *Field = CurRec->getValue(FieldName);
1588 if (Field == 0)
1589 return TokError("Value '" + FieldName + "' unknown!");
1590
1591 RecTy *Type = Field->getType();
Bob Wilson21870412009-11-22 04:24:42 +00001592
David Greene05bce0b2011-07-29 22:43:06 +00001593 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001594 if (Val == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001595
Chris Lattnerf4601652007-11-22 20:49:04 +00001596 if (Lex.getCode() != tgtok::semi)
1597 return TokError("expected ';' after let expression");
1598 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001599
Chris Lattnerf4601652007-11-22 20:49:04 +00001600 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1601}
1602
1603/// ParseBody - Read the body of a class or def. Return true on error, false on
1604/// success.
1605///
1606/// Body ::= ';'
1607/// Body ::= '{' BodyList '}'
1608/// BodyList BodyItem*
1609///
1610bool TGParser::ParseBody(Record *CurRec) {
1611 // If this is a null definition, just eat the semi and return.
1612 if (Lex.getCode() == tgtok::semi) {
1613 Lex.Lex();
1614 return false;
1615 }
Bob Wilson21870412009-11-22 04:24:42 +00001616
Chris Lattnerf4601652007-11-22 20:49:04 +00001617 if (Lex.getCode() != tgtok::l_brace)
1618 return TokError("Expected ';' or '{' to start body");
1619 // Eat the '{'.
1620 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001621
Chris Lattnerf4601652007-11-22 20:49:04 +00001622 while (Lex.getCode() != tgtok::r_brace)
1623 if (ParseBodyItem(CurRec))
1624 return true;
1625
1626 // Eat the '}'.
1627 Lex.Lex();
1628 return false;
1629}
1630
1631/// ParseObjectBody - Parse the body of a def or class. This consists of an
1632/// optional ClassList followed by a Body. CurRec is the current def or class
1633/// that is being parsed.
1634///
1635/// ObjectBody ::= BaseClassList Body
1636/// BaseClassList ::= /*empty*/
1637/// BaseClassList ::= ':' BaseClassListNE
1638/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1639///
1640bool TGParser::ParseObjectBody(Record *CurRec) {
1641 // If there is a baseclass list, read it.
1642 if (Lex.getCode() == tgtok::colon) {
1643 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001644
Chris Lattnerf4601652007-11-22 20:49:04 +00001645 // Read all of the subclasses.
1646 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1647 while (1) {
1648 // Check for error.
1649 if (SubClass.Rec == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001650
Chris Lattnerf4601652007-11-22 20:49:04 +00001651 // Add it.
1652 if (AddSubClass(CurRec, SubClass))
1653 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001654
Chris Lattnerf4601652007-11-22 20:49:04 +00001655 if (Lex.getCode() != tgtok::comma) break;
1656 Lex.Lex(); // eat ','.
1657 SubClass = ParseSubClassReference(CurRec, false);
1658 }
1659 }
1660
1661 // Process any variables on the let stack.
1662 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1663 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1664 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1665 LetStack[i][j].Bits, LetStack[i][j].Value))
1666 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001667
Chris Lattnerf4601652007-11-22 20:49:04 +00001668 return ParseBody(CurRec);
1669}
1670
Chris Lattnerf4601652007-11-22 20:49:04 +00001671/// ParseDef - Parse and return a top level or multiclass def, return the record
1672/// corresponding to it. This returns null on error.
1673///
1674/// DefInst ::= DEF ObjectName ObjectBody
1675///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001676bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001677 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001678 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson21870412009-11-22 04:24:42 +00001679 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4601652007-11-22 20:49:04 +00001680
1681 // Parse ObjectName and make a record for it.
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001682 Record *CurRec = new Record(ParseObjectName(), DefLoc, Records);
Bob Wilson21870412009-11-22 04:24:42 +00001683
Chris Lattnerf4601652007-11-22 20:49:04 +00001684 if (!CurMultiClass) {
1685 // Top-level def definition.
Bob Wilson21870412009-11-22 04:24:42 +00001686
Chris Lattnerf4601652007-11-22 20:49:04 +00001687 // Ensure redefinition doesn't happen.
1688 if (Records.getDef(CurRec->getName())) {
1689 Error(DefLoc, "def '" + CurRec->getName() + "' already defined");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001690 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001691 }
1692 Records.addDef(CurRec);
1693 } else {
1694 // Otherwise, a def inside a multiclass, add it to the multiclass.
1695 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
1696 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
1697 Error(DefLoc, "def '" + CurRec->getName() +
1698 "' already defined in this multiclass!");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001699 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001700 }
1701 CurMultiClass->DefPrototypes.push_back(CurRec);
1702 }
Bob Wilson21870412009-11-22 04:24:42 +00001703
Chris Lattnerf4601652007-11-22 20:49:04 +00001704 if (ParseObjectBody(CurRec))
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001705 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001706
Chris Lattnerf4601652007-11-22 20:49:04 +00001707 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
David Greene0d886402011-08-10 18:27:46 +00001708 // See Record::setName(). This resolve step will see any new name
1709 // for the def that might have been created when resolving
1710 // inheritance, values and arguments above.
Chris Lattnerf4601652007-11-22 20:49:04 +00001711 CurRec->resolveReferences();
Bob Wilson21870412009-11-22 04:24:42 +00001712
Chris Lattnerf4601652007-11-22 20:49:04 +00001713 // If ObjectBody has template arguments, it's an error.
1714 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001715
1716 if (CurMultiClass) {
1717 // Copy the template arguments for the multiclass into the def.
David Greenee22b3212011-10-19 13:02:42 +00001718 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001719 CurMultiClass->Rec.getTemplateArgs();
1720
1721 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1722 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1723 assert(RV && "Template arg doesn't exist?");
1724 CurRec->addValue(*RV);
1725 }
1726 }
1727
1728 return false;
Chris Lattnerf4601652007-11-22 20:49:04 +00001729}
1730
Chris Lattnerf4601652007-11-22 20:49:04 +00001731/// ParseClass - Parse a tblgen class definition.
1732///
1733/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
1734///
1735bool TGParser::ParseClass() {
1736 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
1737 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001738
Chris Lattnerf4601652007-11-22 20:49:04 +00001739 if (Lex.getCode() != tgtok::Id)
1740 return TokError("expected class name after 'class' keyword");
Bob Wilson21870412009-11-22 04:24:42 +00001741
Chris Lattnerf4601652007-11-22 20:49:04 +00001742 Record *CurRec = Records.getClass(Lex.getCurStrVal());
1743 if (CurRec) {
1744 // If the body was previously defined, this is an error.
1745 if (!CurRec->getValues().empty() ||
1746 !CurRec->getSuperClasses().empty() ||
1747 !CurRec->getTemplateArgs().empty())
1748 return TokError("Class '" + CurRec->getName() + "' already defined");
1749 } else {
1750 // If this is the first reference to this class, create and add it.
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001751 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001752 Records.addClass(CurRec);
1753 }
1754 Lex.Lex(); // eat the name.
Bob Wilson21870412009-11-22 04:24:42 +00001755
Chris Lattnerf4601652007-11-22 20:49:04 +00001756 // If there are template args, parse them.
1757 if (Lex.getCode() == tgtok::less)
1758 if (ParseTemplateArgList(CurRec))
1759 return true;
1760
1761 // Finally, parse the object body.
1762 return ParseObjectBody(CurRec);
1763}
1764
1765/// ParseLetList - Parse a non-empty list of assignment expressions into a list
1766/// of LetRecords.
1767///
1768/// LetList ::= LetItem (',' LetItem)*
1769/// LetItem ::= ID OptionalRangeList '=' Value
1770///
1771std::vector<LetRecord> TGParser::ParseLetList() {
1772 std::vector<LetRecord> Result;
Bob Wilson21870412009-11-22 04:24:42 +00001773
Chris Lattnerf4601652007-11-22 20:49:04 +00001774 while (1) {
1775 if (Lex.getCode() != tgtok::Id) {
1776 TokError("expected identifier in let definition");
1777 return std::vector<LetRecord>();
1778 }
1779 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001780 SMLoc NameLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +00001781 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4601652007-11-22 20:49:04 +00001782
1783 // Check for an optional RangeList.
1784 std::vector<unsigned> Bits;
Bob Wilson21870412009-11-22 04:24:42 +00001785 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4601652007-11-22 20:49:04 +00001786 return std::vector<LetRecord>();
1787 std::reverse(Bits.begin(), Bits.end());
Bob Wilson21870412009-11-22 04:24:42 +00001788
Chris Lattnerf4601652007-11-22 20:49:04 +00001789 if (Lex.getCode() != tgtok::equal) {
1790 TokError("expected '=' in let expression");
1791 return std::vector<LetRecord>();
1792 }
1793 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001794
David Greene05bce0b2011-07-29 22:43:06 +00001795 Init *Val = ParseValue(0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001796 if (Val == 0) return std::vector<LetRecord>();
Bob Wilson21870412009-11-22 04:24:42 +00001797
Chris Lattnerf4601652007-11-22 20:49:04 +00001798 // Now that we have everything, add the record.
1799 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson21870412009-11-22 04:24:42 +00001800
Chris Lattnerf4601652007-11-22 20:49:04 +00001801 if (Lex.getCode() != tgtok::comma)
1802 return Result;
Bob Wilson21870412009-11-22 04:24:42 +00001803 Lex.Lex(); // eat the comma.
Chris Lattnerf4601652007-11-22 20:49:04 +00001804 }
1805}
1806
1807/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001808/// different related productions. This works inside multiclasses too.
Chris Lattnerf4601652007-11-22 20:49:04 +00001809///
1810/// Object ::= LET LetList IN '{' ObjectList '}'
1811/// Object ::= LET LetList IN Object
1812///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001813bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001814 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
1815 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001816
Chris Lattnerf4601652007-11-22 20:49:04 +00001817 // Add this entry to the let stack.
1818 std::vector<LetRecord> LetInfo = ParseLetList();
1819 if (LetInfo.empty()) return true;
1820 LetStack.push_back(LetInfo);
1821
1822 if (Lex.getCode() != tgtok::In)
1823 return TokError("expected 'in' at end of top-level 'let'");
1824 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001825
Chris Lattnerf4601652007-11-22 20:49:04 +00001826 // If this is a scalar let, just handle it now
1827 if (Lex.getCode() != tgtok::l_brace) {
1828 // LET LetList IN Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001829 if (ParseObject(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00001830 return true;
1831 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001832 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001833 // Otherwise, this is a group let.
1834 Lex.Lex(); // eat the '{'.
Bob Wilson21870412009-11-22 04:24:42 +00001835
Chris Lattnerf4601652007-11-22 20:49:04 +00001836 // Parse the object list.
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001837 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00001838 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001839
Chris Lattnerf4601652007-11-22 20:49:04 +00001840 if (Lex.getCode() != tgtok::r_brace) {
1841 TokError("expected '}' at end of top level let command");
1842 return Error(BraceLoc, "to match this '{'");
1843 }
1844 Lex.Lex();
1845 }
Bob Wilson21870412009-11-22 04:24:42 +00001846
Chris Lattnerf4601652007-11-22 20:49:04 +00001847 // Outside this let scope, this let block is not active.
1848 LetStack.pop_back();
1849 return false;
1850}
1851
Chris Lattnerf4601652007-11-22 20:49:04 +00001852/// ParseMultiClass - Parse a multiclass definition.
1853///
Bob Wilson32558652009-04-28 19:41:44 +00001854/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
1855/// ':' BaseMultiClassList '{' MultiClassDef+ '}'
Chris Lattnerf4601652007-11-22 20:49:04 +00001856///
1857bool TGParser::ParseMultiClass() {
1858 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
1859 Lex.Lex(); // Eat the multiclass token.
1860
1861 if (Lex.getCode() != tgtok::Id)
1862 return TokError("expected identifier after multiclass for name");
1863 std::string Name = Lex.getCurStrVal();
Bob Wilson21870412009-11-22 04:24:42 +00001864
Chris Lattnerf4601652007-11-22 20:49:04 +00001865 if (MultiClasses.count(Name))
1866 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson21870412009-11-22 04:24:42 +00001867
Chris Lattner67db8832010-12-13 00:23:57 +00001868 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
1869 Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001870 Lex.Lex(); // Eat the identifier.
Bob Wilson21870412009-11-22 04:24:42 +00001871
Chris Lattnerf4601652007-11-22 20:49:04 +00001872 // If there are template args, parse them.
1873 if (Lex.getCode() == tgtok::less)
1874 if (ParseTemplateArgList(0))
1875 return true;
1876
David Greened34a73b2009-04-24 16:55:41 +00001877 bool inherits = false;
1878
David Greenede444af2009-04-22 16:42:54 +00001879 // If there are submulticlasses, parse them.
1880 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00001881 inherits = true;
1882
David Greenede444af2009-04-22 16:42:54 +00001883 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00001884
David Greenede444af2009-04-22 16:42:54 +00001885 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00001886 SubMultiClassReference SubMultiClass =
1887 ParseSubMultiClassReference(CurMultiClass);
David Greenede444af2009-04-22 16:42:54 +00001888 while (1) {
1889 // Check for error.
1890 if (SubMultiClass.MC == 0) return true;
Bob Wilson32558652009-04-28 19:41:44 +00001891
David Greenede444af2009-04-22 16:42:54 +00001892 // Add it.
1893 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
1894 return true;
Bob Wilson32558652009-04-28 19:41:44 +00001895
David Greenede444af2009-04-22 16:42:54 +00001896 if (Lex.getCode() != tgtok::comma) break;
1897 Lex.Lex(); // eat ','.
1898 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
1899 }
1900 }
1901
David Greened34a73b2009-04-24 16:55:41 +00001902 if (Lex.getCode() != tgtok::l_brace) {
1903 if (!inherits)
1904 return TokError("expected '{' in multiclass definition");
Bob Wilson21870412009-11-22 04:24:42 +00001905 else if (Lex.getCode() != tgtok::semi)
1906 return TokError("expected ';' in multiclass definition");
David Greened34a73b2009-04-24 16:55:41 +00001907 else
Bob Wilson21870412009-11-22 04:24:42 +00001908 Lex.Lex(); // eat the ';'.
1909 } else {
David Greened34a73b2009-04-24 16:55:41 +00001910 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
1911 return TokError("multiclass must contain at least one def");
Bob Wilson21870412009-11-22 04:24:42 +00001912
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001913 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001914 switch (Lex.getCode()) {
1915 default:
David Greenea1b1b792011-10-07 18:25:05 +00001916 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001917 case tgtok::Let:
1918 case tgtok::Def:
1919 case tgtok::Defm:
1920 if (ParseObject(CurMultiClass))
1921 return true;
1922 break;
1923 }
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001924 }
David Greened34a73b2009-04-24 16:55:41 +00001925 Lex.Lex(); // eat the '}'.
1926 }
Bob Wilson21870412009-11-22 04:24:42 +00001927
Chris Lattnerf4601652007-11-22 20:49:04 +00001928 CurMultiClass = 0;
1929 return false;
1930}
1931
David Greenee499a2d2011-10-05 22:42:07 +00001932Record *TGParser::
1933InstantiateMulticlassDef(MultiClass &MC,
1934 Record *DefProto,
1935 const std::string &DefmPrefix,
1936 SMLoc DefmPrefixLoc) {
1937 // Add in the defm name. If the defm prefix is empty, give each
1938 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
1939 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
1940 // as a prefix.
1941 std::string DefName = DefProto->getName();
1942 if (DefmPrefix.empty()) {
1943 DefName = GetNewAnonymousName();
1944 } else {
1945 std::string::size_type idx = DefName.find("#NAME#");
1946 if (idx != std::string::npos) {
1947 DefName.replace(idx, 6, DefmPrefix);
1948 } else {
1949 // Add the suffix to the defm name to get the new name.
1950 DefName = DefmPrefix + DefName;
1951 }
1952 }
1953
1954 Record *CurRec = new Record(DefName, DefmPrefixLoc, Records);
1955
1956 SubClassReference Ref;
1957 Ref.RefLoc = DefmPrefixLoc;
1958 Ref.Rec = DefProto;
1959 AddSubClass(CurRec, Ref);
1960
1961 return CurRec;
1962}
1963
1964bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
1965 Record *CurRec,
1966 SMLoc DefmPrefixLoc,
1967 SMLoc SubClassLoc,
David Greenee22b3212011-10-19 13:02:42 +00001968 const std::vector<Init *> &TArgs,
David Greenee499a2d2011-10-05 22:42:07 +00001969 std::vector<Init *> &TemplateVals,
1970 bool DeleteArgs) {
1971 // Loop over all of the template arguments, setting them to the specified
1972 // value or leaving them as the default if necessary.
1973 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1974 // Check if a value is specified for this temp-arg.
1975 if (i < TemplateVals.size()) {
1976 // Set it now.
1977 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
1978 TemplateVals[i]))
1979 return true;
1980
1981 // Resolve it next.
1982 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
1983
1984 if (DeleteArgs)
1985 // Now remove it.
1986 CurRec->removeValue(TArgs[i]);
1987
1988 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
1989 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenee22b3212011-10-19 13:02:42 +00001990 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
1991 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
1992 + "'");
David Greenee499a2d2011-10-05 22:42:07 +00001993 }
1994 }
1995 return false;
1996}
1997
1998bool TGParser::ResolveMulticlassDef(MultiClass &MC,
1999 Record *CurRec,
2000 Record *DefProto,
2001 SMLoc DefmPrefixLoc) {
2002 // If the mdef is inside a 'let' expression, add to each def.
2003 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2004 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2005 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2006 LetStack[i][j].Bits, LetStack[i][j].Value))
2007 return Error(DefmPrefixLoc, "when instantiating this defm");
2008
2009 // Ensure redefinition doesn't happen.
2010 if (Records.getDef(CurRec->getName()))
2011 return Error(DefmPrefixLoc, "def '" + CurRec->getName() +
2012 "' already defined, instantiating defm with subdef '" +
2013 DefProto->getName() + "'");
2014
2015 // Don't create a top level definition for defm inside multiclasses,
2016 // instead, only update the prototypes and bind the template args
2017 // with the new created definition.
2018 if (CurMultiClass) {
2019 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2020 i != e; ++i)
2021 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName())
2022 return Error(DefmPrefixLoc, "defm '" + CurRec->getName() +
2023 "' already defined in this multiclass!");
2024 CurMultiClass->DefPrototypes.push_back(CurRec);
2025
2026 // Copy the template arguments for the multiclass into the new def.
David Greenee22b3212011-10-19 13:02:42 +00002027 const std::vector<Init *> &TA =
David Greenee499a2d2011-10-05 22:42:07 +00002028 CurMultiClass->Rec.getTemplateArgs();
2029
2030 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2031 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2032 assert(RV && "Template arg doesn't exist?");
2033 CurRec->addValue(*RV);
2034 }
2035 } else {
2036 Records.addDef(CurRec);
2037 }
2038
2039 return false;
2040}
2041
Chris Lattnerf4601652007-11-22 20:49:04 +00002042/// ParseDefm - Parse the instantiation of a multiclass.
2043///
2044/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2045///
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002046bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002047 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Bob Wilson21870412009-11-22 04:24:42 +00002048
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002049 std::string DefmPrefix;
2050 if (Lex.Lex() == tgtok::Id) { // eat the defm.
2051 DefmPrefix = Lex.getCurStrVal();
2052 Lex.Lex(); // Eat the defm prefix.
2053 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +00002054
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002055 SMLoc DefmPrefixLoc = Lex.getLoc();
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002056 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4601652007-11-22 20:49:04 +00002057 return TokError("expected ':' after defm identifier");
Bob Wilson21870412009-11-22 04:24:42 +00002058
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002059 // Keep track of the new generated record definitions.
2060 std::vector<Record*> NewRecDefs;
2061
2062 // This record also inherits from a regular class (non-multiclass)?
2063 bool InheritFromClass = false;
2064
Chris Lattnerf4601652007-11-22 20:49:04 +00002065 // eat the colon.
2066 Lex.Lex();
2067
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002068 SMLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002069 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene56546132009-04-22 22:17:51 +00002070
2071 while (1) {
2072 if (Ref.Rec == 0) return true;
2073
2074 // To instantiate a multiclass, we need to first get the multiclass, then
2075 // instantiate each def contained in the multiclass with the SubClassRef
2076 // template parameters.
2077 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2078 assert(MC && "Didn't lookup multiclass correctly?");
David Greene05bce0b2011-07-29 22:43:06 +00002079 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greene56546132009-04-22 22:17:51 +00002080
2081 // Verify that the correct number of template arguments were specified.
David Greenee22b3212011-10-19 13:02:42 +00002082 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greene56546132009-04-22 22:17:51 +00002083 if (TArgs.size() < TemplateVals.size())
2084 return Error(SubClassLoc,
2085 "more template args specified than multiclass expects");
2086
2087 // Loop over all the def's in the multiclass, instantiating each one.
2088 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2089 Record *DefProto = MC->DefPrototypes[i];
2090
David Greenee499a2d2011-10-05 22:42:07 +00002091 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix, DefmPrefixLoc);
David Greene065f2592009-05-05 16:28:25 +00002092
David Greenee499a2d2011-10-05 22:42:07 +00002093 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmPrefixLoc, SubClassLoc,
2094 TArgs, TemplateVals, true/*Delete args*/))
2095 return Error(SubClassLoc, "could not instantiate def");
David Greene56546132009-04-22 22:17:51 +00002096
David Greenee499a2d2011-10-05 22:42:07 +00002097 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmPrefixLoc))
2098 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002099
2100 NewRecDefs.push_back(CurRec);
David Greene56546132009-04-22 22:17:51 +00002101 }
2102
David Greenee499a2d2011-10-05 22:42:07 +00002103
David Greene56546132009-04-22 22:17:51 +00002104 if (Lex.getCode() != tgtok::comma) break;
2105 Lex.Lex(); // eat ','.
2106
2107 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002108
2109 // A defm can inherit from regular classes (non-multiclass) as
2110 // long as they come in the end of the inheritance list.
2111 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0);
2112
2113 if (InheritFromClass)
2114 break;
2115
David Greene56546132009-04-22 22:17:51 +00002116 Ref = ParseSubClassReference(0, true);
2117 }
2118
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002119 if (InheritFromClass) {
2120 // Process all the classes to inherit as if they were part of a
2121 // regular 'def' and inherit all record values.
2122 SubClassReference SubClass = ParseSubClassReference(0, false);
2123 while (1) {
2124 // Check for error.
2125 if (SubClass.Rec == 0) return true;
2126
2127 // Get the expanded definition prototypes and teach them about
2128 // the record values the current class to inherit has
2129 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2130 Record *CurRec = NewRecDefs[i];
2131
2132 // Add it.
2133 if (AddSubClass(CurRec, SubClass))
2134 return true;
2135
2136 // Process any variables on the let stack.
2137 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2138 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2139 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2140 LetStack[i][j].Bits, LetStack[i][j].Value))
2141 return true;
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002142 }
2143
2144 if (Lex.getCode() != tgtok::comma) break;
2145 Lex.Lex(); // eat ','.
2146 SubClass = ParseSubClassReference(0, false);
2147 }
2148 }
2149
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002150 if (!CurMultiClass)
2151 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene0d886402011-08-10 18:27:46 +00002152 // See Record::setName(). This resolve step will see any new
2153 // name for the def that might have been created when resolving
2154 // inheritance, values and arguments above.
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002155 NewRecDefs[i]->resolveReferences();
2156
Chris Lattnerf4601652007-11-22 20:49:04 +00002157 if (Lex.getCode() != tgtok::semi)
2158 return TokError("expected ';' at end of defm");
2159 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002160
Chris Lattnerf4601652007-11-22 20:49:04 +00002161 return false;
2162}
2163
2164/// ParseObject
2165/// Object ::= ClassInst
2166/// Object ::= DefInst
2167/// Object ::= MultiClassInst
2168/// Object ::= DefMInst
2169/// Object ::= LETCommand '{' ObjectList '}'
2170/// Object ::= LETCommand Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002171bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002172 switch (Lex.getCode()) {
Chris Lattnerd6d9dd92010-10-31 19:27:15 +00002173 default:
2174 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002175 case tgtok::Let: return ParseTopLevelLet(MC);
2176 case tgtok::Def: return ParseDef(MC);
2177 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4601652007-11-22 20:49:04 +00002178 case tgtok::Class: return ParseClass();
2179 case tgtok::MultiClass: return ParseMultiClass();
2180 }
2181}
2182
2183/// ParseObjectList
2184/// ObjectList :== Object*
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002185bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002186 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002187 if (ParseObject(MC))
Chris Lattnerf4601652007-11-22 20:49:04 +00002188 return true;
2189 }
2190 return false;
2191}
2192
Chris Lattnerf4601652007-11-22 20:49:04 +00002193bool TGParser::ParseFile() {
2194 Lex.Lex(); // Prime the lexer.
2195 if (ParseObjectList()) return true;
Bob Wilson21870412009-11-22 04:24:42 +00002196
Chris Lattnerf4601652007-11-22 20:49:04 +00002197 // If we have unread input at the end of the file, report it.
2198 if (Lex.getCode() == tgtok::Eof)
2199 return false;
Bob Wilson21870412009-11-22 04:24:42 +00002200
Chris Lattnerf4601652007-11-22 20:49:04 +00002201 return TokError("Unexpected input at top level");
2202}
2203