blob: 66d8555628df83ba0d4c97fa4994b1e7b5fceebb [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.
151 const std::vector<RecordVal> &Vals = SC->getValues();
152 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
Jordan Rosef12e8a92013-01-10 18:50:11 +0000153 if (AddValue(CurRec, SubClass.RefRange.Start, Vals[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000154 return true;
155
David Greenedb10e692011-10-19 13:02:42 +0000156 const std::vector<Init *> &TArgs = SC->getTemplateArgs();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000157
158 // Ensure that an appropriate number of template arguments are specified.
159 if (TArgs.size() < SubClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000160 return Error(SubClass.RefRange.Start,
161 "More template args specified than expected");
Bob Wilson7248f862009-11-22 04:24:42 +0000162
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000163 // Loop over all of the template arguments, setting them to the specified
164 // value or leaving them as the default if necessary.
165 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
166 if (i < SubClass.TemplateArgs.size()) {
167 // If a value is specified for this template arg, set it now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000168 if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
169 std::vector<unsigned>(), SubClass.TemplateArgs[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000170 return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000171
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000172 // Resolve it next.
173 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson7248f862009-11-22 04:24:42 +0000174
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000175 // Now remove it.
176 CurRec->removeValue(TArgs[i]);
177
178 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000179 return Error(SubClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000180 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000181 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000182 ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000183 }
184 }
185
186 // Since everything went well, we can now set the "superclass" list for the
187 // current record.
188 const std::vector<Record*> &SCs = SC->getSuperClasses();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000189 ArrayRef<SMRange> SCRanges = SC->getSuperClassRanges();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000190 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
191 if (CurRec->isSubClassOf(SCs[i]))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000192 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000193 "Already subclass of '" + SCs[i]->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000194 CurRec->addSuperClass(SCs[i], SCRanges[i]);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000195 }
Bob Wilson7248f862009-11-22 04:24:42 +0000196
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000197 if (CurRec->isSubClassOf(SC))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000198 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000199 "Already subclass of '" + SC->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000200 CurRec->addSuperClass(SC, SubClass.RefRange);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000201 return false;
202}
203
David Greene753ed8f2009-04-22 16:42:54 +0000204/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilsonf71e6562009-04-30 18:26:19 +0000205/// CurMC, resolving its template args as SubMultiClass's
David Greene753ed8f2009-04-22 16:42:54 +0000206/// template arguments.
Bob Wilsonf71e6562009-04-30 18:26:19 +0000207bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson92ab8202009-04-30 17:46:20 +0000208 SubMultiClassReference &SubMultiClass) {
David Greene753ed8f2009-04-22 16:42:54 +0000209 MultiClass *SMC = SubMultiClass.MC;
Bob Wilsonf71e6562009-04-30 18:26:19 +0000210 Record *CurRec = &CurMC->Rec;
David Greene753ed8f2009-04-22 16:42:54 +0000211
David Greene753ed8f2009-04-22 16:42:54 +0000212 // Add all of the values in the subclass into the current class.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000213 for (const auto &SMCVal : SMC->Rec.getValues())
214 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000215 return true;
216
Craig Toppera4ea4b02014-11-30 00:24:32 +0000217 unsigned newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000218
David Greene753ed8f2009-04-22 16:42:54 +0000219 // Add all of the defs in the subclass into the current multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000220 for (const std::unique_ptr<Record> &R : SMC->DefPrototypes) {
David Greene753ed8f2009-04-22 16:42:54 +0000221 // Clone the def and add it to the current multiclass
Craig Toppereb4d7c62015-04-29 04:43:36 +0000222 auto NewDef = make_unique<Record>(*R);
David Greene753ed8f2009-04-22 16:42:54 +0000223
224 // Add all of the values in the superclass into the current def.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000225 for (const auto &MCVal : CurRec->getValues())
226 if (AddValue(NewDef.get(), SubMultiClass.RefRange.Start, MCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000227 return true;
228
Craig Topperc3504c42014-12-11 05:25:33 +0000229 CurMC->DefPrototypes.push_back(std::move(NewDef));
David Greene753ed8f2009-04-22 16:42:54 +0000230 }
Bob Wilson3d948162009-04-28 19:41:44 +0000231
David Greenedb10e692011-10-19 13:02:42 +0000232 const std::vector<Init *> &SMCTArgs = SMC->Rec.getTemplateArgs();
David Greene753ed8f2009-04-22 16:42:54 +0000233
David Greene7049e792009-04-24 16:55:41 +0000234 // Ensure that an appropriate number of template arguments are
235 // specified.
David Greene753ed8f2009-04-22 16:42:54 +0000236 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000237 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000238 "More template args specified than expected");
Bob Wilson3d948162009-04-28 19:41:44 +0000239
David Greene753ed8f2009-04-22 16:42:54 +0000240 // Loop over all of the template arguments, setting them to the specified
241 // value or leaving them as the default if necessary.
242 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
243 if (i < SubMultiClass.TemplateArgs.size()) {
David Greene7049e792009-04-24 16:55:41 +0000244 // If a value is specified for this template arg, set it in the
245 // superclass now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000246 if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson3d948162009-04-28 19:41:44 +0000247 std::vector<unsigned>(),
David Greene753ed8f2009-04-22 16:42:54 +0000248 SubMultiClass.TemplateArgs[i]))
249 return true;
250
251 // Resolve it next.
252 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson3d948162009-04-28 19:41:44 +0000253
David Greene753ed8f2009-04-22 16:42:54 +0000254 // Now remove it.
255 CurRec->removeValue(SMCTArgs[i]);
256
David Greene7049e792009-04-24 16:55:41 +0000257 // If a value is specified for this template arg, set it in the
258 // new defs now.
Craig Topperc3504c42014-12-11 05:25:33 +0000259 for (const auto &Def :
260 makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
261 if (SetValue(Def.get(), SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson3d948162009-04-28 19:41:44 +0000262 std::vector<unsigned>(),
David Greene753ed8f2009-04-22 16:42:54 +0000263 SubMultiClass.TemplateArgs[i]))
264 return true;
265
266 // Resolve it next.
267 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
268
269 // Now remove it
270 Def->removeValue(SMCTArgs[i]);
271 }
272 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000273 return Error(SubMultiClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000274 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000275 Twine(i) + " (" + SMCTArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000276 ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000277 }
278 }
279
280 return false;
281}
282
David Greenefb927af2012-02-22 16:09:41 +0000283/// ProcessForeachDefs - Given a record, apply all of the variable
284/// values in all surrounding foreach loops, creating new records for
285/// each combination of values.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000286bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
287 if (Loops.empty())
288 return false;
289
David Greenefb927af2012-02-22 16:09:41 +0000290 // We want to instantiate a new copy of CurRec for each combination
291 // of nested loop iterator values. We don't want top instantiate
292 // any copies until we have values for each loop iterator.
293 IterSet IterVals;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000294 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000295}
296
297/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
298/// apply each of the variable values in this loop and then process
299/// subloops.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000300bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
301 // Recursively build a tuple of iterator values.
302 if (IterVals.size() != Loops.size()) {
303 assert(IterVals.size() < Loops.size());
304 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silvafb509ed2012-10-10 20:24:43 +0000305 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Craig Topper011817a2014-04-09 04:50:04 +0000306 if (!List) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000307 Error(Loc, "Loop list is not a list");
308 return true;
309 }
David Greenefb927af2012-02-22 16:09:41 +0000310
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000311 // Process each value.
312 for (int64_t i = 0; i < List->getSize(); ++i) {
Craig Topper011817a2014-04-09 04:50:04 +0000313 Init *ItemVal = List->resolveListElementReference(*CurRec, nullptr, i);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000314 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
315 if (ProcessForeachDefs(CurRec, Loc, IterVals))
316 return true;
317 IterVals.pop_back();
318 }
319 return false;
320 }
321
322 // This is the bottom of the recursion. We have all of the iterator values
323 // for this point in the iteration space. Instantiate a new record to
324 // reflect this combination of values.
Craig Topper84138712014-11-29 05:31:10 +0000325 auto IterRec = make_unique<Record>(*CurRec);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000326
327 // Set the iterator values now.
328 for (unsigned i = 0, e = IterVals.size(); i != e; ++i) {
329 VarInit *IterVar = IterVals[i].IterVar;
Sean Silvafb509ed2012-10-10 20:24:43 +0000330 TypedInit *IVal = dyn_cast<TypedInit>(IterVals[i].IterValue);
Craig Topper84138712014-11-29 05:31:10 +0000331 if (!IVal)
332 return Error(Loc, "foreach iterator value is untyped");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000333
334 IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false));
335
Craig Topper84138712014-11-29 05:31:10 +0000336 if (SetValue(IterRec.get(), Loc, IterVar->getName(),
337 std::vector<unsigned>(), IVal))
338 return Error(Loc, "when instantiating this def");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000339
340 // Resolve it next.
341 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName()));
342
343 // Remove it.
344 IterRec->removeValue(IterVar->getName());
345 }
346
347 if (Records.getDef(IterRec->getNameInitAsString())) {
Artyom Skrobov8b985322014-06-10 12:41:14 +0000348 // If this record is anonymous, it's no problem, just generate a new name
Craig Topper84138712014-11-29 05:31:10 +0000349 if (!IterRec->isAnonymous())
350 return Error(Loc, "def already exists: " +IterRec->getNameInitAsString());
351
352 IterRec->setName(GetNewAnonymousName());
David Greenefb927af2012-02-22 16:09:41 +0000353 }
354
Craig Topper84138712014-11-29 05:31:10 +0000355 Record *IterRecSave = IterRec.get(); // Keep a copy before release.
Craig Toppercdab2322014-11-29 05:52:51 +0000356 Records.addDef(std::move(IterRec));
Craig Topper84138712014-11-29 05:31:10 +0000357 IterRecSave->resolveReferences();
David Greenefb927af2012-02-22 16:09:41 +0000358 return false;
359}
360
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000361//===----------------------------------------------------------------------===//
362// Parser Code
363//===----------------------------------------------------------------------===//
364
365/// isObjectStart - Return true if this is a valid first token for an Object.
366static bool isObjectStart(tgtok::TokKind K) {
367 return K == tgtok::Class || K == tgtok::Def ||
David Greenefb927af2012-02-22 16:09:41 +0000368 K == tgtok::Defm || K == tgtok::Let ||
369 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000370}
371
Alp Tokerce91fe52013-12-21 18:51:00 +0000372/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
373/// an identifier.
374std::string TGParser::GetNewAnonymousName() {
Aaron Ballman97a59fb2015-02-16 19:33:36 +0000375 return "anonymous_" + utostr(AnonCounter++);
Chris Lattner7538ed82010-10-05 22:51:56 +0000376}
377
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000378/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000379/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000380/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000381/// ObjectName ::= /*empty*/
382///
David Greene2affd672011-10-19 13:04:29 +0000383Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
384 switch (Lex.getCode()) {
385 case tgtok::colon:
386 case tgtok::semi:
387 case tgtok::l_brace:
388 // These are all of the tokens that can begin an object body.
389 // Some of these can also begin values but we disallow those cases
390 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000391 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000392 default:
393 break;
394 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000395
Craig Topper011817a2014-04-09 04:50:04 +0000396 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000397 if (CurMultiClass)
398 CurRec = &CurMultiClass->Rec;
399
Craig Topper011817a2014-04-09 04:50:04 +0000400 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000401 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000402 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000403 if (!CurRecName) {
404 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000405 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000406 }
407 Type = CurRecName->getType();
408 }
409
410 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000411}
412
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000413/// ParseClassID - Parse and resolve a reference to a class name. This returns
414/// null on error.
415///
416/// ClassID ::= ID
417///
418Record *TGParser::ParseClassID() {
419 if (Lex.getCode() != tgtok::Id) {
420 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000421 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000422 }
Bob Wilson7248f862009-11-22 04:24:42 +0000423
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000424 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000425 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000426 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000427
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000428 Lex.Lex();
429 return Result;
430}
431
Bob Wilson3d948162009-04-28 19:41:44 +0000432/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
433/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000434///
435/// MultiClassID ::= ID
436///
437MultiClass *TGParser::ParseMultiClassID() {
438 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000439 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000440 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000441 }
Bob Wilson3d948162009-04-28 19:41:44 +0000442
Craig Topper7adf2bf2014-12-11 05:25:30 +0000443 MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
Craig Topper4ca40012014-11-30 01:20:17 +0000444 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000445 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000446
David Greene753ed8f2009-04-22 16:42:54 +0000447 Lex.Lex();
Craig Topper4ca40012014-11-30 01:20:17 +0000448 return Result;
David Greene753ed8f2009-04-22 16:42:54 +0000449}
450
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000451/// ParseSubClassReference - Parse a reference to a subclass or to a templated
452/// subclass. This returns a SubClassRefTy with a null Record* on error.
453///
454/// SubClassRef ::= ClassID
455/// SubClassRef ::= ClassID '<' ValueList '>'
456///
457SubClassReference TGParser::
458ParseSubClassReference(Record *CurRec, bool isDefm) {
459 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000460 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000461
Sean Silva0657b402013-01-09 02:17:14 +0000462 if (isDefm) {
463 if (MultiClass *MC = ParseMultiClassID())
464 Result.Rec = &MC->Rec;
465 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000466 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000467 }
Craig Topper011817a2014-04-09 04:50:04 +0000468 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000469
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000470 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000471 if (Lex.getCode() != tgtok::less) {
472 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000473 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000474 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000475 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000476
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000477 if (Lex.getCode() == tgtok::greater) {
478 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000479 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000480 return Result;
481 }
Bob Wilson7248f862009-11-22 04:24:42 +0000482
David Greene8618f952009-06-08 20:23:18 +0000483 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000484 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000485 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000486 return Result;
487 }
Bob Wilson7248f862009-11-22 04:24:42 +0000488
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000489 if (Lex.getCode() != tgtok::greater) {
490 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000491 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000492 return Result;
493 }
494 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000495 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000496
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000497 return Result;
498}
499
Bob Wilson3d948162009-04-28 19:41:44 +0000500/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
501/// templated submulticlass. This returns a SubMultiClassRefTy with a null
502/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000503///
504/// SubMultiClassRef ::= MultiClassID
505/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
506///
507SubMultiClassReference TGParser::
508ParseSubMultiClassReference(MultiClass *CurMC) {
509 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000510 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000511
David Greene753ed8f2009-04-22 16:42:54 +0000512 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000513 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000514
David Greene753ed8f2009-04-22 16:42:54 +0000515 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000516 if (Lex.getCode() != tgtok::less) {
517 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000518 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000519 }
David Greene753ed8f2009-04-22 16:42:54 +0000520 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000521
David Greene753ed8f2009-04-22 16:42:54 +0000522 if (Lex.getCode() == tgtok::greater) {
523 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000524 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000525 return Result;
526 }
Bob Wilson3d948162009-04-28 19:41:44 +0000527
David Greene8618f952009-06-08 20:23:18 +0000528 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000529 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000530 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000531 return Result;
532 }
Bob Wilson3d948162009-04-28 19:41:44 +0000533
David Greene753ed8f2009-04-22 16:42:54 +0000534 if (Lex.getCode() != tgtok::greater) {
535 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000536 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000537 return Result;
538 }
539 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000540 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000541
542 return Result;
543}
544
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000545/// ParseRangePiece - Parse a bit/value range.
546/// RangePiece ::= INTVAL
547/// RangePiece ::= INTVAL '-' INTVAL
548/// RangePiece ::= INTVAL INTVAL
549bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000550 if (Lex.getCode() != tgtok::IntVal) {
551 TokError("expected integer or bitrange");
552 return true;
553 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000554 int64_t Start = Lex.getCurIntVal();
555 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000556
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000557 if (Start < 0)
558 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000559
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000560 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000561 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000562 Ranges.push_back(Start);
563 return false;
564 case tgtok::minus:
565 if (Lex.Lex() != tgtok::IntVal) {
566 TokError("expected integer value as end of range");
567 return true;
568 }
569 End = Lex.getCurIntVal();
570 break;
571 case tgtok::IntVal:
572 End = -Lex.getCurIntVal();
573 break;
574 }
Bob Wilson7248f862009-11-22 04:24:42 +0000575 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000576 return TokError("invalid range, cannot be negative");
577 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000578
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000579 // Add to the range.
Craig Toppera9642b42015-05-04 01:35:39 +0000580 if (Start < End)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000581 for (; Start <= End; ++Start)
582 Ranges.push_back(Start);
Craig Toppera9642b42015-05-04 01:35:39 +0000583 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000584 for (; Start >= End; --Start)
585 Ranges.push_back(Start);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000586 return false;
587}
588
589/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
590///
591/// RangeList ::= RangePiece (',' RangePiece)*
592///
593std::vector<unsigned> TGParser::ParseRangeList() {
594 std::vector<unsigned> Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000595
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000596 // Parse the first piece.
597 if (ParseRangePiece(Result))
598 return std::vector<unsigned>();
599 while (Lex.getCode() == tgtok::comma) {
600 Lex.Lex(); // Eat the comma.
601
602 // Parse the next range piece.
603 if (ParseRangePiece(Result))
604 return std::vector<unsigned>();
605 }
606 return Result;
607}
608
609/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
610/// OptionalRangeList ::= '<' RangeList '>'
611/// OptionalRangeList ::= /*empty*/
612bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
613 if (Lex.getCode() != tgtok::less)
614 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000615
Chris Lattner526c8cb2009-06-21 03:39:35 +0000616 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000617 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000618
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000619 // Parse the range list.
620 Ranges = ParseRangeList();
621 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000622
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000623 if (Lex.getCode() != tgtok::greater) {
624 TokError("expected '>' at end of range list");
625 return Error(StartLoc, "to match this '<'");
626 }
627 Lex.Lex(); // eat the '>'.
628 return false;
629}
630
631/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
632/// OptionalBitList ::= '{' RangeList '}'
633/// OptionalBitList ::= /*empty*/
634bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
635 if (Lex.getCode() != tgtok::l_brace)
636 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000637
Chris Lattner526c8cb2009-06-21 03:39:35 +0000638 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000639 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000640
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000641 // Parse the range list.
642 Ranges = ParseRangeList();
643 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000644
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000645 if (Lex.getCode() != tgtok::r_brace) {
646 TokError("expected '}' at end of bit list");
647 return Error(StartLoc, "to match this '{'");
648 }
649 Lex.Lex(); // eat the '}'.
650 return false;
651}
652
653
654/// ParseType - Parse and return a tblgen type. This returns null on error.
655///
656/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000657/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000658/// Type ::= BIT // bit type
659/// Type ::= BITS '<' INTVAL '>' // bits<x> type
660/// Type ::= INT // int type
661/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000662/// Type ::= DAG // dag type
663/// Type ::= ClassID // Record Type
664///
665RecTy *TGParser::ParseType() {
666 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000667 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000668 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000669 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000670 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
671 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000672 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000673 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000674 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000675 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000676 case tgtok::Bits: {
677 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
678 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000679 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000680 }
681 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
682 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000683 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000684 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000685 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000686 if (Lex.Lex() != tgtok::greater) { // Eat count.
687 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000688 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000689 }
690 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000691 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000692 }
693 case tgtok::List: {
694 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
695 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000696 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000697 }
698 Lex.Lex(); // Eat '<'
699 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000700 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000701
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000702 if (Lex.getCode() != tgtok::greater) {
703 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000704 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000705 }
706 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000707 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000708 }
Bob Wilson7248f862009-11-22 04:24:42 +0000709 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000710}
711
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000712/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
713/// has already been read.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000714Init *TGParser::ParseIDValue(Record *CurRec,
David Greened4263a62011-10-19 13:04:20 +0000715 const std::string &Name, SMLoc NameLoc,
716 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000717 if (CurRec) {
718 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000719 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000720
David Greenedb10e692011-10-19 13:02:42 +0000721 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
722
David Greene47a665e2011-10-05 22:42:54 +0000723 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +0000724 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
725 "::");
David Greene47a665e2011-10-05 22:42:54 +0000726
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000727 if (CurRec->isTemplateArg(TemplateArgName)) {
728 const RecordVal *RV = CurRec->getValue(TemplateArgName);
729 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000730 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000731 }
732 }
Bob Wilson7248f862009-11-22 04:24:42 +0000733
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000734 if (CurMultiClass) {
David Greenedb10e692011-10-19 13:02:42 +0000735 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
736 "::");
737
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000738 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
739 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
740 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000741 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000742 }
743 }
Bob Wilson7248f862009-11-22 04:24:42 +0000744
David Greenefb927af2012-02-22 16:09:41 +0000745 // If this is in a foreach loop, make sure it's not a loop iterator
Craig Toppereb4d7c62015-04-29 04:43:36 +0000746 for (const auto &L : Loops) {
747 VarInit *IterVar = dyn_cast<VarInit>(L.IterVar);
David Greenefb927af2012-02-22 16:09:41 +0000748 if (IterVar && IterVar->getName() == Name)
749 return IterVar;
750 }
751
David Greene232bd602011-10-19 13:04:21 +0000752 if (Mode == ParseNameMode)
753 return StringInit::get(Name);
754
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000755 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000756 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000757
David Greene232bd602011-10-19 13:04:21 +0000758 if (Mode == ParseValueMode) {
759 Error(NameLoc, "Variable not defined: '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000760 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000761 }
Craig Toppera9642b42015-05-04 01:35:39 +0000762
David Greene232bd602011-10-19 13:04:21 +0000763 return StringInit::get(Name);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000764}
765
David Greene5d0c0512009-05-14 20:54:48 +0000766/// ParseOperation - Parse an operator. This returns null on error.
767///
768/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
769///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000770Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000771 switch (Lex.getCode()) {
772 default:
773 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000774 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000775 case tgtok::XHead:
776 case tgtok::XTail:
777 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000778 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
779 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000780 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000781
David Greenee8f3b272009-05-14 21:22:49 +0000782 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000783 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000784 case tgtok::XCast:
785 Lex.Lex(); // eat the operation
786 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000787
David Greenee8f3b272009-05-14 21:22:49 +0000788 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000789
Craig Topper011817a2014-04-09 04:50:04 +0000790 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000791 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000792 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000793 }
David Greene5d0c0512009-05-14 20:54:48 +0000794
David Greenee8f3b272009-05-14 21:22:49 +0000795 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000796 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000797 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000798 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000799 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000800 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000801 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000802 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000803 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000804 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000805 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000806 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000807 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000808 break;
David Greenee8f3b272009-05-14 21:22:49 +0000809 }
810 if (Lex.getCode() != tgtok::l_paren) {
811 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000812 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000813 }
814 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000815
David Greeneaf8ee2c2011-07-29 22:43:06 +0000816 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000817 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000818
Craig Topper85c07002015-04-30 05:54:22 +0000819 if (Code == UnOpInit::HEAD ||
820 Code == UnOpInit::TAIL ||
821 Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000822 ListInit *LHSl = dyn_cast<ListInit>(LHS);
823 StringInit *LHSs = dyn_cast<StringInit>(LHS);
824 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000825 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000826 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000827 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000828 }
829 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000830 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
831 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000832 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000833 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000834 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000835 }
836 }
837
Craig Topper85c07002015-04-30 05:54:22 +0000838 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
Craig Topper011817a2014-04-09 04:50:04 +0000839 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000840 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000841 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000842 }
Bob Wilson7248f862009-11-22 04:24:42 +0000843
Craig Topperec9072d2015-05-14 05:53:53 +0000844 if (LHSl && LHSl->empty()) {
David Greened571b3c2009-05-14 22:38:31 +0000845 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000846 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000847 }
848 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000849 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000850 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000851 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000852 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000853 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000854 }
Craig Toppera9642b42015-05-04 01:35:39 +0000855 Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
856 : ListRecTy::get(Itemt->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000857 } else {
David Greened571b3c2009-05-14 22:38:31 +0000858 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000859 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000860 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000861 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000862 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000863 }
Craig Toppera9642b42015-05-04 01:35:39 +0000864 Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
David Greened571b3c2009-05-14 22:38:31 +0000865 }
866 }
867 }
868
David Greenee8f3b272009-05-14 21:22:49 +0000869 if (Lex.getCode() != tgtok::r_paren) {
870 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000871 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000872 }
873 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000874 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000875 }
David Greene5d0c0512009-05-14 20:54:48 +0000876
877 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000878 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000879 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +0000880 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000881 case tgtok::XSRL:
882 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000883 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000884 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000885 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000886 tgtok::TokKind OpTok = Lex.getCode();
887 SMLoc OpLoc = Lex.getLoc();
888 Lex.Lex(); // eat the operation
889
David Greene5d0c0512009-05-14 20:54:48 +0000890 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000891 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000892
Chris Lattner61ea00b2010-10-05 23:58:18 +0000893 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000894 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000895 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000896 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000897 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000898 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
899 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
900 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
901 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000902 case tgtok::XListConcat:
903 Code = BinOpInit::LISTCONCAT;
904 // We don't know the list type until we parse the first argument
905 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000906 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000907 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000908 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000909 break;
David Greene5d0c0512009-05-14 20:54:48 +0000910 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000911
David Greene5d0c0512009-05-14 20:54:48 +0000912 if (Lex.getCode() != tgtok::l_paren) {
913 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000914 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000915 }
916 Lex.Lex(); // eat the '('
917
David Greeneaf8ee2c2011-07-29 22:43:06 +0000918 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000919
Chris Lattner61ea00b2010-10-05 23:58:18 +0000920 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000921 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000922
Chris Lattner61ea00b2010-10-05 23:58:18 +0000923 while (Lex.getCode() == tgtok::comma) {
924 Lex.Lex(); // eat the ','
925
926 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000927 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000928 }
David Greene5d0c0512009-05-14 20:54:48 +0000929
930 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000931 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000932 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000933 }
934 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000935
Daniel Sanders314e80e2014-05-07 10:13:19 +0000936 // If we are doing !listconcat, we should know the type by now
937 if (OpTok == tgtok::XListConcat) {
938 if (VarInit *Arg0 = dyn_cast<VarInit>(InitList[0]))
939 Type = Arg0->getType();
940 else if (ListInit *Arg0 = dyn_cast<ListInit>(InitList[0]))
941 Type = Arg0->getType();
942 else {
943 InitList[0]->dump();
944 Error(OpLoc, "expected a list");
945 return nullptr;
946 }
947 }
948
Chris Lattner61ea00b2010-10-05 23:58:18 +0000949 // We allow multiple operands to associative operators like !strconcat as
950 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000951 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000952 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000953 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000954 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
955 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000956 InitList.back() = RHS;
957 }
958 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000959
Chris Lattner61ea00b2010-10-05 23:58:18 +0000960 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000961 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000962 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000963
Chris Lattner61ea00b2010-10-05 23:58:18 +0000964 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +0000965 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000966 }
967
David Greene3587eed2009-05-14 23:26:46 +0000968 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +0000969 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +0000970 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
971 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000972 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000973
David Greene98ed3c72009-05-14 21:54:42 +0000974 tgtok::TokKind LexCode = Lex.getCode();
975 Lex.Lex(); // eat the operation
976 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +0000977 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +0000978 case tgtok::XIf:
979 Code = TernOpInit::IF;
980 break;
David Greenee917fff2009-05-14 22:23:47 +0000981 case tgtok::XForEach:
982 Code = TernOpInit::FOREACH;
983 break;
David Greene98ed3c72009-05-14 21:54:42 +0000984 case tgtok::XSubst:
985 Code = TernOpInit::SUBST;
986 break;
987 }
988 if (Lex.getCode() != tgtok::l_paren) {
989 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000990 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +0000991 }
992 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000993
David Greeneaf8ee2c2011-07-29 22:43:06 +0000994 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000995 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000996
David Greene98ed3c72009-05-14 21:54:42 +0000997 if (Lex.getCode() != tgtok::comma) {
998 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000999 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001000 }
1001 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001002
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001003 Init *MHS = ParseValue(CurRec, ItemType);
1004 if (!MHS)
1005 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001006
David Greene98ed3c72009-05-14 21:54:42 +00001007 if (Lex.getCode() != tgtok::comma) {
1008 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001009 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001010 }
1011 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001012
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001013 Init *RHS = ParseValue(CurRec, ItemType);
1014 if (!RHS)
1015 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001016
David Greene98ed3c72009-05-14 21:54:42 +00001017 if (Lex.getCode() != tgtok::r_paren) {
1018 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001019 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001020 }
1021 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001022
David Greene98ed3c72009-05-14 21:54:42 +00001023 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001024 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001025 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001026 RecTy *MHSTy = nullptr;
1027 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001028
Sean Silvafb509ed2012-10-10 20:24:43 +00001029 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001030 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001031 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001032 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001033 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001034 MHSTy = BitRecTy::get();
1035
Sean Silvafb509ed2012-10-10 20:24:43 +00001036 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001037 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001038 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001039 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001040 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001041 RHSTy = BitRecTy::get();
1042
1043 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001044 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001045 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001046 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001047 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001048
1049 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001050 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001051 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001052 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001053
1054 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1055 Type = RHSTy;
1056 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1057 Type = MHSTy;
Bob Wilson7248f862009-11-22 04:24:42 +00001058 } else {
David Greene3587eed2009-05-14 23:26:46 +00001059 TokError("inconsistent types for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001060 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001061 }
1062 break;
1063 }
David Greenee917fff2009-05-14 22:23:47 +00001064 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001065 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
Craig Topper011817a2014-04-09 04:50:04 +00001066 if (!MHSt) {
David Greenee917fff2009-05-14 22:23:47 +00001067 TokError("could not get type for !foreach");
Craig Topper011817a2014-04-09 04:50:04 +00001068 return nullptr;
David Greenee917fff2009-05-14 22:23:47 +00001069 }
1070 Type = MHSt->getType();
1071 break;
1072 }
David Greene98ed3c72009-05-14 21:54:42 +00001073 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001074 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001075 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001076 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001077 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001078 }
1079 Type = RHSt->getType();
1080 break;
1081 }
1082 }
David Greenee32ebf22011-07-29 19:07:07 +00001083 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001084 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001085 }
David Greene5d0c0512009-05-14 20:54:48 +00001086 }
David Greene5d0c0512009-05-14 20:54:48 +00001087}
1088
1089/// ParseOperatorType - Parse a type for an operator. This returns
1090/// null on error.
1091///
1092/// OperatorType ::= '<' Type '>'
1093///
Dan Gohman1432ef82009-08-12 22:10:57 +00001094RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001095 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001096
1097 if (Lex.getCode() != tgtok::less) {
1098 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001099 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001100 }
1101 Lex.Lex(); // eat the <
1102
1103 Type = ParseType();
1104
Craig Topper011817a2014-04-09 04:50:04 +00001105 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001106 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001107 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001108 }
1109
1110 if (Lex.getCode() != tgtok::greater) {
1111 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001112 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001113 }
1114 Lex.Lex(); // eat the >
1115
1116 return Type;
1117}
1118
1119
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001120/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1121///
1122/// SimpleValue ::= IDValue
1123/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001124/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001125/// SimpleValue ::= CODEFRAGMENT
1126/// SimpleValue ::= '?'
1127/// SimpleValue ::= '{' ValueList '}'
1128/// SimpleValue ::= ID '<' ValueListNE '>'
1129/// SimpleValue ::= '[' ValueList ']'
1130/// SimpleValue ::= '(' IDValue DagArgList ')'
1131/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001132/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001133/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1134/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1135/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001136/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001137/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1138///
David Greened4263a62011-10-19 13:04:20 +00001139Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1140 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001141 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001142 switch (Lex.getCode()) {
1143 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001144 case tgtok::paste:
1145 // This is a leading paste operation. This is deprecated but
1146 // still exists in some .td files. Ignore it.
1147 Lex.Lex(); // Skip '#'.
1148 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001149 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001150 case tgtok::BinaryIntVal: {
1151 auto BinaryVal = Lex.getCurBinaryIntVal();
1152 SmallVector<Init*, 16> Bits(BinaryVal.second);
1153 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001154 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001155 R = BitsInit::get(Bits);
1156 Lex.Lex();
1157 break;
1158 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001159 case tgtok::StrVal: {
1160 std::string Val = Lex.getCurStrVal();
1161 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001162
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001163 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001164 while (Lex.getCode() == tgtok::StrVal) {
1165 Val += Lex.getCurStrVal();
1166 Lex.Lex();
1167 }
Bob Wilson7248f862009-11-22 04:24:42 +00001168
David Greenee32ebf22011-07-29 19:07:07 +00001169 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001170 break;
1171 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001172 case tgtok::CodeFragment:
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +00001173 R = StringInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001174 Lex.Lex();
1175 break;
1176 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001177 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001178 Lex.Lex();
1179 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001180 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001181 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001182 std::string Name = Lex.getCurStrVal();
1183 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001184 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001185
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001186 // Value ::= ID '<' ValueListNE '>'
1187 if (Lex.Lex() == tgtok::greater) {
1188 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001189 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001190 }
David Greene8618f952009-06-08 20:23:18 +00001191
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001192 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1193 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1194 // body.
1195 Record *Class = Records.getClass(Name);
1196 if (!Class) {
1197 Error(NameLoc, "Expected a class name, got '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001198 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001199 }
David Greene8618f952009-06-08 20:23:18 +00001200
David Greeneaf8ee2c2011-07-29 22:43:06 +00001201 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
Craig Topper011817a2014-04-09 04:50:04 +00001202 if (ValueList.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001203
David Greene8618f952009-06-08 20:23:18 +00001204 if (Lex.getCode() != tgtok::greater) {
1205 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001206 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001207 }
1208 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001209 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001210
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001211 // Create the new record, set it as CurRec temporarily.
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00001212 auto NewRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), NameLoc,
1213 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001214 Record *NewRec = NewRecOwner.get(); // Keep a copy since we may release.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001215 SubClassReference SCRef;
Jordan Rosef12e8a92013-01-10 18:50:11 +00001216 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001217 SCRef.Rec = Class;
1218 SCRef.TemplateArgs = ValueList;
1219 // Add info about the subclass to NewRec.
Craig Topper84138712014-11-29 05:31:10 +00001220 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001221 return nullptr;
Craig Topper84138712014-11-29 05:31:10 +00001222
Hal Finkela8c1f462014-01-02 20:47:09 +00001223 if (!CurMultiClass) {
1224 NewRec->resolveReferences();
Craig Toppercdab2322014-11-29 05:52:51 +00001225 Records.addDef(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001226 } else {
Adam Nemete5a07162014-09-16 17:14:13 +00001227 // This needs to get resolved once the multiclass template arguments are
1228 // known before any use.
1229 NewRec->setResolveFirst(true);
Hal Finkela8c1f462014-01-02 20:47:09 +00001230 // Otherwise, we're inside a multiclass, add it to the multiclass.
Craig Topperc3504c42014-12-11 05:25:33 +00001231 CurMultiClass->DefPrototypes.push_back(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001232
1233 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00001234 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
1235 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Hal Finkela8c1f462014-01-02 20:47:09 +00001236 assert(RV && "Template arg doesn't exist?");
1237 NewRec->addValue(*RV);
1238 }
1239
1240 // We can't return the prototype def here, instead return:
1241 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1242 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1243 assert(MCNameRV && "multiclass record must have a NAME");
1244
1245 return UnOpInit::get(UnOpInit::CAST,
1246 BinOpInit::get(BinOpInit::STRCONCAT,
1247 VarInit::get(MCNameRV->getName(),
1248 MCNameRV->getType()),
1249 NewRec->getNameInit(),
1250 StringRecTy::get()),
1251 Class->getDefInit()->getType());
1252 }
Bob Wilson7248f862009-11-22 04:24:42 +00001253
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001254 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001255 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001256 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001257 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001258 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001259 Lex.Lex(); // eat the '{'
David Greeneaf8ee2c2011-07-29 22:43:06 +00001260 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001261
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001262 if (Lex.getCode() != tgtok::r_brace) {
1263 Vals = ParseValueList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001264 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001265 }
1266 if (Lex.getCode() != tgtok::r_brace) {
1267 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001268 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001269 }
1270 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001271
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001272 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001273
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001274 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1275 // first. We'll first read everything in to a vector, then we can reverse
1276 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001277 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001278 // FIXME: The following two loops would not be duplicated
1279 // if the API was a little more orthogonal.
1280
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001281 // bits<n> values are allowed to initialize n bits.
1282 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1283 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1284 NewBits.push_back(BI->getBit((e - i) - 1));
1285 continue;
1286 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001287 // bits<n> can also come from variable initializers.
1288 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1289 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1290 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1291 NewBits.push_back(VI->getBit((e - i) - 1));
1292 continue;
1293 }
1294 // Fallthrough to try convert this to a bit.
1295 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001296 // All other values must be convertible to just a single bit.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001297 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001298 if (!Bit) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001299 Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
Chris Lattner52416952007-11-22 21:06:59 +00001300 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001301 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001302 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001303 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001304 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001305 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001306 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001307 }
1308 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1309 Lex.Lex(); // eat the '['
David Greeneaf8ee2c2011-07-29 22:43:06 +00001310 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001311
Craig Topper011817a2014-04-09 04:50:04 +00001312 RecTy *DeducedEltTy = nullptr;
1313 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001314
Craig Topper011817a2014-04-09 04:50:04 +00001315 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001316 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001317 if (!ListType) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001318 TokError(Twine("Type mismatch for list, expected list type, got ") +
1319 ItemType->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001320 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001321 }
1322 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001323 }
David Greene8618f952009-06-08 20:23:18 +00001324
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001325 if (Lex.getCode() != tgtok::r_square) {
Craig Topper011817a2014-04-09 04:50:04 +00001326 Vals = ParseValueList(CurRec, nullptr,
1327 GivenListTy ? GivenListTy->getElementType() : nullptr);
1328 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001329 }
1330 if (Lex.getCode() != tgtok::r_square) {
1331 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001332 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001333 }
1334 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001335
Craig Topper011817a2014-04-09 04:50:04 +00001336 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001337 if (Lex.getCode() == tgtok::less) {
1338 // Optional list element type
1339 Lex.Lex(); // eat the '<'
1340
1341 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001342 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001343 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001344 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001345 }
1346
1347 if (Lex.getCode() != tgtok::greater) {
1348 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001349 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001350 }
1351 Lex.Lex(); // eat the '>'
1352 }
1353
1354 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001355 RecTy *EltTy = nullptr;
Craig Toppereb4d7c62015-04-29 04:43:36 +00001356 for (Init *V : Vals) {
1357 TypedInit *TArg = dyn_cast<TypedInit>(V);
Craig Topper011817a2014-04-09 04:50:04 +00001358 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001359 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001360 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001361 }
Craig Topper011817a2014-04-09 04:50:04 +00001362 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001363 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001364 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001365 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001366 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001367 }
Bob Wilson7248f862009-11-22 04:24:42 +00001368 } else {
David Greene8618f952009-06-08 20:23:18 +00001369 EltTy = TArg->getType();
1370 }
1371 }
1372
Craig Topper011817a2014-04-09 04:50:04 +00001373 if (GivenEltTy) {
1374 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001375 // Verify consistency
1376 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1377 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001378 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001379 }
1380 }
1381 EltTy = GivenEltTy;
1382 }
1383
Craig Topper011817a2014-04-09 04:50:04 +00001384 if (!EltTy) {
1385 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001386 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001387 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001388 }
1389 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001390 } else {
David Greene8618f952009-06-08 20:23:18 +00001391 // Make sure the deduced type is compatible with the given type
1392 if (GivenListTy) {
1393 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1394 TokError("Element type mismatch for list");
Craig Topper011817a2014-04-09 04:50:04 +00001395 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001396 }
1397 }
1398 DeducedEltTy = EltTy;
1399 }
Bob Wilson7248f862009-11-22 04:24:42 +00001400
David Greenee32ebf22011-07-29 19:07:07 +00001401 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001402 }
1403 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1404 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001405 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001406 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001407 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001408 }
Bob Wilson7248f862009-11-22 04:24:42 +00001409
David Greeneaf8ee2c2011-07-29 22:43:06 +00001410 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001411 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001412
Nate Begemandbe3f772009-03-19 05:21:56 +00001413 // If the operator name is present, parse it.
1414 std::string OperatorName;
1415 if (Lex.getCode() == tgtok::colon) {
1416 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1417 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001418 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001419 }
1420 OperatorName = Lex.getCurStrVal();
1421 Lex.Lex(); // eat the VarName.
1422 }
Bob Wilson7248f862009-11-22 04:24:42 +00001423
David Greeneaf8ee2c2011-07-29 22:43:06 +00001424 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001425 if (Lex.getCode() != tgtok::r_paren) {
1426 DagArgs = ParseDagArgList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001427 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001428 }
Bob Wilson7248f862009-11-22 04:24:42 +00001429
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001430 if (Lex.getCode() != tgtok::r_paren) {
1431 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001432 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001433 }
1434 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001435
David Greenee32ebf22011-07-29 19:07:07 +00001436 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001437 }
Bob Wilson7248f862009-11-22 04:24:42 +00001438
David Greene2f7cf7f2011-01-07 17:05:37 +00001439 case tgtok::XHead:
1440 case tgtok::XTail:
1441 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001442 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001443 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001444 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001445 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +00001446 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001447 case tgtok::XSRL:
1448 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001449 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001450 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001451 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001452 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001453 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001454 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001455 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001456 }
1457 }
Bob Wilson7248f862009-11-22 04:24:42 +00001458
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001459 return R;
1460}
1461
1462/// ParseValue - Parse a tblgen value. This returns null on error.
1463///
1464/// Value ::= SimpleValue ValueSuffix*
1465/// ValueSuffix ::= '{' BitList '}'
1466/// ValueSuffix ::= '[' BitList ']'
1467/// ValueSuffix ::= '.' ID
1468///
David Greened4263a62011-10-19 13:04:20 +00001469Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1470 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001471 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001472
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001473 // Parse the suffixes now if present.
1474 while (1) {
1475 switch (Lex.getCode()) {
1476 default: return Result;
1477 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001478 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001479 // This is the beginning of the object body.
1480 return Result;
1481
Chris Lattner526c8cb2009-06-21 03:39:35 +00001482 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001483 Lex.Lex(); // eat the '{'
1484 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001485 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001486
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001487 // Reverse the bitlist.
1488 std::reverse(Ranges.begin(), Ranges.end());
1489 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001490 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001491 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001492 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001493 }
Bob Wilson7248f862009-11-22 04:24:42 +00001494
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001495 // Eat the '}'.
1496 if (Lex.getCode() != tgtok::r_brace) {
1497 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001498 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001499 }
1500 Lex.Lex();
1501 break;
1502 }
1503 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001504 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001505 Lex.Lex(); // eat the '['
1506 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001507 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001508
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001509 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001510 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001511 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001512 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001513 }
Bob Wilson7248f862009-11-22 04:24:42 +00001514
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001515 // Eat the ']'.
1516 if (Lex.getCode() != tgtok::r_square) {
1517 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001518 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001519 }
1520 Lex.Lex();
1521 break;
1522 }
1523 case tgtok::period:
1524 if (Lex.Lex() != tgtok::Id) { // eat the .
1525 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001526 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001527 }
1528 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001529 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001530 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001531 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001532 }
David Greenee32ebf22011-07-29 19:07:07 +00001533 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001534 Lex.Lex(); // eat field name
1535 break;
David Greene8e85b482011-10-19 13:04:43 +00001536
1537 case tgtok::paste:
1538 SMLoc PasteLoc = Lex.getLoc();
1539
1540 // Create a !strconcat() operation, first casting each operand to
1541 // a string if necessary.
1542
Sean Silvafb509ed2012-10-10 20:24:43 +00001543 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001544 if (!LHS) {
1545 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001546 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001547 }
Craig Toppera9642b42015-05-04 01:35:39 +00001548
David Greene8e85b482011-10-19 13:04:43 +00001549 if (LHS->getType() != StringRecTy::get()) {
1550 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1551 }
1552
Craig Topper011817a2014-04-09 04:50:04 +00001553 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001554
1555 Lex.Lex(); // Eat the '#'.
1556 switch (Lex.getCode()) {
1557 case tgtok::colon:
1558 case tgtok::semi:
1559 case tgtok::l_brace:
1560 // These are all of the tokens that can begin an object body.
1561 // Some of these can also begin values but we disallow those cases
1562 // because they are unlikely to be useful.
Craig Toppera9642b42015-05-04 01:35:39 +00001563
David Greene8e85b482011-10-19 13:04:43 +00001564 // Trailing paste, concat with an empty string.
1565 RHS = StringInit::get("");
1566 break;
1567
1568 default:
1569 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001570 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001571 if (!RHS) {
1572 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001573 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001574 }
1575
1576 if (RHS->getType() != StringRecTy::get()) {
1577 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1578 }
Craig Toppera9642b42015-05-04 01:35:39 +00001579
David Greene8e85b482011-10-19 13:04:43 +00001580 break;
1581 }
1582
1583 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1584 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1585 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001586 }
1587 }
1588}
1589
1590/// ParseDagArgList - Parse the argument list for a dag literal expression.
1591///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001592/// DagArg ::= Value (':' VARNAME)?
1593/// DagArg ::= VARNAME
1594/// DagArgList ::= DagArg
1595/// DagArgList ::= DagArgList ',' DagArg
David Greeneaf8ee2c2011-07-29 22:43:06 +00001596std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001597TGParser::ParseDagArgList(Record *CurRec) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001598 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson7248f862009-11-22 04:24:42 +00001599
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001600 while (1) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001601 // DagArg ::= VARNAME
1602 if (Lex.getCode() == tgtok::VarName) {
1603 // A missing value is treated like '?'.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00001604 Result.emplace_back(UnsetInit::get(), Lex.getCurStrVal());
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001605 Lex.Lex();
1606 } else {
1607 // DagArg ::= Value (':' VARNAME)?
1608 Init *Val = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001609 if (!Val)
David Greeneaf8ee2c2011-07-29 22:43:06 +00001610 return std::vector<std::pair<llvm::Init*, std::string> >();
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001611
1612 // If the variable name is present, add it.
1613 std::string VarName;
1614 if (Lex.getCode() == tgtok::colon) {
1615 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1616 TokError("expected variable name in dag literal");
1617 return std::vector<std::pair<llvm::Init*, std::string> >();
1618 }
1619 VarName = Lex.getCurStrVal();
1620 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001621 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001622
1623 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001624 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001625 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001626 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001627 }
Bob Wilson7248f862009-11-22 04:24:42 +00001628
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001629 return Result;
1630}
1631
1632
1633/// ParseValueList - Parse a comma separated list of values, returning them as a
1634/// vector. Note that this always expects to be able to parse at least one
1635/// value. It returns an empty list if this is not possible.
1636///
1637/// ValueList ::= Value (',' Value)
1638///
David Greeneaf8ee2c2011-07-29 22:43:06 +00001639std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopher71520a82011-07-11 23:06:52 +00001640 RecTy *EltTy) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001641 std::vector<Init*> Result;
David Greene8618f952009-06-08 20:23:18 +00001642 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001643 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001644 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001645 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001646 if (TArgs.empty()) {
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001647 TokError("template argument provided to non-template class");
1648 return std::vector<Init*>();
1649 }
David Greene8618f952009-06-08 20:23:18 +00001650 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001651 if (!RV) {
1652 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1653 << ")\n";
1654 }
David Greene8618f952009-06-08 20:23:18 +00001655 assert(RV && "Template argument record not found??");
1656 ItemType = RV->getType();
1657 ++ArgN;
1658 }
1659 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001660 if (!Result.back()) return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001661
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001662 while (Lex.getCode() == tgtok::comma) {
1663 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001664
Craig Topper011817a2014-04-09 04:50:04 +00001665 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001666 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001667 if (ArgN >= TArgs.size()) {
1668 TokError("too many template arguments");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001669 return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001670 }
David Greene8618f952009-06-08 20:23:18 +00001671 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1672 assert(RV && "Template argument record not found??");
1673 ItemType = RV->getType();
1674 ++ArgN;
1675 }
1676 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001677 if (!Result.back()) return std::vector<Init*>();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001678 }
Bob Wilson7248f862009-11-22 04:24:42 +00001679
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001680 return Result;
1681}
1682
1683
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001684/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1685/// empty string on error. This can happen in a number of different context's,
1686/// including within a def or in the template args for a def (which which case
1687/// CurRec will be non-null) and within the template args for a multiclass (in
1688/// which case CurRec will be null, but CurMultiClass will be set). This can
1689/// also happen within a def that is within a multiclass, which will set both
1690/// CurRec and CurMultiClass.
1691///
1692/// Declaration ::= FIELD? Type ID ('=' Value)?
1693///
David Greenedb10e692011-10-19 13:02:42 +00001694Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001695 bool ParsingTemplateArgs) {
1696 // Read the field prefix if present.
1697 bool HasField = Lex.getCode() == tgtok::Field;
1698 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001699
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001700 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001701 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001702
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001703 if (Lex.getCode() != tgtok::Id) {
1704 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001705 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001706 }
Bob Wilson7248f862009-11-22 04:24:42 +00001707
Chris Lattner526c8cb2009-06-21 03:39:35 +00001708 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001709 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001710 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001711
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001712 if (ParsingTemplateArgs) {
Craig Toppera9642b42015-05-04 01:35:39 +00001713 if (CurRec)
David Greenedb10e692011-10-19 13:02:42 +00001714 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Craig Toppera9642b42015-05-04 01:35:39 +00001715 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001716 assert(CurMultiClass);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001717 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001718 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1719 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001720 }
Bob Wilson7248f862009-11-22 04:24:42 +00001721
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001722 // Add the value.
1723 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001724 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001725
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001726 // If a value is present, parse it.
1727 if (Lex.getCode() == tgtok::equal) {
1728 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001729 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001730 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001731 if (!Val ||
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001732 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001733 // Return the name, even if an error is thrown. This is so that we can
1734 // continue to make some progress, even without the value having been
1735 // initialized.
1736 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001737 }
Bob Wilson7248f862009-11-22 04:24:42 +00001738
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001739 return DeclName;
1740}
1741
David Greenefb927af2012-02-22 16:09:41 +00001742/// ParseForeachDeclaration - Read a foreach declaration, returning
1743/// the name of the declared object or a NULL Init on error. Return
1744/// the name of the parsed initializer list through ForeachListName.
1745///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001746/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1747/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1748/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001749///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001750VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001751 if (Lex.getCode() != tgtok::Id) {
1752 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001753 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001754 }
1755
1756 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1757 Lex.Lex();
1758
1759 // If a value is present, parse it.
1760 if (Lex.getCode() != tgtok::equal) {
1761 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001762 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001763 }
1764 Lex.Lex(); // Eat the '='
1765
Craig Topper011817a2014-04-09 04:50:04 +00001766 RecTy *IterType = nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001767 std::vector<unsigned> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001768
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001769 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001770 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001771 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001772 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001773 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001774 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001775 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001776 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001777 }
1778 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001779 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001780 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001781 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001782 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001783 }
1784 IterType = ListType->getElementType();
1785 break;
David Greenefb927af2012-02-22 16:09:41 +00001786 }
1787
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001788 case tgtok::IntVal: { // RangePiece.
1789 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001790 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001791 break;
David Greenefb927af2012-02-22 16:09:41 +00001792 }
1793
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001794 case tgtok::l_brace: { // '{' RangeList '}'
1795 Lex.Lex(); // eat the '{'
1796 Ranges = ParseRangeList();
1797 if (Lex.getCode() != tgtok::r_brace) {
1798 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001799 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001800 }
1801 Lex.Lex();
1802 break;
1803 }
1804 }
David Greenefb927af2012-02-22 16:09:41 +00001805
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001806 if (!Ranges.empty()) {
1807 assert(!IterType && "Type already initialized?");
1808 IterType = IntRecTy::get();
1809 std::vector<Init*> Values;
1810 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
1811 Values.push_back(IntInit::get(Ranges[i]));
1812 ForeachListValue = ListInit::get(Values, IterType);
1813 }
1814
1815 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001816 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001817
1818 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001819}
1820
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001821/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1822/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1823/// template args for a def, which may or may not be in a multiclass. If null,
1824/// these are the template args for a multiclass.
1825///
1826/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001827///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001828bool TGParser::ParseTemplateArgList(Record *CurRec) {
1829 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1830 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001831
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001832 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001833
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001834 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001835 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001836 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001837 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001838
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001839 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001840
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001841 while (Lex.getCode() == tgtok::comma) {
1842 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001843
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001844 // Read the following declarations.
1845 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001846 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001847 return true;
1848 TheRecToAddTo->addTemplateArg(TemplArg);
1849 }
Bob Wilson7248f862009-11-22 04:24:42 +00001850
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001851 if (Lex.getCode() != tgtok::greater)
1852 return TokError("expected '>' at end of template argument list");
1853 Lex.Lex(); // eat the '>'.
1854 return false;
1855}
1856
1857
1858/// ParseBodyItem - Parse a single item at within the body of a def or class.
1859///
1860/// BodyItem ::= Declaration ';'
1861/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1862bool TGParser::ParseBodyItem(Record *CurRec) {
1863 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001864 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001865 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001866
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001867 if (Lex.getCode() != tgtok::semi)
1868 return TokError("expected ';' after declaration");
1869 Lex.Lex();
1870 return false;
1871 }
1872
1873 // LET ID OptionalRangeList '=' Value ';'
1874 if (Lex.Lex() != tgtok::Id)
1875 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001876
Chris Lattner526c8cb2009-06-21 03:39:35 +00001877 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001878 std::string FieldName = Lex.getCurStrVal();
1879 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001880
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001881 std::vector<unsigned> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001882 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001883 return true;
1884 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001885
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001886 if (Lex.getCode() != tgtok::equal)
1887 return TokError("expected '=' in let expression");
1888 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001889
David Greene8618f952009-06-08 20:23:18 +00001890 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001891 if (!Field)
David Greene8618f952009-06-08 20:23:18 +00001892 return TokError("Value '" + FieldName + "' unknown!");
1893
1894 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001895
David Greeneaf8ee2c2011-07-29 22:43:06 +00001896 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001897 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001898
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001899 if (Lex.getCode() != tgtok::semi)
1900 return TokError("expected ';' after let expression");
1901 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001902
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001903 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1904}
1905
1906/// ParseBody - Read the body of a class or def. Return true on error, false on
1907/// success.
1908///
1909/// Body ::= ';'
1910/// Body ::= '{' BodyList '}'
1911/// BodyList BodyItem*
1912///
1913bool TGParser::ParseBody(Record *CurRec) {
1914 // If this is a null definition, just eat the semi and return.
1915 if (Lex.getCode() == tgtok::semi) {
1916 Lex.Lex();
1917 return false;
1918 }
Bob Wilson7248f862009-11-22 04:24:42 +00001919
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001920 if (Lex.getCode() != tgtok::l_brace)
1921 return TokError("Expected ';' or '{' to start body");
1922 // Eat the '{'.
1923 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001924
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001925 while (Lex.getCode() != tgtok::r_brace)
1926 if (ParseBodyItem(CurRec))
1927 return true;
1928
1929 // Eat the '}'.
1930 Lex.Lex();
1931 return false;
1932}
1933
Sean Silvacb1a75e2013-01-09 04:49:14 +00001934/// \brief Apply the current let bindings to \a CurRec.
1935/// \returns true on error, false otherwise.
1936bool TGParser::ApplyLetStack(Record *CurRec) {
1937 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1938 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1939 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1940 LetStack[i][j].Bits, LetStack[i][j].Value))
1941 return true;
1942 return false;
1943}
1944
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001945/// ParseObjectBody - Parse the body of a def or class. This consists of an
1946/// optional ClassList followed by a Body. CurRec is the current def or class
1947/// that is being parsed.
1948///
1949/// ObjectBody ::= BaseClassList Body
1950/// BaseClassList ::= /*empty*/
1951/// BaseClassList ::= ':' BaseClassListNE
1952/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1953///
1954bool TGParser::ParseObjectBody(Record *CurRec) {
1955 // If there is a baseclass list, read it.
1956 if (Lex.getCode() == tgtok::colon) {
1957 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001958
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001959 // Read all of the subclasses.
1960 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1961 while (1) {
1962 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00001963 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001964
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001965 // Add it.
1966 if (AddSubClass(CurRec, SubClass))
1967 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001968
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001969 if (Lex.getCode() != tgtok::comma) break;
1970 Lex.Lex(); // eat ','.
1971 SubClass = ParseSubClassReference(CurRec, false);
1972 }
1973 }
1974
Sean Silvacb1a75e2013-01-09 04:49:14 +00001975 if (ApplyLetStack(CurRec))
1976 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001977
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001978 return ParseBody(CurRec);
1979}
1980
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001981/// ParseDef - Parse and return a top level or multiclass def, return the record
1982/// corresponding to it. This returns null on error.
1983///
1984/// DefInst ::= DEF ObjectName ObjectBody
1985///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00001986bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001987 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001988 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00001989 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001990
1991 // Parse ObjectName and make a record for it.
Craig Topper84138712014-11-29 05:31:10 +00001992 std::unique_ptr<Record> CurRecOwner;
Jordan Roseabdd99b2013-01-10 18:50:05 +00001993 Init *Name = ParseObjectName(CurMultiClass);
1994 if (Name)
Craig Topper84138712014-11-29 05:31:10 +00001995 CurRecOwner = make_unique<Record>(Name, DefLoc, Records);
Jordan Roseabdd99b2013-01-10 18:50:05 +00001996 else
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00001997 CurRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), DefLoc,
1998 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001999 Record *CurRec = CurRecOwner.get(); // Keep a copy since we may release.
Bob Wilson7248f862009-11-22 04:24:42 +00002000
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002001 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002002 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002003
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002004 // Ensure redefinition doesn't happen.
Craig Topper84138712014-11-29 05:31:10 +00002005 if (Records.getDef(CurRec->getNameInitAsString()))
2006 return Error(DefLoc, "def '" + CurRec->getNameInitAsString()+
2007 "' already defined");
Craig Toppercdab2322014-11-29 05:52:51 +00002008 Records.addDef(std::move(CurRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00002009
2010 if (ParseObjectBody(CurRec))
2011 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002012 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002013 // Parse the body before adding this prototype to the DefPrototypes vector.
2014 // That way implicit definitions will be added to the DefPrototypes vector
2015 // before this object, instantiated prior to defs derived from this object,
2016 // and this available for indirect name resolution when defs derived from
2017 // this object are instantiated.
Craig Topper84138712014-11-29 05:31:10 +00002018 if (ParseObjectBody(CurRec))
Hal Finkela8c1f462014-01-02 20:47:09 +00002019 return true;
2020
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002021 // Otherwise, a def inside a multiclass, add it to the multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002022 for (const auto &Proto : CurMultiClass->DefPrototypes)
2023 if (Proto->getNameInit() == CurRec->getNameInit())
Craig Topper84138712014-11-29 05:31:10 +00002024 return Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
2025 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002026 CurMultiClass->DefPrototypes.push_back(std::move(CurRecOwner));
Anton Yartsev671dff12014-08-08 00:29:54 +00002027 } else if (ParseObjectBody(CurRec)) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002028 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002029 }
Bob Wilson7248f862009-11-22 04:24:42 +00002030
Craig Topper011817a2014-04-09 04:50:04 +00002031 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002032 // See Record::setName(). This resolve step will see any new name
2033 // for the def that might have been created when resolving
2034 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002035 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002036
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002037 // If ObjectBody has template arguments, it's an error.
2038 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002039
2040 if (CurMultiClass) {
2041 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002042 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
2043 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002044 assert(RV && "Template arg doesn't exist?");
2045 CurRec->addValue(*RV);
2046 }
2047 }
2048
Craig Toppera9642b42015-05-04 01:35:39 +00002049 if (ProcessForeachDefs(CurRec, DefLoc))
Craig Topper84138712014-11-29 05:31:10 +00002050 return Error(DefLoc, "Could not process loops for def" +
2051 CurRec->getNameInitAsString());
David Greenefb927af2012-02-22 16:09:41 +00002052
2053 return false;
2054}
2055
2056/// ParseForeach - Parse a for statement. Return the record corresponding
2057/// to it. This returns true on error.
2058///
2059/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2060/// Foreach ::= FOREACH Declaration IN Object
2061///
2062bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2063 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2064 Lex.Lex(); // Eat the 'for' token.
2065
2066 // Make a temporary object to record items associated with the for
2067 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002068 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002069 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002070 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002071 return TokError("expected declaration in for");
2072
2073 if (Lex.getCode() != tgtok::In)
2074 return TokError("Unknown tok");
2075 Lex.Lex(); // Eat the in
2076
2077 // Create a loop object and remember it.
2078 Loops.push_back(ForeachLoop(IterName, ListValue));
2079
2080 if (Lex.getCode() != tgtok::l_brace) {
2081 // FOREACH Declaration IN Object
2082 if (ParseObject(CurMultiClass))
2083 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002084 } else {
David Greenefb927af2012-02-22 16:09:41 +00002085 SMLoc BraceLoc = Lex.getLoc();
2086 // Otherwise, this is a group foreach.
2087 Lex.Lex(); // eat the '{'.
2088
2089 // Parse the object list.
2090 if (ParseObjectList(CurMultiClass))
2091 return true;
2092
2093 if (Lex.getCode() != tgtok::r_brace) {
2094 TokError("expected '}' at end of foreach command");
2095 return Error(BraceLoc, "to match this '{'");
2096 }
2097 Lex.Lex(); // Eat the }
2098 }
2099
2100 // We've processed everything in this loop.
2101 Loops.pop_back();
2102
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002103 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002104}
2105
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002106/// ParseClass - Parse a tblgen class definition.
2107///
2108/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2109///
2110bool TGParser::ParseClass() {
2111 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2112 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002113
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002114 if (Lex.getCode() != tgtok::Id)
2115 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002116
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002117 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2118 if (CurRec) {
2119 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002120 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002121 !CurRec->getSuperClasses().empty() ||
2122 !CurRec->getTemplateArgs().empty())
Craig Topper85c07002015-04-30 05:54:22 +00002123 return TokError("Class '" + CurRec->getNameInitAsString() +
2124 "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002125 } else {
2126 // If this is the first reference to this class, create and add it.
Hans Wennborgffbbd532014-11-30 00:31:49 +00002127 auto NewRec =
2128 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
Craig Toppercdab2322014-11-29 05:52:51 +00002129 CurRec = NewRec.get();
2130 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002131 }
2132 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002133
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002134 // If there are template args, parse them.
2135 if (Lex.getCode() == tgtok::less)
2136 if (ParseTemplateArgList(CurRec))
2137 return true;
2138
2139 // Finally, parse the object body.
2140 return ParseObjectBody(CurRec);
2141}
2142
2143/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2144/// of LetRecords.
2145///
2146/// LetList ::= LetItem (',' LetItem)*
2147/// LetItem ::= ID OptionalRangeList '=' Value
2148///
2149std::vector<LetRecord> TGParser::ParseLetList() {
2150 std::vector<LetRecord> Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002151
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002152 while (1) {
2153 if (Lex.getCode() != tgtok::Id) {
2154 TokError("expected identifier in let definition");
2155 return std::vector<LetRecord>();
2156 }
2157 std::string Name = Lex.getCurStrVal();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002158 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002159 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002160
2161 // Check for an optional RangeList.
2162 std::vector<unsigned> Bits;
Bob Wilson7248f862009-11-22 04:24:42 +00002163 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002164 return std::vector<LetRecord>();
2165 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002166
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002167 if (Lex.getCode() != tgtok::equal) {
2168 TokError("expected '=' in let expression");
2169 return std::vector<LetRecord>();
2170 }
2171 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002172
Craig Topper011817a2014-04-09 04:50:04 +00002173 Init *Val = ParseValue(nullptr);
2174 if (!Val) return std::vector<LetRecord>();
Bob Wilson7248f862009-11-22 04:24:42 +00002175
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002176 // Now that we have everything, add the record.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00002177 Result.emplace_back(std::move(Name), std::move(Bits), Val, NameLoc);
Bob Wilson7248f862009-11-22 04:24:42 +00002178
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002179 if (Lex.getCode() != tgtok::comma)
2180 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002181 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002182 }
2183}
2184
2185/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002186/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002187///
2188/// Object ::= LET LetList IN '{' ObjectList '}'
2189/// Object ::= LET LetList IN Object
2190///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002191bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002192 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2193 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002194
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002195 // Add this entry to the let stack.
2196 std::vector<LetRecord> LetInfo = ParseLetList();
2197 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002198 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002199
2200 if (Lex.getCode() != tgtok::In)
2201 return TokError("expected 'in' at end of top-level 'let'");
2202 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002203
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002204 // If this is a scalar let, just handle it now
2205 if (Lex.getCode() != tgtok::l_brace) {
2206 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002207 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002208 return true;
2209 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002210 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002211 // Otherwise, this is a group let.
2212 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002213
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002214 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002215 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002216 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002217
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002218 if (Lex.getCode() != tgtok::r_brace) {
2219 TokError("expected '}' at end of top level let command");
2220 return Error(BraceLoc, "to match this '{'");
2221 }
2222 Lex.Lex();
2223 }
Bob Wilson7248f862009-11-22 04:24:42 +00002224
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002225 // Outside this let scope, this let block is not active.
2226 LetStack.pop_back();
2227 return false;
2228}
2229
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002230/// ParseMultiClass - Parse a multiclass definition.
2231///
Bob Wilson3d948162009-04-28 19:41:44 +00002232/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002233/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2234/// MultiClassObject ::= DefInst
2235/// MultiClassObject ::= MultiClassInst
2236/// MultiClassObject ::= DefMInst
2237/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2238/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002239///
2240bool TGParser::ParseMultiClass() {
2241 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2242 Lex.Lex(); // Eat the multiclass token.
2243
2244 if (Lex.getCode() != tgtok::Id)
2245 return TokError("expected identifier after multiclass for name");
2246 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002247
Craig Topper7adf2bf2014-12-11 05:25:30 +00002248 auto Result =
2249 MultiClasses.insert(std::make_pair(Name,
2250 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2251
2252 if (!Result.second)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002253 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002254
Craig Topper7adf2bf2014-12-11 05:25:30 +00002255 CurMultiClass = Result.first->second.get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002256 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002257
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002258 // If there are template args, parse them.
2259 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002260 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002261 return true;
2262
David Greene7049e792009-04-24 16:55:41 +00002263 bool inherits = false;
2264
David Greene753ed8f2009-04-22 16:42:54 +00002265 // If there are submulticlasses, parse them.
2266 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002267 inherits = true;
2268
David Greene753ed8f2009-04-22 16:42:54 +00002269 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002270
David Greene753ed8f2009-04-22 16:42:54 +00002271 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002272 SubMultiClassReference SubMultiClass =
2273 ParseSubMultiClassReference(CurMultiClass);
David Greene753ed8f2009-04-22 16:42:54 +00002274 while (1) {
2275 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002276 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002277
David Greene753ed8f2009-04-22 16:42:54 +00002278 // Add it.
2279 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2280 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002281
David Greene753ed8f2009-04-22 16:42:54 +00002282 if (Lex.getCode() != tgtok::comma) break;
2283 Lex.Lex(); // eat ','.
2284 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2285 }
2286 }
2287
David Greene7049e792009-04-24 16:55:41 +00002288 if (Lex.getCode() != tgtok::l_brace) {
2289 if (!inherits)
2290 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002291 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002292 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002293 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002294 } else {
David Greene7049e792009-04-24 16:55:41 +00002295 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2296 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002297
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002298 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002299 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002300 default:
2301 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
2302 case tgtok::Let:
2303 case tgtok::Def:
2304 case tgtok::Defm:
2305 case tgtok::Foreach:
2306 if (ParseObject(CurMultiClass))
2307 return true;
2308 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002309 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002310 }
David Greene7049e792009-04-24 16:55:41 +00002311 Lex.Lex(); // eat the '}'.
2312 }
Bob Wilson7248f862009-11-22 04:24:42 +00002313
Craig Topper011817a2014-04-09 04:50:04 +00002314 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002315 return false;
2316}
2317
David Greenedb445972011-10-05 22:42:07 +00002318Record *TGParser::
2319InstantiateMulticlassDef(MultiClass &MC,
2320 Record *DefProto,
Hal Finkelf2a0b2b2014-01-02 19:35:33 +00002321 Init *&DefmPrefix,
Hal Finkeld2497362015-05-21 04:32:56 +00002322 SMRange DefmPrefixRange,
2323 const std::vector<Init *> &TArgs,
2324 std::vector<Init *> &TemplateVals) {
David Greene5d5d88c2011-10-19 13:04:31 +00002325 // We need to preserve DefProto so it can be reused for later
2326 // instantiations, so create a new Record to inherit from it.
2327
David Greenedb445972011-10-05 22:42:07 +00002328 // Add in the defm name. If the defm prefix is empty, give each
2329 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2330 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2331 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002332
Jordan Roseabdd99b2013-01-10 18:50:05 +00002333 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002334 if (!DefmPrefix) {
David Greene5d5d88c2011-10-19 13:04:31 +00002335 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002336 IsAnonymous = true;
2337 }
David Greene5d5d88c2011-10-19 13:04:31 +00002338
2339 Init *DefName = DefProto->getNameInit();
Sean Silvafb509ed2012-10-10 20:24:43 +00002340 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002341
Craig Topper011817a2014-04-09 04:50:04 +00002342 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002343 // We have a fully expanded string so there are no operators to
2344 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002345 DefName =
2346 BinOpInit::get(BinOpInit::STRCONCAT,
2347 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2348 StringRecTy::get())->Fold(DefProto, &MC),
2349 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2350 }
David Greene5d5d88c2011-10-19 13:04:31 +00002351
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002352 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002353 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002354 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Craig Topper84138712014-11-29 05:31:10 +00002355 auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002356
2357 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002358 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002359 Ref.Rec = DefProto;
Craig Topper84138712014-11-29 05:31:10 +00002360 AddSubClass(CurRec.get(), Ref);
David Greenedb445972011-10-05 22:42:07 +00002361
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002362 // Set the value for NAME. We don't resolve references to it 'til later,
2363 // though, so that uses in nested multiclass names don't get
2364 // confused.
Craig Topper84138712014-11-29 05:31:10 +00002365 if (SetValue(CurRec.get(), Ref.RefRange.Start, "NAME",
2366 std::vector<unsigned>(), DefmPrefix)) {
Craig Topper85c07002015-04-30 05:54:22 +00002367 Error(DefmPrefixRange.Start, "Could not resolve " +
2368 CurRec->getNameInitAsString() + ":NAME to '" +
2369 DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002370 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002371 }
David Greene8bf0d722011-10-19 13:04:35 +00002372
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002373 // If the DefNameString didn't resolve, we probably have a reference to
2374 // NAME and need to replace it. We need to do at least this much greedily,
2375 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002376 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002377 RecordVal *DefNameRV = CurRec->getValue("NAME");
2378 CurRec->resolveReferencesTo(DefNameRV);
2379 }
2380
2381 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002382 // Now that we're at the top level, resolve all NAME references
2383 // in the resultant defs that weren't in the def names themselves.
2384 RecordVal *DefNameRV = CurRec->getValue("NAME");
2385 CurRec->resolveReferencesTo(DefNameRV);
2386
Hal Finkeld2497362015-05-21 04:32:56 +00002387 // Check if the name is a complex pattern.
2388 // If so, resolve it.
2389 DefName = CurRec->getNameInit();
2390 DefNameString = dyn_cast<StringInit>(DefName);
2391
2392 // OK the pattern is more complex than simply using NAME.
2393 // Let's use the heavy weaponery.
2394 if (!DefNameString) {
2395 ResolveMulticlassDefArgs(MC, CurRec.get(), DefmPrefixRange.Start,
2396 Lex.getLoc(), TArgs, TemplateVals,
2397 false/*Delete args*/);
2398 DefName = CurRec->getNameInit();
2399 DefNameString = dyn_cast<StringInit>(DefName);
2400
2401 if (!DefNameString)
2402 DefName = DefName->convertInitializerTo(StringRecTy::get());
2403
2404 // We ran out of options here...
2405 DefNameString = dyn_cast<StringInit>(DefName);
2406 if (!DefNameString) {
2407 PrintFatalError(CurRec->getLoc()[CurRec->getLoc().size() - 1],
2408 DefName->getAsUnquotedString() + " is not a string.");
2409 return nullptr;
2410 }
2411
2412 CurRec->setName(DefName);
2413 }
2414
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002415 // Now that NAME references are resolved and we're at the top level of
2416 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002417 // currently in a multiclass, it means this defm appears inside a
2418 // multiclass and its name won't be fully resolvable until we see
Hal Finkeld2497362015-05-21 04:32:56 +00002419 // the top-level defm. Therefore, we don't add this to the
2420 // RecordKeeper at this point. If we did we could get duplicate
David Greene8bf0d722011-10-19 13:04:35 +00002421 // defs as more than one probably refers to NAME or some other
2422 // common internal placeholder.
2423
2424 // Ensure redefinition doesn't happen.
2425 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002426 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002427 "' already defined, instantiating defm with subdef '" +
2428 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002429 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002430 }
2431
Craig Topper84138712014-11-29 05:31:10 +00002432 Record *CurRecSave = CurRec.get(); // Keep a copy before we release.
Craig Toppercdab2322014-11-29 05:52:51 +00002433 Records.addDef(std::move(CurRec));
Craig Topper84138712014-11-29 05:31:10 +00002434 return CurRecSave;
David Greene8bf0d722011-10-19 13:04:35 +00002435 }
2436
Craig Topper84138712014-11-29 05:31:10 +00002437 // FIXME This is bad but the ownership transfer to caller is pretty messy.
2438 // The unique_ptr in this function at least protects the exits above.
2439 return CurRec.release();
David Greenedb445972011-10-05 22:42:07 +00002440}
2441
2442bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2443 Record *CurRec,
2444 SMLoc DefmPrefixLoc,
2445 SMLoc SubClassLoc,
David Greenedb10e692011-10-19 13:02:42 +00002446 const std::vector<Init *> &TArgs,
David Greenedb445972011-10-05 22:42:07 +00002447 std::vector<Init *> &TemplateVals,
2448 bool DeleteArgs) {
2449 // Loop over all of the template arguments, setting them to the specified
2450 // value or leaving them as the default if necessary.
2451 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2452 // Check if a value is specified for this temp-arg.
2453 if (i < TemplateVals.size()) {
2454 // Set it now.
2455 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2456 TemplateVals[i]))
2457 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002458
David Greenedb445972011-10-05 22:42:07 +00002459 // Resolve it next.
2460 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2461
2462 if (DeleteArgs)
2463 // Now remove it.
2464 CurRec->removeValue(TArgs[i]);
Craig Toppera9642b42015-05-04 01:35:39 +00002465
David Greenedb445972011-10-05 22:42:07 +00002466 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Craig Topper85c07002015-04-30 05:54:22 +00002467 return Error(SubClassLoc, "value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00002468 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +00002469 ") of multiclassclass '" + MC.Rec.getNameInitAsString() +
2470 "'");
David Greenedb445972011-10-05 22:42:07 +00002471 }
2472 }
2473 return false;
2474}
2475
2476bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2477 Record *CurRec,
2478 Record *DefProto,
2479 SMLoc DefmPrefixLoc) {
2480 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002481 if (ApplyLetStack(CurRec))
2482 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002483
David Greenedb445972011-10-05 22:42:07 +00002484 // Don't create a top level definition for defm inside multiclasses,
2485 // instead, only update the prototypes and bind the template args
2486 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002487 if (!CurMultiClass)
2488 return false;
Craig Toppereb4d7c62015-04-29 04:43:36 +00002489 for (const auto &Proto : CurMultiClass->DefPrototypes)
2490 if (Proto->getNameInit() == CurRec->getNameInit())
Sean Silvacc951b22013-01-09 05:28:12 +00002491 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2492 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002493 CurMultiClass->DefPrototypes.push_back(std::unique_ptr<Record>(CurRec));
David Greenedb445972011-10-05 22:42:07 +00002494
Sean Silvacc951b22013-01-09 05:28:12 +00002495 // Copy the template arguments for the multiclass into the new def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002496 for (Init * TA : CurMultiClass->Rec.getTemplateArgs()) {
2497 const RecordVal *RV = CurMultiClass->Rec.getValue(TA);
Sean Silvacc951b22013-01-09 05:28:12 +00002498 assert(RV && "Template arg doesn't exist?");
2499 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002500 }
2501
2502 return false;
2503}
2504
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002505/// ParseDefm - Parse the instantiation of a multiclass.
2506///
2507/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2508///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002509bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002510 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002511 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002512 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002513
Craig Topperb21afc62013-01-07 05:09:33 +00002514 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002515 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002516 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002517
Jordan Rosef12e8a92013-01-10 18:50:11 +00002518 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002519 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002520 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002521
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002522 // Keep track of the new generated record definitions.
2523 std::vector<Record*> NewRecDefs;
2524
2525 // This record also inherits from a regular class (non-multiclass)?
2526 bool InheritFromClass = false;
2527
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002528 // eat the colon.
2529 Lex.Lex();
2530
Chris Lattner526c8cb2009-06-21 03:39:35 +00002531 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002532 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002533
2534 while (1) {
Craig Topper011817a2014-04-09 04:50:04 +00002535 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002536
2537 // To instantiate a multiclass, we need to first get the multiclass, then
2538 // instantiate each def contained in the multiclass with the SubClassRef
2539 // template parameters.
Craig Topper7adf2bf2014-12-11 05:25:30 +00002540 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper4ca40012014-11-30 01:20:17 +00002541 assert(MC && "Didn't lookup multiclass correctly?");
David Greeneaf8ee2c2011-07-29 22:43:06 +00002542 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002543
2544 // Verify that the correct number of template arguments were specified.
David Greenedb10e692011-10-19 13:02:42 +00002545 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002546 if (TArgs.size() < TemplateVals.size())
2547 return Error(SubClassLoc,
2548 "more template args specified than multiclass expects");
2549
2550 // Loop over all the def's in the multiclass, instantiating each one.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002551 for (const std::unique_ptr<Record> &DefProto : MC->DefPrototypes) {
Hal Finkeld2497362015-05-21 04:32:56 +00002552 // The record name construction goes as follow:
2553 // - If the def name is a string, prepend the prefix.
2554 // - If the def name is a more complex pattern, use that pattern.
2555 // As a result, the record is instanciated before resolving
2556 // arguments, as it would make its name a string.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002557 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto.get(), DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002558 SMRange(DefmLoc,
Hal Finkeld2497362015-05-21 04:32:56 +00002559 DefmPrefixEndLoc),
2560 TArgs, TemplateVals);
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002561 if (!CurRec)
2562 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002563
Hal Finkeld2497362015-05-21 04:32:56 +00002564 // Now that the record is instanciated, we can resolve arguments.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002565 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002566 TArgs, TemplateVals, true/*Delete args*/))
2567 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002568
Craig Toppereb4d7c62015-04-29 04:43:36 +00002569 if (ResolveMulticlassDef(*MC, CurRec, DefProto.get(), DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002570 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002571
Adam Nemete5a07162014-09-16 17:14:13 +00002572 // Defs that can be used by other definitions should be fully resolved
2573 // before any use.
2574 if (DefProto->isResolveFirst() && !CurMultiClass) {
2575 CurRec->resolveReferences();
2576 CurRec->setResolveFirst(false);
2577 }
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002578 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002579 }
2580
David Greenedb445972011-10-05 22:42:07 +00002581
David Greenef00919a2009-04-22 22:17:51 +00002582 if (Lex.getCode() != tgtok::comma) break;
2583 Lex.Lex(); // eat ','.
2584
Craig Topper998a39a2013-08-20 04:22:09 +00002585 if (Lex.getCode() != tgtok::Id)
2586 return TokError("expected identifier");
2587
David Greenef00919a2009-04-22 22:17:51 +00002588 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002589
2590 // A defm can inherit from regular classes (non-multiclass) as
2591 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002592 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002593
2594 if (InheritFromClass)
2595 break;
2596
Craig Topper011817a2014-04-09 04:50:04 +00002597 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002598 }
2599
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002600 if (InheritFromClass) {
2601 // Process all the classes to inherit as if they were part of a
2602 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002603 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002604 while (1) {
2605 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002606 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002607
2608 // Get the expanded definition prototypes and teach them about
2609 // the record values the current class to inherit has
Craig Toppereb4d7c62015-04-29 04:43:36 +00002610 for (Record *CurRec : NewRecDefs) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002611 // Add it.
2612 if (AddSubClass(CurRec, SubClass))
2613 return true;
2614
Sean Silvacb1a75e2013-01-09 04:49:14 +00002615 if (ApplyLetStack(CurRec))
2616 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002617 }
2618
2619 if (Lex.getCode() != tgtok::comma) break;
2620 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002621 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002622 }
2623 }
2624
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002625 if (!CurMultiClass)
Craig Toppereb4d7c62015-04-29 04:43:36 +00002626 for (Record *CurRec : NewRecDefs)
David Greene50c09122011-08-10 18:27:46 +00002627 // See Record::setName(). This resolve step will see any new
2628 // name for the def that might have been created when resolving
2629 // inheritance, values and arguments above.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002630 CurRec->resolveReferences();
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002631
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002632 if (Lex.getCode() != tgtok::semi)
2633 return TokError("expected ';' at end of defm");
2634 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002635
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002636 return false;
2637}
2638
2639/// ParseObject
2640/// Object ::= ClassInst
2641/// Object ::= DefInst
2642/// Object ::= MultiClassInst
2643/// Object ::= DefMInst
2644/// Object ::= LETCommand '{' ObjectList '}'
2645/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002646bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002647 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002648 default:
2649 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002650 case tgtok::Let: return ParseTopLevelLet(MC);
2651 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002652 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002653 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002654 case tgtok::Class: return ParseClass();
2655 case tgtok::MultiClass: return ParseMultiClass();
2656 }
2657}
2658
2659/// ParseObjectList
2660/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002661bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002662 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002663 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002664 return true;
2665 }
2666 return false;
2667}
2668
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002669bool TGParser::ParseFile() {
2670 Lex.Lex(); // Prime the lexer.
2671 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002672
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002673 // If we have unread input at the end of the file, report it.
2674 if (Lex.getCode() == tgtok::Eof)
2675 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002676
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002677 return TokError("Unexpected input at top level");
2678}
2679