blob: 580a73aa5d31241d5e5670bb0fb9b81818de1348 [file] [log] [blame]
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001//===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Parser for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
14#include "TGParser.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000015#include "llvm/ADT/None.h"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000016#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/SmallVector.h"
Chris Lattnerf4127dd2007-11-22 20:49:04 +000018#include "llvm/ADT/StringExtras.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000019#include "llvm/Support/Casting.h"
20#include "llvm/Support/Compiler.h"
21#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/TableGen/Record.h"
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000024#include <algorithm>
Eugene Zelenko33d7b762016-08-23 17:14:32 +000025#include <cassert>
26#include <cstdint>
27
Chris Lattnerf4127dd2007-11-22 20:49:04 +000028using namespace llvm;
29
30//===----------------------------------------------------------------------===//
31// Support Code for the Semantic Actions.
32//===----------------------------------------------------------------------===//
33
34namespace llvm {
Eugene Zelenko33d7b762016-08-23 17:14:32 +000035
Chris Lattnerf4127dd2007-11-22 20:49:04 +000036struct SubClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000037 SMRange RefRange;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000038 Record *Rec;
Matthias Braunc66e7552016-12-05 06:41:54 +000039 SmallVector<Init*, 4> TemplateArgs;
Eugene Zelenko33d7b762016-08-23 17:14:32 +000040
Craig Topper011817a2014-04-09 04:50:04 +000041 SubClassReference() : Rec(nullptr) {}
David Greene7049e792009-04-24 16:55:41 +000042
Craig Topper011817a2014-04-09 04:50:04 +000043 bool isInvalid() const { return Rec == nullptr; }
Chris Lattnerf4127dd2007-11-22 20:49:04 +000044};
David Greene753ed8f2009-04-22 16:42:54 +000045
46struct SubMultiClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000047 SMRange RefRange;
David Greene753ed8f2009-04-22 16:42:54 +000048 MultiClass *MC;
Matthias Braunc66e7552016-12-05 06:41:54 +000049 SmallVector<Init*, 4> TemplateArgs;
Eugene Zelenko33d7b762016-08-23 17:14:32 +000050
Craig Topper011817a2014-04-09 04:50:04 +000051 SubMultiClassReference() : MC(nullptr) {}
Bob Wilson3d948162009-04-28 19:41:44 +000052
Craig Topper011817a2014-04-09 04:50:04 +000053 bool isInvalid() const { return MC == nullptr; }
David Greene7049e792009-04-24 16:55:41 +000054 void dump() const;
David Greene753ed8f2009-04-22 16:42:54 +000055};
David Greene7049e792009-04-24 16:55:41 +000056
Don Hinton3e0199f2017-10-12 16:16:06 +000057#ifdef LLVM_ENABLE_DUMP
Yaron Kereneb2a2542016-01-29 20:50:44 +000058LLVM_DUMP_METHOD void SubMultiClassReference::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000059 errs() << "Multiclass:\n";
Bob Wilson7248f862009-11-22 04:24:42 +000060
David Greene7049e792009-04-24 16:55:41 +000061 MC->dump();
Bob Wilson7248f862009-11-22 04:24:42 +000062
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000063 errs() << "Template args:\n";
Craig Toppera9642b42015-05-04 01:35:39 +000064 for (Init *TA : TemplateArgs)
Craig Toppereb4d7c62015-04-29 04:43:36 +000065 TA->dump();
David Greene7049e792009-04-24 16:55:41 +000066}
Matthias Braun25bcaba2017-01-28 02:47:46 +000067#endif
David Greene7049e792009-04-24 16:55:41 +000068
Chris Lattnerf4127dd2007-11-22 20:49:04 +000069} // end namespace llvm
70
Chris Lattner526c8cb2009-06-21 03:39:35 +000071bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Craig Topper011817a2014-04-09 04:50:04 +000072 if (!CurRec)
Chris Lattnerf4127dd2007-11-22 20:49:04 +000073 CurRec = &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +000074
Jakob Stoklund Olesen9d1c5ee2012-01-13 03:16:35 +000075 if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000076 // The value already exists in the class, treat this as a set.
77 if (ERV->setValue(RV.getValue()))
78 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
79 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson7248f862009-11-22 04:24:42 +000080 "previous definition of type '" +
Chris Lattnerf4127dd2007-11-22 20:49:04 +000081 ERV->getType()->getAsString() + "'");
82 } else {
83 CurRec->addValue(RV);
84 }
85 return false;
86}
87
88/// SetValue -
89/// Return true on error, false on success.
David Greene3ca42122011-10-19 13:02:39 +000090bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
Craig Toppercfd81732016-01-04 03:15:08 +000091 ArrayRef<unsigned> BitList, Init *V,
Craig Topper1e23ed92016-01-04 03:05:14 +000092 bool AllowSelfAssignment) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000093 if (!V) return false;
94
Craig Topper011817a2014-04-09 04:50:04 +000095 if (!CurRec) CurRec = &CurMultiClass->Rec;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000096
97 RecordVal *RV = CurRec->getValue(ValName);
Craig Topper011817a2014-04-09 04:50:04 +000098 if (!RV)
Craig Topper85c07002015-04-30 05:54:22 +000099 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
100 "' unknown!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000101
102 // Do not allow assignments like 'X = X'. This will just cause infinite loops
103 // in the resolution machinery.
104 if (BitList.empty())
Sean Silvafb509ed2012-10-10 20:24:43 +0000105 if (VarInit *VI = dyn_cast<VarInit>(V))
Craig Topper1e23ed92016-01-04 03:05:14 +0000106 if (VI->getNameInit() == ValName && !AllowSelfAssignment)
107 return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000108
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000109 // If we are assigning to a subset of the bits in the value... then we must be
110 // assigning to a field of BitsRecTy, which must have a BitsInit
111 // initializer.
112 //
113 if (!BitList.empty()) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000114 BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue());
Craig Topper011817a2014-04-09 04:50:04 +0000115 if (!CurVal)
Craig Topper85c07002015-04-30 05:54:22 +0000116 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
117 "' is not a bits type");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000118
119 // Convert the incoming value to a bits type of the appropriate size...
David Greeneaf8ee2c2011-07-29 22:43:06 +0000120 Init *BI = V->convertInitializerTo(BitsRecTy::get(BitList.size()));
Craig Toppera9642b42015-05-04 01:35:39 +0000121 if (!BI)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000122 return Error(Loc, "Initializer is not compatible with bit range");
Bob Wilson7248f862009-11-22 04:24:42 +0000123
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000124 // We should have a BitsInit type now.
Craig Toppered5a9502015-04-29 07:13:05 +0000125 BitsInit *BInit = cast<BitsInit>(BI);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000126
David Greeneaf8ee2c2011-07-29 22:43:06 +0000127 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000128
129 // Loop over bits, assigning values as appropriate.
130 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
131 unsigned Bit = BitList[i];
David Greeneb3da8122011-07-29 19:07:00 +0000132 if (NewBits[Bit])
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000133 return Error(Loc, "Cannot set bit #" + Twine(Bit) + " of value '" +
David Greene3ca42122011-10-19 13:02:39 +0000134 ValName->getAsUnquotedString() + "' more than once");
David Greeneb3da8122011-07-29 19:07:00 +0000135 NewBits[Bit] = BInit->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000136 }
137
138 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
Craig Topper011817a2014-04-09 04:50:04 +0000139 if (!NewBits[i])
David Greeneb3da8122011-07-29 19:07:00 +0000140 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000141
David Greenee32ebf22011-07-29 19:07:07 +0000142 V = BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000143 }
144
Pete Cooper040c6a62014-07-31 01:43:57 +0000145 if (RV->setValue(V)) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000146 std::string InitType;
Craig Toppera9642b42015-05-04 01:35:39 +0000147 if (BitsInit *BI = dyn_cast<BitsInit>(V))
Pete Cooper040c6a62014-07-31 01:43:57 +0000148 InitType = (Twine("' of type bit initializer with length ") +
149 Twine(BI->getNumBits())).str();
Craig Topper85c07002015-04-30 05:54:22 +0000150 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
151 "' of type '" + RV->getType()->getAsString() +
152 "' is incompatible with initializer '" + V->getAsString() +
153 InitType + "'");
Pete Cooper040c6a62014-07-31 01:43:57 +0000154 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000155 return false;
156}
157
158/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
159/// args as SubClass's template arguments.
Cedric Venetd1e179d2009-02-14 16:06:42 +0000160bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000161 Record *SC = SubClass.Rec;
162 // Add all of the values in the subclass into the current class.
Craig Topper8eb764c2015-06-02 06:19:28 +0000163 for (const RecordVal &Val : SC->getValues())
164 if (AddValue(CurRec, SubClass.RefRange.Start, Val))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000165 return true;
166
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000167 ArrayRef<Init *> TArgs = SC->getTemplateArgs();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000168
169 // Ensure that an appropriate number of template arguments are specified.
170 if (TArgs.size() < SubClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000171 return Error(SubClass.RefRange.Start,
172 "More template args specified than expected");
Bob Wilson7248f862009-11-22 04:24:42 +0000173
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000174 // Loop over all of the template arguments, setting them to the specified
175 // value or leaving them as the default if necessary.
176 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
177 if (i < SubClass.TemplateArgs.size()) {
178 // If a value is specified for this template arg, set it now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000179 if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000180 None, SubClass.TemplateArgs[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000181 return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000182
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000183 // Resolve it next.
184 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson7248f862009-11-22 04:24:42 +0000185
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000186 // Now remove it.
187 CurRec->removeValue(TArgs[i]);
188
189 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000190 return Error(SubClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000191 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000192 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000193 ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000194 }
195 }
196
197 // Since everything went well, we can now set the "superclass" list for the
198 // current record.
Craig Topper0e41d0b2016-01-18 19:52:37 +0000199 ArrayRef<std::pair<Record *, SMRange>> SCs = SC->getSuperClasses();
200 for (const auto &SCPair : SCs) {
201 if (CurRec->isSubClassOf(SCPair.first))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000202 return Error(SubClass.RefRange.Start,
Craig Topper0e41d0b2016-01-18 19:52:37 +0000203 "Already subclass of '" + SCPair.first->getName() + "'!\n");
204 CurRec->addSuperClass(SCPair.first, SCPair.second);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000205 }
Bob Wilson7248f862009-11-22 04:24:42 +0000206
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000207 if (CurRec->isSubClassOf(SC))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000208 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000209 "Already subclass of '" + SC->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000210 CurRec->addSuperClass(SC, SubClass.RefRange);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000211 return false;
212}
213
David Greene753ed8f2009-04-22 16:42:54 +0000214/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilsonf71e6562009-04-30 18:26:19 +0000215/// CurMC, resolving its template args as SubMultiClass's
David Greene753ed8f2009-04-22 16:42:54 +0000216/// template arguments.
Bob Wilsonf71e6562009-04-30 18:26:19 +0000217bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson92ab8202009-04-30 17:46:20 +0000218 SubMultiClassReference &SubMultiClass) {
David Greene753ed8f2009-04-22 16:42:54 +0000219 MultiClass *SMC = SubMultiClass.MC;
Bob Wilsonf71e6562009-04-30 18:26:19 +0000220 Record *CurRec = &CurMC->Rec;
David Greene753ed8f2009-04-22 16:42:54 +0000221
David Greene753ed8f2009-04-22 16:42:54 +0000222 // Add all of the values in the subclass into the current class.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000223 for (const auto &SMCVal : SMC->Rec.getValues())
224 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000225 return true;
226
Craig Toppera4ea4b02014-11-30 00:24:32 +0000227 unsigned newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000228
David Greene753ed8f2009-04-22 16:42:54 +0000229 // Add all of the defs in the subclass into the current multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000230 for (const std::unique_ptr<Record> &R : SMC->DefPrototypes) {
David Greene753ed8f2009-04-22 16:42:54 +0000231 // Clone the def and add it to the current multiclass
Craig Toppereb4d7c62015-04-29 04:43:36 +0000232 auto NewDef = make_unique<Record>(*R);
David Greene753ed8f2009-04-22 16:42:54 +0000233
234 // Add all of the values in the superclass into the current def.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000235 for (const auto &MCVal : CurRec->getValues())
236 if (AddValue(NewDef.get(), SubMultiClass.RefRange.Start, MCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000237 return true;
238
Craig Topperc3504c42014-12-11 05:25:33 +0000239 CurMC->DefPrototypes.push_back(std::move(NewDef));
David Greene753ed8f2009-04-22 16:42:54 +0000240 }
Bob Wilson3d948162009-04-28 19:41:44 +0000241
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000242 ArrayRef<Init *> SMCTArgs = SMC->Rec.getTemplateArgs();
David Greene753ed8f2009-04-22 16:42:54 +0000243
David Greene7049e792009-04-24 16:55:41 +0000244 // Ensure that an appropriate number of template arguments are
245 // specified.
David Greene753ed8f2009-04-22 16:42:54 +0000246 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000247 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000248 "More template args specified than expected");
Bob Wilson3d948162009-04-28 19:41:44 +0000249
David Greene753ed8f2009-04-22 16:42:54 +0000250 // Loop over all of the template arguments, setting them to the specified
251 // value or leaving them as the default if necessary.
252 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
253 if (i < SubMultiClass.TemplateArgs.size()) {
David Greene7049e792009-04-24 16:55:41 +0000254 // If a value is specified for this template arg, set it in the
255 // superclass now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000256 if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000257 None, SubMultiClass.TemplateArgs[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000258 return true;
259
260 // Resolve it next.
261 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson3d948162009-04-28 19:41:44 +0000262
David Greene753ed8f2009-04-22 16:42:54 +0000263 // Now remove it.
264 CurRec->removeValue(SMCTArgs[i]);
265
David Greene7049e792009-04-24 16:55:41 +0000266 // If a value is specified for this template arg, set it in the
267 // new defs now.
Craig Topperc3504c42014-12-11 05:25:33 +0000268 for (const auto &Def :
269 makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
270 if (SetValue(Def.get(), SubMultiClass.RefRange.Start, SMCTArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000271 None, SubMultiClass.TemplateArgs[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000272 return true;
273
274 // Resolve it next.
275 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
276
277 // Now remove it
278 Def->removeValue(SMCTArgs[i]);
279 }
280 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000281 return Error(SubMultiClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000282 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000283 Twine(i) + " (" + SMCTArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000284 ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000285 }
286 }
287
288 return false;
289}
290
David Greenefb927af2012-02-22 16:09:41 +0000291/// ProcessForeachDefs - Given a record, apply all of the variable
292/// values in all surrounding foreach loops, creating new records for
293/// each combination of values.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000294bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
295 if (Loops.empty())
296 return false;
297
David Greenefb927af2012-02-22 16:09:41 +0000298 // We want to instantiate a new copy of CurRec for each combination
299 // of nested loop iterator values. We don't want top instantiate
300 // any copies until we have values for each loop iterator.
301 IterSet IterVals;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000302 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000303}
304
305/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
306/// apply each of the variable values in this loop and then process
307/// subloops.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000308bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
309 // Recursively build a tuple of iterator values.
310 if (IterVals.size() != Loops.size()) {
311 assert(IterVals.size() < Loops.size());
312 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silvafb509ed2012-10-10 20:24:43 +0000313 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Craig Topper011817a2014-04-09 04:50:04 +0000314 if (!List) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000315 Error(Loc, "Loop list is not a list");
316 return true;
317 }
David Greenefb927af2012-02-22 16:09:41 +0000318
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000319 // Process each value.
Craig Topper664f6a02015-06-02 04:15:57 +0000320 for (unsigned i = 0; i < List->size(); ++i) {
Craig Topper011817a2014-04-09 04:50:04 +0000321 Init *ItemVal = List->resolveListElementReference(*CurRec, nullptr, i);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000322 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
323 if (ProcessForeachDefs(CurRec, Loc, IterVals))
324 return true;
325 IterVals.pop_back();
326 }
327 return false;
328 }
329
330 // This is the bottom of the recursion. We have all of the iterator values
331 // for this point in the iteration space. Instantiate a new record to
332 // reflect this combination of values.
Craig Topper84138712014-11-29 05:31:10 +0000333 auto IterRec = make_unique<Record>(*CurRec);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000334
335 // Set the iterator values now.
Craig Topper8eb764c2015-06-02 06:19:28 +0000336 for (IterRecord &IR : IterVals) {
337 VarInit *IterVar = IR.IterVar;
338 TypedInit *IVal = dyn_cast<TypedInit>(IR.IterValue);
Craig Topper84138712014-11-29 05:31:10 +0000339 if (!IVal)
340 return Error(Loc, "foreach iterator value is untyped");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000341
Craig Topperf4412262017-06-01 06:56:16 +0000342 IterRec->addValue(RecordVal(IterVar->getNameInit(), IVal->getType(), false));
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000343
Matthias Braun215ff842016-12-05 07:35:13 +0000344 if (SetValue(IterRec.get(), Loc, IterVar->getNameInit(), None, IVal))
Craig Topper84138712014-11-29 05:31:10 +0000345 return Error(Loc, "when instantiating this def");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000346
347 // Resolve it next.
Matthias Braun215ff842016-12-05 07:35:13 +0000348 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getNameInit()));
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000349
350 // Remove it.
Matthias Braun215ff842016-12-05 07:35:13 +0000351 IterRec->removeValue(IterVar->getNameInit());
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000352 }
353
354 if (Records.getDef(IterRec->getNameInitAsString())) {
Artyom Skrobov8b985322014-06-10 12:41:14 +0000355 // If this record is anonymous, it's no problem, just generate a new name
Craig Topper84138712014-11-29 05:31:10 +0000356 if (!IterRec->isAnonymous())
357 return Error(Loc, "def already exists: " +IterRec->getNameInitAsString());
358
359 IterRec->setName(GetNewAnonymousName());
David Greenefb927af2012-02-22 16:09:41 +0000360 }
361
Craig Topper84138712014-11-29 05:31:10 +0000362 Record *IterRecSave = IterRec.get(); // Keep a copy before release.
Craig Toppercdab2322014-11-29 05:52:51 +0000363 Records.addDef(std::move(IterRec));
Craig Topper84138712014-11-29 05:31:10 +0000364 IterRecSave->resolveReferences();
David Greenefb927af2012-02-22 16:09:41 +0000365 return false;
366}
367
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000368//===----------------------------------------------------------------------===//
369// Parser Code
370//===----------------------------------------------------------------------===//
371
372/// isObjectStart - Return true if this is a valid first token for an Object.
373static bool isObjectStart(tgtok::TokKind K) {
374 return K == tgtok::Class || K == tgtok::Def ||
David Greenefb927af2012-02-22 16:09:41 +0000375 K == tgtok::Defm || K == tgtok::Let ||
376 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000377}
378
Alp Tokerce91fe52013-12-21 18:51:00 +0000379/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
380/// an identifier.
Craig Topperf4412262017-06-01 06:56:16 +0000381Init *TGParser::GetNewAnonymousName() {
382 return StringInit::get("anonymous_" + utostr(AnonCounter++));
Chris Lattner7538ed82010-10-05 22:51:56 +0000383}
384
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000385/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000386/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000387/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000388/// ObjectName ::= /*empty*/
389///
David Greene2affd672011-10-19 13:04:29 +0000390Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
391 switch (Lex.getCode()) {
392 case tgtok::colon:
393 case tgtok::semi:
394 case tgtok::l_brace:
395 // These are all of the tokens that can begin an object body.
396 // Some of these can also begin values but we disallow those cases
397 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000398 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000399 default:
400 break;
401 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000402
Craig Topper011817a2014-04-09 04:50:04 +0000403 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000404 if (CurMultiClass)
405 CurRec = &CurMultiClass->Rec;
406
Craig Topper011817a2014-04-09 04:50:04 +0000407 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000408 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000409 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000410 if (!CurRecName) {
411 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000412 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000413 }
414 Type = CurRecName->getType();
415 }
416
417 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000418}
419
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000420/// ParseClassID - Parse and resolve a reference to a class name. This returns
421/// null on error.
422///
423/// ClassID ::= ID
424///
425Record *TGParser::ParseClassID() {
426 if (Lex.getCode() != tgtok::Id) {
427 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000428 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000429 }
Bob Wilson7248f862009-11-22 04:24:42 +0000430
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000431 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000432 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000433 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000434
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000435 Lex.Lex();
436 return Result;
437}
438
Bob Wilson3d948162009-04-28 19:41:44 +0000439/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
440/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000441///
442/// MultiClassID ::= ID
443///
444MultiClass *TGParser::ParseMultiClassID() {
445 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000446 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000447 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000448 }
Bob Wilson3d948162009-04-28 19:41:44 +0000449
Craig Topper7adf2bf2014-12-11 05:25:30 +0000450 MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
Craig Topper4ca40012014-11-30 01:20:17 +0000451 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000452 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000453
David Greene753ed8f2009-04-22 16:42:54 +0000454 Lex.Lex();
Craig Topper4ca40012014-11-30 01:20:17 +0000455 return Result;
David Greene753ed8f2009-04-22 16:42:54 +0000456}
457
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000458/// ParseSubClassReference - Parse a reference to a subclass or to a templated
459/// subclass. This returns a SubClassRefTy with a null Record* on error.
460///
461/// SubClassRef ::= ClassID
462/// SubClassRef ::= ClassID '<' ValueList '>'
463///
464SubClassReference TGParser::
465ParseSubClassReference(Record *CurRec, bool isDefm) {
466 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000467 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000468
Sean Silva0657b402013-01-09 02:17:14 +0000469 if (isDefm) {
470 if (MultiClass *MC = ParseMultiClassID())
471 Result.Rec = &MC->Rec;
472 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000473 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000474 }
Craig Topper011817a2014-04-09 04:50:04 +0000475 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000476
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000477 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000478 if (Lex.getCode() != tgtok::less) {
479 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000480 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000481 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000482 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000483
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000484 if (Lex.getCode() == tgtok::greater) {
485 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000486 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000487 return Result;
488 }
Bob Wilson7248f862009-11-22 04:24:42 +0000489
Matthias Braunc66e7552016-12-05 06:41:54 +0000490 ParseValueList(Result.TemplateArgs, CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000491 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000492 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000493 return Result;
494 }
Bob Wilson7248f862009-11-22 04:24:42 +0000495
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000496 if (Lex.getCode() != tgtok::greater) {
497 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000498 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000499 return Result;
500 }
501 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000502 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000503
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000504 return Result;
505}
506
Bob Wilson3d948162009-04-28 19:41:44 +0000507/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
508/// templated submulticlass. This returns a SubMultiClassRefTy with a null
509/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000510///
511/// SubMultiClassRef ::= MultiClassID
512/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
513///
514SubMultiClassReference TGParser::
515ParseSubMultiClassReference(MultiClass *CurMC) {
516 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000517 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000518
David Greene753ed8f2009-04-22 16:42:54 +0000519 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000520 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000521
David Greene753ed8f2009-04-22 16:42:54 +0000522 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000523 if (Lex.getCode() != tgtok::less) {
524 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000525 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000526 }
David Greene753ed8f2009-04-22 16:42:54 +0000527 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000528
David Greene753ed8f2009-04-22 16:42:54 +0000529 if (Lex.getCode() == tgtok::greater) {
530 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000531 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000532 return Result;
533 }
Bob Wilson3d948162009-04-28 19:41:44 +0000534
Matthias Braunc66e7552016-12-05 06:41:54 +0000535 ParseValueList(Result.TemplateArgs, &CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000536 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000537 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000538 return Result;
539 }
Bob Wilson3d948162009-04-28 19:41:44 +0000540
David Greene753ed8f2009-04-22 16:42:54 +0000541 if (Lex.getCode() != tgtok::greater) {
542 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000543 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000544 return Result;
545 }
546 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000547 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000548
549 return Result;
550}
551
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000552/// ParseRangePiece - Parse a bit/value range.
553/// RangePiece ::= INTVAL
554/// RangePiece ::= INTVAL '-' INTVAL
555/// RangePiece ::= INTVAL INTVAL
Matthias Braunc66e7552016-12-05 06:41:54 +0000556bool TGParser::ParseRangePiece(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000557 if (Lex.getCode() != tgtok::IntVal) {
558 TokError("expected integer or bitrange");
559 return true;
560 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000561 int64_t Start = Lex.getCurIntVal();
562 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000563
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000564 if (Start < 0)
565 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000566
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000567 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000568 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000569 Ranges.push_back(Start);
570 return false;
571 case tgtok::minus:
572 if (Lex.Lex() != tgtok::IntVal) {
573 TokError("expected integer value as end of range");
574 return true;
575 }
576 End = Lex.getCurIntVal();
577 break;
578 case tgtok::IntVal:
579 End = -Lex.getCurIntVal();
580 break;
581 }
Bob Wilson7248f862009-11-22 04:24:42 +0000582 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000583 return TokError("invalid range, cannot be negative");
584 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000585
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000586 // Add to the range.
Craig Toppera9642b42015-05-04 01:35:39 +0000587 if (Start < End)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000588 for (; Start <= End; ++Start)
589 Ranges.push_back(Start);
Craig Toppera9642b42015-05-04 01:35:39 +0000590 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000591 for (; Start >= End; --Start)
592 Ranges.push_back(Start);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000593 return false;
594}
595
596/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
597///
598/// RangeList ::= RangePiece (',' RangePiece)*
599///
Matthias Braunc66e7552016-12-05 06:41:54 +0000600void TGParser::ParseRangeList(SmallVectorImpl<unsigned> &Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000601 // Parse the first piece.
Matthias Braunc66e7552016-12-05 06:41:54 +0000602 if (ParseRangePiece(Result)) {
603 Result.clear();
604 return;
605 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000606 while (Lex.getCode() == tgtok::comma) {
607 Lex.Lex(); // Eat the comma.
608
609 // Parse the next range piece.
Matthias Braunc66e7552016-12-05 06:41:54 +0000610 if (ParseRangePiece(Result)) {
611 Result.clear();
612 return;
613 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000614 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000615}
616
617/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
618/// OptionalRangeList ::= '<' RangeList '>'
619/// OptionalRangeList ::= /*empty*/
Matthias Braunc66e7552016-12-05 06:41:54 +0000620bool TGParser::ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000621 if (Lex.getCode() != tgtok::less)
622 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000623
Chris Lattner526c8cb2009-06-21 03:39:35 +0000624 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000625 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000626
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000627 // Parse the range list.
Matthias Braunc66e7552016-12-05 06:41:54 +0000628 ParseRangeList(Ranges);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000629 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000630
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000631 if (Lex.getCode() != tgtok::greater) {
632 TokError("expected '>' at end of range list");
633 return Error(StartLoc, "to match this '<'");
634 }
635 Lex.Lex(); // eat the '>'.
636 return false;
637}
638
639/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
640/// OptionalBitList ::= '{' RangeList '}'
641/// OptionalBitList ::= /*empty*/
Matthias Braunc66e7552016-12-05 06:41:54 +0000642bool TGParser::ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000643 if (Lex.getCode() != tgtok::l_brace)
644 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000645
Chris Lattner526c8cb2009-06-21 03:39:35 +0000646 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000647 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000648
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000649 // Parse the range list.
Matthias Braunc66e7552016-12-05 06:41:54 +0000650 ParseRangeList(Ranges);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000651 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000652
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000653 if (Lex.getCode() != tgtok::r_brace) {
654 TokError("expected '}' at end of bit list");
655 return Error(StartLoc, "to match this '{'");
656 }
657 Lex.Lex(); // eat the '}'.
658 return false;
659}
660
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000661/// ParseType - Parse and return a tblgen type. This returns null on error.
662///
663/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000664/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000665/// Type ::= BIT // bit type
666/// Type ::= BITS '<' INTVAL '>' // bits<x> type
667/// Type ::= INT // int type
668/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000669/// Type ::= DAG // dag type
670/// Type ::= ClassID // Record Type
671///
672RecTy *TGParser::ParseType() {
673 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000674 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000675 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Tim Northover88403d72016-07-05 21:22:55 +0000676 case tgtok::Code: Lex.Lex(); return CodeRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000677 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
678 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000679 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000680 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000681 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000682 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000683 case tgtok::Bits: {
684 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
685 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000686 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000687 }
688 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
689 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000690 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000691 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000692 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000693 if (Lex.Lex() != tgtok::greater) { // Eat count.
694 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000695 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000696 }
697 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000698 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000699 }
700 case tgtok::List: {
701 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
702 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000703 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000704 }
705 Lex.Lex(); // Eat '<'
706 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000707 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000708
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000709 if (Lex.getCode() != tgtok::greater) {
710 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000711 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000712 }
713 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000714 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000715 }
Bob Wilson7248f862009-11-22 04:24:42 +0000716 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000717}
718
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000719/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
720/// has already been read.
Matthias Braun215ff842016-12-05 07:35:13 +0000721Init *TGParser::ParseIDValue(Record *CurRec, StringInit *Name, SMLoc NameLoc,
David Greened4263a62011-10-19 13:04:20 +0000722 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000723 if (CurRec) {
724 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000725 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000726
Matthias Braun215ff842016-12-05 07:35:13 +0000727 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
David Greenedb10e692011-10-19 13:02:42 +0000728
David Greene47a665e2011-10-05 22:42:54 +0000729 if (CurMultiClass)
Matthias Braun215ff842016-12-05 07:35:13 +0000730 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
David Greenedb10e692011-10-19 13:02:42 +0000731 "::");
David Greene47a665e2011-10-05 22:42:54 +0000732
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000733 if (CurRec->isTemplateArg(TemplateArgName)) {
734 const RecordVal *RV = CurRec->getValue(TemplateArgName);
735 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000736 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000737 }
Matthias Braun215ff842016-12-05 07:35:13 +0000738 }
Bob Wilson7248f862009-11-22 04:24:42 +0000739
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000740 if (CurMultiClass) {
Matthias Braun215ff842016-12-05 07:35:13 +0000741 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name, "::");
David Greenedb10e692011-10-19 13:02:42 +0000742
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000743 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
744 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
745 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000746 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000747 }
748 }
Bob Wilson7248f862009-11-22 04:24:42 +0000749
David Greenefb927af2012-02-22 16:09:41 +0000750 // If this is in a foreach loop, make sure it's not a loop iterator
Craig Toppereb4d7c62015-04-29 04:43:36 +0000751 for (const auto &L : Loops) {
752 VarInit *IterVar = dyn_cast<VarInit>(L.IterVar);
Matthias Braun215ff842016-12-05 07:35:13 +0000753 if (IterVar && IterVar->getNameInit() == Name)
David Greenefb927af2012-02-22 16:09:41 +0000754 return IterVar;
755 }
756
David Greene232bd602011-10-19 13:04:21 +0000757 if (Mode == ParseNameMode)
Matthias Braun215ff842016-12-05 07:35:13 +0000758 return Name;
David Greene232bd602011-10-19 13:04:21 +0000759
Matthias Braun215ff842016-12-05 07:35:13 +0000760 if (Record *D = Records.getDef(Name->getValue()))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000761 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000762
David Greene232bd602011-10-19 13:04:21 +0000763 if (Mode == ParseValueMode) {
Matthias Braun215ff842016-12-05 07:35:13 +0000764 Error(NameLoc, "Variable not defined: '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000765 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000766 }
Craig Toppera9642b42015-05-04 01:35:39 +0000767
Matthias Braun215ff842016-12-05 07:35:13 +0000768 return Name;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000769}
770
David Greene5d0c0512009-05-14 20:54:48 +0000771/// ParseOperation - Parse an operator. This returns null on error.
772///
773/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
774///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000775Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000776 switch (Lex.getCode()) {
777 default:
778 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000779 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000780 case tgtok::XHead:
781 case tgtok::XTail:
782 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000783 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
784 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000785 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000786
David Greenee8f3b272009-05-14 21:22:49 +0000787 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000788 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000789 case tgtok::XCast:
790 Lex.Lex(); // eat the operation
791 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000792
David Greenee8f3b272009-05-14 21:22:49 +0000793 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000794
Craig Topper011817a2014-04-09 04:50:04 +0000795 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000796 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000797 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000798 }
David Greene5d0c0512009-05-14 20:54:48 +0000799
David Greenee8f3b272009-05-14 21:22:49 +0000800 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000801 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000802 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000803 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000804 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000805 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000806 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000807 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000808 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000809 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000810 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000811 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000812 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000813 break;
David Greenee8f3b272009-05-14 21:22:49 +0000814 }
815 if (Lex.getCode() != tgtok::l_paren) {
816 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000817 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000818 }
819 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000820
David Greeneaf8ee2c2011-07-29 22:43:06 +0000821 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000822 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000823
Craig Topper85c07002015-04-30 05:54:22 +0000824 if (Code == UnOpInit::HEAD ||
825 Code == UnOpInit::TAIL ||
826 Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000827 ListInit *LHSl = dyn_cast<ListInit>(LHS);
828 StringInit *LHSs = dyn_cast<StringInit>(LHS);
829 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000830 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000831 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000832 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000833 }
834 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000835 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
836 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000837 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000838 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000839 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000840 }
841 }
842
Craig Topper85c07002015-04-30 05:54:22 +0000843 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
Craig Topper011817a2014-04-09 04:50:04 +0000844 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000845 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000846 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000847 }
Bob Wilson7248f862009-11-22 04:24:42 +0000848
Craig Topperec9072d2015-05-14 05:53:53 +0000849 if (LHSl && LHSl->empty()) {
David Greened571b3c2009-05-14 22:38:31 +0000850 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000851 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000852 }
853 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000854 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000855 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000856 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000857 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000858 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000859 }
Craig Toppera9642b42015-05-04 01:35:39 +0000860 Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
861 : ListRecTy::get(Itemt->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000862 } else {
David Greened571b3c2009-05-14 22:38:31 +0000863 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000864 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000865 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000866 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000867 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000868 }
Craig Toppera9642b42015-05-04 01:35:39 +0000869 Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
David Greened571b3c2009-05-14 22:38:31 +0000870 }
871 }
872 }
873
David Greenee8f3b272009-05-14 21:22:49 +0000874 if (Lex.getCode() != tgtok::r_paren) {
875 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000876 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000877 }
878 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000879 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000880 }
David Greene5d0c0512009-05-14 20:54:48 +0000881
882 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000883 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000884 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000885 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +0000886 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000887 case tgtok::XSRL:
888 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000889 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000890 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000891 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000892 tgtok::TokKind OpTok = Lex.getCode();
893 SMLoc OpLoc = Lex.getLoc();
894 Lex.Lex(); // eat the operation
895
David Greene5d0c0512009-05-14 20:54:48 +0000896 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000897 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000898
Chris Lattner61ea00b2010-10-05 23:58:18 +0000899 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000900 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000901 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000902 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000903 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000904 case tgtok::XOR: Code = BinOpInit::OR; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000905 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
906 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
907 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
908 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000909 case tgtok::XListConcat:
910 Code = BinOpInit::LISTCONCAT;
911 // We don't know the list type until we parse the first argument
912 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000913 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000914 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000915 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000916 break;
David Greene5d0c0512009-05-14 20:54:48 +0000917 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000918
David Greene5d0c0512009-05-14 20:54:48 +0000919 if (Lex.getCode() != tgtok::l_paren) {
920 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000921 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000922 }
923 Lex.Lex(); // eat the '('
924
David Greeneaf8ee2c2011-07-29 22:43:06 +0000925 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000926
Chris Lattner61ea00b2010-10-05 23:58:18 +0000927 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000928 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000929
Chris Lattner61ea00b2010-10-05 23:58:18 +0000930 while (Lex.getCode() == tgtok::comma) {
931 Lex.Lex(); // eat the ','
932
933 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000934 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000935 }
David Greene5d0c0512009-05-14 20:54:48 +0000936
937 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000938 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000939 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000940 }
941 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000942
Daniel Sanders314e80e2014-05-07 10:13:19 +0000943 // If we are doing !listconcat, we should know the type by now
944 if (OpTok == tgtok::XListConcat) {
945 if (VarInit *Arg0 = dyn_cast<VarInit>(InitList[0]))
946 Type = Arg0->getType();
947 else if (ListInit *Arg0 = dyn_cast<ListInit>(InitList[0]))
948 Type = Arg0->getType();
949 else {
Matthias Braun8c209aa2017-01-28 02:02:38 +0000950 InitList[0]->print(errs());
Daniel Sanders314e80e2014-05-07 10:13:19 +0000951 Error(OpLoc, "expected a list");
952 return nullptr;
953 }
954 }
955
Chris Lattner61ea00b2010-10-05 23:58:18 +0000956 // We allow multiple operands to associative operators like !strconcat as
957 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000958 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000959 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000960 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000961 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
962 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000963 InitList.back() = RHS;
964 }
965 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000966
Chris Lattner61ea00b2010-10-05 23:58:18 +0000967 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000968 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000969 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000970
Chris Lattner61ea00b2010-10-05 23:58:18 +0000971 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +0000972 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000973 }
974
David Greene3587eed2009-05-14 23:26:46 +0000975 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +0000976 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +0000977 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
978 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000979 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000980
David Greene98ed3c72009-05-14 21:54:42 +0000981 tgtok::TokKind LexCode = Lex.getCode();
982 Lex.Lex(); // eat the operation
983 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +0000984 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +0000985 case tgtok::XIf:
986 Code = TernOpInit::IF;
987 break;
David Greenee917fff2009-05-14 22:23:47 +0000988 case tgtok::XForEach:
989 Code = TernOpInit::FOREACH;
990 break;
David Greene98ed3c72009-05-14 21:54:42 +0000991 case tgtok::XSubst:
992 Code = TernOpInit::SUBST;
993 break;
994 }
995 if (Lex.getCode() != tgtok::l_paren) {
996 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000997 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +0000998 }
999 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001000
David Greeneaf8ee2c2011-07-29 22:43:06 +00001001 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001002 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001003
David Greene98ed3c72009-05-14 21:54:42 +00001004 if (Lex.getCode() != tgtok::comma) {
1005 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001006 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001007 }
1008 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001009
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001010 Init *MHS = ParseValue(CurRec, ItemType);
1011 if (!MHS)
1012 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001013
David Greene98ed3c72009-05-14 21:54:42 +00001014 if (Lex.getCode() != tgtok::comma) {
1015 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001016 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001017 }
1018 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001019
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001020 Init *RHS = ParseValue(CurRec, ItemType);
1021 if (!RHS)
1022 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001023
David Greene98ed3c72009-05-14 21:54:42 +00001024 if (Lex.getCode() != tgtok::r_paren) {
1025 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001026 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001027 }
1028 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001029
David Greene98ed3c72009-05-14 21:54:42 +00001030 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001031 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001032 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001033 RecTy *MHSTy = nullptr;
1034 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001035
Sean Silvafb509ed2012-10-10 20:24:43 +00001036 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001037 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001038 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001039 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001040 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001041 MHSTy = BitRecTy::get();
1042
Sean Silvafb509ed2012-10-10 20:24:43 +00001043 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001044 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001045 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001046 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001047 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001048 RHSTy = BitRecTy::get();
1049
1050 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001051 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001052 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001053 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001054 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001055
1056 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001057 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001058 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001059 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001060
1061 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1062 Type = RHSTy;
1063 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1064 Type = MHSTy;
Bob Wilson7248f862009-11-22 04:24:42 +00001065 } else {
David Greene3587eed2009-05-14 23:26:46 +00001066 TokError("inconsistent types for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001067 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001068 }
1069 break;
1070 }
David Greenee917fff2009-05-14 22:23:47 +00001071 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001072 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
Craig Topper011817a2014-04-09 04:50:04 +00001073 if (!MHSt) {
David Greenee917fff2009-05-14 22:23:47 +00001074 TokError("could not get type for !foreach");
Craig Topper011817a2014-04-09 04:50:04 +00001075 return nullptr;
David Greenee917fff2009-05-14 22:23:47 +00001076 }
1077 Type = MHSt->getType();
1078 break;
1079 }
David Greene98ed3c72009-05-14 21:54:42 +00001080 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001081 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001082 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001083 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001084 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001085 }
1086 Type = RHSt->getType();
1087 break;
1088 }
1089 }
David Greenee32ebf22011-07-29 19:07:07 +00001090 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001091 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001092 }
David Greene5d0c0512009-05-14 20:54:48 +00001093 }
David Greene5d0c0512009-05-14 20:54:48 +00001094}
1095
1096/// ParseOperatorType - Parse a type for an operator. This returns
1097/// null on error.
1098///
1099/// OperatorType ::= '<' Type '>'
1100///
Dan Gohman1432ef82009-08-12 22:10:57 +00001101RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001102 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001103
1104 if (Lex.getCode() != tgtok::less) {
1105 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001106 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001107 }
1108 Lex.Lex(); // eat the <
1109
1110 Type = ParseType();
1111
Craig Topper011817a2014-04-09 04:50:04 +00001112 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001113 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001114 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001115 }
1116
1117 if (Lex.getCode() != tgtok::greater) {
1118 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001119 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001120 }
1121 Lex.Lex(); // eat the >
1122
1123 return Type;
1124}
1125
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001126/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1127///
1128/// SimpleValue ::= IDValue
1129/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001130/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001131/// SimpleValue ::= CODEFRAGMENT
1132/// SimpleValue ::= '?'
1133/// SimpleValue ::= '{' ValueList '}'
1134/// SimpleValue ::= ID '<' ValueListNE '>'
1135/// SimpleValue ::= '[' ValueList ']'
1136/// SimpleValue ::= '(' IDValue DagArgList ')'
1137/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001138/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001139/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1140/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1141/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001142/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001143/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1144///
David Greened4263a62011-10-19 13:04:20 +00001145Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1146 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001147 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001148 switch (Lex.getCode()) {
1149 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001150 case tgtok::paste:
1151 // This is a leading paste operation. This is deprecated but
1152 // still exists in some .td files. Ignore it.
1153 Lex.Lex(); // Skip '#'.
1154 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001155 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001156 case tgtok::BinaryIntVal: {
1157 auto BinaryVal = Lex.getCurBinaryIntVal();
1158 SmallVector<Init*, 16> Bits(BinaryVal.second);
1159 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001160 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001161 R = BitsInit::get(Bits);
1162 Lex.Lex();
1163 break;
1164 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001165 case tgtok::StrVal: {
1166 std::string Val = Lex.getCurStrVal();
1167 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001168
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001169 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001170 while (Lex.getCode() == tgtok::StrVal) {
1171 Val += Lex.getCurStrVal();
1172 Lex.Lex();
1173 }
Bob Wilson7248f862009-11-22 04:24:42 +00001174
David Greenee32ebf22011-07-29 19:07:07 +00001175 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001176 break;
1177 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001178 case tgtok::CodeFragment:
Tim Northover88403d72016-07-05 21:22:55 +00001179 R = CodeInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001180 Lex.Lex();
1181 break;
1182 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001183 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001184 Lex.Lex();
1185 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001186 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001187 SMLoc NameLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00001188 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001189 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001190 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001191
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001192 // Value ::= ID '<' ValueListNE '>'
1193 if (Lex.Lex() == tgtok::greater) {
1194 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001195 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001196 }
David Greene8618f952009-06-08 20:23:18 +00001197
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001198 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1199 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1200 // body.
Matthias Braun215ff842016-12-05 07:35:13 +00001201 Record *Class = Records.getClass(Name->getValue());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001202 if (!Class) {
Matthias Braun215ff842016-12-05 07:35:13 +00001203 Error(NameLoc, "Expected a class name, got '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001204 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001205 }
David Greene8618f952009-06-08 20:23:18 +00001206
Matthias Braunc66e7552016-12-05 06:41:54 +00001207 SubClassReference SCRef;
1208 ParseValueList(SCRef.TemplateArgs, CurRec, Class);
1209 if (SCRef.TemplateArgs.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.
Jordan Rosef12e8a92013-01-10 18:50:11 +00001222 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001223 SCRef.Rec = Class;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001224 // Add info about the subclass to NewRec.
Craig Topper84138712014-11-29 05:31:10 +00001225 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001226 return nullptr;
Craig Topper84138712014-11-29 05:31:10 +00001227
Hal Finkela8c1f462014-01-02 20:47:09 +00001228 if (!CurMultiClass) {
1229 NewRec->resolveReferences();
Craig Toppercdab2322014-11-29 05:52:51 +00001230 Records.addDef(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001231 } else {
Adam Nemete5a07162014-09-16 17:14:13 +00001232 // This needs to get resolved once the multiclass template arguments are
1233 // known before any use.
1234 NewRec->setResolveFirst(true);
Hal Finkela8c1f462014-01-02 20:47:09 +00001235 // Otherwise, we're inside a multiclass, add it to the multiclass.
Craig Topperc3504c42014-12-11 05:25:33 +00001236 CurMultiClass->DefPrototypes.push_back(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001237
1238 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00001239 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
1240 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Hal Finkela8c1f462014-01-02 20:47:09 +00001241 assert(RV && "Template arg doesn't exist?");
1242 NewRec->addValue(*RV);
1243 }
1244
1245 // We can't return the prototype def here, instead return:
1246 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1247 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1248 assert(MCNameRV && "multiclass record must have a NAME");
1249
1250 return UnOpInit::get(UnOpInit::CAST,
1251 BinOpInit::get(BinOpInit::STRCONCAT,
1252 VarInit::get(MCNameRV->getName(),
1253 MCNameRV->getType()),
1254 NewRec->getNameInit(),
1255 StringRecTy::get()),
1256 Class->getDefInit()->getType());
1257 }
Bob Wilson7248f862009-11-22 04:24:42 +00001258
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001259 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001260 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001261 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001262 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001263 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001264 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001265 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001266
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001267 if (Lex.getCode() != tgtok::r_brace) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001268 ParseValueList(Vals, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001269 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001270 }
1271 if (Lex.getCode() != tgtok::r_brace) {
1272 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001273 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001274 }
1275 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001276
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001277 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001278
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001279 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1280 // first. We'll first read everything in to a vector, then we can reverse
1281 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001282 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001283 // FIXME: The following two loops would not be duplicated
1284 // if the API was a little more orthogonal.
1285
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001286 // bits<n> values are allowed to initialize n bits.
1287 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1288 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1289 NewBits.push_back(BI->getBit((e - i) - 1));
1290 continue;
1291 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001292 // bits<n> can also come from variable initializers.
1293 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1294 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1295 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1296 NewBits.push_back(VI->getBit((e - i) - 1));
1297 continue;
1298 }
1299 // Fallthrough to try convert this to a bit.
1300 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001301 // All other values must be convertible to just a single bit.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001302 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001303 if (!Bit) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001304 Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
Chris Lattner52416952007-11-22 21:06:59 +00001305 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001306 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001307 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001308 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001309 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001310 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001311 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001312 }
1313 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1314 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001315 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001316
Craig Topper011817a2014-04-09 04:50:04 +00001317 RecTy *DeducedEltTy = nullptr;
1318 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001319
Craig Topper011817a2014-04-09 04:50:04 +00001320 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001321 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001322 if (!ListType) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001323 TokError(Twine("Type mismatch for list, expected list type, got ") +
1324 ItemType->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001325 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001326 }
1327 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001328 }
David Greene8618f952009-06-08 20:23:18 +00001329
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001330 if (Lex.getCode() != tgtok::r_square) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001331 ParseValueList(Vals, CurRec, nullptr,
1332 GivenListTy ? GivenListTy->getElementType() : nullptr);
Craig Topper011817a2014-04-09 04:50:04 +00001333 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001334 }
1335 if (Lex.getCode() != tgtok::r_square) {
1336 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001337 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001338 }
1339 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001340
Craig Topper011817a2014-04-09 04:50:04 +00001341 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001342 if (Lex.getCode() == tgtok::less) {
1343 // Optional list element type
1344 Lex.Lex(); // eat the '<'
1345
1346 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001347 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001348 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001349 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001350 }
1351
1352 if (Lex.getCode() != tgtok::greater) {
1353 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001354 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001355 }
1356 Lex.Lex(); // eat the '>'
1357 }
1358
1359 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001360 RecTy *EltTy = nullptr;
Craig Toppereb4d7c62015-04-29 04:43:36 +00001361 for (Init *V : Vals) {
1362 TypedInit *TArg = dyn_cast<TypedInit>(V);
Craig Topper011817a2014-04-09 04:50:04 +00001363 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001364 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001365 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001366 }
Craig Topper011817a2014-04-09 04:50:04 +00001367 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001368 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001369 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001370 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001371 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001372 }
Bob Wilson7248f862009-11-22 04:24:42 +00001373 } else {
David Greene8618f952009-06-08 20:23:18 +00001374 EltTy = TArg->getType();
1375 }
1376 }
1377
Craig Topper011817a2014-04-09 04:50:04 +00001378 if (GivenEltTy) {
1379 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001380 // Verify consistency
1381 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1382 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001383 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001384 }
1385 }
1386 EltTy = GivenEltTy;
1387 }
1388
Craig Topper011817a2014-04-09 04:50:04 +00001389 if (!EltTy) {
1390 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001391 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001392 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001393 }
1394 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001395 } else {
David Greene8618f952009-06-08 20:23:18 +00001396 // Make sure the deduced type is compatible with the given type
1397 if (GivenListTy) {
1398 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1399 TokError("Element type mismatch for list");
Craig Topper011817a2014-04-09 04:50:04 +00001400 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001401 }
1402 }
1403 DeducedEltTy = EltTy;
1404 }
Bob Wilson7248f862009-11-22 04:24:42 +00001405
David Greenee32ebf22011-07-29 19:07:07 +00001406 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001407 }
1408 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1409 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001410 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001411 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001412 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001413 }
Bob Wilson7248f862009-11-22 04:24:42 +00001414
David Greeneaf8ee2c2011-07-29 22:43:06 +00001415 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001416 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001417
Nate Begemandbe3f772009-03-19 05:21:56 +00001418 // If the operator name is present, parse it.
Matthias Braun7cf3b112016-12-05 06:00:41 +00001419 StringInit *OperatorName = nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001420 if (Lex.getCode() == tgtok::colon) {
1421 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1422 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001423 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001424 }
Matthias Braun7cf3b112016-12-05 06:00:41 +00001425 OperatorName = StringInit::get(Lex.getCurStrVal());
Nate Begemandbe3f772009-03-19 05:21:56 +00001426 Lex.Lex(); // eat the VarName.
1427 }
Bob Wilson7248f862009-11-22 04:24:42 +00001428
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001429 SmallVector<std::pair<llvm::Init*, StringInit*>, 8> DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001430 if (Lex.getCode() != tgtok::r_paren) {
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001431 ParseDagArgList(DagArgs, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001432 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001433 }
Bob Wilson7248f862009-11-22 04:24:42 +00001434
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001435 if (Lex.getCode() != tgtok::r_paren) {
1436 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001437 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001438 }
1439 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001440
David Greenee32ebf22011-07-29 19:07:07 +00001441 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001442 }
Bob Wilson7248f862009-11-22 04:24:42 +00001443
David Greene2f7cf7f2011-01-07 17:05:37 +00001444 case tgtok::XHead:
1445 case tgtok::XTail:
1446 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001447 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001448 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001449 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001450 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +00001451 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +00001452 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001453 case tgtok::XSRL:
1454 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001455 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001456 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001457 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001458 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001459 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001460 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001461 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001462 }
1463 }
Bob Wilson7248f862009-11-22 04:24:42 +00001464
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001465 return R;
1466}
1467
1468/// ParseValue - Parse a tblgen value. This returns null on error.
1469///
1470/// Value ::= SimpleValue ValueSuffix*
1471/// ValueSuffix ::= '{' BitList '}'
1472/// ValueSuffix ::= '[' BitList ']'
1473/// ValueSuffix ::= '.' ID
1474///
David Greened4263a62011-10-19 13:04:20 +00001475Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1476 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001477 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001478
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001479 // Parse the suffixes now if present.
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001480 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001481 switch (Lex.getCode()) {
1482 default: return Result;
1483 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001484 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001485 // This is the beginning of the object body.
1486 return Result;
1487
Chris Lattner526c8cb2009-06-21 03:39:35 +00001488 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001489 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001490 SmallVector<unsigned, 16> Ranges;
1491 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001492 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001493
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001494 // Reverse the bitlist.
1495 std::reverse(Ranges.begin(), Ranges.end());
1496 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001497 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001498 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001499 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001500 }
Bob Wilson7248f862009-11-22 04:24:42 +00001501
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001502 // Eat the '}'.
1503 if (Lex.getCode() != tgtok::r_brace) {
1504 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001505 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001506 }
1507 Lex.Lex();
1508 break;
1509 }
1510 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001511 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001512 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001513 SmallVector<unsigned, 16> Ranges;
1514 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001515 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001516
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001517 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001518 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001519 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001520 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001521 }
Bob Wilson7248f862009-11-22 04:24:42 +00001522
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001523 // Eat the ']'.
1524 if (Lex.getCode() != tgtok::r_square) {
1525 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001526 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001527 }
1528 Lex.Lex();
1529 break;
1530 }
Matthias Braun6a441832016-12-05 06:00:36 +00001531 case tgtok::period: {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001532 if (Lex.Lex() != tgtok::Id) { // eat the .
1533 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001534 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001535 }
Matthias Braun6a441832016-12-05 06:00:36 +00001536 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
1537 if (!Result->getFieldType(FieldName)) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001538 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001539 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001540 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001541 }
Matthias Braun6a441832016-12-05 06:00:36 +00001542 Result = FieldInit::get(Result, FieldName);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001543 Lex.Lex(); // eat field name
1544 break;
Matthias Braun6a441832016-12-05 06:00:36 +00001545 }
David Greene8e85b482011-10-19 13:04:43 +00001546
1547 case tgtok::paste:
1548 SMLoc PasteLoc = Lex.getLoc();
1549
1550 // Create a !strconcat() operation, first casting each operand to
1551 // a string if necessary.
1552
Sean Silvafb509ed2012-10-10 20:24:43 +00001553 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001554 if (!LHS) {
1555 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001556 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001557 }
Craig Toppera9642b42015-05-04 01:35:39 +00001558
David Greene8e85b482011-10-19 13:04:43 +00001559 if (LHS->getType() != StringRecTy::get()) {
1560 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1561 }
1562
Craig Topper011817a2014-04-09 04:50:04 +00001563 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001564
1565 Lex.Lex(); // Eat the '#'.
1566 switch (Lex.getCode()) {
1567 case tgtok::colon:
1568 case tgtok::semi:
1569 case tgtok::l_brace:
1570 // These are all of the tokens that can begin an object body.
1571 // Some of these can also begin values but we disallow those cases
1572 // because they are unlikely to be useful.
Craig Toppera9642b42015-05-04 01:35:39 +00001573
David Greene8e85b482011-10-19 13:04:43 +00001574 // Trailing paste, concat with an empty string.
1575 RHS = StringInit::get("");
1576 break;
1577
1578 default:
1579 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001580 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001581 if (!RHS) {
1582 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001583 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001584 }
1585
1586 if (RHS->getType() != StringRecTy::get()) {
1587 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1588 }
Craig Toppera9642b42015-05-04 01:35:39 +00001589
David Greene8e85b482011-10-19 13:04:43 +00001590 break;
1591 }
1592
1593 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1594 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1595 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001596 }
1597 }
1598}
1599
1600/// ParseDagArgList - Parse the argument list for a dag literal expression.
1601///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001602/// DagArg ::= Value (':' VARNAME)?
1603/// DagArg ::= VARNAME
1604/// DagArgList ::= DagArg
1605/// DagArgList ::= DagArgList ',' DagArg
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001606void TGParser::ParseDagArgList(
1607 SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
1608 Record *CurRec) {
Bob Wilson7248f862009-11-22 04:24:42 +00001609
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001610 while (true) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001611 // DagArg ::= VARNAME
1612 if (Lex.getCode() == tgtok::VarName) {
1613 // A missing value is treated like '?'.
Matthias Braunbb053162016-12-05 06:00:46 +00001614 StringInit *VarName = StringInit::get(Lex.getCurStrVal());
1615 Result.emplace_back(UnsetInit::get(), VarName);
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001616 Lex.Lex();
1617 } else {
1618 // DagArg ::= Value (':' VARNAME)?
1619 Init *Val = ParseValue(CurRec);
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001620 if (!Val) {
1621 Result.clear();
1622 return;
1623 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001624
1625 // If the variable name is present, add it.
Matthias Braunbb053162016-12-05 06:00:46 +00001626 StringInit *VarName = nullptr;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001627 if (Lex.getCode() == tgtok::colon) {
1628 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1629 TokError("expected variable name in dag literal");
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001630 Result.clear();
1631 return;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001632 }
Matthias Braunbb053162016-12-05 06:00:46 +00001633 VarName = StringInit::get(Lex.getCurStrVal());
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001634 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001635 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001636
1637 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001638 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001639 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001640 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001641 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001642}
1643
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001644/// ParseValueList - Parse a comma separated list of values, returning them as a
1645/// vector. Note that this always expects to be able to parse at least one
1646/// value. It returns an empty list if this is not possible.
1647///
1648/// ValueList ::= Value (',' Value)
1649///
Matthias Braunc66e7552016-12-05 06:41:54 +00001650void TGParser::ParseValueList(SmallVectorImpl<Init*> &Result, Record *CurRec,
1651 Record *ArgsRec, RecTy *EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001652 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001653 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001654 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001655 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001656 if (TArgs.empty()) {
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001657 TokError("template argument provided to non-template class");
Matthias Braunc66e7552016-12-05 06:41:54 +00001658 Result.clear();
1659 return;
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001660 }
David Greene8618f952009-06-08 20:23:18 +00001661 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001662 if (!RV) {
1663 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1664 << ")\n";
1665 }
David Greene8618f952009-06-08 20:23:18 +00001666 assert(RV && "Template argument record not found??");
1667 ItemType = RV->getType();
1668 ++ArgN;
1669 }
1670 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00001671 if (!Result.back()) {
1672 Result.clear();
1673 return;
1674 }
Bob Wilson7248f862009-11-22 04:24:42 +00001675
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001676 while (Lex.getCode() == tgtok::comma) {
1677 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001678
Craig Topper011817a2014-04-09 04:50:04 +00001679 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001680 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001681 if (ArgN >= TArgs.size()) {
1682 TokError("too many template arguments");
Matthias Braunc66e7552016-12-05 06:41:54 +00001683 Result.clear();
1684 return;
Bob Wilson7248f862009-11-22 04:24:42 +00001685 }
David Greene8618f952009-06-08 20:23:18 +00001686 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1687 assert(RV && "Template argument record not found??");
1688 ItemType = RV->getType();
1689 ++ArgN;
1690 }
1691 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00001692 if (!Result.back()) {
1693 Result.clear();
1694 return;
1695 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001696 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001697}
1698
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001699/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1700/// empty string on error. This can happen in a number of different context's,
1701/// including within a def or in the template args for a def (which which case
1702/// CurRec will be non-null) and within the template args for a multiclass (in
1703/// which case CurRec will be null, but CurMultiClass will be set). This can
1704/// also happen within a def that is within a multiclass, which will set both
1705/// CurRec and CurMultiClass.
1706///
1707/// Declaration ::= FIELD? Type ID ('=' Value)?
1708///
David Greenedb10e692011-10-19 13:02:42 +00001709Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001710 bool ParsingTemplateArgs) {
1711 // Read the field prefix if present.
1712 bool HasField = Lex.getCode() == tgtok::Field;
1713 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001714
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001715 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001716 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001717
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001718 if (Lex.getCode() != tgtok::Id) {
1719 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001720 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001721 }
Bob Wilson7248f862009-11-22 04:24:42 +00001722
Chris Lattner526c8cb2009-06-21 03:39:35 +00001723 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001724 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001725 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001726
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001727 if (ParsingTemplateArgs) {
Craig Toppera9642b42015-05-04 01:35:39 +00001728 if (CurRec)
David Greenedb10e692011-10-19 13:02:42 +00001729 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Craig Toppera9642b42015-05-04 01:35:39 +00001730 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001731 assert(CurMultiClass);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001732 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001733 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1734 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001735 }
Bob Wilson7248f862009-11-22 04:24:42 +00001736
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001737 // Add the value.
1738 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001739 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001740
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001741 // If a value is present, parse it.
1742 if (Lex.getCode() == tgtok::equal) {
1743 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001744 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001745 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001746 if (!Val ||
Craig Toppercfd81732016-01-04 03:15:08 +00001747 SetValue(CurRec, ValLoc, DeclName, None, Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001748 // Return the name, even if an error is thrown. This is so that we can
1749 // continue to make some progress, even without the value having been
1750 // initialized.
1751 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001752 }
Bob Wilson7248f862009-11-22 04:24:42 +00001753
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001754 return DeclName;
1755}
1756
David Greenefb927af2012-02-22 16:09:41 +00001757/// ParseForeachDeclaration - Read a foreach declaration, returning
1758/// the name of the declared object or a NULL Init on error. Return
1759/// the name of the parsed initializer list through ForeachListName.
1760///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001761/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1762/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1763/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001764///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001765VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001766 if (Lex.getCode() != tgtok::Id) {
1767 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001768 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001769 }
1770
1771 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1772 Lex.Lex();
1773
1774 // If a value is present, parse it.
1775 if (Lex.getCode() != tgtok::equal) {
1776 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001777 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001778 }
1779 Lex.Lex(); // Eat the '='
1780
Craig Topper011817a2014-04-09 04:50:04 +00001781 RecTy *IterType = nullptr;
Matthias Braunc66e7552016-12-05 06:41:54 +00001782 SmallVector<unsigned, 16> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001783
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001784 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001785 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001786 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001787 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001788 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001789 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001790 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001791 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001792 }
1793 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001794 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001795 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001796 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001797 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001798 }
1799 IterType = ListType->getElementType();
1800 break;
David Greenefb927af2012-02-22 16:09:41 +00001801 }
1802
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001803 case tgtok::IntVal: { // RangePiece.
1804 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001805 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001806 break;
David Greenefb927af2012-02-22 16:09:41 +00001807 }
1808
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001809 case tgtok::l_brace: { // '{' RangeList '}'
1810 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001811 ParseRangeList(Ranges);
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001812 if (Lex.getCode() != tgtok::r_brace) {
1813 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001814 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001815 }
1816 Lex.Lex();
1817 break;
1818 }
1819 }
David Greenefb927af2012-02-22 16:09:41 +00001820
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001821 if (!Ranges.empty()) {
1822 assert(!IterType && "Type already initialized?");
1823 IterType = IntRecTy::get();
1824 std::vector<Init*> Values;
Craig Topper8eb764c2015-06-02 06:19:28 +00001825 for (unsigned R : Ranges)
1826 Values.push_back(IntInit::get(R));
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001827 ForeachListValue = ListInit::get(Values, IterType);
1828 }
1829
1830 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001831 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001832
1833 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001834}
1835
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001836/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1837/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1838/// template args for a def, which may or may not be in a multiclass. If null,
1839/// these are the template args for a multiclass.
1840///
1841/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001842///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001843bool TGParser::ParseTemplateArgList(Record *CurRec) {
1844 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1845 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001846
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001847 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001848
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001849 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001850 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001851 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001852 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001853
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001854 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001855
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001856 while (Lex.getCode() == tgtok::comma) {
1857 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001858
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001859 // Read the following declarations.
1860 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001861 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001862 return true;
1863 TheRecToAddTo->addTemplateArg(TemplArg);
1864 }
Bob Wilson7248f862009-11-22 04:24:42 +00001865
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001866 if (Lex.getCode() != tgtok::greater)
1867 return TokError("expected '>' at end of template argument list");
1868 Lex.Lex(); // eat the '>'.
1869 return false;
1870}
1871
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001872/// ParseBodyItem - Parse a single item at within the body of a def or class.
1873///
1874/// BodyItem ::= Declaration ';'
1875/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1876bool TGParser::ParseBodyItem(Record *CurRec) {
1877 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001878 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001879 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001880
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001881 if (Lex.getCode() != tgtok::semi)
1882 return TokError("expected ';' after declaration");
1883 Lex.Lex();
1884 return false;
1885 }
1886
1887 // LET ID OptionalRangeList '=' Value ';'
1888 if (Lex.Lex() != tgtok::Id)
1889 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001890
Chris Lattner526c8cb2009-06-21 03:39:35 +00001891 SMLoc IdLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00001892 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001893 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001894
Matthias Braunc66e7552016-12-05 06:41:54 +00001895 SmallVector<unsigned, 16> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001896 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001897 return true;
1898 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001899
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001900 if (Lex.getCode() != tgtok::equal)
1901 return TokError("expected '=' in let expression");
1902 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001903
David Greene8618f952009-06-08 20:23:18 +00001904 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001905 if (!Field)
Matthias Braun215ff842016-12-05 07:35:13 +00001906 return TokError("Value '" + FieldName->getValue() + "' unknown!");
David Greene8618f952009-06-08 20:23:18 +00001907
1908 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001909
David Greeneaf8ee2c2011-07-29 22:43:06 +00001910 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001911 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001912
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001913 if (Lex.getCode() != tgtok::semi)
1914 return TokError("expected ';' after let expression");
1915 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001916
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001917 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1918}
1919
1920/// ParseBody - Read the body of a class or def. Return true on error, false on
1921/// success.
1922///
1923/// Body ::= ';'
1924/// Body ::= '{' BodyList '}'
1925/// BodyList BodyItem*
1926///
1927bool TGParser::ParseBody(Record *CurRec) {
1928 // If this is a null definition, just eat the semi and return.
1929 if (Lex.getCode() == tgtok::semi) {
1930 Lex.Lex();
1931 return false;
1932 }
Bob Wilson7248f862009-11-22 04:24:42 +00001933
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001934 if (Lex.getCode() != tgtok::l_brace)
1935 return TokError("Expected ';' or '{' to start body");
1936 // Eat the '{'.
1937 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001938
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001939 while (Lex.getCode() != tgtok::r_brace)
1940 if (ParseBodyItem(CurRec))
1941 return true;
1942
1943 // Eat the '}'.
1944 Lex.Lex();
1945 return false;
1946}
1947
Sean Silvacb1a75e2013-01-09 04:49:14 +00001948/// \brief Apply the current let bindings to \a CurRec.
1949/// \returns true on error, false otherwise.
1950bool TGParser::ApplyLetStack(Record *CurRec) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001951 for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
Craig Topper8eb764c2015-06-02 06:19:28 +00001952 for (LetRecord &LR : LetInfo)
1953 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
Sean Silvacb1a75e2013-01-09 04:49:14 +00001954 return true;
1955 return false;
1956}
1957
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001958/// ParseObjectBody - Parse the body of a def or class. This consists of an
1959/// optional ClassList followed by a Body. CurRec is the current def or class
1960/// that is being parsed.
1961///
1962/// ObjectBody ::= BaseClassList Body
1963/// BaseClassList ::= /*empty*/
1964/// BaseClassList ::= ':' BaseClassListNE
1965/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1966///
1967bool TGParser::ParseObjectBody(Record *CurRec) {
1968 // If there is a baseclass list, read it.
1969 if (Lex.getCode() == tgtok::colon) {
1970 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001971
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001972 // Read all of the subclasses.
1973 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001974 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001975 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00001976 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001977
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001978 // Add it.
1979 if (AddSubClass(CurRec, SubClass))
1980 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001981
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001982 if (Lex.getCode() != tgtok::comma) break;
1983 Lex.Lex(); // eat ','.
1984 SubClass = ParseSubClassReference(CurRec, false);
1985 }
1986 }
1987
Sean Silvacb1a75e2013-01-09 04:49:14 +00001988 if (ApplyLetStack(CurRec))
1989 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001990
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001991 return ParseBody(CurRec);
1992}
1993
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001994/// ParseDef - Parse and return a top level or multiclass def, return the record
1995/// corresponding to it. This returns null on error.
1996///
1997/// DefInst ::= DEF ObjectName ObjectBody
1998///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00001999bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00002000 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002001 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00002002 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002003
2004 // Parse ObjectName and make a record for it.
Craig Topper84138712014-11-29 05:31:10 +00002005 std::unique_ptr<Record> CurRecOwner;
Jordan Roseabdd99b2013-01-10 18:50:05 +00002006 Init *Name = ParseObjectName(CurMultiClass);
2007 if (Name)
Craig Topper84138712014-11-29 05:31:10 +00002008 CurRecOwner = make_unique<Record>(Name, DefLoc, Records);
Jordan Roseabdd99b2013-01-10 18:50:05 +00002009 else
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00002010 CurRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), DefLoc,
2011 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00002012 Record *CurRec = CurRecOwner.get(); // Keep a copy since we may release.
Bob Wilson7248f862009-11-22 04:24:42 +00002013
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002014 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002015 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00002016
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002017 // Ensure redefinition doesn't happen.
Craig Topper84138712014-11-29 05:31:10 +00002018 if (Records.getDef(CurRec->getNameInitAsString()))
2019 return Error(DefLoc, "def '" + CurRec->getNameInitAsString()+
2020 "' already defined");
Craig Toppercdab2322014-11-29 05:52:51 +00002021 Records.addDef(std::move(CurRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00002022
2023 if (ParseObjectBody(CurRec))
2024 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002025 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002026 // Parse the body before adding this prototype to the DefPrototypes vector.
2027 // That way implicit definitions will be added to the DefPrototypes vector
2028 // before this object, instantiated prior to defs derived from this object,
2029 // and this available for indirect name resolution when defs derived from
2030 // this object are instantiated.
Craig Topper84138712014-11-29 05:31:10 +00002031 if (ParseObjectBody(CurRec))
Hal Finkela8c1f462014-01-02 20:47:09 +00002032 return true;
2033
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002034 // Otherwise, a def inside a multiclass, add it to the multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002035 for (const auto &Proto : CurMultiClass->DefPrototypes)
2036 if (Proto->getNameInit() == CurRec->getNameInit())
Craig Topper84138712014-11-29 05:31:10 +00002037 return Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
2038 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002039 CurMultiClass->DefPrototypes.push_back(std::move(CurRecOwner));
Anton Yartsev671dff12014-08-08 00:29:54 +00002040 } else if (ParseObjectBody(CurRec)) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002041 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002042 }
Bob Wilson7248f862009-11-22 04:24:42 +00002043
Craig Topper011817a2014-04-09 04:50:04 +00002044 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002045 // See Record::setName(). This resolve step will see any new name
2046 // for the def that might have been created when resolving
2047 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002048 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002049
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002050 // If ObjectBody has template arguments, it's an error.
2051 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002052
2053 if (CurMultiClass) {
2054 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002055 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
2056 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002057 assert(RV && "Template arg doesn't exist?");
2058 CurRec->addValue(*RV);
2059 }
2060 }
2061
Craig Toppera9642b42015-05-04 01:35:39 +00002062 if (ProcessForeachDefs(CurRec, DefLoc))
Craig Topper84138712014-11-29 05:31:10 +00002063 return Error(DefLoc, "Could not process loops for def" +
2064 CurRec->getNameInitAsString());
David Greenefb927af2012-02-22 16:09:41 +00002065
2066 return false;
2067}
2068
2069/// ParseForeach - Parse a for statement. Return the record corresponding
2070/// to it. This returns true on error.
2071///
2072/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2073/// Foreach ::= FOREACH Declaration IN Object
2074///
2075bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2076 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2077 Lex.Lex(); // Eat the 'for' token.
2078
2079 // Make a temporary object to record items associated with the for
2080 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002081 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002082 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002083 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002084 return TokError("expected declaration in for");
2085
2086 if (Lex.getCode() != tgtok::In)
2087 return TokError("Unknown tok");
2088 Lex.Lex(); // Eat the in
2089
2090 // Create a loop object and remember it.
2091 Loops.push_back(ForeachLoop(IterName, ListValue));
2092
2093 if (Lex.getCode() != tgtok::l_brace) {
2094 // FOREACH Declaration IN Object
2095 if (ParseObject(CurMultiClass))
2096 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002097 } else {
David Greenefb927af2012-02-22 16:09:41 +00002098 SMLoc BraceLoc = Lex.getLoc();
2099 // Otherwise, this is a group foreach.
2100 Lex.Lex(); // eat the '{'.
2101
2102 // Parse the object list.
2103 if (ParseObjectList(CurMultiClass))
2104 return true;
2105
2106 if (Lex.getCode() != tgtok::r_brace) {
2107 TokError("expected '}' at end of foreach command");
2108 return Error(BraceLoc, "to match this '{'");
2109 }
2110 Lex.Lex(); // Eat the }
2111 }
2112
2113 // We've processed everything in this loop.
2114 Loops.pop_back();
2115
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002116 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002117}
2118
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002119/// ParseClass - Parse a tblgen class definition.
2120///
2121/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2122///
2123bool TGParser::ParseClass() {
2124 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2125 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002126
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002127 if (Lex.getCode() != tgtok::Id)
2128 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002129
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002130 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2131 if (CurRec) {
2132 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002133 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002134 !CurRec->getSuperClasses().empty() ||
2135 !CurRec->getTemplateArgs().empty())
Craig Topper85c07002015-04-30 05:54:22 +00002136 return TokError("Class '" + CurRec->getNameInitAsString() +
2137 "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002138 } else {
2139 // If this is the first reference to this class, create and add it.
Hans Wennborgffbbd532014-11-30 00:31:49 +00002140 auto NewRec =
2141 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
Craig Toppercdab2322014-11-29 05:52:51 +00002142 CurRec = NewRec.get();
2143 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002144 }
2145 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002146
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002147 // If there are template args, parse them.
2148 if (Lex.getCode() == tgtok::less)
2149 if (ParseTemplateArgList(CurRec))
2150 return true;
2151
2152 // Finally, parse the object body.
2153 return ParseObjectBody(CurRec);
2154}
2155
2156/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2157/// of LetRecords.
2158///
2159/// LetList ::= LetItem (',' LetItem)*
2160/// LetItem ::= ID OptionalRangeList '=' Value
2161///
Matthias Braunc66e7552016-12-05 06:41:54 +00002162void TGParser::ParseLetList(SmallVectorImpl<LetRecord> &Result) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002163 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002164 if (Lex.getCode() != tgtok::Id) {
2165 TokError("expected identifier in let definition");
Matthias Braunc66e7552016-12-05 06:41:54 +00002166 Result.clear();
2167 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002168 }
Matthias Braunc66e7552016-12-05 06:41:54 +00002169
Matthias Braun215ff842016-12-05 07:35:13 +00002170 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattner526c8cb2009-06-21 03:39:35 +00002171 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002172 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002173
2174 // Check for an optional RangeList.
Matthias Braunc66e7552016-12-05 06:41:54 +00002175 SmallVector<unsigned, 16> Bits;
2176 if (ParseOptionalRangeList(Bits)) {
2177 Result.clear();
2178 return;
2179 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002180 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002181
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002182 if (Lex.getCode() != tgtok::equal) {
2183 TokError("expected '=' in let expression");
Matthias Braunc66e7552016-12-05 06:41:54 +00002184 Result.clear();
2185 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002186 }
2187 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002188
Craig Topper011817a2014-04-09 04:50:04 +00002189 Init *Val = ParseValue(nullptr);
Matthias Braunc66e7552016-12-05 06:41:54 +00002190 if (!Val) {
2191 Result.clear();
2192 return;
2193 }
Bob Wilson7248f862009-11-22 04:24:42 +00002194
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002195 // Now that we have everything, add the record.
Matthias Braun215ff842016-12-05 07:35:13 +00002196 Result.emplace_back(Name, Bits, Val, NameLoc);
Bob Wilson7248f862009-11-22 04:24:42 +00002197
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002198 if (Lex.getCode() != tgtok::comma)
Matthias Braunc66e7552016-12-05 06:41:54 +00002199 return;
Bob Wilson7248f862009-11-22 04:24:42 +00002200 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002201 }
2202}
2203
2204/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002205/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002206///
2207/// Object ::= LET LetList IN '{' ObjectList '}'
2208/// Object ::= LET LetList IN Object
2209///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002210bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002211 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2212 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002213
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002214 // Add this entry to the let stack.
Matthias Braunc66e7552016-12-05 06:41:54 +00002215 SmallVector<LetRecord, 8> LetInfo;
2216 ParseLetList(LetInfo);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002217 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002218 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002219
2220 if (Lex.getCode() != tgtok::In)
2221 return TokError("expected 'in' at end of top-level 'let'");
2222 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002223
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002224 // If this is a scalar let, just handle it now
2225 if (Lex.getCode() != tgtok::l_brace) {
2226 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002227 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002228 return true;
2229 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002230 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002231 // Otherwise, this is a group let.
2232 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002233
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002234 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002235 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002236 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002237
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002238 if (Lex.getCode() != tgtok::r_brace) {
2239 TokError("expected '}' at end of top level let command");
2240 return Error(BraceLoc, "to match this '{'");
2241 }
2242 Lex.Lex();
2243 }
Bob Wilson7248f862009-11-22 04:24:42 +00002244
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002245 // Outside this let scope, this let block is not active.
2246 LetStack.pop_back();
2247 return false;
2248}
2249
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002250/// ParseMultiClass - Parse a multiclass definition.
2251///
Bob Wilson3d948162009-04-28 19:41:44 +00002252/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002253/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2254/// MultiClassObject ::= DefInst
2255/// MultiClassObject ::= MultiClassInst
2256/// MultiClassObject ::= DefMInst
2257/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2258/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002259///
2260bool TGParser::ParseMultiClass() {
2261 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2262 Lex.Lex(); // Eat the multiclass token.
2263
2264 if (Lex.getCode() != tgtok::Id)
2265 return TokError("expected identifier after multiclass for name");
2266 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002267
Craig Topper7adf2bf2014-12-11 05:25:30 +00002268 auto Result =
2269 MultiClasses.insert(std::make_pair(Name,
2270 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2271
2272 if (!Result.second)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002273 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002274
Craig Topper7adf2bf2014-12-11 05:25:30 +00002275 CurMultiClass = Result.first->second.get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002276 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002277
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002278 // If there are template args, parse them.
2279 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002280 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002281 return true;
2282
David Greene7049e792009-04-24 16:55:41 +00002283 bool inherits = false;
2284
David Greene753ed8f2009-04-22 16:42:54 +00002285 // If there are submulticlasses, parse them.
2286 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002287 inherits = true;
2288
David Greene753ed8f2009-04-22 16:42:54 +00002289 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002290
David Greene753ed8f2009-04-22 16:42:54 +00002291 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002292 SubMultiClassReference SubMultiClass =
2293 ParseSubMultiClassReference(CurMultiClass);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002294 while (true) {
David Greene753ed8f2009-04-22 16:42:54 +00002295 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002296 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002297
David Greene753ed8f2009-04-22 16:42:54 +00002298 // Add it.
2299 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2300 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002301
David Greene753ed8f2009-04-22 16:42:54 +00002302 if (Lex.getCode() != tgtok::comma) break;
2303 Lex.Lex(); // eat ','.
2304 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2305 }
2306 }
2307
David Greene7049e792009-04-24 16:55:41 +00002308 if (Lex.getCode() != tgtok::l_brace) {
2309 if (!inherits)
2310 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002311 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002312 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002313 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002314 } else {
David Greene7049e792009-04-24 16:55:41 +00002315 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2316 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002317
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002318 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002319 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002320 default:
2321 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
2322 case tgtok::Let:
2323 case tgtok::Def:
2324 case tgtok::Defm:
2325 case tgtok::Foreach:
2326 if (ParseObject(CurMultiClass))
2327 return true;
2328 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002329 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002330 }
David Greene7049e792009-04-24 16:55:41 +00002331 Lex.Lex(); // eat the '}'.
2332 }
Bob Wilson7248f862009-11-22 04:24:42 +00002333
Craig Topper011817a2014-04-09 04:50:04 +00002334 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002335 return false;
2336}
2337
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002338Record *TGParser::InstantiateMulticlassDef(MultiClass &MC, Record *DefProto,
2339 Init *&DefmPrefix,
2340 SMRange DefmPrefixRange,
2341 ArrayRef<Init *> TArgs,
Matthias Braunc66e7552016-12-05 06:41:54 +00002342 ArrayRef<Init *> TemplateVals) {
David Greene5d5d88c2011-10-19 13:04:31 +00002343 // We need to preserve DefProto so it can be reused for later
2344 // instantiations, so create a new Record to inherit from it.
2345
David Greenedb445972011-10-05 22:42:07 +00002346 // Add in the defm name. If the defm prefix is empty, give each
2347 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2348 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2349 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002350
Jordan Roseabdd99b2013-01-10 18:50:05 +00002351 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002352 if (!DefmPrefix) {
Craig Topperf4412262017-06-01 06:56:16 +00002353 DefmPrefix = GetNewAnonymousName();
Jordan Roseabdd99b2013-01-10 18:50:05 +00002354 IsAnonymous = true;
2355 }
David Greene5d5d88c2011-10-19 13:04:31 +00002356
2357 Init *DefName = DefProto->getNameInit();
Sean Silvafb509ed2012-10-10 20:24:43 +00002358 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002359
Craig Topper011817a2014-04-09 04:50:04 +00002360 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002361 // We have a fully expanded string so there are no operators to
2362 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002363 DefName =
2364 BinOpInit::get(BinOpInit::STRCONCAT,
2365 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2366 StringRecTy::get())->Fold(DefProto, &MC),
2367 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2368 }
David Greene5d5d88c2011-10-19 13:04:31 +00002369
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002370 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002371 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002372 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Craig Topper84138712014-11-29 05:31:10 +00002373 auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002374
2375 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002376 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002377 Ref.Rec = DefProto;
Craig Topper84138712014-11-29 05:31:10 +00002378 AddSubClass(CurRec.get(), Ref);
David Greenedb445972011-10-05 22:42:07 +00002379
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002380 // Set the value for NAME. We don't resolve references to it 'til later,
2381 // though, so that uses in nested multiclass names don't get
2382 // confused.
Matthias Braun215ff842016-12-05 07:35:13 +00002383 if (SetValue(CurRec.get(), Ref.RefRange.Start, StringInit::get("NAME"), None,
2384 DefmPrefix, /*AllowSelfAssignment*/true)) {
Craig Topper85c07002015-04-30 05:54:22 +00002385 Error(DefmPrefixRange.Start, "Could not resolve " +
2386 CurRec->getNameInitAsString() + ":NAME to '" +
2387 DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002388 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002389 }
David Greene8bf0d722011-10-19 13:04:35 +00002390
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002391 // If the DefNameString didn't resolve, we probably have a reference to
2392 // NAME and need to replace it. We need to do at least this much greedily,
2393 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002394 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002395 RecordVal *DefNameRV = CurRec->getValue("NAME");
2396 CurRec->resolveReferencesTo(DefNameRV);
2397 }
2398
2399 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002400 // Now that we're at the top level, resolve all NAME references
2401 // in the resultant defs that weren't in the def names themselves.
2402 RecordVal *DefNameRV = CurRec->getValue("NAME");
2403 CurRec->resolveReferencesTo(DefNameRV);
2404
Hal Finkeld2497362015-05-21 04:32:56 +00002405 // Check if the name is a complex pattern.
2406 // If so, resolve it.
2407 DefName = CurRec->getNameInit();
2408 DefNameString = dyn_cast<StringInit>(DefName);
2409
2410 // OK the pattern is more complex than simply using NAME.
2411 // Let's use the heavy weaponery.
2412 if (!DefNameString) {
2413 ResolveMulticlassDefArgs(MC, CurRec.get(), DefmPrefixRange.Start,
2414 Lex.getLoc(), TArgs, TemplateVals,
2415 false/*Delete args*/);
2416 DefName = CurRec->getNameInit();
2417 DefNameString = dyn_cast<StringInit>(DefName);
2418
2419 if (!DefNameString)
2420 DefName = DefName->convertInitializerTo(StringRecTy::get());
2421
2422 // We ran out of options here...
2423 DefNameString = dyn_cast<StringInit>(DefName);
2424 if (!DefNameString) {
2425 PrintFatalError(CurRec->getLoc()[CurRec->getLoc().size() - 1],
2426 DefName->getAsUnquotedString() + " is not a string.");
2427 return nullptr;
2428 }
2429
2430 CurRec->setName(DefName);
2431 }
2432
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002433 // Now that NAME references are resolved and we're at the top level of
2434 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002435 // currently in a multiclass, it means this defm appears inside a
2436 // multiclass and its name won't be fully resolvable until we see
Hal Finkeld2497362015-05-21 04:32:56 +00002437 // the top-level defm. Therefore, we don't add this to the
2438 // RecordKeeper at this point. If we did we could get duplicate
David Greene8bf0d722011-10-19 13:04:35 +00002439 // defs as more than one probably refers to NAME or some other
2440 // common internal placeholder.
2441
2442 // Ensure redefinition doesn't happen.
2443 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002444 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002445 "' already defined, instantiating defm with subdef '" +
2446 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002447 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002448 }
2449
Craig Topper84138712014-11-29 05:31:10 +00002450 Record *CurRecSave = CurRec.get(); // Keep a copy before we release.
Craig Toppercdab2322014-11-29 05:52:51 +00002451 Records.addDef(std::move(CurRec));
Craig Topper84138712014-11-29 05:31:10 +00002452 return CurRecSave;
David Greene8bf0d722011-10-19 13:04:35 +00002453 }
2454
Craig Topper84138712014-11-29 05:31:10 +00002455 // FIXME This is bad but the ownership transfer to caller is pretty messy.
2456 // The unique_ptr in this function at least protects the exits above.
2457 return CurRec.release();
David Greenedb445972011-10-05 22:42:07 +00002458}
2459
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002460bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC, Record *CurRec,
2461 SMLoc DefmPrefixLoc, SMLoc SubClassLoc,
2462 ArrayRef<Init *> TArgs,
Matthias Braunc66e7552016-12-05 06:41:54 +00002463 ArrayRef<Init *> TemplateVals,
David Greenedb445972011-10-05 22:42:07 +00002464 bool DeleteArgs) {
2465 // Loop over all of the template arguments, setting them to the specified
2466 // value or leaving them as the default if necessary.
2467 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2468 // Check if a value is specified for this temp-arg.
2469 if (i < TemplateVals.size()) {
2470 // Set it now.
Craig Toppercfd81732016-01-04 03:15:08 +00002471 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], None, TemplateVals[i]))
David Greenedb445972011-10-05 22:42:07 +00002472 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002473
David Greenedb445972011-10-05 22:42:07 +00002474 // Resolve it next.
2475 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2476
2477 if (DeleteArgs)
2478 // Now remove it.
2479 CurRec->removeValue(TArgs[i]);
Craig Toppera9642b42015-05-04 01:35:39 +00002480
David Greenedb445972011-10-05 22:42:07 +00002481 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Craig Topper85c07002015-04-30 05:54:22 +00002482 return Error(SubClassLoc, "value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00002483 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +00002484 ") of multiclassclass '" + MC.Rec.getNameInitAsString() +
2485 "'");
David Greenedb445972011-10-05 22:42:07 +00002486 }
2487 }
2488 return false;
2489}
2490
2491bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2492 Record *CurRec,
2493 Record *DefProto,
2494 SMLoc DefmPrefixLoc) {
2495 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002496 if (ApplyLetStack(CurRec))
2497 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002498
David Greenedb445972011-10-05 22:42:07 +00002499 // Don't create a top level definition for defm inside multiclasses,
2500 // instead, only update the prototypes and bind the template args
2501 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002502 if (!CurMultiClass)
2503 return false;
Craig Toppereb4d7c62015-04-29 04:43:36 +00002504 for (const auto &Proto : CurMultiClass->DefPrototypes)
2505 if (Proto->getNameInit() == CurRec->getNameInit())
Sean Silvacc951b22013-01-09 05:28:12 +00002506 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2507 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002508 CurMultiClass->DefPrototypes.push_back(std::unique_ptr<Record>(CurRec));
David Greenedb445972011-10-05 22:42:07 +00002509
Sean Silvacc951b22013-01-09 05:28:12 +00002510 // Copy the template arguments for the multiclass into the new def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002511 for (Init * TA : CurMultiClass->Rec.getTemplateArgs()) {
2512 const RecordVal *RV = CurMultiClass->Rec.getValue(TA);
Sean Silvacc951b22013-01-09 05:28:12 +00002513 assert(RV && "Template arg doesn't exist?");
2514 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002515 }
2516
2517 return false;
2518}
2519
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002520/// ParseDefm - Parse the instantiation of a multiclass.
2521///
2522/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2523///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002524bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002525 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002526 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002527 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002528
Craig Topperb21afc62013-01-07 05:09:33 +00002529 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002530 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002531 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002532
Jordan Rosef12e8a92013-01-10 18:50:11 +00002533 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002534 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002535 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002536
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002537 // Keep track of the new generated record definitions.
2538 std::vector<Record*> NewRecDefs;
2539
2540 // This record also inherits from a regular class (non-multiclass)?
2541 bool InheritFromClass = false;
2542
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002543 // eat the colon.
2544 Lex.Lex();
2545
Chris Lattner526c8cb2009-06-21 03:39:35 +00002546 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002547 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002548
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002549 while (true) {
Craig Topper011817a2014-04-09 04:50:04 +00002550 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002551
2552 // To instantiate a multiclass, we need to first get the multiclass, then
2553 // instantiate each def contained in the multiclass with the SubClassRef
2554 // template parameters.
Craig Topper7adf2bf2014-12-11 05:25:30 +00002555 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper4ca40012014-11-30 01:20:17 +00002556 assert(MC && "Didn't lookup multiclass correctly?");
Matthias Braunc66e7552016-12-05 06:41:54 +00002557 ArrayRef<Init*> TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002558
2559 // Verify that the correct number of template arguments were specified.
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002560 ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002561 if (TArgs.size() < TemplateVals.size())
2562 return Error(SubClassLoc,
2563 "more template args specified than multiclass expects");
2564
2565 // Loop over all the def's in the multiclass, instantiating each one.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002566 for (const std::unique_ptr<Record> &DefProto : MC->DefPrototypes) {
Hal Finkeld2497362015-05-21 04:32:56 +00002567 // The record name construction goes as follow:
2568 // - If the def name is a string, prepend the prefix.
2569 // - If the def name is a more complex pattern, use that pattern.
Craig Topper7f36be92016-02-26 06:50:27 +00002570 // As a result, the record is instantiated before resolving
Hal Finkeld2497362015-05-21 04:32:56 +00002571 // arguments, as it would make its name a string.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002572 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto.get(), DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002573 SMRange(DefmLoc,
Hal Finkeld2497362015-05-21 04:32:56 +00002574 DefmPrefixEndLoc),
2575 TArgs, TemplateVals);
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002576 if (!CurRec)
2577 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002578
Craig Topper7f36be92016-02-26 06:50:27 +00002579 // Now that the record is instantiated, we can resolve arguments.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002580 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002581 TArgs, TemplateVals, true/*Delete args*/))
2582 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002583
Craig Toppereb4d7c62015-04-29 04:43:36 +00002584 if (ResolveMulticlassDef(*MC, CurRec, DefProto.get(), DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002585 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002586
Adam Nemete5a07162014-09-16 17:14:13 +00002587 // Defs that can be used by other definitions should be fully resolved
2588 // before any use.
2589 if (DefProto->isResolveFirst() && !CurMultiClass) {
2590 CurRec->resolveReferences();
2591 CurRec->setResolveFirst(false);
2592 }
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002593 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002594 }
2595
David Greenedb445972011-10-05 22:42:07 +00002596
David Greenef00919a2009-04-22 22:17:51 +00002597 if (Lex.getCode() != tgtok::comma) break;
2598 Lex.Lex(); // eat ','.
2599
Craig Topper998a39a2013-08-20 04:22:09 +00002600 if (Lex.getCode() != tgtok::Id)
2601 return TokError("expected identifier");
2602
David Greenef00919a2009-04-22 22:17:51 +00002603 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002604
2605 // A defm can inherit from regular classes (non-multiclass) as
2606 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002607 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002608
2609 if (InheritFromClass)
2610 break;
2611
Craig Topper011817a2014-04-09 04:50:04 +00002612 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002613 }
2614
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002615 if (InheritFromClass) {
2616 // Process all the classes to inherit as if they were part of a
2617 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002618 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002619 while (true) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002620 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002621 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002622
2623 // Get the expanded definition prototypes and teach them about
2624 // the record values the current class to inherit has
Craig Toppereb4d7c62015-04-29 04:43:36 +00002625 for (Record *CurRec : NewRecDefs) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002626 // Add it.
2627 if (AddSubClass(CurRec, SubClass))
2628 return true;
2629
Sean Silvacb1a75e2013-01-09 04:49:14 +00002630 if (ApplyLetStack(CurRec))
2631 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002632 }
2633
2634 if (Lex.getCode() != tgtok::comma) break;
2635 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002636 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002637 }
2638 }
2639
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002640 if (!CurMultiClass)
Craig Toppereb4d7c62015-04-29 04:43:36 +00002641 for (Record *CurRec : NewRecDefs)
David Greene50c09122011-08-10 18:27:46 +00002642 // See Record::setName(). This resolve step will see any new
2643 // name for the def that might have been created when resolving
2644 // inheritance, values and arguments above.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002645 CurRec->resolveReferences();
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002646
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002647 if (Lex.getCode() != tgtok::semi)
2648 return TokError("expected ';' at end of defm");
2649 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002650
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002651 return false;
2652}
2653
2654/// ParseObject
2655/// Object ::= ClassInst
2656/// Object ::= DefInst
2657/// Object ::= MultiClassInst
2658/// Object ::= DefMInst
2659/// Object ::= LETCommand '{' ObjectList '}'
2660/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002661bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002662 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002663 default:
2664 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002665 case tgtok::Let: return ParseTopLevelLet(MC);
2666 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002667 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002668 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002669 case tgtok::Class: return ParseClass();
2670 case tgtok::MultiClass: return ParseMultiClass();
2671 }
2672}
2673
2674/// ParseObjectList
2675/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002676bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002677 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002678 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002679 return true;
2680 }
2681 return false;
2682}
2683
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002684bool TGParser::ParseFile() {
2685 Lex.Lex(); // Prime the lexer.
2686 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002687
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002688 // If we have unread input at the end of the file, report it.
2689 if (Lex.getCode() == tgtok::Eof)
2690 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002691
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002692 return TokError("Unexpected input at top level");
2693}