blob: 690cbf929c8de4b19b0545f85e31a3fc498931aa [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;
David Greeneaf8ee2c2011-07-29 22:43:06 +000039 std::vector<Init*> 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;
David Greeneaf8ee2c2011-07-29 22:43:06 +000049 std::vector<Init*> 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
Yaron Kereneb2a2542016-01-29 20:50:44 +000057LLVM_DUMP_METHOD void SubMultiClassReference::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000058 errs() << "Multiclass:\n";
Bob Wilson7248f862009-11-22 04:24:42 +000059
David Greene7049e792009-04-24 16:55:41 +000060 MC->dump();
Bob Wilson7248f862009-11-22 04:24:42 +000061
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000062 errs() << "Template args:\n";
Craig Toppera9642b42015-05-04 01:35:39 +000063 for (Init *TA : TemplateArgs)
Craig Toppereb4d7c62015-04-29 04:43:36 +000064 TA->dump();
David Greene7049e792009-04-24 16:55:41 +000065}
66
Chris Lattnerf4127dd2007-11-22 20:49:04 +000067} // end namespace llvm
68
Chris Lattner526c8cb2009-06-21 03:39:35 +000069bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Craig Topper011817a2014-04-09 04:50:04 +000070 if (!CurRec)
Chris Lattnerf4127dd2007-11-22 20:49:04 +000071 CurRec = &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +000072
Jakob Stoklund Olesen9d1c5ee2012-01-13 03:16:35 +000073 if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000074 // The value already exists in the class, treat this as a set.
75 if (ERV->setValue(RV.getValue()))
76 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
77 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson7248f862009-11-22 04:24:42 +000078 "previous definition of type '" +
Chris Lattnerf4127dd2007-11-22 20:49:04 +000079 ERV->getType()->getAsString() + "'");
80 } else {
81 CurRec->addValue(RV);
82 }
83 return false;
84}
85
86/// SetValue -
87/// Return true on error, false on success.
David Greene3ca42122011-10-19 13:02:39 +000088bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
Craig Toppercfd81732016-01-04 03:15:08 +000089 ArrayRef<unsigned> BitList, Init *V,
Craig Topper1e23ed92016-01-04 03:05:14 +000090 bool AllowSelfAssignment) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000091 if (!V) return false;
92
Craig Topper011817a2014-04-09 04:50:04 +000093 if (!CurRec) CurRec = &CurMultiClass->Rec;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000094
95 RecordVal *RV = CurRec->getValue(ValName);
Craig Topper011817a2014-04-09 04:50:04 +000096 if (!RV)
Craig Topper85c07002015-04-30 05:54:22 +000097 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
98 "' unknown!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +000099
100 // Do not allow assignments like 'X = X'. This will just cause infinite loops
101 // in the resolution machinery.
102 if (BitList.empty())
Sean Silvafb509ed2012-10-10 20:24:43 +0000103 if (VarInit *VI = dyn_cast<VarInit>(V))
Craig Topper1e23ed92016-01-04 03:05:14 +0000104 if (VI->getNameInit() == ValName && !AllowSelfAssignment)
105 return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000106
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000107 // If we are assigning to a subset of the bits in the value... then we must be
108 // assigning to a field of BitsRecTy, which must have a BitsInit
109 // initializer.
110 //
111 if (!BitList.empty()) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000112 BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue());
Craig Topper011817a2014-04-09 04:50:04 +0000113 if (!CurVal)
Craig Topper85c07002015-04-30 05:54:22 +0000114 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
115 "' is not a bits type");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000116
117 // Convert the incoming value to a bits type of the appropriate size...
David Greeneaf8ee2c2011-07-29 22:43:06 +0000118 Init *BI = V->convertInitializerTo(BitsRecTy::get(BitList.size()));
Craig Toppera9642b42015-05-04 01:35:39 +0000119 if (!BI)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000120 return Error(Loc, "Initializer is not compatible with bit range");
Bob Wilson7248f862009-11-22 04:24:42 +0000121
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000122 // We should have a BitsInit type now.
Craig Toppered5a9502015-04-29 07:13:05 +0000123 BitsInit *BInit = cast<BitsInit>(BI);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000124
David Greeneaf8ee2c2011-07-29 22:43:06 +0000125 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000126
127 // Loop over bits, assigning values as appropriate.
128 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
129 unsigned Bit = BitList[i];
David Greeneb3da8122011-07-29 19:07:00 +0000130 if (NewBits[Bit])
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000131 return Error(Loc, "Cannot set bit #" + Twine(Bit) + " of value '" +
David Greene3ca42122011-10-19 13:02:39 +0000132 ValName->getAsUnquotedString() + "' more than once");
David Greeneb3da8122011-07-29 19:07:00 +0000133 NewBits[Bit] = BInit->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000134 }
135
136 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
Craig Topper011817a2014-04-09 04:50:04 +0000137 if (!NewBits[i])
David Greeneb3da8122011-07-29 19:07:00 +0000138 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000139
David Greenee32ebf22011-07-29 19:07:07 +0000140 V = BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000141 }
142
Pete Cooper040c6a62014-07-31 01:43:57 +0000143 if (RV->setValue(V)) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000144 std::string InitType;
Craig Toppera9642b42015-05-04 01:35:39 +0000145 if (BitsInit *BI = dyn_cast<BitsInit>(V))
Pete Cooper040c6a62014-07-31 01:43:57 +0000146 InitType = (Twine("' of type bit initializer with length ") +
147 Twine(BI->getNumBits())).str();
Craig Topper85c07002015-04-30 05:54:22 +0000148 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
149 "' of type '" + RV->getType()->getAsString() +
150 "' is incompatible with initializer '" + V->getAsString() +
151 InitType + "'");
Pete Cooper040c6a62014-07-31 01:43:57 +0000152 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000153 return false;
154}
155
156/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
157/// args as SubClass's template arguments.
Cedric Venetd1e179d2009-02-14 16:06:42 +0000158bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000159 Record *SC = SubClass.Rec;
160 // Add all of the values in the subclass into the current class.
Craig Topper8eb764c2015-06-02 06:19:28 +0000161 for (const RecordVal &Val : SC->getValues())
162 if (AddValue(CurRec, SubClass.RefRange.Start, Val))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000163 return true;
164
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000165 ArrayRef<Init *> TArgs = SC->getTemplateArgs();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000166
167 // Ensure that an appropriate number of template arguments are specified.
168 if (TArgs.size() < SubClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000169 return Error(SubClass.RefRange.Start,
170 "More template args specified than expected");
Bob Wilson7248f862009-11-22 04:24:42 +0000171
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000172 // Loop over all of the template arguments, setting them to the specified
173 // value or leaving them as the default if necessary.
174 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
175 if (i < SubClass.TemplateArgs.size()) {
176 // If a value is specified for this template arg, set it now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000177 if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000178 None, SubClass.TemplateArgs[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000179 return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000180
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000181 // Resolve it next.
182 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson7248f862009-11-22 04:24:42 +0000183
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000184 // Now remove it.
185 CurRec->removeValue(TArgs[i]);
186
187 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000188 return Error(SubClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000189 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000190 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000191 ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000192 }
193 }
194
195 // Since everything went well, we can now set the "superclass" list for the
196 // current record.
Craig Topper0e41d0b2016-01-18 19:52:37 +0000197 ArrayRef<std::pair<Record *, SMRange>> SCs = SC->getSuperClasses();
198 for (const auto &SCPair : SCs) {
199 if (CurRec->isSubClassOf(SCPair.first))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000200 return Error(SubClass.RefRange.Start,
Craig Topper0e41d0b2016-01-18 19:52:37 +0000201 "Already subclass of '" + SCPair.first->getName() + "'!\n");
202 CurRec->addSuperClass(SCPair.first, SCPair.second);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000203 }
Bob Wilson7248f862009-11-22 04:24:42 +0000204
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000205 if (CurRec->isSubClassOf(SC))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000206 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000207 "Already subclass of '" + SC->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000208 CurRec->addSuperClass(SC, SubClass.RefRange);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000209 return false;
210}
211
David Greene753ed8f2009-04-22 16:42:54 +0000212/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilsonf71e6562009-04-30 18:26:19 +0000213/// CurMC, resolving its template args as SubMultiClass's
David Greene753ed8f2009-04-22 16:42:54 +0000214/// template arguments.
Bob Wilsonf71e6562009-04-30 18:26:19 +0000215bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson92ab8202009-04-30 17:46:20 +0000216 SubMultiClassReference &SubMultiClass) {
David Greene753ed8f2009-04-22 16:42:54 +0000217 MultiClass *SMC = SubMultiClass.MC;
Bob Wilsonf71e6562009-04-30 18:26:19 +0000218 Record *CurRec = &CurMC->Rec;
David Greene753ed8f2009-04-22 16:42:54 +0000219
David Greene753ed8f2009-04-22 16:42:54 +0000220 // Add all of the values in the subclass into the current class.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000221 for (const auto &SMCVal : SMC->Rec.getValues())
222 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000223 return true;
224
Craig Toppera4ea4b02014-11-30 00:24:32 +0000225 unsigned newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000226
David Greene753ed8f2009-04-22 16:42:54 +0000227 // Add all of the defs in the subclass into the current multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000228 for (const std::unique_ptr<Record> &R : SMC->DefPrototypes) {
David Greene753ed8f2009-04-22 16:42:54 +0000229 // Clone the def and add it to the current multiclass
Craig Toppereb4d7c62015-04-29 04:43:36 +0000230 auto NewDef = make_unique<Record>(*R);
David Greene753ed8f2009-04-22 16:42:54 +0000231
232 // Add all of the values in the superclass into the current def.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000233 for (const auto &MCVal : CurRec->getValues())
234 if (AddValue(NewDef.get(), SubMultiClass.RefRange.Start, MCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000235 return true;
236
Craig Topperc3504c42014-12-11 05:25:33 +0000237 CurMC->DefPrototypes.push_back(std::move(NewDef));
David Greene753ed8f2009-04-22 16:42:54 +0000238 }
Bob Wilson3d948162009-04-28 19:41:44 +0000239
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000240 ArrayRef<Init *> SMCTArgs = SMC->Rec.getTemplateArgs();
David Greene753ed8f2009-04-22 16:42:54 +0000241
David Greene7049e792009-04-24 16:55:41 +0000242 // Ensure that an appropriate number of template arguments are
243 // specified.
David Greene753ed8f2009-04-22 16:42:54 +0000244 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000245 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000246 "More template args specified than expected");
Bob Wilson3d948162009-04-28 19:41:44 +0000247
David Greene753ed8f2009-04-22 16:42:54 +0000248 // Loop over all of the template arguments, setting them to the specified
249 // value or leaving them as the default if necessary.
250 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
251 if (i < SubMultiClass.TemplateArgs.size()) {
David Greene7049e792009-04-24 16:55:41 +0000252 // If a value is specified for this template arg, set it in the
253 // superclass now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000254 if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000255 None, SubMultiClass.TemplateArgs[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000256 return true;
257
258 // Resolve it next.
259 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson3d948162009-04-28 19:41:44 +0000260
David Greene753ed8f2009-04-22 16:42:54 +0000261 // Now remove it.
262 CurRec->removeValue(SMCTArgs[i]);
263
David Greene7049e792009-04-24 16:55:41 +0000264 // If a value is specified for this template arg, set it in the
265 // new defs now.
Craig Topperc3504c42014-12-11 05:25:33 +0000266 for (const auto &Def :
267 makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
268 if (SetValue(Def.get(), SubMultiClass.RefRange.Start, SMCTArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000269 None, SubMultiClass.TemplateArgs[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000270 return true;
271
272 // Resolve it next.
273 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
274
275 // Now remove it
276 Def->removeValue(SMCTArgs[i]);
277 }
278 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000279 return Error(SubMultiClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000280 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000281 Twine(i) + " (" + SMCTArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000282 ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000283 }
284 }
285
286 return false;
287}
288
David Greenefb927af2012-02-22 16:09:41 +0000289/// ProcessForeachDefs - Given a record, apply all of the variable
290/// values in all surrounding foreach loops, creating new records for
291/// each combination of values.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000292bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
293 if (Loops.empty())
294 return false;
295
David Greenefb927af2012-02-22 16:09:41 +0000296 // We want to instantiate a new copy of CurRec for each combination
297 // of nested loop iterator values. We don't want top instantiate
298 // any copies until we have values for each loop iterator.
299 IterSet IterVals;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000300 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000301}
302
303/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
304/// apply each of the variable values in this loop and then process
305/// subloops.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000306bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
307 // Recursively build a tuple of iterator values.
308 if (IterVals.size() != Loops.size()) {
309 assert(IterVals.size() < Loops.size());
310 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silvafb509ed2012-10-10 20:24:43 +0000311 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Craig Topper011817a2014-04-09 04:50:04 +0000312 if (!List) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000313 Error(Loc, "Loop list is not a list");
314 return true;
315 }
David Greenefb927af2012-02-22 16:09:41 +0000316
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000317 // Process each value.
Craig Topper664f6a02015-06-02 04:15:57 +0000318 for (unsigned i = 0; i < List->size(); ++i) {
Craig Topper011817a2014-04-09 04:50:04 +0000319 Init *ItemVal = List->resolveListElementReference(*CurRec, nullptr, i);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000320 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
321 if (ProcessForeachDefs(CurRec, Loc, IterVals))
322 return true;
323 IterVals.pop_back();
324 }
325 return false;
326 }
327
328 // This is the bottom of the recursion. We have all of the iterator values
329 // for this point in the iteration space. Instantiate a new record to
330 // reflect this combination of values.
Craig Topper84138712014-11-29 05:31:10 +0000331 auto IterRec = make_unique<Record>(*CurRec);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000332
333 // Set the iterator values now.
Craig Topper8eb764c2015-06-02 06:19:28 +0000334 for (IterRecord &IR : IterVals) {
335 VarInit *IterVar = IR.IterVar;
336 TypedInit *IVal = dyn_cast<TypedInit>(IR.IterValue);
Craig Topper84138712014-11-29 05:31:10 +0000337 if (!IVal)
338 return Error(Loc, "foreach iterator value is untyped");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000339
340 IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false));
341
Craig Toppercfd81732016-01-04 03:15:08 +0000342 if (SetValue(IterRec.get(), Loc, IterVar->getName(), None, IVal))
Craig Topper84138712014-11-29 05:31:10 +0000343 return Error(Loc, "when instantiating this def");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000344
345 // Resolve it next.
346 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName()));
347
348 // Remove it.
349 IterRec->removeValue(IterVar->getName());
350 }
351
352 if (Records.getDef(IterRec->getNameInitAsString())) {
Artyom Skrobov8b985322014-06-10 12:41:14 +0000353 // If this record is anonymous, it's no problem, just generate a new name
Craig Topper84138712014-11-29 05:31:10 +0000354 if (!IterRec->isAnonymous())
355 return Error(Loc, "def already exists: " +IterRec->getNameInitAsString());
356
357 IterRec->setName(GetNewAnonymousName());
David Greenefb927af2012-02-22 16:09:41 +0000358 }
359
Craig Topper84138712014-11-29 05:31:10 +0000360 Record *IterRecSave = IterRec.get(); // Keep a copy before release.
Craig Toppercdab2322014-11-29 05:52:51 +0000361 Records.addDef(std::move(IterRec));
Craig Topper84138712014-11-29 05:31:10 +0000362 IterRecSave->resolveReferences();
David Greenefb927af2012-02-22 16:09:41 +0000363 return false;
364}
365
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000366//===----------------------------------------------------------------------===//
367// Parser Code
368//===----------------------------------------------------------------------===//
369
370/// isObjectStart - Return true if this is a valid first token for an Object.
371static bool isObjectStart(tgtok::TokKind K) {
372 return K == tgtok::Class || K == tgtok::Def ||
David Greenefb927af2012-02-22 16:09:41 +0000373 K == tgtok::Defm || K == tgtok::Let ||
374 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000375}
376
Alp Tokerce91fe52013-12-21 18:51:00 +0000377/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
378/// an identifier.
379std::string TGParser::GetNewAnonymousName() {
Aaron Ballman97a59fb2015-02-16 19:33:36 +0000380 return "anonymous_" + utostr(AnonCounter++);
Chris Lattner7538ed82010-10-05 22:51:56 +0000381}
382
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000383/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000384/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000385/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000386/// ObjectName ::= /*empty*/
387///
David Greene2affd672011-10-19 13:04:29 +0000388Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
389 switch (Lex.getCode()) {
390 case tgtok::colon:
391 case tgtok::semi:
392 case tgtok::l_brace:
393 // These are all of the tokens that can begin an object body.
394 // Some of these can also begin values but we disallow those cases
395 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000396 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000397 default:
398 break;
399 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000400
Craig Topper011817a2014-04-09 04:50:04 +0000401 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000402 if (CurMultiClass)
403 CurRec = &CurMultiClass->Rec;
404
Craig Topper011817a2014-04-09 04:50:04 +0000405 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000406 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000407 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000408 if (!CurRecName) {
409 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000410 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000411 }
412 Type = CurRecName->getType();
413 }
414
415 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000416}
417
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000418/// ParseClassID - Parse and resolve a reference to a class name. This returns
419/// null on error.
420///
421/// ClassID ::= ID
422///
423Record *TGParser::ParseClassID() {
424 if (Lex.getCode() != tgtok::Id) {
425 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000426 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000427 }
Bob Wilson7248f862009-11-22 04:24:42 +0000428
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000429 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000430 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000431 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000432
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000433 Lex.Lex();
434 return Result;
435}
436
Bob Wilson3d948162009-04-28 19:41:44 +0000437/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
438/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000439///
440/// MultiClassID ::= ID
441///
442MultiClass *TGParser::ParseMultiClassID() {
443 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000444 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000445 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000446 }
Bob Wilson3d948162009-04-28 19:41:44 +0000447
Craig Topper7adf2bf2014-12-11 05:25:30 +0000448 MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
Craig Topper4ca40012014-11-30 01:20:17 +0000449 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000450 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000451
David Greene753ed8f2009-04-22 16:42:54 +0000452 Lex.Lex();
Craig Topper4ca40012014-11-30 01:20:17 +0000453 return Result;
David Greene753ed8f2009-04-22 16:42:54 +0000454}
455
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000456/// ParseSubClassReference - Parse a reference to a subclass or to a templated
457/// subclass. This returns a SubClassRefTy with a null Record* on error.
458///
459/// SubClassRef ::= ClassID
460/// SubClassRef ::= ClassID '<' ValueList '>'
461///
462SubClassReference TGParser::
463ParseSubClassReference(Record *CurRec, bool isDefm) {
464 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000465 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000466
Sean Silva0657b402013-01-09 02:17:14 +0000467 if (isDefm) {
468 if (MultiClass *MC = ParseMultiClassID())
469 Result.Rec = &MC->Rec;
470 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000471 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000472 }
Craig Topper011817a2014-04-09 04:50:04 +0000473 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000474
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000475 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000476 if (Lex.getCode() != tgtok::less) {
477 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000478 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000479 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000480 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000481
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000482 if (Lex.getCode() == tgtok::greater) {
483 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000484 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000485 return Result;
486 }
Bob Wilson7248f862009-11-22 04:24:42 +0000487
David Greene8618f952009-06-08 20:23:18 +0000488 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000489 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000490 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000491 return Result;
492 }
Bob Wilson7248f862009-11-22 04:24:42 +0000493
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000494 if (Lex.getCode() != tgtok::greater) {
495 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000496 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000497 return Result;
498 }
499 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000500 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000501
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000502 return Result;
503}
504
Bob Wilson3d948162009-04-28 19:41:44 +0000505/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
506/// templated submulticlass. This returns a SubMultiClassRefTy with a null
507/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000508///
509/// SubMultiClassRef ::= MultiClassID
510/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
511///
512SubMultiClassReference TGParser::
513ParseSubMultiClassReference(MultiClass *CurMC) {
514 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000515 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000516
David Greene753ed8f2009-04-22 16:42:54 +0000517 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000518 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000519
David Greene753ed8f2009-04-22 16:42:54 +0000520 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000521 if (Lex.getCode() != tgtok::less) {
522 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000523 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000524 }
David Greene753ed8f2009-04-22 16:42:54 +0000525 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000526
David Greene753ed8f2009-04-22 16:42:54 +0000527 if (Lex.getCode() == tgtok::greater) {
528 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000529 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000530 return Result;
531 }
Bob Wilson3d948162009-04-28 19:41:44 +0000532
David Greene8618f952009-06-08 20:23:18 +0000533 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000534 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000535 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000536 return Result;
537 }
Bob Wilson3d948162009-04-28 19:41:44 +0000538
David Greene753ed8f2009-04-22 16:42:54 +0000539 if (Lex.getCode() != tgtok::greater) {
540 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000541 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000542 return Result;
543 }
544 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000545 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000546
547 return Result;
548}
549
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000550/// ParseRangePiece - Parse a bit/value range.
551/// RangePiece ::= INTVAL
552/// RangePiece ::= INTVAL '-' INTVAL
553/// RangePiece ::= INTVAL INTVAL
554bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000555 if (Lex.getCode() != tgtok::IntVal) {
556 TokError("expected integer or bitrange");
557 return true;
558 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000559 int64_t Start = Lex.getCurIntVal();
560 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000561
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000562 if (Start < 0)
563 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000564
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000565 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000566 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000567 Ranges.push_back(Start);
568 return false;
569 case tgtok::minus:
570 if (Lex.Lex() != tgtok::IntVal) {
571 TokError("expected integer value as end of range");
572 return true;
573 }
574 End = Lex.getCurIntVal();
575 break;
576 case tgtok::IntVal:
577 End = -Lex.getCurIntVal();
578 break;
579 }
Bob Wilson7248f862009-11-22 04:24:42 +0000580 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000581 return TokError("invalid range, cannot be negative");
582 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000583
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000584 // Add to the range.
Craig Toppera9642b42015-05-04 01:35:39 +0000585 if (Start < End)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000586 for (; Start <= End; ++Start)
587 Ranges.push_back(Start);
Craig Toppera9642b42015-05-04 01:35:39 +0000588 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000589 for (; Start >= End; --Start)
590 Ranges.push_back(Start);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000591 return false;
592}
593
594/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
595///
596/// RangeList ::= RangePiece (',' RangePiece)*
597///
598std::vector<unsigned> TGParser::ParseRangeList() {
599 std::vector<unsigned> Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000600
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000601 // Parse the first piece.
602 if (ParseRangePiece(Result))
603 return std::vector<unsigned>();
604 while (Lex.getCode() == tgtok::comma) {
605 Lex.Lex(); // Eat the comma.
606
607 // Parse the next range piece.
608 if (ParseRangePiece(Result))
609 return std::vector<unsigned>();
610 }
611 return Result;
612}
613
614/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
615/// OptionalRangeList ::= '<' RangeList '>'
616/// OptionalRangeList ::= /*empty*/
617bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
618 if (Lex.getCode() != tgtok::less)
619 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000620
Chris Lattner526c8cb2009-06-21 03:39:35 +0000621 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000622 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000623
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000624 // Parse the range list.
625 Ranges = ParseRangeList();
626 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000627
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000628 if (Lex.getCode() != tgtok::greater) {
629 TokError("expected '>' at end of range list");
630 return Error(StartLoc, "to match this '<'");
631 }
632 Lex.Lex(); // eat the '>'.
633 return false;
634}
635
636/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
637/// OptionalBitList ::= '{' RangeList '}'
638/// OptionalBitList ::= /*empty*/
639bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
640 if (Lex.getCode() != tgtok::l_brace)
641 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000642
Chris Lattner526c8cb2009-06-21 03:39:35 +0000643 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000644 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000645
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000646 // Parse the range list.
647 Ranges = ParseRangeList();
648 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000649
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000650 if (Lex.getCode() != tgtok::r_brace) {
651 TokError("expected '}' at end of bit list");
652 return Error(StartLoc, "to match this '{'");
653 }
654 Lex.Lex(); // eat the '}'.
655 return false;
656}
657
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000658/// ParseType - Parse and return a tblgen type. This returns null on error.
659///
660/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000661/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000662/// Type ::= BIT // bit type
663/// Type ::= BITS '<' INTVAL '>' // bits<x> type
664/// Type ::= INT // int type
665/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000666/// Type ::= DAG // dag type
667/// Type ::= ClassID // Record Type
668///
669RecTy *TGParser::ParseType() {
670 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000671 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000672 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Tim Northover88403d72016-07-05 21:22:55 +0000673 case tgtok::Code: Lex.Lex(); return CodeRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000674 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
675 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000676 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000677 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000678 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000679 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000680 case tgtok::Bits: {
681 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
682 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000683 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000684 }
685 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
686 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000687 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000688 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000689 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000690 if (Lex.Lex() != tgtok::greater) { // Eat count.
691 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000692 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000693 }
694 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000695 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000696 }
697 case tgtok::List: {
698 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
699 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000700 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000701 }
702 Lex.Lex(); // Eat '<'
703 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000704 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000705
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000706 if (Lex.getCode() != tgtok::greater) {
707 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000708 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000709 }
710 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000711 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000712 }
Bob Wilson7248f862009-11-22 04:24:42 +0000713 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000714}
715
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000716/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
717/// has already been read.
Matthias Braun5ce90572016-12-04 05:48:03 +0000718Init *TGParser::ParseIDValue(Record *CurRec, StringRef Name, SMLoc NameLoc,
David Greened4263a62011-10-19 13:04:20 +0000719 IDParseMode Mode) {
Matthias Braun6a441832016-12-05 06:00:36 +0000720 StringInit *NameInit;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000721 if (CurRec) {
722 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000723 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000724
Matthias Braun6a441832016-12-05 06:00:36 +0000725 NameInit = StringInit::get(Name);
726 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, NameInit, ":");
David Greenedb10e692011-10-19 13:02:42 +0000727
David Greene47a665e2011-10-05 22:42:54 +0000728 if (CurMultiClass)
Matthias Braun6a441832016-12-05 06:00:36 +0000729 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, NameInit,
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 Braun6a441832016-12-05 06:00:36 +0000737 } else
738 NameInit = StringInit::get(Name);
Bob Wilson7248f862009-11-22 04:24:42 +0000739
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000740 if (CurMultiClass) {
Matthias Braun6a441832016-12-05 06:00:36 +0000741 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, NameInit,
David Greenedb10e692011-10-19 13:02:42 +0000742 "::");
743
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000744 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
745 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
746 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000747 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000748 }
749 }
Bob Wilson7248f862009-11-22 04:24:42 +0000750
David Greenefb927af2012-02-22 16:09:41 +0000751 // If this is in a foreach loop, make sure it's not a loop iterator
Craig Toppereb4d7c62015-04-29 04:43:36 +0000752 for (const auto &L : Loops) {
753 VarInit *IterVar = dyn_cast<VarInit>(L.IterVar);
Matthias Braun6a441832016-12-05 06:00:36 +0000754 if (IterVar && IterVar->getNameInit() == NameInit)
David Greenefb927af2012-02-22 16:09:41 +0000755 return IterVar;
756 }
757
David Greene232bd602011-10-19 13:04:21 +0000758 if (Mode == ParseNameMode)
Matthias Braun6a441832016-12-05 06:00:36 +0000759 return NameInit;
David Greene232bd602011-10-19 13:04:21 +0000760
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000761 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000762 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000763
David Greene232bd602011-10-19 13:04:21 +0000764 if (Mode == ParseValueMode) {
765 Error(NameLoc, "Variable not defined: '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000766 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000767 }
Craig Toppera9642b42015-05-04 01:35:39 +0000768
Matthias Braun6a441832016-12-05 06:00:36 +0000769 return NameInit;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000770}
771
David Greene5d0c0512009-05-14 20:54:48 +0000772/// ParseOperation - Parse an operator. This returns null on error.
773///
774/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
775///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000776Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000777 switch (Lex.getCode()) {
778 default:
779 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000780 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000781 case tgtok::XHead:
782 case tgtok::XTail:
783 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000784 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
785 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000786 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000787
David Greenee8f3b272009-05-14 21:22:49 +0000788 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000789 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000790 case tgtok::XCast:
791 Lex.Lex(); // eat the operation
792 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000793
David Greenee8f3b272009-05-14 21:22:49 +0000794 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000795
Craig Topper011817a2014-04-09 04:50:04 +0000796 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000797 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000798 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000799 }
David Greene5d0c0512009-05-14 20:54:48 +0000800
David Greenee8f3b272009-05-14 21:22:49 +0000801 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000802 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000803 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000804 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000805 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000806 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000807 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000808 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000809 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000810 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000811 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000812 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000813 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000814 break;
David Greenee8f3b272009-05-14 21:22:49 +0000815 }
816 if (Lex.getCode() != tgtok::l_paren) {
817 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000818 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000819 }
820 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000821
David Greeneaf8ee2c2011-07-29 22:43:06 +0000822 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000823 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000824
Craig Topper85c07002015-04-30 05:54:22 +0000825 if (Code == UnOpInit::HEAD ||
826 Code == UnOpInit::TAIL ||
827 Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000828 ListInit *LHSl = dyn_cast<ListInit>(LHS);
829 StringInit *LHSs = dyn_cast<StringInit>(LHS);
830 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000831 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000832 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000833 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000834 }
835 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000836 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
837 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000838 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000839 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000840 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000841 }
842 }
843
Craig Topper85c07002015-04-30 05:54:22 +0000844 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
Craig Topper011817a2014-04-09 04:50:04 +0000845 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000846 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000847 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000848 }
Bob Wilson7248f862009-11-22 04:24:42 +0000849
Craig Topperec9072d2015-05-14 05:53:53 +0000850 if (LHSl && LHSl->empty()) {
David Greened571b3c2009-05-14 22:38:31 +0000851 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000852 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000853 }
854 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000855 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000856 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000857 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000858 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000859 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000860 }
Craig Toppera9642b42015-05-04 01:35:39 +0000861 Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
862 : ListRecTy::get(Itemt->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000863 } else {
David Greened571b3c2009-05-14 22:38:31 +0000864 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000865 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000866 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000867 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000868 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000869 }
Craig Toppera9642b42015-05-04 01:35:39 +0000870 Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
David Greened571b3c2009-05-14 22:38:31 +0000871 }
872 }
873 }
874
David Greenee8f3b272009-05-14 21:22:49 +0000875 if (Lex.getCode() != tgtok::r_paren) {
876 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000877 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000878 }
879 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000880 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000881 }
David Greene5d0c0512009-05-14 20:54:48 +0000882
883 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000884 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000885 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000886 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +0000887 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000888 case tgtok::XSRL:
889 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000890 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000891 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000892 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000893 tgtok::TokKind OpTok = Lex.getCode();
894 SMLoc OpLoc = Lex.getLoc();
895 Lex.Lex(); // eat the operation
896
David Greene5d0c0512009-05-14 20:54:48 +0000897 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000898 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000899
Chris Lattner61ea00b2010-10-05 23:58:18 +0000900 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000901 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000902 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000903 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000904 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000905 case tgtok::XOR: Code = BinOpInit::OR; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000906 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
907 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
908 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
909 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000910 case tgtok::XListConcat:
911 Code = BinOpInit::LISTCONCAT;
912 // We don't know the list type until we parse the first argument
913 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000914 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000915 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000916 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000917 break;
David Greene5d0c0512009-05-14 20:54:48 +0000918 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000919
David Greene5d0c0512009-05-14 20:54:48 +0000920 if (Lex.getCode() != tgtok::l_paren) {
921 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000922 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000923 }
924 Lex.Lex(); // eat the '('
925
David Greeneaf8ee2c2011-07-29 22:43:06 +0000926 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000927
Chris Lattner61ea00b2010-10-05 23:58:18 +0000928 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000929 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000930
Chris Lattner61ea00b2010-10-05 23:58:18 +0000931 while (Lex.getCode() == tgtok::comma) {
932 Lex.Lex(); // eat the ','
933
934 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000935 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000936 }
David Greene5d0c0512009-05-14 20:54:48 +0000937
938 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000939 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000940 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000941 }
942 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000943
Daniel Sanders314e80e2014-05-07 10:13:19 +0000944 // If we are doing !listconcat, we should know the type by now
945 if (OpTok == tgtok::XListConcat) {
946 if (VarInit *Arg0 = dyn_cast<VarInit>(InitList[0]))
947 Type = Arg0->getType();
948 else if (ListInit *Arg0 = dyn_cast<ListInit>(InitList[0]))
949 Type = Arg0->getType();
950 else {
951 InitList[0]->dump();
952 Error(OpLoc, "expected a list");
953 return nullptr;
954 }
955 }
956
Chris Lattner61ea00b2010-10-05 23:58:18 +0000957 // We allow multiple operands to associative operators like !strconcat as
958 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000959 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000960 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000961 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000962 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
963 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000964 InitList.back() = RHS;
965 }
966 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000967
Chris Lattner61ea00b2010-10-05 23:58:18 +0000968 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000969 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000970 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000971
Chris Lattner61ea00b2010-10-05 23:58:18 +0000972 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +0000973 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000974 }
975
David Greene3587eed2009-05-14 23:26:46 +0000976 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +0000977 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +0000978 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
979 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000980 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000981
David Greene98ed3c72009-05-14 21:54:42 +0000982 tgtok::TokKind LexCode = Lex.getCode();
983 Lex.Lex(); // eat the operation
984 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +0000985 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +0000986 case tgtok::XIf:
987 Code = TernOpInit::IF;
988 break;
David Greenee917fff2009-05-14 22:23:47 +0000989 case tgtok::XForEach:
990 Code = TernOpInit::FOREACH;
991 break;
David Greene98ed3c72009-05-14 21:54:42 +0000992 case tgtok::XSubst:
993 Code = TernOpInit::SUBST;
994 break;
995 }
996 if (Lex.getCode() != tgtok::l_paren) {
997 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000998 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +0000999 }
1000 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001001
David Greeneaf8ee2c2011-07-29 22:43:06 +00001002 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001003 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001004
David Greene98ed3c72009-05-14 21:54:42 +00001005 if (Lex.getCode() != tgtok::comma) {
1006 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001007 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001008 }
1009 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001010
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001011 Init *MHS = ParseValue(CurRec, ItemType);
1012 if (!MHS)
1013 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001014
David Greene98ed3c72009-05-14 21:54:42 +00001015 if (Lex.getCode() != tgtok::comma) {
1016 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001017 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001018 }
1019 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001020
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001021 Init *RHS = ParseValue(CurRec, ItemType);
1022 if (!RHS)
1023 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001024
David Greene98ed3c72009-05-14 21:54:42 +00001025 if (Lex.getCode() != tgtok::r_paren) {
1026 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001027 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001028 }
1029 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001030
David Greene98ed3c72009-05-14 21:54:42 +00001031 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001032 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001033 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001034 RecTy *MHSTy = nullptr;
1035 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001036
Sean Silvafb509ed2012-10-10 20:24:43 +00001037 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001038 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001039 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001040 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001041 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001042 MHSTy = BitRecTy::get();
1043
Sean Silvafb509ed2012-10-10 20:24:43 +00001044 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001045 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001046 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001047 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001048 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001049 RHSTy = BitRecTy::get();
1050
1051 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001052 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001053 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001054 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001055 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001056
1057 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001058 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001059 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001060 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001061
1062 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1063 Type = RHSTy;
1064 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1065 Type = MHSTy;
Bob Wilson7248f862009-11-22 04:24:42 +00001066 } else {
David Greene3587eed2009-05-14 23:26:46 +00001067 TokError("inconsistent types for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001068 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001069 }
1070 break;
1071 }
David Greenee917fff2009-05-14 22:23:47 +00001072 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001073 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
Craig Topper011817a2014-04-09 04:50:04 +00001074 if (!MHSt) {
David Greenee917fff2009-05-14 22:23:47 +00001075 TokError("could not get type for !foreach");
Craig Topper011817a2014-04-09 04:50:04 +00001076 return nullptr;
David Greenee917fff2009-05-14 22:23:47 +00001077 }
1078 Type = MHSt->getType();
1079 break;
1080 }
David Greene98ed3c72009-05-14 21:54:42 +00001081 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001082 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001083 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001084 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001085 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001086 }
1087 Type = RHSt->getType();
1088 break;
1089 }
1090 }
David Greenee32ebf22011-07-29 19:07:07 +00001091 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001092 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001093 }
David Greene5d0c0512009-05-14 20:54:48 +00001094 }
David Greene5d0c0512009-05-14 20:54:48 +00001095}
1096
1097/// ParseOperatorType - Parse a type for an operator. This returns
1098/// null on error.
1099///
1100/// OperatorType ::= '<' Type '>'
1101///
Dan Gohman1432ef82009-08-12 22:10:57 +00001102RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001103 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001104
1105 if (Lex.getCode() != tgtok::less) {
1106 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001107 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001108 }
1109 Lex.Lex(); // eat the <
1110
1111 Type = ParseType();
1112
Craig Topper011817a2014-04-09 04:50:04 +00001113 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001114 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001115 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001116 }
1117
1118 if (Lex.getCode() != tgtok::greater) {
1119 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001120 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001121 }
1122 Lex.Lex(); // eat the >
1123
1124 return Type;
1125}
1126
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001127/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1128///
1129/// SimpleValue ::= IDValue
1130/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001131/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001132/// SimpleValue ::= CODEFRAGMENT
1133/// SimpleValue ::= '?'
1134/// SimpleValue ::= '{' ValueList '}'
1135/// SimpleValue ::= ID '<' ValueListNE '>'
1136/// SimpleValue ::= '[' ValueList ']'
1137/// SimpleValue ::= '(' IDValue DagArgList ')'
1138/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001139/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001140/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1141/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1142/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001143/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001144/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1145///
David Greened4263a62011-10-19 13:04:20 +00001146Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1147 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001148 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001149 switch (Lex.getCode()) {
1150 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001151 case tgtok::paste:
1152 // This is a leading paste operation. This is deprecated but
1153 // still exists in some .td files. Ignore it.
1154 Lex.Lex(); // Skip '#'.
1155 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001156 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001157 case tgtok::BinaryIntVal: {
1158 auto BinaryVal = Lex.getCurBinaryIntVal();
1159 SmallVector<Init*, 16> Bits(BinaryVal.second);
1160 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001161 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001162 R = BitsInit::get(Bits);
1163 Lex.Lex();
1164 break;
1165 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001166 case tgtok::StrVal: {
1167 std::string Val = Lex.getCurStrVal();
1168 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001169
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001170 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001171 while (Lex.getCode() == tgtok::StrVal) {
1172 Val += Lex.getCurStrVal();
1173 Lex.Lex();
1174 }
Bob Wilson7248f862009-11-22 04:24:42 +00001175
David Greenee32ebf22011-07-29 19:07:07 +00001176 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001177 break;
1178 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001179 case tgtok::CodeFragment:
Tim Northover88403d72016-07-05 21:22:55 +00001180 R = CodeInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001181 Lex.Lex();
1182 break;
1183 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001184 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001185 Lex.Lex();
1186 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001187 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001188 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001189 std::string Name = Lex.getCurStrVal();
1190 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001191 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001192
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001193 // Value ::= ID '<' ValueListNE '>'
1194 if (Lex.Lex() == tgtok::greater) {
1195 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001196 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001197 }
David Greene8618f952009-06-08 20:23:18 +00001198
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001199 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1200 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1201 // body.
1202 Record *Class = Records.getClass(Name);
1203 if (!Class) {
1204 Error(NameLoc, "Expected a class name, got '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001205 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001206 }
David Greene8618f952009-06-08 20:23:18 +00001207
David Greeneaf8ee2c2011-07-29 22:43:06 +00001208 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
Craig Topper011817a2014-04-09 04:50:04 +00001209 if (ValueList.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001210
David Greene8618f952009-06-08 20:23:18 +00001211 if (Lex.getCode() != tgtok::greater) {
1212 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001213 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001214 }
1215 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001216 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001217
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001218 // Create the new record, set it as CurRec temporarily.
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00001219 auto NewRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), NameLoc,
1220 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001221 Record *NewRec = NewRecOwner.get(); // Keep a copy since we may release.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001222 SubClassReference SCRef;
Jordan Rosef12e8a92013-01-10 18:50:11 +00001223 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001224 SCRef.Rec = Class;
1225 SCRef.TemplateArgs = ValueList;
1226 // Add info about the subclass to NewRec.
Craig Topper84138712014-11-29 05:31:10 +00001227 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001228 return nullptr;
Craig Topper84138712014-11-29 05:31:10 +00001229
Hal Finkela8c1f462014-01-02 20:47:09 +00001230 if (!CurMultiClass) {
1231 NewRec->resolveReferences();
Craig Toppercdab2322014-11-29 05:52:51 +00001232 Records.addDef(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001233 } else {
Adam Nemete5a07162014-09-16 17:14:13 +00001234 // This needs to get resolved once the multiclass template arguments are
1235 // known before any use.
1236 NewRec->setResolveFirst(true);
Hal Finkela8c1f462014-01-02 20:47:09 +00001237 // Otherwise, we're inside a multiclass, add it to the multiclass.
Craig Topperc3504c42014-12-11 05:25:33 +00001238 CurMultiClass->DefPrototypes.push_back(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001239
1240 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00001241 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
1242 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Hal Finkela8c1f462014-01-02 20:47:09 +00001243 assert(RV && "Template arg doesn't exist?");
1244 NewRec->addValue(*RV);
1245 }
1246
1247 // We can't return the prototype def here, instead return:
1248 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1249 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1250 assert(MCNameRV && "multiclass record must have a NAME");
1251
1252 return UnOpInit::get(UnOpInit::CAST,
1253 BinOpInit::get(BinOpInit::STRCONCAT,
1254 VarInit::get(MCNameRV->getName(),
1255 MCNameRV->getType()),
1256 NewRec->getNameInit(),
1257 StringRecTy::get()),
1258 Class->getDefInit()->getType());
1259 }
Bob Wilson7248f862009-11-22 04:24:42 +00001260
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001261 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001262 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001263 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001264 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001265 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001266 Lex.Lex(); // eat the '{'
David Greeneaf8ee2c2011-07-29 22:43:06 +00001267 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001268
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001269 if (Lex.getCode() != tgtok::r_brace) {
1270 Vals = ParseValueList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001271 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001272 }
1273 if (Lex.getCode() != tgtok::r_brace) {
1274 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001275 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001276 }
1277 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001278
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001279 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001280
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001281 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1282 // first. We'll first read everything in to a vector, then we can reverse
1283 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001284 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001285 // FIXME: The following two loops would not be duplicated
1286 // if the API was a little more orthogonal.
1287
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001288 // bits<n> values are allowed to initialize n bits.
1289 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1290 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1291 NewBits.push_back(BI->getBit((e - i) - 1));
1292 continue;
1293 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001294 // bits<n> can also come from variable initializers.
1295 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1296 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1297 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1298 NewBits.push_back(VI->getBit((e - i) - 1));
1299 continue;
1300 }
1301 // Fallthrough to try convert this to a bit.
1302 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001303 // All other values must be convertible to just a single bit.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001304 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001305 if (!Bit) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001306 Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
Chris Lattner52416952007-11-22 21:06:59 +00001307 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001308 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001309 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001310 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001311 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001312 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001313 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001314 }
1315 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1316 Lex.Lex(); // eat the '['
David Greeneaf8ee2c2011-07-29 22:43:06 +00001317 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001318
Craig Topper011817a2014-04-09 04:50:04 +00001319 RecTy *DeducedEltTy = nullptr;
1320 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001321
Craig Topper011817a2014-04-09 04:50:04 +00001322 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001323 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001324 if (!ListType) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001325 TokError(Twine("Type mismatch for list, expected list type, got ") +
1326 ItemType->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001327 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001328 }
1329 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001330 }
David Greene8618f952009-06-08 20:23:18 +00001331
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001332 if (Lex.getCode() != tgtok::r_square) {
Craig Topper011817a2014-04-09 04:50:04 +00001333 Vals = ParseValueList(CurRec, nullptr,
1334 GivenListTy ? GivenListTy->getElementType() : nullptr);
1335 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001336 }
1337 if (Lex.getCode() != tgtok::r_square) {
1338 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001339 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001340 }
1341 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001342
Craig Topper011817a2014-04-09 04:50:04 +00001343 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001344 if (Lex.getCode() == tgtok::less) {
1345 // Optional list element type
1346 Lex.Lex(); // eat the '<'
1347
1348 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001349 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001350 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001351 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001352 }
1353
1354 if (Lex.getCode() != tgtok::greater) {
1355 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001356 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001357 }
1358 Lex.Lex(); // eat the '>'
1359 }
1360
1361 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001362 RecTy *EltTy = nullptr;
Craig Toppereb4d7c62015-04-29 04:43:36 +00001363 for (Init *V : Vals) {
1364 TypedInit *TArg = dyn_cast<TypedInit>(V);
Craig Topper011817a2014-04-09 04:50:04 +00001365 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001366 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001367 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001368 }
Craig Topper011817a2014-04-09 04:50:04 +00001369 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001370 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001371 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001372 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001373 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001374 }
Bob Wilson7248f862009-11-22 04:24:42 +00001375 } else {
David Greene8618f952009-06-08 20:23:18 +00001376 EltTy = TArg->getType();
1377 }
1378 }
1379
Craig Topper011817a2014-04-09 04:50:04 +00001380 if (GivenEltTy) {
1381 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001382 // Verify consistency
1383 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1384 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001385 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001386 }
1387 }
1388 EltTy = GivenEltTy;
1389 }
1390
Craig Topper011817a2014-04-09 04:50:04 +00001391 if (!EltTy) {
1392 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001393 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001394 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001395 }
1396 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001397 } else {
David Greene8618f952009-06-08 20:23:18 +00001398 // Make sure the deduced type is compatible with the given type
1399 if (GivenListTy) {
1400 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1401 TokError("Element type mismatch for list");
Craig Topper011817a2014-04-09 04:50:04 +00001402 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001403 }
1404 }
1405 DeducedEltTy = EltTy;
1406 }
Bob Wilson7248f862009-11-22 04:24:42 +00001407
David Greenee32ebf22011-07-29 19:07:07 +00001408 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001409 }
1410 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1411 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001412 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001413 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001414 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001415 }
Bob Wilson7248f862009-11-22 04:24:42 +00001416
David Greeneaf8ee2c2011-07-29 22:43:06 +00001417 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001418 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001419
Nate Begemandbe3f772009-03-19 05:21:56 +00001420 // If the operator name is present, parse it.
Matthias Braun7cf3b112016-12-05 06:00:41 +00001421 StringInit *OperatorName = nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001422 if (Lex.getCode() == tgtok::colon) {
1423 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1424 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001425 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001426 }
Matthias Braun7cf3b112016-12-05 06:00:41 +00001427 OperatorName = StringInit::get(Lex.getCurStrVal());
Nate Begemandbe3f772009-03-19 05:21:56 +00001428 Lex.Lex(); // eat the VarName.
1429 }
Bob Wilson7248f862009-11-22 04:24:42 +00001430
Matthias Braunbb053162016-12-05 06:00:46 +00001431 std::vector<std::pair<llvm::Init*, StringInit*> > DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001432 if (Lex.getCode() != tgtok::r_paren) {
1433 DagArgs = ParseDagArgList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001434 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001435 }
Bob Wilson7248f862009-11-22 04:24:42 +00001436
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001437 if (Lex.getCode() != tgtok::r_paren) {
1438 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001439 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001440 }
1441 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001442
David Greenee32ebf22011-07-29 19:07:07 +00001443 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001444 }
Bob Wilson7248f862009-11-22 04:24:42 +00001445
David Greene2f7cf7f2011-01-07 17:05:37 +00001446 case tgtok::XHead:
1447 case tgtok::XTail:
1448 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001449 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001450 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001451 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001452 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +00001453 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +00001454 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001455 case tgtok::XSRL:
1456 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001457 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001458 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001459 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001460 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001461 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001462 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001463 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001464 }
1465 }
Bob Wilson7248f862009-11-22 04:24:42 +00001466
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001467 return R;
1468}
1469
1470/// ParseValue - Parse a tblgen value. This returns null on error.
1471///
1472/// Value ::= SimpleValue ValueSuffix*
1473/// ValueSuffix ::= '{' BitList '}'
1474/// ValueSuffix ::= '[' BitList ']'
1475/// ValueSuffix ::= '.' ID
1476///
David Greened4263a62011-10-19 13:04:20 +00001477Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1478 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001479 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001480
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001481 // Parse the suffixes now if present.
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001482 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001483 switch (Lex.getCode()) {
1484 default: return Result;
1485 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001486 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001487 // This is the beginning of the object body.
1488 return Result;
1489
Chris Lattner526c8cb2009-06-21 03:39:35 +00001490 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001491 Lex.Lex(); // eat the '{'
1492 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001493 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001494
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001495 // Reverse the bitlist.
1496 std::reverse(Ranges.begin(), Ranges.end());
1497 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001498 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001499 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001500 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001501 }
Bob Wilson7248f862009-11-22 04:24:42 +00001502
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001503 // Eat the '}'.
1504 if (Lex.getCode() != tgtok::r_brace) {
1505 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001506 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001507 }
1508 Lex.Lex();
1509 break;
1510 }
1511 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001512 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001513 Lex.Lex(); // eat the '['
1514 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001515 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001516
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001517 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001518 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001519 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001520 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001521 }
Bob Wilson7248f862009-11-22 04:24:42 +00001522
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001523 // Eat the ']'.
1524 if (Lex.getCode() != tgtok::r_square) {
1525 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001526 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001527 }
1528 Lex.Lex();
1529 break;
1530 }
Matthias Braun6a441832016-12-05 06:00:36 +00001531 case tgtok::period: {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001532 if (Lex.Lex() != tgtok::Id) { // eat the .
1533 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001534 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001535 }
Matthias Braun6a441832016-12-05 06:00:36 +00001536 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
1537 if (!Result->getFieldType(FieldName)) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001538 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001539 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001540 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001541 }
Matthias Braun6a441832016-12-05 06:00:36 +00001542 Result = FieldInit::get(Result, FieldName);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001543 Lex.Lex(); // eat field name
1544 break;
Matthias Braun6a441832016-12-05 06:00:36 +00001545 }
David Greene8e85b482011-10-19 13:04:43 +00001546
1547 case tgtok::paste:
1548 SMLoc PasteLoc = Lex.getLoc();
1549
1550 // Create a !strconcat() operation, first casting each operand to
1551 // a string if necessary.
1552
Sean Silvafb509ed2012-10-10 20:24:43 +00001553 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001554 if (!LHS) {
1555 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001556 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001557 }
Craig Toppera9642b42015-05-04 01:35:39 +00001558
David Greene8e85b482011-10-19 13:04:43 +00001559 if (LHS->getType() != StringRecTy::get()) {
1560 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1561 }
1562
Craig Topper011817a2014-04-09 04:50:04 +00001563 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001564
1565 Lex.Lex(); // Eat the '#'.
1566 switch (Lex.getCode()) {
1567 case tgtok::colon:
1568 case tgtok::semi:
1569 case tgtok::l_brace:
1570 // These are all of the tokens that can begin an object body.
1571 // Some of these can also begin values but we disallow those cases
1572 // because they are unlikely to be useful.
Craig Toppera9642b42015-05-04 01:35:39 +00001573
David Greene8e85b482011-10-19 13:04:43 +00001574 // Trailing paste, concat with an empty string.
1575 RHS = StringInit::get("");
1576 break;
1577
1578 default:
1579 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001580 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001581 if (!RHS) {
1582 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001583 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001584 }
1585
1586 if (RHS->getType() != StringRecTy::get()) {
1587 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1588 }
Craig Toppera9642b42015-05-04 01:35:39 +00001589
David Greene8e85b482011-10-19 13:04:43 +00001590 break;
1591 }
1592
1593 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1594 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1595 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001596 }
1597 }
1598}
1599
1600/// ParseDagArgList - Parse the argument list for a dag literal expression.
1601///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001602/// DagArg ::= Value (':' VARNAME)?
1603/// DagArg ::= VARNAME
1604/// DagArgList ::= DagArg
1605/// DagArgList ::= DagArgList ',' DagArg
Matthias Braunbb053162016-12-05 06:00:46 +00001606std::vector<std::pair<llvm::Init*, StringInit*> >
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001607TGParser::ParseDagArgList(Record *CurRec) {
Matthias Braunbb053162016-12-05 06:00:46 +00001608 std::vector<std::pair<llvm::Init*, StringInit*> > Result;
Bob Wilson7248f862009-11-22 04:24:42 +00001609
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001610 while (true) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001611 // DagArg ::= VARNAME
1612 if (Lex.getCode() == tgtok::VarName) {
1613 // A missing value is treated like '?'.
Matthias Braunbb053162016-12-05 06:00:46 +00001614 StringInit *VarName = StringInit::get(Lex.getCurStrVal());
1615 Result.emplace_back(UnsetInit::get(), VarName);
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001616 Lex.Lex();
1617 } else {
1618 // DagArg ::= Value (':' VARNAME)?
1619 Init *Val = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001620 if (!Val)
Matthias Braunbb053162016-12-05 06:00:46 +00001621 return std::vector<std::pair<llvm::Init*, StringInit*> >();
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001622
1623 // If the variable name is present, add it.
Matthias Braunbb053162016-12-05 06:00:46 +00001624 StringInit *VarName = nullptr;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001625 if (Lex.getCode() == tgtok::colon) {
1626 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1627 TokError("expected variable name in dag literal");
Matthias Braunbb053162016-12-05 06:00:46 +00001628 return std::vector<std::pair<llvm::Init*, StringInit*> >();
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001629 }
Matthias Braunbb053162016-12-05 06:00:46 +00001630 VarName = StringInit::get(Lex.getCurStrVal());
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001631 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001632 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001633
1634 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001635 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001636 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001637 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001638 }
Bob Wilson7248f862009-11-22 04:24:42 +00001639
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001640 return Result;
1641}
1642
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001643/// ParseValueList - Parse a comma separated list of values, returning them as a
1644/// vector. Note that this always expects to be able to parse at least one
1645/// value. It returns an empty list if this is not possible.
1646///
1647/// ValueList ::= Value (',' Value)
1648///
David Greeneaf8ee2c2011-07-29 22:43:06 +00001649std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopher71520a82011-07-11 23:06:52 +00001650 RecTy *EltTy) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001651 std::vector<Init*> Result;
David Greene8618f952009-06-08 20:23:18 +00001652 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001653 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001654 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001655 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001656 if (TArgs.empty()) {
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001657 TokError("template argument provided to non-template class");
1658 return std::vector<Init*>();
1659 }
David Greene8618f952009-06-08 20:23:18 +00001660 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001661 if (!RV) {
1662 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1663 << ")\n";
1664 }
David Greene8618f952009-06-08 20:23:18 +00001665 assert(RV && "Template argument record not found??");
1666 ItemType = RV->getType();
1667 ++ArgN;
1668 }
1669 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001670 if (!Result.back()) return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001671
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001672 while (Lex.getCode() == tgtok::comma) {
1673 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001674
Craig Topper011817a2014-04-09 04:50:04 +00001675 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001676 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001677 if (ArgN >= TArgs.size()) {
1678 TokError("too many template arguments");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001679 return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001680 }
David Greene8618f952009-06-08 20:23:18 +00001681 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1682 assert(RV && "Template argument record not found??");
1683 ItemType = RV->getType();
1684 ++ArgN;
1685 }
1686 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001687 if (!Result.back()) return std::vector<Init*>();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001688 }
Bob Wilson7248f862009-11-22 04:24:42 +00001689
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001690 return Result;
1691}
1692
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001693/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1694/// empty string on error. This can happen in a number of different context's,
1695/// including within a def or in the template args for a def (which which case
1696/// CurRec will be non-null) and within the template args for a multiclass (in
1697/// which case CurRec will be null, but CurMultiClass will be set). This can
1698/// also happen within a def that is within a multiclass, which will set both
1699/// CurRec and CurMultiClass.
1700///
1701/// Declaration ::= FIELD? Type ID ('=' Value)?
1702///
David Greenedb10e692011-10-19 13:02:42 +00001703Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001704 bool ParsingTemplateArgs) {
1705 // Read the field prefix if present.
1706 bool HasField = Lex.getCode() == tgtok::Field;
1707 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001708
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001709 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001710 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001711
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001712 if (Lex.getCode() != tgtok::Id) {
1713 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001714 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001715 }
Bob Wilson7248f862009-11-22 04:24:42 +00001716
Chris Lattner526c8cb2009-06-21 03:39:35 +00001717 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001718 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001719 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001720
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001721 if (ParsingTemplateArgs) {
Craig Toppera9642b42015-05-04 01:35:39 +00001722 if (CurRec)
David Greenedb10e692011-10-19 13:02:42 +00001723 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Craig Toppera9642b42015-05-04 01:35:39 +00001724 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001725 assert(CurMultiClass);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001726 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001727 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1728 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001729 }
Bob Wilson7248f862009-11-22 04:24:42 +00001730
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001731 // Add the value.
1732 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001733 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001734
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001735 // If a value is present, parse it.
1736 if (Lex.getCode() == tgtok::equal) {
1737 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001738 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001739 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001740 if (!Val ||
Craig Toppercfd81732016-01-04 03:15:08 +00001741 SetValue(CurRec, ValLoc, DeclName, None, Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001742 // Return the name, even if an error is thrown. This is so that we can
1743 // continue to make some progress, even without the value having been
1744 // initialized.
1745 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001746 }
Bob Wilson7248f862009-11-22 04:24:42 +00001747
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001748 return DeclName;
1749}
1750
David Greenefb927af2012-02-22 16:09:41 +00001751/// ParseForeachDeclaration - Read a foreach declaration, returning
1752/// the name of the declared object or a NULL Init on error. Return
1753/// the name of the parsed initializer list through ForeachListName.
1754///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001755/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1756/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1757/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001758///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001759VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001760 if (Lex.getCode() != tgtok::Id) {
1761 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001762 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001763 }
1764
1765 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1766 Lex.Lex();
1767
1768 // If a value is present, parse it.
1769 if (Lex.getCode() != tgtok::equal) {
1770 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001771 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001772 }
1773 Lex.Lex(); // Eat the '='
1774
Craig Topper011817a2014-04-09 04:50:04 +00001775 RecTy *IterType = nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001776 std::vector<unsigned> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001777
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001778 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001779 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001780 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001781 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001782 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001783 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001784 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001785 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001786 }
1787 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001788 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001789 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001790 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001791 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001792 }
1793 IterType = ListType->getElementType();
1794 break;
David Greenefb927af2012-02-22 16:09:41 +00001795 }
1796
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001797 case tgtok::IntVal: { // RangePiece.
1798 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001799 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001800 break;
David Greenefb927af2012-02-22 16:09:41 +00001801 }
1802
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001803 case tgtok::l_brace: { // '{' RangeList '}'
1804 Lex.Lex(); // eat the '{'
1805 Ranges = ParseRangeList();
1806 if (Lex.getCode() != tgtok::r_brace) {
1807 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001808 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001809 }
1810 Lex.Lex();
1811 break;
1812 }
1813 }
David Greenefb927af2012-02-22 16:09:41 +00001814
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001815 if (!Ranges.empty()) {
1816 assert(!IterType && "Type already initialized?");
1817 IterType = IntRecTy::get();
1818 std::vector<Init*> Values;
Craig Topper8eb764c2015-06-02 06:19:28 +00001819 for (unsigned R : Ranges)
1820 Values.push_back(IntInit::get(R));
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001821 ForeachListValue = ListInit::get(Values, IterType);
1822 }
1823
1824 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001825 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001826
1827 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001828}
1829
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001830/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1831/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1832/// template args for a def, which may or may not be in a multiclass. If null,
1833/// these are the template args for a multiclass.
1834///
1835/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001836///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001837bool TGParser::ParseTemplateArgList(Record *CurRec) {
1838 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1839 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001840
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001841 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001842
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001843 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001844 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001845 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001846 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001847
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001848 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001849
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001850 while (Lex.getCode() == tgtok::comma) {
1851 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001852
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001853 // Read the following declarations.
1854 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001855 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001856 return true;
1857 TheRecToAddTo->addTemplateArg(TemplArg);
1858 }
Bob Wilson7248f862009-11-22 04:24:42 +00001859
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001860 if (Lex.getCode() != tgtok::greater)
1861 return TokError("expected '>' at end of template argument list");
1862 Lex.Lex(); // eat the '>'.
1863 return false;
1864}
1865
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001866/// ParseBodyItem - Parse a single item at within the body of a def or class.
1867///
1868/// BodyItem ::= Declaration ';'
1869/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1870bool TGParser::ParseBodyItem(Record *CurRec) {
1871 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001872 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001873 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001874
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001875 if (Lex.getCode() != tgtok::semi)
1876 return TokError("expected ';' after declaration");
1877 Lex.Lex();
1878 return false;
1879 }
1880
1881 // LET ID OptionalRangeList '=' Value ';'
1882 if (Lex.Lex() != tgtok::Id)
1883 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001884
Chris Lattner526c8cb2009-06-21 03:39:35 +00001885 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001886 std::string FieldName = Lex.getCurStrVal();
1887 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001888
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001889 std::vector<unsigned> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001890 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001891 return true;
1892 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001893
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001894 if (Lex.getCode() != tgtok::equal)
1895 return TokError("expected '=' in let expression");
1896 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001897
David Greene8618f952009-06-08 20:23:18 +00001898 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001899 if (!Field)
David Greene8618f952009-06-08 20:23:18 +00001900 return TokError("Value '" + FieldName + "' unknown!");
1901
1902 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001903
David Greeneaf8ee2c2011-07-29 22:43:06 +00001904 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001905 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001906
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001907 if (Lex.getCode() != tgtok::semi)
1908 return TokError("expected ';' after let expression");
1909 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001910
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001911 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1912}
1913
1914/// ParseBody - Read the body of a class or def. Return true on error, false on
1915/// success.
1916///
1917/// Body ::= ';'
1918/// Body ::= '{' BodyList '}'
1919/// BodyList BodyItem*
1920///
1921bool TGParser::ParseBody(Record *CurRec) {
1922 // If this is a null definition, just eat the semi and return.
1923 if (Lex.getCode() == tgtok::semi) {
1924 Lex.Lex();
1925 return false;
1926 }
Bob Wilson7248f862009-11-22 04:24:42 +00001927
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001928 if (Lex.getCode() != tgtok::l_brace)
1929 return TokError("Expected ';' or '{' to start body");
1930 // Eat the '{'.
1931 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001932
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001933 while (Lex.getCode() != tgtok::r_brace)
1934 if (ParseBodyItem(CurRec))
1935 return true;
1936
1937 // Eat the '}'.
1938 Lex.Lex();
1939 return false;
1940}
1941
Sean Silvacb1a75e2013-01-09 04:49:14 +00001942/// \brief Apply the current let bindings to \a CurRec.
1943/// \returns true on error, false otherwise.
1944bool TGParser::ApplyLetStack(Record *CurRec) {
Craig Topper8eb764c2015-06-02 06:19:28 +00001945 for (std::vector<LetRecord> &LetInfo : LetStack)
1946 for (LetRecord &LR : LetInfo)
1947 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
Sean Silvacb1a75e2013-01-09 04:49:14 +00001948 return true;
1949 return false;
1950}
1951
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001952/// ParseObjectBody - Parse the body of a def or class. This consists of an
1953/// optional ClassList followed by a Body. CurRec is the current def or class
1954/// that is being parsed.
1955///
1956/// ObjectBody ::= BaseClassList Body
1957/// BaseClassList ::= /*empty*/
1958/// BaseClassList ::= ':' BaseClassListNE
1959/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1960///
1961bool TGParser::ParseObjectBody(Record *CurRec) {
1962 // If there is a baseclass list, read it.
1963 if (Lex.getCode() == tgtok::colon) {
1964 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001965
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001966 // Read all of the subclasses.
1967 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001968 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001969 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00001970 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001971
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001972 // Add it.
1973 if (AddSubClass(CurRec, SubClass))
1974 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001975
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001976 if (Lex.getCode() != tgtok::comma) break;
1977 Lex.Lex(); // eat ','.
1978 SubClass = ParseSubClassReference(CurRec, false);
1979 }
1980 }
1981
Sean Silvacb1a75e2013-01-09 04:49:14 +00001982 if (ApplyLetStack(CurRec))
1983 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001984
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001985 return ParseBody(CurRec);
1986}
1987
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001988/// ParseDef - Parse and return a top level or multiclass def, return the record
1989/// corresponding to it. This returns null on error.
1990///
1991/// DefInst ::= DEF ObjectName ObjectBody
1992///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00001993bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001994 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001995 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00001996 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001997
1998 // Parse ObjectName and make a record for it.
Craig Topper84138712014-11-29 05:31:10 +00001999 std::unique_ptr<Record> CurRecOwner;
Jordan Roseabdd99b2013-01-10 18:50:05 +00002000 Init *Name = ParseObjectName(CurMultiClass);
2001 if (Name)
Craig Topper84138712014-11-29 05:31:10 +00002002 CurRecOwner = make_unique<Record>(Name, DefLoc, Records);
Jordan Roseabdd99b2013-01-10 18:50:05 +00002003 else
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00002004 CurRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), DefLoc,
2005 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00002006 Record *CurRec = CurRecOwner.get(); // Keep a copy since we may release.
Bob Wilson7248f862009-11-22 04:24:42 +00002007
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002008 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002009 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002010
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002011 // Ensure redefinition doesn't happen.
Craig Topper84138712014-11-29 05:31:10 +00002012 if (Records.getDef(CurRec->getNameInitAsString()))
2013 return Error(DefLoc, "def '" + CurRec->getNameInitAsString()+
2014 "' already defined");
Craig Toppercdab2322014-11-29 05:52:51 +00002015 Records.addDef(std::move(CurRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00002016
2017 if (ParseObjectBody(CurRec))
2018 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002019 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002020 // Parse the body before adding this prototype to the DefPrototypes vector.
2021 // That way implicit definitions will be added to the DefPrototypes vector
2022 // before this object, instantiated prior to defs derived from this object,
2023 // and this available for indirect name resolution when defs derived from
2024 // this object are instantiated.
Craig Topper84138712014-11-29 05:31:10 +00002025 if (ParseObjectBody(CurRec))
Hal Finkela8c1f462014-01-02 20:47:09 +00002026 return true;
2027
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002028 // Otherwise, a def inside a multiclass, add it to the multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002029 for (const auto &Proto : CurMultiClass->DefPrototypes)
2030 if (Proto->getNameInit() == CurRec->getNameInit())
Craig Topper84138712014-11-29 05:31:10 +00002031 return Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
2032 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002033 CurMultiClass->DefPrototypes.push_back(std::move(CurRecOwner));
Anton Yartsev671dff12014-08-08 00:29:54 +00002034 } else if (ParseObjectBody(CurRec)) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002035 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002036 }
Bob Wilson7248f862009-11-22 04:24:42 +00002037
Craig Topper011817a2014-04-09 04:50:04 +00002038 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002039 // See Record::setName(). This resolve step will see any new name
2040 // for the def that might have been created when resolving
2041 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002042 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002043
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002044 // If ObjectBody has template arguments, it's an error.
2045 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002046
2047 if (CurMultiClass) {
2048 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002049 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
2050 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002051 assert(RV && "Template arg doesn't exist?");
2052 CurRec->addValue(*RV);
2053 }
2054 }
2055
Craig Toppera9642b42015-05-04 01:35:39 +00002056 if (ProcessForeachDefs(CurRec, DefLoc))
Craig Topper84138712014-11-29 05:31:10 +00002057 return Error(DefLoc, "Could not process loops for def" +
2058 CurRec->getNameInitAsString());
David Greenefb927af2012-02-22 16:09:41 +00002059
2060 return false;
2061}
2062
2063/// ParseForeach - Parse a for statement. Return the record corresponding
2064/// to it. This returns true on error.
2065///
2066/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2067/// Foreach ::= FOREACH Declaration IN Object
2068///
2069bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2070 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2071 Lex.Lex(); // Eat the 'for' token.
2072
2073 // Make a temporary object to record items associated with the for
2074 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002075 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002076 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002077 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002078 return TokError("expected declaration in for");
2079
2080 if (Lex.getCode() != tgtok::In)
2081 return TokError("Unknown tok");
2082 Lex.Lex(); // Eat the in
2083
2084 // Create a loop object and remember it.
2085 Loops.push_back(ForeachLoop(IterName, ListValue));
2086
2087 if (Lex.getCode() != tgtok::l_brace) {
2088 // FOREACH Declaration IN Object
2089 if (ParseObject(CurMultiClass))
2090 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002091 } else {
David Greenefb927af2012-02-22 16:09:41 +00002092 SMLoc BraceLoc = Lex.getLoc();
2093 // Otherwise, this is a group foreach.
2094 Lex.Lex(); // eat the '{'.
2095
2096 // Parse the object list.
2097 if (ParseObjectList(CurMultiClass))
2098 return true;
2099
2100 if (Lex.getCode() != tgtok::r_brace) {
2101 TokError("expected '}' at end of foreach command");
2102 return Error(BraceLoc, "to match this '{'");
2103 }
2104 Lex.Lex(); // Eat the }
2105 }
2106
2107 // We've processed everything in this loop.
2108 Loops.pop_back();
2109
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002110 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002111}
2112
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002113/// ParseClass - Parse a tblgen class definition.
2114///
2115/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2116///
2117bool TGParser::ParseClass() {
2118 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2119 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002120
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002121 if (Lex.getCode() != tgtok::Id)
2122 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002123
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002124 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2125 if (CurRec) {
2126 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002127 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002128 !CurRec->getSuperClasses().empty() ||
2129 !CurRec->getTemplateArgs().empty())
Craig Topper85c07002015-04-30 05:54:22 +00002130 return TokError("Class '" + CurRec->getNameInitAsString() +
2131 "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002132 } else {
2133 // If this is the first reference to this class, create and add it.
Hans Wennborgffbbd532014-11-30 00:31:49 +00002134 auto NewRec =
2135 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
Craig Toppercdab2322014-11-29 05:52:51 +00002136 CurRec = NewRec.get();
2137 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002138 }
2139 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002140
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002141 // If there are template args, parse them.
2142 if (Lex.getCode() == tgtok::less)
2143 if (ParseTemplateArgList(CurRec))
2144 return true;
2145
2146 // Finally, parse the object body.
2147 return ParseObjectBody(CurRec);
2148}
2149
2150/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2151/// of LetRecords.
2152///
2153/// LetList ::= LetItem (',' LetItem)*
2154/// LetItem ::= ID OptionalRangeList '=' Value
2155///
2156std::vector<LetRecord> TGParser::ParseLetList() {
2157 std::vector<LetRecord> Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002158
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002159 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002160 if (Lex.getCode() != tgtok::Id) {
2161 TokError("expected identifier in let definition");
2162 return std::vector<LetRecord>();
2163 }
2164 std::string Name = Lex.getCurStrVal();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002165 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002166 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002167
2168 // Check for an optional RangeList.
2169 std::vector<unsigned> Bits;
Bob Wilson7248f862009-11-22 04:24:42 +00002170 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002171 return std::vector<LetRecord>();
2172 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002173
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002174 if (Lex.getCode() != tgtok::equal) {
2175 TokError("expected '=' in let expression");
2176 return std::vector<LetRecord>();
2177 }
2178 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002179
Craig Topper011817a2014-04-09 04:50:04 +00002180 Init *Val = ParseValue(nullptr);
2181 if (!Val) return std::vector<LetRecord>();
Bob Wilson7248f862009-11-22 04:24:42 +00002182
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002183 // Now that we have everything, add the record.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00002184 Result.emplace_back(std::move(Name), std::move(Bits), Val, NameLoc);
Bob Wilson7248f862009-11-22 04:24:42 +00002185
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002186 if (Lex.getCode() != tgtok::comma)
2187 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002188 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002189 }
2190}
2191
2192/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002193/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002194///
2195/// Object ::= LET LetList IN '{' ObjectList '}'
2196/// Object ::= LET LetList IN Object
2197///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002198bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002199 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2200 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002201
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002202 // Add this entry to the let stack.
2203 std::vector<LetRecord> LetInfo = ParseLetList();
2204 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002205 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002206
2207 if (Lex.getCode() != tgtok::In)
2208 return TokError("expected 'in' at end of top-level 'let'");
2209 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002210
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002211 // If this is a scalar let, just handle it now
2212 if (Lex.getCode() != tgtok::l_brace) {
2213 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002214 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002215 return true;
2216 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002217 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002218 // Otherwise, this is a group let.
2219 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002220
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002221 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002222 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002223 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002224
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002225 if (Lex.getCode() != tgtok::r_brace) {
2226 TokError("expected '}' at end of top level let command");
2227 return Error(BraceLoc, "to match this '{'");
2228 }
2229 Lex.Lex();
2230 }
Bob Wilson7248f862009-11-22 04:24:42 +00002231
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002232 // Outside this let scope, this let block is not active.
2233 LetStack.pop_back();
2234 return false;
2235}
2236
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002237/// ParseMultiClass - Parse a multiclass definition.
2238///
Bob Wilson3d948162009-04-28 19:41:44 +00002239/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002240/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2241/// MultiClassObject ::= DefInst
2242/// MultiClassObject ::= MultiClassInst
2243/// MultiClassObject ::= DefMInst
2244/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2245/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002246///
2247bool TGParser::ParseMultiClass() {
2248 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2249 Lex.Lex(); // Eat the multiclass token.
2250
2251 if (Lex.getCode() != tgtok::Id)
2252 return TokError("expected identifier after multiclass for name");
2253 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002254
Craig Topper7adf2bf2014-12-11 05:25:30 +00002255 auto Result =
2256 MultiClasses.insert(std::make_pair(Name,
2257 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2258
2259 if (!Result.second)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002260 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002261
Craig Topper7adf2bf2014-12-11 05:25:30 +00002262 CurMultiClass = Result.first->second.get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002263 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002264
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002265 // If there are template args, parse them.
2266 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002267 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002268 return true;
2269
David Greene7049e792009-04-24 16:55:41 +00002270 bool inherits = false;
2271
David Greene753ed8f2009-04-22 16:42:54 +00002272 // If there are submulticlasses, parse them.
2273 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002274 inherits = true;
2275
David Greene753ed8f2009-04-22 16:42:54 +00002276 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002277
David Greene753ed8f2009-04-22 16:42:54 +00002278 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002279 SubMultiClassReference SubMultiClass =
2280 ParseSubMultiClassReference(CurMultiClass);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002281 while (true) {
David Greene753ed8f2009-04-22 16:42:54 +00002282 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002283 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002284
David Greene753ed8f2009-04-22 16:42:54 +00002285 // Add it.
2286 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2287 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002288
David Greene753ed8f2009-04-22 16:42:54 +00002289 if (Lex.getCode() != tgtok::comma) break;
2290 Lex.Lex(); // eat ','.
2291 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2292 }
2293 }
2294
David Greene7049e792009-04-24 16:55:41 +00002295 if (Lex.getCode() != tgtok::l_brace) {
2296 if (!inherits)
2297 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002298 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002299 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002300 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002301 } else {
David Greene7049e792009-04-24 16:55:41 +00002302 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2303 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002304
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002305 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002306 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002307 default:
2308 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
2309 case tgtok::Let:
2310 case tgtok::Def:
2311 case tgtok::Defm:
2312 case tgtok::Foreach:
2313 if (ParseObject(CurMultiClass))
2314 return true;
2315 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002316 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002317 }
David Greene7049e792009-04-24 16:55:41 +00002318 Lex.Lex(); // eat the '}'.
2319 }
Bob Wilson7248f862009-11-22 04:24:42 +00002320
Craig Topper011817a2014-04-09 04:50:04 +00002321 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002322 return false;
2323}
2324
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002325Record *TGParser::InstantiateMulticlassDef(MultiClass &MC, Record *DefProto,
2326 Init *&DefmPrefix,
2327 SMRange DefmPrefixRange,
2328 ArrayRef<Init *> TArgs,
2329 std::vector<Init *> &TemplateVals) {
David Greene5d5d88c2011-10-19 13:04:31 +00002330 // We need to preserve DefProto so it can be reused for later
2331 // instantiations, so create a new Record to inherit from it.
2332
David Greenedb445972011-10-05 22:42:07 +00002333 // Add in the defm name. If the defm prefix is empty, give each
2334 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2335 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2336 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002337
Jordan Roseabdd99b2013-01-10 18:50:05 +00002338 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002339 if (!DefmPrefix) {
David Greene5d5d88c2011-10-19 13:04:31 +00002340 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002341 IsAnonymous = true;
2342 }
David Greene5d5d88c2011-10-19 13:04:31 +00002343
2344 Init *DefName = DefProto->getNameInit();
Sean Silvafb509ed2012-10-10 20:24:43 +00002345 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002346
Craig Topper011817a2014-04-09 04:50:04 +00002347 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002348 // We have a fully expanded string so there are no operators to
2349 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002350 DefName =
2351 BinOpInit::get(BinOpInit::STRCONCAT,
2352 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2353 StringRecTy::get())->Fold(DefProto, &MC),
2354 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2355 }
David Greene5d5d88c2011-10-19 13:04:31 +00002356
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002357 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002358 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002359 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Craig Topper84138712014-11-29 05:31:10 +00002360 auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002361
2362 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002363 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002364 Ref.Rec = DefProto;
Craig Topper84138712014-11-29 05:31:10 +00002365 AddSubClass(CurRec.get(), Ref);
David Greenedb445972011-10-05 22:42:07 +00002366
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002367 // Set the value for NAME. We don't resolve references to it 'til later,
2368 // though, so that uses in nested multiclass names don't get
2369 // confused.
Craig Toppercfd81732016-01-04 03:15:08 +00002370 if (SetValue(CurRec.get(), Ref.RefRange.Start, "NAME", None, DefmPrefix,
Craig Topper1e23ed92016-01-04 03:05:14 +00002371 /*AllowSelfAssignment*/true)) {
Craig Topper85c07002015-04-30 05:54:22 +00002372 Error(DefmPrefixRange.Start, "Could not resolve " +
2373 CurRec->getNameInitAsString() + ":NAME to '" +
2374 DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002375 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002376 }
David Greene8bf0d722011-10-19 13:04:35 +00002377
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002378 // If the DefNameString didn't resolve, we probably have a reference to
2379 // NAME and need to replace it. We need to do at least this much greedily,
2380 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002381 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002382 RecordVal *DefNameRV = CurRec->getValue("NAME");
2383 CurRec->resolveReferencesTo(DefNameRV);
2384 }
2385
2386 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002387 // Now that we're at the top level, resolve all NAME references
2388 // in the resultant defs that weren't in the def names themselves.
2389 RecordVal *DefNameRV = CurRec->getValue("NAME");
2390 CurRec->resolveReferencesTo(DefNameRV);
2391
Hal Finkeld2497362015-05-21 04:32:56 +00002392 // Check if the name is a complex pattern.
2393 // If so, resolve it.
2394 DefName = CurRec->getNameInit();
2395 DefNameString = dyn_cast<StringInit>(DefName);
2396
2397 // OK the pattern is more complex than simply using NAME.
2398 // Let's use the heavy weaponery.
2399 if (!DefNameString) {
2400 ResolveMulticlassDefArgs(MC, CurRec.get(), DefmPrefixRange.Start,
2401 Lex.getLoc(), TArgs, TemplateVals,
2402 false/*Delete args*/);
2403 DefName = CurRec->getNameInit();
2404 DefNameString = dyn_cast<StringInit>(DefName);
2405
2406 if (!DefNameString)
2407 DefName = DefName->convertInitializerTo(StringRecTy::get());
2408
2409 // We ran out of options here...
2410 DefNameString = dyn_cast<StringInit>(DefName);
2411 if (!DefNameString) {
2412 PrintFatalError(CurRec->getLoc()[CurRec->getLoc().size() - 1],
2413 DefName->getAsUnquotedString() + " is not a string.");
2414 return nullptr;
2415 }
2416
2417 CurRec->setName(DefName);
2418 }
2419
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002420 // Now that NAME references are resolved and we're at the top level of
2421 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002422 // currently in a multiclass, it means this defm appears inside a
2423 // multiclass and its name won't be fully resolvable until we see
Hal Finkeld2497362015-05-21 04:32:56 +00002424 // the top-level defm. Therefore, we don't add this to the
2425 // RecordKeeper at this point. If we did we could get duplicate
David Greene8bf0d722011-10-19 13:04:35 +00002426 // defs as more than one probably refers to NAME or some other
2427 // common internal placeholder.
2428
2429 // Ensure redefinition doesn't happen.
2430 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002431 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002432 "' already defined, instantiating defm with subdef '" +
2433 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002434 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002435 }
2436
Craig Topper84138712014-11-29 05:31:10 +00002437 Record *CurRecSave = CurRec.get(); // Keep a copy before we release.
Craig Toppercdab2322014-11-29 05:52:51 +00002438 Records.addDef(std::move(CurRec));
Craig Topper84138712014-11-29 05:31:10 +00002439 return CurRecSave;
David Greene8bf0d722011-10-19 13:04:35 +00002440 }
2441
Craig Topper84138712014-11-29 05:31:10 +00002442 // FIXME This is bad but the ownership transfer to caller is pretty messy.
2443 // The unique_ptr in this function at least protects the exits above.
2444 return CurRec.release();
David Greenedb445972011-10-05 22:42:07 +00002445}
2446
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002447bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC, Record *CurRec,
2448 SMLoc DefmPrefixLoc, SMLoc SubClassLoc,
2449 ArrayRef<Init *> TArgs,
David Greenedb445972011-10-05 22:42:07 +00002450 std::vector<Init *> &TemplateVals,
2451 bool DeleteArgs) {
2452 // Loop over all of the template arguments, setting them to the specified
2453 // value or leaving them as the default if necessary.
2454 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2455 // Check if a value is specified for this temp-arg.
2456 if (i < TemplateVals.size()) {
2457 // Set it now.
Craig Toppercfd81732016-01-04 03:15:08 +00002458 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], None, TemplateVals[i]))
David Greenedb445972011-10-05 22:42:07 +00002459 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002460
David Greenedb445972011-10-05 22:42:07 +00002461 // Resolve it next.
2462 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2463
2464 if (DeleteArgs)
2465 // Now remove it.
2466 CurRec->removeValue(TArgs[i]);
Craig Toppera9642b42015-05-04 01:35:39 +00002467
David Greenedb445972011-10-05 22:42:07 +00002468 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Craig Topper85c07002015-04-30 05:54:22 +00002469 return Error(SubClassLoc, "value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00002470 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +00002471 ") of multiclassclass '" + MC.Rec.getNameInitAsString() +
2472 "'");
David Greenedb445972011-10-05 22:42:07 +00002473 }
2474 }
2475 return false;
2476}
2477
2478bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2479 Record *CurRec,
2480 Record *DefProto,
2481 SMLoc DefmPrefixLoc) {
2482 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002483 if (ApplyLetStack(CurRec))
2484 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002485
David Greenedb445972011-10-05 22:42:07 +00002486 // Don't create a top level definition for defm inside multiclasses,
2487 // instead, only update the prototypes and bind the template args
2488 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002489 if (!CurMultiClass)
2490 return false;
Craig Toppereb4d7c62015-04-29 04:43:36 +00002491 for (const auto &Proto : CurMultiClass->DefPrototypes)
2492 if (Proto->getNameInit() == CurRec->getNameInit())
Sean Silvacc951b22013-01-09 05:28:12 +00002493 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2494 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002495 CurMultiClass->DefPrototypes.push_back(std::unique_ptr<Record>(CurRec));
David Greenedb445972011-10-05 22:42:07 +00002496
Sean Silvacc951b22013-01-09 05:28:12 +00002497 // Copy the template arguments for the multiclass into the new def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002498 for (Init * TA : CurMultiClass->Rec.getTemplateArgs()) {
2499 const RecordVal *RV = CurMultiClass->Rec.getValue(TA);
Sean Silvacc951b22013-01-09 05:28:12 +00002500 assert(RV && "Template arg doesn't exist?");
2501 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002502 }
2503
2504 return false;
2505}
2506
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002507/// ParseDefm - Parse the instantiation of a multiclass.
2508///
2509/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2510///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002511bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002512 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002513 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002514 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002515
Craig Topperb21afc62013-01-07 05:09:33 +00002516 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002517 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002518 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002519
Jordan Rosef12e8a92013-01-10 18:50:11 +00002520 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002521 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002522 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002523
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002524 // Keep track of the new generated record definitions.
2525 std::vector<Record*> NewRecDefs;
2526
2527 // This record also inherits from a regular class (non-multiclass)?
2528 bool InheritFromClass = false;
2529
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002530 // eat the colon.
2531 Lex.Lex();
2532
Chris Lattner526c8cb2009-06-21 03:39:35 +00002533 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002534 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002535
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002536 while (true) {
Craig Topper011817a2014-04-09 04:50:04 +00002537 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002538
2539 // To instantiate a multiclass, we need to first get the multiclass, then
2540 // instantiate each def contained in the multiclass with the SubClassRef
2541 // template parameters.
Craig Topper7adf2bf2014-12-11 05:25:30 +00002542 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper4ca40012014-11-30 01:20:17 +00002543 assert(MC && "Didn't lookup multiclass correctly?");
David Greeneaf8ee2c2011-07-29 22:43:06 +00002544 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002545
2546 // Verify that the correct number of template arguments were specified.
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002547 ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002548 if (TArgs.size() < TemplateVals.size())
2549 return Error(SubClassLoc,
2550 "more template args specified than multiclass expects");
2551
2552 // Loop over all the def's in the multiclass, instantiating each one.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002553 for (const std::unique_ptr<Record> &DefProto : MC->DefPrototypes) {
Hal Finkeld2497362015-05-21 04:32:56 +00002554 // The record name construction goes as follow:
2555 // - If the def name is a string, prepend the prefix.
2556 // - If the def name is a more complex pattern, use that pattern.
Craig Topper7f36be92016-02-26 06:50:27 +00002557 // As a result, the record is instantiated before resolving
Hal Finkeld2497362015-05-21 04:32:56 +00002558 // arguments, as it would make its name a string.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002559 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto.get(), DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002560 SMRange(DefmLoc,
Hal Finkeld2497362015-05-21 04:32:56 +00002561 DefmPrefixEndLoc),
2562 TArgs, TemplateVals);
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002563 if (!CurRec)
2564 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002565
Craig Topper7f36be92016-02-26 06:50:27 +00002566 // Now that the record is instantiated, we can resolve arguments.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002567 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002568 TArgs, TemplateVals, true/*Delete args*/))
2569 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002570
Craig Toppereb4d7c62015-04-29 04:43:36 +00002571 if (ResolveMulticlassDef(*MC, CurRec, DefProto.get(), DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002572 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002573
Adam Nemete5a07162014-09-16 17:14:13 +00002574 // Defs that can be used by other definitions should be fully resolved
2575 // before any use.
2576 if (DefProto->isResolveFirst() && !CurMultiClass) {
2577 CurRec->resolveReferences();
2578 CurRec->setResolveFirst(false);
2579 }
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002580 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002581 }
2582
David Greenedb445972011-10-05 22:42:07 +00002583
David Greenef00919a2009-04-22 22:17:51 +00002584 if (Lex.getCode() != tgtok::comma) break;
2585 Lex.Lex(); // eat ','.
2586
Craig Topper998a39a2013-08-20 04:22:09 +00002587 if (Lex.getCode() != tgtok::Id)
2588 return TokError("expected identifier");
2589
David Greenef00919a2009-04-22 22:17:51 +00002590 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002591
2592 // A defm can inherit from regular classes (non-multiclass) as
2593 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002594 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002595
2596 if (InheritFromClass)
2597 break;
2598
Craig Topper011817a2014-04-09 04:50:04 +00002599 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002600 }
2601
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002602 if (InheritFromClass) {
2603 // Process all the classes to inherit as if they were part of a
2604 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002605 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002606 while (true) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002607 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002608 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002609
2610 // Get the expanded definition prototypes and teach them about
2611 // the record values the current class to inherit has
Craig Toppereb4d7c62015-04-29 04:43:36 +00002612 for (Record *CurRec : NewRecDefs) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002613 // Add it.
2614 if (AddSubClass(CurRec, SubClass))
2615 return true;
2616
Sean Silvacb1a75e2013-01-09 04:49:14 +00002617 if (ApplyLetStack(CurRec))
2618 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002619 }
2620
2621 if (Lex.getCode() != tgtok::comma) break;
2622 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002623 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002624 }
2625 }
2626
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002627 if (!CurMultiClass)
Craig Toppereb4d7c62015-04-29 04:43:36 +00002628 for (Record *CurRec : NewRecDefs)
David Greene50c09122011-08-10 18:27:46 +00002629 // See Record::setName(). This resolve step will see any new
2630 // name for the def that might have been created when resolving
2631 // inheritance, values and arguments above.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002632 CurRec->resolveReferences();
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002633
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002634 if (Lex.getCode() != tgtok::semi)
2635 return TokError("expected ';' at end of defm");
2636 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002637
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002638 return false;
2639}
2640
2641/// ParseObject
2642/// Object ::= ClassInst
2643/// Object ::= DefInst
2644/// Object ::= MultiClassInst
2645/// Object ::= DefMInst
2646/// Object ::= LETCommand '{' ObjectList '}'
2647/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002648bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002649 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002650 default:
2651 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002652 case tgtok::Let: return ParseTopLevelLet(MC);
2653 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002654 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002655 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002656 case tgtok::Class: return ParseClass();
2657 case tgtok::MultiClass: return ParseMultiClass();
2658 }
2659}
2660
2661/// ParseObjectList
2662/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002663bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002664 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002665 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002666 return true;
2667 }
2668 return false;
2669}
2670
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002671bool TGParser::ParseFile() {
2672 Lex.Lex(); // Prime the lexer.
2673 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002674
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002675 // If we have unread input at the end of the file, report it.
2676 if (Lex.getCode() == tgtok::Eof)
2677 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002678
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002679 return TokError("Unexpected input at top level");
2680}