blob: d4095392809d7916194af1a334d9624b5c6ddf1b [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 Lattner8d978a72010-10-05 23:58:18 +000019#include "llvm/ADT/SmallVector.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000020using namespace llvm;
21
22//===----------------------------------------------------------------------===//
23// Support Code for the Semantic Actions.
24//===----------------------------------------------------------------------===//
25
26namespace llvm {
Chris Lattnerf4601652007-11-22 20:49:04 +000027struct SubClassReference {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000028 SMLoc RefLoc;
Chris Lattnerf4601652007-11-22 20:49:04 +000029 Record *Rec;
30 std::vector<Init*> TemplateArgs;
Chris Lattner1c8ae592009-03-13 16:01:53 +000031 SubClassReference() : Rec(0) {}
David Greened34a73b2009-04-24 16:55:41 +000032
Chris Lattnerf4601652007-11-22 20:49:04 +000033 bool isInvalid() const { return Rec == 0; }
34};
David Greenede444af2009-04-22 16:42:54 +000035
36struct SubMultiClassReference {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000037 SMLoc RefLoc;
David Greenede444af2009-04-22 16:42:54 +000038 MultiClass *MC;
39 std::vector<Init*> TemplateArgs;
40 SubMultiClassReference() : MC(0) {}
Bob Wilson32558652009-04-28 19:41:44 +000041
David Greenede444af2009-04-22 16:42:54 +000042 bool isInvalid() const { return MC == 0; }
David Greened34a73b2009-04-24 16:55:41 +000043 void dump() const;
David Greenede444af2009-04-22 16:42:54 +000044};
David Greened34a73b2009-04-24 16:55:41 +000045
46void SubMultiClassReference::dump() const {
Daniel Dunbar1a551802009-07-03 00:10:29 +000047 errs() << "Multiclass:\n";
Bob Wilson21870412009-11-22 04:24:42 +000048
David Greened34a73b2009-04-24 16:55:41 +000049 MC->dump();
Bob Wilson21870412009-11-22 04:24:42 +000050
Daniel Dunbar1a551802009-07-03 00:10:29 +000051 errs() << "Template args:\n";
David Greened34a73b2009-04-24 16:55:41 +000052 for (std::vector<Init *>::const_iterator i = TemplateArgs.begin(),
53 iend = TemplateArgs.end();
54 i != iend;
55 ++i) {
56 (*i)->dump();
57 }
58}
59
Chris Lattnerf4601652007-11-22 20:49:04 +000060} // end namespace llvm
61
Chris Lattner1e3a8a42009-06-21 03:39:35 +000062bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Chris Lattnerf4601652007-11-22 20:49:04 +000063 if (CurRec == 0)
64 CurRec = &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +000065
Chris Lattnerf4601652007-11-22 20:49:04 +000066 if (RecordVal *ERV = CurRec->getValue(RV.getName())) {
67 // The value already exists in the class, treat this as a set.
68 if (ERV->setValue(RV.getValue()))
69 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
70 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson21870412009-11-22 04:24:42 +000071 "previous definition of type '" +
Chris Lattnerf4601652007-11-22 20:49:04 +000072 ERV->getType()->getAsString() + "'");
73 } else {
74 CurRec->addValue(RV);
75 }
76 return false;
77}
78
79/// SetValue -
80/// Return true on error, false on success.
Bob Wilson21870412009-11-22 04:24:42 +000081bool TGParser::SetValue(Record *CurRec, SMLoc Loc, const std::string &ValName,
Chris Lattnerf4601652007-11-22 20:49:04 +000082 const std::vector<unsigned> &BitList, Init *V) {
83 if (!V) return false;
84
85 if (CurRec == 0) CurRec = &CurMultiClass->Rec;
86
87 RecordVal *RV = CurRec->getValue(ValName);
88 if (RV == 0)
89 return Error(Loc, "Value '" + ValName + "' unknown!");
90
91 // Do not allow assignments like 'X = X'. This will just cause infinite loops
92 // in the resolution machinery.
93 if (BitList.empty())
94 if (VarInit *VI = dynamic_cast<VarInit*>(V))
95 if (VI->getName() == ValName)
96 return false;
Bob Wilson21870412009-11-22 04:24:42 +000097
Chris Lattnerf4601652007-11-22 20:49:04 +000098 // If we are assigning to a subset of the bits in the value... then we must be
99 // assigning to a field of BitsRecTy, which must have a BitsInit
100 // initializer.
101 //
102 if (!BitList.empty()) {
103 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
104 if (CurVal == 0)
105 return Error(Loc, "Value '" + ValName + "' is not a bits type");
106
107 // Convert the incoming value to a bits type of the appropriate size...
108 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList.size()));
109 if (BI == 0) {
110 V->convertInitializerTo(new BitsRecTy(BitList.size()));
111 return Error(Loc, "Initializer is not compatible with bit range");
112 }
Bob Wilson21870412009-11-22 04:24:42 +0000113
Chris Lattnerf4601652007-11-22 20:49:04 +0000114 // We should have a BitsInit type now.
115 BitsInit *BInit = dynamic_cast<BitsInit*>(BI);
116 assert(BInit != 0);
117
118 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
119
120 // Loop over bits, assigning values as appropriate.
121 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
122 unsigned Bit = BitList[i];
123 if (NewVal->getBit(Bit))
124 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
125 ValName + "' more than once");
126 NewVal->setBit(Bit, BInit->getBit(i));
127 }
128
129 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
130 if (NewVal->getBit(i) == 0)
131 NewVal->setBit(i, CurVal->getBit(i));
132
133 V = NewVal;
134 }
135
136 if (RV->setValue(V))
Bob Wilson21870412009-11-22 04:24:42 +0000137 return Error(Loc, "Value '" + ValName + "' of type '" +
138 RV->getType()->getAsString() +
Chris Lattner5d814862007-11-22 21:06:59 +0000139 "' is incompatible with initializer '" + V->getAsString() +"'");
Chris Lattnerf4601652007-11-22 20:49:04 +0000140 return false;
141}
142
143/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
144/// args as SubClass's template arguments.
Cedric Venetaff9c272009-02-14 16:06:42 +0000145bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000146 Record *SC = SubClass.Rec;
147 // Add all of the values in the subclass into the current class.
148 const std::vector<RecordVal> &Vals = SC->getValues();
149 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
150 if (AddValue(CurRec, SubClass.RefLoc, Vals[i]))
151 return true;
152
153 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
154
155 // Ensure that an appropriate number of template arguments are specified.
156 if (TArgs.size() < SubClass.TemplateArgs.size())
157 return Error(SubClass.RefLoc, "More template args specified than expected");
Bob Wilson21870412009-11-22 04:24:42 +0000158
Chris Lattnerf4601652007-11-22 20:49:04 +0000159 // Loop over all of the template arguments, setting them to the specified
160 // value or leaving them as the default if necessary.
161 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
162 if (i < SubClass.TemplateArgs.size()) {
163 // If a value is specified for this template arg, set it now.
Bob Wilson21870412009-11-22 04:24:42 +0000164 if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(),
Chris Lattnerf4601652007-11-22 20:49:04 +0000165 SubClass.TemplateArgs[i]))
166 return true;
Bob Wilson21870412009-11-22 04:24:42 +0000167
Chris Lattnerf4601652007-11-22 20:49:04 +0000168 // Resolve it next.
169 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson21870412009-11-22 04:24:42 +0000170
Chris Lattnerf4601652007-11-22 20:49:04 +0000171 // Now remove it.
172 CurRec->removeValue(TArgs[i]);
173
174 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
175 return Error(SubClass.RefLoc,"Value not specified for template argument #"
Bob Wilson21870412009-11-22 04:24:42 +0000176 + utostr(i) + " (" + TArgs[i] + ") of subclass '" +
Chris Lattnerf4601652007-11-22 20:49:04 +0000177 SC->getName() + "'!");
178 }
179 }
180
181 // Since everything went well, we can now set the "superclass" list for the
182 // current record.
183 const std::vector<Record*> &SCs = SC->getSuperClasses();
184 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
185 if (CurRec->isSubClassOf(SCs[i]))
186 return Error(SubClass.RefLoc,
187 "Already subclass of '" + SCs[i]->getName() + "'!\n");
188 CurRec->addSuperClass(SCs[i]);
189 }
Bob Wilson21870412009-11-22 04:24:42 +0000190
Chris Lattnerf4601652007-11-22 20:49:04 +0000191 if (CurRec->isSubClassOf(SC))
192 return Error(SubClass.RefLoc,
193 "Already subclass of '" + SC->getName() + "'!\n");
194 CurRec->addSuperClass(SC);
195 return false;
196}
197
David Greenede444af2009-04-22 16:42:54 +0000198/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilson440548d2009-04-30 18:26:19 +0000199/// CurMC, resolving its template args as SubMultiClass's
David Greenede444af2009-04-22 16:42:54 +0000200/// template arguments.
Bob Wilson440548d2009-04-30 18:26:19 +0000201bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson1d512df2009-04-30 17:46:20 +0000202 SubMultiClassReference &SubMultiClass) {
David Greenede444af2009-04-22 16:42:54 +0000203 MultiClass *SMC = SubMultiClass.MC;
Bob Wilson440548d2009-04-30 18:26:19 +0000204 Record *CurRec = &CurMC->Rec;
David Greenede444af2009-04-22 16:42:54 +0000205
Bob Wilson440548d2009-04-30 18:26:19 +0000206 const std::vector<RecordVal> &MCVals = CurRec->getValues();
David Greenede444af2009-04-22 16:42:54 +0000207
208 // Add all of the values in the subclass into the current class.
209 const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues();
210 for (unsigned i = 0, e = SMCVals.size(); i != e; ++i)
211 if (AddValue(CurRec, SubMultiClass.RefLoc, SMCVals[i]))
212 return true;
213
Bob Wilson440548d2009-04-30 18:26:19 +0000214 int newDefStart = CurMC->DefPrototypes.size();
David Greened34a73b2009-04-24 16:55:41 +0000215
David Greenede444af2009-04-22 16:42:54 +0000216 // Add all of the defs in the subclass into the current multiclass.
217 for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(),
218 iend = SMC->DefPrototypes.end();
219 i != iend;
220 ++i) {
221 // Clone the def and add it to the current multiclass
222 Record *NewDef = new Record(**i);
223
224 // Add all of the values in the superclass into the current def.
225 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
226 if (AddValue(NewDef, SubMultiClass.RefLoc, MCVals[i]))
227 return true;
228
Bob Wilson440548d2009-04-30 18:26:19 +0000229 CurMC->DefPrototypes.push_back(NewDef);
David Greenede444af2009-04-22 16:42:54 +0000230 }
Bob Wilson32558652009-04-28 19:41:44 +0000231
David Greenede444af2009-04-22 16:42:54 +0000232 const std::vector<std::string> &SMCTArgs = SMC->Rec.getTemplateArgs();
233
David Greened34a73b2009-04-24 16:55:41 +0000234 // Ensure that an appropriate number of template arguments are
235 // specified.
David Greenede444af2009-04-22 16:42:54 +0000236 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
David Greened34a73b2009-04-24 16:55:41 +0000237 return Error(SubMultiClass.RefLoc,
238 "More template args specified than expected");
Bob Wilson32558652009-04-28 19:41:44 +0000239
David Greenede444af2009-04-22 16:42:54 +0000240 // Loop over all of the template arguments, setting them to the specified
241 // value or leaving them as the default if necessary.
242 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
243 if (i < SubMultiClass.TemplateArgs.size()) {
David Greened34a73b2009-04-24 16:55:41 +0000244 // If a value is specified for this template arg, set it in the
245 // superclass now.
246 if (SetValue(CurRec, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000247 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000248 SubMultiClass.TemplateArgs[i]))
249 return true;
250
251 // Resolve it next.
252 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson32558652009-04-28 19:41:44 +0000253
David Greenede444af2009-04-22 16:42:54 +0000254 // Now remove it.
255 CurRec->removeValue(SMCTArgs[i]);
256
David Greened34a73b2009-04-24 16:55:41 +0000257 // If a value is specified for this template arg, set it in the
258 // new defs now.
259 for (MultiClass::RecordVector::iterator j =
Bob Wilson440548d2009-04-30 18:26:19 +0000260 CurMC->DefPrototypes.begin() + newDefStart,
261 jend = CurMC->DefPrototypes.end();
David Greenede444af2009-04-22 16:42:54 +0000262 j != jend;
263 ++j) {
264 Record *Def = *j;
265
David Greened34a73b2009-04-24 16:55:41 +0000266 if (SetValue(Def, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000267 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000268 SubMultiClass.TemplateArgs[i]))
269 return true;
270
271 // Resolve it next.
272 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
273
274 // Now remove it
275 Def->removeValue(SMCTArgs[i]);
276 }
277 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
David Greened34a73b2009-04-24 16:55:41 +0000278 return Error(SubMultiClass.RefLoc,
279 "Value not specified for template argument #"
Bob Wilson32558652009-04-28 19:41:44 +0000280 + utostr(i) + " (" + SMCTArgs[i] + ") of subclass '" +
David Greenede444af2009-04-22 16:42:54 +0000281 SMC->Rec.getName() + "'!");
282 }
283 }
284
285 return false;
286}
287
Chris Lattnerf4601652007-11-22 20:49:04 +0000288//===----------------------------------------------------------------------===//
289// Parser Code
290//===----------------------------------------------------------------------===//
291
292/// isObjectStart - Return true if this is a valid first token for an Object.
293static bool isObjectStart(tgtok::TokKind K) {
294 return K == tgtok::Class || K == tgtok::Def ||
Bob Wilson21870412009-11-22 04:24:42 +0000295 K == tgtok::Defm || K == tgtok::Let || K == tgtok::MultiClass;
Chris Lattnerf4601652007-11-22 20:49:04 +0000296}
297
Chris Lattnerdf72eae2010-10-05 22:51:56 +0000298static std::string GetNewAnonymousName() {
299 static unsigned AnonCounter = 0;
300 return "anonymous."+utostr(AnonCounter++);
301}
302
Chris Lattnerf4601652007-11-22 20:49:04 +0000303/// ParseObjectName - If an object name is specified, return it. Otherwise,
304/// return an anonymous name.
305/// ObjectName ::= ID
306/// ObjectName ::= /*empty*/
307///
308std::string TGParser::ParseObjectName() {
Chris Lattnerdf72eae2010-10-05 22:51:56 +0000309 if (Lex.getCode() != tgtok::Id)
310 return GetNewAnonymousName();
311
312 std::string Ret = Lex.getCurStrVal();
313 Lex.Lex();
314 return Ret;
Chris Lattnerf4601652007-11-22 20:49:04 +0000315}
316
317
318/// ParseClassID - Parse and resolve a reference to a class name. This returns
319/// null on error.
320///
321/// ClassID ::= ID
322///
323Record *TGParser::ParseClassID() {
324 if (Lex.getCode() != tgtok::Id) {
325 TokError("expected name for ClassID");
326 return 0;
327 }
Bob Wilson21870412009-11-22 04:24:42 +0000328
Chris Lattnerf4601652007-11-22 20:49:04 +0000329 Record *Result = Records.getClass(Lex.getCurStrVal());
330 if (Result == 0)
331 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson21870412009-11-22 04:24:42 +0000332
Chris Lattnerf4601652007-11-22 20:49:04 +0000333 Lex.Lex();
334 return Result;
335}
336
Bob Wilson32558652009-04-28 19:41:44 +0000337/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
338/// This returns null on error.
David Greenede444af2009-04-22 16:42:54 +0000339///
340/// MultiClassID ::= ID
341///
342MultiClass *TGParser::ParseMultiClassID() {
343 if (Lex.getCode() != tgtok::Id) {
344 TokError("expected name for ClassID");
345 return 0;
346 }
Bob Wilson32558652009-04-28 19:41:44 +0000347
David Greenede444af2009-04-22 16:42:54 +0000348 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
349 if (Result == 0)
350 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson32558652009-04-28 19:41:44 +0000351
David Greenede444af2009-04-22 16:42:54 +0000352 Lex.Lex();
353 return Result;
354}
355
Chris Lattnerf4601652007-11-22 20:49:04 +0000356Record *TGParser::ParseDefmID() {
357 if (Lex.getCode() != tgtok::Id) {
358 TokError("expected multiclass name");
359 return 0;
360 }
Bob Wilson21870412009-11-22 04:24:42 +0000361
Chris Lattnerf4601652007-11-22 20:49:04 +0000362 MultiClass *MC = MultiClasses[Lex.getCurStrVal()];
363 if (MC == 0) {
364 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
365 return 0;
366 }
Bob Wilson21870412009-11-22 04:24:42 +0000367
Chris Lattnerf4601652007-11-22 20:49:04 +0000368 Lex.Lex();
369 return &MC->Rec;
Bob Wilson21870412009-11-22 04:24:42 +0000370}
Chris Lattnerf4601652007-11-22 20:49:04 +0000371
372
373/// ParseSubClassReference - Parse a reference to a subclass or to a templated
374/// subclass. This returns a SubClassRefTy with a null Record* on error.
375///
376/// SubClassRef ::= ClassID
377/// SubClassRef ::= ClassID '<' ValueList '>'
378///
379SubClassReference TGParser::
380ParseSubClassReference(Record *CurRec, bool isDefm) {
381 SubClassReference Result;
382 Result.RefLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +0000383
Chris Lattnerf4601652007-11-22 20:49:04 +0000384 if (isDefm)
385 Result.Rec = ParseDefmID();
386 else
387 Result.Rec = ParseClassID();
388 if (Result.Rec == 0) return Result;
Bob Wilson21870412009-11-22 04:24:42 +0000389
Chris Lattnerf4601652007-11-22 20:49:04 +0000390 // If there is no template arg list, we're done.
391 if (Lex.getCode() != tgtok::less)
392 return Result;
393 Lex.Lex(); // Eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000394
Chris Lattnerf4601652007-11-22 20:49:04 +0000395 if (Lex.getCode() == tgtok::greater) {
396 TokError("subclass reference requires a non-empty list of template values");
397 Result.Rec = 0;
398 return Result;
399 }
Bob Wilson21870412009-11-22 04:24:42 +0000400
David Greenee1b46912009-06-08 20:23:18 +0000401 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4601652007-11-22 20:49:04 +0000402 if (Result.TemplateArgs.empty()) {
403 Result.Rec = 0; // Error parsing value list.
404 return Result;
405 }
Bob Wilson21870412009-11-22 04:24:42 +0000406
Chris Lattnerf4601652007-11-22 20:49:04 +0000407 if (Lex.getCode() != tgtok::greater) {
408 TokError("expected '>' in template value list");
409 Result.Rec = 0;
410 return Result;
411 }
412 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000413
Chris Lattnerf4601652007-11-22 20:49:04 +0000414 return Result;
415}
416
Bob Wilson32558652009-04-28 19:41:44 +0000417/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
418/// templated submulticlass. This returns a SubMultiClassRefTy with a null
419/// Record* on error.
David Greenede444af2009-04-22 16:42:54 +0000420///
421/// SubMultiClassRef ::= MultiClassID
422/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
423///
424SubMultiClassReference TGParser::
425ParseSubMultiClassReference(MultiClass *CurMC) {
426 SubMultiClassReference Result;
427 Result.RefLoc = Lex.getLoc();
Bob Wilson32558652009-04-28 19:41:44 +0000428
David Greenede444af2009-04-22 16:42:54 +0000429 Result.MC = ParseMultiClassID();
430 if (Result.MC == 0) return Result;
Bob Wilson32558652009-04-28 19:41:44 +0000431
David Greenede444af2009-04-22 16:42:54 +0000432 // If there is no template arg list, we're done.
433 if (Lex.getCode() != tgtok::less)
434 return Result;
435 Lex.Lex(); // Eat the '<'
Bob Wilson32558652009-04-28 19:41:44 +0000436
David Greenede444af2009-04-22 16:42:54 +0000437 if (Lex.getCode() == tgtok::greater) {
438 TokError("subclass reference requires a non-empty list of template values");
439 Result.MC = 0;
440 return Result;
441 }
Bob Wilson32558652009-04-28 19:41:44 +0000442
David Greenee1b46912009-06-08 20:23:18 +0000443 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greenede444af2009-04-22 16:42:54 +0000444 if (Result.TemplateArgs.empty()) {
445 Result.MC = 0; // Error parsing value list.
446 return Result;
447 }
Bob Wilson32558652009-04-28 19:41:44 +0000448
David Greenede444af2009-04-22 16:42:54 +0000449 if (Lex.getCode() != tgtok::greater) {
450 TokError("expected '>' in template value list");
451 Result.MC = 0;
452 return Result;
453 }
454 Lex.Lex();
455
456 return Result;
457}
458
Chris Lattnerf4601652007-11-22 20:49:04 +0000459/// ParseRangePiece - Parse a bit/value range.
460/// RangePiece ::= INTVAL
461/// RangePiece ::= INTVAL '-' INTVAL
462/// RangePiece ::= INTVAL INTVAL
463bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner811281e2008-01-10 07:01:53 +0000464 if (Lex.getCode() != tgtok::IntVal) {
465 TokError("expected integer or bitrange");
466 return true;
467 }
Dan Gohman63f97202008-10-17 01:33:43 +0000468 int64_t Start = Lex.getCurIntVal();
469 int64_t End;
Bob Wilson21870412009-11-22 04:24:42 +0000470
Chris Lattnerf4601652007-11-22 20:49:04 +0000471 if (Start < 0)
472 return TokError("invalid range, cannot be negative");
Bob Wilson21870412009-11-22 04:24:42 +0000473
Chris Lattnerf4601652007-11-22 20:49:04 +0000474 switch (Lex.Lex()) { // eat first character.
Bob Wilson21870412009-11-22 04:24:42 +0000475 default:
Chris Lattnerf4601652007-11-22 20:49:04 +0000476 Ranges.push_back(Start);
477 return false;
478 case tgtok::minus:
479 if (Lex.Lex() != tgtok::IntVal) {
480 TokError("expected integer value as end of range");
481 return true;
482 }
483 End = Lex.getCurIntVal();
484 break;
485 case tgtok::IntVal:
486 End = -Lex.getCurIntVal();
487 break;
488 }
Bob Wilson21870412009-11-22 04:24:42 +0000489 if (End < 0)
Chris Lattnerf4601652007-11-22 20:49:04 +0000490 return TokError("invalid range, cannot be negative");
491 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000492
Chris Lattnerf4601652007-11-22 20:49:04 +0000493 // Add to the range.
494 if (Start < End) {
495 for (; Start <= End; ++Start)
496 Ranges.push_back(Start);
497 } else {
498 for (; Start >= End; --Start)
499 Ranges.push_back(Start);
500 }
501 return false;
502}
503
504/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
505///
506/// RangeList ::= RangePiece (',' RangePiece)*
507///
508std::vector<unsigned> TGParser::ParseRangeList() {
509 std::vector<unsigned> Result;
Bob Wilson21870412009-11-22 04:24:42 +0000510
Chris Lattnerf4601652007-11-22 20:49:04 +0000511 // Parse the first piece.
512 if (ParseRangePiece(Result))
513 return std::vector<unsigned>();
514 while (Lex.getCode() == tgtok::comma) {
515 Lex.Lex(); // Eat the comma.
516
517 // Parse the next range piece.
518 if (ParseRangePiece(Result))
519 return std::vector<unsigned>();
520 }
521 return Result;
522}
523
524/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
525/// OptionalRangeList ::= '<' RangeList '>'
526/// OptionalRangeList ::= /*empty*/
527bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
528 if (Lex.getCode() != tgtok::less)
529 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000530
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000531 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000532 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000533
Chris Lattnerf4601652007-11-22 20:49:04 +0000534 // Parse the range list.
535 Ranges = ParseRangeList();
536 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000537
Chris Lattnerf4601652007-11-22 20:49:04 +0000538 if (Lex.getCode() != tgtok::greater) {
539 TokError("expected '>' at end of range list");
540 return Error(StartLoc, "to match this '<'");
541 }
542 Lex.Lex(); // eat the '>'.
543 return false;
544}
545
546/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
547/// OptionalBitList ::= '{' RangeList '}'
548/// OptionalBitList ::= /*empty*/
549bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
550 if (Lex.getCode() != tgtok::l_brace)
551 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000552
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000553 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000554 Lex.Lex(); // eat the '{'
Bob Wilson21870412009-11-22 04:24:42 +0000555
Chris Lattnerf4601652007-11-22 20:49:04 +0000556 // Parse the range list.
557 Ranges = ParseRangeList();
558 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000559
Chris Lattnerf4601652007-11-22 20:49:04 +0000560 if (Lex.getCode() != tgtok::r_brace) {
561 TokError("expected '}' at end of bit list");
562 return Error(StartLoc, "to match this '{'");
563 }
564 Lex.Lex(); // eat the '}'.
565 return false;
566}
567
568
569/// ParseType - Parse and return a tblgen type. This returns null on error.
570///
571/// Type ::= STRING // string type
572/// Type ::= BIT // bit type
573/// Type ::= BITS '<' INTVAL '>' // bits<x> type
574/// Type ::= INT // int type
575/// Type ::= LIST '<' Type '>' // list<x> type
576/// Type ::= CODE // code type
577/// Type ::= DAG // dag type
578/// Type ::= ClassID // Record Type
579///
580RecTy *TGParser::ParseType() {
581 switch (Lex.getCode()) {
582 default: TokError("Unknown token when expecting a type"); return 0;
583 case tgtok::String: Lex.Lex(); return new StringRecTy();
584 case tgtok::Bit: Lex.Lex(); return new BitRecTy();
585 case tgtok::Int: Lex.Lex(); return new IntRecTy();
586 case tgtok::Code: Lex.Lex(); return new CodeRecTy();
587 case tgtok::Dag: Lex.Lex(); return new DagRecTy();
588 case tgtok::Id:
589 if (Record *R = ParseClassID()) return new RecordRecTy(R);
590 return 0;
591 case tgtok::Bits: {
592 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
593 TokError("expected '<' after bits type");
594 return 0;
595 }
596 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
597 TokError("expected integer in bits<n> type");
598 return 0;
599 }
Dan Gohman63f97202008-10-17 01:33:43 +0000600 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4601652007-11-22 20:49:04 +0000601 if (Lex.Lex() != tgtok::greater) { // Eat count.
602 TokError("expected '>' at end of bits<n> type");
603 return 0;
604 }
605 Lex.Lex(); // Eat '>'
606 return new BitsRecTy(Val);
607 }
608 case tgtok::List: {
609 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
610 TokError("expected '<' after list type");
611 return 0;
612 }
613 Lex.Lex(); // Eat '<'
614 RecTy *SubType = ParseType();
615 if (SubType == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +0000616
Chris Lattnerf4601652007-11-22 20:49:04 +0000617 if (Lex.getCode() != tgtok::greater) {
618 TokError("expected '>' at end of list<ty> type");
619 return 0;
620 }
621 Lex.Lex(); // Eat '>'
622 return new ListRecTy(SubType);
623 }
Bob Wilson21870412009-11-22 04:24:42 +0000624 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000625}
626
627/// ParseIDValue - Parse an ID as a value and decode what it means.
628///
629/// IDValue ::= ID [def local value]
630/// IDValue ::= ID [def template arg]
631/// IDValue ::= ID [multiclass local value]
632/// IDValue ::= ID [multiclass template argument]
633/// IDValue ::= ID [def name]
634///
635Init *TGParser::ParseIDValue(Record *CurRec) {
636 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
637 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000638 SMLoc Loc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000639 Lex.Lex();
640 return ParseIDValue(CurRec, Name, Loc);
641}
642
643/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
644/// has already been read.
Bob Wilson21870412009-11-22 04:24:42 +0000645Init *TGParser::ParseIDValue(Record *CurRec,
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000646 const std::string &Name, SMLoc NameLoc) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000647 if (CurRec) {
648 if (const RecordVal *RV = CurRec->getValue(Name))
649 return new VarInit(Name, RV->getType());
Bob Wilson21870412009-11-22 04:24:42 +0000650
Chris Lattnerf4601652007-11-22 20:49:04 +0000651 std::string TemplateArgName = CurRec->getName()+":"+Name;
652 if (CurRec->isTemplateArg(TemplateArgName)) {
653 const RecordVal *RV = CurRec->getValue(TemplateArgName);
654 assert(RV && "Template arg doesn't exist??");
655 return new VarInit(TemplateArgName, RV->getType());
656 }
657 }
Bob Wilson21870412009-11-22 04:24:42 +0000658
Chris Lattnerf4601652007-11-22 20:49:04 +0000659 if (CurMultiClass) {
660 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
661 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
662 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
663 assert(RV && "Template arg doesn't exist??");
664 return new VarInit(MCName, RV->getType());
665 }
666 }
Bob Wilson21870412009-11-22 04:24:42 +0000667
Chris Lattnerf4601652007-11-22 20:49:04 +0000668 if (Record *D = Records.getDef(Name))
669 return new DefInit(D);
670
671 Error(NameLoc, "Variable not defined: '" + Name + "'");
672 return 0;
673}
674
David Greened418c1b2009-05-14 20:54:48 +0000675/// ParseOperation - Parse an operator. This returns null on error.
676///
677/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
678///
679Init *TGParser::ParseOperation(Record *CurRec) {
680 switch (Lex.getCode()) {
681 default:
682 TokError("unknown operation");
683 return 0;
684 break;
David Greene5f9f9ba2009-05-14 22:38:31 +0000685 case tgtok::XCar:
686 case tgtok::XCdr:
687 case tgtok::XNull:
David Greenee6c27de2009-05-14 21:22:49 +0000688 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
689 UnOpInit::UnaryOp Code;
690 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000691
David Greenee6c27de2009-05-14 21:22:49 +0000692 switch (Lex.getCode()) {
693 default: assert(0 && "Unhandled code!");
694 case tgtok::XCast:
695 Lex.Lex(); // eat the operation
696 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000697
David Greenee6c27de2009-05-14 21:22:49 +0000698 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000699
David Greenee6c27de2009-05-14 21:22:49 +0000700 if (Type == 0) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000701 TokError("did not get type for unary operator");
David Greenee6c27de2009-05-14 21:22:49 +0000702 return 0;
703 }
David Greened418c1b2009-05-14 20:54:48 +0000704
David Greenee6c27de2009-05-14 21:22:49 +0000705 break;
David Greene5f9f9ba2009-05-14 22:38:31 +0000706 case tgtok::XCar:
707 Lex.Lex(); // eat the operation
708 Code = UnOpInit::CAR;
709 break;
710 case tgtok::XCdr:
711 Lex.Lex(); // eat the operation
712 Code = UnOpInit::CDR;
713 break;
714 case tgtok::XNull:
715 Lex.Lex(); // eat the operation
716 Code = UnOpInit::LNULL;
717 Type = new IntRecTy;
718 break;
David Greenee6c27de2009-05-14 21:22:49 +0000719 }
720 if (Lex.getCode() != tgtok::l_paren) {
721 TokError("expected '(' after unary operator");
722 return 0;
723 }
724 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000725
David Greenee6c27de2009-05-14 21:22:49 +0000726 Init *LHS = ParseValue(CurRec);
727 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000728
David Greene5f9f9ba2009-05-14 22:38:31 +0000729 if (Code == UnOpInit::CAR
730 || Code == UnOpInit::CDR
731 || Code == UnOpInit::LNULL) {
732 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000733 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greene5f9f9ba2009-05-14 22:38:31 +0000734 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000735 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
736 TokError("expected list or string type argument in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000737 return 0;
738 }
739 if (LHSt) {
740 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
David Greenee1b46912009-06-08 20:23:18 +0000741 StringRecTy *SType = dynamic_cast<StringRecTy*>(LHSt->getType());
742 if (LType == 0 && SType == 0) {
743 TokError("expected list or string type argumnet in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000744 return 0;
745 }
746 }
747
748 if (Code == UnOpInit::CAR
749 || Code == UnOpInit::CDR) {
David Greenee1b46912009-06-08 20:23:18 +0000750 if (LHSl == 0 && LHSt == 0) {
751 TokError("expected list type argumnet in unary operator");
752 return 0;
753 }
Bob Wilson21870412009-11-22 04:24:42 +0000754
David Greene5f9f9ba2009-05-14 22:38:31 +0000755 if (LHSl && LHSl->getSize() == 0) {
756 TokError("empty list argument in unary operator");
757 return 0;
758 }
759 if (LHSl) {
760 Init *Item = LHSl->getElement(0);
761 TypedInit *Itemt = dynamic_cast<TypedInit*>(Item);
762 if (Itemt == 0) {
763 TokError("untyped list element in unary operator");
764 return 0;
765 }
766 if (Code == UnOpInit::CAR) {
767 Type = Itemt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000768 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000769 Type = new ListRecTy(Itemt->getType());
770 }
Bob Wilson21870412009-11-22 04:24:42 +0000771 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000772 assert(LHSt && "expected list type argument in unary operator");
773 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
774 if (LType == 0) {
775 TokError("expected list type argumnet in unary operator");
776 return 0;
777 }
778 if (Code == UnOpInit::CAR) {
779 Type = LType->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +0000780 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000781 Type = LType;
782 }
783 }
784 }
785 }
786
David Greenee6c27de2009-05-14 21:22:49 +0000787 if (Lex.getCode() != tgtok::r_paren) {
788 TokError("expected ')' in unary operator");
789 return 0;
790 }
791 Lex.Lex(); // eat the ')'
792 return (new UnOpInit(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
793 }
David Greened418c1b2009-05-14 20:54:48 +0000794
795 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +0000796 case tgtok::XSRA:
David Greened418c1b2009-05-14 20:54:48 +0000797 case tgtok::XSRL:
798 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +0000799 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +0000800 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000801 tgtok::TokKind OpTok = Lex.getCode();
802 SMLoc OpLoc = Lex.getLoc();
803 Lex.Lex(); // eat the operation
804
David Greened418c1b2009-05-14 20:54:48 +0000805 BinOpInit::BinaryOp Code;
806 RecTy *Type = 0;
807
Chris Lattner8d978a72010-10-05 23:58:18 +0000808 switch (OpTok) {
David Greened418c1b2009-05-14 20:54:48 +0000809 default: assert(0 && "Unhandled code!");
Chris Lattnerc7252ce2010-10-06 00:19:21 +0000810 case tgtok::XConcat: Code = BinOpInit::CONCAT; Type = new DagRecTy(); break;
811 case tgtok::XSRA: Code = BinOpInit::SRA; Type = new IntRecTy(); break;
812 case tgtok::XSRL: Code = BinOpInit::SRL; Type = new IntRecTy(); break;
813 case tgtok::XSHL: Code = BinOpInit::SHL; Type = new IntRecTy(); break;
814 case tgtok::XEq: Code = BinOpInit::EQ; Type = new IntRecTy(); break;
Bob Wilson21870412009-11-22 04:24:42 +0000815 case tgtok::XStrConcat:
David Greened418c1b2009-05-14 20:54:48 +0000816 Code = BinOpInit::STRCONCAT;
817 Type = new StringRecTy();
818 break;
David Greened418c1b2009-05-14 20:54:48 +0000819 }
Chris Lattnerc7252ce2010-10-06 00:19:21 +0000820
David Greened418c1b2009-05-14 20:54:48 +0000821 if (Lex.getCode() != tgtok::l_paren) {
822 TokError("expected '(' after binary operator");
823 return 0;
824 }
825 Lex.Lex(); // eat the '('
826
Chris Lattner8d978a72010-10-05 23:58:18 +0000827 SmallVector<Init*, 2> InitList;
828
829 InitList.push_back(ParseValue(CurRec));
830 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000831
Chris Lattner8d978a72010-10-05 23:58:18 +0000832 while (Lex.getCode() == tgtok::comma) {
833 Lex.Lex(); // eat the ','
834
835 InitList.push_back(ParseValue(CurRec));
836 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000837 }
David Greened418c1b2009-05-14 20:54:48 +0000838
839 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner8d978a72010-10-05 23:58:18 +0000840 TokError("expected ')' in operator");
David Greened418c1b2009-05-14 20:54:48 +0000841 return 0;
842 }
843 Lex.Lex(); // eat the ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000844
845 // We allow multiple operands to associative operators like !strconcat as
846 // shorthand for nesting them.
847 if (Code == BinOpInit::STRCONCAT) {
848 while (InitList.size() > 2) {
849 Init *RHS = InitList.pop_back_val();
850 RHS = (new BinOpInit(Code, InitList.back(), RHS, Type))
851 ->Fold(CurRec, CurMultiClass);
852 InitList.back() = RHS;
853 }
854 }
855
856 if (InitList.size() == 2)
857 return (new BinOpInit(Code, InitList[0], InitList[1], Type))
858 ->Fold(CurRec, CurMultiClass);
859
860 Error(OpLoc, "expected two operands to operator");
861 return 0;
David Greened418c1b2009-05-14 20:54:48 +0000862 }
863
David Greene9bea7c82009-05-14 23:26:46 +0000864 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +0000865 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +0000866 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
867 TernOpInit::TernaryOp Code;
868 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000869
870
David Greene4afc5092009-05-14 21:54:42 +0000871 tgtok::TokKind LexCode = Lex.getCode();
872 Lex.Lex(); // eat the operation
873 switch (LexCode) {
874 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000875 case tgtok::XIf:
876 Code = TernOpInit::IF;
877 break;
David Greenebeb31a52009-05-14 22:23:47 +0000878 case tgtok::XForEach:
879 Code = TernOpInit::FOREACH;
880 break;
David Greene4afc5092009-05-14 21:54:42 +0000881 case tgtok::XSubst:
882 Code = TernOpInit::SUBST;
883 break;
884 }
885 if (Lex.getCode() != tgtok::l_paren) {
886 TokError("expected '(' after ternary operator");
887 return 0;
888 }
889 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000890
David Greene4afc5092009-05-14 21:54:42 +0000891 Init *LHS = ParseValue(CurRec);
892 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000893
David Greene4afc5092009-05-14 21:54:42 +0000894 if (Lex.getCode() != tgtok::comma) {
895 TokError("expected ',' in ternary operator");
896 return 0;
897 }
898 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000899
David Greene4afc5092009-05-14 21:54:42 +0000900 Init *MHS = ParseValue(CurRec);
901 if (MHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000902
David Greene4afc5092009-05-14 21:54:42 +0000903 if (Lex.getCode() != tgtok::comma) {
904 TokError("expected ',' in ternary operator");
905 return 0;
906 }
907 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000908
David Greene4afc5092009-05-14 21:54:42 +0000909 Init *RHS = ParseValue(CurRec);
910 if (RHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000911
David Greene4afc5092009-05-14 21:54:42 +0000912 if (Lex.getCode() != tgtok::r_paren) {
913 TokError("expected ')' in binary operator");
914 return 0;
915 }
916 Lex.Lex(); // eat the ')'
David Greened418c1b2009-05-14 20:54:48 +0000917
David Greene4afc5092009-05-14 21:54:42 +0000918 switch (LexCode) {
919 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000920 case tgtok::XIf: {
921 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
922 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
923 if (MHSt == 0 || RHSt == 0) {
924 TokError("could not get type for !if");
925 return 0;
926 }
927 if (MHSt->getType()->typeIsConvertibleTo(RHSt->getType())) {
928 Type = RHSt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000929 } else if (RHSt->getType()->typeIsConvertibleTo(MHSt->getType())) {
David Greene9bea7c82009-05-14 23:26:46 +0000930 Type = MHSt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000931 } else {
David Greene9bea7c82009-05-14 23:26:46 +0000932 TokError("inconsistent types for !if");
933 return 0;
934 }
935 break;
936 }
David Greenebeb31a52009-05-14 22:23:47 +0000937 case tgtok::XForEach: {
938 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
939 if (MHSt == 0) {
940 TokError("could not get type for !foreach");
941 return 0;
942 }
943 Type = MHSt->getType();
944 break;
945 }
David Greene4afc5092009-05-14 21:54:42 +0000946 case tgtok::XSubst: {
947 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
948 if (RHSt == 0) {
949 TokError("could not get type for !subst");
950 return 0;
951 }
952 Type = RHSt->getType();
953 break;
954 }
955 }
Bob Wilson21870412009-11-22 04:24:42 +0000956 return (new TernOpInit(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
957 CurMultiClass);
David Greene4afc5092009-05-14 21:54:42 +0000958 }
David Greened418c1b2009-05-14 20:54:48 +0000959 }
960 TokError("could not parse operation");
961 return 0;
962}
963
964/// ParseOperatorType - Parse a type for an operator. This returns
965/// null on error.
966///
967/// OperatorType ::= '<' Type '>'
968///
Dan Gohmana9ad0412009-08-12 22:10:57 +0000969RecTy *TGParser::ParseOperatorType() {
David Greened418c1b2009-05-14 20:54:48 +0000970 RecTy *Type = 0;
971
972 if (Lex.getCode() != tgtok::less) {
973 TokError("expected type name for operator");
974 return 0;
975 }
976 Lex.Lex(); // eat the <
977
978 Type = ParseType();
979
980 if (Type == 0) {
981 TokError("expected type name for operator");
982 return 0;
983 }
984
985 if (Lex.getCode() != tgtok::greater) {
986 TokError("expected type name for operator");
987 return 0;
988 }
989 Lex.Lex(); // eat the >
990
991 return Type;
992}
993
994
Chris Lattnerf4601652007-11-22 20:49:04 +0000995/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
996///
997/// SimpleValue ::= IDValue
998/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +0000999/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +00001000/// SimpleValue ::= CODEFRAGMENT
1001/// SimpleValue ::= '?'
1002/// SimpleValue ::= '{' ValueList '}'
1003/// SimpleValue ::= ID '<' ValueListNE '>'
1004/// SimpleValue ::= '[' ValueList ']'
1005/// SimpleValue ::= '(' IDValue DagArgList ')'
1006/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
1007/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1008/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1009/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1010/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1011///
David Greenee1b46912009-06-08 20:23:18 +00001012Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001013 Init *R = 0;
1014 switch (Lex.getCode()) {
1015 default: TokError("Unknown token when parsing a value"); break;
1016 case tgtok::IntVal: R = new IntInit(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001017 case tgtok::StrVal: {
1018 std::string Val = Lex.getCurStrVal();
1019 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001020
Jim Grosbachda4231f2009-03-26 16:17:51 +00001021 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001022 while (Lex.getCode() == tgtok::StrVal) {
1023 Val += Lex.getCurStrVal();
1024 Lex.Lex();
1025 }
Bob Wilson21870412009-11-22 04:24:42 +00001026
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001027 R = new StringInit(Val);
1028 break;
1029 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001030 case tgtok::CodeFragment:
Bob Wilson21870412009-11-22 04:24:42 +00001031 R = new CodeInit(Lex.getCurStrVal()); Lex.Lex(); break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001032 case tgtok::question: R = new UnsetInit(); Lex.Lex(); break;
1033 case tgtok::Id: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001034 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001035 std::string Name = Lex.getCurStrVal();
1036 if (Lex.Lex() != tgtok::less) // consume the Id.
1037 return ParseIDValue(CurRec, Name, NameLoc); // Value ::= IDValue
Bob Wilson21870412009-11-22 04:24:42 +00001038
Chris Lattnerf4601652007-11-22 20:49:04 +00001039 // Value ::= ID '<' ValueListNE '>'
1040 if (Lex.Lex() == tgtok::greater) {
1041 TokError("expected non-empty value list");
1042 return 0;
1043 }
David Greenee1b46912009-06-08 20:23:18 +00001044
Chris Lattnerf4601652007-11-22 20:49:04 +00001045 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1046 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1047 // body.
1048 Record *Class = Records.getClass(Name);
1049 if (!Class) {
1050 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1051 return 0;
1052 }
David Greenee1b46912009-06-08 20:23:18 +00001053
1054 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
1055 if (ValueList.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001056
David Greenee1b46912009-06-08 20:23:18 +00001057 if (Lex.getCode() != tgtok::greater) {
1058 TokError("expected '>' at end of value list");
1059 return 0;
1060 }
1061 Lex.Lex(); // eat the '>'
Bob Wilson21870412009-11-22 04:24:42 +00001062
Chris Lattnerf4601652007-11-22 20:49:04 +00001063 // Create the new record, set it as CurRec temporarily.
1064 static unsigned AnonCounter = 0;
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001065 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),NameLoc);
Chris Lattnerf4601652007-11-22 20:49:04 +00001066 SubClassReference SCRef;
1067 SCRef.RefLoc = NameLoc;
1068 SCRef.Rec = Class;
1069 SCRef.TemplateArgs = ValueList;
1070 // Add info about the subclass to NewRec.
1071 if (AddSubClass(NewRec, SCRef))
1072 return 0;
1073 NewRec->resolveReferences();
1074 Records.addDef(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001075
Chris Lattnerf4601652007-11-22 20:49:04 +00001076 // The result of the expression is a reference to the new record.
1077 return new DefInit(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001078 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001079 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001080 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001081 Lex.Lex(); // eat the '{'
1082 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001083
Chris Lattnerf4601652007-11-22 20:49:04 +00001084 if (Lex.getCode() != tgtok::r_brace) {
1085 Vals = ParseValueList(CurRec);
1086 if (Vals.empty()) return 0;
1087 }
1088 if (Lex.getCode() != tgtok::r_brace) {
1089 TokError("expected '}' at end of bit list value");
1090 return 0;
1091 }
1092 Lex.Lex(); // eat the '}'
Bob Wilson21870412009-11-22 04:24:42 +00001093
Chris Lattnerf4601652007-11-22 20:49:04 +00001094 BitsInit *Result = new BitsInit(Vals.size());
1095 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1096 Init *Bit = Vals[i]->convertInitializerTo(new BitRecTy());
1097 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +00001098 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1099 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +00001100 return 0;
1101 }
1102 Result->setBit(Vals.size()-i-1, Bit);
1103 }
1104 return Result;
1105 }
1106 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1107 Lex.Lex(); // eat the '['
1108 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001109
David Greenee1b46912009-06-08 20:23:18 +00001110 RecTy *DeducedEltTy = 0;
1111 ListRecTy *GivenListTy = 0;
Bob Wilson21870412009-11-22 04:24:42 +00001112
David Greenee1b46912009-06-08 20:23:18 +00001113 if (ItemType != 0) {
1114 ListRecTy *ListType = dynamic_cast<ListRecTy*>(ItemType);
1115 if (ListType == 0) {
1116 std::stringstream s;
Bob Wilson21870412009-11-22 04:24:42 +00001117 s << "Type mismatch for list, expected list type, got "
David Greenee1b46912009-06-08 20:23:18 +00001118 << ItemType->getAsString();
1119 TokError(s.str());
1120 }
1121 GivenListTy = ListType;
Bob Wilson21870412009-11-22 04:24:42 +00001122 }
David Greenee1b46912009-06-08 20:23:18 +00001123
Chris Lattnerf4601652007-11-22 20:49:04 +00001124 if (Lex.getCode() != tgtok::r_square) {
Bob Wilson21870412009-11-22 04:24:42 +00001125 Vals = ParseValueList(CurRec, 0,
1126 GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001127 if (Vals.empty()) return 0;
1128 }
1129 if (Lex.getCode() != tgtok::r_square) {
1130 TokError("expected ']' at end of list value");
1131 return 0;
1132 }
1133 Lex.Lex(); // eat the ']'
David Greenee1b46912009-06-08 20:23:18 +00001134
1135 RecTy *GivenEltTy = 0;
1136 if (Lex.getCode() == tgtok::less) {
1137 // Optional list element type
1138 Lex.Lex(); // eat the '<'
1139
1140 GivenEltTy = ParseType();
1141 if (GivenEltTy == 0) {
1142 // Couldn't parse element type
1143 return 0;
1144 }
1145
1146 if (Lex.getCode() != tgtok::greater) {
1147 TokError("expected '>' at end of list element type");
1148 return 0;
1149 }
1150 Lex.Lex(); // eat the '>'
1151 }
1152
1153 // Check elements
1154 RecTy *EltTy = 0;
1155 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
1156 i != ie;
1157 ++i) {
1158 TypedInit *TArg = dynamic_cast<TypedInit*>(*i);
1159 if (TArg == 0) {
1160 TokError("Untyped list element");
1161 return 0;
1162 }
1163 if (EltTy != 0) {
1164 EltTy = resolveTypes(EltTy, TArg->getType());
1165 if (EltTy == 0) {
1166 TokError("Incompatible types in list elements");
1167 return 0;
1168 }
Bob Wilson21870412009-11-22 04:24:42 +00001169 } else {
David Greenee1b46912009-06-08 20:23:18 +00001170 EltTy = TArg->getType();
1171 }
1172 }
1173
1174 if (GivenEltTy != 0) {
1175 if (EltTy != 0) {
1176 // Verify consistency
1177 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1178 TokError("Incompatible types in list elements");
1179 return 0;
1180 }
1181 }
1182 EltTy = GivenEltTy;
1183 }
1184
1185 if (EltTy == 0) {
1186 if (ItemType == 0) {
1187 TokError("No type for list");
1188 return 0;
1189 }
1190 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +00001191 } else {
David Greenee1b46912009-06-08 20:23:18 +00001192 // Make sure the deduced type is compatible with the given type
1193 if (GivenListTy) {
1194 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1195 TokError("Element type mismatch for list");
1196 return 0;
1197 }
1198 }
1199 DeducedEltTy = EltTy;
1200 }
Bob Wilson21870412009-11-22 04:24:42 +00001201
David Greenee1b46912009-06-08 20:23:18 +00001202 return new ListInit(Vals, DeducedEltTy);
Chris Lattnerf4601652007-11-22 20:49:04 +00001203 }
1204 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1205 Lex.Lex(); // eat the '('
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001206 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001207 TokError("expected identifier in dag init");
1208 return 0;
1209 }
Bob Wilson21870412009-11-22 04:24:42 +00001210
David Greenec7cafcd2009-04-22 20:18:10 +00001211 Init *Operator = 0;
1212 if (Lex.getCode() == tgtok::Id) {
1213 Operator = ParseIDValue(CurRec);
1214 if (Operator == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001215 } else {
David Greened418c1b2009-05-14 20:54:48 +00001216 Operator = ParseOperation(CurRec);
1217 if (Operator == 0) return 0;
David Greenec7cafcd2009-04-22 20:18:10 +00001218 }
1219
Nate Begeman7cee8172009-03-19 05:21:56 +00001220 // If the operator name is present, parse it.
1221 std::string OperatorName;
1222 if (Lex.getCode() == tgtok::colon) {
1223 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1224 TokError("expected variable name in dag operator");
1225 return 0;
1226 }
1227 OperatorName = Lex.getCurStrVal();
1228 Lex.Lex(); // eat the VarName.
1229 }
Bob Wilson21870412009-11-22 04:24:42 +00001230
Chris Lattnerf4601652007-11-22 20:49:04 +00001231 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
1232 if (Lex.getCode() != tgtok::r_paren) {
1233 DagArgs = ParseDagArgList(CurRec);
1234 if (DagArgs.empty()) return 0;
1235 }
Bob Wilson21870412009-11-22 04:24:42 +00001236
Chris Lattnerf4601652007-11-22 20:49:04 +00001237 if (Lex.getCode() != tgtok::r_paren) {
1238 TokError("expected ')' in dag init");
1239 return 0;
1240 }
1241 Lex.Lex(); // eat the ')'
Bob Wilson21870412009-11-22 04:24:42 +00001242
Nate Begeman7cee8172009-03-19 05:21:56 +00001243 return new DagInit(Operator, OperatorName, DagArgs);
David Greened418c1b2009-05-14 20:54:48 +00001244 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001245 }
Bob Wilson21870412009-11-22 04:24:42 +00001246
David Greene5f9f9ba2009-05-14 22:38:31 +00001247 case tgtok::XCar:
1248 case tgtok::XCdr:
1249 case tgtok::XNull:
David Greenee6c27de2009-05-14 21:22:49 +00001250 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001251 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +00001252 case tgtok::XSRA:
Chris Lattnerf4601652007-11-22 20:49:04 +00001253 case tgtok::XSRL:
1254 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +00001255 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001256 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene9bea7c82009-05-14 23:26:46 +00001257 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +00001258 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001259 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greened418c1b2009-05-14 20:54:48 +00001260 return ParseOperation(CurRec);
1261 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001262 }
1263 }
Bob Wilson21870412009-11-22 04:24:42 +00001264
Chris Lattnerf4601652007-11-22 20:49:04 +00001265 return R;
1266}
1267
1268/// ParseValue - Parse a tblgen value. This returns null on error.
1269///
1270/// Value ::= SimpleValue ValueSuffix*
1271/// ValueSuffix ::= '{' BitList '}'
1272/// ValueSuffix ::= '[' BitList ']'
1273/// ValueSuffix ::= '.' ID
1274///
David Greenee1b46912009-06-08 20:23:18 +00001275Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType) {
1276 Init *Result = ParseSimpleValue(CurRec, ItemType);
Chris Lattnerf4601652007-11-22 20:49:04 +00001277 if (Result == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001278
Chris Lattnerf4601652007-11-22 20:49:04 +00001279 // Parse the suffixes now if present.
1280 while (1) {
1281 switch (Lex.getCode()) {
1282 default: return Result;
1283 case tgtok::l_brace: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001284 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001285 Lex.Lex(); // eat the '{'
1286 std::vector<unsigned> Ranges = ParseRangeList();
1287 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001288
Chris Lattnerf4601652007-11-22 20:49:04 +00001289 // Reverse the bitlist.
1290 std::reverse(Ranges.begin(), Ranges.end());
1291 Result = Result->convertInitializerBitRange(Ranges);
1292 if (Result == 0) {
1293 Error(CurlyLoc, "Invalid bit range for value");
1294 return 0;
1295 }
Bob Wilson21870412009-11-22 04:24:42 +00001296
Chris Lattnerf4601652007-11-22 20:49:04 +00001297 // Eat the '}'.
1298 if (Lex.getCode() != tgtok::r_brace) {
1299 TokError("expected '}' at end of bit range list");
1300 return 0;
1301 }
1302 Lex.Lex();
1303 break;
1304 }
1305 case tgtok::l_square: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001306 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001307 Lex.Lex(); // eat the '['
1308 std::vector<unsigned> Ranges = ParseRangeList();
1309 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001310
Chris Lattnerf4601652007-11-22 20:49:04 +00001311 Result = Result->convertInitListSlice(Ranges);
1312 if (Result == 0) {
1313 Error(SquareLoc, "Invalid range for list slice");
1314 return 0;
1315 }
Bob Wilson21870412009-11-22 04:24:42 +00001316
Chris Lattnerf4601652007-11-22 20:49:04 +00001317 // Eat the ']'.
1318 if (Lex.getCode() != tgtok::r_square) {
1319 TokError("expected ']' at end of list slice");
1320 return 0;
1321 }
1322 Lex.Lex();
1323 break;
1324 }
1325 case tgtok::period:
1326 if (Lex.Lex() != tgtok::Id) { // eat the .
1327 TokError("expected field identifier after '.'");
1328 return 0;
1329 }
1330 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001331 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00001332 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +00001333 return 0;
1334 }
1335 Result = new FieldInit(Result, Lex.getCurStrVal());
1336 Lex.Lex(); // eat field name
1337 break;
1338 }
1339 }
1340}
1341
1342/// ParseDagArgList - Parse the argument list for a dag literal expression.
1343///
1344/// ParseDagArgList ::= Value (':' VARNAME)?
1345/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
Bob Wilson21870412009-11-22 04:24:42 +00001346std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4601652007-11-22 20:49:04 +00001347TGParser::ParseDagArgList(Record *CurRec) {
1348 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson21870412009-11-22 04:24:42 +00001349
Chris Lattnerf4601652007-11-22 20:49:04 +00001350 while (1) {
1351 Init *Val = ParseValue(CurRec);
1352 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
Bob Wilson21870412009-11-22 04:24:42 +00001353
Chris Lattnerf4601652007-11-22 20:49:04 +00001354 // If the variable name is present, add it.
1355 std::string VarName;
1356 if (Lex.getCode() == tgtok::colon) {
1357 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1358 TokError("expected variable name in dag literal");
1359 return std::vector<std::pair<llvm::Init*, std::string> >();
1360 }
1361 VarName = Lex.getCurStrVal();
1362 Lex.Lex(); // eat the VarName.
1363 }
Bob Wilson21870412009-11-22 04:24:42 +00001364
Chris Lattnerf4601652007-11-22 20:49:04 +00001365 Result.push_back(std::make_pair(Val, VarName));
Bob Wilson21870412009-11-22 04:24:42 +00001366
Chris Lattnerf4601652007-11-22 20:49:04 +00001367 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson21870412009-11-22 04:24:42 +00001368 Lex.Lex(); // eat the ','
Chris Lattnerf4601652007-11-22 20:49:04 +00001369 }
Bob Wilson21870412009-11-22 04:24:42 +00001370
Chris Lattnerf4601652007-11-22 20:49:04 +00001371 return Result;
1372}
1373
1374
1375/// ParseValueList - Parse a comma separated list of values, returning them as a
1376/// vector. Note that this always expects to be able to parse at least one
1377/// value. It returns an empty list if this is not possible.
1378///
1379/// ValueList ::= Value (',' Value)
1380///
Bob Wilson21870412009-11-22 04:24:42 +00001381std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
1382 RecTy *EltTy) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001383 std::vector<Init*> Result;
David Greenee1b46912009-06-08 20:23:18 +00001384 RecTy *ItemType = EltTy;
David Greene67acdf22009-06-29 19:59:52 +00001385 unsigned int ArgN = 0;
David Greenee1b46912009-06-08 20:23:18 +00001386 if (ArgsRec != 0 && EltTy == 0) {
1387 const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs();
1388 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1389 assert(RV && "Template argument record not found??");
1390 ItemType = RV->getType();
1391 ++ArgN;
1392 }
1393 Result.push_back(ParseValue(CurRec, ItemType));
Chris Lattnerf4601652007-11-22 20:49:04 +00001394 if (Result.back() == 0) return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001395
Chris Lattnerf4601652007-11-22 20:49:04 +00001396 while (Lex.getCode() == tgtok::comma) {
1397 Lex.Lex(); // Eat the comma
Bob Wilson21870412009-11-22 04:24:42 +00001398
David Greenee1b46912009-06-08 20:23:18 +00001399 if (ArgsRec != 0 && EltTy == 0) {
1400 const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs();
David Greene67acdf22009-06-29 19:59:52 +00001401 if (ArgN >= TArgs.size()) {
1402 TokError("too many template arguments");
1403 return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001404 }
David Greenee1b46912009-06-08 20:23:18 +00001405 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1406 assert(RV && "Template argument record not found??");
1407 ItemType = RV->getType();
1408 ++ArgN;
1409 }
1410 Result.push_back(ParseValue(CurRec, ItemType));
Chris Lattnerf4601652007-11-22 20:49:04 +00001411 if (Result.back() == 0) return std::vector<Init*>();
1412 }
Bob Wilson21870412009-11-22 04:24:42 +00001413
Chris Lattnerf4601652007-11-22 20:49:04 +00001414 return Result;
1415}
1416
1417
Chris Lattnerf4601652007-11-22 20:49:04 +00001418/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1419/// empty string on error. This can happen in a number of different context's,
1420/// including within a def or in the template args for a def (which which case
1421/// CurRec will be non-null) and within the template args for a multiclass (in
1422/// which case CurRec will be null, but CurMultiClass will be set). This can
1423/// also happen within a def that is within a multiclass, which will set both
1424/// CurRec and CurMultiClass.
1425///
1426/// Declaration ::= FIELD? Type ID ('=' Value)?
1427///
Bob Wilson21870412009-11-22 04:24:42 +00001428std::string TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4601652007-11-22 20:49:04 +00001429 bool ParsingTemplateArgs) {
1430 // Read the field prefix if present.
1431 bool HasField = Lex.getCode() == tgtok::Field;
1432 if (HasField) Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001433
Chris Lattnerf4601652007-11-22 20:49:04 +00001434 RecTy *Type = ParseType();
1435 if (Type == 0) return "";
Bob Wilson21870412009-11-22 04:24:42 +00001436
Chris Lattnerf4601652007-11-22 20:49:04 +00001437 if (Lex.getCode() != tgtok::Id) {
1438 TokError("Expected identifier in declaration");
1439 return "";
1440 }
Bob Wilson21870412009-11-22 04:24:42 +00001441
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001442 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001443 std::string DeclName = Lex.getCurStrVal();
1444 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001445
Chris Lattnerf4601652007-11-22 20:49:04 +00001446 if (ParsingTemplateArgs) {
1447 if (CurRec) {
1448 DeclName = CurRec->getName() + ":" + DeclName;
1449 } else {
1450 assert(CurMultiClass);
1451 }
1452 if (CurMultiClass)
1453 DeclName = CurMultiClass->Rec.getName() + "::" + DeclName;
1454 }
Bob Wilson21870412009-11-22 04:24:42 +00001455
Chris Lattnerf4601652007-11-22 20:49:04 +00001456 // Add the value.
1457 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
1458 return "";
Bob Wilson21870412009-11-22 04:24:42 +00001459
Chris Lattnerf4601652007-11-22 20:49:04 +00001460 // If a value is present, parse it.
1461 if (Lex.getCode() == tgtok::equal) {
1462 Lex.Lex();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001463 SMLoc ValLoc = Lex.getLoc();
David Greenee1b46912009-06-08 20:23:18 +00001464 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001465 if (Val == 0 ||
1466 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
1467 return "";
1468 }
Bob Wilson21870412009-11-22 04:24:42 +00001469
Chris Lattnerf4601652007-11-22 20:49:04 +00001470 return DeclName;
1471}
1472
1473/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1474/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1475/// template args for a def, which may or may not be in a multiclass. If null,
1476/// these are the template args for a multiclass.
1477///
1478/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson21870412009-11-22 04:24:42 +00001479///
Chris Lattnerf4601652007-11-22 20:49:04 +00001480bool TGParser::ParseTemplateArgList(Record *CurRec) {
1481 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1482 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +00001483
Chris Lattnerf4601652007-11-22 20:49:04 +00001484 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +00001485
Chris Lattnerf4601652007-11-22 20:49:04 +00001486 // Read the first declaration.
1487 std::string TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1488 if (TemplArg.empty())
1489 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001490
Chris Lattnerf4601652007-11-22 20:49:04 +00001491 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson21870412009-11-22 04:24:42 +00001492
Chris Lattnerf4601652007-11-22 20:49:04 +00001493 while (Lex.getCode() == tgtok::comma) {
1494 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001495
Chris Lattnerf4601652007-11-22 20:49:04 +00001496 // Read the following declarations.
1497 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1498 if (TemplArg.empty())
1499 return true;
1500 TheRecToAddTo->addTemplateArg(TemplArg);
1501 }
Bob Wilson21870412009-11-22 04:24:42 +00001502
Chris Lattnerf4601652007-11-22 20:49:04 +00001503 if (Lex.getCode() != tgtok::greater)
1504 return TokError("expected '>' at end of template argument list");
1505 Lex.Lex(); // eat the '>'.
1506 return false;
1507}
1508
1509
1510/// ParseBodyItem - Parse a single item at within the body of a def or class.
1511///
1512/// BodyItem ::= Declaration ';'
1513/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1514bool TGParser::ParseBodyItem(Record *CurRec) {
1515 if (Lex.getCode() != tgtok::Let) {
Bob Wilson21870412009-11-22 04:24:42 +00001516 if (ParseDeclaration(CurRec, false).empty())
Chris Lattnerf4601652007-11-22 20:49:04 +00001517 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001518
Chris Lattnerf4601652007-11-22 20:49:04 +00001519 if (Lex.getCode() != tgtok::semi)
1520 return TokError("expected ';' after declaration");
1521 Lex.Lex();
1522 return false;
1523 }
1524
1525 // LET ID OptionalRangeList '=' Value ';'
1526 if (Lex.Lex() != tgtok::Id)
1527 return TokError("expected field identifier after let");
Bob Wilson21870412009-11-22 04:24:42 +00001528
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001529 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001530 std::string FieldName = Lex.getCurStrVal();
1531 Lex.Lex(); // eat the field name.
Bob Wilson21870412009-11-22 04:24:42 +00001532
Chris Lattnerf4601652007-11-22 20:49:04 +00001533 std::vector<unsigned> BitList;
Bob Wilson21870412009-11-22 04:24:42 +00001534 if (ParseOptionalBitList(BitList))
Chris Lattnerf4601652007-11-22 20:49:04 +00001535 return true;
1536 std::reverse(BitList.begin(), BitList.end());
Bob Wilson21870412009-11-22 04:24:42 +00001537
Chris Lattnerf4601652007-11-22 20:49:04 +00001538 if (Lex.getCode() != tgtok::equal)
1539 return TokError("expected '=' in let expression");
1540 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001541
David Greenee1b46912009-06-08 20:23:18 +00001542 RecordVal *Field = CurRec->getValue(FieldName);
1543 if (Field == 0)
1544 return TokError("Value '" + FieldName + "' unknown!");
1545
1546 RecTy *Type = Field->getType();
Bob Wilson21870412009-11-22 04:24:42 +00001547
David Greenee1b46912009-06-08 20:23:18 +00001548 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001549 if (Val == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001550
Chris Lattnerf4601652007-11-22 20:49:04 +00001551 if (Lex.getCode() != tgtok::semi)
1552 return TokError("expected ';' after let expression");
1553 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001554
Chris Lattnerf4601652007-11-22 20:49:04 +00001555 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1556}
1557
1558/// ParseBody - Read the body of a class or def. Return true on error, false on
1559/// success.
1560///
1561/// Body ::= ';'
1562/// Body ::= '{' BodyList '}'
1563/// BodyList BodyItem*
1564///
1565bool TGParser::ParseBody(Record *CurRec) {
1566 // If this is a null definition, just eat the semi and return.
1567 if (Lex.getCode() == tgtok::semi) {
1568 Lex.Lex();
1569 return false;
1570 }
Bob Wilson21870412009-11-22 04:24:42 +00001571
Chris Lattnerf4601652007-11-22 20:49:04 +00001572 if (Lex.getCode() != tgtok::l_brace)
1573 return TokError("Expected ';' or '{' to start body");
1574 // Eat the '{'.
1575 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001576
Chris Lattnerf4601652007-11-22 20:49:04 +00001577 while (Lex.getCode() != tgtok::r_brace)
1578 if (ParseBodyItem(CurRec))
1579 return true;
1580
1581 // Eat the '}'.
1582 Lex.Lex();
1583 return false;
1584}
1585
1586/// ParseObjectBody - Parse the body of a def or class. This consists of an
1587/// optional ClassList followed by a Body. CurRec is the current def or class
1588/// that is being parsed.
1589///
1590/// ObjectBody ::= BaseClassList Body
1591/// BaseClassList ::= /*empty*/
1592/// BaseClassList ::= ':' BaseClassListNE
1593/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1594///
1595bool TGParser::ParseObjectBody(Record *CurRec) {
1596 // If there is a baseclass list, read it.
1597 if (Lex.getCode() == tgtok::colon) {
1598 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001599
Chris Lattnerf4601652007-11-22 20:49:04 +00001600 // Read all of the subclasses.
1601 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1602 while (1) {
1603 // Check for error.
1604 if (SubClass.Rec == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001605
Chris Lattnerf4601652007-11-22 20:49:04 +00001606 // Add it.
1607 if (AddSubClass(CurRec, SubClass))
1608 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001609
Chris Lattnerf4601652007-11-22 20:49:04 +00001610 if (Lex.getCode() != tgtok::comma) break;
1611 Lex.Lex(); // eat ','.
1612 SubClass = ParseSubClassReference(CurRec, false);
1613 }
1614 }
1615
1616 // Process any variables on the let stack.
1617 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1618 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1619 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1620 LetStack[i][j].Bits, LetStack[i][j].Value))
1621 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001622
Chris Lattnerf4601652007-11-22 20:49:04 +00001623 return ParseBody(CurRec);
1624}
1625
Chris Lattnerf4601652007-11-22 20:49:04 +00001626/// ParseDef - Parse and return a top level or multiclass def, return the record
1627/// corresponding to it. This returns null on error.
1628///
1629/// DefInst ::= DEF ObjectName ObjectBody
1630///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001631bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001632 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001633 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson21870412009-11-22 04:24:42 +00001634 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4601652007-11-22 20:49:04 +00001635
1636 // Parse ObjectName and make a record for it.
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001637 Record *CurRec = new Record(ParseObjectName(), DefLoc);
Bob Wilson21870412009-11-22 04:24:42 +00001638
Chris Lattnerf4601652007-11-22 20:49:04 +00001639 if (!CurMultiClass) {
1640 // Top-level def definition.
Bob Wilson21870412009-11-22 04:24:42 +00001641
Chris Lattnerf4601652007-11-22 20:49:04 +00001642 // Ensure redefinition doesn't happen.
1643 if (Records.getDef(CurRec->getName())) {
1644 Error(DefLoc, "def '" + CurRec->getName() + "' already defined");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001645 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001646 }
1647 Records.addDef(CurRec);
1648 } else {
1649 // Otherwise, a def inside a multiclass, add it to the multiclass.
1650 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
1651 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
1652 Error(DefLoc, "def '" + CurRec->getName() +
1653 "' already defined in this multiclass!");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001654 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001655 }
1656 CurMultiClass->DefPrototypes.push_back(CurRec);
1657 }
Bob Wilson21870412009-11-22 04:24:42 +00001658
Chris Lattnerf4601652007-11-22 20:49:04 +00001659 if (ParseObjectBody(CurRec))
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001660 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001661
Chris Lattnerf4601652007-11-22 20:49:04 +00001662 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
1663 CurRec->resolveReferences();
Bob Wilson21870412009-11-22 04:24:42 +00001664
Chris Lattnerf4601652007-11-22 20:49:04 +00001665 // If ObjectBody has template arguments, it's an error.
1666 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001667
1668 if (CurMultiClass) {
1669 // Copy the template arguments for the multiclass into the def.
1670 const std::vector<std::string> &TArgs =
1671 CurMultiClass->Rec.getTemplateArgs();
1672
1673 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1674 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1675 assert(RV && "Template arg doesn't exist?");
1676 CurRec->addValue(*RV);
1677 }
1678 }
1679
1680 return false;
Chris Lattnerf4601652007-11-22 20:49:04 +00001681}
1682
1683
1684/// ParseClass - Parse a tblgen class definition.
1685///
1686/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
1687///
1688bool TGParser::ParseClass() {
1689 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
1690 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001691
Chris Lattnerf4601652007-11-22 20:49:04 +00001692 if (Lex.getCode() != tgtok::Id)
1693 return TokError("expected class name after 'class' keyword");
Bob Wilson21870412009-11-22 04:24:42 +00001694
Chris Lattnerf4601652007-11-22 20:49:04 +00001695 Record *CurRec = Records.getClass(Lex.getCurStrVal());
1696 if (CurRec) {
1697 // If the body was previously defined, this is an error.
1698 if (!CurRec->getValues().empty() ||
1699 !CurRec->getSuperClasses().empty() ||
1700 !CurRec->getTemplateArgs().empty())
1701 return TokError("Class '" + CurRec->getName() + "' already defined");
1702 } else {
1703 // If this is the first reference to this class, create and add it.
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001704 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc());
Chris Lattnerf4601652007-11-22 20:49:04 +00001705 Records.addClass(CurRec);
1706 }
1707 Lex.Lex(); // eat the name.
Bob Wilson21870412009-11-22 04:24:42 +00001708
Chris Lattnerf4601652007-11-22 20:49:04 +00001709 // If there are template args, parse them.
1710 if (Lex.getCode() == tgtok::less)
1711 if (ParseTemplateArgList(CurRec))
1712 return true;
1713
1714 // Finally, parse the object body.
1715 return ParseObjectBody(CurRec);
1716}
1717
1718/// ParseLetList - Parse a non-empty list of assignment expressions into a list
1719/// of LetRecords.
1720///
1721/// LetList ::= LetItem (',' LetItem)*
1722/// LetItem ::= ID OptionalRangeList '=' Value
1723///
1724std::vector<LetRecord> TGParser::ParseLetList() {
1725 std::vector<LetRecord> Result;
Bob Wilson21870412009-11-22 04:24:42 +00001726
Chris Lattnerf4601652007-11-22 20:49:04 +00001727 while (1) {
1728 if (Lex.getCode() != tgtok::Id) {
1729 TokError("expected identifier in let definition");
1730 return std::vector<LetRecord>();
1731 }
1732 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001733 SMLoc NameLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +00001734 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4601652007-11-22 20:49:04 +00001735
1736 // Check for an optional RangeList.
1737 std::vector<unsigned> Bits;
Bob Wilson21870412009-11-22 04:24:42 +00001738 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4601652007-11-22 20:49:04 +00001739 return std::vector<LetRecord>();
1740 std::reverse(Bits.begin(), Bits.end());
Bob Wilson21870412009-11-22 04:24:42 +00001741
Chris Lattnerf4601652007-11-22 20:49:04 +00001742 if (Lex.getCode() != tgtok::equal) {
1743 TokError("expected '=' in let expression");
1744 return std::vector<LetRecord>();
1745 }
1746 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001747
Chris Lattnerf4601652007-11-22 20:49:04 +00001748 Init *Val = ParseValue(0);
1749 if (Val == 0) return std::vector<LetRecord>();
Bob Wilson21870412009-11-22 04:24:42 +00001750
Chris Lattnerf4601652007-11-22 20:49:04 +00001751 // Now that we have everything, add the record.
1752 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson21870412009-11-22 04:24:42 +00001753
Chris Lattnerf4601652007-11-22 20:49:04 +00001754 if (Lex.getCode() != tgtok::comma)
1755 return Result;
Bob Wilson21870412009-11-22 04:24:42 +00001756 Lex.Lex(); // eat the comma.
Chris Lattnerf4601652007-11-22 20:49:04 +00001757 }
1758}
1759
1760/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001761/// different related productions. This works inside multiclasses too.
Chris Lattnerf4601652007-11-22 20:49:04 +00001762///
1763/// Object ::= LET LetList IN '{' ObjectList '}'
1764/// Object ::= LET LetList IN Object
1765///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001766bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001767 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
1768 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001769
Chris Lattnerf4601652007-11-22 20:49:04 +00001770 // Add this entry to the let stack.
1771 std::vector<LetRecord> LetInfo = ParseLetList();
1772 if (LetInfo.empty()) return true;
1773 LetStack.push_back(LetInfo);
1774
1775 if (Lex.getCode() != tgtok::In)
1776 return TokError("expected 'in' at end of top-level 'let'");
1777 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001778
Chris Lattnerf4601652007-11-22 20:49:04 +00001779 // If this is a scalar let, just handle it now
1780 if (Lex.getCode() != tgtok::l_brace) {
1781 // LET LetList IN Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001782 if (ParseObject(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00001783 return true;
1784 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001785 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001786 // Otherwise, this is a group let.
1787 Lex.Lex(); // eat the '{'.
Bob Wilson21870412009-11-22 04:24:42 +00001788
Chris Lattnerf4601652007-11-22 20:49:04 +00001789 // Parse the object list.
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001790 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00001791 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001792
Chris Lattnerf4601652007-11-22 20:49:04 +00001793 if (Lex.getCode() != tgtok::r_brace) {
1794 TokError("expected '}' at end of top level let command");
1795 return Error(BraceLoc, "to match this '{'");
1796 }
1797 Lex.Lex();
1798 }
Bob Wilson21870412009-11-22 04:24:42 +00001799
Chris Lattnerf4601652007-11-22 20:49:04 +00001800 // Outside this let scope, this let block is not active.
1801 LetStack.pop_back();
1802 return false;
1803}
1804
Chris Lattnerf4601652007-11-22 20:49:04 +00001805/// ParseMultiClass - Parse a multiclass definition.
1806///
Bob Wilson32558652009-04-28 19:41:44 +00001807/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
1808/// ':' BaseMultiClassList '{' MultiClassDef+ '}'
Chris Lattnerf4601652007-11-22 20:49:04 +00001809///
1810bool TGParser::ParseMultiClass() {
1811 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
1812 Lex.Lex(); // Eat the multiclass token.
1813
1814 if (Lex.getCode() != tgtok::Id)
1815 return TokError("expected identifier after multiclass for name");
1816 std::string Name = Lex.getCurStrVal();
Bob Wilson21870412009-11-22 04:24:42 +00001817
Chris Lattnerf4601652007-11-22 20:49:04 +00001818 if (MultiClasses.count(Name))
1819 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson21870412009-11-22 04:24:42 +00001820
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001821 CurMultiClass = MultiClasses[Name] = new MultiClass(Name, Lex.getLoc());
Chris Lattnerf4601652007-11-22 20:49:04 +00001822 Lex.Lex(); // Eat the identifier.
Bob Wilson21870412009-11-22 04:24:42 +00001823
Chris Lattnerf4601652007-11-22 20:49:04 +00001824 // If there are template args, parse them.
1825 if (Lex.getCode() == tgtok::less)
1826 if (ParseTemplateArgList(0))
1827 return true;
1828
David Greened34a73b2009-04-24 16:55:41 +00001829 bool inherits = false;
1830
David Greenede444af2009-04-22 16:42:54 +00001831 // If there are submulticlasses, parse them.
1832 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00001833 inherits = true;
1834
David Greenede444af2009-04-22 16:42:54 +00001835 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00001836
David Greenede444af2009-04-22 16:42:54 +00001837 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00001838 SubMultiClassReference SubMultiClass =
1839 ParseSubMultiClassReference(CurMultiClass);
David Greenede444af2009-04-22 16:42:54 +00001840 while (1) {
1841 // Check for error.
1842 if (SubMultiClass.MC == 0) return true;
Bob Wilson32558652009-04-28 19:41:44 +00001843
David Greenede444af2009-04-22 16:42:54 +00001844 // Add it.
1845 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
1846 return true;
Bob Wilson32558652009-04-28 19:41:44 +00001847
David Greenede444af2009-04-22 16:42:54 +00001848 if (Lex.getCode() != tgtok::comma) break;
1849 Lex.Lex(); // eat ','.
1850 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
1851 }
1852 }
1853
David Greened34a73b2009-04-24 16:55:41 +00001854 if (Lex.getCode() != tgtok::l_brace) {
1855 if (!inherits)
1856 return TokError("expected '{' in multiclass definition");
Bob Wilson21870412009-11-22 04:24:42 +00001857 else if (Lex.getCode() != tgtok::semi)
1858 return TokError("expected ';' in multiclass definition");
David Greened34a73b2009-04-24 16:55:41 +00001859 else
Bob Wilson21870412009-11-22 04:24:42 +00001860 Lex.Lex(); // eat the ';'.
1861 } else {
David Greened34a73b2009-04-24 16:55:41 +00001862 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
1863 return TokError("multiclass must contain at least one def");
Bob Wilson21870412009-11-22 04:24:42 +00001864
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001865 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001866 switch (Lex.getCode()) {
1867 default:
1868 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
1869 case tgtok::Let:
1870 case tgtok::Def:
1871 case tgtok::Defm:
1872 if (ParseObject(CurMultiClass))
1873 return true;
1874 break;
1875 }
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001876 }
David Greened34a73b2009-04-24 16:55:41 +00001877 Lex.Lex(); // eat the '}'.
1878 }
Bob Wilson21870412009-11-22 04:24:42 +00001879
Chris Lattnerf4601652007-11-22 20:49:04 +00001880 CurMultiClass = 0;
1881 return false;
1882}
1883
1884/// ParseDefm - Parse the instantiation of a multiclass.
1885///
1886/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
1887///
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001888bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001889 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Bob Wilson21870412009-11-22 04:24:42 +00001890
Chris Lattnerdf72eae2010-10-05 22:51:56 +00001891 std::string DefmPrefix;
1892 if (Lex.Lex() == tgtok::Id) { // eat the defm.
1893 DefmPrefix = Lex.getCurStrVal();
1894 Lex.Lex(); // Eat the defm prefix.
1895 }
1896
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001897 SMLoc DefmPrefixLoc = Lex.getLoc();
Chris Lattnerdf72eae2010-10-05 22:51:56 +00001898 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4601652007-11-22 20:49:04 +00001899 return TokError("expected ':' after defm identifier");
Bob Wilson21870412009-11-22 04:24:42 +00001900
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00001901 // Keep track of the new generated record definitions.
1902 std::vector<Record*> NewRecDefs;
1903
1904 // This record also inherits from a regular class (non-multiclass)?
1905 bool InheritFromClass = false;
1906
Chris Lattnerf4601652007-11-22 20:49:04 +00001907 // eat the colon.
1908 Lex.Lex();
1909
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001910 SMLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001911 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene56546132009-04-22 22:17:51 +00001912
1913 while (1) {
1914 if (Ref.Rec == 0) return true;
1915
1916 // To instantiate a multiclass, we need to first get the multiclass, then
1917 // instantiate each def contained in the multiclass with the SubClassRef
1918 // template parameters.
1919 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
1920 assert(MC && "Didn't lookup multiclass correctly?");
Bob Wilson21870412009-11-22 04:24:42 +00001921 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greene56546132009-04-22 22:17:51 +00001922
1923 // Verify that the correct number of template arguments were specified.
1924 const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs();
1925 if (TArgs.size() < TemplateVals.size())
1926 return Error(SubClassLoc,
1927 "more template args specified than multiclass expects");
1928
1929 // Loop over all the def's in the multiclass, instantiating each one.
1930 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
1931 Record *DefProto = MC->DefPrototypes[i];
1932
Chris Lattnerdf72eae2010-10-05 22:51:56 +00001933 // Add in the defm name. If the defm prefix is empty, give each
1934 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
1935 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
1936 // as a prefix.
David Greene065f2592009-05-05 16:28:25 +00001937 std::string DefName = DefProto->getName();
Chris Lattnerdf72eae2010-10-05 22:51:56 +00001938 if (DefmPrefix.empty()) {
1939 DefName = GetNewAnonymousName();
Bob Wilson21870412009-11-22 04:24:42 +00001940 } else {
Chris Lattnerdf72eae2010-10-05 22:51:56 +00001941 std::string::size_type idx = DefName.find("#NAME#");
1942 if (idx != std::string::npos) {
1943 DefName.replace(idx, 6, DefmPrefix);
1944 } else {
1945 // Add the suffix to the defm name to get the new name.
1946 DefName = DefmPrefix + DefName;
1947 }
David Greene065f2592009-05-05 16:28:25 +00001948 }
1949
1950 Record *CurRec = new Record(DefName, DefmPrefixLoc);
David Greene56546132009-04-22 22:17:51 +00001951
1952 SubClassReference Ref;
1953 Ref.RefLoc = DefmPrefixLoc;
1954 Ref.Rec = DefProto;
1955 AddSubClass(CurRec, Ref);
1956
1957 // Loop over all of the template arguments, setting them to the specified
1958 // value or leaving them as the default if necessary.
1959 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
Bob Wilson32558652009-04-28 19:41:44 +00001960 // Check if a value is specified for this temp-arg.
1961 if (i < TemplateVals.size()) {
David Greene56546132009-04-22 22:17:51 +00001962 // Set it now.
1963 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
1964 TemplateVals[i]))
1965 return true;
1966
1967 // Resolve it next.
1968 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
1969
1970 // Now remove it.
1971 CurRec->removeValue(TArgs[i]);
1972
1973 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Bob Wilson32558652009-04-28 19:41:44 +00001974 return Error(SubClassLoc,
1975 "value not specified for template argument #"+
David Greene56546132009-04-22 22:17:51 +00001976 utostr(i) + " (" + TArgs[i] + ") of multiclassclass '" +
1977 MC->Rec.getName() + "'");
1978 }
1979 }
1980
1981 // If the mdef is inside a 'let' expression, add to each def.
1982 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1983 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1984 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1985 LetStack[i][j].Bits, LetStack[i][j].Value)) {
1986 Error(DefmPrefixLoc, "when instantiating this defm");
1987 return true;
1988 }
1989
1990 // Ensure redefinition doesn't happen.
1991 if (Records.getDef(CurRec->getName()))
Bob Wilson21870412009-11-22 04:24:42 +00001992 return Error(DefmPrefixLoc, "def '" + CurRec->getName() +
1993 "' already defined, instantiating defm with subdef '" +
David Greene56546132009-04-22 22:17:51 +00001994 DefProto->getName() + "'");
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001995
1996 // Don't create a top level definition for defm inside multiclasses,
1997 // instead, only update the prototypes and bind the template args
1998 // with the new created definition.
1999 if (CurMultiClass) {
2000 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2001 i != e; ++i) {
2002 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
2003 Error(DefmPrefixLoc, "defm '" + CurRec->getName() +
2004 "' already defined in this multiclass!");
2005 return 0;
2006 }
2007 }
2008 CurMultiClass->DefPrototypes.push_back(CurRec);
2009
2010 // Copy the template arguments for the multiclass into the new def.
2011 const std::vector<std::string> &TA =
2012 CurMultiClass->Rec.getTemplateArgs();
2013
2014 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2015 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2016 assert(RV && "Template arg doesn't exist?");
2017 CurRec->addValue(*RV);
2018 }
2019 } else {
2020 Records.addDef(CurRec);
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002021 }
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002022
2023 NewRecDefs.push_back(CurRec);
David Greene56546132009-04-22 22:17:51 +00002024 }
2025
2026 if (Lex.getCode() != tgtok::comma) break;
2027 Lex.Lex(); // eat ','.
2028
2029 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002030
2031 // A defm can inherit from regular classes (non-multiclass) as
2032 // long as they come in the end of the inheritance list.
2033 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0);
2034
2035 if (InheritFromClass)
2036 break;
2037
David Greene56546132009-04-22 22:17:51 +00002038 Ref = ParseSubClassReference(0, true);
2039 }
2040
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002041 if (InheritFromClass) {
2042 // Process all the classes to inherit as if they were part of a
2043 // regular 'def' and inherit all record values.
2044 SubClassReference SubClass = ParseSubClassReference(0, false);
2045 while (1) {
2046 // Check for error.
2047 if (SubClass.Rec == 0) return true;
2048
2049 // Get the expanded definition prototypes and teach them about
2050 // the record values the current class to inherit has
2051 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2052 Record *CurRec = NewRecDefs[i];
2053
2054 // Add it.
2055 if (AddSubClass(CurRec, SubClass))
2056 return true;
2057
2058 // Process any variables on the let stack.
2059 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2060 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2061 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2062 LetStack[i][j].Bits, LetStack[i][j].Value))
2063 return true;
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002064 }
2065
2066 if (Lex.getCode() != tgtok::comma) break;
2067 Lex.Lex(); // eat ','.
2068 SubClass = ParseSubClassReference(0, false);
2069 }
2070 }
2071
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002072 if (!CurMultiClass)
2073 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
2074 NewRecDefs[i]->resolveReferences();
2075
Chris Lattnerf4601652007-11-22 20:49:04 +00002076 if (Lex.getCode() != tgtok::semi)
2077 return TokError("expected ';' at end of defm");
2078 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002079
Chris Lattnerf4601652007-11-22 20:49:04 +00002080 return false;
2081}
2082
2083/// ParseObject
2084/// Object ::= ClassInst
2085/// Object ::= DefInst
2086/// Object ::= MultiClassInst
2087/// Object ::= DefMInst
2088/// Object ::= LETCommand '{' ObjectList '}'
2089/// Object ::= LETCommand Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002090bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002091 switch (Lex.getCode()) {
2092 default: assert(0 && "This is not an object");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002093 case tgtok::Let: return ParseTopLevelLet(MC);
2094 case tgtok::Def: return ParseDef(MC);
2095 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4601652007-11-22 20:49:04 +00002096 case tgtok::Class: return ParseClass();
2097 case tgtok::MultiClass: return ParseMultiClass();
2098 }
2099}
2100
2101/// ParseObjectList
2102/// ObjectList :== Object*
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002103bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002104 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002105 if (ParseObject(MC))
Chris Lattnerf4601652007-11-22 20:49:04 +00002106 return true;
2107 }
2108 return false;
2109}
2110
Chris Lattnerf4601652007-11-22 20:49:04 +00002111bool TGParser::ParseFile() {
2112 Lex.Lex(); // Prime the lexer.
2113 if (ParseObjectList()) return true;
Bob Wilson21870412009-11-22 04:24:42 +00002114
Chris Lattnerf4601652007-11-22 20:49:04 +00002115 // If we have unread input at the end of the file, report it.
2116 if (Lex.getCode() == tgtok::Eof)
2117 return false;
Bob Wilson21870412009-11-22 04:24:42 +00002118
Chris Lattnerf4601652007-11-22 20:49:04 +00002119 return TokError("Unexpected input at top level");
2120}
2121