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