blob: 0f64f4bcdac33c34698ffff37722ab2fd26282d5 [file] [log] [blame]
Chris Lattnerf4601652007-11-22 20:49:04 +00001//===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-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 Lattnerf4601652007-11-22 20:49:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Parser for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
14#include "TGParser.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000015#include "llvm/TableGen/Record.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000016#include "llvm/ADT/StringExtras.h"
Daniel Dunbar1a551802009-07-03 00:10:29 +000017#include <algorithm>
18#include <sstream>
Chris Lattner8d978a72010-10-05 23:58:18 +000019#include "llvm/ADT/SmallVector.h"
Chris Lattner46f55522010-10-06 04:55:48 +000020#include "llvm/Support/CommandLine.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000021using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24// Support Code for the Semantic Actions.
25//===----------------------------------------------------------------------===//
26
27namespace llvm {
Chris Lattnerf4601652007-11-22 20:49:04 +000028struct SubClassReference {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000029 SMLoc RefLoc;
Chris Lattnerf4601652007-11-22 20:49:04 +000030 Record *Rec;
David Greene05bce0b2011-07-29 22:43:06 +000031 std::vector<Init*> TemplateArgs;
Chris Lattner1c8ae592009-03-13 16:01:53 +000032 SubClassReference() : Rec(0) {}
David Greened34a73b2009-04-24 16:55:41 +000033
Chris Lattnerf4601652007-11-22 20:49:04 +000034 bool isInvalid() const { return Rec == 0; }
35};
David Greenede444af2009-04-22 16:42:54 +000036
37struct SubMultiClassReference {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000038 SMLoc RefLoc;
David Greenede444af2009-04-22 16:42:54 +000039 MultiClass *MC;
David Greene05bce0b2011-07-29 22:43:06 +000040 std::vector<Init*> TemplateArgs;
David Greenede444af2009-04-22 16:42:54 +000041 SubMultiClassReference() : MC(0) {}
Bob Wilson32558652009-04-28 19:41:44 +000042
David Greenede444af2009-04-22 16:42:54 +000043 bool isInvalid() const { return MC == 0; }
David Greened34a73b2009-04-24 16:55:41 +000044 void dump() const;
David Greenede444af2009-04-22 16:42:54 +000045};
David Greened34a73b2009-04-24 16:55:41 +000046
47void SubMultiClassReference::dump() const {
Daniel Dunbar1a551802009-07-03 00:10:29 +000048 errs() << "Multiclass:\n";
Bob Wilson21870412009-11-22 04:24:42 +000049
David Greened34a73b2009-04-24 16:55:41 +000050 MC->dump();
Bob Wilson21870412009-11-22 04:24:42 +000051
Daniel Dunbar1a551802009-07-03 00:10:29 +000052 errs() << "Template args:\n";
David Greene05bce0b2011-07-29 22:43:06 +000053 for (std::vector<Init *>::const_iterator i = TemplateArgs.begin(),
David Greened34a73b2009-04-24 16:55:41 +000054 iend = TemplateArgs.end();
55 i != iend;
56 ++i) {
57 (*i)->dump();
58 }
59}
60
Chris Lattnerf4601652007-11-22 20:49:04 +000061} // end namespace llvm
62
Chris Lattner1e3a8a42009-06-21 03:39:35 +000063bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Chris Lattnerf4601652007-11-22 20:49:04 +000064 if (CurRec == 0)
65 CurRec = &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +000066
Jakob Stoklund Olesenebaf92c2012-01-13 03:16:35 +000067 if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
Chris Lattnerf4601652007-11-22 20:49:04 +000068 // The value already exists in the class, treat this as a set.
69 if (ERV->setValue(RV.getValue()))
70 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
71 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson21870412009-11-22 04:24:42 +000072 "previous definition of type '" +
Chris Lattnerf4601652007-11-22 20:49:04 +000073 ERV->getType()->getAsString() + "'");
74 } else {
75 CurRec->addValue(RV);
76 }
77 return false;
78}
79
80/// SetValue -
81/// Return true on error, false on success.
David Greene917924d2011-10-19 13:02:39 +000082bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
David Greene05bce0b2011-07-29 22:43:06 +000083 const std::vector<unsigned> &BitList, Init *V) {
Chris Lattnerf4601652007-11-22 20:49:04 +000084 if (!V) return false;
85
86 if (CurRec == 0) CurRec = &CurMultiClass->Rec;
87
88 RecordVal *RV = CurRec->getValue(ValName);
89 if (RV == 0)
David Greene917924d2011-10-19 13:02:39 +000090 return Error(Loc, "Value '" + ValName->getAsUnquotedString()
91 + "' unknown!");
Chris Lattnerf4601652007-11-22 20:49:04 +000092
93 // Do not allow assignments like 'X = X'. This will just cause infinite loops
94 // in the resolution machinery.
95 if (BitList.empty())
David Greene05bce0b2011-07-29 22:43:06 +000096 if (VarInit *VI = dynamic_cast<VarInit*>(V))
David Greene917924d2011-10-19 13:02:39 +000097 if (VI->getNameInit() == ValName)
Chris Lattnerf4601652007-11-22 20:49:04 +000098 return false;
Bob Wilson21870412009-11-22 04:24:42 +000099
Chris Lattnerf4601652007-11-22 20:49:04 +0000100 // If we are assigning to a subset of the bits in the value... then we must be
101 // assigning to a field of BitsRecTy, which must have a BitsInit
102 // initializer.
103 //
104 if (!BitList.empty()) {
David Greene05bce0b2011-07-29 22:43:06 +0000105 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
Chris Lattnerf4601652007-11-22 20:49:04 +0000106 if (CurVal == 0)
David Greene917924d2011-10-19 13:02:39 +0000107 return Error(Loc, "Value '" + ValName->getAsUnquotedString()
108 + "' is not a bits type");
Chris Lattnerf4601652007-11-22 20:49:04 +0000109
110 // Convert the incoming value to a bits type of the appropriate size...
David Greene05bce0b2011-07-29 22:43:06 +0000111 Init *BI = V->convertInitializerTo(BitsRecTy::get(BitList.size()));
Chris Lattnerf4601652007-11-22 20:49:04 +0000112 if (BI == 0) {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000113 V->convertInitializerTo(BitsRecTy::get(BitList.size()));
Chris Lattnerf4601652007-11-22 20:49:04 +0000114 return Error(Loc, "Initializer is not compatible with bit range");
115 }
Bob Wilson21870412009-11-22 04:24:42 +0000116
Chris Lattnerf4601652007-11-22 20:49:04 +0000117 // We should have a BitsInit type now.
David Greene05bce0b2011-07-29 22:43:06 +0000118 BitsInit *BInit = dynamic_cast<BitsInit*>(BI);
Chris Lattnerf4601652007-11-22 20:49:04 +0000119 assert(BInit != 0);
120
David Greene05bce0b2011-07-29 22:43:06 +0000121 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4601652007-11-22 20:49:04 +0000122
123 // Loop over bits, assigning values as appropriate.
124 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
125 unsigned Bit = BitList[i];
David Greeneca7fd3d2011-07-29 19:07:00 +0000126 if (NewBits[Bit])
Chris Lattnerf4601652007-11-22 20:49:04 +0000127 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
David Greene917924d2011-10-19 13:02:39 +0000128 ValName->getAsUnquotedString() + "' more than once");
David Greeneca7fd3d2011-07-29 19:07:00 +0000129 NewBits[Bit] = BInit->getBit(i);
Chris Lattnerf4601652007-11-22 20:49:04 +0000130 }
131
132 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
David Greeneca7fd3d2011-07-29 19:07:00 +0000133 if (NewBits[i] == 0)
134 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4601652007-11-22 20:49:04 +0000135
David Greenedcd35c72011-07-29 19:07:07 +0000136 V = BitsInit::get(NewBits);
Chris Lattnerf4601652007-11-22 20:49:04 +0000137 }
138
139 if (RV->setValue(V))
David Greene917924d2011-10-19 13:02:39 +0000140 return Error(Loc, "Value '" + ValName->getAsUnquotedString() + "' of type '"
141 + RV->getType()->getAsString() +
142 "' is incompatible with initializer '" + V->getAsString()
143 + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +0000144 return false;
145}
146
147/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
148/// args as SubClass's template arguments.
Cedric Venetaff9c272009-02-14 16:06:42 +0000149bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000150 Record *SC = SubClass.Rec;
151 // Add all of the values in the subclass into the current class.
152 const std::vector<RecordVal> &Vals = SC->getValues();
153 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
154 if (AddValue(CurRec, SubClass.RefLoc, Vals[i]))
155 return true;
156
David Greenee22b3212011-10-19 13:02:42 +0000157 const std::vector<Init *> &TArgs = SC->getTemplateArgs();
Chris Lattnerf4601652007-11-22 20:49:04 +0000158
159 // Ensure that an appropriate number of template arguments are specified.
160 if (TArgs.size() < SubClass.TemplateArgs.size())
161 return Error(SubClass.RefLoc, "More template args specified than expected");
Bob Wilson21870412009-11-22 04:24:42 +0000162
Chris Lattnerf4601652007-11-22 20:49:04 +0000163 // Loop over all of the template arguments, setting them to the specified
164 // value or leaving them as the default if necessary.
165 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
166 if (i < SubClass.TemplateArgs.size()) {
167 // If a value is specified for this template arg, set it now.
Bob Wilson21870412009-11-22 04:24:42 +0000168 if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(),
Chris Lattnerf4601652007-11-22 20:49:04 +0000169 SubClass.TemplateArgs[i]))
170 return true;
Bob Wilson21870412009-11-22 04:24:42 +0000171
Chris Lattnerf4601652007-11-22 20:49:04 +0000172 // Resolve it next.
173 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson21870412009-11-22 04:24:42 +0000174
Chris Lattnerf4601652007-11-22 20:49:04 +0000175 // Now remove it.
176 CurRec->removeValue(TArgs[i]);
177
178 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
179 return Error(SubClass.RefLoc,"Value not specified for template argument #"
David Greenee22b3212011-10-19 13:02:42 +0000180 + utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
181 + ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4601652007-11-22 20:49:04 +0000182 }
183 }
184
185 // Since everything went well, we can now set the "superclass" list for the
186 // current record.
187 const std::vector<Record*> &SCs = SC->getSuperClasses();
188 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
189 if (CurRec->isSubClassOf(SCs[i]))
190 return Error(SubClass.RefLoc,
191 "Already subclass of '" + SCs[i]->getName() + "'!\n");
192 CurRec->addSuperClass(SCs[i]);
193 }
Bob Wilson21870412009-11-22 04:24:42 +0000194
Chris Lattnerf4601652007-11-22 20:49:04 +0000195 if (CurRec->isSubClassOf(SC))
196 return Error(SubClass.RefLoc,
197 "Already subclass of '" + SC->getName() + "'!\n");
198 CurRec->addSuperClass(SC);
199 return false;
200}
201
David Greenede444af2009-04-22 16:42:54 +0000202/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilson440548d2009-04-30 18:26:19 +0000203/// CurMC, resolving its template args as SubMultiClass's
David Greenede444af2009-04-22 16:42:54 +0000204/// template arguments.
Bob Wilson440548d2009-04-30 18:26:19 +0000205bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson1d512df2009-04-30 17:46:20 +0000206 SubMultiClassReference &SubMultiClass) {
David Greenede444af2009-04-22 16:42:54 +0000207 MultiClass *SMC = SubMultiClass.MC;
Bob Wilson440548d2009-04-30 18:26:19 +0000208 Record *CurRec = &CurMC->Rec;
David Greenede444af2009-04-22 16:42:54 +0000209
Bob Wilson440548d2009-04-30 18:26:19 +0000210 const std::vector<RecordVal> &MCVals = CurRec->getValues();
David Greenede444af2009-04-22 16:42:54 +0000211
212 // Add all of the values in the subclass into the current class.
213 const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues();
214 for (unsigned i = 0, e = SMCVals.size(); i != e; ++i)
215 if (AddValue(CurRec, SubMultiClass.RefLoc, SMCVals[i]))
216 return true;
217
Bob Wilson440548d2009-04-30 18:26:19 +0000218 int newDefStart = CurMC->DefPrototypes.size();
David Greened34a73b2009-04-24 16:55:41 +0000219
David Greenede444af2009-04-22 16:42:54 +0000220 // Add all of the defs in the subclass into the current multiclass.
221 for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(),
222 iend = SMC->DefPrototypes.end();
223 i != iend;
224 ++i) {
225 // Clone the def and add it to the current multiclass
226 Record *NewDef = new Record(**i);
227
228 // Add all of the values in the superclass into the current def.
229 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
230 if (AddValue(NewDef, SubMultiClass.RefLoc, MCVals[i]))
231 return true;
232
Bob Wilson440548d2009-04-30 18:26:19 +0000233 CurMC->DefPrototypes.push_back(NewDef);
David Greenede444af2009-04-22 16:42:54 +0000234 }
Bob Wilson32558652009-04-28 19:41:44 +0000235
David Greenee22b3212011-10-19 13:02:42 +0000236 const std::vector<Init *> &SMCTArgs = SMC->Rec.getTemplateArgs();
David Greenede444af2009-04-22 16:42:54 +0000237
David Greened34a73b2009-04-24 16:55:41 +0000238 // Ensure that an appropriate number of template arguments are
239 // specified.
David Greenede444af2009-04-22 16:42:54 +0000240 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
David Greened34a73b2009-04-24 16:55:41 +0000241 return Error(SubMultiClass.RefLoc,
242 "More template args specified than expected");
Bob Wilson32558652009-04-28 19:41:44 +0000243
David Greenede444af2009-04-22 16:42:54 +0000244 // Loop over all of the template arguments, setting them to the specified
245 // value or leaving them as the default if necessary.
246 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
247 if (i < SubMultiClass.TemplateArgs.size()) {
David Greened34a73b2009-04-24 16:55:41 +0000248 // If a value is specified for this template arg, set it in the
249 // superclass now.
250 if (SetValue(CurRec, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000251 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000252 SubMultiClass.TemplateArgs[i]))
253 return true;
254
255 // Resolve it next.
256 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson32558652009-04-28 19:41:44 +0000257
David Greenede444af2009-04-22 16:42:54 +0000258 // Now remove it.
259 CurRec->removeValue(SMCTArgs[i]);
260
David Greened34a73b2009-04-24 16:55:41 +0000261 // If a value is specified for this template arg, set it in the
262 // new defs now.
263 for (MultiClass::RecordVector::iterator j =
Bob Wilson440548d2009-04-30 18:26:19 +0000264 CurMC->DefPrototypes.begin() + newDefStart,
265 jend = CurMC->DefPrototypes.end();
David Greenede444af2009-04-22 16:42:54 +0000266 j != jend;
267 ++j) {
268 Record *Def = *j;
269
David Greened34a73b2009-04-24 16:55:41 +0000270 if (SetValue(Def, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000271 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000272 SubMultiClass.TemplateArgs[i]))
273 return true;
274
275 // Resolve it next.
276 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
277
278 // Now remove it
279 Def->removeValue(SMCTArgs[i]);
280 }
281 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
David Greened34a73b2009-04-24 16:55:41 +0000282 return Error(SubMultiClass.RefLoc,
283 "Value not specified for template argument #"
David Greenee22b3212011-10-19 13:02:42 +0000284 + utostr(i) + " (" + SMCTArgs[i]->getAsUnquotedString()
285 + ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greenede444af2009-04-22 16:42:54 +0000286 }
287 }
288
289 return false;
290}
291
Chris Lattnerf4601652007-11-22 20:49:04 +0000292//===----------------------------------------------------------------------===//
293// Parser Code
294//===----------------------------------------------------------------------===//
295
296/// isObjectStart - Return true if this is a valid first token for an Object.
297static bool isObjectStart(tgtok::TokKind K) {
298 return K == tgtok::Class || K == tgtok::Def ||
Bob Wilson21870412009-11-22 04:24:42 +0000299 K == tgtok::Defm || K == tgtok::Let || K == tgtok::MultiClass;
Chris Lattnerf4601652007-11-22 20:49:04 +0000300}
301
Chris Lattnerdf72eae2010-10-05 22:51:56 +0000302static std::string GetNewAnonymousName() {
303 static unsigned AnonCounter = 0;
304 return "anonymous."+utostr(AnonCounter++);
305}
306
Chris Lattnerf4601652007-11-22 20:49:04 +0000307/// ParseObjectName - If an object name is specified, return it. Otherwise,
308/// return an anonymous name.
David Greenea9e07dd2011-10-19 13:04:29 +0000309/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4601652007-11-22 20:49:04 +0000310/// ObjectName ::= /*empty*/
311///
David Greenea9e07dd2011-10-19 13:04:29 +0000312Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
313 switch (Lex.getCode()) {
314 case tgtok::colon:
315 case tgtok::semi:
316 case tgtok::l_brace:
317 // These are all of the tokens that can begin an object body.
318 // Some of these can also begin values but we disallow those cases
319 // because they are unlikely to be useful.
320 return StringInit::get(GetNewAnonymousName());
David Greenea9e07dd2011-10-19 13:04:29 +0000321 default:
322 break;
323 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000324
David Greenea9e07dd2011-10-19 13:04:29 +0000325 Record *CurRec = 0;
326 if (CurMultiClass)
327 CurRec = &CurMultiClass->Rec;
328
329 RecTy *Type = 0;
330 if (CurRec) {
331 const TypedInit *CurRecName =
332 dynamic_cast<const TypedInit *>(CurRec->getNameInit());
333 if (!CurRecName) {
334 TokError("Record name is not typed!");
335 return 0;
336 }
337 Type = CurRecName->getType();
338 }
339
340 return ParseValue(CurRec, Type, ParseNameMode);
Chris Lattnerf4601652007-11-22 20:49:04 +0000341}
342
Chris Lattnerf4601652007-11-22 20:49:04 +0000343/// ParseClassID - Parse and resolve a reference to a class name. This returns
344/// null on error.
345///
346/// ClassID ::= ID
347///
348Record *TGParser::ParseClassID() {
349 if (Lex.getCode() != tgtok::Id) {
350 TokError("expected name for ClassID");
351 return 0;
352 }
Bob Wilson21870412009-11-22 04:24:42 +0000353
Chris Lattnerf4601652007-11-22 20:49:04 +0000354 Record *Result = Records.getClass(Lex.getCurStrVal());
355 if (Result == 0)
356 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson21870412009-11-22 04:24:42 +0000357
Chris Lattnerf4601652007-11-22 20:49:04 +0000358 Lex.Lex();
359 return Result;
360}
361
Bob Wilson32558652009-04-28 19:41:44 +0000362/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
363/// This returns null on error.
David Greenede444af2009-04-22 16:42:54 +0000364///
365/// MultiClassID ::= ID
366///
367MultiClass *TGParser::ParseMultiClassID() {
368 if (Lex.getCode() != tgtok::Id) {
369 TokError("expected name for ClassID");
370 return 0;
371 }
Bob Wilson32558652009-04-28 19:41:44 +0000372
David Greenede444af2009-04-22 16:42:54 +0000373 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
374 if (Result == 0)
375 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson32558652009-04-28 19:41:44 +0000376
David Greenede444af2009-04-22 16:42:54 +0000377 Lex.Lex();
378 return Result;
379}
380
Chris Lattnerf4601652007-11-22 20:49:04 +0000381Record *TGParser::ParseDefmID() {
382 if (Lex.getCode() != tgtok::Id) {
383 TokError("expected multiclass name");
384 return 0;
385 }
Bob Wilson21870412009-11-22 04:24:42 +0000386
Chris Lattnerf4601652007-11-22 20:49:04 +0000387 MultiClass *MC = MultiClasses[Lex.getCurStrVal()];
388 if (MC == 0) {
389 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
390 return 0;
391 }
Bob Wilson21870412009-11-22 04:24:42 +0000392
Chris Lattnerf4601652007-11-22 20:49:04 +0000393 Lex.Lex();
394 return &MC->Rec;
Bob Wilson21870412009-11-22 04:24:42 +0000395}
Chris Lattnerf4601652007-11-22 20:49:04 +0000396
397
398/// ParseSubClassReference - Parse a reference to a subclass or to a templated
399/// subclass. This returns a SubClassRefTy with a null Record* on error.
400///
401/// SubClassRef ::= ClassID
402/// SubClassRef ::= ClassID '<' ValueList '>'
403///
404SubClassReference TGParser::
405ParseSubClassReference(Record *CurRec, bool isDefm) {
406 SubClassReference Result;
407 Result.RefLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +0000408
Chris Lattnerf4601652007-11-22 20:49:04 +0000409 if (isDefm)
410 Result.Rec = ParseDefmID();
411 else
412 Result.Rec = ParseClassID();
413 if (Result.Rec == 0) return Result;
Bob Wilson21870412009-11-22 04:24:42 +0000414
Chris Lattnerf4601652007-11-22 20:49:04 +0000415 // If there is no template arg list, we're done.
416 if (Lex.getCode() != tgtok::less)
417 return Result;
418 Lex.Lex(); // Eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000419
Chris Lattnerf4601652007-11-22 20:49:04 +0000420 if (Lex.getCode() == tgtok::greater) {
421 TokError("subclass reference requires a non-empty list of template values");
422 Result.Rec = 0;
423 return Result;
424 }
Bob Wilson21870412009-11-22 04:24:42 +0000425
David Greenee1b46912009-06-08 20:23:18 +0000426 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4601652007-11-22 20:49:04 +0000427 if (Result.TemplateArgs.empty()) {
428 Result.Rec = 0; // Error parsing value list.
429 return Result;
430 }
Bob Wilson21870412009-11-22 04:24:42 +0000431
Chris Lattnerf4601652007-11-22 20:49:04 +0000432 if (Lex.getCode() != tgtok::greater) {
433 TokError("expected '>' in template value list");
434 Result.Rec = 0;
435 return Result;
436 }
437 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000438
Chris Lattnerf4601652007-11-22 20:49:04 +0000439 return Result;
440}
441
Bob Wilson32558652009-04-28 19:41:44 +0000442/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
443/// templated submulticlass. This returns a SubMultiClassRefTy with a null
444/// Record* on error.
David Greenede444af2009-04-22 16:42:54 +0000445///
446/// SubMultiClassRef ::= MultiClassID
447/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
448///
449SubMultiClassReference TGParser::
450ParseSubMultiClassReference(MultiClass *CurMC) {
451 SubMultiClassReference Result;
452 Result.RefLoc = Lex.getLoc();
Bob Wilson32558652009-04-28 19:41:44 +0000453
David Greenede444af2009-04-22 16:42:54 +0000454 Result.MC = ParseMultiClassID();
455 if (Result.MC == 0) return Result;
Bob Wilson32558652009-04-28 19:41:44 +0000456
David Greenede444af2009-04-22 16:42:54 +0000457 // If there is no template arg list, we're done.
458 if (Lex.getCode() != tgtok::less)
459 return Result;
460 Lex.Lex(); // Eat the '<'
Bob Wilson32558652009-04-28 19:41:44 +0000461
David Greenede444af2009-04-22 16:42:54 +0000462 if (Lex.getCode() == tgtok::greater) {
463 TokError("subclass reference requires a non-empty list of template values");
464 Result.MC = 0;
465 return Result;
466 }
Bob Wilson32558652009-04-28 19:41:44 +0000467
David Greenee1b46912009-06-08 20:23:18 +0000468 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greenede444af2009-04-22 16:42:54 +0000469 if (Result.TemplateArgs.empty()) {
470 Result.MC = 0; // Error parsing value list.
471 return Result;
472 }
Bob Wilson32558652009-04-28 19:41:44 +0000473
David Greenede444af2009-04-22 16:42:54 +0000474 if (Lex.getCode() != tgtok::greater) {
475 TokError("expected '>' in template value list");
476 Result.MC = 0;
477 return Result;
478 }
479 Lex.Lex();
480
481 return Result;
482}
483
Chris Lattnerf4601652007-11-22 20:49:04 +0000484/// ParseRangePiece - Parse a bit/value range.
485/// RangePiece ::= INTVAL
486/// RangePiece ::= INTVAL '-' INTVAL
487/// RangePiece ::= INTVAL INTVAL
488bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner811281e2008-01-10 07:01:53 +0000489 if (Lex.getCode() != tgtok::IntVal) {
490 TokError("expected integer or bitrange");
491 return true;
492 }
Dan Gohman63f97202008-10-17 01:33:43 +0000493 int64_t Start = Lex.getCurIntVal();
494 int64_t End;
Bob Wilson21870412009-11-22 04:24:42 +0000495
Chris Lattnerf4601652007-11-22 20:49:04 +0000496 if (Start < 0)
497 return TokError("invalid range, cannot be negative");
Bob Wilson21870412009-11-22 04:24:42 +0000498
Chris Lattnerf4601652007-11-22 20:49:04 +0000499 switch (Lex.Lex()) { // eat first character.
Bob Wilson21870412009-11-22 04:24:42 +0000500 default:
Chris Lattnerf4601652007-11-22 20:49:04 +0000501 Ranges.push_back(Start);
502 return false;
503 case tgtok::minus:
504 if (Lex.Lex() != tgtok::IntVal) {
505 TokError("expected integer value as end of range");
506 return true;
507 }
508 End = Lex.getCurIntVal();
509 break;
510 case tgtok::IntVal:
511 End = -Lex.getCurIntVal();
512 break;
513 }
Bob Wilson21870412009-11-22 04:24:42 +0000514 if (End < 0)
Chris Lattnerf4601652007-11-22 20:49:04 +0000515 return TokError("invalid range, cannot be negative");
516 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000517
Chris Lattnerf4601652007-11-22 20:49:04 +0000518 // Add to the range.
519 if (Start < End) {
520 for (; Start <= End; ++Start)
521 Ranges.push_back(Start);
522 } else {
523 for (; Start >= End; --Start)
524 Ranges.push_back(Start);
525 }
526 return false;
527}
528
529/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
530///
531/// RangeList ::= RangePiece (',' RangePiece)*
532///
533std::vector<unsigned> TGParser::ParseRangeList() {
534 std::vector<unsigned> Result;
Bob Wilson21870412009-11-22 04:24:42 +0000535
Chris Lattnerf4601652007-11-22 20:49:04 +0000536 // Parse the first piece.
537 if (ParseRangePiece(Result))
538 return std::vector<unsigned>();
539 while (Lex.getCode() == tgtok::comma) {
540 Lex.Lex(); // Eat the comma.
541
542 // Parse the next range piece.
543 if (ParseRangePiece(Result))
544 return std::vector<unsigned>();
545 }
546 return Result;
547}
548
549/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
550/// OptionalRangeList ::= '<' RangeList '>'
551/// OptionalRangeList ::= /*empty*/
552bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
553 if (Lex.getCode() != tgtok::less)
554 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000555
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000556 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000557 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000558
Chris Lattnerf4601652007-11-22 20:49:04 +0000559 // Parse the range list.
560 Ranges = ParseRangeList();
561 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000562
Chris Lattnerf4601652007-11-22 20:49:04 +0000563 if (Lex.getCode() != tgtok::greater) {
564 TokError("expected '>' at end of range list");
565 return Error(StartLoc, "to match this '<'");
566 }
567 Lex.Lex(); // eat the '>'.
568 return false;
569}
570
571/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
572/// OptionalBitList ::= '{' RangeList '}'
573/// OptionalBitList ::= /*empty*/
574bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
575 if (Lex.getCode() != tgtok::l_brace)
576 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000577
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000578 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000579 Lex.Lex(); // eat the '{'
Bob Wilson21870412009-11-22 04:24:42 +0000580
Chris Lattnerf4601652007-11-22 20:49:04 +0000581 // Parse the range list.
582 Ranges = ParseRangeList();
583 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000584
Chris Lattnerf4601652007-11-22 20:49:04 +0000585 if (Lex.getCode() != tgtok::r_brace) {
586 TokError("expected '}' at end of bit list");
587 return Error(StartLoc, "to match this '{'");
588 }
589 Lex.Lex(); // eat the '}'.
590 return false;
591}
592
593
594/// ParseType - Parse and return a tblgen type. This returns null on error.
595///
596/// Type ::= STRING // string type
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +0000597/// Type ::= CODE // code type
Chris Lattnerf4601652007-11-22 20:49:04 +0000598/// Type ::= BIT // bit type
599/// Type ::= BITS '<' INTVAL '>' // bits<x> type
600/// Type ::= INT // int type
601/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4601652007-11-22 20:49:04 +0000602/// Type ::= DAG // dag type
603/// Type ::= ClassID // Record Type
604///
605RecTy *TGParser::ParseType() {
606 switch (Lex.getCode()) {
607 default: TokError("Unknown token when expecting a type"); return 0;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000608 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +0000609 case tgtok::Code: Lex.Lex(); return StringRecTy::get();
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000610 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
611 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000612 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4601652007-11-22 20:49:04 +0000613 case tgtok::Id:
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000614 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Chris Lattnerf4601652007-11-22 20:49:04 +0000615 return 0;
616 case tgtok::Bits: {
617 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
618 TokError("expected '<' after bits type");
619 return 0;
620 }
621 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
622 TokError("expected integer in bits<n> type");
623 return 0;
624 }
Dan Gohman63f97202008-10-17 01:33:43 +0000625 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4601652007-11-22 20:49:04 +0000626 if (Lex.Lex() != tgtok::greater) { // Eat count.
627 TokError("expected '>' at end of bits<n> type");
628 return 0;
629 }
630 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000631 return BitsRecTy::get(Val);
Chris Lattnerf4601652007-11-22 20:49:04 +0000632 }
633 case tgtok::List: {
634 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
635 TokError("expected '<' after list type");
636 return 0;
637 }
638 Lex.Lex(); // Eat '<'
639 RecTy *SubType = ParseType();
640 if (SubType == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +0000641
Chris Lattnerf4601652007-11-22 20:49:04 +0000642 if (Lex.getCode() != tgtok::greater) {
643 TokError("expected '>' at end of list<ty> type");
644 return 0;
645 }
646 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000647 return ListRecTy::get(SubType);
Chris Lattnerf4601652007-11-22 20:49:04 +0000648 }
Bob Wilson21870412009-11-22 04:24:42 +0000649 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000650}
651
652/// ParseIDValue - Parse an ID as a value and decode what it means.
653///
654/// IDValue ::= ID [def local value]
655/// IDValue ::= ID [def template arg]
656/// IDValue ::= ID [multiclass local value]
657/// IDValue ::= ID [multiclass template argument]
658/// IDValue ::= ID [def name]
659///
David Greenef3744a02011-10-19 13:04:20 +0000660Init *TGParser::ParseIDValue(Record *CurRec, IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000661 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
662 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000663 SMLoc Loc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000664 Lex.Lex();
665 return ParseIDValue(CurRec, Name, Loc);
666}
667
668/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
669/// has already been read.
David Greene05bce0b2011-07-29 22:43:06 +0000670Init *TGParser::ParseIDValue(Record *CurRec,
David Greenef3744a02011-10-19 13:04:20 +0000671 const std::string &Name, SMLoc NameLoc,
672 IDParseMode Mode) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000673 if (CurRec) {
674 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenedcd35c72011-07-29 19:07:07 +0000675 return VarInit::get(Name, RV->getType());
Bob Wilson21870412009-11-22 04:24:42 +0000676
David Greenee22b3212011-10-19 13:02:42 +0000677 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
678
David Greenecaa25c82011-10-05 22:42:54 +0000679 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +0000680 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
681 "::");
David Greenecaa25c82011-10-05 22:42:54 +0000682
Chris Lattnerf4601652007-11-22 20:49:04 +0000683 if (CurRec->isTemplateArg(TemplateArgName)) {
684 const RecordVal *RV = CurRec->getValue(TemplateArgName);
685 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000686 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000687 }
688 }
Bob Wilson21870412009-11-22 04:24:42 +0000689
Chris Lattnerf4601652007-11-22 20:49:04 +0000690 if (CurMultiClass) {
David Greenee22b3212011-10-19 13:02:42 +0000691 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
692 "::");
693
Chris Lattnerf4601652007-11-22 20:49:04 +0000694 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
695 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
696 assert(RV && "Template arg doesn't exist??");
David Greenedcd35c72011-07-29 19:07:07 +0000697 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4601652007-11-22 20:49:04 +0000698 }
699 }
Bob Wilson21870412009-11-22 04:24:42 +0000700
David Greenebbec2792011-10-19 13:04:21 +0000701 if (Mode == ParseNameMode)
702 return StringInit::get(Name);
703
Chris Lattnerf4601652007-11-22 20:49:04 +0000704 if (Record *D = Records.getDef(Name))
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000705 return DefInit::get(D);
Chris Lattnerf4601652007-11-22 20:49:04 +0000706
David Greenebbec2792011-10-19 13:04:21 +0000707 if (Mode == ParseValueMode) {
708 Error(NameLoc, "Variable not defined: '" + Name + "'");
709 return 0;
710 }
711
712 return StringInit::get(Name);
Chris Lattnerf4601652007-11-22 20:49:04 +0000713}
714
David Greened418c1b2009-05-14 20:54:48 +0000715/// ParseOperation - Parse an operator. This returns null on error.
716///
717/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
718///
David Greene05bce0b2011-07-29 22:43:06 +0000719Init *TGParser::ParseOperation(Record *CurRec) {
David Greened418c1b2009-05-14 20:54:48 +0000720 switch (Lex.getCode()) {
721 default:
722 TokError("unknown operation");
723 return 0;
David Greene1434f662011-01-07 17:05:37 +0000724 case tgtok::XHead:
725 case tgtok::XTail:
726 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +0000727 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
728 UnOpInit::UnaryOp Code;
729 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000730
David Greenee6c27de2009-05-14 21:22:49 +0000731 switch (Lex.getCode()) {
732 default: assert(0 && "Unhandled code!");
733 case tgtok::XCast:
734 Lex.Lex(); // eat the operation
735 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000736
David Greenee6c27de2009-05-14 21:22:49 +0000737 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000738
David Greenee6c27de2009-05-14 21:22:49 +0000739 if (Type == 0) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000740 TokError("did not get type for unary operator");
David Greenee6c27de2009-05-14 21:22:49 +0000741 return 0;
742 }
David Greened418c1b2009-05-14 20:54:48 +0000743
David Greenee6c27de2009-05-14 21:22:49 +0000744 break;
David Greene1434f662011-01-07 17:05:37 +0000745 case tgtok::XHead:
David Greene5f9f9ba2009-05-14 22:38:31 +0000746 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000747 Code = UnOpInit::HEAD;
David Greene5f9f9ba2009-05-14 22:38:31 +0000748 break;
David Greene1434f662011-01-07 17:05:37 +0000749 case tgtok::XTail:
David Greene5f9f9ba2009-05-14 22:38:31 +0000750 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000751 Code = UnOpInit::TAIL;
David Greene5f9f9ba2009-05-14 22:38:31 +0000752 break;
David Greene1434f662011-01-07 17:05:37 +0000753 case tgtok::XEmpty:
David Greene5f9f9ba2009-05-14 22:38:31 +0000754 Lex.Lex(); // eat the operation
David Greene1434f662011-01-07 17:05:37 +0000755 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000756 Type = IntRecTy::get();
David Greene5f9f9ba2009-05-14 22:38:31 +0000757 break;
David Greenee6c27de2009-05-14 21:22:49 +0000758 }
759 if (Lex.getCode() != tgtok::l_paren) {
760 TokError("expected '(' after unary operator");
761 return 0;
762 }
763 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000764
David Greene05bce0b2011-07-29 22:43:06 +0000765 Init *LHS = ParseValue(CurRec);
David Greenee6c27de2009-05-14 21:22:49 +0000766 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000767
David Greene1434f662011-01-07 17:05:37 +0000768 if (Code == UnOpInit::HEAD
769 || Code == UnOpInit::TAIL
770 || Code == UnOpInit::EMPTY) {
David Greene05bce0b2011-07-29 22:43:06 +0000771 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
772 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
773 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000774 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
775 TokError("expected list or string type argument in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000776 return 0;
777 }
778 if (LHSt) {
779 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
David Greenee1b46912009-06-08 20:23:18 +0000780 StringRecTy *SType = dynamic_cast<StringRecTy*>(LHSt->getType());
781 if (LType == 0 && SType == 0) {
782 TokError("expected list or string type argumnet in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000783 return 0;
784 }
785 }
786
David Greene1434f662011-01-07 17:05:37 +0000787 if (Code == UnOpInit::HEAD
788 || Code == UnOpInit::TAIL) {
David Greenee1b46912009-06-08 20:23:18 +0000789 if (LHSl == 0 && LHSt == 0) {
790 TokError("expected list type argumnet in unary operator");
791 return 0;
792 }
Bob Wilson21870412009-11-22 04:24:42 +0000793
David Greene5f9f9ba2009-05-14 22:38:31 +0000794 if (LHSl && LHSl->getSize() == 0) {
795 TokError("empty list argument in unary operator");
796 return 0;
797 }
798 if (LHSl) {
David Greene05bce0b2011-07-29 22:43:06 +0000799 Init *Item = LHSl->getElement(0);
800 TypedInit *Itemt = dynamic_cast<TypedInit*>(Item);
David Greene5f9f9ba2009-05-14 22:38:31 +0000801 if (Itemt == 0) {
802 TokError("untyped list element in unary operator");
803 return 0;
804 }
David Greene1434f662011-01-07 17:05:37 +0000805 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000806 Type = Itemt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000807 } else {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000808 Type = ListRecTy::get(Itemt->getType());
David Greene5f9f9ba2009-05-14 22:38:31 +0000809 }
Bob Wilson21870412009-11-22 04:24:42 +0000810 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000811 assert(LHSt && "expected list type argument in unary operator");
812 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
813 if (LType == 0) {
814 TokError("expected list type argumnet in unary operator");
815 return 0;
816 }
David Greene1434f662011-01-07 17:05:37 +0000817 if (Code == UnOpInit::HEAD) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000818 Type = LType->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +0000819 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000820 Type = LType;
821 }
822 }
823 }
824 }
825
David Greenee6c27de2009-05-14 21:22:49 +0000826 if (Lex.getCode() != tgtok::r_paren) {
827 TokError("expected ')' in unary operator");
828 return 0;
829 }
830 Lex.Lex(); // eat the ')'
David Greenedcd35c72011-07-29 19:07:07 +0000831 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
David Greenee6c27de2009-05-14 21:22:49 +0000832 }
David Greened418c1b2009-05-14 20:54:48 +0000833
834 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +0000835 case tgtok::XSRA:
David Greened418c1b2009-05-14 20:54:48 +0000836 case tgtok::XSRL:
837 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +0000838 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +0000839 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000840 tgtok::TokKind OpTok = Lex.getCode();
841 SMLoc OpLoc = Lex.getLoc();
842 Lex.Lex(); // eat the operation
843
David Greened418c1b2009-05-14 20:54:48 +0000844 BinOpInit::BinaryOp Code;
845 RecTy *Type = 0;
846
Chris Lattner8d978a72010-10-05 23:58:18 +0000847 switch (OpTok) {
David Greened418c1b2009-05-14 20:54:48 +0000848 default: assert(0 && "Unhandled code!");
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000849 case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
850 case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break;
851 case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break;
852 case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break;
853 case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break;
Bob Wilson21870412009-11-22 04:24:42 +0000854 case tgtok::XStrConcat:
David Greened418c1b2009-05-14 20:54:48 +0000855 Code = BinOpInit::STRCONCAT;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000856 Type = StringRecTy::get();
David Greened418c1b2009-05-14 20:54:48 +0000857 break;
David Greened418c1b2009-05-14 20:54:48 +0000858 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000859
David Greened418c1b2009-05-14 20:54:48 +0000860 if (Lex.getCode() != tgtok::l_paren) {
861 TokError("expected '(' after binary operator");
862 return 0;
863 }
864 Lex.Lex(); // eat the '('
865
David Greene05bce0b2011-07-29 22:43:06 +0000866 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000867
Chris Lattner8d978a72010-10-05 23:58:18 +0000868 InitList.push_back(ParseValue(CurRec));
869 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000870
Chris Lattner8d978a72010-10-05 23:58:18 +0000871 while (Lex.getCode() == tgtok::comma) {
872 Lex.Lex(); // eat the ','
873
874 InitList.push_back(ParseValue(CurRec));
875 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000876 }
David Greened418c1b2009-05-14 20:54:48 +0000877
878 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner8d978a72010-10-05 23:58:18 +0000879 TokError("expected ')' in operator");
David Greened418c1b2009-05-14 20:54:48 +0000880 return 0;
881 }
882 Lex.Lex(); // eat the ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000883
884 // We allow multiple operands to associative operators like !strconcat as
885 // shorthand for nesting them.
886 if (Code == BinOpInit::STRCONCAT) {
887 while (InitList.size() > 2) {
David Greene05bce0b2011-07-29 22:43:06 +0000888 Init *RHS = InitList.pop_back_val();
David Greenedcd35c72011-07-29 19:07:07 +0000889 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
890 ->Fold(CurRec, CurMultiClass);
Chris Lattner8d978a72010-10-05 23:58:18 +0000891 InitList.back() = RHS;
892 }
893 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000894
Chris Lattner8d978a72010-10-05 23:58:18 +0000895 if (InitList.size() == 2)
David Greenedcd35c72011-07-29 19:07:07 +0000896 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Chris Lattner8d978a72010-10-05 23:58:18 +0000897 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000898
Chris Lattner8d978a72010-10-05 23:58:18 +0000899 Error(OpLoc, "expected two operands to operator");
900 return 0;
David Greened418c1b2009-05-14 20:54:48 +0000901 }
902
David Greene9bea7c82009-05-14 23:26:46 +0000903 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +0000904 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +0000905 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
906 TernOpInit::TernaryOp Code;
907 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000908
David Greene4afc5092009-05-14 21:54:42 +0000909 tgtok::TokKind LexCode = Lex.getCode();
910 Lex.Lex(); // eat the operation
911 switch (LexCode) {
912 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000913 case tgtok::XIf:
914 Code = TernOpInit::IF;
915 break;
David Greenebeb31a52009-05-14 22:23:47 +0000916 case tgtok::XForEach:
917 Code = TernOpInit::FOREACH;
918 break;
David Greene4afc5092009-05-14 21:54:42 +0000919 case tgtok::XSubst:
920 Code = TernOpInit::SUBST;
921 break;
922 }
923 if (Lex.getCode() != tgtok::l_paren) {
924 TokError("expected '(' after ternary operator");
925 return 0;
926 }
927 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000928
David Greene05bce0b2011-07-29 22:43:06 +0000929 Init *LHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +0000930 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000931
David Greene4afc5092009-05-14 21:54:42 +0000932 if (Lex.getCode() != tgtok::comma) {
933 TokError("expected ',' in ternary operator");
934 return 0;
935 }
936 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000937
David Greene05bce0b2011-07-29 22:43:06 +0000938 Init *MHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +0000939 if (MHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000940
David Greene4afc5092009-05-14 21:54:42 +0000941 if (Lex.getCode() != tgtok::comma) {
942 TokError("expected ',' in ternary operator");
943 return 0;
944 }
945 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000946
David Greene05bce0b2011-07-29 22:43:06 +0000947 Init *RHS = ParseValue(CurRec);
David Greene4afc5092009-05-14 21:54:42 +0000948 if (RHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000949
David Greene4afc5092009-05-14 21:54:42 +0000950 if (Lex.getCode() != tgtok::r_paren) {
951 TokError("expected ')' in binary operator");
952 return 0;
953 }
954 Lex.Lex(); // eat the ')'
David Greened418c1b2009-05-14 20:54:48 +0000955
David Greene4afc5092009-05-14 21:54:42 +0000956 switch (LexCode) {
957 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000958 case tgtok::XIf: {
Bill Wendling548f5a02010-12-13 01:46:19 +0000959 // FIXME: The `!if' operator doesn't handle non-TypedInit well at
960 // all. This can be made much more robust.
David Greene05bce0b2011-07-29 22:43:06 +0000961 TypedInit *MHSt = dynamic_cast<TypedInit*>(MHS);
962 TypedInit *RHSt = dynamic_cast<TypedInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +0000963
964 RecTy *MHSTy = 0;
965 RecTy *RHSTy = 0;
966
967 if (MHSt == 0 && RHSt == 0) {
David Greene05bce0b2011-07-29 22:43:06 +0000968 BitsInit *MHSbits = dynamic_cast<BitsInit*>(MHS);
969 BitsInit *RHSbits = dynamic_cast<BitsInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +0000970
971 if (MHSbits && RHSbits &&
972 MHSbits->getNumBits() == RHSbits->getNumBits()) {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000973 Type = BitRecTy::get();
Bill Wendling548f5a02010-12-13 01:46:19 +0000974 break;
975 } else {
David Greene05bce0b2011-07-29 22:43:06 +0000976 BitInit *MHSbit = dynamic_cast<BitInit*>(MHS);
977 BitInit *RHSbit = dynamic_cast<BitInit*>(RHS);
Bill Wendling548f5a02010-12-13 01:46:19 +0000978
979 if (MHSbit && RHSbit) {
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +0000980 Type = BitRecTy::get();
Bill Wendling548f5a02010-12-13 01:46:19 +0000981 break;
982 }
983 }
984 } else if (MHSt != 0 && RHSt != 0) {
985 MHSTy = MHSt->getType();
986 RHSTy = RHSt->getType();
987 }
988
989 if (!MHSTy || !RHSTy) {
David Greene9bea7c82009-05-14 23:26:46 +0000990 TokError("could not get type for !if");
991 return 0;
992 }
Bill Wendling548f5a02010-12-13 01:46:19 +0000993
994 if (MHSTy->typeIsConvertibleTo(RHSTy)) {
995 Type = RHSTy;
996 } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
997 Type = MHSTy;
Bob Wilson21870412009-11-22 04:24:42 +0000998 } else {
David Greene9bea7c82009-05-14 23:26:46 +0000999 TokError("inconsistent types for !if");
1000 return 0;
1001 }
1002 break;
1003 }
David Greenebeb31a52009-05-14 22:23:47 +00001004 case tgtok::XForEach: {
David Greene05bce0b2011-07-29 22:43:06 +00001005 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
David Greenebeb31a52009-05-14 22:23:47 +00001006 if (MHSt == 0) {
1007 TokError("could not get type for !foreach");
1008 return 0;
1009 }
1010 Type = MHSt->getType();
1011 break;
1012 }
David Greene4afc5092009-05-14 21:54:42 +00001013 case tgtok::XSubst: {
David Greene05bce0b2011-07-29 22:43:06 +00001014 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
David Greene4afc5092009-05-14 21:54:42 +00001015 if (RHSt == 0) {
1016 TokError("could not get type for !subst");
1017 return 0;
1018 }
1019 Type = RHSt->getType();
1020 break;
1021 }
1022 }
David Greenedcd35c72011-07-29 19:07:07 +00001023 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
Bob Wilson21870412009-11-22 04:24:42 +00001024 CurMultiClass);
David Greene4afc5092009-05-14 21:54:42 +00001025 }
David Greened418c1b2009-05-14 20:54:48 +00001026 }
David Greened418c1b2009-05-14 20:54:48 +00001027}
1028
1029/// ParseOperatorType - Parse a type for an operator. This returns
1030/// null on error.
1031///
1032/// OperatorType ::= '<' Type '>'
1033///
Dan Gohmana9ad0412009-08-12 22:10:57 +00001034RecTy *TGParser::ParseOperatorType() {
David Greened418c1b2009-05-14 20:54:48 +00001035 RecTy *Type = 0;
1036
1037 if (Lex.getCode() != tgtok::less) {
1038 TokError("expected type name for operator");
1039 return 0;
1040 }
1041 Lex.Lex(); // eat the <
1042
1043 Type = ParseType();
1044
1045 if (Type == 0) {
1046 TokError("expected type name for operator");
1047 return 0;
1048 }
1049
1050 if (Lex.getCode() != tgtok::greater) {
1051 TokError("expected type name for operator");
1052 return 0;
1053 }
1054 Lex.Lex(); // eat the >
1055
1056 return Type;
1057}
1058
1059
Chris Lattnerf4601652007-11-22 20:49:04 +00001060/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1061///
1062/// SimpleValue ::= IDValue
1063/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001064/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +00001065/// SimpleValue ::= CODEFRAGMENT
1066/// SimpleValue ::= '?'
1067/// SimpleValue ::= '{' ValueList '}'
1068/// SimpleValue ::= ID '<' ValueListNE '>'
1069/// SimpleValue ::= '[' ValueList ']'
1070/// SimpleValue ::= '(' IDValue DagArgList ')'
1071/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
1072/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1073/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1074/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1075/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1076///
David Greenef3744a02011-10-19 13:04:20 +00001077Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1078 IDParseMode Mode) {
David Greene05bce0b2011-07-29 22:43:06 +00001079 Init *R = 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001080 switch (Lex.getCode()) {
1081 default: TokError("Unknown token when parsing a value"); break;
David Greened3d1cad2011-10-19 13:04:43 +00001082 case tgtok::paste:
1083 // This is a leading paste operation. This is deprecated but
1084 // still exists in some .td files. Ignore it.
1085 Lex.Lex(); // Skip '#'.
1086 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenedcd35c72011-07-29 19:07:07 +00001087 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001088 case tgtok::StrVal: {
1089 std::string Val = Lex.getCurStrVal();
1090 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001091
Jim Grosbachda4231f2009-03-26 16:17:51 +00001092 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001093 while (Lex.getCode() == tgtok::StrVal) {
1094 Val += Lex.getCurStrVal();
1095 Lex.Lex();
1096 }
Bob Wilson21870412009-11-22 04:24:42 +00001097
David Greenedcd35c72011-07-29 19:07:07 +00001098 R = StringInit::get(Val);
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001099 break;
1100 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001101 case tgtok::CodeFragment:
Jakob Stoklund Olesen8dd6f0c2012-01-13 03:38:34 +00001102 R = StringInit::get(Lex.getCurStrVal());
Chris Lattner578bcf02010-10-06 04:31:40 +00001103 Lex.Lex();
1104 break;
1105 case tgtok::question:
David Greenedcd35c72011-07-29 19:07:07 +00001106 R = UnsetInit::get();
Chris Lattner578bcf02010-10-06 04:31:40 +00001107 Lex.Lex();
1108 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001109 case tgtok::Id: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001110 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001111 std::string Name = Lex.getCurStrVal();
1112 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greenef3744a02011-10-19 13:04:20 +00001113 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson21870412009-11-22 04:24:42 +00001114
Chris Lattnerf4601652007-11-22 20:49:04 +00001115 // Value ::= ID '<' ValueListNE '>'
1116 if (Lex.Lex() == tgtok::greater) {
1117 TokError("expected non-empty value list");
1118 return 0;
1119 }
David Greenee1b46912009-06-08 20:23:18 +00001120
Chris Lattnerf4601652007-11-22 20:49:04 +00001121 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1122 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1123 // body.
1124 Record *Class = Records.getClass(Name);
1125 if (!Class) {
1126 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1127 return 0;
1128 }
David Greenee1b46912009-06-08 20:23:18 +00001129
David Greene05bce0b2011-07-29 22:43:06 +00001130 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
David Greenee1b46912009-06-08 20:23:18 +00001131 if (ValueList.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001132
David Greenee1b46912009-06-08 20:23:18 +00001133 if (Lex.getCode() != tgtok::greater) {
1134 TokError("expected '>' at end of value list");
1135 return 0;
1136 }
1137 Lex.Lex(); // eat the '>'
Bob Wilson21870412009-11-22 04:24:42 +00001138
Chris Lattnerf4601652007-11-22 20:49:04 +00001139 // Create the new record, set it as CurRec temporarily.
1140 static unsigned AnonCounter = 0;
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001141 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),
1142 NameLoc,
1143 Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001144 SubClassReference SCRef;
1145 SCRef.RefLoc = NameLoc;
1146 SCRef.Rec = Class;
1147 SCRef.TemplateArgs = ValueList;
1148 // Add info about the subclass to NewRec.
1149 if (AddSubClass(NewRec, SCRef))
1150 return 0;
1151 NewRec->resolveReferences();
1152 Records.addDef(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001153
Chris Lattnerf4601652007-11-22 20:49:04 +00001154 // The result of the expression is a reference to the new record.
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +00001155 return DefInit::get(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001156 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001157 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001158 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001159 Lex.Lex(); // eat the '{'
David Greene05bce0b2011-07-29 22:43:06 +00001160 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001161
Chris Lattnerf4601652007-11-22 20:49:04 +00001162 if (Lex.getCode() != tgtok::r_brace) {
1163 Vals = ParseValueList(CurRec);
1164 if (Vals.empty()) return 0;
1165 }
1166 if (Lex.getCode() != tgtok::r_brace) {
1167 TokError("expected '}' at end of bit list value");
1168 return 0;
1169 }
1170 Lex.Lex(); // eat the '}'
Bob Wilson21870412009-11-22 04:24:42 +00001171
David Greene05bce0b2011-07-29 22:43:06 +00001172 SmallVector<Init *, 16> NewBits(Vals.size());
David Greeneca7fd3d2011-07-29 19:07:00 +00001173
Chris Lattnerf4601652007-11-22 20:49:04 +00001174 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001175 Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
Chris Lattnerf4601652007-11-22 20:49:04 +00001176 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +00001177 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1178 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +00001179 return 0;
1180 }
David Greeneca7fd3d2011-07-29 19:07:00 +00001181 NewBits[Vals.size()-i-1] = Bit;
Chris Lattnerf4601652007-11-22 20:49:04 +00001182 }
David Greenedcd35c72011-07-29 19:07:07 +00001183 return BitsInit::get(NewBits);
Chris Lattnerf4601652007-11-22 20:49:04 +00001184 }
1185 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1186 Lex.Lex(); // eat the '['
David Greene05bce0b2011-07-29 22:43:06 +00001187 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001188
David Greenee1b46912009-06-08 20:23:18 +00001189 RecTy *DeducedEltTy = 0;
1190 ListRecTy *GivenListTy = 0;
Bob Wilson21870412009-11-22 04:24:42 +00001191
David Greenee1b46912009-06-08 20:23:18 +00001192 if (ItemType != 0) {
1193 ListRecTy *ListType = dynamic_cast<ListRecTy*>(ItemType);
1194 if (ListType == 0) {
1195 std::stringstream s;
Bob Wilson21870412009-11-22 04:24:42 +00001196 s << "Type mismatch for list, expected list type, got "
David Greenee1b46912009-06-08 20:23:18 +00001197 << ItemType->getAsString();
1198 TokError(s.str());
Jim Grosbach6a44ada2011-03-11 19:52:52 +00001199 return 0;
David Greenee1b46912009-06-08 20:23:18 +00001200 }
1201 GivenListTy = ListType;
Bob Wilson21870412009-11-22 04:24:42 +00001202 }
David Greenee1b46912009-06-08 20:23:18 +00001203
Chris Lattnerf4601652007-11-22 20:49:04 +00001204 if (Lex.getCode() != tgtok::r_square) {
Bob Wilson21870412009-11-22 04:24:42 +00001205 Vals = ParseValueList(CurRec, 0,
1206 GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001207 if (Vals.empty()) return 0;
1208 }
1209 if (Lex.getCode() != tgtok::r_square) {
1210 TokError("expected ']' at end of list value");
1211 return 0;
1212 }
1213 Lex.Lex(); // eat the ']'
David Greenee1b46912009-06-08 20:23:18 +00001214
1215 RecTy *GivenEltTy = 0;
1216 if (Lex.getCode() == tgtok::less) {
1217 // Optional list element type
1218 Lex.Lex(); // eat the '<'
1219
1220 GivenEltTy = ParseType();
1221 if (GivenEltTy == 0) {
1222 // Couldn't parse element type
1223 return 0;
1224 }
1225
1226 if (Lex.getCode() != tgtok::greater) {
1227 TokError("expected '>' at end of list element type");
1228 return 0;
1229 }
1230 Lex.Lex(); // eat the '>'
1231 }
1232
1233 // Check elements
1234 RecTy *EltTy = 0;
David Greene05bce0b2011-07-29 22:43:06 +00001235 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
David Greenee1b46912009-06-08 20:23:18 +00001236 i != ie;
1237 ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001238 TypedInit *TArg = dynamic_cast<TypedInit*>(*i);
David Greenee1b46912009-06-08 20:23:18 +00001239 if (TArg == 0) {
1240 TokError("Untyped list element");
1241 return 0;
1242 }
1243 if (EltTy != 0) {
1244 EltTy = resolveTypes(EltTy, TArg->getType());
1245 if (EltTy == 0) {
1246 TokError("Incompatible types in list elements");
1247 return 0;
1248 }
Bob Wilson21870412009-11-22 04:24:42 +00001249 } else {
David Greenee1b46912009-06-08 20:23:18 +00001250 EltTy = TArg->getType();
1251 }
1252 }
1253
1254 if (GivenEltTy != 0) {
1255 if (EltTy != 0) {
1256 // Verify consistency
1257 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1258 TokError("Incompatible types in list elements");
1259 return 0;
1260 }
1261 }
1262 EltTy = GivenEltTy;
1263 }
1264
1265 if (EltTy == 0) {
1266 if (ItemType == 0) {
1267 TokError("No type for list");
1268 return 0;
1269 }
1270 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +00001271 } else {
David Greenee1b46912009-06-08 20:23:18 +00001272 // Make sure the deduced type is compatible with the given type
1273 if (GivenListTy) {
1274 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1275 TokError("Element type mismatch for list");
1276 return 0;
1277 }
1278 }
1279 DeducedEltTy = EltTy;
1280 }
Bob Wilson21870412009-11-22 04:24:42 +00001281
David Greenedcd35c72011-07-29 19:07:07 +00001282 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4601652007-11-22 20:49:04 +00001283 }
1284 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1285 Lex.Lex(); // eat the '('
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001286 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001287 TokError("expected identifier in dag init");
1288 return 0;
1289 }
Bob Wilson21870412009-11-22 04:24:42 +00001290
David Greene05bce0b2011-07-29 22:43:06 +00001291 Init *Operator = ParseValue(CurRec);
Chris Lattner578bcf02010-10-06 04:31:40 +00001292 if (Operator == 0) return 0;
David Greenec7cafcd2009-04-22 20:18:10 +00001293
Nate Begeman7cee8172009-03-19 05:21:56 +00001294 // If the operator name is present, parse it.
1295 std::string OperatorName;
1296 if (Lex.getCode() == tgtok::colon) {
1297 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1298 TokError("expected variable name in dag operator");
1299 return 0;
1300 }
1301 OperatorName = Lex.getCurStrVal();
1302 Lex.Lex(); // eat the VarName.
1303 }
Bob Wilson21870412009-11-22 04:24:42 +00001304
David Greene05bce0b2011-07-29 22:43:06 +00001305 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
Chris Lattnerf4601652007-11-22 20:49:04 +00001306 if (Lex.getCode() != tgtok::r_paren) {
1307 DagArgs = ParseDagArgList(CurRec);
1308 if (DagArgs.empty()) return 0;
1309 }
Bob Wilson21870412009-11-22 04:24:42 +00001310
Chris Lattnerf4601652007-11-22 20:49:04 +00001311 if (Lex.getCode() != tgtok::r_paren) {
1312 TokError("expected ')' in dag init");
1313 return 0;
1314 }
1315 Lex.Lex(); // eat the ')'
Bob Wilson21870412009-11-22 04:24:42 +00001316
David Greenedcd35c72011-07-29 19:07:07 +00001317 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4601652007-11-22 20:49:04 +00001318 }
Bob Wilson21870412009-11-22 04:24:42 +00001319
David Greene1434f662011-01-07 17:05:37 +00001320 case tgtok::XHead:
1321 case tgtok::XTail:
1322 case tgtok::XEmpty:
David Greenee6c27de2009-05-14 21:22:49 +00001323 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001324 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +00001325 case tgtok::XSRA:
Chris Lattnerf4601652007-11-22 20:49:04 +00001326 case tgtok::XSRL:
1327 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +00001328 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001329 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene9bea7c82009-05-14 23:26:46 +00001330 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +00001331 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001332 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greened418c1b2009-05-14 20:54:48 +00001333 return ParseOperation(CurRec);
Chris Lattnerf4601652007-11-22 20:49:04 +00001334 }
1335 }
Bob Wilson21870412009-11-22 04:24:42 +00001336
Chris Lattnerf4601652007-11-22 20:49:04 +00001337 return R;
1338}
1339
1340/// ParseValue - Parse a tblgen value. This returns null on error.
1341///
1342/// Value ::= SimpleValue ValueSuffix*
1343/// ValueSuffix ::= '{' BitList '}'
1344/// ValueSuffix ::= '[' BitList ']'
1345/// ValueSuffix ::= '.' ID
1346///
David Greenef3744a02011-10-19 13:04:20 +00001347Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1348 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Chris Lattnerf4601652007-11-22 20:49:04 +00001349 if (Result == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001350
Chris Lattnerf4601652007-11-22 20:49:04 +00001351 // Parse the suffixes now if present.
1352 while (1) {
1353 switch (Lex.getCode()) {
1354 default: return Result;
1355 case tgtok::l_brace: {
David Greene8592b2b2011-10-19 13:04:26 +00001356 if (Mode == ParseNameMode)
1357 // This is the beginning of the object body.
1358 return Result;
1359
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001360 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001361 Lex.Lex(); // eat the '{'
1362 std::vector<unsigned> Ranges = ParseRangeList();
1363 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001364
Chris Lattnerf4601652007-11-22 20:49:04 +00001365 // Reverse the bitlist.
1366 std::reverse(Ranges.begin(), Ranges.end());
1367 Result = Result->convertInitializerBitRange(Ranges);
1368 if (Result == 0) {
1369 Error(CurlyLoc, "Invalid bit range for value");
1370 return 0;
1371 }
Bob Wilson21870412009-11-22 04:24:42 +00001372
Chris Lattnerf4601652007-11-22 20:49:04 +00001373 // Eat the '}'.
1374 if (Lex.getCode() != tgtok::r_brace) {
1375 TokError("expected '}' at end of bit range list");
1376 return 0;
1377 }
1378 Lex.Lex();
1379 break;
1380 }
1381 case tgtok::l_square: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001382 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001383 Lex.Lex(); // eat the '['
1384 std::vector<unsigned> Ranges = ParseRangeList();
1385 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001386
Chris Lattnerf4601652007-11-22 20:49:04 +00001387 Result = Result->convertInitListSlice(Ranges);
1388 if (Result == 0) {
1389 Error(SquareLoc, "Invalid range for list slice");
1390 return 0;
1391 }
Bob Wilson21870412009-11-22 04:24:42 +00001392
Chris Lattnerf4601652007-11-22 20:49:04 +00001393 // Eat the ']'.
1394 if (Lex.getCode() != tgtok::r_square) {
1395 TokError("expected ']' at end of list slice");
1396 return 0;
1397 }
1398 Lex.Lex();
1399 break;
1400 }
1401 case tgtok::period:
1402 if (Lex.Lex() != tgtok::Id) { // eat the .
1403 TokError("expected field identifier after '.'");
1404 return 0;
1405 }
1406 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001407 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00001408 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +00001409 return 0;
1410 }
David Greenedcd35c72011-07-29 19:07:07 +00001411 Result = FieldInit::get(Result, Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001412 Lex.Lex(); // eat field name
1413 break;
David Greened3d1cad2011-10-19 13:04:43 +00001414
1415 case tgtok::paste:
1416 SMLoc PasteLoc = Lex.getLoc();
1417
1418 // Create a !strconcat() operation, first casting each operand to
1419 // a string if necessary.
1420
1421 TypedInit *LHS = dynamic_cast<TypedInit *>(Result);
1422 if (!LHS) {
1423 Error(PasteLoc, "LHS of paste is not typed!");
1424 return 0;
1425 }
1426
1427 if (LHS->getType() != StringRecTy::get()) {
1428 LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1429 }
1430
1431 TypedInit *RHS = 0;
1432
1433 Lex.Lex(); // Eat the '#'.
1434 switch (Lex.getCode()) {
1435 case tgtok::colon:
1436 case tgtok::semi:
1437 case tgtok::l_brace:
1438 // These are all of the tokens that can begin an object body.
1439 // Some of these can also begin values but we disallow those cases
1440 // because they are unlikely to be useful.
1441
1442 // Trailing paste, concat with an empty string.
1443 RHS = StringInit::get("");
1444 break;
1445
1446 default:
1447 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
1448 RHS = dynamic_cast<TypedInit *>(RHSResult);
1449 if (!RHS) {
1450 Error(PasteLoc, "RHS of paste is not typed!");
1451 return 0;
1452 }
1453
1454 if (RHS->getType() != StringRecTy::get()) {
1455 RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1456 }
1457
1458 break;
1459 }
1460
1461 Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1462 StringRecTy::get())->Fold(CurRec, CurMultiClass);
1463 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001464 }
1465 }
1466}
1467
1468/// ParseDagArgList - Parse the argument list for a dag literal expression.
1469///
1470/// ParseDagArgList ::= Value (':' VARNAME)?
1471/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
David Greene05bce0b2011-07-29 22:43:06 +00001472std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4601652007-11-22 20:49:04 +00001473TGParser::ParseDagArgList(Record *CurRec) {
David Greene05bce0b2011-07-29 22:43:06 +00001474 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson21870412009-11-22 04:24:42 +00001475
Chris Lattnerf4601652007-11-22 20:49:04 +00001476 while (1) {
David Greene05bce0b2011-07-29 22:43:06 +00001477 Init *Val = ParseValue(CurRec);
1478 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
Bob Wilson21870412009-11-22 04:24:42 +00001479
Chris Lattnerf4601652007-11-22 20:49:04 +00001480 // If the variable name is present, add it.
1481 std::string VarName;
1482 if (Lex.getCode() == tgtok::colon) {
1483 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1484 TokError("expected variable name in dag literal");
David Greene05bce0b2011-07-29 22:43:06 +00001485 return std::vector<std::pair<llvm::Init*, std::string> >();
Chris Lattnerf4601652007-11-22 20:49:04 +00001486 }
1487 VarName = Lex.getCurStrVal();
1488 Lex.Lex(); // eat the VarName.
1489 }
Bob Wilson21870412009-11-22 04:24:42 +00001490
Chris Lattnerf4601652007-11-22 20:49:04 +00001491 Result.push_back(std::make_pair(Val, VarName));
Bob Wilson21870412009-11-22 04:24:42 +00001492
Chris Lattnerf4601652007-11-22 20:49:04 +00001493 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson21870412009-11-22 04:24:42 +00001494 Lex.Lex(); // eat the ','
Chris Lattnerf4601652007-11-22 20:49:04 +00001495 }
Bob Wilson21870412009-11-22 04:24:42 +00001496
Chris Lattnerf4601652007-11-22 20:49:04 +00001497 return Result;
1498}
1499
1500
1501/// ParseValueList - Parse a comma separated list of values, returning them as a
1502/// vector. Note that this always expects to be able to parse at least one
1503/// value. It returns an empty list if this is not possible.
1504///
1505/// ValueList ::= Value (',' Value)
1506///
David Greene05bce0b2011-07-29 22:43:06 +00001507std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
Eric Christopherd568b3f2011-07-11 23:06:52 +00001508 RecTy *EltTy) {
David Greene05bce0b2011-07-29 22:43:06 +00001509 std::vector<Init*> Result;
David Greenee1b46912009-06-08 20:23:18 +00001510 RecTy *ItemType = EltTy;
David Greene67acdf22009-06-29 19:59:52 +00001511 unsigned int ArgN = 0;
David Greenee1b46912009-06-08 20:23:18 +00001512 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001513 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
Jim Grosbachb1320cb2012-01-20 20:02:39 +00001514 if (!TArgs.size()) {
1515 TokError("template argument provided to non-template class");
1516 return std::vector<Init*>();
1517 }
David Greenee1b46912009-06-08 20:23:18 +00001518 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greened9746fe2011-09-19 18:26:07 +00001519 if (!RV) {
1520 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1521 << ")\n";
1522 }
David Greenee1b46912009-06-08 20:23:18 +00001523 assert(RV && "Template argument record not found??");
1524 ItemType = RV->getType();
1525 ++ArgN;
1526 }
1527 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001528 if (Result.back() == 0) return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001529
Chris Lattnerf4601652007-11-22 20:49:04 +00001530 while (Lex.getCode() == tgtok::comma) {
1531 Lex.Lex(); // Eat the comma
Bob Wilson21870412009-11-22 04:24:42 +00001532
David Greenee1b46912009-06-08 20:23:18 +00001533 if (ArgsRec != 0 && EltTy == 0) {
David Greenee22b3212011-10-19 13:02:42 +00001534 const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs();
David Greene67acdf22009-06-29 19:59:52 +00001535 if (ArgN >= TArgs.size()) {
1536 TokError("too many template arguments");
David Greene05bce0b2011-07-29 22:43:06 +00001537 return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001538 }
David Greenee1b46912009-06-08 20:23:18 +00001539 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1540 assert(RV && "Template argument record not found??");
1541 ItemType = RV->getType();
1542 ++ArgN;
1543 }
1544 Result.push_back(ParseValue(CurRec, ItemType));
David Greene05bce0b2011-07-29 22:43:06 +00001545 if (Result.back() == 0) return std::vector<Init*>();
Chris Lattnerf4601652007-11-22 20:49:04 +00001546 }
Bob Wilson21870412009-11-22 04:24:42 +00001547
Chris Lattnerf4601652007-11-22 20:49:04 +00001548 return Result;
1549}
1550
1551
Chris Lattnerf4601652007-11-22 20:49:04 +00001552/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1553/// empty string on error. This can happen in a number of different context's,
1554/// including within a def or in the template args for a def (which which case
1555/// CurRec will be non-null) and within the template args for a multiclass (in
1556/// which case CurRec will be null, but CurMultiClass will be set). This can
1557/// also happen within a def that is within a multiclass, which will set both
1558/// CurRec and CurMultiClass.
1559///
1560/// Declaration ::= FIELD? Type ID ('=' Value)?
1561///
David Greenee22b3212011-10-19 13:02:42 +00001562Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4601652007-11-22 20:49:04 +00001563 bool ParsingTemplateArgs) {
1564 // Read the field prefix if present.
1565 bool HasField = Lex.getCode() == tgtok::Field;
1566 if (HasField) Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001567
Chris Lattnerf4601652007-11-22 20:49:04 +00001568 RecTy *Type = ParseType();
David Greenee22b3212011-10-19 13:02:42 +00001569 if (Type == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001570
Chris Lattnerf4601652007-11-22 20:49:04 +00001571 if (Lex.getCode() != tgtok::Id) {
1572 TokError("Expected identifier in declaration");
David Greenee22b3212011-10-19 13:02:42 +00001573 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001574 }
Bob Wilson21870412009-11-22 04:24:42 +00001575
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001576 SMLoc IdLoc = Lex.getLoc();
David Greenee22b3212011-10-19 13:02:42 +00001577 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4601652007-11-22 20:49:04 +00001578 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001579
Chris Lattnerf4601652007-11-22 20:49:04 +00001580 if (ParsingTemplateArgs) {
1581 if (CurRec) {
David Greenee22b3212011-10-19 13:02:42 +00001582 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Chris Lattnerf4601652007-11-22 20:49:04 +00001583 } else {
1584 assert(CurMultiClass);
1585 }
1586 if (CurMultiClass)
David Greenee22b3212011-10-19 13:02:42 +00001587 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1588 "::");
Chris Lattnerf4601652007-11-22 20:49:04 +00001589 }
Bob Wilson21870412009-11-22 04:24:42 +00001590
Chris Lattnerf4601652007-11-22 20:49:04 +00001591 // Add the value.
1592 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
David Greenee22b3212011-10-19 13:02:42 +00001593 return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001594
Chris Lattnerf4601652007-11-22 20:49:04 +00001595 // If a value is present, parse it.
1596 if (Lex.getCode() == tgtok::equal) {
1597 Lex.Lex();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001598 SMLoc ValLoc = Lex.getLoc();
David Greene05bce0b2011-07-29 22:43:06 +00001599 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001600 if (Val == 0 ||
1601 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
David Greenee22b3212011-10-19 13:02:42 +00001602 return 0;
Chris Lattnerf4601652007-11-22 20:49:04 +00001603 }
Bob Wilson21870412009-11-22 04:24:42 +00001604
Chris Lattnerf4601652007-11-22 20:49:04 +00001605 return DeclName;
1606}
1607
1608/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1609/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1610/// template args for a def, which may or may not be in a multiclass. If null,
1611/// these are the template args for a multiclass.
1612///
1613/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson21870412009-11-22 04:24:42 +00001614///
Chris Lattnerf4601652007-11-22 20:49:04 +00001615bool TGParser::ParseTemplateArgList(Record *CurRec) {
1616 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1617 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +00001618
Chris Lattnerf4601652007-11-22 20:49:04 +00001619 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +00001620
Chris Lattnerf4601652007-11-22 20:49:04 +00001621 // Read the first declaration.
David Greenee22b3212011-10-19 13:02:42 +00001622 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1623 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001624 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001625
Chris Lattnerf4601652007-11-22 20:49:04 +00001626 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson21870412009-11-22 04:24:42 +00001627
Chris Lattnerf4601652007-11-22 20:49:04 +00001628 while (Lex.getCode() == tgtok::comma) {
1629 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001630
Chris Lattnerf4601652007-11-22 20:49:04 +00001631 // Read the following declarations.
1632 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
David Greenee22b3212011-10-19 13:02:42 +00001633 if (TemplArg == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001634 return true;
1635 TheRecToAddTo->addTemplateArg(TemplArg);
1636 }
Bob Wilson21870412009-11-22 04:24:42 +00001637
Chris Lattnerf4601652007-11-22 20:49:04 +00001638 if (Lex.getCode() != tgtok::greater)
1639 return TokError("expected '>' at end of template argument list");
1640 Lex.Lex(); // eat the '>'.
1641 return false;
1642}
1643
1644
1645/// ParseBodyItem - Parse a single item at within the body of a def or class.
1646///
1647/// BodyItem ::= Declaration ';'
1648/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1649bool TGParser::ParseBodyItem(Record *CurRec) {
1650 if (Lex.getCode() != tgtok::Let) {
David Greenee22b3212011-10-19 13:02:42 +00001651 if (ParseDeclaration(CurRec, false) == 0)
Chris Lattnerf4601652007-11-22 20:49:04 +00001652 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001653
Chris Lattnerf4601652007-11-22 20:49:04 +00001654 if (Lex.getCode() != tgtok::semi)
1655 return TokError("expected ';' after declaration");
1656 Lex.Lex();
1657 return false;
1658 }
1659
1660 // LET ID OptionalRangeList '=' Value ';'
1661 if (Lex.Lex() != tgtok::Id)
1662 return TokError("expected field identifier after let");
Bob Wilson21870412009-11-22 04:24:42 +00001663
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001664 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001665 std::string FieldName = Lex.getCurStrVal();
1666 Lex.Lex(); // eat the field name.
Bob Wilson21870412009-11-22 04:24:42 +00001667
Chris Lattnerf4601652007-11-22 20:49:04 +00001668 std::vector<unsigned> BitList;
Bob Wilson21870412009-11-22 04:24:42 +00001669 if (ParseOptionalBitList(BitList))
Chris Lattnerf4601652007-11-22 20:49:04 +00001670 return true;
1671 std::reverse(BitList.begin(), BitList.end());
Bob Wilson21870412009-11-22 04:24:42 +00001672
Chris Lattnerf4601652007-11-22 20:49:04 +00001673 if (Lex.getCode() != tgtok::equal)
1674 return TokError("expected '=' in let expression");
1675 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001676
David Greenee1b46912009-06-08 20:23:18 +00001677 RecordVal *Field = CurRec->getValue(FieldName);
1678 if (Field == 0)
1679 return TokError("Value '" + FieldName + "' unknown!");
1680
1681 RecTy *Type = Field->getType();
Bob Wilson21870412009-11-22 04:24:42 +00001682
David Greene05bce0b2011-07-29 22:43:06 +00001683 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001684 if (Val == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001685
Chris Lattnerf4601652007-11-22 20:49:04 +00001686 if (Lex.getCode() != tgtok::semi)
1687 return TokError("expected ';' after let expression");
1688 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001689
Chris Lattnerf4601652007-11-22 20:49:04 +00001690 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1691}
1692
1693/// ParseBody - Read the body of a class or def. Return true on error, false on
1694/// success.
1695///
1696/// Body ::= ';'
1697/// Body ::= '{' BodyList '}'
1698/// BodyList BodyItem*
1699///
1700bool TGParser::ParseBody(Record *CurRec) {
1701 // If this is a null definition, just eat the semi and return.
1702 if (Lex.getCode() == tgtok::semi) {
1703 Lex.Lex();
1704 return false;
1705 }
Bob Wilson21870412009-11-22 04:24:42 +00001706
Chris Lattnerf4601652007-11-22 20:49:04 +00001707 if (Lex.getCode() != tgtok::l_brace)
1708 return TokError("Expected ';' or '{' to start body");
1709 // Eat the '{'.
1710 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001711
Chris Lattnerf4601652007-11-22 20:49:04 +00001712 while (Lex.getCode() != tgtok::r_brace)
1713 if (ParseBodyItem(CurRec))
1714 return true;
1715
1716 // Eat the '}'.
1717 Lex.Lex();
1718 return false;
1719}
1720
1721/// ParseObjectBody - Parse the body of a def or class. This consists of an
1722/// optional ClassList followed by a Body. CurRec is the current def or class
1723/// that is being parsed.
1724///
1725/// ObjectBody ::= BaseClassList Body
1726/// BaseClassList ::= /*empty*/
1727/// BaseClassList ::= ':' BaseClassListNE
1728/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1729///
1730bool TGParser::ParseObjectBody(Record *CurRec) {
1731 // If there is a baseclass list, read it.
1732 if (Lex.getCode() == tgtok::colon) {
1733 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001734
Chris Lattnerf4601652007-11-22 20:49:04 +00001735 // Read all of the subclasses.
1736 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1737 while (1) {
1738 // Check for error.
1739 if (SubClass.Rec == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001740
Chris Lattnerf4601652007-11-22 20:49:04 +00001741 // Add it.
1742 if (AddSubClass(CurRec, SubClass))
1743 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001744
Chris Lattnerf4601652007-11-22 20:49:04 +00001745 if (Lex.getCode() != tgtok::comma) break;
1746 Lex.Lex(); // eat ','.
1747 SubClass = ParseSubClassReference(CurRec, false);
1748 }
1749 }
1750
1751 // Process any variables on the let stack.
1752 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1753 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1754 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1755 LetStack[i][j].Bits, LetStack[i][j].Value))
1756 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001757
Chris Lattnerf4601652007-11-22 20:49:04 +00001758 return ParseBody(CurRec);
1759}
1760
Chris Lattnerf4601652007-11-22 20:49:04 +00001761/// ParseDef - Parse and return a top level or multiclass def, return the record
1762/// corresponding to it. This returns null on error.
1763///
1764/// DefInst ::= DEF ObjectName ObjectBody
1765///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001766bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001767 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001768 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson21870412009-11-22 04:24:42 +00001769 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4601652007-11-22 20:49:04 +00001770
1771 // Parse ObjectName and make a record for it.
David Greenea9e07dd2011-10-19 13:04:29 +00001772 Record *CurRec = new Record(ParseObjectName(CurMultiClass), DefLoc, Records);
Bob Wilson21870412009-11-22 04:24:42 +00001773
Chris Lattnerf4601652007-11-22 20:49:04 +00001774 if (!CurMultiClass) {
1775 // Top-level def definition.
Bob Wilson21870412009-11-22 04:24:42 +00001776
Chris Lattnerf4601652007-11-22 20:49:04 +00001777 // Ensure redefinition doesn't happen.
David Greene1d501392012-01-28 00:03:24 +00001778 if (Records.getDef(CurRec->getNameInitAsString())) {
David Greene2c49fbb2011-10-19 13:03:45 +00001779 Error(DefLoc, "def '" + CurRec->getNameInitAsString()
1780 + "' already defined");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001781 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001782 }
1783 Records.addDef(CurRec);
1784 } else {
1785 // Otherwise, a def inside a multiclass, add it to the multiclass.
1786 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
David Greene91919cd2011-10-19 13:03:51 +00001787 if (CurMultiClass->DefPrototypes[i]->getNameInit()
1788 == CurRec->getNameInit()) {
1789 Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
Chris Lattnerf4601652007-11-22 20:49:04 +00001790 "' already defined in this multiclass!");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001791 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001792 }
1793 CurMultiClass->DefPrototypes.push_back(CurRec);
1794 }
Bob Wilson21870412009-11-22 04:24:42 +00001795
Chris Lattnerf4601652007-11-22 20:49:04 +00001796 if (ParseObjectBody(CurRec))
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001797 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001798
Chris Lattnerf4601652007-11-22 20:49:04 +00001799 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
David Greene0d886402011-08-10 18:27:46 +00001800 // See Record::setName(). This resolve step will see any new name
1801 // for the def that might have been created when resolving
1802 // inheritance, values and arguments above.
Chris Lattnerf4601652007-11-22 20:49:04 +00001803 CurRec->resolveReferences();
Bob Wilson21870412009-11-22 04:24:42 +00001804
Chris Lattnerf4601652007-11-22 20:49:04 +00001805 // If ObjectBody has template arguments, it's an error.
1806 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001807
1808 if (CurMultiClass) {
1809 // Copy the template arguments for the multiclass into the def.
David Greenee22b3212011-10-19 13:02:42 +00001810 const std::vector<Init *> &TArgs =
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001811 CurMultiClass->Rec.getTemplateArgs();
1812
1813 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1814 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1815 assert(RV && "Template arg doesn't exist?");
1816 CurRec->addValue(*RV);
1817 }
1818 }
1819
1820 return false;
Chris Lattnerf4601652007-11-22 20:49:04 +00001821}
1822
Chris Lattnerf4601652007-11-22 20:49:04 +00001823/// ParseClass - Parse a tblgen class definition.
1824///
1825/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
1826///
1827bool TGParser::ParseClass() {
1828 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
1829 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001830
Chris Lattnerf4601652007-11-22 20:49:04 +00001831 if (Lex.getCode() != tgtok::Id)
1832 return TokError("expected class name after 'class' keyword");
Bob Wilson21870412009-11-22 04:24:42 +00001833
Chris Lattnerf4601652007-11-22 20:49:04 +00001834 Record *CurRec = Records.getClass(Lex.getCurStrVal());
1835 if (CurRec) {
1836 // If the body was previously defined, this is an error.
David Greenee3385652011-10-19 13:04:13 +00001837 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4601652007-11-22 20:49:04 +00001838 !CurRec->getSuperClasses().empty() ||
1839 !CurRec->getTemplateArgs().empty())
David Greene69a23942011-10-19 13:03:58 +00001840 return TokError("Class '" + CurRec->getNameInitAsString()
1841 + "' already defined");
Chris Lattnerf4601652007-11-22 20:49:04 +00001842 } else {
1843 // If this is the first reference to this class, create and add it.
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001844 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001845 Records.addClass(CurRec);
1846 }
1847 Lex.Lex(); // eat the name.
Bob Wilson21870412009-11-22 04:24:42 +00001848
Chris Lattnerf4601652007-11-22 20:49:04 +00001849 // If there are template args, parse them.
1850 if (Lex.getCode() == tgtok::less)
1851 if (ParseTemplateArgList(CurRec))
1852 return true;
1853
1854 // Finally, parse the object body.
1855 return ParseObjectBody(CurRec);
1856}
1857
1858/// ParseLetList - Parse a non-empty list of assignment expressions into a list
1859/// of LetRecords.
1860///
1861/// LetList ::= LetItem (',' LetItem)*
1862/// LetItem ::= ID OptionalRangeList '=' Value
1863///
1864std::vector<LetRecord> TGParser::ParseLetList() {
1865 std::vector<LetRecord> Result;
Bob Wilson21870412009-11-22 04:24:42 +00001866
Chris Lattnerf4601652007-11-22 20:49:04 +00001867 while (1) {
1868 if (Lex.getCode() != tgtok::Id) {
1869 TokError("expected identifier in let definition");
1870 return std::vector<LetRecord>();
1871 }
1872 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001873 SMLoc NameLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +00001874 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4601652007-11-22 20:49:04 +00001875
1876 // Check for an optional RangeList.
1877 std::vector<unsigned> Bits;
Bob Wilson21870412009-11-22 04:24:42 +00001878 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4601652007-11-22 20:49:04 +00001879 return std::vector<LetRecord>();
1880 std::reverse(Bits.begin(), Bits.end());
Bob Wilson21870412009-11-22 04:24:42 +00001881
Chris Lattnerf4601652007-11-22 20:49:04 +00001882 if (Lex.getCode() != tgtok::equal) {
1883 TokError("expected '=' in let expression");
1884 return std::vector<LetRecord>();
1885 }
1886 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001887
David Greene05bce0b2011-07-29 22:43:06 +00001888 Init *Val = ParseValue(0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001889 if (Val == 0) return std::vector<LetRecord>();
Bob Wilson21870412009-11-22 04:24:42 +00001890
Chris Lattnerf4601652007-11-22 20:49:04 +00001891 // Now that we have everything, add the record.
1892 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson21870412009-11-22 04:24:42 +00001893
Chris Lattnerf4601652007-11-22 20:49:04 +00001894 if (Lex.getCode() != tgtok::comma)
1895 return Result;
Bob Wilson21870412009-11-22 04:24:42 +00001896 Lex.Lex(); // eat the comma.
Chris Lattnerf4601652007-11-22 20:49:04 +00001897 }
1898}
1899
1900/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001901/// different related productions. This works inside multiclasses too.
Chris Lattnerf4601652007-11-22 20:49:04 +00001902///
1903/// Object ::= LET LetList IN '{' ObjectList '}'
1904/// Object ::= LET LetList IN Object
1905///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001906bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001907 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
1908 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001909
Chris Lattnerf4601652007-11-22 20:49:04 +00001910 // Add this entry to the let stack.
1911 std::vector<LetRecord> LetInfo = ParseLetList();
1912 if (LetInfo.empty()) return true;
1913 LetStack.push_back(LetInfo);
1914
1915 if (Lex.getCode() != tgtok::In)
1916 return TokError("expected 'in' at end of top-level 'let'");
1917 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001918
Chris Lattnerf4601652007-11-22 20:49:04 +00001919 // If this is a scalar let, just handle it now
1920 if (Lex.getCode() != tgtok::l_brace) {
1921 // LET LetList IN Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001922 if (ParseObject(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00001923 return true;
1924 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001925 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001926 // Otherwise, this is a group let.
1927 Lex.Lex(); // eat the '{'.
Bob Wilson21870412009-11-22 04:24:42 +00001928
Chris Lattnerf4601652007-11-22 20:49:04 +00001929 // Parse the object list.
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001930 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00001931 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001932
Chris Lattnerf4601652007-11-22 20:49:04 +00001933 if (Lex.getCode() != tgtok::r_brace) {
1934 TokError("expected '}' at end of top level let command");
1935 return Error(BraceLoc, "to match this '{'");
1936 }
1937 Lex.Lex();
1938 }
Bob Wilson21870412009-11-22 04:24:42 +00001939
Chris Lattnerf4601652007-11-22 20:49:04 +00001940 // Outside this let scope, this let block is not active.
1941 LetStack.pop_back();
1942 return false;
1943}
1944
Chris Lattnerf4601652007-11-22 20:49:04 +00001945/// ParseMultiClass - Parse a multiclass definition.
1946///
Bob Wilson32558652009-04-28 19:41:44 +00001947/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
1948/// ':' BaseMultiClassList '{' MultiClassDef+ '}'
Chris Lattnerf4601652007-11-22 20:49:04 +00001949///
1950bool TGParser::ParseMultiClass() {
1951 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
1952 Lex.Lex(); // Eat the multiclass token.
1953
1954 if (Lex.getCode() != tgtok::Id)
1955 return TokError("expected identifier after multiclass for name");
1956 std::string Name = Lex.getCurStrVal();
Bob Wilson21870412009-11-22 04:24:42 +00001957
Chris Lattnerf4601652007-11-22 20:49:04 +00001958 if (MultiClasses.count(Name))
1959 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson21870412009-11-22 04:24:42 +00001960
Chris Lattner67db8832010-12-13 00:23:57 +00001961 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
1962 Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001963 Lex.Lex(); // Eat the identifier.
Bob Wilson21870412009-11-22 04:24:42 +00001964
Chris Lattnerf4601652007-11-22 20:49:04 +00001965 // If there are template args, parse them.
1966 if (Lex.getCode() == tgtok::less)
1967 if (ParseTemplateArgList(0))
1968 return true;
1969
David Greened34a73b2009-04-24 16:55:41 +00001970 bool inherits = false;
1971
David Greenede444af2009-04-22 16:42:54 +00001972 // If there are submulticlasses, parse them.
1973 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00001974 inherits = true;
1975
David Greenede444af2009-04-22 16:42:54 +00001976 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00001977
David Greenede444af2009-04-22 16:42:54 +00001978 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00001979 SubMultiClassReference SubMultiClass =
1980 ParseSubMultiClassReference(CurMultiClass);
David Greenede444af2009-04-22 16:42:54 +00001981 while (1) {
1982 // Check for error.
1983 if (SubMultiClass.MC == 0) return true;
Bob Wilson32558652009-04-28 19:41:44 +00001984
David Greenede444af2009-04-22 16:42:54 +00001985 // Add it.
1986 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
1987 return true;
Bob Wilson32558652009-04-28 19:41:44 +00001988
David Greenede444af2009-04-22 16:42:54 +00001989 if (Lex.getCode() != tgtok::comma) break;
1990 Lex.Lex(); // eat ','.
1991 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
1992 }
1993 }
1994
David Greened34a73b2009-04-24 16:55:41 +00001995 if (Lex.getCode() != tgtok::l_brace) {
1996 if (!inherits)
1997 return TokError("expected '{' in multiclass definition");
Bob Wilson21870412009-11-22 04:24:42 +00001998 else if (Lex.getCode() != tgtok::semi)
1999 return TokError("expected ';' in multiclass definition");
David Greened34a73b2009-04-24 16:55:41 +00002000 else
Bob Wilson21870412009-11-22 04:24:42 +00002001 Lex.Lex(); // eat the ';'.
2002 } else {
David Greened34a73b2009-04-24 16:55:41 +00002003 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2004 return TokError("multiclass must contain at least one def");
Bob Wilson21870412009-11-22 04:24:42 +00002005
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002006 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002007 switch (Lex.getCode()) {
2008 default:
David Greenea1b1b792011-10-07 18:25:05 +00002009 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002010 case tgtok::Let:
2011 case tgtok::Def:
2012 case tgtok::Defm:
2013 if (ParseObject(CurMultiClass))
2014 return true;
2015 break;
2016 }
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002017 }
David Greened34a73b2009-04-24 16:55:41 +00002018 Lex.Lex(); // eat the '}'.
2019 }
Bob Wilson21870412009-11-22 04:24:42 +00002020
Chris Lattnerf4601652007-11-22 20:49:04 +00002021 CurMultiClass = 0;
2022 return false;
2023}
2024
David Greenee499a2d2011-10-05 22:42:07 +00002025Record *TGParser::
2026InstantiateMulticlassDef(MultiClass &MC,
2027 Record *DefProto,
David Greene7be867e2011-10-19 13:04:31 +00002028 Init *DefmPrefix,
David Greenee499a2d2011-10-05 22:42:07 +00002029 SMLoc DefmPrefixLoc) {
David Greene7be867e2011-10-19 13:04:31 +00002030 // We need to preserve DefProto so it can be reused for later
2031 // instantiations, so create a new Record to inherit from it.
2032
David Greenee499a2d2011-10-05 22:42:07 +00002033 // Add in the defm name. If the defm prefix is empty, give each
2034 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
2035 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
2036 // as a prefix.
David Greenee499a2d2011-10-05 22:42:07 +00002037
David Greene7be867e2011-10-19 13:04:31 +00002038 if (DefmPrefix == 0)
2039 DefmPrefix = StringInit::get(GetNewAnonymousName());
2040
2041 Init *DefName = DefProto->getNameInit();
2042
David Greened3d1cad2011-10-19 13:04:43 +00002043 StringInit *DefNameString = dynamic_cast<StringInit *>(DefName);
David Greene7be867e2011-10-19 13:04:31 +00002044
David Greened3d1cad2011-10-19 13:04:43 +00002045 if (DefNameString != 0) {
2046 // We have a fully expanded string so there are no operators to
2047 // resolve. We should concatenate the given prefix and name.
David Greene7be867e2011-10-19 13:04:31 +00002048 DefName =
2049 BinOpInit::get(BinOpInit::STRCONCAT,
2050 UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2051 StringRecTy::get())->Fold(DefProto, &MC),
2052 DefName, StringRecTy::get())->Fold(DefProto, &MC);
2053 }
David Greene7be867e2011-10-19 13:04:31 +00002054
David Greenee499a2d2011-10-05 22:42:07 +00002055 Record *CurRec = new Record(DefName, DefmPrefixLoc, Records);
2056
2057 SubClassReference Ref;
2058 Ref.RefLoc = DefmPrefixLoc;
2059 Ref.Rec = DefProto;
2060 AddSubClass(CurRec, Ref);
2061
David Greenee5b252f2011-10-19 13:04:35 +00002062 if (DefNameString == 0) {
2063 // We must resolve references to NAME.
2064 if (SetValue(CurRec, Ref.RefLoc, "NAME", std::vector<unsigned>(),
2065 DefmPrefix)) {
2066 Error(DefmPrefixLoc, "Could not resolve "
2067 + CurRec->getNameInitAsString() + ":NAME to '"
2068 + DefmPrefix->getAsUnquotedString() + "'");
2069 return 0;
2070 }
2071
2072 RecordVal *DefNameRV = CurRec->getValue("NAME");
2073 CurRec->resolveReferencesTo(DefNameRV);
2074 }
2075
2076 if (!CurMultiClass) {
2077 // We do this after resolving NAME because before resolution, many
2078 // multiclass defs will have the same name expression. If we are
2079 // currently in a multiclass, it means this defm appears inside a
2080 // multiclass and its name won't be fully resolvable until we see
2081 // the top-level defm. Therefore, we don't add this to the
2082 // RecordKeeper at this point. If we did we could get duplicate
2083 // defs as more than one probably refers to NAME or some other
2084 // common internal placeholder.
2085
2086 // Ensure redefinition doesn't happen.
2087 if (Records.getDef(CurRec->getNameInitAsString())) {
2088 Error(DefmPrefixLoc, "def '" + CurRec->getNameInitAsString() +
2089 "' already defined, instantiating defm with subdef '" +
2090 DefProto->getNameInitAsString() + "'");
2091 return 0;
2092 }
2093
2094 Records.addDef(CurRec);
2095 }
2096
David Greenee499a2d2011-10-05 22:42:07 +00002097 return CurRec;
2098}
2099
2100bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC,
2101 Record *CurRec,
2102 SMLoc DefmPrefixLoc,
2103 SMLoc SubClassLoc,
David Greenee22b3212011-10-19 13:02:42 +00002104 const std::vector<Init *> &TArgs,
David Greenee499a2d2011-10-05 22:42:07 +00002105 std::vector<Init *> &TemplateVals,
2106 bool DeleteArgs) {
2107 // Loop over all of the template arguments, setting them to the specified
2108 // value or leaving them as the default if necessary.
2109 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2110 // Check if a value is specified for this temp-arg.
2111 if (i < TemplateVals.size()) {
2112 // Set it now.
2113 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2114 TemplateVals[i]))
2115 return true;
2116
2117 // Resolve it next.
2118 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2119
2120 if (DeleteArgs)
2121 // Now remove it.
2122 CurRec->removeValue(TArgs[i]);
2123
2124 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2125 return Error(SubClassLoc, "value not specified for template argument #"+
David Greenee22b3212011-10-19 13:02:42 +00002126 utostr(i) + " (" + TArgs[i]->getAsUnquotedString()
2127 + ") of multiclassclass '" + MC.Rec.getNameInitAsString()
2128 + "'");
David Greenee499a2d2011-10-05 22:42:07 +00002129 }
2130 }
2131 return false;
2132}
2133
2134bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2135 Record *CurRec,
2136 Record *DefProto,
2137 SMLoc DefmPrefixLoc) {
2138 // If the mdef is inside a 'let' expression, add to each def.
2139 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2140 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2141 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2142 LetStack[i][j].Bits, LetStack[i][j].Value))
2143 return Error(DefmPrefixLoc, "when instantiating this defm");
2144
David Greenee499a2d2011-10-05 22:42:07 +00002145 // Don't create a top level definition for defm inside multiclasses,
2146 // instead, only update the prototypes and bind the template args
2147 // with the new created definition.
2148 if (CurMultiClass) {
2149 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2150 i != e; ++i)
David Greene22dde7e2011-10-19 13:04:02 +00002151 if (CurMultiClass->DefPrototypes[i]->getNameInit()
2152 == CurRec->getNameInit())
2153 return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
David Greenee499a2d2011-10-05 22:42:07 +00002154 "' already defined in this multiclass!");
2155 CurMultiClass->DefPrototypes.push_back(CurRec);
2156
2157 // Copy the template arguments for the multiclass into the new def.
David Greenee22b3212011-10-19 13:02:42 +00002158 const std::vector<Init *> &TA =
David Greenee499a2d2011-10-05 22:42:07 +00002159 CurMultiClass->Rec.getTemplateArgs();
2160
2161 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2162 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2163 assert(RV && "Template arg doesn't exist?");
2164 CurRec->addValue(*RV);
2165 }
David Greenee499a2d2011-10-05 22:42:07 +00002166 }
2167
2168 return false;
2169}
2170
Chris Lattnerf4601652007-11-22 20:49:04 +00002171/// ParseDefm - Parse the instantiation of a multiclass.
2172///
2173/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2174///
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002175bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002176 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Bob Wilson21870412009-11-22 04:24:42 +00002177
David Greenea9e07dd2011-10-19 13:04:29 +00002178 Init *DefmPrefix = 0;
David Greenea9e07dd2011-10-19 13:04:29 +00002179
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002180 if (Lex.Lex() == tgtok::Id) { // eat the defm.
David Greenea9e07dd2011-10-19 13:04:29 +00002181 DefmPrefix = ParseObjectName(CurMultiClass);
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002182 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +00002183
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002184 SMLoc DefmPrefixLoc = Lex.getLoc();
Chris Lattnerdf72eae2010-10-05 22:51:56 +00002185 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4601652007-11-22 20:49:04 +00002186 return TokError("expected ':' after defm identifier");
Bob Wilson21870412009-11-22 04:24:42 +00002187
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002188 // Keep track of the new generated record definitions.
2189 std::vector<Record*> NewRecDefs;
2190
2191 // This record also inherits from a regular class (non-multiclass)?
2192 bool InheritFromClass = false;
2193
Chris Lattnerf4601652007-11-22 20:49:04 +00002194 // eat the colon.
2195 Lex.Lex();
2196
Chris Lattner1e3a8a42009-06-21 03:39:35 +00002197 SMLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00002198 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene56546132009-04-22 22:17:51 +00002199
2200 while (1) {
2201 if (Ref.Rec == 0) return true;
2202
2203 // To instantiate a multiclass, we need to first get the multiclass, then
2204 // instantiate each def contained in the multiclass with the SubClassRef
2205 // template parameters.
2206 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
2207 assert(MC && "Didn't lookup multiclass correctly?");
David Greene05bce0b2011-07-29 22:43:06 +00002208 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greene56546132009-04-22 22:17:51 +00002209
2210 // Verify that the correct number of template arguments were specified.
David Greenee22b3212011-10-19 13:02:42 +00002211 const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs();
David Greene56546132009-04-22 22:17:51 +00002212 if (TArgs.size() < TemplateVals.size())
2213 return Error(SubClassLoc,
2214 "more template args specified than multiclass expects");
2215
2216 // Loop over all the def's in the multiclass, instantiating each one.
2217 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
2218 Record *DefProto = MC->DefPrototypes[i];
2219
David Greene7be867e2011-10-19 13:04:31 +00002220 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix, DefmPrefixLoc);
Jim Grosbach94f2dc92011-12-02 18:33:03 +00002221 if (!CurRec)
2222 return true;
David Greene065f2592009-05-05 16:28:25 +00002223
David Greenee499a2d2011-10-05 22:42:07 +00002224 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmPrefixLoc, SubClassLoc,
2225 TArgs, TemplateVals, true/*Delete args*/))
2226 return Error(SubClassLoc, "could not instantiate def");
David Greene56546132009-04-22 22:17:51 +00002227
David Greenee499a2d2011-10-05 22:42:07 +00002228 if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmPrefixLoc))
2229 return Error(SubClassLoc, "could not instantiate def");
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002230
2231 NewRecDefs.push_back(CurRec);
David Greene56546132009-04-22 22:17:51 +00002232 }
2233
David Greenee499a2d2011-10-05 22:42:07 +00002234
David Greene56546132009-04-22 22:17:51 +00002235 if (Lex.getCode() != tgtok::comma) break;
2236 Lex.Lex(); // eat ','.
2237
2238 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002239
2240 // A defm can inherit from regular classes (non-multiclass) as
2241 // long as they come in the end of the inheritance list.
2242 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0);
2243
2244 if (InheritFromClass)
2245 break;
2246
David Greene56546132009-04-22 22:17:51 +00002247 Ref = ParseSubClassReference(0, true);
2248 }
2249
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002250 if (InheritFromClass) {
2251 // Process all the classes to inherit as if they were part of a
2252 // regular 'def' and inherit all record values.
2253 SubClassReference SubClass = ParseSubClassReference(0, false);
2254 while (1) {
2255 // Check for error.
2256 if (SubClass.Rec == 0) return true;
2257
2258 // Get the expanded definition prototypes and teach them about
2259 // the record values the current class to inherit has
2260 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2261 Record *CurRec = NewRecDefs[i];
2262
2263 // Add it.
2264 if (AddSubClass(CurRec, SubClass))
2265 return true;
2266
2267 // Process any variables on the let stack.
2268 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2269 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2270 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2271 LetStack[i][j].Bits, LetStack[i][j].Value))
2272 return true;
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002273 }
2274
2275 if (Lex.getCode() != tgtok::comma) break;
2276 Lex.Lex(); // eat ','.
2277 SubClass = ParseSubClassReference(0, false);
2278 }
2279 }
2280
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002281 if (!CurMultiClass)
2282 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
David Greene0d886402011-08-10 18:27:46 +00002283 // See Record::setName(). This resolve step will see any new
2284 // name for the def that might have been created when resolving
2285 // inheritance, values and arguments above.
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002286 NewRecDefs[i]->resolveReferences();
2287
Chris Lattnerf4601652007-11-22 20:49:04 +00002288 if (Lex.getCode() != tgtok::semi)
2289 return TokError("expected ';' at end of defm");
2290 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002291
Chris Lattnerf4601652007-11-22 20:49:04 +00002292 return false;
2293}
2294
2295/// ParseObject
2296/// Object ::= ClassInst
2297/// Object ::= DefInst
2298/// Object ::= MultiClassInst
2299/// Object ::= DefMInst
2300/// Object ::= LETCommand '{' ObjectList '}'
2301/// Object ::= LETCommand Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002302bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002303 switch (Lex.getCode()) {
Chris Lattnerd6d9dd92010-10-31 19:27:15 +00002304 default:
2305 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002306 case tgtok::Let: return ParseTopLevelLet(MC);
2307 case tgtok::Def: return ParseDef(MC);
2308 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4601652007-11-22 20:49:04 +00002309 case tgtok::Class: return ParseClass();
2310 case tgtok::MultiClass: return ParseMultiClass();
2311 }
2312}
2313
2314/// ParseObjectList
2315/// ObjectList :== Object*
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002316bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002317 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002318 if (ParseObject(MC))
Chris Lattnerf4601652007-11-22 20:49:04 +00002319 return true;
2320 }
2321 return false;
2322}
2323
Chris Lattnerf4601652007-11-22 20:49:04 +00002324bool TGParser::ParseFile() {
2325 Lex.Lex(); // Prime the lexer.
2326 if (ParseObjectList()) return true;
Bob Wilson21870412009-11-22 04:24:42 +00002327
Chris Lattnerf4601652007-11-22 20:49:04 +00002328 // If we have unread input at the end of the file, report it.
2329 if (Lex.getCode() == tgtok::Eof)
2330 return false;
Bob Wilson21870412009-11-22 04:24:42 +00002331
Chris Lattnerf4601652007-11-22 20:49:04 +00002332 return TokError("Unexpected input at top level");
2333}
2334