blob: baf97baa4c2b50bf57fa61fc477b11377bae4f01 [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...
David Greeneaf8ee2c2011-07-29 22:43:06 +0000120 Init *BI = V->convertInitializerTo(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
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000124 // We should have a BitsInit type now.
Craig Toppered5a9502015-04-29 07:13:05 +0000125 BitsInit *BInit = cast<BitsInit>(BI);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000126
David Greeneaf8ee2c2011-07-29 22:43:06 +0000127 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000128
129 // Loop over bits, assigning values as appropriate.
130 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
131 unsigned Bit = BitList[i];
David Greeneb3da8122011-07-29 19:07:00 +0000132 if (NewBits[Bit])
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000133 return Error(Loc, "Cannot set bit #" + Twine(Bit) + " of value '" +
David Greene3ca42122011-10-19 13:02:39 +0000134 ValName->getAsUnquotedString() + "' more than once");
David Greeneb3da8122011-07-29 19:07:00 +0000135 NewBits[Bit] = BInit->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000136 }
137
138 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
Craig Topper011817a2014-04-09 04:50:04 +0000139 if (!NewBits[i])
David Greeneb3da8122011-07-29 19:07:00 +0000140 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000141
David Greenee32ebf22011-07-29 19:07:07 +0000142 V = BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000143 }
144
Pete Cooper040c6a62014-07-31 01:43:57 +0000145 if (RV->setValue(V)) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000146 std::string InitType;
Craig Toppera9642b42015-05-04 01:35:39 +0000147 if (BitsInit *BI = dyn_cast<BitsInit>(V))
Pete Cooper040c6a62014-07-31 01:43:57 +0000148 InitType = (Twine("' of type bit initializer with length ") +
149 Twine(BI->getNumBits())).str();
Nicolai Haehnlef19083d2018-02-22 15:26:21 +0000150 else if (TypedInit *TI = dyn_cast<TypedInit>(V))
151 InitType = (Twine("' of type '") + TI->getType()->getAsString()).str();
Craig Topper85c07002015-04-30 05:54:22 +0000152 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
Nicolai Haehnlef19083d2018-02-22 15:26:21 +0000153 "' of type '" + RV->getType()->getAsString() +
154 "' is incompatible with initializer '" +
155 V->getAsString() + InitType + "'");
Pete Cooper040c6a62014-07-31 01:43:57 +0000156 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000157 return false;
158}
159
160/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
161/// args as SubClass's template arguments.
Cedric Venetd1e179d2009-02-14 16:06:42 +0000162bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000163 Record *SC = SubClass.Rec;
164 // Add all of the values in the subclass into the current class.
Craig Topper8eb764c2015-06-02 06:19:28 +0000165 for (const RecordVal &Val : SC->getValues())
166 if (AddValue(CurRec, SubClass.RefRange.Start, Val))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000167 return true;
168
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000169 ArrayRef<Init *> TArgs = SC->getTemplateArgs();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000170
171 // Ensure that an appropriate number of template arguments are specified.
172 if (TArgs.size() < SubClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000173 return Error(SubClass.RefRange.Start,
174 "More template args specified than expected");
Bob Wilson7248f862009-11-22 04:24:42 +0000175
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000176 // Loop over all of the template arguments, setting them to the specified
177 // value or leaving them as the default if necessary.
178 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
179 if (i < SubClass.TemplateArgs.size()) {
180 // If a value is specified for this template arg, set it now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000181 if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000182 None, SubClass.TemplateArgs[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000183 return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000184
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000185 // Resolve it next.
186 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson7248f862009-11-22 04:24:42 +0000187
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000188 // Now remove it.
189 CurRec->removeValue(TArgs[i]);
190
191 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000192 return Error(SubClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000193 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000194 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000195 ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000196 }
197 }
198
199 // Since everything went well, we can now set the "superclass" list for the
200 // current record.
Craig Topper0e41d0b2016-01-18 19:52:37 +0000201 ArrayRef<std::pair<Record *, SMRange>> SCs = SC->getSuperClasses();
202 for (const auto &SCPair : SCs) {
203 if (CurRec->isSubClassOf(SCPair.first))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000204 return Error(SubClass.RefRange.Start,
Craig Topper0e41d0b2016-01-18 19:52:37 +0000205 "Already subclass of '" + SCPair.first->getName() + "'!\n");
206 CurRec->addSuperClass(SCPair.first, SCPair.second);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000207 }
Nicolai Haehnlec10570f2018-02-23 11:31:49 +0000208
209 if (CurRec->isSubClassOf(SC))
210 return Error(SubClass.RefRange.Start,
211 "Already subclass of '" + SC->getName() + "'!\n");
212 CurRec->addSuperClass(SC, SubClass.RefRange);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000213 return false;
214}
215
David Greene753ed8f2009-04-22 16:42:54 +0000216/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilsonf71e6562009-04-30 18:26:19 +0000217/// CurMC, resolving its template args as SubMultiClass's
David Greene753ed8f2009-04-22 16:42:54 +0000218/// template arguments.
Bob Wilsonf71e6562009-04-30 18:26:19 +0000219bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson92ab8202009-04-30 17:46:20 +0000220 SubMultiClassReference &SubMultiClass) {
David Greene753ed8f2009-04-22 16:42:54 +0000221 MultiClass *SMC = SubMultiClass.MC;
Bob Wilsonf71e6562009-04-30 18:26:19 +0000222 Record *CurRec = &CurMC->Rec;
David Greene753ed8f2009-04-22 16:42:54 +0000223
David Greene753ed8f2009-04-22 16:42:54 +0000224 // Add all of the values in the subclass into the current class.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000225 for (const auto &SMCVal : SMC->Rec.getValues())
226 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000227 return true;
228
Craig Toppera4ea4b02014-11-30 00:24:32 +0000229 unsigned newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000230
David Greene753ed8f2009-04-22 16:42:54 +0000231 // Add all of the defs in the subclass into the current multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000232 for (const std::unique_ptr<Record> &R : SMC->DefPrototypes) {
David Greene753ed8f2009-04-22 16:42:54 +0000233 // Clone the def and add it to the current multiclass
Craig Toppereb4d7c62015-04-29 04:43:36 +0000234 auto NewDef = make_unique<Record>(*R);
David Greene753ed8f2009-04-22 16:42:54 +0000235
236 // Add all of the values in the superclass into the current def.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000237 for (const auto &MCVal : CurRec->getValues())
238 if (AddValue(NewDef.get(), SubMultiClass.RefRange.Start, MCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000239 return true;
240
Craig Topperc3504c42014-12-11 05:25:33 +0000241 CurMC->DefPrototypes.push_back(std::move(NewDef));
David Greene753ed8f2009-04-22 16:42:54 +0000242 }
Bob Wilson3d948162009-04-28 19:41:44 +0000243
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000244 ArrayRef<Init *> SMCTArgs = SMC->Rec.getTemplateArgs();
David Greene753ed8f2009-04-22 16:42:54 +0000245
David Greene7049e792009-04-24 16:55:41 +0000246 // Ensure that an appropriate number of template arguments are
247 // specified.
David Greene753ed8f2009-04-22 16:42:54 +0000248 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000249 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000250 "More template args specified than expected");
Bob Wilson3d948162009-04-28 19:41:44 +0000251
David Greene753ed8f2009-04-22 16:42:54 +0000252 // Loop over all of the template arguments, setting them to the specified
253 // value or leaving them as the default if necessary.
254 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
262 // Resolve it next.
263 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson3d948162009-04-28 19:41:44 +0000264
David Greene753ed8f2009-04-22 16:42:54 +0000265 // Now remove it.
266 CurRec->removeValue(SMCTArgs[i]);
267
David Greene7049e792009-04-24 16:55:41 +0000268 // If a value is specified for this template arg, set it in the
269 // new defs now.
Craig Topperc3504c42014-12-11 05:25:33 +0000270 for (const auto &Def :
271 makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
272 if (SetValue(Def.get(), SubMultiClass.RefRange.Start, SMCTArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000273 None, SubMultiClass.TemplateArgs[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000274 return true;
275
276 // Resolve it next.
277 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
278
279 // Now remove it
280 Def->removeValue(SMCTArgs[i]);
281 }
282 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000283 return Error(SubMultiClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000284 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000285 Twine(i) + " (" + SMCTArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000286 ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000287 }
288 }
289
290 return false;
291}
292
David Greenefb927af2012-02-22 16:09:41 +0000293/// ProcessForeachDefs - Given a record, apply all of the variable
294/// values in all surrounding foreach loops, creating new records for
295/// each combination of values.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000296bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
297 if (Loops.empty())
298 return false;
299
David Greenefb927af2012-02-22 16:09:41 +0000300 // We want to instantiate a new copy of CurRec for each combination
301 // of nested loop iterator values. We don't want top instantiate
302 // any copies until we have values for each loop iterator.
303 IterSet IterVals;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000304 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000305}
306
307/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
308/// apply each of the variable values in this loop and then process
309/// subloops.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000310bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
311 // Recursively build a tuple of iterator values.
312 if (IterVals.size() != Loops.size()) {
313 assert(IterVals.size() < Loops.size());
314 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silvafb509ed2012-10-10 20:24:43 +0000315 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Craig Topper011817a2014-04-09 04:50:04 +0000316 if (!List) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000317 Error(Loc, "Loop list is not a list");
318 return true;
319 }
David Greenefb927af2012-02-22 16:09:41 +0000320
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000321 // Process each value.
Craig Topper664f6a02015-06-02 04:15:57 +0000322 for (unsigned i = 0; i < List->size(); ++i) {
Nicolai Haehnlec7711ba2018-02-23 10:46:21 +0000323 Init *ItemVal = List->getElement(i)->resolveReferences(*CurRec, nullptr);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000324 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
325 if (ProcessForeachDefs(CurRec, Loc, IterVals))
326 return true;
327 IterVals.pop_back();
328 }
329 return false;
330 }
331
332 // This is the bottom of the recursion. We have all of the iterator values
333 // for this point in the iteration space. Instantiate a new record to
334 // reflect this combination of values.
Craig Topper84138712014-11-29 05:31:10 +0000335 auto IterRec = make_unique<Record>(*CurRec);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000336
337 // Set the iterator values now.
Craig Topper8eb764c2015-06-02 06:19:28 +0000338 for (IterRecord &IR : IterVals) {
339 VarInit *IterVar = IR.IterVar;
340 TypedInit *IVal = dyn_cast<TypedInit>(IR.IterValue);
Craig Topper84138712014-11-29 05:31:10 +0000341 if (!IVal)
342 return Error(Loc, "foreach iterator value is untyped");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000343
Craig Topperf4412262017-06-01 06:56:16 +0000344 IterRec->addValue(RecordVal(IterVar->getNameInit(), IVal->getType(), false));
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000345
Matthias Braun215ff842016-12-05 07:35:13 +0000346 if (SetValue(IterRec.get(), Loc, IterVar->getNameInit(), None, IVal))
Craig Topper84138712014-11-29 05:31:10 +0000347 return Error(Loc, "when instantiating this def");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000348
349 // Resolve it next.
Matthias Braun215ff842016-12-05 07:35:13 +0000350 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getNameInit()));
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000351
352 // Remove it.
Matthias Braun215ff842016-12-05 07:35:13 +0000353 IterRec->removeValue(IterVar->getNameInit());
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000354 }
355
356 if (Records.getDef(IterRec->getNameInitAsString())) {
Artyom Skrobov8b985322014-06-10 12:41:14 +0000357 // If this record is anonymous, it's no problem, just generate a new name
Craig Topper84138712014-11-29 05:31:10 +0000358 if (!IterRec->isAnonymous())
359 return Error(Loc, "def already exists: " +IterRec->getNameInitAsString());
360
361 IterRec->setName(GetNewAnonymousName());
David Greenefb927af2012-02-22 16:09:41 +0000362 }
363
Craig Topper84138712014-11-29 05:31:10 +0000364 Record *IterRecSave = IterRec.get(); // Keep a copy before release.
Craig Toppercdab2322014-11-29 05:52:51 +0000365 Records.addDef(std::move(IterRec));
Craig Topper84138712014-11-29 05:31:10 +0000366 IterRecSave->resolveReferences();
David Greenefb927af2012-02-22 16:09:41 +0000367 return false;
368}
369
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000370//===----------------------------------------------------------------------===//
371// Parser Code
372//===----------------------------------------------------------------------===//
373
374/// isObjectStart - Return true if this is a valid first token for an Object.
375static bool isObjectStart(tgtok::TokKind K) {
376 return K == tgtok::Class || K == tgtok::Def ||
David Greenefb927af2012-02-22 16:09:41 +0000377 K == tgtok::Defm || K == tgtok::Let ||
378 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000379}
380
Alp Tokerce91fe52013-12-21 18:51:00 +0000381/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
382/// an identifier.
Craig Topperf4412262017-06-01 06:56:16 +0000383Init *TGParser::GetNewAnonymousName() {
384 return StringInit::get("anonymous_" + utostr(AnonCounter++));
Chris Lattner7538ed82010-10-05 22:51:56 +0000385}
386
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000387/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000388/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000389/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000390/// ObjectName ::= /*empty*/
391///
David Greene2affd672011-10-19 13:04:29 +0000392Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
393 switch (Lex.getCode()) {
394 case tgtok::colon:
395 case tgtok::semi:
396 case tgtok::l_brace:
397 // These are all of the tokens that can begin an object body.
398 // Some of these can also begin values but we disallow those cases
399 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000400 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000401 default:
402 break;
403 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000404
Craig Topper011817a2014-04-09 04:50:04 +0000405 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000406 if (CurMultiClass)
407 CurRec = &CurMultiClass->Rec;
408
Craig Topper011817a2014-04-09 04:50:04 +0000409 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000410 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000411 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000412 if (!CurRecName) {
413 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000414 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000415 }
416 Type = CurRecName->getType();
417 }
418
419 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000420}
421
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000422/// ParseClassID - Parse and resolve a reference to a class name. This returns
423/// null on error.
424///
425/// ClassID ::= ID
426///
427Record *TGParser::ParseClassID() {
428 if (Lex.getCode() != tgtok::Id) {
429 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000430 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000431 }
Bob Wilson7248f862009-11-22 04:24:42 +0000432
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000433 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000434 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000435 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000436
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000437 Lex.Lex();
438 return Result;
439}
440
Bob Wilson3d948162009-04-28 19:41:44 +0000441/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
442/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000443///
444/// MultiClassID ::= ID
445///
446MultiClass *TGParser::ParseMultiClassID() {
447 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000448 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000449 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000450 }
Bob Wilson3d948162009-04-28 19:41:44 +0000451
Craig Topper7adf2bf2014-12-11 05:25:30 +0000452 MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
Craig Topper4ca40012014-11-30 01:20:17 +0000453 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000454 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000455
David Greene753ed8f2009-04-22 16:42:54 +0000456 Lex.Lex();
Craig Topper4ca40012014-11-30 01:20:17 +0000457 return Result;
David Greene753ed8f2009-04-22 16:42:54 +0000458}
459
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000460/// ParseSubClassReference - Parse a reference to a subclass or to a templated
461/// subclass. This returns a SubClassRefTy with a null Record* on error.
462///
463/// SubClassRef ::= ClassID
464/// SubClassRef ::= ClassID '<' ValueList '>'
465///
466SubClassReference TGParser::
467ParseSubClassReference(Record *CurRec, bool isDefm) {
468 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000469 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000470
Sean Silva0657b402013-01-09 02:17:14 +0000471 if (isDefm) {
472 if (MultiClass *MC = ParseMultiClassID())
473 Result.Rec = &MC->Rec;
474 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000475 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000476 }
Craig Topper011817a2014-04-09 04:50:04 +0000477 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000478
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000479 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000480 if (Lex.getCode() != tgtok::less) {
481 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000482 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000483 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000484 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000485
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000486 if (Lex.getCode() == tgtok::greater) {
487 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000488 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000489 return Result;
490 }
Bob Wilson7248f862009-11-22 04:24:42 +0000491
Matthias Braunc66e7552016-12-05 06:41:54 +0000492 ParseValueList(Result.TemplateArgs, CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000493 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000494 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000495 return Result;
496 }
Bob Wilson7248f862009-11-22 04:24:42 +0000497
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000498 if (Lex.getCode() != tgtok::greater) {
499 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000500 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000501 return Result;
502 }
503 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000504 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000505
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000506 return Result;
507}
508
Bob Wilson3d948162009-04-28 19:41:44 +0000509/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
510/// templated submulticlass. This returns a SubMultiClassRefTy with a null
511/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000512///
513/// SubMultiClassRef ::= MultiClassID
514/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
515///
516SubMultiClassReference TGParser::
517ParseSubMultiClassReference(MultiClass *CurMC) {
518 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000519 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000520
David Greene753ed8f2009-04-22 16:42:54 +0000521 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000522 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000523
David Greene753ed8f2009-04-22 16:42:54 +0000524 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000525 if (Lex.getCode() != tgtok::less) {
526 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000527 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000528 }
David Greene753ed8f2009-04-22 16:42:54 +0000529 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000530
David Greene753ed8f2009-04-22 16:42:54 +0000531 if (Lex.getCode() == tgtok::greater) {
532 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000533 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000534 return Result;
535 }
Bob Wilson3d948162009-04-28 19:41:44 +0000536
Matthias Braunc66e7552016-12-05 06:41:54 +0000537 ParseValueList(Result.TemplateArgs, &CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000538 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000539 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000540 return Result;
541 }
Bob Wilson3d948162009-04-28 19:41:44 +0000542
David Greene753ed8f2009-04-22 16:42:54 +0000543 if (Lex.getCode() != tgtok::greater) {
544 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000545 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000546 return Result;
547 }
548 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000549 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000550
551 return Result;
552}
553
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000554/// ParseRangePiece - Parse a bit/value range.
555/// RangePiece ::= INTVAL
556/// RangePiece ::= INTVAL '-' INTVAL
557/// RangePiece ::= INTVAL INTVAL
Matthias Braunc66e7552016-12-05 06:41:54 +0000558bool TGParser::ParseRangePiece(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000559 if (Lex.getCode() != tgtok::IntVal) {
560 TokError("expected integer or bitrange");
561 return true;
562 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000563 int64_t Start = Lex.getCurIntVal();
564 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000565
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000566 if (Start < 0)
567 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000568
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000569 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000570 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000571 Ranges.push_back(Start);
572 return false;
573 case tgtok::minus:
574 if (Lex.Lex() != tgtok::IntVal) {
575 TokError("expected integer value as end of range");
576 return true;
577 }
578 End = Lex.getCurIntVal();
579 break;
580 case tgtok::IntVal:
581 End = -Lex.getCurIntVal();
582 break;
583 }
Bob Wilson7248f862009-11-22 04:24:42 +0000584 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000585 return TokError("invalid range, cannot be negative");
586 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000587
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000588 // Add to the range.
Craig Toppera9642b42015-05-04 01:35:39 +0000589 if (Start < End)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000590 for (; Start <= End; ++Start)
591 Ranges.push_back(Start);
Craig Toppera9642b42015-05-04 01:35:39 +0000592 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000593 for (; Start >= End; --Start)
594 Ranges.push_back(Start);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000595 return false;
596}
597
598/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
599///
600/// RangeList ::= RangePiece (',' RangePiece)*
601///
Matthias Braunc66e7552016-12-05 06:41:54 +0000602void TGParser::ParseRangeList(SmallVectorImpl<unsigned> &Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000603 // Parse the first piece.
Matthias Braunc66e7552016-12-05 06:41:54 +0000604 if (ParseRangePiece(Result)) {
605 Result.clear();
606 return;
607 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000608 while (Lex.getCode() == tgtok::comma) {
609 Lex.Lex(); // Eat the comma.
610
611 // Parse the next range piece.
Matthias Braunc66e7552016-12-05 06:41:54 +0000612 if (ParseRangePiece(Result)) {
613 Result.clear();
614 return;
615 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000616 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000617}
618
619/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
620/// OptionalRangeList ::= '<' RangeList '>'
621/// OptionalRangeList ::= /*empty*/
Matthias Braunc66e7552016-12-05 06:41:54 +0000622bool TGParser::ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000623 if (Lex.getCode() != tgtok::less)
624 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000625
Chris Lattner526c8cb2009-06-21 03:39:35 +0000626 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000627 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000628
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000629 // Parse the range list.
Matthias Braunc66e7552016-12-05 06:41:54 +0000630 ParseRangeList(Ranges);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000631 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000632
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000633 if (Lex.getCode() != tgtok::greater) {
634 TokError("expected '>' at end of range list");
635 return Error(StartLoc, "to match this '<'");
636 }
637 Lex.Lex(); // eat the '>'.
638 return false;
639}
640
641/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
642/// OptionalBitList ::= '{' RangeList '}'
643/// OptionalBitList ::= /*empty*/
Matthias Braunc66e7552016-12-05 06:41:54 +0000644bool TGParser::ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000645 if (Lex.getCode() != tgtok::l_brace)
646 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000647
Chris Lattner526c8cb2009-06-21 03:39:35 +0000648 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000649 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000650
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000651 // Parse the range list.
Matthias Braunc66e7552016-12-05 06:41:54 +0000652 ParseRangeList(Ranges);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000653 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000654
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000655 if (Lex.getCode() != tgtok::r_brace) {
656 TokError("expected '}' at end of bit list");
657 return Error(StartLoc, "to match this '{'");
658 }
659 Lex.Lex(); // eat the '}'.
660 return false;
661}
662
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000663/// ParseType - Parse and return a tblgen type. This returns null on error.
664///
665/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000666/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000667/// Type ::= BIT // bit type
668/// Type ::= BITS '<' INTVAL '>' // bits<x> type
669/// Type ::= INT // int type
670/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000671/// Type ::= DAG // dag type
672/// Type ::= ClassID // Record Type
673///
674RecTy *TGParser::ParseType() {
675 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000676 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000677 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Tim Northover88403d72016-07-05 21:22:55 +0000678 case tgtok::Code: Lex.Lex(); return CodeRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000679 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
680 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000681 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000682 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000683 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000684 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000685 case tgtok::Bits: {
686 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
687 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000688 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000689 }
690 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
691 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000692 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000693 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000694 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000695 if (Lex.Lex() != tgtok::greater) { // Eat count.
696 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000697 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000698 }
699 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000700 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000701 }
702 case tgtok::List: {
703 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
704 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000705 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000706 }
707 Lex.Lex(); // Eat '<'
708 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000709 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000710
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000711 if (Lex.getCode() != tgtok::greater) {
712 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000713 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000714 }
715 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000716 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000717 }
Bob Wilson7248f862009-11-22 04:24:42 +0000718 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000719}
720
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000721/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
722/// has already been read.
Matthias Braun215ff842016-12-05 07:35:13 +0000723Init *TGParser::ParseIDValue(Record *CurRec, StringInit *Name, SMLoc NameLoc,
David Greened4263a62011-10-19 13:04:20 +0000724 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000725 if (CurRec) {
726 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000727 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000728
Matthias Braun215ff842016-12-05 07:35:13 +0000729 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
David Greenedb10e692011-10-19 13:02:42 +0000730
David Greene47a665e2011-10-05 22:42:54 +0000731 if (CurMultiClass)
Matthias Braun215ff842016-12-05 07:35:13 +0000732 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
David Greenedb10e692011-10-19 13:02:42 +0000733 "::");
David Greene47a665e2011-10-05 22:42:54 +0000734
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000735 if (CurRec->isTemplateArg(TemplateArgName)) {
736 const RecordVal *RV = CurRec->getValue(TemplateArgName);
737 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000738 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000739 }
Matthias Braun215ff842016-12-05 07:35:13 +0000740 }
Bob Wilson7248f862009-11-22 04:24:42 +0000741
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000742 if (CurMultiClass) {
Nicolai Haehnle3c80e4c2018-03-05 14:01:38 +0000743 if (Name->getValue() == "NAME")
744 return VarInit::get(Name, StringRecTy::get());
745
Matthias Braun215ff842016-12-05 07:35:13 +0000746 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name, "::");
David Greenedb10e692011-10-19 13:02:42 +0000747
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000748 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
749 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
750 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000751 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000752 }
753 }
Bob Wilson7248f862009-11-22 04:24:42 +0000754
David Greenefb927af2012-02-22 16:09:41 +0000755 // If this is in a foreach loop, make sure it's not a loop iterator
Craig Toppereb4d7c62015-04-29 04:43:36 +0000756 for (const auto &L : Loops) {
757 VarInit *IterVar = dyn_cast<VarInit>(L.IterVar);
Matthias Braun215ff842016-12-05 07:35:13 +0000758 if (IterVar && IterVar->getNameInit() == Name)
David Greenefb927af2012-02-22 16:09:41 +0000759 return IterVar;
760 }
761
David Greene232bd602011-10-19 13:04:21 +0000762 if (Mode == ParseNameMode)
Matthias Braun215ff842016-12-05 07:35:13 +0000763 return Name;
David Greene232bd602011-10-19 13:04:21 +0000764
Matthias Braun215ff842016-12-05 07:35:13 +0000765 if (Record *D = Records.getDef(Name->getValue()))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000766 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000767
David Greene232bd602011-10-19 13:04:21 +0000768 if (Mode == ParseValueMode) {
Matthias Braun215ff842016-12-05 07:35:13 +0000769 Error(NameLoc, "Variable not defined: '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000770 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000771 }
Craig Toppera9642b42015-05-04 01:35:39 +0000772
Matthias Braun215ff842016-12-05 07:35:13 +0000773 return Name;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000774}
775
David Greene5d0c0512009-05-14 20:54:48 +0000776/// ParseOperation - Parse an operator. This returns null on error.
777///
778/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
779///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000780Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000781 switch (Lex.getCode()) {
782 default:
783 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000784 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000785 case tgtok::XHead:
786 case tgtok::XTail:
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000787 case tgtok::XSize:
David Greene2f7cf7f2011-01-07 17:05:37 +0000788 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000789 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
790 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000791 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000792
David Greenee8f3b272009-05-14 21:22:49 +0000793 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000794 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000795 case tgtok::XCast:
796 Lex.Lex(); // eat the operation
797 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000798
David Greenee8f3b272009-05-14 21:22:49 +0000799 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000800
Craig Topper011817a2014-04-09 04:50:04 +0000801 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000802 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000803 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000804 }
David Greene5d0c0512009-05-14 20:54:48 +0000805
David Greenee8f3b272009-05-14 21:22:49 +0000806 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000807 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000808 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000809 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000810 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000811 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000812 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000813 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000814 break;
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000815 case tgtok::XSize:
816 Lex.Lex();
817 Code = UnOpInit::SIZE;
818 Type = IntRecTy::get();
819 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000820 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000821 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000822 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000823 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000824 break;
David Greenee8f3b272009-05-14 21:22:49 +0000825 }
826 if (Lex.getCode() != tgtok::l_paren) {
827 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000828 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000829 }
830 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000831
David Greeneaf8ee2c2011-07-29 22:43:06 +0000832 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000833 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000834
Craig Topper85c07002015-04-30 05:54:22 +0000835 if (Code == UnOpInit::HEAD ||
836 Code == UnOpInit::TAIL ||
837 Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000838 ListInit *LHSl = dyn_cast<ListInit>(LHS);
839 StringInit *LHSs = dyn_cast<StringInit>(LHS);
840 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000841 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000842 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000843 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000844 }
845 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000846 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
847 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000848 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +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 }
853
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000854 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL ||
855 Code == UnOpInit::SIZE) {
Craig Topper011817a2014-04-09 04:50:04 +0000856 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000857 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000858 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000859 }
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000860 }
Bob Wilson7248f862009-11-22 04:24:42 +0000861
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000862 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
Craig Topperec9072d2015-05-14 05:53:53 +0000863 if (LHSl && LHSl->empty()) {
David Greened571b3c2009-05-14 22:38:31 +0000864 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000865 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000866 }
867 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000868 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000869 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000870 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000871 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000872 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000873 }
Craig Toppera9642b42015-05-04 01:35:39 +0000874 Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
875 : ListRecTy::get(Itemt->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000876 } else {
David Greened571b3c2009-05-14 22:38:31 +0000877 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000878 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000879 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000880 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000881 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000882 }
Craig Toppera9642b42015-05-04 01:35:39 +0000883 Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
David Greened571b3c2009-05-14 22:38:31 +0000884 }
885 }
886 }
887
David Greenee8f3b272009-05-14 21:22:49 +0000888 if (Lex.getCode() != tgtok::r_paren) {
889 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000890 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000891 }
892 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000893 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000894 }
David Greene5d0c0512009-05-14 20:54:48 +0000895
896 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000897 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000898 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000899 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +0000900 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000901 case tgtok::XSRL:
902 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000903 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000904 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000905 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000906 tgtok::TokKind OpTok = Lex.getCode();
907 SMLoc OpLoc = Lex.getLoc();
908 Lex.Lex(); // eat the operation
909
David Greene5d0c0512009-05-14 20:54:48 +0000910 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000911 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000912
Chris Lattner61ea00b2010-10-05 23:58:18 +0000913 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000914 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000915 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000916 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000917 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000918 case tgtok::XOR: Code = BinOpInit::OR; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000919 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
920 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
921 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
922 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000923 case tgtok::XListConcat:
924 Code = BinOpInit::LISTCONCAT;
925 // We don't know the list type until we parse the first argument
926 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000927 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000928 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000929 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000930 break;
David Greene5d0c0512009-05-14 20:54:48 +0000931 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000932
David Greene5d0c0512009-05-14 20:54:48 +0000933 if (Lex.getCode() != tgtok::l_paren) {
934 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000935 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000936 }
937 Lex.Lex(); // eat the '('
938
David Greeneaf8ee2c2011-07-29 22:43:06 +0000939 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000940
Chris Lattner61ea00b2010-10-05 23:58:18 +0000941 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000942 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000943
Chris Lattner61ea00b2010-10-05 23:58:18 +0000944 while (Lex.getCode() == tgtok::comma) {
945 Lex.Lex(); // eat the ','
946
947 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000948 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000949 }
David Greene5d0c0512009-05-14 20:54:48 +0000950
951 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000952 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000953 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000954 }
955 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000956
Daniel Sanders314e80e2014-05-07 10:13:19 +0000957 // If we are doing !listconcat, we should know the type by now
958 if (OpTok == tgtok::XListConcat) {
Nicolai Haehnlee4a2cf52018-02-22 15:26:28 +0000959 if (TypedInit *Arg0 = dyn_cast<TypedInit>(InitList[0]))
Daniel Sanders314e80e2014-05-07 10:13:19 +0000960 Type = Arg0->getType();
961 else {
Matthias Braun8c209aa2017-01-28 02:02:38 +0000962 InitList[0]->print(errs());
Daniel Sanders314e80e2014-05-07 10:13:19 +0000963 Error(OpLoc, "expected a list");
964 return nullptr;
965 }
966 }
967
Chris Lattner61ea00b2010-10-05 23:58:18 +0000968 // We allow multiple operands to associative operators like !strconcat as
969 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000970 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000971 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000972 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000973 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
974 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000975 InitList.back() = RHS;
976 }
977 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000978
Chris Lattner61ea00b2010-10-05 23:58:18 +0000979 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000980 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000981 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000982
Chris Lattner61ea00b2010-10-05 23:58:18 +0000983 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +0000984 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000985 }
986
David Greene3587eed2009-05-14 23:26:46 +0000987 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +0000988 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +0000989 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
990 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000991 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000992
David Greene98ed3c72009-05-14 21:54:42 +0000993 tgtok::TokKind LexCode = Lex.getCode();
994 Lex.Lex(); // eat the operation
995 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +0000996 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +0000997 case tgtok::XIf:
998 Code = TernOpInit::IF;
999 break;
David Greenee917fff2009-05-14 22:23:47 +00001000 case tgtok::XForEach:
1001 Code = TernOpInit::FOREACH;
1002 break;
David Greene98ed3c72009-05-14 21:54:42 +00001003 case tgtok::XSubst:
1004 Code = TernOpInit::SUBST;
1005 break;
1006 }
1007 if (Lex.getCode() != tgtok::l_paren) {
1008 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001009 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001010 }
1011 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001012
David Greeneaf8ee2c2011-07-29 22:43:06 +00001013 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001014 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001015
David Greene98ed3c72009-05-14 21:54:42 +00001016 if (Lex.getCode() != tgtok::comma) {
1017 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001018 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001019 }
1020 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001021
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001022 Init *MHS = ParseValue(CurRec, ItemType);
1023 if (!MHS)
1024 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001025
David Greene98ed3c72009-05-14 21:54:42 +00001026 if (Lex.getCode() != tgtok::comma) {
1027 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001028 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001029 }
1030 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001031
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001032 Init *RHS = ParseValue(CurRec, ItemType);
1033 if (!RHS)
1034 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001035
David Greene98ed3c72009-05-14 21:54:42 +00001036 if (Lex.getCode() != tgtok::r_paren) {
1037 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001038 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001039 }
1040 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001041
David Greene98ed3c72009-05-14 21:54:42 +00001042 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001043 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001044 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001045 RecTy *MHSTy = nullptr;
1046 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001047
Sean Silvafb509ed2012-10-10 20:24:43 +00001048 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001049 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001050 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001051 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001052 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001053 MHSTy = BitRecTy::get();
1054
Sean Silvafb509ed2012-10-10 20:24:43 +00001055 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001056 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001057 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001058 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001059 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001060 RHSTy = BitRecTy::get();
1061
1062 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001063 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001064 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001065 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001066 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001067
1068 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001069 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001070 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001071 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001072
Nicolai Haehnlec10570f2018-02-23 11:31:49 +00001073 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1074 Type = RHSTy;
1075 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1076 Type = MHSTy;
1077 } else {
1078 TokError("inconsistent types for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001079 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001080 }
1081 break;
1082 }
David Greenee917fff2009-05-14 22:23:47 +00001083 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001084 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
Craig Topper011817a2014-04-09 04:50:04 +00001085 if (!MHSt) {
David Greenee917fff2009-05-14 22:23:47 +00001086 TokError("could not get type for !foreach");
Craig Topper011817a2014-04-09 04:50:04 +00001087 return nullptr;
David Greenee917fff2009-05-14 22:23:47 +00001088 }
1089 Type = MHSt->getType();
Nicolai Haehnle6d649152018-02-22 15:26:35 +00001090 if (isa<ListRecTy>(Type)) {
1091 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1092 if (!RHSt) {
1093 TokError("could not get type of !foreach list elements");
1094 return nullptr;
1095 }
1096 Type = RHSt->getType()->getListTy();
1097 }
David Greenee917fff2009-05-14 22:23:47 +00001098 break;
1099 }
David Greene98ed3c72009-05-14 21:54:42 +00001100 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001101 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001102 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001103 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001104 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001105 }
1106 Type = RHSt->getType();
1107 break;
1108 }
1109 }
David Greenee32ebf22011-07-29 19:07:07 +00001110 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001111 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001112 }
David Greene5d0c0512009-05-14 20:54:48 +00001113 }
David Greene5d0c0512009-05-14 20:54:48 +00001114}
1115
1116/// ParseOperatorType - Parse a type for an operator. This returns
1117/// null on error.
1118///
1119/// OperatorType ::= '<' Type '>'
1120///
Dan Gohman1432ef82009-08-12 22:10:57 +00001121RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001122 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001123
1124 if (Lex.getCode() != tgtok::less) {
1125 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001126 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001127 }
1128 Lex.Lex(); // eat the <
1129
1130 Type = ParseType();
1131
Craig Topper011817a2014-04-09 04:50:04 +00001132 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001133 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001134 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001135 }
1136
1137 if (Lex.getCode() != tgtok::greater) {
1138 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001139 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001140 }
1141 Lex.Lex(); // eat the >
1142
1143 return Type;
1144}
1145
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001146/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1147///
1148/// SimpleValue ::= IDValue
1149/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001150/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001151/// SimpleValue ::= CODEFRAGMENT
1152/// SimpleValue ::= '?'
1153/// SimpleValue ::= '{' ValueList '}'
1154/// SimpleValue ::= ID '<' ValueListNE '>'
1155/// SimpleValue ::= '[' ValueList ']'
1156/// SimpleValue ::= '(' IDValue DagArgList ')'
1157/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001158/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001159/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1160/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1161/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001162/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001163/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1164///
David Greened4263a62011-10-19 13:04:20 +00001165Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1166 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001167 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001168 switch (Lex.getCode()) {
1169 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001170 case tgtok::paste:
1171 // This is a leading paste operation. This is deprecated but
1172 // still exists in some .td files. Ignore it.
1173 Lex.Lex(); // Skip '#'.
1174 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001175 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001176 case tgtok::BinaryIntVal: {
1177 auto BinaryVal = Lex.getCurBinaryIntVal();
1178 SmallVector<Init*, 16> Bits(BinaryVal.second);
1179 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001180 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001181 R = BitsInit::get(Bits);
1182 Lex.Lex();
1183 break;
1184 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001185 case tgtok::StrVal: {
1186 std::string Val = Lex.getCurStrVal();
1187 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001188
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001189 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001190 while (Lex.getCode() == tgtok::StrVal) {
1191 Val += Lex.getCurStrVal();
1192 Lex.Lex();
1193 }
Bob Wilson7248f862009-11-22 04:24:42 +00001194
David Greenee32ebf22011-07-29 19:07:07 +00001195 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001196 break;
1197 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001198 case tgtok::CodeFragment:
Tim Northover88403d72016-07-05 21:22:55 +00001199 R = CodeInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001200 Lex.Lex();
1201 break;
1202 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001203 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001204 Lex.Lex();
1205 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001206 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001207 SMLoc NameLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00001208 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001209 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001210 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001211
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001212 // Value ::= ID '<' ValueListNE '>'
1213 if (Lex.Lex() == tgtok::greater) {
1214 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001215 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001216 }
David Greene8618f952009-06-08 20:23:18 +00001217
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001218 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1219 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1220 // body.
Matthias Braun215ff842016-12-05 07:35:13 +00001221 Record *Class = Records.getClass(Name->getValue());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001222 if (!Class) {
Matthias Braun215ff842016-12-05 07:35:13 +00001223 Error(NameLoc, "Expected a class name, got '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001224 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001225 }
David Greene8618f952009-06-08 20:23:18 +00001226
Matthias Braunc66e7552016-12-05 06:41:54 +00001227 SubClassReference SCRef;
1228 ParseValueList(SCRef.TemplateArgs, CurRec, Class);
1229 if (SCRef.TemplateArgs.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001230
David Greene8618f952009-06-08 20:23:18 +00001231 if (Lex.getCode() != tgtok::greater) {
1232 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001233 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001234 }
1235 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001236 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001237
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001238 // Create the new record, set it as CurRec temporarily.
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00001239 auto NewRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), NameLoc,
1240 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001241 Record *NewRec = NewRecOwner.get(); // Keep a copy since we may release.
Jordan Rosef12e8a92013-01-10 18:50:11 +00001242 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001243 SCRef.Rec = Class;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001244 // Add info about the subclass to NewRec.
Craig Topper84138712014-11-29 05:31:10 +00001245 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001246 return nullptr;
Craig Topper84138712014-11-29 05:31:10 +00001247
Hal Finkela8c1f462014-01-02 20:47:09 +00001248 if (!CurMultiClass) {
1249 NewRec->resolveReferences();
Craig Toppercdab2322014-11-29 05:52:51 +00001250 Records.addDef(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001251 } else {
Adam Nemete5a07162014-09-16 17:14:13 +00001252 // This needs to get resolved once the multiclass template arguments are
1253 // known before any use.
1254 NewRec->setResolveFirst(true);
Hal Finkela8c1f462014-01-02 20:47:09 +00001255 // Otherwise, we're inside a multiclass, add it to the multiclass.
Craig Topperc3504c42014-12-11 05:25:33 +00001256 CurMultiClass->DefPrototypes.push_back(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001257
1258 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00001259 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
1260 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Hal Finkela8c1f462014-01-02 20:47:09 +00001261 assert(RV && "Template arg doesn't exist?");
1262 NewRec->addValue(*RV);
1263 }
1264
1265 // We can't return the prototype def here, instead return:
1266 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1267 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1268 assert(MCNameRV && "multiclass record must have a NAME");
1269
1270 return UnOpInit::get(UnOpInit::CAST,
1271 BinOpInit::get(BinOpInit::STRCONCAT,
1272 VarInit::get(MCNameRV->getName(),
1273 MCNameRV->getType()),
1274 NewRec->getNameInit(),
1275 StringRecTy::get()),
1276 Class->getDefInit()->getType());
1277 }
Bob Wilson7248f862009-11-22 04:24:42 +00001278
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001279 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001280 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001281 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001282 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001283 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001284 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001285 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001286
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001287 if (Lex.getCode() != tgtok::r_brace) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001288 ParseValueList(Vals, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001289 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001290 }
1291 if (Lex.getCode() != tgtok::r_brace) {
1292 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001293 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001294 }
1295 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001296
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001297 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001298
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001299 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1300 // first. We'll first read everything in to a vector, then we can reverse
1301 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001302 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001303 // FIXME: The following two loops would not be duplicated
1304 // if the API was a little more orthogonal.
1305
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001306 // bits<n> values are allowed to initialize n bits.
1307 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1308 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1309 NewBits.push_back(BI->getBit((e - i) - 1));
1310 continue;
1311 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001312 // bits<n> can also come from variable initializers.
1313 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1314 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1315 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1316 NewBits.push_back(VI->getBit((e - i) - 1));
1317 continue;
1318 }
1319 // Fallthrough to try convert this to a bit.
1320 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001321 // All other values must be convertible to just a single bit.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001322 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001323 if (!Bit) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001324 Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
Chris Lattner52416952007-11-22 21:06:59 +00001325 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001326 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001327 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001328 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001329 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001330 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001331 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001332 }
1333 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1334 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001335 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001336
Craig Topper011817a2014-04-09 04:50:04 +00001337 RecTy *DeducedEltTy = nullptr;
1338 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001339
Craig Topper011817a2014-04-09 04:50:04 +00001340 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001341 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001342 if (!ListType) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001343 TokError(Twine("Type mismatch for list, expected list type, got ") +
1344 ItemType->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001345 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001346 }
1347 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001348 }
David Greene8618f952009-06-08 20:23:18 +00001349
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001350 if (Lex.getCode() != tgtok::r_square) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001351 ParseValueList(Vals, CurRec, nullptr,
1352 GivenListTy ? GivenListTy->getElementType() : nullptr);
Craig Topper011817a2014-04-09 04:50:04 +00001353 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001354 }
1355 if (Lex.getCode() != tgtok::r_square) {
1356 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001357 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001358 }
1359 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001360
Craig Topper011817a2014-04-09 04:50:04 +00001361 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001362 if (Lex.getCode() == tgtok::less) {
1363 // Optional list element type
1364 Lex.Lex(); // eat the '<'
1365
1366 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001367 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001368 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001369 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001370 }
1371
1372 if (Lex.getCode() != tgtok::greater) {
1373 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001374 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001375 }
1376 Lex.Lex(); // eat the '>'
1377 }
1378
1379 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001380 RecTy *EltTy = nullptr;
Craig Toppereb4d7c62015-04-29 04:43:36 +00001381 for (Init *V : Vals) {
1382 TypedInit *TArg = dyn_cast<TypedInit>(V);
Craig Topper011817a2014-04-09 04:50:04 +00001383 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001384 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001385 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001386 }
Craig Topper011817a2014-04-09 04:50:04 +00001387 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001388 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001389 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001390 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001391 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001392 }
Bob Wilson7248f862009-11-22 04:24:42 +00001393 } else {
David Greene8618f952009-06-08 20:23:18 +00001394 EltTy = TArg->getType();
1395 }
1396 }
1397
Craig Topper011817a2014-04-09 04:50:04 +00001398 if (GivenEltTy) {
1399 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001400 // Verify consistency
1401 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1402 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001403 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001404 }
1405 }
1406 EltTy = GivenEltTy;
1407 }
1408
Craig Topper011817a2014-04-09 04:50:04 +00001409 if (!EltTy) {
1410 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001411 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001412 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001413 }
1414 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001415 } else {
David Greene8618f952009-06-08 20:23:18 +00001416 // Make sure the deduced type is compatible with the given type
1417 if (GivenListTy) {
1418 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
Nicolai Haehnlef19083d2018-02-22 15:26:21 +00001419 TokError(Twine("Element type mismatch for list: element type '") +
1420 EltTy->getAsString() + "' not convertible to '" +
1421 GivenListTy->getElementType()->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001422 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001423 }
1424 }
1425 DeducedEltTy = EltTy;
1426 }
Bob Wilson7248f862009-11-22 04:24:42 +00001427
David Greenee32ebf22011-07-29 19:07:07 +00001428 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001429 }
1430 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1431 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001432 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001433 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001434 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001435 }
Bob Wilson7248f862009-11-22 04:24:42 +00001436
David Greeneaf8ee2c2011-07-29 22:43:06 +00001437 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001438 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001439
Nate Begemandbe3f772009-03-19 05:21:56 +00001440 // If the operator name is present, parse it.
Matthias Braun7cf3b112016-12-05 06:00:41 +00001441 StringInit *OperatorName = nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001442 if (Lex.getCode() == tgtok::colon) {
1443 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1444 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001445 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001446 }
Matthias Braun7cf3b112016-12-05 06:00:41 +00001447 OperatorName = StringInit::get(Lex.getCurStrVal());
Nate Begemandbe3f772009-03-19 05:21:56 +00001448 Lex.Lex(); // eat the VarName.
1449 }
Bob Wilson7248f862009-11-22 04:24:42 +00001450
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001451 SmallVector<std::pair<llvm::Init*, StringInit*>, 8> DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001452 if (Lex.getCode() != tgtok::r_paren) {
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001453 ParseDagArgList(DagArgs, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001454 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001455 }
Bob Wilson7248f862009-11-22 04:24:42 +00001456
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001457 if (Lex.getCode() != tgtok::r_paren) {
1458 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001459 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001460 }
1461 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001462
David Greenee32ebf22011-07-29 19:07:07 +00001463 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001464 }
Bob Wilson7248f862009-11-22 04:24:42 +00001465
David Greene2f7cf7f2011-01-07 17:05:37 +00001466 case tgtok::XHead:
1467 case tgtok::XTail:
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +00001468 case tgtok::XSize:
David Greene2f7cf7f2011-01-07 17:05:37 +00001469 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001470 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001471 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001472 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001473 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +00001474 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +00001475 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001476 case tgtok::XSRL:
1477 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001478 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001479 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001480 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001481 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001482 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001483 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001484 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001485 }
1486 }
Bob Wilson7248f862009-11-22 04:24:42 +00001487
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001488 return R;
1489}
1490
1491/// ParseValue - Parse a tblgen value. This returns null on error.
1492///
1493/// Value ::= SimpleValue ValueSuffix*
1494/// ValueSuffix ::= '{' BitList '}'
1495/// ValueSuffix ::= '[' BitList ']'
1496/// ValueSuffix ::= '.' ID
1497///
David Greened4263a62011-10-19 13:04:20 +00001498Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1499 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001500 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001501
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001502 // Parse the suffixes now if present.
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001503 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001504 switch (Lex.getCode()) {
1505 default: return Result;
1506 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001507 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001508 // This is the beginning of the object body.
1509 return Result;
1510
Chris Lattner526c8cb2009-06-21 03:39:35 +00001511 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001512 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001513 SmallVector<unsigned, 16> Ranges;
1514 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001515 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001516
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001517 // Reverse the bitlist.
1518 std::reverse(Ranges.begin(), Ranges.end());
1519 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001520 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001521 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001522 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001523 }
Bob Wilson7248f862009-11-22 04:24:42 +00001524
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001525 // Eat the '}'.
1526 if (Lex.getCode() != tgtok::r_brace) {
1527 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001528 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001529 }
1530 Lex.Lex();
1531 break;
1532 }
1533 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001534 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001535 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001536 SmallVector<unsigned, 16> Ranges;
1537 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001538 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001539
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001540 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001541 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001542 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001543 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001544 }
Bob Wilson7248f862009-11-22 04:24:42 +00001545
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001546 // Eat the ']'.
1547 if (Lex.getCode() != tgtok::r_square) {
1548 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001549 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001550 }
1551 Lex.Lex();
1552 break;
1553 }
Matthias Braun6a441832016-12-05 06:00:36 +00001554 case tgtok::period: {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001555 if (Lex.Lex() != tgtok::Id) { // eat the .
1556 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001557 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001558 }
Matthias Braun6a441832016-12-05 06:00:36 +00001559 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
1560 if (!Result->getFieldType(FieldName)) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001561 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001562 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001563 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001564 }
Matthias Braun6a441832016-12-05 06:00:36 +00001565 Result = FieldInit::get(Result, FieldName);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001566 Lex.Lex(); // eat field name
1567 break;
Matthias Braun6a441832016-12-05 06:00:36 +00001568 }
David Greene8e85b482011-10-19 13:04:43 +00001569
1570 case tgtok::paste:
1571 SMLoc PasteLoc = Lex.getLoc();
1572
1573 // Create a !strconcat() operation, first casting each operand to
1574 // a string if necessary.
1575
Sean Silvafb509ed2012-10-10 20:24:43 +00001576 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001577 if (!LHS) {
1578 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001579 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001580 }
Craig Toppera9642b42015-05-04 01:35:39 +00001581
David Greene8e85b482011-10-19 13:04:43 +00001582 if (LHS->getType() != StringRecTy::get()) {
1583 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1584 }
1585
Craig Topper011817a2014-04-09 04:50:04 +00001586 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001587
1588 Lex.Lex(); // Eat the '#'.
1589 switch (Lex.getCode()) {
1590 case tgtok::colon:
1591 case tgtok::semi:
1592 case tgtok::l_brace:
1593 // These are all of the tokens that can begin an object body.
1594 // Some of these can also begin values but we disallow those cases
1595 // because they are unlikely to be useful.
Craig Toppera9642b42015-05-04 01:35:39 +00001596
David Greene8e85b482011-10-19 13:04:43 +00001597 // Trailing paste, concat with an empty string.
1598 RHS = StringInit::get("");
1599 break;
1600
1601 default:
1602 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001603 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001604 if (!RHS) {
1605 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001606 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001607 }
1608
1609 if (RHS->getType() != StringRecTy::get()) {
1610 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1611 }
Craig Toppera9642b42015-05-04 01:35:39 +00001612
David Greene8e85b482011-10-19 13:04:43 +00001613 break;
1614 }
1615
1616 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1617 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1618 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001619 }
1620 }
1621}
1622
1623/// ParseDagArgList - Parse the argument list for a dag literal expression.
1624///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001625/// DagArg ::= Value (':' VARNAME)?
1626/// DagArg ::= VARNAME
1627/// DagArgList ::= DagArg
1628/// DagArgList ::= DagArgList ',' DagArg
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001629void TGParser::ParseDagArgList(
1630 SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
1631 Record *CurRec) {
Bob Wilson7248f862009-11-22 04:24:42 +00001632
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001633 while (true) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001634 // DagArg ::= VARNAME
1635 if (Lex.getCode() == tgtok::VarName) {
1636 // A missing value is treated like '?'.
Matthias Braunbb053162016-12-05 06:00:46 +00001637 StringInit *VarName = StringInit::get(Lex.getCurStrVal());
1638 Result.emplace_back(UnsetInit::get(), VarName);
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001639 Lex.Lex();
1640 } else {
1641 // DagArg ::= Value (':' VARNAME)?
1642 Init *Val = ParseValue(CurRec);
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001643 if (!Val) {
1644 Result.clear();
1645 return;
1646 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001647
1648 // If the variable name is present, add it.
Matthias Braunbb053162016-12-05 06:00:46 +00001649 StringInit *VarName = nullptr;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001650 if (Lex.getCode() == tgtok::colon) {
1651 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1652 TokError("expected variable name in dag literal");
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001653 Result.clear();
1654 return;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001655 }
Matthias Braunbb053162016-12-05 06:00:46 +00001656 VarName = StringInit::get(Lex.getCurStrVal());
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001657 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001658 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001659
1660 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001661 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001662 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001663 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001664 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001665}
1666
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001667/// ParseValueList - Parse a comma separated list of values, returning them as a
1668/// vector. Note that this always expects to be able to parse at least one
1669/// value. It returns an empty list if this is not possible.
1670///
1671/// ValueList ::= Value (',' Value)
1672///
Matthias Braunc66e7552016-12-05 06:41:54 +00001673void TGParser::ParseValueList(SmallVectorImpl<Init*> &Result, Record *CurRec,
1674 Record *ArgsRec, RecTy *EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001675 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001676 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001677 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001678 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001679 if (TArgs.empty()) {
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001680 TokError("template argument provided to non-template class");
Matthias Braunc66e7552016-12-05 06:41:54 +00001681 Result.clear();
1682 return;
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001683 }
David Greene8618f952009-06-08 20:23:18 +00001684 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001685 if (!RV) {
1686 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1687 << ")\n";
1688 }
David Greene8618f952009-06-08 20:23:18 +00001689 assert(RV && "Template argument record not found??");
1690 ItemType = RV->getType();
1691 ++ArgN;
1692 }
1693 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00001694 if (!Result.back()) {
1695 Result.clear();
1696 return;
1697 }
Bob Wilson7248f862009-11-22 04:24:42 +00001698
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001699 while (Lex.getCode() == tgtok::comma) {
1700 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001701
Craig Topper011817a2014-04-09 04:50:04 +00001702 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001703 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001704 if (ArgN >= TArgs.size()) {
1705 TokError("too many template arguments");
Matthias Braunc66e7552016-12-05 06:41:54 +00001706 Result.clear();
1707 return;
Bob Wilson7248f862009-11-22 04:24:42 +00001708 }
David Greene8618f952009-06-08 20:23:18 +00001709 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1710 assert(RV && "Template argument record not found??");
1711 ItemType = RV->getType();
1712 ++ArgN;
1713 }
1714 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00001715 if (!Result.back()) {
1716 Result.clear();
1717 return;
1718 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001719 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001720}
1721
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001722/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1723/// empty string on error. This can happen in a number of different context's,
1724/// including within a def or in the template args for a def (which which case
1725/// CurRec will be non-null) and within the template args for a multiclass (in
1726/// which case CurRec will be null, but CurMultiClass will be set). This can
1727/// also happen within a def that is within a multiclass, which will set both
1728/// CurRec and CurMultiClass.
1729///
1730/// Declaration ::= FIELD? Type ID ('=' Value)?
1731///
David Greenedb10e692011-10-19 13:02:42 +00001732Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001733 bool ParsingTemplateArgs) {
1734 // Read the field prefix if present.
1735 bool HasField = Lex.getCode() == tgtok::Field;
1736 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001737
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001738 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001739 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001740
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001741 if (Lex.getCode() != tgtok::Id) {
1742 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001743 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001744 }
Bob Wilson7248f862009-11-22 04:24:42 +00001745
Chris Lattner526c8cb2009-06-21 03:39:35 +00001746 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001747 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001748 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001749
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001750 if (ParsingTemplateArgs) {
Craig Toppera9642b42015-05-04 01:35:39 +00001751 if (CurRec)
David Greenedb10e692011-10-19 13:02:42 +00001752 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Craig Toppera9642b42015-05-04 01:35:39 +00001753 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001754 assert(CurMultiClass);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001755 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001756 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1757 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001758 }
Bob Wilson7248f862009-11-22 04:24:42 +00001759
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001760 // Add the value.
1761 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001762 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001763
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001764 // If a value is present, parse it.
1765 if (Lex.getCode() == tgtok::equal) {
1766 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001767 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001768 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001769 if (!Val ||
Craig Toppercfd81732016-01-04 03:15:08 +00001770 SetValue(CurRec, ValLoc, DeclName, None, Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001771 // Return the name, even if an error is thrown. This is so that we can
1772 // continue to make some progress, even without the value having been
1773 // initialized.
1774 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001775 }
Bob Wilson7248f862009-11-22 04:24:42 +00001776
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001777 return DeclName;
1778}
1779
David Greenefb927af2012-02-22 16:09:41 +00001780/// ParseForeachDeclaration - Read a foreach declaration, returning
1781/// the name of the declared object or a NULL Init on error. Return
1782/// the name of the parsed initializer list through ForeachListName.
1783///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001784/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1785/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1786/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001787///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001788VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001789 if (Lex.getCode() != tgtok::Id) {
1790 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001791 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001792 }
1793
1794 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1795 Lex.Lex();
1796
1797 // If a value is present, parse it.
1798 if (Lex.getCode() != tgtok::equal) {
1799 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001800 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001801 }
1802 Lex.Lex(); // Eat the '='
1803
Craig Topper011817a2014-04-09 04:50:04 +00001804 RecTy *IterType = nullptr;
Matthias Braunc66e7552016-12-05 06:41:54 +00001805 SmallVector<unsigned, 16> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001806
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001807 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001808 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001809 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001810 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001811 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001812 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001813 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001814 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001815 }
1816 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001817 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001818 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001819 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001820 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001821 }
1822 IterType = ListType->getElementType();
1823 break;
David Greenefb927af2012-02-22 16:09:41 +00001824 }
1825
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001826 case tgtok::IntVal: { // RangePiece.
1827 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001828 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001829 break;
David Greenefb927af2012-02-22 16:09:41 +00001830 }
1831
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001832 case tgtok::l_brace: { // '{' RangeList '}'
1833 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001834 ParseRangeList(Ranges);
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001835 if (Lex.getCode() != tgtok::r_brace) {
1836 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001837 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001838 }
1839 Lex.Lex();
1840 break;
1841 }
1842 }
David Greenefb927af2012-02-22 16:09:41 +00001843
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001844 if (!Ranges.empty()) {
1845 assert(!IterType && "Type already initialized?");
1846 IterType = IntRecTy::get();
1847 std::vector<Init*> Values;
Craig Topper8eb764c2015-06-02 06:19:28 +00001848 for (unsigned R : Ranges)
1849 Values.push_back(IntInit::get(R));
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001850 ForeachListValue = ListInit::get(Values, IterType);
1851 }
1852
1853 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001854 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001855
1856 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001857}
1858
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001859/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1860/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1861/// template args for a def, which may or may not be in a multiclass. If null,
1862/// these are the template args for a multiclass.
1863///
1864/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001865///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001866bool TGParser::ParseTemplateArgList(Record *CurRec) {
1867 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1868 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001869
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001870 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001871
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001872 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001873 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001874 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001875 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001876
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001877 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001878
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001879 while (Lex.getCode() == tgtok::comma) {
1880 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001881
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001882 // Read the following declarations.
1883 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001884 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001885 return true;
1886 TheRecToAddTo->addTemplateArg(TemplArg);
1887 }
Bob Wilson7248f862009-11-22 04:24:42 +00001888
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001889 if (Lex.getCode() != tgtok::greater)
1890 return TokError("expected '>' at end of template argument list");
1891 Lex.Lex(); // eat the '>'.
1892 return false;
1893}
1894
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001895/// ParseBodyItem - Parse a single item at within the body of a def or class.
1896///
1897/// BodyItem ::= Declaration ';'
1898/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1899bool TGParser::ParseBodyItem(Record *CurRec) {
1900 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001901 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001902 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001903
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001904 if (Lex.getCode() != tgtok::semi)
1905 return TokError("expected ';' after declaration");
1906 Lex.Lex();
1907 return false;
1908 }
1909
1910 // LET ID OptionalRangeList '=' Value ';'
1911 if (Lex.Lex() != tgtok::Id)
1912 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001913
Chris Lattner526c8cb2009-06-21 03:39:35 +00001914 SMLoc IdLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00001915 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001916 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001917
Matthias Braunc66e7552016-12-05 06:41:54 +00001918 SmallVector<unsigned, 16> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001919 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001920 return true;
1921 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001922
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001923 if (Lex.getCode() != tgtok::equal)
1924 return TokError("expected '=' in let expression");
1925 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001926
David Greene8618f952009-06-08 20:23:18 +00001927 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001928 if (!Field)
Matthias Braun215ff842016-12-05 07:35:13 +00001929 return TokError("Value '" + FieldName->getValue() + "' unknown!");
David Greene8618f952009-06-08 20:23:18 +00001930
1931 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001932
David Greeneaf8ee2c2011-07-29 22:43:06 +00001933 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001934 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001935
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001936 if (Lex.getCode() != tgtok::semi)
1937 return TokError("expected ';' after let expression");
1938 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001939
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001940 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1941}
1942
1943/// ParseBody - Read the body of a class or def. Return true on error, false on
1944/// success.
1945///
1946/// Body ::= ';'
1947/// Body ::= '{' BodyList '}'
1948/// BodyList BodyItem*
1949///
1950bool TGParser::ParseBody(Record *CurRec) {
1951 // If this is a null definition, just eat the semi and return.
1952 if (Lex.getCode() == tgtok::semi) {
1953 Lex.Lex();
1954 return false;
1955 }
Bob Wilson7248f862009-11-22 04:24:42 +00001956
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001957 if (Lex.getCode() != tgtok::l_brace)
1958 return TokError("Expected ';' or '{' to start body");
1959 // Eat the '{'.
1960 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001961
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001962 while (Lex.getCode() != tgtok::r_brace)
1963 if (ParseBodyItem(CurRec))
1964 return true;
1965
1966 // Eat the '}'.
1967 Lex.Lex();
1968 return false;
1969}
1970
Sean Silvacb1a75e2013-01-09 04:49:14 +00001971/// \brief Apply the current let bindings to \a CurRec.
1972/// \returns true on error, false otherwise.
1973bool TGParser::ApplyLetStack(Record *CurRec) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001974 for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
Craig Topper8eb764c2015-06-02 06:19:28 +00001975 for (LetRecord &LR : LetInfo)
1976 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
Sean Silvacb1a75e2013-01-09 04:49:14 +00001977 return true;
1978 return false;
1979}
1980
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001981/// ParseObjectBody - Parse the body of a def or class. This consists of an
1982/// optional ClassList followed by a Body. CurRec is the current def or class
1983/// that is being parsed.
1984///
1985/// ObjectBody ::= BaseClassList Body
1986/// BaseClassList ::= /*empty*/
1987/// BaseClassList ::= ':' BaseClassListNE
1988/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1989///
1990bool TGParser::ParseObjectBody(Record *CurRec) {
1991 // If there is a baseclass list, read it.
1992 if (Lex.getCode() == tgtok::colon) {
1993 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001994
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001995 // Read all of the subclasses.
1996 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001997 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001998 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00001999 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002000
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002001 // Add it.
2002 if (AddSubClass(CurRec, SubClass))
2003 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002004
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002005 if (Lex.getCode() != tgtok::comma) break;
2006 Lex.Lex(); // eat ','.
2007 SubClass = ParseSubClassReference(CurRec, false);
2008 }
2009 }
2010
Sean Silvacb1a75e2013-01-09 04:49:14 +00002011 if (ApplyLetStack(CurRec))
2012 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002013
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002014 return ParseBody(CurRec);
2015}
2016
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002017/// ParseDef - Parse and return a top level or multiclass def, return the record
2018/// corresponding to it. This returns null on error.
2019///
2020/// DefInst ::= DEF ObjectName ObjectBody
2021///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002022bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00002023 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002024 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00002025 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002026
2027 // Parse ObjectName and make a record for it.
Craig Topper84138712014-11-29 05:31:10 +00002028 std::unique_ptr<Record> CurRecOwner;
Jordan Roseabdd99b2013-01-10 18:50:05 +00002029 Init *Name = ParseObjectName(CurMultiClass);
2030 if (Name)
Craig Topper84138712014-11-29 05:31:10 +00002031 CurRecOwner = make_unique<Record>(Name, DefLoc, Records);
Jordan Roseabdd99b2013-01-10 18:50:05 +00002032 else
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00002033 CurRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), DefLoc,
2034 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00002035 Record *CurRec = CurRecOwner.get(); // Keep a copy since we may release.
Bob Wilson7248f862009-11-22 04:24:42 +00002036
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002037 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002038 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002039
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002040 // Ensure redefinition doesn't happen.
Craig Topper84138712014-11-29 05:31:10 +00002041 if (Records.getDef(CurRec->getNameInitAsString()))
2042 return Error(DefLoc, "def '" + CurRec->getNameInitAsString()+
2043 "' already defined");
Craig Toppercdab2322014-11-29 05:52:51 +00002044 Records.addDef(std::move(CurRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00002045
2046 if (ParseObjectBody(CurRec))
2047 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002048 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002049 // Parse the body before adding this prototype to the DefPrototypes vector.
2050 // That way implicit definitions will be added to the DefPrototypes vector
2051 // before this object, instantiated prior to defs derived from this object,
2052 // and this available for indirect name resolution when defs derived from
2053 // this object are instantiated.
Craig Topper84138712014-11-29 05:31:10 +00002054 if (ParseObjectBody(CurRec))
Hal Finkela8c1f462014-01-02 20:47:09 +00002055 return true;
2056
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002057 // Otherwise, a def inside a multiclass, add it to the multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002058 for (const auto &Proto : CurMultiClass->DefPrototypes)
2059 if (Proto->getNameInit() == CurRec->getNameInit())
Craig Topper84138712014-11-29 05:31:10 +00002060 return Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
2061 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002062 CurMultiClass->DefPrototypes.push_back(std::move(CurRecOwner));
Anton Yartsev671dff12014-08-08 00:29:54 +00002063 } else if (ParseObjectBody(CurRec)) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002064 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002065 }
Bob Wilson7248f862009-11-22 04:24:42 +00002066
Craig Topper011817a2014-04-09 04:50:04 +00002067 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002068 // See Record::setName(). This resolve step will see any new name
2069 // for the def that might have been created when resolving
2070 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002071 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002072
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002073 // If ObjectBody has template arguments, it's an error.
2074 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002075
2076 if (CurMultiClass) {
2077 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002078 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
2079 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002080 assert(RV && "Template arg doesn't exist?");
2081 CurRec->addValue(*RV);
2082 }
2083 }
2084
Craig Toppera9642b42015-05-04 01:35:39 +00002085 if (ProcessForeachDefs(CurRec, DefLoc))
Craig Topper84138712014-11-29 05:31:10 +00002086 return Error(DefLoc, "Could not process loops for def" +
2087 CurRec->getNameInitAsString());
David Greenefb927af2012-02-22 16:09:41 +00002088
2089 return false;
2090}
2091
2092/// ParseForeach - Parse a for statement. Return the record corresponding
2093/// to it. This returns true on error.
2094///
2095/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2096/// Foreach ::= FOREACH Declaration IN Object
2097///
2098bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2099 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2100 Lex.Lex(); // Eat the 'for' token.
2101
2102 // Make a temporary object to record items associated with the for
2103 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002104 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002105 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002106 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002107 return TokError("expected declaration in for");
2108
2109 if (Lex.getCode() != tgtok::In)
2110 return TokError("Unknown tok");
2111 Lex.Lex(); // Eat the in
2112
2113 // Create a loop object and remember it.
2114 Loops.push_back(ForeachLoop(IterName, ListValue));
2115
2116 if (Lex.getCode() != tgtok::l_brace) {
2117 // FOREACH Declaration IN Object
2118 if (ParseObject(CurMultiClass))
2119 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002120 } else {
David Greenefb927af2012-02-22 16:09:41 +00002121 SMLoc BraceLoc = Lex.getLoc();
2122 // Otherwise, this is a group foreach.
2123 Lex.Lex(); // eat the '{'.
2124
2125 // Parse the object list.
2126 if (ParseObjectList(CurMultiClass))
2127 return true;
2128
2129 if (Lex.getCode() != tgtok::r_brace) {
2130 TokError("expected '}' at end of foreach command");
2131 return Error(BraceLoc, "to match this '{'");
2132 }
2133 Lex.Lex(); // Eat the }
2134 }
2135
2136 // We've processed everything in this loop.
2137 Loops.pop_back();
2138
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002139 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002140}
2141
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002142/// ParseClass - Parse a tblgen class definition.
2143///
2144/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2145///
2146bool TGParser::ParseClass() {
2147 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2148 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002149
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002150 if (Lex.getCode() != tgtok::Id)
2151 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002152
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002153 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2154 if (CurRec) {
2155 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002156 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002157 !CurRec->getSuperClasses().empty() ||
2158 !CurRec->getTemplateArgs().empty())
Craig Topper85c07002015-04-30 05:54:22 +00002159 return TokError("Class '" + CurRec->getNameInitAsString() +
2160 "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002161 } else {
2162 // If this is the first reference to this class, create and add it.
Hans Wennborgffbbd532014-11-30 00:31:49 +00002163 auto NewRec =
2164 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
Craig Toppercdab2322014-11-29 05:52:51 +00002165 CurRec = NewRec.get();
2166 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002167 }
2168 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002169
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002170 // If there are template args, parse them.
2171 if (Lex.getCode() == tgtok::less)
2172 if (ParseTemplateArgList(CurRec))
2173 return true;
2174
2175 // Finally, parse the object body.
2176 return ParseObjectBody(CurRec);
2177}
2178
2179/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2180/// of LetRecords.
2181///
2182/// LetList ::= LetItem (',' LetItem)*
2183/// LetItem ::= ID OptionalRangeList '=' Value
2184///
Matthias Braunc66e7552016-12-05 06:41:54 +00002185void TGParser::ParseLetList(SmallVectorImpl<LetRecord> &Result) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002186 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002187 if (Lex.getCode() != tgtok::Id) {
2188 TokError("expected identifier in let definition");
Matthias Braunc66e7552016-12-05 06:41:54 +00002189 Result.clear();
2190 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002191 }
Matthias Braunc66e7552016-12-05 06:41:54 +00002192
Matthias Braun215ff842016-12-05 07:35:13 +00002193 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattner526c8cb2009-06-21 03:39:35 +00002194 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002195 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002196
2197 // Check for an optional RangeList.
Matthias Braunc66e7552016-12-05 06:41:54 +00002198 SmallVector<unsigned, 16> Bits;
2199 if (ParseOptionalRangeList(Bits)) {
2200 Result.clear();
2201 return;
2202 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002203 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002204
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002205 if (Lex.getCode() != tgtok::equal) {
2206 TokError("expected '=' in let expression");
Matthias Braunc66e7552016-12-05 06:41:54 +00002207 Result.clear();
2208 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002209 }
2210 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002211
Craig Topper011817a2014-04-09 04:50:04 +00002212 Init *Val = ParseValue(nullptr);
Matthias Braunc66e7552016-12-05 06:41:54 +00002213 if (!Val) {
2214 Result.clear();
2215 return;
2216 }
Bob Wilson7248f862009-11-22 04:24:42 +00002217
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002218 // Now that we have everything, add the record.
Matthias Braun215ff842016-12-05 07:35:13 +00002219 Result.emplace_back(Name, Bits, Val, NameLoc);
Bob Wilson7248f862009-11-22 04:24:42 +00002220
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002221 if (Lex.getCode() != tgtok::comma)
Matthias Braunc66e7552016-12-05 06:41:54 +00002222 return;
Bob Wilson7248f862009-11-22 04:24:42 +00002223 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002224 }
2225}
2226
2227/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002228/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002229///
2230/// Object ::= LET LetList IN '{' ObjectList '}'
2231/// Object ::= LET LetList IN Object
2232///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002233bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002234 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2235 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002236
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002237 // Add this entry to the let stack.
Matthias Braunc66e7552016-12-05 06:41:54 +00002238 SmallVector<LetRecord, 8> LetInfo;
2239 ParseLetList(LetInfo);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002240 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002241 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002242
2243 if (Lex.getCode() != tgtok::In)
2244 return TokError("expected 'in' at end of top-level 'let'");
2245 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002246
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002247 // If this is a scalar let, just handle it now
2248 if (Lex.getCode() != tgtok::l_brace) {
2249 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002250 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002251 return true;
2252 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002253 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002254 // Otherwise, this is a group let.
2255 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002256
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002257 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002258 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002259 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002260
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002261 if (Lex.getCode() != tgtok::r_brace) {
2262 TokError("expected '}' at end of top level let command");
2263 return Error(BraceLoc, "to match this '{'");
2264 }
2265 Lex.Lex();
2266 }
Bob Wilson7248f862009-11-22 04:24:42 +00002267
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002268 // Outside this let scope, this let block is not active.
2269 LetStack.pop_back();
2270 return false;
2271}
2272
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002273/// ParseMultiClass - Parse a multiclass definition.
2274///
Bob Wilson3d948162009-04-28 19:41:44 +00002275/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002276/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2277/// MultiClassObject ::= DefInst
2278/// MultiClassObject ::= MultiClassInst
2279/// MultiClassObject ::= DefMInst
2280/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2281/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002282///
2283bool TGParser::ParseMultiClass() {
2284 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2285 Lex.Lex(); // Eat the multiclass token.
2286
2287 if (Lex.getCode() != tgtok::Id)
2288 return TokError("expected identifier after multiclass for name");
2289 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002290
Craig Topper7adf2bf2014-12-11 05:25:30 +00002291 auto Result =
2292 MultiClasses.insert(std::make_pair(Name,
2293 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2294
2295 if (!Result.second)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002296 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002297
Craig Topper7adf2bf2014-12-11 05:25:30 +00002298 CurMultiClass = Result.first->second.get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002299 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002300
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002301 // If there are template args, parse them.
2302 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002303 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002304 return true;
2305
David Greene7049e792009-04-24 16:55:41 +00002306 bool inherits = false;
2307
David Greene753ed8f2009-04-22 16:42:54 +00002308 // If there are submulticlasses, parse them.
2309 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002310 inherits = true;
2311
David Greene753ed8f2009-04-22 16:42:54 +00002312 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002313
David Greene753ed8f2009-04-22 16:42:54 +00002314 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002315 SubMultiClassReference SubMultiClass =
2316 ParseSubMultiClassReference(CurMultiClass);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002317 while (true) {
David Greene753ed8f2009-04-22 16:42:54 +00002318 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002319 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002320
David Greene753ed8f2009-04-22 16:42:54 +00002321 // Add it.
2322 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2323 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002324
David Greene753ed8f2009-04-22 16:42:54 +00002325 if (Lex.getCode() != tgtok::comma) break;
2326 Lex.Lex(); // eat ','.
2327 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2328 }
2329 }
2330
David Greene7049e792009-04-24 16:55:41 +00002331 if (Lex.getCode() != tgtok::l_brace) {
2332 if (!inherits)
2333 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002334 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002335 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002336 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002337 } else {
David Greene7049e792009-04-24 16:55:41 +00002338 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2339 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002340
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002341 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002342 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002343 default:
2344 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
2345 case tgtok::Let:
2346 case tgtok::Def:
2347 case tgtok::Defm:
2348 case tgtok::Foreach:
2349 if (ParseObject(CurMultiClass))
2350 return true;
2351 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002352 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002353 }
David Greene7049e792009-04-24 16:55:41 +00002354 Lex.Lex(); // eat the '}'.
2355 }
Bob Wilson7248f862009-11-22 04:24:42 +00002356
Craig Topper011817a2014-04-09 04:50:04 +00002357 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002358 return false;
2359}
2360
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002361Record *TGParser::InstantiateMulticlassDef(MultiClass &MC, Record *DefProto,
2362 Init *&DefmPrefix,
2363 SMRange DefmPrefixRange,
2364 ArrayRef<Init *> TArgs,
Matthias Braunc66e7552016-12-05 06:41:54 +00002365 ArrayRef<Init *> TemplateVals) {
David Greene5d5d88c2011-10-19 13:04:31 +00002366 // We need to preserve DefProto so it can be reused for later
2367 // instantiations, so create a new Record to inherit from it.
2368
David Greenedb445972011-10-05 22:42:07 +00002369 // Add in the defm name. If the defm prefix is empty, give each
2370 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2371 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2372 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002373
Jordan Roseabdd99b2013-01-10 18:50:05 +00002374 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002375 if (!DefmPrefix) {
Craig Topperf4412262017-06-01 06:56:16 +00002376 DefmPrefix = GetNewAnonymousName();
Jordan Roseabdd99b2013-01-10 18:50:05 +00002377 IsAnonymous = true;
2378 }
David Greene5d5d88c2011-10-19 13:04:31 +00002379
2380 Init *DefName = DefProto->getNameInit();
Sean Silvafb509ed2012-10-10 20:24:43 +00002381 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002382
Craig Topper011817a2014-04-09 04:50:04 +00002383 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002384 // We have a fully expanded string so there are no operators to
2385 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002386 DefName =
2387 BinOpInit::get(BinOpInit::STRCONCAT,
2388 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2389 StringRecTy::get())->Fold(DefProto, &MC),
2390 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2391 }
David Greene5d5d88c2011-10-19 13:04:31 +00002392
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002393 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002394 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002395 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Craig Topper84138712014-11-29 05:31:10 +00002396 auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002397
2398 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002399 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002400 Ref.Rec = DefProto;
Craig Topper84138712014-11-29 05:31:10 +00002401 AddSubClass(CurRec.get(), Ref);
David Greenedb445972011-10-05 22:42:07 +00002402
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002403 // Set the value for NAME. We don't resolve references to it 'til later,
2404 // though, so that uses in nested multiclass names don't get
2405 // confused.
Matthias Braun215ff842016-12-05 07:35:13 +00002406 if (SetValue(CurRec.get(), Ref.RefRange.Start, StringInit::get("NAME"), None,
2407 DefmPrefix, /*AllowSelfAssignment*/true)) {
Craig Topper85c07002015-04-30 05:54:22 +00002408 Error(DefmPrefixRange.Start, "Could not resolve " +
2409 CurRec->getNameInitAsString() + ":NAME to '" +
2410 DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002411 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002412 }
David Greene8bf0d722011-10-19 13:04:35 +00002413
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002414 // If the DefNameString didn't resolve, we probably have a reference to
2415 // NAME and need to replace it. We need to do at least this much greedily,
2416 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002417 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002418 RecordVal *DefNameRV = CurRec->getValue("NAME");
2419 CurRec->resolveReferencesTo(DefNameRV);
2420 }
2421
2422 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002423 // Now that we're at the top level, resolve all NAME references
2424 // in the resultant defs that weren't in the def names themselves.
2425 RecordVal *DefNameRV = CurRec->getValue("NAME");
2426 CurRec->resolveReferencesTo(DefNameRV);
2427
Hal Finkeld2497362015-05-21 04:32:56 +00002428 // Check if the name is a complex pattern.
2429 // If so, resolve it.
2430 DefName = CurRec->getNameInit();
2431 DefNameString = dyn_cast<StringInit>(DefName);
2432
2433 // OK the pattern is more complex than simply using NAME.
2434 // Let's use the heavy weaponery.
2435 if (!DefNameString) {
2436 ResolveMulticlassDefArgs(MC, CurRec.get(), DefmPrefixRange.Start,
2437 Lex.getLoc(), TArgs, TemplateVals,
2438 false/*Delete args*/);
2439 DefName = CurRec->getNameInit();
2440 DefNameString = dyn_cast<StringInit>(DefName);
2441
2442 if (!DefNameString)
2443 DefName = DefName->convertInitializerTo(StringRecTy::get());
2444
2445 // We ran out of options here...
2446 DefNameString = dyn_cast<StringInit>(DefName);
2447 if (!DefNameString) {
2448 PrintFatalError(CurRec->getLoc()[CurRec->getLoc().size() - 1],
2449 DefName->getAsUnquotedString() + " is not a string.");
2450 return nullptr;
2451 }
2452
2453 CurRec->setName(DefName);
2454 }
2455
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002456 // Now that NAME references are resolved and we're at the top level of
2457 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002458 // currently in a multiclass, it means this defm appears inside a
2459 // multiclass and its name won't be fully resolvable until we see
Hal Finkeld2497362015-05-21 04:32:56 +00002460 // the top-level defm. Therefore, we don't add this to the
2461 // RecordKeeper at this point. If we did we could get duplicate
David Greene8bf0d722011-10-19 13:04:35 +00002462 // defs as more than one probably refers to NAME or some other
2463 // common internal placeholder.
2464
2465 // Ensure redefinition doesn't happen.
2466 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002467 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002468 "' already defined, instantiating defm with subdef '" +
2469 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002470 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002471 }
2472
Craig Topper84138712014-11-29 05:31:10 +00002473 Record *CurRecSave = CurRec.get(); // Keep a copy before we release.
Craig Toppercdab2322014-11-29 05:52:51 +00002474 Records.addDef(std::move(CurRec));
Craig Topper84138712014-11-29 05:31:10 +00002475 return CurRecSave;
David Greene8bf0d722011-10-19 13:04:35 +00002476 }
2477
Craig Topper84138712014-11-29 05:31:10 +00002478 // FIXME This is bad but the ownership transfer to caller is pretty messy.
2479 // The unique_ptr in this function at least protects the exits above.
2480 return CurRec.release();
David Greenedb445972011-10-05 22:42:07 +00002481}
2482
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002483bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC, Record *CurRec,
2484 SMLoc DefmPrefixLoc, SMLoc SubClassLoc,
2485 ArrayRef<Init *> TArgs,
Matthias Braunc66e7552016-12-05 06:41:54 +00002486 ArrayRef<Init *> TemplateVals,
David Greenedb445972011-10-05 22:42:07 +00002487 bool DeleteArgs) {
2488 // Loop over all of the template arguments, setting them to the specified
2489 // value or leaving them as the default if necessary.
2490 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2491 // Check if a value is specified for this temp-arg.
2492 if (i < TemplateVals.size()) {
2493 // Set it now.
Craig Toppercfd81732016-01-04 03:15:08 +00002494 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], None, TemplateVals[i]))
David Greenedb445972011-10-05 22:42:07 +00002495 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002496
David Greenedb445972011-10-05 22:42:07 +00002497 // Resolve it next.
2498 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2499
2500 if (DeleteArgs)
2501 // Now remove it.
2502 CurRec->removeValue(TArgs[i]);
Craig Toppera9642b42015-05-04 01:35:39 +00002503
David Greenedb445972011-10-05 22:42:07 +00002504 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Craig Topper85c07002015-04-30 05:54:22 +00002505 return Error(SubClassLoc, "value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00002506 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +00002507 ") of multiclassclass '" + MC.Rec.getNameInitAsString() +
2508 "'");
David Greenedb445972011-10-05 22:42:07 +00002509 }
2510 }
2511 return false;
2512}
2513
2514bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2515 Record *CurRec,
2516 Record *DefProto,
2517 SMLoc DefmPrefixLoc) {
2518 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002519 if (ApplyLetStack(CurRec))
2520 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002521
David Greenedb445972011-10-05 22:42:07 +00002522 // Don't create a top level definition for defm inside multiclasses,
2523 // instead, only update the prototypes and bind the template args
2524 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002525 if (!CurMultiClass)
2526 return false;
Craig Toppereb4d7c62015-04-29 04:43:36 +00002527 for (const auto &Proto : CurMultiClass->DefPrototypes)
2528 if (Proto->getNameInit() == CurRec->getNameInit())
Sean Silvacc951b22013-01-09 05:28:12 +00002529 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2530 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002531 CurMultiClass->DefPrototypes.push_back(std::unique_ptr<Record>(CurRec));
David Greenedb445972011-10-05 22:42:07 +00002532
Sean Silvacc951b22013-01-09 05:28:12 +00002533 // Copy the template arguments for the multiclass into the new def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002534 for (Init * TA : CurMultiClass->Rec.getTemplateArgs()) {
2535 const RecordVal *RV = CurMultiClass->Rec.getValue(TA);
Sean Silvacc951b22013-01-09 05:28:12 +00002536 assert(RV && "Template arg doesn't exist?");
2537 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002538 }
2539
2540 return false;
2541}
2542
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002543/// ParseDefm - Parse the instantiation of a multiclass.
2544///
2545/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2546///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002547bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002548 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002549 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002550 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002551
Craig Topperb21afc62013-01-07 05:09:33 +00002552 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002553 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002554 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002555
Jordan Rosef12e8a92013-01-10 18:50:11 +00002556 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002557 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002558 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002559
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002560 // Keep track of the new generated record definitions.
2561 std::vector<Record*> NewRecDefs;
2562
2563 // This record also inherits from a regular class (non-multiclass)?
2564 bool InheritFromClass = false;
2565
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002566 // eat the colon.
2567 Lex.Lex();
2568
Chris Lattner526c8cb2009-06-21 03:39:35 +00002569 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002570 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002571
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002572 while (true) {
Craig Topper011817a2014-04-09 04:50:04 +00002573 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002574
2575 // To instantiate a multiclass, we need to first get the multiclass, then
2576 // instantiate each def contained in the multiclass with the SubClassRef
2577 // template parameters.
Craig Topper7adf2bf2014-12-11 05:25:30 +00002578 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper4ca40012014-11-30 01:20:17 +00002579 assert(MC && "Didn't lookup multiclass correctly?");
Matthias Braunc66e7552016-12-05 06:41:54 +00002580 ArrayRef<Init*> TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002581
2582 // Verify that the correct number of template arguments were specified.
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002583 ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002584 if (TArgs.size() < TemplateVals.size())
2585 return Error(SubClassLoc,
2586 "more template args specified than multiclass expects");
2587
2588 // Loop over all the def's in the multiclass, instantiating each one.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002589 for (const std::unique_ptr<Record> &DefProto : MC->DefPrototypes) {
Hal Finkeld2497362015-05-21 04:32:56 +00002590 // The record name construction goes as follow:
2591 // - If the def name is a string, prepend the prefix.
2592 // - If the def name is a more complex pattern, use that pattern.
Craig Topper7f36be92016-02-26 06:50:27 +00002593 // As a result, the record is instantiated before resolving
Hal Finkeld2497362015-05-21 04:32:56 +00002594 // arguments, as it would make its name a string.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002595 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto.get(), DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002596 SMRange(DefmLoc,
Hal Finkeld2497362015-05-21 04:32:56 +00002597 DefmPrefixEndLoc),
2598 TArgs, TemplateVals);
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002599 if (!CurRec)
2600 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002601
Craig Topper7f36be92016-02-26 06:50:27 +00002602 // Now that the record is instantiated, we can resolve arguments.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002603 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002604 TArgs, TemplateVals, true/*Delete args*/))
2605 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002606
Craig Toppereb4d7c62015-04-29 04:43:36 +00002607 if (ResolveMulticlassDef(*MC, CurRec, DefProto.get(), DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002608 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002609
Adam Nemete5a07162014-09-16 17:14:13 +00002610 // Defs that can be used by other definitions should be fully resolved
2611 // before any use.
2612 if (DefProto->isResolveFirst() && !CurMultiClass) {
2613 CurRec->resolveReferences();
2614 CurRec->setResolveFirst(false);
2615 }
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002616 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002617 }
2618
David Greenedb445972011-10-05 22:42:07 +00002619
David Greenef00919a2009-04-22 22:17:51 +00002620 if (Lex.getCode() != tgtok::comma) break;
2621 Lex.Lex(); // eat ','.
2622
Craig Topper998a39a2013-08-20 04:22:09 +00002623 if (Lex.getCode() != tgtok::Id)
2624 return TokError("expected identifier");
2625
David Greenef00919a2009-04-22 22:17:51 +00002626 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002627
2628 // A defm can inherit from regular classes (non-multiclass) as
2629 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002630 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002631
2632 if (InheritFromClass)
2633 break;
2634
Craig Topper011817a2014-04-09 04:50:04 +00002635 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002636 }
2637
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002638 if (InheritFromClass) {
2639 // Process all the classes to inherit as if they were part of a
2640 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002641 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002642 while (true) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002643 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002644 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002645
2646 // Get the expanded definition prototypes and teach them about
2647 // the record values the current class to inherit has
Craig Toppereb4d7c62015-04-29 04:43:36 +00002648 for (Record *CurRec : NewRecDefs) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002649 // Add it.
2650 if (AddSubClass(CurRec, SubClass))
2651 return true;
2652
Sean Silvacb1a75e2013-01-09 04:49:14 +00002653 if (ApplyLetStack(CurRec))
2654 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002655 }
2656
2657 if (Lex.getCode() != tgtok::comma) break;
2658 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002659 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002660 }
2661 }
2662
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002663 if (!CurMultiClass)
Craig Toppereb4d7c62015-04-29 04:43:36 +00002664 for (Record *CurRec : NewRecDefs)
David Greene50c09122011-08-10 18:27:46 +00002665 // See Record::setName(). This resolve step will see any new
2666 // name for the def that might have been created when resolving
2667 // inheritance, values and arguments above.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002668 CurRec->resolveReferences();
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002669
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002670 if (Lex.getCode() != tgtok::semi)
2671 return TokError("expected ';' at end of defm");
2672 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002673
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002674 return false;
2675}
2676
2677/// ParseObject
2678/// Object ::= ClassInst
2679/// Object ::= DefInst
2680/// Object ::= MultiClassInst
2681/// Object ::= DefMInst
2682/// Object ::= LETCommand '{' ObjectList '}'
2683/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002684bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002685 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002686 default:
2687 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002688 case tgtok::Let: return ParseTopLevelLet(MC);
2689 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002690 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002691 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002692 case tgtok::Class: return ParseClass();
2693 case tgtok::MultiClass: return ParseMultiClass();
2694 }
2695}
2696
2697/// ParseObjectList
2698/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002699bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002700 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002701 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002702 return true;
2703 }
2704 return false;
2705}
2706
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002707bool TGParser::ParseFile() {
2708 Lex.Lex(); // Prime the lexer.
2709 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002710
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002711 // If we have unread input at the end of the file, report it.
2712 if (Lex.getCode() == tgtok::Eof)
2713 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002714
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002715 return TokError("Unexpected input at top level");
2716}