blob: 59b837f41872c66fef35317d667e05c79e13fce6 [file] [log] [blame]
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001//===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Parser for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
14#include "TGParser.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000015#include "llvm/ADT/None.h"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000016#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/SmallVector.h"
Chris Lattnerf4127dd2007-11-22 20:49:04 +000018#include "llvm/ADT/StringExtras.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000019#include "llvm/Support/Casting.h"
20#include "llvm/Support/Compiler.h"
21#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/TableGen/Record.h"
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000024#include <algorithm>
Eugene Zelenko33d7b762016-08-23 17:14:32 +000025#include <cassert>
26#include <cstdint>
27
Chris Lattnerf4127dd2007-11-22 20:49:04 +000028using namespace llvm;
29
30//===----------------------------------------------------------------------===//
31// Support Code for the Semantic Actions.
32//===----------------------------------------------------------------------===//
33
34namespace llvm {
Eugene Zelenko33d7b762016-08-23 17:14:32 +000035
Chris Lattnerf4127dd2007-11-22 20:49:04 +000036struct SubClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000037 SMRange RefRange;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000038 Record *Rec;
Matthias Braunc66e7552016-12-05 06:41:54 +000039 SmallVector<Init*, 4> TemplateArgs;
Eugene Zelenko33d7b762016-08-23 17:14:32 +000040
Craig Topper011817a2014-04-09 04:50:04 +000041 SubClassReference() : Rec(nullptr) {}
David Greene7049e792009-04-24 16:55:41 +000042
Craig Topper011817a2014-04-09 04:50:04 +000043 bool isInvalid() const { return Rec == nullptr; }
Chris Lattnerf4127dd2007-11-22 20:49:04 +000044};
David Greene753ed8f2009-04-22 16:42:54 +000045
46struct SubMultiClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000047 SMRange RefRange;
David Greene753ed8f2009-04-22 16:42:54 +000048 MultiClass *MC;
Matthias Braunc66e7552016-12-05 06:41:54 +000049 SmallVector<Init*, 4> TemplateArgs;
Eugene Zelenko33d7b762016-08-23 17:14:32 +000050
Craig Topper011817a2014-04-09 04:50:04 +000051 SubMultiClassReference() : MC(nullptr) {}
Bob Wilson3d948162009-04-28 19:41:44 +000052
Craig Topper011817a2014-04-09 04:50:04 +000053 bool isInvalid() const { return MC == nullptr; }
David Greene7049e792009-04-24 16:55:41 +000054 void dump() const;
David Greene753ed8f2009-04-22 16:42:54 +000055};
David Greene7049e792009-04-24 16:55:41 +000056
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
Matthias Braun215ff842016-12-05 07:35:13 +0000342 if (SetValue(IterRec.get(), Loc, IterVar->getNameInit(), 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.
Matthias Braun215ff842016-12-05 07:35:13 +0000346 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getNameInit()));
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000347
348 // Remove it.
Matthias Braun215ff842016-12-05 07:35:13 +0000349 IterRec->removeValue(IterVar->getNameInit());
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000350 }
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
Matthias Braunc66e7552016-12-05 06:41:54 +0000488 ParseValueList(Result.TemplateArgs, 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
Matthias Braunc66e7552016-12-05 06:41:54 +0000533 ParseValueList(Result.TemplateArgs, &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
Matthias Braunc66e7552016-12-05 06:41:54 +0000554bool TGParser::ParseRangePiece(SmallVectorImpl<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///
Matthias Braunc66e7552016-12-05 06:41:54 +0000598void TGParser::ParseRangeList(SmallVectorImpl<unsigned> &Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000599 // Parse the first piece.
Matthias Braunc66e7552016-12-05 06:41:54 +0000600 if (ParseRangePiece(Result)) {
601 Result.clear();
602 return;
603 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000604 while (Lex.getCode() == tgtok::comma) {
605 Lex.Lex(); // Eat the comma.
606
607 // Parse the next range piece.
Matthias Braunc66e7552016-12-05 06:41:54 +0000608 if (ParseRangePiece(Result)) {
609 Result.clear();
610 return;
611 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000612 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000613}
614
615/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
616/// OptionalRangeList ::= '<' RangeList '>'
617/// OptionalRangeList ::= /*empty*/
Matthias Braunc66e7552016-12-05 06:41:54 +0000618bool TGParser::ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000619 if (Lex.getCode() != tgtok::less)
620 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000621
Chris Lattner526c8cb2009-06-21 03:39:35 +0000622 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000623 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000624
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000625 // Parse the range list.
Matthias Braunc66e7552016-12-05 06:41:54 +0000626 ParseRangeList(Ranges);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000627 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000628
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000629 if (Lex.getCode() != tgtok::greater) {
630 TokError("expected '>' at end of range list");
631 return Error(StartLoc, "to match this '<'");
632 }
633 Lex.Lex(); // eat the '>'.
634 return false;
635}
636
637/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
638/// OptionalBitList ::= '{' RangeList '}'
639/// OptionalBitList ::= /*empty*/
Matthias Braunc66e7552016-12-05 06:41:54 +0000640bool TGParser::ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000641 if (Lex.getCode() != tgtok::l_brace)
642 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000643
Chris Lattner526c8cb2009-06-21 03:39:35 +0000644 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000645 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000646
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000647 // Parse the range list.
Matthias Braunc66e7552016-12-05 06:41:54 +0000648 ParseRangeList(Ranges);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000649 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000650
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000651 if (Lex.getCode() != tgtok::r_brace) {
652 TokError("expected '}' at end of bit list");
653 return Error(StartLoc, "to match this '{'");
654 }
655 Lex.Lex(); // eat the '}'.
656 return false;
657}
658
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000659/// ParseType - Parse and return a tblgen type. This returns null on error.
660///
661/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000662/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000663/// Type ::= BIT // bit type
664/// Type ::= BITS '<' INTVAL '>' // bits<x> type
665/// Type ::= INT // int type
666/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000667/// Type ::= DAG // dag type
668/// Type ::= ClassID // Record Type
669///
670RecTy *TGParser::ParseType() {
671 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000672 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000673 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Tim Northover88403d72016-07-05 21:22:55 +0000674 case tgtok::Code: Lex.Lex(); return CodeRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000675 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
676 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000677 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000678 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000679 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000680 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000681 case tgtok::Bits: {
682 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
683 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000684 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000685 }
686 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
687 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000688 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000689 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000690 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000691 if (Lex.Lex() != tgtok::greater) { // Eat count.
692 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000693 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000694 }
695 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000696 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000697 }
698 case tgtok::List: {
699 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
700 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000701 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000702 }
703 Lex.Lex(); // Eat '<'
704 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000705 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000706
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000707 if (Lex.getCode() != tgtok::greater) {
708 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000709 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000710 }
711 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000712 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000713 }
Bob Wilson7248f862009-11-22 04:24:42 +0000714 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000715}
716
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000717/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
718/// has already been read.
Matthias Braun215ff842016-12-05 07:35:13 +0000719Init *TGParser::ParseIDValue(Record *CurRec, StringInit *Name, SMLoc NameLoc,
David Greened4263a62011-10-19 13:04:20 +0000720 IDParseMode Mode) {
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 Braun215ff842016-12-05 07:35:13 +0000725 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
David Greenedb10e692011-10-19 13:02:42 +0000726
David Greene47a665e2011-10-05 22:42:54 +0000727 if (CurMultiClass)
Matthias Braun215ff842016-12-05 07:35:13 +0000728 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
David Greenedb10e692011-10-19 13:02:42 +0000729 "::");
David Greene47a665e2011-10-05 22:42:54 +0000730
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000731 if (CurRec->isTemplateArg(TemplateArgName)) {
732 const RecordVal *RV = CurRec->getValue(TemplateArgName);
733 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000734 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000735 }
Matthias Braun215ff842016-12-05 07:35:13 +0000736 }
Bob Wilson7248f862009-11-22 04:24:42 +0000737
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000738 if (CurMultiClass) {
Matthias Braun215ff842016-12-05 07:35:13 +0000739 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name, "::");
David Greenedb10e692011-10-19 13:02:42 +0000740
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000741 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
742 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
743 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000744 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000745 }
746 }
Bob Wilson7248f862009-11-22 04:24:42 +0000747
David Greenefb927af2012-02-22 16:09:41 +0000748 // If this is in a foreach loop, make sure it's not a loop iterator
Craig Toppereb4d7c62015-04-29 04:43:36 +0000749 for (const auto &L : Loops) {
750 VarInit *IterVar = dyn_cast<VarInit>(L.IterVar);
Matthias Braun215ff842016-12-05 07:35:13 +0000751 if (IterVar && IterVar->getNameInit() == Name)
David Greenefb927af2012-02-22 16:09:41 +0000752 return IterVar;
753 }
754
David Greene232bd602011-10-19 13:04:21 +0000755 if (Mode == ParseNameMode)
Matthias Braun215ff842016-12-05 07:35:13 +0000756 return Name;
David Greene232bd602011-10-19 13:04:21 +0000757
Matthias Braun215ff842016-12-05 07:35:13 +0000758 if (Record *D = Records.getDef(Name->getValue()))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000759 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000760
David Greene232bd602011-10-19 13:04:21 +0000761 if (Mode == ParseValueMode) {
Matthias Braun215ff842016-12-05 07:35:13 +0000762 Error(NameLoc, "Variable not defined: '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000763 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000764 }
Craig Toppera9642b42015-05-04 01:35:39 +0000765
Matthias Braun215ff842016-12-05 07:35:13 +0000766 return Name;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000767}
768
David Greene5d0c0512009-05-14 20:54:48 +0000769/// ParseOperation - Parse an operator. This returns null on error.
770///
771/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
772///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000773Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000774 switch (Lex.getCode()) {
775 default:
776 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000777 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000778 case tgtok::XHead:
779 case tgtok::XTail:
780 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000781 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
782 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000783 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000784
David Greenee8f3b272009-05-14 21:22:49 +0000785 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000786 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000787 case tgtok::XCast:
788 Lex.Lex(); // eat the operation
789 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000790
David Greenee8f3b272009-05-14 21:22:49 +0000791 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000792
Craig Topper011817a2014-04-09 04:50:04 +0000793 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000794 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000795 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000796 }
David Greene5d0c0512009-05-14 20:54:48 +0000797
David Greenee8f3b272009-05-14 21:22:49 +0000798 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000799 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000800 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000801 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000802 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000803 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000804 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000805 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000806 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000807 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000808 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000809 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000810 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000811 break;
David Greenee8f3b272009-05-14 21:22:49 +0000812 }
813 if (Lex.getCode() != tgtok::l_paren) {
814 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000815 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000816 }
817 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000818
David Greeneaf8ee2c2011-07-29 22:43:06 +0000819 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000820 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000821
Craig Topper85c07002015-04-30 05:54:22 +0000822 if (Code == UnOpInit::HEAD ||
823 Code == UnOpInit::TAIL ||
824 Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000825 ListInit *LHSl = dyn_cast<ListInit>(LHS);
826 StringInit *LHSs = dyn_cast<StringInit>(LHS);
827 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000828 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000829 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000830 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000831 }
832 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000833 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
834 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000835 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000836 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000837 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000838 }
839 }
840
Craig Topper85c07002015-04-30 05:54:22 +0000841 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
Craig Topper011817a2014-04-09 04:50:04 +0000842 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000843 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000844 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000845 }
Bob Wilson7248f862009-11-22 04:24:42 +0000846
Craig Topperec9072d2015-05-14 05:53:53 +0000847 if (LHSl && LHSl->empty()) {
David Greened571b3c2009-05-14 22:38:31 +0000848 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000849 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000850 }
851 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000852 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000853 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000854 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000855 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000856 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000857 }
Craig Toppera9642b42015-05-04 01:35:39 +0000858 Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
859 : ListRecTy::get(Itemt->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000860 } else {
David Greened571b3c2009-05-14 22:38:31 +0000861 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000862 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000863 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000864 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000865 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000866 }
Craig Toppera9642b42015-05-04 01:35:39 +0000867 Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
David Greened571b3c2009-05-14 22:38:31 +0000868 }
869 }
870 }
871
David Greenee8f3b272009-05-14 21:22:49 +0000872 if (Lex.getCode() != tgtok::r_paren) {
873 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000874 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000875 }
876 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000877 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000878 }
David Greene5d0c0512009-05-14 20:54:48 +0000879
880 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000881 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000882 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000883 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +0000884 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000885 case tgtok::XSRL:
886 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000887 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000888 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000889 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000890 tgtok::TokKind OpTok = Lex.getCode();
891 SMLoc OpLoc = Lex.getLoc();
892 Lex.Lex(); // eat the operation
893
David Greene5d0c0512009-05-14 20:54:48 +0000894 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000895 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000896
Chris Lattner61ea00b2010-10-05 23:58:18 +0000897 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000898 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000899 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000900 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000901 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000902 case tgtok::XOR: Code = BinOpInit::OR; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000903 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
904 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
905 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
906 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000907 case tgtok::XListConcat:
908 Code = BinOpInit::LISTCONCAT;
909 // We don't know the list type until we parse the first argument
910 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000911 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000912 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000913 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000914 break;
David Greene5d0c0512009-05-14 20:54:48 +0000915 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000916
David Greene5d0c0512009-05-14 20:54:48 +0000917 if (Lex.getCode() != tgtok::l_paren) {
918 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000919 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000920 }
921 Lex.Lex(); // eat the '('
922
David Greeneaf8ee2c2011-07-29 22:43:06 +0000923 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000924
Chris Lattner61ea00b2010-10-05 23:58:18 +0000925 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000926 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000927
Chris Lattner61ea00b2010-10-05 23:58:18 +0000928 while (Lex.getCode() == tgtok::comma) {
929 Lex.Lex(); // eat the ','
930
931 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000932 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000933 }
David Greene5d0c0512009-05-14 20:54:48 +0000934
935 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000936 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000937 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000938 }
939 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000940
Daniel Sanders314e80e2014-05-07 10:13:19 +0000941 // If we are doing !listconcat, we should know the type by now
942 if (OpTok == tgtok::XListConcat) {
943 if (VarInit *Arg0 = dyn_cast<VarInit>(InitList[0]))
944 Type = Arg0->getType();
945 else if (ListInit *Arg0 = dyn_cast<ListInit>(InitList[0]))
946 Type = Arg0->getType();
947 else {
Matthias Braun8c209aa2017-01-28 02:02:38 +0000948 InitList[0]->print(errs());
Daniel Sanders314e80e2014-05-07 10:13:19 +0000949 Error(OpLoc, "expected a list");
950 return nullptr;
951 }
952 }
953
Chris Lattner61ea00b2010-10-05 23:58:18 +0000954 // We allow multiple operands to associative operators like !strconcat as
955 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000956 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000957 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000958 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000959 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
960 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000961 InitList.back() = RHS;
962 }
963 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000964
Chris Lattner61ea00b2010-10-05 23:58:18 +0000965 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000966 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000967 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000968
Chris Lattner61ea00b2010-10-05 23:58:18 +0000969 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +0000970 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000971 }
972
David Greene3587eed2009-05-14 23:26:46 +0000973 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +0000974 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +0000975 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
976 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000977 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000978
David Greene98ed3c72009-05-14 21:54:42 +0000979 tgtok::TokKind LexCode = Lex.getCode();
980 Lex.Lex(); // eat the operation
981 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +0000982 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +0000983 case tgtok::XIf:
984 Code = TernOpInit::IF;
985 break;
David Greenee917fff2009-05-14 22:23:47 +0000986 case tgtok::XForEach:
987 Code = TernOpInit::FOREACH;
988 break;
David Greene98ed3c72009-05-14 21:54:42 +0000989 case tgtok::XSubst:
990 Code = TernOpInit::SUBST;
991 break;
992 }
993 if (Lex.getCode() != tgtok::l_paren) {
994 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000995 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +0000996 }
997 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000998
David Greeneaf8ee2c2011-07-29 22:43:06 +0000999 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001000 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001001
David Greene98ed3c72009-05-14 21:54:42 +00001002 if (Lex.getCode() != tgtok::comma) {
1003 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001004 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001005 }
1006 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001007
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001008 Init *MHS = ParseValue(CurRec, ItemType);
1009 if (!MHS)
1010 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001011
David Greene98ed3c72009-05-14 21:54:42 +00001012 if (Lex.getCode() != tgtok::comma) {
1013 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001014 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001015 }
1016 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001017
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001018 Init *RHS = ParseValue(CurRec, ItemType);
1019 if (!RHS)
1020 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001021
David Greene98ed3c72009-05-14 21:54:42 +00001022 if (Lex.getCode() != tgtok::r_paren) {
1023 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001024 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001025 }
1026 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001027
David Greene98ed3c72009-05-14 21:54:42 +00001028 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001029 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001030 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001031 RecTy *MHSTy = nullptr;
1032 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001033
Sean Silvafb509ed2012-10-10 20:24:43 +00001034 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001035 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001036 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001037 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001038 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001039 MHSTy = BitRecTy::get();
1040
Sean Silvafb509ed2012-10-10 20:24:43 +00001041 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001042 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001043 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001044 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001045 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001046 RHSTy = BitRecTy::get();
1047
1048 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001049 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001050 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001051 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001052 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001053
1054 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001055 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001056 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001057 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001058
1059 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1060 Type = RHSTy;
1061 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1062 Type = MHSTy;
Bob Wilson7248f862009-11-22 04:24:42 +00001063 } else {
David Greene3587eed2009-05-14 23:26:46 +00001064 TokError("inconsistent types for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001065 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001066 }
1067 break;
1068 }
David Greenee917fff2009-05-14 22:23:47 +00001069 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001070 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
Craig Topper011817a2014-04-09 04:50:04 +00001071 if (!MHSt) {
David Greenee917fff2009-05-14 22:23:47 +00001072 TokError("could not get type for !foreach");
Craig Topper011817a2014-04-09 04:50:04 +00001073 return nullptr;
David Greenee917fff2009-05-14 22:23:47 +00001074 }
1075 Type = MHSt->getType();
1076 break;
1077 }
David Greene98ed3c72009-05-14 21:54:42 +00001078 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001079 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001080 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001081 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001082 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001083 }
1084 Type = RHSt->getType();
1085 break;
1086 }
1087 }
David Greenee32ebf22011-07-29 19:07:07 +00001088 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001089 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001090 }
David Greene5d0c0512009-05-14 20:54:48 +00001091 }
David Greene5d0c0512009-05-14 20:54:48 +00001092}
1093
1094/// ParseOperatorType - Parse a type for an operator. This returns
1095/// null on error.
1096///
1097/// OperatorType ::= '<' Type '>'
1098///
Dan Gohman1432ef82009-08-12 22:10:57 +00001099RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001100 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001101
1102 if (Lex.getCode() != tgtok::less) {
1103 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001104 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001105 }
1106 Lex.Lex(); // eat the <
1107
1108 Type = ParseType();
1109
Craig Topper011817a2014-04-09 04:50:04 +00001110 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001111 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001112 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001113 }
1114
1115 if (Lex.getCode() != tgtok::greater) {
1116 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001117 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001118 }
1119 Lex.Lex(); // eat the >
1120
1121 return Type;
1122}
1123
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001124/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1125///
1126/// SimpleValue ::= IDValue
1127/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001128/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001129/// SimpleValue ::= CODEFRAGMENT
1130/// SimpleValue ::= '?'
1131/// SimpleValue ::= '{' ValueList '}'
1132/// SimpleValue ::= ID '<' ValueListNE '>'
1133/// SimpleValue ::= '[' ValueList ']'
1134/// SimpleValue ::= '(' IDValue DagArgList ')'
1135/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001136/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001137/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1138/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1139/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001140/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001141/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1142///
David Greened4263a62011-10-19 13:04:20 +00001143Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1144 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001145 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001146 switch (Lex.getCode()) {
1147 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001148 case tgtok::paste:
1149 // This is a leading paste operation. This is deprecated but
1150 // still exists in some .td files. Ignore it.
1151 Lex.Lex(); // Skip '#'.
1152 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001153 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001154 case tgtok::BinaryIntVal: {
1155 auto BinaryVal = Lex.getCurBinaryIntVal();
1156 SmallVector<Init*, 16> Bits(BinaryVal.second);
1157 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001158 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001159 R = BitsInit::get(Bits);
1160 Lex.Lex();
1161 break;
1162 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001163 case tgtok::StrVal: {
1164 std::string Val = Lex.getCurStrVal();
1165 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001166
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001167 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001168 while (Lex.getCode() == tgtok::StrVal) {
1169 Val += Lex.getCurStrVal();
1170 Lex.Lex();
1171 }
Bob Wilson7248f862009-11-22 04:24:42 +00001172
David Greenee32ebf22011-07-29 19:07:07 +00001173 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001174 break;
1175 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001176 case tgtok::CodeFragment:
Tim Northover88403d72016-07-05 21:22:55 +00001177 R = CodeInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001178 Lex.Lex();
1179 break;
1180 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001181 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001182 Lex.Lex();
1183 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001184 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001185 SMLoc NameLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00001186 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001187 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001188 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001189
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001190 // Value ::= ID '<' ValueListNE '>'
1191 if (Lex.Lex() == tgtok::greater) {
1192 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001193 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001194 }
David Greene8618f952009-06-08 20:23:18 +00001195
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001196 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1197 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1198 // body.
Matthias Braun215ff842016-12-05 07:35:13 +00001199 Record *Class = Records.getClass(Name->getValue());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001200 if (!Class) {
Matthias Braun215ff842016-12-05 07:35:13 +00001201 Error(NameLoc, "Expected a class name, got '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001202 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001203 }
David Greene8618f952009-06-08 20:23:18 +00001204
Matthias Braunc66e7552016-12-05 06:41:54 +00001205 SubClassReference SCRef;
1206 ParseValueList(SCRef.TemplateArgs, CurRec, Class);
1207 if (SCRef.TemplateArgs.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001208
David Greene8618f952009-06-08 20:23:18 +00001209 if (Lex.getCode() != tgtok::greater) {
1210 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001211 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001212 }
1213 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001214 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001215
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001216 // Create the new record, set it as CurRec temporarily.
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00001217 auto NewRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), NameLoc,
1218 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001219 Record *NewRec = NewRecOwner.get(); // Keep a copy since we may release.
Jordan Rosef12e8a92013-01-10 18:50:11 +00001220 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001221 SCRef.Rec = Class;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001222 // Add info about the subclass to NewRec.
Craig Topper84138712014-11-29 05:31:10 +00001223 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001224 return nullptr;
Craig Topper84138712014-11-29 05:31:10 +00001225
Hal Finkela8c1f462014-01-02 20:47:09 +00001226 if (!CurMultiClass) {
1227 NewRec->resolveReferences();
Craig Toppercdab2322014-11-29 05:52:51 +00001228 Records.addDef(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001229 } else {
Adam Nemete5a07162014-09-16 17:14:13 +00001230 // This needs to get resolved once the multiclass template arguments are
1231 // known before any use.
1232 NewRec->setResolveFirst(true);
Hal Finkela8c1f462014-01-02 20:47:09 +00001233 // Otherwise, we're inside a multiclass, add it to the multiclass.
Craig Topperc3504c42014-12-11 05:25:33 +00001234 CurMultiClass->DefPrototypes.push_back(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001235
1236 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00001237 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
1238 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Hal Finkela8c1f462014-01-02 20:47:09 +00001239 assert(RV && "Template arg doesn't exist?");
1240 NewRec->addValue(*RV);
1241 }
1242
1243 // We can't return the prototype def here, instead return:
1244 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1245 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1246 assert(MCNameRV && "multiclass record must have a NAME");
1247
1248 return UnOpInit::get(UnOpInit::CAST,
1249 BinOpInit::get(BinOpInit::STRCONCAT,
1250 VarInit::get(MCNameRV->getName(),
1251 MCNameRV->getType()),
1252 NewRec->getNameInit(),
1253 StringRecTy::get()),
1254 Class->getDefInit()->getType());
1255 }
Bob Wilson7248f862009-11-22 04:24:42 +00001256
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001257 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001258 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001259 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001260 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001261 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001262 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001263 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001264
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001265 if (Lex.getCode() != tgtok::r_brace) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001266 ParseValueList(Vals, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001267 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001268 }
1269 if (Lex.getCode() != tgtok::r_brace) {
1270 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001271 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001272 }
1273 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001274
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001275 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001276
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001277 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1278 // first. We'll first read everything in to a vector, then we can reverse
1279 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001280 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001281 // FIXME: The following two loops would not be duplicated
1282 // if the API was a little more orthogonal.
1283
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001284 // bits<n> values are allowed to initialize n bits.
1285 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1286 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1287 NewBits.push_back(BI->getBit((e - i) - 1));
1288 continue;
1289 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001290 // bits<n> can also come from variable initializers.
1291 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1292 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1293 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1294 NewBits.push_back(VI->getBit((e - i) - 1));
1295 continue;
1296 }
1297 // Fallthrough to try convert this to a bit.
1298 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001299 // All other values must be convertible to just a single bit.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001300 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001301 if (!Bit) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001302 Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
Chris Lattner52416952007-11-22 21:06:59 +00001303 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001304 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001305 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001306 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001307 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001308 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001309 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001310 }
1311 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1312 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001313 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001314
Craig Topper011817a2014-04-09 04:50:04 +00001315 RecTy *DeducedEltTy = nullptr;
1316 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001317
Craig Topper011817a2014-04-09 04:50:04 +00001318 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001319 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001320 if (!ListType) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001321 TokError(Twine("Type mismatch for list, expected list type, got ") +
1322 ItemType->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001323 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001324 }
1325 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001326 }
David Greene8618f952009-06-08 20:23:18 +00001327
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001328 if (Lex.getCode() != tgtok::r_square) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001329 ParseValueList(Vals, CurRec, nullptr,
1330 GivenListTy ? GivenListTy->getElementType() : nullptr);
Craig Topper011817a2014-04-09 04:50:04 +00001331 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001332 }
1333 if (Lex.getCode() != tgtok::r_square) {
1334 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001335 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001336 }
1337 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001338
Craig Topper011817a2014-04-09 04:50:04 +00001339 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001340 if (Lex.getCode() == tgtok::less) {
1341 // Optional list element type
1342 Lex.Lex(); // eat the '<'
1343
1344 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001345 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001346 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001347 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001348 }
1349
1350 if (Lex.getCode() != tgtok::greater) {
1351 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001352 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001353 }
1354 Lex.Lex(); // eat the '>'
1355 }
1356
1357 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001358 RecTy *EltTy = nullptr;
Craig Toppereb4d7c62015-04-29 04:43:36 +00001359 for (Init *V : Vals) {
1360 TypedInit *TArg = dyn_cast<TypedInit>(V);
Craig Topper011817a2014-04-09 04:50:04 +00001361 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001362 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001363 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001364 }
Craig Topper011817a2014-04-09 04:50:04 +00001365 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001366 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001367 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001368 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001369 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001370 }
Bob Wilson7248f862009-11-22 04:24:42 +00001371 } else {
David Greene8618f952009-06-08 20:23:18 +00001372 EltTy = TArg->getType();
1373 }
1374 }
1375
Craig Topper011817a2014-04-09 04:50:04 +00001376 if (GivenEltTy) {
1377 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001378 // Verify consistency
1379 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1380 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001381 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001382 }
1383 }
1384 EltTy = GivenEltTy;
1385 }
1386
Craig Topper011817a2014-04-09 04:50:04 +00001387 if (!EltTy) {
1388 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001389 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001390 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001391 }
1392 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001393 } else {
David Greene8618f952009-06-08 20:23:18 +00001394 // Make sure the deduced type is compatible with the given type
1395 if (GivenListTy) {
1396 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1397 TokError("Element type mismatch for list");
Craig Topper011817a2014-04-09 04:50:04 +00001398 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001399 }
1400 }
1401 DeducedEltTy = EltTy;
1402 }
Bob Wilson7248f862009-11-22 04:24:42 +00001403
David Greenee32ebf22011-07-29 19:07:07 +00001404 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001405 }
1406 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1407 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001408 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001409 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001410 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001411 }
Bob Wilson7248f862009-11-22 04:24:42 +00001412
David Greeneaf8ee2c2011-07-29 22:43:06 +00001413 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001414 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001415
Nate Begemandbe3f772009-03-19 05:21:56 +00001416 // If the operator name is present, parse it.
Matthias Braun7cf3b112016-12-05 06:00:41 +00001417 StringInit *OperatorName = nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001418 if (Lex.getCode() == tgtok::colon) {
1419 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1420 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001421 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001422 }
Matthias Braun7cf3b112016-12-05 06:00:41 +00001423 OperatorName = StringInit::get(Lex.getCurStrVal());
Nate Begemandbe3f772009-03-19 05:21:56 +00001424 Lex.Lex(); // eat the VarName.
1425 }
Bob Wilson7248f862009-11-22 04:24:42 +00001426
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001427 SmallVector<std::pair<llvm::Init*, StringInit*>, 8> DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001428 if (Lex.getCode() != tgtok::r_paren) {
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001429 ParseDagArgList(DagArgs, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001430 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001431 }
Bob Wilson7248f862009-11-22 04:24:42 +00001432
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001433 if (Lex.getCode() != tgtok::r_paren) {
1434 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001435 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001436 }
1437 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001438
David Greenee32ebf22011-07-29 19:07:07 +00001439 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001440 }
Bob Wilson7248f862009-11-22 04:24:42 +00001441
David Greene2f7cf7f2011-01-07 17:05:37 +00001442 case tgtok::XHead:
1443 case tgtok::XTail:
1444 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001445 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001446 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001447 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001448 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +00001449 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +00001450 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001451 case tgtok::XSRL:
1452 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001453 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001454 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001455 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001456 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001457 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001458 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001459 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001460 }
1461 }
Bob Wilson7248f862009-11-22 04:24:42 +00001462
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001463 return R;
1464}
1465
1466/// ParseValue - Parse a tblgen value. This returns null on error.
1467///
1468/// Value ::= SimpleValue ValueSuffix*
1469/// ValueSuffix ::= '{' BitList '}'
1470/// ValueSuffix ::= '[' BitList ']'
1471/// ValueSuffix ::= '.' ID
1472///
David Greened4263a62011-10-19 13:04:20 +00001473Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1474 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001475 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001476
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001477 // Parse the suffixes now if present.
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001478 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001479 switch (Lex.getCode()) {
1480 default: return Result;
1481 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001482 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001483 // This is the beginning of the object body.
1484 return Result;
1485
Chris Lattner526c8cb2009-06-21 03:39:35 +00001486 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001487 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001488 SmallVector<unsigned, 16> Ranges;
1489 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001490 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001491
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001492 // Reverse the bitlist.
1493 std::reverse(Ranges.begin(), Ranges.end());
1494 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001495 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001496 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001497 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001498 }
Bob Wilson7248f862009-11-22 04:24:42 +00001499
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001500 // Eat the '}'.
1501 if (Lex.getCode() != tgtok::r_brace) {
1502 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001503 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001504 }
1505 Lex.Lex();
1506 break;
1507 }
1508 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001509 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001510 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001511 SmallVector<unsigned, 16> Ranges;
1512 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001513 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001514
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001515 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001516 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001517 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001518 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001519 }
Bob Wilson7248f862009-11-22 04:24:42 +00001520
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001521 // Eat the ']'.
1522 if (Lex.getCode() != tgtok::r_square) {
1523 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001524 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001525 }
1526 Lex.Lex();
1527 break;
1528 }
Matthias Braun6a441832016-12-05 06:00:36 +00001529 case tgtok::period: {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001530 if (Lex.Lex() != tgtok::Id) { // eat the .
1531 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001532 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001533 }
Matthias Braun6a441832016-12-05 06:00:36 +00001534 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
1535 if (!Result->getFieldType(FieldName)) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001536 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001537 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001538 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001539 }
Matthias Braun6a441832016-12-05 06:00:36 +00001540 Result = FieldInit::get(Result, FieldName);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001541 Lex.Lex(); // eat field name
1542 break;
Matthias Braun6a441832016-12-05 06:00:36 +00001543 }
David Greene8e85b482011-10-19 13:04:43 +00001544
1545 case tgtok::paste:
1546 SMLoc PasteLoc = Lex.getLoc();
1547
1548 // Create a !strconcat() operation, first casting each operand to
1549 // a string if necessary.
1550
Sean Silvafb509ed2012-10-10 20:24:43 +00001551 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001552 if (!LHS) {
1553 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001554 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001555 }
Craig Toppera9642b42015-05-04 01:35:39 +00001556
David Greene8e85b482011-10-19 13:04:43 +00001557 if (LHS->getType() != StringRecTy::get()) {
1558 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1559 }
1560
Craig Topper011817a2014-04-09 04:50:04 +00001561 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001562
1563 Lex.Lex(); // Eat the '#'.
1564 switch (Lex.getCode()) {
1565 case tgtok::colon:
1566 case tgtok::semi:
1567 case tgtok::l_brace:
1568 // These are all of the tokens that can begin an object body.
1569 // Some of these can also begin values but we disallow those cases
1570 // because they are unlikely to be useful.
Craig Toppera9642b42015-05-04 01:35:39 +00001571
David Greene8e85b482011-10-19 13:04:43 +00001572 // Trailing paste, concat with an empty string.
1573 RHS = StringInit::get("");
1574 break;
1575
1576 default:
1577 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001578 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001579 if (!RHS) {
1580 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001581 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001582 }
1583
1584 if (RHS->getType() != StringRecTy::get()) {
1585 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1586 }
Craig Toppera9642b42015-05-04 01:35:39 +00001587
David Greene8e85b482011-10-19 13:04:43 +00001588 break;
1589 }
1590
1591 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1592 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1593 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001594 }
1595 }
1596}
1597
1598/// ParseDagArgList - Parse the argument list for a dag literal expression.
1599///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001600/// DagArg ::= Value (':' VARNAME)?
1601/// DagArg ::= VARNAME
1602/// DagArgList ::= DagArg
1603/// DagArgList ::= DagArgList ',' DagArg
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001604void TGParser::ParseDagArgList(
1605 SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
1606 Record *CurRec) {
Bob Wilson7248f862009-11-22 04:24:42 +00001607
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001608 while (true) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001609 // DagArg ::= VARNAME
1610 if (Lex.getCode() == tgtok::VarName) {
1611 // A missing value is treated like '?'.
Matthias Braunbb053162016-12-05 06:00:46 +00001612 StringInit *VarName = StringInit::get(Lex.getCurStrVal());
1613 Result.emplace_back(UnsetInit::get(), VarName);
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001614 Lex.Lex();
1615 } else {
1616 // DagArg ::= Value (':' VARNAME)?
1617 Init *Val = ParseValue(CurRec);
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001618 if (!Val) {
1619 Result.clear();
1620 return;
1621 }
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 Braun1ddb78c2016-12-05 06:41:51 +00001628 Result.clear();
1629 return;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001630 }
Matthias Braunbb053162016-12-05 06:00:46 +00001631 VarName = StringInit::get(Lex.getCurStrVal());
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001632 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001633 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001634
1635 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001636 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001637 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001638 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001639 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001640}
1641
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001642/// ParseValueList - Parse a comma separated list of values, returning them as a
1643/// vector. Note that this always expects to be able to parse at least one
1644/// value. It returns an empty list if this is not possible.
1645///
1646/// ValueList ::= Value (',' Value)
1647///
Matthias Braunc66e7552016-12-05 06:41:54 +00001648void TGParser::ParseValueList(SmallVectorImpl<Init*> &Result, Record *CurRec,
1649 Record *ArgsRec, RecTy *EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001650 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001651 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001652 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001653 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001654 if (TArgs.empty()) {
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001655 TokError("template argument provided to non-template class");
Matthias Braunc66e7552016-12-05 06:41:54 +00001656 Result.clear();
1657 return;
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001658 }
David Greene8618f952009-06-08 20:23:18 +00001659 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001660 if (!RV) {
1661 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1662 << ")\n";
1663 }
David Greene8618f952009-06-08 20:23:18 +00001664 assert(RV && "Template argument record not found??");
1665 ItemType = RV->getType();
1666 ++ArgN;
1667 }
1668 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00001669 if (!Result.back()) {
1670 Result.clear();
1671 return;
1672 }
Bob Wilson7248f862009-11-22 04:24:42 +00001673
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001674 while (Lex.getCode() == tgtok::comma) {
1675 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001676
Craig Topper011817a2014-04-09 04:50:04 +00001677 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001678 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001679 if (ArgN >= TArgs.size()) {
1680 TokError("too many template arguments");
Matthias Braunc66e7552016-12-05 06:41:54 +00001681 Result.clear();
1682 return;
Bob Wilson7248f862009-11-22 04:24:42 +00001683 }
David Greene8618f952009-06-08 20:23:18 +00001684 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1685 assert(RV && "Template argument record not found??");
1686 ItemType = RV->getType();
1687 ++ArgN;
1688 }
1689 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00001690 if (!Result.back()) {
1691 Result.clear();
1692 return;
1693 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001694 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001695}
1696
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001697/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1698/// empty string on error. This can happen in a number of different context's,
1699/// including within a def or in the template args for a def (which which case
1700/// CurRec will be non-null) and within the template args for a multiclass (in
1701/// which case CurRec will be null, but CurMultiClass will be set). This can
1702/// also happen within a def that is within a multiclass, which will set both
1703/// CurRec and CurMultiClass.
1704///
1705/// Declaration ::= FIELD? Type ID ('=' Value)?
1706///
David Greenedb10e692011-10-19 13:02:42 +00001707Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001708 bool ParsingTemplateArgs) {
1709 // Read the field prefix if present.
1710 bool HasField = Lex.getCode() == tgtok::Field;
1711 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001712
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001713 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001714 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001715
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001716 if (Lex.getCode() != tgtok::Id) {
1717 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001718 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001719 }
Bob Wilson7248f862009-11-22 04:24:42 +00001720
Chris Lattner526c8cb2009-06-21 03:39:35 +00001721 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001722 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001723 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001724
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001725 if (ParsingTemplateArgs) {
Craig Toppera9642b42015-05-04 01:35:39 +00001726 if (CurRec)
David Greenedb10e692011-10-19 13:02:42 +00001727 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Craig Toppera9642b42015-05-04 01:35:39 +00001728 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001729 assert(CurMultiClass);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001730 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001731 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1732 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001733 }
Bob Wilson7248f862009-11-22 04:24:42 +00001734
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001735 // Add the value.
1736 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001737 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001738
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001739 // If a value is present, parse it.
1740 if (Lex.getCode() == tgtok::equal) {
1741 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001742 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001743 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001744 if (!Val ||
Craig Toppercfd81732016-01-04 03:15:08 +00001745 SetValue(CurRec, ValLoc, DeclName, None, Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001746 // Return the name, even if an error is thrown. This is so that we can
1747 // continue to make some progress, even without the value having been
1748 // initialized.
1749 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001750 }
Bob Wilson7248f862009-11-22 04:24:42 +00001751
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001752 return DeclName;
1753}
1754
David Greenefb927af2012-02-22 16:09:41 +00001755/// ParseForeachDeclaration - Read a foreach declaration, returning
1756/// the name of the declared object or a NULL Init on error. Return
1757/// the name of the parsed initializer list through ForeachListName.
1758///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001759/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1760/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1761/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001762///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001763VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001764 if (Lex.getCode() != tgtok::Id) {
1765 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001766 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001767 }
1768
1769 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1770 Lex.Lex();
1771
1772 // If a value is present, parse it.
1773 if (Lex.getCode() != tgtok::equal) {
1774 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001775 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001776 }
1777 Lex.Lex(); // Eat the '='
1778
Craig Topper011817a2014-04-09 04:50:04 +00001779 RecTy *IterType = nullptr;
Matthias Braunc66e7552016-12-05 06:41:54 +00001780 SmallVector<unsigned, 16> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001781
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001782 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001783 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001784 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001785 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001786 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001787 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001788 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001789 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001790 }
1791 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001792 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001793 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001794 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001795 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001796 }
1797 IterType = ListType->getElementType();
1798 break;
David Greenefb927af2012-02-22 16:09:41 +00001799 }
1800
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001801 case tgtok::IntVal: { // RangePiece.
1802 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001803 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001804 break;
David Greenefb927af2012-02-22 16:09:41 +00001805 }
1806
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001807 case tgtok::l_brace: { // '{' RangeList '}'
1808 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001809 ParseRangeList(Ranges);
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001810 if (Lex.getCode() != tgtok::r_brace) {
1811 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001812 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001813 }
1814 Lex.Lex();
1815 break;
1816 }
1817 }
David Greenefb927af2012-02-22 16:09:41 +00001818
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001819 if (!Ranges.empty()) {
1820 assert(!IterType && "Type already initialized?");
1821 IterType = IntRecTy::get();
1822 std::vector<Init*> Values;
Craig Topper8eb764c2015-06-02 06:19:28 +00001823 for (unsigned R : Ranges)
1824 Values.push_back(IntInit::get(R));
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001825 ForeachListValue = ListInit::get(Values, IterType);
1826 }
1827
1828 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001829 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001830
1831 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001832}
1833
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001834/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1835/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1836/// template args for a def, which may or may not be in a multiclass. If null,
1837/// these are the template args for a multiclass.
1838///
1839/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001840///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001841bool TGParser::ParseTemplateArgList(Record *CurRec) {
1842 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1843 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001844
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001845 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001846
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001847 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001848 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001849 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001850 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001851
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001852 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001853
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001854 while (Lex.getCode() == tgtok::comma) {
1855 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001856
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001857 // Read the following declarations.
1858 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001859 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001860 return true;
1861 TheRecToAddTo->addTemplateArg(TemplArg);
1862 }
Bob Wilson7248f862009-11-22 04:24:42 +00001863
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001864 if (Lex.getCode() != tgtok::greater)
1865 return TokError("expected '>' at end of template argument list");
1866 Lex.Lex(); // eat the '>'.
1867 return false;
1868}
1869
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001870/// ParseBodyItem - Parse a single item at within the body of a def or class.
1871///
1872/// BodyItem ::= Declaration ';'
1873/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1874bool TGParser::ParseBodyItem(Record *CurRec) {
1875 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001876 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001877 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001878
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001879 if (Lex.getCode() != tgtok::semi)
1880 return TokError("expected ';' after declaration");
1881 Lex.Lex();
1882 return false;
1883 }
1884
1885 // LET ID OptionalRangeList '=' Value ';'
1886 if (Lex.Lex() != tgtok::Id)
1887 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001888
Chris Lattner526c8cb2009-06-21 03:39:35 +00001889 SMLoc IdLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00001890 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001891 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001892
Matthias Braunc66e7552016-12-05 06:41:54 +00001893 SmallVector<unsigned, 16> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001894 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001895 return true;
1896 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001897
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001898 if (Lex.getCode() != tgtok::equal)
1899 return TokError("expected '=' in let expression");
1900 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001901
David Greene8618f952009-06-08 20:23:18 +00001902 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001903 if (!Field)
Matthias Braun215ff842016-12-05 07:35:13 +00001904 return TokError("Value '" + FieldName->getValue() + "' unknown!");
David Greene8618f952009-06-08 20:23:18 +00001905
1906 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001907
David Greeneaf8ee2c2011-07-29 22:43:06 +00001908 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001909 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001910
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001911 if (Lex.getCode() != tgtok::semi)
1912 return TokError("expected ';' after let expression");
1913 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001914
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001915 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1916}
1917
1918/// ParseBody - Read the body of a class or def. Return true on error, false on
1919/// success.
1920///
1921/// Body ::= ';'
1922/// Body ::= '{' BodyList '}'
1923/// BodyList BodyItem*
1924///
1925bool TGParser::ParseBody(Record *CurRec) {
1926 // If this is a null definition, just eat the semi and return.
1927 if (Lex.getCode() == tgtok::semi) {
1928 Lex.Lex();
1929 return false;
1930 }
Bob Wilson7248f862009-11-22 04:24:42 +00001931
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001932 if (Lex.getCode() != tgtok::l_brace)
1933 return TokError("Expected ';' or '{' to start body");
1934 // Eat the '{'.
1935 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001936
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001937 while (Lex.getCode() != tgtok::r_brace)
1938 if (ParseBodyItem(CurRec))
1939 return true;
1940
1941 // Eat the '}'.
1942 Lex.Lex();
1943 return false;
1944}
1945
Sean Silvacb1a75e2013-01-09 04:49:14 +00001946/// \brief Apply the current let bindings to \a CurRec.
1947/// \returns true on error, false otherwise.
1948bool TGParser::ApplyLetStack(Record *CurRec) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001949 for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
Craig Topper8eb764c2015-06-02 06:19:28 +00001950 for (LetRecord &LR : LetInfo)
1951 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
Sean Silvacb1a75e2013-01-09 04:49:14 +00001952 return true;
1953 return false;
1954}
1955
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001956/// ParseObjectBody - Parse the body of a def or class. This consists of an
1957/// optional ClassList followed by a Body. CurRec is the current def or class
1958/// that is being parsed.
1959///
1960/// ObjectBody ::= BaseClassList Body
1961/// BaseClassList ::= /*empty*/
1962/// BaseClassList ::= ':' BaseClassListNE
1963/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1964///
1965bool TGParser::ParseObjectBody(Record *CurRec) {
1966 // If there is a baseclass list, read it.
1967 if (Lex.getCode() == tgtok::colon) {
1968 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001969
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001970 // Read all of the subclasses.
1971 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001972 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001973 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00001974 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001975
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001976 // Add it.
1977 if (AddSubClass(CurRec, SubClass))
1978 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001979
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001980 if (Lex.getCode() != tgtok::comma) break;
1981 Lex.Lex(); // eat ','.
1982 SubClass = ParseSubClassReference(CurRec, false);
1983 }
1984 }
1985
Sean Silvacb1a75e2013-01-09 04:49:14 +00001986 if (ApplyLetStack(CurRec))
1987 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001988
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001989 return ParseBody(CurRec);
1990}
1991
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001992/// ParseDef - Parse and return a top level or multiclass def, return the record
1993/// corresponding to it. This returns null on error.
1994///
1995/// DefInst ::= DEF ObjectName ObjectBody
1996///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00001997bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001998 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001999 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00002000 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002001
2002 // Parse ObjectName and make a record for it.
Craig Topper84138712014-11-29 05:31:10 +00002003 std::unique_ptr<Record> CurRecOwner;
Jordan Roseabdd99b2013-01-10 18:50:05 +00002004 Init *Name = ParseObjectName(CurMultiClass);
2005 if (Name)
Craig Topper84138712014-11-29 05:31:10 +00002006 CurRecOwner = make_unique<Record>(Name, DefLoc, Records);
Jordan Roseabdd99b2013-01-10 18:50:05 +00002007 else
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00002008 CurRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), DefLoc,
2009 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00002010 Record *CurRec = CurRecOwner.get(); // Keep a copy since we may release.
Bob Wilson7248f862009-11-22 04:24:42 +00002011
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002012 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002013 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002014
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002015 // Ensure redefinition doesn't happen.
Craig Topper84138712014-11-29 05:31:10 +00002016 if (Records.getDef(CurRec->getNameInitAsString()))
2017 return Error(DefLoc, "def '" + CurRec->getNameInitAsString()+
2018 "' already defined");
Craig Toppercdab2322014-11-29 05:52:51 +00002019 Records.addDef(std::move(CurRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00002020
2021 if (ParseObjectBody(CurRec))
2022 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002023 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002024 // Parse the body before adding this prototype to the DefPrototypes vector.
2025 // That way implicit definitions will be added to the DefPrototypes vector
2026 // before this object, instantiated prior to defs derived from this object,
2027 // and this available for indirect name resolution when defs derived from
2028 // this object are instantiated.
Craig Topper84138712014-11-29 05:31:10 +00002029 if (ParseObjectBody(CurRec))
Hal Finkela8c1f462014-01-02 20:47:09 +00002030 return true;
2031
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002032 // Otherwise, a def inside a multiclass, add it to the multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002033 for (const auto &Proto : CurMultiClass->DefPrototypes)
2034 if (Proto->getNameInit() == CurRec->getNameInit())
Craig Topper84138712014-11-29 05:31:10 +00002035 return Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
2036 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002037 CurMultiClass->DefPrototypes.push_back(std::move(CurRecOwner));
Anton Yartsev671dff12014-08-08 00:29:54 +00002038 } else if (ParseObjectBody(CurRec)) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002039 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002040 }
Bob Wilson7248f862009-11-22 04:24:42 +00002041
Craig Topper011817a2014-04-09 04:50:04 +00002042 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002043 // See Record::setName(). This resolve step will see any new name
2044 // for the def that might have been created when resolving
2045 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002046 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002047
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002048 // If ObjectBody has template arguments, it's an error.
2049 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002050
2051 if (CurMultiClass) {
2052 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002053 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
2054 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002055 assert(RV && "Template arg doesn't exist?");
2056 CurRec->addValue(*RV);
2057 }
2058 }
2059
Craig Toppera9642b42015-05-04 01:35:39 +00002060 if (ProcessForeachDefs(CurRec, DefLoc))
Craig Topper84138712014-11-29 05:31:10 +00002061 return Error(DefLoc, "Could not process loops for def" +
2062 CurRec->getNameInitAsString());
David Greenefb927af2012-02-22 16:09:41 +00002063
2064 return false;
2065}
2066
2067/// ParseForeach - Parse a for statement. Return the record corresponding
2068/// to it. This returns true on error.
2069///
2070/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2071/// Foreach ::= FOREACH Declaration IN Object
2072///
2073bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2074 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2075 Lex.Lex(); // Eat the 'for' token.
2076
2077 // Make a temporary object to record items associated with the for
2078 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002079 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002080 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002081 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002082 return TokError("expected declaration in for");
2083
2084 if (Lex.getCode() != tgtok::In)
2085 return TokError("Unknown tok");
2086 Lex.Lex(); // Eat the in
2087
2088 // Create a loop object and remember it.
2089 Loops.push_back(ForeachLoop(IterName, ListValue));
2090
2091 if (Lex.getCode() != tgtok::l_brace) {
2092 // FOREACH Declaration IN Object
2093 if (ParseObject(CurMultiClass))
2094 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002095 } else {
David Greenefb927af2012-02-22 16:09:41 +00002096 SMLoc BraceLoc = Lex.getLoc();
2097 // Otherwise, this is a group foreach.
2098 Lex.Lex(); // eat the '{'.
2099
2100 // Parse the object list.
2101 if (ParseObjectList(CurMultiClass))
2102 return true;
2103
2104 if (Lex.getCode() != tgtok::r_brace) {
2105 TokError("expected '}' at end of foreach command");
2106 return Error(BraceLoc, "to match this '{'");
2107 }
2108 Lex.Lex(); // Eat the }
2109 }
2110
2111 // We've processed everything in this loop.
2112 Loops.pop_back();
2113
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002114 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002115}
2116
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002117/// ParseClass - Parse a tblgen class definition.
2118///
2119/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2120///
2121bool TGParser::ParseClass() {
2122 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2123 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002124
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002125 if (Lex.getCode() != tgtok::Id)
2126 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002127
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002128 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2129 if (CurRec) {
2130 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002131 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002132 !CurRec->getSuperClasses().empty() ||
2133 !CurRec->getTemplateArgs().empty())
Craig Topper85c07002015-04-30 05:54:22 +00002134 return TokError("Class '" + CurRec->getNameInitAsString() +
2135 "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002136 } else {
2137 // If this is the first reference to this class, create and add it.
Hans Wennborgffbbd532014-11-30 00:31:49 +00002138 auto NewRec =
2139 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
Craig Toppercdab2322014-11-29 05:52:51 +00002140 CurRec = NewRec.get();
2141 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002142 }
2143 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002144
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002145 // If there are template args, parse them.
2146 if (Lex.getCode() == tgtok::less)
2147 if (ParseTemplateArgList(CurRec))
2148 return true;
2149
2150 // Finally, parse the object body.
2151 return ParseObjectBody(CurRec);
2152}
2153
2154/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2155/// of LetRecords.
2156///
2157/// LetList ::= LetItem (',' LetItem)*
2158/// LetItem ::= ID OptionalRangeList '=' Value
2159///
Matthias Braunc66e7552016-12-05 06:41:54 +00002160void TGParser::ParseLetList(SmallVectorImpl<LetRecord> &Result) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002161 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002162 if (Lex.getCode() != tgtok::Id) {
2163 TokError("expected identifier in let definition");
Matthias Braunc66e7552016-12-05 06:41:54 +00002164 Result.clear();
2165 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002166 }
Matthias Braunc66e7552016-12-05 06:41:54 +00002167
Matthias Braun215ff842016-12-05 07:35:13 +00002168 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattner526c8cb2009-06-21 03:39:35 +00002169 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002170 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002171
2172 // Check for an optional RangeList.
Matthias Braunc66e7552016-12-05 06:41:54 +00002173 SmallVector<unsigned, 16> Bits;
2174 if (ParseOptionalRangeList(Bits)) {
2175 Result.clear();
2176 return;
2177 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002178 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002179
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002180 if (Lex.getCode() != tgtok::equal) {
2181 TokError("expected '=' in let expression");
Matthias Braunc66e7552016-12-05 06:41:54 +00002182 Result.clear();
2183 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002184 }
2185 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002186
Craig Topper011817a2014-04-09 04:50:04 +00002187 Init *Val = ParseValue(nullptr);
Matthias Braunc66e7552016-12-05 06:41:54 +00002188 if (!Val) {
2189 Result.clear();
2190 return;
2191 }
Bob Wilson7248f862009-11-22 04:24:42 +00002192
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002193 // Now that we have everything, add the record.
Matthias Braun215ff842016-12-05 07:35:13 +00002194 Result.emplace_back(Name, Bits, Val, NameLoc);
Bob Wilson7248f862009-11-22 04:24:42 +00002195
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002196 if (Lex.getCode() != tgtok::comma)
Matthias Braunc66e7552016-12-05 06:41:54 +00002197 return;
Bob Wilson7248f862009-11-22 04:24:42 +00002198 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002199 }
2200}
2201
2202/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002203/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002204///
2205/// Object ::= LET LetList IN '{' ObjectList '}'
2206/// Object ::= LET LetList IN Object
2207///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002208bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002209 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2210 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002211
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002212 // Add this entry to the let stack.
Matthias Braunc66e7552016-12-05 06:41:54 +00002213 SmallVector<LetRecord, 8> LetInfo;
2214 ParseLetList(LetInfo);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002215 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002216 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002217
2218 if (Lex.getCode() != tgtok::In)
2219 return TokError("expected 'in' at end of top-level 'let'");
2220 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002221
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002222 // If this is a scalar let, just handle it now
2223 if (Lex.getCode() != tgtok::l_brace) {
2224 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002225 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002226 return true;
2227 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002228 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002229 // Otherwise, this is a group let.
2230 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002231
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002232 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002233 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002234 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002235
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002236 if (Lex.getCode() != tgtok::r_brace) {
2237 TokError("expected '}' at end of top level let command");
2238 return Error(BraceLoc, "to match this '{'");
2239 }
2240 Lex.Lex();
2241 }
Bob Wilson7248f862009-11-22 04:24:42 +00002242
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002243 // Outside this let scope, this let block is not active.
2244 LetStack.pop_back();
2245 return false;
2246}
2247
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002248/// ParseMultiClass - Parse a multiclass definition.
2249///
Bob Wilson3d948162009-04-28 19:41:44 +00002250/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002251/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2252/// MultiClassObject ::= DefInst
2253/// MultiClassObject ::= MultiClassInst
2254/// MultiClassObject ::= DefMInst
2255/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2256/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002257///
2258bool TGParser::ParseMultiClass() {
2259 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2260 Lex.Lex(); // Eat the multiclass token.
2261
2262 if (Lex.getCode() != tgtok::Id)
2263 return TokError("expected identifier after multiclass for name");
2264 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002265
Craig Topper7adf2bf2014-12-11 05:25:30 +00002266 auto Result =
2267 MultiClasses.insert(std::make_pair(Name,
2268 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2269
2270 if (!Result.second)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002271 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002272
Craig Topper7adf2bf2014-12-11 05:25:30 +00002273 CurMultiClass = Result.first->second.get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002274 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002275
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002276 // If there are template args, parse them.
2277 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002278 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002279 return true;
2280
David Greene7049e792009-04-24 16:55:41 +00002281 bool inherits = false;
2282
David Greene753ed8f2009-04-22 16:42:54 +00002283 // If there are submulticlasses, parse them.
2284 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002285 inherits = true;
2286
David Greene753ed8f2009-04-22 16:42:54 +00002287 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002288
David Greene753ed8f2009-04-22 16:42:54 +00002289 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002290 SubMultiClassReference SubMultiClass =
2291 ParseSubMultiClassReference(CurMultiClass);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002292 while (true) {
David Greene753ed8f2009-04-22 16:42:54 +00002293 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002294 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002295
David Greene753ed8f2009-04-22 16:42:54 +00002296 // Add it.
2297 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2298 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002299
David Greene753ed8f2009-04-22 16:42:54 +00002300 if (Lex.getCode() != tgtok::comma) break;
2301 Lex.Lex(); // eat ','.
2302 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2303 }
2304 }
2305
David Greene7049e792009-04-24 16:55:41 +00002306 if (Lex.getCode() != tgtok::l_brace) {
2307 if (!inherits)
2308 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002309 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002310 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002311 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002312 } else {
David Greene7049e792009-04-24 16:55:41 +00002313 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2314 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002315
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002316 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002317 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002318 default:
2319 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
2320 case tgtok::Let:
2321 case tgtok::Def:
2322 case tgtok::Defm:
2323 case tgtok::Foreach:
2324 if (ParseObject(CurMultiClass))
2325 return true;
2326 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002327 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002328 }
David Greene7049e792009-04-24 16:55:41 +00002329 Lex.Lex(); // eat the '}'.
2330 }
Bob Wilson7248f862009-11-22 04:24:42 +00002331
Craig Topper011817a2014-04-09 04:50:04 +00002332 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002333 return false;
2334}
2335
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002336Record *TGParser::InstantiateMulticlassDef(MultiClass &MC, Record *DefProto,
2337 Init *&DefmPrefix,
2338 SMRange DefmPrefixRange,
2339 ArrayRef<Init *> TArgs,
Matthias Braunc66e7552016-12-05 06:41:54 +00002340 ArrayRef<Init *> TemplateVals) {
David Greene5d5d88c2011-10-19 13:04:31 +00002341 // We need to preserve DefProto so it can be reused for later
2342 // instantiations, so create a new Record to inherit from it.
2343
David Greenedb445972011-10-05 22:42:07 +00002344 // Add in the defm name. If the defm prefix is empty, give each
2345 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2346 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2347 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002348
Jordan Roseabdd99b2013-01-10 18:50:05 +00002349 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002350 if (!DefmPrefix) {
David Greene5d5d88c2011-10-19 13:04:31 +00002351 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002352 IsAnonymous = true;
2353 }
David Greene5d5d88c2011-10-19 13:04:31 +00002354
2355 Init *DefName = DefProto->getNameInit();
Sean Silvafb509ed2012-10-10 20:24:43 +00002356 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002357
Craig Topper011817a2014-04-09 04:50:04 +00002358 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002359 // We have a fully expanded string so there are no operators to
2360 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002361 DefName =
2362 BinOpInit::get(BinOpInit::STRCONCAT,
2363 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2364 StringRecTy::get())->Fold(DefProto, &MC),
2365 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2366 }
David Greene5d5d88c2011-10-19 13:04:31 +00002367
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002368 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002369 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002370 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Craig Topper84138712014-11-29 05:31:10 +00002371 auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002372
2373 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002374 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002375 Ref.Rec = DefProto;
Craig Topper84138712014-11-29 05:31:10 +00002376 AddSubClass(CurRec.get(), Ref);
David Greenedb445972011-10-05 22:42:07 +00002377
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002378 // Set the value for NAME. We don't resolve references to it 'til later,
2379 // though, so that uses in nested multiclass names don't get
2380 // confused.
Matthias Braun215ff842016-12-05 07:35:13 +00002381 if (SetValue(CurRec.get(), Ref.RefRange.Start, StringInit::get("NAME"), None,
2382 DefmPrefix, /*AllowSelfAssignment*/true)) {
Craig Topper85c07002015-04-30 05:54:22 +00002383 Error(DefmPrefixRange.Start, "Could not resolve " +
2384 CurRec->getNameInitAsString() + ":NAME to '" +
2385 DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002386 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002387 }
David Greene8bf0d722011-10-19 13:04:35 +00002388
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002389 // If the DefNameString didn't resolve, we probably have a reference to
2390 // NAME and need to replace it. We need to do at least this much greedily,
2391 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002392 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002393 RecordVal *DefNameRV = CurRec->getValue("NAME");
2394 CurRec->resolveReferencesTo(DefNameRV);
2395 }
2396
2397 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002398 // Now that we're at the top level, resolve all NAME references
2399 // in the resultant defs that weren't in the def names themselves.
2400 RecordVal *DefNameRV = CurRec->getValue("NAME");
2401 CurRec->resolveReferencesTo(DefNameRV);
2402
Hal Finkeld2497362015-05-21 04:32:56 +00002403 // Check if the name is a complex pattern.
2404 // If so, resolve it.
2405 DefName = CurRec->getNameInit();
2406 DefNameString = dyn_cast<StringInit>(DefName);
2407
2408 // OK the pattern is more complex than simply using NAME.
2409 // Let's use the heavy weaponery.
2410 if (!DefNameString) {
2411 ResolveMulticlassDefArgs(MC, CurRec.get(), DefmPrefixRange.Start,
2412 Lex.getLoc(), TArgs, TemplateVals,
2413 false/*Delete args*/);
2414 DefName = CurRec->getNameInit();
2415 DefNameString = dyn_cast<StringInit>(DefName);
2416
2417 if (!DefNameString)
2418 DefName = DefName->convertInitializerTo(StringRecTy::get());
2419
2420 // We ran out of options here...
2421 DefNameString = dyn_cast<StringInit>(DefName);
2422 if (!DefNameString) {
2423 PrintFatalError(CurRec->getLoc()[CurRec->getLoc().size() - 1],
2424 DefName->getAsUnquotedString() + " is not a string.");
2425 return nullptr;
2426 }
2427
2428 CurRec->setName(DefName);
2429 }
2430
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002431 // Now that NAME references are resolved and we're at the top level of
2432 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002433 // currently in a multiclass, it means this defm appears inside a
2434 // multiclass and its name won't be fully resolvable until we see
Hal Finkeld2497362015-05-21 04:32:56 +00002435 // the top-level defm. Therefore, we don't add this to the
2436 // RecordKeeper at this point. If we did we could get duplicate
David Greene8bf0d722011-10-19 13:04:35 +00002437 // defs as more than one probably refers to NAME or some other
2438 // common internal placeholder.
2439
2440 // Ensure redefinition doesn't happen.
2441 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002442 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002443 "' already defined, instantiating defm with subdef '" +
2444 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002445 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002446 }
2447
Craig Topper84138712014-11-29 05:31:10 +00002448 Record *CurRecSave = CurRec.get(); // Keep a copy before we release.
Craig Toppercdab2322014-11-29 05:52:51 +00002449 Records.addDef(std::move(CurRec));
Craig Topper84138712014-11-29 05:31:10 +00002450 return CurRecSave;
David Greene8bf0d722011-10-19 13:04:35 +00002451 }
2452
Craig Topper84138712014-11-29 05:31:10 +00002453 // FIXME This is bad but the ownership transfer to caller is pretty messy.
2454 // The unique_ptr in this function at least protects the exits above.
2455 return CurRec.release();
David Greenedb445972011-10-05 22:42:07 +00002456}
2457
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002458bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC, Record *CurRec,
2459 SMLoc DefmPrefixLoc, SMLoc SubClassLoc,
2460 ArrayRef<Init *> TArgs,
Matthias Braunc66e7552016-12-05 06:41:54 +00002461 ArrayRef<Init *> TemplateVals,
David Greenedb445972011-10-05 22:42:07 +00002462 bool DeleteArgs) {
2463 // Loop over all of the template arguments, setting them to the specified
2464 // value or leaving them as the default if necessary.
2465 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2466 // Check if a value is specified for this temp-arg.
2467 if (i < TemplateVals.size()) {
2468 // Set it now.
Craig Toppercfd81732016-01-04 03:15:08 +00002469 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], None, TemplateVals[i]))
David Greenedb445972011-10-05 22:42:07 +00002470 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002471
David Greenedb445972011-10-05 22:42:07 +00002472 // Resolve it next.
2473 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2474
2475 if (DeleteArgs)
2476 // Now remove it.
2477 CurRec->removeValue(TArgs[i]);
Craig Toppera9642b42015-05-04 01:35:39 +00002478
David Greenedb445972011-10-05 22:42:07 +00002479 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Craig Topper85c07002015-04-30 05:54:22 +00002480 return Error(SubClassLoc, "value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00002481 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +00002482 ") of multiclassclass '" + MC.Rec.getNameInitAsString() +
2483 "'");
David Greenedb445972011-10-05 22:42:07 +00002484 }
2485 }
2486 return false;
2487}
2488
2489bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2490 Record *CurRec,
2491 Record *DefProto,
2492 SMLoc DefmPrefixLoc) {
2493 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002494 if (ApplyLetStack(CurRec))
2495 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002496
David Greenedb445972011-10-05 22:42:07 +00002497 // Don't create a top level definition for defm inside multiclasses,
2498 // instead, only update the prototypes and bind the template args
2499 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002500 if (!CurMultiClass)
2501 return false;
Craig Toppereb4d7c62015-04-29 04:43:36 +00002502 for (const auto &Proto : CurMultiClass->DefPrototypes)
2503 if (Proto->getNameInit() == CurRec->getNameInit())
Sean Silvacc951b22013-01-09 05:28:12 +00002504 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2505 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002506 CurMultiClass->DefPrototypes.push_back(std::unique_ptr<Record>(CurRec));
David Greenedb445972011-10-05 22:42:07 +00002507
Sean Silvacc951b22013-01-09 05:28:12 +00002508 // Copy the template arguments for the multiclass into the new def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002509 for (Init * TA : CurMultiClass->Rec.getTemplateArgs()) {
2510 const RecordVal *RV = CurMultiClass->Rec.getValue(TA);
Sean Silvacc951b22013-01-09 05:28:12 +00002511 assert(RV && "Template arg doesn't exist?");
2512 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002513 }
2514
2515 return false;
2516}
2517
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002518/// ParseDefm - Parse the instantiation of a multiclass.
2519///
2520/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2521///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002522bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002523 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002524 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002525 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002526
Craig Topperb21afc62013-01-07 05:09:33 +00002527 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002528 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002529 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002530
Jordan Rosef12e8a92013-01-10 18:50:11 +00002531 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002532 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002533 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002534
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002535 // Keep track of the new generated record definitions.
2536 std::vector<Record*> NewRecDefs;
2537
2538 // This record also inherits from a regular class (non-multiclass)?
2539 bool InheritFromClass = false;
2540
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002541 // eat the colon.
2542 Lex.Lex();
2543
Chris Lattner526c8cb2009-06-21 03:39:35 +00002544 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002545 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002546
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002547 while (true) {
Craig Topper011817a2014-04-09 04:50:04 +00002548 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002549
2550 // To instantiate a multiclass, we need to first get the multiclass, then
2551 // instantiate each def contained in the multiclass with the SubClassRef
2552 // template parameters.
Craig Topper7adf2bf2014-12-11 05:25:30 +00002553 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper4ca40012014-11-30 01:20:17 +00002554 assert(MC && "Didn't lookup multiclass correctly?");
Matthias Braunc66e7552016-12-05 06:41:54 +00002555 ArrayRef<Init*> TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002556
2557 // Verify that the correct number of template arguments were specified.
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002558 ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002559 if (TArgs.size() < TemplateVals.size())
2560 return Error(SubClassLoc,
2561 "more template args specified than multiclass expects");
2562
2563 // Loop over all the def's in the multiclass, instantiating each one.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002564 for (const std::unique_ptr<Record> &DefProto : MC->DefPrototypes) {
Hal Finkeld2497362015-05-21 04:32:56 +00002565 // The record name construction goes as follow:
2566 // - If the def name is a string, prepend the prefix.
2567 // - If the def name is a more complex pattern, use that pattern.
Craig Topper7f36be92016-02-26 06:50:27 +00002568 // As a result, the record is instantiated before resolving
Hal Finkeld2497362015-05-21 04:32:56 +00002569 // arguments, as it would make its name a string.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002570 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto.get(), DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002571 SMRange(DefmLoc,
Hal Finkeld2497362015-05-21 04:32:56 +00002572 DefmPrefixEndLoc),
2573 TArgs, TemplateVals);
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002574 if (!CurRec)
2575 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002576
Craig Topper7f36be92016-02-26 06:50:27 +00002577 // Now that the record is instantiated, we can resolve arguments.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002578 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002579 TArgs, TemplateVals, true/*Delete args*/))
2580 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002581
Craig Toppereb4d7c62015-04-29 04:43:36 +00002582 if (ResolveMulticlassDef(*MC, CurRec, DefProto.get(), DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002583 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002584
Adam Nemete5a07162014-09-16 17:14:13 +00002585 // Defs that can be used by other definitions should be fully resolved
2586 // before any use.
2587 if (DefProto->isResolveFirst() && !CurMultiClass) {
2588 CurRec->resolveReferences();
2589 CurRec->setResolveFirst(false);
2590 }
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002591 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002592 }
2593
David Greenedb445972011-10-05 22:42:07 +00002594
David Greenef00919a2009-04-22 22:17:51 +00002595 if (Lex.getCode() != tgtok::comma) break;
2596 Lex.Lex(); // eat ','.
2597
Craig Topper998a39a2013-08-20 04:22:09 +00002598 if (Lex.getCode() != tgtok::Id)
2599 return TokError("expected identifier");
2600
David Greenef00919a2009-04-22 22:17:51 +00002601 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002602
2603 // A defm can inherit from regular classes (non-multiclass) as
2604 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002605 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002606
2607 if (InheritFromClass)
2608 break;
2609
Craig Topper011817a2014-04-09 04:50:04 +00002610 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002611 }
2612
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002613 if (InheritFromClass) {
2614 // Process all the classes to inherit as if they were part of a
2615 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002616 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002617 while (true) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002618 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002619 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002620
2621 // Get the expanded definition prototypes and teach them about
2622 // the record values the current class to inherit has
Craig Toppereb4d7c62015-04-29 04:43:36 +00002623 for (Record *CurRec : NewRecDefs) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002624 // Add it.
2625 if (AddSubClass(CurRec, SubClass))
2626 return true;
2627
Sean Silvacb1a75e2013-01-09 04:49:14 +00002628 if (ApplyLetStack(CurRec))
2629 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002630 }
2631
2632 if (Lex.getCode() != tgtok::comma) break;
2633 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002634 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002635 }
2636 }
2637
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002638 if (!CurMultiClass)
Craig Toppereb4d7c62015-04-29 04:43:36 +00002639 for (Record *CurRec : NewRecDefs)
David Greene50c09122011-08-10 18:27:46 +00002640 // See Record::setName(). This resolve step will see any new
2641 // name for the def that might have been created when resolving
2642 // inheritance, values and arguments above.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002643 CurRec->resolveReferences();
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002644
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002645 if (Lex.getCode() != tgtok::semi)
2646 return TokError("expected ';' at end of defm");
2647 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002648
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002649 return false;
2650}
2651
2652/// ParseObject
2653/// Object ::= ClassInst
2654/// Object ::= DefInst
2655/// Object ::= MultiClassInst
2656/// Object ::= DefMInst
2657/// Object ::= LETCommand '{' ObjectList '}'
2658/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002659bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002660 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002661 default:
2662 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002663 case tgtok::Let: return ParseTopLevelLet(MC);
2664 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002665 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002666 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002667 case tgtok::Class: return ParseClass();
2668 case tgtok::MultiClass: return ParseMultiClass();
2669 }
2670}
2671
2672/// ParseObjectList
2673/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002674bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002675 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002676 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002677 return true;
2678 }
2679 return false;
2680}
2681
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002682bool TGParser::ParseFile() {
2683 Lex.Lex(); // Prime the lexer.
2684 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002685
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002686 // If we have unread input at the end of the file, report it.
2687 if (Lex.getCode() == tgtok::Eof)
2688 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002689
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002690 return TokError("Unexpected input at top level");
2691}