blob: 1b1c18b589ac9b00663b38db6e238a9acb2e42c6 [file] [log] [blame]
Chris Lattnerf4601652007-11-22 20:49:04 +00001//===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf4601652007-11-22 20:49:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Parser for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
14#include "TGParser.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000015#include "llvm/TableGen/Record.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000016#include "llvm/ADT/StringExtras.h"
Daniel Dunbar1a551802009-07-03 00:10:29 +000017#include <algorithm>
18#include <sstream>
Chris Lattner8d978a72010-10-05 23:58:18 +000019#include "llvm/ADT/SmallVector.h"
Chris Lattner46f55522010-10-06 04:55:48 +000020#include "llvm/Support/CommandLine.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000021using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24// Support Code for the Semantic Actions.
25//===----------------------------------------------------------------------===//
26
27namespace llvm {
Chris Lattnerf4601652007-11-22 20:49:04 +000028struct SubClassReference {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000029 SMLoc RefLoc;
Chris Lattnerf4601652007-11-22 20:49:04 +000030 Record *Rec;
David Greene05bce0b2011-07-29 22:43:06 +000031 std::vector<Init*> TemplateArgs;
Chris Lattner1c8ae592009-03-13 16:01:53 +000032 SubClassReference() : Rec(0) {}
David Greened34a73b2009-04-24 16:55:41 +000033
Chris Lattnerf4601652007-11-22 20:49:04 +000034 bool isInvalid() const { return Rec == 0; }
35};
David Greenede444af2009-04-22 16:42:54 +000036
37struct SubMultiClassReference {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000038 SMLoc RefLoc;
David Greenede444af2009-04-22 16:42:54 +000039 MultiClass *MC;
David Greene05bce0b2011-07-29 22:43:06 +000040 std::vector<Init*> TemplateArgs;
David Greenede444af2009-04-22 16:42:54 +000041 SubMultiClassReference() : MC(0) {}
Bob Wilson32558652009-04-28 19:41:44 +000042
David Greenede444af2009-04-22 16:42:54 +000043 bool isInvalid() const { return MC == 0; }
David Greened34a73b2009-04-24 16:55:41 +000044 void dump() const;
David Greenede444af2009-04-22 16:42:54 +000045};
David Greened34a73b2009-04-24 16:55:41 +000046
47void SubMultiClassReference::dump() const {
Daniel Dunbar1a551802009-07-03 00:10:29 +000048 errs() << "Multiclass:\n";
Bob Wilson21870412009-11-22 04:24:42 +000049
David Greened34a73b2009-04-24 16:55:41 +000050 MC->dump();
Bob Wilson21870412009-11-22 04:24:42 +000051
Daniel Dunbar1a551802009-07-03 00:10:29 +000052 errs() << "Template args:\n";
David Greene05bce0b2011-07-29 22:43:06 +000053 for (std::vector<Init *>::const_iterator i = TemplateArgs.begin(),
David Greened34a73b2009-04-24 16:55:41 +000054 iend = TemplateArgs.end();
55 i != iend;
56 ++i) {
57 (*i)->dump();
58 }
59}
60
Chris Lattnerf4601652007-11-22 20:49:04 +000061} // end namespace llvm
62
Chris Lattner1e3a8a42009-06-21 03:39:35 +000063bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Chris Lattnerf4601652007-11-22 20:49:04 +000064 if (CurRec == 0)
65 CurRec = &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +000066
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) {
409 const TypedInit *CurRecName =
Sean Silva6cfc8062012-10-10 20:24:43 +0000410 dyn_cast<TypedInit>(CurRec->getNameInit());
David Greenea9e07dd2011-10-19 13:04:29 +0000411 if (!CurRecName) {
412 TokError("Record name is not typed!");
413 return 0;
414 }
415 Type = CurRecName->getType();
416 }
417
418 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4601652007-11-22 20:49:04 +0000419}
420
Chris Lattnerf4601652007-11-22 20:49:04 +0000421/// ParseClassID - Parse and resolve a reference to a class name. This returns
422/// null on error.
423///
424/// ClassID ::= ID
425///
426Record *TGParser::ParseClassID() {
427 if (Lex.getCode() != tgtok::Id) {
428 TokError("expected name for ClassID");
429 return 0;
430 }
Bob Wilson21870412009-11-22 04:24:42 +0000431
Chris Lattnerf4601652007-11-22 20:49:04 +0000432 Record *Result = Records.getClass(Lex.getCurStrVal());
433 if (Result == 0)
434 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson21870412009-11-22 04:24:42 +0000435
Chris Lattnerf4601652007-11-22 20:49:04 +0000436 Lex.Lex();
437 return Result;
438}
439
Bob Wilson32558652009-04-28 19:41:44 +0000440/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
441/// This returns null on error.
David Greenede444af2009-04-22 16:42:54 +0000442///
443/// MultiClassID ::= ID
444///
445MultiClass *TGParser::ParseMultiClassID() {
446 if (Lex.getCode() != tgtok::Id) {
447 TokError("expected name for ClassID");
448 return 0;
449 }
Bob Wilson32558652009-04-28 19:41:44 +0000450
David Greenede444af2009-04-22 16:42:54 +0000451 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
452 if (Result == 0)
453 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson32558652009-04-28 19:41:44 +0000454
David Greenede444af2009-04-22 16:42:54 +0000455 Lex.Lex();
456 return Result;
457}
458
Chris Lattnerf4601652007-11-22 20:49:04 +0000459Record *TGParser::ParseDefmID() {
460 if (Lex.getCode() != tgtok::Id) {
461 TokError("expected multiclass name");
462 return 0;
463 }
Bob Wilson21870412009-11-22 04:24:42 +0000464
Chris Lattnerf4601652007-11-22 20:49:04 +0000465 MultiClass *MC = MultiClasses[Lex.getCurStrVal()];
466 if (MC == 0) {
467 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
468 return 0;
469 }
Bob Wilson21870412009-11-22 04:24:42 +0000470
Chris Lattnerf4601652007-11-22 20:49:04 +0000471 Lex.Lex();
472 return &MC->Rec;
Bob Wilson21870412009-11-22 04:24:42 +0000473}
Chris Lattnerf4601652007-11-22 20:49:04 +0000474
475
476/// ParseSubClassReference - Parse a reference to a subclass or to a templated
477/// subclass. This returns a SubClassRefTy with a null Record* on error.
478///
479/// SubClassRef ::= ClassID
480/// SubClassRef ::= ClassID '<' ValueList '>'
481///
482SubClassReference TGParser::
483ParseSubClassReference(Record *CurRec, bool isDefm) {
484 SubClassReference Result;
485 Result.RefLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +0000486
Chris Lattnerf4601652007-11-22 20:49:04 +0000487 if (isDefm)
488 Result.Rec = ParseDefmID();
489 else
490 Result.Rec = ParseClassID();
491 if (Result.Rec == 0) return Result;
Bob Wilson21870412009-11-22 04:24:42 +0000492
Chris Lattnerf4601652007-11-22 20:49:04 +0000493 // If there is no template arg list, we're done.
494 if (Lex.getCode() != tgtok::less)
495 return Result;
496 Lex.Lex(); // Eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000497
Chris Lattnerf4601652007-11-22 20:49:04 +0000498 if (Lex.getCode() == tgtok::greater) {
499 TokError("subclass reference requires a non-empty list of template values");
500 Result.Rec = 0;
501 return Result;
502 }
Bob Wilson21870412009-11-22 04:24:42 +0000503
David Greenee1b46912009-06-08 20:23:18 +0000504 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4601652007-11-22 20:49:04 +0000505 if (Result.TemplateArgs.empty()) {
506 Result.Rec = 0; // Error parsing value list.
507 return Result;
508 }
Bob Wilson21870412009-11-22 04:24:42 +0000509
Chris Lattnerf4601652007-11-22 20:49:04 +0000510 if (Lex.getCode() != tgtok::greater) {
511 TokError("expected '>' in template value list");
512 Result.Rec = 0;
513 return Result;
514 }
515 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000516
Chris Lattnerf4601652007-11-22 20:49:04 +0000517 return Result;
518}
519
Bob Wilson32558652009-04-28 19:41:44 +0000520/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
521/// templated submulticlass. This returns a SubMultiClassRefTy with a null
522/// Record* on error.
David Greenede444af2009-04-22 16:42:54 +0000523///
524/// SubMultiClassRef ::= MultiClassID
525/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
526///
527SubMultiClassReference TGParser::
528ParseSubMultiClassReference(MultiClass *CurMC) {
529 SubMultiClassReference Result;
530 Result.RefLoc = Lex.getLoc();
Bob Wilson32558652009-04-28 19:41:44 +0000531
David Greenede444af2009-04-22 16:42:54 +0000532 Result.MC = ParseMultiClassID();
533 if (Result.MC == 0) return Result;
Bob Wilson32558652009-04-28 19:41:44 +0000534
David Greenede444af2009-04-22 16:42:54 +0000535 // If there is no template arg list, we're done.
536 if (Lex.getCode() != tgtok::less)
537 return Result;
538 Lex.Lex(); // Eat the '<'
Bob Wilson32558652009-04-28 19:41:44 +0000539
David Greenede444af2009-04-22 16:42:54 +0000540 if (Lex.getCode() == tgtok::greater) {
541 TokError("subclass reference requires a non-empty list of template values");
542 Result.MC = 0;
543 return Result;
544 }
Bob Wilson32558652009-04-28 19:41:44 +0000545
David Greenee1b46912009-06-08 20:23:18 +0000546 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greenede444af2009-04-22 16:42:54 +0000547 if (Result.TemplateArgs.empty()) {
548 Result.MC = 0; // Error parsing value list.
549 return Result;
550 }
Bob Wilson32558652009-04-28 19:41:44 +0000551
David Greenede444af2009-04-22 16:42:54 +0000552 if (Lex.getCode() != tgtok::greater) {
553 TokError("expected '>' in template value list");
554 Result.MC = 0;
555 return Result;
556 }
557 Lex.Lex();
558
559 return Result;
560}
561
Chris Lattnerf4601652007-11-22 20:49:04 +0000562/// ParseRangePiece - Parse a bit/value range.
563/// RangePiece ::= INTVAL
564/// RangePiece ::= INTVAL '-' INTVAL
565/// RangePiece ::= INTVAL INTVAL
566bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner811281e2008-01-10 07:01:53 +0000567 if (Lex.getCode() != tgtok::IntVal) {
568 TokError("expected integer or bitrange");
569 return true;
570 }
Dan Gohman63f97202008-10-17 01:33:43 +0000571 int64_t Start = Lex.getCurIntVal();
572 int64_t End;
Bob Wilson21870412009-11-22 04:24:42 +0000573
Chris Lattnerf4601652007-11-22 20:49:04 +0000574 if (Start < 0)
575 return TokError("invalid range, cannot be negative");
Bob Wilson21870412009-11-22 04:24:42 +0000576
Chris Lattnerf4601652007-11-22 20:49:04 +0000577 switch (Lex.Lex()) { // eat first character.
Bob Wilson21870412009-11-22 04:24:42 +0000578 default:
Chris Lattnerf4601652007-11-22 20:49:04 +0000579 Ranges.push_back(Start);
580 return false;
581 case tgtok::minus:
582 if (Lex.Lex() != tgtok::IntVal) {
583 TokError("expected integer value as end of range");
584 return true;
585 }
586 End = Lex.getCurIntVal();
587 break;
588 case tgtok::IntVal:
589 End = -Lex.getCurIntVal();
590 break;
591 }
Bob Wilson21870412009-11-22 04:24:42 +0000592 if (End < 0)
Chris Lattnerf4601652007-11-22 20:49:04 +0000593 return TokError("invalid range, cannot be negative");
594 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000595
Chris Lattnerf4601652007-11-22 20:49:04 +0000596 // Add to the range.
597 if (Start < End) {
598 for (; Start <= End; ++Start)
599 Ranges.push_back(Start);
600 } else {
601 for (; Start >= End; --Start)
602 Ranges.push_back(Start);
603 }
604 return false;
605}
606
607/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
608///
609/// RangeList ::= RangePiece (',' RangePiece)*
610///
611std::vector<unsigned> TGParser::ParseRangeList() {
612 std::vector<unsigned> Result;
Bob Wilson21870412009-11-22 04:24:42 +0000613
Chris Lattnerf4601652007-11-22 20:49:04 +0000614 // Parse the first piece.
615 if (ParseRangePiece(Result))
616 return std::vector<unsigned>();
617 while (Lex.getCode() == tgtok::comma) {
618 Lex.Lex(); // Eat the comma.
619
620 // Parse the next range piece.
621 if (ParseRangePiece(Result))
622 return std::vector<unsigned>();
623 }
624 return Result;
625}
626
627/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
628/// OptionalRangeList ::= '<' RangeList '>'
629/// OptionalRangeList ::= /*empty*/
630bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
631 if (Lex.getCode() != tgtok::less)
632 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000633
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000634 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000635 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000636
Chris Lattnerf4601652007-11-22 20:49:04 +0000637 // Parse the range list.
638 Ranges = ParseRangeList();
639 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000640
Chris Lattnerf4601652007-11-22 20:49:04 +0000641 if (Lex.getCode() != tgtok::greater) {
642 TokError("expected '>' at end of range list");
643 return Error(StartLoc, "to match this '<'");
644 }
645 Lex.Lex(); // eat the '>'.
646 return false;
647}
648
649/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
650/// OptionalBitList ::= '{' RangeList '}'
651/// OptionalBitList ::= /*empty*/
652bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
653 if (Lex.getCode() != tgtok::l_brace)
654 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000655
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000656 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000657 Lex.Lex(); // eat the '{'
Bob Wilson21870412009-11-22 04:24:42 +0000658
Chris Lattnerf4601652007-11-22 20:49:04 +0000659 // Parse the range list.
660 Ranges = ParseRangeList();
661 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000662
Chris Lattnerf4601652007-11-22 20:49:04 +0000663 if (Lex.getCode() != tgtok::r_brace) {
664 TokError("expected '}' at end of bit list");
665 return Error(StartLoc, "to match this '{'");
666 }
667 Lex.Lex(); // eat the '}'.
668 return false;
669}
670
671
672/// ParseType - Parse and return a tblgen type. This returns null on error.
673///
674/// Type ::= STRING // string type
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +0000675/// Type ::= CODE // code type
Chris Lattnerf4601652007-11-22 20:49:04 +0000676/// Type ::= BIT // bit type
677/// Type ::= BITS '<' INTVAL '>' // bits<x> type
678/// Type ::= INT // int type
679/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4601652007-11-22 20:49:04 +0000680/// Type ::= DAG // dag type
681/// Type ::= ClassID // Record Type
682///
683RecTy *TGParser::ParseType() {
684 switch (Lex.getCode()) {
685 default: TokError("Unknown token when expecting a type"); return 0;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000686 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +0000687 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000688 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
689 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000690 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4601652007-11-22 20:49:04 +0000691 case tgtok::Id:
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000692 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Chris Lattnerf4601652007-11-22 20:49:04 +0000693 return 0;
694 case tgtok::Bits: {
695 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
696 TokError("expected '<' after bits type");
697 return 0;
698 }
699 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
700 TokError("expected integer in bits<n> type");
701 return 0;
702 }
Dan Gohman63f97202008-10-17 01:33:43 +0000703 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4601652007-11-22 20:49:04 +0000704 if (Lex.Lex() != tgtok::greater) { // Eat count.
705 TokError("expected '>' at end of bits<n> type");
706 return 0;
707 }
708 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000709 return BitsRecTy::get(Val);
Chris Lattnerf4601652007-11-22 20:49:04 +0000710 }
711 case tgtok::List: {
712 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
713 TokError("expected '<' after list type");
714 return 0;
715 }
716 Lex.Lex(); // Eat '<'
717 RecTy *SubType = ParseType();
718 if (SubType == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +0000719
Chris Lattnerf4601652007-11-22 20:49:04 +0000720 if (Lex.getCode() != tgtok::greater) {
721 TokError("expected '>' at end of list<ty> type");
722 return 0;
723 }
724 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000725 return ListRecTy::get(SubType);
Chris Lattnerf4601652007-11-22 20:49:04 +0000726 }
Bob Wilson21870412009-11-22 04:24:42 +0000727 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000728}
729
730/// ParseIDValue - Parse an ID as a value and decode what it means.
731///
732/// IDValue ::= ID [def local value]
733/// IDValue ::= ID [def template arg]
734/// IDValue ::= ID [multiclass local value]
735/// IDValue ::= ID [multiclass template argument]
736/// IDValue ::= ID [def name]
737///
David Greenef3744a02011-10-19 13:04:20 +0000738Init *TGParser::ParseIDValue(Record *CurRec, IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000739 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
740 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000741 SMLoc Loc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000742 Lex.Lex();
743 return ParseIDValue(CurRec, Name, Loc);
744}
745
746/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
747/// has already been read.
David Greene05bce0b2011-07-29 22:43:06 +0000748Init *TGParser::ParseIDValue(Record *CurRec,
David Greenef3744a02011-10-19 13:04:20 +0000749 const std::string &Name, SMLoc NameLoc,
750 IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000751 if (CurRec) {
752 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenedcd35c72011-07-29 19:07:07 +0000753 return VarInit::get(Name, RV->getType());
Bob Wilson21870412009-11-22 04:24:42 +0000754
David Greenee22b3212011-10-19 13:02:42 +0000755 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
756
David Greenecaa25c82011-10-05 22:42:54 +0000757 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +0000758 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
759 "::");
David Greenecaa25c82011-10-05 22:42:54 +0000760
Chris Lattnerf4601652007-11-22 20:49:04 +0000761 if (CurRec->isTemplateArg(TemplateArgName)) {
762 const RecordVal *RV = CurRec->getValue(TemplateArgName);
763 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000764 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000765 }
766 }
Bob Wilson21870412009-11-22 04:24:42 +0000767
Chris Lattnerf4601652007-11-22 20:49:04 +0000768 if (CurMultiClass) {
David Greenee22b3212011-10-19 13:02:42 +0000769 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
770 "::");
771
Chris Lattnerf4601652007-11-22 20:49:04 +0000772 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
773 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
774 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000775 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000776 }
777 }
Bob Wilson21870412009-11-22 04:24:42 +0000778
David Greenecebb4ee2012-02-22 16:09:41 +0000779 // If this is in a foreach loop, make sure it's not a loop iterator
780 for (LoopVector::iterator i = Loops.begin(), iend = Loops.end();
781 i != iend;
782 ++i) {
Sean Silva6cfc8062012-10-10 20:24:43 +0000783 VarInit *IterVar = dyn_cast<VarInit>(i->IterVar);
David Greenecebb4ee2012-02-22 16:09:41 +0000784 if (IterVar && IterVar->getName() == Name)
785 return IterVar;
786 }
787
David Greenebbec2792011-10-19 13:04:21 +0000788 if (Mode == ParseNameMode)
789 return StringInit::get(Name);
790
Chris Lattnerf4601652007-11-22 20:49:04 +0000791 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000792 return DefInit::get(D);
Chris Lattnerf4601652007-11-22 20:49:04 +0000793
David Greenebbec2792011-10-19 13:04:21 +0000794 if (Mode == ParseValueMode) {
795 Error(NameLoc, "Variable not defined: '" + Name + "'");
796 return 0;
797 }
798
799 return StringInit::get(Name);
Chris Lattnerf4601652007-11-22 20:49:04 +0000800}
801
David Greened418c1b2009-05-14 20:54:48 +0000802/// ParseOperation - Parse an operator. This returns null on error.
803///
804/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
805///
David Greene05bce0b2011-07-29 22:43:06 +0000806Init *TGParser::ParseOperation(Record *CurRec) {
David Greened418c1b2009-05-14 20:54:48 +0000807 switch (Lex.getCode()) {
808 default:
809 TokError("unknown operation");
810 return 0;
David Greene1434f662011-01-07 17:05:37 +0000811 case tgtok::XHead:
812 case tgtok::XTail:
813 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +0000814 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
815 UnOpInit::UnaryOp Code;
816 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000817
David Greenee6c27de2009-05-14 21:22:49 +0000818 switch (Lex.getCode()) {
Craig Topper85814382012-02-07 05:05:23 +0000819 default: llvm_unreachable("Unhandled code!");
David Greenee6c27de2009-05-14 21:22:49 +0000820 case tgtok::XCast:
821 Lex.Lex(); // eat the operation
822 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000823
David Greenee6c27de2009-05-14 21:22:49 +0000824 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000825
David Greenee6c27de2009-05-14 21:22:49 +0000826 if (Type == 0) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000827 TokError("did not get type for unary operator");
David Greenee6c27de2009-05-14 21:22:49 +0000828 return 0;
829 }
David Greened418c1b2009-05-14 20:54:48 +0000830
David Greenee6c27de2009-05-14 21:22:49 +0000831 break;
David Greene1434f662011-01-07 17:05:37 +0000832 case tgtok::XHead:
David Greene5f9f9ba2009-05-14 22:38:31 +0000833 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000834 Code = UnOpInit::HEAD;
David Greene5f9f9ba2009-05-14 22:38:31 +0000835 break;
David Greene1434f662011-01-07 17:05:37 +0000836 case tgtok::XTail:
David Greene5f9f9ba2009-05-14 22:38:31 +0000837 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000838 Code = UnOpInit::TAIL;
David Greene5f9f9ba2009-05-14 22:38:31 +0000839 break;
David Greene1434f662011-01-07 17:05:37 +0000840 case tgtok::XEmpty:
David Greene5f9f9ba2009-05-14 22:38:31 +0000841 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000842 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000843 Type = IntRecTy::get();
David Greene5f9f9ba2009-05-14 22:38:31 +0000844 break;
David Greenee6c27de2009-05-14 21:22:49 +0000845 }
846 if (Lex.getCode() != tgtok::l_paren) {
847 TokError("expected '(' after unary operator");
848 return 0;
849 }
850 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000851
David Greene05bce0b2011-07-29 22:43:06 +0000852 Init *LHS = ParseValue(CurRec);
David Greenee6c27de2009-05-14 21:22:49 +0000853 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000854
David Greene1434f662011-01-07 17:05:37 +0000855 if (Code == UnOpInit::HEAD
856 || Code == UnOpInit::TAIL
857 || Code == UnOpInit::EMPTY) {
Sean Silva6cfc8062012-10-10 20:24:43 +0000858 ListInit *LHSl = dyn_cast<ListInit>(LHS);
859 StringInit *LHSs = dyn_cast<StringInit>(LHS);
860 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000861 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
862 TokError("expected list or string type argument in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000863 return 0;
864 }
865 if (LHSt) {
Sean Silva736ceac2012-10-05 03:31:58 +0000866 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
867 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
David Greenee1b46912009-06-08 20:23:18 +0000868 if (LType == 0 && SType == 0) {
869 TokError("expected list or string type argumnet in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000870 return 0;
871 }
872 }
873
David Greene1434f662011-01-07 17:05:37 +0000874 if (Code == UnOpInit::HEAD
875 || Code == UnOpInit::TAIL) {
David Greenee1b46912009-06-08 20:23:18 +0000876 if (LHSl == 0 && LHSt == 0) {
877 TokError("expected list type argumnet in unary operator");
878 return 0;
879 }
Bob Wilson21870412009-11-22 04:24:42 +0000880
David Greene5f9f9ba2009-05-14 22:38:31 +0000881 if (LHSl && LHSl->getSize() == 0) {
882 TokError("empty list argument in unary operator");
883 return 0;
884 }
885 if (LHSl) {
David Greene05bce0b2011-07-29 22:43:06 +0000886 Init *Item = LHSl->getElement(0);
Sean Silva6cfc8062012-10-10 20:24:43 +0000887 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
David Greene5f9f9ba2009-05-14 22:38:31 +0000888 if (Itemt == 0) {
889 TokError("untyped list element in unary operator");
890 return 0;
891 }
David Greene1434f662011-01-07 17:05:37 +0000892 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000893 Type = Itemt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000894 } else {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000895 Type = ListRecTy::get(Itemt->getType());
David Greene5f9f9ba2009-05-14 22:38:31 +0000896 }
Bob Wilson21870412009-11-22 04:24:42 +0000897 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000898 assert(LHSt && "expected list type argument in unary operator");
Sean Silva736ceac2012-10-05 03:31:58 +0000899 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
David Greene5f9f9ba2009-05-14 22:38:31 +0000900 if (LType == 0) {
901 TokError("expected list type argumnet in unary operator");
902 return 0;
903 }
David Greene1434f662011-01-07 17:05:37 +0000904 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000905 Type = LType->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +0000906 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000907 Type = LType;
908 }
909 }
910 }
911 }
912
David Greenee6c27de2009-05-14 21:22:49 +0000913 if (Lex.getCode() != tgtok::r_paren) {
914 TokError("expected ')' in unary operator");
915 return 0;
916 }
917 Lex.Lex(); // eat the ')'
David Greenedcd35c72011-07-29 19:07:07 +0000918 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee6c27de2009-05-14 21:22:49 +0000919 }
David Greened418c1b2009-05-14 20:54:48 +0000920
921 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +0000922 case tgtok::XSRA:
David Greened418c1b2009-05-14 20:54:48 +0000923 case tgtok::XSRL:
924 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +0000925 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +0000926 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000927 tgtok::TokKind OpTok = Lex.getCode();
928 SMLoc OpLoc = Lex.getLoc();
929 Lex.Lex(); // eat the operation
930
David Greened418c1b2009-05-14 20:54:48 +0000931 BinOpInit::BinaryOp Code;
932 RecTy *Type = 0;
933
Chris Lattner8d978a72010-10-05 23:58:18 +0000934 switch (OpTok) {
Craig Topper85814382012-02-07 05:05:23 +0000935 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000936 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
937 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
938 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
939 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
940 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Bob Wilson21870412009-11-22 04:24:42 +0000941 case tgtok::XStrConcat:
David Greened418c1b2009-05-14 20:54:48 +0000942 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000943 Type = StringRecTy::get();
David Greened418c1b2009-05-14 20:54:48 +0000944 break;
David Greened418c1b2009-05-14 20:54:48 +0000945 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000946
David Greened418c1b2009-05-14 20:54:48 +0000947 if (Lex.getCode() != tgtok::l_paren) {
948 TokError("expected '(' after binary operator");
949 return 0;
950 }
951 Lex.Lex(); // eat the '('
952
David Greene05bce0b2011-07-29 22:43:06 +0000953 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000954
Chris Lattner8d978a72010-10-05 23:58:18 +0000955 InitList.push_back(ParseValue(CurRec));
956 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000957
Chris Lattner8d978a72010-10-05 23:58:18 +0000958 while (Lex.getCode() == tgtok::comma) {
959 Lex.Lex(); // eat the ','
960
961 InitList.push_back(ParseValue(CurRec));
962 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000963 }
David Greened418c1b2009-05-14 20:54:48 +0000964
965 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner8d978a72010-10-05 23:58:18 +0000966 TokError("expected ')' in operator");
David Greened418c1b2009-05-14 20:54:48 +0000967 return 0;
968 }
969 Lex.Lex(); // eat the ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000970
971 // We allow multiple operands to associative operators like !strconcat as
972 // shorthand for nesting them.
973 if (Code == BinOpInit::STRCONCAT) {
974 while (InitList.size() > 2) {
David Greene05bce0b2011-07-29 22:43:06 +0000975 Init *RHS = InitList.pop_back_val();
David Greenedcd35c72011-07-29 19:07:07 +0000976 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
977 ->Fold(CurRec, CurMultiClass);
Chris Lattner8d978a72010-10-05 23:58:18 +0000978 InitList.back() = RHS;
979 }
980 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000981
Chris Lattner8d978a72010-10-05 23:58:18 +0000982 if (InitList.size() == 2)
David Greenedcd35c72011-07-29 19:07:07 +0000983 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner8d978a72010-10-05 23:58:18 +0000984 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000985
Chris Lattner8d978a72010-10-05 23:58:18 +0000986 Error(OpLoc, "expected two operands to operator");
987 return 0;
David Greened418c1b2009-05-14 20:54:48 +0000988 }
989
David Greene9bea7c82009-05-14 23:26:46 +0000990 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +0000991 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +0000992 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
993 TernOpInit::TernaryOp Code;
994 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000995
David Greene4afc5092009-05-14 21:54:42 +0000996 tgtok::TokKind LexCode = Lex.getCode();
997 Lex.Lex(); // eat the operation
998 switch (LexCode) {
Craig Topper85814382012-02-07 05:05:23 +0000999 default: llvm_unreachable("Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +00001000 case tgtok::XIf:
1001 Code = TernOpInit::IF;
1002 break;
David Greenebeb31a52009-05-14 22:23:47 +00001003 case tgtok::XForEach:
1004 Code = TernOpInit::FOREACH;
1005 break;
David Greene4afc5092009-05-14 21:54:42 +00001006 case tgtok::XSubst:
1007 Code = TernOpInit::SUBST;
1008 break;
1009 }
1010 if (Lex.getCode() != tgtok::l_paren) {
1011 TokError("expected '(' after ternary operator");
1012 return 0;
1013 }
1014 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +00001015
David Greene05bce0b2011-07-29 22:43:06 +00001016 Init *LHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001017 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001018
David Greene4afc5092009-05-14 21:54:42 +00001019 if (Lex.getCode() != tgtok::comma) {
1020 TokError("expected ',' in ternary operator");
1021 return 0;
1022 }
1023 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001024
David Greene05bce0b2011-07-29 22:43:06 +00001025 Init *MHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001026 if (MHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001027
David Greene4afc5092009-05-14 21:54:42 +00001028 if (Lex.getCode() != tgtok::comma) {
1029 TokError("expected ',' in ternary operator");
1030 return 0;
1031 }
1032 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001033
David Greene05bce0b2011-07-29 22:43:06 +00001034 Init *RHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001035 if (RHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001036
David Greene4afc5092009-05-14 21:54:42 +00001037 if (Lex.getCode() != tgtok::r_paren) {
1038 TokError("expected ')' in binary operator");
1039 return 0;
1040 }
1041 Lex.Lex(); // eat the ')'
David Greened418c1b2009-05-14 20:54:48 +00001042
David Greene4afc5092009-05-14 21:54:42 +00001043 switch (LexCode) {
Craig Topper85814382012-02-07 05:05:23 +00001044 default: llvm_unreachable("Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +00001045 case tgtok::XIf: {
Bill Wendling548f5a02010-12-13 01:46:19 +00001046 RecTy *MHSTy = 0;
1047 RecTy *RHSTy = 0;
1048
Sean Silva6cfc8062012-10-10 20:24:43 +00001049 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling548f5a02010-12-13 01:46:19 +00001050 MHSTy = MHSt->getType();
Sean Silva6cfc8062012-10-10 20:24:43 +00001051 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao307525c2012-09-06 23:32:48 +00001052 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva6cfc8062012-10-10 20:24:43 +00001053 if (dyn_cast<BitInit>(MHS))
Michael Liao307525c2012-09-06 23:32:48 +00001054 MHSTy = BitRecTy::get();
1055
Sean Silva6cfc8062012-10-10 20:24:43 +00001056 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling548f5a02010-12-13 01:46:19 +00001057 RHSTy = RHSt->getType();
Sean Silva6cfc8062012-10-10 20:24:43 +00001058 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao307525c2012-09-06 23:32:48 +00001059 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva6cfc8062012-10-10 20:24:43 +00001060 if (dyn_cast<BitInit>(RHS))
Michael Liao307525c2012-09-06 23:32:48 +00001061 RHSTy = BitRecTy::get();
1062
1063 // For UnsetInit, it's typed from the other hand.
Sean Silva6cfc8062012-10-10 20:24:43 +00001064 if (dyn_cast<UnsetInit>(MHS))
Michael Liao307525c2012-09-06 23:32:48 +00001065 MHSTy = RHSTy;
Sean Silva6cfc8062012-10-10 20:24:43 +00001066 if (dyn_cast<UnsetInit>(RHS))
Michael Liao307525c2012-09-06 23:32:48 +00001067 RHSTy = MHSTy;
Bill Wendling548f5a02010-12-13 01:46:19 +00001068
1069 if (!MHSTy || !RHSTy) {
David Greene9bea7c82009-05-14 23:26:46 +00001070 TokError("could not get type for !if");
1071 return 0;
1072 }
Bill Wendling548f5a02010-12-13 01:46:19 +00001073
1074 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1075 Type = RHSTy;
1076 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1077 Type = MHSTy;
Bob Wilson21870412009-11-22 04:24:42 +00001078 } else {
David Greene9bea7c82009-05-14 23:26:46 +00001079 TokError("inconsistent types for !if");
1080 return 0;
1081 }
1082 break;
1083 }
David Greenebeb31a52009-05-14 22:23:47 +00001084 case tgtok::XForEach: {
Sean Silva6cfc8062012-10-10 20:24:43 +00001085 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
David Greenebeb31a52009-05-14 22:23:47 +00001086 if (MHSt == 0) {
1087 TokError("could not get type for !foreach");
1088 return 0;
1089 }
1090 Type = MHSt->getType();
1091 break;
1092 }
David Greene4afc5092009-05-14 21:54:42 +00001093 case tgtok::XSubst: {
Sean Silva6cfc8062012-10-10 20:24:43 +00001094 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
David Greene4afc5092009-05-14 21:54:42 +00001095 if (RHSt == 0) {
1096 TokError("could not get type for !subst");
1097 return 0;
1098 }
1099 Type = RHSt->getType();
1100 break;
1101 }
1102 }
David Greenedcd35c72011-07-29 19:07:07 +00001103 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson21870412009-11-22 04:24:42 +00001104 CurMultiClass);
David Greene4afc5092009-05-14 21:54:42 +00001105 }
David Greened418c1b2009-05-14 20:54:48 +00001106 }
David Greened418c1b2009-05-14 20:54:48 +00001107}
1108
1109/// ParseOperatorType - Parse a type for an operator. This returns
1110/// null on error.
1111///
1112/// OperatorType ::= '<' Type '>'
1113///
Dan Gohmana9ad0412009-08-12 22:10:57 +00001114RecTy *TGParser::ParseOperatorType() {
David Greened418c1b2009-05-14 20:54:48 +00001115 RecTy *Type = 0;
1116
1117 if (Lex.getCode() != tgtok::less) {
1118 TokError("expected type name for operator");
1119 return 0;
1120 }
1121 Lex.Lex(); // eat the <
1122
1123 Type = ParseType();
1124
1125 if (Type == 0) {
1126 TokError("expected type name for operator");
1127 return 0;
1128 }
1129
1130 if (Lex.getCode() != tgtok::greater) {
1131 TokError("expected type name for operator");
1132 return 0;
1133 }
1134 Lex.Lex(); // eat the >
1135
1136 return Type;
1137}
1138
1139
Chris Lattnerf4601652007-11-22 20:49:04 +00001140/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1141///
1142/// SimpleValue ::= IDValue
1143/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001144/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +00001145/// SimpleValue ::= CODEFRAGMENT
1146/// SimpleValue ::= '?'
1147/// SimpleValue ::= '{' ValueList '}'
1148/// SimpleValue ::= ID '<' ValueListNE '>'
1149/// SimpleValue ::= '[' ValueList ']'
1150/// SimpleValue ::= '(' IDValue DagArgList ')'
1151/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
1152/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1153/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1154/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1155/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1156///
David Greenef3744a02011-10-19 13:04:20 +00001157Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1158 IDParseMode Mode) {
David Greene05bce0b2011-07-29 22:43:06 +00001159 Init *R = 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001160 switch (Lex.getCode()) {
1161 default: TokError("Unknown token when parsing a value"); break;
David Greened3d1cad2011-10-19 13:04:43 +00001162 case tgtok::paste:
1163 // This is a leading paste operation. This is deprecated but
1164 // still exists in some .td files. Ignore it.
1165 Lex.Lex(); // Skip '#'.
1166 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenedcd35c72011-07-29 19:07:07 +00001167 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001168 case tgtok::StrVal: {
1169 std::string Val = Lex.getCurStrVal();
1170 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001171
Jim Grosbachda4231f2009-03-26 16:17:51 +00001172 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001173 while (Lex.getCode() == tgtok::StrVal) {
1174 Val += Lex.getCurStrVal();
1175 Lex.Lex();
1176 }
Bob Wilson21870412009-11-22 04:24:42 +00001177
David Greenedcd35c72011-07-29 19:07:07 +00001178 R = StringInit::get(Val);
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001179 break;
1180 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001181 case tgtok::CodeFragment:
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +00001182 R = StringInit::get(Lex.getCurStrVal());
Chris Lattner578bcf02010-10-06 04:31:40 +00001183 Lex.Lex();
1184 break;
1185 case tgtok::question:
David Greenedcd35c72011-07-29 19:07:07 +00001186 R = UnsetInit::get();
Chris Lattner578bcf02010-10-06 04:31:40 +00001187 Lex.Lex();
1188 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001189 case tgtok::Id: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001190 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001191 std::string Name = Lex.getCurStrVal();
1192 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greenef3744a02011-10-19 13:04:20 +00001193 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson21870412009-11-22 04:24:42 +00001194
Chris Lattnerf4601652007-11-22 20:49:04 +00001195 // Value ::= ID '<' ValueListNE '>'
1196 if (Lex.Lex() == tgtok::greater) {
1197 TokError("expected non-empty value list");
1198 return 0;
1199 }
David Greenee1b46912009-06-08 20:23:18 +00001200
Chris Lattnerf4601652007-11-22 20:49:04 +00001201 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1202 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1203 // body.
1204 Record *Class = Records.getClass(Name);
1205 if (!Class) {
1206 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1207 return 0;
1208 }
David Greenee1b46912009-06-08 20:23:18 +00001209
David Greene05bce0b2011-07-29 22:43:06 +00001210 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
David Greenee1b46912009-06-08 20:23:18 +00001211 if (ValueList.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001212
David Greenee1b46912009-06-08 20:23:18 +00001213 if (Lex.getCode() != tgtok::greater) {
1214 TokError("expected '>' at end of value list");
1215 return 0;
1216 }
1217 Lex.Lex(); // eat the '>'
Bob Wilson21870412009-11-22 04:24:42 +00001218
Chris Lattnerf4601652007-11-22 20:49:04 +00001219 // Create the new record, set it as CurRec temporarily.
1220 static unsigned AnonCounter = 0;
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001221 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),
1222 NameLoc,
1223 Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001224 SubClassReference SCRef;
1225 SCRef.RefLoc = NameLoc;
1226 SCRef.Rec = Class;
1227 SCRef.TemplateArgs = ValueList;
1228 // Add info about the subclass to NewRec.
1229 if (AddSubClass(NewRec, SCRef))
1230 return 0;
1231 NewRec->resolveReferences();
1232 Records.addDef(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001233
Chris Lattnerf4601652007-11-22 20:49:04 +00001234 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +00001235 return DefInit::get(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001236 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001237 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001238 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001239 Lex.Lex(); // eat the '{'
David Greene05bce0b2011-07-29 22:43:06 +00001240 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001241
Chris Lattnerf4601652007-11-22 20:49:04 +00001242 if (Lex.getCode() != tgtok::r_brace) {
1243 Vals = ParseValueList(CurRec);
1244 if (Vals.empty()) return 0;
1245 }
1246 if (Lex.getCode() != tgtok::r_brace) {
1247 TokError("expected '}' at end of bit list value");
1248 return 0;
1249 }
1250 Lex.Lex(); // eat the '}'
Bob Wilson21870412009-11-22 04:24:42 +00001251
David Greene05bce0b2011-07-29 22:43:06 +00001252 SmallVector<Init *, 16> NewBits(Vals.size());
David Greeneca7fd3d2011-07-29 19:07:00 +00001253
Chris Lattnerf4601652007-11-22 20:49:04 +00001254 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001255 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Chris Lattnerf4601652007-11-22 20:49:04 +00001256 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +00001257 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1258 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +00001259 return 0;
1260 }
David Greeneca7fd3d2011-07-29 19:07:00 +00001261 NewBits[Vals.size()-i-1] = Bit;
Chris Lattnerf4601652007-11-22 20:49:04 +00001262 }
David Greenedcd35c72011-07-29 19:07:07 +00001263 return BitsInit::get(NewBits);
Chris Lattnerf4601652007-11-22 20:49:04 +00001264 }
1265 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1266 Lex.Lex(); // eat the '['
David Greene05bce0b2011-07-29 22:43:06 +00001267 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001268
David Greenee1b46912009-06-08 20:23:18 +00001269 RecTy *DeducedEltTy = 0;
1270 ListRecTy *GivenListTy = 0;
Bob Wilson21870412009-11-22 04:24:42 +00001271
David Greenee1b46912009-06-08 20:23:18 +00001272 if (ItemType != 0) {
Sean Silva736ceac2012-10-05 03:31:58 +00001273 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
David Greenee1b46912009-06-08 20:23:18 +00001274 if (ListType == 0) {
1275 std::stringstream s;
Bob Wilson21870412009-11-22 04:24:42 +00001276 s << "Type mismatch for list, expected list type, got "
David Greenee1b46912009-06-08 20:23:18 +00001277 << ItemType->getAsString();
1278 TokError(s.str());
Jim Grosbach6a44ada2011-03-11 19:52:52 +00001279 return 0;
David Greenee1b46912009-06-08 20:23:18 +00001280 }
1281 GivenListTy = ListType;
Bob Wilson21870412009-11-22 04:24:42 +00001282 }
David Greenee1b46912009-06-08 20:23:18 +00001283
Chris Lattnerf4601652007-11-22 20:49:04 +00001284 if (Lex.getCode() != tgtok::r_square) {
Bob Wilson21870412009-11-22 04:24:42 +00001285 Vals = ParseValueList(CurRec, 0,
1286 GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001287 if (Vals.empty()) return 0;
1288 }
1289 if (Lex.getCode() != tgtok::r_square) {
1290 TokError("expected ']' at end of list value");
1291 return 0;
1292 }
1293 Lex.Lex(); // eat the ']'
David Greenee1b46912009-06-08 20:23:18 +00001294
1295 RecTy *GivenEltTy = 0;
1296 if (Lex.getCode() == tgtok::less) {
1297 // Optional list element type
1298 Lex.Lex(); // eat the '<'
1299
1300 GivenEltTy = ParseType();
1301 if (GivenEltTy == 0) {
1302 // Couldn't parse element type
1303 return 0;
1304 }
1305
1306 if (Lex.getCode() != tgtok::greater) {
1307 TokError("expected '>' at end of list element type");
1308 return 0;
1309 }
1310 Lex.Lex(); // eat the '>'
1311 }
1312
1313 // Check elements
1314 RecTy *EltTy = 0;
David Greene05bce0b2011-07-29 22:43:06 +00001315 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greenee1b46912009-06-08 20:23:18 +00001316 i != ie;
1317 ++i) {
Sean Silva6cfc8062012-10-10 20:24:43 +00001318 TypedInit *TArg = dyn_cast<TypedInit>(*i);
David Greenee1b46912009-06-08 20:23:18 +00001319 if (TArg == 0) {
1320 TokError("Untyped list element");
1321 return 0;
1322 }
1323 if (EltTy != 0) {
1324 EltTy = resolveTypes(EltTy, TArg->getType());
1325 if (EltTy == 0) {
1326 TokError("Incompatible types in list elements");
1327 return 0;
1328 }
Bob Wilson21870412009-11-22 04:24:42 +00001329 } else {
David Greenee1b46912009-06-08 20:23:18 +00001330 EltTy = TArg->getType();
1331 }
1332 }
1333
1334 if (GivenEltTy != 0) {
1335 if (EltTy != 0) {
1336 // Verify consistency
1337 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1338 TokError("Incompatible types in list elements");
1339 return 0;
1340 }
1341 }
1342 EltTy = GivenEltTy;
1343 }
1344
1345 if (EltTy == 0) {
1346 if (ItemType == 0) {
1347 TokError("No type for list");
1348 return 0;
1349 }
1350 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +00001351 } else {
David Greenee1b46912009-06-08 20:23:18 +00001352 // Make sure the deduced type is compatible with the given type
1353 if (GivenListTy) {
1354 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1355 TokError("Element type mismatch for list");
1356 return 0;
1357 }
1358 }
1359 DeducedEltTy = EltTy;
1360 }
Bob Wilson21870412009-11-22 04:24:42 +00001361
David Greenedcd35c72011-07-29 19:07:07 +00001362 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4601652007-11-22 20:49:04 +00001363 }
1364 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1365 Lex.Lex(); // eat the '('
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001366 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001367 TokError("expected identifier in dag init");
1368 return 0;
1369 }
Bob Wilson21870412009-11-22 04:24:42 +00001370
David Greene05bce0b2011-07-29 22:43:06 +00001371 Init *Operator = ParseValue(CurRec);
Chris Lattner578bcf02010-10-06 04:31:40 +00001372 if (Operator == 0) return 0;
David Greenec7cafcd2009-04-22 20:18:10 +00001373
Nate Begeman7cee8172009-03-19 05:21:56 +00001374 // If the operator name is present, parse it.
1375 std::string OperatorName;
1376 if (Lex.getCode() == tgtok::colon) {
1377 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1378 TokError("expected variable name in dag operator");
1379 return 0;
1380 }
1381 OperatorName = Lex.getCurStrVal();
1382 Lex.Lex(); // eat the VarName.
1383 }
Bob Wilson21870412009-11-22 04:24:42 +00001384
David Greene05bce0b2011-07-29 22:43:06 +00001385 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4601652007-11-22 20:49:04 +00001386 if (Lex.getCode() != tgtok::r_paren) {
1387 DagArgs = ParseDagArgList(CurRec);
1388 if (DagArgs.empty()) return 0;
1389 }
Bob Wilson21870412009-11-22 04:24:42 +00001390
Chris Lattnerf4601652007-11-22 20:49:04 +00001391 if (Lex.getCode() != tgtok::r_paren) {
1392 TokError("expected ')' in dag init");
1393 return 0;
1394 }
1395 Lex.Lex(); // eat the ')'
Bob Wilson21870412009-11-22 04:24:42 +00001396
David Greenedcd35c72011-07-29 19:07:07 +00001397 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4601652007-11-22 20:49:04 +00001398 }
Bob Wilson21870412009-11-22 04:24:42 +00001399
David Greene1434f662011-01-07 17:05:37 +00001400 case tgtok::XHead:
1401 case tgtok::XTail:
1402 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +00001403 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001404 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +00001405 case tgtok::XSRA:
Chris Lattnerf4601652007-11-22 20:49:04 +00001406 case tgtok::XSRL:
1407 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +00001408 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001409 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene9bea7c82009-05-14 23:26:46 +00001410 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +00001411 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001412 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greened418c1b2009-05-14 20:54:48 +00001413 return ParseOperation(CurRec);
Chris Lattnerf4601652007-11-22 20:49:04 +00001414 }
1415 }
Bob Wilson21870412009-11-22 04:24:42 +00001416
Chris Lattnerf4601652007-11-22 20:49:04 +00001417 return R;
1418}
1419
1420/// ParseValue - Parse a tblgen value. This returns null on error.
1421///
1422/// Value ::= SimpleValue ValueSuffix*
1423/// ValueSuffix ::= '{' BitList '}'
1424/// ValueSuffix ::= '[' BitList ']'
1425/// ValueSuffix ::= '.' ID
1426///
David Greenef3744a02011-10-19 13:04:20 +00001427Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1428 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Chris Lattnerf4601652007-11-22 20:49:04 +00001429 if (Result == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001430
Chris Lattnerf4601652007-11-22 20:49:04 +00001431 // Parse the suffixes now if present.
1432 while (1) {
1433 switch (Lex.getCode()) {
1434 default: return Result;
1435 case tgtok::l_brace: {
David Greenecebb4ee2012-02-22 16:09:41 +00001436 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greene8592b2b2011-10-19 13:04:26 +00001437 // This is the beginning of the object body.
1438 return Result;
1439
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001440 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001441 Lex.Lex(); // eat the '{'
1442 std::vector<unsigned> Ranges = ParseRangeList();
1443 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001444
Chris Lattnerf4601652007-11-22 20:49:04 +00001445 // Reverse the bitlist.
1446 std::reverse(Ranges.begin(), Ranges.end());
1447 Result = Result->convertInitializerBitRange(Ranges);
1448 if (Result == 0) {
1449 Error(CurlyLoc, "Invalid bit range for value");
1450 return 0;
1451 }
Bob Wilson21870412009-11-22 04:24:42 +00001452
Chris Lattnerf4601652007-11-22 20:49:04 +00001453 // Eat the '}'.
1454 if (Lex.getCode() != tgtok::r_brace) {
1455 TokError("expected '}' at end of bit range list");
1456 return 0;
1457 }
1458 Lex.Lex();
1459 break;
1460 }
1461 case tgtok::l_square: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001462 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001463 Lex.Lex(); // eat the '['
1464 std::vector<unsigned> Ranges = ParseRangeList();
1465 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001466
Chris Lattnerf4601652007-11-22 20:49:04 +00001467 Result = Result->convertInitListSlice(Ranges);
1468 if (Result == 0) {
1469 Error(SquareLoc, "Invalid range for list slice");
1470 return 0;
1471 }
Bob Wilson21870412009-11-22 04:24:42 +00001472
Chris Lattnerf4601652007-11-22 20:49:04 +00001473 // Eat the ']'.
1474 if (Lex.getCode() != tgtok::r_square) {
1475 TokError("expected ']' at end of list slice");
1476 return 0;
1477 }
1478 Lex.Lex();
1479 break;
1480 }
1481 case tgtok::period:
1482 if (Lex.Lex() != tgtok::Id) { // eat the .
1483 TokError("expected field identifier after '.'");
1484 return 0;
1485 }
1486 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001487 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00001488 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +00001489 return 0;
1490 }
David Greenedcd35c72011-07-29 19:07:07 +00001491 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001492 Lex.Lex(); // eat field name
1493 break;
David Greened3d1cad2011-10-19 13:04:43 +00001494
1495 case tgtok::paste:
1496 SMLoc PasteLoc = Lex.getLoc();
1497
1498 // Create a !strconcat() operation, first casting each operand to
1499 // a string if necessary.
1500
Sean Silva6cfc8062012-10-10 20:24:43 +00001501 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greened3d1cad2011-10-19 13:04:43 +00001502 if (!LHS) {
1503 Error(PasteLoc, "LHS of paste is not typed!");
1504 return 0;
1505 }
1506
1507 if (LHS->getType() != StringRecTy::get()) {
1508 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1509 }
1510
1511 TypedInit *RHS = 0;
1512
1513 Lex.Lex(); // Eat the '#'.
1514 switch (Lex.getCode()) {
1515 case tgtok::colon:
1516 case tgtok::semi:
1517 case tgtok::l_brace:
1518 // These are all of the tokens that can begin an object body.
1519 // Some of these can also begin values but we disallow those cases
1520 // because they are unlikely to be useful.
1521
1522 // Trailing paste, concat with an empty string.
1523 RHS = StringInit::get("");
1524 break;
1525
1526 default:
1527 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silva6cfc8062012-10-10 20:24:43 +00001528 RHS = dyn_cast<TypedInit>(RHSResult);
David Greened3d1cad2011-10-19 13:04:43 +00001529 if (!RHS) {
1530 Error(PasteLoc, "RHS of paste is not typed!");
1531 return 0;
1532 }
1533
1534 if (RHS->getType() != StringRecTy::get()) {
1535 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1536 }
1537
1538 break;
1539 }
1540
1541 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1542 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1543 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001544 }
1545 }
1546}
1547
1548/// ParseDagArgList - Parse the argument list for a dag literal expression.
1549///
1550/// ParseDagArgList ::= Value (':' VARNAME)?
1551/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
David Greene05bce0b2011-07-29 22:43:06 +00001552std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4601652007-11-22 20:49:04 +00001553TGParser::ParseDagArgList(Record *CurRec) {
David Greene05bce0b2011-07-29 22:43:06 +00001554 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson21870412009-11-22 04:24:42 +00001555
Chris Lattnerf4601652007-11-22 20:49:04 +00001556 while (1) {
David Greene05bce0b2011-07-29 22:43:06 +00001557 Init *Val = ParseValue(CurRec);
1558 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
Bob Wilson21870412009-11-22 04:24:42 +00001559
Chris Lattnerf4601652007-11-22 20:49:04 +00001560 // If the variable name is present, add it.
1561 std::string VarName;
1562 if (Lex.getCode() == tgtok::colon) {
1563 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1564 TokError("expected variable name in dag literal");
David Greene05bce0b2011-07-29 22:43:06 +00001565 return std::vector<std::pair<llvm::Init*, std::string> >();
Chris Lattnerf4601652007-11-22 20:49:04 +00001566 }
1567 VarName = Lex.getCurStrVal();
1568 Lex.Lex(); // eat the VarName.
1569 }
Bob Wilson21870412009-11-22 04:24:42 +00001570
Chris Lattnerf4601652007-11-22 20:49:04 +00001571 Result.push_back(std::make_pair(Val, VarName));
Bob Wilson21870412009-11-22 04:24:42 +00001572
Chris Lattnerf4601652007-11-22 20:49:04 +00001573 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson21870412009-11-22 04:24:42 +00001574 Lex.Lex(); // eat the ','
Chris Lattnerf4601652007-11-22 20:49:04 +00001575 }
Bob Wilson21870412009-11-22 04:24:42 +00001576
Chris Lattnerf4601652007-11-22 20:49:04 +00001577 return Result;
1578}
1579
1580
1581/// ParseValueList - Parse a comma separated list of values, returning them as a
1582/// vector. Note that this always expects to be able to parse at least one
1583/// value. It returns an empty list if this is not possible.
1584///
1585/// ValueList ::= Value (',' Value)
1586///
David Greene05bce0b2011-07-29 22:43:06 +00001587std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopherd568b3f2011-07-11 23:06:52 +00001588 RecTy *EltTy) {
David Greene05bce0b2011-07-29 22:43:06 +00001589 std::vector<Init*> Result;
David Greenee1b46912009-06-08 20:23:18 +00001590 RecTy *ItemType = EltTy;
David Greene67acdf22009-06-29 19:59:52 +00001591 unsigned int ArgN = 0;
David Greenee1b46912009-06-08 20:23:18 +00001592 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001593 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Jim Grosbachb1320cb2012-01-20 20:02:39 +00001594 if (!TArgs.size()) {
1595 TokError("template argument provided to non-template class");
1596 return std::vector<Init*>();
1597 }
David Greenee1b46912009-06-08 20:23:18 +00001598 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greened9746fe2011-09-19 18:26:07 +00001599 if (!RV) {
1600 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1601 << ")\n";
1602 }
David Greenee1b46912009-06-08 20:23:18 +00001603 assert(RV && "Template argument record not found??");
1604 ItemType = RV->getType();
1605 ++ArgN;
1606 }
1607 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001608 if (Result.back() == 0) return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001609
Chris Lattnerf4601652007-11-22 20:49:04 +00001610 while (Lex.getCode() == tgtok::comma) {
1611 Lex.Lex(); // Eat the comma
Bob Wilson21870412009-11-22 04:24:42 +00001612
David Greenee1b46912009-06-08 20:23:18 +00001613 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001614 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greene67acdf22009-06-29 19:59:52 +00001615 if (ArgN >= TArgs.size()) {
1616 TokError("too many template arguments");
David Greene05bce0b2011-07-29 22:43:06 +00001617 return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001618 }
David Greenee1b46912009-06-08 20:23:18 +00001619 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1620 assert(RV && "Template argument record not found??");
1621 ItemType = RV->getType();
1622 ++ArgN;
1623 }
1624 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001625 if (Result.back() == 0) return std::vector<Init*>();
Chris Lattnerf4601652007-11-22 20:49:04 +00001626 }
Bob Wilson21870412009-11-22 04:24:42 +00001627
Chris Lattnerf4601652007-11-22 20:49:04 +00001628 return Result;
1629}
1630
1631
Chris Lattnerf4601652007-11-22 20:49:04 +00001632/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1633/// empty string on error. This can happen in a number of different context's,
1634/// including within a def or in the template args for a def (which which case
1635/// CurRec will be non-null) and within the template args for a multiclass (in
1636/// which case CurRec will be null, but CurMultiClass will be set). This can
1637/// also happen within a def that is within a multiclass, which will set both
1638/// CurRec and CurMultiClass.
1639///
1640/// Declaration ::= FIELD? Type ID ('=' Value)?
1641///
David Greenee22b3212011-10-19 13:02:42 +00001642Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4601652007-11-22 20:49:04 +00001643 bool ParsingTemplateArgs) {
1644 // Read the field prefix if present.
1645 bool HasField = Lex.getCode() == tgtok::Field;
1646 if (HasField) Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001647
Chris Lattnerf4601652007-11-22 20:49:04 +00001648 RecTy *Type = ParseType();
David Greenee22b3212011-10-19 13:02:42 +00001649 if (Type == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001650
Chris Lattnerf4601652007-11-22 20:49:04 +00001651 if (Lex.getCode() != tgtok::Id) {
1652 TokError("Expected identifier in declaration");
David Greenee22b3212011-10-19 13:02:42 +00001653 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001654 }
Bob Wilson21870412009-11-22 04:24:42 +00001655
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001656 SMLoc IdLoc = Lex.getLoc();
David Greenee22b3212011-10-19 13:02:42 +00001657 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001658 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001659
Chris Lattnerf4601652007-11-22 20:49:04 +00001660 if (ParsingTemplateArgs) {
1661 if (CurRec) {
David Greenee22b3212011-10-19 13:02:42 +00001662 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4601652007-11-22 20:49:04 +00001663 } else {
1664 assert(CurMultiClass);
1665 }
1666 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +00001667 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1668 "::");
Chris Lattnerf4601652007-11-22 20:49:04 +00001669 }
Bob Wilson21870412009-11-22 04:24:42 +00001670
Chris Lattnerf4601652007-11-22 20:49:04 +00001671 // Add the value.
1672 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
David Greenee22b3212011-10-19 13:02:42 +00001673 return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001674
Chris Lattnerf4601652007-11-22 20:49:04 +00001675 // If a value is present, parse it.
1676 if (Lex.getCode() == tgtok::equal) {
1677 Lex.Lex();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001678 SMLoc ValLoc = Lex.getLoc();
David Greene05bce0b2011-07-29 22:43:06 +00001679 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001680 if (Val == 0 ||
1681 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
David Greenee22b3212011-10-19 13:02:42 +00001682 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001683 }
Bob Wilson21870412009-11-22 04:24:42 +00001684
Chris Lattnerf4601652007-11-22 20:49:04 +00001685 return DeclName;
1686}
1687
David Greenecebb4ee2012-02-22 16:09:41 +00001688/// ParseForeachDeclaration - Read a foreach declaration, returning
1689/// the name of the declared object or a NULL Init on error. Return
1690/// the name of the parsed initializer list through ForeachListName.
1691///
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001692/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1693/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1694/// ForeachDeclaration ::= ID '=' RangePiece
David Greenecebb4ee2012-02-22 16:09:41 +00001695///
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00001696VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenecebb4ee2012-02-22 16:09:41 +00001697 if (Lex.getCode() != tgtok::Id) {
1698 TokError("Expected identifier in foreach declaration");
1699 return 0;
1700 }
1701
1702 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1703 Lex.Lex();
1704
1705 // If a value is present, parse it.
1706 if (Lex.getCode() != tgtok::equal) {
1707 TokError("Expected '=' in foreach declaration");
1708 return 0;
1709 }
1710 Lex.Lex(); // Eat the '='
1711
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001712 RecTy *IterType = 0;
1713 std::vector<unsigned> Ranges;
David Greenecebb4ee2012-02-22 16:09:41 +00001714
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001715 switch (Lex.getCode()) {
1716 default: TokError("Unknown token when expecting a range list"); return 0;
1717 case tgtok::l_square: { // '[' ValueList ']'
1718 Init *List = ParseSimpleValue(0, 0, ParseForeachMode);
Sean Silva6cfc8062012-10-10 20:24:43 +00001719 ForeachListValue = dyn_cast<ListInit>(List);
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001720 if (ForeachListValue == 0) {
1721 TokError("Expected a Value list");
1722 return 0;
1723 }
1724 RecTy *ValueType = ForeachListValue->getType();
Sean Silva736ceac2012-10-05 03:31:58 +00001725 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001726 if (ListType == 0) {
1727 TokError("Value list is not of list type");
1728 return 0;
1729 }
1730 IterType = ListType->getElementType();
1731 break;
David Greenecebb4ee2012-02-22 16:09:41 +00001732 }
1733
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001734 case tgtok::IntVal: { // RangePiece.
1735 if (ParseRangePiece(Ranges))
1736 return 0;
1737 break;
David Greenecebb4ee2012-02-22 16:09:41 +00001738 }
1739
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001740 case tgtok::l_brace: { // '{' RangeList '}'
1741 Lex.Lex(); // eat the '{'
1742 Ranges = ParseRangeList();
1743 if (Lex.getCode() != tgtok::r_brace) {
1744 TokError("expected '}' at end of bit range list");
1745 return 0;
1746 }
1747 Lex.Lex();
1748 break;
1749 }
1750 }
David Greenecebb4ee2012-02-22 16:09:41 +00001751
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001752 if (!Ranges.empty()) {
1753 assert(!IterType && "Type already initialized?");
1754 IterType = IntRecTy::get();
1755 std::vector<Init*> Values;
1756 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
1757 Values.push_back(IntInit::get(Ranges[i]));
1758 ForeachListValue = ListInit::get(Values, IterType);
1759 }
1760
1761 if (!IterType)
1762 return 0;
1763
1764 return VarInit::get(DeclName, IterType);
David Greenecebb4ee2012-02-22 16:09:41 +00001765}
1766
Chris Lattnerf4601652007-11-22 20:49:04 +00001767/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1768/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1769/// template args for a def, which may or may not be in a multiclass. If null,
1770/// these are the template args for a multiclass.
1771///
1772/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson21870412009-11-22 04:24:42 +00001773///
Chris Lattnerf4601652007-11-22 20:49:04 +00001774bool TGParser::ParseTemplateArgList(Record *CurRec) {
1775 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1776 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +00001777
Chris Lattnerf4601652007-11-22 20:49:04 +00001778 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +00001779
Chris Lattnerf4601652007-11-22 20:49:04 +00001780 // Read the first declaration.
David Greenee22b3212011-10-19 13:02:42 +00001781 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1782 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001783 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001784
Chris Lattnerf4601652007-11-22 20:49:04 +00001785 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson21870412009-11-22 04:24:42 +00001786
Chris Lattnerf4601652007-11-22 20:49:04 +00001787 while (Lex.getCode() == tgtok::comma) {
1788 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001789
Chris Lattnerf4601652007-11-22 20:49:04 +00001790 // Read the following declarations.
1791 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
David Greenee22b3212011-10-19 13:02:42 +00001792 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001793 return true;
1794 TheRecToAddTo->addTemplateArg(TemplArg);
1795 }
Bob Wilson21870412009-11-22 04:24:42 +00001796
Chris Lattnerf4601652007-11-22 20:49:04 +00001797 if (Lex.getCode() != tgtok::greater)
1798 return TokError("expected '>' at end of template argument list");
1799 Lex.Lex(); // eat the '>'.
1800 return false;
1801}
1802
1803
1804/// ParseBodyItem - Parse a single item at within the body of a def or class.
1805///
1806/// BodyItem ::= Declaration ';'
1807/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1808bool TGParser::ParseBodyItem(Record *CurRec) {
1809 if (Lex.getCode() != tgtok::Let) {
David Greenee22b3212011-10-19 13:02:42 +00001810 if (ParseDeclaration(CurRec, false) == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001811 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001812
Chris Lattnerf4601652007-11-22 20:49:04 +00001813 if (Lex.getCode() != tgtok::semi)
1814 return TokError("expected ';' after declaration");
1815 Lex.Lex();
1816 return false;
1817 }
1818
1819 // LET ID OptionalRangeList '=' Value ';'
1820 if (Lex.Lex() != tgtok::Id)
1821 return TokError("expected field identifier after let");
Bob Wilson21870412009-11-22 04:24:42 +00001822
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001823 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001824 std::string FieldName = Lex.getCurStrVal();
1825 Lex.Lex(); // eat the field name.
Bob Wilson21870412009-11-22 04:24:42 +00001826
Chris Lattnerf4601652007-11-22 20:49:04 +00001827 std::vector<unsigned> BitList;
Bob Wilson21870412009-11-22 04:24:42 +00001828 if (ParseOptionalBitList(BitList))
Chris Lattnerf4601652007-11-22 20:49:04 +00001829 return true;
1830 std::reverse(BitList.begin(), BitList.end());
Bob Wilson21870412009-11-22 04:24:42 +00001831
Chris Lattnerf4601652007-11-22 20:49:04 +00001832 if (Lex.getCode() != tgtok::equal)
1833 return TokError("expected '=' in let expression");
1834 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001835
David Greenee1b46912009-06-08 20:23:18 +00001836 RecordVal *Field = CurRec->getValue(FieldName);
1837 if (Field == 0)
1838 return TokError("Value '" + FieldName + "' unknown!");
1839
1840 RecTy *Type = Field->getType();
Bob Wilson21870412009-11-22 04:24:42 +00001841
David Greene05bce0b2011-07-29 22:43:06 +00001842 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001843 if (Val == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001844
Chris Lattnerf4601652007-11-22 20:49:04 +00001845 if (Lex.getCode() != tgtok::semi)
1846 return TokError("expected ';' after let expression");
1847 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001848
Chris Lattnerf4601652007-11-22 20:49:04 +00001849 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1850}
1851
1852/// ParseBody - Read the body of a class or def. Return true on error, false on
1853/// success.
1854///
1855/// Body ::= ';'
1856/// Body ::= '{' BodyList '}'
1857/// BodyList BodyItem*
1858///
1859bool TGParser::ParseBody(Record *CurRec) {
1860 // If this is a null definition, just eat the semi and return.
1861 if (Lex.getCode() == tgtok::semi) {
1862 Lex.Lex();
1863 return false;
1864 }
Bob Wilson21870412009-11-22 04:24:42 +00001865
Chris Lattnerf4601652007-11-22 20:49:04 +00001866 if (Lex.getCode() != tgtok::l_brace)
1867 return TokError("Expected ';' or '{' to start body");
1868 // Eat the '{'.
1869 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001870
Chris Lattnerf4601652007-11-22 20:49:04 +00001871 while (Lex.getCode() != tgtok::r_brace)
1872 if (ParseBodyItem(CurRec))
1873 return true;
1874
1875 // Eat the '}'.
1876 Lex.Lex();
1877 return false;
1878}
1879
1880/// ParseObjectBody - Parse the body of a def or class. This consists of an
1881/// optional ClassList followed by a Body. CurRec is the current def or class
1882/// that is being parsed.
1883///
1884/// ObjectBody ::= BaseClassList Body
1885/// BaseClassList ::= /*empty*/
1886/// BaseClassList ::= ':' BaseClassListNE
1887/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1888///
1889bool TGParser::ParseObjectBody(Record *CurRec) {
1890 // If there is a baseclass list, read it.
1891 if (Lex.getCode() == tgtok::colon) {
1892 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001893
Chris Lattnerf4601652007-11-22 20:49:04 +00001894 // Read all of the subclasses.
1895 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1896 while (1) {
1897 // Check for error.
1898 if (SubClass.Rec == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001899
Chris Lattnerf4601652007-11-22 20:49:04 +00001900 // Add it.
1901 if (AddSubClass(CurRec, SubClass))
1902 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001903
Chris Lattnerf4601652007-11-22 20:49:04 +00001904 if (Lex.getCode() != tgtok::comma) break;
1905 Lex.Lex(); // eat ','.
1906 SubClass = ParseSubClassReference(CurRec, false);
1907 }
1908 }
1909
1910 // Process any variables on the let stack.
1911 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1912 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1913 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1914 LetStack[i][j].Bits, LetStack[i][j].Value))
1915 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001916
Chris Lattnerf4601652007-11-22 20:49:04 +00001917 return ParseBody(CurRec);
1918}
1919
Chris Lattnerf4601652007-11-22 20:49:04 +00001920/// ParseDef - Parse and return a top level or multiclass def, return the record
1921/// corresponding to it. This returns null on error.
1922///
1923/// DefInst ::= DEF ObjectName ObjectBody
1924///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001925bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001926 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001927 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson21870412009-11-22 04:24:42 +00001928 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4601652007-11-22 20:49:04 +00001929
1930 // Parse ObjectName and make a record for it.
David Greenea9e07dd2011-10-19 13:04:29 +00001931 Record *CurRec = new Record(ParseObjectName(CurMultiClass), DefLoc, Records);
Bob Wilson21870412009-11-22 04:24:42 +00001932
Jakob Stoklund Olesen72cba6c2012-05-24 22:17:36 +00001933 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001934 // Top-level def definition.
Bob Wilson21870412009-11-22 04:24:42 +00001935
Chris Lattnerf4601652007-11-22 20:49:04 +00001936 // Ensure redefinition doesn't happen.
David Greene1d501392012-01-28 00:03:24 +00001937 if (Records.getDef(CurRec->getNameInitAsString())) {
David Greene2c49fbb2011-10-19 13:03:45 +00001938 Error(DefLoc, "def '" + CurRec->getNameInitAsString()
1939 + "' already defined");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001940 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001941 }
1942 Records.addDef(CurRec);
Jakob Stoklund Olesen72cba6c2012-05-24 22:17:36 +00001943 } else if (CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001944 // Otherwise, a def inside a multiclass, add it to the multiclass.
1945 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene91919cd2011-10-19 13:03:51 +00001946 if (CurMultiClass->DefPrototypes[i]->getNameInit()
1947 == CurRec->getNameInit()) {
1948 Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
Chris Lattnerf4601652007-11-22 20:49:04 +00001949 "' already defined in this multiclass!");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001950 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001951 }
1952 CurMultiClass->DefPrototypes.push_back(CurRec);
1953 }
Bob Wilson21870412009-11-22 04:24:42 +00001954
Chris Lattnerf4601652007-11-22 20:49:04 +00001955 if (ParseObjectBody(CurRec))
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001956 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001957
Chris Lattnerf4601652007-11-22 20:49:04 +00001958 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
David Greene0d886402011-08-10 18:27:46 +00001959 // See Record::setName(). This resolve step will see any new name
1960 // for the def that might have been created when resolving
1961 // inheritance, values and arguments above.
Chris Lattnerf4601652007-11-22 20:49:04 +00001962 CurRec->resolveReferences();
Bob Wilson21870412009-11-22 04:24:42 +00001963
Chris Lattnerf4601652007-11-22 20:49:04 +00001964 // If ObjectBody has template arguments, it's an error.
1965 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001966
1967 if (CurMultiClass) {
1968 // Copy the template arguments for the multiclass into the def.
David Greenee22b3212011-10-19 13:02:42 +00001969 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001970 CurMultiClass->Rec.getTemplateArgs();
1971
1972 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1973 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1974 assert(RV && "Template arg doesn't exist?");
1975 CurRec->addValue(*RV);
1976 }
1977 }
1978
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00001979 if (ProcessForeachDefs(CurRec, DefLoc)) {
David Greenecebb4ee2012-02-22 16:09:41 +00001980 Error(DefLoc,
1981 "Could not process loops for def" + CurRec->getNameInitAsString());
1982 return true;
1983 }
1984
1985 return false;
1986}
1987
1988/// ParseForeach - Parse a for statement. Return the record corresponding
1989/// to it. This returns true on error.
1990///
1991/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
1992/// Foreach ::= FOREACH Declaration IN Object
1993///
1994bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
1995 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
1996 Lex.Lex(); // Eat the 'for' token.
1997
1998 // Make a temporary object to record items associated with the for
1999 // loop.
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00002000 ListInit *ListValue = 0;
2001 VarInit *IterName = ParseForeachDeclaration(ListValue);
David Greenecebb4ee2012-02-22 16:09:41 +00002002 if (IterName == 0)
2003 return TokError("expected declaration in for");
2004
2005 if (Lex.getCode() != tgtok::In)
2006 return TokError("Unknown tok");
2007 Lex.Lex(); // Eat the in
2008
2009 // Create a loop object and remember it.
2010 Loops.push_back(ForeachLoop(IterName, ListValue));
2011
2012 if (Lex.getCode() != tgtok::l_brace) {
2013 // FOREACH Declaration IN Object
2014 if (ParseObject(CurMultiClass))
2015 return true;
2016 }
2017 else {
2018 SMLoc BraceLoc = Lex.getLoc();
2019 // Otherwise, this is a group foreach.
2020 Lex.Lex(); // eat the '{'.
2021
2022 // Parse the object list.
2023 if (ParseObjectList(CurMultiClass))
2024 return true;
2025
2026 if (Lex.getCode() != tgtok::r_brace) {
2027 TokError("expected '}' at end of foreach command");
2028 return Error(BraceLoc, "to match this '{'");
2029 }
2030 Lex.Lex(); // Eat the }
2031 }
2032
2033 // We've processed everything in this loop.
2034 Loops.pop_back();
2035
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002036 return false;
Chris Lattnerf4601652007-11-22 20:49:04 +00002037}
2038
Chris Lattnerf4601652007-11-22 20:49:04 +00002039/// ParseClass - Parse a tblgen class definition.
2040///
2041/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2042///
2043bool TGParser::ParseClass() {
2044 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2045 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002046
Chris Lattnerf4601652007-11-22 20:49:04 +00002047 if (Lex.getCode() != tgtok::Id)
2048 return TokError("expected class name after 'class' keyword");
Bob Wilson21870412009-11-22 04:24:42 +00002049
Chris Lattnerf4601652007-11-22 20:49:04 +00002050 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2051 if (CurRec) {
2052 // If the body was previously defined, this is an error.
David Greenee3385652011-10-19 13:04:13 +00002053 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4601652007-11-22 20:49:04 +00002054 !CurRec->getSuperClasses().empty() ||
2055 !CurRec->getTemplateArgs().empty())
David Greene69a23942011-10-19 13:03:58 +00002056 return TokError("Class '" + CurRec->getNameInitAsString()
2057 + "' already defined");
Chris Lattnerf4601652007-11-22 20:49:04 +00002058 } else {
2059 // If this is the first reference to this class, create and add it.
Chris Lattner9c6b60e2010-12-15 04:48:22 +00002060 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00002061 Records.addClass(CurRec);
2062 }
2063 Lex.Lex(); // eat the name.
Bob Wilson21870412009-11-22 04:24:42 +00002064
Chris Lattnerf4601652007-11-22 20:49:04 +00002065 // If there are template args, parse them.
2066 if (Lex.getCode() == tgtok::less)
2067 if (ParseTemplateArgList(CurRec))
2068 return true;
2069
2070 // Finally, parse the object body.
2071 return ParseObjectBody(CurRec);
2072}
2073
2074/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2075/// of LetRecords.
2076///
2077/// LetList ::= LetItem (',' LetItem)*
2078/// LetItem ::= ID OptionalRangeList '=' Value
2079///
2080std::vector<LetRecord> TGParser::ParseLetList() {
2081 std::vector<LetRecord> Result;
Bob Wilson21870412009-11-22 04:24:42 +00002082
Chris Lattnerf4601652007-11-22 20:49:04 +00002083 while (1) {
2084 if (Lex.getCode() != tgtok::Id) {
2085 TokError("expected identifier in let definition");
2086 return std::vector<LetRecord>();
2087 }
2088 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002089 SMLoc NameLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +00002090 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4601652007-11-22 20:49:04 +00002091
2092 // Check for an optional RangeList.
2093 std::vector<unsigned> Bits;
Bob Wilson21870412009-11-22 04:24:42 +00002094 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4601652007-11-22 20:49:04 +00002095 return std::vector<LetRecord>();
2096 std::reverse(Bits.begin(), Bits.end());
Bob Wilson21870412009-11-22 04:24:42 +00002097
Chris Lattnerf4601652007-11-22 20:49:04 +00002098 if (Lex.getCode() != tgtok::equal) {
2099 TokError("expected '=' in let expression");
2100 return std::vector<LetRecord>();
2101 }
2102 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00002103
David Greene05bce0b2011-07-29 22:43:06 +00002104 Init *Val = ParseValue(0);
Chris Lattnerf4601652007-11-22 20:49:04 +00002105 if (Val == 0) return std::vector<LetRecord>();
Bob Wilson21870412009-11-22 04:24:42 +00002106
Chris Lattnerf4601652007-11-22 20:49:04 +00002107 // Now that we have everything, add the record.
2108 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson21870412009-11-22 04:24:42 +00002109
Chris Lattnerf4601652007-11-22 20:49:04 +00002110 if (Lex.getCode() != tgtok::comma)
2111 return Result;
Bob Wilson21870412009-11-22 04:24:42 +00002112 Lex.Lex(); // eat the comma.
Chris Lattnerf4601652007-11-22 20:49:04 +00002113 }
2114}
2115
2116/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002117/// different related productions. This works inside multiclasses too.
Chris Lattnerf4601652007-11-22 20:49:04 +00002118///
2119/// Object ::= LET LetList IN '{' ObjectList '}'
2120/// Object ::= LET LetList IN Object
2121///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002122bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002123 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2124 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002125
Chris Lattnerf4601652007-11-22 20:49:04 +00002126 // Add this entry to the let stack.
2127 std::vector<LetRecord> LetInfo = ParseLetList();
2128 if (LetInfo.empty()) return true;
2129 LetStack.push_back(LetInfo);
2130
2131 if (Lex.getCode() != tgtok::In)
2132 return TokError("expected 'in' at end of top-level 'let'");
2133 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002134
Chris Lattnerf4601652007-11-22 20:49:04 +00002135 // If this is a scalar let, just handle it now
2136 if (Lex.getCode() != tgtok::l_brace) {
2137 // LET LetList IN Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002138 if (ParseObject(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00002139 return true;
2140 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002141 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002142 // Otherwise, this is a group let.
2143 Lex.Lex(); // eat the '{'.
Bob Wilson21870412009-11-22 04:24:42 +00002144
Chris Lattnerf4601652007-11-22 20:49:04 +00002145 // Parse the object list.
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002146 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00002147 return true;
Bob Wilson21870412009-11-22 04:24:42 +00002148
Chris Lattnerf4601652007-11-22 20:49:04 +00002149 if (Lex.getCode() != tgtok::r_brace) {
2150 TokError("expected '}' at end of top level let command");
2151 return Error(BraceLoc, "to match this '{'");
2152 }
2153 Lex.Lex();
2154 }
Bob Wilson21870412009-11-22 04:24:42 +00002155
Chris Lattnerf4601652007-11-22 20:49:04 +00002156 // Outside this let scope, this let block is not active.
2157 LetStack.pop_back();
2158 return false;
2159}
2160
Chris Lattnerf4601652007-11-22 20:49:04 +00002161/// ParseMultiClass - Parse a multiclass definition.
2162///
Bob Wilson32558652009-04-28 19:41:44 +00002163/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
2164/// ':' BaseMultiClassList '{' MultiClassDef+ '}'
Chris Lattnerf4601652007-11-22 20:49:04 +00002165///
2166bool TGParser::ParseMultiClass() {
2167 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2168 Lex.Lex(); // Eat the multiclass token.
2169
2170 if (Lex.getCode() != tgtok::Id)
2171 return TokError("expected identifier after multiclass for name");
2172 std::string Name = Lex.getCurStrVal();
Bob Wilson21870412009-11-22 04:24:42 +00002173
Chris Lattnerf4601652007-11-22 20:49:04 +00002174 if (MultiClasses.count(Name))
2175 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson21870412009-11-22 04:24:42 +00002176
Chris Lattner67db8832010-12-13 00:23:57 +00002177 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
2178 Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00002179 Lex.Lex(); // Eat the identifier.
Bob Wilson21870412009-11-22 04:24:42 +00002180
Chris Lattnerf4601652007-11-22 20:49:04 +00002181 // If there are template args, parse them.
2182 if (Lex.getCode() == tgtok::less)
2183 if (ParseTemplateArgList(0))
2184 return true;
2185
David Greened34a73b2009-04-24 16:55:41 +00002186 bool inherits = false;
2187
David Greenede444af2009-04-22 16:42:54 +00002188 // If there are submulticlasses, parse them.
2189 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00002190 inherits = true;
2191
David Greenede444af2009-04-22 16:42:54 +00002192 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00002193
David Greenede444af2009-04-22 16:42:54 +00002194 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00002195 SubMultiClassReference SubMultiClass =
2196 ParseSubMultiClassReference(CurMultiClass);
David Greenede444af2009-04-22 16:42:54 +00002197 while (1) {
2198 // Check for error.
2199 if (SubMultiClass.MC == 0) return true;
Bob Wilson32558652009-04-28 19:41:44 +00002200
David Greenede444af2009-04-22 16:42:54 +00002201 // Add it.
2202 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2203 return true;
Bob Wilson32558652009-04-28 19:41:44 +00002204
David Greenede444af2009-04-22 16:42:54 +00002205 if (Lex.getCode() != tgtok::comma) break;
2206 Lex.Lex(); // eat ','.
2207 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2208 }
2209 }
2210
David Greened34a73b2009-04-24 16:55:41 +00002211 if (Lex.getCode() != tgtok::l_brace) {
2212 if (!inherits)
2213 return TokError("expected '{' in multiclass definition");
Bob Wilson21870412009-11-22 04:24:42 +00002214 else if (Lex.getCode() != tgtok::semi)
2215 return TokError("expected ';' in multiclass definition");
David Greened34a73b2009-04-24 16:55:41 +00002216 else
Bob Wilson21870412009-11-22 04:24:42 +00002217 Lex.Lex(); // eat the ';'.
2218 } else {
David Greened34a73b2009-04-24 16:55:41 +00002219 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2220 return TokError("multiclass must contain at least one def");
Bob Wilson21870412009-11-22 04:24:42 +00002221
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002222 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002223 switch (Lex.getCode()) {
2224 default:
David Greenea1b1b792011-10-07 18:25:05 +00002225 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002226 case tgtok::Let:
2227 case tgtok::Def:
2228 case tgtok::Defm:
David Greenecebb4ee2012-02-22 16:09:41 +00002229 case tgtok::Foreach:
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002230 if (ParseObject(CurMultiClass))
2231 return true;
2232 break;
2233 }
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002234 }
David Greened34a73b2009-04-24 16:55:41 +00002235 Lex.Lex(); // eat the '}'.
2236 }
Bob Wilson21870412009-11-22 04:24:42 +00002237
Chris Lattnerf4601652007-11-22 20:49:04 +00002238 CurMultiClass = 0;
2239 return false;
2240}
2241
David Greenee499a2d2011-10-05 22:42:07 +00002242Record *TGParser::
2243InstantiateMulticlassDef(MultiClass &MC,
2244 Record *DefProto,
David Greene7be867e2011-10-19 13:04:31 +00002245 Init *DefmPrefix,
David Greenee499a2d2011-10-05 22:42:07 +00002246 SMLoc DefmPrefixLoc) {
David Greene7be867e2011-10-19 13:04:31 +00002247 // We need to preserve DefProto so it can be reused for later
2248 // instantiations, so create a new Record to inherit from it.
2249
David Greenee499a2d2011-10-05 22:42:07 +00002250 // Add in the defm name. If the defm prefix is empty, give each
2251 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2252 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2253 // as a prefix.
David Greenee499a2d2011-10-05 22:42:07 +00002254
David Greene7be867e2011-10-19 13:04:31 +00002255 if (DefmPrefix == 0)
2256 DefmPrefix = StringInit::get(GetNewAnonymousName());
2257
2258 Init *DefName = DefProto->getNameInit();
2259
Sean Silva6cfc8062012-10-10 20:24:43 +00002260 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene7be867e2011-10-19 13:04:31 +00002261
David Greened3d1cad2011-10-19 13:04:43 +00002262 if (DefNameString != 0) {
2263 // We have a fully expanded string so there are no operators to
2264 // resolve. We should concatenate the given prefix and name.
David Greene7be867e2011-10-19 13:04:31 +00002265 DefName =
2266 BinOpInit::get(BinOpInit::STRCONCAT,
2267 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2268 StringRecTy::get())->Fold(DefProto, &MC),
2269 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2270 }
David Greene7be867e2011-10-19 13:04:31 +00002271
Jakob Stoklund Olesen376a8a72012-08-22 23:33:58 +00002272 // Make a trail of SMLocs from the multiclass instantiations.
2273 SmallVector<SMLoc, 4> Locs(1, DefmPrefixLoc);
2274 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
2275 Record *CurRec = new Record(DefName, Locs, Records);
David Greenee499a2d2011-10-05 22:42:07 +00002276
2277 SubClassReference Ref;
2278 Ref.RefLoc = DefmPrefixLoc;
2279 Ref.Rec = DefProto;
2280 AddSubClass(CurRec, Ref);
2281
Jim Grosbachcfbda4a2012-08-02 18:46:42 +00002282 // Set the value for NAME. We don't resolve references to it 'til later,
2283 // though, so that uses in nested multiclass names don't get
2284 // confused.
2285 if (SetValue(CurRec, Ref.RefLoc, "NAME", std::vector<unsigned>(),
2286 DefmPrefix)) {
2287 Error(DefmPrefixLoc, "Could not resolve "
2288 + CurRec->getNameInitAsString() + ":NAME to '"
2289 + DefmPrefix->getAsUnquotedString() + "'");
2290 return 0;
2291 }
David Greenee5b252f2011-10-19 13:04:35 +00002292
Jim Grosbachcfbda4a2012-08-02 18:46:42 +00002293 // If the DefNameString didn't resolve, we probably have a reference to
2294 // NAME and need to replace it. We need to do at least this much greedily,
2295 // otherwise nested multiclasses will end up with incorrect NAME expansions.
2296 if (DefNameString == 0) {
David Greenee5b252f2011-10-19 13:04:35 +00002297 RecordVal *DefNameRV = CurRec->getValue("NAME");
2298 CurRec->resolveReferencesTo(DefNameRV);
2299 }
2300
2301 if (!CurMultiClass) {
Jim Grosbachcfbda4a2012-08-02 18:46:42 +00002302 // Now that we're at the top level, resolve all NAME references
2303 // in the resultant defs that weren't in the def names themselves.
2304 RecordVal *DefNameRV = CurRec->getValue("NAME");
2305 CurRec->resolveReferencesTo(DefNameRV);
2306
2307 // Now that NAME references are resolved and we're at the top level of
2308 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greenee5b252f2011-10-19 13:04:35 +00002309 // currently in a multiclass, it means this defm appears inside a
2310 // multiclass and its name won't be fully resolvable until we see
2311 // the top-level defm. Therefore, we don't add this to the
2312 // RecordKeeper at this point. If we did we could get duplicate
2313 // defs as more than one probably refers to NAME or some other
2314 // common internal placeholder.
2315
2316 // Ensure redefinition doesn't happen.
2317 if (Records.getDef(CurRec->getNameInitAsString())) {
2318 Error(DefmPrefixLoc, "def '" + CurRec->getNameInitAsString() +
2319 "' already defined, instantiating defm with subdef '" +
2320 DefProto->getNameInitAsString() + "'");
2321 return 0;
2322 }
2323
2324 Records.addDef(CurRec);
2325 }
2326
David Greenee499a2d2011-10-05 22:42:07 +00002327 return CurRec;
2328}
2329
2330bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2331 Record *CurRec,
2332 SMLoc DefmPrefixLoc,
2333 SMLoc SubClassLoc,
David Greenee22b3212011-10-19 13:02:42 +00002334 const std::vector<Init *> &TArgs,
David Greenee499a2d2011-10-05 22:42:07 +00002335 std::vector<Init *> &TemplateVals,
2336 bool DeleteArgs) {
2337 // Loop over all of the template arguments, setting them to the specified
2338 // value or leaving them as the default if necessary.
2339 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2340 // Check if a value is specified for this temp-arg.
2341 if (i < TemplateVals.size()) {
2342 // Set it now.
2343 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2344 TemplateVals[i]))
2345 return true;
2346
2347 // Resolve it next.
2348 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2349
2350 if (DeleteArgs)
2351 // Now remove it.
2352 CurRec->removeValue(TArgs[i]);
2353
2354 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2355 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenee22b3212011-10-19 13:02:42 +00002356 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2357 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2358 + "'");
David Greenee499a2d2011-10-05 22:42:07 +00002359 }
2360 }
2361 return false;
2362}
2363
2364bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2365 Record *CurRec,
2366 Record *DefProto,
2367 SMLoc DefmPrefixLoc) {
2368 // If the mdef is inside a 'let' expression, add to each def.
2369 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2370 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2371 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2372 LetStack[i][j].Bits, LetStack[i][j].Value))
2373 return Error(DefmPrefixLoc, "when instantiating this defm");
2374
David Greenee499a2d2011-10-05 22:42:07 +00002375 // Don't create a top level definition for defm inside multiclasses,
2376 // instead, only update the prototypes and bind the template args
2377 // with the new created definition.
2378 if (CurMultiClass) {
2379 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2380 i != e; ++i)
David Greene22dde7e2011-10-19 13:04:02 +00002381 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2382 == CurRec->getNameInit())
2383 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
David Greenee499a2d2011-10-05 22:42:07 +00002384 "' already defined in this multiclass!");
2385 CurMultiClass->DefPrototypes.push_back(CurRec);
2386
2387 // Copy the template arguments for the multiclass into the new def.
David Greenee22b3212011-10-19 13:02:42 +00002388 const std::vector<Init *> &TA =
David Greenee499a2d2011-10-05 22:42:07 +00002389 CurMultiClass->Rec.getTemplateArgs();
2390
2391 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2392 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2393 assert(RV && "Template arg doesn't exist?");
2394 CurRec->addValue(*RV);
2395 }
David Greenee499a2d2011-10-05 22:42:07 +00002396 }
2397
2398 return false;
2399}
2400
Chris Lattnerf4601652007-11-22 20:49:04 +00002401/// ParseDefm - Parse the instantiation of a multiclass.
2402///
2403/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2404///
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002405bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002406 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Bob Wilson21870412009-11-22 04:24:42 +00002407
David Greenea9e07dd2011-10-19 13:04:29 +00002408 Init *DefmPrefix = 0;
David Greenea9e07dd2011-10-19 13:04:29 +00002409
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002410 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greenea9e07dd2011-10-19 13:04:29 +00002411 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002412 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +00002413
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002414 SMLoc DefmPrefixLoc = Lex.getLoc();
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002415 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4601652007-11-22 20:49:04 +00002416 return TokError("expected ':' after defm identifier");
Bob Wilson21870412009-11-22 04:24:42 +00002417
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002418 // Keep track of the new generated record definitions.
2419 std::vector<Record*> NewRecDefs;
2420
2421 // This record also inherits from a regular class (non-multiclass)?
2422 bool InheritFromClass = false;
2423
Chris Lattnerf4601652007-11-22 20:49:04 +00002424 // eat the colon.
2425 Lex.Lex();
2426
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002427 SMLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002428 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene56546132009-04-22 22:17:51 +00002429
2430 while (1) {
2431 if (Ref.Rec == 0) return true;
2432
2433 // To instantiate a multiclass, we need to first get the multiclass, then
2434 // instantiate each def contained in the multiclass with the SubClassRef
2435 // template parameters.
2436 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2437 assert(MC && "Didn't lookup multiclass correctly?");
David Greene05bce0b2011-07-29 22:43:06 +00002438 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greene56546132009-04-22 22:17:51 +00002439
2440 // Verify that the correct number of template arguments were specified.
David Greenee22b3212011-10-19 13:02:42 +00002441 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greene56546132009-04-22 22:17:51 +00002442 if (TArgs.size() < TemplateVals.size())
2443 return Error(SubClassLoc,
2444 "more template args specified than multiclass expects");
2445
2446 // Loop over all the def's in the multiclass, instantiating each one.
2447 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2448 Record *DefProto = MC->DefPrototypes[i];
2449
David Greene7be867e2011-10-19 13:04:31 +00002450 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix, DefmPrefixLoc);
Jim Grosbach94f2dc92011-12-02 18:33:03 +00002451 if (!CurRec)
2452 return true;
David Greene065f2592009-05-05 16:28:25 +00002453
David Greenee499a2d2011-10-05 22:42:07 +00002454 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmPrefixLoc, SubClassLoc,
2455 TArgs, TemplateVals, true/*Delete args*/))
2456 return Error(SubClassLoc, "could not instantiate def");
David Greene56546132009-04-22 22:17:51 +00002457
David Greenee499a2d2011-10-05 22:42:07 +00002458 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmPrefixLoc))
2459 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002460
2461 NewRecDefs.push_back(CurRec);
David Greene56546132009-04-22 22:17:51 +00002462 }
2463
David Greenee499a2d2011-10-05 22:42:07 +00002464
David Greene56546132009-04-22 22:17:51 +00002465 if (Lex.getCode() != tgtok::comma) break;
2466 Lex.Lex(); // eat ','.
2467
2468 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002469
2470 // A defm can inherit from regular classes (non-multiclass) as
2471 // long as they come in the end of the inheritance list.
2472 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0);
2473
2474 if (InheritFromClass)
2475 break;
2476
David Greene56546132009-04-22 22:17:51 +00002477 Ref = ParseSubClassReference(0, true);
2478 }
2479
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002480 if (InheritFromClass) {
2481 // Process all the classes to inherit as if they were part of a
2482 // regular 'def' and inherit all record values.
2483 SubClassReference SubClass = ParseSubClassReference(0, false);
2484 while (1) {
2485 // Check for error.
2486 if (SubClass.Rec == 0) return true;
2487
2488 // Get the expanded definition prototypes and teach them about
2489 // the record values the current class to inherit has
2490 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2491 Record *CurRec = NewRecDefs[i];
2492
2493 // Add it.
2494 if (AddSubClass(CurRec, SubClass))
2495 return true;
2496
2497 // Process any variables on the let stack.
2498 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2499 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2500 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2501 LetStack[i][j].Bits, LetStack[i][j].Value))
2502 return true;
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002503 }
2504
2505 if (Lex.getCode() != tgtok::comma) break;
2506 Lex.Lex(); // eat ','.
2507 SubClass = ParseSubClassReference(0, false);
2508 }
2509 }
2510
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002511 if (!CurMultiClass)
2512 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene0d886402011-08-10 18:27:46 +00002513 // See Record::setName(). This resolve step will see any new
2514 // name for the def that might have been created when resolving
2515 // inheritance, values and arguments above.
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002516 NewRecDefs[i]->resolveReferences();
2517
Chris Lattnerf4601652007-11-22 20:49:04 +00002518 if (Lex.getCode() != tgtok::semi)
2519 return TokError("expected ';' at end of defm");
2520 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002521
Chris Lattnerf4601652007-11-22 20:49:04 +00002522 return false;
2523}
2524
2525/// ParseObject
2526/// Object ::= ClassInst
2527/// Object ::= DefInst
2528/// Object ::= MultiClassInst
2529/// Object ::= DefMInst
2530/// Object ::= LETCommand '{' ObjectList '}'
2531/// Object ::= LETCommand Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002532bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002533 switch (Lex.getCode()) {
Chris Lattnerd6d9dd92010-10-31 19:27:15 +00002534 default:
2535 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002536 case tgtok::Let: return ParseTopLevelLet(MC);
2537 case tgtok::Def: return ParseDef(MC);
David Greenecebb4ee2012-02-22 16:09:41 +00002538 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002539 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4601652007-11-22 20:49:04 +00002540 case tgtok::Class: return ParseClass();
2541 case tgtok::MultiClass: return ParseMultiClass();
2542 }
2543}
2544
2545/// ParseObjectList
2546/// ObjectList :== Object*
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002547bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002548 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002549 if (ParseObject(MC))
Chris Lattnerf4601652007-11-22 20:49:04 +00002550 return true;
2551 }
2552 return false;
2553}
2554
Chris Lattnerf4601652007-11-22 20:49:04 +00002555bool TGParser::ParseFile() {
2556 Lex.Lex(); // Prime the lexer.
2557 if (ParseObjectList()) return true;
Bob Wilson21870412009-11-22 04:24:42 +00002558
Chris Lattnerf4601652007-11-22 20:49:04 +00002559 // If we have unread input at the end of the file, report it.
2560 if (Lex.getCode() == tgtok::Eof)
2561 return false;
Bob Wilson21870412009-11-22 04:24:42 +00002562
Chris Lattnerf4601652007-11-22 20:49:04 +00002563 return TokError("Unexpected input at top level");
2564}
2565