blob: 79f0799197b210a233bf773525ab314398720e1b [file] [log] [blame]
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001//===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Parser for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
14#include "TGParser.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000015#include "llvm/ADT/None.h"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000016#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/SmallVector.h"
Chris Lattnerf4127dd2007-11-22 20:49:04 +000018#include "llvm/ADT/StringExtras.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000019#include "llvm/Support/Casting.h"
20#include "llvm/Support/Compiler.h"
21#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/TableGen/Record.h"
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000024#include <algorithm>
Eugene Zelenko33d7b762016-08-23 17:14:32 +000025#include <cassert>
26#include <cstdint>
27
Chris Lattnerf4127dd2007-11-22 20:49:04 +000028using namespace llvm;
29
30//===----------------------------------------------------------------------===//
31// Support Code for the Semantic Actions.
32//===----------------------------------------------------------------------===//
33
34namespace llvm {
Eugene Zelenko33d7b762016-08-23 17:14:32 +000035
Chris Lattnerf4127dd2007-11-22 20:49:04 +000036struct SubClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000037 SMRange RefRange;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000038 Record *Rec;
Matthias Braunc66e7552016-12-05 06:41:54 +000039 SmallVector<Init*, 4> TemplateArgs;
Eugene Zelenko33d7b762016-08-23 17:14:32 +000040
Craig Topper011817a2014-04-09 04:50:04 +000041 SubClassReference() : Rec(nullptr) {}
David Greene7049e792009-04-24 16:55:41 +000042
Craig Topper011817a2014-04-09 04:50:04 +000043 bool isInvalid() const { return Rec == nullptr; }
Chris Lattnerf4127dd2007-11-22 20:49:04 +000044};
David Greene753ed8f2009-04-22 16:42:54 +000045
46struct SubMultiClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000047 SMRange RefRange;
David Greene753ed8f2009-04-22 16:42:54 +000048 MultiClass *MC;
Matthias Braunc66e7552016-12-05 06:41:54 +000049 SmallVector<Init*, 4> TemplateArgs;
Eugene Zelenko33d7b762016-08-23 17:14:32 +000050
Craig Topper011817a2014-04-09 04:50:04 +000051 SubMultiClassReference() : MC(nullptr) {}
Bob Wilson3d948162009-04-28 19:41:44 +000052
Craig Topper011817a2014-04-09 04:50:04 +000053 bool isInvalid() const { return MC == nullptr; }
David Greene7049e792009-04-24 16:55:41 +000054 void dump() const;
David Greene753ed8f2009-04-22 16:42:54 +000055};
David Greene7049e792009-04-24 16:55:41 +000056
Aaron Ballman615eb472017-10-15 14:32:27 +000057#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +000058LLVM_DUMP_METHOD void SubMultiClassReference::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000059 errs() << "Multiclass:\n";
Bob Wilson7248f862009-11-22 04:24:42 +000060
David Greene7049e792009-04-24 16:55:41 +000061 MC->dump();
Bob Wilson7248f862009-11-22 04:24:42 +000062
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000063 errs() << "Template args:\n";
Craig Toppera9642b42015-05-04 01:35:39 +000064 for (Init *TA : TemplateArgs)
Craig Toppereb4d7c62015-04-29 04:43:36 +000065 TA->dump();
David Greene7049e792009-04-24 16:55:41 +000066}
Matthias Braun25bcaba2017-01-28 02:47:46 +000067#endif
David Greene7049e792009-04-24 16:55:41 +000068
Chris Lattnerf4127dd2007-11-22 20:49:04 +000069} // end namespace llvm
70
Chris Lattner526c8cb2009-06-21 03:39:35 +000071bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Craig Topper011817a2014-04-09 04:50:04 +000072 if (!CurRec)
Chris Lattnerf4127dd2007-11-22 20:49:04 +000073 CurRec = &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +000074
Jakob Stoklund Olesen9d1c5ee2012-01-13 03:16:35 +000075 if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000076 // The value already exists in the class, treat this as a set.
77 if (ERV->setValue(RV.getValue()))
78 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
79 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson7248f862009-11-22 04:24:42 +000080 "previous definition of type '" +
Chris Lattnerf4127dd2007-11-22 20:49:04 +000081 ERV->getType()->getAsString() + "'");
82 } else {
83 CurRec->addValue(RV);
84 }
85 return false;
86}
87
88/// SetValue -
89/// Return true on error, false on success.
David Greene3ca42122011-10-19 13:02:39 +000090bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
Craig Toppercfd81732016-01-04 03:15:08 +000091 ArrayRef<unsigned> BitList, Init *V,
Craig Topper1e23ed92016-01-04 03:05:14 +000092 bool AllowSelfAssignment) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000093 if (!V) return false;
94
Craig Topper011817a2014-04-09 04:50:04 +000095 if (!CurRec) CurRec = &CurMultiClass->Rec;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000096
97 RecordVal *RV = CurRec->getValue(ValName);
Craig Topper011817a2014-04-09 04:50:04 +000098 if (!RV)
Craig Topper85c07002015-04-30 05:54:22 +000099 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
100 "' unknown!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000101
102 // Do not allow assignments like 'X = X'. This will just cause infinite loops
103 // in the resolution machinery.
104 if (BitList.empty())
Sean Silvafb509ed2012-10-10 20:24:43 +0000105 if (VarInit *VI = dyn_cast<VarInit>(V))
Craig Topper1e23ed92016-01-04 03:05:14 +0000106 if (VI->getNameInit() == ValName && !AllowSelfAssignment)
Nicolai Haehnlef19083d2018-02-22 15:26:21 +0000107 return Error(Loc, "Recursion / self-assignment forbidden");
Bob Wilson7248f862009-11-22 04:24:42 +0000108
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000109 // If we are assigning to a subset of the bits in the value... then we must be
110 // assigning to a field of BitsRecTy, which must have a BitsInit
111 // initializer.
112 //
113 if (!BitList.empty()) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000114 BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue());
Craig Topper011817a2014-04-09 04:50:04 +0000115 if (!CurVal)
Craig Topper85c07002015-04-30 05:54:22 +0000116 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
117 "' is not a bits type");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000118
119 // Convert the incoming value to a bits type of the appropriate size...
Nicolai Haehnledfda9dc2018-03-06 13:48:39 +0000120 Init *BI = V->getCastTo(BitsRecTy::get(BitList.size()));
Craig Toppera9642b42015-05-04 01:35:39 +0000121 if (!BI)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000122 return Error(Loc, "Initializer is not compatible with bit range");
Bob Wilson7248f862009-11-22 04:24:42 +0000123
David Greeneaf8ee2c2011-07-29 22:43:06 +0000124 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000125
126 // Loop over bits, assigning values as appropriate.
127 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
128 unsigned Bit = BitList[i];
David Greeneb3da8122011-07-29 19:07:00 +0000129 if (NewBits[Bit])
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000130 return Error(Loc, "Cannot set bit #" + Twine(Bit) + " of value '" +
David Greene3ca42122011-10-19 13:02:39 +0000131 ValName->getAsUnquotedString() + "' more than once");
Nicolai Haehnledfda9dc2018-03-06 13:48:39 +0000132 NewBits[Bit] = BI->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000133 }
134
135 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
Craig Topper011817a2014-04-09 04:50:04 +0000136 if (!NewBits[i])
David Greeneb3da8122011-07-29 19:07:00 +0000137 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000138
David Greenee32ebf22011-07-29 19:07:07 +0000139 V = BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000140 }
141
Pete Cooper040c6a62014-07-31 01:43:57 +0000142 if (RV->setValue(V)) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000143 std::string InitType;
Craig Toppera9642b42015-05-04 01:35:39 +0000144 if (BitsInit *BI = dyn_cast<BitsInit>(V))
Pete Cooper040c6a62014-07-31 01:43:57 +0000145 InitType = (Twine("' of type bit initializer with length ") +
146 Twine(BI->getNumBits())).str();
Nicolai Haehnlef19083d2018-02-22 15:26:21 +0000147 else if (TypedInit *TI = dyn_cast<TypedInit>(V))
148 InitType = (Twine("' of type '") + TI->getType()->getAsString()).str();
Craig Topper85c07002015-04-30 05:54:22 +0000149 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
Nicolai Haehnlef19083d2018-02-22 15:26:21 +0000150 "' of type '" + RV->getType()->getAsString() +
151 "' is incompatible with initializer '" +
152 V->getAsString() + InitType + "'");
Pete Cooper040c6a62014-07-31 01:43:57 +0000153 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000154 return false;
155}
156
157/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
158/// args as SubClass's template arguments.
Cedric Venetd1e179d2009-02-14 16:06:42 +0000159bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000160 Record *SC = SubClass.Rec;
161 // Add all of the values in the subclass into the current class.
Craig Topper8eb764c2015-06-02 06:19:28 +0000162 for (const RecordVal &Val : SC->getValues())
163 if (AddValue(CurRec, SubClass.RefRange.Start, Val))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000164 return true;
165
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000166 ArrayRef<Init *> TArgs = SC->getTemplateArgs();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000167
168 // Ensure that an appropriate number of template arguments are specified.
169 if (TArgs.size() < SubClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000170 return Error(SubClass.RefRange.Start,
171 "More template args specified than expected");
Bob Wilson7248f862009-11-22 04:24:42 +0000172
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000173 // Loop over all of the template arguments, setting them to the specified
174 // value or leaving them as the default if necessary.
Nicolai Haehnleb0cf9e92018-03-05 15:21:11 +0000175 MapResolver R(CurRec);
176
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000177 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
178 if (i < SubClass.TemplateArgs.size()) {
179 // If a value is specified for this template arg, set it now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000180 if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000181 None, SubClass.TemplateArgs[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000182 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000183 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000184 return Error(SubClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000185 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000186 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000187 ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000188 }
Nicolai Haehnleb0cf9e92018-03-05 15:21:11 +0000189
190 R.set(TArgs[i], CurRec->getValue(TArgs[i])->getValue());
191
192 CurRec->removeValue(TArgs[i]);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000193 }
194
Nicolai Haehnleb0cf9e92018-03-05 15:21:11 +0000195 CurRec->resolveReferences(R);
196
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000197 // Since everything went well, we can now set the "superclass" list for the
198 // current record.
Craig Topper0e41d0b2016-01-18 19:52:37 +0000199 ArrayRef<std::pair<Record *, SMRange>> SCs = SC->getSuperClasses();
200 for (const auto &SCPair : SCs) {
201 if (CurRec->isSubClassOf(SCPair.first))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000202 return Error(SubClass.RefRange.Start,
Craig Topper0e41d0b2016-01-18 19:52:37 +0000203 "Already subclass of '" + SCPair.first->getName() + "'!\n");
204 CurRec->addSuperClass(SCPair.first, SCPair.second);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000205 }
Nicolai Haehnlec10570f2018-02-23 11:31:49 +0000206
207 if (CurRec->isSubClassOf(SC))
208 return Error(SubClass.RefRange.Start,
209 "Already subclass of '" + SC->getName() + "'!\n");
210 CurRec->addSuperClass(SC, SubClass.RefRange);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000211 return false;
212}
213
David Greene753ed8f2009-04-22 16:42:54 +0000214/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilsonf71e6562009-04-30 18:26:19 +0000215/// CurMC, resolving its template args as SubMultiClass's
David Greene753ed8f2009-04-22 16:42:54 +0000216/// template arguments.
Bob Wilsonf71e6562009-04-30 18:26:19 +0000217bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson92ab8202009-04-30 17:46:20 +0000218 SubMultiClassReference &SubMultiClass) {
David Greene753ed8f2009-04-22 16:42:54 +0000219 MultiClass *SMC = SubMultiClass.MC;
Bob Wilsonf71e6562009-04-30 18:26:19 +0000220 Record *CurRec = &CurMC->Rec;
David Greene753ed8f2009-04-22 16:42:54 +0000221
David Greene753ed8f2009-04-22 16:42:54 +0000222 // Add all of the values in the subclass into the current class.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000223 for (const auto &SMCVal : SMC->Rec.getValues())
224 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000225 return true;
226
Craig Toppera4ea4b02014-11-30 00:24:32 +0000227 unsigned newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000228
David Greene753ed8f2009-04-22 16:42:54 +0000229 // Add all of the defs in the subclass into the current multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000230 for (const std::unique_ptr<Record> &R : SMC->DefPrototypes) {
David Greene753ed8f2009-04-22 16:42:54 +0000231 // Clone the def and add it to the current multiclass
Craig Toppereb4d7c62015-04-29 04:43:36 +0000232 auto NewDef = make_unique<Record>(*R);
David Greene753ed8f2009-04-22 16:42:54 +0000233
234 // Add all of the values in the superclass into the current def.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000235 for (const auto &MCVal : CurRec->getValues())
236 if (AddValue(NewDef.get(), SubMultiClass.RefRange.Start, MCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000237 return true;
238
Craig Topperc3504c42014-12-11 05:25:33 +0000239 CurMC->DefPrototypes.push_back(std::move(NewDef));
David Greene753ed8f2009-04-22 16:42:54 +0000240 }
Bob Wilson3d948162009-04-28 19:41:44 +0000241
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000242 ArrayRef<Init *> SMCTArgs = SMC->Rec.getTemplateArgs();
David Greene753ed8f2009-04-22 16:42:54 +0000243
David Greene7049e792009-04-24 16:55:41 +0000244 // Ensure that an appropriate number of template arguments are
245 // specified.
David Greene753ed8f2009-04-22 16:42:54 +0000246 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000247 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000248 "More template args specified than expected");
Bob Wilson3d948162009-04-28 19:41:44 +0000249
David Greene753ed8f2009-04-22 16:42:54 +0000250 // Loop over all of the template arguments, setting them to the specified
251 // value or leaving them as the default if necessary.
Nicolai Haehnle1faf8682018-03-05 15:21:15 +0000252 MapResolver CurRecResolver(CurRec);
253
David Greene753ed8f2009-04-22 16:42:54 +0000254 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
255 if (i < SubMultiClass.TemplateArgs.size()) {
David Greene7049e792009-04-24 16:55:41 +0000256 // If a value is specified for this template arg, set it in the
257 // superclass now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000258 if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000259 None, SubMultiClass.TemplateArgs[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000260 return true;
261
David Greene7049e792009-04-24 16:55:41 +0000262 // If a value is specified for this template arg, set it in the
263 // new defs now.
Craig Topperc3504c42014-12-11 05:25:33 +0000264 for (const auto &Def :
265 makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
266 if (SetValue(Def.get(), SubMultiClass.RefRange.Start, SMCTArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000267 None, SubMultiClass.TemplateArgs[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000268 return true;
David Greene753ed8f2009-04-22 16:42:54 +0000269 }
270 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000271 return Error(SubMultiClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000272 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000273 Twine(i) + " (" + SMCTArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000274 ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000275 }
Nicolai Haehnle1faf8682018-03-05 15:21:15 +0000276
277 CurRecResolver.set(SMCTArgs[i], CurRec->getValue(SMCTArgs[i])->getValue());
278
279 CurRec->removeValue(SMCTArgs[i]);
280 }
281
282 CurRec->resolveReferences(CurRecResolver);
283
284 for (const auto &Def :
285 makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
286 MapResolver R(Def.get());
287
288 for (Init *SMCTArg : SMCTArgs) {
289 R.set(SMCTArg, Def->getValue(SMCTArg)->getValue());
290 Def->removeValue(SMCTArg);
291 }
292
293 Def->resolveReferences(R);
David Greene753ed8f2009-04-22 16:42:54 +0000294 }
295
296 return false;
297}
298
David Greenefb927af2012-02-22 16:09:41 +0000299/// ProcessForeachDefs - Given a record, apply all of the variable
300/// values in all surrounding foreach loops, creating new records for
301/// each combination of values.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000302bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
303 if (Loops.empty())
304 return false;
305
David Greenefb927af2012-02-22 16:09:41 +0000306 // We want to instantiate a new copy of CurRec for each combination
307 // of nested loop iterator values. We don't want top instantiate
308 // any copies until we have values for each loop iterator.
309 IterSet IterVals;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000310 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000311}
312
313/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
314/// apply each of the variable values in this loop and then process
315/// subloops.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000316bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
317 // Recursively build a tuple of iterator values.
318 if (IterVals.size() != Loops.size()) {
319 assert(IterVals.size() < Loops.size());
320 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silvafb509ed2012-10-10 20:24:43 +0000321 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Craig Topper011817a2014-04-09 04:50:04 +0000322 if (!List) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000323 Error(Loc, "Loop list is not a list");
324 return true;
325 }
David Greenefb927af2012-02-22 16:09:41 +0000326
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000327 // Process each value.
Craig Topper664f6a02015-06-02 04:15:57 +0000328 for (unsigned i = 0; i < List->size(); ++i) {
Nicolai Haehnle0b0eaf72018-03-05 15:20:51 +0000329 RecordResolver R(*CurRec);
330 Init *ItemVal = List->getElement(i)->resolveReferences(R);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000331 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
332 if (ProcessForeachDefs(CurRec, Loc, IterVals))
333 return true;
334 IterVals.pop_back();
335 }
336 return false;
337 }
338
339 // This is the bottom of the recursion. We have all of the iterator values
340 // for this point in the iteration space. Instantiate a new record to
341 // reflect this combination of values.
Craig Topper84138712014-11-29 05:31:10 +0000342 auto IterRec = make_unique<Record>(*CurRec);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000343
344 // Set the iterator values now.
Craig Topper8eb764c2015-06-02 06:19:28 +0000345 for (IterRecord &IR : IterVals) {
346 VarInit *IterVar = IR.IterVar;
347 TypedInit *IVal = dyn_cast<TypedInit>(IR.IterValue);
Craig Topper84138712014-11-29 05:31:10 +0000348 if (!IVal)
349 return Error(Loc, "foreach iterator value is untyped");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000350
Craig Topperf4412262017-06-01 06:56:16 +0000351 IterRec->addValue(RecordVal(IterVar->getNameInit(), IVal->getType(), false));
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000352
Matthias Braun215ff842016-12-05 07:35:13 +0000353 if (SetValue(IterRec.get(), Loc, IterVar->getNameInit(), None, IVal))
Craig Topper84138712014-11-29 05:31:10 +0000354 return Error(Loc, "when instantiating this def");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000355
356 // Resolve it next.
Matthias Braun215ff842016-12-05 07:35:13 +0000357 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getNameInit()));
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000358
359 // Remove it.
Matthias Braun215ff842016-12-05 07:35:13 +0000360 IterRec->removeValue(IterVar->getNameInit());
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000361 }
362
363 if (Records.getDef(IterRec->getNameInitAsString())) {
Artyom Skrobov8b985322014-06-10 12:41:14 +0000364 // If this record is anonymous, it's no problem, just generate a new name
Craig Topper84138712014-11-29 05:31:10 +0000365 if (!IterRec->isAnonymous())
366 return Error(Loc, "def already exists: " +IterRec->getNameInitAsString());
367
368 IterRec->setName(GetNewAnonymousName());
David Greenefb927af2012-02-22 16:09:41 +0000369 }
370
Craig Topper84138712014-11-29 05:31:10 +0000371 Record *IterRecSave = IterRec.get(); // Keep a copy before release.
Craig Toppercdab2322014-11-29 05:52:51 +0000372 Records.addDef(std::move(IterRec));
Craig Topper84138712014-11-29 05:31:10 +0000373 IterRecSave->resolveReferences();
David Greenefb927af2012-02-22 16:09:41 +0000374 return false;
375}
376
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000377//===----------------------------------------------------------------------===//
378// Parser Code
379//===----------------------------------------------------------------------===//
380
381/// isObjectStart - Return true if this is a valid first token for an Object.
382static bool isObjectStart(tgtok::TokKind K) {
383 return K == tgtok::Class || K == tgtok::Def ||
David Greenefb927af2012-02-22 16:09:41 +0000384 K == tgtok::Defm || K == tgtok::Let ||
385 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000386}
387
Alp Tokerce91fe52013-12-21 18:51:00 +0000388/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
389/// an identifier.
Craig Topperf4412262017-06-01 06:56:16 +0000390Init *TGParser::GetNewAnonymousName() {
391 return StringInit::get("anonymous_" + utostr(AnonCounter++));
Chris Lattner7538ed82010-10-05 22:51:56 +0000392}
393
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000394/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000395/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000396/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000397/// ObjectName ::= /*empty*/
398///
David Greene2affd672011-10-19 13:04:29 +0000399Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
400 switch (Lex.getCode()) {
401 case tgtok::colon:
402 case tgtok::semi:
403 case tgtok::l_brace:
404 // These are all of the tokens that can begin an object body.
405 // Some of these can also begin values but we disallow those cases
406 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000407 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000408 default:
409 break;
410 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000411
Craig Topper011817a2014-04-09 04:50:04 +0000412 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000413 if (CurMultiClass)
414 CurRec = &CurMultiClass->Rec;
415
Craig Topper011817a2014-04-09 04:50:04 +0000416 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000417 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000418 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000419 if (!CurRecName) {
420 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000421 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000422 }
423 Type = CurRecName->getType();
424 }
425
426 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000427}
428
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000429/// ParseClassID - Parse and resolve a reference to a class name. This returns
430/// null on error.
431///
432/// ClassID ::= ID
433///
434Record *TGParser::ParseClassID() {
435 if (Lex.getCode() != tgtok::Id) {
436 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000437 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000438 }
Bob Wilson7248f862009-11-22 04:24:42 +0000439
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000440 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000441 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000442 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000443
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000444 Lex.Lex();
445 return Result;
446}
447
Bob Wilson3d948162009-04-28 19:41:44 +0000448/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
449/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000450///
451/// MultiClassID ::= ID
452///
453MultiClass *TGParser::ParseMultiClassID() {
454 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000455 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000456 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000457 }
Bob Wilson3d948162009-04-28 19:41:44 +0000458
Craig Topper7adf2bf2014-12-11 05:25:30 +0000459 MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
Craig Topper4ca40012014-11-30 01:20:17 +0000460 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000461 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000462
David Greene753ed8f2009-04-22 16:42:54 +0000463 Lex.Lex();
Craig Topper4ca40012014-11-30 01:20:17 +0000464 return Result;
David Greene753ed8f2009-04-22 16:42:54 +0000465}
466
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000467/// ParseSubClassReference - Parse a reference to a subclass or to a templated
468/// subclass. This returns a SubClassRefTy with a null Record* on error.
469///
470/// SubClassRef ::= ClassID
471/// SubClassRef ::= ClassID '<' ValueList '>'
472///
473SubClassReference TGParser::
474ParseSubClassReference(Record *CurRec, bool isDefm) {
475 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000476 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000477
Sean Silva0657b402013-01-09 02:17:14 +0000478 if (isDefm) {
479 if (MultiClass *MC = ParseMultiClassID())
480 Result.Rec = &MC->Rec;
481 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000482 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000483 }
Craig Topper011817a2014-04-09 04:50:04 +0000484 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000485
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000486 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000487 if (Lex.getCode() != tgtok::less) {
488 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000489 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000490 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000491 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000492
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000493 if (Lex.getCode() == tgtok::greater) {
494 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000495 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000496 return Result;
497 }
Bob Wilson7248f862009-11-22 04:24:42 +0000498
Matthias Braunc66e7552016-12-05 06:41:54 +0000499 ParseValueList(Result.TemplateArgs, CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000500 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000501 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000502 return Result;
503 }
Bob Wilson7248f862009-11-22 04:24:42 +0000504
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000505 if (Lex.getCode() != tgtok::greater) {
506 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000507 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000508 return Result;
509 }
510 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000511 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000512
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000513 return Result;
514}
515
Bob Wilson3d948162009-04-28 19:41:44 +0000516/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
517/// templated submulticlass. This returns a SubMultiClassRefTy with a null
518/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000519///
520/// SubMultiClassRef ::= MultiClassID
521/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
522///
523SubMultiClassReference TGParser::
524ParseSubMultiClassReference(MultiClass *CurMC) {
525 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000526 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000527
David Greene753ed8f2009-04-22 16:42:54 +0000528 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000529 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000530
David Greene753ed8f2009-04-22 16:42:54 +0000531 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000532 if (Lex.getCode() != tgtok::less) {
533 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000534 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000535 }
David Greene753ed8f2009-04-22 16:42:54 +0000536 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000537
David Greene753ed8f2009-04-22 16:42:54 +0000538 if (Lex.getCode() == tgtok::greater) {
539 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000540 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000541 return Result;
542 }
Bob Wilson3d948162009-04-28 19:41:44 +0000543
Matthias Braunc66e7552016-12-05 06:41:54 +0000544 ParseValueList(Result.TemplateArgs, &CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000545 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000546 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000547 return Result;
548 }
Bob Wilson3d948162009-04-28 19:41:44 +0000549
David Greene753ed8f2009-04-22 16:42:54 +0000550 if (Lex.getCode() != tgtok::greater) {
551 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000552 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000553 return Result;
554 }
555 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000556 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000557
558 return Result;
559}
560
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000561/// ParseRangePiece - Parse a bit/value range.
562/// RangePiece ::= INTVAL
563/// RangePiece ::= INTVAL '-' INTVAL
564/// RangePiece ::= INTVAL INTVAL
Matthias Braunc66e7552016-12-05 06:41:54 +0000565bool TGParser::ParseRangePiece(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000566 if (Lex.getCode() != tgtok::IntVal) {
567 TokError("expected integer or bitrange");
568 return true;
569 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000570 int64_t Start = Lex.getCurIntVal();
571 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000572
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000573 if (Start < 0)
574 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000575
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000576 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000577 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000578 Ranges.push_back(Start);
579 return false;
580 case tgtok::minus:
581 if (Lex.Lex() != tgtok::IntVal) {
582 TokError("expected integer value as end of range");
583 return true;
584 }
585 End = Lex.getCurIntVal();
586 break;
587 case tgtok::IntVal:
588 End = -Lex.getCurIntVal();
589 break;
590 }
Bob Wilson7248f862009-11-22 04:24:42 +0000591 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000592 return TokError("invalid range, cannot be negative");
593 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000594
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000595 // Add to the range.
Craig Toppera9642b42015-05-04 01:35:39 +0000596 if (Start < End)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000597 for (; Start <= End; ++Start)
598 Ranges.push_back(Start);
Craig Toppera9642b42015-05-04 01:35:39 +0000599 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000600 for (; Start >= End; --Start)
601 Ranges.push_back(Start);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000602 return false;
603}
604
605/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
606///
607/// RangeList ::= RangePiece (',' RangePiece)*
608///
Matthias Braunc66e7552016-12-05 06:41:54 +0000609void TGParser::ParseRangeList(SmallVectorImpl<unsigned> &Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000610 // Parse the first piece.
Matthias Braunc66e7552016-12-05 06:41:54 +0000611 if (ParseRangePiece(Result)) {
612 Result.clear();
613 return;
614 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000615 while (Lex.getCode() == tgtok::comma) {
616 Lex.Lex(); // Eat the comma.
617
618 // Parse the next range piece.
Matthias Braunc66e7552016-12-05 06:41:54 +0000619 if (ParseRangePiece(Result)) {
620 Result.clear();
621 return;
622 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000623 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000624}
625
626/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
627/// OptionalRangeList ::= '<' RangeList '>'
628/// OptionalRangeList ::= /*empty*/
Matthias Braunc66e7552016-12-05 06:41:54 +0000629bool TGParser::ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000630 if (Lex.getCode() != tgtok::less)
631 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000632
Chris Lattner526c8cb2009-06-21 03:39:35 +0000633 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000634 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000635
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000636 // Parse the range list.
Matthias Braunc66e7552016-12-05 06:41:54 +0000637 ParseRangeList(Ranges);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000638 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000639
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000640 if (Lex.getCode() != tgtok::greater) {
641 TokError("expected '>' at end of range list");
642 return Error(StartLoc, "to match this '<'");
643 }
644 Lex.Lex(); // eat the '>'.
645 return false;
646}
647
648/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
649/// OptionalBitList ::= '{' RangeList '}'
650/// OptionalBitList ::= /*empty*/
Matthias Braunc66e7552016-12-05 06:41:54 +0000651bool TGParser::ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000652 if (Lex.getCode() != tgtok::l_brace)
653 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000654
Chris Lattner526c8cb2009-06-21 03:39:35 +0000655 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000656 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000657
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000658 // Parse the range list.
Matthias Braunc66e7552016-12-05 06:41:54 +0000659 ParseRangeList(Ranges);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000660 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000661
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000662 if (Lex.getCode() != tgtok::r_brace) {
663 TokError("expected '}' at end of bit list");
664 return Error(StartLoc, "to match this '{'");
665 }
666 Lex.Lex(); // eat the '}'.
667 return false;
668}
669
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000670/// ParseType - Parse and return a tblgen type. This returns null on error.
671///
672/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000673/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000674/// Type ::= BIT // bit type
675/// Type ::= BITS '<' INTVAL '>' // bits<x> type
676/// Type ::= INT // int type
677/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000678/// Type ::= DAG // dag type
679/// Type ::= ClassID // Record Type
680///
681RecTy *TGParser::ParseType() {
682 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000683 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000684 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Tim Northover88403d72016-07-05 21:22:55 +0000685 case tgtok::Code: Lex.Lex(); return CodeRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000686 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
687 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000688 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000689 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000690 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000691 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000692 case tgtok::Bits: {
693 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
694 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000695 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000696 }
697 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
698 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000699 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000700 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000701 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000702 if (Lex.Lex() != tgtok::greater) { // Eat count.
703 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000704 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000705 }
706 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000707 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000708 }
709 case tgtok::List: {
710 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
711 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000712 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000713 }
714 Lex.Lex(); // Eat '<'
715 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000716 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000717
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000718 if (Lex.getCode() != tgtok::greater) {
719 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000720 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000721 }
722 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000723 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000724 }
Bob Wilson7248f862009-11-22 04:24:42 +0000725 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000726}
727
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000728/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
729/// has already been read.
Matthias Braun215ff842016-12-05 07:35:13 +0000730Init *TGParser::ParseIDValue(Record *CurRec, StringInit *Name, SMLoc NameLoc,
David Greened4263a62011-10-19 13:04:20 +0000731 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000732 if (CurRec) {
733 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000734 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000735
Matthias Braun215ff842016-12-05 07:35:13 +0000736 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
David Greenedb10e692011-10-19 13:02:42 +0000737
David Greene47a665e2011-10-05 22:42:54 +0000738 if (CurMultiClass)
Matthias Braun215ff842016-12-05 07:35:13 +0000739 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
David Greenedb10e692011-10-19 13:02:42 +0000740 "::");
David Greene47a665e2011-10-05 22:42:54 +0000741
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000742 if (CurRec->isTemplateArg(TemplateArgName)) {
743 const RecordVal *RV = CurRec->getValue(TemplateArgName);
744 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000745 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000746 }
Matthias Braun215ff842016-12-05 07:35:13 +0000747 }
Bob Wilson7248f862009-11-22 04:24:42 +0000748
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000749 if (CurMultiClass) {
Nicolai Haehnle3c80e4c2018-03-05 14:01:38 +0000750 if (Name->getValue() == "NAME")
751 return VarInit::get(Name, StringRecTy::get());
752
Matthias Braun215ff842016-12-05 07:35:13 +0000753 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name, "::");
David Greenedb10e692011-10-19 13:02:42 +0000754
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000755 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
756 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
757 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000758 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000759 }
760 }
Bob Wilson7248f862009-11-22 04:24:42 +0000761
David Greenefb927af2012-02-22 16:09:41 +0000762 // If this is in a foreach loop, make sure it's not a loop iterator
Craig Toppereb4d7c62015-04-29 04:43:36 +0000763 for (const auto &L : Loops) {
764 VarInit *IterVar = dyn_cast<VarInit>(L.IterVar);
Matthias Braun215ff842016-12-05 07:35:13 +0000765 if (IterVar && IterVar->getNameInit() == Name)
David Greenefb927af2012-02-22 16:09:41 +0000766 return IterVar;
767 }
768
David Greene232bd602011-10-19 13:04:21 +0000769 if (Mode == ParseNameMode)
Matthias Braun215ff842016-12-05 07:35:13 +0000770 return Name;
David Greene232bd602011-10-19 13:04:21 +0000771
Matthias Braun215ff842016-12-05 07:35:13 +0000772 if (Record *D = Records.getDef(Name->getValue()))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000773 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000774
David Greene232bd602011-10-19 13:04:21 +0000775 if (Mode == ParseValueMode) {
Matthias Braun215ff842016-12-05 07:35:13 +0000776 Error(NameLoc, "Variable not defined: '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000777 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000778 }
Craig Toppera9642b42015-05-04 01:35:39 +0000779
Matthias Braun215ff842016-12-05 07:35:13 +0000780 return Name;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000781}
782
David Greene5d0c0512009-05-14 20:54:48 +0000783/// ParseOperation - Parse an operator. This returns null on error.
784///
785/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
786///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000787Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000788 switch (Lex.getCode()) {
789 default:
790 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000791 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000792 case tgtok::XHead:
793 case tgtok::XTail:
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000794 case tgtok::XSize:
David Greene2f7cf7f2011-01-07 17:05:37 +0000795 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000796 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
797 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000798 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000799
David Greenee8f3b272009-05-14 21:22:49 +0000800 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000801 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000802 case tgtok::XCast:
803 Lex.Lex(); // eat the operation
804 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000805
David Greenee8f3b272009-05-14 21:22:49 +0000806 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000807
Craig Topper011817a2014-04-09 04:50:04 +0000808 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000809 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000810 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000811 }
David Greene5d0c0512009-05-14 20:54:48 +0000812
David Greenee8f3b272009-05-14 21:22:49 +0000813 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000814 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000815 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000816 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000817 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000818 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000819 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000820 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000821 break;
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000822 case tgtok::XSize:
823 Lex.Lex();
824 Code = UnOpInit::SIZE;
825 Type = IntRecTy::get();
826 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000827 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000828 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000829 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000830 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000831 break;
David Greenee8f3b272009-05-14 21:22:49 +0000832 }
833 if (Lex.getCode() != tgtok::l_paren) {
834 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000835 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000836 }
837 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000838
David Greeneaf8ee2c2011-07-29 22:43:06 +0000839 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000840 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000841
Craig Topper85c07002015-04-30 05:54:22 +0000842 if (Code == UnOpInit::HEAD ||
843 Code == UnOpInit::TAIL ||
844 Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000845 ListInit *LHSl = dyn_cast<ListInit>(LHS);
846 StringInit *LHSs = dyn_cast<StringInit>(LHS);
847 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000848 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000849 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000850 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000851 }
852 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000853 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
854 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000855 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000856 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000857 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000858 }
859 }
860
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000861 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL ||
862 Code == UnOpInit::SIZE) {
Craig Topper011817a2014-04-09 04:50:04 +0000863 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000864 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000865 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000866 }
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000867 }
Bob Wilson7248f862009-11-22 04:24:42 +0000868
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000869 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
Craig Topperec9072d2015-05-14 05:53:53 +0000870 if (LHSl && LHSl->empty()) {
David Greened571b3c2009-05-14 22:38:31 +0000871 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000872 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000873 }
874 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000875 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000876 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000877 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000878 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000879 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000880 }
Craig Toppera9642b42015-05-04 01:35:39 +0000881 Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
882 : ListRecTy::get(Itemt->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000883 } else {
David Greened571b3c2009-05-14 22:38:31 +0000884 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000885 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000886 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000887 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000888 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000889 }
Craig Toppera9642b42015-05-04 01:35:39 +0000890 Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
David Greened571b3c2009-05-14 22:38:31 +0000891 }
892 }
893 }
894
David Greenee8f3b272009-05-14 21:22:49 +0000895 if (Lex.getCode() != tgtok::r_paren) {
896 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000897 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000898 }
899 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000900 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000901 }
David Greene5d0c0512009-05-14 20:54:48 +0000902
903 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000904 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000905 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000906 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +0000907 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000908 case tgtok::XSRL:
909 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000910 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000911 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000912 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000913 tgtok::TokKind OpTok = Lex.getCode();
914 SMLoc OpLoc = Lex.getLoc();
915 Lex.Lex(); // eat the operation
916
David Greene5d0c0512009-05-14 20:54:48 +0000917 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000918 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000919
Chris Lattner61ea00b2010-10-05 23:58:18 +0000920 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000921 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000922 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000923 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000924 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000925 case tgtok::XOR: Code = BinOpInit::OR; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000926 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
927 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
928 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
929 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000930 case tgtok::XListConcat:
931 Code = BinOpInit::LISTCONCAT;
932 // We don't know the list type until we parse the first argument
933 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000934 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000935 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000936 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000937 break;
David Greene5d0c0512009-05-14 20:54:48 +0000938 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000939
David Greene5d0c0512009-05-14 20:54:48 +0000940 if (Lex.getCode() != tgtok::l_paren) {
941 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000942 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000943 }
944 Lex.Lex(); // eat the '('
945
David Greeneaf8ee2c2011-07-29 22:43:06 +0000946 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000947
Chris Lattner61ea00b2010-10-05 23:58:18 +0000948 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000949 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000950
Chris Lattner61ea00b2010-10-05 23:58:18 +0000951 while (Lex.getCode() == tgtok::comma) {
952 Lex.Lex(); // eat the ','
953
954 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000955 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000956 }
David Greene5d0c0512009-05-14 20:54:48 +0000957
958 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000959 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000960 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000961 }
962 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000963
Daniel Sanders314e80e2014-05-07 10:13:19 +0000964 // If we are doing !listconcat, we should know the type by now
965 if (OpTok == tgtok::XListConcat) {
Nicolai Haehnlee4a2cf52018-02-22 15:26:28 +0000966 if (TypedInit *Arg0 = dyn_cast<TypedInit>(InitList[0]))
Daniel Sanders314e80e2014-05-07 10:13:19 +0000967 Type = Arg0->getType();
968 else {
Matthias Braun8c209aa2017-01-28 02:02:38 +0000969 InitList[0]->print(errs());
Daniel Sanders314e80e2014-05-07 10:13:19 +0000970 Error(OpLoc, "expected a list");
971 return nullptr;
972 }
973 }
974
Chris Lattner61ea00b2010-10-05 23:58:18 +0000975 // We allow multiple operands to associative operators like !strconcat as
976 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000977 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000978 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000979 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000980 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
981 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000982 InitList.back() = RHS;
983 }
984 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000985
Chris Lattner61ea00b2010-10-05 23:58:18 +0000986 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000987 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000988 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000989
Chris Lattner61ea00b2010-10-05 23:58:18 +0000990 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +0000991 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000992 }
993
Nicolai Haehnle8ebf7e42018-03-05 15:21:04 +0000994 case tgtok::XForEach: { // Value ::= !foreach '(' Id ',' Value ',' Value ')'
995 SMLoc OpLoc = Lex.getLoc();
996 Lex.Lex(); // eat the operation
997 if (Lex.getCode() != tgtok::l_paren) {
998 TokError("expected '(' after !foreach");
999 return nullptr;
1000 }
1001
1002 if (Lex.Lex() != tgtok::Id) { // eat the '('
1003 TokError("first argument of !foreach must be an identifier");
1004 return nullptr;
1005 }
1006
1007 Init *LHS = StringInit::get(Lex.getCurStrVal());
1008
1009 if (CurRec->getValue(LHS)) {
1010 TokError((Twine("iteration variable '") + LHS->getAsString() +
1011 "' already defined")
1012 .str());
1013 return nullptr;
1014 }
1015
1016 if (Lex.Lex() != tgtok::comma) { // eat the id
1017 TokError("expected ',' in ternary operator");
1018 return nullptr;
1019 }
1020 Lex.Lex(); // eat the ','
1021
1022 Init *MHS = ParseValue(CurRec);
1023 if (!MHS)
1024 return nullptr;
1025
1026 if (Lex.getCode() != tgtok::comma) {
1027 TokError("expected ',' in ternary operator");
1028 return nullptr;
1029 }
1030 Lex.Lex(); // eat the ','
1031
1032 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1033 if (!MHSt) {
1034 TokError("could not get type of !foreach input");
1035 return nullptr;
1036 }
1037
1038 RecTy *InEltType = nullptr;
1039 RecTy *OutEltType = nullptr;
1040 bool IsDAG = false;
1041
1042 if (ListRecTy *InListTy = dyn_cast<ListRecTy>(MHSt->getType())) {
1043 InEltType = InListTy->getElementType();
1044 if (ItemType) {
1045 if (ListRecTy *OutListTy = dyn_cast<ListRecTy>(ItemType)) {
1046 OutEltType = OutListTy->getElementType();
1047 } else {
1048 Error(OpLoc,
1049 "expected value of type '" + Twine(ItemType->getAsString()) +
1050 "', but got !foreach of list type");
1051 return nullptr;
1052 }
1053 }
1054 } else if (DagRecTy *InDagTy = dyn_cast<DagRecTy>(MHSt->getType())) {
1055 InEltType = InDagTy;
1056 if (ItemType && !isa<DagRecTy>(ItemType)) {
1057 Error(OpLoc,
1058 "expected value of type '" + Twine(ItemType->getAsString()) +
1059 "', but got !foreach of dag type");
1060 return nullptr;
1061 }
1062 IsDAG = true;
1063 } else {
1064 TokError("!foreach must have list or dag input");
1065 return nullptr;
1066 }
1067
1068 CurRec->addValue(RecordVal(LHS, InEltType, false));
1069 Init *RHS = ParseValue(CurRec, OutEltType);
1070 CurRec->removeValue(LHS);
1071 if (!RHS)
1072 return nullptr;
1073
1074 if (Lex.getCode() != tgtok::r_paren) {
1075 TokError("expected ')' in binary operator");
1076 return nullptr;
1077 }
1078 Lex.Lex(); // eat the ')'
1079
1080 RecTy *OutType;
1081 if (IsDAG) {
1082 OutType = InEltType;
1083 } else {
1084 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1085 if (!RHSt) {
1086 TokError("could not get type of !foreach result");
1087 return nullptr;
1088 }
1089 OutType = RHSt->getType()->getListTy();
1090 }
1091
1092 return (TernOpInit::get(TernOpInit::FOREACH, LHS, MHS, RHS, OutType))
1093 ->Fold(CurRec, CurMultiClass);
1094 }
1095
David Greene3587eed2009-05-14 23:26:46 +00001096 case tgtok::XIf:
David Greene98ed3c72009-05-14 21:54:42 +00001097 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
1098 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +00001099 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001100
David Greene98ed3c72009-05-14 21:54:42 +00001101 tgtok::TokKind LexCode = Lex.getCode();
1102 Lex.Lex(); // eat the operation
1103 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001104 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001105 case tgtok::XIf:
1106 Code = TernOpInit::IF;
1107 break;
David Greene98ed3c72009-05-14 21:54:42 +00001108 case tgtok::XSubst:
1109 Code = TernOpInit::SUBST;
1110 break;
1111 }
1112 if (Lex.getCode() != tgtok::l_paren) {
1113 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001114 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001115 }
1116 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001117
David Greeneaf8ee2c2011-07-29 22:43:06 +00001118 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001119 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001120
David Greene98ed3c72009-05-14 21:54:42 +00001121 if (Lex.getCode() != tgtok::comma) {
1122 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001123 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001124 }
1125 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001126
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001127 Init *MHS = ParseValue(CurRec, ItemType);
1128 if (!MHS)
1129 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001130
David Greene98ed3c72009-05-14 21:54:42 +00001131 if (Lex.getCode() != tgtok::comma) {
1132 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001133 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001134 }
1135 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001136
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001137 Init *RHS = ParseValue(CurRec, ItemType);
1138 if (!RHS)
1139 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001140
David Greene98ed3c72009-05-14 21:54:42 +00001141 if (Lex.getCode() != tgtok::r_paren) {
1142 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001143 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001144 }
1145 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001146
David Greene98ed3c72009-05-14 21:54:42 +00001147 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001148 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001149 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001150 RecTy *MHSTy = nullptr;
1151 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001152
Sean Silvafb509ed2012-10-10 20:24:43 +00001153 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001154 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001155 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001156 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001157 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001158 MHSTy = BitRecTy::get();
1159
Sean Silvafb509ed2012-10-10 20:24:43 +00001160 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001161 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001162 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001163 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001164 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001165 RHSTy = BitRecTy::get();
1166
1167 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001168 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001169 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001170 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001171 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001172
1173 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001174 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001175 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001176 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001177
Nicolai Haehnle13080fd2018-03-06 13:48:20 +00001178 Type = resolveTypes(MHSTy, RHSTy);
1179 if (!Type) {
1180 TokError(Twine("inconsistent types '") + MHSTy->getAsString() +
1181 "' and '" + RHSTy->getAsString() + "' for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001182 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001183 }
1184 break;
1185 }
David Greene98ed3c72009-05-14 21:54:42 +00001186 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001187 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001188 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001189 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001190 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001191 }
1192 Type = RHSt->getType();
1193 break;
1194 }
1195 }
David Greenee32ebf22011-07-29 19:07:07 +00001196 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001197 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001198 }
David Greene5d0c0512009-05-14 20:54:48 +00001199 }
David Greene5d0c0512009-05-14 20:54:48 +00001200}
1201
1202/// ParseOperatorType - Parse a type for an operator. This returns
1203/// null on error.
1204///
1205/// OperatorType ::= '<' Type '>'
1206///
Dan Gohman1432ef82009-08-12 22:10:57 +00001207RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001208 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001209
1210 if (Lex.getCode() != tgtok::less) {
1211 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001212 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001213 }
1214 Lex.Lex(); // eat the <
1215
1216 Type = ParseType();
1217
Craig Topper011817a2014-04-09 04:50:04 +00001218 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001219 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001220 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001221 }
1222
1223 if (Lex.getCode() != tgtok::greater) {
1224 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001225 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001226 }
1227 Lex.Lex(); // eat the >
1228
1229 return Type;
1230}
1231
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001232/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1233///
1234/// SimpleValue ::= IDValue
1235/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001236/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001237/// SimpleValue ::= CODEFRAGMENT
1238/// SimpleValue ::= '?'
1239/// SimpleValue ::= '{' ValueList '}'
1240/// SimpleValue ::= ID '<' ValueListNE '>'
1241/// SimpleValue ::= '[' ValueList ']'
1242/// SimpleValue ::= '(' IDValue DagArgList ')'
1243/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001244/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001245/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1246/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1247/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001248/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001249/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1250///
David Greened4263a62011-10-19 13:04:20 +00001251Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1252 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001253 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001254 switch (Lex.getCode()) {
1255 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001256 case tgtok::paste:
1257 // This is a leading paste operation. This is deprecated but
1258 // still exists in some .td files. Ignore it.
1259 Lex.Lex(); // Skip '#'.
1260 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001261 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001262 case tgtok::BinaryIntVal: {
1263 auto BinaryVal = Lex.getCurBinaryIntVal();
1264 SmallVector<Init*, 16> Bits(BinaryVal.second);
1265 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001266 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001267 R = BitsInit::get(Bits);
1268 Lex.Lex();
1269 break;
1270 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001271 case tgtok::StrVal: {
1272 std::string Val = Lex.getCurStrVal();
1273 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001274
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001275 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001276 while (Lex.getCode() == tgtok::StrVal) {
1277 Val += Lex.getCurStrVal();
1278 Lex.Lex();
1279 }
Bob Wilson7248f862009-11-22 04:24:42 +00001280
David Greenee32ebf22011-07-29 19:07:07 +00001281 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001282 break;
1283 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001284 case tgtok::CodeFragment:
Tim Northover88403d72016-07-05 21:22:55 +00001285 R = CodeInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001286 Lex.Lex();
1287 break;
1288 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001289 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001290 Lex.Lex();
1291 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001292 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001293 SMLoc NameLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00001294 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001295 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001296 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001297
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001298 // Value ::= ID '<' ValueListNE '>'
1299 if (Lex.Lex() == tgtok::greater) {
1300 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001301 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001302 }
David Greene8618f952009-06-08 20:23:18 +00001303
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001304 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1305 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1306 // body.
Matthias Braun215ff842016-12-05 07:35:13 +00001307 Record *Class = Records.getClass(Name->getValue());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001308 if (!Class) {
Matthias Braun215ff842016-12-05 07:35:13 +00001309 Error(NameLoc, "Expected a class name, got '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001310 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001311 }
David Greene8618f952009-06-08 20:23:18 +00001312
Matthias Braunc66e7552016-12-05 06:41:54 +00001313 SubClassReference SCRef;
1314 ParseValueList(SCRef.TemplateArgs, CurRec, Class);
1315 if (SCRef.TemplateArgs.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001316
David Greene8618f952009-06-08 20:23:18 +00001317 if (Lex.getCode() != tgtok::greater) {
1318 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001319 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001320 }
1321 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001322 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001323
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001324 // Create the new record, set it as CurRec temporarily.
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00001325 auto NewRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), NameLoc,
1326 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001327 Record *NewRec = NewRecOwner.get(); // Keep a copy since we may release.
Jordan Rosef12e8a92013-01-10 18:50:11 +00001328 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001329 SCRef.Rec = Class;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001330 // Add info about the subclass to NewRec.
Craig Topper84138712014-11-29 05:31:10 +00001331 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001332 return nullptr;
Craig Topper84138712014-11-29 05:31:10 +00001333
Hal Finkela8c1f462014-01-02 20:47:09 +00001334 if (!CurMultiClass) {
1335 NewRec->resolveReferences();
Craig Toppercdab2322014-11-29 05:52:51 +00001336 Records.addDef(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001337 } else {
Adam Nemete5a07162014-09-16 17:14:13 +00001338 // This needs to get resolved once the multiclass template arguments are
1339 // known before any use.
1340 NewRec->setResolveFirst(true);
Hal Finkela8c1f462014-01-02 20:47:09 +00001341 // Otherwise, we're inside a multiclass, add it to the multiclass.
Craig Topperc3504c42014-12-11 05:25:33 +00001342 CurMultiClass->DefPrototypes.push_back(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001343
1344 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00001345 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
1346 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Hal Finkela8c1f462014-01-02 20:47:09 +00001347 assert(RV && "Template arg doesn't exist?");
1348 NewRec->addValue(*RV);
1349 }
1350
1351 // We can't return the prototype def here, instead return:
1352 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1353 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1354 assert(MCNameRV && "multiclass record must have a NAME");
1355
1356 return UnOpInit::get(UnOpInit::CAST,
1357 BinOpInit::get(BinOpInit::STRCONCAT,
1358 VarInit::get(MCNameRV->getName(),
1359 MCNameRV->getType()),
1360 NewRec->getNameInit(),
1361 StringRecTy::get()),
Nicolai Haehnle13080fd2018-03-06 13:48:20 +00001362 NewRec->getDefInit()->getType());
Hal Finkela8c1f462014-01-02 20:47:09 +00001363 }
Bob Wilson7248f862009-11-22 04:24:42 +00001364
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001365 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001366 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001367 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001368 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001369 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001370 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001371 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001372
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001373 if (Lex.getCode() != tgtok::r_brace) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001374 ParseValueList(Vals, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001375 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001376 }
1377 if (Lex.getCode() != tgtok::r_brace) {
1378 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001379 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001380 }
1381 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001382
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001383 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001384
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001385 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1386 // first. We'll first read everything in to a vector, then we can reverse
1387 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001388 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001389 // FIXME: The following two loops would not be duplicated
1390 // if the API was a little more orthogonal.
1391
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001392 // bits<n> values are allowed to initialize n bits.
1393 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1394 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1395 NewBits.push_back(BI->getBit((e - i) - 1));
1396 continue;
1397 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001398 // bits<n> can also come from variable initializers.
1399 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1400 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1401 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1402 NewBits.push_back(VI->getBit((e - i) - 1));
1403 continue;
1404 }
1405 // Fallthrough to try convert this to a bit.
1406 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001407 // All other values must be convertible to just a single bit.
Nicolai Haehnledfda9dc2018-03-06 13:48:39 +00001408 Init *Bit = Vals[i]->getCastTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001409 if (!Bit) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001410 Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
Chris Lattner52416952007-11-22 21:06:59 +00001411 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001412 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001413 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001414 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001415 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001416 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001417 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001418 }
1419 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1420 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001421 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001422
Craig Topper011817a2014-04-09 04:50:04 +00001423 RecTy *DeducedEltTy = nullptr;
1424 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001425
Craig Topper011817a2014-04-09 04:50:04 +00001426 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001427 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001428 if (!ListType) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001429 TokError(Twine("Type mismatch for list, expected list type, got ") +
1430 ItemType->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001431 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001432 }
1433 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001434 }
David Greene8618f952009-06-08 20:23:18 +00001435
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001436 if (Lex.getCode() != tgtok::r_square) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001437 ParseValueList(Vals, CurRec, nullptr,
1438 GivenListTy ? GivenListTy->getElementType() : nullptr);
Craig Topper011817a2014-04-09 04:50:04 +00001439 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001440 }
1441 if (Lex.getCode() != tgtok::r_square) {
1442 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001443 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001444 }
1445 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001446
Craig Topper011817a2014-04-09 04:50:04 +00001447 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001448 if (Lex.getCode() == tgtok::less) {
1449 // Optional list element type
1450 Lex.Lex(); // eat the '<'
1451
1452 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001453 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001454 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001455 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001456 }
1457
1458 if (Lex.getCode() != tgtok::greater) {
1459 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001460 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001461 }
1462 Lex.Lex(); // eat the '>'
1463 }
1464
1465 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001466 RecTy *EltTy = nullptr;
Craig Toppereb4d7c62015-04-29 04:43:36 +00001467 for (Init *V : Vals) {
1468 TypedInit *TArg = dyn_cast<TypedInit>(V);
Craig Topper011817a2014-04-09 04:50:04 +00001469 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001470 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001471 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001472 }
Craig Topper011817a2014-04-09 04:50:04 +00001473 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001474 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001475 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001476 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001477 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001478 }
Bob Wilson7248f862009-11-22 04:24:42 +00001479 } else {
David Greene8618f952009-06-08 20:23:18 +00001480 EltTy = TArg->getType();
1481 }
1482 }
1483
Craig Topper011817a2014-04-09 04:50:04 +00001484 if (GivenEltTy) {
1485 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001486 // Verify consistency
1487 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1488 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001489 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001490 }
1491 }
1492 EltTy = GivenEltTy;
1493 }
1494
Craig Topper011817a2014-04-09 04:50:04 +00001495 if (!EltTy) {
1496 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001497 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001498 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001499 }
1500 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001501 } else {
David Greene8618f952009-06-08 20:23:18 +00001502 // Make sure the deduced type is compatible with the given type
1503 if (GivenListTy) {
1504 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
Nicolai Haehnlef19083d2018-02-22 15:26:21 +00001505 TokError(Twine("Element type mismatch for list: element type '") +
1506 EltTy->getAsString() + "' not convertible to '" +
1507 GivenListTy->getElementType()->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001508 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001509 }
1510 }
1511 DeducedEltTy = EltTy;
1512 }
Bob Wilson7248f862009-11-22 04:24:42 +00001513
David Greenee32ebf22011-07-29 19:07:07 +00001514 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001515 }
1516 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1517 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001518 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001519 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001520 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001521 }
Bob Wilson7248f862009-11-22 04:24:42 +00001522
David Greeneaf8ee2c2011-07-29 22:43:06 +00001523 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001524 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001525
Nate Begemandbe3f772009-03-19 05:21:56 +00001526 // If the operator name is present, parse it.
Matthias Braun7cf3b112016-12-05 06:00:41 +00001527 StringInit *OperatorName = nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001528 if (Lex.getCode() == tgtok::colon) {
1529 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1530 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001531 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001532 }
Matthias Braun7cf3b112016-12-05 06:00:41 +00001533 OperatorName = StringInit::get(Lex.getCurStrVal());
Nate Begemandbe3f772009-03-19 05:21:56 +00001534 Lex.Lex(); // eat the VarName.
1535 }
Bob Wilson7248f862009-11-22 04:24:42 +00001536
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001537 SmallVector<std::pair<llvm::Init*, StringInit*>, 8> DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001538 if (Lex.getCode() != tgtok::r_paren) {
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001539 ParseDagArgList(DagArgs, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001540 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001541 }
Bob Wilson7248f862009-11-22 04:24:42 +00001542
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001543 if (Lex.getCode() != tgtok::r_paren) {
1544 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001545 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001546 }
1547 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001548
David Greenee32ebf22011-07-29 19:07:07 +00001549 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001550 }
Bob Wilson7248f862009-11-22 04:24:42 +00001551
David Greene2f7cf7f2011-01-07 17:05:37 +00001552 case tgtok::XHead:
1553 case tgtok::XTail:
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +00001554 case tgtok::XSize:
David Greene2f7cf7f2011-01-07 17:05:37 +00001555 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001556 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001557 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001558 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001559 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +00001560 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +00001561 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001562 case tgtok::XSRL:
1563 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001564 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001565 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001566 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001567 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001568 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001569 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001570 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001571 }
1572 }
Bob Wilson7248f862009-11-22 04:24:42 +00001573
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001574 return R;
1575}
1576
1577/// ParseValue - Parse a tblgen value. This returns null on error.
1578///
1579/// Value ::= SimpleValue ValueSuffix*
1580/// ValueSuffix ::= '{' BitList '}'
1581/// ValueSuffix ::= '[' BitList ']'
1582/// ValueSuffix ::= '.' ID
1583///
David Greened4263a62011-10-19 13:04:20 +00001584Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1585 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001586 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001587
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001588 // Parse the suffixes now if present.
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001589 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001590 switch (Lex.getCode()) {
1591 default: return Result;
1592 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001593 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001594 // This is the beginning of the object body.
1595 return Result;
1596
Chris Lattner526c8cb2009-06-21 03:39:35 +00001597 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001598 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001599 SmallVector<unsigned, 16> Ranges;
1600 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001601 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001602
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001603 // Reverse the bitlist.
1604 std::reverse(Ranges.begin(), Ranges.end());
1605 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001606 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001607 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001608 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001609 }
Bob Wilson7248f862009-11-22 04:24:42 +00001610
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001611 // Eat the '}'.
1612 if (Lex.getCode() != tgtok::r_brace) {
1613 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001614 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001615 }
1616 Lex.Lex();
1617 break;
1618 }
1619 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001620 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001621 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001622 SmallVector<unsigned, 16> Ranges;
1623 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001624 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001625
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001626 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001627 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001628 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001629 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001630 }
Bob Wilson7248f862009-11-22 04:24:42 +00001631
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001632 // Eat the ']'.
1633 if (Lex.getCode() != tgtok::r_square) {
1634 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001635 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001636 }
1637 Lex.Lex();
1638 break;
1639 }
Matthias Braun6a441832016-12-05 06:00:36 +00001640 case tgtok::period: {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001641 if (Lex.Lex() != tgtok::Id) { // eat the .
1642 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001643 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001644 }
Matthias Braun6a441832016-12-05 06:00:36 +00001645 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
1646 if (!Result->getFieldType(FieldName)) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001647 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001648 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001649 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001650 }
Matthias Braun6a441832016-12-05 06:00:36 +00001651 Result = FieldInit::get(Result, FieldName);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001652 Lex.Lex(); // eat field name
1653 break;
Matthias Braun6a441832016-12-05 06:00:36 +00001654 }
David Greene8e85b482011-10-19 13:04:43 +00001655
1656 case tgtok::paste:
1657 SMLoc PasteLoc = Lex.getLoc();
1658
1659 // Create a !strconcat() operation, first casting each operand to
1660 // a string if necessary.
1661
Sean Silvafb509ed2012-10-10 20:24:43 +00001662 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001663 if (!LHS) {
1664 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001665 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001666 }
Craig Toppera9642b42015-05-04 01:35:39 +00001667
David Greene8e85b482011-10-19 13:04:43 +00001668 if (LHS->getType() != StringRecTy::get()) {
1669 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1670 }
1671
Craig Topper011817a2014-04-09 04:50:04 +00001672 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001673
1674 Lex.Lex(); // Eat the '#'.
1675 switch (Lex.getCode()) {
1676 case tgtok::colon:
1677 case tgtok::semi:
1678 case tgtok::l_brace:
1679 // These are all of the tokens that can begin an object body.
1680 // Some of these can also begin values but we disallow those cases
1681 // because they are unlikely to be useful.
Craig Toppera9642b42015-05-04 01:35:39 +00001682
David Greene8e85b482011-10-19 13:04:43 +00001683 // Trailing paste, concat with an empty string.
1684 RHS = StringInit::get("");
1685 break;
1686
1687 default:
1688 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001689 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001690 if (!RHS) {
1691 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001692 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001693 }
1694
1695 if (RHS->getType() != StringRecTy::get()) {
1696 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1697 }
Craig Toppera9642b42015-05-04 01:35:39 +00001698
David Greene8e85b482011-10-19 13:04:43 +00001699 break;
1700 }
1701
1702 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1703 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1704 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001705 }
1706 }
1707}
1708
1709/// ParseDagArgList - Parse the argument list for a dag literal expression.
1710///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001711/// DagArg ::= Value (':' VARNAME)?
1712/// DagArg ::= VARNAME
1713/// DagArgList ::= DagArg
1714/// DagArgList ::= DagArgList ',' DagArg
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001715void TGParser::ParseDagArgList(
1716 SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
1717 Record *CurRec) {
Bob Wilson7248f862009-11-22 04:24:42 +00001718
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001719 while (true) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001720 // DagArg ::= VARNAME
1721 if (Lex.getCode() == tgtok::VarName) {
1722 // A missing value is treated like '?'.
Matthias Braunbb053162016-12-05 06:00:46 +00001723 StringInit *VarName = StringInit::get(Lex.getCurStrVal());
1724 Result.emplace_back(UnsetInit::get(), VarName);
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001725 Lex.Lex();
1726 } else {
1727 // DagArg ::= Value (':' VARNAME)?
1728 Init *Val = ParseValue(CurRec);
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001729 if (!Val) {
1730 Result.clear();
1731 return;
1732 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001733
1734 // If the variable name is present, add it.
Matthias Braunbb053162016-12-05 06:00:46 +00001735 StringInit *VarName = nullptr;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001736 if (Lex.getCode() == tgtok::colon) {
1737 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1738 TokError("expected variable name in dag literal");
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001739 Result.clear();
1740 return;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001741 }
Matthias Braunbb053162016-12-05 06:00:46 +00001742 VarName = StringInit::get(Lex.getCurStrVal());
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001743 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001744 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001745
1746 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001747 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001748 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001749 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001750 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001751}
1752
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001753/// ParseValueList - Parse a comma separated list of values, returning them as a
1754/// vector. Note that this always expects to be able to parse at least one
1755/// value. It returns an empty list if this is not possible.
1756///
1757/// ValueList ::= Value (',' Value)
1758///
Matthias Braunc66e7552016-12-05 06:41:54 +00001759void TGParser::ParseValueList(SmallVectorImpl<Init*> &Result, Record *CurRec,
1760 Record *ArgsRec, RecTy *EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001761 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001762 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001763 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001764 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001765 if (TArgs.empty()) {
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001766 TokError("template argument provided to non-template class");
Matthias Braunc66e7552016-12-05 06:41:54 +00001767 Result.clear();
1768 return;
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001769 }
David Greene8618f952009-06-08 20:23:18 +00001770 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001771 if (!RV) {
1772 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1773 << ")\n";
1774 }
David Greene8618f952009-06-08 20:23:18 +00001775 assert(RV && "Template argument record not found??");
1776 ItemType = RV->getType();
1777 ++ArgN;
1778 }
1779 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00001780 if (!Result.back()) {
1781 Result.clear();
1782 return;
1783 }
Bob Wilson7248f862009-11-22 04:24:42 +00001784
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001785 while (Lex.getCode() == tgtok::comma) {
1786 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001787
Craig Topper011817a2014-04-09 04:50:04 +00001788 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001789 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001790 if (ArgN >= TArgs.size()) {
1791 TokError("too many template arguments");
Matthias Braunc66e7552016-12-05 06:41:54 +00001792 Result.clear();
1793 return;
Bob Wilson7248f862009-11-22 04:24:42 +00001794 }
David Greene8618f952009-06-08 20:23:18 +00001795 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1796 assert(RV && "Template argument record not found??");
1797 ItemType = RV->getType();
1798 ++ArgN;
1799 }
1800 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00001801 if (!Result.back()) {
1802 Result.clear();
1803 return;
1804 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001805 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001806}
1807
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001808/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1809/// empty string on error. This can happen in a number of different context's,
1810/// including within a def or in the template args for a def (which which case
1811/// CurRec will be non-null) and within the template args for a multiclass (in
1812/// which case CurRec will be null, but CurMultiClass will be set). This can
1813/// also happen within a def that is within a multiclass, which will set both
1814/// CurRec and CurMultiClass.
1815///
1816/// Declaration ::= FIELD? Type ID ('=' Value)?
1817///
David Greenedb10e692011-10-19 13:02:42 +00001818Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001819 bool ParsingTemplateArgs) {
1820 // Read the field prefix if present.
1821 bool HasField = Lex.getCode() == tgtok::Field;
1822 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001823
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001824 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001825 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001826
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001827 if (Lex.getCode() != tgtok::Id) {
1828 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001829 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001830 }
Bob Wilson7248f862009-11-22 04:24:42 +00001831
Chris Lattner526c8cb2009-06-21 03:39:35 +00001832 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001833 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001834 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001835
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001836 if (ParsingTemplateArgs) {
Craig Toppera9642b42015-05-04 01:35:39 +00001837 if (CurRec)
David Greenedb10e692011-10-19 13:02:42 +00001838 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Craig Toppera9642b42015-05-04 01:35:39 +00001839 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001840 assert(CurMultiClass);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001841 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001842 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1843 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001844 }
Bob Wilson7248f862009-11-22 04:24:42 +00001845
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001846 // Add the value.
1847 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001848 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001849
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001850 // If a value is present, parse it.
1851 if (Lex.getCode() == tgtok::equal) {
1852 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001853 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001854 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001855 if (!Val ||
Craig Toppercfd81732016-01-04 03:15:08 +00001856 SetValue(CurRec, ValLoc, DeclName, None, Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001857 // Return the name, even if an error is thrown. This is so that we can
1858 // continue to make some progress, even without the value having been
1859 // initialized.
1860 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001861 }
Bob Wilson7248f862009-11-22 04:24:42 +00001862
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001863 return DeclName;
1864}
1865
David Greenefb927af2012-02-22 16:09:41 +00001866/// ParseForeachDeclaration - Read a foreach declaration, returning
1867/// the name of the declared object or a NULL Init on error. Return
1868/// the name of the parsed initializer list through ForeachListName.
1869///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001870/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1871/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1872/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001873///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001874VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001875 if (Lex.getCode() != tgtok::Id) {
1876 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001877 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001878 }
1879
1880 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1881 Lex.Lex();
1882
1883 // If a value is present, parse it.
1884 if (Lex.getCode() != tgtok::equal) {
1885 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001886 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001887 }
1888 Lex.Lex(); // Eat the '='
1889
Craig Topper011817a2014-04-09 04:50:04 +00001890 RecTy *IterType = nullptr;
Matthias Braunc66e7552016-12-05 06:41:54 +00001891 SmallVector<unsigned, 16> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001892
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001893 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001894 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001895 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001896 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001897 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001898 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001899 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001900 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001901 }
1902 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001903 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001904 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001905 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001906 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001907 }
1908 IterType = ListType->getElementType();
1909 break;
David Greenefb927af2012-02-22 16:09:41 +00001910 }
1911
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001912 case tgtok::IntVal: { // RangePiece.
1913 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001914 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001915 break;
David Greenefb927af2012-02-22 16:09:41 +00001916 }
1917
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001918 case tgtok::l_brace: { // '{' RangeList '}'
1919 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001920 ParseRangeList(Ranges);
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001921 if (Lex.getCode() != tgtok::r_brace) {
1922 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001923 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001924 }
1925 Lex.Lex();
1926 break;
1927 }
1928 }
David Greenefb927af2012-02-22 16:09:41 +00001929
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001930 if (!Ranges.empty()) {
1931 assert(!IterType && "Type already initialized?");
1932 IterType = IntRecTy::get();
1933 std::vector<Init*> Values;
Craig Topper8eb764c2015-06-02 06:19:28 +00001934 for (unsigned R : Ranges)
1935 Values.push_back(IntInit::get(R));
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001936 ForeachListValue = ListInit::get(Values, IterType);
1937 }
1938
1939 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001940 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001941
1942 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001943}
1944
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001945/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1946/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1947/// template args for a def, which may or may not be in a multiclass. If null,
1948/// these are the template args for a multiclass.
1949///
1950/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001951///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001952bool TGParser::ParseTemplateArgList(Record *CurRec) {
1953 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1954 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001955
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001956 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001957
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001958 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001959 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001960 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001961 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001962
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001963 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001964
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001965 while (Lex.getCode() == tgtok::comma) {
1966 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001967
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001968 // Read the following declarations.
1969 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001970 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001971 return true;
1972 TheRecToAddTo->addTemplateArg(TemplArg);
1973 }
Bob Wilson7248f862009-11-22 04:24:42 +00001974
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001975 if (Lex.getCode() != tgtok::greater)
1976 return TokError("expected '>' at end of template argument list");
1977 Lex.Lex(); // eat the '>'.
1978 return false;
1979}
1980
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001981/// ParseBodyItem - Parse a single item at within the body of a def or class.
1982///
1983/// BodyItem ::= Declaration ';'
1984/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1985bool TGParser::ParseBodyItem(Record *CurRec) {
1986 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001987 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001988 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001989
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001990 if (Lex.getCode() != tgtok::semi)
1991 return TokError("expected ';' after declaration");
1992 Lex.Lex();
1993 return false;
1994 }
1995
1996 // LET ID OptionalRangeList '=' Value ';'
1997 if (Lex.Lex() != tgtok::Id)
1998 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001999
Chris Lattner526c8cb2009-06-21 03:39:35 +00002000 SMLoc IdLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00002001 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002002 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00002003
Matthias Braunc66e7552016-12-05 06:41:54 +00002004 SmallVector<unsigned, 16> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00002005 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002006 return true;
2007 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002008
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002009 if (Lex.getCode() != tgtok::equal)
2010 return TokError("expected '=' in let expression");
2011 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002012
David Greene8618f952009-06-08 20:23:18 +00002013 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00002014 if (!Field)
Matthias Braun215ff842016-12-05 07:35:13 +00002015 return TokError("Value '" + FieldName->getValue() + "' unknown!");
David Greene8618f952009-06-08 20:23:18 +00002016
2017 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00002018
David Greeneaf8ee2c2011-07-29 22:43:06 +00002019 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00002020 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002021
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002022 if (Lex.getCode() != tgtok::semi)
2023 return TokError("expected ';' after let expression");
2024 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002025
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002026 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
2027}
2028
2029/// ParseBody - Read the body of a class or def. Return true on error, false on
2030/// success.
2031///
2032/// Body ::= ';'
2033/// Body ::= '{' BodyList '}'
2034/// BodyList BodyItem*
2035///
2036bool TGParser::ParseBody(Record *CurRec) {
2037 // If this is a null definition, just eat the semi and return.
2038 if (Lex.getCode() == tgtok::semi) {
2039 Lex.Lex();
2040 return false;
2041 }
Bob Wilson7248f862009-11-22 04:24:42 +00002042
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002043 if (Lex.getCode() != tgtok::l_brace)
2044 return TokError("Expected ';' or '{' to start body");
2045 // Eat the '{'.
2046 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002047
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002048 while (Lex.getCode() != tgtok::r_brace)
2049 if (ParseBodyItem(CurRec))
2050 return true;
2051
2052 // Eat the '}'.
2053 Lex.Lex();
2054 return false;
2055}
2056
Sean Silvacb1a75e2013-01-09 04:49:14 +00002057/// \brief Apply the current let bindings to \a CurRec.
2058/// \returns true on error, false otherwise.
2059bool TGParser::ApplyLetStack(Record *CurRec) {
Matthias Braunc66e7552016-12-05 06:41:54 +00002060 for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
Craig Topper8eb764c2015-06-02 06:19:28 +00002061 for (LetRecord &LR : LetInfo)
2062 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
Sean Silvacb1a75e2013-01-09 04:49:14 +00002063 return true;
2064 return false;
2065}
2066
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002067/// ParseObjectBody - Parse the body of a def or class. This consists of an
2068/// optional ClassList followed by a Body. CurRec is the current def or class
2069/// that is being parsed.
2070///
2071/// ObjectBody ::= BaseClassList Body
2072/// BaseClassList ::= /*empty*/
2073/// BaseClassList ::= ':' BaseClassListNE
2074/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
2075///
2076bool TGParser::ParseObjectBody(Record *CurRec) {
2077 // If there is a baseclass list, read it.
2078 if (Lex.getCode() == tgtok::colon) {
2079 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002080
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002081 // Read all of the subclasses.
2082 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002083 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002084 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002085 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002086
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002087 // Add it.
2088 if (AddSubClass(CurRec, SubClass))
2089 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002090
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002091 if (Lex.getCode() != tgtok::comma) break;
2092 Lex.Lex(); // eat ','.
2093 SubClass = ParseSubClassReference(CurRec, false);
2094 }
2095 }
2096
Sean Silvacb1a75e2013-01-09 04:49:14 +00002097 if (ApplyLetStack(CurRec))
2098 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002099
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002100 return ParseBody(CurRec);
2101}
2102
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002103/// ParseDef - Parse and return a top level or multiclass def, return the record
2104/// corresponding to it. This returns null on error.
2105///
2106/// DefInst ::= DEF ObjectName ObjectBody
2107///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002108bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00002109 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002110 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00002111 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002112
2113 // Parse ObjectName and make a record for it.
Craig Topper84138712014-11-29 05:31:10 +00002114 std::unique_ptr<Record> CurRecOwner;
Jordan Roseabdd99b2013-01-10 18:50:05 +00002115 Init *Name = ParseObjectName(CurMultiClass);
2116 if (Name)
Craig Topper84138712014-11-29 05:31:10 +00002117 CurRecOwner = make_unique<Record>(Name, DefLoc, Records);
Jordan Roseabdd99b2013-01-10 18:50:05 +00002118 else
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00002119 CurRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), DefLoc,
2120 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00002121 Record *CurRec = CurRecOwner.get(); // Keep a copy since we may release.
Bob Wilson7248f862009-11-22 04:24:42 +00002122
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002123 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002124 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002125
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002126 // Ensure redefinition doesn't happen.
Craig Topper84138712014-11-29 05:31:10 +00002127 if (Records.getDef(CurRec->getNameInitAsString()))
2128 return Error(DefLoc, "def '" + CurRec->getNameInitAsString()+
2129 "' already defined");
Craig Toppercdab2322014-11-29 05:52:51 +00002130 Records.addDef(std::move(CurRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00002131
2132 if (ParseObjectBody(CurRec))
2133 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002134 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002135 // Parse the body before adding this prototype to the DefPrototypes vector.
2136 // That way implicit definitions will be added to the DefPrototypes vector
2137 // before this object, instantiated prior to defs derived from this object,
2138 // and this available for indirect name resolution when defs derived from
2139 // this object are instantiated.
Craig Topper84138712014-11-29 05:31:10 +00002140 if (ParseObjectBody(CurRec))
Hal Finkela8c1f462014-01-02 20:47:09 +00002141 return true;
2142
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002143 // Otherwise, a def inside a multiclass, add it to the multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002144 for (const auto &Proto : CurMultiClass->DefPrototypes)
2145 if (Proto->getNameInit() == CurRec->getNameInit())
Craig Topper84138712014-11-29 05:31:10 +00002146 return Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
2147 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002148 CurMultiClass->DefPrototypes.push_back(std::move(CurRecOwner));
Anton Yartsev671dff12014-08-08 00:29:54 +00002149 } else if (ParseObjectBody(CurRec)) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002150 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002151 }
Bob Wilson7248f862009-11-22 04:24:42 +00002152
Craig Topper011817a2014-04-09 04:50:04 +00002153 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002154 // See Record::setName(). This resolve step will see any new name
2155 // for the def that might have been created when resolving
2156 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002157 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002158
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002159 // If ObjectBody has template arguments, it's an error.
2160 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002161
2162 if (CurMultiClass) {
2163 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002164 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
2165 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002166 assert(RV && "Template arg doesn't exist?");
2167 CurRec->addValue(*RV);
2168 }
2169 }
2170
Craig Toppera9642b42015-05-04 01:35:39 +00002171 if (ProcessForeachDefs(CurRec, DefLoc))
Craig Topper84138712014-11-29 05:31:10 +00002172 return Error(DefLoc, "Could not process loops for def" +
2173 CurRec->getNameInitAsString());
David Greenefb927af2012-02-22 16:09:41 +00002174
2175 return false;
2176}
2177
2178/// ParseForeach - Parse a for statement. Return the record corresponding
2179/// to it. This returns true on error.
2180///
2181/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2182/// Foreach ::= FOREACH Declaration IN Object
2183///
2184bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2185 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2186 Lex.Lex(); // Eat the 'for' token.
2187
2188 // Make a temporary object to record items associated with the for
2189 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002190 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002191 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002192 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002193 return TokError("expected declaration in for");
2194
2195 if (Lex.getCode() != tgtok::In)
2196 return TokError("Unknown tok");
2197 Lex.Lex(); // Eat the in
2198
2199 // Create a loop object and remember it.
2200 Loops.push_back(ForeachLoop(IterName, ListValue));
2201
2202 if (Lex.getCode() != tgtok::l_brace) {
2203 // FOREACH Declaration IN Object
2204 if (ParseObject(CurMultiClass))
2205 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002206 } else {
David Greenefb927af2012-02-22 16:09:41 +00002207 SMLoc BraceLoc = Lex.getLoc();
2208 // Otherwise, this is a group foreach.
2209 Lex.Lex(); // eat the '{'.
2210
2211 // Parse the object list.
2212 if (ParseObjectList(CurMultiClass))
2213 return true;
2214
2215 if (Lex.getCode() != tgtok::r_brace) {
2216 TokError("expected '}' at end of foreach command");
2217 return Error(BraceLoc, "to match this '{'");
2218 }
2219 Lex.Lex(); // Eat the }
2220 }
2221
2222 // We've processed everything in this loop.
2223 Loops.pop_back();
2224
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002225 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002226}
2227
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002228/// ParseClass - Parse a tblgen class definition.
2229///
2230/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2231///
2232bool TGParser::ParseClass() {
2233 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2234 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002235
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002236 if (Lex.getCode() != tgtok::Id)
2237 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002238
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002239 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2240 if (CurRec) {
2241 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002242 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002243 !CurRec->getSuperClasses().empty() ||
2244 !CurRec->getTemplateArgs().empty())
Craig Topper85c07002015-04-30 05:54:22 +00002245 return TokError("Class '" + CurRec->getNameInitAsString() +
2246 "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002247 } else {
2248 // If this is the first reference to this class, create and add it.
Hans Wennborgffbbd532014-11-30 00:31:49 +00002249 auto NewRec =
2250 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
Craig Toppercdab2322014-11-29 05:52:51 +00002251 CurRec = NewRec.get();
2252 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002253 }
2254 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002255
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002256 // If there are template args, parse them.
2257 if (Lex.getCode() == tgtok::less)
2258 if (ParseTemplateArgList(CurRec))
2259 return true;
2260
2261 // Finally, parse the object body.
2262 return ParseObjectBody(CurRec);
2263}
2264
2265/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2266/// of LetRecords.
2267///
2268/// LetList ::= LetItem (',' LetItem)*
2269/// LetItem ::= ID OptionalRangeList '=' Value
2270///
Matthias Braunc66e7552016-12-05 06:41:54 +00002271void TGParser::ParseLetList(SmallVectorImpl<LetRecord> &Result) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002272 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002273 if (Lex.getCode() != tgtok::Id) {
2274 TokError("expected identifier in let definition");
Matthias Braunc66e7552016-12-05 06:41:54 +00002275 Result.clear();
2276 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002277 }
Matthias Braunc66e7552016-12-05 06:41:54 +00002278
Matthias Braun215ff842016-12-05 07:35:13 +00002279 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattner526c8cb2009-06-21 03:39:35 +00002280 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002281 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002282
2283 // Check for an optional RangeList.
Matthias Braunc66e7552016-12-05 06:41:54 +00002284 SmallVector<unsigned, 16> Bits;
2285 if (ParseOptionalRangeList(Bits)) {
2286 Result.clear();
2287 return;
2288 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002289 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002290
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002291 if (Lex.getCode() != tgtok::equal) {
2292 TokError("expected '=' in let expression");
Matthias Braunc66e7552016-12-05 06:41:54 +00002293 Result.clear();
2294 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002295 }
2296 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002297
Craig Topper011817a2014-04-09 04:50:04 +00002298 Init *Val = ParseValue(nullptr);
Matthias Braunc66e7552016-12-05 06:41:54 +00002299 if (!Val) {
2300 Result.clear();
2301 return;
2302 }
Bob Wilson7248f862009-11-22 04:24:42 +00002303
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002304 // Now that we have everything, add the record.
Matthias Braun215ff842016-12-05 07:35:13 +00002305 Result.emplace_back(Name, Bits, Val, NameLoc);
Bob Wilson7248f862009-11-22 04:24:42 +00002306
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002307 if (Lex.getCode() != tgtok::comma)
Matthias Braunc66e7552016-12-05 06:41:54 +00002308 return;
Bob Wilson7248f862009-11-22 04:24:42 +00002309 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002310 }
2311}
2312
2313/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002314/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002315///
2316/// Object ::= LET LetList IN '{' ObjectList '}'
2317/// Object ::= LET LetList IN Object
2318///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002319bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002320 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2321 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002322
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002323 // Add this entry to the let stack.
Matthias Braunc66e7552016-12-05 06:41:54 +00002324 SmallVector<LetRecord, 8> LetInfo;
2325 ParseLetList(LetInfo);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002326 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002327 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002328
2329 if (Lex.getCode() != tgtok::In)
2330 return TokError("expected 'in' at end of top-level 'let'");
2331 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002332
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002333 // If this is a scalar let, just handle it now
2334 if (Lex.getCode() != tgtok::l_brace) {
2335 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002336 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002337 return true;
2338 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002339 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002340 // Otherwise, this is a group let.
2341 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002342
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002343 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002344 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002345 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002346
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002347 if (Lex.getCode() != tgtok::r_brace) {
2348 TokError("expected '}' at end of top level let command");
2349 return Error(BraceLoc, "to match this '{'");
2350 }
2351 Lex.Lex();
2352 }
Bob Wilson7248f862009-11-22 04:24:42 +00002353
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002354 // Outside this let scope, this let block is not active.
2355 LetStack.pop_back();
2356 return false;
2357}
2358
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002359/// ParseMultiClass - Parse a multiclass definition.
2360///
Bob Wilson3d948162009-04-28 19:41:44 +00002361/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002362/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2363/// MultiClassObject ::= DefInst
2364/// MultiClassObject ::= MultiClassInst
2365/// MultiClassObject ::= DefMInst
2366/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2367/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002368///
2369bool TGParser::ParseMultiClass() {
2370 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2371 Lex.Lex(); // Eat the multiclass token.
2372
2373 if (Lex.getCode() != tgtok::Id)
2374 return TokError("expected identifier after multiclass for name");
2375 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002376
Craig Topper7adf2bf2014-12-11 05:25:30 +00002377 auto Result =
2378 MultiClasses.insert(std::make_pair(Name,
2379 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2380
2381 if (!Result.second)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002382 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002383
Craig Topper7adf2bf2014-12-11 05:25:30 +00002384 CurMultiClass = Result.first->second.get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002385 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002386
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002387 // If there are template args, parse them.
2388 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002389 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002390 return true;
2391
David Greene7049e792009-04-24 16:55:41 +00002392 bool inherits = false;
2393
David Greene753ed8f2009-04-22 16:42:54 +00002394 // If there are submulticlasses, parse them.
2395 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002396 inherits = true;
2397
David Greene753ed8f2009-04-22 16:42:54 +00002398 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002399
David Greene753ed8f2009-04-22 16:42:54 +00002400 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002401 SubMultiClassReference SubMultiClass =
2402 ParseSubMultiClassReference(CurMultiClass);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002403 while (true) {
David Greene753ed8f2009-04-22 16:42:54 +00002404 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002405 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002406
David Greene753ed8f2009-04-22 16:42:54 +00002407 // Add it.
2408 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2409 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002410
David Greene753ed8f2009-04-22 16:42:54 +00002411 if (Lex.getCode() != tgtok::comma) break;
2412 Lex.Lex(); // eat ','.
2413 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2414 }
2415 }
2416
David Greene7049e792009-04-24 16:55:41 +00002417 if (Lex.getCode() != tgtok::l_brace) {
2418 if (!inherits)
2419 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002420 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002421 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002422 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002423 } else {
David Greene7049e792009-04-24 16:55:41 +00002424 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2425 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002426
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002427 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002428 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002429 default:
2430 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
2431 case tgtok::Let:
2432 case tgtok::Def:
2433 case tgtok::Defm:
2434 case tgtok::Foreach:
2435 if (ParseObject(CurMultiClass))
2436 return true;
2437 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002438 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002439 }
David Greene7049e792009-04-24 16:55:41 +00002440 Lex.Lex(); // eat the '}'.
2441 }
Bob Wilson7248f862009-11-22 04:24:42 +00002442
Craig Topper011817a2014-04-09 04:50:04 +00002443 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002444 return false;
2445}
2446
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002447Record *TGParser::InstantiateMulticlassDef(MultiClass &MC, Record *DefProto,
2448 Init *&DefmPrefix,
2449 SMRange DefmPrefixRange,
2450 ArrayRef<Init *> TArgs,
Matthias Braunc66e7552016-12-05 06:41:54 +00002451 ArrayRef<Init *> TemplateVals) {
David Greene5d5d88c2011-10-19 13:04:31 +00002452 // We need to preserve DefProto so it can be reused for later
2453 // instantiations, so create a new Record to inherit from it.
2454
David Greenedb445972011-10-05 22:42:07 +00002455 // Add in the defm name. If the defm prefix is empty, give each
2456 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2457 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2458 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002459
Jordan Roseabdd99b2013-01-10 18:50:05 +00002460 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002461 if (!DefmPrefix) {
Craig Topperf4412262017-06-01 06:56:16 +00002462 DefmPrefix = GetNewAnonymousName();
Jordan Roseabdd99b2013-01-10 18:50:05 +00002463 IsAnonymous = true;
2464 }
David Greene5d5d88c2011-10-19 13:04:31 +00002465
2466 Init *DefName = DefProto->getNameInit();
Sean Silvafb509ed2012-10-10 20:24:43 +00002467 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002468
Craig Topper011817a2014-04-09 04:50:04 +00002469 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002470 // We have a fully expanded string so there are no operators to
2471 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002472 DefName =
2473 BinOpInit::get(BinOpInit::STRCONCAT,
2474 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2475 StringRecTy::get())->Fold(DefProto, &MC),
2476 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2477 }
David Greene5d5d88c2011-10-19 13:04:31 +00002478
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002479 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002480 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002481 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Craig Topper84138712014-11-29 05:31:10 +00002482 auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002483
2484 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002485 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002486 Ref.Rec = DefProto;
Craig Topper84138712014-11-29 05:31:10 +00002487 AddSubClass(CurRec.get(), Ref);
David Greenedb445972011-10-05 22:42:07 +00002488
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002489 // Set the value for NAME. We don't resolve references to it 'til later,
2490 // though, so that uses in nested multiclass names don't get
2491 // confused.
Matthias Braun215ff842016-12-05 07:35:13 +00002492 if (SetValue(CurRec.get(), Ref.RefRange.Start, StringInit::get("NAME"), None,
2493 DefmPrefix, /*AllowSelfAssignment*/true)) {
Craig Topper85c07002015-04-30 05:54:22 +00002494 Error(DefmPrefixRange.Start, "Could not resolve " +
2495 CurRec->getNameInitAsString() + ":NAME to '" +
2496 DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002497 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002498 }
David Greene8bf0d722011-10-19 13:04:35 +00002499
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002500 // If the DefNameString didn't resolve, we probably have a reference to
2501 // NAME and need to replace it. We need to do at least this much greedily,
2502 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002503 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002504 RecordVal *DefNameRV = CurRec->getValue("NAME");
2505 CurRec->resolveReferencesTo(DefNameRV);
2506 }
2507
2508 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002509 // Now that we're at the top level, resolve all NAME references
2510 // in the resultant defs that weren't in the def names themselves.
2511 RecordVal *DefNameRV = CurRec->getValue("NAME");
2512 CurRec->resolveReferencesTo(DefNameRV);
2513
Hal Finkeld2497362015-05-21 04:32:56 +00002514 // Check if the name is a complex pattern.
2515 // If so, resolve it.
2516 DefName = CurRec->getNameInit();
2517 DefNameString = dyn_cast<StringInit>(DefName);
2518
2519 // OK the pattern is more complex than simply using NAME.
2520 // Let's use the heavy weaponery.
2521 if (!DefNameString) {
2522 ResolveMulticlassDefArgs(MC, CurRec.get(), DefmPrefixRange.Start,
2523 Lex.getLoc(), TArgs, TemplateVals,
2524 false/*Delete args*/);
2525 DefName = CurRec->getNameInit();
2526 DefNameString = dyn_cast<StringInit>(DefName);
2527
Nicolai Haehnledfda9dc2018-03-06 13:48:39 +00002528 if (!DefNameString) {
Hal Finkeld2497362015-05-21 04:32:56 +00002529 DefName = DefName->convertInitializerTo(StringRecTy::get());
Nicolai Haehnledfda9dc2018-03-06 13:48:39 +00002530 DefNameString = dyn_cast<StringInit>(DefName);
2531 }
Hal Finkeld2497362015-05-21 04:32:56 +00002532
Hal Finkeld2497362015-05-21 04:32:56 +00002533 if (!DefNameString) {
2534 PrintFatalError(CurRec->getLoc()[CurRec->getLoc().size() - 1],
2535 DefName->getAsUnquotedString() + " is not a string.");
2536 return nullptr;
2537 }
2538
2539 CurRec->setName(DefName);
2540 }
2541
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002542 // Now that NAME references are resolved and we're at the top level of
2543 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002544 // currently in a multiclass, it means this defm appears inside a
2545 // multiclass and its name won't be fully resolvable until we see
Hal Finkeld2497362015-05-21 04:32:56 +00002546 // the top-level defm. Therefore, we don't add this to the
2547 // RecordKeeper at this point. If we did we could get duplicate
David Greene8bf0d722011-10-19 13:04:35 +00002548 // defs as more than one probably refers to NAME or some other
2549 // common internal placeholder.
2550
2551 // Ensure redefinition doesn't happen.
2552 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002553 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002554 "' already defined, instantiating defm with subdef '" +
2555 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002556 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002557 }
2558
Craig Topper84138712014-11-29 05:31:10 +00002559 Record *CurRecSave = CurRec.get(); // Keep a copy before we release.
Craig Toppercdab2322014-11-29 05:52:51 +00002560 Records.addDef(std::move(CurRec));
Craig Topper84138712014-11-29 05:31:10 +00002561 return CurRecSave;
David Greene8bf0d722011-10-19 13:04:35 +00002562 }
2563
Craig Topper84138712014-11-29 05:31:10 +00002564 // FIXME This is bad but the ownership transfer to caller is pretty messy.
2565 // The unique_ptr in this function at least protects the exits above.
2566 return CurRec.release();
David Greenedb445972011-10-05 22:42:07 +00002567}
2568
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002569bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC, Record *CurRec,
2570 SMLoc DefmPrefixLoc, SMLoc SubClassLoc,
2571 ArrayRef<Init *> TArgs,
Matthias Braunc66e7552016-12-05 06:41:54 +00002572 ArrayRef<Init *> TemplateVals,
David Greenedb445972011-10-05 22:42:07 +00002573 bool DeleteArgs) {
Nicolai Haehnle7d697852018-03-05 15:21:19 +00002574 // Set all template arguments to the specified value or leave them as the
2575 // default if necessary, then resolve them all simultaneously.
2576 MapResolver R(CurRec);
2577
David Greenedb445972011-10-05 22:42:07 +00002578 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2579 // Check if a value is specified for this temp-arg.
2580 if (i < TemplateVals.size()) {
Craig Toppercfd81732016-01-04 03:15:08 +00002581 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], None, TemplateVals[i]))
David Greenedb445972011-10-05 22:42:07 +00002582 return true;
David Greenedb445972011-10-05 22:42:07 +00002583 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Craig Topper85c07002015-04-30 05:54:22 +00002584 return Error(SubClassLoc, "value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00002585 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +00002586 ") of multiclassclass '" + MC.Rec.getNameInitAsString() +
2587 "'");
David Greenedb445972011-10-05 22:42:07 +00002588 }
Nicolai Haehnle7d697852018-03-05 15:21:19 +00002589
2590 R.set(TArgs[i], CurRec->getValue(TArgs[i])->getValue());
2591
2592 if (DeleteArgs)
2593 CurRec->removeValue(TArgs[i]);
David Greenedb445972011-10-05 22:42:07 +00002594 }
Nicolai Haehnle7d697852018-03-05 15:21:19 +00002595
2596 CurRec->resolveReferences(R);
2597
David Greenedb445972011-10-05 22:42:07 +00002598 return false;
2599}
2600
2601bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2602 Record *CurRec,
2603 Record *DefProto,
2604 SMLoc DefmPrefixLoc) {
2605 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002606 if (ApplyLetStack(CurRec))
2607 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002608
David Greenedb445972011-10-05 22:42:07 +00002609 // Don't create a top level definition for defm inside multiclasses,
2610 // instead, only update the prototypes and bind the template args
2611 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002612 if (!CurMultiClass)
2613 return false;
Craig Toppereb4d7c62015-04-29 04:43:36 +00002614 for (const auto &Proto : CurMultiClass->DefPrototypes)
2615 if (Proto->getNameInit() == CurRec->getNameInit())
Sean Silvacc951b22013-01-09 05:28:12 +00002616 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2617 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002618 CurMultiClass->DefPrototypes.push_back(std::unique_ptr<Record>(CurRec));
David Greenedb445972011-10-05 22:42:07 +00002619
Sean Silvacc951b22013-01-09 05:28:12 +00002620 // Copy the template arguments for the multiclass into the new def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002621 for (Init * TA : CurMultiClass->Rec.getTemplateArgs()) {
2622 const RecordVal *RV = CurMultiClass->Rec.getValue(TA);
Sean Silvacc951b22013-01-09 05:28:12 +00002623 assert(RV && "Template arg doesn't exist?");
2624 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002625 }
2626
2627 return false;
2628}
2629
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002630/// ParseDefm - Parse the instantiation of a multiclass.
2631///
2632/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2633///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002634bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002635 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002636 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002637 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002638
Craig Topperb21afc62013-01-07 05:09:33 +00002639 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002640 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002641 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002642
Jordan Rosef12e8a92013-01-10 18:50:11 +00002643 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002644 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002645 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002646
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002647 // Keep track of the new generated record definitions.
2648 std::vector<Record*> NewRecDefs;
2649
2650 // This record also inherits from a regular class (non-multiclass)?
2651 bool InheritFromClass = false;
2652
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002653 // eat the colon.
2654 Lex.Lex();
2655
Chris Lattner526c8cb2009-06-21 03:39:35 +00002656 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002657 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002658
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002659 while (true) {
Craig Topper011817a2014-04-09 04:50:04 +00002660 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002661
2662 // To instantiate a multiclass, we need to first get the multiclass, then
2663 // instantiate each def contained in the multiclass with the SubClassRef
2664 // template parameters.
Craig Topper7adf2bf2014-12-11 05:25:30 +00002665 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper4ca40012014-11-30 01:20:17 +00002666 assert(MC && "Didn't lookup multiclass correctly?");
Matthias Braunc66e7552016-12-05 06:41:54 +00002667 ArrayRef<Init*> TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002668
2669 // Verify that the correct number of template arguments were specified.
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002670 ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002671 if (TArgs.size() < TemplateVals.size())
2672 return Error(SubClassLoc,
2673 "more template args specified than multiclass expects");
2674
2675 // Loop over all the def's in the multiclass, instantiating each one.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002676 for (const std::unique_ptr<Record> &DefProto : MC->DefPrototypes) {
Hal Finkeld2497362015-05-21 04:32:56 +00002677 // The record name construction goes as follow:
2678 // - If the def name is a string, prepend the prefix.
2679 // - If the def name is a more complex pattern, use that pattern.
Craig Topper7f36be92016-02-26 06:50:27 +00002680 // As a result, the record is instantiated before resolving
Hal Finkeld2497362015-05-21 04:32:56 +00002681 // arguments, as it would make its name a string.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002682 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto.get(), DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002683 SMRange(DefmLoc,
Hal Finkeld2497362015-05-21 04:32:56 +00002684 DefmPrefixEndLoc),
2685 TArgs, TemplateVals);
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002686 if (!CurRec)
2687 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002688
Craig Topper7f36be92016-02-26 06:50:27 +00002689 // Now that the record is instantiated, we can resolve arguments.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002690 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002691 TArgs, TemplateVals, true/*Delete args*/))
2692 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002693
Craig Toppereb4d7c62015-04-29 04:43:36 +00002694 if (ResolveMulticlassDef(*MC, CurRec, DefProto.get(), DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002695 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002696
Adam Nemete5a07162014-09-16 17:14:13 +00002697 // Defs that can be used by other definitions should be fully resolved
2698 // before any use.
2699 if (DefProto->isResolveFirst() && !CurMultiClass) {
2700 CurRec->resolveReferences();
2701 CurRec->setResolveFirst(false);
2702 }
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002703 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002704 }
2705
David Greenedb445972011-10-05 22:42:07 +00002706
David Greenef00919a2009-04-22 22:17:51 +00002707 if (Lex.getCode() != tgtok::comma) break;
2708 Lex.Lex(); // eat ','.
2709
Craig Topper998a39a2013-08-20 04:22:09 +00002710 if (Lex.getCode() != tgtok::Id)
2711 return TokError("expected identifier");
2712
David Greenef00919a2009-04-22 22:17:51 +00002713 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002714
2715 // A defm can inherit from regular classes (non-multiclass) as
2716 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002717 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002718
2719 if (InheritFromClass)
2720 break;
2721
Craig Topper011817a2014-04-09 04:50:04 +00002722 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002723 }
2724
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002725 if (InheritFromClass) {
2726 // Process all the classes to inherit as if they were part of a
2727 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002728 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002729 while (true) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002730 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002731 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002732
2733 // Get the expanded definition prototypes and teach them about
2734 // the record values the current class to inherit has
Craig Toppereb4d7c62015-04-29 04:43:36 +00002735 for (Record *CurRec : NewRecDefs) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002736 // Add it.
2737 if (AddSubClass(CurRec, SubClass))
2738 return true;
2739
Sean Silvacb1a75e2013-01-09 04:49:14 +00002740 if (ApplyLetStack(CurRec))
2741 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002742 }
2743
2744 if (Lex.getCode() != tgtok::comma) break;
2745 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002746 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002747 }
2748 }
2749
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002750 if (!CurMultiClass)
Craig Toppereb4d7c62015-04-29 04:43:36 +00002751 for (Record *CurRec : NewRecDefs)
David Greene50c09122011-08-10 18:27:46 +00002752 // See Record::setName(). This resolve step will see any new
2753 // name for the def that might have been created when resolving
2754 // inheritance, values and arguments above.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002755 CurRec->resolveReferences();
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002756
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002757 if (Lex.getCode() != tgtok::semi)
2758 return TokError("expected ';' at end of defm");
2759 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002760
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002761 return false;
2762}
2763
2764/// ParseObject
2765/// Object ::= ClassInst
2766/// Object ::= DefInst
2767/// Object ::= MultiClassInst
2768/// Object ::= DefMInst
2769/// Object ::= LETCommand '{' ObjectList '}'
2770/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002771bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002772 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002773 default:
2774 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002775 case tgtok::Let: return ParseTopLevelLet(MC);
2776 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002777 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002778 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002779 case tgtok::Class: return ParseClass();
2780 case tgtok::MultiClass: return ParseMultiClass();
2781 }
2782}
2783
2784/// ParseObjectList
2785/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002786bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002787 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002788 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002789 return true;
2790 }
2791 return false;
2792}
2793
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002794bool TGParser::ParseFile() {
2795 Lex.Lex(); // Prime the lexer.
2796 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002797
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002798 // If we have unread input at the end of the file, report it.
2799 if (Lex.getCode() == tgtok::Eof)
2800 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002801
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002802 return TokError("Unexpected input at top level");
2803}