blob: 58bb743de772a32988cb65f6512df1d10ce0e2ac [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"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000015#include "llvm/ADT/None.h"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000016#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/SmallVector.h"
Chris Lattnerf4127dd2007-11-22 20:49:04 +000018#include "llvm/ADT/StringExtras.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000019#include "llvm/Support/Casting.h"
20#include "llvm/Support/Compiler.h"
21#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/TableGen/Record.h"
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000024#include <algorithm>
Eugene Zelenko33d7b762016-08-23 17:14:32 +000025#include <cassert>
26#include <cstdint>
27
Chris Lattnerf4127dd2007-11-22 20:49:04 +000028using namespace llvm;
29
30//===----------------------------------------------------------------------===//
31// Support Code for the Semantic Actions.
32//===----------------------------------------------------------------------===//
33
34namespace llvm {
Eugene Zelenko33d7b762016-08-23 17:14:32 +000035
Chris Lattnerf4127dd2007-11-22 20:49:04 +000036struct SubClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000037 SMRange RefRange;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000038 Record *Rec;
Matthias Braunc66e7552016-12-05 06:41:54 +000039 SmallVector<Init*, 4> TemplateArgs;
Eugene Zelenko33d7b762016-08-23 17:14:32 +000040
Craig Topper011817a2014-04-09 04:50:04 +000041 SubClassReference() : Rec(nullptr) {}
David Greene7049e792009-04-24 16:55:41 +000042
Craig Topper011817a2014-04-09 04:50:04 +000043 bool isInvalid() const { return Rec == nullptr; }
Chris Lattnerf4127dd2007-11-22 20:49:04 +000044};
David Greene753ed8f2009-04-22 16:42:54 +000045
46struct SubMultiClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000047 SMRange RefRange;
David Greene753ed8f2009-04-22 16:42:54 +000048 MultiClass *MC;
Matthias Braunc66e7552016-12-05 06:41:54 +000049 SmallVector<Init*, 4> TemplateArgs;
Eugene Zelenko33d7b762016-08-23 17:14:32 +000050
Craig Topper011817a2014-04-09 04:50:04 +000051 SubMultiClassReference() : MC(nullptr) {}
Bob Wilson3d948162009-04-28 19:41:44 +000052
Craig Topper011817a2014-04-09 04:50:04 +000053 bool isInvalid() const { return MC == nullptr; }
David Greene7049e792009-04-24 16:55:41 +000054 void dump() const;
David Greene753ed8f2009-04-22 16:42:54 +000055};
David Greene7049e792009-04-24 16:55:41 +000056
Aaron Ballman615eb472017-10-15 14:32:27 +000057#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +000058LLVM_DUMP_METHOD void SubMultiClassReference::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000059 errs() << "Multiclass:\n";
Bob Wilson7248f862009-11-22 04:24:42 +000060
David Greene7049e792009-04-24 16:55:41 +000061 MC->dump();
Bob Wilson7248f862009-11-22 04:24:42 +000062
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000063 errs() << "Template args:\n";
Craig Toppera9642b42015-05-04 01:35:39 +000064 for (Init *TA : TemplateArgs)
Craig Toppereb4d7c62015-04-29 04:43:36 +000065 TA->dump();
David Greene7049e792009-04-24 16:55:41 +000066}
Matthias Braun25bcaba2017-01-28 02:47:46 +000067#endif
David Greene7049e792009-04-24 16:55:41 +000068
Chris Lattnerf4127dd2007-11-22 20:49:04 +000069} // end namespace llvm
70
Chris Lattner526c8cb2009-06-21 03:39:35 +000071bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Craig Topper011817a2014-04-09 04:50:04 +000072 if (!CurRec)
Chris Lattnerf4127dd2007-11-22 20:49:04 +000073 CurRec = &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +000074
Jakob Stoklund Olesen9d1c5ee2012-01-13 03:16:35 +000075 if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000076 // The value already exists in the class, treat this as a set.
77 if (ERV->setValue(RV.getValue()))
78 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
79 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson7248f862009-11-22 04:24:42 +000080 "previous definition of type '" +
Chris Lattnerf4127dd2007-11-22 20:49:04 +000081 ERV->getType()->getAsString() + "'");
82 } else {
83 CurRec->addValue(RV);
84 }
85 return false;
86}
87
88/// SetValue -
89/// Return true on error, false on success.
David Greene3ca42122011-10-19 13:02:39 +000090bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
Craig Toppercfd81732016-01-04 03:15:08 +000091 ArrayRef<unsigned> BitList, Init *V,
Craig Topper1e23ed92016-01-04 03:05:14 +000092 bool AllowSelfAssignment) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000093 if (!V) return false;
94
Craig Topper011817a2014-04-09 04:50:04 +000095 if (!CurRec) CurRec = &CurMultiClass->Rec;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000096
97 RecordVal *RV = CurRec->getValue(ValName);
Craig Topper011817a2014-04-09 04:50:04 +000098 if (!RV)
Craig Topper85c07002015-04-30 05:54:22 +000099 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
100 "' unknown!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000101
102 // Do not allow assignments like 'X = X'. This will just cause infinite loops
103 // in the resolution machinery.
104 if (BitList.empty())
Sean Silvafb509ed2012-10-10 20:24:43 +0000105 if (VarInit *VI = dyn_cast<VarInit>(V))
Craig Topper1e23ed92016-01-04 03:05:14 +0000106 if (VI->getNameInit() == ValName && !AllowSelfAssignment)
Nicolai Haehnlef19083d2018-02-22 15:26:21 +0000107 return Error(Loc, "Recursion / self-assignment forbidden");
Bob Wilson7248f862009-11-22 04:24:42 +0000108
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000109 // If we are assigning to a subset of the bits in the value... then we must be
110 // assigning to a field of BitsRecTy, which must have a BitsInit
111 // initializer.
112 //
113 if (!BitList.empty()) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000114 BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue());
Craig Topper011817a2014-04-09 04:50:04 +0000115 if (!CurVal)
Craig Topper85c07002015-04-30 05:54:22 +0000116 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
117 "' is not a bits type");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000118
119 // Convert the incoming value to a bits type of the appropriate size...
David Greeneaf8ee2c2011-07-29 22:43:06 +0000120 Init *BI = V->convertInitializerTo(BitsRecTy::get(BitList.size()));
Craig Toppera9642b42015-05-04 01:35:39 +0000121 if (!BI)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000122 return Error(Loc, "Initializer is not compatible with bit range");
Bob Wilson7248f862009-11-22 04:24:42 +0000123
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000124 // We should have a BitsInit type now.
Craig Toppered5a9502015-04-29 07:13:05 +0000125 BitsInit *BInit = cast<BitsInit>(BI);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000126
David Greeneaf8ee2c2011-07-29 22:43:06 +0000127 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000128
129 // Loop over bits, assigning values as appropriate.
130 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
131 unsigned Bit = BitList[i];
David Greeneb3da8122011-07-29 19:07:00 +0000132 if (NewBits[Bit])
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000133 return Error(Loc, "Cannot set bit #" + Twine(Bit) + " of value '" +
David Greene3ca42122011-10-19 13:02:39 +0000134 ValName->getAsUnquotedString() + "' more than once");
David Greeneb3da8122011-07-29 19:07:00 +0000135 NewBits[Bit] = BInit->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000136 }
137
138 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
Craig Topper011817a2014-04-09 04:50:04 +0000139 if (!NewBits[i])
David Greeneb3da8122011-07-29 19:07:00 +0000140 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000141
David Greenee32ebf22011-07-29 19:07:07 +0000142 V = BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000143 }
144
Pete Cooper040c6a62014-07-31 01:43:57 +0000145 if (RV->setValue(V)) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000146 std::string InitType;
Craig Toppera9642b42015-05-04 01:35:39 +0000147 if (BitsInit *BI = dyn_cast<BitsInit>(V))
Pete Cooper040c6a62014-07-31 01:43:57 +0000148 InitType = (Twine("' of type bit initializer with length ") +
149 Twine(BI->getNumBits())).str();
Nicolai Haehnlef19083d2018-02-22 15:26:21 +0000150 else if (TypedInit *TI = dyn_cast<TypedInit>(V))
151 InitType = (Twine("' of type '") + TI->getType()->getAsString()).str();
Craig Topper85c07002015-04-30 05:54:22 +0000152 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
Nicolai Haehnlef19083d2018-02-22 15:26:21 +0000153 "' of type '" + RV->getType()->getAsString() +
154 "' is incompatible with initializer '" +
155 V->getAsString() + InitType + "'");
Pete Cooper040c6a62014-07-31 01:43:57 +0000156 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000157 return false;
158}
159
160/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
161/// args as SubClass's template arguments.
Cedric Venetd1e179d2009-02-14 16:06:42 +0000162bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000163 Record *SC = SubClass.Rec;
164 // Add all of the values in the subclass into the current class.
Craig Topper8eb764c2015-06-02 06:19:28 +0000165 for (const RecordVal &Val : SC->getValues())
166 if (AddValue(CurRec, SubClass.RefRange.Start, Val))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000167 return true;
168
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000169 ArrayRef<Init *> TArgs = SC->getTemplateArgs();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000170
171 // Ensure that an appropriate number of template arguments are specified.
172 if (TArgs.size() < SubClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000173 return Error(SubClass.RefRange.Start,
174 "More template args specified than expected");
Bob Wilson7248f862009-11-22 04:24:42 +0000175
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000176 // Loop over all of the template arguments, setting them to the specified
177 // value or leaving them as the default if necessary.
Nicolai Haehnleb0cf9e92018-03-05 15:21:11 +0000178 MapResolver R(CurRec);
179
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000180 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
181 if (i < SubClass.TemplateArgs.size()) {
182 // If a value is specified for this template arg, set it now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000183 if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000184 None, SubClass.TemplateArgs[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000185 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000186 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000187 return Error(SubClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000188 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000189 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000190 ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000191 }
Nicolai Haehnleb0cf9e92018-03-05 15:21:11 +0000192
193 R.set(TArgs[i], CurRec->getValue(TArgs[i])->getValue());
194
195 CurRec->removeValue(TArgs[i]);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000196 }
197
Nicolai Haehnleb0cf9e92018-03-05 15:21:11 +0000198 CurRec->resolveReferences(R);
199
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000200 // Since everything went well, we can now set the "superclass" list for the
201 // current record.
Craig Topper0e41d0b2016-01-18 19:52:37 +0000202 ArrayRef<std::pair<Record *, SMRange>> SCs = SC->getSuperClasses();
203 for (const auto &SCPair : SCs) {
204 if (CurRec->isSubClassOf(SCPair.first))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000205 return Error(SubClass.RefRange.Start,
Craig Topper0e41d0b2016-01-18 19:52:37 +0000206 "Already subclass of '" + SCPair.first->getName() + "'!\n");
207 CurRec->addSuperClass(SCPair.first, SCPair.second);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000208 }
Nicolai Haehnlec10570f2018-02-23 11:31:49 +0000209
210 if (CurRec->isSubClassOf(SC))
211 return Error(SubClass.RefRange.Start,
212 "Already subclass of '" + SC->getName() + "'!\n");
213 CurRec->addSuperClass(SC, SubClass.RefRange);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000214 return false;
215}
216
David Greene753ed8f2009-04-22 16:42:54 +0000217/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilsonf71e6562009-04-30 18:26:19 +0000218/// CurMC, resolving its template args as SubMultiClass's
David Greene753ed8f2009-04-22 16:42:54 +0000219/// template arguments.
Bob Wilsonf71e6562009-04-30 18:26:19 +0000220bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson92ab8202009-04-30 17:46:20 +0000221 SubMultiClassReference &SubMultiClass) {
David Greene753ed8f2009-04-22 16:42:54 +0000222 MultiClass *SMC = SubMultiClass.MC;
Bob Wilsonf71e6562009-04-30 18:26:19 +0000223 Record *CurRec = &CurMC->Rec;
David Greene753ed8f2009-04-22 16:42:54 +0000224
David Greene753ed8f2009-04-22 16:42:54 +0000225 // Add all of the values in the subclass into the current class.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000226 for (const auto &SMCVal : SMC->Rec.getValues())
227 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000228 return true;
229
Craig Toppera4ea4b02014-11-30 00:24:32 +0000230 unsigned newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000231
David Greene753ed8f2009-04-22 16:42:54 +0000232 // Add all of the defs in the subclass into the current multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000233 for (const std::unique_ptr<Record> &R : SMC->DefPrototypes) {
David Greene753ed8f2009-04-22 16:42:54 +0000234 // Clone the def and add it to the current multiclass
Craig Toppereb4d7c62015-04-29 04:43:36 +0000235 auto NewDef = make_unique<Record>(*R);
David Greene753ed8f2009-04-22 16:42:54 +0000236
237 // Add all of the values in the superclass into the current def.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000238 for (const auto &MCVal : CurRec->getValues())
239 if (AddValue(NewDef.get(), SubMultiClass.RefRange.Start, MCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000240 return true;
241
Craig Topperc3504c42014-12-11 05:25:33 +0000242 CurMC->DefPrototypes.push_back(std::move(NewDef));
David Greene753ed8f2009-04-22 16:42:54 +0000243 }
Bob Wilson3d948162009-04-28 19:41:44 +0000244
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000245 ArrayRef<Init *> SMCTArgs = SMC->Rec.getTemplateArgs();
David Greene753ed8f2009-04-22 16:42:54 +0000246
David Greene7049e792009-04-24 16:55:41 +0000247 // Ensure that an appropriate number of template arguments are
248 // specified.
David Greene753ed8f2009-04-22 16:42:54 +0000249 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000250 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000251 "More template args specified than expected");
Bob Wilson3d948162009-04-28 19:41:44 +0000252
David Greene753ed8f2009-04-22 16:42:54 +0000253 // Loop over all of the template arguments, setting them to the specified
254 // value or leaving them as the default if necessary.
Nicolai Haehnle1faf8682018-03-05 15:21:15 +0000255 MapResolver CurRecResolver(CurRec);
256
David Greene753ed8f2009-04-22 16:42:54 +0000257 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
258 if (i < SubMultiClass.TemplateArgs.size()) {
David Greene7049e792009-04-24 16:55:41 +0000259 // If a value is specified for this template arg, set it in the
260 // superclass now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000261 if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000262 None, SubMultiClass.TemplateArgs[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000263 return true;
264
David Greene7049e792009-04-24 16:55:41 +0000265 // If a value is specified for this template arg, set it in the
266 // new defs now.
Craig Topperc3504c42014-12-11 05:25:33 +0000267 for (const auto &Def :
268 makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
269 if (SetValue(Def.get(), SubMultiClass.RefRange.Start, SMCTArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000270 None, SubMultiClass.TemplateArgs[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000271 return true;
David Greene753ed8f2009-04-22 16:42:54 +0000272 }
273 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000274 return Error(SubMultiClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000275 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000276 Twine(i) + " (" + SMCTArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000277 ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000278 }
Nicolai Haehnle1faf8682018-03-05 15:21:15 +0000279
280 CurRecResolver.set(SMCTArgs[i], CurRec->getValue(SMCTArgs[i])->getValue());
281
282 CurRec->removeValue(SMCTArgs[i]);
283 }
284
285 CurRec->resolveReferences(CurRecResolver);
286
287 for (const auto &Def :
288 makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
289 MapResolver R(Def.get());
290
291 for (Init *SMCTArg : SMCTArgs) {
292 R.set(SMCTArg, Def->getValue(SMCTArg)->getValue());
293 Def->removeValue(SMCTArg);
294 }
295
296 Def->resolveReferences(R);
David Greene753ed8f2009-04-22 16:42:54 +0000297 }
298
299 return false;
300}
301
David Greenefb927af2012-02-22 16:09:41 +0000302/// ProcessForeachDefs - Given a record, apply all of the variable
303/// values in all surrounding foreach loops, creating new records for
304/// each combination of values.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000305bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
306 if (Loops.empty())
307 return false;
308
David Greenefb927af2012-02-22 16:09:41 +0000309 // We want to instantiate a new copy of CurRec for each combination
310 // of nested loop iterator values. We don't want top instantiate
311 // any copies until we have values for each loop iterator.
312 IterSet IterVals;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000313 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000314}
315
316/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
317/// apply each of the variable values in this loop and then process
318/// subloops.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000319bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
320 // Recursively build a tuple of iterator values.
321 if (IterVals.size() != Loops.size()) {
322 assert(IterVals.size() < Loops.size());
323 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silvafb509ed2012-10-10 20:24:43 +0000324 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Craig Topper011817a2014-04-09 04:50:04 +0000325 if (!List) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000326 Error(Loc, "Loop list is not a list");
327 return true;
328 }
David Greenefb927af2012-02-22 16:09:41 +0000329
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000330 // Process each value.
Craig Topper664f6a02015-06-02 04:15:57 +0000331 for (unsigned i = 0; i < List->size(); ++i) {
Nicolai Haehnle0b0eaf72018-03-05 15:20:51 +0000332 RecordResolver R(*CurRec);
333 Init *ItemVal = List->getElement(i)->resolveReferences(R);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000334 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
335 if (ProcessForeachDefs(CurRec, Loc, IterVals))
336 return true;
337 IterVals.pop_back();
338 }
339 return false;
340 }
341
342 // This is the bottom of the recursion. We have all of the iterator values
343 // for this point in the iteration space. Instantiate a new record to
344 // reflect this combination of values.
Craig Topper84138712014-11-29 05:31:10 +0000345 auto IterRec = make_unique<Record>(*CurRec);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000346
347 // Set the iterator values now.
Craig Topper8eb764c2015-06-02 06:19:28 +0000348 for (IterRecord &IR : IterVals) {
349 VarInit *IterVar = IR.IterVar;
350 TypedInit *IVal = dyn_cast<TypedInit>(IR.IterValue);
Craig Topper84138712014-11-29 05:31:10 +0000351 if (!IVal)
352 return Error(Loc, "foreach iterator value is untyped");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000353
Craig Topperf4412262017-06-01 06:56:16 +0000354 IterRec->addValue(RecordVal(IterVar->getNameInit(), IVal->getType(), false));
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000355
Matthias Braun215ff842016-12-05 07:35:13 +0000356 if (SetValue(IterRec.get(), Loc, IterVar->getNameInit(), None, IVal))
Craig Topper84138712014-11-29 05:31:10 +0000357 return Error(Loc, "when instantiating this def");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000358
359 // Resolve it next.
Matthias Braun215ff842016-12-05 07:35:13 +0000360 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getNameInit()));
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000361
362 // Remove it.
Matthias Braun215ff842016-12-05 07:35:13 +0000363 IterRec->removeValue(IterVar->getNameInit());
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000364 }
365
366 if (Records.getDef(IterRec->getNameInitAsString())) {
Artyom Skrobov8b985322014-06-10 12:41:14 +0000367 // If this record is anonymous, it's no problem, just generate a new name
Craig Topper84138712014-11-29 05:31:10 +0000368 if (!IterRec->isAnonymous())
369 return Error(Loc, "def already exists: " +IterRec->getNameInitAsString());
370
371 IterRec->setName(GetNewAnonymousName());
David Greenefb927af2012-02-22 16:09:41 +0000372 }
373
Craig Topper84138712014-11-29 05:31:10 +0000374 Record *IterRecSave = IterRec.get(); // Keep a copy before release.
Craig Toppercdab2322014-11-29 05:52:51 +0000375 Records.addDef(std::move(IterRec));
Craig Topper84138712014-11-29 05:31:10 +0000376 IterRecSave->resolveReferences();
David Greenefb927af2012-02-22 16:09:41 +0000377 return false;
378}
379
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000380//===----------------------------------------------------------------------===//
381// Parser Code
382//===----------------------------------------------------------------------===//
383
384/// isObjectStart - Return true if this is a valid first token for an Object.
385static bool isObjectStart(tgtok::TokKind K) {
386 return K == tgtok::Class || K == tgtok::Def ||
David Greenefb927af2012-02-22 16:09:41 +0000387 K == tgtok::Defm || K == tgtok::Let ||
388 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000389}
390
Alp Tokerce91fe52013-12-21 18:51:00 +0000391/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
392/// an identifier.
Craig Topperf4412262017-06-01 06:56:16 +0000393Init *TGParser::GetNewAnonymousName() {
394 return StringInit::get("anonymous_" + utostr(AnonCounter++));
Chris Lattner7538ed82010-10-05 22:51:56 +0000395}
396
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000397/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000398/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000399/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000400/// ObjectName ::= /*empty*/
401///
David Greene2affd672011-10-19 13:04:29 +0000402Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
403 switch (Lex.getCode()) {
404 case tgtok::colon:
405 case tgtok::semi:
406 case tgtok::l_brace:
407 // These are all of the tokens that can begin an object body.
408 // Some of these can also begin values but we disallow those cases
409 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000410 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000411 default:
412 break;
413 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000414
Craig Topper011817a2014-04-09 04:50:04 +0000415 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000416 if (CurMultiClass)
417 CurRec = &CurMultiClass->Rec;
418
Craig Topper011817a2014-04-09 04:50:04 +0000419 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000420 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000421 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000422 if (!CurRecName) {
423 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000424 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000425 }
426 Type = CurRecName->getType();
427 }
428
429 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000430}
431
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000432/// ParseClassID - Parse and resolve a reference to a class name. This returns
433/// null on error.
434///
435/// ClassID ::= ID
436///
437Record *TGParser::ParseClassID() {
438 if (Lex.getCode() != tgtok::Id) {
439 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000440 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000441 }
Bob Wilson7248f862009-11-22 04:24:42 +0000442
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000443 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000444 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000445 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000446
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000447 Lex.Lex();
448 return Result;
449}
450
Bob Wilson3d948162009-04-28 19:41:44 +0000451/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
452/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000453///
454/// MultiClassID ::= ID
455///
456MultiClass *TGParser::ParseMultiClassID() {
457 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000458 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000459 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000460 }
Bob Wilson3d948162009-04-28 19:41:44 +0000461
Craig Topper7adf2bf2014-12-11 05:25:30 +0000462 MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
Craig Topper4ca40012014-11-30 01:20:17 +0000463 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000464 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000465
David Greene753ed8f2009-04-22 16:42:54 +0000466 Lex.Lex();
Craig Topper4ca40012014-11-30 01:20:17 +0000467 return Result;
David Greene753ed8f2009-04-22 16:42:54 +0000468}
469
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000470/// ParseSubClassReference - Parse a reference to a subclass or to a templated
471/// subclass. This returns a SubClassRefTy with a null Record* on error.
472///
473/// SubClassRef ::= ClassID
474/// SubClassRef ::= ClassID '<' ValueList '>'
475///
476SubClassReference TGParser::
477ParseSubClassReference(Record *CurRec, bool isDefm) {
478 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000479 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000480
Sean Silva0657b402013-01-09 02:17:14 +0000481 if (isDefm) {
482 if (MultiClass *MC = ParseMultiClassID())
483 Result.Rec = &MC->Rec;
484 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000485 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000486 }
Craig Topper011817a2014-04-09 04:50:04 +0000487 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000488
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000489 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000490 if (Lex.getCode() != tgtok::less) {
491 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000492 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000493 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000494 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000495
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000496 if (Lex.getCode() == tgtok::greater) {
497 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000498 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000499 return Result;
500 }
Bob Wilson7248f862009-11-22 04:24:42 +0000501
Matthias Braunc66e7552016-12-05 06:41:54 +0000502 ParseValueList(Result.TemplateArgs, CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000503 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000504 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000505 return Result;
506 }
Bob Wilson7248f862009-11-22 04:24:42 +0000507
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000508 if (Lex.getCode() != tgtok::greater) {
509 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000510 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000511 return Result;
512 }
513 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000514 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000515
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000516 return Result;
517}
518
Bob Wilson3d948162009-04-28 19:41:44 +0000519/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
520/// templated submulticlass. This returns a SubMultiClassRefTy with a null
521/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000522///
523/// SubMultiClassRef ::= MultiClassID
524/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
525///
526SubMultiClassReference TGParser::
527ParseSubMultiClassReference(MultiClass *CurMC) {
528 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000529 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000530
David Greene753ed8f2009-04-22 16:42:54 +0000531 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000532 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000533
David Greene753ed8f2009-04-22 16:42:54 +0000534 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000535 if (Lex.getCode() != tgtok::less) {
536 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000537 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000538 }
David Greene753ed8f2009-04-22 16:42:54 +0000539 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000540
David Greene753ed8f2009-04-22 16:42:54 +0000541 if (Lex.getCode() == tgtok::greater) {
542 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000543 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000544 return Result;
545 }
Bob Wilson3d948162009-04-28 19:41:44 +0000546
Matthias Braunc66e7552016-12-05 06:41:54 +0000547 ParseValueList(Result.TemplateArgs, &CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000548 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000549 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000550 return Result;
551 }
Bob Wilson3d948162009-04-28 19:41:44 +0000552
David Greene753ed8f2009-04-22 16:42:54 +0000553 if (Lex.getCode() != tgtok::greater) {
554 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000555 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000556 return Result;
557 }
558 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000559 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000560
561 return Result;
562}
563
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000564/// ParseRangePiece - Parse a bit/value range.
565/// RangePiece ::= INTVAL
566/// RangePiece ::= INTVAL '-' INTVAL
567/// RangePiece ::= INTVAL INTVAL
Matthias Braunc66e7552016-12-05 06:41:54 +0000568bool TGParser::ParseRangePiece(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000569 if (Lex.getCode() != tgtok::IntVal) {
570 TokError("expected integer or bitrange");
571 return true;
572 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000573 int64_t Start = Lex.getCurIntVal();
574 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000575
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000576 if (Start < 0)
577 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000578
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000579 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000580 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000581 Ranges.push_back(Start);
582 return false;
583 case tgtok::minus:
584 if (Lex.Lex() != tgtok::IntVal) {
585 TokError("expected integer value as end of range");
586 return true;
587 }
588 End = Lex.getCurIntVal();
589 break;
590 case tgtok::IntVal:
591 End = -Lex.getCurIntVal();
592 break;
593 }
Bob Wilson7248f862009-11-22 04:24:42 +0000594 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000595 return TokError("invalid range, cannot be negative");
596 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000597
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000598 // Add to the range.
Craig Toppera9642b42015-05-04 01:35:39 +0000599 if (Start < End)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000600 for (; Start <= End; ++Start)
601 Ranges.push_back(Start);
Craig Toppera9642b42015-05-04 01:35:39 +0000602 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000603 for (; Start >= End; --Start)
604 Ranges.push_back(Start);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000605 return false;
606}
607
608/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
609///
610/// RangeList ::= RangePiece (',' RangePiece)*
611///
Matthias Braunc66e7552016-12-05 06:41:54 +0000612void TGParser::ParseRangeList(SmallVectorImpl<unsigned> &Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000613 // Parse the first piece.
Matthias Braunc66e7552016-12-05 06:41:54 +0000614 if (ParseRangePiece(Result)) {
615 Result.clear();
616 return;
617 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000618 while (Lex.getCode() == tgtok::comma) {
619 Lex.Lex(); // Eat the comma.
620
621 // Parse the next range piece.
Matthias Braunc66e7552016-12-05 06:41:54 +0000622 if (ParseRangePiece(Result)) {
623 Result.clear();
624 return;
625 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000626 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000627}
628
629/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
630/// OptionalRangeList ::= '<' RangeList '>'
631/// OptionalRangeList ::= /*empty*/
Matthias Braunc66e7552016-12-05 06:41:54 +0000632bool TGParser::ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000633 if (Lex.getCode() != tgtok::less)
634 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000635
Chris Lattner526c8cb2009-06-21 03:39:35 +0000636 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000637 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000638
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000639 // Parse the range list.
Matthias Braunc66e7552016-12-05 06:41:54 +0000640 ParseRangeList(Ranges);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000641 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000642
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000643 if (Lex.getCode() != tgtok::greater) {
644 TokError("expected '>' at end of range list");
645 return Error(StartLoc, "to match this '<'");
646 }
647 Lex.Lex(); // eat the '>'.
648 return false;
649}
650
651/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
652/// OptionalBitList ::= '{' RangeList '}'
653/// OptionalBitList ::= /*empty*/
Matthias Braunc66e7552016-12-05 06:41:54 +0000654bool TGParser::ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000655 if (Lex.getCode() != tgtok::l_brace)
656 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000657
Chris Lattner526c8cb2009-06-21 03:39:35 +0000658 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000659 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000660
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000661 // Parse the range list.
Matthias Braunc66e7552016-12-05 06:41:54 +0000662 ParseRangeList(Ranges);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000663 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000664
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000665 if (Lex.getCode() != tgtok::r_brace) {
666 TokError("expected '}' at end of bit list");
667 return Error(StartLoc, "to match this '{'");
668 }
669 Lex.Lex(); // eat the '}'.
670 return false;
671}
672
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000673/// ParseType - Parse and return a tblgen type. This returns null on error.
674///
675/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000676/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000677/// Type ::= BIT // bit type
678/// Type ::= BITS '<' INTVAL '>' // bits<x> type
679/// Type ::= INT // int type
680/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000681/// Type ::= DAG // dag type
682/// Type ::= ClassID // Record Type
683///
684RecTy *TGParser::ParseType() {
685 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000686 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000687 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Tim Northover88403d72016-07-05 21:22:55 +0000688 case tgtok::Code: Lex.Lex(); return CodeRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000689 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
690 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000691 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000692 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000693 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000694 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000695 case tgtok::Bits: {
696 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
697 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000698 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000699 }
700 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
701 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000702 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000703 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000704 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000705 if (Lex.Lex() != tgtok::greater) { // Eat count.
706 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000707 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000708 }
709 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000710 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000711 }
712 case tgtok::List: {
713 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
714 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000715 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000716 }
717 Lex.Lex(); // Eat '<'
718 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000719 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000720
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000721 if (Lex.getCode() != tgtok::greater) {
722 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000723 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000724 }
725 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000726 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000727 }
Bob Wilson7248f862009-11-22 04:24:42 +0000728 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000729}
730
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000731/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
732/// has already been read.
Matthias Braun215ff842016-12-05 07:35:13 +0000733Init *TGParser::ParseIDValue(Record *CurRec, StringInit *Name, SMLoc NameLoc,
David Greened4263a62011-10-19 13:04:20 +0000734 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000735 if (CurRec) {
736 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000737 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000738
Matthias Braun215ff842016-12-05 07:35:13 +0000739 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
David Greenedb10e692011-10-19 13:02:42 +0000740
David Greene47a665e2011-10-05 22:42:54 +0000741 if (CurMultiClass)
Matthias Braun215ff842016-12-05 07:35:13 +0000742 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
David Greenedb10e692011-10-19 13:02:42 +0000743 "::");
David Greene47a665e2011-10-05 22:42:54 +0000744
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000745 if (CurRec->isTemplateArg(TemplateArgName)) {
746 const RecordVal *RV = CurRec->getValue(TemplateArgName);
747 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000748 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000749 }
Matthias Braun215ff842016-12-05 07:35:13 +0000750 }
Bob Wilson7248f862009-11-22 04:24:42 +0000751
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000752 if (CurMultiClass) {
Nicolai Haehnle3c80e4c2018-03-05 14:01:38 +0000753 if (Name->getValue() == "NAME")
754 return VarInit::get(Name, StringRecTy::get());
755
Matthias Braun215ff842016-12-05 07:35:13 +0000756 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name, "::");
David Greenedb10e692011-10-19 13:02:42 +0000757
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000758 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
759 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
760 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000761 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000762 }
763 }
Bob Wilson7248f862009-11-22 04:24:42 +0000764
David Greenefb927af2012-02-22 16:09:41 +0000765 // If this is in a foreach loop, make sure it's not a loop iterator
Craig Toppereb4d7c62015-04-29 04:43:36 +0000766 for (const auto &L : Loops) {
767 VarInit *IterVar = dyn_cast<VarInit>(L.IterVar);
Matthias Braun215ff842016-12-05 07:35:13 +0000768 if (IterVar && IterVar->getNameInit() == Name)
David Greenefb927af2012-02-22 16:09:41 +0000769 return IterVar;
770 }
771
David Greene232bd602011-10-19 13:04:21 +0000772 if (Mode == ParseNameMode)
Matthias Braun215ff842016-12-05 07:35:13 +0000773 return Name;
David Greene232bd602011-10-19 13:04:21 +0000774
Matthias Braun215ff842016-12-05 07:35:13 +0000775 if (Record *D = Records.getDef(Name->getValue()))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000776 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000777
David Greene232bd602011-10-19 13:04:21 +0000778 if (Mode == ParseValueMode) {
Matthias Braun215ff842016-12-05 07:35:13 +0000779 Error(NameLoc, "Variable not defined: '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000780 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000781 }
Craig Toppera9642b42015-05-04 01:35:39 +0000782
Matthias Braun215ff842016-12-05 07:35:13 +0000783 return Name;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000784}
785
David Greene5d0c0512009-05-14 20:54:48 +0000786/// ParseOperation - Parse an operator. This returns null on error.
787///
788/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
789///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000790Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000791 switch (Lex.getCode()) {
792 default:
793 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000794 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000795 case tgtok::XHead:
796 case tgtok::XTail:
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000797 case tgtok::XSize:
David Greene2f7cf7f2011-01-07 17:05:37 +0000798 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000799 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
800 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000801 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000802
David Greenee8f3b272009-05-14 21:22:49 +0000803 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000804 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000805 case tgtok::XCast:
806 Lex.Lex(); // eat the operation
807 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000808
David Greenee8f3b272009-05-14 21:22:49 +0000809 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000810
Craig Topper011817a2014-04-09 04:50:04 +0000811 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000812 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000813 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000814 }
David Greene5d0c0512009-05-14 20:54:48 +0000815
David Greenee8f3b272009-05-14 21:22:49 +0000816 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000817 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000818 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000819 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000820 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000821 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000822 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000823 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000824 break;
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000825 case tgtok::XSize:
826 Lex.Lex();
827 Code = UnOpInit::SIZE;
828 Type = IntRecTy::get();
829 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000830 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000831 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000832 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000833 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000834 break;
David Greenee8f3b272009-05-14 21:22:49 +0000835 }
836 if (Lex.getCode() != tgtok::l_paren) {
837 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000838 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000839 }
840 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000841
David Greeneaf8ee2c2011-07-29 22:43:06 +0000842 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000843 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000844
Craig Topper85c07002015-04-30 05:54:22 +0000845 if (Code == UnOpInit::HEAD ||
846 Code == UnOpInit::TAIL ||
847 Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000848 ListInit *LHSl = dyn_cast<ListInit>(LHS);
849 StringInit *LHSs = dyn_cast<StringInit>(LHS);
850 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000851 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000852 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000853 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000854 }
855 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000856 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
857 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000858 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000859 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000860 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000861 }
862 }
863
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000864 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL ||
865 Code == UnOpInit::SIZE) {
Craig Topper011817a2014-04-09 04:50:04 +0000866 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000867 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000868 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000869 }
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000870 }
Bob Wilson7248f862009-11-22 04:24:42 +0000871
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000872 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
Craig Topperec9072d2015-05-14 05:53:53 +0000873 if (LHSl && LHSl->empty()) {
David Greened571b3c2009-05-14 22:38:31 +0000874 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000875 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000876 }
877 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000878 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000879 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000880 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000881 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000882 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000883 }
Craig Toppera9642b42015-05-04 01:35:39 +0000884 Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
885 : ListRecTy::get(Itemt->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000886 } else {
David Greened571b3c2009-05-14 22:38:31 +0000887 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000888 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000889 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000890 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000891 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000892 }
Craig Toppera9642b42015-05-04 01:35:39 +0000893 Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
David Greened571b3c2009-05-14 22:38:31 +0000894 }
895 }
896 }
897
David Greenee8f3b272009-05-14 21:22:49 +0000898 if (Lex.getCode() != tgtok::r_paren) {
899 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000900 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000901 }
902 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000903 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000904 }
David Greene5d0c0512009-05-14 20:54:48 +0000905
906 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000907 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000908 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000909 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +0000910 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000911 case tgtok::XSRL:
912 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000913 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000914 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000915 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000916 tgtok::TokKind OpTok = Lex.getCode();
917 SMLoc OpLoc = Lex.getLoc();
918 Lex.Lex(); // eat the operation
919
David Greene5d0c0512009-05-14 20:54:48 +0000920 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000921 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000922
Chris Lattner61ea00b2010-10-05 23:58:18 +0000923 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000924 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000925 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000926 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000927 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000928 case tgtok::XOR: Code = BinOpInit::OR; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000929 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
930 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
931 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
932 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000933 case tgtok::XListConcat:
934 Code = BinOpInit::LISTCONCAT;
935 // We don't know the list type until we parse the first argument
936 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000937 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000938 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000939 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000940 break;
David Greene5d0c0512009-05-14 20:54:48 +0000941 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000942
David Greene5d0c0512009-05-14 20:54:48 +0000943 if (Lex.getCode() != tgtok::l_paren) {
944 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000945 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000946 }
947 Lex.Lex(); // eat the '('
948
David Greeneaf8ee2c2011-07-29 22:43:06 +0000949 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000950
Chris Lattner61ea00b2010-10-05 23:58:18 +0000951 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000952 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000953
Chris Lattner61ea00b2010-10-05 23:58:18 +0000954 while (Lex.getCode() == tgtok::comma) {
955 Lex.Lex(); // eat the ','
956
957 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000958 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000959 }
David Greene5d0c0512009-05-14 20:54:48 +0000960
961 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000962 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000963 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000964 }
965 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000966
Daniel Sanders314e80e2014-05-07 10:13:19 +0000967 // If we are doing !listconcat, we should know the type by now
968 if (OpTok == tgtok::XListConcat) {
Nicolai Haehnlee4a2cf52018-02-22 15:26:28 +0000969 if (TypedInit *Arg0 = dyn_cast<TypedInit>(InitList[0]))
Daniel Sanders314e80e2014-05-07 10:13:19 +0000970 Type = Arg0->getType();
971 else {
Matthias Braun8c209aa2017-01-28 02:02:38 +0000972 InitList[0]->print(errs());
Daniel Sanders314e80e2014-05-07 10:13:19 +0000973 Error(OpLoc, "expected a list");
974 return nullptr;
975 }
976 }
977
Chris Lattner61ea00b2010-10-05 23:58:18 +0000978 // We allow multiple operands to associative operators like !strconcat as
979 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000980 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000981 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000982 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000983 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
984 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000985 InitList.back() = RHS;
986 }
987 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000988
Chris Lattner61ea00b2010-10-05 23:58:18 +0000989 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000990 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000991 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000992
Chris Lattner61ea00b2010-10-05 23:58:18 +0000993 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +0000994 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000995 }
996
Nicolai Haehnle8ebf7e42018-03-05 15:21:04 +0000997 case tgtok::XForEach: { // Value ::= !foreach '(' Id ',' Value ',' Value ')'
998 SMLoc OpLoc = Lex.getLoc();
999 Lex.Lex(); // eat the operation
1000 if (Lex.getCode() != tgtok::l_paren) {
1001 TokError("expected '(' after !foreach");
1002 return nullptr;
1003 }
1004
1005 if (Lex.Lex() != tgtok::Id) { // eat the '('
1006 TokError("first argument of !foreach must be an identifier");
1007 return nullptr;
1008 }
1009
1010 Init *LHS = StringInit::get(Lex.getCurStrVal());
1011
1012 if (CurRec->getValue(LHS)) {
1013 TokError((Twine("iteration variable '") + LHS->getAsString() +
1014 "' already defined")
1015 .str());
1016 return nullptr;
1017 }
1018
1019 if (Lex.Lex() != tgtok::comma) { // eat the id
1020 TokError("expected ',' in ternary operator");
1021 return nullptr;
1022 }
1023 Lex.Lex(); // eat the ','
1024
1025 Init *MHS = ParseValue(CurRec);
1026 if (!MHS)
1027 return nullptr;
1028
1029 if (Lex.getCode() != tgtok::comma) {
1030 TokError("expected ',' in ternary operator");
1031 return nullptr;
1032 }
1033 Lex.Lex(); // eat the ','
1034
1035 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1036 if (!MHSt) {
1037 TokError("could not get type of !foreach input");
1038 return nullptr;
1039 }
1040
1041 RecTy *InEltType = nullptr;
1042 RecTy *OutEltType = nullptr;
1043 bool IsDAG = false;
1044
1045 if (ListRecTy *InListTy = dyn_cast<ListRecTy>(MHSt->getType())) {
1046 InEltType = InListTy->getElementType();
1047 if (ItemType) {
1048 if (ListRecTy *OutListTy = dyn_cast<ListRecTy>(ItemType)) {
1049 OutEltType = OutListTy->getElementType();
1050 } else {
1051 Error(OpLoc,
1052 "expected value of type '" + Twine(ItemType->getAsString()) +
1053 "', but got !foreach of list type");
1054 return nullptr;
1055 }
1056 }
1057 } else if (DagRecTy *InDagTy = dyn_cast<DagRecTy>(MHSt->getType())) {
1058 InEltType = InDagTy;
1059 if (ItemType && !isa<DagRecTy>(ItemType)) {
1060 Error(OpLoc,
1061 "expected value of type '" + Twine(ItemType->getAsString()) +
1062 "', but got !foreach of dag type");
1063 return nullptr;
1064 }
1065 IsDAG = true;
1066 } else {
1067 TokError("!foreach must have list or dag input");
1068 return nullptr;
1069 }
1070
1071 CurRec->addValue(RecordVal(LHS, InEltType, false));
1072 Init *RHS = ParseValue(CurRec, OutEltType);
1073 CurRec->removeValue(LHS);
1074 if (!RHS)
1075 return nullptr;
1076
1077 if (Lex.getCode() != tgtok::r_paren) {
1078 TokError("expected ')' in binary operator");
1079 return nullptr;
1080 }
1081 Lex.Lex(); // eat the ')'
1082
1083 RecTy *OutType;
1084 if (IsDAG) {
1085 OutType = InEltType;
1086 } else {
1087 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1088 if (!RHSt) {
1089 TokError("could not get type of !foreach result");
1090 return nullptr;
1091 }
1092 OutType = RHSt->getType()->getListTy();
1093 }
1094
1095 return (TernOpInit::get(TernOpInit::FOREACH, LHS, MHS, RHS, OutType))
1096 ->Fold(CurRec, CurMultiClass);
1097 }
1098
David Greene3587eed2009-05-14 23:26:46 +00001099 case tgtok::XIf:
David Greene98ed3c72009-05-14 21:54:42 +00001100 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
1101 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +00001102 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001103
David Greene98ed3c72009-05-14 21:54:42 +00001104 tgtok::TokKind LexCode = Lex.getCode();
1105 Lex.Lex(); // eat the operation
1106 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001107 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001108 case tgtok::XIf:
1109 Code = TernOpInit::IF;
1110 break;
David Greene98ed3c72009-05-14 21:54:42 +00001111 case tgtok::XSubst:
1112 Code = TernOpInit::SUBST;
1113 break;
1114 }
1115 if (Lex.getCode() != tgtok::l_paren) {
1116 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001117 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001118 }
1119 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001120
David Greeneaf8ee2c2011-07-29 22:43:06 +00001121 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001122 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001123
David Greene98ed3c72009-05-14 21:54:42 +00001124 if (Lex.getCode() != tgtok::comma) {
1125 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001126 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001127 }
1128 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001129
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001130 Init *MHS = ParseValue(CurRec, ItemType);
1131 if (!MHS)
1132 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001133
David Greene98ed3c72009-05-14 21:54:42 +00001134 if (Lex.getCode() != tgtok::comma) {
1135 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001136 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001137 }
1138 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001139
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001140 Init *RHS = ParseValue(CurRec, ItemType);
1141 if (!RHS)
1142 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001143
David Greene98ed3c72009-05-14 21:54:42 +00001144 if (Lex.getCode() != tgtok::r_paren) {
1145 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001146 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001147 }
1148 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001149
David Greene98ed3c72009-05-14 21:54:42 +00001150 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001151 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001152 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001153 RecTy *MHSTy = nullptr;
1154 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001155
Sean Silvafb509ed2012-10-10 20:24:43 +00001156 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001157 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001158 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001159 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001160 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001161 MHSTy = BitRecTy::get();
1162
Sean Silvafb509ed2012-10-10 20:24:43 +00001163 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001164 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001165 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001166 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001167 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001168 RHSTy = BitRecTy::get();
1169
1170 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001171 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001172 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001173 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001174 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001175
1176 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001177 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001178 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001179 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001180
Nicolai Haehnle13080fd2018-03-06 13:48:20 +00001181 Type = resolveTypes(MHSTy, RHSTy);
1182 if (!Type) {
1183 TokError(Twine("inconsistent types '") + MHSTy->getAsString() +
1184 "' and '" + RHSTy->getAsString() + "' for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001185 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001186 }
1187 break;
1188 }
David Greene98ed3c72009-05-14 21:54:42 +00001189 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001190 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001191 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001192 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001193 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001194 }
1195 Type = RHSt->getType();
1196 break;
1197 }
1198 }
David Greenee32ebf22011-07-29 19:07:07 +00001199 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001200 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001201 }
David Greene5d0c0512009-05-14 20:54:48 +00001202 }
David Greene5d0c0512009-05-14 20:54:48 +00001203}
1204
1205/// ParseOperatorType - Parse a type for an operator. This returns
1206/// null on error.
1207///
1208/// OperatorType ::= '<' Type '>'
1209///
Dan Gohman1432ef82009-08-12 22:10:57 +00001210RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001211 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001212
1213 if (Lex.getCode() != tgtok::less) {
1214 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001215 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001216 }
1217 Lex.Lex(); // eat the <
1218
1219 Type = ParseType();
1220
Craig Topper011817a2014-04-09 04:50:04 +00001221 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001222 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001223 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001224 }
1225
1226 if (Lex.getCode() != tgtok::greater) {
1227 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001228 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001229 }
1230 Lex.Lex(); // eat the >
1231
1232 return Type;
1233}
1234
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001235/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1236///
1237/// SimpleValue ::= IDValue
1238/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001239/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001240/// SimpleValue ::= CODEFRAGMENT
1241/// SimpleValue ::= '?'
1242/// SimpleValue ::= '{' ValueList '}'
1243/// SimpleValue ::= ID '<' ValueListNE '>'
1244/// SimpleValue ::= '[' ValueList ']'
1245/// SimpleValue ::= '(' IDValue DagArgList ')'
1246/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001247/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001248/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1249/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1250/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001251/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001252/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1253///
David Greened4263a62011-10-19 13:04:20 +00001254Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1255 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001256 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001257 switch (Lex.getCode()) {
1258 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001259 case tgtok::paste:
1260 // This is a leading paste operation. This is deprecated but
1261 // still exists in some .td files. Ignore it.
1262 Lex.Lex(); // Skip '#'.
1263 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001264 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001265 case tgtok::BinaryIntVal: {
1266 auto BinaryVal = Lex.getCurBinaryIntVal();
1267 SmallVector<Init*, 16> Bits(BinaryVal.second);
1268 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001269 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001270 R = BitsInit::get(Bits);
1271 Lex.Lex();
1272 break;
1273 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001274 case tgtok::StrVal: {
1275 std::string Val = Lex.getCurStrVal();
1276 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001277
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001278 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001279 while (Lex.getCode() == tgtok::StrVal) {
1280 Val += Lex.getCurStrVal();
1281 Lex.Lex();
1282 }
Bob Wilson7248f862009-11-22 04:24:42 +00001283
David Greenee32ebf22011-07-29 19:07:07 +00001284 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001285 break;
1286 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001287 case tgtok::CodeFragment:
Tim Northover88403d72016-07-05 21:22:55 +00001288 R = CodeInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001289 Lex.Lex();
1290 break;
1291 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001292 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001293 Lex.Lex();
1294 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001295 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001296 SMLoc NameLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00001297 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001298 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001299 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001300
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001301 // Value ::= ID '<' ValueListNE '>'
1302 if (Lex.Lex() == tgtok::greater) {
1303 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001304 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001305 }
David Greene8618f952009-06-08 20:23:18 +00001306
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001307 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1308 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1309 // body.
Matthias Braun215ff842016-12-05 07:35:13 +00001310 Record *Class = Records.getClass(Name->getValue());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001311 if (!Class) {
Matthias Braun215ff842016-12-05 07:35:13 +00001312 Error(NameLoc, "Expected a class name, got '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001313 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001314 }
David Greene8618f952009-06-08 20:23:18 +00001315
Matthias Braunc66e7552016-12-05 06:41:54 +00001316 SubClassReference SCRef;
1317 ParseValueList(SCRef.TemplateArgs, CurRec, Class);
1318 if (SCRef.TemplateArgs.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001319
David Greene8618f952009-06-08 20:23:18 +00001320 if (Lex.getCode() != tgtok::greater) {
1321 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001322 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001323 }
1324 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001325 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001326
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001327 // Create the new record, set it as CurRec temporarily.
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00001328 auto NewRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), NameLoc,
1329 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001330 Record *NewRec = NewRecOwner.get(); // Keep a copy since we may release.
Jordan Rosef12e8a92013-01-10 18:50:11 +00001331 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001332 SCRef.Rec = Class;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001333 // Add info about the subclass to NewRec.
Craig Topper84138712014-11-29 05:31:10 +00001334 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001335 return nullptr;
Craig Topper84138712014-11-29 05:31:10 +00001336
Hal Finkela8c1f462014-01-02 20:47:09 +00001337 if (!CurMultiClass) {
1338 NewRec->resolveReferences();
Craig Toppercdab2322014-11-29 05:52:51 +00001339 Records.addDef(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001340 } else {
Adam Nemete5a07162014-09-16 17:14:13 +00001341 // This needs to get resolved once the multiclass template arguments are
1342 // known before any use.
1343 NewRec->setResolveFirst(true);
Hal Finkela8c1f462014-01-02 20:47:09 +00001344 // Otherwise, we're inside a multiclass, add it to the multiclass.
Craig Topperc3504c42014-12-11 05:25:33 +00001345 CurMultiClass->DefPrototypes.push_back(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001346
1347 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00001348 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
1349 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Hal Finkela8c1f462014-01-02 20:47:09 +00001350 assert(RV && "Template arg doesn't exist?");
1351 NewRec->addValue(*RV);
1352 }
1353
1354 // We can't return the prototype def here, instead return:
1355 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1356 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1357 assert(MCNameRV && "multiclass record must have a NAME");
1358
1359 return UnOpInit::get(UnOpInit::CAST,
1360 BinOpInit::get(BinOpInit::STRCONCAT,
1361 VarInit::get(MCNameRV->getName(),
1362 MCNameRV->getType()),
1363 NewRec->getNameInit(),
1364 StringRecTy::get()),
Nicolai Haehnle13080fd2018-03-06 13:48:20 +00001365 NewRec->getDefInit()->getType());
Hal Finkela8c1f462014-01-02 20:47:09 +00001366 }
Bob Wilson7248f862009-11-22 04:24:42 +00001367
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001368 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001369 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001370 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001371 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001372 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001373 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001374 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001375
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001376 if (Lex.getCode() != tgtok::r_brace) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001377 ParseValueList(Vals, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001378 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001379 }
1380 if (Lex.getCode() != tgtok::r_brace) {
1381 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001382 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001383 }
1384 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001385
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001386 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001387
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001388 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1389 // first. We'll first read everything in to a vector, then we can reverse
1390 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001391 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001392 // FIXME: The following two loops would not be duplicated
1393 // if the API was a little more orthogonal.
1394
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001395 // bits<n> values are allowed to initialize n bits.
1396 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1397 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1398 NewBits.push_back(BI->getBit((e - i) - 1));
1399 continue;
1400 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001401 // bits<n> can also come from variable initializers.
1402 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1403 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1404 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1405 NewBits.push_back(VI->getBit((e - i) - 1));
1406 continue;
1407 }
1408 // Fallthrough to try convert this to a bit.
1409 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001410 // All other values must be convertible to just a single bit.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001411 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001412 if (!Bit) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001413 Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
Chris Lattner52416952007-11-22 21:06:59 +00001414 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001415 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001416 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001417 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001418 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001419 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001420 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001421 }
1422 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1423 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001424 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001425
Craig Topper011817a2014-04-09 04:50:04 +00001426 RecTy *DeducedEltTy = nullptr;
1427 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001428
Craig Topper011817a2014-04-09 04:50:04 +00001429 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001430 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001431 if (!ListType) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001432 TokError(Twine("Type mismatch for list, expected list type, got ") +
1433 ItemType->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001434 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001435 }
1436 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001437 }
David Greene8618f952009-06-08 20:23:18 +00001438
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001439 if (Lex.getCode() != tgtok::r_square) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001440 ParseValueList(Vals, CurRec, nullptr,
1441 GivenListTy ? GivenListTy->getElementType() : nullptr);
Craig Topper011817a2014-04-09 04:50:04 +00001442 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001443 }
1444 if (Lex.getCode() != tgtok::r_square) {
1445 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001446 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001447 }
1448 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001449
Craig Topper011817a2014-04-09 04:50:04 +00001450 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001451 if (Lex.getCode() == tgtok::less) {
1452 // Optional list element type
1453 Lex.Lex(); // eat the '<'
1454
1455 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001456 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001457 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001458 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001459 }
1460
1461 if (Lex.getCode() != tgtok::greater) {
1462 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001463 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001464 }
1465 Lex.Lex(); // eat the '>'
1466 }
1467
1468 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001469 RecTy *EltTy = nullptr;
Craig Toppereb4d7c62015-04-29 04:43:36 +00001470 for (Init *V : Vals) {
1471 TypedInit *TArg = dyn_cast<TypedInit>(V);
Craig Topper011817a2014-04-09 04:50:04 +00001472 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001473 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001474 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001475 }
Craig Topper011817a2014-04-09 04:50:04 +00001476 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001477 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001478 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001479 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001480 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001481 }
Bob Wilson7248f862009-11-22 04:24:42 +00001482 } else {
David Greene8618f952009-06-08 20:23:18 +00001483 EltTy = TArg->getType();
1484 }
1485 }
1486
Craig Topper011817a2014-04-09 04:50:04 +00001487 if (GivenEltTy) {
1488 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001489 // Verify consistency
1490 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1491 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001492 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001493 }
1494 }
1495 EltTy = GivenEltTy;
1496 }
1497
Craig Topper011817a2014-04-09 04:50:04 +00001498 if (!EltTy) {
1499 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001500 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001501 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001502 }
1503 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001504 } else {
David Greene8618f952009-06-08 20:23:18 +00001505 // Make sure the deduced type is compatible with the given type
1506 if (GivenListTy) {
1507 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
Nicolai Haehnlef19083d2018-02-22 15:26:21 +00001508 TokError(Twine("Element type mismatch for list: element type '") +
1509 EltTy->getAsString() + "' not convertible to '" +
1510 GivenListTy->getElementType()->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001511 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001512 }
1513 }
1514 DeducedEltTy = EltTy;
1515 }
Bob Wilson7248f862009-11-22 04:24:42 +00001516
David Greenee32ebf22011-07-29 19:07:07 +00001517 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001518 }
1519 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1520 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001521 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001522 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001523 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001524 }
Bob Wilson7248f862009-11-22 04:24:42 +00001525
David Greeneaf8ee2c2011-07-29 22:43:06 +00001526 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001527 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001528
Nate Begemandbe3f772009-03-19 05:21:56 +00001529 // If the operator name is present, parse it.
Matthias Braun7cf3b112016-12-05 06:00:41 +00001530 StringInit *OperatorName = nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001531 if (Lex.getCode() == tgtok::colon) {
1532 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1533 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001534 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001535 }
Matthias Braun7cf3b112016-12-05 06:00:41 +00001536 OperatorName = StringInit::get(Lex.getCurStrVal());
Nate Begemandbe3f772009-03-19 05:21:56 +00001537 Lex.Lex(); // eat the VarName.
1538 }
Bob Wilson7248f862009-11-22 04:24:42 +00001539
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001540 SmallVector<std::pair<llvm::Init*, StringInit*>, 8> DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001541 if (Lex.getCode() != tgtok::r_paren) {
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001542 ParseDagArgList(DagArgs, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001543 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001544 }
Bob Wilson7248f862009-11-22 04:24:42 +00001545
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001546 if (Lex.getCode() != tgtok::r_paren) {
1547 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001548 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001549 }
1550 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001551
David Greenee32ebf22011-07-29 19:07:07 +00001552 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001553 }
Bob Wilson7248f862009-11-22 04:24:42 +00001554
David Greene2f7cf7f2011-01-07 17:05:37 +00001555 case tgtok::XHead:
1556 case tgtok::XTail:
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +00001557 case tgtok::XSize:
David Greene2f7cf7f2011-01-07 17:05:37 +00001558 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001559 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001560 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001561 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001562 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +00001563 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +00001564 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001565 case tgtok::XSRL:
1566 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001567 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001568 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001569 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001570 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001571 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001572 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001573 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001574 }
1575 }
Bob Wilson7248f862009-11-22 04:24:42 +00001576
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001577 return R;
1578}
1579
1580/// ParseValue - Parse a tblgen value. This returns null on error.
1581///
1582/// Value ::= SimpleValue ValueSuffix*
1583/// ValueSuffix ::= '{' BitList '}'
1584/// ValueSuffix ::= '[' BitList ']'
1585/// ValueSuffix ::= '.' ID
1586///
David Greened4263a62011-10-19 13:04:20 +00001587Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1588 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001589 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001590
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001591 // Parse the suffixes now if present.
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001592 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001593 switch (Lex.getCode()) {
1594 default: return Result;
1595 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001596 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001597 // This is the beginning of the object body.
1598 return Result;
1599
Chris Lattner526c8cb2009-06-21 03:39:35 +00001600 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001601 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001602 SmallVector<unsigned, 16> Ranges;
1603 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001604 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001605
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001606 // Reverse the bitlist.
1607 std::reverse(Ranges.begin(), Ranges.end());
1608 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001609 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001610 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001611 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001612 }
Bob Wilson7248f862009-11-22 04:24:42 +00001613
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001614 // Eat the '}'.
1615 if (Lex.getCode() != tgtok::r_brace) {
1616 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001617 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001618 }
1619 Lex.Lex();
1620 break;
1621 }
1622 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001623 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001624 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001625 SmallVector<unsigned, 16> Ranges;
1626 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001627 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001628
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001629 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001630 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001631 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001632 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001633 }
Bob Wilson7248f862009-11-22 04:24:42 +00001634
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001635 // Eat the ']'.
1636 if (Lex.getCode() != tgtok::r_square) {
1637 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001638 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001639 }
1640 Lex.Lex();
1641 break;
1642 }
Matthias Braun6a441832016-12-05 06:00:36 +00001643 case tgtok::period: {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001644 if (Lex.Lex() != tgtok::Id) { // eat the .
1645 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001646 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001647 }
Matthias Braun6a441832016-12-05 06:00:36 +00001648 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
1649 if (!Result->getFieldType(FieldName)) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001650 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001651 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001652 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001653 }
Matthias Braun6a441832016-12-05 06:00:36 +00001654 Result = FieldInit::get(Result, FieldName);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001655 Lex.Lex(); // eat field name
1656 break;
Matthias Braun6a441832016-12-05 06:00:36 +00001657 }
David Greene8e85b482011-10-19 13:04:43 +00001658
1659 case tgtok::paste:
1660 SMLoc PasteLoc = Lex.getLoc();
1661
1662 // Create a !strconcat() operation, first casting each operand to
1663 // a string if necessary.
1664
Sean Silvafb509ed2012-10-10 20:24:43 +00001665 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001666 if (!LHS) {
1667 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001668 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001669 }
Craig Toppera9642b42015-05-04 01:35:39 +00001670
David Greene8e85b482011-10-19 13:04:43 +00001671 if (LHS->getType() != StringRecTy::get()) {
1672 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1673 }
1674
Craig Topper011817a2014-04-09 04:50:04 +00001675 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001676
1677 Lex.Lex(); // Eat the '#'.
1678 switch (Lex.getCode()) {
1679 case tgtok::colon:
1680 case tgtok::semi:
1681 case tgtok::l_brace:
1682 // These are all of the tokens that can begin an object body.
1683 // Some of these can also begin values but we disallow those cases
1684 // because they are unlikely to be useful.
Craig Toppera9642b42015-05-04 01:35:39 +00001685
David Greene8e85b482011-10-19 13:04:43 +00001686 // Trailing paste, concat with an empty string.
1687 RHS = StringInit::get("");
1688 break;
1689
1690 default:
1691 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001692 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001693 if (!RHS) {
1694 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001695 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001696 }
1697
1698 if (RHS->getType() != StringRecTy::get()) {
1699 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1700 }
Craig Toppera9642b42015-05-04 01:35:39 +00001701
David Greene8e85b482011-10-19 13:04:43 +00001702 break;
1703 }
1704
1705 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1706 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1707 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001708 }
1709 }
1710}
1711
1712/// ParseDagArgList - Parse the argument list for a dag literal expression.
1713///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001714/// DagArg ::= Value (':' VARNAME)?
1715/// DagArg ::= VARNAME
1716/// DagArgList ::= DagArg
1717/// DagArgList ::= DagArgList ',' DagArg
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001718void TGParser::ParseDagArgList(
1719 SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
1720 Record *CurRec) {
Bob Wilson7248f862009-11-22 04:24:42 +00001721
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001722 while (true) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001723 // DagArg ::= VARNAME
1724 if (Lex.getCode() == tgtok::VarName) {
1725 // A missing value is treated like '?'.
Matthias Braunbb053162016-12-05 06:00:46 +00001726 StringInit *VarName = StringInit::get(Lex.getCurStrVal());
1727 Result.emplace_back(UnsetInit::get(), VarName);
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001728 Lex.Lex();
1729 } else {
1730 // DagArg ::= Value (':' VARNAME)?
1731 Init *Val = ParseValue(CurRec);
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001732 if (!Val) {
1733 Result.clear();
1734 return;
1735 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001736
1737 // If the variable name is present, add it.
Matthias Braunbb053162016-12-05 06:00:46 +00001738 StringInit *VarName = nullptr;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001739 if (Lex.getCode() == tgtok::colon) {
1740 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1741 TokError("expected variable name in dag literal");
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001742 Result.clear();
1743 return;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001744 }
Matthias Braunbb053162016-12-05 06:00:46 +00001745 VarName = StringInit::get(Lex.getCurStrVal());
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001746 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001747 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001748
1749 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001750 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001751 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001752 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001753 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001754}
1755
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001756/// ParseValueList - Parse a comma separated list of values, returning them as a
1757/// vector. Note that this always expects to be able to parse at least one
1758/// value. It returns an empty list if this is not possible.
1759///
1760/// ValueList ::= Value (',' Value)
1761///
Matthias Braunc66e7552016-12-05 06:41:54 +00001762void TGParser::ParseValueList(SmallVectorImpl<Init*> &Result, Record *CurRec,
1763 Record *ArgsRec, RecTy *EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001764 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001765 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001766 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001767 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001768 if (TArgs.empty()) {
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001769 TokError("template argument provided to non-template class");
Matthias Braunc66e7552016-12-05 06:41:54 +00001770 Result.clear();
1771 return;
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001772 }
David Greene8618f952009-06-08 20:23:18 +00001773 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001774 if (!RV) {
1775 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1776 << ")\n";
1777 }
David Greene8618f952009-06-08 20:23:18 +00001778 assert(RV && "Template argument record not found??");
1779 ItemType = RV->getType();
1780 ++ArgN;
1781 }
1782 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00001783 if (!Result.back()) {
1784 Result.clear();
1785 return;
1786 }
Bob Wilson7248f862009-11-22 04:24:42 +00001787
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001788 while (Lex.getCode() == tgtok::comma) {
1789 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001790
Craig Topper011817a2014-04-09 04:50:04 +00001791 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001792 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001793 if (ArgN >= TArgs.size()) {
1794 TokError("too many template arguments");
Matthias Braunc66e7552016-12-05 06:41:54 +00001795 Result.clear();
1796 return;
Bob Wilson7248f862009-11-22 04:24:42 +00001797 }
David Greene8618f952009-06-08 20:23:18 +00001798 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1799 assert(RV && "Template argument record not found??");
1800 ItemType = RV->getType();
1801 ++ArgN;
1802 }
1803 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00001804 if (!Result.back()) {
1805 Result.clear();
1806 return;
1807 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001808 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001809}
1810
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001811/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1812/// empty string on error. This can happen in a number of different context's,
1813/// including within a def or in the template args for a def (which which case
1814/// CurRec will be non-null) and within the template args for a multiclass (in
1815/// which case CurRec will be null, but CurMultiClass will be set). This can
1816/// also happen within a def that is within a multiclass, which will set both
1817/// CurRec and CurMultiClass.
1818///
1819/// Declaration ::= FIELD? Type ID ('=' Value)?
1820///
David Greenedb10e692011-10-19 13:02:42 +00001821Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001822 bool ParsingTemplateArgs) {
1823 // Read the field prefix if present.
1824 bool HasField = Lex.getCode() == tgtok::Field;
1825 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001826
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001827 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001828 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001829
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001830 if (Lex.getCode() != tgtok::Id) {
1831 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001832 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001833 }
Bob Wilson7248f862009-11-22 04:24:42 +00001834
Chris Lattner526c8cb2009-06-21 03:39:35 +00001835 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001836 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001837 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001838
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001839 if (ParsingTemplateArgs) {
Craig Toppera9642b42015-05-04 01:35:39 +00001840 if (CurRec)
David Greenedb10e692011-10-19 13:02:42 +00001841 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Craig Toppera9642b42015-05-04 01:35:39 +00001842 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001843 assert(CurMultiClass);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001844 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001845 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1846 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001847 }
Bob Wilson7248f862009-11-22 04:24:42 +00001848
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001849 // Add the value.
1850 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001851 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001852
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001853 // If a value is present, parse it.
1854 if (Lex.getCode() == tgtok::equal) {
1855 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001856 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001857 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001858 if (!Val ||
Craig Toppercfd81732016-01-04 03:15:08 +00001859 SetValue(CurRec, ValLoc, DeclName, None, Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001860 // Return the name, even if an error is thrown. This is so that we can
1861 // continue to make some progress, even without the value having been
1862 // initialized.
1863 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001864 }
Bob Wilson7248f862009-11-22 04:24:42 +00001865
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001866 return DeclName;
1867}
1868
David Greenefb927af2012-02-22 16:09:41 +00001869/// ParseForeachDeclaration - Read a foreach declaration, returning
1870/// the name of the declared object or a NULL Init on error. Return
1871/// the name of the parsed initializer list through ForeachListName.
1872///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001873/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1874/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1875/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001876///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001877VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001878 if (Lex.getCode() != tgtok::Id) {
1879 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001880 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001881 }
1882
1883 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1884 Lex.Lex();
1885
1886 // If a value is present, parse it.
1887 if (Lex.getCode() != tgtok::equal) {
1888 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001889 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001890 }
1891 Lex.Lex(); // Eat the '='
1892
Craig Topper011817a2014-04-09 04:50:04 +00001893 RecTy *IterType = nullptr;
Matthias Braunc66e7552016-12-05 06:41:54 +00001894 SmallVector<unsigned, 16> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001895
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001896 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001897 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001898 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001899 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001900 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001901 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001902 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001903 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001904 }
1905 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001906 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001907 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001908 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001909 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001910 }
1911 IterType = ListType->getElementType();
1912 break;
David Greenefb927af2012-02-22 16:09:41 +00001913 }
1914
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001915 case tgtok::IntVal: { // RangePiece.
1916 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001917 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001918 break;
David Greenefb927af2012-02-22 16:09:41 +00001919 }
1920
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001921 case tgtok::l_brace: { // '{' RangeList '}'
1922 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001923 ParseRangeList(Ranges);
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001924 if (Lex.getCode() != tgtok::r_brace) {
1925 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001926 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001927 }
1928 Lex.Lex();
1929 break;
1930 }
1931 }
David Greenefb927af2012-02-22 16:09:41 +00001932
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001933 if (!Ranges.empty()) {
1934 assert(!IterType && "Type already initialized?");
1935 IterType = IntRecTy::get();
1936 std::vector<Init*> Values;
Craig Topper8eb764c2015-06-02 06:19:28 +00001937 for (unsigned R : Ranges)
1938 Values.push_back(IntInit::get(R));
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001939 ForeachListValue = ListInit::get(Values, IterType);
1940 }
1941
1942 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001943 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001944
1945 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001946}
1947
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001948/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1949/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1950/// template args for a def, which may or may not be in a multiclass. If null,
1951/// these are the template args for a multiclass.
1952///
1953/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001954///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001955bool TGParser::ParseTemplateArgList(Record *CurRec) {
1956 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1957 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001958
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001959 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001960
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001961 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001962 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001963 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001964 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001965
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001966 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001967
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001968 while (Lex.getCode() == tgtok::comma) {
1969 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001970
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001971 // Read the following declarations.
1972 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001973 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001974 return true;
1975 TheRecToAddTo->addTemplateArg(TemplArg);
1976 }
Bob Wilson7248f862009-11-22 04:24:42 +00001977
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001978 if (Lex.getCode() != tgtok::greater)
1979 return TokError("expected '>' at end of template argument list");
1980 Lex.Lex(); // eat the '>'.
1981 return false;
1982}
1983
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001984/// ParseBodyItem - Parse a single item at within the body of a def or class.
1985///
1986/// BodyItem ::= Declaration ';'
1987/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1988bool TGParser::ParseBodyItem(Record *CurRec) {
1989 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001990 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001991 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001992
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001993 if (Lex.getCode() != tgtok::semi)
1994 return TokError("expected ';' after declaration");
1995 Lex.Lex();
1996 return false;
1997 }
1998
1999 // LET ID OptionalRangeList '=' Value ';'
2000 if (Lex.Lex() != tgtok::Id)
2001 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00002002
Chris Lattner526c8cb2009-06-21 03:39:35 +00002003 SMLoc IdLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00002004 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002005 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00002006
Matthias Braunc66e7552016-12-05 06:41:54 +00002007 SmallVector<unsigned, 16> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00002008 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002009 return true;
2010 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002011
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002012 if (Lex.getCode() != tgtok::equal)
2013 return TokError("expected '=' in let expression");
2014 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002015
David Greene8618f952009-06-08 20:23:18 +00002016 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00002017 if (!Field)
Matthias Braun215ff842016-12-05 07:35:13 +00002018 return TokError("Value '" + FieldName->getValue() + "' unknown!");
David Greene8618f952009-06-08 20:23:18 +00002019
2020 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00002021
David Greeneaf8ee2c2011-07-29 22:43:06 +00002022 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00002023 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002024
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002025 if (Lex.getCode() != tgtok::semi)
2026 return TokError("expected ';' after let expression");
2027 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002028
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002029 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
2030}
2031
2032/// ParseBody - Read the body of a class or def. Return true on error, false on
2033/// success.
2034///
2035/// Body ::= ';'
2036/// Body ::= '{' BodyList '}'
2037/// BodyList BodyItem*
2038///
2039bool TGParser::ParseBody(Record *CurRec) {
2040 // If this is a null definition, just eat the semi and return.
2041 if (Lex.getCode() == tgtok::semi) {
2042 Lex.Lex();
2043 return false;
2044 }
Bob Wilson7248f862009-11-22 04:24:42 +00002045
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002046 if (Lex.getCode() != tgtok::l_brace)
2047 return TokError("Expected ';' or '{' to start body");
2048 // Eat the '{'.
2049 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002050
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002051 while (Lex.getCode() != tgtok::r_brace)
2052 if (ParseBodyItem(CurRec))
2053 return true;
2054
2055 // Eat the '}'.
2056 Lex.Lex();
2057 return false;
2058}
2059
Sean Silvacb1a75e2013-01-09 04:49:14 +00002060/// \brief Apply the current let bindings to \a CurRec.
2061/// \returns true on error, false otherwise.
2062bool TGParser::ApplyLetStack(Record *CurRec) {
Matthias Braunc66e7552016-12-05 06:41:54 +00002063 for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
Craig Topper8eb764c2015-06-02 06:19:28 +00002064 for (LetRecord &LR : LetInfo)
2065 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
Sean Silvacb1a75e2013-01-09 04:49:14 +00002066 return true;
2067 return false;
2068}
2069
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002070/// ParseObjectBody - Parse the body of a def or class. This consists of an
2071/// optional ClassList followed by a Body. CurRec is the current def or class
2072/// that is being parsed.
2073///
2074/// ObjectBody ::= BaseClassList Body
2075/// BaseClassList ::= /*empty*/
2076/// BaseClassList ::= ':' BaseClassListNE
2077/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
2078///
2079bool TGParser::ParseObjectBody(Record *CurRec) {
2080 // If there is a baseclass list, read it.
2081 if (Lex.getCode() == tgtok::colon) {
2082 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002083
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002084 // Read all of the subclasses.
2085 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002086 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002087 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002088 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002089
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002090 // Add it.
2091 if (AddSubClass(CurRec, SubClass))
2092 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002093
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002094 if (Lex.getCode() != tgtok::comma) break;
2095 Lex.Lex(); // eat ','.
2096 SubClass = ParseSubClassReference(CurRec, false);
2097 }
2098 }
2099
Sean Silvacb1a75e2013-01-09 04:49:14 +00002100 if (ApplyLetStack(CurRec))
2101 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002102
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002103 return ParseBody(CurRec);
2104}
2105
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002106/// ParseDef - Parse and return a top level or multiclass def, return the record
2107/// corresponding to it. This returns null on error.
2108///
2109/// DefInst ::= DEF ObjectName ObjectBody
2110///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002111bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00002112 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002113 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00002114 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002115
2116 // Parse ObjectName and make a record for it.
Craig Topper84138712014-11-29 05:31:10 +00002117 std::unique_ptr<Record> CurRecOwner;
Jordan Roseabdd99b2013-01-10 18:50:05 +00002118 Init *Name = ParseObjectName(CurMultiClass);
2119 if (Name)
Craig Topper84138712014-11-29 05:31:10 +00002120 CurRecOwner = make_unique<Record>(Name, DefLoc, Records);
Jordan Roseabdd99b2013-01-10 18:50:05 +00002121 else
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00002122 CurRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), DefLoc,
2123 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00002124 Record *CurRec = CurRecOwner.get(); // Keep a copy since we may release.
Bob Wilson7248f862009-11-22 04:24:42 +00002125
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002126 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002127 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002128
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002129 // Ensure redefinition doesn't happen.
Craig Topper84138712014-11-29 05:31:10 +00002130 if (Records.getDef(CurRec->getNameInitAsString()))
2131 return Error(DefLoc, "def '" + CurRec->getNameInitAsString()+
2132 "' already defined");
Craig Toppercdab2322014-11-29 05:52:51 +00002133 Records.addDef(std::move(CurRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00002134
2135 if (ParseObjectBody(CurRec))
2136 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002137 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002138 // Parse the body before adding this prototype to the DefPrototypes vector.
2139 // That way implicit definitions will be added to the DefPrototypes vector
2140 // before this object, instantiated prior to defs derived from this object,
2141 // and this available for indirect name resolution when defs derived from
2142 // this object are instantiated.
Craig Topper84138712014-11-29 05:31:10 +00002143 if (ParseObjectBody(CurRec))
Hal Finkela8c1f462014-01-02 20:47:09 +00002144 return true;
2145
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002146 // Otherwise, a def inside a multiclass, add it to the multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002147 for (const auto &Proto : CurMultiClass->DefPrototypes)
2148 if (Proto->getNameInit() == CurRec->getNameInit())
Craig Topper84138712014-11-29 05:31:10 +00002149 return Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
2150 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002151 CurMultiClass->DefPrototypes.push_back(std::move(CurRecOwner));
Anton Yartsev671dff12014-08-08 00:29:54 +00002152 } else if (ParseObjectBody(CurRec)) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002153 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002154 }
Bob Wilson7248f862009-11-22 04:24:42 +00002155
Craig Topper011817a2014-04-09 04:50:04 +00002156 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002157 // See Record::setName(). This resolve step will see any new name
2158 // for the def that might have been created when resolving
2159 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002160 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002161
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002162 // If ObjectBody has template arguments, it's an error.
2163 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002164
2165 if (CurMultiClass) {
2166 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002167 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
2168 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002169 assert(RV && "Template arg doesn't exist?");
2170 CurRec->addValue(*RV);
2171 }
2172 }
2173
Craig Toppera9642b42015-05-04 01:35:39 +00002174 if (ProcessForeachDefs(CurRec, DefLoc))
Craig Topper84138712014-11-29 05:31:10 +00002175 return Error(DefLoc, "Could not process loops for def" +
2176 CurRec->getNameInitAsString());
David Greenefb927af2012-02-22 16:09:41 +00002177
2178 return false;
2179}
2180
2181/// ParseForeach - Parse a for statement. Return the record corresponding
2182/// to it. This returns true on error.
2183///
2184/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2185/// Foreach ::= FOREACH Declaration IN Object
2186///
2187bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2188 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2189 Lex.Lex(); // Eat the 'for' token.
2190
2191 // Make a temporary object to record items associated with the for
2192 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002193 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002194 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002195 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002196 return TokError("expected declaration in for");
2197
2198 if (Lex.getCode() != tgtok::In)
2199 return TokError("Unknown tok");
2200 Lex.Lex(); // Eat the in
2201
2202 // Create a loop object and remember it.
2203 Loops.push_back(ForeachLoop(IterName, ListValue));
2204
2205 if (Lex.getCode() != tgtok::l_brace) {
2206 // FOREACH Declaration IN Object
2207 if (ParseObject(CurMultiClass))
2208 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002209 } else {
David Greenefb927af2012-02-22 16:09:41 +00002210 SMLoc BraceLoc = Lex.getLoc();
2211 // Otherwise, this is a group foreach.
2212 Lex.Lex(); // eat the '{'.
2213
2214 // Parse the object list.
2215 if (ParseObjectList(CurMultiClass))
2216 return true;
2217
2218 if (Lex.getCode() != tgtok::r_brace) {
2219 TokError("expected '}' at end of foreach command");
2220 return Error(BraceLoc, "to match this '{'");
2221 }
2222 Lex.Lex(); // Eat the }
2223 }
2224
2225 // We've processed everything in this loop.
2226 Loops.pop_back();
2227
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002228 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002229}
2230
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002231/// ParseClass - Parse a tblgen class definition.
2232///
2233/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2234///
2235bool TGParser::ParseClass() {
2236 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2237 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002238
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002239 if (Lex.getCode() != tgtok::Id)
2240 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002241
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002242 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2243 if (CurRec) {
2244 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002245 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002246 !CurRec->getSuperClasses().empty() ||
2247 !CurRec->getTemplateArgs().empty())
Craig Topper85c07002015-04-30 05:54:22 +00002248 return TokError("Class '" + CurRec->getNameInitAsString() +
2249 "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002250 } else {
2251 // If this is the first reference to this class, create and add it.
Hans Wennborgffbbd532014-11-30 00:31:49 +00002252 auto NewRec =
2253 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
Craig Toppercdab2322014-11-29 05:52:51 +00002254 CurRec = NewRec.get();
2255 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002256 }
2257 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002258
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002259 // If there are template args, parse them.
2260 if (Lex.getCode() == tgtok::less)
2261 if (ParseTemplateArgList(CurRec))
2262 return true;
2263
2264 // Finally, parse the object body.
2265 return ParseObjectBody(CurRec);
2266}
2267
2268/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2269/// of LetRecords.
2270///
2271/// LetList ::= LetItem (',' LetItem)*
2272/// LetItem ::= ID OptionalRangeList '=' Value
2273///
Matthias Braunc66e7552016-12-05 06:41:54 +00002274void TGParser::ParseLetList(SmallVectorImpl<LetRecord> &Result) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002275 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002276 if (Lex.getCode() != tgtok::Id) {
2277 TokError("expected identifier in let definition");
Matthias Braunc66e7552016-12-05 06:41:54 +00002278 Result.clear();
2279 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002280 }
Matthias Braunc66e7552016-12-05 06:41:54 +00002281
Matthias Braun215ff842016-12-05 07:35:13 +00002282 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattner526c8cb2009-06-21 03:39:35 +00002283 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002284 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002285
2286 // Check for an optional RangeList.
Matthias Braunc66e7552016-12-05 06:41:54 +00002287 SmallVector<unsigned, 16> Bits;
2288 if (ParseOptionalRangeList(Bits)) {
2289 Result.clear();
2290 return;
2291 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002292 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002293
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002294 if (Lex.getCode() != tgtok::equal) {
2295 TokError("expected '=' in let expression");
Matthias Braunc66e7552016-12-05 06:41:54 +00002296 Result.clear();
2297 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002298 }
2299 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002300
Craig Topper011817a2014-04-09 04:50:04 +00002301 Init *Val = ParseValue(nullptr);
Matthias Braunc66e7552016-12-05 06:41:54 +00002302 if (!Val) {
2303 Result.clear();
2304 return;
2305 }
Bob Wilson7248f862009-11-22 04:24:42 +00002306
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002307 // Now that we have everything, add the record.
Matthias Braun215ff842016-12-05 07:35:13 +00002308 Result.emplace_back(Name, Bits, Val, NameLoc);
Bob Wilson7248f862009-11-22 04:24:42 +00002309
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002310 if (Lex.getCode() != tgtok::comma)
Matthias Braunc66e7552016-12-05 06:41:54 +00002311 return;
Bob Wilson7248f862009-11-22 04:24:42 +00002312 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002313 }
2314}
2315
2316/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002317/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002318///
2319/// Object ::= LET LetList IN '{' ObjectList '}'
2320/// Object ::= LET LetList IN Object
2321///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002322bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002323 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2324 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002325
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002326 // Add this entry to the let stack.
Matthias Braunc66e7552016-12-05 06:41:54 +00002327 SmallVector<LetRecord, 8> LetInfo;
2328 ParseLetList(LetInfo);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002329 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002330 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002331
2332 if (Lex.getCode() != tgtok::In)
2333 return TokError("expected 'in' at end of top-level 'let'");
2334 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002335
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002336 // If this is a scalar let, just handle it now
2337 if (Lex.getCode() != tgtok::l_brace) {
2338 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002339 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002340 return true;
2341 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002342 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002343 // Otherwise, this is a group let.
2344 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002345
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002346 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002347 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002348 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002349
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002350 if (Lex.getCode() != tgtok::r_brace) {
2351 TokError("expected '}' at end of top level let command");
2352 return Error(BraceLoc, "to match this '{'");
2353 }
2354 Lex.Lex();
2355 }
Bob Wilson7248f862009-11-22 04:24:42 +00002356
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002357 // Outside this let scope, this let block is not active.
2358 LetStack.pop_back();
2359 return false;
2360}
2361
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002362/// ParseMultiClass - Parse a multiclass definition.
2363///
Bob Wilson3d948162009-04-28 19:41:44 +00002364/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002365/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2366/// MultiClassObject ::= DefInst
2367/// MultiClassObject ::= MultiClassInst
2368/// MultiClassObject ::= DefMInst
2369/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2370/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002371///
2372bool TGParser::ParseMultiClass() {
2373 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2374 Lex.Lex(); // Eat the multiclass token.
2375
2376 if (Lex.getCode() != tgtok::Id)
2377 return TokError("expected identifier after multiclass for name");
2378 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002379
Craig Topper7adf2bf2014-12-11 05:25:30 +00002380 auto Result =
2381 MultiClasses.insert(std::make_pair(Name,
2382 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2383
2384 if (!Result.second)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002385 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002386
Craig Topper7adf2bf2014-12-11 05:25:30 +00002387 CurMultiClass = Result.first->second.get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002388 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002389
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002390 // If there are template args, parse them.
2391 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002392 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002393 return true;
2394
David Greene7049e792009-04-24 16:55:41 +00002395 bool inherits = false;
2396
David Greene753ed8f2009-04-22 16:42:54 +00002397 // If there are submulticlasses, parse them.
2398 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002399 inherits = true;
2400
David Greene753ed8f2009-04-22 16:42:54 +00002401 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002402
David Greene753ed8f2009-04-22 16:42:54 +00002403 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002404 SubMultiClassReference SubMultiClass =
2405 ParseSubMultiClassReference(CurMultiClass);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002406 while (true) {
David Greene753ed8f2009-04-22 16:42:54 +00002407 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002408 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002409
David Greene753ed8f2009-04-22 16:42:54 +00002410 // Add it.
2411 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2412 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002413
David Greene753ed8f2009-04-22 16:42:54 +00002414 if (Lex.getCode() != tgtok::comma) break;
2415 Lex.Lex(); // eat ','.
2416 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2417 }
2418 }
2419
David Greene7049e792009-04-24 16:55:41 +00002420 if (Lex.getCode() != tgtok::l_brace) {
2421 if (!inherits)
2422 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002423 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002424 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002425 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002426 } else {
David Greene7049e792009-04-24 16:55:41 +00002427 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2428 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002429
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002430 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002431 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002432 default:
2433 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
2434 case tgtok::Let:
2435 case tgtok::Def:
2436 case tgtok::Defm:
2437 case tgtok::Foreach:
2438 if (ParseObject(CurMultiClass))
2439 return true;
2440 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002441 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002442 }
David Greene7049e792009-04-24 16:55:41 +00002443 Lex.Lex(); // eat the '}'.
2444 }
Bob Wilson7248f862009-11-22 04:24:42 +00002445
Craig Topper011817a2014-04-09 04:50:04 +00002446 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002447 return false;
2448}
2449
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002450Record *TGParser::InstantiateMulticlassDef(MultiClass &MC, Record *DefProto,
2451 Init *&DefmPrefix,
2452 SMRange DefmPrefixRange,
2453 ArrayRef<Init *> TArgs,
Matthias Braunc66e7552016-12-05 06:41:54 +00002454 ArrayRef<Init *> TemplateVals) {
David Greene5d5d88c2011-10-19 13:04:31 +00002455 // We need to preserve DefProto so it can be reused for later
2456 // instantiations, so create a new Record to inherit from it.
2457
David Greenedb445972011-10-05 22:42:07 +00002458 // Add in the defm name. If the defm prefix is empty, give each
2459 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2460 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2461 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002462
Jordan Roseabdd99b2013-01-10 18:50:05 +00002463 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002464 if (!DefmPrefix) {
Craig Topperf4412262017-06-01 06:56:16 +00002465 DefmPrefix = GetNewAnonymousName();
Jordan Roseabdd99b2013-01-10 18:50:05 +00002466 IsAnonymous = true;
2467 }
David Greene5d5d88c2011-10-19 13:04:31 +00002468
2469 Init *DefName = DefProto->getNameInit();
Sean Silvafb509ed2012-10-10 20:24:43 +00002470 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002471
Craig Topper011817a2014-04-09 04:50:04 +00002472 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002473 // We have a fully expanded string so there are no operators to
2474 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002475 DefName =
2476 BinOpInit::get(BinOpInit::STRCONCAT,
2477 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2478 StringRecTy::get())->Fold(DefProto, &MC),
2479 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2480 }
David Greene5d5d88c2011-10-19 13:04:31 +00002481
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002482 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002483 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002484 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Craig Topper84138712014-11-29 05:31:10 +00002485 auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002486
2487 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002488 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002489 Ref.Rec = DefProto;
Craig Topper84138712014-11-29 05:31:10 +00002490 AddSubClass(CurRec.get(), Ref);
David Greenedb445972011-10-05 22:42:07 +00002491
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002492 // Set the value for NAME. We don't resolve references to it 'til later,
2493 // though, so that uses in nested multiclass names don't get
2494 // confused.
Matthias Braun215ff842016-12-05 07:35:13 +00002495 if (SetValue(CurRec.get(), Ref.RefRange.Start, StringInit::get("NAME"), None,
2496 DefmPrefix, /*AllowSelfAssignment*/true)) {
Craig Topper85c07002015-04-30 05:54:22 +00002497 Error(DefmPrefixRange.Start, "Could not resolve " +
2498 CurRec->getNameInitAsString() + ":NAME to '" +
2499 DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002500 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002501 }
David Greene8bf0d722011-10-19 13:04:35 +00002502
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002503 // If the DefNameString didn't resolve, we probably have a reference to
2504 // NAME and need to replace it. We need to do at least this much greedily,
2505 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002506 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002507 RecordVal *DefNameRV = CurRec->getValue("NAME");
2508 CurRec->resolveReferencesTo(DefNameRV);
2509 }
2510
2511 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002512 // Now that we're at the top level, resolve all NAME references
2513 // in the resultant defs that weren't in the def names themselves.
2514 RecordVal *DefNameRV = CurRec->getValue("NAME");
2515 CurRec->resolveReferencesTo(DefNameRV);
2516
Hal Finkeld2497362015-05-21 04:32:56 +00002517 // Check if the name is a complex pattern.
2518 // If so, resolve it.
2519 DefName = CurRec->getNameInit();
2520 DefNameString = dyn_cast<StringInit>(DefName);
2521
2522 // OK the pattern is more complex than simply using NAME.
2523 // Let's use the heavy weaponery.
2524 if (!DefNameString) {
2525 ResolveMulticlassDefArgs(MC, CurRec.get(), DefmPrefixRange.Start,
2526 Lex.getLoc(), TArgs, TemplateVals,
2527 false/*Delete args*/);
2528 DefName = CurRec->getNameInit();
2529 DefNameString = dyn_cast<StringInit>(DefName);
2530
2531 if (!DefNameString)
2532 DefName = DefName->convertInitializerTo(StringRecTy::get());
2533
2534 // We ran out of options here...
2535 DefNameString = dyn_cast<StringInit>(DefName);
2536 if (!DefNameString) {
2537 PrintFatalError(CurRec->getLoc()[CurRec->getLoc().size() - 1],
2538 DefName->getAsUnquotedString() + " is not a string.");
2539 return nullptr;
2540 }
2541
2542 CurRec->setName(DefName);
2543 }
2544
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002545 // Now that NAME references are resolved and we're at the top level of
2546 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002547 // currently in a multiclass, it means this defm appears inside a
2548 // multiclass and its name won't be fully resolvable until we see
Hal Finkeld2497362015-05-21 04:32:56 +00002549 // the top-level defm. Therefore, we don't add this to the
2550 // RecordKeeper at this point. If we did we could get duplicate
David Greene8bf0d722011-10-19 13:04:35 +00002551 // defs as more than one probably refers to NAME or some other
2552 // common internal placeholder.
2553
2554 // Ensure redefinition doesn't happen.
2555 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002556 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002557 "' already defined, instantiating defm with subdef '" +
2558 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002559 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002560 }
2561
Craig Topper84138712014-11-29 05:31:10 +00002562 Record *CurRecSave = CurRec.get(); // Keep a copy before we release.
Craig Toppercdab2322014-11-29 05:52:51 +00002563 Records.addDef(std::move(CurRec));
Craig Topper84138712014-11-29 05:31:10 +00002564 return CurRecSave;
David Greene8bf0d722011-10-19 13:04:35 +00002565 }
2566
Craig Topper84138712014-11-29 05:31:10 +00002567 // FIXME This is bad but the ownership transfer to caller is pretty messy.
2568 // The unique_ptr in this function at least protects the exits above.
2569 return CurRec.release();
David Greenedb445972011-10-05 22:42:07 +00002570}
2571
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002572bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC, Record *CurRec,
2573 SMLoc DefmPrefixLoc, SMLoc SubClassLoc,
2574 ArrayRef<Init *> TArgs,
Matthias Braunc66e7552016-12-05 06:41:54 +00002575 ArrayRef<Init *> TemplateVals,
David Greenedb445972011-10-05 22:42:07 +00002576 bool DeleteArgs) {
Nicolai Haehnle7d697852018-03-05 15:21:19 +00002577 // Set all template arguments to the specified value or leave them as the
2578 // default if necessary, then resolve them all simultaneously.
2579 MapResolver R(CurRec);
2580
David Greenedb445972011-10-05 22:42:07 +00002581 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2582 // Check if a value is specified for this temp-arg.
2583 if (i < TemplateVals.size()) {
Craig Toppercfd81732016-01-04 03:15:08 +00002584 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], None, TemplateVals[i]))
David Greenedb445972011-10-05 22:42:07 +00002585 return true;
David Greenedb445972011-10-05 22:42:07 +00002586 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Craig Topper85c07002015-04-30 05:54:22 +00002587 return Error(SubClassLoc, "value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00002588 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +00002589 ") of multiclassclass '" + MC.Rec.getNameInitAsString() +
2590 "'");
David Greenedb445972011-10-05 22:42:07 +00002591 }
Nicolai Haehnle7d697852018-03-05 15:21:19 +00002592
2593 R.set(TArgs[i], CurRec->getValue(TArgs[i])->getValue());
2594
2595 if (DeleteArgs)
2596 CurRec->removeValue(TArgs[i]);
David Greenedb445972011-10-05 22:42:07 +00002597 }
Nicolai Haehnle7d697852018-03-05 15:21:19 +00002598
2599 CurRec->resolveReferences(R);
2600
David Greenedb445972011-10-05 22:42:07 +00002601 return false;
2602}
2603
2604bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2605 Record *CurRec,
2606 Record *DefProto,
2607 SMLoc DefmPrefixLoc) {
2608 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002609 if (ApplyLetStack(CurRec))
2610 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002611
David Greenedb445972011-10-05 22:42:07 +00002612 // Don't create a top level definition for defm inside multiclasses,
2613 // instead, only update the prototypes and bind the template args
2614 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002615 if (!CurMultiClass)
2616 return false;
Craig Toppereb4d7c62015-04-29 04:43:36 +00002617 for (const auto &Proto : CurMultiClass->DefPrototypes)
2618 if (Proto->getNameInit() == CurRec->getNameInit())
Sean Silvacc951b22013-01-09 05:28:12 +00002619 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2620 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002621 CurMultiClass->DefPrototypes.push_back(std::unique_ptr<Record>(CurRec));
David Greenedb445972011-10-05 22:42:07 +00002622
Sean Silvacc951b22013-01-09 05:28:12 +00002623 // Copy the template arguments for the multiclass into the new def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002624 for (Init * TA : CurMultiClass->Rec.getTemplateArgs()) {
2625 const RecordVal *RV = CurMultiClass->Rec.getValue(TA);
Sean Silvacc951b22013-01-09 05:28:12 +00002626 assert(RV && "Template arg doesn't exist?");
2627 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002628 }
2629
2630 return false;
2631}
2632
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002633/// ParseDefm - Parse the instantiation of a multiclass.
2634///
2635/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2636///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002637bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002638 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002639 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002640 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002641
Craig Topperb21afc62013-01-07 05:09:33 +00002642 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002643 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002644 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002645
Jordan Rosef12e8a92013-01-10 18:50:11 +00002646 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002647 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002648 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002649
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002650 // Keep track of the new generated record definitions.
2651 std::vector<Record*> NewRecDefs;
2652
2653 // This record also inherits from a regular class (non-multiclass)?
2654 bool InheritFromClass = false;
2655
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002656 // eat the colon.
2657 Lex.Lex();
2658
Chris Lattner526c8cb2009-06-21 03:39:35 +00002659 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002660 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002661
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002662 while (true) {
Craig Topper011817a2014-04-09 04:50:04 +00002663 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002664
2665 // To instantiate a multiclass, we need to first get the multiclass, then
2666 // instantiate each def contained in the multiclass with the SubClassRef
2667 // template parameters.
Craig Topper7adf2bf2014-12-11 05:25:30 +00002668 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper4ca40012014-11-30 01:20:17 +00002669 assert(MC && "Didn't lookup multiclass correctly?");
Matthias Braunc66e7552016-12-05 06:41:54 +00002670 ArrayRef<Init*> TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002671
2672 // Verify that the correct number of template arguments were specified.
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002673 ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002674 if (TArgs.size() < TemplateVals.size())
2675 return Error(SubClassLoc,
2676 "more template args specified than multiclass expects");
2677
2678 // Loop over all the def's in the multiclass, instantiating each one.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002679 for (const std::unique_ptr<Record> &DefProto : MC->DefPrototypes) {
Hal Finkeld2497362015-05-21 04:32:56 +00002680 // The record name construction goes as follow:
2681 // - If the def name is a string, prepend the prefix.
2682 // - If the def name is a more complex pattern, use that pattern.
Craig Topper7f36be92016-02-26 06:50:27 +00002683 // As a result, the record is instantiated before resolving
Hal Finkeld2497362015-05-21 04:32:56 +00002684 // arguments, as it would make its name a string.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002685 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto.get(), DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002686 SMRange(DefmLoc,
Hal Finkeld2497362015-05-21 04:32:56 +00002687 DefmPrefixEndLoc),
2688 TArgs, TemplateVals);
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002689 if (!CurRec)
2690 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002691
Craig Topper7f36be92016-02-26 06:50:27 +00002692 // Now that the record is instantiated, we can resolve arguments.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002693 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002694 TArgs, TemplateVals, true/*Delete args*/))
2695 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002696
Craig Toppereb4d7c62015-04-29 04:43:36 +00002697 if (ResolveMulticlassDef(*MC, CurRec, DefProto.get(), DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002698 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002699
Adam Nemete5a07162014-09-16 17:14:13 +00002700 // Defs that can be used by other definitions should be fully resolved
2701 // before any use.
2702 if (DefProto->isResolveFirst() && !CurMultiClass) {
2703 CurRec->resolveReferences();
2704 CurRec->setResolveFirst(false);
2705 }
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002706 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002707 }
2708
David Greenedb445972011-10-05 22:42:07 +00002709
David Greenef00919a2009-04-22 22:17:51 +00002710 if (Lex.getCode() != tgtok::comma) break;
2711 Lex.Lex(); // eat ','.
2712
Craig Topper998a39a2013-08-20 04:22:09 +00002713 if (Lex.getCode() != tgtok::Id)
2714 return TokError("expected identifier");
2715
David Greenef00919a2009-04-22 22:17:51 +00002716 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002717
2718 // A defm can inherit from regular classes (non-multiclass) as
2719 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002720 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002721
2722 if (InheritFromClass)
2723 break;
2724
Craig Topper011817a2014-04-09 04:50:04 +00002725 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002726 }
2727
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002728 if (InheritFromClass) {
2729 // Process all the classes to inherit as if they were part of a
2730 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002731 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002732 while (true) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002733 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002734 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002735
2736 // Get the expanded definition prototypes and teach them about
2737 // the record values the current class to inherit has
Craig Toppereb4d7c62015-04-29 04:43:36 +00002738 for (Record *CurRec : NewRecDefs) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002739 // Add it.
2740 if (AddSubClass(CurRec, SubClass))
2741 return true;
2742
Sean Silvacb1a75e2013-01-09 04:49:14 +00002743 if (ApplyLetStack(CurRec))
2744 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002745 }
2746
2747 if (Lex.getCode() != tgtok::comma) break;
2748 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002749 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002750 }
2751 }
2752
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002753 if (!CurMultiClass)
Craig Toppereb4d7c62015-04-29 04:43:36 +00002754 for (Record *CurRec : NewRecDefs)
David Greene50c09122011-08-10 18:27:46 +00002755 // See Record::setName(). This resolve step will see any new
2756 // name for the def that might have been created when resolving
2757 // inheritance, values and arguments above.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002758 CurRec->resolveReferences();
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002759
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002760 if (Lex.getCode() != tgtok::semi)
2761 return TokError("expected ';' at end of defm");
2762 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002763
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002764 return false;
2765}
2766
2767/// ParseObject
2768/// Object ::= ClassInst
2769/// Object ::= DefInst
2770/// Object ::= MultiClassInst
2771/// Object ::= DefMInst
2772/// Object ::= LETCommand '{' ObjectList '}'
2773/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002774bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002775 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002776 default:
2777 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002778 case tgtok::Let: return ParseTopLevelLet(MC);
2779 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002780 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002781 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002782 case tgtok::Class: return ParseClass();
2783 case tgtok::MultiClass: return ParseMultiClass();
2784 }
2785}
2786
2787/// ParseObjectList
2788/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002789bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002790 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002791 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002792 return true;
2793 }
2794 return false;
2795}
2796
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002797bool TGParser::ParseFile() {
2798 Lex.Lex(); // Prime the lexer.
2799 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002800
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002801 // If we have unread input at the end of the file, report it.
2802 if (Lex.getCode() == tgtok::Eof)
2803 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002804
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002805 return TokError("Unexpected input at top level");
2806}