blob: e167529e4c6c9aadce3154ee641200fb7805d9ba [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
Yaron Kereneb2a2542016-01-29 20:50:44 +000048LLVM_DUMP_METHOD void SubMultiClassReference::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000049 errs() << "Multiclass:\n";
Bob Wilson7248f862009-11-22 04:24:42 +000050
David Greene7049e792009-04-24 16:55:41 +000051 MC->dump();
Bob Wilson7248f862009-11-22 04:24:42 +000052
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000053 errs() << "Template args:\n";
Craig Toppera9642b42015-05-04 01:35:39 +000054 for (Init *TA : TemplateArgs)
Craig Toppereb4d7c62015-04-29 04:43:36 +000055 TA->dump();
David Greene7049e792009-04-24 16:55:41 +000056}
57
Chris Lattnerf4127dd2007-11-22 20:49:04 +000058} // end namespace llvm
59
Chris Lattner526c8cb2009-06-21 03:39:35 +000060bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Craig Topper011817a2014-04-09 04:50:04 +000061 if (!CurRec)
Chris Lattnerf4127dd2007-11-22 20:49:04 +000062 CurRec = &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +000063
Jakob Stoklund Olesen9d1c5ee2012-01-13 03:16:35 +000064 if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000065 // The value already exists in the class, treat this as a set.
66 if (ERV->setValue(RV.getValue()))
67 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
68 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson7248f862009-11-22 04:24:42 +000069 "previous definition of type '" +
Chris Lattnerf4127dd2007-11-22 20:49:04 +000070 ERV->getType()->getAsString() + "'");
71 } else {
72 CurRec->addValue(RV);
73 }
74 return false;
75}
76
77/// SetValue -
78/// Return true on error, false on success.
David Greene3ca42122011-10-19 13:02:39 +000079bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
Craig Toppercfd81732016-01-04 03:15:08 +000080 ArrayRef<unsigned> BitList, Init *V,
Craig Topper1e23ed92016-01-04 03:05:14 +000081 bool AllowSelfAssignment) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000082 if (!V) return false;
83
Craig Topper011817a2014-04-09 04:50:04 +000084 if (!CurRec) CurRec = &CurMultiClass->Rec;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000085
86 RecordVal *RV = CurRec->getValue(ValName);
Craig Topper011817a2014-04-09 04:50:04 +000087 if (!RV)
Craig Topper85c07002015-04-30 05:54:22 +000088 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
89 "' unknown!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +000090
91 // Do not allow assignments like 'X = X'. This will just cause infinite loops
92 // in the resolution machinery.
93 if (BitList.empty())
Sean Silvafb509ed2012-10-10 20:24:43 +000094 if (VarInit *VI = dyn_cast<VarInit>(V))
Craig Topper1e23ed92016-01-04 03:05:14 +000095 if (VI->getNameInit() == ValName && !AllowSelfAssignment)
96 return true;
Bob Wilson7248f862009-11-22 04:24:42 +000097
Chris Lattnerf4127dd2007-11-22 20:49:04 +000098 // If we are assigning to a subset of the bits in the value... then we must be
99 // assigning to a field of BitsRecTy, which must have a BitsInit
100 // initializer.
101 //
102 if (!BitList.empty()) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000103 BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue());
Craig Topper011817a2014-04-09 04:50:04 +0000104 if (!CurVal)
Craig Topper85c07002015-04-30 05:54:22 +0000105 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
106 "' is not a bits type");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000107
108 // Convert the incoming value to a bits type of the appropriate size...
David Greeneaf8ee2c2011-07-29 22:43:06 +0000109 Init *BI = V->convertInitializerTo(BitsRecTy::get(BitList.size()));
Craig Toppera9642b42015-05-04 01:35:39 +0000110 if (!BI)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000111 return Error(Loc, "Initializer is not compatible with bit range");
Bob Wilson7248f862009-11-22 04:24:42 +0000112
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000113 // We should have a BitsInit type now.
Craig Toppered5a9502015-04-29 07:13:05 +0000114 BitsInit *BInit = cast<BitsInit>(BI);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000115
David Greeneaf8ee2c2011-07-29 22:43:06 +0000116 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000117
118 // Loop over bits, assigning values as appropriate.
119 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
120 unsigned Bit = BitList[i];
David Greeneb3da8122011-07-29 19:07:00 +0000121 if (NewBits[Bit])
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000122 return Error(Loc, "Cannot set bit #" + Twine(Bit) + " of value '" +
David Greene3ca42122011-10-19 13:02:39 +0000123 ValName->getAsUnquotedString() + "' more than once");
David Greeneb3da8122011-07-29 19:07:00 +0000124 NewBits[Bit] = BInit->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000125 }
126
127 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
Craig Topper011817a2014-04-09 04:50:04 +0000128 if (!NewBits[i])
David Greeneb3da8122011-07-29 19:07:00 +0000129 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000130
David Greenee32ebf22011-07-29 19:07:07 +0000131 V = BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000132 }
133
Pete Cooper040c6a62014-07-31 01:43:57 +0000134 if (RV->setValue(V)) {
135 std::string InitType = "";
Craig Toppera9642b42015-05-04 01:35:39 +0000136 if (BitsInit *BI = dyn_cast<BitsInit>(V))
Pete Cooper040c6a62014-07-31 01:43:57 +0000137 InitType = (Twine("' of type bit initializer with length ") +
138 Twine(BI->getNumBits())).str();
Craig Topper85c07002015-04-30 05:54:22 +0000139 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
140 "' of type '" + RV->getType()->getAsString() +
141 "' is incompatible with initializer '" + V->getAsString() +
142 InitType + "'");
Pete Cooper040c6a62014-07-31 01:43:57 +0000143 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000144 return false;
145}
146
147/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
148/// args as SubClass's template arguments.
Cedric Venetd1e179d2009-02-14 16:06:42 +0000149bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000150 Record *SC = SubClass.Rec;
151 // Add all of the values in the subclass into the current class.
Craig Topper8eb764c2015-06-02 06:19:28 +0000152 for (const RecordVal &Val : SC->getValues())
153 if (AddValue(CurRec, SubClass.RefRange.Start, Val))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000154 return true;
155
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000156 ArrayRef<Init *> TArgs = SC->getTemplateArgs();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000157
158 // Ensure that an appropriate number of template arguments are specified.
159 if (TArgs.size() < SubClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000160 return Error(SubClass.RefRange.Start,
161 "More template args specified than expected");
Bob Wilson7248f862009-11-22 04:24:42 +0000162
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000163 // Loop over all of the template arguments, setting them to the specified
164 // value or leaving them as the default if necessary.
165 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
166 if (i < SubClass.TemplateArgs.size()) {
167 // If a value is specified for this template arg, set it now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000168 if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000169 None, SubClass.TemplateArgs[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000170 return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000171
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000172 // Resolve it next.
173 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson7248f862009-11-22 04:24:42 +0000174
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000175 // Now remove it.
176 CurRec->removeValue(TArgs[i]);
177
178 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000179 return Error(SubClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000180 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000181 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000182 ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000183 }
184 }
185
186 // Since everything went well, we can now set the "superclass" list for the
187 // current record.
Craig Topper0e41d0b2016-01-18 19:52:37 +0000188 ArrayRef<std::pair<Record *, SMRange>> SCs = SC->getSuperClasses();
189 for (const auto &SCPair : SCs) {
190 if (CurRec->isSubClassOf(SCPair.first))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000191 return Error(SubClass.RefRange.Start,
Craig Topper0e41d0b2016-01-18 19:52:37 +0000192 "Already subclass of '" + SCPair.first->getName() + "'!\n");
193 CurRec->addSuperClass(SCPair.first, SCPair.second);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000194 }
Bob Wilson7248f862009-11-22 04:24:42 +0000195
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000196 if (CurRec->isSubClassOf(SC))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000197 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000198 "Already subclass of '" + SC->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000199 CurRec->addSuperClass(SC, SubClass.RefRange);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000200 return false;
201}
202
David Greene753ed8f2009-04-22 16:42:54 +0000203/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilsonf71e6562009-04-30 18:26:19 +0000204/// CurMC, resolving its template args as SubMultiClass's
David Greene753ed8f2009-04-22 16:42:54 +0000205/// template arguments.
Bob Wilsonf71e6562009-04-30 18:26:19 +0000206bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson92ab8202009-04-30 17:46:20 +0000207 SubMultiClassReference &SubMultiClass) {
David Greene753ed8f2009-04-22 16:42:54 +0000208 MultiClass *SMC = SubMultiClass.MC;
Bob Wilsonf71e6562009-04-30 18:26:19 +0000209 Record *CurRec = &CurMC->Rec;
David Greene753ed8f2009-04-22 16:42:54 +0000210
David Greene753ed8f2009-04-22 16:42:54 +0000211 // Add all of the values in the subclass into the current class.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000212 for (const auto &SMCVal : SMC->Rec.getValues())
213 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000214 return true;
215
Craig Toppera4ea4b02014-11-30 00:24:32 +0000216 unsigned newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000217
David Greene753ed8f2009-04-22 16:42:54 +0000218 // Add all of the defs in the subclass into the current multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000219 for (const std::unique_ptr<Record> &R : SMC->DefPrototypes) {
David Greene753ed8f2009-04-22 16:42:54 +0000220 // Clone the def and add it to the current multiclass
Craig Toppereb4d7c62015-04-29 04:43:36 +0000221 auto NewDef = make_unique<Record>(*R);
David Greene753ed8f2009-04-22 16:42:54 +0000222
223 // Add all of the values in the superclass into the current def.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000224 for (const auto &MCVal : CurRec->getValues())
225 if (AddValue(NewDef.get(), SubMultiClass.RefRange.Start, MCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000226 return true;
227
Craig Topperc3504c42014-12-11 05:25:33 +0000228 CurMC->DefPrototypes.push_back(std::move(NewDef));
David Greene753ed8f2009-04-22 16:42:54 +0000229 }
Bob Wilson3d948162009-04-28 19:41:44 +0000230
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000231 ArrayRef<Init *> SMCTArgs = SMC->Rec.getTemplateArgs();
David Greene753ed8f2009-04-22 16:42:54 +0000232
David Greene7049e792009-04-24 16:55:41 +0000233 // Ensure that an appropriate number of template arguments are
234 // specified.
David Greene753ed8f2009-04-22 16:42:54 +0000235 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000236 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000237 "More template args specified than expected");
Bob Wilson3d948162009-04-28 19:41:44 +0000238
David Greene753ed8f2009-04-22 16:42:54 +0000239 // Loop over all of the template arguments, setting them to the specified
240 // value or leaving them as the default if necessary.
241 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
242 if (i < SubMultiClass.TemplateArgs.size()) {
David Greene7049e792009-04-24 16:55:41 +0000243 // If a value is specified for this template arg, set it in the
244 // superclass now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000245 if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000246 None, SubMultiClass.TemplateArgs[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000247 return true;
248
249 // Resolve it next.
250 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson3d948162009-04-28 19:41:44 +0000251
David Greene753ed8f2009-04-22 16:42:54 +0000252 // Now remove it.
253 CurRec->removeValue(SMCTArgs[i]);
254
David Greene7049e792009-04-24 16:55:41 +0000255 // If a value is specified for this template arg, set it in the
256 // new defs now.
Craig Topperc3504c42014-12-11 05:25:33 +0000257 for (const auto &Def :
258 makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
259 if (SetValue(Def.get(), SubMultiClass.RefRange.Start, SMCTArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000260 None, SubMultiClass.TemplateArgs[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000261 return true;
262
263 // Resolve it next.
264 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
265
266 // Now remove it
267 Def->removeValue(SMCTArgs[i]);
268 }
269 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000270 return Error(SubMultiClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000271 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000272 Twine(i) + " (" + SMCTArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000273 ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000274 }
275 }
276
277 return false;
278}
279
David Greenefb927af2012-02-22 16:09:41 +0000280/// ProcessForeachDefs - Given a record, apply all of the variable
281/// values in all surrounding foreach loops, creating new records for
282/// each combination of values.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000283bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
284 if (Loops.empty())
285 return false;
286
David Greenefb927af2012-02-22 16:09:41 +0000287 // We want to instantiate a new copy of CurRec for each combination
288 // of nested loop iterator values. We don't want top instantiate
289 // any copies until we have values for each loop iterator.
290 IterSet IterVals;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000291 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000292}
293
294/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
295/// apply each of the variable values in this loop and then process
296/// subloops.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000297bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
298 // Recursively build a tuple of iterator values.
299 if (IterVals.size() != Loops.size()) {
300 assert(IterVals.size() < Loops.size());
301 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silvafb509ed2012-10-10 20:24:43 +0000302 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Craig Topper011817a2014-04-09 04:50:04 +0000303 if (!List) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000304 Error(Loc, "Loop list is not a list");
305 return true;
306 }
David Greenefb927af2012-02-22 16:09:41 +0000307
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000308 // Process each value.
Craig Topper664f6a02015-06-02 04:15:57 +0000309 for (unsigned i = 0; i < List->size(); ++i) {
Craig Topper011817a2014-04-09 04:50:04 +0000310 Init *ItemVal = List->resolveListElementReference(*CurRec, nullptr, i);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000311 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
312 if (ProcessForeachDefs(CurRec, Loc, IterVals))
313 return true;
314 IterVals.pop_back();
315 }
316 return false;
317 }
318
319 // This is the bottom of the recursion. We have all of the iterator values
320 // for this point in the iteration space. Instantiate a new record to
321 // reflect this combination of values.
Craig Topper84138712014-11-29 05:31:10 +0000322 auto IterRec = make_unique<Record>(*CurRec);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000323
324 // Set the iterator values now.
Craig Topper8eb764c2015-06-02 06:19:28 +0000325 for (IterRecord &IR : IterVals) {
326 VarInit *IterVar = IR.IterVar;
327 TypedInit *IVal = dyn_cast<TypedInit>(IR.IterValue);
Craig Topper84138712014-11-29 05:31:10 +0000328 if (!IVal)
329 return Error(Loc, "foreach iterator value is untyped");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000330
331 IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false));
332
Craig Toppercfd81732016-01-04 03:15:08 +0000333 if (SetValue(IterRec.get(), Loc, IterVar->getName(), None, IVal))
Craig Topper84138712014-11-29 05:31:10 +0000334 return Error(Loc, "when instantiating this def");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000335
336 // Resolve it next.
337 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName()));
338
339 // Remove it.
340 IterRec->removeValue(IterVar->getName());
341 }
342
343 if (Records.getDef(IterRec->getNameInitAsString())) {
Artyom Skrobov8b985322014-06-10 12:41:14 +0000344 // If this record is anonymous, it's no problem, just generate a new name
Craig Topper84138712014-11-29 05:31:10 +0000345 if (!IterRec->isAnonymous())
346 return Error(Loc, "def already exists: " +IterRec->getNameInitAsString());
347
348 IterRec->setName(GetNewAnonymousName());
David Greenefb927af2012-02-22 16:09:41 +0000349 }
350
Craig Topper84138712014-11-29 05:31:10 +0000351 Record *IterRecSave = IterRec.get(); // Keep a copy before release.
Craig Toppercdab2322014-11-29 05:52:51 +0000352 Records.addDef(std::move(IterRec));
Craig Topper84138712014-11-29 05:31:10 +0000353 IterRecSave->resolveReferences();
David Greenefb927af2012-02-22 16:09:41 +0000354 return false;
355}
356
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000357//===----------------------------------------------------------------------===//
358// Parser Code
359//===----------------------------------------------------------------------===//
360
361/// isObjectStart - Return true if this is a valid first token for an Object.
362static bool isObjectStart(tgtok::TokKind K) {
363 return K == tgtok::Class || K == tgtok::Def ||
David Greenefb927af2012-02-22 16:09:41 +0000364 K == tgtok::Defm || K == tgtok::Let ||
365 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000366}
367
Alp Tokerce91fe52013-12-21 18:51:00 +0000368/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
369/// an identifier.
370std::string TGParser::GetNewAnonymousName() {
Aaron Ballman97a59fb2015-02-16 19:33:36 +0000371 return "anonymous_" + utostr(AnonCounter++);
Chris Lattner7538ed82010-10-05 22:51:56 +0000372}
373
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000374/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000375/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000376/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000377/// ObjectName ::= /*empty*/
378///
David Greene2affd672011-10-19 13:04:29 +0000379Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
380 switch (Lex.getCode()) {
381 case tgtok::colon:
382 case tgtok::semi:
383 case tgtok::l_brace:
384 // These are all of the tokens that can begin an object body.
385 // Some of these can also begin values but we disallow those cases
386 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000387 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000388 default:
389 break;
390 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000391
Craig Topper011817a2014-04-09 04:50:04 +0000392 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000393 if (CurMultiClass)
394 CurRec = &CurMultiClass->Rec;
395
Craig Topper011817a2014-04-09 04:50:04 +0000396 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000397 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000398 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000399 if (!CurRecName) {
400 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000401 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000402 }
403 Type = CurRecName->getType();
404 }
405
406 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000407}
408
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000409/// ParseClassID - Parse and resolve a reference to a class name. This returns
410/// null on error.
411///
412/// ClassID ::= ID
413///
414Record *TGParser::ParseClassID() {
415 if (Lex.getCode() != tgtok::Id) {
416 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000417 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000418 }
Bob Wilson7248f862009-11-22 04:24:42 +0000419
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000420 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000421 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000422 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000423
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000424 Lex.Lex();
425 return Result;
426}
427
Bob Wilson3d948162009-04-28 19:41:44 +0000428/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
429/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000430///
431/// MultiClassID ::= ID
432///
433MultiClass *TGParser::ParseMultiClassID() {
434 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000435 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000436 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000437 }
Bob Wilson3d948162009-04-28 19:41:44 +0000438
Craig Topper7adf2bf2014-12-11 05:25:30 +0000439 MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
Craig Topper4ca40012014-11-30 01:20:17 +0000440 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000441 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000442
David Greene753ed8f2009-04-22 16:42:54 +0000443 Lex.Lex();
Craig Topper4ca40012014-11-30 01:20:17 +0000444 return Result;
David Greene753ed8f2009-04-22 16:42:54 +0000445}
446
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000447/// ParseSubClassReference - Parse a reference to a subclass or to a templated
448/// subclass. This returns a SubClassRefTy with a null Record* on error.
449///
450/// SubClassRef ::= ClassID
451/// SubClassRef ::= ClassID '<' ValueList '>'
452///
453SubClassReference TGParser::
454ParseSubClassReference(Record *CurRec, bool isDefm) {
455 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000456 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000457
Sean Silva0657b402013-01-09 02:17:14 +0000458 if (isDefm) {
459 if (MultiClass *MC = ParseMultiClassID())
460 Result.Rec = &MC->Rec;
461 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000462 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000463 }
Craig Topper011817a2014-04-09 04:50:04 +0000464 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000465
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000466 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000467 if (Lex.getCode() != tgtok::less) {
468 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000469 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000470 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000471 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000472
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000473 if (Lex.getCode() == tgtok::greater) {
474 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000475 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000476 return Result;
477 }
Bob Wilson7248f862009-11-22 04:24:42 +0000478
David Greene8618f952009-06-08 20:23:18 +0000479 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000480 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000481 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000482 return Result;
483 }
Bob Wilson7248f862009-11-22 04:24:42 +0000484
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000485 if (Lex.getCode() != tgtok::greater) {
486 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000487 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000488 return Result;
489 }
490 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000491 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000492
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000493 return Result;
494}
495
Bob Wilson3d948162009-04-28 19:41:44 +0000496/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
497/// templated submulticlass. This returns a SubMultiClassRefTy with a null
498/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000499///
500/// SubMultiClassRef ::= MultiClassID
501/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
502///
503SubMultiClassReference TGParser::
504ParseSubMultiClassReference(MultiClass *CurMC) {
505 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000506 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000507
David Greene753ed8f2009-04-22 16:42:54 +0000508 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000509 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000510
David Greene753ed8f2009-04-22 16:42:54 +0000511 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000512 if (Lex.getCode() != tgtok::less) {
513 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000514 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000515 }
David Greene753ed8f2009-04-22 16:42:54 +0000516 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000517
David Greene753ed8f2009-04-22 16:42:54 +0000518 if (Lex.getCode() == tgtok::greater) {
519 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000520 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000521 return Result;
522 }
Bob Wilson3d948162009-04-28 19:41:44 +0000523
David Greene8618f952009-06-08 20:23:18 +0000524 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000525 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000526 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000527 return Result;
528 }
Bob Wilson3d948162009-04-28 19:41:44 +0000529
David Greene753ed8f2009-04-22 16:42:54 +0000530 if (Lex.getCode() != tgtok::greater) {
531 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000532 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000533 return Result;
534 }
535 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000536 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000537
538 return Result;
539}
540
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000541/// ParseRangePiece - Parse a bit/value range.
542/// RangePiece ::= INTVAL
543/// RangePiece ::= INTVAL '-' INTVAL
544/// RangePiece ::= INTVAL INTVAL
545bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000546 if (Lex.getCode() != tgtok::IntVal) {
547 TokError("expected integer or bitrange");
548 return true;
549 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000550 int64_t Start = Lex.getCurIntVal();
551 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000552
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000553 if (Start < 0)
554 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000555
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000556 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000557 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000558 Ranges.push_back(Start);
559 return false;
560 case tgtok::minus:
561 if (Lex.Lex() != tgtok::IntVal) {
562 TokError("expected integer value as end of range");
563 return true;
564 }
565 End = Lex.getCurIntVal();
566 break;
567 case tgtok::IntVal:
568 End = -Lex.getCurIntVal();
569 break;
570 }
Bob Wilson7248f862009-11-22 04:24:42 +0000571 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000572 return TokError("invalid range, cannot be negative");
573 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000574
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000575 // Add to the range.
Craig Toppera9642b42015-05-04 01:35:39 +0000576 if (Start < End)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000577 for (; Start <= End; ++Start)
578 Ranges.push_back(Start);
Craig Toppera9642b42015-05-04 01:35:39 +0000579 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000580 for (; Start >= End; --Start)
581 Ranges.push_back(Start);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000582 return false;
583}
584
585/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
586///
587/// RangeList ::= RangePiece (',' RangePiece)*
588///
589std::vector<unsigned> TGParser::ParseRangeList() {
590 std::vector<unsigned> Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000591
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000592 // Parse the first piece.
593 if (ParseRangePiece(Result))
594 return std::vector<unsigned>();
595 while (Lex.getCode() == tgtok::comma) {
596 Lex.Lex(); // Eat the comma.
597
598 // Parse the next range piece.
599 if (ParseRangePiece(Result))
600 return std::vector<unsigned>();
601 }
602 return Result;
603}
604
605/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
606/// OptionalRangeList ::= '<' RangeList '>'
607/// OptionalRangeList ::= /*empty*/
608bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
609 if (Lex.getCode() != tgtok::less)
610 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000611
Chris Lattner526c8cb2009-06-21 03:39:35 +0000612 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000613 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000614
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000615 // Parse the range list.
616 Ranges = ParseRangeList();
617 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000618
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000619 if (Lex.getCode() != tgtok::greater) {
620 TokError("expected '>' at end of range list");
621 return Error(StartLoc, "to match this '<'");
622 }
623 Lex.Lex(); // eat the '>'.
624 return false;
625}
626
627/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
628/// OptionalBitList ::= '{' RangeList '}'
629/// OptionalBitList ::= /*empty*/
630bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
631 if (Lex.getCode() != tgtok::l_brace)
632 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000633
Chris Lattner526c8cb2009-06-21 03:39:35 +0000634 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000635 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000636
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000637 // Parse the range list.
638 Ranges = ParseRangeList();
639 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000640
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000641 if (Lex.getCode() != tgtok::r_brace) {
642 TokError("expected '}' at end of bit list");
643 return Error(StartLoc, "to match this '{'");
644 }
645 Lex.Lex(); // eat the '}'.
646 return false;
647}
648
649
650/// ParseType - Parse and return a tblgen type. This returns null on error.
651///
652/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000653/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000654/// Type ::= BIT // bit type
655/// Type ::= BITS '<' INTVAL '>' // bits<x> type
656/// Type ::= INT // int type
657/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000658/// Type ::= DAG // dag type
659/// Type ::= ClassID // Record Type
660///
661RecTy *TGParser::ParseType() {
662 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000663 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000664 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000665 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000666 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
667 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000668 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000669 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000670 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000671 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000672 case tgtok::Bits: {
673 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
674 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000675 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000676 }
677 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
678 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000679 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000680 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000681 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000682 if (Lex.Lex() != tgtok::greater) { // Eat count.
683 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000684 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000685 }
686 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000687 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000688 }
689 case tgtok::List: {
690 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
691 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000692 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000693 }
694 Lex.Lex(); // Eat '<'
695 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000696 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000697
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000698 if (Lex.getCode() != tgtok::greater) {
699 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000700 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000701 }
702 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000703 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000704 }
Bob Wilson7248f862009-11-22 04:24:42 +0000705 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000706}
707
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000708/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
709/// has already been read.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000710Init *TGParser::ParseIDValue(Record *CurRec,
David Greened4263a62011-10-19 13:04:20 +0000711 const std::string &Name, SMLoc NameLoc,
712 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000713 if (CurRec) {
714 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000715 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000716
David Greenedb10e692011-10-19 13:02:42 +0000717 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
718
David Greene47a665e2011-10-05 22:42:54 +0000719 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +0000720 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
721 "::");
David Greene47a665e2011-10-05 22:42:54 +0000722
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000723 if (CurRec->isTemplateArg(TemplateArgName)) {
724 const RecordVal *RV = CurRec->getValue(TemplateArgName);
725 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000726 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000727 }
728 }
Bob Wilson7248f862009-11-22 04:24:42 +0000729
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000730 if (CurMultiClass) {
David Greenedb10e692011-10-19 13:02:42 +0000731 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
732 "::");
733
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000734 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
735 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
736 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000737 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000738 }
739 }
Bob Wilson7248f862009-11-22 04:24:42 +0000740
David Greenefb927af2012-02-22 16:09:41 +0000741 // If this is in a foreach loop, make sure it's not a loop iterator
Craig Toppereb4d7c62015-04-29 04:43:36 +0000742 for (const auto &L : Loops) {
743 VarInit *IterVar = dyn_cast<VarInit>(L.IterVar);
David Greenefb927af2012-02-22 16:09:41 +0000744 if (IterVar && IterVar->getName() == Name)
745 return IterVar;
746 }
747
David Greene232bd602011-10-19 13:04:21 +0000748 if (Mode == ParseNameMode)
749 return StringInit::get(Name);
750
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000751 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000752 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000753
David Greene232bd602011-10-19 13:04:21 +0000754 if (Mode == ParseValueMode) {
755 Error(NameLoc, "Variable not defined: '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000756 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000757 }
Craig Toppera9642b42015-05-04 01:35:39 +0000758
David Greene232bd602011-10-19 13:04:21 +0000759 return StringInit::get(Name);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000760}
761
David Greene5d0c0512009-05-14 20:54:48 +0000762/// ParseOperation - Parse an operator. This returns null on error.
763///
764/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
765///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000766Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000767 switch (Lex.getCode()) {
768 default:
769 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000770 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000771 case tgtok::XHead:
772 case tgtok::XTail:
773 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000774 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
775 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000776 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000777
David Greenee8f3b272009-05-14 21:22:49 +0000778 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000779 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000780 case tgtok::XCast:
781 Lex.Lex(); // eat the operation
782 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000783
David Greenee8f3b272009-05-14 21:22:49 +0000784 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000785
Craig Topper011817a2014-04-09 04:50:04 +0000786 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000787 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000788 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000789 }
David Greene5d0c0512009-05-14 20:54:48 +0000790
David Greenee8f3b272009-05-14 21:22:49 +0000791 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000792 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000793 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000794 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000795 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000796 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000797 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000798 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000799 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000800 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000801 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000802 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000803 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000804 break;
David Greenee8f3b272009-05-14 21:22:49 +0000805 }
806 if (Lex.getCode() != tgtok::l_paren) {
807 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000808 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000809 }
810 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000811
David Greeneaf8ee2c2011-07-29 22:43:06 +0000812 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000813 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000814
Craig Topper85c07002015-04-30 05:54:22 +0000815 if (Code == UnOpInit::HEAD ||
816 Code == UnOpInit::TAIL ||
817 Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000818 ListInit *LHSl = dyn_cast<ListInit>(LHS);
819 StringInit *LHSs = dyn_cast<StringInit>(LHS);
820 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000821 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000822 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000823 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000824 }
825 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000826 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
827 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000828 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000829 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000830 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000831 }
832 }
833
Craig Topper85c07002015-04-30 05:54:22 +0000834 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
Craig Topper011817a2014-04-09 04:50:04 +0000835 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000836 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000837 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000838 }
Bob Wilson7248f862009-11-22 04:24:42 +0000839
Craig Topperec9072d2015-05-14 05:53:53 +0000840 if (LHSl && LHSl->empty()) {
David Greened571b3c2009-05-14 22:38:31 +0000841 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000842 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000843 }
844 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000845 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000846 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000847 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000848 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000849 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000850 }
Craig Toppera9642b42015-05-04 01:35:39 +0000851 Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
852 : ListRecTy::get(Itemt->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000853 } else {
David Greened571b3c2009-05-14 22:38:31 +0000854 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000855 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000856 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000857 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000858 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000859 }
Craig Toppera9642b42015-05-04 01:35:39 +0000860 Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
David Greened571b3c2009-05-14 22:38:31 +0000861 }
862 }
863 }
864
David Greenee8f3b272009-05-14 21:22:49 +0000865 if (Lex.getCode() != tgtok::r_paren) {
866 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000867 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000868 }
869 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000870 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000871 }
David Greene5d0c0512009-05-14 20:54:48 +0000872
873 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000874 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000875 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +0000876 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000877 case tgtok::XSRL:
878 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000879 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000880 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000881 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000882 tgtok::TokKind OpTok = Lex.getCode();
883 SMLoc OpLoc = Lex.getLoc();
884 Lex.Lex(); // eat the operation
885
David Greene5d0c0512009-05-14 20:54:48 +0000886 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000887 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000888
Chris Lattner61ea00b2010-10-05 23:58:18 +0000889 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000890 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000891 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000892 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000893 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000894 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
895 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
896 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
897 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000898 case tgtok::XListConcat:
899 Code = BinOpInit::LISTCONCAT;
900 // We don't know the list type until we parse the first argument
901 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000902 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000903 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000904 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000905 break;
David Greene5d0c0512009-05-14 20:54:48 +0000906 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000907
David Greene5d0c0512009-05-14 20:54:48 +0000908 if (Lex.getCode() != tgtok::l_paren) {
909 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000910 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000911 }
912 Lex.Lex(); // eat the '('
913
David Greeneaf8ee2c2011-07-29 22:43:06 +0000914 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000915
Chris Lattner61ea00b2010-10-05 23:58:18 +0000916 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000917 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000918
Chris Lattner61ea00b2010-10-05 23:58:18 +0000919 while (Lex.getCode() == tgtok::comma) {
920 Lex.Lex(); // eat the ','
921
922 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000923 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000924 }
David Greene5d0c0512009-05-14 20:54:48 +0000925
926 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000927 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000928 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000929 }
930 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000931
Daniel Sanders314e80e2014-05-07 10:13:19 +0000932 // If we are doing !listconcat, we should know the type by now
933 if (OpTok == tgtok::XListConcat) {
934 if (VarInit *Arg0 = dyn_cast<VarInit>(InitList[0]))
935 Type = Arg0->getType();
936 else if (ListInit *Arg0 = dyn_cast<ListInit>(InitList[0]))
937 Type = Arg0->getType();
938 else {
939 InitList[0]->dump();
940 Error(OpLoc, "expected a list");
941 return nullptr;
942 }
943 }
944
Chris Lattner61ea00b2010-10-05 23:58:18 +0000945 // We allow multiple operands to associative operators like !strconcat as
946 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000947 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000948 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000949 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000950 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
951 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000952 InitList.back() = RHS;
953 }
954 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000955
Chris Lattner61ea00b2010-10-05 23:58:18 +0000956 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000957 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000958 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000959
Chris Lattner61ea00b2010-10-05 23:58:18 +0000960 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +0000961 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000962 }
963
David Greene3587eed2009-05-14 23:26:46 +0000964 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +0000965 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +0000966 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
967 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000968 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000969
David Greene98ed3c72009-05-14 21:54:42 +0000970 tgtok::TokKind LexCode = Lex.getCode();
971 Lex.Lex(); // eat the operation
972 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +0000973 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +0000974 case tgtok::XIf:
975 Code = TernOpInit::IF;
976 break;
David Greenee917fff2009-05-14 22:23:47 +0000977 case tgtok::XForEach:
978 Code = TernOpInit::FOREACH;
979 break;
David Greene98ed3c72009-05-14 21:54:42 +0000980 case tgtok::XSubst:
981 Code = TernOpInit::SUBST;
982 break;
983 }
984 if (Lex.getCode() != tgtok::l_paren) {
985 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000986 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +0000987 }
988 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000989
David Greeneaf8ee2c2011-07-29 22:43:06 +0000990 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000991 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000992
David Greene98ed3c72009-05-14 21:54:42 +0000993 if (Lex.getCode() != tgtok::comma) {
994 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000995 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +0000996 }
997 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +0000998
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000999 Init *MHS = ParseValue(CurRec, ItemType);
1000 if (!MHS)
1001 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001002
David Greene98ed3c72009-05-14 21:54:42 +00001003 if (Lex.getCode() != tgtok::comma) {
1004 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001005 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001006 }
1007 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001008
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001009 Init *RHS = ParseValue(CurRec, ItemType);
1010 if (!RHS)
1011 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001012
David Greene98ed3c72009-05-14 21:54:42 +00001013 if (Lex.getCode() != tgtok::r_paren) {
1014 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001015 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001016 }
1017 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001018
David Greene98ed3c72009-05-14 21:54:42 +00001019 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001020 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001021 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001022 RecTy *MHSTy = nullptr;
1023 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001024
Sean Silvafb509ed2012-10-10 20:24:43 +00001025 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001026 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001027 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001028 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001029 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001030 MHSTy = BitRecTy::get();
1031
Sean Silvafb509ed2012-10-10 20:24:43 +00001032 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001033 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001034 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001035 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001036 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001037 RHSTy = BitRecTy::get();
1038
1039 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001040 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001041 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001042 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001043 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001044
1045 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001046 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001047 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001048 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001049
1050 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1051 Type = RHSTy;
1052 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1053 Type = MHSTy;
Bob Wilson7248f862009-11-22 04:24:42 +00001054 } else {
David Greene3587eed2009-05-14 23:26:46 +00001055 TokError("inconsistent types for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001056 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001057 }
1058 break;
1059 }
David Greenee917fff2009-05-14 22:23:47 +00001060 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001061 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
Craig Topper011817a2014-04-09 04:50:04 +00001062 if (!MHSt) {
David Greenee917fff2009-05-14 22:23:47 +00001063 TokError("could not get type for !foreach");
Craig Topper011817a2014-04-09 04:50:04 +00001064 return nullptr;
David Greenee917fff2009-05-14 22:23:47 +00001065 }
1066 Type = MHSt->getType();
1067 break;
1068 }
David Greene98ed3c72009-05-14 21:54:42 +00001069 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001070 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001071 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001072 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001073 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001074 }
1075 Type = RHSt->getType();
1076 break;
1077 }
1078 }
David Greenee32ebf22011-07-29 19:07:07 +00001079 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001080 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001081 }
David Greene5d0c0512009-05-14 20:54:48 +00001082 }
David Greene5d0c0512009-05-14 20:54:48 +00001083}
1084
1085/// ParseOperatorType - Parse a type for an operator. This returns
1086/// null on error.
1087///
1088/// OperatorType ::= '<' Type '>'
1089///
Dan Gohman1432ef82009-08-12 22:10:57 +00001090RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001091 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001092
1093 if (Lex.getCode() != tgtok::less) {
1094 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001095 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001096 }
1097 Lex.Lex(); // eat the <
1098
1099 Type = ParseType();
1100
Craig Topper011817a2014-04-09 04:50:04 +00001101 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001102 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001103 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001104 }
1105
1106 if (Lex.getCode() != tgtok::greater) {
1107 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001108 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001109 }
1110 Lex.Lex(); // eat the >
1111
1112 return Type;
1113}
1114
1115
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001116/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1117///
1118/// SimpleValue ::= IDValue
1119/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001120/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001121/// SimpleValue ::= CODEFRAGMENT
1122/// SimpleValue ::= '?'
1123/// SimpleValue ::= '{' ValueList '}'
1124/// SimpleValue ::= ID '<' ValueListNE '>'
1125/// SimpleValue ::= '[' ValueList ']'
1126/// SimpleValue ::= '(' IDValue DagArgList ')'
1127/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001128/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001129/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1130/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1131/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001132/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001133/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1134///
David Greened4263a62011-10-19 13:04:20 +00001135Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1136 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001137 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001138 switch (Lex.getCode()) {
1139 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001140 case tgtok::paste:
1141 // This is a leading paste operation. This is deprecated but
1142 // still exists in some .td files. Ignore it.
1143 Lex.Lex(); // Skip '#'.
1144 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001145 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001146 case tgtok::BinaryIntVal: {
1147 auto BinaryVal = Lex.getCurBinaryIntVal();
1148 SmallVector<Init*, 16> Bits(BinaryVal.second);
1149 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001150 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001151 R = BitsInit::get(Bits);
1152 Lex.Lex();
1153 break;
1154 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001155 case tgtok::StrVal: {
1156 std::string Val = Lex.getCurStrVal();
1157 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001158
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001159 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001160 while (Lex.getCode() == tgtok::StrVal) {
1161 Val += Lex.getCurStrVal();
1162 Lex.Lex();
1163 }
Bob Wilson7248f862009-11-22 04:24:42 +00001164
David Greenee32ebf22011-07-29 19:07:07 +00001165 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001166 break;
1167 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001168 case tgtok::CodeFragment:
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +00001169 R = StringInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001170 Lex.Lex();
1171 break;
1172 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001173 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001174 Lex.Lex();
1175 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001176 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001177 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001178 std::string Name = Lex.getCurStrVal();
1179 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001180 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001181
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001182 // Value ::= ID '<' ValueListNE '>'
1183 if (Lex.Lex() == tgtok::greater) {
1184 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001185 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001186 }
David Greene8618f952009-06-08 20:23:18 +00001187
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001188 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1189 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1190 // body.
1191 Record *Class = Records.getClass(Name);
1192 if (!Class) {
1193 Error(NameLoc, "Expected a class name, got '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001194 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001195 }
David Greene8618f952009-06-08 20:23:18 +00001196
David Greeneaf8ee2c2011-07-29 22:43:06 +00001197 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
Craig Topper011817a2014-04-09 04:50:04 +00001198 if (ValueList.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001199
David Greene8618f952009-06-08 20:23:18 +00001200 if (Lex.getCode() != tgtok::greater) {
1201 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001202 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001203 }
1204 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001205 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001206
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001207 // Create the new record, set it as CurRec temporarily.
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00001208 auto NewRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), NameLoc,
1209 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001210 Record *NewRec = NewRecOwner.get(); // Keep a copy since we may release.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001211 SubClassReference SCRef;
Jordan Rosef12e8a92013-01-10 18:50:11 +00001212 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001213 SCRef.Rec = Class;
1214 SCRef.TemplateArgs = ValueList;
1215 // Add info about the subclass to NewRec.
Craig Topper84138712014-11-29 05:31:10 +00001216 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001217 return nullptr;
Craig Topper84138712014-11-29 05:31:10 +00001218
Hal Finkela8c1f462014-01-02 20:47:09 +00001219 if (!CurMultiClass) {
1220 NewRec->resolveReferences();
Craig Toppercdab2322014-11-29 05:52:51 +00001221 Records.addDef(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001222 } else {
Adam Nemete5a07162014-09-16 17:14:13 +00001223 // This needs to get resolved once the multiclass template arguments are
1224 // known before any use.
1225 NewRec->setResolveFirst(true);
Hal Finkela8c1f462014-01-02 20:47:09 +00001226 // Otherwise, we're inside a multiclass, add it to the multiclass.
Craig Topperc3504c42014-12-11 05:25:33 +00001227 CurMultiClass->DefPrototypes.push_back(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001228
1229 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00001230 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
1231 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Hal Finkela8c1f462014-01-02 20:47:09 +00001232 assert(RV && "Template arg doesn't exist?");
1233 NewRec->addValue(*RV);
1234 }
1235
1236 // We can't return the prototype def here, instead return:
1237 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1238 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1239 assert(MCNameRV && "multiclass record must have a NAME");
1240
1241 return UnOpInit::get(UnOpInit::CAST,
1242 BinOpInit::get(BinOpInit::STRCONCAT,
1243 VarInit::get(MCNameRV->getName(),
1244 MCNameRV->getType()),
1245 NewRec->getNameInit(),
1246 StringRecTy::get()),
1247 Class->getDefInit()->getType());
1248 }
Bob Wilson7248f862009-11-22 04:24:42 +00001249
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001250 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001251 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001252 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001253 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001254 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001255 Lex.Lex(); // eat the '{'
David Greeneaf8ee2c2011-07-29 22:43:06 +00001256 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001257
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001258 if (Lex.getCode() != tgtok::r_brace) {
1259 Vals = ParseValueList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001260 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001261 }
1262 if (Lex.getCode() != tgtok::r_brace) {
1263 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001264 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001265 }
1266 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001267
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001268 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001269
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001270 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1271 // first. We'll first read everything in to a vector, then we can reverse
1272 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001273 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001274 // FIXME: The following two loops would not be duplicated
1275 // if the API was a little more orthogonal.
1276
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001277 // bits<n> values are allowed to initialize n bits.
1278 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1279 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1280 NewBits.push_back(BI->getBit((e - i) - 1));
1281 continue;
1282 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001283 // bits<n> can also come from variable initializers.
1284 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1285 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1286 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1287 NewBits.push_back(VI->getBit((e - i) - 1));
1288 continue;
1289 }
1290 // Fallthrough to try convert this to a bit.
1291 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001292 // All other values must be convertible to just a single bit.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001293 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001294 if (!Bit) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001295 Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
Chris Lattner52416952007-11-22 21:06:59 +00001296 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001297 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001298 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001299 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001300 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001301 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001302 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001303 }
1304 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1305 Lex.Lex(); // eat the '['
David Greeneaf8ee2c2011-07-29 22:43:06 +00001306 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001307
Craig Topper011817a2014-04-09 04:50:04 +00001308 RecTy *DeducedEltTy = nullptr;
1309 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001310
Craig Topper011817a2014-04-09 04:50:04 +00001311 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001312 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001313 if (!ListType) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001314 TokError(Twine("Type mismatch for list, expected list type, got ") +
1315 ItemType->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001316 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001317 }
1318 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001319 }
David Greene8618f952009-06-08 20:23:18 +00001320
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001321 if (Lex.getCode() != tgtok::r_square) {
Craig Topper011817a2014-04-09 04:50:04 +00001322 Vals = ParseValueList(CurRec, nullptr,
1323 GivenListTy ? GivenListTy->getElementType() : nullptr);
1324 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001325 }
1326 if (Lex.getCode() != tgtok::r_square) {
1327 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001328 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001329 }
1330 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001331
Craig Topper011817a2014-04-09 04:50:04 +00001332 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001333 if (Lex.getCode() == tgtok::less) {
1334 // Optional list element type
1335 Lex.Lex(); // eat the '<'
1336
1337 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001338 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001339 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001340 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001341 }
1342
1343 if (Lex.getCode() != tgtok::greater) {
1344 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001345 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001346 }
1347 Lex.Lex(); // eat the '>'
1348 }
1349
1350 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001351 RecTy *EltTy = nullptr;
Craig Toppereb4d7c62015-04-29 04:43:36 +00001352 for (Init *V : Vals) {
1353 TypedInit *TArg = dyn_cast<TypedInit>(V);
Craig Topper011817a2014-04-09 04:50:04 +00001354 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001355 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001356 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001357 }
Craig Topper011817a2014-04-09 04:50:04 +00001358 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001359 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001360 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001361 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001362 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001363 }
Bob Wilson7248f862009-11-22 04:24:42 +00001364 } else {
David Greene8618f952009-06-08 20:23:18 +00001365 EltTy = TArg->getType();
1366 }
1367 }
1368
Craig Topper011817a2014-04-09 04:50:04 +00001369 if (GivenEltTy) {
1370 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001371 // Verify consistency
1372 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1373 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001374 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001375 }
1376 }
1377 EltTy = GivenEltTy;
1378 }
1379
Craig Topper011817a2014-04-09 04:50:04 +00001380 if (!EltTy) {
1381 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001382 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001383 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001384 }
1385 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001386 } else {
David Greene8618f952009-06-08 20:23:18 +00001387 // Make sure the deduced type is compatible with the given type
1388 if (GivenListTy) {
1389 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1390 TokError("Element type mismatch for list");
Craig Topper011817a2014-04-09 04:50:04 +00001391 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001392 }
1393 }
1394 DeducedEltTy = EltTy;
1395 }
Bob Wilson7248f862009-11-22 04:24:42 +00001396
David Greenee32ebf22011-07-29 19:07:07 +00001397 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001398 }
1399 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1400 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001401 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001402 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001403 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001404 }
Bob Wilson7248f862009-11-22 04:24:42 +00001405
David Greeneaf8ee2c2011-07-29 22:43:06 +00001406 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001407 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001408
Nate Begemandbe3f772009-03-19 05:21:56 +00001409 // If the operator name is present, parse it.
1410 std::string OperatorName;
1411 if (Lex.getCode() == tgtok::colon) {
1412 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1413 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001414 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001415 }
1416 OperatorName = Lex.getCurStrVal();
1417 Lex.Lex(); // eat the VarName.
1418 }
Bob Wilson7248f862009-11-22 04:24:42 +00001419
David Greeneaf8ee2c2011-07-29 22:43:06 +00001420 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001421 if (Lex.getCode() != tgtok::r_paren) {
1422 DagArgs = ParseDagArgList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001423 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001424 }
Bob Wilson7248f862009-11-22 04:24:42 +00001425
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001426 if (Lex.getCode() != tgtok::r_paren) {
1427 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001428 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001429 }
1430 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001431
David Greenee32ebf22011-07-29 19:07:07 +00001432 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001433 }
Bob Wilson7248f862009-11-22 04:24:42 +00001434
David Greene2f7cf7f2011-01-07 17:05:37 +00001435 case tgtok::XHead:
1436 case tgtok::XTail:
1437 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001438 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001439 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001440 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001441 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +00001442 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001443 case tgtok::XSRL:
1444 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001445 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001446 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001447 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001448 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001449 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001450 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001451 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001452 }
1453 }
Bob Wilson7248f862009-11-22 04:24:42 +00001454
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001455 return R;
1456}
1457
1458/// ParseValue - Parse a tblgen value. This returns null on error.
1459///
1460/// Value ::= SimpleValue ValueSuffix*
1461/// ValueSuffix ::= '{' BitList '}'
1462/// ValueSuffix ::= '[' BitList ']'
1463/// ValueSuffix ::= '.' ID
1464///
David Greened4263a62011-10-19 13:04:20 +00001465Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1466 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001467 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001468
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001469 // Parse the suffixes now if present.
1470 while (1) {
1471 switch (Lex.getCode()) {
1472 default: return Result;
1473 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001474 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001475 // This is the beginning of the object body.
1476 return Result;
1477
Chris Lattner526c8cb2009-06-21 03:39:35 +00001478 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001479 Lex.Lex(); // eat the '{'
1480 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001481 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001482
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001483 // Reverse the bitlist.
1484 std::reverse(Ranges.begin(), Ranges.end());
1485 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001486 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001487 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001488 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001489 }
Bob Wilson7248f862009-11-22 04:24:42 +00001490
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001491 // Eat the '}'.
1492 if (Lex.getCode() != tgtok::r_brace) {
1493 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001494 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001495 }
1496 Lex.Lex();
1497 break;
1498 }
1499 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001500 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001501 Lex.Lex(); // eat the '['
1502 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001503 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001504
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001505 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001506 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001507 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001508 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001509 }
Bob Wilson7248f862009-11-22 04:24:42 +00001510
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001511 // Eat the ']'.
1512 if (Lex.getCode() != tgtok::r_square) {
1513 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001514 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001515 }
1516 Lex.Lex();
1517 break;
1518 }
1519 case tgtok::period:
1520 if (Lex.Lex() != tgtok::Id) { // eat the .
1521 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001522 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001523 }
1524 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001525 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001526 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001527 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001528 }
David Greenee32ebf22011-07-29 19:07:07 +00001529 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001530 Lex.Lex(); // eat field name
1531 break;
David Greene8e85b482011-10-19 13:04:43 +00001532
1533 case tgtok::paste:
1534 SMLoc PasteLoc = Lex.getLoc();
1535
1536 // Create a !strconcat() operation, first casting each operand to
1537 // a string if necessary.
1538
Sean Silvafb509ed2012-10-10 20:24:43 +00001539 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001540 if (!LHS) {
1541 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001542 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001543 }
Craig Toppera9642b42015-05-04 01:35:39 +00001544
David Greene8e85b482011-10-19 13:04:43 +00001545 if (LHS->getType() != StringRecTy::get()) {
1546 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1547 }
1548
Craig Topper011817a2014-04-09 04:50:04 +00001549 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001550
1551 Lex.Lex(); // Eat the '#'.
1552 switch (Lex.getCode()) {
1553 case tgtok::colon:
1554 case tgtok::semi:
1555 case tgtok::l_brace:
1556 // These are all of the tokens that can begin an object body.
1557 // Some of these can also begin values but we disallow those cases
1558 // because they are unlikely to be useful.
Craig Toppera9642b42015-05-04 01:35:39 +00001559
David Greene8e85b482011-10-19 13:04:43 +00001560 // Trailing paste, concat with an empty string.
1561 RHS = StringInit::get("");
1562 break;
1563
1564 default:
1565 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001566 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001567 if (!RHS) {
1568 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001569 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001570 }
1571
1572 if (RHS->getType() != StringRecTy::get()) {
1573 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1574 }
Craig Toppera9642b42015-05-04 01:35:39 +00001575
David Greene8e85b482011-10-19 13:04:43 +00001576 break;
1577 }
1578
1579 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1580 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1581 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001582 }
1583 }
1584}
1585
1586/// ParseDagArgList - Parse the argument list for a dag literal expression.
1587///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001588/// DagArg ::= Value (':' VARNAME)?
1589/// DagArg ::= VARNAME
1590/// DagArgList ::= DagArg
1591/// DagArgList ::= DagArgList ',' DagArg
David Greeneaf8ee2c2011-07-29 22:43:06 +00001592std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001593TGParser::ParseDagArgList(Record *CurRec) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001594 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson7248f862009-11-22 04:24:42 +00001595
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001596 while (1) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001597 // DagArg ::= VARNAME
1598 if (Lex.getCode() == tgtok::VarName) {
1599 // A missing value is treated like '?'.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00001600 Result.emplace_back(UnsetInit::get(), Lex.getCurStrVal());
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001601 Lex.Lex();
1602 } else {
1603 // DagArg ::= Value (':' VARNAME)?
1604 Init *Val = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001605 if (!Val)
David Greeneaf8ee2c2011-07-29 22:43:06 +00001606 return std::vector<std::pair<llvm::Init*, std::string> >();
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001607
1608 // If the variable name is present, add it.
1609 std::string VarName;
1610 if (Lex.getCode() == tgtok::colon) {
1611 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1612 TokError("expected variable name in dag literal");
1613 return std::vector<std::pair<llvm::Init*, std::string> >();
1614 }
1615 VarName = Lex.getCurStrVal();
1616 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001617 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001618
1619 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001620 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001621 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001622 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001623 }
Bob Wilson7248f862009-11-22 04:24:42 +00001624
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001625 return Result;
1626}
1627
1628
1629/// ParseValueList - Parse a comma separated list of values, returning them as a
1630/// vector. Note that this always expects to be able to parse at least one
1631/// value. It returns an empty list if this is not possible.
1632///
1633/// ValueList ::= Value (',' Value)
1634///
David Greeneaf8ee2c2011-07-29 22:43:06 +00001635std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopher71520a82011-07-11 23:06:52 +00001636 RecTy *EltTy) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001637 std::vector<Init*> Result;
David Greene8618f952009-06-08 20:23:18 +00001638 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001639 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001640 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001641 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001642 if (TArgs.empty()) {
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001643 TokError("template argument provided to non-template class");
1644 return std::vector<Init*>();
1645 }
David Greene8618f952009-06-08 20:23:18 +00001646 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001647 if (!RV) {
1648 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1649 << ")\n";
1650 }
David Greene8618f952009-06-08 20:23:18 +00001651 assert(RV && "Template argument record not found??");
1652 ItemType = RV->getType();
1653 ++ArgN;
1654 }
1655 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001656 if (!Result.back()) return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001657
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001658 while (Lex.getCode() == tgtok::comma) {
1659 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001660
Craig Topper011817a2014-04-09 04:50:04 +00001661 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001662 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001663 if (ArgN >= TArgs.size()) {
1664 TokError("too many template arguments");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001665 return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001666 }
David Greene8618f952009-06-08 20:23:18 +00001667 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1668 assert(RV && "Template argument record not found??");
1669 ItemType = RV->getType();
1670 ++ArgN;
1671 }
1672 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001673 if (!Result.back()) return std::vector<Init*>();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001674 }
Bob Wilson7248f862009-11-22 04:24:42 +00001675
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001676 return Result;
1677}
1678
1679
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001680/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1681/// empty string on error. This can happen in a number of different context's,
1682/// including within a def or in the template args for a def (which which case
1683/// CurRec will be non-null) and within the template args for a multiclass (in
1684/// which case CurRec will be null, but CurMultiClass will be set). This can
1685/// also happen within a def that is within a multiclass, which will set both
1686/// CurRec and CurMultiClass.
1687///
1688/// Declaration ::= FIELD? Type ID ('=' Value)?
1689///
David Greenedb10e692011-10-19 13:02:42 +00001690Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001691 bool ParsingTemplateArgs) {
1692 // Read the field prefix if present.
1693 bool HasField = Lex.getCode() == tgtok::Field;
1694 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001695
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001696 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001697 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001698
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001699 if (Lex.getCode() != tgtok::Id) {
1700 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001701 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001702 }
Bob Wilson7248f862009-11-22 04:24:42 +00001703
Chris Lattner526c8cb2009-06-21 03:39:35 +00001704 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001705 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001706 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001707
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001708 if (ParsingTemplateArgs) {
Craig Toppera9642b42015-05-04 01:35:39 +00001709 if (CurRec)
David Greenedb10e692011-10-19 13:02:42 +00001710 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Craig Toppera9642b42015-05-04 01:35:39 +00001711 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001712 assert(CurMultiClass);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001713 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001714 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1715 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001716 }
Bob Wilson7248f862009-11-22 04:24:42 +00001717
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001718 // Add the value.
1719 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001720 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001721
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001722 // If a value is present, parse it.
1723 if (Lex.getCode() == tgtok::equal) {
1724 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001725 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001726 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001727 if (!Val ||
Craig Toppercfd81732016-01-04 03:15:08 +00001728 SetValue(CurRec, ValLoc, DeclName, None, Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001729 // Return the name, even if an error is thrown. This is so that we can
1730 // continue to make some progress, even without the value having been
1731 // initialized.
1732 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001733 }
Bob Wilson7248f862009-11-22 04:24:42 +00001734
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001735 return DeclName;
1736}
1737
David Greenefb927af2012-02-22 16:09:41 +00001738/// ParseForeachDeclaration - Read a foreach declaration, returning
1739/// the name of the declared object or a NULL Init on error. Return
1740/// the name of the parsed initializer list through ForeachListName.
1741///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001742/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1743/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1744/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001745///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001746VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001747 if (Lex.getCode() != tgtok::Id) {
1748 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001749 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001750 }
1751
1752 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1753 Lex.Lex();
1754
1755 // If a value is present, parse it.
1756 if (Lex.getCode() != tgtok::equal) {
1757 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001758 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001759 }
1760 Lex.Lex(); // Eat the '='
1761
Craig Topper011817a2014-04-09 04:50:04 +00001762 RecTy *IterType = nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001763 std::vector<unsigned> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001764
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001765 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001766 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001767 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001768 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001769 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001770 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001771 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001772 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001773 }
1774 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001775 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001776 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001777 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001778 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001779 }
1780 IterType = ListType->getElementType();
1781 break;
David Greenefb927af2012-02-22 16:09:41 +00001782 }
1783
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001784 case tgtok::IntVal: { // RangePiece.
1785 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001786 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001787 break;
David Greenefb927af2012-02-22 16:09:41 +00001788 }
1789
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001790 case tgtok::l_brace: { // '{' RangeList '}'
1791 Lex.Lex(); // eat the '{'
1792 Ranges = ParseRangeList();
1793 if (Lex.getCode() != tgtok::r_brace) {
1794 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001795 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001796 }
1797 Lex.Lex();
1798 break;
1799 }
1800 }
David Greenefb927af2012-02-22 16:09:41 +00001801
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001802 if (!Ranges.empty()) {
1803 assert(!IterType && "Type already initialized?");
1804 IterType = IntRecTy::get();
1805 std::vector<Init*> Values;
Craig Topper8eb764c2015-06-02 06:19:28 +00001806 for (unsigned R : Ranges)
1807 Values.push_back(IntInit::get(R));
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001808 ForeachListValue = ListInit::get(Values, IterType);
1809 }
1810
1811 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001812 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001813
1814 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001815}
1816
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001817/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1818/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1819/// template args for a def, which may or may not be in a multiclass. If null,
1820/// these are the template args for a multiclass.
1821///
1822/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001823///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001824bool TGParser::ParseTemplateArgList(Record *CurRec) {
1825 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1826 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001827
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001828 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001829
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001830 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001831 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001832 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001833 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001834
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001835 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001836
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001837 while (Lex.getCode() == tgtok::comma) {
1838 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001839
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001840 // Read the following declarations.
1841 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001842 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001843 return true;
1844 TheRecToAddTo->addTemplateArg(TemplArg);
1845 }
Bob Wilson7248f862009-11-22 04:24:42 +00001846
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001847 if (Lex.getCode() != tgtok::greater)
1848 return TokError("expected '>' at end of template argument list");
1849 Lex.Lex(); // eat the '>'.
1850 return false;
1851}
1852
1853
1854/// ParseBodyItem - Parse a single item at within the body of a def or class.
1855///
1856/// BodyItem ::= Declaration ';'
1857/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1858bool TGParser::ParseBodyItem(Record *CurRec) {
1859 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001860 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001861 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001862
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001863 if (Lex.getCode() != tgtok::semi)
1864 return TokError("expected ';' after declaration");
1865 Lex.Lex();
1866 return false;
1867 }
1868
1869 // LET ID OptionalRangeList '=' Value ';'
1870 if (Lex.Lex() != tgtok::Id)
1871 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001872
Chris Lattner526c8cb2009-06-21 03:39:35 +00001873 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001874 std::string FieldName = Lex.getCurStrVal();
1875 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001876
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001877 std::vector<unsigned> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001878 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001879 return true;
1880 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001881
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001882 if (Lex.getCode() != tgtok::equal)
1883 return TokError("expected '=' in let expression");
1884 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001885
David Greene8618f952009-06-08 20:23:18 +00001886 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001887 if (!Field)
David Greene8618f952009-06-08 20:23:18 +00001888 return TokError("Value '" + FieldName + "' unknown!");
1889
1890 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001891
David Greeneaf8ee2c2011-07-29 22:43:06 +00001892 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001893 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001894
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001895 if (Lex.getCode() != tgtok::semi)
1896 return TokError("expected ';' after let expression");
1897 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001898
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001899 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1900}
1901
1902/// ParseBody - Read the body of a class or def. Return true on error, false on
1903/// success.
1904///
1905/// Body ::= ';'
1906/// Body ::= '{' BodyList '}'
1907/// BodyList BodyItem*
1908///
1909bool TGParser::ParseBody(Record *CurRec) {
1910 // If this is a null definition, just eat the semi and return.
1911 if (Lex.getCode() == tgtok::semi) {
1912 Lex.Lex();
1913 return false;
1914 }
Bob Wilson7248f862009-11-22 04:24:42 +00001915
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001916 if (Lex.getCode() != tgtok::l_brace)
1917 return TokError("Expected ';' or '{' to start body");
1918 // Eat the '{'.
1919 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001920
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001921 while (Lex.getCode() != tgtok::r_brace)
1922 if (ParseBodyItem(CurRec))
1923 return true;
1924
1925 // Eat the '}'.
1926 Lex.Lex();
1927 return false;
1928}
1929
Sean Silvacb1a75e2013-01-09 04:49:14 +00001930/// \brief Apply the current let bindings to \a CurRec.
1931/// \returns true on error, false otherwise.
1932bool TGParser::ApplyLetStack(Record *CurRec) {
Craig Topper8eb764c2015-06-02 06:19:28 +00001933 for (std::vector<LetRecord> &LetInfo : LetStack)
1934 for (LetRecord &LR : LetInfo)
1935 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
Sean Silvacb1a75e2013-01-09 04:49:14 +00001936 return true;
1937 return false;
1938}
1939
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001940/// ParseObjectBody - Parse the body of a def or class. This consists of an
1941/// optional ClassList followed by a Body. CurRec is the current def or class
1942/// that is being parsed.
1943///
1944/// ObjectBody ::= BaseClassList Body
1945/// BaseClassList ::= /*empty*/
1946/// BaseClassList ::= ':' BaseClassListNE
1947/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1948///
1949bool TGParser::ParseObjectBody(Record *CurRec) {
1950 // If there is a baseclass list, read it.
1951 if (Lex.getCode() == tgtok::colon) {
1952 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001953
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001954 // Read all of the subclasses.
1955 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1956 while (1) {
1957 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00001958 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001959
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001960 // Add it.
1961 if (AddSubClass(CurRec, SubClass))
1962 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001963
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001964 if (Lex.getCode() != tgtok::comma) break;
1965 Lex.Lex(); // eat ','.
1966 SubClass = ParseSubClassReference(CurRec, false);
1967 }
1968 }
1969
Sean Silvacb1a75e2013-01-09 04:49:14 +00001970 if (ApplyLetStack(CurRec))
1971 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001972
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001973 return ParseBody(CurRec);
1974}
1975
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001976/// ParseDef - Parse and return a top level or multiclass def, return the record
1977/// corresponding to it. This returns null on error.
1978///
1979/// DefInst ::= DEF ObjectName ObjectBody
1980///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00001981bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001982 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001983 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00001984 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001985
1986 // Parse ObjectName and make a record for it.
Craig Topper84138712014-11-29 05:31:10 +00001987 std::unique_ptr<Record> CurRecOwner;
Jordan Roseabdd99b2013-01-10 18:50:05 +00001988 Init *Name = ParseObjectName(CurMultiClass);
1989 if (Name)
Craig Topper84138712014-11-29 05:31:10 +00001990 CurRecOwner = make_unique<Record>(Name, DefLoc, Records);
Jordan Roseabdd99b2013-01-10 18:50:05 +00001991 else
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00001992 CurRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), DefLoc,
1993 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001994 Record *CurRec = CurRecOwner.get(); // Keep a copy since we may release.
Bob Wilson7248f862009-11-22 04:24:42 +00001995
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00001996 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001997 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00001998
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001999 // Ensure redefinition doesn't happen.
Craig Topper84138712014-11-29 05:31:10 +00002000 if (Records.getDef(CurRec->getNameInitAsString()))
2001 return Error(DefLoc, "def '" + CurRec->getNameInitAsString()+
2002 "' already defined");
Craig Toppercdab2322014-11-29 05:52:51 +00002003 Records.addDef(std::move(CurRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00002004
2005 if (ParseObjectBody(CurRec))
2006 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002007 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002008 // Parse the body before adding this prototype to the DefPrototypes vector.
2009 // That way implicit definitions will be added to the DefPrototypes vector
2010 // before this object, instantiated prior to defs derived from this object,
2011 // and this available for indirect name resolution when defs derived from
2012 // this object are instantiated.
Craig Topper84138712014-11-29 05:31:10 +00002013 if (ParseObjectBody(CurRec))
Hal Finkela8c1f462014-01-02 20:47:09 +00002014 return true;
2015
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002016 // Otherwise, a def inside a multiclass, add it to the multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002017 for (const auto &Proto : CurMultiClass->DefPrototypes)
2018 if (Proto->getNameInit() == CurRec->getNameInit())
Craig Topper84138712014-11-29 05:31:10 +00002019 return Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
2020 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002021 CurMultiClass->DefPrototypes.push_back(std::move(CurRecOwner));
Anton Yartsev671dff12014-08-08 00:29:54 +00002022 } else if (ParseObjectBody(CurRec)) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002023 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002024 }
Bob Wilson7248f862009-11-22 04:24:42 +00002025
Craig Topper011817a2014-04-09 04:50:04 +00002026 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002027 // See Record::setName(). This resolve step will see any new name
2028 // for the def that might have been created when resolving
2029 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002030 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002031
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002032 // If ObjectBody has template arguments, it's an error.
2033 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002034
2035 if (CurMultiClass) {
2036 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002037 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
2038 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002039 assert(RV && "Template arg doesn't exist?");
2040 CurRec->addValue(*RV);
2041 }
2042 }
2043
Craig Toppera9642b42015-05-04 01:35:39 +00002044 if (ProcessForeachDefs(CurRec, DefLoc))
Craig Topper84138712014-11-29 05:31:10 +00002045 return Error(DefLoc, "Could not process loops for def" +
2046 CurRec->getNameInitAsString());
David Greenefb927af2012-02-22 16:09:41 +00002047
2048 return false;
2049}
2050
2051/// ParseForeach - Parse a for statement. Return the record corresponding
2052/// to it. This returns true on error.
2053///
2054/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2055/// Foreach ::= FOREACH Declaration IN Object
2056///
2057bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2058 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2059 Lex.Lex(); // Eat the 'for' token.
2060
2061 // Make a temporary object to record items associated with the for
2062 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002063 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002064 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002065 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002066 return TokError("expected declaration in for");
2067
2068 if (Lex.getCode() != tgtok::In)
2069 return TokError("Unknown tok");
2070 Lex.Lex(); // Eat the in
2071
2072 // Create a loop object and remember it.
2073 Loops.push_back(ForeachLoop(IterName, ListValue));
2074
2075 if (Lex.getCode() != tgtok::l_brace) {
2076 // FOREACH Declaration IN Object
2077 if (ParseObject(CurMultiClass))
2078 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002079 } else {
David Greenefb927af2012-02-22 16:09:41 +00002080 SMLoc BraceLoc = Lex.getLoc();
2081 // Otherwise, this is a group foreach.
2082 Lex.Lex(); // eat the '{'.
2083
2084 // Parse the object list.
2085 if (ParseObjectList(CurMultiClass))
2086 return true;
2087
2088 if (Lex.getCode() != tgtok::r_brace) {
2089 TokError("expected '}' at end of foreach command");
2090 return Error(BraceLoc, "to match this '{'");
2091 }
2092 Lex.Lex(); // Eat the }
2093 }
2094
2095 // We've processed everything in this loop.
2096 Loops.pop_back();
2097
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002098 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002099}
2100
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002101/// ParseClass - Parse a tblgen class definition.
2102///
2103/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2104///
2105bool TGParser::ParseClass() {
2106 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2107 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002108
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002109 if (Lex.getCode() != tgtok::Id)
2110 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002111
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002112 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2113 if (CurRec) {
2114 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002115 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002116 !CurRec->getSuperClasses().empty() ||
2117 !CurRec->getTemplateArgs().empty())
Craig Topper85c07002015-04-30 05:54:22 +00002118 return TokError("Class '" + CurRec->getNameInitAsString() +
2119 "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002120 } else {
2121 // If this is the first reference to this class, create and add it.
Hans Wennborgffbbd532014-11-30 00:31:49 +00002122 auto NewRec =
2123 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
Craig Toppercdab2322014-11-29 05:52:51 +00002124 CurRec = NewRec.get();
2125 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002126 }
2127 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002128
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002129 // If there are template args, parse them.
2130 if (Lex.getCode() == tgtok::less)
2131 if (ParseTemplateArgList(CurRec))
2132 return true;
2133
2134 // Finally, parse the object body.
2135 return ParseObjectBody(CurRec);
2136}
2137
2138/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2139/// of LetRecords.
2140///
2141/// LetList ::= LetItem (',' LetItem)*
2142/// LetItem ::= ID OptionalRangeList '=' Value
2143///
2144std::vector<LetRecord> TGParser::ParseLetList() {
2145 std::vector<LetRecord> Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002146
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002147 while (1) {
2148 if (Lex.getCode() != tgtok::Id) {
2149 TokError("expected identifier in let definition");
2150 return std::vector<LetRecord>();
2151 }
2152 std::string Name = Lex.getCurStrVal();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002153 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002154 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002155
2156 // Check for an optional RangeList.
2157 std::vector<unsigned> Bits;
Bob Wilson7248f862009-11-22 04:24:42 +00002158 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002159 return std::vector<LetRecord>();
2160 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002161
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002162 if (Lex.getCode() != tgtok::equal) {
2163 TokError("expected '=' in let expression");
2164 return std::vector<LetRecord>();
2165 }
2166 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002167
Craig Topper011817a2014-04-09 04:50:04 +00002168 Init *Val = ParseValue(nullptr);
2169 if (!Val) return std::vector<LetRecord>();
Bob Wilson7248f862009-11-22 04:24:42 +00002170
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002171 // Now that we have everything, add the record.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00002172 Result.emplace_back(std::move(Name), std::move(Bits), Val, NameLoc);
Bob Wilson7248f862009-11-22 04:24:42 +00002173
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002174 if (Lex.getCode() != tgtok::comma)
2175 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002176 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002177 }
2178}
2179
2180/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002181/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002182///
2183/// Object ::= LET LetList IN '{' ObjectList '}'
2184/// Object ::= LET LetList IN Object
2185///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002186bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002187 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2188 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002189
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002190 // Add this entry to the let stack.
2191 std::vector<LetRecord> LetInfo = ParseLetList();
2192 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002193 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002194
2195 if (Lex.getCode() != tgtok::In)
2196 return TokError("expected 'in' at end of top-level 'let'");
2197 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002198
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002199 // If this is a scalar let, just handle it now
2200 if (Lex.getCode() != tgtok::l_brace) {
2201 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002202 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002203 return true;
2204 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002205 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002206 // Otherwise, this is a group let.
2207 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002208
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002209 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002210 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002211 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002212
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002213 if (Lex.getCode() != tgtok::r_brace) {
2214 TokError("expected '}' at end of top level let command");
2215 return Error(BraceLoc, "to match this '{'");
2216 }
2217 Lex.Lex();
2218 }
Bob Wilson7248f862009-11-22 04:24:42 +00002219
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002220 // Outside this let scope, this let block is not active.
2221 LetStack.pop_back();
2222 return false;
2223}
2224
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002225/// ParseMultiClass - Parse a multiclass definition.
2226///
Bob Wilson3d948162009-04-28 19:41:44 +00002227/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002228/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2229/// MultiClassObject ::= DefInst
2230/// MultiClassObject ::= MultiClassInst
2231/// MultiClassObject ::= DefMInst
2232/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2233/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002234///
2235bool TGParser::ParseMultiClass() {
2236 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2237 Lex.Lex(); // Eat the multiclass token.
2238
2239 if (Lex.getCode() != tgtok::Id)
2240 return TokError("expected identifier after multiclass for name");
2241 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002242
Craig Topper7adf2bf2014-12-11 05:25:30 +00002243 auto Result =
2244 MultiClasses.insert(std::make_pair(Name,
2245 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2246
2247 if (!Result.second)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002248 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002249
Craig Topper7adf2bf2014-12-11 05:25:30 +00002250 CurMultiClass = Result.first->second.get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002251 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002252
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002253 // If there are template args, parse them.
2254 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002255 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002256 return true;
2257
David Greene7049e792009-04-24 16:55:41 +00002258 bool inherits = false;
2259
David Greene753ed8f2009-04-22 16:42:54 +00002260 // If there are submulticlasses, parse them.
2261 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002262 inherits = true;
2263
David Greene753ed8f2009-04-22 16:42:54 +00002264 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002265
David Greene753ed8f2009-04-22 16:42:54 +00002266 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002267 SubMultiClassReference SubMultiClass =
2268 ParseSubMultiClassReference(CurMultiClass);
David Greene753ed8f2009-04-22 16:42:54 +00002269 while (1) {
2270 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002271 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002272
David Greene753ed8f2009-04-22 16:42:54 +00002273 // Add it.
2274 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2275 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002276
David Greene753ed8f2009-04-22 16:42:54 +00002277 if (Lex.getCode() != tgtok::comma) break;
2278 Lex.Lex(); // eat ','.
2279 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2280 }
2281 }
2282
David Greene7049e792009-04-24 16:55:41 +00002283 if (Lex.getCode() != tgtok::l_brace) {
2284 if (!inherits)
2285 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002286 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002287 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002288 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002289 } else {
David Greene7049e792009-04-24 16:55:41 +00002290 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2291 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002292
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002293 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002294 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002295 default:
2296 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
2297 case tgtok::Let:
2298 case tgtok::Def:
2299 case tgtok::Defm:
2300 case tgtok::Foreach:
2301 if (ParseObject(CurMultiClass))
2302 return true;
2303 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002304 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002305 }
David Greene7049e792009-04-24 16:55:41 +00002306 Lex.Lex(); // eat the '}'.
2307 }
Bob Wilson7248f862009-11-22 04:24:42 +00002308
Craig Topper011817a2014-04-09 04:50:04 +00002309 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002310 return false;
2311}
2312
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002313Record *TGParser::InstantiateMulticlassDef(MultiClass &MC, Record *DefProto,
2314 Init *&DefmPrefix,
2315 SMRange DefmPrefixRange,
2316 ArrayRef<Init *> TArgs,
2317 std::vector<Init *> &TemplateVals) {
David Greene5d5d88c2011-10-19 13:04:31 +00002318 // We need to preserve DefProto so it can be reused for later
2319 // instantiations, so create a new Record to inherit from it.
2320
David Greenedb445972011-10-05 22:42:07 +00002321 // Add in the defm name. If the defm prefix is empty, give each
2322 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2323 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2324 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002325
Jordan Roseabdd99b2013-01-10 18:50:05 +00002326 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002327 if (!DefmPrefix) {
David Greene5d5d88c2011-10-19 13:04:31 +00002328 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002329 IsAnonymous = true;
2330 }
David Greene5d5d88c2011-10-19 13:04:31 +00002331
2332 Init *DefName = DefProto->getNameInit();
Sean Silvafb509ed2012-10-10 20:24:43 +00002333 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002334
Craig Topper011817a2014-04-09 04:50:04 +00002335 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002336 // We have a fully expanded string so there are no operators to
2337 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002338 DefName =
2339 BinOpInit::get(BinOpInit::STRCONCAT,
2340 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2341 StringRecTy::get())->Fold(DefProto, &MC),
2342 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2343 }
David Greene5d5d88c2011-10-19 13:04:31 +00002344
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002345 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002346 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002347 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Craig Topper84138712014-11-29 05:31:10 +00002348 auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002349
2350 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002351 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002352 Ref.Rec = DefProto;
Craig Topper84138712014-11-29 05:31:10 +00002353 AddSubClass(CurRec.get(), Ref);
David Greenedb445972011-10-05 22:42:07 +00002354
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002355 // Set the value for NAME. We don't resolve references to it 'til later,
2356 // though, so that uses in nested multiclass names don't get
2357 // confused.
Craig Toppercfd81732016-01-04 03:15:08 +00002358 if (SetValue(CurRec.get(), Ref.RefRange.Start, "NAME", None, DefmPrefix,
Craig Topper1e23ed92016-01-04 03:05:14 +00002359 /*AllowSelfAssignment*/true)) {
Craig Topper85c07002015-04-30 05:54:22 +00002360 Error(DefmPrefixRange.Start, "Could not resolve " +
2361 CurRec->getNameInitAsString() + ":NAME to '" +
2362 DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002363 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002364 }
David Greene8bf0d722011-10-19 13:04:35 +00002365
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002366 // If the DefNameString didn't resolve, we probably have a reference to
2367 // NAME and need to replace it. We need to do at least this much greedily,
2368 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002369 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002370 RecordVal *DefNameRV = CurRec->getValue("NAME");
2371 CurRec->resolveReferencesTo(DefNameRV);
2372 }
2373
2374 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002375 // Now that we're at the top level, resolve all NAME references
2376 // in the resultant defs that weren't in the def names themselves.
2377 RecordVal *DefNameRV = CurRec->getValue("NAME");
2378 CurRec->resolveReferencesTo(DefNameRV);
2379
Hal Finkeld2497362015-05-21 04:32:56 +00002380 // Check if the name is a complex pattern.
2381 // If so, resolve it.
2382 DefName = CurRec->getNameInit();
2383 DefNameString = dyn_cast<StringInit>(DefName);
2384
2385 // OK the pattern is more complex than simply using NAME.
2386 // Let's use the heavy weaponery.
2387 if (!DefNameString) {
2388 ResolveMulticlassDefArgs(MC, CurRec.get(), DefmPrefixRange.Start,
2389 Lex.getLoc(), TArgs, TemplateVals,
2390 false/*Delete args*/);
2391 DefName = CurRec->getNameInit();
2392 DefNameString = dyn_cast<StringInit>(DefName);
2393
2394 if (!DefNameString)
2395 DefName = DefName->convertInitializerTo(StringRecTy::get());
2396
2397 // We ran out of options here...
2398 DefNameString = dyn_cast<StringInit>(DefName);
2399 if (!DefNameString) {
2400 PrintFatalError(CurRec->getLoc()[CurRec->getLoc().size() - 1],
2401 DefName->getAsUnquotedString() + " is not a string.");
2402 return nullptr;
2403 }
2404
2405 CurRec->setName(DefName);
2406 }
2407
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002408 // Now that NAME references are resolved and we're at the top level of
2409 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002410 // currently in a multiclass, it means this defm appears inside a
2411 // multiclass and its name won't be fully resolvable until we see
Hal Finkeld2497362015-05-21 04:32:56 +00002412 // the top-level defm. Therefore, we don't add this to the
2413 // RecordKeeper at this point. If we did we could get duplicate
David Greene8bf0d722011-10-19 13:04:35 +00002414 // defs as more than one probably refers to NAME or some other
2415 // common internal placeholder.
2416
2417 // Ensure redefinition doesn't happen.
2418 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002419 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002420 "' already defined, instantiating defm with subdef '" +
2421 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002422 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002423 }
2424
Craig Topper84138712014-11-29 05:31:10 +00002425 Record *CurRecSave = CurRec.get(); // Keep a copy before we release.
Craig Toppercdab2322014-11-29 05:52:51 +00002426 Records.addDef(std::move(CurRec));
Craig Topper84138712014-11-29 05:31:10 +00002427 return CurRecSave;
David Greene8bf0d722011-10-19 13:04:35 +00002428 }
2429
Craig Topper84138712014-11-29 05:31:10 +00002430 // FIXME This is bad but the ownership transfer to caller is pretty messy.
2431 // The unique_ptr in this function at least protects the exits above.
2432 return CurRec.release();
David Greenedb445972011-10-05 22:42:07 +00002433}
2434
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002435bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC, Record *CurRec,
2436 SMLoc DefmPrefixLoc, SMLoc SubClassLoc,
2437 ArrayRef<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.
Craig Toppercfd81732016-01-04 03:15:08 +00002446 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], None, TemplateVals[i]))
David Greenedb445972011-10-05 22:42:07 +00002447 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002448
David Greenedb445972011-10-05 22:42:07 +00002449 // Resolve it next.
2450 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2451
2452 if (DeleteArgs)
2453 // Now remove it.
2454 CurRec->removeValue(TArgs[i]);
Craig Toppera9642b42015-05-04 01:35:39 +00002455
David Greenedb445972011-10-05 22:42:07 +00002456 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Craig Topper85c07002015-04-30 05:54:22 +00002457 return Error(SubClassLoc, "value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00002458 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +00002459 ") of multiclassclass '" + MC.Rec.getNameInitAsString() +
2460 "'");
David Greenedb445972011-10-05 22:42:07 +00002461 }
2462 }
2463 return false;
2464}
2465
2466bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2467 Record *CurRec,
2468 Record *DefProto,
2469 SMLoc DefmPrefixLoc) {
2470 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002471 if (ApplyLetStack(CurRec))
2472 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002473
David Greenedb445972011-10-05 22:42:07 +00002474 // Don't create a top level definition for defm inside multiclasses,
2475 // instead, only update the prototypes and bind the template args
2476 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002477 if (!CurMultiClass)
2478 return false;
Craig Toppereb4d7c62015-04-29 04:43:36 +00002479 for (const auto &Proto : CurMultiClass->DefPrototypes)
2480 if (Proto->getNameInit() == CurRec->getNameInit())
Sean Silvacc951b22013-01-09 05:28:12 +00002481 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2482 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002483 CurMultiClass->DefPrototypes.push_back(std::unique_ptr<Record>(CurRec));
David Greenedb445972011-10-05 22:42:07 +00002484
Sean Silvacc951b22013-01-09 05:28:12 +00002485 // Copy the template arguments for the multiclass into the new def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002486 for (Init * TA : CurMultiClass->Rec.getTemplateArgs()) {
2487 const RecordVal *RV = CurMultiClass->Rec.getValue(TA);
Sean Silvacc951b22013-01-09 05:28:12 +00002488 assert(RV && "Template arg doesn't exist?");
2489 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002490 }
2491
2492 return false;
2493}
2494
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002495/// ParseDefm - Parse the instantiation of a multiclass.
2496///
2497/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2498///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002499bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002500 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002501 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002502 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002503
Craig Topperb21afc62013-01-07 05:09:33 +00002504 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002505 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002506 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002507
Jordan Rosef12e8a92013-01-10 18:50:11 +00002508 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002509 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002510 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002511
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002512 // Keep track of the new generated record definitions.
2513 std::vector<Record*> NewRecDefs;
2514
2515 // This record also inherits from a regular class (non-multiclass)?
2516 bool InheritFromClass = false;
2517
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002518 // eat the colon.
2519 Lex.Lex();
2520
Chris Lattner526c8cb2009-06-21 03:39:35 +00002521 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002522 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002523
2524 while (1) {
Craig Topper011817a2014-04-09 04:50:04 +00002525 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002526
2527 // To instantiate a multiclass, we need to first get the multiclass, then
2528 // instantiate each def contained in the multiclass with the SubClassRef
2529 // template parameters.
Craig Topper7adf2bf2014-12-11 05:25:30 +00002530 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper4ca40012014-11-30 01:20:17 +00002531 assert(MC && "Didn't lookup multiclass correctly?");
David Greeneaf8ee2c2011-07-29 22:43:06 +00002532 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002533
2534 // Verify that the correct number of template arguments were specified.
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002535 ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002536 if (TArgs.size() < TemplateVals.size())
2537 return Error(SubClassLoc,
2538 "more template args specified than multiclass expects");
2539
2540 // Loop over all the def's in the multiclass, instantiating each one.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002541 for (const std::unique_ptr<Record> &DefProto : MC->DefPrototypes) {
Hal Finkeld2497362015-05-21 04:32:56 +00002542 // The record name construction goes as follow:
2543 // - If the def name is a string, prepend the prefix.
2544 // - If the def name is a more complex pattern, use that pattern.
2545 // As a result, the record is instanciated before resolving
2546 // arguments, as it would make its name a string.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002547 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto.get(), DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002548 SMRange(DefmLoc,
Hal Finkeld2497362015-05-21 04:32:56 +00002549 DefmPrefixEndLoc),
2550 TArgs, TemplateVals);
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002551 if (!CurRec)
2552 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002553
Hal Finkeld2497362015-05-21 04:32:56 +00002554 // Now that the record is instanciated, we can resolve arguments.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002555 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002556 TArgs, TemplateVals, true/*Delete args*/))
2557 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002558
Craig Toppereb4d7c62015-04-29 04:43:36 +00002559 if (ResolveMulticlassDef(*MC, CurRec, DefProto.get(), DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002560 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002561
Adam Nemete5a07162014-09-16 17:14:13 +00002562 // Defs that can be used by other definitions should be fully resolved
2563 // before any use.
2564 if (DefProto->isResolveFirst() && !CurMultiClass) {
2565 CurRec->resolveReferences();
2566 CurRec->setResolveFirst(false);
2567 }
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002568 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002569 }
2570
David Greenedb445972011-10-05 22:42:07 +00002571
David Greenef00919a2009-04-22 22:17:51 +00002572 if (Lex.getCode() != tgtok::comma) break;
2573 Lex.Lex(); // eat ','.
2574
Craig Topper998a39a2013-08-20 04:22:09 +00002575 if (Lex.getCode() != tgtok::Id)
2576 return TokError("expected identifier");
2577
David Greenef00919a2009-04-22 22:17:51 +00002578 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002579
2580 // A defm can inherit from regular classes (non-multiclass) as
2581 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002582 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002583
2584 if (InheritFromClass)
2585 break;
2586
Craig Topper011817a2014-04-09 04:50:04 +00002587 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002588 }
2589
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002590 if (InheritFromClass) {
2591 // Process all the classes to inherit as if they were part of a
2592 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002593 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002594 while (1) {
2595 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002596 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002597
2598 // Get the expanded definition prototypes and teach them about
2599 // the record values the current class to inherit has
Craig Toppereb4d7c62015-04-29 04:43:36 +00002600 for (Record *CurRec : NewRecDefs) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002601 // Add it.
2602 if (AddSubClass(CurRec, SubClass))
2603 return true;
2604
Sean Silvacb1a75e2013-01-09 04:49:14 +00002605 if (ApplyLetStack(CurRec))
2606 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002607 }
2608
2609 if (Lex.getCode() != tgtok::comma) break;
2610 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002611 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002612 }
2613 }
2614
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002615 if (!CurMultiClass)
Craig Toppereb4d7c62015-04-29 04:43:36 +00002616 for (Record *CurRec : NewRecDefs)
David Greene50c09122011-08-10 18:27:46 +00002617 // See Record::setName(). This resolve step will see any new
2618 // name for the def that might have been created when resolving
2619 // inheritance, values and arguments above.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002620 CurRec->resolveReferences();
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002621
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002622 if (Lex.getCode() != tgtok::semi)
2623 return TokError("expected ';' at end of defm");
2624 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002625
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002626 return false;
2627}
2628
2629/// ParseObject
2630/// Object ::= ClassInst
2631/// Object ::= DefInst
2632/// Object ::= MultiClassInst
2633/// Object ::= DefMInst
2634/// Object ::= LETCommand '{' ObjectList '}'
2635/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002636bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002637 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002638 default:
2639 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002640 case tgtok::Let: return ParseTopLevelLet(MC);
2641 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002642 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002643 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002644 case tgtok::Class: return ParseClass();
2645 case tgtok::MultiClass: return ParseMultiClass();
2646 }
2647}
2648
2649/// ParseObjectList
2650/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002651bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002652 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002653 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002654 return true;
2655 }
2656 return false;
2657}
2658
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002659bool TGParser::ParseFile() {
2660 Lex.Lex(); // Prime the lexer.
2661 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002662
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002663 // If we have unread input at the end of the file, report it.
2664 if (Lex.getCode() == tgtok::Eof)
2665 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002666
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002667 return TokError("Unexpected input at top level");
2668}
2669