blob: 17f0abc974771c9bdd45de85ad4d7fd5db1f0664 [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/ADT/SmallVector.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000016#include "llvm/ADT/StringExtras.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/Support/CommandLine.h"
18#include "llvm/TableGen/Record.h"
Daniel Dunbar1a551802009-07-03 00:10:29 +000019#include <algorithm>
20#include <sstream>
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
Jakob Stoklund Olesenebaf92c2012-01-13 03:16:35 +000067 if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
Chris Lattnerf4601652007-11-22 20:49:04 +000068 // 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())
Sean Silva6cfc8062012-10-10 20:24:43 +000096 if (VarInit *VI = dyn_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()) {
Sean Silva6cfc8062012-10-10 20:24:43 +0000105 BitsInit *CurVal = dyn_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) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000113 return Error(Loc, "Initializer is not compatible with bit range");
114 }
Bob Wilson21870412009-11-22 04:24:42 +0000115
Chris Lattnerf4601652007-11-22 20:49:04 +0000116 // We should have a BitsInit type now.
Sean Silva6cfc8062012-10-10 20:24:43 +0000117 BitsInit *BInit = dyn_cast<BitsInit>(BI);
Chris Lattnerf4601652007-11-22 20:49:04 +0000118 assert(BInit != 0);
119
David Greene05bce0b2011-07-29 22:43:06 +0000120 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4601652007-11-22 20:49:04 +0000121
122 // Loop over bits, assigning values as appropriate.
123 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
124 unsigned Bit = BitList[i];
David Greeneca7fd3d2011-07-29 19:07:00 +0000125 if (NewBits[Bit])
Chris Lattnerf4601652007-11-22 20:49:04 +0000126 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
David Greene917924d2011-10-19 13:02:39 +0000127 ValName->getAsUnquotedString() + "' more than once");
David Greeneca7fd3d2011-07-29 19:07:00 +0000128 NewBits[Bit] = BInit->getBit(i);
Chris Lattnerf4601652007-11-22 20:49:04 +0000129 }
130
131 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
David Greeneca7fd3d2011-07-29 19:07:00 +0000132 if (NewBits[i] == 0)
133 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4601652007-11-22 20:49:04 +0000134
David Greenedcd35c72011-07-29 19:07:07 +0000135 V = BitsInit::get(NewBits);
Chris Lattnerf4601652007-11-22 20:49:04 +0000136 }
137
138 if (RV->setValue(V))
David Greene917924d2011-10-19 13:02:39 +0000139 return Error(Loc, "Value '" + ValName->getAsUnquotedString() + "' of type '"
140 + RV->getType()->getAsString() +
141 "' is incompatible with initializer '" + V->getAsString()
142 + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +0000143 return false;
144}
145
146/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
147/// args as SubClass's template arguments.
Cedric Venetaff9c272009-02-14 16:06:42 +0000148bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000149 Record *SC = SubClass.Rec;
150 // Add all of the values in the subclass into the current class.
151 const std::vector<RecordVal> &Vals = SC->getValues();
152 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
153 if (AddValue(CurRec, SubClass.RefLoc, Vals[i]))
154 return true;
155
David Greenee22b3212011-10-19 13:02:42 +0000156 const std::vector<Init *> &TArgs = SC->getTemplateArgs();
Chris Lattnerf4601652007-11-22 20:49:04 +0000157
158 // Ensure that an appropriate number of template arguments are specified.
159 if (TArgs.size() < SubClass.TemplateArgs.size())
160 return Error(SubClass.RefLoc, "More template args specified than expected");
Bob Wilson21870412009-11-22 04:24:42 +0000161
Chris Lattnerf4601652007-11-22 20:49:04 +0000162 // Loop over all of the template arguments, setting them to the specified
163 // value or leaving them as the default if necessary.
164 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
165 if (i < SubClass.TemplateArgs.size()) {
166 // If a value is specified for this template arg, set it now.
Bob Wilson21870412009-11-22 04:24:42 +0000167 if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(),
Chris Lattnerf4601652007-11-22 20:49:04 +0000168 SubClass.TemplateArgs[i]))
169 return true;
Bob Wilson21870412009-11-22 04:24:42 +0000170
Chris Lattnerf4601652007-11-22 20:49:04 +0000171 // Resolve it next.
172 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson21870412009-11-22 04:24:42 +0000173
Chris Lattnerf4601652007-11-22 20:49:04 +0000174 // Now remove it.
175 CurRec->removeValue(TArgs[i]);
176
177 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
178 return Error(SubClass.RefLoc,"Value not specified for template argument #"
David Greenee22b3212011-10-19 13:02:42 +0000179 + utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
180 + ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4601652007-11-22 20:49:04 +0000181 }
182 }
183
184 // Since everything went well, we can now set the "superclass" list for the
185 // current record.
186 const std::vector<Record*> &SCs = SC->getSuperClasses();
187 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
188 if (CurRec->isSubClassOf(SCs[i]))
189 return Error(SubClass.RefLoc,
190 "Already subclass of '" + SCs[i]->getName() + "'!\n");
191 CurRec->addSuperClass(SCs[i]);
192 }
Bob Wilson21870412009-11-22 04:24:42 +0000193
Chris Lattnerf4601652007-11-22 20:49:04 +0000194 if (CurRec->isSubClassOf(SC))
195 return Error(SubClass.RefLoc,
196 "Already subclass of '" + SC->getName() + "'!\n");
197 CurRec->addSuperClass(SC);
198 return false;
199}
200
David Greenede444af2009-04-22 16:42:54 +0000201/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilson440548d2009-04-30 18:26:19 +0000202/// CurMC, resolving its template args as SubMultiClass's
David Greenede444af2009-04-22 16:42:54 +0000203/// template arguments.
Bob Wilson440548d2009-04-30 18:26:19 +0000204bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson1d512df2009-04-30 17:46:20 +0000205 SubMultiClassReference &SubMultiClass) {
David Greenede444af2009-04-22 16:42:54 +0000206 MultiClass *SMC = SubMultiClass.MC;
Bob Wilson440548d2009-04-30 18:26:19 +0000207 Record *CurRec = &CurMC->Rec;
David Greenede444af2009-04-22 16:42:54 +0000208
Bob Wilson440548d2009-04-30 18:26:19 +0000209 const std::vector<RecordVal> &MCVals = CurRec->getValues();
David Greenede444af2009-04-22 16:42:54 +0000210
211 // Add all of the values in the subclass into the current class.
212 const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues();
213 for (unsigned i = 0, e = SMCVals.size(); i != e; ++i)
214 if (AddValue(CurRec, SubMultiClass.RefLoc, SMCVals[i]))
215 return true;
216
Bob Wilson440548d2009-04-30 18:26:19 +0000217 int newDefStart = CurMC->DefPrototypes.size();
David Greened34a73b2009-04-24 16:55:41 +0000218
David Greenede444af2009-04-22 16:42:54 +0000219 // Add all of the defs in the subclass into the current multiclass.
220 for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(),
221 iend = SMC->DefPrototypes.end();
222 i != iend;
223 ++i) {
224 // Clone the def and add it to the current multiclass
225 Record *NewDef = new Record(**i);
226
227 // Add all of the values in the superclass into the current def.
228 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
229 if (AddValue(NewDef, SubMultiClass.RefLoc, MCVals[i]))
230 return true;
231
Bob Wilson440548d2009-04-30 18:26:19 +0000232 CurMC->DefPrototypes.push_back(NewDef);
David Greenede444af2009-04-22 16:42:54 +0000233 }
Bob Wilson32558652009-04-28 19:41:44 +0000234
David Greenee22b3212011-10-19 13:02:42 +0000235 const std::vector<Init *> &SMCTArgs = SMC->Rec.getTemplateArgs();
David Greenede444af2009-04-22 16:42:54 +0000236
David Greened34a73b2009-04-24 16:55:41 +0000237 // Ensure that an appropriate number of template arguments are
238 // specified.
David Greenede444af2009-04-22 16:42:54 +0000239 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
David Greened34a73b2009-04-24 16:55:41 +0000240 return Error(SubMultiClass.RefLoc,
241 "More template args specified than expected");
Bob Wilson32558652009-04-28 19:41:44 +0000242
David Greenede444af2009-04-22 16:42:54 +0000243 // Loop over all of the template arguments, setting them to the specified
244 // value or leaving them as the default if necessary.
245 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
246 if (i < SubMultiClass.TemplateArgs.size()) {
David Greened34a73b2009-04-24 16:55:41 +0000247 // If a value is specified for this template arg, set it in the
248 // superclass now.
249 if (SetValue(CurRec, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000250 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000251 SubMultiClass.TemplateArgs[i]))
252 return true;
253
254 // Resolve it next.
255 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson32558652009-04-28 19:41:44 +0000256
David Greenede444af2009-04-22 16:42:54 +0000257 // Now remove it.
258 CurRec->removeValue(SMCTArgs[i]);
259
David Greened34a73b2009-04-24 16:55:41 +0000260 // If a value is specified for this template arg, set it in the
261 // new defs now.
262 for (MultiClass::RecordVector::iterator j =
Bob Wilson440548d2009-04-30 18:26:19 +0000263 CurMC->DefPrototypes.begin() + newDefStart,
264 jend = CurMC->DefPrototypes.end();
David Greenede444af2009-04-22 16:42:54 +0000265 j != jend;
266 ++j) {
267 Record *Def = *j;
268
David Greened34a73b2009-04-24 16:55:41 +0000269 if (SetValue(Def, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000270 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000271 SubMultiClass.TemplateArgs[i]))
272 return true;
273
274 // Resolve it next.
275 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
276
277 // Now remove it
278 Def->removeValue(SMCTArgs[i]);
279 }
280 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
David Greened34a73b2009-04-24 16:55:41 +0000281 return Error(SubMultiClass.RefLoc,
282 "Value not specified for template argument #"
David Greenee22b3212011-10-19 13:02:42 +0000283 + utostr(i) + " (" + SMCTArgs[i]->getAsUnquotedString()
284 + ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greenede444af2009-04-22 16:42:54 +0000285 }
286 }
287
288 return false;
289}
290
David Greenecebb4ee2012-02-22 16:09:41 +0000291/// ProcessForeachDefs - Given a record, apply all of the variable
292/// values in all surrounding foreach loops, creating new records for
293/// each combination of values.
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000294bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
295 if (Loops.empty())
296 return false;
297
David Greenecebb4ee2012-02-22 16:09:41 +0000298 // We want to instantiate a new copy of CurRec for each combination
299 // of nested loop iterator values. We don't want top instantiate
300 // any copies until we have values for each loop iterator.
301 IterSet IterVals;
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000302 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenecebb4ee2012-02-22 16:09:41 +0000303}
304
305/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
306/// apply each of the variable values in this loop and then process
307/// subloops.
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000308bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
309 // Recursively build a tuple of iterator values.
310 if (IterVals.size() != Loops.size()) {
311 assert(IterVals.size() < Loops.size());
312 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silva6cfc8062012-10-10 20:24:43 +0000313 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000314 if (List == 0) {
315 Error(Loc, "Loop list is not a list");
316 return true;
317 }
David Greenecebb4ee2012-02-22 16:09:41 +0000318
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000319 // Process each value.
320 for (int64_t i = 0; i < List->getSize(); ++i) {
321 Init *ItemVal = List->resolveListElementReference(*CurRec, 0, i);
322 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
323 if (ProcessForeachDefs(CurRec, Loc, IterVals))
324 return true;
325 IterVals.pop_back();
326 }
327 return false;
328 }
329
330 // This is the bottom of the recursion. We have all of the iterator values
331 // for this point in the iteration space. Instantiate a new record to
332 // reflect this combination of values.
333 Record *IterRec = new Record(*CurRec);
334
335 // Set the iterator values now.
336 for (unsigned i = 0, e = IterVals.size(); i != e; ++i) {
337 VarInit *IterVar = IterVals[i].IterVar;
Sean Silva6cfc8062012-10-10 20:24:43 +0000338 TypedInit *IVal = dyn_cast<TypedInit>(IterVals[i].IterValue);
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000339 if (IVal == 0) {
340 Error(Loc, "foreach iterator value is untyped");
341 return true;
342 }
343
344 IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false));
345
346 if (SetValue(IterRec, Loc, IterVar->getName(),
347 std::vector<unsigned>(), IVal)) {
348 Error(Loc, "when instantiating this def");
349 return true;
350 }
351
352 // Resolve it next.
353 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName()));
354
355 // Remove it.
356 IterRec->removeValue(IterVar->getName());
357 }
358
359 if (Records.getDef(IterRec->getNameInitAsString())) {
360 Error(Loc, "def already exists: " + IterRec->getNameInitAsString());
David Greenecebb4ee2012-02-22 16:09:41 +0000361 return true;
362 }
363
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000364 Records.addDef(IterRec);
365 IterRec->resolveReferences();
David Greenecebb4ee2012-02-22 16:09:41 +0000366 return false;
367}
368
Chris Lattnerf4601652007-11-22 20:49:04 +0000369//===----------------------------------------------------------------------===//
370// Parser Code
371//===----------------------------------------------------------------------===//
372
373/// isObjectStart - Return true if this is a valid first token for an Object.
374static bool isObjectStart(tgtok::TokKind K) {
375 return K == tgtok::Class || K == tgtok::Def ||
David Greenecebb4ee2012-02-22 16:09:41 +0000376 K == tgtok::Defm || K == tgtok::Let ||
377 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4601652007-11-22 20:49:04 +0000378}
379
Chris Lattnerdf72eae2010-10-05 22:51:56 +0000380static std::string GetNewAnonymousName() {
381 static unsigned AnonCounter = 0;
382 return "anonymous."+utostr(AnonCounter++);
383}
384
Chris Lattnerf4601652007-11-22 20:49:04 +0000385/// ParseObjectName - If an object name is specified, return it. Otherwise,
386/// return an anonymous name.
David Greenea9e07dd2011-10-19 13:04:29 +0000387/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4601652007-11-22 20:49:04 +0000388/// ObjectName ::= /*empty*/
389///
David Greenea9e07dd2011-10-19 13:04:29 +0000390Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
391 switch (Lex.getCode()) {
392 case tgtok::colon:
393 case tgtok::semi:
394 case tgtok::l_brace:
395 // These are all of the tokens that can begin an object body.
396 // Some of these can also begin values but we disallow those cases
397 // because they are unlikely to be useful.
398 return StringInit::get(GetNewAnonymousName());
David Greenea9e07dd2011-10-19 13:04:29 +0000399 default:
400 break;
401 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000402
David Greenea9e07dd2011-10-19 13:04:29 +0000403 Record *CurRec = 0;
404 if (CurMultiClass)
405 CurRec = &CurMultiClass->Rec;
406
407 RecTy *Type = 0;
408 if (CurRec) {
Sean Silva3f7b7f82012-10-10 20:24:47 +0000409 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greenea9e07dd2011-10-19 13:04:29 +0000410 if (!CurRecName) {
411 TokError("Record name is not typed!");
412 return 0;
413 }
414 Type = CurRecName->getType();
415 }
416
417 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4601652007-11-22 20:49:04 +0000418}
419
Chris Lattnerf4601652007-11-22 20:49:04 +0000420/// ParseClassID - Parse and resolve a reference to a class name. This returns
421/// null on error.
422///
423/// ClassID ::= ID
424///
425Record *TGParser::ParseClassID() {
426 if (Lex.getCode() != tgtok::Id) {
427 TokError("expected name for ClassID");
428 return 0;
429 }
Bob Wilson21870412009-11-22 04:24:42 +0000430
Chris Lattnerf4601652007-11-22 20:49:04 +0000431 Record *Result = Records.getClass(Lex.getCurStrVal());
432 if (Result == 0)
433 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson21870412009-11-22 04:24:42 +0000434
Chris Lattnerf4601652007-11-22 20:49:04 +0000435 Lex.Lex();
436 return Result;
437}
438
Bob Wilson32558652009-04-28 19:41:44 +0000439/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
440/// This returns null on error.
David Greenede444af2009-04-22 16:42:54 +0000441///
442/// MultiClassID ::= ID
443///
444MultiClass *TGParser::ParseMultiClassID() {
445 if (Lex.getCode() != tgtok::Id) {
446 TokError("expected name for ClassID");
447 return 0;
448 }
Bob Wilson32558652009-04-28 19:41:44 +0000449
David Greenede444af2009-04-22 16:42:54 +0000450 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
451 if (Result == 0)
452 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson32558652009-04-28 19:41:44 +0000453
David Greenede444af2009-04-22 16:42:54 +0000454 Lex.Lex();
455 return Result;
456}
457
Chris Lattnerf4601652007-11-22 20:49:04 +0000458Record *TGParser::ParseDefmID() {
459 if (Lex.getCode() != tgtok::Id) {
460 TokError("expected multiclass name");
461 return 0;
462 }
Bob Wilson21870412009-11-22 04:24:42 +0000463
Chris Lattnerf4601652007-11-22 20:49:04 +0000464 MultiClass *MC = MultiClasses[Lex.getCurStrVal()];
465 if (MC == 0) {
466 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
467 return 0;
468 }
Bob Wilson21870412009-11-22 04:24:42 +0000469
Chris Lattnerf4601652007-11-22 20:49:04 +0000470 Lex.Lex();
471 return &MC->Rec;
Bob Wilson21870412009-11-22 04:24:42 +0000472}
Chris Lattnerf4601652007-11-22 20:49:04 +0000473
474
475/// ParseSubClassReference - Parse a reference to a subclass or to a templated
476/// subclass. This returns a SubClassRefTy with a null Record* on error.
477///
478/// SubClassRef ::= ClassID
479/// SubClassRef ::= ClassID '<' ValueList '>'
480///
481SubClassReference TGParser::
482ParseSubClassReference(Record *CurRec, bool isDefm) {
483 SubClassReference Result;
484 Result.RefLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +0000485
Chris Lattnerf4601652007-11-22 20:49:04 +0000486 if (isDefm)
487 Result.Rec = ParseDefmID();
488 else
489 Result.Rec = ParseClassID();
490 if (Result.Rec == 0) return Result;
Bob Wilson21870412009-11-22 04:24:42 +0000491
Chris Lattnerf4601652007-11-22 20:49:04 +0000492 // If there is no template arg list, we're done.
493 if (Lex.getCode() != tgtok::less)
494 return Result;
495 Lex.Lex(); // Eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000496
Chris Lattnerf4601652007-11-22 20:49:04 +0000497 if (Lex.getCode() == tgtok::greater) {
498 TokError("subclass reference requires a non-empty list of template values");
499 Result.Rec = 0;
500 return Result;
501 }
Bob Wilson21870412009-11-22 04:24:42 +0000502
David Greenee1b46912009-06-08 20:23:18 +0000503 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4601652007-11-22 20:49:04 +0000504 if (Result.TemplateArgs.empty()) {
505 Result.Rec = 0; // Error parsing value list.
506 return Result;
507 }
Bob Wilson21870412009-11-22 04:24:42 +0000508
Chris Lattnerf4601652007-11-22 20:49:04 +0000509 if (Lex.getCode() != tgtok::greater) {
510 TokError("expected '>' in template value list");
511 Result.Rec = 0;
512 return Result;
513 }
514 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000515
Chris Lattnerf4601652007-11-22 20:49:04 +0000516 return Result;
517}
518
Bob Wilson32558652009-04-28 19:41:44 +0000519/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
520/// templated submulticlass. This returns a SubMultiClassRefTy with a null
521/// Record* on error.
David Greenede444af2009-04-22 16:42:54 +0000522///
523/// SubMultiClassRef ::= MultiClassID
524/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
525///
526SubMultiClassReference TGParser::
527ParseSubMultiClassReference(MultiClass *CurMC) {
528 SubMultiClassReference Result;
529 Result.RefLoc = Lex.getLoc();
Bob Wilson32558652009-04-28 19:41:44 +0000530
David Greenede444af2009-04-22 16:42:54 +0000531 Result.MC = ParseMultiClassID();
532 if (Result.MC == 0) return Result;
Bob Wilson32558652009-04-28 19:41:44 +0000533
David Greenede444af2009-04-22 16:42:54 +0000534 // If there is no template arg list, we're done.
535 if (Lex.getCode() != tgtok::less)
536 return Result;
537 Lex.Lex(); // Eat the '<'
Bob Wilson32558652009-04-28 19:41:44 +0000538
David Greenede444af2009-04-22 16:42:54 +0000539 if (Lex.getCode() == tgtok::greater) {
540 TokError("subclass reference requires a non-empty list of template values");
541 Result.MC = 0;
542 return Result;
543 }
Bob Wilson32558652009-04-28 19:41:44 +0000544
David Greenee1b46912009-06-08 20:23:18 +0000545 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greenede444af2009-04-22 16:42:54 +0000546 if (Result.TemplateArgs.empty()) {
547 Result.MC = 0; // Error parsing value list.
548 return Result;
549 }
Bob Wilson32558652009-04-28 19:41:44 +0000550
David Greenede444af2009-04-22 16:42:54 +0000551 if (Lex.getCode() != tgtok::greater) {
552 TokError("expected '>' in template value list");
553 Result.MC = 0;
554 return Result;
555 }
556 Lex.Lex();
557
558 return Result;
559}
560
Chris Lattnerf4601652007-11-22 20:49:04 +0000561/// ParseRangePiece - Parse a bit/value range.
562/// RangePiece ::= INTVAL
563/// RangePiece ::= INTVAL '-' INTVAL
564/// RangePiece ::= INTVAL INTVAL
565bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner811281e2008-01-10 07:01:53 +0000566 if (Lex.getCode() != tgtok::IntVal) {
567 TokError("expected integer or bitrange");
568 return true;
569 }
Dan Gohman63f97202008-10-17 01:33:43 +0000570 int64_t Start = Lex.getCurIntVal();
571 int64_t End;
Bob Wilson21870412009-11-22 04:24:42 +0000572
Chris Lattnerf4601652007-11-22 20:49:04 +0000573 if (Start < 0)
574 return TokError("invalid range, cannot be negative");
Bob Wilson21870412009-11-22 04:24:42 +0000575
Chris Lattnerf4601652007-11-22 20:49:04 +0000576 switch (Lex.Lex()) { // eat first character.
Bob Wilson21870412009-11-22 04:24:42 +0000577 default:
Chris Lattnerf4601652007-11-22 20:49:04 +0000578 Ranges.push_back(Start);
579 return false;
580 case tgtok::minus:
581 if (Lex.Lex() != tgtok::IntVal) {
582 TokError("expected integer value as end of range");
583 return true;
584 }
585 End = Lex.getCurIntVal();
586 break;
587 case tgtok::IntVal:
588 End = -Lex.getCurIntVal();
589 break;
590 }
Bob Wilson21870412009-11-22 04:24:42 +0000591 if (End < 0)
Chris Lattnerf4601652007-11-22 20:49:04 +0000592 return TokError("invalid range, cannot be negative");
593 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000594
Chris Lattnerf4601652007-11-22 20:49:04 +0000595 // Add to the range.
596 if (Start < End) {
597 for (; Start <= End; ++Start)
598 Ranges.push_back(Start);
599 } else {
600 for (; Start >= End; --Start)
601 Ranges.push_back(Start);
602 }
603 return false;
604}
605
606/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
607///
608/// RangeList ::= RangePiece (',' RangePiece)*
609///
610std::vector<unsigned> TGParser::ParseRangeList() {
611 std::vector<unsigned> Result;
Bob Wilson21870412009-11-22 04:24:42 +0000612
Chris Lattnerf4601652007-11-22 20:49:04 +0000613 // Parse the first piece.
614 if (ParseRangePiece(Result))
615 return std::vector<unsigned>();
616 while (Lex.getCode() == tgtok::comma) {
617 Lex.Lex(); // Eat the comma.
618
619 // Parse the next range piece.
620 if (ParseRangePiece(Result))
621 return std::vector<unsigned>();
622 }
623 return Result;
624}
625
626/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
627/// OptionalRangeList ::= '<' RangeList '>'
628/// OptionalRangeList ::= /*empty*/
629bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
630 if (Lex.getCode() != tgtok::less)
631 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000632
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000633 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000634 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000635
Chris Lattnerf4601652007-11-22 20:49:04 +0000636 // Parse the range list.
637 Ranges = ParseRangeList();
638 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000639
Chris Lattnerf4601652007-11-22 20:49:04 +0000640 if (Lex.getCode() != tgtok::greater) {
641 TokError("expected '>' at end of range list");
642 return Error(StartLoc, "to match this '<'");
643 }
644 Lex.Lex(); // eat the '>'.
645 return false;
646}
647
648/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
649/// OptionalBitList ::= '{' RangeList '}'
650/// OptionalBitList ::= /*empty*/
651bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
652 if (Lex.getCode() != tgtok::l_brace)
653 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000654
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000655 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000656 Lex.Lex(); // eat the '{'
Bob Wilson21870412009-11-22 04:24:42 +0000657
Chris Lattnerf4601652007-11-22 20:49:04 +0000658 // Parse the range list.
659 Ranges = ParseRangeList();
660 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000661
Chris Lattnerf4601652007-11-22 20:49:04 +0000662 if (Lex.getCode() != tgtok::r_brace) {
663 TokError("expected '}' at end of bit list");
664 return Error(StartLoc, "to match this '{'");
665 }
666 Lex.Lex(); // eat the '}'.
667 return false;
668}
669
670
671/// ParseType - Parse and return a tblgen type. This returns null on error.
672///
673/// Type ::= STRING // string type
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +0000674/// Type ::= CODE // code type
Chris Lattnerf4601652007-11-22 20:49:04 +0000675/// Type ::= BIT // bit type
676/// Type ::= BITS '<' INTVAL '>' // bits<x> type
677/// Type ::= INT // int type
678/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4601652007-11-22 20:49:04 +0000679/// Type ::= DAG // dag type
680/// Type ::= ClassID // Record Type
681///
682RecTy *TGParser::ParseType() {
683 switch (Lex.getCode()) {
684 default: TokError("Unknown token when expecting a type"); return 0;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000685 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +0000686 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000687 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
688 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000689 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4601652007-11-22 20:49:04 +0000690 case tgtok::Id:
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000691 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Chris Lattnerf4601652007-11-22 20:49:04 +0000692 return 0;
693 case tgtok::Bits: {
694 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
695 TokError("expected '<' after bits type");
696 return 0;
697 }
698 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
699 TokError("expected integer in bits<n> type");
700 return 0;
701 }
Dan Gohman63f97202008-10-17 01:33:43 +0000702 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4601652007-11-22 20:49:04 +0000703 if (Lex.Lex() != tgtok::greater) { // Eat count.
704 TokError("expected '>' at end of bits<n> type");
705 return 0;
706 }
707 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000708 return BitsRecTy::get(Val);
Chris Lattnerf4601652007-11-22 20:49:04 +0000709 }
710 case tgtok::List: {
711 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
712 TokError("expected '<' after list type");
713 return 0;
714 }
715 Lex.Lex(); // Eat '<'
716 RecTy *SubType = ParseType();
717 if (SubType == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +0000718
Chris Lattnerf4601652007-11-22 20:49:04 +0000719 if (Lex.getCode() != tgtok::greater) {
720 TokError("expected '>' at end of list<ty> type");
721 return 0;
722 }
723 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000724 return ListRecTy::get(SubType);
Chris Lattnerf4601652007-11-22 20:49:04 +0000725 }
Bob Wilson21870412009-11-22 04:24:42 +0000726 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000727}
728
729/// ParseIDValue - Parse an ID as a value and decode what it means.
730///
731/// IDValue ::= ID [def local value]
732/// IDValue ::= ID [def template arg]
733/// IDValue ::= ID [multiclass local value]
734/// IDValue ::= ID [multiclass template argument]
735/// IDValue ::= ID [def name]
736///
David Greenef3744a02011-10-19 13:04:20 +0000737Init *TGParser::ParseIDValue(Record *CurRec, IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000738 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
739 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000740 SMLoc Loc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000741 Lex.Lex();
742 return ParseIDValue(CurRec, Name, Loc);
743}
744
745/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
746/// has already been read.
David Greene05bce0b2011-07-29 22:43:06 +0000747Init *TGParser::ParseIDValue(Record *CurRec,
David Greenef3744a02011-10-19 13:04:20 +0000748 const std::string &Name, SMLoc NameLoc,
749 IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000750 if (CurRec) {
751 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenedcd35c72011-07-29 19:07:07 +0000752 return VarInit::get(Name, RV->getType());
Bob Wilson21870412009-11-22 04:24:42 +0000753
David Greenee22b3212011-10-19 13:02:42 +0000754 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
755
David Greenecaa25c82011-10-05 22:42:54 +0000756 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +0000757 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
758 "::");
David Greenecaa25c82011-10-05 22:42:54 +0000759
Chris Lattnerf4601652007-11-22 20:49:04 +0000760 if (CurRec->isTemplateArg(TemplateArgName)) {
761 const RecordVal *RV = CurRec->getValue(TemplateArgName);
762 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000763 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000764 }
765 }
Bob Wilson21870412009-11-22 04:24:42 +0000766
Chris Lattnerf4601652007-11-22 20:49:04 +0000767 if (CurMultiClass) {
David Greenee22b3212011-10-19 13:02:42 +0000768 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
769 "::");
770
Chris Lattnerf4601652007-11-22 20:49:04 +0000771 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
772 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
773 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000774 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000775 }
776 }
Bob Wilson21870412009-11-22 04:24:42 +0000777
David Greenecebb4ee2012-02-22 16:09:41 +0000778 // If this is in a foreach loop, make sure it's not a loop iterator
779 for (LoopVector::iterator i = Loops.begin(), iend = Loops.end();
780 i != iend;
781 ++i) {
Sean Silva6cfc8062012-10-10 20:24:43 +0000782 VarInit *IterVar = dyn_cast<VarInit>(i->IterVar);
David Greenecebb4ee2012-02-22 16:09:41 +0000783 if (IterVar && IterVar->getName() == Name)
784 return IterVar;
785 }
786
David Greenebbec2792011-10-19 13:04:21 +0000787 if (Mode == ParseNameMode)
788 return StringInit::get(Name);
789
Chris Lattnerf4601652007-11-22 20:49:04 +0000790 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000791 return DefInit::get(D);
Chris Lattnerf4601652007-11-22 20:49:04 +0000792
David Greenebbec2792011-10-19 13:04:21 +0000793 if (Mode == ParseValueMode) {
794 Error(NameLoc, "Variable not defined: '" + Name + "'");
795 return 0;
796 }
797
798 return StringInit::get(Name);
Chris Lattnerf4601652007-11-22 20:49:04 +0000799}
800
David Greened418c1b2009-05-14 20:54:48 +0000801/// ParseOperation - Parse an operator. This returns null on error.
802///
803/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
804///
David Greene05bce0b2011-07-29 22:43:06 +0000805Init *TGParser::ParseOperation(Record *CurRec) {
David Greened418c1b2009-05-14 20:54:48 +0000806 switch (Lex.getCode()) {
807 default:
808 TokError("unknown operation");
809 return 0;
David Greene1434f662011-01-07 17:05:37 +0000810 case tgtok::XHead:
811 case tgtok::XTail:
812 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +0000813 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
814 UnOpInit::UnaryOp Code;
815 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000816
David Greenee6c27de2009-05-14 21:22:49 +0000817 switch (Lex.getCode()) {
Craig Topper85814382012-02-07 05:05:23 +0000818 default: llvm_unreachable("Unhandled code!");
David Greenee6c27de2009-05-14 21:22:49 +0000819 case tgtok::XCast:
820 Lex.Lex(); // eat the operation
821 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000822
David Greenee6c27de2009-05-14 21:22:49 +0000823 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000824
David Greenee6c27de2009-05-14 21:22:49 +0000825 if (Type == 0) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000826 TokError("did not get type for unary operator");
David Greenee6c27de2009-05-14 21:22:49 +0000827 return 0;
828 }
David Greened418c1b2009-05-14 20:54:48 +0000829
David Greenee6c27de2009-05-14 21:22:49 +0000830 break;
David Greene1434f662011-01-07 17:05:37 +0000831 case tgtok::XHead:
David Greene5f9f9ba2009-05-14 22:38:31 +0000832 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000833 Code = UnOpInit::HEAD;
David Greene5f9f9ba2009-05-14 22:38:31 +0000834 break;
David Greene1434f662011-01-07 17:05:37 +0000835 case tgtok::XTail:
David Greene5f9f9ba2009-05-14 22:38:31 +0000836 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000837 Code = UnOpInit::TAIL;
David Greene5f9f9ba2009-05-14 22:38:31 +0000838 break;
David Greene1434f662011-01-07 17:05:37 +0000839 case tgtok::XEmpty:
David Greene5f9f9ba2009-05-14 22:38:31 +0000840 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000841 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000842 Type = IntRecTy::get();
David Greene5f9f9ba2009-05-14 22:38:31 +0000843 break;
David Greenee6c27de2009-05-14 21:22:49 +0000844 }
845 if (Lex.getCode() != tgtok::l_paren) {
846 TokError("expected '(' after unary operator");
847 return 0;
848 }
849 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000850
David Greene05bce0b2011-07-29 22:43:06 +0000851 Init *LHS = ParseValue(CurRec);
David Greenee6c27de2009-05-14 21:22:49 +0000852 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000853
David Greene1434f662011-01-07 17:05:37 +0000854 if (Code == UnOpInit::HEAD
855 || Code == UnOpInit::TAIL
856 || Code == UnOpInit::EMPTY) {
Sean Silva6cfc8062012-10-10 20:24:43 +0000857 ListInit *LHSl = dyn_cast<ListInit>(LHS);
858 StringInit *LHSs = dyn_cast<StringInit>(LHS);
859 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000860 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
861 TokError("expected list or string type argument in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000862 return 0;
863 }
864 if (LHSt) {
Sean Silva736ceac2012-10-05 03:31:58 +0000865 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
866 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
David Greenee1b46912009-06-08 20:23:18 +0000867 if (LType == 0 && SType == 0) {
868 TokError("expected list or string type argumnet in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000869 return 0;
870 }
871 }
872
David Greene1434f662011-01-07 17:05:37 +0000873 if (Code == UnOpInit::HEAD
874 || Code == UnOpInit::TAIL) {
David Greenee1b46912009-06-08 20:23:18 +0000875 if (LHSl == 0 && LHSt == 0) {
876 TokError("expected list type argumnet in unary operator");
877 return 0;
878 }
Bob Wilson21870412009-11-22 04:24:42 +0000879
David Greene5f9f9ba2009-05-14 22:38:31 +0000880 if (LHSl && LHSl->getSize() == 0) {
881 TokError("empty list argument in unary operator");
882 return 0;
883 }
884 if (LHSl) {
David Greene05bce0b2011-07-29 22:43:06 +0000885 Init *Item = LHSl->getElement(0);
Sean Silva6cfc8062012-10-10 20:24:43 +0000886 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
David Greene5f9f9ba2009-05-14 22:38:31 +0000887 if (Itemt == 0) {
888 TokError("untyped list element in unary operator");
889 return 0;
890 }
David Greene1434f662011-01-07 17:05:37 +0000891 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000892 Type = Itemt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000893 } else {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000894 Type = ListRecTy::get(Itemt->getType());
David Greene5f9f9ba2009-05-14 22:38:31 +0000895 }
Bob Wilson21870412009-11-22 04:24:42 +0000896 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000897 assert(LHSt && "expected list type argument in unary operator");
Sean Silva736ceac2012-10-05 03:31:58 +0000898 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
David Greene5f9f9ba2009-05-14 22:38:31 +0000899 if (LType == 0) {
900 TokError("expected list type argumnet in unary operator");
901 return 0;
902 }
David Greene1434f662011-01-07 17:05:37 +0000903 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000904 Type = LType->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +0000905 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000906 Type = LType;
907 }
908 }
909 }
910 }
911
David Greenee6c27de2009-05-14 21:22:49 +0000912 if (Lex.getCode() != tgtok::r_paren) {
913 TokError("expected ')' in unary operator");
914 return 0;
915 }
916 Lex.Lex(); // eat the ')'
David Greenedcd35c72011-07-29 19:07:07 +0000917 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee6c27de2009-05-14 21:22:49 +0000918 }
David Greened418c1b2009-05-14 20:54:48 +0000919
920 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +0000921 case tgtok::XSRA:
David Greened418c1b2009-05-14 20:54:48 +0000922 case tgtok::XSRL:
923 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +0000924 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +0000925 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000926 tgtok::TokKind OpTok = Lex.getCode();
927 SMLoc OpLoc = Lex.getLoc();
928 Lex.Lex(); // eat the operation
929
David Greened418c1b2009-05-14 20:54:48 +0000930 BinOpInit::BinaryOp Code;
931 RecTy *Type = 0;
932
Chris Lattner8d978a72010-10-05 23:58:18 +0000933 switch (OpTok) {
Craig Topper85814382012-02-07 05:05:23 +0000934 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000935 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
936 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
937 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
938 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
939 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Bob Wilson21870412009-11-22 04:24:42 +0000940 case tgtok::XStrConcat:
David Greened418c1b2009-05-14 20:54:48 +0000941 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000942 Type = StringRecTy::get();
David Greened418c1b2009-05-14 20:54:48 +0000943 break;
David Greened418c1b2009-05-14 20:54:48 +0000944 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000945
David Greened418c1b2009-05-14 20:54:48 +0000946 if (Lex.getCode() != tgtok::l_paren) {
947 TokError("expected '(' after binary operator");
948 return 0;
949 }
950 Lex.Lex(); // eat the '('
951
David Greene05bce0b2011-07-29 22:43:06 +0000952 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000953
Chris Lattner8d978a72010-10-05 23:58:18 +0000954 InitList.push_back(ParseValue(CurRec));
955 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000956
Chris Lattner8d978a72010-10-05 23:58:18 +0000957 while (Lex.getCode() == tgtok::comma) {
958 Lex.Lex(); // eat the ','
959
960 InitList.push_back(ParseValue(CurRec));
961 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000962 }
David Greened418c1b2009-05-14 20:54:48 +0000963
964 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner8d978a72010-10-05 23:58:18 +0000965 TokError("expected ')' in operator");
David Greened418c1b2009-05-14 20:54:48 +0000966 return 0;
967 }
968 Lex.Lex(); // eat the ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000969
970 // We allow multiple operands to associative operators like !strconcat as
971 // shorthand for nesting them.
972 if (Code == BinOpInit::STRCONCAT) {
973 while (InitList.size() > 2) {
David Greene05bce0b2011-07-29 22:43:06 +0000974 Init *RHS = InitList.pop_back_val();
David Greenedcd35c72011-07-29 19:07:07 +0000975 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
976 ->Fold(CurRec, CurMultiClass);
Chris Lattner8d978a72010-10-05 23:58:18 +0000977 InitList.back() = RHS;
978 }
979 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000980
Chris Lattner8d978a72010-10-05 23:58:18 +0000981 if (InitList.size() == 2)
David Greenedcd35c72011-07-29 19:07:07 +0000982 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner8d978a72010-10-05 23:58:18 +0000983 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000984
Chris Lattner8d978a72010-10-05 23:58:18 +0000985 Error(OpLoc, "expected two operands to operator");
986 return 0;
David Greened418c1b2009-05-14 20:54:48 +0000987 }
988
David Greene9bea7c82009-05-14 23:26:46 +0000989 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +0000990 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +0000991 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
992 TernOpInit::TernaryOp Code;
993 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000994
David Greene4afc5092009-05-14 21:54:42 +0000995 tgtok::TokKind LexCode = Lex.getCode();
996 Lex.Lex(); // eat the operation
997 switch (LexCode) {
Craig Topper85814382012-02-07 05:05:23 +0000998 default: llvm_unreachable("Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000999 case tgtok::XIf:
1000 Code = TernOpInit::IF;
1001 break;
David Greenebeb31a52009-05-14 22:23:47 +00001002 case tgtok::XForEach:
1003 Code = TernOpInit::FOREACH;
1004 break;
David Greene4afc5092009-05-14 21:54:42 +00001005 case tgtok::XSubst:
1006 Code = TernOpInit::SUBST;
1007 break;
1008 }
1009 if (Lex.getCode() != tgtok::l_paren) {
1010 TokError("expected '(' after ternary operator");
1011 return 0;
1012 }
1013 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +00001014
David Greene05bce0b2011-07-29 22:43:06 +00001015 Init *LHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001016 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001017
David Greene4afc5092009-05-14 21:54:42 +00001018 if (Lex.getCode() != tgtok::comma) {
1019 TokError("expected ',' in ternary operator");
1020 return 0;
1021 }
1022 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001023
David Greene05bce0b2011-07-29 22:43:06 +00001024 Init *MHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001025 if (MHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001026
David Greene4afc5092009-05-14 21:54:42 +00001027 if (Lex.getCode() != tgtok::comma) {
1028 TokError("expected ',' in ternary operator");
1029 return 0;
1030 }
1031 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001032
David Greene05bce0b2011-07-29 22:43:06 +00001033 Init *RHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001034 if (RHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001035
David Greene4afc5092009-05-14 21:54:42 +00001036 if (Lex.getCode() != tgtok::r_paren) {
1037 TokError("expected ')' in binary operator");
1038 return 0;
1039 }
1040 Lex.Lex(); // eat the ')'
David Greened418c1b2009-05-14 20:54:48 +00001041
David Greene4afc5092009-05-14 21:54:42 +00001042 switch (LexCode) {
Craig Topper85814382012-02-07 05:05:23 +00001043 default: llvm_unreachable("Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +00001044 case tgtok::XIf: {
Bill Wendling548f5a02010-12-13 01:46:19 +00001045 RecTy *MHSTy = 0;
1046 RecTy *RHSTy = 0;
1047
Sean Silva6cfc8062012-10-10 20:24:43 +00001048 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling548f5a02010-12-13 01:46:19 +00001049 MHSTy = MHSt->getType();
Sean Silva6cfc8062012-10-10 20:24:43 +00001050 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao307525c2012-09-06 23:32:48 +00001051 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva3f7b7f82012-10-10 20:24:47 +00001052 if (isa<BitInit>(MHS))
Michael Liao307525c2012-09-06 23:32:48 +00001053 MHSTy = BitRecTy::get();
1054
Sean Silva6cfc8062012-10-10 20:24:43 +00001055 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling548f5a02010-12-13 01:46:19 +00001056 RHSTy = RHSt->getType();
Sean Silva6cfc8062012-10-10 20:24:43 +00001057 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao307525c2012-09-06 23:32:48 +00001058 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva3f7b7f82012-10-10 20:24:47 +00001059 if (isa<BitInit>(RHS))
Michael Liao307525c2012-09-06 23:32:48 +00001060 RHSTy = BitRecTy::get();
1061
1062 // For UnsetInit, it's typed from the other hand.
Sean Silva3f7b7f82012-10-10 20:24:47 +00001063 if (isa<UnsetInit>(MHS))
Michael Liao307525c2012-09-06 23:32:48 +00001064 MHSTy = RHSTy;
Sean Silva3f7b7f82012-10-10 20:24:47 +00001065 if (isa<UnsetInit>(RHS))
Michael Liao307525c2012-09-06 23:32:48 +00001066 RHSTy = MHSTy;
Bill Wendling548f5a02010-12-13 01:46:19 +00001067
1068 if (!MHSTy || !RHSTy) {
David Greene9bea7c82009-05-14 23:26:46 +00001069 TokError("could not get type for !if");
1070 return 0;
1071 }
Bill Wendling548f5a02010-12-13 01:46:19 +00001072
1073 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1074 Type = RHSTy;
1075 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1076 Type = MHSTy;
Bob Wilson21870412009-11-22 04:24:42 +00001077 } else {
David Greene9bea7c82009-05-14 23:26:46 +00001078 TokError("inconsistent types for !if");
1079 return 0;
1080 }
1081 break;
1082 }
David Greenebeb31a52009-05-14 22:23:47 +00001083 case tgtok::XForEach: {
Sean Silva6cfc8062012-10-10 20:24:43 +00001084 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
David Greenebeb31a52009-05-14 22:23:47 +00001085 if (MHSt == 0) {
1086 TokError("could not get type for !foreach");
1087 return 0;
1088 }
1089 Type = MHSt->getType();
1090 break;
1091 }
David Greene4afc5092009-05-14 21:54:42 +00001092 case tgtok::XSubst: {
Sean Silva6cfc8062012-10-10 20:24:43 +00001093 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
David Greene4afc5092009-05-14 21:54:42 +00001094 if (RHSt == 0) {
1095 TokError("could not get type for !subst");
1096 return 0;
1097 }
1098 Type = RHSt->getType();
1099 break;
1100 }
1101 }
David Greenedcd35c72011-07-29 19:07:07 +00001102 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson21870412009-11-22 04:24:42 +00001103 CurMultiClass);
David Greene4afc5092009-05-14 21:54:42 +00001104 }
David Greened418c1b2009-05-14 20:54:48 +00001105 }
David Greened418c1b2009-05-14 20:54:48 +00001106}
1107
1108/// ParseOperatorType - Parse a type for an operator. This returns
1109/// null on error.
1110///
1111/// OperatorType ::= '<' Type '>'
1112///
Dan Gohmana9ad0412009-08-12 22:10:57 +00001113RecTy *TGParser::ParseOperatorType() {
David Greened418c1b2009-05-14 20:54:48 +00001114 RecTy *Type = 0;
1115
1116 if (Lex.getCode() != tgtok::less) {
1117 TokError("expected type name for operator");
1118 return 0;
1119 }
1120 Lex.Lex(); // eat the <
1121
1122 Type = ParseType();
1123
1124 if (Type == 0) {
1125 TokError("expected type name for operator");
1126 return 0;
1127 }
1128
1129 if (Lex.getCode() != tgtok::greater) {
1130 TokError("expected type name for operator");
1131 return 0;
1132 }
1133 Lex.Lex(); // eat the >
1134
1135 return Type;
1136}
1137
1138
Chris Lattnerf4601652007-11-22 20:49:04 +00001139/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1140///
1141/// SimpleValue ::= IDValue
1142/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001143/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +00001144/// SimpleValue ::= CODEFRAGMENT
1145/// SimpleValue ::= '?'
1146/// SimpleValue ::= '{' ValueList '}'
1147/// SimpleValue ::= ID '<' ValueListNE '>'
1148/// SimpleValue ::= '[' ValueList ']'
1149/// SimpleValue ::= '(' IDValue DagArgList ')'
1150/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
1151/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1152/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1153/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1154/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1155///
David Greenef3744a02011-10-19 13:04:20 +00001156Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1157 IDParseMode Mode) {
David Greene05bce0b2011-07-29 22:43:06 +00001158 Init *R = 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001159 switch (Lex.getCode()) {
1160 default: TokError("Unknown token when parsing a value"); break;
David Greened3d1cad2011-10-19 13:04:43 +00001161 case tgtok::paste:
1162 // This is a leading paste operation. This is deprecated but
1163 // still exists in some .td files. Ignore it.
1164 Lex.Lex(); // Skip '#'.
1165 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenedcd35c72011-07-29 19:07:07 +00001166 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001167 case tgtok::StrVal: {
1168 std::string Val = Lex.getCurStrVal();
1169 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001170
Jim Grosbachda4231f2009-03-26 16:17:51 +00001171 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001172 while (Lex.getCode() == tgtok::StrVal) {
1173 Val += Lex.getCurStrVal();
1174 Lex.Lex();
1175 }
Bob Wilson21870412009-11-22 04:24:42 +00001176
David Greenedcd35c72011-07-29 19:07:07 +00001177 R = StringInit::get(Val);
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001178 break;
1179 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001180 case tgtok::CodeFragment:
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +00001181 R = StringInit::get(Lex.getCurStrVal());
Chris Lattner578bcf02010-10-06 04:31:40 +00001182 Lex.Lex();
1183 break;
1184 case tgtok::question:
David Greenedcd35c72011-07-29 19:07:07 +00001185 R = UnsetInit::get();
Chris Lattner578bcf02010-10-06 04:31:40 +00001186 Lex.Lex();
1187 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001188 case tgtok::Id: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001189 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001190 std::string Name = Lex.getCurStrVal();
1191 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greenef3744a02011-10-19 13:04:20 +00001192 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson21870412009-11-22 04:24:42 +00001193
Chris Lattnerf4601652007-11-22 20:49:04 +00001194 // Value ::= ID '<' ValueListNE '>'
1195 if (Lex.Lex() == tgtok::greater) {
1196 TokError("expected non-empty value list");
1197 return 0;
1198 }
David Greenee1b46912009-06-08 20:23:18 +00001199
Chris Lattnerf4601652007-11-22 20:49:04 +00001200 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1201 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1202 // body.
1203 Record *Class = Records.getClass(Name);
1204 if (!Class) {
1205 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1206 return 0;
1207 }
David Greenee1b46912009-06-08 20:23:18 +00001208
David Greene05bce0b2011-07-29 22:43:06 +00001209 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
David Greenee1b46912009-06-08 20:23:18 +00001210 if (ValueList.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001211
David Greenee1b46912009-06-08 20:23:18 +00001212 if (Lex.getCode() != tgtok::greater) {
1213 TokError("expected '>' at end of value list");
1214 return 0;
1215 }
1216 Lex.Lex(); // eat the '>'
Bob Wilson21870412009-11-22 04:24:42 +00001217
Chris Lattnerf4601652007-11-22 20:49:04 +00001218 // Create the new record, set it as CurRec temporarily.
1219 static unsigned AnonCounter = 0;
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001220 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),
1221 NameLoc,
1222 Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001223 SubClassReference SCRef;
1224 SCRef.RefLoc = NameLoc;
1225 SCRef.Rec = Class;
1226 SCRef.TemplateArgs = ValueList;
1227 // Add info about the subclass to NewRec.
1228 if (AddSubClass(NewRec, SCRef))
1229 return 0;
1230 NewRec->resolveReferences();
1231 Records.addDef(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001232
Chris Lattnerf4601652007-11-22 20:49:04 +00001233 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +00001234 return DefInit::get(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001235 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001236 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001237 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001238 Lex.Lex(); // eat the '{'
David Greene05bce0b2011-07-29 22:43:06 +00001239 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001240
Chris Lattnerf4601652007-11-22 20:49:04 +00001241 if (Lex.getCode() != tgtok::r_brace) {
1242 Vals = ParseValueList(CurRec);
1243 if (Vals.empty()) return 0;
1244 }
1245 if (Lex.getCode() != tgtok::r_brace) {
1246 TokError("expected '}' at end of bit list value");
1247 return 0;
1248 }
1249 Lex.Lex(); // eat the '}'
Bob Wilson21870412009-11-22 04:24:42 +00001250
David Greene05bce0b2011-07-29 22:43:06 +00001251 SmallVector<Init *, 16> NewBits(Vals.size());
David Greeneca7fd3d2011-07-29 19:07:00 +00001252
Chris Lattnerf4601652007-11-22 20:49:04 +00001253 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001254 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Chris Lattnerf4601652007-11-22 20:49:04 +00001255 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +00001256 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1257 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +00001258 return 0;
1259 }
David Greeneca7fd3d2011-07-29 19:07:00 +00001260 NewBits[Vals.size()-i-1] = Bit;
Chris Lattnerf4601652007-11-22 20:49:04 +00001261 }
David Greenedcd35c72011-07-29 19:07:07 +00001262 return BitsInit::get(NewBits);
Chris Lattnerf4601652007-11-22 20:49:04 +00001263 }
1264 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1265 Lex.Lex(); // eat the '['
David Greene05bce0b2011-07-29 22:43:06 +00001266 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001267
David Greenee1b46912009-06-08 20:23:18 +00001268 RecTy *DeducedEltTy = 0;
1269 ListRecTy *GivenListTy = 0;
Bob Wilson21870412009-11-22 04:24:42 +00001270
David Greenee1b46912009-06-08 20:23:18 +00001271 if (ItemType != 0) {
Sean Silva736ceac2012-10-05 03:31:58 +00001272 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
David Greenee1b46912009-06-08 20:23:18 +00001273 if (ListType == 0) {
1274 std::stringstream s;
Bob Wilson21870412009-11-22 04:24:42 +00001275 s << "Type mismatch for list, expected list type, got "
David Greenee1b46912009-06-08 20:23:18 +00001276 << ItemType->getAsString();
1277 TokError(s.str());
Jim Grosbach6a44ada2011-03-11 19:52:52 +00001278 return 0;
David Greenee1b46912009-06-08 20:23:18 +00001279 }
1280 GivenListTy = ListType;
Bob Wilson21870412009-11-22 04:24:42 +00001281 }
David Greenee1b46912009-06-08 20:23:18 +00001282
Chris Lattnerf4601652007-11-22 20:49:04 +00001283 if (Lex.getCode() != tgtok::r_square) {
Bob Wilson21870412009-11-22 04:24:42 +00001284 Vals = ParseValueList(CurRec, 0,
1285 GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001286 if (Vals.empty()) return 0;
1287 }
1288 if (Lex.getCode() != tgtok::r_square) {
1289 TokError("expected ']' at end of list value");
1290 return 0;
1291 }
1292 Lex.Lex(); // eat the ']'
David Greenee1b46912009-06-08 20:23:18 +00001293
1294 RecTy *GivenEltTy = 0;
1295 if (Lex.getCode() == tgtok::less) {
1296 // Optional list element type
1297 Lex.Lex(); // eat the '<'
1298
1299 GivenEltTy = ParseType();
1300 if (GivenEltTy == 0) {
1301 // Couldn't parse element type
1302 return 0;
1303 }
1304
1305 if (Lex.getCode() != tgtok::greater) {
1306 TokError("expected '>' at end of list element type");
1307 return 0;
1308 }
1309 Lex.Lex(); // eat the '>'
1310 }
1311
1312 // Check elements
1313 RecTy *EltTy = 0;
David Greene05bce0b2011-07-29 22:43:06 +00001314 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greenee1b46912009-06-08 20:23:18 +00001315 i != ie;
1316 ++i) {
Sean Silva6cfc8062012-10-10 20:24:43 +00001317 TypedInit *TArg = dyn_cast<TypedInit>(*i);
David Greenee1b46912009-06-08 20:23:18 +00001318 if (TArg == 0) {
1319 TokError("Untyped list element");
1320 return 0;
1321 }
1322 if (EltTy != 0) {
1323 EltTy = resolveTypes(EltTy, TArg->getType());
1324 if (EltTy == 0) {
1325 TokError("Incompatible types in list elements");
1326 return 0;
1327 }
Bob Wilson21870412009-11-22 04:24:42 +00001328 } else {
David Greenee1b46912009-06-08 20:23:18 +00001329 EltTy = TArg->getType();
1330 }
1331 }
1332
1333 if (GivenEltTy != 0) {
1334 if (EltTy != 0) {
1335 // Verify consistency
1336 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1337 TokError("Incompatible types in list elements");
1338 return 0;
1339 }
1340 }
1341 EltTy = GivenEltTy;
1342 }
1343
1344 if (EltTy == 0) {
1345 if (ItemType == 0) {
1346 TokError("No type for list");
1347 return 0;
1348 }
1349 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +00001350 } else {
David Greenee1b46912009-06-08 20:23:18 +00001351 // Make sure the deduced type is compatible with the given type
1352 if (GivenListTy) {
1353 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1354 TokError("Element type mismatch for list");
1355 return 0;
1356 }
1357 }
1358 DeducedEltTy = EltTy;
1359 }
Bob Wilson21870412009-11-22 04:24:42 +00001360
David Greenedcd35c72011-07-29 19:07:07 +00001361 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4601652007-11-22 20:49:04 +00001362 }
1363 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1364 Lex.Lex(); // eat the '('
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001365 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001366 TokError("expected identifier in dag init");
1367 return 0;
1368 }
Bob Wilson21870412009-11-22 04:24:42 +00001369
David Greene05bce0b2011-07-29 22:43:06 +00001370 Init *Operator = ParseValue(CurRec);
Chris Lattner578bcf02010-10-06 04:31:40 +00001371 if (Operator == 0) return 0;
David Greenec7cafcd2009-04-22 20:18:10 +00001372
Nate Begeman7cee8172009-03-19 05:21:56 +00001373 // If the operator name is present, parse it.
1374 std::string OperatorName;
1375 if (Lex.getCode() == tgtok::colon) {
1376 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1377 TokError("expected variable name in dag operator");
1378 return 0;
1379 }
1380 OperatorName = Lex.getCurStrVal();
1381 Lex.Lex(); // eat the VarName.
1382 }
Bob Wilson21870412009-11-22 04:24:42 +00001383
David Greene05bce0b2011-07-29 22:43:06 +00001384 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4601652007-11-22 20:49:04 +00001385 if (Lex.getCode() != tgtok::r_paren) {
1386 DagArgs = ParseDagArgList(CurRec);
1387 if (DagArgs.empty()) return 0;
1388 }
Bob Wilson21870412009-11-22 04:24:42 +00001389
Chris Lattnerf4601652007-11-22 20:49:04 +00001390 if (Lex.getCode() != tgtok::r_paren) {
1391 TokError("expected ')' in dag init");
1392 return 0;
1393 }
1394 Lex.Lex(); // eat the ')'
Bob Wilson21870412009-11-22 04:24:42 +00001395
David Greenedcd35c72011-07-29 19:07:07 +00001396 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4601652007-11-22 20:49:04 +00001397 }
Bob Wilson21870412009-11-22 04:24:42 +00001398
David Greene1434f662011-01-07 17:05:37 +00001399 case tgtok::XHead:
1400 case tgtok::XTail:
1401 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +00001402 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001403 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +00001404 case tgtok::XSRA:
Chris Lattnerf4601652007-11-22 20:49:04 +00001405 case tgtok::XSRL:
1406 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +00001407 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001408 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene9bea7c82009-05-14 23:26:46 +00001409 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +00001410 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001411 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greened418c1b2009-05-14 20:54:48 +00001412 return ParseOperation(CurRec);
Chris Lattnerf4601652007-11-22 20:49:04 +00001413 }
1414 }
Bob Wilson21870412009-11-22 04:24:42 +00001415
Chris Lattnerf4601652007-11-22 20:49:04 +00001416 return R;
1417}
1418
1419/// ParseValue - Parse a tblgen value. This returns null on error.
1420///
1421/// Value ::= SimpleValue ValueSuffix*
1422/// ValueSuffix ::= '{' BitList '}'
1423/// ValueSuffix ::= '[' BitList ']'
1424/// ValueSuffix ::= '.' ID
1425///
David Greenef3744a02011-10-19 13:04:20 +00001426Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1427 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Chris Lattnerf4601652007-11-22 20:49:04 +00001428 if (Result == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001429
Chris Lattnerf4601652007-11-22 20:49:04 +00001430 // Parse the suffixes now if present.
1431 while (1) {
1432 switch (Lex.getCode()) {
1433 default: return Result;
1434 case tgtok::l_brace: {
David Greenecebb4ee2012-02-22 16:09:41 +00001435 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greene8592b2b2011-10-19 13:04:26 +00001436 // This is the beginning of the object body.
1437 return Result;
1438
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001439 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001440 Lex.Lex(); // eat the '{'
1441 std::vector<unsigned> Ranges = ParseRangeList();
1442 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001443
Chris Lattnerf4601652007-11-22 20:49:04 +00001444 // Reverse the bitlist.
1445 std::reverse(Ranges.begin(), Ranges.end());
1446 Result = Result->convertInitializerBitRange(Ranges);
1447 if (Result == 0) {
1448 Error(CurlyLoc, "Invalid bit range for value");
1449 return 0;
1450 }
Bob Wilson21870412009-11-22 04:24:42 +00001451
Chris Lattnerf4601652007-11-22 20:49:04 +00001452 // Eat the '}'.
1453 if (Lex.getCode() != tgtok::r_brace) {
1454 TokError("expected '}' at end of bit range list");
1455 return 0;
1456 }
1457 Lex.Lex();
1458 break;
1459 }
1460 case tgtok::l_square: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001461 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001462 Lex.Lex(); // eat the '['
1463 std::vector<unsigned> Ranges = ParseRangeList();
1464 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001465
Chris Lattnerf4601652007-11-22 20:49:04 +00001466 Result = Result->convertInitListSlice(Ranges);
1467 if (Result == 0) {
1468 Error(SquareLoc, "Invalid range for list slice");
1469 return 0;
1470 }
Bob Wilson21870412009-11-22 04:24:42 +00001471
Chris Lattnerf4601652007-11-22 20:49:04 +00001472 // Eat the ']'.
1473 if (Lex.getCode() != tgtok::r_square) {
1474 TokError("expected ']' at end of list slice");
1475 return 0;
1476 }
1477 Lex.Lex();
1478 break;
1479 }
1480 case tgtok::period:
1481 if (Lex.Lex() != tgtok::Id) { // eat the .
1482 TokError("expected field identifier after '.'");
1483 return 0;
1484 }
1485 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001486 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00001487 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +00001488 return 0;
1489 }
David Greenedcd35c72011-07-29 19:07:07 +00001490 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001491 Lex.Lex(); // eat field name
1492 break;
David Greened3d1cad2011-10-19 13:04:43 +00001493
1494 case tgtok::paste:
1495 SMLoc PasteLoc = Lex.getLoc();
1496
1497 // Create a !strconcat() operation, first casting each operand to
1498 // a string if necessary.
1499
Sean Silva6cfc8062012-10-10 20:24:43 +00001500 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greened3d1cad2011-10-19 13:04:43 +00001501 if (!LHS) {
1502 Error(PasteLoc, "LHS of paste is not typed!");
1503 return 0;
1504 }
1505
1506 if (LHS->getType() != StringRecTy::get()) {
1507 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1508 }
1509
1510 TypedInit *RHS = 0;
1511
1512 Lex.Lex(); // Eat the '#'.
1513 switch (Lex.getCode()) {
1514 case tgtok::colon:
1515 case tgtok::semi:
1516 case tgtok::l_brace:
1517 // These are all of the tokens that can begin an object body.
1518 // Some of these can also begin values but we disallow those cases
1519 // because they are unlikely to be useful.
1520
1521 // Trailing paste, concat with an empty string.
1522 RHS = StringInit::get("");
1523 break;
1524
1525 default:
1526 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silva6cfc8062012-10-10 20:24:43 +00001527 RHS = dyn_cast<TypedInit>(RHSResult);
David Greened3d1cad2011-10-19 13:04:43 +00001528 if (!RHS) {
1529 Error(PasteLoc, "RHS of paste is not typed!");
1530 return 0;
1531 }
1532
1533 if (RHS->getType() != StringRecTy::get()) {
1534 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1535 }
1536
1537 break;
1538 }
1539
1540 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1541 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1542 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001543 }
1544 }
1545}
1546
1547/// ParseDagArgList - Parse the argument list for a dag literal expression.
1548///
1549/// ParseDagArgList ::= Value (':' VARNAME)?
1550/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
David Greene05bce0b2011-07-29 22:43:06 +00001551std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4601652007-11-22 20:49:04 +00001552TGParser::ParseDagArgList(Record *CurRec) {
David Greene05bce0b2011-07-29 22:43:06 +00001553 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson21870412009-11-22 04:24:42 +00001554
Chris Lattnerf4601652007-11-22 20:49:04 +00001555 while (1) {
David Greene05bce0b2011-07-29 22:43:06 +00001556 Init *Val = ParseValue(CurRec);
1557 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
Bob Wilson21870412009-11-22 04:24:42 +00001558
Chris Lattnerf4601652007-11-22 20:49:04 +00001559 // If the variable name is present, add it.
1560 std::string VarName;
1561 if (Lex.getCode() == tgtok::colon) {
1562 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1563 TokError("expected variable name in dag literal");
David Greene05bce0b2011-07-29 22:43:06 +00001564 return std::vector<std::pair<llvm::Init*, std::string> >();
Chris Lattnerf4601652007-11-22 20:49:04 +00001565 }
1566 VarName = Lex.getCurStrVal();
1567 Lex.Lex(); // eat the VarName.
1568 }
Bob Wilson21870412009-11-22 04:24:42 +00001569
Chris Lattnerf4601652007-11-22 20:49:04 +00001570 Result.push_back(std::make_pair(Val, VarName));
Bob Wilson21870412009-11-22 04:24:42 +00001571
Chris Lattnerf4601652007-11-22 20:49:04 +00001572 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson21870412009-11-22 04:24:42 +00001573 Lex.Lex(); // eat the ','
Chris Lattnerf4601652007-11-22 20:49:04 +00001574 }
Bob Wilson21870412009-11-22 04:24:42 +00001575
Chris Lattnerf4601652007-11-22 20:49:04 +00001576 return Result;
1577}
1578
1579
1580/// ParseValueList - Parse a comma separated list of values, returning them as a
1581/// vector. Note that this always expects to be able to parse at least one
1582/// value. It returns an empty list if this is not possible.
1583///
1584/// ValueList ::= Value (',' Value)
1585///
David Greene05bce0b2011-07-29 22:43:06 +00001586std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopherd568b3f2011-07-11 23:06:52 +00001587 RecTy *EltTy) {
David Greene05bce0b2011-07-29 22:43:06 +00001588 std::vector<Init*> Result;
David Greenee1b46912009-06-08 20:23:18 +00001589 RecTy *ItemType = EltTy;
David Greene67acdf22009-06-29 19:59:52 +00001590 unsigned int ArgN = 0;
David Greenee1b46912009-06-08 20:23:18 +00001591 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001592 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Jim Grosbachb1320cb2012-01-20 20:02:39 +00001593 if (!TArgs.size()) {
1594 TokError("template argument provided to non-template class");
1595 return std::vector<Init*>();
1596 }
David Greenee1b46912009-06-08 20:23:18 +00001597 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greened9746fe2011-09-19 18:26:07 +00001598 if (!RV) {
1599 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1600 << ")\n";
1601 }
David Greenee1b46912009-06-08 20:23:18 +00001602 assert(RV && "Template argument record not found??");
1603 ItemType = RV->getType();
1604 ++ArgN;
1605 }
1606 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001607 if (Result.back() == 0) return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001608
Chris Lattnerf4601652007-11-22 20:49:04 +00001609 while (Lex.getCode() == tgtok::comma) {
1610 Lex.Lex(); // Eat the comma
Bob Wilson21870412009-11-22 04:24:42 +00001611
David Greenee1b46912009-06-08 20:23:18 +00001612 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001613 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greene67acdf22009-06-29 19:59:52 +00001614 if (ArgN >= TArgs.size()) {
1615 TokError("too many template arguments");
David Greene05bce0b2011-07-29 22:43:06 +00001616 return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001617 }
David Greenee1b46912009-06-08 20:23:18 +00001618 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1619 assert(RV && "Template argument record not found??");
1620 ItemType = RV->getType();
1621 ++ArgN;
1622 }
1623 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001624 if (Result.back() == 0) return std::vector<Init*>();
Chris Lattnerf4601652007-11-22 20:49:04 +00001625 }
Bob Wilson21870412009-11-22 04:24:42 +00001626
Chris Lattnerf4601652007-11-22 20:49:04 +00001627 return Result;
1628}
1629
1630
Chris Lattnerf4601652007-11-22 20:49:04 +00001631/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1632/// empty string on error. This can happen in a number of different context's,
1633/// including within a def or in the template args for a def (which which case
1634/// CurRec will be non-null) and within the template args for a multiclass (in
1635/// which case CurRec will be null, but CurMultiClass will be set). This can
1636/// also happen within a def that is within a multiclass, which will set both
1637/// CurRec and CurMultiClass.
1638///
1639/// Declaration ::= FIELD? Type ID ('=' Value)?
1640///
David Greenee22b3212011-10-19 13:02:42 +00001641Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4601652007-11-22 20:49:04 +00001642 bool ParsingTemplateArgs) {
1643 // Read the field prefix if present.
1644 bool HasField = Lex.getCode() == tgtok::Field;
1645 if (HasField) Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001646
Chris Lattnerf4601652007-11-22 20:49:04 +00001647 RecTy *Type = ParseType();
David Greenee22b3212011-10-19 13:02:42 +00001648 if (Type == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001649
Chris Lattnerf4601652007-11-22 20:49:04 +00001650 if (Lex.getCode() != tgtok::Id) {
1651 TokError("Expected identifier in declaration");
David Greenee22b3212011-10-19 13:02:42 +00001652 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001653 }
Bob Wilson21870412009-11-22 04:24:42 +00001654
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001655 SMLoc IdLoc = Lex.getLoc();
David Greenee22b3212011-10-19 13:02:42 +00001656 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001657 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001658
Chris Lattnerf4601652007-11-22 20:49:04 +00001659 if (ParsingTemplateArgs) {
1660 if (CurRec) {
David Greenee22b3212011-10-19 13:02:42 +00001661 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4601652007-11-22 20:49:04 +00001662 } else {
1663 assert(CurMultiClass);
1664 }
1665 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +00001666 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1667 "::");
Chris Lattnerf4601652007-11-22 20:49:04 +00001668 }
Bob Wilson21870412009-11-22 04:24:42 +00001669
Chris Lattnerf4601652007-11-22 20:49:04 +00001670 // Add the value.
1671 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
David Greenee22b3212011-10-19 13:02:42 +00001672 return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001673
Chris Lattnerf4601652007-11-22 20:49:04 +00001674 // If a value is present, parse it.
1675 if (Lex.getCode() == tgtok::equal) {
1676 Lex.Lex();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001677 SMLoc ValLoc = Lex.getLoc();
David Greene05bce0b2011-07-29 22:43:06 +00001678 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001679 if (Val == 0 ||
1680 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
David Greenee22b3212011-10-19 13:02:42 +00001681 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001682 }
Bob Wilson21870412009-11-22 04:24:42 +00001683
Chris Lattnerf4601652007-11-22 20:49:04 +00001684 return DeclName;
1685}
1686
David Greenecebb4ee2012-02-22 16:09:41 +00001687/// ParseForeachDeclaration - Read a foreach declaration, returning
1688/// the name of the declared object or a NULL Init on error. Return
1689/// the name of the parsed initializer list through ForeachListName.
1690///
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001691/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1692/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1693/// ForeachDeclaration ::= ID '=' RangePiece
David Greenecebb4ee2012-02-22 16:09:41 +00001694///
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00001695VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenecebb4ee2012-02-22 16:09:41 +00001696 if (Lex.getCode() != tgtok::Id) {
1697 TokError("Expected identifier in foreach declaration");
1698 return 0;
1699 }
1700
1701 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1702 Lex.Lex();
1703
1704 // If a value is present, parse it.
1705 if (Lex.getCode() != tgtok::equal) {
1706 TokError("Expected '=' in foreach declaration");
1707 return 0;
1708 }
1709 Lex.Lex(); // Eat the '='
1710
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001711 RecTy *IterType = 0;
1712 std::vector<unsigned> Ranges;
David Greenecebb4ee2012-02-22 16:09:41 +00001713
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001714 switch (Lex.getCode()) {
1715 default: TokError("Unknown token when expecting a range list"); return 0;
1716 case tgtok::l_square: { // '[' ValueList ']'
1717 Init *List = ParseSimpleValue(0, 0, ParseForeachMode);
Sean Silva6cfc8062012-10-10 20:24:43 +00001718 ForeachListValue = dyn_cast<ListInit>(List);
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001719 if (ForeachListValue == 0) {
1720 TokError("Expected a Value list");
1721 return 0;
1722 }
1723 RecTy *ValueType = ForeachListValue->getType();
Sean Silva736ceac2012-10-05 03:31:58 +00001724 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001725 if (ListType == 0) {
1726 TokError("Value list is not of list type");
1727 return 0;
1728 }
1729 IterType = ListType->getElementType();
1730 break;
David Greenecebb4ee2012-02-22 16:09:41 +00001731 }
1732
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001733 case tgtok::IntVal: { // RangePiece.
1734 if (ParseRangePiece(Ranges))
1735 return 0;
1736 break;
David Greenecebb4ee2012-02-22 16:09:41 +00001737 }
1738
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001739 case tgtok::l_brace: { // '{' RangeList '}'
1740 Lex.Lex(); // eat the '{'
1741 Ranges = ParseRangeList();
1742 if (Lex.getCode() != tgtok::r_brace) {
1743 TokError("expected '}' at end of bit range list");
1744 return 0;
1745 }
1746 Lex.Lex();
1747 break;
1748 }
1749 }
David Greenecebb4ee2012-02-22 16:09:41 +00001750
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001751 if (!Ranges.empty()) {
1752 assert(!IterType && "Type already initialized?");
1753 IterType = IntRecTy::get();
1754 std::vector<Init*> Values;
1755 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
1756 Values.push_back(IntInit::get(Ranges[i]));
1757 ForeachListValue = ListInit::get(Values, IterType);
1758 }
1759
1760 if (!IterType)
1761 return 0;
1762
1763 return VarInit::get(DeclName, IterType);
David Greenecebb4ee2012-02-22 16:09:41 +00001764}
1765
Chris Lattnerf4601652007-11-22 20:49:04 +00001766/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1767/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1768/// template args for a def, which may or may not be in a multiclass. If null,
1769/// these are the template args for a multiclass.
1770///
1771/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson21870412009-11-22 04:24:42 +00001772///
Chris Lattnerf4601652007-11-22 20:49:04 +00001773bool TGParser::ParseTemplateArgList(Record *CurRec) {
1774 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1775 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +00001776
Chris Lattnerf4601652007-11-22 20:49:04 +00001777 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +00001778
Chris Lattnerf4601652007-11-22 20:49:04 +00001779 // Read the first declaration.
David Greenee22b3212011-10-19 13:02:42 +00001780 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1781 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001782 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001783
Chris Lattnerf4601652007-11-22 20:49:04 +00001784 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson21870412009-11-22 04:24:42 +00001785
Chris Lattnerf4601652007-11-22 20:49:04 +00001786 while (Lex.getCode() == tgtok::comma) {
1787 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001788
Chris Lattnerf4601652007-11-22 20:49:04 +00001789 // Read the following declarations.
1790 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
David Greenee22b3212011-10-19 13:02:42 +00001791 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001792 return true;
1793 TheRecToAddTo->addTemplateArg(TemplArg);
1794 }
Bob Wilson21870412009-11-22 04:24:42 +00001795
Chris Lattnerf4601652007-11-22 20:49:04 +00001796 if (Lex.getCode() != tgtok::greater)
1797 return TokError("expected '>' at end of template argument list");
1798 Lex.Lex(); // eat the '>'.
1799 return false;
1800}
1801
1802
1803/// ParseBodyItem - Parse a single item at within the body of a def or class.
1804///
1805/// BodyItem ::= Declaration ';'
1806/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1807bool TGParser::ParseBodyItem(Record *CurRec) {
1808 if (Lex.getCode() != tgtok::Let) {
David Greenee22b3212011-10-19 13:02:42 +00001809 if (ParseDeclaration(CurRec, false) == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001810 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001811
Chris Lattnerf4601652007-11-22 20:49:04 +00001812 if (Lex.getCode() != tgtok::semi)
1813 return TokError("expected ';' after declaration");
1814 Lex.Lex();
1815 return false;
1816 }
1817
1818 // LET ID OptionalRangeList '=' Value ';'
1819 if (Lex.Lex() != tgtok::Id)
1820 return TokError("expected field identifier after let");
Bob Wilson21870412009-11-22 04:24:42 +00001821
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001822 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001823 std::string FieldName = Lex.getCurStrVal();
1824 Lex.Lex(); // eat the field name.
Bob Wilson21870412009-11-22 04:24:42 +00001825
Chris Lattnerf4601652007-11-22 20:49:04 +00001826 std::vector<unsigned> BitList;
Bob Wilson21870412009-11-22 04:24:42 +00001827 if (ParseOptionalBitList(BitList))
Chris Lattnerf4601652007-11-22 20:49:04 +00001828 return true;
1829 std::reverse(BitList.begin(), BitList.end());
Bob Wilson21870412009-11-22 04:24:42 +00001830
Chris Lattnerf4601652007-11-22 20:49:04 +00001831 if (Lex.getCode() != tgtok::equal)
1832 return TokError("expected '=' in let expression");
1833 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001834
David Greenee1b46912009-06-08 20:23:18 +00001835 RecordVal *Field = CurRec->getValue(FieldName);
1836 if (Field == 0)
1837 return TokError("Value '" + FieldName + "' unknown!");
1838
1839 RecTy *Type = Field->getType();
Bob Wilson21870412009-11-22 04:24:42 +00001840
David Greene05bce0b2011-07-29 22:43:06 +00001841 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001842 if (Val == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001843
Chris Lattnerf4601652007-11-22 20:49:04 +00001844 if (Lex.getCode() != tgtok::semi)
1845 return TokError("expected ';' after let expression");
1846 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001847
Chris Lattnerf4601652007-11-22 20:49:04 +00001848 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1849}
1850
1851/// ParseBody - Read the body of a class or def. Return true on error, false on
1852/// success.
1853///
1854/// Body ::= ';'
1855/// Body ::= '{' BodyList '}'
1856/// BodyList BodyItem*
1857///
1858bool TGParser::ParseBody(Record *CurRec) {
1859 // If this is a null definition, just eat the semi and return.
1860 if (Lex.getCode() == tgtok::semi) {
1861 Lex.Lex();
1862 return false;
1863 }
Bob Wilson21870412009-11-22 04:24:42 +00001864
Chris Lattnerf4601652007-11-22 20:49:04 +00001865 if (Lex.getCode() != tgtok::l_brace)
1866 return TokError("Expected ';' or '{' to start body");
1867 // Eat the '{'.
1868 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001869
Chris Lattnerf4601652007-11-22 20:49:04 +00001870 while (Lex.getCode() != tgtok::r_brace)
1871 if (ParseBodyItem(CurRec))
1872 return true;
1873
1874 // Eat the '}'.
1875 Lex.Lex();
1876 return false;
1877}
1878
1879/// ParseObjectBody - Parse the body of a def or class. This consists of an
1880/// optional ClassList followed by a Body. CurRec is the current def or class
1881/// that is being parsed.
1882///
1883/// ObjectBody ::= BaseClassList Body
1884/// BaseClassList ::= /*empty*/
1885/// BaseClassList ::= ':' BaseClassListNE
1886/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1887///
1888bool TGParser::ParseObjectBody(Record *CurRec) {
1889 // If there is a baseclass list, read it.
1890 if (Lex.getCode() == tgtok::colon) {
1891 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001892
Chris Lattnerf4601652007-11-22 20:49:04 +00001893 // Read all of the subclasses.
1894 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1895 while (1) {
1896 // Check for error.
1897 if (SubClass.Rec == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001898
Chris Lattnerf4601652007-11-22 20:49:04 +00001899 // Add it.
1900 if (AddSubClass(CurRec, SubClass))
1901 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001902
Chris Lattnerf4601652007-11-22 20:49:04 +00001903 if (Lex.getCode() != tgtok::comma) break;
1904 Lex.Lex(); // eat ','.
1905 SubClass = ParseSubClassReference(CurRec, false);
1906 }
1907 }
1908
1909 // Process any variables on the let stack.
1910 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1911 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1912 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1913 LetStack[i][j].Bits, LetStack[i][j].Value))
1914 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001915
Chris Lattnerf4601652007-11-22 20:49:04 +00001916 return ParseBody(CurRec);
1917}
1918
Chris Lattnerf4601652007-11-22 20:49:04 +00001919/// ParseDef - Parse and return a top level or multiclass def, return the record
1920/// corresponding to it. This returns null on error.
1921///
1922/// DefInst ::= DEF ObjectName ObjectBody
1923///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001924bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001925 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001926 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson21870412009-11-22 04:24:42 +00001927 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4601652007-11-22 20:49:04 +00001928
1929 // Parse ObjectName and make a record for it.
David Greenea9e07dd2011-10-19 13:04:29 +00001930 Record *CurRec = new Record(ParseObjectName(CurMultiClass), DefLoc, Records);
Bob Wilson21870412009-11-22 04:24:42 +00001931
Jakob Stoklund Olesen72cba6c2012-05-24 22:17:36 +00001932 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001933 // Top-level def definition.
Bob Wilson21870412009-11-22 04:24:42 +00001934
Chris Lattnerf4601652007-11-22 20:49:04 +00001935 // Ensure redefinition doesn't happen.
David Greene1d501392012-01-28 00:03:24 +00001936 if (Records.getDef(CurRec->getNameInitAsString())) {
David Greene2c49fbb2011-10-19 13:03:45 +00001937 Error(DefLoc, "def '" + CurRec->getNameInitAsString()
1938 + "' already defined");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001939 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001940 }
1941 Records.addDef(CurRec);
Jakob Stoklund Olesen72cba6c2012-05-24 22:17:36 +00001942 } else if (CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001943 // Otherwise, a def inside a multiclass, add it to the multiclass.
1944 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene91919cd2011-10-19 13:03:51 +00001945 if (CurMultiClass->DefPrototypes[i]->getNameInit()
1946 == CurRec->getNameInit()) {
1947 Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
Chris Lattnerf4601652007-11-22 20:49:04 +00001948 "' already defined in this multiclass!");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001949 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001950 }
1951 CurMultiClass->DefPrototypes.push_back(CurRec);
1952 }
Bob Wilson21870412009-11-22 04:24:42 +00001953
Chris Lattnerf4601652007-11-22 20:49:04 +00001954 if (ParseObjectBody(CurRec))
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001955 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001956
Chris Lattnerf4601652007-11-22 20:49:04 +00001957 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
David Greene0d886402011-08-10 18:27:46 +00001958 // See Record::setName(). This resolve step will see any new name
1959 // for the def that might have been created when resolving
1960 // inheritance, values and arguments above.
Chris Lattnerf4601652007-11-22 20:49:04 +00001961 CurRec->resolveReferences();
Bob Wilson21870412009-11-22 04:24:42 +00001962
Chris Lattnerf4601652007-11-22 20:49:04 +00001963 // If ObjectBody has template arguments, it's an error.
1964 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001965
1966 if (CurMultiClass) {
1967 // Copy the template arguments for the multiclass into the def.
David Greenee22b3212011-10-19 13:02:42 +00001968 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001969 CurMultiClass->Rec.getTemplateArgs();
1970
1971 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1972 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1973 assert(RV && "Template arg doesn't exist?");
1974 CurRec->addValue(*RV);
1975 }
1976 }
1977
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00001978 if (ProcessForeachDefs(CurRec, DefLoc)) {
David Greenecebb4ee2012-02-22 16:09:41 +00001979 Error(DefLoc,
1980 "Could not process loops for def" + CurRec->getNameInitAsString());
1981 return true;
1982 }
1983
1984 return false;
1985}
1986
1987/// ParseForeach - Parse a for statement. Return the record corresponding
1988/// to it. This returns true on error.
1989///
1990/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
1991/// Foreach ::= FOREACH Declaration IN Object
1992///
1993bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
1994 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
1995 Lex.Lex(); // Eat the 'for' token.
1996
1997 // Make a temporary object to record items associated with the for
1998 // loop.
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00001999 ListInit *ListValue = 0;
2000 VarInit *IterName = ParseForeachDeclaration(ListValue);
David Greenecebb4ee2012-02-22 16:09:41 +00002001 if (IterName == 0)
2002 return TokError("expected declaration in for");
2003
2004 if (Lex.getCode() != tgtok::In)
2005 return TokError("Unknown tok");
2006 Lex.Lex(); // Eat the in
2007
2008 // Create a loop object and remember it.
2009 Loops.push_back(ForeachLoop(IterName, ListValue));
2010
2011 if (Lex.getCode() != tgtok::l_brace) {
2012 // FOREACH Declaration IN Object
2013 if (ParseObject(CurMultiClass))
2014 return true;
2015 }
2016 else {
2017 SMLoc BraceLoc = Lex.getLoc();
2018 // Otherwise, this is a group foreach.
2019 Lex.Lex(); // eat the '{'.
2020
2021 // Parse the object list.
2022 if (ParseObjectList(CurMultiClass))
2023 return true;
2024
2025 if (Lex.getCode() != tgtok::r_brace) {
2026 TokError("expected '}' at end of foreach command");
2027 return Error(BraceLoc, "to match this '{'");
2028 }
2029 Lex.Lex(); // Eat the }
2030 }
2031
2032 // We've processed everything in this loop.
2033 Loops.pop_back();
2034
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002035 return false;
Chris Lattnerf4601652007-11-22 20:49:04 +00002036}
2037
Chris Lattnerf4601652007-11-22 20:49:04 +00002038/// ParseClass - Parse a tblgen class definition.
2039///
2040/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2041///
2042bool TGParser::ParseClass() {
2043 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2044 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002045
Chris Lattnerf4601652007-11-22 20:49:04 +00002046 if (Lex.getCode() != tgtok::Id)
2047 return TokError("expected class name after 'class' keyword");
Bob Wilson21870412009-11-22 04:24:42 +00002048
Chris Lattnerf4601652007-11-22 20:49:04 +00002049 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2050 if (CurRec) {
2051 // If the body was previously defined, this is an error.
David Greenee3385652011-10-19 13:04:13 +00002052 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4601652007-11-22 20:49:04 +00002053 !CurRec->getSuperClasses().empty() ||
2054 !CurRec->getTemplateArgs().empty())
David Greene69a23942011-10-19 13:03:58 +00002055 return TokError("Class '" + CurRec->getNameInitAsString()
2056 + "' already defined");
Chris Lattnerf4601652007-11-22 20:49:04 +00002057 } else {
2058 // If this is the first reference to this class, create and add it.
Chris Lattner9c6b60e2010-12-15 04:48:22 +00002059 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00002060 Records.addClass(CurRec);
2061 }
2062 Lex.Lex(); // eat the name.
Bob Wilson21870412009-11-22 04:24:42 +00002063
Chris Lattnerf4601652007-11-22 20:49:04 +00002064 // If there are template args, parse them.
2065 if (Lex.getCode() == tgtok::less)
2066 if (ParseTemplateArgList(CurRec))
2067 return true;
2068
2069 // Finally, parse the object body.
2070 return ParseObjectBody(CurRec);
2071}
2072
2073/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2074/// of LetRecords.
2075///
2076/// LetList ::= LetItem (',' LetItem)*
2077/// LetItem ::= ID OptionalRangeList '=' Value
2078///
2079std::vector<LetRecord> TGParser::ParseLetList() {
2080 std::vector<LetRecord> Result;
Bob Wilson21870412009-11-22 04:24:42 +00002081
Chris Lattnerf4601652007-11-22 20:49:04 +00002082 while (1) {
2083 if (Lex.getCode() != tgtok::Id) {
2084 TokError("expected identifier in let definition");
2085 return std::vector<LetRecord>();
2086 }
2087 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002088 SMLoc NameLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +00002089 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4601652007-11-22 20:49:04 +00002090
2091 // Check for an optional RangeList.
2092 std::vector<unsigned> Bits;
Bob Wilson21870412009-11-22 04:24:42 +00002093 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4601652007-11-22 20:49:04 +00002094 return std::vector<LetRecord>();
2095 std::reverse(Bits.begin(), Bits.end());
Bob Wilson21870412009-11-22 04:24:42 +00002096
Chris Lattnerf4601652007-11-22 20:49:04 +00002097 if (Lex.getCode() != tgtok::equal) {
2098 TokError("expected '=' in let expression");
2099 return std::vector<LetRecord>();
2100 }
2101 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00002102
David Greene05bce0b2011-07-29 22:43:06 +00002103 Init *Val = ParseValue(0);
Chris Lattnerf4601652007-11-22 20:49:04 +00002104 if (Val == 0) return std::vector<LetRecord>();
Bob Wilson21870412009-11-22 04:24:42 +00002105
Chris Lattnerf4601652007-11-22 20:49:04 +00002106 // Now that we have everything, add the record.
2107 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson21870412009-11-22 04:24:42 +00002108
Chris Lattnerf4601652007-11-22 20:49:04 +00002109 if (Lex.getCode() != tgtok::comma)
2110 return Result;
Bob Wilson21870412009-11-22 04:24:42 +00002111 Lex.Lex(); // eat the comma.
Chris Lattnerf4601652007-11-22 20:49:04 +00002112 }
2113}
2114
2115/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002116/// different related productions. This works inside multiclasses too.
Chris Lattnerf4601652007-11-22 20:49:04 +00002117///
2118/// Object ::= LET LetList IN '{' ObjectList '}'
2119/// Object ::= LET LetList IN Object
2120///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002121bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002122 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2123 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002124
Chris Lattnerf4601652007-11-22 20:49:04 +00002125 // Add this entry to the let stack.
2126 std::vector<LetRecord> LetInfo = ParseLetList();
2127 if (LetInfo.empty()) return true;
2128 LetStack.push_back(LetInfo);
2129
2130 if (Lex.getCode() != tgtok::In)
2131 return TokError("expected 'in' at end of top-level 'let'");
2132 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002133
Chris Lattnerf4601652007-11-22 20:49:04 +00002134 // If this is a scalar let, just handle it now
2135 if (Lex.getCode() != tgtok::l_brace) {
2136 // LET LetList IN Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002137 if (ParseObject(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00002138 return true;
2139 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002140 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002141 // Otherwise, this is a group let.
2142 Lex.Lex(); // eat the '{'.
Bob Wilson21870412009-11-22 04:24:42 +00002143
Chris Lattnerf4601652007-11-22 20:49:04 +00002144 // Parse the object list.
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002145 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00002146 return true;
Bob Wilson21870412009-11-22 04:24:42 +00002147
Chris Lattnerf4601652007-11-22 20:49:04 +00002148 if (Lex.getCode() != tgtok::r_brace) {
2149 TokError("expected '}' at end of top level let command");
2150 return Error(BraceLoc, "to match this '{'");
2151 }
2152 Lex.Lex();
2153 }
Bob Wilson21870412009-11-22 04:24:42 +00002154
Chris Lattnerf4601652007-11-22 20:49:04 +00002155 // Outside this let scope, this let block is not active.
2156 LetStack.pop_back();
2157 return false;
2158}
2159
Chris Lattnerf4601652007-11-22 20:49:04 +00002160/// ParseMultiClass - Parse a multiclass definition.
2161///
Bob Wilson32558652009-04-28 19:41:44 +00002162/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
2163/// ':' BaseMultiClassList '{' MultiClassDef+ '}'
Chris Lattnerf4601652007-11-22 20:49:04 +00002164///
2165bool TGParser::ParseMultiClass() {
2166 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2167 Lex.Lex(); // Eat the multiclass token.
2168
2169 if (Lex.getCode() != tgtok::Id)
2170 return TokError("expected identifier after multiclass for name");
2171 std::string Name = Lex.getCurStrVal();
Bob Wilson21870412009-11-22 04:24:42 +00002172
Chris Lattnerf4601652007-11-22 20:49:04 +00002173 if (MultiClasses.count(Name))
2174 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson21870412009-11-22 04:24:42 +00002175
Chris Lattner67db8832010-12-13 00:23:57 +00002176 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
2177 Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00002178 Lex.Lex(); // Eat the identifier.
Bob Wilson21870412009-11-22 04:24:42 +00002179
Chris Lattnerf4601652007-11-22 20:49:04 +00002180 // If there are template args, parse them.
2181 if (Lex.getCode() == tgtok::less)
2182 if (ParseTemplateArgList(0))
2183 return true;
2184
David Greened34a73b2009-04-24 16:55:41 +00002185 bool inherits = false;
2186
David Greenede444af2009-04-22 16:42:54 +00002187 // If there are submulticlasses, parse them.
2188 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00002189 inherits = true;
2190
David Greenede444af2009-04-22 16:42:54 +00002191 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00002192
David Greenede444af2009-04-22 16:42:54 +00002193 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00002194 SubMultiClassReference SubMultiClass =
2195 ParseSubMultiClassReference(CurMultiClass);
David Greenede444af2009-04-22 16:42:54 +00002196 while (1) {
2197 // Check for error.
2198 if (SubMultiClass.MC == 0) return true;
Bob Wilson32558652009-04-28 19:41:44 +00002199
David Greenede444af2009-04-22 16:42:54 +00002200 // Add it.
2201 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2202 return true;
Bob Wilson32558652009-04-28 19:41:44 +00002203
David Greenede444af2009-04-22 16:42:54 +00002204 if (Lex.getCode() != tgtok::comma) break;
2205 Lex.Lex(); // eat ','.
2206 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2207 }
2208 }
2209
David Greened34a73b2009-04-24 16:55:41 +00002210 if (Lex.getCode() != tgtok::l_brace) {
2211 if (!inherits)
2212 return TokError("expected '{' in multiclass definition");
Bob Wilson21870412009-11-22 04:24:42 +00002213 else if (Lex.getCode() != tgtok::semi)
2214 return TokError("expected ';' in multiclass definition");
David Greened34a73b2009-04-24 16:55:41 +00002215 else
Bob Wilson21870412009-11-22 04:24:42 +00002216 Lex.Lex(); // eat the ';'.
2217 } else {
David Greened34a73b2009-04-24 16:55:41 +00002218 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2219 return TokError("multiclass must contain at least one def");
Bob Wilson21870412009-11-22 04:24:42 +00002220
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002221 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002222 switch (Lex.getCode()) {
2223 default:
David Greenea1b1b792011-10-07 18:25:05 +00002224 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002225 case tgtok::Let:
2226 case tgtok::Def:
2227 case tgtok::Defm:
David Greenecebb4ee2012-02-22 16:09:41 +00002228 case tgtok::Foreach:
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002229 if (ParseObject(CurMultiClass))
2230 return true;
2231 break;
2232 }
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002233 }
David Greened34a73b2009-04-24 16:55:41 +00002234 Lex.Lex(); // eat the '}'.
2235 }
Bob Wilson21870412009-11-22 04:24:42 +00002236
Chris Lattnerf4601652007-11-22 20:49:04 +00002237 CurMultiClass = 0;
2238 return false;
2239}
2240
David Greenee499a2d2011-10-05 22:42:07 +00002241Record *TGParser::
2242InstantiateMulticlassDef(MultiClass &MC,
2243 Record *DefProto,
David Greene7be867e2011-10-19 13:04:31 +00002244 Init *DefmPrefix,
David Greenee499a2d2011-10-05 22:42:07 +00002245 SMLoc DefmPrefixLoc) {
David Greene7be867e2011-10-19 13:04:31 +00002246 // We need to preserve DefProto so it can be reused for later
2247 // instantiations, so create a new Record to inherit from it.
2248
David Greenee499a2d2011-10-05 22:42:07 +00002249 // Add in the defm name. If the defm prefix is empty, give each
2250 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2251 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2252 // as a prefix.
David Greenee499a2d2011-10-05 22:42:07 +00002253
David Greene7be867e2011-10-19 13:04:31 +00002254 if (DefmPrefix == 0)
2255 DefmPrefix = StringInit::get(GetNewAnonymousName());
2256
2257 Init *DefName = DefProto->getNameInit();
2258
Sean Silva6cfc8062012-10-10 20:24:43 +00002259 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene7be867e2011-10-19 13:04:31 +00002260
David Greened3d1cad2011-10-19 13:04:43 +00002261 if (DefNameString != 0) {
2262 // We have a fully expanded string so there are no operators to
2263 // resolve. We should concatenate the given prefix and name.
David Greene7be867e2011-10-19 13:04:31 +00002264 DefName =
2265 BinOpInit::get(BinOpInit::STRCONCAT,
2266 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2267 StringRecTy::get())->Fold(DefProto, &MC),
2268 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2269 }
David Greene7be867e2011-10-19 13:04:31 +00002270
Jakob Stoklund Olesen376a8a72012-08-22 23:33:58 +00002271 // Make a trail of SMLocs from the multiclass instantiations.
2272 SmallVector<SMLoc, 4> Locs(1, DefmPrefixLoc);
2273 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
2274 Record *CurRec = new Record(DefName, Locs, Records);
David Greenee499a2d2011-10-05 22:42:07 +00002275
2276 SubClassReference Ref;
2277 Ref.RefLoc = DefmPrefixLoc;
2278 Ref.Rec = DefProto;
2279 AddSubClass(CurRec, Ref);
2280
Jim Grosbachcfbda4a2012-08-02 18:46:42 +00002281 // Set the value for NAME. We don't resolve references to it 'til later,
2282 // though, so that uses in nested multiclass names don't get
2283 // confused.
2284 if (SetValue(CurRec, Ref.RefLoc, "NAME", std::vector<unsigned>(),
2285 DefmPrefix)) {
2286 Error(DefmPrefixLoc, "Could not resolve "
2287 + CurRec->getNameInitAsString() + ":NAME to '"
2288 + DefmPrefix->getAsUnquotedString() + "'");
2289 return 0;
2290 }
David Greenee5b252f2011-10-19 13:04:35 +00002291
Jim Grosbachcfbda4a2012-08-02 18:46:42 +00002292 // If the DefNameString didn't resolve, we probably have a reference to
2293 // NAME and need to replace it. We need to do at least this much greedily,
2294 // otherwise nested multiclasses will end up with incorrect NAME expansions.
2295 if (DefNameString == 0) {
David Greenee5b252f2011-10-19 13:04:35 +00002296 RecordVal *DefNameRV = CurRec->getValue("NAME");
2297 CurRec->resolveReferencesTo(DefNameRV);
2298 }
2299
2300 if (!CurMultiClass) {
Jim Grosbachcfbda4a2012-08-02 18:46:42 +00002301 // Now that we're at the top level, resolve all NAME references
2302 // in the resultant defs that weren't in the def names themselves.
2303 RecordVal *DefNameRV = CurRec->getValue("NAME");
2304 CurRec->resolveReferencesTo(DefNameRV);
2305
2306 // Now that NAME references are resolved and we're at the top level of
2307 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greenee5b252f2011-10-19 13:04:35 +00002308 // currently in a multiclass, it means this defm appears inside a
2309 // multiclass and its name won't be fully resolvable until we see
2310 // the top-level defm. Therefore, we don't add this to the
2311 // RecordKeeper at this point. If we did we could get duplicate
2312 // defs as more than one probably refers to NAME or some other
2313 // common internal placeholder.
2314
2315 // Ensure redefinition doesn't happen.
2316 if (Records.getDef(CurRec->getNameInitAsString())) {
2317 Error(DefmPrefixLoc, "def '" + CurRec->getNameInitAsString() +
2318 "' already defined, instantiating defm with subdef '" +
2319 DefProto->getNameInitAsString() + "'");
2320 return 0;
2321 }
2322
2323 Records.addDef(CurRec);
2324 }
2325
David Greenee499a2d2011-10-05 22:42:07 +00002326 return CurRec;
2327}
2328
2329bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2330 Record *CurRec,
2331 SMLoc DefmPrefixLoc,
2332 SMLoc SubClassLoc,
David Greenee22b3212011-10-19 13:02:42 +00002333 const std::vector<Init *> &TArgs,
David Greenee499a2d2011-10-05 22:42:07 +00002334 std::vector<Init *> &TemplateVals,
2335 bool DeleteArgs) {
2336 // Loop over all of the template arguments, setting them to the specified
2337 // value or leaving them as the default if necessary.
2338 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2339 // Check if a value is specified for this temp-arg.
2340 if (i < TemplateVals.size()) {
2341 // Set it now.
2342 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2343 TemplateVals[i]))
2344 return true;
2345
2346 // Resolve it next.
2347 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2348
2349 if (DeleteArgs)
2350 // Now remove it.
2351 CurRec->removeValue(TArgs[i]);
2352
2353 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2354 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenee22b3212011-10-19 13:02:42 +00002355 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2356 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2357 + "'");
David Greenee499a2d2011-10-05 22:42:07 +00002358 }
2359 }
2360 return false;
2361}
2362
2363bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2364 Record *CurRec,
2365 Record *DefProto,
2366 SMLoc DefmPrefixLoc) {
2367 // If the mdef is inside a 'let' expression, add to each def.
2368 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2369 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2370 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2371 LetStack[i][j].Bits, LetStack[i][j].Value))
2372 return Error(DefmPrefixLoc, "when instantiating this defm");
2373
David Greenee499a2d2011-10-05 22:42:07 +00002374 // Don't create a top level definition for defm inside multiclasses,
2375 // instead, only update the prototypes and bind the template args
2376 // with the new created definition.
2377 if (CurMultiClass) {
2378 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2379 i != e; ++i)
David Greene22dde7e2011-10-19 13:04:02 +00002380 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2381 == CurRec->getNameInit())
2382 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
David Greenee499a2d2011-10-05 22:42:07 +00002383 "' already defined in this multiclass!");
2384 CurMultiClass->DefPrototypes.push_back(CurRec);
2385
2386 // Copy the template arguments for the multiclass into the new def.
David Greenee22b3212011-10-19 13:02:42 +00002387 const std::vector<Init *> &TA =
David Greenee499a2d2011-10-05 22:42:07 +00002388 CurMultiClass->Rec.getTemplateArgs();
2389
2390 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2391 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2392 assert(RV && "Template arg doesn't exist?");
2393 CurRec->addValue(*RV);
2394 }
David Greenee499a2d2011-10-05 22:42:07 +00002395 }
2396
2397 return false;
2398}
2399
Chris Lattnerf4601652007-11-22 20:49:04 +00002400/// ParseDefm - Parse the instantiation of a multiclass.
2401///
2402/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2403///
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002404bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002405 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Bob Wilson21870412009-11-22 04:24:42 +00002406
David Greenea9e07dd2011-10-19 13:04:29 +00002407 Init *DefmPrefix = 0;
David Greenea9e07dd2011-10-19 13:04:29 +00002408
Craig Topper6a59f5a2013-01-07 05:09:33 +00002409 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greenea9e07dd2011-10-19 13:04:29 +00002410 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002411 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +00002412
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002413 SMLoc DefmPrefixLoc = Lex.getLoc();
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002414 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4601652007-11-22 20:49:04 +00002415 return TokError("expected ':' after defm identifier");
Bob Wilson21870412009-11-22 04:24:42 +00002416
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002417 // Keep track of the new generated record definitions.
2418 std::vector<Record*> NewRecDefs;
2419
2420 // This record also inherits from a regular class (non-multiclass)?
2421 bool InheritFromClass = false;
2422
Chris Lattnerf4601652007-11-22 20:49:04 +00002423 // eat the colon.
2424 Lex.Lex();
2425
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002426 SMLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002427 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene56546132009-04-22 22:17:51 +00002428
2429 while (1) {
2430 if (Ref.Rec == 0) return true;
2431
2432 // To instantiate a multiclass, we need to first get the multiclass, then
2433 // instantiate each def contained in the multiclass with the SubClassRef
2434 // template parameters.
2435 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2436 assert(MC && "Didn't lookup multiclass correctly?");
David Greene05bce0b2011-07-29 22:43:06 +00002437 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greene56546132009-04-22 22:17:51 +00002438
2439 // Verify that the correct number of template arguments were specified.
David Greenee22b3212011-10-19 13:02:42 +00002440 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greene56546132009-04-22 22:17:51 +00002441 if (TArgs.size() < TemplateVals.size())
2442 return Error(SubClassLoc,
2443 "more template args specified than multiclass expects");
2444
2445 // Loop over all the def's in the multiclass, instantiating each one.
2446 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2447 Record *DefProto = MC->DefPrototypes[i];
2448
David Greene7be867e2011-10-19 13:04:31 +00002449 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix, DefmPrefixLoc);
Jim Grosbach94f2dc92011-12-02 18:33:03 +00002450 if (!CurRec)
2451 return true;
David Greene065f2592009-05-05 16:28:25 +00002452
David Greenee499a2d2011-10-05 22:42:07 +00002453 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmPrefixLoc, SubClassLoc,
2454 TArgs, TemplateVals, true/*Delete args*/))
2455 return Error(SubClassLoc, "could not instantiate def");
David Greene56546132009-04-22 22:17:51 +00002456
David Greenee499a2d2011-10-05 22:42:07 +00002457 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmPrefixLoc))
2458 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002459
2460 NewRecDefs.push_back(CurRec);
David Greene56546132009-04-22 22:17:51 +00002461 }
2462
David Greenee499a2d2011-10-05 22:42:07 +00002463
David Greene56546132009-04-22 22:17:51 +00002464 if (Lex.getCode() != tgtok::comma) break;
2465 Lex.Lex(); // eat ','.
2466
2467 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002468
2469 // A defm can inherit from regular classes (non-multiclass) as
2470 // long as they come in the end of the inheritance list.
2471 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0);
2472
2473 if (InheritFromClass)
2474 break;
2475
David Greene56546132009-04-22 22:17:51 +00002476 Ref = ParseSubClassReference(0, true);
2477 }
2478
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002479 if (InheritFromClass) {
2480 // Process all the classes to inherit as if they were part of a
2481 // regular 'def' and inherit all record values.
2482 SubClassReference SubClass = ParseSubClassReference(0, false);
2483 while (1) {
2484 // Check for error.
2485 if (SubClass.Rec == 0) return true;
2486
2487 // Get the expanded definition prototypes and teach them about
2488 // the record values the current class to inherit has
2489 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2490 Record *CurRec = NewRecDefs[i];
2491
2492 // Add it.
2493 if (AddSubClass(CurRec, SubClass))
2494 return true;
2495
2496 // Process any variables on the let stack.
2497 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2498 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2499 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2500 LetStack[i][j].Bits, LetStack[i][j].Value))
2501 return true;
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002502 }
2503
2504 if (Lex.getCode() != tgtok::comma) break;
2505 Lex.Lex(); // eat ','.
2506 SubClass = ParseSubClassReference(0, false);
2507 }
2508 }
2509
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002510 if (!CurMultiClass)
2511 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene0d886402011-08-10 18:27:46 +00002512 // See Record::setName(). This resolve step will see any new
2513 // name for the def that might have been created when resolving
2514 // inheritance, values and arguments above.
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002515 NewRecDefs[i]->resolveReferences();
2516
Chris Lattnerf4601652007-11-22 20:49:04 +00002517 if (Lex.getCode() != tgtok::semi)
2518 return TokError("expected ';' at end of defm");
2519 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002520
Chris Lattnerf4601652007-11-22 20:49:04 +00002521 return false;
2522}
2523
2524/// ParseObject
2525/// Object ::= ClassInst
2526/// Object ::= DefInst
2527/// Object ::= MultiClassInst
2528/// Object ::= DefMInst
2529/// Object ::= LETCommand '{' ObjectList '}'
2530/// Object ::= LETCommand Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002531bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002532 switch (Lex.getCode()) {
Chris Lattnerd6d9dd92010-10-31 19:27:15 +00002533 default:
2534 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002535 case tgtok::Let: return ParseTopLevelLet(MC);
2536 case tgtok::Def: return ParseDef(MC);
David Greenecebb4ee2012-02-22 16:09:41 +00002537 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002538 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4601652007-11-22 20:49:04 +00002539 case tgtok::Class: return ParseClass();
2540 case tgtok::MultiClass: return ParseMultiClass();
2541 }
2542}
2543
2544/// ParseObjectList
2545/// ObjectList :== Object*
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002546bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002547 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002548 if (ParseObject(MC))
Chris Lattnerf4601652007-11-22 20:49:04 +00002549 return true;
2550 }
2551 return false;
2552}
2553
Chris Lattnerf4601652007-11-22 20:49:04 +00002554bool TGParser::ParseFile() {
2555 Lex.Lex(); // Prime the lexer.
2556 if (ParseObjectList()) return true;
Bob Wilson21870412009-11-22 04:24:42 +00002557
Chris Lattnerf4601652007-11-22 20:49:04 +00002558 // If we have unread input at the end of the file, report it.
2559 if (Lex.getCode() == tgtok::Eof)
2560 return false;
Bob Wilson21870412009-11-22 04:24:42 +00002561
Chris Lattnerf4601652007-11-22 20:49:04 +00002562 return TokError("Unexpected input at top level");
2563}
2564