blob: d3bd5cd710c59ea8320465663f59e8c53dc472cf [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"
15#include "Record.h"
16#include "llvm/ADT/StringExtras.h"
Daniel Dunbar1a551802009-07-03 00:10:29 +000017#include <algorithm>
18#include <sstream>
Chris Lattnerf4601652007-11-22 20:49:04 +000019using namespace llvm;
20
21//===----------------------------------------------------------------------===//
22// Support Code for the Semantic Actions.
23//===----------------------------------------------------------------------===//
24
25namespace llvm {
Chris Lattnerf4601652007-11-22 20:49:04 +000026struct SubClassReference {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000027 SMLoc RefLoc;
Chris Lattnerf4601652007-11-22 20:49:04 +000028 Record *Rec;
29 std::vector<Init*> TemplateArgs;
Chris Lattner1c8ae592009-03-13 16:01:53 +000030 SubClassReference() : Rec(0) {}
David Greened34a73b2009-04-24 16:55:41 +000031
Chris Lattnerf4601652007-11-22 20:49:04 +000032 bool isInvalid() const { return Rec == 0; }
33};
David Greenede444af2009-04-22 16:42:54 +000034
35struct SubMultiClassReference {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000036 SMLoc RefLoc;
David Greenede444af2009-04-22 16:42:54 +000037 MultiClass *MC;
38 std::vector<Init*> TemplateArgs;
39 SubMultiClassReference() : MC(0) {}
Bob Wilson32558652009-04-28 19:41:44 +000040
David Greenede444af2009-04-22 16:42:54 +000041 bool isInvalid() const { return MC == 0; }
David Greened34a73b2009-04-24 16:55:41 +000042 void dump() const;
David Greenede444af2009-04-22 16:42:54 +000043};
David Greened34a73b2009-04-24 16:55:41 +000044
45void SubMultiClassReference::dump() const {
Daniel Dunbar1a551802009-07-03 00:10:29 +000046 errs() << "Multiclass:\n";
Bob Wilson21870412009-11-22 04:24:42 +000047
David Greened34a73b2009-04-24 16:55:41 +000048 MC->dump();
Bob Wilson21870412009-11-22 04:24:42 +000049
Daniel Dunbar1a551802009-07-03 00:10:29 +000050 errs() << "Template args:\n";
David Greened34a73b2009-04-24 16:55:41 +000051 for (std::vector<Init *>::const_iterator i = TemplateArgs.begin(),
52 iend = TemplateArgs.end();
53 i != iend;
54 ++i) {
55 (*i)->dump();
56 }
57}
58
Chris Lattnerf4601652007-11-22 20:49:04 +000059} // end namespace llvm
60
Chris Lattner1e3a8a42009-06-21 03:39:35 +000061bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Chris Lattnerf4601652007-11-22 20:49:04 +000062 if (CurRec == 0)
63 CurRec = &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +000064
Chris Lattnerf4601652007-11-22 20:49:04 +000065 if (RecordVal *ERV = CurRec->getValue(RV.getName())) {
66 // The value already exists in the class, treat this as a set.
67 if (ERV->setValue(RV.getValue()))
68 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
69 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson21870412009-11-22 04:24:42 +000070 "previous definition of type '" +
Chris Lattnerf4601652007-11-22 20:49:04 +000071 ERV->getType()->getAsString() + "'");
72 } else {
73 CurRec->addValue(RV);
74 }
75 return false;
76}
77
78/// SetValue -
79/// Return true on error, false on success.
Bob Wilson21870412009-11-22 04:24:42 +000080bool TGParser::SetValue(Record *CurRec, SMLoc Loc, const std::string &ValName,
Chris Lattnerf4601652007-11-22 20:49:04 +000081 const std::vector<unsigned> &BitList, Init *V) {
82 if (!V) return false;
83
84 if (CurRec == 0) CurRec = &CurMultiClass->Rec;
85
86 RecordVal *RV = CurRec->getValue(ValName);
87 if (RV == 0)
88 return Error(Loc, "Value '" + ValName + "' unknown!");
89
90 // Do not allow assignments like 'X = X'. This will just cause infinite loops
91 // in the resolution machinery.
92 if (BitList.empty())
93 if (VarInit *VI = dynamic_cast<VarInit*>(V))
94 if (VI->getName() == ValName)
95 return false;
Bob Wilson21870412009-11-22 04:24:42 +000096
Chris Lattnerf4601652007-11-22 20:49:04 +000097 // If we are assigning to a subset of the bits in the value... then we must be
98 // assigning to a field of BitsRecTy, which must have a BitsInit
99 // initializer.
100 //
101 if (!BitList.empty()) {
102 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
103 if (CurVal == 0)
104 return Error(Loc, "Value '" + ValName + "' is not a bits type");
105
106 // Convert the incoming value to a bits type of the appropriate size...
107 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList.size()));
108 if (BI == 0) {
109 V->convertInitializerTo(new BitsRecTy(BitList.size()));
110 return Error(Loc, "Initializer is not compatible with bit range");
111 }
Bob Wilson21870412009-11-22 04:24:42 +0000112
Chris Lattnerf4601652007-11-22 20:49:04 +0000113 // We should have a BitsInit type now.
114 BitsInit *BInit = dynamic_cast<BitsInit*>(BI);
115 assert(BInit != 0);
116
117 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
118
119 // Loop over bits, assigning values as appropriate.
120 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
121 unsigned Bit = BitList[i];
122 if (NewVal->getBit(Bit))
123 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
124 ValName + "' more than once");
125 NewVal->setBit(Bit, BInit->getBit(i));
126 }
127
128 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
129 if (NewVal->getBit(i) == 0)
130 NewVal->setBit(i, CurVal->getBit(i));
131
132 V = NewVal;
133 }
134
135 if (RV->setValue(V))
Bob Wilson21870412009-11-22 04:24:42 +0000136 return Error(Loc, "Value '" + ValName + "' of type '" +
137 RV->getType()->getAsString() +
Chris Lattner5d814862007-11-22 21:06:59 +0000138 "' is incompatible with initializer '" + V->getAsString() +"'");
Chris Lattnerf4601652007-11-22 20:49:04 +0000139 return false;
140}
141
142/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
143/// args as SubClass's template arguments.
Cedric Venetaff9c272009-02-14 16:06:42 +0000144bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000145 Record *SC = SubClass.Rec;
146 // Add all of the values in the subclass into the current class.
147 const std::vector<RecordVal> &Vals = SC->getValues();
148 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
149 if (AddValue(CurRec, SubClass.RefLoc, Vals[i]))
150 return true;
151
152 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
153
154 // Ensure that an appropriate number of template arguments are specified.
155 if (TArgs.size() < SubClass.TemplateArgs.size())
156 return Error(SubClass.RefLoc, "More template args specified than expected");
Bob Wilson21870412009-11-22 04:24:42 +0000157
Chris Lattnerf4601652007-11-22 20:49:04 +0000158 // Loop over all of the template arguments, setting them to the specified
159 // value or leaving them as the default if necessary.
160 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
161 if (i < SubClass.TemplateArgs.size()) {
162 // If a value is specified for this template arg, set it now.
Bob Wilson21870412009-11-22 04:24:42 +0000163 if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(),
Chris Lattnerf4601652007-11-22 20:49:04 +0000164 SubClass.TemplateArgs[i]))
165 return true;
Bob Wilson21870412009-11-22 04:24:42 +0000166
Chris Lattnerf4601652007-11-22 20:49:04 +0000167 // Resolve it next.
168 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson21870412009-11-22 04:24:42 +0000169
Chris Lattnerf4601652007-11-22 20:49:04 +0000170 // Now remove it.
171 CurRec->removeValue(TArgs[i]);
172
173 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
174 return Error(SubClass.RefLoc,"Value not specified for template argument #"
Bob Wilson21870412009-11-22 04:24:42 +0000175 + utostr(i) + " (" + TArgs[i] + ") of subclass '" +
Chris Lattnerf4601652007-11-22 20:49:04 +0000176 SC->getName() + "'!");
177 }
178 }
179
180 // Since everything went well, we can now set the "superclass" list for the
181 // current record.
182 const std::vector<Record*> &SCs = SC->getSuperClasses();
183 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
184 if (CurRec->isSubClassOf(SCs[i]))
185 return Error(SubClass.RefLoc,
186 "Already subclass of '" + SCs[i]->getName() + "'!\n");
187 CurRec->addSuperClass(SCs[i]);
188 }
Bob Wilson21870412009-11-22 04:24:42 +0000189
Chris Lattnerf4601652007-11-22 20:49:04 +0000190 if (CurRec->isSubClassOf(SC))
191 return Error(SubClass.RefLoc,
192 "Already subclass of '" + SC->getName() + "'!\n");
193 CurRec->addSuperClass(SC);
194 return false;
195}
196
David Greenede444af2009-04-22 16:42:54 +0000197/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilson440548d2009-04-30 18:26:19 +0000198/// CurMC, resolving its template args as SubMultiClass's
David Greenede444af2009-04-22 16:42:54 +0000199/// template arguments.
Bob Wilson440548d2009-04-30 18:26:19 +0000200bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson1d512df2009-04-30 17:46:20 +0000201 SubMultiClassReference &SubMultiClass) {
David Greenede444af2009-04-22 16:42:54 +0000202 MultiClass *SMC = SubMultiClass.MC;
Bob Wilson440548d2009-04-30 18:26:19 +0000203 Record *CurRec = &CurMC->Rec;
David Greenede444af2009-04-22 16:42:54 +0000204
Bob Wilson440548d2009-04-30 18:26:19 +0000205 const std::vector<RecordVal> &MCVals = CurRec->getValues();
David Greenede444af2009-04-22 16:42:54 +0000206
207 // Add all of the values in the subclass into the current class.
208 const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues();
209 for (unsigned i = 0, e = SMCVals.size(); i != e; ++i)
210 if (AddValue(CurRec, SubMultiClass.RefLoc, SMCVals[i]))
211 return true;
212
Bob Wilson440548d2009-04-30 18:26:19 +0000213 int newDefStart = CurMC->DefPrototypes.size();
David Greened34a73b2009-04-24 16:55:41 +0000214
David Greenede444af2009-04-22 16:42:54 +0000215 // Add all of the defs in the subclass into the current multiclass.
216 for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(),
217 iend = SMC->DefPrototypes.end();
218 i != iend;
219 ++i) {
220 // Clone the def and add it to the current multiclass
221 Record *NewDef = new Record(**i);
222
223 // Add all of the values in the superclass into the current def.
224 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
225 if (AddValue(NewDef, SubMultiClass.RefLoc, MCVals[i]))
226 return true;
227
Bob Wilson440548d2009-04-30 18:26:19 +0000228 CurMC->DefPrototypes.push_back(NewDef);
David Greenede444af2009-04-22 16:42:54 +0000229 }
Bob Wilson32558652009-04-28 19:41:44 +0000230
David Greenede444af2009-04-22 16:42:54 +0000231 const std::vector<std::string> &SMCTArgs = SMC->Rec.getTemplateArgs();
232
David Greened34a73b2009-04-24 16:55:41 +0000233 // Ensure that an appropriate number of template arguments are
234 // specified.
David Greenede444af2009-04-22 16:42:54 +0000235 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
David Greened34a73b2009-04-24 16:55:41 +0000236 return Error(SubMultiClass.RefLoc,
237 "More template args specified than expected");
Bob Wilson32558652009-04-28 19:41:44 +0000238
David Greenede444af2009-04-22 16:42:54 +0000239 // Loop over all of the template arguments, setting them to the specified
240 // value or leaving them as the default if necessary.
241 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
242 if (i < SubMultiClass.TemplateArgs.size()) {
David Greened34a73b2009-04-24 16:55:41 +0000243 // If a value is specified for this template arg, set it in the
244 // superclass now.
245 if (SetValue(CurRec, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000246 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000247 SubMultiClass.TemplateArgs[i]))
248 return true;
249
250 // Resolve it next.
251 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson32558652009-04-28 19:41:44 +0000252
David Greenede444af2009-04-22 16:42:54 +0000253 // Now remove it.
254 CurRec->removeValue(SMCTArgs[i]);
255
David Greened34a73b2009-04-24 16:55:41 +0000256 // If a value is specified for this template arg, set it in the
257 // new defs now.
258 for (MultiClass::RecordVector::iterator j =
Bob Wilson440548d2009-04-30 18:26:19 +0000259 CurMC->DefPrototypes.begin() + newDefStart,
260 jend = CurMC->DefPrototypes.end();
David Greenede444af2009-04-22 16:42:54 +0000261 j != jend;
262 ++j) {
263 Record *Def = *j;
264
David Greened34a73b2009-04-24 16:55:41 +0000265 if (SetValue(Def, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000266 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000267 SubMultiClass.TemplateArgs[i]))
268 return true;
269
270 // Resolve it next.
271 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
272
273 // Now remove it
274 Def->removeValue(SMCTArgs[i]);
275 }
276 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
David Greened34a73b2009-04-24 16:55:41 +0000277 return Error(SubMultiClass.RefLoc,
278 "Value not specified for template argument #"
Bob Wilson32558652009-04-28 19:41:44 +0000279 + utostr(i) + " (" + SMCTArgs[i] + ") of subclass '" +
David Greenede444af2009-04-22 16:42:54 +0000280 SMC->Rec.getName() + "'!");
281 }
282 }
283
284 return false;
285}
286
Chris Lattnerf4601652007-11-22 20:49:04 +0000287//===----------------------------------------------------------------------===//
288// Parser Code
289//===----------------------------------------------------------------------===//
290
291/// isObjectStart - Return true if this is a valid first token for an Object.
292static bool isObjectStart(tgtok::TokKind K) {
293 return K == tgtok::Class || K == tgtok::Def ||
Bob Wilson21870412009-11-22 04:24:42 +0000294 K == tgtok::Defm || K == tgtok::Let || K == tgtok::MultiClass;
Chris Lattnerf4601652007-11-22 20:49:04 +0000295}
296
Chris Lattnerdf72eae2010-10-05 22:51:56 +0000297static std::string GetNewAnonymousName() {
298 static unsigned AnonCounter = 0;
299 return "anonymous."+utostr(AnonCounter++);
300}
301
Chris Lattnerf4601652007-11-22 20:49:04 +0000302/// ParseObjectName - If an object name is specified, return it. Otherwise,
303/// return an anonymous name.
304/// ObjectName ::= ID
305/// ObjectName ::= /*empty*/
306///
307std::string TGParser::ParseObjectName() {
Chris Lattnerdf72eae2010-10-05 22:51:56 +0000308 if (Lex.getCode() != tgtok::Id)
309 return GetNewAnonymousName();
310
311 std::string Ret = Lex.getCurStrVal();
312 Lex.Lex();
313 return Ret;
Chris Lattnerf4601652007-11-22 20:49:04 +0000314}
315
316
317/// ParseClassID - Parse and resolve a reference to a class name. This returns
318/// null on error.
319///
320/// ClassID ::= ID
321///
322Record *TGParser::ParseClassID() {
323 if (Lex.getCode() != tgtok::Id) {
324 TokError("expected name for ClassID");
325 return 0;
326 }
Bob Wilson21870412009-11-22 04:24:42 +0000327
Chris Lattnerf4601652007-11-22 20:49:04 +0000328 Record *Result = Records.getClass(Lex.getCurStrVal());
329 if (Result == 0)
330 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson21870412009-11-22 04:24:42 +0000331
Chris Lattnerf4601652007-11-22 20:49:04 +0000332 Lex.Lex();
333 return Result;
334}
335
Bob Wilson32558652009-04-28 19:41:44 +0000336/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
337/// This returns null on error.
David Greenede444af2009-04-22 16:42:54 +0000338///
339/// MultiClassID ::= ID
340///
341MultiClass *TGParser::ParseMultiClassID() {
342 if (Lex.getCode() != tgtok::Id) {
343 TokError("expected name for ClassID");
344 return 0;
345 }
Bob Wilson32558652009-04-28 19:41:44 +0000346
David Greenede444af2009-04-22 16:42:54 +0000347 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
348 if (Result == 0)
349 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson32558652009-04-28 19:41:44 +0000350
David Greenede444af2009-04-22 16:42:54 +0000351 Lex.Lex();
352 return Result;
353}
354
Chris Lattnerf4601652007-11-22 20:49:04 +0000355Record *TGParser::ParseDefmID() {
356 if (Lex.getCode() != tgtok::Id) {
357 TokError("expected multiclass name");
358 return 0;
359 }
Bob Wilson21870412009-11-22 04:24:42 +0000360
Chris Lattnerf4601652007-11-22 20:49:04 +0000361 MultiClass *MC = MultiClasses[Lex.getCurStrVal()];
362 if (MC == 0) {
363 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
364 return 0;
365 }
Bob Wilson21870412009-11-22 04:24:42 +0000366
Chris Lattnerf4601652007-11-22 20:49:04 +0000367 Lex.Lex();
368 return &MC->Rec;
Bob Wilson21870412009-11-22 04:24:42 +0000369}
Chris Lattnerf4601652007-11-22 20:49:04 +0000370
371
372/// ParseSubClassReference - Parse a reference to a subclass or to a templated
373/// subclass. This returns a SubClassRefTy with a null Record* on error.
374///
375/// SubClassRef ::= ClassID
376/// SubClassRef ::= ClassID '<' ValueList '>'
377///
378SubClassReference TGParser::
379ParseSubClassReference(Record *CurRec, bool isDefm) {
380 SubClassReference Result;
381 Result.RefLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +0000382
Chris Lattnerf4601652007-11-22 20:49:04 +0000383 if (isDefm)
384 Result.Rec = ParseDefmID();
385 else
386 Result.Rec = ParseClassID();
387 if (Result.Rec == 0) return Result;
Bob Wilson21870412009-11-22 04:24:42 +0000388
Chris Lattnerf4601652007-11-22 20:49:04 +0000389 // If there is no template arg list, we're done.
390 if (Lex.getCode() != tgtok::less)
391 return Result;
392 Lex.Lex(); // Eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000393
Chris Lattnerf4601652007-11-22 20:49:04 +0000394 if (Lex.getCode() == tgtok::greater) {
395 TokError("subclass reference requires a non-empty list of template values");
396 Result.Rec = 0;
397 return Result;
398 }
Bob Wilson21870412009-11-22 04:24:42 +0000399
David Greenee1b46912009-06-08 20:23:18 +0000400 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4601652007-11-22 20:49:04 +0000401 if (Result.TemplateArgs.empty()) {
402 Result.Rec = 0; // Error parsing value list.
403 return Result;
404 }
Bob Wilson21870412009-11-22 04:24:42 +0000405
Chris Lattnerf4601652007-11-22 20:49:04 +0000406 if (Lex.getCode() != tgtok::greater) {
407 TokError("expected '>' in template value list");
408 Result.Rec = 0;
409 return Result;
410 }
411 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000412
Chris Lattnerf4601652007-11-22 20:49:04 +0000413 return Result;
414}
415
Bob Wilson32558652009-04-28 19:41:44 +0000416/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
417/// templated submulticlass. This returns a SubMultiClassRefTy with a null
418/// Record* on error.
David Greenede444af2009-04-22 16:42:54 +0000419///
420/// SubMultiClassRef ::= MultiClassID
421/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
422///
423SubMultiClassReference TGParser::
424ParseSubMultiClassReference(MultiClass *CurMC) {
425 SubMultiClassReference Result;
426 Result.RefLoc = Lex.getLoc();
Bob Wilson32558652009-04-28 19:41:44 +0000427
David Greenede444af2009-04-22 16:42:54 +0000428 Result.MC = ParseMultiClassID();
429 if (Result.MC == 0) return Result;
Bob Wilson32558652009-04-28 19:41:44 +0000430
David Greenede444af2009-04-22 16:42:54 +0000431 // If there is no template arg list, we're done.
432 if (Lex.getCode() != tgtok::less)
433 return Result;
434 Lex.Lex(); // Eat the '<'
Bob Wilson32558652009-04-28 19:41:44 +0000435
David Greenede444af2009-04-22 16:42:54 +0000436 if (Lex.getCode() == tgtok::greater) {
437 TokError("subclass reference requires a non-empty list of template values");
438 Result.MC = 0;
439 return Result;
440 }
Bob Wilson32558652009-04-28 19:41:44 +0000441
David Greenee1b46912009-06-08 20:23:18 +0000442 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greenede444af2009-04-22 16:42:54 +0000443 if (Result.TemplateArgs.empty()) {
444 Result.MC = 0; // Error parsing value list.
445 return Result;
446 }
Bob Wilson32558652009-04-28 19:41:44 +0000447
David Greenede444af2009-04-22 16:42:54 +0000448 if (Lex.getCode() != tgtok::greater) {
449 TokError("expected '>' in template value list");
450 Result.MC = 0;
451 return Result;
452 }
453 Lex.Lex();
454
455 return Result;
456}
457
Chris Lattnerf4601652007-11-22 20:49:04 +0000458/// ParseRangePiece - Parse a bit/value range.
459/// RangePiece ::= INTVAL
460/// RangePiece ::= INTVAL '-' INTVAL
461/// RangePiece ::= INTVAL INTVAL
462bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner811281e2008-01-10 07:01:53 +0000463 if (Lex.getCode() != tgtok::IntVal) {
464 TokError("expected integer or bitrange");
465 return true;
466 }
Dan Gohman63f97202008-10-17 01:33:43 +0000467 int64_t Start = Lex.getCurIntVal();
468 int64_t End;
Bob Wilson21870412009-11-22 04:24:42 +0000469
Chris Lattnerf4601652007-11-22 20:49:04 +0000470 if (Start < 0)
471 return TokError("invalid range, cannot be negative");
Bob Wilson21870412009-11-22 04:24:42 +0000472
Chris Lattnerf4601652007-11-22 20:49:04 +0000473 switch (Lex.Lex()) { // eat first character.
Bob Wilson21870412009-11-22 04:24:42 +0000474 default:
Chris Lattnerf4601652007-11-22 20:49:04 +0000475 Ranges.push_back(Start);
476 return false;
477 case tgtok::minus:
478 if (Lex.Lex() != tgtok::IntVal) {
479 TokError("expected integer value as end of range");
480 return true;
481 }
482 End = Lex.getCurIntVal();
483 break;
484 case tgtok::IntVal:
485 End = -Lex.getCurIntVal();
486 break;
487 }
Bob Wilson21870412009-11-22 04:24:42 +0000488 if (End < 0)
Chris Lattnerf4601652007-11-22 20:49:04 +0000489 return TokError("invalid range, cannot be negative");
490 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000491
Chris Lattnerf4601652007-11-22 20:49:04 +0000492 // Add to the range.
493 if (Start < End) {
494 for (; Start <= End; ++Start)
495 Ranges.push_back(Start);
496 } else {
497 for (; Start >= End; --Start)
498 Ranges.push_back(Start);
499 }
500 return false;
501}
502
503/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
504///
505/// RangeList ::= RangePiece (',' RangePiece)*
506///
507std::vector<unsigned> TGParser::ParseRangeList() {
508 std::vector<unsigned> Result;
Bob Wilson21870412009-11-22 04:24:42 +0000509
Chris Lattnerf4601652007-11-22 20:49:04 +0000510 // Parse the first piece.
511 if (ParseRangePiece(Result))
512 return std::vector<unsigned>();
513 while (Lex.getCode() == tgtok::comma) {
514 Lex.Lex(); // Eat the comma.
515
516 // Parse the next range piece.
517 if (ParseRangePiece(Result))
518 return std::vector<unsigned>();
519 }
520 return Result;
521}
522
523/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
524/// OptionalRangeList ::= '<' RangeList '>'
525/// OptionalRangeList ::= /*empty*/
526bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
527 if (Lex.getCode() != tgtok::less)
528 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000529
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000530 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000531 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000532
Chris Lattnerf4601652007-11-22 20:49:04 +0000533 // Parse the range list.
534 Ranges = ParseRangeList();
535 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000536
Chris Lattnerf4601652007-11-22 20:49:04 +0000537 if (Lex.getCode() != tgtok::greater) {
538 TokError("expected '>' at end of range list");
539 return Error(StartLoc, "to match this '<'");
540 }
541 Lex.Lex(); // eat the '>'.
542 return false;
543}
544
545/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
546/// OptionalBitList ::= '{' RangeList '}'
547/// OptionalBitList ::= /*empty*/
548bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
549 if (Lex.getCode() != tgtok::l_brace)
550 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000551
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000552 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000553 Lex.Lex(); // eat the '{'
Bob Wilson21870412009-11-22 04:24:42 +0000554
Chris Lattnerf4601652007-11-22 20:49:04 +0000555 // Parse the range list.
556 Ranges = ParseRangeList();
557 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000558
Chris Lattnerf4601652007-11-22 20:49:04 +0000559 if (Lex.getCode() != tgtok::r_brace) {
560 TokError("expected '}' at end of bit list");
561 return Error(StartLoc, "to match this '{'");
562 }
563 Lex.Lex(); // eat the '}'.
564 return false;
565}
566
567
568/// ParseType - Parse and return a tblgen type. This returns null on error.
569///
570/// Type ::= STRING // string type
571/// Type ::= BIT // bit type
572/// Type ::= BITS '<' INTVAL '>' // bits<x> type
573/// Type ::= INT // int type
574/// Type ::= LIST '<' Type '>' // list<x> type
575/// Type ::= CODE // code type
576/// Type ::= DAG // dag type
577/// Type ::= ClassID // Record Type
578///
579RecTy *TGParser::ParseType() {
580 switch (Lex.getCode()) {
581 default: TokError("Unknown token when expecting a type"); return 0;
582 case tgtok::String: Lex.Lex(); return new StringRecTy();
583 case tgtok::Bit: Lex.Lex(); return new BitRecTy();
584 case tgtok::Int: Lex.Lex(); return new IntRecTy();
585 case tgtok::Code: Lex.Lex(); return new CodeRecTy();
586 case tgtok::Dag: Lex.Lex(); return new DagRecTy();
587 case tgtok::Id:
588 if (Record *R = ParseClassID()) return new RecordRecTy(R);
589 return 0;
590 case tgtok::Bits: {
591 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
592 TokError("expected '<' after bits type");
593 return 0;
594 }
595 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
596 TokError("expected integer in bits<n> type");
597 return 0;
598 }
Dan Gohman63f97202008-10-17 01:33:43 +0000599 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4601652007-11-22 20:49:04 +0000600 if (Lex.Lex() != tgtok::greater) { // Eat count.
601 TokError("expected '>' at end of bits<n> type");
602 return 0;
603 }
604 Lex.Lex(); // Eat '>'
605 return new BitsRecTy(Val);
606 }
607 case tgtok::List: {
608 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
609 TokError("expected '<' after list type");
610 return 0;
611 }
612 Lex.Lex(); // Eat '<'
613 RecTy *SubType = ParseType();
614 if (SubType == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +0000615
Chris Lattnerf4601652007-11-22 20:49:04 +0000616 if (Lex.getCode() != tgtok::greater) {
617 TokError("expected '>' at end of list<ty> type");
618 return 0;
619 }
620 Lex.Lex(); // Eat '>'
621 return new ListRecTy(SubType);
622 }
Bob Wilson21870412009-11-22 04:24:42 +0000623 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000624}
625
626/// ParseIDValue - Parse an ID as a value and decode what it means.
627///
628/// IDValue ::= ID [def local value]
629/// IDValue ::= ID [def template arg]
630/// IDValue ::= ID [multiclass local value]
631/// IDValue ::= ID [multiclass template argument]
632/// IDValue ::= ID [def name]
633///
634Init *TGParser::ParseIDValue(Record *CurRec) {
635 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
636 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000637 SMLoc Loc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000638 Lex.Lex();
639 return ParseIDValue(CurRec, Name, Loc);
640}
641
642/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
643/// has already been read.
Bob Wilson21870412009-11-22 04:24:42 +0000644Init *TGParser::ParseIDValue(Record *CurRec,
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000645 const std::string &Name, SMLoc NameLoc) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000646 if (CurRec) {
647 if (const RecordVal *RV = CurRec->getValue(Name))
648 return new VarInit(Name, RV->getType());
Bob Wilson21870412009-11-22 04:24:42 +0000649
Chris Lattnerf4601652007-11-22 20:49:04 +0000650 std::string TemplateArgName = CurRec->getName()+":"+Name;
651 if (CurRec->isTemplateArg(TemplateArgName)) {
652 const RecordVal *RV = CurRec->getValue(TemplateArgName);
653 assert(RV && "Template arg doesn't exist??");
654 return new VarInit(TemplateArgName, RV->getType());
655 }
656 }
Bob Wilson21870412009-11-22 04:24:42 +0000657
Chris Lattnerf4601652007-11-22 20:49:04 +0000658 if (CurMultiClass) {
659 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
660 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
661 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
662 assert(RV && "Template arg doesn't exist??");
663 return new VarInit(MCName, RV->getType());
664 }
665 }
Bob Wilson21870412009-11-22 04:24:42 +0000666
Chris Lattnerf4601652007-11-22 20:49:04 +0000667 if (Record *D = Records.getDef(Name))
668 return new DefInit(D);
669
670 Error(NameLoc, "Variable not defined: '" + Name + "'");
671 return 0;
672}
673
David Greened418c1b2009-05-14 20:54:48 +0000674/// ParseOperation - Parse an operator. This returns null on error.
675///
676/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
677///
678Init *TGParser::ParseOperation(Record *CurRec) {
679 switch (Lex.getCode()) {
680 default:
681 TokError("unknown operation");
682 return 0;
683 break;
David Greene5f9f9ba2009-05-14 22:38:31 +0000684 case tgtok::XCar:
685 case tgtok::XCdr:
686 case tgtok::XNull:
David Greenee6c27de2009-05-14 21:22:49 +0000687 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
688 UnOpInit::UnaryOp Code;
689 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000690
David Greenee6c27de2009-05-14 21:22:49 +0000691 switch (Lex.getCode()) {
692 default: assert(0 && "Unhandled code!");
693 case tgtok::XCast:
694 Lex.Lex(); // eat the operation
695 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000696
David Greenee6c27de2009-05-14 21:22:49 +0000697 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000698
David Greenee6c27de2009-05-14 21:22:49 +0000699 if (Type == 0) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000700 TokError("did not get type for unary operator");
David Greenee6c27de2009-05-14 21:22:49 +0000701 return 0;
702 }
David Greened418c1b2009-05-14 20:54:48 +0000703
David Greenee6c27de2009-05-14 21:22:49 +0000704 break;
David Greene5f9f9ba2009-05-14 22:38:31 +0000705 case tgtok::XCar:
706 Lex.Lex(); // eat the operation
707 Code = UnOpInit::CAR;
708 break;
709 case tgtok::XCdr:
710 Lex.Lex(); // eat the operation
711 Code = UnOpInit::CDR;
712 break;
713 case tgtok::XNull:
714 Lex.Lex(); // eat the operation
715 Code = UnOpInit::LNULL;
716 Type = new IntRecTy;
717 break;
David Greenee6c27de2009-05-14 21:22:49 +0000718 }
719 if (Lex.getCode() != tgtok::l_paren) {
720 TokError("expected '(' after unary operator");
721 return 0;
722 }
723 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000724
David Greenee6c27de2009-05-14 21:22:49 +0000725 Init *LHS = ParseValue(CurRec);
726 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000727
David Greene5f9f9ba2009-05-14 22:38:31 +0000728 if (Code == UnOpInit::CAR
729 || Code == UnOpInit::CDR
730 || Code == UnOpInit::LNULL) {
731 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000732 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greene5f9f9ba2009-05-14 22:38:31 +0000733 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000734 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
735 TokError("expected list or string type argument in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000736 return 0;
737 }
738 if (LHSt) {
739 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
David Greenee1b46912009-06-08 20:23:18 +0000740 StringRecTy *SType = dynamic_cast<StringRecTy*>(LHSt->getType());
741 if (LType == 0 && SType == 0) {
742 TokError("expected list or string type argumnet in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000743 return 0;
744 }
745 }
746
747 if (Code == UnOpInit::CAR
748 || Code == UnOpInit::CDR) {
David Greenee1b46912009-06-08 20:23:18 +0000749 if (LHSl == 0 && LHSt == 0) {
750 TokError("expected list type argumnet in unary operator");
751 return 0;
752 }
Bob Wilson21870412009-11-22 04:24:42 +0000753
David Greene5f9f9ba2009-05-14 22:38:31 +0000754 if (LHSl && LHSl->getSize() == 0) {
755 TokError("empty list argument in unary operator");
756 return 0;
757 }
758 if (LHSl) {
759 Init *Item = LHSl->getElement(0);
760 TypedInit *Itemt = dynamic_cast<TypedInit*>(Item);
761 if (Itemt == 0) {
762 TokError("untyped list element in unary operator");
763 return 0;
764 }
765 if (Code == UnOpInit::CAR) {
766 Type = Itemt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000767 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000768 Type = new ListRecTy(Itemt->getType());
769 }
Bob Wilson21870412009-11-22 04:24:42 +0000770 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000771 assert(LHSt && "expected list type argument in unary operator");
772 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
773 if (LType == 0) {
774 TokError("expected list type argumnet in unary operator");
775 return 0;
776 }
777 if (Code == UnOpInit::CAR) {
778 Type = LType->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +0000779 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000780 Type = LType;
781 }
782 }
783 }
784 }
785
David Greenee6c27de2009-05-14 21:22:49 +0000786 if (Lex.getCode() != tgtok::r_paren) {
787 TokError("expected ')' in unary operator");
788 return 0;
789 }
790 Lex.Lex(); // eat the ')'
791 return (new UnOpInit(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
792 }
David Greened418c1b2009-05-14 20:54:48 +0000793
794 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +0000795 case tgtok::XSRA:
David Greened418c1b2009-05-14 20:54:48 +0000796 case tgtok::XSRL:
797 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +0000798 case tgtok::XEq:
David Greened418c1b2009-05-14 20:54:48 +0000799 case tgtok::XStrConcat:
800 case tgtok::XNameConcat: { // Value ::= !binop '(' Value ',' Value ')'
801 BinOpInit::BinaryOp Code;
802 RecTy *Type = 0;
803
804
805 switch (Lex.getCode()) {
806 default: assert(0 && "Unhandled code!");
Bob Wilson21870412009-11-22 04:24:42 +0000807 case tgtok::XConcat:
David Greened418c1b2009-05-14 20:54:48 +0000808 Lex.Lex(); // eat the operation
809 Code = BinOpInit::CONCAT;
810 Type = new DagRecTy();
811 break;
Bob Wilson21870412009-11-22 04:24:42 +0000812 case tgtok::XSRA:
David Greened418c1b2009-05-14 20:54:48 +0000813 Lex.Lex(); // eat the operation
814 Code = BinOpInit::SRA;
815 Type = new IntRecTy();
816 break;
Bob Wilson21870412009-11-22 04:24:42 +0000817 case tgtok::XSRL:
David Greened418c1b2009-05-14 20:54:48 +0000818 Lex.Lex(); // eat the operation
819 Code = BinOpInit::SRL;
820 Type = new IntRecTy();
821 break;
Bob Wilson21870412009-11-22 04:24:42 +0000822 case tgtok::XSHL:
David Greened418c1b2009-05-14 20:54:48 +0000823 Lex.Lex(); // eat the operation
824 Code = BinOpInit::SHL;
825 Type = new IntRecTy();
826 break;
David Greene6786d5e2010-01-05 19:11:42 +0000827 case tgtok::XEq:
828 Lex.Lex(); // eat the operation
829 Code = BinOpInit::EQ;
830 Type = new IntRecTy();
831 break;
Bob Wilson21870412009-11-22 04:24:42 +0000832 case tgtok::XStrConcat:
David Greened418c1b2009-05-14 20:54:48 +0000833 Lex.Lex(); // eat the operation
834 Code = BinOpInit::STRCONCAT;
835 Type = new StringRecTy();
836 break;
Bob Wilson21870412009-11-22 04:24:42 +0000837 case tgtok::XNameConcat:
David Greened418c1b2009-05-14 20:54:48 +0000838 Lex.Lex(); // eat the operation
839 Code = BinOpInit::NAMECONCAT;
840
841 Type = ParseOperatorType();
842
843 if (Type == 0) {
844 TokError("did not get type for binary operator");
845 return 0;
846 }
847
848 break;
849 }
850 if (Lex.getCode() != tgtok::l_paren) {
851 TokError("expected '(' after binary operator");
852 return 0;
853 }
854 Lex.Lex(); // eat the '('
855
856 Init *LHS = ParseValue(CurRec);
857 if (LHS == 0) return 0;
858
859 if (Lex.getCode() != tgtok::comma) {
860 TokError("expected ',' in binary operator");
861 return 0;
862 }
863 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000864
David Greened418c1b2009-05-14 20:54:48 +0000865 Init *RHS = ParseValue(CurRec);
866 if (RHS == 0) return 0;
867
868 if (Lex.getCode() != tgtok::r_paren) {
869 TokError("expected ')' in binary operator");
870 return 0;
871 }
872 Lex.Lex(); // eat the ')'
873 return (new BinOpInit(Code, LHS, RHS, Type))->Fold(CurRec, CurMultiClass);
874 }
875
David Greene9bea7c82009-05-14 23:26:46 +0000876 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +0000877 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +0000878 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
879 TernOpInit::TernaryOp Code;
880 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000881
882
David Greene4afc5092009-05-14 21:54:42 +0000883 tgtok::TokKind LexCode = Lex.getCode();
884 Lex.Lex(); // eat the operation
885 switch (LexCode) {
886 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000887 case tgtok::XIf:
888 Code = TernOpInit::IF;
889 break;
David Greenebeb31a52009-05-14 22:23:47 +0000890 case tgtok::XForEach:
891 Code = TernOpInit::FOREACH;
892 break;
David Greene4afc5092009-05-14 21:54:42 +0000893 case tgtok::XSubst:
894 Code = TernOpInit::SUBST;
895 break;
896 }
897 if (Lex.getCode() != tgtok::l_paren) {
898 TokError("expected '(' after ternary operator");
899 return 0;
900 }
901 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000902
David Greene4afc5092009-05-14 21:54:42 +0000903 Init *LHS = ParseValue(CurRec);
904 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000905
David Greene4afc5092009-05-14 21:54:42 +0000906 if (Lex.getCode() != tgtok::comma) {
907 TokError("expected ',' in ternary operator");
908 return 0;
909 }
910 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000911
David Greene4afc5092009-05-14 21:54:42 +0000912 Init *MHS = ParseValue(CurRec);
913 if (MHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000914
David Greene4afc5092009-05-14 21:54:42 +0000915 if (Lex.getCode() != tgtok::comma) {
916 TokError("expected ',' in ternary operator");
917 return 0;
918 }
919 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000920
David Greene4afc5092009-05-14 21:54:42 +0000921 Init *RHS = ParseValue(CurRec);
922 if (RHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000923
David Greene4afc5092009-05-14 21:54:42 +0000924 if (Lex.getCode() != tgtok::r_paren) {
925 TokError("expected ')' in binary operator");
926 return 0;
927 }
928 Lex.Lex(); // eat the ')'
David Greened418c1b2009-05-14 20:54:48 +0000929
David Greene4afc5092009-05-14 21:54:42 +0000930 switch (LexCode) {
931 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000932 case tgtok::XIf: {
933 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
934 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
935 if (MHSt == 0 || RHSt == 0) {
936 TokError("could not get type for !if");
937 return 0;
938 }
939 if (MHSt->getType()->typeIsConvertibleTo(RHSt->getType())) {
940 Type = RHSt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000941 } else if (RHSt->getType()->typeIsConvertibleTo(MHSt->getType())) {
David Greene9bea7c82009-05-14 23:26:46 +0000942 Type = MHSt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000943 } else {
David Greene9bea7c82009-05-14 23:26:46 +0000944 TokError("inconsistent types for !if");
945 return 0;
946 }
947 break;
948 }
David Greenebeb31a52009-05-14 22:23:47 +0000949 case tgtok::XForEach: {
950 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
951 if (MHSt == 0) {
952 TokError("could not get type for !foreach");
953 return 0;
954 }
955 Type = MHSt->getType();
956 break;
957 }
David Greene4afc5092009-05-14 21:54:42 +0000958 case tgtok::XSubst: {
959 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
960 if (RHSt == 0) {
961 TokError("could not get type for !subst");
962 return 0;
963 }
964 Type = RHSt->getType();
965 break;
966 }
967 }
Bob Wilson21870412009-11-22 04:24:42 +0000968 return (new TernOpInit(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
969 CurMultiClass);
David Greene4afc5092009-05-14 21:54:42 +0000970 }
David Greened418c1b2009-05-14 20:54:48 +0000971 }
972 TokError("could not parse operation");
973 return 0;
974}
975
976/// ParseOperatorType - Parse a type for an operator. This returns
977/// null on error.
978///
979/// OperatorType ::= '<' Type '>'
980///
Dan Gohmana9ad0412009-08-12 22:10:57 +0000981RecTy *TGParser::ParseOperatorType() {
David Greened418c1b2009-05-14 20:54:48 +0000982 RecTy *Type = 0;
983
984 if (Lex.getCode() != tgtok::less) {
985 TokError("expected type name for operator");
986 return 0;
987 }
988 Lex.Lex(); // eat the <
989
990 Type = ParseType();
991
992 if (Type == 0) {
993 TokError("expected type name for operator");
994 return 0;
995 }
996
997 if (Lex.getCode() != tgtok::greater) {
998 TokError("expected type name for operator");
999 return 0;
1000 }
1001 Lex.Lex(); // eat the >
1002
1003 return Type;
1004}
1005
1006
Chris Lattnerf4601652007-11-22 20:49:04 +00001007/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1008///
1009/// SimpleValue ::= IDValue
1010/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001011/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +00001012/// SimpleValue ::= CODEFRAGMENT
1013/// SimpleValue ::= '?'
1014/// SimpleValue ::= '{' ValueList '}'
1015/// SimpleValue ::= ID '<' ValueListNE '>'
1016/// SimpleValue ::= '[' ValueList ']'
1017/// SimpleValue ::= '(' IDValue DagArgList ')'
1018/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
1019/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1020/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1021/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1022/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1023///
David Greenee1b46912009-06-08 20:23:18 +00001024Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001025 Init *R = 0;
1026 switch (Lex.getCode()) {
1027 default: TokError("Unknown token when parsing a value"); break;
1028 case tgtok::IntVal: R = new IntInit(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001029 case tgtok::StrVal: {
1030 std::string Val = Lex.getCurStrVal();
1031 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001032
Jim Grosbachda4231f2009-03-26 16:17:51 +00001033 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001034 while (Lex.getCode() == tgtok::StrVal) {
1035 Val += Lex.getCurStrVal();
1036 Lex.Lex();
1037 }
Bob Wilson21870412009-11-22 04:24:42 +00001038
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001039 R = new StringInit(Val);
1040 break;
1041 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001042 case tgtok::CodeFragment:
Bob Wilson21870412009-11-22 04:24:42 +00001043 R = new CodeInit(Lex.getCurStrVal()); Lex.Lex(); break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001044 case tgtok::question: R = new UnsetInit(); Lex.Lex(); break;
1045 case tgtok::Id: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001046 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001047 std::string Name = Lex.getCurStrVal();
1048 if (Lex.Lex() != tgtok::less) // consume the Id.
1049 return ParseIDValue(CurRec, Name, NameLoc); // Value ::= IDValue
Bob Wilson21870412009-11-22 04:24:42 +00001050
Chris Lattnerf4601652007-11-22 20:49:04 +00001051 // Value ::= ID '<' ValueListNE '>'
1052 if (Lex.Lex() == tgtok::greater) {
1053 TokError("expected non-empty value list");
1054 return 0;
1055 }
David Greenee1b46912009-06-08 20:23:18 +00001056
Chris Lattnerf4601652007-11-22 20:49:04 +00001057 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1058 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1059 // body.
1060 Record *Class = Records.getClass(Name);
1061 if (!Class) {
1062 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1063 return 0;
1064 }
David Greenee1b46912009-06-08 20:23:18 +00001065
1066 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
1067 if (ValueList.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001068
David Greenee1b46912009-06-08 20:23:18 +00001069 if (Lex.getCode() != tgtok::greater) {
1070 TokError("expected '>' at end of value list");
1071 return 0;
1072 }
1073 Lex.Lex(); // eat the '>'
Bob Wilson21870412009-11-22 04:24:42 +00001074
Chris Lattnerf4601652007-11-22 20:49:04 +00001075 // Create the new record, set it as CurRec temporarily.
1076 static unsigned AnonCounter = 0;
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001077 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),NameLoc);
Chris Lattnerf4601652007-11-22 20:49:04 +00001078 SubClassReference SCRef;
1079 SCRef.RefLoc = NameLoc;
1080 SCRef.Rec = Class;
1081 SCRef.TemplateArgs = ValueList;
1082 // Add info about the subclass to NewRec.
1083 if (AddSubClass(NewRec, SCRef))
1084 return 0;
1085 NewRec->resolveReferences();
1086 Records.addDef(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001087
Chris Lattnerf4601652007-11-22 20:49:04 +00001088 // The result of the expression is a reference to the new record.
1089 return new DefInit(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001090 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001091 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001092 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001093 Lex.Lex(); // eat the '{'
1094 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001095
Chris Lattnerf4601652007-11-22 20:49:04 +00001096 if (Lex.getCode() != tgtok::r_brace) {
1097 Vals = ParseValueList(CurRec);
1098 if (Vals.empty()) return 0;
1099 }
1100 if (Lex.getCode() != tgtok::r_brace) {
1101 TokError("expected '}' at end of bit list value");
1102 return 0;
1103 }
1104 Lex.Lex(); // eat the '}'
Bob Wilson21870412009-11-22 04:24:42 +00001105
Chris Lattnerf4601652007-11-22 20:49:04 +00001106 BitsInit *Result = new BitsInit(Vals.size());
1107 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1108 Init *Bit = Vals[i]->convertInitializerTo(new BitRecTy());
1109 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +00001110 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1111 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +00001112 return 0;
1113 }
1114 Result->setBit(Vals.size()-i-1, Bit);
1115 }
1116 return Result;
1117 }
1118 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1119 Lex.Lex(); // eat the '['
1120 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001121
David Greenee1b46912009-06-08 20:23:18 +00001122 RecTy *DeducedEltTy = 0;
1123 ListRecTy *GivenListTy = 0;
Bob Wilson21870412009-11-22 04:24:42 +00001124
David Greenee1b46912009-06-08 20:23:18 +00001125 if (ItemType != 0) {
1126 ListRecTy *ListType = dynamic_cast<ListRecTy*>(ItemType);
1127 if (ListType == 0) {
1128 std::stringstream s;
Bob Wilson21870412009-11-22 04:24:42 +00001129 s << "Type mismatch for list, expected list type, got "
David Greenee1b46912009-06-08 20:23:18 +00001130 << ItemType->getAsString();
1131 TokError(s.str());
1132 }
1133 GivenListTy = ListType;
Bob Wilson21870412009-11-22 04:24:42 +00001134 }
David Greenee1b46912009-06-08 20:23:18 +00001135
Chris Lattnerf4601652007-11-22 20:49:04 +00001136 if (Lex.getCode() != tgtok::r_square) {
Bob Wilson21870412009-11-22 04:24:42 +00001137 Vals = ParseValueList(CurRec, 0,
1138 GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001139 if (Vals.empty()) return 0;
1140 }
1141 if (Lex.getCode() != tgtok::r_square) {
1142 TokError("expected ']' at end of list value");
1143 return 0;
1144 }
1145 Lex.Lex(); // eat the ']'
David Greenee1b46912009-06-08 20:23:18 +00001146
1147 RecTy *GivenEltTy = 0;
1148 if (Lex.getCode() == tgtok::less) {
1149 // Optional list element type
1150 Lex.Lex(); // eat the '<'
1151
1152 GivenEltTy = ParseType();
1153 if (GivenEltTy == 0) {
1154 // Couldn't parse element type
1155 return 0;
1156 }
1157
1158 if (Lex.getCode() != tgtok::greater) {
1159 TokError("expected '>' at end of list element type");
1160 return 0;
1161 }
1162 Lex.Lex(); // eat the '>'
1163 }
1164
1165 // Check elements
1166 RecTy *EltTy = 0;
1167 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
1168 i != ie;
1169 ++i) {
1170 TypedInit *TArg = dynamic_cast<TypedInit*>(*i);
1171 if (TArg == 0) {
1172 TokError("Untyped list element");
1173 return 0;
1174 }
1175 if (EltTy != 0) {
1176 EltTy = resolveTypes(EltTy, TArg->getType());
1177 if (EltTy == 0) {
1178 TokError("Incompatible types in list elements");
1179 return 0;
1180 }
Bob Wilson21870412009-11-22 04:24:42 +00001181 } else {
David Greenee1b46912009-06-08 20:23:18 +00001182 EltTy = TArg->getType();
1183 }
1184 }
1185
1186 if (GivenEltTy != 0) {
1187 if (EltTy != 0) {
1188 // Verify consistency
1189 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1190 TokError("Incompatible types in list elements");
1191 return 0;
1192 }
1193 }
1194 EltTy = GivenEltTy;
1195 }
1196
1197 if (EltTy == 0) {
1198 if (ItemType == 0) {
1199 TokError("No type for list");
1200 return 0;
1201 }
1202 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +00001203 } else {
David Greenee1b46912009-06-08 20:23:18 +00001204 // Make sure the deduced type is compatible with the given type
1205 if (GivenListTy) {
1206 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1207 TokError("Element type mismatch for list");
1208 return 0;
1209 }
1210 }
1211 DeducedEltTy = EltTy;
1212 }
Bob Wilson21870412009-11-22 04:24:42 +00001213
David Greenee1b46912009-06-08 20:23:18 +00001214 return new ListInit(Vals, DeducedEltTy);
Chris Lattnerf4601652007-11-22 20:49:04 +00001215 }
1216 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1217 Lex.Lex(); // eat the '('
David Greenec7cafcd2009-04-22 20:18:10 +00001218 if (Lex.getCode() != tgtok::Id
David Greenee6c27de2009-05-14 21:22:49 +00001219 && Lex.getCode() != tgtok::XCast
David Greenec7cafcd2009-04-22 20:18:10 +00001220 && Lex.getCode() != tgtok::XNameConcat) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001221 TokError("expected identifier in dag init");
1222 return 0;
1223 }
Bob Wilson21870412009-11-22 04:24:42 +00001224
David Greenec7cafcd2009-04-22 20:18:10 +00001225 Init *Operator = 0;
1226 if (Lex.getCode() == tgtok::Id) {
1227 Operator = ParseIDValue(CurRec);
1228 if (Operator == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001229 } else {
David Greened418c1b2009-05-14 20:54:48 +00001230 Operator = ParseOperation(CurRec);
1231 if (Operator == 0) return 0;
David Greenec7cafcd2009-04-22 20:18:10 +00001232 }
1233
Nate Begeman7cee8172009-03-19 05:21:56 +00001234 // If the operator name is present, parse it.
1235 std::string OperatorName;
1236 if (Lex.getCode() == tgtok::colon) {
1237 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1238 TokError("expected variable name in dag operator");
1239 return 0;
1240 }
1241 OperatorName = Lex.getCurStrVal();
1242 Lex.Lex(); // eat the VarName.
1243 }
Bob Wilson21870412009-11-22 04:24:42 +00001244
Chris Lattnerf4601652007-11-22 20:49:04 +00001245 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
1246 if (Lex.getCode() != tgtok::r_paren) {
1247 DagArgs = ParseDagArgList(CurRec);
1248 if (DagArgs.empty()) return 0;
1249 }
Bob Wilson21870412009-11-22 04:24:42 +00001250
Chris Lattnerf4601652007-11-22 20:49:04 +00001251 if (Lex.getCode() != tgtok::r_paren) {
1252 TokError("expected ')' in dag init");
1253 return 0;
1254 }
1255 Lex.Lex(); // eat the ')'
Bob Wilson21870412009-11-22 04:24:42 +00001256
Nate Begeman7cee8172009-03-19 05:21:56 +00001257 return new DagInit(Operator, OperatorName, DagArgs);
David Greened418c1b2009-05-14 20:54:48 +00001258 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001259 }
Bob Wilson21870412009-11-22 04:24:42 +00001260
David Greene5f9f9ba2009-05-14 22:38:31 +00001261 case tgtok::XCar:
1262 case tgtok::XCdr:
1263 case tgtok::XNull:
David Greenee6c27de2009-05-14 21:22:49 +00001264 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001265 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +00001266 case tgtok::XSRA:
Chris Lattnerf4601652007-11-22 20:49:04 +00001267 case tgtok::XSRL:
1268 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +00001269 case tgtok::XEq:
David Greenec7cafcd2009-04-22 20:18:10 +00001270 case tgtok::XStrConcat:
David Greene4afc5092009-05-14 21:54:42 +00001271 case tgtok::XNameConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene9bea7c82009-05-14 23:26:46 +00001272 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +00001273 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001274 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greened418c1b2009-05-14 20:54:48 +00001275 return ParseOperation(CurRec);
1276 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001277 }
1278 }
Bob Wilson21870412009-11-22 04:24:42 +00001279
Chris Lattnerf4601652007-11-22 20:49:04 +00001280 return R;
1281}
1282
1283/// ParseValue - Parse a tblgen value. This returns null on error.
1284///
1285/// Value ::= SimpleValue ValueSuffix*
1286/// ValueSuffix ::= '{' BitList '}'
1287/// ValueSuffix ::= '[' BitList ']'
1288/// ValueSuffix ::= '.' ID
1289///
David Greenee1b46912009-06-08 20:23:18 +00001290Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType) {
1291 Init *Result = ParseSimpleValue(CurRec, ItemType);
Chris Lattnerf4601652007-11-22 20:49:04 +00001292 if (Result == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001293
Chris Lattnerf4601652007-11-22 20:49:04 +00001294 // Parse the suffixes now if present.
1295 while (1) {
1296 switch (Lex.getCode()) {
1297 default: return Result;
1298 case tgtok::l_brace: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001299 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001300 Lex.Lex(); // eat the '{'
1301 std::vector<unsigned> Ranges = ParseRangeList();
1302 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001303
Chris Lattnerf4601652007-11-22 20:49:04 +00001304 // Reverse the bitlist.
1305 std::reverse(Ranges.begin(), Ranges.end());
1306 Result = Result->convertInitializerBitRange(Ranges);
1307 if (Result == 0) {
1308 Error(CurlyLoc, "Invalid bit range for value");
1309 return 0;
1310 }
Bob Wilson21870412009-11-22 04:24:42 +00001311
Chris Lattnerf4601652007-11-22 20:49:04 +00001312 // Eat the '}'.
1313 if (Lex.getCode() != tgtok::r_brace) {
1314 TokError("expected '}' at end of bit range list");
1315 return 0;
1316 }
1317 Lex.Lex();
1318 break;
1319 }
1320 case tgtok::l_square: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001321 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001322 Lex.Lex(); // eat the '['
1323 std::vector<unsigned> Ranges = ParseRangeList();
1324 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001325
Chris Lattnerf4601652007-11-22 20:49:04 +00001326 Result = Result->convertInitListSlice(Ranges);
1327 if (Result == 0) {
1328 Error(SquareLoc, "Invalid range for list slice");
1329 return 0;
1330 }
Bob Wilson21870412009-11-22 04:24:42 +00001331
Chris Lattnerf4601652007-11-22 20:49:04 +00001332 // Eat the ']'.
1333 if (Lex.getCode() != tgtok::r_square) {
1334 TokError("expected ']' at end of list slice");
1335 return 0;
1336 }
1337 Lex.Lex();
1338 break;
1339 }
1340 case tgtok::period:
1341 if (Lex.Lex() != tgtok::Id) { // eat the .
1342 TokError("expected field identifier after '.'");
1343 return 0;
1344 }
1345 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001346 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00001347 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +00001348 return 0;
1349 }
1350 Result = new FieldInit(Result, Lex.getCurStrVal());
1351 Lex.Lex(); // eat field name
1352 break;
1353 }
1354 }
1355}
1356
1357/// ParseDagArgList - Parse the argument list for a dag literal expression.
1358///
1359/// ParseDagArgList ::= Value (':' VARNAME)?
1360/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
Bob Wilson21870412009-11-22 04:24:42 +00001361std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4601652007-11-22 20:49:04 +00001362TGParser::ParseDagArgList(Record *CurRec) {
1363 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson21870412009-11-22 04:24:42 +00001364
Chris Lattnerf4601652007-11-22 20:49:04 +00001365 while (1) {
1366 Init *Val = ParseValue(CurRec);
1367 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
Bob Wilson21870412009-11-22 04:24:42 +00001368
Chris Lattnerf4601652007-11-22 20:49:04 +00001369 // If the variable name is present, add it.
1370 std::string VarName;
1371 if (Lex.getCode() == tgtok::colon) {
1372 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1373 TokError("expected variable name in dag literal");
1374 return std::vector<std::pair<llvm::Init*, std::string> >();
1375 }
1376 VarName = Lex.getCurStrVal();
1377 Lex.Lex(); // eat the VarName.
1378 }
Bob Wilson21870412009-11-22 04:24:42 +00001379
Chris Lattnerf4601652007-11-22 20:49:04 +00001380 Result.push_back(std::make_pair(Val, VarName));
Bob Wilson21870412009-11-22 04:24:42 +00001381
Chris Lattnerf4601652007-11-22 20:49:04 +00001382 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson21870412009-11-22 04:24:42 +00001383 Lex.Lex(); // eat the ','
Chris Lattnerf4601652007-11-22 20:49:04 +00001384 }
Bob Wilson21870412009-11-22 04:24:42 +00001385
Chris Lattnerf4601652007-11-22 20:49:04 +00001386 return Result;
1387}
1388
1389
1390/// ParseValueList - Parse a comma separated list of values, returning them as a
1391/// vector. Note that this always expects to be able to parse at least one
1392/// value. It returns an empty list if this is not possible.
1393///
1394/// ValueList ::= Value (',' Value)
1395///
Bob Wilson21870412009-11-22 04:24:42 +00001396std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
1397 RecTy *EltTy) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001398 std::vector<Init*> Result;
David Greenee1b46912009-06-08 20:23:18 +00001399 RecTy *ItemType = EltTy;
David Greene67acdf22009-06-29 19:59:52 +00001400 unsigned int ArgN = 0;
David Greenee1b46912009-06-08 20:23:18 +00001401 if (ArgsRec != 0 && EltTy == 0) {
1402 const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs();
1403 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1404 assert(RV && "Template argument record not found??");
1405 ItemType = RV->getType();
1406 ++ArgN;
1407 }
1408 Result.push_back(ParseValue(CurRec, ItemType));
Chris Lattnerf4601652007-11-22 20:49:04 +00001409 if (Result.back() == 0) return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001410
Chris Lattnerf4601652007-11-22 20:49:04 +00001411 while (Lex.getCode() == tgtok::comma) {
1412 Lex.Lex(); // Eat the comma
Bob Wilson21870412009-11-22 04:24:42 +00001413
David Greenee1b46912009-06-08 20:23:18 +00001414 if (ArgsRec != 0 && EltTy == 0) {
1415 const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs();
David Greene67acdf22009-06-29 19:59:52 +00001416 if (ArgN >= TArgs.size()) {
1417 TokError("too many template arguments");
1418 return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001419 }
David Greenee1b46912009-06-08 20:23:18 +00001420 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1421 assert(RV && "Template argument record not found??");
1422 ItemType = RV->getType();
1423 ++ArgN;
1424 }
1425 Result.push_back(ParseValue(CurRec, ItemType));
Chris Lattnerf4601652007-11-22 20:49:04 +00001426 if (Result.back() == 0) return std::vector<Init*>();
1427 }
Bob Wilson21870412009-11-22 04:24:42 +00001428
Chris Lattnerf4601652007-11-22 20:49:04 +00001429 return Result;
1430}
1431
1432
Chris Lattnerf4601652007-11-22 20:49:04 +00001433/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1434/// empty string on error. This can happen in a number of different context's,
1435/// including within a def or in the template args for a def (which which case
1436/// CurRec will be non-null) and within the template args for a multiclass (in
1437/// which case CurRec will be null, but CurMultiClass will be set). This can
1438/// also happen within a def that is within a multiclass, which will set both
1439/// CurRec and CurMultiClass.
1440///
1441/// Declaration ::= FIELD? Type ID ('=' Value)?
1442///
Bob Wilson21870412009-11-22 04:24:42 +00001443std::string TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4601652007-11-22 20:49:04 +00001444 bool ParsingTemplateArgs) {
1445 // Read the field prefix if present.
1446 bool HasField = Lex.getCode() == tgtok::Field;
1447 if (HasField) Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001448
Chris Lattnerf4601652007-11-22 20:49:04 +00001449 RecTy *Type = ParseType();
1450 if (Type == 0) return "";
Bob Wilson21870412009-11-22 04:24:42 +00001451
Chris Lattnerf4601652007-11-22 20:49:04 +00001452 if (Lex.getCode() != tgtok::Id) {
1453 TokError("Expected identifier in declaration");
1454 return "";
1455 }
Bob Wilson21870412009-11-22 04:24:42 +00001456
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001457 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001458 std::string DeclName = Lex.getCurStrVal();
1459 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001460
Chris Lattnerf4601652007-11-22 20:49:04 +00001461 if (ParsingTemplateArgs) {
1462 if (CurRec) {
1463 DeclName = CurRec->getName() + ":" + DeclName;
1464 } else {
1465 assert(CurMultiClass);
1466 }
1467 if (CurMultiClass)
1468 DeclName = CurMultiClass->Rec.getName() + "::" + DeclName;
1469 }
Bob Wilson21870412009-11-22 04:24:42 +00001470
Chris Lattnerf4601652007-11-22 20:49:04 +00001471 // Add the value.
1472 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
1473 return "";
Bob Wilson21870412009-11-22 04:24:42 +00001474
Chris Lattnerf4601652007-11-22 20:49:04 +00001475 // If a value is present, parse it.
1476 if (Lex.getCode() == tgtok::equal) {
1477 Lex.Lex();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001478 SMLoc ValLoc = Lex.getLoc();
David Greenee1b46912009-06-08 20:23:18 +00001479 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001480 if (Val == 0 ||
1481 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
1482 return "";
1483 }
Bob Wilson21870412009-11-22 04:24:42 +00001484
Chris Lattnerf4601652007-11-22 20:49:04 +00001485 return DeclName;
1486}
1487
1488/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1489/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1490/// template args for a def, which may or may not be in a multiclass. If null,
1491/// these are the template args for a multiclass.
1492///
1493/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson21870412009-11-22 04:24:42 +00001494///
Chris Lattnerf4601652007-11-22 20:49:04 +00001495bool TGParser::ParseTemplateArgList(Record *CurRec) {
1496 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1497 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +00001498
Chris Lattnerf4601652007-11-22 20:49:04 +00001499 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +00001500
Chris Lattnerf4601652007-11-22 20:49:04 +00001501 // Read the first declaration.
1502 std::string TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1503 if (TemplArg.empty())
1504 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001505
Chris Lattnerf4601652007-11-22 20:49:04 +00001506 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson21870412009-11-22 04:24:42 +00001507
Chris Lattnerf4601652007-11-22 20:49:04 +00001508 while (Lex.getCode() == tgtok::comma) {
1509 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001510
Chris Lattnerf4601652007-11-22 20:49:04 +00001511 // Read the following declarations.
1512 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1513 if (TemplArg.empty())
1514 return true;
1515 TheRecToAddTo->addTemplateArg(TemplArg);
1516 }
Bob Wilson21870412009-11-22 04:24:42 +00001517
Chris Lattnerf4601652007-11-22 20:49:04 +00001518 if (Lex.getCode() != tgtok::greater)
1519 return TokError("expected '>' at end of template argument list");
1520 Lex.Lex(); // eat the '>'.
1521 return false;
1522}
1523
1524
1525/// ParseBodyItem - Parse a single item at within the body of a def or class.
1526///
1527/// BodyItem ::= Declaration ';'
1528/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1529bool TGParser::ParseBodyItem(Record *CurRec) {
1530 if (Lex.getCode() != tgtok::Let) {
Bob Wilson21870412009-11-22 04:24:42 +00001531 if (ParseDeclaration(CurRec, false).empty())
Chris Lattnerf4601652007-11-22 20:49:04 +00001532 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001533
Chris Lattnerf4601652007-11-22 20:49:04 +00001534 if (Lex.getCode() != tgtok::semi)
1535 return TokError("expected ';' after declaration");
1536 Lex.Lex();
1537 return false;
1538 }
1539
1540 // LET ID OptionalRangeList '=' Value ';'
1541 if (Lex.Lex() != tgtok::Id)
1542 return TokError("expected field identifier after let");
Bob Wilson21870412009-11-22 04:24:42 +00001543
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001544 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001545 std::string FieldName = Lex.getCurStrVal();
1546 Lex.Lex(); // eat the field name.
Bob Wilson21870412009-11-22 04:24:42 +00001547
Chris Lattnerf4601652007-11-22 20:49:04 +00001548 std::vector<unsigned> BitList;
Bob Wilson21870412009-11-22 04:24:42 +00001549 if (ParseOptionalBitList(BitList))
Chris Lattnerf4601652007-11-22 20:49:04 +00001550 return true;
1551 std::reverse(BitList.begin(), BitList.end());
Bob Wilson21870412009-11-22 04:24:42 +00001552
Chris Lattnerf4601652007-11-22 20:49:04 +00001553 if (Lex.getCode() != tgtok::equal)
1554 return TokError("expected '=' in let expression");
1555 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001556
David Greenee1b46912009-06-08 20:23:18 +00001557 RecordVal *Field = CurRec->getValue(FieldName);
1558 if (Field == 0)
1559 return TokError("Value '" + FieldName + "' unknown!");
1560
1561 RecTy *Type = Field->getType();
Bob Wilson21870412009-11-22 04:24:42 +00001562
David Greenee1b46912009-06-08 20:23:18 +00001563 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001564 if (Val == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001565
Chris Lattnerf4601652007-11-22 20:49:04 +00001566 if (Lex.getCode() != tgtok::semi)
1567 return TokError("expected ';' after let expression");
1568 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001569
Chris Lattnerf4601652007-11-22 20:49:04 +00001570 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1571}
1572
1573/// ParseBody - Read the body of a class or def. Return true on error, false on
1574/// success.
1575///
1576/// Body ::= ';'
1577/// Body ::= '{' BodyList '}'
1578/// BodyList BodyItem*
1579///
1580bool TGParser::ParseBody(Record *CurRec) {
1581 // If this is a null definition, just eat the semi and return.
1582 if (Lex.getCode() == tgtok::semi) {
1583 Lex.Lex();
1584 return false;
1585 }
Bob Wilson21870412009-11-22 04:24:42 +00001586
Chris Lattnerf4601652007-11-22 20:49:04 +00001587 if (Lex.getCode() != tgtok::l_brace)
1588 return TokError("Expected ';' or '{' to start body");
1589 // Eat the '{'.
1590 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001591
Chris Lattnerf4601652007-11-22 20:49:04 +00001592 while (Lex.getCode() != tgtok::r_brace)
1593 if (ParseBodyItem(CurRec))
1594 return true;
1595
1596 // Eat the '}'.
1597 Lex.Lex();
1598 return false;
1599}
1600
1601/// ParseObjectBody - Parse the body of a def or class. This consists of an
1602/// optional ClassList followed by a Body. CurRec is the current def or class
1603/// that is being parsed.
1604///
1605/// ObjectBody ::= BaseClassList Body
1606/// BaseClassList ::= /*empty*/
1607/// BaseClassList ::= ':' BaseClassListNE
1608/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1609///
1610bool TGParser::ParseObjectBody(Record *CurRec) {
1611 // If there is a baseclass list, read it.
1612 if (Lex.getCode() == tgtok::colon) {
1613 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001614
Chris Lattnerf4601652007-11-22 20:49:04 +00001615 // Read all of the subclasses.
1616 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1617 while (1) {
1618 // Check for error.
1619 if (SubClass.Rec == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001620
Chris Lattnerf4601652007-11-22 20:49:04 +00001621 // Add it.
1622 if (AddSubClass(CurRec, SubClass))
1623 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001624
Chris Lattnerf4601652007-11-22 20:49:04 +00001625 if (Lex.getCode() != tgtok::comma) break;
1626 Lex.Lex(); // eat ','.
1627 SubClass = ParseSubClassReference(CurRec, false);
1628 }
1629 }
1630
1631 // Process any variables on the let stack.
1632 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1633 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1634 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1635 LetStack[i][j].Bits, LetStack[i][j].Value))
1636 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001637
Chris Lattnerf4601652007-11-22 20:49:04 +00001638 return ParseBody(CurRec);
1639}
1640
Chris Lattnerf4601652007-11-22 20:49:04 +00001641/// ParseDef - Parse and return a top level or multiclass def, return the record
1642/// corresponding to it. This returns null on error.
1643///
1644/// DefInst ::= DEF ObjectName ObjectBody
1645///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001646bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001647 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001648 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson21870412009-11-22 04:24:42 +00001649 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4601652007-11-22 20:49:04 +00001650
1651 // Parse ObjectName and make a record for it.
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001652 Record *CurRec = new Record(ParseObjectName(), DefLoc);
Bob Wilson21870412009-11-22 04:24:42 +00001653
Chris Lattnerf4601652007-11-22 20:49:04 +00001654 if (!CurMultiClass) {
1655 // Top-level def definition.
Bob Wilson21870412009-11-22 04:24:42 +00001656
Chris Lattnerf4601652007-11-22 20:49:04 +00001657 // Ensure redefinition doesn't happen.
1658 if (Records.getDef(CurRec->getName())) {
1659 Error(DefLoc, "def '" + CurRec->getName() + "' already defined");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001660 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001661 }
1662 Records.addDef(CurRec);
1663 } else {
1664 // Otherwise, a def inside a multiclass, add it to the multiclass.
1665 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
1666 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
1667 Error(DefLoc, "def '" + CurRec->getName() +
1668 "' already defined in this multiclass!");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001669 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001670 }
1671 CurMultiClass->DefPrototypes.push_back(CurRec);
1672 }
Bob Wilson21870412009-11-22 04:24:42 +00001673
Chris Lattnerf4601652007-11-22 20:49:04 +00001674 if (ParseObjectBody(CurRec))
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001675 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001676
Chris Lattnerf4601652007-11-22 20:49:04 +00001677 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
1678 CurRec->resolveReferences();
Bob Wilson21870412009-11-22 04:24:42 +00001679
Chris Lattnerf4601652007-11-22 20:49:04 +00001680 // If ObjectBody has template arguments, it's an error.
1681 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001682
1683 if (CurMultiClass) {
1684 // Copy the template arguments for the multiclass into the def.
1685 const std::vector<std::string> &TArgs =
1686 CurMultiClass->Rec.getTemplateArgs();
1687
1688 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1689 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1690 assert(RV && "Template arg doesn't exist?");
1691 CurRec->addValue(*RV);
1692 }
1693 }
1694
1695 return false;
Chris Lattnerf4601652007-11-22 20:49:04 +00001696}
1697
1698
1699/// ParseClass - Parse a tblgen class definition.
1700///
1701/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
1702///
1703bool TGParser::ParseClass() {
1704 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
1705 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001706
Chris Lattnerf4601652007-11-22 20:49:04 +00001707 if (Lex.getCode() != tgtok::Id)
1708 return TokError("expected class name after 'class' keyword");
Bob Wilson21870412009-11-22 04:24:42 +00001709
Chris Lattnerf4601652007-11-22 20:49:04 +00001710 Record *CurRec = Records.getClass(Lex.getCurStrVal());
1711 if (CurRec) {
1712 // If the body was previously defined, this is an error.
1713 if (!CurRec->getValues().empty() ||
1714 !CurRec->getSuperClasses().empty() ||
1715 !CurRec->getTemplateArgs().empty())
1716 return TokError("Class '" + CurRec->getName() + "' already defined");
1717 } else {
1718 // If this is the first reference to this class, create and add it.
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001719 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc());
Chris Lattnerf4601652007-11-22 20:49:04 +00001720 Records.addClass(CurRec);
1721 }
1722 Lex.Lex(); // eat the name.
Bob Wilson21870412009-11-22 04:24:42 +00001723
Chris Lattnerf4601652007-11-22 20:49:04 +00001724 // If there are template args, parse them.
1725 if (Lex.getCode() == tgtok::less)
1726 if (ParseTemplateArgList(CurRec))
1727 return true;
1728
1729 // Finally, parse the object body.
1730 return ParseObjectBody(CurRec);
1731}
1732
1733/// ParseLetList - Parse a non-empty list of assignment expressions into a list
1734/// of LetRecords.
1735///
1736/// LetList ::= LetItem (',' LetItem)*
1737/// LetItem ::= ID OptionalRangeList '=' Value
1738///
1739std::vector<LetRecord> TGParser::ParseLetList() {
1740 std::vector<LetRecord> Result;
Bob Wilson21870412009-11-22 04:24:42 +00001741
Chris Lattnerf4601652007-11-22 20:49:04 +00001742 while (1) {
1743 if (Lex.getCode() != tgtok::Id) {
1744 TokError("expected identifier in let definition");
1745 return std::vector<LetRecord>();
1746 }
1747 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001748 SMLoc NameLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +00001749 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4601652007-11-22 20:49:04 +00001750
1751 // Check for an optional RangeList.
1752 std::vector<unsigned> Bits;
Bob Wilson21870412009-11-22 04:24:42 +00001753 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4601652007-11-22 20:49:04 +00001754 return std::vector<LetRecord>();
1755 std::reverse(Bits.begin(), Bits.end());
Bob Wilson21870412009-11-22 04:24:42 +00001756
Chris Lattnerf4601652007-11-22 20:49:04 +00001757 if (Lex.getCode() != tgtok::equal) {
1758 TokError("expected '=' in let expression");
1759 return std::vector<LetRecord>();
1760 }
1761 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001762
Chris Lattnerf4601652007-11-22 20:49:04 +00001763 Init *Val = ParseValue(0);
1764 if (Val == 0) return std::vector<LetRecord>();
Bob Wilson21870412009-11-22 04:24:42 +00001765
Chris Lattnerf4601652007-11-22 20:49:04 +00001766 // Now that we have everything, add the record.
1767 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson21870412009-11-22 04:24:42 +00001768
Chris Lattnerf4601652007-11-22 20:49:04 +00001769 if (Lex.getCode() != tgtok::comma)
1770 return Result;
Bob Wilson21870412009-11-22 04:24:42 +00001771 Lex.Lex(); // eat the comma.
Chris Lattnerf4601652007-11-22 20:49:04 +00001772 }
1773}
1774
1775/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001776/// different related productions. This works inside multiclasses too.
Chris Lattnerf4601652007-11-22 20:49:04 +00001777///
1778/// Object ::= LET LetList IN '{' ObjectList '}'
1779/// Object ::= LET LetList IN Object
1780///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001781bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001782 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
1783 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001784
Chris Lattnerf4601652007-11-22 20:49:04 +00001785 // Add this entry to the let stack.
1786 std::vector<LetRecord> LetInfo = ParseLetList();
1787 if (LetInfo.empty()) return true;
1788 LetStack.push_back(LetInfo);
1789
1790 if (Lex.getCode() != tgtok::In)
1791 return TokError("expected 'in' at end of top-level 'let'");
1792 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001793
Chris Lattnerf4601652007-11-22 20:49:04 +00001794 // If this is a scalar let, just handle it now
1795 if (Lex.getCode() != tgtok::l_brace) {
1796 // LET LetList IN Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001797 if (ParseObject(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00001798 return true;
1799 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001800 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001801 // Otherwise, this is a group let.
1802 Lex.Lex(); // eat the '{'.
Bob Wilson21870412009-11-22 04:24:42 +00001803
Chris Lattnerf4601652007-11-22 20:49:04 +00001804 // Parse the object list.
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001805 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00001806 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001807
Chris Lattnerf4601652007-11-22 20:49:04 +00001808 if (Lex.getCode() != tgtok::r_brace) {
1809 TokError("expected '}' at end of top level let command");
1810 return Error(BraceLoc, "to match this '{'");
1811 }
1812 Lex.Lex();
1813 }
Bob Wilson21870412009-11-22 04:24:42 +00001814
Chris Lattnerf4601652007-11-22 20:49:04 +00001815 // Outside this let scope, this let block is not active.
1816 LetStack.pop_back();
1817 return false;
1818}
1819
Chris Lattnerf4601652007-11-22 20:49:04 +00001820/// ParseMultiClass - Parse a multiclass definition.
1821///
Bob Wilson32558652009-04-28 19:41:44 +00001822/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
1823/// ':' BaseMultiClassList '{' MultiClassDef+ '}'
Chris Lattnerf4601652007-11-22 20:49:04 +00001824///
1825bool TGParser::ParseMultiClass() {
1826 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
1827 Lex.Lex(); // Eat the multiclass token.
1828
1829 if (Lex.getCode() != tgtok::Id)
1830 return TokError("expected identifier after multiclass for name");
1831 std::string Name = Lex.getCurStrVal();
Bob Wilson21870412009-11-22 04:24:42 +00001832
Chris Lattnerf4601652007-11-22 20:49:04 +00001833 if (MultiClasses.count(Name))
1834 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson21870412009-11-22 04:24:42 +00001835
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001836 CurMultiClass = MultiClasses[Name] = new MultiClass(Name, Lex.getLoc());
Chris Lattnerf4601652007-11-22 20:49:04 +00001837 Lex.Lex(); // Eat the identifier.
Bob Wilson21870412009-11-22 04:24:42 +00001838
Chris Lattnerf4601652007-11-22 20:49:04 +00001839 // If there are template args, parse them.
1840 if (Lex.getCode() == tgtok::less)
1841 if (ParseTemplateArgList(0))
1842 return true;
1843
David Greened34a73b2009-04-24 16:55:41 +00001844 bool inherits = false;
1845
David Greenede444af2009-04-22 16:42:54 +00001846 // If there are submulticlasses, parse them.
1847 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00001848 inherits = true;
1849
David Greenede444af2009-04-22 16:42:54 +00001850 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00001851
David Greenede444af2009-04-22 16:42:54 +00001852 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00001853 SubMultiClassReference SubMultiClass =
1854 ParseSubMultiClassReference(CurMultiClass);
David Greenede444af2009-04-22 16:42:54 +00001855 while (1) {
1856 // Check for error.
1857 if (SubMultiClass.MC == 0) return true;
Bob Wilson32558652009-04-28 19:41:44 +00001858
David Greenede444af2009-04-22 16:42:54 +00001859 // Add it.
1860 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
1861 return true;
Bob Wilson32558652009-04-28 19:41:44 +00001862
David Greenede444af2009-04-22 16:42:54 +00001863 if (Lex.getCode() != tgtok::comma) break;
1864 Lex.Lex(); // eat ','.
1865 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
1866 }
1867 }
1868
David Greened34a73b2009-04-24 16:55:41 +00001869 if (Lex.getCode() != tgtok::l_brace) {
1870 if (!inherits)
1871 return TokError("expected '{' in multiclass definition");
Bob Wilson21870412009-11-22 04:24:42 +00001872 else if (Lex.getCode() != tgtok::semi)
1873 return TokError("expected ';' in multiclass definition");
David Greened34a73b2009-04-24 16:55:41 +00001874 else
Bob Wilson21870412009-11-22 04:24:42 +00001875 Lex.Lex(); // eat the ';'.
1876 } else {
David Greened34a73b2009-04-24 16:55:41 +00001877 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
1878 return TokError("multiclass must contain at least one def");
Bob Wilson21870412009-11-22 04:24:42 +00001879
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001880 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001881 switch (Lex.getCode()) {
1882 default:
1883 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
1884 case tgtok::Let:
1885 case tgtok::Def:
1886 case tgtok::Defm:
1887 if (ParseObject(CurMultiClass))
1888 return true;
1889 break;
1890 }
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001891 }
David Greened34a73b2009-04-24 16:55:41 +00001892 Lex.Lex(); // eat the '}'.
1893 }
Bob Wilson21870412009-11-22 04:24:42 +00001894
Chris Lattnerf4601652007-11-22 20:49:04 +00001895 CurMultiClass = 0;
1896 return false;
1897}
1898
1899/// ParseDefm - Parse the instantiation of a multiclass.
1900///
1901/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
1902///
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001903bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001904 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Bob Wilson21870412009-11-22 04:24:42 +00001905
Chris Lattnerdf72eae2010-10-05 22:51:56 +00001906 std::string DefmPrefix;
1907 if (Lex.Lex() == tgtok::Id) { // eat the defm.
1908 DefmPrefix = Lex.getCurStrVal();
1909 Lex.Lex(); // Eat the defm prefix.
1910 }
1911
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001912 SMLoc DefmPrefixLoc = Lex.getLoc();
Chris Lattnerdf72eae2010-10-05 22:51:56 +00001913 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4601652007-11-22 20:49:04 +00001914 return TokError("expected ':' after defm identifier");
Bob Wilson21870412009-11-22 04:24:42 +00001915
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00001916 // Keep track of the new generated record definitions.
1917 std::vector<Record*> NewRecDefs;
1918
1919 // This record also inherits from a regular class (non-multiclass)?
1920 bool InheritFromClass = false;
1921
Chris Lattnerf4601652007-11-22 20:49:04 +00001922 // eat the colon.
1923 Lex.Lex();
1924
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001925 SMLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001926 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene56546132009-04-22 22:17:51 +00001927
1928 while (1) {
1929 if (Ref.Rec == 0) return true;
1930
1931 // To instantiate a multiclass, we need to first get the multiclass, then
1932 // instantiate each def contained in the multiclass with the SubClassRef
1933 // template parameters.
1934 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
1935 assert(MC && "Didn't lookup multiclass correctly?");
Bob Wilson21870412009-11-22 04:24:42 +00001936 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greene56546132009-04-22 22:17:51 +00001937
1938 // Verify that the correct number of template arguments were specified.
1939 const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs();
1940 if (TArgs.size() < TemplateVals.size())
1941 return Error(SubClassLoc,
1942 "more template args specified than multiclass expects");
1943
1944 // Loop over all the def's in the multiclass, instantiating each one.
1945 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
1946 Record *DefProto = MC->DefPrototypes[i];
1947
Chris Lattnerdf72eae2010-10-05 22:51:56 +00001948 // Add in the defm name. If the defm prefix is empty, give each
1949 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
1950 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
1951 // as a prefix.
David Greene065f2592009-05-05 16:28:25 +00001952 std::string DefName = DefProto->getName();
Chris Lattnerdf72eae2010-10-05 22:51:56 +00001953 if (DefmPrefix.empty()) {
1954 DefName = GetNewAnonymousName();
Bob Wilson21870412009-11-22 04:24:42 +00001955 } else {
Chris Lattnerdf72eae2010-10-05 22:51:56 +00001956 std::string::size_type idx = DefName.find("#NAME#");
1957 if (idx != std::string::npos) {
1958 DefName.replace(idx, 6, DefmPrefix);
1959 } else {
1960 // Add the suffix to the defm name to get the new name.
1961 DefName = DefmPrefix + DefName;
1962 }
David Greene065f2592009-05-05 16:28:25 +00001963 }
1964
1965 Record *CurRec = new Record(DefName, DefmPrefixLoc);
David Greene56546132009-04-22 22:17:51 +00001966
1967 SubClassReference Ref;
1968 Ref.RefLoc = DefmPrefixLoc;
1969 Ref.Rec = DefProto;
1970 AddSubClass(CurRec, Ref);
1971
1972 // Loop over all of the template arguments, setting them to the specified
1973 // value or leaving them as the default if necessary.
1974 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
Bob Wilson32558652009-04-28 19:41:44 +00001975 // Check if a value is specified for this temp-arg.
1976 if (i < TemplateVals.size()) {
David Greene56546132009-04-22 22:17:51 +00001977 // Set it now.
1978 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
1979 TemplateVals[i]))
1980 return true;
1981
1982 // Resolve it next.
1983 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
1984
1985 // Now remove it.
1986 CurRec->removeValue(TArgs[i]);
1987
1988 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Bob Wilson32558652009-04-28 19:41:44 +00001989 return Error(SubClassLoc,
1990 "value not specified for template argument #"+
David Greene56546132009-04-22 22:17:51 +00001991 utostr(i) + " (" + TArgs[i] + ") of multiclassclass '" +
1992 MC->Rec.getName() + "'");
1993 }
1994 }
1995
1996 // If the mdef is inside a 'let' expression, add to each def.
1997 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1998 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1999 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2000 LetStack[i][j].Bits, LetStack[i][j].Value)) {
2001 Error(DefmPrefixLoc, "when instantiating this defm");
2002 return true;
2003 }
2004
2005 // Ensure redefinition doesn't happen.
2006 if (Records.getDef(CurRec->getName()))
Bob Wilson21870412009-11-22 04:24:42 +00002007 return Error(DefmPrefixLoc, "def '" + CurRec->getName() +
2008 "' already defined, instantiating defm with subdef '" +
David Greene56546132009-04-22 22:17:51 +00002009 DefProto->getName() + "'");
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002010
2011 // Don't create a top level definition for defm inside multiclasses,
2012 // instead, only update the prototypes and bind the template args
2013 // with the new created definition.
2014 if (CurMultiClass) {
2015 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2016 i != e; ++i) {
2017 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
2018 Error(DefmPrefixLoc, "defm '" + CurRec->getName() +
2019 "' already defined in this multiclass!");
2020 return 0;
2021 }
2022 }
2023 CurMultiClass->DefPrototypes.push_back(CurRec);
2024
2025 // Copy the template arguments for the multiclass into the new def.
2026 const std::vector<std::string> &TA =
2027 CurMultiClass->Rec.getTemplateArgs();
2028
2029 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2030 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2031 assert(RV && "Template arg doesn't exist?");
2032 CurRec->addValue(*RV);
2033 }
2034 } else {
2035 Records.addDef(CurRec);
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002036 }
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002037
2038 NewRecDefs.push_back(CurRec);
David Greene56546132009-04-22 22:17:51 +00002039 }
2040
2041 if (Lex.getCode() != tgtok::comma) break;
2042 Lex.Lex(); // eat ','.
2043
2044 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002045
2046 // A defm can inherit from regular classes (non-multiclass) as
2047 // long as they come in the end of the inheritance list.
2048 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0);
2049
2050 if (InheritFromClass)
2051 break;
2052
David Greene56546132009-04-22 22:17:51 +00002053 Ref = ParseSubClassReference(0, true);
2054 }
2055
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002056 if (InheritFromClass) {
2057 // Process all the classes to inherit as if they were part of a
2058 // regular 'def' and inherit all record values.
2059 SubClassReference SubClass = ParseSubClassReference(0, false);
2060 while (1) {
2061 // Check for error.
2062 if (SubClass.Rec == 0) return true;
2063
2064 // Get the expanded definition prototypes and teach them about
2065 // the record values the current class to inherit has
2066 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2067 Record *CurRec = NewRecDefs[i];
2068
2069 // Add it.
2070 if (AddSubClass(CurRec, SubClass))
2071 return true;
2072
2073 // Process any variables on the let stack.
2074 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2075 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2076 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2077 LetStack[i][j].Bits, LetStack[i][j].Value))
2078 return true;
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002079 }
2080
2081 if (Lex.getCode() != tgtok::comma) break;
2082 Lex.Lex(); // eat ','.
2083 SubClass = ParseSubClassReference(0, false);
2084 }
2085 }
2086
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002087 if (!CurMultiClass)
2088 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
2089 NewRecDefs[i]->resolveReferences();
2090
Chris Lattnerf4601652007-11-22 20:49:04 +00002091 if (Lex.getCode() != tgtok::semi)
2092 return TokError("expected ';' at end of defm");
2093 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002094
Chris Lattnerf4601652007-11-22 20:49:04 +00002095 return false;
2096}
2097
2098/// ParseObject
2099/// Object ::= ClassInst
2100/// Object ::= DefInst
2101/// Object ::= MultiClassInst
2102/// Object ::= DefMInst
2103/// Object ::= LETCommand '{' ObjectList '}'
2104/// Object ::= LETCommand Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002105bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002106 switch (Lex.getCode()) {
2107 default: assert(0 && "This is not an object");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002108 case tgtok::Let: return ParseTopLevelLet(MC);
2109 case tgtok::Def: return ParseDef(MC);
2110 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4601652007-11-22 20:49:04 +00002111 case tgtok::Class: return ParseClass();
2112 case tgtok::MultiClass: return ParseMultiClass();
2113 }
2114}
2115
2116/// ParseObjectList
2117/// ObjectList :== Object*
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002118bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002119 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002120 if (ParseObject(MC))
Chris Lattnerf4601652007-11-22 20:49:04 +00002121 return true;
2122 }
2123 return false;
2124}
2125
Chris Lattnerf4601652007-11-22 20:49:04 +00002126bool TGParser::ParseFile() {
2127 Lex.Lex(); // Prime the lexer.
2128 if (ParseObjectList()) return true;
Bob Wilson21870412009-11-22 04:24:42 +00002129
Chris Lattnerf4601652007-11-22 20:49:04 +00002130 // If we have unread input at the end of the file, report it.
2131 if (Lex.getCode() == tgtok::Eof)
2132 return false;
Bob Wilson21870412009-11-22 04:24:42 +00002133
Chris Lattnerf4601652007-11-22 20:49:04 +00002134 return TokError("Unexpected input at top level");
2135}
2136