blob: efd993d8b2bdb9b6449accd076dc0b09cc79e95c [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
409 IterRec->setName(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
Alp Tokerce91fe52013-12-21 18:51:00 +0000430/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
431/// an identifier.
Craig Topperf4412262017-06-01 06:56:16 +0000432Init *TGParser::GetNewAnonymousName() {
433 return StringInit::get("anonymous_" + utostr(AnonCounter++));
Chris Lattner7538ed82010-10-05 22:51:56 +0000434}
435
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000436/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000437/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000438/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000439/// ObjectName ::= /*empty*/
440///
David Greene2affd672011-10-19 13:04:29 +0000441Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
442 switch (Lex.getCode()) {
443 case tgtok::colon:
444 case tgtok::semi:
445 case tgtok::l_brace:
446 // These are all of the tokens that can begin an object body.
447 // Some of these can also begin values but we disallow those cases
448 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000449 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000450 default:
451 break;
452 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000453
Craig Topper011817a2014-04-09 04:50:04 +0000454 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000455 if (CurMultiClass)
456 CurRec = &CurMultiClass->Rec;
457
Craig Topper011817a2014-04-09 04:50:04 +0000458 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000459 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000460 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000461 if (!CurRecName) {
462 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000463 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000464 }
465 Type = CurRecName->getType();
466 }
467
468 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000469}
470
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000471/// ParseClassID - Parse and resolve a reference to a class name. This returns
472/// null on error.
473///
474/// ClassID ::= ID
475///
476Record *TGParser::ParseClassID() {
477 if (Lex.getCode() != tgtok::Id) {
478 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000479 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000480 }
Bob Wilson7248f862009-11-22 04:24:42 +0000481
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000482 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000483 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000484 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000485
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000486 Lex.Lex();
487 return Result;
488}
489
Bob Wilson3d948162009-04-28 19:41:44 +0000490/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
491/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000492///
493/// MultiClassID ::= ID
494///
495MultiClass *TGParser::ParseMultiClassID() {
496 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000497 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000498 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000499 }
Bob Wilson3d948162009-04-28 19:41:44 +0000500
Craig Topper7adf2bf2014-12-11 05:25:30 +0000501 MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
Craig Topper4ca40012014-11-30 01:20:17 +0000502 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000503 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000504
David Greene753ed8f2009-04-22 16:42:54 +0000505 Lex.Lex();
Craig Topper4ca40012014-11-30 01:20:17 +0000506 return Result;
David Greene753ed8f2009-04-22 16:42:54 +0000507}
508
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000509/// ParseSubClassReference - Parse a reference to a subclass or to a templated
510/// subclass. This returns a SubClassRefTy with a null Record* on error.
511///
512/// SubClassRef ::= ClassID
513/// SubClassRef ::= ClassID '<' ValueList '>'
514///
515SubClassReference TGParser::
516ParseSubClassReference(Record *CurRec, bool isDefm) {
517 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000518 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000519
Sean Silva0657b402013-01-09 02:17:14 +0000520 if (isDefm) {
521 if (MultiClass *MC = ParseMultiClassID())
522 Result.Rec = &MC->Rec;
523 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000524 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000525 }
Craig Topper011817a2014-04-09 04:50:04 +0000526 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000527
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000528 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000529 if (Lex.getCode() != tgtok::less) {
530 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000531 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000532 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000533 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000534
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000535 if (Lex.getCode() == tgtok::greater) {
536 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000537 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000538 return Result;
539 }
Bob Wilson7248f862009-11-22 04:24:42 +0000540
Matthias Braunc66e7552016-12-05 06:41:54 +0000541 ParseValueList(Result.TemplateArgs, CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000542 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000543 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000544 return Result;
545 }
Bob Wilson7248f862009-11-22 04:24:42 +0000546
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000547 if (Lex.getCode() != tgtok::greater) {
548 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000549 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000550 return Result;
551 }
552 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000553 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000554
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000555 return Result;
556}
557
Bob Wilson3d948162009-04-28 19:41:44 +0000558/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
559/// templated submulticlass. This returns a SubMultiClassRefTy with a null
560/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000561///
562/// SubMultiClassRef ::= MultiClassID
563/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
564///
565SubMultiClassReference TGParser::
566ParseSubMultiClassReference(MultiClass *CurMC) {
567 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000568 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000569
David Greene753ed8f2009-04-22 16:42:54 +0000570 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000571 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000572
David Greene753ed8f2009-04-22 16:42:54 +0000573 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000574 if (Lex.getCode() != tgtok::less) {
575 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000576 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000577 }
David Greene753ed8f2009-04-22 16:42:54 +0000578 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000579
David Greene753ed8f2009-04-22 16:42:54 +0000580 if (Lex.getCode() == tgtok::greater) {
581 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000582 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000583 return Result;
584 }
Bob Wilson3d948162009-04-28 19:41:44 +0000585
Matthias Braunc66e7552016-12-05 06:41:54 +0000586 ParseValueList(Result.TemplateArgs, &CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000587 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000588 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000589 return Result;
590 }
Bob Wilson3d948162009-04-28 19:41:44 +0000591
David Greene753ed8f2009-04-22 16:42:54 +0000592 if (Lex.getCode() != tgtok::greater) {
593 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000594 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000595 return Result;
596 }
597 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000598 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000599
600 return Result;
601}
602
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000603/// ParseRangePiece - Parse a bit/value range.
604/// RangePiece ::= INTVAL
605/// RangePiece ::= INTVAL '-' INTVAL
606/// RangePiece ::= INTVAL INTVAL
Matthias Braunc66e7552016-12-05 06:41:54 +0000607bool TGParser::ParseRangePiece(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000608 if (Lex.getCode() != tgtok::IntVal) {
609 TokError("expected integer or bitrange");
610 return true;
611 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000612 int64_t Start = Lex.getCurIntVal();
613 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000614
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000615 if (Start < 0)
616 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000617
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000618 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000619 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000620 Ranges.push_back(Start);
621 return false;
622 case tgtok::minus:
623 if (Lex.Lex() != tgtok::IntVal) {
624 TokError("expected integer value as end of range");
625 return true;
626 }
627 End = Lex.getCurIntVal();
628 break;
629 case tgtok::IntVal:
630 End = -Lex.getCurIntVal();
631 break;
632 }
Bob Wilson7248f862009-11-22 04:24:42 +0000633 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000634 return TokError("invalid range, cannot be negative");
635 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000636
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000637 // Add to the range.
Craig Toppera9642b42015-05-04 01:35:39 +0000638 if (Start < End)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000639 for (; Start <= End; ++Start)
640 Ranges.push_back(Start);
Craig Toppera9642b42015-05-04 01:35:39 +0000641 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000642 for (; Start >= End; --Start)
643 Ranges.push_back(Start);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000644 return false;
645}
646
647/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
648///
649/// RangeList ::= RangePiece (',' RangePiece)*
650///
Matthias Braunc66e7552016-12-05 06:41:54 +0000651void TGParser::ParseRangeList(SmallVectorImpl<unsigned> &Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000652 // Parse the first piece.
Matthias Braunc66e7552016-12-05 06:41:54 +0000653 if (ParseRangePiece(Result)) {
654 Result.clear();
655 return;
656 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000657 while (Lex.getCode() == tgtok::comma) {
658 Lex.Lex(); // Eat the comma.
659
660 // Parse the next range piece.
Matthias Braunc66e7552016-12-05 06:41:54 +0000661 if (ParseRangePiece(Result)) {
662 Result.clear();
663 return;
664 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000665 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000666}
667
668/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
669/// OptionalRangeList ::= '<' RangeList '>'
670/// OptionalRangeList ::= /*empty*/
Matthias Braunc66e7552016-12-05 06:41:54 +0000671bool TGParser::ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000672 if (Lex.getCode() != tgtok::less)
673 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000674
Chris Lattner526c8cb2009-06-21 03:39:35 +0000675 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000676 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000677
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000678 // Parse the range list.
Matthias Braunc66e7552016-12-05 06:41:54 +0000679 ParseRangeList(Ranges);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000680 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000681
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000682 if (Lex.getCode() != tgtok::greater) {
683 TokError("expected '>' at end of range list");
684 return Error(StartLoc, "to match this '<'");
685 }
686 Lex.Lex(); // eat the '>'.
687 return false;
688}
689
690/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
691/// OptionalBitList ::= '{' RangeList '}'
692/// OptionalBitList ::= /*empty*/
Matthias Braunc66e7552016-12-05 06:41:54 +0000693bool TGParser::ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000694 if (Lex.getCode() != tgtok::l_brace)
695 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000696
Chris Lattner526c8cb2009-06-21 03:39:35 +0000697 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000698 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000699
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000700 // Parse the range list.
Matthias Braunc66e7552016-12-05 06:41:54 +0000701 ParseRangeList(Ranges);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000702 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000703
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000704 if (Lex.getCode() != tgtok::r_brace) {
705 TokError("expected '}' at end of bit list");
706 return Error(StartLoc, "to match this '{'");
707 }
708 Lex.Lex(); // eat the '}'.
709 return false;
710}
711
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000712/// ParseType - Parse and return a tblgen type. This returns null on error.
713///
714/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000715/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000716/// Type ::= BIT // bit type
717/// Type ::= BITS '<' INTVAL '>' // bits<x> type
718/// Type ::= INT // int type
719/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000720/// Type ::= DAG // dag type
721/// Type ::= ClassID // Record Type
722///
723RecTy *TGParser::ParseType() {
724 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000725 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000726 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Tim Northover88403d72016-07-05 21:22:55 +0000727 case tgtok::Code: Lex.Lex(); return CodeRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000728 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
729 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000730 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000731 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000732 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000733 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000734 case tgtok::Bits: {
735 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
736 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000737 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000738 }
739 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
740 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000741 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000742 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000743 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000744 if (Lex.Lex() != tgtok::greater) { // Eat count.
745 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000746 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000747 }
748 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000749 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000750 }
751 case tgtok::List: {
752 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
753 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000754 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000755 }
756 Lex.Lex(); // Eat '<'
757 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000758 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000759
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000760 if (Lex.getCode() != tgtok::greater) {
761 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000762 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000763 }
764 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000765 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000766 }
Bob Wilson7248f862009-11-22 04:24:42 +0000767 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000768}
769
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000770/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
771/// has already been read.
Matthias Braun215ff842016-12-05 07:35:13 +0000772Init *TGParser::ParseIDValue(Record *CurRec, StringInit *Name, SMLoc NameLoc,
David Greened4263a62011-10-19 13:04:20 +0000773 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000774 if (CurRec) {
775 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000776 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000777
Matthias Braun215ff842016-12-05 07:35:13 +0000778 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
David Greenedb10e692011-10-19 13:02:42 +0000779
David Greene47a665e2011-10-05 22:42:54 +0000780 if (CurMultiClass)
Matthias Braun215ff842016-12-05 07:35:13 +0000781 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
David Greenedb10e692011-10-19 13:02:42 +0000782 "::");
David Greene47a665e2011-10-05 22:42:54 +0000783
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000784 if (CurRec->isTemplateArg(TemplateArgName)) {
785 const RecordVal *RV = CurRec->getValue(TemplateArgName);
786 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000787 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000788 }
Matthias Braun215ff842016-12-05 07:35:13 +0000789 }
Bob Wilson7248f862009-11-22 04:24:42 +0000790
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000791 if (CurMultiClass) {
Nicolai Haehnle3c80e4c2018-03-05 14:01:38 +0000792 if (Name->getValue() == "NAME")
793 return VarInit::get(Name, StringRecTy::get());
794
Matthias Braun215ff842016-12-05 07:35:13 +0000795 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name, "::");
David Greenedb10e692011-10-19 13:02:42 +0000796
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000797 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
798 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
799 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000800 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000801 }
802 }
Bob Wilson7248f862009-11-22 04:24:42 +0000803
David Greenefb927af2012-02-22 16:09:41 +0000804 // If this is in a foreach loop, make sure it's not a loop iterator
Craig Toppereb4d7c62015-04-29 04:43:36 +0000805 for (const auto &L : Loops) {
806 VarInit *IterVar = dyn_cast<VarInit>(L.IterVar);
Matthias Braun215ff842016-12-05 07:35:13 +0000807 if (IterVar && IterVar->getNameInit() == Name)
David Greenefb927af2012-02-22 16:09:41 +0000808 return IterVar;
809 }
810
David Greene232bd602011-10-19 13:04:21 +0000811 if (Mode == ParseNameMode)
Matthias Braun215ff842016-12-05 07:35:13 +0000812 return Name;
David Greene232bd602011-10-19 13:04:21 +0000813
Matthias Braun215ff842016-12-05 07:35:13 +0000814 if (Record *D = Records.getDef(Name->getValue()))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000815 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000816
David Greene232bd602011-10-19 13:04:21 +0000817 if (Mode == ParseValueMode) {
Matthias Braun215ff842016-12-05 07:35:13 +0000818 Error(NameLoc, "Variable not defined: '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000819 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000820 }
Craig Toppera9642b42015-05-04 01:35:39 +0000821
Matthias Braun215ff842016-12-05 07:35:13 +0000822 return Name;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000823}
824
David Greene5d0c0512009-05-14 20:54:48 +0000825/// ParseOperation - Parse an operator. This returns null on error.
826///
827/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
828///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000829Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000830 switch (Lex.getCode()) {
831 default:
832 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000833 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000834 case tgtok::XHead:
835 case tgtok::XTail:
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000836 case tgtok::XSize:
David Greene2f7cf7f2011-01-07 17:05:37 +0000837 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000838 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
839 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000840 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000841
David Greenee8f3b272009-05-14 21:22:49 +0000842 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000843 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000844 case tgtok::XCast:
845 Lex.Lex(); // eat the operation
846 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000847
David Greenee8f3b272009-05-14 21:22:49 +0000848 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000849
Craig Topper011817a2014-04-09 04:50:04 +0000850 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000851 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000852 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000853 }
David Greene5d0c0512009-05-14 20:54:48 +0000854
David Greenee8f3b272009-05-14 21:22:49 +0000855 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000856 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000857 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000858 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000859 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000860 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000861 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000862 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000863 break;
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000864 case tgtok::XSize:
865 Lex.Lex();
866 Code = UnOpInit::SIZE;
867 Type = IntRecTy::get();
868 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000869 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000870 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000871 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000872 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000873 break;
David Greenee8f3b272009-05-14 21:22:49 +0000874 }
875 if (Lex.getCode() != tgtok::l_paren) {
876 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000877 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000878 }
879 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000880
David Greeneaf8ee2c2011-07-29 22:43:06 +0000881 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000882 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000883
Craig Topper85c07002015-04-30 05:54:22 +0000884 if (Code == UnOpInit::HEAD ||
885 Code == UnOpInit::TAIL ||
886 Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000887 ListInit *LHSl = dyn_cast<ListInit>(LHS);
888 StringInit *LHSs = dyn_cast<StringInit>(LHS);
889 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000890 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000891 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000892 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000893 }
894 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000895 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
896 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000897 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000898 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000899 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000900 }
901 }
902
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000903 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL ||
904 Code == UnOpInit::SIZE) {
Craig Topper011817a2014-04-09 04:50:04 +0000905 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000906 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000907 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000908 }
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000909 }
Bob Wilson7248f862009-11-22 04:24:42 +0000910
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000911 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
Craig Topperec9072d2015-05-14 05:53:53 +0000912 if (LHSl && LHSl->empty()) {
David Greened571b3c2009-05-14 22:38:31 +0000913 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000914 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000915 }
916 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000917 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000918 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000919 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000920 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000921 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000922 }
Craig Toppera9642b42015-05-04 01:35:39 +0000923 Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
924 : ListRecTy::get(Itemt->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000925 } else {
David Greened571b3c2009-05-14 22:38:31 +0000926 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000927 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000928 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000929 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000930 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000931 }
Craig Toppera9642b42015-05-04 01:35:39 +0000932 Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
David Greened571b3c2009-05-14 22:38:31 +0000933 }
934 }
935 }
936
David Greenee8f3b272009-05-14 21:22:49 +0000937 if (Lex.getCode() != tgtok::r_paren) {
938 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000939 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000940 }
941 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000942 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000943 }
David Greene5d0c0512009-05-14 20:54:48 +0000944
945 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000946 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000947 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000948 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +0000949 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000950 case tgtok::XSRL:
951 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000952 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000953 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000954 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000955 tgtok::TokKind OpTok = Lex.getCode();
956 SMLoc OpLoc = Lex.getLoc();
957 Lex.Lex(); // eat the operation
958
David Greene5d0c0512009-05-14 20:54:48 +0000959 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000960 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000961
Chris Lattner61ea00b2010-10-05 23:58:18 +0000962 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000963 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000964 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000965 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000966 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000967 case tgtok::XOR: Code = BinOpInit::OR; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000968 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
969 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
970 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
971 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000972 case tgtok::XListConcat:
973 Code = BinOpInit::LISTCONCAT;
974 // We don't know the list type until we parse the first argument
975 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000976 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000977 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000978 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000979 break;
David Greene5d0c0512009-05-14 20:54:48 +0000980 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000981
David Greene5d0c0512009-05-14 20:54:48 +0000982 if (Lex.getCode() != tgtok::l_paren) {
983 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000984 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000985 }
986 Lex.Lex(); // eat the '('
987
David Greeneaf8ee2c2011-07-29 22:43:06 +0000988 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000989
Chris Lattner61ea00b2010-10-05 23:58:18 +0000990 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
Chris Lattner61ea00b2010-10-05 23:58:18 +0000993 while (Lex.getCode() == tgtok::comma) {
994 Lex.Lex(); // eat the ','
995
996 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000997 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000998 }
David Greene5d0c0512009-05-14 20:54:48 +0000999
1000 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +00001001 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +00001002 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001003 }
1004 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +00001005
Daniel Sanders314e80e2014-05-07 10:13:19 +00001006 // If we are doing !listconcat, we should know the type by now
1007 if (OpTok == tgtok::XListConcat) {
Nicolai Haehnlee4a2cf52018-02-22 15:26:28 +00001008 if (TypedInit *Arg0 = dyn_cast<TypedInit>(InitList[0]))
Daniel Sanders314e80e2014-05-07 10:13:19 +00001009 Type = Arg0->getType();
1010 else {
Matthias Braun8c209aa2017-01-28 02:02:38 +00001011 InitList[0]->print(errs());
Daniel Sanders314e80e2014-05-07 10:13:19 +00001012 Error(OpLoc, "expected a list");
1013 return nullptr;
1014 }
1015 }
1016
Chris Lattner61ea00b2010-10-05 23:58:18 +00001017 // We allow multiple operands to associative operators like !strconcat as
1018 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +00001019 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +00001020 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001021 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +00001022 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
1023 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +00001024 InitList.back() = RHS;
1025 }
1026 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00001027
Chris Lattner61ea00b2010-10-05 23:58:18 +00001028 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +00001029 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +00001030 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +00001031
Chris Lattner61ea00b2010-10-05 23:58:18 +00001032 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +00001033 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001034 }
1035
Nicolai Haehnle8ebf7e42018-03-05 15:21:04 +00001036 case tgtok::XForEach: { // Value ::= !foreach '(' Id ',' Value ',' Value ')'
1037 SMLoc OpLoc = Lex.getLoc();
1038 Lex.Lex(); // eat the operation
1039 if (Lex.getCode() != tgtok::l_paren) {
1040 TokError("expected '(' after !foreach");
1041 return nullptr;
1042 }
1043
1044 if (Lex.Lex() != tgtok::Id) { // eat the '('
1045 TokError("first argument of !foreach must be an identifier");
1046 return nullptr;
1047 }
1048
1049 Init *LHS = StringInit::get(Lex.getCurStrVal());
1050
1051 if (CurRec->getValue(LHS)) {
1052 TokError((Twine("iteration variable '") + LHS->getAsString() +
1053 "' already defined")
1054 .str());
1055 return nullptr;
1056 }
1057
1058 if (Lex.Lex() != tgtok::comma) { // eat the id
1059 TokError("expected ',' in ternary operator");
1060 return nullptr;
1061 }
1062 Lex.Lex(); // eat the ','
1063
1064 Init *MHS = ParseValue(CurRec);
1065 if (!MHS)
1066 return nullptr;
1067
1068 if (Lex.getCode() != tgtok::comma) {
1069 TokError("expected ',' in ternary operator");
1070 return nullptr;
1071 }
1072 Lex.Lex(); // eat the ','
1073
1074 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1075 if (!MHSt) {
1076 TokError("could not get type of !foreach input");
1077 return nullptr;
1078 }
1079
1080 RecTy *InEltType = nullptr;
1081 RecTy *OutEltType = nullptr;
1082 bool IsDAG = false;
1083
1084 if (ListRecTy *InListTy = dyn_cast<ListRecTy>(MHSt->getType())) {
1085 InEltType = InListTy->getElementType();
1086 if (ItemType) {
1087 if (ListRecTy *OutListTy = dyn_cast<ListRecTy>(ItemType)) {
1088 OutEltType = OutListTy->getElementType();
1089 } else {
1090 Error(OpLoc,
1091 "expected value of type '" + Twine(ItemType->getAsString()) +
1092 "', but got !foreach of list type");
1093 return nullptr;
1094 }
1095 }
1096 } else if (DagRecTy *InDagTy = dyn_cast<DagRecTy>(MHSt->getType())) {
1097 InEltType = InDagTy;
1098 if (ItemType && !isa<DagRecTy>(ItemType)) {
1099 Error(OpLoc,
1100 "expected value of type '" + Twine(ItemType->getAsString()) +
1101 "', but got !foreach of dag type");
1102 return nullptr;
1103 }
1104 IsDAG = true;
1105 } else {
1106 TokError("!foreach must have list or dag input");
1107 return nullptr;
1108 }
1109
1110 CurRec->addValue(RecordVal(LHS, InEltType, false));
1111 Init *RHS = ParseValue(CurRec, OutEltType);
1112 CurRec->removeValue(LHS);
1113 if (!RHS)
1114 return nullptr;
1115
1116 if (Lex.getCode() != tgtok::r_paren) {
1117 TokError("expected ')' in binary operator");
1118 return nullptr;
1119 }
1120 Lex.Lex(); // eat the ')'
1121
1122 RecTy *OutType;
1123 if (IsDAG) {
1124 OutType = InEltType;
1125 } else {
1126 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1127 if (!RHSt) {
1128 TokError("could not get type of !foreach result");
1129 return nullptr;
1130 }
1131 OutType = RHSt->getType()->getListTy();
1132 }
1133
1134 return (TernOpInit::get(TernOpInit::FOREACH, LHS, MHS, RHS, OutType))
1135 ->Fold(CurRec, CurMultiClass);
1136 }
1137
David Greene3587eed2009-05-14 23:26:46 +00001138 case tgtok::XIf:
David Greene98ed3c72009-05-14 21:54:42 +00001139 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
1140 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +00001141 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001142
David Greene98ed3c72009-05-14 21:54:42 +00001143 tgtok::TokKind LexCode = Lex.getCode();
1144 Lex.Lex(); // eat the operation
1145 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001146 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001147 case tgtok::XIf:
1148 Code = TernOpInit::IF;
1149 break;
David Greene98ed3c72009-05-14 21:54:42 +00001150 case tgtok::XSubst:
1151 Code = TernOpInit::SUBST;
1152 break;
1153 }
1154 if (Lex.getCode() != tgtok::l_paren) {
1155 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001156 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001157 }
1158 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001159
David Greeneaf8ee2c2011-07-29 22:43:06 +00001160 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001161 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001162
David Greene98ed3c72009-05-14 21:54:42 +00001163 if (Lex.getCode() != tgtok::comma) {
1164 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001165 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001166 }
1167 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001168
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001169 Init *MHS = ParseValue(CurRec, ItemType);
1170 if (!MHS)
1171 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001172
David Greene98ed3c72009-05-14 21:54:42 +00001173 if (Lex.getCode() != tgtok::comma) {
1174 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001175 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001176 }
1177 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001178
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001179 Init *RHS = ParseValue(CurRec, ItemType);
1180 if (!RHS)
1181 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001182
David Greene98ed3c72009-05-14 21:54:42 +00001183 if (Lex.getCode() != tgtok::r_paren) {
1184 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001185 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001186 }
1187 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001188
David Greene98ed3c72009-05-14 21:54:42 +00001189 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001190 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001191 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001192 RecTy *MHSTy = nullptr;
1193 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001194
Sean Silvafb509ed2012-10-10 20:24:43 +00001195 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001196 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001197 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001198 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001199 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001200 MHSTy = BitRecTy::get();
1201
Sean Silvafb509ed2012-10-10 20:24:43 +00001202 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001203 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001204 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001205 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001206 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001207 RHSTy = BitRecTy::get();
1208
1209 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001210 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001211 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001212 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001213 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001214
1215 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001216 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001217 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001218 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001219
Nicolai Haehnle13080fd2018-03-06 13:48:20 +00001220 Type = resolveTypes(MHSTy, RHSTy);
1221 if (!Type) {
1222 TokError(Twine("inconsistent types '") + MHSTy->getAsString() +
1223 "' and '" + RHSTy->getAsString() + "' for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001224 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001225 }
1226 break;
1227 }
David Greene98ed3c72009-05-14 21:54:42 +00001228 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001229 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001230 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001231 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001232 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001233 }
1234 Type = RHSt->getType();
1235 break;
1236 }
1237 }
David Greenee32ebf22011-07-29 19:07:07 +00001238 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001239 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001240 }
David Greene5d0c0512009-05-14 20:54:48 +00001241 }
David Greene5d0c0512009-05-14 20:54:48 +00001242}
1243
1244/// ParseOperatorType - Parse a type for an operator. This returns
1245/// null on error.
1246///
1247/// OperatorType ::= '<' Type '>'
1248///
Dan Gohman1432ef82009-08-12 22:10:57 +00001249RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001250 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001251
1252 if (Lex.getCode() != tgtok::less) {
1253 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001254 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001255 }
1256 Lex.Lex(); // eat the <
1257
1258 Type = ParseType();
1259
Craig Topper011817a2014-04-09 04:50:04 +00001260 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001261 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001262 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001263 }
1264
1265 if (Lex.getCode() != tgtok::greater) {
1266 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001267 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001268 }
1269 Lex.Lex(); // eat the >
1270
1271 return Type;
1272}
1273
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001274/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1275///
1276/// SimpleValue ::= IDValue
1277/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001278/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001279/// SimpleValue ::= CODEFRAGMENT
1280/// SimpleValue ::= '?'
1281/// SimpleValue ::= '{' ValueList '}'
1282/// SimpleValue ::= ID '<' ValueListNE '>'
1283/// SimpleValue ::= '[' ValueList ']'
1284/// SimpleValue ::= '(' IDValue DagArgList ')'
1285/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001286/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001287/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1288/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1289/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001290/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001291/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1292///
David Greened4263a62011-10-19 13:04:20 +00001293Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1294 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001295 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001296 switch (Lex.getCode()) {
1297 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001298 case tgtok::paste:
1299 // This is a leading paste operation. This is deprecated but
1300 // still exists in some .td files. Ignore it.
1301 Lex.Lex(); // Skip '#'.
1302 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001303 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001304 case tgtok::BinaryIntVal: {
1305 auto BinaryVal = Lex.getCurBinaryIntVal();
1306 SmallVector<Init*, 16> Bits(BinaryVal.second);
1307 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001308 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001309 R = BitsInit::get(Bits);
1310 Lex.Lex();
1311 break;
1312 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001313 case tgtok::StrVal: {
1314 std::string Val = Lex.getCurStrVal();
1315 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001316
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001317 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001318 while (Lex.getCode() == tgtok::StrVal) {
1319 Val += Lex.getCurStrVal();
1320 Lex.Lex();
1321 }
Bob Wilson7248f862009-11-22 04:24:42 +00001322
David Greenee32ebf22011-07-29 19:07:07 +00001323 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001324 break;
1325 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001326 case tgtok::CodeFragment:
Tim Northover88403d72016-07-05 21:22:55 +00001327 R = CodeInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001328 Lex.Lex();
1329 break;
1330 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001331 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001332 Lex.Lex();
1333 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001334 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001335 SMLoc NameLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00001336 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001337 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001338 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001339
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001340 // Value ::= ID '<' ValueListNE '>'
1341 if (Lex.Lex() == tgtok::greater) {
1342 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001343 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001344 }
David Greene8618f952009-06-08 20:23:18 +00001345
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001346 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1347 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1348 // body.
Matthias Braun215ff842016-12-05 07:35:13 +00001349 Record *Class = Records.getClass(Name->getValue());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001350 if (!Class) {
Matthias Braun215ff842016-12-05 07:35:13 +00001351 Error(NameLoc, "Expected a class name, got '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001352 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001353 }
David Greene8618f952009-06-08 20:23:18 +00001354
Matthias Braunc66e7552016-12-05 06:41:54 +00001355 SubClassReference SCRef;
1356 ParseValueList(SCRef.TemplateArgs, CurRec, Class);
1357 if (SCRef.TemplateArgs.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001358
David Greene8618f952009-06-08 20:23:18 +00001359 if (Lex.getCode() != tgtok::greater) {
1360 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001361 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001362 }
1363 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001364 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001365
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001366 // Create the new record, set it as CurRec temporarily.
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00001367 auto NewRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), NameLoc,
1368 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001369 Record *NewRec = NewRecOwner.get(); // Keep a copy since we may release.
Jordan Rosef12e8a92013-01-10 18:50:11 +00001370 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001371 SCRef.Rec = Class;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001372 // Add info about the subclass to NewRec.
Craig Topper84138712014-11-29 05:31:10 +00001373 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001374 return nullptr;
Craig Topper84138712014-11-29 05:31:10 +00001375
Hal Finkela8c1f462014-01-02 20:47:09 +00001376 if (!CurMultiClass) {
1377 NewRec->resolveReferences();
Craig Toppercdab2322014-11-29 05:52:51 +00001378 Records.addDef(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001379 } else {
Adam Nemete5a07162014-09-16 17:14:13 +00001380 // This needs to get resolved once the multiclass template arguments are
1381 // known before any use.
1382 NewRec->setResolveFirst(true);
Hal Finkela8c1f462014-01-02 20:47:09 +00001383 // Otherwise, we're inside a multiclass, add it to the multiclass.
Craig Topperc3504c42014-12-11 05:25:33 +00001384 CurMultiClass->DefPrototypes.push_back(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001385
1386 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00001387 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
1388 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Hal Finkela8c1f462014-01-02 20:47:09 +00001389 assert(RV && "Template arg doesn't exist?");
1390 NewRec->addValue(*RV);
1391 }
1392
1393 // We can't return the prototype def here, instead return:
1394 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1395 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1396 assert(MCNameRV && "multiclass record must have a NAME");
1397
1398 return UnOpInit::get(UnOpInit::CAST,
1399 BinOpInit::get(BinOpInit::STRCONCAT,
1400 VarInit::get(MCNameRV->getName(),
1401 MCNameRV->getType()),
1402 NewRec->getNameInit(),
1403 StringRecTy::get()),
Nicolai Haehnle13080fd2018-03-06 13:48:20 +00001404 NewRec->getDefInit()->getType());
Hal Finkela8c1f462014-01-02 20:47:09 +00001405 }
Bob Wilson7248f862009-11-22 04:24:42 +00001406
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001407 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001408 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001409 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001410 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001411 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001412 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001413 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001414
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001415 if (Lex.getCode() != tgtok::r_brace) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001416 ParseValueList(Vals, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001417 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001418 }
1419 if (Lex.getCode() != tgtok::r_brace) {
1420 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001421 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001422 }
1423 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001424
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001425 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001426
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001427 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1428 // first. We'll first read everything in to a vector, then we can reverse
1429 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001430 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001431 // FIXME: The following two loops would not be duplicated
1432 // if the API was a little more orthogonal.
1433
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001434 // bits<n> values are allowed to initialize n bits.
1435 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1436 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1437 NewBits.push_back(BI->getBit((e - i) - 1));
1438 continue;
1439 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001440 // bits<n> can also come from variable initializers.
1441 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1442 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1443 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1444 NewBits.push_back(VI->getBit((e - i) - 1));
1445 continue;
1446 }
1447 // Fallthrough to try convert this to a bit.
1448 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001449 // All other values must be convertible to just a single bit.
Nicolai Haehnledfda9dc2018-03-06 13:48:39 +00001450 Init *Bit = Vals[i]->getCastTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001451 if (!Bit) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001452 Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
Chris Lattner52416952007-11-22 21:06:59 +00001453 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001454 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001455 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001456 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001457 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001458 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001459 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001460 }
1461 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1462 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001463 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001464
Craig Topper011817a2014-04-09 04:50:04 +00001465 RecTy *DeducedEltTy = nullptr;
1466 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001467
Craig Topper011817a2014-04-09 04:50:04 +00001468 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001469 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001470 if (!ListType) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001471 TokError(Twine("Type mismatch for list, expected list type, got ") +
1472 ItemType->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001473 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001474 }
1475 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001476 }
David Greene8618f952009-06-08 20:23:18 +00001477
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001478 if (Lex.getCode() != tgtok::r_square) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001479 ParseValueList(Vals, CurRec, nullptr,
1480 GivenListTy ? GivenListTy->getElementType() : nullptr);
Craig Topper011817a2014-04-09 04:50:04 +00001481 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001482 }
1483 if (Lex.getCode() != tgtok::r_square) {
1484 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001485 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001486 }
1487 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001488
Craig Topper011817a2014-04-09 04:50:04 +00001489 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001490 if (Lex.getCode() == tgtok::less) {
1491 // Optional list element type
1492 Lex.Lex(); // eat the '<'
1493
1494 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001495 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001496 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001497 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001498 }
1499
1500 if (Lex.getCode() != tgtok::greater) {
1501 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001502 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001503 }
1504 Lex.Lex(); // eat the '>'
1505 }
1506
1507 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001508 RecTy *EltTy = nullptr;
Craig Toppereb4d7c62015-04-29 04:43:36 +00001509 for (Init *V : Vals) {
1510 TypedInit *TArg = dyn_cast<TypedInit>(V);
Craig Topper011817a2014-04-09 04:50:04 +00001511 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001512 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001513 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001514 }
Craig Topper011817a2014-04-09 04:50:04 +00001515 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001516 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001517 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001518 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001519 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001520 }
Bob Wilson7248f862009-11-22 04:24:42 +00001521 } else {
David Greene8618f952009-06-08 20:23:18 +00001522 EltTy = TArg->getType();
1523 }
1524 }
1525
Craig Topper011817a2014-04-09 04:50:04 +00001526 if (GivenEltTy) {
1527 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001528 // Verify consistency
1529 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1530 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001531 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001532 }
1533 }
1534 EltTy = GivenEltTy;
1535 }
1536
Craig Topper011817a2014-04-09 04:50:04 +00001537 if (!EltTy) {
1538 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001539 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001540 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001541 }
1542 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001543 } else {
David Greene8618f952009-06-08 20:23:18 +00001544 // Make sure the deduced type is compatible with the given type
1545 if (GivenListTy) {
1546 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
Nicolai Haehnlef19083d2018-02-22 15:26:21 +00001547 TokError(Twine("Element type mismatch for list: element type '") +
1548 EltTy->getAsString() + "' not convertible to '" +
1549 GivenListTy->getElementType()->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001550 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001551 }
1552 }
1553 DeducedEltTy = EltTy;
1554 }
Bob Wilson7248f862009-11-22 04:24:42 +00001555
David Greenee32ebf22011-07-29 19:07:07 +00001556 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001557 }
1558 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1559 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001560 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001561 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001562 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001563 }
Bob Wilson7248f862009-11-22 04:24:42 +00001564
David Greeneaf8ee2c2011-07-29 22:43:06 +00001565 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001566 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001567
Nate Begemandbe3f772009-03-19 05:21:56 +00001568 // If the operator name is present, parse it.
Matthias Braun7cf3b112016-12-05 06:00:41 +00001569 StringInit *OperatorName = nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001570 if (Lex.getCode() == tgtok::colon) {
1571 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1572 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001573 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001574 }
Matthias Braun7cf3b112016-12-05 06:00:41 +00001575 OperatorName = StringInit::get(Lex.getCurStrVal());
Nate Begemandbe3f772009-03-19 05:21:56 +00001576 Lex.Lex(); // eat the VarName.
1577 }
Bob Wilson7248f862009-11-22 04:24:42 +00001578
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001579 SmallVector<std::pair<llvm::Init*, StringInit*>, 8> DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001580 if (Lex.getCode() != tgtok::r_paren) {
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001581 ParseDagArgList(DagArgs, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001582 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001583 }
Bob Wilson7248f862009-11-22 04:24:42 +00001584
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001585 if (Lex.getCode() != tgtok::r_paren) {
1586 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001587 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001588 }
1589 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001590
David Greenee32ebf22011-07-29 19:07:07 +00001591 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001592 }
Bob Wilson7248f862009-11-22 04:24:42 +00001593
David Greene2f7cf7f2011-01-07 17:05:37 +00001594 case tgtok::XHead:
1595 case tgtok::XTail:
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +00001596 case tgtok::XSize:
David Greene2f7cf7f2011-01-07 17:05:37 +00001597 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001598 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001599 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001600 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001601 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +00001602 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +00001603 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001604 case tgtok::XSRL:
1605 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001606 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001607 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001608 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001609 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001610 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001611 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001612 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001613 }
1614 }
Bob Wilson7248f862009-11-22 04:24:42 +00001615
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001616 return R;
1617}
1618
1619/// ParseValue - Parse a tblgen value. This returns null on error.
1620///
1621/// Value ::= SimpleValue ValueSuffix*
1622/// ValueSuffix ::= '{' BitList '}'
1623/// ValueSuffix ::= '[' BitList ']'
1624/// ValueSuffix ::= '.' ID
1625///
David Greened4263a62011-10-19 13:04:20 +00001626Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1627 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001628 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001629
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001630 // Parse the suffixes now if present.
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001631 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001632 switch (Lex.getCode()) {
1633 default: return Result;
1634 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001635 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001636 // This is the beginning of the object body.
1637 return Result;
1638
Chris Lattner526c8cb2009-06-21 03:39:35 +00001639 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001640 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001641 SmallVector<unsigned, 16> Ranges;
1642 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001643 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001644
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001645 // Reverse the bitlist.
1646 std::reverse(Ranges.begin(), Ranges.end());
1647 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001648 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001649 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001650 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001651 }
Bob Wilson7248f862009-11-22 04:24:42 +00001652
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001653 // Eat the '}'.
1654 if (Lex.getCode() != tgtok::r_brace) {
1655 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001656 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001657 }
1658 Lex.Lex();
1659 break;
1660 }
1661 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001662 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001663 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001664 SmallVector<unsigned, 16> Ranges;
1665 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001666 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001667
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001668 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001669 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001670 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001671 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001672 }
Bob Wilson7248f862009-11-22 04:24:42 +00001673
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001674 // Eat the ']'.
1675 if (Lex.getCode() != tgtok::r_square) {
1676 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001677 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001678 }
1679 Lex.Lex();
1680 break;
1681 }
Matthias Braun6a441832016-12-05 06:00:36 +00001682 case tgtok::period: {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001683 if (Lex.Lex() != tgtok::Id) { // eat the .
1684 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001685 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001686 }
Matthias Braun6a441832016-12-05 06:00:36 +00001687 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
1688 if (!Result->getFieldType(FieldName)) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001689 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001690 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001691 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001692 }
Matthias Braun6a441832016-12-05 06:00:36 +00001693 Result = FieldInit::get(Result, FieldName);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001694 Lex.Lex(); // eat field name
1695 break;
Matthias Braun6a441832016-12-05 06:00:36 +00001696 }
David Greene8e85b482011-10-19 13:04:43 +00001697
1698 case tgtok::paste:
1699 SMLoc PasteLoc = Lex.getLoc();
1700
1701 // Create a !strconcat() operation, first casting each operand to
1702 // a string if necessary.
1703
Sean Silvafb509ed2012-10-10 20:24:43 +00001704 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001705 if (!LHS) {
1706 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001707 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001708 }
Craig Toppera9642b42015-05-04 01:35:39 +00001709
David Greene8e85b482011-10-19 13:04:43 +00001710 if (LHS->getType() != StringRecTy::get()) {
1711 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1712 }
1713
Craig Topper011817a2014-04-09 04:50:04 +00001714 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001715
1716 Lex.Lex(); // Eat the '#'.
1717 switch (Lex.getCode()) {
1718 case tgtok::colon:
1719 case tgtok::semi:
1720 case tgtok::l_brace:
1721 // These are all of the tokens that can begin an object body.
1722 // Some of these can also begin values but we disallow those cases
1723 // because they are unlikely to be useful.
Craig Toppera9642b42015-05-04 01:35:39 +00001724
David Greene8e85b482011-10-19 13:04:43 +00001725 // Trailing paste, concat with an empty string.
1726 RHS = StringInit::get("");
1727 break;
1728
1729 default:
1730 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001731 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001732 if (!RHS) {
1733 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001734 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001735 }
1736
1737 if (RHS->getType() != StringRecTy::get()) {
1738 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1739 }
Craig Toppera9642b42015-05-04 01:35:39 +00001740
David Greene8e85b482011-10-19 13:04:43 +00001741 break;
1742 }
1743
1744 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1745 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1746 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001747 }
1748 }
1749}
1750
1751/// ParseDagArgList - Parse the argument list for a dag literal expression.
1752///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001753/// DagArg ::= Value (':' VARNAME)?
1754/// DagArg ::= VARNAME
1755/// DagArgList ::= DagArg
1756/// DagArgList ::= DagArgList ',' DagArg
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001757void TGParser::ParseDagArgList(
1758 SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
1759 Record *CurRec) {
Bob Wilson7248f862009-11-22 04:24:42 +00001760
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001761 while (true) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001762 // DagArg ::= VARNAME
1763 if (Lex.getCode() == tgtok::VarName) {
1764 // A missing value is treated like '?'.
Matthias Braunbb053162016-12-05 06:00:46 +00001765 StringInit *VarName = StringInit::get(Lex.getCurStrVal());
1766 Result.emplace_back(UnsetInit::get(), VarName);
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001767 Lex.Lex();
1768 } else {
1769 // DagArg ::= Value (':' VARNAME)?
1770 Init *Val = ParseValue(CurRec);
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001771 if (!Val) {
1772 Result.clear();
1773 return;
1774 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001775
1776 // If the variable name is present, add it.
Matthias Braunbb053162016-12-05 06:00:46 +00001777 StringInit *VarName = nullptr;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001778 if (Lex.getCode() == tgtok::colon) {
1779 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1780 TokError("expected variable name in dag literal");
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001781 Result.clear();
1782 return;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001783 }
Matthias Braunbb053162016-12-05 06:00:46 +00001784 VarName = StringInit::get(Lex.getCurStrVal());
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001785 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001786 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001787
1788 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001789 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001790 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001791 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001792 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001793}
1794
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001795/// ParseValueList - Parse a comma separated list of values, returning them as a
1796/// vector. Note that this always expects to be able to parse at least one
1797/// value. It returns an empty list if this is not possible.
1798///
1799/// ValueList ::= Value (',' Value)
1800///
Matthias Braunc66e7552016-12-05 06:41:54 +00001801void TGParser::ParseValueList(SmallVectorImpl<Init*> &Result, Record *CurRec,
1802 Record *ArgsRec, RecTy *EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001803 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001804 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001805 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001806 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001807 if (TArgs.empty()) {
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001808 TokError("template argument provided to non-template class");
Matthias Braunc66e7552016-12-05 06:41:54 +00001809 Result.clear();
1810 return;
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001811 }
David Greene8618f952009-06-08 20:23:18 +00001812 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001813 if (!RV) {
1814 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1815 << ")\n";
1816 }
David Greene8618f952009-06-08 20:23:18 +00001817 assert(RV && "Template argument record not found??");
1818 ItemType = RV->getType();
1819 ++ArgN;
1820 }
1821 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00001822 if (!Result.back()) {
1823 Result.clear();
1824 return;
1825 }
Bob Wilson7248f862009-11-22 04:24:42 +00001826
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001827 while (Lex.getCode() == tgtok::comma) {
1828 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001829
Craig Topper011817a2014-04-09 04:50:04 +00001830 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001831 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001832 if (ArgN >= TArgs.size()) {
1833 TokError("too many template arguments");
Matthias Braunc66e7552016-12-05 06:41:54 +00001834 Result.clear();
1835 return;
Bob Wilson7248f862009-11-22 04:24:42 +00001836 }
David Greene8618f952009-06-08 20:23:18 +00001837 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1838 assert(RV && "Template argument record not found??");
1839 ItemType = RV->getType();
1840 ++ArgN;
1841 }
1842 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00001843 if (!Result.back()) {
1844 Result.clear();
1845 return;
1846 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001847 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001848}
1849
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001850/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1851/// empty string on error. This can happen in a number of different context's,
1852/// including within a def or in the template args for a def (which which case
1853/// CurRec will be non-null) and within the template args for a multiclass (in
1854/// which case CurRec will be null, but CurMultiClass will be set). This can
1855/// also happen within a def that is within a multiclass, which will set both
1856/// CurRec and CurMultiClass.
1857///
1858/// Declaration ::= FIELD? Type ID ('=' Value)?
1859///
David Greenedb10e692011-10-19 13:02:42 +00001860Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001861 bool ParsingTemplateArgs) {
1862 // Read the field prefix if present.
1863 bool HasField = Lex.getCode() == tgtok::Field;
1864 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001865
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001866 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001867 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001868
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001869 if (Lex.getCode() != tgtok::Id) {
1870 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001871 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001872 }
Bob Wilson7248f862009-11-22 04:24:42 +00001873
Chris Lattner526c8cb2009-06-21 03:39:35 +00001874 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001875 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001876 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001877
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001878 if (ParsingTemplateArgs) {
Craig Toppera9642b42015-05-04 01:35:39 +00001879 if (CurRec)
David Greenedb10e692011-10-19 13:02:42 +00001880 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Craig Toppera9642b42015-05-04 01:35:39 +00001881 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001882 assert(CurMultiClass);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001883 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001884 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1885 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001886 }
Bob Wilson7248f862009-11-22 04:24:42 +00001887
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001888 // Add the value.
1889 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001890 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001891
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001892 // If a value is present, parse it.
1893 if (Lex.getCode() == tgtok::equal) {
1894 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001895 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001896 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001897 if (!Val ||
Craig Toppercfd81732016-01-04 03:15:08 +00001898 SetValue(CurRec, ValLoc, DeclName, None, Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001899 // Return the name, even if an error is thrown. This is so that we can
1900 // continue to make some progress, even without the value having been
1901 // initialized.
1902 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001903 }
Bob Wilson7248f862009-11-22 04:24:42 +00001904
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001905 return DeclName;
1906}
1907
David Greenefb927af2012-02-22 16:09:41 +00001908/// ParseForeachDeclaration - Read a foreach declaration, returning
1909/// the name of the declared object or a NULL Init on error. Return
1910/// the name of the parsed initializer list through ForeachListName.
1911///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001912/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1913/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1914/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001915///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001916VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001917 if (Lex.getCode() != tgtok::Id) {
1918 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001919 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001920 }
1921
1922 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1923 Lex.Lex();
1924
1925 // If a value is present, parse it.
1926 if (Lex.getCode() != tgtok::equal) {
1927 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001928 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001929 }
1930 Lex.Lex(); // Eat the '='
1931
Craig Topper011817a2014-04-09 04:50:04 +00001932 RecTy *IterType = nullptr;
Matthias Braunc66e7552016-12-05 06:41:54 +00001933 SmallVector<unsigned, 16> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001934
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001935 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001936 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001937 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001938 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001939 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001940 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001941 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001942 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001943 }
1944 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001945 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001946 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001947 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001948 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001949 }
1950 IterType = ListType->getElementType();
1951 break;
David Greenefb927af2012-02-22 16:09:41 +00001952 }
1953
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001954 case tgtok::IntVal: { // RangePiece.
1955 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001956 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001957 break;
David Greenefb927af2012-02-22 16:09:41 +00001958 }
1959
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001960 case tgtok::l_brace: { // '{' RangeList '}'
1961 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001962 ParseRangeList(Ranges);
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001963 if (Lex.getCode() != tgtok::r_brace) {
1964 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001965 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001966 }
1967 Lex.Lex();
1968 break;
1969 }
1970 }
David Greenefb927af2012-02-22 16:09:41 +00001971
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001972 if (!Ranges.empty()) {
1973 assert(!IterType && "Type already initialized?");
1974 IterType = IntRecTy::get();
1975 std::vector<Init*> Values;
Craig Topper8eb764c2015-06-02 06:19:28 +00001976 for (unsigned R : Ranges)
1977 Values.push_back(IntInit::get(R));
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001978 ForeachListValue = ListInit::get(Values, IterType);
1979 }
1980
1981 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001982 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001983
1984 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001985}
1986
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001987/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1988/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1989/// template args for a def, which may or may not be in a multiclass. If null,
1990/// these are the template args for a multiclass.
1991///
1992/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001993///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001994bool TGParser::ParseTemplateArgList(Record *CurRec) {
1995 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1996 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001997
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001998 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001999
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002000 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00002001 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00002002 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002003 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002004
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002005 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00002006
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002007 while (Lex.getCode() == tgtok::comma) {
2008 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00002009
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002010 // Read the following declarations.
2011 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00002012 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002013 return true;
2014 TheRecToAddTo->addTemplateArg(TemplArg);
2015 }
Bob Wilson7248f862009-11-22 04:24:42 +00002016
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002017 if (Lex.getCode() != tgtok::greater)
2018 return TokError("expected '>' at end of template argument list");
2019 Lex.Lex(); // eat the '>'.
2020 return false;
2021}
2022
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002023/// ParseBodyItem - Parse a single item at within the body of a def or class.
2024///
2025/// BodyItem ::= Declaration ';'
2026/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
2027bool TGParser::ParseBodyItem(Record *CurRec) {
2028 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00002029 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002030 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002031
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002032 if (Lex.getCode() != tgtok::semi)
2033 return TokError("expected ';' after declaration");
2034 Lex.Lex();
2035 return false;
2036 }
2037
2038 // LET ID OptionalRangeList '=' Value ';'
2039 if (Lex.Lex() != tgtok::Id)
2040 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00002041
Chris Lattner526c8cb2009-06-21 03:39:35 +00002042 SMLoc IdLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00002043 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002044 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00002045
Matthias Braunc66e7552016-12-05 06:41:54 +00002046 SmallVector<unsigned, 16> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00002047 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002048 return true;
2049 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002050
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002051 if (Lex.getCode() != tgtok::equal)
2052 return TokError("expected '=' in let expression");
2053 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002054
David Greene8618f952009-06-08 20:23:18 +00002055 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00002056 if (!Field)
Matthias Braun215ff842016-12-05 07:35:13 +00002057 return TokError("Value '" + FieldName->getValue() + "' unknown!");
David Greene8618f952009-06-08 20:23:18 +00002058
2059 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00002060
David Greeneaf8ee2c2011-07-29 22:43:06 +00002061 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00002062 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002063
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002064 if (Lex.getCode() != tgtok::semi)
2065 return TokError("expected ';' after let expression");
2066 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002067
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002068 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
2069}
2070
2071/// ParseBody - Read the body of a class or def. Return true on error, false on
2072/// success.
2073///
2074/// Body ::= ';'
2075/// Body ::= '{' BodyList '}'
2076/// BodyList BodyItem*
2077///
2078bool TGParser::ParseBody(Record *CurRec) {
2079 // If this is a null definition, just eat the semi and return.
2080 if (Lex.getCode() == tgtok::semi) {
2081 Lex.Lex();
2082 return false;
2083 }
Bob Wilson7248f862009-11-22 04:24:42 +00002084
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002085 if (Lex.getCode() != tgtok::l_brace)
2086 return TokError("Expected ';' or '{' to start body");
2087 // Eat the '{'.
2088 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002089
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002090 while (Lex.getCode() != tgtok::r_brace)
2091 if (ParseBodyItem(CurRec))
2092 return true;
2093
2094 // Eat the '}'.
2095 Lex.Lex();
2096 return false;
2097}
2098
Sean Silvacb1a75e2013-01-09 04:49:14 +00002099/// \brief Apply the current let bindings to \a CurRec.
2100/// \returns true on error, false otherwise.
2101bool TGParser::ApplyLetStack(Record *CurRec) {
Matthias Braunc66e7552016-12-05 06:41:54 +00002102 for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
Craig Topper8eb764c2015-06-02 06:19:28 +00002103 for (LetRecord &LR : LetInfo)
2104 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
Sean Silvacb1a75e2013-01-09 04:49:14 +00002105 return true;
2106 return false;
2107}
2108
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002109/// ParseObjectBody - Parse the body of a def or class. This consists of an
2110/// optional ClassList followed by a Body. CurRec is the current def or class
2111/// that is being parsed.
2112///
2113/// ObjectBody ::= BaseClassList Body
2114/// BaseClassList ::= /*empty*/
2115/// BaseClassList ::= ':' BaseClassListNE
2116/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
2117///
2118bool TGParser::ParseObjectBody(Record *CurRec) {
2119 // If there is a baseclass list, read it.
2120 if (Lex.getCode() == tgtok::colon) {
2121 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002122
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002123 // Read all of the subclasses.
2124 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002125 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002126 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002127 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002128
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002129 // Add it.
2130 if (AddSubClass(CurRec, SubClass))
2131 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002132
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002133 if (Lex.getCode() != tgtok::comma) break;
2134 Lex.Lex(); // eat ','.
2135 SubClass = ParseSubClassReference(CurRec, false);
2136 }
2137 }
2138
Sean Silvacb1a75e2013-01-09 04:49:14 +00002139 if (ApplyLetStack(CurRec))
2140 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002141
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002142 return ParseBody(CurRec);
2143}
2144
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002145/// ParseDef - Parse and return a top level or multiclass def, return the record
2146/// corresponding to it. This returns null on error.
2147///
2148/// DefInst ::= DEF ObjectName ObjectBody
2149///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002150bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00002151 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002152 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00002153 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002154
2155 // Parse ObjectName and make a record for it.
Craig Topper84138712014-11-29 05:31:10 +00002156 std::unique_ptr<Record> CurRecOwner;
Jordan Roseabdd99b2013-01-10 18:50:05 +00002157 Init *Name = ParseObjectName(CurMultiClass);
2158 if (Name)
Craig Topper84138712014-11-29 05:31:10 +00002159 CurRecOwner = make_unique<Record>(Name, DefLoc, Records);
Jordan Roseabdd99b2013-01-10 18:50:05 +00002160 else
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00002161 CurRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), DefLoc,
2162 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00002163 Record *CurRec = CurRecOwner.get(); // Keep a copy since we may release.
Bob Wilson7248f862009-11-22 04:24:42 +00002164
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002165 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002166 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002167
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002168 // Ensure redefinition doesn't happen.
Craig Topper84138712014-11-29 05:31:10 +00002169 if (Records.getDef(CurRec->getNameInitAsString()))
2170 return Error(DefLoc, "def '" + CurRec->getNameInitAsString()+
2171 "' already defined");
Craig Toppercdab2322014-11-29 05:52:51 +00002172 Records.addDef(std::move(CurRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00002173
2174 if (ParseObjectBody(CurRec))
2175 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002176 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002177 // Parse the body before adding this prototype to the DefPrototypes vector.
2178 // That way implicit definitions will be added to the DefPrototypes vector
2179 // before this object, instantiated prior to defs derived from this object,
2180 // and this available for indirect name resolution when defs derived from
2181 // this object are instantiated.
Craig Topper84138712014-11-29 05:31:10 +00002182 if (ParseObjectBody(CurRec))
Hal Finkela8c1f462014-01-02 20:47:09 +00002183 return true;
2184
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002185 // Otherwise, a def inside a multiclass, add it to the multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002186 for (const auto &Proto : CurMultiClass->DefPrototypes)
2187 if (Proto->getNameInit() == CurRec->getNameInit())
Craig Topper84138712014-11-29 05:31:10 +00002188 return Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
2189 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002190 CurMultiClass->DefPrototypes.push_back(std::move(CurRecOwner));
Anton Yartsev671dff12014-08-08 00:29:54 +00002191 } else if (ParseObjectBody(CurRec)) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002192 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002193 }
Bob Wilson7248f862009-11-22 04:24:42 +00002194
Nicolai Haehnle0f529882018-03-06 13:48:47 +00002195 if (!CurMultiClass) { // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002196 // See Record::setName(). This resolve step will see any new name
2197 // for the def that might have been created when resolving
2198 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002199 CurRec->resolveReferences();
Nicolai Haehnle0f529882018-03-06 13:48:47 +00002200 if (Loops.empty())
2201 checkConcrete(*CurRec);
2202 }
Bob Wilson7248f862009-11-22 04:24:42 +00002203
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002204 // If ObjectBody has template arguments, it's an error.
2205 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002206
2207 if (CurMultiClass) {
2208 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002209 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
2210 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002211 assert(RV && "Template arg doesn't exist?");
2212 CurRec->addValue(*RV);
2213 }
2214 }
2215
Craig Toppera9642b42015-05-04 01:35:39 +00002216 if (ProcessForeachDefs(CurRec, DefLoc))
Craig Topper84138712014-11-29 05:31:10 +00002217 return Error(DefLoc, "Could not process loops for def" +
2218 CurRec->getNameInitAsString());
David Greenefb927af2012-02-22 16:09:41 +00002219
2220 return false;
2221}
2222
2223/// ParseForeach - Parse a for statement. Return the record corresponding
2224/// to it. This returns true on error.
2225///
2226/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2227/// Foreach ::= FOREACH Declaration IN Object
2228///
2229bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2230 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2231 Lex.Lex(); // Eat the 'for' token.
2232
2233 // Make a temporary object to record items associated with the for
2234 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002235 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002236 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002237 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002238 return TokError("expected declaration in for");
2239
2240 if (Lex.getCode() != tgtok::In)
2241 return TokError("Unknown tok");
2242 Lex.Lex(); // Eat the in
2243
2244 // Create a loop object and remember it.
2245 Loops.push_back(ForeachLoop(IterName, ListValue));
2246
2247 if (Lex.getCode() != tgtok::l_brace) {
2248 // FOREACH Declaration IN Object
2249 if (ParseObject(CurMultiClass))
2250 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002251 } else {
David Greenefb927af2012-02-22 16:09:41 +00002252 SMLoc BraceLoc = Lex.getLoc();
2253 // Otherwise, this is a group foreach.
2254 Lex.Lex(); // eat the '{'.
2255
2256 // Parse the object list.
2257 if (ParseObjectList(CurMultiClass))
2258 return true;
2259
2260 if (Lex.getCode() != tgtok::r_brace) {
2261 TokError("expected '}' at end of foreach command");
2262 return Error(BraceLoc, "to match this '{'");
2263 }
2264 Lex.Lex(); // Eat the }
2265 }
2266
2267 // We've processed everything in this loop.
2268 Loops.pop_back();
2269
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002270 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002271}
2272
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002273/// ParseClass - Parse a tblgen class definition.
2274///
2275/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2276///
2277bool TGParser::ParseClass() {
2278 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2279 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002280
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002281 if (Lex.getCode() != tgtok::Id)
2282 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002283
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002284 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2285 if (CurRec) {
2286 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002287 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002288 !CurRec->getSuperClasses().empty() ||
2289 !CurRec->getTemplateArgs().empty())
Craig Topper85c07002015-04-30 05:54:22 +00002290 return TokError("Class '" + CurRec->getNameInitAsString() +
2291 "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002292 } else {
2293 // If this is the first reference to this class, create and add it.
Hans Wennborgffbbd532014-11-30 00:31:49 +00002294 auto NewRec =
2295 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
Craig Toppercdab2322014-11-29 05:52:51 +00002296 CurRec = NewRec.get();
2297 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002298 }
2299 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002300
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002301 // If there are template args, parse them.
2302 if (Lex.getCode() == tgtok::less)
2303 if (ParseTemplateArgList(CurRec))
2304 return true;
2305
2306 // Finally, parse the object body.
2307 return ParseObjectBody(CurRec);
2308}
2309
2310/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2311/// of LetRecords.
2312///
2313/// LetList ::= LetItem (',' LetItem)*
2314/// LetItem ::= ID OptionalRangeList '=' Value
2315///
Matthias Braunc66e7552016-12-05 06:41:54 +00002316void TGParser::ParseLetList(SmallVectorImpl<LetRecord> &Result) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002317 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002318 if (Lex.getCode() != tgtok::Id) {
2319 TokError("expected identifier in let definition");
Matthias Braunc66e7552016-12-05 06:41:54 +00002320 Result.clear();
2321 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002322 }
Matthias Braunc66e7552016-12-05 06:41:54 +00002323
Matthias Braun215ff842016-12-05 07:35:13 +00002324 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattner526c8cb2009-06-21 03:39:35 +00002325 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002326 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002327
2328 // Check for an optional RangeList.
Matthias Braunc66e7552016-12-05 06:41:54 +00002329 SmallVector<unsigned, 16> Bits;
2330 if (ParseOptionalRangeList(Bits)) {
2331 Result.clear();
2332 return;
2333 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002334 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002335
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002336 if (Lex.getCode() != tgtok::equal) {
2337 TokError("expected '=' in let expression");
Matthias Braunc66e7552016-12-05 06:41:54 +00002338 Result.clear();
2339 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002340 }
2341 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002342
Craig Topper011817a2014-04-09 04:50:04 +00002343 Init *Val = ParseValue(nullptr);
Matthias Braunc66e7552016-12-05 06:41:54 +00002344 if (!Val) {
2345 Result.clear();
2346 return;
2347 }
Bob Wilson7248f862009-11-22 04:24:42 +00002348
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002349 // Now that we have everything, add the record.
Matthias Braun215ff842016-12-05 07:35:13 +00002350 Result.emplace_back(Name, Bits, Val, NameLoc);
Bob Wilson7248f862009-11-22 04:24:42 +00002351
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002352 if (Lex.getCode() != tgtok::comma)
Matthias Braunc66e7552016-12-05 06:41:54 +00002353 return;
Bob Wilson7248f862009-11-22 04:24:42 +00002354 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002355 }
2356}
2357
2358/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002359/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002360///
2361/// Object ::= LET LetList IN '{' ObjectList '}'
2362/// Object ::= LET LetList IN Object
2363///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002364bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002365 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2366 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002367
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002368 // Add this entry to the let stack.
Matthias Braunc66e7552016-12-05 06:41:54 +00002369 SmallVector<LetRecord, 8> LetInfo;
2370 ParseLetList(LetInfo);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002371 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002372 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002373
2374 if (Lex.getCode() != tgtok::In)
2375 return TokError("expected 'in' at end of top-level 'let'");
2376 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002377
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002378 // If this is a scalar let, just handle it now
2379 if (Lex.getCode() != tgtok::l_brace) {
2380 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002381 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002382 return true;
2383 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002384 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002385 // Otherwise, this is a group let.
2386 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002387
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002388 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002389 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002390 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002391
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002392 if (Lex.getCode() != tgtok::r_brace) {
2393 TokError("expected '}' at end of top level let command");
2394 return Error(BraceLoc, "to match this '{'");
2395 }
2396 Lex.Lex();
2397 }
Bob Wilson7248f862009-11-22 04:24:42 +00002398
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002399 // Outside this let scope, this let block is not active.
2400 LetStack.pop_back();
2401 return false;
2402}
2403
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002404/// ParseMultiClass - Parse a multiclass definition.
2405///
Bob Wilson3d948162009-04-28 19:41:44 +00002406/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002407/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2408/// MultiClassObject ::= DefInst
2409/// MultiClassObject ::= MultiClassInst
2410/// MultiClassObject ::= DefMInst
2411/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2412/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002413///
2414bool TGParser::ParseMultiClass() {
2415 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2416 Lex.Lex(); // Eat the multiclass token.
2417
2418 if (Lex.getCode() != tgtok::Id)
2419 return TokError("expected identifier after multiclass for name");
2420 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002421
Craig Topper7adf2bf2014-12-11 05:25:30 +00002422 auto Result =
2423 MultiClasses.insert(std::make_pair(Name,
2424 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2425
2426 if (!Result.second)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002427 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002428
Craig Topper7adf2bf2014-12-11 05:25:30 +00002429 CurMultiClass = Result.first->second.get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002430 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002431
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002432 // If there are template args, parse them.
2433 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002434 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002435 return true;
2436
David Greene7049e792009-04-24 16:55:41 +00002437 bool inherits = false;
2438
David Greene753ed8f2009-04-22 16:42:54 +00002439 // If there are submulticlasses, parse them.
2440 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002441 inherits = true;
2442
David Greene753ed8f2009-04-22 16:42:54 +00002443 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002444
David Greene753ed8f2009-04-22 16:42:54 +00002445 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002446 SubMultiClassReference SubMultiClass =
2447 ParseSubMultiClassReference(CurMultiClass);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002448 while (true) {
David Greene753ed8f2009-04-22 16:42:54 +00002449 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002450 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002451
David Greene753ed8f2009-04-22 16:42:54 +00002452 // Add it.
2453 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2454 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002455
David Greene753ed8f2009-04-22 16:42:54 +00002456 if (Lex.getCode() != tgtok::comma) break;
2457 Lex.Lex(); // eat ','.
2458 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2459 }
2460 }
2461
David Greene7049e792009-04-24 16:55:41 +00002462 if (Lex.getCode() != tgtok::l_brace) {
2463 if (!inherits)
2464 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002465 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002466 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002467 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002468 } else {
David Greene7049e792009-04-24 16:55:41 +00002469 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2470 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002471
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002472 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002473 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002474 default:
2475 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
2476 case tgtok::Let:
2477 case tgtok::Def:
2478 case tgtok::Defm:
2479 case tgtok::Foreach:
2480 if (ParseObject(CurMultiClass))
2481 return true;
2482 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002483 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002484 }
David Greene7049e792009-04-24 16:55:41 +00002485 Lex.Lex(); // eat the '}'.
2486 }
Bob Wilson7248f862009-11-22 04:24:42 +00002487
Craig Topper011817a2014-04-09 04:50:04 +00002488 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002489 return false;
2490}
2491
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002492Record *TGParser::InstantiateMulticlassDef(MultiClass &MC, Record *DefProto,
2493 Init *&DefmPrefix,
2494 SMRange DefmPrefixRange,
2495 ArrayRef<Init *> TArgs,
Matthias Braunc66e7552016-12-05 06:41:54 +00002496 ArrayRef<Init *> TemplateVals) {
David Greene5d5d88c2011-10-19 13:04:31 +00002497 // We need to preserve DefProto so it can be reused for later
2498 // instantiations, so create a new Record to inherit from it.
2499
David Greenedb445972011-10-05 22:42:07 +00002500 // Add in the defm name. If the defm prefix is empty, give each
2501 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2502 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2503 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002504
Jordan Roseabdd99b2013-01-10 18:50:05 +00002505 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002506 if (!DefmPrefix) {
Craig Topperf4412262017-06-01 06:56:16 +00002507 DefmPrefix = GetNewAnonymousName();
Jordan Roseabdd99b2013-01-10 18:50:05 +00002508 IsAnonymous = true;
2509 }
David Greene5d5d88c2011-10-19 13:04:31 +00002510
2511 Init *DefName = DefProto->getNameInit();
Sean Silvafb509ed2012-10-10 20:24:43 +00002512 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002513
Craig Topper011817a2014-04-09 04:50:04 +00002514 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002515 // We have a fully expanded string so there are no operators to
2516 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002517 DefName =
2518 BinOpInit::get(BinOpInit::STRCONCAT,
2519 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2520 StringRecTy::get())->Fold(DefProto, &MC),
2521 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2522 }
David Greene5d5d88c2011-10-19 13:04:31 +00002523
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002524 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002525 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002526 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Craig Topper84138712014-11-29 05:31:10 +00002527 auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002528
2529 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002530 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002531 Ref.Rec = DefProto;
Craig Topper84138712014-11-29 05:31:10 +00002532 AddSubClass(CurRec.get(), Ref);
David Greenedb445972011-10-05 22:42:07 +00002533
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002534 // Set the value for NAME. We don't resolve references to it 'til later,
2535 // though, so that uses in nested multiclass names don't get
2536 // confused.
Matthias Braun215ff842016-12-05 07:35:13 +00002537 if (SetValue(CurRec.get(), Ref.RefRange.Start, StringInit::get("NAME"), None,
2538 DefmPrefix, /*AllowSelfAssignment*/true)) {
Craig Topper85c07002015-04-30 05:54:22 +00002539 Error(DefmPrefixRange.Start, "Could not resolve " +
2540 CurRec->getNameInitAsString() + ":NAME to '" +
2541 DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002542 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002543 }
David Greene8bf0d722011-10-19 13:04:35 +00002544
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002545 // If the DefNameString didn't resolve, we probably have a reference to
2546 // NAME and need to replace it. We need to do at least this much greedily,
2547 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002548 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002549 RecordVal *DefNameRV = CurRec->getValue("NAME");
2550 CurRec->resolveReferencesTo(DefNameRV);
2551 }
2552
2553 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002554 // Now that we're at the top level, resolve all NAME references
2555 // in the resultant defs that weren't in the def names themselves.
2556 RecordVal *DefNameRV = CurRec->getValue("NAME");
2557 CurRec->resolveReferencesTo(DefNameRV);
2558
Hal Finkeld2497362015-05-21 04:32:56 +00002559 // Check if the name is a complex pattern.
2560 // If so, resolve it.
2561 DefName = CurRec->getNameInit();
2562 DefNameString = dyn_cast<StringInit>(DefName);
2563
2564 // OK the pattern is more complex than simply using NAME.
2565 // Let's use the heavy weaponery.
2566 if (!DefNameString) {
2567 ResolveMulticlassDefArgs(MC, CurRec.get(), DefmPrefixRange.Start,
2568 Lex.getLoc(), TArgs, TemplateVals,
2569 false/*Delete args*/);
2570 DefName = CurRec->getNameInit();
2571 DefNameString = dyn_cast<StringInit>(DefName);
2572
Nicolai Haehnledfda9dc2018-03-06 13:48:39 +00002573 if (!DefNameString) {
Hal Finkeld2497362015-05-21 04:32:56 +00002574 DefName = DefName->convertInitializerTo(StringRecTy::get());
Nicolai Haehnledfda9dc2018-03-06 13:48:39 +00002575 DefNameString = dyn_cast<StringInit>(DefName);
2576 }
Hal Finkeld2497362015-05-21 04:32:56 +00002577
Hal Finkeld2497362015-05-21 04:32:56 +00002578 if (!DefNameString) {
2579 PrintFatalError(CurRec->getLoc()[CurRec->getLoc().size() - 1],
2580 DefName->getAsUnquotedString() + " is not a string.");
2581 return nullptr;
2582 }
2583
2584 CurRec->setName(DefName);
2585 }
2586
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002587 // Now that NAME references are resolved and we're at the top level of
2588 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002589 // currently in a multiclass, it means this defm appears inside a
2590 // multiclass and its name won't be fully resolvable until we see
Hal Finkeld2497362015-05-21 04:32:56 +00002591 // the top-level defm. Therefore, we don't add this to the
2592 // RecordKeeper at this point. If we did we could get duplicate
David Greene8bf0d722011-10-19 13:04:35 +00002593 // defs as more than one probably refers to NAME or some other
2594 // common internal placeholder.
2595
2596 // Ensure redefinition doesn't happen.
2597 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002598 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002599 "' already defined, instantiating defm with subdef '" +
2600 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002601 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002602 }
2603
Craig Topper84138712014-11-29 05:31:10 +00002604 Record *CurRecSave = CurRec.get(); // Keep a copy before we release.
Craig Toppercdab2322014-11-29 05:52:51 +00002605 Records.addDef(std::move(CurRec));
Craig Topper84138712014-11-29 05:31:10 +00002606 return CurRecSave;
David Greene8bf0d722011-10-19 13:04:35 +00002607 }
2608
Craig Topper84138712014-11-29 05:31:10 +00002609 // FIXME This is bad but the ownership transfer to caller is pretty messy.
2610 // The unique_ptr in this function at least protects the exits above.
2611 return CurRec.release();
David Greenedb445972011-10-05 22:42:07 +00002612}
2613
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002614bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC, Record *CurRec,
2615 SMLoc DefmPrefixLoc, SMLoc SubClassLoc,
2616 ArrayRef<Init *> TArgs,
Matthias Braunc66e7552016-12-05 06:41:54 +00002617 ArrayRef<Init *> TemplateVals,
David Greenedb445972011-10-05 22:42:07 +00002618 bool DeleteArgs) {
Nicolai Haehnle7d697852018-03-05 15:21:19 +00002619 // Set all template arguments to the specified value or leave them as the
2620 // default if necessary, then resolve them all simultaneously.
2621 MapResolver R(CurRec);
2622
David Greenedb445972011-10-05 22:42:07 +00002623 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2624 // Check if a value is specified for this temp-arg.
2625 if (i < TemplateVals.size()) {
Craig Toppercfd81732016-01-04 03:15:08 +00002626 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], None, TemplateVals[i]))
David Greenedb445972011-10-05 22:42:07 +00002627 return true;
David Greenedb445972011-10-05 22:42:07 +00002628 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Craig Topper85c07002015-04-30 05:54:22 +00002629 return Error(SubClassLoc, "value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00002630 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +00002631 ") of multiclassclass '" + MC.Rec.getNameInitAsString() +
2632 "'");
David Greenedb445972011-10-05 22:42:07 +00002633 }
Nicolai Haehnle7d697852018-03-05 15:21:19 +00002634
2635 R.set(TArgs[i], CurRec->getValue(TArgs[i])->getValue());
2636
2637 if (DeleteArgs)
2638 CurRec->removeValue(TArgs[i]);
David Greenedb445972011-10-05 22:42:07 +00002639 }
Nicolai Haehnle7d697852018-03-05 15:21:19 +00002640
2641 CurRec->resolveReferences(R);
2642
David Greenedb445972011-10-05 22:42:07 +00002643 return false;
2644}
2645
2646bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2647 Record *CurRec,
2648 Record *DefProto,
2649 SMLoc DefmPrefixLoc) {
2650 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002651 if (ApplyLetStack(CurRec))
2652 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002653
David Greenedb445972011-10-05 22:42:07 +00002654 // Don't create a top level definition for defm inside multiclasses,
2655 // instead, only update the prototypes and bind the template args
2656 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002657 if (!CurMultiClass)
2658 return false;
Craig Toppereb4d7c62015-04-29 04:43:36 +00002659 for (const auto &Proto : CurMultiClass->DefPrototypes)
2660 if (Proto->getNameInit() == CurRec->getNameInit())
Sean Silvacc951b22013-01-09 05:28:12 +00002661 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2662 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002663 CurMultiClass->DefPrototypes.push_back(std::unique_ptr<Record>(CurRec));
David Greenedb445972011-10-05 22:42:07 +00002664
Sean Silvacc951b22013-01-09 05:28:12 +00002665 // Copy the template arguments for the multiclass into the new def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002666 for (Init * TA : CurMultiClass->Rec.getTemplateArgs()) {
2667 const RecordVal *RV = CurMultiClass->Rec.getValue(TA);
Sean Silvacc951b22013-01-09 05:28:12 +00002668 assert(RV && "Template arg doesn't exist?");
2669 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002670 }
2671
2672 return false;
2673}
2674
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002675/// ParseDefm - Parse the instantiation of a multiclass.
2676///
2677/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2678///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002679bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002680 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002681 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002682 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002683
Craig Topperb21afc62013-01-07 05:09:33 +00002684 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002685 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002686 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002687
Jordan Rosef12e8a92013-01-10 18:50:11 +00002688 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002689 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002690 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002691
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002692 // Keep track of the new generated record definitions.
2693 std::vector<Record*> NewRecDefs;
2694
2695 // This record also inherits from a regular class (non-multiclass)?
2696 bool InheritFromClass = false;
2697
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002698 // eat the colon.
2699 Lex.Lex();
2700
Chris Lattner526c8cb2009-06-21 03:39:35 +00002701 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002702 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002703
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002704 while (true) {
Craig Topper011817a2014-04-09 04:50:04 +00002705 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002706
2707 // To instantiate a multiclass, we need to first get the multiclass, then
2708 // instantiate each def contained in the multiclass with the SubClassRef
2709 // template parameters.
Craig Topper7adf2bf2014-12-11 05:25:30 +00002710 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper4ca40012014-11-30 01:20:17 +00002711 assert(MC && "Didn't lookup multiclass correctly?");
Matthias Braunc66e7552016-12-05 06:41:54 +00002712 ArrayRef<Init*> TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002713
2714 // Verify that the correct number of template arguments were specified.
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002715 ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002716 if (TArgs.size() < TemplateVals.size())
2717 return Error(SubClassLoc,
2718 "more template args specified than multiclass expects");
2719
2720 // Loop over all the def's in the multiclass, instantiating each one.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002721 for (const std::unique_ptr<Record> &DefProto : MC->DefPrototypes) {
Hal Finkeld2497362015-05-21 04:32:56 +00002722 // The record name construction goes as follow:
2723 // - If the def name is a string, prepend the prefix.
2724 // - If the def name is a more complex pattern, use that pattern.
Craig Topper7f36be92016-02-26 06:50:27 +00002725 // As a result, the record is instantiated before resolving
Hal Finkeld2497362015-05-21 04:32:56 +00002726 // arguments, as it would make its name a string.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002727 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto.get(), DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002728 SMRange(DefmLoc,
Hal Finkeld2497362015-05-21 04:32:56 +00002729 DefmPrefixEndLoc),
2730 TArgs, TemplateVals);
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002731 if (!CurRec)
2732 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002733
Craig Topper7f36be92016-02-26 06:50:27 +00002734 // Now that the record is instantiated, we can resolve arguments.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002735 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002736 TArgs, TemplateVals, true/*Delete args*/))
2737 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002738
Craig Toppereb4d7c62015-04-29 04:43:36 +00002739 if (ResolveMulticlassDef(*MC, CurRec, DefProto.get(), DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002740 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002741
Adam Nemete5a07162014-09-16 17:14:13 +00002742 // Defs that can be used by other definitions should be fully resolved
2743 // before any use.
2744 if (DefProto->isResolveFirst() && !CurMultiClass) {
2745 CurRec->resolveReferences();
2746 CurRec->setResolveFirst(false);
2747 }
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002748 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002749 }
2750
David Greenedb445972011-10-05 22:42:07 +00002751
David Greenef00919a2009-04-22 22:17:51 +00002752 if (Lex.getCode() != tgtok::comma) break;
2753 Lex.Lex(); // eat ','.
2754
Craig Topper998a39a2013-08-20 04:22:09 +00002755 if (Lex.getCode() != tgtok::Id)
2756 return TokError("expected identifier");
2757
David Greenef00919a2009-04-22 22:17:51 +00002758 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002759
2760 // A defm can inherit from regular classes (non-multiclass) as
2761 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002762 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002763
2764 if (InheritFromClass)
2765 break;
2766
Craig Topper011817a2014-04-09 04:50:04 +00002767 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002768 }
2769
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002770 if (InheritFromClass) {
2771 // Process all the classes to inherit as if they were part of a
2772 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002773 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002774 while (true) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002775 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002776 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002777
2778 // Get the expanded definition prototypes and teach them about
2779 // the record values the current class to inherit has
Craig Toppereb4d7c62015-04-29 04:43:36 +00002780 for (Record *CurRec : NewRecDefs) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002781 // Add it.
2782 if (AddSubClass(CurRec, SubClass))
2783 return true;
2784
Sean Silvacb1a75e2013-01-09 04:49:14 +00002785 if (ApplyLetStack(CurRec))
2786 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002787 }
2788
2789 if (Lex.getCode() != tgtok::comma) break;
2790 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002791 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002792 }
2793 }
2794
Nicolai Haehnle0f529882018-03-06 13:48:47 +00002795 if (!CurMultiClass) {
2796 for (Record *CurRec : NewRecDefs) {
David Greene50c09122011-08-10 18:27:46 +00002797 // See Record::setName(). This resolve step will see any new
2798 // name for the def that might have been created when resolving
2799 // inheritance, values and arguments above.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002800 CurRec->resolveReferences();
Nicolai Haehnle0f529882018-03-06 13:48:47 +00002801 checkConcrete(*CurRec);
2802 }
2803 }
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002804
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002805 if (Lex.getCode() != tgtok::semi)
2806 return TokError("expected ';' at end of defm");
2807 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002808
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002809 return false;
2810}
2811
2812/// ParseObject
2813/// Object ::= ClassInst
2814/// Object ::= DefInst
2815/// Object ::= MultiClassInst
2816/// Object ::= DefMInst
2817/// Object ::= LETCommand '{' ObjectList '}'
2818/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002819bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002820 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002821 default:
2822 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002823 case tgtok::Let: return ParseTopLevelLet(MC);
2824 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002825 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002826 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002827 case tgtok::Class: return ParseClass();
2828 case tgtok::MultiClass: return ParseMultiClass();
2829 }
2830}
2831
2832/// ParseObjectList
2833/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002834bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002835 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002836 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002837 return true;
2838 }
2839 return false;
2840}
2841
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002842bool TGParser::ParseFile() {
2843 Lex.Lex(); // Prime the lexer.
2844 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002845
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002846 // If we have unread input at the end of the file, report it.
2847 if (Lex.getCode() == tgtok::Eof)
2848 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002849
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002850 return TokError("Unexpected input at top level");
2851}