blob: 4a4a510eefe6ff362505785152d1b721d967501a [file] [log] [blame]
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001//===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Parser for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
14#include "TGParser.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000015#include "llvm/ADT/None.h"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000016#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/SmallVector.h"
Chris Lattnerf4127dd2007-11-22 20:49:04 +000018#include "llvm/ADT/StringExtras.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000019#include "llvm/Support/Casting.h"
20#include "llvm/Support/Compiler.h"
21#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/TableGen/Record.h"
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000024#include <algorithm>
Eugene Zelenko33d7b762016-08-23 17:14:32 +000025#include <cassert>
26#include <cstdint>
27
Chris Lattnerf4127dd2007-11-22 20:49:04 +000028using namespace llvm;
29
30//===----------------------------------------------------------------------===//
31// Support Code for the Semantic Actions.
32//===----------------------------------------------------------------------===//
33
34namespace llvm {
Eugene Zelenko33d7b762016-08-23 17:14:32 +000035
Chris Lattnerf4127dd2007-11-22 20:49:04 +000036struct SubClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000037 SMRange RefRange;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000038 Record *Rec;
Matthias Braunc66e7552016-12-05 06:41:54 +000039 SmallVector<Init*, 4> TemplateArgs;
Eugene Zelenko33d7b762016-08-23 17:14:32 +000040
Craig Topper011817a2014-04-09 04:50:04 +000041 SubClassReference() : Rec(nullptr) {}
David Greene7049e792009-04-24 16:55:41 +000042
Craig Topper011817a2014-04-09 04:50:04 +000043 bool isInvalid() const { return Rec == nullptr; }
Chris Lattnerf4127dd2007-11-22 20:49:04 +000044};
David Greene753ed8f2009-04-22 16:42:54 +000045
46struct SubMultiClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000047 SMRange RefRange;
David Greene753ed8f2009-04-22 16:42:54 +000048 MultiClass *MC;
Matthias Braunc66e7552016-12-05 06:41:54 +000049 SmallVector<Init*, 4> TemplateArgs;
Eugene Zelenko33d7b762016-08-23 17:14:32 +000050
Craig Topper011817a2014-04-09 04:50:04 +000051 SubMultiClassReference() : MC(nullptr) {}
Bob Wilson3d948162009-04-28 19:41:44 +000052
Craig Topper011817a2014-04-09 04:50:04 +000053 bool isInvalid() const { return MC == nullptr; }
David Greene7049e792009-04-24 16:55:41 +000054 void dump() const;
David Greene753ed8f2009-04-22 16:42:54 +000055};
David Greene7049e792009-04-24 16:55:41 +000056
Aaron Ballman615eb472017-10-15 14:32:27 +000057#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +000058LLVM_DUMP_METHOD void SubMultiClassReference::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000059 errs() << "Multiclass:\n";
Bob Wilson7248f862009-11-22 04:24:42 +000060
David Greene7049e792009-04-24 16:55:41 +000061 MC->dump();
Bob Wilson7248f862009-11-22 04:24:42 +000062
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000063 errs() << "Template args:\n";
Craig Toppera9642b42015-05-04 01:35:39 +000064 for (Init *TA : TemplateArgs)
Craig Toppereb4d7c62015-04-29 04:43:36 +000065 TA->dump();
David Greene7049e792009-04-24 16:55:41 +000066}
Matthias Braun25bcaba2017-01-28 02:47:46 +000067#endif
David Greene7049e792009-04-24 16:55:41 +000068
Chris Lattnerf4127dd2007-11-22 20:49:04 +000069} // end namespace llvm
70
Nicolai Haehnle0f529882018-03-06 13:48:47 +000071static bool checkBitsConcrete(Record &R, const RecordVal &RV) {
72 BitsInit *BV = cast<BitsInit>(RV.getValue());
73 for (unsigned i = 0, e = BV->getNumBits(); i != e; ++i) {
74 Init *Bit = BV->getBit(i);
75 bool IsReference = false;
76 if (auto VBI = dyn_cast<VarBitInit>(Bit)) {
77 if (auto VI = dyn_cast<VarInit>(VBI->getBitVar())) {
78 if (R.getValue(VI->getName()))
79 IsReference = true;
80 }
81 } else if (isa<VarInit>(Bit)) {
82 IsReference = true;
83 }
84 if (!(IsReference || Bit->isConcrete()))
85 return false;
86 }
87 return true;
88}
89
90static void checkConcrete(Record &R) {
91 for (const RecordVal &RV : R.getValues()) {
92 // HACK: Disable this check for variables declared with 'field'. This is
93 // done merely because existing targets have legitimate cases of
94 // non-concrete variables in helper defs. Ideally, we'd introduce a
95 // 'maybe' or 'optional' modifier instead of this.
96 if (RV.getPrefix())
97 continue;
98
99 if (Init *V = RV.getValue()) {
100 bool Ok = isa<BitsInit>(V) ? checkBitsConcrete(R, RV) : V->isConcrete();
101 if (!Ok) {
102 PrintError(R.getLoc(),
103 Twine("Initializer of '") + RV.getNameInitAsString() +
104 "' in '" + R.getNameInitAsString() +
105 "' could not be fully resolved: " +
106 RV.getValue()->getAsString());
107 }
108 }
109 }
110}
111
Chris Lattner526c8cb2009-06-21 03:39:35 +0000112bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Craig Topper011817a2014-04-09 04:50:04 +0000113 if (!CurRec)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000114 CurRec = &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +0000115
Jakob Stoklund Olesen9d1c5ee2012-01-13 03:16:35 +0000116 if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000117 // The value already exists in the class, treat this as a set.
118 if (ERV->setValue(RV.getValue()))
119 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
120 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson7248f862009-11-22 04:24:42 +0000121 "previous definition of type '" +
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000122 ERV->getType()->getAsString() + "'");
123 } else {
124 CurRec->addValue(RV);
125 }
126 return false;
127}
128
129/// SetValue -
130/// Return true on error, false on success.
David Greene3ca42122011-10-19 13:02:39 +0000131bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
Craig Toppercfd81732016-01-04 03:15:08 +0000132 ArrayRef<unsigned> BitList, Init *V,
Craig Topper1e23ed92016-01-04 03:05:14 +0000133 bool AllowSelfAssignment) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000134 if (!V) return false;
135
Craig Topper011817a2014-04-09 04:50:04 +0000136 if (!CurRec) CurRec = &CurMultiClass->Rec;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000137
138 RecordVal *RV = CurRec->getValue(ValName);
Craig Topper011817a2014-04-09 04:50:04 +0000139 if (!RV)
Craig Topper85c07002015-04-30 05:54:22 +0000140 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
141 "' unknown!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000142
143 // Do not allow assignments like 'X = X'. This will just cause infinite loops
144 // in the resolution machinery.
145 if (BitList.empty())
Sean Silvafb509ed2012-10-10 20:24:43 +0000146 if (VarInit *VI = dyn_cast<VarInit>(V))
Craig Topper1e23ed92016-01-04 03:05:14 +0000147 if (VI->getNameInit() == ValName && !AllowSelfAssignment)
Nicolai Haehnlef19083d2018-02-22 15:26:21 +0000148 return Error(Loc, "Recursion / self-assignment forbidden");
Bob Wilson7248f862009-11-22 04:24:42 +0000149
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000150 // If we are assigning to a subset of the bits in the value... then we must be
151 // assigning to a field of BitsRecTy, which must have a BitsInit
152 // initializer.
153 //
154 if (!BitList.empty()) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000155 BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue());
Craig Topper011817a2014-04-09 04:50:04 +0000156 if (!CurVal)
Craig Topper85c07002015-04-30 05:54:22 +0000157 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
158 "' is not a bits type");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000159
160 // Convert the incoming value to a bits type of the appropriate size...
Nicolai Haehnledfda9dc2018-03-06 13:48:39 +0000161 Init *BI = V->getCastTo(BitsRecTy::get(BitList.size()));
Craig Toppera9642b42015-05-04 01:35:39 +0000162 if (!BI)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000163 return Error(Loc, "Initializer is not compatible with bit range");
Bob Wilson7248f862009-11-22 04:24:42 +0000164
David Greeneaf8ee2c2011-07-29 22:43:06 +0000165 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000166
167 // Loop over bits, assigning values as appropriate.
168 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
169 unsigned Bit = BitList[i];
David Greeneb3da8122011-07-29 19:07:00 +0000170 if (NewBits[Bit])
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000171 return Error(Loc, "Cannot set bit #" + Twine(Bit) + " of value '" +
David Greene3ca42122011-10-19 13:02:39 +0000172 ValName->getAsUnquotedString() + "' more than once");
Nicolai Haehnledfda9dc2018-03-06 13:48:39 +0000173 NewBits[Bit] = BI->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000174 }
175
176 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
Craig Topper011817a2014-04-09 04:50:04 +0000177 if (!NewBits[i])
David Greeneb3da8122011-07-29 19:07:00 +0000178 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000179
David Greenee32ebf22011-07-29 19:07:07 +0000180 V = BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000181 }
182
Pete Cooper040c6a62014-07-31 01:43:57 +0000183 if (RV->setValue(V)) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000184 std::string InitType;
Craig Toppera9642b42015-05-04 01:35:39 +0000185 if (BitsInit *BI = dyn_cast<BitsInit>(V))
Pete Cooper040c6a62014-07-31 01:43:57 +0000186 InitType = (Twine("' of type bit initializer with length ") +
187 Twine(BI->getNumBits())).str();
Nicolai Haehnlef19083d2018-02-22 15:26:21 +0000188 else if (TypedInit *TI = dyn_cast<TypedInit>(V))
189 InitType = (Twine("' of type '") + TI->getType()->getAsString()).str();
Craig Topper85c07002015-04-30 05:54:22 +0000190 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
Nicolai Haehnlef19083d2018-02-22 15:26:21 +0000191 "' of type '" + RV->getType()->getAsString() +
192 "' is incompatible with initializer '" +
193 V->getAsString() + InitType + "'");
Pete Cooper040c6a62014-07-31 01:43:57 +0000194 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000195 return false;
196}
197
198/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
199/// args as SubClass's template arguments.
Cedric Venetd1e179d2009-02-14 16:06:42 +0000200bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000201 Record *SC = SubClass.Rec;
202 // Add all of the values in the subclass into the current class.
Craig Topper8eb764c2015-06-02 06:19:28 +0000203 for (const RecordVal &Val : SC->getValues())
204 if (AddValue(CurRec, SubClass.RefRange.Start, Val))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000205 return true;
206
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000207 ArrayRef<Init *> TArgs = SC->getTemplateArgs();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000208
209 // Ensure that an appropriate number of template arguments are specified.
210 if (TArgs.size() < SubClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000211 return Error(SubClass.RefRange.Start,
212 "More template args specified than expected");
Bob Wilson7248f862009-11-22 04:24:42 +0000213
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000214 // Loop over all of the template arguments, setting them to the specified
215 // value or leaving them as the default if necessary.
Nicolai Haehnleb0cf9e92018-03-05 15:21:11 +0000216 MapResolver R(CurRec);
217
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000218 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
219 if (i < SubClass.TemplateArgs.size()) {
220 // If a value is specified for this template arg, set it now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000221 if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000222 None, SubClass.TemplateArgs[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000223 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000224 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000225 return Error(SubClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000226 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000227 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000228 ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000229 }
Nicolai Haehnleb0cf9e92018-03-05 15:21:11 +0000230
231 R.set(TArgs[i], CurRec->getValue(TArgs[i])->getValue());
232
233 CurRec->removeValue(TArgs[i]);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000234 }
235
Nicolai Haehnleb0cf9e92018-03-05 15:21:11 +0000236 CurRec->resolveReferences(R);
237
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000238 // Since everything went well, we can now set the "superclass" list for the
239 // current record.
Craig Topper0e41d0b2016-01-18 19:52:37 +0000240 ArrayRef<std::pair<Record *, SMRange>> SCs = SC->getSuperClasses();
241 for (const auto &SCPair : SCs) {
242 if (CurRec->isSubClassOf(SCPair.first))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000243 return Error(SubClass.RefRange.Start,
Craig Topper0e41d0b2016-01-18 19:52:37 +0000244 "Already subclass of '" + SCPair.first->getName() + "'!\n");
245 CurRec->addSuperClass(SCPair.first, SCPair.second);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000246 }
Nicolai Haehnlec10570f2018-02-23 11:31:49 +0000247
248 if (CurRec->isSubClassOf(SC))
249 return Error(SubClass.RefRange.Start,
250 "Already subclass of '" + SC->getName() + "'!\n");
251 CurRec->addSuperClass(SC, SubClass.RefRange);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000252 return false;
253}
254
David Greene753ed8f2009-04-22 16:42:54 +0000255/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilsonf71e6562009-04-30 18:26:19 +0000256/// CurMC, resolving its template args as SubMultiClass's
David Greene753ed8f2009-04-22 16:42:54 +0000257/// template arguments.
Bob Wilsonf71e6562009-04-30 18:26:19 +0000258bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson92ab8202009-04-30 17:46:20 +0000259 SubMultiClassReference &SubMultiClass) {
David Greene753ed8f2009-04-22 16:42:54 +0000260 MultiClass *SMC = SubMultiClass.MC;
Bob Wilsonf71e6562009-04-30 18:26:19 +0000261 Record *CurRec = &CurMC->Rec;
David Greene753ed8f2009-04-22 16:42:54 +0000262
David Greene753ed8f2009-04-22 16:42:54 +0000263 // Add all of the values in the subclass into the current class.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000264 for (const auto &SMCVal : SMC->Rec.getValues())
265 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000266 return true;
267
Craig Toppera4ea4b02014-11-30 00:24:32 +0000268 unsigned newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000269
David Greene753ed8f2009-04-22 16:42:54 +0000270 // Add all of the defs in the subclass into the current multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000271 for (const std::unique_ptr<Record> &R : SMC->DefPrototypes) {
David Greene753ed8f2009-04-22 16:42:54 +0000272 // Clone the def and add it to the current multiclass
Craig Toppereb4d7c62015-04-29 04:43:36 +0000273 auto NewDef = make_unique<Record>(*R);
David Greene753ed8f2009-04-22 16:42:54 +0000274
275 // Add all of the values in the superclass into the current def.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000276 for (const auto &MCVal : CurRec->getValues())
277 if (AddValue(NewDef.get(), SubMultiClass.RefRange.Start, MCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000278 return true;
279
Craig Topperc3504c42014-12-11 05:25:33 +0000280 CurMC->DefPrototypes.push_back(std::move(NewDef));
David Greene753ed8f2009-04-22 16:42:54 +0000281 }
Bob Wilson3d948162009-04-28 19:41:44 +0000282
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000283 ArrayRef<Init *> SMCTArgs = SMC->Rec.getTemplateArgs();
David Greene753ed8f2009-04-22 16:42:54 +0000284
David Greene7049e792009-04-24 16:55:41 +0000285 // Ensure that an appropriate number of template arguments are
286 // specified.
David Greene753ed8f2009-04-22 16:42:54 +0000287 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000288 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000289 "More template args specified than expected");
Bob Wilson3d948162009-04-28 19:41:44 +0000290
David Greene753ed8f2009-04-22 16:42:54 +0000291 // Loop over all of the template arguments, setting them to the specified
292 // value or leaving them as the default if necessary.
Nicolai Haehnle1faf8682018-03-05 15:21:15 +0000293 MapResolver CurRecResolver(CurRec);
294
David Greene753ed8f2009-04-22 16:42:54 +0000295 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
296 if (i < SubMultiClass.TemplateArgs.size()) {
David Greene7049e792009-04-24 16:55:41 +0000297 // If a value is specified for this template arg, set it in the
298 // superclass now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000299 if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000300 None, SubMultiClass.TemplateArgs[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000301 return true;
302
David Greene7049e792009-04-24 16:55:41 +0000303 // If a value is specified for this template arg, set it in the
304 // new defs now.
Craig Topperc3504c42014-12-11 05:25:33 +0000305 for (const auto &Def :
306 makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
307 if (SetValue(Def.get(), SubMultiClass.RefRange.Start, SMCTArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000308 None, SubMultiClass.TemplateArgs[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000309 return true;
David Greene753ed8f2009-04-22 16:42:54 +0000310 }
311 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000312 return Error(SubMultiClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000313 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000314 Twine(i) + " (" + SMCTArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000315 ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000316 }
Nicolai Haehnle1faf8682018-03-05 15:21:15 +0000317
318 CurRecResolver.set(SMCTArgs[i], CurRec->getValue(SMCTArgs[i])->getValue());
319
320 CurRec->removeValue(SMCTArgs[i]);
321 }
322
323 CurRec->resolveReferences(CurRecResolver);
324
325 for (const auto &Def :
326 makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
327 MapResolver R(Def.get());
328
329 for (Init *SMCTArg : SMCTArgs) {
330 R.set(SMCTArg, Def->getValue(SMCTArg)->getValue());
331 Def->removeValue(SMCTArg);
332 }
333
334 Def->resolveReferences(R);
David Greene753ed8f2009-04-22 16:42:54 +0000335 }
336
337 return false;
338}
339
David Greenefb927af2012-02-22 16:09:41 +0000340/// ProcessForeachDefs - Given a record, apply all of the variable
341/// values in all surrounding foreach loops, creating new records for
342/// each combination of values.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000343bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
344 if (Loops.empty())
345 return false;
346
David Greenefb927af2012-02-22 16:09:41 +0000347 // We want to instantiate a new copy of CurRec for each combination
348 // of nested loop iterator values. We don't want top instantiate
349 // any copies until we have values for each loop iterator.
350 IterSet IterVals;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000351 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000352}
353
354/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
355/// apply each of the variable values in this loop and then process
356/// subloops.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000357bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
358 // Recursively build a tuple of iterator values.
359 if (IterVals.size() != Loops.size()) {
360 assert(IterVals.size() < Loops.size());
361 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silvafb509ed2012-10-10 20:24:43 +0000362 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Craig Topper011817a2014-04-09 04:50:04 +0000363 if (!List) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000364 Error(Loc, "Loop list is not a list");
365 return true;
366 }
David Greenefb927af2012-02-22 16:09:41 +0000367
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000368 // Process each value.
Craig Topper664f6a02015-06-02 04:15:57 +0000369 for (unsigned i = 0; i < List->size(); ++i) {
Nicolai Haehnle0b0eaf72018-03-05 15:20:51 +0000370 RecordResolver R(*CurRec);
371 Init *ItemVal = List->getElement(i)->resolveReferences(R);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000372 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
373 if (ProcessForeachDefs(CurRec, Loc, IterVals))
374 return true;
375 IterVals.pop_back();
376 }
377 return false;
378 }
379
380 // This is the bottom of the recursion. We have all of the iterator values
381 // for this point in the iteration space. Instantiate a new record to
382 // reflect this combination of values.
Craig Topper84138712014-11-29 05:31:10 +0000383 auto IterRec = make_unique<Record>(*CurRec);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000384
385 // Set the iterator values now.
Craig Topper8eb764c2015-06-02 06:19:28 +0000386 for (IterRecord &IR : IterVals) {
387 VarInit *IterVar = IR.IterVar;
388 TypedInit *IVal = dyn_cast<TypedInit>(IR.IterValue);
Craig Topper84138712014-11-29 05:31:10 +0000389 if (!IVal)
390 return Error(Loc, "foreach iterator value is untyped");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000391
Craig Topperf4412262017-06-01 06:56:16 +0000392 IterRec->addValue(RecordVal(IterVar->getNameInit(), IVal->getType(), false));
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000393
Matthias Braun215ff842016-12-05 07:35:13 +0000394 if (SetValue(IterRec.get(), Loc, IterVar->getNameInit(), None, IVal))
Craig Topper84138712014-11-29 05:31:10 +0000395 return Error(Loc, "when instantiating this def");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000396
397 // Resolve it next.
Matthias Braun215ff842016-12-05 07:35:13 +0000398 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getNameInit()));
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000399
400 // Remove it.
Matthias Braun215ff842016-12-05 07:35:13 +0000401 IterRec->removeValue(IterVar->getNameInit());
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000402 }
403
404 if (Records.getDef(IterRec->getNameInitAsString())) {
Artyom Skrobov8b985322014-06-10 12:41:14 +0000405 // If this record is anonymous, it's no problem, just generate a new name
Craig Topper84138712014-11-29 05:31:10 +0000406 if (!IterRec->isAnonymous())
407 return Error(Loc, "def already exists: " +IterRec->getNameInitAsString());
408
Nicolai Haehnle73355bc2018-03-06 13:48:54 +0000409 IterRec->setName(Records.getNewAnonymousName());
David Greenefb927af2012-02-22 16:09:41 +0000410 }
411
Craig Topper84138712014-11-29 05:31:10 +0000412 Record *IterRecSave = IterRec.get(); // Keep a copy before release.
Craig Toppercdab2322014-11-29 05:52:51 +0000413 Records.addDef(std::move(IterRec));
Craig Topper84138712014-11-29 05:31:10 +0000414 IterRecSave->resolveReferences();
Nicolai Haehnle0f529882018-03-06 13:48:47 +0000415 checkConcrete(*IterRecSave);
Nicolai Haehnlefcd65252018-03-09 12:24:42 +0000416 if (addToDefsets(*IterRecSave))
417 return true;
David Greenefb927af2012-02-22 16:09:41 +0000418 return false;
419}
420
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000421//===----------------------------------------------------------------------===//
422// Parser Code
423//===----------------------------------------------------------------------===//
424
425/// isObjectStart - Return true if this is a valid first token for an Object.
426static bool isObjectStart(tgtok::TokKind K) {
Nicolai Haehnlefcd65252018-03-09 12:24:42 +0000427 return K == tgtok::Class || K == tgtok::Def || K == tgtok::Defm ||
428 K == tgtok::Let || K == tgtok::MultiClass || K == tgtok::Foreach ||
429 K == tgtok::Defset;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000430}
431
432/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000433/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000434/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000435/// ObjectName ::= /*empty*/
436///
David Greene2affd672011-10-19 13:04:29 +0000437Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
438 switch (Lex.getCode()) {
439 case tgtok::colon:
440 case tgtok::semi:
441 case tgtok::l_brace:
442 // These are all of the tokens that can begin an object body.
443 // Some of these can also begin values but we disallow those cases
444 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000445 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000446 default:
447 break;
448 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000449
Craig Topper011817a2014-04-09 04:50:04 +0000450 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000451 if (CurMultiClass)
452 CurRec = &CurMultiClass->Rec;
453
Craig Topper011817a2014-04-09 04:50:04 +0000454 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000455 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000456 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000457 if (!CurRecName) {
458 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000459 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000460 }
461 Type = CurRecName->getType();
462 }
463
464 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000465}
466
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000467/// ParseClassID - Parse and resolve a reference to a class name. This returns
468/// null on error.
469///
470/// ClassID ::= ID
471///
472Record *TGParser::ParseClassID() {
473 if (Lex.getCode() != tgtok::Id) {
474 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000475 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000476 }
Bob Wilson7248f862009-11-22 04:24:42 +0000477
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000478 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000479 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000480 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000481
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000482 Lex.Lex();
483 return Result;
484}
485
Bob Wilson3d948162009-04-28 19:41:44 +0000486/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
487/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000488///
489/// MultiClassID ::= ID
490///
491MultiClass *TGParser::ParseMultiClassID() {
492 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000493 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000494 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000495 }
Bob Wilson3d948162009-04-28 19:41:44 +0000496
Craig Topper7adf2bf2014-12-11 05:25:30 +0000497 MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
Craig Topper4ca40012014-11-30 01:20:17 +0000498 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000499 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000500
David Greene753ed8f2009-04-22 16:42:54 +0000501 Lex.Lex();
Craig Topper4ca40012014-11-30 01:20:17 +0000502 return Result;
David Greene753ed8f2009-04-22 16:42:54 +0000503}
504
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000505/// ParseSubClassReference - Parse a reference to a subclass or to a templated
506/// subclass. This returns a SubClassRefTy with a null Record* on error.
507///
508/// SubClassRef ::= ClassID
509/// SubClassRef ::= ClassID '<' ValueList '>'
510///
511SubClassReference TGParser::
512ParseSubClassReference(Record *CurRec, bool isDefm) {
513 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000514 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000515
Sean Silva0657b402013-01-09 02:17:14 +0000516 if (isDefm) {
517 if (MultiClass *MC = ParseMultiClassID())
518 Result.Rec = &MC->Rec;
519 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000520 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000521 }
Craig Topper011817a2014-04-09 04:50:04 +0000522 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000523
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000524 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000525 if (Lex.getCode() != tgtok::less) {
526 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000527 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000528 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000529 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000530
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000531 if (Lex.getCode() == tgtok::greater) {
532 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000533 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000534 return Result;
535 }
Bob Wilson7248f862009-11-22 04:24:42 +0000536
Matthias Braunc66e7552016-12-05 06:41:54 +0000537 ParseValueList(Result.TemplateArgs, CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000538 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000539 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000540 return Result;
541 }
Bob Wilson7248f862009-11-22 04:24:42 +0000542
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000543 if (Lex.getCode() != tgtok::greater) {
544 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000545 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000546 return Result;
547 }
548 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000549 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000550
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000551 return Result;
552}
553
Bob Wilson3d948162009-04-28 19:41:44 +0000554/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
555/// templated submulticlass. This returns a SubMultiClassRefTy with a null
556/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000557///
558/// SubMultiClassRef ::= MultiClassID
559/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
560///
561SubMultiClassReference TGParser::
562ParseSubMultiClassReference(MultiClass *CurMC) {
563 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000564 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000565
David Greene753ed8f2009-04-22 16:42:54 +0000566 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000567 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000568
David Greene753ed8f2009-04-22 16:42:54 +0000569 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000570 if (Lex.getCode() != tgtok::less) {
571 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000572 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000573 }
David Greene753ed8f2009-04-22 16:42:54 +0000574 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000575
David Greene753ed8f2009-04-22 16:42:54 +0000576 if (Lex.getCode() == tgtok::greater) {
577 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000578 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000579 return Result;
580 }
Bob Wilson3d948162009-04-28 19:41:44 +0000581
Matthias Braunc66e7552016-12-05 06:41:54 +0000582 ParseValueList(Result.TemplateArgs, &CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000583 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000584 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000585 return Result;
586 }
Bob Wilson3d948162009-04-28 19:41:44 +0000587
David Greene753ed8f2009-04-22 16:42:54 +0000588 if (Lex.getCode() != tgtok::greater) {
589 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000590 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000591 return Result;
592 }
593 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000594 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000595
596 return Result;
597}
598
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000599/// ParseRangePiece - Parse a bit/value range.
600/// RangePiece ::= INTVAL
601/// RangePiece ::= INTVAL '-' INTVAL
602/// RangePiece ::= INTVAL INTVAL
Matthias Braunc66e7552016-12-05 06:41:54 +0000603bool TGParser::ParseRangePiece(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000604 if (Lex.getCode() != tgtok::IntVal) {
605 TokError("expected integer or bitrange");
606 return true;
607 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000608 int64_t Start = Lex.getCurIntVal();
609 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000610
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000611 if (Start < 0)
612 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000613
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000614 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000615 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000616 Ranges.push_back(Start);
617 return false;
618 case tgtok::minus:
619 if (Lex.Lex() != tgtok::IntVal) {
620 TokError("expected integer value as end of range");
621 return true;
622 }
623 End = Lex.getCurIntVal();
624 break;
625 case tgtok::IntVal:
626 End = -Lex.getCurIntVal();
627 break;
628 }
Bob Wilson7248f862009-11-22 04:24:42 +0000629 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000630 return TokError("invalid range, cannot be negative");
631 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000632
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000633 // Add to the range.
Craig Toppera9642b42015-05-04 01:35:39 +0000634 if (Start < End)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000635 for (; Start <= End; ++Start)
636 Ranges.push_back(Start);
Craig Toppera9642b42015-05-04 01:35:39 +0000637 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000638 for (; Start >= End; --Start)
639 Ranges.push_back(Start);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000640 return false;
641}
642
643/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
644///
645/// RangeList ::= RangePiece (',' RangePiece)*
646///
Matthias Braunc66e7552016-12-05 06:41:54 +0000647void TGParser::ParseRangeList(SmallVectorImpl<unsigned> &Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000648 // Parse the first piece.
Matthias Braunc66e7552016-12-05 06:41:54 +0000649 if (ParseRangePiece(Result)) {
650 Result.clear();
651 return;
652 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000653 while (Lex.getCode() == tgtok::comma) {
654 Lex.Lex(); // Eat the comma.
655
656 // Parse the next range piece.
Matthias Braunc66e7552016-12-05 06:41:54 +0000657 if (ParseRangePiece(Result)) {
658 Result.clear();
659 return;
660 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000661 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000662}
663
664/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
665/// OptionalRangeList ::= '<' RangeList '>'
666/// OptionalRangeList ::= /*empty*/
Matthias Braunc66e7552016-12-05 06:41:54 +0000667bool TGParser::ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000668 if (Lex.getCode() != tgtok::less)
669 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000670
Chris Lattner526c8cb2009-06-21 03:39:35 +0000671 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000672 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000673
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000674 // Parse the range list.
Matthias Braunc66e7552016-12-05 06:41:54 +0000675 ParseRangeList(Ranges);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000676 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000677
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000678 if (Lex.getCode() != tgtok::greater) {
679 TokError("expected '>' at end of range list");
680 return Error(StartLoc, "to match this '<'");
681 }
682 Lex.Lex(); // eat the '>'.
683 return false;
684}
685
686/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
687/// OptionalBitList ::= '{' RangeList '}'
688/// OptionalBitList ::= /*empty*/
Matthias Braunc66e7552016-12-05 06:41:54 +0000689bool TGParser::ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000690 if (Lex.getCode() != tgtok::l_brace)
691 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000692
Chris Lattner526c8cb2009-06-21 03:39:35 +0000693 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000694 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000695
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000696 // Parse the range list.
Matthias Braunc66e7552016-12-05 06:41:54 +0000697 ParseRangeList(Ranges);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000698 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000699
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000700 if (Lex.getCode() != tgtok::r_brace) {
701 TokError("expected '}' at end of bit list");
702 return Error(StartLoc, "to match this '{'");
703 }
704 Lex.Lex(); // eat the '}'.
705 return false;
706}
707
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000708/// ParseType - Parse and return a tblgen type. This returns null on error.
709///
710/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000711/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000712/// Type ::= BIT // bit type
713/// Type ::= BITS '<' INTVAL '>' // bits<x> type
714/// Type ::= INT // int type
715/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000716/// Type ::= DAG // dag type
717/// Type ::= ClassID // Record Type
718///
719RecTy *TGParser::ParseType() {
720 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000721 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000722 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Tim Northover88403d72016-07-05 21:22:55 +0000723 case tgtok::Code: Lex.Lex(); return CodeRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000724 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
725 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000726 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000727 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000728 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Nicolai Haehnlefcd65252018-03-09 12:24:42 +0000729 TokError("unknown class name");
Craig Topper011817a2014-04-09 04:50:04 +0000730 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000731 case tgtok::Bits: {
732 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
733 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000734 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000735 }
736 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
737 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000738 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000739 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000740 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000741 if (Lex.Lex() != tgtok::greater) { // Eat count.
742 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000743 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000744 }
745 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000746 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000747 }
748 case tgtok::List: {
749 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
750 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000751 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000752 }
753 Lex.Lex(); // Eat '<'
754 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000755 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000756
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000757 if (Lex.getCode() != tgtok::greater) {
758 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000759 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000760 }
761 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000762 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000763 }
Bob Wilson7248f862009-11-22 04:24:42 +0000764 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000765}
766
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000767/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
768/// has already been read.
Matthias Braun215ff842016-12-05 07:35:13 +0000769Init *TGParser::ParseIDValue(Record *CurRec, StringInit *Name, SMLoc NameLoc,
David Greened4263a62011-10-19 13:04:20 +0000770 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000771 if (CurRec) {
772 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000773 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000774
Matthias Braun215ff842016-12-05 07:35:13 +0000775 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
David Greenedb10e692011-10-19 13:02:42 +0000776
David Greene47a665e2011-10-05 22:42:54 +0000777 if (CurMultiClass)
Matthias Braun215ff842016-12-05 07:35:13 +0000778 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
David Greenedb10e692011-10-19 13:02:42 +0000779 "::");
David Greene47a665e2011-10-05 22:42:54 +0000780
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000781 if (CurRec->isTemplateArg(TemplateArgName)) {
782 const RecordVal *RV = CurRec->getValue(TemplateArgName);
783 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000784 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000785 }
Matthias Braun215ff842016-12-05 07:35:13 +0000786 }
Bob Wilson7248f862009-11-22 04:24:42 +0000787
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000788 if (CurMultiClass) {
Nicolai Haehnle3c80e4c2018-03-05 14:01:38 +0000789 if (Name->getValue() == "NAME")
790 return VarInit::get(Name, StringRecTy::get());
791
Matthias Braun215ff842016-12-05 07:35:13 +0000792 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name, "::");
David Greenedb10e692011-10-19 13:02:42 +0000793
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000794 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
795 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
796 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000797 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000798 }
799 }
Bob Wilson7248f862009-11-22 04:24:42 +0000800
David Greenefb927af2012-02-22 16:09:41 +0000801 // If this is in a foreach loop, make sure it's not a loop iterator
Craig Toppereb4d7c62015-04-29 04:43:36 +0000802 for (const auto &L : Loops) {
803 VarInit *IterVar = dyn_cast<VarInit>(L.IterVar);
Matthias Braun215ff842016-12-05 07:35:13 +0000804 if (IterVar && IterVar->getNameInit() == Name)
David Greenefb927af2012-02-22 16:09:41 +0000805 return IterVar;
806 }
807
David Greene232bd602011-10-19 13:04:21 +0000808 if (Mode == ParseNameMode)
Matthias Braun215ff842016-12-05 07:35:13 +0000809 return Name;
David Greene232bd602011-10-19 13:04:21 +0000810
Nicolai Haehnlefcd65252018-03-09 12:24:42 +0000811 if (Init *I = Records.getGlobal(Name->getValue()))
812 return I;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000813
David Greene232bd602011-10-19 13:04:21 +0000814 if (Mode == ParseValueMode) {
Matthias Braun215ff842016-12-05 07:35:13 +0000815 Error(NameLoc, "Variable not defined: '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000816 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000817 }
Craig Toppera9642b42015-05-04 01:35:39 +0000818
Matthias Braun215ff842016-12-05 07:35:13 +0000819 return Name;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000820}
821
David Greene5d0c0512009-05-14 20:54:48 +0000822/// ParseOperation - Parse an operator. This returns null on error.
823///
824/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
825///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000826Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000827 switch (Lex.getCode()) {
828 default:
829 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000830 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000831 case tgtok::XHead:
832 case tgtok::XTail:
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000833 case tgtok::XSize:
David Greene2f7cf7f2011-01-07 17:05:37 +0000834 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000835 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
836 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000837 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000838
David Greenee8f3b272009-05-14 21:22:49 +0000839 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000840 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000841 case tgtok::XCast:
842 Lex.Lex(); // eat the operation
843 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000844
David Greenee8f3b272009-05-14 21:22:49 +0000845 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000846
Craig Topper011817a2014-04-09 04:50:04 +0000847 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000848 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000849 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000850 }
David Greene5d0c0512009-05-14 20:54:48 +0000851
David Greenee8f3b272009-05-14 21:22:49 +0000852 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000853 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000854 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000855 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000856 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000857 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000858 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000859 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000860 break;
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000861 case tgtok::XSize:
862 Lex.Lex();
863 Code = UnOpInit::SIZE;
864 Type = IntRecTy::get();
865 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000866 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000867 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000868 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000869 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000870 break;
David Greenee8f3b272009-05-14 21:22:49 +0000871 }
872 if (Lex.getCode() != tgtok::l_paren) {
873 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000874 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000875 }
876 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000877
David Greeneaf8ee2c2011-07-29 22:43:06 +0000878 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000879 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000880
Craig Topper85c07002015-04-30 05:54:22 +0000881 if (Code == UnOpInit::HEAD ||
882 Code == UnOpInit::TAIL ||
883 Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000884 ListInit *LHSl = dyn_cast<ListInit>(LHS);
885 StringInit *LHSs = dyn_cast<StringInit>(LHS);
886 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000887 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000888 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000889 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000890 }
891 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000892 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
893 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000894 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000895 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000896 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000897 }
898 }
899
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000900 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL ||
901 Code == UnOpInit::SIZE) {
Craig Topper011817a2014-04-09 04:50:04 +0000902 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000903 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000904 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000905 }
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000906 }
Bob Wilson7248f862009-11-22 04:24:42 +0000907
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000908 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
Craig Topperec9072d2015-05-14 05:53:53 +0000909 if (LHSl && LHSl->empty()) {
David Greened571b3c2009-05-14 22:38:31 +0000910 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000911 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000912 }
913 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000914 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000915 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000916 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000917 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000918 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000919 }
Craig Toppera9642b42015-05-04 01:35:39 +0000920 Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
921 : ListRecTy::get(Itemt->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000922 } else {
David Greened571b3c2009-05-14 22:38:31 +0000923 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000924 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000925 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000926 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000927 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000928 }
Craig Toppera9642b42015-05-04 01:35:39 +0000929 Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
David Greened571b3c2009-05-14 22:38:31 +0000930 }
931 }
932 }
933
David Greenee8f3b272009-05-14 21:22:49 +0000934 if (Lex.getCode() != tgtok::r_paren) {
935 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000936 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000937 }
938 Lex.Lex(); // eat the ')'
Nicolai Haehnlec47fe122018-03-19 14:13:37 +0000939 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec);
David Greenee8f3b272009-05-14 21:22:49 +0000940 }
David Greene5d0c0512009-05-14 20:54:48 +0000941
Nicolai Haehnleb5376052018-03-09 12:24:06 +0000942 case tgtok::XIsA: {
943 // Value ::= !isa '<' Type '>' '(' Value ')'
944 Lex.Lex(); // eat the operation
945
946 RecTy *Type = ParseOperatorType();
947 if (!Type)
948 return nullptr;
949
950 if (Lex.getCode() != tgtok::l_paren) {
951 TokError("expected '(' after type of !isa");
952 return nullptr;
953 }
954 Lex.Lex(); // eat the '('
955
956 Init *LHS = ParseValue(CurRec);
957 if (!LHS)
958 return nullptr;
959
960 if (Lex.getCode() != tgtok::r_paren) {
961 TokError("expected ')' in !isa");
962 return nullptr;
963 }
964 Lex.Lex(); // eat the ')'
965
966 return (IsAOpInit::get(Type, LHS))->Fold();
967 }
968
David Greene5d0c0512009-05-14 20:54:48 +0000969 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000970 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000971 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000972 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +0000973 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000974 case tgtok::XSRL:
975 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000976 case tgtok::XEq:
Nicolai Haehnleaa9ca692018-03-14 11:00:57 +0000977 case tgtok::XNe:
978 case tgtok::XLe:
979 case tgtok::XLt:
980 case tgtok::XGe:
981 case tgtok::XGt:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000982 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000983 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000984 tgtok::TokKind OpTok = Lex.getCode();
985 SMLoc OpLoc = Lex.getLoc();
986 Lex.Lex(); // eat the operation
987
David Greene5d0c0512009-05-14 20:54:48 +0000988 BinOpInit::BinaryOp Code;
Chris Lattner61ea00b2010-10-05 23:58:18 +0000989 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000990 default: llvm_unreachable("Unhandled code!");
Nicolai Haehnle77841b12018-03-14 11:00:43 +0000991 case tgtok::XConcat: Code = BinOpInit::CONCAT; break;
992 case tgtok::XADD: Code = BinOpInit::ADD; break;
993 case tgtok::XAND: Code = BinOpInit::AND; break;
994 case tgtok::XOR: Code = BinOpInit::OR; break;
995 case tgtok::XSRA: Code = BinOpInit::SRA; break;
996 case tgtok::XSRL: Code = BinOpInit::SRL; break;
997 case tgtok::XSHL: Code = BinOpInit::SHL; break;
998 case tgtok::XEq: Code = BinOpInit::EQ; break;
Nicolai Haehnleaa9ca692018-03-14 11:00:57 +0000999 case tgtok::XNe: Code = BinOpInit::NE; break;
1000 case tgtok::XLe: Code = BinOpInit::LE; break;
1001 case tgtok::XLt: Code = BinOpInit::LT; break;
1002 case tgtok::XGe: Code = BinOpInit::GE; break;
1003 case tgtok::XGt: Code = BinOpInit::GT; break;
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001004 case tgtok::XListConcat: Code = BinOpInit::LISTCONCAT; break;
1005 case tgtok::XStrConcat: Code = BinOpInit::STRCONCAT; break;
1006 }
1007
1008 RecTy *Type = nullptr;
1009 RecTy *ArgType = nullptr;
1010 switch (OpTok) {
1011 default:
1012 llvm_unreachable("Unhandled code!");
1013 case tgtok::XConcat:
1014 Type = DagRecTy::get();
1015 ArgType = DagRecTy::get();
1016 break;
1017 case tgtok::XAND:
1018 case tgtok::XOR:
1019 case tgtok::XSRA:
1020 case tgtok::XSRL:
1021 case tgtok::XSHL:
1022 case tgtok::XADD:
1023 Type = IntRecTy::get();
1024 ArgType = IntRecTy::get();
1025 break;
1026 case tgtok::XEq:
Nicolai Haehnleaa9ca692018-03-14 11:00:57 +00001027 case tgtok::XNe:
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001028 Type = BitRecTy::get();
Nicolai Haehnleaa9ca692018-03-14 11:00:57 +00001029 // ArgType for Eq / Ne is not known at this point
1030 break;
1031 case tgtok::XLe:
1032 case tgtok::XLt:
1033 case tgtok::XGe:
1034 case tgtok::XGt:
1035 Type = BitRecTy::get();
1036 ArgType = IntRecTy::get();
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001037 break;
Daniel Sanders314e80e2014-05-07 10:13:19 +00001038 case tgtok::XListConcat:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001039 // We don't know the list type until we parse the first argument
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001040 ArgType = ItemType;
Daniel Sanders314e80e2014-05-07 10:13:19 +00001041 break;
Bob Wilson7248f862009-11-22 04:24:42 +00001042 case tgtok::XStrConcat:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001043 Type = StringRecTy::get();
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001044 ArgType = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +00001045 break;
David Greene5d0c0512009-05-14 20:54:48 +00001046 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00001047
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001048 if (Type && ItemType && !Type->typeIsConvertibleTo(ItemType)) {
1049 Error(OpLoc, Twine("expected value of type '") +
1050 ItemType->getAsString() + "', got '" +
1051 Type->getAsString() + "'");
1052 return nullptr;
1053 }
1054
David Greene5d0c0512009-05-14 20:54:48 +00001055 if (Lex.getCode() != tgtok::l_paren) {
1056 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001057 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001058 }
1059 Lex.Lex(); // eat the '('
1060
David Greeneaf8ee2c2011-07-29 22:43:06 +00001061 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +00001062
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001063 for (;;) {
1064 SMLoc InitLoc = Lex.getLoc();
1065 InitList.push_back(ParseValue(CurRec, ArgType));
Craig Topper011817a2014-04-09 04:50:04 +00001066 if (!InitList.back()) return nullptr;
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001067
1068 // All BinOps require their arguments to be of compatible types.
1069 TypedInit *TI = dyn_cast<TypedInit>(InitList.back());
1070 if (!ArgType) {
1071 ArgType = TI->getType();
1072
1073 switch (Code) {
1074 case BinOpInit::LISTCONCAT:
1075 if (!isa<ListRecTy>(ArgType)) {
1076 Error(InitLoc, Twine("expected a list, got value of type '") +
1077 ArgType->getAsString() + "'");
1078 return nullptr;
1079 }
1080 break;
1081 case BinOpInit::EQ:
Nicolai Haehnleaa9ca692018-03-14 11:00:57 +00001082 case BinOpInit::NE:
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001083 if (!ArgType->typeIsConvertibleTo(IntRecTy::get()) &&
1084 !ArgType->typeIsConvertibleTo(StringRecTy::get())) {
1085 Error(InitLoc, Twine("expected int, bits, or string; got value of "
1086 "type '") + ArgType->getAsString() + "'");
1087 return nullptr;
1088 }
1089 break;
1090 default: llvm_unreachable("other ops have fixed argument types");
1091 }
1092 } else {
1093 RecTy *Resolved = resolveTypes(ArgType, TI->getType());
1094 if (!Resolved) {
1095 Error(InitLoc, Twine("expected value of type '") +
1096 ArgType->getAsString() + "', got '" +
1097 TI->getType()->getAsString() + "'");
1098 return nullptr;
1099 }
1100 if (Code != BinOpInit::ADD && Code != BinOpInit::AND &&
1101 Code != BinOpInit::OR && Code != BinOpInit::SRA &&
1102 Code != BinOpInit::SRL && Code != BinOpInit::SHL)
1103 ArgType = Resolved;
1104 }
1105
1106 if (Lex.getCode() != tgtok::comma)
1107 break;
1108 Lex.Lex(); // eat the ','
David Greene5d0c0512009-05-14 20:54:48 +00001109 }
David Greene5d0c0512009-05-14 20:54:48 +00001110
1111 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +00001112 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +00001113 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001114 }
1115 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +00001116
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001117 if (Code == BinOpInit::LISTCONCAT)
1118 Type = ArgType;
Daniel Sanders314e80e2014-05-07 10:13:19 +00001119
Chris Lattner61ea00b2010-10-05 23:58:18 +00001120 // We allow multiple operands to associative operators like !strconcat as
1121 // shorthand for nesting them.
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001122 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT ||
1123 Code == BinOpInit::CONCAT || Code == BinOpInit::ADD ||
1124 Code == BinOpInit::AND || Code == BinOpInit::OR) {
Chris Lattner61ea00b2010-10-05 23:58:18 +00001125 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001126 Init *RHS = InitList.pop_back_val();
Nicolai Haehnlec47fe122018-03-19 14:13:37 +00001127 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))->Fold(CurRec);
Chris Lattner61ea00b2010-10-05 23:58:18 +00001128 InitList.back() = RHS;
1129 }
1130 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00001131
Chris Lattner61ea00b2010-10-05 23:58:18 +00001132 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +00001133 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Nicolai Haehnlec47fe122018-03-19 14:13:37 +00001134 ->Fold(CurRec);
Mikhail Glushenkovde683892010-10-23 07:32:37 +00001135
Chris Lattner61ea00b2010-10-05 23:58:18 +00001136 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +00001137 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001138 }
1139
Nicolai Haehnle8ebf7e42018-03-05 15:21:04 +00001140 case tgtok::XForEach: { // Value ::= !foreach '(' Id ',' Value ',' Value ')'
1141 SMLoc OpLoc = Lex.getLoc();
1142 Lex.Lex(); // eat the operation
1143 if (Lex.getCode() != tgtok::l_paren) {
1144 TokError("expected '(' after !foreach");
1145 return nullptr;
1146 }
1147
1148 if (Lex.Lex() != tgtok::Id) { // eat the '('
1149 TokError("first argument of !foreach must be an identifier");
1150 return nullptr;
1151 }
1152
1153 Init *LHS = StringInit::get(Lex.getCurStrVal());
1154
1155 if (CurRec->getValue(LHS)) {
1156 TokError((Twine("iteration variable '") + LHS->getAsString() +
1157 "' already defined")
1158 .str());
1159 return nullptr;
1160 }
1161
1162 if (Lex.Lex() != tgtok::comma) { // eat the id
1163 TokError("expected ',' in ternary operator");
1164 return nullptr;
1165 }
1166 Lex.Lex(); // eat the ','
1167
1168 Init *MHS = ParseValue(CurRec);
1169 if (!MHS)
1170 return nullptr;
1171
1172 if (Lex.getCode() != tgtok::comma) {
1173 TokError("expected ',' in ternary operator");
1174 return nullptr;
1175 }
1176 Lex.Lex(); // eat the ','
1177
1178 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1179 if (!MHSt) {
1180 TokError("could not get type of !foreach input");
1181 return nullptr;
1182 }
1183
1184 RecTy *InEltType = nullptr;
1185 RecTy *OutEltType = nullptr;
1186 bool IsDAG = false;
1187
1188 if (ListRecTy *InListTy = dyn_cast<ListRecTy>(MHSt->getType())) {
1189 InEltType = InListTy->getElementType();
1190 if (ItemType) {
1191 if (ListRecTy *OutListTy = dyn_cast<ListRecTy>(ItemType)) {
1192 OutEltType = OutListTy->getElementType();
1193 } else {
1194 Error(OpLoc,
1195 "expected value of type '" + Twine(ItemType->getAsString()) +
1196 "', but got !foreach of list type");
1197 return nullptr;
1198 }
1199 }
1200 } else if (DagRecTy *InDagTy = dyn_cast<DagRecTy>(MHSt->getType())) {
1201 InEltType = InDagTy;
1202 if (ItemType && !isa<DagRecTy>(ItemType)) {
1203 Error(OpLoc,
1204 "expected value of type '" + Twine(ItemType->getAsString()) +
1205 "', but got !foreach of dag type");
1206 return nullptr;
1207 }
1208 IsDAG = true;
1209 } else {
1210 TokError("!foreach must have list or dag input");
1211 return nullptr;
1212 }
1213
1214 CurRec->addValue(RecordVal(LHS, InEltType, false));
1215 Init *RHS = ParseValue(CurRec, OutEltType);
1216 CurRec->removeValue(LHS);
1217 if (!RHS)
1218 return nullptr;
1219
1220 if (Lex.getCode() != tgtok::r_paren) {
1221 TokError("expected ')' in binary operator");
1222 return nullptr;
1223 }
1224 Lex.Lex(); // eat the ')'
1225
1226 RecTy *OutType;
1227 if (IsDAG) {
1228 OutType = InEltType;
1229 } else {
1230 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1231 if (!RHSt) {
1232 TokError("could not get type of !foreach result");
1233 return nullptr;
1234 }
1235 OutType = RHSt->getType()->getListTy();
1236 }
1237
1238 return (TernOpInit::get(TernOpInit::FOREACH, LHS, MHS, RHS, OutType))
Nicolai Haehnlec47fe122018-03-19 14:13:37 +00001239 ->Fold(CurRec);
Nicolai Haehnle8ebf7e42018-03-05 15:21:04 +00001240 }
1241
Nicolai Haehnle6c118652018-03-14 11:00:26 +00001242 case tgtok::XDag:
David Greene3587eed2009-05-14 23:26:46 +00001243 case tgtok::XIf:
David Greene98ed3c72009-05-14 21:54:42 +00001244 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
1245 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +00001246 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001247
David Greene98ed3c72009-05-14 21:54:42 +00001248 tgtok::TokKind LexCode = Lex.getCode();
1249 Lex.Lex(); // eat the operation
1250 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001251 default: llvm_unreachable("Unhandled code!");
Nicolai Haehnle6c118652018-03-14 11:00:26 +00001252 case tgtok::XDag:
1253 Code = TernOpInit::DAG;
1254 Type = DagRecTy::get();
1255 ItemType = nullptr;
1256 break;
David Greene3587eed2009-05-14 23:26:46 +00001257 case tgtok::XIf:
1258 Code = TernOpInit::IF;
1259 break;
David Greene98ed3c72009-05-14 21:54:42 +00001260 case tgtok::XSubst:
1261 Code = TernOpInit::SUBST;
1262 break;
1263 }
1264 if (Lex.getCode() != tgtok::l_paren) {
1265 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001266 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001267 }
1268 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001269
David Greeneaf8ee2c2011-07-29 22:43:06 +00001270 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001271 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001272
David Greene98ed3c72009-05-14 21:54:42 +00001273 if (Lex.getCode() != tgtok::comma) {
1274 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001275 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001276 }
1277 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001278
Nicolai Haehnle6c118652018-03-14 11:00:26 +00001279 SMLoc MHSLoc = Lex.getLoc();
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001280 Init *MHS = ParseValue(CurRec, ItemType);
1281 if (!MHS)
1282 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001283
David Greene98ed3c72009-05-14 21:54:42 +00001284 if (Lex.getCode() != tgtok::comma) {
1285 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001286 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001287 }
1288 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001289
Nicolai Haehnle6c118652018-03-14 11:00:26 +00001290 SMLoc RHSLoc = Lex.getLoc();
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001291 Init *RHS = ParseValue(CurRec, ItemType);
1292 if (!RHS)
1293 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001294
David Greene98ed3c72009-05-14 21:54:42 +00001295 if (Lex.getCode() != tgtok::r_paren) {
1296 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001297 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001298 }
1299 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001300
David Greene98ed3c72009-05-14 21:54:42 +00001301 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001302 default: llvm_unreachable("Unhandled code!");
Nicolai Haehnle6c118652018-03-14 11:00:26 +00001303 case tgtok::XDag: {
1304 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1305 if (!MHSt && !isa<UnsetInit>(MHS)) {
1306 Error(MHSLoc, "could not determine type of the child list in !dag");
1307 return nullptr;
1308 }
1309 if (MHSt && !isa<ListRecTy>(MHSt->getType())) {
1310 Error(MHSLoc, Twine("expected list of children, got type '") +
1311 MHSt->getType()->getAsString() + "'");
1312 return nullptr;
1313 }
1314
1315 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1316 if (!RHSt && !isa<UnsetInit>(RHS)) {
1317 Error(RHSLoc, "could not determine type of the name list in !dag");
1318 return nullptr;
1319 }
1320 if (RHSt && StringRecTy::get()->getListTy() != RHSt->getType()) {
1321 Error(RHSLoc, Twine("expected list<string>, got type '") +
1322 RHSt->getType()->getAsString() + "'");
1323 return nullptr;
1324 }
1325
1326 if (!MHSt && !RHSt) {
1327 Error(MHSLoc,
1328 "cannot have both unset children and unset names in !dag");
1329 return nullptr;
1330 }
1331 break;
1332 }
David Greene3587eed2009-05-14 23:26:46 +00001333 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001334 RecTy *MHSTy = nullptr;
1335 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001336
Sean Silvafb509ed2012-10-10 20:24:43 +00001337 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001338 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001339 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001340 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001341 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001342 MHSTy = BitRecTy::get();
1343
Sean Silvafb509ed2012-10-10 20:24:43 +00001344 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001345 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001346 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001347 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001348 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001349 RHSTy = BitRecTy::get();
1350
1351 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001352 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001353 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001354 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001355 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001356
1357 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001358 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001359 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001360 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001361
Nicolai Haehnle13080fd2018-03-06 13:48:20 +00001362 Type = resolveTypes(MHSTy, RHSTy);
1363 if (!Type) {
1364 TokError(Twine("inconsistent types '") + MHSTy->getAsString() +
1365 "' and '" + RHSTy->getAsString() + "' for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001366 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001367 }
1368 break;
1369 }
David Greene98ed3c72009-05-14 21:54:42 +00001370 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001371 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001372 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001373 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001374 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001375 }
1376 Type = RHSt->getType();
1377 break;
1378 }
1379 }
Nicolai Haehnlec47fe122018-03-19 14:13:37 +00001380 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec);
David Greene98ed3c72009-05-14 21:54:42 +00001381 }
Nicolai Haehnled34f6842018-03-06 13:49:16 +00001382
1383 case tgtok::XFoldl: {
1384 // Value ::= !foldl '(' Id ',' Id ',' Value ',' Value ',' Value ')'
1385 Lex.Lex(); // eat the operation
1386 if (Lex.getCode() != tgtok::l_paren) {
1387 TokError("expected '(' after !foldl");
1388 return nullptr;
1389 }
1390 Lex.Lex(); // eat the '('
1391
1392 Init *StartUntyped = ParseValue(CurRec);
1393 if (!StartUntyped)
1394 return nullptr;
1395
1396 TypedInit *Start = dyn_cast<TypedInit>(StartUntyped);
1397 if (!Start) {
1398 TokError(Twine("could not get type of !foldl start: '") +
1399 StartUntyped->getAsString() + "'");
1400 return nullptr;
1401 }
1402
1403 if (Lex.getCode() != tgtok::comma) {
1404 TokError("expected ',' in !foldl");
1405 return nullptr;
1406 }
1407 Lex.Lex(); // eat the ','
1408
1409 Init *ListUntyped = ParseValue(CurRec);
1410 if (!ListUntyped)
1411 return nullptr;
1412
1413 TypedInit *List = dyn_cast<TypedInit>(ListUntyped);
1414 if (!List) {
1415 TokError(Twine("could not get type of !foldl list: '") +
1416 ListUntyped->getAsString() + "'");
1417 return nullptr;
1418 }
1419
1420 ListRecTy *ListType = dyn_cast<ListRecTy>(List->getType());
1421 if (!ListType) {
1422 TokError(Twine("!foldl list must be a list, but is of type '") +
1423 List->getType()->getAsString());
1424 return nullptr;
1425 }
1426
1427 if (Lex.getCode() != tgtok::comma) {
1428 TokError("expected ',' in !foldl");
1429 return nullptr;
1430 }
1431
1432 if (Lex.Lex() != tgtok::Id) { // eat the ','
1433 TokError("third argument of !foldl must be an identifier");
1434 return nullptr;
1435 }
1436
1437 Init *A = StringInit::get(Lex.getCurStrVal());
1438 if (CurRec->getValue(A)) {
1439 TokError((Twine("left !foldl variable '") + A->getAsString() +
1440 "' already defined")
1441 .str());
1442 return nullptr;
1443 }
1444
1445 if (Lex.Lex() != tgtok::comma) { // eat the id
1446 TokError("expected ',' in !foldl");
1447 return nullptr;
1448 }
1449
1450 if (Lex.Lex() != tgtok::Id) { // eat the ','
1451 TokError("fourth argument of !foldl must be an identifier");
1452 return nullptr;
1453 }
1454
1455 Init *B = StringInit::get(Lex.getCurStrVal());
1456 if (CurRec->getValue(B)) {
1457 TokError((Twine("right !foldl variable '") + B->getAsString() +
1458 "' already defined")
1459 .str());
1460 return nullptr;
1461 }
1462
1463 if (Lex.Lex() != tgtok::comma) { // eat the id
1464 TokError("expected ',' in !foldl");
1465 return nullptr;
1466 }
1467 Lex.Lex(); // eat the ','
1468
1469 CurRec->addValue(RecordVal(A, Start->getType(), false));
1470 CurRec->addValue(RecordVal(B, ListType->getElementType(), false));
1471 Init *ExprUntyped = ParseValue(CurRec);
1472 CurRec->removeValue(A);
1473 CurRec->removeValue(B);
1474 if (!ExprUntyped)
1475 return nullptr;
1476
1477 TypedInit *Expr = dyn_cast<TypedInit>(ExprUntyped);
1478 if (!Expr) {
1479 TokError("could not get type of !foldl expression");
1480 return nullptr;
1481 }
1482
1483 if (Expr->getType() != Start->getType()) {
1484 TokError(Twine("!foldl expression must be of same type as start (") +
1485 Start->getType()->getAsString() + "), but is of type " +
1486 Expr->getType()->getAsString());
1487 return nullptr;
1488 }
1489
1490 if (Lex.getCode() != tgtok::r_paren) {
1491 TokError("expected ')' in fold operator");
1492 return nullptr;
1493 }
1494 Lex.Lex(); // eat the ')'
1495
1496 return FoldOpInit::get(Start, List, A, B, Expr, Start->getType())
1497 ->Fold(CurRec);
1498 }
David Greene5d0c0512009-05-14 20:54:48 +00001499 }
David Greene5d0c0512009-05-14 20:54:48 +00001500}
1501
1502/// ParseOperatorType - Parse a type for an operator. This returns
1503/// null on error.
1504///
1505/// OperatorType ::= '<' Type '>'
1506///
Dan Gohman1432ef82009-08-12 22:10:57 +00001507RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001508 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001509
1510 if (Lex.getCode() != tgtok::less) {
1511 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001512 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001513 }
1514 Lex.Lex(); // eat the <
1515
1516 Type = ParseType();
1517
Craig Topper011817a2014-04-09 04:50:04 +00001518 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001519 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001520 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001521 }
1522
1523 if (Lex.getCode() != tgtok::greater) {
1524 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001525 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001526 }
1527 Lex.Lex(); // eat the >
1528
1529 return Type;
1530}
1531
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001532/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1533///
1534/// SimpleValue ::= IDValue
1535/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001536/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001537/// SimpleValue ::= CODEFRAGMENT
1538/// SimpleValue ::= '?'
1539/// SimpleValue ::= '{' ValueList '}'
1540/// SimpleValue ::= ID '<' ValueListNE '>'
1541/// SimpleValue ::= '[' ValueList ']'
1542/// SimpleValue ::= '(' IDValue DagArgList ')'
1543/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001544/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001545/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1546/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1547/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001548/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001549/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1550///
David Greened4263a62011-10-19 13:04:20 +00001551Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1552 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001553 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001554 switch (Lex.getCode()) {
1555 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001556 case tgtok::paste:
1557 // This is a leading paste operation. This is deprecated but
1558 // still exists in some .td files. Ignore it.
1559 Lex.Lex(); // Skip '#'.
1560 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001561 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001562 case tgtok::BinaryIntVal: {
1563 auto BinaryVal = Lex.getCurBinaryIntVal();
1564 SmallVector<Init*, 16> Bits(BinaryVal.second);
1565 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001566 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001567 R = BitsInit::get(Bits);
1568 Lex.Lex();
1569 break;
1570 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001571 case tgtok::StrVal: {
1572 std::string Val = Lex.getCurStrVal();
1573 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001574
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001575 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001576 while (Lex.getCode() == tgtok::StrVal) {
1577 Val += Lex.getCurStrVal();
1578 Lex.Lex();
1579 }
Bob Wilson7248f862009-11-22 04:24:42 +00001580
David Greenee32ebf22011-07-29 19:07:07 +00001581 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001582 break;
1583 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001584 case tgtok::CodeFragment:
Tim Northover88403d72016-07-05 21:22:55 +00001585 R = CodeInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001586 Lex.Lex();
1587 break;
1588 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001589 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001590 Lex.Lex();
1591 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001592 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001593 SMLoc NameLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00001594 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001595 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001596 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001597
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001598 // Value ::= ID '<' ValueListNE '>'
1599 if (Lex.Lex() == tgtok::greater) {
1600 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001601 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001602 }
David Greene8618f952009-06-08 20:23:18 +00001603
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001604 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1605 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1606 // body.
Matthias Braun215ff842016-12-05 07:35:13 +00001607 Record *Class = Records.getClass(Name->getValue());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001608 if (!Class) {
Matthias Braun215ff842016-12-05 07:35:13 +00001609 Error(NameLoc, "Expected a class name, got '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001610 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001611 }
David Greene8618f952009-06-08 20:23:18 +00001612
Nicolai Haehnled4c0a5d2018-03-06 13:49:01 +00001613 SmallVector<Init *, 8> Args;
1614 ParseValueList(Args, CurRec, Class);
1615 if (Args.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001616
David Greene8618f952009-06-08 20:23:18 +00001617 if (Lex.getCode() != tgtok::greater) {
1618 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001619 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001620 }
1621 Lex.Lex(); // eat the '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001622
Nicolai Haehnled4c0a5d2018-03-06 13:49:01 +00001623 // Typecheck the template arguments list
1624 ArrayRef<Init *> ExpectedArgs = Class->getTemplateArgs();
1625 if (ExpectedArgs.size() < Args.size()) {
1626 Error(NameLoc,
1627 "More template args specified than expected");
Craig Topper011817a2014-04-09 04:50:04 +00001628 return nullptr;
Hal Finkela8c1f462014-01-02 20:47:09 +00001629 }
Bob Wilson7248f862009-11-22 04:24:42 +00001630
Nicolai Haehnled4c0a5d2018-03-06 13:49:01 +00001631 for (unsigned i = 0, e = ExpectedArgs.size(); i != e; ++i) {
1632 RecordVal *ExpectedArg = Class->getValue(ExpectedArgs[i]);
1633 if (i < Args.size()) {
1634 if (TypedInit *TI = dyn_cast<TypedInit>(Args[i])) {
1635 RecTy *ExpectedType = ExpectedArg->getType();
1636 if (!TI->getType()->typeIsConvertibleTo(ExpectedType)) {
1637 Error(NameLoc,
1638 "Value specified for template argument #" + Twine(i) + " (" +
1639 ExpectedArg->getNameInitAsString() + ") is of type '" +
1640 TI->getType()->getAsString() + "', expected '" +
1641 ExpectedType->getAsString() + "': " + TI->getAsString());
1642 return nullptr;
1643 }
1644 continue;
1645 }
1646 } else if (ExpectedArg->getValue()->isComplete())
1647 continue;
1648
1649 Error(NameLoc,
1650 "Value not specified for template argument #" + Twine(i) + " (" +
1651 ExpectedArgs[i]->getAsUnquotedString() + ")");
1652 return nullptr;
1653 }
1654
1655 return VarDefInit::get(Class, Args)->Fold();
Bob Wilson7248f862009-11-22 04:24:42 +00001656 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001657 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001658 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001659 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001660 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001661
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001662 if (Lex.getCode() != tgtok::r_brace) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001663 ParseValueList(Vals, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001664 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001665 }
1666 if (Lex.getCode() != tgtok::r_brace) {
1667 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001668 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001669 }
1670 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001671
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001672 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001673
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001674 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1675 // first. We'll first read everything in to a vector, then we can reverse
1676 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001677 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001678 // FIXME: The following two loops would not be duplicated
1679 // if the API was a little more orthogonal.
1680
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001681 // bits<n> values are allowed to initialize n bits.
1682 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1683 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1684 NewBits.push_back(BI->getBit((e - i) - 1));
1685 continue;
1686 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001687 // bits<n> can also come from variable initializers.
1688 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1689 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1690 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1691 NewBits.push_back(VI->getBit((e - i) - 1));
1692 continue;
1693 }
1694 // Fallthrough to try convert this to a bit.
1695 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001696 // All other values must be convertible to just a single bit.
Nicolai Haehnledfda9dc2018-03-06 13:48:39 +00001697 Init *Bit = Vals[i]->getCastTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001698 if (!Bit) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001699 Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
Chris Lattner52416952007-11-22 21:06:59 +00001700 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001701 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001702 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001703 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001704 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001705 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001706 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001707 }
1708 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1709 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001710 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001711
Craig Topper011817a2014-04-09 04:50:04 +00001712 RecTy *DeducedEltTy = nullptr;
1713 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001714
Craig Topper011817a2014-04-09 04:50:04 +00001715 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001716 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001717 if (!ListType) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001718 TokError(Twine("Type mismatch for list, expected list type, got ") +
1719 ItemType->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001720 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001721 }
1722 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001723 }
David Greene8618f952009-06-08 20:23:18 +00001724
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001725 if (Lex.getCode() != tgtok::r_square) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001726 ParseValueList(Vals, CurRec, nullptr,
1727 GivenListTy ? GivenListTy->getElementType() : nullptr);
Craig Topper011817a2014-04-09 04:50:04 +00001728 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001729 }
1730 if (Lex.getCode() != tgtok::r_square) {
1731 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001732 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001733 }
1734 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001735
Craig Topper011817a2014-04-09 04:50:04 +00001736 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001737 if (Lex.getCode() == tgtok::less) {
1738 // Optional list element type
1739 Lex.Lex(); // eat the '<'
1740
1741 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001742 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001743 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001744 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001745 }
1746
1747 if (Lex.getCode() != tgtok::greater) {
1748 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001749 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001750 }
1751 Lex.Lex(); // eat the '>'
1752 }
1753
1754 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001755 RecTy *EltTy = nullptr;
Craig Toppereb4d7c62015-04-29 04:43:36 +00001756 for (Init *V : Vals) {
1757 TypedInit *TArg = dyn_cast<TypedInit>(V);
Nicolai Haehnleef60a262018-03-14 11:00:33 +00001758 if (TArg) {
1759 if (EltTy) {
1760 EltTy = resolveTypes(EltTy, TArg->getType());
1761 if (!EltTy) {
1762 TokError("Incompatible types in list elements");
1763 return nullptr;
1764 }
1765 } else {
1766 EltTy = TArg->getType();
David Greene8618f952009-06-08 20:23:18 +00001767 }
David Greene8618f952009-06-08 20:23:18 +00001768 }
1769 }
1770
Craig Topper011817a2014-04-09 04:50:04 +00001771 if (GivenEltTy) {
1772 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001773 // Verify consistency
1774 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1775 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001776 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001777 }
1778 }
1779 EltTy = GivenEltTy;
1780 }
1781
Craig Topper011817a2014-04-09 04:50:04 +00001782 if (!EltTy) {
1783 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001784 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001785 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001786 }
1787 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001788 } else {
David Greene8618f952009-06-08 20:23:18 +00001789 // Make sure the deduced type is compatible with the given type
1790 if (GivenListTy) {
1791 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
Nicolai Haehnlef19083d2018-02-22 15:26:21 +00001792 TokError(Twine("Element type mismatch for list: element type '") +
1793 EltTy->getAsString() + "' not convertible to '" +
1794 GivenListTy->getElementType()->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001795 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001796 }
1797 }
1798 DeducedEltTy = EltTy;
1799 }
Bob Wilson7248f862009-11-22 04:24:42 +00001800
David Greenee32ebf22011-07-29 19:07:07 +00001801 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001802 }
1803 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1804 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001805 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001806 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001807 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001808 }
Bob Wilson7248f862009-11-22 04:24:42 +00001809
David Greeneaf8ee2c2011-07-29 22:43:06 +00001810 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001811 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001812
Nate Begemandbe3f772009-03-19 05:21:56 +00001813 // If the operator name is present, parse it.
Matthias Braun7cf3b112016-12-05 06:00:41 +00001814 StringInit *OperatorName = nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001815 if (Lex.getCode() == tgtok::colon) {
1816 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1817 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001818 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001819 }
Matthias Braun7cf3b112016-12-05 06:00:41 +00001820 OperatorName = StringInit::get(Lex.getCurStrVal());
Nate Begemandbe3f772009-03-19 05:21:56 +00001821 Lex.Lex(); // eat the VarName.
1822 }
Bob Wilson7248f862009-11-22 04:24:42 +00001823
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001824 SmallVector<std::pair<llvm::Init*, StringInit*>, 8> DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001825 if (Lex.getCode() != tgtok::r_paren) {
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001826 ParseDagArgList(DagArgs, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001827 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001828 }
Bob Wilson7248f862009-11-22 04:24:42 +00001829
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001830 if (Lex.getCode() != tgtok::r_paren) {
1831 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001832 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001833 }
1834 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001835
David Greenee32ebf22011-07-29 19:07:07 +00001836 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001837 }
Bob Wilson7248f862009-11-22 04:24:42 +00001838
David Greene2f7cf7f2011-01-07 17:05:37 +00001839 case tgtok::XHead:
1840 case tgtok::XTail:
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +00001841 case tgtok::XSize:
David Greene2f7cf7f2011-01-07 17:05:37 +00001842 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001843 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Nicolai Haehnleb5376052018-03-09 12:24:06 +00001844 case tgtok::XIsA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001845 case tgtok::XConcat:
Nicolai Haehnle6c118652018-03-14 11:00:26 +00001846 case tgtok::XDag:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001847 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001848 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +00001849 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +00001850 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001851 case tgtok::XSRL:
1852 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001853 case tgtok::XEq:
Nicolai Haehnleaa9ca692018-03-14 11:00:57 +00001854 case tgtok::XNe:
1855 case tgtok::XLe:
1856 case tgtok::XLt:
1857 case tgtok::XGe:
1858 case tgtok::XGt:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001859 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001860 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001861 case tgtok::XIf:
Nicolai Haehnled34f6842018-03-06 13:49:16 +00001862 case tgtok::XFoldl:
David Greenee917fff2009-05-14 22:23:47 +00001863 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001864 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001865 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001866 }
1867 }
Bob Wilson7248f862009-11-22 04:24:42 +00001868
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001869 return R;
1870}
1871
1872/// ParseValue - Parse a tblgen value. This returns null on error.
1873///
1874/// Value ::= SimpleValue ValueSuffix*
1875/// ValueSuffix ::= '{' BitList '}'
1876/// ValueSuffix ::= '[' BitList ']'
1877/// ValueSuffix ::= '.' ID
1878///
David Greened4263a62011-10-19 13:04:20 +00001879Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1880 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001881 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001882
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001883 // Parse the suffixes now if present.
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001884 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001885 switch (Lex.getCode()) {
1886 default: return Result;
1887 case tgtok::l_brace: {
Nicolai Haehnle2435855a2018-03-09 12:24:20 +00001888 if (Mode == ParseNameMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001889 // This is the beginning of the object body.
1890 return Result;
1891
Chris Lattner526c8cb2009-06-21 03:39:35 +00001892 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001893 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001894 SmallVector<unsigned, 16> Ranges;
1895 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001896 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001897
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001898 // Reverse the bitlist.
1899 std::reverse(Ranges.begin(), Ranges.end());
1900 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001901 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001902 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001903 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001904 }
Bob Wilson7248f862009-11-22 04:24:42 +00001905
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001906 // Eat the '}'.
1907 if (Lex.getCode() != tgtok::r_brace) {
1908 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001909 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001910 }
1911 Lex.Lex();
1912 break;
1913 }
1914 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001915 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001916 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001917 SmallVector<unsigned, 16> Ranges;
1918 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001919 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001920
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001921 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001922 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001923 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001924 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001925 }
Bob Wilson7248f862009-11-22 04:24:42 +00001926
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001927 // Eat the ']'.
1928 if (Lex.getCode() != tgtok::r_square) {
1929 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001930 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001931 }
1932 Lex.Lex();
1933 break;
1934 }
Matthias Braun6a441832016-12-05 06:00:36 +00001935 case tgtok::period: {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001936 if (Lex.Lex() != tgtok::Id) { // eat the .
1937 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001938 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001939 }
Matthias Braun6a441832016-12-05 06:00:36 +00001940 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
1941 if (!Result->getFieldType(FieldName)) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001942 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001943 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001944 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001945 }
Nicolai Haehnle8aa9d582018-03-09 12:24:30 +00001946 Result = FieldInit::get(Result, FieldName)->Fold();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001947 Lex.Lex(); // eat field name
1948 break;
Matthias Braun6a441832016-12-05 06:00:36 +00001949 }
David Greene8e85b482011-10-19 13:04:43 +00001950
1951 case tgtok::paste:
1952 SMLoc PasteLoc = Lex.getLoc();
1953
1954 // Create a !strconcat() operation, first casting each operand to
1955 // a string if necessary.
1956
Sean Silvafb509ed2012-10-10 20:24:43 +00001957 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001958 if (!LHS) {
1959 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001960 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001961 }
Craig Toppera9642b42015-05-04 01:35:39 +00001962
David Greene8e85b482011-10-19 13:04:43 +00001963 if (LHS->getType() != StringRecTy::get()) {
1964 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1965 }
1966
Craig Topper011817a2014-04-09 04:50:04 +00001967 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001968
1969 Lex.Lex(); // Eat the '#'.
Nicolai Haehnled34f6842018-03-06 13:49:16 +00001970 switch (Lex.getCode()) {
David Greene8e85b482011-10-19 13:04:43 +00001971 case tgtok::colon:
1972 case tgtok::semi:
1973 case tgtok::l_brace:
1974 // These are all of the tokens that can begin an object body.
1975 // Some of these can also begin values but we disallow those cases
1976 // because they are unlikely to be useful.
Craig Toppera9642b42015-05-04 01:35:39 +00001977
David Greene8e85b482011-10-19 13:04:43 +00001978 // Trailing paste, concat with an empty string.
1979 RHS = StringInit::get("");
1980 break;
1981
1982 default:
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001983 Init *RHSResult = ParseValue(CurRec, nullptr, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001984 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001985 if (!RHS) {
1986 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001987 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001988 }
1989
1990 if (RHS->getType() != StringRecTy::get()) {
1991 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1992 }
Craig Toppera9642b42015-05-04 01:35:39 +00001993
David Greene8e85b482011-10-19 13:04:43 +00001994 break;
1995 }
1996
Nicolai Haehnlec47fe122018-03-19 14:13:37 +00001997 Result =
1998 BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS, StringRecTy::get())
1999 ->Fold(CurRec);
David Greene8e85b482011-10-19 13:04:43 +00002000 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002001 }
2002 }
2003}
2004
2005/// ParseDagArgList - Parse the argument list for a dag literal expression.
2006///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002007/// DagArg ::= Value (':' VARNAME)?
2008/// DagArg ::= VARNAME
2009/// DagArgList ::= DagArg
2010/// DagArgList ::= DagArgList ',' DagArg
Matthias Braun1ddb78c2016-12-05 06:41:51 +00002011void TGParser::ParseDagArgList(
2012 SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
2013 Record *CurRec) {
Bob Wilson7248f862009-11-22 04:24:42 +00002014
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002015 while (true) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002016 // DagArg ::= VARNAME
2017 if (Lex.getCode() == tgtok::VarName) {
2018 // A missing value is treated like '?'.
Matthias Braunbb053162016-12-05 06:00:46 +00002019 StringInit *VarName = StringInit::get(Lex.getCurStrVal());
2020 Result.emplace_back(UnsetInit::get(), VarName);
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002021 Lex.Lex();
2022 } else {
2023 // DagArg ::= Value (':' VARNAME)?
2024 Init *Val = ParseValue(CurRec);
Matthias Braun1ddb78c2016-12-05 06:41:51 +00002025 if (!Val) {
2026 Result.clear();
2027 return;
2028 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002029
2030 // If the variable name is present, add it.
Matthias Braunbb053162016-12-05 06:00:46 +00002031 StringInit *VarName = nullptr;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002032 if (Lex.getCode() == tgtok::colon) {
2033 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
2034 TokError("expected variable name in dag literal");
Matthias Braun1ddb78c2016-12-05 06:41:51 +00002035 Result.clear();
2036 return;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002037 }
Matthias Braunbb053162016-12-05 06:00:46 +00002038 VarName = StringInit::get(Lex.getCurStrVal());
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002039 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002040 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002041
2042 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002043 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002044 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00002045 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002046 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002047}
2048
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002049/// ParseValueList - Parse a comma separated list of values, returning them as a
2050/// vector. Note that this always expects to be able to parse at least one
2051/// value. It returns an empty list if this is not possible.
2052///
2053/// ValueList ::= Value (',' Value)
2054///
Matthias Braunc66e7552016-12-05 06:41:54 +00002055void TGParser::ParseValueList(SmallVectorImpl<Init*> &Result, Record *CurRec,
2056 Record *ArgsRec, RecTy *EltTy) {
David Greene8618f952009-06-08 20:23:18 +00002057 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00002058 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00002059 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002060 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00002061 if (TArgs.empty()) {
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00002062 TokError("template argument provided to non-template class");
Matthias Braunc66e7552016-12-05 06:41:54 +00002063 Result.clear();
2064 return;
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00002065 }
David Greene8618f952009-06-08 20:23:18 +00002066 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00002067 if (!RV) {
2068 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
2069 << ")\n";
2070 }
David Greene8618f952009-06-08 20:23:18 +00002071 assert(RV && "Template argument record not found??");
2072 ItemType = RV->getType();
2073 ++ArgN;
2074 }
2075 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00002076 if (!Result.back()) {
2077 Result.clear();
2078 return;
2079 }
Bob Wilson7248f862009-11-22 04:24:42 +00002080
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002081 while (Lex.getCode() == tgtok::comma) {
2082 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00002083
Craig Topper011817a2014-04-09 04:50:04 +00002084 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002085 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00002086 if (ArgN >= TArgs.size()) {
2087 TokError("too many template arguments");
Matthias Braunc66e7552016-12-05 06:41:54 +00002088 Result.clear();
2089 return;
Bob Wilson7248f862009-11-22 04:24:42 +00002090 }
David Greene8618f952009-06-08 20:23:18 +00002091 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
2092 assert(RV && "Template argument record not found??");
2093 ItemType = RV->getType();
2094 ++ArgN;
2095 }
2096 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00002097 if (!Result.back()) {
2098 Result.clear();
2099 return;
2100 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002101 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002102}
2103
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002104/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
2105/// empty string on error. This can happen in a number of different context's,
2106/// including within a def or in the template args for a def (which which case
2107/// CurRec will be non-null) and within the template args for a multiclass (in
2108/// which case CurRec will be null, but CurMultiClass will be set). This can
2109/// also happen within a def that is within a multiclass, which will set both
2110/// CurRec and CurMultiClass.
2111///
2112/// Declaration ::= FIELD? Type ID ('=' Value)?
2113///
David Greenedb10e692011-10-19 13:02:42 +00002114Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002115 bool ParsingTemplateArgs) {
2116 // Read the field prefix if present.
2117 bool HasField = Lex.getCode() == tgtok::Field;
2118 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002119
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002120 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00002121 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00002122
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002123 if (Lex.getCode() != tgtok::Id) {
2124 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00002125 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002126 }
Bob Wilson7248f862009-11-22 04:24:42 +00002127
Chris Lattner526c8cb2009-06-21 03:39:35 +00002128 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00002129 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002130 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002131
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002132 if (ParsingTemplateArgs) {
Craig Toppera9642b42015-05-04 01:35:39 +00002133 if (CurRec)
David Greenedb10e692011-10-19 13:02:42 +00002134 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Craig Toppera9642b42015-05-04 01:35:39 +00002135 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002136 assert(CurMultiClass);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002137 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00002138 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
2139 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002140 }
Bob Wilson7248f862009-11-22 04:24:42 +00002141
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002142 // Add the value.
2143 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00002144 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00002145
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002146 // If a value is present, parse it.
2147 if (Lex.getCode() == tgtok::equal) {
2148 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002149 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00002150 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00002151 if (!Val ||
Craig Toppercfd81732016-01-04 03:15:08 +00002152 SetValue(CurRec, ValLoc, DeclName, None, Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00002153 // Return the name, even if an error is thrown. This is so that we can
2154 // continue to make some progress, even without the value having been
2155 // initialized.
2156 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002157 }
Bob Wilson7248f862009-11-22 04:24:42 +00002158
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002159 return DeclName;
2160}
2161
David Greenefb927af2012-02-22 16:09:41 +00002162/// ParseForeachDeclaration - Read a foreach declaration, returning
2163/// the name of the declared object or a NULL Init on error. Return
2164/// the name of the parsed initializer list through ForeachListName.
2165///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002166/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
2167/// ForeachDeclaration ::= ID '=' RangePiece
Nicolai Haehnle8aa9d582018-03-09 12:24:30 +00002168/// ForeachDeclaration ::= ID '=' Value
David Greenefb927af2012-02-22 16:09:41 +00002169///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002170VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00002171 if (Lex.getCode() != tgtok::Id) {
2172 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00002173 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00002174 }
2175
2176 Init *DeclName = StringInit::get(Lex.getCurStrVal());
2177 Lex.Lex();
2178
2179 // If a value is present, parse it.
2180 if (Lex.getCode() != tgtok::equal) {
2181 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00002182 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00002183 }
2184 Lex.Lex(); // Eat the '='
2185
Craig Topper011817a2014-04-09 04:50:04 +00002186 RecTy *IterType = nullptr;
Matthias Braunc66e7552016-12-05 06:41:54 +00002187 SmallVector<unsigned, 16> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00002188
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002189 switch (Lex.getCode()) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002190 case tgtok::IntVal: { // RangePiece.
2191 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00002192 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002193 break;
David Greenefb927af2012-02-22 16:09:41 +00002194 }
2195
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002196 case tgtok::l_brace: { // '{' RangeList '}'
2197 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00002198 ParseRangeList(Ranges);
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002199 if (Lex.getCode() != tgtok::r_brace) {
2200 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00002201 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002202 }
2203 Lex.Lex();
2204 break;
2205 }
Nicolai Haehnle8aa9d582018-03-09 12:24:30 +00002206
2207 default: {
2208 SMLoc ValueLoc = Lex.getLoc();
2209 Init *I = ParseValue(nullptr);
2210 if (!isa<ListInit>(I)) {
2211 std::string Type;
2212 if (TypedInit *TI = dyn_cast<TypedInit>(I))
2213 Type = (Twine("' of type '") + TI->getType()->getAsString()).str();
2214 Error(ValueLoc, "expected a list, got '" + I->getAsString() + Type + "'");
2215 return nullptr;
2216 }
2217 ForeachListValue = dyn_cast<ListInit>(I);
2218 IterType = ForeachListValue->getElementType();
2219 break;
2220 }
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002221 }
David Greenefb927af2012-02-22 16:09:41 +00002222
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002223 if (!Ranges.empty()) {
2224 assert(!IterType && "Type already initialized?");
2225 IterType = IntRecTy::get();
2226 std::vector<Init*> Values;
Craig Topper8eb764c2015-06-02 06:19:28 +00002227 for (unsigned R : Ranges)
2228 Values.push_back(IntInit::get(R));
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002229 ForeachListValue = ListInit::get(Values, IterType);
2230 }
2231
2232 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00002233 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002234
2235 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00002236}
2237
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002238/// ParseTemplateArgList - Read a template argument list, which is a non-empty
2239/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
2240/// template args for a def, which may or may not be in a multiclass. If null,
2241/// these are the template args for a multiclass.
2242///
2243/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00002244///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002245bool TGParser::ParseTemplateArgList(Record *CurRec) {
2246 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
2247 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00002248
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002249 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00002250
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002251 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00002252 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00002253 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002254 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002255
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002256 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00002257
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002258 while (Lex.getCode() == tgtok::comma) {
2259 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00002260
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002261 // Read the following declarations.
2262 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00002263 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002264 return true;
2265 TheRecToAddTo->addTemplateArg(TemplArg);
2266 }
Bob Wilson7248f862009-11-22 04:24:42 +00002267
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002268 if (Lex.getCode() != tgtok::greater)
2269 return TokError("expected '>' at end of template argument list");
2270 Lex.Lex(); // eat the '>'.
2271 return false;
2272}
2273
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002274/// ParseBodyItem - Parse a single item at within the body of a def or class.
2275///
2276/// BodyItem ::= Declaration ';'
2277/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
2278bool TGParser::ParseBodyItem(Record *CurRec) {
2279 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00002280 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002281 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002282
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002283 if (Lex.getCode() != tgtok::semi)
2284 return TokError("expected ';' after declaration");
2285 Lex.Lex();
2286 return false;
2287 }
2288
2289 // LET ID OptionalRangeList '=' Value ';'
2290 if (Lex.Lex() != tgtok::Id)
2291 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00002292
Chris Lattner526c8cb2009-06-21 03:39:35 +00002293 SMLoc IdLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00002294 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002295 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00002296
Matthias Braunc66e7552016-12-05 06:41:54 +00002297 SmallVector<unsigned, 16> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00002298 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002299 return true;
2300 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002301
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002302 if (Lex.getCode() != tgtok::equal)
2303 return TokError("expected '=' in let expression");
2304 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002305
David Greene8618f952009-06-08 20:23:18 +00002306 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00002307 if (!Field)
Matthias Braun215ff842016-12-05 07:35:13 +00002308 return TokError("Value '" + FieldName->getValue() + "' unknown!");
David Greene8618f952009-06-08 20:23:18 +00002309
2310 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00002311
David Greeneaf8ee2c2011-07-29 22:43:06 +00002312 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00002313 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002314
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002315 if (Lex.getCode() != tgtok::semi)
2316 return TokError("expected ';' after let expression");
2317 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002318
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002319 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
2320}
2321
2322/// ParseBody - Read the body of a class or def. Return true on error, false on
2323/// success.
2324///
2325/// Body ::= ';'
2326/// Body ::= '{' BodyList '}'
2327/// BodyList BodyItem*
2328///
2329bool TGParser::ParseBody(Record *CurRec) {
2330 // If this is a null definition, just eat the semi and return.
2331 if (Lex.getCode() == tgtok::semi) {
2332 Lex.Lex();
2333 return false;
2334 }
Bob Wilson7248f862009-11-22 04:24:42 +00002335
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002336 if (Lex.getCode() != tgtok::l_brace)
2337 return TokError("Expected ';' or '{' to start body");
2338 // Eat the '{'.
2339 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002340
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002341 while (Lex.getCode() != tgtok::r_brace)
2342 if (ParseBodyItem(CurRec))
2343 return true;
2344
2345 // Eat the '}'.
2346 Lex.Lex();
2347 return false;
2348}
2349
Sean Silvacb1a75e2013-01-09 04:49:14 +00002350/// \brief Apply the current let bindings to \a CurRec.
2351/// \returns true on error, false otherwise.
2352bool TGParser::ApplyLetStack(Record *CurRec) {
Matthias Braunc66e7552016-12-05 06:41:54 +00002353 for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
Craig Topper8eb764c2015-06-02 06:19:28 +00002354 for (LetRecord &LR : LetInfo)
2355 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
Sean Silvacb1a75e2013-01-09 04:49:14 +00002356 return true;
2357 return false;
2358}
2359
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002360/// ParseObjectBody - Parse the body of a def or class. This consists of an
2361/// optional ClassList followed by a Body. CurRec is the current def or class
2362/// that is being parsed.
2363///
2364/// ObjectBody ::= BaseClassList Body
2365/// BaseClassList ::= /*empty*/
2366/// BaseClassList ::= ':' BaseClassListNE
2367/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
2368///
2369bool TGParser::ParseObjectBody(Record *CurRec) {
2370 // If there is a baseclass list, read it.
2371 if (Lex.getCode() == tgtok::colon) {
2372 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002373
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002374 // Read all of the subclasses.
2375 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002376 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002377 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002378 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002379
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002380 // Add it.
2381 if (AddSubClass(CurRec, SubClass))
2382 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002383
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002384 if (Lex.getCode() != tgtok::comma) break;
2385 Lex.Lex(); // eat ','.
2386 SubClass = ParseSubClassReference(CurRec, false);
2387 }
2388 }
2389
Sean Silvacb1a75e2013-01-09 04:49:14 +00002390 if (ApplyLetStack(CurRec))
2391 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002392
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002393 return ParseBody(CurRec);
2394}
2395
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002396/// ParseDef - Parse and return a top level or multiclass def, return the record
2397/// corresponding to it. This returns null on error.
2398///
2399/// DefInst ::= DEF ObjectName ObjectBody
2400///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002401bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00002402 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002403 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00002404 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002405
2406 // Parse ObjectName and make a record for it.
Craig Topper84138712014-11-29 05:31:10 +00002407 std::unique_ptr<Record> CurRecOwner;
Jordan Roseabdd99b2013-01-10 18:50:05 +00002408 Init *Name = ParseObjectName(CurMultiClass);
2409 if (Name)
Craig Topper84138712014-11-29 05:31:10 +00002410 CurRecOwner = make_unique<Record>(Name, DefLoc, Records);
Jordan Roseabdd99b2013-01-10 18:50:05 +00002411 else
Nicolai Haehnle73355bc2018-03-06 13:48:54 +00002412 CurRecOwner = make_unique<Record>(Records.getNewAnonymousName(), DefLoc,
2413 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00002414 Record *CurRec = CurRecOwner.get(); // Keep a copy since we may release.
Bob Wilson7248f862009-11-22 04:24:42 +00002415
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002416 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002417 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002418
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002419 // Ensure redefinition doesn't happen.
Craig Topper84138712014-11-29 05:31:10 +00002420 if (Records.getDef(CurRec->getNameInitAsString()))
2421 return Error(DefLoc, "def '" + CurRec->getNameInitAsString()+
2422 "' already defined");
Craig Toppercdab2322014-11-29 05:52:51 +00002423 Records.addDef(std::move(CurRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00002424
2425 if (ParseObjectBody(CurRec))
2426 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002427 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002428 // Parse the body before adding this prototype to the DefPrototypes vector.
2429 // That way implicit definitions will be added to the DefPrototypes vector
2430 // before this object, instantiated prior to defs derived from this object,
2431 // and this available for indirect name resolution when defs derived from
2432 // this object are instantiated.
Craig Topper84138712014-11-29 05:31:10 +00002433 if (ParseObjectBody(CurRec))
Hal Finkela8c1f462014-01-02 20:47:09 +00002434 return true;
2435
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002436 // Otherwise, a def inside a multiclass, add it to the multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002437 for (const auto &Proto : CurMultiClass->DefPrototypes)
2438 if (Proto->getNameInit() == CurRec->getNameInit())
Craig Topper84138712014-11-29 05:31:10 +00002439 return Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
2440 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002441 CurMultiClass->DefPrototypes.push_back(std::move(CurRecOwner));
Anton Yartsev671dff12014-08-08 00:29:54 +00002442 } else if (ParseObjectBody(CurRec)) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002443 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002444 }
Bob Wilson7248f862009-11-22 04:24:42 +00002445
Nicolai Haehnle0f529882018-03-06 13:48:47 +00002446 if (!CurMultiClass) { // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002447 // See Record::setName(). This resolve step will see any new name
2448 // for the def that might have been created when resolving
2449 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002450 CurRec->resolveReferences();
Nicolai Haehnlefcd65252018-03-09 12:24:42 +00002451 if (Loops.empty()) {
Nicolai Haehnle0f529882018-03-06 13:48:47 +00002452 checkConcrete(*CurRec);
Nicolai Haehnlefcd65252018-03-09 12:24:42 +00002453 if (addToDefsets(*CurRec))
2454 return true;
2455 }
Nicolai Haehnle0f529882018-03-06 13:48:47 +00002456 }
Bob Wilson7248f862009-11-22 04:24:42 +00002457
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002458 // If ObjectBody has template arguments, it's an error.
2459 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002460
2461 if (CurMultiClass) {
2462 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002463 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
2464 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002465 assert(RV && "Template arg doesn't exist?");
2466 CurRec->addValue(*RV);
2467 }
2468 }
2469
Craig Toppera9642b42015-05-04 01:35:39 +00002470 if (ProcessForeachDefs(CurRec, DefLoc))
Craig Topper84138712014-11-29 05:31:10 +00002471 return Error(DefLoc, "Could not process loops for def" +
2472 CurRec->getNameInitAsString());
David Greenefb927af2012-02-22 16:09:41 +00002473
2474 return false;
2475}
2476
Nicolai Haehnlefcd65252018-03-09 12:24:42 +00002477bool TGParser::addToDefsets(Record &R) {
2478 for (DefsetRecord *Defset : Defsets) {
2479 DefInit *I = R.getDefInit();
2480 if (!I->getType()->typeIsA(Defset->EltTy)) {
2481 PrintError(R.getLoc(),
2482 Twine("adding record of incompatible type '") +
2483 I->getType()->getAsString() + "' to defset");
2484 PrintNote(Defset->Loc, "to this defset");
2485 return true;
2486 }
2487 Defset->Elements.push_back(I);
2488 }
2489 return false;
2490}
2491
2492/// ParseDefset - Parse a defset statement.
2493///
2494/// Defset ::= DEFSET Type Id '=' '{' ObjectList '}'
2495///
2496bool TGParser::ParseDefset() {
2497 assert(Lex.getCode() == tgtok::Defset);
2498 Lex.Lex(); // Eat the 'defset' token
2499
2500 DefsetRecord Defset;
2501 Defset.Loc = Lex.getLoc();
2502 RecTy *Type = ParseType();
2503 if (!Type)
2504 return true;
2505 if (!isa<ListRecTy>(Type))
2506 return Error(Defset.Loc, "expected list type");
2507 Defset.EltTy = cast<ListRecTy>(Type)->getElementType();
2508
2509 if (Lex.getCode() != tgtok::Id)
2510 return TokError("expected identifier");
2511 StringInit *DeclName = StringInit::get(Lex.getCurStrVal());
2512 if (Records.getGlobal(DeclName->getValue()))
2513 return TokError("def or global variable of this name already exists");
2514
2515 if (Lex.Lex() != tgtok::equal) // Eat the identifier
2516 return TokError("expected '='");
2517 if (Lex.Lex() != tgtok::l_brace) // Eat the '='
2518 return TokError("expected '{'");
2519 SMLoc BraceLoc = Lex.getLoc();
2520 Lex.Lex(); // Eat the '{'
2521
2522 Defsets.push_back(&Defset);
2523 bool Err = ParseObjectList(nullptr);
2524 Defsets.pop_back();
2525 if (Err)
2526 return true;
2527
2528 if (Lex.getCode() != tgtok::r_brace) {
2529 TokError("expected '}' at end of defset");
2530 return Error(BraceLoc, "to match this '{'");
2531 }
2532 Lex.Lex(); // Eat the '}'
2533
2534 Records.addExtraGlobal(DeclName->getValue(),
2535 ListInit::get(Defset.Elements, Defset.EltTy));
2536 return false;
2537}
2538
David Greenefb927af2012-02-22 16:09:41 +00002539/// ParseForeach - Parse a for statement. Return the record corresponding
2540/// to it. This returns true on error.
2541///
2542/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2543/// Foreach ::= FOREACH Declaration IN Object
2544///
2545bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2546 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2547 Lex.Lex(); // Eat the 'for' token.
2548
2549 // Make a temporary object to record items associated with the for
2550 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002551 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002552 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002553 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002554 return TokError("expected declaration in for");
2555
2556 if (Lex.getCode() != tgtok::In)
2557 return TokError("Unknown tok");
2558 Lex.Lex(); // Eat the in
2559
2560 // Create a loop object and remember it.
2561 Loops.push_back(ForeachLoop(IterName, ListValue));
2562
2563 if (Lex.getCode() != tgtok::l_brace) {
2564 // FOREACH Declaration IN Object
2565 if (ParseObject(CurMultiClass))
2566 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002567 } else {
David Greenefb927af2012-02-22 16:09:41 +00002568 SMLoc BraceLoc = Lex.getLoc();
2569 // Otherwise, this is a group foreach.
2570 Lex.Lex(); // eat the '{'.
2571
2572 // Parse the object list.
2573 if (ParseObjectList(CurMultiClass))
2574 return true;
2575
2576 if (Lex.getCode() != tgtok::r_brace) {
2577 TokError("expected '}' at end of foreach command");
2578 return Error(BraceLoc, "to match this '{'");
2579 }
2580 Lex.Lex(); // Eat the }
2581 }
2582
2583 // We've processed everything in this loop.
2584 Loops.pop_back();
2585
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002586 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002587}
2588
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002589/// ParseClass - Parse a tblgen class definition.
2590///
2591/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2592///
2593bool TGParser::ParseClass() {
2594 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2595 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002596
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002597 if (Lex.getCode() != tgtok::Id)
2598 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002599
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002600 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2601 if (CurRec) {
2602 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002603 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002604 !CurRec->getSuperClasses().empty() ||
2605 !CurRec->getTemplateArgs().empty())
Craig Topper85c07002015-04-30 05:54:22 +00002606 return TokError("Class '" + CurRec->getNameInitAsString() +
2607 "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002608 } else {
2609 // If this is the first reference to this class, create and add it.
Hans Wennborgffbbd532014-11-30 00:31:49 +00002610 auto NewRec =
2611 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
Craig Toppercdab2322014-11-29 05:52:51 +00002612 CurRec = NewRec.get();
2613 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002614 }
2615 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002616
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002617 // If there are template args, parse them.
2618 if (Lex.getCode() == tgtok::less)
2619 if (ParseTemplateArgList(CurRec))
2620 return true;
2621
2622 // Finally, parse the object body.
2623 return ParseObjectBody(CurRec);
2624}
2625
2626/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2627/// of LetRecords.
2628///
2629/// LetList ::= LetItem (',' LetItem)*
2630/// LetItem ::= ID OptionalRangeList '=' Value
2631///
Matthias Braunc66e7552016-12-05 06:41:54 +00002632void TGParser::ParseLetList(SmallVectorImpl<LetRecord> &Result) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002633 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002634 if (Lex.getCode() != tgtok::Id) {
2635 TokError("expected identifier in let definition");
Matthias Braunc66e7552016-12-05 06:41:54 +00002636 Result.clear();
2637 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002638 }
Matthias Braunc66e7552016-12-05 06:41:54 +00002639
Matthias Braun215ff842016-12-05 07:35:13 +00002640 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattner526c8cb2009-06-21 03:39:35 +00002641 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002642 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002643
2644 // Check for an optional RangeList.
Matthias Braunc66e7552016-12-05 06:41:54 +00002645 SmallVector<unsigned, 16> Bits;
2646 if (ParseOptionalRangeList(Bits)) {
2647 Result.clear();
2648 return;
2649 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002650 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002651
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002652 if (Lex.getCode() != tgtok::equal) {
2653 TokError("expected '=' in let expression");
Matthias Braunc66e7552016-12-05 06:41:54 +00002654 Result.clear();
2655 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002656 }
2657 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002658
Craig Topper011817a2014-04-09 04:50:04 +00002659 Init *Val = ParseValue(nullptr);
Matthias Braunc66e7552016-12-05 06:41:54 +00002660 if (!Val) {
2661 Result.clear();
2662 return;
2663 }
Bob Wilson7248f862009-11-22 04:24:42 +00002664
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002665 // Now that we have everything, add the record.
Matthias Braun215ff842016-12-05 07:35:13 +00002666 Result.emplace_back(Name, Bits, Val, NameLoc);
Bob Wilson7248f862009-11-22 04:24:42 +00002667
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002668 if (Lex.getCode() != tgtok::comma)
Matthias Braunc66e7552016-12-05 06:41:54 +00002669 return;
Bob Wilson7248f862009-11-22 04:24:42 +00002670 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002671 }
2672}
2673
2674/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002675/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002676///
2677/// Object ::= LET LetList IN '{' ObjectList '}'
2678/// Object ::= LET LetList IN Object
2679///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002680bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002681 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2682 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002683
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002684 // Add this entry to the let stack.
Matthias Braunc66e7552016-12-05 06:41:54 +00002685 SmallVector<LetRecord, 8> LetInfo;
2686 ParseLetList(LetInfo);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002687 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002688 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002689
2690 if (Lex.getCode() != tgtok::In)
2691 return TokError("expected 'in' at end of top-level 'let'");
2692 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002693
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002694 // If this is a scalar let, just handle it now
2695 if (Lex.getCode() != tgtok::l_brace) {
2696 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002697 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002698 return true;
2699 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002700 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002701 // Otherwise, this is a group let.
2702 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002703
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002704 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002705 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002706 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002707
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002708 if (Lex.getCode() != tgtok::r_brace) {
2709 TokError("expected '}' at end of top level let command");
2710 return Error(BraceLoc, "to match this '{'");
2711 }
2712 Lex.Lex();
2713 }
Bob Wilson7248f862009-11-22 04:24:42 +00002714
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002715 // Outside this let scope, this let block is not active.
2716 LetStack.pop_back();
2717 return false;
2718}
2719
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002720/// ParseMultiClass - Parse a multiclass definition.
2721///
Bob Wilson3d948162009-04-28 19:41:44 +00002722/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002723/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2724/// MultiClassObject ::= DefInst
2725/// MultiClassObject ::= MultiClassInst
2726/// MultiClassObject ::= DefMInst
2727/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2728/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002729///
2730bool TGParser::ParseMultiClass() {
2731 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2732 Lex.Lex(); // Eat the multiclass token.
2733
2734 if (Lex.getCode() != tgtok::Id)
2735 return TokError("expected identifier after multiclass for name");
2736 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002737
Craig Topper7adf2bf2014-12-11 05:25:30 +00002738 auto Result =
2739 MultiClasses.insert(std::make_pair(Name,
2740 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2741
2742 if (!Result.second)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002743 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002744
Craig Topper7adf2bf2014-12-11 05:25:30 +00002745 CurMultiClass = Result.first->second.get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002746 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002747
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002748 // If there are template args, parse them.
2749 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002750 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002751 return true;
2752
David Greene7049e792009-04-24 16:55:41 +00002753 bool inherits = false;
2754
David Greene753ed8f2009-04-22 16:42:54 +00002755 // If there are submulticlasses, parse them.
2756 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002757 inherits = true;
2758
David Greene753ed8f2009-04-22 16:42:54 +00002759 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002760
David Greene753ed8f2009-04-22 16:42:54 +00002761 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002762 SubMultiClassReference SubMultiClass =
2763 ParseSubMultiClassReference(CurMultiClass);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002764 while (true) {
David Greene753ed8f2009-04-22 16:42:54 +00002765 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002766 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002767
David Greene753ed8f2009-04-22 16:42:54 +00002768 // Add it.
2769 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2770 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002771
David Greene753ed8f2009-04-22 16:42:54 +00002772 if (Lex.getCode() != tgtok::comma) break;
2773 Lex.Lex(); // eat ','.
2774 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2775 }
2776 }
2777
David Greene7049e792009-04-24 16:55:41 +00002778 if (Lex.getCode() != tgtok::l_brace) {
2779 if (!inherits)
2780 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002781 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002782 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002783 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002784 } else {
David Greene7049e792009-04-24 16:55:41 +00002785 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2786 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002787
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002788 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002789 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002790 default:
Nicolai Haehnlefcd65252018-03-09 12:24:42 +00002791 return TokError("expected 'let', 'def', 'defm' or 'foreach' in "
2792 "multiclass body");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002793 case tgtok::Let:
2794 case tgtok::Def:
2795 case tgtok::Defm:
2796 case tgtok::Foreach:
2797 if (ParseObject(CurMultiClass))
2798 return true;
2799 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002800 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002801 }
David Greene7049e792009-04-24 16:55:41 +00002802 Lex.Lex(); // eat the '}'.
2803 }
Bob Wilson7248f862009-11-22 04:24:42 +00002804
Craig Topper011817a2014-04-09 04:50:04 +00002805 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002806 return false;
2807}
2808
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002809Record *TGParser::InstantiateMulticlassDef(MultiClass &MC, Record *DefProto,
2810 Init *&DefmPrefix,
2811 SMRange DefmPrefixRange,
2812 ArrayRef<Init *> TArgs,
Matthias Braunc66e7552016-12-05 06:41:54 +00002813 ArrayRef<Init *> TemplateVals) {
David Greene5d5d88c2011-10-19 13:04:31 +00002814 // We need to preserve DefProto so it can be reused for later
2815 // instantiations, so create a new Record to inherit from it.
2816
David Greenedb445972011-10-05 22:42:07 +00002817 // Add in the defm name. If the defm prefix is empty, give each
2818 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2819 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2820 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002821
Jordan Roseabdd99b2013-01-10 18:50:05 +00002822 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002823 if (!DefmPrefix) {
Nicolai Haehnle73355bc2018-03-06 13:48:54 +00002824 DefmPrefix = Records.getNewAnonymousName();
Jordan Roseabdd99b2013-01-10 18:50:05 +00002825 IsAnonymous = true;
2826 }
David Greene5d5d88c2011-10-19 13:04:31 +00002827
2828 Init *DefName = DefProto->getNameInit();
Sean Silvafb509ed2012-10-10 20:24:43 +00002829 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002830
Craig Topper011817a2014-04-09 04:50:04 +00002831 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002832 // We have a fully expanded string so there are no operators to
2833 // resolve. We should concatenate the given prefix and name.
Nicolai Haehnlec47fe122018-03-19 14:13:37 +00002834 DefName = BinOpInit::get(
2835 BinOpInit::STRCONCAT,
2836 UnOpInit::get(UnOpInit::CAST, DefmPrefix, StringRecTy::get())
2837 ->Fold(DefProto),
2838 DefName, StringRecTy::get())
2839 ->Fold(DefProto);
David Greene5d5d88c2011-10-19 13:04:31 +00002840 }
David Greene5d5d88c2011-10-19 13:04:31 +00002841
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002842 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002843 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002844 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Craig Topper84138712014-11-29 05:31:10 +00002845 auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002846
2847 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002848 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002849 Ref.Rec = DefProto;
Craig Topper84138712014-11-29 05:31:10 +00002850 AddSubClass(CurRec.get(), Ref);
David Greenedb445972011-10-05 22:42:07 +00002851
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002852 // Set the value for NAME. We don't resolve references to it 'til later,
2853 // though, so that uses in nested multiclass names don't get
2854 // confused.
Matthias Braun215ff842016-12-05 07:35:13 +00002855 if (SetValue(CurRec.get(), Ref.RefRange.Start, StringInit::get("NAME"), None,
2856 DefmPrefix, /*AllowSelfAssignment*/true)) {
Craig Topper85c07002015-04-30 05:54:22 +00002857 Error(DefmPrefixRange.Start, "Could not resolve " +
2858 CurRec->getNameInitAsString() + ":NAME to '" +
2859 DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002860 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002861 }
David Greene8bf0d722011-10-19 13:04:35 +00002862
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002863 // If the DefNameString didn't resolve, we probably have a reference to
2864 // NAME and need to replace it. We need to do at least this much greedily,
2865 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002866 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002867 RecordVal *DefNameRV = CurRec->getValue("NAME");
2868 CurRec->resolveReferencesTo(DefNameRV);
2869 }
2870
2871 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002872 // Now that we're at the top level, resolve all NAME references
2873 // in the resultant defs that weren't in the def names themselves.
2874 RecordVal *DefNameRV = CurRec->getValue("NAME");
2875 CurRec->resolveReferencesTo(DefNameRV);
2876
Hal Finkeld2497362015-05-21 04:32:56 +00002877 // Check if the name is a complex pattern.
2878 // If so, resolve it.
2879 DefName = CurRec->getNameInit();
2880 DefNameString = dyn_cast<StringInit>(DefName);
2881
2882 // OK the pattern is more complex than simply using NAME.
2883 // Let's use the heavy weaponery.
2884 if (!DefNameString) {
2885 ResolveMulticlassDefArgs(MC, CurRec.get(), DefmPrefixRange.Start,
2886 Lex.getLoc(), TArgs, TemplateVals,
2887 false/*Delete args*/);
2888 DefName = CurRec->getNameInit();
2889 DefNameString = dyn_cast<StringInit>(DefName);
2890
Nicolai Haehnledfda9dc2018-03-06 13:48:39 +00002891 if (!DefNameString) {
Hal Finkeld2497362015-05-21 04:32:56 +00002892 DefName = DefName->convertInitializerTo(StringRecTy::get());
Nicolai Haehnledfda9dc2018-03-06 13:48:39 +00002893 DefNameString = dyn_cast<StringInit>(DefName);
2894 }
Hal Finkeld2497362015-05-21 04:32:56 +00002895
Hal Finkeld2497362015-05-21 04:32:56 +00002896 if (!DefNameString) {
2897 PrintFatalError(CurRec->getLoc()[CurRec->getLoc().size() - 1],
2898 DefName->getAsUnquotedString() + " is not a string.");
2899 return nullptr;
2900 }
2901
2902 CurRec->setName(DefName);
2903 }
2904
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002905 // Now that NAME references are resolved and we're at the top level of
2906 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002907 // currently in a multiclass, it means this defm appears inside a
2908 // multiclass and its name won't be fully resolvable until we see
Hal Finkeld2497362015-05-21 04:32:56 +00002909 // the top-level defm. Therefore, we don't add this to the
2910 // RecordKeeper at this point. If we did we could get duplicate
David Greene8bf0d722011-10-19 13:04:35 +00002911 // defs as more than one probably refers to NAME or some other
2912 // common internal placeholder.
2913
2914 // Ensure redefinition doesn't happen.
2915 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002916 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
Nicolai Haehnled34f6842018-03-06 13:49:16 +00002917 "' already defined, instantiating defm with subdef '" +
David Greene8bf0d722011-10-19 13:04:35 +00002918 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002919 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002920 }
2921
Craig Topper84138712014-11-29 05:31:10 +00002922 Record *CurRecSave = CurRec.get(); // Keep a copy before we release.
Craig Toppercdab2322014-11-29 05:52:51 +00002923 Records.addDef(std::move(CurRec));
Craig Topper84138712014-11-29 05:31:10 +00002924 return CurRecSave;
David Greene8bf0d722011-10-19 13:04:35 +00002925 }
2926
Craig Topper84138712014-11-29 05:31:10 +00002927 // FIXME This is bad but the ownership transfer to caller is pretty messy.
2928 // The unique_ptr in this function at least protects the exits above.
2929 return CurRec.release();
David Greenedb445972011-10-05 22:42:07 +00002930}
2931
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002932bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC, Record *CurRec,
2933 SMLoc DefmPrefixLoc, SMLoc SubClassLoc,
2934 ArrayRef<Init *> TArgs,
Matthias Braunc66e7552016-12-05 06:41:54 +00002935 ArrayRef<Init *> TemplateVals,
David Greenedb445972011-10-05 22:42:07 +00002936 bool DeleteArgs) {
Nicolai Haehnle7d697852018-03-05 15:21:19 +00002937 // Set all template arguments to the specified value or leave them as the
2938 // default if necessary, then resolve them all simultaneously.
2939 MapResolver R(CurRec);
2940
David Greenedb445972011-10-05 22:42:07 +00002941 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2942 // Check if a value is specified for this temp-arg.
2943 if (i < TemplateVals.size()) {
Craig Toppercfd81732016-01-04 03:15:08 +00002944 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], None, TemplateVals[i]))
David Greenedb445972011-10-05 22:42:07 +00002945 return true;
David Greenedb445972011-10-05 22:42:07 +00002946 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Craig Topper85c07002015-04-30 05:54:22 +00002947 return Error(SubClassLoc, "value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00002948 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +00002949 ") of multiclassclass '" + MC.Rec.getNameInitAsString() +
2950 "'");
David Greenedb445972011-10-05 22:42:07 +00002951 }
Nicolai Haehnle7d697852018-03-05 15:21:19 +00002952
2953 R.set(TArgs[i], CurRec->getValue(TArgs[i])->getValue());
2954
2955 if (DeleteArgs)
2956 CurRec->removeValue(TArgs[i]);
David Greenedb445972011-10-05 22:42:07 +00002957 }
Nicolai Haehnle7d697852018-03-05 15:21:19 +00002958
2959 CurRec->resolveReferences(R);
2960
David Greenedb445972011-10-05 22:42:07 +00002961 return false;
2962}
2963
2964bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2965 Record *CurRec,
2966 Record *DefProto,
2967 SMLoc DefmPrefixLoc) {
2968 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002969 if (ApplyLetStack(CurRec))
2970 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002971
David Greenedb445972011-10-05 22:42:07 +00002972 // Don't create a top level definition for defm inside multiclasses,
2973 // instead, only update the prototypes and bind the template args
2974 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002975 if (!CurMultiClass)
2976 return false;
Craig Toppereb4d7c62015-04-29 04:43:36 +00002977 for (const auto &Proto : CurMultiClass->DefPrototypes)
2978 if (Proto->getNameInit() == CurRec->getNameInit())
Sean Silvacc951b22013-01-09 05:28:12 +00002979 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2980 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002981 CurMultiClass->DefPrototypes.push_back(std::unique_ptr<Record>(CurRec));
David Greenedb445972011-10-05 22:42:07 +00002982
Sean Silvacc951b22013-01-09 05:28:12 +00002983 // Copy the template arguments for the multiclass into the new def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002984 for (Init * TA : CurMultiClass->Rec.getTemplateArgs()) {
2985 const RecordVal *RV = CurMultiClass->Rec.getValue(TA);
Sean Silvacc951b22013-01-09 05:28:12 +00002986 assert(RV && "Template arg doesn't exist?");
2987 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002988 }
2989
2990 return false;
2991}
2992
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002993/// ParseDefm - Parse the instantiation of a multiclass.
2994///
2995/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2996///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002997bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002998 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002999 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00003000 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00003001
Craig Topperb21afc62013-01-07 05:09:33 +00003002 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00003003 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00003004 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00003005
Jordan Rosef12e8a92013-01-10 18:50:11 +00003006 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00003007 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00003008 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00003009
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00003010 // Keep track of the new generated record definitions.
3011 std::vector<Record*> NewRecDefs;
3012
3013 // This record also inherits from a regular class (non-multiclass)?
3014 bool InheritFromClass = false;
3015
Chris Lattnerf4127dd2007-11-22 20:49:04 +00003016 // eat the colon.
3017 Lex.Lex();
3018
Chris Lattner526c8cb2009-06-21 03:39:35 +00003019 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00003020 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00003021
Eugene Zelenko33d7b762016-08-23 17:14:32 +00003022 while (true) {
Craig Topper011817a2014-04-09 04:50:04 +00003023 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00003024
3025 // To instantiate a multiclass, we need to first get the multiclass, then
3026 // instantiate each def contained in the multiclass with the SubClassRef
3027 // template parameters.
Craig Topper7adf2bf2014-12-11 05:25:30 +00003028 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper4ca40012014-11-30 01:20:17 +00003029 assert(MC && "Didn't lookup multiclass correctly?");
Matthias Braunc66e7552016-12-05 06:41:54 +00003030 ArrayRef<Init*> TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00003031
3032 // Verify that the correct number of template arguments were specified.
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00003033 ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00003034 if (TArgs.size() < TemplateVals.size())
3035 return Error(SubClassLoc,
3036 "more template args specified than multiclass expects");
3037
3038 // Loop over all the def's in the multiclass, instantiating each one.
Craig Toppereb4d7c62015-04-29 04:43:36 +00003039 for (const std::unique_ptr<Record> &DefProto : MC->DefPrototypes) {
Hal Finkeld2497362015-05-21 04:32:56 +00003040 // The record name construction goes as follow:
3041 // - If the def name is a string, prepend the prefix.
3042 // - If the def name is a more complex pattern, use that pattern.
Craig Topper7f36be92016-02-26 06:50:27 +00003043 // As a result, the record is instantiated before resolving
Hal Finkeld2497362015-05-21 04:32:56 +00003044 // arguments, as it would make its name a string.
Craig Toppereb4d7c62015-04-29 04:43:36 +00003045 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto.get(), DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00003046 SMRange(DefmLoc,
Hal Finkeld2497362015-05-21 04:32:56 +00003047 DefmPrefixEndLoc),
3048 TArgs, TemplateVals);
Jim Grosbachbccc4c12011-12-02 18:33:03 +00003049 if (!CurRec)
3050 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00003051
Craig Topper7f36be92016-02-26 06:50:27 +00003052 // Now that the record is instantiated, we can resolve arguments.
Jordan Rosef12e8a92013-01-10 18:50:11 +00003053 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00003054 TArgs, TemplateVals, true/*Delete args*/))
3055 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00003056
Craig Toppereb4d7c62015-04-29 04:43:36 +00003057 if (ResolveMulticlassDef(*MC, CurRec, DefProto.get(), DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00003058 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00003059
3060 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00003061 }
3062
David Greenedb445972011-10-05 22:42:07 +00003063
David Greenef00919a2009-04-22 22:17:51 +00003064 if (Lex.getCode() != tgtok::comma) break;
3065 Lex.Lex(); // eat ','.
3066
Craig Topper998a39a2013-08-20 04:22:09 +00003067 if (Lex.getCode() != tgtok::Id)
3068 return TokError("expected identifier");
3069
David Greenef00919a2009-04-22 22:17:51 +00003070 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00003071
3072 // A defm can inherit from regular classes (non-multiclass) as
3073 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00003074 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00003075
3076 if (InheritFromClass)
3077 break;
3078
Craig Topper011817a2014-04-09 04:50:04 +00003079 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00003080 }
3081
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00003082 if (InheritFromClass) {
3083 // Process all the classes to inherit as if they were part of a
3084 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00003085 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00003086 while (true) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00003087 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00003088 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00003089
3090 // Get the expanded definition prototypes and teach them about
3091 // the record values the current class to inherit has
Craig Toppereb4d7c62015-04-29 04:43:36 +00003092 for (Record *CurRec : NewRecDefs) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00003093 // Add it.
3094 if (AddSubClass(CurRec, SubClass))
3095 return true;
3096
Sean Silvacb1a75e2013-01-09 04:49:14 +00003097 if (ApplyLetStack(CurRec))
3098 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00003099 }
3100
3101 if (Lex.getCode() != tgtok::comma) break;
3102 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00003103 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00003104 }
3105 }
3106
Nicolai Haehnle0f529882018-03-06 13:48:47 +00003107 if (!CurMultiClass) {
3108 for (Record *CurRec : NewRecDefs) {
David Greene50c09122011-08-10 18:27:46 +00003109 // See Record::setName(). This resolve step will see any new
3110 // name for the def that might have been created when resolving
3111 // inheritance, values and arguments above.
Craig Toppereb4d7c62015-04-29 04:43:36 +00003112 CurRec->resolveReferences();
Nicolai Haehnle0f529882018-03-06 13:48:47 +00003113 checkConcrete(*CurRec);
Nicolai Haehnlefcd65252018-03-09 12:24:42 +00003114 if (addToDefsets(*CurRec))
3115 return true;
Nicolai Haehnle0f529882018-03-06 13:48:47 +00003116 }
3117 }
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00003118
Chris Lattnerf4127dd2007-11-22 20:49:04 +00003119 if (Lex.getCode() != tgtok::semi)
3120 return TokError("expected ';' at end of defm");
3121 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00003122
Chris Lattnerf4127dd2007-11-22 20:49:04 +00003123 return false;
3124}
3125
3126/// ParseObject
3127/// Object ::= ClassInst
3128/// Object ::= DefInst
3129/// Object ::= MultiClassInst
3130/// Object ::= DefMInst
3131/// Object ::= LETCommand '{' ObjectList '}'
3132/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00003133bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00003134 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00003135 default:
Nicolai Haehnlefcd65252018-03-09 12:24:42 +00003136 return TokError("Expected class, def, defm, defset, multiclass, let or "
3137 "foreach");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00003138 case tgtok::Let: return ParseTopLevelLet(MC);
3139 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00003140 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00003141 case tgtok::Defm: return ParseDefm(MC);
Nicolai Haehnlefcd65252018-03-09 12:24:42 +00003142 case tgtok::Defset:
3143 if (MC)
3144 return TokError("defset is not allowed inside multiclass");
3145 return ParseDefset();
Nicolai Haehnlea511ddd2018-03-14 11:01:01 +00003146 case tgtok::Class:
3147 if (MC)
3148 return TokError("class is not allowed inside multiclass");
3149 if (!Loops.empty())
3150 return TokError("class is not allowed inside foreach loop");
3151 return ParseClass();
3152 case tgtok::MultiClass:
3153 if (!Loops.empty())
3154 return TokError("multiclass is not allowed inside foreach loop");
3155 return ParseMultiClass();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00003156 }
3157}
3158
3159/// ParseObjectList
3160/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00003161bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00003162 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00003163 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00003164 return true;
3165 }
3166 return false;
3167}
3168
Chris Lattnerf4127dd2007-11-22 20:49:04 +00003169bool TGParser::ParseFile() {
3170 Lex.Lex(); // Prime the lexer.
3171 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00003172
Chris Lattnerf4127dd2007-11-22 20:49:04 +00003173 // If we have unread input at the end of the file, report it.
3174 if (Lex.getCode() == tgtok::Eof)
3175 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00003176
Chris Lattnerf4127dd2007-11-22 20:49:04 +00003177 return TokError("Unexpected input at top level");
3178}