blob: e5f6f165d13f707d0b7eb86c991c571ea5f7c115 [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])
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000121 return Error(Loc, "Cannot set bit #" + Twine(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.
Craig Topper8eb764c2015-06-02 06:19:28 +0000151 for (const RecordVal &Val : SC->getValues())
152 if (AddValue(CurRec, SubClass.RefRange.Start, Val))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000153 return true;
154
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000155 ArrayRef<Init *> TArgs = SC->getTemplateArgs();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000156
157 // Ensure that an appropriate number of template arguments are specified.
158 if (TArgs.size() < SubClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000159 return Error(SubClass.RefRange.Start,
160 "More template args specified than expected");
Bob Wilson7248f862009-11-22 04:24:42 +0000161
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000162 // Loop over all of the template arguments, setting them to the specified
163 // value or leaving them as the default if necessary.
164 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
165 if (i < SubClass.TemplateArgs.size()) {
166 // If a value is specified for this template arg, set it now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000167 if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
168 std::vector<unsigned>(), SubClass.TemplateArgs[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000169 return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000170
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000171 // Resolve it next.
172 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson7248f862009-11-22 04:24:42 +0000173
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000174 // Now remove it.
175 CurRec->removeValue(TArgs[i]);
176
177 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000178 return Error(SubClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000179 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000180 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000181 ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000182 }
183 }
184
185 // Since everything went well, we can now set the "superclass" list for the
186 // current record.
Craig Topperd26d2d92015-07-06 06:23:01 +0000187 ArrayRef<Record *> SCs = SC->getSuperClasses();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000188 ArrayRef<SMRange> SCRanges = SC->getSuperClassRanges();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000189 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
190 if (CurRec->isSubClassOf(SCs[i]))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000191 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000192 "Already subclass of '" + SCs[i]->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000193 CurRec->addSuperClass(SCs[i], SCRanges[i]);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000194 }
Bob Wilson7248f862009-11-22 04:24:42 +0000195
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000196 if (CurRec->isSubClassOf(SC))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000197 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000198 "Already subclass of '" + SC->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000199 CurRec->addSuperClass(SC, SubClass.RefRange);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000200 return false;
201}
202
David Greene753ed8f2009-04-22 16:42:54 +0000203/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilsonf71e6562009-04-30 18:26:19 +0000204/// CurMC, resolving its template args as SubMultiClass's
David Greene753ed8f2009-04-22 16:42:54 +0000205/// template arguments.
Bob Wilsonf71e6562009-04-30 18:26:19 +0000206bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson92ab8202009-04-30 17:46:20 +0000207 SubMultiClassReference &SubMultiClass) {
David Greene753ed8f2009-04-22 16:42:54 +0000208 MultiClass *SMC = SubMultiClass.MC;
Bob Wilsonf71e6562009-04-30 18:26:19 +0000209 Record *CurRec = &CurMC->Rec;
David Greene753ed8f2009-04-22 16:42:54 +0000210
David Greene753ed8f2009-04-22 16:42:54 +0000211 // Add all of the values in the subclass into the current class.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000212 for (const auto &SMCVal : SMC->Rec.getValues())
213 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000214 return true;
215
Craig Toppera4ea4b02014-11-30 00:24:32 +0000216 unsigned newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000217
David Greene753ed8f2009-04-22 16:42:54 +0000218 // Add all of the defs in the subclass into the current multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000219 for (const std::unique_ptr<Record> &R : SMC->DefPrototypes) {
David Greene753ed8f2009-04-22 16:42:54 +0000220 // Clone the def and add it to the current multiclass
Craig Toppereb4d7c62015-04-29 04:43:36 +0000221 auto NewDef = make_unique<Record>(*R);
David Greene753ed8f2009-04-22 16:42:54 +0000222
223 // Add all of the values in the superclass into the current def.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000224 for (const auto &MCVal : CurRec->getValues())
225 if (AddValue(NewDef.get(), SubMultiClass.RefRange.Start, MCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000226 return true;
227
Craig Topperc3504c42014-12-11 05:25:33 +0000228 CurMC->DefPrototypes.push_back(std::move(NewDef));
David Greene753ed8f2009-04-22 16:42:54 +0000229 }
Bob Wilson3d948162009-04-28 19:41:44 +0000230
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000231 ArrayRef<Init *> SMCTArgs = SMC->Rec.getTemplateArgs();
David Greene753ed8f2009-04-22 16:42:54 +0000232
David Greene7049e792009-04-24 16:55:41 +0000233 // Ensure that an appropriate number of template arguments are
234 // specified.
David Greene753ed8f2009-04-22 16:42:54 +0000235 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000236 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000237 "More template args specified than expected");
Bob Wilson3d948162009-04-28 19:41:44 +0000238
David Greene753ed8f2009-04-22 16:42:54 +0000239 // Loop over all of the template arguments, setting them to the specified
240 // value or leaving them as the default if necessary.
241 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
242 if (i < SubMultiClass.TemplateArgs.size()) {
David Greene7049e792009-04-24 16:55:41 +0000243 // If a value is specified for this template arg, set it in the
244 // superclass now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000245 if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson3d948162009-04-28 19:41:44 +0000246 std::vector<unsigned>(),
David Greene753ed8f2009-04-22 16:42:54 +0000247 SubMultiClass.TemplateArgs[i]))
248 return true;
249
250 // Resolve it next.
251 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson3d948162009-04-28 19:41:44 +0000252
David Greene753ed8f2009-04-22 16:42:54 +0000253 // Now remove it.
254 CurRec->removeValue(SMCTArgs[i]);
255
David Greene7049e792009-04-24 16:55:41 +0000256 // If a value is specified for this template arg, set it in the
257 // new defs now.
Craig Topperc3504c42014-12-11 05:25:33 +0000258 for (const auto &Def :
259 makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
260 if (SetValue(Def.get(), SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson3d948162009-04-28 19:41:44 +0000261 std::vector<unsigned>(),
David Greene753ed8f2009-04-22 16:42:54 +0000262 SubMultiClass.TemplateArgs[i]))
263 return true;
264
265 // Resolve it next.
266 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
267
268 // Now remove it
269 Def->removeValue(SMCTArgs[i]);
270 }
271 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000272 return Error(SubMultiClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000273 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000274 Twine(i) + " (" + SMCTArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000275 ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000276 }
277 }
278
279 return false;
280}
281
David Greenefb927af2012-02-22 16:09:41 +0000282/// ProcessForeachDefs - Given a record, apply all of the variable
283/// values in all surrounding foreach loops, creating new records for
284/// each combination of values.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000285bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
286 if (Loops.empty())
287 return false;
288
David Greenefb927af2012-02-22 16:09:41 +0000289 // We want to instantiate a new copy of CurRec for each combination
290 // of nested loop iterator values. We don't want top instantiate
291 // any copies until we have values for each loop iterator.
292 IterSet IterVals;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000293 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000294}
295
296/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
297/// apply each of the variable values in this loop and then process
298/// subloops.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000299bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
300 // Recursively build a tuple of iterator values.
301 if (IterVals.size() != Loops.size()) {
302 assert(IterVals.size() < Loops.size());
303 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silvafb509ed2012-10-10 20:24:43 +0000304 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Craig Topper011817a2014-04-09 04:50:04 +0000305 if (!List) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000306 Error(Loc, "Loop list is not a list");
307 return true;
308 }
David Greenefb927af2012-02-22 16:09:41 +0000309
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000310 // Process each value.
Craig Topper664f6a02015-06-02 04:15:57 +0000311 for (unsigned i = 0; i < List->size(); ++i) {
Craig Topper011817a2014-04-09 04:50:04 +0000312 Init *ItemVal = List->resolveListElementReference(*CurRec, nullptr, i);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000313 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
314 if (ProcessForeachDefs(CurRec, Loc, IterVals))
315 return true;
316 IterVals.pop_back();
317 }
318 return false;
319 }
320
321 // This is the bottom of the recursion. We have all of the iterator values
322 // for this point in the iteration space. Instantiate a new record to
323 // reflect this combination of values.
Craig Topper84138712014-11-29 05:31:10 +0000324 auto IterRec = make_unique<Record>(*CurRec);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000325
326 // Set the iterator values now.
Craig Topper8eb764c2015-06-02 06:19:28 +0000327 for (IterRecord &IR : IterVals) {
328 VarInit *IterVar = IR.IterVar;
329 TypedInit *IVal = dyn_cast<TypedInit>(IR.IterValue);
Craig Topper84138712014-11-29 05:31:10 +0000330 if (!IVal)
331 return Error(Loc, "foreach iterator value is untyped");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000332
333 IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false));
334
Craig Topper84138712014-11-29 05:31:10 +0000335 if (SetValue(IterRec.get(), Loc, IterVar->getName(),
336 std::vector<unsigned>(), IVal))
337 return Error(Loc, "when instantiating this def");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000338
339 // Resolve it next.
340 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName()));
341
342 // Remove it.
343 IterRec->removeValue(IterVar->getName());
344 }
345
346 if (Records.getDef(IterRec->getNameInitAsString())) {
Artyom Skrobov8b985322014-06-10 12:41:14 +0000347 // If this record is anonymous, it's no problem, just generate a new name
Craig Topper84138712014-11-29 05:31:10 +0000348 if (!IterRec->isAnonymous())
349 return Error(Loc, "def already exists: " +IterRec->getNameInitAsString());
350
351 IterRec->setName(GetNewAnonymousName());
David Greenefb927af2012-02-22 16:09:41 +0000352 }
353
Craig Topper84138712014-11-29 05:31:10 +0000354 Record *IterRecSave = IterRec.get(); // Keep a copy before release.
Craig Toppercdab2322014-11-29 05:52:51 +0000355 Records.addDef(std::move(IterRec));
Craig Topper84138712014-11-29 05:31:10 +0000356 IterRecSave->resolveReferences();
David Greenefb927af2012-02-22 16:09:41 +0000357 return false;
358}
359
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000360//===----------------------------------------------------------------------===//
361// Parser Code
362//===----------------------------------------------------------------------===//
363
364/// isObjectStart - Return true if this is a valid first token for an Object.
365static bool isObjectStart(tgtok::TokKind K) {
366 return K == tgtok::Class || K == tgtok::Def ||
David Greenefb927af2012-02-22 16:09:41 +0000367 K == tgtok::Defm || K == tgtok::Let ||
368 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000369}
370
Alp Tokerce91fe52013-12-21 18:51:00 +0000371/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
372/// an identifier.
373std::string TGParser::GetNewAnonymousName() {
Aaron Ballman97a59fb2015-02-16 19:33:36 +0000374 return "anonymous_" + utostr(AnonCounter++);
Chris Lattner7538ed82010-10-05 22:51:56 +0000375}
376
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000377/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000378/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000379/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000380/// ObjectName ::= /*empty*/
381///
David Greene2affd672011-10-19 13:04:29 +0000382Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
383 switch (Lex.getCode()) {
384 case tgtok::colon:
385 case tgtok::semi:
386 case tgtok::l_brace:
387 // These are all of the tokens that can begin an object body.
388 // Some of these can also begin values but we disallow those cases
389 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000390 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000391 default:
392 break;
393 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000394
Craig Topper011817a2014-04-09 04:50:04 +0000395 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000396 if (CurMultiClass)
397 CurRec = &CurMultiClass->Rec;
398
Craig Topper011817a2014-04-09 04:50:04 +0000399 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000400 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000401 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000402 if (!CurRecName) {
403 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000404 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000405 }
406 Type = CurRecName->getType();
407 }
408
409 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000410}
411
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000412/// ParseClassID - Parse and resolve a reference to a class name. This returns
413/// null on error.
414///
415/// ClassID ::= ID
416///
417Record *TGParser::ParseClassID() {
418 if (Lex.getCode() != tgtok::Id) {
419 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000420 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000421 }
Bob Wilson7248f862009-11-22 04:24:42 +0000422
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000423 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000424 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000425 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000426
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000427 Lex.Lex();
428 return Result;
429}
430
Bob Wilson3d948162009-04-28 19:41:44 +0000431/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
432/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000433///
434/// MultiClassID ::= ID
435///
436MultiClass *TGParser::ParseMultiClassID() {
437 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000438 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000439 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000440 }
Bob Wilson3d948162009-04-28 19:41:44 +0000441
Craig Topper7adf2bf2014-12-11 05:25:30 +0000442 MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
Craig Topper4ca40012014-11-30 01:20:17 +0000443 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000444 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000445
David Greene753ed8f2009-04-22 16:42:54 +0000446 Lex.Lex();
Craig Topper4ca40012014-11-30 01:20:17 +0000447 return Result;
David Greene753ed8f2009-04-22 16:42:54 +0000448}
449
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000450/// ParseSubClassReference - Parse a reference to a subclass or to a templated
451/// subclass. This returns a SubClassRefTy with a null Record* on error.
452///
453/// SubClassRef ::= ClassID
454/// SubClassRef ::= ClassID '<' ValueList '>'
455///
456SubClassReference TGParser::
457ParseSubClassReference(Record *CurRec, bool isDefm) {
458 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000459 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000460
Sean Silva0657b402013-01-09 02:17:14 +0000461 if (isDefm) {
462 if (MultiClass *MC = ParseMultiClassID())
463 Result.Rec = &MC->Rec;
464 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000465 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000466 }
Craig Topper011817a2014-04-09 04:50:04 +0000467 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000468
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000469 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000470 if (Lex.getCode() != tgtok::less) {
471 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000472 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000473 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000474 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000475
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000476 if (Lex.getCode() == tgtok::greater) {
477 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000478 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000479 return Result;
480 }
Bob Wilson7248f862009-11-22 04:24:42 +0000481
David Greene8618f952009-06-08 20:23:18 +0000482 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000483 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000484 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000485 return Result;
486 }
Bob Wilson7248f862009-11-22 04:24:42 +0000487
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000488 if (Lex.getCode() != tgtok::greater) {
489 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000490 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000491 return Result;
492 }
493 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000494 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000495
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000496 return Result;
497}
498
Bob Wilson3d948162009-04-28 19:41:44 +0000499/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
500/// templated submulticlass. This returns a SubMultiClassRefTy with a null
501/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000502///
503/// SubMultiClassRef ::= MultiClassID
504/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
505///
506SubMultiClassReference TGParser::
507ParseSubMultiClassReference(MultiClass *CurMC) {
508 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000509 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000510
David Greene753ed8f2009-04-22 16:42:54 +0000511 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000512 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000513
David Greene753ed8f2009-04-22 16:42:54 +0000514 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000515 if (Lex.getCode() != tgtok::less) {
516 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000517 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000518 }
David Greene753ed8f2009-04-22 16:42:54 +0000519 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000520
David Greene753ed8f2009-04-22 16:42:54 +0000521 if (Lex.getCode() == tgtok::greater) {
522 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000523 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000524 return Result;
525 }
Bob Wilson3d948162009-04-28 19:41:44 +0000526
David Greene8618f952009-06-08 20:23:18 +0000527 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000528 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000529 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000530 return Result;
531 }
Bob Wilson3d948162009-04-28 19:41:44 +0000532
David Greene753ed8f2009-04-22 16:42:54 +0000533 if (Lex.getCode() != tgtok::greater) {
534 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000535 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000536 return Result;
537 }
538 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000539 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000540
541 return Result;
542}
543
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000544/// ParseRangePiece - Parse a bit/value range.
545/// RangePiece ::= INTVAL
546/// RangePiece ::= INTVAL '-' INTVAL
547/// RangePiece ::= INTVAL INTVAL
548bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000549 if (Lex.getCode() != tgtok::IntVal) {
550 TokError("expected integer or bitrange");
551 return true;
552 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000553 int64_t Start = Lex.getCurIntVal();
554 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000555
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000556 if (Start < 0)
557 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000558
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000559 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000560 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000561 Ranges.push_back(Start);
562 return false;
563 case tgtok::minus:
564 if (Lex.Lex() != tgtok::IntVal) {
565 TokError("expected integer value as end of range");
566 return true;
567 }
568 End = Lex.getCurIntVal();
569 break;
570 case tgtok::IntVal:
571 End = -Lex.getCurIntVal();
572 break;
573 }
Bob Wilson7248f862009-11-22 04:24:42 +0000574 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000575 return TokError("invalid range, cannot be negative");
576 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000577
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000578 // Add to the range.
Craig Toppera9642b42015-05-04 01:35:39 +0000579 if (Start < End)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000580 for (; Start <= End; ++Start)
581 Ranges.push_back(Start);
Craig Toppera9642b42015-05-04 01:35:39 +0000582 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000583 for (; Start >= End; --Start)
584 Ranges.push_back(Start);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000585 return false;
586}
587
588/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
589///
590/// RangeList ::= RangePiece (',' RangePiece)*
591///
592std::vector<unsigned> TGParser::ParseRangeList() {
593 std::vector<unsigned> Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000594
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000595 // Parse the first piece.
596 if (ParseRangePiece(Result))
597 return std::vector<unsigned>();
598 while (Lex.getCode() == tgtok::comma) {
599 Lex.Lex(); // Eat the comma.
600
601 // Parse the next range piece.
602 if (ParseRangePiece(Result))
603 return std::vector<unsigned>();
604 }
605 return Result;
606}
607
608/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
609/// OptionalRangeList ::= '<' RangeList '>'
610/// OptionalRangeList ::= /*empty*/
611bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
612 if (Lex.getCode() != tgtok::less)
613 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000614
Chris Lattner526c8cb2009-06-21 03:39:35 +0000615 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000616 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000617
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000618 // Parse the range list.
619 Ranges = ParseRangeList();
620 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000621
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000622 if (Lex.getCode() != tgtok::greater) {
623 TokError("expected '>' at end of range list");
624 return Error(StartLoc, "to match this '<'");
625 }
626 Lex.Lex(); // eat the '>'.
627 return false;
628}
629
630/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
631/// OptionalBitList ::= '{' RangeList '}'
632/// OptionalBitList ::= /*empty*/
633bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
634 if (Lex.getCode() != tgtok::l_brace)
635 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000636
Chris Lattner526c8cb2009-06-21 03:39:35 +0000637 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000638 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000639
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000640 // Parse the range list.
641 Ranges = ParseRangeList();
642 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000643
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000644 if (Lex.getCode() != tgtok::r_brace) {
645 TokError("expected '}' at end of bit list");
646 return Error(StartLoc, "to match this '{'");
647 }
648 Lex.Lex(); // eat the '}'.
649 return false;
650}
651
652
653/// ParseType - Parse and return a tblgen type. This returns null on error.
654///
655/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000656/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000657/// Type ::= BIT // bit type
658/// Type ::= BITS '<' INTVAL '>' // bits<x> type
659/// Type ::= INT // int type
660/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000661/// Type ::= DAG // dag type
662/// Type ::= ClassID // Record Type
663///
664RecTy *TGParser::ParseType() {
665 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000666 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000667 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000668 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000669 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
670 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000671 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000672 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000673 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000674 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000675 case tgtok::Bits: {
676 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
677 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000678 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000679 }
680 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
681 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000682 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000683 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000684 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000685 if (Lex.Lex() != tgtok::greater) { // Eat count.
686 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000687 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000688 }
689 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000690 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000691 }
692 case tgtok::List: {
693 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
694 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000695 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000696 }
697 Lex.Lex(); // Eat '<'
698 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000699 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000700
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000701 if (Lex.getCode() != tgtok::greater) {
702 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000703 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000704 }
705 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000706 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000707 }
Bob Wilson7248f862009-11-22 04:24:42 +0000708 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000709}
710
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000711/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
712/// has already been read.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000713Init *TGParser::ParseIDValue(Record *CurRec,
David Greened4263a62011-10-19 13:04:20 +0000714 const std::string &Name, SMLoc NameLoc,
715 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000716 if (CurRec) {
717 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000718 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000719
David Greenedb10e692011-10-19 13:02:42 +0000720 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
721
David Greene47a665e2011-10-05 22:42:54 +0000722 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +0000723 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
724 "::");
David Greene47a665e2011-10-05 22:42:54 +0000725
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000726 if (CurRec->isTemplateArg(TemplateArgName)) {
727 const RecordVal *RV = CurRec->getValue(TemplateArgName);
728 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000729 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000730 }
731 }
Bob Wilson7248f862009-11-22 04:24:42 +0000732
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000733 if (CurMultiClass) {
David Greenedb10e692011-10-19 13:02:42 +0000734 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
735 "::");
736
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000737 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
738 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
739 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000740 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000741 }
742 }
Bob Wilson7248f862009-11-22 04:24:42 +0000743
David Greenefb927af2012-02-22 16:09:41 +0000744 // If this is in a foreach loop, make sure it's not a loop iterator
Craig Toppereb4d7c62015-04-29 04:43:36 +0000745 for (const auto &L : Loops) {
746 VarInit *IterVar = dyn_cast<VarInit>(L.IterVar);
David Greenefb927af2012-02-22 16:09:41 +0000747 if (IterVar && IterVar->getName() == Name)
748 return IterVar;
749 }
750
David Greene232bd602011-10-19 13:04:21 +0000751 if (Mode == ParseNameMode)
752 return StringInit::get(Name);
753
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000754 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000755 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000756
David Greene232bd602011-10-19 13:04:21 +0000757 if (Mode == ParseValueMode) {
758 Error(NameLoc, "Variable not defined: '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000759 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000760 }
Craig Toppera9642b42015-05-04 01:35:39 +0000761
David Greene232bd602011-10-19 13:04:21 +0000762 return StringInit::get(Name);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000763}
764
David Greene5d0c0512009-05-14 20:54:48 +0000765/// ParseOperation - Parse an operator. This returns null on error.
766///
767/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
768///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000769Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000770 switch (Lex.getCode()) {
771 default:
772 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000773 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000774 case tgtok::XHead:
775 case tgtok::XTail:
776 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000777 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
778 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000779 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000780
David Greenee8f3b272009-05-14 21:22:49 +0000781 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000782 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000783 case tgtok::XCast:
784 Lex.Lex(); // eat the operation
785 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000786
David Greenee8f3b272009-05-14 21:22:49 +0000787 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000788
Craig Topper011817a2014-04-09 04:50:04 +0000789 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000790 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000791 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000792 }
David Greene5d0c0512009-05-14 20:54:48 +0000793
David Greenee8f3b272009-05-14 21:22:49 +0000794 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000795 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000796 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000797 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000798 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000799 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000800 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000801 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000802 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000803 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000804 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000805 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000806 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000807 break;
David Greenee8f3b272009-05-14 21:22:49 +0000808 }
809 if (Lex.getCode() != tgtok::l_paren) {
810 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000811 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000812 }
813 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000814
David Greeneaf8ee2c2011-07-29 22:43:06 +0000815 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000816 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000817
Craig Topper85c07002015-04-30 05:54:22 +0000818 if (Code == UnOpInit::HEAD ||
819 Code == UnOpInit::TAIL ||
820 Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000821 ListInit *LHSl = dyn_cast<ListInit>(LHS);
822 StringInit *LHSs = dyn_cast<StringInit>(LHS);
823 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000824 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000825 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000826 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000827 }
828 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000829 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
830 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000831 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000832 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000833 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000834 }
835 }
836
Craig Topper85c07002015-04-30 05:54:22 +0000837 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
Craig Topper011817a2014-04-09 04:50:04 +0000838 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000839 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000840 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000841 }
Bob Wilson7248f862009-11-22 04:24:42 +0000842
Craig Topperec9072d2015-05-14 05:53:53 +0000843 if (LHSl && LHSl->empty()) {
David Greened571b3c2009-05-14 22:38:31 +0000844 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000845 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000846 }
847 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000848 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000849 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000850 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000851 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000852 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000853 }
Craig Toppera9642b42015-05-04 01:35:39 +0000854 Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
855 : ListRecTy::get(Itemt->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000856 } else {
David Greened571b3c2009-05-14 22:38:31 +0000857 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000858 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000859 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000860 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000861 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000862 }
Craig Toppera9642b42015-05-04 01:35:39 +0000863 Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
David Greened571b3c2009-05-14 22:38:31 +0000864 }
865 }
866 }
867
David Greenee8f3b272009-05-14 21:22:49 +0000868 if (Lex.getCode() != tgtok::r_paren) {
869 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000870 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000871 }
872 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000873 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000874 }
David Greene5d0c0512009-05-14 20:54:48 +0000875
876 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000877 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000878 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +0000879 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000880 case tgtok::XSRL:
881 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000882 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000883 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000884 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000885 tgtok::TokKind OpTok = Lex.getCode();
886 SMLoc OpLoc = Lex.getLoc();
887 Lex.Lex(); // eat the operation
888
David Greene5d0c0512009-05-14 20:54:48 +0000889 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000890 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000891
Chris Lattner61ea00b2010-10-05 23:58:18 +0000892 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000893 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000894 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000895 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000896 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000897 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
898 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
899 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
900 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000901 case tgtok::XListConcat:
902 Code = BinOpInit::LISTCONCAT;
903 // We don't know the list type until we parse the first argument
904 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000905 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000906 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000907 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000908 break;
David Greene5d0c0512009-05-14 20:54:48 +0000909 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000910
David Greene5d0c0512009-05-14 20:54:48 +0000911 if (Lex.getCode() != tgtok::l_paren) {
912 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000913 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000914 }
915 Lex.Lex(); // eat the '('
916
David Greeneaf8ee2c2011-07-29 22:43:06 +0000917 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000918
Chris Lattner61ea00b2010-10-05 23:58:18 +0000919 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000920 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000921
Chris Lattner61ea00b2010-10-05 23:58:18 +0000922 while (Lex.getCode() == tgtok::comma) {
923 Lex.Lex(); // eat the ','
924
925 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000926 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000927 }
David Greene5d0c0512009-05-14 20:54:48 +0000928
929 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000930 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000931 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000932 }
933 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000934
Daniel Sanders314e80e2014-05-07 10:13:19 +0000935 // If we are doing !listconcat, we should know the type by now
936 if (OpTok == tgtok::XListConcat) {
937 if (VarInit *Arg0 = dyn_cast<VarInit>(InitList[0]))
938 Type = Arg0->getType();
939 else if (ListInit *Arg0 = dyn_cast<ListInit>(InitList[0]))
940 Type = Arg0->getType();
941 else {
942 InitList[0]->dump();
943 Error(OpLoc, "expected a list");
944 return nullptr;
945 }
946 }
947
Chris Lattner61ea00b2010-10-05 23:58:18 +0000948 // We allow multiple operands to associative operators like !strconcat as
949 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000950 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000951 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000952 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000953 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
954 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000955 InitList.back() = RHS;
956 }
957 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000958
Chris Lattner61ea00b2010-10-05 23:58:18 +0000959 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000960 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000961 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000962
Chris Lattner61ea00b2010-10-05 23:58:18 +0000963 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +0000964 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000965 }
966
David Greene3587eed2009-05-14 23:26:46 +0000967 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +0000968 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +0000969 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
970 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000971 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000972
David Greene98ed3c72009-05-14 21:54:42 +0000973 tgtok::TokKind LexCode = Lex.getCode();
974 Lex.Lex(); // eat the operation
975 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +0000976 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +0000977 case tgtok::XIf:
978 Code = TernOpInit::IF;
979 break;
David Greenee917fff2009-05-14 22:23:47 +0000980 case tgtok::XForEach:
981 Code = TernOpInit::FOREACH;
982 break;
David Greene98ed3c72009-05-14 21:54:42 +0000983 case tgtok::XSubst:
984 Code = TernOpInit::SUBST;
985 break;
986 }
987 if (Lex.getCode() != tgtok::l_paren) {
988 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000989 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +0000990 }
991 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000992
David Greeneaf8ee2c2011-07-29 22:43:06 +0000993 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000994 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000995
David Greene98ed3c72009-05-14 21:54:42 +0000996 if (Lex.getCode() != tgtok::comma) {
997 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000998 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +0000999 }
1000 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001001
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001002 Init *MHS = ParseValue(CurRec, ItemType);
1003 if (!MHS)
1004 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001005
David Greene98ed3c72009-05-14 21:54:42 +00001006 if (Lex.getCode() != tgtok::comma) {
1007 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001008 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001009 }
1010 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001011
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001012 Init *RHS = ParseValue(CurRec, ItemType);
1013 if (!RHS)
1014 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001015
David Greene98ed3c72009-05-14 21:54:42 +00001016 if (Lex.getCode() != tgtok::r_paren) {
1017 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001018 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001019 }
1020 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001021
David Greene98ed3c72009-05-14 21:54:42 +00001022 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001023 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001024 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001025 RecTy *MHSTy = nullptr;
1026 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001027
Sean Silvafb509ed2012-10-10 20:24:43 +00001028 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001029 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001030 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001031 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001032 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001033 MHSTy = BitRecTy::get();
1034
Sean Silvafb509ed2012-10-10 20:24:43 +00001035 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001036 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001037 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001038 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001039 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001040 RHSTy = BitRecTy::get();
1041
1042 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001043 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001044 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001045 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001046 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001047
1048 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001049 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001050 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001051 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001052
1053 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1054 Type = RHSTy;
1055 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1056 Type = MHSTy;
Bob Wilson7248f862009-11-22 04:24:42 +00001057 } else {
David Greene3587eed2009-05-14 23:26:46 +00001058 TokError("inconsistent types for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001059 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001060 }
1061 break;
1062 }
David Greenee917fff2009-05-14 22:23:47 +00001063 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001064 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
Craig Topper011817a2014-04-09 04:50:04 +00001065 if (!MHSt) {
David Greenee917fff2009-05-14 22:23:47 +00001066 TokError("could not get type for !foreach");
Craig Topper011817a2014-04-09 04:50:04 +00001067 return nullptr;
David Greenee917fff2009-05-14 22:23:47 +00001068 }
1069 Type = MHSt->getType();
1070 break;
1071 }
David Greene98ed3c72009-05-14 21:54:42 +00001072 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001073 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001074 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001075 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001076 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001077 }
1078 Type = RHSt->getType();
1079 break;
1080 }
1081 }
David Greenee32ebf22011-07-29 19:07:07 +00001082 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001083 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001084 }
David Greene5d0c0512009-05-14 20:54:48 +00001085 }
David Greene5d0c0512009-05-14 20:54:48 +00001086}
1087
1088/// ParseOperatorType - Parse a type for an operator. This returns
1089/// null on error.
1090///
1091/// OperatorType ::= '<' Type '>'
1092///
Dan Gohman1432ef82009-08-12 22:10:57 +00001093RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001094 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001095
1096 if (Lex.getCode() != tgtok::less) {
1097 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001098 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001099 }
1100 Lex.Lex(); // eat the <
1101
1102 Type = ParseType();
1103
Craig Topper011817a2014-04-09 04:50:04 +00001104 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001105 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001106 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001107 }
1108
1109 if (Lex.getCode() != tgtok::greater) {
1110 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001111 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001112 }
1113 Lex.Lex(); // eat the >
1114
1115 return Type;
1116}
1117
1118
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001119/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1120///
1121/// SimpleValue ::= IDValue
1122/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001123/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001124/// SimpleValue ::= CODEFRAGMENT
1125/// SimpleValue ::= '?'
1126/// SimpleValue ::= '{' ValueList '}'
1127/// SimpleValue ::= ID '<' ValueListNE '>'
1128/// SimpleValue ::= '[' ValueList ']'
1129/// SimpleValue ::= '(' IDValue DagArgList ')'
1130/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001131/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001132/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1133/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1134/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001135/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001136/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1137///
David Greened4263a62011-10-19 13:04:20 +00001138Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1139 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001140 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001141 switch (Lex.getCode()) {
1142 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001143 case tgtok::paste:
1144 // This is a leading paste operation. This is deprecated but
1145 // still exists in some .td files. Ignore it.
1146 Lex.Lex(); // Skip '#'.
1147 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001148 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001149 case tgtok::BinaryIntVal: {
1150 auto BinaryVal = Lex.getCurBinaryIntVal();
1151 SmallVector<Init*, 16> Bits(BinaryVal.second);
1152 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001153 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001154 R = BitsInit::get(Bits);
1155 Lex.Lex();
1156 break;
1157 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001158 case tgtok::StrVal: {
1159 std::string Val = Lex.getCurStrVal();
1160 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001161
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001162 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001163 while (Lex.getCode() == tgtok::StrVal) {
1164 Val += Lex.getCurStrVal();
1165 Lex.Lex();
1166 }
Bob Wilson7248f862009-11-22 04:24:42 +00001167
David Greenee32ebf22011-07-29 19:07:07 +00001168 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001169 break;
1170 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001171 case tgtok::CodeFragment:
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +00001172 R = StringInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001173 Lex.Lex();
1174 break;
1175 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001176 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001177 Lex.Lex();
1178 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001179 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001180 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001181 std::string Name = Lex.getCurStrVal();
1182 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001183 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001184
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001185 // Value ::= ID '<' ValueListNE '>'
1186 if (Lex.Lex() == tgtok::greater) {
1187 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001188 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001189 }
David Greene8618f952009-06-08 20:23:18 +00001190
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001191 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1192 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1193 // body.
1194 Record *Class = Records.getClass(Name);
1195 if (!Class) {
1196 Error(NameLoc, "Expected a class name, got '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001197 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001198 }
David Greene8618f952009-06-08 20:23:18 +00001199
David Greeneaf8ee2c2011-07-29 22:43:06 +00001200 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
Craig Topper011817a2014-04-09 04:50:04 +00001201 if (ValueList.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001202
David Greene8618f952009-06-08 20:23:18 +00001203 if (Lex.getCode() != tgtok::greater) {
1204 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001205 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001206 }
1207 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001208 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001209
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001210 // Create the new record, set it as CurRec temporarily.
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00001211 auto NewRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), NameLoc,
1212 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001213 Record *NewRec = NewRecOwner.get(); // Keep a copy since we may release.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001214 SubClassReference SCRef;
Jordan Rosef12e8a92013-01-10 18:50:11 +00001215 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001216 SCRef.Rec = Class;
1217 SCRef.TemplateArgs = ValueList;
1218 // Add info about the subclass to NewRec.
Craig Topper84138712014-11-29 05:31:10 +00001219 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001220 return nullptr;
Craig Topper84138712014-11-29 05:31:10 +00001221
Hal Finkela8c1f462014-01-02 20:47:09 +00001222 if (!CurMultiClass) {
1223 NewRec->resolveReferences();
Craig Toppercdab2322014-11-29 05:52:51 +00001224 Records.addDef(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001225 } else {
Adam Nemete5a07162014-09-16 17:14:13 +00001226 // This needs to get resolved once the multiclass template arguments are
1227 // known before any use.
1228 NewRec->setResolveFirst(true);
Hal Finkela8c1f462014-01-02 20:47:09 +00001229 // Otherwise, we're inside a multiclass, add it to the multiclass.
Craig Topperc3504c42014-12-11 05:25:33 +00001230 CurMultiClass->DefPrototypes.push_back(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001231
1232 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00001233 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
1234 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Hal Finkela8c1f462014-01-02 20:47:09 +00001235 assert(RV && "Template arg doesn't exist?");
1236 NewRec->addValue(*RV);
1237 }
1238
1239 // We can't return the prototype def here, instead return:
1240 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1241 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1242 assert(MCNameRV && "multiclass record must have a NAME");
1243
1244 return UnOpInit::get(UnOpInit::CAST,
1245 BinOpInit::get(BinOpInit::STRCONCAT,
1246 VarInit::get(MCNameRV->getName(),
1247 MCNameRV->getType()),
1248 NewRec->getNameInit(),
1249 StringRecTy::get()),
1250 Class->getDefInit()->getType());
1251 }
Bob Wilson7248f862009-11-22 04:24:42 +00001252
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001253 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001254 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001255 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001256 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001257 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001258 Lex.Lex(); // eat the '{'
David Greeneaf8ee2c2011-07-29 22:43:06 +00001259 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001260
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001261 if (Lex.getCode() != tgtok::r_brace) {
1262 Vals = ParseValueList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001263 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001264 }
1265 if (Lex.getCode() != tgtok::r_brace) {
1266 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001267 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001268 }
1269 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001270
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001271 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001272
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001273 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1274 // first. We'll first read everything in to a vector, then we can reverse
1275 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001276 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001277 // FIXME: The following two loops would not be duplicated
1278 // if the API was a little more orthogonal.
1279
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001280 // bits<n> values are allowed to initialize n bits.
1281 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1282 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1283 NewBits.push_back(BI->getBit((e - i) - 1));
1284 continue;
1285 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001286 // bits<n> can also come from variable initializers.
1287 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1288 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1289 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1290 NewBits.push_back(VI->getBit((e - i) - 1));
1291 continue;
1292 }
1293 // Fallthrough to try convert this to a bit.
1294 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001295 // All other values must be convertible to just a single bit.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001296 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001297 if (!Bit) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001298 Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
Chris Lattner52416952007-11-22 21:06:59 +00001299 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001300 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001301 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001302 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001303 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001304 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001305 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001306 }
1307 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1308 Lex.Lex(); // eat the '['
David Greeneaf8ee2c2011-07-29 22:43:06 +00001309 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001310
Craig Topper011817a2014-04-09 04:50:04 +00001311 RecTy *DeducedEltTy = nullptr;
1312 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001313
Craig Topper011817a2014-04-09 04:50:04 +00001314 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001315 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001316 if (!ListType) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001317 TokError(Twine("Type mismatch for list, expected list type, got ") +
1318 ItemType->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001319 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001320 }
1321 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001322 }
David Greene8618f952009-06-08 20:23:18 +00001323
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001324 if (Lex.getCode() != tgtok::r_square) {
Craig Topper011817a2014-04-09 04:50:04 +00001325 Vals = ParseValueList(CurRec, nullptr,
1326 GivenListTy ? GivenListTy->getElementType() : nullptr);
1327 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001328 }
1329 if (Lex.getCode() != tgtok::r_square) {
1330 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001331 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001332 }
1333 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001334
Craig Topper011817a2014-04-09 04:50:04 +00001335 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001336 if (Lex.getCode() == tgtok::less) {
1337 // Optional list element type
1338 Lex.Lex(); // eat the '<'
1339
1340 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001341 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001342 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001343 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001344 }
1345
1346 if (Lex.getCode() != tgtok::greater) {
1347 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001348 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001349 }
1350 Lex.Lex(); // eat the '>'
1351 }
1352
1353 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001354 RecTy *EltTy = nullptr;
Craig Toppereb4d7c62015-04-29 04:43:36 +00001355 for (Init *V : Vals) {
1356 TypedInit *TArg = dyn_cast<TypedInit>(V);
Craig Topper011817a2014-04-09 04:50:04 +00001357 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001358 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001359 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001360 }
Craig Topper011817a2014-04-09 04:50:04 +00001361 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001362 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001363 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001364 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001365 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001366 }
Bob Wilson7248f862009-11-22 04:24:42 +00001367 } else {
David Greene8618f952009-06-08 20:23:18 +00001368 EltTy = TArg->getType();
1369 }
1370 }
1371
Craig Topper011817a2014-04-09 04:50:04 +00001372 if (GivenEltTy) {
1373 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001374 // Verify consistency
1375 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1376 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001377 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001378 }
1379 }
1380 EltTy = GivenEltTy;
1381 }
1382
Craig Topper011817a2014-04-09 04:50:04 +00001383 if (!EltTy) {
1384 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001385 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001386 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001387 }
1388 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001389 } else {
David Greene8618f952009-06-08 20:23:18 +00001390 // Make sure the deduced type is compatible with the given type
1391 if (GivenListTy) {
1392 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1393 TokError("Element type mismatch for list");
Craig Topper011817a2014-04-09 04:50:04 +00001394 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001395 }
1396 }
1397 DeducedEltTy = EltTy;
1398 }
Bob Wilson7248f862009-11-22 04:24:42 +00001399
David Greenee32ebf22011-07-29 19:07:07 +00001400 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001401 }
1402 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1403 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001404 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001405 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001406 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001407 }
Bob Wilson7248f862009-11-22 04:24:42 +00001408
David Greeneaf8ee2c2011-07-29 22:43:06 +00001409 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001410 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001411
Nate Begemandbe3f772009-03-19 05:21:56 +00001412 // If the operator name is present, parse it.
1413 std::string OperatorName;
1414 if (Lex.getCode() == tgtok::colon) {
1415 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1416 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001417 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001418 }
1419 OperatorName = Lex.getCurStrVal();
1420 Lex.Lex(); // eat the VarName.
1421 }
Bob Wilson7248f862009-11-22 04:24:42 +00001422
David Greeneaf8ee2c2011-07-29 22:43:06 +00001423 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001424 if (Lex.getCode() != tgtok::r_paren) {
1425 DagArgs = ParseDagArgList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001426 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001427 }
Bob Wilson7248f862009-11-22 04:24:42 +00001428
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001429 if (Lex.getCode() != tgtok::r_paren) {
1430 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001431 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001432 }
1433 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001434
David Greenee32ebf22011-07-29 19:07:07 +00001435 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001436 }
Bob Wilson7248f862009-11-22 04:24:42 +00001437
David Greene2f7cf7f2011-01-07 17:05:37 +00001438 case tgtok::XHead:
1439 case tgtok::XTail:
1440 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001441 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001442 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001443 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001444 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +00001445 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001446 case tgtok::XSRL:
1447 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001448 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001449 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001450 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001451 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001452 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001453 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001454 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001455 }
1456 }
Bob Wilson7248f862009-11-22 04:24:42 +00001457
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001458 return R;
1459}
1460
1461/// ParseValue - Parse a tblgen value. This returns null on error.
1462///
1463/// Value ::= SimpleValue ValueSuffix*
1464/// ValueSuffix ::= '{' BitList '}'
1465/// ValueSuffix ::= '[' BitList ']'
1466/// ValueSuffix ::= '.' ID
1467///
David Greened4263a62011-10-19 13:04:20 +00001468Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1469 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001470 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001471
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001472 // Parse the suffixes now if present.
1473 while (1) {
1474 switch (Lex.getCode()) {
1475 default: return Result;
1476 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001477 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001478 // This is the beginning of the object body.
1479 return Result;
1480
Chris Lattner526c8cb2009-06-21 03:39:35 +00001481 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001482 Lex.Lex(); // eat the '{'
1483 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001484 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001485
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001486 // Reverse the bitlist.
1487 std::reverse(Ranges.begin(), Ranges.end());
1488 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001489 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001490 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001491 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001492 }
Bob Wilson7248f862009-11-22 04:24:42 +00001493
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001494 // Eat the '}'.
1495 if (Lex.getCode() != tgtok::r_brace) {
1496 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001497 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001498 }
1499 Lex.Lex();
1500 break;
1501 }
1502 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001503 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001504 Lex.Lex(); // eat the '['
1505 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001506 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001507
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001508 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001509 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001510 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001511 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001512 }
Bob Wilson7248f862009-11-22 04:24:42 +00001513
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001514 // Eat the ']'.
1515 if (Lex.getCode() != tgtok::r_square) {
1516 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001517 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001518 }
1519 Lex.Lex();
1520 break;
1521 }
1522 case tgtok::period:
1523 if (Lex.Lex() != tgtok::Id) { // eat the .
1524 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001525 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001526 }
1527 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001528 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001529 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001530 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001531 }
David Greenee32ebf22011-07-29 19:07:07 +00001532 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001533 Lex.Lex(); // eat field name
1534 break;
David Greene8e85b482011-10-19 13:04:43 +00001535
1536 case tgtok::paste:
1537 SMLoc PasteLoc = Lex.getLoc();
1538
1539 // Create a !strconcat() operation, first casting each operand to
1540 // a string if necessary.
1541
Sean Silvafb509ed2012-10-10 20:24:43 +00001542 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001543 if (!LHS) {
1544 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001545 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001546 }
Craig Toppera9642b42015-05-04 01:35:39 +00001547
David Greene8e85b482011-10-19 13:04:43 +00001548 if (LHS->getType() != StringRecTy::get()) {
1549 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1550 }
1551
Craig Topper011817a2014-04-09 04:50:04 +00001552 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001553
1554 Lex.Lex(); // Eat the '#'.
1555 switch (Lex.getCode()) {
1556 case tgtok::colon:
1557 case tgtok::semi:
1558 case tgtok::l_brace:
1559 // These are all of the tokens that can begin an object body.
1560 // Some of these can also begin values but we disallow those cases
1561 // because they are unlikely to be useful.
Craig Toppera9642b42015-05-04 01:35:39 +00001562
David Greene8e85b482011-10-19 13:04:43 +00001563 // Trailing paste, concat with an empty string.
1564 RHS = StringInit::get("");
1565 break;
1566
1567 default:
1568 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001569 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001570 if (!RHS) {
1571 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001572 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001573 }
1574
1575 if (RHS->getType() != StringRecTy::get()) {
1576 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1577 }
Craig Toppera9642b42015-05-04 01:35:39 +00001578
David Greene8e85b482011-10-19 13:04:43 +00001579 break;
1580 }
1581
1582 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1583 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1584 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001585 }
1586 }
1587}
1588
1589/// ParseDagArgList - Parse the argument list for a dag literal expression.
1590///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001591/// DagArg ::= Value (':' VARNAME)?
1592/// DagArg ::= VARNAME
1593/// DagArgList ::= DagArg
1594/// DagArgList ::= DagArgList ',' DagArg
David Greeneaf8ee2c2011-07-29 22:43:06 +00001595std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001596TGParser::ParseDagArgList(Record *CurRec) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001597 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson7248f862009-11-22 04:24:42 +00001598
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001599 while (1) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001600 // DagArg ::= VARNAME
1601 if (Lex.getCode() == tgtok::VarName) {
1602 // A missing value is treated like '?'.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00001603 Result.emplace_back(UnsetInit::get(), Lex.getCurStrVal());
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001604 Lex.Lex();
1605 } else {
1606 // DagArg ::= Value (':' VARNAME)?
1607 Init *Val = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001608 if (!Val)
David Greeneaf8ee2c2011-07-29 22:43:06 +00001609 return std::vector<std::pair<llvm::Init*, std::string> >();
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001610
1611 // If the variable name is present, add it.
1612 std::string VarName;
1613 if (Lex.getCode() == tgtok::colon) {
1614 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1615 TokError("expected variable name in dag literal");
1616 return std::vector<std::pair<llvm::Init*, std::string> >();
1617 }
1618 VarName = Lex.getCurStrVal();
1619 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001620 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001621
1622 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001623 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001624 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001625 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001626 }
Bob Wilson7248f862009-11-22 04:24:42 +00001627
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001628 return Result;
1629}
1630
1631
1632/// ParseValueList - Parse a comma separated list of values, returning them as a
1633/// vector. Note that this always expects to be able to parse at least one
1634/// value. It returns an empty list if this is not possible.
1635///
1636/// ValueList ::= Value (',' Value)
1637///
David Greeneaf8ee2c2011-07-29 22:43:06 +00001638std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopher71520a82011-07-11 23:06:52 +00001639 RecTy *EltTy) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001640 std::vector<Init*> Result;
David Greene8618f952009-06-08 20:23:18 +00001641 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001642 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001643 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001644 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001645 if (TArgs.empty()) {
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001646 TokError("template argument provided to non-template class");
1647 return std::vector<Init*>();
1648 }
David Greene8618f952009-06-08 20:23:18 +00001649 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001650 if (!RV) {
1651 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1652 << ")\n";
1653 }
David Greene8618f952009-06-08 20:23:18 +00001654 assert(RV && "Template argument record not found??");
1655 ItemType = RV->getType();
1656 ++ArgN;
1657 }
1658 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001659 if (!Result.back()) return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001660
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001661 while (Lex.getCode() == tgtok::comma) {
1662 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001663
Craig Topper011817a2014-04-09 04:50:04 +00001664 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001665 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001666 if (ArgN >= TArgs.size()) {
1667 TokError("too many template arguments");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001668 return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001669 }
David Greene8618f952009-06-08 20:23:18 +00001670 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1671 assert(RV && "Template argument record not found??");
1672 ItemType = RV->getType();
1673 ++ArgN;
1674 }
1675 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001676 if (!Result.back()) return std::vector<Init*>();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001677 }
Bob Wilson7248f862009-11-22 04:24:42 +00001678
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001679 return Result;
1680}
1681
1682
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001683/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1684/// empty string on error. This can happen in a number of different context's,
1685/// including within a def or in the template args for a def (which which case
1686/// CurRec will be non-null) and within the template args for a multiclass (in
1687/// which case CurRec will be null, but CurMultiClass will be set). This can
1688/// also happen within a def that is within a multiclass, which will set both
1689/// CurRec and CurMultiClass.
1690///
1691/// Declaration ::= FIELD? Type ID ('=' Value)?
1692///
David Greenedb10e692011-10-19 13:02:42 +00001693Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001694 bool ParsingTemplateArgs) {
1695 // Read the field prefix if present.
1696 bool HasField = Lex.getCode() == tgtok::Field;
1697 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001698
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001699 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001700 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001701
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001702 if (Lex.getCode() != tgtok::Id) {
1703 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001704 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001705 }
Bob Wilson7248f862009-11-22 04:24:42 +00001706
Chris Lattner526c8cb2009-06-21 03:39:35 +00001707 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001708 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001709 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001710
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001711 if (ParsingTemplateArgs) {
Craig Toppera9642b42015-05-04 01:35:39 +00001712 if (CurRec)
David Greenedb10e692011-10-19 13:02:42 +00001713 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Craig Toppera9642b42015-05-04 01:35:39 +00001714 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001715 assert(CurMultiClass);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001716 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001717 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1718 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001719 }
Bob Wilson7248f862009-11-22 04:24:42 +00001720
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001721 // Add the value.
1722 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001723 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001724
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001725 // If a value is present, parse it.
1726 if (Lex.getCode() == tgtok::equal) {
1727 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001728 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001729 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001730 if (!Val ||
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001731 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001732 // Return the name, even if an error is thrown. This is so that we can
1733 // continue to make some progress, even without the value having been
1734 // initialized.
1735 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001736 }
Bob Wilson7248f862009-11-22 04:24:42 +00001737
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001738 return DeclName;
1739}
1740
David Greenefb927af2012-02-22 16:09:41 +00001741/// ParseForeachDeclaration - Read a foreach declaration, returning
1742/// the name of the declared object or a NULL Init on error. Return
1743/// the name of the parsed initializer list through ForeachListName.
1744///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001745/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1746/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1747/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001748///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001749VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001750 if (Lex.getCode() != tgtok::Id) {
1751 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001752 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001753 }
1754
1755 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1756 Lex.Lex();
1757
1758 // If a value is present, parse it.
1759 if (Lex.getCode() != tgtok::equal) {
1760 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001761 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001762 }
1763 Lex.Lex(); // Eat the '='
1764
Craig Topper011817a2014-04-09 04:50:04 +00001765 RecTy *IterType = nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001766 std::vector<unsigned> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001767
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001768 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001769 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001770 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001771 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001772 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001773 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001774 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001775 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001776 }
1777 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001778 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001779 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001780 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001781 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001782 }
1783 IterType = ListType->getElementType();
1784 break;
David Greenefb927af2012-02-22 16:09:41 +00001785 }
1786
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001787 case tgtok::IntVal: { // RangePiece.
1788 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001789 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001790 break;
David Greenefb927af2012-02-22 16:09:41 +00001791 }
1792
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001793 case tgtok::l_brace: { // '{' RangeList '}'
1794 Lex.Lex(); // eat the '{'
1795 Ranges = ParseRangeList();
1796 if (Lex.getCode() != tgtok::r_brace) {
1797 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001798 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001799 }
1800 Lex.Lex();
1801 break;
1802 }
1803 }
David Greenefb927af2012-02-22 16:09:41 +00001804
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001805 if (!Ranges.empty()) {
1806 assert(!IterType && "Type already initialized?");
1807 IterType = IntRecTy::get();
1808 std::vector<Init*> Values;
Craig Topper8eb764c2015-06-02 06:19:28 +00001809 for (unsigned R : Ranges)
1810 Values.push_back(IntInit::get(R));
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001811 ForeachListValue = ListInit::get(Values, IterType);
1812 }
1813
1814 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001815 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001816
1817 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001818}
1819
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001820/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1821/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1822/// template args for a def, which may or may not be in a multiclass. If null,
1823/// these are the template args for a multiclass.
1824///
1825/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001826///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001827bool TGParser::ParseTemplateArgList(Record *CurRec) {
1828 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1829 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001830
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001831 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001832
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001833 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001834 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001835 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001836 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001837
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001838 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001839
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001840 while (Lex.getCode() == tgtok::comma) {
1841 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001842
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001843 // Read the following declarations.
1844 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001845 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001846 return true;
1847 TheRecToAddTo->addTemplateArg(TemplArg);
1848 }
Bob Wilson7248f862009-11-22 04:24:42 +00001849
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001850 if (Lex.getCode() != tgtok::greater)
1851 return TokError("expected '>' at end of template argument list");
1852 Lex.Lex(); // eat the '>'.
1853 return false;
1854}
1855
1856
1857/// ParseBodyItem - Parse a single item at within the body of a def or class.
1858///
1859/// BodyItem ::= Declaration ';'
1860/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1861bool TGParser::ParseBodyItem(Record *CurRec) {
1862 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001863 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001864 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001865
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001866 if (Lex.getCode() != tgtok::semi)
1867 return TokError("expected ';' after declaration");
1868 Lex.Lex();
1869 return false;
1870 }
1871
1872 // LET ID OptionalRangeList '=' Value ';'
1873 if (Lex.Lex() != tgtok::Id)
1874 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001875
Chris Lattner526c8cb2009-06-21 03:39:35 +00001876 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001877 std::string FieldName = Lex.getCurStrVal();
1878 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001879
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001880 std::vector<unsigned> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001881 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001882 return true;
1883 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001884
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001885 if (Lex.getCode() != tgtok::equal)
1886 return TokError("expected '=' in let expression");
1887 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001888
David Greene8618f952009-06-08 20:23:18 +00001889 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001890 if (!Field)
David Greene8618f952009-06-08 20:23:18 +00001891 return TokError("Value '" + FieldName + "' unknown!");
1892
1893 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001894
David Greeneaf8ee2c2011-07-29 22:43:06 +00001895 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001896 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001897
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001898 if (Lex.getCode() != tgtok::semi)
1899 return TokError("expected ';' after let expression");
1900 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001901
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001902 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1903}
1904
1905/// ParseBody - Read the body of a class or def. Return true on error, false on
1906/// success.
1907///
1908/// Body ::= ';'
1909/// Body ::= '{' BodyList '}'
1910/// BodyList BodyItem*
1911///
1912bool TGParser::ParseBody(Record *CurRec) {
1913 // If this is a null definition, just eat the semi and return.
1914 if (Lex.getCode() == tgtok::semi) {
1915 Lex.Lex();
1916 return false;
1917 }
Bob Wilson7248f862009-11-22 04:24:42 +00001918
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001919 if (Lex.getCode() != tgtok::l_brace)
1920 return TokError("Expected ';' or '{' to start body");
1921 // Eat the '{'.
1922 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001923
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001924 while (Lex.getCode() != tgtok::r_brace)
1925 if (ParseBodyItem(CurRec))
1926 return true;
1927
1928 // Eat the '}'.
1929 Lex.Lex();
1930 return false;
1931}
1932
Sean Silvacb1a75e2013-01-09 04:49:14 +00001933/// \brief Apply the current let bindings to \a CurRec.
1934/// \returns true on error, false otherwise.
1935bool TGParser::ApplyLetStack(Record *CurRec) {
Craig Topper8eb764c2015-06-02 06:19:28 +00001936 for (std::vector<LetRecord> &LetInfo : LetStack)
1937 for (LetRecord &LR : LetInfo)
1938 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
Sean Silvacb1a75e2013-01-09 04:49:14 +00001939 return true;
1940 return false;
1941}
1942
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001943/// ParseObjectBody - Parse the body of a def or class. This consists of an
1944/// optional ClassList followed by a Body. CurRec is the current def or class
1945/// that is being parsed.
1946///
1947/// ObjectBody ::= BaseClassList Body
1948/// BaseClassList ::= /*empty*/
1949/// BaseClassList ::= ':' BaseClassListNE
1950/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1951///
1952bool TGParser::ParseObjectBody(Record *CurRec) {
1953 // If there is a baseclass list, read it.
1954 if (Lex.getCode() == tgtok::colon) {
1955 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001956
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001957 // Read all of the subclasses.
1958 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1959 while (1) {
1960 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00001961 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001962
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001963 // Add it.
1964 if (AddSubClass(CurRec, SubClass))
1965 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001966
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001967 if (Lex.getCode() != tgtok::comma) break;
1968 Lex.Lex(); // eat ','.
1969 SubClass = ParseSubClassReference(CurRec, false);
1970 }
1971 }
1972
Sean Silvacb1a75e2013-01-09 04:49:14 +00001973 if (ApplyLetStack(CurRec))
1974 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001975
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001976 return ParseBody(CurRec);
1977}
1978
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001979/// ParseDef - Parse and return a top level or multiclass def, return the record
1980/// corresponding to it. This returns null on error.
1981///
1982/// DefInst ::= DEF ObjectName ObjectBody
1983///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00001984bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001985 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001986 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00001987 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001988
1989 // Parse ObjectName and make a record for it.
Craig Topper84138712014-11-29 05:31:10 +00001990 std::unique_ptr<Record> CurRecOwner;
Jordan Roseabdd99b2013-01-10 18:50:05 +00001991 Init *Name = ParseObjectName(CurMultiClass);
1992 if (Name)
Craig Topper84138712014-11-29 05:31:10 +00001993 CurRecOwner = make_unique<Record>(Name, DefLoc, Records);
Jordan Roseabdd99b2013-01-10 18:50:05 +00001994 else
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00001995 CurRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), DefLoc,
1996 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001997 Record *CurRec = CurRecOwner.get(); // Keep a copy since we may release.
Bob Wilson7248f862009-11-22 04:24:42 +00001998
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00001999 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002000 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002001
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002002 // Ensure redefinition doesn't happen.
Craig Topper84138712014-11-29 05:31:10 +00002003 if (Records.getDef(CurRec->getNameInitAsString()))
2004 return Error(DefLoc, "def '" + CurRec->getNameInitAsString()+
2005 "' already defined");
Craig Toppercdab2322014-11-29 05:52:51 +00002006 Records.addDef(std::move(CurRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00002007
2008 if (ParseObjectBody(CurRec))
2009 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002010 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002011 // Parse the body before adding this prototype to the DefPrototypes vector.
2012 // That way implicit definitions will be added to the DefPrototypes vector
2013 // before this object, instantiated prior to defs derived from this object,
2014 // and this available for indirect name resolution when defs derived from
2015 // this object are instantiated.
Craig Topper84138712014-11-29 05:31:10 +00002016 if (ParseObjectBody(CurRec))
Hal Finkela8c1f462014-01-02 20:47:09 +00002017 return true;
2018
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002019 // Otherwise, a def inside a multiclass, add it to the multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002020 for (const auto &Proto : CurMultiClass->DefPrototypes)
2021 if (Proto->getNameInit() == CurRec->getNameInit())
Craig Topper84138712014-11-29 05:31:10 +00002022 return Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
2023 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002024 CurMultiClass->DefPrototypes.push_back(std::move(CurRecOwner));
Anton Yartsev671dff12014-08-08 00:29:54 +00002025 } else if (ParseObjectBody(CurRec)) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002026 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002027 }
Bob Wilson7248f862009-11-22 04:24:42 +00002028
Craig Topper011817a2014-04-09 04:50:04 +00002029 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002030 // See Record::setName(). This resolve step will see any new name
2031 // for the def that might have been created when resolving
2032 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002033 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002034
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002035 // If ObjectBody has template arguments, it's an error.
2036 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002037
2038 if (CurMultiClass) {
2039 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002040 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
2041 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002042 assert(RV && "Template arg doesn't exist?");
2043 CurRec->addValue(*RV);
2044 }
2045 }
2046
Craig Toppera9642b42015-05-04 01:35:39 +00002047 if (ProcessForeachDefs(CurRec, DefLoc))
Craig Topper84138712014-11-29 05:31:10 +00002048 return Error(DefLoc, "Could not process loops for def" +
2049 CurRec->getNameInitAsString());
David Greenefb927af2012-02-22 16:09:41 +00002050
2051 return false;
2052}
2053
2054/// ParseForeach - Parse a for statement. Return the record corresponding
2055/// to it. This returns true on error.
2056///
2057/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2058/// Foreach ::= FOREACH Declaration IN Object
2059///
2060bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2061 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2062 Lex.Lex(); // Eat the 'for' token.
2063
2064 // Make a temporary object to record items associated with the for
2065 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002066 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002067 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002068 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002069 return TokError("expected declaration in for");
2070
2071 if (Lex.getCode() != tgtok::In)
2072 return TokError("Unknown tok");
2073 Lex.Lex(); // Eat the in
2074
2075 // Create a loop object and remember it.
2076 Loops.push_back(ForeachLoop(IterName, ListValue));
2077
2078 if (Lex.getCode() != tgtok::l_brace) {
2079 // FOREACH Declaration IN Object
2080 if (ParseObject(CurMultiClass))
2081 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002082 } else {
David Greenefb927af2012-02-22 16:09:41 +00002083 SMLoc BraceLoc = Lex.getLoc();
2084 // Otherwise, this is a group foreach.
2085 Lex.Lex(); // eat the '{'.
2086
2087 // Parse the object list.
2088 if (ParseObjectList(CurMultiClass))
2089 return true;
2090
2091 if (Lex.getCode() != tgtok::r_brace) {
2092 TokError("expected '}' at end of foreach command");
2093 return Error(BraceLoc, "to match this '{'");
2094 }
2095 Lex.Lex(); // Eat the }
2096 }
2097
2098 // We've processed everything in this loop.
2099 Loops.pop_back();
2100
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002101 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002102}
2103
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002104/// ParseClass - Parse a tblgen class definition.
2105///
2106/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2107///
2108bool TGParser::ParseClass() {
2109 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2110 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002111
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002112 if (Lex.getCode() != tgtok::Id)
2113 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002114
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002115 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2116 if (CurRec) {
2117 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002118 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002119 !CurRec->getSuperClasses().empty() ||
2120 !CurRec->getTemplateArgs().empty())
Craig Topper85c07002015-04-30 05:54:22 +00002121 return TokError("Class '" + CurRec->getNameInitAsString() +
2122 "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002123 } else {
2124 // If this is the first reference to this class, create and add it.
Hans Wennborgffbbd532014-11-30 00:31:49 +00002125 auto NewRec =
2126 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
Craig Toppercdab2322014-11-29 05:52:51 +00002127 CurRec = NewRec.get();
2128 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002129 }
2130 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002131
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002132 // If there are template args, parse them.
2133 if (Lex.getCode() == tgtok::less)
2134 if (ParseTemplateArgList(CurRec))
2135 return true;
2136
2137 // Finally, parse the object body.
2138 return ParseObjectBody(CurRec);
2139}
2140
2141/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2142/// of LetRecords.
2143///
2144/// LetList ::= LetItem (',' LetItem)*
2145/// LetItem ::= ID OptionalRangeList '=' Value
2146///
2147std::vector<LetRecord> TGParser::ParseLetList() {
2148 std::vector<LetRecord> Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002149
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002150 while (1) {
2151 if (Lex.getCode() != tgtok::Id) {
2152 TokError("expected identifier in let definition");
2153 return std::vector<LetRecord>();
2154 }
2155 std::string Name = Lex.getCurStrVal();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002156 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002157 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002158
2159 // Check for an optional RangeList.
2160 std::vector<unsigned> Bits;
Bob Wilson7248f862009-11-22 04:24:42 +00002161 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002162 return std::vector<LetRecord>();
2163 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002164
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002165 if (Lex.getCode() != tgtok::equal) {
2166 TokError("expected '=' in let expression");
2167 return std::vector<LetRecord>();
2168 }
2169 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002170
Craig Topper011817a2014-04-09 04:50:04 +00002171 Init *Val = ParseValue(nullptr);
2172 if (!Val) return std::vector<LetRecord>();
Bob Wilson7248f862009-11-22 04:24:42 +00002173
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002174 // Now that we have everything, add the record.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00002175 Result.emplace_back(std::move(Name), std::move(Bits), Val, NameLoc);
Bob Wilson7248f862009-11-22 04:24:42 +00002176
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002177 if (Lex.getCode() != tgtok::comma)
2178 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002179 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002180 }
2181}
2182
2183/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002184/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002185///
2186/// Object ::= LET LetList IN '{' ObjectList '}'
2187/// Object ::= LET LetList IN Object
2188///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002189bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002190 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2191 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002192
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002193 // Add this entry to the let stack.
2194 std::vector<LetRecord> LetInfo = ParseLetList();
2195 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002196 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002197
2198 if (Lex.getCode() != tgtok::In)
2199 return TokError("expected 'in' at end of top-level 'let'");
2200 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002201
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002202 // If this is a scalar let, just handle it now
2203 if (Lex.getCode() != tgtok::l_brace) {
2204 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002205 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002206 return true;
2207 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002208 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002209 // Otherwise, this is a group let.
2210 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002211
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002212 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002213 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002214 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002215
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002216 if (Lex.getCode() != tgtok::r_brace) {
2217 TokError("expected '}' at end of top level let command");
2218 return Error(BraceLoc, "to match this '{'");
2219 }
2220 Lex.Lex();
2221 }
Bob Wilson7248f862009-11-22 04:24:42 +00002222
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002223 // Outside this let scope, this let block is not active.
2224 LetStack.pop_back();
2225 return false;
2226}
2227
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002228/// ParseMultiClass - Parse a multiclass definition.
2229///
Bob Wilson3d948162009-04-28 19:41:44 +00002230/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002231/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2232/// MultiClassObject ::= DefInst
2233/// MultiClassObject ::= MultiClassInst
2234/// MultiClassObject ::= DefMInst
2235/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2236/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002237///
2238bool TGParser::ParseMultiClass() {
2239 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2240 Lex.Lex(); // Eat the multiclass token.
2241
2242 if (Lex.getCode() != tgtok::Id)
2243 return TokError("expected identifier after multiclass for name");
2244 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002245
Craig Topper7adf2bf2014-12-11 05:25:30 +00002246 auto Result =
2247 MultiClasses.insert(std::make_pair(Name,
2248 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2249
2250 if (!Result.second)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002251 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002252
Craig Topper7adf2bf2014-12-11 05:25:30 +00002253 CurMultiClass = Result.first->second.get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002254 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002255
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002256 // If there are template args, parse them.
2257 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002258 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002259 return true;
2260
David Greene7049e792009-04-24 16:55:41 +00002261 bool inherits = false;
2262
David Greene753ed8f2009-04-22 16:42:54 +00002263 // If there are submulticlasses, parse them.
2264 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002265 inherits = true;
2266
David Greene753ed8f2009-04-22 16:42:54 +00002267 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002268
David Greene753ed8f2009-04-22 16:42:54 +00002269 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002270 SubMultiClassReference SubMultiClass =
2271 ParseSubMultiClassReference(CurMultiClass);
David Greene753ed8f2009-04-22 16:42:54 +00002272 while (1) {
2273 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002274 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002275
David Greene753ed8f2009-04-22 16:42:54 +00002276 // Add it.
2277 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2278 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002279
David Greene753ed8f2009-04-22 16:42:54 +00002280 if (Lex.getCode() != tgtok::comma) break;
2281 Lex.Lex(); // eat ','.
2282 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2283 }
2284 }
2285
David Greene7049e792009-04-24 16:55:41 +00002286 if (Lex.getCode() != tgtok::l_brace) {
2287 if (!inherits)
2288 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002289 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002290 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002291 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002292 } else {
David Greene7049e792009-04-24 16:55:41 +00002293 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2294 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002295
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002296 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002297 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002298 default:
2299 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
2300 case tgtok::Let:
2301 case tgtok::Def:
2302 case tgtok::Defm:
2303 case tgtok::Foreach:
2304 if (ParseObject(CurMultiClass))
2305 return true;
2306 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002307 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002308 }
David Greene7049e792009-04-24 16:55:41 +00002309 Lex.Lex(); // eat the '}'.
2310 }
Bob Wilson7248f862009-11-22 04:24:42 +00002311
Craig Topper011817a2014-04-09 04:50:04 +00002312 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002313 return false;
2314}
2315
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002316Record *TGParser::InstantiateMulticlassDef(MultiClass &MC, Record *DefProto,
2317 Init *&DefmPrefix,
2318 SMRange DefmPrefixRange,
2319 ArrayRef<Init *> TArgs,
2320 std::vector<Init *> &TemplateVals) {
David Greene5d5d88c2011-10-19 13:04:31 +00002321 // We need to preserve DefProto so it can be reused for later
2322 // instantiations, so create a new Record to inherit from it.
2323
David Greenedb445972011-10-05 22:42:07 +00002324 // Add in the defm name. If the defm prefix is empty, give each
2325 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2326 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2327 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002328
Jordan Roseabdd99b2013-01-10 18:50:05 +00002329 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002330 if (!DefmPrefix) {
David Greene5d5d88c2011-10-19 13:04:31 +00002331 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002332 IsAnonymous = true;
2333 }
David Greene5d5d88c2011-10-19 13:04:31 +00002334
2335 Init *DefName = DefProto->getNameInit();
Sean Silvafb509ed2012-10-10 20:24:43 +00002336 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002337
Craig Topper011817a2014-04-09 04:50:04 +00002338 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002339 // We have a fully expanded string so there are no operators to
2340 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002341 DefName =
2342 BinOpInit::get(BinOpInit::STRCONCAT,
2343 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2344 StringRecTy::get())->Fold(DefProto, &MC),
2345 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2346 }
David Greene5d5d88c2011-10-19 13:04:31 +00002347
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002348 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002349 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002350 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Craig Topper84138712014-11-29 05:31:10 +00002351 auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002352
2353 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002354 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002355 Ref.Rec = DefProto;
Craig Topper84138712014-11-29 05:31:10 +00002356 AddSubClass(CurRec.get(), Ref);
David Greenedb445972011-10-05 22:42:07 +00002357
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002358 // Set the value for NAME. We don't resolve references to it 'til later,
2359 // though, so that uses in nested multiclass names don't get
2360 // confused.
Craig Topper84138712014-11-29 05:31:10 +00002361 if (SetValue(CurRec.get(), Ref.RefRange.Start, "NAME",
2362 std::vector<unsigned>(), DefmPrefix)) {
Craig Topper85c07002015-04-30 05:54:22 +00002363 Error(DefmPrefixRange.Start, "Could not resolve " +
2364 CurRec->getNameInitAsString() + ":NAME to '" +
2365 DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002366 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002367 }
David Greene8bf0d722011-10-19 13:04:35 +00002368
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002369 // If the DefNameString didn't resolve, we probably have a reference to
2370 // NAME and need to replace it. We need to do at least this much greedily,
2371 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002372 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002373 RecordVal *DefNameRV = CurRec->getValue("NAME");
2374 CurRec->resolveReferencesTo(DefNameRV);
2375 }
2376
2377 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002378 // Now that we're at the top level, resolve all NAME references
2379 // in the resultant defs that weren't in the def names themselves.
2380 RecordVal *DefNameRV = CurRec->getValue("NAME");
2381 CurRec->resolveReferencesTo(DefNameRV);
2382
Hal Finkeld2497362015-05-21 04:32:56 +00002383 // Check if the name is a complex pattern.
2384 // If so, resolve it.
2385 DefName = CurRec->getNameInit();
2386 DefNameString = dyn_cast<StringInit>(DefName);
2387
2388 // OK the pattern is more complex than simply using NAME.
2389 // Let's use the heavy weaponery.
2390 if (!DefNameString) {
2391 ResolveMulticlassDefArgs(MC, CurRec.get(), DefmPrefixRange.Start,
2392 Lex.getLoc(), TArgs, TemplateVals,
2393 false/*Delete args*/);
2394 DefName = CurRec->getNameInit();
2395 DefNameString = dyn_cast<StringInit>(DefName);
2396
2397 if (!DefNameString)
2398 DefName = DefName->convertInitializerTo(StringRecTy::get());
2399
2400 // We ran out of options here...
2401 DefNameString = dyn_cast<StringInit>(DefName);
2402 if (!DefNameString) {
2403 PrintFatalError(CurRec->getLoc()[CurRec->getLoc().size() - 1],
2404 DefName->getAsUnquotedString() + " is not a string.");
2405 return nullptr;
2406 }
2407
2408 CurRec->setName(DefName);
2409 }
2410
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002411 // Now that NAME references are resolved and we're at the top level of
2412 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002413 // currently in a multiclass, it means this defm appears inside a
2414 // multiclass and its name won't be fully resolvable until we see
Hal Finkeld2497362015-05-21 04:32:56 +00002415 // the top-level defm. Therefore, we don't add this to the
2416 // RecordKeeper at this point. If we did we could get duplicate
David Greene8bf0d722011-10-19 13:04:35 +00002417 // defs as more than one probably refers to NAME or some other
2418 // common internal placeholder.
2419
2420 // Ensure redefinition doesn't happen.
2421 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002422 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002423 "' already defined, instantiating defm with subdef '" +
2424 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002425 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002426 }
2427
Craig Topper84138712014-11-29 05:31:10 +00002428 Record *CurRecSave = CurRec.get(); // Keep a copy before we release.
Craig Toppercdab2322014-11-29 05:52:51 +00002429 Records.addDef(std::move(CurRec));
Craig Topper84138712014-11-29 05:31:10 +00002430 return CurRecSave;
David Greene8bf0d722011-10-19 13:04:35 +00002431 }
2432
Craig Topper84138712014-11-29 05:31:10 +00002433 // FIXME This is bad but the ownership transfer to caller is pretty messy.
2434 // The unique_ptr in this function at least protects the exits above.
2435 return CurRec.release();
David Greenedb445972011-10-05 22:42:07 +00002436}
2437
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002438bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC, Record *CurRec,
2439 SMLoc DefmPrefixLoc, SMLoc SubClassLoc,
2440 ArrayRef<Init *> TArgs,
David Greenedb445972011-10-05 22:42:07 +00002441 std::vector<Init *> &TemplateVals,
2442 bool DeleteArgs) {
2443 // Loop over all of the template arguments, setting them to the specified
2444 // value or leaving them as the default if necessary.
2445 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2446 // Check if a value is specified for this temp-arg.
2447 if (i < TemplateVals.size()) {
2448 // Set it now.
2449 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2450 TemplateVals[i]))
2451 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002452
David Greenedb445972011-10-05 22:42:07 +00002453 // Resolve it next.
2454 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2455
2456 if (DeleteArgs)
2457 // Now remove it.
2458 CurRec->removeValue(TArgs[i]);
Craig Toppera9642b42015-05-04 01:35:39 +00002459
David Greenedb445972011-10-05 22:42:07 +00002460 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Craig Topper85c07002015-04-30 05:54:22 +00002461 return Error(SubClassLoc, "value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00002462 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +00002463 ") of multiclassclass '" + MC.Rec.getNameInitAsString() +
2464 "'");
David Greenedb445972011-10-05 22:42:07 +00002465 }
2466 }
2467 return false;
2468}
2469
2470bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2471 Record *CurRec,
2472 Record *DefProto,
2473 SMLoc DefmPrefixLoc) {
2474 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002475 if (ApplyLetStack(CurRec))
2476 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002477
David Greenedb445972011-10-05 22:42:07 +00002478 // Don't create a top level definition for defm inside multiclasses,
2479 // instead, only update the prototypes and bind the template args
2480 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002481 if (!CurMultiClass)
2482 return false;
Craig Toppereb4d7c62015-04-29 04:43:36 +00002483 for (const auto &Proto : CurMultiClass->DefPrototypes)
2484 if (Proto->getNameInit() == CurRec->getNameInit())
Sean Silvacc951b22013-01-09 05:28:12 +00002485 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2486 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002487 CurMultiClass->DefPrototypes.push_back(std::unique_ptr<Record>(CurRec));
David Greenedb445972011-10-05 22:42:07 +00002488
Sean Silvacc951b22013-01-09 05:28:12 +00002489 // Copy the template arguments for the multiclass into the new def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002490 for (Init * TA : CurMultiClass->Rec.getTemplateArgs()) {
2491 const RecordVal *RV = CurMultiClass->Rec.getValue(TA);
Sean Silvacc951b22013-01-09 05:28:12 +00002492 assert(RV && "Template arg doesn't exist?");
2493 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002494 }
2495
2496 return false;
2497}
2498
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002499/// ParseDefm - Parse the instantiation of a multiclass.
2500///
2501/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2502///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002503bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002504 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002505 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002506 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002507
Craig Topperb21afc62013-01-07 05:09:33 +00002508 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002509 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002510 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002511
Jordan Rosef12e8a92013-01-10 18:50:11 +00002512 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002513 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002514 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002515
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002516 // Keep track of the new generated record definitions.
2517 std::vector<Record*> NewRecDefs;
2518
2519 // This record also inherits from a regular class (non-multiclass)?
2520 bool InheritFromClass = false;
2521
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002522 // eat the colon.
2523 Lex.Lex();
2524
Chris Lattner526c8cb2009-06-21 03:39:35 +00002525 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002526 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002527
2528 while (1) {
Craig Topper011817a2014-04-09 04:50:04 +00002529 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002530
2531 // To instantiate a multiclass, we need to first get the multiclass, then
2532 // instantiate each def contained in the multiclass with the SubClassRef
2533 // template parameters.
Craig Topper7adf2bf2014-12-11 05:25:30 +00002534 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper4ca40012014-11-30 01:20:17 +00002535 assert(MC && "Didn't lookup multiclass correctly?");
David Greeneaf8ee2c2011-07-29 22:43:06 +00002536 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002537
2538 // Verify that the correct number of template arguments were specified.
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002539 ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002540 if (TArgs.size() < TemplateVals.size())
2541 return Error(SubClassLoc,
2542 "more template args specified than multiclass expects");
2543
2544 // Loop over all the def's in the multiclass, instantiating each one.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002545 for (const std::unique_ptr<Record> &DefProto : MC->DefPrototypes) {
Hal Finkeld2497362015-05-21 04:32:56 +00002546 // The record name construction goes as follow:
2547 // - If the def name is a string, prepend the prefix.
2548 // - If the def name is a more complex pattern, use that pattern.
2549 // As a result, the record is instanciated before resolving
2550 // arguments, as it would make its name a string.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002551 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto.get(), DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002552 SMRange(DefmLoc,
Hal Finkeld2497362015-05-21 04:32:56 +00002553 DefmPrefixEndLoc),
2554 TArgs, TemplateVals);
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002555 if (!CurRec)
2556 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002557
Hal Finkeld2497362015-05-21 04:32:56 +00002558 // Now that the record is instanciated, we can resolve arguments.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002559 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002560 TArgs, TemplateVals, true/*Delete args*/))
2561 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002562
Craig Toppereb4d7c62015-04-29 04:43:36 +00002563 if (ResolveMulticlassDef(*MC, CurRec, DefProto.get(), DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002564 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002565
Adam Nemete5a07162014-09-16 17:14:13 +00002566 // Defs that can be used by other definitions should be fully resolved
2567 // before any use.
2568 if (DefProto->isResolveFirst() && !CurMultiClass) {
2569 CurRec->resolveReferences();
2570 CurRec->setResolveFirst(false);
2571 }
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002572 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002573 }
2574
David Greenedb445972011-10-05 22:42:07 +00002575
David Greenef00919a2009-04-22 22:17:51 +00002576 if (Lex.getCode() != tgtok::comma) break;
2577 Lex.Lex(); // eat ','.
2578
Craig Topper998a39a2013-08-20 04:22:09 +00002579 if (Lex.getCode() != tgtok::Id)
2580 return TokError("expected identifier");
2581
David Greenef00919a2009-04-22 22:17:51 +00002582 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002583
2584 // A defm can inherit from regular classes (non-multiclass) as
2585 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002586 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002587
2588 if (InheritFromClass)
2589 break;
2590
Craig Topper011817a2014-04-09 04:50:04 +00002591 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002592 }
2593
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002594 if (InheritFromClass) {
2595 // Process all the classes to inherit as if they were part of a
2596 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002597 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002598 while (1) {
2599 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002600 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002601
2602 // Get the expanded definition prototypes and teach them about
2603 // the record values the current class to inherit has
Craig Toppereb4d7c62015-04-29 04:43:36 +00002604 for (Record *CurRec : NewRecDefs) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002605 // Add it.
2606 if (AddSubClass(CurRec, SubClass))
2607 return true;
2608
Sean Silvacb1a75e2013-01-09 04:49:14 +00002609 if (ApplyLetStack(CurRec))
2610 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002611 }
2612
2613 if (Lex.getCode() != tgtok::comma) break;
2614 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002615 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002616 }
2617 }
2618
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002619 if (!CurMultiClass)
Craig Toppereb4d7c62015-04-29 04:43:36 +00002620 for (Record *CurRec : NewRecDefs)
David Greene50c09122011-08-10 18:27:46 +00002621 // See Record::setName(). This resolve step will see any new
2622 // name for the def that might have been created when resolving
2623 // inheritance, values and arguments above.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002624 CurRec->resolveReferences();
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002625
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002626 if (Lex.getCode() != tgtok::semi)
2627 return TokError("expected ';' at end of defm");
2628 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002629
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002630 return false;
2631}
2632
2633/// ParseObject
2634/// Object ::= ClassInst
2635/// Object ::= DefInst
2636/// Object ::= MultiClassInst
2637/// Object ::= DefMInst
2638/// Object ::= LETCommand '{' ObjectList '}'
2639/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002640bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002641 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002642 default:
2643 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002644 case tgtok::Let: return ParseTopLevelLet(MC);
2645 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002646 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002647 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002648 case tgtok::Class: return ParseClass();
2649 case tgtok::MultiClass: return ParseMultiClass();
2650 }
2651}
2652
2653/// ParseObjectList
2654/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002655bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002656 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002657 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002658 return true;
2659 }
2660 return false;
2661}
2662
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002663bool TGParser::ParseFile() {
2664 Lex.Lex(); // Prime the lexer.
2665 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002666
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002667 // If we have unread input at the end of the file, report it.
2668 if (Lex.getCode() == tgtok::Eof)
2669 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002670
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002671 return TokError("Unexpected input at top level");
2672}
2673