blob: b1067009426b4aaa2ea24812d86edc0ffb572832 [file] [log] [blame]
Chris Lattnerf4601652007-11-22 20:49:04 +00001//===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf4601652007-11-22 20:49:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Parser for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
14#include "TGParser.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/ADT/SmallVector.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000016#include "llvm/ADT/StringExtras.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/Support/CommandLine.h"
18#include "llvm/TableGen/Record.h"
Daniel Dunbar1a551802009-07-03 00:10:29 +000019#include <algorithm>
20#include <sstream>
Chris Lattnerf4601652007-11-22 20:49:04 +000021using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24// Support Code for the Semantic Actions.
25//===----------------------------------------------------------------------===//
26
27namespace llvm {
Chris Lattnerf4601652007-11-22 20:49:04 +000028struct SubClassReference {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000029 SMLoc RefLoc;
Chris Lattnerf4601652007-11-22 20:49:04 +000030 Record *Rec;
David Greene05bce0b2011-07-29 22:43:06 +000031 std::vector<Init*> TemplateArgs;
Chris Lattner1c8ae592009-03-13 16:01:53 +000032 SubClassReference() : Rec(0) {}
David Greened34a73b2009-04-24 16:55:41 +000033
Chris Lattnerf4601652007-11-22 20:49:04 +000034 bool isInvalid() const { return Rec == 0; }
35};
David Greenede444af2009-04-22 16:42:54 +000036
37struct SubMultiClassReference {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000038 SMLoc RefLoc;
David Greenede444af2009-04-22 16:42:54 +000039 MultiClass *MC;
David Greene05bce0b2011-07-29 22:43:06 +000040 std::vector<Init*> TemplateArgs;
David Greenede444af2009-04-22 16:42:54 +000041 SubMultiClassReference() : MC(0) {}
Bob Wilson32558652009-04-28 19:41:44 +000042
David Greenede444af2009-04-22 16:42:54 +000043 bool isInvalid() const { return MC == 0; }
David Greened34a73b2009-04-24 16:55:41 +000044 void dump() const;
David Greenede444af2009-04-22 16:42:54 +000045};
David Greened34a73b2009-04-24 16:55:41 +000046
47void SubMultiClassReference::dump() const {
Daniel Dunbar1a551802009-07-03 00:10:29 +000048 errs() << "Multiclass:\n";
Bob Wilson21870412009-11-22 04:24:42 +000049
David Greened34a73b2009-04-24 16:55:41 +000050 MC->dump();
Bob Wilson21870412009-11-22 04:24:42 +000051
Daniel Dunbar1a551802009-07-03 00:10:29 +000052 errs() << "Template args:\n";
David Greene05bce0b2011-07-29 22:43:06 +000053 for (std::vector<Init *>::const_iterator i = TemplateArgs.begin(),
David Greened34a73b2009-04-24 16:55:41 +000054 iend = TemplateArgs.end();
55 i != iend;
56 ++i) {
57 (*i)->dump();
58 }
59}
60
Chris Lattnerf4601652007-11-22 20:49:04 +000061} // end namespace llvm
62
Chris Lattner1e3a8a42009-06-21 03:39:35 +000063bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Chris Lattnerf4601652007-11-22 20:49:04 +000064 if (CurRec == 0)
65 CurRec = &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +000066
Jakob Stoklund Olesenebaf92c2012-01-13 03:16:35 +000067 if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
Chris Lattnerf4601652007-11-22 20:49:04 +000068 // The value already exists in the class, treat this as a set.
69 if (ERV->setValue(RV.getValue()))
70 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
71 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson21870412009-11-22 04:24:42 +000072 "previous definition of type '" +
Chris Lattnerf4601652007-11-22 20:49:04 +000073 ERV->getType()->getAsString() + "'");
74 } else {
75 CurRec->addValue(RV);
76 }
77 return false;
78}
79
80/// SetValue -
81/// Return true on error, false on success.
David Greene917924d2011-10-19 13:02:39 +000082bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
David Greene05bce0b2011-07-29 22:43:06 +000083 const std::vector<unsigned> &BitList, Init *V) {
Chris Lattnerf4601652007-11-22 20:49:04 +000084 if (!V) return false;
85
86 if (CurRec == 0) CurRec = &CurMultiClass->Rec;
87
88 RecordVal *RV = CurRec->getValue(ValName);
89 if (RV == 0)
David Greene917924d2011-10-19 13:02:39 +000090 return Error(Loc, "Value '" + ValName->getAsUnquotedString()
91 + "' unknown!");
Chris Lattnerf4601652007-11-22 20:49:04 +000092
93 // Do not allow assignments like 'X = X'. This will just cause infinite loops
94 // in the resolution machinery.
95 if (BitList.empty())
Sean Silva6cfc8062012-10-10 20:24:43 +000096 if (VarInit *VI = dyn_cast<VarInit>(V))
David Greene917924d2011-10-19 13:02:39 +000097 if (VI->getNameInit() == ValName)
Chris Lattnerf4601652007-11-22 20:49:04 +000098 return false;
Bob Wilson21870412009-11-22 04:24:42 +000099
Chris Lattnerf4601652007-11-22 20:49:04 +0000100 // If we are assigning to a subset of the bits in the value... then we must be
101 // assigning to a field of BitsRecTy, which must have a BitsInit
102 // initializer.
103 //
104 if (!BitList.empty()) {
Sean Silva6cfc8062012-10-10 20:24:43 +0000105 BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue());
Chris Lattnerf4601652007-11-22 20:49:04 +0000106 if (CurVal == 0)
David Greene917924d2011-10-19 13:02:39 +0000107 return Error(Loc, "Value '" + ValName->getAsUnquotedString()
108 + "' is not a bits type");
Chris Lattnerf4601652007-11-22 20:49:04 +0000109
110 // Convert the incoming value to a bits type of the appropriate size...
David Greene05bce0b2011-07-29 22:43:06 +0000111 Init *BI = V->convertInitializerTo(BitsRecTy::get(BitList.size()));
Chris Lattnerf4601652007-11-22 20:49:04 +0000112 if (BI == 0) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000113 return Error(Loc, "Initializer is not compatible with bit range");
114 }
Bob Wilson21870412009-11-22 04:24:42 +0000115
Chris Lattnerf4601652007-11-22 20:49:04 +0000116 // We should have a BitsInit type now.
Sean Silva6cfc8062012-10-10 20:24:43 +0000117 BitsInit *BInit = dyn_cast<BitsInit>(BI);
Chris Lattnerf4601652007-11-22 20:49:04 +0000118 assert(BInit != 0);
119
David Greene05bce0b2011-07-29 22:43:06 +0000120 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4601652007-11-22 20:49:04 +0000121
122 // Loop over bits, assigning values as appropriate.
123 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
124 unsigned Bit = BitList[i];
David Greeneca7fd3d2011-07-29 19:07:00 +0000125 if (NewBits[Bit])
Chris Lattnerf4601652007-11-22 20:49:04 +0000126 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
David Greene917924d2011-10-19 13:02:39 +0000127 ValName->getAsUnquotedString() + "' more than once");
David Greeneca7fd3d2011-07-29 19:07:00 +0000128 NewBits[Bit] = BInit->getBit(i);
Chris Lattnerf4601652007-11-22 20:49:04 +0000129 }
130
131 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
David Greeneca7fd3d2011-07-29 19:07:00 +0000132 if (NewBits[i] == 0)
133 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4601652007-11-22 20:49:04 +0000134
David Greenedcd35c72011-07-29 19:07:07 +0000135 V = BitsInit::get(NewBits);
Chris Lattnerf4601652007-11-22 20:49:04 +0000136 }
137
138 if (RV->setValue(V))
David Greene917924d2011-10-19 13:02:39 +0000139 return Error(Loc, "Value '" + ValName->getAsUnquotedString() + "' of type '"
140 + RV->getType()->getAsString() +
141 "' is incompatible with initializer '" + V->getAsString()
142 + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +0000143 return false;
144}
145
146/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
147/// args as SubClass's template arguments.
Cedric Venetaff9c272009-02-14 16:06:42 +0000148bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000149 Record *SC = SubClass.Rec;
150 // Add all of the values in the subclass into the current class.
151 const std::vector<RecordVal> &Vals = SC->getValues();
152 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
153 if (AddValue(CurRec, SubClass.RefLoc, Vals[i]))
154 return true;
155
David Greenee22b3212011-10-19 13:02:42 +0000156 const std::vector<Init *> &TArgs = SC->getTemplateArgs();
Chris Lattnerf4601652007-11-22 20:49:04 +0000157
158 // Ensure that an appropriate number of template arguments are specified.
159 if (TArgs.size() < SubClass.TemplateArgs.size())
160 return Error(SubClass.RefLoc, "More template args specified than expected");
Bob Wilson21870412009-11-22 04:24:42 +0000161
Chris Lattnerf4601652007-11-22 20:49:04 +0000162 // Loop over all of the template arguments, setting them to the specified
163 // value or leaving them as the default if necessary.
164 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
165 if (i < SubClass.TemplateArgs.size()) {
166 // If a value is specified for this template arg, set it now.
Bob Wilson21870412009-11-22 04:24:42 +0000167 if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(),
Chris Lattnerf4601652007-11-22 20:49:04 +0000168 SubClass.TemplateArgs[i]))
169 return true;
Bob Wilson21870412009-11-22 04:24:42 +0000170
Chris Lattnerf4601652007-11-22 20:49:04 +0000171 // Resolve it next.
172 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson21870412009-11-22 04:24:42 +0000173
Chris Lattnerf4601652007-11-22 20:49:04 +0000174 // Now remove it.
175 CurRec->removeValue(TArgs[i]);
176
177 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
178 return Error(SubClass.RefLoc,"Value not specified for template argument #"
David Greenee22b3212011-10-19 13:02:42 +0000179 + utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
180 + ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4601652007-11-22 20:49:04 +0000181 }
182 }
183
184 // Since everything went well, we can now set the "superclass" list for the
185 // current record.
186 const std::vector<Record*> &SCs = SC->getSuperClasses();
187 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
188 if (CurRec->isSubClassOf(SCs[i]))
189 return Error(SubClass.RefLoc,
190 "Already subclass of '" + SCs[i]->getName() + "'!\n");
191 CurRec->addSuperClass(SCs[i]);
192 }
Bob Wilson21870412009-11-22 04:24:42 +0000193
Chris Lattnerf4601652007-11-22 20:49:04 +0000194 if (CurRec->isSubClassOf(SC))
195 return Error(SubClass.RefLoc,
196 "Already subclass of '" + SC->getName() + "'!\n");
197 CurRec->addSuperClass(SC);
198 return false;
199}
200
David Greenede444af2009-04-22 16:42:54 +0000201/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilson440548d2009-04-30 18:26:19 +0000202/// CurMC, resolving its template args as SubMultiClass's
David Greenede444af2009-04-22 16:42:54 +0000203/// template arguments.
Bob Wilson440548d2009-04-30 18:26:19 +0000204bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson1d512df2009-04-30 17:46:20 +0000205 SubMultiClassReference &SubMultiClass) {
David Greenede444af2009-04-22 16:42:54 +0000206 MultiClass *SMC = SubMultiClass.MC;
Bob Wilson440548d2009-04-30 18:26:19 +0000207 Record *CurRec = &CurMC->Rec;
David Greenede444af2009-04-22 16:42:54 +0000208
Bob Wilson440548d2009-04-30 18:26:19 +0000209 const std::vector<RecordVal> &MCVals = CurRec->getValues();
David Greenede444af2009-04-22 16:42:54 +0000210
211 // Add all of the values in the subclass into the current class.
212 const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues();
213 for (unsigned i = 0, e = SMCVals.size(); i != e; ++i)
214 if (AddValue(CurRec, SubMultiClass.RefLoc, SMCVals[i]))
215 return true;
216
Bob Wilson440548d2009-04-30 18:26:19 +0000217 int newDefStart = CurMC->DefPrototypes.size();
David Greened34a73b2009-04-24 16:55:41 +0000218
David Greenede444af2009-04-22 16:42:54 +0000219 // Add all of the defs in the subclass into the current multiclass.
220 for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(),
221 iend = SMC->DefPrototypes.end();
222 i != iend;
223 ++i) {
224 // Clone the def and add it to the current multiclass
225 Record *NewDef = new Record(**i);
226
227 // Add all of the values in the superclass into the current def.
228 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
229 if (AddValue(NewDef, SubMultiClass.RefLoc, MCVals[i]))
230 return true;
231
Bob Wilson440548d2009-04-30 18:26:19 +0000232 CurMC->DefPrototypes.push_back(NewDef);
David Greenede444af2009-04-22 16:42:54 +0000233 }
Bob Wilson32558652009-04-28 19:41:44 +0000234
David Greenee22b3212011-10-19 13:02:42 +0000235 const std::vector<Init *> &SMCTArgs = SMC->Rec.getTemplateArgs();
David Greenede444af2009-04-22 16:42:54 +0000236
David Greened34a73b2009-04-24 16:55:41 +0000237 // Ensure that an appropriate number of template arguments are
238 // specified.
David Greenede444af2009-04-22 16:42:54 +0000239 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
David Greened34a73b2009-04-24 16:55:41 +0000240 return Error(SubMultiClass.RefLoc,
241 "More template args specified than expected");
Bob Wilson32558652009-04-28 19:41:44 +0000242
David Greenede444af2009-04-22 16:42:54 +0000243 // Loop over all of the template arguments, setting them to the specified
244 // value or leaving them as the default if necessary.
245 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
246 if (i < SubMultiClass.TemplateArgs.size()) {
David Greened34a73b2009-04-24 16:55:41 +0000247 // If a value is specified for this template arg, set it in the
248 // superclass now.
249 if (SetValue(CurRec, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000250 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000251 SubMultiClass.TemplateArgs[i]))
252 return true;
253
254 // Resolve it next.
255 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson32558652009-04-28 19:41:44 +0000256
David Greenede444af2009-04-22 16:42:54 +0000257 // Now remove it.
258 CurRec->removeValue(SMCTArgs[i]);
259
David Greened34a73b2009-04-24 16:55:41 +0000260 // If a value is specified for this template arg, set it in the
261 // new defs now.
262 for (MultiClass::RecordVector::iterator j =
Bob Wilson440548d2009-04-30 18:26:19 +0000263 CurMC->DefPrototypes.begin() + newDefStart,
264 jend = CurMC->DefPrototypes.end();
David Greenede444af2009-04-22 16:42:54 +0000265 j != jend;
266 ++j) {
267 Record *Def = *j;
268
David Greened34a73b2009-04-24 16:55:41 +0000269 if (SetValue(Def, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000270 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000271 SubMultiClass.TemplateArgs[i]))
272 return true;
273
274 // Resolve it next.
275 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
276
277 // Now remove it
278 Def->removeValue(SMCTArgs[i]);
279 }
280 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
David Greened34a73b2009-04-24 16:55:41 +0000281 return Error(SubMultiClass.RefLoc,
282 "Value not specified for template argument #"
David Greenee22b3212011-10-19 13:02:42 +0000283 + utostr(i) + " (" + SMCTArgs[i]->getAsUnquotedString()
284 + ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greenede444af2009-04-22 16:42:54 +0000285 }
286 }
287
288 return false;
289}
290
David Greenecebb4ee2012-02-22 16:09:41 +0000291/// ProcessForeachDefs - Given a record, apply all of the variable
292/// values in all surrounding foreach loops, creating new records for
293/// each combination of values.
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000294bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
295 if (Loops.empty())
296 return false;
297
David Greenecebb4ee2012-02-22 16:09:41 +0000298 // We want to instantiate a new copy of CurRec for each combination
299 // of nested loop iterator values. We don't want top instantiate
300 // any copies until we have values for each loop iterator.
301 IterSet IterVals;
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000302 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenecebb4ee2012-02-22 16:09:41 +0000303}
304
305/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
306/// apply each of the variable values in this loop and then process
307/// subloops.
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000308bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
309 // Recursively build a tuple of iterator values.
310 if (IterVals.size() != Loops.size()) {
311 assert(IterVals.size() < Loops.size());
312 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silva6cfc8062012-10-10 20:24:43 +0000313 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000314 if (List == 0) {
315 Error(Loc, "Loop list is not a list");
316 return true;
317 }
David Greenecebb4ee2012-02-22 16:09:41 +0000318
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000319 // Process each value.
320 for (int64_t i = 0; i < List->getSize(); ++i) {
321 Init *ItemVal = List->resolveListElementReference(*CurRec, 0, i);
322 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
323 if (ProcessForeachDefs(CurRec, Loc, IterVals))
324 return true;
325 IterVals.pop_back();
326 }
327 return false;
328 }
329
330 // This is the bottom of the recursion. We have all of the iterator values
331 // for this point in the iteration space. Instantiate a new record to
332 // reflect this combination of values.
333 Record *IterRec = new Record(*CurRec);
334
335 // Set the iterator values now.
336 for (unsigned i = 0, e = IterVals.size(); i != e; ++i) {
337 VarInit *IterVar = IterVals[i].IterVar;
Sean Silva6cfc8062012-10-10 20:24:43 +0000338 TypedInit *IVal = dyn_cast<TypedInit>(IterVals[i].IterValue);
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000339 if (IVal == 0) {
340 Error(Loc, "foreach iterator value is untyped");
341 return true;
342 }
343
344 IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false));
345
346 if (SetValue(IterRec, Loc, IterVar->getName(),
347 std::vector<unsigned>(), IVal)) {
348 Error(Loc, "when instantiating this def");
349 return true;
350 }
351
352 // Resolve it next.
353 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName()));
354
355 // Remove it.
356 IterRec->removeValue(IterVar->getName());
357 }
358
359 if (Records.getDef(IterRec->getNameInitAsString())) {
360 Error(Loc, "def already exists: " + IterRec->getNameInitAsString());
David Greenecebb4ee2012-02-22 16:09:41 +0000361 return true;
362 }
363
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000364 Records.addDef(IterRec);
365 IterRec->resolveReferences();
David Greenecebb4ee2012-02-22 16:09:41 +0000366 return false;
367}
368
Chris Lattnerf4601652007-11-22 20:49:04 +0000369//===----------------------------------------------------------------------===//
370// Parser Code
371//===----------------------------------------------------------------------===//
372
373/// isObjectStart - Return true if this is a valid first token for an Object.
374static bool isObjectStart(tgtok::TokKind K) {
375 return K == tgtok::Class || K == tgtok::Def ||
David Greenecebb4ee2012-02-22 16:09:41 +0000376 K == tgtok::Defm || K == tgtok::Let ||
377 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4601652007-11-22 20:49:04 +0000378}
379
Chris Lattnerdf72eae2010-10-05 22:51:56 +0000380static std::string GetNewAnonymousName() {
381 static unsigned AnonCounter = 0;
382 return "anonymous."+utostr(AnonCounter++);
383}
384
Chris Lattnerf4601652007-11-22 20:49:04 +0000385/// ParseObjectName - If an object name is specified, return it. Otherwise,
386/// return an anonymous name.
David Greenea9e07dd2011-10-19 13:04:29 +0000387/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4601652007-11-22 20:49:04 +0000388/// ObjectName ::= /*empty*/
389///
David Greenea9e07dd2011-10-19 13:04:29 +0000390Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
391 switch (Lex.getCode()) {
392 case tgtok::colon:
393 case tgtok::semi:
394 case tgtok::l_brace:
395 // These are all of the tokens that can begin an object body.
396 // Some of these can also begin values but we disallow those cases
397 // because they are unlikely to be useful.
398 return StringInit::get(GetNewAnonymousName());
David Greenea9e07dd2011-10-19 13:04:29 +0000399 default:
400 break;
401 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000402
David Greenea9e07dd2011-10-19 13:04:29 +0000403 Record *CurRec = 0;
404 if (CurMultiClass)
405 CurRec = &CurMultiClass->Rec;
406
407 RecTy *Type = 0;
408 if (CurRec) {
Sean Silva3f7b7f82012-10-10 20:24:47 +0000409 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greenea9e07dd2011-10-19 13:04:29 +0000410 if (!CurRecName) {
411 TokError("Record name is not typed!");
412 return 0;
413 }
414 Type = CurRecName->getType();
415 }
416
417 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4601652007-11-22 20:49:04 +0000418}
419
Chris Lattnerf4601652007-11-22 20:49:04 +0000420/// ParseClassID - Parse and resolve a reference to a class name. This returns
421/// null on error.
422///
423/// ClassID ::= ID
424///
425Record *TGParser::ParseClassID() {
426 if (Lex.getCode() != tgtok::Id) {
427 TokError("expected name for ClassID");
428 return 0;
429 }
Bob Wilson21870412009-11-22 04:24:42 +0000430
Chris Lattnerf4601652007-11-22 20:49:04 +0000431 Record *Result = Records.getClass(Lex.getCurStrVal());
432 if (Result == 0)
433 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson21870412009-11-22 04:24:42 +0000434
Chris Lattnerf4601652007-11-22 20:49:04 +0000435 Lex.Lex();
436 return Result;
437}
438
Bob Wilson32558652009-04-28 19:41:44 +0000439/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
440/// This returns null on error.
David Greenede444af2009-04-22 16:42:54 +0000441///
442/// MultiClassID ::= ID
443///
444MultiClass *TGParser::ParseMultiClassID() {
445 if (Lex.getCode() != tgtok::Id) {
Sean Silva36febfd2013-01-09 02:11:57 +0000446 TokError("expected name for MultiClassID");
David Greenede444af2009-04-22 16:42:54 +0000447 return 0;
448 }
Bob Wilson32558652009-04-28 19:41:44 +0000449
David Greenede444af2009-04-22 16:42:54 +0000450 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
451 if (Result == 0)
Sean Silva36febfd2013-01-09 02:11:57 +0000452 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson32558652009-04-28 19:41:44 +0000453
David Greenede444af2009-04-22 16:42:54 +0000454 Lex.Lex();
455 return Result;
456}
457
Chris Lattnerf4601652007-11-22 20:49:04 +0000458Record *TGParser::ParseDefmID() {
Sean Silva9d4a6612013-01-09 02:17:13 +0000459 MultiClass *MC = ParseMultiClassID();
460 if (!MC)
Chris Lattnerf4601652007-11-22 20:49:04 +0000461 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +0000462 return &MC->Rec;
Bob Wilson21870412009-11-22 04:24:42 +0000463}
Chris Lattnerf4601652007-11-22 20:49:04 +0000464
465
466/// ParseSubClassReference - Parse a reference to a subclass or to a templated
467/// subclass. This returns a SubClassRefTy with a null Record* on error.
468///
469/// SubClassRef ::= ClassID
470/// SubClassRef ::= ClassID '<' ValueList '>'
471///
472SubClassReference TGParser::
473ParseSubClassReference(Record *CurRec, bool isDefm) {
474 SubClassReference Result;
475 Result.RefLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +0000476
Chris Lattnerf4601652007-11-22 20:49:04 +0000477 if (isDefm)
478 Result.Rec = ParseDefmID();
479 else
480 Result.Rec = ParseClassID();
481 if (Result.Rec == 0) return Result;
Bob Wilson21870412009-11-22 04:24:42 +0000482
Chris Lattnerf4601652007-11-22 20:49:04 +0000483 // If there is no template arg list, we're done.
484 if (Lex.getCode() != tgtok::less)
485 return Result;
486 Lex.Lex(); // Eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000487
Chris Lattnerf4601652007-11-22 20:49:04 +0000488 if (Lex.getCode() == tgtok::greater) {
489 TokError("subclass reference requires a non-empty list of template values");
490 Result.Rec = 0;
491 return Result;
492 }
Bob Wilson21870412009-11-22 04:24:42 +0000493
David Greenee1b46912009-06-08 20:23:18 +0000494 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4601652007-11-22 20:49:04 +0000495 if (Result.TemplateArgs.empty()) {
496 Result.Rec = 0; // Error parsing value list.
497 return Result;
498 }
Bob Wilson21870412009-11-22 04:24:42 +0000499
Chris Lattnerf4601652007-11-22 20:49:04 +0000500 if (Lex.getCode() != tgtok::greater) {
501 TokError("expected '>' in template value list");
502 Result.Rec = 0;
503 return Result;
504 }
505 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000506
Chris Lattnerf4601652007-11-22 20:49:04 +0000507 return Result;
508}
509
Bob Wilson32558652009-04-28 19:41:44 +0000510/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
511/// templated submulticlass. This returns a SubMultiClassRefTy with a null
512/// Record* on error.
David Greenede444af2009-04-22 16:42:54 +0000513///
514/// SubMultiClassRef ::= MultiClassID
515/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
516///
517SubMultiClassReference TGParser::
518ParseSubMultiClassReference(MultiClass *CurMC) {
519 SubMultiClassReference Result;
520 Result.RefLoc = Lex.getLoc();
Bob Wilson32558652009-04-28 19:41:44 +0000521
David Greenede444af2009-04-22 16:42:54 +0000522 Result.MC = ParseMultiClassID();
523 if (Result.MC == 0) return Result;
Bob Wilson32558652009-04-28 19:41:44 +0000524
David Greenede444af2009-04-22 16:42:54 +0000525 // If there is no template arg list, we're done.
526 if (Lex.getCode() != tgtok::less)
527 return Result;
528 Lex.Lex(); // Eat the '<'
Bob Wilson32558652009-04-28 19:41:44 +0000529
David Greenede444af2009-04-22 16:42:54 +0000530 if (Lex.getCode() == tgtok::greater) {
531 TokError("subclass reference requires a non-empty list of template values");
532 Result.MC = 0;
533 return Result;
534 }
Bob Wilson32558652009-04-28 19:41:44 +0000535
David Greenee1b46912009-06-08 20:23:18 +0000536 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greenede444af2009-04-22 16:42:54 +0000537 if (Result.TemplateArgs.empty()) {
538 Result.MC = 0; // Error parsing value list.
539 return Result;
540 }
Bob Wilson32558652009-04-28 19:41:44 +0000541
David Greenede444af2009-04-22 16:42:54 +0000542 if (Lex.getCode() != tgtok::greater) {
543 TokError("expected '>' in template value list");
544 Result.MC = 0;
545 return Result;
546 }
547 Lex.Lex();
548
549 return Result;
550}
551
Chris Lattnerf4601652007-11-22 20:49:04 +0000552/// ParseRangePiece - Parse a bit/value range.
553/// RangePiece ::= INTVAL
554/// RangePiece ::= INTVAL '-' INTVAL
555/// RangePiece ::= INTVAL INTVAL
556bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner811281e2008-01-10 07:01:53 +0000557 if (Lex.getCode() != tgtok::IntVal) {
558 TokError("expected integer or bitrange");
559 return true;
560 }
Dan Gohman63f97202008-10-17 01:33:43 +0000561 int64_t Start = Lex.getCurIntVal();
562 int64_t End;
Bob Wilson21870412009-11-22 04:24:42 +0000563
Chris Lattnerf4601652007-11-22 20:49:04 +0000564 if (Start < 0)
565 return TokError("invalid range, cannot be negative");
Bob Wilson21870412009-11-22 04:24:42 +0000566
Chris Lattnerf4601652007-11-22 20:49:04 +0000567 switch (Lex.Lex()) { // eat first character.
Bob Wilson21870412009-11-22 04:24:42 +0000568 default:
Chris Lattnerf4601652007-11-22 20:49:04 +0000569 Ranges.push_back(Start);
570 return false;
571 case tgtok::minus:
572 if (Lex.Lex() != tgtok::IntVal) {
573 TokError("expected integer value as end of range");
574 return true;
575 }
576 End = Lex.getCurIntVal();
577 break;
578 case tgtok::IntVal:
579 End = -Lex.getCurIntVal();
580 break;
581 }
Bob Wilson21870412009-11-22 04:24:42 +0000582 if (End < 0)
Chris Lattnerf4601652007-11-22 20:49:04 +0000583 return TokError("invalid range, cannot be negative");
584 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000585
Chris Lattnerf4601652007-11-22 20:49:04 +0000586 // Add to the range.
587 if (Start < End) {
588 for (; Start <= End; ++Start)
589 Ranges.push_back(Start);
590 } else {
591 for (; Start >= End; --Start)
592 Ranges.push_back(Start);
593 }
594 return false;
595}
596
597/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
598///
599/// RangeList ::= RangePiece (',' RangePiece)*
600///
601std::vector<unsigned> TGParser::ParseRangeList() {
602 std::vector<unsigned> Result;
Bob Wilson21870412009-11-22 04:24:42 +0000603
Chris Lattnerf4601652007-11-22 20:49:04 +0000604 // Parse the first piece.
605 if (ParseRangePiece(Result))
606 return std::vector<unsigned>();
607 while (Lex.getCode() == tgtok::comma) {
608 Lex.Lex(); // Eat the comma.
609
610 // Parse the next range piece.
611 if (ParseRangePiece(Result))
612 return std::vector<unsigned>();
613 }
614 return Result;
615}
616
617/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
618/// OptionalRangeList ::= '<' RangeList '>'
619/// OptionalRangeList ::= /*empty*/
620bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
621 if (Lex.getCode() != tgtok::less)
622 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000623
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000624 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000625 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000626
Chris Lattnerf4601652007-11-22 20:49:04 +0000627 // Parse the range list.
628 Ranges = ParseRangeList();
629 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000630
Chris Lattnerf4601652007-11-22 20:49:04 +0000631 if (Lex.getCode() != tgtok::greater) {
632 TokError("expected '>' at end of range list");
633 return Error(StartLoc, "to match this '<'");
634 }
635 Lex.Lex(); // eat the '>'.
636 return false;
637}
638
639/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
640/// OptionalBitList ::= '{' RangeList '}'
641/// OptionalBitList ::= /*empty*/
642bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
643 if (Lex.getCode() != tgtok::l_brace)
644 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000645
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000646 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000647 Lex.Lex(); // eat the '{'
Bob Wilson21870412009-11-22 04:24:42 +0000648
Chris Lattnerf4601652007-11-22 20:49:04 +0000649 // Parse the range list.
650 Ranges = ParseRangeList();
651 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000652
Chris Lattnerf4601652007-11-22 20:49:04 +0000653 if (Lex.getCode() != tgtok::r_brace) {
654 TokError("expected '}' at end of bit list");
655 return Error(StartLoc, "to match this '{'");
656 }
657 Lex.Lex(); // eat the '}'.
658 return false;
659}
660
661
662/// ParseType - Parse and return a tblgen type. This returns null on error.
663///
664/// Type ::= STRING // string type
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +0000665/// Type ::= CODE // code type
Chris Lattnerf4601652007-11-22 20:49:04 +0000666/// Type ::= BIT // bit type
667/// Type ::= BITS '<' INTVAL '>' // bits<x> type
668/// Type ::= INT // int type
669/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4601652007-11-22 20:49:04 +0000670/// Type ::= DAG // dag type
671/// Type ::= ClassID // Record Type
672///
673RecTy *TGParser::ParseType() {
674 switch (Lex.getCode()) {
675 default: TokError("Unknown token when expecting a type"); return 0;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000676 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +0000677 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000678 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
679 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000680 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4601652007-11-22 20:49:04 +0000681 case tgtok::Id:
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000682 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Chris Lattnerf4601652007-11-22 20:49:04 +0000683 return 0;
684 case tgtok::Bits: {
685 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
686 TokError("expected '<' after bits type");
687 return 0;
688 }
689 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
690 TokError("expected integer in bits<n> type");
691 return 0;
692 }
Dan Gohman63f97202008-10-17 01:33:43 +0000693 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4601652007-11-22 20:49:04 +0000694 if (Lex.Lex() != tgtok::greater) { // Eat count.
695 TokError("expected '>' at end of bits<n> type");
696 return 0;
697 }
698 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000699 return BitsRecTy::get(Val);
Chris Lattnerf4601652007-11-22 20:49:04 +0000700 }
701 case tgtok::List: {
702 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
703 TokError("expected '<' after list type");
704 return 0;
705 }
706 Lex.Lex(); // Eat '<'
707 RecTy *SubType = ParseType();
708 if (SubType == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +0000709
Chris Lattnerf4601652007-11-22 20:49:04 +0000710 if (Lex.getCode() != tgtok::greater) {
711 TokError("expected '>' at end of list<ty> type");
712 return 0;
713 }
714 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000715 return ListRecTy::get(SubType);
Chris Lattnerf4601652007-11-22 20:49:04 +0000716 }
Bob Wilson21870412009-11-22 04:24:42 +0000717 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000718}
719
720/// ParseIDValue - Parse an ID as a value and decode what it means.
721///
722/// IDValue ::= ID [def local value]
723/// IDValue ::= ID [def template arg]
724/// IDValue ::= ID [multiclass local value]
725/// IDValue ::= ID [multiclass template argument]
726/// IDValue ::= ID [def name]
727///
David Greenef3744a02011-10-19 13:04:20 +0000728Init *TGParser::ParseIDValue(Record *CurRec, IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000729 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
730 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000731 SMLoc Loc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000732 Lex.Lex();
733 return ParseIDValue(CurRec, Name, Loc);
734}
735
736/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
737/// has already been read.
David Greene05bce0b2011-07-29 22:43:06 +0000738Init *TGParser::ParseIDValue(Record *CurRec,
David Greenef3744a02011-10-19 13:04:20 +0000739 const std::string &Name, SMLoc NameLoc,
740 IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000741 if (CurRec) {
742 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenedcd35c72011-07-29 19:07:07 +0000743 return VarInit::get(Name, RV->getType());
Bob Wilson21870412009-11-22 04:24:42 +0000744
David Greenee22b3212011-10-19 13:02:42 +0000745 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
746
David Greenecaa25c82011-10-05 22:42:54 +0000747 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +0000748 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
749 "::");
David Greenecaa25c82011-10-05 22:42:54 +0000750
Chris Lattnerf4601652007-11-22 20:49:04 +0000751 if (CurRec->isTemplateArg(TemplateArgName)) {
752 const RecordVal *RV = CurRec->getValue(TemplateArgName);
753 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000754 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000755 }
756 }
Bob Wilson21870412009-11-22 04:24:42 +0000757
Chris Lattnerf4601652007-11-22 20:49:04 +0000758 if (CurMultiClass) {
David Greenee22b3212011-10-19 13:02:42 +0000759 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
760 "::");
761
Chris Lattnerf4601652007-11-22 20:49:04 +0000762 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
763 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
764 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000765 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000766 }
767 }
Bob Wilson21870412009-11-22 04:24:42 +0000768
David Greenecebb4ee2012-02-22 16:09:41 +0000769 // If this is in a foreach loop, make sure it's not a loop iterator
770 for (LoopVector::iterator i = Loops.begin(), iend = Loops.end();
771 i != iend;
772 ++i) {
Sean Silva6cfc8062012-10-10 20:24:43 +0000773 VarInit *IterVar = dyn_cast<VarInit>(i->IterVar);
David Greenecebb4ee2012-02-22 16:09:41 +0000774 if (IterVar && IterVar->getName() == Name)
775 return IterVar;
776 }
777
David Greenebbec2792011-10-19 13:04:21 +0000778 if (Mode == ParseNameMode)
779 return StringInit::get(Name);
780
Chris Lattnerf4601652007-11-22 20:49:04 +0000781 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000782 return DefInit::get(D);
Chris Lattnerf4601652007-11-22 20:49:04 +0000783
David Greenebbec2792011-10-19 13:04:21 +0000784 if (Mode == ParseValueMode) {
785 Error(NameLoc, "Variable not defined: '" + Name + "'");
786 return 0;
787 }
788
789 return StringInit::get(Name);
Chris Lattnerf4601652007-11-22 20:49:04 +0000790}
791
David Greened418c1b2009-05-14 20:54:48 +0000792/// ParseOperation - Parse an operator. This returns null on error.
793///
794/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
795///
David Greene05bce0b2011-07-29 22:43:06 +0000796Init *TGParser::ParseOperation(Record *CurRec) {
David Greened418c1b2009-05-14 20:54:48 +0000797 switch (Lex.getCode()) {
798 default:
799 TokError("unknown operation");
800 return 0;
David Greene1434f662011-01-07 17:05:37 +0000801 case tgtok::XHead:
802 case tgtok::XTail:
803 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +0000804 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
805 UnOpInit::UnaryOp Code;
806 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000807
David Greenee6c27de2009-05-14 21:22:49 +0000808 switch (Lex.getCode()) {
Craig Topper85814382012-02-07 05:05:23 +0000809 default: llvm_unreachable("Unhandled code!");
David Greenee6c27de2009-05-14 21:22:49 +0000810 case tgtok::XCast:
811 Lex.Lex(); // eat the operation
812 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000813
David Greenee6c27de2009-05-14 21:22:49 +0000814 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000815
David Greenee6c27de2009-05-14 21:22:49 +0000816 if (Type == 0) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000817 TokError("did not get type for unary operator");
David Greenee6c27de2009-05-14 21:22:49 +0000818 return 0;
819 }
David Greened418c1b2009-05-14 20:54:48 +0000820
David Greenee6c27de2009-05-14 21:22:49 +0000821 break;
David Greene1434f662011-01-07 17:05:37 +0000822 case tgtok::XHead:
David Greene5f9f9ba2009-05-14 22:38:31 +0000823 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000824 Code = UnOpInit::HEAD;
David Greene5f9f9ba2009-05-14 22:38:31 +0000825 break;
David Greene1434f662011-01-07 17:05:37 +0000826 case tgtok::XTail:
David Greene5f9f9ba2009-05-14 22:38:31 +0000827 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000828 Code = UnOpInit::TAIL;
David Greene5f9f9ba2009-05-14 22:38:31 +0000829 break;
David Greene1434f662011-01-07 17:05:37 +0000830 case tgtok::XEmpty:
David Greene5f9f9ba2009-05-14 22:38:31 +0000831 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000832 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000833 Type = IntRecTy::get();
David Greene5f9f9ba2009-05-14 22:38:31 +0000834 break;
David Greenee6c27de2009-05-14 21:22:49 +0000835 }
836 if (Lex.getCode() != tgtok::l_paren) {
837 TokError("expected '(' after unary operator");
838 return 0;
839 }
840 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000841
David Greene05bce0b2011-07-29 22:43:06 +0000842 Init *LHS = ParseValue(CurRec);
David Greenee6c27de2009-05-14 21:22:49 +0000843 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000844
David Greene1434f662011-01-07 17:05:37 +0000845 if (Code == UnOpInit::HEAD
846 || Code == UnOpInit::TAIL
847 || Code == UnOpInit::EMPTY) {
Sean Silva6cfc8062012-10-10 20:24:43 +0000848 ListInit *LHSl = dyn_cast<ListInit>(LHS);
849 StringInit *LHSs = dyn_cast<StringInit>(LHS);
850 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000851 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
852 TokError("expected list or string type argument in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000853 return 0;
854 }
855 if (LHSt) {
Sean Silva736ceac2012-10-05 03:31:58 +0000856 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
857 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
David Greenee1b46912009-06-08 20:23:18 +0000858 if (LType == 0 && SType == 0) {
859 TokError("expected list or string type argumnet in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000860 return 0;
861 }
862 }
863
David Greene1434f662011-01-07 17:05:37 +0000864 if (Code == UnOpInit::HEAD
865 || Code == UnOpInit::TAIL) {
David Greenee1b46912009-06-08 20:23:18 +0000866 if (LHSl == 0 && LHSt == 0) {
867 TokError("expected list type argumnet in unary operator");
868 return 0;
869 }
Bob Wilson21870412009-11-22 04:24:42 +0000870
David Greene5f9f9ba2009-05-14 22:38:31 +0000871 if (LHSl && LHSl->getSize() == 0) {
872 TokError("empty list argument in unary operator");
873 return 0;
874 }
875 if (LHSl) {
David Greene05bce0b2011-07-29 22:43:06 +0000876 Init *Item = LHSl->getElement(0);
Sean Silva6cfc8062012-10-10 20:24:43 +0000877 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
David Greene5f9f9ba2009-05-14 22:38:31 +0000878 if (Itemt == 0) {
879 TokError("untyped list element in unary operator");
880 return 0;
881 }
David Greene1434f662011-01-07 17:05:37 +0000882 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000883 Type = Itemt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000884 } else {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000885 Type = ListRecTy::get(Itemt->getType());
David Greene5f9f9ba2009-05-14 22:38:31 +0000886 }
Bob Wilson21870412009-11-22 04:24:42 +0000887 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000888 assert(LHSt && "expected list type argument in unary operator");
Sean Silva736ceac2012-10-05 03:31:58 +0000889 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
David Greene5f9f9ba2009-05-14 22:38:31 +0000890 if (LType == 0) {
891 TokError("expected list type argumnet in unary operator");
892 return 0;
893 }
David Greene1434f662011-01-07 17:05:37 +0000894 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000895 Type = LType->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +0000896 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000897 Type = LType;
898 }
899 }
900 }
901 }
902
David Greenee6c27de2009-05-14 21:22:49 +0000903 if (Lex.getCode() != tgtok::r_paren) {
904 TokError("expected ')' in unary operator");
905 return 0;
906 }
907 Lex.Lex(); // eat the ')'
David Greenedcd35c72011-07-29 19:07:07 +0000908 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee6c27de2009-05-14 21:22:49 +0000909 }
David Greened418c1b2009-05-14 20:54:48 +0000910
911 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +0000912 case tgtok::XSRA:
David Greened418c1b2009-05-14 20:54:48 +0000913 case tgtok::XSRL:
914 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +0000915 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +0000916 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000917 tgtok::TokKind OpTok = Lex.getCode();
918 SMLoc OpLoc = Lex.getLoc();
919 Lex.Lex(); // eat the operation
920
David Greened418c1b2009-05-14 20:54:48 +0000921 BinOpInit::BinaryOp Code;
922 RecTy *Type = 0;
923
Chris Lattner8d978a72010-10-05 23:58:18 +0000924 switch (OpTok) {
Craig Topper85814382012-02-07 05:05:23 +0000925 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000926 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
927 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
928 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
929 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
930 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Bob Wilson21870412009-11-22 04:24:42 +0000931 case tgtok::XStrConcat:
David Greened418c1b2009-05-14 20:54:48 +0000932 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000933 Type = StringRecTy::get();
David Greened418c1b2009-05-14 20:54:48 +0000934 break;
David Greened418c1b2009-05-14 20:54:48 +0000935 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000936
David Greened418c1b2009-05-14 20:54:48 +0000937 if (Lex.getCode() != tgtok::l_paren) {
938 TokError("expected '(' after binary operator");
939 return 0;
940 }
941 Lex.Lex(); // eat the '('
942
David Greene05bce0b2011-07-29 22:43:06 +0000943 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000944
Chris Lattner8d978a72010-10-05 23:58:18 +0000945 InitList.push_back(ParseValue(CurRec));
946 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000947
Chris Lattner8d978a72010-10-05 23:58:18 +0000948 while (Lex.getCode() == tgtok::comma) {
949 Lex.Lex(); // eat the ','
950
951 InitList.push_back(ParseValue(CurRec));
952 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000953 }
David Greened418c1b2009-05-14 20:54:48 +0000954
955 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner8d978a72010-10-05 23:58:18 +0000956 TokError("expected ')' in operator");
David Greened418c1b2009-05-14 20:54:48 +0000957 return 0;
958 }
959 Lex.Lex(); // eat the ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000960
961 // We allow multiple operands to associative operators like !strconcat as
962 // shorthand for nesting them.
963 if (Code == BinOpInit::STRCONCAT) {
964 while (InitList.size() > 2) {
David Greene05bce0b2011-07-29 22:43:06 +0000965 Init *RHS = InitList.pop_back_val();
David Greenedcd35c72011-07-29 19:07:07 +0000966 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
967 ->Fold(CurRec, CurMultiClass);
Chris Lattner8d978a72010-10-05 23:58:18 +0000968 InitList.back() = RHS;
969 }
970 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000971
Chris Lattner8d978a72010-10-05 23:58:18 +0000972 if (InitList.size() == 2)
David Greenedcd35c72011-07-29 19:07:07 +0000973 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner8d978a72010-10-05 23:58:18 +0000974 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000975
Chris Lattner8d978a72010-10-05 23:58:18 +0000976 Error(OpLoc, "expected two operands to operator");
977 return 0;
David Greened418c1b2009-05-14 20:54:48 +0000978 }
979
David Greene9bea7c82009-05-14 23:26:46 +0000980 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +0000981 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +0000982 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
983 TernOpInit::TernaryOp Code;
984 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000985
David Greene4afc5092009-05-14 21:54:42 +0000986 tgtok::TokKind LexCode = Lex.getCode();
987 Lex.Lex(); // eat the operation
988 switch (LexCode) {
Craig Topper85814382012-02-07 05:05:23 +0000989 default: llvm_unreachable("Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000990 case tgtok::XIf:
991 Code = TernOpInit::IF;
992 break;
David Greenebeb31a52009-05-14 22:23:47 +0000993 case tgtok::XForEach:
994 Code = TernOpInit::FOREACH;
995 break;
David Greene4afc5092009-05-14 21:54:42 +0000996 case tgtok::XSubst:
997 Code = TernOpInit::SUBST;
998 break;
999 }
1000 if (Lex.getCode() != tgtok::l_paren) {
1001 TokError("expected '(' after ternary operator");
1002 return 0;
1003 }
1004 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +00001005
David Greene05bce0b2011-07-29 22:43:06 +00001006 Init *LHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001007 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001008
David Greene4afc5092009-05-14 21:54:42 +00001009 if (Lex.getCode() != tgtok::comma) {
1010 TokError("expected ',' in ternary operator");
1011 return 0;
1012 }
1013 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001014
David Greene05bce0b2011-07-29 22:43:06 +00001015 Init *MHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001016 if (MHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001017
David Greene4afc5092009-05-14 21:54:42 +00001018 if (Lex.getCode() != tgtok::comma) {
1019 TokError("expected ',' in ternary operator");
1020 return 0;
1021 }
1022 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001023
David Greene05bce0b2011-07-29 22:43:06 +00001024 Init *RHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001025 if (RHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001026
David Greene4afc5092009-05-14 21:54:42 +00001027 if (Lex.getCode() != tgtok::r_paren) {
1028 TokError("expected ')' in binary operator");
1029 return 0;
1030 }
1031 Lex.Lex(); // eat the ')'
David Greened418c1b2009-05-14 20:54:48 +00001032
David Greene4afc5092009-05-14 21:54:42 +00001033 switch (LexCode) {
Craig Topper85814382012-02-07 05:05:23 +00001034 default: llvm_unreachable("Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +00001035 case tgtok::XIf: {
Bill Wendling548f5a02010-12-13 01:46:19 +00001036 RecTy *MHSTy = 0;
1037 RecTy *RHSTy = 0;
1038
Sean Silva6cfc8062012-10-10 20:24:43 +00001039 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling548f5a02010-12-13 01:46:19 +00001040 MHSTy = MHSt->getType();
Sean Silva6cfc8062012-10-10 20:24:43 +00001041 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao307525c2012-09-06 23:32:48 +00001042 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva3f7b7f82012-10-10 20:24:47 +00001043 if (isa<BitInit>(MHS))
Michael Liao307525c2012-09-06 23:32:48 +00001044 MHSTy = BitRecTy::get();
1045
Sean Silva6cfc8062012-10-10 20:24:43 +00001046 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling548f5a02010-12-13 01:46:19 +00001047 RHSTy = RHSt->getType();
Sean Silva6cfc8062012-10-10 20:24:43 +00001048 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao307525c2012-09-06 23:32:48 +00001049 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva3f7b7f82012-10-10 20:24:47 +00001050 if (isa<BitInit>(RHS))
Michael Liao307525c2012-09-06 23:32:48 +00001051 RHSTy = BitRecTy::get();
1052
1053 // For UnsetInit, it's typed from the other hand.
Sean Silva3f7b7f82012-10-10 20:24:47 +00001054 if (isa<UnsetInit>(MHS))
Michael Liao307525c2012-09-06 23:32:48 +00001055 MHSTy = RHSTy;
Sean Silva3f7b7f82012-10-10 20:24:47 +00001056 if (isa<UnsetInit>(RHS))
Michael Liao307525c2012-09-06 23:32:48 +00001057 RHSTy = MHSTy;
Bill Wendling548f5a02010-12-13 01:46:19 +00001058
1059 if (!MHSTy || !RHSTy) {
David Greene9bea7c82009-05-14 23:26:46 +00001060 TokError("could not get type for !if");
1061 return 0;
1062 }
Bill Wendling548f5a02010-12-13 01:46:19 +00001063
1064 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1065 Type = RHSTy;
1066 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1067 Type = MHSTy;
Bob Wilson21870412009-11-22 04:24:42 +00001068 } else {
David Greene9bea7c82009-05-14 23:26:46 +00001069 TokError("inconsistent types for !if");
1070 return 0;
1071 }
1072 break;
1073 }
David Greenebeb31a52009-05-14 22:23:47 +00001074 case tgtok::XForEach: {
Sean Silva6cfc8062012-10-10 20:24:43 +00001075 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
David Greenebeb31a52009-05-14 22:23:47 +00001076 if (MHSt == 0) {
1077 TokError("could not get type for !foreach");
1078 return 0;
1079 }
1080 Type = MHSt->getType();
1081 break;
1082 }
David Greene4afc5092009-05-14 21:54:42 +00001083 case tgtok::XSubst: {
Sean Silva6cfc8062012-10-10 20:24:43 +00001084 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
David Greene4afc5092009-05-14 21:54:42 +00001085 if (RHSt == 0) {
1086 TokError("could not get type for !subst");
1087 return 0;
1088 }
1089 Type = RHSt->getType();
1090 break;
1091 }
1092 }
David Greenedcd35c72011-07-29 19:07:07 +00001093 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson21870412009-11-22 04:24:42 +00001094 CurMultiClass);
David Greene4afc5092009-05-14 21:54:42 +00001095 }
David Greened418c1b2009-05-14 20:54:48 +00001096 }
David Greened418c1b2009-05-14 20:54:48 +00001097}
1098
1099/// ParseOperatorType - Parse a type for an operator. This returns
1100/// null on error.
1101///
1102/// OperatorType ::= '<' Type '>'
1103///
Dan Gohmana9ad0412009-08-12 22:10:57 +00001104RecTy *TGParser::ParseOperatorType() {
David Greened418c1b2009-05-14 20:54:48 +00001105 RecTy *Type = 0;
1106
1107 if (Lex.getCode() != tgtok::less) {
1108 TokError("expected type name for operator");
1109 return 0;
1110 }
1111 Lex.Lex(); // eat the <
1112
1113 Type = ParseType();
1114
1115 if (Type == 0) {
1116 TokError("expected type name for operator");
1117 return 0;
1118 }
1119
1120 if (Lex.getCode() != tgtok::greater) {
1121 TokError("expected type name for operator");
1122 return 0;
1123 }
1124 Lex.Lex(); // eat the >
1125
1126 return Type;
1127}
1128
1129
Chris Lattnerf4601652007-11-22 20:49:04 +00001130/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1131///
1132/// SimpleValue ::= IDValue
1133/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001134/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +00001135/// SimpleValue ::= CODEFRAGMENT
1136/// SimpleValue ::= '?'
1137/// SimpleValue ::= '{' ValueList '}'
1138/// SimpleValue ::= ID '<' ValueListNE '>'
1139/// SimpleValue ::= '[' ValueList ']'
1140/// SimpleValue ::= '(' IDValue DagArgList ')'
1141/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
1142/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1143/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1144/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1145/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1146///
David Greenef3744a02011-10-19 13:04:20 +00001147Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1148 IDParseMode Mode) {
David Greene05bce0b2011-07-29 22:43:06 +00001149 Init *R = 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001150 switch (Lex.getCode()) {
1151 default: TokError("Unknown token when parsing a value"); break;
David Greened3d1cad2011-10-19 13:04:43 +00001152 case tgtok::paste:
1153 // This is a leading paste operation. This is deprecated but
1154 // still exists in some .td files. Ignore it.
1155 Lex.Lex(); // Skip '#'.
1156 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenedcd35c72011-07-29 19:07:07 +00001157 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001158 case tgtok::StrVal: {
1159 std::string Val = Lex.getCurStrVal();
1160 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001161
Jim Grosbachda4231f2009-03-26 16:17:51 +00001162 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001163 while (Lex.getCode() == tgtok::StrVal) {
1164 Val += Lex.getCurStrVal();
1165 Lex.Lex();
1166 }
Bob Wilson21870412009-11-22 04:24:42 +00001167
David Greenedcd35c72011-07-29 19:07:07 +00001168 R = StringInit::get(Val);
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001169 break;
1170 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001171 case tgtok::CodeFragment:
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +00001172 R = StringInit::get(Lex.getCurStrVal());
Chris Lattner578bcf02010-10-06 04:31:40 +00001173 Lex.Lex();
1174 break;
1175 case tgtok::question:
David Greenedcd35c72011-07-29 19:07:07 +00001176 R = UnsetInit::get();
Chris Lattner578bcf02010-10-06 04:31:40 +00001177 Lex.Lex();
1178 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001179 case tgtok::Id: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001180 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001181 std::string Name = Lex.getCurStrVal();
1182 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greenef3744a02011-10-19 13:04:20 +00001183 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson21870412009-11-22 04:24:42 +00001184
Chris Lattnerf4601652007-11-22 20:49:04 +00001185 // Value ::= ID '<' ValueListNE '>'
1186 if (Lex.Lex() == tgtok::greater) {
1187 TokError("expected non-empty value list");
1188 return 0;
1189 }
David Greenee1b46912009-06-08 20:23:18 +00001190
Chris Lattnerf4601652007-11-22 20:49:04 +00001191 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1192 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1193 // body.
1194 Record *Class = Records.getClass(Name);
1195 if (!Class) {
1196 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1197 return 0;
1198 }
David Greenee1b46912009-06-08 20:23:18 +00001199
David Greene05bce0b2011-07-29 22:43:06 +00001200 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
David Greenee1b46912009-06-08 20:23:18 +00001201 if (ValueList.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001202
David Greenee1b46912009-06-08 20:23:18 +00001203 if (Lex.getCode() != tgtok::greater) {
1204 TokError("expected '>' at end of value list");
1205 return 0;
1206 }
1207 Lex.Lex(); // eat the '>'
Bob Wilson21870412009-11-22 04:24:42 +00001208
Chris Lattnerf4601652007-11-22 20:49:04 +00001209 // Create the new record, set it as CurRec temporarily.
1210 static unsigned AnonCounter = 0;
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001211 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),
1212 NameLoc,
1213 Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001214 SubClassReference SCRef;
1215 SCRef.RefLoc = NameLoc;
1216 SCRef.Rec = Class;
1217 SCRef.TemplateArgs = ValueList;
1218 // Add info about the subclass to NewRec.
1219 if (AddSubClass(NewRec, SCRef))
1220 return 0;
1221 NewRec->resolveReferences();
1222 Records.addDef(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001223
Chris Lattnerf4601652007-11-22 20:49:04 +00001224 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +00001225 return DefInit::get(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001226 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001227 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001228 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001229 Lex.Lex(); // eat the '{'
David Greene05bce0b2011-07-29 22:43:06 +00001230 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001231
Chris Lattnerf4601652007-11-22 20:49:04 +00001232 if (Lex.getCode() != tgtok::r_brace) {
1233 Vals = ParseValueList(CurRec);
1234 if (Vals.empty()) return 0;
1235 }
1236 if (Lex.getCode() != tgtok::r_brace) {
1237 TokError("expected '}' at end of bit list value");
1238 return 0;
1239 }
1240 Lex.Lex(); // eat the '}'
Bob Wilson21870412009-11-22 04:24:42 +00001241
David Greene05bce0b2011-07-29 22:43:06 +00001242 SmallVector<Init *, 16> NewBits(Vals.size());
David Greeneca7fd3d2011-07-29 19:07:00 +00001243
Chris Lattnerf4601652007-11-22 20:49:04 +00001244 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001245 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Chris Lattnerf4601652007-11-22 20:49:04 +00001246 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +00001247 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1248 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +00001249 return 0;
1250 }
David Greeneca7fd3d2011-07-29 19:07:00 +00001251 NewBits[Vals.size()-i-1] = Bit;
Chris Lattnerf4601652007-11-22 20:49:04 +00001252 }
David Greenedcd35c72011-07-29 19:07:07 +00001253 return BitsInit::get(NewBits);
Chris Lattnerf4601652007-11-22 20:49:04 +00001254 }
1255 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1256 Lex.Lex(); // eat the '['
David Greene05bce0b2011-07-29 22:43:06 +00001257 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001258
David Greenee1b46912009-06-08 20:23:18 +00001259 RecTy *DeducedEltTy = 0;
1260 ListRecTy *GivenListTy = 0;
Bob Wilson21870412009-11-22 04:24:42 +00001261
David Greenee1b46912009-06-08 20:23:18 +00001262 if (ItemType != 0) {
Sean Silva736ceac2012-10-05 03:31:58 +00001263 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
David Greenee1b46912009-06-08 20:23:18 +00001264 if (ListType == 0) {
1265 std::stringstream s;
Bob Wilson21870412009-11-22 04:24:42 +00001266 s << "Type mismatch for list, expected list type, got "
David Greenee1b46912009-06-08 20:23:18 +00001267 << ItemType->getAsString();
1268 TokError(s.str());
Jim Grosbach6a44ada2011-03-11 19:52:52 +00001269 return 0;
David Greenee1b46912009-06-08 20:23:18 +00001270 }
1271 GivenListTy = ListType;
Bob Wilson21870412009-11-22 04:24:42 +00001272 }
David Greenee1b46912009-06-08 20:23:18 +00001273
Chris Lattnerf4601652007-11-22 20:49:04 +00001274 if (Lex.getCode() != tgtok::r_square) {
Bob Wilson21870412009-11-22 04:24:42 +00001275 Vals = ParseValueList(CurRec, 0,
1276 GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001277 if (Vals.empty()) return 0;
1278 }
1279 if (Lex.getCode() != tgtok::r_square) {
1280 TokError("expected ']' at end of list value");
1281 return 0;
1282 }
1283 Lex.Lex(); // eat the ']'
David Greenee1b46912009-06-08 20:23:18 +00001284
1285 RecTy *GivenEltTy = 0;
1286 if (Lex.getCode() == tgtok::less) {
1287 // Optional list element type
1288 Lex.Lex(); // eat the '<'
1289
1290 GivenEltTy = ParseType();
1291 if (GivenEltTy == 0) {
1292 // Couldn't parse element type
1293 return 0;
1294 }
1295
1296 if (Lex.getCode() != tgtok::greater) {
1297 TokError("expected '>' at end of list element type");
1298 return 0;
1299 }
1300 Lex.Lex(); // eat the '>'
1301 }
1302
1303 // Check elements
1304 RecTy *EltTy = 0;
David Greene05bce0b2011-07-29 22:43:06 +00001305 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greenee1b46912009-06-08 20:23:18 +00001306 i != ie;
1307 ++i) {
Sean Silva6cfc8062012-10-10 20:24:43 +00001308 TypedInit *TArg = dyn_cast<TypedInit>(*i);
David Greenee1b46912009-06-08 20:23:18 +00001309 if (TArg == 0) {
1310 TokError("Untyped list element");
1311 return 0;
1312 }
1313 if (EltTy != 0) {
1314 EltTy = resolveTypes(EltTy, TArg->getType());
1315 if (EltTy == 0) {
1316 TokError("Incompatible types in list elements");
1317 return 0;
1318 }
Bob Wilson21870412009-11-22 04:24:42 +00001319 } else {
David Greenee1b46912009-06-08 20:23:18 +00001320 EltTy = TArg->getType();
1321 }
1322 }
1323
1324 if (GivenEltTy != 0) {
1325 if (EltTy != 0) {
1326 // Verify consistency
1327 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1328 TokError("Incompatible types in list elements");
1329 return 0;
1330 }
1331 }
1332 EltTy = GivenEltTy;
1333 }
1334
1335 if (EltTy == 0) {
1336 if (ItemType == 0) {
1337 TokError("No type for list");
1338 return 0;
1339 }
1340 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +00001341 } else {
David Greenee1b46912009-06-08 20:23:18 +00001342 // Make sure the deduced type is compatible with the given type
1343 if (GivenListTy) {
1344 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1345 TokError("Element type mismatch for list");
1346 return 0;
1347 }
1348 }
1349 DeducedEltTy = EltTy;
1350 }
Bob Wilson21870412009-11-22 04:24:42 +00001351
David Greenedcd35c72011-07-29 19:07:07 +00001352 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4601652007-11-22 20:49:04 +00001353 }
1354 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1355 Lex.Lex(); // eat the '('
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001356 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001357 TokError("expected identifier in dag init");
1358 return 0;
1359 }
Bob Wilson21870412009-11-22 04:24:42 +00001360
David Greene05bce0b2011-07-29 22:43:06 +00001361 Init *Operator = ParseValue(CurRec);
Chris Lattner578bcf02010-10-06 04:31:40 +00001362 if (Operator == 0) return 0;
David Greenec7cafcd2009-04-22 20:18:10 +00001363
Nate Begeman7cee8172009-03-19 05:21:56 +00001364 // If the operator name is present, parse it.
1365 std::string OperatorName;
1366 if (Lex.getCode() == tgtok::colon) {
1367 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1368 TokError("expected variable name in dag operator");
1369 return 0;
1370 }
1371 OperatorName = Lex.getCurStrVal();
1372 Lex.Lex(); // eat the VarName.
1373 }
Bob Wilson21870412009-11-22 04:24:42 +00001374
David Greene05bce0b2011-07-29 22:43:06 +00001375 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4601652007-11-22 20:49:04 +00001376 if (Lex.getCode() != tgtok::r_paren) {
1377 DagArgs = ParseDagArgList(CurRec);
1378 if (DagArgs.empty()) return 0;
1379 }
Bob Wilson21870412009-11-22 04:24:42 +00001380
Chris Lattnerf4601652007-11-22 20:49:04 +00001381 if (Lex.getCode() != tgtok::r_paren) {
1382 TokError("expected ')' in dag init");
1383 return 0;
1384 }
1385 Lex.Lex(); // eat the ')'
Bob Wilson21870412009-11-22 04:24:42 +00001386
David Greenedcd35c72011-07-29 19:07:07 +00001387 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4601652007-11-22 20:49:04 +00001388 }
Bob Wilson21870412009-11-22 04:24:42 +00001389
David Greene1434f662011-01-07 17:05:37 +00001390 case tgtok::XHead:
1391 case tgtok::XTail:
1392 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +00001393 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001394 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +00001395 case tgtok::XSRA:
Chris Lattnerf4601652007-11-22 20:49:04 +00001396 case tgtok::XSRL:
1397 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +00001398 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001399 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene9bea7c82009-05-14 23:26:46 +00001400 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +00001401 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001402 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greened418c1b2009-05-14 20:54:48 +00001403 return ParseOperation(CurRec);
Chris Lattnerf4601652007-11-22 20:49:04 +00001404 }
1405 }
Bob Wilson21870412009-11-22 04:24:42 +00001406
Chris Lattnerf4601652007-11-22 20:49:04 +00001407 return R;
1408}
1409
1410/// ParseValue - Parse a tblgen value. This returns null on error.
1411///
1412/// Value ::= SimpleValue ValueSuffix*
1413/// ValueSuffix ::= '{' BitList '}'
1414/// ValueSuffix ::= '[' BitList ']'
1415/// ValueSuffix ::= '.' ID
1416///
David Greenef3744a02011-10-19 13:04:20 +00001417Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1418 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Chris Lattnerf4601652007-11-22 20:49:04 +00001419 if (Result == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001420
Chris Lattnerf4601652007-11-22 20:49:04 +00001421 // Parse the suffixes now if present.
1422 while (1) {
1423 switch (Lex.getCode()) {
1424 default: return Result;
1425 case tgtok::l_brace: {
David Greenecebb4ee2012-02-22 16:09:41 +00001426 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greene8592b2b2011-10-19 13:04:26 +00001427 // This is the beginning of the object body.
1428 return Result;
1429
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001430 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001431 Lex.Lex(); // eat the '{'
1432 std::vector<unsigned> Ranges = ParseRangeList();
1433 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001434
Chris Lattnerf4601652007-11-22 20:49:04 +00001435 // Reverse the bitlist.
1436 std::reverse(Ranges.begin(), Ranges.end());
1437 Result = Result->convertInitializerBitRange(Ranges);
1438 if (Result == 0) {
1439 Error(CurlyLoc, "Invalid bit range for value");
1440 return 0;
1441 }
Bob Wilson21870412009-11-22 04:24:42 +00001442
Chris Lattnerf4601652007-11-22 20:49:04 +00001443 // Eat the '}'.
1444 if (Lex.getCode() != tgtok::r_brace) {
1445 TokError("expected '}' at end of bit range list");
1446 return 0;
1447 }
1448 Lex.Lex();
1449 break;
1450 }
1451 case tgtok::l_square: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001452 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001453 Lex.Lex(); // eat the '['
1454 std::vector<unsigned> Ranges = ParseRangeList();
1455 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001456
Chris Lattnerf4601652007-11-22 20:49:04 +00001457 Result = Result->convertInitListSlice(Ranges);
1458 if (Result == 0) {
1459 Error(SquareLoc, "Invalid range for list slice");
1460 return 0;
1461 }
Bob Wilson21870412009-11-22 04:24:42 +00001462
Chris Lattnerf4601652007-11-22 20:49:04 +00001463 // Eat the ']'.
1464 if (Lex.getCode() != tgtok::r_square) {
1465 TokError("expected ']' at end of list slice");
1466 return 0;
1467 }
1468 Lex.Lex();
1469 break;
1470 }
1471 case tgtok::period:
1472 if (Lex.Lex() != tgtok::Id) { // eat the .
1473 TokError("expected field identifier after '.'");
1474 return 0;
1475 }
1476 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001477 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00001478 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +00001479 return 0;
1480 }
David Greenedcd35c72011-07-29 19:07:07 +00001481 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001482 Lex.Lex(); // eat field name
1483 break;
David Greened3d1cad2011-10-19 13:04:43 +00001484
1485 case tgtok::paste:
1486 SMLoc PasteLoc = Lex.getLoc();
1487
1488 // Create a !strconcat() operation, first casting each operand to
1489 // a string if necessary.
1490
Sean Silva6cfc8062012-10-10 20:24:43 +00001491 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greened3d1cad2011-10-19 13:04:43 +00001492 if (!LHS) {
1493 Error(PasteLoc, "LHS of paste is not typed!");
1494 return 0;
1495 }
1496
1497 if (LHS->getType() != StringRecTy::get()) {
1498 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1499 }
1500
1501 TypedInit *RHS = 0;
1502
1503 Lex.Lex(); // Eat the '#'.
1504 switch (Lex.getCode()) {
1505 case tgtok::colon:
1506 case tgtok::semi:
1507 case tgtok::l_brace:
1508 // These are all of the tokens that can begin an object body.
1509 // Some of these can also begin values but we disallow those cases
1510 // because they are unlikely to be useful.
1511
1512 // Trailing paste, concat with an empty string.
1513 RHS = StringInit::get("");
1514 break;
1515
1516 default:
1517 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silva6cfc8062012-10-10 20:24:43 +00001518 RHS = dyn_cast<TypedInit>(RHSResult);
David Greened3d1cad2011-10-19 13:04:43 +00001519 if (!RHS) {
1520 Error(PasteLoc, "RHS of paste is not typed!");
1521 return 0;
1522 }
1523
1524 if (RHS->getType() != StringRecTy::get()) {
1525 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1526 }
1527
1528 break;
1529 }
1530
1531 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1532 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1533 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001534 }
1535 }
1536}
1537
1538/// ParseDagArgList - Parse the argument list for a dag literal expression.
1539///
1540/// ParseDagArgList ::= Value (':' VARNAME)?
1541/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
David Greene05bce0b2011-07-29 22:43:06 +00001542std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4601652007-11-22 20:49:04 +00001543TGParser::ParseDagArgList(Record *CurRec) {
David Greene05bce0b2011-07-29 22:43:06 +00001544 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson21870412009-11-22 04:24:42 +00001545
Chris Lattnerf4601652007-11-22 20:49:04 +00001546 while (1) {
David Greene05bce0b2011-07-29 22:43:06 +00001547 Init *Val = ParseValue(CurRec);
1548 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
Bob Wilson21870412009-11-22 04:24:42 +00001549
Chris Lattnerf4601652007-11-22 20:49:04 +00001550 // If the variable name is present, add it.
1551 std::string VarName;
1552 if (Lex.getCode() == tgtok::colon) {
1553 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1554 TokError("expected variable name in dag literal");
David Greene05bce0b2011-07-29 22:43:06 +00001555 return std::vector<std::pair<llvm::Init*, std::string> >();
Chris Lattnerf4601652007-11-22 20:49:04 +00001556 }
1557 VarName = Lex.getCurStrVal();
1558 Lex.Lex(); // eat the VarName.
1559 }
Bob Wilson21870412009-11-22 04:24:42 +00001560
Chris Lattnerf4601652007-11-22 20:49:04 +00001561 Result.push_back(std::make_pair(Val, VarName));
Bob Wilson21870412009-11-22 04:24:42 +00001562
Chris Lattnerf4601652007-11-22 20:49:04 +00001563 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson21870412009-11-22 04:24:42 +00001564 Lex.Lex(); // eat the ','
Chris Lattnerf4601652007-11-22 20:49:04 +00001565 }
Bob Wilson21870412009-11-22 04:24:42 +00001566
Chris Lattnerf4601652007-11-22 20:49:04 +00001567 return Result;
1568}
1569
1570
1571/// ParseValueList - Parse a comma separated list of values, returning them as a
1572/// vector. Note that this always expects to be able to parse at least one
1573/// value. It returns an empty list if this is not possible.
1574///
1575/// ValueList ::= Value (',' Value)
1576///
David Greene05bce0b2011-07-29 22:43:06 +00001577std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopherd568b3f2011-07-11 23:06:52 +00001578 RecTy *EltTy) {
David Greene05bce0b2011-07-29 22:43:06 +00001579 std::vector<Init*> Result;
David Greenee1b46912009-06-08 20:23:18 +00001580 RecTy *ItemType = EltTy;
David Greene67acdf22009-06-29 19:59:52 +00001581 unsigned int ArgN = 0;
David Greenee1b46912009-06-08 20:23:18 +00001582 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001583 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Jim Grosbachb1320cb2012-01-20 20:02:39 +00001584 if (!TArgs.size()) {
1585 TokError("template argument provided to non-template class");
1586 return std::vector<Init*>();
1587 }
David Greenee1b46912009-06-08 20:23:18 +00001588 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greened9746fe2011-09-19 18:26:07 +00001589 if (!RV) {
1590 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1591 << ")\n";
1592 }
David Greenee1b46912009-06-08 20:23:18 +00001593 assert(RV && "Template argument record not found??");
1594 ItemType = RV->getType();
1595 ++ArgN;
1596 }
1597 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001598 if (Result.back() == 0) return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001599
Chris Lattnerf4601652007-11-22 20:49:04 +00001600 while (Lex.getCode() == tgtok::comma) {
1601 Lex.Lex(); // Eat the comma
Bob Wilson21870412009-11-22 04:24:42 +00001602
David Greenee1b46912009-06-08 20:23:18 +00001603 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001604 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greene67acdf22009-06-29 19:59:52 +00001605 if (ArgN >= TArgs.size()) {
1606 TokError("too many template arguments");
David Greene05bce0b2011-07-29 22:43:06 +00001607 return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001608 }
David Greenee1b46912009-06-08 20:23:18 +00001609 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1610 assert(RV && "Template argument record not found??");
1611 ItemType = RV->getType();
1612 ++ArgN;
1613 }
1614 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001615 if (Result.back() == 0) return std::vector<Init*>();
Chris Lattnerf4601652007-11-22 20:49:04 +00001616 }
Bob Wilson21870412009-11-22 04:24:42 +00001617
Chris Lattnerf4601652007-11-22 20:49:04 +00001618 return Result;
1619}
1620
1621
Chris Lattnerf4601652007-11-22 20:49:04 +00001622/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1623/// empty string on error. This can happen in a number of different context's,
1624/// including within a def or in the template args for a def (which which case
1625/// CurRec will be non-null) and within the template args for a multiclass (in
1626/// which case CurRec will be null, but CurMultiClass will be set). This can
1627/// also happen within a def that is within a multiclass, which will set both
1628/// CurRec and CurMultiClass.
1629///
1630/// Declaration ::= FIELD? Type ID ('=' Value)?
1631///
David Greenee22b3212011-10-19 13:02:42 +00001632Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4601652007-11-22 20:49:04 +00001633 bool ParsingTemplateArgs) {
1634 // Read the field prefix if present.
1635 bool HasField = Lex.getCode() == tgtok::Field;
1636 if (HasField) Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001637
Chris Lattnerf4601652007-11-22 20:49:04 +00001638 RecTy *Type = ParseType();
David Greenee22b3212011-10-19 13:02:42 +00001639 if (Type == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001640
Chris Lattnerf4601652007-11-22 20:49:04 +00001641 if (Lex.getCode() != tgtok::Id) {
1642 TokError("Expected identifier in declaration");
David Greenee22b3212011-10-19 13:02:42 +00001643 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001644 }
Bob Wilson21870412009-11-22 04:24:42 +00001645
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001646 SMLoc IdLoc = Lex.getLoc();
David Greenee22b3212011-10-19 13:02:42 +00001647 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001648 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001649
Chris Lattnerf4601652007-11-22 20:49:04 +00001650 if (ParsingTemplateArgs) {
1651 if (CurRec) {
David Greenee22b3212011-10-19 13:02:42 +00001652 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4601652007-11-22 20:49:04 +00001653 } else {
1654 assert(CurMultiClass);
1655 }
1656 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +00001657 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1658 "::");
Chris Lattnerf4601652007-11-22 20:49:04 +00001659 }
Bob Wilson21870412009-11-22 04:24:42 +00001660
Chris Lattnerf4601652007-11-22 20:49:04 +00001661 // Add the value.
1662 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
David Greenee22b3212011-10-19 13:02:42 +00001663 return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001664
Chris Lattnerf4601652007-11-22 20:49:04 +00001665 // If a value is present, parse it.
1666 if (Lex.getCode() == tgtok::equal) {
1667 Lex.Lex();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001668 SMLoc ValLoc = Lex.getLoc();
David Greene05bce0b2011-07-29 22:43:06 +00001669 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001670 if (Val == 0 ||
1671 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
David Greenee22b3212011-10-19 13:02:42 +00001672 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001673 }
Bob Wilson21870412009-11-22 04:24:42 +00001674
Chris Lattnerf4601652007-11-22 20:49:04 +00001675 return DeclName;
1676}
1677
David Greenecebb4ee2012-02-22 16:09:41 +00001678/// ParseForeachDeclaration - Read a foreach declaration, returning
1679/// the name of the declared object or a NULL Init on error. Return
1680/// the name of the parsed initializer list through ForeachListName.
1681///
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001682/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1683/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1684/// ForeachDeclaration ::= ID '=' RangePiece
David Greenecebb4ee2012-02-22 16:09:41 +00001685///
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00001686VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenecebb4ee2012-02-22 16:09:41 +00001687 if (Lex.getCode() != tgtok::Id) {
1688 TokError("Expected identifier in foreach declaration");
1689 return 0;
1690 }
1691
1692 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1693 Lex.Lex();
1694
1695 // If a value is present, parse it.
1696 if (Lex.getCode() != tgtok::equal) {
1697 TokError("Expected '=' in foreach declaration");
1698 return 0;
1699 }
1700 Lex.Lex(); // Eat the '='
1701
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001702 RecTy *IterType = 0;
1703 std::vector<unsigned> Ranges;
David Greenecebb4ee2012-02-22 16:09:41 +00001704
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001705 switch (Lex.getCode()) {
1706 default: TokError("Unknown token when expecting a range list"); return 0;
1707 case tgtok::l_square: { // '[' ValueList ']'
1708 Init *List = ParseSimpleValue(0, 0, ParseForeachMode);
Sean Silva6cfc8062012-10-10 20:24:43 +00001709 ForeachListValue = dyn_cast<ListInit>(List);
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001710 if (ForeachListValue == 0) {
1711 TokError("Expected a Value list");
1712 return 0;
1713 }
1714 RecTy *ValueType = ForeachListValue->getType();
Sean Silva736ceac2012-10-05 03:31:58 +00001715 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001716 if (ListType == 0) {
1717 TokError("Value list is not of list type");
1718 return 0;
1719 }
1720 IterType = ListType->getElementType();
1721 break;
David Greenecebb4ee2012-02-22 16:09:41 +00001722 }
1723
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001724 case tgtok::IntVal: { // RangePiece.
1725 if (ParseRangePiece(Ranges))
1726 return 0;
1727 break;
David Greenecebb4ee2012-02-22 16:09:41 +00001728 }
1729
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001730 case tgtok::l_brace: { // '{' RangeList '}'
1731 Lex.Lex(); // eat the '{'
1732 Ranges = ParseRangeList();
1733 if (Lex.getCode() != tgtok::r_brace) {
1734 TokError("expected '}' at end of bit range list");
1735 return 0;
1736 }
1737 Lex.Lex();
1738 break;
1739 }
1740 }
David Greenecebb4ee2012-02-22 16:09:41 +00001741
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001742 if (!Ranges.empty()) {
1743 assert(!IterType && "Type already initialized?");
1744 IterType = IntRecTy::get();
1745 std::vector<Init*> Values;
1746 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
1747 Values.push_back(IntInit::get(Ranges[i]));
1748 ForeachListValue = ListInit::get(Values, IterType);
1749 }
1750
1751 if (!IterType)
1752 return 0;
1753
1754 return VarInit::get(DeclName, IterType);
David Greenecebb4ee2012-02-22 16:09:41 +00001755}
1756
Chris Lattnerf4601652007-11-22 20:49:04 +00001757/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1758/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1759/// template args for a def, which may or may not be in a multiclass. If null,
1760/// these are the template args for a multiclass.
1761///
1762/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson21870412009-11-22 04:24:42 +00001763///
Chris Lattnerf4601652007-11-22 20:49:04 +00001764bool TGParser::ParseTemplateArgList(Record *CurRec) {
1765 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1766 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +00001767
Chris Lattnerf4601652007-11-22 20:49:04 +00001768 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +00001769
Chris Lattnerf4601652007-11-22 20:49:04 +00001770 // Read the first declaration.
David Greenee22b3212011-10-19 13:02:42 +00001771 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1772 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001773 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001774
Chris Lattnerf4601652007-11-22 20:49:04 +00001775 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson21870412009-11-22 04:24:42 +00001776
Chris Lattnerf4601652007-11-22 20:49:04 +00001777 while (Lex.getCode() == tgtok::comma) {
1778 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001779
Chris Lattnerf4601652007-11-22 20:49:04 +00001780 // Read the following declarations.
1781 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
David Greenee22b3212011-10-19 13:02:42 +00001782 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001783 return true;
1784 TheRecToAddTo->addTemplateArg(TemplArg);
1785 }
Bob Wilson21870412009-11-22 04:24:42 +00001786
Chris Lattnerf4601652007-11-22 20:49:04 +00001787 if (Lex.getCode() != tgtok::greater)
1788 return TokError("expected '>' at end of template argument list");
1789 Lex.Lex(); // eat the '>'.
1790 return false;
1791}
1792
1793
1794/// ParseBodyItem - Parse a single item at within the body of a def or class.
1795///
1796/// BodyItem ::= Declaration ';'
1797/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1798bool TGParser::ParseBodyItem(Record *CurRec) {
1799 if (Lex.getCode() != tgtok::Let) {
David Greenee22b3212011-10-19 13:02:42 +00001800 if (ParseDeclaration(CurRec, false) == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001801 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001802
Chris Lattnerf4601652007-11-22 20:49:04 +00001803 if (Lex.getCode() != tgtok::semi)
1804 return TokError("expected ';' after declaration");
1805 Lex.Lex();
1806 return false;
1807 }
1808
1809 // LET ID OptionalRangeList '=' Value ';'
1810 if (Lex.Lex() != tgtok::Id)
1811 return TokError("expected field identifier after let");
Bob Wilson21870412009-11-22 04:24:42 +00001812
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001813 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001814 std::string FieldName = Lex.getCurStrVal();
1815 Lex.Lex(); // eat the field name.
Bob Wilson21870412009-11-22 04:24:42 +00001816
Chris Lattnerf4601652007-11-22 20:49:04 +00001817 std::vector<unsigned> BitList;
Bob Wilson21870412009-11-22 04:24:42 +00001818 if (ParseOptionalBitList(BitList))
Chris Lattnerf4601652007-11-22 20:49:04 +00001819 return true;
1820 std::reverse(BitList.begin(), BitList.end());
Bob Wilson21870412009-11-22 04:24:42 +00001821
Chris Lattnerf4601652007-11-22 20:49:04 +00001822 if (Lex.getCode() != tgtok::equal)
1823 return TokError("expected '=' in let expression");
1824 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001825
David Greenee1b46912009-06-08 20:23:18 +00001826 RecordVal *Field = CurRec->getValue(FieldName);
1827 if (Field == 0)
1828 return TokError("Value '" + FieldName + "' unknown!");
1829
1830 RecTy *Type = Field->getType();
Bob Wilson21870412009-11-22 04:24:42 +00001831
David Greene05bce0b2011-07-29 22:43:06 +00001832 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001833 if (Val == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001834
Chris Lattnerf4601652007-11-22 20:49:04 +00001835 if (Lex.getCode() != tgtok::semi)
1836 return TokError("expected ';' after let expression");
1837 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001838
Chris Lattnerf4601652007-11-22 20:49:04 +00001839 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1840}
1841
1842/// ParseBody - Read the body of a class or def. Return true on error, false on
1843/// success.
1844///
1845/// Body ::= ';'
1846/// Body ::= '{' BodyList '}'
1847/// BodyList BodyItem*
1848///
1849bool TGParser::ParseBody(Record *CurRec) {
1850 // If this is a null definition, just eat the semi and return.
1851 if (Lex.getCode() == tgtok::semi) {
1852 Lex.Lex();
1853 return false;
1854 }
Bob Wilson21870412009-11-22 04:24:42 +00001855
Chris Lattnerf4601652007-11-22 20:49:04 +00001856 if (Lex.getCode() != tgtok::l_brace)
1857 return TokError("Expected ';' or '{' to start body");
1858 // Eat the '{'.
1859 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001860
Chris Lattnerf4601652007-11-22 20:49:04 +00001861 while (Lex.getCode() != tgtok::r_brace)
1862 if (ParseBodyItem(CurRec))
1863 return true;
1864
1865 // Eat the '}'.
1866 Lex.Lex();
1867 return false;
1868}
1869
1870/// ParseObjectBody - Parse the body of a def or class. This consists of an
1871/// optional ClassList followed by a Body. CurRec is the current def or class
1872/// that is being parsed.
1873///
1874/// ObjectBody ::= BaseClassList Body
1875/// BaseClassList ::= /*empty*/
1876/// BaseClassList ::= ':' BaseClassListNE
1877/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1878///
1879bool TGParser::ParseObjectBody(Record *CurRec) {
1880 // If there is a baseclass list, read it.
1881 if (Lex.getCode() == tgtok::colon) {
1882 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001883
Chris Lattnerf4601652007-11-22 20:49:04 +00001884 // Read all of the subclasses.
1885 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1886 while (1) {
1887 // Check for error.
1888 if (SubClass.Rec == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001889
Chris Lattnerf4601652007-11-22 20:49:04 +00001890 // Add it.
1891 if (AddSubClass(CurRec, SubClass))
1892 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001893
Chris Lattnerf4601652007-11-22 20:49:04 +00001894 if (Lex.getCode() != tgtok::comma) break;
1895 Lex.Lex(); // eat ','.
1896 SubClass = ParseSubClassReference(CurRec, false);
1897 }
1898 }
1899
1900 // Process any variables on the let stack.
1901 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1902 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1903 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1904 LetStack[i][j].Bits, LetStack[i][j].Value))
1905 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001906
Chris Lattnerf4601652007-11-22 20:49:04 +00001907 return ParseBody(CurRec);
1908}
1909
Chris Lattnerf4601652007-11-22 20:49:04 +00001910/// ParseDef - Parse and return a top level or multiclass def, return the record
1911/// corresponding to it. This returns null on error.
1912///
1913/// DefInst ::= DEF ObjectName ObjectBody
1914///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001915bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001916 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001917 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson21870412009-11-22 04:24:42 +00001918 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4601652007-11-22 20:49:04 +00001919
1920 // Parse ObjectName and make a record for it.
David Greenea9e07dd2011-10-19 13:04:29 +00001921 Record *CurRec = new Record(ParseObjectName(CurMultiClass), DefLoc, Records);
Bob Wilson21870412009-11-22 04:24:42 +00001922
Jakob Stoklund Olesen72cba6c2012-05-24 22:17:36 +00001923 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001924 // Top-level def definition.
Bob Wilson21870412009-11-22 04:24:42 +00001925
Chris Lattnerf4601652007-11-22 20:49:04 +00001926 // Ensure redefinition doesn't happen.
David Greene1d501392012-01-28 00:03:24 +00001927 if (Records.getDef(CurRec->getNameInitAsString())) {
David Greene2c49fbb2011-10-19 13:03:45 +00001928 Error(DefLoc, "def '" + CurRec->getNameInitAsString()
1929 + "' already defined");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001930 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001931 }
1932 Records.addDef(CurRec);
Jakob Stoklund Olesen72cba6c2012-05-24 22:17:36 +00001933 } else if (CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001934 // Otherwise, a def inside a multiclass, add it to the multiclass.
1935 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene91919cd2011-10-19 13:03:51 +00001936 if (CurMultiClass->DefPrototypes[i]->getNameInit()
1937 == CurRec->getNameInit()) {
1938 Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
Chris Lattnerf4601652007-11-22 20:49:04 +00001939 "' already defined in this multiclass!");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001940 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001941 }
1942 CurMultiClass->DefPrototypes.push_back(CurRec);
1943 }
Bob Wilson21870412009-11-22 04:24:42 +00001944
Chris Lattnerf4601652007-11-22 20:49:04 +00001945 if (ParseObjectBody(CurRec))
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001946 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001947
Chris Lattnerf4601652007-11-22 20:49:04 +00001948 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
David Greene0d886402011-08-10 18:27:46 +00001949 // See Record::setName(). This resolve step will see any new name
1950 // for the def that might have been created when resolving
1951 // inheritance, values and arguments above.
Chris Lattnerf4601652007-11-22 20:49:04 +00001952 CurRec->resolveReferences();
Bob Wilson21870412009-11-22 04:24:42 +00001953
Chris Lattnerf4601652007-11-22 20:49:04 +00001954 // If ObjectBody has template arguments, it's an error.
1955 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001956
1957 if (CurMultiClass) {
1958 // Copy the template arguments for the multiclass into the def.
David Greenee22b3212011-10-19 13:02:42 +00001959 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001960 CurMultiClass->Rec.getTemplateArgs();
1961
1962 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1963 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1964 assert(RV && "Template arg doesn't exist?");
1965 CurRec->addValue(*RV);
1966 }
1967 }
1968
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00001969 if (ProcessForeachDefs(CurRec, DefLoc)) {
David Greenecebb4ee2012-02-22 16:09:41 +00001970 Error(DefLoc,
1971 "Could not process loops for def" + CurRec->getNameInitAsString());
1972 return true;
1973 }
1974
1975 return false;
1976}
1977
1978/// ParseForeach - Parse a for statement. Return the record corresponding
1979/// to it. This returns true on error.
1980///
1981/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
1982/// Foreach ::= FOREACH Declaration IN Object
1983///
1984bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
1985 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
1986 Lex.Lex(); // Eat the 'for' token.
1987
1988 // Make a temporary object to record items associated with the for
1989 // loop.
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00001990 ListInit *ListValue = 0;
1991 VarInit *IterName = ParseForeachDeclaration(ListValue);
David Greenecebb4ee2012-02-22 16:09:41 +00001992 if (IterName == 0)
1993 return TokError("expected declaration in for");
1994
1995 if (Lex.getCode() != tgtok::In)
1996 return TokError("Unknown tok");
1997 Lex.Lex(); // Eat the in
1998
1999 // Create a loop object and remember it.
2000 Loops.push_back(ForeachLoop(IterName, ListValue));
2001
2002 if (Lex.getCode() != tgtok::l_brace) {
2003 // FOREACH Declaration IN Object
2004 if (ParseObject(CurMultiClass))
2005 return true;
2006 }
2007 else {
2008 SMLoc BraceLoc = Lex.getLoc();
2009 // Otherwise, this is a group foreach.
2010 Lex.Lex(); // eat the '{'.
2011
2012 // Parse the object list.
2013 if (ParseObjectList(CurMultiClass))
2014 return true;
2015
2016 if (Lex.getCode() != tgtok::r_brace) {
2017 TokError("expected '}' at end of foreach command");
2018 return Error(BraceLoc, "to match this '{'");
2019 }
2020 Lex.Lex(); // Eat the }
2021 }
2022
2023 // We've processed everything in this loop.
2024 Loops.pop_back();
2025
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002026 return false;
Chris Lattnerf4601652007-11-22 20:49:04 +00002027}
2028
Chris Lattnerf4601652007-11-22 20:49:04 +00002029/// ParseClass - Parse a tblgen class definition.
2030///
2031/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2032///
2033bool TGParser::ParseClass() {
2034 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2035 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002036
Chris Lattnerf4601652007-11-22 20:49:04 +00002037 if (Lex.getCode() != tgtok::Id)
2038 return TokError("expected class name after 'class' keyword");
Bob Wilson21870412009-11-22 04:24:42 +00002039
Chris Lattnerf4601652007-11-22 20:49:04 +00002040 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2041 if (CurRec) {
2042 // If the body was previously defined, this is an error.
David Greenee3385652011-10-19 13:04:13 +00002043 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4601652007-11-22 20:49:04 +00002044 !CurRec->getSuperClasses().empty() ||
2045 !CurRec->getTemplateArgs().empty())
David Greene69a23942011-10-19 13:03:58 +00002046 return TokError("Class '" + CurRec->getNameInitAsString()
2047 + "' already defined");
Chris Lattnerf4601652007-11-22 20:49:04 +00002048 } else {
2049 // If this is the first reference to this class, create and add it.
Chris Lattner9c6b60e2010-12-15 04:48:22 +00002050 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00002051 Records.addClass(CurRec);
2052 }
2053 Lex.Lex(); // eat the name.
Bob Wilson21870412009-11-22 04:24:42 +00002054
Chris Lattnerf4601652007-11-22 20:49:04 +00002055 // If there are template args, parse them.
2056 if (Lex.getCode() == tgtok::less)
2057 if (ParseTemplateArgList(CurRec))
2058 return true;
2059
2060 // Finally, parse the object body.
2061 return ParseObjectBody(CurRec);
2062}
2063
2064/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2065/// of LetRecords.
2066///
2067/// LetList ::= LetItem (',' LetItem)*
2068/// LetItem ::= ID OptionalRangeList '=' Value
2069///
2070std::vector<LetRecord> TGParser::ParseLetList() {
2071 std::vector<LetRecord> Result;
Bob Wilson21870412009-11-22 04:24:42 +00002072
Chris Lattnerf4601652007-11-22 20:49:04 +00002073 while (1) {
2074 if (Lex.getCode() != tgtok::Id) {
2075 TokError("expected identifier in let definition");
2076 return std::vector<LetRecord>();
2077 }
2078 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002079 SMLoc NameLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +00002080 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4601652007-11-22 20:49:04 +00002081
2082 // Check for an optional RangeList.
2083 std::vector<unsigned> Bits;
Bob Wilson21870412009-11-22 04:24:42 +00002084 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4601652007-11-22 20:49:04 +00002085 return std::vector<LetRecord>();
2086 std::reverse(Bits.begin(), Bits.end());
Bob Wilson21870412009-11-22 04:24:42 +00002087
Chris Lattnerf4601652007-11-22 20:49:04 +00002088 if (Lex.getCode() != tgtok::equal) {
2089 TokError("expected '=' in let expression");
2090 return std::vector<LetRecord>();
2091 }
2092 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00002093
David Greene05bce0b2011-07-29 22:43:06 +00002094 Init *Val = ParseValue(0);
Chris Lattnerf4601652007-11-22 20:49:04 +00002095 if (Val == 0) return std::vector<LetRecord>();
Bob Wilson21870412009-11-22 04:24:42 +00002096
Chris Lattnerf4601652007-11-22 20:49:04 +00002097 // Now that we have everything, add the record.
2098 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson21870412009-11-22 04:24:42 +00002099
Chris Lattnerf4601652007-11-22 20:49:04 +00002100 if (Lex.getCode() != tgtok::comma)
2101 return Result;
Bob Wilson21870412009-11-22 04:24:42 +00002102 Lex.Lex(); // eat the comma.
Chris Lattnerf4601652007-11-22 20:49:04 +00002103 }
2104}
2105
2106/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002107/// different related productions. This works inside multiclasses too.
Chris Lattnerf4601652007-11-22 20:49:04 +00002108///
2109/// Object ::= LET LetList IN '{' ObjectList '}'
2110/// Object ::= LET LetList IN Object
2111///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002112bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002113 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2114 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002115
Chris Lattnerf4601652007-11-22 20:49:04 +00002116 // Add this entry to the let stack.
2117 std::vector<LetRecord> LetInfo = ParseLetList();
2118 if (LetInfo.empty()) return true;
2119 LetStack.push_back(LetInfo);
2120
2121 if (Lex.getCode() != tgtok::In)
2122 return TokError("expected 'in' at end of top-level 'let'");
2123 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002124
Chris Lattnerf4601652007-11-22 20:49:04 +00002125 // If this is a scalar let, just handle it now
2126 if (Lex.getCode() != tgtok::l_brace) {
2127 // LET LetList IN Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002128 if (ParseObject(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00002129 return true;
2130 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002131 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002132 // Otherwise, this is a group let.
2133 Lex.Lex(); // eat the '{'.
Bob Wilson21870412009-11-22 04:24:42 +00002134
Chris Lattnerf4601652007-11-22 20:49:04 +00002135 // Parse the object list.
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002136 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00002137 return true;
Bob Wilson21870412009-11-22 04:24:42 +00002138
Chris Lattnerf4601652007-11-22 20:49:04 +00002139 if (Lex.getCode() != tgtok::r_brace) {
2140 TokError("expected '}' at end of top level let command");
2141 return Error(BraceLoc, "to match this '{'");
2142 }
2143 Lex.Lex();
2144 }
Bob Wilson21870412009-11-22 04:24:42 +00002145
Chris Lattnerf4601652007-11-22 20:49:04 +00002146 // Outside this let scope, this let block is not active.
2147 LetStack.pop_back();
2148 return false;
2149}
2150
Chris Lattnerf4601652007-11-22 20:49:04 +00002151/// ParseMultiClass - Parse a multiclass definition.
2152///
Bob Wilson32558652009-04-28 19:41:44 +00002153/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silva9302dcc2013-01-09 02:11:55 +00002154/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2155/// MultiClassObject ::= DefInst
2156/// MultiClassObject ::= MultiClassInst
2157/// MultiClassObject ::= DefMInst
2158/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2159/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4601652007-11-22 20:49:04 +00002160///
2161bool TGParser::ParseMultiClass() {
2162 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2163 Lex.Lex(); // Eat the multiclass token.
2164
2165 if (Lex.getCode() != tgtok::Id)
2166 return TokError("expected identifier after multiclass for name");
2167 std::string Name = Lex.getCurStrVal();
Bob Wilson21870412009-11-22 04:24:42 +00002168
Chris Lattnerf4601652007-11-22 20:49:04 +00002169 if (MultiClasses.count(Name))
2170 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson21870412009-11-22 04:24:42 +00002171
Chris Lattner67db8832010-12-13 00:23:57 +00002172 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
2173 Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00002174 Lex.Lex(); // Eat the identifier.
Bob Wilson21870412009-11-22 04:24:42 +00002175
Chris Lattnerf4601652007-11-22 20:49:04 +00002176 // If there are template args, parse them.
2177 if (Lex.getCode() == tgtok::less)
2178 if (ParseTemplateArgList(0))
2179 return true;
2180
David Greened34a73b2009-04-24 16:55:41 +00002181 bool inherits = false;
2182
David Greenede444af2009-04-22 16:42:54 +00002183 // If there are submulticlasses, parse them.
2184 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00002185 inherits = true;
2186
David Greenede444af2009-04-22 16:42:54 +00002187 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00002188
David Greenede444af2009-04-22 16:42:54 +00002189 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00002190 SubMultiClassReference SubMultiClass =
2191 ParseSubMultiClassReference(CurMultiClass);
David Greenede444af2009-04-22 16:42:54 +00002192 while (1) {
2193 // Check for error.
2194 if (SubMultiClass.MC == 0) return true;
Bob Wilson32558652009-04-28 19:41:44 +00002195
David Greenede444af2009-04-22 16:42:54 +00002196 // Add it.
2197 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2198 return true;
Bob Wilson32558652009-04-28 19:41:44 +00002199
David Greenede444af2009-04-22 16:42:54 +00002200 if (Lex.getCode() != tgtok::comma) break;
2201 Lex.Lex(); // eat ','.
2202 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2203 }
2204 }
2205
David Greened34a73b2009-04-24 16:55:41 +00002206 if (Lex.getCode() != tgtok::l_brace) {
2207 if (!inherits)
2208 return TokError("expected '{' in multiclass definition");
Bob Wilson21870412009-11-22 04:24:42 +00002209 else if (Lex.getCode() != tgtok::semi)
2210 return TokError("expected ';' in multiclass definition");
David Greened34a73b2009-04-24 16:55:41 +00002211 else
Bob Wilson21870412009-11-22 04:24:42 +00002212 Lex.Lex(); // eat the ';'.
2213 } else {
David Greened34a73b2009-04-24 16:55:41 +00002214 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2215 return TokError("multiclass must contain at least one def");
Bob Wilson21870412009-11-22 04:24:42 +00002216
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002217 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002218 switch (Lex.getCode()) {
2219 default:
David Greenea1b1b792011-10-07 18:25:05 +00002220 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002221 case tgtok::Let:
2222 case tgtok::Def:
2223 case tgtok::Defm:
David Greenecebb4ee2012-02-22 16:09:41 +00002224 case tgtok::Foreach:
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002225 if (ParseObject(CurMultiClass))
2226 return true;
2227 break;
2228 }
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002229 }
David Greened34a73b2009-04-24 16:55:41 +00002230 Lex.Lex(); // eat the '}'.
2231 }
Bob Wilson21870412009-11-22 04:24:42 +00002232
Chris Lattnerf4601652007-11-22 20:49:04 +00002233 CurMultiClass = 0;
2234 return false;
2235}
2236
David Greenee499a2d2011-10-05 22:42:07 +00002237Record *TGParser::
2238InstantiateMulticlassDef(MultiClass &MC,
2239 Record *DefProto,
David Greene7be867e2011-10-19 13:04:31 +00002240 Init *DefmPrefix,
David Greenee499a2d2011-10-05 22:42:07 +00002241 SMLoc DefmPrefixLoc) {
David Greene7be867e2011-10-19 13:04:31 +00002242 // We need to preserve DefProto so it can be reused for later
2243 // instantiations, so create a new Record to inherit from it.
2244
David Greenee499a2d2011-10-05 22:42:07 +00002245 // Add in the defm name. If the defm prefix is empty, give each
2246 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2247 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2248 // as a prefix.
David Greenee499a2d2011-10-05 22:42:07 +00002249
David Greene7be867e2011-10-19 13:04:31 +00002250 if (DefmPrefix == 0)
2251 DefmPrefix = StringInit::get(GetNewAnonymousName());
2252
2253 Init *DefName = DefProto->getNameInit();
2254
Sean Silva6cfc8062012-10-10 20:24:43 +00002255 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene7be867e2011-10-19 13:04:31 +00002256
David Greened3d1cad2011-10-19 13:04:43 +00002257 if (DefNameString != 0) {
2258 // We have a fully expanded string so there are no operators to
2259 // resolve. We should concatenate the given prefix and name.
David Greene7be867e2011-10-19 13:04:31 +00002260 DefName =
2261 BinOpInit::get(BinOpInit::STRCONCAT,
2262 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2263 StringRecTy::get())->Fold(DefProto, &MC),
2264 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2265 }
David Greene7be867e2011-10-19 13:04:31 +00002266
Jakob Stoklund Olesen376a8a72012-08-22 23:33:58 +00002267 // Make a trail of SMLocs from the multiclass instantiations.
2268 SmallVector<SMLoc, 4> Locs(1, DefmPrefixLoc);
2269 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
2270 Record *CurRec = new Record(DefName, Locs, Records);
David Greenee499a2d2011-10-05 22:42:07 +00002271
2272 SubClassReference Ref;
2273 Ref.RefLoc = DefmPrefixLoc;
2274 Ref.Rec = DefProto;
2275 AddSubClass(CurRec, Ref);
2276
Jim Grosbachcfbda4a2012-08-02 18:46:42 +00002277 // Set the value for NAME. We don't resolve references to it 'til later,
2278 // though, so that uses in nested multiclass names don't get
2279 // confused.
2280 if (SetValue(CurRec, Ref.RefLoc, "NAME", std::vector<unsigned>(),
2281 DefmPrefix)) {
2282 Error(DefmPrefixLoc, "Could not resolve "
2283 + CurRec->getNameInitAsString() + ":NAME to '"
2284 + DefmPrefix->getAsUnquotedString() + "'");
2285 return 0;
2286 }
David Greenee5b252f2011-10-19 13:04:35 +00002287
Jim Grosbachcfbda4a2012-08-02 18:46:42 +00002288 // If the DefNameString didn't resolve, we probably have a reference to
2289 // NAME and need to replace it. We need to do at least this much greedily,
2290 // otherwise nested multiclasses will end up with incorrect NAME expansions.
2291 if (DefNameString == 0) {
David Greenee5b252f2011-10-19 13:04:35 +00002292 RecordVal *DefNameRV = CurRec->getValue("NAME");
2293 CurRec->resolveReferencesTo(DefNameRV);
2294 }
2295
2296 if (!CurMultiClass) {
Jim Grosbachcfbda4a2012-08-02 18:46:42 +00002297 // Now that we're at the top level, resolve all NAME references
2298 // in the resultant defs that weren't in the def names themselves.
2299 RecordVal *DefNameRV = CurRec->getValue("NAME");
2300 CurRec->resolveReferencesTo(DefNameRV);
2301
2302 // Now that NAME references are resolved and we're at the top level of
2303 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greenee5b252f2011-10-19 13:04:35 +00002304 // currently in a multiclass, it means this defm appears inside a
2305 // multiclass and its name won't be fully resolvable until we see
2306 // the top-level defm. Therefore, we don't add this to the
2307 // RecordKeeper at this point. If we did we could get duplicate
2308 // defs as more than one probably refers to NAME or some other
2309 // common internal placeholder.
2310
2311 // Ensure redefinition doesn't happen.
2312 if (Records.getDef(CurRec->getNameInitAsString())) {
2313 Error(DefmPrefixLoc, "def '" + CurRec->getNameInitAsString() +
2314 "' already defined, instantiating defm with subdef '" +
2315 DefProto->getNameInitAsString() + "'");
2316 return 0;
2317 }
2318
2319 Records.addDef(CurRec);
2320 }
2321
David Greenee499a2d2011-10-05 22:42:07 +00002322 return CurRec;
2323}
2324
2325bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2326 Record *CurRec,
2327 SMLoc DefmPrefixLoc,
2328 SMLoc SubClassLoc,
David Greenee22b3212011-10-19 13:02:42 +00002329 const std::vector<Init *> &TArgs,
David Greenee499a2d2011-10-05 22:42:07 +00002330 std::vector<Init *> &TemplateVals,
2331 bool DeleteArgs) {
2332 // Loop over all of the template arguments, setting them to the specified
2333 // value or leaving them as the default if necessary.
2334 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2335 // Check if a value is specified for this temp-arg.
2336 if (i < TemplateVals.size()) {
2337 // Set it now.
2338 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2339 TemplateVals[i]))
2340 return true;
2341
2342 // Resolve it next.
2343 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2344
2345 if (DeleteArgs)
2346 // Now remove it.
2347 CurRec->removeValue(TArgs[i]);
2348
2349 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2350 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenee22b3212011-10-19 13:02:42 +00002351 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2352 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2353 + "'");
David Greenee499a2d2011-10-05 22:42:07 +00002354 }
2355 }
2356 return false;
2357}
2358
2359bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2360 Record *CurRec,
2361 Record *DefProto,
2362 SMLoc DefmPrefixLoc) {
2363 // If the mdef is inside a 'let' expression, add to each def.
2364 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2365 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2366 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2367 LetStack[i][j].Bits, LetStack[i][j].Value))
2368 return Error(DefmPrefixLoc, "when instantiating this defm");
2369
David Greenee499a2d2011-10-05 22:42:07 +00002370 // Don't create a top level definition for defm inside multiclasses,
2371 // instead, only update the prototypes and bind the template args
2372 // with the new created definition.
2373 if (CurMultiClass) {
2374 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2375 i != e; ++i)
David Greene22dde7e2011-10-19 13:04:02 +00002376 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2377 == CurRec->getNameInit())
2378 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
David Greenee499a2d2011-10-05 22:42:07 +00002379 "' already defined in this multiclass!");
2380 CurMultiClass->DefPrototypes.push_back(CurRec);
2381
2382 // Copy the template arguments for the multiclass into the new def.
David Greenee22b3212011-10-19 13:02:42 +00002383 const std::vector<Init *> &TA =
David Greenee499a2d2011-10-05 22:42:07 +00002384 CurMultiClass->Rec.getTemplateArgs();
2385
2386 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2387 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2388 assert(RV && "Template arg doesn't exist?");
2389 CurRec->addValue(*RV);
2390 }
David Greenee499a2d2011-10-05 22:42:07 +00002391 }
2392
2393 return false;
2394}
2395
Chris Lattnerf4601652007-11-22 20:49:04 +00002396/// ParseDefm - Parse the instantiation of a multiclass.
2397///
2398/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2399///
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002400bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002401 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Bob Wilson21870412009-11-22 04:24:42 +00002402
David Greenea9e07dd2011-10-19 13:04:29 +00002403 Init *DefmPrefix = 0;
David Greenea9e07dd2011-10-19 13:04:29 +00002404
Craig Topper6a59f5a2013-01-07 05:09:33 +00002405 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greenea9e07dd2011-10-19 13:04:29 +00002406 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002407 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +00002408
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002409 SMLoc DefmPrefixLoc = Lex.getLoc();
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002410 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4601652007-11-22 20:49:04 +00002411 return TokError("expected ':' after defm identifier");
Bob Wilson21870412009-11-22 04:24:42 +00002412
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002413 // Keep track of the new generated record definitions.
2414 std::vector<Record*> NewRecDefs;
2415
2416 // This record also inherits from a regular class (non-multiclass)?
2417 bool InheritFromClass = false;
2418
Chris Lattnerf4601652007-11-22 20:49:04 +00002419 // eat the colon.
2420 Lex.Lex();
2421
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002422 SMLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002423 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene56546132009-04-22 22:17:51 +00002424
2425 while (1) {
2426 if (Ref.Rec == 0) return true;
2427
2428 // To instantiate a multiclass, we need to first get the multiclass, then
2429 // instantiate each def contained in the multiclass with the SubClassRef
2430 // template parameters.
2431 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2432 assert(MC && "Didn't lookup multiclass correctly?");
David Greene05bce0b2011-07-29 22:43:06 +00002433 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greene56546132009-04-22 22:17:51 +00002434
2435 // Verify that the correct number of template arguments were specified.
David Greenee22b3212011-10-19 13:02:42 +00002436 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greene56546132009-04-22 22:17:51 +00002437 if (TArgs.size() < TemplateVals.size())
2438 return Error(SubClassLoc,
2439 "more template args specified than multiclass expects");
2440
2441 // Loop over all the def's in the multiclass, instantiating each one.
2442 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2443 Record *DefProto = MC->DefPrototypes[i];
2444
David Greene7be867e2011-10-19 13:04:31 +00002445 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix, DefmPrefixLoc);
Jim Grosbach94f2dc92011-12-02 18:33:03 +00002446 if (!CurRec)
2447 return true;
David Greene065f2592009-05-05 16:28:25 +00002448
David Greenee499a2d2011-10-05 22:42:07 +00002449 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmPrefixLoc, SubClassLoc,
2450 TArgs, TemplateVals, true/*Delete args*/))
2451 return Error(SubClassLoc, "could not instantiate def");
David Greene56546132009-04-22 22:17:51 +00002452
David Greenee499a2d2011-10-05 22:42:07 +00002453 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmPrefixLoc))
2454 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002455
2456 NewRecDefs.push_back(CurRec);
David Greene56546132009-04-22 22:17:51 +00002457 }
2458
David Greenee499a2d2011-10-05 22:42:07 +00002459
David Greene56546132009-04-22 22:17:51 +00002460 if (Lex.getCode() != tgtok::comma) break;
2461 Lex.Lex(); // eat ','.
2462
2463 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002464
2465 // A defm can inherit from regular classes (non-multiclass) as
2466 // long as they come in the end of the inheritance list.
2467 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0);
2468
2469 if (InheritFromClass)
2470 break;
2471
David Greene56546132009-04-22 22:17:51 +00002472 Ref = ParseSubClassReference(0, true);
2473 }
2474
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002475 if (InheritFromClass) {
2476 // Process all the classes to inherit as if they were part of a
2477 // regular 'def' and inherit all record values.
2478 SubClassReference SubClass = ParseSubClassReference(0, false);
2479 while (1) {
2480 // Check for error.
2481 if (SubClass.Rec == 0) return true;
2482
2483 // Get the expanded definition prototypes and teach them about
2484 // the record values the current class to inherit has
2485 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2486 Record *CurRec = NewRecDefs[i];
2487
2488 // Add it.
2489 if (AddSubClass(CurRec, SubClass))
2490 return true;
2491
2492 // Process any variables on the let stack.
2493 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2494 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2495 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2496 LetStack[i][j].Bits, LetStack[i][j].Value))
2497 return true;
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002498 }
2499
2500 if (Lex.getCode() != tgtok::comma) break;
2501 Lex.Lex(); // eat ','.
2502 SubClass = ParseSubClassReference(0, false);
2503 }
2504 }
2505
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002506 if (!CurMultiClass)
2507 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene0d886402011-08-10 18:27:46 +00002508 // See Record::setName(). This resolve step will see any new
2509 // name for the def that might have been created when resolving
2510 // inheritance, values and arguments above.
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002511 NewRecDefs[i]->resolveReferences();
2512
Chris Lattnerf4601652007-11-22 20:49:04 +00002513 if (Lex.getCode() != tgtok::semi)
2514 return TokError("expected ';' at end of defm");
2515 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002516
Chris Lattnerf4601652007-11-22 20:49:04 +00002517 return false;
2518}
2519
2520/// ParseObject
2521/// Object ::= ClassInst
2522/// Object ::= DefInst
2523/// Object ::= MultiClassInst
2524/// Object ::= DefMInst
2525/// Object ::= LETCommand '{' ObjectList '}'
2526/// Object ::= LETCommand Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002527bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002528 switch (Lex.getCode()) {
Chris Lattnerd6d9dd92010-10-31 19:27:15 +00002529 default:
2530 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002531 case tgtok::Let: return ParseTopLevelLet(MC);
2532 case tgtok::Def: return ParseDef(MC);
David Greenecebb4ee2012-02-22 16:09:41 +00002533 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002534 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4601652007-11-22 20:49:04 +00002535 case tgtok::Class: return ParseClass();
2536 case tgtok::MultiClass: return ParseMultiClass();
2537 }
2538}
2539
2540/// ParseObjectList
2541/// ObjectList :== Object*
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002542bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002543 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002544 if (ParseObject(MC))
Chris Lattnerf4601652007-11-22 20:49:04 +00002545 return true;
2546 }
2547 return false;
2548}
2549
Chris Lattnerf4601652007-11-22 20:49:04 +00002550bool TGParser::ParseFile() {
2551 Lex.Lex(); // Prime the lexer.
2552 if (ParseObjectList()) return true;
Bob Wilson21870412009-11-22 04:24:42 +00002553
Chris Lattnerf4601652007-11-22 20:49:04 +00002554 // If we have unread input at the end of the file, report it.
2555 if (Lex.getCode() == tgtok::Eof)
2556 return false;
Bob Wilson21870412009-11-22 04:24:42 +00002557
Chris Lattnerf4601652007-11-22 20:49:04 +00002558 return TokError("Unexpected input at top level");
2559}
2560