blob: 4dd05fd3297911df29b28d369a2e8ab9ff0a46b7 [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.
Nicolai Haehnleaecb68b2018-02-23 10:46:13 +0000201 CurRec->addSuperClass(SC, SubClass.RefRange);
202
Craig Topper0e41d0b2016-01-18 19:52:37 +0000203 ArrayRef<std::pair<Record *, SMRange>> SCs = SC->getSuperClasses();
204 for (const auto &SCPair : SCs) {
205 if (CurRec->isSubClassOf(SCPair.first))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000206 return Error(SubClass.RefRange.Start,
Craig Topper0e41d0b2016-01-18 19:52:37 +0000207 "Already subclass of '" + SCPair.first->getName() + "'!\n");
208 CurRec->addSuperClass(SCPair.first, SCPair.second);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000209 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000210 return false;
211}
212
David Greene753ed8f2009-04-22 16:42:54 +0000213/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilsonf71e6562009-04-30 18:26:19 +0000214/// CurMC, resolving its template args as SubMultiClass's
David Greene753ed8f2009-04-22 16:42:54 +0000215/// template arguments.
Bob Wilsonf71e6562009-04-30 18:26:19 +0000216bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson92ab8202009-04-30 17:46:20 +0000217 SubMultiClassReference &SubMultiClass) {
David Greene753ed8f2009-04-22 16:42:54 +0000218 MultiClass *SMC = SubMultiClass.MC;
Bob Wilsonf71e6562009-04-30 18:26:19 +0000219 Record *CurRec = &CurMC->Rec;
David Greene753ed8f2009-04-22 16:42:54 +0000220
David Greene753ed8f2009-04-22 16:42:54 +0000221 // Add all of the values in the subclass into the current class.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000222 for (const auto &SMCVal : SMC->Rec.getValues())
223 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000224 return true;
225
Craig Toppera4ea4b02014-11-30 00:24:32 +0000226 unsigned newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000227
David Greene753ed8f2009-04-22 16:42:54 +0000228 // Add all of the defs in the subclass into the current multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000229 for (const std::unique_ptr<Record> &R : SMC->DefPrototypes) {
David Greene753ed8f2009-04-22 16:42:54 +0000230 // Clone the def and add it to the current multiclass
Craig Toppereb4d7c62015-04-29 04:43:36 +0000231 auto NewDef = make_unique<Record>(*R);
David Greene753ed8f2009-04-22 16:42:54 +0000232
233 // Add all of the values in the superclass into the current def.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000234 for (const auto &MCVal : CurRec->getValues())
235 if (AddValue(NewDef.get(), SubMultiClass.RefRange.Start, MCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000236 return true;
237
Craig Topperc3504c42014-12-11 05:25:33 +0000238 CurMC->DefPrototypes.push_back(std::move(NewDef));
David Greene753ed8f2009-04-22 16:42:54 +0000239 }
Bob Wilson3d948162009-04-28 19:41:44 +0000240
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000241 ArrayRef<Init *> SMCTArgs = SMC->Rec.getTemplateArgs();
David Greene753ed8f2009-04-22 16:42:54 +0000242
David Greene7049e792009-04-24 16:55:41 +0000243 // Ensure that an appropriate number of template arguments are
244 // specified.
David Greene753ed8f2009-04-22 16:42:54 +0000245 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000246 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000247 "More template args specified than expected");
Bob Wilson3d948162009-04-28 19:41:44 +0000248
David Greene753ed8f2009-04-22 16:42:54 +0000249 // Loop over all of the template arguments, setting them to the specified
250 // value or leaving them as the default if necessary.
251 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
252 if (i < SubMultiClass.TemplateArgs.size()) {
David Greene7049e792009-04-24 16:55:41 +0000253 // If a value is specified for this template arg, set it in the
254 // superclass now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000255 if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000256 None, SubMultiClass.TemplateArgs[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000257 return true;
258
259 // Resolve it next.
260 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson3d948162009-04-28 19:41:44 +0000261
David Greene753ed8f2009-04-22 16:42:54 +0000262 // Now remove it.
263 CurRec->removeValue(SMCTArgs[i]);
264
David Greene7049e792009-04-24 16:55:41 +0000265 // If a value is specified for this template arg, set it in the
266 // new defs now.
Craig Topperc3504c42014-12-11 05:25:33 +0000267 for (const auto &Def :
268 makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
269 if (SetValue(Def.get(), SubMultiClass.RefRange.Start, SMCTArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000270 None, SubMultiClass.TemplateArgs[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000271 return true;
272
273 // Resolve it next.
274 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
275
276 // Now remove it
277 Def->removeValue(SMCTArgs[i]);
278 }
279 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000280 return Error(SubMultiClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000281 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000282 Twine(i) + " (" + SMCTArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000283 ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000284 }
285 }
286
287 return false;
288}
289
David Greenefb927af2012-02-22 16:09:41 +0000290/// ProcessForeachDefs - Given a record, apply all of the variable
291/// values in all surrounding foreach loops, creating new records for
292/// each combination of values.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000293bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
294 if (Loops.empty())
295 return false;
296
David Greenefb927af2012-02-22 16:09:41 +0000297 // We want to instantiate a new copy of CurRec for each combination
298 // of nested loop iterator values. We don't want top instantiate
299 // any copies until we have values for each loop iterator.
300 IterSet IterVals;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000301 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000302}
303
304/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
305/// apply each of the variable values in this loop and then process
306/// subloops.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000307bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
308 // Recursively build a tuple of iterator values.
309 if (IterVals.size() != Loops.size()) {
310 assert(IterVals.size() < Loops.size());
311 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silvafb509ed2012-10-10 20:24:43 +0000312 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Craig Topper011817a2014-04-09 04:50:04 +0000313 if (!List) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000314 Error(Loc, "Loop list is not a list");
315 return true;
316 }
David Greenefb927af2012-02-22 16:09:41 +0000317
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000318 // Process each value.
Craig Topper664f6a02015-06-02 04:15:57 +0000319 for (unsigned i = 0; i < List->size(); ++i) {
Nicolai Haehnlec7711ba2018-02-23 10:46:21 +0000320 Init *ItemVal = List->getElement(i)->resolveReferences(*CurRec, nullptr);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000321 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
322 if (ProcessForeachDefs(CurRec, Loc, IterVals))
323 return true;
324 IterVals.pop_back();
325 }
326 return false;
327 }
328
329 // This is the bottom of the recursion. We have all of the iterator values
330 // for this point in the iteration space. Instantiate a new record to
331 // reflect this combination of values.
Craig Topper84138712014-11-29 05:31:10 +0000332 auto IterRec = make_unique<Record>(*CurRec);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000333
334 // Set the iterator values now.
Craig Topper8eb764c2015-06-02 06:19:28 +0000335 for (IterRecord &IR : IterVals) {
336 VarInit *IterVar = IR.IterVar;
337 TypedInit *IVal = dyn_cast<TypedInit>(IR.IterValue);
Craig Topper84138712014-11-29 05:31:10 +0000338 if (!IVal)
339 return Error(Loc, "foreach iterator value is untyped");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000340
Craig Topperf4412262017-06-01 06:56:16 +0000341 IterRec->addValue(RecordVal(IterVar->getNameInit(), IVal->getType(), false));
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000342
Matthias Braun215ff842016-12-05 07:35:13 +0000343 if (SetValue(IterRec.get(), Loc, IterVar->getNameInit(), None, IVal))
Craig Topper84138712014-11-29 05:31:10 +0000344 return Error(Loc, "when instantiating this def");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000345
346 // Resolve it next.
Matthias Braun215ff842016-12-05 07:35:13 +0000347 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getNameInit()));
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000348
349 // Remove it.
Matthias Braun215ff842016-12-05 07:35:13 +0000350 IterRec->removeValue(IterVar->getNameInit());
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000351 }
352
353 if (Records.getDef(IterRec->getNameInitAsString())) {
Artyom Skrobov8b985322014-06-10 12:41:14 +0000354 // If this record is anonymous, it's no problem, just generate a new name
Craig Topper84138712014-11-29 05:31:10 +0000355 if (!IterRec->isAnonymous())
356 return Error(Loc, "def already exists: " +IterRec->getNameInitAsString());
357
358 IterRec->setName(GetNewAnonymousName());
David Greenefb927af2012-02-22 16:09:41 +0000359 }
360
Craig Topper84138712014-11-29 05:31:10 +0000361 Record *IterRecSave = IterRec.get(); // Keep a copy before release.
Craig Toppercdab2322014-11-29 05:52:51 +0000362 Records.addDef(std::move(IterRec));
Craig Topper84138712014-11-29 05:31:10 +0000363 IterRecSave->resolveReferences();
David Greenefb927af2012-02-22 16:09:41 +0000364 return false;
365}
366
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000367//===----------------------------------------------------------------------===//
368// Parser Code
369//===----------------------------------------------------------------------===//
370
371/// isObjectStart - Return true if this is a valid first token for an Object.
372static bool isObjectStart(tgtok::TokKind K) {
373 return K == tgtok::Class || K == tgtok::Def ||
David Greenefb927af2012-02-22 16:09:41 +0000374 K == tgtok::Defm || K == tgtok::Let ||
375 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000376}
377
Alp Tokerce91fe52013-12-21 18:51:00 +0000378/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
379/// an identifier.
Craig Topperf4412262017-06-01 06:56:16 +0000380Init *TGParser::GetNewAnonymousName() {
381 return StringInit::get("anonymous_" + utostr(AnonCounter++));
Chris Lattner7538ed82010-10-05 22:51:56 +0000382}
383
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000384/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000385/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000386/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000387/// ObjectName ::= /*empty*/
388///
David Greene2affd672011-10-19 13:04:29 +0000389Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
390 switch (Lex.getCode()) {
391 case tgtok::colon:
392 case tgtok::semi:
393 case tgtok::l_brace:
394 // These are all of the tokens that can begin an object body.
395 // Some of these can also begin values but we disallow those cases
396 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000397 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000398 default:
399 break;
400 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000401
Craig Topper011817a2014-04-09 04:50:04 +0000402 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000403 if (CurMultiClass)
404 CurRec = &CurMultiClass->Rec;
405
Craig Topper011817a2014-04-09 04:50:04 +0000406 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000407 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000408 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000409 if (!CurRecName) {
410 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000411 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000412 }
413 Type = CurRecName->getType();
414 }
415
416 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000417}
418
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000419/// ParseClassID - Parse and resolve a reference to a class name. This returns
420/// null on error.
421///
422/// ClassID ::= ID
423///
424Record *TGParser::ParseClassID() {
425 if (Lex.getCode() != tgtok::Id) {
426 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000427 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000428 }
Bob Wilson7248f862009-11-22 04:24:42 +0000429
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000430 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000431 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000432 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000433
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000434 Lex.Lex();
435 return Result;
436}
437
Bob Wilson3d948162009-04-28 19:41:44 +0000438/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
439/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000440///
441/// MultiClassID ::= ID
442///
443MultiClass *TGParser::ParseMultiClassID() {
444 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000445 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000446 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000447 }
Bob Wilson3d948162009-04-28 19:41:44 +0000448
Craig Topper7adf2bf2014-12-11 05:25:30 +0000449 MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
Craig Topper4ca40012014-11-30 01:20:17 +0000450 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000451 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000452
David Greene753ed8f2009-04-22 16:42:54 +0000453 Lex.Lex();
Craig Topper4ca40012014-11-30 01:20:17 +0000454 return Result;
David Greene753ed8f2009-04-22 16:42:54 +0000455}
456
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000457/// ParseSubClassReference - Parse a reference to a subclass or to a templated
458/// subclass. This returns a SubClassRefTy with a null Record* on error.
459///
460/// SubClassRef ::= ClassID
461/// SubClassRef ::= ClassID '<' ValueList '>'
462///
463SubClassReference TGParser::
464ParseSubClassReference(Record *CurRec, bool isDefm) {
465 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000466 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000467
Sean Silva0657b402013-01-09 02:17:14 +0000468 if (isDefm) {
469 if (MultiClass *MC = ParseMultiClassID())
470 Result.Rec = &MC->Rec;
471 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000472 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000473 }
Craig Topper011817a2014-04-09 04:50:04 +0000474 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000475
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000476 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000477 if (Lex.getCode() != tgtok::less) {
478 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000479 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000480 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000481 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000482
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000483 if (Lex.getCode() == tgtok::greater) {
484 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000485 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000486 return Result;
487 }
Bob Wilson7248f862009-11-22 04:24:42 +0000488
Matthias Braunc66e7552016-12-05 06:41:54 +0000489 ParseValueList(Result.TemplateArgs, CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000490 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000491 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000492 return Result;
493 }
Bob Wilson7248f862009-11-22 04:24:42 +0000494
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000495 if (Lex.getCode() != tgtok::greater) {
496 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000497 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000498 return Result;
499 }
500 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000501 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000502
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000503 return Result;
504}
505
Bob Wilson3d948162009-04-28 19:41:44 +0000506/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
507/// templated submulticlass. This returns a SubMultiClassRefTy with a null
508/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000509///
510/// SubMultiClassRef ::= MultiClassID
511/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
512///
513SubMultiClassReference TGParser::
514ParseSubMultiClassReference(MultiClass *CurMC) {
515 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000516 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000517
David Greene753ed8f2009-04-22 16:42:54 +0000518 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000519 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000520
David Greene753ed8f2009-04-22 16:42:54 +0000521 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000522 if (Lex.getCode() != tgtok::less) {
523 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000524 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000525 }
David Greene753ed8f2009-04-22 16:42:54 +0000526 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000527
David Greene753ed8f2009-04-22 16:42:54 +0000528 if (Lex.getCode() == tgtok::greater) {
529 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000530 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000531 return Result;
532 }
Bob Wilson3d948162009-04-28 19:41:44 +0000533
Matthias Braunc66e7552016-12-05 06:41:54 +0000534 ParseValueList(Result.TemplateArgs, &CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000535 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000536 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000537 return Result;
538 }
Bob Wilson3d948162009-04-28 19:41:44 +0000539
David Greene753ed8f2009-04-22 16:42:54 +0000540 if (Lex.getCode() != tgtok::greater) {
541 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000542 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000543 return Result;
544 }
545 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000546 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000547
548 return Result;
549}
550
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000551/// ParseRangePiece - Parse a bit/value range.
552/// RangePiece ::= INTVAL
553/// RangePiece ::= INTVAL '-' INTVAL
554/// RangePiece ::= INTVAL INTVAL
Matthias Braunc66e7552016-12-05 06:41:54 +0000555bool TGParser::ParseRangePiece(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000556 if (Lex.getCode() != tgtok::IntVal) {
557 TokError("expected integer or bitrange");
558 return true;
559 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000560 int64_t Start = Lex.getCurIntVal();
561 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000562
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000563 if (Start < 0)
564 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000565
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000566 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000567 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000568 Ranges.push_back(Start);
569 return false;
570 case tgtok::minus:
571 if (Lex.Lex() != tgtok::IntVal) {
572 TokError("expected integer value as end of range");
573 return true;
574 }
575 End = Lex.getCurIntVal();
576 break;
577 case tgtok::IntVal:
578 End = -Lex.getCurIntVal();
579 break;
580 }
Bob Wilson7248f862009-11-22 04:24:42 +0000581 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000582 return TokError("invalid range, cannot be negative");
583 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000584
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000585 // Add to the range.
Craig Toppera9642b42015-05-04 01:35:39 +0000586 if (Start < End)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000587 for (; Start <= End; ++Start)
588 Ranges.push_back(Start);
Craig Toppera9642b42015-05-04 01:35:39 +0000589 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000590 for (; Start >= End; --Start)
591 Ranges.push_back(Start);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000592 return false;
593}
594
595/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
596///
597/// RangeList ::= RangePiece (',' RangePiece)*
598///
Matthias Braunc66e7552016-12-05 06:41:54 +0000599void TGParser::ParseRangeList(SmallVectorImpl<unsigned> &Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000600 // Parse the first piece.
Matthias Braunc66e7552016-12-05 06:41:54 +0000601 if (ParseRangePiece(Result)) {
602 Result.clear();
603 return;
604 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000605 while (Lex.getCode() == tgtok::comma) {
606 Lex.Lex(); // Eat the comma.
607
608 // Parse the next range piece.
Matthias Braunc66e7552016-12-05 06:41:54 +0000609 if (ParseRangePiece(Result)) {
610 Result.clear();
611 return;
612 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000613 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000614}
615
616/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
617/// OptionalRangeList ::= '<' RangeList '>'
618/// OptionalRangeList ::= /*empty*/
Matthias Braunc66e7552016-12-05 06:41:54 +0000619bool TGParser::ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000620 if (Lex.getCode() != tgtok::less)
621 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000622
Chris Lattner526c8cb2009-06-21 03:39:35 +0000623 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000624 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000625
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000626 // Parse the range list.
Matthias Braunc66e7552016-12-05 06:41:54 +0000627 ParseRangeList(Ranges);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000628 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000629
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000630 if (Lex.getCode() != tgtok::greater) {
631 TokError("expected '>' at end of range list");
632 return Error(StartLoc, "to match this '<'");
633 }
634 Lex.Lex(); // eat the '>'.
635 return false;
636}
637
638/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
639/// OptionalBitList ::= '{' RangeList '}'
640/// OptionalBitList ::= /*empty*/
Matthias Braunc66e7552016-12-05 06:41:54 +0000641bool TGParser::ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000642 if (Lex.getCode() != tgtok::l_brace)
643 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000644
Chris Lattner526c8cb2009-06-21 03:39:35 +0000645 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000646 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000647
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000648 // Parse the range list.
Matthias Braunc66e7552016-12-05 06:41:54 +0000649 ParseRangeList(Ranges);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000650 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000651
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000652 if (Lex.getCode() != tgtok::r_brace) {
653 TokError("expected '}' at end of bit list");
654 return Error(StartLoc, "to match this '{'");
655 }
656 Lex.Lex(); // eat the '}'.
657 return false;
658}
659
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000660/// ParseType - Parse and return a tblgen type. This returns null on error.
661///
662/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000663/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000664/// Type ::= BIT // bit type
665/// Type ::= BITS '<' INTVAL '>' // bits<x> type
666/// Type ::= INT // int type
667/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000668/// Type ::= DAG // dag type
669/// Type ::= ClassID // Record Type
670///
671RecTy *TGParser::ParseType() {
672 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000673 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000674 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Tim Northover88403d72016-07-05 21:22:55 +0000675 case tgtok::Code: Lex.Lex(); return CodeRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000676 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
677 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000678 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000679 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000680 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000681 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000682 case tgtok::Bits: {
683 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
684 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000685 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000686 }
687 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
688 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000689 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000690 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000691 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000692 if (Lex.Lex() != tgtok::greater) { // Eat count.
693 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000694 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000695 }
696 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000697 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000698 }
699 case tgtok::List: {
700 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
701 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000702 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000703 }
704 Lex.Lex(); // Eat '<'
705 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000706 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000707
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000708 if (Lex.getCode() != tgtok::greater) {
709 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000710 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000711 }
712 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000713 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000714 }
Bob Wilson7248f862009-11-22 04:24:42 +0000715 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000716}
717
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000718/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
719/// has already been read.
Matthias Braun215ff842016-12-05 07:35:13 +0000720Init *TGParser::ParseIDValue(Record *CurRec, StringInit *Name, SMLoc NameLoc,
David Greened4263a62011-10-19 13:04:20 +0000721 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000722 if (CurRec) {
723 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000724 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000725
Matthias Braun215ff842016-12-05 07:35:13 +0000726 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
David Greenedb10e692011-10-19 13:02:42 +0000727
David Greene47a665e2011-10-05 22:42:54 +0000728 if (CurMultiClass)
Matthias Braun215ff842016-12-05 07:35:13 +0000729 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
David Greenedb10e692011-10-19 13:02:42 +0000730 "::");
David Greene47a665e2011-10-05 22:42:54 +0000731
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000732 if (CurRec->isTemplateArg(TemplateArgName)) {
733 const RecordVal *RV = CurRec->getValue(TemplateArgName);
734 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000735 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000736 }
Matthias Braun215ff842016-12-05 07:35:13 +0000737 }
Bob Wilson7248f862009-11-22 04:24:42 +0000738
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000739 if (CurMultiClass) {
Matthias Braun215ff842016-12-05 07:35:13 +0000740 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name, "::");
David Greenedb10e692011-10-19 13:02:42 +0000741
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000742 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
743 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
744 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000745 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000746 }
747 }
Bob Wilson7248f862009-11-22 04:24:42 +0000748
David Greenefb927af2012-02-22 16:09:41 +0000749 // If this is in a foreach loop, make sure it's not a loop iterator
Craig Toppereb4d7c62015-04-29 04:43:36 +0000750 for (const auto &L : Loops) {
751 VarInit *IterVar = dyn_cast<VarInit>(L.IterVar);
Matthias Braun215ff842016-12-05 07:35:13 +0000752 if (IterVar && IterVar->getNameInit() == Name)
David Greenefb927af2012-02-22 16:09:41 +0000753 return IterVar;
754 }
755
David Greene232bd602011-10-19 13:04:21 +0000756 if (Mode == ParseNameMode)
Matthias Braun215ff842016-12-05 07:35:13 +0000757 return Name;
David Greene232bd602011-10-19 13:04:21 +0000758
Matthias Braun215ff842016-12-05 07:35:13 +0000759 if (Record *D = Records.getDef(Name->getValue()))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000760 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000761
David Greene232bd602011-10-19 13:04:21 +0000762 if (Mode == ParseValueMode) {
Matthias Braun215ff842016-12-05 07:35:13 +0000763 Error(NameLoc, "Variable not defined: '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000764 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000765 }
Craig Toppera9642b42015-05-04 01:35:39 +0000766
Matthias Braun215ff842016-12-05 07:35:13 +0000767 return Name;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000768}
769
David Greene5d0c0512009-05-14 20:54:48 +0000770/// ParseOperation - Parse an operator. This returns null on error.
771///
772/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
773///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000774Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000775 switch (Lex.getCode()) {
776 default:
777 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000778 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000779 case tgtok::XHead:
780 case tgtok::XTail:
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000781 case tgtok::XSize:
David Greene2f7cf7f2011-01-07 17:05:37 +0000782 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000783 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
784 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000785 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000786
David Greenee8f3b272009-05-14 21:22:49 +0000787 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000788 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000789 case tgtok::XCast:
790 Lex.Lex(); // eat the operation
791 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000792
David Greenee8f3b272009-05-14 21:22:49 +0000793 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000794
Craig Topper011817a2014-04-09 04:50:04 +0000795 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000796 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000797 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000798 }
David Greene5d0c0512009-05-14 20:54:48 +0000799
David Greenee8f3b272009-05-14 21:22:49 +0000800 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000801 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000802 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000803 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000804 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000805 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000806 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000807 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000808 break;
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000809 case tgtok::XSize:
810 Lex.Lex();
811 Code = UnOpInit::SIZE;
812 Type = IntRecTy::get();
813 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000814 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000815 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000816 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000817 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000818 break;
David Greenee8f3b272009-05-14 21:22:49 +0000819 }
820 if (Lex.getCode() != tgtok::l_paren) {
821 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000822 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000823 }
824 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000825
David Greeneaf8ee2c2011-07-29 22:43:06 +0000826 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000827 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000828
Craig Topper85c07002015-04-30 05:54:22 +0000829 if (Code == UnOpInit::HEAD ||
830 Code == UnOpInit::TAIL ||
831 Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000832 ListInit *LHSl = dyn_cast<ListInit>(LHS);
833 StringInit *LHSs = dyn_cast<StringInit>(LHS);
834 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000835 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000836 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000837 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000838 }
839 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000840 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
841 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000842 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000843 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000844 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000845 }
846 }
847
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000848 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL ||
849 Code == UnOpInit::SIZE) {
Craig Topper011817a2014-04-09 04:50:04 +0000850 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000851 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000852 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000853 }
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000854 }
Bob Wilson7248f862009-11-22 04:24:42 +0000855
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000856 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
Craig Topperec9072d2015-05-14 05:53:53 +0000857 if (LHSl && LHSl->empty()) {
David Greened571b3c2009-05-14 22:38:31 +0000858 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000859 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000860 }
861 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000862 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000863 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000864 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000865 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000866 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000867 }
Craig Toppera9642b42015-05-04 01:35:39 +0000868 Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
869 : ListRecTy::get(Itemt->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000870 } else {
David Greened571b3c2009-05-14 22:38:31 +0000871 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000872 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000873 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000874 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000875 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000876 }
Craig Toppera9642b42015-05-04 01:35:39 +0000877 Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
David Greened571b3c2009-05-14 22:38:31 +0000878 }
879 }
880 }
881
David Greenee8f3b272009-05-14 21:22:49 +0000882 if (Lex.getCode() != tgtok::r_paren) {
883 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000884 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000885 }
886 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000887 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000888 }
David Greene5d0c0512009-05-14 20:54:48 +0000889
890 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000891 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000892 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000893 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +0000894 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000895 case tgtok::XSRL:
896 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000897 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000898 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000899 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000900 tgtok::TokKind OpTok = Lex.getCode();
901 SMLoc OpLoc = Lex.getLoc();
902 Lex.Lex(); // eat the operation
903
David Greene5d0c0512009-05-14 20:54:48 +0000904 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000905 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000906
Chris Lattner61ea00b2010-10-05 23:58:18 +0000907 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000908 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000909 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000910 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000911 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000912 case tgtok::XOR: Code = BinOpInit::OR; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000913 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
914 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
915 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
916 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000917 case tgtok::XListConcat:
918 Code = BinOpInit::LISTCONCAT;
919 // We don't know the list type until we parse the first argument
920 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000921 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000922 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000923 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000924 break;
David Greene5d0c0512009-05-14 20:54:48 +0000925 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000926
David Greene5d0c0512009-05-14 20:54:48 +0000927 if (Lex.getCode() != tgtok::l_paren) {
928 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000929 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000930 }
931 Lex.Lex(); // eat the '('
932
David Greeneaf8ee2c2011-07-29 22:43:06 +0000933 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000934
Chris Lattner61ea00b2010-10-05 23:58:18 +0000935 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000936 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000937
Chris Lattner61ea00b2010-10-05 23:58:18 +0000938 while (Lex.getCode() == tgtok::comma) {
939 Lex.Lex(); // eat the ','
940
941 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000942 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000943 }
David Greene5d0c0512009-05-14 20:54:48 +0000944
945 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000946 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000947 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000948 }
949 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000950
Daniel Sanders314e80e2014-05-07 10:13:19 +0000951 // If we are doing !listconcat, we should know the type by now
952 if (OpTok == tgtok::XListConcat) {
Nicolai Haehnlee4a2cf52018-02-22 15:26:28 +0000953 if (TypedInit *Arg0 = dyn_cast<TypedInit>(InitList[0]))
Daniel Sanders314e80e2014-05-07 10:13:19 +0000954 Type = Arg0->getType();
955 else {
Matthias Braun8c209aa2017-01-28 02:02:38 +0000956 InitList[0]->print(errs());
Daniel Sanders314e80e2014-05-07 10:13:19 +0000957 Error(OpLoc, "expected a list");
958 return nullptr;
959 }
960 }
961
Chris Lattner61ea00b2010-10-05 23:58:18 +0000962 // We allow multiple operands to associative operators like !strconcat as
963 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000964 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000965 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000966 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000967 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
968 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000969 InitList.back() = RHS;
970 }
971 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000972
Chris Lattner61ea00b2010-10-05 23:58:18 +0000973 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000974 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000975 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000976
Chris Lattner61ea00b2010-10-05 23:58:18 +0000977 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +0000978 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000979 }
980
David Greene3587eed2009-05-14 23:26:46 +0000981 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +0000982 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +0000983 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
984 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000985 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000986
David Greene98ed3c72009-05-14 21:54:42 +0000987 tgtok::TokKind LexCode = Lex.getCode();
988 Lex.Lex(); // eat the operation
989 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +0000990 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +0000991 case tgtok::XIf:
992 Code = TernOpInit::IF;
993 break;
David Greenee917fff2009-05-14 22:23:47 +0000994 case tgtok::XForEach:
995 Code = TernOpInit::FOREACH;
996 break;
David Greene98ed3c72009-05-14 21:54:42 +0000997 case tgtok::XSubst:
998 Code = TernOpInit::SUBST;
999 break;
1000 }
1001 if (Lex.getCode() != tgtok::l_paren) {
1002 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001003 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001004 }
1005 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001006
David Greeneaf8ee2c2011-07-29 22:43:06 +00001007 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001008 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001009
David Greene98ed3c72009-05-14 21:54:42 +00001010 if (Lex.getCode() != tgtok::comma) {
1011 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001012 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001013 }
1014 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001015
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001016 Init *MHS = ParseValue(CurRec, ItemType);
1017 if (!MHS)
1018 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001019
David Greene98ed3c72009-05-14 21:54:42 +00001020 if (Lex.getCode() != tgtok::comma) {
1021 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001022 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001023 }
1024 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001025
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001026 Init *RHS = ParseValue(CurRec, ItemType);
1027 if (!RHS)
1028 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001029
David Greene98ed3c72009-05-14 21:54:42 +00001030 if (Lex.getCode() != tgtok::r_paren) {
1031 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001032 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001033 }
1034 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001035
David Greene98ed3c72009-05-14 21:54:42 +00001036 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001037 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001038 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001039 RecTy *MHSTy = nullptr;
1040 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001041
Sean Silvafb509ed2012-10-10 20:24:43 +00001042 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001043 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001044 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001045 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001046 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001047 MHSTy = BitRecTy::get();
1048
Sean Silvafb509ed2012-10-10 20:24:43 +00001049 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001050 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001051 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001052 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001053 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001054 RHSTy = BitRecTy::get();
1055
1056 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001057 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001058 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001059 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001060 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001061
1062 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001063 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001064 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001065 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001066
Nicolai Haehnleaecb68b2018-02-23 10:46:13 +00001067 Type = resolveTypes(MHSTy, RHSTy);
1068 if (!Type) {
1069 TokError(Twine("inconsistent types '") + MHSTy->getAsString() +
1070 "' and '" + RHSTy->getAsString() + "' for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001071 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001072 }
1073 break;
1074 }
David Greenee917fff2009-05-14 22:23:47 +00001075 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001076 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
Craig Topper011817a2014-04-09 04:50:04 +00001077 if (!MHSt) {
David Greenee917fff2009-05-14 22:23:47 +00001078 TokError("could not get type for !foreach");
Craig Topper011817a2014-04-09 04:50:04 +00001079 return nullptr;
David Greenee917fff2009-05-14 22:23:47 +00001080 }
1081 Type = MHSt->getType();
Nicolai Haehnle6d649152018-02-22 15:26:35 +00001082 if (isa<ListRecTy>(Type)) {
1083 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1084 if (!RHSt) {
1085 TokError("could not get type of !foreach list elements");
1086 return nullptr;
1087 }
1088 Type = RHSt->getType()->getListTy();
1089 }
David Greenee917fff2009-05-14 22:23:47 +00001090 break;
1091 }
David Greene98ed3c72009-05-14 21:54:42 +00001092 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001093 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001094 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001095 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001096 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001097 }
1098 Type = RHSt->getType();
1099 break;
1100 }
1101 }
David Greenee32ebf22011-07-29 19:07:07 +00001102 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001103 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001104 }
David Greene5d0c0512009-05-14 20:54:48 +00001105 }
David Greene5d0c0512009-05-14 20:54:48 +00001106}
1107
1108/// ParseOperatorType - Parse a type for an operator. This returns
1109/// null on error.
1110///
1111/// OperatorType ::= '<' Type '>'
1112///
Dan Gohman1432ef82009-08-12 22:10:57 +00001113RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001114 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001115
1116 if (Lex.getCode() != tgtok::less) {
1117 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001118 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001119 }
1120 Lex.Lex(); // eat the <
1121
1122 Type = ParseType();
1123
Craig Topper011817a2014-04-09 04:50:04 +00001124 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001125 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001126 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001127 }
1128
1129 if (Lex.getCode() != tgtok::greater) {
1130 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 Lex.Lex(); // eat the >
1134
1135 return Type;
1136}
1137
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001138/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1139///
1140/// SimpleValue ::= IDValue
1141/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001142/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001143/// SimpleValue ::= CODEFRAGMENT
1144/// SimpleValue ::= '?'
1145/// SimpleValue ::= '{' ValueList '}'
1146/// SimpleValue ::= ID '<' ValueListNE '>'
1147/// SimpleValue ::= '[' ValueList ']'
1148/// SimpleValue ::= '(' IDValue DagArgList ')'
1149/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001150/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001151/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1152/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1153/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001154/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001155/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1156///
David Greened4263a62011-10-19 13:04:20 +00001157Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1158 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001159 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001160 switch (Lex.getCode()) {
1161 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001162 case tgtok::paste:
1163 // This is a leading paste operation. This is deprecated but
1164 // still exists in some .td files. Ignore it.
1165 Lex.Lex(); // Skip '#'.
1166 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001167 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001168 case tgtok::BinaryIntVal: {
1169 auto BinaryVal = Lex.getCurBinaryIntVal();
1170 SmallVector<Init*, 16> Bits(BinaryVal.second);
1171 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001172 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001173 R = BitsInit::get(Bits);
1174 Lex.Lex();
1175 break;
1176 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001177 case tgtok::StrVal: {
1178 std::string Val = Lex.getCurStrVal();
1179 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001180
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001181 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001182 while (Lex.getCode() == tgtok::StrVal) {
1183 Val += Lex.getCurStrVal();
1184 Lex.Lex();
1185 }
Bob Wilson7248f862009-11-22 04:24:42 +00001186
David Greenee32ebf22011-07-29 19:07:07 +00001187 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001188 break;
1189 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001190 case tgtok::CodeFragment:
Tim Northover88403d72016-07-05 21:22:55 +00001191 R = CodeInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001192 Lex.Lex();
1193 break;
1194 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001195 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001196 Lex.Lex();
1197 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001198 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001199 SMLoc NameLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00001200 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001201 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001202 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001203
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001204 // Value ::= ID '<' ValueListNE '>'
1205 if (Lex.Lex() == tgtok::greater) {
1206 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001207 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001208 }
David Greene8618f952009-06-08 20:23:18 +00001209
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001210 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1211 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1212 // body.
Matthias Braun215ff842016-12-05 07:35:13 +00001213 Record *Class = Records.getClass(Name->getValue());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001214 if (!Class) {
Matthias Braun215ff842016-12-05 07:35:13 +00001215 Error(NameLoc, "Expected a class name, got '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001216 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001217 }
David Greene8618f952009-06-08 20:23:18 +00001218
Matthias Braunc66e7552016-12-05 06:41:54 +00001219 SubClassReference SCRef;
1220 ParseValueList(SCRef.TemplateArgs, CurRec, Class);
1221 if (SCRef.TemplateArgs.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001222
David Greene8618f952009-06-08 20:23:18 +00001223 if (Lex.getCode() != tgtok::greater) {
1224 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001225 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001226 }
1227 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001228 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001229
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001230 // Create the new record, set it as CurRec temporarily.
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00001231 auto NewRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), NameLoc,
1232 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001233 Record *NewRec = NewRecOwner.get(); // Keep a copy since we may release.
Jordan Rosef12e8a92013-01-10 18:50:11 +00001234 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001235 SCRef.Rec = Class;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001236 // Add info about the subclass to NewRec.
Craig Topper84138712014-11-29 05:31:10 +00001237 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001238 return nullptr;
Craig Topper84138712014-11-29 05:31:10 +00001239
Hal Finkela8c1f462014-01-02 20:47:09 +00001240 if (!CurMultiClass) {
1241 NewRec->resolveReferences();
Craig Toppercdab2322014-11-29 05:52:51 +00001242 Records.addDef(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001243 } else {
Adam Nemete5a07162014-09-16 17:14:13 +00001244 // This needs to get resolved once the multiclass template arguments are
1245 // known before any use.
1246 NewRec->setResolveFirst(true);
Hal Finkela8c1f462014-01-02 20:47:09 +00001247 // Otherwise, we're inside a multiclass, add it to the multiclass.
Craig Topperc3504c42014-12-11 05:25:33 +00001248 CurMultiClass->DefPrototypes.push_back(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001249
1250 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00001251 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
1252 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Hal Finkela8c1f462014-01-02 20:47:09 +00001253 assert(RV && "Template arg doesn't exist?");
1254 NewRec->addValue(*RV);
1255 }
1256
1257 // We can't return the prototype def here, instead return:
1258 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1259 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1260 assert(MCNameRV && "multiclass record must have a NAME");
1261
1262 return UnOpInit::get(UnOpInit::CAST,
1263 BinOpInit::get(BinOpInit::STRCONCAT,
1264 VarInit::get(MCNameRV->getName(),
1265 MCNameRV->getType()),
1266 NewRec->getNameInit(),
1267 StringRecTy::get()),
1268 Class->getDefInit()->getType());
1269 }
Bob Wilson7248f862009-11-22 04:24:42 +00001270
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001271 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001272 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001273 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001274 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001275 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001276 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001277 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001278
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001279 if (Lex.getCode() != tgtok::r_brace) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001280 ParseValueList(Vals, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001281 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001282 }
1283 if (Lex.getCode() != tgtok::r_brace) {
1284 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001285 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001286 }
1287 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001288
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001289 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001290
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001291 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1292 // first. We'll first read everything in to a vector, then we can reverse
1293 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001294 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001295 // FIXME: The following two loops would not be duplicated
1296 // if the API was a little more orthogonal.
1297
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001298 // bits<n> values are allowed to initialize n bits.
1299 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1300 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1301 NewBits.push_back(BI->getBit((e - i) - 1));
1302 continue;
1303 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001304 // bits<n> can also come from variable initializers.
1305 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1306 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1307 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1308 NewBits.push_back(VI->getBit((e - i) - 1));
1309 continue;
1310 }
1311 // Fallthrough to try convert this to a bit.
1312 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001313 // All other values must be convertible to just a single bit.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001314 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001315 if (!Bit) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001316 Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
Chris Lattner52416952007-11-22 21:06:59 +00001317 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001318 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001319 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001320 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001321 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001322 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001323 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001324 }
1325 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1326 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001327 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001328
Craig Topper011817a2014-04-09 04:50:04 +00001329 RecTy *DeducedEltTy = nullptr;
1330 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001331
Craig Topper011817a2014-04-09 04:50:04 +00001332 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001333 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001334 if (!ListType) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001335 TokError(Twine("Type mismatch for list, expected list type, got ") +
1336 ItemType->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001337 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001338 }
1339 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001340 }
David Greene8618f952009-06-08 20:23:18 +00001341
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001342 if (Lex.getCode() != tgtok::r_square) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001343 ParseValueList(Vals, CurRec, nullptr,
1344 GivenListTy ? GivenListTy->getElementType() : nullptr);
Craig Topper011817a2014-04-09 04:50:04 +00001345 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001346 }
1347 if (Lex.getCode() != tgtok::r_square) {
1348 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001349 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001350 }
1351 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001352
Craig Topper011817a2014-04-09 04:50:04 +00001353 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001354 if (Lex.getCode() == tgtok::less) {
1355 // Optional list element type
1356 Lex.Lex(); // eat the '<'
1357
1358 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001359 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001360 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001361 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001362 }
1363
1364 if (Lex.getCode() != tgtok::greater) {
1365 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001366 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001367 }
1368 Lex.Lex(); // eat the '>'
1369 }
1370
1371 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001372 RecTy *EltTy = nullptr;
Craig Toppereb4d7c62015-04-29 04:43:36 +00001373 for (Init *V : Vals) {
1374 TypedInit *TArg = dyn_cast<TypedInit>(V);
Craig Topper011817a2014-04-09 04:50:04 +00001375 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001376 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001377 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001378 }
Craig Topper011817a2014-04-09 04:50:04 +00001379 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001380 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001381 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001382 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001383 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001384 }
Bob Wilson7248f862009-11-22 04:24:42 +00001385 } else {
David Greene8618f952009-06-08 20:23:18 +00001386 EltTy = TArg->getType();
1387 }
1388 }
1389
Craig Topper011817a2014-04-09 04:50:04 +00001390 if (GivenEltTy) {
1391 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001392 // Verify consistency
1393 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1394 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001395 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001396 }
1397 }
1398 EltTy = GivenEltTy;
1399 }
1400
Craig Topper011817a2014-04-09 04:50:04 +00001401 if (!EltTy) {
1402 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001403 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001404 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001405 }
1406 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001407 } else {
David Greene8618f952009-06-08 20:23:18 +00001408 // Make sure the deduced type is compatible with the given type
1409 if (GivenListTy) {
1410 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
Nicolai Haehnlef19083d2018-02-22 15:26:21 +00001411 TokError(Twine("Element type mismatch for list: element type '") +
1412 EltTy->getAsString() + "' not convertible to '" +
1413 GivenListTy->getElementType()->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001414 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001415 }
1416 }
1417 DeducedEltTy = EltTy;
1418 }
Bob Wilson7248f862009-11-22 04:24:42 +00001419
David Greenee32ebf22011-07-29 19:07:07 +00001420 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001421 }
1422 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1423 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001424 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001425 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001426 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001427 }
Bob Wilson7248f862009-11-22 04:24:42 +00001428
David Greeneaf8ee2c2011-07-29 22:43:06 +00001429 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001430 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001431
Nate Begemandbe3f772009-03-19 05:21:56 +00001432 // If the operator name is present, parse it.
Matthias Braun7cf3b112016-12-05 06:00:41 +00001433 StringInit *OperatorName = nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001434 if (Lex.getCode() == tgtok::colon) {
1435 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1436 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001437 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001438 }
Matthias Braun7cf3b112016-12-05 06:00:41 +00001439 OperatorName = StringInit::get(Lex.getCurStrVal());
Nate Begemandbe3f772009-03-19 05:21:56 +00001440 Lex.Lex(); // eat the VarName.
1441 }
Bob Wilson7248f862009-11-22 04:24:42 +00001442
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001443 SmallVector<std::pair<llvm::Init*, StringInit*>, 8> DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001444 if (Lex.getCode() != tgtok::r_paren) {
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001445 ParseDagArgList(DagArgs, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001446 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001447 }
Bob Wilson7248f862009-11-22 04:24:42 +00001448
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001449 if (Lex.getCode() != tgtok::r_paren) {
1450 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001451 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001452 }
1453 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001454
David Greenee32ebf22011-07-29 19:07:07 +00001455 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001456 }
Bob Wilson7248f862009-11-22 04:24:42 +00001457
David Greene2f7cf7f2011-01-07 17:05:37 +00001458 case tgtok::XHead:
1459 case tgtok::XTail:
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +00001460 case tgtok::XSize:
David Greene2f7cf7f2011-01-07 17:05:37 +00001461 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001462 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001463 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001464 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001465 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +00001466 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +00001467 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001468 case tgtok::XSRL:
1469 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001470 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001471 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001472 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001473 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001474 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001475 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001476 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001477 }
1478 }
Bob Wilson7248f862009-11-22 04:24:42 +00001479
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001480 return R;
1481}
1482
1483/// ParseValue - Parse a tblgen value. This returns null on error.
1484///
1485/// Value ::= SimpleValue ValueSuffix*
1486/// ValueSuffix ::= '{' BitList '}'
1487/// ValueSuffix ::= '[' BitList ']'
1488/// ValueSuffix ::= '.' ID
1489///
David Greened4263a62011-10-19 13:04:20 +00001490Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1491 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001492 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001493
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001494 // Parse the suffixes now if present.
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001495 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001496 switch (Lex.getCode()) {
1497 default: return Result;
1498 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001499 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001500 // This is the beginning of the object body.
1501 return Result;
1502
Chris Lattner526c8cb2009-06-21 03:39:35 +00001503 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001504 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001505 SmallVector<unsigned, 16> Ranges;
1506 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001507 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001508
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001509 // Reverse the bitlist.
1510 std::reverse(Ranges.begin(), Ranges.end());
1511 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001512 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001513 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001514 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001515 }
Bob Wilson7248f862009-11-22 04:24:42 +00001516
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001517 // Eat the '}'.
1518 if (Lex.getCode() != tgtok::r_brace) {
1519 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001520 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001521 }
1522 Lex.Lex();
1523 break;
1524 }
1525 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001526 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001527 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001528 SmallVector<unsigned, 16> Ranges;
1529 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001530 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001531
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001532 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001533 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001534 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001535 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001536 }
Bob Wilson7248f862009-11-22 04:24:42 +00001537
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001538 // Eat the ']'.
1539 if (Lex.getCode() != tgtok::r_square) {
1540 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001541 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001542 }
1543 Lex.Lex();
1544 break;
1545 }
Matthias Braun6a441832016-12-05 06:00:36 +00001546 case tgtok::period: {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001547 if (Lex.Lex() != tgtok::Id) { // eat the .
1548 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001549 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001550 }
Matthias Braun6a441832016-12-05 06:00:36 +00001551 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
1552 if (!Result->getFieldType(FieldName)) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001553 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001554 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001555 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001556 }
Matthias Braun6a441832016-12-05 06:00:36 +00001557 Result = FieldInit::get(Result, FieldName);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001558 Lex.Lex(); // eat field name
1559 break;
Matthias Braun6a441832016-12-05 06:00:36 +00001560 }
David Greene8e85b482011-10-19 13:04:43 +00001561
1562 case tgtok::paste:
1563 SMLoc PasteLoc = Lex.getLoc();
1564
1565 // Create a !strconcat() operation, first casting each operand to
1566 // a string if necessary.
1567
Sean Silvafb509ed2012-10-10 20:24:43 +00001568 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001569 if (!LHS) {
1570 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001571 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001572 }
Craig Toppera9642b42015-05-04 01:35:39 +00001573
David Greene8e85b482011-10-19 13:04:43 +00001574 if (LHS->getType() != StringRecTy::get()) {
1575 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1576 }
1577
Craig Topper011817a2014-04-09 04:50:04 +00001578 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001579
1580 Lex.Lex(); // Eat the '#'.
1581 switch (Lex.getCode()) {
1582 case tgtok::colon:
1583 case tgtok::semi:
1584 case tgtok::l_brace:
1585 // These are all of the tokens that can begin an object body.
1586 // Some of these can also begin values but we disallow those cases
1587 // because they are unlikely to be useful.
Craig Toppera9642b42015-05-04 01:35:39 +00001588
David Greene8e85b482011-10-19 13:04:43 +00001589 // Trailing paste, concat with an empty string.
1590 RHS = StringInit::get("");
1591 break;
1592
1593 default:
1594 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001595 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001596 if (!RHS) {
1597 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001598 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001599 }
1600
1601 if (RHS->getType() != StringRecTy::get()) {
1602 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1603 }
Craig Toppera9642b42015-05-04 01:35:39 +00001604
David Greene8e85b482011-10-19 13:04:43 +00001605 break;
1606 }
1607
1608 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1609 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1610 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001611 }
1612 }
1613}
1614
1615/// ParseDagArgList - Parse the argument list for a dag literal expression.
1616///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001617/// DagArg ::= Value (':' VARNAME)?
1618/// DagArg ::= VARNAME
1619/// DagArgList ::= DagArg
1620/// DagArgList ::= DagArgList ',' DagArg
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001621void TGParser::ParseDagArgList(
1622 SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
1623 Record *CurRec) {
Bob Wilson7248f862009-11-22 04:24:42 +00001624
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001625 while (true) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001626 // DagArg ::= VARNAME
1627 if (Lex.getCode() == tgtok::VarName) {
1628 // A missing value is treated like '?'.
Matthias Braunbb053162016-12-05 06:00:46 +00001629 StringInit *VarName = StringInit::get(Lex.getCurStrVal());
1630 Result.emplace_back(UnsetInit::get(), VarName);
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001631 Lex.Lex();
1632 } else {
1633 // DagArg ::= Value (':' VARNAME)?
1634 Init *Val = ParseValue(CurRec);
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001635 if (!Val) {
1636 Result.clear();
1637 return;
1638 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001639
1640 // If the variable name is present, add it.
Matthias Braunbb053162016-12-05 06:00:46 +00001641 StringInit *VarName = nullptr;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001642 if (Lex.getCode() == tgtok::colon) {
1643 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1644 TokError("expected variable name in dag literal");
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001645 Result.clear();
1646 return;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001647 }
Matthias Braunbb053162016-12-05 06:00:46 +00001648 VarName = StringInit::get(Lex.getCurStrVal());
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001649 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001650 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001651
1652 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001653 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001654 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001655 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001656 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001657}
1658
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001659/// ParseValueList - Parse a comma separated list of values, returning them as a
1660/// vector. Note that this always expects to be able to parse at least one
1661/// value. It returns an empty list if this is not possible.
1662///
1663/// ValueList ::= Value (',' Value)
1664///
Matthias Braunc66e7552016-12-05 06:41:54 +00001665void TGParser::ParseValueList(SmallVectorImpl<Init*> &Result, Record *CurRec,
1666 Record *ArgsRec, RecTy *EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001667 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001668 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001669 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001670 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001671 if (TArgs.empty()) {
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001672 TokError("template argument provided to non-template class");
Matthias Braunc66e7552016-12-05 06:41:54 +00001673 Result.clear();
1674 return;
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001675 }
David Greene8618f952009-06-08 20:23:18 +00001676 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001677 if (!RV) {
1678 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1679 << ")\n";
1680 }
David Greene8618f952009-06-08 20:23:18 +00001681 assert(RV && "Template argument record not found??");
1682 ItemType = RV->getType();
1683 ++ArgN;
1684 }
1685 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00001686 if (!Result.back()) {
1687 Result.clear();
1688 return;
1689 }
Bob Wilson7248f862009-11-22 04:24:42 +00001690
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001691 while (Lex.getCode() == tgtok::comma) {
1692 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001693
Craig Topper011817a2014-04-09 04:50:04 +00001694 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001695 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001696 if (ArgN >= TArgs.size()) {
1697 TokError("too many template arguments");
Matthias Braunc66e7552016-12-05 06:41:54 +00001698 Result.clear();
1699 return;
Bob Wilson7248f862009-11-22 04:24:42 +00001700 }
David Greene8618f952009-06-08 20:23:18 +00001701 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1702 assert(RV && "Template argument record not found??");
1703 ItemType = RV->getType();
1704 ++ArgN;
1705 }
1706 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00001707 if (!Result.back()) {
1708 Result.clear();
1709 return;
1710 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001711 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001712}
1713
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001714/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1715/// empty string on error. This can happen in a number of different context's,
1716/// including within a def or in the template args for a def (which which case
1717/// CurRec will be non-null) and within the template args for a multiclass (in
1718/// which case CurRec will be null, but CurMultiClass will be set). This can
1719/// also happen within a def that is within a multiclass, which will set both
1720/// CurRec and CurMultiClass.
1721///
1722/// Declaration ::= FIELD? Type ID ('=' Value)?
1723///
David Greenedb10e692011-10-19 13:02:42 +00001724Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001725 bool ParsingTemplateArgs) {
1726 // Read the field prefix if present.
1727 bool HasField = Lex.getCode() == tgtok::Field;
1728 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001729
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001730 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001731 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001732
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001733 if (Lex.getCode() != tgtok::Id) {
1734 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001735 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001736 }
Bob Wilson7248f862009-11-22 04:24:42 +00001737
Chris Lattner526c8cb2009-06-21 03:39:35 +00001738 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001739 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001740 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001741
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001742 if (ParsingTemplateArgs) {
Craig Toppera9642b42015-05-04 01:35:39 +00001743 if (CurRec)
David Greenedb10e692011-10-19 13:02:42 +00001744 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Craig Toppera9642b42015-05-04 01:35:39 +00001745 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001746 assert(CurMultiClass);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001747 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001748 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1749 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001750 }
Bob Wilson7248f862009-11-22 04:24:42 +00001751
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001752 // Add the value.
1753 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001754 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001755
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001756 // If a value is present, parse it.
1757 if (Lex.getCode() == tgtok::equal) {
1758 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001759 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001760 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001761 if (!Val ||
Craig Toppercfd81732016-01-04 03:15:08 +00001762 SetValue(CurRec, ValLoc, DeclName, None, Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001763 // Return the name, even if an error is thrown. This is so that we can
1764 // continue to make some progress, even without the value having been
1765 // initialized.
1766 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001767 }
Bob Wilson7248f862009-11-22 04:24:42 +00001768
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001769 return DeclName;
1770}
1771
David Greenefb927af2012-02-22 16:09:41 +00001772/// ParseForeachDeclaration - Read a foreach declaration, returning
1773/// the name of the declared object or a NULL Init on error. Return
1774/// the name of the parsed initializer list through ForeachListName.
1775///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001776/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1777/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1778/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001779///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001780VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001781 if (Lex.getCode() != tgtok::Id) {
1782 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001783 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001784 }
1785
1786 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1787 Lex.Lex();
1788
1789 // If a value is present, parse it.
1790 if (Lex.getCode() != tgtok::equal) {
1791 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001792 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001793 }
1794 Lex.Lex(); // Eat the '='
1795
Craig Topper011817a2014-04-09 04:50:04 +00001796 RecTy *IterType = nullptr;
Matthias Braunc66e7552016-12-05 06:41:54 +00001797 SmallVector<unsigned, 16> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001798
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001799 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001800 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001801 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001802 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001803 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001804 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001805 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001806 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001807 }
1808 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001809 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001810 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001811 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001812 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001813 }
1814 IterType = ListType->getElementType();
1815 break;
David Greenefb927af2012-02-22 16:09:41 +00001816 }
1817
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001818 case tgtok::IntVal: { // RangePiece.
1819 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001820 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001821 break;
David Greenefb927af2012-02-22 16:09:41 +00001822 }
1823
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001824 case tgtok::l_brace: { // '{' RangeList '}'
1825 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001826 ParseRangeList(Ranges);
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001827 if (Lex.getCode() != tgtok::r_brace) {
1828 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001829 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001830 }
1831 Lex.Lex();
1832 break;
1833 }
1834 }
David Greenefb927af2012-02-22 16:09:41 +00001835
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001836 if (!Ranges.empty()) {
1837 assert(!IterType && "Type already initialized?");
1838 IterType = IntRecTy::get();
1839 std::vector<Init*> Values;
Craig Topper8eb764c2015-06-02 06:19:28 +00001840 for (unsigned R : Ranges)
1841 Values.push_back(IntInit::get(R));
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001842 ForeachListValue = ListInit::get(Values, IterType);
1843 }
1844
1845 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001846 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001847
1848 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001849}
1850
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001851/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1852/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1853/// template args for a def, which may or may not be in a multiclass. If null,
1854/// these are the template args for a multiclass.
1855///
1856/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001857///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001858bool TGParser::ParseTemplateArgList(Record *CurRec) {
1859 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1860 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001861
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001862 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001863
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001864 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001865 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001866 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001867 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001868
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001869 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001870
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001871 while (Lex.getCode() == tgtok::comma) {
1872 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001873
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001874 // Read the following declarations.
1875 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001876 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001877 return true;
1878 TheRecToAddTo->addTemplateArg(TemplArg);
1879 }
Bob Wilson7248f862009-11-22 04:24:42 +00001880
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001881 if (Lex.getCode() != tgtok::greater)
1882 return TokError("expected '>' at end of template argument list");
1883 Lex.Lex(); // eat the '>'.
1884 return false;
1885}
1886
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001887/// ParseBodyItem - Parse a single item at within the body of a def or class.
1888///
1889/// BodyItem ::= Declaration ';'
1890/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1891bool TGParser::ParseBodyItem(Record *CurRec) {
1892 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001893 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001894 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001895
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001896 if (Lex.getCode() != tgtok::semi)
1897 return TokError("expected ';' after declaration");
1898 Lex.Lex();
1899 return false;
1900 }
1901
1902 // LET ID OptionalRangeList '=' Value ';'
1903 if (Lex.Lex() != tgtok::Id)
1904 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001905
Chris Lattner526c8cb2009-06-21 03:39:35 +00001906 SMLoc IdLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00001907 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001908 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001909
Matthias Braunc66e7552016-12-05 06:41:54 +00001910 SmallVector<unsigned, 16> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001911 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001912 return true;
1913 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001914
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001915 if (Lex.getCode() != tgtok::equal)
1916 return TokError("expected '=' in let expression");
1917 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001918
David Greene8618f952009-06-08 20:23:18 +00001919 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001920 if (!Field)
Matthias Braun215ff842016-12-05 07:35:13 +00001921 return TokError("Value '" + FieldName->getValue() + "' unknown!");
David Greene8618f952009-06-08 20:23:18 +00001922
1923 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001924
David Greeneaf8ee2c2011-07-29 22:43:06 +00001925 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001926 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001927
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001928 if (Lex.getCode() != tgtok::semi)
1929 return TokError("expected ';' after let expression");
1930 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001931
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001932 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1933}
1934
1935/// ParseBody - Read the body of a class or def. Return true on error, false on
1936/// success.
1937///
1938/// Body ::= ';'
1939/// Body ::= '{' BodyList '}'
1940/// BodyList BodyItem*
1941///
1942bool TGParser::ParseBody(Record *CurRec) {
1943 // If this is a null definition, just eat the semi and return.
1944 if (Lex.getCode() == tgtok::semi) {
1945 Lex.Lex();
1946 return false;
1947 }
Bob Wilson7248f862009-11-22 04:24:42 +00001948
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001949 if (Lex.getCode() != tgtok::l_brace)
1950 return TokError("Expected ';' or '{' to start body");
1951 // Eat the '{'.
1952 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001953
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001954 while (Lex.getCode() != tgtok::r_brace)
1955 if (ParseBodyItem(CurRec))
1956 return true;
1957
1958 // Eat the '}'.
1959 Lex.Lex();
1960 return false;
1961}
1962
Sean Silvacb1a75e2013-01-09 04:49:14 +00001963/// \brief Apply the current let bindings to \a CurRec.
1964/// \returns true on error, false otherwise.
1965bool TGParser::ApplyLetStack(Record *CurRec) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001966 for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
Craig Topper8eb764c2015-06-02 06:19:28 +00001967 for (LetRecord &LR : LetInfo)
1968 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
Sean Silvacb1a75e2013-01-09 04:49:14 +00001969 return true;
1970 return false;
1971}
1972
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001973/// ParseObjectBody - Parse the body of a def or class. This consists of an
1974/// optional ClassList followed by a Body. CurRec is the current def or class
1975/// that is being parsed.
1976///
1977/// ObjectBody ::= BaseClassList Body
1978/// BaseClassList ::= /*empty*/
1979/// BaseClassList ::= ':' BaseClassListNE
1980/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1981///
1982bool TGParser::ParseObjectBody(Record *CurRec) {
1983 // If there is a baseclass list, read it.
1984 if (Lex.getCode() == tgtok::colon) {
1985 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001986
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001987 // Read all of the subclasses.
1988 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001989 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001990 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00001991 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001992
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001993 // Add it.
1994 if (AddSubClass(CurRec, SubClass))
1995 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001996
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001997 if (Lex.getCode() != tgtok::comma) break;
1998 Lex.Lex(); // eat ','.
1999 SubClass = ParseSubClassReference(CurRec, false);
2000 }
2001 }
2002
Sean Silvacb1a75e2013-01-09 04:49:14 +00002003 if (ApplyLetStack(CurRec))
2004 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002005
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002006 return ParseBody(CurRec);
2007}
2008
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002009/// ParseDef - Parse and return a top level or multiclass def, return the record
2010/// corresponding to it. This returns null on error.
2011///
2012/// DefInst ::= DEF ObjectName ObjectBody
2013///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002014bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00002015 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002016 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00002017 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002018
2019 // Parse ObjectName and make a record for it.
Craig Topper84138712014-11-29 05:31:10 +00002020 std::unique_ptr<Record> CurRecOwner;
Jordan Roseabdd99b2013-01-10 18:50:05 +00002021 Init *Name = ParseObjectName(CurMultiClass);
2022 if (Name)
Craig Topper84138712014-11-29 05:31:10 +00002023 CurRecOwner = make_unique<Record>(Name, DefLoc, Records);
Jordan Roseabdd99b2013-01-10 18:50:05 +00002024 else
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00002025 CurRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), DefLoc,
2026 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00002027 Record *CurRec = CurRecOwner.get(); // Keep a copy since we may release.
Bob Wilson7248f862009-11-22 04:24:42 +00002028
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002029 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002030 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002031
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002032 // Ensure redefinition doesn't happen.
Craig Topper84138712014-11-29 05:31:10 +00002033 if (Records.getDef(CurRec->getNameInitAsString()))
2034 return Error(DefLoc, "def '" + CurRec->getNameInitAsString()+
2035 "' already defined");
Craig Toppercdab2322014-11-29 05:52:51 +00002036 Records.addDef(std::move(CurRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00002037
2038 if (ParseObjectBody(CurRec))
2039 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002040 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002041 // Parse the body before adding this prototype to the DefPrototypes vector.
2042 // That way implicit definitions will be added to the DefPrototypes vector
2043 // before this object, instantiated prior to defs derived from this object,
2044 // and this available for indirect name resolution when defs derived from
2045 // this object are instantiated.
Craig Topper84138712014-11-29 05:31:10 +00002046 if (ParseObjectBody(CurRec))
Hal Finkela8c1f462014-01-02 20:47:09 +00002047 return true;
2048
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002049 // Otherwise, a def inside a multiclass, add it to the multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002050 for (const auto &Proto : CurMultiClass->DefPrototypes)
2051 if (Proto->getNameInit() == CurRec->getNameInit())
Craig Topper84138712014-11-29 05:31:10 +00002052 return Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
2053 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002054 CurMultiClass->DefPrototypes.push_back(std::move(CurRecOwner));
Anton Yartsev671dff12014-08-08 00:29:54 +00002055 } else if (ParseObjectBody(CurRec)) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002056 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002057 }
Bob Wilson7248f862009-11-22 04:24:42 +00002058
Craig Topper011817a2014-04-09 04:50:04 +00002059 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002060 // See Record::setName(). This resolve step will see any new name
2061 // for the def that might have been created when resolving
2062 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002063 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002064
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002065 // If ObjectBody has template arguments, it's an error.
2066 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002067
2068 if (CurMultiClass) {
2069 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002070 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
2071 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002072 assert(RV && "Template arg doesn't exist?");
2073 CurRec->addValue(*RV);
2074 }
2075 }
2076
Craig Toppera9642b42015-05-04 01:35:39 +00002077 if (ProcessForeachDefs(CurRec, DefLoc))
Craig Topper84138712014-11-29 05:31:10 +00002078 return Error(DefLoc, "Could not process loops for def" +
2079 CurRec->getNameInitAsString());
David Greenefb927af2012-02-22 16:09:41 +00002080
2081 return false;
2082}
2083
2084/// ParseForeach - Parse a for statement. Return the record corresponding
2085/// to it. This returns true on error.
2086///
2087/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2088/// Foreach ::= FOREACH Declaration IN Object
2089///
2090bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2091 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2092 Lex.Lex(); // Eat the 'for' token.
2093
2094 // Make a temporary object to record items associated with the for
2095 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002096 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002097 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002098 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002099 return TokError("expected declaration in for");
2100
2101 if (Lex.getCode() != tgtok::In)
2102 return TokError("Unknown tok");
2103 Lex.Lex(); // Eat the in
2104
2105 // Create a loop object and remember it.
2106 Loops.push_back(ForeachLoop(IterName, ListValue));
2107
2108 if (Lex.getCode() != tgtok::l_brace) {
2109 // FOREACH Declaration IN Object
2110 if (ParseObject(CurMultiClass))
2111 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002112 } else {
David Greenefb927af2012-02-22 16:09:41 +00002113 SMLoc BraceLoc = Lex.getLoc();
2114 // Otherwise, this is a group foreach.
2115 Lex.Lex(); // eat the '{'.
2116
2117 // Parse the object list.
2118 if (ParseObjectList(CurMultiClass))
2119 return true;
2120
2121 if (Lex.getCode() != tgtok::r_brace) {
2122 TokError("expected '}' at end of foreach command");
2123 return Error(BraceLoc, "to match this '{'");
2124 }
2125 Lex.Lex(); // Eat the }
2126 }
2127
2128 // We've processed everything in this loop.
2129 Loops.pop_back();
2130
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002131 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002132}
2133
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002134/// ParseClass - Parse a tblgen class definition.
2135///
2136/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2137///
2138bool TGParser::ParseClass() {
2139 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2140 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002141
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002142 if (Lex.getCode() != tgtok::Id)
2143 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002144
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002145 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2146 if (CurRec) {
2147 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002148 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002149 !CurRec->getSuperClasses().empty() ||
2150 !CurRec->getTemplateArgs().empty())
Craig Topper85c07002015-04-30 05:54:22 +00002151 return TokError("Class '" + CurRec->getNameInitAsString() +
2152 "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002153 } else {
2154 // If this is the first reference to this class, create and add it.
Hans Wennborgffbbd532014-11-30 00:31:49 +00002155 auto NewRec =
2156 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
Craig Toppercdab2322014-11-29 05:52:51 +00002157 CurRec = NewRec.get();
2158 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002159 }
2160 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002161
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002162 // If there are template args, parse them.
2163 if (Lex.getCode() == tgtok::less)
2164 if (ParseTemplateArgList(CurRec))
2165 return true;
2166
2167 // Finally, parse the object body.
2168 return ParseObjectBody(CurRec);
2169}
2170
2171/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2172/// of LetRecords.
2173///
2174/// LetList ::= LetItem (',' LetItem)*
2175/// LetItem ::= ID OptionalRangeList '=' Value
2176///
Matthias Braunc66e7552016-12-05 06:41:54 +00002177void TGParser::ParseLetList(SmallVectorImpl<LetRecord> &Result) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002178 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002179 if (Lex.getCode() != tgtok::Id) {
2180 TokError("expected identifier in let definition");
Matthias Braunc66e7552016-12-05 06:41:54 +00002181 Result.clear();
2182 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002183 }
Matthias Braunc66e7552016-12-05 06:41:54 +00002184
Matthias Braun215ff842016-12-05 07:35:13 +00002185 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattner526c8cb2009-06-21 03:39:35 +00002186 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002187 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002188
2189 // Check for an optional RangeList.
Matthias Braunc66e7552016-12-05 06:41:54 +00002190 SmallVector<unsigned, 16> Bits;
2191 if (ParseOptionalRangeList(Bits)) {
2192 Result.clear();
2193 return;
2194 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002195 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002196
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002197 if (Lex.getCode() != tgtok::equal) {
2198 TokError("expected '=' in let expression");
Matthias Braunc66e7552016-12-05 06:41:54 +00002199 Result.clear();
2200 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002201 }
2202 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002203
Craig Topper011817a2014-04-09 04:50:04 +00002204 Init *Val = ParseValue(nullptr);
Matthias Braunc66e7552016-12-05 06:41:54 +00002205 if (!Val) {
2206 Result.clear();
2207 return;
2208 }
Bob Wilson7248f862009-11-22 04:24:42 +00002209
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002210 // Now that we have everything, add the record.
Matthias Braun215ff842016-12-05 07:35:13 +00002211 Result.emplace_back(Name, Bits, Val, NameLoc);
Bob Wilson7248f862009-11-22 04:24:42 +00002212
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002213 if (Lex.getCode() != tgtok::comma)
Matthias Braunc66e7552016-12-05 06:41:54 +00002214 return;
Bob Wilson7248f862009-11-22 04:24:42 +00002215 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002216 }
2217}
2218
2219/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002220/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002221///
2222/// Object ::= LET LetList IN '{' ObjectList '}'
2223/// Object ::= LET LetList IN Object
2224///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002225bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002226 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2227 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002228
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002229 // Add this entry to the let stack.
Matthias Braunc66e7552016-12-05 06:41:54 +00002230 SmallVector<LetRecord, 8> LetInfo;
2231 ParseLetList(LetInfo);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002232 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002233 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002234
2235 if (Lex.getCode() != tgtok::In)
2236 return TokError("expected 'in' at end of top-level 'let'");
2237 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002238
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002239 // If this is a scalar let, just handle it now
2240 if (Lex.getCode() != tgtok::l_brace) {
2241 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002242 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002243 return true;
2244 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002245 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002246 // Otherwise, this is a group let.
2247 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002248
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002249 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002250 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002251 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002252
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002253 if (Lex.getCode() != tgtok::r_brace) {
2254 TokError("expected '}' at end of top level let command");
2255 return Error(BraceLoc, "to match this '{'");
2256 }
2257 Lex.Lex();
2258 }
Bob Wilson7248f862009-11-22 04:24:42 +00002259
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002260 // Outside this let scope, this let block is not active.
2261 LetStack.pop_back();
2262 return false;
2263}
2264
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002265/// ParseMultiClass - Parse a multiclass definition.
2266///
Bob Wilson3d948162009-04-28 19:41:44 +00002267/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002268/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2269/// MultiClassObject ::= DefInst
2270/// MultiClassObject ::= MultiClassInst
2271/// MultiClassObject ::= DefMInst
2272/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2273/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002274///
2275bool TGParser::ParseMultiClass() {
2276 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2277 Lex.Lex(); // Eat the multiclass token.
2278
2279 if (Lex.getCode() != tgtok::Id)
2280 return TokError("expected identifier after multiclass for name");
2281 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002282
Craig Topper7adf2bf2014-12-11 05:25:30 +00002283 auto Result =
2284 MultiClasses.insert(std::make_pair(Name,
2285 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2286
2287 if (!Result.second)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002288 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002289
Craig Topper7adf2bf2014-12-11 05:25:30 +00002290 CurMultiClass = Result.first->second.get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002291 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002292
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002293 // If there are template args, parse them.
2294 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002295 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002296 return true;
2297
David Greene7049e792009-04-24 16:55:41 +00002298 bool inherits = false;
2299
David Greene753ed8f2009-04-22 16:42:54 +00002300 // If there are submulticlasses, parse them.
2301 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002302 inherits = true;
2303
David Greene753ed8f2009-04-22 16:42:54 +00002304 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002305
David Greene753ed8f2009-04-22 16:42:54 +00002306 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002307 SubMultiClassReference SubMultiClass =
2308 ParseSubMultiClassReference(CurMultiClass);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002309 while (true) {
David Greene753ed8f2009-04-22 16:42:54 +00002310 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002311 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002312
David Greene753ed8f2009-04-22 16:42:54 +00002313 // Add it.
2314 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2315 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002316
David Greene753ed8f2009-04-22 16:42:54 +00002317 if (Lex.getCode() != tgtok::comma) break;
2318 Lex.Lex(); // eat ','.
2319 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2320 }
2321 }
2322
David Greene7049e792009-04-24 16:55:41 +00002323 if (Lex.getCode() != tgtok::l_brace) {
2324 if (!inherits)
2325 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002326 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002327 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002328 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002329 } else {
David Greene7049e792009-04-24 16:55:41 +00002330 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2331 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002332
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002333 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002334 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002335 default:
2336 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
2337 case tgtok::Let:
2338 case tgtok::Def:
2339 case tgtok::Defm:
2340 case tgtok::Foreach:
2341 if (ParseObject(CurMultiClass))
2342 return true;
2343 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002344 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002345 }
David Greene7049e792009-04-24 16:55:41 +00002346 Lex.Lex(); // eat the '}'.
2347 }
Bob Wilson7248f862009-11-22 04:24:42 +00002348
Craig Topper011817a2014-04-09 04:50:04 +00002349 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002350 return false;
2351}
2352
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002353Record *TGParser::InstantiateMulticlassDef(MultiClass &MC, Record *DefProto,
2354 Init *&DefmPrefix,
2355 SMRange DefmPrefixRange,
2356 ArrayRef<Init *> TArgs,
Matthias Braunc66e7552016-12-05 06:41:54 +00002357 ArrayRef<Init *> TemplateVals) {
David Greene5d5d88c2011-10-19 13:04:31 +00002358 // We need to preserve DefProto so it can be reused for later
2359 // instantiations, so create a new Record to inherit from it.
2360
David Greenedb445972011-10-05 22:42:07 +00002361 // Add in the defm name. If the defm prefix is empty, give each
2362 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2363 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2364 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002365
Jordan Roseabdd99b2013-01-10 18:50:05 +00002366 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002367 if (!DefmPrefix) {
Craig Topperf4412262017-06-01 06:56:16 +00002368 DefmPrefix = GetNewAnonymousName();
Jordan Roseabdd99b2013-01-10 18:50:05 +00002369 IsAnonymous = true;
2370 }
David Greene5d5d88c2011-10-19 13:04:31 +00002371
2372 Init *DefName = DefProto->getNameInit();
Sean Silvafb509ed2012-10-10 20:24:43 +00002373 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002374
Craig Topper011817a2014-04-09 04:50:04 +00002375 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002376 // We have a fully expanded string so there are no operators to
2377 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002378 DefName =
2379 BinOpInit::get(BinOpInit::STRCONCAT,
2380 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2381 StringRecTy::get())->Fold(DefProto, &MC),
2382 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2383 }
David Greene5d5d88c2011-10-19 13:04:31 +00002384
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002385 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002386 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002387 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Craig Topper84138712014-11-29 05:31:10 +00002388 auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002389
2390 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002391 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002392 Ref.Rec = DefProto;
Craig Topper84138712014-11-29 05:31:10 +00002393 AddSubClass(CurRec.get(), Ref);
David Greenedb445972011-10-05 22:42:07 +00002394
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002395 // Set the value for NAME. We don't resolve references to it 'til later,
2396 // though, so that uses in nested multiclass names don't get
2397 // confused.
Matthias Braun215ff842016-12-05 07:35:13 +00002398 if (SetValue(CurRec.get(), Ref.RefRange.Start, StringInit::get("NAME"), None,
2399 DefmPrefix, /*AllowSelfAssignment*/true)) {
Craig Topper85c07002015-04-30 05:54:22 +00002400 Error(DefmPrefixRange.Start, "Could not resolve " +
2401 CurRec->getNameInitAsString() + ":NAME to '" +
2402 DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002403 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002404 }
David Greene8bf0d722011-10-19 13:04:35 +00002405
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002406 // If the DefNameString didn't resolve, we probably have a reference to
2407 // NAME and need to replace it. We need to do at least this much greedily,
2408 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002409 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002410 RecordVal *DefNameRV = CurRec->getValue("NAME");
2411 CurRec->resolveReferencesTo(DefNameRV);
2412 }
2413
2414 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002415 // Now that we're at the top level, resolve all NAME references
2416 // in the resultant defs that weren't in the def names themselves.
2417 RecordVal *DefNameRV = CurRec->getValue("NAME");
2418 CurRec->resolveReferencesTo(DefNameRV);
2419
Hal Finkeld2497362015-05-21 04:32:56 +00002420 // Check if the name is a complex pattern.
2421 // If so, resolve it.
2422 DefName = CurRec->getNameInit();
2423 DefNameString = dyn_cast<StringInit>(DefName);
2424
2425 // OK the pattern is more complex than simply using NAME.
2426 // Let's use the heavy weaponery.
2427 if (!DefNameString) {
2428 ResolveMulticlassDefArgs(MC, CurRec.get(), DefmPrefixRange.Start,
2429 Lex.getLoc(), TArgs, TemplateVals,
2430 false/*Delete args*/);
2431 DefName = CurRec->getNameInit();
2432 DefNameString = dyn_cast<StringInit>(DefName);
2433
2434 if (!DefNameString)
2435 DefName = DefName->convertInitializerTo(StringRecTy::get());
2436
2437 // We ran out of options here...
2438 DefNameString = dyn_cast<StringInit>(DefName);
2439 if (!DefNameString) {
2440 PrintFatalError(CurRec->getLoc()[CurRec->getLoc().size() - 1],
2441 DefName->getAsUnquotedString() + " is not a string.");
2442 return nullptr;
2443 }
2444
2445 CurRec->setName(DefName);
2446 }
2447
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002448 // Now that NAME references are resolved and we're at the top level of
2449 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002450 // currently in a multiclass, it means this defm appears inside a
2451 // multiclass and its name won't be fully resolvable until we see
Hal Finkeld2497362015-05-21 04:32:56 +00002452 // the top-level defm. Therefore, we don't add this to the
2453 // RecordKeeper at this point. If we did we could get duplicate
David Greene8bf0d722011-10-19 13:04:35 +00002454 // defs as more than one probably refers to NAME or some other
2455 // common internal placeholder.
2456
2457 // Ensure redefinition doesn't happen.
2458 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002459 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002460 "' already defined, instantiating defm with subdef '" +
2461 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002462 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002463 }
2464
Craig Topper84138712014-11-29 05:31:10 +00002465 Record *CurRecSave = CurRec.get(); // Keep a copy before we release.
Craig Toppercdab2322014-11-29 05:52:51 +00002466 Records.addDef(std::move(CurRec));
Craig Topper84138712014-11-29 05:31:10 +00002467 return CurRecSave;
David Greene8bf0d722011-10-19 13:04:35 +00002468 }
2469
Craig Topper84138712014-11-29 05:31:10 +00002470 // FIXME This is bad but the ownership transfer to caller is pretty messy.
2471 // The unique_ptr in this function at least protects the exits above.
2472 return CurRec.release();
David Greenedb445972011-10-05 22:42:07 +00002473}
2474
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002475bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC, Record *CurRec,
2476 SMLoc DefmPrefixLoc, SMLoc SubClassLoc,
2477 ArrayRef<Init *> TArgs,
Matthias Braunc66e7552016-12-05 06:41:54 +00002478 ArrayRef<Init *> TemplateVals,
David Greenedb445972011-10-05 22:42:07 +00002479 bool DeleteArgs) {
2480 // Loop over all of the template arguments, setting them to the specified
2481 // value or leaving them as the default if necessary.
2482 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2483 // Check if a value is specified for this temp-arg.
2484 if (i < TemplateVals.size()) {
2485 // Set it now.
Craig Toppercfd81732016-01-04 03:15:08 +00002486 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], None, TemplateVals[i]))
David Greenedb445972011-10-05 22:42:07 +00002487 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002488
David Greenedb445972011-10-05 22:42:07 +00002489 // Resolve it next.
2490 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2491
2492 if (DeleteArgs)
2493 // Now remove it.
2494 CurRec->removeValue(TArgs[i]);
Craig Toppera9642b42015-05-04 01:35:39 +00002495
David Greenedb445972011-10-05 22:42:07 +00002496 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Craig Topper85c07002015-04-30 05:54:22 +00002497 return Error(SubClassLoc, "value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00002498 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +00002499 ") of multiclassclass '" + MC.Rec.getNameInitAsString() +
2500 "'");
David Greenedb445972011-10-05 22:42:07 +00002501 }
2502 }
2503 return false;
2504}
2505
2506bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2507 Record *CurRec,
2508 Record *DefProto,
2509 SMLoc DefmPrefixLoc) {
2510 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002511 if (ApplyLetStack(CurRec))
2512 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002513
David Greenedb445972011-10-05 22:42:07 +00002514 // Don't create a top level definition for defm inside multiclasses,
2515 // instead, only update the prototypes and bind the template args
2516 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002517 if (!CurMultiClass)
2518 return false;
Craig Toppereb4d7c62015-04-29 04:43:36 +00002519 for (const auto &Proto : CurMultiClass->DefPrototypes)
2520 if (Proto->getNameInit() == CurRec->getNameInit())
Sean Silvacc951b22013-01-09 05:28:12 +00002521 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2522 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002523 CurMultiClass->DefPrototypes.push_back(std::unique_ptr<Record>(CurRec));
David Greenedb445972011-10-05 22:42:07 +00002524
Sean Silvacc951b22013-01-09 05:28:12 +00002525 // Copy the template arguments for the multiclass into the new def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002526 for (Init * TA : CurMultiClass->Rec.getTemplateArgs()) {
2527 const RecordVal *RV = CurMultiClass->Rec.getValue(TA);
Sean Silvacc951b22013-01-09 05:28:12 +00002528 assert(RV && "Template arg doesn't exist?");
2529 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002530 }
2531
2532 return false;
2533}
2534
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002535/// ParseDefm - Parse the instantiation of a multiclass.
2536///
2537/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2538///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002539bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002540 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002541 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002542 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002543
Craig Topperb21afc62013-01-07 05:09:33 +00002544 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002545 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002546 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002547
Jordan Rosef12e8a92013-01-10 18:50:11 +00002548 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002549 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002550 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002551
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002552 // Keep track of the new generated record definitions.
2553 std::vector<Record*> NewRecDefs;
2554
2555 // This record also inherits from a regular class (non-multiclass)?
2556 bool InheritFromClass = false;
2557
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002558 // eat the colon.
2559 Lex.Lex();
2560
Chris Lattner526c8cb2009-06-21 03:39:35 +00002561 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002562 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002563
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002564 while (true) {
Craig Topper011817a2014-04-09 04:50:04 +00002565 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002566
2567 // To instantiate a multiclass, we need to first get the multiclass, then
2568 // instantiate each def contained in the multiclass with the SubClassRef
2569 // template parameters.
Craig Topper7adf2bf2014-12-11 05:25:30 +00002570 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper4ca40012014-11-30 01:20:17 +00002571 assert(MC && "Didn't lookup multiclass correctly?");
Matthias Braunc66e7552016-12-05 06:41:54 +00002572 ArrayRef<Init*> TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002573
2574 // Verify that the correct number of template arguments were specified.
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002575 ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002576 if (TArgs.size() < TemplateVals.size())
2577 return Error(SubClassLoc,
2578 "more template args specified than multiclass expects");
2579
2580 // Loop over all the def's in the multiclass, instantiating each one.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002581 for (const std::unique_ptr<Record> &DefProto : MC->DefPrototypes) {
Hal Finkeld2497362015-05-21 04:32:56 +00002582 // The record name construction goes as follow:
2583 // - If the def name is a string, prepend the prefix.
2584 // - If the def name is a more complex pattern, use that pattern.
Craig Topper7f36be92016-02-26 06:50:27 +00002585 // As a result, the record is instantiated before resolving
Hal Finkeld2497362015-05-21 04:32:56 +00002586 // arguments, as it would make its name a string.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002587 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto.get(), DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002588 SMRange(DefmLoc,
Hal Finkeld2497362015-05-21 04:32:56 +00002589 DefmPrefixEndLoc),
2590 TArgs, TemplateVals);
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002591 if (!CurRec)
2592 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002593
Craig Topper7f36be92016-02-26 06:50:27 +00002594 // Now that the record is instantiated, we can resolve arguments.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002595 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002596 TArgs, TemplateVals, true/*Delete args*/))
2597 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002598
Craig Toppereb4d7c62015-04-29 04:43:36 +00002599 if (ResolveMulticlassDef(*MC, CurRec, DefProto.get(), DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002600 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002601
Adam Nemete5a07162014-09-16 17:14:13 +00002602 // Defs that can be used by other definitions should be fully resolved
2603 // before any use.
2604 if (DefProto->isResolveFirst() && !CurMultiClass) {
2605 CurRec->resolveReferences();
2606 CurRec->setResolveFirst(false);
2607 }
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002608 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002609 }
2610
David Greenedb445972011-10-05 22:42:07 +00002611
David Greenef00919a2009-04-22 22:17:51 +00002612 if (Lex.getCode() != tgtok::comma) break;
2613 Lex.Lex(); // eat ','.
2614
Craig Topper998a39a2013-08-20 04:22:09 +00002615 if (Lex.getCode() != tgtok::Id)
2616 return TokError("expected identifier");
2617
David Greenef00919a2009-04-22 22:17:51 +00002618 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002619
2620 // A defm can inherit from regular classes (non-multiclass) as
2621 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002622 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002623
2624 if (InheritFromClass)
2625 break;
2626
Craig Topper011817a2014-04-09 04:50:04 +00002627 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002628 }
2629
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002630 if (InheritFromClass) {
2631 // Process all the classes to inherit as if they were part of a
2632 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002633 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002634 while (true) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002635 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002636 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002637
2638 // Get the expanded definition prototypes and teach them about
2639 // the record values the current class to inherit has
Craig Toppereb4d7c62015-04-29 04:43:36 +00002640 for (Record *CurRec : NewRecDefs) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002641 // Add it.
2642 if (AddSubClass(CurRec, SubClass))
2643 return true;
2644
Sean Silvacb1a75e2013-01-09 04:49:14 +00002645 if (ApplyLetStack(CurRec))
2646 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002647 }
2648
2649 if (Lex.getCode() != tgtok::comma) break;
2650 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002651 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002652 }
2653 }
2654
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002655 if (!CurMultiClass)
Craig Toppereb4d7c62015-04-29 04:43:36 +00002656 for (Record *CurRec : NewRecDefs)
David Greene50c09122011-08-10 18:27:46 +00002657 // See Record::setName(). This resolve step will see any new
2658 // name for the def that might have been created when resolving
2659 // inheritance, values and arguments above.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002660 CurRec->resolveReferences();
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002661
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002662 if (Lex.getCode() != tgtok::semi)
2663 return TokError("expected ';' at end of defm");
2664 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002665
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002666 return false;
2667}
2668
2669/// ParseObject
2670/// Object ::= ClassInst
2671/// Object ::= DefInst
2672/// Object ::= MultiClassInst
2673/// Object ::= DefMInst
2674/// Object ::= LETCommand '{' ObjectList '}'
2675/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002676bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002677 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002678 default:
2679 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002680 case tgtok::Let: return ParseTopLevelLet(MC);
2681 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002682 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002683 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002684 case tgtok::Class: return ParseClass();
2685 case tgtok::MultiClass: return ParseMultiClass();
2686 }
2687}
2688
2689/// ParseObjectList
2690/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002691bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002692 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002693 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002694 return true;
2695 }
2696 return false;
2697}
2698
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002699bool TGParser::ParseFile() {
2700 Lex.Lex(); // Prime the lexer.
2701 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002702
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002703 // If we have unread input at the end of the file, report it.
2704 if (Lex.getCode() == tgtok::Eof)
2705 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002706
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002707 return TokError("Unexpected input at top level");
2708}