blob: 1506a7171ac4eb60eeaab958ff4a5c681d4868f1 [file] [log] [blame]
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001//===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Parser for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
14#include "TGParser.h"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/SmallVector.h"
Chris Lattnerf4127dd2007-11-22 20:49:04 +000017#include "llvm/ADT/StringExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/Support/CommandLine.h"
19#include "llvm/TableGen/Record.h"
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000020#include <algorithm>
21#include <sstream>
Chris Lattnerf4127dd2007-11-22 20:49:04 +000022using namespace llvm;
23
24//===----------------------------------------------------------------------===//
25// Support Code for the Semantic Actions.
26//===----------------------------------------------------------------------===//
27
28namespace llvm {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000029struct SubClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000030 SMRange RefRange;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000031 Record *Rec;
David Greeneaf8ee2c2011-07-29 22:43:06 +000032 std::vector<Init*> TemplateArgs;
Craig Topper011817a2014-04-09 04:50:04 +000033 SubClassReference() : Rec(nullptr) {}
David Greene7049e792009-04-24 16:55:41 +000034
Craig Topper011817a2014-04-09 04:50:04 +000035 bool isInvalid() const { return Rec == nullptr; }
Chris Lattnerf4127dd2007-11-22 20:49:04 +000036};
David Greene753ed8f2009-04-22 16:42:54 +000037
38struct SubMultiClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000039 SMRange RefRange;
David Greene753ed8f2009-04-22 16:42:54 +000040 MultiClass *MC;
David Greeneaf8ee2c2011-07-29 22:43:06 +000041 std::vector<Init*> TemplateArgs;
Craig Topper011817a2014-04-09 04:50:04 +000042 SubMultiClassReference() : MC(nullptr) {}
Bob Wilson3d948162009-04-28 19:41:44 +000043
Craig Topper011817a2014-04-09 04:50:04 +000044 bool isInvalid() const { return MC == nullptr; }
David Greene7049e792009-04-24 16:55:41 +000045 void dump() const;
David Greene753ed8f2009-04-22 16:42:54 +000046};
David Greene7049e792009-04-24 16:55:41 +000047
48void SubMultiClassReference::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000049 errs() << "Multiclass:\n";
Bob Wilson7248f862009-11-22 04:24:42 +000050
David Greene7049e792009-04-24 16:55:41 +000051 MC->dump();
Bob Wilson7248f862009-11-22 04:24:42 +000052
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000053 errs() << "Template args:\n";
Craig Toppera9642b42015-05-04 01:35:39 +000054 for (Init *TA : TemplateArgs)
Craig Toppereb4d7c62015-04-29 04:43:36 +000055 TA->dump();
David Greene7049e792009-04-24 16:55:41 +000056}
57
Chris Lattnerf4127dd2007-11-22 20:49:04 +000058} // end namespace llvm
59
Chris Lattner526c8cb2009-06-21 03:39:35 +000060bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Craig Topper011817a2014-04-09 04:50:04 +000061 if (!CurRec)
Chris Lattnerf4127dd2007-11-22 20:49:04 +000062 CurRec = &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +000063
Jakob Stoklund Olesen9d1c5ee2012-01-13 03:16:35 +000064 if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000065 // The value already exists in the class, treat this as a set.
66 if (ERV->setValue(RV.getValue()))
67 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
68 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson7248f862009-11-22 04:24:42 +000069 "previous definition of type '" +
Chris Lattnerf4127dd2007-11-22 20:49:04 +000070 ERV->getType()->getAsString() + "'");
71 } else {
72 CurRec->addValue(RV);
73 }
74 return false;
75}
76
77/// SetValue -
78/// Return true on error, false on success.
David Greene3ca42122011-10-19 13:02:39 +000079bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
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 Topperd26d2d92015-07-06 06:23:01 +0000188 ArrayRef<Record *> SCs = SC->getSuperClasses();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000189 ArrayRef<SMRange> SCRanges = SC->getSuperClassRanges();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000190 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
191 if (CurRec->isSubClassOf(SCs[i]))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000192 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000193 "Already subclass of '" + SCs[i]->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000194 CurRec->addSuperClass(SCs[i], SCRanges[i]);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000195 }
Bob Wilson7248f862009-11-22 04:24:42 +0000196
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000197 if (CurRec->isSubClassOf(SC))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000198 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000199 "Already subclass of '" + SC->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000200 CurRec->addSuperClass(SC, SubClass.RefRange);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000201 return false;
202}
203
David Greene753ed8f2009-04-22 16:42:54 +0000204/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilsonf71e6562009-04-30 18:26:19 +0000205/// CurMC, resolving its template args as SubMultiClass's
David Greene753ed8f2009-04-22 16:42:54 +0000206/// template arguments.
Bob Wilsonf71e6562009-04-30 18:26:19 +0000207bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson92ab8202009-04-30 17:46:20 +0000208 SubMultiClassReference &SubMultiClass) {
David Greene753ed8f2009-04-22 16:42:54 +0000209 MultiClass *SMC = SubMultiClass.MC;
Bob Wilsonf71e6562009-04-30 18:26:19 +0000210 Record *CurRec = &CurMC->Rec;
David Greene753ed8f2009-04-22 16:42:54 +0000211
David Greene753ed8f2009-04-22 16:42:54 +0000212 // Add all of the values in the subclass into the current class.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000213 for (const auto &SMCVal : SMC->Rec.getValues())
214 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000215 return true;
216
Craig Toppera4ea4b02014-11-30 00:24:32 +0000217 unsigned newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000218
David Greene753ed8f2009-04-22 16:42:54 +0000219 // Add all of the defs in the subclass into the current multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000220 for (const std::unique_ptr<Record> &R : SMC->DefPrototypes) {
David Greene753ed8f2009-04-22 16:42:54 +0000221 // Clone the def and add it to the current multiclass
Craig Toppereb4d7c62015-04-29 04:43:36 +0000222 auto NewDef = make_unique<Record>(*R);
David Greene753ed8f2009-04-22 16:42:54 +0000223
224 // Add all of the values in the superclass into the current def.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000225 for (const auto &MCVal : CurRec->getValues())
226 if (AddValue(NewDef.get(), SubMultiClass.RefRange.Start, MCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000227 return true;
228
Craig Topperc3504c42014-12-11 05:25:33 +0000229 CurMC->DefPrototypes.push_back(std::move(NewDef));
David Greene753ed8f2009-04-22 16:42:54 +0000230 }
Bob Wilson3d948162009-04-28 19:41:44 +0000231
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000232 ArrayRef<Init *> SMCTArgs = SMC->Rec.getTemplateArgs();
David Greene753ed8f2009-04-22 16:42:54 +0000233
David Greene7049e792009-04-24 16:55:41 +0000234 // Ensure that an appropriate number of template arguments are
235 // specified.
David Greene753ed8f2009-04-22 16:42:54 +0000236 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000237 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000238 "More template args specified than expected");
Bob Wilson3d948162009-04-28 19:41:44 +0000239
David Greene753ed8f2009-04-22 16:42:54 +0000240 // Loop over all of the template arguments, setting them to the specified
241 // value or leaving them as the default if necessary.
242 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
243 if (i < SubMultiClass.TemplateArgs.size()) {
David Greene7049e792009-04-24 16:55:41 +0000244 // If a value is specified for this template arg, set it in the
245 // superclass now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000246 if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000247 None, SubMultiClass.TemplateArgs[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000248 return true;
249
250 // Resolve it next.
251 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson3d948162009-04-28 19:41:44 +0000252
David Greene753ed8f2009-04-22 16:42:54 +0000253 // Now remove it.
254 CurRec->removeValue(SMCTArgs[i]);
255
David Greene7049e792009-04-24 16:55:41 +0000256 // If a value is specified for this template arg, set it in the
257 // new defs now.
Craig Topperc3504c42014-12-11 05:25:33 +0000258 for (const auto &Def :
259 makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
260 if (SetValue(Def.get(), SubMultiClass.RefRange.Start, SMCTArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000261 None, SubMultiClass.TemplateArgs[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000262 return true;
263
264 // Resolve it next.
265 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
266
267 // Now remove it
268 Def->removeValue(SMCTArgs[i]);
269 }
270 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000271 return Error(SubMultiClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000272 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000273 Twine(i) + " (" + SMCTArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000274 ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000275 }
276 }
277
278 return false;
279}
280
David Greenefb927af2012-02-22 16:09:41 +0000281/// ProcessForeachDefs - Given a record, apply all of the variable
282/// values in all surrounding foreach loops, creating new records for
283/// each combination of values.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000284bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
285 if (Loops.empty())
286 return false;
287
David Greenefb927af2012-02-22 16:09:41 +0000288 // We want to instantiate a new copy of CurRec for each combination
289 // of nested loop iterator values. We don't want top instantiate
290 // any copies until we have values for each loop iterator.
291 IterSet IterVals;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000292 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000293}
294
295/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
296/// apply each of the variable values in this loop and then process
297/// subloops.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000298bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
299 // Recursively build a tuple of iterator values.
300 if (IterVals.size() != Loops.size()) {
301 assert(IterVals.size() < Loops.size());
302 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silvafb509ed2012-10-10 20:24:43 +0000303 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Craig Topper011817a2014-04-09 04:50:04 +0000304 if (!List) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000305 Error(Loc, "Loop list is not a list");
306 return true;
307 }
David Greenefb927af2012-02-22 16:09:41 +0000308
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000309 // Process each value.
Craig Topper664f6a02015-06-02 04:15:57 +0000310 for (unsigned i = 0; i < List->size(); ++i) {
Craig Topper011817a2014-04-09 04:50:04 +0000311 Init *ItemVal = List->resolveListElementReference(*CurRec, nullptr, i);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000312 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
313 if (ProcessForeachDefs(CurRec, Loc, IterVals))
314 return true;
315 IterVals.pop_back();
316 }
317 return false;
318 }
319
320 // This is the bottom of the recursion. We have all of the iterator values
321 // for this point in the iteration space. Instantiate a new record to
322 // reflect this combination of values.
Craig Topper84138712014-11-29 05:31:10 +0000323 auto IterRec = make_unique<Record>(*CurRec);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000324
325 // Set the iterator values now.
Craig Topper8eb764c2015-06-02 06:19:28 +0000326 for (IterRecord &IR : IterVals) {
327 VarInit *IterVar = IR.IterVar;
328 TypedInit *IVal = dyn_cast<TypedInit>(IR.IterValue);
Craig Topper84138712014-11-29 05:31:10 +0000329 if (!IVal)
330 return Error(Loc, "foreach iterator value is untyped");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000331
332 IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false));
333
Craig Toppercfd81732016-01-04 03:15:08 +0000334 if (SetValue(IterRec.get(), Loc, IterVar->getName(), None, IVal))
Craig Topper84138712014-11-29 05:31:10 +0000335 return Error(Loc, "when instantiating this def");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000336
337 // Resolve it next.
338 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName()));
339
340 // Remove it.
341 IterRec->removeValue(IterVar->getName());
342 }
343
344 if (Records.getDef(IterRec->getNameInitAsString())) {
Artyom Skrobov8b985322014-06-10 12:41:14 +0000345 // If this record is anonymous, it's no problem, just generate a new name
Craig Topper84138712014-11-29 05:31:10 +0000346 if (!IterRec->isAnonymous())
347 return Error(Loc, "def already exists: " +IterRec->getNameInitAsString());
348
349 IterRec->setName(GetNewAnonymousName());
David Greenefb927af2012-02-22 16:09:41 +0000350 }
351
Craig Topper84138712014-11-29 05:31:10 +0000352 Record *IterRecSave = IterRec.get(); // Keep a copy before release.
Craig Toppercdab2322014-11-29 05:52:51 +0000353 Records.addDef(std::move(IterRec));
Craig Topper84138712014-11-29 05:31:10 +0000354 IterRecSave->resolveReferences();
David Greenefb927af2012-02-22 16:09:41 +0000355 return false;
356}
357
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000358//===----------------------------------------------------------------------===//
359// Parser Code
360//===----------------------------------------------------------------------===//
361
362/// isObjectStart - Return true if this is a valid first token for an Object.
363static bool isObjectStart(tgtok::TokKind K) {
364 return K == tgtok::Class || K == tgtok::Def ||
David Greenefb927af2012-02-22 16:09:41 +0000365 K == tgtok::Defm || K == tgtok::Let ||
366 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000367}
368
Alp Tokerce91fe52013-12-21 18:51:00 +0000369/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
370/// an identifier.
371std::string TGParser::GetNewAnonymousName() {
Aaron Ballman97a59fb2015-02-16 19:33:36 +0000372 return "anonymous_" + utostr(AnonCounter++);
Chris Lattner7538ed82010-10-05 22:51:56 +0000373}
374
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000375/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000376/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000377/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000378/// ObjectName ::= /*empty*/
379///
David Greene2affd672011-10-19 13:04:29 +0000380Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
381 switch (Lex.getCode()) {
382 case tgtok::colon:
383 case tgtok::semi:
384 case tgtok::l_brace:
385 // These are all of the tokens that can begin an object body.
386 // Some of these can also begin values but we disallow those cases
387 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000388 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000389 default:
390 break;
391 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000392
Craig Topper011817a2014-04-09 04:50:04 +0000393 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000394 if (CurMultiClass)
395 CurRec = &CurMultiClass->Rec;
396
Craig Topper011817a2014-04-09 04:50:04 +0000397 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000398 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000399 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000400 if (!CurRecName) {
401 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000402 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000403 }
404 Type = CurRecName->getType();
405 }
406
407 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000408}
409
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000410/// ParseClassID - Parse and resolve a reference to a class name. This returns
411/// null on error.
412///
413/// ClassID ::= ID
414///
415Record *TGParser::ParseClassID() {
416 if (Lex.getCode() != tgtok::Id) {
417 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000418 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000419 }
Bob Wilson7248f862009-11-22 04:24:42 +0000420
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000421 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000422 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000423 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000424
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000425 Lex.Lex();
426 return Result;
427}
428
Bob Wilson3d948162009-04-28 19:41:44 +0000429/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
430/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000431///
432/// MultiClassID ::= ID
433///
434MultiClass *TGParser::ParseMultiClassID() {
435 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000436 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000437 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000438 }
Bob Wilson3d948162009-04-28 19:41:44 +0000439
Craig Topper7adf2bf2014-12-11 05:25:30 +0000440 MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
Craig Topper4ca40012014-11-30 01:20:17 +0000441 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000442 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000443
David Greene753ed8f2009-04-22 16:42:54 +0000444 Lex.Lex();
Craig Topper4ca40012014-11-30 01:20:17 +0000445 return Result;
David Greene753ed8f2009-04-22 16:42:54 +0000446}
447
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000448/// ParseSubClassReference - Parse a reference to a subclass or to a templated
449/// subclass. This returns a SubClassRefTy with a null Record* on error.
450///
451/// SubClassRef ::= ClassID
452/// SubClassRef ::= ClassID '<' ValueList '>'
453///
454SubClassReference TGParser::
455ParseSubClassReference(Record *CurRec, bool isDefm) {
456 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000457 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000458
Sean Silva0657b402013-01-09 02:17:14 +0000459 if (isDefm) {
460 if (MultiClass *MC = ParseMultiClassID())
461 Result.Rec = &MC->Rec;
462 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000463 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000464 }
Craig Topper011817a2014-04-09 04:50:04 +0000465 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000466
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000467 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000468 if (Lex.getCode() != tgtok::less) {
469 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000470 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000471 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000472 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000473
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000474 if (Lex.getCode() == tgtok::greater) {
475 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000476 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000477 return Result;
478 }
Bob Wilson7248f862009-11-22 04:24:42 +0000479
David Greene8618f952009-06-08 20:23:18 +0000480 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000481 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000482 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000483 return Result;
484 }
Bob Wilson7248f862009-11-22 04:24:42 +0000485
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000486 if (Lex.getCode() != tgtok::greater) {
487 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000488 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000489 return Result;
490 }
491 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000492 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000493
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000494 return Result;
495}
496
Bob Wilson3d948162009-04-28 19:41:44 +0000497/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
498/// templated submulticlass. This returns a SubMultiClassRefTy with a null
499/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000500///
501/// SubMultiClassRef ::= MultiClassID
502/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
503///
504SubMultiClassReference TGParser::
505ParseSubMultiClassReference(MultiClass *CurMC) {
506 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000507 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000508
David Greene753ed8f2009-04-22 16:42:54 +0000509 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000510 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000511
David Greene753ed8f2009-04-22 16:42:54 +0000512 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000513 if (Lex.getCode() != tgtok::less) {
514 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000515 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000516 }
David Greene753ed8f2009-04-22 16:42:54 +0000517 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000518
David Greene753ed8f2009-04-22 16:42:54 +0000519 if (Lex.getCode() == tgtok::greater) {
520 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000521 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000522 return Result;
523 }
Bob Wilson3d948162009-04-28 19:41:44 +0000524
David Greene8618f952009-06-08 20:23:18 +0000525 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000526 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000527 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000528 return Result;
529 }
Bob Wilson3d948162009-04-28 19:41:44 +0000530
David Greene753ed8f2009-04-22 16:42:54 +0000531 if (Lex.getCode() != tgtok::greater) {
532 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000533 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000534 return Result;
535 }
536 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000537 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000538
539 return Result;
540}
541
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000542/// ParseRangePiece - Parse a bit/value range.
543/// RangePiece ::= INTVAL
544/// RangePiece ::= INTVAL '-' INTVAL
545/// RangePiece ::= INTVAL INTVAL
546bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000547 if (Lex.getCode() != tgtok::IntVal) {
548 TokError("expected integer or bitrange");
549 return true;
550 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000551 int64_t Start = Lex.getCurIntVal();
552 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000553
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000554 if (Start < 0)
555 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000556
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000557 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000558 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000559 Ranges.push_back(Start);
560 return false;
561 case tgtok::minus:
562 if (Lex.Lex() != tgtok::IntVal) {
563 TokError("expected integer value as end of range");
564 return true;
565 }
566 End = Lex.getCurIntVal();
567 break;
568 case tgtok::IntVal:
569 End = -Lex.getCurIntVal();
570 break;
571 }
Bob Wilson7248f862009-11-22 04:24:42 +0000572 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000573 return TokError("invalid range, cannot be negative");
574 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000575
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000576 // Add to the range.
Craig Toppera9642b42015-05-04 01:35:39 +0000577 if (Start < End)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000578 for (; Start <= End; ++Start)
579 Ranges.push_back(Start);
Craig Toppera9642b42015-05-04 01:35:39 +0000580 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000581 for (; Start >= End; --Start)
582 Ranges.push_back(Start);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000583 return false;
584}
585
586/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
587///
588/// RangeList ::= RangePiece (',' RangePiece)*
589///
590std::vector<unsigned> TGParser::ParseRangeList() {
591 std::vector<unsigned> Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000592
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000593 // Parse the first piece.
594 if (ParseRangePiece(Result))
595 return std::vector<unsigned>();
596 while (Lex.getCode() == tgtok::comma) {
597 Lex.Lex(); // Eat the comma.
598
599 // Parse the next range piece.
600 if (ParseRangePiece(Result))
601 return std::vector<unsigned>();
602 }
603 return Result;
604}
605
606/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
607/// OptionalRangeList ::= '<' RangeList '>'
608/// OptionalRangeList ::= /*empty*/
609bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
610 if (Lex.getCode() != tgtok::less)
611 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000612
Chris Lattner526c8cb2009-06-21 03:39:35 +0000613 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000614 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000615
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000616 // Parse the range list.
617 Ranges = ParseRangeList();
618 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000619
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000620 if (Lex.getCode() != tgtok::greater) {
621 TokError("expected '>' at end of range list");
622 return Error(StartLoc, "to match this '<'");
623 }
624 Lex.Lex(); // eat the '>'.
625 return false;
626}
627
628/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
629/// OptionalBitList ::= '{' RangeList '}'
630/// OptionalBitList ::= /*empty*/
631bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
632 if (Lex.getCode() != tgtok::l_brace)
633 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000634
Chris Lattner526c8cb2009-06-21 03:39:35 +0000635 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000636 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000637
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000638 // Parse the range list.
639 Ranges = ParseRangeList();
640 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000641
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000642 if (Lex.getCode() != tgtok::r_brace) {
643 TokError("expected '}' at end of bit list");
644 return Error(StartLoc, "to match this '{'");
645 }
646 Lex.Lex(); // eat the '}'.
647 return false;
648}
649
650
651/// ParseType - Parse and return a tblgen type. This returns null on error.
652///
653/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000654/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000655/// Type ::= BIT // bit type
656/// Type ::= BITS '<' INTVAL '>' // bits<x> type
657/// Type ::= INT // int type
658/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000659/// Type ::= DAG // dag type
660/// Type ::= ClassID // Record Type
661///
662RecTy *TGParser::ParseType() {
663 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000664 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000665 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000666 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000667 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
668 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000669 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000670 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000671 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000672 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000673 case tgtok::Bits: {
674 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
675 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000676 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000677 }
678 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
679 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000680 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000681 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000682 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000683 if (Lex.Lex() != tgtok::greater) { // Eat count.
684 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000685 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000686 }
687 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000688 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000689 }
690 case tgtok::List: {
691 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
692 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000693 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000694 }
695 Lex.Lex(); // Eat '<'
696 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000697 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000698
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000699 if (Lex.getCode() != tgtok::greater) {
700 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000701 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000702 }
703 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000704 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000705 }
Bob Wilson7248f862009-11-22 04:24:42 +0000706 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000707}
708
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000709/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
710/// has already been read.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000711Init *TGParser::ParseIDValue(Record *CurRec,
David Greened4263a62011-10-19 13:04:20 +0000712 const std::string &Name, SMLoc NameLoc,
713 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000714 if (CurRec) {
715 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000716 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000717
David Greenedb10e692011-10-19 13:02:42 +0000718 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
719
David Greene47a665e2011-10-05 22:42:54 +0000720 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +0000721 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
722 "::");
David Greene47a665e2011-10-05 22:42:54 +0000723
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000724 if (CurRec->isTemplateArg(TemplateArgName)) {
725 const RecordVal *RV = CurRec->getValue(TemplateArgName);
726 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000727 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000728 }
729 }
Bob Wilson7248f862009-11-22 04:24:42 +0000730
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000731 if (CurMultiClass) {
David Greenedb10e692011-10-19 13:02:42 +0000732 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
733 "::");
734
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000735 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
736 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
737 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000738 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000739 }
740 }
Bob Wilson7248f862009-11-22 04:24:42 +0000741
David Greenefb927af2012-02-22 16:09:41 +0000742 // If this is in a foreach loop, make sure it's not a loop iterator
Craig Toppereb4d7c62015-04-29 04:43:36 +0000743 for (const auto &L : Loops) {
744 VarInit *IterVar = dyn_cast<VarInit>(L.IterVar);
David Greenefb927af2012-02-22 16:09:41 +0000745 if (IterVar && IterVar->getName() == Name)
746 return IterVar;
747 }
748
David Greene232bd602011-10-19 13:04:21 +0000749 if (Mode == ParseNameMode)
750 return StringInit::get(Name);
751
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000752 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000753 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000754
David Greene232bd602011-10-19 13:04:21 +0000755 if (Mode == ParseValueMode) {
756 Error(NameLoc, "Variable not defined: '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000757 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000758 }
Craig Toppera9642b42015-05-04 01:35:39 +0000759
David Greene232bd602011-10-19 13:04:21 +0000760 return StringInit::get(Name);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000761}
762
David Greene5d0c0512009-05-14 20:54:48 +0000763/// ParseOperation - Parse an operator. This returns null on error.
764///
765/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
766///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000767Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000768 switch (Lex.getCode()) {
769 default:
770 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000771 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000772 case tgtok::XHead:
773 case tgtok::XTail:
774 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000775 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
776 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000777 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000778
David Greenee8f3b272009-05-14 21:22:49 +0000779 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000780 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000781 case tgtok::XCast:
782 Lex.Lex(); // eat the operation
783 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000784
David Greenee8f3b272009-05-14 21:22:49 +0000785 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000786
Craig Topper011817a2014-04-09 04:50:04 +0000787 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000788 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000789 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000790 }
David Greene5d0c0512009-05-14 20:54:48 +0000791
David Greenee8f3b272009-05-14 21:22:49 +0000792 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000793 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000794 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000795 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000796 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000797 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000798 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000799 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000800 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000801 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000802 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000803 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000804 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000805 break;
David Greenee8f3b272009-05-14 21:22:49 +0000806 }
807 if (Lex.getCode() != tgtok::l_paren) {
808 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000809 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000810 }
811 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000812
David Greeneaf8ee2c2011-07-29 22:43:06 +0000813 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000814 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000815
Craig Topper85c07002015-04-30 05:54:22 +0000816 if (Code == UnOpInit::HEAD ||
817 Code == UnOpInit::TAIL ||
818 Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000819 ListInit *LHSl = dyn_cast<ListInit>(LHS);
820 StringInit *LHSs = dyn_cast<StringInit>(LHS);
821 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000822 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000823 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000824 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000825 }
826 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000827 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
828 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000829 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000830 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000831 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000832 }
833 }
834
Craig Topper85c07002015-04-30 05:54:22 +0000835 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
Craig Topper011817a2014-04-09 04:50:04 +0000836 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000837 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000838 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000839 }
Bob Wilson7248f862009-11-22 04:24:42 +0000840
Craig Topperec9072d2015-05-14 05:53:53 +0000841 if (LHSl && LHSl->empty()) {
David Greened571b3c2009-05-14 22:38:31 +0000842 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000843 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000844 }
845 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000846 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000847 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000848 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000849 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000850 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000851 }
Craig Toppera9642b42015-05-04 01:35:39 +0000852 Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
853 : ListRecTy::get(Itemt->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000854 } else {
David Greened571b3c2009-05-14 22:38:31 +0000855 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000856 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000857 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000858 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000859 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000860 }
Craig Toppera9642b42015-05-04 01:35:39 +0000861 Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
David Greened571b3c2009-05-14 22:38:31 +0000862 }
863 }
864 }
865
David Greenee8f3b272009-05-14 21:22:49 +0000866 if (Lex.getCode() != tgtok::r_paren) {
867 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000868 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000869 }
870 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000871 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000872 }
David Greene5d0c0512009-05-14 20:54:48 +0000873
874 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000875 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000876 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +0000877 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000878 case tgtok::XSRL:
879 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000880 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000881 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000882 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000883 tgtok::TokKind OpTok = Lex.getCode();
884 SMLoc OpLoc = Lex.getLoc();
885 Lex.Lex(); // eat the operation
886
David Greene5d0c0512009-05-14 20:54:48 +0000887 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000888 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000889
Chris Lattner61ea00b2010-10-05 23:58:18 +0000890 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000891 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000892 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000893 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000894 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000895 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
896 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
897 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
898 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000899 case tgtok::XListConcat:
900 Code = BinOpInit::LISTCONCAT;
901 // We don't know the list type until we parse the first argument
902 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000903 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000904 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000905 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000906 break;
David Greene5d0c0512009-05-14 20:54:48 +0000907 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000908
David Greene5d0c0512009-05-14 20:54:48 +0000909 if (Lex.getCode() != tgtok::l_paren) {
910 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000911 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000912 }
913 Lex.Lex(); // eat the '('
914
David Greeneaf8ee2c2011-07-29 22:43:06 +0000915 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000916
Chris Lattner61ea00b2010-10-05 23:58:18 +0000917 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000918 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000919
Chris Lattner61ea00b2010-10-05 23:58:18 +0000920 while (Lex.getCode() == tgtok::comma) {
921 Lex.Lex(); // eat the ','
922
923 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000924 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000925 }
David Greene5d0c0512009-05-14 20:54:48 +0000926
927 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000928 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000929 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000930 }
931 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000932
Daniel Sanders314e80e2014-05-07 10:13:19 +0000933 // If we are doing !listconcat, we should know the type by now
934 if (OpTok == tgtok::XListConcat) {
935 if (VarInit *Arg0 = dyn_cast<VarInit>(InitList[0]))
936 Type = Arg0->getType();
937 else if (ListInit *Arg0 = dyn_cast<ListInit>(InitList[0]))
938 Type = Arg0->getType();
939 else {
940 InitList[0]->dump();
941 Error(OpLoc, "expected a list");
942 return nullptr;
943 }
944 }
945
Chris Lattner61ea00b2010-10-05 23:58:18 +0000946 // We allow multiple operands to associative operators like !strconcat as
947 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000948 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000949 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000950 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000951 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
952 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000953 InitList.back() = RHS;
954 }
955 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000956
Chris Lattner61ea00b2010-10-05 23:58:18 +0000957 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000958 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000959 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000960
Chris Lattner61ea00b2010-10-05 23:58:18 +0000961 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +0000962 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000963 }
964
David Greene3587eed2009-05-14 23:26:46 +0000965 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +0000966 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +0000967 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
968 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000969 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000970
David Greene98ed3c72009-05-14 21:54:42 +0000971 tgtok::TokKind LexCode = Lex.getCode();
972 Lex.Lex(); // eat the operation
973 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +0000974 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +0000975 case tgtok::XIf:
976 Code = TernOpInit::IF;
977 break;
David Greenee917fff2009-05-14 22:23:47 +0000978 case tgtok::XForEach:
979 Code = TernOpInit::FOREACH;
980 break;
David Greene98ed3c72009-05-14 21:54:42 +0000981 case tgtok::XSubst:
982 Code = TernOpInit::SUBST;
983 break;
984 }
985 if (Lex.getCode() != tgtok::l_paren) {
986 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000987 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +0000988 }
989 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000990
David Greeneaf8ee2c2011-07-29 22:43:06 +0000991 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000992 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000993
David Greene98ed3c72009-05-14 21:54:42 +0000994 if (Lex.getCode() != tgtok::comma) {
995 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000996 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +0000997 }
998 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +0000999
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001000 Init *MHS = ParseValue(CurRec, ItemType);
1001 if (!MHS)
1002 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001003
David Greene98ed3c72009-05-14 21:54:42 +00001004 if (Lex.getCode() != tgtok::comma) {
1005 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001006 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001007 }
1008 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001009
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001010 Init *RHS = ParseValue(CurRec, ItemType);
1011 if (!RHS)
1012 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001013
David Greene98ed3c72009-05-14 21:54:42 +00001014 if (Lex.getCode() != tgtok::r_paren) {
1015 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001016 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001017 }
1018 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001019
David Greene98ed3c72009-05-14 21:54:42 +00001020 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001021 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001022 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001023 RecTy *MHSTy = nullptr;
1024 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001025
Sean Silvafb509ed2012-10-10 20:24:43 +00001026 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001027 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001028 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001029 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001030 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001031 MHSTy = BitRecTy::get();
1032
Sean Silvafb509ed2012-10-10 20:24:43 +00001033 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001034 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001035 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001036 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001037 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001038 RHSTy = BitRecTy::get();
1039
1040 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001041 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001042 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001043 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001044 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001045
1046 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001047 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001048 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001049 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001050
1051 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1052 Type = RHSTy;
1053 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1054 Type = MHSTy;
Bob Wilson7248f862009-11-22 04:24:42 +00001055 } else {
David Greene3587eed2009-05-14 23:26:46 +00001056 TokError("inconsistent types for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001057 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001058 }
1059 break;
1060 }
David Greenee917fff2009-05-14 22:23:47 +00001061 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001062 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
Craig Topper011817a2014-04-09 04:50:04 +00001063 if (!MHSt) {
David Greenee917fff2009-05-14 22:23:47 +00001064 TokError("could not get type for !foreach");
Craig Topper011817a2014-04-09 04:50:04 +00001065 return nullptr;
David Greenee917fff2009-05-14 22:23:47 +00001066 }
1067 Type = MHSt->getType();
1068 break;
1069 }
David Greene98ed3c72009-05-14 21:54:42 +00001070 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001071 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001072 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001073 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001074 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001075 }
1076 Type = RHSt->getType();
1077 break;
1078 }
1079 }
David Greenee32ebf22011-07-29 19:07:07 +00001080 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001081 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001082 }
David Greene5d0c0512009-05-14 20:54:48 +00001083 }
David Greene5d0c0512009-05-14 20:54:48 +00001084}
1085
1086/// ParseOperatorType - Parse a type for an operator. This returns
1087/// null on error.
1088///
1089/// OperatorType ::= '<' Type '>'
1090///
Dan Gohman1432ef82009-08-12 22:10:57 +00001091RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001092 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001093
1094 if (Lex.getCode() != tgtok::less) {
1095 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001096 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001097 }
1098 Lex.Lex(); // eat the <
1099
1100 Type = ParseType();
1101
Craig Topper011817a2014-04-09 04:50:04 +00001102 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001103 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001104 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001105 }
1106
1107 if (Lex.getCode() != tgtok::greater) {
1108 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001109 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001110 }
1111 Lex.Lex(); // eat the >
1112
1113 return Type;
1114}
1115
1116
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001117/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1118///
1119/// SimpleValue ::= IDValue
1120/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001121/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001122/// SimpleValue ::= CODEFRAGMENT
1123/// SimpleValue ::= '?'
1124/// SimpleValue ::= '{' ValueList '}'
1125/// SimpleValue ::= ID '<' ValueListNE '>'
1126/// SimpleValue ::= '[' ValueList ']'
1127/// SimpleValue ::= '(' IDValue DagArgList ')'
1128/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001129/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001130/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1131/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1132/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001133/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001134/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1135///
David Greened4263a62011-10-19 13:04:20 +00001136Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1137 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001138 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001139 switch (Lex.getCode()) {
1140 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001141 case tgtok::paste:
1142 // This is a leading paste operation. This is deprecated but
1143 // still exists in some .td files. Ignore it.
1144 Lex.Lex(); // Skip '#'.
1145 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001146 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001147 case tgtok::BinaryIntVal: {
1148 auto BinaryVal = Lex.getCurBinaryIntVal();
1149 SmallVector<Init*, 16> Bits(BinaryVal.second);
1150 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001151 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001152 R = BitsInit::get(Bits);
1153 Lex.Lex();
1154 break;
1155 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001156 case tgtok::StrVal: {
1157 std::string Val = Lex.getCurStrVal();
1158 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001159
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001160 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001161 while (Lex.getCode() == tgtok::StrVal) {
1162 Val += Lex.getCurStrVal();
1163 Lex.Lex();
1164 }
Bob Wilson7248f862009-11-22 04:24:42 +00001165
David Greenee32ebf22011-07-29 19:07:07 +00001166 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001167 break;
1168 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001169 case tgtok::CodeFragment:
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +00001170 R = StringInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001171 Lex.Lex();
1172 break;
1173 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001174 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001175 Lex.Lex();
1176 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001177 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001178 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001179 std::string Name = Lex.getCurStrVal();
1180 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001181 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001182
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001183 // Value ::= ID '<' ValueListNE '>'
1184 if (Lex.Lex() == tgtok::greater) {
1185 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001186 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001187 }
David Greene8618f952009-06-08 20:23:18 +00001188
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001189 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1190 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1191 // body.
1192 Record *Class = Records.getClass(Name);
1193 if (!Class) {
1194 Error(NameLoc, "Expected a class name, got '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001195 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001196 }
David Greene8618f952009-06-08 20:23:18 +00001197
David Greeneaf8ee2c2011-07-29 22:43:06 +00001198 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
Craig Topper011817a2014-04-09 04:50:04 +00001199 if (ValueList.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001200
David Greene8618f952009-06-08 20:23:18 +00001201 if (Lex.getCode() != tgtok::greater) {
1202 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001203 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001204 }
1205 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001206 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001207
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001208 // Create the new record, set it as CurRec temporarily.
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00001209 auto NewRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), NameLoc,
1210 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001211 Record *NewRec = NewRecOwner.get(); // Keep a copy since we may release.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001212 SubClassReference SCRef;
Jordan Rosef12e8a92013-01-10 18:50:11 +00001213 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001214 SCRef.Rec = Class;
1215 SCRef.TemplateArgs = ValueList;
1216 // Add info about the subclass to NewRec.
Craig Topper84138712014-11-29 05:31:10 +00001217 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001218 return nullptr;
Craig Topper84138712014-11-29 05:31:10 +00001219
Hal Finkela8c1f462014-01-02 20:47:09 +00001220 if (!CurMultiClass) {
1221 NewRec->resolveReferences();
Craig Toppercdab2322014-11-29 05:52:51 +00001222 Records.addDef(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001223 } else {
Adam Nemete5a07162014-09-16 17:14:13 +00001224 // This needs to get resolved once the multiclass template arguments are
1225 // known before any use.
1226 NewRec->setResolveFirst(true);
Hal Finkela8c1f462014-01-02 20:47:09 +00001227 // Otherwise, we're inside a multiclass, add it to the multiclass.
Craig Topperc3504c42014-12-11 05:25:33 +00001228 CurMultiClass->DefPrototypes.push_back(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001229
1230 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00001231 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
1232 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Hal Finkela8c1f462014-01-02 20:47:09 +00001233 assert(RV && "Template arg doesn't exist?");
1234 NewRec->addValue(*RV);
1235 }
1236
1237 // We can't return the prototype def here, instead return:
1238 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1239 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1240 assert(MCNameRV && "multiclass record must have a NAME");
1241
1242 return UnOpInit::get(UnOpInit::CAST,
1243 BinOpInit::get(BinOpInit::STRCONCAT,
1244 VarInit::get(MCNameRV->getName(),
1245 MCNameRV->getType()),
1246 NewRec->getNameInit(),
1247 StringRecTy::get()),
1248 Class->getDefInit()->getType());
1249 }
Bob Wilson7248f862009-11-22 04:24:42 +00001250
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001251 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001252 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001253 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001254 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001255 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001256 Lex.Lex(); // eat the '{'
David Greeneaf8ee2c2011-07-29 22:43:06 +00001257 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001258
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001259 if (Lex.getCode() != tgtok::r_brace) {
1260 Vals = ParseValueList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001261 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001262 }
1263 if (Lex.getCode() != tgtok::r_brace) {
1264 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001265 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001266 }
1267 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001268
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001269 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001270
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001271 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1272 // first. We'll first read everything in to a vector, then we can reverse
1273 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001274 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001275 // FIXME: The following two loops would not be duplicated
1276 // if the API was a little more orthogonal.
1277
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001278 // bits<n> values are allowed to initialize n bits.
1279 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1280 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1281 NewBits.push_back(BI->getBit((e - i) - 1));
1282 continue;
1283 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001284 // bits<n> can also come from variable initializers.
1285 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1286 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1287 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1288 NewBits.push_back(VI->getBit((e - i) - 1));
1289 continue;
1290 }
1291 // Fallthrough to try convert this to a bit.
1292 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001293 // All other values must be convertible to just a single bit.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001294 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001295 if (!Bit) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001296 Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
Chris Lattner52416952007-11-22 21:06:59 +00001297 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001298 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001299 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001300 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001301 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001302 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001303 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001304 }
1305 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1306 Lex.Lex(); // eat the '['
David Greeneaf8ee2c2011-07-29 22:43:06 +00001307 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001308
Craig Topper011817a2014-04-09 04:50:04 +00001309 RecTy *DeducedEltTy = nullptr;
1310 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001311
Craig Topper011817a2014-04-09 04:50:04 +00001312 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001313 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001314 if (!ListType) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001315 TokError(Twine("Type mismatch for list, expected list type, got ") +
1316 ItemType->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001317 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001318 }
1319 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001320 }
David Greene8618f952009-06-08 20:23:18 +00001321
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001322 if (Lex.getCode() != tgtok::r_square) {
Craig Topper011817a2014-04-09 04:50:04 +00001323 Vals = ParseValueList(CurRec, nullptr,
1324 GivenListTy ? GivenListTy->getElementType() : nullptr);
1325 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001326 }
1327 if (Lex.getCode() != tgtok::r_square) {
1328 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001329 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001330 }
1331 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001332
Craig Topper011817a2014-04-09 04:50:04 +00001333 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001334 if (Lex.getCode() == tgtok::less) {
1335 // Optional list element type
1336 Lex.Lex(); // eat the '<'
1337
1338 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001339 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001340 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001341 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001342 }
1343
1344 if (Lex.getCode() != tgtok::greater) {
1345 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001346 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001347 }
1348 Lex.Lex(); // eat the '>'
1349 }
1350
1351 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001352 RecTy *EltTy = nullptr;
Craig Toppereb4d7c62015-04-29 04:43:36 +00001353 for (Init *V : Vals) {
1354 TypedInit *TArg = dyn_cast<TypedInit>(V);
Craig Topper011817a2014-04-09 04:50:04 +00001355 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001356 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001357 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001358 }
Craig Topper011817a2014-04-09 04:50:04 +00001359 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001360 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001361 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001362 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001363 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001364 }
Bob Wilson7248f862009-11-22 04:24:42 +00001365 } else {
David Greene8618f952009-06-08 20:23:18 +00001366 EltTy = TArg->getType();
1367 }
1368 }
1369
Craig Topper011817a2014-04-09 04:50:04 +00001370 if (GivenEltTy) {
1371 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001372 // Verify consistency
1373 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1374 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001375 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001376 }
1377 }
1378 EltTy = GivenEltTy;
1379 }
1380
Craig Topper011817a2014-04-09 04:50:04 +00001381 if (!EltTy) {
1382 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001383 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001384 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001385 }
1386 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001387 } else {
David Greene8618f952009-06-08 20:23:18 +00001388 // Make sure the deduced type is compatible with the given type
1389 if (GivenListTy) {
1390 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1391 TokError("Element type mismatch for list");
Craig Topper011817a2014-04-09 04:50:04 +00001392 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001393 }
1394 }
1395 DeducedEltTy = EltTy;
1396 }
Bob Wilson7248f862009-11-22 04:24:42 +00001397
David Greenee32ebf22011-07-29 19:07:07 +00001398 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001399 }
1400 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1401 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001402 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001403 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001404 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001405 }
Bob Wilson7248f862009-11-22 04:24:42 +00001406
David Greeneaf8ee2c2011-07-29 22:43:06 +00001407 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001408 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001409
Nate Begemandbe3f772009-03-19 05:21:56 +00001410 // If the operator name is present, parse it.
1411 std::string OperatorName;
1412 if (Lex.getCode() == tgtok::colon) {
1413 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1414 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001415 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001416 }
1417 OperatorName = Lex.getCurStrVal();
1418 Lex.Lex(); // eat the VarName.
1419 }
Bob Wilson7248f862009-11-22 04:24:42 +00001420
David Greeneaf8ee2c2011-07-29 22:43:06 +00001421 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001422 if (Lex.getCode() != tgtok::r_paren) {
1423 DagArgs = ParseDagArgList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001424 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001425 }
Bob Wilson7248f862009-11-22 04:24:42 +00001426
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001427 if (Lex.getCode() != tgtok::r_paren) {
1428 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001429 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001430 }
1431 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001432
David Greenee32ebf22011-07-29 19:07:07 +00001433 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001434 }
Bob Wilson7248f862009-11-22 04:24:42 +00001435
David Greene2f7cf7f2011-01-07 17:05:37 +00001436 case tgtok::XHead:
1437 case tgtok::XTail:
1438 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001439 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001440 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001441 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001442 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +00001443 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001444 case tgtok::XSRL:
1445 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001446 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001447 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001448 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001449 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001450 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001451 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001452 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001453 }
1454 }
Bob Wilson7248f862009-11-22 04:24:42 +00001455
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001456 return R;
1457}
1458
1459/// ParseValue - Parse a tblgen value. This returns null on error.
1460///
1461/// Value ::= SimpleValue ValueSuffix*
1462/// ValueSuffix ::= '{' BitList '}'
1463/// ValueSuffix ::= '[' BitList ']'
1464/// ValueSuffix ::= '.' ID
1465///
David Greened4263a62011-10-19 13:04:20 +00001466Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1467 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001468 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001469
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001470 // Parse the suffixes now if present.
1471 while (1) {
1472 switch (Lex.getCode()) {
1473 default: return Result;
1474 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001475 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001476 // This is the beginning of the object body.
1477 return Result;
1478
Chris Lattner526c8cb2009-06-21 03:39:35 +00001479 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001480 Lex.Lex(); // eat the '{'
1481 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001482 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001483
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001484 // Reverse the bitlist.
1485 std::reverse(Ranges.begin(), Ranges.end());
1486 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001487 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001488 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001489 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001490 }
Bob Wilson7248f862009-11-22 04:24:42 +00001491
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001492 // Eat the '}'.
1493 if (Lex.getCode() != tgtok::r_brace) {
1494 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001495 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001496 }
1497 Lex.Lex();
1498 break;
1499 }
1500 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001501 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001502 Lex.Lex(); // eat the '['
1503 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001504 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001505
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001506 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001507 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001508 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001509 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001510 }
Bob Wilson7248f862009-11-22 04:24:42 +00001511
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001512 // Eat the ']'.
1513 if (Lex.getCode() != tgtok::r_square) {
1514 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001515 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001516 }
1517 Lex.Lex();
1518 break;
1519 }
1520 case tgtok::period:
1521 if (Lex.Lex() != tgtok::Id) { // eat the .
1522 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001523 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001524 }
1525 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001526 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001527 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001528 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001529 }
David Greenee32ebf22011-07-29 19:07:07 +00001530 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001531 Lex.Lex(); // eat field name
1532 break;
David Greene8e85b482011-10-19 13:04:43 +00001533
1534 case tgtok::paste:
1535 SMLoc PasteLoc = Lex.getLoc();
1536
1537 // Create a !strconcat() operation, first casting each operand to
1538 // a string if necessary.
1539
Sean Silvafb509ed2012-10-10 20:24:43 +00001540 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001541 if (!LHS) {
1542 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001543 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001544 }
Craig Toppera9642b42015-05-04 01:35:39 +00001545
David Greene8e85b482011-10-19 13:04:43 +00001546 if (LHS->getType() != StringRecTy::get()) {
1547 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1548 }
1549
Craig Topper011817a2014-04-09 04:50:04 +00001550 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001551
1552 Lex.Lex(); // Eat the '#'.
1553 switch (Lex.getCode()) {
1554 case tgtok::colon:
1555 case tgtok::semi:
1556 case tgtok::l_brace:
1557 // These are all of the tokens that can begin an object body.
1558 // Some of these can also begin values but we disallow those cases
1559 // because they are unlikely to be useful.
Craig Toppera9642b42015-05-04 01:35:39 +00001560
David Greene8e85b482011-10-19 13:04:43 +00001561 // Trailing paste, concat with an empty string.
1562 RHS = StringInit::get("");
1563 break;
1564
1565 default:
1566 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001567 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001568 if (!RHS) {
1569 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001570 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001571 }
1572
1573 if (RHS->getType() != StringRecTy::get()) {
1574 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1575 }
Craig Toppera9642b42015-05-04 01:35:39 +00001576
David Greene8e85b482011-10-19 13:04:43 +00001577 break;
1578 }
1579
1580 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1581 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1582 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001583 }
1584 }
1585}
1586
1587/// ParseDagArgList - Parse the argument list for a dag literal expression.
1588///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001589/// DagArg ::= Value (':' VARNAME)?
1590/// DagArg ::= VARNAME
1591/// DagArgList ::= DagArg
1592/// DagArgList ::= DagArgList ',' DagArg
David Greeneaf8ee2c2011-07-29 22:43:06 +00001593std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001594TGParser::ParseDagArgList(Record *CurRec) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001595 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson7248f862009-11-22 04:24:42 +00001596
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001597 while (1) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001598 // DagArg ::= VARNAME
1599 if (Lex.getCode() == tgtok::VarName) {
1600 // A missing value is treated like '?'.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00001601 Result.emplace_back(UnsetInit::get(), Lex.getCurStrVal());
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001602 Lex.Lex();
1603 } else {
1604 // DagArg ::= Value (':' VARNAME)?
1605 Init *Val = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001606 if (!Val)
David Greeneaf8ee2c2011-07-29 22:43:06 +00001607 return std::vector<std::pair<llvm::Init*, std::string> >();
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001608
1609 // If the variable name is present, add it.
1610 std::string VarName;
1611 if (Lex.getCode() == tgtok::colon) {
1612 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1613 TokError("expected variable name in dag literal");
1614 return std::vector<std::pair<llvm::Init*, std::string> >();
1615 }
1616 VarName = Lex.getCurStrVal();
1617 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001618 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001619
1620 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001621 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001622 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001623 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001624 }
Bob Wilson7248f862009-11-22 04:24:42 +00001625
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001626 return Result;
1627}
1628
1629
1630/// ParseValueList - Parse a comma separated list of values, returning them as a
1631/// vector. Note that this always expects to be able to parse at least one
1632/// value. It returns an empty list if this is not possible.
1633///
1634/// ValueList ::= Value (',' Value)
1635///
David Greeneaf8ee2c2011-07-29 22:43:06 +00001636std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopher71520a82011-07-11 23:06:52 +00001637 RecTy *EltTy) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001638 std::vector<Init*> Result;
David Greene8618f952009-06-08 20:23:18 +00001639 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001640 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001641 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001642 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001643 if (TArgs.empty()) {
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001644 TokError("template argument provided to non-template class");
1645 return std::vector<Init*>();
1646 }
David Greene8618f952009-06-08 20:23:18 +00001647 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001648 if (!RV) {
1649 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1650 << ")\n";
1651 }
David Greene8618f952009-06-08 20:23:18 +00001652 assert(RV && "Template argument record not found??");
1653 ItemType = RV->getType();
1654 ++ArgN;
1655 }
1656 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001657 if (!Result.back()) return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001658
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001659 while (Lex.getCode() == tgtok::comma) {
1660 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001661
Craig Topper011817a2014-04-09 04:50:04 +00001662 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001663 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001664 if (ArgN >= TArgs.size()) {
1665 TokError("too many template arguments");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001666 return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001667 }
David Greene8618f952009-06-08 20:23:18 +00001668 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1669 assert(RV && "Template argument record not found??");
1670 ItemType = RV->getType();
1671 ++ArgN;
1672 }
1673 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001674 if (!Result.back()) return std::vector<Init*>();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001675 }
Bob Wilson7248f862009-11-22 04:24:42 +00001676
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001677 return Result;
1678}
1679
1680
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001681/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1682/// empty string on error. This can happen in a number of different context's,
1683/// including within a def or in the template args for a def (which which case
1684/// CurRec will be non-null) and within the template args for a multiclass (in
1685/// which case CurRec will be null, but CurMultiClass will be set). This can
1686/// also happen within a def that is within a multiclass, which will set both
1687/// CurRec and CurMultiClass.
1688///
1689/// Declaration ::= FIELD? Type ID ('=' Value)?
1690///
David Greenedb10e692011-10-19 13:02:42 +00001691Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001692 bool ParsingTemplateArgs) {
1693 // Read the field prefix if present.
1694 bool HasField = Lex.getCode() == tgtok::Field;
1695 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001696
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001697 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001698 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001699
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001700 if (Lex.getCode() != tgtok::Id) {
1701 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001702 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001703 }
Bob Wilson7248f862009-11-22 04:24:42 +00001704
Chris Lattner526c8cb2009-06-21 03:39:35 +00001705 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001706 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001707 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001708
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001709 if (ParsingTemplateArgs) {
Craig Toppera9642b42015-05-04 01:35:39 +00001710 if (CurRec)
David Greenedb10e692011-10-19 13:02:42 +00001711 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Craig Toppera9642b42015-05-04 01:35:39 +00001712 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001713 assert(CurMultiClass);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001714 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001715 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1716 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001717 }
Bob Wilson7248f862009-11-22 04:24:42 +00001718
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001719 // Add the value.
1720 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001721 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001722
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001723 // If a value is present, parse it.
1724 if (Lex.getCode() == tgtok::equal) {
1725 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001726 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001727 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001728 if (!Val ||
Craig Toppercfd81732016-01-04 03:15:08 +00001729 SetValue(CurRec, ValLoc, DeclName, None, Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001730 // Return the name, even if an error is thrown. This is so that we can
1731 // continue to make some progress, even without the value having been
1732 // initialized.
1733 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001734 }
Bob Wilson7248f862009-11-22 04:24:42 +00001735
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001736 return DeclName;
1737}
1738
David Greenefb927af2012-02-22 16:09:41 +00001739/// ParseForeachDeclaration - Read a foreach declaration, returning
1740/// the name of the declared object or a NULL Init on error. Return
1741/// the name of the parsed initializer list through ForeachListName.
1742///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001743/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1744/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1745/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001746///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001747VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001748 if (Lex.getCode() != tgtok::Id) {
1749 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001750 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001751 }
1752
1753 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1754 Lex.Lex();
1755
1756 // If a value is present, parse it.
1757 if (Lex.getCode() != tgtok::equal) {
1758 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001759 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001760 }
1761 Lex.Lex(); // Eat the '='
1762
Craig Topper011817a2014-04-09 04:50:04 +00001763 RecTy *IterType = nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001764 std::vector<unsigned> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001765
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001766 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001767 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001768 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001769 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001770 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001771 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001772 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001773 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001774 }
1775 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001776 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001777 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001778 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001779 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001780 }
1781 IterType = ListType->getElementType();
1782 break;
David Greenefb927af2012-02-22 16:09:41 +00001783 }
1784
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001785 case tgtok::IntVal: { // RangePiece.
1786 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001787 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001788 break;
David Greenefb927af2012-02-22 16:09:41 +00001789 }
1790
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001791 case tgtok::l_brace: { // '{' RangeList '}'
1792 Lex.Lex(); // eat the '{'
1793 Ranges = ParseRangeList();
1794 if (Lex.getCode() != tgtok::r_brace) {
1795 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001796 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001797 }
1798 Lex.Lex();
1799 break;
1800 }
1801 }
David Greenefb927af2012-02-22 16:09:41 +00001802
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001803 if (!Ranges.empty()) {
1804 assert(!IterType && "Type already initialized?");
1805 IterType = IntRecTy::get();
1806 std::vector<Init*> Values;
Craig Topper8eb764c2015-06-02 06:19:28 +00001807 for (unsigned R : Ranges)
1808 Values.push_back(IntInit::get(R));
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001809 ForeachListValue = ListInit::get(Values, IterType);
1810 }
1811
1812 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001813 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001814
1815 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001816}
1817
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001818/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1819/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1820/// template args for a def, which may or may not be in a multiclass. If null,
1821/// these are the template args for a multiclass.
1822///
1823/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001824///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001825bool TGParser::ParseTemplateArgList(Record *CurRec) {
1826 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1827 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001828
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001829 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001830
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001831 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001832 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001833 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001834 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001835
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001836 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001837
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001838 while (Lex.getCode() == tgtok::comma) {
1839 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001840
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001841 // Read the following declarations.
1842 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001843 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001844 return true;
1845 TheRecToAddTo->addTemplateArg(TemplArg);
1846 }
Bob Wilson7248f862009-11-22 04:24:42 +00001847
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001848 if (Lex.getCode() != tgtok::greater)
1849 return TokError("expected '>' at end of template argument list");
1850 Lex.Lex(); // eat the '>'.
1851 return false;
1852}
1853
1854
1855/// ParseBodyItem - Parse a single item at within the body of a def or class.
1856///
1857/// BodyItem ::= Declaration ';'
1858/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1859bool TGParser::ParseBodyItem(Record *CurRec) {
1860 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001861 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001862 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001863
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001864 if (Lex.getCode() != tgtok::semi)
1865 return TokError("expected ';' after declaration");
1866 Lex.Lex();
1867 return false;
1868 }
1869
1870 // LET ID OptionalRangeList '=' Value ';'
1871 if (Lex.Lex() != tgtok::Id)
1872 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001873
Chris Lattner526c8cb2009-06-21 03:39:35 +00001874 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001875 std::string FieldName = Lex.getCurStrVal();
1876 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001877
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001878 std::vector<unsigned> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001879 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001880 return true;
1881 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001882
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001883 if (Lex.getCode() != tgtok::equal)
1884 return TokError("expected '=' in let expression");
1885 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001886
David Greene8618f952009-06-08 20:23:18 +00001887 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001888 if (!Field)
David Greene8618f952009-06-08 20:23:18 +00001889 return TokError("Value '" + FieldName + "' unknown!");
1890
1891 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001892
David Greeneaf8ee2c2011-07-29 22:43:06 +00001893 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001894 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001895
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001896 if (Lex.getCode() != tgtok::semi)
1897 return TokError("expected ';' after let expression");
1898 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001899
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001900 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1901}
1902
1903/// ParseBody - Read the body of a class or def. Return true on error, false on
1904/// success.
1905///
1906/// Body ::= ';'
1907/// Body ::= '{' BodyList '}'
1908/// BodyList BodyItem*
1909///
1910bool TGParser::ParseBody(Record *CurRec) {
1911 // If this is a null definition, just eat the semi and return.
1912 if (Lex.getCode() == tgtok::semi) {
1913 Lex.Lex();
1914 return false;
1915 }
Bob Wilson7248f862009-11-22 04:24:42 +00001916
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001917 if (Lex.getCode() != tgtok::l_brace)
1918 return TokError("Expected ';' or '{' to start body");
1919 // Eat the '{'.
1920 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001921
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001922 while (Lex.getCode() != tgtok::r_brace)
1923 if (ParseBodyItem(CurRec))
1924 return true;
1925
1926 // Eat the '}'.
1927 Lex.Lex();
1928 return false;
1929}
1930
Sean Silvacb1a75e2013-01-09 04:49:14 +00001931/// \brief Apply the current let bindings to \a CurRec.
1932/// \returns true on error, false otherwise.
1933bool TGParser::ApplyLetStack(Record *CurRec) {
Craig Topper8eb764c2015-06-02 06:19:28 +00001934 for (std::vector<LetRecord> &LetInfo : LetStack)
1935 for (LetRecord &LR : LetInfo)
1936 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
Sean Silvacb1a75e2013-01-09 04:49:14 +00001937 return true;
1938 return false;
1939}
1940
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001941/// ParseObjectBody - Parse the body of a def or class. This consists of an
1942/// optional ClassList followed by a Body. CurRec is the current def or class
1943/// that is being parsed.
1944///
1945/// ObjectBody ::= BaseClassList Body
1946/// BaseClassList ::= /*empty*/
1947/// BaseClassList ::= ':' BaseClassListNE
1948/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1949///
1950bool TGParser::ParseObjectBody(Record *CurRec) {
1951 // If there is a baseclass list, read it.
1952 if (Lex.getCode() == tgtok::colon) {
1953 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001954
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001955 // Read all of the subclasses.
1956 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1957 while (1) {
1958 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00001959 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001960
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001961 // Add it.
1962 if (AddSubClass(CurRec, SubClass))
1963 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001964
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001965 if (Lex.getCode() != tgtok::comma) break;
1966 Lex.Lex(); // eat ','.
1967 SubClass = ParseSubClassReference(CurRec, false);
1968 }
1969 }
1970
Sean Silvacb1a75e2013-01-09 04:49:14 +00001971 if (ApplyLetStack(CurRec))
1972 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001973
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001974 return ParseBody(CurRec);
1975}
1976
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001977/// ParseDef - Parse and return a top level or multiclass def, return the record
1978/// corresponding to it. This returns null on error.
1979///
1980/// DefInst ::= DEF ObjectName ObjectBody
1981///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00001982bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001983 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001984 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00001985 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001986
1987 // Parse ObjectName and make a record for it.
Craig Topper84138712014-11-29 05:31:10 +00001988 std::unique_ptr<Record> CurRecOwner;
Jordan Roseabdd99b2013-01-10 18:50:05 +00001989 Init *Name = ParseObjectName(CurMultiClass);
1990 if (Name)
Craig Topper84138712014-11-29 05:31:10 +00001991 CurRecOwner = make_unique<Record>(Name, DefLoc, Records);
Jordan Roseabdd99b2013-01-10 18:50:05 +00001992 else
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00001993 CurRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), DefLoc,
1994 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001995 Record *CurRec = CurRecOwner.get(); // Keep a copy since we may release.
Bob Wilson7248f862009-11-22 04:24:42 +00001996
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00001997 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001998 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00001999
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002000 // Ensure redefinition doesn't happen.
Craig Topper84138712014-11-29 05:31:10 +00002001 if (Records.getDef(CurRec->getNameInitAsString()))
2002 return Error(DefLoc, "def '" + CurRec->getNameInitAsString()+
2003 "' already defined");
Craig Toppercdab2322014-11-29 05:52:51 +00002004 Records.addDef(std::move(CurRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00002005
2006 if (ParseObjectBody(CurRec))
2007 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002008 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002009 // Parse the body before adding this prototype to the DefPrototypes vector.
2010 // That way implicit definitions will be added to the DefPrototypes vector
2011 // before this object, instantiated prior to defs derived from this object,
2012 // and this available for indirect name resolution when defs derived from
2013 // this object are instantiated.
Craig Topper84138712014-11-29 05:31:10 +00002014 if (ParseObjectBody(CurRec))
Hal Finkela8c1f462014-01-02 20:47:09 +00002015 return true;
2016
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002017 // Otherwise, a def inside a multiclass, add it to the multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002018 for (const auto &Proto : CurMultiClass->DefPrototypes)
2019 if (Proto->getNameInit() == CurRec->getNameInit())
Craig Topper84138712014-11-29 05:31:10 +00002020 return Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
2021 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002022 CurMultiClass->DefPrototypes.push_back(std::move(CurRecOwner));
Anton Yartsev671dff12014-08-08 00:29:54 +00002023 } else if (ParseObjectBody(CurRec)) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002024 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002025 }
Bob Wilson7248f862009-11-22 04:24:42 +00002026
Craig Topper011817a2014-04-09 04:50:04 +00002027 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002028 // See Record::setName(). This resolve step will see any new name
2029 // for the def that might have been created when resolving
2030 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002031 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002032
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002033 // If ObjectBody has template arguments, it's an error.
2034 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002035
2036 if (CurMultiClass) {
2037 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002038 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
2039 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002040 assert(RV && "Template arg doesn't exist?");
2041 CurRec->addValue(*RV);
2042 }
2043 }
2044
Craig Toppera9642b42015-05-04 01:35:39 +00002045 if (ProcessForeachDefs(CurRec, DefLoc))
Craig Topper84138712014-11-29 05:31:10 +00002046 return Error(DefLoc, "Could not process loops for def" +
2047 CurRec->getNameInitAsString());
David Greenefb927af2012-02-22 16:09:41 +00002048
2049 return false;
2050}
2051
2052/// ParseForeach - Parse a for statement. Return the record corresponding
2053/// to it. This returns true on error.
2054///
2055/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2056/// Foreach ::= FOREACH Declaration IN Object
2057///
2058bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2059 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2060 Lex.Lex(); // Eat the 'for' token.
2061
2062 // Make a temporary object to record items associated with the for
2063 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002064 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002065 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002066 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002067 return TokError("expected declaration in for");
2068
2069 if (Lex.getCode() != tgtok::In)
2070 return TokError("Unknown tok");
2071 Lex.Lex(); // Eat the in
2072
2073 // Create a loop object and remember it.
2074 Loops.push_back(ForeachLoop(IterName, ListValue));
2075
2076 if (Lex.getCode() != tgtok::l_brace) {
2077 // FOREACH Declaration IN Object
2078 if (ParseObject(CurMultiClass))
2079 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002080 } else {
David Greenefb927af2012-02-22 16:09:41 +00002081 SMLoc BraceLoc = Lex.getLoc();
2082 // Otherwise, this is a group foreach.
2083 Lex.Lex(); // eat the '{'.
2084
2085 // Parse the object list.
2086 if (ParseObjectList(CurMultiClass))
2087 return true;
2088
2089 if (Lex.getCode() != tgtok::r_brace) {
2090 TokError("expected '}' at end of foreach command");
2091 return Error(BraceLoc, "to match this '{'");
2092 }
2093 Lex.Lex(); // Eat the }
2094 }
2095
2096 // We've processed everything in this loop.
2097 Loops.pop_back();
2098
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002099 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002100}
2101
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002102/// ParseClass - Parse a tblgen class definition.
2103///
2104/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2105///
2106bool TGParser::ParseClass() {
2107 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2108 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002109
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002110 if (Lex.getCode() != tgtok::Id)
2111 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002112
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002113 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2114 if (CurRec) {
2115 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002116 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002117 !CurRec->getSuperClasses().empty() ||
2118 !CurRec->getTemplateArgs().empty())
Craig Topper85c07002015-04-30 05:54:22 +00002119 return TokError("Class '" + CurRec->getNameInitAsString() +
2120 "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002121 } else {
2122 // If this is the first reference to this class, create and add it.
Hans Wennborgffbbd532014-11-30 00:31:49 +00002123 auto NewRec =
2124 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
Craig Toppercdab2322014-11-29 05:52:51 +00002125 CurRec = NewRec.get();
2126 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002127 }
2128 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002129
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002130 // If there are template args, parse them.
2131 if (Lex.getCode() == tgtok::less)
2132 if (ParseTemplateArgList(CurRec))
2133 return true;
2134
2135 // Finally, parse the object body.
2136 return ParseObjectBody(CurRec);
2137}
2138
2139/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2140/// of LetRecords.
2141///
2142/// LetList ::= LetItem (',' LetItem)*
2143/// LetItem ::= ID OptionalRangeList '=' Value
2144///
2145std::vector<LetRecord> TGParser::ParseLetList() {
2146 std::vector<LetRecord> Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002147
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002148 while (1) {
2149 if (Lex.getCode() != tgtok::Id) {
2150 TokError("expected identifier in let definition");
2151 return std::vector<LetRecord>();
2152 }
2153 std::string Name = Lex.getCurStrVal();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002154 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002155 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002156
2157 // Check for an optional RangeList.
2158 std::vector<unsigned> Bits;
Bob Wilson7248f862009-11-22 04:24:42 +00002159 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002160 return std::vector<LetRecord>();
2161 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002162
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002163 if (Lex.getCode() != tgtok::equal) {
2164 TokError("expected '=' in let expression");
2165 return std::vector<LetRecord>();
2166 }
2167 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002168
Craig Topper011817a2014-04-09 04:50:04 +00002169 Init *Val = ParseValue(nullptr);
2170 if (!Val) return std::vector<LetRecord>();
Bob Wilson7248f862009-11-22 04:24:42 +00002171
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002172 // Now that we have everything, add the record.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00002173 Result.emplace_back(std::move(Name), std::move(Bits), Val, NameLoc);
Bob Wilson7248f862009-11-22 04:24:42 +00002174
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002175 if (Lex.getCode() != tgtok::comma)
2176 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002177 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002178 }
2179}
2180
2181/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002182/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002183///
2184/// Object ::= LET LetList IN '{' ObjectList '}'
2185/// Object ::= LET LetList IN Object
2186///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002187bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002188 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2189 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002190
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002191 // Add this entry to the let stack.
2192 std::vector<LetRecord> LetInfo = ParseLetList();
2193 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002194 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002195
2196 if (Lex.getCode() != tgtok::In)
2197 return TokError("expected 'in' at end of top-level 'let'");
2198 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002199
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002200 // If this is a scalar let, just handle it now
2201 if (Lex.getCode() != tgtok::l_brace) {
2202 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002203 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002204 return true;
2205 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002206 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002207 // Otherwise, this is a group let.
2208 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002209
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002210 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002211 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002212 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002213
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002214 if (Lex.getCode() != tgtok::r_brace) {
2215 TokError("expected '}' at end of top level let command");
2216 return Error(BraceLoc, "to match this '{'");
2217 }
2218 Lex.Lex();
2219 }
Bob Wilson7248f862009-11-22 04:24:42 +00002220
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002221 // Outside this let scope, this let block is not active.
2222 LetStack.pop_back();
2223 return false;
2224}
2225
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002226/// ParseMultiClass - Parse a multiclass definition.
2227///
Bob Wilson3d948162009-04-28 19:41:44 +00002228/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002229/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2230/// MultiClassObject ::= DefInst
2231/// MultiClassObject ::= MultiClassInst
2232/// MultiClassObject ::= DefMInst
2233/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2234/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002235///
2236bool TGParser::ParseMultiClass() {
2237 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2238 Lex.Lex(); // Eat the multiclass token.
2239
2240 if (Lex.getCode() != tgtok::Id)
2241 return TokError("expected identifier after multiclass for name");
2242 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002243
Craig Topper7adf2bf2014-12-11 05:25:30 +00002244 auto Result =
2245 MultiClasses.insert(std::make_pair(Name,
2246 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2247
2248 if (!Result.second)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002249 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002250
Craig Topper7adf2bf2014-12-11 05:25:30 +00002251 CurMultiClass = Result.first->second.get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002252 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002253
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002254 // If there are template args, parse them.
2255 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002256 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002257 return true;
2258
David Greene7049e792009-04-24 16:55:41 +00002259 bool inherits = false;
2260
David Greene753ed8f2009-04-22 16:42:54 +00002261 // If there are submulticlasses, parse them.
2262 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002263 inherits = true;
2264
David Greene753ed8f2009-04-22 16:42:54 +00002265 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002266
David Greene753ed8f2009-04-22 16:42:54 +00002267 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002268 SubMultiClassReference SubMultiClass =
2269 ParseSubMultiClassReference(CurMultiClass);
David Greene753ed8f2009-04-22 16:42:54 +00002270 while (1) {
2271 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002272 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002273
David Greene753ed8f2009-04-22 16:42:54 +00002274 // Add it.
2275 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2276 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002277
David Greene753ed8f2009-04-22 16:42:54 +00002278 if (Lex.getCode() != tgtok::comma) break;
2279 Lex.Lex(); // eat ','.
2280 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2281 }
2282 }
2283
David Greene7049e792009-04-24 16:55:41 +00002284 if (Lex.getCode() != tgtok::l_brace) {
2285 if (!inherits)
2286 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002287 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002288 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002289 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002290 } else {
David Greene7049e792009-04-24 16:55:41 +00002291 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2292 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002293
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002294 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002295 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002296 default:
2297 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
2298 case tgtok::Let:
2299 case tgtok::Def:
2300 case tgtok::Defm:
2301 case tgtok::Foreach:
2302 if (ParseObject(CurMultiClass))
2303 return true;
2304 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002305 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002306 }
David Greene7049e792009-04-24 16:55:41 +00002307 Lex.Lex(); // eat the '}'.
2308 }
Bob Wilson7248f862009-11-22 04:24:42 +00002309
Craig Topper011817a2014-04-09 04:50:04 +00002310 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002311 return false;
2312}
2313
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002314Record *TGParser::InstantiateMulticlassDef(MultiClass &MC, Record *DefProto,
2315 Init *&DefmPrefix,
2316 SMRange DefmPrefixRange,
2317 ArrayRef<Init *> TArgs,
2318 std::vector<Init *> &TemplateVals) {
David Greene5d5d88c2011-10-19 13:04:31 +00002319 // We need to preserve DefProto so it can be reused for later
2320 // instantiations, so create a new Record to inherit from it.
2321
David Greenedb445972011-10-05 22:42:07 +00002322 // Add in the defm name. If the defm prefix is empty, give each
2323 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2324 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2325 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002326
Jordan Roseabdd99b2013-01-10 18:50:05 +00002327 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002328 if (!DefmPrefix) {
David Greene5d5d88c2011-10-19 13:04:31 +00002329 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002330 IsAnonymous = true;
2331 }
David Greene5d5d88c2011-10-19 13:04:31 +00002332
2333 Init *DefName = DefProto->getNameInit();
Sean Silvafb509ed2012-10-10 20:24:43 +00002334 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002335
Craig Topper011817a2014-04-09 04:50:04 +00002336 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002337 // We have a fully expanded string so there are no operators to
2338 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002339 DefName =
2340 BinOpInit::get(BinOpInit::STRCONCAT,
2341 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2342 StringRecTy::get())->Fold(DefProto, &MC),
2343 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2344 }
David Greene5d5d88c2011-10-19 13:04:31 +00002345
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002346 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002347 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002348 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Craig Topper84138712014-11-29 05:31:10 +00002349 auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002350
2351 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002352 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002353 Ref.Rec = DefProto;
Craig Topper84138712014-11-29 05:31:10 +00002354 AddSubClass(CurRec.get(), Ref);
David Greenedb445972011-10-05 22:42:07 +00002355
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002356 // Set the value for NAME. We don't resolve references to it 'til later,
2357 // though, so that uses in nested multiclass names don't get
2358 // confused.
Craig Toppercfd81732016-01-04 03:15:08 +00002359 if (SetValue(CurRec.get(), Ref.RefRange.Start, "NAME", None, DefmPrefix,
Craig Topper1e23ed92016-01-04 03:05:14 +00002360 /*AllowSelfAssignment*/true)) {
Craig Topper85c07002015-04-30 05:54:22 +00002361 Error(DefmPrefixRange.Start, "Could not resolve " +
2362 CurRec->getNameInitAsString() + ":NAME to '" +
2363 DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002364 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002365 }
David Greene8bf0d722011-10-19 13:04:35 +00002366
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002367 // If the DefNameString didn't resolve, we probably have a reference to
2368 // NAME and need to replace it. We need to do at least this much greedily,
2369 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002370 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002371 RecordVal *DefNameRV = CurRec->getValue("NAME");
2372 CurRec->resolveReferencesTo(DefNameRV);
2373 }
2374
2375 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002376 // Now that we're at the top level, resolve all NAME references
2377 // in the resultant defs that weren't in the def names themselves.
2378 RecordVal *DefNameRV = CurRec->getValue("NAME");
2379 CurRec->resolveReferencesTo(DefNameRV);
2380
Hal Finkeld2497362015-05-21 04:32:56 +00002381 // Check if the name is a complex pattern.
2382 // If so, resolve it.
2383 DefName = CurRec->getNameInit();
2384 DefNameString = dyn_cast<StringInit>(DefName);
2385
2386 // OK the pattern is more complex than simply using NAME.
2387 // Let's use the heavy weaponery.
2388 if (!DefNameString) {
2389 ResolveMulticlassDefArgs(MC, CurRec.get(), DefmPrefixRange.Start,
2390 Lex.getLoc(), TArgs, TemplateVals,
2391 false/*Delete args*/);
2392 DefName = CurRec->getNameInit();
2393 DefNameString = dyn_cast<StringInit>(DefName);
2394
2395 if (!DefNameString)
2396 DefName = DefName->convertInitializerTo(StringRecTy::get());
2397
2398 // We ran out of options here...
2399 DefNameString = dyn_cast<StringInit>(DefName);
2400 if (!DefNameString) {
2401 PrintFatalError(CurRec->getLoc()[CurRec->getLoc().size() - 1],
2402 DefName->getAsUnquotedString() + " is not a string.");
2403 return nullptr;
2404 }
2405
2406 CurRec->setName(DefName);
2407 }
2408
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002409 // Now that NAME references are resolved and we're at the top level of
2410 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002411 // currently in a multiclass, it means this defm appears inside a
2412 // multiclass and its name won't be fully resolvable until we see
Hal Finkeld2497362015-05-21 04:32:56 +00002413 // the top-level defm. Therefore, we don't add this to the
2414 // RecordKeeper at this point. If we did we could get duplicate
David Greene8bf0d722011-10-19 13:04:35 +00002415 // defs as more than one probably refers to NAME or some other
2416 // common internal placeholder.
2417
2418 // Ensure redefinition doesn't happen.
2419 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002420 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002421 "' already defined, instantiating defm with subdef '" +
2422 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002423 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002424 }
2425
Craig Topper84138712014-11-29 05:31:10 +00002426 Record *CurRecSave = CurRec.get(); // Keep a copy before we release.
Craig Toppercdab2322014-11-29 05:52:51 +00002427 Records.addDef(std::move(CurRec));
Craig Topper84138712014-11-29 05:31:10 +00002428 return CurRecSave;
David Greene8bf0d722011-10-19 13:04:35 +00002429 }
2430
Craig Topper84138712014-11-29 05:31:10 +00002431 // FIXME This is bad but the ownership transfer to caller is pretty messy.
2432 // The unique_ptr in this function at least protects the exits above.
2433 return CurRec.release();
David Greenedb445972011-10-05 22:42:07 +00002434}
2435
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002436bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC, Record *CurRec,
2437 SMLoc DefmPrefixLoc, SMLoc SubClassLoc,
2438 ArrayRef<Init *> TArgs,
David Greenedb445972011-10-05 22:42:07 +00002439 std::vector<Init *> &TemplateVals,
2440 bool DeleteArgs) {
2441 // Loop over all of the template arguments, setting them to the specified
2442 // value or leaving them as the default if necessary.
2443 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2444 // Check if a value is specified for this temp-arg.
2445 if (i < TemplateVals.size()) {
2446 // Set it now.
Craig Toppercfd81732016-01-04 03:15:08 +00002447 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], None, TemplateVals[i]))
David Greenedb445972011-10-05 22:42:07 +00002448 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002449
David Greenedb445972011-10-05 22:42:07 +00002450 // Resolve it next.
2451 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2452
2453 if (DeleteArgs)
2454 // Now remove it.
2455 CurRec->removeValue(TArgs[i]);
Craig Toppera9642b42015-05-04 01:35:39 +00002456
David Greenedb445972011-10-05 22:42:07 +00002457 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Craig Topper85c07002015-04-30 05:54:22 +00002458 return Error(SubClassLoc, "value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00002459 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +00002460 ") of multiclassclass '" + MC.Rec.getNameInitAsString() +
2461 "'");
David Greenedb445972011-10-05 22:42:07 +00002462 }
2463 }
2464 return false;
2465}
2466
2467bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2468 Record *CurRec,
2469 Record *DefProto,
2470 SMLoc DefmPrefixLoc) {
2471 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002472 if (ApplyLetStack(CurRec))
2473 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002474
David Greenedb445972011-10-05 22:42:07 +00002475 // Don't create a top level definition for defm inside multiclasses,
2476 // instead, only update the prototypes and bind the template args
2477 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002478 if (!CurMultiClass)
2479 return false;
Craig Toppereb4d7c62015-04-29 04:43:36 +00002480 for (const auto &Proto : CurMultiClass->DefPrototypes)
2481 if (Proto->getNameInit() == CurRec->getNameInit())
Sean Silvacc951b22013-01-09 05:28:12 +00002482 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2483 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002484 CurMultiClass->DefPrototypes.push_back(std::unique_ptr<Record>(CurRec));
David Greenedb445972011-10-05 22:42:07 +00002485
Sean Silvacc951b22013-01-09 05:28:12 +00002486 // Copy the template arguments for the multiclass into the new def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002487 for (Init * TA : CurMultiClass->Rec.getTemplateArgs()) {
2488 const RecordVal *RV = CurMultiClass->Rec.getValue(TA);
Sean Silvacc951b22013-01-09 05:28:12 +00002489 assert(RV && "Template arg doesn't exist?");
2490 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002491 }
2492
2493 return false;
2494}
2495
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002496/// ParseDefm - Parse the instantiation of a multiclass.
2497///
2498/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2499///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002500bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002501 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002502 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002503 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002504
Craig Topperb21afc62013-01-07 05:09:33 +00002505 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002506 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002507 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002508
Jordan Rosef12e8a92013-01-10 18:50:11 +00002509 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002510 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002511 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002512
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002513 // Keep track of the new generated record definitions.
2514 std::vector<Record*> NewRecDefs;
2515
2516 // This record also inherits from a regular class (non-multiclass)?
2517 bool InheritFromClass = false;
2518
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002519 // eat the colon.
2520 Lex.Lex();
2521
Chris Lattner526c8cb2009-06-21 03:39:35 +00002522 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002523 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002524
2525 while (1) {
Craig Topper011817a2014-04-09 04:50:04 +00002526 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002527
2528 // To instantiate a multiclass, we need to first get the multiclass, then
2529 // instantiate each def contained in the multiclass with the SubClassRef
2530 // template parameters.
Craig Topper7adf2bf2014-12-11 05:25:30 +00002531 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper4ca40012014-11-30 01:20:17 +00002532 assert(MC && "Didn't lookup multiclass correctly?");
David Greeneaf8ee2c2011-07-29 22:43:06 +00002533 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002534
2535 // Verify that the correct number of template arguments were specified.
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002536 ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002537 if (TArgs.size() < TemplateVals.size())
2538 return Error(SubClassLoc,
2539 "more template args specified than multiclass expects");
2540
2541 // Loop over all the def's in the multiclass, instantiating each one.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002542 for (const std::unique_ptr<Record> &DefProto : MC->DefPrototypes) {
Hal Finkeld2497362015-05-21 04:32:56 +00002543 // The record name construction goes as follow:
2544 // - If the def name is a string, prepend the prefix.
2545 // - If the def name is a more complex pattern, use that pattern.
2546 // As a result, the record is instanciated before resolving
2547 // arguments, as it would make its name a string.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002548 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto.get(), DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002549 SMRange(DefmLoc,
Hal Finkeld2497362015-05-21 04:32:56 +00002550 DefmPrefixEndLoc),
2551 TArgs, TemplateVals);
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002552 if (!CurRec)
2553 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002554
Hal Finkeld2497362015-05-21 04:32:56 +00002555 // Now that the record is instanciated, we can resolve arguments.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002556 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002557 TArgs, TemplateVals, true/*Delete args*/))
2558 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002559
Craig Toppereb4d7c62015-04-29 04:43:36 +00002560 if (ResolveMulticlassDef(*MC, CurRec, DefProto.get(), DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002561 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002562
Adam Nemete5a07162014-09-16 17:14:13 +00002563 // Defs that can be used by other definitions should be fully resolved
2564 // before any use.
2565 if (DefProto->isResolveFirst() && !CurMultiClass) {
2566 CurRec->resolveReferences();
2567 CurRec->setResolveFirst(false);
2568 }
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002569 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002570 }
2571
David Greenedb445972011-10-05 22:42:07 +00002572
David Greenef00919a2009-04-22 22:17:51 +00002573 if (Lex.getCode() != tgtok::comma) break;
2574 Lex.Lex(); // eat ','.
2575
Craig Topper998a39a2013-08-20 04:22:09 +00002576 if (Lex.getCode() != tgtok::Id)
2577 return TokError("expected identifier");
2578
David Greenef00919a2009-04-22 22:17:51 +00002579 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002580
2581 // A defm can inherit from regular classes (non-multiclass) as
2582 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002583 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002584
2585 if (InheritFromClass)
2586 break;
2587
Craig Topper011817a2014-04-09 04:50:04 +00002588 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002589 }
2590
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002591 if (InheritFromClass) {
2592 // Process all the classes to inherit as if they were part of a
2593 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002594 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002595 while (1) {
2596 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002597 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002598
2599 // Get the expanded definition prototypes and teach them about
2600 // the record values the current class to inherit has
Craig Toppereb4d7c62015-04-29 04:43:36 +00002601 for (Record *CurRec : NewRecDefs) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002602 // Add it.
2603 if (AddSubClass(CurRec, SubClass))
2604 return true;
2605
Sean Silvacb1a75e2013-01-09 04:49:14 +00002606 if (ApplyLetStack(CurRec))
2607 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002608 }
2609
2610 if (Lex.getCode() != tgtok::comma) break;
2611 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002612 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002613 }
2614 }
2615
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002616 if (!CurMultiClass)
Craig Toppereb4d7c62015-04-29 04:43:36 +00002617 for (Record *CurRec : NewRecDefs)
David Greene50c09122011-08-10 18:27:46 +00002618 // See Record::setName(). This resolve step will see any new
2619 // name for the def that might have been created when resolving
2620 // inheritance, values and arguments above.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002621 CurRec->resolveReferences();
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002622
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002623 if (Lex.getCode() != tgtok::semi)
2624 return TokError("expected ';' at end of defm");
2625 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002626
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002627 return false;
2628}
2629
2630/// ParseObject
2631/// Object ::= ClassInst
2632/// Object ::= DefInst
2633/// Object ::= MultiClassInst
2634/// Object ::= DefMInst
2635/// Object ::= LETCommand '{' ObjectList '}'
2636/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002637bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002638 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002639 default:
2640 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002641 case tgtok::Let: return ParseTopLevelLet(MC);
2642 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002643 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002644 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002645 case tgtok::Class: return ParseClass();
2646 case tgtok::MultiClass: return ParseMultiClass();
2647 }
2648}
2649
2650/// ParseObjectList
2651/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002652bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002653 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002654 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002655 return true;
2656 }
2657 return false;
2658}
2659
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002660bool TGParser::ParseFile() {
2661 Lex.Lex(); // Prime the lexer.
2662 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002663
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002664 // If we have unread input at the end of the file, report it.
2665 if (Lex.getCode() == tgtok::Eof)
2666 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002667
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002668 return TokError("Unexpected input at top level");
2669}
2670