blob: 73a7e5cd6a744e845093d6480ecdca3c04e36fc9 [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 Haehnlec10570f2018-02-23 11:31:49 +00001181 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1182 Type = RHSTy;
1183 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1184 Type = MHSTy;
1185 } else {
1186 TokError("inconsistent types for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001187 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001188 }
1189 break;
1190 }
David Greene98ed3c72009-05-14 21:54:42 +00001191 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001192 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001193 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001194 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001195 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001196 }
1197 Type = RHSt->getType();
1198 break;
1199 }
1200 }
David Greenee32ebf22011-07-29 19:07:07 +00001201 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001202 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001203 }
David Greene5d0c0512009-05-14 20:54:48 +00001204 }
David Greene5d0c0512009-05-14 20:54:48 +00001205}
1206
1207/// ParseOperatorType - Parse a type for an operator. This returns
1208/// null on error.
1209///
1210/// OperatorType ::= '<' Type '>'
1211///
Dan Gohman1432ef82009-08-12 22:10:57 +00001212RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001213 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001214
1215 if (Lex.getCode() != tgtok::less) {
1216 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001217 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001218 }
1219 Lex.Lex(); // eat the <
1220
1221 Type = ParseType();
1222
Craig Topper011817a2014-04-09 04:50:04 +00001223 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001224 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001225 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001226 }
1227
1228 if (Lex.getCode() != tgtok::greater) {
1229 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001230 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001231 }
1232 Lex.Lex(); // eat the >
1233
1234 return Type;
1235}
1236
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001237/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1238///
1239/// SimpleValue ::= IDValue
1240/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001241/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001242/// SimpleValue ::= CODEFRAGMENT
1243/// SimpleValue ::= '?'
1244/// SimpleValue ::= '{' ValueList '}'
1245/// SimpleValue ::= ID '<' ValueListNE '>'
1246/// SimpleValue ::= '[' ValueList ']'
1247/// SimpleValue ::= '(' IDValue DagArgList ')'
1248/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001249/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001250/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1251/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1252/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001253/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001254/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1255///
David Greened4263a62011-10-19 13:04:20 +00001256Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1257 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001258 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001259 switch (Lex.getCode()) {
1260 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001261 case tgtok::paste:
1262 // This is a leading paste operation. This is deprecated but
1263 // still exists in some .td files. Ignore it.
1264 Lex.Lex(); // Skip '#'.
1265 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001266 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001267 case tgtok::BinaryIntVal: {
1268 auto BinaryVal = Lex.getCurBinaryIntVal();
1269 SmallVector<Init*, 16> Bits(BinaryVal.second);
1270 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001271 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001272 R = BitsInit::get(Bits);
1273 Lex.Lex();
1274 break;
1275 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001276 case tgtok::StrVal: {
1277 std::string Val = Lex.getCurStrVal();
1278 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001279
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001280 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001281 while (Lex.getCode() == tgtok::StrVal) {
1282 Val += Lex.getCurStrVal();
1283 Lex.Lex();
1284 }
Bob Wilson7248f862009-11-22 04:24:42 +00001285
David Greenee32ebf22011-07-29 19:07:07 +00001286 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001287 break;
1288 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001289 case tgtok::CodeFragment:
Tim Northover88403d72016-07-05 21:22:55 +00001290 R = CodeInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001291 Lex.Lex();
1292 break;
1293 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001294 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001295 Lex.Lex();
1296 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001297 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001298 SMLoc NameLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00001299 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001300 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001301 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001302
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001303 // Value ::= ID '<' ValueListNE '>'
1304 if (Lex.Lex() == tgtok::greater) {
1305 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001306 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001307 }
David Greene8618f952009-06-08 20:23:18 +00001308
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001309 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1310 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1311 // body.
Matthias Braun215ff842016-12-05 07:35:13 +00001312 Record *Class = Records.getClass(Name->getValue());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001313 if (!Class) {
Matthias Braun215ff842016-12-05 07:35:13 +00001314 Error(NameLoc, "Expected a class name, got '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001315 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001316 }
David Greene8618f952009-06-08 20:23:18 +00001317
Matthias Braunc66e7552016-12-05 06:41:54 +00001318 SubClassReference SCRef;
1319 ParseValueList(SCRef.TemplateArgs, CurRec, Class);
1320 if (SCRef.TemplateArgs.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001321
David Greene8618f952009-06-08 20:23:18 +00001322 if (Lex.getCode() != tgtok::greater) {
1323 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001324 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001325 }
1326 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001327 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001328
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001329 // Create the new record, set it as CurRec temporarily.
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00001330 auto NewRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), NameLoc,
1331 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001332 Record *NewRec = NewRecOwner.get(); // Keep a copy since we may release.
Jordan Rosef12e8a92013-01-10 18:50:11 +00001333 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001334 SCRef.Rec = Class;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001335 // Add info about the subclass to NewRec.
Craig Topper84138712014-11-29 05:31:10 +00001336 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001337 return nullptr;
Craig Topper84138712014-11-29 05:31:10 +00001338
Hal Finkela8c1f462014-01-02 20:47:09 +00001339 if (!CurMultiClass) {
1340 NewRec->resolveReferences();
Craig Toppercdab2322014-11-29 05:52:51 +00001341 Records.addDef(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001342 } else {
Adam Nemete5a07162014-09-16 17:14:13 +00001343 // This needs to get resolved once the multiclass template arguments are
1344 // known before any use.
1345 NewRec->setResolveFirst(true);
Hal Finkela8c1f462014-01-02 20:47:09 +00001346 // Otherwise, we're inside a multiclass, add it to the multiclass.
Craig Topperc3504c42014-12-11 05:25:33 +00001347 CurMultiClass->DefPrototypes.push_back(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001348
1349 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00001350 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
1351 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Hal Finkela8c1f462014-01-02 20:47:09 +00001352 assert(RV && "Template arg doesn't exist?");
1353 NewRec->addValue(*RV);
1354 }
1355
1356 // We can't return the prototype def here, instead return:
1357 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1358 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1359 assert(MCNameRV && "multiclass record must have a NAME");
1360
1361 return UnOpInit::get(UnOpInit::CAST,
1362 BinOpInit::get(BinOpInit::STRCONCAT,
1363 VarInit::get(MCNameRV->getName(),
1364 MCNameRV->getType()),
1365 NewRec->getNameInit(),
1366 StringRecTy::get()),
1367 Class->getDefInit()->getType());
1368 }
Bob Wilson7248f862009-11-22 04:24:42 +00001369
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001370 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001371 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001372 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001373 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001374 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001375 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001376 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001377
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001378 if (Lex.getCode() != tgtok::r_brace) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001379 ParseValueList(Vals, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001380 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001381 }
1382 if (Lex.getCode() != tgtok::r_brace) {
1383 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001384 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001385 }
1386 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001387
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001388 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001389
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001390 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1391 // first. We'll first read everything in to a vector, then we can reverse
1392 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001393 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001394 // FIXME: The following two loops would not be duplicated
1395 // if the API was a little more orthogonal.
1396
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001397 // bits<n> values are allowed to initialize n bits.
1398 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1399 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1400 NewBits.push_back(BI->getBit((e - i) - 1));
1401 continue;
1402 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001403 // bits<n> can also come from variable initializers.
1404 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1405 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1406 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1407 NewBits.push_back(VI->getBit((e - i) - 1));
1408 continue;
1409 }
1410 // Fallthrough to try convert this to a bit.
1411 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001412 // All other values must be convertible to just a single bit.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001413 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001414 if (!Bit) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001415 Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
Chris Lattner52416952007-11-22 21:06:59 +00001416 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001417 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001418 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001419 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001420 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001421 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001422 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001423 }
1424 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1425 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001426 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001427
Craig Topper011817a2014-04-09 04:50:04 +00001428 RecTy *DeducedEltTy = nullptr;
1429 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001430
Craig Topper011817a2014-04-09 04:50:04 +00001431 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001432 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001433 if (!ListType) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001434 TokError(Twine("Type mismatch for list, expected list type, got ") +
1435 ItemType->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001436 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001437 }
1438 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001439 }
David Greene8618f952009-06-08 20:23:18 +00001440
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001441 if (Lex.getCode() != tgtok::r_square) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001442 ParseValueList(Vals, CurRec, nullptr,
1443 GivenListTy ? GivenListTy->getElementType() : nullptr);
Craig Topper011817a2014-04-09 04:50:04 +00001444 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001445 }
1446 if (Lex.getCode() != tgtok::r_square) {
1447 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001448 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001449 }
1450 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001451
Craig Topper011817a2014-04-09 04:50:04 +00001452 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001453 if (Lex.getCode() == tgtok::less) {
1454 // Optional list element type
1455 Lex.Lex(); // eat the '<'
1456
1457 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001458 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001459 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001460 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001461 }
1462
1463 if (Lex.getCode() != tgtok::greater) {
1464 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001465 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001466 }
1467 Lex.Lex(); // eat the '>'
1468 }
1469
1470 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001471 RecTy *EltTy = nullptr;
Craig Toppereb4d7c62015-04-29 04:43:36 +00001472 for (Init *V : Vals) {
1473 TypedInit *TArg = dyn_cast<TypedInit>(V);
Craig Topper011817a2014-04-09 04:50:04 +00001474 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001475 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001476 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001477 }
Craig Topper011817a2014-04-09 04:50:04 +00001478 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001479 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001480 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001481 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001482 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001483 }
Bob Wilson7248f862009-11-22 04:24:42 +00001484 } else {
David Greene8618f952009-06-08 20:23:18 +00001485 EltTy = TArg->getType();
1486 }
1487 }
1488
Craig Topper011817a2014-04-09 04:50:04 +00001489 if (GivenEltTy) {
1490 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001491 // Verify consistency
1492 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1493 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001494 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001495 }
1496 }
1497 EltTy = GivenEltTy;
1498 }
1499
Craig Topper011817a2014-04-09 04:50:04 +00001500 if (!EltTy) {
1501 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001502 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001503 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001504 }
1505 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001506 } else {
David Greene8618f952009-06-08 20:23:18 +00001507 // Make sure the deduced type is compatible with the given type
1508 if (GivenListTy) {
1509 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
Nicolai Haehnlef19083d2018-02-22 15:26:21 +00001510 TokError(Twine("Element type mismatch for list: element type '") +
1511 EltTy->getAsString() + "' not convertible to '" +
1512 GivenListTy->getElementType()->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001513 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001514 }
1515 }
1516 DeducedEltTy = EltTy;
1517 }
Bob Wilson7248f862009-11-22 04:24:42 +00001518
David Greenee32ebf22011-07-29 19:07:07 +00001519 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001520 }
1521 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1522 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001523 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001524 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001525 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001526 }
Bob Wilson7248f862009-11-22 04:24:42 +00001527
David Greeneaf8ee2c2011-07-29 22:43:06 +00001528 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001529 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001530
Nate Begemandbe3f772009-03-19 05:21:56 +00001531 // If the operator name is present, parse it.
Matthias Braun7cf3b112016-12-05 06:00:41 +00001532 StringInit *OperatorName = nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001533 if (Lex.getCode() == tgtok::colon) {
1534 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1535 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001536 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001537 }
Matthias Braun7cf3b112016-12-05 06:00:41 +00001538 OperatorName = StringInit::get(Lex.getCurStrVal());
Nate Begemandbe3f772009-03-19 05:21:56 +00001539 Lex.Lex(); // eat the VarName.
1540 }
Bob Wilson7248f862009-11-22 04:24:42 +00001541
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001542 SmallVector<std::pair<llvm::Init*, StringInit*>, 8> DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001543 if (Lex.getCode() != tgtok::r_paren) {
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001544 ParseDagArgList(DagArgs, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001545 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001546 }
Bob Wilson7248f862009-11-22 04:24:42 +00001547
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001548 if (Lex.getCode() != tgtok::r_paren) {
1549 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001550 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001551 }
1552 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001553
David Greenee32ebf22011-07-29 19:07:07 +00001554 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001555 }
Bob Wilson7248f862009-11-22 04:24:42 +00001556
David Greene2f7cf7f2011-01-07 17:05:37 +00001557 case tgtok::XHead:
1558 case tgtok::XTail:
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +00001559 case tgtok::XSize:
David Greene2f7cf7f2011-01-07 17:05:37 +00001560 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001561 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001562 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001563 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001564 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +00001565 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +00001566 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001567 case tgtok::XSRL:
1568 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001569 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001570 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001571 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001572 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001573 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001574 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001575 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001576 }
1577 }
Bob Wilson7248f862009-11-22 04:24:42 +00001578
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001579 return R;
1580}
1581
1582/// ParseValue - Parse a tblgen value. This returns null on error.
1583///
1584/// Value ::= SimpleValue ValueSuffix*
1585/// ValueSuffix ::= '{' BitList '}'
1586/// ValueSuffix ::= '[' BitList ']'
1587/// ValueSuffix ::= '.' ID
1588///
David Greened4263a62011-10-19 13:04:20 +00001589Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1590 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001591 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001592
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001593 // Parse the suffixes now if present.
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001594 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001595 switch (Lex.getCode()) {
1596 default: return Result;
1597 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001598 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001599 // This is the beginning of the object body.
1600 return Result;
1601
Chris Lattner526c8cb2009-06-21 03:39:35 +00001602 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001603 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001604 SmallVector<unsigned, 16> Ranges;
1605 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001606 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001607
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001608 // Reverse the bitlist.
1609 std::reverse(Ranges.begin(), Ranges.end());
1610 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001611 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001612 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001613 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001614 }
Bob Wilson7248f862009-11-22 04:24:42 +00001615
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001616 // Eat the '}'.
1617 if (Lex.getCode() != tgtok::r_brace) {
1618 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001619 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001620 }
1621 Lex.Lex();
1622 break;
1623 }
1624 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001625 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001626 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001627 SmallVector<unsigned, 16> Ranges;
1628 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001629 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001630
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001631 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001632 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001633 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001634 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001635 }
Bob Wilson7248f862009-11-22 04:24:42 +00001636
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001637 // Eat the ']'.
1638 if (Lex.getCode() != tgtok::r_square) {
1639 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001640 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001641 }
1642 Lex.Lex();
1643 break;
1644 }
Matthias Braun6a441832016-12-05 06:00:36 +00001645 case tgtok::period: {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001646 if (Lex.Lex() != tgtok::Id) { // eat the .
1647 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001648 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001649 }
Matthias Braun6a441832016-12-05 06:00:36 +00001650 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
1651 if (!Result->getFieldType(FieldName)) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001652 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001653 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001654 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001655 }
Matthias Braun6a441832016-12-05 06:00:36 +00001656 Result = FieldInit::get(Result, FieldName);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001657 Lex.Lex(); // eat field name
1658 break;
Matthias Braun6a441832016-12-05 06:00:36 +00001659 }
David Greene8e85b482011-10-19 13:04:43 +00001660
1661 case tgtok::paste:
1662 SMLoc PasteLoc = Lex.getLoc();
1663
1664 // Create a !strconcat() operation, first casting each operand to
1665 // a string if necessary.
1666
Sean Silvafb509ed2012-10-10 20:24:43 +00001667 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001668 if (!LHS) {
1669 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001670 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001671 }
Craig Toppera9642b42015-05-04 01:35:39 +00001672
David Greene8e85b482011-10-19 13:04:43 +00001673 if (LHS->getType() != StringRecTy::get()) {
1674 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1675 }
1676
Craig Topper011817a2014-04-09 04:50:04 +00001677 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001678
1679 Lex.Lex(); // Eat the '#'.
1680 switch (Lex.getCode()) {
1681 case tgtok::colon:
1682 case tgtok::semi:
1683 case tgtok::l_brace:
1684 // These are all of the tokens that can begin an object body.
1685 // Some of these can also begin values but we disallow those cases
1686 // because they are unlikely to be useful.
Craig Toppera9642b42015-05-04 01:35:39 +00001687
David Greene8e85b482011-10-19 13:04:43 +00001688 // Trailing paste, concat with an empty string.
1689 RHS = StringInit::get("");
1690 break;
1691
1692 default:
1693 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001694 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001695 if (!RHS) {
1696 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001697 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001698 }
1699
1700 if (RHS->getType() != StringRecTy::get()) {
1701 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1702 }
Craig Toppera9642b42015-05-04 01:35:39 +00001703
David Greene8e85b482011-10-19 13:04:43 +00001704 break;
1705 }
1706
1707 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1708 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1709 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001710 }
1711 }
1712}
1713
1714/// ParseDagArgList - Parse the argument list for a dag literal expression.
1715///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001716/// DagArg ::= Value (':' VARNAME)?
1717/// DagArg ::= VARNAME
1718/// DagArgList ::= DagArg
1719/// DagArgList ::= DagArgList ',' DagArg
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001720void TGParser::ParseDagArgList(
1721 SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
1722 Record *CurRec) {
Bob Wilson7248f862009-11-22 04:24:42 +00001723
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001724 while (true) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001725 // DagArg ::= VARNAME
1726 if (Lex.getCode() == tgtok::VarName) {
1727 // A missing value is treated like '?'.
Matthias Braunbb053162016-12-05 06:00:46 +00001728 StringInit *VarName = StringInit::get(Lex.getCurStrVal());
1729 Result.emplace_back(UnsetInit::get(), VarName);
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001730 Lex.Lex();
1731 } else {
1732 // DagArg ::= Value (':' VARNAME)?
1733 Init *Val = ParseValue(CurRec);
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001734 if (!Val) {
1735 Result.clear();
1736 return;
1737 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001738
1739 // If the variable name is present, add it.
Matthias Braunbb053162016-12-05 06:00:46 +00001740 StringInit *VarName = nullptr;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001741 if (Lex.getCode() == tgtok::colon) {
1742 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1743 TokError("expected variable name in dag literal");
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001744 Result.clear();
1745 return;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001746 }
Matthias Braunbb053162016-12-05 06:00:46 +00001747 VarName = StringInit::get(Lex.getCurStrVal());
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001748 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001749 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001750
1751 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001752 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001753 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001754 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001755 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001756}
1757
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001758/// ParseValueList - Parse a comma separated list of values, returning them as a
1759/// vector. Note that this always expects to be able to parse at least one
1760/// value. It returns an empty list if this is not possible.
1761///
1762/// ValueList ::= Value (',' Value)
1763///
Matthias Braunc66e7552016-12-05 06:41:54 +00001764void TGParser::ParseValueList(SmallVectorImpl<Init*> &Result, Record *CurRec,
1765 Record *ArgsRec, RecTy *EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001766 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001767 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001768 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001769 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001770 if (TArgs.empty()) {
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001771 TokError("template argument provided to non-template class");
Matthias Braunc66e7552016-12-05 06:41:54 +00001772 Result.clear();
1773 return;
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001774 }
David Greene8618f952009-06-08 20:23:18 +00001775 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001776 if (!RV) {
1777 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1778 << ")\n";
1779 }
David Greene8618f952009-06-08 20:23:18 +00001780 assert(RV && "Template argument record not found??");
1781 ItemType = RV->getType();
1782 ++ArgN;
1783 }
1784 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00001785 if (!Result.back()) {
1786 Result.clear();
1787 return;
1788 }
Bob Wilson7248f862009-11-22 04:24:42 +00001789
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001790 while (Lex.getCode() == tgtok::comma) {
1791 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001792
Craig Topper011817a2014-04-09 04:50:04 +00001793 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001794 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001795 if (ArgN >= TArgs.size()) {
1796 TokError("too many template arguments");
Matthias Braunc66e7552016-12-05 06:41:54 +00001797 Result.clear();
1798 return;
Bob Wilson7248f862009-11-22 04:24:42 +00001799 }
David Greene8618f952009-06-08 20:23:18 +00001800 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1801 assert(RV && "Template argument record not found??");
1802 ItemType = RV->getType();
1803 ++ArgN;
1804 }
1805 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00001806 if (!Result.back()) {
1807 Result.clear();
1808 return;
1809 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001810 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001811}
1812
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001813/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1814/// empty string on error. This can happen in a number of different context's,
1815/// including within a def or in the template args for a def (which which case
1816/// CurRec will be non-null) and within the template args for a multiclass (in
1817/// which case CurRec will be null, but CurMultiClass will be set). This can
1818/// also happen within a def that is within a multiclass, which will set both
1819/// CurRec and CurMultiClass.
1820///
1821/// Declaration ::= FIELD? Type ID ('=' Value)?
1822///
David Greenedb10e692011-10-19 13:02:42 +00001823Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001824 bool ParsingTemplateArgs) {
1825 // Read the field prefix if present.
1826 bool HasField = Lex.getCode() == tgtok::Field;
1827 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001828
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001829 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001830 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001831
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001832 if (Lex.getCode() != tgtok::Id) {
1833 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001834 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001835 }
Bob Wilson7248f862009-11-22 04:24:42 +00001836
Chris Lattner526c8cb2009-06-21 03:39:35 +00001837 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001838 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001839 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001840
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001841 if (ParsingTemplateArgs) {
Craig Toppera9642b42015-05-04 01:35:39 +00001842 if (CurRec)
David Greenedb10e692011-10-19 13:02:42 +00001843 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Craig Toppera9642b42015-05-04 01:35:39 +00001844 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001845 assert(CurMultiClass);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001846 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001847 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1848 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001849 }
Bob Wilson7248f862009-11-22 04:24:42 +00001850
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001851 // Add the value.
1852 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001853 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001854
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001855 // If a value is present, parse it.
1856 if (Lex.getCode() == tgtok::equal) {
1857 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001858 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001859 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001860 if (!Val ||
Craig Toppercfd81732016-01-04 03:15:08 +00001861 SetValue(CurRec, ValLoc, DeclName, None, Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001862 // Return the name, even if an error is thrown. This is so that we can
1863 // continue to make some progress, even without the value having been
1864 // initialized.
1865 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001866 }
Bob Wilson7248f862009-11-22 04:24:42 +00001867
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001868 return DeclName;
1869}
1870
David Greenefb927af2012-02-22 16:09:41 +00001871/// ParseForeachDeclaration - Read a foreach declaration, returning
1872/// the name of the declared object or a NULL Init on error. Return
1873/// the name of the parsed initializer list through ForeachListName.
1874///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001875/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1876/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1877/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001878///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001879VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001880 if (Lex.getCode() != tgtok::Id) {
1881 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001882 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001883 }
1884
1885 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1886 Lex.Lex();
1887
1888 // If a value is present, parse it.
1889 if (Lex.getCode() != tgtok::equal) {
1890 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001891 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001892 }
1893 Lex.Lex(); // Eat the '='
1894
Craig Topper011817a2014-04-09 04:50:04 +00001895 RecTy *IterType = nullptr;
Matthias Braunc66e7552016-12-05 06:41:54 +00001896 SmallVector<unsigned, 16> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001897
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001898 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001899 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001900 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001901 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001902 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001903 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001904 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001905 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001906 }
1907 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001908 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001909 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001910 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001911 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001912 }
1913 IterType = ListType->getElementType();
1914 break;
David Greenefb927af2012-02-22 16:09:41 +00001915 }
1916
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001917 case tgtok::IntVal: { // RangePiece.
1918 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001919 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001920 break;
David Greenefb927af2012-02-22 16:09:41 +00001921 }
1922
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001923 case tgtok::l_brace: { // '{' RangeList '}'
1924 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001925 ParseRangeList(Ranges);
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001926 if (Lex.getCode() != tgtok::r_brace) {
1927 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001928 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001929 }
1930 Lex.Lex();
1931 break;
1932 }
1933 }
David Greenefb927af2012-02-22 16:09:41 +00001934
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001935 if (!Ranges.empty()) {
1936 assert(!IterType && "Type already initialized?");
1937 IterType = IntRecTy::get();
1938 std::vector<Init*> Values;
Craig Topper8eb764c2015-06-02 06:19:28 +00001939 for (unsigned R : Ranges)
1940 Values.push_back(IntInit::get(R));
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001941 ForeachListValue = ListInit::get(Values, IterType);
1942 }
1943
1944 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001945 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001946
1947 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001948}
1949
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001950/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1951/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1952/// template args for a def, which may or may not be in a multiclass. If null,
1953/// these are the template args for a multiclass.
1954///
1955/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001956///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001957bool TGParser::ParseTemplateArgList(Record *CurRec) {
1958 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1959 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001960
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001961 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001962
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001963 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001964 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001965 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001966 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001967
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001968 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001969
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001970 while (Lex.getCode() == tgtok::comma) {
1971 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001972
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001973 // Read the following declarations.
1974 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001975 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001976 return true;
1977 TheRecToAddTo->addTemplateArg(TemplArg);
1978 }
Bob Wilson7248f862009-11-22 04:24:42 +00001979
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001980 if (Lex.getCode() != tgtok::greater)
1981 return TokError("expected '>' at end of template argument list");
1982 Lex.Lex(); // eat the '>'.
1983 return false;
1984}
1985
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001986/// ParseBodyItem - Parse a single item at within the body of a def or class.
1987///
1988/// BodyItem ::= Declaration ';'
1989/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1990bool TGParser::ParseBodyItem(Record *CurRec) {
1991 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001992 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001993 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001994
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001995 if (Lex.getCode() != tgtok::semi)
1996 return TokError("expected ';' after declaration");
1997 Lex.Lex();
1998 return false;
1999 }
2000
2001 // LET ID OptionalRangeList '=' Value ';'
2002 if (Lex.Lex() != tgtok::Id)
2003 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00002004
Chris Lattner526c8cb2009-06-21 03:39:35 +00002005 SMLoc IdLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00002006 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002007 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00002008
Matthias Braunc66e7552016-12-05 06:41:54 +00002009 SmallVector<unsigned, 16> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00002010 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002011 return true;
2012 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002013
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002014 if (Lex.getCode() != tgtok::equal)
2015 return TokError("expected '=' in let expression");
2016 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002017
David Greene8618f952009-06-08 20:23:18 +00002018 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00002019 if (!Field)
Matthias Braun215ff842016-12-05 07:35:13 +00002020 return TokError("Value '" + FieldName->getValue() + "' unknown!");
David Greene8618f952009-06-08 20:23:18 +00002021
2022 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00002023
David Greeneaf8ee2c2011-07-29 22:43:06 +00002024 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00002025 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002026
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002027 if (Lex.getCode() != tgtok::semi)
2028 return TokError("expected ';' after let expression");
2029 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002030
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002031 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
2032}
2033
2034/// ParseBody - Read the body of a class or def. Return true on error, false on
2035/// success.
2036///
2037/// Body ::= ';'
2038/// Body ::= '{' BodyList '}'
2039/// BodyList BodyItem*
2040///
2041bool TGParser::ParseBody(Record *CurRec) {
2042 // If this is a null definition, just eat the semi and return.
2043 if (Lex.getCode() == tgtok::semi) {
2044 Lex.Lex();
2045 return false;
2046 }
Bob Wilson7248f862009-11-22 04:24:42 +00002047
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002048 if (Lex.getCode() != tgtok::l_brace)
2049 return TokError("Expected ';' or '{' to start body");
2050 // Eat the '{'.
2051 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002052
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002053 while (Lex.getCode() != tgtok::r_brace)
2054 if (ParseBodyItem(CurRec))
2055 return true;
2056
2057 // Eat the '}'.
2058 Lex.Lex();
2059 return false;
2060}
2061
Sean Silvacb1a75e2013-01-09 04:49:14 +00002062/// \brief Apply the current let bindings to \a CurRec.
2063/// \returns true on error, false otherwise.
2064bool TGParser::ApplyLetStack(Record *CurRec) {
Matthias Braunc66e7552016-12-05 06:41:54 +00002065 for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
Craig Topper8eb764c2015-06-02 06:19:28 +00002066 for (LetRecord &LR : LetInfo)
2067 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
Sean Silvacb1a75e2013-01-09 04:49:14 +00002068 return true;
2069 return false;
2070}
2071
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002072/// ParseObjectBody - Parse the body of a def or class. This consists of an
2073/// optional ClassList followed by a Body. CurRec is the current def or class
2074/// that is being parsed.
2075///
2076/// ObjectBody ::= BaseClassList Body
2077/// BaseClassList ::= /*empty*/
2078/// BaseClassList ::= ':' BaseClassListNE
2079/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
2080///
2081bool TGParser::ParseObjectBody(Record *CurRec) {
2082 // If there is a baseclass list, read it.
2083 if (Lex.getCode() == tgtok::colon) {
2084 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002085
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002086 // Read all of the subclasses.
2087 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002088 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002089 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002090 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002091
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002092 // Add it.
2093 if (AddSubClass(CurRec, SubClass))
2094 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002095
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002096 if (Lex.getCode() != tgtok::comma) break;
2097 Lex.Lex(); // eat ','.
2098 SubClass = ParseSubClassReference(CurRec, false);
2099 }
2100 }
2101
Sean Silvacb1a75e2013-01-09 04:49:14 +00002102 if (ApplyLetStack(CurRec))
2103 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002104
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002105 return ParseBody(CurRec);
2106}
2107
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002108/// ParseDef - Parse and return a top level or multiclass def, return the record
2109/// corresponding to it. This returns null on error.
2110///
2111/// DefInst ::= DEF ObjectName ObjectBody
2112///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002113bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00002114 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002115 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00002116 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002117
2118 // Parse ObjectName and make a record for it.
Craig Topper84138712014-11-29 05:31:10 +00002119 std::unique_ptr<Record> CurRecOwner;
Jordan Roseabdd99b2013-01-10 18:50:05 +00002120 Init *Name = ParseObjectName(CurMultiClass);
2121 if (Name)
Craig Topper84138712014-11-29 05:31:10 +00002122 CurRecOwner = make_unique<Record>(Name, DefLoc, Records);
Jordan Roseabdd99b2013-01-10 18:50:05 +00002123 else
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00002124 CurRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), DefLoc,
2125 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00002126 Record *CurRec = CurRecOwner.get(); // Keep a copy since we may release.
Bob Wilson7248f862009-11-22 04:24:42 +00002127
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002128 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002129 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002130
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002131 // Ensure redefinition doesn't happen.
Craig Topper84138712014-11-29 05:31:10 +00002132 if (Records.getDef(CurRec->getNameInitAsString()))
2133 return Error(DefLoc, "def '" + CurRec->getNameInitAsString()+
2134 "' already defined");
Craig Toppercdab2322014-11-29 05:52:51 +00002135 Records.addDef(std::move(CurRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00002136
2137 if (ParseObjectBody(CurRec))
2138 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002139 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002140 // Parse the body before adding this prototype to the DefPrototypes vector.
2141 // That way implicit definitions will be added to the DefPrototypes vector
2142 // before this object, instantiated prior to defs derived from this object,
2143 // and this available for indirect name resolution when defs derived from
2144 // this object are instantiated.
Craig Topper84138712014-11-29 05:31:10 +00002145 if (ParseObjectBody(CurRec))
Hal Finkela8c1f462014-01-02 20:47:09 +00002146 return true;
2147
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002148 // Otherwise, a def inside a multiclass, add it to the multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002149 for (const auto &Proto : CurMultiClass->DefPrototypes)
2150 if (Proto->getNameInit() == CurRec->getNameInit())
Craig Topper84138712014-11-29 05:31:10 +00002151 return Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
2152 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002153 CurMultiClass->DefPrototypes.push_back(std::move(CurRecOwner));
Anton Yartsev671dff12014-08-08 00:29:54 +00002154 } else if (ParseObjectBody(CurRec)) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002155 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002156 }
Bob Wilson7248f862009-11-22 04:24:42 +00002157
Craig Topper011817a2014-04-09 04:50:04 +00002158 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002159 // See Record::setName(). This resolve step will see any new name
2160 // for the def that might have been created when resolving
2161 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002162 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002163
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002164 // If ObjectBody has template arguments, it's an error.
2165 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002166
2167 if (CurMultiClass) {
2168 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002169 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
2170 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002171 assert(RV && "Template arg doesn't exist?");
2172 CurRec->addValue(*RV);
2173 }
2174 }
2175
Craig Toppera9642b42015-05-04 01:35:39 +00002176 if (ProcessForeachDefs(CurRec, DefLoc))
Craig Topper84138712014-11-29 05:31:10 +00002177 return Error(DefLoc, "Could not process loops for def" +
2178 CurRec->getNameInitAsString());
David Greenefb927af2012-02-22 16:09:41 +00002179
2180 return false;
2181}
2182
2183/// ParseForeach - Parse a for statement. Return the record corresponding
2184/// to it. This returns true on error.
2185///
2186/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2187/// Foreach ::= FOREACH Declaration IN Object
2188///
2189bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2190 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2191 Lex.Lex(); // Eat the 'for' token.
2192
2193 // Make a temporary object to record items associated with the for
2194 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002195 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002196 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002197 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002198 return TokError("expected declaration in for");
2199
2200 if (Lex.getCode() != tgtok::In)
2201 return TokError("Unknown tok");
2202 Lex.Lex(); // Eat the in
2203
2204 // Create a loop object and remember it.
2205 Loops.push_back(ForeachLoop(IterName, ListValue));
2206
2207 if (Lex.getCode() != tgtok::l_brace) {
2208 // FOREACH Declaration IN Object
2209 if (ParseObject(CurMultiClass))
2210 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002211 } else {
David Greenefb927af2012-02-22 16:09:41 +00002212 SMLoc BraceLoc = Lex.getLoc();
2213 // Otherwise, this is a group foreach.
2214 Lex.Lex(); // eat the '{'.
2215
2216 // Parse the object list.
2217 if (ParseObjectList(CurMultiClass))
2218 return true;
2219
2220 if (Lex.getCode() != tgtok::r_brace) {
2221 TokError("expected '}' at end of foreach command");
2222 return Error(BraceLoc, "to match this '{'");
2223 }
2224 Lex.Lex(); // Eat the }
2225 }
2226
2227 // We've processed everything in this loop.
2228 Loops.pop_back();
2229
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002230 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002231}
2232
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002233/// ParseClass - Parse a tblgen class definition.
2234///
2235/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2236///
2237bool TGParser::ParseClass() {
2238 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2239 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002240
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002241 if (Lex.getCode() != tgtok::Id)
2242 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002243
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002244 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2245 if (CurRec) {
2246 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002247 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002248 !CurRec->getSuperClasses().empty() ||
2249 !CurRec->getTemplateArgs().empty())
Craig Topper85c07002015-04-30 05:54:22 +00002250 return TokError("Class '" + CurRec->getNameInitAsString() +
2251 "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002252 } else {
2253 // If this is the first reference to this class, create and add it.
Hans Wennborgffbbd532014-11-30 00:31:49 +00002254 auto NewRec =
2255 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
Craig Toppercdab2322014-11-29 05:52:51 +00002256 CurRec = NewRec.get();
2257 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002258 }
2259 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002260
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002261 // If there are template args, parse them.
2262 if (Lex.getCode() == tgtok::less)
2263 if (ParseTemplateArgList(CurRec))
2264 return true;
2265
2266 // Finally, parse the object body.
2267 return ParseObjectBody(CurRec);
2268}
2269
2270/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2271/// of LetRecords.
2272///
2273/// LetList ::= LetItem (',' LetItem)*
2274/// LetItem ::= ID OptionalRangeList '=' Value
2275///
Matthias Braunc66e7552016-12-05 06:41:54 +00002276void TGParser::ParseLetList(SmallVectorImpl<LetRecord> &Result) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002277 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002278 if (Lex.getCode() != tgtok::Id) {
2279 TokError("expected identifier in let definition");
Matthias Braunc66e7552016-12-05 06:41:54 +00002280 Result.clear();
2281 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002282 }
Matthias Braunc66e7552016-12-05 06:41:54 +00002283
Matthias Braun215ff842016-12-05 07:35:13 +00002284 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattner526c8cb2009-06-21 03:39:35 +00002285 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002286 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002287
2288 // Check for an optional RangeList.
Matthias Braunc66e7552016-12-05 06:41:54 +00002289 SmallVector<unsigned, 16> Bits;
2290 if (ParseOptionalRangeList(Bits)) {
2291 Result.clear();
2292 return;
2293 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002294 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002295
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002296 if (Lex.getCode() != tgtok::equal) {
2297 TokError("expected '=' in let expression");
Matthias Braunc66e7552016-12-05 06:41:54 +00002298 Result.clear();
2299 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002300 }
2301 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002302
Craig Topper011817a2014-04-09 04:50:04 +00002303 Init *Val = ParseValue(nullptr);
Matthias Braunc66e7552016-12-05 06:41:54 +00002304 if (!Val) {
2305 Result.clear();
2306 return;
2307 }
Bob Wilson7248f862009-11-22 04:24:42 +00002308
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002309 // Now that we have everything, add the record.
Matthias Braun215ff842016-12-05 07:35:13 +00002310 Result.emplace_back(Name, Bits, Val, NameLoc);
Bob Wilson7248f862009-11-22 04:24:42 +00002311
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002312 if (Lex.getCode() != tgtok::comma)
Matthias Braunc66e7552016-12-05 06:41:54 +00002313 return;
Bob Wilson7248f862009-11-22 04:24:42 +00002314 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002315 }
2316}
2317
2318/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002319/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002320///
2321/// Object ::= LET LetList IN '{' ObjectList '}'
2322/// Object ::= LET LetList IN Object
2323///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002324bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002325 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2326 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002327
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002328 // Add this entry to the let stack.
Matthias Braunc66e7552016-12-05 06:41:54 +00002329 SmallVector<LetRecord, 8> LetInfo;
2330 ParseLetList(LetInfo);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002331 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002332 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002333
2334 if (Lex.getCode() != tgtok::In)
2335 return TokError("expected 'in' at end of top-level 'let'");
2336 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002337
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002338 // If this is a scalar let, just handle it now
2339 if (Lex.getCode() != tgtok::l_brace) {
2340 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002341 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002342 return true;
2343 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002344 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002345 // Otherwise, this is a group let.
2346 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002347
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002348 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002349 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002350 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002351
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002352 if (Lex.getCode() != tgtok::r_brace) {
2353 TokError("expected '}' at end of top level let command");
2354 return Error(BraceLoc, "to match this '{'");
2355 }
2356 Lex.Lex();
2357 }
Bob Wilson7248f862009-11-22 04:24:42 +00002358
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002359 // Outside this let scope, this let block is not active.
2360 LetStack.pop_back();
2361 return false;
2362}
2363
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002364/// ParseMultiClass - Parse a multiclass definition.
2365///
Bob Wilson3d948162009-04-28 19:41:44 +00002366/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002367/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2368/// MultiClassObject ::= DefInst
2369/// MultiClassObject ::= MultiClassInst
2370/// MultiClassObject ::= DefMInst
2371/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2372/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002373///
2374bool TGParser::ParseMultiClass() {
2375 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2376 Lex.Lex(); // Eat the multiclass token.
2377
2378 if (Lex.getCode() != tgtok::Id)
2379 return TokError("expected identifier after multiclass for name");
2380 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002381
Craig Topper7adf2bf2014-12-11 05:25:30 +00002382 auto Result =
2383 MultiClasses.insert(std::make_pair(Name,
2384 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2385
2386 if (!Result.second)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002387 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002388
Craig Topper7adf2bf2014-12-11 05:25:30 +00002389 CurMultiClass = Result.first->second.get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002390 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002391
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002392 // If there are template args, parse them.
2393 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002394 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002395 return true;
2396
David Greene7049e792009-04-24 16:55:41 +00002397 bool inherits = false;
2398
David Greene753ed8f2009-04-22 16:42:54 +00002399 // If there are submulticlasses, parse them.
2400 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002401 inherits = true;
2402
David Greene753ed8f2009-04-22 16:42:54 +00002403 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002404
David Greene753ed8f2009-04-22 16:42:54 +00002405 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002406 SubMultiClassReference SubMultiClass =
2407 ParseSubMultiClassReference(CurMultiClass);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002408 while (true) {
David Greene753ed8f2009-04-22 16:42:54 +00002409 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002410 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002411
David Greene753ed8f2009-04-22 16:42:54 +00002412 // Add it.
2413 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2414 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002415
David Greene753ed8f2009-04-22 16:42:54 +00002416 if (Lex.getCode() != tgtok::comma) break;
2417 Lex.Lex(); // eat ','.
2418 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2419 }
2420 }
2421
David Greene7049e792009-04-24 16:55:41 +00002422 if (Lex.getCode() != tgtok::l_brace) {
2423 if (!inherits)
2424 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002425 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002426 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002427 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002428 } else {
David Greene7049e792009-04-24 16:55:41 +00002429 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2430 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002431
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002432 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002433 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002434 default:
2435 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
2436 case tgtok::Let:
2437 case tgtok::Def:
2438 case tgtok::Defm:
2439 case tgtok::Foreach:
2440 if (ParseObject(CurMultiClass))
2441 return true;
2442 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002443 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002444 }
David Greene7049e792009-04-24 16:55:41 +00002445 Lex.Lex(); // eat the '}'.
2446 }
Bob Wilson7248f862009-11-22 04:24:42 +00002447
Craig Topper011817a2014-04-09 04:50:04 +00002448 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002449 return false;
2450}
2451
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002452Record *TGParser::InstantiateMulticlassDef(MultiClass &MC, Record *DefProto,
2453 Init *&DefmPrefix,
2454 SMRange DefmPrefixRange,
2455 ArrayRef<Init *> TArgs,
Matthias Braunc66e7552016-12-05 06:41:54 +00002456 ArrayRef<Init *> TemplateVals) {
David Greene5d5d88c2011-10-19 13:04:31 +00002457 // We need to preserve DefProto so it can be reused for later
2458 // instantiations, so create a new Record to inherit from it.
2459
David Greenedb445972011-10-05 22:42:07 +00002460 // Add in the defm name. If the defm prefix is empty, give each
2461 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2462 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2463 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002464
Jordan Roseabdd99b2013-01-10 18:50:05 +00002465 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002466 if (!DefmPrefix) {
Craig Topperf4412262017-06-01 06:56:16 +00002467 DefmPrefix = GetNewAnonymousName();
Jordan Roseabdd99b2013-01-10 18:50:05 +00002468 IsAnonymous = true;
2469 }
David Greene5d5d88c2011-10-19 13:04:31 +00002470
2471 Init *DefName = DefProto->getNameInit();
Sean Silvafb509ed2012-10-10 20:24:43 +00002472 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002473
Craig Topper011817a2014-04-09 04:50:04 +00002474 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002475 // We have a fully expanded string so there are no operators to
2476 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002477 DefName =
2478 BinOpInit::get(BinOpInit::STRCONCAT,
2479 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2480 StringRecTy::get())->Fold(DefProto, &MC),
2481 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2482 }
David Greene5d5d88c2011-10-19 13:04:31 +00002483
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002484 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002485 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002486 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Craig Topper84138712014-11-29 05:31:10 +00002487 auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002488
2489 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002490 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002491 Ref.Rec = DefProto;
Craig Topper84138712014-11-29 05:31:10 +00002492 AddSubClass(CurRec.get(), Ref);
David Greenedb445972011-10-05 22:42:07 +00002493
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002494 // Set the value for NAME. We don't resolve references to it 'til later,
2495 // though, so that uses in nested multiclass names don't get
2496 // confused.
Matthias Braun215ff842016-12-05 07:35:13 +00002497 if (SetValue(CurRec.get(), Ref.RefRange.Start, StringInit::get("NAME"), None,
2498 DefmPrefix, /*AllowSelfAssignment*/true)) {
Craig Topper85c07002015-04-30 05:54:22 +00002499 Error(DefmPrefixRange.Start, "Could not resolve " +
2500 CurRec->getNameInitAsString() + ":NAME to '" +
2501 DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002502 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002503 }
David Greene8bf0d722011-10-19 13:04:35 +00002504
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002505 // If the DefNameString didn't resolve, we probably have a reference to
2506 // NAME and need to replace it. We need to do at least this much greedily,
2507 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002508 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002509 RecordVal *DefNameRV = CurRec->getValue("NAME");
2510 CurRec->resolveReferencesTo(DefNameRV);
2511 }
2512
2513 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002514 // Now that we're at the top level, resolve all NAME references
2515 // in the resultant defs that weren't in the def names themselves.
2516 RecordVal *DefNameRV = CurRec->getValue("NAME");
2517 CurRec->resolveReferencesTo(DefNameRV);
2518
Hal Finkeld2497362015-05-21 04:32:56 +00002519 // Check if the name is a complex pattern.
2520 // If so, resolve it.
2521 DefName = CurRec->getNameInit();
2522 DefNameString = dyn_cast<StringInit>(DefName);
2523
2524 // OK the pattern is more complex than simply using NAME.
2525 // Let's use the heavy weaponery.
2526 if (!DefNameString) {
2527 ResolveMulticlassDefArgs(MC, CurRec.get(), DefmPrefixRange.Start,
2528 Lex.getLoc(), TArgs, TemplateVals,
2529 false/*Delete args*/);
2530 DefName = CurRec->getNameInit();
2531 DefNameString = dyn_cast<StringInit>(DefName);
2532
2533 if (!DefNameString)
2534 DefName = DefName->convertInitializerTo(StringRecTy::get());
2535
2536 // We ran out of options here...
2537 DefNameString = dyn_cast<StringInit>(DefName);
2538 if (!DefNameString) {
2539 PrintFatalError(CurRec->getLoc()[CurRec->getLoc().size() - 1],
2540 DefName->getAsUnquotedString() + " is not a string.");
2541 return nullptr;
2542 }
2543
2544 CurRec->setName(DefName);
2545 }
2546
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002547 // Now that NAME references are resolved and we're at the top level of
2548 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002549 // currently in a multiclass, it means this defm appears inside a
2550 // multiclass and its name won't be fully resolvable until we see
Hal Finkeld2497362015-05-21 04:32:56 +00002551 // the top-level defm. Therefore, we don't add this to the
2552 // RecordKeeper at this point. If we did we could get duplicate
David Greene8bf0d722011-10-19 13:04:35 +00002553 // defs as more than one probably refers to NAME or some other
2554 // common internal placeholder.
2555
2556 // Ensure redefinition doesn't happen.
2557 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002558 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002559 "' already defined, instantiating defm with subdef '" +
2560 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002561 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002562 }
2563
Craig Topper84138712014-11-29 05:31:10 +00002564 Record *CurRecSave = CurRec.get(); // Keep a copy before we release.
Craig Toppercdab2322014-11-29 05:52:51 +00002565 Records.addDef(std::move(CurRec));
Craig Topper84138712014-11-29 05:31:10 +00002566 return CurRecSave;
David Greene8bf0d722011-10-19 13:04:35 +00002567 }
2568
Craig Topper84138712014-11-29 05:31:10 +00002569 // FIXME This is bad but the ownership transfer to caller is pretty messy.
2570 // The unique_ptr in this function at least protects the exits above.
2571 return CurRec.release();
David Greenedb445972011-10-05 22:42:07 +00002572}
2573
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002574bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC, Record *CurRec,
2575 SMLoc DefmPrefixLoc, SMLoc SubClassLoc,
2576 ArrayRef<Init *> TArgs,
Matthias Braunc66e7552016-12-05 06:41:54 +00002577 ArrayRef<Init *> TemplateVals,
David Greenedb445972011-10-05 22:42:07 +00002578 bool DeleteArgs) {
2579 // Loop over all of the template arguments, setting them to the specified
2580 // value or leaving them as the default if necessary.
2581 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()) {
2584 // Set it now.
Craig Toppercfd81732016-01-04 03:15:08 +00002585 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], None, TemplateVals[i]))
David Greenedb445972011-10-05 22:42:07 +00002586 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002587
David Greenedb445972011-10-05 22:42:07 +00002588 // Resolve it next.
2589 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2590
2591 if (DeleteArgs)
2592 // Now remove it.
2593 CurRec->removeValue(TArgs[i]);
Craig Toppera9642b42015-05-04 01:35:39 +00002594
David Greenedb445972011-10-05 22:42:07 +00002595 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Craig Topper85c07002015-04-30 05:54:22 +00002596 return Error(SubClassLoc, "value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00002597 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +00002598 ") of multiclassclass '" + MC.Rec.getNameInitAsString() +
2599 "'");
David Greenedb445972011-10-05 22:42:07 +00002600 }
2601 }
2602 return false;
2603}
2604
2605bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2606 Record *CurRec,
2607 Record *DefProto,
2608 SMLoc DefmPrefixLoc) {
2609 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002610 if (ApplyLetStack(CurRec))
2611 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002612
David Greenedb445972011-10-05 22:42:07 +00002613 // Don't create a top level definition for defm inside multiclasses,
2614 // instead, only update the prototypes and bind the template args
2615 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002616 if (!CurMultiClass)
2617 return false;
Craig Toppereb4d7c62015-04-29 04:43:36 +00002618 for (const auto &Proto : CurMultiClass->DefPrototypes)
2619 if (Proto->getNameInit() == CurRec->getNameInit())
Sean Silvacc951b22013-01-09 05:28:12 +00002620 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2621 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002622 CurMultiClass->DefPrototypes.push_back(std::unique_ptr<Record>(CurRec));
David Greenedb445972011-10-05 22:42:07 +00002623
Sean Silvacc951b22013-01-09 05:28:12 +00002624 // Copy the template arguments for the multiclass into the new def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002625 for (Init * TA : CurMultiClass->Rec.getTemplateArgs()) {
2626 const RecordVal *RV = CurMultiClass->Rec.getValue(TA);
Sean Silvacc951b22013-01-09 05:28:12 +00002627 assert(RV && "Template arg doesn't exist?");
2628 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002629 }
2630
2631 return false;
2632}
2633
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002634/// ParseDefm - Parse the instantiation of a multiclass.
2635///
2636/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2637///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002638bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002639 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002640 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002641 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002642
Craig Topperb21afc62013-01-07 05:09:33 +00002643 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002644 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002645 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002646
Jordan Rosef12e8a92013-01-10 18:50:11 +00002647 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002648 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002649 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002650
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002651 // Keep track of the new generated record definitions.
2652 std::vector<Record*> NewRecDefs;
2653
2654 // This record also inherits from a regular class (non-multiclass)?
2655 bool InheritFromClass = false;
2656
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002657 // eat the colon.
2658 Lex.Lex();
2659
Chris Lattner526c8cb2009-06-21 03:39:35 +00002660 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002661 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002662
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002663 while (true) {
Craig Topper011817a2014-04-09 04:50:04 +00002664 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002665
2666 // To instantiate a multiclass, we need to first get the multiclass, then
2667 // instantiate each def contained in the multiclass with the SubClassRef
2668 // template parameters.
Craig Topper7adf2bf2014-12-11 05:25:30 +00002669 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper4ca40012014-11-30 01:20:17 +00002670 assert(MC && "Didn't lookup multiclass correctly?");
Matthias Braunc66e7552016-12-05 06:41:54 +00002671 ArrayRef<Init*> TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002672
2673 // Verify that the correct number of template arguments were specified.
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002674 ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002675 if (TArgs.size() < TemplateVals.size())
2676 return Error(SubClassLoc,
2677 "more template args specified than multiclass expects");
2678
2679 // Loop over all the def's in the multiclass, instantiating each one.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002680 for (const std::unique_ptr<Record> &DefProto : MC->DefPrototypes) {
Hal Finkeld2497362015-05-21 04:32:56 +00002681 // The record name construction goes as follow:
2682 // - If the def name is a string, prepend the prefix.
2683 // - If the def name is a more complex pattern, use that pattern.
Craig Topper7f36be92016-02-26 06:50:27 +00002684 // As a result, the record is instantiated before resolving
Hal Finkeld2497362015-05-21 04:32:56 +00002685 // arguments, as it would make its name a string.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002686 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto.get(), DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002687 SMRange(DefmLoc,
Hal Finkeld2497362015-05-21 04:32:56 +00002688 DefmPrefixEndLoc),
2689 TArgs, TemplateVals);
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002690 if (!CurRec)
2691 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002692
Craig Topper7f36be92016-02-26 06:50:27 +00002693 // Now that the record is instantiated, we can resolve arguments.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002694 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002695 TArgs, TemplateVals, true/*Delete args*/))
2696 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002697
Craig Toppereb4d7c62015-04-29 04:43:36 +00002698 if (ResolveMulticlassDef(*MC, CurRec, DefProto.get(), DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002699 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002700
Adam Nemete5a07162014-09-16 17:14:13 +00002701 // Defs that can be used by other definitions should be fully resolved
2702 // before any use.
2703 if (DefProto->isResolveFirst() && !CurMultiClass) {
2704 CurRec->resolveReferences();
2705 CurRec->setResolveFirst(false);
2706 }
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002707 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002708 }
2709
David Greenedb445972011-10-05 22:42:07 +00002710
David Greenef00919a2009-04-22 22:17:51 +00002711 if (Lex.getCode() != tgtok::comma) break;
2712 Lex.Lex(); // eat ','.
2713
Craig Topper998a39a2013-08-20 04:22:09 +00002714 if (Lex.getCode() != tgtok::Id)
2715 return TokError("expected identifier");
2716
David Greenef00919a2009-04-22 22:17:51 +00002717 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002718
2719 // A defm can inherit from regular classes (non-multiclass) as
2720 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002721 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002722
2723 if (InheritFromClass)
2724 break;
2725
Craig Topper011817a2014-04-09 04:50:04 +00002726 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002727 }
2728
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002729 if (InheritFromClass) {
2730 // Process all the classes to inherit as if they were part of a
2731 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002732 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002733 while (true) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002734 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002735 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002736
2737 // Get the expanded definition prototypes and teach them about
2738 // the record values the current class to inherit has
Craig Toppereb4d7c62015-04-29 04:43:36 +00002739 for (Record *CurRec : NewRecDefs) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002740 // Add it.
2741 if (AddSubClass(CurRec, SubClass))
2742 return true;
2743
Sean Silvacb1a75e2013-01-09 04:49:14 +00002744 if (ApplyLetStack(CurRec))
2745 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002746 }
2747
2748 if (Lex.getCode() != tgtok::comma) break;
2749 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002750 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002751 }
2752 }
2753
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002754 if (!CurMultiClass)
Craig Toppereb4d7c62015-04-29 04:43:36 +00002755 for (Record *CurRec : NewRecDefs)
David Greene50c09122011-08-10 18:27:46 +00002756 // See Record::setName(). This resolve step will see any new
2757 // name for the def that might have been created when resolving
2758 // inheritance, values and arguments above.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002759 CurRec->resolveReferences();
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002760
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002761 if (Lex.getCode() != tgtok::semi)
2762 return TokError("expected ';' at end of defm");
2763 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002764
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002765 return false;
2766}
2767
2768/// ParseObject
2769/// Object ::= ClassInst
2770/// Object ::= DefInst
2771/// Object ::= MultiClassInst
2772/// Object ::= DefMInst
2773/// Object ::= LETCommand '{' ObjectList '}'
2774/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002775bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002776 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002777 default:
2778 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002779 case tgtok::Let: return ParseTopLevelLet(MC);
2780 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002781 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002782 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002783 case tgtok::Class: return ParseClass();
2784 case tgtok::MultiClass: return ParseMultiClass();
2785 }
2786}
2787
2788/// ParseObjectList
2789/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002790bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002791 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002792 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002793 return true;
2794 }
2795 return false;
2796}
2797
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002798bool TGParser::ParseFile() {
2799 Lex.Lex(); // Prime the lexer.
2800 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002801
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002802 // If we have unread input at the end of the file, report it.
2803 if (Lex.getCode() == tgtok::Eof)
2804 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002805
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002806 return TokError("Unexpected input at top level");
2807}