blob: 53e4fdcae0dbe003d99351f5a5843fbac77f284d [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.
David Greenea9e07dd2011-10-19 13:04:29 +0000309/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4601652007-11-22 20:49:04 +0000310/// ObjectName ::= /*empty*/
311///
David Greenea9e07dd2011-10-19 13:04:29 +0000312Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
313 switch (Lex.getCode()) {
314 case tgtok::colon:
315 case tgtok::semi:
316 case tgtok::l_brace:
317 // These are all of the tokens that can begin an object body.
318 // Some of these can also begin values but we disallow those cases
319 // because they are unlikely to be useful.
320 return StringInit::get(GetNewAnonymousName());
321 break;
322 default:
323 break;
324 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000325
David Greenea9e07dd2011-10-19 13:04:29 +0000326 Record *CurRec = 0;
327 if (CurMultiClass)
328 CurRec = &CurMultiClass->Rec;
329
330 RecTy *Type = 0;
331 if (CurRec) {
332 const TypedInit *CurRecName =
333 dynamic_cast<const TypedInit *>(CurRec->getNameInit());
334 if (!CurRecName) {
335 TokError("Record name is not typed!");
336 return 0;
337 }
338 Type = CurRecName->getType();
339 }
340
341 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4601652007-11-22 20:49:04 +0000342}
343
Chris Lattnerf4601652007-11-22 20:49:04 +0000344/// ParseClassID - Parse and resolve a reference to a class name. This returns
345/// null on error.
346///
347/// ClassID ::= ID
348///
349Record *TGParser::ParseClassID() {
350 if (Lex.getCode() != tgtok::Id) {
351 TokError("expected name for ClassID");
352 return 0;
353 }
Bob Wilson21870412009-11-22 04:24:42 +0000354
Chris Lattnerf4601652007-11-22 20:49:04 +0000355 Record *Result = Records.getClass(Lex.getCurStrVal());
356 if (Result == 0)
357 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson21870412009-11-22 04:24:42 +0000358
Chris Lattnerf4601652007-11-22 20:49:04 +0000359 Lex.Lex();
360 return Result;
361}
362
Bob Wilson32558652009-04-28 19:41:44 +0000363/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
364/// This returns null on error.
David Greenede444af2009-04-22 16:42:54 +0000365///
366/// MultiClassID ::= ID
367///
368MultiClass *TGParser::ParseMultiClassID() {
369 if (Lex.getCode() != tgtok::Id) {
370 TokError("expected name for ClassID");
371 return 0;
372 }
Bob Wilson32558652009-04-28 19:41:44 +0000373
David Greenede444af2009-04-22 16:42:54 +0000374 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
375 if (Result == 0)
376 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson32558652009-04-28 19:41:44 +0000377
David Greenede444af2009-04-22 16:42:54 +0000378 Lex.Lex();
379 return Result;
380}
381
Chris Lattnerf4601652007-11-22 20:49:04 +0000382Record *TGParser::ParseDefmID() {
383 if (Lex.getCode() != tgtok::Id) {
384 TokError("expected multiclass name");
385 return 0;
386 }
Bob Wilson21870412009-11-22 04:24:42 +0000387
Chris Lattnerf4601652007-11-22 20:49:04 +0000388 MultiClass *MC = MultiClasses[Lex.getCurStrVal()];
389 if (MC == 0) {
390 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
391 return 0;
392 }
Bob Wilson21870412009-11-22 04:24:42 +0000393
Chris Lattnerf4601652007-11-22 20:49:04 +0000394 Lex.Lex();
395 return &MC->Rec;
Bob Wilson21870412009-11-22 04:24:42 +0000396}
Chris Lattnerf4601652007-11-22 20:49:04 +0000397
398
399/// ParseSubClassReference - Parse a reference to a subclass or to a templated
400/// subclass. This returns a SubClassRefTy with a null Record* on error.
401///
402/// SubClassRef ::= ClassID
403/// SubClassRef ::= ClassID '<' ValueList '>'
404///
405SubClassReference TGParser::
406ParseSubClassReference(Record *CurRec, bool isDefm) {
407 SubClassReference Result;
408 Result.RefLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +0000409
Chris Lattnerf4601652007-11-22 20:49:04 +0000410 if (isDefm)
411 Result.Rec = ParseDefmID();
412 else
413 Result.Rec = ParseClassID();
414 if (Result.Rec == 0) return Result;
Bob Wilson21870412009-11-22 04:24:42 +0000415
Chris Lattnerf4601652007-11-22 20:49:04 +0000416 // If there is no template arg list, we're done.
417 if (Lex.getCode() != tgtok::less)
418 return Result;
419 Lex.Lex(); // Eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000420
Chris Lattnerf4601652007-11-22 20:49:04 +0000421 if (Lex.getCode() == tgtok::greater) {
422 TokError("subclass reference requires a non-empty list of template values");
423 Result.Rec = 0;
424 return Result;
425 }
Bob Wilson21870412009-11-22 04:24:42 +0000426
David Greenee1b46912009-06-08 20:23:18 +0000427 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4601652007-11-22 20:49:04 +0000428 if (Result.TemplateArgs.empty()) {
429 Result.Rec = 0; // Error parsing value list.
430 return Result;
431 }
Bob Wilson21870412009-11-22 04:24:42 +0000432
Chris Lattnerf4601652007-11-22 20:49:04 +0000433 if (Lex.getCode() != tgtok::greater) {
434 TokError("expected '>' in template value list");
435 Result.Rec = 0;
436 return Result;
437 }
438 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000439
Chris Lattnerf4601652007-11-22 20:49:04 +0000440 return Result;
441}
442
Bob Wilson32558652009-04-28 19:41:44 +0000443/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
444/// templated submulticlass. This returns a SubMultiClassRefTy with a null
445/// Record* on error.
David Greenede444af2009-04-22 16:42:54 +0000446///
447/// SubMultiClassRef ::= MultiClassID
448/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
449///
450SubMultiClassReference TGParser::
451ParseSubMultiClassReference(MultiClass *CurMC) {
452 SubMultiClassReference Result;
453 Result.RefLoc = Lex.getLoc();
Bob Wilson32558652009-04-28 19:41:44 +0000454
David Greenede444af2009-04-22 16:42:54 +0000455 Result.MC = ParseMultiClassID();
456 if (Result.MC == 0) return Result;
Bob Wilson32558652009-04-28 19:41:44 +0000457
David Greenede444af2009-04-22 16:42:54 +0000458 // If there is no template arg list, we're done.
459 if (Lex.getCode() != tgtok::less)
460 return Result;
461 Lex.Lex(); // Eat the '<'
Bob Wilson32558652009-04-28 19:41:44 +0000462
David Greenede444af2009-04-22 16:42:54 +0000463 if (Lex.getCode() == tgtok::greater) {
464 TokError("subclass reference requires a non-empty list of template values");
465 Result.MC = 0;
466 return Result;
467 }
Bob Wilson32558652009-04-28 19:41:44 +0000468
David Greenee1b46912009-06-08 20:23:18 +0000469 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greenede444af2009-04-22 16:42:54 +0000470 if (Result.TemplateArgs.empty()) {
471 Result.MC = 0; // Error parsing value list.
472 return Result;
473 }
Bob Wilson32558652009-04-28 19:41:44 +0000474
David Greenede444af2009-04-22 16:42:54 +0000475 if (Lex.getCode() != tgtok::greater) {
476 TokError("expected '>' in template value list");
477 Result.MC = 0;
478 return Result;
479 }
480 Lex.Lex();
481
482 return Result;
483}
484
Chris Lattnerf4601652007-11-22 20:49:04 +0000485/// ParseRangePiece - Parse a bit/value range.
486/// RangePiece ::= INTVAL
487/// RangePiece ::= INTVAL '-' INTVAL
488/// RangePiece ::= INTVAL INTVAL
489bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner811281e2008-01-10 07:01:53 +0000490 if (Lex.getCode() != tgtok::IntVal) {
491 TokError("expected integer or bitrange");
492 return true;
493 }
Dan Gohman63f97202008-10-17 01:33:43 +0000494 int64_t Start = Lex.getCurIntVal();
495 int64_t End;
Bob Wilson21870412009-11-22 04:24:42 +0000496
Chris Lattnerf4601652007-11-22 20:49:04 +0000497 if (Start < 0)
498 return TokError("invalid range, cannot be negative");
Bob Wilson21870412009-11-22 04:24:42 +0000499
Chris Lattnerf4601652007-11-22 20:49:04 +0000500 switch (Lex.Lex()) { // eat first character.
Bob Wilson21870412009-11-22 04:24:42 +0000501 default:
Chris Lattnerf4601652007-11-22 20:49:04 +0000502 Ranges.push_back(Start);
503 return false;
504 case tgtok::minus:
505 if (Lex.Lex() != tgtok::IntVal) {
506 TokError("expected integer value as end of range");
507 return true;
508 }
509 End = Lex.getCurIntVal();
510 break;
511 case tgtok::IntVal:
512 End = -Lex.getCurIntVal();
513 break;
514 }
Bob Wilson21870412009-11-22 04:24:42 +0000515 if (End < 0)
Chris Lattnerf4601652007-11-22 20:49:04 +0000516 return TokError("invalid range, cannot be negative");
517 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000518
Chris Lattnerf4601652007-11-22 20:49:04 +0000519 // Add to the range.
520 if (Start < End) {
521 for (; Start <= End; ++Start)
522 Ranges.push_back(Start);
523 } else {
524 for (; Start >= End; --Start)
525 Ranges.push_back(Start);
526 }
527 return false;
528}
529
530/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
531///
532/// RangeList ::= RangePiece (',' RangePiece)*
533///
534std::vector<unsigned> TGParser::ParseRangeList() {
535 std::vector<unsigned> Result;
Bob Wilson21870412009-11-22 04:24:42 +0000536
Chris Lattnerf4601652007-11-22 20:49:04 +0000537 // Parse the first piece.
538 if (ParseRangePiece(Result))
539 return std::vector<unsigned>();
540 while (Lex.getCode() == tgtok::comma) {
541 Lex.Lex(); // Eat the comma.
542
543 // Parse the next range piece.
544 if (ParseRangePiece(Result))
545 return std::vector<unsigned>();
546 }
547 return Result;
548}
549
550/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
551/// OptionalRangeList ::= '<' RangeList '>'
552/// OptionalRangeList ::= /*empty*/
553bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
554 if (Lex.getCode() != tgtok::less)
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::greater) {
565 TokError("expected '>' at end of range list");
566 return Error(StartLoc, "to match this '<'");
567 }
568 Lex.Lex(); // eat the '>'.
569 return false;
570}
571
572/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
573/// OptionalBitList ::= '{' RangeList '}'
574/// OptionalBitList ::= /*empty*/
575bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
576 if (Lex.getCode() != tgtok::l_brace)
577 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000578
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000579 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000580 Lex.Lex(); // eat the '{'
Bob Wilson21870412009-11-22 04:24:42 +0000581
Chris Lattnerf4601652007-11-22 20:49:04 +0000582 // Parse the range list.
583 Ranges = ParseRangeList();
584 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000585
Chris Lattnerf4601652007-11-22 20:49:04 +0000586 if (Lex.getCode() != tgtok::r_brace) {
587 TokError("expected '}' at end of bit list");
588 return Error(StartLoc, "to match this '{'");
589 }
590 Lex.Lex(); // eat the '}'.
591 return false;
592}
593
594
595/// ParseType - Parse and return a tblgen type. This returns null on error.
596///
597/// Type ::= STRING // string type
598/// Type ::= BIT // bit type
599/// Type ::= BITS '<' INTVAL '>' // bits<x> type
600/// Type ::= INT // int type
601/// Type ::= LIST '<' Type '>' // list<x> type
602/// Type ::= CODE // code type
603/// Type ::= DAG // dag type
604/// Type ::= ClassID // Record Type
605///
606RecTy *TGParser::ParseType() {
607 switch (Lex.getCode()) {
608 default: TokError("Unknown token when expecting a type"); return 0;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000609 case tgtok::String: Lex.Lex(); return StringRecTy::get();
610 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
611 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
612 case tgtok::Code: Lex.Lex(); return CodeRecTy::get();
613 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4601652007-11-22 20:49:04 +0000614 case tgtok::Id:
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000615 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Chris Lattnerf4601652007-11-22 20:49:04 +0000616 return 0;
617 case tgtok::Bits: {
618 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
619 TokError("expected '<' after bits type");
620 return 0;
621 }
622 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
623 TokError("expected integer in bits<n> type");
624 return 0;
625 }
Dan Gohman63f97202008-10-17 01:33:43 +0000626 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4601652007-11-22 20:49:04 +0000627 if (Lex.Lex() != tgtok::greater) { // Eat count.
628 TokError("expected '>' at end of bits<n> type");
629 return 0;
630 }
631 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000632 return BitsRecTy::get(Val);
Chris Lattnerf4601652007-11-22 20:49:04 +0000633 }
634 case tgtok::List: {
635 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
636 TokError("expected '<' after list type");
637 return 0;
638 }
639 Lex.Lex(); // Eat '<'
640 RecTy *SubType = ParseType();
641 if (SubType == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +0000642
Chris Lattnerf4601652007-11-22 20:49:04 +0000643 if (Lex.getCode() != tgtok::greater) {
644 TokError("expected '>' at end of list<ty> type");
645 return 0;
646 }
647 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000648 return ListRecTy::get(SubType);
Chris Lattnerf4601652007-11-22 20:49:04 +0000649 }
Bob Wilson21870412009-11-22 04:24:42 +0000650 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000651}
652
653/// ParseIDValue - Parse an ID as a value and decode what it means.
654///
655/// IDValue ::= ID [def local value]
656/// IDValue ::= ID [def template arg]
657/// IDValue ::= ID [multiclass local value]
658/// IDValue ::= ID [multiclass template argument]
659/// IDValue ::= ID [def name]
660///
David Greenef3744a02011-10-19 13:04:20 +0000661Init *TGParser::ParseIDValue(Record *CurRec, IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000662 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
663 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000664 SMLoc Loc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000665 Lex.Lex();
666 return ParseIDValue(CurRec, Name, Loc);
667}
668
669/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
670/// has already been read.
David Greene05bce0b2011-07-29 22:43:06 +0000671Init *TGParser::ParseIDValue(Record *CurRec,
David Greenef3744a02011-10-19 13:04:20 +0000672 const std::string &Name, SMLoc NameLoc,
673 IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000674 if (CurRec) {
675 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenedcd35c72011-07-29 19:07:07 +0000676 return VarInit::get(Name, RV->getType());
Bob Wilson21870412009-11-22 04:24:42 +0000677
David Greenee22b3212011-10-19 13:02:42 +0000678 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
679
David Greenecaa25c82011-10-05 22:42:54 +0000680 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +0000681 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
682 "::");
David Greenecaa25c82011-10-05 22:42:54 +0000683
Chris Lattnerf4601652007-11-22 20:49:04 +0000684 if (CurRec->isTemplateArg(TemplateArgName)) {
685 const RecordVal *RV = CurRec->getValue(TemplateArgName);
686 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000687 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000688 }
689 }
Bob Wilson21870412009-11-22 04:24:42 +0000690
Chris Lattnerf4601652007-11-22 20:49:04 +0000691 if (CurMultiClass) {
David Greenee22b3212011-10-19 13:02:42 +0000692 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
693 "::");
694
Chris Lattnerf4601652007-11-22 20:49:04 +0000695 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
696 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
697 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000698 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000699 }
700 }
Bob Wilson21870412009-11-22 04:24:42 +0000701
David Greenebbec2792011-10-19 13:04:21 +0000702 if (Mode == ParseNameMode)
703 return StringInit::get(Name);
704
Chris Lattnerf4601652007-11-22 20:49:04 +0000705 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000706 return DefInit::get(D);
Chris Lattnerf4601652007-11-22 20:49:04 +0000707
David Greenebbec2792011-10-19 13:04:21 +0000708 if (Mode == ParseValueMode) {
709 Error(NameLoc, "Variable not defined: '" + Name + "'");
710 return 0;
711 }
712
713 return StringInit::get(Name);
Chris Lattnerf4601652007-11-22 20:49:04 +0000714}
715
David Greened418c1b2009-05-14 20:54:48 +0000716/// ParseOperation - Parse an operator. This returns null on error.
717///
718/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
719///
David Greene05bce0b2011-07-29 22:43:06 +0000720Init *TGParser::ParseOperation(Record *CurRec) {
David Greened418c1b2009-05-14 20:54:48 +0000721 switch (Lex.getCode()) {
722 default:
723 TokError("unknown operation");
724 return 0;
725 break;
David Greene1434f662011-01-07 17:05:37 +0000726 case tgtok::XHead:
727 case tgtok::XTail:
728 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +0000729 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
730 UnOpInit::UnaryOp Code;
731 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000732
David Greenee6c27de2009-05-14 21:22:49 +0000733 switch (Lex.getCode()) {
734 default: assert(0 && "Unhandled code!");
735 case tgtok::XCast:
736 Lex.Lex(); // eat the operation
737 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000738
David Greenee6c27de2009-05-14 21:22:49 +0000739 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000740
David Greenee6c27de2009-05-14 21:22:49 +0000741 if (Type == 0) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000742 TokError("did not get type for unary operator");
David Greenee6c27de2009-05-14 21:22:49 +0000743 return 0;
744 }
David Greened418c1b2009-05-14 20:54:48 +0000745
David Greenee6c27de2009-05-14 21:22:49 +0000746 break;
David Greene1434f662011-01-07 17:05:37 +0000747 case tgtok::XHead:
David Greene5f9f9ba2009-05-14 22:38:31 +0000748 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000749 Code = UnOpInit::HEAD;
David Greene5f9f9ba2009-05-14 22:38:31 +0000750 break;
David Greene1434f662011-01-07 17:05:37 +0000751 case tgtok::XTail:
David Greene5f9f9ba2009-05-14 22:38:31 +0000752 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000753 Code = UnOpInit::TAIL;
David Greene5f9f9ba2009-05-14 22:38:31 +0000754 break;
David Greene1434f662011-01-07 17:05:37 +0000755 case tgtok::XEmpty:
David Greene5f9f9ba2009-05-14 22:38:31 +0000756 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000757 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000758 Type = IntRecTy::get();
David Greene5f9f9ba2009-05-14 22:38:31 +0000759 break;
David Greenee6c27de2009-05-14 21:22:49 +0000760 }
761 if (Lex.getCode() != tgtok::l_paren) {
762 TokError("expected '(' after unary operator");
763 return 0;
764 }
765 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000766
David Greene05bce0b2011-07-29 22:43:06 +0000767 Init *LHS = ParseValue(CurRec);
David Greenee6c27de2009-05-14 21:22:49 +0000768 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000769
David Greene1434f662011-01-07 17:05:37 +0000770 if (Code == UnOpInit::HEAD
771 || Code == UnOpInit::TAIL
772 || Code == UnOpInit::EMPTY) {
David Greene05bce0b2011-07-29 22:43:06 +0000773 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
774 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
775 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000776 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
777 TokError("expected list or string type argument in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000778 return 0;
779 }
780 if (LHSt) {
781 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
David Greenee1b46912009-06-08 20:23:18 +0000782 StringRecTy *SType = dynamic_cast<StringRecTy*>(LHSt->getType());
783 if (LType == 0 && SType == 0) {
784 TokError("expected list or string type argumnet in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000785 return 0;
786 }
787 }
788
David Greene1434f662011-01-07 17:05:37 +0000789 if (Code == UnOpInit::HEAD
790 || Code == UnOpInit::TAIL) {
David Greenee1b46912009-06-08 20:23:18 +0000791 if (LHSl == 0 && LHSt == 0) {
792 TokError("expected list type argumnet in unary operator");
793 return 0;
794 }
Bob Wilson21870412009-11-22 04:24:42 +0000795
David Greene5f9f9ba2009-05-14 22:38:31 +0000796 if (LHSl && LHSl->getSize() == 0) {
797 TokError("empty list argument in unary operator");
798 return 0;
799 }
800 if (LHSl) {
David Greene05bce0b2011-07-29 22:43:06 +0000801 Init *Item = LHSl->getElement(0);
802 TypedInit *Itemt = dynamic_cast<TypedInit*>(Item);
David Greene5f9f9ba2009-05-14 22:38:31 +0000803 if (Itemt == 0) {
804 TokError("untyped list element in unary operator");
805 return 0;
806 }
David Greene1434f662011-01-07 17:05:37 +0000807 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000808 Type = Itemt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000809 } else {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000810 Type = ListRecTy::get(Itemt->getType());
David Greene5f9f9ba2009-05-14 22:38:31 +0000811 }
Bob Wilson21870412009-11-22 04:24:42 +0000812 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000813 assert(LHSt && "expected list type argument in unary operator");
814 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
815 if (LType == 0) {
816 TokError("expected list type argumnet in unary operator");
817 return 0;
818 }
David Greene1434f662011-01-07 17:05:37 +0000819 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000820 Type = LType->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +0000821 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000822 Type = LType;
823 }
824 }
825 }
826 }
827
David Greenee6c27de2009-05-14 21:22:49 +0000828 if (Lex.getCode() != tgtok::r_paren) {
829 TokError("expected ')' in unary operator");
830 return 0;
831 }
832 Lex.Lex(); // eat the ')'
David Greenedcd35c72011-07-29 19:07:07 +0000833 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee6c27de2009-05-14 21:22:49 +0000834 }
David Greened418c1b2009-05-14 20:54:48 +0000835
836 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +0000837 case tgtok::XSRA:
David Greened418c1b2009-05-14 20:54:48 +0000838 case tgtok::XSRL:
839 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +0000840 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +0000841 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000842 tgtok::TokKind OpTok = Lex.getCode();
843 SMLoc OpLoc = Lex.getLoc();
844 Lex.Lex(); // eat the operation
845
David Greened418c1b2009-05-14 20:54:48 +0000846 BinOpInit::BinaryOp Code;
847 RecTy *Type = 0;
848
Chris Lattner8d978a72010-10-05 23:58:18 +0000849 switch (OpTok) {
David Greened418c1b2009-05-14 20:54:48 +0000850 default: assert(0 && "Unhandled code!");
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000851 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
852 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
853 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
854 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
855 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Bob Wilson21870412009-11-22 04:24:42 +0000856 case tgtok::XStrConcat:
David Greened418c1b2009-05-14 20:54:48 +0000857 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000858 Type = StringRecTy::get();
David Greened418c1b2009-05-14 20:54:48 +0000859 break;
David Greened418c1b2009-05-14 20:54:48 +0000860 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000861
David Greened418c1b2009-05-14 20:54:48 +0000862 if (Lex.getCode() != tgtok::l_paren) {
863 TokError("expected '(' after binary operator");
864 return 0;
865 }
866 Lex.Lex(); // eat the '('
867
David Greene05bce0b2011-07-29 22:43:06 +0000868 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000869
Chris Lattner8d978a72010-10-05 23:58:18 +0000870 InitList.push_back(ParseValue(CurRec));
871 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000872
Chris Lattner8d978a72010-10-05 23:58:18 +0000873 while (Lex.getCode() == tgtok::comma) {
874 Lex.Lex(); // eat the ','
875
876 InitList.push_back(ParseValue(CurRec));
877 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000878 }
David Greened418c1b2009-05-14 20:54:48 +0000879
880 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner8d978a72010-10-05 23:58:18 +0000881 TokError("expected ')' in operator");
David Greened418c1b2009-05-14 20:54:48 +0000882 return 0;
883 }
884 Lex.Lex(); // eat the ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000885
886 // We allow multiple operands to associative operators like !strconcat as
887 // shorthand for nesting them.
888 if (Code == BinOpInit::STRCONCAT) {
889 while (InitList.size() > 2) {
David Greene05bce0b2011-07-29 22:43:06 +0000890 Init *RHS = InitList.pop_back_val();
David Greenedcd35c72011-07-29 19:07:07 +0000891 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
892 ->Fold(CurRec, CurMultiClass);
Chris Lattner8d978a72010-10-05 23:58:18 +0000893 InitList.back() = RHS;
894 }
895 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000896
Chris Lattner8d978a72010-10-05 23:58:18 +0000897 if (InitList.size() == 2)
David Greenedcd35c72011-07-29 19:07:07 +0000898 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner8d978a72010-10-05 23:58:18 +0000899 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000900
Chris Lattner8d978a72010-10-05 23:58:18 +0000901 Error(OpLoc, "expected two operands to operator");
902 return 0;
David Greened418c1b2009-05-14 20:54:48 +0000903 }
904
David Greene9bea7c82009-05-14 23:26:46 +0000905 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +0000906 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +0000907 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
908 TernOpInit::TernaryOp Code;
909 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000910
David Greene4afc5092009-05-14 21:54:42 +0000911 tgtok::TokKind LexCode = Lex.getCode();
912 Lex.Lex(); // eat the operation
913 switch (LexCode) {
914 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000915 case tgtok::XIf:
916 Code = TernOpInit::IF;
917 break;
David Greenebeb31a52009-05-14 22:23:47 +0000918 case tgtok::XForEach:
919 Code = TernOpInit::FOREACH;
920 break;
David Greene4afc5092009-05-14 21:54:42 +0000921 case tgtok::XSubst:
922 Code = TernOpInit::SUBST;
923 break;
924 }
925 if (Lex.getCode() != tgtok::l_paren) {
926 TokError("expected '(' after ternary operator");
927 return 0;
928 }
929 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000930
David Greene05bce0b2011-07-29 22:43:06 +0000931 Init *LHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +0000932 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000933
David Greene4afc5092009-05-14 21:54:42 +0000934 if (Lex.getCode() != tgtok::comma) {
935 TokError("expected ',' in ternary operator");
936 return 0;
937 }
938 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000939
David Greene05bce0b2011-07-29 22:43:06 +0000940 Init *MHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +0000941 if (MHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000942
David Greene4afc5092009-05-14 21:54:42 +0000943 if (Lex.getCode() != tgtok::comma) {
944 TokError("expected ',' in ternary operator");
945 return 0;
946 }
947 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000948
David Greene05bce0b2011-07-29 22:43:06 +0000949 Init *RHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +0000950 if (RHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000951
David Greene4afc5092009-05-14 21:54:42 +0000952 if (Lex.getCode() != tgtok::r_paren) {
953 TokError("expected ')' in binary operator");
954 return 0;
955 }
956 Lex.Lex(); // eat the ')'
David Greened418c1b2009-05-14 20:54:48 +0000957
David Greene4afc5092009-05-14 21:54:42 +0000958 switch (LexCode) {
959 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000960 case tgtok::XIf: {
Bill Wendling548f5a02010-12-13 01:46:19 +0000961 // FIXME: The `!if' operator doesn't handle non-TypedInit well at
962 // all. This can be made much more robust.
David Greene05bce0b2011-07-29 22:43:06 +0000963 TypedInit *MHSt = dynamic_cast<TypedInit*>(MHS);
964 TypedInit *RHSt = dynamic_cast<TypedInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +0000965
966 RecTy *MHSTy = 0;
967 RecTy *RHSTy = 0;
968
969 if (MHSt == 0 && RHSt == 0) {
David Greene05bce0b2011-07-29 22:43:06 +0000970 BitsInit *MHSbits = dynamic_cast<BitsInit*>(MHS);
971 BitsInit *RHSbits = dynamic_cast<BitsInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +0000972
973 if (MHSbits && RHSbits &&
974 MHSbits->getNumBits() == RHSbits->getNumBits()) {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000975 Type = BitRecTy::get();
Bill Wendling548f5a02010-12-13 01:46:19 +0000976 break;
977 } else {
David Greene05bce0b2011-07-29 22:43:06 +0000978 BitInit *MHSbit = dynamic_cast<BitInit*>(MHS);
979 BitInit *RHSbit = dynamic_cast<BitInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +0000980
981 if (MHSbit && RHSbit) {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000982 Type = BitRecTy::get();
Bill Wendling548f5a02010-12-13 01:46:19 +0000983 break;
984 }
985 }
986 } else if (MHSt != 0 && RHSt != 0) {
987 MHSTy = MHSt->getType();
988 RHSTy = RHSt->getType();
989 }
990
991 if (!MHSTy || !RHSTy) {
David Greene9bea7c82009-05-14 23:26:46 +0000992 TokError("could not get type for !if");
993 return 0;
994 }
Bill Wendling548f5a02010-12-13 01:46:19 +0000995
996 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
997 Type = RHSTy;
998 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
999 Type = MHSTy;
Bob Wilson21870412009-11-22 04:24:42 +00001000 } else {
David Greene9bea7c82009-05-14 23:26:46 +00001001 TokError("inconsistent types for !if");
1002 return 0;
1003 }
1004 break;
1005 }
David Greenebeb31a52009-05-14 22:23:47 +00001006 case tgtok::XForEach: {
David Greene05bce0b2011-07-29 22:43:06 +00001007 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
David Greenebeb31a52009-05-14 22:23:47 +00001008 if (MHSt == 0) {
1009 TokError("could not get type for !foreach");
1010 return 0;
1011 }
1012 Type = MHSt->getType();
1013 break;
1014 }
David Greene4afc5092009-05-14 21:54:42 +00001015 case tgtok::XSubst: {
David Greene05bce0b2011-07-29 22:43:06 +00001016 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
David Greene4afc5092009-05-14 21:54:42 +00001017 if (RHSt == 0) {
1018 TokError("could not get type for !subst");
1019 return 0;
1020 }
1021 Type = RHSt->getType();
1022 break;
1023 }
1024 }
David Greenedcd35c72011-07-29 19:07:07 +00001025 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson21870412009-11-22 04:24:42 +00001026 CurMultiClass);
David Greene4afc5092009-05-14 21:54:42 +00001027 }
David Greened418c1b2009-05-14 20:54:48 +00001028 }
1029 TokError("could not parse operation");
1030 return 0;
1031}
1032
1033/// ParseOperatorType - Parse a type for an operator. This returns
1034/// null on error.
1035///
1036/// OperatorType ::= '<' Type '>'
1037///
Dan Gohmana9ad0412009-08-12 22:10:57 +00001038RecTy *TGParser::ParseOperatorType() {
David Greened418c1b2009-05-14 20:54:48 +00001039 RecTy *Type = 0;
1040
1041 if (Lex.getCode() != tgtok::less) {
1042 TokError("expected type name for operator");
1043 return 0;
1044 }
1045 Lex.Lex(); // eat the <
1046
1047 Type = ParseType();
1048
1049 if (Type == 0) {
1050 TokError("expected type name for operator");
1051 return 0;
1052 }
1053
1054 if (Lex.getCode() != tgtok::greater) {
1055 TokError("expected type name for operator");
1056 return 0;
1057 }
1058 Lex.Lex(); // eat the >
1059
1060 return Type;
1061}
1062
1063
Chris Lattnerf4601652007-11-22 20:49:04 +00001064/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1065///
1066/// SimpleValue ::= IDValue
1067/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001068/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +00001069/// SimpleValue ::= CODEFRAGMENT
1070/// SimpleValue ::= '?'
1071/// SimpleValue ::= '{' ValueList '}'
1072/// SimpleValue ::= ID '<' ValueListNE '>'
1073/// SimpleValue ::= '[' ValueList ']'
1074/// SimpleValue ::= '(' IDValue DagArgList ')'
1075/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
1076/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1077/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1078/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1079/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1080///
David Greenef3744a02011-10-19 13:04:20 +00001081Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1082 IDParseMode Mode) {
David Greene05bce0b2011-07-29 22:43:06 +00001083 Init *R = 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001084 switch (Lex.getCode()) {
1085 default: TokError("Unknown token when parsing a value"); break;
David Greenedcd35c72011-07-29 19:07:07 +00001086 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001087 case tgtok::StrVal: {
1088 std::string Val = Lex.getCurStrVal();
1089 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001090
Jim Grosbachda4231f2009-03-26 16:17:51 +00001091 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001092 while (Lex.getCode() == tgtok::StrVal) {
1093 Val += Lex.getCurStrVal();
1094 Lex.Lex();
1095 }
Bob Wilson21870412009-11-22 04:24:42 +00001096
David Greenedcd35c72011-07-29 19:07:07 +00001097 R = StringInit::get(Val);
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001098 break;
1099 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001100 case tgtok::CodeFragment:
David Greenedcd35c72011-07-29 19:07:07 +00001101 R = CodeInit::get(Lex.getCurStrVal());
Chris Lattner578bcf02010-10-06 04:31:40 +00001102 Lex.Lex();
1103 break;
1104 case tgtok::question:
David Greenedcd35c72011-07-29 19:07:07 +00001105 R = UnsetInit::get();
Chris Lattner578bcf02010-10-06 04:31:40 +00001106 Lex.Lex();
1107 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001108 case tgtok::Id: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001109 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001110 std::string Name = Lex.getCurStrVal();
1111 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greenef3744a02011-10-19 13:04:20 +00001112 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson21870412009-11-22 04:24:42 +00001113
Chris Lattnerf4601652007-11-22 20:49:04 +00001114 // Value ::= ID '<' ValueListNE '>'
1115 if (Lex.Lex() == tgtok::greater) {
1116 TokError("expected non-empty value list");
1117 return 0;
1118 }
David Greenee1b46912009-06-08 20:23:18 +00001119
Chris Lattnerf4601652007-11-22 20:49:04 +00001120 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1121 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1122 // body.
1123 Record *Class = Records.getClass(Name);
1124 if (!Class) {
1125 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1126 return 0;
1127 }
David Greenee1b46912009-06-08 20:23:18 +00001128
David Greene05bce0b2011-07-29 22:43:06 +00001129 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
David Greenee1b46912009-06-08 20:23:18 +00001130 if (ValueList.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001131
David Greenee1b46912009-06-08 20:23:18 +00001132 if (Lex.getCode() != tgtok::greater) {
1133 TokError("expected '>' at end of value list");
1134 return 0;
1135 }
1136 Lex.Lex(); // eat the '>'
Bob Wilson21870412009-11-22 04:24:42 +00001137
Chris Lattnerf4601652007-11-22 20:49:04 +00001138 // Create the new record, set it as CurRec temporarily.
1139 static unsigned AnonCounter = 0;
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001140 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),
1141 NameLoc,
1142 Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001143 SubClassReference SCRef;
1144 SCRef.RefLoc = NameLoc;
1145 SCRef.Rec = Class;
1146 SCRef.TemplateArgs = ValueList;
1147 // Add info about the subclass to NewRec.
1148 if (AddSubClass(NewRec, SCRef))
1149 return 0;
1150 NewRec->resolveReferences();
1151 Records.addDef(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001152
Chris Lattnerf4601652007-11-22 20:49:04 +00001153 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +00001154 return DefInit::get(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001155 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001156 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001157 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001158 Lex.Lex(); // eat the '{'
David Greene05bce0b2011-07-29 22:43:06 +00001159 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001160
Chris Lattnerf4601652007-11-22 20:49:04 +00001161 if (Lex.getCode() != tgtok::r_brace) {
1162 Vals = ParseValueList(CurRec);
1163 if (Vals.empty()) return 0;
1164 }
1165 if (Lex.getCode() != tgtok::r_brace) {
1166 TokError("expected '}' at end of bit list value");
1167 return 0;
1168 }
1169 Lex.Lex(); // eat the '}'
Bob Wilson21870412009-11-22 04:24:42 +00001170
David Greene05bce0b2011-07-29 22:43:06 +00001171 SmallVector<Init *, 16> NewBits(Vals.size());
David Greeneca7fd3d2011-07-29 19:07:00 +00001172
Chris Lattnerf4601652007-11-22 20:49:04 +00001173 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001174 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Chris Lattnerf4601652007-11-22 20:49:04 +00001175 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +00001176 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1177 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +00001178 return 0;
1179 }
David Greeneca7fd3d2011-07-29 19:07:00 +00001180 NewBits[Vals.size()-i-1] = Bit;
Chris Lattnerf4601652007-11-22 20:49:04 +00001181 }
David Greenedcd35c72011-07-29 19:07:07 +00001182 return BitsInit::get(NewBits);
Chris Lattnerf4601652007-11-22 20:49:04 +00001183 }
1184 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1185 Lex.Lex(); // eat the '['
David Greene05bce0b2011-07-29 22:43:06 +00001186 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001187
David Greenee1b46912009-06-08 20:23:18 +00001188 RecTy *DeducedEltTy = 0;
1189 ListRecTy *GivenListTy = 0;
Bob Wilson21870412009-11-22 04:24:42 +00001190
David Greenee1b46912009-06-08 20:23:18 +00001191 if (ItemType != 0) {
1192 ListRecTy *ListType = dynamic_cast<ListRecTy*>(ItemType);
1193 if (ListType == 0) {
1194 std::stringstream s;
Bob Wilson21870412009-11-22 04:24:42 +00001195 s << "Type mismatch for list, expected list type, got "
David Greenee1b46912009-06-08 20:23:18 +00001196 << ItemType->getAsString();
1197 TokError(s.str());
Jim Grosbach6a44ada2011-03-11 19:52:52 +00001198 return 0;
David Greenee1b46912009-06-08 20:23:18 +00001199 }
1200 GivenListTy = ListType;
Bob Wilson21870412009-11-22 04:24:42 +00001201 }
David Greenee1b46912009-06-08 20:23:18 +00001202
Chris Lattnerf4601652007-11-22 20:49:04 +00001203 if (Lex.getCode() != tgtok::r_square) {
Bob Wilson21870412009-11-22 04:24:42 +00001204 Vals = ParseValueList(CurRec, 0,
1205 GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001206 if (Vals.empty()) return 0;
1207 }
1208 if (Lex.getCode() != tgtok::r_square) {
1209 TokError("expected ']' at end of list value");
1210 return 0;
1211 }
1212 Lex.Lex(); // eat the ']'
David Greenee1b46912009-06-08 20:23:18 +00001213
1214 RecTy *GivenEltTy = 0;
1215 if (Lex.getCode() == tgtok::less) {
1216 // Optional list element type
1217 Lex.Lex(); // eat the '<'
1218
1219 GivenEltTy = ParseType();
1220 if (GivenEltTy == 0) {
1221 // Couldn't parse element type
1222 return 0;
1223 }
1224
1225 if (Lex.getCode() != tgtok::greater) {
1226 TokError("expected '>' at end of list element type");
1227 return 0;
1228 }
1229 Lex.Lex(); // eat the '>'
1230 }
1231
1232 // Check elements
1233 RecTy *EltTy = 0;
David Greene05bce0b2011-07-29 22:43:06 +00001234 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greenee1b46912009-06-08 20:23:18 +00001235 i != ie;
1236 ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001237 TypedInit *TArg = dynamic_cast<TypedInit*>(*i);
David Greenee1b46912009-06-08 20:23:18 +00001238 if (TArg == 0) {
1239 TokError("Untyped list element");
1240 return 0;
1241 }
1242 if (EltTy != 0) {
1243 EltTy = resolveTypes(EltTy, TArg->getType());
1244 if (EltTy == 0) {
1245 TokError("Incompatible types in list elements");
1246 return 0;
1247 }
Bob Wilson21870412009-11-22 04:24:42 +00001248 } else {
David Greenee1b46912009-06-08 20:23:18 +00001249 EltTy = TArg->getType();
1250 }
1251 }
1252
1253 if (GivenEltTy != 0) {
1254 if (EltTy != 0) {
1255 // Verify consistency
1256 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1257 TokError("Incompatible types in list elements");
1258 return 0;
1259 }
1260 }
1261 EltTy = GivenEltTy;
1262 }
1263
1264 if (EltTy == 0) {
1265 if (ItemType == 0) {
1266 TokError("No type for list");
1267 return 0;
1268 }
1269 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +00001270 } else {
David Greenee1b46912009-06-08 20:23:18 +00001271 // Make sure the deduced type is compatible with the given type
1272 if (GivenListTy) {
1273 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1274 TokError("Element type mismatch for list");
1275 return 0;
1276 }
1277 }
1278 DeducedEltTy = EltTy;
1279 }
Bob Wilson21870412009-11-22 04:24:42 +00001280
David Greenedcd35c72011-07-29 19:07:07 +00001281 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4601652007-11-22 20:49:04 +00001282 }
1283 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1284 Lex.Lex(); // eat the '('
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001285 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001286 TokError("expected identifier in dag init");
1287 return 0;
1288 }
Bob Wilson21870412009-11-22 04:24:42 +00001289
David Greene05bce0b2011-07-29 22:43:06 +00001290 Init *Operator = ParseValue(CurRec);
Chris Lattner578bcf02010-10-06 04:31:40 +00001291 if (Operator == 0) return 0;
David Greenec7cafcd2009-04-22 20:18:10 +00001292
Nate Begeman7cee8172009-03-19 05:21:56 +00001293 // If the operator name is present, parse it.
1294 std::string OperatorName;
1295 if (Lex.getCode() == tgtok::colon) {
1296 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1297 TokError("expected variable name in dag operator");
1298 return 0;
1299 }
1300 OperatorName = Lex.getCurStrVal();
1301 Lex.Lex(); // eat the VarName.
1302 }
Bob Wilson21870412009-11-22 04:24:42 +00001303
David Greene05bce0b2011-07-29 22:43:06 +00001304 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4601652007-11-22 20:49:04 +00001305 if (Lex.getCode() != tgtok::r_paren) {
1306 DagArgs = ParseDagArgList(CurRec);
1307 if (DagArgs.empty()) return 0;
1308 }
Bob Wilson21870412009-11-22 04:24:42 +00001309
Chris Lattnerf4601652007-11-22 20:49:04 +00001310 if (Lex.getCode() != tgtok::r_paren) {
1311 TokError("expected ')' in dag init");
1312 return 0;
1313 }
1314 Lex.Lex(); // eat the ')'
Bob Wilson21870412009-11-22 04:24:42 +00001315
David Greenedcd35c72011-07-29 19:07:07 +00001316 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4601652007-11-22 20:49:04 +00001317 }
Bob Wilson21870412009-11-22 04:24:42 +00001318
David Greene1434f662011-01-07 17:05:37 +00001319 case tgtok::XHead:
1320 case tgtok::XTail:
1321 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +00001322 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001323 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +00001324 case tgtok::XSRA:
Chris Lattnerf4601652007-11-22 20:49:04 +00001325 case tgtok::XSRL:
1326 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +00001327 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001328 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene9bea7c82009-05-14 23:26:46 +00001329 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +00001330 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001331 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greened418c1b2009-05-14 20:54:48 +00001332 return ParseOperation(CurRec);
Chris Lattnerf4601652007-11-22 20:49:04 +00001333 }
1334 }
Bob Wilson21870412009-11-22 04:24:42 +00001335
Chris Lattnerf4601652007-11-22 20:49:04 +00001336 return R;
1337}
1338
1339/// ParseValue - Parse a tblgen value. This returns null on error.
1340///
1341/// Value ::= SimpleValue ValueSuffix*
1342/// ValueSuffix ::= '{' BitList '}'
1343/// ValueSuffix ::= '[' BitList ']'
1344/// ValueSuffix ::= '.' ID
1345///
David Greenef3744a02011-10-19 13:04:20 +00001346Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1347 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Chris Lattnerf4601652007-11-22 20:49:04 +00001348 if (Result == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001349
Chris Lattnerf4601652007-11-22 20:49:04 +00001350 // Parse the suffixes now if present.
1351 while (1) {
1352 switch (Lex.getCode()) {
1353 default: return Result;
1354 case tgtok::l_brace: {
David Greene8592b2b2011-10-19 13:04:26 +00001355 if (Mode == ParseNameMode)
1356 // This is the beginning of the object body.
1357 return Result;
1358
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001359 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001360 Lex.Lex(); // eat the '{'
1361 std::vector<unsigned> Ranges = ParseRangeList();
1362 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001363
Chris Lattnerf4601652007-11-22 20:49:04 +00001364 // Reverse the bitlist.
1365 std::reverse(Ranges.begin(), Ranges.end());
1366 Result = Result->convertInitializerBitRange(Ranges);
1367 if (Result == 0) {
1368 Error(CurlyLoc, "Invalid bit range for value");
1369 return 0;
1370 }
Bob Wilson21870412009-11-22 04:24:42 +00001371
Chris Lattnerf4601652007-11-22 20:49:04 +00001372 // Eat the '}'.
1373 if (Lex.getCode() != tgtok::r_brace) {
1374 TokError("expected '}' at end of bit range list");
1375 return 0;
1376 }
1377 Lex.Lex();
1378 break;
1379 }
1380 case tgtok::l_square: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001381 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001382 Lex.Lex(); // eat the '['
1383 std::vector<unsigned> Ranges = ParseRangeList();
1384 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001385
Chris Lattnerf4601652007-11-22 20:49:04 +00001386 Result = Result->convertInitListSlice(Ranges);
1387 if (Result == 0) {
1388 Error(SquareLoc, "Invalid range for list slice");
1389 return 0;
1390 }
Bob Wilson21870412009-11-22 04:24:42 +00001391
Chris Lattnerf4601652007-11-22 20:49:04 +00001392 // Eat the ']'.
1393 if (Lex.getCode() != tgtok::r_square) {
1394 TokError("expected ']' at end of list slice");
1395 return 0;
1396 }
1397 Lex.Lex();
1398 break;
1399 }
1400 case tgtok::period:
1401 if (Lex.Lex() != tgtok::Id) { // eat the .
1402 TokError("expected field identifier after '.'");
1403 return 0;
1404 }
1405 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001406 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00001407 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +00001408 return 0;
1409 }
David Greenedcd35c72011-07-29 19:07:07 +00001410 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001411 Lex.Lex(); // eat field name
1412 break;
1413 }
1414 }
1415}
1416
1417/// ParseDagArgList - Parse the argument list for a dag literal expression.
1418///
1419/// ParseDagArgList ::= Value (':' VARNAME)?
1420/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
David Greene05bce0b2011-07-29 22:43:06 +00001421std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4601652007-11-22 20:49:04 +00001422TGParser::ParseDagArgList(Record *CurRec) {
David Greene05bce0b2011-07-29 22:43:06 +00001423 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson21870412009-11-22 04:24:42 +00001424
Chris Lattnerf4601652007-11-22 20:49:04 +00001425 while (1) {
David Greene05bce0b2011-07-29 22:43:06 +00001426 Init *Val = ParseValue(CurRec);
1427 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
Bob Wilson21870412009-11-22 04:24:42 +00001428
Chris Lattnerf4601652007-11-22 20:49:04 +00001429 // If the variable name is present, add it.
1430 std::string VarName;
1431 if (Lex.getCode() == tgtok::colon) {
1432 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1433 TokError("expected variable name in dag literal");
David Greene05bce0b2011-07-29 22:43:06 +00001434 return std::vector<std::pair<llvm::Init*, std::string> >();
Chris Lattnerf4601652007-11-22 20:49:04 +00001435 }
1436 VarName = Lex.getCurStrVal();
1437 Lex.Lex(); // eat the VarName.
1438 }
Bob Wilson21870412009-11-22 04:24:42 +00001439
Chris Lattnerf4601652007-11-22 20:49:04 +00001440 Result.push_back(std::make_pair(Val, VarName));
Bob Wilson21870412009-11-22 04:24:42 +00001441
Chris Lattnerf4601652007-11-22 20:49:04 +00001442 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson21870412009-11-22 04:24:42 +00001443 Lex.Lex(); // eat the ','
Chris Lattnerf4601652007-11-22 20:49:04 +00001444 }
Bob Wilson21870412009-11-22 04:24:42 +00001445
Chris Lattnerf4601652007-11-22 20:49:04 +00001446 return Result;
1447}
1448
1449
1450/// ParseValueList - Parse a comma separated list of values, returning them as a
1451/// vector. Note that this always expects to be able to parse at least one
1452/// value. It returns an empty list if this is not possible.
1453///
1454/// ValueList ::= Value (',' Value)
1455///
David Greene05bce0b2011-07-29 22:43:06 +00001456std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopherd568b3f2011-07-11 23:06:52 +00001457 RecTy *EltTy) {
David Greene05bce0b2011-07-29 22:43:06 +00001458 std::vector<Init*> Result;
David Greenee1b46912009-06-08 20:23:18 +00001459 RecTy *ItemType = EltTy;
David Greene67acdf22009-06-29 19:59:52 +00001460 unsigned int ArgN = 0;
David Greenee1b46912009-06-08 20:23:18 +00001461 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001462 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greenee1b46912009-06-08 20:23:18 +00001463 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greened9746fe2011-09-19 18:26:07 +00001464 if (!RV) {
1465 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1466 << ")\n";
1467 }
David Greenee1b46912009-06-08 20:23:18 +00001468 assert(RV && "Template argument record not found??");
1469 ItemType = RV->getType();
1470 ++ArgN;
1471 }
1472 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001473 if (Result.back() == 0) return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001474
Chris Lattnerf4601652007-11-22 20:49:04 +00001475 while (Lex.getCode() == tgtok::comma) {
1476 Lex.Lex(); // Eat the comma
Bob Wilson21870412009-11-22 04:24:42 +00001477
David Greenee1b46912009-06-08 20:23:18 +00001478 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001479 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greene67acdf22009-06-29 19:59:52 +00001480 if (ArgN >= TArgs.size()) {
1481 TokError("too many template arguments");
David Greene05bce0b2011-07-29 22:43:06 +00001482 return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001483 }
David Greenee1b46912009-06-08 20:23:18 +00001484 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1485 assert(RV && "Template argument record not found??");
1486 ItemType = RV->getType();
1487 ++ArgN;
1488 }
1489 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001490 if (Result.back() == 0) return std::vector<Init*>();
Chris Lattnerf4601652007-11-22 20:49:04 +00001491 }
Bob Wilson21870412009-11-22 04:24:42 +00001492
Chris Lattnerf4601652007-11-22 20:49:04 +00001493 return Result;
1494}
1495
1496
Chris Lattnerf4601652007-11-22 20:49:04 +00001497/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1498/// empty string on error. This can happen in a number of different context's,
1499/// including within a def or in the template args for a def (which which case
1500/// CurRec will be non-null) and within the template args for a multiclass (in
1501/// which case CurRec will be null, but CurMultiClass will be set). This can
1502/// also happen within a def that is within a multiclass, which will set both
1503/// CurRec and CurMultiClass.
1504///
1505/// Declaration ::= FIELD? Type ID ('=' Value)?
1506///
David Greenee22b3212011-10-19 13:02:42 +00001507Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4601652007-11-22 20:49:04 +00001508 bool ParsingTemplateArgs) {
1509 // Read the field prefix if present.
1510 bool HasField = Lex.getCode() == tgtok::Field;
1511 if (HasField) Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001512
Chris Lattnerf4601652007-11-22 20:49:04 +00001513 RecTy *Type = ParseType();
David Greenee22b3212011-10-19 13:02:42 +00001514 if (Type == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001515
Chris Lattnerf4601652007-11-22 20:49:04 +00001516 if (Lex.getCode() != tgtok::Id) {
1517 TokError("Expected identifier in declaration");
David Greenee22b3212011-10-19 13:02:42 +00001518 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001519 }
Bob Wilson21870412009-11-22 04:24:42 +00001520
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001521 SMLoc IdLoc = Lex.getLoc();
David Greenee22b3212011-10-19 13:02:42 +00001522 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001523 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001524
Chris Lattnerf4601652007-11-22 20:49:04 +00001525 if (ParsingTemplateArgs) {
1526 if (CurRec) {
David Greenee22b3212011-10-19 13:02:42 +00001527 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4601652007-11-22 20:49:04 +00001528 } else {
1529 assert(CurMultiClass);
1530 }
1531 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +00001532 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1533 "::");
Chris Lattnerf4601652007-11-22 20:49:04 +00001534 }
Bob Wilson21870412009-11-22 04:24:42 +00001535
Chris Lattnerf4601652007-11-22 20:49:04 +00001536 // Add the value.
1537 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
David Greenee22b3212011-10-19 13:02:42 +00001538 return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001539
Chris Lattnerf4601652007-11-22 20:49:04 +00001540 // If a value is present, parse it.
1541 if (Lex.getCode() == tgtok::equal) {
1542 Lex.Lex();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001543 SMLoc ValLoc = Lex.getLoc();
David Greene05bce0b2011-07-29 22:43:06 +00001544 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001545 if (Val == 0 ||
1546 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
David Greenee22b3212011-10-19 13:02:42 +00001547 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001548 }
Bob Wilson21870412009-11-22 04:24:42 +00001549
Chris Lattnerf4601652007-11-22 20:49:04 +00001550 return DeclName;
1551}
1552
1553/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1554/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1555/// template args for a def, which may or may not be in a multiclass. If null,
1556/// these are the template args for a multiclass.
1557///
1558/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson21870412009-11-22 04:24:42 +00001559///
Chris Lattnerf4601652007-11-22 20:49:04 +00001560bool TGParser::ParseTemplateArgList(Record *CurRec) {
1561 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1562 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +00001563
Chris Lattnerf4601652007-11-22 20:49:04 +00001564 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +00001565
Chris Lattnerf4601652007-11-22 20:49:04 +00001566 // Read the first declaration.
David Greenee22b3212011-10-19 13:02:42 +00001567 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1568 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001569 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001570
Chris Lattnerf4601652007-11-22 20:49:04 +00001571 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson21870412009-11-22 04:24:42 +00001572
Chris Lattnerf4601652007-11-22 20:49:04 +00001573 while (Lex.getCode() == tgtok::comma) {
1574 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001575
Chris Lattnerf4601652007-11-22 20:49:04 +00001576 // Read the following declarations.
1577 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
David Greenee22b3212011-10-19 13:02:42 +00001578 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001579 return true;
1580 TheRecToAddTo->addTemplateArg(TemplArg);
1581 }
Bob Wilson21870412009-11-22 04:24:42 +00001582
Chris Lattnerf4601652007-11-22 20:49:04 +00001583 if (Lex.getCode() != tgtok::greater)
1584 return TokError("expected '>' at end of template argument list");
1585 Lex.Lex(); // eat the '>'.
1586 return false;
1587}
1588
1589
1590/// ParseBodyItem - Parse a single item at within the body of a def or class.
1591///
1592/// BodyItem ::= Declaration ';'
1593/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1594bool TGParser::ParseBodyItem(Record *CurRec) {
1595 if (Lex.getCode() != tgtok::Let) {
David Greenee22b3212011-10-19 13:02:42 +00001596 if (ParseDeclaration(CurRec, false) == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001597 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001598
Chris Lattnerf4601652007-11-22 20:49:04 +00001599 if (Lex.getCode() != tgtok::semi)
1600 return TokError("expected ';' after declaration");
1601 Lex.Lex();
1602 return false;
1603 }
1604
1605 // LET ID OptionalRangeList '=' Value ';'
1606 if (Lex.Lex() != tgtok::Id)
1607 return TokError("expected field identifier after let");
Bob Wilson21870412009-11-22 04:24:42 +00001608
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001609 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001610 std::string FieldName = Lex.getCurStrVal();
1611 Lex.Lex(); // eat the field name.
Bob Wilson21870412009-11-22 04:24:42 +00001612
Chris Lattnerf4601652007-11-22 20:49:04 +00001613 std::vector<unsigned> BitList;
Bob Wilson21870412009-11-22 04:24:42 +00001614 if (ParseOptionalBitList(BitList))
Chris Lattnerf4601652007-11-22 20:49:04 +00001615 return true;
1616 std::reverse(BitList.begin(), BitList.end());
Bob Wilson21870412009-11-22 04:24:42 +00001617
Chris Lattnerf4601652007-11-22 20:49:04 +00001618 if (Lex.getCode() != tgtok::equal)
1619 return TokError("expected '=' in let expression");
1620 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001621
David Greenee1b46912009-06-08 20:23:18 +00001622 RecordVal *Field = CurRec->getValue(FieldName);
1623 if (Field == 0)
1624 return TokError("Value '" + FieldName + "' unknown!");
1625
1626 RecTy *Type = Field->getType();
Bob Wilson21870412009-11-22 04:24:42 +00001627
David Greene05bce0b2011-07-29 22:43:06 +00001628 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001629 if (Val == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001630
Chris Lattnerf4601652007-11-22 20:49:04 +00001631 if (Lex.getCode() != tgtok::semi)
1632 return TokError("expected ';' after let expression");
1633 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001634
Chris Lattnerf4601652007-11-22 20:49:04 +00001635 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1636}
1637
1638/// ParseBody - Read the body of a class or def. Return true on error, false on
1639/// success.
1640///
1641/// Body ::= ';'
1642/// Body ::= '{' BodyList '}'
1643/// BodyList BodyItem*
1644///
1645bool TGParser::ParseBody(Record *CurRec) {
1646 // If this is a null definition, just eat the semi and return.
1647 if (Lex.getCode() == tgtok::semi) {
1648 Lex.Lex();
1649 return false;
1650 }
Bob Wilson21870412009-11-22 04:24:42 +00001651
Chris Lattnerf4601652007-11-22 20:49:04 +00001652 if (Lex.getCode() != tgtok::l_brace)
1653 return TokError("Expected ';' or '{' to start body");
1654 // Eat the '{'.
1655 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001656
Chris Lattnerf4601652007-11-22 20:49:04 +00001657 while (Lex.getCode() != tgtok::r_brace)
1658 if (ParseBodyItem(CurRec))
1659 return true;
1660
1661 // Eat the '}'.
1662 Lex.Lex();
1663 return false;
1664}
1665
1666/// ParseObjectBody - Parse the body of a def or class. This consists of an
1667/// optional ClassList followed by a Body. CurRec is the current def or class
1668/// that is being parsed.
1669///
1670/// ObjectBody ::= BaseClassList Body
1671/// BaseClassList ::= /*empty*/
1672/// BaseClassList ::= ':' BaseClassListNE
1673/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1674///
1675bool TGParser::ParseObjectBody(Record *CurRec) {
1676 // If there is a baseclass list, read it.
1677 if (Lex.getCode() == tgtok::colon) {
1678 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001679
Chris Lattnerf4601652007-11-22 20:49:04 +00001680 // Read all of the subclasses.
1681 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1682 while (1) {
1683 // Check for error.
1684 if (SubClass.Rec == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001685
Chris Lattnerf4601652007-11-22 20:49:04 +00001686 // Add it.
1687 if (AddSubClass(CurRec, SubClass))
1688 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001689
Chris Lattnerf4601652007-11-22 20:49:04 +00001690 if (Lex.getCode() != tgtok::comma) break;
1691 Lex.Lex(); // eat ','.
1692 SubClass = ParseSubClassReference(CurRec, false);
1693 }
1694 }
1695
1696 // Process any variables on the let stack.
1697 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1698 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1699 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1700 LetStack[i][j].Bits, LetStack[i][j].Value))
1701 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001702
Chris Lattnerf4601652007-11-22 20:49:04 +00001703 return ParseBody(CurRec);
1704}
1705
Chris Lattnerf4601652007-11-22 20:49:04 +00001706/// ParseDef - Parse and return a top level or multiclass def, return the record
1707/// corresponding to it. This returns null on error.
1708///
1709/// DefInst ::= DEF ObjectName ObjectBody
1710///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001711bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001712 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001713 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson21870412009-11-22 04:24:42 +00001714 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4601652007-11-22 20:49:04 +00001715
1716 // Parse ObjectName and make a record for it.
David Greenea9e07dd2011-10-19 13:04:29 +00001717 Record *CurRec = new Record(ParseObjectName(CurMultiClass), DefLoc, Records);
Bob Wilson21870412009-11-22 04:24:42 +00001718
Chris Lattnerf4601652007-11-22 20:49:04 +00001719 if (!CurMultiClass) {
1720 // Top-level def definition.
Bob Wilson21870412009-11-22 04:24:42 +00001721
Chris Lattnerf4601652007-11-22 20:49:04 +00001722 // Ensure redefinition doesn't happen.
1723 if (Records.getDef(CurRec->getName())) {
David Greene2c49fbb2011-10-19 13:03:45 +00001724 Error(DefLoc, "def '" + CurRec->getNameInitAsString()
1725 + "' already defined");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001726 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001727 }
1728 Records.addDef(CurRec);
1729 } else {
1730 // Otherwise, a def inside a multiclass, add it to the multiclass.
1731 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene91919cd2011-10-19 13:03:51 +00001732 if (CurMultiClass->DefPrototypes[i]->getNameInit()
1733 == CurRec->getNameInit()) {
1734 Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
Chris Lattnerf4601652007-11-22 20:49:04 +00001735 "' already defined in this multiclass!");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001736 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001737 }
1738 CurMultiClass->DefPrototypes.push_back(CurRec);
1739 }
Bob Wilson21870412009-11-22 04:24:42 +00001740
Chris Lattnerf4601652007-11-22 20:49:04 +00001741 if (ParseObjectBody(CurRec))
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001742 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001743
Chris Lattnerf4601652007-11-22 20:49:04 +00001744 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
David Greene0d886402011-08-10 18:27:46 +00001745 // See Record::setName(). This resolve step will see any new name
1746 // for the def that might have been created when resolving
1747 // inheritance, values and arguments above.
Chris Lattnerf4601652007-11-22 20:49:04 +00001748 CurRec->resolveReferences();
Bob Wilson21870412009-11-22 04:24:42 +00001749
Chris Lattnerf4601652007-11-22 20:49:04 +00001750 // If ObjectBody has template arguments, it's an error.
1751 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001752
1753 if (CurMultiClass) {
1754 // Copy the template arguments for the multiclass into the def.
David Greenee22b3212011-10-19 13:02:42 +00001755 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001756 CurMultiClass->Rec.getTemplateArgs();
1757
1758 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1759 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1760 assert(RV && "Template arg doesn't exist?");
1761 CurRec->addValue(*RV);
1762 }
1763 }
1764
1765 return false;
Chris Lattnerf4601652007-11-22 20:49:04 +00001766}
1767
Chris Lattnerf4601652007-11-22 20:49:04 +00001768/// ParseClass - Parse a tblgen class definition.
1769///
1770/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
1771///
1772bool TGParser::ParseClass() {
1773 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
1774 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001775
Chris Lattnerf4601652007-11-22 20:49:04 +00001776 if (Lex.getCode() != tgtok::Id)
1777 return TokError("expected class name after 'class' keyword");
Bob Wilson21870412009-11-22 04:24:42 +00001778
Chris Lattnerf4601652007-11-22 20:49:04 +00001779 Record *CurRec = Records.getClass(Lex.getCurStrVal());
1780 if (CurRec) {
1781 // If the body was previously defined, this is an error.
David Greenee3385652011-10-19 13:04:13 +00001782 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4601652007-11-22 20:49:04 +00001783 !CurRec->getSuperClasses().empty() ||
1784 !CurRec->getTemplateArgs().empty())
David Greene69a23942011-10-19 13:03:58 +00001785 return TokError("Class '" + CurRec->getNameInitAsString()
1786 + "' already defined");
Chris Lattnerf4601652007-11-22 20:49:04 +00001787 } else {
1788 // If this is the first reference to this class, create and add it.
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001789 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001790 Records.addClass(CurRec);
1791 }
1792 Lex.Lex(); // eat the name.
Bob Wilson21870412009-11-22 04:24:42 +00001793
Chris Lattnerf4601652007-11-22 20:49:04 +00001794 // If there are template args, parse them.
1795 if (Lex.getCode() == tgtok::less)
1796 if (ParseTemplateArgList(CurRec))
1797 return true;
1798
1799 // Finally, parse the object body.
1800 return ParseObjectBody(CurRec);
1801}
1802
1803/// ParseLetList - Parse a non-empty list of assignment expressions into a list
1804/// of LetRecords.
1805///
1806/// LetList ::= LetItem (',' LetItem)*
1807/// LetItem ::= ID OptionalRangeList '=' Value
1808///
1809std::vector<LetRecord> TGParser::ParseLetList() {
1810 std::vector<LetRecord> Result;
Bob Wilson21870412009-11-22 04:24:42 +00001811
Chris Lattnerf4601652007-11-22 20:49:04 +00001812 while (1) {
1813 if (Lex.getCode() != tgtok::Id) {
1814 TokError("expected identifier in let definition");
1815 return std::vector<LetRecord>();
1816 }
1817 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001818 SMLoc NameLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +00001819 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4601652007-11-22 20:49:04 +00001820
1821 // Check for an optional RangeList.
1822 std::vector<unsigned> Bits;
Bob Wilson21870412009-11-22 04:24:42 +00001823 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4601652007-11-22 20:49:04 +00001824 return std::vector<LetRecord>();
1825 std::reverse(Bits.begin(), Bits.end());
Bob Wilson21870412009-11-22 04:24:42 +00001826
Chris Lattnerf4601652007-11-22 20:49:04 +00001827 if (Lex.getCode() != tgtok::equal) {
1828 TokError("expected '=' in let expression");
1829 return std::vector<LetRecord>();
1830 }
1831 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001832
David Greene05bce0b2011-07-29 22:43:06 +00001833 Init *Val = ParseValue(0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001834 if (Val == 0) return std::vector<LetRecord>();
Bob Wilson21870412009-11-22 04:24:42 +00001835
Chris Lattnerf4601652007-11-22 20:49:04 +00001836 // Now that we have everything, add the record.
1837 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson21870412009-11-22 04:24:42 +00001838
Chris Lattnerf4601652007-11-22 20:49:04 +00001839 if (Lex.getCode() != tgtok::comma)
1840 return Result;
Bob Wilson21870412009-11-22 04:24:42 +00001841 Lex.Lex(); // eat the comma.
Chris Lattnerf4601652007-11-22 20:49:04 +00001842 }
1843}
1844
1845/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001846/// different related productions. This works inside multiclasses too.
Chris Lattnerf4601652007-11-22 20:49:04 +00001847///
1848/// Object ::= LET LetList IN '{' ObjectList '}'
1849/// Object ::= LET LetList IN Object
1850///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001851bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001852 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
1853 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001854
Chris Lattnerf4601652007-11-22 20:49:04 +00001855 // Add this entry to the let stack.
1856 std::vector<LetRecord> LetInfo = ParseLetList();
1857 if (LetInfo.empty()) return true;
1858 LetStack.push_back(LetInfo);
1859
1860 if (Lex.getCode() != tgtok::In)
1861 return TokError("expected 'in' at end of top-level 'let'");
1862 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001863
Chris Lattnerf4601652007-11-22 20:49:04 +00001864 // If this is a scalar let, just handle it now
1865 if (Lex.getCode() != tgtok::l_brace) {
1866 // LET LetList IN Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001867 if (ParseObject(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00001868 return true;
1869 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001870 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001871 // Otherwise, this is a group let.
1872 Lex.Lex(); // eat the '{'.
Bob Wilson21870412009-11-22 04:24:42 +00001873
Chris Lattnerf4601652007-11-22 20:49:04 +00001874 // Parse the object list.
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001875 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00001876 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001877
Chris Lattnerf4601652007-11-22 20:49:04 +00001878 if (Lex.getCode() != tgtok::r_brace) {
1879 TokError("expected '}' at end of top level let command");
1880 return Error(BraceLoc, "to match this '{'");
1881 }
1882 Lex.Lex();
1883 }
Bob Wilson21870412009-11-22 04:24:42 +00001884
Chris Lattnerf4601652007-11-22 20:49:04 +00001885 // Outside this let scope, this let block is not active.
1886 LetStack.pop_back();
1887 return false;
1888}
1889
Chris Lattnerf4601652007-11-22 20:49:04 +00001890/// ParseMultiClass - Parse a multiclass definition.
1891///
Bob Wilson32558652009-04-28 19:41:44 +00001892/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
1893/// ':' BaseMultiClassList '{' MultiClassDef+ '}'
Chris Lattnerf4601652007-11-22 20:49:04 +00001894///
1895bool TGParser::ParseMultiClass() {
1896 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
1897 Lex.Lex(); // Eat the multiclass token.
1898
1899 if (Lex.getCode() != tgtok::Id)
1900 return TokError("expected identifier after multiclass for name");
1901 std::string Name = Lex.getCurStrVal();
Bob Wilson21870412009-11-22 04:24:42 +00001902
Chris Lattnerf4601652007-11-22 20:49:04 +00001903 if (MultiClasses.count(Name))
1904 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson21870412009-11-22 04:24:42 +00001905
Chris Lattner67db8832010-12-13 00:23:57 +00001906 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
1907 Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001908 Lex.Lex(); // Eat the identifier.
Bob Wilson21870412009-11-22 04:24:42 +00001909
Chris Lattnerf4601652007-11-22 20:49:04 +00001910 // If there are template args, parse them.
1911 if (Lex.getCode() == tgtok::less)
1912 if (ParseTemplateArgList(0))
1913 return true;
1914
David Greened34a73b2009-04-24 16:55:41 +00001915 bool inherits = false;
1916
David Greenede444af2009-04-22 16:42:54 +00001917 // If there are submulticlasses, parse them.
1918 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00001919 inherits = true;
1920
David Greenede444af2009-04-22 16:42:54 +00001921 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00001922
David Greenede444af2009-04-22 16:42:54 +00001923 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00001924 SubMultiClassReference SubMultiClass =
1925 ParseSubMultiClassReference(CurMultiClass);
David Greenede444af2009-04-22 16:42:54 +00001926 while (1) {
1927 // Check for error.
1928 if (SubMultiClass.MC == 0) return true;
Bob Wilson32558652009-04-28 19:41:44 +00001929
David Greenede444af2009-04-22 16:42:54 +00001930 // Add it.
1931 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
1932 return true;
Bob Wilson32558652009-04-28 19:41:44 +00001933
David Greenede444af2009-04-22 16:42:54 +00001934 if (Lex.getCode() != tgtok::comma) break;
1935 Lex.Lex(); // eat ','.
1936 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
1937 }
1938 }
1939
David Greened34a73b2009-04-24 16:55:41 +00001940 if (Lex.getCode() != tgtok::l_brace) {
1941 if (!inherits)
1942 return TokError("expected '{' in multiclass definition");
Bob Wilson21870412009-11-22 04:24:42 +00001943 else if (Lex.getCode() != tgtok::semi)
1944 return TokError("expected ';' in multiclass definition");
David Greened34a73b2009-04-24 16:55:41 +00001945 else
Bob Wilson21870412009-11-22 04:24:42 +00001946 Lex.Lex(); // eat the ';'.
1947 } else {
David Greened34a73b2009-04-24 16:55:41 +00001948 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
1949 return TokError("multiclass must contain at least one def");
Bob Wilson21870412009-11-22 04:24:42 +00001950
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001951 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001952 switch (Lex.getCode()) {
1953 default:
David Greenea1b1b792011-10-07 18:25:05 +00001954 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001955 case tgtok::Let:
1956 case tgtok::Def:
1957 case tgtok::Defm:
1958 if (ParseObject(CurMultiClass))
1959 return true;
1960 break;
1961 }
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001962 }
David Greened34a73b2009-04-24 16:55:41 +00001963 Lex.Lex(); // eat the '}'.
1964 }
Bob Wilson21870412009-11-22 04:24:42 +00001965
Chris Lattnerf4601652007-11-22 20:49:04 +00001966 CurMultiClass = 0;
1967 return false;
1968}
1969
David Greenee499a2d2011-10-05 22:42:07 +00001970Record *TGParser::
1971InstantiateMulticlassDef(MultiClass &MC,
1972 Record *DefProto,
David Greene7be867e2011-10-19 13:04:31 +00001973 Init *DefmPrefix,
David Greenee499a2d2011-10-05 22:42:07 +00001974 SMLoc DefmPrefixLoc) {
David Greene7be867e2011-10-19 13:04:31 +00001975 // We need to preserve DefProto so it can be reused for later
1976 // instantiations, so create a new Record to inherit from it.
1977
David Greenee499a2d2011-10-05 22:42:07 +00001978 // Add in the defm name. If the defm prefix is empty, give each
1979 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
1980 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
1981 // as a prefix.
David Greene7be867e2011-10-19 13:04:31 +00001982 StringInit *DefNameString =
1983 dynamic_cast<StringInit *>(DefProto->getNameInit());
1984
1985 if (DefNameString == 0) {
1986 Error(DefmPrefixLoc, "Def name is not a string");
1987 return 0;
David Greenee499a2d2011-10-05 22:42:07 +00001988 }
1989
David Greene7be867e2011-10-19 13:04:31 +00001990 if (DefmPrefix == 0)
1991 DefmPrefix = StringInit::get(GetNewAnonymousName());
1992
1993 Init *DefName = DefProto->getNameInit();
1994
1995 // See if we can substitute #NAME#.
1996 Init *NewDefName =
1997 TernOpInit::get(TernOpInit::SUBST,
1998 StringInit::get("#NAME#"),
1999 DefmPrefix,
2000 DefName,
2001 StringRecTy::get())->Fold(DefProto, &MC);
2002
2003 if (NewDefName == DefName) {
2004 // We did't do any substitution. We should concatenate the given
2005 // prefix and name.
2006 if (DefmPrefix == 0)
2007 DefmPrefix = StringInit::get(GetNewAnonymousName());
2008
2009 DefName =
2010 BinOpInit::get(BinOpInit::STRCONCAT,
2011 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2012 StringRecTy::get())->Fold(DefProto, &MC),
2013 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2014 }
2015 else
2016 DefName = NewDefName;
2017
David Greenee499a2d2011-10-05 22:42:07 +00002018 Record *CurRec = new Record(DefName, DefmPrefixLoc, Records);
2019
2020 SubClassReference Ref;
2021 Ref.RefLoc = DefmPrefixLoc;
2022 Ref.Rec = DefProto;
2023 AddSubClass(CurRec, Ref);
2024
David Greenee5b252f2011-10-19 13:04:35 +00002025 if (DefNameString == 0) {
2026 // We must resolve references to NAME.
2027 if (SetValue(CurRec, Ref.RefLoc, "NAME", std::vector<unsigned>(),
2028 DefmPrefix)) {
2029 Error(DefmPrefixLoc, "Could not resolve "
2030 + CurRec->getNameInitAsString() + ":NAME to '"
2031 + DefmPrefix->getAsUnquotedString() + "'");
2032 return 0;
2033 }
2034
2035 RecordVal *DefNameRV = CurRec->getValue("NAME");
2036 CurRec->resolveReferencesTo(DefNameRV);
2037 }
2038
2039 if (!CurMultiClass) {
2040 // We do this after resolving NAME because before resolution, many
2041 // multiclass defs will have the same name expression. If we are
2042 // currently in a multiclass, it means this defm appears inside a
2043 // multiclass and its name won't be fully resolvable until we see
2044 // the top-level defm. Therefore, we don't add this to the
2045 // RecordKeeper at this point. If we did we could get duplicate
2046 // defs as more than one probably refers to NAME or some other
2047 // common internal placeholder.
2048
2049 // Ensure redefinition doesn't happen.
2050 if (Records.getDef(CurRec->getNameInitAsString())) {
2051 Error(DefmPrefixLoc, "def '" + CurRec->getNameInitAsString() +
2052 "' already defined, instantiating defm with subdef '" +
2053 DefProto->getNameInitAsString() + "'");
2054 return 0;
2055 }
2056
2057 Records.addDef(CurRec);
2058 }
2059
David Greenee499a2d2011-10-05 22:42:07 +00002060 return CurRec;
2061}
2062
2063bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2064 Record *CurRec,
2065 SMLoc DefmPrefixLoc,
2066 SMLoc SubClassLoc,
David Greenee22b3212011-10-19 13:02:42 +00002067 const std::vector<Init *> &TArgs,
David Greenee499a2d2011-10-05 22:42:07 +00002068 std::vector<Init *> &TemplateVals,
2069 bool DeleteArgs) {
2070 // Loop over all of the template arguments, setting them to the specified
2071 // value or leaving them as the default if necessary.
2072 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2073 // Check if a value is specified for this temp-arg.
2074 if (i < TemplateVals.size()) {
2075 // Set it now.
2076 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2077 TemplateVals[i]))
2078 return true;
2079
2080 // Resolve it next.
2081 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2082
2083 if (DeleteArgs)
2084 // Now remove it.
2085 CurRec->removeValue(TArgs[i]);
2086
2087 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2088 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenee22b3212011-10-19 13:02:42 +00002089 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2090 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2091 + "'");
David Greenee499a2d2011-10-05 22:42:07 +00002092 }
2093 }
2094 return false;
2095}
2096
2097bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2098 Record *CurRec,
2099 Record *DefProto,
2100 SMLoc DefmPrefixLoc) {
2101 // If the mdef is inside a 'let' expression, add to each def.
2102 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2103 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2104 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2105 LetStack[i][j].Bits, LetStack[i][j].Value))
2106 return Error(DefmPrefixLoc, "when instantiating this defm");
2107
David Greenee499a2d2011-10-05 22:42:07 +00002108 // Don't create a top level definition for defm inside multiclasses,
2109 // instead, only update the prototypes and bind the template args
2110 // with the new created definition.
2111 if (CurMultiClass) {
2112 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2113 i != e; ++i)
David Greene22dde7e2011-10-19 13:04:02 +00002114 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2115 == CurRec->getNameInit())
2116 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
David Greenee499a2d2011-10-05 22:42:07 +00002117 "' already defined in this multiclass!");
2118 CurMultiClass->DefPrototypes.push_back(CurRec);
2119
2120 // Copy the template arguments for the multiclass into the new def.
David Greenee22b3212011-10-19 13:02:42 +00002121 const std::vector<Init *> &TA =
David Greenee499a2d2011-10-05 22:42:07 +00002122 CurMultiClass->Rec.getTemplateArgs();
2123
2124 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2125 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2126 assert(RV && "Template arg doesn't exist?");
2127 CurRec->addValue(*RV);
2128 }
David Greenee499a2d2011-10-05 22:42:07 +00002129 }
2130
2131 return false;
2132}
2133
Chris Lattnerf4601652007-11-22 20:49:04 +00002134/// ParseDefm - Parse the instantiation of a multiclass.
2135///
2136/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2137///
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002138bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002139 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Bob Wilson21870412009-11-22 04:24:42 +00002140
David Greenea9e07dd2011-10-19 13:04:29 +00002141 Init *DefmPrefix = 0;
David Greenea9e07dd2011-10-19 13:04:29 +00002142
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002143 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greenea9e07dd2011-10-19 13:04:29 +00002144 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002145 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +00002146
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002147 SMLoc DefmPrefixLoc = Lex.getLoc();
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002148 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4601652007-11-22 20:49:04 +00002149 return TokError("expected ':' after defm identifier");
Bob Wilson21870412009-11-22 04:24:42 +00002150
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002151 // Keep track of the new generated record definitions.
2152 std::vector<Record*> NewRecDefs;
2153
2154 // This record also inherits from a regular class (non-multiclass)?
2155 bool InheritFromClass = false;
2156
Chris Lattnerf4601652007-11-22 20:49:04 +00002157 // eat the colon.
2158 Lex.Lex();
2159
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002160 SMLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002161 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene56546132009-04-22 22:17:51 +00002162
2163 while (1) {
2164 if (Ref.Rec == 0) return true;
2165
2166 // To instantiate a multiclass, we need to first get the multiclass, then
2167 // instantiate each def contained in the multiclass with the SubClassRef
2168 // template parameters.
2169 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2170 assert(MC && "Didn't lookup multiclass correctly?");
David Greene05bce0b2011-07-29 22:43:06 +00002171 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greene56546132009-04-22 22:17:51 +00002172
2173 // Verify that the correct number of template arguments were specified.
David Greenee22b3212011-10-19 13:02:42 +00002174 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greene56546132009-04-22 22:17:51 +00002175 if (TArgs.size() < TemplateVals.size())
2176 return Error(SubClassLoc,
2177 "more template args specified than multiclass expects");
2178
2179 // Loop over all the def's in the multiclass, instantiating each one.
2180 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2181 Record *DefProto = MC->DefPrototypes[i];
2182
David Greene7be867e2011-10-19 13:04:31 +00002183 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix, DefmPrefixLoc);
David Greene065f2592009-05-05 16:28:25 +00002184
David Greenee499a2d2011-10-05 22:42:07 +00002185 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmPrefixLoc, SubClassLoc,
2186 TArgs, TemplateVals, true/*Delete args*/))
2187 return Error(SubClassLoc, "could not instantiate def");
David Greene56546132009-04-22 22:17:51 +00002188
David Greenee499a2d2011-10-05 22:42:07 +00002189 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmPrefixLoc))
2190 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002191
2192 NewRecDefs.push_back(CurRec);
David Greene56546132009-04-22 22:17:51 +00002193 }
2194
David Greenee499a2d2011-10-05 22:42:07 +00002195
David Greene56546132009-04-22 22:17:51 +00002196 if (Lex.getCode() != tgtok::comma) break;
2197 Lex.Lex(); // eat ','.
2198
2199 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002200
2201 // A defm can inherit from regular classes (non-multiclass) as
2202 // long as they come in the end of the inheritance list.
2203 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0);
2204
2205 if (InheritFromClass)
2206 break;
2207
David Greene56546132009-04-22 22:17:51 +00002208 Ref = ParseSubClassReference(0, true);
2209 }
2210
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002211 if (InheritFromClass) {
2212 // Process all the classes to inherit as if they were part of a
2213 // regular 'def' and inherit all record values.
2214 SubClassReference SubClass = ParseSubClassReference(0, false);
2215 while (1) {
2216 // Check for error.
2217 if (SubClass.Rec == 0) return true;
2218
2219 // Get the expanded definition prototypes and teach them about
2220 // the record values the current class to inherit has
2221 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2222 Record *CurRec = NewRecDefs[i];
2223
2224 // Add it.
2225 if (AddSubClass(CurRec, SubClass))
2226 return true;
2227
2228 // Process any variables on the let stack.
2229 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2230 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2231 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2232 LetStack[i][j].Bits, LetStack[i][j].Value))
2233 return true;
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002234 }
2235
2236 if (Lex.getCode() != tgtok::comma) break;
2237 Lex.Lex(); // eat ','.
2238 SubClass = ParseSubClassReference(0, false);
2239 }
2240 }
2241
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002242 if (!CurMultiClass)
2243 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene0d886402011-08-10 18:27:46 +00002244 // See Record::setName(). This resolve step will see any new
2245 // name for the def that might have been created when resolving
2246 // inheritance, values and arguments above.
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002247 NewRecDefs[i]->resolveReferences();
2248
Chris Lattnerf4601652007-11-22 20:49:04 +00002249 if (Lex.getCode() != tgtok::semi)
2250 return TokError("expected ';' at end of defm");
2251 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002252
Chris Lattnerf4601652007-11-22 20:49:04 +00002253 return false;
2254}
2255
2256/// ParseObject
2257/// Object ::= ClassInst
2258/// Object ::= DefInst
2259/// Object ::= MultiClassInst
2260/// Object ::= DefMInst
2261/// Object ::= LETCommand '{' ObjectList '}'
2262/// Object ::= LETCommand Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002263bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002264 switch (Lex.getCode()) {
Chris Lattnerd6d9dd92010-10-31 19:27:15 +00002265 default:
2266 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002267 case tgtok::Let: return ParseTopLevelLet(MC);
2268 case tgtok::Def: return ParseDef(MC);
2269 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4601652007-11-22 20:49:04 +00002270 case tgtok::Class: return ParseClass();
2271 case tgtok::MultiClass: return ParseMultiClass();
2272 }
2273}
2274
2275/// ParseObjectList
2276/// ObjectList :== Object*
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002277bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002278 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002279 if (ParseObject(MC))
Chris Lattnerf4601652007-11-22 20:49:04 +00002280 return true;
2281 }
2282 return false;
2283}
2284
Chris Lattnerf4601652007-11-22 20:49:04 +00002285bool TGParser::ParseFile() {
2286 Lex.Lex(); // Prime the lexer.
2287 if (ParseObjectList()) return true;
Bob Wilson21870412009-11-22 04:24:42 +00002288
Chris Lattnerf4601652007-11-22 20:49:04 +00002289 // If we have unread input at the end of the file, report it.
2290 if (Lex.getCode() == tgtok::Eof)
2291 return false;
Bob Wilson21870412009-11-22 04:24:42 +00002292
Chris Lattnerf4601652007-11-22 20:49:04 +00002293 return TokError("Unexpected input at top level");
2294}
2295