blob: fcde2d85618f0b59843f1dbaadefa979264c15f2 [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
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000340/// Add a record that results from 'def' or 'defm', after template arguments
341/// and the external let stack have been resolved.
342///
343/// Apply foreach loops, resolve internal variable references, and add to the
344/// current multi class or the global record keeper as appropriate.
345bool TGParser::addDef(std::unique_ptr<Record> Rec, Init *DefmName) {
David Greenefb927af2012-02-22 16:09:41 +0000346 IterSet IterVals;
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000347
348 if (Loops.empty())
349 return addDefOne(std::move(Rec), DefmName, IterVals);
350
351 return addDefForeach(Rec.get(), DefmName, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000352}
353
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000354/// Recursive helper function for addDef/addDefOne to resolve references to
355/// foreach variables.
356bool TGParser::addDefForeach(Record *Rec, Init *DefmName, IterSet &IterVals) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000357 if (IterVals.size() != Loops.size()) {
358 assert(IterVals.size() < Loops.size());
359 ForeachLoop &CurLoop = Loops[IterVals.size()];
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000360 ListInit *List = CurLoop.ListValue;
David Greenefb927af2012-02-22 16:09:41 +0000361
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000362 // Process each value.
Craig Topper664f6a02015-06-02 04:15:57 +0000363 for (unsigned i = 0; i < List->size(); ++i) {
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000364 IterVals.push_back(IterRecord(CurLoop.IterVar, List->getElement(i)));
365 if (addDefForeach(Rec, DefmName, IterVals))
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000366 return true;
367 IterVals.pop_back();
368 }
369 return false;
370 }
371
372 // This is the bottom of the recursion. We have all of the iterator values
373 // for this point in the iteration space. Instantiate a new record to
374 // reflect this combination of values.
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000375 auto IterRec = make_unique<Record>(*Rec);
376 return addDefOne(std::move(IterRec), DefmName, IterVals);
377}
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000378
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000379/// After resolving foreach loops, add the record as a prototype to the
380/// current multiclass, or resolve fully and add to the record keeper.
381bool TGParser::addDefOne(std::unique_ptr<Record> Rec, Init *DefmName,
382 IterSet &IterVals) {
383 MapResolver R(Rec.get());
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000384
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000385 for (IterRecord &IR : IterVals)
386 R.set(IR.IterVar->getNameInit(), IR.IterValue);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000387
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000388 Rec->resolveReferences(R);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000389
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000390 if (CurMultiClass) {
391 for (const auto &Proto : CurMultiClass->DefPrototypes) {
392 if (Proto->getNameInit() == Rec->getNameInit()) {
393 if (!Rec->isAnonymous()) {
394 PrintError(Rec->getLoc(),
395 Twine("def '") + Rec->getNameInitAsString() +
396 "' already defined in this multiclass!");
397 PrintNote(Proto->getLoc(), "location of previous definition");
398 return true;
399 }
400 Rec->setName(Records.getNewAnonymousName());
401 break;
402 }
403 }
404 CurMultiClass->DefPrototypes.emplace_back(std::move(Rec));
405 return false;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000406 }
407
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000408 // Name construction is an incoherent mess. Unfortunately, existing .td
409 // files rely on pretty much all the quirks and implementation details of
410 // this.
411 if (DefmName) {
412 MapResolver R(Rec.get());
413 R.set(StringInit::get("NAME"), DefmName);
414 Rec->resolveReferences(R);
David Greenefb927af2012-02-22 16:09:41 +0000415 }
416
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000417 if (Record *Prev = Records.getDef(Rec->getNameInitAsString())) {
418 if (!Rec->isAnonymous()) {
419 PrintError(Rec->getLoc(),
420 "def already exists: " + Rec->getNameInitAsString());
421 PrintNote(Prev->getLoc(), "location of previous definition");
422 return true;
423 }
424 Rec->setName(Records.getNewAnonymousName());
425 }
426
427 Rec->resolveReferences();
428 checkConcrete(*Rec);
429
430 // If ObjectBody has template arguments, it's an error.
431 assert(Rec->getTemplateArgs().empty() && "How'd this get template args?");
432
433 for (DefsetRecord *Defset : Defsets) {
434 DefInit *I = Rec->getDefInit();
435 if (!I->getType()->typeIsA(Defset->EltTy)) {
436 PrintError(Rec->getLoc(), Twine("adding record of incompatible type '") +
437 I->getType()->getAsString() +
438 "' to defset");
439 PrintNote(Defset->Loc, "location of defset declaration");
440 return true;
441 }
442 Defset->Elements.push_back(I);
443 }
444
445 Records.addDef(std::move(Rec));
David Greenefb927af2012-02-22 16:09:41 +0000446 return false;
447}
448
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000449//===----------------------------------------------------------------------===//
450// Parser Code
451//===----------------------------------------------------------------------===//
452
453/// isObjectStart - Return true if this is a valid first token for an Object.
454static bool isObjectStart(tgtok::TokKind K) {
Nicolai Haehnlefcd65252018-03-09 12:24:42 +0000455 return K == tgtok::Class || K == tgtok::Def || K == tgtok::Defm ||
456 K == tgtok::Let || K == tgtok::MultiClass || K == tgtok::Foreach ||
457 K == tgtok::Defset;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000458}
459
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000460/// ParseObjectName - If a valid object name is specified, return it. If no
461/// name is specified, return the unset initializer. Return nullptr on parse
462/// error.
David Greene2affd672011-10-19 13:04:29 +0000463/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000464/// ObjectName ::= /*empty*/
465///
David Greene2affd672011-10-19 13:04:29 +0000466Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
467 switch (Lex.getCode()) {
468 case tgtok::colon:
469 case tgtok::semi:
470 case tgtok::l_brace:
471 // These are all of the tokens that can begin an object body.
472 // Some of these can also begin values but we disallow those cases
473 // because they are unlikely to be useful.
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000474 return UnsetInit::get();
David Greene2affd672011-10-19 13:04:29 +0000475 default:
476 break;
477 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000478
Craig Topper011817a2014-04-09 04:50:04 +0000479 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000480 if (CurMultiClass)
481 CurRec = &CurMultiClass->Rec;
482
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000483 return ParseValue(CurRec, StringRecTy::get(), ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000484}
485
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000486/// ParseClassID - Parse and resolve a reference to a class name. This returns
487/// null on error.
488///
489/// ClassID ::= ID
490///
491Record *TGParser::ParseClassID() {
492 if (Lex.getCode() != tgtok::Id) {
493 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000494 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000495 }
Bob Wilson7248f862009-11-22 04:24:42 +0000496
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000497 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000498 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000499 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000500
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000501 Lex.Lex();
502 return Result;
503}
504
Bob Wilson3d948162009-04-28 19:41:44 +0000505/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
506/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000507///
508/// MultiClassID ::= ID
509///
510MultiClass *TGParser::ParseMultiClassID() {
511 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000512 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000513 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000514 }
Bob Wilson3d948162009-04-28 19:41:44 +0000515
Craig Topper7adf2bf2014-12-11 05:25:30 +0000516 MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
Craig Topper4ca40012014-11-30 01:20:17 +0000517 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000518 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000519
David Greene753ed8f2009-04-22 16:42:54 +0000520 Lex.Lex();
Craig Topper4ca40012014-11-30 01:20:17 +0000521 return Result;
David Greene753ed8f2009-04-22 16:42:54 +0000522}
523
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000524/// ParseSubClassReference - Parse a reference to a subclass or to a templated
525/// subclass. This returns a SubClassRefTy with a null Record* on error.
526///
527/// SubClassRef ::= ClassID
528/// SubClassRef ::= ClassID '<' ValueList '>'
529///
530SubClassReference TGParser::
531ParseSubClassReference(Record *CurRec, bool isDefm) {
532 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000533 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000534
Sean Silva0657b402013-01-09 02:17:14 +0000535 if (isDefm) {
536 if (MultiClass *MC = ParseMultiClassID())
537 Result.Rec = &MC->Rec;
538 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000539 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000540 }
Craig Topper011817a2014-04-09 04:50:04 +0000541 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000542
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000543 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000544 if (Lex.getCode() != tgtok::less) {
545 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000546 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000547 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000548 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000549
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000550 if (Lex.getCode() == tgtok::greater) {
551 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000552 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000553 return Result;
554 }
Bob Wilson7248f862009-11-22 04:24:42 +0000555
Matthias Braunc66e7552016-12-05 06:41:54 +0000556 ParseValueList(Result.TemplateArgs, CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000557 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000558 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000559 return Result;
560 }
Bob Wilson7248f862009-11-22 04:24:42 +0000561
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000562 if (Lex.getCode() != tgtok::greater) {
563 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000564 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000565 return Result;
566 }
567 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000568 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000569
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000570 return Result;
571}
572
Bob Wilson3d948162009-04-28 19:41:44 +0000573/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
574/// templated submulticlass. This returns a SubMultiClassRefTy with a null
575/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000576///
577/// SubMultiClassRef ::= MultiClassID
578/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
579///
580SubMultiClassReference TGParser::
581ParseSubMultiClassReference(MultiClass *CurMC) {
582 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000583 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000584
David Greene753ed8f2009-04-22 16:42:54 +0000585 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000586 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000587
David Greene753ed8f2009-04-22 16:42:54 +0000588 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000589 if (Lex.getCode() != tgtok::less) {
590 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000591 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000592 }
David Greene753ed8f2009-04-22 16:42:54 +0000593 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000594
David Greene753ed8f2009-04-22 16:42:54 +0000595 if (Lex.getCode() == tgtok::greater) {
596 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000597 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000598 return Result;
599 }
Bob Wilson3d948162009-04-28 19:41:44 +0000600
Matthias Braunc66e7552016-12-05 06:41:54 +0000601 ParseValueList(Result.TemplateArgs, &CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000602 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000603 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000604 return Result;
605 }
Bob Wilson3d948162009-04-28 19:41:44 +0000606
David Greene753ed8f2009-04-22 16:42:54 +0000607 if (Lex.getCode() != tgtok::greater) {
608 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000609 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000610 return Result;
611 }
612 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000613 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000614
615 return Result;
616}
617
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000618/// ParseRangePiece - Parse a bit/value range.
619/// RangePiece ::= INTVAL
620/// RangePiece ::= INTVAL '-' INTVAL
621/// RangePiece ::= INTVAL INTVAL
Matthias Braunc66e7552016-12-05 06:41:54 +0000622bool TGParser::ParseRangePiece(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000623 if (Lex.getCode() != tgtok::IntVal) {
624 TokError("expected integer or bitrange");
625 return true;
626 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000627 int64_t Start = Lex.getCurIntVal();
628 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000629
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000630 if (Start < 0)
631 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000632
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000633 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000634 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000635 Ranges.push_back(Start);
636 return false;
637 case tgtok::minus:
638 if (Lex.Lex() != tgtok::IntVal) {
639 TokError("expected integer value as end of range");
640 return true;
641 }
642 End = Lex.getCurIntVal();
643 break;
644 case tgtok::IntVal:
645 End = -Lex.getCurIntVal();
646 break;
647 }
Bob Wilson7248f862009-11-22 04:24:42 +0000648 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000649 return TokError("invalid range, cannot be negative");
650 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000651
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000652 // Add to the range.
Craig Toppera9642b42015-05-04 01:35:39 +0000653 if (Start < End)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000654 for (; Start <= End; ++Start)
655 Ranges.push_back(Start);
Craig Toppera9642b42015-05-04 01:35:39 +0000656 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000657 for (; Start >= End; --Start)
658 Ranges.push_back(Start);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000659 return false;
660}
661
662/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
663///
664/// RangeList ::= RangePiece (',' RangePiece)*
665///
Matthias Braunc66e7552016-12-05 06:41:54 +0000666void TGParser::ParseRangeList(SmallVectorImpl<unsigned> &Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000667 // Parse the first piece.
Matthias Braunc66e7552016-12-05 06:41:54 +0000668 if (ParseRangePiece(Result)) {
669 Result.clear();
670 return;
671 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000672 while (Lex.getCode() == tgtok::comma) {
673 Lex.Lex(); // Eat the comma.
674
675 // Parse the next range piece.
Matthias Braunc66e7552016-12-05 06:41:54 +0000676 if (ParseRangePiece(Result)) {
677 Result.clear();
678 return;
679 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000680 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000681}
682
683/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
684/// OptionalRangeList ::= '<' RangeList '>'
685/// OptionalRangeList ::= /*empty*/
Matthias Braunc66e7552016-12-05 06:41:54 +0000686bool TGParser::ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000687 if (Lex.getCode() != tgtok::less)
688 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000689
Chris Lattner526c8cb2009-06-21 03:39:35 +0000690 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000691 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000692
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000693 // Parse the range list.
Matthias Braunc66e7552016-12-05 06:41:54 +0000694 ParseRangeList(Ranges);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000695 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000696
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000697 if (Lex.getCode() != tgtok::greater) {
698 TokError("expected '>' at end of range list");
699 return Error(StartLoc, "to match this '<'");
700 }
701 Lex.Lex(); // eat the '>'.
702 return false;
703}
704
705/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
706/// OptionalBitList ::= '{' RangeList '}'
707/// OptionalBitList ::= /*empty*/
Matthias Braunc66e7552016-12-05 06:41:54 +0000708bool TGParser::ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000709 if (Lex.getCode() != tgtok::l_brace)
710 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000711
Chris Lattner526c8cb2009-06-21 03:39:35 +0000712 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000713 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000714
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000715 // Parse the range list.
Matthias Braunc66e7552016-12-05 06:41:54 +0000716 ParseRangeList(Ranges);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000717 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000718
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000719 if (Lex.getCode() != tgtok::r_brace) {
720 TokError("expected '}' at end of bit list");
721 return Error(StartLoc, "to match this '{'");
722 }
723 Lex.Lex(); // eat the '}'.
724 return false;
725}
726
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000727/// ParseType - Parse and return a tblgen type. This returns null on error.
728///
729/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000730/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000731/// Type ::= BIT // bit type
732/// Type ::= BITS '<' INTVAL '>' // bits<x> type
733/// Type ::= INT // int type
734/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000735/// Type ::= DAG // dag type
736/// Type ::= ClassID // Record Type
737///
738RecTy *TGParser::ParseType() {
739 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000740 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000741 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Tim Northover88403d72016-07-05 21:22:55 +0000742 case tgtok::Code: Lex.Lex(); return CodeRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000743 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
744 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000745 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000746 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000747 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Nicolai Haehnlefcd65252018-03-09 12:24:42 +0000748 TokError("unknown class name");
Craig Topper011817a2014-04-09 04:50:04 +0000749 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000750 case tgtok::Bits: {
751 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
752 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000753 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000754 }
755 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
756 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000757 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000758 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000759 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000760 if (Lex.Lex() != tgtok::greater) { // Eat count.
761 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000762 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000763 }
764 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000765 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000766 }
767 case tgtok::List: {
768 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
769 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000770 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000771 }
772 Lex.Lex(); // Eat '<'
773 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000774 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000775
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000776 if (Lex.getCode() != tgtok::greater) {
777 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000778 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000779 }
780 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000781 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000782 }
Bob Wilson7248f862009-11-22 04:24:42 +0000783 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000784}
785
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000786/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
787/// has already been read.
Matthias Braun215ff842016-12-05 07:35:13 +0000788Init *TGParser::ParseIDValue(Record *CurRec, StringInit *Name, SMLoc NameLoc,
David Greened4263a62011-10-19 13:04:20 +0000789 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000790 if (CurRec) {
791 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000792 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000793
Matthias Braun215ff842016-12-05 07:35:13 +0000794 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
David Greenedb10e692011-10-19 13:02:42 +0000795
David Greene47a665e2011-10-05 22:42:54 +0000796 if (CurMultiClass)
Matthias Braun215ff842016-12-05 07:35:13 +0000797 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
David Greenedb10e692011-10-19 13:02:42 +0000798 "::");
David Greene47a665e2011-10-05 22:42:54 +0000799
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000800 if (CurRec->isTemplateArg(TemplateArgName)) {
801 const RecordVal *RV = CurRec->getValue(TemplateArgName);
802 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000803 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000804 }
Matthias Braun215ff842016-12-05 07:35:13 +0000805 }
Bob Wilson7248f862009-11-22 04:24:42 +0000806
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000807 if (CurMultiClass) {
Nicolai Haehnle3c80e4c2018-03-05 14:01:38 +0000808 if (Name->getValue() == "NAME")
809 return VarInit::get(Name, StringRecTy::get());
810
Matthias Braun215ff842016-12-05 07:35:13 +0000811 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name, "::");
David Greenedb10e692011-10-19 13:02:42 +0000812
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000813 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
814 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
815 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000816 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000817 }
818 }
Bob Wilson7248f862009-11-22 04:24:42 +0000819
David Greenefb927af2012-02-22 16:09:41 +0000820 // If this is in a foreach loop, make sure it's not a loop iterator
Craig Toppereb4d7c62015-04-29 04:43:36 +0000821 for (const auto &L : Loops) {
822 VarInit *IterVar = dyn_cast<VarInit>(L.IterVar);
Matthias Braun215ff842016-12-05 07:35:13 +0000823 if (IterVar && IterVar->getNameInit() == Name)
David Greenefb927af2012-02-22 16:09:41 +0000824 return IterVar;
825 }
826
David Greene232bd602011-10-19 13:04:21 +0000827 if (Mode == ParseNameMode)
Matthias Braun215ff842016-12-05 07:35:13 +0000828 return Name;
David Greene232bd602011-10-19 13:04:21 +0000829
Nicolai Haehnlefcd65252018-03-09 12:24:42 +0000830 if (Init *I = Records.getGlobal(Name->getValue()))
831 return I;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000832
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000833 // Allow self-references of concrete defs, but delay the lookup so that we
834 // get the correct type.
835 if (CurRec && !CurMultiClass && CurRec->getNameInit() == Name)
836 return UnOpInit::get(UnOpInit::CAST, Name, CurRec->getType());
837
David Greene232bd602011-10-19 13:04:21 +0000838 if (Mode == ParseValueMode) {
Matthias Braun215ff842016-12-05 07:35:13 +0000839 Error(NameLoc, "Variable not defined: '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000840 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000841 }
Craig Toppera9642b42015-05-04 01:35:39 +0000842
Matthias Braun215ff842016-12-05 07:35:13 +0000843 return Name;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000844}
845
David Greene5d0c0512009-05-14 20:54:48 +0000846/// ParseOperation - Parse an operator. This returns null on error.
847///
848/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
849///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000850Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000851 switch (Lex.getCode()) {
852 default:
853 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000854 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000855 case tgtok::XHead:
856 case tgtok::XTail:
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000857 case tgtok::XSize:
David Greene2f7cf7f2011-01-07 17:05:37 +0000858 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000859 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
860 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000861 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000862
David Greenee8f3b272009-05-14 21:22:49 +0000863 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000864 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000865 case tgtok::XCast:
866 Lex.Lex(); // eat the operation
867 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000868
David Greenee8f3b272009-05-14 21:22:49 +0000869 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000870
Craig Topper011817a2014-04-09 04:50:04 +0000871 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000872 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000873 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000874 }
David Greene5d0c0512009-05-14 20:54:48 +0000875
David Greenee8f3b272009-05-14 21:22:49 +0000876 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000877 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000878 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000879 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000880 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000881 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000882 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000883 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000884 break;
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000885 case tgtok::XSize:
886 Lex.Lex();
887 Code = UnOpInit::SIZE;
888 Type = IntRecTy::get();
889 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000890 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000891 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000892 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000893 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000894 break;
David Greenee8f3b272009-05-14 21:22:49 +0000895 }
896 if (Lex.getCode() != tgtok::l_paren) {
897 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000898 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000899 }
900 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000901
David Greeneaf8ee2c2011-07-29 22:43:06 +0000902 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000903 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000904
Craig Topper85c07002015-04-30 05:54:22 +0000905 if (Code == UnOpInit::HEAD ||
906 Code == UnOpInit::TAIL ||
907 Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000908 ListInit *LHSl = dyn_cast<ListInit>(LHS);
909 StringInit *LHSs = dyn_cast<StringInit>(LHS);
910 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000911 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000912 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000913 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000914 }
915 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000916 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
917 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000918 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000919 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000920 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000921 }
922 }
923
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000924 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL ||
925 Code == UnOpInit::SIZE) {
Craig Topper011817a2014-04-09 04:50:04 +0000926 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000927 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000928 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000929 }
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000930 }
Bob Wilson7248f862009-11-22 04:24:42 +0000931
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000932 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
Craig Topperec9072d2015-05-14 05:53:53 +0000933 if (LHSl && LHSl->empty()) {
David Greened571b3c2009-05-14 22:38:31 +0000934 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000935 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000936 }
937 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000938 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000939 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000940 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000941 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000942 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000943 }
Craig Toppera9642b42015-05-04 01:35:39 +0000944 Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
945 : ListRecTy::get(Itemt->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000946 } else {
David Greened571b3c2009-05-14 22:38:31 +0000947 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000948 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000949 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000950 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000951 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000952 }
Craig Toppera9642b42015-05-04 01:35:39 +0000953 Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
David Greened571b3c2009-05-14 22:38:31 +0000954 }
955 }
956 }
957
David Greenee8f3b272009-05-14 21:22:49 +0000958 if (Lex.getCode() != tgtok::r_paren) {
959 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000960 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000961 }
962 Lex.Lex(); // eat the ')'
Nicolai Haehnlec47fe122018-03-19 14:13:37 +0000963 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec);
David Greenee8f3b272009-05-14 21:22:49 +0000964 }
David Greene5d0c0512009-05-14 20:54:48 +0000965
Nicolai Haehnleb5376052018-03-09 12:24:06 +0000966 case tgtok::XIsA: {
967 // Value ::= !isa '<' Type '>' '(' Value ')'
968 Lex.Lex(); // eat the operation
969
970 RecTy *Type = ParseOperatorType();
971 if (!Type)
972 return nullptr;
973
974 if (Lex.getCode() != tgtok::l_paren) {
975 TokError("expected '(' after type of !isa");
976 return nullptr;
977 }
978 Lex.Lex(); // eat the '('
979
980 Init *LHS = ParseValue(CurRec);
981 if (!LHS)
982 return nullptr;
983
984 if (Lex.getCode() != tgtok::r_paren) {
985 TokError("expected ')' in !isa");
986 return nullptr;
987 }
988 Lex.Lex(); // eat the ')'
989
990 return (IsAOpInit::get(Type, LHS))->Fold();
991 }
992
David Greene5d0c0512009-05-14 20:54:48 +0000993 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000994 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000995 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000996 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +0000997 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000998 case tgtok::XSRL:
999 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001000 case tgtok::XEq:
Nicolai Haehnleaa9ca692018-03-14 11:00:57 +00001001 case tgtok::XNe:
1002 case tgtok::XLe:
1003 case tgtok::XLt:
1004 case tgtok::XGe:
1005 case tgtok::XGt:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001006 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001007 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +00001008 tgtok::TokKind OpTok = Lex.getCode();
1009 SMLoc OpLoc = Lex.getLoc();
1010 Lex.Lex(); // eat the operation
1011
David Greene5d0c0512009-05-14 20:54:48 +00001012 BinOpInit::BinaryOp Code;
Chris Lattner61ea00b2010-10-05 23:58:18 +00001013 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +00001014 default: llvm_unreachable("Unhandled code!");
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001015 case tgtok::XConcat: Code = BinOpInit::CONCAT; break;
1016 case tgtok::XADD: Code = BinOpInit::ADD; break;
1017 case tgtok::XAND: Code = BinOpInit::AND; break;
1018 case tgtok::XOR: Code = BinOpInit::OR; break;
1019 case tgtok::XSRA: Code = BinOpInit::SRA; break;
1020 case tgtok::XSRL: Code = BinOpInit::SRL; break;
1021 case tgtok::XSHL: Code = BinOpInit::SHL; break;
1022 case tgtok::XEq: Code = BinOpInit::EQ; break;
Nicolai Haehnleaa9ca692018-03-14 11:00:57 +00001023 case tgtok::XNe: Code = BinOpInit::NE; break;
1024 case tgtok::XLe: Code = BinOpInit::LE; break;
1025 case tgtok::XLt: Code = BinOpInit::LT; break;
1026 case tgtok::XGe: Code = BinOpInit::GE; break;
1027 case tgtok::XGt: Code = BinOpInit::GT; break;
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001028 case tgtok::XListConcat: Code = BinOpInit::LISTCONCAT; break;
1029 case tgtok::XStrConcat: Code = BinOpInit::STRCONCAT; break;
1030 }
1031
1032 RecTy *Type = nullptr;
1033 RecTy *ArgType = nullptr;
1034 switch (OpTok) {
1035 default:
1036 llvm_unreachable("Unhandled code!");
1037 case tgtok::XConcat:
1038 Type = DagRecTy::get();
1039 ArgType = DagRecTy::get();
1040 break;
1041 case tgtok::XAND:
1042 case tgtok::XOR:
1043 case tgtok::XSRA:
1044 case tgtok::XSRL:
1045 case tgtok::XSHL:
1046 case tgtok::XADD:
1047 Type = IntRecTy::get();
1048 ArgType = IntRecTy::get();
1049 break;
1050 case tgtok::XEq:
Nicolai Haehnleaa9ca692018-03-14 11:00:57 +00001051 case tgtok::XNe:
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001052 Type = BitRecTy::get();
Nicolai Haehnleaa9ca692018-03-14 11:00:57 +00001053 // ArgType for Eq / Ne is not known at this point
1054 break;
1055 case tgtok::XLe:
1056 case tgtok::XLt:
1057 case tgtok::XGe:
1058 case tgtok::XGt:
1059 Type = BitRecTy::get();
1060 ArgType = IntRecTy::get();
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001061 break;
Daniel Sanders314e80e2014-05-07 10:13:19 +00001062 case tgtok::XListConcat:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001063 // We don't know the list type until we parse the first argument
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001064 ArgType = ItemType;
Daniel Sanders314e80e2014-05-07 10:13:19 +00001065 break;
Bob Wilson7248f862009-11-22 04:24:42 +00001066 case tgtok::XStrConcat:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001067 Type = StringRecTy::get();
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001068 ArgType = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +00001069 break;
David Greene5d0c0512009-05-14 20:54:48 +00001070 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00001071
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001072 if (Type && ItemType && !Type->typeIsConvertibleTo(ItemType)) {
1073 Error(OpLoc, Twine("expected value of type '") +
1074 ItemType->getAsString() + "', got '" +
1075 Type->getAsString() + "'");
1076 return nullptr;
1077 }
1078
David Greene5d0c0512009-05-14 20:54:48 +00001079 if (Lex.getCode() != tgtok::l_paren) {
1080 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001081 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001082 }
1083 Lex.Lex(); // eat the '('
1084
David Greeneaf8ee2c2011-07-29 22:43:06 +00001085 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +00001086
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001087 for (;;) {
1088 SMLoc InitLoc = Lex.getLoc();
1089 InitList.push_back(ParseValue(CurRec, ArgType));
Craig Topper011817a2014-04-09 04:50:04 +00001090 if (!InitList.back()) return nullptr;
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001091
1092 // All BinOps require their arguments to be of compatible types.
1093 TypedInit *TI = dyn_cast<TypedInit>(InitList.back());
1094 if (!ArgType) {
1095 ArgType = TI->getType();
1096
1097 switch (Code) {
1098 case BinOpInit::LISTCONCAT:
1099 if (!isa<ListRecTy>(ArgType)) {
1100 Error(InitLoc, Twine("expected a list, got value of type '") +
1101 ArgType->getAsString() + "'");
1102 return nullptr;
1103 }
1104 break;
1105 case BinOpInit::EQ:
Nicolai Haehnleaa9ca692018-03-14 11:00:57 +00001106 case BinOpInit::NE:
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001107 if (!ArgType->typeIsConvertibleTo(IntRecTy::get()) &&
1108 !ArgType->typeIsConvertibleTo(StringRecTy::get())) {
1109 Error(InitLoc, Twine("expected int, bits, or string; got value of "
1110 "type '") + ArgType->getAsString() + "'");
1111 return nullptr;
1112 }
1113 break;
1114 default: llvm_unreachable("other ops have fixed argument types");
1115 }
1116 } else {
1117 RecTy *Resolved = resolveTypes(ArgType, TI->getType());
1118 if (!Resolved) {
1119 Error(InitLoc, Twine("expected value of type '") +
1120 ArgType->getAsString() + "', got '" +
1121 TI->getType()->getAsString() + "'");
1122 return nullptr;
1123 }
1124 if (Code != BinOpInit::ADD && Code != BinOpInit::AND &&
1125 Code != BinOpInit::OR && Code != BinOpInit::SRA &&
1126 Code != BinOpInit::SRL && Code != BinOpInit::SHL)
1127 ArgType = Resolved;
1128 }
1129
1130 if (Lex.getCode() != tgtok::comma)
1131 break;
1132 Lex.Lex(); // eat the ','
David Greene5d0c0512009-05-14 20:54:48 +00001133 }
David Greene5d0c0512009-05-14 20:54:48 +00001134
1135 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +00001136 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +00001137 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001138 }
1139 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +00001140
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001141 if (Code == BinOpInit::LISTCONCAT)
1142 Type = ArgType;
Daniel Sanders314e80e2014-05-07 10:13:19 +00001143
Chris Lattner61ea00b2010-10-05 23:58:18 +00001144 // We allow multiple operands to associative operators like !strconcat as
1145 // shorthand for nesting them.
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001146 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT ||
1147 Code == BinOpInit::CONCAT || Code == BinOpInit::ADD ||
1148 Code == BinOpInit::AND || Code == BinOpInit::OR) {
Chris Lattner61ea00b2010-10-05 23:58:18 +00001149 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001150 Init *RHS = InitList.pop_back_val();
Nicolai Haehnlec47fe122018-03-19 14:13:37 +00001151 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))->Fold(CurRec);
Chris Lattner61ea00b2010-10-05 23:58:18 +00001152 InitList.back() = RHS;
1153 }
1154 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00001155
Chris Lattner61ea00b2010-10-05 23:58:18 +00001156 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +00001157 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Nicolai Haehnlec47fe122018-03-19 14:13:37 +00001158 ->Fold(CurRec);
Mikhail Glushenkovde683892010-10-23 07:32:37 +00001159
Chris Lattner61ea00b2010-10-05 23:58:18 +00001160 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +00001161 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001162 }
1163
Nicolai Haehnle8ebf7e42018-03-05 15:21:04 +00001164 case tgtok::XForEach: { // Value ::= !foreach '(' Id ',' Value ',' Value ')'
1165 SMLoc OpLoc = Lex.getLoc();
1166 Lex.Lex(); // eat the operation
1167 if (Lex.getCode() != tgtok::l_paren) {
1168 TokError("expected '(' after !foreach");
1169 return nullptr;
1170 }
1171
1172 if (Lex.Lex() != tgtok::Id) { // eat the '('
1173 TokError("first argument of !foreach must be an identifier");
1174 return nullptr;
1175 }
1176
1177 Init *LHS = StringInit::get(Lex.getCurStrVal());
1178
1179 if (CurRec->getValue(LHS)) {
1180 TokError((Twine("iteration variable '") + LHS->getAsString() +
1181 "' already defined")
1182 .str());
1183 return nullptr;
1184 }
1185
1186 if (Lex.Lex() != tgtok::comma) { // eat the id
1187 TokError("expected ',' in ternary operator");
1188 return nullptr;
1189 }
1190 Lex.Lex(); // eat the ','
1191
1192 Init *MHS = ParseValue(CurRec);
1193 if (!MHS)
1194 return nullptr;
1195
1196 if (Lex.getCode() != tgtok::comma) {
1197 TokError("expected ',' in ternary operator");
1198 return nullptr;
1199 }
1200 Lex.Lex(); // eat the ','
1201
1202 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1203 if (!MHSt) {
1204 TokError("could not get type of !foreach input");
1205 return nullptr;
1206 }
1207
1208 RecTy *InEltType = nullptr;
1209 RecTy *OutEltType = nullptr;
1210 bool IsDAG = false;
1211
1212 if (ListRecTy *InListTy = dyn_cast<ListRecTy>(MHSt->getType())) {
1213 InEltType = InListTy->getElementType();
1214 if (ItemType) {
1215 if (ListRecTy *OutListTy = dyn_cast<ListRecTy>(ItemType)) {
1216 OutEltType = OutListTy->getElementType();
1217 } else {
1218 Error(OpLoc,
1219 "expected value of type '" + Twine(ItemType->getAsString()) +
1220 "', but got !foreach of list type");
1221 return nullptr;
1222 }
1223 }
1224 } else if (DagRecTy *InDagTy = dyn_cast<DagRecTy>(MHSt->getType())) {
1225 InEltType = InDagTy;
1226 if (ItemType && !isa<DagRecTy>(ItemType)) {
1227 Error(OpLoc,
1228 "expected value of type '" + Twine(ItemType->getAsString()) +
1229 "', but got !foreach of dag type");
1230 return nullptr;
1231 }
1232 IsDAG = true;
1233 } else {
1234 TokError("!foreach must have list or dag input");
1235 return nullptr;
1236 }
1237
1238 CurRec->addValue(RecordVal(LHS, InEltType, false));
1239 Init *RHS = ParseValue(CurRec, OutEltType);
1240 CurRec->removeValue(LHS);
1241 if (!RHS)
1242 return nullptr;
1243
1244 if (Lex.getCode() != tgtok::r_paren) {
1245 TokError("expected ')' in binary operator");
1246 return nullptr;
1247 }
1248 Lex.Lex(); // eat the ')'
1249
1250 RecTy *OutType;
1251 if (IsDAG) {
1252 OutType = InEltType;
1253 } else {
1254 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1255 if (!RHSt) {
1256 TokError("could not get type of !foreach result");
1257 return nullptr;
1258 }
1259 OutType = RHSt->getType()->getListTy();
1260 }
1261
1262 return (TernOpInit::get(TernOpInit::FOREACH, LHS, MHS, RHS, OutType))
Nicolai Haehnlec47fe122018-03-19 14:13:37 +00001263 ->Fold(CurRec);
Nicolai Haehnle8ebf7e42018-03-05 15:21:04 +00001264 }
1265
Nicolai Haehnle6c118652018-03-14 11:00:26 +00001266 case tgtok::XDag:
David Greene3587eed2009-05-14 23:26:46 +00001267 case tgtok::XIf:
David Greene98ed3c72009-05-14 21:54:42 +00001268 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
1269 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +00001270 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001271
David Greene98ed3c72009-05-14 21:54:42 +00001272 tgtok::TokKind LexCode = Lex.getCode();
1273 Lex.Lex(); // eat the operation
1274 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001275 default: llvm_unreachable("Unhandled code!");
Nicolai Haehnle6c118652018-03-14 11:00:26 +00001276 case tgtok::XDag:
1277 Code = TernOpInit::DAG;
1278 Type = DagRecTy::get();
1279 ItemType = nullptr;
1280 break;
David Greene3587eed2009-05-14 23:26:46 +00001281 case tgtok::XIf:
1282 Code = TernOpInit::IF;
1283 break;
David Greene98ed3c72009-05-14 21:54:42 +00001284 case tgtok::XSubst:
1285 Code = TernOpInit::SUBST;
1286 break;
1287 }
1288 if (Lex.getCode() != tgtok::l_paren) {
1289 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001290 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001291 }
1292 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001293
David Greeneaf8ee2c2011-07-29 22:43:06 +00001294 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001295 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001296
David Greene98ed3c72009-05-14 21:54:42 +00001297 if (Lex.getCode() != tgtok::comma) {
1298 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001299 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001300 }
1301 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001302
Nicolai Haehnle6c118652018-03-14 11:00:26 +00001303 SMLoc MHSLoc = Lex.getLoc();
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001304 Init *MHS = ParseValue(CurRec, ItemType);
1305 if (!MHS)
1306 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001307
David Greene98ed3c72009-05-14 21:54:42 +00001308 if (Lex.getCode() != tgtok::comma) {
1309 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001310 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001311 }
1312 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001313
Nicolai Haehnle6c118652018-03-14 11:00:26 +00001314 SMLoc RHSLoc = Lex.getLoc();
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001315 Init *RHS = ParseValue(CurRec, ItemType);
1316 if (!RHS)
1317 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001318
David Greene98ed3c72009-05-14 21:54:42 +00001319 if (Lex.getCode() != tgtok::r_paren) {
1320 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001321 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001322 }
1323 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001324
David Greene98ed3c72009-05-14 21:54:42 +00001325 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001326 default: llvm_unreachable("Unhandled code!");
Nicolai Haehnle6c118652018-03-14 11:00:26 +00001327 case tgtok::XDag: {
1328 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1329 if (!MHSt && !isa<UnsetInit>(MHS)) {
1330 Error(MHSLoc, "could not determine type of the child list in !dag");
1331 return nullptr;
1332 }
1333 if (MHSt && !isa<ListRecTy>(MHSt->getType())) {
1334 Error(MHSLoc, Twine("expected list of children, got type '") +
1335 MHSt->getType()->getAsString() + "'");
1336 return nullptr;
1337 }
1338
1339 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1340 if (!RHSt && !isa<UnsetInit>(RHS)) {
1341 Error(RHSLoc, "could not determine type of the name list in !dag");
1342 return nullptr;
1343 }
1344 if (RHSt && StringRecTy::get()->getListTy() != RHSt->getType()) {
1345 Error(RHSLoc, Twine("expected list<string>, got type '") +
1346 RHSt->getType()->getAsString() + "'");
1347 return nullptr;
1348 }
1349
1350 if (!MHSt && !RHSt) {
1351 Error(MHSLoc,
1352 "cannot have both unset children and unset names in !dag");
1353 return nullptr;
1354 }
1355 break;
1356 }
David Greene3587eed2009-05-14 23:26:46 +00001357 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001358 RecTy *MHSTy = nullptr;
1359 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001360
Sean Silvafb509ed2012-10-10 20:24:43 +00001361 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001362 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001363 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001364 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001365 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001366 MHSTy = BitRecTy::get();
1367
Sean Silvafb509ed2012-10-10 20:24:43 +00001368 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001369 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001370 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001371 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001372 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001373 RHSTy = BitRecTy::get();
1374
1375 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001376 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001377 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001378 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001379 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001380
1381 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001382 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001383 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001384 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001385
Nicolai Haehnle13080fd2018-03-06 13:48:20 +00001386 Type = resolveTypes(MHSTy, RHSTy);
1387 if (!Type) {
1388 TokError(Twine("inconsistent types '") + MHSTy->getAsString() +
1389 "' and '" + RHSTy->getAsString() + "' for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001390 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001391 }
1392 break;
1393 }
David Greene98ed3c72009-05-14 21:54:42 +00001394 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001395 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001396 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001397 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001398 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001399 }
1400 Type = RHSt->getType();
1401 break;
1402 }
1403 }
Nicolai Haehnlec47fe122018-03-19 14:13:37 +00001404 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec);
David Greene98ed3c72009-05-14 21:54:42 +00001405 }
Nicolai Haehnled34f6842018-03-06 13:49:16 +00001406
1407 case tgtok::XFoldl: {
1408 // Value ::= !foldl '(' Id ',' Id ',' Value ',' Value ',' Value ')'
1409 Lex.Lex(); // eat the operation
1410 if (Lex.getCode() != tgtok::l_paren) {
1411 TokError("expected '(' after !foldl");
1412 return nullptr;
1413 }
1414 Lex.Lex(); // eat the '('
1415
1416 Init *StartUntyped = ParseValue(CurRec);
1417 if (!StartUntyped)
1418 return nullptr;
1419
1420 TypedInit *Start = dyn_cast<TypedInit>(StartUntyped);
1421 if (!Start) {
1422 TokError(Twine("could not get type of !foldl start: '") +
1423 StartUntyped->getAsString() + "'");
1424 return nullptr;
1425 }
1426
1427 if (Lex.getCode() != tgtok::comma) {
1428 TokError("expected ',' in !foldl");
1429 return nullptr;
1430 }
1431 Lex.Lex(); // eat the ','
1432
1433 Init *ListUntyped = ParseValue(CurRec);
1434 if (!ListUntyped)
1435 return nullptr;
1436
1437 TypedInit *List = dyn_cast<TypedInit>(ListUntyped);
1438 if (!List) {
1439 TokError(Twine("could not get type of !foldl list: '") +
1440 ListUntyped->getAsString() + "'");
1441 return nullptr;
1442 }
1443
1444 ListRecTy *ListType = dyn_cast<ListRecTy>(List->getType());
1445 if (!ListType) {
1446 TokError(Twine("!foldl list must be a list, but is of type '") +
1447 List->getType()->getAsString());
1448 return nullptr;
1449 }
1450
1451 if (Lex.getCode() != tgtok::comma) {
1452 TokError("expected ',' in !foldl");
1453 return nullptr;
1454 }
1455
1456 if (Lex.Lex() != tgtok::Id) { // eat the ','
1457 TokError("third argument of !foldl must be an identifier");
1458 return nullptr;
1459 }
1460
1461 Init *A = StringInit::get(Lex.getCurStrVal());
1462 if (CurRec->getValue(A)) {
1463 TokError((Twine("left !foldl variable '") + A->getAsString() +
1464 "' already defined")
1465 .str());
1466 return nullptr;
1467 }
1468
1469 if (Lex.Lex() != tgtok::comma) { // eat the id
1470 TokError("expected ',' in !foldl");
1471 return nullptr;
1472 }
1473
1474 if (Lex.Lex() != tgtok::Id) { // eat the ','
1475 TokError("fourth argument of !foldl must be an identifier");
1476 return nullptr;
1477 }
1478
1479 Init *B = StringInit::get(Lex.getCurStrVal());
1480 if (CurRec->getValue(B)) {
1481 TokError((Twine("right !foldl variable '") + B->getAsString() +
1482 "' already defined")
1483 .str());
1484 return nullptr;
1485 }
1486
1487 if (Lex.Lex() != tgtok::comma) { // eat the id
1488 TokError("expected ',' in !foldl");
1489 return nullptr;
1490 }
1491 Lex.Lex(); // eat the ','
1492
1493 CurRec->addValue(RecordVal(A, Start->getType(), false));
1494 CurRec->addValue(RecordVal(B, ListType->getElementType(), false));
1495 Init *ExprUntyped = ParseValue(CurRec);
1496 CurRec->removeValue(A);
1497 CurRec->removeValue(B);
1498 if (!ExprUntyped)
1499 return nullptr;
1500
1501 TypedInit *Expr = dyn_cast<TypedInit>(ExprUntyped);
1502 if (!Expr) {
1503 TokError("could not get type of !foldl expression");
1504 return nullptr;
1505 }
1506
1507 if (Expr->getType() != Start->getType()) {
1508 TokError(Twine("!foldl expression must be of same type as start (") +
1509 Start->getType()->getAsString() + "), but is of type " +
1510 Expr->getType()->getAsString());
1511 return nullptr;
1512 }
1513
1514 if (Lex.getCode() != tgtok::r_paren) {
1515 TokError("expected ')' in fold operator");
1516 return nullptr;
1517 }
1518 Lex.Lex(); // eat the ')'
1519
1520 return FoldOpInit::get(Start, List, A, B, Expr, Start->getType())
1521 ->Fold(CurRec);
1522 }
David Greene5d0c0512009-05-14 20:54:48 +00001523 }
David Greene5d0c0512009-05-14 20:54:48 +00001524}
1525
1526/// ParseOperatorType - Parse a type for an operator. This returns
1527/// null on error.
1528///
1529/// OperatorType ::= '<' Type '>'
1530///
Dan Gohman1432ef82009-08-12 22:10:57 +00001531RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001532 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001533
1534 if (Lex.getCode() != tgtok::less) {
1535 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001536 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001537 }
1538 Lex.Lex(); // eat the <
1539
1540 Type = ParseType();
1541
Craig Topper011817a2014-04-09 04:50:04 +00001542 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001543 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001544 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001545 }
1546
1547 if (Lex.getCode() != tgtok::greater) {
1548 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001549 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001550 }
1551 Lex.Lex(); // eat the >
1552
1553 return Type;
1554}
1555
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001556/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1557///
1558/// SimpleValue ::= IDValue
1559/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001560/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001561/// SimpleValue ::= CODEFRAGMENT
1562/// SimpleValue ::= '?'
1563/// SimpleValue ::= '{' ValueList '}'
1564/// SimpleValue ::= ID '<' ValueListNE '>'
1565/// SimpleValue ::= '[' ValueList ']'
1566/// SimpleValue ::= '(' IDValue DagArgList ')'
1567/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001568/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001569/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1570/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1571/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001572/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001573/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1574///
David Greened4263a62011-10-19 13:04:20 +00001575Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1576 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001577 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001578 switch (Lex.getCode()) {
1579 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001580 case tgtok::paste:
1581 // This is a leading paste operation. This is deprecated but
1582 // still exists in some .td files. Ignore it.
1583 Lex.Lex(); // Skip '#'.
1584 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001585 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001586 case tgtok::BinaryIntVal: {
1587 auto BinaryVal = Lex.getCurBinaryIntVal();
1588 SmallVector<Init*, 16> Bits(BinaryVal.second);
1589 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001590 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001591 R = BitsInit::get(Bits);
1592 Lex.Lex();
1593 break;
1594 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001595 case tgtok::StrVal: {
1596 std::string Val = Lex.getCurStrVal();
1597 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001598
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001599 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001600 while (Lex.getCode() == tgtok::StrVal) {
1601 Val += Lex.getCurStrVal();
1602 Lex.Lex();
1603 }
Bob Wilson7248f862009-11-22 04:24:42 +00001604
David Greenee32ebf22011-07-29 19:07:07 +00001605 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001606 break;
1607 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001608 case tgtok::CodeFragment:
Tim Northover88403d72016-07-05 21:22:55 +00001609 R = CodeInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001610 Lex.Lex();
1611 break;
1612 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001613 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001614 Lex.Lex();
1615 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001616 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001617 SMLoc NameLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00001618 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001619 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001620 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001621
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001622 // Value ::= ID '<' ValueListNE '>'
1623 if (Lex.Lex() == tgtok::greater) {
1624 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001625 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001626 }
David Greene8618f952009-06-08 20:23:18 +00001627
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001628 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1629 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1630 // body.
Matthias Braun215ff842016-12-05 07:35:13 +00001631 Record *Class = Records.getClass(Name->getValue());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001632 if (!Class) {
Matthias Braun215ff842016-12-05 07:35:13 +00001633 Error(NameLoc, "Expected a class name, got '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001634 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001635 }
David Greene8618f952009-06-08 20:23:18 +00001636
Nicolai Haehnled4c0a5d2018-03-06 13:49:01 +00001637 SmallVector<Init *, 8> Args;
1638 ParseValueList(Args, CurRec, Class);
1639 if (Args.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001640
David Greene8618f952009-06-08 20:23:18 +00001641 if (Lex.getCode() != tgtok::greater) {
1642 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001643 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001644 }
1645 Lex.Lex(); // eat the '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001646
Nicolai Haehnled4c0a5d2018-03-06 13:49:01 +00001647 // Typecheck the template arguments list
1648 ArrayRef<Init *> ExpectedArgs = Class->getTemplateArgs();
1649 if (ExpectedArgs.size() < Args.size()) {
1650 Error(NameLoc,
1651 "More template args specified than expected");
Craig Topper011817a2014-04-09 04:50:04 +00001652 return nullptr;
Hal Finkela8c1f462014-01-02 20:47:09 +00001653 }
Bob Wilson7248f862009-11-22 04:24:42 +00001654
Nicolai Haehnled4c0a5d2018-03-06 13:49:01 +00001655 for (unsigned i = 0, e = ExpectedArgs.size(); i != e; ++i) {
1656 RecordVal *ExpectedArg = Class->getValue(ExpectedArgs[i]);
1657 if (i < Args.size()) {
1658 if (TypedInit *TI = dyn_cast<TypedInit>(Args[i])) {
1659 RecTy *ExpectedType = ExpectedArg->getType();
1660 if (!TI->getType()->typeIsConvertibleTo(ExpectedType)) {
1661 Error(NameLoc,
1662 "Value specified for template argument #" + Twine(i) + " (" +
1663 ExpectedArg->getNameInitAsString() + ") is of type '" +
1664 TI->getType()->getAsString() + "', expected '" +
1665 ExpectedType->getAsString() + "': " + TI->getAsString());
1666 return nullptr;
1667 }
1668 continue;
1669 }
1670 } else if (ExpectedArg->getValue()->isComplete())
1671 continue;
1672
1673 Error(NameLoc,
1674 "Value not specified for template argument #" + Twine(i) + " (" +
1675 ExpectedArgs[i]->getAsUnquotedString() + ")");
1676 return nullptr;
1677 }
1678
1679 return VarDefInit::get(Class, Args)->Fold();
Bob Wilson7248f862009-11-22 04:24:42 +00001680 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001681 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001682 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001683 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001684 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001685
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001686 if (Lex.getCode() != tgtok::r_brace) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001687 ParseValueList(Vals, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001688 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001689 }
1690 if (Lex.getCode() != tgtok::r_brace) {
1691 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001692 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001693 }
1694 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001695
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001696 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001697
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001698 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1699 // first. We'll first read everything in to a vector, then we can reverse
1700 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001701 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001702 // FIXME: The following two loops would not be duplicated
1703 // if the API was a little more orthogonal.
1704
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001705 // bits<n> values are allowed to initialize n bits.
1706 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1707 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1708 NewBits.push_back(BI->getBit((e - i) - 1));
1709 continue;
1710 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001711 // bits<n> can also come from variable initializers.
1712 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1713 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1714 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1715 NewBits.push_back(VI->getBit((e - i) - 1));
1716 continue;
1717 }
1718 // Fallthrough to try convert this to a bit.
1719 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001720 // All other values must be convertible to just a single bit.
Nicolai Haehnledfda9dc2018-03-06 13:48:39 +00001721 Init *Bit = Vals[i]->getCastTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001722 if (!Bit) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001723 Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
Chris Lattner52416952007-11-22 21:06:59 +00001724 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001725 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001726 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001727 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001728 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001729 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001730 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001731 }
1732 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1733 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001734 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001735
Craig Topper011817a2014-04-09 04:50:04 +00001736 RecTy *DeducedEltTy = nullptr;
1737 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001738
Craig Topper011817a2014-04-09 04:50:04 +00001739 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001740 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001741 if (!ListType) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001742 TokError(Twine("Type mismatch for list, expected list type, got ") +
1743 ItemType->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001744 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001745 }
1746 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001747 }
David Greene8618f952009-06-08 20:23:18 +00001748
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001749 if (Lex.getCode() != tgtok::r_square) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001750 ParseValueList(Vals, CurRec, nullptr,
1751 GivenListTy ? GivenListTy->getElementType() : nullptr);
Craig Topper011817a2014-04-09 04:50:04 +00001752 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001753 }
1754 if (Lex.getCode() != tgtok::r_square) {
1755 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001756 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001757 }
1758 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001759
Craig Topper011817a2014-04-09 04:50:04 +00001760 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001761 if (Lex.getCode() == tgtok::less) {
1762 // Optional list element type
1763 Lex.Lex(); // eat the '<'
1764
1765 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001766 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001767 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001768 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001769 }
1770
1771 if (Lex.getCode() != tgtok::greater) {
1772 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001773 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001774 }
1775 Lex.Lex(); // eat the '>'
1776 }
1777
1778 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001779 RecTy *EltTy = nullptr;
Craig Toppereb4d7c62015-04-29 04:43:36 +00001780 for (Init *V : Vals) {
1781 TypedInit *TArg = dyn_cast<TypedInit>(V);
Nicolai Haehnleef60a262018-03-14 11:00:33 +00001782 if (TArg) {
1783 if (EltTy) {
1784 EltTy = resolveTypes(EltTy, TArg->getType());
1785 if (!EltTy) {
1786 TokError("Incompatible types in list elements");
1787 return nullptr;
1788 }
1789 } else {
1790 EltTy = TArg->getType();
David Greene8618f952009-06-08 20:23:18 +00001791 }
David Greene8618f952009-06-08 20:23:18 +00001792 }
1793 }
1794
Craig Topper011817a2014-04-09 04:50:04 +00001795 if (GivenEltTy) {
1796 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001797 // Verify consistency
1798 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1799 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001800 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001801 }
1802 }
1803 EltTy = GivenEltTy;
1804 }
1805
Craig Topper011817a2014-04-09 04:50:04 +00001806 if (!EltTy) {
1807 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001808 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001809 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001810 }
1811 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001812 } else {
David Greene8618f952009-06-08 20:23:18 +00001813 // Make sure the deduced type is compatible with the given type
1814 if (GivenListTy) {
1815 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
Nicolai Haehnlef19083d2018-02-22 15:26:21 +00001816 TokError(Twine("Element type mismatch for list: element type '") +
1817 EltTy->getAsString() + "' not convertible to '" +
1818 GivenListTy->getElementType()->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001819 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001820 }
1821 }
1822 DeducedEltTy = EltTy;
1823 }
Bob Wilson7248f862009-11-22 04:24:42 +00001824
David Greenee32ebf22011-07-29 19:07:07 +00001825 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001826 }
1827 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1828 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001829 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001830 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001831 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001832 }
Bob Wilson7248f862009-11-22 04:24:42 +00001833
David Greeneaf8ee2c2011-07-29 22:43:06 +00001834 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001835 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001836
Nate Begemandbe3f772009-03-19 05:21:56 +00001837 // If the operator name is present, parse it.
Matthias Braun7cf3b112016-12-05 06:00:41 +00001838 StringInit *OperatorName = nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001839 if (Lex.getCode() == tgtok::colon) {
1840 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1841 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001842 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001843 }
Matthias Braun7cf3b112016-12-05 06:00:41 +00001844 OperatorName = StringInit::get(Lex.getCurStrVal());
Nate Begemandbe3f772009-03-19 05:21:56 +00001845 Lex.Lex(); // eat the VarName.
1846 }
Bob Wilson7248f862009-11-22 04:24:42 +00001847
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001848 SmallVector<std::pair<llvm::Init*, StringInit*>, 8> DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001849 if (Lex.getCode() != tgtok::r_paren) {
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001850 ParseDagArgList(DagArgs, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001851 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001852 }
Bob Wilson7248f862009-11-22 04:24:42 +00001853
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001854 if (Lex.getCode() != tgtok::r_paren) {
1855 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001856 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001857 }
1858 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001859
David Greenee32ebf22011-07-29 19:07:07 +00001860 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001861 }
Bob Wilson7248f862009-11-22 04:24:42 +00001862
David Greene2f7cf7f2011-01-07 17:05:37 +00001863 case tgtok::XHead:
1864 case tgtok::XTail:
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +00001865 case tgtok::XSize:
David Greene2f7cf7f2011-01-07 17:05:37 +00001866 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001867 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Nicolai Haehnleb5376052018-03-09 12:24:06 +00001868 case tgtok::XIsA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001869 case tgtok::XConcat:
Nicolai Haehnle6c118652018-03-14 11:00:26 +00001870 case tgtok::XDag:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001871 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001872 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +00001873 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +00001874 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001875 case tgtok::XSRL:
1876 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001877 case tgtok::XEq:
Nicolai Haehnleaa9ca692018-03-14 11:00:57 +00001878 case tgtok::XNe:
1879 case tgtok::XLe:
1880 case tgtok::XLt:
1881 case tgtok::XGe:
1882 case tgtok::XGt:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001883 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001884 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001885 case tgtok::XIf:
Nicolai Haehnled34f6842018-03-06 13:49:16 +00001886 case tgtok::XFoldl:
David Greenee917fff2009-05-14 22:23:47 +00001887 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001888 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001889 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001890 }
1891 }
Bob Wilson7248f862009-11-22 04:24:42 +00001892
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001893 return R;
1894}
1895
1896/// ParseValue - Parse a tblgen value. This returns null on error.
1897///
1898/// Value ::= SimpleValue ValueSuffix*
1899/// ValueSuffix ::= '{' BitList '}'
1900/// ValueSuffix ::= '[' BitList ']'
1901/// ValueSuffix ::= '.' ID
1902///
David Greened4263a62011-10-19 13:04:20 +00001903Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1904 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001905 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001906
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001907 // Parse the suffixes now if present.
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001908 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001909 switch (Lex.getCode()) {
1910 default: return Result;
1911 case tgtok::l_brace: {
Nicolai Haehnle2435855a2018-03-09 12:24:20 +00001912 if (Mode == ParseNameMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001913 // This is the beginning of the object body.
1914 return Result;
1915
Chris Lattner526c8cb2009-06-21 03:39:35 +00001916 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001917 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001918 SmallVector<unsigned, 16> Ranges;
1919 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001920 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001921
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001922 // Reverse the bitlist.
1923 std::reverse(Ranges.begin(), Ranges.end());
1924 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001925 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001926 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001927 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001928 }
Bob Wilson7248f862009-11-22 04:24:42 +00001929
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001930 // Eat the '}'.
1931 if (Lex.getCode() != tgtok::r_brace) {
1932 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001933 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001934 }
1935 Lex.Lex();
1936 break;
1937 }
1938 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001939 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001940 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001941 SmallVector<unsigned, 16> Ranges;
1942 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001943 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001944
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001945 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001946 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001947 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001948 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001949 }
Bob Wilson7248f862009-11-22 04:24:42 +00001950
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001951 // Eat the ']'.
1952 if (Lex.getCode() != tgtok::r_square) {
1953 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001954 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001955 }
1956 Lex.Lex();
1957 break;
1958 }
Matthias Braun6a441832016-12-05 06:00:36 +00001959 case tgtok::period: {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001960 if (Lex.Lex() != tgtok::Id) { // eat the .
1961 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001962 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001963 }
Matthias Braun6a441832016-12-05 06:00:36 +00001964 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
1965 if (!Result->getFieldType(FieldName)) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001966 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001967 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001968 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001969 }
Nicolai Haehnle2ad19012018-03-19 14:14:28 +00001970 Result = FieldInit::get(Result, FieldName)->Fold(CurRec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001971 Lex.Lex(); // eat field name
1972 break;
Matthias Braun6a441832016-12-05 06:00:36 +00001973 }
David Greene8e85b482011-10-19 13:04:43 +00001974
1975 case tgtok::paste:
1976 SMLoc PasteLoc = Lex.getLoc();
1977
1978 // Create a !strconcat() operation, first casting each operand to
1979 // a string if necessary.
1980
Sean Silvafb509ed2012-10-10 20:24:43 +00001981 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001982 if (!LHS) {
1983 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001984 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001985 }
Craig Toppera9642b42015-05-04 01:35:39 +00001986
David Greene8e85b482011-10-19 13:04:43 +00001987 if (LHS->getType() != StringRecTy::get()) {
Nicolai Haehnle335c70f2018-03-19 14:14:04 +00001988 LHS = dyn_cast<TypedInit>(
1989 UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get())
1990 ->Fold(CurRec));
1991 if (!LHS) {
1992 Error(PasteLoc, Twine("can't cast '") + LHS->getAsString() +
1993 "' to string");
1994 return nullptr;
1995 }
David Greene8e85b482011-10-19 13:04:43 +00001996 }
1997
Craig Topper011817a2014-04-09 04:50:04 +00001998 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001999
2000 Lex.Lex(); // Eat the '#'.
Nicolai Haehnled34f6842018-03-06 13:49:16 +00002001 switch (Lex.getCode()) {
David Greene8e85b482011-10-19 13:04:43 +00002002 case tgtok::colon:
2003 case tgtok::semi:
2004 case tgtok::l_brace:
2005 // These are all of the tokens that can begin an object body.
2006 // Some of these can also begin values but we disallow those cases
2007 // because they are unlikely to be useful.
Craig Toppera9642b42015-05-04 01:35:39 +00002008
David Greene8e85b482011-10-19 13:04:43 +00002009 // Trailing paste, concat with an empty string.
2010 RHS = StringInit::get("");
2011 break;
2012
2013 default:
Nicolai Haehnle77841b12018-03-14 11:00:43 +00002014 Init *RHSResult = ParseValue(CurRec, nullptr, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00002015 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00002016 if (!RHS) {
2017 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00002018 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00002019 }
2020
2021 if (RHS->getType() != StringRecTy::get()) {
Nicolai Haehnle335c70f2018-03-19 14:14:04 +00002022 RHS = dyn_cast<TypedInit>(
2023 UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get())
2024 ->Fold(CurRec));
2025 if (!RHS) {
2026 Error(PasteLoc, Twine("can't cast '") + RHS->getAsString() +
2027 "' to string");
2028 return nullptr;
2029 }
David Greene8e85b482011-10-19 13:04:43 +00002030 }
Craig Toppera9642b42015-05-04 01:35:39 +00002031
David Greene8e85b482011-10-19 13:04:43 +00002032 break;
2033 }
2034
Nicolai Haehnle1eaebc62018-03-19 14:13:54 +00002035 Result = BinOpInit::getStrConcat(LHS, RHS);
David Greene8e85b482011-10-19 13:04:43 +00002036 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002037 }
2038 }
2039}
2040
2041/// ParseDagArgList - Parse the argument list for a dag literal expression.
2042///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002043/// DagArg ::= Value (':' VARNAME)?
2044/// DagArg ::= VARNAME
2045/// DagArgList ::= DagArg
2046/// DagArgList ::= DagArgList ',' DagArg
Matthias Braun1ddb78c2016-12-05 06:41:51 +00002047void TGParser::ParseDagArgList(
2048 SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
2049 Record *CurRec) {
Bob Wilson7248f862009-11-22 04:24:42 +00002050
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002051 while (true) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002052 // DagArg ::= VARNAME
2053 if (Lex.getCode() == tgtok::VarName) {
2054 // A missing value is treated like '?'.
Matthias Braunbb053162016-12-05 06:00:46 +00002055 StringInit *VarName = StringInit::get(Lex.getCurStrVal());
2056 Result.emplace_back(UnsetInit::get(), VarName);
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002057 Lex.Lex();
2058 } else {
2059 // DagArg ::= Value (':' VARNAME)?
2060 Init *Val = ParseValue(CurRec);
Matthias Braun1ddb78c2016-12-05 06:41:51 +00002061 if (!Val) {
2062 Result.clear();
2063 return;
2064 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002065
2066 // If the variable name is present, add it.
Matthias Braunbb053162016-12-05 06:00:46 +00002067 StringInit *VarName = nullptr;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002068 if (Lex.getCode() == tgtok::colon) {
2069 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
2070 TokError("expected variable name in dag literal");
Matthias Braun1ddb78c2016-12-05 06:41:51 +00002071 Result.clear();
2072 return;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002073 }
Matthias Braunbb053162016-12-05 06:00:46 +00002074 VarName = StringInit::get(Lex.getCurStrVal());
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002075 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002076 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002077
2078 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002079 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002080 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00002081 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002082 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002083}
2084
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002085/// ParseValueList - Parse a comma separated list of values, returning them as a
2086/// vector. Note that this always expects to be able to parse at least one
2087/// value. It returns an empty list if this is not possible.
2088///
2089/// ValueList ::= Value (',' Value)
2090///
Matthias Braunc66e7552016-12-05 06:41:54 +00002091void TGParser::ParseValueList(SmallVectorImpl<Init*> &Result, Record *CurRec,
2092 Record *ArgsRec, RecTy *EltTy) {
David Greene8618f952009-06-08 20:23:18 +00002093 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00002094 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00002095 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002096 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00002097 if (TArgs.empty()) {
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00002098 TokError("template argument provided to non-template class");
Matthias Braunc66e7552016-12-05 06:41:54 +00002099 Result.clear();
2100 return;
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00002101 }
David Greene8618f952009-06-08 20:23:18 +00002102 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00002103 if (!RV) {
2104 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
2105 << ")\n";
2106 }
David Greene8618f952009-06-08 20:23:18 +00002107 assert(RV && "Template argument record not found??");
2108 ItemType = RV->getType();
2109 ++ArgN;
2110 }
2111 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00002112 if (!Result.back()) {
2113 Result.clear();
2114 return;
2115 }
Bob Wilson7248f862009-11-22 04:24:42 +00002116
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002117 while (Lex.getCode() == tgtok::comma) {
2118 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00002119
Craig Topper011817a2014-04-09 04:50:04 +00002120 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002121 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00002122 if (ArgN >= TArgs.size()) {
2123 TokError("too many template arguments");
Matthias Braunc66e7552016-12-05 06:41:54 +00002124 Result.clear();
2125 return;
Bob Wilson7248f862009-11-22 04:24:42 +00002126 }
David Greene8618f952009-06-08 20:23:18 +00002127 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
2128 assert(RV && "Template argument record not found??");
2129 ItemType = RV->getType();
2130 ++ArgN;
2131 }
2132 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00002133 if (!Result.back()) {
2134 Result.clear();
2135 return;
2136 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002137 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002138}
2139
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002140/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
2141/// empty string on error. This can happen in a number of different context's,
2142/// including within a def or in the template args for a def (which which case
2143/// CurRec will be non-null) and within the template args for a multiclass (in
2144/// which case CurRec will be null, but CurMultiClass will be set). This can
2145/// also happen within a def that is within a multiclass, which will set both
2146/// CurRec and CurMultiClass.
2147///
2148/// Declaration ::= FIELD? Type ID ('=' Value)?
2149///
David Greenedb10e692011-10-19 13:02:42 +00002150Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002151 bool ParsingTemplateArgs) {
2152 // Read the field prefix if present.
2153 bool HasField = Lex.getCode() == tgtok::Field;
2154 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002155
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002156 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00002157 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00002158
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002159 if (Lex.getCode() != tgtok::Id) {
2160 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00002161 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002162 }
Bob Wilson7248f862009-11-22 04:24:42 +00002163
Chris Lattner526c8cb2009-06-21 03:39:35 +00002164 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00002165 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002166 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002167
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002168 if (ParsingTemplateArgs) {
Craig Toppera9642b42015-05-04 01:35:39 +00002169 if (CurRec)
David Greenedb10e692011-10-19 13:02:42 +00002170 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Craig Toppera9642b42015-05-04 01:35:39 +00002171 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002172 assert(CurMultiClass);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002173 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00002174 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
2175 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002176 }
Bob Wilson7248f862009-11-22 04:24:42 +00002177
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002178 // Add the value.
2179 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00002180 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00002181
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002182 // If a value is present, parse it.
2183 if (Lex.getCode() == tgtok::equal) {
2184 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002185 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00002186 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00002187 if (!Val ||
Craig Toppercfd81732016-01-04 03:15:08 +00002188 SetValue(CurRec, ValLoc, DeclName, None, Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00002189 // Return the name, even if an error is thrown. This is so that we can
2190 // continue to make some progress, even without the value having been
2191 // initialized.
2192 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002193 }
Bob Wilson7248f862009-11-22 04:24:42 +00002194
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002195 return DeclName;
2196}
2197
David Greenefb927af2012-02-22 16:09:41 +00002198/// ParseForeachDeclaration - Read a foreach declaration, returning
2199/// the name of the declared object or a NULL Init on error. Return
2200/// the name of the parsed initializer list through ForeachListName.
2201///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002202/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
2203/// ForeachDeclaration ::= ID '=' RangePiece
Nicolai Haehnle8aa9d582018-03-09 12:24:30 +00002204/// ForeachDeclaration ::= ID '=' Value
David Greenefb927af2012-02-22 16:09:41 +00002205///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002206VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00002207 if (Lex.getCode() != tgtok::Id) {
2208 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00002209 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00002210 }
2211
2212 Init *DeclName = StringInit::get(Lex.getCurStrVal());
2213 Lex.Lex();
2214
2215 // If a value is present, parse it.
2216 if (Lex.getCode() != tgtok::equal) {
2217 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00002218 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00002219 }
2220 Lex.Lex(); // Eat the '='
2221
Craig Topper011817a2014-04-09 04:50:04 +00002222 RecTy *IterType = nullptr;
Matthias Braunc66e7552016-12-05 06:41:54 +00002223 SmallVector<unsigned, 16> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00002224
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002225 switch (Lex.getCode()) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002226 case tgtok::IntVal: { // RangePiece.
2227 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00002228 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002229 break;
David Greenefb927af2012-02-22 16:09:41 +00002230 }
2231
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002232 case tgtok::l_brace: { // '{' RangeList '}'
2233 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00002234 ParseRangeList(Ranges);
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002235 if (Lex.getCode() != tgtok::r_brace) {
2236 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00002237 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002238 }
2239 Lex.Lex();
2240 break;
2241 }
Nicolai Haehnle8aa9d582018-03-09 12:24:30 +00002242
2243 default: {
2244 SMLoc ValueLoc = Lex.getLoc();
2245 Init *I = ParseValue(nullptr);
2246 if (!isa<ListInit>(I)) {
2247 std::string Type;
2248 if (TypedInit *TI = dyn_cast<TypedInit>(I))
2249 Type = (Twine("' of type '") + TI->getType()->getAsString()).str();
2250 Error(ValueLoc, "expected a list, got '" + I->getAsString() + Type + "'");
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002251 if (CurMultiClass)
2252 PrintNote({}, "references to multiclass template arguments cannot be "
2253 "resolved at this time");
Nicolai Haehnle8aa9d582018-03-09 12:24:30 +00002254 return nullptr;
2255 }
2256 ForeachListValue = dyn_cast<ListInit>(I);
2257 IterType = ForeachListValue->getElementType();
2258 break;
2259 }
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002260 }
David Greenefb927af2012-02-22 16:09:41 +00002261
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002262 if (!Ranges.empty()) {
2263 assert(!IterType && "Type already initialized?");
2264 IterType = IntRecTy::get();
2265 std::vector<Init*> Values;
Craig Topper8eb764c2015-06-02 06:19:28 +00002266 for (unsigned R : Ranges)
2267 Values.push_back(IntInit::get(R));
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002268 ForeachListValue = ListInit::get(Values, IterType);
2269 }
2270
2271 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00002272 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002273
2274 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00002275}
2276
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002277/// ParseTemplateArgList - Read a template argument list, which is a non-empty
2278/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
2279/// template args for a def, which may or may not be in a multiclass. If null,
2280/// these are the template args for a multiclass.
2281///
2282/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00002283///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002284bool TGParser::ParseTemplateArgList(Record *CurRec) {
2285 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
2286 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00002287
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002288 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00002289
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002290 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00002291 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00002292 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002293 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002294
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002295 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00002296
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002297 while (Lex.getCode() == tgtok::comma) {
2298 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00002299
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002300 // Read the following declarations.
2301 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00002302 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002303 return true;
2304 TheRecToAddTo->addTemplateArg(TemplArg);
2305 }
Bob Wilson7248f862009-11-22 04:24:42 +00002306
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002307 if (Lex.getCode() != tgtok::greater)
2308 return TokError("expected '>' at end of template argument list");
2309 Lex.Lex(); // eat the '>'.
2310 return false;
2311}
2312
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002313/// ParseBodyItem - Parse a single item at within the body of a def or class.
2314///
2315/// BodyItem ::= Declaration ';'
2316/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
2317bool TGParser::ParseBodyItem(Record *CurRec) {
2318 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00002319 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002320 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002321
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002322 if (Lex.getCode() != tgtok::semi)
2323 return TokError("expected ';' after declaration");
2324 Lex.Lex();
2325 return false;
2326 }
2327
2328 // LET ID OptionalRangeList '=' Value ';'
2329 if (Lex.Lex() != tgtok::Id)
2330 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00002331
Chris Lattner526c8cb2009-06-21 03:39:35 +00002332 SMLoc IdLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00002333 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002334 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00002335
Matthias Braunc66e7552016-12-05 06:41:54 +00002336 SmallVector<unsigned, 16> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00002337 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002338 return true;
2339 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002340
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002341 if (Lex.getCode() != tgtok::equal)
2342 return TokError("expected '=' in let expression");
2343 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002344
David Greene8618f952009-06-08 20:23:18 +00002345 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00002346 if (!Field)
Matthias Braun215ff842016-12-05 07:35:13 +00002347 return TokError("Value '" + FieldName->getValue() + "' unknown!");
David Greene8618f952009-06-08 20:23:18 +00002348
2349 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00002350
David Greeneaf8ee2c2011-07-29 22:43:06 +00002351 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00002352 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002353
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002354 if (Lex.getCode() != tgtok::semi)
2355 return TokError("expected ';' after let expression");
2356 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002357
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002358 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
2359}
2360
2361/// ParseBody - Read the body of a class or def. Return true on error, false on
2362/// success.
2363///
2364/// Body ::= ';'
2365/// Body ::= '{' BodyList '}'
2366/// BodyList BodyItem*
2367///
2368bool TGParser::ParseBody(Record *CurRec) {
2369 // If this is a null definition, just eat the semi and return.
2370 if (Lex.getCode() == tgtok::semi) {
2371 Lex.Lex();
2372 return false;
2373 }
Bob Wilson7248f862009-11-22 04:24:42 +00002374
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002375 if (Lex.getCode() != tgtok::l_brace)
2376 return TokError("Expected ';' or '{' to start body");
2377 // Eat the '{'.
2378 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002379
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002380 while (Lex.getCode() != tgtok::r_brace)
2381 if (ParseBodyItem(CurRec))
2382 return true;
2383
2384 // Eat the '}'.
2385 Lex.Lex();
2386 return false;
2387}
2388
Sean Silvacb1a75e2013-01-09 04:49:14 +00002389/// \brief Apply the current let bindings to \a CurRec.
2390/// \returns true on error, false otherwise.
2391bool TGParser::ApplyLetStack(Record *CurRec) {
Matthias Braunc66e7552016-12-05 06:41:54 +00002392 for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
Craig Topper8eb764c2015-06-02 06:19:28 +00002393 for (LetRecord &LR : LetInfo)
2394 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
Sean Silvacb1a75e2013-01-09 04:49:14 +00002395 return true;
2396 return false;
2397}
2398
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002399/// ParseObjectBody - Parse the body of a def or class. This consists of an
2400/// optional ClassList followed by a Body. CurRec is the current def or class
2401/// that is being parsed.
2402///
2403/// ObjectBody ::= BaseClassList Body
2404/// BaseClassList ::= /*empty*/
2405/// BaseClassList ::= ':' BaseClassListNE
2406/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
2407///
2408bool TGParser::ParseObjectBody(Record *CurRec) {
2409 // If there is a baseclass list, read it.
2410 if (Lex.getCode() == tgtok::colon) {
2411 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002412
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002413 // Read all of the subclasses.
2414 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002415 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002416 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002417 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002418
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002419 // Add it.
2420 if (AddSubClass(CurRec, SubClass))
2421 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002422
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002423 if (Lex.getCode() != tgtok::comma) break;
2424 Lex.Lex(); // eat ','.
2425 SubClass = ParseSubClassReference(CurRec, false);
2426 }
2427 }
2428
Sean Silvacb1a75e2013-01-09 04:49:14 +00002429 if (ApplyLetStack(CurRec))
2430 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002431
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002432 return ParseBody(CurRec);
2433}
2434
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002435/// ParseDef - Parse and return a top level or multiclass def, return the record
2436/// corresponding to it. This returns null on error.
2437///
2438/// DefInst ::= DEF ObjectName ObjectBody
2439///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002440bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00002441 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002442 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00002443 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002444
2445 // Parse ObjectName and make a record for it.
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002446 std::unique_ptr<Record> CurRec;
Jordan Roseabdd99b2013-01-10 18:50:05 +00002447 Init *Name = ParseObjectName(CurMultiClass);
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002448 if (!Name)
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002449 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002450
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002451 if (isa<UnsetInit>(Name))
2452 CurRec = make_unique<Record>(Records.getNewAnonymousName(), DefLoc, Records,
2453 /*Anonymous=*/true);
2454 else
2455 CurRec = make_unique<Record>(Name, DefLoc, Records);
Bob Wilson7248f862009-11-22 04:24:42 +00002456
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002457 if (ParseObjectBody(CurRec.get()))
2458 return true;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002459
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002460 return addDef(std::move(CurRec), nullptr);
Nicolai Haehnlefcd65252018-03-09 12:24:42 +00002461}
2462
2463/// ParseDefset - Parse a defset statement.
2464///
2465/// Defset ::= DEFSET Type Id '=' '{' ObjectList '}'
2466///
2467bool TGParser::ParseDefset() {
2468 assert(Lex.getCode() == tgtok::Defset);
2469 Lex.Lex(); // Eat the 'defset' token
2470
2471 DefsetRecord Defset;
2472 Defset.Loc = Lex.getLoc();
2473 RecTy *Type = ParseType();
2474 if (!Type)
2475 return true;
2476 if (!isa<ListRecTy>(Type))
2477 return Error(Defset.Loc, "expected list type");
2478 Defset.EltTy = cast<ListRecTy>(Type)->getElementType();
2479
2480 if (Lex.getCode() != tgtok::Id)
2481 return TokError("expected identifier");
2482 StringInit *DeclName = StringInit::get(Lex.getCurStrVal());
2483 if (Records.getGlobal(DeclName->getValue()))
2484 return TokError("def or global variable of this name already exists");
2485
2486 if (Lex.Lex() != tgtok::equal) // Eat the identifier
2487 return TokError("expected '='");
2488 if (Lex.Lex() != tgtok::l_brace) // Eat the '='
2489 return TokError("expected '{'");
2490 SMLoc BraceLoc = Lex.getLoc();
2491 Lex.Lex(); // Eat the '{'
2492
2493 Defsets.push_back(&Defset);
2494 bool Err = ParseObjectList(nullptr);
2495 Defsets.pop_back();
2496 if (Err)
2497 return true;
2498
2499 if (Lex.getCode() != tgtok::r_brace) {
2500 TokError("expected '}' at end of defset");
2501 return Error(BraceLoc, "to match this '{'");
2502 }
2503 Lex.Lex(); // Eat the '}'
2504
2505 Records.addExtraGlobal(DeclName->getValue(),
2506 ListInit::get(Defset.Elements, Defset.EltTy));
2507 return false;
2508}
2509
David Greenefb927af2012-02-22 16:09:41 +00002510/// ParseForeach - Parse a for statement. Return the record corresponding
2511/// to it. This returns true on error.
2512///
2513/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2514/// Foreach ::= FOREACH Declaration IN Object
2515///
2516bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2517 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2518 Lex.Lex(); // Eat the 'for' token.
2519
2520 // Make a temporary object to record items associated with the for
2521 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002522 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002523 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002524 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002525 return TokError("expected declaration in for");
2526
2527 if (Lex.getCode() != tgtok::In)
2528 return TokError("Unknown tok");
2529 Lex.Lex(); // Eat the in
2530
2531 // Create a loop object and remember it.
2532 Loops.push_back(ForeachLoop(IterName, ListValue));
2533
2534 if (Lex.getCode() != tgtok::l_brace) {
2535 // FOREACH Declaration IN Object
2536 if (ParseObject(CurMultiClass))
2537 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002538 } else {
David Greenefb927af2012-02-22 16:09:41 +00002539 SMLoc BraceLoc = Lex.getLoc();
2540 // Otherwise, this is a group foreach.
2541 Lex.Lex(); // eat the '{'.
2542
2543 // Parse the object list.
2544 if (ParseObjectList(CurMultiClass))
2545 return true;
2546
2547 if (Lex.getCode() != tgtok::r_brace) {
2548 TokError("expected '}' at end of foreach command");
2549 return Error(BraceLoc, "to match this '{'");
2550 }
2551 Lex.Lex(); // Eat the }
2552 }
2553
2554 // We've processed everything in this loop.
2555 Loops.pop_back();
2556
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002557 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002558}
2559
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002560/// ParseClass - Parse a tblgen class definition.
2561///
2562/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2563///
2564bool TGParser::ParseClass() {
2565 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2566 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002567
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002568 if (Lex.getCode() != tgtok::Id)
2569 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002570
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002571 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2572 if (CurRec) {
2573 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002574 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002575 !CurRec->getSuperClasses().empty() ||
2576 !CurRec->getTemplateArgs().empty())
Craig Topper85c07002015-04-30 05:54:22 +00002577 return TokError("Class '" + CurRec->getNameInitAsString() +
2578 "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002579 } else {
2580 // If this is the first reference to this class, create and add it.
Hans Wennborgffbbd532014-11-30 00:31:49 +00002581 auto NewRec =
2582 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
Craig Toppercdab2322014-11-29 05:52:51 +00002583 CurRec = NewRec.get();
2584 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002585 }
2586 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002587
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002588 // If there are template args, parse them.
2589 if (Lex.getCode() == tgtok::less)
2590 if (ParseTemplateArgList(CurRec))
2591 return true;
2592
2593 // Finally, parse the object body.
2594 return ParseObjectBody(CurRec);
2595}
2596
2597/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2598/// of LetRecords.
2599///
2600/// LetList ::= LetItem (',' LetItem)*
2601/// LetItem ::= ID OptionalRangeList '=' Value
2602///
Matthias Braunc66e7552016-12-05 06:41:54 +00002603void TGParser::ParseLetList(SmallVectorImpl<LetRecord> &Result) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002604 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002605 if (Lex.getCode() != tgtok::Id) {
2606 TokError("expected identifier in let definition");
Matthias Braunc66e7552016-12-05 06:41:54 +00002607 Result.clear();
2608 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002609 }
Matthias Braunc66e7552016-12-05 06:41:54 +00002610
Matthias Braun215ff842016-12-05 07:35:13 +00002611 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattner526c8cb2009-06-21 03:39:35 +00002612 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002613 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002614
2615 // Check for an optional RangeList.
Matthias Braunc66e7552016-12-05 06:41:54 +00002616 SmallVector<unsigned, 16> Bits;
2617 if (ParseOptionalRangeList(Bits)) {
2618 Result.clear();
2619 return;
2620 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002621 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002622
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002623 if (Lex.getCode() != tgtok::equal) {
2624 TokError("expected '=' in let expression");
Matthias Braunc66e7552016-12-05 06:41:54 +00002625 Result.clear();
2626 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002627 }
2628 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002629
Craig Topper011817a2014-04-09 04:50:04 +00002630 Init *Val = ParseValue(nullptr);
Matthias Braunc66e7552016-12-05 06:41:54 +00002631 if (!Val) {
2632 Result.clear();
2633 return;
2634 }
Bob Wilson7248f862009-11-22 04:24:42 +00002635
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002636 // Now that we have everything, add the record.
Matthias Braun215ff842016-12-05 07:35:13 +00002637 Result.emplace_back(Name, Bits, Val, NameLoc);
Bob Wilson7248f862009-11-22 04:24:42 +00002638
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002639 if (Lex.getCode() != tgtok::comma)
Matthias Braunc66e7552016-12-05 06:41:54 +00002640 return;
Bob Wilson7248f862009-11-22 04:24:42 +00002641 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002642 }
2643}
2644
2645/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002646/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002647///
2648/// Object ::= LET LetList IN '{' ObjectList '}'
2649/// Object ::= LET LetList IN Object
2650///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002651bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002652 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2653 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002654
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002655 // Add this entry to the let stack.
Matthias Braunc66e7552016-12-05 06:41:54 +00002656 SmallVector<LetRecord, 8> LetInfo;
2657 ParseLetList(LetInfo);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002658 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002659 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002660
2661 if (Lex.getCode() != tgtok::In)
2662 return TokError("expected 'in' at end of top-level 'let'");
2663 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002664
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002665 // If this is a scalar let, just handle it now
2666 if (Lex.getCode() != tgtok::l_brace) {
2667 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002668 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002669 return true;
2670 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002671 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002672 // Otherwise, this is a group let.
2673 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002674
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002675 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002676 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002677 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002678
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002679 if (Lex.getCode() != tgtok::r_brace) {
2680 TokError("expected '}' at end of top level let command");
2681 return Error(BraceLoc, "to match this '{'");
2682 }
2683 Lex.Lex();
2684 }
Bob Wilson7248f862009-11-22 04:24:42 +00002685
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002686 // Outside this let scope, this let block is not active.
2687 LetStack.pop_back();
2688 return false;
2689}
2690
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002691/// ParseMultiClass - Parse a multiclass definition.
2692///
Bob Wilson3d948162009-04-28 19:41:44 +00002693/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002694/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2695/// MultiClassObject ::= DefInst
2696/// MultiClassObject ::= MultiClassInst
2697/// MultiClassObject ::= DefMInst
2698/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2699/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002700///
2701bool TGParser::ParseMultiClass() {
2702 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2703 Lex.Lex(); // Eat the multiclass token.
2704
2705 if (Lex.getCode() != tgtok::Id)
2706 return TokError("expected identifier after multiclass for name");
2707 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002708
Craig Topper7adf2bf2014-12-11 05:25:30 +00002709 auto Result =
2710 MultiClasses.insert(std::make_pair(Name,
2711 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2712
2713 if (!Result.second)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002714 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002715
Craig Topper7adf2bf2014-12-11 05:25:30 +00002716 CurMultiClass = Result.first->second.get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002717 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002718
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002719 // If there are template args, parse them.
2720 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002721 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002722 return true;
2723
David Greene7049e792009-04-24 16:55:41 +00002724 bool inherits = false;
2725
David Greene753ed8f2009-04-22 16:42:54 +00002726 // If there are submulticlasses, parse them.
2727 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002728 inherits = true;
2729
David Greene753ed8f2009-04-22 16:42:54 +00002730 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002731
David Greene753ed8f2009-04-22 16:42:54 +00002732 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002733 SubMultiClassReference SubMultiClass =
2734 ParseSubMultiClassReference(CurMultiClass);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002735 while (true) {
David Greene753ed8f2009-04-22 16:42:54 +00002736 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002737 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002738
David Greene753ed8f2009-04-22 16:42:54 +00002739 // Add it.
2740 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2741 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002742
David Greene753ed8f2009-04-22 16:42:54 +00002743 if (Lex.getCode() != tgtok::comma) break;
2744 Lex.Lex(); // eat ','.
2745 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2746 }
2747 }
2748
David Greene7049e792009-04-24 16:55:41 +00002749 if (Lex.getCode() != tgtok::l_brace) {
2750 if (!inherits)
2751 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002752 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002753 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002754 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002755 } else {
David Greene7049e792009-04-24 16:55:41 +00002756 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2757 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002758
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002759 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002760 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002761 default:
Nicolai Haehnlefcd65252018-03-09 12:24:42 +00002762 return TokError("expected 'let', 'def', 'defm' or 'foreach' in "
2763 "multiclass body");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002764 case tgtok::Let:
2765 case tgtok::Def:
2766 case tgtok::Defm:
2767 case tgtok::Foreach:
2768 if (ParseObject(CurMultiClass))
2769 return true;
2770 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002771 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002772 }
David Greene7049e792009-04-24 16:55:41 +00002773 Lex.Lex(); // eat the '}'.
2774 }
Bob Wilson7248f862009-11-22 04:24:42 +00002775
Craig Topper011817a2014-04-09 04:50:04 +00002776 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002777 return false;
2778}
2779
2780/// ParseDefm - Parse the instantiation of a multiclass.
2781///
2782/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2783///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002784bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002785 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002786 Lex.Lex(); // eat the defm
David Greene2affd672011-10-19 13:04:29 +00002787
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002788 Init *DefmName = ParseObjectName(CurMultiClass);
2789 if (!DefmName)
2790 return true;
2791 if (isa<UnsetInit>(DefmName))
2792 DefmName = Records.getNewAnonymousName();
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002793
Chris Lattner7538ed82010-10-05 22:51:56 +00002794 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002795 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002796
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002797 // Keep track of the new generated record definitions.
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002798 SmallVector<std::unique_ptr<Record>, 8> NewRecDefs;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002799
2800 // This record also inherits from a regular class (non-multiclass)?
2801 bool InheritFromClass = false;
2802
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002803 // eat the colon.
2804 Lex.Lex();
2805
Chris Lattner526c8cb2009-06-21 03:39:35 +00002806 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002807 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002808
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002809 while (true) {
Craig Topper011817a2014-04-09 04:50:04 +00002810 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002811
2812 // To instantiate a multiclass, we need to first get the multiclass, then
2813 // instantiate each def contained in the multiclass with the SubClassRef
2814 // template parameters.
Craig Topper7adf2bf2014-12-11 05:25:30 +00002815 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper4ca40012014-11-30 01:20:17 +00002816 assert(MC && "Didn't lookup multiclass correctly?");
Matthias Braunc66e7552016-12-05 06:41:54 +00002817 ArrayRef<Init*> TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002818
2819 // Verify that the correct number of template arguments were specified.
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002820 ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002821 if (TArgs.size() < TemplateVals.size())
2822 return Error(SubClassLoc,
2823 "more template args specified than multiclass expects");
2824
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002825 DenseMap<Init *, Init *> TemplateArgs;
2826 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2827 if (i < TemplateVals.size()) {
2828 TemplateArgs.insert({TArgs[i], TemplateVals[i]});
2829 } else {
2830 Init *Default = MC->Rec.getValue(TArgs[i])->getValue();
2831 if (!Default->isComplete()) {
2832 return Error(SubClassLoc,
2833 "value not specified for template argument #" +
2834 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
2835 ") of multiclass '" + MC->Rec.getNameInitAsString() +
2836 "'");
2837 }
2838 TemplateArgs.insert({TArgs[i], Default});
2839 }
David Greenef00919a2009-04-22 22:17:51 +00002840 }
2841
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002842 // Loop over all the def's in the multiclass, instantiating each one.
2843 for (const std::unique_ptr<Record> &DefProto : MC->DefPrototypes) {
2844 bool ResolveName = true;
2845 auto CurRec = make_unique<Record>(*DefProto);
2846 CurRec->appendLoc(SubClassLoc);
2847
2848 if (StringInit *NameString =
2849 dyn_cast<StringInit>(CurRec->getNameInit())) {
2850 // We have a fully expanded string so there are no operators to
2851 // resolve. We should concatenate the given prefix and name.
2852 //
2853 // TODO: This MUST happen before template argument resolution. This
2854 // does not make sense and should be changed, but at the time of
2855 // writing, there are existing .td files which rely on this
2856 // implementation detail. It's a bad idea and should be fixed.
2857 // See test/TableGen/name-resolution-consistency.td for some
2858 // examples.
2859 CurRec->setName(BinOpInit::getStrConcat(DefmName, NameString));
2860 ResolveName = false;
2861 }
2862
2863 MapResolver R(CurRec.get());
2864
2865 if (ResolveName) {
2866 // If the proto's name wasn't resolved, we probably have a reference to
2867 // NAME and need to replace it.
2868 //
2869 // TODO: Whether the name is resolved is basically determined by magic.
2870 // Unfortunately, existing .td files depend on it.
2871 R.set(StringInit::get("NAME"), DefmName);
2872 }
2873
2874 for (const auto &TArg : TemplateArgs)
2875 R.set(TArg.first, TArg.second);
2876
2877 CurRec->resolveReferences(R);
2878
2879 NewRecDefs.emplace_back(std::move(CurRec));
2880 }
David Greenedb445972011-10-05 22:42:07 +00002881
David Greenef00919a2009-04-22 22:17:51 +00002882 if (Lex.getCode() != tgtok::comma) break;
2883 Lex.Lex(); // eat ','.
2884
Craig Topper998a39a2013-08-20 04:22:09 +00002885 if (Lex.getCode() != tgtok::Id)
2886 return TokError("expected identifier");
2887
David Greenef00919a2009-04-22 22:17:51 +00002888 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002889
2890 // A defm can inherit from regular classes (non-multiclass) as
2891 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002892 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002893
2894 if (InheritFromClass)
2895 break;
2896
Craig Topper011817a2014-04-09 04:50:04 +00002897 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002898 }
2899
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002900 if (InheritFromClass) {
2901 // Process all the classes to inherit as if they were part of a
2902 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002903 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002904 while (true) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002905 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002906 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002907
2908 // Get the expanded definition prototypes and teach them about
2909 // the record values the current class to inherit has
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002910 for (const auto &CurRec : NewRecDefs) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002911 // Add it.
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002912 if (AddSubClass(CurRec.get(), SubClass))
Sean Silvacb1a75e2013-01-09 04:49:14 +00002913 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002914 }
2915
2916 if (Lex.getCode() != tgtok::comma) break;
2917 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002918 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002919 }
2920 }
2921
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002922 for (auto &CurRec : NewRecDefs) {
2923 if (ApplyLetStack(CurRec.get()))
2924 return true;
2925
2926 addDef(std::move(CurRec), DefmName);
Nicolai Haehnle0f529882018-03-06 13:48:47 +00002927 }
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002928
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002929 if (Lex.getCode() != tgtok::semi)
2930 return TokError("expected ';' at end of defm");
2931 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002932
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002933 return false;
2934}
2935
2936/// ParseObject
2937/// Object ::= ClassInst
2938/// Object ::= DefInst
2939/// Object ::= MultiClassInst
2940/// Object ::= DefMInst
2941/// Object ::= LETCommand '{' ObjectList '}'
2942/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002943bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002944 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002945 default:
Nicolai Haehnlefcd65252018-03-09 12:24:42 +00002946 return TokError("Expected class, def, defm, defset, multiclass, let or "
2947 "foreach");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002948 case tgtok::Let: return ParseTopLevelLet(MC);
2949 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002950 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002951 case tgtok::Defm: return ParseDefm(MC);
Nicolai Haehnlefcd65252018-03-09 12:24:42 +00002952 case tgtok::Defset:
2953 if (MC)
2954 return TokError("defset is not allowed inside multiclass");
2955 return ParseDefset();
Nicolai Haehnlea511ddd2018-03-14 11:01:01 +00002956 case tgtok::Class:
2957 if (MC)
2958 return TokError("class is not allowed inside multiclass");
2959 if (!Loops.empty())
2960 return TokError("class is not allowed inside foreach loop");
2961 return ParseClass();
2962 case tgtok::MultiClass:
2963 if (!Loops.empty())
2964 return TokError("multiclass is not allowed inside foreach loop");
2965 return ParseMultiClass();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002966 }
2967}
2968
2969/// ParseObjectList
2970/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002971bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002972 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002973 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002974 return true;
2975 }
2976 return false;
2977}
2978
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002979bool TGParser::ParseFile() {
2980 Lex.Lex(); // Prime the lexer.
2981 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002982
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002983 // If we have unread input at the end of the file, report it.
2984 if (Lex.getCode() == tgtok::Eof)
2985 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002986
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002987 return TokError("Unexpected input at top level");
2988}