blob: 86ad2a6e3c09c6899d0d534baa5c72654d8d3b6e [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 {
Jordan Roseb50df4a2013-01-10 18:50:11 +000029 SMRange RefRange;
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 {
Jordan Roseb50df4a2013-01-10 18:50:11 +000038 SMRange RefRange;
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)
Jordan Roseb50df4a2013-01-10 18:50:11 +0000153 if (AddValue(CurRec, SubClass.RefRange.Start, Vals[i]))
Chris Lattnerf4601652007-11-22 20:49:04 +0000154 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())
Jordan Roseb50df4a2013-01-10 18:50:11 +0000160 return Error(SubClass.RefRange.Start,
161 "More template args specified than expected");
Bob Wilson21870412009-11-22 04:24:42 +0000162
Chris Lattnerf4601652007-11-22 20:49:04 +0000163 // Loop over all of the template arguments, setting them to the specified
164 // value or leaving them as the default if necessary.
165 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
166 if (i < SubClass.TemplateArgs.size()) {
167 // If a value is specified for this template arg, set it now.
Jordan Roseb50df4a2013-01-10 18:50:11 +0000168 if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
169 std::vector<unsigned>(), SubClass.TemplateArgs[i]))
Chris Lattnerf4601652007-11-22 20:49:04 +0000170 return true;
Bob Wilson21870412009-11-22 04:24:42 +0000171
Chris Lattnerf4601652007-11-22 20:49:04 +0000172 // Resolve it next.
173 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson21870412009-11-22 04:24:42 +0000174
Chris Lattnerf4601652007-11-22 20:49:04 +0000175 // Now remove it.
176 CurRec->removeValue(TArgs[i]);
177
178 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Jordan Roseb50df4a2013-01-10 18:50:11 +0000179 return Error(SubClass.RefRange.Start,
180 "Value not specified for template argument #"
David Greenee22b3212011-10-19 13:02:42 +0000181 + utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
182 + ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4601652007-11-22 20:49:04 +0000183 }
184 }
185
186 // Since everything went well, we can now set the "superclass" list for the
187 // current record.
188 const std::vector<Record*> &SCs = SC->getSuperClasses();
Jordan Roseb50df4a2013-01-10 18:50:11 +0000189 ArrayRef<SMRange> SCRanges = SC->getSuperClassRanges();
Chris Lattnerf4601652007-11-22 20:49:04 +0000190 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
191 if (CurRec->isSubClassOf(SCs[i]))
Jordan Roseb50df4a2013-01-10 18:50:11 +0000192 return Error(SubClass.RefRange.Start,
Chris Lattnerf4601652007-11-22 20:49:04 +0000193 "Already subclass of '" + SCs[i]->getName() + "'!\n");
Jordan Roseb50df4a2013-01-10 18:50:11 +0000194 CurRec->addSuperClass(SCs[i], SCRanges[i]);
Chris Lattnerf4601652007-11-22 20:49:04 +0000195 }
Bob Wilson21870412009-11-22 04:24:42 +0000196
Chris Lattnerf4601652007-11-22 20:49:04 +0000197 if (CurRec->isSubClassOf(SC))
Jordan Roseb50df4a2013-01-10 18:50:11 +0000198 return Error(SubClass.RefRange.Start,
Chris Lattnerf4601652007-11-22 20:49:04 +0000199 "Already subclass of '" + SC->getName() + "'!\n");
Jordan Roseb50df4a2013-01-10 18:50:11 +0000200 CurRec->addSuperClass(SC, SubClass.RefRange);
Chris Lattnerf4601652007-11-22 20:49:04 +0000201 return false;
202}
203
David Greenede444af2009-04-22 16:42:54 +0000204/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilson440548d2009-04-30 18:26:19 +0000205/// CurMC, resolving its template args as SubMultiClass's
David Greenede444af2009-04-22 16:42:54 +0000206/// template arguments.
Bob Wilson440548d2009-04-30 18:26:19 +0000207bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson1d512df2009-04-30 17:46:20 +0000208 SubMultiClassReference &SubMultiClass) {
David Greenede444af2009-04-22 16:42:54 +0000209 MultiClass *SMC = SubMultiClass.MC;
Bob Wilson440548d2009-04-30 18:26:19 +0000210 Record *CurRec = &CurMC->Rec;
David Greenede444af2009-04-22 16:42:54 +0000211
Bob Wilson440548d2009-04-30 18:26:19 +0000212 const std::vector<RecordVal> &MCVals = CurRec->getValues();
David Greenede444af2009-04-22 16:42:54 +0000213
214 // Add all of the values in the subclass into the current class.
215 const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues();
216 for (unsigned i = 0, e = SMCVals.size(); i != e; ++i)
Jordan Roseb50df4a2013-01-10 18:50:11 +0000217 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVals[i]))
David Greenede444af2009-04-22 16:42:54 +0000218 return true;
219
Bob Wilson440548d2009-04-30 18:26:19 +0000220 int newDefStart = CurMC->DefPrototypes.size();
David Greened34a73b2009-04-24 16:55:41 +0000221
David Greenede444af2009-04-22 16:42:54 +0000222 // Add all of the defs in the subclass into the current multiclass.
223 for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(),
224 iend = SMC->DefPrototypes.end();
225 i != iend;
226 ++i) {
227 // Clone the def and add it to the current multiclass
228 Record *NewDef = new Record(**i);
229
230 // Add all of the values in the superclass into the current def.
231 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
Jordan Roseb50df4a2013-01-10 18:50:11 +0000232 if (AddValue(NewDef, SubMultiClass.RefRange.Start, MCVals[i]))
David Greenede444af2009-04-22 16:42:54 +0000233 return true;
234
Bob Wilson440548d2009-04-30 18:26:19 +0000235 CurMC->DefPrototypes.push_back(NewDef);
David Greenede444af2009-04-22 16:42:54 +0000236 }
Bob Wilson32558652009-04-28 19:41:44 +0000237
David Greenee22b3212011-10-19 13:02:42 +0000238 const std::vector<Init *> &SMCTArgs = SMC->Rec.getTemplateArgs();
David Greenede444af2009-04-22 16:42:54 +0000239
David Greened34a73b2009-04-24 16:55:41 +0000240 // Ensure that an appropriate number of template arguments are
241 // specified.
David Greenede444af2009-04-22 16:42:54 +0000242 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Roseb50df4a2013-01-10 18:50:11 +0000243 return Error(SubMultiClass.RefRange.Start,
David Greened34a73b2009-04-24 16:55:41 +0000244 "More template args specified than expected");
Bob Wilson32558652009-04-28 19:41:44 +0000245
David Greenede444af2009-04-22 16:42:54 +0000246 // Loop over all of the template arguments, setting them to the specified
247 // value or leaving them as the default if necessary.
248 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
249 if (i < SubMultiClass.TemplateArgs.size()) {
David Greened34a73b2009-04-24 16:55:41 +0000250 // If a value is specified for this template arg, set it in the
251 // superclass now.
Jordan Roseb50df4a2013-01-10 18:50:11 +0000252 if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000253 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000254 SubMultiClass.TemplateArgs[i]))
255 return true;
256
257 // Resolve it next.
258 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson32558652009-04-28 19:41:44 +0000259
David Greenede444af2009-04-22 16:42:54 +0000260 // Now remove it.
261 CurRec->removeValue(SMCTArgs[i]);
262
David Greened34a73b2009-04-24 16:55:41 +0000263 // If a value is specified for this template arg, set it in the
264 // new defs now.
265 for (MultiClass::RecordVector::iterator j =
Bob Wilson440548d2009-04-30 18:26:19 +0000266 CurMC->DefPrototypes.begin() + newDefStart,
267 jend = CurMC->DefPrototypes.end();
David Greenede444af2009-04-22 16:42:54 +0000268 j != jend;
269 ++j) {
270 Record *Def = *j;
271
Jordan Roseb50df4a2013-01-10 18:50:11 +0000272 if (SetValue(Def, SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000273 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000274 SubMultiClass.TemplateArgs[i]))
275 return true;
276
277 // Resolve it next.
278 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
279
280 // Now remove it
281 Def->removeValue(SMCTArgs[i]);
282 }
283 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Roseb50df4a2013-01-10 18:50:11 +0000284 return Error(SubMultiClass.RefRange.Start,
David Greened34a73b2009-04-24 16:55:41 +0000285 "Value not specified for template argument #"
David Greenee22b3212011-10-19 13:02:42 +0000286 + utostr(i) + " (" + SMCTArgs[i]->getAsUnquotedString()
287 + ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greenede444af2009-04-22 16:42:54 +0000288 }
289 }
290
291 return false;
292}
293
David Greenecebb4ee2012-02-22 16:09:41 +0000294/// ProcessForeachDefs - Given a record, apply all of the variable
295/// values in all surrounding foreach loops, creating new records for
296/// each combination of values.
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000297bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
298 if (Loops.empty())
299 return false;
300
David Greenecebb4ee2012-02-22 16:09:41 +0000301 // We want to instantiate a new copy of CurRec for each combination
302 // of nested loop iterator values. We don't want top instantiate
303 // any copies until we have values for each loop iterator.
304 IterSet IterVals;
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000305 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenecebb4ee2012-02-22 16:09:41 +0000306}
307
308/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
309/// apply each of the variable values in this loop and then process
310/// subloops.
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000311bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
312 // Recursively build a tuple of iterator values.
313 if (IterVals.size() != Loops.size()) {
314 assert(IterVals.size() < Loops.size());
315 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silva6cfc8062012-10-10 20:24:43 +0000316 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000317 if (List == 0) {
318 Error(Loc, "Loop list is not a list");
319 return true;
320 }
David Greenecebb4ee2012-02-22 16:09:41 +0000321
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000322 // Process each value.
323 for (int64_t i = 0; i < List->getSize(); ++i) {
324 Init *ItemVal = List->resolveListElementReference(*CurRec, 0, i);
325 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
326 if (ProcessForeachDefs(CurRec, Loc, IterVals))
327 return true;
328 IterVals.pop_back();
329 }
330 return false;
331 }
332
333 // This is the bottom of the recursion. We have all of the iterator values
334 // for this point in the iteration space. Instantiate a new record to
335 // reflect this combination of values.
336 Record *IterRec = new Record(*CurRec);
337
338 // Set the iterator values now.
339 for (unsigned i = 0, e = IterVals.size(); i != e; ++i) {
340 VarInit *IterVar = IterVals[i].IterVar;
Sean Silva6cfc8062012-10-10 20:24:43 +0000341 TypedInit *IVal = dyn_cast<TypedInit>(IterVals[i].IterValue);
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000342 if (IVal == 0) {
343 Error(Loc, "foreach iterator value is untyped");
344 return true;
345 }
346
347 IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false));
348
349 if (SetValue(IterRec, Loc, IterVar->getName(),
350 std::vector<unsigned>(), IVal)) {
351 Error(Loc, "when instantiating this def");
352 return true;
353 }
354
355 // Resolve it next.
356 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName()));
357
358 // Remove it.
359 IterRec->removeValue(IterVar->getName());
360 }
361
362 if (Records.getDef(IterRec->getNameInitAsString())) {
363 Error(Loc, "def already exists: " + IterRec->getNameInitAsString());
David Greenecebb4ee2012-02-22 16:09:41 +0000364 return true;
365 }
366
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000367 Records.addDef(IterRec);
368 IterRec->resolveReferences();
David Greenecebb4ee2012-02-22 16:09:41 +0000369 return false;
370}
371
Chris Lattnerf4601652007-11-22 20:49:04 +0000372//===----------------------------------------------------------------------===//
373// Parser Code
374//===----------------------------------------------------------------------===//
375
376/// isObjectStart - Return true if this is a valid first token for an Object.
377static bool isObjectStart(tgtok::TokKind K) {
378 return K == tgtok::Class || K == tgtok::Def ||
David Greenecebb4ee2012-02-22 16:09:41 +0000379 K == tgtok::Defm || K == tgtok::Let ||
380 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4601652007-11-22 20:49:04 +0000381}
382
Chris Lattnerdf72eae2010-10-05 22:51:56 +0000383static std::string GetNewAnonymousName() {
384 static unsigned AnonCounter = 0;
Michael J. Spencerf15fe812013-02-26 21:29:47 +0000385 unsigned Tmp = AnonCounter++; // MSVC2012 ICEs without this.
386 return "anonymous." + utostr(Tmp);
Chris Lattnerdf72eae2010-10-05 22:51:56 +0000387}
388
Chris Lattnerf4601652007-11-22 20:49:04 +0000389/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Rosed1220092013-01-10 18:50:05 +0000390/// return 0.
David Greenea9e07dd2011-10-19 13:04:29 +0000391/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4601652007-11-22 20:49:04 +0000392/// ObjectName ::= /*empty*/
393///
David Greenea9e07dd2011-10-19 13:04:29 +0000394Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
395 switch (Lex.getCode()) {
396 case tgtok::colon:
397 case tgtok::semi:
398 case tgtok::l_brace:
399 // These are all of the tokens that can begin an object body.
400 // Some of these can also begin values but we disallow those cases
401 // because they are unlikely to be useful.
Jordan Rosed1220092013-01-10 18:50:05 +0000402 return 0;
David Greenea9e07dd2011-10-19 13:04:29 +0000403 default:
404 break;
405 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000406
David Greenea9e07dd2011-10-19 13:04:29 +0000407 Record *CurRec = 0;
408 if (CurMultiClass)
409 CurRec = &CurMultiClass->Rec;
410
411 RecTy *Type = 0;
412 if (CurRec) {
Sean Silva3f7b7f82012-10-10 20:24:47 +0000413 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greenea9e07dd2011-10-19 13:04:29 +0000414 if (!CurRecName) {
415 TokError("Record name is not typed!");
416 return 0;
417 }
418 Type = CurRecName->getType();
419 }
420
421 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4601652007-11-22 20:49:04 +0000422}
423
Chris Lattnerf4601652007-11-22 20:49:04 +0000424/// ParseClassID - Parse and resolve a reference to a class name. This returns
425/// null on error.
426///
427/// ClassID ::= ID
428///
429Record *TGParser::ParseClassID() {
430 if (Lex.getCode() != tgtok::Id) {
431 TokError("expected name for ClassID");
432 return 0;
433 }
Bob Wilson21870412009-11-22 04:24:42 +0000434
Chris Lattnerf4601652007-11-22 20:49:04 +0000435 Record *Result = Records.getClass(Lex.getCurStrVal());
436 if (Result == 0)
437 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson21870412009-11-22 04:24:42 +0000438
Chris Lattnerf4601652007-11-22 20:49:04 +0000439 Lex.Lex();
440 return Result;
441}
442
Bob Wilson32558652009-04-28 19:41:44 +0000443/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
444/// This returns null on error.
David Greenede444af2009-04-22 16:42:54 +0000445///
446/// MultiClassID ::= ID
447///
448MultiClass *TGParser::ParseMultiClassID() {
449 if (Lex.getCode() != tgtok::Id) {
Sean Silva36febfd2013-01-09 02:11:57 +0000450 TokError("expected name for MultiClassID");
David Greenede444af2009-04-22 16:42:54 +0000451 return 0;
452 }
Bob Wilson32558652009-04-28 19:41:44 +0000453
David Greenede444af2009-04-22 16:42:54 +0000454 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
455 if (Result == 0)
Sean Silva36febfd2013-01-09 02:11:57 +0000456 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson32558652009-04-28 19:41:44 +0000457
David Greenede444af2009-04-22 16:42:54 +0000458 Lex.Lex();
459 return Result;
460}
461
Chris Lattnerf4601652007-11-22 20:49:04 +0000462/// ParseSubClassReference - Parse a reference to a subclass or to a templated
463/// subclass. This returns a SubClassRefTy with a null Record* on error.
464///
465/// SubClassRef ::= ClassID
466/// SubClassRef ::= ClassID '<' ValueList '>'
467///
468SubClassReference TGParser::
469ParseSubClassReference(Record *CurRec, bool isDefm) {
470 SubClassReference Result;
Jordan Roseb50df4a2013-01-10 18:50:11 +0000471 Result.RefRange.Start = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +0000472
Sean Silva7be90212013-01-09 02:17:14 +0000473 if (isDefm) {
474 if (MultiClass *MC = ParseMultiClassID())
475 Result.Rec = &MC->Rec;
476 } else {
Chris Lattnerf4601652007-11-22 20:49:04 +0000477 Result.Rec = ParseClassID();
Sean Silva7be90212013-01-09 02:17:14 +0000478 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000479 if (Result.Rec == 0) return Result;
Bob Wilson21870412009-11-22 04:24:42 +0000480
Chris Lattnerf4601652007-11-22 20:49:04 +0000481 // If there is no template arg list, we're done.
Jordan Roseb50df4a2013-01-10 18:50:11 +0000482 if (Lex.getCode() != tgtok::less) {
483 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000484 return Result;
Jordan Roseb50df4a2013-01-10 18:50:11 +0000485 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000486 Lex.Lex(); // Eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000487
Chris Lattnerf4601652007-11-22 20:49:04 +0000488 if (Lex.getCode() == tgtok::greater) {
489 TokError("subclass reference requires a non-empty list of template values");
490 Result.Rec = 0;
491 return Result;
492 }
Bob Wilson21870412009-11-22 04:24:42 +0000493
David Greenee1b46912009-06-08 20:23:18 +0000494 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4601652007-11-22 20:49:04 +0000495 if (Result.TemplateArgs.empty()) {
496 Result.Rec = 0; // Error parsing value list.
497 return Result;
498 }
Bob Wilson21870412009-11-22 04:24:42 +0000499
Chris Lattnerf4601652007-11-22 20:49:04 +0000500 if (Lex.getCode() != tgtok::greater) {
501 TokError("expected '>' in template value list");
502 Result.Rec = 0;
503 return Result;
504 }
505 Lex.Lex();
Jordan Roseb50df4a2013-01-10 18:50:11 +0000506 Result.RefRange.End = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +0000507
Chris Lattnerf4601652007-11-22 20:49:04 +0000508 return Result;
509}
510
Bob Wilson32558652009-04-28 19:41:44 +0000511/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
512/// templated submulticlass. This returns a SubMultiClassRefTy with a null
513/// Record* on error.
David Greenede444af2009-04-22 16:42:54 +0000514///
515/// SubMultiClassRef ::= MultiClassID
516/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
517///
518SubMultiClassReference TGParser::
519ParseSubMultiClassReference(MultiClass *CurMC) {
520 SubMultiClassReference Result;
Jordan Roseb50df4a2013-01-10 18:50:11 +0000521 Result.RefRange.Start = Lex.getLoc();
Bob Wilson32558652009-04-28 19:41:44 +0000522
David Greenede444af2009-04-22 16:42:54 +0000523 Result.MC = ParseMultiClassID();
524 if (Result.MC == 0) return Result;
Bob Wilson32558652009-04-28 19:41:44 +0000525
David Greenede444af2009-04-22 16:42:54 +0000526 // If there is no template arg list, we're done.
Jordan Roseb50df4a2013-01-10 18:50:11 +0000527 if (Lex.getCode() != tgtok::less) {
528 Result.RefRange.End = Lex.getLoc();
David Greenede444af2009-04-22 16:42:54 +0000529 return Result;
Jordan Roseb50df4a2013-01-10 18:50:11 +0000530 }
David Greenede444af2009-04-22 16:42:54 +0000531 Lex.Lex(); // Eat the '<'
Bob Wilson32558652009-04-28 19:41:44 +0000532
David Greenede444af2009-04-22 16:42:54 +0000533 if (Lex.getCode() == tgtok::greater) {
534 TokError("subclass reference requires a non-empty list of template values");
535 Result.MC = 0;
536 return Result;
537 }
Bob Wilson32558652009-04-28 19:41:44 +0000538
David Greenee1b46912009-06-08 20:23:18 +0000539 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greenede444af2009-04-22 16:42:54 +0000540 if (Result.TemplateArgs.empty()) {
541 Result.MC = 0; // Error parsing value list.
542 return Result;
543 }
Bob Wilson32558652009-04-28 19:41:44 +0000544
David Greenede444af2009-04-22 16:42:54 +0000545 if (Lex.getCode() != tgtok::greater) {
546 TokError("expected '>' in template value list");
547 Result.MC = 0;
548 return Result;
549 }
550 Lex.Lex();
Jordan Roseb50df4a2013-01-10 18:50:11 +0000551 Result.RefRange.End = Lex.getLoc();
David Greenede444af2009-04-22 16:42:54 +0000552
553 return Result;
554}
555
Chris Lattnerf4601652007-11-22 20:49:04 +0000556/// ParseRangePiece - Parse a bit/value range.
557/// RangePiece ::= INTVAL
558/// RangePiece ::= INTVAL '-' INTVAL
559/// RangePiece ::= INTVAL INTVAL
560bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner811281e2008-01-10 07:01:53 +0000561 if (Lex.getCode() != tgtok::IntVal) {
562 TokError("expected integer or bitrange");
563 return true;
564 }
Dan Gohman63f97202008-10-17 01:33:43 +0000565 int64_t Start = Lex.getCurIntVal();
566 int64_t End;
Bob Wilson21870412009-11-22 04:24:42 +0000567
Chris Lattnerf4601652007-11-22 20:49:04 +0000568 if (Start < 0)
569 return TokError("invalid range, cannot be negative");
Bob Wilson21870412009-11-22 04:24:42 +0000570
Chris Lattnerf4601652007-11-22 20:49:04 +0000571 switch (Lex.Lex()) { // eat first character.
Bob Wilson21870412009-11-22 04:24:42 +0000572 default:
Chris Lattnerf4601652007-11-22 20:49:04 +0000573 Ranges.push_back(Start);
574 return false;
575 case tgtok::minus:
576 if (Lex.Lex() != tgtok::IntVal) {
577 TokError("expected integer value as end of range");
578 return true;
579 }
580 End = Lex.getCurIntVal();
581 break;
582 case tgtok::IntVal:
583 End = -Lex.getCurIntVal();
584 break;
585 }
Bob Wilson21870412009-11-22 04:24:42 +0000586 if (End < 0)
Chris Lattnerf4601652007-11-22 20:49:04 +0000587 return TokError("invalid range, cannot be negative");
588 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000589
Chris Lattnerf4601652007-11-22 20:49:04 +0000590 // Add to the range.
591 if (Start < End) {
592 for (; Start <= End; ++Start)
593 Ranges.push_back(Start);
594 } else {
595 for (; Start >= End; --Start)
596 Ranges.push_back(Start);
597 }
598 return false;
599}
600
601/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
602///
603/// RangeList ::= RangePiece (',' RangePiece)*
604///
605std::vector<unsigned> TGParser::ParseRangeList() {
606 std::vector<unsigned> Result;
Bob Wilson21870412009-11-22 04:24:42 +0000607
Chris Lattnerf4601652007-11-22 20:49:04 +0000608 // Parse the first piece.
609 if (ParseRangePiece(Result))
610 return std::vector<unsigned>();
611 while (Lex.getCode() == tgtok::comma) {
612 Lex.Lex(); // Eat the comma.
613
614 // Parse the next range piece.
615 if (ParseRangePiece(Result))
616 return std::vector<unsigned>();
617 }
618 return Result;
619}
620
621/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
622/// OptionalRangeList ::= '<' RangeList '>'
623/// OptionalRangeList ::= /*empty*/
624bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
625 if (Lex.getCode() != tgtok::less)
626 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000627
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000628 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000629 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000630
Chris Lattnerf4601652007-11-22 20:49:04 +0000631 // Parse the range list.
632 Ranges = ParseRangeList();
633 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000634
Chris Lattnerf4601652007-11-22 20:49:04 +0000635 if (Lex.getCode() != tgtok::greater) {
636 TokError("expected '>' at end of range list");
637 return Error(StartLoc, "to match this '<'");
638 }
639 Lex.Lex(); // eat the '>'.
640 return false;
641}
642
643/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
644/// OptionalBitList ::= '{' RangeList '}'
645/// OptionalBitList ::= /*empty*/
646bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
647 if (Lex.getCode() != tgtok::l_brace)
648 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000649
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000650 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000651 Lex.Lex(); // eat the '{'
Bob Wilson21870412009-11-22 04:24:42 +0000652
Chris Lattnerf4601652007-11-22 20:49:04 +0000653 // Parse the range list.
654 Ranges = ParseRangeList();
655 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000656
Chris Lattnerf4601652007-11-22 20:49:04 +0000657 if (Lex.getCode() != tgtok::r_brace) {
658 TokError("expected '}' at end of bit list");
659 return Error(StartLoc, "to match this '{'");
660 }
661 Lex.Lex(); // eat the '}'.
662 return false;
663}
664
665
666/// ParseType - Parse and return a tblgen type. This returns null on error.
667///
668/// Type ::= STRING // string type
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +0000669/// Type ::= CODE // code type
Chris Lattnerf4601652007-11-22 20:49:04 +0000670/// Type ::= BIT // bit type
671/// Type ::= BITS '<' INTVAL '>' // bits<x> type
672/// Type ::= INT // int type
673/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4601652007-11-22 20:49:04 +0000674/// Type ::= DAG // dag type
675/// Type ::= ClassID // Record Type
676///
677RecTy *TGParser::ParseType() {
678 switch (Lex.getCode()) {
679 default: TokError("Unknown token when expecting a type"); return 0;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000680 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +0000681 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000682 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
683 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000684 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4601652007-11-22 20:49:04 +0000685 case tgtok::Id:
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000686 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Chris Lattnerf4601652007-11-22 20:49:04 +0000687 return 0;
688 case tgtok::Bits: {
689 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
690 TokError("expected '<' after bits type");
691 return 0;
692 }
693 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
694 TokError("expected integer in bits<n> type");
695 return 0;
696 }
Dan Gohman63f97202008-10-17 01:33:43 +0000697 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4601652007-11-22 20:49:04 +0000698 if (Lex.Lex() != tgtok::greater) { // Eat count.
699 TokError("expected '>' at end of bits<n> type");
700 return 0;
701 }
702 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000703 return BitsRecTy::get(Val);
Chris Lattnerf4601652007-11-22 20:49:04 +0000704 }
705 case tgtok::List: {
706 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
707 TokError("expected '<' after list type");
708 return 0;
709 }
710 Lex.Lex(); // Eat '<'
711 RecTy *SubType = ParseType();
712 if (SubType == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +0000713
Chris Lattnerf4601652007-11-22 20:49:04 +0000714 if (Lex.getCode() != tgtok::greater) {
715 TokError("expected '>' at end of list<ty> type");
716 return 0;
717 }
718 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000719 return ListRecTy::get(SubType);
Chris Lattnerf4601652007-11-22 20:49:04 +0000720 }
Bob Wilson21870412009-11-22 04:24:42 +0000721 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000722}
723
724/// ParseIDValue - Parse an ID as a value and decode what it means.
725///
726/// IDValue ::= ID [def local value]
727/// IDValue ::= ID [def template arg]
728/// IDValue ::= ID [multiclass local value]
729/// IDValue ::= ID [multiclass template argument]
730/// IDValue ::= ID [def name]
731///
David Greenef3744a02011-10-19 13:04:20 +0000732Init *TGParser::ParseIDValue(Record *CurRec, IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000733 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
734 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000735 SMLoc Loc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000736 Lex.Lex();
737 return ParseIDValue(CurRec, Name, Loc);
738}
739
740/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
741/// has already been read.
David Greene05bce0b2011-07-29 22:43:06 +0000742Init *TGParser::ParseIDValue(Record *CurRec,
David Greenef3744a02011-10-19 13:04:20 +0000743 const std::string &Name, SMLoc NameLoc,
744 IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000745 if (CurRec) {
746 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenedcd35c72011-07-29 19:07:07 +0000747 return VarInit::get(Name, RV->getType());
Bob Wilson21870412009-11-22 04:24:42 +0000748
David Greenee22b3212011-10-19 13:02:42 +0000749 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
750
David Greenecaa25c82011-10-05 22:42:54 +0000751 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +0000752 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
753 "::");
David Greenecaa25c82011-10-05 22:42:54 +0000754
Chris Lattnerf4601652007-11-22 20:49:04 +0000755 if (CurRec->isTemplateArg(TemplateArgName)) {
756 const RecordVal *RV = CurRec->getValue(TemplateArgName);
757 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000758 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000759 }
760 }
Bob Wilson21870412009-11-22 04:24:42 +0000761
Chris Lattnerf4601652007-11-22 20:49:04 +0000762 if (CurMultiClass) {
David Greenee22b3212011-10-19 13:02:42 +0000763 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
764 "::");
765
Chris Lattnerf4601652007-11-22 20:49:04 +0000766 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
767 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
768 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000769 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000770 }
771 }
Bob Wilson21870412009-11-22 04:24:42 +0000772
David Greenecebb4ee2012-02-22 16:09:41 +0000773 // If this is in a foreach loop, make sure it's not a loop iterator
774 for (LoopVector::iterator i = Loops.begin(), iend = Loops.end();
775 i != iend;
776 ++i) {
Sean Silva6cfc8062012-10-10 20:24:43 +0000777 VarInit *IterVar = dyn_cast<VarInit>(i->IterVar);
David Greenecebb4ee2012-02-22 16:09:41 +0000778 if (IterVar && IterVar->getName() == Name)
779 return IterVar;
780 }
781
David Greenebbec2792011-10-19 13:04:21 +0000782 if (Mode == ParseNameMode)
783 return StringInit::get(Name);
784
Chris Lattnerf4601652007-11-22 20:49:04 +0000785 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000786 return DefInit::get(D);
Chris Lattnerf4601652007-11-22 20:49:04 +0000787
David Greenebbec2792011-10-19 13:04:21 +0000788 if (Mode == ParseValueMode) {
789 Error(NameLoc, "Variable not defined: '" + Name + "'");
790 return 0;
791 }
792
793 return StringInit::get(Name);
Chris Lattnerf4601652007-11-22 20:49:04 +0000794}
795
David Greened418c1b2009-05-14 20:54:48 +0000796/// ParseOperation - Parse an operator. This returns null on error.
797///
798/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
799///
David Greene05bce0b2011-07-29 22:43:06 +0000800Init *TGParser::ParseOperation(Record *CurRec) {
David Greened418c1b2009-05-14 20:54:48 +0000801 switch (Lex.getCode()) {
802 default:
803 TokError("unknown operation");
804 return 0;
David Greene1434f662011-01-07 17:05:37 +0000805 case tgtok::XHead:
806 case tgtok::XTail:
807 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +0000808 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
809 UnOpInit::UnaryOp Code;
810 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000811
David Greenee6c27de2009-05-14 21:22:49 +0000812 switch (Lex.getCode()) {
Craig Topper85814382012-02-07 05:05:23 +0000813 default: llvm_unreachable("Unhandled code!");
David Greenee6c27de2009-05-14 21:22:49 +0000814 case tgtok::XCast:
815 Lex.Lex(); // eat the operation
816 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000817
David Greenee6c27de2009-05-14 21:22:49 +0000818 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000819
David Greenee6c27de2009-05-14 21:22:49 +0000820 if (Type == 0) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000821 TokError("did not get type for unary operator");
David Greenee6c27de2009-05-14 21:22:49 +0000822 return 0;
823 }
David Greened418c1b2009-05-14 20:54:48 +0000824
David Greenee6c27de2009-05-14 21:22:49 +0000825 break;
David Greene1434f662011-01-07 17:05:37 +0000826 case tgtok::XHead:
David Greene5f9f9ba2009-05-14 22:38:31 +0000827 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000828 Code = UnOpInit::HEAD;
David Greene5f9f9ba2009-05-14 22:38:31 +0000829 break;
David Greene1434f662011-01-07 17:05:37 +0000830 case tgtok::XTail:
David Greene5f9f9ba2009-05-14 22:38:31 +0000831 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000832 Code = UnOpInit::TAIL;
David Greene5f9f9ba2009-05-14 22:38:31 +0000833 break;
David Greene1434f662011-01-07 17:05:37 +0000834 case tgtok::XEmpty:
David Greene5f9f9ba2009-05-14 22:38:31 +0000835 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000836 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000837 Type = IntRecTy::get();
David Greene5f9f9ba2009-05-14 22:38:31 +0000838 break;
David Greenee6c27de2009-05-14 21:22:49 +0000839 }
840 if (Lex.getCode() != tgtok::l_paren) {
841 TokError("expected '(' after unary operator");
842 return 0;
843 }
844 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000845
David Greene05bce0b2011-07-29 22:43:06 +0000846 Init *LHS = ParseValue(CurRec);
David Greenee6c27de2009-05-14 21:22:49 +0000847 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000848
David Greene1434f662011-01-07 17:05:37 +0000849 if (Code == UnOpInit::HEAD
850 || Code == UnOpInit::TAIL
851 || Code == UnOpInit::EMPTY) {
Sean Silva6cfc8062012-10-10 20:24:43 +0000852 ListInit *LHSl = dyn_cast<ListInit>(LHS);
853 StringInit *LHSs = dyn_cast<StringInit>(LHS);
854 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000855 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
856 TokError("expected list or string type argument in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000857 return 0;
858 }
859 if (LHSt) {
Sean Silva736ceac2012-10-05 03:31:58 +0000860 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
861 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
David Greenee1b46912009-06-08 20:23:18 +0000862 if (LType == 0 && SType == 0) {
863 TokError("expected list or string type argumnet in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000864 return 0;
865 }
866 }
867
David Greene1434f662011-01-07 17:05:37 +0000868 if (Code == UnOpInit::HEAD
869 || Code == UnOpInit::TAIL) {
David Greenee1b46912009-06-08 20:23:18 +0000870 if (LHSl == 0 && LHSt == 0) {
871 TokError("expected list type argumnet in unary operator");
872 return 0;
873 }
Bob Wilson21870412009-11-22 04:24:42 +0000874
David Greene5f9f9ba2009-05-14 22:38:31 +0000875 if (LHSl && LHSl->getSize() == 0) {
876 TokError("empty list argument in unary operator");
877 return 0;
878 }
879 if (LHSl) {
David Greene05bce0b2011-07-29 22:43:06 +0000880 Init *Item = LHSl->getElement(0);
Sean Silva6cfc8062012-10-10 20:24:43 +0000881 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
David Greene5f9f9ba2009-05-14 22:38:31 +0000882 if (Itemt == 0) {
883 TokError("untyped list element in unary operator");
884 return 0;
885 }
David Greene1434f662011-01-07 17:05:37 +0000886 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000887 Type = Itemt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000888 } else {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000889 Type = ListRecTy::get(Itemt->getType());
David Greene5f9f9ba2009-05-14 22:38:31 +0000890 }
Bob Wilson21870412009-11-22 04:24:42 +0000891 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000892 assert(LHSt && "expected list type argument in unary operator");
Sean Silva736ceac2012-10-05 03:31:58 +0000893 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
David Greene5f9f9ba2009-05-14 22:38:31 +0000894 if (LType == 0) {
895 TokError("expected list type argumnet in unary operator");
896 return 0;
897 }
David Greene1434f662011-01-07 17:05:37 +0000898 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000899 Type = LType->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +0000900 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000901 Type = LType;
902 }
903 }
904 }
905 }
906
David Greenee6c27de2009-05-14 21:22:49 +0000907 if (Lex.getCode() != tgtok::r_paren) {
908 TokError("expected ')' in unary operator");
909 return 0;
910 }
911 Lex.Lex(); // eat the ')'
David Greenedcd35c72011-07-29 19:07:07 +0000912 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee6c27de2009-05-14 21:22:49 +0000913 }
David Greened418c1b2009-05-14 20:54:48 +0000914
915 case tgtok::XConcat:
Hal Finkeld23a41c2013-01-25 14:49:08 +0000916 case tgtok::XADD:
Bob Wilson21870412009-11-22 04:24:42 +0000917 case tgtok::XSRA:
David Greened418c1b2009-05-14 20:54:48 +0000918 case tgtok::XSRL:
919 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +0000920 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +0000921 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000922 tgtok::TokKind OpTok = Lex.getCode();
923 SMLoc OpLoc = Lex.getLoc();
924 Lex.Lex(); // eat the operation
925
David Greened418c1b2009-05-14 20:54:48 +0000926 BinOpInit::BinaryOp Code;
927 RecTy *Type = 0;
928
Chris Lattner8d978a72010-10-05 23:58:18 +0000929 switch (OpTok) {
Craig Topper85814382012-02-07 05:05:23 +0000930 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000931 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkeld23a41c2013-01-25 14:49:08 +0000932 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000933 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
934 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
935 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
936 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Bob Wilson21870412009-11-22 04:24:42 +0000937 case tgtok::XStrConcat:
David Greened418c1b2009-05-14 20:54:48 +0000938 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000939 Type = StringRecTy::get();
David Greened418c1b2009-05-14 20:54:48 +0000940 break;
David Greened418c1b2009-05-14 20:54:48 +0000941 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000942
David Greened418c1b2009-05-14 20:54:48 +0000943 if (Lex.getCode() != tgtok::l_paren) {
944 TokError("expected '(' after binary operator");
945 return 0;
946 }
947 Lex.Lex(); // eat the '('
948
David Greene05bce0b2011-07-29 22:43:06 +0000949 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000950
Chris Lattner8d978a72010-10-05 23:58:18 +0000951 InitList.push_back(ParseValue(CurRec));
952 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000953
Chris Lattner8d978a72010-10-05 23:58:18 +0000954 while (Lex.getCode() == tgtok::comma) {
955 Lex.Lex(); // eat the ','
956
957 InitList.push_back(ParseValue(CurRec));
958 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000959 }
David Greened418c1b2009-05-14 20:54:48 +0000960
961 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner8d978a72010-10-05 23:58:18 +0000962 TokError("expected ')' in operator");
David Greened418c1b2009-05-14 20:54:48 +0000963 return 0;
964 }
965 Lex.Lex(); // eat the ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000966
967 // We allow multiple operands to associative operators like !strconcat as
968 // shorthand for nesting them.
969 if (Code == BinOpInit::STRCONCAT) {
970 while (InitList.size() > 2) {
David Greene05bce0b2011-07-29 22:43:06 +0000971 Init *RHS = InitList.pop_back_val();
David Greenedcd35c72011-07-29 19:07:07 +0000972 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
973 ->Fold(CurRec, CurMultiClass);
Chris Lattner8d978a72010-10-05 23:58:18 +0000974 InitList.back() = RHS;
975 }
976 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000977
Chris Lattner8d978a72010-10-05 23:58:18 +0000978 if (InitList.size() == 2)
David Greenedcd35c72011-07-29 19:07:07 +0000979 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner8d978a72010-10-05 23:58:18 +0000980 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000981
Chris Lattner8d978a72010-10-05 23:58:18 +0000982 Error(OpLoc, "expected two operands to operator");
983 return 0;
David Greened418c1b2009-05-14 20:54:48 +0000984 }
985
David Greene9bea7c82009-05-14 23:26:46 +0000986 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +0000987 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +0000988 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
989 TernOpInit::TernaryOp Code;
990 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000991
David Greene4afc5092009-05-14 21:54:42 +0000992 tgtok::TokKind LexCode = Lex.getCode();
993 Lex.Lex(); // eat the operation
994 switch (LexCode) {
Craig Topper85814382012-02-07 05:05:23 +0000995 default: llvm_unreachable("Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000996 case tgtok::XIf:
997 Code = TernOpInit::IF;
998 break;
David Greenebeb31a52009-05-14 22:23:47 +0000999 case tgtok::XForEach:
1000 Code = TernOpInit::FOREACH;
1001 break;
David Greene4afc5092009-05-14 21:54:42 +00001002 case tgtok::XSubst:
1003 Code = TernOpInit::SUBST;
1004 break;
1005 }
1006 if (Lex.getCode() != tgtok::l_paren) {
1007 TokError("expected '(' after ternary operator");
1008 return 0;
1009 }
1010 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +00001011
David Greene05bce0b2011-07-29 22:43:06 +00001012 Init *LHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001013 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001014
David Greene4afc5092009-05-14 21:54:42 +00001015 if (Lex.getCode() != tgtok::comma) {
1016 TokError("expected ',' in ternary operator");
1017 return 0;
1018 }
1019 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001020
David Greene05bce0b2011-07-29 22:43:06 +00001021 Init *MHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001022 if (MHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001023
David Greene4afc5092009-05-14 21:54:42 +00001024 if (Lex.getCode() != tgtok::comma) {
1025 TokError("expected ',' in ternary operator");
1026 return 0;
1027 }
1028 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001029
David Greene05bce0b2011-07-29 22:43:06 +00001030 Init *RHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001031 if (RHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001032
David Greene4afc5092009-05-14 21:54:42 +00001033 if (Lex.getCode() != tgtok::r_paren) {
1034 TokError("expected ')' in binary operator");
1035 return 0;
1036 }
1037 Lex.Lex(); // eat the ')'
David Greened418c1b2009-05-14 20:54:48 +00001038
David Greene4afc5092009-05-14 21:54:42 +00001039 switch (LexCode) {
Craig Topper85814382012-02-07 05:05:23 +00001040 default: llvm_unreachable("Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +00001041 case tgtok::XIf: {
Bill Wendling548f5a02010-12-13 01:46:19 +00001042 RecTy *MHSTy = 0;
1043 RecTy *RHSTy = 0;
1044
Sean Silva6cfc8062012-10-10 20:24:43 +00001045 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling548f5a02010-12-13 01:46:19 +00001046 MHSTy = MHSt->getType();
Sean Silva6cfc8062012-10-10 20:24:43 +00001047 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao307525c2012-09-06 23:32:48 +00001048 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva3f7b7f82012-10-10 20:24:47 +00001049 if (isa<BitInit>(MHS))
Michael Liao307525c2012-09-06 23:32:48 +00001050 MHSTy = BitRecTy::get();
1051
Sean Silva6cfc8062012-10-10 20:24:43 +00001052 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling548f5a02010-12-13 01:46:19 +00001053 RHSTy = RHSt->getType();
Sean Silva6cfc8062012-10-10 20:24:43 +00001054 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao307525c2012-09-06 23:32:48 +00001055 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva3f7b7f82012-10-10 20:24:47 +00001056 if (isa<BitInit>(RHS))
Michael Liao307525c2012-09-06 23:32:48 +00001057 RHSTy = BitRecTy::get();
1058
1059 // For UnsetInit, it's typed from the other hand.
Sean Silva3f7b7f82012-10-10 20:24:47 +00001060 if (isa<UnsetInit>(MHS))
Michael Liao307525c2012-09-06 23:32:48 +00001061 MHSTy = RHSTy;
Sean Silva3f7b7f82012-10-10 20:24:47 +00001062 if (isa<UnsetInit>(RHS))
Michael Liao307525c2012-09-06 23:32:48 +00001063 RHSTy = MHSTy;
Bill Wendling548f5a02010-12-13 01:46:19 +00001064
1065 if (!MHSTy || !RHSTy) {
David Greene9bea7c82009-05-14 23:26:46 +00001066 TokError("could not get type for !if");
1067 return 0;
1068 }
Bill Wendling548f5a02010-12-13 01:46:19 +00001069
1070 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1071 Type = RHSTy;
1072 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1073 Type = MHSTy;
Bob Wilson21870412009-11-22 04:24:42 +00001074 } else {
David Greene9bea7c82009-05-14 23:26:46 +00001075 TokError("inconsistent types for !if");
1076 return 0;
1077 }
1078 break;
1079 }
David Greenebeb31a52009-05-14 22:23:47 +00001080 case tgtok::XForEach: {
Sean Silva6cfc8062012-10-10 20:24:43 +00001081 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
David Greenebeb31a52009-05-14 22:23:47 +00001082 if (MHSt == 0) {
1083 TokError("could not get type for !foreach");
1084 return 0;
1085 }
1086 Type = MHSt->getType();
1087 break;
1088 }
David Greene4afc5092009-05-14 21:54:42 +00001089 case tgtok::XSubst: {
Sean Silva6cfc8062012-10-10 20:24:43 +00001090 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
David Greene4afc5092009-05-14 21:54:42 +00001091 if (RHSt == 0) {
1092 TokError("could not get type for !subst");
1093 return 0;
1094 }
1095 Type = RHSt->getType();
1096 break;
1097 }
1098 }
David Greenedcd35c72011-07-29 19:07:07 +00001099 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson21870412009-11-22 04:24:42 +00001100 CurMultiClass);
David Greene4afc5092009-05-14 21:54:42 +00001101 }
David Greened418c1b2009-05-14 20:54:48 +00001102 }
David Greened418c1b2009-05-14 20:54:48 +00001103}
1104
1105/// ParseOperatorType - Parse a type for an operator. This returns
1106/// null on error.
1107///
1108/// OperatorType ::= '<' Type '>'
1109///
Dan Gohmana9ad0412009-08-12 22:10:57 +00001110RecTy *TGParser::ParseOperatorType() {
David Greened418c1b2009-05-14 20:54:48 +00001111 RecTy *Type = 0;
1112
1113 if (Lex.getCode() != tgtok::less) {
1114 TokError("expected type name for operator");
1115 return 0;
1116 }
1117 Lex.Lex(); // eat the <
1118
1119 Type = ParseType();
1120
1121 if (Type == 0) {
1122 TokError("expected type name for operator");
1123 return 0;
1124 }
1125
1126 if (Lex.getCode() != tgtok::greater) {
1127 TokError("expected type name for operator");
1128 return 0;
1129 }
1130 Lex.Lex(); // eat the >
1131
1132 return Type;
1133}
1134
1135
Chris Lattnerf4601652007-11-22 20:49:04 +00001136/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1137///
1138/// SimpleValue ::= IDValue
1139/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001140/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +00001141/// SimpleValue ::= CODEFRAGMENT
1142/// SimpleValue ::= '?'
1143/// SimpleValue ::= '{' ValueList '}'
1144/// SimpleValue ::= ID '<' ValueListNE '>'
1145/// SimpleValue ::= '[' ValueList ']'
1146/// SimpleValue ::= '(' IDValue DagArgList ')'
1147/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkeld23a41c2013-01-25 14:49:08 +00001148/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001149/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1150/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1151/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1152/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1153///
David Greenef3744a02011-10-19 13:04:20 +00001154Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1155 IDParseMode Mode) {
David Greene05bce0b2011-07-29 22:43:06 +00001156 Init *R = 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001157 switch (Lex.getCode()) {
1158 default: TokError("Unknown token when parsing a value"); break;
David Greened3d1cad2011-10-19 13:04:43 +00001159 case tgtok::paste:
1160 // This is a leading paste operation. This is deprecated but
1161 // still exists in some .td files. Ignore it.
1162 Lex.Lex(); // Skip '#'.
1163 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenedcd35c72011-07-29 19:07:07 +00001164 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001165 case tgtok::StrVal: {
1166 std::string Val = Lex.getCurStrVal();
1167 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001168
Jim Grosbachda4231f2009-03-26 16:17:51 +00001169 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001170 while (Lex.getCode() == tgtok::StrVal) {
1171 Val += Lex.getCurStrVal();
1172 Lex.Lex();
1173 }
Bob Wilson21870412009-11-22 04:24:42 +00001174
David Greenedcd35c72011-07-29 19:07:07 +00001175 R = StringInit::get(Val);
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001176 break;
1177 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001178 case tgtok::CodeFragment:
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +00001179 R = StringInit::get(Lex.getCurStrVal());
Chris Lattner578bcf02010-10-06 04:31:40 +00001180 Lex.Lex();
1181 break;
1182 case tgtok::question:
David Greenedcd35c72011-07-29 19:07:07 +00001183 R = UnsetInit::get();
Chris Lattner578bcf02010-10-06 04:31:40 +00001184 Lex.Lex();
1185 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001186 case tgtok::Id: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001187 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001188 std::string Name = Lex.getCurStrVal();
1189 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greenef3744a02011-10-19 13:04:20 +00001190 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson21870412009-11-22 04:24:42 +00001191
Chris Lattnerf4601652007-11-22 20:49:04 +00001192 // Value ::= ID '<' ValueListNE '>'
1193 if (Lex.Lex() == tgtok::greater) {
1194 TokError("expected non-empty value list");
1195 return 0;
1196 }
David Greenee1b46912009-06-08 20:23:18 +00001197
Chris Lattnerf4601652007-11-22 20:49:04 +00001198 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1199 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1200 // body.
1201 Record *Class = Records.getClass(Name);
1202 if (!Class) {
1203 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1204 return 0;
1205 }
David Greenee1b46912009-06-08 20:23:18 +00001206
David Greene05bce0b2011-07-29 22:43:06 +00001207 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
David Greenee1b46912009-06-08 20:23:18 +00001208 if (ValueList.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001209
David Greenee1b46912009-06-08 20:23:18 +00001210 if (Lex.getCode() != tgtok::greater) {
1211 TokError("expected '>' at end of value list");
1212 return 0;
1213 }
1214 Lex.Lex(); // eat the '>'
Jordan Roseb50df4a2013-01-10 18:50:11 +00001215 SMLoc EndLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +00001216
Chris Lattnerf4601652007-11-22 20:49:04 +00001217 // Create the new record, set it as CurRec temporarily.
1218 static unsigned AnonCounter = 0;
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001219 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),
1220 NameLoc,
Jordan Rosed1220092013-01-10 18:50:05 +00001221 Records,
1222 /*IsAnonymous=*/true);
Chris Lattnerf4601652007-11-22 20:49:04 +00001223 SubClassReference SCRef;
Jordan Roseb50df4a2013-01-10 18:50:11 +00001224 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4601652007-11-22 20:49:04 +00001225 SCRef.Rec = Class;
1226 SCRef.TemplateArgs = ValueList;
1227 // Add info about the subclass to NewRec.
1228 if (AddSubClass(NewRec, SCRef))
1229 return 0;
1230 NewRec->resolveReferences();
1231 Records.addDef(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001232
Chris Lattnerf4601652007-11-22 20:49:04 +00001233 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +00001234 return DefInit::get(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001235 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001236 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001237 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001238 Lex.Lex(); // eat the '{'
David Greene05bce0b2011-07-29 22:43:06 +00001239 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001240
Chris Lattnerf4601652007-11-22 20:49:04 +00001241 if (Lex.getCode() != tgtok::r_brace) {
1242 Vals = ParseValueList(CurRec);
1243 if (Vals.empty()) return 0;
1244 }
1245 if (Lex.getCode() != tgtok::r_brace) {
1246 TokError("expected '}' at end of bit list value");
1247 return 0;
1248 }
1249 Lex.Lex(); // eat the '}'
Bob Wilson21870412009-11-22 04:24:42 +00001250
David Greene05bce0b2011-07-29 22:43:06 +00001251 SmallVector<Init *, 16> NewBits(Vals.size());
David Greeneca7fd3d2011-07-29 19:07:00 +00001252
Chris Lattnerf4601652007-11-22 20:49:04 +00001253 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001254 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Chris Lattnerf4601652007-11-22 20:49:04 +00001255 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +00001256 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1257 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +00001258 return 0;
1259 }
David Greeneca7fd3d2011-07-29 19:07:00 +00001260 NewBits[Vals.size()-i-1] = Bit;
Chris Lattnerf4601652007-11-22 20:49:04 +00001261 }
David Greenedcd35c72011-07-29 19:07:07 +00001262 return BitsInit::get(NewBits);
Chris Lattnerf4601652007-11-22 20:49:04 +00001263 }
1264 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1265 Lex.Lex(); // eat the '['
David Greene05bce0b2011-07-29 22:43:06 +00001266 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001267
David Greenee1b46912009-06-08 20:23:18 +00001268 RecTy *DeducedEltTy = 0;
1269 ListRecTy *GivenListTy = 0;
Bob Wilson21870412009-11-22 04:24:42 +00001270
David Greenee1b46912009-06-08 20:23:18 +00001271 if (ItemType != 0) {
Sean Silva736ceac2012-10-05 03:31:58 +00001272 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
David Greenee1b46912009-06-08 20:23:18 +00001273 if (ListType == 0) {
1274 std::stringstream s;
Bob Wilson21870412009-11-22 04:24:42 +00001275 s << "Type mismatch for list, expected list type, got "
David Greenee1b46912009-06-08 20:23:18 +00001276 << ItemType->getAsString();
1277 TokError(s.str());
Jim Grosbach6a44ada2011-03-11 19:52:52 +00001278 return 0;
David Greenee1b46912009-06-08 20:23:18 +00001279 }
1280 GivenListTy = ListType;
Bob Wilson21870412009-11-22 04:24:42 +00001281 }
David Greenee1b46912009-06-08 20:23:18 +00001282
Chris Lattnerf4601652007-11-22 20:49:04 +00001283 if (Lex.getCode() != tgtok::r_square) {
Bob Wilson21870412009-11-22 04:24:42 +00001284 Vals = ParseValueList(CurRec, 0,
1285 GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001286 if (Vals.empty()) return 0;
1287 }
1288 if (Lex.getCode() != tgtok::r_square) {
1289 TokError("expected ']' at end of list value");
1290 return 0;
1291 }
1292 Lex.Lex(); // eat the ']'
David Greenee1b46912009-06-08 20:23:18 +00001293
1294 RecTy *GivenEltTy = 0;
1295 if (Lex.getCode() == tgtok::less) {
1296 // Optional list element type
1297 Lex.Lex(); // eat the '<'
1298
1299 GivenEltTy = ParseType();
1300 if (GivenEltTy == 0) {
1301 // Couldn't parse element type
1302 return 0;
1303 }
1304
1305 if (Lex.getCode() != tgtok::greater) {
1306 TokError("expected '>' at end of list element type");
1307 return 0;
1308 }
1309 Lex.Lex(); // eat the '>'
1310 }
1311
1312 // Check elements
1313 RecTy *EltTy = 0;
David Greene05bce0b2011-07-29 22:43:06 +00001314 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greenee1b46912009-06-08 20:23:18 +00001315 i != ie;
1316 ++i) {
Sean Silva6cfc8062012-10-10 20:24:43 +00001317 TypedInit *TArg = dyn_cast<TypedInit>(*i);
David Greenee1b46912009-06-08 20:23:18 +00001318 if (TArg == 0) {
1319 TokError("Untyped list element");
1320 return 0;
1321 }
1322 if (EltTy != 0) {
1323 EltTy = resolveTypes(EltTy, TArg->getType());
1324 if (EltTy == 0) {
1325 TokError("Incompatible types in list elements");
1326 return 0;
1327 }
Bob Wilson21870412009-11-22 04:24:42 +00001328 } else {
David Greenee1b46912009-06-08 20:23:18 +00001329 EltTy = TArg->getType();
1330 }
1331 }
1332
1333 if (GivenEltTy != 0) {
1334 if (EltTy != 0) {
1335 // Verify consistency
1336 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1337 TokError("Incompatible types in list elements");
1338 return 0;
1339 }
1340 }
1341 EltTy = GivenEltTy;
1342 }
1343
1344 if (EltTy == 0) {
1345 if (ItemType == 0) {
1346 TokError("No type for list");
1347 return 0;
1348 }
1349 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +00001350 } else {
David Greenee1b46912009-06-08 20:23:18 +00001351 // Make sure the deduced type is compatible with the given type
1352 if (GivenListTy) {
1353 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1354 TokError("Element type mismatch for list");
1355 return 0;
1356 }
1357 }
1358 DeducedEltTy = EltTy;
1359 }
Bob Wilson21870412009-11-22 04:24:42 +00001360
David Greenedcd35c72011-07-29 19:07:07 +00001361 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4601652007-11-22 20:49:04 +00001362 }
1363 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1364 Lex.Lex(); // eat the '('
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001365 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001366 TokError("expected identifier in dag init");
1367 return 0;
1368 }
Bob Wilson21870412009-11-22 04:24:42 +00001369
David Greene05bce0b2011-07-29 22:43:06 +00001370 Init *Operator = ParseValue(CurRec);
Chris Lattner578bcf02010-10-06 04:31:40 +00001371 if (Operator == 0) return 0;
David Greenec7cafcd2009-04-22 20:18:10 +00001372
Nate Begeman7cee8172009-03-19 05:21:56 +00001373 // If the operator name is present, parse it.
1374 std::string OperatorName;
1375 if (Lex.getCode() == tgtok::colon) {
1376 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1377 TokError("expected variable name in dag operator");
1378 return 0;
1379 }
1380 OperatorName = Lex.getCurStrVal();
1381 Lex.Lex(); // eat the VarName.
1382 }
Bob Wilson21870412009-11-22 04:24:42 +00001383
David Greene05bce0b2011-07-29 22:43:06 +00001384 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4601652007-11-22 20:49:04 +00001385 if (Lex.getCode() != tgtok::r_paren) {
1386 DagArgs = ParseDagArgList(CurRec);
1387 if (DagArgs.empty()) return 0;
1388 }
Bob Wilson21870412009-11-22 04:24:42 +00001389
Chris Lattnerf4601652007-11-22 20:49:04 +00001390 if (Lex.getCode() != tgtok::r_paren) {
1391 TokError("expected ')' in dag init");
1392 return 0;
1393 }
1394 Lex.Lex(); // eat the ')'
Bob Wilson21870412009-11-22 04:24:42 +00001395
David Greenedcd35c72011-07-29 19:07:07 +00001396 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4601652007-11-22 20:49:04 +00001397 }
Bob Wilson21870412009-11-22 04:24:42 +00001398
David Greene1434f662011-01-07 17:05:37 +00001399 case tgtok::XHead:
1400 case tgtok::XTail:
1401 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +00001402 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001403 case tgtok::XConcat:
Hal Finkeld23a41c2013-01-25 14:49:08 +00001404 case tgtok::XADD:
Bob Wilson21870412009-11-22 04:24:42 +00001405 case tgtok::XSRA:
Chris Lattnerf4601652007-11-22 20:49:04 +00001406 case tgtok::XSRL:
1407 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +00001408 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001409 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene9bea7c82009-05-14 23:26:46 +00001410 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +00001411 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001412 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greened418c1b2009-05-14 20:54:48 +00001413 return ParseOperation(CurRec);
Chris Lattnerf4601652007-11-22 20:49:04 +00001414 }
1415 }
Bob Wilson21870412009-11-22 04:24:42 +00001416
Chris Lattnerf4601652007-11-22 20:49:04 +00001417 return R;
1418}
1419
1420/// ParseValue - Parse a tblgen value. This returns null on error.
1421///
1422/// Value ::= SimpleValue ValueSuffix*
1423/// ValueSuffix ::= '{' BitList '}'
1424/// ValueSuffix ::= '[' BitList ']'
1425/// ValueSuffix ::= '.' ID
1426///
David Greenef3744a02011-10-19 13:04:20 +00001427Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1428 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Chris Lattnerf4601652007-11-22 20:49:04 +00001429 if (Result == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001430
Chris Lattnerf4601652007-11-22 20:49:04 +00001431 // Parse the suffixes now if present.
1432 while (1) {
1433 switch (Lex.getCode()) {
1434 default: return Result;
1435 case tgtok::l_brace: {
David Greenecebb4ee2012-02-22 16:09:41 +00001436 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greene8592b2b2011-10-19 13:04:26 +00001437 // This is the beginning of the object body.
1438 return Result;
1439
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001440 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001441 Lex.Lex(); // eat the '{'
1442 std::vector<unsigned> Ranges = ParseRangeList();
1443 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001444
Chris Lattnerf4601652007-11-22 20:49:04 +00001445 // Reverse the bitlist.
1446 std::reverse(Ranges.begin(), Ranges.end());
1447 Result = Result->convertInitializerBitRange(Ranges);
1448 if (Result == 0) {
1449 Error(CurlyLoc, "Invalid bit range for value");
1450 return 0;
1451 }
Bob Wilson21870412009-11-22 04:24:42 +00001452
Chris Lattnerf4601652007-11-22 20:49:04 +00001453 // Eat the '}'.
1454 if (Lex.getCode() != tgtok::r_brace) {
1455 TokError("expected '}' at end of bit range list");
1456 return 0;
1457 }
1458 Lex.Lex();
1459 break;
1460 }
1461 case tgtok::l_square: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001462 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001463 Lex.Lex(); // eat the '['
1464 std::vector<unsigned> Ranges = ParseRangeList();
1465 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001466
Chris Lattnerf4601652007-11-22 20:49:04 +00001467 Result = Result->convertInitListSlice(Ranges);
1468 if (Result == 0) {
1469 Error(SquareLoc, "Invalid range for list slice");
1470 return 0;
1471 }
Bob Wilson21870412009-11-22 04:24:42 +00001472
Chris Lattnerf4601652007-11-22 20:49:04 +00001473 // Eat the ']'.
1474 if (Lex.getCode() != tgtok::r_square) {
1475 TokError("expected ']' at end of list slice");
1476 return 0;
1477 }
1478 Lex.Lex();
1479 break;
1480 }
1481 case tgtok::period:
1482 if (Lex.Lex() != tgtok::Id) { // eat the .
1483 TokError("expected field identifier after '.'");
1484 return 0;
1485 }
1486 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001487 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00001488 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +00001489 return 0;
1490 }
David Greenedcd35c72011-07-29 19:07:07 +00001491 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001492 Lex.Lex(); // eat field name
1493 break;
David Greened3d1cad2011-10-19 13:04:43 +00001494
1495 case tgtok::paste:
1496 SMLoc PasteLoc = Lex.getLoc();
1497
1498 // Create a !strconcat() operation, first casting each operand to
1499 // a string if necessary.
1500
Sean Silva6cfc8062012-10-10 20:24:43 +00001501 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greened3d1cad2011-10-19 13:04:43 +00001502 if (!LHS) {
1503 Error(PasteLoc, "LHS of paste is not typed!");
1504 return 0;
1505 }
1506
1507 if (LHS->getType() != StringRecTy::get()) {
1508 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1509 }
1510
1511 TypedInit *RHS = 0;
1512
1513 Lex.Lex(); // Eat the '#'.
1514 switch (Lex.getCode()) {
1515 case tgtok::colon:
1516 case tgtok::semi:
1517 case tgtok::l_brace:
1518 // These are all of the tokens that can begin an object body.
1519 // Some of these can also begin values but we disallow those cases
1520 // because they are unlikely to be useful.
1521
1522 // Trailing paste, concat with an empty string.
1523 RHS = StringInit::get("");
1524 break;
1525
1526 default:
1527 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silva6cfc8062012-10-10 20:24:43 +00001528 RHS = dyn_cast<TypedInit>(RHSResult);
David Greened3d1cad2011-10-19 13:04:43 +00001529 if (!RHS) {
1530 Error(PasteLoc, "RHS of paste is not typed!");
1531 return 0;
1532 }
1533
1534 if (RHS->getType() != StringRecTy::get()) {
1535 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1536 }
1537
1538 break;
1539 }
1540
1541 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1542 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1543 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001544 }
1545 }
1546}
1547
1548/// ParseDagArgList - Parse the argument list for a dag literal expression.
1549///
Jakob Stoklund Olesen4717fd42013-03-24 19:36:51 +00001550/// DagArg ::= Value (':' VARNAME)?
1551/// DagArg ::= VARNAME
1552/// DagArgList ::= DagArg
1553/// DagArgList ::= DagArgList ',' DagArg
David Greene05bce0b2011-07-29 22:43:06 +00001554std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4601652007-11-22 20:49:04 +00001555TGParser::ParseDagArgList(Record *CurRec) {
David Greene05bce0b2011-07-29 22:43:06 +00001556 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson21870412009-11-22 04:24:42 +00001557
Chris Lattnerf4601652007-11-22 20:49:04 +00001558 while (1) {
Jakob Stoklund Olesen4717fd42013-03-24 19:36:51 +00001559 // DagArg ::= VARNAME
1560 if (Lex.getCode() == tgtok::VarName) {
1561 // A missing value is treated like '?'.
1562 Result.push_back(std::make_pair(UnsetInit::get(), Lex.getCurStrVal()));
1563 Lex.Lex();
1564 } else {
1565 // DagArg ::= Value (':' VARNAME)?
1566 Init *Val = ParseValue(CurRec);
1567 if (Val == 0)
David Greene05bce0b2011-07-29 22:43:06 +00001568 return std::vector<std::pair<llvm::Init*, std::string> >();
Jakob Stoklund Olesen4717fd42013-03-24 19:36:51 +00001569
1570 // If the variable name is present, add it.
1571 std::string VarName;
1572 if (Lex.getCode() == tgtok::colon) {
1573 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1574 TokError("expected variable name in dag literal");
1575 return std::vector<std::pair<llvm::Init*, std::string> >();
1576 }
1577 VarName = Lex.getCurStrVal();
1578 Lex.Lex(); // eat the VarName.
Chris Lattnerf4601652007-11-22 20:49:04 +00001579 }
Jakob Stoklund Olesen4717fd42013-03-24 19:36:51 +00001580
1581 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4601652007-11-22 20:49:04 +00001582 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001583 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson21870412009-11-22 04:24:42 +00001584 Lex.Lex(); // eat the ','
Chris Lattnerf4601652007-11-22 20:49:04 +00001585 }
Bob Wilson21870412009-11-22 04:24:42 +00001586
Chris Lattnerf4601652007-11-22 20:49:04 +00001587 return Result;
1588}
1589
1590
1591/// ParseValueList - Parse a comma separated list of values, returning them as a
1592/// vector. Note that this always expects to be able to parse at least one
1593/// value. It returns an empty list if this is not possible.
1594///
1595/// ValueList ::= Value (',' Value)
1596///
David Greene05bce0b2011-07-29 22:43:06 +00001597std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopherd568b3f2011-07-11 23:06:52 +00001598 RecTy *EltTy) {
David Greene05bce0b2011-07-29 22:43:06 +00001599 std::vector<Init*> Result;
David Greenee1b46912009-06-08 20:23:18 +00001600 RecTy *ItemType = EltTy;
David Greene67acdf22009-06-29 19:59:52 +00001601 unsigned int ArgN = 0;
David Greenee1b46912009-06-08 20:23:18 +00001602 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001603 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Jim Grosbachb1320cb2012-01-20 20:02:39 +00001604 if (!TArgs.size()) {
1605 TokError("template argument provided to non-template class");
1606 return std::vector<Init*>();
1607 }
David Greenee1b46912009-06-08 20:23:18 +00001608 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greened9746fe2011-09-19 18:26:07 +00001609 if (!RV) {
1610 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1611 << ")\n";
1612 }
David Greenee1b46912009-06-08 20:23:18 +00001613 assert(RV && "Template argument record not found??");
1614 ItemType = RV->getType();
1615 ++ArgN;
1616 }
1617 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001618 if (Result.back() == 0) return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001619
Chris Lattnerf4601652007-11-22 20:49:04 +00001620 while (Lex.getCode() == tgtok::comma) {
1621 Lex.Lex(); // Eat the comma
Bob Wilson21870412009-11-22 04:24:42 +00001622
David Greenee1b46912009-06-08 20:23:18 +00001623 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001624 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greene67acdf22009-06-29 19:59:52 +00001625 if (ArgN >= TArgs.size()) {
1626 TokError("too many template arguments");
David Greene05bce0b2011-07-29 22:43:06 +00001627 return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001628 }
David Greenee1b46912009-06-08 20:23:18 +00001629 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1630 assert(RV && "Template argument record not found??");
1631 ItemType = RV->getType();
1632 ++ArgN;
1633 }
1634 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001635 if (Result.back() == 0) return std::vector<Init*>();
Chris Lattnerf4601652007-11-22 20:49:04 +00001636 }
Bob Wilson21870412009-11-22 04:24:42 +00001637
Chris Lattnerf4601652007-11-22 20:49:04 +00001638 return Result;
1639}
1640
1641
Chris Lattnerf4601652007-11-22 20:49:04 +00001642/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1643/// empty string on error. This can happen in a number of different context's,
1644/// including within a def or in the template args for a def (which which case
1645/// CurRec will be non-null) and within the template args for a multiclass (in
1646/// which case CurRec will be null, but CurMultiClass will be set). This can
1647/// also happen within a def that is within a multiclass, which will set both
1648/// CurRec and CurMultiClass.
1649///
1650/// Declaration ::= FIELD? Type ID ('=' Value)?
1651///
David Greenee22b3212011-10-19 13:02:42 +00001652Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4601652007-11-22 20:49:04 +00001653 bool ParsingTemplateArgs) {
1654 // Read the field prefix if present.
1655 bool HasField = Lex.getCode() == tgtok::Field;
1656 if (HasField) Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001657
Chris Lattnerf4601652007-11-22 20:49:04 +00001658 RecTy *Type = ParseType();
David Greenee22b3212011-10-19 13:02:42 +00001659 if (Type == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001660
Chris Lattnerf4601652007-11-22 20:49:04 +00001661 if (Lex.getCode() != tgtok::Id) {
1662 TokError("Expected identifier in declaration");
David Greenee22b3212011-10-19 13:02:42 +00001663 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001664 }
Bob Wilson21870412009-11-22 04:24:42 +00001665
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001666 SMLoc IdLoc = Lex.getLoc();
David Greenee22b3212011-10-19 13:02:42 +00001667 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001668 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001669
Chris Lattnerf4601652007-11-22 20:49:04 +00001670 if (ParsingTemplateArgs) {
1671 if (CurRec) {
David Greenee22b3212011-10-19 13:02:42 +00001672 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4601652007-11-22 20:49:04 +00001673 } else {
1674 assert(CurMultiClass);
1675 }
1676 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +00001677 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1678 "::");
Chris Lattnerf4601652007-11-22 20:49:04 +00001679 }
Bob Wilson21870412009-11-22 04:24:42 +00001680
Chris Lattnerf4601652007-11-22 20:49:04 +00001681 // Add the value.
1682 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
David Greenee22b3212011-10-19 13:02:42 +00001683 return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001684
Chris Lattnerf4601652007-11-22 20:49:04 +00001685 // If a value is present, parse it.
1686 if (Lex.getCode() == tgtok::equal) {
1687 Lex.Lex();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001688 SMLoc ValLoc = Lex.getLoc();
David Greene05bce0b2011-07-29 22:43:06 +00001689 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001690 if (Val == 0 ||
1691 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
David Greenee22b3212011-10-19 13:02:42 +00001692 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001693 }
Bob Wilson21870412009-11-22 04:24:42 +00001694
Chris Lattnerf4601652007-11-22 20:49:04 +00001695 return DeclName;
1696}
1697
David Greenecebb4ee2012-02-22 16:09:41 +00001698/// ParseForeachDeclaration - Read a foreach declaration, returning
1699/// the name of the declared object or a NULL Init on error. Return
1700/// the name of the parsed initializer list through ForeachListName.
1701///
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001702/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1703/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1704/// ForeachDeclaration ::= ID '=' RangePiece
David Greenecebb4ee2012-02-22 16:09:41 +00001705///
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00001706VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenecebb4ee2012-02-22 16:09:41 +00001707 if (Lex.getCode() != tgtok::Id) {
1708 TokError("Expected identifier in foreach declaration");
1709 return 0;
1710 }
1711
1712 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1713 Lex.Lex();
1714
1715 // If a value is present, parse it.
1716 if (Lex.getCode() != tgtok::equal) {
1717 TokError("Expected '=' in foreach declaration");
1718 return 0;
1719 }
1720 Lex.Lex(); // Eat the '='
1721
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001722 RecTy *IterType = 0;
1723 std::vector<unsigned> Ranges;
David Greenecebb4ee2012-02-22 16:09:41 +00001724
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001725 switch (Lex.getCode()) {
1726 default: TokError("Unknown token when expecting a range list"); return 0;
1727 case tgtok::l_square: { // '[' ValueList ']'
1728 Init *List = ParseSimpleValue(0, 0, ParseForeachMode);
Sean Silva6cfc8062012-10-10 20:24:43 +00001729 ForeachListValue = dyn_cast<ListInit>(List);
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001730 if (ForeachListValue == 0) {
1731 TokError("Expected a Value list");
1732 return 0;
1733 }
1734 RecTy *ValueType = ForeachListValue->getType();
Sean Silva736ceac2012-10-05 03:31:58 +00001735 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001736 if (ListType == 0) {
1737 TokError("Value list is not of list type");
1738 return 0;
1739 }
1740 IterType = ListType->getElementType();
1741 break;
David Greenecebb4ee2012-02-22 16:09:41 +00001742 }
1743
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001744 case tgtok::IntVal: { // RangePiece.
1745 if (ParseRangePiece(Ranges))
1746 return 0;
1747 break;
David Greenecebb4ee2012-02-22 16:09:41 +00001748 }
1749
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001750 case tgtok::l_brace: { // '{' RangeList '}'
1751 Lex.Lex(); // eat the '{'
1752 Ranges = ParseRangeList();
1753 if (Lex.getCode() != tgtok::r_brace) {
1754 TokError("expected '}' at end of bit range list");
1755 return 0;
1756 }
1757 Lex.Lex();
1758 break;
1759 }
1760 }
David Greenecebb4ee2012-02-22 16:09:41 +00001761
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001762 if (!Ranges.empty()) {
1763 assert(!IterType && "Type already initialized?");
1764 IterType = IntRecTy::get();
1765 std::vector<Init*> Values;
1766 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
1767 Values.push_back(IntInit::get(Ranges[i]));
1768 ForeachListValue = ListInit::get(Values, IterType);
1769 }
1770
1771 if (!IterType)
1772 return 0;
1773
1774 return VarInit::get(DeclName, IterType);
David Greenecebb4ee2012-02-22 16:09:41 +00001775}
1776
Chris Lattnerf4601652007-11-22 20:49:04 +00001777/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1778/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1779/// template args for a def, which may or may not be in a multiclass. If null,
1780/// these are the template args for a multiclass.
1781///
1782/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson21870412009-11-22 04:24:42 +00001783///
Chris Lattnerf4601652007-11-22 20:49:04 +00001784bool TGParser::ParseTemplateArgList(Record *CurRec) {
1785 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1786 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +00001787
Chris Lattnerf4601652007-11-22 20:49:04 +00001788 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +00001789
Chris Lattnerf4601652007-11-22 20:49:04 +00001790 // Read the first declaration.
David Greenee22b3212011-10-19 13:02:42 +00001791 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1792 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001793 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001794
Chris Lattnerf4601652007-11-22 20:49:04 +00001795 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson21870412009-11-22 04:24:42 +00001796
Chris Lattnerf4601652007-11-22 20:49:04 +00001797 while (Lex.getCode() == tgtok::comma) {
1798 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001799
Chris Lattnerf4601652007-11-22 20:49:04 +00001800 // Read the following declarations.
1801 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
David Greenee22b3212011-10-19 13:02:42 +00001802 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001803 return true;
1804 TheRecToAddTo->addTemplateArg(TemplArg);
1805 }
Bob Wilson21870412009-11-22 04:24:42 +00001806
Chris Lattnerf4601652007-11-22 20:49:04 +00001807 if (Lex.getCode() != tgtok::greater)
1808 return TokError("expected '>' at end of template argument list");
1809 Lex.Lex(); // eat the '>'.
1810 return false;
1811}
1812
1813
1814/// ParseBodyItem - Parse a single item at within the body of a def or class.
1815///
1816/// BodyItem ::= Declaration ';'
1817/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1818bool TGParser::ParseBodyItem(Record *CurRec) {
1819 if (Lex.getCode() != tgtok::Let) {
David Greenee22b3212011-10-19 13:02:42 +00001820 if (ParseDeclaration(CurRec, false) == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001821 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001822
Chris Lattnerf4601652007-11-22 20:49:04 +00001823 if (Lex.getCode() != tgtok::semi)
1824 return TokError("expected ';' after declaration");
1825 Lex.Lex();
1826 return false;
1827 }
1828
1829 // LET ID OptionalRangeList '=' Value ';'
1830 if (Lex.Lex() != tgtok::Id)
1831 return TokError("expected field identifier after let");
Bob Wilson21870412009-11-22 04:24:42 +00001832
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001833 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001834 std::string FieldName = Lex.getCurStrVal();
1835 Lex.Lex(); // eat the field name.
Bob Wilson21870412009-11-22 04:24:42 +00001836
Chris Lattnerf4601652007-11-22 20:49:04 +00001837 std::vector<unsigned> BitList;
Bob Wilson21870412009-11-22 04:24:42 +00001838 if (ParseOptionalBitList(BitList))
Chris Lattnerf4601652007-11-22 20:49:04 +00001839 return true;
1840 std::reverse(BitList.begin(), BitList.end());
Bob Wilson21870412009-11-22 04:24:42 +00001841
Chris Lattnerf4601652007-11-22 20:49:04 +00001842 if (Lex.getCode() != tgtok::equal)
1843 return TokError("expected '=' in let expression");
1844 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001845
David Greenee1b46912009-06-08 20:23:18 +00001846 RecordVal *Field = CurRec->getValue(FieldName);
1847 if (Field == 0)
1848 return TokError("Value '" + FieldName + "' unknown!");
1849
1850 RecTy *Type = Field->getType();
Bob Wilson21870412009-11-22 04:24:42 +00001851
David Greene05bce0b2011-07-29 22:43:06 +00001852 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001853 if (Val == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001854
Chris Lattnerf4601652007-11-22 20:49:04 +00001855 if (Lex.getCode() != tgtok::semi)
1856 return TokError("expected ';' after let expression");
1857 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001858
Chris Lattnerf4601652007-11-22 20:49:04 +00001859 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1860}
1861
1862/// ParseBody - Read the body of a class or def. Return true on error, false on
1863/// success.
1864///
1865/// Body ::= ';'
1866/// Body ::= '{' BodyList '}'
1867/// BodyList BodyItem*
1868///
1869bool TGParser::ParseBody(Record *CurRec) {
1870 // If this is a null definition, just eat the semi and return.
1871 if (Lex.getCode() == tgtok::semi) {
1872 Lex.Lex();
1873 return false;
1874 }
Bob Wilson21870412009-11-22 04:24:42 +00001875
Chris Lattnerf4601652007-11-22 20:49:04 +00001876 if (Lex.getCode() != tgtok::l_brace)
1877 return TokError("Expected ';' or '{' to start body");
1878 // Eat the '{'.
1879 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001880
Chris Lattnerf4601652007-11-22 20:49:04 +00001881 while (Lex.getCode() != tgtok::r_brace)
1882 if (ParseBodyItem(CurRec))
1883 return true;
1884
1885 // Eat the '}'.
1886 Lex.Lex();
1887 return false;
1888}
1889
Sean Silva9cceede2013-01-09 04:49:14 +00001890/// \brief Apply the current let bindings to \a CurRec.
1891/// \returns true on error, false otherwise.
1892bool TGParser::ApplyLetStack(Record *CurRec) {
1893 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1894 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1895 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1896 LetStack[i][j].Bits, LetStack[i][j].Value))
1897 return true;
1898 return false;
1899}
1900
Chris Lattnerf4601652007-11-22 20:49:04 +00001901/// ParseObjectBody - Parse the body of a def or class. This consists of an
1902/// optional ClassList followed by a Body. CurRec is the current def or class
1903/// that is being parsed.
1904///
1905/// ObjectBody ::= BaseClassList Body
1906/// BaseClassList ::= /*empty*/
1907/// BaseClassList ::= ':' BaseClassListNE
1908/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1909///
1910bool TGParser::ParseObjectBody(Record *CurRec) {
1911 // If there is a baseclass list, read it.
1912 if (Lex.getCode() == tgtok::colon) {
1913 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001914
Chris Lattnerf4601652007-11-22 20:49:04 +00001915 // Read all of the subclasses.
1916 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1917 while (1) {
1918 // Check for error.
1919 if (SubClass.Rec == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001920
Chris Lattnerf4601652007-11-22 20:49:04 +00001921 // Add it.
1922 if (AddSubClass(CurRec, SubClass))
1923 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001924
Chris Lattnerf4601652007-11-22 20:49:04 +00001925 if (Lex.getCode() != tgtok::comma) break;
1926 Lex.Lex(); // eat ','.
1927 SubClass = ParseSubClassReference(CurRec, false);
1928 }
1929 }
1930
Sean Silva9cceede2013-01-09 04:49:14 +00001931 if (ApplyLetStack(CurRec))
1932 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001933
Chris Lattnerf4601652007-11-22 20:49:04 +00001934 return ParseBody(CurRec);
1935}
1936
Chris Lattnerf4601652007-11-22 20:49:04 +00001937/// ParseDef - Parse and return a top level or multiclass def, return the record
1938/// corresponding to it. This returns null on error.
1939///
1940/// DefInst ::= DEF ObjectName ObjectBody
1941///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001942bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001943 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001944 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson21870412009-11-22 04:24:42 +00001945 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4601652007-11-22 20:49:04 +00001946
1947 // Parse ObjectName and make a record for it.
Jordan Rosed1220092013-01-10 18:50:05 +00001948 Record *CurRec;
1949 Init *Name = ParseObjectName(CurMultiClass);
1950 if (Name)
1951 CurRec = new Record(Name, DefLoc, Records);
1952 else
1953 CurRec = new Record(GetNewAnonymousName(), DefLoc, Records,
1954 /*IsAnonymous=*/true);
Bob Wilson21870412009-11-22 04:24:42 +00001955
Jakob Stoklund Olesen72cba6c2012-05-24 22:17:36 +00001956 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001957 // Top-level def definition.
Bob Wilson21870412009-11-22 04:24:42 +00001958
Chris Lattnerf4601652007-11-22 20:49:04 +00001959 // Ensure redefinition doesn't happen.
David Greene1d501392012-01-28 00:03:24 +00001960 if (Records.getDef(CurRec->getNameInitAsString())) {
David Greene2c49fbb2011-10-19 13:03:45 +00001961 Error(DefLoc, "def '" + CurRec->getNameInitAsString()
1962 + "' already defined");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001963 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001964 }
1965 Records.addDef(CurRec);
Jakob Stoklund Olesen72cba6c2012-05-24 22:17:36 +00001966 } else if (CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001967 // Otherwise, a def inside a multiclass, add it to the multiclass.
1968 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene91919cd2011-10-19 13:03:51 +00001969 if (CurMultiClass->DefPrototypes[i]->getNameInit()
1970 == CurRec->getNameInit()) {
1971 Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
Chris Lattnerf4601652007-11-22 20:49:04 +00001972 "' already defined in this multiclass!");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001973 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001974 }
1975 CurMultiClass->DefPrototypes.push_back(CurRec);
1976 }
Bob Wilson21870412009-11-22 04:24:42 +00001977
Chris Lattnerf4601652007-11-22 20:49:04 +00001978 if (ParseObjectBody(CurRec))
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001979 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001980
Chris Lattnerf4601652007-11-22 20:49:04 +00001981 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
David Greene0d886402011-08-10 18:27:46 +00001982 // See Record::setName(). This resolve step will see any new name
1983 // for the def that might have been created when resolving
1984 // inheritance, values and arguments above.
Chris Lattnerf4601652007-11-22 20:49:04 +00001985 CurRec->resolveReferences();
Bob Wilson21870412009-11-22 04:24:42 +00001986
Chris Lattnerf4601652007-11-22 20:49:04 +00001987 // If ObjectBody has template arguments, it's an error.
1988 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001989
1990 if (CurMultiClass) {
1991 // Copy the template arguments for the multiclass into the def.
David Greenee22b3212011-10-19 13:02:42 +00001992 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001993 CurMultiClass->Rec.getTemplateArgs();
1994
1995 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1996 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1997 assert(RV && "Template arg doesn't exist?");
1998 CurRec->addValue(*RV);
1999 }
2000 }
2001
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00002002 if (ProcessForeachDefs(CurRec, DefLoc)) {
David Greenecebb4ee2012-02-22 16:09:41 +00002003 Error(DefLoc,
2004 "Could not process loops for def" + CurRec->getNameInitAsString());
2005 return true;
2006 }
2007
2008 return false;
2009}
2010
2011/// ParseForeach - Parse a for statement. Return the record corresponding
2012/// to it. This returns true on error.
2013///
2014/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2015/// Foreach ::= FOREACH Declaration IN Object
2016///
2017bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2018 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2019 Lex.Lex(); // Eat the 'for' token.
2020
2021 // Make a temporary object to record items associated with the for
2022 // loop.
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00002023 ListInit *ListValue = 0;
2024 VarInit *IterName = ParseForeachDeclaration(ListValue);
David Greenecebb4ee2012-02-22 16:09:41 +00002025 if (IterName == 0)
2026 return TokError("expected declaration in for");
2027
2028 if (Lex.getCode() != tgtok::In)
2029 return TokError("Unknown tok");
2030 Lex.Lex(); // Eat the in
2031
2032 // Create a loop object and remember it.
2033 Loops.push_back(ForeachLoop(IterName, ListValue));
2034
2035 if (Lex.getCode() != tgtok::l_brace) {
2036 // FOREACH Declaration IN Object
2037 if (ParseObject(CurMultiClass))
2038 return true;
2039 }
2040 else {
2041 SMLoc BraceLoc = Lex.getLoc();
2042 // Otherwise, this is a group foreach.
2043 Lex.Lex(); // eat the '{'.
2044
2045 // Parse the object list.
2046 if (ParseObjectList(CurMultiClass))
2047 return true;
2048
2049 if (Lex.getCode() != tgtok::r_brace) {
2050 TokError("expected '}' at end of foreach command");
2051 return Error(BraceLoc, "to match this '{'");
2052 }
2053 Lex.Lex(); // Eat the }
2054 }
2055
2056 // We've processed everything in this loop.
2057 Loops.pop_back();
2058
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002059 return false;
Chris Lattnerf4601652007-11-22 20:49:04 +00002060}
2061
Chris Lattnerf4601652007-11-22 20:49:04 +00002062/// ParseClass - Parse a tblgen class definition.
2063///
2064/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2065///
2066bool TGParser::ParseClass() {
2067 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2068 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002069
Chris Lattnerf4601652007-11-22 20:49:04 +00002070 if (Lex.getCode() != tgtok::Id)
2071 return TokError("expected class name after 'class' keyword");
Bob Wilson21870412009-11-22 04:24:42 +00002072
Chris Lattnerf4601652007-11-22 20:49:04 +00002073 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2074 if (CurRec) {
2075 // If the body was previously defined, this is an error.
David Greenee3385652011-10-19 13:04:13 +00002076 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4601652007-11-22 20:49:04 +00002077 !CurRec->getSuperClasses().empty() ||
2078 !CurRec->getTemplateArgs().empty())
David Greene69a23942011-10-19 13:03:58 +00002079 return TokError("Class '" + CurRec->getNameInitAsString()
2080 + "' already defined");
Chris Lattnerf4601652007-11-22 20:49:04 +00002081 } else {
2082 // If this is the first reference to this class, create and add it.
Chris Lattner9c6b60e2010-12-15 04:48:22 +00002083 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00002084 Records.addClass(CurRec);
2085 }
2086 Lex.Lex(); // eat the name.
Bob Wilson21870412009-11-22 04:24:42 +00002087
Chris Lattnerf4601652007-11-22 20:49:04 +00002088 // If there are template args, parse them.
2089 if (Lex.getCode() == tgtok::less)
2090 if (ParseTemplateArgList(CurRec))
2091 return true;
2092
2093 // Finally, parse the object body.
2094 return ParseObjectBody(CurRec);
2095}
2096
2097/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2098/// of LetRecords.
2099///
2100/// LetList ::= LetItem (',' LetItem)*
2101/// LetItem ::= ID OptionalRangeList '=' Value
2102///
2103std::vector<LetRecord> TGParser::ParseLetList() {
2104 std::vector<LetRecord> Result;
Bob Wilson21870412009-11-22 04:24:42 +00002105
Chris Lattnerf4601652007-11-22 20:49:04 +00002106 while (1) {
2107 if (Lex.getCode() != tgtok::Id) {
2108 TokError("expected identifier in let definition");
2109 return std::vector<LetRecord>();
2110 }
2111 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002112 SMLoc NameLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +00002113 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4601652007-11-22 20:49:04 +00002114
2115 // Check for an optional RangeList.
2116 std::vector<unsigned> Bits;
Bob Wilson21870412009-11-22 04:24:42 +00002117 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4601652007-11-22 20:49:04 +00002118 return std::vector<LetRecord>();
2119 std::reverse(Bits.begin(), Bits.end());
Bob Wilson21870412009-11-22 04:24:42 +00002120
Chris Lattnerf4601652007-11-22 20:49:04 +00002121 if (Lex.getCode() != tgtok::equal) {
2122 TokError("expected '=' in let expression");
2123 return std::vector<LetRecord>();
2124 }
2125 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00002126
David Greene05bce0b2011-07-29 22:43:06 +00002127 Init *Val = ParseValue(0);
Chris Lattnerf4601652007-11-22 20:49:04 +00002128 if (Val == 0) return std::vector<LetRecord>();
Bob Wilson21870412009-11-22 04:24:42 +00002129
Chris Lattnerf4601652007-11-22 20:49:04 +00002130 // Now that we have everything, add the record.
2131 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson21870412009-11-22 04:24:42 +00002132
Chris Lattnerf4601652007-11-22 20:49:04 +00002133 if (Lex.getCode() != tgtok::comma)
2134 return Result;
Bob Wilson21870412009-11-22 04:24:42 +00002135 Lex.Lex(); // eat the comma.
Chris Lattnerf4601652007-11-22 20:49:04 +00002136 }
2137}
2138
2139/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002140/// different related productions. This works inside multiclasses too.
Chris Lattnerf4601652007-11-22 20:49:04 +00002141///
2142/// Object ::= LET LetList IN '{' ObjectList '}'
2143/// Object ::= LET LetList IN Object
2144///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002145bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002146 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2147 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002148
Chris Lattnerf4601652007-11-22 20:49:04 +00002149 // Add this entry to the let stack.
2150 std::vector<LetRecord> LetInfo = ParseLetList();
2151 if (LetInfo.empty()) return true;
2152 LetStack.push_back(LetInfo);
2153
2154 if (Lex.getCode() != tgtok::In)
2155 return TokError("expected 'in' at end of top-level 'let'");
2156 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002157
Chris Lattnerf4601652007-11-22 20:49:04 +00002158 // If this is a scalar let, just handle it now
2159 if (Lex.getCode() != tgtok::l_brace) {
2160 // LET LetList IN Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002161 if (ParseObject(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00002162 return true;
2163 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002164 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002165 // Otherwise, this is a group let.
2166 Lex.Lex(); // eat the '{'.
Bob Wilson21870412009-11-22 04:24:42 +00002167
Chris Lattnerf4601652007-11-22 20:49:04 +00002168 // Parse the object list.
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002169 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00002170 return true;
Bob Wilson21870412009-11-22 04:24:42 +00002171
Chris Lattnerf4601652007-11-22 20:49:04 +00002172 if (Lex.getCode() != tgtok::r_brace) {
2173 TokError("expected '}' at end of top level let command");
2174 return Error(BraceLoc, "to match this '{'");
2175 }
2176 Lex.Lex();
2177 }
Bob Wilson21870412009-11-22 04:24:42 +00002178
Chris Lattnerf4601652007-11-22 20:49:04 +00002179 // Outside this let scope, this let block is not active.
2180 LetStack.pop_back();
2181 return false;
2182}
2183
Chris Lattnerf4601652007-11-22 20:49:04 +00002184/// ParseMultiClass - Parse a multiclass definition.
2185///
Bob Wilson32558652009-04-28 19:41:44 +00002186/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silva9302dcc2013-01-09 02:11:55 +00002187/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2188/// MultiClassObject ::= DefInst
2189/// MultiClassObject ::= MultiClassInst
2190/// MultiClassObject ::= DefMInst
2191/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2192/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4601652007-11-22 20:49:04 +00002193///
2194bool TGParser::ParseMultiClass() {
2195 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2196 Lex.Lex(); // Eat the multiclass token.
2197
2198 if (Lex.getCode() != tgtok::Id)
2199 return TokError("expected identifier after multiclass for name");
2200 std::string Name = Lex.getCurStrVal();
Bob Wilson21870412009-11-22 04:24:42 +00002201
Chris Lattnerf4601652007-11-22 20:49:04 +00002202 if (MultiClasses.count(Name))
2203 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson21870412009-11-22 04:24:42 +00002204
Chris Lattner67db8832010-12-13 00:23:57 +00002205 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
2206 Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00002207 Lex.Lex(); // Eat the identifier.
Bob Wilson21870412009-11-22 04:24:42 +00002208
Chris Lattnerf4601652007-11-22 20:49:04 +00002209 // If there are template args, parse them.
2210 if (Lex.getCode() == tgtok::less)
2211 if (ParseTemplateArgList(0))
2212 return true;
2213
David Greened34a73b2009-04-24 16:55:41 +00002214 bool inherits = false;
2215
David Greenede444af2009-04-22 16:42:54 +00002216 // If there are submulticlasses, parse them.
2217 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00002218 inherits = true;
2219
David Greenede444af2009-04-22 16:42:54 +00002220 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00002221
David Greenede444af2009-04-22 16:42:54 +00002222 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00002223 SubMultiClassReference SubMultiClass =
2224 ParseSubMultiClassReference(CurMultiClass);
David Greenede444af2009-04-22 16:42:54 +00002225 while (1) {
2226 // Check for error.
2227 if (SubMultiClass.MC == 0) return true;
Bob Wilson32558652009-04-28 19:41:44 +00002228
David Greenede444af2009-04-22 16:42:54 +00002229 // Add it.
2230 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2231 return true;
Bob Wilson32558652009-04-28 19:41:44 +00002232
David Greenede444af2009-04-22 16:42:54 +00002233 if (Lex.getCode() != tgtok::comma) break;
2234 Lex.Lex(); // eat ','.
2235 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2236 }
2237 }
2238
David Greened34a73b2009-04-24 16:55:41 +00002239 if (Lex.getCode() != tgtok::l_brace) {
2240 if (!inherits)
2241 return TokError("expected '{' in multiclass definition");
Bob Wilson21870412009-11-22 04:24:42 +00002242 else if (Lex.getCode() != tgtok::semi)
2243 return TokError("expected ';' in multiclass definition");
David Greened34a73b2009-04-24 16:55:41 +00002244 else
Bob Wilson21870412009-11-22 04:24:42 +00002245 Lex.Lex(); // eat the ';'.
2246 } else {
David Greened34a73b2009-04-24 16:55:41 +00002247 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2248 return TokError("multiclass must contain at least one def");
Bob Wilson21870412009-11-22 04:24:42 +00002249
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002250 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002251 switch (Lex.getCode()) {
2252 default:
David Greenea1b1b792011-10-07 18:25:05 +00002253 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002254 case tgtok::Let:
2255 case tgtok::Def:
2256 case tgtok::Defm:
David Greenecebb4ee2012-02-22 16:09:41 +00002257 case tgtok::Foreach:
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002258 if (ParseObject(CurMultiClass))
2259 return true;
2260 break;
2261 }
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002262 }
David Greened34a73b2009-04-24 16:55:41 +00002263 Lex.Lex(); // eat the '}'.
2264 }
Bob Wilson21870412009-11-22 04:24:42 +00002265
Chris Lattnerf4601652007-11-22 20:49:04 +00002266 CurMultiClass = 0;
2267 return false;
2268}
2269
David Greenee499a2d2011-10-05 22:42:07 +00002270Record *TGParser::
2271InstantiateMulticlassDef(MultiClass &MC,
2272 Record *DefProto,
David Greene7be867e2011-10-19 13:04:31 +00002273 Init *DefmPrefix,
Jordan Roseb50df4a2013-01-10 18:50:11 +00002274 SMRange DefmPrefixRange) {
David Greene7be867e2011-10-19 13:04:31 +00002275 // We need to preserve DefProto so it can be reused for later
2276 // instantiations, so create a new Record to inherit from it.
2277
David Greenee499a2d2011-10-05 22:42:07 +00002278 // Add in the defm name. If the defm prefix is empty, give each
2279 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2280 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2281 // as a prefix.
David Greenee499a2d2011-10-05 22:42:07 +00002282
Jordan Rosed1220092013-01-10 18:50:05 +00002283 bool IsAnonymous = false;
2284 if (DefmPrefix == 0) {
David Greene7be867e2011-10-19 13:04:31 +00002285 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Rosed1220092013-01-10 18:50:05 +00002286 IsAnonymous = true;
2287 }
David Greene7be867e2011-10-19 13:04:31 +00002288
2289 Init *DefName = DefProto->getNameInit();
2290
Sean Silva6cfc8062012-10-10 20:24:43 +00002291 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene7be867e2011-10-19 13:04:31 +00002292
David Greened3d1cad2011-10-19 13:04:43 +00002293 if (DefNameString != 0) {
2294 // We have a fully expanded string so there are no operators to
2295 // resolve. We should concatenate the given prefix and name.
David Greene7be867e2011-10-19 13:04:31 +00002296 DefName =
2297 BinOpInit::get(BinOpInit::STRCONCAT,
2298 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2299 StringRecTy::get())->Fold(DefProto, &MC),
2300 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2301 }
David Greene7be867e2011-10-19 13:04:31 +00002302
Jakob Stoklund Olesen376a8a72012-08-22 23:33:58 +00002303 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Roseb50df4a2013-01-10 18:50:11 +00002304 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesen376a8a72012-08-22 23:33:58 +00002305 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Jordan Rosed1220092013-01-10 18:50:05 +00002306 Record *CurRec = new Record(DefName, Locs, Records, IsAnonymous);
David Greenee499a2d2011-10-05 22:42:07 +00002307
2308 SubClassReference Ref;
Jordan Roseb50df4a2013-01-10 18:50:11 +00002309 Ref.RefRange = DefmPrefixRange;
David Greenee499a2d2011-10-05 22:42:07 +00002310 Ref.Rec = DefProto;
2311 AddSubClass(CurRec, Ref);
2312
Jim Grosbachcfbda4a2012-08-02 18:46:42 +00002313 // Set the value for NAME. We don't resolve references to it 'til later,
2314 // though, so that uses in nested multiclass names don't get
2315 // confused.
Jordan Roseb50df4a2013-01-10 18:50:11 +00002316 if (SetValue(CurRec, Ref.RefRange.Start, "NAME", std::vector<unsigned>(),
Jim Grosbachcfbda4a2012-08-02 18:46:42 +00002317 DefmPrefix)) {
Jordan Roseb50df4a2013-01-10 18:50:11 +00002318 Error(DefmPrefixRange.Start, "Could not resolve "
Jim Grosbachcfbda4a2012-08-02 18:46:42 +00002319 + CurRec->getNameInitAsString() + ":NAME to '"
2320 + DefmPrefix->getAsUnquotedString() + "'");
2321 return 0;
2322 }
David Greenee5b252f2011-10-19 13:04:35 +00002323
Jim Grosbachcfbda4a2012-08-02 18:46:42 +00002324 // If the DefNameString didn't resolve, we probably have a reference to
2325 // NAME and need to replace it. We need to do at least this much greedily,
2326 // otherwise nested multiclasses will end up with incorrect NAME expansions.
2327 if (DefNameString == 0) {
David Greenee5b252f2011-10-19 13:04:35 +00002328 RecordVal *DefNameRV = CurRec->getValue("NAME");
2329 CurRec->resolveReferencesTo(DefNameRV);
2330 }
2331
2332 if (!CurMultiClass) {
Jim Grosbachcfbda4a2012-08-02 18:46:42 +00002333 // Now that we're at the top level, resolve all NAME references
2334 // in the resultant defs that weren't in the def names themselves.
2335 RecordVal *DefNameRV = CurRec->getValue("NAME");
2336 CurRec->resolveReferencesTo(DefNameRV);
2337
2338 // Now that NAME references are resolved and we're at the top level of
2339 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greenee5b252f2011-10-19 13:04:35 +00002340 // currently in a multiclass, it means this defm appears inside a
2341 // multiclass and its name won't be fully resolvable until we see
2342 // the top-level defm. Therefore, we don't add this to the
2343 // RecordKeeper at this point. If we did we could get duplicate
2344 // defs as more than one probably refers to NAME or some other
2345 // common internal placeholder.
2346
2347 // Ensure redefinition doesn't happen.
2348 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Roseb50df4a2013-01-10 18:50:11 +00002349 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greenee5b252f2011-10-19 13:04:35 +00002350 "' already defined, instantiating defm with subdef '" +
2351 DefProto->getNameInitAsString() + "'");
2352 return 0;
2353 }
2354
2355 Records.addDef(CurRec);
2356 }
2357
David Greenee499a2d2011-10-05 22:42:07 +00002358 return CurRec;
2359}
2360
2361bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2362 Record *CurRec,
2363 SMLoc DefmPrefixLoc,
2364 SMLoc SubClassLoc,
David Greenee22b3212011-10-19 13:02:42 +00002365 const std::vector<Init *> &TArgs,
David Greenee499a2d2011-10-05 22:42:07 +00002366 std::vector<Init *> &TemplateVals,
2367 bool DeleteArgs) {
2368 // Loop over all of the template arguments, setting them to the specified
2369 // value or leaving them as the default if necessary.
2370 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2371 // Check if a value is specified for this temp-arg.
2372 if (i < TemplateVals.size()) {
2373 // Set it now.
2374 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2375 TemplateVals[i]))
2376 return true;
2377
2378 // Resolve it next.
2379 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2380
2381 if (DeleteArgs)
2382 // Now remove it.
2383 CurRec->removeValue(TArgs[i]);
2384
2385 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2386 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenee22b3212011-10-19 13:02:42 +00002387 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2388 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2389 + "'");
David Greenee499a2d2011-10-05 22:42:07 +00002390 }
2391 }
2392 return false;
2393}
2394
2395bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2396 Record *CurRec,
2397 Record *DefProto,
2398 SMLoc DefmPrefixLoc) {
2399 // If the mdef is inside a 'let' expression, add to each def.
Sean Silva9cceede2013-01-09 04:49:14 +00002400 if (ApplyLetStack(CurRec))
2401 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenee499a2d2011-10-05 22:42:07 +00002402
David Greenee499a2d2011-10-05 22:42:07 +00002403 // Don't create a top level definition for defm inside multiclasses,
2404 // instead, only update the prototypes and bind the template args
2405 // with the new created definition.
Sean Silva699b8702013-01-09 05:28:12 +00002406 if (!CurMultiClass)
2407 return false;
2408 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2409 i != e; ++i)
2410 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2411 == CurRec->getNameInit())
2412 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2413 "' already defined in this multiclass!");
2414 CurMultiClass->DefPrototypes.push_back(CurRec);
David Greenee499a2d2011-10-05 22:42:07 +00002415
Sean Silva699b8702013-01-09 05:28:12 +00002416 // Copy the template arguments for the multiclass into the new def.
2417 const std::vector<Init *> &TA =
2418 CurMultiClass->Rec.getTemplateArgs();
David Greenee499a2d2011-10-05 22:42:07 +00002419
Sean Silva699b8702013-01-09 05:28:12 +00002420 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2421 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2422 assert(RV && "Template arg doesn't exist?");
2423 CurRec->addValue(*RV);
David Greenee499a2d2011-10-05 22:42:07 +00002424 }
2425
2426 return false;
2427}
2428
Chris Lattnerf4601652007-11-22 20:49:04 +00002429/// ParseDefm - Parse the instantiation of a multiclass.
2430///
2431/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2432///
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002433bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002434 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Roseb50df4a2013-01-10 18:50:11 +00002435 SMLoc DefmLoc = Lex.getLoc();
David Greenea9e07dd2011-10-19 13:04:29 +00002436 Init *DefmPrefix = 0;
David Greenea9e07dd2011-10-19 13:04:29 +00002437
Craig Topper6a59f5a2013-01-07 05:09:33 +00002438 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greenea9e07dd2011-10-19 13:04:29 +00002439 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002440 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +00002441
Jordan Roseb50df4a2013-01-10 18:50:11 +00002442 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002443 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4601652007-11-22 20:49:04 +00002444 return TokError("expected ':' after defm identifier");
Bob Wilson21870412009-11-22 04:24:42 +00002445
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002446 // Keep track of the new generated record definitions.
2447 std::vector<Record*> NewRecDefs;
2448
2449 // This record also inherits from a regular class (non-multiclass)?
2450 bool InheritFromClass = false;
2451
Chris Lattnerf4601652007-11-22 20:49:04 +00002452 // eat the colon.
2453 Lex.Lex();
2454
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002455 SMLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002456 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene56546132009-04-22 22:17:51 +00002457
2458 while (1) {
2459 if (Ref.Rec == 0) return true;
2460
2461 // To instantiate a multiclass, we need to first get the multiclass, then
2462 // instantiate each def contained in the multiclass with the SubClassRef
2463 // template parameters.
2464 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2465 assert(MC && "Didn't lookup multiclass correctly?");
David Greene05bce0b2011-07-29 22:43:06 +00002466 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greene56546132009-04-22 22:17:51 +00002467
2468 // Verify that the correct number of template arguments were specified.
David Greenee22b3212011-10-19 13:02:42 +00002469 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greene56546132009-04-22 22:17:51 +00002470 if (TArgs.size() < TemplateVals.size())
2471 return Error(SubClassLoc,
2472 "more template args specified than multiclass expects");
2473
2474 // Loop over all the def's in the multiclass, instantiating each one.
2475 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2476 Record *DefProto = MC->DefPrototypes[i];
2477
Jordan Roseb50df4a2013-01-10 18:50:11 +00002478 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix,
2479 SMRange(DefmLoc,
2480 DefmPrefixEndLoc));
Jim Grosbach94f2dc92011-12-02 18:33:03 +00002481 if (!CurRec)
2482 return true;
David Greene065f2592009-05-05 16:28:25 +00002483
Jordan Roseb50df4a2013-01-10 18:50:11 +00002484 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenee499a2d2011-10-05 22:42:07 +00002485 TArgs, TemplateVals, true/*Delete args*/))
2486 return Error(SubClassLoc, "could not instantiate def");
David Greene56546132009-04-22 22:17:51 +00002487
Jordan Roseb50df4a2013-01-10 18:50:11 +00002488 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmLoc))
David Greenee499a2d2011-10-05 22:42:07 +00002489 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002490
2491 NewRecDefs.push_back(CurRec);
David Greene56546132009-04-22 22:17:51 +00002492 }
2493
David Greenee499a2d2011-10-05 22:42:07 +00002494
David Greene56546132009-04-22 22:17:51 +00002495 if (Lex.getCode() != tgtok::comma) break;
2496 Lex.Lex(); // eat ','.
2497
2498 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002499
2500 // A defm can inherit from regular classes (non-multiclass) as
2501 // long as they come in the end of the inheritance list.
2502 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0);
2503
2504 if (InheritFromClass)
2505 break;
2506
David Greene56546132009-04-22 22:17:51 +00002507 Ref = ParseSubClassReference(0, true);
2508 }
2509
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002510 if (InheritFromClass) {
2511 // Process all the classes to inherit as if they were part of a
2512 // regular 'def' and inherit all record values.
2513 SubClassReference SubClass = ParseSubClassReference(0, false);
2514 while (1) {
2515 // Check for error.
2516 if (SubClass.Rec == 0) return true;
2517
2518 // Get the expanded definition prototypes and teach them about
2519 // the record values the current class to inherit has
2520 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2521 Record *CurRec = NewRecDefs[i];
2522
2523 // Add it.
2524 if (AddSubClass(CurRec, SubClass))
2525 return true;
2526
Sean Silva9cceede2013-01-09 04:49:14 +00002527 if (ApplyLetStack(CurRec))
2528 return true;
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002529 }
2530
2531 if (Lex.getCode() != tgtok::comma) break;
2532 Lex.Lex(); // eat ','.
2533 SubClass = ParseSubClassReference(0, false);
2534 }
2535 }
2536
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002537 if (!CurMultiClass)
2538 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene0d886402011-08-10 18:27:46 +00002539 // See Record::setName(). This resolve step will see any new
2540 // name for the def that might have been created when resolving
2541 // inheritance, values and arguments above.
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002542 NewRecDefs[i]->resolveReferences();
2543
Chris Lattnerf4601652007-11-22 20:49:04 +00002544 if (Lex.getCode() != tgtok::semi)
2545 return TokError("expected ';' at end of defm");
2546 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002547
Chris Lattnerf4601652007-11-22 20:49:04 +00002548 return false;
2549}
2550
2551/// ParseObject
2552/// Object ::= ClassInst
2553/// Object ::= DefInst
2554/// Object ::= MultiClassInst
2555/// Object ::= DefMInst
2556/// Object ::= LETCommand '{' ObjectList '}'
2557/// Object ::= LETCommand Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002558bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002559 switch (Lex.getCode()) {
Chris Lattnerd6d9dd92010-10-31 19:27:15 +00002560 default:
2561 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002562 case tgtok::Let: return ParseTopLevelLet(MC);
2563 case tgtok::Def: return ParseDef(MC);
David Greenecebb4ee2012-02-22 16:09:41 +00002564 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002565 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4601652007-11-22 20:49:04 +00002566 case tgtok::Class: return ParseClass();
2567 case tgtok::MultiClass: return ParseMultiClass();
2568 }
2569}
2570
2571/// ParseObjectList
2572/// ObjectList :== Object*
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002573bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002574 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002575 if (ParseObject(MC))
Chris Lattnerf4601652007-11-22 20:49:04 +00002576 return true;
2577 }
2578 return false;
2579}
2580
Chris Lattnerf4601652007-11-22 20:49:04 +00002581bool TGParser::ParseFile() {
2582 Lex.Lex(); // Prime the lexer.
2583 if (ParseObjectList()) return true;
Bob Wilson21870412009-11-22 04:24:42 +00002584
Chris Lattnerf4601652007-11-22 20:49:04 +00002585 // If we have unread input at the end of the file, report it.
2586 if (Lex.getCode() == tgtok::Eof)
2587 return false;
Bob Wilson21870412009-11-22 04:24:42 +00002588
Chris Lattnerf4601652007-11-22 20:49:04 +00002589 return TokError("Unexpected input at top level");
2590}
2591