blob: da0086a74acde651b5a12e66664bde59755c30eb [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;
385 return "anonymous."+utostr(AnonCounter++);
386}
387
Chris Lattnerf4601652007-11-22 20:49:04 +0000388/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Rosed1220092013-01-10 18:50:05 +0000389/// return 0.
David Greenea9e07dd2011-10-19 13:04:29 +0000390/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4601652007-11-22 20:49:04 +0000391/// ObjectName ::= /*empty*/
392///
David Greenea9e07dd2011-10-19 13:04:29 +0000393Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
394 switch (Lex.getCode()) {
395 case tgtok::colon:
396 case tgtok::semi:
397 case tgtok::l_brace:
398 // These are all of the tokens that can begin an object body.
399 // Some of these can also begin values but we disallow those cases
400 // because they are unlikely to be useful.
Jordan Rosed1220092013-01-10 18:50:05 +0000401 return 0;
David Greenea9e07dd2011-10-19 13:04:29 +0000402 default:
403 break;
404 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000405
David Greenea9e07dd2011-10-19 13:04:29 +0000406 Record *CurRec = 0;
407 if (CurMultiClass)
408 CurRec = &CurMultiClass->Rec;
409
410 RecTy *Type = 0;
411 if (CurRec) {
Sean Silva3f7b7f82012-10-10 20:24:47 +0000412 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greenea9e07dd2011-10-19 13:04:29 +0000413 if (!CurRecName) {
414 TokError("Record name is not typed!");
415 return 0;
416 }
417 Type = CurRecName->getType();
418 }
419
420 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4601652007-11-22 20:49:04 +0000421}
422
Chris Lattnerf4601652007-11-22 20:49:04 +0000423/// ParseClassID - Parse and resolve a reference to a class name. This returns
424/// null on error.
425///
426/// ClassID ::= ID
427///
428Record *TGParser::ParseClassID() {
429 if (Lex.getCode() != tgtok::Id) {
430 TokError("expected name for ClassID");
431 return 0;
432 }
Bob Wilson21870412009-11-22 04:24:42 +0000433
Chris Lattnerf4601652007-11-22 20:49:04 +0000434 Record *Result = Records.getClass(Lex.getCurStrVal());
435 if (Result == 0)
436 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson21870412009-11-22 04:24:42 +0000437
Chris Lattnerf4601652007-11-22 20:49:04 +0000438 Lex.Lex();
439 return Result;
440}
441
Bob Wilson32558652009-04-28 19:41:44 +0000442/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
443/// This returns null on error.
David Greenede444af2009-04-22 16:42:54 +0000444///
445/// MultiClassID ::= ID
446///
447MultiClass *TGParser::ParseMultiClassID() {
448 if (Lex.getCode() != tgtok::Id) {
Sean Silva36febfd2013-01-09 02:11:57 +0000449 TokError("expected name for MultiClassID");
David Greenede444af2009-04-22 16:42:54 +0000450 return 0;
451 }
Bob Wilson32558652009-04-28 19:41:44 +0000452
David Greenede444af2009-04-22 16:42:54 +0000453 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
454 if (Result == 0)
Sean Silva36febfd2013-01-09 02:11:57 +0000455 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson32558652009-04-28 19:41:44 +0000456
David Greenede444af2009-04-22 16:42:54 +0000457 Lex.Lex();
458 return Result;
459}
460
Chris Lattnerf4601652007-11-22 20:49:04 +0000461/// ParseSubClassReference - Parse a reference to a subclass or to a templated
462/// subclass. This returns a SubClassRefTy with a null Record* on error.
463///
464/// SubClassRef ::= ClassID
465/// SubClassRef ::= ClassID '<' ValueList '>'
466///
467SubClassReference TGParser::
468ParseSubClassReference(Record *CurRec, bool isDefm) {
469 SubClassReference Result;
Jordan Roseb50df4a2013-01-10 18:50:11 +0000470 Result.RefRange.Start = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +0000471
Sean Silva7be90212013-01-09 02:17:14 +0000472 if (isDefm) {
473 if (MultiClass *MC = ParseMultiClassID())
474 Result.Rec = &MC->Rec;
475 } else {
Chris Lattnerf4601652007-11-22 20:49:04 +0000476 Result.Rec = ParseClassID();
Sean Silva7be90212013-01-09 02:17:14 +0000477 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000478 if (Result.Rec == 0) return Result;
Bob Wilson21870412009-11-22 04:24:42 +0000479
Chris Lattnerf4601652007-11-22 20:49:04 +0000480 // If there is no template arg list, we're done.
Jordan Roseb50df4a2013-01-10 18:50:11 +0000481 if (Lex.getCode() != tgtok::less) {
482 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000483 return Result;
Jordan Roseb50df4a2013-01-10 18:50:11 +0000484 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000485 Lex.Lex(); // Eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000486
Chris Lattnerf4601652007-11-22 20:49:04 +0000487 if (Lex.getCode() == tgtok::greater) {
488 TokError("subclass reference requires a non-empty list of template values");
489 Result.Rec = 0;
490 return Result;
491 }
Bob Wilson21870412009-11-22 04:24:42 +0000492
David Greenee1b46912009-06-08 20:23:18 +0000493 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4601652007-11-22 20:49:04 +0000494 if (Result.TemplateArgs.empty()) {
495 Result.Rec = 0; // Error parsing value list.
496 return Result;
497 }
Bob Wilson21870412009-11-22 04:24:42 +0000498
Chris Lattnerf4601652007-11-22 20:49:04 +0000499 if (Lex.getCode() != tgtok::greater) {
500 TokError("expected '>' in template value list");
501 Result.Rec = 0;
502 return Result;
503 }
504 Lex.Lex();
Jordan Roseb50df4a2013-01-10 18:50:11 +0000505 Result.RefRange.End = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +0000506
Chris Lattnerf4601652007-11-22 20:49:04 +0000507 return Result;
508}
509
Bob Wilson32558652009-04-28 19:41:44 +0000510/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
511/// templated submulticlass. This returns a SubMultiClassRefTy with a null
512/// Record* on error.
David Greenede444af2009-04-22 16:42:54 +0000513///
514/// SubMultiClassRef ::= MultiClassID
515/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
516///
517SubMultiClassReference TGParser::
518ParseSubMultiClassReference(MultiClass *CurMC) {
519 SubMultiClassReference Result;
Jordan Roseb50df4a2013-01-10 18:50:11 +0000520 Result.RefRange.Start = Lex.getLoc();
Bob Wilson32558652009-04-28 19:41:44 +0000521
David Greenede444af2009-04-22 16:42:54 +0000522 Result.MC = ParseMultiClassID();
523 if (Result.MC == 0) return Result;
Bob Wilson32558652009-04-28 19:41:44 +0000524
David Greenede444af2009-04-22 16:42:54 +0000525 // If there is no template arg list, we're done.
Jordan Roseb50df4a2013-01-10 18:50:11 +0000526 if (Lex.getCode() != tgtok::less) {
527 Result.RefRange.End = Lex.getLoc();
David Greenede444af2009-04-22 16:42:54 +0000528 return Result;
Jordan Roseb50df4a2013-01-10 18:50:11 +0000529 }
David Greenede444af2009-04-22 16:42:54 +0000530 Lex.Lex(); // Eat the '<'
Bob Wilson32558652009-04-28 19:41:44 +0000531
David Greenede444af2009-04-22 16:42:54 +0000532 if (Lex.getCode() == tgtok::greater) {
533 TokError("subclass reference requires a non-empty list of template values");
534 Result.MC = 0;
535 return Result;
536 }
Bob Wilson32558652009-04-28 19:41:44 +0000537
David Greenee1b46912009-06-08 20:23:18 +0000538 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greenede444af2009-04-22 16:42:54 +0000539 if (Result.TemplateArgs.empty()) {
540 Result.MC = 0; // Error parsing value list.
541 return Result;
542 }
Bob Wilson32558652009-04-28 19:41:44 +0000543
David Greenede444af2009-04-22 16:42:54 +0000544 if (Lex.getCode() != tgtok::greater) {
545 TokError("expected '>' in template value list");
546 Result.MC = 0;
547 return Result;
548 }
549 Lex.Lex();
Jordan Roseb50df4a2013-01-10 18:50:11 +0000550 Result.RefRange.End = Lex.getLoc();
David Greenede444af2009-04-22 16:42:54 +0000551
552 return Result;
553}
554
Chris Lattnerf4601652007-11-22 20:49:04 +0000555/// ParseRangePiece - Parse a bit/value range.
556/// RangePiece ::= INTVAL
557/// RangePiece ::= INTVAL '-' INTVAL
558/// RangePiece ::= INTVAL INTVAL
559bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner811281e2008-01-10 07:01:53 +0000560 if (Lex.getCode() != tgtok::IntVal) {
561 TokError("expected integer or bitrange");
562 return true;
563 }
Dan Gohman63f97202008-10-17 01:33:43 +0000564 int64_t Start = Lex.getCurIntVal();
565 int64_t End;
Bob Wilson21870412009-11-22 04:24:42 +0000566
Chris Lattnerf4601652007-11-22 20:49:04 +0000567 if (Start < 0)
568 return TokError("invalid range, cannot be negative");
Bob Wilson21870412009-11-22 04:24:42 +0000569
Chris Lattnerf4601652007-11-22 20:49:04 +0000570 switch (Lex.Lex()) { // eat first character.
Bob Wilson21870412009-11-22 04:24:42 +0000571 default:
Chris Lattnerf4601652007-11-22 20:49:04 +0000572 Ranges.push_back(Start);
573 return false;
574 case tgtok::minus:
575 if (Lex.Lex() != tgtok::IntVal) {
576 TokError("expected integer value as end of range");
577 return true;
578 }
579 End = Lex.getCurIntVal();
580 break;
581 case tgtok::IntVal:
582 End = -Lex.getCurIntVal();
583 break;
584 }
Bob Wilson21870412009-11-22 04:24:42 +0000585 if (End < 0)
Chris Lattnerf4601652007-11-22 20:49:04 +0000586 return TokError("invalid range, cannot be negative");
587 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000588
Chris Lattnerf4601652007-11-22 20:49:04 +0000589 // Add to the range.
590 if (Start < End) {
591 for (; Start <= End; ++Start)
592 Ranges.push_back(Start);
593 } else {
594 for (; Start >= End; --Start)
595 Ranges.push_back(Start);
596 }
597 return false;
598}
599
600/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
601///
602/// RangeList ::= RangePiece (',' RangePiece)*
603///
604std::vector<unsigned> TGParser::ParseRangeList() {
605 std::vector<unsigned> Result;
Bob Wilson21870412009-11-22 04:24:42 +0000606
Chris Lattnerf4601652007-11-22 20:49:04 +0000607 // Parse the first piece.
608 if (ParseRangePiece(Result))
609 return std::vector<unsigned>();
610 while (Lex.getCode() == tgtok::comma) {
611 Lex.Lex(); // Eat the comma.
612
613 // Parse the next range piece.
614 if (ParseRangePiece(Result))
615 return std::vector<unsigned>();
616 }
617 return Result;
618}
619
620/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
621/// OptionalRangeList ::= '<' RangeList '>'
622/// OptionalRangeList ::= /*empty*/
623bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
624 if (Lex.getCode() != tgtok::less)
625 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000626
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000627 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000628 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000629
Chris Lattnerf4601652007-11-22 20:49:04 +0000630 // Parse the range list.
631 Ranges = ParseRangeList();
632 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000633
Chris Lattnerf4601652007-11-22 20:49:04 +0000634 if (Lex.getCode() != tgtok::greater) {
635 TokError("expected '>' at end of range list");
636 return Error(StartLoc, "to match this '<'");
637 }
638 Lex.Lex(); // eat the '>'.
639 return false;
640}
641
642/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
643/// OptionalBitList ::= '{' RangeList '}'
644/// OptionalBitList ::= /*empty*/
645bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
646 if (Lex.getCode() != tgtok::l_brace)
647 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000648
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000649 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000650 Lex.Lex(); // eat the '{'
Bob Wilson21870412009-11-22 04:24:42 +0000651
Chris Lattnerf4601652007-11-22 20:49:04 +0000652 // Parse the range list.
653 Ranges = ParseRangeList();
654 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000655
Chris Lattnerf4601652007-11-22 20:49:04 +0000656 if (Lex.getCode() != tgtok::r_brace) {
657 TokError("expected '}' at end of bit list");
658 return Error(StartLoc, "to match this '{'");
659 }
660 Lex.Lex(); // eat the '}'.
661 return false;
662}
663
664
665/// ParseType - Parse and return a tblgen type. This returns null on error.
666///
667/// Type ::= STRING // string type
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +0000668/// Type ::= CODE // code type
Chris Lattnerf4601652007-11-22 20:49:04 +0000669/// Type ::= BIT // bit type
670/// Type ::= BITS '<' INTVAL '>' // bits<x> type
671/// Type ::= INT // int type
672/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4601652007-11-22 20:49:04 +0000673/// Type ::= DAG // dag type
674/// Type ::= ClassID // Record Type
675///
676RecTy *TGParser::ParseType() {
677 switch (Lex.getCode()) {
678 default: TokError("Unknown token when expecting a type"); return 0;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000679 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +0000680 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000681 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
682 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000683 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4601652007-11-22 20:49:04 +0000684 case tgtok::Id:
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000685 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Chris Lattnerf4601652007-11-22 20:49:04 +0000686 return 0;
687 case tgtok::Bits: {
688 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
689 TokError("expected '<' after bits type");
690 return 0;
691 }
692 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
693 TokError("expected integer in bits<n> type");
694 return 0;
695 }
Dan Gohman63f97202008-10-17 01:33:43 +0000696 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4601652007-11-22 20:49:04 +0000697 if (Lex.Lex() != tgtok::greater) { // Eat count.
698 TokError("expected '>' at end of bits<n> type");
699 return 0;
700 }
701 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000702 return BitsRecTy::get(Val);
Chris Lattnerf4601652007-11-22 20:49:04 +0000703 }
704 case tgtok::List: {
705 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
706 TokError("expected '<' after list type");
707 return 0;
708 }
709 Lex.Lex(); // Eat '<'
710 RecTy *SubType = ParseType();
711 if (SubType == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +0000712
Chris Lattnerf4601652007-11-22 20:49:04 +0000713 if (Lex.getCode() != tgtok::greater) {
714 TokError("expected '>' at end of list<ty> type");
715 return 0;
716 }
717 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000718 return ListRecTy::get(SubType);
Chris Lattnerf4601652007-11-22 20:49:04 +0000719 }
Bob Wilson21870412009-11-22 04:24:42 +0000720 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000721}
722
723/// ParseIDValue - Parse an ID as a value and decode what it means.
724///
725/// IDValue ::= ID [def local value]
726/// IDValue ::= ID [def template arg]
727/// IDValue ::= ID [multiclass local value]
728/// IDValue ::= ID [multiclass template argument]
729/// IDValue ::= ID [def name]
730///
David Greenef3744a02011-10-19 13:04:20 +0000731Init *TGParser::ParseIDValue(Record *CurRec, IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000732 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
733 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000734 SMLoc Loc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000735 Lex.Lex();
736 return ParseIDValue(CurRec, Name, Loc);
737}
738
739/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
740/// has already been read.
David Greene05bce0b2011-07-29 22:43:06 +0000741Init *TGParser::ParseIDValue(Record *CurRec,
David Greenef3744a02011-10-19 13:04:20 +0000742 const std::string &Name, SMLoc NameLoc,
743 IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000744 if (CurRec) {
745 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenedcd35c72011-07-29 19:07:07 +0000746 return VarInit::get(Name, RV->getType());
Bob Wilson21870412009-11-22 04:24:42 +0000747
David Greenee22b3212011-10-19 13:02:42 +0000748 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
749
David Greenecaa25c82011-10-05 22:42:54 +0000750 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +0000751 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
752 "::");
David Greenecaa25c82011-10-05 22:42:54 +0000753
Chris Lattnerf4601652007-11-22 20:49:04 +0000754 if (CurRec->isTemplateArg(TemplateArgName)) {
755 const RecordVal *RV = CurRec->getValue(TemplateArgName);
756 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000757 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000758 }
759 }
Bob Wilson21870412009-11-22 04:24:42 +0000760
Chris Lattnerf4601652007-11-22 20:49:04 +0000761 if (CurMultiClass) {
David Greenee22b3212011-10-19 13:02:42 +0000762 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
763 "::");
764
Chris Lattnerf4601652007-11-22 20:49:04 +0000765 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
766 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
767 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000768 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000769 }
770 }
Bob Wilson21870412009-11-22 04:24:42 +0000771
David Greenecebb4ee2012-02-22 16:09:41 +0000772 // If this is in a foreach loop, make sure it's not a loop iterator
773 for (LoopVector::iterator i = Loops.begin(), iend = Loops.end();
774 i != iend;
775 ++i) {
Sean Silva6cfc8062012-10-10 20:24:43 +0000776 VarInit *IterVar = dyn_cast<VarInit>(i->IterVar);
David Greenecebb4ee2012-02-22 16:09:41 +0000777 if (IterVar && IterVar->getName() == Name)
778 return IterVar;
779 }
780
David Greenebbec2792011-10-19 13:04:21 +0000781 if (Mode == ParseNameMode)
782 return StringInit::get(Name);
783
Chris Lattnerf4601652007-11-22 20:49:04 +0000784 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000785 return DefInit::get(D);
Chris Lattnerf4601652007-11-22 20:49:04 +0000786
David Greenebbec2792011-10-19 13:04:21 +0000787 if (Mode == ParseValueMode) {
788 Error(NameLoc, "Variable not defined: '" + Name + "'");
789 return 0;
790 }
791
792 return StringInit::get(Name);
Chris Lattnerf4601652007-11-22 20:49:04 +0000793}
794
David Greened418c1b2009-05-14 20:54:48 +0000795/// ParseOperation - Parse an operator. This returns null on error.
796///
797/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
798///
David Greene05bce0b2011-07-29 22:43:06 +0000799Init *TGParser::ParseOperation(Record *CurRec) {
David Greened418c1b2009-05-14 20:54:48 +0000800 switch (Lex.getCode()) {
801 default:
802 TokError("unknown operation");
803 return 0;
David Greene1434f662011-01-07 17:05:37 +0000804 case tgtok::XHead:
805 case tgtok::XTail:
806 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +0000807 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
808 UnOpInit::UnaryOp Code;
809 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000810
David Greenee6c27de2009-05-14 21:22:49 +0000811 switch (Lex.getCode()) {
Craig Topper85814382012-02-07 05:05:23 +0000812 default: llvm_unreachable("Unhandled code!");
David Greenee6c27de2009-05-14 21:22:49 +0000813 case tgtok::XCast:
814 Lex.Lex(); // eat the operation
815 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000816
David Greenee6c27de2009-05-14 21:22:49 +0000817 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000818
David Greenee6c27de2009-05-14 21:22:49 +0000819 if (Type == 0) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000820 TokError("did not get type for unary operator");
David Greenee6c27de2009-05-14 21:22:49 +0000821 return 0;
822 }
David Greened418c1b2009-05-14 20:54:48 +0000823
David Greenee6c27de2009-05-14 21:22:49 +0000824 break;
David Greene1434f662011-01-07 17:05:37 +0000825 case tgtok::XHead:
David Greene5f9f9ba2009-05-14 22:38:31 +0000826 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000827 Code = UnOpInit::HEAD;
David Greene5f9f9ba2009-05-14 22:38:31 +0000828 break;
David Greene1434f662011-01-07 17:05:37 +0000829 case tgtok::XTail:
David Greene5f9f9ba2009-05-14 22:38:31 +0000830 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000831 Code = UnOpInit::TAIL;
David Greene5f9f9ba2009-05-14 22:38:31 +0000832 break;
David Greene1434f662011-01-07 17:05:37 +0000833 case tgtok::XEmpty:
David Greene5f9f9ba2009-05-14 22:38:31 +0000834 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000835 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000836 Type = IntRecTy::get();
David Greene5f9f9ba2009-05-14 22:38:31 +0000837 break;
David Greenee6c27de2009-05-14 21:22:49 +0000838 }
839 if (Lex.getCode() != tgtok::l_paren) {
840 TokError("expected '(' after unary operator");
841 return 0;
842 }
843 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000844
David Greene05bce0b2011-07-29 22:43:06 +0000845 Init *LHS = ParseValue(CurRec);
David Greenee6c27de2009-05-14 21:22:49 +0000846 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000847
David Greene1434f662011-01-07 17:05:37 +0000848 if (Code == UnOpInit::HEAD
849 || Code == UnOpInit::TAIL
850 || Code == UnOpInit::EMPTY) {
Sean Silva6cfc8062012-10-10 20:24:43 +0000851 ListInit *LHSl = dyn_cast<ListInit>(LHS);
852 StringInit *LHSs = dyn_cast<StringInit>(LHS);
853 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000854 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
855 TokError("expected list or string type argument in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000856 return 0;
857 }
858 if (LHSt) {
Sean Silva736ceac2012-10-05 03:31:58 +0000859 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
860 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
David Greenee1b46912009-06-08 20:23:18 +0000861 if (LType == 0 && SType == 0) {
862 TokError("expected list or string type argumnet in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000863 return 0;
864 }
865 }
866
David Greene1434f662011-01-07 17:05:37 +0000867 if (Code == UnOpInit::HEAD
868 || Code == UnOpInit::TAIL) {
David Greenee1b46912009-06-08 20:23:18 +0000869 if (LHSl == 0 && LHSt == 0) {
870 TokError("expected list type argumnet in unary operator");
871 return 0;
872 }
Bob Wilson21870412009-11-22 04:24:42 +0000873
David Greene5f9f9ba2009-05-14 22:38:31 +0000874 if (LHSl && LHSl->getSize() == 0) {
875 TokError("empty list argument in unary operator");
876 return 0;
877 }
878 if (LHSl) {
David Greene05bce0b2011-07-29 22:43:06 +0000879 Init *Item = LHSl->getElement(0);
Sean Silva6cfc8062012-10-10 20:24:43 +0000880 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
David Greene5f9f9ba2009-05-14 22:38:31 +0000881 if (Itemt == 0) {
882 TokError("untyped list element in unary operator");
883 return 0;
884 }
David Greene1434f662011-01-07 17:05:37 +0000885 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000886 Type = Itemt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000887 } else {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000888 Type = ListRecTy::get(Itemt->getType());
David Greene5f9f9ba2009-05-14 22:38:31 +0000889 }
Bob Wilson21870412009-11-22 04:24:42 +0000890 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000891 assert(LHSt && "expected list type argument in unary operator");
Sean Silva736ceac2012-10-05 03:31:58 +0000892 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
David Greene5f9f9ba2009-05-14 22:38:31 +0000893 if (LType == 0) {
894 TokError("expected list type argumnet in unary operator");
895 return 0;
896 }
David Greene1434f662011-01-07 17:05:37 +0000897 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000898 Type = LType->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +0000899 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000900 Type = LType;
901 }
902 }
903 }
904 }
905
David Greenee6c27de2009-05-14 21:22:49 +0000906 if (Lex.getCode() != tgtok::r_paren) {
907 TokError("expected ')' in unary operator");
908 return 0;
909 }
910 Lex.Lex(); // eat the ')'
David Greenedcd35c72011-07-29 19:07:07 +0000911 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee6c27de2009-05-14 21:22:49 +0000912 }
David Greened418c1b2009-05-14 20:54:48 +0000913
914 case tgtok::XConcat:
Hal Finkeld23a41c2013-01-25 14:49:08 +0000915 case tgtok::XADD:
Bob Wilson21870412009-11-22 04:24:42 +0000916 case tgtok::XSRA:
David Greened418c1b2009-05-14 20:54:48 +0000917 case tgtok::XSRL:
918 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +0000919 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +0000920 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000921 tgtok::TokKind OpTok = Lex.getCode();
922 SMLoc OpLoc = Lex.getLoc();
923 Lex.Lex(); // eat the operation
924
David Greened418c1b2009-05-14 20:54:48 +0000925 BinOpInit::BinaryOp Code;
926 RecTy *Type = 0;
927
Chris Lattner8d978a72010-10-05 23:58:18 +0000928 switch (OpTok) {
Craig Topper85814382012-02-07 05:05:23 +0000929 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000930 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkeld23a41c2013-01-25 14:49:08 +0000931 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000932 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
933 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
934 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
935 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Bob Wilson21870412009-11-22 04:24:42 +0000936 case tgtok::XStrConcat:
David Greened418c1b2009-05-14 20:54:48 +0000937 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000938 Type = StringRecTy::get();
David Greened418c1b2009-05-14 20:54:48 +0000939 break;
David Greened418c1b2009-05-14 20:54:48 +0000940 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000941
David Greened418c1b2009-05-14 20:54:48 +0000942 if (Lex.getCode() != tgtok::l_paren) {
943 TokError("expected '(' after binary operator");
944 return 0;
945 }
946 Lex.Lex(); // eat the '('
947
David Greene05bce0b2011-07-29 22:43:06 +0000948 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000949
Chris Lattner8d978a72010-10-05 23:58:18 +0000950 InitList.push_back(ParseValue(CurRec));
951 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000952
Chris Lattner8d978a72010-10-05 23:58:18 +0000953 while (Lex.getCode() == tgtok::comma) {
954 Lex.Lex(); // eat the ','
955
956 InitList.push_back(ParseValue(CurRec));
957 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000958 }
David Greened418c1b2009-05-14 20:54:48 +0000959
960 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner8d978a72010-10-05 23:58:18 +0000961 TokError("expected ')' in operator");
David Greened418c1b2009-05-14 20:54:48 +0000962 return 0;
963 }
964 Lex.Lex(); // eat the ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000965
966 // We allow multiple operands to associative operators like !strconcat as
967 // shorthand for nesting them.
968 if (Code == BinOpInit::STRCONCAT) {
969 while (InitList.size() > 2) {
David Greene05bce0b2011-07-29 22:43:06 +0000970 Init *RHS = InitList.pop_back_val();
David Greenedcd35c72011-07-29 19:07:07 +0000971 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
972 ->Fold(CurRec, CurMultiClass);
Chris Lattner8d978a72010-10-05 23:58:18 +0000973 InitList.back() = RHS;
974 }
975 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000976
Chris Lattner8d978a72010-10-05 23:58:18 +0000977 if (InitList.size() == 2)
David Greenedcd35c72011-07-29 19:07:07 +0000978 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner8d978a72010-10-05 23:58:18 +0000979 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000980
Chris Lattner8d978a72010-10-05 23:58:18 +0000981 Error(OpLoc, "expected two operands to operator");
982 return 0;
David Greened418c1b2009-05-14 20:54:48 +0000983 }
984
David Greene9bea7c82009-05-14 23:26:46 +0000985 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +0000986 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +0000987 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
988 TernOpInit::TernaryOp Code;
989 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000990
David Greene4afc5092009-05-14 21:54:42 +0000991 tgtok::TokKind LexCode = Lex.getCode();
992 Lex.Lex(); // eat the operation
993 switch (LexCode) {
Craig Topper85814382012-02-07 05:05:23 +0000994 default: llvm_unreachable("Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000995 case tgtok::XIf:
996 Code = TernOpInit::IF;
997 break;
David Greenebeb31a52009-05-14 22:23:47 +0000998 case tgtok::XForEach:
999 Code = TernOpInit::FOREACH;
1000 break;
David Greene4afc5092009-05-14 21:54:42 +00001001 case tgtok::XSubst:
1002 Code = TernOpInit::SUBST;
1003 break;
1004 }
1005 if (Lex.getCode() != tgtok::l_paren) {
1006 TokError("expected '(' after ternary operator");
1007 return 0;
1008 }
1009 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +00001010
David Greene05bce0b2011-07-29 22:43:06 +00001011 Init *LHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001012 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001013
David Greene4afc5092009-05-14 21:54:42 +00001014 if (Lex.getCode() != tgtok::comma) {
1015 TokError("expected ',' in ternary operator");
1016 return 0;
1017 }
1018 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001019
David Greene05bce0b2011-07-29 22:43:06 +00001020 Init *MHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001021 if (MHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001022
David Greene4afc5092009-05-14 21:54:42 +00001023 if (Lex.getCode() != tgtok::comma) {
1024 TokError("expected ',' in ternary operator");
1025 return 0;
1026 }
1027 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001028
David Greene05bce0b2011-07-29 22:43:06 +00001029 Init *RHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +00001030 if (RHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +00001031
David Greene4afc5092009-05-14 21:54:42 +00001032 if (Lex.getCode() != tgtok::r_paren) {
1033 TokError("expected ')' in binary operator");
1034 return 0;
1035 }
1036 Lex.Lex(); // eat the ')'
David Greened418c1b2009-05-14 20:54:48 +00001037
David Greene4afc5092009-05-14 21:54:42 +00001038 switch (LexCode) {
Craig Topper85814382012-02-07 05:05:23 +00001039 default: llvm_unreachable("Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +00001040 case tgtok::XIf: {
Bill Wendling548f5a02010-12-13 01:46:19 +00001041 RecTy *MHSTy = 0;
1042 RecTy *RHSTy = 0;
1043
Sean Silva6cfc8062012-10-10 20:24:43 +00001044 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling548f5a02010-12-13 01:46:19 +00001045 MHSTy = MHSt->getType();
Sean Silva6cfc8062012-10-10 20:24:43 +00001046 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao307525c2012-09-06 23:32:48 +00001047 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva3f7b7f82012-10-10 20:24:47 +00001048 if (isa<BitInit>(MHS))
Michael Liao307525c2012-09-06 23:32:48 +00001049 MHSTy = BitRecTy::get();
1050
Sean Silva6cfc8062012-10-10 20:24:43 +00001051 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling548f5a02010-12-13 01:46:19 +00001052 RHSTy = RHSt->getType();
Sean Silva6cfc8062012-10-10 20:24:43 +00001053 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao307525c2012-09-06 23:32:48 +00001054 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva3f7b7f82012-10-10 20:24:47 +00001055 if (isa<BitInit>(RHS))
Michael Liao307525c2012-09-06 23:32:48 +00001056 RHSTy = BitRecTy::get();
1057
1058 // For UnsetInit, it's typed from the other hand.
Sean Silva3f7b7f82012-10-10 20:24:47 +00001059 if (isa<UnsetInit>(MHS))
Michael Liao307525c2012-09-06 23:32:48 +00001060 MHSTy = RHSTy;
Sean Silva3f7b7f82012-10-10 20:24:47 +00001061 if (isa<UnsetInit>(RHS))
Michael Liao307525c2012-09-06 23:32:48 +00001062 RHSTy = MHSTy;
Bill Wendling548f5a02010-12-13 01:46:19 +00001063
1064 if (!MHSTy || !RHSTy) {
David Greene9bea7c82009-05-14 23:26:46 +00001065 TokError("could not get type for !if");
1066 return 0;
1067 }
Bill Wendling548f5a02010-12-13 01:46:19 +00001068
1069 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1070 Type = RHSTy;
1071 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1072 Type = MHSTy;
Bob Wilson21870412009-11-22 04:24:42 +00001073 } else {
David Greene9bea7c82009-05-14 23:26:46 +00001074 TokError("inconsistent types for !if");
1075 return 0;
1076 }
1077 break;
1078 }
David Greenebeb31a52009-05-14 22:23:47 +00001079 case tgtok::XForEach: {
Sean Silva6cfc8062012-10-10 20:24:43 +00001080 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
David Greenebeb31a52009-05-14 22:23:47 +00001081 if (MHSt == 0) {
1082 TokError("could not get type for !foreach");
1083 return 0;
1084 }
1085 Type = MHSt->getType();
1086 break;
1087 }
David Greene4afc5092009-05-14 21:54:42 +00001088 case tgtok::XSubst: {
Sean Silva6cfc8062012-10-10 20:24:43 +00001089 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
David Greene4afc5092009-05-14 21:54:42 +00001090 if (RHSt == 0) {
1091 TokError("could not get type for !subst");
1092 return 0;
1093 }
1094 Type = RHSt->getType();
1095 break;
1096 }
1097 }
David Greenedcd35c72011-07-29 19:07:07 +00001098 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson21870412009-11-22 04:24:42 +00001099 CurMultiClass);
David Greene4afc5092009-05-14 21:54:42 +00001100 }
David Greened418c1b2009-05-14 20:54:48 +00001101 }
David Greened418c1b2009-05-14 20:54:48 +00001102}
1103
1104/// ParseOperatorType - Parse a type for an operator. This returns
1105/// null on error.
1106///
1107/// OperatorType ::= '<' Type '>'
1108///
Dan Gohmana9ad0412009-08-12 22:10:57 +00001109RecTy *TGParser::ParseOperatorType() {
David Greened418c1b2009-05-14 20:54:48 +00001110 RecTy *Type = 0;
1111
1112 if (Lex.getCode() != tgtok::less) {
1113 TokError("expected type name for operator");
1114 return 0;
1115 }
1116 Lex.Lex(); // eat the <
1117
1118 Type = ParseType();
1119
1120 if (Type == 0) {
1121 TokError("expected type name for operator");
1122 return 0;
1123 }
1124
1125 if (Lex.getCode() != tgtok::greater) {
1126 TokError("expected type name for operator");
1127 return 0;
1128 }
1129 Lex.Lex(); // eat the >
1130
1131 return Type;
1132}
1133
1134
Chris Lattnerf4601652007-11-22 20:49:04 +00001135/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1136///
1137/// SimpleValue ::= IDValue
1138/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001139/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +00001140/// SimpleValue ::= CODEFRAGMENT
1141/// SimpleValue ::= '?'
1142/// SimpleValue ::= '{' ValueList '}'
1143/// SimpleValue ::= ID '<' ValueListNE '>'
1144/// SimpleValue ::= '[' ValueList ']'
1145/// SimpleValue ::= '(' IDValue DagArgList ')'
1146/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkeld23a41c2013-01-25 14:49:08 +00001147/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001148/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1149/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1150/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1151/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1152///
David Greenef3744a02011-10-19 13:04:20 +00001153Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1154 IDParseMode Mode) {
David Greene05bce0b2011-07-29 22:43:06 +00001155 Init *R = 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001156 switch (Lex.getCode()) {
1157 default: TokError("Unknown token when parsing a value"); break;
David Greened3d1cad2011-10-19 13:04:43 +00001158 case tgtok::paste:
1159 // This is a leading paste operation. This is deprecated but
1160 // still exists in some .td files. Ignore it.
1161 Lex.Lex(); // Skip '#'.
1162 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenedcd35c72011-07-29 19:07:07 +00001163 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001164 case tgtok::StrVal: {
1165 std::string Val = Lex.getCurStrVal();
1166 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001167
Jim Grosbachda4231f2009-03-26 16:17:51 +00001168 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001169 while (Lex.getCode() == tgtok::StrVal) {
1170 Val += Lex.getCurStrVal();
1171 Lex.Lex();
1172 }
Bob Wilson21870412009-11-22 04:24:42 +00001173
David Greenedcd35c72011-07-29 19:07:07 +00001174 R = StringInit::get(Val);
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001175 break;
1176 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001177 case tgtok::CodeFragment:
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +00001178 R = StringInit::get(Lex.getCurStrVal());
Chris Lattner578bcf02010-10-06 04:31:40 +00001179 Lex.Lex();
1180 break;
1181 case tgtok::question:
David Greenedcd35c72011-07-29 19:07:07 +00001182 R = UnsetInit::get();
Chris Lattner578bcf02010-10-06 04:31:40 +00001183 Lex.Lex();
1184 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001185 case tgtok::Id: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001186 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001187 std::string Name = Lex.getCurStrVal();
1188 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greenef3744a02011-10-19 13:04:20 +00001189 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson21870412009-11-22 04:24:42 +00001190
Chris Lattnerf4601652007-11-22 20:49:04 +00001191 // Value ::= ID '<' ValueListNE '>'
1192 if (Lex.Lex() == tgtok::greater) {
1193 TokError("expected non-empty value list");
1194 return 0;
1195 }
David Greenee1b46912009-06-08 20:23:18 +00001196
Chris Lattnerf4601652007-11-22 20:49:04 +00001197 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1198 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1199 // body.
1200 Record *Class = Records.getClass(Name);
1201 if (!Class) {
1202 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1203 return 0;
1204 }
David Greenee1b46912009-06-08 20:23:18 +00001205
David Greene05bce0b2011-07-29 22:43:06 +00001206 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
David Greenee1b46912009-06-08 20:23:18 +00001207 if (ValueList.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001208
David Greenee1b46912009-06-08 20:23:18 +00001209 if (Lex.getCode() != tgtok::greater) {
1210 TokError("expected '>' at end of value list");
1211 return 0;
1212 }
1213 Lex.Lex(); // eat the '>'
Jordan Roseb50df4a2013-01-10 18:50:11 +00001214 SMLoc EndLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +00001215
Chris Lattnerf4601652007-11-22 20:49:04 +00001216 // Create the new record, set it as CurRec temporarily.
1217 static unsigned AnonCounter = 0;
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001218 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),
1219 NameLoc,
Jordan Rosed1220092013-01-10 18:50:05 +00001220 Records,
1221 /*IsAnonymous=*/true);
Chris Lattnerf4601652007-11-22 20:49:04 +00001222 SubClassReference SCRef;
Jordan Roseb50df4a2013-01-10 18:50:11 +00001223 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4601652007-11-22 20:49:04 +00001224 SCRef.Rec = Class;
1225 SCRef.TemplateArgs = ValueList;
1226 // Add info about the subclass to NewRec.
1227 if (AddSubClass(NewRec, SCRef))
1228 return 0;
1229 NewRec->resolveReferences();
1230 Records.addDef(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001231
Chris Lattnerf4601652007-11-22 20:49:04 +00001232 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +00001233 return DefInit::get(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001234 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001235 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001236 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001237 Lex.Lex(); // eat the '{'
David Greene05bce0b2011-07-29 22:43:06 +00001238 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001239
Chris Lattnerf4601652007-11-22 20:49:04 +00001240 if (Lex.getCode() != tgtok::r_brace) {
1241 Vals = ParseValueList(CurRec);
1242 if (Vals.empty()) return 0;
1243 }
1244 if (Lex.getCode() != tgtok::r_brace) {
1245 TokError("expected '}' at end of bit list value");
1246 return 0;
1247 }
1248 Lex.Lex(); // eat the '}'
Bob Wilson21870412009-11-22 04:24:42 +00001249
David Greene05bce0b2011-07-29 22:43:06 +00001250 SmallVector<Init *, 16> NewBits(Vals.size());
David Greeneca7fd3d2011-07-29 19:07:00 +00001251
Chris Lattnerf4601652007-11-22 20:49:04 +00001252 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001253 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Chris Lattnerf4601652007-11-22 20:49:04 +00001254 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +00001255 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1256 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +00001257 return 0;
1258 }
David Greeneca7fd3d2011-07-29 19:07:00 +00001259 NewBits[Vals.size()-i-1] = Bit;
Chris Lattnerf4601652007-11-22 20:49:04 +00001260 }
David Greenedcd35c72011-07-29 19:07:07 +00001261 return BitsInit::get(NewBits);
Chris Lattnerf4601652007-11-22 20:49:04 +00001262 }
1263 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1264 Lex.Lex(); // eat the '['
David Greene05bce0b2011-07-29 22:43:06 +00001265 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001266
David Greenee1b46912009-06-08 20:23:18 +00001267 RecTy *DeducedEltTy = 0;
1268 ListRecTy *GivenListTy = 0;
Bob Wilson21870412009-11-22 04:24:42 +00001269
David Greenee1b46912009-06-08 20:23:18 +00001270 if (ItemType != 0) {
Sean Silva736ceac2012-10-05 03:31:58 +00001271 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
David Greenee1b46912009-06-08 20:23:18 +00001272 if (ListType == 0) {
1273 std::stringstream s;
Bob Wilson21870412009-11-22 04:24:42 +00001274 s << "Type mismatch for list, expected list type, got "
David Greenee1b46912009-06-08 20:23:18 +00001275 << ItemType->getAsString();
1276 TokError(s.str());
Jim Grosbach6a44ada2011-03-11 19:52:52 +00001277 return 0;
David Greenee1b46912009-06-08 20:23:18 +00001278 }
1279 GivenListTy = ListType;
Bob Wilson21870412009-11-22 04:24:42 +00001280 }
David Greenee1b46912009-06-08 20:23:18 +00001281
Chris Lattnerf4601652007-11-22 20:49:04 +00001282 if (Lex.getCode() != tgtok::r_square) {
Bob Wilson21870412009-11-22 04:24:42 +00001283 Vals = ParseValueList(CurRec, 0,
1284 GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001285 if (Vals.empty()) return 0;
1286 }
1287 if (Lex.getCode() != tgtok::r_square) {
1288 TokError("expected ']' at end of list value");
1289 return 0;
1290 }
1291 Lex.Lex(); // eat the ']'
David Greenee1b46912009-06-08 20:23:18 +00001292
1293 RecTy *GivenEltTy = 0;
1294 if (Lex.getCode() == tgtok::less) {
1295 // Optional list element type
1296 Lex.Lex(); // eat the '<'
1297
1298 GivenEltTy = ParseType();
1299 if (GivenEltTy == 0) {
1300 // Couldn't parse element type
1301 return 0;
1302 }
1303
1304 if (Lex.getCode() != tgtok::greater) {
1305 TokError("expected '>' at end of list element type");
1306 return 0;
1307 }
1308 Lex.Lex(); // eat the '>'
1309 }
1310
1311 // Check elements
1312 RecTy *EltTy = 0;
David Greene05bce0b2011-07-29 22:43:06 +00001313 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greenee1b46912009-06-08 20:23:18 +00001314 i != ie;
1315 ++i) {
Sean Silva6cfc8062012-10-10 20:24:43 +00001316 TypedInit *TArg = dyn_cast<TypedInit>(*i);
David Greenee1b46912009-06-08 20:23:18 +00001317 if (TArg == 0) {
1318 TokError("Untyped list element");
1319 return 0;
1320 }
1321 if (EltTy != 0) {
1322 EltTy = resolveTypes(EltTy, TArg->getType());
1323 if (EltTy == 0) {
1324 TokError("Incompatible types in list elements");
1325 return 0;
1326 }
Bob Wilson21870412009-11-22 04:24:42 +00001327 } else {
David Greenee1b46912009-06-08 20:23:18 +00001328 EltTy = TArg->getType();
1329 }
1330 }
1331
1332 if (GivenEltTy != 0) {
1333 if (EltTy != 0) {
1334 // Verify consistency
1335 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1336 TokError("Incompatible types in list elements");
1337 return 0;
1338 }
1339 }
1340 EltTy = GivenEltTy;
1341 }
1342
1343 if (EltTy == 0) {
1344 if (ItemType == 0) {
1345 TokError("No type for list");
1346 return 0;
1347 }
1348 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +00001349 } else {
David Greenee1b46912009-06-08 20:23:18 +00001350 // Make sure the deduced type is compatible with the given type
1351 if (GivenListTy) {
1352 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1353 TokError("Element type mismatch for list");
1354 return 0;
1355 }
1356 }
1357 DeducedEltTy = EltTy;
1358 }
Bob Wilson21870412009-11-22 04:24:42 +00001359
David Greenedcd35c72011-07-29 19:07:07 +00001360 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4601652007-11-22 20:49:04 +00001361 }
1362 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1363 Lex.Lex(); // eat the '('
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001364 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001365 TokError("expected identifier in dag init");
1366 return 0;
1367 }
Bob Wilson21870412009-11-22 04:24:42 +00001368
David Greene05bce0b2011-07-29 22:43:06 +00001369 Init *Operator = ParseValue(CurRec);
Chris Lattner578bcf02010-10-06 04:31:40 +00001370 if (Operator == 0) return 0;
David Greenec7cafcd2009-04-22 20:18:10 +00001371
Nate Begeman7cee8172009-03-19 05:21:56 +00001372 // If the operator name is present, parse it.
1373 std::string OperatorName;
1374 if (Lex.getCode() == tgtok::colon) {
1375 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1376 TokError("expected variable name in dag operator");
1377 return 0;
1378 }
1379 OperatorName = Lex.getCurStrVal();
1380 Lex.Lex(); // eat the VarName.
1381 }
Bob Wilson21870412009-11-22 04:24:42 +00001382
David Greene05bce0b2011-07-29 22:43:06 +00001383 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4601652007-11-22 20:49:04 +00001384 if (Lex.getCode() != tgtok::r_paren) {
1385 DagArgs = ParseDagArgList(CurRec);
1386 if (DagArgs.empty()) return 0;
1387 }
Bob Wilson21870412009-11-22 04:24:42 +00001388
Chris Lattnerf4601652007-11-22 20:49:04 +00001389 if (Lex.getCode() != tgtok::r_paren) {
1390 TokError("expected ')' in dag init");
1391 return 0;
1392 }
1393 Lex.Lex(); // eat the ')'
Bob Wilson21870412009-11-22 04:24:42 +00001394
David Greenedcd35c72011-07-29 19:07:07 +00001395 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4601652007-11-22 20:49:04 +00001396 }
Bob Wilson21870412009-11-22 04:24:42 +00001397
David Greene1434f662011-01-07 17:05:37 +00001398 case tgtok::XHead:
1399 case tgtok::XTail:
1400 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +00001401 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001402 case tgtok::XConcat:
Hal Finkeld23a41c2013-01-25 14:49:08 +00001403 case tgtok::XADD:
Bob Wilson21870412009-11-22 04:24:42 +00001404 case tgtok::XSRA:
Chris Lattnerf4601652007-11-22 20:49:04 +00001405 case tgtok::XSRL:
1406 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +00001407 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001408 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene9bea7c82009-05-14 23:26:46 +00001409 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +00001410 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001411 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greened418c1b2009-05-14 20:54:48 +00001412 return ParseOperation(CurRec);
Chris Lattnerf4601652007-11-22 20:49:04 +00001413 }
1414 }
Bob Wilson21870412009-11-22 04:24:42 +00001415
Chris Lattnerf4601652007-11-22 20:49:04 +00001416 return R;
1417}
1418
1419/// ParseValue - Parse a tblgen value. This returns null on error.
1420///
1421/// Value ::= SimpleValue ValueSuffix*
1422/// ValueSuffix ::= '{' BitList '}'
1423/// ValueSuffix ::= '[' BitList ']'
1424/// ValueSuffix ::= '.' ID
1425///
David Greenef3744a02011-10-19 13:04:20 +00001426Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1427 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Chris Lattnerf4601652007-11-22 20:49:04 +00001428 if (Result == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001429
Chris Lattnerf4601652007-11-22 20:49:04 +00001430 // Parse the suffixes now if present.
1431 while (1) {
1432 switch (Lex.getCode()) {
1433 default: return Result;
1434 case tgtok::l_brace: {
David Greenecebb4ee2012-02-22 16:09:41 +00001435 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greene8592b2b2011-10-19 13:04:26 +00001436 // This is the beginning of the object body.
1437 return Result;
1438
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001439 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001440 Lex.Lex(); // eat the '{'
1441 std::vector<unsigned> Ranges = ParseRangeList();
1442 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001443
Chris Lattnerf4601652007-11-22 20:49:04 +00001444 // Reverse the bitlist.
1445 std::reverse(Ranges.begin(), Ranges.end());
1446 Result = Result->convertInitializerBitRange(Ranges);
1447 if (Result == 0) {
1448 Error(CurlyLoc, "Invalid bit range for value");
1449 return 0;
1450 }
Bob Wilson21870412009-11-22 04:24:42 +00001451
Chris Lattnerf4601652007-11-22 20:49:04 +00001452 // Eat the '}'.
1453 if (Lex.getCode() != tgtok::r_brace) {
1454 TokError("expected '}' at end of bit range list");
1455 return 0;
1456 }
1457 Lex.Lex();
1458 break;
1459 }
1460 case tgtok::l_square: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001461 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001462 Lex.Lex(); // eat the '['
1463 std::vector<unsigned> Ranges = ParseRangeList();
1464 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001465
Chris Lattnerf4601652007-11-22 20:49:04 +00001466 Result = Result->convertInitListSlice(Ranges);
1467 if (Result == 0) {
1468 Error(SquareLoc, "Invalid range for list slice");
1469 return 0;
1470 }
Bob Wilson21870412009-11-22 04:24:42 +00001471
Chris Lattnerf4601652007-11-22 20:49:04 +00001472 // Eat the ']'.
1473 if (Lex.getCode() != tgtok::r_square) {
1474 TokError("expected ']' at end of list slice");
1475 return 0;
1476 }
1477 Lex.Lex();
1478 break;
1479 }
1480 case tgtok::period:
1481 if (Lex.Lex() != tgtok::Id) { // eat the .
1482 TokError("expected field identifier after '.'");
1483 return 0;
1484 }
1485 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001486 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00001487 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +00001488 return 0;
1489 }
David Greenedcd35c72011-07-29 19:07:07 +00001490 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001491 Lex.Lex(); // eat field name
1492 break;
David Greened3d1cad2011-10-19 13:04:43 +00001493
1494 case tgtok::paste:
1495 SMLoc PasteLoc = Lex.getLoc();
1496
1497 // Create a !strconcat() operation, first casting each operand to
1498 // a string if necessary.
1499
Sean Silva6cfc8062012-10-10 20:24:43 +00001500 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greened3d1cad2011-10-19 13:04:43 +00001501 if (!LHS) {
1502 Error(PasteLoc, "LHS of paste is not typed!");
1503 return 0;
1504 }
1505
1506 if (LHS->getType() != StringRecTy::get()) {
1507 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1508 }
1509
1510 TypedInit *RHS = 0;
1511
1512 Lex.Lex(); // Eat the '#'.
1513 switch (Lex.getCode()) {
1514 case tgtok::colon:
1515 case tgtok::semi:
1516 case tgtok::l_brace:
1517 // These are all of the tokens that can begin an object body.
1518 // Some of these can also begin values but we disallow those cases
1519 // because they are unlikely to be useful.
1520
1521 // Trailing paste, concat with an empty string.
1522 RHS = StringInit::get("");
1523 break;
1524
1525 default:
1526 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silva6cfc8062012-10-10 20:24:43 +00001527 RHS = dyn_cast<TypedInit>(RHSResult);
David Greened3d1cad2011-10-19 13:04:43 +00001528 if (!RHS) {
1529 Error(PasteLoc, "RHS of paste is not typed!");
1530 return 0;
1531 }
1532
1533 if (RHS->getType() != StringRecTy::get()) {
1534 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1535 }
1536
1537 break;
1538 }
1539
1540 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1541 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1542 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001543 }
1544 }
1545}
1546
1547/// ParseDagArgList - Parse the argument list for a dag literal expression.
1548///
1549/// ParseDagArgList ::= Value (':' VARNAME)?
1550/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
David Greene05bce0b2011-07-29 22:43:06 +00001551std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4601652007-11-22 20:49:04 +00001552TGParser::ParseDagArgList(Record *CurRec) {
David Greene05bce0b2011-07-29 22:43:06 +00001553 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson21870412009-11-22 04:24:42 +00001554
Chris Lattnerf4601652007-11-22 20:49:04 +00001555 while (1) {
David Greene05bce0b2011-07-29 22:43:06 +00001556 Init *Val = ParseValue(CurRec);
1557 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
Bob Wilson21870412009-11-22 04:24:42 +00001558
Chris Lattnerf4601652007-11-22 20:49:04 +00001559 // If the variable name is present, add it.
1560 std::string VarName;
1561 if (Lex.getCode() == tgtok::colon) {
1562 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1563 TokError("expected variable name in dag literal");
David Greene05bce0b2011-07-29 22:43:06 +00001564 return std::vector<std::pair<llvm::Init*, std::string> >();
Chris Lattnerf4601652007-11-22 20:49:04 +00001565 }
1566 VarName = Lex.getCurStrVal();
1567 Lex.Lex(); // eat the VarName.
1568 }
Bob Wilson21870412009-11-22 04:24:42 +00001569
Chris Lattnerf4601652007-11-22 20:49:04 +00001570 Result.push_back(std::make_pair(Val, VarName));
Bob Wilson21870412009-11-22 04:24:42 +00001571
Chris Lattnerf4601652007-11-22 20:49:04 +00001572 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson21870412009-11-22 04:24:42 +00001573 Lex.Lex(); // eat the ','
Chris Lattnerf4601652007-11-22 20:49:04 +00001574 }
Bob Wilson21870412009-11-22 04:24:42 +00001575
Chris Lattnerf4601652007-11-22 20:49:04 +00001576 return Result;
1577}
1578
1579
1580/// ParseValueList - Parse a comma separated list of values, returning them as a
1581/// vector. Note that this always expects to be able to parse at least one
1582/// value. It returns an empty list if this is not possible.
1583///
1584/// ValueList ::= Value (',' Value)
1585///
David Greene05bce0b2011-07-29 22:43:06 +00001586std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopherd568b3f2011-07-11 23:06:52 +00001587 RecTy *EltTy) {
David Greene05bce0b2011-07-29 22:43:06 +00001588 std::vector<Init*> Result;
David Greenee1b46912009-06-08 20:23:18 +00001589 RecTy *ItemType = EltTy;
David Greene67acdf22009-06-29 19:59:52 +00001590 unsigned int ArgN = 0;
David Greenee1b46912009-06-08 20:23:18 +00001591 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001592 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Jim Grosbachb1320cb2012-01-20 20:02:39 +00001593 if (!TArgs.size()) {
1594 TokError("template argument provided to non-template class");
1595 return std::vector<Init*>();
1596 }
David Greenee1b46912009-06-08 20:23:18 +00001597 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greened9746fe2011-09-19 18:26:07 +00001598 if (!RV) {
1599 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1600 << ")\n";
1601 }
David Greenee1b46912009-06-08 20:23:18 +00001602 assert(RV && "Template argument record not found??");
1603 ItemType = RV->getType();
1604 ++ArgN;
1605 }
1606 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001607 if (Result.back() == 0) return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001608
Chris Lattnerf4601652007-11-22 20:49:04 +00001609 while (Lex.getCode() == tgtok::comma) {
1610 Lex.Lex(); // Eat the comma
Bob Wilson21870412009-11-22 04:24:42 +00001611
David Greenee1b46912009-06-08 20:23:18 +00001612 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001613 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greene67acdf22009-06-29 19:59:52 +00001614 if (ArgN >= TArgs.size()) {
1615 TokError("too many template arguments");
David Greene05bce0b2011-07-29 22:43:06 +00001616 return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001617 }
David Greenee1b46912009-06-08 20:23:18 +00001618 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1619 assert(RV && "Template argument record not found??");
1620 ItemType = RV->getType();
1621 ++ArgN;
1622 }
1623 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001624 if (Result.back() == 0) return std::vector<Init*>();
Chris Lattnerf4601652007-11-22 20:49:04 +00001625 }
Bob Wilson21870412009-11-22 04:24:42 +00001626
Chris Lattnerf4601652007-11-22 20:49:04 +00001627 return Result;
1628}
1629
1630
Chris Lattnerf4601652007-11-22 20:49:04 +00001631/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1632/// empty string on error. This can happen in a number of different context's,
1633/// including within a def or in the template args for a def (which which case
1634/// CurRec will be non-null) and within the template args for a multiclass (in
1635/// which case CurRec will be null, but CurMultiClass will be set). This can
1636/// also happen within a def that is within a multiclass, which will set both
1637/// CurRec and CurMultiClass.
1638///
1639/// Declaration ::= FIELD? Type ID ('=' Value)?
1640///
David Greenee22b3212011-10-19 13:02:42 +00001641Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4601652007-11-22 20:49:04 +00001642 bool ParsingTemplateArgs) {
1643 // Read the field prefix if present.
1644 bool HasField = Lex.getCode() == tgtok::Field;
1645 if (HasField) Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001646
Chris Lattnerf4601652007-11-22 20:49:04 +00001647 RecTy *Type = ParseType();
David Greenee22b3212011-10-19 13:02:42 +00001648 if (Type == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001649
Chris Lattnerf4601652007-11-22 20:49:04 +00001650 if (Lex.getCode() != tgtok::Id) {
1651 TokError("Expected identifier in declaration");
David Greenee22b3212011-10-19 13:02:42 +00001652 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001653 }
Bob Wilson21870412009-11-22 04:24:42 +00001654
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001655 SMLoc IdLoc = Lex.getLoc();
David Greenee22b3212011-10-19 13:02:42 +00001656 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001657 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001658
Chris Lattnerf4601652007-11-22 20:49:04 +00001659 if (ParsingTemplateArgs) {
1660 if (CurRec) {
David Greenee22b3212011-10-19 13:02:42 +00001661 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4601652007-11-22 20:49:04 +00001662 } else {
1663 assert(CurMultiClass);
1664 }
1665 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +00001666 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1667 "::");
Chris Lattnerf4601652007-11-22 20:49:04 +00001668 }
Bob Wilson21870412009-11-22 04:24:42 +00001669
Chris Lattnerf4601652007-11-22 20:49:04 +00001670 // Add the value.
1671 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
David Greenee22b3212011-10-19 13:02:42 +00001672 return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001673
Chris Lattnerf4601652007-11-22 20:49:04 +00001674 // If a value is present, parse it.
1675 if (Lex.getCode() == tgtok::equal) {
1676 Lex.Lex();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001677 SMLoc ValLoc = Lex.getLoc();
David Greene05bce0b2011-07-29 22:43:06 +00001678 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001679 if (Val == 0 ||
1680 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
David Greenee22b3212011-10-19 13:02:42 +00001681 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001682 }
Bob Wilson21870412009-11-22 04:24:42 +00001683
Chris Lattnerf4601652007-11-22 20:49:04 +00001684 return DeclName;
1685}
1686
David Greenecebb4ee2012-02-22 16:09:41 +00001687/// ParseForeachDeclaration - Read a foreach declaration, returning
1688/// the name of the declared object or a NULL Init on error. Return
1689/// the name of the parsed initializer list through ForeachListName.
1690///
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001691/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1692/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1693/// ForeachDeclaration ::= ID '=' RangePiece
David Greenecebb4ee2012-02-22 16:09:41 +00001694///
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00001695VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenecebb4ee2012-02-22 16:09:41 +00001696 if (Lex.getCode() != tgtok::Id) {
1697 TokError("Expected identifier in foreach declaration");
1698 return 0;
1699 }
1700
1701 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1702 Lex.Lex();
1703
1704 // If a value is present, parse it.
1705 if (Lex.getCode() != tgtok::equal) {
1706 TokError("Expected '=' in foreach declaration");
1707 return 0;
1708 }
1709 Lex.Lex(); // Eat the '='
1710
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001711 RecTy *IterType = 0;
1712 std::vector<unsigned> Ranges;
David Greenecebb4ee2012-02-22 16:09:41 +00001713
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001714 switch (Lex.getCode()) {
1715 default: TokError("Unknown token when expecting a range list"); return 0;
1716 case tgtok::l_square: { // '[' ValueList ']'
1717 Init *List = ParseSimpleValue(0, 0, ParseForeachMode);
Sean Silva6cfc8062012-10-10 20:24:43 +00001718 ForeachListValue = dyn_cast<ListInit>(List);
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001719 if (ForeachListValue == 0) {
1720 TokError("Expected a Value list");
1721 return 0;
1722 }
1723 RecTy *ValueType = ForeachListValue->getType();
Sean Silva736ceac2012-10-05 03:31:58 +00001724 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001725 if (ListType == 0) {
1726 TokError("Value list is not of list type");
1727 return 0;
1728 }
1729 IterType = ListType->getElementType();
1730 break;
David Greenecebb4ee2012-02-22 16:09:41 +00001731 }
1732
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001733 case tgtok::IntVal: { // RangePiece.
1734 if (ParseRangePiece(Ranges))
1735 return 0;
1736 break;
David Greenecebb4ee2012-02-22 16:09:41 +00001737 }
1738
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001739 case tgtok::l_brace: { // '{' RangeList '}'
1740 Lex.Lex(); // eat the '{'
1741 Ranges = ParseRangeList();
1742 if (Lex.getCode() != tgtok::r_brace) {
1743 TokError("expected '}' at end of bit range list");
1744 return 0;
1745 }
1746 Lex.Lex();
1747 break;
1748 }
1749 }
David Greenecebb4ee2012-02-22 16:09:41 +00001750
Jakob Stoklund Olesenfae8b1d2012-05-24 22:17:39 +00001751 if (!Ranges.empty()) {
1752 assert(!IterType && "Type already initialized?");
1753 IterType = IntRecTy::get();
1754 std::vector<Init*> Values;
1755 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
1756 Values.push_back(IntInit::get(Ranges[i]));
1757 ForeachListValue = ListInit::get(Values, IterType);
1758 }
1759
1760 if (!IterType)
1761 return 0;
1762
1763 return VarInit::get(DeclName, IterType);
David Greenecebb4ee2012-02-22 16:09:41 +00001764}
1765
Chris Lattnerf4601652007-11-22 20:49:04 +00001766/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1767/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1768/// template args for a def, which may or may not be in a multiclass. If null,
1769/// these are the template args for a multiclass.
1770///
1771/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson21870412009-11-22 04:24:42 +00001772///
Chris Lattnerf4601652007-11-22 20:49:04 +00001773bool TGParser::ParseTemplateArgList(Record *CurRec) {
1774 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1775 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +00001776
Chris Lattnerf4601652007-11-22 20:49:04 +00001777 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +00001778
Chris Lattnerf4601652007-11-22 20:49:04 +00001779 // Read the first declaration.
David Greenee22b3212011-10-19 13:02:42 +00001780 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1781 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001782 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001783
Chris Lattnerf4601652007-11-22 20:49:04 +00001784 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson21870412009-11-22 04:24:42 +00001785
Chris Lattnerf4601652007-11-22 20:49:04 +00001786 while (Lex.getCode() == tgtok::comma) {
1787 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001788
Chris Lattnerf4601652007-11-22 20:49:04 +00001789 // Read the following declarations.
1790 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
David Greenee22b3212011-10-19 13:02:42 +00001791 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001792 return true;
1793 TheRecToAddTo->addTemplateArg(TemplArg);
1794 }
Bob Wilson21870412009-11-22 04:24:42 +00001795
Chris Lattnerf4601652007-11-22 20:49:04 +00001796 if (Lex.getCode() != tgtok::greater)
1797 return TokError("expected '>' at end of template argument list");
1798 Lex.Lex(); // eat the '>'.
1799 return false;
1800}
1801
1802
1803/// ParseBodyItem - Parse a single item at within the body of a def or class.
1804///
1805/// BodyItem ::= Declaration ';'
1806/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1807bool TGParser::ParseBodyItem(Record *CurRec) {
1808 if (Lex.getCode() != tgtok::Let) {
David Greenee22b3212011-10-19 13:02:42 +00001809 if (ParseDeclaration(CurRec, false) == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001810 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001811
Chris Lattnerf4601652007-11-22 20:49:04 +00001812 if (Lex.getCode() != tgtok::semi)
1813 return TokError("expected ';' after declaration");
1814 Lex.Lex();
1815 return false;
1816 }
1817
1818 // LET ID OptionalRangeList '=' Value ';'
1819 if (Lex.Lex() != tgtok::Id)
1820 return TokError("expected field identifier after let");
Bob Wilson21870412009-11-22 04:24:42 +00001821
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001822 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001823 std::string FieldName = Lex.getCurStrVal();
1824 Lex.Lex(); // eat the field name.
Bob Wilson21870412009-11-22 04:24:42 +00001825
Chris Lattnerf4601652007-11-22 20:49:04 +00001826 std::vector<unsigned> BitList;
Bob Wilson21870412009-11-22 04:24:42 +00001827 if (ParseOptionalBitList(BitList))
Chris Lattnerf4601652007-11-22 20:49:04 +00001828 return true;
1829 std::reverse(BitList.begin(), BitList.end());
Bob Wilson21870412009-11-22 04:24:42 +00001830
Chris Lattnerf4601652007-11-22 20:49:04 +00001831 if (Lex.getCode() != tgtok::equal)
1832 return TokError("expected '=' in let expression");
1833 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001834
David Greenee1b46912009-06-08 20:23:18 +00001835 RecordVal *Field = CurRec->getValue(FieldName);
1836 if (Field == 0)
1837 return TokError("Value '" + FieldName + "' unknown!");
1838
1839 RecTy *Type = Field->getType();
Bob Wilson21870412009-11-22 04:24:42 +00001840
David Greene05bce0b2011-07-29 22:43:06 +00001841 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001842 if (Val == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001843
Chris Lattnerf4601652007-11-22 20:49:04 +00001844 if (Lex.getCode() != tgtok::semi)
1845 return TokError("expected ';' after let expression");
1846 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001847
Chris Lattnerf4601652007-11-22 20:49:04 +00001848 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1849}
1850
1851/// ParseBody - Read the body of a class or def. Return true on error, false on
1852/// success.
1853///
1854/// Body ::= ';'
1855/// Body ::= '{' BodyList '}'
1856/// BodyList BodyItem*
1857///
1858bool TGParser::ParseBody(Record *CurRec) {
1859 // If this is a null definition, just eat the semi and return.
1860 if (Lex.getCode() == tgtok::semi) {
1861 Lex.Lex();
1862 return false;
1863 }
Bob Wilson21870412009-11-22 04:24:42 +00001864
Chris Lattnerf4601652007-11-22 20:49:04 +00001865 if (Lex.getCode() != tgtok::l_brace)
1866 return TokError("Expected ';' or '{' to start body");
1867 // Eat the '{'.
1868 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001869
Chris Lattnerf4601652007-11-22 20:49:04 +00001870 while (Lex.getCode() != tgtok::r_brace)
1871 if (ParseBodyItem(CurRec))
1872 return true;
1873
1874 // Eat the '}'.
1875 Lex.Lex();
1876 return false;
1877}
1878
Sean Silva9cceede2013-01-09 04:49:14 +00001879/// \brief Apply the current let bindings to \a CurRec.
1880/// \returns true on error, false otherwise.
1881bool TGParser::ApplyLetStack(Record *CurRec) {
1882 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1883 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1884 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1885 LetStack[i][j].Bits, LetStack[i][j].Value))
1886 return true;
1887 return false;
1888}
1889
Chris Lattnerf4601652007-11-22 20:49:04 +00001890/// ParseObjectBody - Parse the body of a def or class. This consists of an
1891/// optional ClassList followed by a Body. CurRec is the current def or class
1892/// that is being parsed.
1893///
1894/// ObjectBody ::= BaseClassList Body
1895/// BaseClassList ::= /*empty*/
1896/// BaseClassList ::= ':' BaseClassListNE
1897/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1898///
1899bool TGParser::ParseObjectBody(Record *CurRec) {
1900 // If there is a baseclass list, read it.
1901 if (Lex.getCode() == tgtok::colon) {
1902 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001903
Chris Lattnerf4601652007-11-22 20:49:04 +00001904 // Read all of the subclasses.
1905 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1906 while (1) {
1907 // Check for error.
1908 if (SubClass.Rec == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001909
Chris Lattnerf4601652007-11-22 20:49:04 +00001910 // Add it.
1911 if (AddSubClass(CurRec, SubClass))
1912 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001913
Chris Lattnerf4601652007-11-22 20:49:04 +00001914 if (Lex.getCode() != tgtok::comma) break;
1915 Lex.Lex(); // eat ','.
1916 SubClass = ParseSubClassReference(CurRec, false);
1917 }
1918 }
1919
Sean Silva9cceede2013-01-09 04:49:14 +00001920 if (ApplyLetStack(CurRec))
1921 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001922
Chris Lattnerf4601652007-11-22 20:49:04 +00001923 return ParseBody(CurRec);
1924}
1925
Chris Lattnerf4601652007-11-22 20:49:04 +00001926/// ParseDef - Parse and return a top level or multiclass def, return the record
1927/// corresponding to it. This returns null on error.
1928///
1929/// DefInst ::= DEF ObjectName ObjectBody
1930///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001931bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001932 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001933 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson21870412009-11-22 04:24:42 +00001934 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4601652007-11-22 20:49:04 +00001935
1936 // Parse ObjectName and make a record for it.
Jordan Rosed1220092013-01-10 18:50:05 +00001937 Record *CurRec;
1938 Init *Name = ParseObjectName(CurMultiClass);
1939 if (Name)
1940 CurRec = new Record(Name, DefLoc, Records);
1941 else
1942 CurRec = new Record(GetNewAnonymousName(), DefLoc, Records,
1943 /*IsAnonymous=*/true);
Bob Wilson21870412009-11-22 04:24:42 +00001944
Jakob Stoklund Olesen72cba6c2012-05-24 22:17:36 +00001945 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001946 // Top-level def definition.
Bob Wilson21870412009-11-22 04:24:42 +00001947
Chris Lattnerf4601652007-11-22 20:49:04 +00001948 // Ensure redefinition doesn't happen.
David Greene1d501392012-01-28 00:03:24 +00001949 if (Records.getDef(CurRec->getNameInitAsString())) {
David Greene2c49fbb2011-10-19 13:03:45 +00001950 Error(DefLoc, "def '" + CurRec->getNameInitAsString()
1951 + "' already defined");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001952 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001953 }
1954 Records.addDef(CurRec);
Jakob Stoklund Olesen72cba6c2012-05-24 22:17:36 +00001955 } else if (CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001956 // Otherwise, a def inside a multiclass, add it to the multiclass.
1957 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene91919cd2011-10-19 13:03:51 +00001958 if (CurMultiClass->DefPrototypes[i]->getNameInit()
1959 == CurRec->getNameInit()) {
1960 Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
Chris Lattnerf4601652007-11-22 20:49:04 +00001961 "' already defined in this multiclass!");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001962 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001963 }
1964 CurMultiClass->DefPrototypes.push_back(CurRec);
1965 }
Bob Wilson21870412009-11-22 04:24:42 +00001966
Chris Lattnerf4601652007-11-22 20:49:04 +00001967 if (ParseObjectBody(CurRec))
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001968 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001969
Chris Lattnerf4601652007-11-22 20:49:04 +00001970 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
David Greene0d886402011-08-10 18:27:46 +00001971 // See Record::setName(). This resolve step will see any new name
1972 // for the def that might have been created when resolving
1973 // inheritance, values and arguments above.
Chris Lattnerf4601652007-11-22 20:49:04 +00001974 CurRec->resolveReferences();
Bob Wilson21870412009-11-22 04:24:42 +00001975
Chris Lattnerf4601652007-11-22 20:49:04 +00001976 // If ObjectBody has template arguments, it's an error.
1977 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001978
1979 if (CurMultiClass) {
1980 // Copy the template arguments for the multiclass into the def.
David Greenee22b3212011-10-19 13:02:42 +00001981 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001982 CurMultiClass->Rec.getTemplateArgs();
1983
1984 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1985 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1986 assert(RV && "Template arg doesn't exist?");
1987 CurRec->addValue(*RV);
1988 }
1989 }
1990
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00001991 if (ProcessForeachDefs(CurRec, DefLoc)) {
David Greenecebb4ee2012-02-22 16:09:41 +00001992 Error(DefLoc,
1993 "Could not process loops for def" + CurRec->getNameInitAsString());
1994 return true;
1995 }
1996
1997 return false;
1998}
1999
2000/// ParseForeach - Parse a for statement. Return the record corresponding
2001/// to it. This returns true on error.
2002///
2003/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2004/// Foreach ::= FOREACH Declaration IN Object
2005///
2006bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2007 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2008 Lex.Lex(); // Eat the 'for' token.
2009
2010 // Make a temporary object to record items associated with the for
2011 // loop.
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +00002012 ListInit *ListValue = 0;
2013 VarInit *IterName = ParseForeachDeclaration(ListValue);
David Greenecebb4ee2012-02-22 16:09:41 +00002014 if (IterName == 0)
2015 return TokError("expected declaration in for");
2016
2017 if (Lex.getCode() != tgtok::In)
2018 return TokError("Unknown tok");
2019 Lex.Lex(); // Eat the in
2020
2021 // Create a loop object and remember it.
2022 Loops.push_back(ForeachLoop(IterName, ListValue));
2023
2024 if (Lex.getCode() != tgtok::l_brace) {
2025 // FOREACH Declaration IN Object
2026 if (ParseObject(CurMultiClass))
2027 return true;
2028 }
2029 else {
2030 SMLoc BraceLoc = Lex.getLoc();
2031 // Otherwise, this is a group foreach.
2032 Lex.Lex(); // eat the '{'.
2033
2034 // Parse the object list.
2035 if (ParseObjectList(CurMultiClass))
2036 return true;
2037
2038 if (Lex.getCode() != tgtok::r_brace) {
2039 TokError("expected '}' at end of foreach command");
2040 return Error(BraceLoc, "to match this '{'");
2041 }
2042 Lex.Lex(); // Eat the }
2043 }
2044
2045 // We've processed everything in this loop.
2046 Loops.pop_back();
2047
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002048 return false;
Chris Lattnerf4601652007-11-22 20:49:04 +00002049}
2050
Chris Lattnerf4601652007-11-22 20:49:04 +00002051/// ParseClass - Parse a tblgen class definition.
2052///
2053/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2054///
2055bool TGParser::ParseClass() {
2056 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2057 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002058
Chris Lattnerf4601652007-11-22 20:49:04 +00002059 if (Lex.getCode() != tgtok::Id)
2060 return TokError("expected class name after 'class' keyword");
Bob Wilson21870412009-11-22 04:24:42 +00002061
Chris Lattnerf4601652007-11-22 20:49:04 +00002062 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2063 if (CurRec) {
2064 // If the body was previously defined, this is an error.
David Greenee3385652011-10-19 13:04:13 +00002065 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4601652007-11-22 20:49:04 +00002066 !CurRec->getSuperClasses().empty() ||
2067 !CurRec->getTemplateArgs().empty())
David Greene69a23942011-10-19 13:03:58 +00002068 return TokError("Class '" + CurRec->getNameInitAsString()
2069 + "' already defined");
Chris Lattnerf4601652007-11-22 20:49:04 +00002070 } else {
2071 // If this is the first reference to this class, create and add it.
Chris Lattner9c6b60e2010-12-15 04:48:22 +00002072 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00002073 Records.addClass(CurRec);
2074 }
2075 Lex.Lex(); // eat the name.
Bob Wilson21870412009-11-22 04:24:42 +00002076
Chris Lattnerf4601652007-11-22 20:49:04 +00002077 // If there are template args, parse them.
2078 if (Lex.getCode() == tgtok::less)
2079 if (ParseTemplateArgList(CurRec))
2080 return true;
2081
2082 // Finally, parse the object body.
2083 return ParseObjectBody(CurRec);
2084}
2085
2086/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2087/// of LetRecords.
2088///
2089/// LetList ::= LetItem (',' LetItem)*
2090/// LetItem ::= ID OptionalRangeList '=' Value
2091///
2092std::vector<LetRecord> TGParser::ParseLetList() {
2093 std::vector<LetRecord> Result;
Bob Wilson21870412009-11-22 04:24:42 +00002094
Chris Lattnerf4601652007-11-22 20:49:04 +00002095 while (1) {
2096 if (Lex.getCode() != tgtok::Id) {
2097 TokError("expected identifier in let definition");
2098 return std::vector<LetRecord>();
2099 }
2100 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002101 SMLoc NameLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +00002102 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4601652007-11-22 20:49:04 +00002103
2104 // Check for an optional RangeList.
2105 std::vector<unsigned> Bits;
Bob Wilson21870412009-11-22 04:24:42 +00002106 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4601652007-11-22 20:49:04 +00002107 return std::vector<LetRecord>();
2108 std::reverse(Bits.begin(), Bits.end());
Bob Wilson21870412009-11-22 04:24:42 +00002109
Chris Lattnerf4601652007-11-22 20:49:04 +00002110 if (Lex.getCode() != tgtok::equal) {
2111 TokError("expected '=' in let expression");
2112 return std::vector<LetRecord>();
2113 }
2114 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00002115
David Greene05bce0b2011-07-29 22:43:06 +00002116 Init *Val = ParseValue(0);
Chris Lattnerf4601652007-11-22 20:49:04 +00002117 if (Val == 0) return std::vector<LetRecord>();
Bob Wilson21870412009-11-22 04:24:42 +00002118
Chris Lattnerf4601652007-11-22 20:49:04 +00002119 // Now that we have everything, add the record.
2120 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson21870412009-11-22 04:24:42 +00002121
Chris Lattnerf4601652007-11-22 20:49:04 +00002122 if (Lex.getCode() != tgtok::comma)
2123 return Result;
Bob Wilson21870412009-11-22 04:24:42 +00002124 Lex.Lex(); // eat the comma.
Chris Lattnerf4601652007-11-22 20:49:04 +00002125 }
2126}
2127
2128/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002129/// different related productions. This works inside multiclasses too.
Chris Lattnerf4601652007-11-22 20:49:04 +00002130///
2131/// Object ::= LET LetList IN '{' ObjectList '}'
2132/// Object ::= LET LetList IN Object
2133///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002134bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002135 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2136 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002137
Chris Lattnerf4601652007-11-22 20:49:04 +00002138 // Add this entry to the let stack.
2139 std::vector<LetRecord> LetInfo = ParseLetList();
2140 if (LetInfo.empty()) return true;
2141 LetStack.push_back(LetInfo);
2142
2143 if (Lex.getCode() != tgtok::In)
2144 return TokError("expected 'in' at end of top-level 'let'");
2145 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002146
Chris Lattnerf4601652007-11-22 20:49:04 +00002147 // If this is a scalar let, just handle it now
2148 if (Lex.getCode() != tgtok::l_brace) {
2149 // LET LetList IN Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002150 if (ParseObject(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00002151 return true;
2152 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002153 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002154 // Otherwise, this is a group let.
2155 Lex.Lex(); // eat the '{'.
Bob Wilson21870412009-11-22 04:24:42 +00002156
Chris Lattnerf4601652007-11-22 20:49:04 +00002157 // Parse the object list.
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002158 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00002159 return true;
Bob Wilson21870412009-11-22 04:24:42 +00002160
Chris Lattnerf4601652007-11-22 20:49:04 +00002161 if (Lex.getCode() != tgtok::r_brace) {
2162 TokError("expected '}' at end of top level let command");
2163 return Error(BraceLoc, "to match this '{'");
2164 }
2165 Lex.Lex();
2166 }
Bob Wilson21870412009-11-22 04:24:42 +00002167
Chris Lattnerf4601652007-11-22 20:49:04 +00002168 // Outside this let scope, this let block is not active.
2169 LetStack.pop_back();
2170 return false;
2171}
2172
Chris Lattnerf4601652007-11-22 20:49:04 +00002173/// ParseMultiClass - Parse a multiclass definition.
2174///
Bob Wilson32558652009-04-28 19:41:44 +00002175/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silva9302dcc2013-01-09 02:11:55 +00002176/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2177/// MultiClassObject ::= DefInst
2178/// MultiClassObject ::= MultiClassInst
2179/// MultiClassObject ::= DefMInst
2180/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2181/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4601652007-11-22 20:49:04 +00002182///
2183bool TGParser::ParseMultiClass() {
2184 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2185 Lex.Lex(); // Eat the multiclass token.
2186
2187 if (Lex.getCode() != tgtok::Id)
2188 return TokError("expected identifier after multiclass for name");
2189 std::string Name = Lex.getCurStrVal();
Bob Wilson21870412009-11-22 04:24:42 +00002190
Chris Lattnerf4601652007-11-22 20:49:04 +00002191 if (MultiClasses.count(Name))
2192 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson21870412009-11-22 04:24:42 +00002193
Chris Lattner67db8832010-12-13 00:23:57 +00002194 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
2195 Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00002196 Lex.Lex(); // Eat the identifier.
Bob Wilson21870412009-11-22 04:24:42 +00002197
Chris Lattnerf4601652007-11-22 20:49:04 +00002198 // If there are template args, parse them.
2199 if (Lex.getCode() == tgtok::less)
2200 if (ParseTemplateArgList(0))
2201 return true;
2202
David Greened34a73b2009-04-24 16:55:41 +00002203 bool inherits = false;
2204
David Greenede444af2009-04-22 16:42:54 +00002205 // If there are submulticlasses, parse them.
2206 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00002207 inherits = true;
2208
David Greenede444af2009-04-22 16:42:54 +00002209 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00002210
David Greenede444af2009-04-22 16:42:54 +00002211 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00002212 SubMultiClassReference SubMultiClass =
2213 ParseSubMultiClassReference(CurMultiClass);
David Greenede444af2009-04-22 16:42:54 +00002214 while (1) {
2215 // Check for error.
2216 if (SubMultiClass.MC == 0) return true;
Bob Wilson32558652009-04-28 19:41:44 +00002217
David Greenede444af2009-04-22 16:42:54 +00002218 // Add it.
2219 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2220 return true;
Bob Wilson32558652009-04-28 19:41:44 +00002221
David Greenede444af2009-04-22 16:42:54 +00002222 if (Lex.getCode() != tgtok::comma) break;
2223 Lex.Lex(); // eat ','.
2224 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2225 }
2226 }
2227
David Greened34a73b2009-04-24 16:55:41 +00002228 if (Lex.getCode() != tgtok::l_brace) {
2229 if (!inherits)
2230 return TokError("expected '{' in multiclass definition");
Bob Wilson21870412009-11-22 04:24:42 +00002231 else if (Lex.getCode() != tgtok::semi)
2232 return TokError("expected ';' in multiclass definition");
David Greened34a73b2009-04-24 16:55:41 +00002233 else
Bob Wilson21870412009-11-22 04:24:42 +00002234 Lex.Lex(); // eat the ';'.
2235 } else {
David Greened34a73b2009-04-24 16:55:41 +00002236 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2237 return TokError("multiclass must contain at least one def");
Bob Wilson21870412009-11-22 04:24:42 +00002238
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002239 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002240 switch (Lex.getCode()) {
2241 default:
David Greenea1b1b792011-10-07 18:25:05 +00002242 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002243 case tgtok::Let:
2244 case tgtok::Def:
2245 case tgtok::Defm:
David Greenecebb4ee2012-02-22 16:09:41 +00002246 case tgtok::Foreach:
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002247 if (ParseObject(CurMultiClass))
2248 return true;
2249 break;
2250 }
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002251 }
David Greened34a73b2009-04-24 16:55:41 +00002252 Lex.Lex(); // eat the '}'.
2253 }
Bob Wilson21870412009-11-22 04:24:42 +00002254
Chris Lattnerf4601652007-11-22 20:49:04 +00002255 CurMultiClass = 0;
2256 return false;
2257}
2258
David Greenee499a2d2011-10-05 22:42:07 +00002259Record *TGParser::
2260InstantiateMulticlassDef(MultiClass &MC,
2261 Record *DefProto,
David Greene7be867e2011-10-19 13:04:31 +00002262 Init *DefmPrefix,
Jordan Roseb50df4a2013-01-10 18:50:11 +00002263 SMRange DefmPrefixRange) {
David Greene7be867e2011-10-19 13:04:31 +00002264 // We need to preserve DefProto so it can be reused for later
2265 // instantiations, so create a new Record to inherit from it.
2266
David Greenee499a2d2011-10-05 22:42:07 +00002267 // Add in the defm name. If the defm prefix is empty, give each
2268 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2269 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2270 // as a prefix.
David Greenee499a2d2011-10-05 22:42:07 +00002271
Jordan Rosed1220092013-01-10 18:50:05 +00002272 bool IsAnonymous = false;
2273 if (DefmPrefix == 0) {
David Greene7be867e2011-10-19 13:04:31 +00002274 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Rosed1220092013-01-10 18:50:05 +00002275 IsAnonymous = true;
2276 }
David Greene7be867e2011-10-19 13:04:31 +00002277
2278 Init *DefName = DefProto->getNameInit();
2279
Sean Silva6cfc8062012-10-10 20:24:43 +00002280 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene7be867e2011-10-19 13:04:31 +00002281
David Greened3d1cad2011-10-19 13:04:43 +00002282 if (DefNameString != 0) {
2283 // We have a fully expanded string so there are no operators to
2284 // resolve. We should concatenate the given prefix and name.
David Greene7be867e2011-10-19 13:04:31 +00002285 DefName =
2286 BinOpInit::get(BinOpInit::STRCONCAT,
2287 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2288 StringRecTy::get())->Fold(DefProto, &MC),
2289 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2290 }
David Greene7be867e2011-10-19 13:04:31 +00002291
Jakob Stoklund Olesen376a8a72012-08-22 23:33:58 +00002292 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Roseb50df4a2013-01-10 18:50:11 +00002293 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesen376a8a72012-08-22 23:33:58 +00002294 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Jordan Rosed1220092013-01-10 18:50:05 +00002295 Record *CurRec = new Record(DefName, Locs, Records, IsAnonymous);
David Greenee499a2d2011-10-05 22:42:07 +00002296
2297 SubClassReference Ref;
Jordan Roseb50df4a2013-01-10 18:50:11 +00002298 Ref.RefRange = DefmPrefixRange;
David Greenee499a2d2011-10-05 22:42:07 +00002299 Ref.Rec = DefProto;
2300 AddSubClass(CurRec, Ref);
2301
Jim Grosbachcfbda4a2012-08-02 18:46:42 +00002302 // Set the value for NAME. We don't resolve references to it 'til later,
2303 // though, so that uses in nested multiclass names don't get
2304 // confused.
Jordan Roseb50df4a2013-01-10 18:50:11 +00002305 if (SetValue(CurRec, Ref.RefRange.Start, "NAME", std::vector<unsigned>(),
Jim Grosbachcfbda4a2012-08-02 18:46:42 +00002306 DefmPrefix)) {
Jordan Roseb50df4a2013-01-10 18:50:11 +00002307 Error(DefmPrefixRange.Start, "Could not resolve "
Jim Grosbachcfbda4a2012-08-02 18:46:42 +00002308 + CurRec->getNameInitAsString() + ":NAME to '"
2309 + DefmPrefix->getAsUnquotedString() + "'");
2310 return 0;
2311 }
David Greenee5b252f2011-10-19 13:04:35 +00002312
Jim Grosbachcfbda4a2012-08-02 18:46:42 +00002313 // If the DefNameString didn't resolve, we probably have a reference to
2314 // NAME and need to replace it. We need to do at least this much greedily,
2315 // otherwise nested multiclasses will end up with incorrect NAME expansions.
2316 if (DefNameString == 0) {
David Greenee5b252f2011-10-19 13:04:35 +00002317 RecordVal *DefNameRV = CurRec->getValue("NAME");
2318 CurRec->resolveReferencesTo(DefNameRV);
2319 }
2320
2321 if (!CurMultiClass) {
Jim Grosbachcfbda4a2012-08-02 18:46:42 +00002322 // Now that we're at the top level, resolve all NAME references
2323 // in the resultant defs that weren't in the def names themselves.
2324 RecordVal *DefNameRV = CurRec->getValue("NAME");
2325 CurRec->resolveReferencesTo(DefNameRV);
2326
2327 // Now that NAME references are resolved and we're at the top level of
2328 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greenee5b252f2011-10-19 13:04:35 +00002329 // currently in a multiclass, it means this defm appears inside a
2330 // multiclass and its name won't be fully resolvable until we see
2331 // the top-level defm. Therefore, we don't add this to the
2332 // RecordKeeper at this point. If we did we could get duplicate
2333 // defs as more than one probably refers to NAME or some other
2334 // common internal placeholder.
2335
2336 // Ensure redefinition doesn't happen.
2337 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Roseb50df4a2013-01-10 18:50:11 +00002338 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greenee5b252f2011-10-19 13:04:35 +00002339 "' already defined, instantiating defm with subdef '" +
2340 DefProto->getNameInitAsString() + "'");
2341 return 0;
2342 }
2343
2344 Records.addDef(CurRec);
2345 }
2346
David Greenee499a2d2011-10-05 22:42:07 +00002347 return CurRec;
2348}
2349
2350bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2351 Record *CurRec,
2352 SMLoc DefmPrefixLoc,
2353 SMLoc SubClassLoc,
David Greenee22b3212011-10-19 13:02:42 +00002354 const std::vector<Init *> &TArgs,
David Greenee499a2d2011-10-05 22:42:07 +00002355 std::vector<Init *> &TemplateVals,
2356 bool DeleteArgs) {
2357 // Loop over all of the template arguments, setting them to the specified
2358 // value or leaving them as the default if necessary.
2359 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2360 // Check if a value is specified for this temp-arg.
2361 if (i < TemplateVals.size()) {
2362 // Set it now.
2363 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2364 TemplateVals[i]))
2365 return true;
2366
2367 // Resolve it next.
2368 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2369
2370 if (DeleteArgs)
2371 // Now remove it.
2372 CurRec->removeValue(TArgs[i]);
2373
2374 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2375 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenee22b3212011-10-19 13:02:42 +00002376 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2377 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2378 + "'");
David Greenee499a2d2011-10-05 22:42:07 +00002379 }
2380 }
2381 return false;
2382}
2383
2384bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2385 Record *CurRec,
2386 Record *DefProto,
2387 SMLoc DefmPrefixLoc) {
2388 // If the mdef is inside a 'let' expression, add to each def.
Sean Silva9cceede2013-01-09 04:49:14 +00002389 if (ApplyLetStack(CurRec))
2390 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenee499a2d2011-10-05 22:42:07 +00002391
David Greenee499a2d2011-10-05 22:42:07 +00002392 // Don't create a top level definition for defm inside multiclasses,
2393 // instead, only update the prototypes and bind the template args
2394 // with the new created definition.
Sean Silva699b8702013-01-09 05:28:12 +00002395 if (!CurMultiClass)
2396 return false;
2397 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2398 i != e; ++i)
2399 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2400 == CurRec->getNameInit())
2401 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2402 "' already defined in this multiclass!");
2403 CurMultiClass->DefPrototypes.push_back(CurRec);
David Greenee499a2d2011-10-05 22:42:07 +00002404
Sean Silva699b8702013-01-09 05:28:12 +00002405 // Copy the template arguments for the multiclass into the new def.
2406 const std::vector<Init *> &TA =
2407 CurMultiClass->Rec.getTemplateArgs();
David Greenee499a2d2011-10-05 22:42:07 +00002408
Sean Silva699b8702013-01-09 05:28:12 +00002409 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2410 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2411 assert(RV && "Template arg doesn't exist?");
2412 CurRec->addValue(*RV);
David Greenee499a2d2011-10-05 22:42:07 +00002413 }
2414
2415 return false;
2416}
2417
Chris Lattnerf4601652007-11-22 20:49:04 +00002418/// ParseDefm - Parse the instantiation of a multiclass.
2419///
2420/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2421///
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002422bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002423 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Roseb50df4a2013-01-10 18:50:11 +00002424 SMLoc DefmLoc = Lex.getLoc();
David Greenea9e07dd2011-10-19 13:04:29 +00002425 Init *DefmPrefix = 0;
David Greenea9e07dd2011-10-19 13:04:29 +00002426
Craig Topper6a59f5a2013-01-07 05:09:33 +00002427 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greenea9e07dd2011-10-19 13:04:29 +00002428 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002429 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +00002430
Jordan Roseb50df4a2013-01-10 18:50:11 +00002431 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002432 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4601652007-11-22 20:49:04 +00002433 return TokError("expected ':' after defm identifier");
Bob Wilson21870412009-11-22 04:24:42 +00002434
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002435 // Keep track of the new generated record definitions.
2436 std::vector<Record*> NewRecDefs;
2437
2438 // This record also inherits from a regular class (non-multiclass)?
2439 bool InheritFromClass = false;
2440
Chris Lattnerf4601652007-11-22 20:49:04 +00002441 // eat the colon.
2442 Lex.Lex();
2443
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002444 SMLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002445 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene56546132009-04-22 22:17:51 +00002446
2447 while (1) {
2448 if (Ref.Rec == 0) return true;
2449
2450 // To instantiate a multiclass, we need to first get the multiclass, then
2451 // instantiate each def contained in the multiclass with the SubClassRef
2452 // template parameters.
2453 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2454 assert(MC && "Didn't lookup multiclass correctly?");
David Greene05bce0b2011-07-29 22:43:06 +00002455 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greene56546132009-04-22 22:17:51 +00002456
2457 // Verify that the correct number of template arguments were specified.
David Greenee22b3212011-10-19 13:02:42 +00002458 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greene56546132009-04-22 22:17:51 +00002459 if (TArgs.size() < TemplateVals.size())
2460 return Error(SubClassLoc,
2461 "more template args specified than multiclass expects");
2462
2463 // Loop over all the def's in the multiclass, instantiating each one.
2464 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2465 Record *DefProto = MC->DefPrototypes[i];
2466
Jordan Roseb50df4a2013-01-10 18:50:11 +00002467 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix,
2468 SMRange(DefmLoc,
2469 DefmPrefixEndLoc));
Jim Grosbach94f2dc92011-12-02 18:33:03 +00002470 if (!CurRec)
2471 return true;
David Greene065f2592009-05-05 16:28:25 +00002472
Jordan Roseb50df4a2013-01-10 18:50:11 +00002473 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenee499a2d2011-10-05 22:42:07 +00002474 TArgs, TemplateVals, true/*Delete args*/))
2475 return Error(SubClassLoc, "could not instantiate def");
David Greene56546132009-04-22 22:17:51 +00002476
Jordan Roseb50df4a2013-01-10 18:50:11 +00002477 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmLoc))
David Greenee499a2d2011-10-05 22:42:07 +00002478 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002479
2480 NewRecDefs.push_back(CurRec);
David Greene56546132009-04-22 22:17:51 +00002481 }
2482
David Greenee499a2d2011-10-05 22:42:07 +00002483
David Greene56546132009-04-22 22:17:51 +00002484 if (Lex.getCode() != tgtok::comma) break;
2485 Lex.Lex(); // eat ','.
2486
2487 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002488
2489 // A defm can inherit from regular classes (non-multiclass) as
2490 // long as they come in the end of the inheritance list.
2491 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0);
2492
2493 if (InheritFromClass)
2494 break;
2495
David Greene56546132009-04-22 22:17:51 +00002496 Ref = ParseSubClassReference(0, true);
2497 }
2498
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002499 if (InheritFromClass) {
2500 // Process all the classes to inherit as if they were part of a
2501 // regular 'def' and inherit all record values.
2502 SubClassReference SubClass = ParseSubClassReference(0, false);
2503 while (1) {
2504 // Check for error.
2505 if (SubClass.Rec == 0) return true;
2506
2507 // Get the expanded definition prototypes and teach them about
2508 // the record values the current class to inherit has
2509 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2510 Record *CurRec = NewRecDefs[i];
2511
2512 // Add it.
2513 if (AddSubClass(CurRec, SubClass))
2514 return true;
2515
Sean Silva9cceede2013-01-09 04:49:14 +00002516 if (ApplyLetStack(CurRec))
2517 return true;
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002518 }
2519
2520 if (Lex.getCode() != tgtok::comma) break;
2521 Lex.Lex(); // eat ','.
2522 SubClass = ParseSubClassReference(0, false);
2523 }
2524 }
2525
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002526 if (!CurMultiClass)
2527 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene0d886402011-08-10 18:27:46 +00002528 // See Record::setName(). This resolve step will see any new
2529 // name for the def that might have been created when resolving
2530 // inheritance, values and arguments above.
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002531 NewRecDefs[i]->resolveReferences();
2532
Chris Lattnerf4601652007-11-22 20:49:04 +00002533 if (Lex.getCode() != tgtok::semi)
2534 return TokError("expected ';' at end of defm");
2535 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002536
Chris Lattnerf4601652007-11-22 20:49:04 +00002537 return false;
2538}
2539
2540/// ParseObject
2541/// Object ::= ClassInst
2542/// Object ::= DefInst
2543/// Object ::= MultiClassInst
2544/// Object ::= DefMInst
2545/// Object ::= LETCommand '{' ObjectList '}'
2546/// Object ::= LETCommand Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002547bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002548 switch (Lex.getCode()) {
Chris Lattnerd6d9dd92010-10-31 19:27:15 +00002549 default:
2550 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002551 case tgtok::Let: return ParseTopLevelLet(MC);
2552 case tgtok::Def: return ParseDef(MC);
David Greenecebb4ee2012-02-22 16:09:41 +00002553 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002554 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4601652007-11-22 20:49:04 +00002555 case tgtok::Class: return ParseClass();
2556 case tgtok::MultiClass: return ParseMultiClass();
2557 }
2558}
2559
2560/// ParseObjectList
2561/// ObjectList :== Object*
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002562bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002563 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002564 if (ParseObject(MC))
Chris Lattnerf4601652007-11-22 20:49:04 +00002565 return true;
2566 }
2567 return false;
2568}
2569
Chris Lattnerf4601652007-11-22 20:49:04 +00002570bool TGParser::ParseFile() {
2571 Lex.Lex(); // Prime the lexer.
2572 if (ParseObjectList()) return true;
Bob Wilson21870412009-11-22 04:24:42 +00002573
Chris Lattnerf4601652007-11-22 20:49:04 +00002574 // If we have unread input at the end of the file, report it.
2575 if (Lex.getCode() == tgtok::Eof)
2576 return false;
Bob Wilson21870412009-11-22 04:24:42 +00002577
Chris Lattnerf4601652007-11-22 20:49:04 +00002578 return TokError("Unexpected input at top level");
2579}
2580