blob: fdf88d86ec42e2ee05065558e3ebaddcf400e61f [file] [log] [blame]
Chris Lattnerf4601652007-11-22 20:49:04 +00001//===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf4601652007-11-22 20:49:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Parser for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
14#include "TGParser.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000015#include "llvm/TableGen/Record.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000016#include "llvm/ADT/StringExtras.h"
Daniel Dunbar1a551802009-07-03 00:10:29 +000017#include <algorithm>
18#include <sstream>
Chris Lattner8d978a72010-10-05 23:58:18 +000019#include "llvm/ADT/SmallVector.h"
Chris Lattner46f55522010-10-06 04:55:48 +000020#include "llvm/Support/CommandLine.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000021using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24// Support Code for the Semantic Actions.
25//===----------------------------------------------------------------------===//
26
27namespace llvm {
Chris Lattnerf4601652007-11-22 20:49:04 +000028struct SubClassReference {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000029 SMLoc RefLoc;
Chris Lattnerf4601652007-11-22 20:49:04 +000030 Record *Rec;
David Greene05bce0b2011-07-29 22:43:06 +000031 std::vector<Init*> TemplateArgs;
Chris Lattner1c8ae592009-03-13 16:01:53 +000032 SubClassReference() : Rec(0) {}
David Greened34a73b2009-04-24 16:55:41 +000033
Chris Lattnerf4601652007-11-22 20:49:04 +000034 bool isInvalid() const { return Rec == 0; }
35};
David Greenede444af2009-04-22 16:42:54 +000036
37struct SubMultiClassReference {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000038 SMLoc RefLoc;
David Greenede444af2009-04-22 16:42:54 +000039 MultiClass *MC;
David Greene05bce0b2011-07-29 22:43:06 +000040 std::vector<Init*> TemplateArgs;
David Greenede444af2009-04-22 16:42:54 +000041 SubMultiClassReference() : MC(0) {}
Bob Wilson32558652009-04-28 19:41:44 +000042
David Greenede444af2009-04-22 16:42:54 +000043 bool isInvalid() const { return MC == 0; }
David Greened34a73b2009-04-24 16:55:41 +000044 void dump() const;
David Greenede444af2009-04-22 16:42:54 +000045};
David Greened34a73b2009-04-24 16:55:41 +000046
47void SubMultiClassReference::dump() const {
Daniel Dunbar1a551802009-07-03 00:10:29 +000048 errs() << "Multiclass:\n";
Bob Wilson21870412009-11-22 04:24:42 +000049
David Greened34a73b2009-04-24 16:55:41 +000050 MC->dump();
Bob Wilson21870412009-11-22 04:24:42 +000051
Daniel Dunbar1a551802009-07-03 00:10:29 +000052 errs() << "Template args:\n";
David Greene05bce0b2011-07-29 22:43:06 +000053 for (std::vector<Init *>::const_iterator i = TemplateArgs.begin(),
David Greened34a73b2009-04-24 16:55:41 +000054 iend = TemplateArgs.end();
55 i != iend;
56 ++i) {
57 (*i)->dump();
58 }
59}
60
Chris Lattnerf4601652007-11-22 20:49:04 +000061} // end namespace llvm
62
Chris Lattner1e3a8a42009-06-21 03:39:35 +000063bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Chris Lattnerf4601652007-11-22 20:49:04 +000064 if (CurRec == 0)
65 CurRec = &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +000066
Chris Lattnerf4601652007-11-22 20:49:04 +000067 if (RecordVal *ERV = CurRec->getValue(RV.getName())) {
68 // The value already exists in the class, treat this as a set.
69 if (ERV->setValue(RV.getValue()))
70 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
71 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson21870412009-11-22 04:24:42 +000072 "previous definition of type '" +
Chris Lattnerf4601652007-11-22 20:49:04 +000073 ERV->getType()->getAsString() + "'");
74 } else {
75 CurRec->addValue(RV);
76 }
77 return false;
78}
79
80/// SetValue -
81/// Return true on error, false on success.
David Greene917924d2011-10-19 13:02:39 +000082bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
David Greene05bce0b2011-07-29 22:43:06 +000083 const std::vector<unsigned> &BitList, Init *V) {
Chris Lattnerf4601652007-11-22 20:49:04 +000084 if (!V) return false;
85
86 if (CurRec == 0) CurRec = &CurMultiClass->Rec;
87
88 RecordVal *RV = CurRec->getValue(ValName);
89 if (RV == 0)
David Greene917924d2011-10-19 13:02:39 +000090 return Error(Loc, "Value '" + ValName->getAsUnquotedString()
91 + "' unknown!");
Chris Lattnerf4601652007-11-22 20:49:04 +000092
93 // Do not allow assignments like 'X = X'. This will just cause infinite loops
94 // in the resolution machinery.
95 if (BitList.empty())
David Greene05bce0b2011-07-29 22:43:06 +000096 if (VarInit *VI = dynamic_cast<VarInit*>(V))
David Greene917924d2011-10-19 13:02:39 +000097 if (VI->getNameInit() == ValName)
Chris Lattnerf4601652007-11-22 20:49:04 +000098 return false;
Bob Wilson21870412009-11-22 04:24:42 +000099
Chris Lattnerf4601652007-11-22 20:49:04 +0000100 // If we are assigning to a subset of the bits in the value... then we must be
101 // assigning to a field of BitsRecTy, which must have a BitsInit
102 // initializer.
103 //
104 if (!BitList.empty()) {
David Greene05bce0b2011-07-29 22:43:06 +0000105 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
Chris Lattnerf4601652007-11-22 20:49:04 +0000106 if (CurVal == 0)
David Greene917924d2011-10-19 13:02:39 +0000107 return Error(Loc, "Value '" + ValName->getAsUnquotedString()
108 + "' is not a bits type");
Chris Lattnerf4601652007-11-22 20:49:04 +0000109
110 // Convert the incoming value to a bits type of the appropriate size...
David Greene05bce0b2011-07-29 22:43:06 +0000111 Init *BI = V->convertInitializerTo(BitsRecTy::get(BitList.size()));
Chris Lattnerf4601652007-11-22 20:49:04 +0000112 if (BI == 0) {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000113 V->convertInitializerTo(BitsRecTy::get(BitList.size()));
Chris Lattnerf4601652007-11-22 20:49:04 +0000114 return Error(Loc, "Initializer is not compatible with bit range");
115 }
Bob Wilson21870412009-11-22 04:24:42 +0000116
Chris Lattnerf4601652007-11-22 20:49:04 +0000117 // We should have a BitsInit type now.
David Greene05bce0b2011-07-29 22:43:06 +0000118 BitsInit *BInit = dynamic_cast<BitsInit*>(BI);
Chris Lattnerf4601652007-11-22 20:49:04 +0000119 assert(BInit != 0);
120
David Greene05bce0b2011-07-29 22:43:06 +0000121 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4601652007-11-22 20:49:04 +0000122
123 // Loop over bits, assigning values as appropriate.
124 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
125 unsigned Bit = BitList[i];
David Greeneca7fd3d2011-07-29 19:07:00 +0000126 if (NewBits[Bit])
Chris Lattnerf4601652007-11-22 20:49:04 +0000127 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
David Greene917924d2011-10-19 13:02:39 +0000128 ValName->getAsUnquotedString() + "' more than once");
David Greeneca7fd3d2011-07-29 19:07:00 +0000129 NewBits[Bit] = BInit->getBit(i);
Chris Lattnerf4601652007-11-22 20:49:04 +0000130 }
131
132 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
David Greeneca7fd3d2011-07-29 19:07:00 +0000133 if (NewBits[i] == 0)
134 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4601652007-11-22 20:49:04 +0000135
David Greenedcd35c72011-07-29 19:07:07 +0000136 V = BitsInit::get(NewBits);
Chris Lattnerf4601652007-11-22 20:49:04 +0000137 }
138
139 if (RV->setValue(V))
David Greene917924d2011-10-19 13:02:39 +0000140 return Error(Loc, "Value '" + ValName->getAsUnquotedString() + "' of type '"
141 + RV->getType()->getAsString() +
142 "' is incompatible with initializer '" + V->getAsString()
143 + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +0000144 return false;
145}
146
147/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
148/// args as SubClass's template arguments.
Cedric Venetaff9c272009-02-14 16:06:42 +0000149bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000150 Record *SC = SubClass.Rec;
151 // Add all of the values in the subclass into the current class.
152 const std::vector<RecordVal> &Vals = SC->getValues();
153 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
154 if (AddValue(CurRec, SubClass.RefLoc, Vals[i]))
155 return true;
156
David Greenee22b3212011-10-19 13:02:42 +0000157 const std::vector<Init *> &TArgs = SC->getTemplateArgs();
Chris Lattnerf4601652007-11-22 20:49:04 +0000158
159 // Ensure that an appropriate number of template arguments are specified.
160 if (TArgs.size() < SubClass.TemplateArgs.size())
161 return Error(SubClass.RefLoc, "More template args specified than expected");
Bob Wilson21870412009-11-22 04:24:42 +0000162
Chris Lattnerf4601652007-11-22 20:49:04 +0000163 // Loop over all of the template arguments, setting them to the specified
164 // value or leaving them as the default if necessary.
165 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
166 if (i < SubClass.TemplateArgs.size()) {
167 // If a value is specified for this template arg, set it now.
Bob Wilson21870412009-11-22 04:24:42 +0000168 if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(),
Chris Lattnerf4601652007-11-22 20:49:04 +0000169 SubClass.TemplateArgs[i]))
170 return true;
Bob Wilson21870412009-11-22 04:24:42 +0000171
Chris Lattnerf4601652007-11-22 20:49:04 +0000172 // Resolve it next.
173 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson21870412009-11-22 04:24:42 +0000174
Chris Lattnerf4601652007-11-22 20:49:04 +0000175 // Now remove it.
176 CurRec->removeValue(TArgs[i]);
177
178 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
179 return Error(SubClass.RefLoc,"Value not specified for template argument #"
David Greenee22b3212011-10-19 13:02:42 +0000180 + utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
181 + ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4601652007-11-22 20:49:04 +0000182 }
183 }
184
185 // Since everything went well, we can now set the "superclass" list for the
186 // current record.
187 const std::vector<Record*> &SCs = SC->getSuperClasses();
188 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
189 if (CurRec->isSubClassOf(SCs[i]))
190 return Error(SubClass.RefLoc,
191 "Already subclass of '" + SCs[i]->getName() + "'!\n");
192 CurRec->addSuperClass(SCs[i]);
193 }
Bob Wilson21870412009-11-22 04:24:42 +0000194
Chris Lattnerf4601652007-11-22 20:49:04 +0000195 if (CurRec->isSubClassOf(SC))
196 return Error(SubClass.RefLoc,
197 "Already subclass of '" + SC->getName() + "'!\n");
198 CurRec->addSuperClass(SC);
199 return false;
200}
201
David Greenede444af2009-04-22 16:42:54 +0000202/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilson440548d2009-04-30 18:26:19 +0000203/// CurMC, resolving its template args as SubMultiClass's
David Greenede444af2009-04-22 16:42:54 +0000204/// template arguments.
Bob Wilson440548d2009-04-30 18:26:19 +0000205bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson1d512df2009-04-30 17:46:20 +0000206 SubMultiClassReference &SubMultiClass) {
David Greenede444af2009-04-22 16:42:54 +0000207 MultiClass *SMC = SubMultiClass.MC;
Bob Wilson440548d2009-04-30 18:26:19 +0000208 Record *CurRec = &CurMC->Rec;
David Greenede444af2009-04-22 16:42:54 +0000209
Bob Wilson440548d2009-04-30 18:26:19 +0000210 const std::vector<RecordVal> &MCVals = CurRec->getValues();
David Greenede444af2009-04-22 16:42:54 +0000211
212 // Add all of the values in the subclass into the current class.
213 const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues();
214 for (unsigned i = 0, e = SMCVals.size(); i != e; ++i)
215 if (AddValue(CurRec, SubMultiClass.RefLoc, SMCVals[i]))
216 return true;
217
Bob Wilson440548d2009-04-30 18:26:19 +0000218 int newDefStart = CurMC->DefPrototypes.size();
David Greened34a73b2009-04-24 16:55:41 +0000219
David Greenede444af2009-04-22 16:42:54 +0000220 // Add all of the defs in the subclass into the current multiclass.
221 for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(),
222 iend = SMC->DefPrototypes.end();
223 i != iend;
224 ++i) {
225 // Clone the def and add it to the current multiclass
226 Record *NewDef = new Record(**i);
227
228 // Add all of the values in the superclass into the current def.
229 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
230 if (AddValue(NewDef, SubMultiClass.RefLoc, MCVals[i]))
231 return true;
232
Bob Wilson440548d2009-04-30 18:26:19 +0000233 CurMC->DefPrototypes.push_back(NewDef);
David Greenede444af2009-04-22 16:42:54 +0000234 }
Bob Wilson32558652009-04-28 19:41:44 +0000235
David Greenee22b3212011-10-19 13:02:42 +0000236 const std::vector<Init *> &SMCTArgs = SMC->Rec.getTemplateArgs();
David Greenede444af2009-04-22 16:42:54 +0000237
David Greened34a73b2009-04-24 16:55:41 +0000238 // Ensure that an appropriate number of template arguments are
239 // specified.
David Greenede444af2009-04-22 16:42:54 +0000240 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
David Greened34a73b2009-04-24 16:55:41 +0000241 return Error(SubMultiClass.RefLoc,
242 "More template args specified than expected");
Bob Wilson32558652009-04-28 19:41:44 +0000243
David Greenede444af2009-04-22 16:42:54 +0000244 // Loop over all of the template arguments, setting them to the specified
245 // value or leaving them as the default if necessary.
246 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
247 if (i < SubMultiClass.TemplateArgs.size()) {
David Greened34a73b2009-04-24 16:55:41 +0000248 // If a value is specified for this template arg, set it in the
249 // superclass now.
250 if (SetValue(CurRec, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000251 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000252 SubMultiClass.TemplateArgs[i]))
253 return true;
254
255 // Resolve it next.
256 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson32558652009-04-28 19:41:44 +0000257
David Greenede444af2009-04-22 16:42:54 +0000258 // Now remove it.
259 CurRec->removeValue(SMCTArgs[i]);
260
David Greened34a73b2009-04-24 16:55:41 +0000261 // If a value is specified for this template arg, set it in the
262 // new defs now.
263 for (MultiClass::RecordVector::iterator j =
Bob Wilson440548d2009-04-30 18:26:19 +0000264 CurMC->DefPrototypes.begin() + newDefStart,
265 jend = CurMC->DefPrototypes.end();
David Greenede444af2009-04-22 16:42:54 +0000266 j != jend;
267 ++j) {
268 Record *Def = *j;
269
David Greened34a73b2009-04-24 16:55:41 +0000270 if (SetValue(Def, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000271 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000272 SubMultiClass.TemplateArgs[i]))
273 return true;
274
275 // Resolve it next.
276 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
277
278 // Now remove it
279 Def->removeValue(SMCTArgs[i]);
280 }
281 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
David Greened34a73b2009-04-24 16:55:41 +0000282 return Error(SubMultiClass.RefLoc,
283 "Value not specified for template argument #"
David Greenee22b3212011-10-19 13:02:42 +0000284 + utostr(i) + " (" + SMCTArgs[i]->getAsUnquotedString()
285 + ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greenede444af2009-04-22 16:42:54 +0000286 }
287 }
288
289 return false;
290}
291
Chris Lattnerf4601652007-11-22 20:49:04 +0000292//===----------------------------------------------------------------------===//
293// Parser Code
294//===----------------------------------------------------------------------===//
295
296/// isObjectStart - Return true if this is a valid first token for an Object.
297static bool isObjectStart(tgtok::TokKind K) {
298 return K == tgtok::Class || K == tgtok::Def ||
Bob Wilson21870412009-11-22 04:24:42 +0000299 K == tgtok::Defm || K == tgtok::Let || K == tgtok::MultiClass;
Chris Lattnerf4601652007-11-22 20:49:04 +0000300}
301
Chris Lattnerdf72eae2010-10-05 22:51:56 +0000302static std::string GetNewAnonymousName() {
303 static unsigned AnonCounter = 0;
304 return "anonymous."+utostr(AnonCounter++);
305}
306
Chris Lattnerf4601652007-11-22 20:49:04 +0000307/// ParseObjectName - If an object name is specified, return it. Otherwise,
308/// return an anonymous name.
309/// ObjectName ::= ID
310/// ObjectName ::= /*empty*/
311///
312std::string TGParser::ParseObjectName() {
Chris Lattnerdf72eae2010-10-05 22:51:56 +0000313 if (Lex.getCode() != tgtok::Id)
314 return GetNewAnonymousName();
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000315
Chris Lattnerdf72eae2010-10-05 22:51:56 +0000316 std::string Ret = Lex.getCurStrVal();
317 Lex.Lex();
318 return Ret;
Chris Lattnerf4601652007-11-22 20:49:04 +0000319}
320
321
322/// ParseClassID - Parse and resolve a reference to a class name. This returns
323/// null on error.
324///
325/// ClassID ::= ID
326///
327Record *TGParser::ParseClassID() {
328 if (Lex.getCode() != tgtok::Id) {
329 TokError("expected name for ClassID");
330 return 0;
331 }
Bob Wilson21870412009-11-22 04:24:42 +0000332
Chris Lattnerf4601652007-11-22 20:49:04 +0000333 Record *Result = Records.getClass(Lex.getCurStrVal());
334 if (Result == 0)
335 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson21870412009-11-22 04:24:42 +0000336
Chris Lattnerf4601652007-11-22 20:49:04 +0000337 Lex.Lex();
338 return Result;
339}
340
Bob Wilson32558652009-04-28 19:41:44 +0000341/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
342/// This returns null on error.
David Greenede444af2009-04-22 16:42:54 +0000343///
344/// MultiClassID ::= ID
345///
346MultiClass *TGParser::ParseMultiClassID() {
347 if (Lex.getCode() != tgtok::Id) {
348 TokError("expected name for ClassID");
349 return 0;
350 }
Bob Wilson32558652009-04-28 19:41:44 +0000351
David Greenede444af2009-04-22 16:42:54 +0000352 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
353 if (Result == 0)
354 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson32558652009-04-28 19:41:44 +0000355
David Greenede444af2009-04-22 16:42:54 +0000356 Lex.Lex();
357 return Result;
358}
359
Chris Lattnerf4601652007-11-22 20:49:04 +0000360Record *TGParser::ParseDefmID() {
361 if (Lex.getCode() != tgtok::Id) {
362 TokError("expected multiclass name");
363 return 0;
364 }
Bob Wilson21870412009-11-22 04:24:42 +0000365
Chris Lattnerf4601652007-11-22 20:49:04 +0000366 MultiClass *MC = MultiClasses[Lex.getCurStrVal()];
367 if (MC == 0) {
368 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
369 return 0;
370 }
Bob Wilson21870412009-11-22 04:24:42 +0000371
Chris Lattnerf4601652007-11-22 20:49:04 +0000372 Lex.Lex();
373 return &MC->Rec;
Bob Wilson21870412009-11-22 04:24:42 +0000374}
Chris Lattnerf4601652007-11-22 20:49:04 +0000375
376
377/// ParseSubClassReference - Parse a reference to a subclass or to a templated
378/// subclass. This returns a SubClassRefTy with a null Record* on error.
379///
380/// SubClassRef ::= ClassID
381/// SubClassRef ::= ClassID '<' ValueList '>'
382///
383SubClassReference TGParser::
384ParseSubClassReference(Record *CurRec, bool isDefm) {
385 SubClassReference Result;
386 Result.RefLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +0000387
Chris Lattnerf4601652007-11-22 20:49:04 +0000388 if (isDefm)
389 Result.Rec = ParseDefmID();
390 else
391 Result.Rec = ParseClassID();
392 if (Result.Rec == 0) return Result;
Bob Wilson21870412009-11-22 04:24:42 +0000393
Chris Lattnerf4601652007-11-22 20:49:04 +0000394 // If there is no template arg list, we're done.
395 if (Lex.getCode() != tgtok::less)
396 return Result;
397 Lex.Lex(); // Eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000398
Chris Lattnerf4601652007-11-22 20:49:04 +0000399 if (Lex.getCode() == tgtok::greater) {
400 TokError("subclass reference requires a non-empty list of template values");
401 Result.Rec = 0;
402 return Result;
403 }
Bob Wilson21870412009-11-22 04:24:42 +0000404
David Greenee1b46912009-06-08 20:23:18 +0000405 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4601652007-11-22 20:49:04 +0000406 if (Result.TemplateArgs.empty()) {
407 Result.Rec = 0; // Error parsing value list.
408 return Result;
409 }
Bob Wilson21870412009-11-22 04:24:42 +0000410
Chris Lattnerf4601652007-11-22 20:49:04 +0000411 if (Lex.getCode() != tgtok::greater) {
412 TokError("expected '>' in template value list");
413 Result.Rec = 0;
414 return Result;
415 }
416 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000417
Chris Lattnerf4601652007-11-22 20:49:04 +0000418 return Result;
419}
420
Bob Wilson32558652009-04-28 19:41:44 +0000421/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
422/// templated submulticlass. This returns a SubMultiClassRefTy with a null
423/// Record* on error.
David Greenede444af2009-04-22 16:42:54 +0000424///
425/// SubMultiClassRef ::= MultiClassID
426/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
427///
428SubMultiClassReference TGParser::
429ParseSubMultiClassReference(MultiClass *CurMC) {
430 SubMultiClassReference Result;
431 Result.RefLoc = Lex.getLoc();
Bob Wilson32558652009-04-28 19:41:44 +0000432
David Greenede444af2009-04-22 16:42:54 +0000433 Result.MC = ParseMultiClassID();
434 if (Result.MC == 0) return Result;
Bob Wilson32558652009-04-28 19:41:44 +0000435
David Greenede444af2009-04-22 16:42:54 +0000436 // If there is no template arg list, we're done.
437 if (Lex.getCode() != tgtok::less)
438 return Result;
439 Lex.Lex(); // Eat the '<'
Bob Wilson32558652009-04-28 19:41:44 +0000440
David Greenede444af2009-04-22 16:42:54 +0000441 if (Lex.getCode() == tgtok::greater) {
442 TokError("subclass reference requires a non-empty list of template values");
443 Result.MC = 0;
444 return Result;
445 }
Bob Wilson32558652009-04-28 19:41:44 +0000446
David Greenee1b46912009-06-08 20:23:18 +0000447 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greenede444af2009-04-22 16:42:54 +0000448 if (Result.TemplateArgs.empty()) {
449 Result.MC = 0; // Error parsing value list.
450 return Result;
451 }
Bob Wilson32558652009-04-28 19:41:44 +0000452
David Greenede444af2009-04-22 16:42:54 +0000453 if (Lex.getCode() != tgtok::greater) {
454 TokError("expected '>' in template value list");
455 Result.MC = 0;
456 return Result;
457 }
458 Lex.Lex();
459
460 return Result;
461}
462
Chris Lattnerf4601652007-11-22 20:49:04 +0000463/// ParseRangePiece - Parse a bit/value range.
464/// RangePiece ::= INTVAL
465/// RangePiece ::= INTVAL '-' INTVAL
466/// RangePiece ::= INTVAL INTVAL
467bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner811281e2008-01-10 07:01:53 +0000468 if (Lex.getCode() != tgtok::IntVal) {
469 TokError("expected integer or bitrange");
470 return true;
471 }
Dan Gohman63f97202008-10-17 01:33:43 +0000472 int64_t Start = Lex.getCurIntVal();
473 int64_t End;
Bob Wilson21870412009-11-22 04:24:42 +0000474
Chris Lattnerf4601652007-11-22 20:49:04 +0000475 if (Start < 0)
476 return TokError("invalid range, cannot be negative");
Bob Wilson21870412009-11-22 04:24:42 +0000477
Chris Lattnerf4601652007-11-22 20:49:04 +0000478 switch (Lex.Lex()) { // eat first character.
Bob Wilson21870412009-11-22 04:24:42 +0000479 default:
Chris Lattnerf4601652007-11-22 20:49:04 +0000480 Ranges.push_back(Start);
481 return false;
482 case tgtok::minus:
483 if (Lex.Lex() != tgtok::IntVal) {
484 TokError("expected integer value as end of range");
485 return true;
486 }
487 End = Lex.getCurIntVal();
488 break;
489 case tgtok::IntVal:
490 End = -Lex.getCurIntVal();
491 break;
492 }
Bob Wilson21870412009-11-22 04:24:42 +0000493 if (End < 0)
Chris Lattnerf4601652007-11-22 20:49:04 +0000494 return TokError("invalid range, cannot be negative");
495 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000496
Chris Lattnerf4601652007-11-22 20:49:04 +0000497 // Add to the range.
498 if (Start < End) {
499 for (; Start <= End; ++Start)
500 Ranges.push_back(Start);
501 } else {
502 for (; Start >= End; --Start)
503 Ranges.push_back(Start);
504 }
505 return false;
506}
507
508/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
509///
510/// RangeList ::= RangePiece (',' RangePiece)*
511///
512std::vector<unsigned> TGParser::ParseRangeList() {
513 std::vector<unsigned> Result;
Bob Wilson21870412009-11-22 04:24:42 +0000514
Chris Lattnerf4601652007-11-22 20:49:04 +0000515 // Parse the first piece.
516 if (ParseRangePiece(Result))
517 return std::vector<unsigned>();
518 while (Lex.getCode() == tgtok::comma) {
519 Lex.Lex(); // Eat the comma.
520
521 // Parse the next range piece.
522 if (ParseRangePiece(Result))
523 return std::vector<unsigned>();
524 }
525 return Result;
526}
527
528/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
529/// OptionalRangeList ::= '<' RangeList '>'
530/// OptionalRangeList ::= /*empty*/
531bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
532 if (Lex.getCode() != tgtok::less)
533 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000534
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000535 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000536 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000537
Chris Lattnerf4601652007-11-22 20:49:04 +0000538 // Parse the range list.
539 Ranges = ParseRangeList();
540 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000541
Chris Lattnerf4601652007-11-22 20:49:04 +0000542 if (Lex.getCode() != tgtok::greater) {
543 TokError("expected '>' at end of range list");
544 return Error(StartLoc, "to match this '<'");
545 }
546 Lex.Lex(); // eat the '>'.
547 return false;
548}
549
550/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
551/// OptionalBitList ::= '{' RangeList '}'
552/// OptionalBitList ::= /*empty*/
553bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
554 if (Lex.getCode() != tgtok::l_brace)
555 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000556
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000557 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000558 Lex.Lex(); // eat the '{'
Bob Wilson21870412009-11-22 04:24:42 +0000559
Chris Lattnerf4601652007-11-22 20:49:04 +0000560 // Parse the range list.
561 Ranges = ParseRangeList();
562 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000563
Chris Lattnerf4601652007-11-22 20:49:04 +0000564 if (Lex.getCode() != tgtok::r_brace) {
565 TokError("expected '}' at end of bit list");
566 return Error(StartLoc, "to match this '{'");
567 }
568 Lex.Lex(); // eat the '}'.
569 return false;
570}
571
572
573/// ParseType - Parse and return a tblgen type. This returns null on error.
574///
575/// Type ::= STRING // string type
576/// Type ::= BIT // bit type
577/// Type ::= BITS '<' INTVAL '>' // bits<x> type
578/// Type ::= INT // int type
579/// Type ::= LIST '<' Type '>' // list<x> type
580/// Type ::= CODE // code type
581/// Type ::= DAG // dag type
582/// Type ::= ClassID // Record Type
583///
584RecTy *TGParser::ParseType() {
585 switch (Lex.getCode()) {
586 default: TokError("Unknown token when expecting a type"); return 0;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000587 case tgtok::String: Lex.Lex(); return StringRecTy::get();
588 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
589 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
590 case tgtok::Code: Lex.Lex(); return CodeRecTy::get();
591 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4601652007-11-22 20:49:04 +0000592 case tgtok::Id:
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000593 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Chris Lattnerf4601652007-11-22 20:49:04 +0000594 return 0;
595 case tgtok::Bits: {
596 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
597 TokError("expected '<' after bits type");
598 return 0;
599 }
600 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
601 TokError("expected integer in bits<n> type");
602 return 0;
603 }
Dan Gohman63f97202008-10-17 01:33:43 +0000604 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4601652007-11-22 20:49:04 +0000605 if (Lex.Lex() != tgtok::greater) { // Eat count.
606 TokError("expected '>' at end of bits<n> type");
607 return 0;
608 }
609 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000610 return BitsRecTy::get(Val);
Chris Lattnerf4601652007-11-22 20:49:04 +0000611 }
612 case tgtok::List: {
613 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
614 TokError("expected '<' after list type");
615 return 0;
616 }
617 Lex.Lex(); // Eat '<'
618 RecTy *SubType = ParseType();
619 if (SubType == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +0000620
Chris Lattnerf4601652007-11-22 20:49:04 +0000621 if (Lex.getCode() != tgtok::greater) {
622 TokError("expected '>' at end of list<ty> type");
623 return 0;
624 }
625 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000626 return ListRecTy::get(SubType);
Chris Lattnerf4601652007-11-22 20:49:04 +0000627 }
Bob Wilson21870412009-11-22 04:24:42 +0000628 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000629}
630
631/// ParseIDValue - Parse an ID as a value and decode what it means.
632///
633/// IDValue ::= ID [def local value]
634/// IDValue ::= ID [def template arg]
635/// IDValue ::= ID [multiclass local value]
636/// IDValue ::= ID [multiclass template argument]
637/// IDValue ::= ID [def name]
638///
David Greenef3744a02011-10-19 13:04:20 +0000639Init *TGParser::ParseIDValue(Record *CurRec, IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000640 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
641 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000642 SMLoc Loc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000643 Lex.Lex();
644 return ParseIDValue(CurRec, Name, Loc);
645}
646
647/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
648/// has already been read.
David Greene05bce0b2011-07-29 22:43:06 +0000649Init *TGParser::ParseIDValue(Record *CurRec,
David Greenef3744a02011-10-19 13:04:20 +0000650 const std::string &Name, SMLoc NameLoc,
651 IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000652 if (CurRec) {
653 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenedcd35c72011-07-29 19:07:07 +0000654 return VarInit::get(Name, RV->getType());
Bob Wilson21870412009-11-22 04:24:42 +0000655
David Greenee22b3212011-10-19 13:02:42 +0000656 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
657
David Greenecaa25c82011-10-05 22:42:54 +0000658 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +0000659 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
660 "::");
David Greenecaa25c82011-10-05 22:42:54 +0000661
Chris Lattnerf4601652007-11-22 20:49:04 +0000662 if (CurRec->isTemplateArg(TemplateArgName)) {
663 const RecordVal *RV = CurRec->getValue(TemplateArgName);
664 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000665 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000666 }
667 }
Bob Wilson21870412009-11-22 04:24:42 +0000668
Chris Lattnerf4601652007-11-22 20:49:04 +0000669 if (CurMultiClass) {
David Greenee22b3212011-10-19 13:02:42 +0000670 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
671 "::");
672
Chris Lattnerf4601652007-11-22 20:49:04 +0000673 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
674 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
675 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000676 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000677 }
678 }
Bob Wilson21870412009-11-22 04:24:42 +0000679
David Greenebbec2792011-10-19 13:04:21 +0000680 if (Mode == ParseNameMode)
681 return StringInit::get(Name);
682
Chris Lattnerf4601652007-11-22 20:49:04 +0000683 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000684 return DefInit::get(D);
Chris Lattnerf4601652007-11-22 20:49:04 +0000685
David Greenebbec2792011-10-19 13:04:21 +0000686 if (Mode == ParseValueMode) {
687 Error(NameLoc, "Variable not defined: '" + Name + "'");
688 return 0;
689 }
690
691 return StringInit::get(Name);
Chris Lattnerf4601652007-11-22 20:49:04 +0000692}
693
David Greened418c1b2009-05-14 20:54:48 +0000694/// ParseOperation - Parse an operator. This returns null on error.
695///
696/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
697///
David Greene05bce0b2011-07-29 22:43:06 +0000698Init *TGParser::ParseOperation(Record *CurRec) {
David Greened418c1b2009-05-14 20:54:48 +0000699 switch (Lex.getCode()) {
700 default:
701 TokError("unknown operation");
702 return 0;
703 break;
David Greene1434f662011-01-07 17:05:37 +0000704 case tgtok::XHead:
705 case tgtok::XTail:
706 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +0000707 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
708 UnOpInit::UnaryOp Code;
709 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000710
David Greenee6c27de2009-05-14 21:22:49 +0000711 switch (Lex.getCode()) {
712 default: assert(0 && "Unhandled code!");
713 case tgtok::XCast:
714 Lex.Lex(); // eat the operation
715 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000716
David Greenee6c27de2009-05-14 21:22:49 +0000717 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000718
David Greenee6c27de2009-05-14 21:22:49 +0000719 if (Type == 0) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000720 TokError("did not get type for unary operator");
David Greenee6c27de2009-05-14 21:22:49 +0000721 return 0;
722 }
David Greened418c1b2009-05-14 20:54:48 +0000723
David Greenee6c27de2009-05-14 21:22:49 +0000724 break;
David Greene1434f662011-01-07 17:05:37 +0000725 case tgtok::XHead:
David Greene5f9f9ba2009-05-14 22:38:31 +0000726 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000727 Code = UnOpInit::HEAD;
David Greene5f9f9ba2009-05-14 22:38:31 +0000728 break;
David Greene1434f662011-01-07 17:05:37 +0000729 case tgtok::XTail:
David Greene5f9f9ba2009-05-14 22:38:31 +0000730 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000731 Code = UnOpInit::TAIL;
David Greene5f9f9ba2009-05-14 22:38:31 +0000732 break;
David Greene1434f662011-01-07 17:05:37 +0000733 case tgtok::XEmpty:
David Greene5f9f9ba2009-05-14 22:38:31 +0000734 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000735 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000736 Type = IntRecTy::get();
David Greene5f9f9ba2009-05-14 22:38:31 +0000737 break;
David Greenee6c27de2009-05-14 21:22:49 +0000738 }
739 if (Lex.getCode() != tgtok::l_paren) {
740 TokError("expected '(' after unary operator");
741 return 0;
742 }
743 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000744
David Greene05bce0b2011-07-29 22:43:06 +0000745 Init *LHS = ParseValue(CurRec);
David Greenee6c27de2009-05-14 21:22:49 +0000746 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000747
David Greene1434f662011-01-07 17:05:37 +0000748 if (Code == UnOpInit::HEAD
749 || Code == UnOpInit::TAIL
750 || Code == UnOpInit::EMPTY) {
David Greene05bce0b2011-07-29 22:43:06 +0000751 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
752 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
753 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000754 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
755 TokError("expected list or string type argument in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000756 return 0;
757 }
758 if (LHSt) {
759 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
David Greenee1b46912009-06-08 20:23:18 +0000760 StringRecTy *SType = dynamic_cast<StringRecTy*>(LHSt->getType());
761 if (LType == 0 && SType == 0) {
762 TokError("expected list or string type argumnet in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000763 return 0;
764 }
765 }
766
David Greene1434f662011-01-07 17:05:37 +0000767 if (Code == UnOpInit::HEAD
768 || Code == UnOpInit::TAIL) {
David Greenee1b46912009-06-08 20:23:18 +0000769 if (LHSl == 0 && LHSt == 0) {
770 TokError("expected list type argumnet in unary operator");
771 return 0;
772 }
Bob Wilson21870412009-11-22 04:24:42 +0000773
David Greene5f9f9ba2009-05-14 22:38:31 +0000774 if (LHSl && LHSl->getSize() == 0) {
775 TokError("empty list argument in unary operator");
776 return 0;
777 }
778 if (LHSl) {
David Greene05bce0b2011-07-29 22:43:06 +0000779 Init *Item = LHSl->getElement(0);
780 TypedInit *Itemt = dynamic_cast<TypedInit*>(Item);
David Greene5f9f9ba2009-05-14 22:38:31 +0000781 if (Itemt == 0) {
782 TokError("untyped list element in unary operator");
783 return 0;
784 }
David Greene1434f662011-01-07 17:05:37 +0000785 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000786 Type = Itemt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000787 } else {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000788 Type = ListRecTy::get(Itemt->getType());
David Greene5f9f9ba2009-05-14 22:38:31 +0000789 }
Bob Wilson21870412009-11-22 04:24:42 +0000790 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000791 assert(LHSt && "expected list type argument in unary operator");
792 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
793 if (LType == 0) {
794 TokError("expected list type argumnet in unary operator");
795 return 0;
796 }
David Greene1434f662011-01-07 17:05:37 +0000797 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000798 Type = LType->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +0000799 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000800 Type = LType;
801 }
802 }
803 }
804 }
805
David Greenee6c27de2009-05-14 21:22:49 +0000806 if (Lex.getCode() != tgtok::r_paren) {
807 TokError("expected ')' in unary operator");
808 return 0;
809 }
810 Lex.Lex(); // eat the ')'
David Greenedcd35c72011-07-29 19:07:07 +0000811 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee6c27de2009-05-14 21:22:49 +0000812 }
David Greened418c1b2009-05-14 20:54:48 +0000813
814 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +0000815 case tgtok::XSRA:
David Greened418c1b2009-05-14 20:54:48 +0000816 case tgtok::XSRL:
817 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +0000818 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +0000819 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000820 tgtok::TokKind OpTok = Lex.getCode();
821 SMLoc OpLoc = Lex.getLoc();
822 Lex.Lex(); // eat the operation
823
David Greened418c1b2009-05-14 20:54:48 +0000824 BinOpInit::BinaryOp Code;
825 RecTy *Type = 0;
826
Chris Lattner8d978a72010-10-05 23:58:18 +0000827 switch (OpTok) {
David Greened418c1b2009-05-14 20:54:48 +0000828 default: assert(0 && "Unhandled code!");
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000829 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
830 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
831 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
832 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
833 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Bob Wilson21870412009-11-22 04:24:42 +0000834 case tgtok::XStrConcat:
David Greened418c1b2009-05-14 20:54:48 +0000835 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000836 Type = StringRecTy::get();
David Greened418c1b2009-05-14 20:54:48 +0000837 break;
David Greened418c1b2009-05-14 20:54:48 +0000838 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000839
David Greened418c1b2009-05-14 20:54:48 +0000840 if (Lex.getCode() != tgtok::l_paren) {
841 TokError("expected '(' after binary operator");
842 return 0;
843 }
844 Lex.Lex(); // eat the '('
845
David Greene05bce0b2011-07-29 22:43:06 +0000846 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000847
Chris Lattner8d978a72010-10-05 23:58:18 +0000848 InitList.push_back(ParseValue(CurRec));
849 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000850
Chris Lattner8d978a72010-10-05 23:58:18 +0000851 while (Lex.getCode() == tgtok::comma) {
852 Lex.Lex(); // eat the ','
853
854 InitList.push_back(ParseValue(CurRec));
855 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000856 }
David Greened418c1b2009-05-14 20:54:48 +0000857
858 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner8d978a72010-10-05 23:58:18 +0000859 TokError("expected ')' in operator");
David Greened418c1b2009-05-14 20:54:48 +0000860 return 0;
861 }
862 Lex.Lex(); // eat the ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000863
864 // We allow multiple operands to associative operators like !strconcat as
865 // shorthand for nesting them.
866 if (Code == BinOpInit::STRCONCAT) {
867 while (InitList.size() > 2) {
David Greene05bce0b2011-07-29 22:43:06 +0000868 Init *RHS = InitList.pop_back_val();
David Greenedcd35c72011-07-29 19:07:07 +0000869 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
870 ->Fold(CurRec, CurMultiClass);
Chris Lattner8d978a72010-10-05 23:58:18 +0000871 InitList.back() = RHS;
872 }
873 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000874
Chris Lattner8d978a72010-10-05 23:58:18 +0000875 if (InitList.size() == 2)
David Greenedcd35c72011-07-29 19:07:07 +0000876 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner8d978a72010-10-05 23:58:18 +0000877 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000878
Chris Lattner8d978a72010-10-05 23:58:18 +0000879 Error(OpLoc, "expected two operands to operator");
880 return 0;
David Greened418c1b2009-05-14 20:54:48 +0000881 }
882
David Greene9bea7c82009-05-14 23:26:46 +0000883 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +0000884 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +0000885 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
886 TernOpInit::TernaryOp Code;
887 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000888
David Greene4afc5092009-05-14 21:54:42 +0000889 tgtok::TokKind LexCode = Lex.getCode();
890 Lex.Lex(); // eat the operation
891 switch (LexCode) {
892 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000893 case tgtok::XIf:
894 Code = TernOpInit::IF;
895 break;
David Greenebeb31a52009-05-14 22:23:47 +0000896 case tgtok::XForEach:
897 Code = TernOpInit::FOREACH;
898 break;
David Greene4afc5092009-05-14 21:54:42 +0000899 case tgtok::XSubst:
900 Code = TernOpInit::SUBST;
901 break;
902 }
903 if (Lex.getCode() != tgtok::l_paren) {
904 TokError("expected '(' after ternary operator");
905 return 0;
906 }
907 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000908
David Greene05bce0b2011-07-29 22:43:06 +0000909 Init *LHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +0000910 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000911
David Greene4afc5092009-05-14 21:54:42 +0000912 if (Lex.getCode() != tgtok::comma) {
913 TokError("expected ',' in ternary operator");
914 return 0;
915 }
916 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000917
David Greene05bce0b2011-07-29 22:43:06 +0000918 Init *MHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +0000919 if (MHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000920
David Greene4afc5092009-05-14 21:54:42 +0000921 if (Lex.getCode() != tgtok::comma) {
922 TokError("expected ',' in ternary operator");
923 return 0;
924 }
925 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000926
David Greene05bce0b2011-07-29 22:43:06 +0000927 Init *RHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +0000928 if (RHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000929
David Greene4afc5092009-05-14 21:54:42 +0000930 if (Lex.getCode() != tgtok::r_paren) {
931 TokError("expected ')' in binary operator");
932 return 0;
933 }
934 Lex.Lex(); // eat the ')'
David Greened418c1b2009-05-14 20:54:48 +0000935
David Greene4afc5092009-05-14 21:54:42 +0000936 switch (LexCode) {
937 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000938 case tgtok::XIf: {
Bill Wendling548f5a02010-12-13 01:46:19 +0000939 // FIXME: The `!if' operator doesn't handle non-TypedInit well at
940 // all. This can be made much more robust.
David Greene05bce0b2011-07-29 22:43:06 +0000941 TypedInit *MHSt = dynamic_cast<TypedInit*>(MHS);
942 TypedInit *RHSt = dynamic_cast<TypedInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +0000943
944 RecTy *MHSTy = 0;
945 RecTy *RHSTy = 0;
946
947 if (MHSt == 0 && RHSt == 0) {
David Greene05bce0b2011-07-29 22:43:06 +0000948 BitsInit *MHSbits = dynamic_cast<BitsInit*>(MHS);
949 BitsInit *RHSbits = dynamic_cast<BitsInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +0000950
951 if (MHSbits && RHSbits &&
952 MHSbits->getNumBits() == RHSbits->getNumBits()) {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000953 Type = BitRecTy::get();
Bill Wendling548f5a02010-12-13 01:46:19 +0000954 break;
955 } else {
David Greene05bce0b2011-07-29 22:43:06 +0000956 BitInit *MHSbit = dynamic_cast<BitInit*>(MHS);
957 BitInit *RHSbit = dynamic_cast<BitInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +0000958
959 if (MHSbit && RHSbit) {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000960 Type = BitRecTy::get();
Bill Wendling548f5a02010-12-13 01:46:19 +0000961 break;
962 }
963 }
964 } else if (MHSt != 0 && RHSt != 0) {
965 MHSTy = MHSt->getType();
966 RHSTy = RHSt->getType();
967 }
968
969 if (!MHSTy || !RHSTy) {
David Greene9bea7c82009-05-14 23:26:46 +0000970 TokError("could not get type for !if");
971 return 0;
972 }
Bill Wendling548f5a02010-12-13 01:46:19 +0000973
974 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
975 Type = RHSTy;
976 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
977 Type = MHSTy;
Bob Wilson21870412009-11-22 04:24:42 +0000978 } else {
David Greene9bea7c82009-05-14 23:26:46 +0000979 TokError("inconsistent types for !if");
980 return 0;
981 }
982 break;
983 }
David Greenebeb31a52009-05-14 22:23:47 +0000984 case tgtok::XForEach: {
David Greene05bce0b2011-07-29 22:43:06 +0000985 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
David Greenebeb31a52009-05-14 22:23:47 +0000986 if (MHSt == 0) {
987 TokError("could not get type for !foreach");
988 return 0;
989 }
990 Type = MHSt->getType();
991 break;
992 }
David Greene4afc5092009-05-14 21:54:42 +0000993 case tgtok::XSubst: {
David Greene05bce0b2011-07-29 22:43:06 +0000994 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
David Greene4afc5092009-05-14 21:54:42 +0000995 if (RHSt == 0) {
996 TokError("could not get type for !subst");
997 return 0;
998 }
999 Type = RHSt->getType();
1000 break;
1001 }
1002 }
David Greenedcd35c72011-07-29 19:07:07 +00001003 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson21870412009-11-22 04:24:42 +00001004 CurMultiClass);
David Greene4afc5092009-05-14 21:54:42 +00001005 }
David Greened418c1b2009-05-14 20:54:48 +00001006 }
1007 TokError("could not parse operation");
1008 return 0;
1009}
1010
1011/// ParseOperatorType - Parse a type for an operator. This returns
1012/// null on error.
1013///
1014/// OperatorType ::= '<' Type '>'
1015///
Dan Gohmana9ad0412009-08-12 22:10:57 +00001016RecTy *TGParser::ParseOperatorType() {
David Greened418c1b2009-05-14 20:54:48 +00001017 RecTy *Type = 0;
1018
1019 if (Lex.getCode() != tgtok::less) {
1020 TokError("expected type name for operator");
1021 return 0;
1022 }
1023 Lex.Lex(); // eat the <
1024
1025 Type = ParseType();
1026
1027 if (Type == 0) {
1028 TokError("expected type name for operator");
1029 return 0;
1030 }
1031
1032 if (Lex.getCode() != tgtok::greater) {
1033 TokError("expected type name for operator");
1034 return 0;
1035 }
1036 Lex.Lex(); // eat the >
1037
1038 return Type;
1039}
1040
1041
Chris Lattnerf4601652007-11-22 20:49:04 +00001042/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1043///
1044/// SimpleValue ::= IDValue
1045/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001046/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +00001047/// SimpleValue ::= CODEFRAGMENT
1048/// SimpleValue ::= '?'
1049/// SimpleValue ::= '{' ValueList '}'
1050/// SimpleValue ::= ID '<' ValueListNE '>'
1051/// SimpleValue ::= '[' ValueList ']'
1052/// SimpleValue ::= '(' IDValue DagArgList ')'
1053/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
1054/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1055/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1056/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1057/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1058///
David Greenef3744a02011-10-19 13:04:20 +00001059Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1060 IDParseMode Mode) {
David Greene05bce0b2011-07-29 22:43:06 +00001061 Init *R = 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001062 switch (Lex.getCode()) {
1063 default: TokError("Unknown token when parsing a value"); break;
David Greenedcd35c72011-07-29 19:07:07 +00001064 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001065 case tgtok::StrVal: {
1066 std::string Val = Lex.getCurStrVal();
1067 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001068
Jim Grosbachda4231f2009-03-26 16:17:51 +00001069 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001070 while (Lex.getCode() == tgtok::StrVal) {
1071 Val += Lex.getCurStrVal();
1072 Lex.Lex();
1073 }
Bob Wilson21870412009-11-22 04:24:42 +00001074
David Greenedcd35c72011-07-29 19:07:07 +00001075 R = StringInit::get(Val);
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001076 break;
1077 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001078 case tgtok::CodeFragment:
David Greenedcd35c72011-07-29 19:07:07 +00001079 R = CodeInit::get(Lex.getCurStrVal());
Chris Lattner578bcf02010-10-06 04:31:40 +00001080 Lex.Lex();
1081 break;
1082 case tgtok::question:
David Greenedcd35c72011-07-29 19:07:07 +00001083 R = UnsetInit::get();
Chris Lattner578bcf02010-10-06 04:31:40 +00001084 Lex.Lex();
1085 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001086 case tgtok::Id: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001087 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001088 std::string Name = Lex.getCurStrVal();
1089 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greenef3744a02011-10-19 13:04:20 +00001090 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson21870412009-11-22 04:24:42 +00001091
Chris Lattnerf4601652007-11-22 20:49:04 +00001092 // Value ::= ID '<' ValueListNE '>'
1093 if (Lex.Lex() == tgtok::greater) {
1094 TokError("expected non-empty value list");
1095 return 0;
1096 }
David Greenee1b46912009-06-08 20:23:18 +00001097
Chris Lattnerf4601652007-11-22 20:49:04 +00001098 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1099 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1100 // body.
1101 Record *Class = Records.getClass(Name);
1102 if (!Class) {
1103 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1104 return 0;
1105 }
David Greenee1b46912009-06-08 20:23:18 +00001106
David Greene05bce0b2011-07-29 22:43:06 +00001107 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
David Greenee1b46912009-06-08 20:23:18 +00001108 if (ValueList.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001109
David Greenee1b46912009-06-08 20:23:18 +00001110 if (Lex.getCode() != tgtok::greater) {
1111 TokError("expected '>' at end of value list");
1112 return 0;
1113 }
1114 Lex.Lex(); // eat the '>'
Bob Wilson21870412009-11-22 04:24:42 +00001115
Chris Lattnerf4601652007-11-22 20:49:04 +00001116 // Create the new record, set it as CurRec temporarily.
1117 static unsigned AnonCounter = 0;
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001118 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),
1119 NameLoc,
1120 Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001121 SubClassReference SCRef;
1122 SCRef.RefLoc = NameLoc;
1123 SCRef.Rec = Class;
1124 SCRef.TemplateArgs = ValueList;
1125 // Add info about the subclass to NewRec.
1126 if (AddSubClass(NewRec, SCRef))
1127 return 0;
1128 NewRec->resolveReferences();
1129 Records.addDef(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001130
Chris Lattnerf4601652007-11-22 20:49:04 +00001131 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +00001132 return DefInit::get(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001133 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001134 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001135 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001136 Lex.Lex(); // eat the '{'
David Greene05bce0b2011-07-29 22:43:06 +00001137 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001138
Chris Lattnerf4601652007-11-22 20:49:04 +00001139 if (Lex.getCode() != tgtok::r_brace) {
1140 Vals = ParseValueList(CurRec);
1141 if (Vals.empty()) return 0;
1142 }
1143 if (Lex.getCode() != tgtok::r_brace) {
1144 TokError("expected '}' at end of bit list value");
1145 return 0;
1146 }
1147 Lex.Lex(); // eat the '}'
Bob Wilson21870412009-11-22 04:24:42 +00001148
David Greene05bce0b2011-07-29 22:43:06 +00001149 SmallVector<Init *, 16> NewBits(Vals.size());
David Greeneca7fd3d2011-07-29 19:07:00 +00001150
Chris Lattnerf4601652007-11-22 20:49:04 +00001151 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001152 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Chris Lattnerf4601652007-11-22 20:49:04 +00001153 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +00001154 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1155 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +00001156 return 0;
1157 }
David Greeneca7fd3d2011-07-29 19:07:00 +00001158 NewBits[Vals.size()-i-1] = Bit;
Chris Lattnerf4601652007-11-22 20:49:04 +00001159 }
David Greenedcd35c72011-07-29 19:07:07 +00001160 return BitsInit::get(NewBits);
Chris Lattnerf4601652007-11-22 20:49:04 +00001161 }
1162 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1163 Lex.Lex(); // eat the '['
David Greene05bce0b2011-07-29 22:43:06 +00001164 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001165
David Greenee1b46912009-06-08 20:23:18 +00001166 RecTy *DeducedEltTy = 0;
1167 ListRecTy *GivenListTy = 0;
Bob Wilson21870412009-11-22 04:24:42 +00001168
David Greenee1b46912009-06-08 20:23:18 +00001169 if (ItemType != 0) {
1170 ListRecTy *ListType = dynamic_cast<ListRecTy*>(ItemType);
1171 if (ListType == 0) {
1172 std::stringstream s;
Bob Wilson21870412009-11-22 04:24:42 +00001173 s << "Type mismatch for list, expected list type, got "
David Greenee1b46912009-06-08 20:23:18 +00001174 << ItemType->getAsString();
1175 TokError(s.str());
Jim Grosbach6a44ada2011-03-11 19:52:52 +00001176 return 0;
David Greenee1b46912009-06-08 20:23:18 +00001177 }
1178 GivenListTy = ListType;
Bob Wilson21870412009-11-22 04:24:42 +00001179 }
David Greenee1b46912009-06-08 20:23:18 +00001180
Chris Lattnerf4601652007-11-22 20:49:04 +00001181 if (Lex.getCode() != tgtok::r_square) {
Bob Wilson21870412009-11-22 04:24:42 +00001182 Vals = ParseValueList(CurRec, 0,
1183 GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001184 if (Vals.empty()) return 0;
1185 }
1186 if (Lex.getCode() != tgtok::r_square) {
1187 TokError("expected ']' at end of list value");
1188 return 0;
1189 }
1190 Lex.Lex(); // eat the ']'
David Greenee1b46912009-06-08 20:23:18 +00001191
1192 RecTy *GivenEltTy = 0;
1193 if (Lex.getCode() == tgtok::less) {
1194 // Optional list element type
1195 Lex.Lex(); // eat the '<'
1196
1197 GivenEltTy = ParseType();
1198 if (GivenEltTy == 0) {
1199 // Couldn't parse element type
1200 return 0;
1201 }
1202
1203 if (Lex.getCode() != tgtok::greater) {
1204 TokError("expected '>' at end of list element type");
1205 return 0;
1206 }
1207 Lex.Lex(); // eat the '>'
1208 }
1209
1210 // Check elements
1211 RecTy *EltTy = 0;
David Greene05bce0b2011-07-29 22:43:06 +00001212 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greenee1b46912009-06-08 20:23:18 +00001213 i != ie;
1214 ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001215 TypedInit *TArg = dynamic_cast<TypedInit*>(*i);
David Greenee1b46912009-06-08 20:23:18 +00001216 if (TArg == 0) {
1217 TokError("Untyped list element");
1218 return 0;
1219 }
1220 if (EltTy != 0) {
1221 EltTy = resolveTypes(EltTy, TArg->getType());
1222 if (EltTy == 0) {
1223 TokError("Incompatible types in list elements");
1224 return 0;
1225 }
Bob Wilson21870412009-11-22 04:24:42 +00001226 } else {
David Greenee1b46912009-06-08 20:23:18 +00001227 EltTy = TArg->getType();
1228 }
1229 }
1230
1231 if (GivenEltTy != 0) {
1232 if (EltTy != 0) {
1233 // Verify consistency
1234 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1235 TokError("Incompatible types in list elements");
1236 return 0;
1237 }
1238 }
1239 EltTy = GivenEltTy;
1240 }
1241
1242 if (EltTy == 0) {
1243 if (ItemType == 0) {
1244 TokError("No type for list");
1245 return 0;
1246 }
1247 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +00001248 } else {
David Greenee1b46912009-06-08 20:23:18 +00001249 // Make sure the deduced type is compatible with the given type
1250 if (GivenListTy) {
1251 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1252 TokError("Element type mismatch for list");
1253 return 0;
1254 }
1255 }
1256 DeducedEltTy = EltTy;
1257 }
Bob Wilson21870412009-11-22 04:24:42 +00001258
David Greenedcd35c72011-07-29 19:07:07 +00001259 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4601652007-11-22 20:49:04 +00001260 }
1261 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1262 Lex.Lex(); // eat the '('
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001263 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001264 TokError("expected identifier in dag init");
1265 return 0;
1266 }
Bob Wilson21870412009-11-22 04:24:42 +00001267
David Greene05bce0b2011-07-29 22:43:06 +00001268 Init *Operator = ParseValue(CurRec);
Chris Lattner578bcf02010-10-06 04:31:40 +00001269 if (Operator == 0) return 0;
David Greenec7cafcd2009-04-22 20:18:10 +00001270
Nate Begeman7cee8172009-03-19 05:21:56 +00001271 // If the operator name is present, parse it.
1272 std::string OperatorName;
1273 if (Lex.getCode() == tgtok::colon) {
1274 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1275 TokError("expected variable name in dag operator");
1276 return 0;
1277 }
1278 OperatorName = Lex.getCurStrVal();
1279 Lex.Lex(); // eat the VarName.
1280 }
Bob Wilson21870412009-11-22 04:24:42 +00001281
David Greene05bce0b2011-07-29 22:43:06 +00001282 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4601652007-11-22 20:49:04 +00001283 if (Lex.getCode() != tgtok::r_paren) {
1284 DagArgs = ParseDagArgList(CurRec);
1285 if (DagArgs.empty()) return 0;
1286 }
Bob Wilson21870412009-11-22 04:24:42 +00001287
Chris Lattnerf4601652007-11-22 20:49:04 +00001288 if (Lex.getCode() != tgtok::r_paren) {
1289 TokError("expected ')' in dag init");
1290 return 0;
1291 }
1292 Lex.Lex(); // eat the ')'
Bob Wilson21870412009-11-22 04:24:42 +00001293
David Greenedcd35c72011-07-29 19:07:07 +00001294 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4601652007-11-22 20:49:04 +00001295 }
Bob Wilson21870412009-11-22 04:24:42 +00001296
David Greene1434f662011-01-07 17:05:37 +00001297 case tgtok::XHead:
1298 case tgtok::XTail:
1299 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +00001300 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001301 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +00001302 case tgtok::XSRA:
Chris Lattnerf4601652007-11-22 20:49:04 +00001303 case tgtok::XSRL:
1304 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +00001305 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001306 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene9bea7c82009-05-14 23:26:46 +00001307 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +00001308 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001309 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greened418c1b2009-05-14 20:54:48 +00001310 return ParseOperation(CurRec);
Chris Lattnerf4601652007-11-22 20:49:04 +00001311 }
1312 }
Bob Wilson21870412009-11-22 04:24:42 +00001313
Chris Lattnerf4601652007-11-22 20:49:04 +00001314 return R;
1315}
1316
1317/// ParseValue - Parse a tblgen value. This returns null on error.
1318///
1319/// Value ::= SimpleValue ValueSuffix*
1320/// ValueSuffix ::= '{' BitList '}'
1321/// ValueSuffix ::= '[' BitList ']'
1322/// ValueSuffix ::= '.' ID
1323///
David Greenef3744a02011-10-19 13:04:20 +00001324Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1325 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Chris Lattnerf4601652007-11-22 20:49:04 +00001326 if (Result == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001327
Chris Lattnerf4601652007-11-22 20:49:04 +00001328 // Parse the suffixes now if present.
1329 while (1) {
1330 switch (Lex.getCode()) {
1331 default: return Result;
1332 case tgtok::l_brace: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001333 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001334 Lex.Lex(); // eat the '{'
1335 std::vector<unsigned> Ranges = ParseRangeList();
1336 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001337
Chris Lattnerf4601652007-11-22 20:49:04 +00001338 // Reverse the bitlist.
1339 std::reverse(Ranges.begin(), Ranges.end());
1340 Result = Result->convertInitializerBitRange(Ranges);
1341 if (Result == 0) {
1342 Error(CurlyLoc, "Invalid bit range for value");
1343 return 0;
1344 }
Bob Wilson21870412009-11-22 04:24:42 +00001345
Chris Lattnerf4601652007-11-22 20:49:04 +00001346 // Eat the '}'.
1347 if (Lex.getCode() != tgtok::r_brace) {
1348 TokError("expected '}' at end of bit range list");
1349 return 0;
1350 }
1351 Lex.Lex();
1352 break;
1353 }
1354 case tgtok::l_square: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001355 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001356 Lex.Lex(); // eat the '['
1357 std::vector<unsigned> Ranges = ParseRangeList();
1358 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001359
Chris Lattnerf4601652007-11-22 20:49:04 +00001360 Result = Result->convertInitListSlice(Ranges);
1361 if (Result == 0) {
1362 Error(SquareLoc, "Invalid range for list slice");
1363 return 0;
1364 }
Bob Wilson21870412009-11-22 04:24:42 +00001365
Chris Lattnerf4601652007-11-22 20:49:04 +00001366 // Eat the ']'.
1367 if (Lex.getCode() != tgtok::r_square) {
1368 TokError("expected ']' at end of list slice");
1369 return 0;
1370 }
1371 Lex.Lex();
1372 break;
1373 }
1374 case tgtok::period:
1375 if (Lex.Lex() != tgtok::Id) { // eat the .
1376 TokError("expected field identifier after '.'");
1377 return 0;
1378 }
1379 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001380 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00001381 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +00001382 return 0;
1383 }
David Greenedcd35c72011-07-29 19:07:07 +00001384 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001385 Lex.Lex(); // eat field name
1386 break;
1387 }
1388 }
1389}
1390
1391/// ParseDagArgList - Parse the argument list for a dag literal expression.
1392///
1393/// ParseDagArgList ::= Value (':' VARNAME)?
1394/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
David Greene05bce0b2011-07-29 22:43:06 +00001395std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4601652007-11-22 20:49:04 +00001396TGParser::ParseDagArgList(Record *CurRec) {
David Greene05bce0b2011-07-29 22:43:06 +00001397 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson21870412009-11-22 04:24:42 +00001398
Chris Lattnerf4601652007-11-22 20:49:04 +00001399 while (1) {
David Greene05bce0b2011-07-29 22:43:06 +00001400 Init *Val = ParseValue(CurRec);
1401 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
Bob Wilson21870412009-11-22 04:24:42 +00001402
Chris Lattnerf4601652007-11-22 20:49:04 +00001403 // If the variable name is present, add it.
1404 std::string VarName;
1405 if (Lex.getCode() == tgtok::colon) {
1406 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1407 TokError("expected variable name in dag literal");
David Greene05bce0b2011-07-29 22:43:06 +00001408 return std::vector<std::pair<llvm::Init*, std::string> >();
Chris Lattnerf4601652007-11-22 20:49:04 +00001409 }
1410 VarName = Lex.getCurStrVal();
1411 Lex.Lex(); // eat the VarName.
1412 }
Bob Wilson21870412009-11-22 04:24:42 +00001413
Chris Lattnerf4601652007-11-22 20:49:04 +00001414 Result.push_back(std::make_pair(Val, VarName));
Bob Wilson21870412009-11-22 04:24:42 +00001415
Chris Lattnerf4601652007-11-22 20:49:04 +00001416 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson21870412009-11-22 04:24:42 +00001417 Lex.Lex(); // eat the ','
Chris Lattnerf4601652007-11-22 20:49:04 +00001418 }
Bob Wilson21870412009-11-22 04:24:42 +00001419
Chris Lattnerf4601652007-11-22 20:49:04 +00001420 return Result;
1421}
1422
1423
1424/// ParseValueList - Parse a comma separated list of values, returning them as a
1425/// vector. Note that this always expects to be able to parse at least one
1426/// value. It returns an empty list if this is not possible.
1427///
1428/// ValueList ::= Value (',' Value)
1429///
David Greene05bce0b2011-07-29 22:43:06 +00001430std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopherd568b3f2011-07-11 23:06:52 +00001431 RecTy *EltTy) {
David Greene05bce0b2011-07-29 22:43:06 +00001432 std::vector<Init*> Result;
David Greenee1b46912009-06-08 20:23:18 +00001433 RecTy *ItemType = EltTy;
David Greene67acdf22009-06-29 19:59:52 +00001434 unsigned int ArgN = 0;
David Greenee1b46912009-06-08 20:23:18 +00001435 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001436 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greenee1b46912009-06-08 20:23:18 +00001437 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greened9746fe2011-09-19 18:26:07 +00001438 if (!RV) {
1439 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1440 << ")\n";
1441 }
David Greenee1b46912009-06-08 20:23:18 +00001442 assert(RV && "Template argument record not found??");
1443 ItemType = RV->getType();
1444 ++ArgN;
1445 }
1446 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001447 if (Result.back() == 0) return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001448
Chris Lattnerf4601652007-11-22 20:49:04 +00001449 while (Lex.getCode() == tgtok::comma) {
1450 Lex.Lex(); // Eat the comma
Bob Wilson21870412009-11-22 04:24:42 +00001451
David Greenee1b46912009-06-08 20:23:18 +00001452 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001453 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greene67acdf22009-06-29 19:59:52 +00001454 if (ArgN >= TArgs.size()) {
1455 TokError("too many template arguments");
David Greene05bce0b2011-07-29 22:43:06 +00001456 return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001457 }
David Greenee1b46912009-06-08 20:23:18 +00001458 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1459 assert(RV && "Template argument record not found??");
1460 ItemType = RV->getType();
1461 ++ArgN;
1462 }
1463 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001464 if (Result.back() == 0) return std::vector<Init*>();
Chris Lattnerf4601652007-11-22 20:49:04 +00001465 }
Bob Wilson21870412009-11-22 04:24:42 +00001466
Chris Lattnerf4601652007-11-22 20:49:04 +00001467 return Result;
1468}
1469
1470
Chris Lattnerf4601652007-11-22 20:49:04 +00001471/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1472/// empty string on error. This can happen in a number of different context's,
1473/// including within a def or in the template args for a def (which which case
1474/// CurRec will be non-null) and within the template args for a multiclass (in
1475/// which case CurRec will be null, but CurMultiClass will be set). This can
1476/// also happen within a def that is within a multiclass, which will set both
1477/// CurRec and CurMultiClass.
1478///
1479/// Declaration ::= FIELD? Type ID ('=' Value)?
1480///
David Greenee22b3212011-10-19 13:02:42 +00001481Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4601652007-11-22 20:49:04 +00001482 bool ParsingTemplateArgs) {
1483 // Read the field prefix if present.
1484 bool HasField = Lex.getCode() == tgtok::Field;
1485 if (HasField) Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001486
Chris Lattnerf4601652007-11-22 20:49:04 +00001487 RecTy *Type = ParseType();
David Greenee22b3212011-10-19 13:02:42 +00001488 if (Type == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001489
Chris Lattnerf4601652007-11-22 20:49:04 +00001490 if (Lex.getCode() != tgtok::Id) {
1491 TokError("Expected identifier in declaration");
David Greenee22b3212011-10-19 13:02:42 +00001492 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001493 }
Bob Wilson21870412009-11-22 04:24:42 +00001494
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001495 SMLoc IdLoc = Lex.getLoc();
David Greenee22b3212011-10-19 13:02:42 +00001496 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001497 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001498
Chris Lattnerf4601652007-11-22 20:49:04 +00001499 if (ParsingTemplateArgs) {
1500 if (CurRec) {
David Greenee22b3212011-10-19 13:02:42 +00001501 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4601652007-11-22 20:49:04 +00001502 } else {
1503 assert(CurMultiClass);
1504 }
1505 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +00001506 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1507 "::");
Chris Lattnerf4601652007-11-22 20:49:04 +00001508 }
Bob Wilson21870412009-11-22 04:24:42 +00001509
Chris Lattnerf4601652007-11-22 20:49:04 +00001510 // Add the value.
1511 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
David Greenee22b3212011-10-19 13:02:42 +00001512 return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001513
Chris Lattnerf4601652007-11-22 20:49:04 +00001514 // If a value is present, parse it.
1515 if (Lex.getCode() == tgtok::equal) {
1516 Lex.Lex();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001517 SMLoc ValLoc = Lex.getLoc();
David Greene05bce0b2011-07-29 22:43:06 +00001518 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001519 if (Val == 0 ||
1520 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
David Greenee22b3212011-10-19 13:02:42 +00001521 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001522 }
Bob Wilson21870412009-11-22 04:24:42 +00001523
Chris Lattnerf4601652007-11-22 20:49:04 +00001524 return DeclName;
1525}
1526
1527/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1528/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1529/// template args for a def, which may or may not be in a multiclass. If null,
1530/// these are the template args for a multiclass.
1531///
1532/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson21870412009-11-22 04:24:42 +00001533///
Chris Lattnerf4601652007-11-22 20:49:04 +00001534bool TGParser::ParseTemplateArgList(Record *CurRec) {
1535 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1536 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +00001537
Chris Lattnerf4601652007-11-22 20:49:04 +00001538 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +00001539
Chris Lattnerf4601652007-11-22 20:49:04 +00001540 // Read the first declaration.
David Greenee22b3212011-10-19 13:02:42 +00001541 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1542 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001543 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001544
Chris Lattnerf4601652007-11-22 20:49:04 +00001545 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson21870412009-11-22 04:24:42 +00001546
Chris Lattnerf4601652007-11-22 20:49:04 +00001547 while (Lex.getCode() == tgtok::comma) {
1548 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001549
Chris Lattnerf4601652007-11-22 20:49:04 +00001550 // Read the following declarations.
1551 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
David Greenee22b3212011-10-19 13:02:42 +00001552 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001553 return true;
1554 TheRecToAddTo->addTemplateArg(TemplArg);
1555 }
Bob Wilson21870412009-11-22 04:24:42 +00001556
Chris Lattnerf4601652007-11-22 20:49:04 +00001557 if (Lex.getCode() != tgtok::greater)
1558 return TokError("expected '>' at end of template argument list");
1559 Lex.Lex(); // eat the '>'.
1560 return false;
1561}
1562
1563
1564/// ParseBodyItem - Parse a single item at within the body of a def or class.
1565///
1566/// BodyItem ::= Declaration ';'
1567/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1568bool TGParser::ParseBodyItem(Record *CurRec) {
1569 if (Lex.getCode() != tgtok::Let) {
David Greenee22b3212011-10-19 13:02:42 +00001570 if (ParseDeclaration(CurRec, false) == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001571 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001572
Chris Lattnerf4601652007-11-22 20:49:04 +00001573 if (Lex.getCode() != tgtok::semi)
1574 return TokError("expected ';' after declaration");
1575 Lex.Lex();
1576 return false;
1577 }
1578
1579 // LET ID OptionalRangeList '=' Value ';'
1580 if (Lex.Lex() != tgtok::Id)
1581 return TokError("expected field identifier after let");
Bob Wilson21870412009-11-22 04:24:42 +00001582
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001583 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001584 std::string FieldName = Lex.getCurStrVal();
1585 Lex.Lex(); // eat the field name.
Bob Wilson21870412009-11-22 04:24:42 +00001586
Chris Lattnerf4601652007-11-22 20:49:04 +00001587 std::vector<unsigned> BitList;
Bob Wilson21870412009-11-22 04:24:42 +00001588 if (ParseOptionalBitList(BitList))
Chris Lattnerf4601652007-11-22 20:49:04 +00001589 return true;
1590 std::reverse(BitList.begin(), BitList.end());
Bob Wilson21870412009-11-22 04:24:42 +00001591
Chris Lattnerf4601652007-11-22 20:49:04 +00001592 if (Lex.getCode() != tgtok::equal)
1593 return TokError("expected '=' in let expression");
1594 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001595
David Greenee1b46912009-06-08 20:23:18 +00001596 RecordVal *Field = CurRec->getValue(FieldName);
1597 if (Field == 0)
1598 return TokError("Value '" + FieldName + "' unknown!");
1599
1600 RecTy *Type = Field->getType();
Bob Wilson21870412009-11-22 04:24:42 +00001601
David Greene05bce0b2011-07-29 22:43:06 +00001602 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001603 if (Val == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001604
Chris Lattnerf4601652007-11-22 20:49:04 +00001605 if (Lex.getCode() != tgtok::semi)
1606 return TokError("expected ';' after let expression");
1607 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001608
Chris Lattnerf4601652007-11-22 20:49:04 +00001609 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1610}
1611
1612/// ParseBody - Read the body of a class or def. Return true on error, false on
1613/// success.
1614///
1615/// Body ::= ';'
1616/// Body ::= '{' BodyList '}'
1617/// BodyList BodyItem*
1618///
1619bool TGParser::ParseBody(Record *CurRec) {
1620 // If this is a null definition, just eat the semi and return.
1621 if (Lex.getCode() == tgtok::semi) {
1622 Lex.Lex();
1623 return false;
1624 }
Bob Wilson21870412009-11-22 04:24:42 +00001625
Chris Lattnerf4601652007-11-22 20:49:04 +00001626 if (Lex.getCode() != tgtok::l_brace)
1627 return TokError("Expected ';' or '{' to start body");
1628 // Eat the '{'.
1629 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001630
Chris Lattnerf4601652007-11-22 20:49:04 +00001631 while (Lex.getCode() != tgtok::r_brace)
1632 if (ParseBodyItem(CurRec))
1633 return true;
1634
1635 // Eat the '}'.
1636 Lex.Lex();
1637 return false;
1638}
1639
1640/// ParseObjectBody - Parse the body of a def or class. This consists of an
1641/// optional ClassList followed by a Body. CurRec is the current def or class
1642/// that is being parsed.
1643///
1644/// ObjectBody ::= BaseClassList Body
1645/// BaseClassList ::= /*empty*/
1646/// BaseClassList ::= ':' BaseClassListNE
1647/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1648///
1649bool TGParser::ParseObjectBody(Record *CurRec) {
1650 // If there is a baseclass list, read it.
1651 if (Lex.getCode() == tgtok::colon) {
1652 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001653
Chris Lattnerf4601652007-11-22 20:49:04 +00001654 // Read all of the subclasses.
1655 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1656 while (1) {
1657 // Check for error.
1658 if (SubClass.Rec == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001659
Chris Lattnerf4601652007-11-22 20:49:04 +00001660 // Add it.
1661 if (AddSubClass(CurRec, SubClass))
1662 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001663
Chris Lattnerf4601652007-11-22 20:49:04 +00001664 if (Lex.getCode() != tgtok::comma) break;
1665 Lex.Lex(); // eat ','.
1666 SubClass = ParseSubClassReference(CurRec, false);
1667 }
1668 }
1669
1670 // Process any variables on the let stack.
1671 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1672 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1673 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1674 LetStack[i][j].Bits, LetStack[i][j].Value))
1675 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001676
Chris Lattnerf4601652007-11-22 20:49:04 +00001677 return ParseBody(CurRec);
1678}
1679
Chris Lattnerf4601652007-11-22 20:49:04 +00001680/// ParseDef - Parse and return a top level or multiclass def, return the record
1681/// corresponding to it. This returns null on error.
1682///
1683/// DefInst ::= DEF ObjectName ObjectBody
1684///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001685bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001686 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001687 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson21870412009-11-22 04:24:42 +00001688 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4601652007-11-22 20:49:04 +00001689
1690 // Parse ObjectName and make a record for it.
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001691 Record *CurRec = new Record(ParseObjectName(), DefLoc, Records);
Bob Wilson21870412009-11-22 04:24:42 +00001692
Chris Lattnerf4601652007-11-22 20:49:04 +00001693 if (!CurMultiClass) {
1694 // Top-level def definition.
Bob Wilson21870412009-11-22 04:24:42 +00001695
Chris Lattnerf4601652007-11-22 20:49:04 +00001696 // Ensure redefinition doesn't happen.
1697 if (Records.getDef(CurRec->getName())) {
David Greene2c49fbb2011-10-19 13:03:45 +00001698 Error(DefLoc, "def '" + CurRec->getNameInitAsString()
1699 + "' already defined");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001700 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001701 }
1702 Records.addDef(CurRec);
1703 } else {
1704 // Otherwise, a def inside a multiclass, add it to the multiclass.
1705 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene91919cd2011-10-19 13:03:51 +00001706 if (CurMultiClass->DefPrototypes[i]->getNameInit()
1707 == CurRec->getNameInit()) {
1708 Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
Chris Lattnerf4601652007-11-22 20:49:04 +00001709 "' already defined in this multiclass!");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001710 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001711 }
1712 CurMultiClass->DefPrototypes.push_back(CurRec);
1713 }
Bob Wilson21870412009-11-22 04:24:42 +00001714
Chris Lattnerf4601652007-11-22 20:49:04 +00001715 if (ParseObjectBody(CurRec))
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001716 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001717
Chris Lattnerf4601652007-11-22 20:49:04 +00001718 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
David Greene0d886402011-08-10 18:27:46 +00001719 // See Record::setName(). This resolve step will see any new name
1720 // for the def that might have been created when resolving
1721 // inheritance, values and arguments above.
Chris Lattnerf4601652007-11-22 20:49:04 +00001722 CurRec->resolveReferences();
Bob Wilson21870412009-11-22 04:24:42 +00001723
Chris Lattnerf4601652007-11-22 20:49:04 +00001724 // If ObjectBody has template arguments, it's an error.
1725 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001726
1727 if (CurMultiClass) {
1728 // Copy the template arguments for the multiclass into the def.
David Greenee22b3212011-10-19 13:02:42 +00001729 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001730 CurMultiClass->Rec.getTemplateArgs();
1731
1732 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1733 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1734 assert(RV && "Template arg doesn't exist?");
1735 CurRec->addValue(*RV);
1736 }
1737 }
1738
1739 return false;
Chris Lattnerf4601652007-11-22 20:49:04 +00001740}
1741
Chris Lattnerf4601652007-11-22 20:49:04 +00001742/// ParseClass - Parse a tblgen class definition.
1743///
1744/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
1745///
1746bool TGParser::ParseClass() {
1747 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
1748 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001749
Chris Lattnerf4601652007-11-22 20:49:04 +00001750 if (Lex.getCode() != tgtok::Id)
1751 return TokError("expected class name after 'class' keyword");
Bob Wilson21870412009-11-22 04:24:42 +00001752
Chris Lattnerf4601652007-11-22 20:49:04 +00001753 Record *CurRec = Records.getClass(Lex.getCurStrVal());
1754 if (CurRec) {
1755 // If the body was previously defined, this is an error.
David Greenee3385652011-10-19 13:04:13 +00001756 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4601652007-11-22 20:49:04 +00001757 !CurRec->getSuperClasses().empty() ||
1758 !CurRec->getTemplateArgs().empty())
David Greene69a23942011-10-19 13:03:58 +00001759 return TokError("Class '" + CurRec->getNameInitAsString()
1760 + "' already defined");
Chris Lattnerf4601652007-11-22 20:49:04 +00001761 } else {
1762 // If this is the first reference to this class, create and add it.
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001763 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001764 Records.addClass(CurRec);
1765 }
1766 Lex.Lex(); // eat the name.
Bob Wilson21870412009-11-22 04:24:42 +00001767
Chris Lattnerf4601652007-11-22 20:49:04 +00001768 // If there are template args, parse them.
1769 if (Lex.getCode() == tgtok::less)
1770 if (ParseTemplateArgList(CurRec))
1771 return true;
1772
1773 // Finally, parse the object body.
1774 return ParseObjectBody(CurRec);
1775}
1776
1777/// ParseLetList - Parse a non-empty list of assignment expressions into a list
1778/// of LetRecords.
1779///
1780/// LetList ::= LetItem (',' LetItem)*
1781/// LetItem ::= ID OptionalRangeList '=' Value
1782///
1783std::vector<LetRecord> TGParser::ParseLetList() {
1784 std::vector<LetRecord> Result;
Bob Wilson21870412009-11-22 04:24:42 +00001785
Chris Lattnerf4601652007-11-22 20:49:04 +00001786 while (1) {
1787 if (Lex.getCode() != tgtok::Id) {
1788 TokError("expected identifier in let definition");
1789 return std::vector<LetRecord>();
1790 }
1791 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001792 SMLoc NameLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +00001793 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4601652007-11-22 20:49:04 +00001794
1795 // Check for an optional RangeList.
1796 std::vector<unsigned> Bits;
Bob Wilson21870412009-11-22 04:24:42 +00001797 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4601652007-11-22 20:49:04 +00001798 return std::vector<LetRecord>();
1799 std::reverse(Bits.begin(), Bits.end());
Bob Wilson21870412009-11-22 04:24:42 +00001800
Chris Lattnerf4601652007-11-22 20:49:04 +00001801 if (Lex.getCode() != tgtok::equal) {
1802 TokError("expected '=' in let expression");
1803 return std::vector<LetRecord>();
1804 }
1805 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001806
David Greene05bce0b2011-07-29 22:43:06 +00001807 Init *Val = ParseValue(0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001808 if (Val == 0) return std::vector<LetRecord>();
Bob Wilson21870412009-11-22 04:24:42 +00001809
Chris Lattnerf4601652007-11-22 20:49:04 +00001810 // Now that we have everything, add the record.
1811 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson21870412009-11-22 04:24:42 +00001812
Chris Lattnerf4601652007-11-22 20:49:04 +00001813 if (Lex.getCode() != tgtok::comma)
1814 return Result;
Bob Wilson21870412009-11-22 04:24:42 +00001815 Lex.Lex(); // eat the comma.
Chris Lattnerf4601652007-11-22 20:49:04 +00001816 }
1817}
1818
1819/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001820/// different related productions. This works inside multiclasses too.
Chris Lattnerf4601652007-11-22 20:49:04 +00001821///
1822/// Object ::= LET LetList IN '{' ObjectList '}'
1823/// Object ::= LET LetList IN Object
1824///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001825bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001826 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
1827 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001828
Chris Lattnerf4601652007-11-22 20:49:04 +00001829 // Add this entry to the let stack.
1830 std::vector<LetRecord> LetInfo = ParseLetList();
1831 if (LetInfo.empty()) return true;
1832 LetStack.push_back(LetInfo);
1833
1834 if (Lex.getCode() != tgtok::In)
1835 return TokError("expected 'in' at end of top-level 'let'");
1836 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001837
Chris Lattnerf4601652007-11-22 20:49:04 +00001838 // If this is a scalar let, just handle it now
1839 if (Lex.getCode() != tgtok::l_brace) {
1840 // LET LetList IN Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001841 if (ParseObject(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00001842 return true;
1843 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001844 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001845 // Otherwise, this is a group let.
1846 Lex.Lex(); // eat the '{'.
Bob Wilson21870412009-11-22 04:24:42 +00001847
Chris Lattnerf4601652007-11-22 20:49:04 +00001848 // Parse the object list.
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001849 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00001850 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001851
Chris Lattnerf4601652007-11-22 20:49:04 +00001852 if (Lex.getCode() != tgtok::r_brace) {
1853 TokError("expected '}' at end of top level let command");
1854 return Error(BraceLoc, "to match this '{'");
1855 }
1856 Lex.Lex();
1857 }
Bob Wilson21870412009-11-22 04:24:42 +00001858
Chris Lattnerf4601652007-11-22 20:49:04 +00001859 // Outside this let scope, this let block is not active.
1860 LetStack.pop_back();
1861 return false;
1862}
1863
Chris Lattnerf4601652007-11-22 20:49:04 +00001864/// ParseMultiClass - Parse a multiclass definition.
1865///
Bob Wilson32558652009-04-28 19:41:44 +00001866/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
1867/// ':' BaseMultiClassList '{' MultiClassDef+ '}'
Chris Lattnerf4601652007-11-22 20:49:04 +00001868///
1869bool TGParser::ParseMultiClass() {
1870 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
1871 Lex.Lex(); // Eat the multiclass token.
1872
1873 if (Lex.getCode() != tgtok::Id)
1874 return TokError("expected identifier after multiclass for name");
1875 std::string Name = Lex.getCurStrVal();
Bob Wilson21870412009-11-22 04:24:42 +00001876
Chris Lattnerf4601652007-11-22 20:49:04 +00001877 if (MultiClasses.count(Name))
1878 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson21870412009-11-22 04:24:42 +00001879
Chris Lattner67db8832010-12-13 00:23:57 +00001880 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
1881 Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001882 Lex.Lex(); // Eat the identifier.
Bob Wilson21870412009-11-22 04:24:42 +00001883
Chris Lattnerf4601652007-11-22 20:49:04 +00001884 // If there are template args, parse them.
1885 if (Lex.getCode() == tgtok::less)
1886 if (ParseTemplateArgList(0))
1887 return true;
1888
David Greened34a73b2009-04-24 16:55:41 +00001889 bool inherits = false;
1890
David Greenede444af2009-04-22 16:42:54 +00001891 // If there are submulticlasses, parse them.
1892 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00001893 inherits = true;
1894
David Greenede444af2009-04-22 16:42:54 +00001895 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00001896
David Greenede444af2009-04-22 16:42:54 +00001897 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00001898 SubMultiClassReference SubMultiClass =
1899 ParseSubMultiClassReference(CurMultiClass);
David Greenede444af2009-04-22 16:42:54 +00001900 while (1) {
1901 // Check for error.
1902 if (SubMultiClass.MC == 0) return true;
Bob Wilson32558652009-04-28 19:41:44 +00001903
David Greenede444af2009-04-22 16:42:54 +00001904 // Add it.
1905 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
1906 return true;
Bob Wilson32558652009-04-28 19:41:44 +00001907
David Greenede444af2009-04-22 16:42:54 +00001908 if (Lex.getCode() != tgtok::comma) break;
1909 Lex.Lex(); // eat ','.
1910 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
1911 }
1912 }
1913
David Greened34a73b2009-04-24 16:55:41 +00001914 if (Lex.getCode() != tgtok::l_brace) {
1915 if (!inherits)
1916 return TokError("expected '{' in multiclass definition");
Bob Wilson21870412009-11-22 04:24:42 +00001917 else if (Lex.getCode() != tgtok::semi)
1918 return TokError("expected ';' in multiclass definition");
David Greened34a73b2009-04-24 16:55:41 +00001919 else
Bob Wilson21870412009-11-22 04:24:42 +00001920 Lex.Lex(); // eat the ';'.
1921 } else {
David Greened34a73b2009-04-24 16:55:41 +00001922 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
1923 return TokError("multiclass must contain at least one def");
Bob Wilson21870412009-11-22 04:24:42 +00001924
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001925 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001926 switch (Lex.getCode()) {
1927 default:
David Greenea1b1b792011-10-07 18:25:05 +00001928 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001929 case tgtok::Let:
1930 case tgtok::Def:
1931 case tgtok::Defm:
1932 if (ParseObject(CurMultiClass))
1933 return true;
1934 break;
1935 }
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001936 }
David Greened34a73b2009-04-24 16:55:41 +00001937 Lex.Lex(); // eat the '}'.
1938 }
Bob Wilson21870412009-11-22 04:24:42 +00001939
Chris Lattnerf4601652007-11-22 20:49:04 +00001940 CurMultiClass = 0;
1941 return false;
1942}
1943
David Greenee499a2d2011-10-05 22:42:07 +00001944Record *TGParser::
1945InstantiateMulticlassDef(MultiClass &MC,
1946 Record *DefProto,
1947 const std::string &DefmPrefix,
1948 SMLoc DefmPrefixLoc) {
1949 // Add in the defm name. If the defm prefix is empty, give each
1950 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
1951 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
1952 // as a prefix.
1953 std::string DefName = DefProto->getName();
1954 if (DefmPrefix.empty()) {
1955 DefName = GetNewAnonymousName();
1956 } else {
1957 std::string::size_type idx = DefName.find("#NAME#");
1958 if (idx != std::string::npos) {
1959 DefName.replace(idx, 6, DefmPrefix);
1960 } else {
1961 // Add the suffix to the defm name to get the new name.
1962 DefName = DefmPrefix + DefName;
1963 }
1964 }
1965
1966 Record *CurRec = new Record(DefName, DefmPrefixLoc, Records);
1967
1968 SubClassReference Ref;
1969 Ref.RefLoc = DefmPrefixLoc;
1970 Ref.Rec = DefProto;
1971 AddSubClass(CurRec, Ref);
1972
1973 return CurRec;
1974}
1975
1976bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
1977 Record *CurRec,
1978 SMLoc DefmPrefixLoc,
1979 SMLoc SubClassLoc,
David Greenee22b3212011-10-19 13:02:42 +00001980 const std::vector<Init *> &TArgs,
David Greenee499a2d2011-10-05 22:42:07 +00001981 std::vector<Init *> &TemplateVals,
1982 bool DeleteArgs) {
1983 // Loop over all of the template arguments, setting them to the specified
1984 // value or leaving them as the default if necessary.
1985 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1986 // Check if a value is specified for this temp-arg.
1987 if (i < TemplateVals.size()) {
1988 // Set it now.
1989 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
1990 TemplateVals[i]))
1991 return true;
1992
1993 // Resolve it next.
1994 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
1995
1996 if (DeleteArgs)
1997 // Now remove it.
1998 CurRec->removeValue(TArgs[i]);
1999
2000 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2001 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenee22b3212011-10-19 13:02:42 +00002002 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2003 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2004 + "'");
David Greenee499a2d2011-10-05 22:42:07 +00002005 }
2006 }
2007 return false;
2008}
2009
2010bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2011 Record *CurRec,
2012 Record *DefProto,
2013 SMLoc DefmPrefixLoc) {
2014 // If the mdef is inside a 'let' expression, add to each def.
2015 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2016 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2017 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2018 LetStack[i][j].Bits, LetStack[i][j].Value))
2019 return Error(DefmPrefixLoc, "when instantiating this defm");
2020
2021 // Ensure redefinition doesn't happen.
2022 if (Records.getDef(CurRec->getName()))
2023 return Error(DefmPrefixLoc, "def '" + CurRec->getName() +
2024 "' already defined, instantiating defm with subdef '" +
2025 DefProto->getName() + "'");
2026
2027 // Don't create a top level definition for defm inside multiclasses,
2028 // instead, only update the prototypes and bind the template args
2029 // with the new created definition.
2030 if (CurMultiClass) {
2031 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2032 i != e; ++i)
David Greene22dde7e2011-10-19 13:04:02 +00002033 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2034 == CurRec->getNameInit())
2035 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
David Greenee499a2d2011-10-05 22:42:07 +00002036 "' already defined in this multiclass!");
2037 CurMultiClass->DefPrototypes.push_back(CurRec);
2038
2039 // Copy the template arguments for the multiclass into the new def.
David Greenee22b3212011-10-19 13:02:42 +00002040 const std::vector<Init *> &TA =
David Greenee499a2d2011-10-05 22:42:07 +00002041 CurMultiClass->Rec.getTemplateArgs();
2042
2043 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2044 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2045 assert(RV && "Template arg doesn't exist?");
2046 CurRec->addValue(*RV);
2047 }
2048 } else {
2049 Records.addDef(CurRec);
2050 }
2051
2052 return false;
2053}
2054
Chris Lattnerf4601652007-11-22 20:49:04 +00002055/// ParseDefm - Parse the instantiation of a multiclass.
2056///
2057/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2058///
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002059bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002060 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Bob Wilson21870412009-11-22 04:24:42 +00002061
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002062 std::string DefmPrefix;
2063 if (Lex.Lex() == tgtok::Id) { // eat the defm.
2064 DefmPrefix = Lex.getCurStrVal();
2065 Lex.Lex(); // Eat the defm prefix.
2066 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +00002067
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002068 SMLoc DefmPrefixLoc = Lex.getLoc();
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002069 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4601652007-11-22 20:49:04 +00002070 return TokError("expected ':' after defm identifier");
Bob Wilson21870412009-11-22 04:24:42 +00002071
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002072 // Keep track of the new generated record definitions.
2073 std::vector<Record*> NewRecDefs;
2074
2075 // This record also inherits from a regular class (non-multiclass)?
2076 bool InheritFromClass = false;
2077
Chris Lattnerf4601652007-11-22 20:49:04 +00002078 // eat the colon.
2079 Lex.Lex();
2080
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002081 SMLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002082 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene56546132009-04-22 22:17:51 +00002083
2084 while (1) {
2085 if (Ref.Rec == 0) return true;
2086
2087 // To instantiate a multiclass, we need to first get the multiclass, then
2088 // instantiate each def contained in the multiclass with the SubClassRef
2089 // template parameters.
2090 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2091 assert(MC && "Didn't lookup multiclass correctly?");
David Greene05bce0b2011-07-29 22:43:06 +00002092 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greene56546132009-04-22 22:17:51 +00002093
2094 // Verify that the correct number of template arguments were specified.
David Greenee22b3212011-10-19 13:02:42 +00002095 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greene56546132009-04-22 22:17:51 +00002096 if (TArgs.size() < TemplateVals.size())
2097 return Error(SubClassLoc,
2098 "more template args specified than multiclass expects");
2099
2100 // Loop over all the def's in the multiclass, instantiating each one.
2101 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2102 Record *DefProto = MC->DefPrototypes[i];
2103
David Greenee499a2d2011-10-05 22:42:07 +00002104 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix, DefmPrefixLoc);
David Greene065f2592009-05-05 16:28:25 +00002105
David Greenee499a2d2011-10-05 22:42:07 +00002106 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmPrefixLoc, SubClassLoc,
2107 TArgs, TemplateVals, true/*Delete args*/))
2108 return Error(SubClassLoc, "could not instantiate def");
David Greene56546132009-04-22 22:17:51 +00002109
David Greenee499a2d2011-10-05 22:42:07 +00002110 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmPrefixLoc))
2111 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002112
2113 NewRecDefs.push_back(CurRec);
David Greene56546132009-04-22 22:17:51 +00002114 }
2115
David Greenee499a2d2011-10-05 22:42:07 +00002116
David Greene56546132009-04-22 22:17:51 +00002117 if (Lex.getCode() != tgtok::comma) break;
2118 Lex.Lex(); // eat ','.
2119
2120 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002121
2122 // A defm can inherit from regular classes (non-multiclass) as
2123 // long as they come in the end of the inheritance list.
2124 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0);
2125
2126 if (InheritFromClass)
2127 break;
2128
David Greene56546132009-04-22 22:17:51 +00002129 Ref = ParseSubClassReference(0, true);
2130 }
2131
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002132 if (InheritFromClass) {
2133 // Process all the classes to inherit as if they were part of a
2134 // regular 'def' and inherit all record values.
2135 SubClassReference SubClass = ParseSubClassReference(0, false);
2136 while (1) {
2137 // Check for error.
2138 if (SubClass.Rec == 0) return true;
2139
2140 // Get the expanded definition prototypes and teach them about
2141 // the record values the current class to inherit has
2142 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2143 Record *CurRec = NewRecDefs[i];
2144
2145 // Add it.
2146 if (AddSubClass(CurRec, SubClass))
2147 return true;
2148
2149 // Process any variables on the let stack.
2150 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2151 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2152 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2153 LetStack[i][j].Bits, LetStack[i][j].Value))
2154 return true;
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002155 }
2156
2157 if (Lex.getCode() != tgtok::comma) break;
2158 Lex.Lex(); // eat ','.
2159 SubClass = ParseSubClassReference(0, false);
2160 }
2161 }
2162
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002163 if (!CurMultiClass)
2164 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene0d886402011-08-10 18:27:46 +00002165 // See Record::setName(). This resolve step will see any new
2166 // name for the def that might have been created when resolving
2167 // inheritance, values and arguments above.
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002168 NewRecDefs[i]->resolveReferences();
2169
Chris Lattnerf4601652007-11-22 20:49:04 +00002170 if (Lex.getCode() != tgtok::semi)
2171 return TokError("expected ';' at end of defm");
2172 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002173
Chris Lattnerf4601652007-11-22 20:49:04 +00002174 return false;
2175}
2176
2177/// ParseObject
2178/// Object ::= ClassInst
2179/// Object ::= DefInst
2180/// Object ::= MultiClassInst
2181/// Object ::= DefMInst
2182/// Object ::= LETCommand '{' ObjectList '}'
2183/// Object ::= LETCommand Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002184bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002185 switch (Lex.getCode()) {
Chris Lattnerd6d9dd92010-10-31 19:27:15 +00002186 default:
2187 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002188 case tgtok::Let: return ParseTopLevelLet(MC);
2189 case tgtok::Def: return ParseDef(MC);
2190 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4601652007-11-22 20:49:04 +00002191 case tgtok::Class: return ParseClass();
2192 case tgtok::MultiClass: return ParseMultiClass();
2193 }
2194}
2195
2196/// ParseObjectList
2197/// ObjectList :== Object*
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002198bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002199 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002200 if (ParseObject(MC))
Chris Lattnerf4601652007-11-22 20:49:04 +00002201 return true;
2202 }
2203 return false;
2204}
2205
Chris Lattnerf4601652007-11-22 20:49:04 +00002206bool TGParser::ParseFile() {
2207 Lex.Lex(); // Prime the lexer.
2208 if (ParseObjectList()) return true;
Bob Wilson21870412009-11-22 04:24:42 +00002209
Chris Lattnerf4601652007-11-22 20:49:04 +00002210 // If we have unread input at the end of the file, report it.
2211 if (Lex.getCode() == tgtok::Eof)
2212 return false;
Bob Wilson21870412009-11-22 04:24:42 +00002213
Chris Lattnerf4601652007-11-22 20:49:04 +00002214 return TokError("Unexpected input at top level");
2215}
2216