blob: 56f156dceab1bad815a02f3a0049737d8b812aaf [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"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/SmallVector.h"
Chris Lattnerf4127dd2007-11-22 20:49:04 +000017#include "llvm/ADT/StringExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/Support/CommandLine.h"
19#include "llvm/TableGen/Record.h"
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000020#include <algorithm>
21#include <sstream>
Chris Lattnerf4127dd2007-11-22 20:49:04 +000022using namespace llvm;
23
24//===----------------------------------------------------------------------===//
25// Support Code for the Semantic Actions.
26//===----------------------------------------------------------------------===//
27
28namespace llvm {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000029struct SubClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000030 SMRange RefRange;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000031 Record *Rec;
David Greeneaf8ee2c2011-07-29 22:43:06 +000032 std::vector<Init*> TemplateArgs;
Craig Topper011817a2014-04-09 04:50:04 +000033 SubClassReference() : Rec(nullptr) {}
David Greene7049e792009-04-24 16:55:41 +000034
Craig Topper011817a2014-04-09 04:50:04 +000035 bool isInvalid() const { return Rec == nullptr; }
Chris Lattnerf4127dd2007-11-22 20:49:04 +000036};
David Greene753ed8f2009-04-22 16:42:54 +000037
38struct SubMultiClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000039 SMRange RefRange;
David Greene753ed8f2009-04-22 16:42:54 +000040 MultiClass *MC;
David Greeneaf8ee2c2011-07-29 22:43:06 +000041 std::vector<Init*> TemplateArgs;
Craig Topper011817a2014-04-09 04:50:04 +000042 SubMultiClassReference() : MC(nullptr) {}
Bob Wilson3d948162009-04-28 19:41:44 +000043
Craig Topper011817a2014-04-09 04:50:04 +000044 bool isInvalid() const { return MC == nullptr; }
David Greene7049e792009-04-24 16:55:41 +000045 void dump() const;
David Greene753ed8f2009-04-22 16:42:54 +000046};
David Greene7049e792009-04-24 16:55:41 +000047
48void SubMultiClassReference::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000049 errs() << "Multiclass:\n";
Bob Wilson7248f862009-11-22 04:24:42 +000050
David Greene7049e792009-04-24 16:55:41 +000051 MC->dump();
Bob Wilson7248f862009-11-22 04:24:42 +000052
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000053 errs() << "Template args:\n";
Craig Toppera9642b42015-05-04 01:35:39 +000054 for (Init *TA : TemplateArgs)
Craig Toppereb4d7c62015-04-29 04:43:36 +000055 TA->dump();
David Greene7049e792009-04-24 16:55:41 +000056}
57
Chris Lattnerf4127dd2007-11-22 20:49:04 +000058} // end namespace llvm
59
Chris Lattner526c8cb2009-06-21 03:39:35 +000060bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Craig Topper011817a2014-04-09 04:50:04 +000061 if (!CurRec)
Chris Lattnerf4127dd2007-11-22 20:49:04 +000062 CurRec = &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +000063
Jakob Stoklund Olesen9d1c5ee2012-01-13 03:16:35 +000064 if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000065 // The value already exists in the class, treat this as a set.
66 if (ERV->setValue(RV.getValue()))
67 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
68 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson7248f862009-11-22 04:24:42 +000069 "previous definition of type '" +
Chris Lattnerf4127dd2007-11-22 20:49:04 +000070 ERV->getType()->getAsString() + "'");
71 } else {
72 CurRec->addValue(RV);
73 }
74 return false;
75}
76
77/// SetValue -
78/// Return true on error, false on success.
David Greene3ca42122011-10-19 13:02:39 +000079bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
David Greeneaf8ee2c2011-07-29 22:43:06 +000080 const std::vector<unsigned> &BitList, Init *V) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000081 if (!V) return false;
82
Craig Topper011817a2014-04-09 04:50:04 +000083 if (!CurRec) CurRec = &CurMultiClass->Rec;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000084
85 RecordVal *RV = CurRec->getValue(ValName);
Craig Topper011817a2014-04-09 04:50:04 +000086 if (!RV)
Craig Topper85c07002015-04-30 05:54:22 +000087 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
88 "' unknown!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +000089
90 // Do not allow assignments like 'X = X'. This will just cause infinite loops
91 // in the resolution machinery.
92 if (BitList.empty())
Sean Silvafb509ed2012-10-10 20:24:43 +000093 if (VarInit *VI = dyn_cast<VarInit>(V))
David Greene3ca42122011-10-19 13:02:39 +000094 if (VI->getNameInit() == ValName)
Chris Lattnerf4127dd2007-11-22 20:49:04 +000095 return false;
Bob Wilson7248f862009-11-22 04:24:42 +000096
Chris Lattnerf4127dd2007-11-22 20:49:04 +000097 // If we are assigning to a subset of the bits in the value... then we must be
98 // assigning to a field of BitsRecTy, which must have a BitsInit
99 // initializer.
100 //
101 if (!BitList.empty()) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000102 BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue());
Craig Topper011817a2014-04-09 04:50:04 +0000103 if (!CurVal)
Craig Topper85c07002015-04-30 05:54:22 +0000104 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
105 "' is not a bits type");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000106
107 // Convert the incoming value to a bits type of the appropriate size...
David Greeneaf8ee2c2011-07-29 22:43:06 +0000108 Init *BI = V->convertInitializerTo(BitsRecTy::get(BitList.size()));
Craig Toppera9642b42015-05-04 01:35:39 +0000109 if (!BI)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000110 return Error(Loc, "Initializer is not compatible with bit range");
Bob Wilson7248f862009-11-22 04:24:42 +0000111
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000112 // We should have a BitsInit type now.
Craig Toppered5a9502015-04-29 07:13:05 +0000113 BitsInit *BInit = cast<BitsInit>(BI);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000114
David Greeneaf8ee2c2011-07-29 22:43:06 +0000115 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000116
117 // Loop over bits, assigning values as appropriate.
118 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
119 unsigned Bit = BitList[i];
David Greeneb3da8122011-07-29 19:07:00 +0000120 if (NewBits[Bit])
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000121 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
David Greene3ca42122011-10-19 13:02:39 +0000122 ValName->getAsUnquotedString() + "' more than once");
David Greeneb3da8122011-07-29 19:07:00 +0000123 NewBits[Bit] = BInit->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000124 }
125
126 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
Craig Topper011817a2014-04-09 04:50:04 +0000127 if (!NewBits[i])
David Greeneb3da8122011-07-29 19:07:00 +0000128 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000129
David Greenee32ebf22011-07-29 19:07:07 +0000130 V = BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000131 }
132
Pete Cooper040c6a62014-07-31 01:43:57 +0000133 if (RV->setValue(V)) {
134 std::string InitType = "";
Craig Toppera9642b42015-05-04 01:35:39 +0000135 if (BitsInit *BI = dyn_cast<BitsInit>(V))
Pete Cooper040c6a62014-07-31 01:43:57 +0000136 InitType = (Twine("' of type bit initializer with length ") +
137 Twine(BI->getNumBits())).str();
Craig Topper85c07002015-04-30 05:54:22 +0000138 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
139 "' of type '" + RV->getType()->getAsString() +
140 "' is incompatible with initializer '" + V->getAsString() +
141 InitType + "'");
Pete Cooper040c6a62014-07-31 01:43:57 +0000142 }
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,
Craig Topper85c07002015-04-30 05:54:22 +0000180 "Value not specified for template argument #" +
181 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
David Greene753ed8f2009-04-22 16:42:54 +0000212 // Add all of the values in the subclass into the current class.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000213 for (const auto &SMCVal : SMC->Rec.getValues())
214 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000215 return true;
216
Craig Toppera4ea4b02014-11-30 00:24:32 +0000217 unsigned newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000218
David Greene753ed8f2009-04-22 16:42:54 +0000219 // Add all of the defs in the subclass into the current multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000220 for (const std::unique_ptr<Record> &R : SMC->DefPrototypes) {
David Greene753ed8f2009-04-22 16:42:54 +0000221 // Clone the def and add it to the current multiclass
Craig Toppereb4d7c62015-04-29 04:43:36 +0000222 auto NewDef = make_unique<Record>(*R);
David Greene753ed8f2009-04-22 16:42:54 +0000223
224 // Add all of the values in the superclass into the current def.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000225 for (const auto &MCVal : CurRec->getValues())
226 if (AddValue(NewDef.get(), SubMultiClass.RefRange.Start, MCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000227 return true;
228
Craig Topperc3504c42014-12-11 05:25:33 +0000229 CurMC->DefPrototypes.push_back(std::move(NewDef));
David Greene753ed8f2009-04-22 16:42:54 +0000230 }
Bob Wilson3d948162009-04-28 19:41:44 +0000231
David Greenedb10e692011-10-19 13:02:42 +0000232 const std::vector<Init *> &SMCTArgs = SMC->Rec.getTemplateArgs();
David Greene753ed8f2009-04-22 16:42:54 +0000233
David Greene7049e792009-04-24 16:55:41 +0000234 // Ensure that an appropriate number of template arguments are
235 // specified.
David Greene753ed8f2009-04-22 16:42:54 +0000236 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000237 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000238 "More template args specified than expected");
Bob Wilson3d948162009-04-28 19:41:44 +0000239
David Greene753ed8f2009-04-22 16:42:54 +0000240 // Loop over all of the template arguments, setting them to the specified
241 // value or leaving them as the default if necessary.
242 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
243 if (i < SubMultiClass.TemplateArgs.size()) {
David Greene7049e792009-04-24 16:55:41 +0000244 // If a value is specified for this template arg, set it in the
245 // superclass now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000246 if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson3d948162009-04-28 19:41:44 +0000247 std::vector<unsigned>(),
David Greene753ed8f2009-04-22 16:42:54 +0000248 SubMultiClass.TemplateArgs[i]))
249 return true;
250
251 // Resolve it next.
252 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson3d948162009-04-28 19:41:44 +0000253
David Greene753ed8f2009-04-22 16:42:54 +0000254 // Now remove it.
255 CurRec->removeValue(SMCTArgs[i]);
256
David Greene7049e792009-04-24 16:55:41 +0000257 // If a value is specified for this template arg, set it in the
258 // new defs now.
Craig Topperc3504c42014-12-11 05:25:33 +0000259 for (const auto &Def :
260 makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
261 if (SetValue(Def.get(), SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson3d948162009-04-28 19:41:44 +0000262 std::vector<unsigned>(),
David Greene753ed8f2009-04-22 16:42:54 +0000263 SubMultiClass.TemplateArgs[i]))
264 return true;
265
266 // Resolve it next.
267 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
268
269 // Now remove it
270 Def->removeValue(SMCTArgs[i]);
271 }
272 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000273 return Error(SubMultiClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000274 "Value not specified for template argument #" +
275 utostr(i) + " (" + SMCTArgs[i]->getAsUnquotedString() +
276 ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000277 }
278 }
279
280 return false;
281}
282
David Greenefb927af2012-02-22 16:09:41 +0000283/// ProcessForeachDefs - Given a record, apply all of the variable
284/// values in all surrounding foreach loops, creating new records for
285/// each combination of values.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000286bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
287 if (Loops.empty())
288 return false;
289
David Greenefb927af2012-02-22 16:09:41 +0000290 // We want to instantiate a new copy of CurRec for each combination
291 // of nested loop iterator values. We don't want top instantiate
292 // any copies until we have values for each loop iterator.
293 IterSet IterVals;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000294 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000295}
296
297/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
298/// apply each of the variable values in this loop and then process
299/// subloops.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000300bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
301 // Recursively build a tuple of iterator values.
302 if (IterVals.size() != Loops.size()) {
303 assert(IterVals.size() < Loops.size());
304 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silvafb509ed2012-10-10 20:24:43 +0000305 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Craig Topper011817a2014-04-09 04:50:04 +0000306 if (!List) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000307 Error(Loc, "Loop list is not a list");
308 return true;
309 }
David Greenefb927af2012-02-22 16:09:41 +0000310
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000311 // Process each value.
312 for (int64_t i = 0; i < List->getSize(); ++i) {
Craig Topper011817a2014-04-09 04:50:04 +0000313 Init *ItemVal = List->resolveListElementReference(*CurRec, nullptr, i);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000314 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
315 if (ProcessForeachDefs(CurRec, Loc, IterVals))
316 return true;
317 IterVals.pop_back();
318 }
319 return false;
320 }
321
322 // This is the bottom of the recursion. We have all of the iterator values
323 // for this point in the iteration space. Instantiate a new record to
324 // reflect this combination of values.
Craig Topper84138712014-11-29 05:31:10 +0000325 auto IterRec = make_unique<Record>(*CurRec);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000326
327 // Set the iterator values now.
328 for (unsigned i = 0, e = IterVals.size(); i != e; ++i) {
329 VarInit *IterVar = IterVals[i].IterVar;
Sean Silvafb509ed2012-10-10 20:24:43 +0000330 TypedInit *IVal = dyn_cast<TypedInit>(IterVals[i].IterValue);
Craig Topper84138712014-11-29 05:31:10 +0000331 if (!IVal)
332 return Error(Loc, "foreach iterator value is untyped");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000333
334 IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false));
335
Craig Topper84138712014-11-29 05:31:10 +0000336 if (SetValue(IterRec.get(), Loc, IterVar->getName(),
337 std::vector<unsigned>(), IVal))
338 return Error(Loc, "when instantiating this def");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000339
340 // Resolve it next.
341 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName()));
342
343 // Remove it.
344 IterRec->removeValue(IterVar->getName());
345 }
346
347 if (Records.getDef(IterRec->getNameInitAsString())) {
Artyom Skrobov8b985322014-06-10 12:41:14 +0000348 // If this record is anonymous, it's no problem, just generate a new name
Craig Topper84138712014-11-29 05:31:10 +0000349 if (!IterRec->isAnonymous())
350 return Error(Loc, "def already exists: " +IterRec->getNameInitAsString());
351
352 IterRec->setName(GetNewAnonymousName());
David Greenefb927af2012-02-22 16:09:41 +0000353 }
354
Craig Topper84138712014-11-29 05:31:10 +0000355 Record *IterRecSave = IterRec.get(); // Keep a copy before release.
Craig Toppercdab2322014-11-29 05:52:51 +0000356 Records.addDef(std::move(IterRec));
Craig Topper84138712014-11-29 05:31:10 +0000357 IterRecSave->resolveReferences();
David Greenefb927af2012-02-22 16:09:41 +0000358 return false;
359}
360
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000361//===----------------------------------------------------------------------===//
362// Parser Code
363//===----------------------------------------------------------------------===//
364
365/// isObjectStart - Return true if this is a valid first token for an Object.
366static bool isObjectStart(tgtok::TokKind K) {
367 return K == tgtok::Class || K == tgtok::Def ||
David Greenefb927af2012-02-22 16:09:41 +0000368 K == tgtok::Defm || K == tgtok::Let ||
369 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000370}
371
Alp Tokerce91fe52013-12-21 18:51:00 +0000372/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
373/// an identifier.
374std::string TGParser::GetNewAnonymousName() {
Aaron Ballman97a59fb2015-02-16 19:33:36 +0000375 return "anonymous_" + utostr(AnonCounter++);
Chris Lattner7538ed82010-10-05 22:51:56 +0000376}
377
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000378/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000379/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000380/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000381/// ObjectName ::= /*empty*/
382///
David Greene2affd672011-10-19 13:04:29 +0000383Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
384 switch (Lex.getCode()) {
385 case tgtok::colon:
386 case tgtok::semi:
387 case tgtok::l_brace:
388 // These are all of the tokens that can begin an object body.
389 // Some of these can also begin values but we disallow those cases
390 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000391 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000392 default:
393 break;
394 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000395
Craig Topper011817a2014-04-09 04:50:04 +0000396 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000397 if (CurMultiClass)
398 CurRec = &CurMultiClass->Rec;
399
Craig Topper011817a2014-04-09 04:50:04 +0000400 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000401 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000402 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000403 if (!CurRecName) {
404 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000405 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000406 }
407 Type = CurRecName->getType();
408 }
409
410 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000411}
412
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000413/// ParseClassID - Parse and resolve a reference to a class name. This returns
414/// null on error.
415///
416/// ClassID ::= ID
417///
418Record *TGParser::ParseClassID() {
419 if (Lex.getCode() != tgtok::Id) {
420 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000421 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000422 }
Bob Wilson7248f862009-11-22 04:24:42 +0000423
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000424 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000425 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000426 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000427
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000428 Lex.Lex();
429 return Result;
430}
431
Bob Wilson3d948162009-04-28 19:41:44 +0000432/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
433/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000434///
435/// MultiClassID ::= ID
436///
437MultiClass *TGParser::ParseMultiClassID() {
438 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000439 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000440 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000441 }
Bob Wilson3d948162009-04-28 19:41:44 +0000442
Craig Topper7adf2bf2014-12-11 05:25:30 +0000443 MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
Craig Topper4ca40012014-11-30 01:20:17 +0000444 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000445 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000446
David Greene753ed8f2009-04-22 16:42:54 +0000447 Lex.Lex();
Craig Topper4ca40012014-11-30 01:20:17 +0000448 return Result;
David Greene753ed8f2009-04-22 16:42:54 +0000449}
450
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000451/// ParseSubClassReference - Parse a reference to a subclass or to a templated
452/// subclass. This returns a SubClassRefTy with a null Record* on error.
453///
454/// SubClassRef ::= ClassID
455/// SubClassRef ::= ClassID '<' ValueList '>'
456///
457SubClassReference TGParser::
458ParseSubClassReference(Record *CurRec, bool isDefm) {
459 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000460 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000461
Sean Silva0657b402013-01-09 02:17:14 +0000462 if (isDefm) {
463 if (MultiClass *MC = ParseMultiClassID())
464 Result.Rec = &MC->Rec;
465 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000466 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000467 }
Craig Topper011817a2014-04-09 04:50:04 +0000468 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000469
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000470 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000471 if (Lex.getCode() != tgtok::less) {
472 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000473 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000474 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000475 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000476
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000477 if (Lex.getCode() == tgtok::greater) {
478 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000479 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000480 return Result;
481 }
Bob Wilson7248f862009-11-22 04:24:42 +0000482
David Greene8618f952009-06-08 20:23:18 +0000483 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000484 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000485 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000486 return Result;
487 }
Bob Wilson7248f862009-11-22 04:24:42 +0000488
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000489 if (Lex.getCode() != tgtok::greater) {
490 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000491 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000492 return Result;
493 }
494 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000495 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000496
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000497 return Result;
498}
499
Bob Wilson3d948162009-04-28 19:41:44 +0000500/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
501/// templated submulticlass. This returns a SubMultiClassRefTy with a null
502/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000503///
504/// SubMultiClassRef ::= MultiClassID
505/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
506///
507SubMultiClassReference TGParser::
508ParseSubMultiClassReference(MultiClass *CurMC) {
509 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000510 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000511
David Greene753ed8f2009-04-22 16:42:54 +0000512 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000513 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000514
David Greene753ed8f2009-04-22 16:42:54 +0000515 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000516 if (Lex.getCode() != tgtok::less) {
517 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000518 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000519 }
David Greene753ed8f2009-04-22 16:42:54 +0000520 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000521
David Greene753ed8f2009-04-22 16:42:54 +0000522 if (Lex.getCode() == tgtok::greater) {
523 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000524 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000525 return Result;
526 }
Bob Wilson3d948162009-04-28 19:41:44 +0000527
David Greene8618f952009-06-08 20:23:18 +0000528 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000529 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000530 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000531 return Result;
532 }
Bob Wilson3d948162009-04-28 19:41:44 +0000533
David Greene753ed8f2009-04-22 16:42:54 +0000534 if (Lex.getCode() != tgtok::greater) {
535 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000536 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000537 return Result;
538 }
539 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000540 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000541
542 return Result;
543}
544
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000545/// ParseRangePiece - Parse a bit/value range.
546/// RangePiece ::= INTVAL
547/// RangePiece ::= INTVAL '-' INTVAL
548/// RangePiece ::= INTVAL INTVAL
549bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000550 if (Lex.getCode() != tgtok::IntVal) {
551 TokError("expected integer or bitrange");
552 return true;
553 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000554 int64_t Start = Lex.getCurIntVal();
555 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000556
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000557 if (Start < 0)
558 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000559
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000560 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000561 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000562 Ranges.push_back(Start);
563 return false;
564 case tgtok::minus:
565 if (Lex.Lex() != tgtok::IntVal) {
566 TokError("expected integer value as end of range");
567 return true;
568 }
569 End = Lex.getCurIntVal();
570 break;
571 case tgtok::IntVal:
572 End = -Lex.getCurIntVal();
573 break;
574 }
Bob Wilson7248f862009-11-22 04:24:42 +0000575 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000576 return TokError("invalid range, cannot be negative");
577 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000578
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000579 // Add to the range.
Craig Toppera9642b42015-05-04 01:35:39 +0000580 if (Start < End)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000581 for (; Start <= End; ++Start)
582 Ranges.push_back(Start);
Craig Toppera9642b42015-05-04 01:35:39 +0000583 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000584 for (; Start >= End; --Start)
585 Ranges.push_back(Start);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000586 return false;
587}
588
589/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
590///
591/// RangeList ::= RangePiece (',' RangePiece)*
592///
593std::vector<unsigned> TGParser::ParseRangeList() {
594 std::vector<unsigned> Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000595
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000596 // Parse the first piece.
597 if (ParseRangePiece(Result))
598 return std::vector<unsigned>();
599 while (Lex.getCode() == tgtok::comma) {
600 Lex.Lex(); // Eat the comma.
601
602 // Parse the next range piece.
603 if (ParseRangePiece(Result))
604 return std::vector<unsigned>();
605 }
606 return Result;
607}
608
609/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
610/// OptionalRangeList ::= '<' RangeList '>'
611/// OptionalRangeList ::= /*empty*/
612bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
613 if (Lex.getCode() != tgtok::less)
614 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000615
Chris Lattner526c8cb2009-06-21 03:39:35 +0000616 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000617 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000618
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000619 // Parse the range list.
620 Ranges = ParseRangeList();
621 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000622
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000623 if (Lex.getCode() != tgtok::greater) {
624 TokError("expected '>' at end of range list");
625 return Error(StartLoc, "to match this '<'");
626 }
627 Lex.Lex(); // eat the '>'.
628 return false;
629}
630
631/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
632/// OptionalBitList ::= '{' RangeList '}'
633/// OptionalBitList ::= /*empty*/
634bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
635 if (Lex.getCode() != tgtok::l_brace)
636 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000637
Chris Lattner526c8cb2009-06-21 03:39:35 +0000638 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000639 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000640
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000641 // Parse the range list.
642 Ranges = ParseRangeList();
643 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000644
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000645 if (Lex.getCode() != tgtok::r_brace) {
646 TokError("expected '}' at end of bit list");
647 return Error(StartLoc, "to match this '{'");
648 }
649 Lex.Lex(); // eat the '}'.
650 return false;
651}
652
653
654/// ParseType - Parse and return a tblgen type. This returns null on error.
655///
656/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000657/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000658/// Type ::= BIT // bit type
659/// Type ::= BITS '<' INTVAL '>' // bits<x> type
660/// Type ::= INT // int type
661/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000662/// Type ::= DAG // dag type
663/// Type ::= ClassID // Record Type
664///
665RecTy *TGParser::ParseType() {
666 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000667 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000668 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000669 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000670 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
671 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000672 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000673 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000674 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000675 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000676 case tgtok::Bits: {
677 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
678 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000679 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000680 }
681 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
682 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000683 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000684 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000685 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000686 if (Lex.Lex() != tgtok::greater) { // Eat count.
687 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000688 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000689 }
690 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000691 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000692 }
693 case tgtok::List: {
694 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
695 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000696 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000697 }
698 Lex.Lex(); // Eat '<'
699 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000700 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000701
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000702 if (Lex.getCode() != tgtok::greater) {
703 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000704 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000705 }
706 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000707 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000708 }
Bob Wilson7248f862009-11-22 04:24:42 +0000709 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000710}
711
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000712/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
713/// has already been read.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000714Init *TGParser::ParseIDValue(Record *CurRec,
David Greened4263a62011-10-19 13:04:20 +0000715 const std::string &Name, SMLoc NameLoc,
716 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000717 if (CurRec) {
718 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000719 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000720
David Greenedb10e692011-10-19 13:02:42 +0000721 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
722
David Greene47a665e2011-10-05 22:42:54 +0000723 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +0000724 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
725 "::");
David Greene47a665e2011-10-05 22:42:54 +0000726
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000727 if (CurRec->isTemplateArg(TemplateArgName)) {
728 const RecordVal *RV = CurRec->getValue(TemplateArgName);
729 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000730 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000731 }
732 }
Bob Wilson7248f862009-11-22 04:24:42 +0000733
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000734 if (CurMultiClass) {
David Greenedb10e692011-10-19 13:02:42 +0000735 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
736 "::");
737
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000738 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
739 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
740 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000741 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000742 }
743 }
Bob Wilson7248f862009-11-22 04:24:42 +0000744
David Greenefb927af2012-02-22 16:09:41 +0000745 // If this is in a foreach loop, make sure it's not a loop iterator
Craig Toppereb4d7c62015-04-29 04:43:36 +0000746 for (const auto &L : Loops) {
747 VarInit *IterVar = dyn_cast<VarInit>(L.IterVar);
David Greenefb927af2012-02-22 16:09:41 +0000748 if (IterVar && IterVar->getName() == Name)
749 return IterVar;
750 }
751
David Greene232bd602011-10-19 13:04:21 +0000752 if (Mode == ParseNameMode)
753 return StringInit::get(Name);
754
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000755 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000756 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000757
David Greene232bd602011-10-19 13:04:21 +0000758 if (Mode == ParseValueMode) {
759 Error(NameLoc, "Variable not defined: '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000760 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000761 }
Craig Toppera9642b42015-05-04 01:35:39 +0000762
David Greene232bd602011-10-19 13:04:21 +0000763 return StringInit::get(Name);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000764}
765
David Greene5d0c0512009-05-14 20:54:48 +0000766/// ParseOperation - Parse an operator. This returns null on error.
767///
768/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
769///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000770Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000771 switch (Lex.getCode()) {
772 default:
773 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000774 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000775 case tgtok::XHead:
776 case tgtok::XTail:
777 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000778 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
779 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000780 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000781
David Greenee8f3b272009-05-14 21:22:49 +0000782 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000783 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000784 case tgtok::XCast:
785 Lex.Lex(); // eat the operation
786 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000787
David Greenee8f3b272009-05-14 21:22:49 +0000788 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000789
Craig Topper011817a2014-04-09 04:50:04 +0000790 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000791 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000792 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000793 }
David Greene5d0c0512009-05-14 20:54:48 +0000794
David Greenee8f3b272009-05-14 21:22:49 +0000795 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000796 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000797 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000798 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000799 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000800 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000801 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000802 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000803 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000804 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000805 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000806 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000807 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000808 break;
David Greenee8f3b272009-05-14 21:22:49 +0000809 }
810 if (Lex.getCode() != tgtok::l_paren) {
811 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000812 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000813 }
814 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000815
David Greeneaf8ee2c2011-07-29 22:43:06 +0000816 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000817 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000818
Craig Topper85c07002015-04-30 05:54:22 +0000819 if (Code == UnOpInit::HEAD ||
820 Code == UnOpInit::TAIL ||
821 Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000822 ListInit *LHSl = dyn_cast<ListInit>(LHS);
823 StringInit *LHSs = dyn_cast<StringInit>(LHS);
824 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000825 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000826 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000827 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000828 }
829 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000830 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
831 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000832 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000833 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000834 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000835 }
836 }
837
Craig Topper85c07002015-04-30 05:54:22 +0000838 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
Craig Topper011817a2014-04-09 04:50:04 +0000839 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000840 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000841 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000842 }
Bob Wilson7248f862009-11-22 04:24:42 +0000843
David Greened571b3c2009-05-14 22:38:31 +0000844 if (LHSl && LHSl->getSize() == 0) {
845 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000846 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000847 }
848 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000849 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000850 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000851 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000852 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000853 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000854 }
Craig Toppera9642b42015-05-04 01:35:39 +0000855 Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
856 : ListRecTy::get(Itemt->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000857 } else {
David Greened571b3c2009-05-14 22:38:31 +0000858 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000859 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000860 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000861 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000862 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000863 }
Craig Toppera9642b42015-05-04 01:35:39 +0000864 Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
David Greened571b3c2009-05-14 22:38:31 +0000865 }
866 }
867 }
868
David Greenee8f3b272009-05-14 21:22:49 +0000869 if (Lex.getCode() != tgtok::r_paren) {
870 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000871 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000872 }
873 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000874 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000875 }
David Greene5d0c0512009-05-14 20:54:48 +0000876
877 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000878 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000879 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +0000880 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000881 case tgtok::XSRL:
882 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000883 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000884 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000885 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000886 tgtok::TokKind OpTok = Lex.getCode();
887 SMLoc OpLoc = Lex.getLoc();
888 Lex.Lex(); // eat the operation
889
David Greene5d0c0512009-05-14 20:54:48 +0000890 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000891 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000892
Chris Lattner61ea00b2010-10-05 23:58:18 +0000893 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000894 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000895 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000896 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000897 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000898 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
899 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
900 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
901 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000902 case tgtok::XListConcat:
903 Code = BinOpInit::LISTCONCAT;
904 // We don't know the list type until we parse the first argument
905 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000906 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000907 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000908 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000909 break;
David Greene5d0c0512009-05-14 20:54:48 +0000910 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000911
David Greene5d0c0512009-05-14 20:54:48 +0000912 if (Lex.getCode() != tgtok::l_paren) {
913 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000914 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000915 }
916 Lex.Lex(); // eat the '('
917
David Greeneaf8ee2c2011-07-29 22:43:06 +0000918 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000919
Chris Lattner61ea00b2010-10-05 23:58:18 +0000920 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000921 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000922
Chris Lattner61ea00b2010-10-05 23:58:18 +0000923 while (Lex.getCode() == tgtok::comma) {
924 Lex.Lex(); // eat the ','
925
926 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000927 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000928 }
David Greene5d0c0512009-05-14 20:54:48 +0000929
930 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000931 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000932 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000933 }
934 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000935
Daniel Sanders314e80e2014-05-07 10:13:19 +0000936 // If we are doing !listconcat, we should know the type by now
937 if (OpTok == tgtok::XListConcat) {
938 if (VarInit *Arg0 = dyn_cast<VarInit>(InitList[0]))
939 Type = Arg0->getType();
940 else if (ListInit *Arg0 = dyn_cast<ListInit>(InitList[0]))
941 Type = Arg0->getType();
942 else {
943 InitList[0]->dump();
944 Error(OpLoc, "expected a list");
945 return nullptr;
946 }
947 }
948
Chris Lattner61ea00b2010-10-05 23:58:18 +0000949 // We allow multiple operands to associative operators like !strconcat as
950 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000951 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000952 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000953 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000954 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
955 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000956 InitList.back() = RHS;
957 }
958 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000959
Chris Lattner61ea00b2010-10-05 23:58:18 +0000960 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000961 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000962 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000963
Chris Lattner61ea00b2010-10-05 23:58:18 +0000964 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +0000965 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000966 }
967
David Greene3587eed2009-05-14 23:26:46 +0000968 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +0000969 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +0000970 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
971 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000972 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000973
David Greene98ed3c72009-05-14 21:54:42 +0000974 tgtok::TokKind LexCode = Lex.getCode();
975 Lex.Lex(); // eat the operation
976 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +0000977 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +0000978 case tgtok::XIf:
979 Code = TernOpInit::IF;
980 break;
David Greenee917fff2009-05-14 22:23:47 +0000981 case tgtok::XForEach:
982 Code = TernOpInit::FOREACH;
983 break;
David Greene98ed3c72009-05-14 21:54:42 +0000984 case tgtok::XSubst:
985 Code = TernOpInit::SUBST;
986 break;
987 }
988 if (Lex.getCode() != tgtok::l_paren) {
989 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000990 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +0000991 }
992 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000993
David Greeneaf8ee2c2011-07-29 22:43:06 +0000994 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000995 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000996
David Greene98ed3c72009-05-14 21:54:42 +0000997 if (Lex.getCode() != tgtok::comma) {
998 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000999 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001000 }
1001 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001002
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001003 Init *MHS = ParseValue(CurRec, ItemType);
1004 if (!MHS)
1005 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001006
David Greene98ed3c72009-05-14 21:54:42 +00001007 if (Lex.getCode() != tgtok::comma) {
1008 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001009 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001010 }
1011 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001012
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001013 Init *RHS = ParseValue(CurRec, ItemType);
1014 if (!RHS)
1015 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001016
David Greene98ed3c72009-05-14 21:54:42 +00001017 if (Lex.getCode() != tgtok::r_paren) {
1018 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001019 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001020 }
1021 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001022
David Greene98ed3c72009-05-14 21:54:42 +00001023 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001024 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001025 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001026 RecTy *MHSTy = nullptr;
1027 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001028
Sean Silvafb509ed2012-10-10 20:24:43 +00001029 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001030 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001031 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001032 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001033 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001034 MHSTy = BitRecTy::get();
1035
Sean Silvafb509ed2012-10-10 20:24:43 +00001036 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001037 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001038 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001039 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001040 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001041 RHSTy = BitRecTy::get();
1042
1043 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001044 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001045 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001046 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001047 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001048
1049 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001050 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001051 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001052 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001053
1054 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1055 Type = RHSTy;
1056 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1057 Type = MHSTy;
Bob Wilson7248f862009-11-22 04:24:42 +00001058 } else {
David Greene3587eed2009-05-14 23:26:46 +00001059 TokError("inconsistent types for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001060 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001061 }
1062 break;
1063 }
David Greenee917fff2009-05-14 22:23:47 +00001064 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001065 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
Craig Topper011817a2014-04-09 04:50:04 +00001066 if (!MHSt) {
David Greenee917fff2009-05-14 22:23:47 +00001067 TokError("could not get type for !foreach");
Craig Topper011817a2014-04-09 04:50:04 +00001068 return nullptr;
David Greenee917fff2009-05-14 22:23:47 +00001069 }
1070 Type = MHSt->getType();
1071 break;
1072 }
David Greene98ed3c72009-05-14 21:54:42 +00001073 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001074 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001075 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001076 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001077 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001078 }
1079 Type = RHSt->getType();
1080 break;
1081 }
1082 }
David Greenee32ebf22011-07-29 19:07:07 +00001083 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001084 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001085 }
David Greene5d0c0512009-05-14 20:54:48 +00001086 }
David Greene5d0c0512009-05-14 20:54:48 +00001087}
1088
1089/// ParseOperatorType - Parse a type for an operator. This returns
1090/// null on error.
1091///
1092/// OperatorType ::= '<' Type '>'
1093///
Dan Gohman1432ef82009-08-12 22:10:57 +00001094RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001095 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001096
1097 if (Lex.getCode() != tgtok::less) {
1098 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001099 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001100 }
1101 Lex.Lex(); // eat the <
1102
1103 Type = ParseType();
1104
Craig Topper011817a2014-04-09 04:50:04 +00001105 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001106 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001107 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001108 }
1109
1110 if (Lex.getCode() != tgtok::greater) {
1111 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001112 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001113 }
1114 Lex.Lex(); // eat the >
1115
1116 return Type;
1117}
1118
1119
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001120/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1121///
1122/// SimpleValue ::= IDValue
1123/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001124/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001125/// SimpleValue ::= CODEFRAGMENT
1126/// SimpleValue ::= '?'
1127/// SimpleValue ::= '{' ValueList '}'
1128/// SimpleValue ::= ID '<' ValueListNE '>'
1129/// SimpleValue ::= '[' ValueList ']'
1130/// SimpleValue ::= '(' IDValue DagArgList ')'
1131/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001132/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001133/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1134/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1135/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001136/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001137/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1138///
David Greened4263a62011-10-19 13:04:20 +00001139Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1140 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001141 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001142 switch (Lex.getCode()) {
1143 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001144 case tgtok::paste:
1145 // This is a leading paste operation. This is deprecated but
1146 // still exists in some .td files. Ignore it.
1147 Lex.Lex(); // Skip '#'.
1148 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001149 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001150 case tgtok::BinaryIntVal: {
1151 auto BinaryVal = Lex.getCurBinaryIntVal();
1152 SmallVector<Init*, 16> Bits(BinaryVal.second);
1153 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001154 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001155 R = BitsInit::get(Bits);
1156 Lex.Lex();
1157 break;
1158 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001159 case tgtok::StrVal: {
1160 std::string Val = Lex.getCurStrVal();
1161 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001162
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001163 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001164 while (Lex.getCode() == tgtok::StrVal) {
1165 Val += Lex.getCurStrVal();
1166 Lex.Lex();
1167 }
Bob Wilson7248f862009-11-22 04:24:42 +00001168
David Greenee32ebf22011-07-29 19:07:07 +00001169 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001170 break;
1171 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001172 case tgtok::CodeFragment:
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +00001173 R = StringInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001174 Lex.Lex();
1175 break;
1176 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001177 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001178 Lex.Lex();
1179 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001180 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001181 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001182 std::string Name = Lex.getCurStrVal();
1183 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001184 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001185
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001186 // Value ::= ID '<' ValueListNE '>'
1187 if (Lex.Lex() == tgtok::greater) {
1188 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001189 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001190 }
David Greene8618f952009-06-08 20:23:18 +00001191
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001192 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1193 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1194 // body.
1195 Record *Class = Records.getClass(Name);
1196 if (!Class) {
1197 Error(NameLoc, "Expected a class name, got '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001198 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001199 }
David Greene8618f952009-06-08 20:23:18 +00001200
David Greeneaf8ee2c2011-07-29 22:43:06 +00001201 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
Craig Topper011817a2014-04-09 04:50:04 +00001202 if (ValueList.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001203
David Greene8618f952009-06-08 20:23:18 +00001204 if (Lex.getCode() != tgtok::greater) {
1205 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001206 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001207 }
1208 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001209 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001210
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001211 // Create the new record, set it as CurRec temporarily.
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00001212 auto NewRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), NameLoc,
1213 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001214 Record *NewRec = NewRecOwner.get(); // Keep a copy since we may release.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001215 SubClassReference SCRef;
Jordan Rosef12e8a92013-01-10 18:50:11 +00001216 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001217 SCRef.Rec = Class;
1218 SCRef.TemplateArgs = ValueList;
1219 // Add info about the subclass to NewRec.
Craig Topper84138712014-11-29 05:31:10 +00001220 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001221 return nullptr;
Craig Topper84138712014-11-29 05:31:10 +00001222
Hal Finkela8c1f462014-01-02 20:47:09 +00001223 if (!CurMultiClass) {
1224 NewRec->resolveReferences();
Craig Toppercdab2322014-11-29 05:52:51 +00001225 Records.addDef(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001226 } else {
Adam Nemete5a07162014-09-16 17:14:13 +00001227 // This needs to get resolved once the multiclass template arguments are
1228 // known before any use.
1229 NewRec->setResolveFirst(true);
Hal Finkela8c1f462014-01-02 20:47:09 +00001230 // Otherwise, we're inside a multiclass, add it to the multiclass.
Craig Topperc3504c42014-12-11 05:25:33 +00001231 CurMultiClass->DefPrototypes.push_back(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001232
1233 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00001234 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
1235 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Hal Finkela8c1f462014-01-02 20:47:09 +00001236 assert(RV && "Template arg doesn't exist?");
1237 NewRec->addValue(*RV);
1238 }
1239
1240 // We can't return the prototype def here, instead return:
1241 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1242 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1243 assert(MCNameRV && "multiclass record must have a NAME");
1244
1245 return UnOpInit::get(UnOpInit::CAST,
1246 BinOpInit::get(BinOpInit::STRCONCAT,
1247 VarInit::get(MCNameRV->getName(),
1248 MCNameRV->getType()),
1249 NewRec->getNameInit(),
1250 StringRecTy::get()),
1251 Class->getDefInit()->getType());
1252 }
Bob Wilson7248f862009-11-22 04:24:42 +00001253
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001254 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001255 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001256 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001257 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001258 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001259 Lex.Lex(); // eat the '{'
David Greeneaf8ee2c2011-07-29 22:43:06 +00001260 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001261
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001262 if (Lex.getCode() != tgtok::r_brace) {
1263 Vals = ParseValueList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001264 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001265 }
1266 if (Lex.getCode() != tgtok::r_brace) {
1267 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001268 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001269 }
1270 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001271
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001272 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001273
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001274 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1275 // first. We'll first read everything in to a vector, then we can reverse
1276 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001277 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001278 // FIXME: The following two loops would not be duplicated
1279 // if the API was a little more orthogonal.
1280
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001281 // bits<n> values are allowed to initialize n bits.
1282 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1283 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1284 NewBits.push_back(BI->getBit((e - i) - 1));
1285 continue;
1286 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001287 // bits<n> can also come from variable initializers.
1288 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1289 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1290 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1291 NewBits.push_back(VI->getBit((e - i) - 1));
1292 continue;
1293 }
1294 // Fallthrough to try convert this to a bit.
1295 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001296 // All other values must be convertible to just a single bit.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001297 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001298 if (!Bit) {
Chris Lattner52416952007-11-22 21:06:59 +00001299 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1300 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001301 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001302 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001303 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001304 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001305 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001306 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001307 }
1308 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1309 Lex.Lex(); // eat the '['
David Greeneaf8ee2c2011-07-29 22:43:06 +00001310 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001311
Craig Topper011817a2014-04-09 04:50:04 +00001312 RecTy *DeducedEltTy = nullptr;
1313 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001314
Craig Topper011817a2014-04-09 04:50:04 +00001315 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001316 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001317 if (!ListType) {
Alp Tokere69170a2014-06-26 22:52:05 +00001318 std::string s;
1319 raw_string_ostream ss(s);
Reid Klecknerd78273f2013-08-06 22:51:21 +00001320 ss << "Type mismatch for list, expected list type, got "
1321 << ItemType->getAsString();
1322 TokError(ss.str());
Craig Topper011817a2014-04-09 04:50:04 +00001323 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001324 }
1325 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001326 }
David Greene8618f952009-06-08 20:23:18 +00001327
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001328 if (Lex.getCode() != tgtok::r_square) {
Craig Topper011817a2014-04-09 04:50:04 +00001329 Vals = ParseValueList(CurRec, nullptr,
1330 GivenListTy ? GivenListTy->getElementType() : nullptr);
1331 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001332 }
1333 if (Lex.getCode() != tgtok::r_square) {
1334 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001335 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001336 }
1337 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001338
Craig Topper011817a2014-04-09 04:50:04 +00001339 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001340 if (Lex.getCode() == tgtok::less) {
1341 // Optional list element type
1342 Lex.Lex(); // eat the '<'
1343
1344 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001345 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001346 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001347 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001348 }
1349
1350 if (Lex.getCode() != tgtok::greater) {
1351 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001352 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001353 }
1354 Lex.Lex(); // eat the '>'
1355 }
1356
1357 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001358 RecTy *EltTy = nullptr;
Craig Toppereb4d7c62015-04-29 04:43:36 +00001359 for (Init *V : Vals) {
1360 TypedInit *TArg = dyn_cast<TypedInit>(V);
Craig Topper011817a2014-04-09 04:50:04 +00001361 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001362 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001363 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001364 }
Craig Topper011817a2014-04-09 04:50:04 +00001365 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001366 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001367 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001368 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001369 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001370 }
Bob Wilson7248f862009-11-22 04:24:42 +00001371 } else {
David Greene8618f952009-06-08 20:23:18 +00001372 EltTy = TArg->getType();
1373 }
1374 }
1375
Craig Topper011817a2014-04-09 04:50:04 +00001376 if (GivenEltTy) {
1377 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001378 // Verify consistency
1379 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1380 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001381 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001382 }
1383 }
1384 EltTy = GivenEltTy;
1385 }
1386
Craig Topper011817a2014-04-09 04:50:04 +00001387 if (!EltTy) {
1388 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001389 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001390 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001391 }
1392 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001393 } else {
David Greene8618f952009-06-08 20:23:18 +00001394 // Make sure the deduced type is compatible with the given type
1395 if (GivenListTy) {
1396 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1397 TokError("Element type mismatch for list");
Craig Topper011817a2014-04-09 04:50:04 +00001398 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001399 }
1400 }
1401 DeducedEltTy = EltTy;
1402 }
Bob Wilson7248f862009-11-22 04:24:42 +00001403
David Greenee32ebf22011-07-29 19:07:07 +00001404 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001405 }
1406 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1407 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001408 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001409 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001410 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001411 }
Bob Wilson7248f862009-11-22 04:24:42 +00001412
David Greeneaf8ee2c2011-07-29 22:43:06 +00001413 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001414 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001415
Nate Begemandbe3f772009-03-19 05:21:56 +00001416 // If the operator name is present, parse it.
1417 std::string OperatorName;
1418 if (Lex.getCode() == tgtok::colon) {
1419 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1420 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001421 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001422 }
1423 OperatorName = Lex.getCurStrVal();
1424 Lex.Lex(); // eat the VarName.
1425 }
Bob Wilson7248f862009-11-22 04:24:42 +00001426
David Greeneaf8ee2c2011-07-29 22:43:06 +00001427 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001428 if (Lex.getCode() != tgtok::r_paren) {
1429 DagArgs = ParseDagArgList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001430 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001431 }
Bob Wilson7248f862009-11-22 04:24:42 +00001432
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001433 if (Lex.getCode() != tgtok::r_paren) {
1434 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001435 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001436 }
1437 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001438
David Greenee32ebf22011-07-29 19:07:07 +00001439 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001440 }
Bob Wilson7248f862009-11-22 04:24:42 +00001441
David Greene2f7cf7f2011-01-07 17:05:37 +00001442 case tgtok::XHead:
1443 case tgtok::XTail:
1444 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001445 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001446 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001447 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001448 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +00001449 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001450 case tgtok::XSRL:
1451 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001452 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001453 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001454 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001455 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001456 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001457 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001458 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001459 }
1460 }
Bob Wilson7248f862009-11-22 04:24:42 +00001461
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001462 return R;
1463}
1464
1465/// ParseValue - Parse a tblgen value. This returns null on error.
1466///
1467/// Value ::= SimpleValue ValueSuffix*
1468/// ValueSuffix ::= '{' BitList '}'
1469/// ValueSuffix ::= '[' BitList ']'
1470/// ValueSuffix ::= '.' ID
1471///
David Greened4263a62011-10-19 13:04:20 +00001472Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1473 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001474 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001475
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001476 // Parse the suffixes now if present.
1477 while (1) {
1478 switch (Lex.getCode()) {
1479 default: return Result;
1480 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001481 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001482 // This is the beginning of the object body.
1483 return Result;
1484
Chris Lattner526c8cb2009-06-21 03:39:35 +00001485 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001486 Lex.Lex(); // eat the '{'
1487 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001488 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001489
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001490 // Reverse the bitlist.
1491 std::reverse(Ranges.begin(), Ranges.end());
1492 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001493 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001494 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001495 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001496 }
Bob Wilson7248f862009-11-22 04:24:42 +00001497
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001498 // Eat the '}'.
1499 if (Lex.getCode() != tgtok::r_brace) {
1500 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001501 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001502 }
1503 Lex.Lex();
1504 break;
1505 }
1506 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001507 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001508 Lex.Lex(); // eat the '['
1509 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001510 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001511
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001512 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001513 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001514 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001515 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001516 }
Bob Wilson7248f862009-11-22 04:24:42 +00001517
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001518 // Eat the ']'.
1519 if (Lex.getCode() != tgtok::r_square) {
1520 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001521 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001522 }
1523 Lex.Lex();
1524 break;
1525 }
1526 case tgtok::period:
1527 if (Lex.Lex() != tgtok::Id) { // eat the .
1528 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001529 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001530 }
1531 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001532 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001533 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001534 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001535 }
David Greenee32ebf22011-07-29 19:07:07 +00001536 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001537 Lex.Lex(); // eat field name
1538 break;
David Greene8e85b482011-10-19 13:04:43 +00001539
1540 case tgtok::paste:
1541 SMLoc PasteLoc = Lex.getLoc();
1542
1543 // Create a !strconcat() operation, first casting each operand to
1544 // a string if necessary.
1545
Sean Silvafb509ed2012-10-10 20:24:43 +00001546 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001547 if (!LHS) {
1548 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001549 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001550 }
Craig Toppera9642b42015-05-04 01:35:39 +00001551
David Greene8e85b482011-10-19 13:04:43 +00001552 if (LHS->getType() != StringRecTy::get()) {
1553 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1554 }
1555
Craig Topper011817a2014-04-09 04:50:04 +00001556 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001557
1558 Lex.Lex(); // Eat the '#'.
1559 switch (Lex.getCode()) {
1560 case tgtok::colon:
1561 case tgtok::semi:
1562 case tgtok::l_brace:
1563 // These are all of the tokens that can begin an object body.
1564 // Some of these can also begin values but we disallow those cases
1565 // because they are unlikely to be useful.
Craig Toppera9642b42015-05-04 01:35:39 +00001566
David Greene8e85b482011-10-19 13:04:43 +00001567 // Trailing paste, concat with an empty string.
1568 RHS = StringInit::get("");
1569 break;
1570
1571 default:
1572 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001573 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001574 if (!RHS) {
1575 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001576 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001577 }
1578
1579 if (RHS->getType() != StringRecTy::get()) {
1580 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1581 }
Craig Toppera9642b42015-05-04 01:35:39 +00001582
David Greene8e85b482011-10-19 13:04:43 +00001583 break;
1584 }
1585
1586 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1587 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1588 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001589 }
1590 }
1591}
1592
1593/// ParseDagArgList - Parse the argument list for a dag literal expression.
1594///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001595/// DagArg ::= Value (':' VARNAME)?
1596/// DagArg ::= VARNAME
1597/// DagArgList ::= DagArg
1598/// DagArgList ::= DagArgList ',' DagArg
David Greeneaf8ee2c2011-07-29 22:43:06 +00001599std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001600TGParser::ParseDagArgList(Record *CurRec) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001601 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson7248f862009-11-22 04:24:42 +00001602
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001603 while (1) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001604 // DagArg ::= VARNAME
1605 if (Lex.getCode() == tgtok::VarName) {
1606 // A missing value is treated like '?'.
1607 Result.push_back(std::make_pair(UnsetInit::get(), Lex.getCurStrVal()));
1608 Lex.Lex();
1609 } else {
1610 // DagArg ::= Value (':' VARNAME)?
1611 Init *Val = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001612 if (!Val)
David Greeneaf8ee2c2011-07-29 22:43:06 +00001613 return std::vector<std::pair<llvm::Init*, std::string> >();
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001614
1615 // If the variable name is present, add it.
1616 std::string VarName;
1617 if (Lex.getCode() == tgtok::colon) {
1618 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1619 TokError("expected variable name in dag literal");
1620 return std::vector<std::pair<llvm::Init*, std::string> >();
1621 }
1622 VarName = Lex.getCurStrVal();
1623 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001624 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001625
1626 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001627 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001628 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001629 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001630 }
Bob Wilson7248f862009-11-22 04:24:42 +00001631
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001632 return Result;
1633}
1634
1635
1636/// ParseValueList - Parse a comma separated list of values, returning them as a
1637/// vector. Note that this always expects to be able to parse at least one
1638/// value. It returns an empty list if this is not possible.
1639///
1640/// ValueList ::= Value (',' Value)
1641///
David Greeneaf8ee2c2011-07-29 22:43:06 +00001642std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopher71520a82011-07-11 23:06:52 +00001643 RecTy *EltTy) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001644 std::vector<Init*> Result;
David Greene8618f952009-06-08 20:23:18 +00001645 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001646 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001647 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001648 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001649 if (TArgs.empty()) {
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001650 TokError("template argument provided to non-template class");
1651 return std::vector<Init*>();
1652 }
David Greene8618f952009-06-08 20:23:18 +00001653 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001654 if (!RV) {
1655 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1656 << ")\n";
1657 }
David Greene8618f952009-06-08 20:23:18 +00001658 assert(RV && "Template argument record not found??");
1659 ItemType = RV->getType();
1660 ++ArgN;
1661 }
1662 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001663 if (!Result.back()) return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001664
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001665 while (Lex.getCode() == tgtok::comma) {
1666 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001667
Craig Topper011817a2014-04-09 04:50:04 +00001668 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001669 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001670 if (ArgN >= TArgs.size()) {
1671 TokError("too many template arguments");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001672 return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001673 }
David Greene8618f952009-06-08 20:23:18 +00001674 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1675 assert(RV && "Template argument record not found??");
1676 ItemType = RV->getType();
1677 ++ArgN;
1678 }
1679 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001680 if (!Result.back()) return std::vector<Init*>();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001681 }
Bob Wilson7248f862009-11-22 04:24:42 +00001682
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001683 return Result;
1684}
1685
1686
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001687/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1688/// empty string on error. This can happen in a number of different context's,
1689/// including within a def or in the template args for a def (which which case
1690/// CurRec will be non-null) and within the template args for a multiclass (in
1691/// which case CurRec will be null, but CurMultiClass will be set). This can
1692/// also happen within a def that is within a multiclass, which will set both
1693/// CurRec and CurMultiClass.
1694///
1695/// Declaration ::= FIELD? Type ID ('=' Value)?
1696///
David Greenedb10e692011-10-19 13:02:42 +00001697Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001698 bool ParsingTemplateArgs) {
1699 // Read the field prefix if present.
1700 bool HasField = Lex.getCode() == tgtok::Field;
1701 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001702
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001703 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001704 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001705
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001706 if (Lex.getCode() != tgtok::Id) {
1707 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001708 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001709 }
Bob Wilson7248f862009-11-22 04:24:42 +00001710
Chris Lattner526c8cb2009-06-21 03:39:35 +00001711 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001712 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001713 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001714
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001715 if (ParsingTemplateArgs) {
Craig Toppera9642b42015-05-04 01:35:39 +00001716 if (CurRec)
David Greenedb10e692011-10-19 13:02:42 +00001717 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Craig Toppera9642b42015-05-04 01:35:39 +00001718 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001719 assert(CurMultiClass);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001720 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001721 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1722 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001723 }
Bob Wilson7248f862009-11-22 04:24:42 +00001724
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001725 // Add the value.
1726 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001727 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001728
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001729 // If a value is present, parse it.
1730 if (Lex.getCode() == tgtok::equal) {
1731 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001732 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001733 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001734 if (!Val ||
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001735 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001736 // Return the name, even if an error is thrown. This is so that we can
1737 // continue to make some progress, even without the value having been
1738 // initialized.
1739 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001740 }
Bob Wilson7248f862009-11-22 04:24:42 +00001741
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001742 return DeclName;
1743}
1744
David Greenefb927af2012-02-22 16:09:41 +00001745/// ParseForeachDeclaration - Read a foreach declaration, returning
1746/// the name of the declared object or a NULL Init on error. Return
1747/// the name of the parsed initializer list through ForeachListName.
1748///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001749/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1750/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1751/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001752///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001753VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001754 if (Lex.getCode() != tgtok::Id) {
1755 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001756 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001757 }
1758
1759 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1760 Lex.Lex();
1761
1762 // If a value is present, parse it.
1763 if (Lex.getCode() != tgtok::equal) {
1764 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001765 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001766 }
1767 Lex.Lex(); // Eat the '='
1768
Craig Topper011817a2014-04-09 04:50:04 +00001769 RecTy *IterType = nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001770 std::vector<unsigned> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001771
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001772 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001773 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001774 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001775 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001776 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001777 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001778 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001779 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001780 }
1781 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001782 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001783 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001784 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001785 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001786 }
1787 IterType = ListType->getElementType();
1788 break;
David Greenefb927af2012-02-22 16:09:41 +00001789 }
1790
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001791 case tgtok::IntVal: { // RangePiece.
1792 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001793 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001794 break;
David Greenefb927af2012-02-22 16:09:41 +00001795 }
1796
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001797 case tgtok::l_brace: { // '{' RangeList '}'
1798 Lex.Lex(); // eat the '{'
1799 Ranges = ParseRangeList();
1800 if (Lex.getCode() != tgtok::r_brace) {
1801 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001802 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001803 }
1804 Lex.Lex();
1805 break;
1806 }
1807 }
David Greenefb927af2012-02-22 16:09:41 +00001808
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001809 if (!Ranges.empty()) {
1810 assert(!IterType && "Type already initialized?");
1811 IterType = IntRecTy::get();
1812 std::vector<Init*> Values;
1813 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
1814 Values.push_back(IntInit::get(Ranges[i]));
1815 ForeachListValue = ListInit::get(Values, IterType);
1816 }
1817
1818 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001819 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001820
1821 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001822}
1823
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001824/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1825/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1826/// template args for a def, which may or may not be in a multiclass. If null,
1827/// these are the template args for a multiclass.
1828///
1829/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001830///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001831bool TGParser::ParseTemplateArgList(Record *CurRec) {
1832 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1833 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001834
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001835 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001836
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001837 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001838 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001839 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001840 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001841
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001842 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001843
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001844 while (Lex.getCode() == tgtok::comma) {
1845 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001846
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001847 // Read the following declarations.
1848 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001849 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001850 return true;
1851 TheRecToAddTo->addTemplateArg(TemplArg);
1852 }
Bob Wilson7248f862009-11-22 04:24:42 +00001853
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001854 if (Lex.getCode() != tgtok::greater)
1855 return TokError("expected '>' at end of template argument list");
1856 Lex.Lex(); // eat the '>'.
1857 return false;
1858}
1859
1860
1861/// ParseBodyItem - Parse a single item at within the body of a def or class.
1862///
1863/// BodyItem ::= Declaration ';'
1864/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1865bool TGParser::ParseBodyItem(Record *CurRec) {
1866 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001867 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001868 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001869
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001870 if (Lex.getCode() != tgtok::semi)
1871 return TokError("expected ';' after declaration");
1872 Lex.Lex();
1873 return false;
1874 }
1875
1876 // LET ID OptionalRangeList '=' Value ';'
1877 if (Lex.Lex() != tgtok::Id)
1878 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001879
Chris Lattner526c8cb2009-06-21 03:39:35 +00001880 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001881 std::string FieldName = Lex.getCurStrVal();
1882 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001883
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001884 std::vector<unsigned> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001885 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001886 return true;
1887 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001888
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001889 if (Lex.getCode() != tgtok::equal)
1890 return TokError("expected '=' in let expression");
1891 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001892
David Greene8618f952009-06-08 20:23:18 +00001893 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001894 if (!Field)
David Greene8618f952009-06-08 20:23:18 +00001895 return TokError("Value '" + FieldName + "' unknown!");
1896
1897 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001898
David Greeneaf8ee2c2011-07-29 22:43:06 +00001899 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001900 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001901
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001902 if (Lex.getCode() != tgtok::semi)
1903 return TokError("expected ';' after let expression");
1904 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001905
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001906 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1907}
1908
1909/// ParseBody - Read the body of a class or def. Return true on error, false on
1910/// success.
1911///
1912/// Body ::= ';'
1913/// Body ::= '{' BodyList '}'
1914/// BodyList BodyItem*
1915///
1916bool TGParser::ParseBody(Record *CurRec) {
1917 // If this is a null definition, just eat the semi and return.
1918 if (Lex.getCode() == tgtok::semi) {
1919 Lex.Lex();
1920 return false;
1921 }
Bob Wilson7248f862009-11-22 04:24:42 +00001922
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001923 if (Lex.getCode() != tgtok::l_brace)
1924 return TokError("Expected ';' or '{' to start body");
1925 // Eat the '{'.
1926 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001927
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001928 while (Lex.getCode() != tgtok::r_brace)
1929 if (ParseBodyItem(CurRec))
1930 return true;
1931
1932 // Eat the '}'.
1933 Lex.Lex();
1934 return false;
1935}
1936
Sean Silvacb1a75e2013-01-09 04:49:14 +00001937/// \brief Apply the current let bindings to \a CurRec.
1938/// \returns true on error, false otherwise.
1939bool TGParser::ApplyLetStack(Record *CurRec) {
1940 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1941 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1942 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1943 LetStack[i][j].Bits, LetStack[i][j].Value))
1944 return true;
1945 return false;
1946}
1947
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001948/// ParseObjectBody - Parse the body of a def or class. This consists of an
1949/// optional ClassList followed by a Body. CurRec is the current def or class
1950/// that is being parsed.
1951///
1952/// ObjectBody ::= BaseClassList Body
1953/// BaseClassList ::= /*empty*/
1954/// BaseClassList ::= ':' BaseClassListNE
1955/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1956///
1957bool TGParser::ParseObjectBody(Record *CurRec) {
1958 // If there is a baseclass list, read it.
1959 if (Lex.getCode() == tgtok::colon) {
1960 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001961
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001962 // Read all of the subclasses.
1963 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1964 while (1) {
1965 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00001966 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001967
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001968 // Add it.
1969 if (AddSubClass(CurRec, SubClass))
1970 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001971
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001972 if (Lex.getCode() != tgtok::comma) break;
1973 Lex.Lex(); // eat ','.
1974 SubClass = ParseSubClassReference(CurRec, false);
1975 }
1976 }
1977
Sean Silvacb1a75e2013-01-09 04:49:14 +00001978 if (ApplyLetStack(CurRec))
1979 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001980
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001981 return ParseBody(CurRec);
1982}
1983
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001984/// ParseDef - Parse and return a top level or multiclass def, return the record
1985/// corresponding to it. This returns null on error.
1986///
1987/// DefInst ::= DEF ObjectName ObjectBody
1988///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00001989bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001990 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001991 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00001992 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001993
1994 // Parse ObjectName and make a record for it.
Craig Topper84138712014-11-29 05:31:10 +00001995 std::unique_ptr<Record> CurRecOwner;
Jordan Roseabdd99b2013-01-10 18:50:05 +00001996 Init *Name = ParseObjectName(CurMultiClass);
1997 if (Name)
Craig Topper84138712014-11-29 05:31:10 +00001998 CurRecOwner = make_unique<Record>(Name, DefLoc, Records);
Jordan Roseabdd99b2013-01-10 18:50:05 +00001999 else
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00002000 CurRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), DefLoc,
2001 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00002002 Record *CurRec = CurRecOwner.get(); // Keep a copy since we may release.
Bob Wilson7248f862009-11-22 04:24:42 +00002003
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002004 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002005 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002006
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002007 // Ensure redefinition doesn't happen.
Craig Topper84138712014-11-29 05:31:10 +00002008 if (Records.getDef(CurRec->getNameInitAsString()))
2009 return Error(DefLoc, "def '" + CurRec->getNameInitAsString()+
2010 "' already defined");
Craig Toppercdab2322014-11-29 05:52:51 +00002011 Records.addDef(std::move(CurRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00002012
2013 if (ParseObjectBody(CurRec))
2014 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002015 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002016 // Parse the body before adding this prototype to the DefPrototypes vector.
2017 // That way implicit definitions will be added to the DefPrototypes vector
2018 // before this object, instantiated prior to defs derived from this object,
2019 // and this available for indirect name resolution when defs derived from
2020 // this object are instantiated.
Craig Topper84138712014-11-29 05:31:10 +00002021 if (ParseObjectBody(CurRec))
Hal Finkela8c1f462014-01-02 20:47:09 +00002022 return true;
2023
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002024 // Otherwise, a def inside a multiclass, add it to the multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002025 for (const auto &Proto : CurMultiClass->DefPrototypes)
2026 if (Proto->getNameInit() == CurRec->getNameInit())
Craig Topper84138712014-11-29 05:31:10 +00002027 return Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
2028 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002029 CurMultiClass->DefPrototypes.push_back(std::move(CurRecOwner));
Anton Yartsev671dff12014-08-08 00:29:54 +00002030 } else if (ParseObjectBody(CurRec)) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002031 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002032 }
Bob Wilson7248f862009-11-22 04:24:42 +00002033
Craig Topper011817a2014-04-09 04:50:04 +00002034 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002035 // See Record::setName(). This resolve step will see any new name
2036 // for the def that might have been created when resolving
2037 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002038 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002039
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002040 // If ObjectBody has template arguments, it's an error.
2041 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002042
2043 if (CurMultiClass) {
2044 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002045 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
2046 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002047 assert(RV && "Template arg doesn't exist?");
2048 CurRec->addValue(*RV);
2049 }
2050 }
2051
Craig Toppera9642b42015-05-04 01:35:39 +00002052 if (ProcessForeachDefs(CurRec, DefLoc))
Craig Topper84138712014-11-29 05:31:10 +00002053 return Error(DefLoc, "Could not process loops for def" +
2054 CurRec->getNameInitAsString());
David Greenefb927af2012-02-22 16:09:41 +00002055
2056 return false;
2057}
2058
2059/// ParseForeach - Parse a for statement. Return the record corresponding
2060/// to it. This returns true on error.
2061///
2062/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2063/// Foreach ::= FOREACH Declaration IN Object
2064///
2065bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2066 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2067 Lex.Lex(); // Eat the 'for' token.
2068
2069 // Make a temporary object to record items associated with the for
2070 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002071 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002072 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002073 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002074 return TokError("expected declaration in for");
2075
2076 if (Lex.getCode() != tgtok::In)
2077 return TokError("Unknown tok");
2078 Lex.Lex(); // Eat the in
2079
2080 // Create a loop object and remember it.
2081 Loops.push_back(ForeachLoop(IterName, ListValue));
2082
2083 if (Lex.getCode() != tgtok::l_brace) {
2084 // FOREACH Declaration IN Object
2085 if (ParseObject(CurMultiClass))
2086 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002087 } else {
David Greenefb927af2012-02-22 16:09:41 +00002088 SMLoc BraceLoc = Lex.getLoc();
2089 // Otherwise, this is a group foreach.
2090 Lex.Lex(); // eat the '{'.
2091
2092 // Parse the object list.
2093 if (ParseObjectList(CurMultiClass))
2094 return true;
2095
2096 if (Lex.getCode() != tgtok::r_brace) {
2097 TokError("expected '}' at end of foreach command");
2098 return Error(BraceLoc, "to match this '{'");
2099 }
2100 Lex.Lex(); // Eat the }
2101 }
2102
2103 // We've processed everything in this loop.
2104 Loops.pop_back();
2105
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002106 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002107}
2108
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002109/// ParseClass - Parse a tblgen class definition.
2110///
2111/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2112///
2113bool TGParser::ParseClass() {
2114 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2115 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002116
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002117 if (Lex.getCode() != tgtok::Id)
2118 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002119
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002120 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2121 if (CurRec) {
2122 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002123 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002124 !CurRec->getSuperClasses().empty() ||
2125 !CurRec->getTemplateArgs().empty())
Craig Topper85c07002015-04-30 05:54:22 +00002126 return TokError("Class '" + CurRec->getNameInitAsString() +
2127 "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002128 } else {
2129 // If this is the first reference to this class, create and add it.
Hans Wennborgffbbd532014-11-30 00:31:49 +00002130 auto NewRec =
2131 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
Craig Toppercdab2322014-11-29 05:52:51 +00002132 CurRec = NewRec.get();
2133 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002134 }
2135 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002136
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002137 // If there are template args, parse them.
2138 if (Lex.getCode() == tgtok::less)
2139 if (ParseTemplateArgList(CurRec))
2140 return true;
2141
2142 // Finally, parse the object body.
2143 return ParseObjectBody(CurRec);
2144}
2145
2146/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2147/// of LetRecords.
2148///
2149/// LetList ::= LetItem (',' LetItem)*
2150/// LetItem ::= ID OptionalRangeList '=' Value
2151///
2152std::vector<LetRecord> TGParser::ParseLetList() {
2153 std::vector<LetRecord> Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002154
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002155 while (1) {
2156 if (Lex.getCode() != tgtok::Id) {
2157 TokError("expected identifier in let definition");
2158 return std::vector<LetRecord>();
2159 }
2160 std::string Name = Lex.getCurStrVal();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002161 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002162 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002163
2164 // Check for an optional RangeList.
2165 std::vector<unsigned> Bits;
Bob Wilson7248f862009-11-22 04:24:42 +00002166 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002167 return std::vector<LetRecord>();
2168 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002169
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002170 if (Lex.getCode() != tgtok::equal) {
2171 TokError("expected '=' in let expression");
2172 return std::vector<LetRecord>();
2173 }
2174 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002175
Craig Topper011817a2014-04-09 04:50:04 +00002176 Init *Val = ParseValue(nullptr);
2177 if (!Val) return std::vector<LetRecord>();
Bob Wilson7248f862009-11-22 04:24:42 +00002178
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002179 // Now that we have everything, add the record.
2180 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson7248f862009-11-22 04:24:42 +00002181
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002182 if (Lex.getCode() != tgtok::comma)
2183 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002184 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002185 }
2186}
2187
2188/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002189/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002190///
2191/// Object ::= LET LetList IN '{' ObjectList '}'
2192/// Object ::= LET LetList IN Object
2193///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002194bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002195 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2196 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002197
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002198 // Add this entry to the let stack.
2199 std::vector<LetRecord> LetInfo = ParseLetList();
2200 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002201 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002202
2203 if (Lex.getCode() != tgtok::In)
2204 return TokError("expected 'in' at end of top-level 'let'");
2205 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002206
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002207 // If this is a scalar let, just handle it now
2208 if (Lex.getCode() != tgtok::l_brace) {
2209 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002210 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002211 return true;
2212 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002213 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002214 // Otherwise, this is a group let.
2215 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002216
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002217 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002218 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002219 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002220
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002221 if (Lex.getCode() != tgtok::r_brace) {
2222 TokError("expected '}' at end of top level let command");
2223 return Error(BraceLoc, "to match this '{'");
2224 }
2225 Lex.Lex();
2226 }
Bob Wilson7248f862009-11-22 04:24:42 +00002227
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002228 // Outside this let scope, this let block is not active.
2229 LetStack.pop_back();
2230 return false;
2231}
2232
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002233/// ParseMultiClass - Parse a multiclass definition.
2234///
Bob Wilson3d948162009-04-28 19:41:44 +00002235/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002236/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2237/// MultiClassObject ::= DefInst
2238/// MultiClassObject ::= MultiClassInst
2239/// MultiClassObject ::= DefMInst
2240/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2241/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002242///
2243bool TGParser::ParseMultiClass() {
2244 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2245 Lex.Lex(); // Eat the multiclass token.
2246
2247 if (Lex.getCode() != tgtok::Id)
2248 return TokError("expected identifier after multiclass for name");
2249 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002250
Craig Topper7adf2bf2014-12-11 05:25:30 +00002251 auto Result =
2252 MultiClasses.insert(std::make_pair(Name,
2253 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2254
2255 if (!Result.second)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002256 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002257
Craig Topper7adf2bf2014-12-11 05:25:30 +00002258 CurMultiClass = Result.first->second.get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002259 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002260
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002261 // If there are template args, parse them.
2262 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002263 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002264 return true;
2265
David Greene7049e792009-04-24 16:55:41 +00002266 bool inherits = false;
2267
David Greene753ed8f2009-04-22 16:42:54 +00002268 // If there are submulticlasses, parse them.
2269 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002270 inherits = true;
2271
David Greene753ed8f2009-04-22 16:42:54 +00002272 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002273
David Greene753ed8f2009-04-22 16:42:54 +00002274 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002275 SubMultiClassReference SubMultiClass =
2276 ParseSubMultiClassReference(CurMultiClass);
David Greene753ed8f2009-04-22 16:42:54 +00002277 while (1) {
2278 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002279 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002280
David Greene753ed8f2009-04-22 16:42:54 +00002281 // Add it.
2282 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2283 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002284
David Greene753ed8f2009-04-22 16:42:54 +00002285 if (Lex.getCode() != tgtok::comma) break;
2286 Lex.Lex(); // eat ','.
2287 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2288 }
2289 }
2290
David Greene7049e792009-04-24 16:55:41 +00002291 if (Lex.getCode() != tgtok::l_brace) {
2292 if (!inherits)
2293 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002294 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002295 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002296 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002297 } else {
David Greene7049e792009-04-24 16:55:41 +00002298 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2299 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002300
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002301 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002302 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002303 default:
2304 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
2305 case tgtok::Let:
2306 case tgtok::Def:
2307 case tgtok::Defm:
2308 case tgtok::Foreach:
2309 if (ParseObject(CurMultiClass))
2310 return true;
2311 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002312 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002313 }
David Greene7049e792009-04-24 16:55:41 +00002314 Lex.Lex(); // eat the '}'.
2315 }
Bob Wilson7248f862009-11-22 04:24:42 +00002316
Craig Topper011817a2014-04-09 04:50:04 +00002317 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002318 return false;
2319}
2320
David Greenedb445972011-10-05 22:42:07 +00002321Record *TGParser::
2322InstantiateMulticlassDef(MultiClass &MC,
2323 Record *DefProto,
Hal Finkelf2a0b2b2014-01-02 19:35:33 +00002324 Init *&DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002325 SMRange DefmPrefixRange) {
David Greene5d5d88c2011-10-19 13:04:31 +00002326 // We need to preserve DefProto so it can be reused for later
2327 // instantiations, so create a new Record to inherit from it.
2328
David Greenedb445972011-10-05 22:42:07 +00002329 // Add in the defm name. If the defm prefix is empty, give each
2330 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2331 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2332 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002333
Jordan Roseabdd99b2013-01-10 18:50:05 +00002334 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002335 if (!DefmPrefix) {
David Greene5d5d88c2011-10-19 13:04:31 +00002336 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002337 IsAnonymous = true;
2338 }
David Greene5d5d88c2011-10-19 13:04:31 +00002339
2340 Init *DefName = DefProto->getNameInit();
2341
Sean Silvafb509ed2012-10-10 20:24:43 +00002342 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002343
Craig Topper011817a2014-04-09 04:50:04 +00002344 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002345 // We have a fully expanded string so there are no operators to
2346 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002347 DefName =
2348 BinOpInit::get(BinOpInit::STRCONCAT,
2349 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2350 StringRecTy::get())->Fold(DefProto, &MC),
2351 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2352 }
David Greene5d5d88c2011-10-19 13:04:31 +00002353
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002354 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002355 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002356 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Craig Topper84138712014-11-29 05:31:10 +00002357 auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002358
2359 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002360 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002361 Ref.Rec = DefProto;
Craig Topper84138712014-11-29 05:31:10 +00002362 AddSubClass(CurRec.get(), Ref);
David Greenedb445972011-10-05 22:42:07 +00002363
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002364 // Set the value for NAME. We don't resolve references to it 'til later,
2365 // though, so that uses in nested multiclass names don't get
2366 // confused.
Craig Topper84138712014-11-29 05:31:10 +00002367 if (SetValue(CurRec.get(), Ref.RefRange.Start, "NAME",
2368 std::vector<unsigned>(), DefmPrefix)) {
Craig Topper85c07002015-04-30 05:54:22 +00002369 Error(DefmPrefixRange.Start, "Could not resolve " +
2370 CurRec->getNameInitAsString() + ":NAME to '" +
2371 DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002372 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002373 }
David Greene8bf0d722011-10-19 13:04:35 +00002374
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002375 // If the DefNameString didn't resolve, we probably have a reference to
2376 // NAME and need to replace it. We need to do at least this much greedily,
2377 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002378 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002379 RecordVal *DefNameRV = CurRec->getValue("NAME");
2380 CurRec->resolveReferencesTo(DefNameRV);
2381 }
2382
2383 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002384 // Now that we're at the top level, resolve all NAME references
2385 // in the resultant defs that weren't in the def names themselves.
2386 RecordVal *DefNameRV = CurRec->getValue("NAME");
2387 CurRec->resolveReferencesTo(DefNameRV);
2388
2389 // Now that NAME references are resolved and we're at the top level of
2390 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002391 // currently in a multiclass, it means this defm appears inside a
2392 // multiclass and its name won't be fully resolvable until we see
2393 // the top-level defm. Therefore, we don't add this to the
2394 // RecordKeeper at this point. If we did we could get duplicate
2395 // defs as more than one probably refers to NAME or some other
2396 // common internal placeholder.
2397
2398 // Ensure redefinition doesn't happen.
2399 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002400 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002401 "' already defined, instantiating defm with subdef '" +
2402 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002403 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002404 }
2405
Craig Topper84138712014-11-29 05:31:10 +00002406 Record *CurRecSave = CurRec.get(); // Keep a copy before we release.
Craig Toppercdab2322014-11-29 05:52:51 +00002407 Records.addDef(std::move(CurRec));
Craig Topper84138712014-11-29 05:31:10 +00002408 return CurRecSave;
David Greene8bf0d722011-10-19 13:04:35 +00002409 }
2410
Craig Topper84138712014-11-29 05:31:10 +00002411 // FIXME This is bad but the ownership transfer to caller is pretty messy.
2412 // The unique_ptr in this function at least protects the exits above.
2413 return CurRec.release();
David Greenedb445972011-10-05 22:42:07 +00002414}
2415
2416bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2417 Record *CurRec,
2418 SMLoc DefmPrefixLoc,
2419 SMLoc SubClassLoc,
David Greenedb10e692011-10-19 13:02:42 +00002420 const std::vector<Init *> &TArgs,
David Greenedb445972011-10-05 22:42:07 +00002421 std::vector<Init *> &TemplateVals,
2422 bool DeleteArgs) {
2423 // Loop over all of the template arguments, setting them to the specified
2424 // value or leaving them as the default if necessary.
2425 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2426 // Check if a value is specified for this temp-arg.
2427 if (i < TemplateVals.size()) {
2428 // Set it now.
2429 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2430 TemplateVals[i]))
2431 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002432
David Greenedb445972011-10-05 22:42:07 +00002433 // Resolve it next.
2434 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2435
2436 if (DeleteArgs)
2437 // Now remove it.
2438 CurRec->removeValue(TArgs[i]);
Craig Toppera9642b42015-05-04 01:35:39 +00002439
David Greenedb445972011-10-05 22:42:07 +00002440 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Craig Topper85c07002015-04-30 05:54:22 +00002441 return Error(SubClassLoc, "value not specified for template argument #" +
2442 utostr(i) + " (" + TArgs[i]->getAsUnquotedString() +
2443 ") of multiclassclass '" + MC.Rec.getNameInitAsString() +
2444 "'");
David Greenedb445972011-10-05 22:42:07 +00002445 }
2446 }
2447 return false;
2448}
2449
2450bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2451 Record *CurRec,
2452 Record *DefProto,
2453 SMLoc DefmPrefixLoc) {
2454 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002455 if (ApplyLetStack(CurRec))
2456 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002457
David Greenedb445972011-10-05 22:42:07 +00002458 // Don't create a top level definition for defm inside multiclasses,
2459 // instead, only update the prototypes and bind the template args
2460 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002461 if (!CurMultiClass)
2462 return false;
Craig Toppereb4d7c62015-04-29 04:43:36 +00002463 for (const auto &Proto : CurMultiClass->DefPrototypes)
2464 if (Proto->getNameInit() == CurRec->getNameInit())
Sean Silvacc951b22013-01-09 05:28:12 +00002465 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2466 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002467 CurMultiClass->DefPrototypes.push_back(std::unique_ptr<Record>(CurRec));
David Greenedb445972011-10-05 22:42:07 +00002468
Sean Silvacc951b22013-01-09 05:28:12 +00002469 // Copy the template arguments for the multiclass into the new def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002470 for (Init * TA : CurMultiClass->Rec.getTemplateArgs()) {
2471 const RecordVal *RV = CurMultiClass->Rec.getValue(TA);
Sean Silvacc951b22013-01-09 05:28:12 +00002472 assert(RV && "Template arg doesn't exist?");
2473 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002474 }
2475
2476 return false;
2477}
2478
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002479/// ParseDefm - Parse the instantiation of a multiclass.
2480///
2481/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2482///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002483bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002484 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002485 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002486 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002487
Craig Topperb21afc62013-01-07 05:09:33 +00002488 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002489 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002490 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002491
Jordan Rosef12e8a92013-01-10 18:50:11 +00002492 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002493 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002494 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002495
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002496 // Keep track of the new generated record definitions.
2497 std::vector<Record*> NewRecDefs;
2498
2499 // This record also inherits from a regular class (non-multiclass)?
2500 bool InheritFromClass = false;
2501
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002502 // eat the colon.
2503 Lex.Lex();
2504
Chris Lattner526c8cb2009-06-21 03:39:35 +00002505 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002506 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002507
2508 while (1) {
Craig Topper011817a2014-04-09 04:50:04 +00002509 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002510
2511 // To instantiate a multiclass, we need to first get the multiclass, then
2512 // instantiate each def contained in the multiclass with the SubClassRef
2513 // template parameters.
Craig Topper7adf2bf2014-12-11 05:25:30 +00002514 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper4ca40012014-11-30 01:20:17 +00002515 assert(MC && "Didn't lookup multiclass correctly?");
David Greeneaf8ee2c2011-07-29 22:43:06 +00002516 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002517
2518 // Verify that the correct number of template arguments were specified.
David Greenedb10e692011-10-19 13:02:42 +00002519 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002520 if (TArgs.size() < TemplateVals.size())
2521 return Error(SubClassLoc,
2522 "more template args specified than multiclass expects");
2523
2524 // Loop over all the def's in the multiclass, instantiating each one.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002525 for (const std::unique_ptr<Record> &DefProto : MC->DefPrototypes) {
2526 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto.get(), DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002527 SMRange(DefmLoc,
2528 DefmPrefixEndLoc));
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002529 if (!CurRec)
2530 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002531
Jordan Rosef12e8a92013-01-10 18:50:11 +00002532 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002533 TArgs, TemplateVals, true/*Delete args*/))
2534 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002535
Craig Toppereb4d7c62015-04-29 04:43:36 +00002536 if (ResolveMulticlassDef(*MC, CurRec, DefProto.get(), DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002537 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002538
Adam Nemete5a07162014-09-16 17:14:13 +00002539 // Defs that can be used by other definitions should be fully resolved
2540 // before any use.
2541 if (DefProto->isResolveFirst() && !CurMultiClass) {
2542 CurRec->resolveReferences();
2543 CurRec->setResolveFirst(false);
2544 }
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002545 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002546 }
2547
David Greenedb445972011-10-05 22:42:07 +00002548
David Greenef00919a2009-04-22 22:17:51 +00002549 if (Lex.getCode() != tgtok::comma) break;
2550 Lex.Lex(); // eat ','.
2551
Craig Topper998a39a2013-08-20 04:22:09 +00002552 if (Lex.getCode() != tgtok::Id)
2553 return TokError("expected identifier");
2554
David Greenef00919a2009-04-22 22:17:51 +00002555 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002556
2557 // A defm can inherit from regular classes (non-multiclass) as
2558 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002559 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002560
2561 if (InheritFromClass)
2562 break;
2563
Craig Topper011817a2014-04-09 04:50:04 +00002564 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002565 }
2566
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002567 if (InheritFromClass) {
2568 // Process all the classes to inherit as if they were part of a
2569 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002570 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002571 while (1) {
2572 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002573 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002574
2575 // Get the expanded definition prototypes and teach them about
2576 // the record values the current class to inherit has
Craig Toppereb4d7c62015-04-29 04:43:36 +00002577 for (Record *CurRec : NewRecDefs) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002578 // Add it.
2579 if (AddSubClass(CurRec, SubClass))
2580 return true;
2581
Sean Silvacb1a75e2013-01-09 04:49:14 +00002582 if (ApplyLetStack(CurRec))
2583 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002584 }
2585
2586 if (Lex.getCode() != tgtok::comma) break;
2587 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002588 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002589 }
2590 }
2591
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002592 if (!CurMultiClass)
Craig Toppereb4d7c62015-04-29 04:43:36 +00002593 for (Record *CurRec : NewRecDefs)
David Greene50c09122011-08-10 18:27:46 +00002594 // See Record::setName(). This resolve step will see any new
2595 // name for the def that might have been created when resolving
2596 // inheritance, values and arguments above.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002597 CurRec->resolveReferences();
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002598
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002599 if (Lex.getCode() != tgtok::semi)
2600 return TokError("expected ';' at end of defm");
2601 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002602
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002603 return false;
2604}
2605
2606/// ParseObject
2607/// Object ::= ClassInst
2608/// Object ::= DefInst
2609/// Object ::= MultiClassInst
2610/// Object ::= DefMInst
2611/// Object ::= LETCommand '{' ObjectList '}'
2612/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002613bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002614 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002615 default:
2616 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002617 case tgtok::Let: return ParseTopLevelLet(MC);
2618 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002619 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002620 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002621 case tgtok::Class: return ParseClass();
2622 case tgtok::MultiClass: return ParseMultiClass();
2623 }
2624}
2625
2626/// ParseObjectList
2627/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002628bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002629 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002630 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002631 return true;
2632 }
2633 return false;
2634}
2635
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002636bool TGParser::ParseFile() {
2637 Lex.Lex(); // Prime the lexer.
2638 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002639
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002640 // If we have unread input at the end of the file, report it.
2641 if (Lex.getCode() == tgtok::Eof)
2642 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002643
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002644 return TokError("Unexpected input at top level");
2645}
2646