blob: 2a6b3f40492ad0e945cb2ad16f390959359f5808 [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;
Hal Finkela8c1f462014-01-02 20:47:09 +00001228 if (!CurMultiClass) {
1229 NewRec->resolveReferences();
1230 Records.addDef(NewRec);
1231 } else {
1232 // Otherwise, we're inside a multiclass, add it to the multiclass.
1233 CurMultiClass->DefPrototypes.push_back(NewRec);
1234
1235 // Copy the template arguments for the multiclass into the def.
1236 const std::vector<Init *> &TArgs =
1237 CurMultiClass->Rec.getTemplateArgs();
1238
1239 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1240 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1241 assert(RV && "Template arg doesn't exist?");
1242 NewRec->addValue(*RV);
1243 }
1244
1245 // We can't return the prototype def here, instead return:
1246 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1247 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1248 assert(MCNameRV && "multiclass record must have a NAME");
1249
1250 return UnOpInit::get(UnOpInit::CAST,
1251 BinOpInit::get(BinOpInit::STRCONCAT,
1252 VarInit::get(MCNameRV->getName(),
1253 MCNameRV->getType()),
1254 NewRec->getNameInit(),
1255 StringRecTy::get()),
1256 Class->getDefInit()->getType());
1257 }
Bob Wilson7248f862009-11-22 04:24:42 +00001258
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001259 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001260 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001261 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001262 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001263 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001264 Lex.Lex(); // eat the '{'
David Greeneaf8ee2c2011-07-29 22:43:06 +00001265 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001266
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001267 if (Lex.getCode() != tgtok::r_brace) {
1268 Vals = ParseValueList(CurRec);
1269 if (Vals.empty()) return 0;
1270 }
1271 if (Lex.getCode() != tgtok::r_brace) {
1272 TokError("expected '}' at end of bit list value");
1273 return 0;
1274 }
1275 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001276
David Greeneaf8ee2c2011-07-29 22:43:06 +00001277 SmallVector<Init *, 16> NewBits(Vals.size());
David Greeneb3da8122011-07-29 19:07:00 +00001278
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001279 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001280 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001281 if (Bit == 0) {
Chris Lattner52416952007-11-22 21:06:59 +00001282 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1283 ") is not convertable to a bit");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001284 return 0;
1285 }
David Greeneb3da8122011-07-29 19:07:00 +00001286 NewBits[Vals.size()-i-1] = Bit;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001287 }
David Greenee32ebf22011-07-29 19:07:07 +00001288 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001289 }
1290 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1291 Lex.Lex(); // eat the '['
David Greeneaf8ee2c2011-07-29 22:43:06 +00001292 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001293
David Greene8618f952009-06-08 20:23:18 +00001294 RecTy *DeducedEltTy = 0;
1295 ListRecTy *GivenListTy = 0;
Bob Wilson7248f862009-11-22 04:24:42 +00001296
David Greene8618f952009-06-08 20:23:18 +00001297 if (ItemType != 0) {
Sean Silva98c61712012-10-05 03:31:58 +00001298 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
David Greene8618f952009-06-08 20:23:18 +00001299 if (ListType == 0) {
Reid Klecknerd78273f2013-08-06 22:51:21 +00001300 std::string s;
1301 raw_string_ostream ss(s);
1302 ss << "Type mismatch for list, expected list type, got "
1303 << ItemType->getAsString();
1304 TokError(ss.str());
Jim Grosbach730e1c22011-03-11 19:52:52 +00001305 return 0;
David Greene8618f952009-06-08 20:23:18 +00001306 }
1307 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001308 }
David Greene8618f952009-06-08 20:23:18 +00001309
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001310 if (Lex.getCode() != tgtok::r_square) {
Bob Wilson7248f862009-11-22 04:24:42 +00001311 Vals = ParseValueList(CurRec, 0,
1312 GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001313 if (Vals.empty()) return 0;
1314 }
1315 if (Lex.getCode() != tgtok::r_square) {
1316 TokError("expected ']' at end of list value");
1317 return 0;
1318 }
1319 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001320
1321 RecTy *GivenEltTy = 0;
1322 if (Lex.getCode() == tgtok::less) {
1323 // Optional list element type
1324 Lex.Lex(); // eat the '<'
1325
1326 GivenEltTy = ParseType();
1327 if (GivenEltTy == 0) {
1328 // Couldn't parse element type
1329 return 0;
1330 }
1331
1332 if (Lex.getCode() != tgtok::greater) {
1333 TokError("expected '>' at end of list element type");
1334 return 0;
1335 }
1336 Lex.Lex(); // eat the '>'
1337 }
1338
1339 // Check elements
1340 RecTy *EltTy = 0;
David Greeneaf8ee2c2011-07-29 22:43:06 +00001341 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greene8618f952009-06-08 20:23:18 +00001342 i != ie;
1343 ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +00001344 TypedInit *TArg = dyn_cast<TypedInit>(*i);
David Greene8618f952009-06-08 20:23:18 +00001345 if (TArg == 0) {
1346 TokError("Untyped list element");
1347 return 0;
1348 }
1349 if (EltTy != 0) {
1350 EltTy = resolveTypes(EltTy, TArg->getType());
1351 if (EltTy == 0) {
1352 TokError("Incompatible types in list elements");
1353 return 0;
1354 }
Bob Wilson7248f862009-11-22 04:24:42 +00001355 } else {
David Greene8618f952009-06-08 20:23:18 +00001356 EltTy = TArg->getType();
1357 }
1358 }
1359
1360 if (GivenEltTy != 0) {
1361 if (EltTy != 0) {
1362 // Verify consistency
1363 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1364 TokError("Incompatible types in list elements");
1365 return 0;
1366 }
1367 }
1368 EltTy = GivenEltTy;
1369 }
1370
1371 if (EltTy == 0) {
1372 if (ItemType == 0) {
1373 TokError("No type for list");
1374 return 0;
1375 }
1376 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001377 } else {
David Greene8618f952009-06-08 20:23:18 +00001378 // Make sure the deduced type is compatible with the given type
1379 if (GivenListTy) {
1380 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1381 TokError("Element type mismatch for list");
1382 return 0;
1383 }
1384 }
1385 DeducedEltTy = EltTy;
1386 }
Bob Wilson7248f862009-11-22 04:24:42 +00001387
David Greenee32ebf22011-07-29 19:07:07 +00001388 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001389 }
1390 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1391 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001392 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001393 TokError("expected identifier in dag init");
1394 return 0;
1395 }
Bob Wilson7248f862009-11-22 04:24:42 +00001396
David Greeneaf8ee2c2011-07-29 22:43:06 +00001397 Init *Operator = ParseValue(CurRec);
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001398 if (Operator == 0) return 0;
David Greenea9c6c5d2009-04-22 20:18:10 +00001399
Nate Begemandbe3f772009-03-19 05:21:56 +00001400 // If the operator name is present, parse it.
1401 std::string OperatorName;
1402 if (Lex.getCode() == tgtok::colon) {
1403 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1404 TokError("expected variable name in dag operator");
1405 return 0;
1406 }
1407 OperatorName = Lex.getCurStrVal();
1408 Lex.Lex(); // eat the VarName.
1409 }
Bob Wilson7248f862009-11-22 04:24:42 +00001410
David Greeneaf8ee2c2011-07-29 22:43:06 +00001411 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001412 if (Lex.getCode() != tgtok::r_paren) {
1413 DagArgs = ParseDagArgList(CurRec);
1414 if (DagArgs.empty()) return 0;
1415 }
Bob Wilson7248f862009-11-22 04:24:42 +00001416
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001417 if (Lex.getCode() != tgtok::r_paren) {
1418 TokError("expected ')' in dag init");
1419 return 0;
1420 }
1421 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001422
David Greenee32ebf22011-07-29 19:07:07 +00001423 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001424 }
Bob Wilson7248f862009-11-22 04:24:42 +00001425
David Greene2f7cf7f2011-01-07 17:05:37 +00001426 case tgtok::XHead:
1427 case tgtok::XTail:
1428 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001429 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001430 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001431 case tgtok::XADD:
Bob Wilson7248f862009-11-22 04:24:42 +00001432 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001433 case tgtok::XSRL:
1434 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001435 case tgtok::XEq:
Chris Lattner94026332010-10-06 00:19:21 +00001436 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001437 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001438 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001439 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greene5d0c0512009-05-14 20:54:48 +00001440 return ParseOperation(CurRec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001441 }
1442 }
Bob Wilson7248f862009-11-22 04:24:42 +00001443
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001444 return R;
1445}
1446
1447/// ParseValue - Parse a tblgen value. This returns null on error.
1448///
1449/// Value ::= SimpleValue ValueSuffix*
1450/// ValueSuffix ::= '{' BitList '}'
1451/// ValueSuffix ::= '[' BitList ']'
1452/// ValueSuffix ::= '.' ID
1453///
David Greened4263a62011-10-19 13:04:20 +00001454Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1455 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001456 if (Result == 0) return 0;
Bob Wilson7248f862009-11-22 04:24:42 +00001457
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001458 // Parse the suffixes now if present.
1459 while (1) {
1460 switch (Lex.getCode()) {
1461 default: return Result;
1462 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001463 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001464 // This is the beginning of the object body.
1465 return Result;
1466
Chris Lattner526c8cb2009-06-21 03:39:35 +00001467 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001468 Lex.Lex(); // eat the '{'
1469 std::vector<unsigned> Ranges = ParseRangeList();
1470 if (Ranges.empty()) return 0;
Bob Wilson7248f862009-11-22 04:24:42 +00001471
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001472 // Reverse the bitlist.
1473 std::reverse(Ranges.begin(), Ranges.end());
1474 Result = Result->convertInitializerBitRange(Ranges);
1475 if (Result == 0) {
1476 Error(CurlyLoc, "Invalid bit range for value");
1477 return 0;
1478 }
Bob Wilson7248f862009-11-22 04:24:42 +00001479
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001480 // Eat the '}'.
1481 if (Lex.getCode() != tgtok::r_brace) {
1482 TokError("expected '}' at end of bit range list");
1483 return 0;
1484 }
1485 Lex.Lex();
1486 break;
1487 }
1488 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001489 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001490 Lex.Lex(); // eat the '['
1491 std::vector<unsigned> Ranges = ParseRangeList();
1492 if (Ranges.empty()) return 0;
Bob Wilson7248f862009-11-22 04:24:42 +00001493
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001494 Result = Result->convertInitListSlice(Ranges);
1495 if (Result == 0) {
1496 Error(SquareLoc, "Invalid range for list slice");
1497 return 0;
1498 }
Bob Wilson7248f862009-11-22 04:24:42 +00001499
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001500 // Eat the ']'.
1501 if (Lex.getCode() != tgtok::r_square) {
1502 TokError("expected ']' at end of list slice");
1503 return 0;
1504 }
1505 Lex.Lex();
1506 break;
1507 }
1508 case tgtok::period:
1509 if (Lex.Lex() != tgtok::Id) { // eat the .
1510 TokError("expected field identifier after '.'");
1511 return 0;
1512 }
1513 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001514 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001515 Result->getAsString() + "'");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001516 return 0;
1517 }
David Greenee32ebf22011-07-29 19:07:07 +00001518 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001519 Lex.Lex(); // eat field name
1520 break;
David Greene8e85b482011-10-19 13:04:43 +00001521
1522 case tgtok::paste:
1523 SMLoc PasteLoc = Lex.getLoc();
1524
1525 // Create a !strconcat() operation, first casting each operand to
1526 // a string if necessary.
1527
Sean Silvafb509ed2012-10-10 20:24:43 +00001528 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001529 if (!LHS) {
1530 Error(PasteLoc, "LHS of paste is not typed!");
1531 return 0;
1532 }
1533
1534 if (LHS->getType() != StringRecTy::get()) {
1535 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1536 }
1537
1538 TypedInit *RHS = 0;
1539
1540 Lex.Lex(); // Eat the '#'.
1541 switch (Lex.getCode()) {
1542 case tgtok::colon:
1543 case tgtok::semi:
1544 case tgtok::l_brace:
1545 // These are all of the tokens that can begin an object body.
1546 // Some of these can also begin values but we disallow those cases
1547 // because they are unlikely to be useful.
1548
1549 // Trailing paste, concat with an empty string.
1550 RHS = StringInit::get("");
1551 break;
1552
1553 default:
1554 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001555 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001556 if (!RHS) {
1557 Error(PasteLoc, "RHS of paste is not typed!");
1558 return 0;
1559 }
1560
1561 if (RHS->getType() != StringRecTy::get()) {
1562 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1563 }
1564
1565 break;
1566 }
1567
1568 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1569 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1570 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001571 }
1572 }
1573}
1574
1575/// ParseDagArgList - Parse the argument list for a dag literal expression.
1576///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001577/// DagArg ::= Value (':' VARNAME)?
1578/// DagArg ::= VARNAME
1579/// DagArgList ::= DagArg
1580/// DagArgList ::= DagArgList ',' DagArg
David Greeneaf8ee2c2011-07-29 22:43:06 +00001581std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001582TGParser::ParseDagArgList(Record *CurRec) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001583 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson7248f862009-11-22 04:24:42 +00001584
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001585 while (1) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001586 // DagArg ::= VARNAME
1587 if (Lex.getCode() == tgtok::VarName) {
1588 // A missing value is treated like '?'.
1589 Result.push_back(std::make_pair(UnsetInit::get(), Lex.getCurStrVal()));
1590 Lex.Lex();
1591 } else {
1592 // DagArg ::= Value (':' VARNAME)?
1593 Init *Val = ParseValue(CurRec);
1594 if (Val == 0)
David Greeneaf8ee2c2011-07-29 22:43:06 +00001595 return std::vector<std::pair<llvm::Init*, std::string> >();
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001596
1597 // If the variable name is present, add it.
1598 std::string VarName;
1599 if (Lex.getCode() == tgtok::colon) {
1600 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1601 TokError("expected variable name in dag literal");
1602 return std::vector<std::pair<llvm::Init*, std::string> >();
1603 }
1604 VarName = Lex.getCurStrVal();
1605 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001606 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001607
1608 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001609 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001610 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001611 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001612 }
Bob Wilson7248f862009-11-22 04:24:42 +00001613
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001614 return Result;
1615}
1616
1617
1618/// ParseValueList - Parse a comma separated list of values, returning them as a
1619/// vector. Note that this always expects to be able to parse at least one
1620/// value. It returns an empty list if this is not possible.
1621///
1622/// ValueList ::= Value (',' Value)
1623///
David Greeneaf8ee2c2011-07-29 22:43:06 +00001624std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopher71520a82011-07-11 23:06:52 +00001625 RecTy *EltTy) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001626 std::vector<Init*> Result;
David Greene8618f952009-06-08 20:23:18 +00001627 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001628 unsigned int ArgN = 0;
David Greene8618f952009-06-08 20:23:18 +00001629 if (ArgsRec != 0 && EltTy == 0) {
David Greenedb10e692011-10-19 13:02:42 +00001630 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001631 if (!TArgs.size()) {
1632 TokError("template argument provided to non-template class");
1633 return std::vector<Init*>();
1634 }
David Greene8618f952009-06-08 20:23:18 +00001635 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001636 if (!RV) {
1637 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1638 << ")\n";
1639 }
David Greene8618f952009-06-08 20:23:18 +00001640 assert(RV && "Template argument record not found??");
1641 ItemType = RV->getType();
1642 ++ArgN;
1643 }
1644 Result.push_back(ParseValue(CurRec, ItemType));
David Greeneaf8ee2c2011-07-29 22:43:06 +00001645 if (Result.back() == 0) return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001646
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001647 while (Lex.getCode() == tgtok::comma) {
1648 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001649
David Greene8618f952009-06-08 20:23:18 +00001650 if (ArgsRec != 0 && EltTy == 0) {
David Greenedb10e692011-10-19 13:02:42 +00001651 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001652 if (ArgN >= TArgs.size()) {
1653 TokError("too many template arguments");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001654 return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001655 }
David Greene8618f952009-06-08 20:23:18 +00001656 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1657 assert(RV && "Template argument record not found??");
1658 ItemType = RV->getType();
1659 ++ArgN;
1660 }
1661 Result.push_back(ParseValue(CurRec, ItemType));
David Greeneaf8ee2c2011-07-29 22:43:06 +00001662 if (Result.back() == 0) return std::vector<Init*>();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001663 }
Bob Wilson7248f862009-11-22 04:24:42 +00001664
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001665 return Result;
1666}
1667
1668
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001669/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1670/// empty string on error. This can happen in a number of different context's,
1671/// including within a def or in the template args for a def (which which case
1672/// CurRec will be non-null) and within the template args for a multiclass (in
1673/// which case CurRec will be null, but CurMultiClass will be set). This can
1674/// also happen within a def that is within a multiclass, which will set both
1675/// CurRec and CurMultiClass.
1676///
1677/// Declaration ::= FIELD? Type ID ('=' Value)?
1678///
David Greenedb10e692011-10-19 13:02:42 +00001679Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001680 bool ParsingTemplateArgs) {
1681 // Read the field prefix if present.
1682 bool HasField = Lex.getCode() == tgtok::Field;
1683 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001684
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001685 RecTy *Type = ParseType();
David Greenedb10e692011-10-19 13:02:42 +00001686 if (Type == 0) return 0;
Bob Wilson7248f862009-11-22 04:24:42 +00001687
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001688 if (Lex.getCode() != tgtok::Id) {
1689 TokError("Expected identifier in declaration");
David Greenedb10e692011-10-19 13:02:42 +00001690 return 0;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001691 }
Bob Wilson7248f862009-11-22 04:24:42 +00001692
Chris Lattner526c8cb2009-06-21 03:39:35 +00001693 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001694 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001695 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001696
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001697 if (ParsingTemplateArgs) {
1698 if (CurRec) {
David Greenedb10e692011-10-19 13:02:42 +00001699 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001700 } else {
1701 assert(CurMultiClass);
1702 }
1703 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001704 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1705 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001706 }
Bob Wilson7248f862009-11-22 04:24:42 +00001707
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001708 // Add the value.
1709 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
David Greenedb10e692011-10-19 13:02:42 +00001710 return 0;
Bob Wilson7248f862009-11-22 04:24:42 +00001711
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001712 // If a value is present, parse it.
1713 if (Lex.getCode() == tgtok::equal) {
1714 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001715 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001716 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001717 if (Val == 0 ||
1718 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
David Greenedb10e692011-10-19 13:02:42 +00001719 return 0;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001720 }
Bob Wilson7248f862009-11-22 04:24:42 +00001721
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001722 return DeclName;
1723}
1724
David Greenefb927af2012-02-22 16:09:41 +00001725/// ParseForeachDeclaration - Read a foreach declaration, returning
1726/// the name of the declared object or a NULL Init on error. Return
1727/// the name of the parsed initializer list through ForeachListName.
1728///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001729/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1730/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1731/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001732///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001733VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001734 if (Lex.getCode() != tgtok::Id) {
1735 TokError("Expected identifier in foreach declaration");
1736 return 0;
1737 }
1738
1739 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1740 Lex.Lex();
1741
1742 // If a value is present, parse it.
1743 if (Lex.getCode() != tgtok::equal) {
1744 TokError("Expected '=' in foreach declaration");
1745 return 0;
1746 }
1747 Lex.Lex(); // Eat the '='
1748
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001749 RecTy *IterType = 0;
1750 std::vector<unsigned> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001751
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001752 switch (Lex.getCode()) {
1753 default: TokError("Unknown token when expecting a range list"); return 0;
1754 case tgtok::l_square: { // '[' ValueList ']'
1755 Init *List = ParseSimpleValue(0, 0, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001756 ForeachListValue = dyn_cast<ListInit>(List);
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001757 if (ForeachListValue == 0) {
1758 TokError("Expected a Value list");
1759 return 0;
1760 }
1761 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001762 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001763 if (ListType == 0) {
1764 TokError("Value list is not of list type");
1765 return 0;
1766 }
1767 IterType = ListType->getElementType();
1768 break;
David Greenefb927af2012-02-22 16:09:41 +00001769 }
1770
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001771 case tgtok::IntVal: { // RangePiece.
1772 if (ParseRangePiece(Ranges))
1773 return 0;
1774 break;
David Greenefb927af2012-02-22 16:09:41 +00001775 }
1776
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001777 case tgtok::l_brace: { // '{' RangeList '}'
1778 Lex.Lex(); // eat the '{'
1779 Ranges = ParseRangeList();
1780 if (Lex.getCode() != tgtok::r_brace) {
1781 TokError("expected '}' at end of bit range list");
1782 return 0;
1783 }
1784 Lex.Lex();
1785 break;
1786 }
1787 }
David Greenefb927af2012-02-22 16:09:41 +00001788
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001789 if (!Ranges.empty()) {
1790 assert(!IterType && "Type already initialized?");
1791 IterType = IntRecTy::get();
1792 std::vector<Init*> Values;
1793 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
1794 Values.push_back(IntInit::get(Ranges[i]));
1795 ForeachListValue = ListInit::get(Values, IterType);
1796 }
1797
1798 if (!IterType)
1799 return 0;
1800
1801 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001802}
1803
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001804/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1805/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1806/// template args for a def, which may or may not be in a multiclass. If null,
1807/// these are the template args for a multiclass.
1808///
1809/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001810///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001811bool TGParser::ParseTemplateArgList(Record *CurRec) {
1812 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1813 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001814
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001815 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001816
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001817 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001818 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1819 if (TemplArg == 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 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001823
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001824 while (Lex.getCode() == tgtok::comma) {
1825 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001826
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001827 // Read the following declarations.
1828 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
David Greenedb10e692011-10-19 13:02:42 +00001829 if (TemplArg == 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001830 return true;
1831 TheRecToAddTo->addTemplateArg(TemplArg);
1832 }
Bob Wilson7248f862009-11-22 04:24:42 +00001833
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001834 if (Lex.getCode() != tgtok::greater)
1835 return TokError("expected '>' at end of template argument list");
1836 Lex.Lex(); // eat the '>'.
1837 return false;
1838}
1839
1840
1841/// ParseBodyItem - Parse a single item at within the body of a def or class.
1842///
1843/// BodyItem ::= Declaration ';'
1844/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1845bool TGParser::ParseBodyItem(Record *CurRec) {
1846 if (Lex.getCode() != tgtok::Let) {
David Greenedb10e692011-10-19 13:02:42 +00001847 if (ParseDeclaration(CurRec, false) == 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001848 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001849
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001850 if (Lex.getCode() != tgtok::semi)
1851 return TokError("expected ';' after declaration");
1852 Lex.Lex();
1853 return false;
1854 }
1855
1856 // LET ID OptionalRangeList '=' Value ';'
1857 if (Lex.Lex() != tgtok::Id)
1858 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001859
Chris Lattner526c8cb2009-06-21 03:39:35 +00001860 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001861 std::string FieldName = Lex.getCurStrVal();
1862 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001863
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001864 std::vector<unsigned> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001865 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001866 return true;
1867 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001868
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001869 if (Lex.getCode() != tgtok::equal)
1870 return TokError("expected '=' in let expression");
1871 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001872
David Greene8618f952009-06-08 20:23:18 +00001873 RecordVal *Field = CurRec->getValue(FieldName);
1874 if (Field == 0)
1875 return TokError("Value '" + FieldName + "' unknown!");
1876
1877 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001878
David Greeneaf8ee2c2011-07-29 22:43:06 +00001879 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001880 if (Val == 0) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001881
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001882 if (Lex.getCode() != tgtok::semi)
1883 return TokError("expected ';' after let expression");
1884 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001885
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001886 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1887}
1888
1889/// ParseBody - Read the body of a class or def. Return true on error, false on
1890/// success.
1891///
1892/// Body ::= ';'
1893/// Body ::= '{' BodyList '}'
1894/// BodyList BodyItem*
1895///
1896bool TGParser::ParseBody(Record *CurRec) {
1897 // If this is a null definition, just eat the semi and return.
1898 if (Lex.getCode() == tgtok::semi) {
1899 Lex.Lex();
1900 return false;
1901 }
Bob Wilson7248f862009-11-22 04:24:42 +00001902
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001903 if (Lex.getCode() != tgtok::l_brace)
1904 return TokError("Expected ';' or '{' to start body");
1905 // Eat the '{'.
1906 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001907
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001908 while (Lex.getCode() != tgtok::r_brace)
1909 if (ParseBodyItem(CurRec))
1910 return true;
1911
1912 // Eat the '}'.
1913 Lex.Lex();
1914 return false;
1915}
1916
Sean Silvacb1a75e2013-01-09 04:49:14 +00001917/// \brief Apply the current let bindings to \a CurRec.
1918/// \returns true on error, false otherwise.
1919bool TGParser::ApplyLetStack(Record *CurRec) {
1920 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1921 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1922 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1923 LetStack[i][j].Bits, LetStack[i][j].Value))
1924 return true;
1925 return false;
1926}
1927
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001928/// ParseObjectBody - Parse the body of a def or class. This consists of an
1929/// optional ClassList followed by a Body. CurRec is the current def or class
1930/// that is being parsed.
1931///
1932/// ObjectBody ::= BaseClassList Body
1933/// BaseClassList ::= /*empty*/
1934/// BaseClassList ::= ':' BaseClassListNE
1935/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1936///
1937bool TGParser::ParseObjectBody(Record *CurRec) {
1938 // If there is a baseclass list, read it.
1939 if (Lex.getCode() == tgtok::colon) {
1940 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001941
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001942 // Read all of the subclasses.
1943 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1944 while (1) {
1945 // Check for error.
1946 if (SubClass.Rec == 0) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001947
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001948 // Add it.
1949 if (AddSubClass(CurRec, SubClass))
1950 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001951
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001952 if (Lex.getCode() != tgtok::comma) break;
1953 Lex.Lex(); // eat ','.
1954 SubClass = ParseSubClassReference(CurRec, false);
1955 }
1956 }
1957
Sean Silvacb1a75e2013-01-09 04:49:14 +00001958 if (ApplyLetStack(CurRec))
1959 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001960
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001961 return ParseBody(CurRec);
1962}
1963
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001964/// ParseDef - Parse and return a top level or multiclass def, return the record
1965/// corresponding to it. This returns null on error.
1966///
1967/// DefInst ::= DEF ObjectName ObjectBody
1968///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00001969bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001970 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001971 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00001972 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001973
1974 // Parse ObjectName and make a record for it.
Jordan Roseabdd99b2013-01-10 18:50:05 +00001975 Record *CurRec;
1976 Init *Name = ParseObjectName(CurMultiClass);
1977 if (Name)
1978 CurRec = new Record(Name, DefLoc, Records);
1979 else
1980 CurRec = new Record(GetNewAnonymousName(), DefLoc, Records,
1981 /*IsAnonymous=*/true);
Bob Wilson7248f862009-11-22 04:24:42 +00001982
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00001983 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001984 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00001985
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001986 // Ensure redefinition doesn't happen.
David Greeneebb006f2012-01-28 00:03:24 +00001987 if (Records.getDef(CurRec->getNameInitAsString())) {
David Greene3a20f5a2011-10-19 13:03:45 +00001988 Error(DefLoc, "def '" + CurRec->getNameInitAsString()
1989 + "' already defined");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00001990 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001991 }
1992 Records.addDef(CurRec);
Hal Finkela8c1f462014-01-02 20:47:09 +00001993
1994 if (ParseObjectBody(CurRec))
1995 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00001996 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00001997 // Parse the body before adding this prototype to the DefPrototypes vector.
1998 // That way implicit definitions will be added to the DefPrototypes vector
1999 // before this object, instantiated prior to defs derived from this object,
2000 // and this available for indirect name resolution when defs derived from
2001 // this object are instantiated.
2002 if (ParseObjectBody(CurRec))
2003 return true;
2004
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002005 // Otherwise, a def inside a multiclass, add it to the multiclass.
2006 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene07e055f2011-10-19 13:03:51 +00002007 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2008 == CurRec->getNameInit()) {
2009 Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002010 "' already defined in this multiclass!");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002011 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002012 }
2013 CurMultiClass->DefPrototypes.push_back(CurRec);
Hal Finkela8c1f462014-01-02 20:47:09 +00002014 } else if (ParseObjectBody(CurRec))
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002015 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002016
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002017 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002018 // See Record::setName(). This resolve step will see any new name
2019 // for the def that might have been created when resolving
2020 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002021 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002022
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002023 // If ObjectBody has template arguments, it's an error.
2024 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002025
2026 if (CurMultiClass) {
2027 // Copy the template arguments for the multiclass into the def.
David Greenedb10e692011-10-19 13:02:42 +00002028 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002029 CurMultiClass->Rec.getTemplateArgs();
2030
2031 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2032 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
2033 assert(RV && "Template arg doesn't exist?");
2034 CurRec->addValue(*RV);
2035 }
2036 }
2037
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002038 if (ProcessForeachDefs(CurRec, DefLoc)) {
David Greenefb927af2012-02-22 16:09:41 +00002039 Error(DefLoc,
2040 "Could not process loops for def" + CurRec->getNameInitAsString());
2041 return true;
2042 }
2043
2044 return false;
2045}
2046
2047/// ParseForeach - Parse a for statement. Return the record corresponding
2048/// to it. This returns true on error.
2049///
2050/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2051/// Foreach ::= FOREACH Declaration IN Object
2052///
2053bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2054 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2055 Lex.Lex(); // Eat the 'for' token.
2056
2057 // Make a temporary object to record items associated with the for
2058 // loop.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002059 ListInit *ListValue = 0;
2060 VarInit *IterName = ParseForeachDeclaration(ListValue);
David Greenefb927af2012-02-22 16:09:41 +00002061 if (IterName == 0)
2062 return TokError("expected declaration in for");
2063
2064 if (Lex.getCode() != tgtok::In)
2065 return TokError("Unknown tok");
2066 Lex.Lex(); // Eat the in
2067
2068 // Create a loop object and remember it.
2069 Loops.push_back(ForeachLoop(IterName, ListValue));
2070
2071 if (Lex.getCode() != tgtok::l_brace) {
2072 // FOREACH Declaration IN Object
2073 if (ParseObject(CurMultiClass))
2074 return true;
2075 }
2076 else {
2077 SMLoc BraceLoc = Lex.getLoc();
2078 // Otherwise, this is a group foreach.
2079 Lex.Lex(); // eat the '{'.
2080
2081 // Parse the object list.
2082 if (ParseObjectList(CurMultiClass))
2083 return true;
2084
2085 if (Lex.getCode() != tgtok::r_brace) {
2086 TokError("expected '}' at end of foreach command");
2087 return Error(BraceLoc, "to match this '{'");
2088 }
2089 Lex.Lex(); // Eat the }
2090 }
2091
2092 // We've processed everything in this loop.
2093 Loops.pop_back();
2094
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002095 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002096}
2097
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002098/// ParseClass - Parse a tblgen class definition.
2099///
2100/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2101///
2102bool TGParser::ParseClass() {
2103 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2104 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002105
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002106 if (Lex.getCode() != tgtok::Id)
2107 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002108
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002109 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2110 if (CurRec) {
2111 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002112 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002113 !CurRec->getSuperClasses().empty() ||
2114 !CurRec->getTemplateArgs().empty())
David Greene8eed9982011-10-19 13:03:58 +00002115 return TokError("Class '" + CurRec->getNameInitAsString()
2116 + "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002117 } else {
2118 // If this is the first reference to this class, create and add it.
Chris Lattner89dcb682010-12-15 04:48:22 +00002119 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002120 Records.addClass(CurRec);
2121 }
2122 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002123
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002124 // If there are template args, parse them.
2125 if (Lex.getCode() == tgtok::less)
2126 if (ParseTemplateArgList(CurRec))
2127 return true;
2128
2129 // Finally, parse the object body.
2130 return ParseObjectBody(CurRec);
2131}
2132
2133/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2134/// of LetRecords.
2135///
2136/// LetList ::= LetItem (',' LetItem)*
2137/// LetItem ::= ID OptionalRangeList '=' Value
2138///
2139std::vector<LetRecord> TGParser::ParseLetList() {
2140 std::vector<LetRecord> Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002141
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002142 while (1) {
2143 if (Lex.getCode() != tgtok::Id) {
2144 TokError("expected identifier in let definition");
2145 return std::vector<LetRecord>();
2146 }
2147 std::string Name = Lex.getCurStrVal();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002148 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002149 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002150
2151 // Check for an optional RangeList.
2152 std::vector<unsigned> Bits;
Bob Wilson7248f862009-11-22 04:24:42 +00002153 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002154 return std::vector<LetRecord>();
2155 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002156
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002157 if (Lex.getCode() != tgtok::equal) {
2158 TokError("expected '=' in let expression");
2159 return std::vector<LetRecord>();
2160 }
2161 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002162
David Greeneaf8ee2c2011-07-29 22:43:06 +00002163 Init *Val = ParseValue(0);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002164 if (Val == 0) return std::vector<LetRecord>();
Bob Wilson7248f862009-11-22 04:24:42 +00002165
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002166 // Now that we have everything, add the record.
2167 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson7248f862009-11-22 04:24:42 +00002168
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002169 if (Lex.getCode() != tgtok::comma)
2170 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002171 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002172 }
2173}
2174
2175/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002176/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002177///
2178/// Object ::= LET LetList IN '{' ObjectList '}'
2179/// Object ::= LET LetList IN Object
2180///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002181bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002182 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2183 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002184
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002185 // Add this entry to the let stack.
2186 std::vector<LetRecord> LetInfo = ParseLetList();
2187 if (LetInfo.empty()) return true;
2188 LetStack.push_back(LetInfo);
2189
2190 if (Lex.getCode() != tgtok::In)
2191 return TokError("expected 'in' at end of top-level 'let'");
2192 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002193
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002194 // If this is a scalar let, just handle it now
2195 if (Lex.getCode() != tgtok::l_brace) {
2196 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002197 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002198 return true;
2199 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002200 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002201 // Otherwise, this is a group let.
2202 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002203
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002204 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002205 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002206 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002207
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002208 if (Lex.getCode() != tgtok::r_brace) {
2209 TokError("expected '}' at end of top level let command");
2210 return Error(BraceLoc, "to match this '{'");
2211 }
2212 Lex.Lex();
2213 }
Bob Wilson7248f862009-11-22 04:24:42 +00002214
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002215 // Outside this let scope, this let block is not active.
2216 LetStack.pop_back();
2217 return false;
2218}
2219
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002220/// ParseMultiClass - Parse a multiclass definition.
2221///
Bob Wilson3d948162009-04-28 19:41:44 +00002222/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002223/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2224/// MultiClassObject ::= DefInst
2225/// MultiClassObject ::= MultiClassInst
2226/// MultiClassObject ::= DefMInst
2227/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2228/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002229///
2230bool TGParser::ParseMultiClass() {
2231 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2232 Lex.Lex(); // Eat the multiclass token.
2233
2234 if (Lex.getCode() != tgtok::Id)
2235 return TokError("expected identifier after multiclass for name");
2236 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002237
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002238 if (MultiClasses.count(Name))
2239 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002240
Chris Lattner77d369c2010-12-13 00:23:57 +00002241 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
2242 Lex.getLoc(), Records);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002243 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002244
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002245 // If there are template args, parse them.
2246 if (Lex.getCode() == tgtok::less)
2247 if (ParseTemplateArgList(0))
2248 return true;
2249
David Greene7049e792009-04-24 16:55:41 +00002250 bool inherits = false;
2251
David Greene753ed8f2009-04-22 16:42:54 +00002252 // If there are submulticlasses, parse them.
2253 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002254 inherits = true;
2255
David Greene753ed8f2009-04-22 16:42:54 +00002256 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002257
David Greene753ed8f2009-04-22 16:42:54 +00002258 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002259 SubMultiClassReference SubMultiClass =
2260 ParseSubMultiClassReference(CurMultiClass);
David Greene753ed8f2009-04-22 16:42:54 +00002261 while (1) {
2262 // Check for error.
2263 if (SubMultiClass.MC == 0) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002264
David Greene753ed8f2009-04-22 16:42:54 +00002265 // Add it.
2266 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2267 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002268
David Greene753ed8f2009-04-22 16:42:54 +00002269 if (Lex.getCode() != tgtok::comma) break;
2270 Lex.Lex(); // eat ','.
2271 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2272 }
2273 }
2274
David Greene7049e792009-04-24 16:55:41 +00002275 if (Lex.getCode() != tgtok::l_brace) {
2276 if (!inherits)
2277 return TokError("expected '{' in multiclass definition");
Bob Wilson7248f862009-11-22 04:24:42 +00002278 else if (Lex.getCode() != tgtok::semi)
2279 return TokError("expected ';' in multiclass definition");
David Greene7049e792009-04-24 16:55:41 +00002280 else
Bob Wilson7248f862009-11-22 04:24:42 +00002281 Lex.Lex(); // eat the ';'.
2282 } else {
David Greene7049e792009-04-24 16:55:41 +00002283 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2284 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002285
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002286 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002287 switch (Lex.getCode()) {
2288 default:
David Greene33f61992011-10-07 18:25:05 +00002289 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002290 case tgtok::Let:
2291 case tgtok::Def:
2292 case tgtok::Defm:
David Greenefb927af2012-02-22 16:09:41 +00002293 case tgtok::Foreach:
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002294 if (ParseObject(CurMultiClass))
2295 return true;
2296 break;
2297 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002298 }
David Greene7049e792009-04-24 16:55:41 +00002299 Lex.Lex(); // eat the '}'.
2300 }
Bob Wilson7248f862009-11-22 04:24:42 +00002301
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002302 CurMultiClass = 0;
2303 return false;
2304}
2305
David Greenedb445972011-10-05 22:42:07 +00002306Record *TGParser::
2307InstantiateMulticlassDef(MultiClass &MC,
2308 Record *DefProto,
Hal Finkelf2a0b2b2014-01-02 19:35:33 +00002309 Init *&DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002310 SMRange DefmPrefixRange) {
David Greene5d5d88c2011-10-19 13:04:31 +00002311 // We need to preserve DefProto so it can be reused for later
2312 // instantiations, so create a new Record to inherit from it.
2313
David Greenedb445972011-10-05 22:42:07 +00002314 // Add in the defm name. If the defm prefix is empty, give each
2315 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2316 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2317 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002318
Jordan Roseabdd99b2013-01-10 18:50:05 +00002319 bool IsAnonymous = false;
2320 if (DefmPrefix == 0) {
David Greene5d5d88c2011-10-19 13:04:31 +00002321 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002322 IsAnonymous = true;
2323 }
David Greene5d5d88c2011-10-19 13:04:31 +00002324
2325 Init *DefName = DefProto->getNameInit();
2326
Sean Silvafb509ed2012-10-10 20:24:43 +00002327 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002328
David Greene8e85b482011-10-19 13:04:43 +00002329 if (DefNameString != 0) {
2330 // We have a fully expanded string so there are no operators to
2331 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002332 DefName =
2333 BinOpInit::get(BinOpInit::STRCONCAT,
2334 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2335 StringRecTy::get())->Fold(DefProto, &MC),
2336 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2337 }
David Greene5d5d88c2011-10-19 13:04:31 +00002338
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002339 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002340 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002341 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002342 Record *CurRec = new Record(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002343
2344 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002345 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002346 Ref.Rec = DefProto;
2347 AddSubClass(CurRec, Ref);
2348
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002349 // Set the value for NAME. We don't resolve references to it 'til later,
2350 // though, so that uses in nested multiclass names don't get
2351 // confused.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002352 if (SetValue(CurRec, Ref.RefRange.Start, "NAME", std::vector<unsigned>(),
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002353 DefmPrefix)) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002354 Error(DefmPrefixRange.Start, "Could not resolve "
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002355 + CurRec->getNameInitAsString() + ":NAME to '"
2356 + DefmPrefix->getAsUnquotedString() + "'");
2357 return 0;
2358 }
David Greene8bf0d722011-10-19 13:04:35 +00002359
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002360 // If the DefNameString didn't resolve, we probably have a reference to
2361 // NAME and need to replace it. We need to do at least this much greedily,
2362 // otherwise nested multiclasses will end up with incorrect NAME expansions.
2363 if (DefNameString == 0) {
David Greene8bf0d722011-10-19 13:04:35 +00002364 RecordVal *DefNameRV = CurRec->getValue("NAME");
2365 CurRec->resolveReferencesTo(DefNameRV);
2366 }
2367
2368 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002369 // Now that we're at the top level, resolve all NAME references
2370 // in the resultant defs that weren't in the def names themselves.
2371 RecordVal *DefNameRV = CurRec->getValue("NAME");
2372 CurRec->resolveReferencesTo(DefNameRV);
2373
2374 // Now that NAME references are resolved and we're at the top level of
2375 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002376 // currently in a multiclass, it means this defm appears inside a
2377 // multiclass and its name won't be fully resolvable until we see
2378 // the top-level defm. Therefore, we don't add this to the
2379 // RecordKeeper at this point. If we did we could get duplicate
2380 // defs as more than one probably refers to NAME or some other
2381 // common internal placeholder.
2382
2383 // Ensure redefinition doesn't happen.
2384 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002385 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002386 "' already defined, instantiating defm with subdef '" +
2387 DefProto->getNameInitAsString() + "'");
2388 return 0;
2389 }
2390
2391 Records.addDef(CurRec);
2392 }
2393
David Greenedb445972011-10-05 22:42:07 +00002394 return CurRec;
2395}
2396
2397bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2398 Record *CurRec,
2399 SMLoc DefmPrefixLoc,
2400 SMLoc SubClassLoc,
David Greenedb10e692011-10-19 13:02:42 +00002401 const std::vector<Init *> &TArgs,
David Greenedb445972011-10-05 22:42:07 +00002402 std::vector<Init *> &TemplateVals,
2403 bool DeleteArgs) {
2404 // Loop over all of the template arguments, setting them to the specified
2405 // value or leaving them as the default if necessary.
2406 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2407 // Check if a value is specified for this temp-arg.
2408 if (i < TemplateVals.size()) {
2409 // Set it now.
2410 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2411 TemplateVals[i]))
2412 return true;
2413
2414 // Resolve it next.
2415 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2416
2417 if (DeleteArgs)
2418 // Now remove it.
2419 CurRec->removeValue(TArgs[i]);
2420
2421 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2422 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenedb10e692011-10-19 13:02:42 +00002423 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2424 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2425 + "'");
David Greenedb445972011-10-05 22:42:07 +00002426 }
2427 }
2428 return false;
2429}
2430
2431bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2432 Record *CurRec,
2433 Record *DefProto,
2434 SMLoc DefmPrefixLoc) {
2435 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002436 if (ApplyLetStack(CurRec))
2437 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002438
David Greenedb445972011-10-05 22:42:07 +00002439 // Don't create a top level definition for defm inside multiclasses,
2440 // instead, only update the prototypes and bind the template args
2441 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002442 if (!CurMultiClass)
2443 return false;
2444 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2445 i != e; ++i)
2446 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2447 == CurRec->getNameInit())
2448 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2449 "' already defined in this multiclass!");
2450 CurMultiClass->DefPrototypes.push_back(CurRec);
David Greenedb445972011-10-05 22:42:07 +00002451
Sean Silvacc951b22013-01-09 05:28:12 +00002452 // Copy the template arguments for the multiclass into the new def.
2453 const std::vector<Init *> &TA =
2454 CurMultiClass->Rec.getTemplateArgs();
David Greenedb445972011-10-05 22:42:07 +00002455
Sean Silvacc951b22013-01-09 05:28:12 +00002456 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2457 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2458 assert(RV && "Template arg doesn't exist?");
2459 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002460 }
2461
2462 return false;
2463}
2464
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002465/// ParseDefm - Parse the instantiation of a multiclass.
2466///
2467/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2468///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002469bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002470 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002471 SMLoc DefmLoc = Lex.getLoc();
David Greene2affd672011-10-19 13:04:29 +00002472 Init *DefmPrefix = 0;
David Greene2affd672011-10-19 13:04:29 +00002473
Craig Topperb21afc62013-01-07 05:09:33 +00002474 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002475 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002476 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002477
Jordan Rosef12e8a92013-01-10 18:50:11 +00002478 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002479 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002480 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002481
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002482 // Keep track of the new generated record definitions.
2483 std::vector<Record*> NewRecDefs;
2484
2485 // This record also inherits from a regular class (non-multiclass)?
2486 bool InheritFromClass = false;
2487
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002488 // eat the colon.
2489 Lex.Lex();
2490
Chris Lattner526c8cb2009-06-21 03:39:35 +00002491 SMLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002492 SubClassReference Ref = ParseSubClassReference(0, true);
David Greenef00919a2009-04-22 22:17:51 +00002493
2494 while (1) {
2495 if (Ref.Rec == 0) return true;
2496
2497 // To instantiate a multiclass, we need to first get the multiclass, then
2498 // instantiate each def contained in the multiclass with the SubClassRef
2499 // template parameters.
2500 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2501 assert(MC && "Didn't lookup multiclass correctly?");
David Greeneaf8ee2c2011-07-29 22:43:06 +00002502 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002503
2504 // Verify that the correct number of template arguments were specified.
David Greenedb10e692011-10-19 13:02:42 +00002505 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002506 if (TArgs.size() < TemplateVals.size())
2507 return Error(SubClassLoc,
2508 "more template args specified than multiclass expects");
2509
2510 // Loop over all the def's in the multiclass, instantiating each one.
2511 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2512 Record *DefProto = MC->DefPrototypes[i];
2513
Jordan Rosef12e8a92013-01-10 18:50:11 +00002514 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix,
2515 SMRange(DefmLoc,
2516 DefmPrefixEndLoc));
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002517 if (!CurRec)
2518 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002519
Jordan Rosef12e8a92013-01-10 18:50:11 +00002520 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002521 TArgs, TemplateVals, true/*Delete args*/))
2522 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002523
Jordan Rosef12e8a92013-01-10 18:50:11 +00002524 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002525 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002526
2527 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002528 }
2529
David Greenedb445972011-10-05 22:42:07 +00002530
David Greenef00919a2009-04-22 22:17:51 +00002531 if (Lex.getCode() != tgtok::comma) break;
2532 Lex.Lex(); // eat ','.
2533
Craig Topper998a39a2013-08-20 04:22:09 +00002534 if (Lex.getCode() != tgtok::Id)
2535 return TokError("expected identifier");
2536
David Greenef00919a2009-04-22 22:17:51 +00002537 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002538
2539 // A defm can inherit from regular classes (non-multiclass) as
2540 // long as they come in the end of the inheritance list.
2541 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0);
2542
2543 if (InheritFromClass)
2544 break;
2545
David Greenef00919a2009-04-22 22:17:51 +00002546 Ref = ParseSubClassReference(0, true);
2547 }
2548
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002549 if (InheritFromClass) {
2550 // Process all the classes to inherit as if they were part of a
2551 // regular 'def' and inherit all record values.
2552 SubClassReference SubClass = ParseSubClassReference(0, false);
2553 while (1) {
2554 // Check for error.
2555 if (SubClass.Rec == 0) return true;
2556
2557 // Get the expanded definition prototypes and teach them about
2558 // the record values the current class to inherit has
2559 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2560 Record *CurRec = NewRecDefs[i];
2561
2562 // Add it.
2563 if (AddSubClass(CurRec, SubClass))
2564 return true;
2565
Sean Silvacb1a75e2013-01-09 04:49:14 +00002566 if (ApplyLetStack(CurRec))
2567 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002568 }
2569
2570 if (Lex.getCode() != tgtok::comma) break;
2571 Lex.Lex(); // eat ','.
2572 SubClass = ParseSubClassReference(0, false);
2573 }
2574 }
2575
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002576 if (!CurMultiClass)
2577 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene50c09122011-08-10 18:27:46 +00002578 // See Record::setName(). This resolve step will see any new
2579 // name for the def that might have been created when resolving
2580 // inheritance, values and arguments above.
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002581 NewRecDefs[i]->resolveReferences();
2582
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002583 if (Lex.getCode() != tgtok::semi)
2584 return TokError("expected ';' at end of defm");
2585 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002586
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002587 return false;
2588}
2589
2590/// ParseObject
2591/// Object ::= ClassInst
2592/// Object ::= DefInst
2593/// Object ::= MultiClassInst
2594/// Object ::= DefMInst
2595/// Object ::= LETCommand '{' ObjectList '}'
2596/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002597bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002598 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002599 default:
2600 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002601 case tgtok::Let: return ParseTopLevelLet(MC);
2602 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002603 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002604 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002605 case tgtok::Class: return ParseClass();
2606 case tgtok::MultiClass: return ParseMultiClass();
2607 }
2608}
2609
2610/// ParseObjectList
2611/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002612bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002613 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002614 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002615 return true;
2616 }
2617 return false;
2618}
2619
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002620bool TGParser::ParseFile() {
2621 Lex.Lex(); // Prime the lexer.
2622 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002623
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002624 // If we have unread input at the end of the file, report it.
2625 if (Lex.getCode() == tgtok::Eof)
2626 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002627
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002628 return TokError("Unexpected input at top level");
2629}
2630