blob: 4239628498b10f258b8a6abe8d9dce1c14f507fa [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"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/SmallVector.h"
Chris Lattnerf4127dd2007-11-22 20:49:04 +000017#include "llvm/ADT/StringExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/TableGen/Record.h"
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000019#include <algorithm>
Chris Lattnerf4127dd2007-11-22 20:49:04 +000020using namespace llvm;
21
22//===----------------------------------------------------------------------===//
23// Support Code for the Semantic Actions.
24//===----------------------------------------------------------------------===//
25
26namespace llvm {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000027struct SubClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000028 SMRange RefRange;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000029 Record *Rec;
David Greeneaf8ee2c2011-07-29 22:43:06 +000030 std::vector<Init*> TemplateArgs;
Craig Topper011817a2014-04-09 04:50:04 +000031 SubClassReference() : Rec(nullptr) {}
David Greene7049e792009-04-24 16:55:41 +000032
Craig Topper011817a2014-04-09 04:50:04 +000033 bool isInvalid() const { return Rec == nullptr; }
Chris Lattnerf4127dd2007-11-22 20:49:04 +000034};
David Greene753ed8f2009-04-22 16:42:54 +000035
36struct SubMultiClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000037 SMRange RefRange;
David Greene753ed8f2009-04-22 16:42:54 +000038 MultiClass *MC;
David Greeneaf8ee2c2011-07-29 22:43:06 +000039 std::vector<Init*> TemplateArgs;
Craig Topper011817a2014-04-09 04:50:04 +000040 SubMultiClassReference() : MC(nullptr) {}
Bob Wilson3d948162009-04-28 19:41:44 +000041
Craig Topper011817a2014-04-09 04:50:04 +000042 bool isInvalid() const { return MC == nullptr; }
David Greene7049e792009-04-24 16:55:41 +000043 void dump() const;
David Greene753ed8f2009-04-22 16:42:54 +000044};
David Greene7049e792009-04-24 16:55:41 +000045
Yaron Kereneb2a2542016-01-29 20:50:44 +000046LLVM_DUMP_METHOD void SubMultiClassReference::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000047 errs() << "Multiclass:\n";
Bob Wilson7248f862009-11-22 04:24:42 +000048
David Greene7049e792009-04-24 16:55:41 +000049 MC->dump();
Bob Wilson7248f862009-11-22 04:24:42 +000050
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000051 errs() << "Template args:\n";
Craig Toppera9642b42015-05-04 01:35:39 +000052 for (Init *TA : TemplateArgs)
Craig Toppereb4d7c62015-04-29 04:43:36 +000053 TA->dump();
David Greene7049e792009-04-24 16:55:41 +000054}
55
Chris Lattnerf4127dd2007-11-22 20:49:04 +000056} // end namespace llvm
57
Chris Lattner526c8cb2009-06-21 03:39:35 +000058bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Craig Topper011817a2014-04-09 04:50:04 +000059 if (!CurRec)
Chris Lattnerf4127dd2007-11-22 20:49:04 +000060 CurRec = &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +000061
Jakob Stoklund Olesen9d1c5ee2012-01-13 03:16:35 +000062 if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000063 // The value already exists in the class, treat this as a set.
64 if (ERV->setValue(RV.getValue()))
65 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
66 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson7248f862009-11-22 04:24:42 +000067 "previous definition of type '" +
Chris Lattnerf4127dd2007-11-22 20:49:04 +000068 ERV->getType()->getAsString() + "'");
69 } else {
70 CurRec->addValue(RV);
71 }
72 return false;
73}
74
75/// SetValue -
76/// Return true on error, false on success.
David Greene3ca42122011-10-19 13:02:39 +000077bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
Craig Toppercfd81732016-01-04 03:15:08 +000078 ArrayRef<unsigned> BitList, Init *V,
Craig Topper1e23ed92016-01-04 03:05:14 +000079 bool AllowSelfAssignment) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +000080 if (!V) return false;
81
Craig Topper011817a2014-04-09 04:50:04 +000082 if (!CurRec) CurRec = &CurMultiClass->Rec;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000083
84 RecordVal *RV = CurRec->getValue(ValName);
Craig Topper011817a2014-04-09 04:50:04 +000085 if (!RV)
Craig Topper85c07002015-04-30 05:54:22 +000086 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
87 "' unknown!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +000088
89 // Do not allow assignments like 'X = X'. This will just cause infinite loops
90 // in the resolution machinery.
91 if (BitList.empty())
Sean Silvafb509ed2012-10-10 20:24:43 +000092 if (VarInit *VI = dyn_cast<VarInit>(V))
Craig Topper1e23ed92016-01-04 03:05:14 +000093 if (VI->getNameInit() == ValName && !AllowSelfAssignment)
94 return true;
Bob Wilson7248f862009-11-22 04:24:42 +000095
Chris Lattnerf4127dd2007-11-22 20:49:04 +000096 // If we are assigning to a subset of the bits in the value... then we must be
97 // assigning to a field of BitsRecTy, which must have a BitsInit
98 // initializer.
99 //
100 if (!BitList.empty()) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000101 BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue());
Craig Topper011817a2014-04-09 04:50:04 +0000102 if (!CurVal)
Craig Topper85c07002015-04-30 05:54:22 +0000103 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
104 "' is not a bits type");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000105
106 // Convert the incoming value to a bits type of the appropriate size...
David Greeneaf8ee2c2011-07-29 22:43:06 +0000107 Init *BI = V->convertInitializerTo(BitsRecTy::get(BitList.size()));
Craig Toppera9642b42015-05-04 01:35:39 +0000108 if (!BI)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000109 return Error(Loc, "Initializer is not compatible with bit range");
Bob Wilson7248f862009-11-22 04:24:42 +0000110
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000111 // We should have a BitsInit type now.
Craig Toppered5a9502015-04-29 07:13:05 +0000112 BitsInit *BInit = cast<BitsInit>(BI);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000113
David Greeneaf8ee2c2011-07-29 22:43:06 +0000114 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000115
116 // Loop over bits, assigning values as appropriate.
117 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
118 unsigned Bit = BitList[i];
David Greeneb3da8122011-07-29 19:07:00 +0000119 if (NewBits[Bit])
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000120 return Error(Loc, "Cannot set bit #" + Twine(Bit) + " of value '" +
David Greene3ca42122011-10-19 13:02:39 +0000121 ValName->getAsUnquotedString() + "' more than once");
David Greeneb3da8122011-07-29 19:07:00 +0000122 NewBits[Bit] = BInit->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000123 }
124
125 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
Craig Topper011817a2014-04-09 04:50:04 +0000126 if (!NewBits[i])
David Greeneb3da8122011-07-29 19:07:00 +0000127 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000128
David Greenee32ebf22011-07-29 19:07:07 +0000129 V = BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000130 }
131
Pete Cooper040c6a62014-07-31 01:43:57 +0000132 if (RV->setValue(V)) {
133 std::string InitType = "";
Craig Toppera9642b42015-05-04 01:35:39 +0000134 if (BitsInit *BI = dyn_cast<BitsInit>(V))
Pete Cooper040c6a62014-07-31 01:43:57 +0000135 InitType = (Twine("' of type bit initializer with length ") +
136 Twine(BI->getNumBits())).str();
Craig Topper85c07002015-04-30 05:54:22 +0000137 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
138 "' of type '" + RV->getType()->getAsString() +
139 "' is incompatible with initializer '" + V->getAsString() +
140 InitType + "'");
Pete Cooper040c6a62014-07-31 01:43:57 +0000141 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000142 return false;
143}
144
145/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
146/// args as SubClass's template arguments.
Cedric Venetd1e179d2009-02-14 16:06:42 +0000147bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000148 Record *SC = SubClass.Rec;
149 // Add all of the values in the subclass into the current class.
Craig Topper8eb764c2015-06-02 06:19:28 +0000150 for (const RecordVal &Val : SC->getValues())
151 if (AddValue(CurRec, SubClass.RefRange.Start, Val))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000152 return true;
153
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000154 ArrayRef<Init *> TArgs = SC->getTemplateArgs();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000155
156 // Ensure that an appropriate number of template arguments are specified.
157 if (TArgs.size() < SubClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000158 return Error(SubClass.RefRange.Start,
159 "More template args specified than expected");
Bob Wilson7248f862009-11-22 04:24:42 +0000160
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000161 // Loop over all of the template arguments, setting them to the specified
162 // value or leaving them as the default if necessary.
163 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
164 if (i < SubClass.TemplateArgs.size()) {
165 // If a value is specified for this template arg, set it now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000166 if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000167 None, SubClass.TemplateArgs[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000168 return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000169
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000170 // Resolve it next.
171 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson7248f862009-11-22 04:24:42 +0000172
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000173 // Now remove it.
174 CurRec->removeValue(TArgs[i]);
175
176 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000177 return Error(SubClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000178 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000179 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000180 ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000181 }
182 }
183
184 // Since everything went well, we can now set the "superclass" list for the
185 // current record.
Craig Topper0e41d0b2016-01-18 19:52:37 +0000186 ArrayRef<std::pair<Record *, SMRange>> SCs = SC->getSuperClasses();
187 for (const auto &SCPair : SCs) {
188 if (CurRec->isSubClassOf(SCPair.first))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000189 return Error(SubClass.RefRange.Start,
Craig Topper0e41d0b2016-01-18 19:52:37 +0000190 "Already subclass of '" + SCPair.first->getName() + "'!\n");
191 CurRec->addSuperClass(SCPair.first, SCPair.second);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000192 }
Bob Wilson7248f862009-11-22 04:24:42 +0000193
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000194 if (CurRec->isSubClassOf(SC))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000195 return Error(SubClass.RefRange.Start,
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000196 "Already subclass of '" + SC->getName() + "'!\n");
Jordan Rosef12e8a92013-01-10 18:50:11 +0000197 CurRec->addSuperClass(SC, SubClass.RefRange);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000198 return false;
199}
200
David Greene753ed8f2009-04-22 16:42:54 +0000201/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilsonf71e6562009-04-30 18:26:19 +0000202/// CurMC, resolving its template args as SubMultiClass's
David Greene753ed8f2009-04-22 16:42:54 +0000203/// template arguments.
Bob Wilsonf71e6562009-04-30 18:26:19 +0000204bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson92ab8202009-04-30 17:46:20 +0000205 SubMultiClassReference &SubMultiClass) {
David Greene753ed8f2009-04-22 16:42:54 +0000206 MultiClass *SMC = SubMultiClass.MC;
Bob Wilsonf71e6562009-04-30 18:26:19 +0000207 Record *CurRec = &CurMC->Rec;
David Greene753ed8f2009-04-22 16:42:54 +0000208
David Greene753ed8f2009-04-22 16:42:54 +0000209 // Add all of the values in the subclass into the current class.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000210 for (const auto &SMCVal : SMC->Rec.getValues())
211 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000212 return true;
213
Craig Toppera4ea4b02014-11-30 00:24:32 +0000214 unsigned newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000215
David Greene753ed8f2009-04-22 16:42:54 +0000216 // Add all of the defs in the subclass into the current multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000217 for (const std::unique_ptr<Record> &R : SMC->DefPrototypes) {
David Greene753ed8f2009-04-22 16:42:54 +0000218 // Clone the def and add it to the current multiclass
Craig Toppereb4d7c62015-04-29 04:43:36 +0000219 auto NewDef = make_unique<Record>(*R);
David Greene753ed8f2009-04-22 16:42:54 +0000220
221 // Add all of the values in the superclass into the current def.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000222 for (const auto &MCVal : CurRec->getValues())
223 if (AddValue(NewDef.get(), SubMultiClass.RefRange.Start, MCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000224 return true;
225
Craig Topperc3504c42014-12-11 05:25:33 +0000226 CurMC->DefPrototypes.push_back(std::move(NewDef));
David Greene753ed8f2009-04-22 16:42:54 +0000227 }
Bob Wilson3d948162009-04-28 19:41:44 +0000228
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000229 ArrayRef<Init *> SMCTArgs = SMC->Rec.getTemplateArgs();
David Greene753ed8f2009-04-22 16:42:54 +0000230
David Greene7049e792009-04-24 16:55:41 +0000231 // Ensure that an appropriate number of template arguments are
232 // specified.
David Greene753ed8f2009-04-22 16:42:54 +0000233 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000234 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000235 "More template args specified than expected");
Bob Wilson3d948162009-04-28 19:41:44 +0000236
David Greene753ed8f2009-04-22 16:42:54 +0000237 // Loop over all of the template arguments, setting them to the specified
238 // value or leaving them as the default if necessary.
239 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
240 if (i < SubMultiClass.TemplateArgs.size()) {
David Greene7049e792009-04-24 16:55:41 +0000241 // If a value is specified for this template arg, set it in the
242 // superclass now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000243 if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000244 None, SubMultiClass.TemplateArgs[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000245 return true;
246
247 // Resolve it next.
248 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson3d948162009-04-28 19:41:44 +0000249
David Greene753ed8f2009-04-22 16:42:54 +0000250 // Now remove it.
251 CurRec->removeValue(SMCTArgs[i]);
252
David Greene7049e792009-04-24 16:55:41 +0000253 // If a value is specified for this template arg, set it in the
254 // new defs now.
Craig Topperc3504c42014-12-11 05:25:33 +0000255 for (const auto &Def :
256 makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
257 if (SetValue(Def.get(), SubMultiClass.RefRange.Start, SMCTArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000258 None, SubMultiClass.TemplateArgs[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000259 return true;
260
261 // Resolve it next.
262 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
263
264 // Now remove it
265 Def->removeValue(SMCTArgs[i]);
266 }
267 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000268 return Error(SubMultiClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000269 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000270 Twine(i) + " (" + SMCTArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000271 ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000272 }
273 }
274
275 return false;
276}
277
David Greenefb927af2012-02-22 16:09:41 +0000278/// ProcessForeachDefs - Given a record, apply all of the variable
279/// values in all surrounding foreach loops, creating new records for
280/// each combination of values.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000281bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
282 if (Loops.empty())
283 return false;
284
David Greenefb927af2012-02-22 16:09:41 +0000285 // We want to instantiate a new copy of CurRec for each combination
286 // of nested loop iterator values. We don't want top instantiate
287 // any copies until we have values for each loop iterator.
288 IterSet IterVals;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000289 return ProcessForeachDefs(CurRec, Loc, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000290}
291
292/// ProcessForeachDefs - Given a record, a loop and a loop iterator,
293/// apply each of the variable values in this loop and then process
294/// subloops.
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000295bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
296 // Recursively build a tuple of iterator values.
297 if (IterVals.size() != Loops.size()) {
298 assert(IterVals.size() < Loops.size());
299 ForeachLoop &CurLoop = Loops[IterVals.size()];
Sean Silvafb509ed2012-10-10 20:24:43 +0000300 ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
Craig Topper011817a2014-04-09 04:50:04 +0000301 if (!List) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000302 Error(Loc, "Loop list is not a list");
303 return true;
304 }
David Greenefb927af2012-02-22 16:09:41 +0000305
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000306 // Process each value.
Craig Topper664f6a02015-06-02 04:15:57 +0000307 for (unsigned i = 0; i < List->size(); ++i) {
Craig Topper011817a2014-04-09 04:50:04 +0000308 Init *ItemVal = List->resolveListElementReference(*CurRec, nullptr, i);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000309 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
310 if (ProcessForeachDefs(CurRec, Loc, IterVals))
311 return true;
312 IterVals.pop_back();
313 }
314 return false;
315 }
316
317 // This is the bottom of the recursion. We have all of the iterator values
318 // for this point in the iteration space. Instantiate a new record to
319 // reflect this combination of values.
Craig Topper84138712014-11-29 05:31:10 +0000320 auto IterRec = make_unique<Record>(*CurRec);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000321
322 // Set the iterator values now.
Craig Topper8eb764c2015-06-02 06:19:28 +0000323 for (IterRecord &IR : IterVals) {
324 VarInit *IterVar = IR.IterVar;
325 TypedInit *IVal = dyn_cast<TypedInit>(IR.IterValue);
Craig Topper84138712014-11-29 05:31:10 +0000326 if (!IVal)
327 return Error(Loc, "foreach iterator value is untyped");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000328
329 IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false));
330
Craig Toppercfd81732016-01-04 03:15:08 +0000331 if (SetValue(IterRec.get(), Loc, IterVar->getName(), None, IVal))
Craig Topper84138712014-11-29 05:31:10 +0000332 return Error(Loc, "when instantiating this def");
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000333
334 // Resolve it next.
335 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName()));
336
337 // Remove it.
338 IterRec->removeValue(IterVar->getName());
339 }
340
341 if (Records.getDef(IterRec->getNameInitAsString())) {
Artyom Skrobov8b985322014-06-10 12:41:14 +0000342 // If this record is anonymous, it's no problem, just generate a new name
Craig Topper84138712014-11-29 05:31:10 +0000343 if (!IterRec->isAnonymous())
344 return Error(Loc, "def already exists: " +IterRec->getNameInitAsString());
345
346 IterRec->setName(GetNewAnonymousName());
David Greenefb927af2012-02-22 16:09:41 +0000347 }
348
Craig Topper84138712014-11-29 05:31:10 +0000349 Record *IterRecSave = IterRec.get(); // Keep a copy before release.
Craig Toppercdab2322014-11-29 05:52:51 +0000350 Records.addDef(std::move(IterRec));
Craig Topper84138712014-11-29 05:31:10 +0000351 IterRecSave->resolveReferences();
David Greenefb927af2012-02-22 16:09:41 +0000352 return false;
353}
354
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000355//===----------------------------------------------------------------------===//
356// Parser Code
357//===----------------------------------------------------------------------===//
358
359/// isObjectStart - Return true if this is a valid first token for an Object.
360static bool isObjectStart(tgtok::TokKind K) {
361 return K == tgtok::Class || K == tgtok::Def ||
David Greenefb927af2012-02-22 16:09:41 +0000362 K == tgtok::Defm || K == tgtok::Let ||
363 K == tgtok::MultiClass || K == tgtok::Foreach;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000364}
365
Alp Tokerce91fe52013-12-21 18:51:00 +0000366/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
367/// an identifier.
368std::string TGParser::GetNewAnonymousName() {
Aaron Ballman97a59fb2015-02-16 19:33:36 +0000369 return "anonymous_" + utostr(AnonCounter++);
Chris Lattner7538ed82010-10-05 22:51:56 +0000370}
371
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000372/// ParseObjectName - If an object name is specified, return it. Otherwise,
Jordan Roseabdd99b2013-01-10 18:50:05 +0000373/// return 0.
David Greene2affd672011-10-19 13:04:29 +0000374/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000375/// ObjectName ::= /*empty*/
376///
David Greene2affd672011-10-19 13:04:29 +0000377Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
378 switch (Lex.getCode()) {
379 case tgtok::colon:
380 case tgtok::semi:
381 case tgtok::l_brace:
382 // These are all of the tokens that can begin an object body.
383 // Some of these can also begin values but we disallow those cases
384 // because they are unlikely to be useful.
Craig Topper011817a2014-04-09 04:50:04 +0000385 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000386 default:
387 break;
388 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000389
Craig Topper011817a2014-04-09 04:50:04 +0000390 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000391 if (CurMultiClass)
392 CurRec = &CurMultiClass->Rec;
393
Craig Topper011817a2014-04-09 04:50:04 +0000394 RecTy *Type = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000395 if (CurRec) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000396 const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
David Greene2affd672011-10-19 13:04:29 +0000397 if (!CurRecName) {
398 TokError("Record name is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +0000399 return nullptr;
David Greene2affd672011-10-19 13:04:29 +0000400 }
401 Type = CurRecName->getType();
402 }
403
404 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000405}
406
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000407/// ParseClassID - Parse and resolve a reference to a class name. This returns
408/// null on error.
409///
410/// ClassID ::= ID
411///
412Record *TGParser::ParseClassID() {
413 if (Lex.getCode() != tgtok::Id) {
414 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000415 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000416 }
Bob Wilson7248f862009-11-22 04:24:42 +0000417
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000418 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000419 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000420 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000421
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000422 Lex.Lex();
423 return Result;
424}
425
Bob Wilson3d948162009-04-28 19:41:44 +0000426/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
427/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000428///
429/// MultiClassID ::= ID
430///
431MultiClass *TGParser::ParseMultiClassID() {
432 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000433 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000434 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000435 }
Bob Wilson3d948162009-04-28 19:41:44 +0000436
Craig Topper7adf2bf2014-12-11 05:25:30 +0000437 MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
Craig Topper4ca40012014-11-30 01:20:17 +0000438 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000439 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000440
David Greene753ed8f2009-04-22 16:42:54 +0000441 Lex.Lex();
Craig Topper4ca40012014-11-30 01:20:17 +0000442 return Result;
David Greene753ed8f2009-04-22 16:42:54 +0000443}
444
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000445/// ParseSubClassReference - Parse a reference to a subclass or to a templated
446/// subclass. This returns a SubClassRefTy with a null Record* on error.
447///
448/// SubClassRef ::= ClassID
449/// SubClassRef ::= ClassID '<' ValueList '>'
450///
451SubClassReference TGParser::
452ParseSubClassReference(Record *CurRec, bool isDefm) {
453 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000454 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000455
Sean Silva0657b402013-01-09 02:17:14 +0000456 if (isDefm) {
457 if (MultiClass *MC = ParseMultiClassID())
458 Result.Rec = &MC->Rec;
459 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000460 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000461 }
Craig Topper011817a2014-04-09 04:50:04 +0000462 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000463
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000464 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000465 if (Lex.getCode() != tgtok::less) {
466 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000467 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000468 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000469 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000470
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000471 if (Lex.getCode() == tgtok::greater) {
472 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000473 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000474 return Result;
475 }
Bob Wilson7248f862009-11-22 04:24:42 +0000476
David Greene8618f952009-06-08 20:23:18 +0000477 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000478 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000479 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000480 return Result;
481 }
Bob Wilson7248f862009-11-22 04:24:42 +0000482
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000483 if (Lex.getCode() != tgtok::greater) {
484 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000485 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000486 return Result;
487 }
488 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000489 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000490
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000491 return Result;
492}
493
Bob Wilson3d948162009-04-28 19:41:44 +0000494/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
495/// templated submulticlass. This returns a SubMultiClassRefTy with a null
496/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000497///
498/// SubMultiClassRef ::= MultiClassID
499/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
500///
501SubMultiClassReference TGParser::
502ParseSubMultiClassReference(MultiClass *CurMC) {
503 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000504 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000505
David Greene753ed8f2009-04-22 16:42:54 +0000506 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000507 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000508
David Greene753ed8f2009-04-22 16:42:54 +0000509 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000510 if (Lex.getCode() != tgtok::less) {
511 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000512 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000513 }
David Greene753ed8f2009-04-22 16:42:54 +0000514 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000515
David Greene753ed8f2009-04-22 16:42:54 +0000516 if (Lex.getCode() == tgtok::greater) {
517 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000518 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000519 return Result;
520 }
Bob Wilson3d948162009-04-28 19:41:44 +0000521
David Greene8618f952009-06-08 20:23:18 +0000522 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000523 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000524 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000525 return Result;
526 }
Bob Wilson3d948162009-04-28 19:41:44 +0000527
David Greene753ed8f2009-04-22 16:42:54 +0000528 if (Lex.getCode() != tgtok::greater) {
529 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000530 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000531 return Result;
532 }
533 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000534 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000535
536 return Result;
537}
538
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000539/// ParseRangePiece - Parse a bit/value range.
540/// RangePiece ::= INTVAL
541/// RangePiece ::= INTVAL '-' INTVAL
542/// RangePiece ::= INTVAL INTVAL
543bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000544 if (Lex.getCode() != tgtok::IntVal) {
545 TokError("expected integer or bitrange");
546 return true;
547 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000548 int64_t Start = Lex.getCurIntVal();
549 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000550
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000551 if (Start < 0)
552 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000553
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000554 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000555 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000556 Ranges.push_back(Start);
557 return false;
558 case tgtok::minus:
559 if (Lex.Lex() != tgtok::IntVal) {
560 TokError("expected integer value as end of range");
561 return true;
562 }
563 End = Lex.getCurIntVal();
564 break;
565 case tgtok::IntVal:
566 End = -Lex.getCurIntVal();
567 break;
568 }
Bob Wilson7248f862009-11-22 04:24:42 +0000569 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000570 return TokError("invalid range, cannot be negative");
571 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000572
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000573 // Add to the range.
Craig Toppera9642b42015-05-04 01:35:39 +0000574 if (Start < End)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000575 for (; Start <= End; ++Start)
576 Ranges.push_back(Start);
Craig Toppera9642b42015-05-04 01:35:39 +0000577 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000578 for (; Start >= End; --Start)
579 Ranges.push_back(Start);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000580 return false;
581}
582
583/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
584///
585/// RangeList ::= RangePiece (',' RangePiece)*
586///
587std::vector<unsigned> TGParser::ParseRangeList() {
588 std::vector<unsigned> Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000589
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000590 // Parse the first piece.
591 if (ParseRangePiece(Result))
592 return std::vector<unsigned>();
593 while (Lex.getCode() == tgtok::comma) {
594 Lex.Lex(); // Eat the comma.
595
596 // Parse the next range piece.
597 if (ParseRangePiece(Result))
598 return std::vector<unsigned>();
599 }
600 return Result;
601}
602
603/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
604/// OptionalRangeList ::= '<' RangeList '>'
605/// OptionalRangeList ::= /*empty*/
606bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
607 if (Lex.getCode() != tgtok::less)
608 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000609
Chris Lattner526c8cb2009-06-21 03:39:35 +0000610 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000611 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000612
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000613 // Parse the range list.
614 Ranges = ParseRangeList();
615 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000616
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000617 if (Lex.getCode() != tgtok::greater) {
618 TokError("expected '>' at end of range list");
619 return Error(StartLoc, "to match this '<'");
620 }
621 Lex.Lex(); // eat the '>'.
622 return false;
623}
624
625/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
626/// OptionalBitList ::= '{' RangeList '}'
627/// OptionalBitList ::= /*empty*/
628bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
629 if (Lex.getCode() != tgtok::l_brace)
630 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000631
Chris Lattner526c8cb2009-06-21 03:39:35 +0000632 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000633 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000634
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000635 // Parse the range list.
636 Ranges = ParseRangeList();
637 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000638
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000639 if (Lex.getCode() != tgtok::r_brace) {
640 TokError("expected '}' at end of bit list");
641 return Error(StartLoc, "to match this '{'");
642 }
643 Lex.Lex(); // eat the '}'.
644 return false;
645}
646
647
648/// ParseType - Parse and return a tblgen type. This returns null on error.
649///
650/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000651/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000652/// Type ::= BIT // bit type
653/// Type ::= BITS '<' INTVAL '>' // bits<x> type
654/// Type ::= INT // int type
655/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000656/// Type ::= DAG // dag type
657/// Type ::= ClassID // Record Type
658///
659RecTy *TGParser::ParseType() {
660 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000661 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000662 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000663 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000664 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
665 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000666 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000667 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000668 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Craig Topper011817a2014-04-09 04:50:04 +0000669 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000670 case tgtok::Bits: {
671 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
672 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000673 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000674 }
675 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
676 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000677 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000678 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000679 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000680 if (Lex.Lex() != tgtok::greater) { // Eat count.
681 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000682 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000683 }
684 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000685 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000686 }
687 case tgtok::List: {
688 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
689 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000690 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000691 }
692 Lex.Lex(); // Eat '<'
693 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000694 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000695
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000696 if (Lex.getCode() != tgtok::greater) {
697 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000698 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000699 }
700 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000701 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000702 }
Bob Wilson7248f862009-11-22 04:24:42 +0000703 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000704}
705
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000706/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
707/// has already been read.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000708Init *TGParser::ParseIDValue(Record *CurRec,
David Greened4263a62011-10-19 13:04:20 +0000709 const std::string &Name, SMLoc NameLoc,
710 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000711 if (CurRec) {
712 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000713 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000714
David Greenedb10e692011-10-19 13:02:42 +0000715 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
716
David Greene47a665e2011-10-05 22:42:54 +0000717 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +0000718 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
719 "::");
David Greene47a665e2011-10-05 22:42:54 +0000720
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000721 if (CurRec->isTemplateArg(TemplateArgName)) {
722 const RecordVal *RV = CurRec->getValue(TemplateArgName);
723 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000724 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000725 }
726 }
Bob Wilson7248f862009-11-22 04:24:42 +0000727
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000728 if (CurMultiClass) {
David Greenedb10e692011-10-19 13:02:42 +0000729 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
730 "::");
731
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000732 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
733 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
734 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000735 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000736 }
737 }
Bob Wilson7248f862009-11-22 04:24:42 +0000738
David Greenefb927af2012-02-22 16:09:41 +0000739 // If this is in a foreach loop, make sure it's not a loop iterator
Craig Toppereb4d7c62015-04-29 04:43:36 +0000740 for (const auto &L : Loops) {
741 VarInit *IterVar = dyn_cast<VarInit>(L.IterVar);
David Greenefb927af2012-02-22 16:09:41 +0000742 if (IterVar && IterVar->getName() == Name)
743 return IterVar;
744 }
745
David Greene232bd602011-10-19 13:04:21 +0000746 if (Mode == ParseNameMode)
747 return StringInit::get(Name);
748
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000749 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000750 return DefInit::get(D);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000751
David Greene232bd602011-10-19 13:04:21 +0000752 if (Mode == ParseValueMode) {
753 Error(NameLoc, "Variable not defined: '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000754 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000755 }
Craig Toppera9642b42015-05-04 01:35:39 +0000756
David Greene232bd602011-10-19 13:04:21 +0000757 return StringInit::get(Name);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000758}
759
David Greene5d0c0512009-05-14 20:54:48 +0000760/// ParseOperation - Parse an operator. This returns null on error.
761///
762/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
763///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000764Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000765 switch (Lex.getCode()) {
766 default:
767 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000768 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000769 case tgtok::XHead:
770 case tgtok::XTail:
771 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000772 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
773 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000774 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000775
David Greenee8f3b272009-05-14 21:22:49 +0000776 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000777 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000778 case tgtok::XCast:
779 Lex.Lex(); // eat the operation
780 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000781
David Greenee8f3b272009-05-14 21:22:49 +0000782 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000783
Craig Topper011817a2014-04-09 04:50:04 +0000784 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000785 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000786 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000787 }
David Greene5d0c0512009-05-14 20:54:48 +0000788
David Greenee8f3b272009-05-14 21:22:49 +0000789 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000790 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000791 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000792 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000793 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000794 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000795 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000796 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000797 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000798 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000799 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000800 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000801 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000802 break;
David Greenee8f3b272009-05-14 21:22:49 +0000803 }
804 if (Lex.getCode() != tgtok::l_paren) {
805 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000806 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000807 }
808 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000809
David Greeneaf8ee2c2011-07-29 22:43:06 +0000810 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000811 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000812
Craig Topper85c07002015-04-30 05:54:22 +0000813 if (Code == UnOpInit::HEAD ||
814 Code == UnOpInit::TAIL ||
815 Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000816 ListInit *LHSl = dyn_cast<ListInit>(LHS);
817 StringInit *LHSs = dyn_cast<StringInit>(LHS);
818 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000819 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000820 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000821 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000822 }
823 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000824 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
825 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000826 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000827 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000828 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000829 }
830 }
831
Craig Topper85c07002015-04-30 05:54:22 +0000832 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
Craig Topper011817a2014-04-09 04:50:04 +0000833 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000834 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000835 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000836 }
Bob Wilson7248f862009-11-22 04:24:42 +0000837
Craig Topperec9072d2015-05-14 05:53:53 +0000838 if (LHSl && LHSl->empty()) {
David Greened571b3c2009-05-14 22:38:31 +0000839 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000840 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000841 }
842 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000843 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000844 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000845 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000846 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000847 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000848 }
Craig Toppera9642b42015-05-04 01:35:39 +0000849 Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
850 : ListRecTy::get(Itemt->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000851 } else {
David Greened571b3c2009-05-14 22:38:31 +0000852 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000853 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000854 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000855 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000856 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000857 }
Craig Toppera9642b42015-05-04 01:35:39 +0000858 Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
David Greened571b3c2009-05-14 22:38:31 +0000859 }
860 }
861 }
862
David Greenee8f3b272009-05-14 21:22:49 +0000863 if (Lex.getCode() != tgtok::r_paren) {
864 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000865 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000866 }
867 Lex.Lex(); // eat the ')'
David Greenee32ebf22011-07-29 19:07:07 +0000868 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee8f3b272009-05-14 21:22:49 +0000869 }
David Greene5d0c0512009-05-14 20:54:48 +0000870
871 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000872 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000873 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +0000874 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000875 case tgtok::XSRL:
876 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +0000877 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +0000878 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +0000879 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000880 tgtok::TokKind OpTok = Lex.getCode();
881 SMLoc OpLoc = Lex.getLoc();
882 Lex.Lex(); // eat the operation
883
David Greene5d0c0512009-05-14 20:54:48 +0000884 BinOpInit::BinaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000885 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000886
Chris Lattner61ea00b2010-10-05 23:58:18 +0000887 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +0000888 default: llvm_unreachable("Unhandled code!");
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000889 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000890 case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break;
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000891 case tgtok::XAND: Code = BinOpInit::AND; Type = IntRecTy::get(); break;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000892 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
893 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
894 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
895 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Daniel Sanders314e80e2014-05-07 10:13:19 +0000896 case tgtok::XListConcat:
897 Code = BinOpInit::LISTCONCAT;
898 // We don't know the list type until we parse the first argument
899 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000900 case tgtok::XStrConcat:
David Greene5d0c0512009-05-14 20:54:48 +0000901 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000902 Type = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +0000903 break;
David Greene5d0c0512009-05-14 20:54:48 +0000904 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000905
David Greene5d0c0512009-05-14 20:54:48 +0000906 if (Lex.getCode() != tgtok::l_paren) {
907 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000908 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000909 }
910 Lex.Lex(); // eat the '('
911
David Greeneaf8ee2c2011-07-29 22:43:06 +0000912 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000913
Chris Lattner61ea00b2010-10-05 23:58:18 +0000914 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000915 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000916
Chris Lattner61ea00b2010-10-05 23:58:18 +0000917 while (Lex.getCode() == tgtok::comma) {
918 Lex.Lex(); // eat the ','
919
920 InitList.push_back(ParseValue(CurRec));
Craig Topper011817a2014-04-09 04:50:04 +0000921 if (!InitList.back()) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000922 }
David Greene5d0c0512009-05-14 20:54:48 +0000923
924 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000925 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +0000926 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000927 }
928 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +0000929
Daniel Sanders314e80e2014-05-07 10:13:19 +0000930 // If we are doing !listconcat, we should know the type by now
931 if (OpTok == tgtok::XListConcat) {
932 if (VarInit *Arg0 = dyn_cast<VarInit>(InitList[0]))
933 Type = Arg0->getType();
934 else if (ListInit *Arg0 = dyn_cast<ListInit>(InitList[0]))
935 Type = Arg0->getType();
936 else {
937 InitList[0]->dump();
938 Error(OpLoc, "expected a list");
939 return nullptr;
940 }
941 }
942
Chris Lattner61ea00b2010-10-05 23:58:18 +0000943 // We allow multiple operands to associative operators like !strconcat as
944 // shorthand for nesting them.
Daniel Sanders314e80e2014-05-07 10:13:19 +0000945 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
Chris Lattner61ea00b2010-10-05 23:58:18 +0000946 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000947 Init *RHS = InitList.pop_back_val();
David Greenee32ebf22011-07-29 19:07:07 +0000948 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
949 ->Fold(CurRec, CurMultiClass);
Chris Lattner61ea00b2010-10-05 23:58:18 +0000950 InitList.back() = RHS;
951 }
952 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000953
Chris Lattner61ea00b2010-10-05 23:58:18 +0000954 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +0000955 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner61ea00b2010-10-05 23:58:18 +0000956 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000957
Chris Lattner61ea00b2010-10-05 23:58:18 +0000958 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +0000959 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000960 }
961
David Greene3587eed2009-05-14 23:26:46 +0000962 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +0000963 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +0000964 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
965 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000966 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000967
David Greene98ed3c72009-05-14 21:54:42 +0000968 tgtok::TokKind LexCode = Lex.getCode();
969 Lex.Lex(); // eat the operation
970 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +0000971 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +0000972 case tgtok::XIf:
973 Code = TernOpInit::IF;
974 break;
David Greenee917fff2009-05-14 22:23:47 +0000975 case tgtok::XForEach:
976 Code = TernOpInit::FOREACH;
977 break;
David Greene98ed3c72009-05-14 21:54:42 +0000978 case tgtok::XSubst:
979 Code = TernOpInit::SUBST;
980 break;
981 }
982 if (Lex.getCode() != tgtok::l_paren) {
983 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000984 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +0000985 }
986 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000987
David Greeneaf8ee2c2011-07-29 22:43:06 +0000988 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000989 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000990
David Greene98ed3c72009-05-14 21:54:42 +0000991 if (Lex.getCode() != tgtok::comma) {
992 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000993 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +0000994 }
995 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +0000996
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000997 Init *MHS = ParseValue(CurRec, ItemType);
998 if (!MHS)
999 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001000
David Greene98ed3c72009-05-14 21:54:42 +00001001 if (Lex.getCode() != tgtok::comma) {
1002 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001003 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001004 }
1005 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001006
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001007 Init *RHS = ParseValue(CurRec, ItemType);
1008 if (!RHS)
1009 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001010
David Greene98ed3c72009-05-14 21:54:42 +00001011 if (Lex.getCode() != tgtok::r_paren) {
1012 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001013 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001014 }
1015 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001016
David Greene98ed3c72009-05-14 21:54:42 +00001017 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001018 default: llvm_unreachable("Unhandled code!");
David Greene3587eed2009-05-14 23:26:46 +00001019 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001020 RecTy *MHSTy = nullptr;
1021 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001022
Sean Silvafb509ed2012-10-10 20:24:43 +00001023 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001024 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001025 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001026 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001027 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001028 MHSTy = BitRecTy::get();
1029
Sean Silvafb509ed2012-10-10 20:24:43 +00001030 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001031 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001032 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001033 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001034 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001035 RHSTy = BitRecTy::get();
1036
1037 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001038 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001039 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001040 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001041 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001042
1043 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001044 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001045 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001046 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001047
1048 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1049 Type = RHSTy;
1050 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1051 Type = MHSTy;
Bob Wilson7248f862009-11-22 04:24:42 +00001052 } else {
David Greene3587eed2009-05-14 23:26:46 +00001053 TokError("inconsistent types for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001054 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001055 }
1056 break;
1057 }
David Greenee917fff2009-05-14 22:23:47 +00001058 case tgtok::XForEach: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001059 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
Craig Topper011817a2014-04-09 04:50:04 +00001060 if (!MHSt) {
David Greenee917fff2009-05-14 22:23:47 +00001061 TokError("could not get type for !foreach");
Craig Topper011817a2014-04-09 04:50:04 +00001062 return nullptr;
David Greenee917fff2009-05-14 22:23:47 +00001063 }
1064 Type = MHSt->getType();
1065 break;
1066 }
David Greene98ed3c72009-05-14 21:54:42 +00001067 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001068 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001069 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001070 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001071 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001072 }
1073 Type = RHSt->getType();
1074 break;
1075 }
1076 }
David Greenee32ebf22011-07-29 19:07:07 +00001077 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson7248f862009-11-22 04:24:42 +00001078 CurMultiClass);
David Greene98ed3c72009-05-14 21:54:42 +00001079 }
David Greene5d0c0512009-05-14 20:54:48 +00001080 }
David Greene5d0c0512009-05-14 20:54:48 +00001081}
1082
1083/// ParseOperatorType - Parse a type for an operator. This returns
1084/// null on error.
1085///
1086/// OperatorType ::= '<' Type '>'
1087///
Dan Gohman1432ef82009-08-12 22:10:57 +00001088RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001089 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001090
1091 if (Lex.getCode() != tgtok::less) {
1092 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001093 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001094 }
1095 Lex.Lex(); // eat the <
1096
1097 Type = ParseType();
1098
Craig Topper011817a2014-04-09 04:50:04 +00001099 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001100 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001101 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001102 }
1103
1104 if (Lex.getCode() != tgtok::greater) {
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 return Type;
1111}
1112
1113
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001114/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1115///
1116/// SimpleValue ::= IDValue
1117/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001118/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001119/// SimpleValue ::= CODEFRAGMENT
1120/// SimpleValue ::= '?'
1121/// SimpleValue ::= '{' ValueList '}'
1122/// SimpleValue ::= ID '<' ValueListNE '>'
1123/// SimpleValue ::= '[' ValueList ']'
1124/// SimpleValue ::= '(' IDValue DagArgList ')'
1125/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001126/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001127/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1128/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1129/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001130/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001131/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1132///
David Greened4263a62011-10-19 13:04:20 +00001133Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1134 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001135 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001136 switch (Lex.getCode()) {
1137 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001138 case tgtok::paste:
1139 // This is a leading paste operation. This is deprecated but
1140 // still exists in some .td files. Ignore it.
1141 Lex.Lex(); // Skip '#'.
1142 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001143 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001144 case tgtok::BinaryIntVal: {
1145 auto BinaryVal = Lex.getCurBinaryIntVal();
1146 SmallVector<Init*, 16> Bits(BinaryVal.second);
1147 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001148 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001149 R = BitsInit::get(Bits);
1150 Lex.Lex();
1151 break;
1152 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001153 case tgtok::StrVal: {
1154 std::string Val = Lex.getCurStrVal();
1155 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001156
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001157 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001158 while (Lex.getCode() == tgtok::StrVal) {
1159 Val += Lex.getCurStrVal();
1160 Lex.Lex();
1161 }
Bob Wilson7248f862009-11-22 04:24:42 +00001162
David Greenee32ebf22011-07-29 19:07:07 +00001163 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001164 break;
1165 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001166 case tgtok::CodeFragment:
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +00001167 R = StringInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001168 Lex.Lex();
1169 break;
1170 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001171 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001172 Lex.Lex();
1173 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001174 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001175 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001176 std::string Name = Lex.getCurStrVal();
1177 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001178 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001179
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001180 // Value ::= ID '<' ValueListNE '>'
1181 if (Lex.Lex() == tgtok::greater) {
1182 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001183 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001184 }
David Greene8618f952009-06-08 20:23:18 +00001185
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001186 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1187 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1188 // body.
1189 Record *Class = Records.getClass(Name);
1190 if (!Class) {
1191 Error(NameLoc, "Expected a class name, got '" + Name + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001192 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001193 }
David Greene8618f952009-06-08 20:23:18 +00001194
David Greeneaf8ee2c2011-07-29 22:43:06 +00001195 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
Craig Topper011817a2014-04-09 04:50:04 +00001196 if (ValueList.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001197
David Greene8618f952009-06-08 20:23:18 +00001198 if (Lex.getCode() != tgtok::greater) {
1199 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001200 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001201 }
1202 Lex.Lex(); // eat the '>'
Jordan Rosef12e8a92013-01-10 18:50:11 +00001203 SMLoc EndLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00001204
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001205 // Create the new record, set it as CurRec temporarily.
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00001206 auto NewRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), NameLoc,
1207 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001208 Record *NewRec = NewRecOwner.get(); // Keep a copy since we may release.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001209 SubClassReference SCRef;
Jordan Rosef12e8a92013-01-10 18:50:11 +00001210 SCRef.RefRange = SMRange(NameLoc, EndLoc);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001211 SCRef.Rec = Class;
1212 SCRef.TemplateArgs = ValueList;
1213 // Add info about the subclass to NewRec.
Craig Topper84138712014-11-29 05:31:10 +00001214 if (AddSubClass(NewRec, SCRef))
Craig Topper011817a2014-04-09 04:50:04 +00001215 return nullptr;
Craig Topper84138712014-11-29 05:31:10 +00001216
Hal Finkela8c1f462014-01-02 20:47:09 +00001217 if (!CurMultiClass) {
1218 NewRec->resolveReferences();
Craig Toppercdab2322014-11-29 05:52:51 +00001219 Records.addDef(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001220 } else {
Adam Nemete5a07162014-09-16 17:14:13 +00001221 // This needs to get resolved once the multiclass template arguments are
1222 // known before any use.
1223 NewRec->setResolveFirst(true);
Hal Finkela8c1f462014-01-02 20:47:09 +00001224 // Otherwise, we're inside a multiclass, add it to the multiclass.
Craig Topperc3504c42014-12-11 05:25:33 +00001225 CurMultiClass->DefPrototypes.push_back(std::move(NewRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00001226
1227 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00001228 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
1229 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Hal Finkela8c1f462014-01-02 20:47:09 +00001230 assert(RV && "Template arg doesn't exist?");
1231 NewRec->addValue(*RV);
1232 }
1233
1234 // We can't return the prototype def here, instead return:
1235 // !cast<ItemType>(!strconcat(NAME, AnonName)).
1236 const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1237 assert(MCNameRV && "multiclass record must have a NAME");
1238
1239 return UnOpInit::get(UnOpInit::CAST,
1240 BinOpInit::get(BinOpInit::STRCONCAT,
1241 VarInit::get(MCNameRV->getName(),
1242 MCNameRV->getType()),
1243 NewRec->getNameInit(),
1244 StringRecTy::get()),
1245 Class->getDefInit()->getType());
1246 }
Bob Wilson7248f862009-11-22 04:24:42 +00001247
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001248 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001249 return DefInit::get(NewRec);
Bob Wilson7248f862009-11-22 04:24:42 +00001250 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001251 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001252 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001253 Lex.Lex(); // eat the '{'
David Greeneaf8ee2c2011-07-29 22:43:06 +00001254 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001255
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001256 if (Lex.getCode() != tgtok::r_brace) {
1257 Vals = ParseValueList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001258 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001259 }
1260 if (Lex.getCode() != tgtok::r_brace) {
1261 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001262 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001263 }
1264 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001265
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001266 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001267
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001268 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1269 // first. We'll first read everything in to a vector, then we can reverse
1270 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001271 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001272 // FIXME: The following two loops would not be duplicated
1273 // if the API was a little more orthogonal.
1274
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001275 // bits<n> values are allowed to initialize n bits.
1276 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1277 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1278 NewBits.push_back(BI->getBit((e - i) - 1));
1279 continue;
1280 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001281 // bits<n> can also come from variable initializers.
1282 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1283 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1284 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1285 NewBits.push_back(VI->getBit((e - i) - 1));
1286 continue;
1287 }
1288 // Fallthrough to try convert this to a bit.
1289 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001290 // All other values must be convertible to just a single bit.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001291 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001292 if (!Bit) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001293 Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
Chris Lattner52416952007-11-22 21:06:59 +00001294 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001295 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001296 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001297 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001298 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001299 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001300 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001301 }
1302 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1303 Lex.Lex(); // eat the '['
David Greeneaf8ee2c2011-07-29 22:43:06 +00001304 std::vector<Init*> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001305
Craig Topper011817a2014-04-09 04:50:04 +00001306 RecTy *DeducedEltTy = nullptr;
1307 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001308
Craig Topper011817a2014-04-09 04:50:04 +00001309 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001310 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001311 if (!ListType) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001312 TokError(Twine("Type mismatch for list, expected list type, got ") +
1313 ItemType->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001314 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001315 }
1316 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001317 }
David Greene8618f952009-06-08 20:23:18 +00001318
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001319 if (Lex.getCode() != tgtok::r_square) {
Craig Topper011817a2014-04-09 04:50:04 +00001320 Vals = ParseValueList(CurRec, nullptr,
1321 GivenListTy ? GivenListTy->getElementType() : nullptr);
1322 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001323 }
1324 if (Lex.getCode() != tgtok::r_square) {
1325 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001326 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001327 }
1328 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001329
Craig Topper011817a2014-04-09 04:50:04 +00001330 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001331 if (Lex.getCode() == tgtok::less) {
1332 // Optional list element type
1333 Lex.Lex(); // eat the '<'
1334
1335 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001336 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001337 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001338 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001339 }
1340
1341 if (Lex.getCode() != tgtok::greater) {
1342 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001343 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001344 }
1345 Lex.Lex(); // eat the '>'
1346 }
1347
1348 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001349 RecTy *EltTy = nullptr;
Craig Toppereb4d7c62015-04-29 04:43:36 +00001350 for (Init *V : Vals) {
1351 TypedInit *TArg = dyn_cast<TypedInit>(V);
Craig Topper011817a2014-04-09 04:50:04 +00001352 if (!TArg) {
David Greene8618f952009-06-08 20:23:18 +00001353 TokError("Untyped list element");
Craig Topper011817a2014-04-09 04:50:04 +00001354 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001355 }
Craig Topper011817a2014-04-09 04:50:04 +00001356 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001357 EltTy = resolveTypes(EltTy, TArg->getType());
Craig Topper011817a2014-04-09 04:50:04 +00001358 if (!EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001359 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001360 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001361 }
Bob Wilson7248f862009-11-22 04:24:42 +00001362 } else {
David Greene8618f952009-06-08 20:23:18 +00001363 EltTy = TArg->getType();
1364 }
1365 }
1366
Craig Topper011817a2014-04-09 04:50:04 +00001367 if (GivenEltTy) {
1368 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001369 // Verify consistency
1370 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1371 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001372 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001373 }
1374 }
1375 EltTy = GivenEltTy;
1376 }
1377
Craig Topper011817a2014-04-09 04:50:04 +00001378 if (!EltTy) {
1379 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001380 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001381 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001382 }
1383 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001384 } else {
David Greene8618f952009-06-08 20:23:18 +00001385 // Make sure the deduced type is compatible with the given type
1386 if (GivenListTy) {
1387 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1388 TokError("Element type mismatch for list");
Craig Topper011817a2014-04-09 04:50:04 +00001389 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001390 }
1391 }
1392 DeducedEltTy = EltTy;
1393 }
Bob Wilson7248f862009-11-22 04:24:42 +00001394
David Greenee32ebf22011-07-29 19:07:07 +00001395 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001396 }
1397 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1398 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001399 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001400 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001401 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001402 }
Bob Wilson7248f862009-11-22 04:24:42 +00001403
David Greeneaf8ee2c2011-07-29 22:43:06 +00001404 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001405 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001406
Nate Begemandbe3f772009-03-19 05:21:56 +00001407 // If the operator name is present, parse it.
1408 std::string OperatorName;
1409 if (Lex.getCode() == tgtok::colon) {
1410 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1411 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001412 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001413 }
1414 OperatorName = Lex.getCurStrVal();
1415 Lex.Lex(); // eat the VarName.
1416 }
Bob Wilson7248f862009-11-22 04:24:42 +00001417
David Greeneaf8ee2c2011-07-29 22:43:06 +00001418 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001419 if (Lex.getCode() != tgtok::r_paren) {
1420 DagArgs = ParseDagArgList(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001421 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001422 }
Bob Wilson7248f862009-11-22 04:24:42 +00001423
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001424 if (Lex.getCode() != tgtok::r_paren) {
1425 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001426 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001427 }
1428 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001429
David Greenee32ebf22011-07-29 19:07:07 +00001430 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001431 }
Bob Wilson7248f862009-11-22 04:24:42 +00001432
David Greene2f7cf7f2011-01-07 17:05:37 +00001433 case tgtok::XHead:
1434 case tgtok::XTail:
1435 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001436 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001437 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001438 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001439 case tgtok::XAND:
Bob Wilson7248f862009-11-22 04:24:42 +00001440 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001441 case tgtok::XSRL:
1442 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001443 case tgtok::XEq:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001444 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001445 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001446 case tgtok::XIf:
David Greenee917fff2009-05-14 22:23:47 +00001447 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001448 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001449 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001450 }
1451 }
Bob Wilson7248f862009-11-22 04:24:42 +00001452
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001453 return R;
1454}
1455
1456/// ParseValue - Parse a tblgen value. This returns null on error.
1457///
1458/// Value ::= SimpleValue ValueSuffix*
1459/// ValueSuffix ::= '{' BitList '}'
1460/// ValueSuffix ::= '[' BitList ']'
1461/// ValueSuffix ::= '.' ID
1462///
David Greened4263a62011-10-19 13:04:20 +00001463Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1464 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001465 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001466
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001467 // Parse the suffixes now if present.
1468 while (1) {
1469 switch (Lex.getCode()) {
1470 default: return Result;
1471 case tgtok::l_brace: {
David Greenefb927af2012-02-22 16:09:41 +00001472 if (Mode == ParseNameMode || Mode == ParseForeachMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001473 // This is the beginning of the object body.
1474 return Result;
1475
Chris Lattner526c8cb2009-06-21 03:39:35 +00001476 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001477 Lex.Lex(); // eat the '{'
1478 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001479 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001480
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001481 // Reverse the bitlist.
1482 std::reverse(Ranges.begin(), Ranges.end());
1483 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001484 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001485 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001486 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001487 }
Bob Wilson7248f862009-11-22 04:24:42 +00001488
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001489 // Eat the '}'.
1490 if (Lex.getCode() != tgtok::r_brace) {
1491 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001492 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001493 }
1494 Lex.Lex();
1495 break;
1496 }
1497 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001498 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001499 Lex.Lex(); // eat the '['
1500 std::vector<unsigned> Ranges = ParseRangeList();
Craig Topper011817a2014-04-09 04:50:04 +00001501 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001502
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001503 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001504 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001505 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001506 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001507 }
Bob Wilson7248f862009-11-22 04:24:42 +00001508
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001509 // Eat the ']'.
1510 if (Lex.getCode() != tgtok::r_square) {
1511 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001512 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001513 }
1514 Lex.Lex();
1515 break;
1516 }
1517 case tgtok::period:
1518 if (Lex.Lex() != tgtok::Id) { // eat the .
1519 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001520 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001521 }
1522 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001523 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001524 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001525 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001526 }
David Greenee32ebf22011-07-29 19:07:07 +00001527 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001528 Lex.Lex(); // eat field name
1529 break;
David Greene8e85b482011-10-19 13:04:43 +00001530
1531 case tgtok::paste:
1532 SMLoc PasteLoc = Lex.getLoc();
1533
1534 // Create a !strconcat() operation, first casting each operand to
1535 // a string if necessary.
1536
Sean Silvafb509ed2012-10-10 20:24:43 +00001537 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001538 if (!LHS) {
1539 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001540 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001541 }
Craig Toppera9642b42015-05-04 01:35:39 +00001542
David Greene8e85b482011-10-19 13:04:43 +00001543 if (LHS->getType() != StringRecTy::get()) {
1544 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1545 }
1546
Craig Topper011817a2014-04-09 04:50:04 +00001547 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001548
1549 Lex.Lex(); // Eat the '#'.
1550 switch (Lex.getCode()) {
1551 case tgtok::colon:
1552 case tgtok::semi:
1553 case tgtok::l_brace:
1554 // These are all of the tokens that can begin an object body.
1555 // Some of these can also begin values but we disallow those cases
1556 // because they are unlikely to be useful.
Craig Toppera9642b42015-05-04 01:35:39 +00001557
David Greene8e85b482011-10-19 13:04:43 +00001558 // Trailing paste, concat with an empty string.
1559 RHS = StringInit::get("");
1560 break;
1561
1562 default:
1563 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001564 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00001565 if (!RHS) {
1566 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001567 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001568 }
1569
1570 if (RHS->getType() != StringRecTy::get()) {
1571 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1572 }
Craig Toppera9642b42015-05-04 01:35:39 +00001573
David Greene8e85b482011-10-19 13:04:43 +00001574 break;
1575 }
1576
1577 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1578 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1579 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001580 }
1581 }
1582}
1583
1584/// ParseDagArgList - Parse the argument list for a dag literal expression.
1585///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001586/// DagArg ::= Value (':' VARNAME)?
1587/// DagArg ::= VARNAME
1588/// DagArgList ::= DagArg
1589/// DagArgList ::= DagArgList ',' DagArg
David Greeneaf8ee2c2011-07-29 22:43:06 +00001590std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001591TGParser::ParseDagArgList(Record *CurRec) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001592 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson7248f862009-11-22 04:24:42 +00001593
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001594 while (1) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001595 // DagArg ::= VARNAME
1596 if (Lex.getCode() == tgtok::VarName) {
1597 // A missing value is treated like '?'.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00001598 Result.emplace_back(UnsetInit::get(), Lex.getCurStrVal());
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001599 Lex.Lex();
1600 } else {
1601 // DagArg ::= Value (':' VARNAME)?
1602 Init *Val = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001603 if (!Val)
David Greeneaf8ee2c2011-07-29 22:43:06 +00001604 return std::vector<std::pair<llvm::Init*, std::string> >();
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001605
1606 // If the variable name is present, add it.
1607 std::string VarName;
1608 if (Lex.getCode() == tgtok::colon) {
1609 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1610 TokError("expected variable name in dag literal");
1611 return std::vector<std::pair<llvm::Init*, std::string> >();
1612 }
1613 VarName = Lex.getCurStrVal();
1614 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001615 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00001616
1617 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001618 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001619 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00001620 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001621 }
Bob Wilson7248f862009-11-22 04:24:42 +00001622
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001623 return Result;
1624}
1625
1626
1627/// ParseValueList - Parse a comma separated list of values, returning them as a
1628/// vector. Note that this always expects to be able to parse at least one
1629/// value. It returns an empty list if this is not possible.
1630///
1631/// ValueList ::= Value (',' Value)
1632///
David Greeneaf8ee2c2011-07-29 22:43:06 +00001633std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopher71520a82011-07-11 23:06:52 +00001634 RecTy *EltTy) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001635 std::vector<Init*> Result;
David Greene8618f952009-06-08 20:23:18 +00001636 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00001637 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00001638 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001639 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001640 if (TArgs.empty()) {
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00001641 TokError("template argument provided to non-template class");
1642 return std::vector<Init*>();
1643 }
David Greene8618f952009-06-08 20:23:18 +00001644 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00001645 if (!RV) {
1646 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1647 << ")\n";
1648 }
David Greene8618f952009-06-08 20:23:18 +00001649 assert(RV && "Template argument record not found??");
1650 ItemType = RV->getType();
1651 ++ArgN;
1652 }
1653 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001654 if (!Result.back()) return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001655
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001656 while (Lex.getCode() == tgtok::comma) {
1657 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00001658
Craig Topper011817a2014-04-09 04:50:04 +00001659 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00001660 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00001661 if (ArgN >= TArgs.size()) {
1662 TokError("too many template arguments");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001663 return std::vector<Init*>();
Bob Wilson7248f862009-11-22 04:24:42 +00001664 }
David Greene8618f952009-06-08 20:23:18 +00001665 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1666 assert(RV && "Template argument record not found??");
1667 ItemType = RV->getType();
1668 ++ArgN;
1669 }
1670 Result.push_back(ParseValue(CurRec, ItemType));
Craig Topper011817a2014-04-09 04:50:04 +00001671 if (!Result.back()) return std::vector<Init*>();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001672 }
Bob Wilson7248f862009-11-22 04:24:42 +00001673
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001674 return Result;
1675}
1676
1677
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001678/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1679/// empty string on error. This can happen in a number of different context's,
1680/// including within a def or in the template args for a def (which which case
1681/// CurRec will be non-null) and within the template args for a multiclass (in
1682/// which case CurRec will be null, but CurMultiClass will be set). This can
1683/// also happen within a def that is within a multiclass, which will set both
1684/// CurRec and CurMultiClass.
1685///
1686/// Declaration ::= FIELD? Type ID ('=' Value)?
1687///
David Greenedb10e692011-10-19 13:02:42 +00001688Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001689 bool ParsingTemplateArgs) {
1690 // Read the field prefix if present.
1691 bool HasField = Lex.getCode() == tgtok::Field;
1692 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001693
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001694 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001695 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001696
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001697 if (Lex.getCode() != tgtok::Id) {
1698 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001699 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001700 }
Bob Wilson7248f862009-11-22 04:24:42 +00001701
Chris Lattner526c8cb2009-06-21 03:39:35 +00001702 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00001703 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001704 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001705
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001706 if (ParsingTemplateArgs) {
Craig Toppera9642b42015-05-04 01:35:39 +00001707 if (CurRec)
David Greenedb10e692011-10-19 13:02:42 +00001708 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Craig Toppera9642b42015-05-04 01:35:39 +00001709 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001710 assert(CurMultiClass);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001711 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00001712 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1713 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001714 }
Bob Wilson7248f862009-11-22 04:24:42 +00001715
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001716 // Add the value.
1717 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00001718 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001719
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001720 // If a value is present, parse it.
1721 if (Lex.getCode() == tgtok::equal) {
1722 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00001723 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00001724 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001725 if (!Val ||
Craig Toppercfd81732016-01-04 03:15:08 +00001726 SetValue(CurRec, ValLoc, DeclName, None, Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00001727 // Return the name, even if an error is thrown. This is so that we can
1728 // continue to make some progress, even without the value having been
1729 // initialized.
1730 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001731 }
Bob Wilson7248f862009-11-22 04:24:42 +00001732
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001733 return DeclName;
1734}
1735
David Greenefb927af2012-02-22 16:09:41 +00001736/// ParseForeachDeclaration - Read a foreach declaration, returning
1737/// the name of the declared object or a NULL Init on error. Return
1738/// the name of the parsed initializer list through ForeachListName.
1739///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001740/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
1741/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
1742/// ForeachDeclaration ::= ID '=' RangePiece
David Greenefb927af2012-02-22 16:09:41 +00001743///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00001744VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00001745 if (Lex.getCode() != tgtok::Id) {
1746 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001747 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001748 }
1749
1750 Init *DeclName = StringInit::get(Lex.getCurStrVal());
1751 Lex.Lex();
1752
1753 // If a value is present, parse it.
1754 if (Lex.getCode() != tgtok::equal) {
1755 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00001756 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00001757 }
1758 Lex.Lex(); // Eat the '='
1759
Craig Topper011817a2014-04-09 04:50:04 +00001760 RecTy *IterType = nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001761 std::vector<unsigned> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00001762
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001763 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +00001764 default: TokError("Unknown token when expecting a range list"); return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001765 case tgtok::l_square: { // '[' ValueList ']'
Craig Topper011817a2014-04-09 04:50:04 +00001766 Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00001767 ForeachListValue = dyn_cast<ListInit>(List);
Craig Topper011817a2014-04-09 04:50:04 +00001768 if (!ForeachListValue) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001769 TokError("Expected a Value list");
Craig Topper011817a2014-04-09 04:50:04 +00001770 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001771 }
1772 RecTy *ValueType = ForeachListValue->getType();
Sean Silva98c61712012-10-05 03:31:58 +00001773 ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
Craig Topper011817a2014-04-09 04:50:04 +00001774 if (!ListType) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001775 TokError("Value list is not of list type");
Craig Topper011817a2014-04-09 04:50:04 +00001776 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001777 }
1778 IterType = ListType->getElementType();
1779 break;
David Greenefb927af2012-02-22 16:09:41 +00001780 }
1781
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001782 case tgtok::IntVal: { // RangePiece.
1783 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00001784 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001785 break;
David Greenefb927af2012-02-22 16:09:41 +00001786 }
1787
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001788 case tgtok::l_brace: { // '{' RangeList '}'
1789 Lex.Lex(); // eat the '{'
1790 Ranges = ParseRangeList();
1791 if (Lex.getCode() != tgtok::r_brace) {
1792 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001793 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001794 }
1795 Lex.Lex();
1796 break;
1797 }
1798 }
David Greenefb927af2012-02-22 16:09:41 +00001799
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001800 if (!Ranges.empty()) {
1801 assert(!IterType && "Type already initialized?");
1802 IterType = IntRecTy::get();
1803 std::vector<Init*> Values;
Craig Topper8eb764c2015-06-02 06:19:28 +00001804 for (unsigned R : Ranges)
1805 Values.push_back(IntInit::get(R));
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001806 ForeachListValue = ListInit::get(Values, IterType);
1807 }
1808
1809 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00001810 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00001811
1812 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00001813}
1814
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001815/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1816/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1817/// template args for a def, which may or may not be in a multiclass. If null,
1818/// these are the template args for a multiclass.
1819///
1820/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001821///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001822bool TGParser::ParseTemplateArgList(Record *CurRec) {
1823 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1824 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00001825
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001826 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00001827
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001828 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00001829 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001830 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001831 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001832
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001833 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00001834
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001835 while (Lex.getCode() == tgtok::comma) {
1836 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001837
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001838 // Read the following declarations.
1839 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00001840 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001841 return true;
1842 TheRecToAddTo->addTemplateArg(TemplArg);
1843 }
Bob Wilson7248f862009-11-22 04:24:42 +00001844
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001845 if (Lex.getCode() != tgtok::greater)
1846 return TokError("expected '>' at end of template argument list");
1847 Lex.Lex(); // eat the '>'.
1848 return false;
1849}
1850
1851
1852/// ParseBodyItem - Parse a single item at within the body of a def or class.
1853///
1854/// BodyItem ::= Declaration ';'
1855/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1856bool TGParser::ParseBodyItem(Record *CurRec) {
1857 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00001858 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001859 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001860
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001861 if (Lex.getCode() != tgtok::semi)
1862 return TokError("expected ';' after declaration");
1863 Lex.Lex();
1864 return false;
1865 }
1866
1867 // LET ID OptionalRangeList '=' Value ';'
1868 if (Lex.Lex() != tgtok::Id)
1869 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00001870
Chris Lattner526c8cb2009-06-21 03:39:35 +00001871 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001872 std::string FieldName = Lex.getCurStrVal();
1873 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00001874
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001875 std::vector<unsigned> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00001876 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001877 return true;
1878 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00001879
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001880 if (Lex.getCode() != tgtok::equal)
1881 return TokError("expected '=' in let expression");
1882 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00001883
David Greene8618f952009-06-08 20:23:18 +00001884 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00001885 if (!Field)
David Greene8618f952009-06-08 20:23:18 +00001886 return TokError("Value '" + FieldName + "' unknown!");
1887
1888 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00001889
David Greeneaf8ee2c2011-07-29 22:43:06 +00001890 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00001891 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001892
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001893 if (Lex.getCode() != tgtok::semi)
1894 return TokError("expected ';' after let expression");
1895 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001896
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001897 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1898}
1899
1900/// ParseBody - Read the body of a class or def. Return true on error, false on
1901/// success.
1902///
1903/// Body ::= ';'
1904/// Body ::= '{' BodyList '}'
1905/// BodyList BodyItem*
1906///
1907bool TGParser::ParseBody(Record *CurRec) {
1908 // If this is a null definition, just eat the semi and return.
1909 if (Lex.getCode() == tgtok::semi) {
1910 Lex.Lex();
1911 return false;
1912 }
Bob Wilson7248f862009-11-22 04:24:42 +00001913
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001914 if (Lex.getCode() != tgtok::l_brace)
1915 return TokError("Expected ';' or '{' to start body");
1916 // Eat the '{'.
1917 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001918
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001919 while (Lex.getCode() != tgtok::r_brace)
1920 if (ParseBodyItem(CurRec))
1921 return true;
1922
1923 // Eat the '}'.
1924 Lex.Lex();
1925 return false;
1926}
1927
Sean Silvacb1a75e2013-01-09 04:49:14 +00001928/// \brief Apply the current let bindings to \a CurRec.
1929/// \returns true on error, false otherwise.
1930bool TGParser::ApplyLetStack(Record *CurRec) {
Craig Topper8eb764c2015-06-02 06:19:28 +00001931 for (std::vector<LetRecord> &LetInfo : LetStack)
1932 for (LetRecord &LR : LetInfo)
1933 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
Sean Silvacb1a75e2013-01-09 04:49:14 +00001934 return true;
1935 return false;
1936}
1937
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001938/// ParseObjectBody - Parse the body of a def or class. This consists of an
1939/// optional ClassList followed by a Body. CurRec is the current def or class
1940/// that is being parsed.
1941///
1942/// ObjectBody ::= BaseClassList Body
1943/// BaseClassList ::= /*empty*/
1944/// BaseClassList ::= ':' BaseClassListNE
1945/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1946///
1947bool TGParser::ParseObjectBody(Record *CurRec) {
1948 // If there is a baseclass list, read it.
1949 if (Lex.getCode() == tgtok::colon) {
1950 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001951
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001952 // Read all of the subclasses.
1953 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1954 while (1) {
1955 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00001956 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001957
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001958 // Add it.
1959 if (AddSubClass(CurRec, SubClass))
1960 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001961
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001962 if (Lex.getCode() != tgtok::comma) break;
1963 Lex.Lex(); // eat ','.
1964 SubClass = ParseSubClassReference(CurRec, false);
1965 }
1966 }
1967
Sean Silvacb1a75e2013-01-09 04:49:14 +00001968 if (ApplyLetStack(CurRec))
1969 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00001970
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001971 return ParseBody(CurRec);
1972}
1973
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001974/// ParseDef - Parse and return a top level or multiclass def, return the record
1975/// corresponding to it. This returns null on error.
1976///
1977/// DefInst ::= DEF ObjectName ObjectBody
1978///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00001979bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001980 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001981 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00001982 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001983
1984 // Parse ObjectName and make a record for it.
Craig Topper84138712014-11-29 05:31:10 +00001985 std::unique_ptr<Record> CurRecOwner;
Jordan Roseabdd99b2013-01-10 18:50:05 +00001986 Init *Name = ParseObjectName(CurMultiClass);
1987 if (Name)
Craig Topper84138712014-11-29 05:31:10 +00001988 CurRecOwner = make_unique<Record>(Name, DefLoc, Records);
Jordan Roseabdd99b2013-01-10 18:50:05 +00001989 else
Hans Wennborgb9a6eaa2014-11-30 00:24:43 +00001990 CurRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), DefLoc,
1991 Records, /*IsAnonymous=*/true);
Craig Topper84138712014-11-29 05:31:10 +00001992 Record *CurRec = CurRecOwner.get(); // Keep a copy since we may release.
Bob Wilson7248f862009-11-22 04:24:42 +00001993
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00001994 if (!CurMultiClass && Loops.empty()) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001995 // Top-level def definition.
Bob Wilson7248f862009-11-22 04:24:42 +00001996
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001997 // Ensure redefinition doesn't happen.
Craig Topper84138712014-11-29 05:31:10 +00001998 if (Records.getDef(CurRec->getNameInitAsString()))
1999 return Error(DefLoc, "def '" + CurRec->getNameInitAsString()+
2000 "' already defined");
Craig Toppercdab2322014-11-29 05:52:51 +00002001 Records.addDef(std::move(CurRecOwner));
Hal Finkela8c1f462014-01-02 20:47:09 +00002002
2003 if (ParseObjectBody(CurRec))
2004 return true;
Jakob Stoklund Olesen74fd80e2012-05-24 22:17:36 +00002005 } else if (CurMultiClass) {
Hal Finkela8c1f462014-01-02 20:47:09 +00002006 // Parse the body before adding this prototype to the DefPrototypes vector.
2007 // That way implicit definitions will be added to the DefPrototypes vector
2008 // before this object, instantiated prior to defs derived from this object,
2009 // and this available for indirect name resolution when defs derived from
2010 // this object are instantiated.
Craig Topper84138712014-11-29 05:31:10 +00002011 if (ParseObjectBody(CurRec))
Hal Finkela8c1f462014-01-02 20:47:09 +00002012 return true;
2013
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002014 // Otherwise, a def inside a multiclass, add it to the multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002015 for (const auto &Proto : CurMultiClass->DefPrototypes)
2016 if (Proto->getNameInit() == CurRec->getNameInit())
Craig Topper84138712014-11-29 05:31:10 +00002017 return Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
2018 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002019 CurMultiClass->DefPrototypes.push_back(std::move(CurRecOwner));
Anton Yartsev671dff12014-08-08 00:29:54 +00002020 } else if (ParseObjectBody(CurRec)) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002021 return true;
Anton Yartsev671dff12014-08-08 00:29:54 +00002022 }
Bob Wilson7248f862009-11-22 04:24:42 +00002023
Craig Topper011817a2014-04-09 04:50:04 +00002024 if (!CurMultiClass) // Def's in multiclasses aren't really defs.
David Greene50c09122011-08-10 18:27:46 +00002025 // See Record::setName(). This resolve step will see any new name
2026 // for the def that might have been created when resolving
2027 // inheritance, values and arguments above.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002028 CurRec->resolveReferences();
Bob Wilson7248f862009-11-22 04:24:42 +00002029
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002030 // If ObjectBody has template arguments, it's an error.
2031 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002032
2033 if (CurMultiClass) {
2034 // Copy the template arguments for the multiclass into the def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002035 for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
2036 const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002037 assert(RV && "Template arg doesn't exist?");
2038 CurRec->addValue(*RV);
2039 }
2040 }
2041
Craig Toppera9642b42015-05-04 01:35:39 +00002042 if (ProcessForeachDefs(CurRec, DefLoc))
Craig Topper84138712014-11-29 05:31:10 +00002043 return Error(DefLoc, "Could not process loops for def" +
2044 CurRec->getNameInitAsString());
David Greenefb927af2012-02-22 16:09:41 +00002045
2046 return false;
2047}
2048
2049/// ParseForeach - Parse a for statement. Return the record corresponding
2050/// to it. This returns true on error.
2051///
2052/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2053/// Foreach ::= FOREACH Declaration IN Object
2054///
2055bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2056 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2057 Lex.Lex(); // Eat the 'for' token.
2058
2059 // Make a temporary object to record items associated with the for
2060 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002061 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002062 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002063 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002064 return TokError("expected declaration in for");
2065
2066 if (Lex.getCode() != tgtok::In)
2067 return TokError("Unknown tok");
2068 Lex.Lex(); // Eat the in
2069
2070 // Create a loop object and remember it.
2071 Loops.push_back(ForeachLoop(IterName, ListValue));
2072
2073 if (Lex.getCode() != tgtok::l_brace) {
2074 // FOREACH Declaration IN Object
2075 if (ParseObject(CurMultiClass))
2076 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002077 } else {
David Greenefb927af2012-02-22 16:09:41 +00002078 SMLoc BraceLoc = Lex.getLoc();
2079 // Otherwise, this is a group foreach.
2080 Lex.Lex(); // eat the '{'.
2081
2082 // Parse the object list.
2083 if (ParseObjectList(CurMultiClass))
2084 return true;
2085
2086 if (Lex.getCode() != tgtok::r_brace) {
2087 TokError("expected '}' at end of foreach command");
2088 return Error(BraceLoc, "to match this '{'");
2089 }
2090 Lex.Lex(); // Eat the }
2091 }
2092
2093 // We've processed everything in this loop.
2094 Loops.pop_back();
2095
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002096 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002097}
2098
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002099/// ParseClass - Parse a tblgen class definition.
2100///
2101/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2102///
2103bool TGParser::ParseClass() {
2104 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2105 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002106
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002107 if (Lex.getCode() != tgtok::Id)
2108 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002109
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002110 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2111 if (CurRec) {
2112 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002113 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002114 !CurRec->getSuperClasses().empty() ||
2115 !CurRec->getTemplateArgs().empty())
Craig Topper85c07002015-04-30 05:54:22 +00002116 return TokError("Class '" + CurRec->getNameInitAsString() +
2117 "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002118 } else {
2119 // If this is the first reference to this class, create and add it.
Hans Wennborgffbbd532014-11-30 00:31:49 +00002120 auto NewRec =
2121 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
Craig Toppercdab2322014-11-29 05:52:51 +00002122 CurRec = NewRec.get();
2123 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002124 }
2125 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002126
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002127 // If there are template args, parse them.
2128 if (Lex.getCode() == tgtok::less)
2129 if (ParseTemplateArgList(CurRec))
2130 return true;
2131
2132 // Finally, parse the object body.
2133 return ParseObjectBody(CurRec);
2134}
2135
2136/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2137/// of LetRecords.
2138///
2139/// LetList ::= LetItem (',' LetItem)*
2140/// LetItem ::= ID OptionalRangeList '=' Value
2141///
2142std::vector<LetRecord> TGParser::ParseLetList() {
2143 std::vector<LetRecord> Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002144
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002145 while (1) {
2146 if (Lex.getCode() != tgtok::Id) {
2147 TokError("expected identifier in let definition");
2148 return std::vector<LetRecord>();
2149 }
2150 std::string Name = Lex.getCurStrVal();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002151 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002152 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002153
2154 // Check for an optional RangeList.
2155 std::vector<unsigned> Bits;
Bob Wilson7248f862009-11-22 04:24:42 +00002156 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002157 return std::vector<LetRecord>();
2158 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002159
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002160 if (Lex.getCode() != tgtok::equal) {
2161 TokError("expected '=' in let expression");
2162 return std::vector<LetRecord>();
2163 }
2164 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002165
Craig Topper011817a2014-04-09 04:50:04 +00002166 Init *Val = ParseValue(nullptr);
2167 if (!Val) return std::vector<LetRecord>();
Bob Wilson7248f862009-11-22 04:24:42 +00002168
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002169 // Now that we have everything, add the record.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00002170 Result.emplace_back(std::move(Name), std::move(Bits), Val, NameLoc);
Bob Wilson7248f862009-11-22 04:24:42 +00002171
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002172 if (Lex.getCode() != tgtok::comma)
2173 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +00002174 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002175 }
2176}
2177
2178/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002179/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002180///
2181/// Object ::= LET LetList IN '{' ObjectList '}'
2182/// Object ::= LET LetList IN Object
2183///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002184bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002185 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2186 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002187
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002188 // Add this entry to the let stack.
2189 std::vector<LetRecord> LetInfo = ParseLetList();
2190 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002191 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002192
2193 if (Lex.getCode() != tgtok::In)
2194 return TokError("expected 'in' at end of top-level 'let'");
2195 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002196
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002197 // If this is a scalar let, just handle it now
2198 if (Lex.getCode() != tgtok::l_brace) {
2199 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002200 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002201 return true;
2202 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002203 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002204 // Otherwise, this is a group let.
2205 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002206
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002207 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002208 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002209 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002210
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002211 if (Lex.getCode() != tgtok::r_brace) {
2212 TokError("expected '}' at end of top level let command");
2213 return Error(BraceLoc, "to match this '{'");
2214 }
2215 Lex.Lex();
2216 }
Bob Wilson7248f862009-11-22 04:24:42 +00002217
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002218 // Outside this let scope, this let block is not active.
2219 LetStack.pop_back();
2220 return false;
2221}
2222
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002223/// ParseMultiClass - Parse a multiclass definition.
2224///
Bob Wilson3d948162009-04-28 19:41:44 +00002225/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002226/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2227/// MultiClassObject ::= DefInst
2228/// MultiClassObject ::= MultiClassInst
2229/// MultiClassObject ::= DefMInst
2230/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2231/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002232///
2233bool TGParser::ParseMultiClass() {
2234 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2235 Lex.Lex(); // Eat the multiclass token.
2236
2237 if (Lex.getCode() != tgtok::Id)
2238 return TokError("expected identifier after multiclass for name");
2239 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002240
Craig Topper7adf2bf2014-12-11 05:25:30 +00002241 auto Result =
2242 MultiClasses.insert(std::make_pair(Name,
2243 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2244
2245 if (!Result.second)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002246 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002247
Craig Topper7adf2bf2014-12-11 05:25:30 +00002248 CurMultiClass = Result.first->second.get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002249 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002250
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002251 // If there are template args, parse them.
2252 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002253 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002254 return true;
2255
David Greene7049e792009-04-24 16:55:41 +00002256 bool inherits = false;
2257
David Greene753ed8f2009-04-22 16:42:54 +00002258 // If there are submulticlasses, parse them.
2259 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002260 inherits = true;
2261
David Greene753ed8f2009-04-22 16:42:54 +00002262 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002263
David Greene753ed8f2009-04-22 16:42:54 +00002264 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002265 SubMultiClassReference SubMultiClass =
2266 ParseSubMultiClassReference(CurMultiClass);
David Greene753ed8f2009-04-22 16:42:54 +00002267 while (1) {
2268 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002269 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002270
David Greene753ed8f2009-04-22 16:42:54 +00002271 // Add it.
2272 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2273 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002274
David Greene753ed8f2009-04-22 16:42:54 +00002275 if (Lex.getCode() != tgtok::comma) break;
2276 Lex.Lex(); // eat ','.
2277 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2278 }
2279 }
2280
David Greene7049e792009-04-24 16:55:41 +00002281 if (Lex.getCode() != tgtok::l_brace) {
2282 if (!inherits)
2283 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002284 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002285 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002286 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002287 } else {
David Greene7049e792009-04-24 16:55:41 +00002288 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2289 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002290
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002291 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002292 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002293 default:
2294 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
2295 case tgtok::Let:
2296 case tgtok::Def:
2297 case tgtok::Defm:
2298 case tgtok::Foreach:
2299 if (ParseObject(CurMultiClass))
2300 return true;
2301 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002302 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002303 }
David Greene7049e792009-04-24 16:55:41 +00002304 Lex.Lex(); // eat the '}'.
2305 }
Bob Wilson7248f862009-11-22 04:24:42 +00002306
Craig Topper011817a2014-04-09 04:50:04 +00002307 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002308 return false;
2309}
2310
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002311Record *TGParser::InstantiateMulticlassDef(MultiClass &MC, Record *DefProto,
2312 Init *&DefmPrefix,
2313 SMRange DefmPrefixRange,
2314 ArrayRef<Init *> TArgs,
2315 std::vector<Init *> &TemplateVals) {
David Greene5d5d88c2011-10-19 13:04:31 +00002316 // We need to preserve DefProto so it can be reused for later
2317 // instantiations, so create a new Record to inherit from it.
2318
David Greenedb445972011-10-05 22:42:07 +00002319 // Add in the defm name. If the defm prefix is empty, give each
2320 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2321 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2322 // as a prefix.
David Greenedb445972011-10-05 22:42:07 +00002323
Jordan Roseabdd99b2013-01-10 18:50:05 +00002324 bool IsAnonymous = false;
Craig Topper011817a2014-04-09 04:50:04 +00002325 if (!DefmPrefix) {
David Greene5d5d88c2011-10-19 13:04:31 +00002326 DefmPrefix = StringInit::get(GetNewAnonymousName());
Jordan Roseabdd99b2013-01-10 18:50:05 +00002327 IsAnonymous = true;
2328 }
David Greene5d5d88c2011-10-19 13:04:31 +00002329
2330 Init *DefName = DefProto->getNameInit();
Sean Silvafb509ed2012-10-10 20:24:43 +00002331 StringInit *DefNameString = dyn_cast<StringInit>(DefName);
David Greene5d5d88c2011-10-19 13:04:31 +00002332
Craig Topper011817a2014-04-09 04:50:04 +00002333 if (DefNameString) {
David Greene8e85b482011-10-19 13:04:43 +00002334 // We have a fully expanded string so there are no operators to
2335 // resolve. We should concatenate the given prefix and name.
David Greene5d5d88c2011-10-19 13:04:31 +00002336 DefName =
2337 BinOpInit::get(BinOpInit::STRCONCAT,
2338 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2339 StringRecTy::get())->Fold(DefProto, &MC),
2340 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2341 }
David Greene5d5d88c2011-10-19 13:04:31 +00002342
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002343 // Make a trail of SMLocs from the multiclass instantiations.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002344 SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +00002345 Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
Craig Topper84138712014-11-29 05:31:10 +00002346 auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
David Greenedb445972011-10-05 22:42:07 +00002347
2348 SubClassReference Ref;
Jordan Rosef12e8a92013-01-10 18:50:11 +00002349 Ref.RefRange = DefmPrefixRange;
David Greenedb445972011-10-05 22:42:07 +00002350 Ref.Rec = DefProto;
Craig Topper84138712014-11-29 05:31:10 +00002351 AddSubClass(CurRec.get(), Ref);
David Greenedb445972011-10-05 22:42:07 +00002352
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002353 // Set the value for NAME. We don't resolve references to it 'til later,
2354 // though, so that uses in nested multiclass names don't get
2355 // confused.
Craig Toppercfd81732016-01-04 03:15:08 +00002356 if (SetValue(CurRec.get(), Ref.RefRange.Start, "NAME", None, DefmPrefix,
Craig Topper1e23ed92016-01-04 03:05:14 +00002357 /*AllowSelfAssignment*/true)) {
Craig Topper85c07002015-04-30 05:54:22 +00002358 Error(DefmPrefixRange.Start, "Could not resolve " +
2359 CurRec->getNameInitAsString() + ":NAME to '" +
2360 DefmPrefix->getAsUnquotedString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002361 return nullptr;
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002362 }
David Greene8bf0d722011-10-19 13:04:35 +00002363
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002364 // If the DefNameString didn't resolve, we probably have a reference to
2365 // NAME and need to replace it. We need to do at least this much greedily,
2366 // otherwise nested multiclasses will end up with incorrect NAME expansions.
Craig Topper011817a2014-04-09 04:50:04 +00002367 if (!DefNameString) {
David Greene8bf0d722011-10-19 13:04:35 +00002368 RecordVal *DefNameRV = CurRec->getValue("NAME");
2369 CurRec->resolveReferencesTo(DefNameRV);
2370 }
2371
2372 if (!CurMultiClass) {
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002373 // Now that we're at the top level, resolve all NAME references
2374 // in the resultant defs that weren't in the def names themselves.
2375 RecordVal *DefNameRV = CurRec->getValue("NAME");
2376 CurRec->resolveReferencesTo(DefNameRV);
2377
Hal Finkeld2497362015-05-21 04:32:56 +00002378 // Check if the name is a complex pattern.
2379 // If so, resolve it.
2380 DefName = CurRec->getNameInit();
2381 DefNameString = dyn_cast<StringInit>(DefName);
2382
2383 // OK the pattern is more complex than simply using NAME.
2384 // Let's use the heavy weaponery.
2385 if (!DefNameString) {
2386 ResolveMulticlassDefArgs(MC, CurRec.get(), DefmPrefixRange.Start,
2387 Lex.getLoc(), TArgs, TemplateVals,
2388 false/*Delete args*/);
2389 DefName = CurRec->getNameInit();
2390 DefNameString = dyn_cast<StringInit>(DefName);
2391
2392 if (!DefNameString)
2393 DefName = DefName->convertInitializerTo(StringRecTy::get());
2394
2395 // We ran out of options here...
2396 DefNameString = dyn_cast<StringInit>(DefName);
2397 if (!DefNameString) {
2398 PrintFatalError(CurRec->getLoc()[CurRec->getLoc().size() - 1],
2399 DefName->getAsUnquotedString() + " is not a string.");
2400 return nullptr;
2401 }
2402
2403 CurRec->setName(DefName);
2404 }
2405
Jim Grosbachbc5b61c2012-08-02 18:46:42 +00002406 // Now that NAME references are resolved and we're at the top level of
2407 // any multiclass expansions, add the record to the RecordKeeper. If we are
David Greene8bf0d722011-10-19 13:04:35 +00002408 // currently in a multiclass, it means this defm appears inside a
2409 // multiclass and its name won't be fully resolvable until we see
Hal Finkeld2497362015-05-21 04:32:56 +00002410 // the top-level defm. Therefore, we don't add this to the
2411 // RecordKeeper at this point. If we did we could get duplicate
David Greene8bf0d722011-10-19 13:04:35 +00002412 // defs as more than one probably refers to NAME or some other
2413 // common internal placeholder.
2414
2415 // Ensure redefinition doesn't happen.
2416 if (Records.getDef(CurRec->getNameInitAsString())) {
Jordan Rosef12e8a92013-01-10 18:50:11 +00002417 Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
David Greene8bf0d722011-10-19 13:04:35 +00002418 "' already defined, instantiating defm with subdef '" +
2419 DefProto->getNameInitAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00002420 return nullptr;
David Greene8bf0d722011-10-19 13:04:35 +00002421 }
2422
Craig Topper84138712014-11-29 05:31:10 +00002423 Record *CurRecSave = CurRec.get(); // Keep a copy before we release.
Craig Toppercdab2322014-11-29 05:52:51 +00002424 Records.addDef(std::move(CurRec));
Craig Topper84138712014-11-29 05:31:10 +00002425 return CurRecSave;
David Greene8bf0d722011-10-19 13:04:35 +00002426 }
2427
Craig Topper84138712014-11-29 05:31:10 +00002428 // FIXME This is bad but the ownership transfer to caller is pretty messy.
2429 // The unique_ptr in this function at least protects the exits above.
2430 return CurRec.release();
David Greenedb445972011-10-05 22:42:07 +00002431}
2432
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002433bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC, Record *CurRec,
2434 SMLoc DefmPrefixLoc, SMLoc SubClassLoc,
2435 ArrayRef<Init *> TArgs,
David Greenedb445972011-10-05 22:42:07 +00002436 std::vector<Init *> &TemplateVals,
2437 bool DeleteArgs) {
2438 // Loop over all of the template arguments, setting them to the specified
2439 // value or leaving them as the default if necessary.
2440 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2441 // Check if a value is specified for this temp-arg.
2442 if (i < TemplateVals.size()) {
2443 // Set it now.
Craig Toppercfd81732016-01-04 03:15:08 +00002444 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], None, TemplateVals[i]))
David Greenedb445972011-10-05 22:42:07 +00002445 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002446
David Greenedb445972011-10-05 22:42:07 +00002447 // Resolve it next.
2448 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2449
2450 if (DeleteArgs)
2451 // Now remove it.
2452 CurRec->removeValue(TArgs[i]);
Craig Toppera9642b42015-05-04 01:35:39 +00002453
David Greenedb445972011-10-05 22:42:07 +00002454 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Craig Topper85c07002015-04-30 05:54:22 +00002455 return Error(SubClassLoc, "value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00002456 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +00002457 ") of multiclassclass '" + MC.Rec.getNameInitAsString() +
2458 "'");
David Greenedb445972011-10-05 22:42:07 +00002459 }
2460 }
2461 return false;
2462}
2463
2464bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2465 Record *CurRec,
2466 Record *DefProto,
2467 SMLoc DefmPrefixLoc) {
2468 // If the mdef is inside a 'let' expression, add to each def.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002469 if (ApplyLetStack(CurRec))
2470 return Error(DefmPrefixLoc, "when instantiating this defm");
David Greenedb445972011-10-05 22:42:07 +00002471
David Greenedb445972011-10-05 22:42:07 +00002472 // Don't create a top level definition for defm inside multiclasses,
2473 // instead, only update the prototypes and bind the template args
2474 // with the new created definition.
Sean Silvacc951b22013-01-09 05:28:12 +00002475 if (!CurMultiClass)
2476 return false;
Craig Toppereb4d7c62015-04-29 04:43:36 +00002477 for (const auto &Proto : CurMultiClass->DefPrototypes)
2478 if (Proto->getNameInit() == CurRec->getNameInit())
Sean Silvacc951b22013-01-09 05:28:12 +00002479 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2480 "' already defined in this multiclass!");
Craig Topperc3504c42014-12-11 05:25:33 +00002481 CurMultiClass->DefPrototypes.push_back(std::unique_ptr<Record>(CurRec));
David Greenedb445972011-10-05 22:42:07 +00002482
Sean Silvacc951b22013-01-09 05:28:12 +00002483 // Copy the template arguments for the multiclass into the new def.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002484 for (Init * TA : CurMultiClass->Rec.getTemplateArgs()) {
2485 const RecordVal *RV = CurMultiClass->Rec.getValue(TA);
Sean Silvacc951b22013-01-09 05:28:12 +00002486 assert(RV && "Template arg doesn't exist?");
2487 CurRec->addValue(*RV);
David Greenedb445972011-10-05 22:42:07 +00002488 }
2489
2490 return false;
2491}
2492
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002493/// ParseDefm - Parse the instantiation of a multiclass.
2494///
2495/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2496///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002497bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002498 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Jordan Rosef12e8a92013-01-10 18:50:11 +00002499 SMLoc DefmLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002500 Init *DefmPrefix = nullptr;
David Greene2affd672011-10-19 13:04:29 +00002501
Craig Topperb21afc62013-01-07 05:09:33 +00002502 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greene2affd672011-10-19 13:04:29 +00002503 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattner7538ed82010-10-05 22:51:56 +00002504 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002505
Jordan Rosef12e8a92013-01-10 18:50:11 +00002506 SMLoc DefmPrefixEndLoc = Lex.getLoc();
Chris Lattner7538ed82010-10-05 22:51:56 +00002507 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002508 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002509
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002510 // Keep track of the new generated record definitions.
2511 std::vector<Record*> NewRecDefs;
2512
2513 // This record also inherits from a regular class (non-multiclass)?
2514 bool InheritFromClass = false;
2515
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002516 // eat the colon.
2517 Lex.Lex();
2518
Chris Lattner526c8cb2009-06-21 03:39:35 +00002519 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002520 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002521
2522 while (1) {
Craig Topper011817a2014-04-09 04:50:04 +00002523 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002524
2525 // To instantiate a multiclass, we need to first get the multiclass, then
2526 // instantiate each def contained in the multiclass with the SubClassRef
2527 // template parameters.
Craig Topper7adf2bf2014-12-11 05:25:30 +00002528 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper4ca40012014-11-30 01:20:17 +00002529 assert(MC && "Didn't lookup multiclass correctly?");
David Greeneaf8ee2c2011-07-29 22:43:06 +00002530 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002531
2532 // Verify that the correct number of template arguments were specified.
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002533 ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002534 if (TArgs.size() < TemplateVals.size())
2535 return Error(SubClassLoc,
2536 "more template args specified than multiclass expects");
2537
2538 // Loop over all the def's in the multiclass, instantiating each one.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002539 for (const std::unique_ptr<Record> &DefProto : MC->DefPrototypes) {
Hal Finkeld2497362015-05-21 04:32:56 +00002540 // The record name construction goes as follow:
2541 // - If the def name is a string, prepend the prefix.
2542 // - If the def name is a more complex pattern, use that pattern.
Craig Topper7f36be92016-02-26 06:50:27 +00002543 // As a result, the record is instantiated before resolving
Hal Finkeld2497362015-05-21 04:32:56 +00002544 // arguments, as it would make its name a string.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002545 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto.get(), DefmPrefix,
Jordan Rosef12e8a92013-01-10 18:50:11 +00002546 SMRange(DefmLoc,
Hal Finkeld2497362015-05-21 04:32:56 +00002547 DefmPrefixEndLoc),
2548 TArgs, TemplateVals);
Jim Grosbachbccc4c12011-12-02 18:33:03 +00002549 if (!CurRec)
2550 return true;
David Greene44f9d7a2009-05-05 16:28:25 +00002551
Craig Topper7f36be92016-02-26 06:50:27 +00002552 // Now that the record is instantiated, we can resolve arguments.
Jordan Rosef12e8a92013-01-10 18:50:11 +00002553 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
David Greenedb445972011-10-05 22:42:07 +00002554 TArgs, TemplateVals, true/*Delete args*/))
2555 return Error(SubClassLoc, "could not instantiate def");
David Greenef00919a2009-04-22 22:17:51 +00002556
Craig Toppereb4d7c62015-04-29 04:43:36 +00002557 if (ResolveMulticlassDef(*MC, CurRec, DefProto.get(), DefmLoc))
David Greenedb445972011-10-05 22:42:07 +00002558 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002559
Adam Nemete5a07162014-09-16 17:14:13 +00002560 // Defs that can be used by other definitions should be fully resolved
2561 // before any use.
2562 if (DefProto->isResolveFirst() && !CurMultiClass) {
2563 CurRec->resolveReferences();
2564 CurRec->setResolveFirst(false);
2565 }
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002566 NewRecDefs.push_back(CurRec);
David Greenef00919a2009-04-22 22:17:51 +00002567 }
2568
David Greenedb445972011-10-05 22:42:07 +00002569
David Greenef00919a2009-04-22 22:17:51 +00002570 if (Lex.getCode() != tgtok::comma) break;
2571 Lex.Lex(); // eat ','.
2572
Craig Topper998a39a2013-08-20 04:22:09 +00002573 if (Lex.getCode() != tgtok::Id)
2574 return TokError("expected identifier");
2575
David Greenef00919a2009-04-22 22:17:51 +00002576 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002577
2578 // A defm can inherit from regular classes (non-multiclass) as
2579 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002580 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002581
2582 if (InheritFromClass)
2583 break;
2584
Craig Topper011817a2014-04-09 04:50:04 +00002585 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002586 }
2587
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002588 if (InheritFromClass) {
2589 // Process all the classes to inherit as if they were part of a
2590 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002591 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002592 while (1) {
2593 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002594 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002595
2596 // Get the expanded definition prototypes and teach them about
2597 // the record values the current class to inherit has
Craig Toppereb4d7c62015-04-29 04:43:36 +00002598 for (Record *CurRec : NewRecDefs) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002599 // Add it.
2600 if (AddSubClass(CurRec, SubClass))
2601 return true;
2602
Sean Silvacb1a75e2013-01-09 04:49:14 +00002603 if (ApplyLetStack(CurRec))
2604 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002605 }
2606
2607 if (Lex.getCode() != tgtok::comma) break;
2608 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002609 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002610 }
2611 }
2612
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002613 if (!CurMultiClass)
Craig Toppereb4d7c62015-04-29 04:43:36 +00002614 for (Record *CurRec : NewRecDefs)
David Greene50c09122011-08-10 18:27:46 +00002615 // See Record::setName(). This resolve step will see any new
2616 // name for the def that might have been created when resolving
2617 // inheritance, values and arguments above.
Craig Toppereb4d7c62015-04-29 04:43:36 +00002618 CurRec->resolveReferences();
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002619
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002620 if (Lex.getCode() != tgtok::semi)
2621 return TokError("expected ';' at end of defm");
2622 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002623
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002624 return false;
2625}
2626
2627/// ParseObject
2628/// Object ::= ClassInst
2629/// Object ::= DefInst
2630/// Object ::= MultiClassInst
2631/// Object ::= DefMInst
2632/// Object ::= LETCommand '{' ObjectList '}'
2633/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002634bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002635 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002636 default:
2637 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002638 case tgtok::Let: return ParseTopLevelLet(MC);
2639 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002640 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002641 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002642 case tgtok::Class: return ParseClass();
2643 case tgtok::MultiClass: return ParseMultiClass();
2644 }
2645}
2646
2647/// ParseObjectList
2648/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002649bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002650 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002651 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002652 return true;
2653 }
2654 return false;
2655}
2656
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002657bool TGParser::ParseFile() {
2658 Lex.Lex(); // Prime the lexer.
2659 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002660
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002661 // If we have unread input at the end of the file, report it.
2662 if (Lex.getCode() == tgtok::Eof)
2663 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002664
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002665 return TokError("Unexpected input at top level");
2666}
2667