blob: b095a6c30148a2497d7f05090c2a21fac409e700 [file] [log] [blame]
Chris Lattnerf4601652007-11-22 20:49:04 +00001//===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf4601652007-11-22 20:49:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Parser for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
14#include "TGParser.h"
15#include "Record.h"
16#include "llvm/ADT/StringExtras.h"
Daniel Dunbar1a551802009-07-03 00:10:29 +000017#include <algorithm>
18#include <sstream>
Chris Lattnerf4601652007-11-22 20:49:04 +000019using namespace llvm;
20
21//===----------------------------------------------------------------------===//
22// Support Code for the Semantic Actions.
23//===----------------------------------------------------------------------===//
24
25namespace llvm {
Chris Lattnerf4601652007-11-22 20:49:04 +000026struct SubClassReference {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000027 SMLoc RefLoc;
Chris Lattnerf4601652007-11-22 20:49:04 +000028 Record *Rec;
29 std::vector<Init*> TemplateArgs;
Chris Lattner1c8ae592009-03-13 16:01:53 +000030 SubClassReference() : Rec(0) {}
David Greened34a73b2009-04-24 16:55:41 +000031
Chris Lattnerf4601652007-11-22 20:49:04 +000032 bool isInvalid() const { return Rec == 0; }
33};
David Greenede444af2009-04-22 16:42:54 +000034
35struct SubMultiClassReference {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000036 SMLoc RefLoc;
David Greenede444af2009-04-22 16:42:54 +000037 MultiClass *MC;
38 std::vector<Init*> TemplateArgs;
39 SubMultiClassReference() : MC(0) {}
Bob Wilson32558652009-04-28 19:41:44 +000040
David Greenede444af2009-04-22 16:42:54 +000041 bool isInvalid() const { return MC == 0; }
David Greened34a73b2009-04-24 16:55:41 +000042 void dump() const;
David Greenede444af2009-04-22 16:42:54 +000043};
David Greened34a73b2009-04-24 16:55:41 +000044
45void SubMultiClassReference::dump() const {
Daniel Dunbar1a551802009-07-03 00:10:29 +000046 errs() << "Multiclass:\n";
Bob Wilson21870412009-11-22 04:24:42 +000047
David Greened34a73b2009-04-24 16:55:41 +000048 MC->dump();
Bob Wilson21870412009-11-22 04:24:42 +000049
Daniel Dunbar1a551802009-07-03 00:10:29 +000050 errs() << "Template args:\n";
David Greened34a73b2009-04-24 16:55:41 +000051 for (std::vector<Init *>::const_iterator i = TemplateArgs.begin(),
52 iend = TemplateArgs.end();
53 i != iend;
54 ++i) {
55 (*i)->dump();
56 }
57}
58
Chris Lattnerf4601652007-11-22 20:49:04 +000059} // end namespace llvm
60
Chris Lattner1e3a8a42009-06-21 03:39:35 +000061bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Chris Lattnerf4601652007-11-22 20:49:04 +000062 if (CurRec == 0)
63 CurRec = &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +000064
Chris Lattnerf4601652007-11-22 20:49:04 +000065 if (RecordVal *ERV = CurRec->getValue(RV.getName())) {
66 // The value already exists in the class, treat this as a set.
67 if (ERV->setValue(RV.getValue()))
68 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
69 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson21870412009-11-22 04:24:42 +000070 "previous definition of type '" +
Chris Lattnerf4601652007-11-22 20:49:04 +000071 ERV->getType()->getAsString() + "'");
72 } else {
73 CurRec->addValue(RV);
74 }
75 return false;
76}
77
78/// SetValue -
79/// Return true on error, false on success.
Bob Wilson21870412009-11-22 04:24:42 +000080bool TGParser::SetValue(Record *CurRec, SMLoc Loc, const std::string &ValName,
Chris Lattnerf4601652007-11-22 20:49:04 +000081 const std::vector<unsigned> &BitList, Init *V) {
82 if (!V) return false;
83
84 if (CurRec == 0) CurRec = &CurMultiClass->Rec;
85
86 RecordVal *RV = CurRec->getValue(ValName);
87 if (RV == 0)
88 return Error(Loc, "Value '" + ValName + "' unknown!");
89
90 // Do not allow assignments like 'X = X'. This will just cause infinite loops
91 // in the resolution machinery.
92 if (BitList.empty())
93 if (VarInit *VI = dynamic_cast<VarInit*>(V))
94 if (VI->getName() == ValName)
95 return false;
Bob Wilson21870412009-11-22 04:24:42 +000096
Chris Lattnerf4601652007-11-22 20:49:04 +000097 // If we are assigning to a subset of the bits in the value... then we must be
98 // assigning to a field of BitsRecTy, which must have a BitsInit
99 // initializer.
100 //
101 if (!BitList.empty()) {
102 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
103 if (CurVal == 0)
104 return Error(Loc, "Value '" + ValName + "' is not a bits type");
105
106 // Convert the incoming value to a bits type of the appropriate size...
107 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList.size()));
108 if (BI == 0) {
109 V->convertInitializerTo(new BitsRecTy(BitList.size()));
110 return Error(Loc, "Initializer is not compatible with bit range");
111 }
Bob Wilson21870412009-11-22 04:24:42 +0000112
Chris Lattnerf4601652007-11-22 20:49:04 +0000113 // We should have a BitsInit type now.
114 BitsInit *BInit = dynamic_cast<BitsInit*>(BI);
115 assert(BInit != 0);
116
117 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
118
119 // Loop over bits, assigning values as appropriate.
120 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
121 unsigned Bit = BitList[i];
122 if (NewVal->getBit(Bit))
123 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
124 ValName + "' more than once");
125 NewVal->setBit(Bit, BInit->getBit(i));
126 }
127
128 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
129 if (NewVal->getBit(i) == 0)
130 NewVal->setBit(i, CurVal->getBit(i));
131
132 V = NewVal;
133 }
134
135 if (RV->setValue(V))
Bob Wilson21870412009-11-22 04:24:42 +0000136 return Error(Loc, "Value '" + ValName + "' of type '" +
137 RV->getType()->getAsString() +
Chris Lattner5d814862007-11-22 21:06:59 +0000138 "' is incompatible with initializer '" + V->getAsString() +"'");
Chris Lattnerf4601652007-11-22 20:49:04 +0000139 return false;
140}
141
142/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
143/// args as SubClass's template arguments.
Cedric Venetaff9c272009-02-14 16:06:42 +0000144bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000145 Record *SC = SubClass.Rec;
146 // Add all of the values in the subclass into the current class.
147 const std::vector<RecordVal> &Vals = SC->getValues();
148 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
149 if (AddValue(CurRec, SubClass.RefLoc, Vals[i]))
150 return true;
151
152 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
153
154 // Ensure that an appropriate number of template arguments are specified.
155 if (TArgs.size() < SubClass.TemplateArgs.size())
156 return Error(SubClass.RefLoc, "More template args specified than expected");
Bob Wilson21870412009-11-22 04:24:42 +0000157
Chris Lattnerf4601652007-11-22 20:49:04 +0000158 // Loop over all of the template arguments, setting them to the specified
159 // value or leaving them as the default if necessary.
160 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
161 if (i < SubClass.TemplateArgs.size()) {
162 // If a value is specified for this template arg, set it now.
Bob Wilson21870412009-11-22 04:24:42 +0000163 if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(),
Chris Lattnerf4601652007-11-22 20:49:04 +0000164 SubClass.TemplateArgs[i]))
165 return true;
Bob Wilson21870412009-11-22 04:24:42 +0000166
Chris Lattnerf4601652007-11-22 20:49:04 +0000167 // Resolve it next.
168 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson21870412009-11-22 04:24:42 +0000169
Chris Lattnerf4601652007-11-22 20:49:04 +0000170 // Now remove it.
171 CurRec->removeValue(TArgs[i]);
172
173 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
174 return Error(SubClass.RefLoc,"Value not specified for template argument #"
Bob Wilson21870412009-11-22 04:24:42 +0000175 + utostr(i) + " (" + TArgs[i] + ") of subclass '" +
Chris Lattnerf4601652007-11-22 20:49:04 +0000176 SC->getName() + "'!");
177 }
178 }
179
180 // Since everything went well, we can now set the "superclass" list for the
181 // current record.
182 const std::vector<Record*> &SCs = SC->getSuperClasses();
183 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
184 if (CurRec->isSubClassOf(SCs[i]))
185 return Error(SubClass.RefLoc,
186 "Already subclass of '" + SCs[i]->getName() + "'!\n");
187 CurRec->addSuperClass(SCs[i]);
188 }
Bob Wilson21870412009-11-22 04:24:42 +0000189
Chris Lattnerf4601652007-11-22 20:49:04 +0000190 if (CurRec->isSubClassOf(SC))
191 return Error(SubClass.RefLoc,
192 "Already subclass of '" + SC->getName() + "'!\n");
193 CurRec->addSuperClass(SC);
194 return false;
195}
196
David Greenede444af2009-04-22 16:42:54 +0000197/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilson440548d2009-04-30 18:26:19 +0000198/// CurMC, resolving its template args as SubMultiClass's
David Greenede444af2009-04-22 16:42:54 +0000199/// template arguments.
Bob Wilson440548d2009-04-30 18:26:19 +0000200bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson1d512df2009-04-30 17:46:20 +0000201 SubMultiClassReference &SubMultiClass) {
David Greenede444af2009-04-22 16:42:54 +0000202 MultiClass *SMC = SubMultiClass.MC;
Bob Wilson440548d2009-04-30 18:26:19 +0000203 Record *CurRec = &CurMC->Rec;
David Greenede444af2009-04-22 16:42:54 +0000204
Bob Wilson440548d2009-04-30 18:26:19 +0000205 const std::vector<RecordVal> &MCVals = CurRec->getValues();
David Greenede444af2009-04-22 16:42:54 +0000206
207 // Add all of the values in the subclass into the current class.
208 const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues();
209 for (unsigned i = 0, e = SMCVals.size(); i != e; ++i)
210 if (AddValue(CurRec, SubMultiClass.RefLoc, SMCVals[i]))
211 return true;
212
Bob Wilson440548d2009-04-30 18:26:19 +0000213 int newDefStart = CurMC->DefPrototypes.size();
David Greened34a73b2009-04-24 16:55:41 +0000214
David Greenede444af2009-04-22 16:42:54 +0000215 // Add all of the defs in the subclass into the current multiclass.
216 for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(),
217 iend = SMC->DefPrototypes.end();
218 i != iend;
219 ++i) {
220 // Clone the def and add it to the current multiclass
221 Record *NewDef = new Record(**i);
222
223 // Add all of the values in the superclass into the current def.
224 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
225 if (AddValue(NewDef, SubMultiClass.RefLoc, MCVals[i]))
226 return true;
227
Bob Wilson440548d2009-04-30 18:26:19 +0000228 CurMC->DefPrototypes.push_back(NewDef);
David Greenede444af2009-04-22 16:42:54 +0000229 }
Bob Wilson32558652009-04-28 19:41:44 +0000230
David Greenede444af2009-04-22 16:42:54 +0000231 const std::vector<std::string> &SMCTArgs = SMC->Rec.getTemplateArgs();
232
David Greened34a73b2009-04-24 16:55:41 +0000233 // Ensure that an appropriate number of template arguments are
234 // specified.
David Greenede444af2009-04-22 16:42:54 +0000235 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
David Greened34a73b2009-04-24 16:55:41 +0000236 return Error(SubMultiClass.RefLoc,
237 "More template args specified than expected");
Bob Wilson32558652009-04-28 19:41:44 +0000238
David Greenede444af2009-04-22 16:42:54 +0000239 // Loop over all of the template arguments, setting them to the specified
240 // value or leaving them as the default if necessary.
241 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
242 if (i < SubMultiClass.TemplateArgs.size()) {
David Greened34a73b2009-04-24 16:55:41 +0000243 // If a value is specified for this template arg, set it in the
244 // superclass now.
245 if (SetValue(CurRec, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000246 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000247 SubMultiClass.TemplateArgs[i]))
248 return true;
249
250 // Resolve it next.
251 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson32558652009-04-28 19:41:44 +0000252
David Greenede444af2009-04-22 16:42:54 +0000253 // Now remove it.
254 CurRec->removeValue(SMCTArgs[i]);
255
David Greened34a73b2009-04-24 16:55:41 +0000256 // If a value is specified for this template arg, set it in the
257 // new defs now.
258 for (MultiClass::RecordVector::iterator j =
Bob Wilson440548d2009-04-30 18:26:19 +0000259 CurMC->DefPrototypes.begin() + newDefStart,
260 jend = CurMC->DefPrototypes.end();
David Greenede444af2009-04-22 16:42:54 +0000261 j != jend;
262 ++j) {
263 Record *Def = *j;
264
David Greened34a73b2009-04-24 16:55:41 +0000265 if (SetValue(Def, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000266 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000267 SubMultiClass.TemplateArgs[i]))
268 return true;
269
270 // Resolve it next.
271 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
272
273 // Now remove it
274 Def->removeValue(SMCTArgs[i]);
275 }
276 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
David Greened34a73b2009-04-24 16:55:41 +0000277 return Error(SubMultiClass.RefLoc,
278 "Value not specified for template argument #"
Bob Wilson32558652009-04-28 19:41:44 +0000279 + utostr(i) + " (" + SMCTArgs[i] + ") of subclass '" +
David Greenede444af2009-04-22 16:42:54 +0000280 SMC->Rec.getName() + "'!");
281 }
282 }
283
284 return false;
285}
286
Chris Lattnerf4601652007-11-22 20:49:04 +0000287//===----------------------------------------------------------------------===//
288// Parser Code
289//===----------------------------------------------------------------------===//
290
291/// isObjectStart - Return true if this is a valid first token for an Object.
292static bool isObjectStart(tgtok::TokKind K) {
293 return K == tgtok::Class || K == tgtok::Def ||
Bob Wilson21870412009-11-22 04:24:42 +0000294 K == tgtok::Defm || K == tgtok::Let || K == tgtok::MultiClass;
Chris Lattnerf4601652007-11-22 20:49:04 +0000295}
296
297/// ParseObjectName - If an object name is specified, return it. Otherwise,
298/// return an anonymous name.
299/// ObjectName ::= ID
300/// ObjectName ::= /*empty*/
301///
302std::string TGParser::ParseObjectName() {
303 if (Lex.getCode() == tgtok::Id) {
304 std::string Ret = Lex.getCurStrVal();
305 Lex.Lex();
306 return Ret;
307 }
Bob Wilson21870412009-11-22 04:24:42 +0000308
Chris Lattnerf4601652007-11-22 20:49:04 +0000309 static unsigned AnonCounter = 0;
310 return "anonymous."+utostr(AnonCounter++);
311}
312
313
314/// ParseClassID - Parse and resolve a reference to a class name. This returns
315/// null on error.
316///
317/// ClassID ::= ID
318///
319Record *TGParser::ParseClassID() {
320 if (Lex.getCode() != tgtok::Id) {
321 TokError("expected name for ClassID");
322 return 0;
323 }
Bob Wilson21870412009-11-22 04:24:42 +0000324
Chris Lattnerf4601652007-11-22 20:49:04 +0000325 Record *Result = Records.getClass(Lex.getCurStrVal());
326 if (Result == 0)
327 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson21870412009-11-22 04:24:42 +0000328
Chris Lattnerf4601652007-11-22 20:49:04 +0000329 Lex.Lex();
330 return Result;
331}
332
Bob Wilson32558652009-04-28 19:41:44 +0000333/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
334/// This returns null on error.
David Greenede444af2009-04-22 16:42:54 +0000335///
336/// MultiClassID ::= ID
337///
338MultiClass *TGParser::ParseMultiClassID() {
339 if (Lex.getCode() != tgtok::Id) {
340 TokError("expected name for ClassID");
341 return 0;
342 }
Bob Wilson32558652009-04-28 19:41:44 +0000343
David Greenede444af2009-04-22 16:42:54 +0000344 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
345 if (Result == 0)
346 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson32558652009-04-28 19:41:44 +0000347
David Greenede444af2009-04-22 16:42:54 +0000348 Lex.Lex();
349 return Result;
350}
351
Chris Lattnerf4601652007-11-22 20:49:04 +0000352Record *TGParser::ParseDefmID() {
353 if (Lex.getCode() != tgtok::Id) {
354 TokError("expected multiclass name");
355 return 0;
356 }
Bob Wilson21870412009-11-22 04:24:42 +0000357
Chris Lattnerf4601652007-11-22 20:49:04 +0000358 MultiClass *MC = MultiClasses[Lex.getCurStrVal()];
359 if (MC == 0) {
360 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
361 return 0;
362 }
Bob Wilson21870412009-11-22 04:24:42 +0000363
Chris Lattnerf4601652007-11-22 20:49:04 +0000364 Lex.Lex();
365 return &MC->Rec;
Bob Wilson21870412009-11-22 04:24:42 +0000366}
Chris Lattnerf4601652007-11-22 20:49:04 +0000367
368
369/// ParseSubClassReference - Parse a reference to a subclass or to a templated
370/// subclass. This returns a SubClassRefTy with a null Record* on error.
371///
372/// SubClassRef ::= ClassID
373/// SubClassRef ::= ClassID '<' ValueList '>'
374///
375SubClassReference TGParser::
376ParseSubClassReference(Record *CurRec, bool isDefm) {
377 SubClassReference Result;
378 Result.RefLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +0000379
Chris Lattnerf4601652007-11-22 20:49:04 +0000380 if (isDefm)
381 Result.Rec = ParseDefmID();
382 else
383 Result.Rec = ParseClassID();
384 if (Result.Rec == 0) return Result;
Bob Wilson21870412009-11-22 04:24:42 +0000385
Chris Lattnerf4601652007-11-22 20:49:04 +0000386 // If there is no template arg list, we're done.
387 if (Lex.getCode() != tgtok::less)
388 return Result;
389 Lex.Lex(); // Eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000390
Chris Lattnerf4601652007-11-22 20:49:04 +0000391 if (Lex.getCode() == tgtok::greater) {
392 TokError("subclass reference requires a non-empty list of template values");
393 Result.Rec = 0;
394 return Result;
395 }
Bob Wilson21870412009-11-22 04:24:42 +0000396
David Greenee1b46912009-06-08 20:23:18 +0000397 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4601652007-11-22 20:49:04 +0000398 if (Result.TemplateArgs.empty()) {
399 Result.Rec = 0; // Error parsing value list.
400 return Result;
401 }
Bob Wilson21870412009-11-22 04:24:42 +0000402
Chris Lattnerf4601652007-11-22 20:49:04 +0000403 if (Lex.getCode() != tgtok::greater) {
404 TokError("expected '>' in template value list");
405 Result.Rec = 0;
406 return Result;
407 }
408 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000409
Chris Lattnerf4601652007-11-22 20:49:04 +0000410 return Result;
411}
412
Bob Wilson32558652009-04-28 19:41:44 +0000413/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
414/// templated submulticlass. This returns a SubMultiClassRefTy with a null
415/// Record* on error.
David Greenede444af2009-04-22 16:42:54 +0000416///
417/// SubMultiClassRef ::= MultiClassID
418/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
419///
420SubMultiClassReference TGParser::
421ParseSubMultiClassReference(MultiClass *CurMC) {
422 SubMultiClassReference Result;
423 Result.RefLoc = Lex.getLoc();
Bob Wilson32558652009-04-28 19:41:44 +0000424
David Greenede444af2009-04-22 16:42:54 +0000425 Result.MC = ParseMultiClassID();
426 if (Result.MC == 0) return Result;
Bob Wilson32558652009-04-28 19:41:44 +0000427
David Greenede444af2009-04-22 16:42:54 +0000428 // If there is no template arg list, we're done.
429 if (Lex.getCode() != tgtok::less)
430 return Result;
431 Lex.Lex(); // Eat the '<'
Bob Wilson32558652009-04-28 19:41:44 +0000432
David Greenede444af2009-04-22 16:42:54 +0000433 if (Lex.getCode() == tgtok::greater) {
434 TokError("subclass reference requires a non-empty list of template values");
435 Result.MC = 0;
436 return Result;
437 }
Bob Wilson32558652009-04-28 19:41:44 +0000438
David Greenee1b46912009-06-08 20:23:18 +0000439 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greenede444af2009-04-22 16:42:54 +0000440 if (Result.TemplateArgs.empty()) {
441 Result.MC = 0; // Error parsing value list.
442 return Result;
443 }
Bob Wilson32558652009-04-28 19:41:44 +0000444
David Greenede444af2009-04-22 16:42:54 +0000445 if (Lex.getCode() != tgtok::greater) {
446 TokError("expected '>' in template value list");
447 Result.MC = 0;
448 return Result;
449 }
450 Lex.Lex();
451
452 return Result;
453}
454
Chris Lattnerf4601652007-11-22 20:49:04 +0000455/// ParseRangePiece - Parse a bit/value range.
456/// RangePiece ::= INTVAL
457/// RangePiece ::= INTVAL '-' INTVAL
458/// RangePiece ::= INTVAL INTVAL
459bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner811281e2008-01-10 07:01:53 +0000460 if (Lex.getCode() != tgtok::IntVal) {
461 TokError("expected integer or bitrange");
462 return true;
463 }
Dan Gohman63f97202008-10-17 01:33:43 +0000464 int64_t Start = Lex.getCurIntVal();
465 int64_t End;
Bob Wilson21870412009-11-22 04:24:42 +0000466
Chris Lattnerf4601652007-11-22 20:49:04 +0000467 if (Start < 0)
468 return TokError("invalid range, cannot be negative");
Bob Wilson21870412009-11-22 04:24:42 +0000469
Chris Lattnerf4601652007-11-22 20:49:04 +0000470 switch (Lex.Lex()) { // eat first character.
Bob Wilson21870412009-11-22 04:24:42 +0000471 default:
Chris Lattnerf4601652007-11-22 20:49:04 +0000472 Ranges.push_back(Start);
473 return false;
474 case tgtok::minus:
475 if (Lex.Lex() != tgtok::IntVal) {
476 TokError("expected integer value as end of range");
477 return true;
478 }
479 End = Lex.getCurIntVal();
480 break;
481 case tgtok::IntVal:
482 End = -Lex.getCurIntVal();
483 break;
484 }
Bob Wilson21870412009-11-22 04:24:42 +0000485 if (End < 0)
Chris Lattnerf4601652007-11-22 20:49:04 +0000486 return TokError("invalid range, cannot be negative");
487 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000488
Chris Lattnerf4601652007-11-22 20:49:04 +0000489 // Add to the range.
490 if (Start < End) {
491 for (; Start <= End; ++Start)
492 Ranges.push_back(Start);
493 } else {
494 for (; Start >= End; --Start)
495 Ranges.push_back(Start);
496 }
497 return false;
498}
499
500/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
501///
502/// RangeList ::= RangePiece (',' RangePiece)*
503///
504std::vector<unsigned> TGParser::ParseRangeList() {
505 std::vector<unsigned> Result;
Bob Wilson21870412009-11-22 04:24:42 +0000506
Chris Lattnerf4601652007-11-22 20:49:04 +0000507 // Parse the first piece.
508 if (ParseRangePiece(Result))
509 return std::vector<unsigned>();
510 while (Lex.getCode() == tgtok::comma) {
511 Lex.Lex(); // Eat the comma.
512
513 // Parse the next range piece.
514 if (ParseRangePiece(Result))
515 return std::vector<unsigned>();
516 }
517 return Result;
518}
519
520/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
521/// OptionalRangeList ::= '<' RangeList '>'
522/// OptionalRangeList ::= /*empty*/
523bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
524 if (Lex.getCode() != tgtok::less)
525 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000526
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000527 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000528 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000529
Chris Lattnerf4601652007-11-22 20:49:04 +0000530 // Parse the range list.
531 Ranges = ParseRangeList();
532 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000533
Chris Lattnerf4601652007-11-22 20:49:04 +0000534 if (Lex.getCode() != tgtok::greater) {
535 TokError("expected '>' at end of range list");
536 return Error(StartLoc, "to match this '<'");
537 }
538 Lex.Lex(); // eat the '>'.
539 return false;
540}
541
542/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
543/// OptionalBitList ::= '{' RangeList '}'
544/// OptionalBitList ::= /*empty*/
545bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
546 if (Lex.getCode() != tgtok::l_brace)
547 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000548
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000549 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000550 Lex.Lex(); // eat the '{'
Bob Wilson21870412009-11-22 04:24:42 +0000551
Chris Lattnerf4601652007-11-22 20:49:04 +0000552 // Parse the range list.
553 Ranges = ParseRangeList();
554 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000555
Chris Lattnerf4601652007-11-22 20:49:04 +0000556 if (Lex.getCode() != tgtok::r_brace) {
557 TokError("expected '}' at end of bit list");
558 return Error(StartLoc, "to match this '{'");
559 }
560 Lex.Lex(); // eat the '}'.
561 return false;
562}
563
564
565/// ParseType - Parse and return a tblgen type. This returns null on error.
566///
567/// Type ::= STRING // string type
568/// Type ::= BIT // bit type
569/// Type ::= BITS '<' INTVAL '>' // bits<x> type
570/// Type ::= INT // int type
571/// Type ::= LIST '<' Type '>' // list<x> type
572/// Type ::= CODE // code type
573/// Type ::= DAG // dag type
574/// Type ::= ClassID // Record Type
575///
576RecTy *TGParser::ParseType() {
577 switch (Lex.getCode()) {
578 default: TokError("Unknown token when expecting a type"); return 0;
579 case tgtok::String: Lex.Lex(); return new StringRecTy();
580 case tgtok::Bit: Lex.Lex(); return new BitRecTy();
581 case tgtok::Int: Lex.Lex(); return new IntRecTy();
582 case tgtok::Code: Lex.Lex(); return new CodeRecTy();
583 case tgtok::Dag: Lex.Lex(); return new DagRecTy();
584 case tgtok::Id:
585 if (Record *R = ParseClassID()) return new RecordRecTy(R);
586 return 0;
587 case tgtok::Bits: {
588 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
589 TokError("expected '<' after bits type");
590 return 0;
591 }
592 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
593 TokError("expected integer in bits<n> type");
594 return 0;
595 }
Dan Gohman63f97202008-10-17 01:33:43 +0000596 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4601652007-11-22 20:49:04 +0000597 if (Lex.Lex() != tgtok::greater) { // Eat count.
598 TokError("expected '>' at end of bits<n> type");
599 return 0;
600 }
601 Lex.Lex(); // Eat '>'
602 return new BitsRecTy(Val);
603 }
604 case tgtok::List: {
605 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
606 TokError("expected '<' after list type");
607 return 0;
608 }
609 Lex.Lex(); // Eat '<'
610 RecTy *SubType = ParseType();
611 if (SubType == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +0000612
Chris Lattnerf4601652007-11-22 20:49:04 +0000613 if (Lex.getCode() != tgtok::greater) {
614 TokError("expected '>' at end of list<ty> type");
615 return 0;
616 }
617 Lex.Lex(); // Eat '>'
618 return new ListRecTy(SubType);
619 }
Bob Wilson21870412009-11-22 04:24:42 +0000620 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000621}
622
623/// ParseIDValue - Parse an ID as a value and decode what it means.
624///
625/// IDValue ::= ID [def local value]
626/// IDValue ::= ID [def template arg]
627/// IDValue ::= ID [multiclass local value]
628/// IDValue ::= ID [multiclass template argument]
629/// IDValue ::= ID [def name]
630///
631Init *TGParser::ParseIDValue(Record *CurRec) {
632 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
633 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000634 SMLoc Loc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000635 Lex.Lex();
636 return ParseIDValue(CurRec, Name, Loc);
637}
638
639/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
640/// has already been read.
Bob Wilson21870412009-11-22 04:24:42 +0000641Init *TGParser::ParseIDValue(Record *CurRec,
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000642 const std::string &Name, SMLoc NameLoc) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000643 if (CurRec) {
644 if (const RecordVal *RV = CurRec->getValue(Name))
645 return new VarInit(Name, RV->getType());
Bob Wilson21870412009-11-22 04:24:42 +0000646
Chris Lattnerf4601652007-11-22 20:49:04 +0000647 std::string TemplateArgName = CurRec->getName()+":"+Name;
648 if (CurRec->isTemplateArg(TemplateArgName)) {
649 const RecordVal *RV = CurRec->getValue(TemplateArgName);
650 assert(RV && "Template arg doesn't exist??");
651 return new VarInit(TemplateArgName, RV->getType());
652 }
653 }
Bob Wilson21870412009-11-22 04:24:42 +0000654
Chris Lattnerf4601652007-11-22 20:49:04 +0000655 if (CurMultiClass) {
656 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
657 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
658 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
659 assert(RV && "Template arg doesn't exist??");
660 return new VarInit(MCName, RV->getType());
661 }
662 }
Bob Wilson21870412009-11-22 04:24:42 +0000663
Chris Lattnerf4601652007-11-22 20:49:04 +0000664 if (Record *D = Records.getDef(Name))
665 return new DefInit(D);
666
667 Error(NameLoc, "Variable not defined: '" + Name + "'");
668 return 0;
669}
670
David Greened418c1b2009-05-14 20:54:48 +0000671/// ParseOperation - Parse an operator. This returns null on error.
672///
673/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
674///
675Init *TGParser::ParseOperation(Record *CurRec) {
676 switch (Lex.getCode()) {
677 default:
678 TokError("unknown operation");
679 return 0;
680 break;
David Greene5f9f9ba2009-05-14 22:38:31 +0000681 case tgtok::XCar:
682 case tgtok::XCdr:
683 case tgtok::XNull:
David Greenee6c27de2009-05-14 21:22:49 +0000684 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
685 UnOpInit::UnaryOp Code;
686 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000687
David Greenee6c27de2009-05-14 21:22:49 +0000688 switch (Lex.getCode()) {
689 default: assert(0 && "Unhandled code!");
690 case tgtok::XCast:
691 Lex.Lex(); // eat the operation
692 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000693
David Greenee6c27de2009-05-14 21:22:49 +0000694 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000695
David Greenee6c27de2009-05-14 21:22:49 +0000696 if (Type == 0) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000697 TokError("did not get type for unary operator");
David Greenee6c27de2009-05-14 21:22:49 +0000698 return 0;
699 }
David Greened418c1b2009-05-14 20:54:48 +0000700
David Greenee6c27de2009-05-14 21:22:49 +0000701 break;
David Greene5f9f9ba2009-05-14 22:38:31 +0000702 case tgtok::XCar:
703 Lex.Lex(); // eat the operation
704 Code = UnOpInit::CAR;
705 break;
706 case tgtok::XCdr:
707 Lex.Lex(); // eat the operation
708 Code = UnOpInit::CDR;
709 break;
710 case tgtok::XNull:
711 Lex.Lex(); // eat the operation
712 Code = UnOpInit::LNULL;
713 Type = new IntRecTy;
714 break;
David Greenee6c27de2009-05-14 21:22:49 +0000715 }
716 if (Lex.getCode() != tgtok::l_paren) {
717 TokError("expected '(' after unary operator");
718 return 0;
719 }
720 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000721
David Greenee6c27de2009-05-14 21:22:49 +0000722 Init *LHS = ParseValue(CurRec);
723 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000724
David Greene5f9f9ba2009-05-14 22:38:31 +0000725 if (Code == UnOpInit::CAR
726 || Code == UnOpInit::CDR
727 || Code == UnOpInit::LNULL) {
728 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000729 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greene5f9f9ba2009-05-14 22:38:31 +0000730 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000731 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
732 TokError("expected list or string type argument in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000733 return 0;
734 }
735 if (LHSt) {
736 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
David Greenee1b46912009-06-08 20:23:18 +0000737 StringRecTy *SType = dynamic_cast<StringRecTy*>(LHSt->getType());
738 if (LType == 0 && SType == 0) {
739 TokError("expected list or string type argumnet in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000740 return 0;
741 }
742 }
743
744 if (Code == UnOpInit::CAR
745 || Code == UnOpInit::CDR) {
David Greenee1b46912009-06-08 20:23:18 +0000746 if (LHSl == 0 && LHSt == 0) {
747 TokError("expected list type argumnet in unary operator");
748 return 0;
749 }
Bob Wilson21870412009-11-22 04:24:42 +0000750
David Greene5f9f9ba2009-05-14 22:38:31 +0000751 if (LHSl && LHSl->getSize() == 0) {
752 TokError("empty list argument in unary operator");
753 return 0;
754 }
755 if (LHSl) {
756 Init *Item = LHSl->getElement(0);
757 TypedInit *Itemt = dynamic_cast<TypedInit*>(Item);
758 if (Itemt == 0) {
759 TokError("untyped list element in unary operator");
760 return 0;
761 }
762 if (Code == UnOpInit::CAR) {
763 Type = Itemt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000764 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000765 Type = new ListRecTy(Itemt->getType());
766 }
Bob Wilson21870412009-11-22 04:24:42 +0000767 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000768 assert(LHSt && "expected list type argument in unary operator");
769 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
770 if (LType == 0) {
771 TokError("expected list type argumnet in unary operator");
772 return 0;
773 }
774 if (Code == UnOpInit::CAR) {
775 Type = LType->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +0000776 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000777 Type = LType;
778 }
779 }
780 }
781 }
782
David Greenee6c27de2009-05-14 21:22:49 +0000783 if (Lex.getCode() != tgtok::r_paren) {
784 TokError("expected ')' in unary operator");
785 return 0;
786 }
787 Lex.Lex(); // eat the ')'
788 return (new UnOpInit(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
789 }
David Greened418c1b2009-05-14 20:54:48 +0000790
791 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +0000792 case tgtok::XSRA:
David Greened418c1b2009-05-14 20:54:48 +0000793 case tgtok::XSRL:
794 case tgtok::XSHL:
795 case tgtok::XStrConcat:
796 case tgtok::XNameConcat: { // Value ::= !binop '(' Value ',' Value ')'
797 BinOpInit::BinaryOp Code;
798 RecTy *Type = 0;
799
800
801 switch (Lex.getCode()) {
802 default: assert(0 && "Unhandled code!");
Bob Wilson21870412009-11-22 04:24:42 +0000803 case tgtok::XConcat:
David Greened418c1b2009-05-14 20:54:48 +0000804 Lex.Lex(); // eat the operation
805 Code = BinOpInit::CONCAT;
806 Type = new DagRecTy();
807 break;
Bob Wilson21870412009-11-22 04:24:42 +0000808 case tgtok::XSRA:
David Greened418c1b2009-05-14 20:54:48 +0000809 Lex.Lex(); // eat the operation
810 Code = BinOpInit::SRA;
811 Type = new IntRecTy();
812 break;
Bob Wilson21870412009-11-22 04:24:42 +0000813 case tgtok::XSRL:
David Greened418c1b2009-05-14 20:54:48 +0000814 Lex.Lex(); // eat the operation
815 Code = BinOpInit::SRL;
816 Type = new IntRecTy();
817 break;
Bob Wilson21870412009-11-22 04:24:42 +0000818 case tgtok::XSHL:
David Greened418c1b2009-05-14 20:54:48 +0000819 Lex.Lex(); // eat the operation
820 Code = BinOpInit::SHL;
821 Type = new IntRecTy();
822 break;
Bob Wilson21870412009-11-22 04:24:42 +0000823 case tgtok::XStrConcat:
David Greened418c1b2009-05-14 20:54:48 +0000824 Lex.Lex(); // eat the operation
825 Code = BinOpInit::STRCONCAT;
826 Type = new StringRecTy();
827 break;
Bob Wilson21870412009-11-22 04:24:42 +0000828 case tgtok::XNameConcat:
David Greened418c1b2009-05-14 20:54:48 +0000829 Lex.Lex(); // eat the operation
830 Code = BinOpInit::NAMECONCAT;
831
832 Type = ParseOperatorType();
833
834 if (Type == 0) {
835 TokError("did not get type for binary operator");
836 return 0;
837 }
838
839 break;
840 }
841 if (Lex.getCode() != tgtok::l_paren) {
842 TokError("expected '(' after binary operator");
843 return 0;
844 }
845 Lex.Lex(); // eat the '('
846
847 Init *LHS = ParseValue(CurRec);
848 if (LHS == 0) return 0;
849
850 if (Lex.getCode() != tgtok::comma) {
851 TokError("expected ',' in binary operator");
852 return 0;
853 }
854 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000855
David Greened418c1b2009-05-14 20:54:48 +0000856 Init *RHS = ParseValue(CurRec);
857 if (RHS == 0) return 0;
858
859 if (Lex.getCode() != tgtok::r_paren) {
860 TokError("expected ')' in binary operator");
861 return 0;
862 }
863 Lex.Lex(); // eat the ')'
864 return (new BinOpInit(Code, LHS, RHS, Type))->Fold(CurRec, CurMultiClass);
865 }
866
David Greene9bea7c82009-05-14 23:26:46 +0000867 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +0000868 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +0000869 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
870 TernOpInit::TernaryOp Code;
871 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000872
873
David Greene4afc5092009-05-14 21:54:42 +0000874 tgtok::TokKind LexCode = Lex.getCode();
875 Lex.Lex(); // eat the operation
876 switch (LexCode) {
877 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000878 case tgtok::XIf:
879 Code = TernOpInit::IF;
880 break;
David Greenebeb31a52009-05-14 22:23:47 +0000881 case tgtok::XForEach:
882 Code = TernOpInit::FOREACH;
883 break;
David Greene4afc5092009-05-14 21:54:42 +0000884 case tgtok::XSubst:
885 Code = TernOpInit::SUBST;
886 break;
887 }
888 if (Lex.getCode() != tgtok::l_paren) {
889 TokError("expected '(' after ternary operator");
890 return 0;
891 }
892 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000893
David Greene4afc5092009-05-14 21:54:42 +0000894 Init *LHS = ParseValue(CurRec);
895 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000896
David Greene4afc5092009-05-14 21:54:42 +0000897 if (Lex.getCode() != tgtok::comma) {
898 TokError("expected ',' in ternary operator");
899 return 0;
900 }
901 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000902
David Greene4afc5092009-05-14 21:54:42 +0000903 Init *MHS = ParseValue(CurRec);
904 if (MHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000905
David Greene4afc5092009-05-14 21:54:42 +0000906 if (Lex.getCode() != tgtok::comma) {
907 TokError("expected ',' in ternary operator");
908 return 0;
909 }
910 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000911
David Greene4afc5092009-05-14 21:54:42 +0000912 Init *RHS = ParseValue(CurRec);
913 if (RHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000914
David Greene4afc5092009-05-14 21:54:42 +0000915 if (Lex.getCode() != tgtok::r_paren) {
916 TokError("expected ')' in binary operator");
917 return 0;
918 }
919 Lex.Lex(); // eat the ')'
David Greened418c1b2009-05-14 20:54:48 +0000920
David Greene4afc5092009-05-14 21:54:42 +0000921 switch (LexCode) {
922 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000923 case tgtok::XIf: {
924 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
925 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
926 if (MHSt == 0 || RHSt == 0) {
927 TokError("could not get type for !if");
928 return 0;
929 }
930 if (MHSt->getType()->typeIsConvertibleTo(RHSt->getType())) {
931 Type = RHSt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000932 } else if (RHSt->getType()->typeIsConvertibleTo(MHSt->getType())) {
David Greene9bea7c82009-05-14 23:26:46 +0000933 Type = MHSt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000934 } else {
David Greene9bea7c82009-05-14 23:26:46 +0000935 TokError("inconsistent types for !if");
936 return 0;
937 }
938 break;
939 }
David Greenebeb31a52009-05-14 22:23:47 +0000940 case tgtok::XForEach: {
941 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
942 if (MHSt == 0) {
943 TokError("could not get type for !foreach");
944 return 0;
945 }
946 Type = MHSt->getType();
947 break;
948 }
David Greene4afc5092009-05-14 21:54:42 +0000949 case tgtok::XSubst: {
950 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
951 if (RHSt == 0) {
952 TokError("could not get type for !subst");
953 return 0;
954 }
955 Type = RHSt->getType();
956 break;
957 }
958 }
Bob Wilson21870412009-11-22 04:24:42 +0000959 return (new TernOpInit(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
960 CurMultiClass);
David Greene4afc5092009-05-14 21:54:42 +0000961 }
David Greened418c1b2009-05-14 20:54:48 +0000962 }
963 TokError("could not parse operation");
964 return 0;
965}
966
967/// ParseOperatorType - Parse a type for an operator. This returns
968/// null on error.
969///
970/// OperatorType ::= '<' Type '>'
971///
Dan Gohmana9ad0412009-08-12 22:10:57 +0000972RecTy *TGParser::ParseOperatorType() {
David Greened418c1b2009-05-14 20:54:48 +0000973 RecTy *Type = 0;
974
975 if (Lex.getCode() != tgtok::less) {
976 TokError("expected type name for operator");
977 return 0;
978 }
979 Lex.Lex(); // eat the <
980
981 Type = ParseType();
982
983 if (Type == 0) {
984 TokError("expected type name for operator");
985 return 0;
986 }
987
988 if (Lex.getCode() != tgtok::greater) {
989 TokError("expected type name for operator");
990 return 0;
991 }
992 Lex.Lex(); // eat the >
993
994 return Type;
995}
996
997
Chris Lattnerf4601652007-11-22 20:49:04 +0000998/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
999///
1000/// SimpleValue ::= IDValue
1001/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001002/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +00001003/// SimpleValue ::= CODEFRAGMENT
1004/// SimpleValue ::= '?'
1005/// SimpleValue ::= '{' ValueList '}'
1006/// SimpleValue ::= ID '<' ValueListNE '>'
1007/// SimpleValue ::= '[' ValueList ']'
1008/// SimpleValue ::= '(' IDValue DagArgList ')'
1009/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
1010/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1011/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1012/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1013/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1014///
David Greenee1b46912009-06-08 20:23:18 +00001015Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001016 Init *R = 0;
1017 switch (Lex.getCode()) {
1018 default: TokError("Unknown token when parsing a value"); break;
1019 case tgtok::IntVal: R = new IntInit(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001020 case tgtok::StrVal: {
1021 std::string Val = Lex.getCurStrVal();
1022 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001023
Jim Grosbachda4231f2009-03-26 16:17:51 +00001024 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001025 while (Lex.getCode() == tgtok::StrVal) {
1026 Val += Lex.getCurStrVal();
1027 Lex.Lex();
1028 }
Bob Wilson21870412009-11-22 04:24:42 +00001029
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001030 R = new StringInit(Val);
1031 break;
1032 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001033 case tgtok::CodeFragment:
Bob Wilson21870412009-11-22 04:24:42 +00001034 R = new CodeInit(Lex.getCurStrVal()); Lex.Lex(); break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001035 case tgtok::question: R = new UnsetInit(); Lex.Lex(); break;
1036 case tgtok::Id: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001037 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001038 std::string Name = Lex.getCurStrVal();
1039 if (Lex.Lex() != tgtok::less) // consume the Id.
1040 return ParseIDValue(CurRec, Name, NameLoc); // Value ::= IDValue
Bob Wilson21870412009-11-22 04:24:42 +00001041
Chris Lattnerf4601652007-11-22 20:49:04 +00001042 // Value ::= ID '<' ValueListNE '>'
1043 if (Lex.Lex() == tgtok::greater) {
1044 TokError("expected non-empty value list");
1045 return 0;
1046 }
David Greenee1b46912009-06-08 20:23:18 +00001047
Chris Lattnerf4601652007-11-22 20:49:04 +00001048 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1049 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1050 // body.
1051 Record *Class = Records.getClass(Name);
1052 if (!Class) {
1053 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1054 return 0;
1055 }
David Greenee1b46912009-06-08 20:23:18 +00001056
1057 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
1058 if (ValueList.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001059
David Greenee1b46912009-06-08 20:23:18 +00001060 if (Lex.getCode() != tgtok::greater) {
1061 TokError("expected '>' at end of value list");
1062 return 0;
1063 }
1064 Lex.Lex(); // eat the '>'
Bob Wilson21870412009-11-22 04:24:42 +00001065
Chris Lattnerf4601652007-11-22 20:49:04 +00001066 // Create the new record, set it as CurRec temporarily.
1067 static unsigned AnonCounter = 0;
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001068 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),NameLoc);
Chris Lattnerf4601652007-11-22 20:49:04 +00001069 SubClassReference SCRef;
1070 SCRef.RefLoc = NameLoc;
1071 SCRef.Rec = Class;
1072 SCRef.TemplateArgs = ValueList;
1073 // Add info about the subclass to NewRec.
1074 if (AddSubClass(NewRec, SCRef))
1075 return 0;
1076 NewRec->resolveReferences();
1077 Records.addDef(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001078
Chris Lattnerf4601652007-11-22 20:49:04 +00001079 // The result of the expression is a reference to the new record.
1080 return new DefInit(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001081 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001082 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001083 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001084 Lex.Lex(); // eat the '{'
1085 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001086
Chris Lattnerf4601652007-11-22 20:49:04 +00001087 if (Lex.getCode() != tgtok::r_brace) {
1088 Vals = ParseValueList(CurRec);
1089 if (Vals.empty()) return 0;
1090 }
1091 if (Lex.getCode() != tgtok::r_brace) {
1092 TokError("expected '}' at end of bit list value");
1093 return 0;
1094 }
1095 Lex.Lex(); // eat the '}'
Bob Wilson21870412009-11-22 04:24:42 +00001096
Chris Lattnerf4601652007-11-22 20:49:04 +00001097 BitsInit *Result = new BitsInit(Vals.size());
1098 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1099 Init *Bit = Vals[i]->convertInitializerTo(new BitRecTy());
1100 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +00001101 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1102 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +00001103 return 0;
1104 }
1105 Result->setBit(Vals.size()-i-1, Bit);
1106 }
1107 return Result;
1108 }
1109 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1110 Lex.Lex(); // eat the '['
1111 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001112
David Greenee1b46912009-06-08 20:23:18 +00001113 RecTy *DeducedEltTy = 0;
1114 ListRecTy *GivenListTy = 0;
Bob Wilson21870412009-11-22 04:24:42 +00001115
David Greenee1b46912009-06-08 20:23:18 +00001116 if (ItemType != 0) {
1117 ListRecTy *ListType = dynamic_cast<ListRecTy*>(ItemType);
1118 if (ListType == 0) {
1119 std::stringstream s;
Bob Wilson21870412009-11-22 04:24:42 +00001120 s << "Type mismatch for list, expected list type, got "
David Greenee1b46912009-06-08 20:23:18 +00001121 << ItemType->getAsString();
1122 TokError(s.str());
1123 }
1124 GivenListTy = ListType;
Bob Wilson21870412009-11-22 04:24:42 +00001125 }
David Greenee1b46912009-06-08 20:23:18 +00001126
Chris Lattnerf4601652007-11-22 20:49:04 +00001127 if (Lex.getCode() != tgtok::r_square) {
Bob Wilson21870412009-11-22 04:24:42 +00001128 Vals = ParseValueList(CurRec, 0,
1129 GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001130 if (Vals.empty()) return 0;
1131 }
1132 if (Lex.getCode() != tgtok::r_square) {
1133 TokError("expected ']' at end of list value");
1134 return 0;
1135 }
1136 Lex.Lex(); // eat the ']'
David Greenee1b46912009-06-08 20:23:18 +00001137
1138 RecTy *GivenEltTy = 0;
1139 if (Lex.getCode() == tgtok::less) {
1140 // Optional list element type
1141 Lex.Lex(); // eat the '<'
1142
1143 GivenEltTy = ParseType();
1144 if (GivenEltTy == 0) {
1145 // Couldn't parse element type
1146 return 0;
1147 }
1148
1149 if (Lex.getCode() != tgtok::greater) {
1150 TokError("expected '>' at end of list element type");
1151 return 0;
1152 }
1153 Lex.Lex(); // eat the '>'
1154 }
1155
1156 // Check elements
1157 RecTy *EltTy = 0;
1158 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
1159 i != ie;
1160 ++i) {
1161 TypedInit *TArg = dynamic_cast<TypedInit*>(*i);
1162 if (TArg == 0) {
1163 TokError("Untyped list element");
1164 return 0;
1165 }
1166 if (EltTy != 0) {
1167 EltTy = resolveTypes(EltTy, TArg->getType());
1168 if (EltTy == 0) {
1169 TokError("Incompatible types in list elements");
1170 return 0;
1171 }
Bob Wilson21870412009-11-22 04:24:42 +00001172 } else {
David Greenee1b46912009-06-08 20:23:18 +00001173 EltTy = TArg->getType();
1174 }
1175 }
1176
1177 if (GivenEltTy != 0) {
1178 if (EltTy != 0) {
1179 // Verify consistency
1180 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1181 TokError("Incompatible types in list elements");
1182 return 0;
1183 }
1184 }
1185 EltTy = GivenEltTy;
1186 }
1187
1188 if (EltTy == 0) {
1189 if (ItemType == 0) {
1190 TokError("No type for list");
1191 return 0;
1192 }
1193 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +00001194 } else {
David Greenee1b46912009-06-08 20:23:18 +00001195 // Make sure the deduced type is compatible with the given type
1196 if (GivenListTy) {
1197 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1198 TokError("Element type mismatch for list");
1199 return 0;
1200 }
1201 }
1202 DeducedEltTy = EltTy;
1203 }
Bob Wilson21870412009-11-22 04:24:42 +00001204
David Greenee1b46912009-06-08 20:23:18 +00001205 return new ListInit(Vals, DeducedEltTy);
Chris Lattnerf4601652007-11-22 20:49:04 +00001206 }
1207 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1208 Lex.Lex(); // eat the '('
David Greenec7cafcd2009-04-22 20:18:10 +00001209 if (Lex.getCode() != tgtok::Id
David Greenee6c27de2009-05-14 21:22:49 +00001210 && Lex.getCode() != tgtok::XCast
David Greenec7cafcd2009-04-22 20:18:10 +00001211 && Lex.getCode() != tgtok::XNameConcat) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001212 TokError("expected identifier in dag init");
1213 return 0;
1214 }
Bob Wilson21870412009-11-22 04:24:42 +00001215
David Greenec7cafcd2009-04-22 20:18:10 +00001216 Init *Operator = 0;
1217 if (Lex.getCode() == tgtok::Id) {
1218 Operator = ParseIDValue(CurRec);
1219 if (Operator == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001220 } else {
David Greened418c1b2009-05-14 20:54:48 +00001221 Operator = ParseOperation(CurRec);
1222 if (Operator == 0) return 0;
David Greenec7cafcd2009-04-22 20:18:10 +00001223 }
1224
Nate Begeman7cee8172009-03-19 05:21:56 +00001225 // If the operator name is present, parse it.
1226 std::string OperatorName;
1227 if (Lex.getCode() == tgtok::colon) {
1228 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1229 TokError("expected variable name in dag operator");
1230 return 0;
1231 }
1232 OperatorName = Lex.getCurStrVal();
1233 Lex.Lex(); // eat the VarName.
1234 }
Bob Wilson21870412009-11-22 04:24:42 +00001235
Chris Lattnerf4601652007-11-22 20:49:04 +00001236 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
1237 if (Lex.getCode() != tgtok::r_paren) {
1238 DagArgs = ParseDagArgList(CurRec);
1239 if (DagArgs.empty()) return 0;
1240 }
Bob Wilson21870412009-11-22 04:24:42 +00001241
Chris Lattnerf4601652007-11-22 20:49:04 +00001242 if (Lex.getCode() != tgtok::r_paren) {
1243 TokError("expected ')' in dag init");
1244 return 0;
1245 }
1246 Lex.Lex(); // eat the ')'
Bob Wilson21870412009-11-22 04:24:42 +00001247
Nate Begeman7cee8172009-03-19 05:21:56 +00001248 return new DagInit(Operator, OperatorName, DagArgs);
David Greened418c1b2009-05-14 20:54:48 +00001249 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001250 }
Bob Wilson21870412009-11-22 04:24:42 +00001251
David Greene5f9f9ba2009-05-14 22:38:31 +00001252 case tgtok::XCar:
1253 case tgtok::XCdr:
1254 case tgtok::XNull:
David Greenee6c27de2009-05-14 21:22:49 +00001255 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001256 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +00001257 case tgtok::XSRA:
Chris Lattnerf4601652007-11-22 20:49:04 +00001258 case tgtok::XSRL:
1259 case tgtok::XSHL:
David Greenec7cafcd2009-04-22 20:18:10 +00001260 case tgtok::XStrConcat:
David Greene4afc5092009-05-14 21:54:42 +00001261 case tgtok::XNameConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene9bea7c82009-05-14 23:26:46 +00001262 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +00001263 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001264 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greened418c1b2009-05-14 20:54:48 +00001265 return ParseOperation(CurRec);
1266 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001267 }
1268 }
Bob Wilson21870412009-11-22 04:24:42 +00001269
Chris Lattnerf4601652007-11-22 20:49:04 +00001270 return R;
1271}
1272
1273/// ParseValue - Parse a tblgen value. This returns null on error.
1274///
1275/// Value ::= SimpleValue ValueSuffix*
1276/// ValueSuffix ::= '{' BitList '}'
1277/// ValueSuffix ::= '[' BitList ']'
1278/// ValueSuffix ::= '.' ID
1279///
David Greenee1b46912009-06-08 20:23:18 +00001280Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType) {
1281 Init *Result = ParseSimpleValue(CurRec, ItemType);
Chris Lattnerf4601652007-11-22 20:49:04 +00001282 if (Result == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001283
Chris Lattnerf4601652007-11-22 20:49:04 +00001284 // Parse the suffixes now if present.
1285 while (1) {
1286 switch (Lex.getCode()) {
1287 default: return Result;
1288 case tgtok::l_brace: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001289 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001290 Lex.Lex(); // eat the '{'
1291 std::vector<unsigned> Ranges = ParseRangeList();
1292 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001293
Chris Lattnerf4601652007-11-22 20:49:04 +00001294 // Reverse the bitlist.
1295 std::reverse(Ranges.begin(), Ranges.end());
1296 Result = Result->convertInitializerBitRange(Ranges);
1297 if (Result == 0) {
1298 Error(CurlyLoc, "Invalid bit range for value");
1299 return 0;
1300 }
Bob Wilson21870412009-11-22 04:24:42 +00001301
Chris Lattnerf4601652007-11-22 20:49:04 +00001302 // Eat the '}'.
1303 if (Lex.getCode() != tgtok::r_brace) {
1304 TokError("expected '}' at end of bit range list");
1305 return 0;
1306 }
1307 Lex.Lex();
1308 break;
1309 }
1310 case tgtok::l_square: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001311 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001312 Lex.Lex(); // eat the '['
1313 std::vector<unsigned> Ranges = ParseRangeList();
1314 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001315
Chris Lattnerf4601652007-11-22 20:49:04 +00001316 Result = Result->convertInitListSlice(Ranges);
1317 if (Result == 0) {
1318 Error(SquareLoc, "Invalid range for list slice");
1319 return 0;
1320 }
Bob Wilson21870412009-11-22 04:24:42 +00001321
Chris Lattnerf4601652007-11-22 20:49:04 +00001322 // Eat the ']'.
1323 if (Lex.getCode() != tgtok::r_square) {
1324 TokError("expected ']' at end of list slice");
1325 return 0;
1326 }
1327 Lex.Lex();
1328 break;
1329 }
1330 case tgtok::period:
1331 if (Lex.Lex() != tgtok::Id) { // eat the .
1332 TokError("expected field identifier after '.'");
1333 return 0;
1334 }
1335 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001336 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00001337 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +00001338 return 0;
1339 }
1340 Result = new FieldInit(Result, Lex.getCurStrVal());
1341 Lex.Lex(); // eat field name
1342 break;
1343 }
1344 }
1345}
1346
1347/// ParseDagArgList - Parse the argument list for a dag literal expression.
1348///
1349/// ParseDagArgList ::= Value (':' VARNAME)?
1350/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
Bob Wilson21870412009-11-22 04:24:42 +00001351std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4601652007-11-22 20:49:04 +00001352TGParser::ParseDagArgList(Record *CurRec) {
1353 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson21870412009-11-22 04:24:42 +00001354
Chris Lattnerf4601652007-11-22 20:49:04 +00001355 while (1) {
1356 Init *Val = ParseValue(CurRec);
1357 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
Bob Wilson21870412009-11-22 04:24:42 +00001358
Chris Lattnerf4601652007-11-22 20:49:04 +00001359 // If the variable name is present, add it.
1360 std::string VarName;
1361 if (Lex.getCode() == tgtok::colon) {
1362 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1363 TokError("expected variable name in dag literal");
1364 return std::vector<std::pair<llvm::Init*, std::string> >();
1365 }
1366 VarName = Lex.getCurStrVal();
1367 Lex.Lex(); // eat the VarName.
1368 }
Bob Wilson21870412009-11-22 04:24:42 +00001369
Chris Lattnerf4601652007-11-22 20:49:04 +00001370 Result.push_back(std::make_pair(Val, VarName));
Bob Wilson21870412009-11-22 04:24:42 +00001371
Chris Lattnerf4601652007-11-22 20:49:04 +00001372 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson21870412009-11-22 04:24:42 +00001373 Lex.Lex(); // eat the ','
Chris Lattnerf4601652007-11-22 20:49:04 +00001374 }
Bob Wilson21870412009-11-22 04:24:42 +00001375
Chris Lattnerf4601652007-11-22 20:49:04 +00001376 return Result;
1377}
1378
1379
1380/// ParseValueList - Parse a comma separated list of values, returning them as a
1381/// vector. Note that this always expects to be able to parse at least one
1382/// value. It returns an empty list if this is not possible.
1383///
1384/// ValueList ::= Value (',' Value)
1385///
Bob Wilson21870412009-11-22 04:24:42 +00001386std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
1387 RecTy *EltTy) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001388 std::vector<Init*> Result;
David Greenee1b46912009-06-08 20:23:18 +00001389 RecTy *ItemType = EltTy;
David Greene67acdf22009-06-29 19:59:52 +00001390 unsigned int ArgN = 0;
David Greenee1b46912009-06-08 20:23:18 +00001391 if (ArgsRec != 0 && EltTy == 0) {
1392 const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs();
1393 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1394 assert(RV && "Template argument record not found??");
1395 ItemType = RV->getType();
1396 ++ArgN;
1397 }
1398 Result.push_back(ParseValue(CurRec, ItemType));
Chris Lattnerf4601652007-11-22 20:49:04 +00001399 if (Result.back() == 0) return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001400
Chris Lattnerf4601652007-11-22 20:49:04 +00001401 while (Lex.getCode() == tgtok::comma) {
1402 Lex.Lex(); // Eat the comma
Bob Wilson21870412009-11-22 04:24:42 +00001403
David Greenee1b46912009-06-08 20:23:18 +00001404 if (ArgsRec != 0 && EltTy == 0) {
1405 const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs();
David Greene67acdf22009-06-29 19:59:52 +00001406 if (ArgN >= TArgs.size()) {
1407 TokError("too many template arguments");
1408 return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001409 }
David Greenee1b46912009-06-08 20:23:18 +00001410 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1411 assert(RV && "Template argument record not found??");
1412 ItemType = RV->getType();
1413 ++ArgN;
1414 }
1415 Result.push_back(ParseValue(CurRec, ItemType));
Chris Lattnerf4601652007-11-22 20:49:04 +00001416 if (Result.back() == 0) return std::vector<Init*>();
1417 }
Bob Wilson21870412009-11-22 04:24:42 +00001418
Chris Lattnerf4601652007-11-22 20:49:04 +00001419 return Result;
1420}
1421
1422
Chris Lattnerf4601652007-11-22 20:49:04 +00001423/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1424/// empty string on error. This can happen in a number of different context's,
1425/// including within a def or in the template args for a def (which which case
1426/// CurRec will be non-null) and within the template args for a multiclass (in
1427/// which case CurRec will be null, but CurMultiClass will be set). This can
1428/// also happen within a def that is within a multiclass, which will set both
1429/// CurRec and CurMultiClass.
1430///
1431/// Declaration ::= FIELD? Type ID ('=' Value)?
1432///
Bob Wilson21870412009-11-22 04:24:42 +00001433std::string TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4601652007-11-22 20:49:04 +00001434 bool ParsingTemplateArgs) {
1435 // Read the field prefix if present.
1436 bool HasField = Lex.getCode() == tgtok::Field;
1437 if (HasField) Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001438
Chris Lattnerf4601652007-11-22 20:49:04 +00001439 RecTy *Type = ParseType();
1440 if (Type == 0) return "";
Bob Wilson21870412009-11-22 04:24:42 +00001441
Chris Lattnerf4601652007-11-22 20:49:04 +00001442 if (Lex.getCode() != tgtok::Id) {
1443 TokError("Expected identifier in declaration");
1444 return "";
1445 }
Bob Wilson21870412009-11-22 04:24:42 +00001446
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001447 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001448 std::string DeclName = Lex.getCurStrVal();
1449 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001450
Chris Lattnerf4601652007-11-22 20:49:04 +00001451 if (ParsingTemplateArgs) {
1452 if (CurRec) {
1453 DeclName = CurRec->getName() + ":" + DeclName;
1454 } else {
1455 assert(CurMultiClass);
1456 }
1457 if (CurMultiClass)
1458 DeclName = CurMultiClass->Rec.getName() + "::" + DeclName;
1459 }
Bob Wilson21870412009-11-22 04:24:42 +00001460
Chris Lattnerf4601652007-11-22 20:49:04 +00001461 // Add the value.
1462 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
1463 return "";
Bob Wilson21870412009-11-22 04:24:42 +00001464
Chris Lattnerf4601652007-11-22 20:49:04 +00001465 // If a value is present, parse it.
1466 if (Lex.getCode() == tgtok::equal) {
1467 Lex.Lex();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001468 SMLoc ValLoc = Lex.getLoc();
David Greenee1b46912009-06-08 20:23:18 +00001469 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001470 if (Val == 0 ||
1471 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
1472 return "";
1473 }
Bob Wilson21870412009-11-22 04:24:42 +00001474
Chris Lattnerf4601652007-11-22 20:49:04 +00001475 return DeclName;
1476}
1477
1478/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1479/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1480/// template args for a def, which may or may not be in a multiclass. If null,
1481/// these are the template args for a multiclass.
1482///
1483/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson21870412009-11-22 04:24:42 +00001484///
Chris Lattnerf4601652007-11-22 20:49:04 +00001485bool TGParser::ParseTemplateArgList(Record *CurRec) {
1486 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1487 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +00001488
Chris Lattnerf4601652007-11-22 20:49:04 +00001489 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +00001490
Chris Lattnerf4601652007-11-22 20:49:04 +00001491 // Read the first declaration.
1492 std::string TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1493 if (TemplArg.empty())
1494 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001495
Chris Lattnerf4601652007-11-22 20:49:04 +00001496 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson21870412009-11-22 04:24:42 +00001497
Chris Lattnerf4601652007-11-22 20:49:04 +00001498 while (Lex.getCode() == tgtok::comma) {
1499 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001500
Chris Lattnerf4601652007-11-22 20:49:04 +00001501 // Read the following declarations.
1502 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1503 if (TemplArg.empty())
1504 return true;
1505 TheRecToAddTo->addTemplateArg(TemplArg);
1506 }
Bob Wilson21870412009-11-22 04:24:42 +00001507
Chris Lattnerf4601652007-11-22 20:49:04 +00001508 if (Lex.getCode() != tgtok::greater)
1509 return TokError("expected '>' at end of template argument list");
1510 Lex.Lex(); // eat the '>'.
1511 return false;
1512}
1513
1514
1515/// ParseBodyItem - Parse a single item at within the body of a def or class.
1516///
1517/// BodyItem ::= Declaration ';'
1518/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1519bool TGParser::ParseBodyItem(Record *CurRec) {
1520 if (Lex.getCode() != tgtok::Let) {
Bob Wilson21870412009-11-22 04:24:42 +00001521 if (ParseDeclaration(CurRec, false).empty())
Chris Lattnerf4601652007-11-22 20:49:04 +00001522 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001523
Chris Lattnerf4601652007-11-22 20:49:04 +00001524 if (Lex.getCode() != tgtok::semi)
1525 return TokError("expected ';' after declaration");
1526 Lex.Lex();
1527 return false;
1528 }
1529
1530 // LET ID OptionalRangeList '=' Value ';'
1531 if (Lex.Lex() != tgtok::Id)
1532 return TokError("expected field identifier after let");
Bob Wilson21870412009-11-22 04:24:42 +00001533
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001534 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001535 std::string FieldName = Lex.getCurStrVal();
1536 Lex.Lex(); // eat the field name.
Bob Wilson21870412009-11-22 04:24:42 +00001537
Chris Lattnerf4601652007-11-22 20:49:04 +00001538 std::vector<unsigned> BitList;
Bob Wilson21870412009-11-22 04:24:42 +00001539 if (ParseOptionalBitList(BitList))
Chris Lattnerf4601652007-11-22 20:49:04 +00001540 return true;
1541 std::reverse(BitList.begin(), BitList.end());
Bob Wilson21870412009-11-22 04:24:42 +00001542
Chris Lattnerf4601652007-11-22 20:49:04 +00001543 if (Lex.getCode() != tgtok::equal)
1544 return TokError("expected '=' in let expression");
1545 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001546
David Greenee1b46912009-06-08 20:23:18 +00001547 RecordVal *Field = CurRec->getValue(FieldName);
1548 if (Field == 0)
1549 return TokError("Value '" + FieldName + "' unknown!");
1550
1551 RecTy *Type = Field->getType();
Bob Wilson21870412009-11-22 04:24:42 +00001552
David Greenee1b46912009-06-08 20:23:18 +00001553 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001554 if (Val == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001555
Chris Lattnerf4601652007-11-22 20:49:04 +00001556 if (Lex.getCode() != tgtok::semi)
1557 return TokError("expected ';' after let expression");
1558 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001559
Chris Lattnerf4601652007-11-22 20:49:04 +00001560 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1561}
1562
1563/// ParseBody - Read the body of a class or def. Return true on error, false on
1564/// success.
1565///
1566/// Body ::= ';'
1567/// Body ::= '{' BodyList '}'
1568/// BodyList BodyItem*
1569///
1570bool TGParser::ParseBody(Record *CurRec) {
1571 // If this is a null definition, just eat the semi and return.
1572 if (Lex.getCode() == tgtok::semi) {
1573 Lex.Lex();
1574 return false;
1575 }
Bob Wilson21870412009-11-22 04:24:42 +00001576
Chris Lattnerf4601652007-11-22 20:49:04 +00001577 if (Lex.getCode() != tgtok::l_brace)
1578 return TokError("Expected ';' or '{' to start body");
1579 // Eat the '{'.
1580 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001581
Chris Lattnerf4601652007-11-22 20:49:04 +00001582 while (Lex.getCode() != tgtok::r_brace)
1583 if (ParseBodyItem(CurRec))
1584 return true;
1585
1586 // Eat the '}'.
1587 Lex.Lex();
1588 return false;
1589}
1590
1591/// ParseObjectBody - Parse the body of a def or class. This consists of an
1592/// optional ClassList followed by a Body. CurRec is the current def or class
1593/// that is being parsed.
1594///
1595/// ObjectBody ::= BaseClassList Body
1596/// BaseClassList ::= /*empty*/
1597/// BaseClassList ::= ':' BaseClassListNE
1598/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1599///
1600bool TGParser::ParseObjectBody(Record *CurRec) {
1601 // If there is a baseclass list, read it.
1602 if (Lex.getCode() == tgtok::colon) {
1603 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001604
Chris Lattnerf4601652007-11-22 20:49:04 +00001605 // Read all of the subclasses.
1606 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1607 while (1) {
1608 // Check for error.
1609 if (SubClass.Rec == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001610
Chris Lattnerf4601652007-11-22 20:49:04 +00001611 // Add it.
1612 if (AddSubClass(CurRec, SubClass))
1613 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001614
Chris Lattnerf4601652007-11-22 20:49:04 +00001615 if (Lex.getCode() != tgtok::comma) break;
1616 Lex.Lex(); // eat ','.
1617 SubClass = ParseSubClassReference(CurRec, false);
1618 }
1619 }
1620
1621 // Process any variables on the let stack.
1622 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1623 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1624 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1625 LetStack[i][j].Bits, LetStack[i][j].Value))
1626 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001627
Chris Lattnerf4601652007-11-22 20:49:04 +00001628 return ParseBody(CurRec);
1629}
1630
1631
1632/// ParseDef - Parse and return a top level or multiclass def, return the record
1633/// corresponding to it. This returns null on error.
1634///
1635/// DefInst ::= DEF ObjectName ObjectBody
1636///
1637llvm::Record *TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001638 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001639 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson21870412009-11-22 04:24:42 +00001640 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4601652007-11-22 20:49:04 +00001641
1642 // Parse ObjectName and make a record for it.
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001643 Record *CurRec = new Record(ParseObjectName(), DefLoc);
Bob Wilson21870412009-11-22 04:24:42 +00001644
Chris Lattnerf4601652007-11-22 20:49:04 +00001645 if (!CurMultiClass) {
1646 // Top-level def definition.
Bob Wilson21870412009-11-22 04:24:42 +00001647
Chris Lattnerf4601652007-11-22 20:49:04 +00001648 // Ensure redefinition doesn't happen.
1649 if (Records.getDef(CurRec->getName())) {
1650 Error(DefLoc, "def '" + CurRec->getName() + "' already defined");
1651 return 0;
1652 }
1653 Records.addDef(CurRec);
1654 } else {
1655 // Otherwise, a def inside a multiclass, add it to the multiclass.
1656 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
1657 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
1658 Error(DefLoc, "def '" + CurRec->getName() +
1659 "' already defined in this multiclass!");
1660 return 0;
1661 }
1662 CurMultiClass->DefPrototypes.push_back(CurRec);
1663 }
Bob Wilson21870412009-11-22 04:24:42 +00001664
Chris Lattnerf4601652007-11-22 20:49:04 +00001665 if (ParseObjectBody(CurRec))
1666 return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001667
Chris Lattnerf4601652007-11-22 20:49:04 +00001668 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
1669 CurRec->resolveReferences();
Bob Wilson21870412009-11-22 04:24:42 +00001670
Chris Lattnerf4601652007-11-22 20:49:04 +00001671 // If ObjectBody has template arguments, it's an error.
1672 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
1673 return CurRec;
1674}
1675
1676
1677/// ParseClass - Parse a tblgen class definition.
1678///
1679/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
1680///
1681bool TGParser::ParseClass() {
1682 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
1683 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001684
Chris Lattnerf4601652007-11-22 20:49:04 +00001685 if (Lex.getCode() != tgtok::Id)
1686 return TokError("expected class name after 'class' keyword");
Bob Wilson21870412009-11-22 04:24:42 +00001687
Chris Lattnerf4601652007-11-22 20:49:04 +00001688 Record *CurRec = Records.getClass(Lex.getCurStrVal());
1689 if (CurRec) {
1690 // If the body was previously defined, this is an error.
1691 if (!CurRec->getValues().empty() ||
1692 !CurRec->getSuperClasses().empty() ||
1693 !CurRec->getTemplateArgs().empty())
1694 return TokError("Class '" + CurRec->getName() + "' already defined");
1695 } else {
1696 // If this is the first reference to this class, create and add it.
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001697 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc());
Chris Lattnerf4601652007-11-22 20:49:04 +00001698 Records.addClass(CurRec);
1699 }
1700 Lex.Lex(); // eat the name.
Bob Wilson21870412009-11-22 04:24:42 +00001701
Chris Lattnerf4601652007-11-22 20:49:04 +00001702 // If there are template args, parse them.
1703 if (Lex.getCode() == tgtok::less)
1704 if (ParseTemplateArgList(CurRec))
1705 return true;
1706
1707 // Finally, parse the object body.
1708 return ParseObjectBody(CurRec);
1709}
1710
1711/// ParseLetList - Parse a non-empty list of assignment expressions into a list
1712/// of LetRecords.
1713///
1714/// LetList ::= LetItem (',' LetItem)*
1715/// LetItem ::= ID OptionalRangeList '=' Value
1716///
1717std::vector<LetRecord> TGParser::ParseLetList() {
1718 std::vector<LetRecord> Result;
Bob Wilson21870412009-11-22 04:24:42 +00001719
Chris Lattnerf4601652007-11-22 20:49:04 +00001720 while (1) {
1721 if (Lex.getCode() != tgtok::Id) {
1722 TokError("expected identifier in let definition");
1723 return std::vector<LetRecord>();
1724 }
1725 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001726 SMLoc NameLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +00001727 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4601652007-11-22 20:49:04 +00001728
1729 // Check for an optional RangeList.
1730 std::vector<unsigned> Bits;
Bob Wilson21870412009-11-22 04:24:42 +00001731 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4601652007-11-22 20:49:04 +00001732 return std::vector<LetRecord>();
1733 std::reverse(Bits.begin(), Bits.end());
Bob Wilson21870412009-11-22 04:24:42 +00001734
Chris Lattnerf4601652007-11-22 20:49:04 +00001735 if (Lex.getCode() != tgtok::equal) {
1736 TokError("expected '=' in let expression");
1737 return std::vector<LetRecord>();
1738 }
1739 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001740
Chris Lattnerf4601652007-11-22 20:49:04 +00001741 Init *Val = ParseValue(0);
1742 if (Val == 0) return std::vector<LetRecord>();
Bob Wilson21870412009-11-22 04:24:42 +00001743
Chris Lattnerf4601652007-11-22 20:49:04 +00001744 // Now that we have everything, add the record.
1745 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson21870412009-11-22 04:24:42 +00001746
Chris Lattnerf4601652007-11-22 20:49:04 +00001747 if (Lex.getCode() != tgtok::comma)
1748 return Result;
Bob Wilson21870412009-11-22 04:24:42 +00001749 Lex.Lex(); // eat the comma.
Chris Lattnerf4601652007-11-22 20:49:04 +00001750 }
1751}
1752
1753/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
1754/// different related productions.
1755///
1756/// Object ::= LET LetList IN '{' ObjectList '}'
1757/// Object ::= LET LetList IN Object
1758///
1759bool TGParser::ParseTopLevelLet() {
1760 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
1761 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001762
Chris Lattnerf4601652007-11-22 20:49:04 +00001763 // Add this entry to the let stack.
1764 std::vector<LetRecord> LetInfo = ParseLetList();
1765 if (LetInfo.empty()) return true;
1766 LetStack.push_back(LetInfo);
1767
1768 if (Lex.getCode() != tgtok::In)
1769 return TokError("expected 'in' at end of top-level 'let'");
1770 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001771
Chris Lattnerf4601652007-11-22 20:49:04 +00001772 // If this is a scalar let, just handle it now
1773 if (Lex.getCode() != tgtok::l_brace) {
1774 // LET LetList IN Object
1775 if (ParseObject())
1776 return true;
1777 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001778 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001779 // Otherwise, this is a group let.
1780 Lex.Lex(); // eat the '{'.
Bob Wilson21870412009-11-22 04:24:42 +00001781
Chris Lattnerf4601652007-11-22 20:49:04 +00001782 // Parse the object list.
1783 if (ParseObjectList())
1784 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001785
Chris Lattnerf4601652007-11-22 20:49:04 +00001786 if (Lex.getCode() != tgtok::r_brace) {
1787 TokError("expected '}' at end of top level let command");
1788 return Error(BraceLoc, "to match this '{'");
1789 }
1790 Lex.Lex();
1791 }
Bob Wilson21870412009-11-22 04:24:42 +00001792
Chris Lattnerf4601652007-11-22 20:49:04 +00001793 // Outside this let scope, this let block is not active.
1794 LetStack.pop_back();
1795 return false;
1796}
1797
1798/// ParseMultiClassDef - Parse a def in a multiclass context.
1799///
1800/// MultiClassDef ::= DefInst
1801///
1802bool TGParser::ParseMultiClassDef(MultiClass *CurMC) {
Bob Wilson21870412009-11-22 04:24:42 +00001803 if (Lex.getCode() != tgtok::Def)
Chris Lattnerf4601652007-11-22 20:49:04 +00001804 return TokError("expected 'def' in multiclass body");
1805
1806 Record *D = ParseDef(CurMC);
1807 if (D == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001808
Chris Lattnerf4601652007-11-22 20:49:04 +00001809 // Copy the template arguments for the multiclass into the def.
1810 const std::vector<std::string> &TArgs = CurMC->Rec.getTemplateArgs();
Bob Wilson21870412009-11-22 04:24:42 +00001811
Chris Lattnerf4601652007-11-22 20:49:04 +00001812 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1813 const RecordVal *RV = CurMC->Rec.getValue(TArgs[i]);
1814 assert(RV && "Template arg doesn't exist?");
1815 D->addValue(*RV);
1816 }
1817
1818 return false;
1819}
1820
1821/// ParseMultiClass - Parse a multiclass definition.
1822///
Bob Wilson32558652009-04-28 19:41:44 +00001823/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
1824/// ':' BaseMultiClassList '{' MultiClassDef+ '}'
Chris Lattnerf4601652007-11-22 20:49:04 +00001825///
1826bool TGParser::ParseMultiClass() {
1827 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
1828 Lex.Lex(); // Eat the multiclass token.
1829
1830 if (Lex.getCode() != tgtok::Id)
1831 return TokError("expected identifier after multiclass for name");
1832 std::string Name = Lex.getCurStrVal();
Bob Wilson21870412009-11-22 04:24:42 +00001833
Chris Lattnerf4601652007-11-22 20:49:04 +00001834 if (MultiClasses.count(Name))
1835 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson21870412009-11-22 04:24:42 +00001836
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001837 CurMultiClass = MultiClasses[Name] = new MultiClass(Name, Lex.getLoc());
Chris Lattnerf4601652007-11-22 20:49:04 +00001838 Lex.Lex(); // Eat the identifier.
Bob Wilson21870412009-11-22 04:24:42 +00001839
Chris Lattnerf4601652007-11-22 20:49:04 +00001840 // If there are template args, parse them.
1841 if (Lex.getCode() == tgtok::less)
1842 if (ParseTemplateArgList(0))
1843 return true;
1844
David Greened34a73b2009-04-24 16:55:41 +00001845 bool inherits = false;
1846
David Greenede444af2009-04-22 16:42:54 +00001847 // If there are submulticlasses, parse them.
1848 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00001849 inherits = true;
1850
David Greenede444af2009-04-22 16:42:54 +00001851 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00001852
David Greenede444af2009-04-22 16:42:54 +00001853 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00001854 SubMultiClassReference SubMultiClass =
1855 ParseSubMultiClassReference(CurMultiClass);
David Greenede444af2009-04-22 16:42:54 +00001856 while (1) {
1857 // Check for error.
1858 if (SubMultiClass.MC == 0) return true;
Bob Wilson32558652009-04-28 19:41:44 +00001859
David Greenede444af2009-04-22 16:42:54 +00001860 // Add it.
1861 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
1862 return true;
Bob Wilson32558652009-04-28 19:41:44 +00001863
David Greenede444af2009-04-22 16:42:54 +00001864 if (Lex.getCode() != tgtok::comma) break;
1865 Lex.Lex(); // eat ','.
1866 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
1867 }
1868 }
1869
David Greened34a73b2009-04-24 16:55:41 +00001870 if (Lex.getCode() != tgtok::l_brace) {
1871 if (!inherits)
1872 return TokError("expected '{' in multiclass definition");
Bob Wilson21870412009-11-22 04:24:42 +00001873 else if (Lex.getCode() != tgtok::semi)
1874 return TokError("expected ';' in multiclass definition");
David Greened34a73b2009-04-24 16:55:41 +00001875 else
Bob Wilson21870412009-11-22 04:24:42 +00001876 Lex.Lex(); // eat the ';'.
1877 } else {
David Greened34a73b2009-04-24 16:55:41 +00001878 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
1879 return TokError("multiclass must contain at least one def");
Bob Wilson21870412009-11-22 04:24:42 +00001880
David Greened34a73b2009-04-24 16:55:41 +00001881 while (Lex.getCode() != tgtok::r_brace)
1882 if (ParseMultiClassDef(CurMultiClass))
1883 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001884
David Greened34a73b2009-04-24 16:55:41 +00001885 Lex.Lex(); // eat the '}'.
1886 }
Bob Wilson21870412009-11-22 04:24:42 +00001887
Chris Lattnerf4601652007-11-22 20:49:04 +00001888 CurMultiClass = 0;
1889 return false;
1890}
1891
1892/// ParseDefm - Parse the instantiation of a multiclass.
1893///
1894/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
1895///
1896bool TGParser::ParseDefm() {
1897 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
1898 if (Lex.Lex() != tgtok::Id) // eat the defm.
1899 return TokError("expected identifier after defm");
Bob Wilson21870412009-11-22 04:24:42 +00001900
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001901 SMLoc DefmPrefixLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001902 std::string DefmPrefix = Lex.getCurStrVal();
1903 if (Lex.Lex() != tgtok::colon)
1904 return TokError("expected ':' after defm identifier");
Bob Wilson21870412009-11-22 04:24:42 +00001905
Chris Lattnerf4601652007-11-22 20:49:04 +00001906 // eat the colon.
1907 Lex.Lex();
1908
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001909 SMLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001910 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene56546132009-04-22 22:17:51 +00001911
1912 while (1) {
1913 if (Ref.Rec == 0) return true;
1914
1915 // To instantiate a multiclass, we need to first get the multiclass, then
1916 // instantiate each def contained in the multiclass with the SubClassRef
1917 // template parameters.
1918 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
1919 assert(MC && "Didn't lookup multiclass correctly?");
Bob Wilson21870412009-11-22 04:24:42 +00001920 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greene56546132009-04-22 22:17:51 +00001921
1922 // Verify that the correct number of template arguments were specified.
1923 const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs();
1924 if (TArgs.size() < TemplateVals.size())
1925 return Error(SubClassLoc,
1926 "more template args specified than multiclass expects");
1927
1928 // Loop over all the def's in the multiclass, instantiating each one.
1929 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
1930 Record *DefProto = MC->DefPrototypes[i];
1931
David Greene065f2592009-05-05 16:28:25 +00001932 // Add in the defm name
1933 std::string DefName = DefProto->getName();
1934 std::string::size_type idx = DefName.find("#NAME#");
1935 if (idx != std::string::npos) {
1936 DefName.replace(idx, 6, DefmPrefix);
Bob Wilson21870412009-11-22 04:24:42 +00001937 } else {
David Greene065f2592009-05-05 16:28:25 +00001938 // Add the suffix to the defm name to get the new name.
1939 DefName = DefmPrefix + DefName;
1940 }
1941
1942 Record *CurRec = new Record(DefName, DefmPrefixLoc);
David Greene56546132009-04-22 22:17:51 +00001943
1944 SubClassReference Ref;
1945 Ref.RefLoc = DefmPrefixLoc;
1946 Ref.Rec = DefProto;
1947 AddSubClass(CurRec, Ref);
1948
1949 // Loop over all of the template arguments, setting them to the specified
1950 // value or leaving them as the default if necessary.
1951 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
Bob Wilson32558652009-04-28 19:41:44 +00001952 // Check if a value is specified for this temp-arg.
1953 if (i < TemplateVals.size()) {
David Greene56546132009-04-22 22:17:51 +00001954 // Set it now.
1955 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
1956 TemplateVals[i]))
1957 return true;
1958
1959 // Resolve it next.
1960 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
1961
1962 // Now remove it.
1963 CurRec->removeValue(TArgs[i]);
1964
1965 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Bob Wilson32558652009-04-28 19:41:44 +00001966 return Error(SubClassLoc,
1967 "value not specified for template argument #"+
David Greene56546132009-04-22 22:17:51 +00001968 utostr(i) + " (" + TArgs[i] + ") of multiclassclass '" +
1969 MC->Rec.getName() + "'");
1970 }
1971 }
1972
1973 // If the mdef is inside a 'let' expression, add to each def.
1974 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1975 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1976 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1977 LetStack[i][j].Bits, LetStack[i][j].Value)) {
1978 Error(DefmPrefixLoc, "when instantiating this defm");
1979 return true;
1980 }
1981
1982 // Ensure redefinition doesn't happen.
1983 if (Records.getDef(CurRec->getName()))
Bob Wilson21870412009-11-22 04:24:42 +00001984 return Error(DefmPrefixLoc, "def '" + CurRec->getName() +
1985 "' already defined, instantiating defm with subdef '" +
David Greene56546132009-04-22 22:17:51 +00001986 DefProto->getName() + "'");
1987 Records.addDef(CurRec);
1988 CurRec->resolveReferences();
1989 }
1990
1991 if (Lex.getCode() != tgtok::comma) break;
1992 Lex.Lex(); // eat ','.
1993
1994 SubClassLoc = Lex.getLoc();
1995 Ref = ParseSubClassReference(0, true);
1996 }
1997
Chris Lattnerf4601652007-11-22 20:49:04 +00001998 if (Lex.getCode() != tgtok::semi)
1999 return TokError("expected ';' at end of defm");
2000 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002001
Chris Lattnerf4601652007-11-22 20:49:04 +00002002 return false;
2003}
2004
2005/// ParseObject
2006/// Object ::= ClassInst
2007/// Object ::= DefInst
2008/// Object ::= MultiClassInst
2009/// Object ::= DefMInst
2010/// Object ::= LETCommand '{' ObjectList '}'
2011/// Object ::= LETCommand Object
2012bool TGParser::ParseObject() {
2013 switch (Lex.getCode()) {
2014 default: assert(0 && "This is not an object");
2015 case tgtok::Let: return ParseTopLevelLet();
2016 case tgtok::Def: return ParseDef(0) == 0;
2017 case tgtok::Defm: return ParseDefm();
2018 case tgtok::Class: return ParseClass();
2019 case tgtok::MultiClass: return ParseMultiClass();
2020 }
2021}
2022
2023/// ParseObjectList
2024/// ObjectList :== Object*
2025bool TGParser::ParseObjectList() {
2026 while (isObjectStart(Lex.getCode())) {
2027 if (ParseObject())
2028 return true;
2029 }
2030 return false;
2031}
2032
Chris Lattnerf4601652007-11-22 20:49:04 +00002033bool TGParser::ParseFile() {
2034 Lex.Lex(); // Prime the lexer.
2035 if (ParseObjectList()) return true;
Bob Wilson21870412009-11-22 04:24:42 +00002036
Chris Lattnerf4601652007-11-22 20:49:04 +00002037 // If we have unread input at the end of the file, report it.
2038 if (Lex.getCode() == tgtok::Eof)
2039 return false;
Bob Wilson21870412009-11-22 04:24:42 +00002040
Chris Lattnerf4601652007-11-22 20:49:04 +00002041 return TokError("Unexpected input at top level");
2042}
2043