blob: 72ab8d3838d43239de8ecce51fe302224a0e7f29 [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 }
Bob Wilson7248f862009-11-22 04:24:42 +0000208
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000209 if (CurRec->isSubClassOf(SC))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000210 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000211 "Already subclass of '" + SC->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000212 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) {
Craig Topper011817a2014-04-09 04:50:04 +0000323 Init *ItemVal = List->resolveListElementReference(*CurRec, nullptr, i);
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) {
Matthias Braun215ff842016-12-05 07:35:13 +0000743 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name, "::");
David Greenedb10e692011-10-19 13:02:42 +0000744
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000745 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
746 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
747 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000748 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000749 }
750 }
Bob Wilson7248f862009-11-22 04:24:42 +0000751
David Greenefb927af2012-02-22 16:09:41 +0000752 // If this is in a foreach loop, make sure it's not a loop iterator
Craig Toppereb4d7c62015-04-29 04:43:36 +0000753 for (const auto &L : Loops) {
754 VarInit *IterVar = dyn_cast<VarInit>(L.IterVar);
Matthias Braun215ff842016-12-05 07:35:13 +0000755 if (IterVar && IterVar->getNameInit() == Name)
David Greenefb927af2012-02-22 16:09:41 +0000756 return IterVar;
757 }
758
David Greene232bd602011-10-19 13:04:21 +0000759 if (Mode == ParseNameMode)
Matthias Braun215ff842016-12-05 07:35:13 +0000760 return Name;
David Greene232bd602011-10-19 13:04:21 +0000761
Matthias Braun215ff842016-12-05 07:35:13 +0000762 if (Record *D = Records.getDef(Name->getValue()))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000763 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000764
David Greene232bd602011-10-19 13:04:21 +0000765 if (Mode == ParseValueMode) {
Matthias Braun215ff842016-12-05 07:35:13 +0000766 Error(NameLoc, "Variable not defined: '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000767 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000768 }
Craig Toppera9642b42015-05-04 01:35:39 +0000769
Matthias Braun215ff842016-12-05 07:35:13 +0000770 return Name;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000771}
772
David Greene5d0c0512009-05-14 20:54:48 +0000773/// ParseOperation - Parse an operator. This returns null on error.
774///
775/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
776///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000777Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000778 switch (Lex.getCode()) {
779 default:
780 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000781 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000782 case tgtok::XHead:
783 case tgtok::XTail:
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000784 case tgtok::XSize:
David Greene2f7cf7f2011-01-07 17:05:37 +0000785 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000786 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
787 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000788 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000789
David Greenee8f3b272009-05-14 21:22:49 +0000790 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000791 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000792 case tgtok::XCast:
793 Lex.Lex(); // eat the operation
794 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000795
David Greenee8f3b272009-05-14 21:22:49 +0000796 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000797
Craig Topper011817a2014-04-09 04:50:04 +0000798 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000799 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000800 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000801 }
David Greene5d0c0512009-05-14 20:54:48 +0000802
David Greenee8f3b272009-05-14 21:22:49 +0000803 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000804 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000805 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000806 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000807 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000808 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000809 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000810 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000811 break;
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000812 case tgtok::XSize:
813 Lex.Lex();
814 Code = UnOpInit::SIZE;
815 Type = IntRecTy::get();
816 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000817 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000818 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000819 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000820 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000821 break;
David Greenee8f3b272009-05-14 21:22:49 +0000822 }
823 if (Lex.getCode() != tgtok::l_paren) {
824 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000825 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000826 }
827 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000828
David Greeneaf8ee2c2011-07-29 22:43:06 +0000829 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000830 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000831
Craig Topper85c07002015-04-30 05:54:22 +0000832 if (Code == UnOpInit::HEAD ||
833 Code == UnOpInit::TAIL ||
834 Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000835 ListInit *LHSl = dyn_cast<ListInit>(LHS);
836 StringInit *LHSs = dyn_cast<StringInit>(LHS);
837 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000838 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000839 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000840 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000841 }
842 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000843 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
844 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000845 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000846 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000847 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000848 }
849 }
850
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000851 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL ||
852 Code == UnOpInit::SIZE) {
Craig Topper011817a2014-04-09 04:50:04 +0000853 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000854 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000855 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000856 }
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000857 }
Bob Wilson7248f862009-11-22 04:24:42 +0000858
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000859 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
Craig Topperec9072d2015-05-14 05:53:53 +0000860 if (LHSl && LHSl->empty()) {
David Greened571b3c2009-05-14 22:38:31 +0000861 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000862 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000863 }
864 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000865 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000866 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000867 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000868 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000869 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000870 }
Craig Toppera9642b42015-05-04 01:35:39 +0000871 Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
872 : ListRecTy::get(Itemt->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000873 } else {
David Greened571b3c2009-05-14 22:38:31 +0000874 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000875 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000876 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000877 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000878 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000879 }
Craig Toppera9642b42015-05-04 01:35:39 +0000880 Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
David Greened571b3c2009-05-14 22:38:31 +0000881 }
882 }
883 }
884
David Greenee8f3b272009-05-14 21:22:49 +0000885 if (Lex.getCode() != tgtok::r_paren) {
886 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000887 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000888 }
889 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000890 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000891 }
David Greene5d0c0512009-05-14 20:54:48 +0000892
893 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000894 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000895 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000896 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +0000897 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000898 case tgtok::XSRL:
899 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000900 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000901 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000902 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000903 tgtok::TokKind OpTok = Lex.getCode();
904 SMLoc OpLoc = Lex.getLoc();
905 Lex.Lex(); // eat the operation
906
David Greene5d0c0512009-05-14 20:54:48 +0000907 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000908 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000909
Chris Lattner61ea00b2010-10-05 23:58:18 +0000910 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000911 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000912 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000913 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000914 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000915 case tgtok::XOR: Code = BinOpInit::OR; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000916 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
917 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
918 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
919 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000920 case tgtok::XListConcat:
921 Code = BinOpInit::LISTCONCAT;
922 // We don't know the list type until we parse the first argument
923 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000924 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000925 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000926 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000927 break;
David Greene5d0c0512009-05-14 20:54:48 +0000928 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000929
David Greene5d0c0512009-05-14 20:54:48 +0000930 if (Lex.getCode() != tgtok::l_paren) {
931 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000932 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000933 }
934 Lex.Lex(); // eat the '('
935
David Greeneaf8ee2c2011-07-29 22:43:06 +0000936 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000937
Chris Lattner61ea00b2010-10-05 23:58:18 +0000938 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000939 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000940
Chris Lattner61ea00b2010-10-05 23:58:18 +0000941 while (Lex.getCode() == tgtok::comma) {
942 Lex.Lex(); // eat the ','
943
944 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000945 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000946 }
David Greene5d0c0512009-05-14 20:54:48 +0000947
948 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000949 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000950 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000951 }
952 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000953
Daniel Sanders314e80e2014-05-07 10:13:19 +0000954 // If we are doing !listconcat, we should know the type by now
955 if (OpTok == tgtok::XListConcat) {
Nicolai Haehnlee4a2cf52018-02-22 15:26:28 +0000956 if (TypedInit *Arg0 = dyn_cast<TypedInit>(InitList[0]))
Daniel Sanders314e80e2014-05-07 10:13:19 +0000957 Type = Arg0->getType();
958 else {
Matthias Braun8c209aa2017-01-28 02:02:38 +0000959 InitList[0]->print(errs());
Daniel Sanders314e80e2014-05-07 10:13:19 +0000960 Error(OpLoc, "expected a list");
961 return nullptr;
962 }
963 }
964
Chris Lattner61ea00b2010-10-05 23:58:18 +0000965 // We allow multiple operands to associative operators like !strconcat as
966 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000967 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000968 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000969 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000970 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
971 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000972 InitList.back() = RHS;
973 }
974 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000975
Chris Lattner61ea00b2010-10-05 23:58:18 +0000976 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000977 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000978 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000979
Chris Lattner61ea00b2010-10-05 23:58:18 +0000980 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +0000981 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000982 }
983
David Greene3587eed2009-05-14 23:26:46 +0000984 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +0000985 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +0000986 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
987 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000988 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000989
David Greene98ed3c72009-05-14 21:54:42 +0000990 tgtok::TokKind LexCode = Lex.getCode();
991 Lex.Lex(); // eat the operation
992 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +0000993 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +0000994 case tgtok::XIf:
995 Code = TernOpInit::IF;
996 break;
David Greenee917fff2009-05-14 22:23:47 +0000997 case tgtok::XForEach:
998 Code = TernOpInit::FOREACH;
999 break;
David Greene98ed3c72009-05-14 21:54:42 +00001000 case tgtok::XSubst:
1001 Code = TernOpInit::SUBST;
1002 break;
1003 }
1004 if (Lex.getCode() != tgtok::l_paren) {
1005 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001006 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001007 }
1008 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001009
David Greeneaf8ee2c2011-07-29 22:43:06 +00001010 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001011 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001012
David Greene98ed3c72009-05-14 21:54:42 +00001013 if (Lex.getCode() != tgtok::comma) {
1014 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001015 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001016 }
1017 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001018
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001019 Init *MHS = ParseValue(CurRec, ItemType);
1020 if (!MHS)
1021 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001022
David Greene98ed3c72009-05-14 21:54:42 +00001023 if (Lex.getCode() != tgtok::comma) {
1024 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001025 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001026 }
1027 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001028
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001029 Init *RHS = ParseValue(CurRec, ItemType);
1030 if (!RHS)
1031 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001032
David Greene98ed3c72009-05-14 21:54:42 +00001033 if (Lex.getCode() != tgtok::r_paren) {
1034 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001035 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001036 }
1037 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001038
David Greene98ed3c72009-05-14 21:54:42 +00001039 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001040 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001041 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001042 RecTy *MHSTy = nullptr;
1043 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001044
Sean Silvafb509ed2012-10-10 20:24:43 +00001045 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001046 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001047 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001048 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001049 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001050 MHSTy = BitRecTy::get();
1051
Sean Silvafb509ed2012-10-10 20:24:43 +00001052 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001053 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001054 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001055 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001056 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001057 RHSTy = BitRecTy::get();
1058
1059 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001060 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001061 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001062 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001063 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001064
1065 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001066 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001067 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001068 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001069
1070 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1071 Type = RHSTy;
1072 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1073 Type = MHSTy;
Bob Wilson7248f862009-11-22 04:24:42 +00001074 } else {
David Greene3587eed2009-05-14 23:26:46 +00001075 TokError("inconsistent types for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001076 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001077 }
1078 break;
1079 }
David Greenee917fff2009-05-14 22:23:47 +00001080 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001081 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
Craig Topper011817a2014-04-09 04:50:04 +00001082 if (!MHSt) {
David Greenee917fff2009-05-14 22:23:47 +00001083 TokError("could not get type for !foreach");
Craig Topper011817a2014-04-09 04:50:04 +00001084 return nullptr;
David Greenee917fff2009-05-14 22:23:47 +00001085 }
1086 Type = MHSt->getType();
Nicolai Haehnle6d649152018-02-22 15:26:35 +00001087 if (isa<ListRecTy>(Type)) {
1088 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1089 if (!RHSt) {
1090 TokError("could not get type of !foreach list elements");
1091 return nullptr;
1092 }
1093 Type = RHSt->getType()->getListTy();
1094 }
David Greenee917fff2009-05-14 22:23:47 +00001095 break;
1096 }
David Greene98ed3c72009-05-14 21:54:42 +00001097 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001098 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001099 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001100 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001101 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001102 }
1103 Type = RHSt->getType();
1104 break;
1105 }
1106 }
David Greenee32ebf22011-07-29 19:07:07 +00001107 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001108 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001109 }
David Greene5d0c0512009-05-14 20:54:48 +00001110 }
David Greene5d0c0512009-05-14 20:54:48 +00001111}
1112
1113/// ParseOperatorType - Parse a type for an operator. This returns
1114/// null on error.
1115///
1116/// OperatorType ::= '<' Type '>'
1117///
Dan Gohman1432ef82009-08-12 22:10:57 +00001118RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001119 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001120
1121 if (Lex.getCode() != tgtok::less) {
1122 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001123 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001124 }
1125 Lex.Lex(); // eat the <
1126
1127 Type = ParseType();
1128
Craig Topper011817a2014-04-09 04:50:04 +00001129 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001130 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001131 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001132 }
1133
1134 if (Lex.getCode() != tgtok::greater) {
1135 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001136 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001137 }
1138 Lex.Lex(); // eat the >
1139
1140 return Type;
1141}
1142
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001143/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1144///
1145/// SimpleValue ::= IDValue
1146/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001147/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001148/// SimpleValue ::= CODEFRAGMENT
1149/// SimpleValue ::= '?'
1150/// SimpleValue ::= '{' ValueList '}'
1151/// SimpleValue ::= ID '<' ValueListNE '>'
1152/// SimpleValue ::= '[' ValueList ']'
1153/// SimpleValue ::= '(' IDValue DagArgList ')'
1154/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001155/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001156/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1157/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1158/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001159/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001160/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1161///
David Greened4263a62011-10-19 13:04:20 +00001162Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1163 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001164 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001165 switch (Lex.getCode()) {
1166 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001167 case tgtok::paste:
1168 // This is a leading paste operation. This is deprecated but
1169 // still exists in some .td files. Ignore it.
1170 Lex.Lex(); // Skip '#'.
1171 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001172 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001173 case tgtok::BinaryIntVal: {
1174 auto BinaryVal = Lex.getCurBinaryIntVal();
1175 SmallVector<Init*, 16> Bits(BinaryVal.second);
1176 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001177 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001178 R = BitsInit::get(Bits);
1179 Lex.Lex();
1180 break;
1181 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001182 case tgtok::StrVal: {
1183 std::string Val = Lex.getCurStrVal();
1184 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001185
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001186 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001187 while (Lex.getCode() == tgtok::StrVal) {
1188 Val += Lex.getCurStrVal();
1189 Lex.Lex();
1190 }
Bob Wilson7248f862009-11-22 04:24:42 +00001191
David Greenee32ebf22011-07-29 19:07:07 +00001192 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001193 break;
1194 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001195 case tgtok::CodeFragment:
Tim Northover88403d72016-07-05 21:22:55 +00001196 R = CodeInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001197 Lex.Lex();
1198 break;
1199 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001200 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001201 Lex.Lex();
1202 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001203 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001204 SMLoc NameLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00001205 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001206 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001207 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001208
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001209 // Value ::= ID '<' ValueListNE '>'
1210 if (Lex.Lex() == tgtok::greater) {
1211 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001212 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001213 }
David Greene8618f952009-06-08 20:23:18 +00001214
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001215 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1216 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1217 // body.
Matthias Braun215ff842016-12-05 07:35:13 +00001218 Record *Class = Records.getClass(Name->getValue());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001219 if (!Class) {
Matthias Braun215ff842016-12-05 07:35:13 +00001220 Error(NameLoc, "Expected a class name, got '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001221 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001222 }
David Greene8618f952009-06-08 20:23:18 +00001223
Matthias Braunc66e7552016-12-05 06:41:54 +00001224 SubClassReference SCRef;
1225 ParseValueList(SCRef.TemplateArgs, CurRec, Class);
1226 if (SCRef.TemplateArgs.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001227
David Greene8618f952009-06-08 20:23:18 +00001228 if (Lex.getCode() != tgtok::greater) {
1229 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001230 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001231 }
1232 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001233 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001234
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001235 // Create the new record, set it as CurRec temporarily.
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00001236 auto NewRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), NameLoc,
1237 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001238 Record *NewRec = NewRecOwner.get(); // Keep a copy since we may release.
Jordan Rosef12e8a92013-01-10 18:50:11 +00001239 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001240 SCRef.Rec = Class;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001241 // Add info about the subclass to NewRec.
Craig Topper84138712014-11-29 05:31:10 +00001242 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001243 return nullptr;
Craig Topper84138712014-11-29 05:31:10 +00001244
Hal Finkela8c1f462014-01-02 20:47:09 +00001245 if (!CurMultiClass) {
1246 NewRec->resolveReferences();
Craig Toppercdab2322014-11-29 05:52:51 +00001247 Records.addDef(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001248 } else {
Adam Nemete5a07162014-09-16 17:14:13 +00001249 // This needs to get resolved once the multiclass template arguments are
1250 // known before any use.
1251 NewRec->setResolveFirst(true);
Hal Finkela8c1f462014-01-02 20:47:09 +00001252 // Otherwise, we're inside a multiclass, add it to the multiclass.
Craig Topperc3504c42014-12-11 05:25:33 +00001253 CurMultiClass->DefPrototypes.push_back(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001254
1255 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00001256 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
1257 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Hal Finkela8c1f462014-01-02 20:47:09 +00001258 assert(RV && "Template arg doesn't exist?");
1259 NewRec->addValue(*RV);
1260 }
1261
1262 // We can't return the prototype def here, instead return:
1263 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1264 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1265 assert(MCNameRV && "multiclass record must have a NAME");
1266
1267 return UnOpInit::get(UnOpInit::CAST,
1268 BinOpInit::get(BinOpInit::STRCONCAT,
1269 VarInit::get(MCNameRV->getName(),
1270 MCNameRV->getType()),
1271 NewRec->getNameInit(),
1272 StringRecTy::get()),
1273 Class->getDefInit()->getType());
1274 }
Bob Wilson7248f862009-11-22 04:24:42 +00001275
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001276 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001277 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001278 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001279 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001280 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001281 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001282 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001283
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001284 if (Lex.getCode() != tgtok::r_brace) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001285 ParseValueList(Vals, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001286 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001287 }
1288 if (Lex.getCode() != tgtok::r_brace) {
1289 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001290 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001291 }
1292 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001293
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001294 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001295
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001296 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1297 // first. We'll first read everything in to a vector, then we can reverse
1298 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001299 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001300 // FIXME: The following two loops would not be duplicated
1301 // if the API was a little more orthogonal.
1302
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001303 // bits<n> values are allowed to initialize n bits.
1304 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1305 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1306 NewBits.push_back(BI->getBit((e - i) - 1));
1307 continue;
1308 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001309 // bits<n> can also come from variable initializers.
1310 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1311 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1312 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1313 NewBits.push_back(VI->getBit((e - i) - 1));
1314 continue;
1315 }
1316 // Fallthrough to try convert this to a bit.
1317 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001318 // All other values must be convertible to just a single bit.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001319 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001320 if (!Bit) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001321 Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
Chris Lattner52416952007-11-22 21:06:59 +00001322 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001323 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001324 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001325 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001326 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001327 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001328 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001329 }
1330 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1331 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001332 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001333
Craig Topper011817a2014-04-09 04:50:04 +00001334 RecTy *DeducedEltTy = nullptr;
1335 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001336
Craig Topper011817a2014-04-09 04:50:04 +00001337 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001338 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001339 if (!ListType) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001340 TokError(Twine("Type mismatch for list, expected list type, got ") +
1341 ItemType->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001342 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001343 }
1344 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001345 }
David Greene8618f952009-06-08 20:23:18 +00001346
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001347 if (Lex.getCode() != tgtok::r_square) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001348 ParseValueList(Vals, CurRec, nullptr,
1349 GivenListTy ? GivenListTy->getElementType() : nullptr);
Craig Topper011817a2014-04-09 04:50:04 +00001350 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001351 }
1352 if (Lex.getCode() != tgtok::r_square) {
1353 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001354 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001355 }
1356 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001357
Craig Topper011817a2014-04-09 04:50:04 +00001358 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001359 if (Lex.getCode() == tgtok::less) {
1360 // Optional list element type
1361 Lex.Lex(); // eat the '<'
1362
1363 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001364 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001365 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001366 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001367 }
1368
1369 if (Lex.getCode() != tgtok::greater) {
1370 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001371 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001372 }
1373 Lex.Lex(); // eat the '>'
1374 }
1375
1376 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001377 RecTy *EltTy = nullptr;
Craig Toppereb4d7c62015-04-29 04:43:36 +00001378 for (Init *V : Vals) {
1379 TypedInit *TArg = dyn_cast<TypedInit>(V);
Craig Topper011817a2014-04-09 04:50:04 +00001380 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001381 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001382 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001383 }
Craig Topper011817a2014-04-09 04:50:04 +00001384 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001385 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001386 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001387 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001388 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001389 }
Bob Wilson7248f862009-11-22 04:24:42 +00001390 } else {
David Greene8618f952009-06-08 20:23:18 +00001391 EltTy = TArg->getType();
1392 }
1393 }
1394
Craig Topper011817a2014-04-09 04:50:04 +00001395 if (GivenEltTy) {
1396 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001397 // Verify consistency
1398 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1399 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001400 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001401 }
1402 }
1403 EltTy = GivenEltTy;
1404 }
1405
Craig Topper011817a2014-04-09 04:50:04 +00001406 if (!EltTy) {
1407 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001408 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001409 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001410 }
1411 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001412 } else {
David Greene8618f952009-06-08 20:23:18 +00001413 // Make sure the deduced type is compatible with the given type
1414 if (GivenListTy) {
1415 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
Nicolai Haehnlef19083d2018-02-22 15:26:21 +00001416 TokError(Twine("Element type mismatch for list: element type '") +
1417 EltTy->getAsString() + "' not convertible to '" +
1418 GivenListTy->getElementType()->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001419 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001420 }
1421 }
1422 DeducedEltTy = EltTy;
1423 }
Bob Wilson7248f862009-11-22 04:24:42 +00001424
David Greenee32ebf22011-07-29 19:07:07 +00001425 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001426 }
1427 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1428 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001429 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001430 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001431 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001432 }
Bob Wilson7248f862009-11-22 04:24:42 +00001433
David Greeneaf8ee2c2011-07-29 22:43:06 +00001434 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001435 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001436
Nate Begemandbe3f772009-03-19 05:21:56 +00001437 // If the operator name is present, parse it.
Matthias Braun7cf3b112016-12-05 06:00:41 +00001438 StringInit *OperatorName = nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001439 if (Lex.getCode() == tgtok::colon) {
1440 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1441 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001442 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001443 }
Matthias Braun7cf3b112016-12-05 06:00:41 +00001444 OperatorName = StringInit::get(Lex.getCurStrVal());
Nate Begemandbe3f772009-03-19 05:21:56 +00001445 Lex.Lex(); // eat the VarName.
1446 }
Bob Wilson7248f862009-11-22 04:24:42 +00001447
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001448 SmallVector<std::pair<llvm::Init*, StringInit*>, 8> DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001449 if (Lex.getCode() != tgtok::r_paren) {
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001450 ParseDagArgList(DagArgs, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001451 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001452 }
Bob Wilson7248f862009-11-22 04:24:42 +00001453
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001454 if (Lex.getCode() != tgtok::r_paren) {
1455 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001456 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001457 }
1458 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001459
David Greenee32ebf22011-07-29 19:07:07 +00001460 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001461 }
Bob Wilson7248f862009-11-22 04:24:42 +00001462
David Greene2f7cf7f2011-01-07 17:05:37 +00001463 case tgtok::XHead:
1464 case tgtok::XTail:
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +00001465 case tgtok::XSize:
David Greene2f7cf7f2011-01-07 17:05:37 +00001466 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001467 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001468 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001469 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001470 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +00001471 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +00001472 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001473 case tgtok::XSRL:
1474 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001475 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001476 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001477 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001478 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001479 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001480 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001481 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001482 }
1483 }
Bob Wilson7248f862009-11-22 04:24:42 +00001484
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001485 return R;
1486}
1487
1488/// ParseValue - Parse a tblgen value. This returns null on error.
1489///
1490/// Value ::= SimpleValue ValueSuffix*
1491/// ValueSuffix ::= '{' BitList '}'
1492/// ValueSuffix ::= '[' BitList ']'
1493/// ValueSuffix ::= '.' ID
1494///
David Greened4263a62011-10-19 13:04:20 +00001495Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1496 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001497 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001498
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001499 // Parse the suffixes now if present.
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001500 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001501 switch (Lex.getCode()) {
1502 default: return Result;
1503 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001504 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001505 // This is the beginning of the object body.
1506 return Result;
1507
Chris Lattner526c8cb2009-06-21 03:39:35 +00001508 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001509 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001510 SmallVector<unsigned, 16> Ranges;
1511 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001512 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001513
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001514 // Reverse the bitlist.
1515 std::reverse(Ranges.begin(), Ranges.end());
1516 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001517 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001518 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001519 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001520 }
Bob Wilson7248f862009-11-22 04:24:42 +00001521
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001522 // Eat the '}'.
1523 if (Lex.getCode() != tgtok::r_brace) {
1524 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001525 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001526 }
1527 Lex.Lex();
1528 break;
1529 }
1530 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001531 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001532 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001533 SmallVector<unsigned, 16> Ranges;
1534 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001535 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001536
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001537 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001538 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001539 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001540 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001541 }
Bob Wilson7248f862009-11-22 04:24:42 +00001542
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001543 // Eat the ']'.
1544 if (Lex.getCode() != tgtok::r_square) {
1545 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001546 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001547 }
1548 Lex.Lex();
1549 break;
1550 }
Matthias Braun6a441832016-12-05 06:00:36 +00001551 case tgtok::period: {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001552 if (Lex.Lex() != tgtok::Id) { // eat the .
1553 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001554 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001555 }
Matthias Braun6a441832016-12-05 06:00:36 +00001556 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
1557 if (!Result->getFieldType(FieldName)) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001558 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001559 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001560 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001561 }
Matthias Braun6a441832016-12-05 06:00:36 +00001562 Result = FieldInit::get(Result, FieldName);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001563 Lex.Lex(); // eat field name
1564 break;
Matthias Braun6a441832016-12-05 06:00:36 +00001565 }
David Greene8e85b482011-10-19 13:04:43 +00001566
1567 case tgtok::paste:
1568 SMLoc PasteLoc = Lex.getLoc();
1569
1570 // Create a !strconcat() operation, first casting each operand to
1571 // a string if necessary.
1572
Sean Silvafb509ed2012-10-10 20:24:43 +00001573 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001574 if (!LHS) {
1575 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001576 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001577 }
Craig Toppera9642b42015-05-04 01:35:39 +00001578
David Greene8e85b482011-10-19 13:04:43 +00001579 if (LHS->getType() != StringRecTy::get()) {
1580 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1581 }
1582
Craig Topper011817a2014-04-09 04:50:04 +00001583 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001584
1585 Lex.Lex(); // Eat the '#'.
1586 switch (Lex.getCode()) {
1587 case tgtok::colon:
1588 case tgtok::semi:
1589 case tgtok::l_brace:
1590 // These are all of the tokens that can begin an object body.
1591 // Some of these can also begin values but we disallow those cases
1592 // because they are unlikely to be useful.
Craig Toppera9642b42015-05-04 01:35:39 +00001593
David Greene8e85b482011-10-19 13:04:43 +00001594 // Trailing paste, concat with an empty string.
1595 RHS = StringInit::get("");
1596 break;
1597
1598 default:
1599 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001600 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001601 if (!RHS) {
1602 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001603 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001604 }
1605
1606 if (RHS->getType() != StringRecTy::get()) {
1607 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1608 }
Craig Toppera9642b42015-05-04 01:35:39 +00001609
David Greene8e85b482011-10-19 13:04:43 +00001610 break;
1611 }
1612
1613 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1614 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1615 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001616 }
1617 }
1618}
1619
1620/// ParseDagArgList - Parse the argument list for a dag literal expression.
1621///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001622/// DagArg ::= Value (':' VARNAME)?
1623/// DagArg ::= VARNAME
1624/// DagArgList ::= DagArg
1625/// DagArgList ::= DagArgList ',' DagArg
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001626void TGParser::ParseDagArgList(
1627 SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
1628 Record *CurRec) {
Bob Wilson7248f862009-11-22 04:24:42 +00001629
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001630 while (true) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001631 // DagArg ::= VARNAME
1632 if (Lex.getCode() == tgtok::VarName) {
1633 // A missing value is treated like '?'.
Matthias Braunbb053162016-12-05 06:00:46 +00001634 StringInit *VarName = StringInit::get(Lex.getCurStrVal());
1635 Result.emplace_back(UnsetInit::get(), VarName);
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001636 Lex.Lex();
1637 } else {
1638 // DagArg ::= Value (':' VARNAME)?
1639 Init *Val = ParseValue(CurRec);
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001640 if (!Val) {
1641 Result.clear();
1642 return;
1643 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001644
1645 // If the variable name is present, add it.
Matthias Braunbb053162016-12-05 06:00:46 +00001646 StringInit *VarName = nullptr;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001647 if (Lex.getCode() == tgtok::colon) {
1648 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1649 TokError("expected variable name in dag literal");
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001650 Result.clear();
1651 return;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001652 }
Matthias Braunbb053162016-12-05 06:00:46 +00001653 VarName = StringInit::get(Lex.getCurStrVal());
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001654 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001655 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001656
1657 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001658 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001659 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001660 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001661 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001662}
1663
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001664/// ParseValueList - Parse a comma separated list of values, returning them as a
1665/// vector. Note that this always expects to be able to parse at least one
1666/// value. It returns an empty list if this is not possible.
1667///
1668/// ValueList ::= Value (',' Value)
1669///
Matthias Braunc66e7552016-12-05 06:41:54 +00001670void TGParser::ParseValueList(SmallVectorImpl<Init*> &Result, Record *CurRec,
1671 Record *ArgsRec, RecTy *EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001672 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001673 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001674 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001675 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001676 if (TArgs.empty()) {
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001677 TokError("template argument provided to non-template class");
Matthias Braunc66e7552016-12-05 06:41:54 +00001678 Result.clear();
1679 return;
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001680 }
David Greene8618f952009-06-08 20:23:18 +00001681 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001682 if (!RV) {
1683 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1684 << ")\n";
1685 }
David Greene8618f952009-06-08 20:23:18 +00001686 assert(RV && "Template argument record not found??");
1687 ItemType = RV->getType();
1688 ++ArgN;
1689 }
1690 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00001691 if (!Result.back()) {
1692 Result.clear();
1693 return;
1694 }
Bob Wilson7248f862009-11-22 04:24:42 +00001695
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001696 while (Lex.getCode() == tgtok::comma) {
1697 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001698
Craig Topper011817a2014-04-09 04:50:04 +00001699 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001700 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001701 if (ArgN >= TArgs.size()) {
1702 TokError("too many template arguments");
Matthias Braunc66e7552016-12-05 06:41:54 +00001703 Result.clear();
1704 return;
Bob Wilson7248f862009-11-22 04:24:42 +00001705 }
David Greene8618f952009-06-08 20:23:18 +00001706 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1707 assert(RV && "Template argument record not found??");
1708 ItemType = RV->getType();
1709 ++ArgN;
1710 }
1711 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00001712 if (!Result.back()) {
1713 Result.clear();
1714 return;
1715 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001716 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001717}
1718
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001719/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1720/// empty string on error. This can happen in a number of different context's,
1721/// including within a def or in the template args for a def (which which case
1722/// CurRec will be non-null) and within the template args for a multiclass (in
1723/// which case CurRec will be null, but CurMultiClass will be set). This can
1724/// also happen within a def that is within a multiclass, which will set both
1725/// CurRec and CurMultiClass.
1726///
1727/// Declaration ::= FIELD? Type ID ('=' Value)?
1728///
David Greenedb10e692011-10-19 13:02:42 +00001729Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001730 bool ParsingTemplateArgs) {
1731 // Read the field prefix if present.
1732 bool HasField = Lex.getCode() == tgtok::Field;
1733 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001734
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001735 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001736 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001737
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001738 if (Lex.getCode() != tgtok::Id) {
1739 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001740 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001741 }
Bob Wilson7248f862009-11-22 04:24:42 +00001742
Chris Lattner526c8cb2009-06-21 03:39:35 +00001743 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001744 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001745 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001746
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001747 if (ParsingTemplateArgs) {
Craig Toppera9642b42015-05-04 01:35:39 +00001748 if (CurRec)
David Greenedb10e692011-10-19 13:02:42 +00001749 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Craig Toppera9642b42015-05-04 01:35:39 +00001750 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001751 assert(CurMultiClass);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001752 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001753 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1754 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001755 }
Bob Wilson7248f862009-11-22 04:24:42 +00001756
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001757 // Add the value.
1758 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001759 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001760
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001761 // If a value is present, parse it.
1762 if (Lex.getCode() == tgtok::equal) {
1763 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001764 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001765 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001766 if (!Val ||
Craig Toppercfd81732016-01-04 03:15:08 +00001767 SetValue(CurRec, ValLoc, DeclName, None, Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001768 // Return the name, even if an error is thrown. This is so that we can
1769 // continue to make some progress, even without the value having been
1770 // initialized.
1771 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001772 }
Bob Wilson7248f862009-11-22 04:24:42 +00001773
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001774 return DeclName;
1775}
1776
David Greenefb927af2012-02-22 16:09:41 +00001777/// ParseForeachDeclaration - Read a foreach declaration, returning
1778/// the name of the declared object or a NULL Init on error. Return
1779/// the name of the parsed initializer list through ForeachListName.
1780///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001781/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1782/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1783/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001784///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001785VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001786 if (Lex.getCode() != tgtok::Id) {
1787 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001788 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001789 }
1790
1791 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1792 Lex.Lex();
1793
1794 // If a value is present, parse it.
1795 if (Lex.getCode() != tgtok::equal) {
1796 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001797 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001798 }
1799 Lex.Lex(); // Eat the '='
1800
Craig Topper011817a2014-04-09 04:50:04 +00001801 RecTy *IterType = nullptr;
Matthias Braunc66e7552016-12-05 06:41:54 +00001802 SmallVector<unsigned, 16> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001803
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001804 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001805 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001806 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001807 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001808 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001809 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001810 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001811 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001812 }
1813 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001814 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001815 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001816 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001817 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001818 }
1819 IterType = ListType->getElementType();
1820 break;
David Greenefb927af2012-02-22 16:09:41 +00001821 }
1822
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001823 case tgtok::IntVal: { // RangePiece.
1824 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001825 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001826 break;
David Greenefb927af2012-02-22 16:09:41 +00001827 }
1828
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001829 case tgtok::l_brace: { // '{' RangeList '}'
1830 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001831 ParseRangeList(Ranges);
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001832 if (Lex.getCode() != tgtok::r_brace) {
1833 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001834 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001835 }
1836 Lex.Lex();
1837 break;
1838 }
1839 }
David Greenefb927af2012-02-22 16:09:41 +00001840
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001841 if (!Ranges.empty()) {
1842 assert(!IterType && "Type already initialized?");
1843 IterType = IntRecTy::get();
1844 std::vector<Init*> Values;
Craig Topper8eb764c2015-06-02 06:19:28 +00001845 for (unsigned R : Ranges)
1846 Values.push_back(IntInit::get(R));
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001847 ForeachListValue = ListInit::get(Values, IterType);
1848 }
1849
1850 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001851 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001852
1853 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001854}
1855
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001856/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1857/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1858/// template args for a def, which may or may not be in a multiclass. If null,
1859/// these are the template args for a multiclass.
1860///
1861/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001862///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001863bool TGParser::ParseTemplateArgList(Record *CurRec) {
1864 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1865 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001866
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001867 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001868
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001869 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001870 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001871 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001872 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001873
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001874 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001875
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001876 while (Lex.getCode() == tgtok::comma) {
1877 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001878
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001879 // Read the following declarations.
1880 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001881 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001882 return true;
1883 TheRecToAddTo->addTemplateArg(TemplArg);
1884 }
Bob Wilson7248f862009-11-22 04:24:42 +00001885
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001886 if (Lex.getCode() != tgtok::greater)
1887 return TokError("expected '>' at end of template argument list");
1888 Lex.Lex(); // eat the '>'.
1889 return false;
1890}
1891
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001892/// ParseBodyItem - Parse a single item at within the body of a def or class.
1893///
1894/// BodyItem ::= Declaration ';'
1895/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1896bool TGParser::ParseBodyItem(Record *CurRec) {
1897 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001898 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001899 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001900
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001901 if (Lex.getCode() != tgtok::semi)
1902 return TokError("expected ';' after declaration");
1903 Lex.Lex();
1904 return false;
1905 }
1906
1907 // LET ID OptionalRangeList '=' Value ';'
1908 if (Lex.Lex() != tgtok::Id)
1909 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001910
Chris Lattner526c8cb2009-06-21 03:39:35 +00001911 SMLoc IdLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00001912 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001913 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001914
Matthias Braunc66e7552016-12-05 06:41:54 +00001915 SmallVector<unsigned, 16> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001916 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001917 return true;
1918 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001919
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001920 if (Lex.getCode() != tgtok::equal)
1921 return TokError("expected '=' in let expression");
1922 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001923
David Greene8618f952009-06-08 20:23:18 +00001924 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001925 if (!Field)
Matthias Braun215ff842016-12-05 07:35:13 +00001926 return TokError("Value '" + FieldName->getValue() + "' unknown!");
David Greene8618f952009-06-08 20:23:18 +00001927
1928 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001929
David Greeneaf8ee2c2011-07-29 22:43:06 +00001930 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001931 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001932
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001933 if (Lex.getCode() != tgtok::semi)
1934 return TokError("expected ';' after let expression");
1935 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001936
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001937 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1938}
1939
1940/// ParseBody - Read the body of a class or def. Return true on error, false on
1941/// success.
1942///
1943/// Body ::= ';'
1944/// Body ::= '{' BodyList '}'
1945/// BodyList BodyItem*
1946///
1947bool TGParser::ParseBody(Record *CurRec) {
1948 // If this is a null definition, just eat the semi and return.
1949 if (Lex.getCode() == tgtok::semi) {
1950 Lex.Lex();
1951 return false;
1952 }
Bob Wilson7248f862009-11-22 04:24:42 +00001953
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001954 if (Lex.getCode() != tgtok::l_brace)
1955 return TokError("Expected ';' or '{' to start body");
1956 // Eat the '{'.
1957 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001958
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001959 while (Lex.getCode() != tgtok::r_brace)
1960 if (ParseBodyItem(CurRec))
1961 return true;
1962
1963 // Eat the '}'.
1964 Lex.Lex();
1965 return false;
1966}
1967
Sean Silvacb1a75e2013-01-09 04:49:14 +00001968/// \brief Apply the current let bindings to \a CurRec.
1969/// \returns true on error, false otherwise.
1970bool TGParser::ApplyLetStack(Record *CurRec) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001971 for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
Craig Topper8eb764c2015-06-02 06:19:28 +00001972 for (LetRecord &LR : LetInfo)
1973 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
Sean Silvacb1a75e2013-01-09 04:49:14 +00001974 return true;
1975 return false;
1976}
1977
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001978/// ParseObjectBody - Parse the body of a def or class. This consists of an
1979/// optional ClassList followed by a Body. CurRec is the current def or class
1980/// that is being parsed.
1981///
1982/// ObjectBody ::= BaseClassList Body
1983/// BaseClassList ::= /*empty*/
1984/// BaseClassList ::= ':' BaseClassListNE
1985/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1986///
1987bool TGParser::ParseObjectBody(Record *CurRec) {
1988 // If there is a baseclass list, read it.
1989 if (Lex.getCode() == tgtok::colon) {
1990 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001991
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001992 // Read all of the subclasses.
1993 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001994 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001995 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00001996 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001997
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001998 // Add it.
1999 if (AddSubClass(CurRec, SubClass))
2000 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002001
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002002 if (Lex.getCode() != tgtok::comma) break;
2003 Lex.Lex(); // eat ','.
2004 SubClass = ParseSubClassReference(CurRec, false);
2005 }
2006 }
2007
Sean Silvacb1a75e2013-01-09 04:49:14 +00002008 if (ApplyLetStack(CurRec))
2009 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002010
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002011 return ParseBody(CurRec);
2012}
2013
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002014/// ParseDef - Parse and return a top level or multiclass def, return the record
2015/// corresponding to it. This returns null on error.
2016///
2017/// DefInst ::= DEF ObjectName ObjectBody
2018///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002019bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00002020 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002021 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00002022 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002023
2024 // Parse ObjectName and make a record for it.
Craig Topper84138712014-11-29 05:31:10 +00002025 std::unique_ptr<Record> CurRecOwner;
Jordan Roseabdd99b2013-01-10 18:50:05 +00002026 Init *Name = ParseObjectName(CurMultiClass);
2027 if (Name)
Craig Topper84138712014-11-29 05:31:10 +00002028 CurRecOwner = make_unique<Record>(Name, DefLoc, Records);
Jordan Roseabdd99b2013-01-10 18:50:05 +00002029 else
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00002030 CurRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), DefLoc,
2031 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00002032 Record *CurRec = CurRecOwner.get(); // Keep a copy since we may release.
Bob Wilson7248f862009-11-22 04:24:42 +00002033
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002034 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002035 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002036
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002037 // Ensure redefinition doesn't happen.
Craig Topper84138712014-11-29 05:31:10 +00002038 if (Records.getDef(CurRec->getNameInitAsString()))
2039 return Error(DefLoc, "def '" + CurRec->getNameInitAsString()+
2040 "' already defined");
Craig Toppercdab2322014-11-29 05:52:51 +00002041 Records.addDef(std::move(CurRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00002042
2043 if (ParseObjectBody(CurRec))
2044 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002045 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002046 // Parse the body before adding this prototype to the DefPrototypes vector.
2047 // That way implicit definitions will be added to the DefPrototypes vector
2048 // before this object, instantiated prior to defs derived from this object,
2049 // and this available for indirect name resolution when defs derived from
2050 // this object are instantiated.
Craig Topper84138712014-11-29 05:31:10 +00002051 if (ParseObjectBody(CurRec))
Hal Finkela8c1f462014-01-02 20:47:09 +00002052 return true;
2053
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002054 // Otherwise, a def inside a multiclass, add it to the multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002055 for (const auto &Proto : CurMultiClass->DefPrototypes)
2056 if (Proto->getNameInit() == CurRec->getNameInit())
Craig Topper84138712014-11-29 05:31:10 +00002057 return Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
2058 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002059 CurMultiClass->DefPrototypes.push_back(std::move(CurRecOwner));
Anton Yartsev671dff12014-08-08 00:29:54 +00002060 } else if (ParseObjectBody(CurRec)) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002061 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002062 }
Bob Wilson7248f862009-11-22 04:24:42 +00002063
Craig Topper011817a2014-04-09 04:50:04 +00002064 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002065 // See Record::setName(). This resolve step will see any new name
2066 // for the def that might have been created when resolving
2067 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002068 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002069
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002070 // If ObjectBody has template arguments, it's an error.
2071 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002072
2073 if (CurMultiClass) {
2074 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002075 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
2076 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002077 assert(RV && "Template arg doesn't exist?");
2078 CurRec->addValue(*RV);
2079 }
2080 }
2081
Craig Toppera9642b42015-05-04 01:35:39 +00002082 if (ProcessForeachDefs(CurRec, DefLoc))
Craig Topper84138712014-11-29 05:31:10 +00002083 return Error(DefLoc, "Could not process loops for def" +
2084 CurRec->getNameInitAsString());
David Greenefb927af2012-02-22 16:09:41 +00002085
2086 return false;
2087}
2088
2089/// ParseForeach - Parse a for statement. Return the record corresponding
2090/// to it. This returns true on error.
2091///
2092/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2093/// Foreach ::= FOREACH Declaration IN Object
2094///
2095bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2096 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2097 Lex.Lex(); // Eat the 'for' token.
2098
2099 // Make a temporary object to record items associated with the for
2100 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002101 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002102 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002103 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002104 return TokError("expected declaration in for");
2105
2106 if (Lex.getCode() != tgtok::In)
2107 return TokError("Unknown tok");
2108 Lex.Lex(); // Eat the in
2109
2110 // Create a loop object and remember it.
2111 Loops.push_back(ForeachLoop(IterName, ListValue));
2112
2113 if (Lex.getCode() != tgtok::l_brace) {
2114 // FOREACH Declaration IN Object
2115 if (ParseObject(CurMultiClass))
2116 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002117 } else {
David Greenefb927af2012-02-22 16:09:41 +00002118 SMLoc BraceLoc = Lex.getLoc();
2119 // Otherwise, this is a group foreach.
2120 Lex.Lex(); // eat the '{'.
2121
2122 // Parse the object list.
2123 if (ParseObjectList(CurMultiClass))
2124 return true;
2125
2126 if (Lex.getCode() != tgtok::r_brace) {
2127 TokError("expected '}' at end of foreach command");
2128 return Error(BraceLoc, "to match this '{'");
2129 }
2130 Lex.Lex(); // Eat the }
2131 }
2132
2133 // We've processed everything in this loop.
2134 Loops.pop_back();
2135
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002136 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002137}
2138
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002139/// ParseClass - Parse a tblgen class definition.
2140///
2141/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2142///
2143bool TGParser::ParseClass() {
2144 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2145 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002146
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002147 if (Lex.getCode() != tgtok::Id)
2148 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002149
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002150 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2151 if (CurRec) {
2152 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002153 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002154 !CurRec->getSuperClasses().empty() ||
2155 !CurRec->getTemplateArgs().empty())
Craig Topper85c07002015-04-30 05:54:22 +00002156 return TokError("Class '" + CurRec->getNameInitAsString() +
2157 "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002158 } else {
2159 // If this is the first reference to this class, create and add it.
Hans Wennborgffbbd532014-11-30 00:31:49 +00002160 auto NewRec =
2161 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
Craig Toppercdab2322014-11-29 05:52:51 +00002162 CurRec = NewRec.get();
2163 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002164 }
2165 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002166
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002167 // If there are template args, parse them.
2168 if (Lex.getCode() == tgtok::less)
2169 if (ParseTemplateArgList(CurRec))
2170 return true;
2171
2172 // Finally, parse the object body.
2173 return ParseObjectBody(CurRec);
2174}
2175
2176/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2177/// of LetRecords.
2178///
2179/// LetList ::= LetItem (',' LetItem)*
2180/// LetItem ::= ID OptionalRangeList '=' Value
2181///
Matthias Braunc66e7552016-12-05 06:41:54 +00002182void TGParser::ParseLetList(SmallVectorImpl<LetRecord> &Result) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002183 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002184 if (Lex.getCode() != tgtok::Id) {
2185 TokError("expected identifier in let definition");
Matthias Braunc66e7552016-12-05 06:41:54 +00002186 Result.clear();
2187 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002188 }
Matthias Braunc66e7552016-12-05 06:41:54 +00002189
Matthias Braun215ff842016-12-05 07:35:13 +00002190 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattner526c8cb2009-06-21 03:39:35 +00002191 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002192 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002193
2194 // Check for an optional RangeList.
Matthias Braunc66e7552016-12-05 06:41:54 +00002195 SmallVector<unsigned, 16> Bits;
2196 if (ParseOptionalRangeList(Bits)) {
2197 Result.clear();
2198 return;
2199 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002200 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002201
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002202 if (Lex.getCode() != tgtok::equal) {
2203 TokError("expected '=' in let expression");
Matthias Braunc66e7552016-12-05 06:41:54 +00002204 Result.clear();
2205 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002206 }
2207 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002208
Craig Topper011817a2014-04-09 04:50:04 +00002209 Init *Val = ParseValue(nullptr);
Matthias Braunc66e7552016-12-05 06:41:54 +00002210 if (!Val) {
2211 Result.clear();
2212 return;
2213 }
Bob Wilson7248f862009-11-22 04:24:42 +00002214
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002215 // Now that we have everything, add the record.
Matthias Braun215ff842016-12-05 07:35:13 +00002216 Result.emplace_back(Name, Bits, Val, NameLoc);
Bob Wilson7248f862009-11-22 04:24:42 +00002217
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002218 if (Lex.getCode() != tgtok::comma)
Matthias Braunc66e7552016-12-05 06:41:54 +00002219 return;
Bob Wilson7248f862009-11-22 04:24:42 +00002220 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002221 }
2222}
2223
2224/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002225/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002226///
2227/// Object ::= LET LetList IN '{' ObjectList '}'
2228/// Object ::= LET LetList IN Object
2229///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002230bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002231 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2232 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002233
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002234 // Add this entry to the let stack.
Matthias Braunc66e7552016-12-05 06:41:54 +00002235 SmallVector<LetRecord, 8> LetInfo;
2236 ParseLetList(LetInfo);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002237 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002238 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002239
2240 if (Lex.getCode() != tgtok::In)
2241 return TokError("expected 'in' at end of top-level 'let'");
2242 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002243
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002244 // If this is a scalar let, just handle it now
2245 if (Lex.getCode() != tgtok::l_brace) {
2246 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002247 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002248 return true;
2249 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002250 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002251 // Otherwise, this is a group let.
2252 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002253
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002254 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002255 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002256 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002257
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002258 if (Lex.getCode() != tgtok::r_brace) {
2259 TokError("expected '}' at end of top level let command");
2260 return Error(BraceLoc, "to match this '{'");
2261 }
2262 Lex.Lex();
2263 }
Bob Wilson7248f862009-11-22 04:24:42 +00002264
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002265 // Outside this let scope, this let block is not active.
2266 LetStack.pop_back();
2267 return false;
2268}
2269
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002270/// ParseMultiClass - Parse a multiclass definition.
2271///
Bob Wilson3d948162009-04-28 19:41:44 +00002272/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002273/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2274/// MultiClassObject ::= DefInst
2275/// MultiClassObject ::= MultiClassInst
2276/// MultiClassObject ::= DefMInst
2277/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2278/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002279///
2280bool TGParser::ParseMultiClass() {
2281 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2282 Lex.Lex(); // Eat the multiclass token.
2283
2284 if (Lex.getCode() != tgtok::Id)
2285 return TokError("expected identifier after multiclass for name");
2286 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002287
Craig Topper7adf2bf2014-12-11 05:25:30 +00002288 auto Result =
2289 MultiClasses.insert(std::make_pair(Name,
2290 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2291
2292 if (!Result.second)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002293 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002294
Craig Topper7adf2bf2014-12-11 05:25:30 +00002295 CurMultiClass = Result.first->second.get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002296 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002297
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002298 // If there are template args, parse them.
2299 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002300 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002301 return true;
2302
David Greene7049e792009-04-24 16:55:41 +00002303 bool inherits = false;
2304
David Greene753ed8f2009-04-22 16:42:54 +00002305 // If there are submulticlasses, parse them.
2306 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002307 inherits = true;
2308
David Greene753ed8f2009-04-22 16:42:54 +00002309 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002310
David Greene753ed8f2009-04-22 16:42:54 +00002311 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002312 SubMultiClassReference SubMultiClass =
2313 ParseSubMultiClassReference(CurMultiClass);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002314 while (true) {
David Greene753ed8f2009-04-22 16:42:54 +00002315 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002316 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002317
David Greene753ed8f2009-04-22 16:42:54 +00002318 // Add it.
2319 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2320 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002321
David Greene753ed8f2009-04-22 16:42:54 +00002322 if (Lex.getCode() != tgtok::comma) break;
2323 Lex.Lex(); // eat ','.
2324 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2325 }
2326 }
2327
David Greene7049e792009-04-24 16:55:41 +00002328 if (Lex.getCode() != tgtok::l_brace) {
2329 if (!inherits)
2330 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002331 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002332 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002333 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002334 } else {
David Greene7049e792009-04-24 16:55:41 +00002335 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2336 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002337
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002338 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002339 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002340 default:
2341 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
2342 case tgtok::Let:
2343 case tgtok::Def:
2344 case tgtok::Defm:
2345 case tgtok::Foreach:
2346 if (ParseObject(CurMultiClass))
2347 return true;
2348 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002349 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002350 }
David Greene7049e792009-04-24 16:55:41 +00002351 Lex.Lex(); // eat the '}'.
2352 }
Bob Wilson7248f862009-11-22 04:24:42 +00002353
Craig Topper011817a2014-04-09 04:50:04 +00002354 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002355 return false;
2356}
2357
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002358Record *TGParser::InstantiateMulticlassDef(MultiClass &MC, Record *DefProto,
2359 Init *&DefmPrefix,
2360 SMRange DefmPrefixRange,
2361 ArrayRef<Init *> TArgs,
Matthias Braunc66e7552016-12-05 06:41:54 +00002362 ArrayRef<Init *> TemplateVals) {
David Greene5d5d88c2011-10-19 13:04:31 +00002363 // We need to preserve DefProto so it can be reused for later
2364 // instantiations, so create a new Record to inherit from it.
2365
David Greenedb445972011-10-05 22:42:07 +00002366 // Add in the defm name. If the defm prefix is empty, give each
2367 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2368 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2369 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002370
Jordan Roseabdd99b2013-01-10 18:50:05 +00002371 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002372 if (!DefmPrefix) {
Craig Topperf4412262017-06-01 06:56:16 +00002373 DefmPrefix = GetNewAnonymousName();
Jordan Roseabdd99b2013-01-10 18:50:05 +00002374 IsAnonymous = true;
2375 }
David Greene5d5d88c2011-10-19 13:04:31 +00002376
2377 Init *DefName = DefProto->getNameInit();
Sean Silvafb509ed2012-10-10 20:24:43 +00002378 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002379
Craig Topper011817a2014-04-09 04:50:04 +00002380 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002381 // We have a fully expanded string so there are no operators to
2382 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002383 DefName =
2384 BinOpInit::get(BinOpInit::STRCONCAT,
2385 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2386 StringRecTy::get())->Fold(DefProto, &MC),
2387 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2388 }
David Greene5d5d88c2011-10-19 13:04:31 +00002389
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002390 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002391 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002392 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Craig Topper84138712014-11-29 05:31:10 +00002393 auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002394
2395 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002396 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002397 Ref.Rec = DefProto;
Craig Topper84138712014-11-29 05:31:10 +00002398 AddSubClass(CurRec.get(), Ref);
David Greenedb445972011-10-05 22:42:07 +00002399
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002400 // Set the value for NAME. We don't resolve references to it 'til later,
2401 // though, so that uses in nested multiclass names don't get
2402 // confused.
Matthias Braun215ff842016-12-05 07:35:13 +00002403 if (SetValue(CurRec.get(), Ref.RefRange.Start, StringInit::get("NAME"), None,
2404 DefmPrefix, /*AllowSelfAssignment*/true)) {
Craig Topper85c07002015-04-30 05:54:22 +00002405 Error(DefmPrefixRange.Start, "Could not resolve " +
2406 CurRec->getNameInitAsString() + ":NAME to '" +
2407 DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002408 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002409 }
David Greene8bf0d722011-10-19 13:04:35 +00002410
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002411 // If the DefNameString didn't resolve, we probably have a reference to
2412 // NAME and need to replace it. We need to do at least this much greedily,
2413 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002414 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002415 RecordVal *DefNameRV = CurRec->getValue("NAME");
2416 CurRec->resolveReferencesTo(DefNameRV);
2417 }
2418
2419 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002420 // Now that we're at the top level, resolve all NAME references
2421 // in the resultant defs that weren't in the def names themselves.
2422 RecordVal *DefNameRV = CurRec->getValue("NAME");
2423 CurRec->resolveReferencesTo(DefNameRV);
2424
Hal Finkeld2497362015-05-21 04:32:56 +00002425 // Check if the name is a complex pattern.
2426 // If so, resolve it.
2427 DefName = CurRec->getNameInit();
2428 DefNameString = dyn_cast<StringInit>(DefName);
2429
2430 // OK the pattern is more complex than simply using NAME.
2431 // Let's use the heavy weaponery.
2432 if (!DefNameString) {
2433 ResolveMulticlassDefArgs(MC, CurRec.get(), DefmPrefixRange.Start,
2434 Lex.getLoc(), TArgs, TemplateVals,
2435 false/*Delete args*/);
2436 DefName = CurRec->getNameInit();
2437 DefNameString = dyn_cast<StringInit>(DefName);
2438
2439 if (!DefNameString)
2440 DefName = DefName->convertInitializerTo(StringRecTy::get());
2441
2442 // We ran out of options here...
2443 DefNameString = dyn_cast<StringInit>(DefName);
2444 if (!DefNameString) {
2445 PrintFatalError(CurRec->getLoc()[CurRec->getLoc().size() - 1],
2446 DefName->getAsUnquotedString() + " is not a string.");
2447 return nullptr;
2448 }
2449
2450 CurRec->setName(DefName);
2451 }
2452
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002453 // Now that NAME references are resolved and we're at the top level of
2454 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002455 // currently in a multiclass, it means this defm appears inside a
2456 // multiclass and its name won't be fully resolvable until we see
Hal Finkeld2497362015-05-21 04:32:56 +00002457 // the top-level defm. Therefore, we don't add this to the
2458 // RecordKeeper at this point. If we did we could get duplicate
David Greene8bf0d722011-10-19 13:04:35 +00002459 // defs as more than one probably refers to NAME or some other
2460 // common internal placeholder.
2461
2462 // Ensure redefinition doesn't happen.
2463 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002464 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002465 "' already defined, instantiating defm with subdef '" +
2466 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002467 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002468 }
2469
Craig Topper84138712014-11-29 05:31:10 +00002470 Record *CurRecSave = CurRec.get(); // Keep a copy before we release.
Craig Toppercdab2322014-11-29 05:52:51 +00002471 Records.addDef(std::move(CurRec));
Craig Topper84138712014-11-29 05:31:10 +00002472 return CurRecSave;
David Greene8bf0d722011-10-19 13:04:35 +00002473 }
2474
Craig Topper84138712014-11-29 05:31:10 +00002475 // FIXME This is bad but the ownership transfer to caller is pretty messy.
2476 // The unique_ptr in this function at least protects the exits above.
2477 return CurRec.release();
David Greenedb445972011-10-05 22:42:07 +00002478}
2479
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002480bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC, Record *CurRec,
2481 SMLoc DefmPrefixLoc, SMLoc SubClassLoc,
2482 ArrayRef<Init *> TArgs,
Matthias Braunc66e7552016-12-05 06:41:54 +00002483 ArrayRef<Init *> TemplateVals,
David Greenedb445972011-10-05 22:42:07 +00002484 bool DeleteArgs) {
2485 // Loop over all of the template arguments, setting them to the specified
2486 // value or leaving them as the default if necessary.
2487 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2488 // Check if a value is specified for this temp-arg.
2489 if (i < TemplateVals.size()) {
2490 // Set it now.
Craig Toppercfd81732016-01-04 03:15:08 +00002491 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], None, TemplateVals[i]))
David Greenedb445972011-10-05 22:42:07 +00002492 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002493
David Greenedb445972011-10-05 22:42:07 +00002494 // Resolve it next.
2495 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2496
2497 if (DeleteArgs)
2498 // Now remove it.
2499 CurRec->removeValue(TArgs[i]);
Craig Toppera9642b42015-05-04 01:35:39 +00002500
David Greenedb445972011-10-05 22:42:07 +00002501 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Craig Topper85c07002015-04-30 05:54:22 +00002502 return Error(SubClassLoc, "value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00002503 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +00002504 ") of multiclassclass '" + MC.Rec.getNameInitAsString() +
2505 "'");
David Greenedb445972011-10-05 22:42:07 +00002506 }
2507 }
2508 return false;
2509}
2510
2511bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2512 Record *CurRec,
2513 Record *DefProto,
2514 SMLoc DefmPrefixLoc) {
2515 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002516 if (ApplyLetStack(CurRec))
2517 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002518
David Greenedb445972011-10-05 22:42:07 +00002519 // Don't create a top level definition for defm inside multiclasses,
2520 // instead, only update the prototypes and bind the template args
2521 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002522 if (!CurMultiClass)
2523 return false;
Craig Toppereb4d7c62015-04-29 04:43:36 +00002524 for (const auto &Proto : CurMultiClass->DefPrototypes)
2525 if (Proto->getNameInit() == CurRec->getNameInit())
Sean Silvacc951b22013-01-09 05:28:12 +00002526 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2527 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002528 CurMultiClass->DefPrototypes.push_back(std::unique_ptr<Record>(CurRec));
David Greenedb445972011-10-05 22:42:07 +00002529
Sean Silvacc951b22013-01-09 05:28:12 +00002530 // Copy the template arguments for the multiclass into the new def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002531 for (Init * TA : CurMultiClass->Rec.getTemplateArgs()) {
2532 const RecordVal *RV = CurMultiClass->Rec.getValue(TA);
Sean Silvacc951b22013-01-09 05:28:12 +00002533 assert(RV && "Template arg doesn't exist?");
2534 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002535 }
2536
2537 return false;
2538}
2539
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002540/// ParseDefm - Parse the instantiation of a multiclass.
2541///
2542/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2543///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002544bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002545 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002546 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002547 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002548
Craig Topperb21afc62013-01-07 05:09:33 +00002549 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002550 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002551 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002552
Jordan Rosef12e8a92013-01-10 18:50:11 +00002553 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002554 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002555 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002556
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002557 // Keep track of the new generated record definitions.
2558 std::vector<Record*> NewRecDefs;
2559
2560 // This record also inherits from a regular class (non-multiclass)?
2561 bool InheritFromClass = false;
2562
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002563 // eat the colon.
2564 Lex.Lex();
2565
Chris Lattner526c8cb2009-06-21 03:39:35 +00002566 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002567 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002568
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002569 while (true) {
Craig Topper011817a2014-04-09 04:50:04 +00002570 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002571
2572 // To instantiate a multiclass, we need to first get the multiclass, then
2573 // instantiate each def contained in the multiclass with the SubClassRef
2574 // template parameters.
Craig Topper7adf2bf2014-12-11 05:25:30 +00002575 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper4ca40012014-11-30 01:20:17 +00002576 assert(MC && "Didn't lookup multiclass correctly?");
Matthias Braunc66e7552016-12-05 06:41:54 +00002577 ArrayRef<Init*> TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002578
2579 // Verify that the correct number of template arguments were specified.
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002580 ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002581 if (TArgs.size() < TemplateVals.size())
2582 return Error(SubClassLoc,
2583 "more template args specified than multiclass expects");
2584
2585 // Loop over all the def's in the multiclass, instantiating each one.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002586 for (const std::unique_ptr<Record> &DefProto : MC->DefPrototypes) {
Hal Finkeld2497362015-05-21 04:32:56 +00002587 // The record name construction goes as follow:
2588 // - If the def name is a string, prepend the prefix.
2589 // - If the def name is a more complex pattern, use that pattern.
Craig Topper7f36be92016-02-26 06:50:27 +00002590 // As a result, the record is instantiated before resolving
Hal Finkeld2497362015-05-21 04:32:56 +00002591 // arguments, as it would make its name a string.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002592 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto.get(), DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002593 SMRange(DefmLoc,
Hal Finkeld2497362015-05-21 04:32:56 +00002594 DefmPrefixEndLoc),
2595 TArgs, TemplateVals);
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002596 if (!CurRec)
2597 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002598
Craig Topper7f36be92016-02-26 06:50:27 +00002599 // Now that the record is instantiated, we can resolve arguments.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002600 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002601 TArgs, TemplateVals, true/*Delete args*/))
2602 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002603
Craig Toppereb4d7c62015-04-29 04:43:36 +00002604 if (ResolveMulticlassDef(*MC, CurRec, DefProto.get(), DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002605 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002606
Adam Nemete5a07162014-09-16 17:14:13 +00002607 // Defs that can be used by other definitions should be fully resolved
2608 // before any use.
2609 if (DefProto->isResolveFirst() && !CurMultiClass) {
2610 CurRec->resolveReferences();
2611 CurRec->setResolveFirst(false);
2612 }
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002613 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002614 }
2615
David Greenedb445972011-10-05 22:42:07 +00002616
David Greenef00919a2009-04-22 22:17:51 +00002617 if (Lex.getCode() != tgtok::comma) break;
2618 Lex.Lex(); // eat ','.
2619
Craig Topper998a39a2013-08-20 04:22:09 +00002620 if (Lex.getCode() != tgtok::Id)
2621 return TokError("expected identifier");
2622
David Greenef00919a2009-04-22 22:17:51 +00002623 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002624
2625 // A defm can inherit from regular classes (non-multiclass) as
2626 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002627 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002628
2629 if (InheritFromClass)
2630 break;
2631
Craig Topper011817a2014-04-09 04:50:04 +00002632 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002633 }
2634
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002635 if (InheritFromClass) {
2636 // Process all the classes to inherit as if they were part of a
2637 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002638 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002639 while (true) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002640 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002641 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002642
2643 // Get the expanded definition prototypes and teach them about
2644 // the record values the current class to inherit has
Craig Toppereb4d7c62015-04-29 04:43:36 +00002645 for (Record *CurRec : NewRecDefs) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002646 // Add it.
2647 if (AddSubClass(CurRec, SubClass))
2648 return true;
2649
Sean Silvacb1a75e2013-01-09 04:49:14 +00002650 if (ApplyLetStack(CurRec))
2651 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002652 }
2653
2654 if (Lex.getCode() != tgtok::comma) break;
2655 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002656 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002657 }
2658 }
2659
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002660 if (!CurMultiClass)
Craig Toppereb4d7c62015-04-29 04:43:36 +00002661 for (Record *CurRec : NewRecDefs)
David Greene50c09122011-08-10 18:27:46 +00002662 // See Record::setName(). This resolve step will see any new
2663 // name for the def that might have been created when resolving
2664 // inheritance, values and arguments above.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002665 CurRec->resolveReferences();
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002666
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002667 if (Lex.getCode() != tgtok::semi)
2668 return TokError("expected ';' at end of defm");
2669 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002670
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002671 return false;
2672}
2673
2674/// ParseObject
2675/// Object ::= ClassInst
2676/// Object ::= DefInst
2677/// Object ::= MultiClassInst
2678/// Object ::= DefMInst
2679/// Object ::= LETCommand '{' ObjectList '}'
2680/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002681bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002682 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002683 default:
2684 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002685 case tgtok::Let: return ParseTopLevelLet(MC);
2686 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002687 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002688 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002689 case tgtok::Class: return ParseClass();
2690 case tgtok::MultiClass: return ParseMultiClass();
2691 }
2692}
2693
2694/// ParseObjectList
2695/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002696bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002697 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002698 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002699 return true;
2700 }
2701 return false;
2702}
2703
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002704bool TGParser::ParseFile() {
2705 Lex.Lex(); // Prime the lexer.
2706 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002707
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002708 // If we have unread input at the end of the file, report it.
2709 if (Lex.getCode() == tgtok::Eof)
2710 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002711
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002712 return TokError("Unexpected input at top level");
2713}