blob: 8bc28f041ef0db59b63c9aa1d95199f19cbce0b0 [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,
Jordan Rosed1220092013-01-10 18:50:05 +0000386/// return 0.
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.
Jordan Rosed1220092013-01-10 18:50:05 +0000398 return 0;
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 +0000458/// ParseSubClassReference - Parse a reference to a subclass or to a templated
459/// subclass. This returns a SubClassRefTy with a null Record* on error.
460///
461/// SubClassRef ::= ClassID
462/// SubClassRef ::= ClassID '<' ValueList '>'
463///
464SubClassReference TGParser::
465ParseSubClassReference(Record *CurRec, bool isDefm) {
466 SubClassReference Result;
467 Result.RefLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +0000468
Sean Silva7be90212013-01-09 02:17:14 +0000469 if (isDefm) {
470 if (MultiClass *MC = ParseMultiClassID())
471 Result.Rec = &MC->Rec;
472 } else {
Chris Lattnerf4601652007-11-22 20:49:04 +0000473 Result.Rec = ParseClassID();
Sean Silva7be90212013-01-09 02:17:14 +0000474 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000475 if (Result.Rec == 0) return Result;
Bob Wilson21870412009-11-22 04:24:42 +0000476
Chris Lattnerf4601652007-11-22 20:49:04 +0000477 // If there is no template arg list, we're done.
478 if (Lex.getCode() != tgtok::less)
479 return Result;
480 Lex.Lex(); // Eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000481
Chris Lattnerf4601652007-11-22 20:49:04 +0000482 if (Lex.getCode() == tgtok::greater) {
483 TokError("subclass reference requires a non-empty list of template values");
484 Result.Rec = 0;
485 return Result;
486 }
Bob Wilson21870412009-11-22 04:24:42 +0000487
David Greenee1b46912009-06-08 20:23:18 +0000488 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4601652007-11-22 20:49:04 +0000489 if (Result.TemplateArgs.empty()) {
490 Result.Rec = 0; // Error parsing value list.
491 return Result;
492 }
Bob Wilson21870412009-11-22 04:24:42 +0000493
Chris Lattnerf4601652007-11-22 20:49:04 +0000494 if (Lex.getCode() != tgtok::greater) {
495 TokError("expected '>' in template value list");
496 Result.Rec = 0;
497 return Result;
498 }
499 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000500
Chris Lattnerf4601652007-11-22 20:49:04 +0000501 return Result;
502}
503
Bob Wilson32558652009-04-28 19:41:44 +0000504/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
505/// templated submulticlass. This returns a SubMultiClassRefTy with a null
506/// Record* on error.
David Greenede444af2009-04-22 16:42:54 +0000507///
508/// SubMultiClassRef ::= MultiClassID
509/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
510///
511SubMultiClassReference TGParser::
512ParseSubMultiClassReference(MultiClass *CurMC) {
513 SubMultiClassReference Result;
514 Result.RefLoc = Lex.getLoc();
Bob Wilson32558652009-04-28 19:41:44 +0000515
David Greenede444af2009-04-22 16:42:54 +0000516 Result.MC = ParseMultiClassID();
517 if (Result.MC == 0) return Result;
Bob Wilson32558652009-04-28 19:41:44 +0000518
David Greenede444af2009-04-22 16:42:54 +0000519 // If there is no template arg list, we're done.
520 if (Lex.getCode() != tgtok::less)
521 return Result;
522 Lex.Lex(); // Eat the '<'
Bob Wilson32558652009-04-28 19:41:44 +0000523
David Greenede444af2009-04-22 16:42:54 +0000524 if (Lex.getCode() == tgtok::greater) {
525 TokError("subclass reference requires a non-empty list of template values");
526 Result.MC = 0;
527 return Result;
528 }
Bob Wilson32558652009-04-28 19:41:44 +0000529
David Greenee1b46912009-06-08 20:23:18 +0000530 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greenede444af2009-04-22 16:42:54 +0000531 if (Result.TemplateArgs.empty()) {
532 Result.MC = 0; // Error parsing value list.
533 return Result;
534 }
Bob Wilson32558652009-04-28 19:41:44 +0000535
David Greenede444af2009-04-22 16:42:54 +0000536 if (Lex.getCode() != tgtok::greater) {
537 TokError("expected '>' in template value list");
538 Result.MC = 0;
539 return Result;
540 }
541 Lex.Lex();
542
543 return Result;
544}
545
Chris Lattnerf4601652007-11-22 20:49:04 +0000546/// ParseRangePiece - Parse a bit/value range.
547/// RangePiece ::= INTVAL
548/// RangePiece ::= INTVAL '-' INTVAL
549/// RangePiece ::= INTVAL INTVAL
550bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner811281e2008-01-10 07:01:53 +0000551 if (Lex.getCode() != tgtok::IntVal) {
552 TokError("expected integer or bitrange");
553 return true;
554 }
Dan Gohman63f97202008-10-17 01:33:43 +0000555 int64_t Start = Lex.getCurIntVal();
556 int64_t End;
Bob Wilson21870412009-11-22 04:24:42 +0000557
Chris Lattnerf4601652007-11-22 20:49:04 +0000558 if (Start < 0)
559 return TokError("invalid range, cannot be negative");
Bob Wilson21870412009-11-22 04:24:42 +0000560
Chris Lattnerf4601652007-11-22 20:49:04 +0000561 switch (Lex.Lex()) { // eat first character.
Bob Wilson21870412009-11-22 04:24:42 +0000562 default:
Chris Lattnerf4601652007-11-22 20:49:04 +0000563 Ranges.push_back(Start);
564 return false;
565 case tgtok::minus:
566 if (Lex.Lex() != tgtok::IntVal) {
567 TokError("expected integer value as end of range");
568 return true;
569 }
570 End = Lex.getCurIntVal();
571 break;
572 case tgtok::IntVal:
573 End = -Lex.getCurIntVal();
574 break;
575 }
Bob Wilson21870412009-11-22 04:24:42 +0000576 if (End < 0)
Chris Lattnerf4601652007-11-22 20:49:04 +0000577 return TokError("invalid range, cannot be negative");
578 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000579
Chris Lattnerf4601652007-11-22 20:49:04 +0000580 // Add to the range.
581 if (Start < End) {
582 for (; Start <= End; ++Start)
583 Ranges.push_back(Start);
584 } else {
585 for (; Start >= End; --Start)
586 Ranges.push_back(Start);
587 }
588 return false;
589}
590
591/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
592///
593/// RangeList ::= RangePiece (',' RangePiece)*
594///
595std::vector<unsigned> TGParser::ParseRangeList() {
596 std::vector<unsigned> Result;
Bob Wilson21870412009-11-22 04:24:42 +0000597
Chris Lattnerf4601652007-11-22 20:49:04 +0000598 // Parse the first piece.
599 if (ParseRangePiece(Result))
600 return std::vector<unsigned>();
601 while (Lex.getCode() == tgtok::comma) {
602 Lex.Lex(); // Eat the comma.
603
604 // Parse the next range piece.
605 if (ParseRangePiece(Result))
606 return std::vector<unsigned>();
607 }
608 return Result;
609}
610
611/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
612/// OptionalRangeList ::= '<' RangeList '>'
613/// OptionalRangeList ::= /*empty*/
614bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
615 if (Lex.getCode() != tgtok::less)
616 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000617
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000618 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000619 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000620
Chris Lattnerf4601652007-11-22 20:49:04 +0000621 // Parse the range list.
622 Ranges = ParseRangeList();
623 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000624
Chris Lattnerf4601652007-11-22 20:49:04 +0000625 if (Lex.getCode() != tgtok::greater) {
626 TokError("expected '>' at end of range list");
627 return Error(StartLoc, "to match this '<'");
628 }
629 Lex.Lex(); // eat the '>'.
630 return false;
631}
632
633/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
634/// OptionalBitList ::= '{' RangeList '}'
635/// OptionalBitList ::= /*empty*/
636bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
637 if (Lex.getCode() != tgtok::l_brace)
638 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000639
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000640 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000641 Lex.Lex(); // eat the '{'
Bob Wilson21870412009-11-22 04:24:42 +0000642
Chris Lattnerf4601652007-11-22 20:49:04 +0000643 // Parse the range list.
644 Ranges = ParseRangeList();
645 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000646
Chris Lattnerf4601652007-11-22 20:49:04 +0000647 if (Lex.getCode() != tgtok::r_brace) {
648 TokError("expected '}' at end of bit list");
649 return Error(StartLoc, "to match this '{'");
650 }
651 Lex.Lex(); // eat the '}'.
652 return false;
653}
654
655
656/// ParseType - Parse and return a tblgen type. This returns null on error.
657///
658/// Type ::= STRING // string type
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +0000659/// Type ::= CODE // code type
Chris Lattnerf4601652007-11-22 20:49:04 +0000660/// Type ::= BIT // bit type
661/// Type ::= BITS '<' INTVAL '>' // bits<x> type
662/// Type ::= INT // int type
663/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4601652007-11-22 20:49:04 +0000664/// Type ::= DAG // dag type
665/// Type ::= ClassID // Record Type
666///
667RecTy *TGParser::ParseType() {
668 switch (Lex.getCode()) {
669 default: TokError("Unknown token when expecting a type"); return 0;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000670 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +0000671 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000672 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
673 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000674 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4601652007-11-22 20:49:04 +0000675 case tgtok::Id:
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000676 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Chris Lattnerf4601652007-11-22 20:49:04 +0000677 return 0;
678 case tgtok::Bits: {
679 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
680 TokError("expected '<' after bits type");
681 return 0;
682 }
683 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
684 TokError("expected integer in bits<n> type");
685 return 0;
686 }
Dan Gohman63f97202008-10-17 01:33:43 +0000687 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4601652007-11-22 20:49:04 +0000688 if (Lex.Lex() != tgtok::greater) { // Eat count.
689 TokError("expected '>' at end of bits<n> type");
690 return 0;
691 }
692 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000693 return BitsRecTy::get(Val);
Chris Lattnerf4601652007-11-22 20:49:04 +0000694 }
695 case tgtok::List: {
696 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
697 TokError("expected '<' after list type");
698 return 0;
699 }
700 Lex.Lex(); // Eat '<'
701 RecTy *SubType = ParseType();
702 if (SubType == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +0000703
Chris Lattnerf4601652007-11-22 20:49:04 +0000704 if (Lex.getCode() != tgtok::greater) {
705 TokError("expected '>' at end of list<ty> type");
706 return 0;
707 }
708 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000709 return ListRecTy::get(SubType);
Chris Lattnerf4601652007-11-22 20:49:04 +0000710 }
Bob Wilson21870412009-11-22 04:24:42 +0000711 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000712}
713
714/// ParseIDValue - Parse an ID as a value and decode what it means.
715///
716/// IDValue ::= ID [def local value]
717/// IDValue ::= ID [def template arg]
718/// IDValue ::= ID [multiclass local value]
719/// IDValue ::= ID [multiclass template argument]
720/// IDValue ::= ID [def name]
721///
David Greenef3744a02011-10-19 13:04:20 +0000722Init *TGParser::ParseIDValue(Record *CurRec, IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000723 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
724 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000725 SMLoc Loc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000726 Lex.Lex();
727 return ParseIDValue(CurRec, Name, Loc);
728}
729
730/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
731/// has already been read.
David Greene05bce0b2011-07-29 22:43:06 +0000732Init *TGParser::ParseIDValue(Record *CurRec,
David Greenef3744a02011-10-19 13:04:20 +0000733 const std::string &Name, SMLoc NameLoc,
734 IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000735 if (CurRec) {
736 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenedcd35c72011-07-29 19:07:07 +0000737 return VarInit::get(Name, RV->getType());
Bob Wilson21870412009-11-22 04:24:42 +0000738
David Greenee22b3212011-10-19 13:02:42 +0000739 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
740
David Greenecaa25c82011-10-05 22:42:54 +0000741 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +0000742 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
743 "::");
David Greenecaa25c82011-10-05 22:42:54 +0000744
Chris Lattnerf4601652007-11-22 20:49:04 +0000745 if (CurRec->isTemplateArg(TemplateArgName)) {
746 const RecordVal *RV = CurRec->getValue(TemplateArgName);
747 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000748 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000749 }
750 }
Bob Wilson21870412009-11-22 04:24:42 +0000751
Chris Lattnerf4601652007-11-22 20:49:04 +0000752 if (CurMultiClass) {
David Greenee22b3212011-10-19 13:02:42 +0000753 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
754 "::");
755
Chris Lattnerf4601652007-11-22 20:49:04 +0000756 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
757 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
758 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000759 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000760 }
761 }
Bob Wilson21870412009-11-22 04:24:42 +0000762
David Greenecebb4ee2012-02-22 16:09:41 +0000763 // If this is in a foreach loop, make sure it's not a loop iterator
764 for (LoopVector::iterator i = Loops.begin(), iend = Loops.end();
765 i != iend;
766 ++i) {
Sean Silva6cfc8062012-10-10 20:24:43 +0000767 VarInit *IterVar = dyn_cast<VarInit>(i->IterVar);
David Greenecebb4ee2012-02-22 16:09:41 +0000768 if (IterVar && IterVar->getName() == Name)
769 return IterVar;
770 }
771
David Greenebbec2792011-10-19 13:04:21 +0000772 if (Mode == ParseNameMode)
773 return StringInit::get(Name);
774
Chris Lattnerf4601652007-11-22 20:49:04 +0000775 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000776 return DefInit::get(D);
Chris Lattnerf4601652007-11-22 20:49:04 +0000777
David Greenebbec2792011-10-19 13:04:21 +0000778 if (Mode == ParseValueMode) {
779 Error(NameLoc, "Variable not defined: '" + Name + "'");
780 return 0;
781 }
782
783 return StringInit::get(Name);
Chris Lattnerf4601652007-11-22 20:49:04 +0000784}
785
David Greened418c1b2009-05-14 20:54:48 +0000786/// ParseOperation - Parse an operator. This returns null on error.
787///
788/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
789///
David Greene05bce0b2011-07-29 22:43:06 +0000790Init *TGParser::ParseOperation(Record *CurRec) {
David Greened418c1b2009-05-14 20:54:48 +0000791 switch (Lex.getCode()) {
792 default:
793 TokError("unknown operation");
794 return 0;
David Greene1434f662011-01-07 17:05:37 +0000795 case tgtok::XHead:
796 case tgtok::XTail:
797 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +0000798 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
799 UnOpInit::UnaryOp Code;
800 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000801
David Greenee6c27de2009-05-14 21:22:49 +0000802 switch (Lex.getCode()) {
Craig Topper85814382012-02-07 05:05:23 +0000803 default: llvm_unreachable("Unhandled code!");
David Greenee6c27de2009-05-14 21:22:49 +0000804 case tgtok::XCast:
805 Lex.Lex(); // eat the operation
806 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000807
David Greenee6c27de2009-05-14 21:22:49 +0000808 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000809
David Greenee6c27de2009-05-14 21:22:49 +0000810 if (Type == 0) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000811 TokError("did not get type for unary operator");
David Greenee6c27de2009-05-14 21:22:49 +0000812 return 0;
813 }
David Greened418c1b2009-05-14 20:54:48 +0000814
David Greenee6c27de2009-05-14 21:22:49 +0000815 break;
David Greene1434f662011-01-07 17:05:37 +0000816 case tgtok::XHead:
David Greene5f9f9ba2009-05-14 22:38:31 +0000817 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000818 Code = UnOpInit::HEAD;
David Greene5f9f9ba2009-05-14 22:38:31 +0000819 break;
David Greene1434f662011-01-07 17:05:37 +0000820 case tgtok::XTail:
David Greene5f9f9ba2009-05-14 22:38:31 +0000821 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000822 Code = UnOpInit::TAIL;
David Greene5f9f9ba2009-05-14 22:38:31 +0000823 break;
David Greene1434f662011-01-07 17:05:37 +0000824 case tgtok::XEmpty:
David Greene5f9f9ba2009-05-14 22:38:31 +0000825 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000826 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000827 Type = IntRecTy::get();
David Greene5f9f9ba2009-05-14 22:38:31 +0000828 break;
David Greenee6c27de2009-05-14 21:22:49 +0000829 }
830 if (Lex.getCode() != tgtok::l_paren) {
831 TokError("expected '(' after unary operator");
832 return 0;
833 }
834 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000835
David Greene05bce0b2011-07-29 22:43:06 +0000836 Init *LHS = ParseValue(CurRec);
David Greenee6c27de2009-05-14 21:22:49 +0000837 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000838
David Greene1434f662011-01-07 17:05:37 +0000839 if (Code == UnOpInit::HEAD
840 || Code == UnOpInit::TAIL
841 || Code == UnOpInit::EMPTY) {
Sean Silva6cfc8062012-10-10 20:24:43 +0000842 ListInit *LHSl = dyn_cast<ListInit>(LHS);
843 StringInit *LHSs = dyn_cast<StringInit>(LHS);
844 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000845 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
846 TokError("expected list or string type argument in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000847 return 0;
848 }
849 if (LHSt) {
Sean Silva736ceac2012-10-05 03:31:58 +0000850 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
851 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
David Greenee1b46912009-06-08 20:23:18 +0000852 if (LType == 0 && SType == 0) {
853 TokError("expected list or string type argumnet in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000854 return 0;
855 }
856 }
857
David Greene1434f662011-01-07 17:05:37 +0000858 if (Code == UnOpInit::HEAD
859 || Code == UnOpInit::TAIL) {
David Greenee1b46912009-06-08 20:23:18 +0000860 if (LHSl == 0 && LHSt == 0) {
861 TokError("expected list type argumnet in unary operator");
862 return 0;
863 }
Bob Wilson21870412009-11-22 04:24:42 +0000864
David Greene5f9f9ba2009-05-14 22:38:31 +0000865 if (LHSl && LHSl->getSize() == 0) {
866 TokError("empty list argument in unary operator");
867 return 0;
868 }
869 if (LHSl) {
David Greene05bce0b2011-07-29 22:43:06 +0000870 Init *Item = LHSl->getElement(0);
Sean Silva6cfc8062012-10-10 20:24:43 +0000871 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
David Greene5f9f9ba2009-05-14 22:38:31 +0000872 if (Itemt == 0) {
873 TokError("untyped list element in unary operator");
874 return 0;
875 }
David Greene1434f662011-01-07 17:05:37 +0000876 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000877 Type = Itemt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000878 } else {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000879 Type = ListRecTy::get(Itemt->getType());
David Greene5f9f9ba2009-05-14 22:38:31 +0000880 }
Bob Wilson21870412009-11-22 04:24:42 +0000881 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000882 assert(LHSt && "expected list type argument in unary operator");
Sean Silva736ceac2012-10-05 03:31:58 +0000883 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
David Greene5f9f9ba2009-05-14 22:38:31 +0000884 if (LType == 0) {
885 TokError("expected list type argumnet in unary operator");
886 return 0;
887 }
David Greene1434f662011-01-07 17:05:37 +0000888 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000889 Type = LType->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +0000890 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000891 Type = LType;
892 }
893 }
894 }
895 }
896
David Greenee6c27de2009-05-14 21:22:49 +0000897 if (Lex.getCode() != tgtok::r_paren) {
898 TokError("expected ')' in unary operator");
899 return 0;
900 }
901 Lex.Lex(); // eat the ')'
David Greenedcd35c72011-07-29 19:07:07 +0000902 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee6c27de2009-05-14 21:22:49 +0000903 }
David Greened418c1b2009-05-14 20:54:48 +0000904
905 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +0000906 case tgtok::XSRA:
David Greened418c1b2009-05-14 20:54:48 +0000907 case tgtok::XSRL:
908 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +0000909 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +0000910 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000911 tgtok::TokKind OpTok = Lex.getCode();
912 SMLoc OpLoc = Lex.getLoc();
913 Lex.Lex(); // eat the operation
914
David Greened418c1b2009-05-14 20:54:48 +0000915 BinOpInit::BinaryOp Code;
916 RecTy *Type = 0;
917
Chris Lattner8d978a72010-10-05 23:58:18 +0000918 switch (OpTok) {
Craig Topper85814382012-02-07 05:05:23 +0000919 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000920 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
921 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
922 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
923 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
924 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Bob Wilson21870412009-11-22 04:24:42 +0000925 case tgtok::XStrConcat:
David Greened418c1b2009-05-14 20:54:48 +0000926 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000927 Type = StringRecTy::get();
David Greened418c1b2009-05-14 20:54:48 +0000928 break;
David Greened418c1b2009-05-14 20:54:48 +0000929 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000930
David Greened418c1b2009-05-14 20:54:48 +0000931 if (Lex.getCode() != tgtok::l_paren) {
932 TokError("expected '(' after binary operator");
933 return 0;
934 }
935 Lex.Lex(); // eat the '('
936
David Greene05bce0b2011-07-29 22:43:06 +0000937 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000938
Chris Lattner8d978a72010-10-05 23:58:18 +0000939 InitList.push_back(ParseValue(CurRec));
940 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000941
Chris Lattner8d978a72010-10-05 23:58:18 +0000942 while (Lex.getCode() == tgtok::comma) {
943 Lex.Lex(); // eat the ','
944
945 InitList.push_back(ParseValue(CurRec));
946 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000947 }
David Greened418c1b2009-05-14 20:54:48 +0000948
949 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner8d978a72010-10-05 23:58:18 +0000950 TokError("expected ')' in operator");
David Greened418c1b2009-05-14 20:54:48 +0000951 return 0;
952 }
953 Lex.Lex(); // eat the ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000954
955 // We allow multiple operands to associative operators like !strconcat as
956 // shorthand for nesting them.
957 if (Code == BinOpInit::STRCONCAT) {
958 while (InitList.size() > 2) {
David Greene05bce0b2011-07-29 22:43:06 +0000959 Init *RHS = InitList.pop_back_val();
David Greenedcd35c72011-07-29 19:07:07 +0000960 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
961 ->Fold(CurRec, CurMultiClass);
Chris Lattner8d978a72010-10-05 23:58:18 +0000962 InitList.back() = RHS;
963 }
964 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000965
Chris Lattner8d978a72010-10-05 23:58:18 +0000966 if (InitList.size() == 2)
David Greenedcd35c72011-07-29 19:07:07 +0000967 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner8d978a72010-10-05 23:58:18 +0000968 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000969
Chris Lattner8d978a72010-10-05 23:58:18 +0000970 Error(OpLoc, "expected two operands to operator");
971 return 0;
David Greened418c1b2009-05-14 20:54:48 +0000972 }
973
David Greene9bea7c82009-05-14 23:26:46 +0000974 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +0000975 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +0000976 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
977 TernOpInit::TernaryOp Code;
978 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000979
David Greene4afc5092009-05-14 21:54:42 +0000980 tgtok::TokKind LexCode = Lex.getCode();
981 Lex.Lex(); // eat the operation
982 switch (LexCode) {
Craig Topper85814382012-02-07 05:05:23 +0000983 default: llvm_unreachable("Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000984 case tgtok::XIf:
985 Code = TernOpInit::IF;
986 break;
David Greenebeb31a52009-05-14 22:23:47 +0000987 case tgtok::XForEach:
988 Code = TernOpInit::FOREACH;
989 break;
David Greene4afc5092009-05-14 21:54:42 +0000990 case tgtok::XSubst:
991 Code = TernOpInit::SUBST;
992 break;
993 }
994 if (Lex.getCode() != tgtok::l_paren) {
995 TokError("expected '(' after ternary operator");
996 return 0;
997 }
998 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000999
David Greene05bce0b2011-07-29 22:43:06 +00001000 Init *LHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001001 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001002
David Greene4afc5092009-05-14 21:54:42 +00001003 if (Lex.getCode() != tgtok::comma) {
1004 TokError("expected ',' in ternary operator");
1005 return 0;
1006 }
1007 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001008
David Greene05bce0b2011-07-29 22:43:06 +00001009 Init *MHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001010 if (MHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001011
David Greene4afc5092009-05-14 21:54:42 +00001012 if (Lex.getCode() != tgtok::comma) {
1013 TokError("expected ',' in ternary operator");
1014 return 0;
1015 }
1016 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001017
David Greene05bce0b2011-07-29 22:43:06 +00001018 Init *RHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001019 if (RHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001020
David Greene4afc5092009-05-14 21:54:42 +00001021 if (Lex.getCode() != tgtok::r_paren) {
1022 TokError("expected ')' in binary operator");
1023 return 0;
1024 }
1025 Lex.Lex(); // eat the ')'
David Greened418c1b2009-05-14 20:54:48 +00001026
David Greene4afc5092009-05-14 21:54:42 +00001027 switch (LexCode) {
Craig Topper85814382012-02-07 05:05:23 +00001028 default: llvm_unreachable("Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +00001029 case tgtok::XIf: {
Bill Wendling548f5a02010-12-13 01:46:19 +00001030 RecTy *MHSTy = 0;
1031 RecTy *RHSTy = 0;
1032
Sean Silva6cfc8062012-10-10 20:24:43 +00001033 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling548f5a02010-12-13 01:46:19 +00001034 MHSTy = MHSt->getType();
Sean Silva6cfc8062012-10-10 20:24:43 +00001035 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao307525c2012-09-06 23:32:48 +00001036 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva3f7b7f82012-10-10 20:24:47 +00001037 if (isa<BitInit>(MHS))
Michael Liao307525c2012-09-06 23:32:48 +00001038 MHSTy = BitRecTy::get();
1039
Sean Silva6cfc8062012-10-10 20:24:43 +00001040 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling548f5a02010-12-13 01:46:19 +00001041 RHSTy = RHSt->getType();
Sean Silva6cfc8062012-10-10 20:24:43 +00001042 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao307525c2012-09-06 23:32:48 +00001043 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva3f7b7f82012-10-10 20:24:47 +00001044 if (isa<BitInit>(RHS))
Michael Liao307525c2012-09-06 23:32:48 +00001045 RHSTy = BitRecTy::get();
1046
1047 // For UnsetInit, it's typed from the other hand.
Sean Silva3f7b7f82012-10-10 20:24:47 +00001048 if (isa<UnsetInit>(MHS))
Michael Liao307525c2012-09-06 23:32:48 +00001049 MHSTy = RHSTy;
Sean Silva3f7b7f82012-10-10 20:24:47 +00001050 if (isa<UnsetInit>(RHS))
Michael Liao307525c2012-09-06 23:32:48 +00001051 RHSTy = MHSTy;
Bill Wendling548f5a02010-12-13 01:46:19 +00001052
1053 if (!MHSTy || !RHSTy) {
David Greene9bea7c82009-05-14 23:26:46 +00001054 TokError("could not get type for !if");
1055 return 0;
1056 }
Bill Wendling548f5a02010-12-13 01:46:19 +00001057
1058 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1059 Type = RHSTy;
1060 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1061 Type = MHSTy;
Bob Wilson21870412009-11-22 04:24:42 +00001062 } else {
David Greene9bea7c82009-05-14 23:26:46 +00001063 TokError("inconsistent types for !if");
1064 return 0;
1065 }
1066 break;
1067 }
David Greenebeb31a52009-05-14 22:23:47 +00001068 case tgtok::XForEach: {
Sean Silva6cfc8062012-10-10 20:24:43 +00001069 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
David Greenebeb31a52009-05-14 22:23:47 +00001070 if (MHSt == 0) {
1071 TokError("could not get type for !foreach");
1072 return 0;
1073 }
1074 Type = MHSt->getType();
1075 break;
1076 }
David Greene4afc5092009-05-14 21:54:42 +00001077 case tgtok::XSubst: {
Sean Silva6cfc8062012-10-10 20:24:43 +00001078 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
David Greene4afc5092009-05-14 21:54:42 +00001079 if (RHSt == 0) {
1080 TokError("could not get type for !subst");
1081 return 0;
1082 }
1083 Type = RHSt->getType();
1084 break;
1085 }
1086 }
David Greenedcd35c72011-07-29 19:07:07 +00001087 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson21870412009-11-22 04:24:42 +00001088 CurMultiClass);
David Greene4afc5092009-05-14 21:54:42 +00001089 }
David Greened418c1b2009-05-14 20:54:48 +00001090 }
David Greened418c1b2009-05-14 20:54:48 +00001091}
1092
1093/// ParseOperatorType - Parse a type for an operator. This returns
1094/// null on error.
1095///
1096/// OperatorType ::= '<' Type '>'
1097///
Dan Gohmana9ad0412009-08-12 22:10:57 +00001098RecTy *TGParser::ParseOperatorType() {
David Greened418c1b2009-05-14 20:54:48 +00001099 RecTy *Type = 0;
1100
1101 if (Lex.getCode() != tgtok::less) {
1102 TokError("expected type name for operator");
1103 return 0;
1104 }
1105 Lex.Lex(); // eat the <
1106
1107 Type = ParseType();
1108
1109 if (Type == 0) {
1110 TokError("expected type name for operator");
1111 return 0;
1112 }
1113
1114 if (Lex.getCode() != tgtok::greater) {
1115 TokError("expected type name for operator");
1116 return 0;
1117 }
1118 Lex.Lex(); // eat the >
1119
1120 return Type;
1121}
1122
1123
Chris Lattnerf4601652007-11-22 20:49:04 +00001124/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1125///
1126/// SimpleValue ::= IDValue
1127/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001128/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +00001129/// SimpleValue ::= CODEFRAGMENT
1130/// SimpleValue ::= '?'
1131/// SimpleValue ::= '{' ValueList '}'
1132/// SimpleValue ::= ID '<' ValueListNE '>'
1133/// SimpleValue ::= '[' ValueList ']'
1134/// SimpleValue ::= '(' IDValue DagArgList ')'
1135/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
1136/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1137/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1138/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1139/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1140///
David Greenef3744a02011-10-19 13:04:20 +00001141Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1142 IDParseMode Mode) {
David Greene05bce0b2011-07-29 22:43:06 +00001143 Init *R = 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001144 switch (Lex.getCode()) {
1145 default: TokError("Unknown token when parsing a value"); break;
David Greened3d1cad2011-10-19 13:04:43 +00001146 case tgtok::paste:
1147 // This is a leading paste operation. This is deprecated but
1148 // still exists in some .td files. Ignore it.
1149 Lex.Lex(); // Skip '#'.
1150 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenedcd35c72011-07-29 19:07:07 +00001151 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001152 case tgtok::StrVal: {
1153 std::string Val = Lex.getCurStrVal();
1154 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001155
Jim Grosbachda4231f2009-03-26 16:17:51 +00001156 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001157 while (Lex.getCode() == tgtok::StrVal) {
1158 Val += Lex.getCurStrVal();
1159 Lex.Lex();
1160 }
Bob Wilson21870412009-11-22 04:24:42 +00001161
David Greenedcd35c72011-07-29 19:07:07 +00001162 R = StringInit::get(Val);
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001163 break;
1164 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001165 case tgtok::CodeFragment:
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +00001166 R = StringInit::get(Lex.getCurStrVal());
Chris Lattner578bcf02010-10-06 04:31:40 +00001167 Lex.Lex();
1168 break;
1169 case tgtok::question:
David Greenedcd35c72011-07-29 19:07:07 +00001170 R = UnsetInit::get();
Chris Lattner578bcf02010-10-06 04:31:40 +00001171 Lex.Lex();
1172 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001173 case tgtok::Id: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001174 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001175 std::string Name = Lex.getCurStrVal();
1176 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greenef3744a02011-10-19 13:04:20 +00001177 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson21870412009-11-22 04:24:42 +00001178
Chris Lattnerf4601652007-11-22 20:49:04 +00001179 // Value ::= ID '<' ValueListNE '>'
1180 if (Lex.Lex() == tgtok::greater) {
1181 TokError("expected non-empty value list");
1182 return 0;
1183 }
David Greenee1b46912009-06-08 20:23:18 +00001184
Chris Lattnerf4601652007-11-22 20:49:04 +00001185 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1186 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1187 // body.
1188 Record *Class = Records.getClass(Name);
1189 if (!Class) {
1190 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1191 return 0;
1192 }
David Greenee1b46912009-06-08 20:23:18 +00001193
David Greene05bce0b2011-07-29 22:43:06 +00001194 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
David Greenee1b46912009-06-08 20:23:18 +00001195 if (ValueList.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001196
David Greenee1b46912009-06-08 20:23:18 +00001197 if (Lex.getCode() != tgtok::greater) {
1198 TokError("expected '>' at end of value list");
1199 return 0;
1200 }
1201 Lex.Lex(); // eat the '>'
Bob Wilson21870412009-11-22 04:24:42 +00001202
Chris Lattnerf4601652007-11-22 20:49:04 +00001203 // Create the new record, set it as CurRec temporarily.
1204 static unsigned AnonCounter = 0;
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001205 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),
1206 NameLoc,
Jordan Rosed1220092013-01-10 18:50:05 +00001207 Records,
1208 /*IsAnonymous=*/true);
Chris Lattnerf4601652007-11-22 20:49:04 +00001209 SubClassReference SCRef;
1210 SCRef.RefLoc = NameLoc;
1211 SCRef.Rec = Class;
1212 SCRef.TemplateArgs = ValueList;
1213 // Add info about the subclass to NewRec.
1214 if (AddSubClass(NewRec, SCRef))
1215 return 0;
1216 NewRec->resolveReferences();
1217 Records.addDef(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001218
Chris Lattnerf4601652007-11-22 20:49:04 +00001219 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +00001220 return DefInit::get(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001221 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001222 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001223 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001224 Lex.Lex(); // eat the '{'
David Greene05bce0b2011-07-29 22:43:06 +00001225 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001226
Chris Lattnerf4601652007-11-22 20:49:04 +00001227 if (Lex.getCode() != tgtok::r_brace) {
1228 Vals = ParseValueList(CurRec);
1229 if (Vals.empty()) return 0;
1230 }
1231 if (Lex.getCode() != tgtok::r_brace) {
1232 TokError("expected '}' at end of bit list value");
1233 return 0;
1234 }
1235 Lex.Lex(); // eat the '}'
Bob Wilson21870412009-11-22 04:24:42 +00001236
David Greene05bce0b2011-07-29 22:43:06 +00001237 SmallVector<Init *, 16> NewBits(Vals.size());
David Greeneca7fd3d2011-07-29 19:07:00 +00001238
Chris Lattnerf4601652007-11-22 20:49:04 +00001239 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001240 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Chris Lattnerf4601652007-11-22 20:49:04 +00001241 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +00001242 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1243 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +00001244 return 0;
1245 }
David Greeneca7fd3d2011-07-29 19:07:00 +00001246 NewBits[Vals.size()-i-1] = Bit;
Chris Lattnerf4601652007-11-22 20:49:04 +00001247 }
David Greenedcd35c72011-07-29 19:07:07 +00001248 return BitsInit::get(NewBits);
Chris Lattnerf4601652007-11-22 20:49:04 +00001249 }
1250 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1251 Lex.Lex(); // eat the '['
David Greene05bce0b2011-07-29 22:43:06 +00001252 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001253
David Greenee1b46912009-06-08 20:23:18 +00001254 RecTy *DeducedEltTy = 0;
1255 ListRecTy *GivenListTy = 0;
Bob Wilson21870412009-11-22 04:24:42 +00001256
David Greenee1b46912009-06-08 20:23:18 +00001257 if (ItemType != 0) {
Sean Silva736ceac2012-10-05 03:31:58 +00001258 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
David Greenee1b46912009-06-08 20:23:18 +00001259 if (ListType == 0) {
1260 std::stringstream s;
Bob Wilson21870412009-11-22 04:24:42 +00001261 s << "Type mismatch for list, expected list type, got "
David Greenee1b46912009-06-08 20:23:18 +00001262 << ItemType->getAsString();
1263 TokError(s.str());
Jim Grosbach6a44ada2011-03-11 19:52:52 +00001264 return 0;
David Greenee1b46912009-06-08 20:23:18 +00001265 }
1266 GivenListTy = ListType;
Bob Wilson21870412009-11-22 04:24:42 +00001267 }
David Greenee1b46912009-06-08 20:23:18 +00001268
Chris Lattnerf4601652007-11-22 20:49:04 +00001269 if (Lex.getCode() != tgtok::r_square) {
Bob Wilson21870412009-11-22 04:24:42 +00001270 Vals = ParseValueList(CurRec, 0,
1271 GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001272 if (Vals.empty()) return 0;
1273 }
1274 if (Lex.getCode() != tgtok::r_square) {
1275 TokError("expected ']' at end of list value");
1276 return 0;
1277 }
1278 Lex.Lex(); // eat the ']'
David Greenee1b46912009-06-08 20:23:18 +00001279
1280 RecTy *GivenEltTy = 0;
1281 if (Lex.getCode() == tgtok::less) {
1282 // Optional list element type
1283 Lex.Lex(); // eat the '<'
1284
1285 GivenEltTy = ParseType();
1286 if (GivenEltTy == 0) {
1287 // Couldn't parse element type
1288 return 0;
1289 }
1290
1291 if (Lex.getCode() != tgtok::greater) {
1292 TokError("expected '>' at end of list element type");
1293 return 0;
1294 }
1295 Lex.Lex(); // eat the '>'
1296 }
1297
1298 // Check elements
1299 RecTy *EltTy = 0;
David Greene05bce0b2011-07-29 22:43:06 +00001300 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greenee1b46912009-06-08 20:23:18 +00001301 i != ie;
1302 ++i) {
Sean Silva6cfc8062012-10-10 20:24:43 +00001303 TypedInit *TArg = dyn_cast<TypedInit>(*i);
David Greenee1b46912009-06-08 20:23:18 +00001304 if (TArg == 0) {
1305 TokError("Untyped list element");
1306 return 0;
1307 }
1308 if (EltTy != 0) {
1309 EltTy = resolveTypes(EltTy, TArg->getType());
1310 if (EltTy == 0) {
1311 TokError("Incompatible types in list elements");
1312 return 0;
1313 }
Bob Wilson21870412009-11-22 04:24:42 +00001314 } else {
David Greenee1b46912009-06-08 20:23:18 +00001315 EltTy = TArg->getType();
1316 }
1317 }
1318
1319 if (GivenEltTy != 0) {
1320 if (EltTy != 0) {
1321 // Verify consistency
1322 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1323 TokError("Incompatible types in list elements");
1324 return 0;
1325 }
1326 }
1327 EltTy = GivenEltTy;
1328 }
1329
1330 if (EltTy == 0) {
1331 if (ItemType == 0) {
1332 TokError("No type for list");
1333 return 0;
1334 }
1335 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +00001336 } else {
David Greenee1b46912009-06-08 20:23:18 +00001337 // Make sure the deduced type is compatible with the given type
1338 if (GivenListTy) {
1339 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1340 TokError("Element type mismatch for list");
1341 return 0;
1342 }
1343 }
1344 DeducedEltTy = EltTy;
1345 }
Bob Wilson21870412009-11-22 04:24:42 +00001346
David Greenedcd35c72011-07-29 19:07:07 +00001347 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4601652007-11-22 20:49:04 +00001348 }
1349 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1350 Lex.Lex(); // eat the '('
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001351 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001352 TokError("expected identifier in dag init");
1353 return 0;
1354 }
Bob Wilson21870412009-11-22 04:24:42 +00001355
David Greene05bce0b2011-07-29 22:43:06 +00001356 Init *Operator = ParseValue(CurRec);
Chris Lattner578bcf02010-10-06 04:31:40 +00001357 if (Operator == 0) return 0;
David Greenec7cafcd2009-04-22 20:18:10 +00001358
Nate Begeman7cee8172009-03-19 05:21:56 +00001359 // If the operator name is present, parse it.
1360 std::string OperatorName;
1361 if (Lex.getCode() == tgtok::colon) {
1362 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1363 TokError("expected variable name in dag operator");
1364 return 0;
1365 }
1366 OperatorName = Lex.getCurStrVal();
1367 Lex.Lex(); // eat the VarName.
1368 }
Bob Wilson21870412009-11-22 04:24:42 +00001369
David Greene05bce0b2011-07-29 22:43:06 +00001370 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4601652007-11-22 20:49:04 +00001371 if (Lex.getCode() != tgtok::r_paren) {
1372 DagArgs = ParseDagArgList(CurRec);
1373 if (DagArgs.empty()) return 0;
1374 }
Bob Wilson21870412009-11-22 04:24:42 +00001375
Chris Lattnerf4601652007-11-22 20:49:04 +00001376 if (Lex.getCode() != tgtok::r_paren) {
1377 TokError("expected ')' in dag init");
1378 return 0;
1379 }
1380 Lex.Lex(); // eat the ')'
Bob Wilson21870412009-11-22 04:24:42 +00001381
David Greenedcd35c72011-07-29 19:07:07 +00001382 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4601652007-11-22 20:49:04 +00001383 }
Bob Wilson21870412009-11-22 04:24:42 +00001384
David Greene1434f662011-01-07 17:05:37 +00001385 case tgtok::XHead:
1386 case tgtok::XTail:
1387 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +00001388 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001389 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +00001390 case tgtok::XSRA:
Chris Lattnerf4601652007-11-22 20:49:04 +00001391 case tgtok::XSRL:
1392 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +00001393 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001394 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene9bea7c82009-05-14 23:26:46 +00001395 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +00001396 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001397 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greened418c1b2009-05-14 20:54:48 +00001398 return ParseOperation(CurRec);
Chris Lattnerf4601652007-11-22 20:49:04 +00001399 }
1400 }
Bob Wilson21870412009-11-22 04:24:42 +00001401
Chris Lattnerf4601652007-11-22 20:49:04 +00001402 return R;
1403}
1404
1405/// ParseValue - Parse a tblgen value. This returns null on error.
1406///
1407/// Value ::= SimpleValue ValueSuffix*
1408/// ValueSuffix ::= '{' BitList '}'
1409/// ValueSuffix ::= '[' BitList ']'
1410/// ValueSuffix ::= '.' ID
1411///
David Greenef3744a02011-10-19 13:04:20 +00001412Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1413 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Chris Lattnerf4601652007-11-22 20:49:04 +00001414 if (Result == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001415
Chris Lattnerf4601652007-11-22 20:49:04 +00001416 // Parse the suffixes now if present.
1417 while (1) {
1418 switch (Lex.getCode()) {
1419 default: return Result;
1420 case tgtok::l_brace: {
David Greenecebb4ee2012-02-22 16:09:41 +00001421 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greene8592b2b2011-10-19 13:04:26 +00001422 // This is the beginning of the object body.
1423 return Result;
1424
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001425 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001426 Lex.Lex(); // eat the '{'
1427 std::vector<unsigned> Ranges = ParseRangeList();
1428 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001429
Chris Lattnerf4601652007-11-22 20:49:04 +00001430 // Reverse the bitlist.
1431 std::reverse(Ranges.begin(), Ranges.end());
1432 Result = Result->convertInitializerBitRange(Ranges);
1433 if (Result == 0) {
1434 Error(CurlyLoc, "Invalid bit range for value");
1435 return 0;
1436 }
Bob Wilson21870412009-11-22 04:24:42 +00001437
Chris Lattnerf4601652007-11-22 20:49:04 +00001438 // Eat the '}'.
1439 if (Lex.getCode() != tgtok::r_brace) {
1440 TokError("expected '}' at end of bit range list");
1441 return 0;
1442 }
1443 Lex.Lex();
1444 break;
1445 }
1446 case tgtok::l_square: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001447 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001448 Lex.Lex(); // eat the '['
1449 std::vector<unsigned> Ranges = ParseRangeList();
1450 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001451
Chris Lattnerf4601652007-11-22 20:49:04 +00001452 Result = Result->convertInitListSlice(Ranges);
1453 if (Result == 0) {
1454 Error(SquareLoc, "Invalid range for list slice");
1455 return 0;
1456 }
Bob Wilson21870412009-11-22 04:24:42 +00001457
Chris Lattnerf4601652007-11-22 20:49:04 +00001458 // Eat the ']'.
1459 if (Lex.getCode() != tgtok::r_square) {
1460 TokError("expected ']' at end of list slice");
1461 return 0;
1462 }
1463 Lex.Lex();
1464 break;
1465 }
1466 case tgtok::period:
1467 if (Lex.Lex() != tgtok::Id) { // eat the .
1468 TokError("expected field identifier after '.'");
1469 return 0;
1470 }
1471 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001472 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00001473 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +00001474 return 0;
1475 }
David Greenedcd35c72011-07-29 19:07:07 +00001476 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001477 Lex.Lex(); // eat field name
1478 break;
David Greened3d1cad2011-10-19 13:04:43 +00001479
1480 case tgtok::paste:
1481 SMLoc PasteLoc = Lex.getLoc();
1482
1483 // Create a !strconcat() operation, first casting each operand to
1484 // a string if necessary.
1485
Sean Silva6cfc8062012-10-10 20:24:43 +00001486 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greened3d1cad2011-10-19 13:04:43 +00001487 if (!LHS) {
1488 Error(PasteLoc, "LHS of paste is not typed!");
1489 return 0;
1490 }
1491
1492 if (LHS->getType() != StringRecTy::get()) {
1493 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1494 }
1495
1496 TypedInit *RHS = 0;
1497
1498 Lex.Lex(); // Eat the '#'.
1499 switch (Lex.getCode()) {
1500 case tgtok::colon:
1501 case tgtok::semi:
1502 case tgtok::l_brace:
1503 // These are all of the tokens that can begin an object body.
1504 // Some of these can also begin values but we disallow those cases
1505 // because they are unlikely to be useful.
1506
1507 // Trailing paste, concat with an empty string.
1508 RHS = StringInit::get("");
1509 break;
1510
1511 default:
1512 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silva6cfc8062012-10-10 20:24:43 +00001513 RHS = dyn_cast<TypedInit>(RHSResult);
David Greened3d1cad2011-10-19 13:04:43 +00001514 if (!RHS) {
1515 Error(PasteLoc, "RHS of paste is not typed!");
1516 return 0;
1517 }
1518
1519 if (RHS->getType() != StringRecTy::get()) {
1520 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1521 }
1522
1523 break;
1524 }
1525
1526 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1527 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1528 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001529 }
1530 }
1531}
1532
1533/// ParseDagArgList - Parse the argument list for a dag literal expression.
1534///
1535/// ParseDagArgList ::= Value (':' VARNAME)?
1536/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
David Greene05bce0b2011-07-29 22:43:06 +00001537std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4601652007-11-22 20:49:04 +00001538TGParser::ParseDagArgList(Record *CurRec) {
David Greene05bce0b2011-07-29 22:43:06 +00001539 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson21870412009-11-22 04:24:42 +00001540
Chris Lattnerf4601652007-11-22 20:49:04 +00001541 while (1) {
David Greene05bce0b2011-07-29 22:43:06 +00001542 Init *Val = ParseValue(CurRec);
1543 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
Bob Wilson21870412009-11-22 04:24:42 +00001544
Chris Lattnerf4601652007-11-22 20:49:04 +00001545 // If the variable name is present, add it.
1546 std::string VarName;
1547 if (Lex.getCode() == tgtok::colon) {
1548 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1549 TokError("expected variable name in dag literal");
David Greene05bce0b2011-07-29 22:43:06 +00001550 return std::vector<std::pair<llvm::Init*, std::string> >();
Chris Lattnerf4601652007-11-22 20:49:04 +00001551 }
1552 VarName = Lex.getCurStrVal();
1553 Lex.Lex(); // eat the VarName.
1554 }
Bob Wilson21870412009-11-22 04:24:42 +00001555
Chris Lattnerf4601652007-11-22 20:49:04 +00001556 Result.push_back(std::make_pair(Val, VarName));
Bob Wilson21870412009-11-22 04:24:42 +00001557
Chris Lattnerf4601652007-11-22 20:49:04 +00001558 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson21870412009-11-22 04:24:42 +00001559 Lex.Lex(); // eat the ','
Chris Lattnerf4601652007-11-22 20:49:04 +00001560 }
Bob Wilson21870412009-11-22 04:24:42 +00001561
Chris Lattnerf4601652007-11-22 20:49:04 +00001562 return Result;
1563}
1564
1565
1566/// ParseValueList - Parse a comma separated list of values, returning them as a
1567/// vector. Note that this always expects to be able to parse at least one
1568/// value. It returns an empty list if this is not possible.
1569///
1570/// ValueList ::= Value (',' Value)
1571///
David Greene05bce0b2011-07-29 22:43:06 +00001572std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopherd568b3f2011-07-11 23:06:52 +00001573 RecTy *EltTy) {
David Greene05bce0b2011-07-29 22:43:06 +00001574 std::vector<Init*> Result;
David Greenee1b46912009-06-08 20:23:18 +00001575 RecTy *ItemType = EltTy;
David Greene67acdf22009-06-29 19:59:52 +00001576 unsigned int ArgN = 0;
David Greenee1b46912009-06-08 20:23:18 +00001577 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001578 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Jim Grosbachb1320cb2012-01-20 20:02:39 +00001579 if (!TArgs.size()) {
1580 TokError("template argument provided to non-template class");
1581 return std::vector<Init*>();
1582 }
David Greenee1b46912009-06-08 20:23:18 +00001583 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greened9746fe2011-09-19 18:26:07 +00001584 if (!RV) {
1585 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1586 << ")\n";
1587 }
David Greenee1b46912009-06-08 20:23:18 +00001588 assert(RV && "Template argument record not found??");
1589 ItemType = RV->getType();
1590 ++ArgN;
1591 }
1592 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001593 if (Result.back() == 0) return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001594
Chris Lattnerf4601652007-11-22 20:49:04 +00001595 while (Lex.getCode() == tgtok::comma) {
1596 Lex.Lex(); // Eat the comma
Bob Wilson21870412009-11-22 04:24:42 +00001597
David Greenee1b46912009-06-08 20:23:18 +00001598 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001599 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greene67acdf22009-06-29 19:59:52 +00001600 if (ArgN >= TArgs.size()) {
1601 TokError("too many template arguments");
David Greene05bce0b2011-07-29 22:43:06 +00001602 return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001603 }
David Greenee1b46912009-06-08 20:23:18 +00001604 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1605 assert(RV && "Template argument record not found??");
1606 ItemType = RV->getType();
1607 ++ArgN;
1608 }
1609 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001610 if (Result.back() == 0) return std::vector<Init*>();
Chris Lattnerf4601652007-11-22 20:49:04 +00001611 }
Bob Wilson21870412009-11-22 04:24:42 +00001612
Chris Lattnerf4601652007-11-22 20:49:04 +00001613 return Result;
1614}
1615
1616
Chris Lattnerf4601652007-11-22 20:49:04 +00001617/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1618/// empty string on error. This can happen in a number of different context's,
1619/// including within a def or in the template args for a def (which which case
1620/// CurRec will be non-null) and within the template args for a multiclass (in
1621/// which case CurRec will be null, but CurMultiClass will be set). This can
1622/// also happen within a def that is within a multiclass, which will set both
1623/// CurRec and CurMultiClass.
1624///
1625/// Declaration ::= FIELD? Type ID ('=' Value)?
1626///
David Greenee22b3212011-10-19 13:02:42 +00001627Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4601652007-11-22 20:49:04 +00001628 bool ParsingTemplateArgs) {
1629 // Read the field prefix if present.
1630 bool HasField = Lex.getCode() == tgtok::Field;
1631 if (HasField) Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001632
Chris Lattnerf4601652007-11-22 20:49:04 +00001633 RecTy *Type = ParseType();
David Greenee22b3212011-10-19 13:02:42 +00001634 if (Type == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001635
Chris Lattnerf4601652007-11-22 20:49:04 +00001636 if (Lex.getCode() != tgtok::Id) {
1637 TokError("Expected identifier in declaration");
David Greenee22b3212011-10-19 13:02:42 +00001638 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001639 }
Bob Wilson21870412009-11-22 04:24:42 +00001640
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001641 SMLoc IdLoc = Lex.getLoc();
David Greenee22b3212011-10-19 13:02:42 +00001642 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001643 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001644
Chris Lattnerf4601652007-11-22 20:49:04 +00001645 if (ParsingTemplateArgs) {
1646 if (CurRec) {
David Greenee22b3212011-10-19 13:02:42 +00001647 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4601652007-11-22 20:49:04 +00001648 } else {
1649 assert(CurMultiClass);
1650 }
1651 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +00001652 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1653 "::");
Chris Lattnerf4601652007-11-22 20:49:04 +00001654 }
Bob Wilson21870412009-11-22 04:24:42 +00001655
Chris Lattnerf4601652007-11-22 20:49:04 +00001656 // Add the value.
1657 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
David Greenee22b3212011-10-19 13:02:42 +00001658 return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001659
Chris Lattnerf4601652007-11-22 20:49:04 +00001660 // If a value is present, parse it.
1661 if (Lex.getCode() == tgtok::equal) {
1662 Lex.Lex();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001663 SMLoc ValLoc = Lex.getLoc();
David Greene05bce0b2011-07-29 22:43:06 +00001664 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001665 if (Val == 0 ||
1666 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
David Greenee22b3212011-10-19 13:02:42 +00001667 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001668 }
Bob Wilson21870412009-11-22 04:24:42 +00001669
Chris Lattnerf4601652007-11-22 20:49:04 +00001670 return DeclName;
1671}
1672
David Greenecebb4ee2012-02-22 16:09:41 +00001673/// ParseForeachDeclaration - Read a foreach declaration, returning
1674/// the name of the declared object or a NULL Init on error. Return
1675/// the name of the parsed initializer list through ForeachListName.
1676///
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001677/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1678/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1679/// ForeachDeclaration ::= ID '=' RangePiece
David Greenecebb4ee2012-02-22 16:09:41 +00001680///
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00001681VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenecebb4ee2012-02-22 16:09:41 +00001682 if (Lex.getCode() != tgtok::Id) {
1683 TokError("Expected identifier in foreach declaration");
1684 return 0;
1685 }
1686
1687 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1688 Lex.Lex();
1689
1690 // If a value is present, parse it.
1691 if (Lex.getCode() != tgtok::equal) {
1692 TokError("Expected '=' in foreach declaration");
1693 return 0;
1694 }
1695 Lex.Lex(); // Eat the '='
1696
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001697 RecTy *IterType = 0;
1698 std::vector<unsigned> Ranges;
David Greenecebb4ee2012-02-22 16:09:41 +00001699
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001700 switch (Lex.getCode()) {
1701 default: TokError("Unknown token when expecting a range list"); return 0;
1702 case tgtok::l_square: { // '[' ValueList ']'
1703 Init *List = ParseSimpleValue(0, 0, ParseForeachMode);
Sean Silva6cfc8062012-10-10 20:24:43 +00001704 ForeachListValue = dyn_cast<ListInit>(List);
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001705 if (ForeachListValue == 0) {
1706 TokError("Expected a Value list");
1707 return 0;
1708 }
1709 RecTy *ValueType = ForeachListValue->getType();
Sean Silva736ceac2012-10-05 03:31:58 +00001710 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001711 if (ListType == 0) {
1712 TokError("Value list is not of list type");
1713 return 0;
1714 }
1715 IterType = ListType->getElementType();
1716 break;
David Greenecebb4ee2012-02-22 16:09:41 +00001717 }
1718
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001719 case tgtok::IntVal: { // RangePiece.
1720 if (ParseRangePiece(Ranges))
1721 return 0;
1722 break;
David Greenecebb4ee2012-02-22 16:09:41 +00001723 }
1724
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001725 case tgtok::l_brace: { // '{' RangeList '}'
1726 Lex.Lex(); // eat the '{'
1727 Ranges = ParseRangeList();
1728 if (Lex.getCode() != tgtok::r_brace) {
1729 TokError("expected '}' at end of bit range list");
1730 return 0;
1731 }
1732 Lex.Lex();
1733 break;
1734 }
1735 }
David Greenecebb4ee2012-02-22 16:09:41 +00001736
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001737 if (!Ranges.empty()) {
1738 assert(!IterType && "Type already initialized?");
1739 IterType = IntRecTy::get();
1740 std::vector<Init*> Values;
1741 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
1742 Values.push_back(IntInit::get(Ranges[i]));
1743 ForeachListValue = ListInit::get(Values, IterType);
1744 }
1745
1746 if (!IterType)
1747 return 0;
1748
1749 return VarInit::get(DeclName, IterType);
David Greenecebb4ee2012-02-22 16:09:41 +00001750}
1751
Chris Lattnerf4601652007-11-22 20:49:04 +00001752/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1753/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1754/// template args for a def, which may or may not be in a multiclass. If null,
1755/// these are the template args for a multiclass.
1756///
1757/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson21870412009-11-22 04:24:42 +00001758///
Chris Lattnerf4601652007-11-22 20:49:04 +00001759bool TGParser::ParseTemplateArgList(Record *CurRec) {
1760 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1761 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +00001762
Chris Lattnerf4601652007-11-22 20:49:04 +00001763 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +00001764
Chris Lattnerf4601652007-11-22 20:49:04 +00001765 // Read the first declaration.
David Greenee22b3212011-10-19 13:02:42 +00001766 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1767 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001768 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001769
Chris Lattnerf4601652007-11-22 20:49:04 +00001770 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson21870412009-11-22 04:24:42 +00001771
Chris Lattnerf4601652007-11-22 20:49:04 +00001772 while (Lex.getCode() == tgtok::comma) {
1773 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001774
Chris Lattnerf4601652007-11-22 20:49:04 +00001775 // Read the following declarations.
1776 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
David Greenee22b3212011-10-19 13:02:42 +00001777 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001778 return true;
1779 TheRecToAddTo->addTemplateArg(TemplArg);
1780 }
Bob Wilson21870412009-11-22 04:24:42 +00001781
Chris Lattnerf4601652007-11-22 20:49:04 +00001782 if (Lex.getCode() != tgtok::greater)
1783 return TokError("expected '>' at end of template argument list");
1784 Lex.Lex(); // eat the '>'.
1785 return false;
1786}
1787
1788
1789/// ParseBodyItem - Parse a single item at within the body of a def or class.
1790///
1791/// BodyItem ::= Declaration ';'
1792/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1793bool TGParser::ParseBodyItem(Record *CurRec) {
1794 if (Lex.getCode() != tgtok::Let) {
David Greenee22b3212011-10-19 13:02:42 +00001795 if (ParseDeclaration(CurRec, false) == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001796 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001797
Chris Lattnerf4601652007-11-22 20:49:04 +00001798 if (Lex.getCode() != tgtok::semi)
1799 return TokError("expected ';' after declaration");
1800 Lex.Lex();
1801 return false;
1802 }
1803
1804 // LET ID OptionalRangeList '=' Value ';'
1805 if (Lex.Lex() != tgtok::Id)
1806 return TokError("expected field identifier after let");
Bob Wilson21870412009-11-22 04:24:42 +00001807
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001808 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001809 std::string FieldName = Lex.getCurStrVal();
1810 Lex.Lex(); // eat the field name.
Bob Wilson21870412009-11-22 04:24:42 +00001811
Chris Lattnerf4601652007-11-22 20:49:04 +00001812 std::vector<unsigned> BitList;
Bob Wilson21870412009-11-22 04:24:42 +00001813 if (ParseOptionalBitList(BitList))
Chris Lattnerf4601652007-11-22 20:49:04 +00001814 return true;
1815 std::reverse(BitList.begin(), BitList.end());
Bob Wilson21870412009-11-22 04:24:42 +00001816
Chris Lattnerf4601652007-11-22 20:49:04 +00001817 if (Lex.getCode() != tgtok::equal)
1818 return TokError("expected '=' in let expression");
1819 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001820
David Greenee1b46912009-06-08 20:23:18 +00001821 RecordVal *Field = CurRec->getValue(FieldName);
1822 if (Field == 0)
1823 return TokError("Value '" + FieldName + "' unknown!");
1824
1825 RecTy *Type = Field->getType();
Bob Wilson21870412009-11-22 04:24:42 +00001826
David Greene05bce0b2011-07-29 22:43:06 +00001827 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001828 if (Val == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001829
Chris Lattnerf4601652007-11-22 20:49:04 +00001830 if (Lex.getCode() != tgtok::semi)
1831 return TokError("expected ';' after let expression");
1832 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001833
Chris Lattnerf4601652007-11-22 20:49:04 +00001834 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1835}
1836
1837/// ParseBody - Read the body of a class or def. Return true on error, false on
1838/// success.
1839///
1840/// Body ::= ';'
1841/// Body ::= '{' BodyList '}'
1842/// BodyList BodyItem*
1843///
1844bool TGParser::ParseBody(Record *CurRec) {
1845 // If this is a null definition, just eat the semi and return.
1846 if (Lex.getCode() == tgtok::semi) {
1847 Lex.Lex();
1848 return false;
1849 }
Bob Wilson21870412009-11-22 04:24:42 +00001850
Chris Lattnerf4601652007-11-22 20:49:04 +00001851 if (Lex.getCode() != tgtok::l_brace)
1852 return TokError("Expected ';' or '{' to start body");
1853 // Eat the '{'.
1854 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001855
Chris Lattnerf4601652007-11-22 20:49:04 +00001856 while (Lex.getCode() != tgtok::r_brace)
1857 if (ParseBodyItem(CurRec))
1858 return true;
1859
1860 // Eat the '}'.
1861 Lex.Lex();
1862 return false;
1863}
1864
Sean Silva9cceede2013-01-09 04:49:14 +00001865/// \brief Apply the current let bindings to \a CurRec.
1866/// \returns true on error, false otherwise.
1867bool TGParser::ApplyLetStack(Record *CurRec) {
1868 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1869 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1870 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1871 LetStack[i][j].Bits, LetStack[i][j].Value))
1872 return true;
1873 return false;
1874}
1875
Chris Lattnerf4601652007-11-22 20:49:04 +00001876/// ParseObjectBody - Parse the body of a def or class. This consists of an
1877/// optional ClassList followed by a Body. CurRec is the current def or class
1878/// that is being parsed.
1879///
1880/// ObjectBody ::= BaseClassList Body
1881/// BaseClassList ::= /*empty*/
1882/// BaseClassList ::= ':' BaseClassListNE
1883/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1884///
1885bool TGParser::ParseObjectBody(Record *CurRec) {
1886 // If there is a baseclass list, read it.
1887 if (Lex.getCode() == tgtok::colon) {
1888 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001889
Chris Lattnerf4601652007-11-22 20:49:04 +00001890 // Read all of the subclasses.
1891 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1892 while (1) {
1893 // Check for error.
1894 if (SubClass.Rec == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001895
Chris Lattnerf4601652007-11-22 20:49:04 +00001896 // Add it.
1897 if (AddSubClass(CurRec, SubClass))
1898 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001899
Chris Lattnerf4601652007-11-22 20:49:04 +00001900 if (Lex.getCode() != tgtok::comma) break;
1901 Lex.Lex(); // eat ','.
1902 SubClass = ParseSubClassReference(CurRec, false);
1903 }
1904 }
1905
Sean Silva9cceede2013-01-09 04:49:14 +00001906 if (ApplyLetStack(CurRec))
1907 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001908
Chris Lattnerf4601652007-11-22 20:49:04 +00001909 return ParseBody(CurRec);
1910}
1911
Chris Lattnerf4601652007-11-22 20:49:04 +00001912/// ParseDef - Parse and return a top level or multiclass def, return the record
1913/// corresponding to it. This returns null on error.
1914///
1915/// DefInst ::= DEF ObjectName ObjectBody
1916///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001917bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001918 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001919 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson21870412009-11-22 04:24:42 +00001920 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4601652007-11-22 20:49:04 +00001921
1922 // Parse ObjectName and make a record for it.
Jordan Rosed1220092013-01-10 18:50:05 +00001923 Record *CurRec;
1924 Init *Name = ParseObjectName(CurMultiClass);
1925 if (Name)
1926 CurRec = new Record(Name, DefLoc, Records);
1927 else
1928 CurRec = new Record(GetNewAnonymousName(), DefLoc, Records,
1929 /*IsAnonymous=*/true);
Bob Wilson21870412009-11-22 04:24:42 +00001930
Jakob Stoklund Olesen72cba6c2012-05-24 22:17:36 +00001931 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001932 // Top-level def definition.
Bob Wilson21870412009-11-22 04:24:42 +00001933
Chris Lattnerf4601652007-11-22 20:49:04 +00001934 // Ensure redefinition doesn't happen.
David Greene1d501392012-01-28 00:03:24 +00001935 if (Records.getDef(CurRec->getNameInitAsString())) {
David Greene2c49fbb2011-10-19 13:03:45 +00001936 Error(DefLoc, "def '" + CurRec->getNameInitAsString()
1937 + "' already defined");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001938 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001939 }
1940 Records.addDef(CurRec);
Jakob Stoklund Olesen72cba6c2012-05-24 22:17:36 +00001941 } else if (CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001942 // Otherwise, a def inside a multiclass, add it to the multiclass.
1943 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene91919cd2011-10-19 13:03:51 +00001944 if (CurMultiClass->DefPrototypes[i]->getNameInit()
1945 == CurRec->getNameInit()) {
1946 Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
Chris Lattnerf4601652007-11-22 20:49:04 +00001947 "' already defined in this multiclass!");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001948 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001949 }
1950 CurMultiClass->DefPrototypes.push_back(CurRec);
1951 }
Bob Wilson21870412009-11-22 04:24:42 +00001952
Chris Lattnerf4601652007-11-22 20:49:04 +00001953 if (ParseObjectBody(CurRec))
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001954 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001955
Chris Lattnerf4601652007-11-22 20:49:04 +00001956 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
David Greene0d886402011-08-10 18:27:46 +00001957 // See Record::setName(). This resolve step will see any new name
1958 // for the def that might have been created when resolving
1959 // inheritance, values and arguments above.
Chris Lattnerf4601652007-11-22 20:49:04 +00001960 CurRec->resolveReferences();
Bob Wilson21870412009-11-22 04:24:42 +00001961
Chris Lattnerf4601652007-11-22 20:49:04 +00001962 // If ObjectBody has template arguments, it's an error.
1963 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001964
1965 if (CurMultiClass) {
1966 // Copy the template arguments for the multiclass into the def.
David Greenee22b3212011-10-19 13:02:42 +00001967 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001968 CurMultiClass->Rec.getTemplateArgs();
1969
1970 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1971 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1972 assert(RV && "Template arg doesn't exist?");
1973 CurRec->addValue(*RV);
1974 }
1975 }
1976
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00001977 if (ProcessForeachDefs(CurRec, DefLoc)) {
David Greenecebb4ee2012-02-22 16:09:41 +00001978 Error(DefLoc,
1979 "Could not process loops for def" + CurRec->getNameInitAsString());
1980 return true;
1981 }
1982
1983 return false;
1984}
1985
1986/// ParseForeach - Parse a for statement. Return the record corresponding
1987/// to it. This returns true on error.
1988///
1989/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
1990/// Foreach ::= FOREACH Declaration IN Object
1991///
1992bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
1993 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
1994 Lex.Lex(); // Eat the 'for' token.
1995
1996 // Make a temporary object to record items associated with the for
1997 // loop.
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00001998 ListInit *ListValue = 0;
1999 VarInit *IterName = ParseForeachDeclaration(ListValue);
David Greenecebb4ee2012-02-22 16:09:41 +00002000 if (IterName == 0)
2001 return TokError("expected declaration in for");
2002
2003 if (Lex.getCode() != tgtok::In)
2004 return TokError("Unknown tok");
2005 Lex.Lex(); // Eat the in
2006
2007 // Create a loop object and remember it.
2008 Loops.push_back(ForeachLoop(IterName, ListValue));
2009
2010 if (Lex.getCode() != tgtok::l_brace) {
2011 // FOREACH Declaration IN Object
2012 if (ParseObject(CurMultiClass))
2013 return true;
2014 }
2015 else {
2016 SMLoc BraceLoc = Lex.getLoc();
2017 // Otherwise, this is a group foreach.
2018 Lex.Lex(); // eat the '{'.
2019
2020 // Parse the object list.
2021 if (ParseObjectList(CurMultiClass))
2022 return true;
2023
2024 if (Lex.getCode() != tgtok::r_brace) {
2025 TokError("expected '}' at end of foreach command");
2026 return Error(BraceLoc, "to match this '{'");
2027 }
2028 Lex.Lex(); // Eat the }
2029 }
2030
2031 // We've processed everything in this loop.
2032 Loops.pop_back();
2033
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002034 return false;
Chris Lattnerf4601652007-11-22 20:49:04 +00002035}
2036
Chris Lattnerf4601652007-11-22 20:49:04 +00002037/// ParseClass - Parse a tblgen class definition.
2038///
2039/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2040///
2041bool TGParser::ParseClass() {
2042 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2043 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002044
Chris Lattnerf4601652007-11-22 20:49:04 +00002045 if (Lex.getCode() != tgtok::Id)
2046 return TokError("expected class name after 'class' keyword");
Bob Wilson21870412009-11-22 04:24:42 +00002047
Chris Lattnerf4601652007-11-22 20:49:04 +00002048 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2049 if (CurRec) {
2050 // If the body was previously defined, this is an error.
David Greenee3385652011-10-19 13:04:13 +00002051 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4601652007-11-22 20:49:04 +00002052 !CurRec->getSuperClasses().empty() ||
2053 !CurRec->getTemplateArgs().empty())
David Greene69a23942011-10-19 13:03:58 +00002054 return TokError("Class '" + CurRec->getNameInitAsString()
2055 + "' already defined");
Chris Lattnerf4601652007-11-22 20:49:04 +00002056 } else {
2057 // If this is the first reference to this class, create and add it.
Chris Lattner9c6b60e2010-12-15 04:48:22 +00002058 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00002059 Records.addClass(CurRec);
2060 }
2061 Lex.Lex(); // eat the name.
Bob Wilson21870412009-11-22 04:24:42 +00002062
Chris Lattnerf4601652007-11-22 20:49:04 +00002063 // If there are template args, parse them.
2064 if (Lex.getCode() == tgtok::less)
2065 if (ParseTemplateArgList(CurRec))
2066 return true;
2067
2068 // Finally, parse the object body.
2069 return ParseObjectBody(CurRec);
2070}
2071
2072/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2073/// of LetRecords.
2074///
2075/// LetList ::= LetItem (',' LetItem)*
2076/// LetItem ::= ID OptionalRangeList '=' Value
2077///
2078std::vector<LetRecord> TGParser::ParseLetList() {
2079 std::vector<LetRecord> Result;
Bob Wilson21870412009-11-22 04:24:42 +00002080
Chris Lattnerf4601652007-11-22 20:49:04 +00002081 while (1) {
2082 if (Lex.getCode() != tgtok::Id) {
2083 TokError("expected identifier in let definition");
2084 return std::vector<LetRecord>();
2085 }
2086 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002087 SMLoc NameLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +00002088 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4601652007-11-22 20:49:04 +00002089
2090 // Check for an optional RangeList.
2091 std::vector<unsigned> Bits;
Bob Wilson21870412009-11-22 04:24:42 +00002092 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4601652007-11-22 20:49:04 +00002093 return std::vector<LetRecord>();
2094 std::reverse(Bits.begin(), Bits.end());
Bob Wilson21870412009-11-22 04:24:42 +00002095
Chris Lattnerf4601652007-11-22 20:49:04 +00002096 if (Lex.getCode() != tgtok::equal) {
2097 TokError("expected '=' in let expression");
2098 return std::vector<LetRecord>();
2099 }
2100 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00002101
David Greene05bce0b2011-07-29 22:43:06 +00002102 Init *Val = ParseValue(0);
Chris Lattnerf4601652007-11-22 20:49:04 +00002103 if (Val == 0) return std::vector<LetRecord>();
Bob Wilson21870412009-11-22 04:24:42 +00002104
Chris Lattnerf4601652007-11-22 20:49:04 +00002105 // Now that we have everything, add the record.
2106 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson21870412009-11-22 04:24:42 +00002107
Chris Lattnerf4601652007-11-22 20:49:04 +00002108 if (Lex.getCode() != tgtok::comma)
2109 return Result;
Bob Wilson21870412009-11-22 04:24:42 +00002110 Lex.Lex(); // eat the comma.
Chris Lattnerf4601652007-11-22 20:49:04 +00002111 }
2112}
2113
2114/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002115/// different related productions. This works inside multiclasses too.
Chris Lattnerf4601652007-11-22 20:49:04 +00002116///
2117/// Object ::= LET LetList IN '{' ObjectList '}'
2118/// Object ::= LET LetList IN Object
2119///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002120bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002121 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2122 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002123
Chris Lattnerf4601652007-11-22 20:49:04 +00002124 // Add this entry to the let stack.
2125 std::vector<LetRecord> LetInfo = ParseLetList();
2126 if (LetInfo.empty()) return true;
2127 LetStack.push_back(LetInfo);
2128
2129 if (Lex.getCode() != tgtok::In)
2130 return TokError("expected 'in' at end of top-level 'let'");
2131 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002132
Chris Lattnerf4601652007-11-22 20:49:04 +00002133 // If this is a scalar let, just handle it now
2134 if (Lex.getCode() != tgtok::l_brace) {
2135 // LET LetList IN Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002136 if (ParseObject(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00002137 return true;
2138 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002139 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002140 // Otherwise, this is a group let.
2141 Lex.Lex(); // eat the '{'.
Bob Wilson21870412009-11-22 04:24:42 +00002142
Chris Lattnerf4601652007-11-22 20:49:04 +00002143 // Parse the object list.
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002144 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00002145 return true;
Bob Wilson21870412009-11-22 04:24:42 +00002146
Chris Lattnerf4601652007-11-22 20:49:04 +00002147 if (Lex.getCode() != tgtok::r_brace) {
2148 TokError("expected '}' at end of top level let command");
2149 return Error(BraceLoc, "to match this '{'");
2150 }
2151 Lex.Lex();
2152 }
Bob Wilson21870412009-11-22 04:24:42 +00002153
Chris Lattnerf4601652007-11-22 20:49:04 +00002154 // Outside this let scope, this let block is not active.
2155 LetStack.pop_back();
2156 return false;
2157}
2158
Chris Lattnerf4601652007-11-22 20:49:04 +00002159/// ParseMultiClass - Parse a multiclass definition.
2160///
Bob Wilson32558652009-04-28 19:41:44 +00002161/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silva9302dcc2013-01-09 02:11:55 +00002162/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2163/// MultiClassObject ::= DefInst
2164/// MultiClassObject ::= MultiClassInst
2165/// MultiClassObject ::= DefMInst
2166/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2167/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4601652007-11-22 20:49:04 +00002168///
2169bool TGParser::ParseMultiClass() {
2170 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2171 Lex.Lex(); // Eat the multiclass token.
2172
2173 if (Lex.getCode() != tgtok::Id)
2174 return TokError("expected identifier after multiclass for name");
2175 std::string Name = Lex.getCurStrVal();
Bob Wilson21870412009-11-22 04:24:42 +00002176
Chris Lattnerf4601652007-11-22 20:49:04 +00002177 if (MultiClasses.count(Name))
2178 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson21870412009-11-22 04:24:42 +00002179
Chris Lattner67db8832010-12-13 00:23:57 +00002180 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
2181 Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00002182 Lex.Lex(); // Eat the identifier.
Bob Wilson21870412009-11-22 04:24:42 +00002183
Chris Lattnerf4601652007-11-22 20:49:04 +00002184 // If there are template args, parse them.
2185 if (Lex.getCode() == tgtok::less)
2186 if (ParseTemplateArgList(0))
2187 return true;
2188
David Greened34a73b2009-04-24 16:55:41 +00002189 bool inherits = false;
2190
David Greenede444af2009-04-22 16:42:54 +00002191 // If there are submulticlasses, parse them.
2192 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00002193 inherits = true;
2194
David Greenede444af2009-04-22 16:42:54 +00002195 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00002196
David Greenede444af2009-04-22 16:42:54 +00002197 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00002198 SubMultiClassReference SubMultiClass =
2199 ParseSubMultiClassReference(CurMultiClass);
David Greenede444af2009-04-22 16:42:54 +00002200 while (1) {
2201 // Check for error.
2202 if (SubMultiClass.MC == 0) return true;
Bob Wilson32558652009-04-28 19:41:44 +00002203
David Greenede444af2009-04-22 16:42:54 +00002204 // Add it.
2205 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2206 return true;
Bob Wilson32558652009-04-28 19:41:44 +00002207
David Greenede444af2009-04-22 16:42:54 +00002208 if (Lex.getCode() != tgtok::comma) break;
2209 Lex.Lex(); // eat ','.
2210 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2211 }
2212 }
2213
David Greened34a73b2009-04-24 16:55:41 +00002214 if (Lex.getCode() != tgtok::l_brace) {
2215 if (!inherits)
2216 return TokError("expected '{' in multiclass definition");
Bob Wilson21870412009-11-22 04:24:42 +00002217 else if (Lex.getCode() != tgtok::semi)
2218 return TokError("expected ';' in multiclass definition");
David Greened34a73b2009-04-24 16:55:41 +00002219 else
Bob Wilson21870412009-11-22 04:24:42 +00002220 Lex.Lex(); // eat the ';'.
2221 } else {
David Greened34a73b2009-04-24 16:55:41 +00002222 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2223 return TokError("multiclass must contain at least one def");
Bob Wilson21870412009-11-22 04:24:42 +00002224
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002225 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002226 switch (Lex.getCode()) {
2227 default:
David Greenea1b1b792011-10-07 18:25:05 +00002228 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002229 case tgtok::Let:
2230 case tgtok::Def:
2231 case tgtok::Defm:
David Greenecebb4ee2012-02-22 16:09:41 +00002232 case tgtok::Foreach:
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002233 if (ParseObject(CurMultiClass))
2234 return true;
2235 break;
2236 }
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002237 }
David Greened34a73b2009-04-24 16:55:41 +00002238 Lex.Lex(); // eat the '}'.
2239 }
Bob Wilson21870412009-11-22 04:24:42 +00002240
Chris Lattnerf4601652007-11-22 20:49:04 +00002241 CurMultiClass = 0;
2242 return false;
2243}
2244
David Greenee499a2d2011-10-05 22:42:07 +00002245Record *TGParser::
2246InstantiateMulticlassDef(MultiClass &MC,
2247 Record *DefProto,
David Greene7be867e2011-10-19 13:04:31 +00002248 Init *DefmPrefix,
David Greenee499a2d2011-10-05 22:42:07 +00002249 SMLoc DefmPrefixLoc) {
David Greene7be867e2011-10-19 13:04:31 +00002250 // We need to preserve DefProto so it can be reused for later
2251 // instantiations, so create a new Record to inherit from it.
2252
David Greenee499a2d2011-10-05 22:42:07 +00002253 // Add in the defm name. If the defm prefix is empty, give each
2254 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2255 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2256 // as a prefix.
David Greenee499a2d2011-10-05 22:42:07 +00002257
Jordan Rosed1220092013-01-10 18:50:05 +00002258 bool IsAnonymous = false;
2259 if (DefmPrefix == 0) {
David Greene7be867e2011-10-19 13:04:31 +00002260 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Rosed1220092013-01-10 18:50:05 +00002261 IsAnonymous = true;
2262 }
David Greene7be867e2011-10-19 13:04:31 +00002263
2264 Init *DefName = DefProto->getNameInit();
2265
Sean Silva6cfc8062012-10-10 20:24:43 +00002266 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene7be867e2011-10-19 13:04:31 +00002267
David Greened3d1cad2011-10-19 13:04:43 +00002268 if (DefNameString != 0) {
2269 // We have a fully expanded string so there are no operators to
2270 // resolve. We should concatenate the given prefix and name.
David Greene7be867e2011-10-19 13:04:31 +00002271 DefName =
2272 BinOpInit::get(BinOpInit::STRCONCAT,
2273 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2274 StringRecTy::get())->Fold(DefProto, &MC),
2275 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2276 }
David Greene7be867e2011-10-19 13:04:31 +00002277
Jakob Stoklund Olesen376a8a72012-08-22 23:33:58 +00002278 // Make a trail of SMLocs from the multiclass instantiations.
2279 SmallVector<SMLoc, 4> Locs(1, DefmPrefixLoc);
2280 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Jordan Rosed1220092013-01-10 18:50:05 +00002281 Record *CurRec = new Record(DefName, Locs, Records, IsAnonymous);
David Greenee499a2d2011-10-05 22:42:07 +00002282
2283 SubClassReference Ref;
2284 Ref.RefLoc = DefmPrefixLoc;
2285 Ref.Rec = DefProto;
2286 AddSubClass(CurRec, Ref);
2287
Jim Grosbachcfbda4a2012-08-02 18:46:42 +00002288 // Set the value for NAME. We don't resolve references to it 'til later,
2289 // though, so that uses in nested multiclass names don't get
2290 // confused.
2291 if (SetValue(CurRec, Ref.RefLoc, "NAME", std::vector<unsigned>(),
2292 DefmPrefix)) {
2293 Error(DefmPrefixLoc, "Could not resolve "
2294 + CurRec->getNameInitAsString() + ":NAME to '"
2295 + DefmPrefix->getAsUnquotedString() + "'");
2296 return 0;
2297 }
David Greenee5b252f2011-10-19 13:04:35 +00002298
Jim Grosbachcfbda4a2012-08-02 18:46:42 +00002299 // If the DefNameString didn't resolve, we probably have a reference to
2300 // NAME and need to replace it. We need to do at least this much greedily,
2301 // otherwise nested multiclasses will end up with incorrect NAME expansions.
2302 if (DefNameString == 0) {
David Greenee5b252f2011-10-19 13:04:35 +00002303 RecordVal *DefNameRV = CurRec->getValue("NAME");
2304 CurRec->resolveReferencesTo(DefNameRV);
2305 }
2306
2307 if (!CurMultiClass) {
Jim Grosbachcfbda4a2012-08-02 18:46:42 +00002308 // Now that we're at the top level, resolve all NAME references
2309 // in the resultant defs that weren't in the def names themselves.
2310 RecordVal *DefNameRV = CurRec->getValue("NAME");
2311 CurRec->resolveReferencesTo(DefNameRV);
2312
2313 // Now that NAME references are resolved and we're at the top level of
2314 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greenee5b252f2011-10-19 13:04:35 +00002315 // currently in a multiclass, it means this defm appears inside a
2316 // multiclass and its name won't be fully resolvable until we see
2317 // the top-level defm. Therefore, we don't add this to the
2318 // RecordKeeper at this point. If we did we could get duplicate
2319 // defs as more than one probably refers to NAME or some other
2320 // common internal placeholder.
2321
2322 // Ensure redefinition doesn't happen.
2323 if (Records.getDef(CurRec->getNameInitAsString())) {
2324 Error(DefmPrefixLoc, "def '" + CurRec->getNameInitAsString() +
2325 "' already defined, instantiating defm with subdef '" +
2326 DefProto->getNameInitAsString() + "'");
2327 return 0;
2328 }
2329
2330 Records.addDef(CurRec);
2331 }
2332
David Greenee499a2d2011-10-05 22:42:07 +00002333 return CurRec;
2334}
2335
2336bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2337 Record *CurRec,
2338 SMLoc DefmPrefixLoc,
2339 SMLoc SubClassLoc,
David Greenee22b3212011-10-19 13:02:42 +00002340 const std::vector<Init *> &TArgs,
David Greenee499a2d2011-10-05 22:42:07 +00002341 std::vector<Init *> &TemplateVals,
2342 bool DeleteArgs) {
2343 // Loop over all of the template arguments, setting them to the specified
2344 // value or leaving them as the default if necessary.
2345 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2346 // Check if a value is specified for this temp-arg.
2347 if (i < TemplateVals.size()) {
2348 // Set it now.
2349 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2350 TemplateVals[i]))
2351 return true;
2352
2353 // Resolve it next.
2354 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2355
2356 if (DeleteArgs)
2357 // Now remove it.
2358 CurRec->removeValue(TArgs[i]);
2359
2360 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2361 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenee22b3212011-10-19 13:02:42 +00002362 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2363 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2364 + "'");
David Greenee499a2d2011-10-05 22:42:07 +00002365 }
2366 }
2367 return false;
2368}
2369
2370bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2371 Record *CurRec,
2372 Record *DefProto,
2373 SMLoc DefmPrefixLoc) {
2374 // If the mdef is inside a 'let' expression, add to each def.
Sean Silva9cceede2013-01-09 04:49:14 +00002375 if (ApplyLetStack(CurRec))
2376 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenee499a2d2011-10-05 22:42:07 +00002377
David Greenee499a2d2011-10-05 22:42:07 +00002378 // Don't create a top level definition for defm inside multiclasses,
2379 // instead, only update the prototypes and bind the template args
2380 // with the new created definition.
Sean Silva699b8702013-01-09 05:28:12 +00002381 if (!CurMultiClass)
2382 return false;
2383 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2384 i != e; ++i)
2385 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2386 == CurRec->getNameInit())
2387 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2388 "' already defined in this multiclass!");
2389 CurMultiClass->DefPrototypes.push_back(CurRec);
David Greenee499a2d2011-10-05 22:42:07 +00002390
Sean Silva699b8702013-01-09 05:28:12 +00002391 // Copy the template arguments for the multiclass into the new def.
2392 const std::vector<Init *> &TA =
2393 CurMultiClass->Rec.getTemplateArgs();
David Greenee499a2d2011-10-05 22:42:07 +00002394
Sean Silva699b8702013-01-09 05:28:12 +00002395 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2396 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2397 assert(RV && "Template arg doesn't exist?");
2398 CurRec->addValue(*RV);
David Greenee499a2d2011-10-05 22:42:07 +00002399 }
2400
2401 return false;
2402}
2403
Chris Lattnerf4601652007-11-22 20:49:04 +00002404/// ParseDefm - Parse the instantiation of a multiclass.
2405///
2406/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2407///
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002408bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002409 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Bob Wilson21870412009-11-22 04:24:42 +00002410
David Greenea9e07dd2011-10-19 13:04:29 +00002411 Init *DefmPrefix = 0;
David Greenea9e07dd2011-10-19 13:04:29 +00002412
Craig Topper6a59f5a2013-01-07 05:09:33 +00002413 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greenea9e07dd2011-10-19 13:04:29 +00002414 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002415 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +00002416
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002417 SMLoc DefmPrefixLoc = Lex.getLoc();
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002418 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4601652007-11-22 20:49:04 +00002419 return TokError("expected ':' after defm identifier");
Bob Wilson21870412009-11-22 04:24:42 +00002420
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002421 // Keep track of the new generated record definitions.
2422 std::vector<Record*> NewRecDefs;
2423
2424 // This record also inherits from a regular class (non-multiclass)?
2425 bool InheritFromClass = false;
2426
Chris Lattnerf4601652007-11-22 20:49:04 +00002427 // eat the colon.
2428 Lex.Lex();
2429
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002430 SMLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002431 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene56546132009-04-22 22:17:51 +00002432
2433 while (1) {
2434 if (Ref.Rec == 0) return true;
2435
2436 // To instantiate a multiclass, we need to first get the multiclass, then
2437 // instantiate each def contained in the multiclass with the SubClassRef
2438 // template parameters.
2439 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2440 assert(MC && "Didn't lookup multiclass correctly?");
David Greene05bce0b2011-07-29 22:43:06 +00002441 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greene56546132009-04-22 22:17:51 +00002442
2443 // Verify that the correct number of template arguments were specified.
David Greenee22b3212011-10-19 13:02:42 +00002444 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greene56546132009-04-22 22:17:51 +00002445 if (TArgs.size() < TemplateVals.size())
2446 return Error(SubClassLoc,
2447 "more template args specified than multiclass expects");
2448
2449 // Loop over all the def's in the multiclass, instantiating each one.
2450 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2451 Record *DefProto = MC->DefPrototypes[i];
2452
David Greene7be867e2011-10-19 13:04:31 +00002453 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix, DefmPrefixLoc);
Jim Grosbach94f2dc92011-12-02 18:33:03 +00002454 if (!CurRec)
2455 return true;
David Greene065f2592009-05-05 16:28:25 +00002456
David Greenee499a2d2011-10-05 22:42:07 +00002457 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmPrefixLoc, SubClassLoc,
2458 TArgs, TemplateVals, true/*Delete args*/))
2459 return Error(SubClassLoc, "could not instantiate def");
David Greene56546132009-04-22 22:17:51 +00002460
David Greenee499a2d2011-10-05 22:42:07 +00002461 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmPrefixLoc))
2462 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002463
2464 NewRecDefs.push_back(CurRec);
David Greene56546132009-04-22 22:17:51 +00002465 }
2466
David Greenee499a2d2011-10-05 22:42:07 +00002467
David Greene56546132009-04-22 22:17:51 +00002468 if (Lex.getCode() != tgtok::comma) break;
2469 Lex.Lex(); // eat ','.
2470
2471 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002472
2473 // A defm can inherit from regular classes (non-multiclass) as
2474 // long as they come in the end of the inheritance list.
2475 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0);
2476
2477 if (InheritFromClass)
2478 break;
2479
David Greene56546132009-04-22 22:17:51 +00002480 Ref = ParseSubClassReference(0, true);
2481 }
2482
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002483 if (InheritFromClass) {
2484 // Process all the classes to inherit as if they were part of a
2485 // regular 'def' and inherit all record values.
2486 SubClassReference SubClass = ParseSubClassReference(0, false);
2487 while (1) {
2488 // Check for error.
2489 if (SubClass.Rec == 0) return true;
2490
2491 // Get the expanded definition prototypes and teach them about
2492 // the record values the current class to inherit has
2493 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2494 Record *CurRec = NewRecDefs[i];
2495
2496 // Add it.
2497 if (AddSubClass(CurRec, SubClass))
2498 return true;
2499
Sean Silva9cceede2013-01-09 04:49:14 +00002500 if (ApplyLetStack(CurRec))
2501 return true;
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002502 }
2503
2504 if (Lex.getCode() != tgtok::comma) break;
2505 Lex.Lex(); // eat ','.
2506 SubClass = ParseSubClassReference(0, false);
2507 }
2508 }
2509
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002510 if (!CurMultiClass)
2511 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene0d886402011-08-10 18:27:46 +00002512 // See Record::setName(). This resolve step will see any new
2513 // name for the def that might have been created when resolving
2514 // inheritance, values and arguments above.
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002515 NewRecDefs[i]->resolveReferences();
2516
Chris Lattnerf4601652007-11-22 20:49:04 +00002517 if (Lex.getCode() != tgtok::semi)
2518 return TokError("expected ';' at end of defm");
2519 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002520
Chris Lattnerf4601652007-11-22 20:49:04 +00002521 return false;
2522}
2523
2524/// ParseObject
2525/// Object ::= ClassInst
2526/// Object ::= DefInst
2527/// Object ::= MultiClassInst
2528/// Object ::= DefMInst
2529/// Object ::= LETCommand '{' ObjectList '}'
2530/// Object ::= LETCommand Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002531bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002532 switch (Lex.getCode()) {
Chris Lattnerd6d9dd92010-10-31 19:27:15 +00002533 default:
2534 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002535 case tgtok::Let: return ParseTopLevelLet(MC);
2536 case tgtok::Def: return ParseDef(MC);
David Greenecebb4ee2012-02-22 16:09:41 +00002537 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002538 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4601652007-11-22 20:49:04 +00002539 case tgtok::Class: return ParseClass();
2540 case tgtok::MultiClass: return ParseMultiClass();
2541 }
2542}
2543
2544/// ParseObjectList
2545/// ObjectList :== Object*
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002546bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002547 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002548 if (ParseObject(MC))
Chris Lattnerf4601652007-11-22 20:49:04 +00002549 return true;
2550 }
2551 return false;
2552}
2553
Chris Lattnerf4601652007-11-22 20:49:04 +00002554bool TGParser::ParseFile() {
2555 Lex.Lex(); // Prime the lexer.
2556 if (ParseObjectList()) return true;
Bob Wilson21870412009-11-22 04:24:42 +00002557
Chris Lattnerf4601652007-11-22 20:49:04 +00002558 // If we have unread input at the end of the file, report it.
2559 if (Lex.getCode() == tgtok::Eof)
2560 return false;
Bob Wilson21870412009-11-22 04:24:42 +00002561
Chris Lattnerf4601652007-11-22 20:49:04 +00002562 return TokError("Unexpected input at top level");
2563}
2564