blob: 642812d5cebd585dcd9016b98c4fd9bf89c7fccc [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
Nicolai Haehnle0f529882018-03-06 13:48:47 +000071static bool checkBitsConcrete(Record &R, const RecordVal &RV) {
72 BitsInit *BV = cast<BitsInit>(RV.getValue());
73 for (unsigned i = 0, e = BV->getNumBits(); i != e; ++i) {
74 Init *Bit = BV->getBit(i);
75 bool IsReference = false;
76 if (auto VBI = dyn_cast<VarBitInit>(Bit)) {
77 if (auto VI = dyn_cast<VarInit>(VBI->getBitVar())) {
78 if (R.getValue(VI->getName()))
79 IsReference = true;
80 }
81 } else if (isa<VarInit>(Bit)) {
82 IsReference = true;
83 }
84 if (!(IsReference || Bit->isConcrete()))
85 return false;
86 }
87 return true;
88}
89
90static void checkConcrete(Record &R) {
91 for (const RecordVal &RV : R.getValues()) {
92 // HACK: Disable this check for variables declared with 'field'. This is
93 // done merely because existing targets have legitimate cases of
94 // non-concrete variables in helper defs. Ideally, we'd introduce a
95 // 'maybe' or 'optional' modifier instead of this.
96 if (RV.getPrefix())
97 continue;
98
99 if (Init *V = RV.getValue()) {
100 bool Ok = isa<BitsInit>(V) ? checkBitsConcrete(R, RV) : V->isConcrete();
101 if (!Ok) {
102 PrintError(R.getLoc(),
103 Twine("Initializer of '") + RV.getNameInitAsString() +
104 "' in '" + R.getNameInitAsString() +
105 "' could not be fully resolved: " +
106 RV.getValue()->getAsString());
107 }
108 }
109 }
110}
111
Chris Lattner526c8cb2009-06-21 03:39:35 +0000112bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Craig Topper011817a2014-04-09 04:50:04 +0000113 if (!CurRec)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000114 CurRec = &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +0000115
Jakob Stoklund Olesen9d1c5ee2012-01-13 03:16:35 +0000116 if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000117 // The value already exists in the class, treat this as a set.
118 if (ERV->setValue(RV.getValue()))
119 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
120 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson7248f862009-11-22 04:24:42 +0000121 "previous definition of type '" +
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000122 ERV->getType()->getAsString() + "'");
123 } else {
124 CurRec->addValue(RV);
125 }
126 return false;
127}
128
129/// SetValue -
130/// Return true on error, false on success.
David Greene3ca42122011-10-19 13:02:39 +0000131bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
Craig Toppercfd81732016-01-04 03:15:08 +0000132 ArrayRef<unsigned> BitList, Init *V,
Craig Topper1e23ed92016-01-04 03:05:14 +0000133 bool AllowSelfAssignment) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000134 if (!V) return false;
135
Craig Topper011817a2014-04-09 04:50:04 +0000136 if (!CurRec) CurRec = &CurMultiClass->Rec;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000137
138 RecordVal *RV = CurRec->getValue(ValName);
Craig Topper011817a2014-04-09 04:50:04 +0000139 if (!RV)
Craig Topper85c07002015-04-30 05:54:22 +0000140 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
141 "' unknown!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000142
143 // Do not allow assignments like 'X = X'. This will just cause infinite loops
144 // in the resolution machinery.
145 if (BitList.empty())
Sean Silvafb509ed2012-10-10 20:24:43 +0000146 if (VarInit *VI = dyn_cast<VarInit>(V))
Craig Topper1e23ed92016-01-04 03:05:14 +0000147 if (VI->getNameInit() == ValName && !AllowSelfAssignment)
Nicolai Haehnlef19083d2018-02-22 15:26:21 +0000148 return Error(Loc, "Recursion / self-assignment forbidden");
Bob Wilson7248f862009-11-22 04:24:42 +0000149
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000150 // If we are assigning to a subset of the bits in the value... then we must be
151 // assigning to a field of BitsRecTy, which must have a BitsInit
152 // initializer.
153 //
154 if (!BitList.empty()) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000155 BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue());
Craig Topper011817a2014-04-09 04:50:04 +0000156 if (!CurVal)
Craig Topper85c07002015-04-30 05:54:22 +0000157 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
158 "' is not a bits type");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000159
160 // Convert the incoming value to a bits type of the appropriate size...
Nicolai Haehnledfda9dc2018-03-06 13:48:39 +0000161 Init *BI = V->getCastTo(BitsRecTy::get(BitList.size()));
Craig Toppera9642b42015-05-04 01:35:39 +0000162 if (!BI)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000163 return Error(Loc, "Initializer is not compatible with bit range");
Bob Wilson7248f862009-11-22 04:24:42 +0000164
David Greeneaf8ee2c2011-07-29 22:43:06 +0000165 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000166
167 // Loop over bits, assigning values as appropriate.
168 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
169 unsigned Bit = BitList[i];
David Greeneb3da8122011-07-29 19:07:00 +0000170 if (NewBits[Bit])
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000171 return Error(Loc, "Cannot set bit #" + Twine(Bit) + " of value '" +
David Greene3ca42122011-10-19 13:02:39 +0000172 ValName->getAsUnquotedString() + "' more than once");
Nicolai Haehnledfda9dc2018-03-06 13:48:39 +0000173 NewBits[Bit] = BI->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000174 }
175
176 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
Craig Topper011817a2014-04-09 04:50:04 +0000177 if (!NewBits[i])
David Greeneb3da8122011-07-29 19:07:00 +0000178 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000179
David Greenee32ebf22011-07-29 19:07:07 +0000180 V = BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000181 }
182
Pete Cooper040c6a62014-07-31 01:43:57 +0000183 if (RV->setValue(V)) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000184 std::string InitType;
Craig Toppera9642b42015-05-04 01:35:39 +0000185 if (BitsInit *BI = dyn_cast<BitsInit>(V))
Pete Cooper040c6a62014-07-31 01:43:57 +0000186 InitType = (Twine("' of type bit initializer with length ") +
187 Twine(BI->getNumBits())).str();
Nicolai Haehnlef19083d2018-02-22 15:26:21 +0000188 else if (TypedInit *TI = dyn_cast<TypedInit>(V))
189 InitType = (Twine("' of type '") + TI->getType()->getAsString()).str();
Craig Topper85c07002015-04-30 05:54:22 +0000190 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
Nicolai Haehnlef19083d2018-02-22 15:26:21 +0000191 "' of type '" + RV->getType()->getAsString() +
192 "' is incompatible with initializer '" +
193 V->getAsString() + InitType + "'");
Pete Cooper040c6a62014-07-31 01:43:57 +0000194 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000195 return false;
196}
197
198/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
199/// args as SubClass's template arguments.
Cedric Venetd1e179d2009-02-14 16:06:42 +0000200bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000201 Record *SC = SubClass.Rec;
202 // Add all of the values in the subclass into the current class.
Craig Topper8eb764c2015-06-02 06:19:28 +0000203 for (const RecordVal &Val : SC->getValues())
204 if (AddValue(CurRec, SubClass.RefRange.Start, Val))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000205 return true;
206
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000207 ArrayRef<Init *> TArgs = SC->getTemplateArgs();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000208
209 // Ensure that an appropriate number of template arguments are specified.
210 if (TArgs.size() < SubClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000211 return Error(SubClass.RefRange.Start,
212 "More template args specified than expected");
Bob Wilson7248f862009-11-22 04:24:42 +0000213
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000214 // Loop over all of the template arguments, setting them to the specified
215 // value or leaving them as the default if necessary.
Nicolai Haehnleb0cf9e92018-03-05 15:21:11 +0000216 MapResolver R(CurRec);
217
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000218 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
219 if (i < SubClass.TemplateArgs.size()) {
220 // If a value is specified for this template arg, set it now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000221 if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000222 None, SubClass.TemplateArgs[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000223 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000224 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000225 return Error(SubClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000226 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000227 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000228 ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000229 }
Nicolai Haehnleb0cf9e92018-03-05 15:21:11 +0000230
231 R.set(TArgs[i], CurRec->getValue(TArgs[i])->getValue());
232
233 CurRec->removeValue(TArgs[i]);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000234 }
235
Nicolai Haehnleb0cf9e92018-03-05 15:21:11 +0000236 CurRec->resolveReferences(R);
237
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000238 // Since everything went well, we can now set the "superclass" list for the
239 // current record.
Craig Topper0e41d0b2016-01-18 19:52:37 +0000240 ArrayRef<std::pair<Record *, SMRange>> SCs = SC->getSuperClasses();
241 for (const auto &SCPair : SCs) {
242 if (CurRec->isSubClassOf(SCPair.first))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000243 return Error(SubClass.RefRange.Start,
Craig Topper0e41d0b2016-01-18 19:52:37 +0000244 "Already subclass of '" + SCPair.first->getName() + "'!\n");
245 CurRec->addSuperClass(SCPair.first, SCPair.second);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000246 }
Nicolai Haehnlec10570f2018-02-23 11:31:49 +0000247
248 if (CurRec->isSubClassOf(SC))
249 return Error(SubClass.RefRange.Start,
250 "Already subclass of '" + SC->getName() + "'!\n");
251 CurRec->addSuperClass(SC, SubClass.RefRange);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000252 return false;
253}
254
David Greene753ed8f2009-04-22 16:42:54 +0000255/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilsonf71e6562009-04-30 18:26:19 +0000256/// CurMC, resolving its template args as SubMultiClass's
David Greene753ed8f2009-04-22 16:42:54 +0000257/// template arguments.
Bob Wilsonf71e6562009-04-30 18:26:19 +0000258bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson92ab8202009-04-30 17:46:20 +0000259 SubMultiClassReference &SubMultiClass) {
David Greene753ed8f2009-04-22 16:42:54 +0000260 MultiClass *SMC = SubMultiClass.MC;
Bob Wilsonf71e6562009-04-30 18:26:19 +0000261 Record *CurRec = &CurMC->Rec;
David Greene753ed8f2009-04-22 16:42:54 +0000262
David Greene753ed8f2009-04-22 16:42:54 +0000263 // Add all of the values in the subclass into the current class.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000264 for (const auto &SMCVal : SMC->Rec.getValues())
265 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000266 return true;
267
Craig Toppera4ea4b02014-11-30 00:24:32 +0000268 unsigned newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000269
David Greene753ed8f2009-04-22 16:42:54 +0000270 // Add all of the defs in the subclass into the current multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000271 for (const std::unique_ptr<Record> &R : SMC->DefPrototypes) {
David Greene753ed8f2009-04-22 16:42:54 +0000272 // Clone the def and add it to the current multiclass
Craig Toppereb4d7c62015-04-29 04:43:36 +0000273 auto NewDef = make_unique<Record>(*R);
David Greene753ed8f2009-04-22 16:42:54 +0000274
275 // Add all of the values in the superclass into the current def.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000276 for (const auto &MCVal : CurRec->getValues())
277 if (AddValue(NewDef.get(), SubMultiClass.RefRange.Start, MCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000278 return true;
279
Craig Topperc3504c42014-12-11 05:25:33 +0000280 CurMC->DefPrototypes.push_back(std::move(NewDef));
David Greene753ed8f2009-04-22 16:42:54 +0000281 }
Bob Wilson3d948162009-04-28 19:41:44 +0000282
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000283 ArrayRef<Init *> SMCTArgs = SMC->Rec.getTemplateArgs();
David Greene753ed8f2009-04-22 16:42:54 +0000284
David Greene7049e792009-04-24 16:55:41 +0000285 // Ensure that an appropriate number of template arguments are
286 // specified.
David Greene753ed8f2009-04-22 16:42:54 +0000287 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000288 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000289 "More template args specified than expected");
Bob Wilson3d948162009-04-28 19:41:44 +0000290
David Greene753ed8f2009-04-22 16:42:54 +0000291 // Loop over all of the template arguments, setting them to the specified
292 // value or leaving them as the default if necessary.
Nicolai Haehnle1faf8682018-03-05 15:21:15 +0000293 MapResolver CurRecResolver(CurRec);
294
David Greene753ed8f2009-04-22 16:42:54 +0000295 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
296 if (i < SubMultiClass.TemplateArgs.size()) {
David Greene7049e792009-04-24 16:55:41 +0000297 // If a value is specified for this template arg, set it in the
298 // superclass now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000299 if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000300 None, SubMultiClass.TemplateArgs[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000301 return true;
302
David Greene7049e792009-04-24 16:55:41 +0000303 // If a value is specified for this template arg, set it in the
304 // new defs now.
Craig Topperc3504c42014-12-11 05:25:33 +0000305 for (const auto &Def :
306 makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
307 if (SetValue(Def.get(), SubMultiClass.RefRange.Start, SMCTArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000308 None, SubMultiClass.TemplateArgs[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000309 return true;
David Greene753ed8f2009-04-22 16:42:54 +0000310 }
311 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000312 return Error(SubMultiClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000313 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000314 Twine(i) + " (" + SMCTArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000315 ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000316 }
Nicolai Haehnle1faf8682018-03-05 15:21:15 +0000317
318 CurRecResolver.set(SMCTArgs[i], CurRec->getValue(SMCTArgs[i])->getValue());
319
320 CurRec->removeValue(SMCTArgs[i]);
321 }
322
323 CurRec->resolveReferences(CurRecResolver);
324
325 for (const auto &Def :
326 makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
327 MapResolver R(Def.get());
328
329 for (Init *SMCTArg : SMCTArgs) {
330 R.set(SMCTArg, Def->getValue(SMCTArg)->getValue());
331 Def->removeValue(SMCTArg);
332 }
333
334 Def->resolveReferences(R);
David Greene753ed8f2009-04-22 16:42:54 +0000335 }
336
337 return false;
338}
339
David Greenefb927af2012-02-22 16:09:41 +0000340/// ProcessForeachDefs - Given a record, apply all of the variable
341/// values in all surrounding foreach loops, creating new records for
342/// each combination of values.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000343bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
344 if (Loops.empty())
345 return false;
346
David Greenefb927af2012-02-22 16:09:41 +0000347 // We want to instantiate a new copy of CurRec for each combination
348 // of nested loop iterator values. We don't want top instantiate
349 // any copies until we have values for each loop iterator.
350 IterSet IterVals;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000351 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000352}
353
354/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
355/// apply each of the variable values in this loop and then process
356/// subloops.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000357bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
358 // Recursively build a tuple of iterator values.
359 if (IterVals.size() != Loops.size()) {
360 assert(IterVals.size() < Loops.size());
361 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silvafb509ed2012-10-10 20:24:43 +0000362 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Craig Topper011817a2014-04-09 04:50:04 +0000363 if (!List) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000364 Error(Loc, "Loop list is not a list");
365 return true;
366 }
David Greenefb927af2012-02-22 16:09:41 +0000367
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000368 // Process each value.
Craig Topper664f6a02015-06-02 04:15:57 +0000369 for (unsigned i = 0; i < List->size(); ++i) {
Nicolai Haehnle0b0eaf72018-03-05 15:20:51 +0000370 RecordResolver R(*CurRec);
371 Init *ItemVal = List->getElement(i)->resolveReferences(R);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000372 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
373 if (ProcessForeachDefs(CurRec, Loc, IterVals))
374 return true;
375 IterVals.pop_back();
376 }
377 return false;
378 }
379
380 // This is the bottom of the recursion. We have all of the iterator values
381 // for this point in the iteration space. Instantiate a new record to
382 // reflect this combination of values.
Craig Topper84138712014-11-29 05:31:10 +0000383 auto IterRec = make_unique<Record>(*CurRec);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000384
385 // Set the iterator values now.
Craig Topper8eb764c2015-06-02 06:19:28 +0000386 for (IterRecord &IR : IterVals) {
387 VarInit *IterVar = IR.IterVar;
388 TypedInit *IVal = dyn_cast<TypedInit>(IR.IterValue);
Craig Topper84138712014-11-29 05:31:10 +0000389 if (!IVal)
390 return Error(Loc, "foreach iterator value is untyped");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000391
Craig Topperf4412262017-06-01 06:56:16 +0000392 IterRec->addValue(RecordVal(IterVar->getNameInit(), IVal->getType(), false));
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000393
Matthias Braun215ff842016-12-05 07:35:13 +0000394 if (SetValue(IterRec.get(), Loc, IterVar->getNameInit(), None, IVal))
Craig Topper84138712014-11-29 05:31:10 +0000395 return Error(Loc, "when instantiating this def");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000396
397 // Resolve it next.
Matthias Braun215ff842016-12-05 07:35:13 +0000398 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getNameInit()));
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000399
400 // Remove it.
Matthias Braun215ff842016-12-05 07:35:13 +0000401 IterRec->removeValue(IterVar->getNameInit());
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000402 }
403
404 if (Records.getDef(IterRec->getNameInitAsString())) {
Artyom Skrobov8b985322014-06-10 12:41:14 +0000405 // If this record is anonymous, it's no problem, just generate a new name
Craig Topper84138712014-11-29 05:31:10 +0000406 if (!IterRec->isAnonymous())
407 return Error(Loc, "def already exists: " +IterRec->getNameInitAsString());
408
Nicolai Haehnle73355bc2018-03-06 13:48:54 +0000409 IterRec->setName(Records.getNewAnonymousName());
David Greenefb927af2012-02-22 16:09:41 +0000410 }
411
Craig Topper84138712014-11-29 05:31:10 +0000412 Record *IterRecSave = IterRec.get(); // Keep a copy before release.
Craig Toppercdab2322014-11-29 05:52:51 +0000413 Records.addDef(std::move(IterRec));
Craig Topper84138712014-11-29 05:31:10 +0000414 IterRecSave->resolveReferences();
Nicolai Haehnle0f529882018-03-06 13:48:47 +0000415 checkConcrete(*IterRecSave);
David Greenefb927af2012-02-22 16:09:41 +0000416 return false;
417}
418
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000419//===----------------------------------------------------------------------===//
420// Parser Code
421//===----------------------------------------------------------------------===//
422
423/// isObjectStart - Return true if this is a valid first token for an Object.
424static bool isObjectStart(tgtok::TokKind K) {
425 return K == tgtok::Class || K == tgtok::Def ||
David Greenefb927af2012-02-22 16:09:41 +0000426 K == tgtok::Defm || K == tgtok::Let ||
427 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000428}
429
430/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000431/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000432/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000433/// ObjectName ::= /*empty*/
434///
David Greene2affd672011-10-19 13:04:29 +0000435Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
436 switch (Lex.getCode()) {
437 case tgtok::colon:
438 case tgtok::semi:
439 case tgtok::l_brace:
440 // These are all of the tokens that can begin an object body.
441 // Some of these can also begin values but we disallow those cases
442 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000443 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000444 default:
445 break;
446 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000447
Craig Topper011817a2014-04-09 04:50:04 +0000448 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000449 if (CurMultiClass)
450 CurRec = &CurMultiClass->Rec;
451
Craig Topper011817a2014-04-09 04:50:04 +0000452 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000453 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000454 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000455 if (!CurRecName) {
456 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000457 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000458 }
459 Type = CurRecName->getType();
460 }
461
462 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000463}
464
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000465/// ParseClassID - Parse and resolve a reference to a class name. This returns
466/// null on error.
467///
468/// ClassID ::= ID
469///
470Record *TGParser::ParseClassID() {
471 if (Lex.getCode() != tgtok::Id) {
472 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000473 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000474 }
Bob Wilson7248f862009-11-22 04:24:42 +0000475
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000476 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000477 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000478 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000479
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000480 Lex.Lex();
481 return Result;
482}
483
Bob Wilson3d948162009-04-28 19:41:44 +0000484/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
485/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000486///
487/// MultiClassID ::= ID
488///
489MultiClass *TGParser::ParseMultiClassID() {
490 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000491 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000492 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000493 }
Bob Wilson3d948162009-04-28 19:41:44 +0000494
Craig Topper7adf2bf2014-12-11 05:25:30 +0000495 MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
Craig Topper4ca40012014-11-30 01:20:17 +0000496 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000497 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000498
David Greene753ed8f2009-04-22 16:42:54 +0000499 Lex.Lex();
Craig Topper4ca40012014-11-30 01:20:17 +0000500 return Result;
David Greene753ed8f2009-04-22 16:42:54 +0000501}
502
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000503/// ParseSubClassReference - Parse a reference to a subclass or to a templated
504/// subclass. This returns a SubClassRefTy with a null Record* on error.
505///
506/// SubClassRef ::= ClassID
507/// SubClassRef ::= ClassID '<' ValueList '>'
508///
509SubClassReference TGParser::
510ParseSubClassReference(Record *CurRec, bool isDefm) {
511 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000512 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000513
Sean Silva0657b402013-01-09 02:17:14 +0000514 if (isDefm) {
515 if (MultiClass *MC = ParseMultiClassID())
516 Result.Rec = &MC->Rec;
517 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000518 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000519 }
Craig Topper011817a2014-04-09 04:50:04 +0000520 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000521
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000522 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000523 if (Lex.getCode() != tgtok::less) {
524 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000525 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000526 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000527 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000528
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000529 if (Lex.getCode() == tgtok::greater) {
530 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000531 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000532 return Result;
533 }
Bob Wilson7248f862009-11-22 04:24:42 +0000534
Matthias Braunc66e7552016-12-05 06:41:54 +0000535 ParseValueList(Result.TemplateArgs, CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000536 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000537 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000538 return Result;
539 }
Bob Wilson7248f862009-11-22 04:24:42 +0000540
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000541 if (Lex.getCode() != tgtok::greater) {
542 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000543 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000544 return Result;
545 }
546 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000547 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000548
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000549 return Result;
550}
551
Bob Wilson3d948162009-04-28 19:41:44 +0000552/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
553/// templated submulticlass. This returns a SubMultiClassRefTy with a null
554/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000555///
556/// SubMultiClassRef ::= MultiClassID
557/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
558///
559SubMultiClassReference TGParser::
560ParseSubMultiClassReference(MultiClass *CurMC) {
561 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000562 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000563
David Greene753ed8f2009-04-22 16:42:54 +0000564 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000565 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000566
David Greene753ed8f2009-04-22 16:42:54 +0000567 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000568 if (Lex.getCode() != tgtok::less) {
569 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000570 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000571 }
David Greene753ed8f2009-04-22 16:42:54 +0000572 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000573
David Greene753ed8f2009-04-22 16:42:54 +0000574 if (Lex.getCode() == tgtok::greater) {
575 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000576 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000577 return Result;
578 }
Bob Wilson3d948162009-04-28 19:41:44 +0000579
Matthias Braunc66e7552016-12-05 06:41:54 +0000580 ParseValueList(Result.TemplateArgs, &CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000581 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000582 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000583 return Result;
584 }
Bob Wilson3d948162009-04-28 19:41:44 +0000585
David Greene753ed8f2009-04-22 16:42:54 +0000586 if (Lex.getCode() != tgtok::greater) {
587 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000588 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000589 return Result;
590 }
591 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000592 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000593
594 return Result;
595}
596
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000597/// ParseRangePiece - Parse a bit/value range.
598/// RangePiece ::= INTVAL
599/// RangePiece ::= INTVAL '-' INTVAL
600/// RangePiece ::= INTVAL INTVAL
Matthias Braunc66e7552016-12-05 06:41:54 +0000601bool TGParser::ParseRangePiece(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000602 if (Lex.getCode() != tgtok::IntVal) {
603 TokError("expected integer or bitrange");
604 return true;
605 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000606 int64_t Start = Lex.getCurIntVal();
607 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000608
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000609 if (Start < 0)
610 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000611
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000612 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000613 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000614 Ranges.push_back(Start);
615 return false;
616 case tgtok::minus:
617 if (Lex.Lex() != tgtok::IntVal) {
618 TokError("expected integer value as end of range");
619 return true;
620 }
621 End = Lex.getCurIntVal();
622 break;
623 case tgtok::IntVal:
624 End = -Lex.getCurIntVal();
625 break;
626 }
Bob Wilson7248f862009-11-22 04:24:42 +0000627 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000628 return TokError("invalid range, cannot be negative");
629 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000630
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000631 // Add to the range.
Craig Toppera9642b42015-05-04 01:35:39 +0000632 if (Start < End)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000633 for (; Start <= End; ++Start)
634 Ranges.push_back(Start);
Craig Toppera9642b42015-05-04 01:35:39 +0000635 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000636 for (; Start >= End; --Start)
637 Ranges.push_back(Start);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000638 return false;
639}
640
641/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
642///
643/// RangeList ::= RangePiece (',' RangePiece)*
644///
Matthias Braunc66e7552016-12-05 06:41:54 +0000645void TGParser::ParseRangeList(SmallVectorImpl<unsigned> &Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000646 // Parse the first piece.
Matthias Braunc66e7552016-12-05 06:41:54 +0000647 if (ParseRangePiece(Result)) {
648 Result.clear();
649 return;
650 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000651 while (Lex.getCode() == tgtok::comma) {
652 Lex.Lex(); // Eat the comma.
653
654 // Parse the next range piece.
Matthias Braunc66e7552016-12-05 06:41:54 +0000655 if (ParseRangePiece(Result)) {
656 Result.clear();
657 return;
658 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000659 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000660}
661
662/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
663/// OptionalRangeList ::= '<' RangeList '>'
664/// OptionalRangeList ::= /*empty*/
Matthias Braunc66e7552016-12-05 06:41:54 +0000665bool TGParser::ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000666 if (Lex.getCode() != tgtok::less)
667 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000668
Chris Lattner526c8cb2009-06-21 03:39:35 +0000669 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000670 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000671
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000672 // Parse the range list.
Matthias Braunc66e7552016-12-05 06:41:54 +0000673 ParseRangeList(Ranges);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000674 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000675
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000676 if (Lex.getCode() != tgtok::greater) {
677 TokError("expected '>' at end of range list");
678 return Error(StartLoc, "to match this '<'");
679 }
680 Lex.Lex(); // eat the '>'.
681 return false;
682}
683
684/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
685/// OptionalBitList ::= '{' RangeList '}'
686/// OptionalBitList ::= /*empty*/
Matthias Braunc66e7552016-12-05 06:41:54 +0000687bool TGParser::ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000688 if (Lex.getCode() != tgtok::l_brace)
689 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000690
Chris Lattner526c8cb2009-06-21 03:39:35 +0000691 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000692 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000693
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000694 // Parse the range list.
Matthias Braunc66e7552016-12-05 06:41:54 +0000695 ParseRangeList(Ranges);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000696 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000697
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000698 if (Lex.getCode() != tgtok::r_brace) {
699 TokError("expected '}' at end of bit list");
700 return Error(StartLoc, "to match this '{'");
701 }
702 Lex.Lex(); // eat the '}'.
703 return false;
704}
705
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000706/// ParseType - Parse and return a tblgen type. This returns null on error.
707///
708/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000709/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000710/// Type ::= BIT // bit type
711/// Type ::= BITS '<' INTVAL '>' // bits<x> type
712/// Type ::= INT // int type
713/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000714/// Type ::= DAG // dag type
715/// Type ::= ClassID // Record Type
716///
717RecTy *TGParser::ParseType() {
718 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000719 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000720 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Tim Northover88403d72016-07-05 21:22:55 +0000721 case tgtok::Code: Lex.Lex(); return CodeRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000722 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
723 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000724 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000725 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000726 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000727 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000728 case tgtok::Bits: {
729 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
730 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000731 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000732 }
733 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
734 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000735 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000736 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000737 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000738 if (Lex.Lex() != tgtok::greater) { // Eat count.
739 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000740 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000741 }
742 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000743 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000744 }
745 case tgtok::List: {
746 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
747 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000748 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000749 }
750 Lex.Lex(); // Eat '<'
751 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000752 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000753
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000754 if (Lex.getCode() != tgtok::greater) {
755 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000756 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000757 }
758 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000759 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000760 }
Bob Wilson7248f862009-11-22 04:24:42 +0000761 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000762}
763
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000764/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
765/// has already been read.
Matthias Braun215ff842016-12-05 07:35:13 +0000766Init *TGParser::ParseIDValue(Record *CurRec, StringInit *Name, SMLoc NameLoc,
David Greened4263a62011-10-19 13:04:20 +0000767 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000768 if (CurRec) {
769 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000770 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000771
Matthias Braun215ff842016-12-05 07:35:13 +0000772 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
David Greenedb10e692011-10-19 13:02:42 +0000773
David Greene47a665e2011-10-05 22:42:54 +0000774 if (CurMultiClass)
Matthias Braun215ff842016-12-05 07:35:13 +0000775 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
David Greenedb10e692011-10-19 13:02:42 +0000776 "::");
David Greene47a665e2011-10-05 22:42:54 +0000777
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000778 if (CurRec->isTemplateArg(TemplateArgName)) {
779 const RecordVal *RV = CurRec->getValue(TemplateArgName);
780 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000781 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000782 }
Matthias Braun215ff842016-12-05 07:35:13 +0000783 }
Bob Wilson7248f862009-11-22 04:24:42 +0000784
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000785 if (CurMultiClass) {
Nicolai Haehnle3c80e4c2018-03-05 14:01:38 +0000786 if (Name->getValue() == "NAME")
787 return VarInit::get(Name, StringRecTy::get());
788
Matthias Braun215ff842016-12-05 07:35:13 +0000789 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name, "::");
David Greenedb10e692011-10-19 13:02:42 +0000790
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000791 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
792 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
793 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000794 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000795 }
796 }
Bob Wilson7248f862009-11-22 04:24:42 +0000797
David Greenefb927af2012-02-22 16:09:41 +0000798 // If this is in a foreach loop, make sure it's not a loop iterator
Craig Toppereb4d7c62015-04-29 04:43:36 +0000799 for (const auto &L : Loops) {
800 VarInit *IterVar = dyn_cast<VarInit>(L.IterVar);
Matthias Braun215ff842016-12-05 07:35:13 +0000801 if (IterVar && IterVar->getNameInit() == Name)
David Greenefb927af2012-02-22 16:09:41 +0000802 return IterVar;
803 }
804
David Greene232bd602011-10-19 13:04:21 +0000805 if (Mode == ParseNameMode)
Matthias Braun215ff842016-12-05 07:35:13 +0000806 return Name;
David Greene232bd602011-10-19 13:04:21 +0000807
Matthias Braun215ff842016-12-05 07:35:13 +0000808 if (Record *D = Records.getDef(Name->getValue()))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000809 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000810
David Greene232bd602011-10-19 13:04:21 +0000811 if (Mode == ParseValueMode) {
Matthias Braun215ff842016-12-05 07:35:13 +0000812 Error(NameLoc, "Variable not defined: '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000813 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000814 }
Craig Toppera9642b42015-05-04 01:35:39 +0000815
Matthias Braun215ff842016-12-05 07:35:13 +0000816 return Name;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000817}
818
David Greene5d0c0512009-05-14 20:54:48 +0000819/// ParseOperation - Parse an operator. This returns null on error.
820///
821/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
822///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000823Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000824 switch (Lex.getCode()) {
825 default:
826 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000827 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000828 case tgtok::XHead:
829 case tgtok::XTail:
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000830 case tgtok::XSize:
David Greene2f7cf7f2011-01-07 17:05:37 +0000831 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000832 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
833 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000834 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000835
David Greenee8f3b272009-05-14 21:22:49 +0000836 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000837 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000838 case tgtok::XCast:
839 Lex.Lex(); // eat the operation
840 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000841
David Greenee8f3b272009-05-14 21:22:49 +0000842 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000843
Craig Topper011817a2014-04-09 04:50:04 +0000844 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000845 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000846 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000847 }
David Greene5d0c0512009-05-14 20:54:48 +0000848
David Greenee8f3b272009-05-14 21:22:49 +0000849 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000850 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000851 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000852 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000853 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000854 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000855 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000856 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000857 break;
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000858 case tgtok::XSize:
859 Lex.Lex();
860 Code = UnOpInit::SIZE;
861 Type = IntRecTy::get();
862 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000863 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000864 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000865 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000866 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000867 break;
David Greenee8f3b272009-05-14 21:22:49 +0000868 }
869 if (Lex.getCode() != tgtok::l_paren) {
870 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000871 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000872 }
873 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000874
David Greeneaf8ee2c2011-07-29 22:43:06 +0000875 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000876 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000877
Craig Topper85c07002015-04-30 05:54:22 +0000878 if (Code == UnOpInit::HEAD ||
879 Code == UnOpInit::TAIL ||
880 Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000881 ListInit *LHSl = dyn_cast<ListInit>(LHS);
882 StringInit *LHSs = dyn_cast<StringInit>(LHS);
883 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000884 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000885 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000886 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000887 }
888 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000889 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
890 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000891 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000892 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000893 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000894 }
895 }
896
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000897 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL ||
898 Code == UnOpInit::SIZE) {
Craig Topper011817a2014-04-09 04:50:04 +0000899 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000900 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000901 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000902 }
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000903 }
Bob Wilson7248f862009-11-22 04:24:42 +0000904
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000905 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
Craig Topperec9072d2015-05-14 05:53:53 +0000906 if (LHSl && LHSl->empty()) {
David Greened571b3c2009-05-14 22:38:31 +0000907 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000908 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000909 }
910 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000911 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000912 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000913 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000914 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000915 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000916 }
Craig Toppera9642b42015-05-04 01:35:39 +0000917 Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
918 : ListRecTy::get(Itemt->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000919 } else {
David Greened571b3c2009-05-14 22:38:31 +0000920 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000921 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000922 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000923 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000924 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000925 }
Craig Toppera9642b42015-05-04 01:35:39 +0000926 Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
David Greened571b3c2009-05-14 22:38:31 +0000927 }
928 }
929 }
930
David Greenee8f3b272009-05-14 21:22:49 +0000931 if (Lex.getCode() != tgtok::r_paren) {
932 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000933 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000934 }
935 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000936 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000937 }
David Greene5d0c0512009-05-14 20:54:48 +0000938
939 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000940 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000941 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000942 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +0000943 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000944 case tgtok::XSRL:
945 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000946 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000947 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000948 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000949 tgtok::TokKind OpTok = Lex.getCode();
950 SMLoc OpLoc = Lex.getLoc();
951 Lex.Lex(); // eat the operation
952
David Greene5d0c0512009-05-14 20:54:48 +0000953 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000954 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000955
Chris Lattner61ea00b2010-10-05 23:58:18 +0000956 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000957 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000958 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000959 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000960 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000961 case tgtok::XOR: Code = BinOpInit::OR; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000962 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
963 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
964 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
965 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000966 case tgtok::XListConcat:
967 Code = BinOpInit::LISTCONCAT;
968 // We don't know the list type until we parse the first argument
969 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000970 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000971 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000972 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000973 break;
David Greene5d0c0512009-05-14 20:54:48 +0000974 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000975
David Greene5d0c0512009-05-14 20:54:48 +0000976 if (Lex.getCode() != tgtok::l_paren) {
977 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000978 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000979 }
980 Lex.Lex(); // eat the '('
981
David Greeneaf8ee2c2011-07-29 22:43:06 +0000982 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000983
Chris Lattner61ea00b2010-10-05 23:58:18 +0000984 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000985 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000986
Chris Lattner61ea00b2010-10-05 23:58:18 +0000987 while (Lex.getCode() == tgtok::comma) {
988 Lex.Lex(); // eat the ','
989
990 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000991 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000992 }
David Greene5d0c0512009-05-14 20:54:48 +0000993
994 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000995 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000996 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000997 }
998 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000999
Daniel Sanders314e80e2014-05-07 10:13:19 +00001000 // If we are doing !listconcat, we should know the type by now
1001 if (OpTok == tgtok::XListConcat) {
Nicolai Haehnlee4a2cf52018-02-22 15:26:28 +00001002 if (TypedInit *Arg0 = dyn_cast<TypedInit>(InitList[0]))
Daniel Sanders314e80e2014-05-07 10:13:19 +00001003 Type = Arg0->getType();
1004 else {
Matthias Braun8c209aa2017-01-28 02:02:38 +00001005 InitList[0]->print(errs());
Daniel Sanders314e80e2014-05-07 10:13:19 +00001006 Error(OpLoc, "expected a list");
1007 return nullptr;
1008 }
1009 }
1010
Chris Lattner61ea00b2010-10-05 23:58:18 +00001011 // We allow multiple operands to associative operators like !strconcat as
1012 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +00001013 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +00001014 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001015 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +00001016 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
1017 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +00001018 InitList.back() = RHS;
1019 }
1020 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00001021
Chris Lattner61ea00b2010-10-05 23:58:18 +00001022 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +00001023 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +00001024 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +00001025
Chris Lattner61ea00b2010-10-05 23:58:18 +00001026 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +00001027 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001028 }
1029
Nicolai Haehnle8ebf7e42018-03-05 15:21:04 +00001030 case tgtok::XForEach: { // Value ::= !foreach '(' Id ',' Value ',' Value ')'
1031 SMLoc OpLoc = Lex.getLoc();
1032 Lex.Lex(); // eat the operation
1033 if (Lex.getCode() != tgtok::l_paren) {
1034 TokError("expected '(' after !foreach");
1035 return nullptr;
1036 }
1037
1038 if (Lex.Lex() != tgtok::Id) { // eat the '('
1039 TokError("first argument of !foreach must be an identifier");
1040 return nullptr;
1041 }
1042
1043 Init *LHS = StringInit::get(Lex.getCurStrVal());
1044
1045 if (CurRec->getValue(LHS)) {
1046 TokError((Twine("iteration variable '") + LHS->getAsString() +
1047 "' already defined")
1048 .str());
1049 return nullptr;
1050 }
1051
1052 if (Lex.Lex() != tgtok::comma) { // eat the id
1053 TokError("expected ',' in ternary operator");
1054 return nullptr;
1055 }
1056 Lex.Lex(); // eat the ','
1057
1058 Init *MHS = ParseValue(CurRec);
1059 if (!MHS)
1060 return nullptr;
1061
1062 if (Lex.getCode() != tgtok::comma) {
1063 TokError("expected ',' in ternary operator");
1064 return nullptr;
1065 }
1066 Lex.Lex(); // eat the ','
1067
1068 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1069 if (!MHSt) {
1070 TokError("could not get type of !foreach input");
1071 return nullptr;
1072 }
1073
1074 RecTy *InEltType = nullptr;
1075 RecTy *OutEltType = nullptr;
1076 bool IsDAG = false;
1077
1078 if (ListRecTy *InListTy = dyn_cast<ListRecTy>(MHSt->getType())) {
1079 InEltType = InListTy->getElementType();
1080 if (ItemType) {
1081 if (ListRecTy *OutListTy = dyn_cast<ListRecTy>(ItemType)) {
1082 OutEltType = OutListTy->getElementType();
1083 } else {
1084 Error(OpLoc,
1085 "expected value of type '" + Twine(ItemType->getAsString()) +
1086 "', but got !foreach of list type");
1087 return nullptr;
1088 }
1089 }
1090 } else if (DagRecTy *InDagTy = dyn_cast<DagRecTy>(MHSt->getType())) {
1091 InEltType = InDagTy;
1092 if (ItemType && !isa<DagRecTy>(ItemType)) {
1093 Error(OpLoc,
1094 "expected value of type '" + Twine(ItemType->getAsString()) +
1095 "', but got !foreach of dag type");
1096 return nullptr;
1097 }
1098 IsDAG = true;
1099 } else {
1100 TokError("!foreach must have list or dag input");
1101 return nullptr;
1102 }
1103
1104 CurRec->addValue(RecordVal(LHS, InEltType, false));
1105 Init *RHS = ParseValue(CurRec, OutEltType);
1106 CurRec->removeValue(LHS);
1107 if (!RHS)
1108 return nullptr;
1109
1110 if (Lex.getCode() != tgtok::r_paren) {
1111 TokError("expected ')' in binary operator");
1112 return nullptr;
1113 }
1114 Lex.Lex(); // eat the ')'
1115
1116 RecTy *OutType;
1117 if (IsDAG) {
1118 OutType = InEltType;
1119 } else {
1120 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1121 if (!RHSt) {
1122 TokError("could not get type of !foreach result");
1123 return nullptr;
1124 }
1125 OutType = RHSt->getType()->getListTy();
1126 }
1127
1128 return (TernOpInit::get(TernOpInit::FOREACH, LHS, MHS, RHS, OutType))
1129 ->Fold(CurRec, CurMultiClass);
1130 }
1131
David Greene3587eed2009-05-14 23:26:46 +00001132 case tgtok::XIf:
David Greene98ed3c72009-05-14 21:54:42 +00001133 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
1134 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +00001135 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001136
David Greene98ed3c72009-05-14 21:54:42 +00001137 tgtok::TokKind LexCode = Lex.getCode();
1138 Lex.Lex(); // eat the operation
1139 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001140 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001141 case tgtok::XIf:
1142 Code = TernOpInit::IF;
1143 break;
David Greene98ed3c72009-05-14 21:54:42 +00001144 case tgtok::XSubst:
1145 Code = TernOpInit::SUBST;
1146 break;
1147 }
1148 if (Lex.getCode() != tgtok::l_paren) {
1149 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001150 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001151 }
1152 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001153
David Greeneaf8ee2c2011-07-29 22:43:06 +00001154 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001155 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001156
David Greene98ed3c72009-05-14 21:54:42 +00001157 if (Lex.getCode() != tgtok::comma) {
1158 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001159 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001160 }
1161 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001162
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001163 Init *MHS = ParseValue(CurRec, ItemType);
1164 if (!MHS)
1165 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001166
David Greene98ed3c72009-05-14 21:54:42 +00001167 if (Lex.getCode() != tgtok::comma) {
1168 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001169 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001170 }
1171 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001172
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001173 Init *RHS = ParseValue(CurRec, ItemType);
1174 if (!RHS)
1175 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001176
David Greene98ed3c72009-05-14 21:54:42 +00001177 if (Lex.getCode() != tgtok::r_paren) {
1178 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001179 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001180 }
1181 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001182
David Greene98ed3c72009-05-14 21:54:42 +00001183 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001184 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001185 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001186 RecTy *MHSTy = nullptr;
1187 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001188
Sean Silvafb509ed2012-10-10 20:24:43 +00001189 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001190 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001191 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001192 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001193 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001194 MHSTy = BitRecTy::get();
1195
Sean Silvafb509ed2012-10-10 20:24:43 +00001196 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001197 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001198 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001199 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001200 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001201 RHSTy = BitRecTy::get();
1202
1203 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001204 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001205 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001206 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001207 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001208
1209 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001210 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001211 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001212 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001213
Nicolai Haehnle13080fd2018-03-06 13:48:20 +00001214 Type = resolveTypes(MHSTy, RHSTy);
1215 if (!Type) {
1216 TokError(Twine("inconsistent types '") + MHSTy->getAsString() +
1217 "' and '" + RHSTy->getAsString() + "' for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001218 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001219 }
1220 break;
1221 }
David Greene98ed3c72009-05-14 21:54:42 +00001222 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001223 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001224 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001225 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001226 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001227 }
1228 Type = RHSt->getType();
1229 break;
1230 }
1231 }
David Greenee32ebf22011-07-29 19:07:07 +00001232 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001233 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001234 }
David Greene5d0c0512009-05-14 20:54:48 +00001235 }
David Greene5d0c0512009-05-14 20:54:48 +00001236}
1237
1238/// ParseOperatorType - Parse a type for an operator. This returns
1239/// null on error.
1240///
1241/// OperatorType ::= '<' Type '>'
1242///
Dan Gohman1432ef82009-08-12 22:10:57 +00001243RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001244 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001245
1246 if (Lex.getCode() != tgtok::less) {
1247 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001248 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001249 }
1250 Lex.Lex(); // eat the <
1251
1252 Type = ParseType();
1253
Craig Topper011817a2014-04-09 04:50:04 +00001254 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001255 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001256 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001257 }
1258
1259 if (Lex.getCode() != tgtok::greater) {
1260 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001261 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001262 }
1263 Lex.Lex(); // eat the >
1264
1265 return Type;
1266}
1267
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001268/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1269///
1270/// SimpleValue ::= IDValue
1271/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001272/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001273/// SimpleValue ::= CODEFRAGMENT
1274/// SimpleValue ::= '?'
1275/// SimpleValue ::= '{' ValueList '}'
1276/// SimpleValue ::= ID '<' ValueListNE '>'
1277/// SimpleValue ::= '[' ValueList ']'
1278/// SimpleValue ::= '(' IDValue DagArgList ')'
1279/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001280/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001281/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1282/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1283/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001284/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001285/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1286///
David Greened4263a62011-10-19 13:04:20 +00001287Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1288 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001289 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001290 switch (Lex.getCode()) {
1291 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001292 case tgtok::paste:
1293 // This is a leading paste operation. This is deprecated but
1294 // still exists in some .td files. Ignore it.
1295 Lex.Lex(); // Skip '#'.
1296 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001297 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001298 case tgtok::BinaryIntVal: {
1299 auto BinaryVal = Lex.getCurBinaryIntVal();
1300 SmallVector<Init*, 16> Bits(BinaryVal.second);
1301 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001302 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001303 R = BitsInit::get(Bits);
1304 Lex.Lex();
1305 break;
1306 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001307 case tgtok::StrVal: {
1308 std::string Val = Lex.getCurStrVal();
1309 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001310
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001311 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001312 while (Lex.getCode() == tgtok::StrVal) {
1313 Val += Lex.getCurStrVal();
1314 Lex.Lex();
1315 }
Bob Wilson7248f862009-11-22 04:24:42 +00001316
David Greenee32ebf22011-07-29 19:07:07 +00001317 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001318 break;
1319 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001320 case tgtok::CodeFragment:
Tim Northover88403d72016-07-05 21:22:55 +00001321 R = CodeInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001322 Lex.Lex();
1323 break;
1324 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001325 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001326 Lex.Lex();
1327 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001328 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001329 SMLoc NameLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00001330 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001331 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001332 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001333
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001334 // Value ::= ID '<' ValueListNE '>'
1335 if (Lex.Lex() == tgtok::greater) {
1336 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001337 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001338 }
David Greene8618f952009-06-08 20:23:18 +00001339
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001340 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1341 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1342 // body.
Matthias Braun215ff842016-12-05 07:35:13 +00001343 Record *Class = Records.getClass(Name->getValue());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001344 if (!Class) {
Matthias Braun215ff842016-12-05 07:35:13 +00001345 Error(NameLoc, "Expected a class name, got '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001346 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001347 }
David Greene8618f952009-06-08 20:23:18 +00001348
Matthias Braunc66e7552016-12-05 06:41:54 +00001349 SubClassReference SCRef;
1350 ParseValueList(SCRef.TemplateArgs, CurRec, Class);
1351 if (SCRef.TemplateArgs.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001352
David Greene8618f952009-06-08 20:23:18 +00001353 if (Lex.getCode() != tgtok::greater) {
1354 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001355 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001356 }
1357 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001358 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001359
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001360 // Create the new record, set it as CurRec temporarily.
Nicolai Haehnle73355bc2018-03-06 13:48:54 +00001361 auto NewRecOwner =
1362 make_unique<Record>(Records.getNewAnonymousName(), NameLoc, Records,
1363 /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001364 Record *NewRec = NewRecOwner.get(); // Keep a copy since we may release.
Jordan Rosef12e8a92013-01-10 18:50:11 +00001365 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001366 SCRef.Rec = Class;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001367 // Add info about the subclass to NewRec.
Craig Topper84138712014-11-29 05:31:10 +00001368 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001369 return nullptr;
Craig Topper84138712014-11-29 05:31:10 +00001370
Hal Finkela8c1f462014-01-02 20:47:09 +00001371 if (!CurMultiClass) {
1372 NewRec->resolveReferences();
Craig Toppercdab2322014-11-29 05:52:51 +00001373 Records.addDef(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001374 } else {
Adam Nemete5a07162014-09-16 17:14:13 +00001375 // This needs to get resolved once the multiclass template arguments are
1376 // known before any use.
1377 NewRec->setResolveFirst(true);
Hal Finkela8c1f462014-01-02 20:47:09 +00001378 // Otherwise, we're inside a multiclass, add it to the multiclass.
Craig Topperc3504c42014-12-11 05:25:33 +00001379 CurMultiClass->DefPrototypes.push_back(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001380
1381 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00001382 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
1383 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Hal Finkela8c1f462014-01-02 20:47:09 +00001384 assert(RV && "Template arg doesn't exist?");
1385 NewRec->addValue(*RV);
1386 }
1387
1388 // We can't return the prototype def here, instead return:
1389 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1390 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1391 assert(MCNameRV && "multiclass record must have a NAME");
1392
1393 return UnOpInit::get(UnOpInit::CAST,
1394 BinOpInit::get(BinOpInit::STRCONCAT,
1395 VarInit::get(MCNameRV->getName(),
1396 MCNameRV->getType()),
1397 NewRec->getNameInit(),
1398 StringRecTy::get()),
Nicolai Haehnle13080fd2018-03-06 13:48:20 +00001399 NewRec->getDefInit()->getType());
Hal Finkela8c1f462014-01-02 20:47:09 +00001400 }
Bob Wilson7248f862009-11-22 04:24:42 +00001401
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001402 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001403 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001404 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001405 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001406 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001407 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001408 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001409
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001410 if (Lex.getCode() != tgtok::r_brace) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001411 ParseValueList(Vals, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001412 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001413 }
1414 if (Lex.getCode() != tgtok::r_brace) {
1415 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001416 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001417 }
1418 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001419
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001420 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001421
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001422 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1423 // first. We'll first read everything in to a vector, then we can reverse
1424 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001425 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001426 // FIXME: The following two loops would not be duplicated
1427 // if the API was a little more orthogonal.
1428
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001429 // bits<n> values are allowed to initialize n bits.
1430 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1431 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1432 NewBits.push_back(BI->getBit((e - i) - 1));
1433 continue;
1434 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001435 // bits<n> can also come from variable initializers.
1436 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1437 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1438 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1439 NewBits.push_back(VI->getBit((e - i) - 1));
1440 continue;
1441 }
1442 // Fallthrough to try convert this to a bit.
1443 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001444 // All other values must be convertible to just a single bit.
Nicolai Haehnledfda9dc2018-03-06 13:48:39 +00001445 Init *Bit = Vals[i]->getCastTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001446 if (!Bit) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001447 Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
Chris Lattner52416952007-11-22 21:06:59 +00001448 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001449 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001450 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001451 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001452 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001453 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001454 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001455 }
1456 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1457 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001458 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001459
Craig Topper011817a2014-04-09 04:50:04 +00001460 RecTy *DeducedEltTy = nullptr;
1461 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001462
Craig Topper011817a2014-04-09 04:50:04 +00001463 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001464 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001465 if (!ListType) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001466 TokError(Twine("Type mismatch for list, expected list type, got ") +
1467 ItemType->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001468 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001469 }
1470 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001471 }
David Greene8618f952009-06-08 20:23:18 +00001472
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001473 if (Lex.getCode() != tgtok::r_square) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001474 ParseValueList(Vals, CurRec, nullptr,
1475 GivenListTy ? GivenListTy->getElementType() : nullptr);
Craig Topper011817a2014-04-09 04:50:04 +00001476 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001477 }
1478 if (Lex.getCode() != tgtok::r_square) {
1479 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001480 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001481 }
1482 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001483
Craig Topper011817a2014-04-09 04:50:04 +00001484 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001485 if (Lex.getCode() == tgtok::less) {
1486 // Optional list element type
1487 Lex.Lex(); // eat the '<'
1488
1489 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001490 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001491 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001492 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001493 }
1494
1495 if (Lex.getCode() != tgtok::greater) {
1496 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001497 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001498 }
1499 Lex.Lex(); // eat the '>'
1500 }
1501
1502 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001503 RecTy *EltTy = nullptr;
Craig Toppereb4d7c62015-04-29 04:43:36 +00001504 for (Init *V : Vals) {
1505 TypedInit *TArg = dyn_cast<TypedInit>(V);
Craig Topper011817a2014-04-09 04:50:04 +00001506 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001507 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001508 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001509 }
Craig Topper011817a2014-04-09 04:50:04 +00001510 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001511 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001512 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001513 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001514 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001515 }
Bob Wilson7248f862009-11-22 04:24:42 +00001516 } else {
David Greene8618f952009-06-08 20:23:18 +00001517 EltTy = TArg->getType();
1518 }
1519 }
1520
Craig Topper011817a2014-04-09 04:50:04 +00001521 if (GivenEltTy) {
1522 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001523 // Verify consistency
1524 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1525 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001526 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001527 }
1528 }
1529 EltTy = GivenEltTy;
1530 }
1531
Craig Topper011817a2014-04-09 04:50:04 +00001532 if (!EltTy) {
1533 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001534 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001535 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001536 }
1537 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001538 } else {
David Greene8618f952009-06-08 20:23:18 +00001539 // Make sure the deduced type is compatible with the given type
1540 if (GivenListTy) {
1541 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
Nicolai Haehnlef19083d2018-02-22 15:26:21 +00001542 TokError(Twine("Element type mismatch for list: element type '") +
1543 EltTy->getAsString() + "' not convertible to '" +
1544 GivenListTy->getElementType()->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001545 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001546 }
1547 }
1548 DeducedEltTy = EltTy;
1549 }
Bob Wilson7248f862009-11-22 04:24:42 +00001550
David Greenee32ebf22011-07-29 19:07:07 +00001551 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001552 }
1553 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1554 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001555 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001556 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001557 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001558 }
Bob Wilson7248f862009-11-22 04:24:42 +00001559
David Greeneaf8ee2c2011-07-29 22:43:06 +00001560 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001561 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001562
Nate Begemandbe3f772009-03-19 05:21:56 +00001563 // If the operator name is present, parse it.
Matthias Braun7cf3b112016-12-05 06:00:41 +00001564 StringInit *OperatorName = nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001565 if (Lex.getCode() == tgtok::colon) {
1566 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1567 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001568 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001569 }
Matthias Braun7cf3b112016-12-05 06:00:41 +00001570 OperatorName = StringInit::get(Lex.getCurStrVal());
Nate Begemandbe3f772009-03-19 05:21:56 +00001571 Lex.Lex(); // eat the VarName.
1572 }
Bob Wilson7248f862009-11-22 04:24:42 +00001573
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001574 SmallVector<std::pair<llvm::Init*, StringInit*>, 8> DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001575 if (Lex.getCode() != tgtok::r_paren) {
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001576 ParseDagArgList(DagArgs, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001577 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001578 }
Bob Wilson7248f862009-11-22 04:24:42 +00001579
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001580 if (Lex.getCode() != tgtok::r_paren) {
1581 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001582 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001583 }
1584 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001585
David Greenee32ebf22011-07-29 19:07:07 +00001586 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001587 }
Bob Wilson7248f862009-11-22 04:24:42 +00001588
David Greene2f7cf7f2011-01-07 17:05:37 +00001589 case tgtok::XHead:
1590 case tgtok::XTail:
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +00001591 case tgtok::XSize:
David Greene2f7cf7f2011-01-07 17:05:37 +00001592 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001593 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001594 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001595 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001596 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +00001597 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +00001598 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001599 case tgtok::XSRL:
1600 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001601 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001602 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001603 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001604 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001605 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001606 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001607 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001608 }
1609 }
Bob Wilson7248f862009-11-22 04:24:42 +00001610
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001611 return R;
1612}
1613
1614/// ParseValue - Parse a tblgen value. This returns null on error.
1615///
1616/// Value ::= SimpleValue ValueSuffix*
1617/// ValueSuffix ::= '{' BitList '}'
1618/// ValueSuffix ::= '[' BitList ']'
1619/// ValueSuffix ::= '.' ID
1620///
David Greened4263a62011-10-19 13:04:20 +00001621Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1622 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001623 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001624
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001625 // Parse the suffixes now if present.
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001626 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001627 switch (Lex.getCode()) {
1628 default: return Result;
1629 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001630 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001631 // This is the beginning of the object body.
1632 return Result;
1633
Chris Lattner526c8cb2009-06-21 03:39:35 +00001634 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001635 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001636 SmallVector<unsigned, 16> Ranges;
1637 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001638 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001639
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001640 // Reverse the bitlist.
1641 std::reverse(Ranges.begin(), Ranges.end());
1642 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001643 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001644 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001645 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001646 }
Bob Wilson7248f862009-11-22 04:24:42 +00001647
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001648 // Eat the '}'.
1649 if (Lex.getCode() != tgtok::r_brace) {
1650 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001651 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001652 }
1653 Lex.Lex();
1654 break;
1655 }
1656 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001657 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001658 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001659 SmallVector<unsigned, 16> Ranges;
1660 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001661 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001662
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001663 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001664 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001665 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001666 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001667 }
Bob Wilson7248f862009-11-22 04:24:42 +00001668
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001669 // Eat the ']'.
1670 if (Lex.getCode() != tgtok::r_square) {
1671 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001672 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001673 }
1674 Lex.Lex();
1675 break;
1676 }
Matthias Braun6a441832016-12-05 06:00:36 +00001677 case tgtok::period: {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001678 if (Lex.Lex() != tgtok::Id) { // eat the .
1679 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001680 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001681 }
Matthias Braun6a441832016-12-05 06:00:36 +00001682 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
1683 if (!Result->getFieldType(FieldName)) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001684 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001685 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001686 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001687 }
Matthias Braun6a441832016-12-05 06:00:36 +00001688 Result = FieldInit::get(Result, FieldName);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001689 Lex.Lex(); // eat field name
1690 break;
Matthias Braun6a441832016-12-05 06:00:36 +00001691 }
David Greene8e85b482011-10-19 13:04:43 +00001692
1693 case tgtok::paste:
1694 SMLoc PasteLoc = Lex.getLoc();
1695
1696 // Create a !strconcat() operation, first casting each operand to
1697 // a string if necessary.
1698
Sean Silvafb509ed2012-10-10 20:24:43 +00001699 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001700 if (!LHS) {
1701 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001702 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001703 }
Craig Toppera9642b42015-05-04 01:35:39 +00001704
David Greene8e85b482011-10-19 13:04:43 +00001705 if (LHS->getType() != StringRecTy::get()) {
1706 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1707 }
1708
Craig Topper011817a2014-04-09 04:50:04 +00001709 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001710
1711 Lex.Lex(); // Eat the '#'.
1712 switch (Lex.getCode()) {
1713 case tgtok::colon:
1714 case tgtok::semi:
1715 case tgtok::l_brace:
1716 // These are all of the tokens that can begin an object body.
1717 // Some of these can also begin values but we disallow those cases
1718 // because they are unlikely to be useful.
Craig Toppera9642b42015-05-04 01:35:39 +00001719
David Greene8e85b482011-10-19 13:04:43 +00001720 // Trailing paste, concat with an empty string.
1721 RHS = StringInit::get("");
1722 break;
1723
1724 default:
1725 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001726 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001727 if (!RHS) {
1728 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001729 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001730 }
1731
1732 if (RHS->getType() != StringRecTy::get()) {
1733 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1734 }
Craig Toppera9642b42015-05-04 01:35:39 +00001735
David Greene8e85b482011-10-19 13:04:43 +00001736 break;
1737 }
1738
1739 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1740 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1741 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001742 }
1743 }
1744}
1745
1746/// ParseDagArgList - Parse the argument list for a dag literal expression.
1747///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001748/// DagArg ::= Value (':' VARNAME)?
1749/// DagArg ::= VARNAME
1750/// DagArgList ::= DagArg
1751/// DagArgList ::= DagArgList ',' DagArg
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001752void TGParser::ParseDagArgList(
1753 SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
1754 Record *CurRec) {
Bob Wilson7248f862009-11-22 04:24:42 +00001755
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001756 while (true) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001757 // DagArg ::= VARNAME
1758 if (Lex.getCode() == tgtok::VarName) {
1759 // A missing value is treated like '?'.
Matthias Braunbb053162016-12-05 06:00:46 +00001760 StringInit *VarName = StringInit::get(Lex.getCurStrVal());
1761 Result.emplace_back(UnsetInit::get(), VarName);
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001762 Lex.Lex();
1763 } else {
1764 // DagArg ::= Value (':' VARNAME)?
1765 Init *Val = ParseValue(CurRec);
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001766 if (!Val) {
1767 Result.clear();
1768 return;
1769 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001770
1771 // If the variable name is present, add it.
Matthias Braunbb053162016-12-05 06:00:46 +00001772 StringInit *VarName = nullptr;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001773 if (Lex.getCode() == tgtok::colon) {
1774 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1775 TokError("expected variable name in dag literal");
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001776 Result.clear();
1777 return;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001778 }
Matthias Braunbb053162016-12-05 06:00:46 +00001779 VarName = StringInit::get(Lex.getCurStrVal());
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001780 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001781 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001782
1783 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001784 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001785 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001786 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001787 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001788}
1789
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001790/// ParseValueList - Parse a comma separated list of values, returning them as a
1791/// vector. Note that this always expects to be able to parse at least one
1792/// value. It returns an empty list if this is not possible.
1793///
1794/// ValueList ::= Value (',' Value)
1795///
Matthias Braunc66e7552016-12-05 06:41:54 +00001796void TGParser::ParseValueList(SmallVectorImpl<Init*> &Result, Record *CurRec,
1797 Record *ArgsRec, RecTy *EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001798 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001799 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001800 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001801 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001802 if (TArgs.empty()) {
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001803 TokError("template argument provided to non-template class");
Matthias Braunc66e7552016-12-05 06:41:54 +00001804 Result.clear();
1805 return;
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001806 }
David Greene8618f952009-06-08 20:23:18 +00001807 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001808 if (!RV) {
1809 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1810 << ")\n";
1811 }
David Greene8618f952009-06-08 20:23:18 +00001812 assert(RV && "Template argument record not found??");
1813 ItemType = RV->getType();
1814 ++ArgN;
1815 }
1816 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00001817 if (!Result.back()) {
1818 Result.clear();
1819 return;
1820 }
Bob Wilson7248f862009-11-22 04:24:42 +00001821
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001822 while (Lex.getCode() == tgtok::comma) {
1823 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001824
Craig Topper011817a2014-04-09 04:50:04 +00001825 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001826 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001827 if (ArgN >= TArgs.size()) {
1828 TokError("too many template arguments");
Matthias Braunc66e7552016-12-05 06:41:54 +00001829 Result.clear();
1830 return;
Bob Wilson7248f862009-11-22 04:24:42 +00001831 }
David Greene8618f952009-06-08 20:23:18 +00001832 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1833 assert(RV && "Template argument record not found??");
1834 ItemType = RV->getType();
1835 ++ArgN;
1836 }
1837 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00001838 if (!Result.back()) {
1839 Result.clear();
1840 return;
1841 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001842 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001843}
1844
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001845/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1846/// empty string on error. This can happen in a number of different context's,
1847/// including within a def or in the template args for a def (which which case
1848/// CurRec will be non-null) and within the template args for a multiclass (in
1849/// which case CurRec will be null, but CurMultiClass will be set). This can
1850/// also happen within a def that is within a multiclass, which will set both
1851/// CurRec and CurMultiClass.
1852///
1853/// Declaration ::= FIELD? Type ID ('=' Value)?
1854///
David Greenedb10e692011-10-19 13:02:42 +00001855Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001856 bool ParsingTemplateArgs) {
1857 // Read the field prefix if present.
1858 bool HasField = Lex.getCode() == tgtok::Field;
1859 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001860
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001861 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001862 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001863
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001864 if (Lex.getCode() != tgtok::Id) {
1865 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001866 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001867 }
Bob Wilson7248f862009-11-22 04:24:42 +00001868
Chris Lattner526c8cb2009-06-21 03:39:35 +00001869 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001870 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001871 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001872
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001873 if (ParsingTemplateArgs) {
Craig Toppera9642b42015-05-04 01:35:39 +00001874 if (CurRec)
David Greenedb10e692011-10-19 13:02:42 +00001875 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Craig Toppera9642b42015-05-04 01:35:39 +00001876 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001877 assert(CurMultiClass);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001878 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001879 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1880 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001881 }
Bob Wilson7248f862009-11-22 04:24:42 +00001882
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001883 // Add the value.
1884 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001885 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001886
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001887 // If a value is present, parse it.
1888 if (Lex.getCode() == tgtok::equal) {
1889 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001890 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001891 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001892 if (!Val ||
Craig Toppercfd81732016-01-04 03:15:08 +00001893 SetValue(CurRec, ValLoc, DeclName, None, Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001894 // Return the name, even if an error is thrown. This is so that we can
1895 // continue to make some progress, even without the value having been
1896 // initialized.
1897 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001898 }
Bob Wilson7248f862009-11-22 04:24:42 +00001899
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001900 return DeclName;
1901}
1902
David Greenefb927af2012-02-22 16:09:41 +00001903/// ParseForeachDeclaration - Read a foreach declaration, returning
1904/// the name of the declared object or a NULL Init on error. Return
1905/// the name of the parsed initializer list through ForeachListName.
1906///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001907/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1908/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1909/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001910///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001911VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001912 if (Lex.getCode() != tgtok::Id) {
1913 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001914 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001915 }
1916
1917 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1918 Lex.Lex();
1919
1920 // If a value is present, parse it.
1921 if (Lex.getCode() != tgtok::equal) {
1922 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001923 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001924 }
1925 Lex.Lex(); // Eat the '='
1926
Craig Topper011817a2014-04-09 04:50:04 +00001927 RecTy *IterType = nullptr;
Matthias Braunc66e7552016-12-05 06:41:54 +00001928 SmallVector<unsigned, 16> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001929
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001930 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001931 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001932 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001933 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001934 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001935 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001936 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001937 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001938 }
1939 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001940 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001941 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001942 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001943 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001944 }
1945 IterType = ListType->getElementType();
1946 break;
David Greenefb927af2012-02-22 16:09:41 +00001947 }
1948
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001949 case tgtok::IntVal: { // RangePiece.
1950 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001951 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001952 break;
David Greenefb927af2012-02-22 16:09:41 +00001953 }
1954
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001955 case tgtok::l_brace: { // '{' RangeList '}'
1956 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001957 ParseRangeList(Ranges);
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001958 if (Lex.getCode() != tgtok::r_brace) {
1959 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001960 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001961 }
1962 Lex.Lex();
1963 break;
1964 }
1965 }
David Greenefb927af2012-02-22 16:09:41 +00001966
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001967 if (!Ranges.empty()) {
1968 assert(!IterType && "Type already initialized?");
1969 IterType = IntRecTy::get();
1970 std::vector<Init*> Values;
Craig Topper8eb764c2015-06-02 06:19:28 +00001971 for (unsigned R : Ranges)
1972 Values.push_back(IntInit::get(R));
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001973 ForeachListValue = ListInit::get(Values, IterType);
1974 }
1975
1976 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001977 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001978
1979 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001980}
1981
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001982/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1983/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1984/// template args for a def, which may or may not be in a multiclass. If null,
1985/// these are the template args for a multiclass.
1986///
1987/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001988///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001989bool TGParser::ParseTemplateArgList(Record *CurRec) {
1990 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1991 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001992
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001993 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001994
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001995 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001996 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001997 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001998 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001999
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002000 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00002001
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002002 while (Lex.getCode() == tgtok::comma) {
2003 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00002004
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002005 // Read the following declarations.
2006 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00002007 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002008 return true;
2009 TheRecToAddTo->addTemplateArg(TemplArg);
2010 }
Bob Wilson7248f862009-11-22 04:24:42 +00002011
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002012 if (Lex.getCode() != tgtok::greater)
2013 return TokError("expected '>' at end of template argument list");
2014 Lex.Lex(); // eat the '>'.
2015 return false;
2016}
2017
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002018/// ParseBodyItem - Parse a single item at within the body of a def or class.
2019///
2020/// BodyItem ::= Declaration ';'
2021/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
2022bool TGParser::ParseBodyItem(Record *CurRec) {
2023 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00002024 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002025 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 declaration");
2029 Lex.Lex();
2030 return false;
2031 }
2032
2033 // LET ID OptionalRangeList '=' Value ';'
2034 if (Lex.Lex() != tgtok::Id)
2035 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00002036
Chris Lattner526c8cb2009-06-21 03:39:35 +00002037 SMLoc IdLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00002038 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002039 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00002040
Matthias Braunc66e7552016-12-05 06:41:54 +00002041 SmallVector<unsigned, 16> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00002042 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002043 return true;
2044 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002045
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002046 if (Lex.getCode() != tgtok::equal)
2047 return TokError("expected '=' in let expression");
2048 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002049
David Greene8618f952009-06-08 20:23:18 +00002050 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00002051 if (!Field)
Matthias Braun215ff842016-12-05 07:35:13 +00002052 return TokError("Value '" + FieldName->getValue() + "' unknown!");
David Greene8618f952009-06-08 20:23:18 +00002053
2054 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00002055
David Greeneaf8ee2c2011-07-29 22:43:06 +00002056 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00002057 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002058
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002059 if (Lex.getCode() != tgtok::semi)
2060 return TokError("expected ';' after let expression");
2061 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002062
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002063 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
2064}
2065
2066/// ParseBody - Read the body of a class or def. Return true on error, false on
2067/// success.
2068///
2069/// Body ::= ';'
2070/// Body ::= '{' BodyList '}'
2071/// BodyList BodyItem*
2072///
2073bool TGParser::ParseBody(Record *CurRec) {
2074 // If this is a null definition, just eat the semi and return.
2075 if (Lex.getCode() == tgtok::semi) {
2076 Lex.Lex();
2077 return false;
2078 }
Bob Wilson7248f862009-11-22 04:24:42 +00002079
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002080 if (Lex.getCode() != tgtok::l_brace)
2081 return TokError("Expected ';' or '{' to start body");
2082 // Eat the '{'.
2083 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002084
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002085 while (Lex.getCode() != tgtok::r_brace)
2086 if (ParseBodyItem(CurRec))
2087 return true;
2088
2089 // Eat the '}'.
2090 Lex.Lex();
2091 return false;
2092}
2093
Sean Silvacb1a75e2013-01-09 04:49:14 +00002094/// \brief Apply the current let bindings to \a CurRec.
2095/// \returns true on error, false otherwise.
2096bool TGParser::ApplyLetStack(Record *CurRec) {
Matthias Braunc66e7552016-12-05 06:41:54 +00002097 for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
Craig Topper8eb764c2015-06-02 06:19:28 +00002098 for (LetRecord &LR : LetInfo)
2099 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
Sean Silvacb1a75e2013-01-09 04:49:14 +00002100 return true;
2101 return false;
2102}
2103
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002104/// ParseObjectBody - Parse the body of a def or class. This consists of an
2105/// optional ClassList followed by a Body. CurRec is the current def or class
2106/// that is being parsed.
2107///
2108/// ObjectBody ::= BaseClassList Body
2109/// BaseClassList ::= /*empty*/
2110/// BaseClassList ::= ':' BaseClassListNE
2111/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
2112///
2113bool TGParser::ParseObjectBody(Record *CurRec) {
2114 // If there is a baseclass list, read it.
2115 if (Lex.getCode() == tgtok::colon) {
2116 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002117
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002118 // Read all of the subclasses.
2119 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002120 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002121 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002122 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002123
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002124 // Add it.
2125 if (AddSubClass(CurRec, SubClass))
2126 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002127
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002128 if (Lex.getCode() != tgtok::comma) break;
2129 Lex.Lex(); // eat ','.
2130 SubClass = ParseSubClassReference(CurRec, false);
2131 }
2132 }
2133
Sean Silvacb1a75e2013-01-09 04:49:14 +00002134 if (ApplyLetStack(CurRec))
2135 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002136
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002137 return ParseBody(CurRec);
2138}
2139
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002140/// ParseDef - Parse and return a top level or multiclass def, return the record
2141/// corresponding to it. This returns null on error.
2142///
2143/// DefInst ::= DEF ObjectName ObjectBody
2144///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002145bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00002146 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002147 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00002148 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002149
2150 // Parse ObjectName and make a record for it.
Craig Topper84138712014-11-29 05:31:10 +00002151 std::unique_ptr<Record> CurRecOwner;
Jordan Roseabdd99b2013-01-10 18:50:05 +00002152 Init *Name = ParseObjectName(CurMultiClass);
2153 if (Name)
Craig Topper84138712014-11-29 05:31:10 +00002154 CurRecOwner = make_unique<Record>(Name, DefLoc, Records);
Jordan Roseabdd99b2013-01-10 18:50:05 +00002155 else
Nicolai Haehnle73355bc2018-03-06 13:48:54 +00002156 CurRecOwner = make_unique<Record>(Records.getNewAnonymousName(), DefLoc,
2157 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00002158 Record *CurRec = CurRecOwner.get(); // Keep a copy since we may release.
Bob Wilson7248f862009-11-22 04:24:42 +00002159
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002160 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002161 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002162
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002163 // Ensure redefinition doesn't happen.
Craig Topper84138712014-11-29 05:31:10 +00002164 if (Records.getDef(CurRec->getNameInitAsString()))
2165 return Error(DefLoc, "def '" + CurRec->getNameInitAsString()+
2166 "' already defined");
Craig Toppercdab2322014-11-29 05:52:51 +00002167 Records.addDef(std::move(CurRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00002168
2169 if (ParseObjectBody(CurRec))
2170 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002171 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002172 // Parse the body before adding this prototype to the DefPrototypes vector.
2173 // That way implicit definitions will be added to the DefPrototypes vector
2174 // before this object, instantiated prior to defs derived from this object,
2175 // and this available for indirect name resolution when defs derived from
2176 // this object are instantiated.
Craig Topper84138712014-11-29 05:31:10 +00002177 if (ParseObjectBody(CurRec))
Hal Finkela8c1f462014-01-02 20:47:09 +00002178 return true;
2179
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002180 // Otherwise, a def inside a multiclass, add it to the multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002181 for (const auto &Proto : CurMultiClass->DefPrototypes)
2182 if (Proto->getNameInit() == CurRec->getNameInit())
Craig Topper84138712014-11-29 05:31:10 +00002183 return Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
2184 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002185 CurMultiClass->DefPrototypes.push_back(std::move(CurRecOwner));
Anton Yartsev671dff12014-08-08 00:29:54 +00002186 } else if (ParseObjectBody(CurRec)) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002187 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002188 }
Bob Wilson7248f862009-11-22 04:24:42 +00002189
Nicolai Haehnle0f529882018-03-06 13:48:47 +00002190 if (!CurMultiClass) { // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002191 // See Record::setName(). This resolve step will see any new name
2192 // for the def that might have been created when resolving
2193 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002194 CurRec->resolveReferences();
Nicolai Haehnle0f529882018-03-06 13:48:47 +00002195 if (Loops.empty())
2196 checkConcrete(*CurRec);
2197 }
Bob Wilson7248f862009-11-22 04:24:42 +00002198
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002199 // If ObjectBody has template arguments, it's an error.
2200 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002201
2202 if (CurMultiClass) {
2203 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002204 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
2205 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002206 assert(RV && "Template arg doesn't exist?");
2207 CurRec->addValue(*RV);
2208 }
2209 }
2210
Craig Toppera9642b42015-05-04 01:35:39 +00002211 if (ProcessForeachDefs(CurRec, DefLoc))
Craig Topper84138712014-11-29 05:31:10 +00002212 return Error(DefLoc, "Could not process loops for def" +
2213 CurRec->getNameInitAsString());
David Greenefb927af2012-02-22 16:09:41 +00002214
2215 return false;
2216}
2217
2218/// ParseForeach - Parse a for statement. Return the record corresponding
2219/// to it. This returns true on error.
2220///
2221/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2222/// Foreach ::= FOREACH Declaration IN Object
2223///
2224bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2225 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2226 Lex.Lex(); // Eat the 'for' token.
2227
2228 // Make a temporary object to record items associated with the for
2229 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002230 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002231 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002232 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002233 return TokError("expected declaration in for");
2234
2235 if (Lex.getCode() != tgtok::In)
2236 return TokError("Unknown tok");
2237 Lex.Lex(); // Eat the in
2238
2239 // Create a loop object and remember it.
2240 Loops.push_back(ForeachLoop(IterName, ListValue));
2241
2242 if (Lex.getCode() != tgtok::l_brace) {
2243 // FOREACH Declaration IN Object
2244 if (ParseObject(CurMultiClass))
2245 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002246 } else {
David Greenefb927af2012-02-22 16:09:41 +00002247 SMLoc BraceLoc = Lex.getLoc();
2248 // Otherwise, this is a group foreach.
2249 Lex.Lex(); // eat the '{'.
2250
2251 // Parse the object list.
2252 if (ParseObjectList(CurMultiClass))
2253 return true;
2254
2255 if (Lex.getCode() != tgtok::r_brace) {
2256 TokError("expected '}' at end of foreach command");
2257 return Error(BraceLoc, "to match this '{'");
2258 }
2259 Lex.Lex(); // Eat the }
2260 }
2261
2262 // We've processed everything in this loop.
2263 Loops.pop_back();
2264
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002265 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002266}
2267
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002268/// ParseClass - Parse a tblgen class definition.
2269///
2270/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2271///
2272bool TGParser::ParseClass() {
2273 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2274 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002275
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002276 if (Lex.getCode() != tgtok::Id)
2277 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002278
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002279 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2280 if (CurRec) {
2281 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002282 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002283 !CurRec->getSuperClasses().empty() ||
2284 !CurRec->getTemplateArgs().empty())
Craig Topper85c07002015-04-30 05:54:22 +00002285 return TokError("Class '" + CurRec->getNameInitAsString() +
2286 "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002287 } else {
2288 // If this is the first reference to this class, create and add it.
Hans Wennborgffbbd532014-11-30 00:31:49 +00002289 auto NewRec =
2290 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
Craig Toppercdab2322014-11-29 05:52:51 +00002291 CurRec = NewRec.get();
2292 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002293 }
2294 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002295
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002296 // If there are template args, parse them.
2297 if (Lex.getCode() == tgtok::less)
2298 if (ParseTemplateArgList(CurRec))
2299 return true;
2300
2301 // Finally, parse the object body.
2302 return ParseObjectBody(CurRec);
2303}
2304
2305/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2306/// of LetRecords.
2307///
2308/// LetList ::= LetItem (',' LetItem)*
2309/// LetItem ::= ID OptionalRangeList '=' Value
2310///
Matthias Braunc66e7552016-12-05 06:41:54 +00002311void TGParser::ParseLetList(SmallVectorImpl<LetRecord> &Result) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002312 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002313 if (Lex.getCode() != tgtok::Id) {
2314 TokError("expected identifier in let definition");
Matthias Braunc66e7552016-12-05 06:41:54 +00002315 Result.clear();
2316 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002317 }
Matthias Braunc66e7552016-12-05 06:41:54 +00002318
Matthias Braun215ff842016-12-05 07:35:13 +00002319 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattner526c8cb2009-06-21 03:39:35 +00002320 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002321 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002322
2323 // Check for an optional RangeList.
Matthias Braunc66e7552016-12-05 06:41:54 +00002324 SmallVector<unsigned, 16> Bits;
2325 if (ParseOptionalRangeList(Bits)) {
2326 Result.clear();
2327 return;
2328 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002329 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002330
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002331 if (Lex.getCode() != tgtok::equal) {
2332 TokError("expected '=' in let expression");
Matthias Braunc66e7552016-12-05 06:41:54 +00002333 Result.clear();
2334 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002335 }
2336 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002337
Craig Topper011817a2014-04-09 04:50:04 +00002338 Init *Val = ParseValue(nullptr);
Matthias Braunc66e7552016-12-05 06:41:54 +00002339 if (!Val) {
2340 Result.clear();
2341 return;
2342 }
Bob Wilson7248f862009-11-22 04:24:42 +00002343
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002344 // Now that we have everything, add the record.
Matthias Braun215ff842016-12-05 07:35:13 +00002345 Result.emplace_back(Name, Bits, Val, NameLoc);
Bob Wilson7248f862009-11-22 04:24:42 +00002346
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002347 if (Lex.getCode() != tgtok::comma)
Matthias Braunc66e7552016-12-05 06:41:54 +00002348 return;
Bob Wilson7248f862009-11-22 04:24:42 +00002349 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002350 }
2351}
2352
2353/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002354/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002355///
2356/// Object ::= LET LetList IN '{' ObjectList '}'
2357/// Object ::= LET LetList IN Object
2358///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002359bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002360 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2361 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002362
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002363 // Add this entry to the let stack.
Matthias Braunc66e7552016-12-05 06:41:54 +00002364 SmallVector<LetRecord, 8> LetInfo;
2365 ParseLetList(LetInfo);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002366 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002367 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002368
2369 if (Lex.getCode() != tgtok::In)
2370 return TokError("expected 'in' at end of top-level 'let'");
2371 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002372
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002373 // If this is a scalar let, just handle it now
2374 if (Lex.getCode() != tgtok::l_brace) {
2375 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002376 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002377 return true;
2378 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002379 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002380 // Otherwise, this is a group let.
2381 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002382
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002383 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002384 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002385 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002386
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002387 if (Lex.getCode() != tgtok::r_brace) {
2388 TokError("expected '}' at end of top level let command");
2389 return Error(BraceLoc, "to match this '{'");
2390 }
2391 Lex.Lex();
2392 }
Bob Wilson7248f862009-11-22 04:24:42 +00002393
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002394 // Outside this let scope, this let block is not active.
2395 LetStack.pop_back();
2396 return false;
2397}
2398
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002399/// ParseMultiClass - Parse a multiclass definition.
2400///
Bob Wilson3d948162009-04-28 19:41:44 +00002401/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002402/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2403/// MultiClassObject ::= DefInst
2404/// MultiClassObject ::= MultiClassInst
2405/// MultiClassObject ::= DefMInst
2406/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2407/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002408///
2409bool TGParser::ParseMultiClass() {
2410 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2411 Lex.Lex(); // Eat the multiclass token.
2412
2413 if (Lex.getCode() != tgtok::Id)
2414 return TokError("expected identifier after multiclass for name");
2415 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002416
Craig Topper7adf2bf2014-12-11 05:25:30 +00002417 auto Result =
2418 MultiClasses.insert(std::make_pair(Name,
2419 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2420
2421 if (!Result.second)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002422 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002423
Craig Topper7adf2bf2014-12-11 05:25:30 +00002424 CurMultiClass = Result.first->second.get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002425 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002426
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002427 // If there are template args, parse them.
2428 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002429 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002430 return true;
2431
David Greene7049e792009-04-24 16:55:41 +00002432 bool inherits = false;
2433
David Greene753ed8f2009-04-22 16:42:54 +00002434 // If there are submulticlasses, parse them.
2435 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002436 inherits = true;
2437
David Greene753ed8f2009-04-22 16:42:54 +00002438 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002439
David Greene753ed8f2009-04-22 16:42:54 +00002440 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002441 SubMultiClassReference SubMultiClass =
2442 ParseSubMultiClassReference(CurMultiClass);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002443 while (true) {
David Greene753ed8f2009-04-22 16:42:54 +00002444 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002445 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002446
David Greene753ed8f2009-04-22 16:42:54 +00002447 // Add it.
2448 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2449 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002450
David Greene753ed8f2009-04-22 16:42:54 +00002451 if (Lex.getCode() != tgtok::comma) break;
2452 Lex.Lex(); // eat ','.
2453 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2454 }
2455 }
2456
David Greene7049e792009-04-24 16:55:41 +00002457 if (Lex.getCode() != tgtok::l_brace) {
2458 if (!inherits)
2459 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002460 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002461 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002462 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002463 } else {
David Greene7049e792009-04-24 16:55:41 +00002464 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2465 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002466
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002467 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002468 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002469 default:
2470 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
2471 case tgtok::Let:
2472 case tgtok::Def:
2473 case tgtok::Defm:
2474 case tgtok::Foreach:
2475 if (ParseObject(CurMultiClass))
2476 return true;
2477 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002478 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002479 }
David Greene7049e792009-04-24 16:55:41 +00002480 Lex.Lex(); // eat the '}'.
2481 }
Bob Wilson7248f862009-11-22 04:24:42 +00002482
Craig Topper011817a2014-04-09 04:50:04 +00002483 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002484 return false;
2485}
2486
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002487Record *TGParser::InstantiateMulticlassDef(MultiClass &MC, Record *DefProto,
2488 Init *&DefmPrefix,
2489 SMRange DefmPrefixRange,
2490 ArrayRef<Init *> TArgs,
Matthias Braunc66e7552016-12-05 06:41:54 +00002491 ArrayRef<Init *> TemplateVals) {
David Greene5d5d88c2011-10-19 13:04:31 +00002492 // We need to preserve DefProto so it can be reused for later
2493 // instantiations, so create a new Record to inherit from it.
2494
David Greenedb445972011-10-05 22:42:07 +00002495 // Add in the defm name. If the defm prefix is empty, give each
2496 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2497 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2498 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002499
Jordan Roseabdd99b2013-01-10 18:50:05 +00002500 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002501 if (!DefmPrefix) {
Nicolai Haehnle73355bc2018-03-06 13:48:54 +00002502 DefmPrefix = Records.getNewAnonymousName();
Jordan Roseabdd99b2013-01-10 18:50:05 +00002503 IsAnonymous = true;
2504 }
David Greene5d5d88c2011-10-19 13:04:31 +00002505
2506 Init *DefName = DefProto->getNameInit();
Sean Silvafb509ed2012-10-10 20:24:43 +00002507 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002508
Craig Topper011817a2014-04-09 04:50:04 +00002509 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002510 // We have a fully expanded string so there are no operators to
2511 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002512 DefName =
2513 BinOpInit::get(BinOpInit::STRCONCAT,
2514 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2515 StringRecTy::get())->Fold(DefProto, &MC),
2516 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2517 }
David Greene5d5d88c2011-10-19 13:04:31 +00002518
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002519 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002520 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002521 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Craig Topper84138712014-11-29 05:31:10 +00002522 auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002523
2524 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002525 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002526 Ref.Rec = DefProto;
Craig Topper84138712014-11-29 05:31:10 +00002527 AddSubClass(CurRec.get(), Ref);
David Greenedb445972011-10-05 22:42:07 +00002528
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002529 // Set the value for NAME. We don't resolve references to it 'til later,
2530 // though, so that uses in nested multiclass names don't get
2531 // confused.
Matthias Braun215ff842016-12-05 07:35:13 +00002532 if (SetValue(CurRec.get(), Ref.RefRange.Start, StringInit::get("NAME"), None,
2533 DefmPrefix, /*AllowSelfAssignment*/true)) {
Craig Topper85c07002015-04-30 05:54:22 +00002534 Error(DefmPrefixRange.Start, "Could not resolve " +
2535 CurRec->getNameInitAsString() + ":NAME to '" +
2536 DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002537 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002538 }
David Greene8bf0d722011-10-19 13:04:35 +00002539
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002540 // If the DefNameString didn't resolve, we probably have a reference to
2541 // NAME and need to replace it. We need to do at least this much greedily,
2542 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002543 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002544 RecordVal *DefNameRV = CurRec->getValue("NAME");
2545 CurRec->resolveReferencesTo(DefNameRV);
2546 }
2547
2548 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002549 // Now that we're at the top level, resolve all NAME references
2550 // in the resultant defs that weren't in the def names themselves.
2551 RecordVal *DefNameRV = CurRec->getValue("NAME");
2552 CurRec->resolveReferencesTo(DefNameRV);
2553
Hal Finkeld2497362015-05-21 04:32:56 +00002554 // Check if the name is a complex pattern.
2555 // If so, resolve it.
2556 DefName = CurRec->getNameInit();
2557 DefNameString = dyn_cast<StringInit>(DefName);
2558
2559 // OK the pattern is more complex than simply using NAME.
2560 // Let's use the heavy weaponery.
2561 if (!DefNameString) {
2562 ResolveMulticlassDefArgs(MC, CurRec.get(), DefmPrefixRange.Start,
2563 Lex.getLoc(), TArgs, TemplateVals,
2564 false/*Delete args*/);
2565 DefName = CurRec->getNameInit();
2566 DefNameString = dyn_cast<StringInit>(DefName);
2567
Nicolai Haehnledfda9dc2018-03-06 13:48:39 +00002568 if (!DefNameString) {
Hal Finkeld2497362015-05-21 04:32:56 +00002569 DefName = DefName->convertInitializerTo(StringRecTy::get());
Nicolai Haehnledfda9dc2018-03-06 13:48:39 +00002570 DefNameString = dyn_cast<StringInit>(DefName);
2571 }
Hal Finkeld2497362015-05-21 04:32:56 +00002572
Hal Finkeld2497362015-05-21 04:32:56 +00002573 if (!DefNameString) {
2574 PrintFatalError(CurRec->getLoc()[CurRec->getLoc().size() - 1],
2575 DefName->getAsUnquotedString() + " is not a string.");
2576 return nullptr;
2577 }
2578
2579 CurRec->setName(DefName);
2580 }
2581
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002582 // Now that NAME references are resolved and we're at the top level of
2583 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002584 // currently in a multiclass, it means this defm appears inside a
2585 // multiclass and its name won't be fully resolvable until we see
Hal Finkeld2497362015-05-21 04:32:56 +00002586 // the top-level defm. Therefore, we don't add this to the
2587 // RecordKeeper at this point. If we did we could get duplicate
David Greene8bf0d722011-10-19 13:04:35 +00002588 // defs as more than one probably refers to NAME or some other
2589 // common internal placeholder.
2590
2591 // Ensure redefinition doesn't happen.
2592 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002593 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002594 "' already defined, instantiating defm with subdef '" +
2595 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002596 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002597 }
2598
Craig Topper84138712014-11-29 05:31:10 +00002599 Record *CurRecSave = CurRec.get(); // Keep a copy before we release.
Craig Toppercdab2322014-11-29 05:52:51 +00002600 Records.addDef(std::move(CurRec));
Craig Topper84138712014-11-29 05:31:10 +00002601 return CurRecSave;
David Greene8bf0d722011-10-19 13:04:35 +00002602 }
2603
Craig Topper84138712014-11-29 05:31:10 +00002604 // FIXME This is bad but the ownership transfer to caller is pretty messy.
2605 // The unique_ptr in this function at least protects the exits above.
2606 return CurRec.release();
David Greenedb445972011-10-05 22:42:07 +00002607}
2608
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002609bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC, Record *CurRec,
2610 SMLoc DefmPrefixLoc, SMLoc SubClassLoc,
2611 ArrayRef<Init *> TArgs,
Matthias Braunc66e7552016-12-05 06:41:54 +00002612 ArrayRef<Init *> TemplateVals,
David Greenedb445972011-10-05 22:42:07 +00002613 bool DeleteArgs) {
Nicolai Haehnle7d697852018-03-05 15:21:19 +00002614 // Set all template arguments to the specified value or leave them as the
2615 // default if necessary, then resolve them all simultaneously.
2616 MapResolver R(CurRec);
2617
David Greenedb445972011-10-05 22:42:07 +00002618 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2619 // Check if a value is specified for this temp-arg.
2620 if (i < TemplateVals.size()) {
Craig Toppercfd81732016-01-04 03:15:08 +00002621 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], None, TemplateVals[i]))
David Greenedb445972011-10-05 22:42:07 +00002622 return true;
David Greenedb445972011-10-05 22:42:07 +00002623 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Craig Topper85c07002015-04-30 05:54:22 +00002624 return Error(SubClassLoc, "value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00002625 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +00002626 ") of multiclassclass '" + MC.Rec.getNameInitAsString() +
2627 "'");
David Greenedb445972011-10-05 22:42:07 +00002628 }
Nicolai Haehnle7d697852018-03-05 15:21:19 +00002629
2630 R.set(TArgs[i], CurRec->getValue(TArgs[i])->getValue());
2631
2632 if (DeleteArgs)
2633 CurRec->removeValue(TArgs[i]);
David Greenedb445972011-10-05 22:42:07 +00002634 }
Nicolai Haehnle7d697852018-03-05 15:21:19 +00002635
2636 CurRec->resolveReferences(R);
2637
David Greenedb445972011-10-05 22:42:07 +00002638 return false;
2639}
2640
2641bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2642 Record *CurRec,
2643 Record *DefProto,
2644 SMLoc DefmPrefixLoc) {
2645 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002646 if (ApplyLetStack(CurRec))
2647 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002648
David Greenedb445972011-10-05 22:42:07 +00002649 // Don't create a top level definition for defm inside multiclasses,
2650 // instead, only update the prototypes and bind the template args
2651 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002652 if (!CurMultiClass)
2653 return false;
Craig Toppereb4d7c62015-04-29 04:43:36 +00002654 for (const auto &Proto : CurMultiClass->DefPrototypes)
2655 if (Proto->getNameInit() == CurRec->getNameInit())
Sean Silvacc951b22013-01-09 05:28:12 +00002656 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2657 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002658 CurMultiClass->DefPrototypes.push_back(std::unique_ptr<Record>(CurRec));
David Greenedb445972011-10-05 22:42:07 +00002659
Sean Silvacc951b22013-01-09 05:28:12 +00002660 // Copy the template arguments for the multiclass into the new def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002661 for (Init * TA : CurMultiClass->Rec.getTemplateArgs()) {
2662 const RecordVal *RV = CurMultiClass->Rec.getValue(TA);
Sean Silvacc951b22013-01-09 05:28:12 +00002663 assert(RV && "Template arg doesn't exist?");
2664 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002665 }
2666
2667 return false;
2668}
2669
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002670/// ParseDefm - Parse the instantiation of a multiclass.
2671///
2672/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2673///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002674bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002675 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002676 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002677 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002678
Craig Topperb21afc62013-01-07 05:09:33 +00002679 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002680 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002681 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002682
Jordan Rosef12e8a92013-01-10 18:50:11 +00002683 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002684 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002685 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002686
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002687 // Keep track of the new generated record definitions.
2688 std::vector<Record*> NewRecDefs;
2689
2690 // This record also inherits from a regular class (non-multiclass)?
2691 bool InheritFromClass = false;
2692
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002693 // eat the colon.
2694 Lex.Lex();
2695
Chris Lattner526c8cb2009-06-21 03:39:35 +00002696 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002697 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002698
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002699 while (true) {
Craig Topper011817a2014-04-09 04:50:04 +00002700 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002701
2702 // To instantiate a multiclass, we need to first get the multiclass, then
2703 // instantiate each def contained in the multiclass with the SubClassRef
2704 // template parameters.
Craig Topper7adf2bf2014-12-11 05:25:30 +00002705 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper4ca40012014-11-30 01:20:17 +00002706 assert(MC && "Didn't lookup multiclass correctly?");
Matthias Braunc66e7552016-12-05 06:41:54 +00002707 ArrayRef<Init*> TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002708
2709 // Verify that the correct number of template arguments were specified.
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002710 ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002711 if (TArgs.size() < TemplateVals.size())
2712 return Error(SubClassLoc,
2713 "more template args specified than multiclass expects");
2714
2715 // Loop over all the def's in the multiclass, instantiating each one.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002716 for (const std::unique_ptr<Record> &DefProto : MC->DefPrototypes) {
Hal Finkeld2497362015-05-21 04:32:56 +00002717 // The record name construction goes as follow:
2718 // - If the def name is a string, prepend the prefix.
2719 // - If the def name is a more complex pattern, use that pattern.
Craig Topper7f36be92016-02-26 06:50:27 +00002720 // As a result, the record is instantiated before resolving
Hal Finkeld2497362015-05-21 04:32:56 +00002721 // arguments, as it would make its name a string.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002722 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto.get(), DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002723 SMRange(DefmLoc,
Hal Finkeld2497362015-05-21 04:32:56 +00002724 DefmPrefixEndLoc),
2725 TArgs, TemplateVals);
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002726 if (!CurRec)
2727 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002728
Craig Topper7f36be92016-02-26 06:50:27 +00002729 // Now that the record is instantiated, we can resolve arguments.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002730 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002731 TArgs, TemplateVals, true/*Delete args*/))
2732 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002733
Craig Toppereb4d7c62015-04-29 04:43:36 +00002734 if (ResolveMulticlassDef(*MC, CurRec, DefProto.get(), DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002735 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002736
Adam Nemete5a07162014-09-16 17:14:13 +00002737 // Defs that can be used by other definitions should be fully resolved
2738 // before any use.
2739 if (DefProto->isResolveFirst() && !CurMultiClass) {
2740 CurRec->resolveReferences();
2741 CurRec->setResolveFirst(false);
2742 }
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002743 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002744 }
2745
David Greenedb445972011-10-05 22:42:07 +00002746
David Greenef00919a2009-04-22 22:17:51 +00002747 if (Lex.getCode() != tgtok::comma) break;
2748 Lex.Lex(); // eat ','.
2749
Craig Topper998a39a2013-08-20 04:22:09 +00002750 if (Lex.getCode() != tgtok::Id)
2751 return TokError("expected identifier");
2752
David Greenef00919a2009-04-22 22:17:51 +00002753 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002754
2755 // A defm can inherit from regular classes (non-multiclass) as
2756 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002757 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002758
2759 if (InheritFromClass)
2760 break;
2761
Craig Topper011817a2014-04-09 04:50:04 +00002762 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002763 }
2764
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002765 if (InheritFromClass) {
2766 // Process all the classes to inherit as if they were part of a
2767 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002768 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002769 while (true) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002770 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002771 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002772
2773 // Get the expanded definition prototypes and teach them about
2774 // the record values the current class to inherit has
Craig Toppereb4d7c62015-04-29 04:43:36 +00002775 for (Record *CurRec : NewRecDefs) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002776 // Add it.
2777 if (AddSubClass(CurRec, SubClass))
2778 return true;
2779
Sean Silvacb1a75e2013-01-09 04:49:14 +00002780 if (ApplyLetStack(CurRec))
2781 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002782 }
2783
2784 if (Lex.getCode() != tgtok::comma) break;
2785 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002786 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002787 }
2788 }
2789
Nicolai Haehnle0f529882018-03-06 13:48:47 +00002790 if (!CurMultiClass) {
2791 for (Record *CurRec : NewRecDefs) {
David Greene50c09122011-08-10 18:27:46 +00002792 // See Record::setName(). This resolve step will see any new
2793 // name for the def that might have been created when resolving
2794 // inheritance, values and arguments above.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002795 CurRec->resolveReferences();
Nicolai Haehnle0f529882018-03-06 13:48:47 +00002796 checkConcrete(*CurRec);
2797 }
2798 }
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002799
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002800 if (Lex.getCode() != tgtok::semi)
2801 return TokError("expected ';' at end of defm");
2802 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002803
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002804 return false;
2805}
2806
2807/// ParseObject
2808/// Object ::= ClassInst
2809/// Object ::= DefInst
2810/// Object ::= MultiClassInst
2811/// Object ::= DefMInst
2812/// Object ::= LETCommand '{' ObjectList '}'
2813/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002814bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002815 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002816 default:
2817 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002818 case tgtok::Let: return ParseTopLevelLet(MC);
2819 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002820 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002821 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002822 case tgtok::Class: return ParseClass();
2823 case tgtok::MultiClass: return ParseMultiClass();
2824 }
2825}
2826
2827/// ParseObjectList
2828/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002829bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002830 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002831 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002832 return true;
2833 }
2834 return false;
2835}
2836
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002837bool TGParser::ParseFile() {
2838 Lex.Lex(); // Prime the lexer.
2839 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002840
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002841 // If we have unread input at the end of the file, report it.
2842 if (Lex.getCode() == tgtok::Eof)
2843 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002844
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002845 return TokError("Unexpected input at top level");
2846}