blob: 1efff04e1e2a96c2c11d6ea53af1d66b965ee871 [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 Toppereb4d7c62015-04-29 04:43:36 +000054 for (Init *TA : TemplateArgs) {
55 TA->dump();
David Greene7049e792009-04-24 16:55:41 +000056 }
57}
58
Chris Lattnerf4127dd2007-11-22 20:49:04 +000059} // end namespace llvm
60
Chris Lattner526c8cb2009-06-21 03:39:35 +000061bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Craig Topper011817a2014-04-09 04:50:04 +000062 if (!CurRec)
Chris Lattnerf4127dd2007-11-22 20:49:04 +000063 CurRec = &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +000064
Jakob Stoklund Olesen9d1c5ee2012-01-13 03:16:35 +000065 if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000066 // The value already exists in the class, treat this as a set.
67 if (ERV->setValue(RV.getValue()))
68 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
69 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson7248f862009-11-22 04:24:42 +000070 "previous definition of type '" +
Chris Lattnerf4127dd2007-11-22 20:49:04 +000071 ERV->getType()->getAsString() + "'");
72 } else {
73 CurRec->addValue(RV);
74 }
75 return false;
76}
77
78/// SetValue -
79/// Return true on error, false on success.
David Greene3ca42122011-10-19 13:02:39 +000080bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
David Greeneaf8ee2c2011-07-29 22:43:06 +000081 const std::vector<unsigned> &BitList, Init *V) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000082 if (!V) return false;
83
Craig Topper011817a2014-04-09 04:50:04 +000084 if (!CurRec) CurRec = &CurMultiClass->Rec;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000085
86 RecordVal *RV = CurRec->getValue(ValName);
Craig Topper011817a2014-04-09 04:50:04 +000087 if (!RV)
Craig Topper85c07002015-04-30 05:54:22 +000088 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
89 "' unknown!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +000090
91 // Do not allow assignments like 'X = X'. This will just cause infinite loops
92 // in the resolution machinery.
93 if (BitList.empty())
Sean Silvafb509ed2012-10-10 20:24:43 +000094 if (VarInit *VI = dyn_cast<VarInit>(V))
David Greene3ca42122011-10-19 13:02:39 +000095 if (VI->getNameInit() == ValName)
Chris Lattnerf4127dd2007-11-22 20:49:04 +000096 return false;
Bob Wilson7248f862009-11-22 04:24:42 +000097
Chris Lattnerf4127dd2007-11-22 20:49:04 +000098 // If we are assigning to a subset of the bits in the value... then we must be
99 // assigning to a field of BitsRecTy, which must have a BitsInit
100 // initializer.
101 //
102 if (!BitList.empty()) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000103 BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue());
Craig Topper011817a2014-04-09 04:50:04 +0000104 if (!CurVal)
Craig Topper85c07002015-04-30 05:54:22 +0000105 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
106 "' is not a bits type");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000107
108 // Convert the incoming value to a bits type of the appropriate size...
David Greeneaf8ee2c2011-07-29 22:43:06 +0000109 Init *BI = V->convertInitializerTo(BitsRecTy::get(BitList.size()));
Craig Topper011817a2014-04-09 04:50:04 +0000110 if (!BI) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000111 return Error(Loc, "Initializer is not compatible with bit range");
112 }
Bob Wilson7248f862009-11-22 04:24:42 +0000113
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000114 // We should have a BitsInit type now.
Craig Toppered5a9502015-04-29 07:13:05 +0000115 BitsInit *BInit = cast<BitsInit>(BI);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000116
David Greeneaf8ee2c2011-07-29 22:43:06 +0000117 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000118
119 // Loop over bits, assigning values as appropriate.
120 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
121 unsigned Bit = BitList[i];
David Greeneb3da8122011-07-29 19:07:00 +0000122 if (NewBits[Bit])
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000123 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
David Greene3ca42122011-10-19 13:02:39 +0000124 ValName->getAsUnquotedString() + "' more than once");
David Greeneb3da8122011-07-29 19:07:00 +0000125 NewBits[Bit] = BInit->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000126 }
127
128 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
Craig Topper011817a2014-04-09 04:50:04 +0000129 if (!NewBits[i])
David Greeneb3da8122011-07-29 19:07:00 +0000130 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000131
David Greenee32ebf22011-07-29 19:07:07 +0000132 V = BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000133 }
134
Pete Cooper040c6a62014-07-31 01:43:57 +0000135 if (RV->setValue(V)) {
136 std::string InitType = "";
137 if (BitsInit *BI = dyn_cast<BitsInit>(V)) {
138 InitType = (Twine("' of type bit initializer with length ") +
139 Twine(BI->getNumBits())).str();
140 }
Craig Topper85c07002015-04-30 05:54:22 +0000141 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
142 "' of type '" + RV->getType()->getAsString() +
143 "' is incompatible with initializer '" + V->getAsString() +
144 InitType + "'");
Pete Cooper040c6a62014-07-31 01:43:57 +0000145 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000146 return false;
147}
148
149/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
150/// args as SubClass's template arguments.
Cedric Venetd1e179d2009-02-14 16:06:42 +0000151bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000152 Record *SC = SubClass.Rec;
153 // Add all of the values in the subclass into the current class.
154 const std::vector<RecordVal> &Vals = SC->getValues();
155 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
Jordan Rosef12e8a92013-01-10 18:50:11 +0000156 if (AddValue(CurRec, SubClass.RefRange.Start, Vals[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000157 return true;
158
David Greenedb10e692011-10-19 13:02:42 +0000159 const std::vector<Init *> &TArgs = SC->getTemplateArgs();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000160
161 // Ensure that an appropriate number of template arguments are specified.
162 if (TArgs.size() < SubClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000163 return Error(SubClass.RefRange.Start,
164 "More template args specified than expected");
Bob Wilson7248f862009-11-22 04:24:42 +0000165
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000166 // Loop over all of the template arguments, setting them to the specified
167 // value or leaving them as the default if necessary.
168 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
169 if (i < SubClass.TemplateArgs.size()) {
170 // If a value is specified for this template arg, set it now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000171 if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
172 std::vector<unsigned>(), SubClass.TemplateArgs[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000173 return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000174
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000175 // Resolve it next.
176 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson7248f862009-11-22 04:24:42 +0000177
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000178 // Now remove it.
179 CurRec->removeValue(TArgs[i]);
180
181 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000182 return Error(SubClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000183 "Value not specified for template argument #" +
184 utostr(i) + " (" + TArgs[i]->getAsUnquotedString() +
185 ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000186 }
187 }
188
189 // Since everything went well, we can now set the "superclass" list for the
190 // current record.
191 const std::vector<Record*> &SCs = SC->getSuperClasses();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000192 ArrayRef<SMRange> SCRanges = SC->getSuperClassRanges();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000193 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
194 if (CurRec->isSubClassOf(SCs[i]))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000195 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000196 "Already subclass of '" + SCs[i]->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000197 CurRec->addSuperClass(SCs[i], SCRanges[i]);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000198 }
Bob Wilson7248f862009-11-22 04:24:42 +0000199
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000200 if (CurRec->isSubClassOf(SC))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000201 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000202 "Already subclass of '" + SC->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000203 CurRec->addSuperClass(SC, SubClass.RefRange);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000204 return false;
205}
206
David Greene753ed8f2009-04-22 16:42:54 +0000207/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilsonf71e6562009-04-30 18:26:19 +0000208/// CurMC, resolving its template args as SubMultiClass's
David Greene753ed8f2009-04-22 16:42:54 +0000209/// template arguments.
Bob Wilsonf71e6562009-04-30 18:26:19 +0000210bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson92ab8202009-04-30 17:46:20 +0000211 SubMultiClassReference &SubMultiClass) {
David Greene753ed8f2009-04-22 16:42:54 +0000212 MultiClass *SMC = SubMultiClass.MC;
Bob Wilsonf71e6562009-04-30 18:26:19 +0000213 Record *CurRec = &CurMC->Rec;
David Greene753ed8f2009-04-22 16:42:54 +0000214
David Greene753ed8f2009-04-22 16:42:54 +0000215 // Add all of the values in the subclass into the current class.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000216 for (const auto &SMCVal : SMC->Rec.getValues())
217 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000218 return true;
219
Craig Toppera4ea4b02014-11-30 00:24:32 +0000220 unsigned newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000221
David Greene753ed8f2009-04-22 16:42:54 +0000222 // Add all of the defs in the subclass into the current multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000223 for (const std::unique_ptr<Record> &R : SMC->DefPrototypes) {
David Greene753ed8f2009-04-22 16:42:54 +0000224 // Clone the def and add it to the current multiclass
Craig Toppereb4d7c62015-04-29 04:43:36 +0000225 auto NewDef = make_unique<Record>(*R);
David Greene753ed8f2009-04-22 16:42:54 +0000226
227 // Add all of the values in the superclass into the current def.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000228 for (const auto &MCVal : CurRec->getValues())
229 if (AddValue(NewDef.get(), SubMultiClass.RefRange.Start, MCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000230 return true;
231
Craig Topperc3504c42014-12-11 05:25:33 +0000232 CurMC->DefPrototypes.push_back(std::move(NewDef));
David Greene753ed8f2009-04-22 16:42:54 +0000233 }
Bob Wilson3d948162009-04-28 19:41:44 +0000234
David Greenedb10e692011-10-19 13:02:42 +0000235 const std::vector<Init *> &SMCTArgs = SMC->Rec.getTemplateArgs();
David Greene753ed8f2009-04-22 16:42:54 +0000236
David Greene7049e792009-04-24 16:55:41 +0000237 // Ensure that an appropriate number of template arguments are
238 // specified.
David Greene753ed8f2009-04-22 16:42:54 +0000239 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000240 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000241 "More template args specified than expected");
Bob Wilson3d948162009-04-28 19:41:44 +0000242
David Greene753ed8f2009-04-22 16:42:54 +0000243 // Loop over all of the template arguments, setting them to the specified
244 // value or leaving them as the default if necessary.
245 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
246 if (i < SubMultiClass.TemplateArgs.size()) {
David Greene7049e792009-04-24 16:55:41 +0000247 // If a value is specified for this template arg, set it in the
248 // superclass now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000249 if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson3d948162009-04-28 19:41:44 +0000250 std::vector<unsigned>(),
David Greene753ed8f2009-04-22 16:42:54 +0000251 SubMultiClass.TemplateArgs[i]))
252 return true;
253
254 // Resolve it next.
255 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson3d948162009-04-28 19:41:44 +0000256
David Greene753ed8f2009-04-22 16:42:54 +0000257 // Now remove it.
258 CurRec->removeValue(SMCTArgs[i]);
259
David Greene7049e792009-04-24 16:55:41 +0000260 // If a value is specified for this template arg, set it in the
261 // new defs now.
Craig Topperc3504c42014-12-11 05:25:33 +0000262 for (const auto &Def :
263 makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
264 if (SetValue(Def.get(), SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson3d948162009-04-28 19:41:44 +0000265 std::vector<unsigned>(),
David Greene753ed8f2009-04-22 16:42:54 +0000266 SubMultiClass.TemplateArgs[i]))
267 return true;
268
269 // Resolve it next.
270 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
271
272 // Now remove it
273 Def->removeValue(SMCTArgs[i]);
274 }
275 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000276 return Error(SubMultiClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000277 "Value not specified for template argument #" +
278 utostr(i) + " (" + SMCTArgs[i]->getAsUnquotedString() +
279 ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000280 }
281 }
282
283 return false;
284}
285
David Greenefb927af2012-02-22 16:09:41 +0000286/// ProcessForeachDefs - Given a record, apply all of the variable
287/// values in all surrounding foreach loops, creating new records for
288/// each combination of values.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000289bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
290 if (Loops.empty())
291 return false;
292
David Greenefb927af2012-02-22 16:09:41 +0000293 // We want to instantiate a new copy of CurRec for each combination
294 // of nested loop iterator values. We don't want top instantiate
295 // any copies until we have values for each loop iterator.
296 IterSet IterVals;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000297 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000298}
299
300/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
301/// apply each of the variable values in this loop and then process
302/// subloops.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000303bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
304 // Recursively build a tuple of iterator values.
305 if (IterVals.size() != Loops.size()) {
306 assert(IterVals.size() < Loops.size());
307 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silvafb509ed2012-10-10 20:24:43 +0000308 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Craig Topper011817a2014-04-09 04:50:04 +0000309 if (!List) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000310 Error(Loc, "Loop list is not a list");
311 return true;
312 }
David Greenefb927af2012-02-22 16:09:41 +0000313
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000314 // Process each value.
315 for (int64_t i = 0; i < List->getSize(); ++i) {
Craig Topper011817a2014-04-09 04:50:04 +0000316 Init *ItemVal = List->resolveListElementReference(*CurRec, nullptr, i);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000317 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
318 if (ProcessForeachDefs(CurRec, Loc, IterVals))
319 return true;
320 IterVals.pop_back();
321 }
322 return false;
323 }
324
325 // This is the bottom of the recursion. We have all of the iterator values
326 // for this point in the iteration space. Instantiate a new record to
327 // reflect this combination of values.
Craig Topper84138712014-11-29 05:31:10 +0000328 auto IterRec = make_unique<Record>(*CurRec);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000329
330 // Set the iterator values now.
331 for (unsigned i = 0, e = IterVals.size(); i != e; ++i) {
332 VarInit *IterVar = IterVals[i].IterVar;
Sean Silvafb509ed2012-10-10 20:24:43 +0000333 TypedInit *IVal = dyn_cast<TypedInit>(IterVals[i].IterValue);
Craig Topper84138712014-11-29 05:31:10 +0000334 if (!IVal)
335 return Error(Loc, "foreach iterator value is untyped");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000336
337 IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false));
338
Craig Topper84138712014-11-29 05:31:10 +0000339 if (SetValue(IterRec.get(), Loc, IterVar->getName(),
340 std::vector<unsigned>(), IVal))
341 return Error(Loc, "when instantiating this def");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000342
343 // Resolve it next.
344 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName()));
345
346 // Remove it.
347 IterRec->removeValue(IterVar->getName());
348 }
349
350 if (Records.getDef(IterRec->getNameInitAsString())) {
Artyom Skrobov8b985322014-06-10 12:41:14 +0000351 // If this record is anonymous, it's no problem, just generate a new name
Craig Topper84138712014-11-29 05:31:10 +0000352 if (!IterRec->isAnonymous())
353 return Error(Loc, "def already exists: " +IterRec->getNameInitAsString());
354
355 IterRec->setName(GetNewAnonymousName());
David Greenefb927af2012-02-22 16:09:41 +0000356 }
357
Craig Topper84138712014-11-29 05:31:10 +0000358 Record *IterRecSave = IterRec.get(); // Keep a copy before release.
Craig Toppercdab2322014-11-29 05:52:51 +0000359 Records.addDef(std::move(IterRec));
Craig Topper84138712014-11-29 05:31:10 +0000360 IterRecSave->resolveReferences();
David Greenefb927af2012-02-22 16:09:41 +0000361 return false;
362}
363
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000364//===----------------------------------------------------------------------===//
365// Parser Code
366//===----------------------------------------------------------------------===//
367
368/// isObjectStart - Return true if this is a valid first token for an Object.
369static bool isObjectStart(tgtok::TokKind K) {
370 return K == tgtok::Class || K == tgtok::Def ||
David Greenefb927af2012-02-22 16:09:41 +0000371 K == tgtok::Defm || K == tgtok::Let ||
372 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000373}
374
Alp Tokerce91fe52013-12-21 18:51:00 +0000375/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
376/// an identifier.
377std::string TGParser::GetNewAnonymousName() {
Aaron Ballman97a59fb2015-02-16 19:33:36 +0000378 return "anonymous_" + utostr(AnonCounter++);
Chris Lattner7538ed82010-10-05 22:51:56 +0000379}
380
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000381/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000382/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000383/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000384/// ObjectName ::= /*empty*/
385///
David Greene2affd672011-10-19 13:04:29 +0000386Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
387 switch (Lex.getCode()) {
388 case tgtok::colon:
389 case tgtok::semi:
390 case tgtok::l_brace:
391 // These are all of the tokens that can begin an object body.
392 // Some of these can also begin values but we disallow those cases
393 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000394 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000395 default:
396 break;
397 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000398
Craig Topper011817a2014-04-09 04:50:04 +0000399 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000400 if (CurMultiClass)
401 CurRec = &CurMultiClass->Rec;
402
Craig Topper011817a2014-04-09 04:50:04 +0000403 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000404 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000405 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000406 if (!CurRecName) {
407 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000408 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000409 }
410 Type = CurRecName->getType();
411 }
412
413 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000414}
415
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000416/// ParseClassID - Parse and resolve a reference to a class name. This returns
417/// null on error.
418///
419/// ClassID ::= ID
420///
421Record *TGParser::ParseClassID() {
422 if (Lex.getCode() != tgtok::Id) {
423 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000424 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000425 }
Bob Wilson7248f862009-11-22 04:24:42 +0000426
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000427 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000428 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000429 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000430
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000431 Lex.Lex();
432 return Result;
433}
434
Bob Wilson3d948162009-04-28 19:41:44 +0000435/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
436/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000437///
438/// MultiClassID ::= ID
439///
440MultiClass *TGParser::ParseMultiClassID() {
441 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000442 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000443 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000444 }
Bob Wilson3d948162009-04-28 19:41:44 +0000445
Craig Topper7adf2bf2014-12-11 05:25:30 +0000446 MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
Craig Topper4ca40012014-11-30 01:20:17 +0000447 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000448 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000449
David Greene753ed8f2009-04-22 16:42:54 +0000450 Lex.Lex();
Craig Topper4ca40012014-11-30 01:20:17 +0000451 return Result;
David Greene753ed8f2009-04-22 16:42:54 +0000452}
453
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000454/// ParseSubClassReference - Parse a reference to a subclass or to a templated
455/// subclass. This returns a SubClassRefTy with a null Record* on error.
456///
457/// SubClassRef ::= ClassID
458/// SubClassRef ::= ClassID '<' ValueList '>'
459///
460SubClassReference TGParser::
461ParseSubClassReference(Record *CurRec, bool isDefm) {
462 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000463 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000464
Sean Silva0657b402013-01-09 02:17:14 +0000465 if (isDefm) {
466 if (MultiClass *MC = ParseMultiClassID())
467 Result.Rec = &MC->Rec;
468 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000469 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000470 }
Craig Topper011817a2014-04-09 04:50:04 +0000471 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000472
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000473 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000474 if (Lex.getCode() != tgtok::less) {
475 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000476 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000477 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000478 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000479
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000480 if (Lex.getCode() == tgtok::greater) {
481 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000482 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000483 return Result;
484 }
Bob Wilson7248f862009-11-22 04:24:42 +0000485
David Greene8618f952009-06-08 20:23:18 +0000486 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000487 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000488 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000489 return Result;
490 }
Bob Wilson7248f862009-11-22 04:24:42 +0000491
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000492 if (Lex.getCode() != tgtok::greater) {
493 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000494 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000495 return Result;
496 }
497 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000498 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000499
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000500 return Result;
501}
502
Bob Wilson3d948162009-04-28 19:41:44 +0000503/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
504/// templated submulticlass. This returns a SubMultiClassRefTy with a null
505/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000506///
507/// SubMultiClassRef ::= MultiClassID
508/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
509///
510SubMultiClassReference TGParser::
511ParseSubMultiClassReference(MultiClass *CurMC) {
512 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000513 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000514
David Greene753ed8f2009-04-22 16:42:54 +0000515 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000516 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000517
David Greene753ed8f2009-04-22 16:42:54 +0000518 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000519 if (Lex.getCode() != tgtok::less) {
520 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000521 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000522 }
David Greene753ed8f2009-04-22 16:42:54 +0000523 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000524
David Greene753ed8f2009-04-22 16:42:54 +0000525 if (Lex.getCode() == tgtok::greater) {
526 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000527 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000528 return Result;
529 }
Bob Wilson3d948162009-04-28 19:41:44 +0000530
David Greene8618f952009-06-08 20:23:18 +0000531 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000532 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000533 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000534 return Result;
535 }
Bob Wilson3d948162009-04-28 19:41:44 +0000536
David Greene753ed8f2009-04-22 16:42:54 +0000537 if (Lex.getCode() != tgtok::greater) {
538 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000539 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000540 return Result;
541 }
542 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000543 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000544
545 return Result;
546}
547
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000548/// ParseRangePiece - Parse a bit/value range.
549/// RangePiece ::= INTVAL
550/// RangePiece ::= INTVAL '-' INTVAL
551/// RangePiece ::= INTVAL INTVAL
552bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000553 if (Lex.getCode() != tgtok::IntVal) {
554 TokError("expected integer or bitrange");
555 return true;
556 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000557 int64_t Start = Lex.getCurIntVal();
558 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000559
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000560 if (Start < 0)
561 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000562
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000563 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000564 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000565 Ranges.push_back(Start);
566 return false;
567 case tgtok::minus:
568 if (Lex.Lex() != tgtok::IntVal) {
569 TokError("expected integer value as end of range");
570 return true;
571 }
572 End = Lex.getCurIntVal();
573 break;
574 case tgtok::IntVal:
575 End = -Lex.getCurIntVal();
576 break;
577 }
Bob Wilson7248f862009-11-22 04:24:42 +0000578 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000579 return TokError("invalid range, cannot be negative");
580 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000581
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000582 // Add to the range.
583 if (Start < End) {
584 for (; Start <= End; ++Start)
585 Ranges.push_back(Start);
586 } else {
587 for (; Start >= End; --Start)
588 Ranges.push_back(Start);
589 }
590 return false;
591}
592
593/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
594///
595/// RangeList ::= RangePiece (',' RangePiece)*
596///
597std::vector<unsigned> TGParser::ParseRangeList() {
598 std::vector<unsigned> Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000599
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000600 // Parse the first piece.
601 if (ParseRangePiece(Result))
602 return std::vector<unsigned>();
603 while (Lex.getCode() == tgtok::comma) {
604 Lex.Lex(); // Eat the comma.
605
606 // Parse the next range piece.
607 if (ParseRangePiece(Result))
608 return std::vector<unsigned>();
609 }
610 return Result;
611}
612
613/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
614/// OptionalRangeList ::= '<' RangeList '>'
615/// OptionalRangeList ::= /*empty*/
616bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
617 if (Lex.getCode() != tgtok::less)
618 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000619
Chris Lattner526c8cb2009-06-21 03:39:35 +0000620 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000621 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000622
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000623 // Parse the range list.
624 Ranges = ParseRangeList();
625 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000626
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000627 if (Lex.getCode() != tgtok::greater) {
628 TokError("expected '>' at end of range list");
629 return Error(StartLoc, "to match this '<'");
630 }
631 Lex.Lex(); // eat the '>'.
632 return false;
633}
634
635/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
636/// OptionalBitList ::= '{' RangeList '}'
637/// OptionalBitList ::= /*empty*/
638bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
639 if (Lex.getCode() != tgtok::l_brace)
640 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000641
Chris Lattner526c8cb2009-06-21 03:39:35 +0000642 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000643 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000644
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000645 // Parse the range list.
646 Ranges = ParseRangeList();
647 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000648
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000649 if (Lex.getCode() != tgtok::r_brace) {
650 TokError("expected '}' at end of bit list");
651 return Error(StartLoc, "to match this '{'");
652 }
653 Lex.Lex(); // eat the '}'.
654 return false;
655}
656
657
658/// ParseType - Parse and return a tblgen type. This returns null on error.
659///
660/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000661/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000662/// Type ::= BIT // bit type
663/// Type ::= BITS '<' INTVAL '>' // bits<x> type
664/// Type ::= INT // int type
665/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000666/// Type ::= DAG // dag type
667/// Type ::= ClassID // Record Type
668///
669RecTy *TGParser::ParseType() {
670 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000671 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000672 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000673 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000674 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
675 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000676 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000677 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000678 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000679 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000680 case tgtok::Bits: {
681 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
682 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000683 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000684 }
685 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
686 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000687 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000688 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000689 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000690 if (Lex.Lex() != tgtok::greater) { // Eat count.
691 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000692 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000693 }
694 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000695 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000696 }
697 case tgtok::List: {
698 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
699 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000700 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000701 }
702 Lex.Lex(); // Eat '<'
703 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000704 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000705
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000706 if (Lex.getCode() != tgtok::greater) {
707 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000708 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000709 }
710 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000711 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000712 }
Bob Wilson7248f862009-11-22 04:24:42 +0000713 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000714}
715
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000716/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
717/// has already been read.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000718Init *TGParser::ParseIDValue(Record *CurRec,
David Greened4263a62011-10-19 13:04:20 +0000719 const std::string &Name, SMLoc NameLoc,
720 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000721 if (CurRec) {
722 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000723 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000724
David Greenedb10e692011-10-19 13:02:42 +0000725 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
726
David Greene47a665e2011-10-05 22:42:54 +0000727 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +0000728 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
729 "::");
David Greene47a665e2011-10-05 22:42:54 +0000730
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000731 if (CurRec->isTemplateArg(TemplateArgName)) {
732 const RecordVal *RV = CurRec->getValue(TemplateArgName);
733 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000734 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000735 }
736 }
Bob Wilson7248f862009-11-22 04:24:42 +0000737
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000738 if (CurMultiClass) {
David Greenedb10e692011-10-19 13:02:42 +0000739 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
740 "::");
741
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000742 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
743 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
744 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000745 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000746 }
747 }
Bob Wilson7248f862009-11-22 04:24:42 +0000748
David Greenefb927af2012-02-22 16:09:41 +0000749 // If this is in a foreach loop, make sure it's not a loop iterator
Craig Toppereb4d7c62015-04-29 04:43:36 +0000750 for (const auto &L : Loops) {
751 VarInit *IterVar = dyn_cast<VarInit>(L.IterVar);
David Greenefb927af2012-02-22 16:09:41 +0000752 if (IterVar && IterVar->getName() == Name)
753 return IterVar;
754 }
755
David Greene232bd602011-10-19 13:04:21 +0000756 if (Mode == ParseNameMode)
757 return StringInit::get(Name);
758
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000759 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000760 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000761
David Greene232bd602011-10-19 13:04:21 +0000762 if (Mode == ParseValueMode) {
763 Error(NameLoc, "Variable not defined: '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000764 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000765 }
766
767 return StringInit::get(Name);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000768}
769
David Greene5d0c0512009-05-14 20:54:48 +0000770/// ParseOperation - Parse an operator. This returns null on error.
771///
772/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
773///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000774Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000775 switch (Lex.getCode()) {
776 default:
777 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000778 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000779 case tgtok::XHead:
780 case tgtok::XTail:
781 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000782 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
783 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000784 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000785
David Greenee8f3b272009-05-14 21:22:49 +0000786 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000787 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000788 case tgtok::XCast:
789 Lex.Lex(); // eat the operation
790 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000791
David Greenee8f3b272009-05-14 21:22:49 +0000792 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000793
Craig Topper011817a2014-04-09 04:50:04 +0000794 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000795 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000796 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000797 }
David Greene5d0c0512009-05-14 20:54:48 +0000798
David Greenee8f3b272009-05-14 21:22:49 +0000799 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000800 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000801 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000802 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000803 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000804 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000805 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000806 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000807 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000808 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000809 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000810 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000811 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000812 break;
David Greenee8f3b272009-05-14 21:22:49 +0000813 }
814 if (Lex.getCode() != tgtok::l_paren) {
815 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000816 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000817 }
818 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000819
David Greeneaf8ee2c2011-07-29 22:43:06 +0000820 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000821 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000822
Craig Topper85c07002015-04-30 05:54:22 +0000823 if (Code == UnOpInit::HEAD ||
824 Code == UnOpInit::TAIL ||
825 Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000826 ListInit *LHSl = dyn_cast<ListInit>(LHS);
827 StringInit *LHSs = dyn_cast<StringInit>(LHS);
828 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000829 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000830 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000831 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000832 }
833 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000834 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
835 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000836 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000837 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000838 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000839 }
840 }
841
Craig Topper85c07002015-04-30 05:54:22 +0000842 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
Craig Topper011817a2014-04-09 04:50:04 +0000843 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000844 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000845 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000846 }
Bob Wilson7248f862009-11-22 04:24:42 +0000847
David Greened571b3c2009-05-14 22:38:31 +0000848 if (LHSl && LHSl->getSize() == 0) {
849 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000850 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000851 }
852 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000853 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000854 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000855 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000856 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000857 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000858 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000859 if (Code == UnOpInit::HEAD) {
David Greened571b3c2009-05-14 22:38:31 +0000860 Type = Itemt->getType();
Bob Wilson7248f862009-11-22 04:24:42 +0000861 } else {
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000862 Type = ListRecTy::get(Itemt->getType());
David Greened571b3c2009-05-14 22:38:31 +0000863 }
Bob Wilson7248f862009-11-22 04:24:42 +0000864 } else {
David Greened571b3c2009-05-14 22:38:31 +0000865 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000866 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000867 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000868 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000869 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000870 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000871 if (Code == UnOpInit::HEAD) {
David Greened571b3c2009-05-14 22:38:31 +0000872 Type = LType->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +0000873 } else {
David Greened571b3c2009-05-14 22:38:31 +0000874 Type = LType;
875 }
876 }
877 }
878 }
879
David Greenee8f3b272009-05-14 21:22:49 +0000880 if (Lex.getCode() != tgtok::r_paren) {
881 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000882 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000883 }
884 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000885 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000886 }
David Greene5d0c0512009-05-14 20:54:48 +0000887
888 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000889 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000890 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +0000891 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000892 case tgtok::XSRL:
893 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000894 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000895 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000896 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000897 tgtok::TokKind OpTok = Lex.getCode();
898 SMLoc OpLoc = Lex.getLoc();
899 Lex.Lex(); // eat the operation
900
David Greene5d0c0512009-05-14 20:54:48 +0000901 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000902 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000903
Chris Lattner61ea00b2010-10-05 23:58:18 +0000904 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000905 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000906 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000907 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000908 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000909 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
910 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
911 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
912 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000913 case tgtok::XListConcat:
914 Code = BinOpInit::LISTCONCAT;
915 // We don't know the list type until we parse the first argument
916 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000917 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000918 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000919 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000920 break;
David Greene5d0c0512009-05-14 20:54:48 +0000921 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000922
David Greene5d0c0512009-05-14 20:54:48 +0000923 if (Lex.getCode() != tgtok::l_paren) {
924 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000925 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000926 }
927 Lex.Lex(); // eat the '('
928
David Greeneaf8ee2c2011-07-29 22:43:06 +0000929 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000930
Chris Lattner61ea00b2010-10-05 23:58:18 +0000931 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000932 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000933
Chris Lattner61ea00b2010-10-05 23:58:18 +0000934 while (Lex.getCode() == tgtok::comma) {
935 Lex.Lex(); // eat the ','
936
937 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000938 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000939 }
David Greene5d0c0512009-05-14 20:54:48 +0000940
941 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000942 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000943 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000944 }
945 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000946
Daniel Sanders314e80e2014-05-07 10:13:19 +0000947 // If we are doing !listconcat, we should know the type by now
948 if (OpTok == tgtok::XListConcat) {
949 if (VarInit *Arg0 = dyn_cast<VarInit>(InitList[0]))
950 Type = Arg0->getType();
951 else if (ListInit *Arg0 = dyn_cast<ListInit>(InitList[0]))
952 Type = Arg0->getType();
953 else {
954 InitList[0]->dump();
955 Error(OpLoc, "expected a list");
956 return nullptr;
957 }
958 }
959
Chris Lattner61ea00b2010-10-05 23:58:18 +0000960 // We allow multiple operands to associative operators like !strconcat as
961 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000962 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000963 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000964 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000965 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
966 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000967 InitList.back() = RHS;
968 }
969 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000970
Chris Lattner61ea00b2010-10-05 23:58:18 +0000971 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000972 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000973 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000974
Chris Lattner61ea00b2010-10-05 23:58:18 +0000975 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +0000976 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000977 }
978
David Greene3587eed2009-05-14 23:26:46 +0000979 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +0000980 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +0000981 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
982 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000983 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000984
David Greene98ed3c72009-05-14 21:54:42 +0000985 tgtok::TokKind LexCode = Lex.getCode();
986 Lex.Lex(); // eat the operation
987 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +0000988 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +0000989 case tgtok::XIf:
990 Code = TernOpInit::IF;
991 break;
David Greenee917fff2009-05-14 22:23:47 +0000992 case tgtok::XForEach:
993 Code = TernOpInit::FOREACH;
994 break;
David Greene98ed3c72009-05-14 21:54:42 +0000995 case tgtok::XSubst:
996 Code = TernOpInit::SUBST;
997 break;
998 }
999 if (Lex.getCode() != tgtok::l_paren) {
1000 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001001 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001002 }
1003 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001004
David Greeneaf8ee2c2011-07-29 22:43:06 +00001005 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001006 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001007
David Greene98ed3c72009-05-14 21:54:42 +00001008 if (Lex.getCode() != tgtok::comma) {
1009 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001010 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001011 }
1012 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001013
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001014 Init *MHS = ParseValue(CurRec, ItemType);
1015 if (!MHS)
1016 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001017
David Greene98ed3c72009-05-14 21:54:42 +00001018 if (Lex.getCode() != tgtok::comma) {
1019 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001020 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001021 }
1022 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001023
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001024 Init *RHS = ParseValue(CurRec, ItemType);
1025 if (!RHS)
1026 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001027
David Greene98ed3c72009-05-14 21:54:42 +00001028 if (Lex.getCode() != tgtok::r_paren) {
1029 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001030 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001031 }
1032 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001033
David Greene98ed3c72009-05-14 21:54:42 +00001034 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001035 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001036 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001037 RecTy *MHSTy = nullptr;
1038 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001039
Sean Silvafb509ed2012-10-10 20:24:43 +00001040 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001041 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001042 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001043 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001044 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001045 MHSTy = BitRecTy::get();
1046
Sean Silvafb509ed2012-10-10 20:24:43 +00001047 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001048 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001049 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001050 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001051 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001052 RHSTy = BitRecTy::get();
1053
1054 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001055 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001056 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001057 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001058 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001059
1060 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001061 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001062 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001063 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001064
1065 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1066 Type = RHSTy;
1067 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1068 Type = MHSTy;
Bob Wilson7248f862009-11-22 04:24:42 +00001069 } else {
David Greene3587eed2009-05-14 23:26:46 +00001070 TokError("inconsistent types for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001071 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001072 }
1073 break;
1074 }
David Greenee917fff2009-05-14 22:23:47 +00001075 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001076 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
Craig Topper011817a2014-04-09 04:50:04 +00001077 if (!MHSt) {
David Greenee917fff2009-05-14 22:23:47 +00001078 TokError("could not get type for !foreach");
Craig Topper011817a2014-04-09 04:50:04 +00001079 return nullptr;
David Greenee917fff2009-05-14 22:23:47 +00001080 }
1081 Type = MHSt->getType();
1082 break;
1083 }
David Greene98ed3c72009-05-14 21:54:42 +00001084 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001085 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001086 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001087 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001088 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001089 }
1090 Type = RHSt->getType();
1091 break;
1092 }
1093 }
David Greenee32ebf22011-07-29 19:07:07 +00001094 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001095 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001096 }
David Greene5d0c0512009-05-14 20:54:48 +00001097 }
David Greene5d0c0512009-05-14 20:54:48 +00001098}
1099
1100/// ParseOperatorType - Parse a type for an operator. This returns
1101/// null on error.
1102///
1103/// OperatorType ::= '<' Type '>'
1104///
Dan Gohman1432ef82009-08-12 22:10:57 +00001105RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001106 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001107
1108 if (Lex.getCode() != tgtok::less) {
1109 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001110 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001111 }
1112 Lex.Lex(); // eat the <
1113
1114 Type = ParseType();
1115
Craig Topper011817a2014-04-09 04:50:04 +00001116 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001117 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001118 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001119 }
1120
1121 if (Lex.getCode() != tgtok::greater) {
1122 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001123 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001124 }
1125 Lex.Lex(); // eat the >
1126
1127 return Type;
1128}
1129
1130
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001131/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1132///
1133/// SimpleValue ::= IDValue
1134/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001135/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001136/// SimpleValue ::= CODEFRAGMENT
1137/// SimpleValue ::= '?'
1138/// SimpleValue ::= '{' ValueList '}'
1139/// SimpleValue ::= ID '<' ValueListNE '>'
1140/// SimpleValue ::= '[' ValueList ']'
1141/// SimpleValue ::= '(' IDValue DagArgList ')'
1142/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001143/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001144/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1145/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1146/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001147/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001148/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1149///
David Greened4263a62011-10-19 13:04:20 +00001150Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1151 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001152 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001153 switch (Lex.getCode()) {
1154 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001155 case tgtok::paste:
1156 // This is a leading paste operation. This is deprecated but
1157 // still exists in some .td files. Ignore it.
1158 Lex.Lex(); // Skip '#'.
1159 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001160 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001161 case tgtok::BinaryIntVal: {
1162 auto BinaryVal = Lex.getCurBinaryIntVal();
1163 SmallVector<Init*, 16> Bits(BinaryVal.second);
1164 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001165 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001166 R = BitsInit::get(Bits);
1167 Lex.Lex();
1168 break;
1169 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001170 case tgtok::StrVal: {
1171 std::string Val = Lex.getCurStrVal();
1172 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001173
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001174 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001175 while (Lex.getCode() == tgtok::StrVal) {
1176 Val += Lex.getCurStrVal();
1177 Lex.Lex();
1178 }
Bob Wilson7248f862009-11-22 04:24:42 +00001179
David Greenee32ebf22011-07-29 19:07:07 +00001180 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001181 break;
1182 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001183 case tgtok::CodeFragment:
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +00001184 R = StringInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001185 Lex.Lex();
1186 break;
1187 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001188 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001189 Lex.Lex();
1190 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001191 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001192 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001193 std::string Name = Lex.getCurStrVal();
1194 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001195 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001196
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001197 // Value ::= ID '<' ValueListNE '>'
1198 if (Lex.Lex() == tgtok::greater) {
1199 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001200 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001201 }
David Greene8618f952009-06-08 20:23:18 +00001202
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001203 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1204 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1205 // body.
1206 Record *Class = Records.getClass(Name);
1207 if (!Class) {
1208 Error(NameLoc, "Expected a class name, got '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001209 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001210 }
David Greene8618f952009-06-08 20:23:18 +00001211
David Greeneaf8ee2c2011-07-29 22:43:06 +00001212 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
Craig Topper011817a2014-04-09 04:50:04 +00001213 if (ValueList.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001214
David Greene8618f952009-06-08 20:23:18 +00001215 if (Lex.getCode() != tgtok::greater) {
1216 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001217 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001218 }
1219 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001220 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001221
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001222 // Create the new record, set it as CurRec temporarily.
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00001223 auto NewRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), NameLoc,
1224 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001225 Record *NewRec = NewRecOwner.get(); // Keep a copy since we may release.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001226 SubClassReference SCRef;
Jordan Rosef12e8a92013-01-10 18:50:11 +00001227 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001228 SCRef.Rec = Class;
1229 SCRef.TemplateArgs = ValueList;
1230 // Add info about the subclass to NewRec.
Craig Topper84138712014-11-29 05:31:10 +00001231 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001232 return nullptr;
Craig Topper84138712014-11-29 05:31:10 +00001233
Hal Finkela8c1f462014-01-02 20:47:09 +00001234 if (!CurMultiClass) {
1235 NewRec->resolveReferences();
Craig Toppercdab2322014-11-29 05:52:51 +00001236 Records.addDef(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001237 } else {
Adam Nemete5a07162014-09-16 17:14:13 +00001238 // This needs to get resolved once the multiclass template arguments are
1239 // known before any use.
1240 NewRec->setResolveFirst(true);
Hal Finkela8c1f462014-01-02 20:47:09 +00001241 // Otherwise, we're inside a multiclass, add it to the multiclass.
Craig Topperc3504c42014-12-11 05:25:33 +00001242 CurMultiClass->DefPrototypes.push_back(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001243
1244 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00001245 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
1246 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Hal Finkela8c1f462014-01-02 20:47:09 +00001247 assert(RV && "Template arg doesn't exist?");
1248 NewRec->addValue(*RV);
1249 }
1250
1251 // We can't return the prototype def here, instead return:
1252 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1253 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1254 assert(MCNameRV && "multiclass record must have a NAME");
1255
1256 return UnOpInit::get(UnOpInit::CAST,
1257 BinOpInit::get(BinOpInit::STRCONCAT,
1258 VarInit::get(MCNameRV->getName(),
1259 MCNameRV->getType()),
1260 NewRec->getNameInit(),
1261 StringRecTy::get()),
1262 Class->getDefInit()->getType());
1263 }
Bob Wilson7248f862009-11-22 04:24:42 +00001264
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001265 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001266 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001267 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001268 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001269 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001270 Lex.Lex(); // eat the '{'
David Greeneaf8ee2c2011-07-29 22:43:06 +00001271 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001272
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001273 if (Lex.getCode() != tgtok::r_brace) {
1274 Vals = ParseValueList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001275 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001276 }
1277 if (Lex.getCode() != tgtok::r_brace) {
1278 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001279 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001280 }
1281 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001282
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001283 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001284
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001285 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1286 // first. We'll first read everything in to a vector, then we can reverse
1287 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001288 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001289 // FIXME: The following two loops would not be duplicated
1290 // if the API was a little more orthogonal.
1291
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001292 // bits<n> values are allowed to initialize n bits.
1293 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1294 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1295 NewBits.push_back(BI->getBit((e - i) - 1));
1296 continue;
1297 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001298 // bits<n> can also come from variable initializers.
1299 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1300 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1301 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1302 NewBits.push_back(VI->getBit((e - i) - 1));
1303 continue;
1304 }
1305 // Fallthrough to try convert this to a bit.
1306 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001307 // All other values must be convertible to just a single bit.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001308 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001309 if (!Bit) {
Chris Lattner52416952007-11-22 21:06:59 +00001310 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1311 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001312 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001313 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001314 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001315 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001316 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001317 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001318 }
1319 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1320 Lex.Lex(); // eat the '['
David Greeneaf8ee2c2011-07-29 22:43:06 +00001321 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001322
Craig Topper011817a2014-04-09 04:50:04 +00001323 RecTy *DeducedEltTy = nullptr;
1324 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001325
Craig Topper011817a2014-04-09 04:50:04 +00001326 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001327 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001328 if (!ListType) {
Alp Tokere69170a2014-06-26 22:52:05 +00001329 std::string s;
1330 raw_string_ostream ss(s);
Reid Klecknerd78273f2013-08-06 22:51:21 +00001331 ss << "Type mismatch for list, expected list type, got "
1332 << ItemType->getAsString();
1333 TokError(ss.str());
Craig Topper011817a2014-04-09 04:50:04 +00001334 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001335 }
1336 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001337 }
David Greene8618f952009-06-08 20:23:18 +00001338
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001339 if (Lex.getCode() != tgtok::r_square) {
Craig Topper011817a2014-04-09 04:50:04 +00001340 Vals = ParseValueList(CurRec, nullptr,
1341 GivenListTy ? GivenListTy->getElementType() : nullptr);
1342 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001343 }
1344 if (Lex.getCode() != tgtok::r_square) {
1345 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001346 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001347 }
1348 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001349
Craig Topper011817a2014-04-09 04:50:04 +00001350 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001351 if (Lex.getCode() == tgtok::less) {
1352 // Optional list element type
1353 Lex.Lex(); // eat the '<'
1354
1355 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001356 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001357 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001358 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001359 }
1360
1361 if (Lex.getCode() != tgtok::greater) {
1362 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001363 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001364 }
1365 Lex.Lex(); // eat the '>'
1366 }
1367
1368 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001369 RecTy *EltTy = nullptr;
Craig Toppereb4d7c62015-04-29 04:43:36 +00001370 for (Init *V : Vals) {
1371 TypedInit *TArg = dyn_cast<TypedInit>(V);
Craig Topper011817a2014-04-09 04:50:04 +00001372 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001373 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001374 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001375 }
Craig Topper011817a2014-04-09 04:50:04 +00001376 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001377 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001378 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001379 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001380 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001381 }
Bob Wilson7248f862009-11-22 04:24:42 +00001382 } else {
David Greene8618f952009-06-08 20:23:18 +00001383 EltTy = TArg->getType();
1384 }
1385 }
1386
Craig Topper011817a2014-04-09 04:50:04 +00001387 if (GivenEltTy) {
1388 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001389 // Verify consistency
1390 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1391 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001392 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001393 }
1394 }
1395 EltTy = GivenEltTy;
1396 }
1397
Craig Topper011817a2014-04-09 04:50:04 +00001398 if (!EltTy) {
1399 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001400 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001401 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001402 }
1403 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001404 } else {
David Greene8618f952009-06-08 20:23:18 +00001405 // Make sure the deduced type is compatible with the given type
1406 if (GivenListTy) {
1407 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1408 TokError("Element type mismatch for list");
Craig Topper011817a2014-04-09 04:50:04 +00001409 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001410 }
1411 }
1412 DeducedEltTy = EltTy;
1413 }
Bob Wilson7248f862009-11-22 04:24:42 +00001414
David Greenee32ebf22011-07-29 19:07:07 +00001415 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001416 }
1417 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1418 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001419 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001420 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001421 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001422 }
Bob Wilson7248f862009-11-22 04:24:42 +00001423
David Greeneaf8ee2c2011-07-29 22:43:06 +00001424 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001425 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001426
Nate Begemandbe3f772009-03-19 05:21:56 +00001427 // If the operator name is present, parse it.
1428 std::string OperatorName;
1429 if (Lex.getCode() == tgtok::colon) {
1430 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1431 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001432 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001433 }
1434 OperatorName = Lex.getCurStrVal();
1435 Lex.Lex(); // eat the VarName.
1436 }
Bob Wilson7248f862009-11-22 04:24:42 +00001437
David Greeneaf8ee2c2011-07-29 22:43:06 +00001438 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001439 if (Lex.getCode() != tgtok::r_paren) {
1440 DagArgs = ParseDagArgList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001441 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001442 }
Bob Wilson7248f862009-11-22 04:24:42 +00001443
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001444 if (Lex.getCode() != tgtok::r_paren) {
1445 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001446 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001447 }
1448 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001449
David Greenee32ebf22011-07-29 19:07:07 +00001450 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001451 }
Bob Wilson7248f862009-11-22 04:24:42 +00001452
David Greene2f7cf7f2011-01-07 17:05:37 +00001453 case tgtok::XHead:
1454 case tgtok::XTail:
1455 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001456 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001457 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001458 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001459 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +00001460 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001461 case tgtok::XSRL:
1462 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001463 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001464 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001465 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001466 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001467 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001468 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001469 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001470 }
1471 }
Bob Wilson7248f862009-11-22 04:24:42 +00001472
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001473 return R;
1474}
1475
1476/// ParseValue - Parse a tblgen value. This returns null on error.
1477///
1478/// Value ::= SimpleValue ValueSuffix*
1479/// ValueSuffix ::= '{' BitList '}'
1480/// ValueSuffix ::= '[' BitList ']'
1481/// ValueSuffix ::= '.' ID
1482///
David Greened4263a62011-10-19 13:04:20 +00001483Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1484 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001485 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001486
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001487 // Parse the suffixes now if present.
1488 while (1) {
1489 switch (Lex.getCode()) {
1490 default: return Result;
1491 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001492 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001493 // This is the beginning of the object body.
1494 return Result;
1495
Chris Lattner526c8cb2009-06-21 03:39:35 +00001496 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001497 Lex.Lex(); // eat the '{'
1498 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001499 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001500
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001501 // Reverse the bitlist.
1502 std::reverse(Ranges.begin(), Ranges.end());
1503 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001504 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001505 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001506 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001507 }
Bob Wilson7248f862009-11-22 04:24:42 +00001508
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001509 // Eat the '}'.
1510 if (Lex.getCode() != tgtok::r_brace) {
1511 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001512 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001513 }
1514 Lex.Lex();
1515 break;
1516 }
1517 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001518 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001519 Lex.Lex(); // eat the '['
1520 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001521 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001522
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001523 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001524 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001525 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001526 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001527 }
Bob Wilson7248f862009-11-22 04:24:42 +00001528
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001529 // Eat the ']'.
1530 if (Lex.getCode() != tgtok::r_square) {
1531 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001532 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001533 }
1534 Lex.Lex();
1535 break;
1536 }
1537 case tgtok::period:
1538 if (Lex.Lex() != tgtok::Id) { // eat the .
1539 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001540 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001541 }
1542 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001543 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001544 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001545 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001546 }
David Greenee32ebf22011-07-29 19:07:07 +00001547 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001548 Lex.Lex(); // eat field name
1549 break;
David Greene8e85b482011-10-19 13:04:43 +00001550
1551 case tgtok::paste:
1552 SMLoc PasteLoc = Lex.getLoc();
1553
1554 // Create a !strconcat() operation, first casting each operand to
1555 // a string if necessary.
1556
Sean Silvafb509ed2012-10-10 20:24:43 +00001557 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001558 if (!LHS) {
1559 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001560 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001561 }
1562
1563 if (LHS->getType() != StringRecTy::get()) {
1564 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1565 }
1566
Craig Topper011817a2014-04-09 04:50:04 +00001567 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001568
1569 Lex.Lex(); // Eat the '#'.
1570 switch (Lex.getCode()) {
1571 case tgtok::colon:
1572 case tgtok::semi:
1573 case tgtok::l_brace:
1574 // These are all of the tokens that can begin an object body.
1575 // Some of these can also begin values but we disallow those cases
1576 // because they are unlikely to be useful.
1577
1578 // Trailing paste, concat with an empty string.
1579 RHS = StringInit::get("");
1580 break;
1581
1582 default:
1583 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001584 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001585 if (!RHS) {
1586 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001587 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001588 }
1589
1590 if (RHS->getType() != StringRecTy::get()) {
1591 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1592 }
1593
1594 break;
1595 }
1596
1597 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1598 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1599 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001600 }
1601 }
1602}
1603
1604/// ParseDagArgList - Parse the argument list for a dag literal expression.
1605///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001606/// DagArg ::= Value (':' VARNAME)?
1607/// DagArg ::= VARNAME
1608/// DagArgList ::= DagArg
1609/// DagArgList ::= DagArgList ',' DagArg
David Greeneaf8ee2c2011-07-29 22:43:06 +00001610std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001611TGParser::ParseDagArgList(Record *CurRec) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001612 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson7248f862009-11-22 04:24:42 +00001613
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001614 while (1) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001615 // DagArg ::= VARNAME
1616 if (Lex.getCode() == tgtok::VarName) {
1617 // A missing value is treated like '?'.
1618 Result.push_back(std::make_pair(UnsetInit::get(), Lex.getCurStrVal()));
1619 Lex.Lex();
1620 } else {
1621 // DagArg ::= Value (':' VARNAME)?
1622 Init *Val = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001623 if (!Val)
David Greeneaf8ee2c2011-07-29 22:43:06 +00001624 return std::vector<std::pair<llvm::Init*, std::string> >();
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001625
1626 // If the variable name is present, add it.
1627 std::string VarName;
1628 if (Lex.getCode() == tgtok::colon) {
1629 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1630 TokError("expected variable name in dag literal");
1631 return std::vector<std::pair<llvm::Init*, std::string> >();
1632 }
1633 VarName = Lex.getCurStrVal();
1634 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001635 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001636
1637 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001638 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001639 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001640 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001641 }
Bob Wilson7248f862009-11-22 04:24:42 +00001642
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001643 return Result;
1644}
1645
1646
1647/// ParseValueList - Parse a comma separated list of values, returning them as a
1648/// vector. Note that this always expects to be able to parse at least one
1649/// value. It returns an empty list if this is not possible.
1650///
1651/// ValueList ::= Value (',' Value)
1652///
David Greeneaf8ee2c2011-07-29 22:43:06 +00001653std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopher71520a82011-07-11 23:06:52 +00001654 RecTy *EltTy) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001655 std::vector<Init*> Result;
David Greene8618f952009-06-08 20:23:18 +00001656 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001657 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001658 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001659 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001660 if (TArgs.empty()) {
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001661 TokError("template argument provided to non-template class");
1662 return std::vector<Init*>();
1663 }
David Greene8618f952009-06-08 20:23:18 +00001664 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001665 if (!RV) {
1666 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1667 << ")\n";
1668 }
David Greene8618f952009-06-08 20:23:18 +00001669 assert(RV && "Template argument record not found??");
1670 ItemType = RV->getType();
1671 ++ArgN;
1672 }
1673 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001674 if (!Result.back()) return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001675
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001676 while (Lex.getCode() == tgtok::comma) {
1677 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001678
Craig Topper011817a2014-04-09 04:50:04 +00001679 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001680 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001681 if (ArgN >= TArgs.size()) {
1682 TokError("too many template arguments");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001683 return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001684 }
David Greene8618f952009-06-08 20:23:18 +00001685 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1686 assert(RV && "Template argument record not found??");
1687 ItemType = RV->getType();
1688 ++ArgN;
1689 }
1690 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001691 if (!Result.back()) return std::vector<Init*>();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001692 }
Bob Wilson7248f862009-11-22 04:24:42 +00001693
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001694 return Result;
1695}
1696
1697
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001698/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1699/// empty string on error. This can happen in a number of different context's,
1700/// including within a def or in the template args for a def (which which case
1701/// CurRec will be non-null) and within the template args for a multiclass (in
1702/// which case CurRec will be null, but CurMultiClass will be set). This can
1703/// also happen within a def that is within a multiclass, which will set both
1704/// CurRec and CurMultiClass.
1705///
1706/// Declaration ::= FIELD? Type ID ('=' Value)?
1707///
David Greenedb10e692011-10-19 13:02:42 +00001708Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001709 bool ParsingTemplateArgs) {
1710 // Read the field prefix if present.
1711 bool HasField = Lex.getCode() == tgtok::Field;
1712 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001713
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001714 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001715 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001716
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001717 if (Lex.getCode() != tgtok::Id) {
1718 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001719 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001720 }
Bob Wilson7248f862009-11-22 04:24:42 +00001721
Chris Lattner526c8cb2009-06-21 03:39:35 +00001722 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001723 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001724 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001725
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001726 if (ParsingTemplateArgs) {
1727 if (CurRec) {
David Greenedb10e692011-10-19 13:02:42 +00001728 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001729 } else {
1730 assert(CurMultiClass);
1731 }
1732 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001733 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1734 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001735 }
Bob Wilson7248f862009-11-22 04:24:42 +00001736
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001737 // Add the value.
1738 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001739 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001740
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001741 // If a value is present, parse it.
1742 if (Lex.getCode() == tgtok::equal) {
1743 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001744 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001745 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001746 if (!Val ||
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001747 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001748 // Return the name, even if an error is thrown. This is so that we can
1749 // continue to make some progress, even without the value having been
1750 // initialized.
1751 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001752 }
Bob Wilson7248f862009-11-22 04:24:42 +00001753
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001754 return DeclName;
1755}
1756
David Greenefb927af2012-02-22 16:09:41 +00001757/// ParseForeachDeclaration - Read a foreach declaration, returning
1758/// the name of the declared object or a NULL Init on error. Return
1759/// the name of the parsed initializer list through ForeachListName.
1760///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001761/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1762/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1763/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001764///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001765VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001766 if (Lex.getCode() != tgtok::Id) {
1767 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001768 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001769 }
1770
1771 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1772 Lex.Lex();
1773
1774 // If a value is present, parse it.
1775 if (Lex.getCode() != tgtok::equal) {
1776 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001777 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001778 }
1779 Lex.Lex(); // Eat the '='
1780
Craig Topper011817a2014-04-09 04:50:04 +00001781 RecTy *IterType = nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001782 std::vector<unsigned> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001783
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001784 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001785 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001786 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001787 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001788 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001789 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001790 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001791 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001792 }
1793 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001794 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001795 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001796 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001797 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001798 }
1799 IterType = ListType->getElementType();
1800 break;
David Greenefb927af2012-02-22 16:09:41 +00001801 }
1802
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001803 case tgtok::IntVal: { // RangePiece.
1804 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001805 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001806 break;
David Greenefb927af2012-02-22 16:09:41 +00001807 }
1808
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001809 case tgtok::l_brace: { // '{' RangeList '}'
1810 Lex.Lex(); // eat the '{'
1811 Ranges = ParseRangeList();
1812 if (Lex.getCode() != tgtok::r_brace) {
1813 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001814 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001815 }
1816 Lex.Lex();
1817 break;
1818 }
1819 }
David Greenefb927af2012-02-22 16:09:41 +00001820
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001821 if (!Ranges.empty()) {
1822 assert(!IterType && "Type already initialized?");
1823 IterType = IntRecTy::get();
1824 std::vector<Init*> Values;
1825 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
1826 Values.push_back(IntInit::get(Ranges[i]));
1827 ForeachListValue = ListInit::get(Values, IterType);
1828 }
1829
1830 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001831 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001832
1833 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001834}
1835
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001836/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1837/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1838/// template args for a def, which may or may not be in a multiclass. If null,
1839/// these are the template args for a multiclass.
1840///
1841/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001842///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001843bool TGParser::ParseTemplateArgList(Record *CurRec) {
1844 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1845 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001846
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001847 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001848
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001849 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001850 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001851 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001852 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001853
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001854 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001855
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001856 while (Lex.getCode() == tgtok::comma) {
1857 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001858
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001859 // Read the following declarations.
1860 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001861 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001862 return true;
1863 TheRecToAddTo->addTemplateArg(TemplArg);
1864 }
Bob Wilson7248f862009-11-22 04:24:42 +00001865
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001866 if (Lex.getCode() != tgtok::greater)
1867 return TokError("expected '>' at end of template argument list");
1868 Lex.Lex(); // eat the '>'.
1869 return false;
1870}
1871
1872
1873/// ParseBodyItem - Parse a single item at within the body of a def or class.
1874///
1875/// BodyItem ::= Declaration ';'
1876/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1877bool TGParser::ParseBodyItem(Record *CurRec) {
1878 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001879 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001880 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001881
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001882 if (Lex.getCode() != tgtok::semi)
1883 return TokError("expected ';' after declaration");
1884 Lex.Lex();
1885 return false;
1886 }
1887
1888 // LET ID OptionalRangeList '=' Value ';'
1889 if (Lex.Lex() != tgtok::Id)
1890 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001891
Chris Lattner526c8cb2009-06-21 03:39:35 +00001892 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001893 std::string FieldName = Lex.getCurStrVal();
1894 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001895
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001896 std::vector<unsigned> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001897 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001898 return true;
1899 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001900
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001901 if (Lex.getCode() != tgtok::equal)
1902 return TokError("expected '=' in let expression");
1903 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001904
David Greene8618f952009-06-08 20:23:18 +00001905 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001906 if (!Field)
David Greene8618f952009-06-08 20:23:18 +00001907 return TokError("Value '" + FieldName + "' unknown!");
1908
1909 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001910
David Greeneaf8ee2c2011-07-29 22:43:06 +00001911 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001912 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001913
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001914 if (Lex.getCode() != tgtok::semi)
1915 return TokError("expected ';' after let expression");
1916 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001917
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001918 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1919}
1920
1921/// ParseBody - Read the body of a class or def. Return true on error, false on
1922/// success.
1923///
1924/// Body ::= ';'
1925/// Body ::= '{' BodyList '}'
1926/// BodyList BodyItem*
1927///
1928bool TGParser::ParseBody(Record *CurRec) {
1929 // If this is a null definition, just eat the semi and return.
1930 if (Lex.getCode() == tgtok::semi) {
1931 Lex.Lex();
1932 return false;
1933 }
Bob Wilson7248f862009-11-22 04:24:42 +00001934
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001935 if (Lex.getCode() != tgtok::l_brace)
1936 return TokError("Expected ';' or '{' to start body");
1937 // Eat the '{'.
1938 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001939
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001940 while (Lex.getCode() != tgtok::r_brace)
1941 if (ParseBodyItem(CurRec))
1942 return true;
1943
1944 // Eat the '}'.
1945 Lex.Lex();
1946 return false;
1947}
1948
Sean Silvacb1a75e2013-01-09 04:49:14 +00001949/// \brief Apply the current let bindings to \a CurRec.
1950/// \returns true on error, false otherwise.
1951bool TGParser::ApplyLetStack(Record *CurRec) {
1952 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1953 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1954 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1955 LetStack[i][j].Bits, LetStack[i][j].Value))
1956 return true;
1957 return false;
1958}
1959
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001960/// ParseObjectBody - Parse the body of a def or class. This consists of an
1961/// optional ClassList followed by a Body. CurRec is the current def or class
1962/// that is being parsed.
1963///
1964/// ObjectBody ::= BaseClassList Body
1965/// BaseClassList ::= /*empty*/
1966/// BaseClassList ::= ':' BaseClassListNE
1967/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1968///
1969bool TGParser::ParseObjectBody(Record *CurRec) {
1970 // If there is a baseclass list, read it.
1971 if (Lex.getCode() == tgtok::colon) {
1972 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001973
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001974 // Read all of the subclasses.
1975 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1976 while (1) {
1977 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00001978 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001979
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001980 // Add it.
1981 if (AddSubClass(CurRec, SubClass))
1982 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001983
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001984 if (Lex.getCode() != tgtok::comma) break;
1985 Lex.Lex(); // eat ','.
1986 SubClass = ParseSubClassReference(CurRec, false);
1987 }
1988 }
1989
Sean Silvacb1a75e2013-01-09 04:49:14 +00001990 if (ApplyLetStack(CurRec))
1991 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001992
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001993 return ParseBody(CurRec);
1994}
1995
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001996/// ParseDef - Parse and return a top level or multiclass def, return the record
1997/// corresponding to it. This returns null on error.
1998///
1999/// DefInst ::= DEF ObjectName ObjectBody
2000///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002001bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00002002 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002003 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00002004 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002005
2006 // Parse ObjectName and make a record for it.
Craig Topper84138712014-11-29 05:31:10 +00002007 std::unique_ptr<Record> CurRecOwner;
Jordan Roseabdd99b2013-01-10 18:50:05 +00002008 Init *Name = ParseObjectName(CurMultiClass);
2009 if (Name)
Craig Topper84138712014-11-29 05:31:10 +00002010 CurRecOwner = make_unique<Record>(Name, DefLoc, Records);
Jordan Roseabdd99b2013-01-10 18:50:05 +00002011 else
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00002012 CurRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), DefLoc,
2013 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00002014 Record *CurRec = CurRecOwner.get(); // Keep a copy since we may release.
Bob Wilson7248f862009-11-22 04:24:42 +00002015
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002016 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002017 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002018
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002019 // Ensure redefinition doesn't happen.
Craig Topper84138712014-11-29 05:31:10 +00002020 if (Records.getDef(CurRec->getNameInitAsString()))
2021 return Error(DefLoc, "def '" + CurRec->getNameInitAsString()+
2022 "' already defined");
Craig Toppercdab2322014-11-29 05:52:51 +00002023 Records.addDef(std::move(CurRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00002024
2025 if (ParseObjectBody(CurRec))
2026 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002027 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002028 // Parse the body before adding this prototype to the DefPrototypes vector.
2029 // That way implicit definitions will be added to the DefPrototypes vector
2030 // before this object, instantiated prior to defs derived from this object,
2031 // and this available for indirect name resolution when defs derived from
2032 // this object are instantiated.
Craig Topper84138712014-11-29 05:31:10 +00002033 if (ParseObjectBody(CurRec))
Hal Finkela8c1f462014-01-02 20:47:09 +00002034 return true;
2035
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002036 // Otherwise, a def inside a multiclass, add it to the multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002037 for (const auto &Proto : CurMultiClass->DefPrototypes)
2038 if (Proto->getNameInit() == CurRec->getNameInit())
Craig Topper84138712014-11-29 05:31:10 +00002039 return Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
2040 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002041 CurMultiClass->DefPrototypes.push_back(std::move(CurRecOwner));
Anton Yartsev671dff12014-08-08 00:29:54 +00002042 } else if (ParseObjectBody(CurRec)) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002043 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002044 }
Bob Wilson7248f862009-11-22 04:24:42 +00002045
Craig Topper011817a2014-04-09 04:50:04 +00002046 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002047 // See Record::setName(). This resolve step will see any new name
2048 // for the def that might have been created when resolving
2049 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002050 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002051
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002052 // If ObjectBody has template arguments, it's an error.
2053 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002054
2055 if (CurMultiClass) {
2056 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002057 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
2058 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002059 assert(RV && "Template arg doesn't exist?");
2060 CurRec->addValue(*RV);
2061 }
2062 }
2063
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002064 if (ProcessForeachDefs(CurRec, DefLoc)) {
Craig Topper84138712014-11-29 05:31:10 +00002065 return Error(DefLoc, "Could not process loops for def" +
2066 CurRec->getNameInitAsString());
David Greenefb927af2012-02-22 16:09:41 +00002067 }
2068
2069 return false;
2070}
2071
2072/// ParseForeach - Parse a for statement. Return the record corresponding
2073/// to it. This returns true on error.
2074///
2075/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2076/// Foreach ::= FOREACH Declaration IN Object
2077///
2078bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2079 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2080 Lex.Lex(); // Eat the 'for' token.
2081
2082 // Make a temporary object to record items associated with the for
2083 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002084 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002085 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002086 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002087 return TokError("expected declaration in for");
2088
2089 if (Lex.getCode() != tgtok::In)
2090 return TokError("Unknown tok");
2091 Lex.Lex(); // Eat the in
2092
2093 // Create a loop object and remember it.
2094 Loops.push_back(ForeachLoop(IterName, ListValue));
2095
2096 if (Lex.getCode() != tgtok::l_brace) {
2097 // FOREACH Declaration IN Object
2098 if (ParseObject(CurMultiClass))
2099 return true;
2100 }
2101 else {
2102 SMLoc BraceLoc = Lex.getLoc();
2103 // Otherwise, this is a group foreach.
2104 Lex.Lex(); // eat the '{'.
2105
2106 // Parse the object list.
2107 if (ParseObjectList(CurMultiClass))
2108 return true;
2109
2110 if (Lex.getCode() != tgtok::r_brace) {
2111 TokError("expected '}' at end of foreach command");
2112 return Error(BraceLoc, "to match this '{'");
2113 }
2114 Lex.Lex(); // Eat the }
2115 }
2116
2117 // We've processed everything in this loop.
2118 Loops.pop_back();
2119
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002120 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002121}
2122
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002123/// ParseClass - Parse a tblgen class definition.
2124///
2125/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2126///
2127bool TGParser::ParseClass() {
2128 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2129 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002130
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002131 if (Lex.getCode() != tgtok::Id)
2132 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002133
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002134 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2135 if (CurRec) {
2136 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002137 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002138 !CurRec->getSuperClasses().empty() ||
2139 !CurRec->getTemplateArgs().empty())
Craig Topper85c07002015-04-30 05:54:22 +00002140 return TokError("Class '" + CurRec->getNameInitAsString() +
2141 "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002142 } else {
2143 // If this is the first reference to this class, create and add it.
Hans Wennborgffbbd532014-11-30 00:31:49 +00002144 auto NewRec =
2145 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
Craig Toppercdab2322014-11-29 05:52:51 +00002146 CurRec = NewRec.get();
2147 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002148 }
2149 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002150
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002151 // If there are template args, parse them.
2152 if (Lex.getCode() == tgtok::less)
2153 if (ParseTemplateArgList(CurRec))
2154 return true;
2155
2156 // Finally, parse the object body.
2157 return ParseObjectBody(CurRec);
2158}
2159
2160/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2161/// of LetRecords.
2162///
2163/// LetList ::= LetItem (',' LetItem)*
2164/// LetItem ::= ID OptionalRangeList '=' Value
2165///
2166std::vector<LetRecord> TGParser::ParseLetList() {
2167 std::vector<LetRecord> Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002168
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002169 while (1) {
2170 if (Lex.getCode() != tgtok::Id) {
2171 TokError("expected identifier in let definition");
2172 return std::vector<LetRecord>();
2173 }
2174 std::string Name = Lex.getCurStrVal();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002175 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002176 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002177
2178 // Check for an optional RangeList.
2179 std::vector<unsigned> Bits;
Bob Wilson7248f862009-11-22 04:24:42 +00002180 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002181 return std::vector<LetRecord>();
2182 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002183
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002184 if (Lex.getCode() != tgtok::equal) {
2185 TokError("expected '=' in let expression");
2186 return std::vector<LetRecord>();
2187 }
2188 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002189
Craig Topper011817a2014-04-09 04:50:04 +00002190 Init *Val = ParseValue(nullptr);
2191 if (!Val) return std::vector<LetRecord>();
Bob Wilson7248f862009-11-22 04:24:42 +00002192
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002193 // Now that we have everything, add the record.
2194 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson7248f862009-11-22 04:24:42 +00002195
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002196 if (Lex.getCode() != tgtok::comma)
2197 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002198 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002199 }
2200}
2201
2202/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002203/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002204///
2205/// Object ::= LET LetList IN '{' ObjectList '}'
2206/// Object ::= LET LetList IN Object
2207///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002208bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002209 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2210 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002211
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002212 // Add this entry to the let stack.
2213 std::vector<LetRecord> LetInfo = ParseLetList();
2214 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002215 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002216
2217 if (Lex.getCode() != tgtok::In)
2218 return TokError("expected 'in' at end of top-level 'let'");
2219 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002220
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002221 // If this is a scalar let, just handle it now
2222 if (Lex.getCode() != tgtok::l_brace) {
2223 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002224 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002225 return true;
2226 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002227 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002228 // Otherwise, this is a group let.
2229 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002230
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002231 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002232 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002233 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002234
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002235 if (Lex.getCode() != tgtok::r_brace) {
2236 TokError("expected '}' at end of top level let command");
2237 return Error(BraceLoc, "to match this '{'");
2238 }
2239 Lex.Lex();
2240 }
Bob Wilson7248f862009-11-22 04:24:42 +00002241
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002242 // Outside this let scope, this let block is not active.
2243 LetStack.pop_back();
2244 return false;
2245}
2246
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002247/// ParseMultiClass - Parse a multiclass definition.
2248///
Bob Wilson3d948162009-04-28 19:41:44 +00002249/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002250/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2251/// MultiClassObject ::= DefInst
2252/// MultiClassObject ::= MultiClassInst
2253/// MultiClassObject ::= DefMInst
2254/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2255/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002256///
2257bool TGParser::ParseMultiClass() {
2258 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2259 Lex.Lex(); // Eat the multiclass token.
2260
2261 if (Lex.getCode() != tgtok::Id)
2262 return TokError("expected identifier after multiclass for name");
2263 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002264
Craig Topper7adf2bf2014-12-11 05:25:30 +00002265 auto Result =
2266 MultiClasses.insert(std::make_pair(Name,
2267 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2268
2269 if (!Result.second)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002270 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002271
Craig Topper7adf2bf2014-12-11 05:25:30 +00002272 CurMultiClass = Result.first->second.get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002273 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002274
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002275 // If there are template args, parse them.
2276 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002277 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002278 return true;
2279
David Greene7049e792009-04-24 16:55:41 +00002280 bool inherits = false;
2281
David Greene753ed8f2009-04-22 16:42:54 +00002282 // If there are submulticlasses, parse them.
2283 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002284 inherits = true;
2285
David Greene753ed8f2009-04-22 16:42:54 +00002286 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002287
David Greene753ed8f2009-04-22 16:42:54 +00002288 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002289 SubMultiClassReference SubMultiClass =
2290 ParseSubMultiClassReference(CurMultiClass);
David Greene753ed8f2009-04-22 16:42:54 +00002291 while (1) {
2292 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002293 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002294
David Greene753ed8f2009-04-22 16:42:54 +00002295 // Add it.
2296 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2297 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002298
David Greene753ed8f2009-04-22 16:42:54 +00002299 if (Lex.getCode() != tgtok::comma) break;
2300 Lex.Lex(); // eat ','.
2301 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2302 }
2303 }
2304
David Greene7049e792009-04-24 16:55:41 +00002305 if (Lex.getCode() != tgtok::l_brace) {
2306 if (!inherits)
2307 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002308 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002309 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002310 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002311 } else {
David Greene7049e792009-04-24 16:55:41 +00002312 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2313 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002314
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002315 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002316 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002317 default:
2318 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
2319 case tgtok::Let:
2320 case tgtok::Def:
2321 case tgtok::Defm:
2322 case tgtok::Foreach:
2323 if (ParseObject(CurMultiClass))
2324 return true;
2325 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002326 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002327 }
David Greene7049e792009-04-24 16:55:41 +00002328 Lex.Lex(); // eat the '}'.
2329 }
Bob Wilson7248f862009-11-22 04:24:42 +00002330
Craig Topper011817a2014-04-09 04:50:04 +00002331 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002332 return false;
2333}
2334
David Greenedb445972011-10-05 22:42:07 +00002335Record *TGParser::
2336InstantiateMulticlassDef(MultiClass &MC,
2337 Record *DefProto,
Hal Finkelf2a0b2b2014-01-02 19:35:33 +00002338 Init *&DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002339 SMRange DefmPrefixRange) {
David Greene5d5d88c2011-10-19 13:04:31 +00002340 // We need to preserve DefProto so it can be reused for later
2341 // instantiations, so create a new Record to inherit from it.
2342
David Greenedb445972011-10-05 22:42:07 +00002343 // Add in the defm name. If the defm prefix is empty, give each
2344 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2345 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2346 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002347
Jordan Roseabdd99b2013-01-10 18:50:05 +00002348 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002349 if (!DefmPrefix) {
David Greene5d5d88c2011-10-19 13:04:31 +00002350 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002351 IsAnonymous = true;
2352 }
David Greene5d5d88c2011-10-19 13:04:31 +00002353
2354 Init *DefName = DefProto->getNameInit();
2355
Sean Silvafb509ed2012-10-10 20:24:43 +00002356 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002357
Craig Topper011817a2014-04-09 04:50:04 +00002358 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002359 // We have a fully expanded string so there are no operators to
2360 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002361 DefName =
2362 BinOpInit::get(BinOpInit::STRCONCAT,
2363 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2364 StringRecTy::get())->Fold(DefProto, &MC),
2365 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2366 }
David Greene5d5d88c2011-10-19 13:04:31 +00002367
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002368 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002369 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002370 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Craig Topper84138712014-11-29 05:31:10 +00002371 auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002372
2373 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002374 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002375 Ref.Rec = DefProto;
Craig Topper84138712014-11-29 05:31:10 +00002376 AddSubClass(CurRec.get(), Ref);
David Greenedb445972011-10-05 22:42:07 +00002377
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002378 // Set the value for NAME. We don't resolve references to it 'til later,
2379 // though, so that uses in nested multiclass names don't get
2380 // confused.
Craig Topper84138712014-11-29 05:31:10 +00002381 if (SetValue(CurRec.get(), Ref.RefRange.Start, "NAME",
2382 std::vector<unsigned>(), DefmPrefix)) {
Craig Topper85c07002015-04-30 05:54:22 +00002383 Error(DefmPrefixRange.Start, "Could not resolve " +
2384 CurRec->getNameInitAsString() + ":NAME to '" +
2385 DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002386 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002387 }
David Greene8bf0d722011-10-19 13:04:35 +00002388
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002389 // If the DefNameString didn't resolve, we probably have a reference to
2390 // NAME and need to replace it. We need to do at least this much greedily,
2391 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002392 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002393 RecordVal *DefNameRV = CurRec->getValue("NAME");
2394 CurRec->resolveReferencesTo(DefNameRV);
2395 }
2396
2397 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002398 // Now that we're at the top level, resolve all NAME references
2399 // in the resultant defs that weren't in the def names themselves.
2400 RecordVal *DefNameRV = CurRec->getValue("NAME");
2401 CurRec->resolveReferencesTo(DefNameRV);
2402
2403 // Now that NAME references are resolved and we're at the top level of
2404 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002405 // currently in a multiclass, it means this defm appears inside a
2406 // multiclass and its name won't be fully resolvable until we see
2407 // the top-level defm. Therefore, we don't add this to the
2408 // RecordKeeper at this point. If we did we could get duplicate
2409 // defs as more than one probably refers to NAME or some other
2410 // common internal placeholder.
2411
2412 // Ensure redefinition doesn't happen.
2413 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002414 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002415 "' already defined, instantiating defm with subdef '" +
2416 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002417 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002418 }
2419
Craig Topper84138712014-11-29 05:31:10 +00002420 Record *CurRecSave = CurRec.get(); // Keep a copy before we release.
Craig Toppercdab2322014-11-29 05:52:51 +00002421 Records.addDef(std::move(CurRec));
Craig Topper84138712014-11-29 05:31:10 +00002422 return CurRecSave;
David Greene8bf0d722011-10-19 13:04:35 +00002423 }
2424
Craig Topper84138712014-11-29 05:31:10 +00002425 // FIXME This is bad but the ownership transfer to caller is pretty messy.
2426 // The unique_ptr in this function at least protects the exits above.
2427 return CurRec.release();
David Greenedb445972011-10-05 22:42:07 +00002428}
2429
2430bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2431 Record *CurRec,
2432 SMLoc DefmPrefixLoc,
2433 SMLoc SubClassLoc,
David Greenedb10e692011-10-19 13:02:42 +00002434 const std::vector<Init *> &TArgs,
David Greenedb445972011-10-05 22:42:07 +00002435 std::vector<Init *> &TemplateVals,
2436 bool DeleteArgs) {
2437 // Loop over all of the template arguments, setting them to the specified
2438 // value or leaving them as the default if necessary.
2439 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2440 // Check if a value is specified for this temp-arg.
2441 if (i < TemplateVals.size()) {
2442 // Set it now.
2443 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2444 TemplateVals[i]))
2445 return true;
2446
2447 // Resolve it next.
2448 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2449
2450 if (DeleteArgs)
2451 // Now remove it.
2452 CurRec->removeValue(TArgs[i]);
2453
2454 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Craig Topper85c07002015-04-30 05:54:22 +00002455 return Error(SubClassLoc, "value not specified for template argument #" +
2456 utostr(i) + " (" + TArgs[i]->getAsUnquotedString() +
2457 ") of multiclassclass '" + MC.Rec.getNameInitAsString() +
2458 "'");
David Greenedb445972011-10-05 22:42:07 +00002459 }
2460 }
2461 return false;
2462}
2463
2464bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2465 Record *CurRec,
2466 Record *DefProto,
2467 SMLoc DefmPrefixLoc) {
2468 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002469 if (ApplyLetStack(CurRec))
2470 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002471
David Greenedb445972011-10-05 22:42:07 +00002472 // Don't create a top level definition for defm inside multiclasses,
2473 // instead, only update the prototypes and bind the template args
2474 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002475 if (!CurMultiClass)
2476 return false;
Craig Toppereb4d7c62015-04-29 04:43:36 +00002477 for (const auto &Proto : CurMultiClass->DefPrototypes)
2478 if (Proto->getNameInit() == CurRec->getNameInit())
Sean Silvacc951b22013-01-09 05:28:12 +00002479 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2480 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002481 CurMultiClass->DefPrototypes.push_back(std::unique_ptr<Record>(CurRec));
David Greenedb445972011-10-05 22:42:07 +00002482
Sean Silvacc951b22013-01-09 05:28:12 +00002483 // Copy the template arguments for the multiclass into the new def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002484 for (Init * TA : CurMultiClass->Rec.getTemplateArgs()) {
2485 const RecordVal *RV = CurMultiClass->Rec.getValue(TA);
Sean Silvacc951b22013-01-09 05:28:12 +00002486 assert(RV && "Template arg doesn't exist?");
2487 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002488 }
2489
2490 return false;
2491}
2492
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002493/// ParseDefm - Parse the instantiation of a multiclass.
2494///
2495/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2496///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002497bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002498 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002499 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002500 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002501
Craig Topperb21afc62013-01-07 05:09:33 +00002502 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002503 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002504 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002505
Jordan Rosef12e8a92013-01-10 18:50:11 +00002506 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002507 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002508 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002509
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002510 // Keep track of the new generated record definitions.
2511 std::vector<Record*> NewRecDefs;
2512
2513 // This record also inherits from a regular class (non-multiclass)?
2514 bool InheritFromClass = false;
2515
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002516 // eat the colon.
2517 Lex.Lex();
2518
Chris Lattner526c8cb2009-06-21 03:39:35 +00002519 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002520 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002521
2522 while (1) {
Craig Topper011817a2014-04-09 04:50:04 +00002523 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002524
2525 // To instantiate a multiclass, we need to first get the multiclass, then
2526 // instantiate each def contained in the multiclass with the SubClassRef
2527 // template parameters.
Craig Topper7adf2bf2014-12-11 05:25:30 +00002528 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper4ca40012014-11-30 01:20:17 +00002529 assert(MC && "Didn't lookup multiclass correctly?");
David Greeneaf8ee2c2011-07-29 22:43:06 +00002530 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002531
2532 // Verify that the correct number of template arguments were specified.
David Greenedb10e692011-10-19 13:02:42 +00002533 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002534 if (TArgs.size() < TemplateVals.size())
2535 return Error(SubClassLoc,
2536 "more template args specified than multiclass expects");
2537
2538 // Loop over all the def's in the multiclass, instantiating each one.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002539 for (const std::unique_ptr<Record> &DefProto : MC->DefPrototypes) {
2540 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto.get(), DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002541 SMRange(DefmLoc,
2542 DefmPrefixEndLoc));
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002543 if (!CurRec)
2544 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002545
Jordan Rosef12e8a92013-01-10 18:50:11 +00002546 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002547 TArgs, TemplateVals, true/*Delete args*/))
2548 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002549
Craig Toppereb4d7c62015-04-29 04:43:36 +00002550 if (ResolveMulticlassDef(*MC, CurRec, DefProto.get(), DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002551 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002552
Adam Nemete5a07162014-09-16 17:14:13 +00002553 // Defs that can be used by other definitions should be fully resolved
2554 // before any use.
2555 if (DefProto->isResolveFirst() && !CurMultiClass) {
2556 CurRec->resolveReferences();
2557 CurRec->setResolveFirst(false);
2558 }
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002559 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002560 }
2561
David Greenedb445972011-10-05 22:42:07 +00002562
David Greenef00919a2009-04-22 22:17:51 +00002563 if (Lex.getCode() != tgtok::comma) break;
2564 Lex.Lex(); // eat ','.
2565
Craig Topper998a39a2013-08-20 04:22:09 +00002566 if (Lex.getCode() != tgtok::Id)
2567 return TokError("expected identifier");
2568
David Greenef00919a2009-04-22 22:17:51 +00002569 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002570
2571 // A defm can inherit from regular classes (non-multiclass) as
2572 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002573 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002574
2575 if (InheritFromClass)
2576 break;
2577
Craig Topper011817a2014-04-09 04:50:04 +00002578 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002579 }
2580
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002581 if (InheritFromClass) {
2582 // Process all the classes to inherit as if they were part of a
2583 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002584 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002585 while (1) {
2586 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002587 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002588
2589 // Get the expanded definition prototypes and teach them about
2590 // the record values the current class to inherit has
Craig Toppereb4d7c62015-04-29 04:43:36 +00002591 for (Record *CurRec : NewRecDefs) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002592 // Add it.
2593 if (AddSubClass(CurRec, SubClass))
2594 return true;
2595
Sean Silvacb1a75e2013-01-09 04:49:14 +00002596 if (ApplyLetStack(CurRec))
2597 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002598 }
2599
2600 if (Lex.getCode() != tgtok::comma) break;
2601 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002602 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002603 }
2604 }
2605
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002606 if (!CurMultiClass)
Craig Toppereb4d7c62015-04-29 04:43:36 +00002607 for (Record *CurRec : NewRecDefs)
David Greene50c09122011-08-10 18:27:46 +00002608 // See Record::setName(). This resolve step will see any new
2609 // name for the def that might have been created when resolving
2610 // inheritance, values and arguments above.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002611 CurRec->resolveReferences();
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002612
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002613 if (Lex.getCode() != tgtok::semi)
2614 return TokError("expected ';' at end of defm");
2615 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002616
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002617 return false;
2618}
2619
2620/// ParseObject
2621/// Object ::= ClassInst
2622/// Object ::= DefInst
2623/// Object ::= MultiClassInst
2624/// Object ::= DefMInst
2625/// Object ::= LETCommand '{' ObjectList '}'
2626/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002627bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002628 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002629 default:
2630 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002631 case tgtok::Let: return ParseTopLevelLet(MC);
2632 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002633 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002634 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002635 case tgtok::Class: return ParseClass();
2636 case tgtok::MultiClass: return ParseMultiClass();
2637 }
2638}
2639
2640/// ParseObjectList
2641/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002642bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002643 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002644 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002645 return true;
2646 }
2647 return false;
2648}
2649
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002650bool TGParser::ParseFile() {
2651 Lex.Lex(); // Prime the lexer.
2652 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002653
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002654 // If we have unread input at the end of the file, report it.
2655 if (Lex.getCode() == tgtok::Eof)
2656 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002657
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002658 return TokError("Unexpected input at top level");
2659}
2660