blob: 3730172934835cb8350ea0154813ff87d032c618 [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)
David Greene3ca42122011-10-19 13:02:39 +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)
David Greene3ca42122011-10-19 13:02:39 +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.
Sean Silvafb509ed2012-10-10 20:24:43 +0000115 BitsInit *BInit = dyn_cast<BitsInit>(BI);
Craig Toppere73658d2014-04-28 04:05:08 +0000116 assert(BInit != nullptr);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000117
David Greeneaf8ee2c2011-07-29 22:43:06 +0000118 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000119
120 // Loop over bits, assigning values as appropriate.
121 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
122 unsigned Bit = BitList[i];
David Greeneb3da8122011-07-29 19:07:00 +0000123 if (NewBits[Bit])
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000124 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
David Greene3ca42122011-10-19 13:02:39 +0000125 ValName->getAsUnquotedString() + "' more than once");
David Greeneb3da8122011-07-29 19:07:00 +0000126 NewBits[Bit] = BInit->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000127 }
128
129 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
Craig Topper011817a2014-04-09 04:50:04 +0000130 if (!NewBits[i])
David Greeneb3da8122011-07-29 19:07:00 +0000131 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000132
David Greenee32ebf22011-07-29 19:07:07 +0000133 V = BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000134 }
135
Pete Cooper040c6a62014-07-31 01:43:57 +0000136 if (RV->setValue(V)) {
137 std::string InitType = "";
138 if (BitsInit *BI = dyn_cast<BitsInit>(V)) {
139 InitType = (Twine("' of type bit initializer with length ") +
140 Twine(BI->getNumBits())).str();
141 }
David Greene3ca42122011-10-19 13:02:39 +0000142 return Error(Loc, "Value '" + ValName->getAsUnquotedString() + "' of type '"
143 + RV->getType()->getAsString() +
144 "' is incompatible with initializer '" + V->getAsString()
Pete Cooper040c6a62014-07-31 01:43:57 +0000145 + InitType
David Greene3ca42122011-10-19 13:02:39 +0000146 + "'");
Pete Cooper040c6a62014-07-31 01:43:57 +0000147 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000148 return false;
149}
150
151/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
152/// args as SubClass's template arguments.
Cedric Venetd1e179d2009-02-14 16:06:42 +0000153bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000154 Record *SC = SubClass.Rec;
155 // Add all of the values in the subclass into the current class.
156 const std::vector<RecordVal> &Vals = SC->getValues();
157 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
Jordan Rosef12e8a92013-01-10 18:50:11 +0000158 if (AddValue(CurRec, SubClass.RefRange.Start, Vals[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000159 return true;
160
David Greenedb10e692011-10-19 13:02:42 +0000161 const std::vector<Init *> &TArgs = SC->getTemplateArgs();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000162
163 // Ensure that an appropriate number of template arguments are specified.
164 if (TArgs.size() < SubClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000165 return Error(SubClass.RefRange.Start,
166 "More template args specified than expected");
Bob Wilson7248f862009-11-22 04:24:42 +0000167
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000168 // Loop over all of the template arguments, setting them to the specified
169 // value or leaving them as the default if necessary.
170 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
171 if (i < SubClass.TemplateArgs.size()) {
172 // If a value is specified for this template arg, set it now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000173 if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
174 std::vector<unsigned>(), SubClass.TemplateArgs[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000175 return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000176
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000177 // Resolve it next.
178 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson7248f862009-11-22 04:24:42 +0000179
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000180 // Now remove it.
181 CurRec->removeValue(TArgs[i]);
182
183 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000184 return Error(SubClass.RefRange.Start,
185 "Value not specified for template argument #"
David Greenedb10e692011-10-19 13:02:42 +0000186 + utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
187 + ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000188 }
189 }
190
191 // Since everything went well, we can now set the "superclass" list for the
192 // current record.
193 const std::vector<Record*> &SCs = SC->getSuperClasses();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000194 ArrayRef<SMRange> SCRanges = SC->getSuperClassRanges();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000195 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
196 if (CurRec->isSubClassOf(SCs[i]))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000197 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000198 "Already subclass of '" + SCs[i]->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000199 CurRec->addSuperClass(SCs[i], SCRanges[i]);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000200 }
Bob Wilson7248f862009-11-22 04:24:42 +0000201
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000202 if (CurRec->isSubClassOf(SC))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000203 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000204 "Already subclass of '" + SC->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000205 CurRec->addSuperClass(SC, SubClass.RefRange);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000206 return false;
207}
208
David Greene753ed8f2009-04-22 16:42:54 +0000209/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilsonf71e6562009-04-30 18:26:19 +0000210/// CurMC, resolving its template args as SubMultiClass's
David Greene753ed8f2009-04-22 16:42:54 +0000211/// template arguments.
Bob Wilsonf71e6562009-04-30 18:26:19 +0000212bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson92ab8202009-04-30 17:46:20 +0000213 SubMultiClassReference &SubMultiClass) {
David Greene753ed8f2009-04-22 16:42:54 +0000214 MultiClass *SMC = SubMultiClass.MC;
Bob Wilsonf71e6562009-04-30 18:26:19 +0000215 Record *CurRec = &CurMC->Rec;
David Greene753ed8f2009-04-22 16:42:54 +0000216
David Greene753ed8f2009-04-22 16:42:54 +0000217 // Add all of the values in the subclass into the current class.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000218 for (const auto &SMCVal : SMC->Rec.getValues())
219 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000220 return true;
221
Craig Toppera4ea4b02014-11-30 00:24:32 +0000222 unsigned newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000223
David Greene753ed8f2009-04-22 16:42:54 +0000224 // Add all of the defs in the subclass into the current multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000225 for (const std::unique_ptr<Record> &R : SMC->DefPrototypes) {
David Greene753ed8f2009-04-22 16:42:54 +0000226 // Clone the def and add it to the current multiclass
Craig Toppereb4d7c62015-04-29 04:43:36 +0000227 auto NewDef = make_unique<Record>(*R);
David Greene753ed8f2009-04-22 16:42:54 +0000228
229 // Add all of the values in the superclass into the current def.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000230 for (const auto &MCVal : CurRec->getValues())
231 if (AddValue(NewDef.get(), SubMultiClass.RefRange.Start, MCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000232 return true;
233
Craig Topperc3504c42014-12-11 05:25:33 +0000234 CurMC->DefPrototypes.push_back(std::move(NewDef));
David Greene753ed8f2009-04-22 16:42:54 +0000235 }
Bob Wilson3d948162009-04-28 19:41:44 +0000236
David Greenedb10e692011-10-19 13:02:42 +0000237 const std::vector<Init *> &SMCTArgs = SMC->Rec.getTemplateArgs();
David Greene753ed8f2009-04-22 16:42:54 +0000238
David Greene7049e792009-04-24 16:55:41 +0000239 // Ensure that an appropriate number of template arguments are
240 // specified.
David Greene753ed8f2009-04-22 16:42:54 +0000241 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000242 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000243 "More template args specified than expected");
Bob Wilson3d948162009-04-28 19:41:44 +0000244
David Greene753ed8f2009-04-22 16:42:54 +0000245 // Loop over all of the template arguments, setting them to the specified
246 // value or leaving them as the default if necessary.
247 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
248 if (i < SubMultiClass.TemplateArgs.size()) {
David Greene7049e792009-04-24 16:55:41 +0000249 // If a value is specified for this template arg, set it in the
250 // superclass now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000251 if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson3d948162009-04-28 19:41:44 +0000252 std::vector<unsigned>(),
David Greene753ed8f2009-04-22 16:42:54 +0000253 SubMultiClass.TemplateArgs[i]))
254 return true;
255
256 // Resolve it next.
257 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson3d948162009-04-28 19:41:44 +0000258
David Greene753ed8f2009-04-22 16:42:54 +0000259 // Now remove it.
260 CurRec->removeValue(SMCTArgs[i]);
261
David Greene7049e792009-04-24 16:55:41 +0000262 // If a value is specified for this template arg, set it in the
263 // new defs now.
Craig Topperc3504c42014-12-11 05:25:33 +0000264 for (const auto &Def :
265 makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
266 if (SetValue(Def.get(), SubMultiClass.RefRange.Start, SMCTArgs[i],
Bob Wilson3d948162009-04-28 19:41:44 +0000267 std::vector<unsigned>(),
David Greene753ed8f2009-04-22 16:42:54 +0000268 SubMultiClass.TemplateArgs[i]))
269 return true;
270
271 // Resolve it next.
272 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
273
274 // Now remove it
275 Def->removeValue(SMCTArgs[i]);
276 }
277 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000278 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000279 "Value not specified for template argument #"
David Greenedb10e692011-10-19 13:02:42 +0000280 + utostr(i) + " (" + SMCTArgs[i]->getAsUnquotedString()
281 + ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000282 }
283 }
284
285 return false;
286}
287
David Greenefb927af2012-02-22 16:09:41 +0000288/// ProcessForeachDefs - Given a record, apply all of the variable
289/// values in all surrounding foreach loops, creating new records for
290/// each combination of values.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000291bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
292 if (Loops.empty())
293 return false;
294
David Greenefb927af2012-02-22 16:09:41 +0000295 // We want to instantiate a new copy of CurRec for each combination
296 // of nested loop iterator values. We don't want top instantiate
297 // any copies until we have values for each loop iterator.
298 IterSet IterVals;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000299 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000300}
301
302/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
303/// apply each of the variable values in this loop and then process
304/// subloops.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000305bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
306 // Recursively build a tuple of iterator values.
307 if (IterVals.size() != Loops.size()) {
308 assert(IterVals.size() < Loops.size());
309 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silvafb509ed2012-10-10 20:24:43 +0000310 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Craig Topper011817a2014-04-09 04:50:04 +0000311 if (!List) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000312 Error(Loc, "Loop list is not a list");
313 return true;
314 }
David Greenefb927af2012-02-22 16:09:41 +0000315
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000316 // Process each value.
317 for (int64_t i = 0; i < List->getSize(); ++i) {
Craig Topper011817a2014-04-09 04:50:04 +0000318 Init *ItemVal = List->resolveListElementReference(*CurRec, nullptr, i);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000319 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
320 if (ProcessForeachDefs(CurRec, Loc, IterVals))
321 return true;
322 IterVals.pop_back();
323 }
324 return false;
325 }
326
327 // This is the bottom of the recursion. We have all of the iterator values
328 // for this point in the iteration space. Instantiate a new record to
329 // reflect this combination of values.
Craig Topper84138712014-11-29 05:31:10 +0000330 auto IterRec = make_unique<Record>(*CurRec);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000331
332 // Set the iterator values now.
333 for (unsigned i = 0, e = IterVals.size(); i != e; ++i) {
334 VarInit *IterVar = IterVals[i].IterVar;
Sean Silvafb509ed2012-10-10 20:24:43 +0000335 TypedInit *IVal = dyn_cast<TypedInit>(IterVals[i].IterValue);
Craig Topper84138712014-11-29 05:31:10 +0000336 if (!IVal)
337 return Error(Loc, "foreach iterator value is untyped");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000338
339 IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false));
340
Craig Topper84138712014-11-29 05:31:10 +0000341 if (SetValue(IterRec.get(), Loc, IterVar->getName(),
342 std::vector<unsigned>(), IVal))
343 return Error(Loc, "when instantiating this def");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000344
345 // Resolve it next.
346 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName()));
347
348 // Remove it.
349 IterRec->removeValue(IterVar->getName());
350 }
351
352 if (Records.getDef(IterRec->getNameInitAsString())) {
Artyom Skrobov8b985322014-06-10 12:41:14 +0000353 // If this record is anonymous, it's no problem, just generate a new name
Craig Topper84138712014-11-29 05:31:10 +0000354 if (!IterRec->isAnonymous())
355 return Error(Loc, "def already exists: " +IterRec->getNameInitAsString());
356
357 IterRec->setName(GetNewAnonymousName());
David Greenefb927af2012-02-22 16:09:41 +0000358 }
359
Craig Topper84138712014-11-29 05:31:10 +0000360 Record *IterRecSave = IterRec.get(); // Keep a copy before release.
Craig Toppercdab2322014-11-29 05:52:51 +0000361 Records.addDef(std::move(IterRec));
Craig Topper84138712014-11-29 05:31:10 +0000362 IterRecSave->resolveReferences();
David Greenefb927af2012-02-22 16:09:41 +0000363 return false;
364}
365
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000366//===----------------------------------------------------------------------===//
367// Parser Code
368//===----------------------------------------------------------------------===//
369
370/// isObjectStart - Return true if this is a valid first token for an Object.
371static bool isObjectStart(tgtok::TokKind K) {
372 return K == tgtok::Class || K == tgtok::Def ||
David Greenefb927af2012-02-22 16:09:41 +0000373 K == tgtok::Defm || K == tgtok::Let ||
374 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000375}
376
Alp Tokerce91fe52013-12-21 18:51:00 +0000377/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
378/// an identifier.
379std::string TGParser::GetNewAnonymousName() {
Aaron Ballman97a59fb2015-02-16 19:33:36 +0000380 return "anonymous_" + utostr(AnonCounter++);
Chris Lattner7538ed82010-10-05 22:51:56 +0000381}
382
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000383/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000384/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000385/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000386/// ObjectName ::= /*empty*/
387///
David Greene2affd672011-10-19 13:04:29 +0000388Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
389 switch (Lex.getCode()) {
390 case tgtok::colon:
391 case tgtok::semi:
392 case tgtok::l_brace:
393 // These are all of the tokens that can begin an object body.
394 // Some of these can also begin values but we disallow those cases
395 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000396 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000397 default:
398 break;
399 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000400
Craig Topper011817a2014-04-09 04:50:04 +0000401 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000402 if (CurMultiClass)
403 CurRec = &CurMultiClass->Rec;
404
Craig Topper011817a2014-04-09 04:50:04 +0000405 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000406 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000407 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000408 if (!CurRecName) {
409 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000410 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000411 }
412 Type = CurRecName->getType();
413 }
414
415 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000416}
417
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000418/// ParseClassID - Parse and resolve a reference to a class name. This returns
419/// null on error.
420///
421/// ClassID ::= ID
422///
423Record *TGParser::ParseClassID() {
424 if (Lex.getCode() != tgtok::Id) {
425 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000426 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000427 }
Bob Wilson7248f862009-11-22 04:24:42 +0000428
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000429 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000430 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000431 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000432
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000433 Lex.Lex();
434 return Result;
435}
436
Bob Wilson3d948162009-04-28 19:41:44 +0000437/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
438/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000439///
440/// MultiClassID ::= ID
441///
442MultiClass *TGParser::ParseMultiClassID() {
443 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000444 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000445 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000446 }
Bob Wilson3d948162009-04-28 19:41:44 +0000447
Craig Topper7adf2bf2014-12-11 05:25:30 +0000448 MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
Craig Topper4ca40012014-11-30 01:20:17 +0000449 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000450 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000451
David Greene753ed8f2009-04-22 16:42:54 +0000452 Lex.Lex();
Craig Topper4ca40012014-11-30 01:20:17 +0000453 return Result;
David Greene753ed8f2009-04-22 16:42:54 +0000454}
455
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000456/// ParseSubClassReference - Parse a reference to a subclass or to a templated
457/// subclass. This returns a SubClassRefTy with a null Record* on error.
458///
459/// SubClassRef ::= ClassID
460/// SubClassRef ::= ClassID '<' ValueList '>'
461///
462SubClassReference TGParser::
463ParseSubClassReference(Record *CurRec, bool isDefm) {
464 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000465 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000466
Sean Silva0657b402013-01-09 02:17:14 +0000467 if (isDefm) {
468 if (MultiClass *MC = ParseMultiClassID())
469 Result.Rec = &MC->Rec;
470 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000471 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000472 }
Craig Topper011817a2014-04-09 04:50:04 +0000473 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000474
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000475 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000476 if (Lex.getCode() != tgtok::less) {
477 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000478 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000479 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000480 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000481
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000482 if (Lex.getCode() == tgtok::greater) {
483 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000484 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000485 return Result;
486 }
Bob Wilson7248f862009-11-22 04:24:42 +0000487
David Greene8618f952009-06-08 20:23:18 +0000488 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000489 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000490 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000491 return Result;
492 }
Bob Wilson7248f862009-11-22 04:24:42 +0000493
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000494 if (Lex.getCode() != tgtok::greater) {
495 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000496 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000497 return Result;
498 }
499 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000500 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000501
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000502 return Result;
503}
504
Bob Wilson3d948162009-04-28 19:41:44 +0000505/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
506/// templated submulticlass. This returns a SubMultiClassRefTy with a null
507/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000508///
509/// SubMultiClassRef ::= MultiClassID
510/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
511///
512SubMultiClassReference TGParser::
513ParseSubMultiClassReference(MultiClass *CurMC) {
514 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000515 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000516
David Greene753ed8f2009-04-22 16:42:54 +0000517 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000518 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000519
David Greene753ed8f2009-04-22 16:42:54 +0000520 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000521 if (Lex.getCode() != tgtok::less) {
522 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000523 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000524 }
David Greene753ed8f2009-04-22 16:42:54 +0000525 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000526
David Greene753ed8f2009-04-22 16:42:54 +0000527 if (Lex.getCode() == tgtok::greater) {
528 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000529 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000530 return Result;
531 }
Bob Wilson3d948162009-04-28 19:41:44 +0000532
David Greene8618f952009-06-08 20:23:18 +0000533 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000534 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000535 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000536 return Result;
537 }
Bob Wilson3d948162009-04-28 19:41:44 +0000538
David Greene753ed8f2009-04-22 16:42:54 +0000539 if (Lex.getCode() != tgtok::greater) {
540 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000541 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000542 return Result;
543 }
544 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000545 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000546
547 return Result;
548}
549
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000550/// ParseRangePiece - Parse a bit/value range.
551/// RangePiece ::= INTVAL
552/// RangePiece ::= INTVAL '-' INTVAL
553/// RangePiece ::= INTVAL INTVAL
554bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000555 if (Lex.getCode() != tgtok::IntVal) {
556 TokError("expected integer or bitrange");
557 return true;
558 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000559 int64_t Start = Lex.getCurIntVal();
560 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000561
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000562 if (Start < 0)
563 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000564
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000565 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000566 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000567 Ranges.push_back(Start);
568 return false;
569 case tgtok::minus:
570 if (Lex.Lex() != tgtok::IntVal) {
571 TokError("expected integer value as end of range");
572 return true;
573 }
574 End = Lex.getCurIntVal();
575 break;
576 case tgtok::IntVal:
577 End = -Lex.getCurIntVal();
578 break;
579 }
Bob Wilson7248f862009-11-22 04:24:42 +0000580 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000581 return TokError("invalid range, cannot be negative");
582 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000583
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000584 // Add to the range.
585 if (Start < End) {
586 for (; Start <= End; ++Start)
587 Ranges.push_back(Start);
588 } else {
589 for (; Start >= End; --Start)
590 Ranges.push_back(Start);
591 }
592 return false;
593}
594
595/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
596///
597/// RangeList ::= RangePiece (',' RangePiece)*
598///
599std::vector<unsigned> TGParser::ParseRangeList() {
600 std::vector<unsigned> Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000601
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000602 // Parse the first piece.
603 if (ParseRangePiece(Result))
604 return std::vector<unsigned>();
605 while (Lex.getCode() == tgtok::comma) {
606 Lex.Lex(); // Eat the comma.
607
608 // Parse the next range piece.
609 if (ParseRangePiece(Result))
610 return std::vector<unsigned>();
611 }
612 return Result;
613}
614
615/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
616/// OptionalRangeList ::= '<' RangeList '>'
617/// OptionalRangeList ::= /*empty*/
618bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
619 if (Lex.getCode() != tgtok::less)
620 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000621
Chris Lattner526c8cb2009-06-21 03:39:35 +0000622 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000623 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000624
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000625 // Parse the range list.
626 Ranges = ParseRangeList();
627 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000628
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000629 if (Lex.getCode() != tgtok::greater) {
630 TokError("expected '>' at end of range list");
631 return Error(StartLoc, "to match this '<'");
632 }
633 Lex.Lex(); // eat the '>'.
634 return false;
635}
636
637/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
638/// OptionalBitList ::= '{' RangeList '}'
639/// OptionalBitList ::= /*empty*/
640bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
641 if (Lex.getCode() != tgtok::l_brace)
642 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000643
Chris Lattner526c8cb2009-06-21 03:39:35 +0000644 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000645 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000646
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000647 // Parse the range list.
648 Ranges = ParseRangeList();
649 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000650
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000651 if (Lex.getCode() != tgtok::r_brace) {
652 TokError("expected '}' at end of bit list");
653 return Error(StartLoc, "to match this '{'");
654 }
655 Lex.Lex(); // eat the '}'.
656 return false;
657}
658
659
660/// ParseType - Parse and return a tblgen type. This returns null on error.
661///
662/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000663/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000664/// Type ::= BIT // bit type
665/// Type ::= BITS '<' INTVAL '>' // bits<x> type
666/// Type ::= INT // int type
667/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000668/// Type ::= DAG // dag type
669/// Type ::= ClassID // Record Type
670///
671RecTy *TGParser::ParseType() {
672 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000673 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000674 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000675 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000676 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
677 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000678 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000679 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000680 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000681 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000682 case tgtok::Bits: {
683 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
684 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000685 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000686 }
687 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
688 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000689 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000690 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000691 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000692 if (Lex.Lex() != tgtok::greater) { // Eat count.
693 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000694 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000695 }
696 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000697 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000698 }
699 case tgtok::List: {
700 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
701 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000702 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000703 }
704 Lex.Lex(); // Eat '<'
705 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000706 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000707
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000708 if (Lex.getCode() != tgtok::greater) {
709 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000710 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000711 }
712 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000713 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000714 }
Bob Wilson7248f862009-11-22 04:24:42 +0000715 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000716}
717
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000718/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
719/// has already been read.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000720Init *TGParser::ParseIDValue(Record *CurRec,
David Greened4263a62011-10-19 13:04:20 +0000721 const std::string &Name, SMLoc NameLoc,
722 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000723 if (CurRec) {
724 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000725 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000726
David Greenedb10e692011-10-19 13:02:42 +0000727 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
728
David Greene47a665e2011-10-05 22:42:54 +0000729 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +0000730 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
731 "::");
David Greene47a665e2011-10-05 22:42:54 +0000732
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000733 if (CurRec->isTemplateArg(TemplateArgName)) {
734 const RecordVal *RV = CurRec->getValue(TemplateArgName);
735 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000736 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000737 }
738 }
Bob Wilson7248f862009-11-22 04:24:42 +0000739
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000740 if (CurMultiClass) {
David Greenedb10e692011-10-19 13:02:42 +0000741 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
742 "::");
743
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000744 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
745 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
746 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000747 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000748 }
749 }
Bob Wilson7248f862009-11-22 04:24:42 +0000750
David Greenefb927af2012-02-22 16:09:41 +0000751 // If this is in a foreach loop, make sure it's not a loop iterator
Craig Toppereb4d7c62015-04-29 04:43:36 +0000752 for (const auto &L : Loops) {
753 VarInit *IterVar = dyn_cast<VarInit>(L.IterVar);
David Greenefb927af2012-02-22 16:09:41 +0000754 if (IterVar && IterVar->getName() == Name)
755 return IterVar;
756 }
757
David Greene232bd602011-10-19 13:04:21 +0000758 if (Mode == ParseNameMode)
759 return StringInit::get(Name);
760
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000761 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000762 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000763
David Greene232bd602011-10-19 13:04:21 +0000764 if (Mode == ParseValueMode) {
765 Error(NameLoc, "Variable not defined: '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000766 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000767 }
768
769 return StringInit::get(Name);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000770}
771
David Greene5d0c0512009-05-14 20:54:48 +0000772/// ParseOperation - Parse an operator. This returns null on error.
773///
774/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
775///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000776Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000777 switch (Lex.getCode()) {
778 default:
779 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000780 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000781 case tgtok::XHead:
782 case tgtok::XTail:
783 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000784 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
785 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000786 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000787
David Greenee8f3b272009-05-14 21:22:49 +0000788 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000789 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000790 case tgtok::XCast:
791 Lex.Lex(); // eat the operation
792 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000793
David Greenee8f3b272009-05-14 21:22:49 +0000794 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000795
Craig Topper011817a2014-04-09 04:50:04 +0000796 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000797 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000798 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000799 }
David Greene5d0c0512009-05-14 20:54:48 +0000800
David Greenee8f3b272009-05-14 21:22:49 +0000801 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000802 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000803 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000804 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000805 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000806 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000807 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000808 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000809 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000810 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000811 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000812 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000813 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000814 break;
David Greenee8f3b272009-05-14 21:22:49 +0000815 }
816 if (Lex.getCode() != tgtok::l_paren) {
817 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000818 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000819 }
820 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000821
David Greeneaf8ee2c2011-07-29 22:43:06 +0000822 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000823 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000824
David Greene2f7cf7f2011-01-07 17:05:37 +0000825 if (Code == UnOpInit::HEAD
826 || Code == UnOpInit::TAIL
827 || Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000828 ListInit *LHSl = dyn_cast<ListInit>(LHS);
829 StringInit *LHSs = dyn_cast<StringInit>(LHS);
830 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000831 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000832 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000833 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000834 }
835 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000836 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
837 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000838 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000839 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000840 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000841 }
842 }
843
David Greene2f7cf7f2011-01-07 17:05:37 +0000844 if (Code == UnOpInit::HEAD
845 || Code == UnOpInit::TAIL) {
Craig Topper011817a2014-04-09 04:50:04 +0000846 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000847 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000848 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000849 }
Bob Wilson7248f862009-11-22 04:24:42 +0000850
David Greened571b3c2009-05-14 22:38:31 +0000851 if (LHSl && LHSl->getSize() == 0) {
852 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000853 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000854 }
855 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000856 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000857 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000858 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000859 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000860 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000861 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000862 if (Code == UnOpInit::HEAD) {
David Greened571b3c2009-05-14 22:38:31 +0000863 Type = Itemt->getType();
Bob Wilson7248f862009-11-22 04:24:42 +0000864 } else {
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000865 Type = ListRecTy::get(Itemt->getType());
David Greened571b3c2009-05-14 22:38:31 +0000866 }
Bob Wilson7248f862009-11-22 04:24:42 +0000867 } else {
David Greened571b3c2009-05-14 22:38:31 +0000868 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000869 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000870 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000871 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000872 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000873 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000874 if (Code == UnOpInit::HEAD) {
David Greened571b3c2009-05-14 22:38:31 +0000875 Type = LType->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +0000876 } else {
David Greened571b3c2009-05-14 22:38:31 +0000877 Type = LType;
878 }
879 }
880 }
881 }
882
David Greenee8f3b272009-05-14 21:22:49 +0000883 if (Lex.getCode() != tgtok::r_paren) {
884 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000885 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000886 }
887 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000888 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000889 }
David Greene5d0c0512009-05-14 20:54:48 +0000890
891 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000892 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000893 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +0000894 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000895 case tgtok::XSRL:
896 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000897 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000898 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000899 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000900 tgtok::TokKind OpTok = Lex.getCode();
901 SMLoc OpLoc = Lex.getLoc();
902 Lex.Lex(); // eat the operation
903
David Greene5d0c0512009-05-14 20:54:48 +0000904 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000905 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000906
Chris Lattner61ea00b2010-10-05 23:58:18 +0000907 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000908 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000909 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000910 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000911 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000912 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
913 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
914 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
915 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000916 case tgtok::XListConcat:
917 Code = BinOpInit::LISTCONCAT;
918 // We don't know the list type until we parse the first argument
919 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000920 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000921 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000922 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000923 break;
David Greene5d0c0512009-05-14 20:54:48 +0000924 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000925
David Greene5d0c0512009-05-14 20:54:48 +0000926 if (Lex.getCode() != tgtok::l_paren) {
927 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000928 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000929 }
930 Lex.Lex(); // eat the '('
931
David Greeneaf8ee2c2011-07-29 22:43:06 +0000932 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000933
Chris Lattner61ea00b2010-10-05 23:58:18 +0000934 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000935 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000936
Chris Lattner61ea00b2010-10-05 23:58:18 +0000937 while (Lex.getCode() == tgtok::comma) {
938 Lex.Lex(); // eat the ','
939
940 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000941 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000942 }
David Greene5d0c0512009-05-14 20:54:48 +0000943
944 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000945 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000946 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000947 }
948 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000949
Daniel Sanders314e80e2014-05-07 10:13:19 +0000950 // If we are doing !listconcat, we should know the type by now
951 if (OpTok == tgtok::XListConcat) {
952 if (VarInit *Arg0 = dyn_cast<VarInit>(InitList[0]))
953 Type = Arg0->getType();
954 else if (ListInit *Arg0 = dyn_cast<ListInit>(InitList[0]))
955 Type = Arg0->getType();
956 else {
957 InitList[0]->dump();
958 Error(OpLoc, "expected a list");
959 return nullptr;
960 }
961 }
962
Chris Lattner61ea00b2010-10-05 23:58:18 +0000963 // We allow multiple operands to associative operators like !strconcat as
964 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000965 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000966 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000967 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000968 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
969 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000970 InitList.back() = RHS;
971 }
972 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000973
Chris Lattner61ea00b2010-10-05 23:58:18 +0000974 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000975 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000976 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000977
Chris Lattner61ea00b2010-10-05 23:58:18 +0000978 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +0000979 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000980 }
981
David Greene3587eed2009-05-14 23:26:46 +0000982 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +0000983 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +0000984 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
985 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000986 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000987
David Greene98ed3c72009-05-14 21:54:42 +0000988 tgtok::TokKind LexCode = Lex.getCode();
989 Lex.Lex(); // eat the operation
990 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +0000991 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +0000992 case tgtok::XIf:
993 Code = TernOpInit::IF;
994 break;
David Greenee917fff2009-05-14 22:23:47 +0000995 case tgtok::XForEach:
996 Code = TernOpInit::FOREACH;
997 break;
David Greene98ed3c72009-05-14 21:54:42 +0000998 case tgtok::XSubst:
999 Code = TernOpInit::SUBST;
1000 break;
1001 }
1002 if (Lex.getCode() != tgtok::l_paren) {
1003 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001004 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001005 }
1006 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001007
David Greeneaf8ee2c2011-07-29 22:43:06 +00001008 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001009 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001010
David Greene98ed3c72009-05-14 21:54:42 +00001011 if (Lex.getCode() != tgtok::comma) {
1012 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001013 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001014 }
1015 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001016
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001017 Init *MHS = ParseValue(CurRec, ItemType);
1018 if (!MHS)
1019 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001020
David Greene98ed3c72009-05-14 21:54:42 +00001021 if (Lex.getCode() != tgtok::comma) {
1022 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001023 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001024 }
1025 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001026
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001027 Init *RHS = ParseValue(CurRec, ItemType);
1028 if (!RHS)
1029 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001030
David Greene98ed3c72009-05-14 21:54:42 +00001031 if (Lex.getCode() != tgtok::r_paren) {
1032 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001033 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001034 }
1035 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001036
David Greene98ed3c72009-05-14 21:54:42 +00001037 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001038 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001039 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001040 RecTy *MHSTy = nullptr;
1041 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001042
Sean Silvafb509ed2012-10-10 20:24:43 +00001043 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001044 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001045 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001046 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001047 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001048 MHSTy = BitRecTy::get();
1049
Sean Silvafb509ed2012-10-10 20:24:43 +00001050 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001051 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001052 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001053 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001054 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001055 RHSTy = BitRecTy::get();
1056
1057 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001058 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001059 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001060 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001061 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001062
1063 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001064 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001065 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001066 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001067
1068 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1069 Type = RHSTy;
1070 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1071 Type = MHSTy;
Bob Wilson7248f862009-11-22 04:24:42 +00001072 } else {
David Greene3587eed2009-05-14 23:26:46 +00001073 TokError("inconsistent types for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001074 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001075 }
1076 break;
1077 }
David Greenee917fff2009-05-14 22:23:47 +00001078 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001079 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
Craig Topper011817a2014-04-09 04:50:04 +00001080 if (!MHSt) {
David Greenee917fff2009-05-14 22:23:47 +00001081 TokError("could not get type for !foreach");
Craig Topper011817a2014-04-09 04:50:04 +00001082 return nullptr;
David Greenee917fff2009-05-14 22:23:47 +00001083 }
1084 Type = MHSt->getType();
1085 break;
1086 }
David Greene98ed3c72009-05-14 21:54:42 +00001087 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001088 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001089 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001090 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001091 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001092 }
1093 Type = RHSt->getType();
1094 break;
1095 }
1096 }
David Greenee32ebf22011-07-29 19:07:07 +00001097 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001098 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001099 }
David Greene5d0c0512009-05-14 20:54:48 +00001100 }
David Greene5d0c0512009-05-14 20:54:48 +00001101}
1102
1103/// ParseOperatorType - Parse a type for an operator. This returns
1104/// null on error.
1105///
1106/// OperatorType ::= '<' Type '>'
1107///
Dan Gohman1432ef82009-08-12 22:10:57 +00001108RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001109 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001110
1111 if (Lex.getCode() != tgtok::less) {
1112 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001113 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001114 }
1115 Lex.Lex(); // eat the <
1116
1117 Type = ParseType();
1118
Craig Topper011817a2014-04-09 04:50:04 +00001119 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001120 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001121 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001122 }
1123
1124 if (Lex.getCode() != tgtok::greater) {
1125 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001126 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001127 }
1128 Lex.Lex(); // eat the >
1129
1130 return Type;
1131}
1132
1133
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001134/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1135///
1136/// SimpleValue ::= IDValue
1137/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001138/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001139/// SimpleValue ::= CODEFRAGMENT
1140/// SimpleValue ::= '?'
1141/// SimpleValue ::= '{' ValueList '}'
1142/// SimpleValue ::= ID '<' ValueListNE '>'
1143/// SimpleValue ::= '[' ValueList ']'
1144/// SimpleValue ::= '(' IDValue DagArgList ')'
1145/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001146/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001147/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1148/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1149/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001150/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001151/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1152///
David Greened4263a62011-10-19 13:04:20 +00001153Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1154 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001155 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001156 switch (Lex.getCode()) {
1157 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001158 case tgtok::paste:
1159 // This is a leading paste operation. This is deprecated but
1160 // still exists in some .td files. Ignore it.
1161 Lex.Lex(); // Skip '#'.
1162 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001163 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001164 case tgtok::BinaryIntVal: {
1165 auto BinaryVal = Lex.getCurBinaryIntVal();
1166 SmallVector<Init*, 16> Bits(BinaryVal.second);
1167 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001168 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001169 R = BitsInit::get(Bits);
1170 Lex.Lex();
1171 break;
1172 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001173 case tgtok::StrVal: {
1174 std::string Val = Lex.getCurStrVal();
1175 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001176
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001177 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001178 while (Lex.getCode() == tgtok::StrVal) {
1179 Val += Lex.getCurStrVal();
1180 Lex.Lex();
1181 }
Bob Wilson7248f862009-11-22 04:24:42 +00001182
David Greenee32ebf22011-07-29 19:07:07 +00001183 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001184 break;
1185 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001186 case tgtok::CodeFragment:
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +00001187 R = StringInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001188 Lex.Lex();
1189 break;
1190 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001191 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001192 Lex.Lex();
1193 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001194 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001195 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001196 std::string Name = Lex.getCurStrVal();
1197 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001198 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001199
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001200 // Value ::= ID '<' ValueListNE '>'
1201 if (Lex.Lex() == tgtok::greater) {
1202 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001203 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001204 }
David Greene8618f952009-06-08 20:23:18 +00001205
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001206 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1207 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1208 // body.
1209 Record *Class = Records.getClass(Name);
1210 if (!Class) {
1211 Error(NameLoc, "Expected a class name, got '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001212 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001213 }
David Greene8618f952009-06-08 20:23:18 +00001214
David Greeneaf8ee2c2011-07-29 22:43:06 +00001215 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
Craig Topper011817a2014-04-09 04:50:04 +00001216 if (ValueList.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001217
David Greene8618f952009-06-08 20:23:18 +00001218 if (Lex.getCode() != tgtok::greater) {
1219 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001220 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001221 }
1222 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001223 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001224
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001225 // Create the new record, set it as CurRec temporarily.
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00001226 auto NewRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), NameLoc,
1227 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001228 Record *NewRec = NewRecOwner.get(); // Keep a copy since we may release.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001229 SubClassReference SCRef;
Jordan Rosef12e8a92013-01-10 18:50:11 +00001230 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001231 SCRef.Rec = Class;
1232 SCRef.TemplateArgs = ValueList;
1233 // Add info about the subclass to NewRec.
Craig Topper84138712014-11-29 05:31:10 +00001234 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001235 return nullptr;
Craig Topper84138712014-11-29 05:31:10 +00001236
Hal Finkela8c1f462014-01-02 20:47:09 +00001237 if (!CurMultiClass) {
1238 NewRec->resolveReferences();
Craig Toppercdab2322014-11-29 05:52:51 +00001239 Records.addDef(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001240 } else {
Adam Nemete5a07162014-09-16 17:14:13 +00001241 // This needs to get resolved once the multiclass template arguments are
1242 // known before any use.
1243 NewRec->setResolveFirst(true);
Hal Finkela8c1f462014-01-02 20:47:09 +00001244 // Otherwise, we're inside a multiclass, add it to the multiclass.
Craig Topperc3504c42014-12-11 05:25:33 +00001245 CurMultiClass->DefPrototypes.push_back(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001246
1247 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00001248 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
1249 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Hal Finkela8c1f462014-01-02 20:47:09 +00001250 assert(RV && "Template arg doesn't exist?");
1251 NewRec->addValue(*RV);
1252 }
1253
1254 // We can't return the prototype def here, instead return:
1255 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1256 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1257 assert(MCNameRV && "multiclass record must have a NAME");
1258
1259 return UnOpInit::get(UnOpInit::CAST,
1260 BinOpInit::get(BinOpInit::STRCONCAT,
1261 VarInit::get(MCNameRV->getName(),
1262 MCNameRV->getType()),
1263 NewRec->getNameInit(),
1264 StringRecTy::get()),
1265 Class->getDefInit()->getType());
1266 }
Bob Wilson7248f862009-11-22 04:24:42 +00001267
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001268 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001269 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001270 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001271 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001272 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001273 Lex.Lex(); // eat the '{'
David Greeneaf8ee2c2011-07-29 22:43:06 +00001274 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001275
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001276 if (Lex.getCode() != tgtok::r_brace) {
1277 Vals = ParseValueList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001278 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001279 }
1280 if (Lex.getCode() != tgtok::r_brace) {
1281 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001282 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001283 }
1284 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001285
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001286 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001287
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001288 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1289 // first. We'll first read everything in to a vector, then we can reverse
1290 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001291 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001292 // FIXME: The following two loops would not be duplicated
1293 // if the API was a little more orthogonal.
1294
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001295 // bits<n> values are allowed to initialize n bits.
1296 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1297 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1298 NewBits.push_back(BI->getBit((e - i) - 1));
1299 continue;
1300 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001301 // bits<n> can also come from variable initializers.
1302 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1303 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1304 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1305 NewBits.push_back(VI->getBit((e - i) - 1));
1306 continue;
1307 }
1308 // Fallthrough to try convert this to a bit.
1309 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001310 // All other values must be convertible to just a single bit.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001311 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001312 if (!Bit) {
Chris Lattner52416952007-11-22 21:06:59 +00001313 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1314 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001315 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001316 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001317 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001318 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001319 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001320 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001321 }
1322 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1323 Lex.Lex(); // eat the '['
David Greeneaf8ee2c2011-07-29 22:43:06 +00001324 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001325
Craig Topper011817a2014-04-09 04:50:04 +00001326 RecTy *DeducedEltTy = nullptr;
1327 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001328
Craig Topper011817a2014-04-09 04:50:04 +00001329 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001330 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001331 if (!ListType) {
Alp Tokere69170a2014-06-26 22:52:05 +00001332 std::string s;
1333 raw_string_ostream ss(s);
Reid Klecknerd78273f2013-08-06 22:51:21 +00001334 ss << "Type mismatch for list, expected list type, got "
1335 << ItemType->getAsString();
1336 TokError(ss.str());
Craig Topper011817a2014-04-09 04:50:04 +00001337 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001338 }
1339 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001340 }
David Greene8618f952009-06-08 20:23:18 +00001341
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001342 if (Lex.getCode() != tgtok::r_square) {
Craig Topper011817a2014-04-09 04:50:04 +00001343 Vals = ParseValueList(CurRec, nullptr,
1344 GivenListTy ? GivenListTy->getElementType() : nullptr);
1345 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001346 }
1347 if (Lex.getCode() != tgtok::r_square) {
1348 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001349 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001350 }
1351 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001352
Craig Topper011817a2014-04-09 04:50:04 +00001353 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001354 if (Lex.getCode() == tgtok::less) {
1355 // Optional list element type
1356 Lex.Lex(); // eat the '<'
1357
1358 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001359 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001360 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001361 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001362 }
1363
1364 if (Lex.getCode() != tgtok::greater) {
1365 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001366 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001367 }
1368 Lex.Lex(); // eat the '>'
1369 }
1370
1371 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001372 RecTy *EltTy = nullptr;
Craig Toppereb4d7c62015-04-29 04:43:36 +00001373 for (Init *V : Vals) {
1374 TypedInit *TArg = dyn_cast<TypedInit>(V);
Craig Topper011817a2014-04-09 04:50:04 +00001375 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001376 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001377 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001378 }
Craig Topper011817a2014-04-09 04:50:04 +00001379 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001380 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001381 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001382 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001383 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001384 }
Bob Wilson7248f862009-11-22 04:24:42 +00001385 } else {
David Greene8618f952009-06-08 20:23:18 +00001386 EltTy = TArg->getType();
1387 }
1388 }
1389
Craig Topper011817a2014-04-09 04:50:04 +00001390 if (GivenEltTy) {
1391 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001392 // Verify consistency
1393 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1394 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001395 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001396 }
1397 }
1398 EltTy = GivenEltTy;
1399 }
1400
Craig Topper011817a2014-04-09 04:50:04 +00001401 if (!EltTy) {
1402 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001403 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001404 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001405 }
1406 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001407 } else {
David Greene8618f952009-06-08 20:23:18 +00001408 // Make sure the deduced type is compatible with the given type
1409 if (GivenListTy) {
1410 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1411 TokError("Element type mismatch for list");
Craig Topper011817a2014-04-09 04:50:04 +00001412 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001413 }
1414 }
1415 DeducedEltTy = EltTy;
1416 }
Bob Wilson7248f862009-11-22 04:24:42 +00001417
David Greenee32ebf22011-07-29 19:07:07 +00001418 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001419 }
1420 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1421 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001422 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001423 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001424 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001425 }
Bob Wilson7248f862009-11-22 04:24:42 +00001426
David Greeneaf8ee2c2011-07-29 22:43:06 +00001427 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001428 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001429
Nate Begemandbe3f772009-03-19 05:21:56 +00001430 // If the operator name is present, parse it.
1431 std::string OperatorName;
1432 if (Lex.getCode() == tgtok::colon) {
1433 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1434 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001435 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001436 }
1437 OperatorName = Lex.getCurStrVal();
1438 Lex.Lex(); // eat the VarName.
1439 }
Bob Wilson7248f862009-11-22 04:24:42 +00001440
David Greeneaf8ee2c2011-07-29 22:43:06 +00001441 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001442 if (Lex.getCode() != tgtok::r_paren) {
1443 DagArgs = ParseDagArgList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001444 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001445 }
Bob Wilson7248f862009-11-22 04:24:42 +00001446
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001447 if (Lex.getCode() != tgtok::r_paren) {
1448 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001449 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001450 }
1451 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001452
David Greenee32ebf22011-07-29 19:07:07 +00001453 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001454 }
Bob Wilson7248f862009-11-22 04:24:42 +00001455
David Greene2f7cf7f2011-01-07 17:05:37 +00001456 case tgtok::XHead:
1457 case tgtok::XTail:
1458 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001459 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001460 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001461 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001462 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +00001463 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001464 case tgtok::XSRL:
1465 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001466 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001467 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001468 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001469 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001470 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001471 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001472 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001473 }
1474 }
Bob Wilson7248f862009-11-22 04:24:42 +00001475
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001476 return R;
1477}
1478
1479/// ParseValue - Parse a tblgen value. This returns null on error.
1480///
1481/// Value ::= SimpleValue ValueSuffix*
1482/// ValueSuffix ::= '{' BitList '}'
1483/// ValueSuffix ::= '[' BitList ']'
1484/// ValueSuffix ::= '.' ID
1485///
David Greened4263a62011-10-19 13:04:20 +00001486Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1487 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001488 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001489
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001490 // Parse the suffixes now if present.
1491 while (1) {
1492 switch (Lex.getCode()) {
1493 default: return Result;
1494 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001495 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001496 // This is the beginning of the object body.
1497 return Result;
1498
Chris Lattner526c8cb2009-06-21 03:39:35 +00001499 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001500 Lex.Lex(); // eat the '{'
1501 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001502 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001503
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001504 // Reverse the bitlist.
1505 std::reverse(Ranges.begin(), Ranges.end());
1506 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001507 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001508 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001509 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001510 }
Bob Wilson7248f862009-11-22 04:24:42 +00001511
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001512 // Eat the '}'.
1513 if (Lex.getCode() != tgtok::r_brace) {
1514 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001515 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001516 }
1517 Lex.Lex();
1518 break;
1519 }
1520 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001521 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001522 Lex.Lex(); // eat the '['
1523 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001524 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001525
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001526 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001527 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001528 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001529 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001530 }
Bob Wilson7248f862009-11-22 04:24:42 +00001531
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001532 // Eat the ']'.
1533 if (Lex.getCode() != tgtok::r_square) {
1534 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001535 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001536 }
1537 Lex.Lex();
1538 break;
1539 }
1540 case tgtok::period:
1541 if (Lex.Lex() != tgtok::Id) { // eat the .
1542 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001543 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001544 }
1545 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001546 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001547 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001548 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001549 }
David Greenee32ebf22011-07-29 19:07:07 +00001550 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001551 Lex.Lex(); // eat field name
1552 break;
David Greene8e85b482011-10-19 13:04:43 +00001553
1554 case tgtok::paste:
1555 SMLoc PasteLoc = Lex.getLoc();
1556
1557 // Create a !strconcat() operation, first casting each operand to
1558 // a string if necessary.
1559
Sean Silvafb509ed2012-10-10 20:24:43 +00001560 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001561 if (!LHS) {
1562 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001563 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001564 }
1565
1566 if (LHS->getType() != StringRecTy::get()) {
1567 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1568 }
1569
Craig Topper011817a2014-04-09 04:50:04 +00001570 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001571
1572 Lex.Lex(); // Eat the '#'.
1573 switch (Lex.getCode()) {
1574 case tgtok::colon:
1575 case tgtok::semi:
1576 case tgtok::l_brace:
1577 // These are all of the tokens that can begin an object body.
1578 // Some of these can also begin values but we disallow those cases
1579 // because they are unlikely to be useful.
1580
1581 // Trailing paste, concat with an empty string.
1582 RHS = StringInit::get("");
1583 break;
1584
1585 default:
1586 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001587 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001588 if (!RHS) {
1589 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001590 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001591 }
1592
1593 if (RHS->getType() != StringRecTy::get()) {
1594 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1595 }
1596
1597 break;
1598 }
1599
1600 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1601 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1602 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001603 }
1604 }
1605}
1606
1607/// ParseDagArgList - Parse the argument list for a dag literal expression.
1608///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001609/// DagArg ::= Value (':' VARNAME)?
1610/// DagArg ::= VARNAME
1611/// DagArgList ::= DagArg
1612/// DagArgList ::= DagArgList ',' DagArg
David Greeneaf8ee2c2011-07-29 22:43:06 +00001613std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001614TGParser::ParseDagArgList(Record *CurRec) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001615 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson7248f862009-11-22 04:24:42 +00001616
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001617 while (1) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001618 // DagArg ::= VARNAME
1619 if (Lex.getCode() == tgtok::VarName) {
1620 // A missing value is treated like '?'.
1621 Result.push_back(std::make_pair(UnsetInit::get(), Lex.getCurStrVal()));
1622 Lex.Lex();
1623 } else {
1624 // DagArg ::= Value (':' VARNAME)?
1625 Init *Val = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001626 if (!Val)
David Greeneaf8ee2c2011-07-29 22:43:06 +00001627 return std::vector<std::pair<llvm::Init*, std::string> >();
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001628
1629 // If the variable name is present, add it.
1630 std::string VarName;
1631 if (Lex.getCode() == tgtok::colon) {
1632 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1633 TokError("expected variable name in dag literal");
1634 return std::vector<std::pair<llvm::Init*, std::string> >();
1635 }
1636 VarName = Lex.getCurStrVal();
1637 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001638 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001639
1640 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001641 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001642 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001643 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001644 }
Bob Wilson7248f862009-11-22 04:24:42 +00001645
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001646 return Result;
1647}
1648
1649
1650/// ParseValueList - Parse a comma separated list of values, returning them as a
1651/// vector. Note that this always expects to be able to parse at least one
1652/// value. It returns an empty list if this is not possible.
1653///
1654/// ValueList ::= Value (',' Value)
1655///
David Greeneaf8ee2c2011-07-29 22:43:06 +00001656std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopher71520a82011-07-11 23:06:52 +00001657 RecTy *EltTy) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001658 std::vector<Init*> Result;
David Greene8618f952009-06-08 20:23:18 +00001659 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001660 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001661 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001662 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001663 if (TArgs.empty()) {
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001664 TokError("template argument provided to non-template class");
1665 return std::vector<Init*>();
1666 }
David Greene8618f952009-06-08 20:23:18 +00001667 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001668 if (!RV) {
1669 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1670 << ")\n";
1671 }
David Greene8618f952009-06-08 20:23:18 +00001672 assert(RV && "Template argument record not found??");
1673 ItemType = RV->getType();
1674 ++ArgN;
1675 }
1676 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001677 if (!Result.back()) return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001678
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001679 while (Lex.getCode() == tgtok::comma) {
1680 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001681
Craig Topper011817a2014-04-09 04:50:04 +00001682 if (ArgsRec && !EltTy) {
David Greenedb10e692011-10-19 13:02:42 +00001683 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001684 if (ArgN >= TArgs.size()) {
1685 TokError("too many template arguments");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001686 return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001687 }
David Greene8618f952009-06-08 20:23:18 +00001688 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1689 assert(RV && "Template argument record not found??");
1690 ItemType = RV->getType();
1691 ++ArgN;
1692 }
1693 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001694 if (!Result.back()) return std::vector<Init*>();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001695 }
Bob Wilson7248f862009-11-22 04:24:42 +00001696
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001697 return Result;
1698}
1699
1700
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001701/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1702/// empty string on error. This can happen in a number of different context's,
1703/// including within a def or in the template args for a def (which which case
1704/// CurRec will be non-null) and within the template args for a multiclass (in
1705/// which case CurRec will be null, but CurMultiClass will be set). This can
1706/// also happen within a def that is within a multiclass, which will set both
1707/// CurRec and CurMultiClass.
1708///
1709/// Declaration ::= FIELD? Type ID ('=' Value)?
1710///
David Greenedb10e692011-10-19 13:02:42 +00001711Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001712 bool ParsingTemplateArgs) {
1713 // Read the field prefix if present.
1714 bool HasField = Lex.getCode() == tgtok::Field;
1715 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001716
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001717 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001718 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001719
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001720 if (Lex.getCode() != tgtok::Id) {
1721 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001722 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001723 }
Bob Wilson7248f862009-11-22 04:24:42 +00001724
Chris Lattner526c8cb2009-06-21 03:39:35 +00001725 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001726 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001727 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001728
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001729 if (ParsingTemplateArgs) {
1730 if (CurRec) {
David Greenedb10e692011-10-19 13:02:42 +00001731 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001732 } else {
1733 assert(CurMultiClass);
1734 }
1735 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001736 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1737 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001738 }
Bob Wilson7248f862009-11-22 04:24:42 +00001739
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001740 // Add the value.
1741 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001742 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001743
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001744 // If a value is present, parse it.
1745 if (Lex.getCode() == tgtok::equal) {
1746 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001747 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001748 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001749 if (!Val ||
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001750 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001751 // Return the name, even if an error is thrown. This is so that we can
1752 // continue to make some progress, even without the value having been
1753 // initialized.
1754 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001755 }
Bob Wilson7248f862009-11-22 04:24:42 +00001756
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001757 return DeclName;
1758}
1759
David Greenefb927af2012-02-22 16:09:41 +00001760/// ParseForeachDeclaration - Read a foreach declaration, returning
1761/// the name of the declared object or a NULL Init on error. Return
1762/// the name of the parsed initializer list through ForeachListName.
1763///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001764/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1765/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1766/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001767///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001768VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001769 if (Lex.getCode() != tgtok::Id) {
1770 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001771 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001772 }
1773
1774 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1775 Lex.Lex();
1776
1777 // If a value is present, parse it.
1778 if (Lex.getCode() != tgtok::equal) {
1779 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001780 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001781 }
1782 Lex.Lex(); // Eat the '='
1783
Craig Topper011817a2014-04-09 04:50:04 +00001784 RecTy *IterType = nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001785 std::vector<unsigned> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001786
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001787 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001788 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001789 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001790 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001791 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001792 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001793 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001794 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001795 }
1796 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001797 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001798 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001799 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001800 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001801 }
1802 IterType = ListType->getElementType();
1803 break;
David Greenefb927af2012-02-22 16:09:41 +00001804 }
1805
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001806 case tgtok::IntVal: { // RangePiece.
1807 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001808 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001809 break;
David Greenefb927af2012-02-22 16:09:41 +00001810 }
1811
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001812 case tgtok::l_brace: { // '{' RangeList '}'
1813 Lex.Lex(); // eat the '{'
1814 Ranges = ParseRangeList();
1815 if (Lex.getCode() != tgtok::r_brace) {
1816 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001817 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001818 }
1819 Lex.Lex();
1820 break;
1821 }
1822 }
David Greenefb927af2012-02-22 16:09:41 +00001823
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001824 if (!Ranges.empty()) {
1825 assert(!IterType && "Type already initialized?");
1826 IterType = IntRecTy::get();
1827 std::vector<Init*> Values;
1828 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
1829 Values.push_back(IntInit::get(Ranges[i]));
1830 ForeachListValue = ListInit::get(Values, IterType);
1831 }
1832
1833 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001834 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001835
1836 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001837}
1838
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001839/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1840/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1841/// template args for a def, which may or may not be in a multiclass. If null,
1842/// these are the template args for a multiclass.
1843///
1844/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001845///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001846bool TGParser::ParseTemplateArgList(Record *CurRec) {
1847 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1848 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001849
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001850 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001851
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001852 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001853 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001854 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001855 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001856
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001857 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001858
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001859 while (Lex.getCode() == tgtok::comma) {
1860 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001861
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001862 // Read the following declarations.
1863 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001864 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001865 return true;
1866 TheRecToAddTo->addTemplateArg(TemplArg);
1867 }
Bob Wilson7248f862009-11-22 04:24:42 +00001868
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001869 if (Lex.getCode() != tgtok::greater)
1870 return TokError("expected '>' at end of template argument list");
1871 Lex.Lex(); // eat the '>'.
1872 return false;
1873}
1874
1875
1876/// ParseBodyItem - Parse a single item at within the body of a def or class.
1877///
1878/// BodyItem ::= Declaration ';'
1879/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1880bool TGParser::ParseBodyItem(Record *CurRec) {
1881 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001882 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001883 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001884
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001885 if (Lex.getCode() != tgtok::semi)
1886 return TokError("expected ';' after declaration");
1887 Lex.Lex();
1888 return false;
1889 }
1890
1891 // LET ID OptionalRangeList '=' Value ';'
1892 if (Lex.Lex() != tgtok::Id)
1893 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001894
Chris Lattner526c8cb2009-06-21 03:39:35 +00001895 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001896 std::string FieldName = Lex.getCurStrVal();
1897 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001898
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001899 std::vector<unsigned> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001900 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001901 return true;
1902 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001903
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001904 if (Lex.getCode() != tgtok::equal)
1905 return TokError("expected '=' in let expression");
1906 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001907
David Greene8618f952009-06-08 20:23:18 +00001908 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001909 if (!Field)
David Greene8618f952009-06-08 20:23:18 +00001910 return TokError("Value '" + FieldName + "' unknown!");
1911
1912 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001913
David Greeneaf8ee2c2011-07-29 22:43:06 +00001914 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001915 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001916
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001917 if (Lex.getCode() != tgtok::semi)
1918 return TokError("expected ';' after let expression");
1919 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001920
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001921 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1922}
1923
1924/// ParseBody - Read the body of a class or def. Return true on error, false on
1925/// success.
1926///
1927/// Body ::= ';'
1928/// Body ::= '{' BodyList '}'
1929/// BodyList BodyItem*
1930///
1931bool TGParser::ParseBody(Record *CurRec) {
1932 // If this is a null definition, just eat the semi and return.
1933 if (Lex.getCode() == tgtok::semi) {
1934 Lex.Lex();
1935 return false;
1936 }
Bob Wilson7248f862009-11-22 04:24:42 +00001937
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001938 if (Lex.getCode() != tgtok::l_brace)
1939 return TokError("Expected ';' or '{' to start body");
1940 // Eat the '{'.
1941 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001942
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001943 while (Lex.getCode() != tgtok::r_brace)
1944 if (ParseBodyItem(CurRec))
1945 return true;
1946
1947 // Eat the '}'.
1948 Lex.Lex();
1949 return false;
1950}
1951
Sean Silvacb1a75e2013-01-09 04:49:14 +00001952/// \brief Apply the current let bindings to \a CurRec.
1953/// \returns true on error, false otherwise.
1954bool TGParser::ApplyLetStack(Record *CurRec) {
1955 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1956 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1957 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1958 LetStack[i][j].Bits, LetStack[i][j].Value))
1959 return true;
1960 return false;
1961}
1962
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001963/// ParseObjectBody - Parse the body of a def or class. This consists of an
1964/// optional ClassList followed by a Body. CurRec is the current def or class
1965/// that is being parsed.
1966///
1967/// ObjectBody ::= BaseClassList Body
1968/// BaseClassList ::= /*empty*/
1969/// BaseClassList ::= ':' BaseClassListNE
1970/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1971///
1972bool TGParser::ParseObjectBody(Record *CurRec) {
1973 // If there is a baseclass list, read it.
1974 if (Lex.getCode() == tgtok::colon) {
1975 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001976
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001977 // Read all of the subclasses.
1978 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1979 while (1) {
1980 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00001981 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001982
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001983 // Add it.
1984 if (AddSubClass(CurRec, SubClass))
1985 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001986
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001987 if (Lex.getCode() != tgtok::comma) break;
1988 Lex.Lex(); // eat ','.
1989 SubClass = ParseSubClassReference(CurRec, false);
1990 }
1991 }
1992
Sean Silvacb1a75e2013-01-09 04:49:14 +00001993 if (ApplyLetStack(CurRec))
1994 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001995
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001996 return ParseBody(CurRec);
1997}
1998
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001999/// ParseDef - Parse and return a top level or multiclass def, return the record
2000/// corresponding to it. This returns null on error.
2001///
2002/// DefInst ::= DEF ObjectName ObjectBody
2003///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002004bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00002005 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002006 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00002007 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002008
2009 // Parse ObjectName and make a record for it.
Craig Topper84138712014-11-29 05:31:10 +00002010 std::unique_ptr<Record> CurRecOwner;
Jordan Roseabdd99b2013-01-10 18:50:05 +00002011 Init *Name = ParseObjectName(CurMultiClass);
2012 if (Name)
Craig Topper84138712014-11-29 05:31:10 +00002013 CurRecOwner = make_unique<Record>(Name, DefLoc, Records);
Jordan Roseabdd99b2013-01-10 18:50:05 +00002014 else
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00002015 CurRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), DefLoc,
2016 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00002017 Record *CurRec = CurRecOwner.get(); // Keep a copy since we may release.
Bob Wilson7248f862009-11-22 04:24:42 +00002018
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002019 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002020 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002021
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002022 // Ensure redefinition doesn't happen.
Craig Topper84138712014-11-29 05:31:10 +00002023 if (Records.getDef(CurRec->getNameInitAsString()))
2024 return Error(DefLoc, "def '" + CurRec->getNameInitAsString()+
2025 "' already defined");
Craig Toppercdab2322014-11-29 05:52:51 +00002026 Records.addDef(std::move(CurRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00002027
2028 if (ParseObjectBody(CurRec))
2029 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002030 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002031 // Parse the body before adding this prototype to the DefPrototypes vector.
2032 // That way implicit definitions will be added to the DefPrototypes vector
2033 // before this object, instantiated prior to defs derived from this object,
2034 // and this available for indirect name resolution when defs derived from
2035 // this object are instantiated.
Craig Topper84138712014-11-29 05:31:10 +00002036 if (ParseObjectBody(CurRec))
Hal Finkela8c1f462014-01-02 20:47:09 +00002037 return true;
2038
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002039 // Otherwise, a def inside a multiclass, add it to the multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002040 for (const auto &Proto : CurMultiClass->DefPrototypes)
2041 if (Proto->getNameInit() == CurRec->getNameInit())
Craig Topper84138712014-11-29 05:31:10 +00002042 return Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
2043 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002044 CurMultiClass->DefPrototypes.push_back(std::move(CurRecOwner));
Anton Yartsev671dff12014-08-08 00:29:54 +00002045 } else if (ParseObjectBody(CurRec)) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002046 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002047 }
Bob Wilson7248f862009-11-22 04:24:42 +00002048
Craig Topper011817a2014-04-09 04:50:04 +00002049 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002050 // See Record::setName(). This resolve step will see any new name
2051 // for the def that might have been created when resolving
2052 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002053 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002054
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002055 // If ObjectBody has template arguments, it's an error.
2056 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002057
2058 if (CurMultiClass) {
2059 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002060 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
2061 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002062 assert(RV && "Template arg doesn't exist?");
2063 CurRec->addValue(*RV);
2064 }
2065 }
2066
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002067 if (ProcessForeachDefs(CurRec, DefLoc)) {
Craig Topper84138712014-11-29 05:31:10 +00002068 return Error(DefLoc, "Could not process loops for def" +
2069 CurRec->getNameInitAsString());
David Greenefb927af2012-02-22 16:09:41 +00002070 }
2071
2072 return false;
2073}
2074
2075/// ParseForeach - Parse a for statement. Return the record corresponding
2076/// to it. This returns true on error.
2077///
2078/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2079/// Foreach ::= FOREACH Declaration IN Object
2080///
2081bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2082 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2083 Lex.Lex(); // Eat the 'for' token.
2084
2085 // Make a temporary object to record items associated with the for
2086 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002087 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002088 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002089 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002090 return TokError("expected declaration in for");
2091
2092 if (Lex.getCode() != tgtok::In)
2093 return TokError("Unknown tok");
2094 Lex.Lex(); // Eat the in
2095
2096 // Create a loop object and remember it.
2097 Loops.push_back(ForeachLoop(IterName, ListValue));
2098
2099 if (Lex.getCode() != tgtok::l_brace) {
2100 // FOREACH Declaration IN Object
2101 if (ParseObject(CurMultiClass))
2102 return true;
2103 }
2104 else {
2105 SMLoc BraceLoc = Lex.getLoc();
2106 // Otherwise, this is a group foreach.
2107 Lex.Lex(); // eat the '{'.
2108
2109 // Parse the object list.
2110 if (ParseObjectList(CurMultiClass))
2111 return true;
2112
2113 if (Lex.getCode() != tgtok::r_brace) {
2114 TokError("expected '}' at end of foreach command");
2115 return Error(BraceLoc, "to match this '{'");
2116 }
2117 Lex.Lex(); // Eat the }
2118 }
2119
2120 // We've processed everything in this loop.
2121 Loops.pop_back();
2122
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002123 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002124}
2125
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002126/// ParseClass - Parse a tblgen class definition.
2127///
2128/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2129///
2130bool TGParser::ParseClass() {
2131 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2132 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002133
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002134 if (Lex.getCode() != tgtok::Id)
2135 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002136
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002137 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2138 if (CurRec) {
2139 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002140 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002141 !CurRec->getSuperClasses().empty() ||
2142 !CurRec->getTemplateArgs().empty())
David Greene8eed9982011-10-19 13:03:58 +00002143 return TokError("Class '" + CurRec->getNameInitAsString()
2144 + "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002145 } else {
2146 // If this is the first reference to this class, create and add it.
Hans Wennborgffbbd532014-11-30 00:31:49 +00002147 auto NewRec =
2148 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
Craig Toppercdab2322014-11-29 05:52:51 +00002149 CurRec = NewRec.get();
2150 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002151 }
2152 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002153
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002154 // If there are template args, parse them.
2155 if (Lex.getCode() == tgtok::less)
2156 if (ParseTemplateArgList(CurRec))
2157 return true;
2158
2159 // Finally, parse the object body.
2160 return ParseObjectBody(CurRec);
2161}
2162
2163/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2164/// of LetRecords.
2165///
2166/// LetList ::= LetItem (',' LetItem)*
2167/// LetItem ::= ID OptionalRangeList '=' Value
2168///
2169std::vector<LetRecord> TGParser::ParseLetList() {
2170 std::vector<LetRecord> Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002171
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002172 while (1) {
2173 if (Lex.getCode() != tgtok::Id) {
2174 TokError("expected identifier in let definition");
2175 return std::vector<LetRecord>();
2176 }
2177 std::string Name = Lex.getCurStrVal();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002178 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002179 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002180
2181 // Check for an optional RangeList.
2182 std::vector<unsigned> Bits;
Bob Wilson7248f862009-11-22 04:24:42 +00002183 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002184 return std::vector<LetRecord>();
2185 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002186
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002187 if (Lex.getCode() != tgtok::equal) {
2188 TokError("expected '=' in let expression");
2189 return std::vector<LetRecord>();
2190 }
2191 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002192
Craig Topper011817a2014-04-09 04:50:04 +00002193 Init *Val = ParseValue(nullptr);
2194 if (!Val) return std::vector<LetRecord>();
Bob Wilson7248f862009-11-22 04:24:42 +00002195
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002196 // Now that we have everything, add the record.
2197 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson7248f862009-11-22 04:24:42 +00002198
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002199 if (Lex.getCode() != tgtok::comma)
2200 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002201 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002202 }
2203}
2204
2205/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002206/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002207///
2208/// Object ::= LET LetList IN '{' ObjectList '}'
2209/// Object ::= LET LetList IN Object
2210///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002211bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002212 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2213 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002214
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002215 // Add this entry to the let stack.
2216 std::vector<LetRecord> LetInfo = ParseLetList();
2217 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002218 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002219
2220 if (Lex.getCode() != tgtok::In)
2221 return TokError("expected 'in' at end of top-level 'let'");
2222 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002223
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002224 // If this is a scalar let, just handle it now
2225 if (Lex.getCode() != tgtok::l_brace) {
2226 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002227 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002228 return true;
2229 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002230 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002231 // Otherwise, this is a group let.
2232 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002233
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002234 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002235 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002236 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002237
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002238 if (Lex.getCode() != tgtok::r_brace) {
2239 TokError("expected '}' at end of top level let command");
2240 return Error(BraceLoc, "to match this '{'");
2241 }
2242 Lex.Lex();
2243 }
Bob Wilson7248f862009-11-22 04:24:42 +00002244
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002245 // Outside this let scope, this let block is not active.
2246 LetStack.pop_back();
2247 return false;
2248}
2249
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002250/// ParseMultiClass - Parse a multiclass definition.
2251///
Bob Wilson3d948162009-04-28 19:41:44 +00002252/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002253/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2254/// MultiClassObject ::= DefInst
2255/// MultiClassObject ::= MultiClassInst
2256/// MultiClassObject ::= DefMInst
2257/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2258/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002259///
2260bool TGParser::ParseMultiClass() {
2261 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2262 Lex.Lex(); // Eat the multiclass token.
2263
2264 if (Lex.getCode() != tgtok::Id)
2265 return TokError("expected identifier after multiclass for name");
2266 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002267
Craig Topper7adf2bf2014-12-11 05:25:30 +00002268 auto Result =
2269 MultiClasses.insert(std::make_pair(Name,
2270 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2271
2272 if (!Result.second)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002273 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002274
Craig Topper7adf2bf2014-12-11 05:25:30 +00002275 CurMultiClass = Result.first->second.get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002276 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002277
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002278 // If there are template args, parse them.
2279 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002280 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002281 return true;
2282
David Greene7049e792009-04-24 16:55:41 +00002283 bool inherits = false;
2284
David Greene753ed8f2009-04-22 16:42:54 +00002285 // If there are submulticlasses, parse them.
2286 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002287 inherits = true;
2288
David Greene753ed8f2009-04-22 16:42:54 +00002289 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002290
David Greene753ed8f2009-04-22 16:42:54 +00002291 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002292 SubMultiClassReference SubMultiClass =
2293 ParseSubMultiClassReference(CurMultiClass);
David Greene753ed8f2009-04-22 16:42:54 +00002294 while (1) {
2295 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002296 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002297
David Greene753ed8f2009-04-22 16:42:54 +00002298 // Add it.
2299 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2300 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002301
David Greene753ed8f2009-04-22 16:42:54 +00002302 if (Lex.getCode() != tgtok::comma) break;
2303 Lex.Lex(); // eat ','.
2304 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2305 }
2306 }
2307
David Greene7049e792009-04-24 16:55:41 +00002308 if (Lex.getCode() != tgtok::l_brace) {
2309 if (!inherits)
2310 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002311 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002312 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002313 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002314 } else {
David Greene7049e792009-04-24 16:55:41 +00002315 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2316 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002317
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002318 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002319 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002320 default:
2321 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
2322 case tgtok::Let:
2323 case tgtok::Def:
2324 case tgtok::Defm:
2325 case tgtok::Foreach:
2326 if (ParseObject(CurMultiClass))
2327 return true;
2328 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002329 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002330 }
David Greene7049e792009-04-24 16:55:41 +00002331 Lex.Lex(); // eat the '}'.
2332 }
Bob Wilson7248f862009-11-22 04:24:42 +00002333
Craig Topper011817a2014-04-09 04:50:04 +00002334 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002335 return false;
2336}
2337
David Greenedb445972011-10-05 22:42:07 +00002338Record *TGParser::
2339InstantiateMulticlassDef(MultiClass &MC,
2340 Record *DefProto,
Hal Finkelf2a0b2b2014-01-02 19:35:33 +00002341 Init *&DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002342 SMRange DefmPrefixRange) {
David Greene5d5d88c2011-10-19 13:04:31 +00002343 // We need to preserve DefProto so it can be reused for later
2344 // instantiations, so create a new Record to inherit from it.
2345
David Greenedb445972011-10-05 22:42:07 +00002346 // Add in the defm name. If the defm prefix is empty, give each
2347 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2348 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2349 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002350
Jordan Roseabdd99b2013-01-10 18:50:05 +00002351 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002352 if (!DefmPrefix) {
David Greene5d5d88c2011-10-19 13:04:31 +00002353 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002354 IsAnonymous = true;
2355 }
David Greene5d5d88c2011-10-19 13:04:31 +00002356
2357 Init *DefName = DefProto->getNameInit();
2358
Sean Silvafb509ed2012-10-10 20:24:43 +00002359 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002360
Craig Topper011817a2014-04-09 04:50:04 +00002361 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002362 // We have a fully expanded string so there are no operators to
2363 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002364 DefName =
2365 BinOpInit::get(BinOpInit::STRCONCAT,
2366 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2367 StringRecTy::get())->Fold(DefProto, &MC),
2368 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2369 }
David Greene5d5d88c2011-10-19 13:04:31 +00002370
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002371 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002372 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002373 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Craig Topper84138712014-11-29 05:31:10 +00002374 auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002375
2376 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002377 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002378 Ref.Rec = DefProto;
Craig Topper84138712014-11-29 05:31:10 +00002379 AddSubClass(CurRec.get(), Ref);
David Greenedb445972011-10-05 22:42:07 +00002380
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002381 // Set the value for NAME. We don't resolve references to it 'til later,
2382 // though, so that uses in nested multiclass names don't get
2383 // confused.
Craig Topper84138712014-11-29 05:31:10 +00002384 if (SetValue(CurRec.get(), Ref.RefRange.Start, "NAME",
2385 std::vector<unsigned>(), DefmPrefix)) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002386 Error(DefmPrefixRange.Start, "Could not resolve "
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002387 + CurRec->getNameInitAsString() + ":NAME to '"
2388 + DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002389 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002390 }
David Greene8bf0d722011-10-19 13:04:35 +00002391
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002392 // If the DefNameString didn't resolve, we probably have a reference to
2393 // NAME and need to replace it. We need to do at least this much greedily,
2394 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002395 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002396 RecordVal *DefNameRV = CurRec->getValue("NAME");
2397 CurRec->resolveReferencesTo(DefNameRV);
2398 }
2399
2400 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002401 // Now that we're at the top level, resolve all NAME references
2402 // in the resultant defs that weren't in the def names themselves.
2403 RecordVal *DefNameRV = CurRec->getValue("NAME");
2404 CurRec->resolveReferencesTo(DefNameRV);
2405
2406 // Now that NAME references are resolved and we're at the top level of
2407 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002408 // currently in a multiclass, it means this defm appears inside a
2409 // multiclass and its name won't be fully resolvable until we see
2410 // the top-level defm. Therefore, we don't add this to the
2411 // RecordKeeper at this point. If we did we could get duplicate
2412 // defs as more than one probably refers to NAME or some other
2413 // common internal placeholder.
2414
2415 // Ensure redefinition doesn't happen.
2416 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002417 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002418 "' already defined, instantiating defm with subdef '" +
2419 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002420 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002421 }
2422
Craig Topper84138712014-11-29 05:31:10 +00002423 Record *CurRecSave = CurRec.get(); // Keep a copy before we release.
Craig Toppercdab2322014-11-29 05:52:51 +00002424 Records.addDef(std::move(CurRec));
Craig Topper84138712014-11-29 05:31:10 +00002425 return CurRecSave;
David Greene8bf0d722011-10-19 13:04:35 +00002426 }
2427
Craig Topper84138712014-11-29 05:31:10 +00002428 // FIXME This is bad but the ownership transfer to caller is pretty messy.
2429 // The unique_ptr in this function at least protects the exits above.
2430 return CurRec.release();
David Greenedb445972011-10-05 22:42:07 +00002431}
2432
2433bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2434 Record *CurRec,
2435 SMLoc DefmPrefixLoc,
2436 SMLoc SubClassLoc,
David Greenedb10e692011-10-19 13:02:42 +00002437 const std::vector<Init *> &TArgs,
David Greenedb445972011-10-05 22:42:07 +00002438 std::vector<Init *> &TemplateVals,
2439 bool DeleteArgs) {
2440 // Loop over all of the template arguments, setting them to the specified
2441 // value or leaving them as the default if necessary.
2442 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2443 // Check if a value is specified for this temp-arg.
2444 if (i < TemplateVals.size()) {
2445 // Set it now.
2446 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2447 TemplateVals[i]))
2448 return true;
2449
2450 // Resolve it next.
2451 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2452
2453 if (DeleteArgs)
2454 // Now remove it.
2455 CurRec->removeValue(TArgs[i]);
2456
2457 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2458 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenedb10e692011-10-19 13:02:42 +00002459 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2460 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2461 + "'");
David Greenedb445972011-10-05 22:42:07 +00002462 }
2463 }
2464 return false;
2465}
2466
2467bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2468 Record *CurRec,
2469 Record *DefProto,
2470 SMLoc DefmPrefixLoc) {
2471 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002472 if (ApplyLetStack(CurRec))
2473 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002474
David Greenedb445972011-10-05 22:42:07 +00002475 // Don't create a top level definition for defm inside multiclasses,
2476 // instead, only update the prototypes and bind the template args
2477 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002478 if (!CurMultiClass)
2479 return false;
Craig Toppereb4d7c62015-04-29 04:43:36 +00002480 for (const auto &Proto : CurMultiClass->DefPrototypes)
2481 if (Proto->getNameInit() == CurRec->getNameInit())
Sean Silvacc951b22013-01-09 05:28:12 +00002482 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2483 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002484 CurMultiClass->DefPrototypes.push_back(std::unique_ptr<Record>(CurRec));
David Greenedb445972011-10-05 22:42:07 +00002485
Sean Silvacc951b22013-01-09 05:28:12 +00002486 // Copy the template arguments for the multiclass into the new def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002487 for (Init * TA : CurMultiClass->Rec.getTemplateArgs()) {
2488 const RecordVal *RV = CurMultiClass->Rec.getValue(TA);
Sean Silvacc951b22013-01-09 05:28:12 +00002489 assert(RV && "Template arg doesn't exist?");
2490 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002491 }
2492
2493 return false;
2494}
2495
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002496/// ParseDefm - Parse the instantiation of a multiclass.
2497///
2498/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2499///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002500bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002501 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002502 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002503 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002504
Craig Topperb21afc62013-01-07 05:09:33 +00002505 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002506 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002507 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002508
Jordan Rosef12e8a92013-01-10 18:50:11 +00002509 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002510 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002511 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002512
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002513 // Keep track of the new generated record definitions.
2514 std::vector<Record*> NewRecDefs;
2515
2516 // This record also inherits from a regular class (non-multiclass)?
2517 bool InheritFromClass = false;
2518
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002519 // eat the colon.
2520 Lex.Lex();
2521
Chris Lattner526c8cb2009-06-21 03:39:35 +00002522 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002523 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002524
2525 while (1) {
Craig Topper011817a2014-04-09 04:50:04 +00002526 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002527
2528 // To instantiate a multiclass, we need to first get the multiclass, then
2529 // instantiate each def contained in the multiclass with the SubClassRef
2530 // template parameters.
Craig Topper7adf2bf2014-12-11 05:25:30 +00002531 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper4ca40012014-11-30 01:20:17 +00002532 assert(MC && "Didn't lookup multiclass correctly?");
David Greeneaf8ee2c2011-07-29 22:43:06 +00002533 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002534
2535 // Verify that the correct number of template arguments were specified.
David Greenedb10e692011-10-19 13:02:42 +00002536 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002537 if (TArgs.size() < TemplateVals.size())
2538 return Error(SubClassLoc,
2539 "more template args specified than multiclass expects");
2540
2541 // Loop over all the def's in the multiclass, instantiating each one.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002542 for (const std::unique_ptr<Record> &DefProto : MC->DefPrototypes) {
2543 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto.get(), DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002544 SMRange(DefmLoc,
2545 DefmPrefixEndLoc));
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002546 if (!CurRec)
2547 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002548
Jordan Rosef12e8a92013-01-10 18:50:11 +00002549 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002550 TArgs, TemplateVals, true/*Delete args*/))
2551 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002552
Craig Toppereb4d7c62015-04-29 04:43:36 +00002553 if (ResolveMulticlassDef(*MC, CurRec, DefProto.get(), DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002554 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002555
Adam Nemete5a07162014-09-16 17:14:13 +00002556 // Defs that can be used by other definitions should be fully resolved
2557 // before any use.
2558 if (DefProto->isResolveFirst() && !CurMultiClass) {
2559 CurRec->resolveReferences();
2560 CurRec->setResolveFirst(false);
2561 }
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002562 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002563 }
2564
David Greenedb445972011-10-05 22:42:07 +00002565
David Greenef00919a2009-04-22 22:17:51 +00002566 if (Lex.getCode() != tgtok::comma) break;
2567 Lex.Lex(); // eat ','.
2568
Craig Topper998a39a2013-08-20 04:22:09 +00002569 if (Lex.getCode() != tgtok::Id)
2570 return TokError("expected identifier");
2571
David Greenef00919a2009-04-22 22:17:51 +00002572 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002573
2574 // A defm can inherit from regular classes (non-multiclass) as
2575 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002576 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002577
2578 if (InheritFromClass)
2579 break;
2580
Craig Topper011817a2014-04-09 04:50:04 +00002581 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002582 }
2583
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002584 if (InheritFromClass) {
2585 // Process all the classes to inherit as if they were part of a
2586 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002587 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002588 while (1) {
2589 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002590 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002591
2592 // Get the expanded definition prototypes and teach them about
2593 // the record values the current class to inherit has
Craig Toppereb4d7c62015-04-29 04:43:36 +00002594 for (Record *CurRec : NewRecDefs) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002595 // Add it.
2596 if (AddSubClass(CurRec, SubClass))
2597 return true;
2598
Sean Silvacb1a75e2013-01-09 04:49:14 +00002599 if (ApplyLetStack(CurRec))
2600 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002601 }
2602
2603 if (Lex.getCode() != tgtok::comma) break;
2604 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002605 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002606 }
2607 }
2608
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002609 if (!CurMultiClass)
Craig Toppereb4d7c62015-04-29 04:43:36 +00002610 for (Record *CurRec : NewRecDefs)
David Greene50c09122011-08-10 18:27:46 +00002611 // See Record::setName(). This resolve step will see any new
2612 // name for the def that might have been created when resolving
2613 // inheritance, values and arguments above.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002614 CurRec->resolveReferences();
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002615
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002616 if (Lex.getCode() != tgtok::semi)
2617 return TokError("expected ';' at end of defm");
2618 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002619
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002620 return false;
2621}
2622
2623/// ParseObject
2624/// Object ::= ClassInst
2625/// Object ::= DefInst
2626/// Object ::= MultiClassInst
2627/// Object ::= DefMInst
2628/// Object ::= LETCommand '{' ObjectList '}'
2629/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002630bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002631 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002632 default:
2633 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002634 case tgtok::Let: return ParseTopLevelLet(MC);
2635 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002636 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002637 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002638 case tgtok::Class: return ParseClass();
2639 case tgtok::MultiClass: return ParseMultiClass();
2640 }
2641}
2642
2643/// ParseObjectList
2644/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002645bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002646 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002647 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002648 return true;
2649 }
2650 return false;
2651}
2652
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002653bool TGParser::ParseFile() {
2654 Lex.Lex(); // Prime the lexer.
2655 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002656
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002657 // If we have unread input at the end of the file, report it.
2658 if (Lex.getCode() == tgtok::Eof)
2659 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002660
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002661 return TokError("Unexpected input at top level");
2662}
2663