blob: be5a5242518ee70e1610182452a2b21291f78370 [file] [log] [blame]
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001//===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-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 Lattnerf4127dd2007-11-22 20:49:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Parser for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
14#include "TGParser.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallVector.h"
Chris Lattnerf4127dd2007-11-22 20:49:04 +000016#include "llvm/ADT/StringExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/Support/CommandLine.h"
18#include "llvm/TableGen/Record.h"
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000019#include <algorithm>
20#include <sstream>
Chris Lattnerf4127dd2007-11-22 20:49:04 +000021using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24// Support Code for the Semantic Actions.
25//===----------------------------------------------------------------------===//
26
27namespace llvm {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000028struct SubClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000029 SMRange RefRange;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000030 Record *Rec;
David Greeneaf8ee2c2011-07-29 22:43:06 +000031 std::vector<Init*> TemplateArgs;
Chris Lattner87710ca2009-03-13 16:01:53 +000032 SubClassReference() : Rec(0) {}
David Greene7049e792009-04-24 16:55:41 +000033
Chris Lattnerf4127dd2007-11-22 20:49:04 +000034 bool isInvalid() const { return Rec == 0; }
35};
David Greene753ed8f2009-04-22 16:42:54 +000036
37struct SubMultiClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000038 SMRange RefRange;
David Greene753ed8f2009-04-22 16:42:54 +000039 MultiClass *MC;
David Greeneaf8ee2c2011-07-29 22:43:06 +000040 std::vector<Init*> TemplateArgs;
David Greene753ed8f2009-04-22 16:42:54 +000041 SubMultiClassReference() : MC(0) {}
Bob Wilson3d948162009-04-28 19:41:44 +000042
David Greene753ed8f2009-04-22 16:42:54 +000043 bool isInvalid() const { return MC == 0; }
David Greene7049e792009-04-24 16:55:41 +000044 void dump() const;
David Greene753ed8f2009-04-22 16:42:54 +000045};
David Greene7049e792009-04-24 16:55:41 +000046
47void SubMultiClassReference::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000048 errs() << "Multiclass:\n";
Bob Wilson7248f862009-11-22 04:24:42 +000049
David Greene7049e792009-04-24 16:55:41 +000050 MC->dump();
Bob Wilson7248f862009-11-22 04:24:42 +000051
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000052 errs() << "Template args:\n";
David Greeneaf8ee2c2011-07-29 22:43:06 +000053 for (std::vector<Init *>::const_iterator i = TemplateArgs.begin(),
David Greene7049e792009-04-24 16:55:41 +000054 iend = TemplateArgs.end();
55 i != iend;
56 ++i) {
57 (*i)->dump();
58 }
59}
60
Chris Lattnerf4127dd2007-11-22 20:49:04 +000061} // end namespace llvm
62
Chris Lattner526c8cb2009-06-21 03:39:35 +000063bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000064 if (CurRec == 0)
65 CurRec = &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +000066
Jakob Stoklund Olesen9d1c5ee2012-01-13 03:16:35 +000067 if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
Chris Lattnerf4127dd2007-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 Wilson7248f862009-11-22 04:24:42 +000072 "previous definition of type '" +
Chris Lattnerf4127dd2007-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 Greene3ca42122011-10-19 13:02:39 +000082bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
David Greeneaf8ee2c2011-07-29 22:43:06 +000083 const std::vector<unsigned> &BitList, Init *V) {
Chris Lattnerf4127dd2007-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 Greene3ca42122011-10-19 13:02:39 +000090 return Error(Loc, "Value '" + ValName->getAsUnquotedString()
91 + "' unknown!");
Chris Lattnerf4127dd2007-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 Silvafb509ed2012-10-10 20:24:43 +000096 if (VarInit *VI = dyn_cast<VarInit>(V))
David Greene3ca42122011-10-19 13:02:39 +000097 if (VI->getNameInit() == ValName)
Chris Lattnerf4127dd2007-11-22 20:49:04 +000098 return false;
Bob Wilson7248f862009-11-22 04:24:42 +000099
Chris Lattnerf4127dd2007-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 Silvafb509ed2012-10-10 20:24:43 +0000105 BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000106 if (CurVal == 0)
David Greene3ca42122011-10-19 13:02:39 +0000107 return Error(Loc, "Value '" + ValName->getAsUnquotedString()
108 + "' is not a bits type");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000109
110 // Convert the incoming value to a bits type of the appropriate size...
David Greeneaf8ee2c2011-07-29 22:43:06 +0000111 Init *BI = V->convertInitializerTo(BitsRecTy::get(BitList.size()));
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000112 if (BI == 0) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000113 return Error(Loc, "Initializer is not compatible with bit range");
114 }
Bob Wilson7248f862009-11-22 04:24:42 +0000115
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000116 // We should have a BitsInit type now.
Sean Silvafb509ed2012-10-10 20:24:43 +0000117 BitsInit *BInit = dyn_cast<BitsInit>(BI);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000118 assert(BInit != 0);
119
David Greeneaf8ee2c2011-07-29 22:43:06 +0000120 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4127dd2007-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 Greeneb3da8122011-07-29 19:07:00 +0000125 if (NewBits[Bit])
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000126 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
David Greene3ca42122011-10-19 13:02:39 +0000127 ValName->getAsUnquotedString() + "' more than once");
David Greeneb3da8122011-07-29 19:07:00 +0000128 NewBits[Bit] = BInit->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000129 }
130
131 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
David Greeneb3da8122011-07-29 19:07:00 +0000132 if (NewBits[i] == 0)
133 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000134
David Greenee32ebf22011-07-29 19:07:07 +0000135 V = BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000136 }
137
138 if (RV->setValue(V))
David Greene3ca42122011-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 Lattnerf4127dd2007-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 Venetd1e179d2009-02-14 16:06:42 +0000148bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4127dd2007-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 Rosef12e8a92013-01-10 18:50:11 +0000153 if (AddValue(CurRec, SubClass.RefRange.Start, Vals[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000154 return true;
155
David Greenedb10e692011-10-19 13:02:42 +0000156 const std::vector<Init *> &TArgs = SC->getTemplateArgs();
Chris Lattnerf4127dd2007-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 Rosef12e8a92013-01-10 18:50:11 +0000160 return Error(SubClass.RefRange.Start,
161 "More template args specified than expected");
Bob Wilson7248f862009-11-22 04:24:42 +0000162
Chris Lattnerf4127dd2007-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 Rosef12e8a92013-01-10 18:50:11 +0000168 if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
169 std::vector<unsigned>(), SubClass.TemplateArgs[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000170 return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000171
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000172 // Resolve it next.
173 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson7248f862009-11-22 04:24:42 +0000174
Chris Lattnerf4127dd2007-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 Rosef12e8a92013-01-10 18:50:11 +0000179 return Error(SubClass.RefRange.Start,
180 "Value not specified for template argument #"
David Greenedb10e692011-10-19 13:02:42 +0000181 + utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
182 + ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4127dd2007-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 Rosef12e8a92013-01-10 18:50:11 +0000189 ArrayRef<SMRange> SCRanges = SC->getSuperClassRanges();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000190 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
191 if (CurRec->isSubClassOf(SCs[i]))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000192 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000193 "Already subclass of '" + SCs[i]->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000194 CurRec->addSuperClass(SCs[i], SCRanges[i]);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000195 }
Bob Wilson7248f862009-11-22 04:24:42 +0000196
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000197 if (CurRec->isSubClassOf(SC))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000198 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000199 "Already subclass of '" + SC->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000200 CurRec->addSuperClass(SC, SubClass.RefRange);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000201 return false;
202}
203
David Greene753ed8f2009-04-22 16:42:54 +0000204/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilsonf71e6562009-04-30 18:26:19 +0000205/// CurMC, resolving its template args as SubMultiClass's
David Greene753ed8f2009-04-22 16:42:54 +0000206/// template arguments.
Bob Wilsonf71e6562009-04-30 18:26:19 +0000207bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson92ab8202009-04-30 17:46:20 +0000208 SubMultiClassReference &SubMultiClass) {
David Greene753ed8f2009-04-22 16:42:54 +0000209 MultiClass *SMC = SubMultiClass.MC;
Bob Wilsonf71e6562009-04-30 18:26:19 +0000210 Record *CurRec = &CurMC->Rec;
David Greene753ed8f2009-04-22 16:42:54 +0000211
Bob Wilsonf71e6562009-04-30 18:26:19 +0000212 const std::vector<RecordVal> &MCVals = CurRec->getValues();
David Greene753ed8f2009-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 Rosef12e8a92013-01-10 18:50:11 +0000217 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVals[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000218 return true;
219
Bob Wilsonf71e6562009-04-30 18:26:19 +0000220 int newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000221
David Greene753ed8f2009-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 Rosef12e8a92013-01-10 18:50:11 +0000232 if (AddValue(NewDef, SubMultiClass.RefRange.Start, MCVals[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000233 return true;
234
Bob Wilsonf71e6562009-04-30 18:26:19 +0000235 CurMC->DefPrototypes.push_back(NewDef);
David Greene753ed8f2009-04-22 16:42:54 +0000236 }
Bob Wilson3d948162009-04-28 19:41:44 +0000237
David Greenedb10e692011-10-19 13:02:42 +0000238 const std::vector<Init *> &SMCTArgs = SMC->Rec.getTemplateArgs();
David Greene753ed8f2009-04-22 16:42:54 +0000239
David Greene7049e792009-04-24 16:55:41 +0000240 // Ensure that an appropriate number of template arguments are
241 // specified.
David Greene753ed8f2009-04-22 16:42:54 +0000242 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000243 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000244 "More template args specified than expected");
Bob Wilson3d948162009-04-28 19:41:44 +0000245
David Greene753ed8f2009-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 Greene7049e792009-04-24 16:55:41 +0000250 // If a value is specified for this template arg, set it in the
251 // superclass now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000252 if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson3d948162009-04-28 19:41:44 +0000253 std::vector<unsigned>(),
David Greene753ed8f2009-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 Wilson3d948162009-04-28 19:41:44 +0000259
David Greene753ed8f2009-04-22 16:42:54 +0000260 // Now remove it.
261 CurRec->removeValue(SMCTArgs[i]);
262
David Greene7049e792009-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 Wilsonf71e6562009-04-30 18:26:19 +0000266 CurMC->DefPrototypes.begin() + newDefStart,
267 jend = CurMC->DefPrototypes.end();
David Greene753ed8f2009-04-22 16:42:54 +0000268 j != jend;
269 ++j) {
270 Record *Def = *j;
271
Jordan Rosef12e8a92013-01-10 18:50:11 +0000272 if (SetValue(Def, SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson3d948162009-04-28 19:41:44 +0000273 std::vector<unsigned>(),
David Greene753ed8f2009-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 Rosef12e8a92013-01-10 18:50:11 +0000284 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000285 "Value not specified for template argument #"
David Greenedb10e692011-10-19 13:02:42 +0000286 + utostr(i) + " (" + SMCTArgs[i]->getAsUnquotedString()
287 + ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000288 }
289 }
290
291 return false;
292}
293
David Greenefb927af2012-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 Olesen8a120b12012-05-24 22:17:33 +0000297bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
298 if (Loops.empty())
299 return false;
300
David Greenefb927af2012-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 Olesen8a120b12012-05-24 22:17:33 +0000305 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenefb927af2012-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 Olesen8a120b12012-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 Silvafb509ed2012-10-10 20:24:43 +0000316 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000317 if (List == 0) {
318 Error(Loc, "Loop list is not a list");
319 return true;
320 }
David Greenefb927af2012-02-22 16:09:41 +0000321
Jakob Stoklund Olesen8a120b12012-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 Silvafb509ed2012-10-10 20:24:43 +0000341 TypedInit *IVal = dyn_cast<TypedInit>(IterVals[i].IterValue);
Jakob Stoklund Olesen8a120b12012-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 Greenefb927af2012-02-22 16:09:41 +0000364 return true;
365 }
366
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000367 Records.addDef(IterRec);
368 IterRec->resolveReferences();
David Greenefb927af2012-02-22 16:09:41 +0000369 return false;
370}
371
Chris Lattnerf4127dd2007-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 Greenefb927af2012-02-22 16:09:41 +0000379 K == tgtok::Defm || K == tgtok::Let ||
380 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000381}
382
Alp Tokerce91fe52013-12-21 18:51:00 +0000383/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
384/// an identifier.
385std::string TGParser::GetNewAnonymousName() {
Michael J. Spencer2bb7db92013-02-26 21:29:47 +0000386 unsigned Tmp = AnonCounter++; // MSVC2012 ICEs without this.
Alp Tokerce91fe52013-12-21 18:51:00 +0000387 return "anonymous_" + utostr(Tmp);
Chris Lattner7538ed82010-10-05 22:51:56 +0000388}
389
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000390/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000391/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000392/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000393/// ObjectName ::= /*empty*/
394///
David Greene2affd672011-10-19 13:04:29 +0000395Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
396 switch (Lex.getCode()) {
397 case tgtok::colon:
398 case tgtok::semi:
399 case tgtok::l_brace:
400 // These are all of the tokens that can begin an object body.
401 // Some of these can also begin values but we disallow those cases
402 // because they are unlikely to be useful.
Jordan Roseabdd99b2013-01-10 18:50:05 +0000403 return 0;
David Greene2affd672011-10-19 13:04:29 +0000404 default:
405 break;
406 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000407
David Greene2affd672011-10-19 13:04:29 +0000408 Record *CurRec = 0;
409 if (CurMultiClass)
410 CurRec = &CurMultiClass->Rec;
411
412 RecTy *Type = 0;
413 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000414 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000415 if (!CurRecName) {
416 TokError("Record name is not typed!");
417 return 0;
418 }
419 Type = CurRecName->getType();
420 }
421
422 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000423}
424
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000425/// ParseClassID - Parse and resolve a reference to a class name. This returns
426/// null on error.
427///
428/// ClassID ::= ID
429///
430Record *TGParser::ParseClassID() {
431 if (Lex.getCode() != tgtok::Id) {
432 TokError("expected name for ClassID");
433 return 0;
434 }
Bob Wilson7248f862009-11-22 04:24:42 +0000435
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000436 Record *Result = Records.getClass(Lex.getCurStrVal());
437 if (Result == 0)
438 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000439
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000440 Lex.Lex();
441 return Result;
442}
443
Bob Wilson3d948162009-04-28 19:41:44 +0000444/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
445/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000446///
447/// MultiClassID ::= ID
448///
449MultiClass *TGParser::ParseMultiClassID() {
450 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000451 TokError("expected name for MultiClassID");
David Greene753ed8f2009-04-22 16:42:54 +0000452 return 0;
453 }
Bob Wilson3d948162009-04-28 19:41:44 +0000454
David Greene753ed8f2009-04-22 16:42:54 +0000455 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
456 if (Result == 0)
Sean Silva710c3ae2013-01-09 02:11:57 +0000457 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000458
David Greene753ed8f2009-04-22 16:42:54 +0000459 Lex.Lex();
460 return Result;
461}
462
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000463/// ParseSubClassReference - Parse a reference to a subclass or to a templated
464/// subclass. This returns a SubClassRefTy with a null Record* on error.
465///
466/// SubClassRef ::= ClassID
467/// SubClassRef ::= ClassID '<' ValueList '>'
468///
469SubClassReference TGParser::
470ParseSubClassReference(Record *CurRec, bool isDefm) {
471 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000472 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000473
Sean Silva0657b402013-01-09 02:17:14 +0000474 if (isDefm) {
475 if (MultiClass *MC = ParseMultiClassID())
476 Result.Rec = &MC->Rec;
477 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000478 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000479 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000480 if (Result.Rec == 0) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000481
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000482 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000483 if (Lex.getCode() != tgtok::less) {
484 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000485 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000486 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000487 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000488
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000489 if (Lex.getCode() == tgtok::greater) {
490 TokError("subclass reference requires a non-empty list of template values");
491 Result.Rec = 0;
492 return Result;
493 }
Bob Wilson7248f862009-11-22 04:24:42 +0000494
David Greene8618f952009-06-08 20:23:18 +0000495 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000496 if (Result.TemplateArgs.empty()) {
497 Result.Rec = 0; // Error parsing value list.
498 return Result;
499 }
Bob Wilson7248f862009-11-22 04:24:42 +0000500
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000501 if (Lex.getCode() != tgtok::greater) {
502 TokError("expected '>' in template value list");
503 Result.Rec = 0;
504 return Result;
505 }
506 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000507 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000508
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000509 return Result;
510}
511
Bob Wilson3d948162009-04-28 19:41:44 +0000512/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
513/// templated submulticlass. This returns a SubMultiClassRefTy with a null
514/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000515///
516/// SubMultiClassRef ::= MultiClassID
517/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
518///
519SubMultiClassReference TGParser::
520ParseSubMultiClassReference(MultiClass *CurMC) {
521 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000522 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000523
David Greene753ed8f2009-04-22 16:42:54 +0000524 Result.MC = ParseMultiClassID();
525 if (Result.MC == 0) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000526
David Greene753ed8f2009-04-22 16:42:54 +0000527 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000528 if (Lex.getCode() != tgtok::less) {
529 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000530 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000531 }
David Greene753ed8f2009-04-22 16:42:54 +0000532 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000533
David Greene753ed8f2009-04-22 16:42:54 +0000534 if (Lex.getCode() == tgtok::greater) {
535 TokError("subclass reference requires a non-empty list of template values");
536 Result.MC = 0;
537 return Result;
538 }
Bob Wilson3d948162009-04-28 19:41:44 +0000539
David Greene8618f952009-06-08 20:23:18 +0000540 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000541 if (Result.TemplateArgs.empty()) {
542 Result.MC = 0; // Error parsing value list.
543 return Result;
544 }
Bob Wilson3d948162009-04-28 19:41:44 +0000545
David Greene753ed8f2009-04-22 16:42:54 +0000546 if (Lex.getCode() != tgtok::greater) {
547 TokError("expected '>' in template value list");
548 Result.MC = 0;
549 return Result;
550 }
551 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000552 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000553
554 return Result;
555}
556
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000557/// ParseRangePiece - Parse a bit/value range.
558/// RangePiece ::= INTVAL
559/// RangePiece ::= INTVAL '-' INTVAL
560/// RangePiece ::= INTVAL INTVAL
561bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000562 if (Lex.getCode() != tgtok::IntVal) {
563 TokError("expected integer or bitrange");
564 return true;
565 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000566 int64_t Start = Lex.getCurIntVal();
567 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000568
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000569 if (Start < 0)
570 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000571
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000572 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000573 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000574 Ranges.push_back(Start);
575 return false;
576 case tgtok::minus:
577 if (Lex.Lex() != tgtok::IntVal) {
578 TokError("expected integer value as end of range");
579 return true;
580 }
581 End = Lex.getCurIntVal();
582 break;
583 case tgtok::IntVal:
584 End = -Lex.getCurIntVal();
585 break;
586 }
Bob Wilson7248f862009-11-22 04:24:42 +0000587 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000588 return TokError("invalid range, cannot be negative");
589 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000590
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000591 // Add to the range.
592 if (Start < End) {
593 for (; Start <= End; ++Start)
594 Ranges.push_back(Start);
595 } else {
596 for (; Start >= End; --Start)
597 Ranges.push_back(Start);
598 }
599 return false;
600}
601
602/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
603///
604/// RangeList ::= RangePiece (',' RangePiece)*
605///
606std::vector<unsigned> TGParser::ParseRangeList() {
607 std::vector<unsigned> Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000608
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000609 // Parse the first piece.
610 if (ParseRangePiece(Result))
611 return std::vector<unsigned>();
612 while (Lex.getCode() == tgtok::comma) {
613 Lex.Lex(); // Eat the comma.
614
615 // Parse the next range piece.
616 if (ParseRangePiece(Result))
617 return std::vector<unsigned>();
618 }
619 return Result;
620}
621
622/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
623/// OptionalRangeList ::= '<' RangeList '>'
624/// OptionalRangeList ::= /*empty*/
625bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
626 if (Lex.getCode() != tgtok::less)
627 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000628
Chris Lattner526c8cb2009-06-21 03:39:35 +0000629 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000630 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000631
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000632 // Parse the range list.
633 Ranges = ParseRangeList();
634 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000635
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000636 if (Lex.getCode() != tgtok::greater) {
637 TokError("expected '>' at end of range list");
638 return Error(StartLoc, "to match this '<'");
639 }
640 Lex.Lex(); // eat the '>'.
641 return false;
642}
643
644/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
645/// OptionalBitList ::= '{' RangeList '}'
646/// OptionalBitList ::= /*empty*/
647bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
648 if (Lex.getCode() != tgtok::l_brace)
649 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000650
Chris Lattner526c8cb2009-06-21 03:39:35 +0000651 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000652 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000653
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000654 // Parse the range list.
655 Ranges = ParseRangeList();
656 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000657
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000658 if (Lex.getCode() != tgtok::r_brace) {
659 TokError("expected '}' at end of bit list");
660 return Error(StartLoc, "to match this '{'");
661 }
662 Lex.Lex(); // eat the '}'.
663 return false;
664}
665
666
667/// ParseType - Parse and return a tblgen type. This returns null on error.
668///
669/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000670/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000671/// Type ::= BIT // bit type
672/// Type ::= BITS '<' INTVAL '>' // bits<x> type
673/// Type ::= INT // int type
674/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000675/// Type ::= DAG // dag type
676/// Type ::= ClassID // Record Type
677///
678RecTy *TGParser::ParseType() {
679 switch (Lex.getCode()) {
680 default: TokError("Unknown token when expecting a type"); return 0;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000681 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000682 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000683 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
684 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000685 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000686 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000687 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000688 return 0;
689 case tgtok::Bits: {
690 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
691 TokError("expected '<' after bits type");
692 return 0;
693 }
694 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
695 TokError("expected integer in bits<n> type");
696 return 0;
697 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000698 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000699 if (Lex.Lex() != tgtok::greater) { // Eat count.
700 TokError("expected '>' at end of bits<n> type");
701 return 0;
702 }
703 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000704 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000705 }
706 case tgtok::List: {
707 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
708 TokError("expected '<' after list type");
709 return 0;
710 }
711 Lex.Lex(); // Eat '<'
712 RecTy *SubType = ParseType();
713 if (SubType == 0) return 0;
Bob Wilson7248f862009-11-22 04:24:42 +0000714
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000715 if (Lex.getCode() != tgtok::greater) {
716 TokError("expected '>' at end of list<ty> type");
717 return 0;
718 }
719 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000720 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000721 }
Bob Wilson7248f862009-11-22 04:24:42 +0000722 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000723}
724
725/// ParseIDValue - Parse an ID as a value and decode what it means.
726///
727/// IDValue ::= ID [def local value]
728/// IDValue ::= ID [def template arg]
729/// IDValue ::= ID [multiclass local value]
730/// IDValue ::= ID [multiclass template argument]
731/// IDValue ::= ID [def name]
732///
David Greened4263a62011-10-19 13:04:20 +0000733Init *TGParser::ParseIDValue(Record *CurRec, IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000734 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
735 std::string Name = Lex.getCurStrVal();
Chris Lattner526c8cb2009-06-21 03:39:35 +0000736 SMLoc Loc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000737 Lex.Lex();
738 return ParseIDValue(CurRec, Name, Loc);
739}
740
741/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
742/// has already been read.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000743Init *TGParser::ParseIDValue(Record *CurRec,
David Greened4263a62011-10-19 13:04:20 +0000744 const std::string &Name, SMLoc NameLoc,
745 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000746 if (CurRec) {
747 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000748 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000749
David Greenedb10e692011-10-19 13:02:42 +0000750 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
751
David Greene47a665e2011-10-05 22:42:54 +0000752 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +0000753 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
754 "::");
David Greene47a665e2011-10-05 22:42:54 +0000755
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000756 if (CurRec->isTemplateArg(TemplateArgName)) {
757 const RecordVal *RV = CurRec->getValue(TemplateArgName);
758 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000759 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000760 }
761 }
Bob Wilson7248f862009-11-22 04:24:42 +0000762
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000763 if (CurMultiClass) {
David Greenedb10e692011-10-19 13:02:42 +0000764 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
765 "::");
766
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000767 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
768 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
769 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000770 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000771 }
772 }
Bob Wilson7248f862009-11-22 04:24:42 +0000773
David Greenefb927af2012-02-22 16:09:41 +0000774 // If this is in a foreach loop, make sure it's not a loop iterator
775 for (LoopVector::iterator i = Loops.begin(), iend = Loops.end();
776 i != iend;
777 ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000778 VarInit *IterVar = dyn_cast<VarInit>(i->IterVar);
David Greenefb927af2012-02-22 16:09:41 +0000779 if (IterVar && IterVar->getName() == Name)
780 return IterVar;
781 }
782
David Greene232bd602011-10-19 13:04:21 +0000783 if (Mode == ParseNameMode)
784 return StringInit::get(Name);
785
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000786 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000787 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000788
David Greene232bd602011-10-19 13:04:21 +0000789 if (Mode == ParseValueMode) {
790 Error(NameLoc, "Variable not defined: '" + Name + "'");
791 return 0;
792 }
793
794 return StringInit::get(Name);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000795}
796
David Greene5d0c0512009-05-14 20:54:48 +0000797/// ParseOperation - Parse an operator. This returns null on error.
798///
799/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
800///
David Greeneaf8ee2c2011-07-29 22:43:06 +0000801Init *TGParser::ParseOperation(Record *CurRec) {
David Greene5d0c0512009-05-14 20:54:48 +0000802 switch (Lex.getCode()) {
803 default:
804 TokError("unknown operation");
805 return 0;
David Greene2f7cf7f2011-01-07 17:05:37 +0000806 case tgtok::XHead:
807 case tgtok::XTail:
808 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000809 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
810 UnOpInit::UnaryOp Code;
811 RecTy *Type = 0;
David Greene5d0c0512009-05-14 20:54:48 +0000812
David Greenee8f3b272009-05-14 21:22:49 +0000813 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000814 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000815 case tgtok::XCast:
816 Lex.Lex(); // eat the operation
817 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000818
David Greenee8f3b272009-05-14 21:22:49 +0000819 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000820
David Greenee8f3b272009-05-14 21:22:49 +0000821 if (Type == 0) {
David Greened571b3c2009-05-14 22:38:31 +0000822 TokError("did not get type for unary operator");
David Greenee8f3b272009-05-14 21:22:49 +0000823 return 0;
824 }
David Greene5d0c0512009-05-14 20:54:48 +0000825
David Greenee8f3b272009-05-14 21:22:49 +0000826 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000827 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000828 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000829 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000830 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000831 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000832 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000833 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000834 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000835 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000836 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000837 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000838 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000839 break;
David Greenee8f3b272009-05-14 21:22:49 +0000840 }
841 if (Lex.getCode() != tgtok::l_paren) {
842 TokError("expected '(' after unary operator");
843 return 0;
844 }
845 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000846
David Greeneaf8ee2c2011-07-29 22:43:06 +0000847 Init *LHS = ParseValue(CurRec);
David Greenee8f3b272009-05-14 21:22:49 +0000848 if (LHS == 0) return 0;
David Greene5d0c0512009-05-14 20:54:48 +0000849
David Greene2f7cf7f2011-01-07 17:05:37 +0000850 if (Code == UnOpInit::HEAD
851 || Code == UnOpInit::TAIL
852 || Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000853 ListInit *LHSl = dyn_cast<ListInit>(LHS);
854 StringInit *LHSs = dyn_cast<StringInit>(LHS);
855 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
David Greene8618f952009-06-08 20:23:18 +0000856 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
857 TokError("expected list or string type argument in unary operator");
David Greened571b3c2009-05-14 22:38:31 +0000858 return 0;
859 }
860 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000861 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
862 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
David Greene8618f952009-06-08 20:23:18 +0000863 if (LType == 0 && SType == 0) {
864 TokError("expected list or string type argumnet in unary operator");
David Greened571b3c2009-05-14 22:38:31 +0000865 return 0;
866 }
867 }
868
David Greene2f7cf7f2011-01-07 17:05:37 +0000869 if (Code == UnOpInit::HEAD
870 || Code == UnOpInit::TAIL) {
David Greene8618f952009-06-08 20:23:18 +0000871 if (LHSl == 0 && LHSt == 0) {
872 TokError("expected list type argumnet in unary operator");
873 return 0;
874 }
Bob Wilson7248f862009-11-22 04:24:42 +0000875
David Greened571b3c2009-05-14 22:38:31 +0000876 if (LHSl && LHSl->getSize() == 0) {
877 TokError("empty list argument in unary operator");
878 return 0;
879 }
880 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000881 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000882 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
David Greened571b3c2009-05-14 22:38:31 +0000883 if (Itemt == 0) {
884 TokError("untyped list element in unary operator");
885 return 0;
886 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000887 if (Code == UnOpInit::HEAD) {
David Greened571b3c2009-05-14 22:38:31 +0000888 Type = Itemt->getType();
Bob Wilson7248f862009-11-22 04:24:42 +0000889 } else {
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000890 Type = ListRecTy::get(Itemt->getType());
David Greened571b3c2009-05-14 22:38:31 +0000891 }
Bob Wilson7248f862009-11-22 04:24:42 +0000892 } else {
David Greened571b3c2009-05-14 22:38:31 +0000893 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000894 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
David Greened571b3c2009-05-14 22:38:31 +0000895 if (LType == 0) {
896 TokError("expected list type argumnet in unary operator");
897 return 0;
898 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000899 if (Code == UnOpInit::HEAD) {
David Greened571b3c2009-05-14 22:38:31 +0000900 Type = LType->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +0000901 } else {
David Greened571b3c2009-05-14 22:38:31 +0000902 Type = LType;
903 }
904 }
905 }
906 }
907
David Greenee8f3b272009-05-14 21:22:49 +0000908 if (Lex.getCode() != tgtok::r_paren) {
909 TokError("expected ')' in unary operator");
910 return 0;
911 }
912 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000913 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000914 }
David Greene5d0c0512009-05-14 20:54:48 +0000915
916 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000917 case tgtok::XADD:
Bob Wilson7248f862009-11-22 04:24:42 +0000918 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000919 case tgtok::XSRL:
920 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000921 case tgtok::XEq:
Chris Lattner94026332010-10-06 00:19:21 +0000922 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000923 tgtok::TokKind OpTok = Lex.getCode();
924 SMLoc OpLoc = Lex.getLoc();
925 Lex.Lex(); // eat the operation
926
David Greene5d0c0512009-05-14 20:54:48 +0000927 BinOpInit::BinaryOp Code;
928 RecTy *Type = 0;
929
Chris Lattner61ea00b2010-10-05 23:58:18 +0000930 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000931 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000932 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000933 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000934 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
935 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
936 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
937 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Bob Wilson7248f862009-11-22 04:24:42 +0000938 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000939 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000940 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000941 break;
David Greene5d0c0512009-05-14 20:54:48 +0000942 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000943
David Greene5d0c0512009-05-14 20:54:48 +0000944 if (Lex.getCode() != tgtok::l_paren) {
945 TokError("expected '(' after binary operator");
946 return 0;
947 }
948 Lex.Lex(); // eat the '('
949
David Greeneaf8ee2c2011-07-29 22:43:06 +0000950 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000951
Chris Lattner61ea00b2010-10-05 23:58:18 +0000952 InitList.push_back(ParseValue(CurRec));
953 if (InitList.back() == 0) return 0;
David Greene5d0c0512009-05-14 20:54:48 +0000954
Chris Lattner61ea00b2010-10-05 23:58:18 +0000955 while (Lex.getCode() == tgtok::comma) {
956 Lex.Lex(); // eat the ','
957
958 InitList.push_back(ParseValue(CurRec));
959 if (InitList.back() == 0) return 0;
David Greene5d0c0512009-05-14 20:54:48 +0000960 }
David Greene5d0c0512009-05-14 20:54:48 +0000961
962 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000963 TokError("expected ')' in operator");
David Greene5d0c0512009-05-14 20:54:48 +0000964 return 0;
965 }
966 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000967
968 // We allow multiple operands to associative operators like !strconcat as
969 // shorthand for nesting them.
970 if (Code == BinOpInit::STRCONCAT) {
971 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000972 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000973 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
974 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000975 InitList.back() = RHS;
976 }
977 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000978
Chris Lattner61ea00b2010-10-05 23:58:18 +0000979 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000980 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000981 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000982
Chris Lattner61ea00b2010-10-05 23:58:18 +0000983 Error(OpLoc, "expected two operands to operator");
984 return 0;
David Greene5d0c0512009-05-14 20:54:48 +0000985 }
986
David Greene3587eed2009-05-14 23:26:46 +0000987 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +0000988 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +0000989 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
990 TernOpInit::TernaryOp Code;
991 RecTy *Type = 0;
David Greene5d0c0512009-05-14 20:54:48 +0000992
David Greene98ed3c72009-05-14 21:54:42 +0000993 tgtok::TokKind LexCode = Lex.getCode();
994 Lex.Lex(); // eat the operation
995 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +0000996 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +0000997 case tgtok::XIf:
998 Code = TernOpInit::IF;
999 break;
David Greenee917fff2009-05-14 22:23:47 +00001000 case tgtok::XForEach:
1001 Code = TernOpInit::FOREACH;
1002 break;
David Greene98ed3c72009-05-14 21:54:42 +00001003 case tgtok::XSubst:
1004 Code = TernOpInit::SUBST;
1005 break;
1006 }
1007 if (Lex.getCode() != tgtok::l_paren) {
1008 TokError("expected '(' after ternary operator");
1009 return 0;
1010 }
1011 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001012
David Greeneaf8ee2c2011-07-29 22:43:06 +00001013 Init *LHS = ParseValue(CurRec);
David Greene98ed3c72009-05-14 21:54:42 +00001014 if (LHS == 0) return 0;
David Greene5d0c0512009-05-14 20:54:48 +00001015
David Greene98ed3c72009-05-14 21:54:42 +00001016 if (Lex.getCode() != tgtok::comma) {
1017 TokError("expected ',' in ternary operator");
1018 return 0;
1019 }
1020 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001021
David Greeneaf8ee2c2011-07-29 22:43:06 +00001022 Init *MHS = ParseValue(CurRec);
David Greene98ed3c72009-05-14 21:54:42 +00001023 if (MHS == 0) return 0;
David Greene5d0c0512009-05-14 20:54:48 +00001024
David Greene98ed3c72009-05-14 21:54:42 +00001025 if (Lex.getCode() != tgtok::comma) {
1026 TokError("expected ',' in ternary operator");
1027 return 0;
1028 }
1029 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001030
David Greeneaf8ee2c2011-07-29 22:43:06 +00001031 Init *RHS = ParseValue(CurRec);
David Greene98ed3c72009-05-14 21:54:42 +00001032 if (RHS == 0) return 0;
David Greene5d0c0512009-05-14 20:54:48 +00001033
David Greene98ed3c72009-05-14 21:54:42 +00001034 if (Lex.getCode() != tgtok::r_paren) {
1035 TokError("expected ')' in binary operator");
1036 return 0;
1037 }
1038 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001039
David Greene98ed3c72009-05-14 21:54:42 +00001040 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001041 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001042 case tgtok::XIf: {
Bill Wendling73ce4a62010-12-13 01:46:19 +00001043 RecTy *MHSTy = 0;
1044 RecTy *RHSTy = 0;
1045
Sean Silvafb509ed2012-10-10 20:24:43 +00001046 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001047 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001048 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001049 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001050 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001051 MHSTy = BitRecTy::get();
1052
Sean Silvafb509ed2012-10-10 20:24:43 +00001053 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001054 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001055 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001056 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001057 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001058 RHSTy = BitRecTy::get();
1059
1060 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001061 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001062 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001063 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001064 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001065
1066 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001067 TokError("could not get type for !if");
1068 return 0;
1069 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001070
1071 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1072 Type = RHSTy;
1073 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1074 Type = MHSTy;
Bob Wilson7248f862009-11-22 04:24:42 +00001075 } else {
David Greene3587eed2009-05-14 23:26:46 +00001076 TokError("inconsistent types for !if");
1077 return 0;
1078 }
1079 break;
1080 }
David Greenee917fff2009-05-14 22:23:47 +00001081 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001082 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
David Greenee917fff2009-05-14 22:23:47 +00001083 if (MHSt == 0) {
1084 TokError("could not get type for !foreach");
1085 return 0;
1086 }
1087 Type = MHSt->getType();
1088 break;
1089 }
David Greene98ed3c72009-05-14 21:54:42 +00001090 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001091 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
David Greene98ed3c72009-05-14 21:54:42 +00001092 if (RHSt == 0) {
1093 TokError("could not get type for !subst");
1094 return 0;
1095 }
1096 Type = RHSt->getType();
1097 break;
1098 }
1099 }
David Greenee32ebf22011-07-29 19:07:07 +00001100 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001101 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001102 }
David Greene5d0c0512009-05-14 20:54:48 +00001103 }
David Greene5d0c0512009-05-14 20:54:48 +00001104}
1105
1106/// ParseOperatorType - Parse a type for an operator. This returns
1107/// null on error.
1108///
1109/// OperatorType ::= '<' Type '>'
1110///
Dan Gohman1432ef82009-08-12 22:10:57 +00001111RecTy *TGParser::ParseOperatorType() {
David Greene5d0c0512009-05-14 20:54:48 +00001112 RecTy *Type = 0;
1113
1114 if (Lex.getCode() != tgtok::less) {
1115 TokError("expected type name for operator");
1116 return 0;
1117 }
1118 Lex.Lex(); // eat the <
1119
1120 Type = ParseType();
1121
1122 if (Type == 0) {
1123 TokError("expected type name for operator");
1124 return 0;
1125 }
1126
1127 if (Lex.getCode() != tgtok::greater) {
1128 TokError("expected type name for operator");
1129 return 0;
1130 }
1131 Lex.Lex(); // eat the >
1132
1133 return Type;
1134}
1135
1136
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001137/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1138///
1139/// SimpleValue ::= IDValue
1140/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001141/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001142/// SimpleValue ::= CODEFRAGMENT
1143/// SimpleValue ::= '?'
1144/// SimpleValue ::= '{' ValueList '}'
1145/// SimpleValue ::= ID '<' ValueListNE '>'
1146/// SimpleValue ::= '[' ValueList ']'
1147/// SimpleValue ::= '(' IDValue DagArgList ')'
1148/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001149/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001150/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1151/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1152/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1153/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1154///
David Greened4263a62011-10-19 13:04:20 +00001155Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1156 IDParseMode Mode) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001157 Init *R = 0;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001158 switch (Lex.getCode()) {
1159 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001160 case tgtok::paste:
1161 // This is a leading paste operation. This is deprecated but
1162 // still exists in some .td files. Ignore it.
1163 Lex.Lex(); // Skip '#'.
1164 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001165 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001166 case tgtok::StrVal: {
1167 std::string Val = Lex.getCurStrVal();
1168 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001169
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001170 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001171 while (Lex.getCode() == tgtok::StrVal) {
1172 Val += Lex.getCurStrVal();
1173 Lex.Lex();
1174 }
Bob Wilson7248f862009-11-22 04:24:42 +00001175
David Greenee32ebf22011-07-29 19:07:07 +00001176 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001177 break;
1178 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001179 case tgtok::CodeFragment:
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +00001180 R = StringInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001181 Lex.Lex();
1182 break;
1183 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001184 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001185 Lex.Lex();
1186 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001187 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001188 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001189 std::string Name = Lex.getCurStrVal();
1190 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001191 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001192
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001193 // Value ::= ID '<' ValueListNE '>'
1194 if (Lex.Lex() == tgtok::greater) {
1195 TokError("expected non-empty value list");
1196 return 0;
1197 }
David Greene8618f952009-06-08 20:23:18 +00001198
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001199 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1200 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1201 // body.
1202 Record *Class = Records.getClass(Name);
1203 if (!Class) {
1204 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1205 return 0;
1206 }
David Greene8618f952009-06-08 20:23:18 +00001207
David Greeneaf8ee2c2011-07-29 22:43:06 +00001208 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
David Greene8618f952009-06-08 20:23:18 +00001209 if (ValueList.empty()) return 0;
Bob Wilson7248f862009-11-22 04:24:42 +00001210
David Greene8618f952009-06-08 20:23:18 +00001211 if (Lex.getCode() != tgtok::greater) {
1212 TokError("expected '>' at end of value list");
1213 return 0;
1214 }
1215 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001216 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001217
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001218 // Create the new record, set it as CurRec temporarily.
Alp Tokerce91fe52013-12-21 18:51:00 +00001219 Record *NewRec = new Record(GetNewAnonymousName(), NameLoc, Records,
Jordan Roseabdd99b2013-01-10 18:50:05 +00001220 /*IsAnonymous=*/true);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001221 SubClassReference SCRef;
Jordan Rosef12e8a92013-01-10 18:50:11 +00001222 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001223 SCRef.Rec = Class;
1224 SCRef.TemplateArgs = ValueList;
1225 // Add info about the subclass to NewRec.
1226 if (AddSubClass(NewRec, SCRef))
1227 return 0;
1228 NewRec->resolveReferences();
1229 Records.addDef(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001230
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001231 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001232 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001233 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001234 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001235 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001236 Lex.Lex(); // eat the '{'
David Greeneaf8ee2c2011-07-29 22:43:06 +00001237 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001238
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001239 if (Lex.getCode() != tgtok::r_brace) {
1240 Vals = ParseValueList(CurRec);
1241 if (Vals.empty()) return 0;
1242 }
1243 if (Lex.getCode() != tgtok::r_brace) {
1244 TokError("expected '}' at end of bit list value");
1245 return 0;
1246 }
1247 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001248
David Greeneaf8ee2c2011-07-29 22:43:06 +00001249 SmallVector<Init *, 16> NewBits(Vals.size());
David Greeneb3da8122011-07-29 19:07:00 +00001250
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001251 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001252 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001253 if (Bit == 0) {
Chris Lattner52416952007-11-22 21:06:59 +00001254 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1255 ") is not convertable to a bit");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001256 return 0;
1257 }
David Greeneb3da8122011-07-29 19:07:00 +00001258 NewBits[Vals.size()-i-1] = Bit;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001259 }
David Greenee32ebf22011-07-29 19:07:07 +00001260 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001261 }
1262 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1263 Lex.Lex(); // eat the '['
David Greeneaf8ee2c2011-07-29 22:43:06 +00001264 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001265
David Greene8618f952009-06-08 20:23:18 +00001266 RecTy *DeducedEltTy = 0;
1267 ListRecTy *GivenListTy = 0;
Bob Wilson7248f862009-11-22 04:24:42 +00001268
David Greene8618f952009-06-08 20:23:18 +00001269 if (ItemType != 0) {
Sean Silva98c61712012-10-05 03:31:58 +00001270 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
David Greene8618f952009-06-08 20:23:18 +00001271 if (ListType == 0) {
Reid Klecknerd78273f2013-08-06 22:51:21 +00001272 std::string s;
1273 raw_string_ostream ss(s);
1274 ss << "Type mismatch for list, expected list type, got "
1275 << ItemType->getAsString();
1276 TokError(ss.str());
Jim Grosbach730e1c22011-03-11 19:52:52 +00001277 return 0;
David Greene8618f952009-06-08 20:23:18 +00001278 }
1279 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001280 }
David Greene8618f952009-06-08 20:23:18 +00001281
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001282 if (Lex.getCode() != tgtok::r_square) {
Bob Wilson7248f862009-11-22 04:24:42 +00001283 Vals = ParseValueList(CurRec, 0,
1284 GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattnerf4127dd2007-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 Greene8618f952009-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 Greeneaf8ee2c2011-07-29 22:43:06 +00001313 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greene8618f952009-06-08 20:23:18 +00001314 i != ie;
1315 ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +00001316 TypedInit *TArg = dyn_cast<TypedInit>(*i);
David Greene8618f952009-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 Wilson7248f862009-11-22 04:24:42 +00001327 } else {
David Greene8618f952009-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 Wilson7248f862009-11-22 04:24:42 +00001349 } else {
David Greene8618f952009-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 Wilson7248f862009-11-22 04:24:42 +00001359
David Greenee32ebf22011-07-29 19:07:07 +00001360 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001361 }
1362 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1363 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001364 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001365 TokError("expected identifier in dag init");
1366 return 0;
1367 }
Bob Wilson7248f862009-11-22 04:24:42 +00001368
David Greeneaf8ee2c2011-07-29 22:43:06 +00001369 Init *Operator = ParseValue(CurRec);
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001370 if (Operator == 0) return 0;
David Greenea9c6c5d2009-04-22 20:18:10 +00001371
Nate Begemandbe3f772009-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 Wilson7248f862009-11-22 04:24:42 +00001382
David Greeneaf8ee2c2011-07-29 22:43:06 +00001383 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001384 if (Lex.getCode() != tgtok::r_paren) {
1385 DagArgs = ParseDagArgList(CurRec);
1386 if (DagArgs.empty()) return 0;
1387 }
Bob Wilson7248f862009-11-22 04:24:42 +00001388
Chris Lattnerf4127dd2007-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 Wilson7248f862009-11-22 04:24:42 +00001394
David Greenee32ebf22011-07-29 19:07:07 +00001395 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001396 }
Bob Wilson7248f862009-11-22 04:24:42 +00001397
David Greene2f7cf7f2011-01-07 17:05:37 +00001398 case tgtok::XHead:
1399 case tgtok::XTail:
1400 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001401 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001402 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001403 case tgtok::XADD:
Bob Wilson7248f862009-11-22 04:24:42 +00001404 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001405 case tgtok::XSRL:
1406 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001407 case tgtok::XEq:
Chris Lattner94026332010-10-06 00:19:21 +00001408 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001409 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001410 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001411 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greene5d0c0512009-05-14 20:54:48 +00001412 return ParseOperation(CurRec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001413 }
1414 }
Bob Wilson7248f862009-11-22 04:24:42 +00001415
Chris Lattnerf4127dd2007-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 Greened4263a62011-10-19 13:04:20 +00001426Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1427 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001428 if (Result == 0) return 0;
Bob Wilson7248f862009-11-22 04:24:42 +00001429
Chris Lattnerf4127dd2007-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 Greenefb927af2012-02-22 16:09:41 +00001435 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001436 // This is the beginning of the object body.
1437 return Result;
1438
Chris Lattner526c8cb2009-06-21 03:39:35 +00001439 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001440 Lex.Lex(); // eat the '{'
1441 std::vector<unsigned> Ranges = ParseRangeList();
1442 if (Ranges.empty()) return 0;
Bob Wilson7248f862009-11-22 04:24:42 +00001443
Chris Lattnerf4127dd2007-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 Wilson7248f862009-11-22 04:24:42 +00001451
Chris Lattnerf4127dd2007-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 Lattner526c8cb2009-06-21 03:39:35 +00001461 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001462 Lex.Lex(); // eat the '['
1463 std::vector<unsigned> Ranges = ParseRangeList();
1464 if (Ranges.empty()) return 0;
Bob Wilson7248f862009-11-22 04:24:42 +00001465
Chris Lattnerf4127dd2007-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 Wilson7248f862009-11-22 04:24:42 +00001471
Chris Lattnerf4127dd2007-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 Lattnerf4127dd2007-11-22 20:49:04 +00001486 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001487 Result->getAsString() + "'");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001488 return 0;
1489 }
David Greenee32ebf22011-07-29 19:07:07 +00001490 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001491 Lex.Lex(); // eat field name
1492 break;
David Greene8e85b482011-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 Silvafb509ed2012-10-10 20:24:43 +00001500 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-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 Silvafb509ed2012-10-10 20:24:43 +00001527 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-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 Lattnerf4127dd2007-11-22 20:49:04 +00001543 }
1544 }
1545}
1546
1547/// ParseDagArgList - Parse the argument list for a dag literal expression.
1548///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001549/// DagArg ::= Value (':' VARNAME)?
1550/// DagArg ::= VARNAME
1551/// DagArgList ::= DagArg
1552/// DagArgList ::= DagArgList ',' DagArg
David Greeneaf8ee2c2011-07-29 22:43:06 +00001553std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001554TGParser::ParseDagArgList(Record *CurRec) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001555 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson7248f862009-11-22 04:24:42 +00001556
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001557 while (1) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001558 // DagArg ::= VARNAME
1559 if (Lex.getCode() == tgtok::VarName) {
1560 // A missing value is treated like '?'.
1561 Result.push_back(std::make_pair(UnsetInit::get(), Lex.getCurStrVal()));
1562 Lex.Lex();
1563 } else {
1564 // DagArg ::= Value (':' VARNAME)?
1565 Init *Val = ParseValue(CurRec);
1566 if (Val == 0)
David Greeneaf8ee2c2011-07-29 22:43:06 +00001567 return std::vector<std::pair<llvm::Init*, std::string> >();
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001568
1569 // If the variable name is present, add it.
1570 std::string VarName;
1571 if (Lex.getCode() == tgtok::colon) {
1572 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1573 TokError("expected variable name in dag literal");
1574 return std::vector<std::pair<llvm::Init*, std::string> >();
1575 }
1576 VarName = Lex.getCurStrVal();
1577 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001578 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001579
1580 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001581 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001582 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001583 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001584 }
Bob Wilson7248f862009-11-22 04:24:42 +00001585
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001586 return Result;
1587}
1588
1589
1590/// ParseValueList - Parse a comma separated list of values, returning them as a
1591/// vector. Note that this always expects to be able to parse at least one
1592/// value. It returns an empty list if this is not possible.
1593///
1594/// ValueList ::= Value (',' Value)
1595///
David Greeneaf8ee2c2011-07-29 22:43:06 +00001596std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopher71520a82011-07-11 23:06:52 +00001597 RecTy *EltTy) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001598 std::vector<Init*> Result;
David Greene8618f952009-06-08 20:23:18 +00001599 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001600 unsigned int ArgN = 0;
David Greene8618f952009-06-08 20:23:18 +00001601 if (ArgsRec != 0 && EltTy == 0) {
David Greenedb10e692011-10-19 13:02:42 +00001602 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001603 if (!TArgs.size()) {
1604 TokError("template argument provided to non-template class");
1605 return std::vector<Init*>();
1606 }
David Greene8618f952009-06-08 20:23:18 +00001607 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001608 if (!RV) {
1609 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1610 << ")\n";
1611 }
David Greene8618f952009-06-08 20:23:18 +00001612 assert(RV && "Template argument record not found??");
1613 ItemType = RV->getType();
1614 ++ArgN;
1615 }
1616 Result.push_back(ParseValue(CurRec, ItemType));
David Greeneaf8ee2c2011-07-29 22:43:06 +00001617 if (Result.back() == 0) return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001618
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001619 while (Lex.getCode() == tgtok::comma) {
1620 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001621
David Greene8618f952009-06-08 20:23:18 +00001622 if (ArgsRec != 0 && EltTy == 0) {
David Greenedb10e692011-10-19 13:02:42 +00001623 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001624 if (ArgN >= TArgs.size()) {
1625 TokError("too many template arguments");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001626 return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001627 }
David Greene8618f952009-06-08 20:23:18 +00001628 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1629 assert(RV && "Template argument record not found??");
1630 ItemType = RV->getType();
1631 ++ArgN;
1632 }
1633 Result.push_back(ParseValue(CurRec, ItemType));
David Greeneaf8ee2c2011-07-29 22:43:06 +00001634 if (Result.back() == 0) return std::vector<Init*>();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001635 }
Bob Wilson7248f862009-11-22 04:24:42 +00001636
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001637 return Result;
1638}
1639
1640
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001641/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1642/// empty string on error. This can happen in a number of different context's,
1643/// including within a def or in the template args for a def (which which case
1644/// CurRec will be non-null) and within the template args for a multiclass (in
1645/// which case CurRec will be null, but CurMultiClass will be set). This can
1646/// also happen within a def that is within a multiclass, which will set both
1647/// CurRec and CurMultiClass.
1648///
1649/// Declaration ::= FIELD? Type ID ('=' Value)?
1650///
David Greenedb10e692011-10-19 13:02:42 +00001651Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001652 bool ParsingTemplateArgs) {
1653 // Read the field prefix if present.
1654 bool HasField = Lex.getCode() == tgtok::Field;
1655 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001656
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001657 RecTy *Type = ParseType();
David Greenedb10e692011-10-19 13:02:42 +00001658 if (Type == 0) return 0;
Bob Wilson7248f862009-11-22 04:24:42 +00001659
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001660 if (Lex.getCode() != tgtok::Id) {
1661 TokError("Expected identifier in declaration");
David Greenedb10e692011-10-19 13:02:42 +00001662 return 0;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001663 }
Bob Wilson7248f862009-11-22 04:24:42 +00001664
Chris Lattner526c8cb2009-06-21 03:39:35 +00001665 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001666 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001667 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001668
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001669 if (ParsingTemplateArgs) {
1670 if (CurRec) {
David Greenedb10e692011-10-19 13:02:42 +00001671 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001672 } else {
1673 assert(CurMultiClass);
1674 }
1675 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001676 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1677 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001678 }
Bob Wilson7248f862009-11-22 04:24:42 +00001679
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001680 // Add the value.
1681 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
David Greenedb10e692011-10-19 13:02:42 +00001682 return 0;
Bob Wilson7248f862009-11-22 04:24:42 +00001683
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001684 // If a value is present, parse it.
1685 if (Lex.getCode() == tgtok::equal) {
1686 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001687 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001688 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001689 if (Val == 0 ||
1690 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
David Greenedb10e692011-10-19 13:02:42 +00001691 return 0;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001692 }
Bob Wilson7248f862009-11-22 04:24:42 +00001693
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001694 return DeclName;
1695}
1696
David Greenefb927af2012-02-22 16:09:41 +00001697/// ParseForeachDeclaration - Read a foreach declaration, returning
1698/// the name of the declared object or a NULL Init on error. Return
1699/// the name of the parsed initializer list through ForeachListName.
1700///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001701/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1702/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1703/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001704///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001705VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001706 if (Lex.getCode() != tgtok::Id) {
1707 TokError("Expected identifier in foreach declaration");
1708 return 0;
1709 }
1710
1711 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1712 Lex.Lex();
1713
1714 // If a value is present, parse it.
1715 if (Lex.getCode() != tgtok::equal) {
1716 TokError("Expected '=' in foreach declaration");
1717 return 0;
1718 }
1719 Lex.Lex(); // Eat the '='
1720
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001721 RecTy *IterType = 0;
1722 std::vector<unsigned> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001723
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001724 switch (Lex.getCode()) {
1725 default: TokError("Unknown token when expecting a range list"); return 0;
1726 case tgtok::l_square: { // '[' ValueList ']'
1727 Init *List = ParseSimpleValue(0, 0, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001728 ForeachListValue = dyn_cast<ListInit>(List);
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001729 if (ForeachListValue == 0) {
1730 TokError("Expected a Value list");
1731 return 0;
1732 }
1733 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001734 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001735 if (ListType == 0) {
1736 TokError("Value list is not of list type");
1737 return 0;
1738 }
1739 IterType = ListType->getElementType();
1740 break;
David Greenefb927af2012-02-22 16:09:41 +00001741 }
1742
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001743 case tgtok::IntVal: { // RangePiece.
1744 if (ParseRangePiece(Ranges))
1745 return 0;
1746 break;
David Greenefb927af2012-02-22 16:09:41 +00001747 }
1748
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001749 case tgtok::l_brace: { // '{' RangeList '}'
1750 Lex.Lex(); // eat the '{'
1751 Ranges = ParseRangeList();
1752 if (Lex.getCode() != tgtok::r_brace) {
1753 TokError("expected '}' at end of bit range list");
1754 return 0;
1755 }
1756 Lex.Lex();
1757 break;
1758 }
1759 }
David Greenefb927af2012-02-22 16:09:41 +00001760
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001761 if (!Ranges.empty()) {
1762 assert(!IterType && "Type already initialized?");
1763 IterType = IntRecTy::get();
1764 std::vector<Init*> Values;
1765 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
1766 Values.push_back(IntInit::get(Ranges[i]));
1767 ForeachListValue = ListInit::get(Values, IterType);
1768 }
1769
1770 if (!IterType)
1771 return 0;
1772
1773 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001774}
1775
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001776/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1777/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1778/// template args for a def, which may or may not be in a multiclass. If null,
1779/// these are the template args for a multiclass.
1780///
1781/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001782///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001783bool TGParser::ParseTemplateArgList(Record *CurRec) {
1784 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1785 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001786
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001787 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001788
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001789 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001790 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1791 if (TemplArg == 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001792 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001793
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001794 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001795
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001796 while (Lex.getCode() == tgtok::comma) {
1797 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001798
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001799 // Read the following declarations.
1800 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
David Greenedb10e692011-10-19 13:02:42 +00001801 if (TemplArg == 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001802 return true;
1803 TheRecToAddTo->addTemplateArg(TemplArg);
1804 }
Bob Wilson7248f862009-11-22 04:24:42 +00001805
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001806 if (Lex.getCode() != tgtok::greater)
1807 return TokError("expected '>' at end of template argument list");
1808 Lex.Lex(); // eat the '>'.
1809 return false;
1810}
1811
1812
1813/// ParseBodyItem - Parse a single item at within the body of a def or class.
1814///
1815/// BodyItem ::= Declaration ';'
1816/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1817bool TGParser::ParseBodyItem(Record *CurRec) {
1818 if (Lex.getCode() != tgtok::Let) {
David Greenedb10e692011-10-19 13:02:42 +00001819 if (ParseDeclaration(CurRec, false) == 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001820 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001821
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001822 if (Lex.getCode() != tgtok::semi)
1823 return TokError("expected ';' after declaration");
1824 Lex.Lex();
1825 return false;
1826 }
1827
1828 // LET ID OptionalRangeList '=' Value ';'
1829 if (Lex.Lex() != tgtok::Id)
1830 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001831
Chris Lattner526c8cb2009-06-21 03:39:35 +00001832 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001833 std::string FieldName = Lex.getCurStrVal();
1834 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001835
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001836 std::vector<unsigned> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001837 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001838 return true;
1839 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001840
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001841 if (Lex.getCode() != tgtok::equal)
1842 return TokError("expected '=' in let expression");
1843 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001844
David Greene8618f952009-06-08 20:23:18 +00001845 RecordVal *Field = CurRec->getValue(FieldName);
1846 if (Field == 0)
1847 return TokError("Value '" + FieldName + "' unknown!");
1848
1849 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001850
David Greeneaf8ee2c2011-07-29 22:43:06 +00001851 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001852 if (Val == 0) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001853
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001854 if (Lex.getCode() != tgtok::semi)
1855 return TokError("expected ';' after let expression");
1856 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001857
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001858 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1859}
1860
1861/// ParseBody - Read the body of a class or def. Return true on error, false on
1862/// success.
1863///
1864/// Body ::= ';'
1865/// Body ::= '{' BodyList '}'
1866/// BodyList BodyItem*
1867///
1868bool TGParser::ParseBody(Record *CurRec) {
1869 // If this is a null definition, just eat the semi and return.
1870 if (Lex.getCode() == tgtok::semi) {
1871 Lex.Lex();
1872 return false;
1873 }
Bob Wilson7248f862009-11-22 04:24:42 +00001874
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001875 if (Lex.getCode() != tgtok::l_brace)
1876 return TokError("Expected ';' or '{' to start body");
1877 // Eat the '{'.
1878 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001879
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001880 while (Lex.getCode() != tgtok::r_brace)
1881 if (ParseBodyItem(CurRec))
1882 return true;
1883
1884 // Eat the '}'.
1885 Lex.Lex();
1886 return false;
1887}
1888
Sean Silvacb1a75e2013-01-09 04:49:14 +00001889/// \brief Apply the current let bindings to \a CurRec.
1890/// \returns true on error, false otherwise.
1891bool TGParser::ApplyLetStack(Record *CurRec) {
1892 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1893 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1894 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1895 LetStack[i][j].Bits, LetStack[i][j].Value))
1896 return true;
1897 return false;
1898}
1899
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001900/// ParseObjectBody - Parse the body of a def or class. This consists of an
1901/// optional ClassList followed by a Body. CurRec is the current def or class
1902/// that is being parsed.
1903///
1904/// ObjectBody ::= BaseClassList Body
1905/// BaseClassList ::= /*empty*/
1906/// BaseClassList ::= ':' BaseClassListNE
1907/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1908///
1909bool TGParser::ParseObjectBody(Record *CurRec) {
1910 // If there is a baseclass list, read it.
1911 if (Lex.getCode() == tgtok::colon) {
1912 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001913
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001914 // Read all of the subclasses.
1915 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1916 while (1) {
1917 // Check for error.
1918 if (SubClass.Rec == 0) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001919
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001920 // Add it.
1921 if (AddSubClass(CurRec, SubClass))
1922 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001923
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001924 if (Lex.getCode() != tgtok::comma) break;
1925 Lex.Lex(); // eat ','.
1926 SubClass = ParseSubClassReference(CurRec, false);
1927 }
1928 }
1929
Sean Silvacb1a75e2013-01-09 04:49:14 +00001930 if (ApplyLetStack(CurRec))
1931 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001932
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001933 return ParseBody(CurRec);
1934}
1935
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001936/// ParseDef - Parse and return a top level or multiclass def, return the record
1937/// corresponding to it. This returns null on error.
1938///
1939/// DefInst ::= DEF ObjectName ObjectBody
1940///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00001941bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001942 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001943 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00001944 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001945
1946 // Parse ObjectName and make a record for it.
Jordan Roseabdd99b2013-01-10 18:50:05 +00001947 Record *CurRec;
1948 Init *Name = ParseObjectName(CurMultiClass);
1949 if (Name)
1950 CurRec = new Record(Name, DefLoc, Records);
1951 else
1952 CurRec = new Record(GetNewAnonymousName(), DefLoc, Records,
1953 /*IsAnonymous=*/true);
Bob Wilson7248f862009-11-22 04:24:42 +00001954
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00001955 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001956 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00001957
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001958 // Ensure redefinition doesn't happen.
David Greeneebb006f2012-01-28 00:03:24 +00001959 if (Records.getDef(CurRec->getNameInitAsString())) {
David Greene3a20f5a2011-10-19 13:03:45 +00001960 Error(DefLoc, "def '" + CurRec->getNameInitAsString()
1961 + "' already defined");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00001962 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001963 }
1964 Records.addDef(CurRec);
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00001965 } else if (CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001966 // Otherwise, a def inside a multiclass, add it to the multiclass.
1967 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene07e055f2011-10-19 13:03:51 +00001968 if (CurMultiClass->DefPrototypes[i]->getNameInit()
1969 == CurRec->getNameInit()) {
1970 Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001971 "' already defined in this multiclass!");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00001972 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001973 }
1974 CurMultiClass->DefPrototypes.push_back(CurRec);
1975 }
Bob Wilson7248f862009-11-22 04:24:42 +00001976
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001977 if (ParseObjectBody(CurRec))
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00001978 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001979
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001980 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00001981 // See Record::setName(). This resolve step will see any new name
1982 // for the def that might have been created when resolving
1983 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001984 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00001985
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001986 // If ObjectBody has template arguments, it's an error.
1987 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00001988
1989 if (CurMultiClass) {
1990 // Copy the template arguments for the multiclass into the def.
David Greenedb10e692011-10-19 13:02:42 +00001991 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00001992 CurMultiClass->Rec.getTemplateArgs();
1993
1994 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1995 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1996 assert(RV && "Template arg doesn't exist?");
1997 CurRec->addValue(*RV);
1998 }
1999 }
2000
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002001 if (ProcessForeachDefs(CurRec, DefLoc)) {
David Greenefb927af2012-02-22 16:09:41 +00002002 Error(DefLoc,
2003 "Could not process loops for def" + CurRec->getNameInitAsString());
2004 return true;
2005 }
2006
2007 return false;
2008}
2009
2010/// ParseForeach - Parse a for statement. Return the record corresponding
2011/// to it. This returns true on error.
2012///
2013/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2014/// Foreach ::= FOREACH Declaration IN Object
2015///
2016bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2017 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2018 Lex.Lex(); // Eat the 'for' token.
2019
2020 // Make a temporary object to record items associated with the for
2021 // loop.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002022 ListInit *ListValue = 0;
2023 VarInit *IterName = ParseForeachDeclaration(ListValue);
David Greenefb927af2012-02-22 16:09:41 +00002024 if (IterName == 0)
2025 return TokError("expected declaration in for");
2026
2027 if (Lex.getCode() != tgtok::In)
2028 return TokError("Unknown tok");
2029 Lex.Lex(); // Eat the in
2030
2031 // Create a loop object and remember it.
2032 Loops.push_back(ForeachLoop(IterName, ListValue));
2033
2034 if (Lex.getCode() != tgtok::l_brace) {
2035 // FOREACH Declaration IN Object
2036 if (ParseObject(CurMultiClass))
2037 return true;
2038 }
2039 else {
2040 SMLoc BraceLoc = Lex.getLoc();
2041 // Otherwise, this is a group foreach.
2042 Lex.Lex(); // eat the '{'.
2043
2044 // Parse the object list.
2045 if (ParseObjectList(CurMultiClass))
2046 return true;
2047
2048 if (Lex.getCode() != tgtok::r_brace) {
2049 TokError("expected '}' at end of foreach command");
2050 return Error(BraceLoc, "to match this '{'");
2051 }
2052 Lex.Lex(); // Eat the }
2053 }
2054
2055 // We've processed everything in this loop.
2056 Loops.pop_back();
2057
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002058 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002059}
2060
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002061/// ParseClass - Parse a tblgen class definition.
2062///
2063/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2064///
2065bool TGParser::ParseClass() {
2066 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2067 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002068
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002069 if (Lex.getCode() != tgtok::Id)
2070 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002071
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002072 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2073 if (CurRec) {
2074 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002075 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002076 !CurRec->getSuperClasses().empty() ||
2077 !CurRec->getTemplateArgs().empty())
David Greene8eed9982011-10-19 13:03:58 +00002078 return TokError("Class '" + CurRec->getNameInitAsString()
2079 + "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002080 } else {
2081 // If this is the first reference to this class, create and add it.
Chris Lattner89dcb682010-12-15 04:48:22 +00002082 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002083 Records.addClass(CurRec);
2084 }
2085 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002086
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002087 // If there are template args, parse them.
2088 if (Lex.getCode() == tgtok::less)
2089 if (ParseTemplateArgList(CurRec))
2090 return true;
2091
2092 // Finally, parse the object body.
2093 return ParseObjectBody(CurRec);
2094}
2095
2096/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2097/// of LetRecords.
2098///
2099/// LetList ::= LetItem (',' LetItem)*
2100/// LetItem ::= ID OptionalRangeList '=' Value
2101///
2102std::vector<LetRecord> TGParser::ParseLetList() {
2103 std::vector<LetRecord> Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002104
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002105 while (1) {
2106 if (Lex.getCode() != tgtok::Id) {
2107 TokError("expected identifier in let definition");
2108 return std::vector<LetRecord>();
2109 }
2110 std::string Name = Lex.getCurStrVal();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002111 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002112 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002113
2114 // Check for an optional RangeList.
2115 std::vector<unsigned> Bits;
Bob Wilson7248f862009-11-22 04:24:42 +00002116 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002117 return std::vector<LetRecord>();
2118 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002119
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002120 if (Lex.getCode() != tgtok::equal) {
2121 TokError("expected '=' in let expression");
2122 return std::vector<LetRecord>();
2123 }
2124 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002125
David Greeneaf8ee2c2011-07-29 22:43:06 +00002126 Init *Val = ParseValue(0);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002127 if (Val == 0) return std::vector<LetRecord>();
Bob Wilson7248f862009-11-22 04:24:42 +00002128
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002129 // Now that we have everything, add the record.
2130 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson7248f862009-11-22 04:24:42 +00002131
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002132 if (Lex.getCode() != tgtok::comma)
2133 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002134 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002135 }
2136}
2137
2138/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002139/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002140///
2141/// Object ::= LET LetList IN '{' ObjectList '}'
2142/// Object ::= LET LetList IN Object
2143///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002144bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002145 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2146 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002147
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002148 // Add this entry to the let stack.
2149 std::vector<LetRecord> LetInfo = ParseLetList();
2150 if (LetInfo.empty()) return true;
2151 LetStack.push_back(LetInfo);
2152
2153 if (Lex.getCode() != tgtok::In)
2154 return TokError("expected 'in' at end of top-level 'let'");
2155 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002156
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002157 // If this is a scalar let, just handle it now
2158 if (Lex.getCode() != tgtok::l_brace) {
2159 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002160 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002161 return true;
2162 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002163 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002164 // Otherwise, this is a group let.
2165 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002166
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002167 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002168 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002169 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002170
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002171 if (Lex.getCode() != tgtok::r_brace) {
2172 TokError("expected '}' at end of top level let command");
2173 return Error(BraceLoc, "to match this '{'");
2174 }
2175 Lex.Lex();
2176 }
Bob Wilson7248f862009-11-22 04:24:42 +00002177
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002178 // Outside this let scope, this let block is not active.
2179 LetStack.pop_back();
2180 return false;
2181}
2182
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002183/// ParseMultiClass - Parse a multiclass definition.
2184///
Bob Wilson3d948162009-04-28 19:41:44 +00002185/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002186/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2187/// MultiClassObject ::= DefInst
2188/// MultiClassObject ::= MultiClassInst
2189/// MultiClassObject ::= DefMInst
2190/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2191/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002192///
2193bool TGParser::ParseMultiClass() {
2194 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2195 Lex.Lex(); // Eat the multiclass token.
2196
2197 if (Lex.getCode() != tgtok::Id)
2198 return TokError("expected identifier after multiclass for name");
2199 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002200
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002201 if (MultiClasses.count(Name))
2202 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002203
Chris Lattner77d369c2010-12-13 00:23:57 +00002204 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
2205 Lex.getLoc(), Records);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002206 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002207
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002208 // If there are template args, parse them.
2209 if (Lex.getCode() == tgtok::less)
2210 if (ParseTemplateArgList(0))
2211 return true;
2212
David Greene7049e792009-04-24 16:55:41 +00002213 bool inherits = false;
2214
David Greene753ed8f2009-04-22 16:42:54 +00002215 // If there are submulticlasses, parse them.
2216 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002217 inherits = true;
2218
David Greene753ed8f2009-04-22 16:42:54 +00002219 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002220
David Greene753ed8f2009-04-22 16:42:54 +00002221 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002222 SubMultiClassReference SubMultiClass =
2223 ParseSubMultiClassReference(CurMultiClass);
David Greene753ed8f2009-04-22 16:42:54 +00002224 while (1) {
2225 // Check for error.
2226 if (SubMultiClass.MC == 0) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002227
David Greene753ed8f2009-04-22 16:42:54 +00002228 // Add it.
2229 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2230 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002231
David Greene753ed8f2009-04-22 16:42:54 +00002232 if (Lex.getCode() != tgtok::comma) break;
2233 Lex.Lex(); // eat ','.
2234 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2235 }
2236 }
2237
David Greene7049e792009-04-24 16:55:41 +00002238 if (Lex.getCode() != tgtok::l_brace) {
2239 if (!inherits)
2240 return TokError("expected '{' in multiclass definition");
Bob Wilson7248f862009-11-22 04:24:42 +00002241 else if (Lex.getCode() != tgtok::semi)
2242 return TokError("expected ';' in multiclass definition");
David Greene7049e792009-04-24 16:55:41 +00002243 else
Bob Wilson7248f862009-11-22 04:24:42 +00002244 Lex.Lex(); // eat the ';'.
2245 } else {
David Greene7049e792009-04-24 16:55:41 +00002246 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2247 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002248
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002249 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002250 switch (Lex.getCode()) {
2251 default:
David Greene33f61992011-10-07 18:25:05 +00002252 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002253 case tgtok::Let:
2254 case tgtok::Def:
2255 case tgtok::Defm:
David Greenefb927af2012-02-22 16:09:41 +00002256 case tgtok::Foreach:
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002257 if (ParseObject(CurMultiClass))
2258 return true;
2259 break;
2260 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002261 }
David Greene7049e792009-04-24 16:55:41 +00002262 Lex.Lex(); // eat the '}'.
2263 }
Bob Wilson7248f862009-11-22 04:24:42 +00002264
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002265 CurMultiClass = 0;
2266 return false;
2267}
2268
David Greenedb445972011-10-05 22:42:07 +00002269Record *TGParser::
2270InstantiateMulticlassDef(MultiClass &MC,
2271 Record *DefProto,
Hal Finkelf2a0b2b2014-01-02 19:35:33 +00002272 Init *&DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002273 SMRange DefmPrefixRange) {
David Greene5d5d88c2011-10-19 13:04:31 +00002274 // We need to preserve DefProto so it can be reused for later
2275 // instantiations, so create a new Record to inherit from it.
2276
David Greenedb445972011-10-05 22:42:07 +00002277 // Add in the defm name. If the defm prefix is empty, give each
2278 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2279 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2280 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002281
Jordan Roseabdd99b2013-01-10 18:50:05 +00002282 bool IsAnonymous = false;
2283 if (DefmPrefix == 0) {
David Greene5d5d88c2011-10-19 13:04:31 +00002284 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002285 IsAnonymous = true;
2286 }
David Greene5d5d88c2011-10-19 13:04:31 +00002287
2288 Init *DefName = DefProto->getNameInit();
2289
Sean Silvafb509ed2012-10-10 20:24:43 +00002290 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002291
David Greene8e85b482011-10-19 13:04:43 +00002292 if (DefNameString != 0) {
2293 // We have a fully expanded string so there are no operators to
2294 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002295 DefName =
2296 BinOpInit::get(BinOpInit::STRCONCAT,
2297 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2298 StringRecTy::get())->Fold(DefProto, &MC),
2299 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2300 }
David Greene5d5d88c2011-10-19 13:04:31 +00002301
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002302 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002303 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002304 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002305 Record *CurRec = new Record(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002306
2307 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002308 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002309 Ref.Rec = DefProto;
2310 AddSubClass(CurRec, Ref);
2311
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002312 // Set the value for NAME. We don't resolve references to it 'til later,
2313 // though, so that uses in nested multiclass names don't get
2314 // confused.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002315 if (SetValue(CurRec, Ref.RefRange.Start, "NAME", std::vector<unsigned>(),
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002316 DefmPrefix)) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002317 Error(DefmPrefixRange.Start, "Could not resolve "
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002318 + CurRec->getNameInitAsString() + ":NAME to '"
2319 + DefmPrefix->getAsUnquotedString() + "'");
2320 return 0;
2321 }
David Greene8bf0d722011-10-19 13:04:35 +00002322
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002323 // If the DefNameString didn't resolve, we probably have a reference to
2324 // NAME and need to replace it. We need to do at least this much greedily,
2325 // otherwise nested multiclasses will end up with incorrect NAME expansions.
2326 if (DefNameString == 0) {
David Greene8bf0d722011-10-19 13:04:35 +00002327 RecordVal *DefNameRV = CurRec->getValue("NAME");
2328 CurRec->resolveReferencesTo(DefNameRV);
2329 }
2330
2331 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002332 // Now that we're at the top level, resolve all NAME references
2333 // in the resultant defs that weren't in the def names themselves.
2334 RecordVal *DefNameRV = CurRec->getValue("NAME");
2335 CurRec->resolveReferencesTo(DefNameRV);
2336
2337 // Now that NAME references are resolved and we're at the top level of
2338 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002339 // currently in a multiclass, it means this defm appears inside a
2340 // multiclass and its name won't be fully resolvable until we see
2341 // the top-level defm. Therefore, we don't add this to the
2342 // RecordKeeper at this point. If we did we could get duplicate
2343 // defs as more than one probably refers to NAME or some other
2344 // common internal placeholder.
2345
2346 // Ensure redefinition doesn't happen.
2347 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002348 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002349 "' already defined, instantiating defm with subdef '" +
2350 DefProto->getNameInitAsString() + "'");
2351 return 0;
2352 }
2353
2354 Records.addDef(CurRec);
2355 }
2356
David Greenedb445972011-10-05 22:42:07 +00002357 return CurRec;
2358}
2359
2360bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2361 Record *CurRec,
2362 SMLoc DefmPrefixLoc,
2363 SMLoc SubClassLoc,
David Greenedb10e692011-10-19 13:02:42 +00002364 const std::vector<Init *> &TArgs,
David Greenedb445972011-10-05 22:42:07 +00002365 std::vector<Init *> &TemplateVals,
2366 bool DeleteArgs) {
2367 // Loop over all of the template arguments, setting them to the specified
2368 // value or leaving them as the default if necessary.
2369 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2370 // Check if a value is specified for this temp-arg.
2371 if (i < TemplateVals.size()) {
2372 // Set it now.
2373 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2374 TemplateVals[i]))
2375 return true;
2376
2377 // Resolve it next.
2378 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2379
2380 if (DeleteArgs)
2381 // Now remove it.
2382 CurRec->removeValue(TArgs[i]);
2383
2384 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2385 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenedb10e692011-10-19 13:02:42 +00002386 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2387 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2388 + "'");
David Greenedb445972011-10-05 22:42:07 +00002389 }
2390 }
2391 return false;
2392}
2393
2394bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2395 Record *CurRec,
2396 Record *DefProto,
2397 SMLoc DefmPrefixLoc) {
2398 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002399 if (ApplyLetStack(CurRec))
2400 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002401
David Greenedb445972011-10-05 22:42:07 +00002402 // Don't create a top level definition for defm inside multiclasses,
2403 // instead, only update the prototypes and bind the template args
2404 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002405 if (!CurMultiClass)
2406 return false;
2407 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2408 i != e; ++i)
2409 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2410 == CurRec->getNameInit())
2411 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2412 "' already defined in this multiclass!");
2413 CurMultiClass->DefPrototypes.push_back(CurRec);
David Greenedb445972011-10-05 22:42:07 +00002414
Sean Silvacc951b22013-01-09 05:28:12 +00002415 // Copy the template arguments for the multiclass into the new def.
2416 const std::vector<Init *> &TA =
2417 CurMultiClass->Rec.getTemplateArgs();
David Greenedb445972011-10-05 22:42:07 +00002418
Sean Silvacc951b22013-01-09 05:28:12 +00002419 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2420 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2421 assert(RV && "Template arg doesn't exist?");
2422 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002423 }
2424
2425 return false;
2426}
2427
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002428/// ParseDefm - Parse the instantiation of a multiclass.
2429///
2430/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2431///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002432bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002433 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002434 SMLoc DefmLoc = Lex.getLoc();
David Greene2affd672011-10-19 13:04:29 +00002435 Init *DefmPrefix = 0;
David Greene2affd672011-10-19 13:04:29 +00002436
Craig Topperb21afc62013-01-07 05:09:33 +00002437 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002438 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002439 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002440
Jordan Rosef12e8a92013-01-10 18:50:11 +00002441 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002442 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002443 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002444
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002445 // Keep track of the new generated record definitions.
2446 std::vector<Record*> NewRecDefs;
2447
2448 // This record also inherits from a regular class (non-multiclass)?
2449 bool InheritFromClass = false;
2450
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002451 // eat the colon.
2452 Lex.Lex();
2453
Chris Lattner526c8cb2009-06-21 03:39:35 +00002454 SMLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002455 SubClassReference Ref = ParseSubClassReference(0, true);
David Greenef00919a2009-04-22 22:17:51 +00002456
2457 while (1) {
2458 if (Ref.Rec == 0) return true;
2459
2460 // To instantiate a multiclass, we need to first get the multiclass, then
2461 // instantiate each def contained in the multiclass with the SubClassRef
2462 // template parameters.
2463 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2464 assert(MC && "Didn't lookup multiclass correctly?");
David Greeneaf8ee2c2011-07-29 22:43:06 +00002465 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002466
2467 // Verify that the correct number of template arguments were specified.
David Greenedb10e692011-10-19 13:02:42 +00002468 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002469 if (TArgs.size() < TemplateVals.size())
2470 return Error(SubClassLoc,
2471 "more template args specified than multiclass expects");
2472
2473 // Loop over all the def's in the multiclass, instantiating each one.
2474 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2475 Record *DefProto = MC->DefPrototypes[i];
2476
Jordan Rosef12e8a92013-01-10 18:50:11 +00002477 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix,
2478 SMRange(DefmLoc,
2479 DefmPrefixEndLoc));
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002480 if (!CurRec)
2481 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002482
Jordan Rosef12e8a92013-01-10 18:50:11 +00002483 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002484 TArgs, TemplateVals, true/*Delete args*/))
2485 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002486
Jordan Rosef12e8a92013-01-10 18:50:11 +00002487 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002488 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002489
2490 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002491 }
2492
David Greenedb445972011-10-05 22:42:07 +00002493
David Greenef00919a2009-04-22 22:17:51 +00002494 if (Lex.getCode() != tgtok::comma) break;
2495 Lex.Lex(); // eat ','.
2496
Craig Topper998a39a2013-08-20 04:22:09 +00002497 if (Lex.getCode() != tgtok::Id)
2498 return TokError("expected identifier");
2499
David Greenef00919a2009-04-22 22:17:51 +00002500 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002501
2502 // A defm can inherit from regular classes (non-multiclass) as
2503 // long as they come in the end of the inheritance list.
2504 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0);
2505
2506 if (InheritFromClass)
2507 break;
2508
David Greenef00919a2009-04-22 22:17:51 +00002509 Ref = ParseSubClassReference(0, true);
2510 }
2511
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002512 if (InheritFromClass) {
2513 // Process all the classes to inherit as if they were part of a
2514 // regular 'def' and inherit all record values.
2515 SubClassReference SubClass = ParseSubClassReference(0, false);
2516 while (1) {
2517 // Check for error.
2518 if (SubClass.Rec == 0) return true;
2519
2520 // Get the expanded definition prototypes and teach them about
2521 // the record values the current class to inherit has
2522 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2523 Record *CurRec = NewRecDefs[i];
2524
2525 // Add it.
2526 if (AddSubClass(CurRec, SubClass))
2527 return true;
2528
Sean Silvacb1a75e2013-01-09 04:49:14 +00002529 if (ApplyLetStack(CurRec))
2530 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002531 }
2532
2533 if (Lex.getCode() != tgtok::comma) break;
2534 Lex.Lex(); // eat ','.
2535 SubClass = ParseSubClassReference(0, false);
2536 }
2537 }
2538
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002539 if (!CurMultiClass)
2540 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene50c09122011-08-10 18:27:46 +00002541 // See Record::setName(). This resolve step will see any new
2542 // name for the def that might have been created when resolving
2543 // inheritance, values and arguments above.
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002544 NewRecDefs[i]->resolveReferences();
2545
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002546 if (Lex.getCode() != tgtok::semi)
2547 return TokError("expected ';' at end of defm");
2548 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002549
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002550 return false;
2551}
2552
2553/// ParseObject
2554/// Object ::= ClassInst
2555/// Object ::= DefInst
2556/// Object ::= MultiClassInst
2557/// Object ::= DefMInst
2558/// Object ::= LETCommand '{' ObjectList '}'
2559/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002560bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002561 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002562 default:
2563 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002564 case tgtok::Let: return ParseTopLevelLet(MC);
2565 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002566 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002567 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002568 case tgtok::Class: return ParseClass();
2569 case tgtok::MultiClass: return ParseMultiClass();
2570 }
2571}
2572
2573/// ParseObjectList
2574/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002575bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002576 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002577 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002578 return true;
2579 }
2580 return false;
2581}
2582
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002583bool TGParser::ParseFile() {
2584 Lex.Lex(); // Prime the lexer.
2585 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002586
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002587 // If we have unread input at the end of the file, report it.
2588 if (Lex.getCode() == tgtok::Eof)
2589 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002590
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002591 return TokError("Unexpected input at top level");
2592}
2593