blob: 6b4c431635cbfc9c7ac8439ba7eb5bcfbcc1f05b [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
Chuck Rose IIIaa917922007-11-26 23:19:59 +000014#include <algorithm>
15
Chris Lattnerf4601652007-11-22 20:49:04 +000016#include "TGParser.h"
17#include "Record.h"
18#include "llvm/ADT/StringExtras.h"
David Greened34a73b2009-04-24 16:55:41 +000019#include "llvm/Support/Streams.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 Lattner1c8ae592009-03-13 16:01:53 +000028 TGLoc 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 {
37 TGLoc RefLoc;
38 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 {
47 cerr << "Multiclass:\n";
48
49 MC->dump();
50
51 cerr << "Template args:\n";
52 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 Lattner1c8ae592009-03-13 16:01:53 +000062bool TGParser::AddValue(Record *CurRec, TGLoc Loc, const RecordVal &RV) {
Chris Lattnerf4601652007-11-22 20:49:04 +000063 if (CurRec == 0)
64 CurRec = &CurMultiClass->Rec;
65
66 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 " +
71 "previous definition of type '" +
72 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.
Chris Lattner1c8ae592009-03-13 16:01:53 +000081bool TGParser::SetValue(Record *CurRec, TGLoc 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;
97
98 // 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 }
113
114 // 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))
137 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");
158
159 // 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.
164 if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(),
165 SubClass.TemplateArgs[i]))
166 return true;
167
168 // Resolve it next.
169 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
170
171 // 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 #"
176 + utostr(i) + " (" + TArgs[i] + ") of subclass '" +
177 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 }
190
191 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 ||
295 K == tgtok::Defm || K == tgtok::Let || K == tgtok::MultiClass;
296}
297
298/// ParseObjectName - If an object name is specified, return it. Otherwise,
299/// return an anonymous name.
300/// ObjectName ::= ID
301/// ObjectName ::= /*empty*/
302///
303std::string TGParser::ParseObjectName() {
304 if (Lex.getCode() == tgtok::Id) {
305 std::string Ret = Lex.getCurStrVal();
306 Lex.Lex();
307 return Ret;
308 }
309
310 static unsigned AnonCounter = 0;
311 return "anonymous."+utostr(AnonCounter++);
312}
313
314
315/// ParseClassID - Parse and resolve a reference to a class name. This returns
316/// null on error.
317///
318/// ClassID ::= ID
319///
320Record *TGParser::ParseClassID() {
321 if (Lex.getCode() != tgtok::Id) {
322 TokError("expected name for ClassID");
323 return 0;
324 }
325
326 Record *Result = Records.getClass(Lex.getCurStrVal());
327 if (Result == 0)
328 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
329
330 Lex.Lex();
331 return Result;
332}
333
Bob Wilson32558652009-04-28 19:41:44 +0000334/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
335/// This returns null on error.
David Greenede444af2009-04-22 16:42:54 +0000336///
337/// MultiClassID ::= ID
338///
339MultiClass *TGParser::ParseMultiClassID() {
340 if (Lex.getCode() != tgtok::Id) {
341 TokError("expected name for ClassID");
342 return 0;
343 }
Bob Wilson32558652009-04-28 19:41:44 +0000344
David Greenede444af2009-04-22 16:42:54 +0000345 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
346 if (Result == 0)
347 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson32558652009-04-28 19:41:44 +0000348
David Greenede444af2009-04-22 16:42:54 +0000349 Lex.Lex();
350 return Result;
351}
352
Chris Lattnerf4601652007-11-22 20:49:04 +0000353Record *TGParser::ParseDefmID() {
354 if (Lex.getCode() != tgtok::Id) {
355 TokError("expected multiclass name");
356 return 0;
357 }
358
359 MultiClass *MC = MultiClasses[Lex.getCurStrVal()];
360 if (MC == 0) {
361 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
362 return 0;
363 }
364
365 Lex.Lex();
366 return &MC->Rec;
367}
368
369
370
371/// ParseSubClassReference - Parse a reference to a subclass or to a templated
372/// subclass. This returns a SubClassRefTy with a null Record* on error.
373///
374/// SubClassRef ::= ClassID
375/// SubClassRef ::= ClassID '<' ValueList '>'
376///
377SubClassReference TGParser::
378ParseSubClassReference(Record *CurRec, bool isDefm) {
379 SubClassReference Result;
380 Result.RefLoc = Lex.getLoc();
381
382 if (isDefm)
383 Result.Rec = ParseDefmID();
384 else
385 Result.Rec = ParseClassID();
386 if (Result.Rec == 0) return Result;
387
388 // If there is no template arg list, we're done.
389 if (Lex.getCode() != tgtok::less)
390 return Result;
391 Lex.Lex(); // Eat the '<'
392
393 if (Lex.getCode() == tgtok::greater) {
394 TokError("subclass reference requires a non-empty list of template values");
395 Result.Rec = 0;
396 return Result;
397 }
398
399 Result.TemplateArgs = ParseValueList(CurRec);
400 if (Result.TemplateArgs.empty()) {
401 Result.Rec = 0; // Error parsing value list.
402 return Result;
403 }
404
405 if (Lex.getCode() != tgtok::greater) {
406 TokError("expected '>' in template value list");
407 Result.Rec = 0;
408 return Result;
409 }
410 Lex.Lex();
411
412 return Result;
413}
414
Bob Wilson32558652009-04-28 19:41:44 +0000415/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
416/// templated submulticlass. This returns a SubMultiClassRefTy with a null
417/// Record* on error.
David Greenede444af2009-04-22 16:42:54 +0000418///
419/// SubMultiClassRef ::= MultiClassID
420/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
421///
422SubMultiClassReference TGParser::
423ParseSubMultiClassReference(MultiClass *CurMC) {
424 SubMultiClassReference Result;
425 Result.RefLoc = Lex.getLoc();
Bob Wilson32558652009-04-28 19:41:44 +0000426
David Greenede444af2009-04-22 16:42:54 +0000427 Result.MC = ParseMultiClassID();
428 if (Result.MC == 0) return Result;
Bob Wilson32558652009-04-28 19:41:44 +0000429
David Greenede444af2009-04-22 16:42:54 +0000430 // If there is no template arg list, we're done.
431 if (Lex.getCode() != tgtok::less)
432 return Result;
433 Lex.Lex(); // Eat the '<'
Bob Wilson32558652009-04-28 19:41:44 +0000434
David Greenede444af2009-04-22 16:42:54 +0000435 if (Lex.getCode() == tgtok::greater) {
436 TokError("subclass reference requires a non-empty list of template values");
437 Result.MC = 0;
438 return Result;
439 }
Bob Wilson32558652009-04-28 19:41:44 +0000440
David Greenede444af2009-04-22 16:42:54 +0000441 Result.TemplateArgs = ParseValueList(&CurMC->Rec);
442 if (Result.TemplateArgs.empty()) {
443 Result.MC = 0; // Error parsing value list.
444 return Result;
445 }
Bob Wilson32558652009-04-28 19:41:44 +0000446
David Greenede444af2009-04-22 16:42:54 +0000447 if (Lex.getCode() != tgtok::greater) {
448 TokError("expected '>' in template value list");
449 Result.MC = 0;
450 return Result;
451 }
452 Lex.Lex();
453
454 return Result;
455}
456
Chris Lattnerf4601652007-11-22 20:49:04 +0000457/// ParseRangePiece - Parse a bit/value range.
458/// RangePiece ::= INTVAL
459/// RangePiece ::= INTVAL '-' INTVAL
460/// RangePiece ::= INTVAL INTVAL
461bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner811281e2008-01-10 07:01:53 +0000462 if (Lex.getCode() != tgtok::IntVal) {
463 TokError("expected integer or bitrange");
464 return true;
465 }
Dan Gohman63f97202008-10-17 01:33:43 +0000466 int64_t Start = Lex.getCurIntVal();
467 int64_t End;
Chris Lattnerf4601652007-11-22 20:49:04 +0000468
469 if (Start < 0)
470 return TokError("invalid range, cannot be negative");
471
472 switch (Lex.Lex()) { // eat first character.
473 default:
474 Ranges.push_back(Start);
475 return false;
476 case tgtok::minus:
477 if (Lex.Lex() != tgtok::IntVal) {
478 TokError("expected integer value as end of range");
479 return true;
480 }
481 End = Lex.getCurIntVal();
482 break;
483 case tgtok::IntVal:
484 End = -Lex.getCurIntVal();
485 break;
486 }
487 if (End < 0)
488 return TokError("invalid range, cannot be negative");
489 Lex.Lex();
490
491 // Add to the range.
492 if (Start < End) {
493 for (; Start <= End; ++Start)
494 Ranges.push_back(Start);
495 } else {
496 for (; Start >= End; --Start)
497 Ranges.push_back(Start);
498 }
499 return false;
500}
501
502/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
503///
504/// RangeList ::= RangePiece (',' RangePiece)*
505///
506std::vector<unsigned> TGParser::ParseRangeList() {
507 std::vector<unsigned> Result;
508
509 // Parse the first piece.
510 if (ParseRangePiece(Result))
511 return std::vector<unsigned>();
512 while (Lex.getCode() == tgtok::comma) {
513 Lex.Lex(); // Eat the comma.
514
515 // Parse the next range piece.
516 if (ParseRangePiece(Result))
517 return std::vector<unsigned>();
518 }
519 return Result;
520}
521
522/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
523/// OptionalRangeList ::= '<' RangeList '>'
524/// OptionalRangeList ::= /*empty*/
525bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
526 if (Lex.getCode() != tgtok::less)
527 return false;
528
Chris Lattner1c8ae592009-03-13 16:01:53 +0000529 TGLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000530 Lex.Lex(); // eat the '<'
531
532 // Parse the range list.
533 Ranges = ParseRangeList();
534 if (Ranges.empty()) return true;
535
536 if (Lex.getCode() != tgtok::greater) {
537 TokError("expected '>' at end of range list");
538 return Error(StartLoc, "to match this '<'");
539 }
540 Lex.Lex(); // eat the '>'.
541 return false;
542}
543
544/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
545/// OptionalBitList ::= '{' RangeList '}'
546/// OptionalBitList ::= /*empty*/
547bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
548 if (Lex.getCode() != tgtok::l_brace)
549 return false;
550
Chris Lattner1c8ae592009-03-13 16:01:53 +0000551 TGLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000552 Lex.Lex(); // eat the '{'
553
554 // Parse the range list.
555 Ranges = ParseRangeList();
556 if (Ranges.empty()) return true;
557
558 if (Lex.getCode() != tgtok::r_brace) {
559 TokError("expected '}' at end of bit list");
560 return Error(StartLoc, "to match this '{'");
561 }
562 Lex.Lex(); // eat the '}'.
563 return false;
564}
565
566
567/// ParseType - Parse and return a tblgen type. This returns null on error.
568///
569/// Type ::= STRING // string type
570/// Type ::= BIT // bit type
571/// Type ::= BITS '<' INTVAL '>' // bits<x> type
572/// Type ::= INT // int type
573/// Type ::= LIST '<' Type '>' // list<x> type
574/// Type ::= CODE // code type
575/// Type ::= DAG // dag type
576/// Type ::= ClassID // Record Type
577///
578RecTy *TGParser::ParseType() {
579 switch (Lex.getCode()) {
580 default: TokError("Unknown token when expecting a type"); return 0;
581 case tgtok::String: Lex.Lex(); return new StringRecTy();
582 case tgtok::Bit: Lex.Lex(); return new BitRecTy();
583 case tgtok::Int: Lex.Lex(); return new IntRecTy();
584 case tgtok::Code: Lex.Lex(); return new CodeRecTy();
585 case tgtok::Dag: Lex.Lex(); return new DagRecTy();
586 case tgtok::Id:
587 if (Record *R = ParseClassID()) return new RecordRecTy(R);
588 return 0;
589 case tgtok::Bits: {
590 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
591 TokError("expected '<' after bits type");
592 return 0;
593 }
594 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
595 TokError("expected integer in bits<n> type");
596 return 0;
597 }
Dan Gohman63f97202008-10-17 01:33:43 +0000598 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4601652007-11-22 20:49:04 +0000599 if (Lex.Lex() != tgtok::greater) { // Eat count.
600 TokError("expected '>' at end of bits<n> type");
601 return 0;
602 }
603 Lex.Lex(); // Eat '>'
604 return new BitsRecTy(Val);
605 }
606 case tgtok::List: {
607 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
608 TokError("expected '<' after list type");
609 return 0;
610 }
611 Lex.Lex(); // Eat '<'
612 RecTy *SubType = ParseType();
613 if (SubType == 0) return 0;
614
615 if (Lex.getCode() != tgtok::greater) {
616 TokError("expected '>' at end of list<ty> type");
617 return 0;
618 }
619 Lex.Lex(); // Eat '>'
620 return new ListRecTy(SubType);
621 }
622 }
623}
624
625/// ParseIDValue - Parse an ID as a value and decode what it means.
626///
627/// IDValue ::= ID [def local value]
628/// IDValue ::= ID [def template arg]
629/// IDValue ::= ID [multiclass local value]
630/// IDValue ::= ID [multiclass template argument]
631/// IDValue ::= ID [def name]
632///
633Init *TGParser::ParseIDValue(Record *CurRec) {
634 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
635 std::string Name = Lex.getCurStrVal();
Chris Lattner1c8ae592009-03-13 16:01:53 +0000636 TGLoc Loc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000637 Lex.Lex();
638 return ParseIDValue(CurRec, Name, Loc);
639}
640
641/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
642/// has already been read.
643Init *TGParser::ParseIDValue(Record *CurRec,
Chris Lattner1c8ae592009-03-13 16:01:53 +0000644 const std::string &Name, TGLoc NameLoc) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000645 if (CurRec) {
646 if (const RecordVal *RV = CurRec->getValue(Name))
647 return new VarInit(Name, RV->getType());
648
649 std::string TemplateArgName = CurRec->getName()+":"+Name;
650 if (CurRec->isTemplateArg(TemplateArgName)) {
651 const RecordVal *RV = CurRec->getValue(TemplateArgName);
652 assert(RV && "Template arg doesn't exist??");
653 return new VarInit(TemplateArgName, RV->getType());
654 }
655 }
656
657 if (CurMultiClass) {
658 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
659 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
660 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
661 assert(RV && "Template arg doesn't exist??");
662 return new VarInit(MCName, RV->getType());
663 }
664 }
665
666 if (Record *D = Records.getDef(Name))
667 return new DefInit(D);
668
669 Error(NameLoc, "Variable not defined: '" + Name + "'");
670 return 0;
671}
672
David Greened418c1b2009-05-14 20:54:48 +0000673/// ParseOperation - Parse an operator. This returns null on error.
674///
675/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
676///
677Init *TGParser::ParseOperation(Record *CurRec) {
678 switch (Lex.getCode()) {
679 default:
680 TokError("unknown operation");
681 return 0;
682 break;
David Greenee6c27de2009-05-14 21:22:49 +0000683 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
684 UnOpInit::UnaryOp Code;
685 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000686
David Greenee6c27de2009-05-14 21:22:49 +0000687 switch (Lex.getCode()) {
688 default: assert(0 && "Unhandled code!");
689 case tgtok::XCast:
690 Lex.Lex(); // eat the operation
691 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000692
David Greenee6c27de2009-05-14 21:22:49 +0000693 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000694
David Greenee6c27de2009-05-14 21:22:49 +0000695 if (Type == 0) {
696 TokError("did not get type for binary operator");
697 return 0;
698 }
David Greened418c1b2009-05-14 20:54:48 +0000699
David Greenee6c27de2009-05-14 21:22:49 +0000700 break;
701 }
702 if (Lex.getCode() != tgtok::l_paren) {
703 TokError("expected '(' after unary operator");
704 return 0;
705 }
706 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000707
David Greenee6c27de2009-05-14 21:22:49 +0000708 Init *LHS = ParseValue(CurRec);
709 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000710
David Greenee6c27de2009-05-14 21:22:49 +0000711 if (Lex.getCode() != tgtok::r_paren) {
712 TokError("expected ')' in unary operator");
713 return 0;
714 }
715 Lex.Lex(); // eat the ')'
716 return (new UnOpInit(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
717 }
David Greened418c1b2009-05-14 20:54:48 +0000718
719 case tgtok::XConcat:
720 case tgtok::XSRA:
721 case tgtok::XSRL:
722 case tgtok::XSHL:
723 case tgtok::XStrConcat:
724 case tgtok::XNameConcat: { // Value ::= !binop '(' Value ',' Value ')'
725 BinOpInit::BinaryOp Code;
726 RecTy *Type = 0;
727
728
729 switch (Lex.getCode()) {
730 default: assert(0 && "Unhandled code!");
731 case tgtok::XConcat:
732 Lex.Lex(); // eat the operation
733 Code = BinOpInit::CONCAT;
734 Type = new DagRecTy();
735 break;
736 case tgtok::XSRA:
737 Lex.Lex(); // eat the operation
738 Code = BinOpInit::SRA;
739 Type = new IntRecTy();
740 break;
741 case tgtok::XSRL:
742 Lex.Lex(); // eat the operation
743 Code = BinOpInit::SRL;
744 Type = new IntRecTy();
745 break;
746 case tgtok::XSHL:
747 Lex.Lex(); // eat the operation
748 Code = BinOpInit::SHL;
749 Type = new IntRecTy();
750 break;
751 case tgtok::XStrConcat:
752 Lex.Lex(); // eat the operation
753 Code = BinOpInit::STRCONCAT;
754 Type = new StringRecTy();
755 break;
756 case tgtok::XNameConcat:
757 Lex.Lex(); // eat the operation
758 Code = BinOpInit::NAMECONCAT;
759
760 Type = ParseOperatorType();
761
762 if (Type == 0) {
763 TokError("did not get type for binary operator");
764 return 0;
765 }
766
767 break;
768 }
769 if (Lex.getCode() != tgtok::l_paren) {
770 TokError("expected '(' after binary operator");
771 return 0;
772 }
773 Lex.Lex(); // eat the '('
774
775 Init *LHS = ParseValue(CurRec);
776 if (LHS == 0) return 0;
777
778 if (Lex.getCode() != tgtok::comma) {
779 TokError("expected ',' in binary operator");
780 return 0;
781 }
782 Lex.Lex(); // eat the ','
783
784 Init *RHS = ParseValue(CurRec);
785 if (RHS == 0) return 0;
786
787 if (Lex.getCode() != tgtok::r_paren) {
788 TokError("expected ')' in binary operator");
789 return 0;
790 }
791 Lex.Lex(); // eat the ')'
792 return (new BinOpInit(Code, LHS, RHS, Type))->Fold(CurRec, CurMultiClass);
793 }
794
795// case tgtok::XForEach:
796// case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
797// TernOpInit::TernaryOp Code;
798// RecTy *Type = 0;
799
800
801// tgtok::TokKind LexCode = Lex.getCode();
802// Lex.Lex(); // eat the operation
803// switch (LexCode) {
804// default: assert(0 && "Unhandled code!");
805// case tgtok::XForEach:
806// Code = TernOpInit::FOREACH;
807// break;
808// case tgtok::XSubst:
809// Code = TernOpInit::SUBST;
810// break;
811// }
812// if (Lex.getCode() != tgtok::l_paren) {
813// TokError("expected '(' after ternary operator");
814// return 0;
815// }
816// Lex.Lex(); // eat the '('
817
818// Init *LHS = ParseValue(CurRec);
819// if (LHS == 0) return 0;
820
821// if (Lex.getCode() != tgtok::comma) {
822// TokError("expected ',' in ternary operator");
823// return 0;
824// }
825// Lex.Lex(); // eat the ','
826
827// Init *MHS = ParseValue(CurRec);
828// if (MHS == 0) return 0;
829
830// if (Lex.getCode() != tgtok::comma) {
831// TokError("expected ',' in ternary operator");
832// return 0;
833// }
834// Lex.Lex(); // eat the ','
835
836// Init *RHS = ParseValue(CurRec);
837// if (RHS == 0) return 0;
838
839// if (Lex.getCode() != tgtok::r_paren) {
840// TokError("expected ')' in binary operator");
841// return 0;
842// }
843// Lex.Lex(); // eat the ')'
844
845// switch (LexCode) {
846// default: assert(0 && "Unhandled code!");
847// case tgtok::XForEach: {
848// TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
849// if (MHSt == 0) {
850// TokError("could not get type for !foreach");
851// return 0;
852// }
853// Type = MHSt->getType();
854// break;
855// }
856// case tgtok::XSubst: {
857// TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
858// if (RHSt == 0) {
859// TokError("could not get type for !subst");
860// return 0;
861// }
862// Type = RHSt->getType();
863// break;
864// }
865// }
866// return (new TernOpInit(Code, LHS, MHS, RHS, Type))->Fold(CurRec, CurMultiClass);
867// }
868 }
869 TokError("could not parse operation");
870 return 0;
871}
872
873/// ParseOperatorType - Parse a type for an operator. This returns
874/// null on error.
875///
876/// OperatorType ::= '<' Type '>'
877///
878RecTy *TGParser::ParseOperatorType(void) {
879 RecTy *Type = 0;
880
881 if (Lex.getCode() != tgtok::less) {
882 TokError("expected type name for operator");
883 return 0;
884 }
885 Lex.Lex(); // eat the <
886
887 Type = ParseType();
888
889 if (Type == 0) {
890 TokError("expected type name for operator");
891 return 0;
892 }
893
894 if (Lex.getCode() != tgtok::greater) {
895 TokError("expected type name for operator");
896 return 0;
897 }
898 Lex.Lex(); // eat the >
899
900 return Type;
901}
902
903
Chris Lattnerf4601652007-11-22 20:49:04 +0000904/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
905///
906/// SimpleValue ::= IDValue
907/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +0000908/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +0000909/// SimpleValue ::= CODEFRAGMENT
910/// SimpleValue ::= '?'
911/// SimpleValue ::= '{' ValueList '}'
912/// SimpleValue ::= ID '<' ValueListNE '>'
913/// SimpleValue ::= '[' ValueList ']'
914/// SimpleValue ::= '(' IDValue DagArgList ')'
915/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
916/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
917/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
918/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
919/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
920///
921Init *TGParser::ParseSimpleValue(Record *CurRec) {
922 Init *R = 0;
923 switch (Lex.getCode()) {
924 default: TokError("Unknown token when parsing a value"); break;
925 case tgtok::IntVal: R = new IntInit(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerd7a50cf2009-03-11 17:08:13 +0000926 case tgtok::StrVal: {
927 std::string Val = Lex.getCurStrVal();
928 Lex.Lex();
929
Jim Grosbachda4231f2009-03-26 16:17:51 +0000930 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +0000931 while (Lex.getCode() == tgtok::StrVal) {
932 Val += Lex.getCurStrVal();
933 Lex.Lex();
934 }
935
936 R = new StringInit(Val);
937 break;
938 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000939 case tgtok::CodeFragment:
940 R = new CodeInit(Lex.getCurStrVal()); Lex.Lex(); break;
941 case tgtok::question: R = new UnsetInit(); Lex.Lex(); break;
942 case tgtok::Id: {
Chris Lattner1c8ae592009-03-13 16:01:53 +0000943 TGLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000944 std::string Name = Lex.getCurStrVal();
945 if (Lex.Lex() != tgtok::less) // consume the Id.
946 return ParseIDValue(CurRec, Name, NameLoc); // Value ::= IDValue
947
948 // Value ::= ID '<' ValueListNE '>'
949 if (Lex.Lex() == tgtok::greater) {
950 TokError("expected non-empty value list");
951 return 0;
952 }
953 std::vector<Init*> ValueList = ParseValueList(CurRec);
954 if (ValueList.empty()) return 0;
955
956 if (Lex.getCode() != tgtok::greater) {
957 TokError("expected '>' at end of value list");
958 return 0;
959 }
960 Lex.Lex(); // eat the '>'
961
962 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
963 // a new anonymous definition, deriving from CLASS<initvalslist> with no
964 // body.
965 Record *Class = Records.getClass(Name);
966 if (!Class) {
967 Error(NameLoc, "Expected a class name, got '" + Name + "'");
968 return 0;
969 }
970
971 // Create the new record, set it as CurRec temporarily.
972 static unsigned AnonCounter = 0;
Chris Lattner7b9ffe42009-03-13 16:09:24 +0000973 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),NameLoc);
Chris Lattnerf4601652007-11-22 20:49:04 +0000974 SubClassReference SCRef;
975 SCRef.RefLoc = NameLoc;
976 SCRef.Rec = Class;
977 SCRef.TemplateArgs = ValueList;
978 // Add info about the subclass to NewRec.
979 if (AddSubClass(NewRec, SCRef))
980 return 0;
981 NewRec->resolveReferences();
982 Records.addDef(NewRec);
983
984 // The result of the expression is a reference to the new record.
985 return new DefInit(NewRec);
986 }
987 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1c8ae592009-03-13 16:01:53 +0000988 TGLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000989 Lex.Lex(); // eat the '{'
990 std::vector<Init*> Vals;
991
992 if (Lex.getCode() != tgtok::r_brace) {
993 Vals = ParseValueList(CurRec);
994 if (Vals.empty()) return 0;
995 }
996 if (Lex.getCode() != tgtok::r_brace) {
997 TokError("expected '}' at end of bit list value");
998 return 0;
999 }
1000 Lex.Lex(); // eat the '}'
1001
1002 BitsInit *Result = new BitsInit(Vals.size());
1003 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1004 Init *Bit = Vals[i]->convertInitializerTo(new BitRecTy());
1005 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +00001006 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1007 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +00001008 return 0;
1009 }
1010 Result->setBit(Vals.size()-i-1, Bit);
1011 }
1012 return Result;
1013 }
1014 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1015 Lex.Lex(); // eat the '['
1016 std::vector<Init*> Vals;
1017
1018 if (Lex.getCode() != tgtok::r_square) {
1019 Vals = ParseValueList(CurRec);
1020 if (Vals.empty()) return 0;
1021 }
1022 if (Lex.getCode() != tgtok::r_square) {
1023 TokError("expected ']' at end of list value");
1024 return 0;
1025 }
1026 Lex.Lex(); // eat the ']'
1027 return new ListInit(Vals);
1028 }
1029 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1030 Lex.Lex(); // eat the '('
David Greenec7cafcd2009-04-22 20:18:10 +00001031 if (Lex.getCode() != tgtok::Id
David Greenee6c27de2009-05-14 21:22:49 +00001032 && Lex.getCode() != tgtok::XCast
David Greenec7cafcd2009-04-22 20:18:10 +00001033 && Lex.getCode() != tgtok::XNameConcat) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001034 TokError("expected identifier in dag init");
1035 return 0;
1036 }
1037
David Greenec7cafcd2009-04-22 20:18:10 +00001038 Init *Operator = 0;
1039 if (Lex.getCode() == tgtok::Id) {
1040 Operator = ParseIDValue(CurRec);
1041 if (Operator == 0) return 0;
1042 }
1043 else {
David Greened418c1b2009-05-14 20:54:48 +00001044 Operator = ParseOperation(CurRec);
1045 if (Operator == 0) return 0;
David Greenec7cafcd2009-04-22 20:18:10 +00001046 }
1047
Nate Begeman7cee8172009-03-19 05:21:56 +00001048 // If the operator name is present, parse it.
1049 std::string OperatorName;
1050 if (Lex.getCode() == tgtok::colon) {
1051 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1052 TokError("expected variable name in dag operator");
1053 return 0;
1054 }
1055 OperatorName = Lex.getCurStrVal();
1056 Lex.Lex(); // eat the VarName.
1057 }
1058
Chris Lattnerf4601652007-11-22 20:49:04 +00001059 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
1060 if (Lex.getCode() != tgtok::r_paren) {
1061 DagArgs = ParseDagArgList(CurRec);
1062 if (DagArgs.empty()) return 0;
1063 }
1064
1065 if (Lex.getCode() != tgtok::r_paren) {
1066 TokError("expected ')' in dag init");
1067 return 0;
1068 }
1069 Lex.Lex(); // eat the ')'
1070
Nate Begeman7cee8172009-03-19 05:21:56 +00001071 return new DagInit(Operator, OperatorName, DagArgs);
David Greened418c1b2009-05-14 20:54:48 +00001072 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001073 }
David Greened418c1b2009-05-14 20:54:48 +00001074
David Greenee6c27de2009-05-14 21:22:49 +00001075 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001076 case tgtok::XConcat:
1077 case tgtok::XSRA:
1078 case tgtok::XSRL:
1079 case tgtok::XSHL:
David Greenec7cafcd2009-04-22 20:18:10 +00001080 case tgtok::XStrConcat:
1081 case tgtok::XNameConcat: { // Value ::= !binop '(' Value ',' Value ')'
David Greened418c1b2009-05-14 20:54:48 +00001082 // case tgtok::XForEach:
1083 // case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
1084 return ParseOperation(CurRec);
1085 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001086 }
1087 }
1088
1089 return R;
1090}
1091
1092/// ParseValue - Parse a tblgen value. This returns null on error.
1093///
1094/// Value ::= SimpleValue ValueSuffix*
1095/// ValueSuffix ::= '{' BitList '}'
1096/// ValueSuffix ::= '[' BitList ']'
1097/// ValueSuffix ::= '.' ID
1098///
1099Init *TGParser::ParseValue(Record *CurRec) {
1100 Init *Result = ParseSimpleValue(CurRec);
1101 if (Result == 0) return 0;
1102
1103 // Parse the suffixes now if present.
1104 while (1) {
1105 switch (Lex.getCode()) {
1106 default: return Result;
1107 case tgtok::l_brace: {
Chris Lattner1c8ae592009-03-13 16:01:53 +00001108 TGLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001109 Lex.Lex(); // eat the '{'
1110 std::vector<unsigned> Ranges = ParseRangeList();
1111 if (Ranges.empty()) return 0;
1112
1113 // Reverse the bitlist.
1114 std::reverse(Ranges.begin(), Ranges.end());
1115 Result = Result->convertInitializerBitRange(Ranges);
1116 if (Result == 0) {
1117 Error(CurlyLoc, "Invalid bit range for value");
1118 return 0;
1119 }
1120
1121 // Eat the '}'.
1122 if (Lex.getCode() != tgtok::r_brace) {
1123 TokError("expected '}' at end of bit range list");
1124 return 0;
1125 }
1126 Lex.Lex();
1127 break;
1128 }
1129 case tgtok::l_square: {
Chris Lattner1c8ae592009-03-13 16:01:53 +00001130 TGLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001131 Lex.Lex(); // eat the '['
1132 std::vector<unsigned> Ranges = ParseRangeList();
1133 if (Ranges.empty()) return 0;
1134
1135 Result = Result->convertInitListSlice(Ranges);
1136 if (Result == 0) {
1137 Error(SquareLoc, "Invalid range for list slice");
1138 return 0;
1139 }
1140
1141 // Eat the ']'.
1142 if (Lex.getCode() != tgtok::r_square) {
1143 TokError("expected ']' at end of list slice");
1144 return 0;
1145 }
1146 Lex.Lex();
1147 break;
1148 }
1149 case tgtok::period:
1150 if (Lex.Lex() != tgtok::Id) { // eat the .
1151 TokError("expected field identifier after '.'");
1152 return 0;
1153 }
1154 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001155 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00001156 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +00001157 return 0;
1158 }
1159 Result = new FieldInit(Result, Lex.getCurStrVal());
1160 Lex.Lex(); // eat field name
1161 break;
1162 }
1163 }
1164}
1165
1166/// ParseDagArgList - Parse the argument list for a dag literal expression.
1167///
1168/// ParseDagArgList ::= Value (':' VARNAME)?
1169/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
1170std::vector<std::pair<llvm::Init*, std::string> >
1171TGParser::ParseDagArgList(Record *CurRec) {
1172 std::vector<std::pair<llvm::Init*, std::string> > Result;
1173
1174 while (1) {
1175 Init *Val = ParseValue(CurRec);
1176 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
1177
1178 // If the variable name is present, add it.
1179 std::string VarName;
1180 if (Lex.getCode() == tgtok::colon) {
1181 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1182 TokError("expected variable name in dag literal");
1183 return std::vector<std::pair<llvm::Init*, std::string> >();
1184 }
1185 VarName = Lex.getCurStrVal();
1186 Lex.Lex(); // eat the VarName.
1187 }
1188
1189 Result.push_back(std::make_pair(Val, VarName));
1190
1191 if (Lex.getCode() != tgtok::comma) break;
1192 Lex.Lex(); // eat the ','
1193 }
1194
1195 return Result;
1196}
1197
1198
1199/// ParseValueList - Parse a comma separated list of values, returning them as a
1200/// vector. Note that this always expects to be able to parse at least one
1201/// value. It returns an empty list if this is not possible.
1202///
1203/// ValueList ::= Value (',' Value)
1204///
1205std::vector<Init*> TGParser::ParseValueList(Record *CurRec) {
1206 std::vector<Init*> Result;
1207 Result.push_back(ParseValue(CurRec));
1208 if (Result.back() == 0) return std::vector<Init*>();
1209
1210 while (Lex.getCode() == tgtok::comma) {
1211 Lex.Lex(); // Eat the comma
1212
1213 Result.push_back(ParseValue(CurRec));
1214 if (Result.back() == 0) return std::vector<Init*>();
1215 }
1216
1217 return Result;
1218}
1219
1220
1221
1222/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1223/// empty string on error. This can happen in a number of different context's,
1224/// including within a def or in the template args for a def (which which case
1225/// CurRec will be non-null) and within the template args for a multiclass (in
1226/// which case CurRec will be null, but CurMultiClass will be set). This can
1227/// also happen within a def that is within a multiclass, which will set both
1228/// CurRec and CurMultiClass.
1229///
1230/// Declaration ::= FIELD? Type ID ('=' Value)?
1231///
1232std::string TGParser::ParseDeclaration(Record *CurRec,
1233 bool ParsingTemplateArgs) {
1234 // Read the field prefix if present.
1235 bool HasField = Lex.getCode() == tgtok::Field;
1236 if (HasField) Lex.Lex();
1237
1238 RecTy *Type = ParseType();
1239 if (Type == 0) return "";
1240
1241 if (Lex.getCode() != tgtok::Id) {
1242 TokError("Expected identifier in declaration");
1243 return "";
1244 }
1245
Chris Lattner1c8ae592009-03-13 16:01:53 +00001246 TGLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001247 std::string DeclName = Lex.getCurStrVal();
1248 Lex.Lex();
1249
1250 if (ParsingTemplateArgs) {
1251 if (CurRec) {
1252 DeclName = CurRec->getName() + ":" + DeclName;
1253 } else {
1254 assert(CurMultiClass);
1255 }
1256 if (CurMultiClass)
1257 DeclName = CurMultiClass->Rec.getName() + "::" + DeclName;
1258 }
1259
1260 // Add the value.
1261 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
1262 return "";
1263
1264 // If a value is present, parse it.
1265 if (Lex.getCode() == tgtok::equal) {
1266 Lex.Lex();
Chris Lattner1c8ae592009-03-13 16:01:53 +00001267 TGLoc ValLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001268 Init *Val = ParseValue(CurRec);
1269 if (Val == 0 ||
1270 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
1271 return "";
1272 }
1273
1274 return DeclName;
1275}
1276
1277/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1278/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1279/// template args for a def, which may or may not be in a multiclass. If null,
1280/// these are the template args for a multiclass.
1281///
1282/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
1283///
1284bool TGParser::ParseTemplateArgList(Record *CurRec) {
1285 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1286 Lex.Lex(); // eat the '<'
1287
1288 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
1289
1290 // Read the first declaration.
1291 std::string TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1292 if (TemplArg.empty())
1293 return true;
1294
1295 TheRecToAddTo->addTemplateArg(TemplArg);
1296
1297 while (Lex.getCode() == tgtok::comma) {
1298 Lex.Lex(); // eat the ','
1299
1300 // Read the following declarations.
1301 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1302 if (TemplArg.empty())
1303 return true;
1304 TheRecToAddTo->addTemplateArg(TemplArg);
1305 }
1306
1307 if (Lex.getCode() != tgtok::greater)
1308 return TokError("expected '>' at end of template argument list");
1309 Lex.Lex(); // eat the '>'.
1310 return false;
1311}
1312
1313
1314/// ParseBodyItem - Parse a single item at within the body of a def or class.
1315///
1316/// BodyItem ::= Declaration ';'
1317/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1318bool TGParser::ParseBodyItem(Record *CurRec) {
1319 if (Lex.getCode() != tgtok::Let) {
1320 if (ParseDeclaration(CurRec, false).empty())
1321 return true;
1322
1323 if (Lex.getCode() != tgtok::semi)
1324 return TokError("expected ';' after declaration");
1325 Lex.Lex();
1326 return false;
1327 }
1328
1329 // LET ID OptionalRangeList '=' Value ';'
1330 if (Lex.Lex() != tgtok::Id)
1331 return TokError("expected field identifier after let");
1332
Chris Lattner1c8ae592009-03-13 16:01:53 +00001333 TGLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001334 std::string FieldName = Lex.getCurStrVal();
1335 Lex.Lex(); // eat the field name.
1336
1337 std::vector<unsigned> BitList;
1338 if (ParseOptionalBitList(BitList))
1339 return true;
1340 std::reverse(BitList.begin(), BitList.end());
1341
1342 if (Lex.getCode() != tgtok::equal)
1343 return TokError("expected '=' in let expression");
1344 Lex.Lex(); // eat the '='.
1345
1346 Init *Val = ParseValue(CurRec);
1347 if (Val == 0) return true;
1348
1349 if (Lex.getCode() != tgtok::semi)
1350 return TokError("expected ';' after let expression");
1351 Lex.Lex();
1352
1353 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1354}
1355
1356/// ParseBody - Read the body of a class or def. Return true on error, false on
1357/// success.
1358///
1359/// Body ::= ';'
1360/// Body ::= '{' BodyList '}'
1361/// BodyList BodyItem*
1362///
1363bool TGParser::ParseBody(Record *CurRec) {
1364 // If this is a null definition, just eat the semi and return.
1365 if (Lex.getCode() == tgtok::semi) {
1366 Lex.Lex();
1367 return false;
1368 }
1369
1370 if (Lex.getCode() != tgtok::l_brace)
1371 return TokError("Expected ';' or '{' to start body");
1372 // Eat the '{'.
1373 Lex.Lex();
1374
1375 while (Lex.getCode() != tgtok::r_brace)
1376 if (ParseBodyItem(CurRec))
1377 return true;
1378
1379 // Eat the '}'.
1380 Lex.Lex();
1381 return false;
1382}
1383
1384/// ParseObjectBody - Parse the body of a def or class. This consists of an
1385/// optional ClassList followed by a Body. CurRec is the current def or class
1386/// that is being parsed.
1387///
1388/// ObjectBody ::= BaseClassList Body
1389/// BaseClassList ::= /*empty*/
1390/// BaseClassList ::= ':' BaseClassListNE
1391/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1392///
1393bool TGParser::ParseObjectBody(Record *CurRec) {
1394 // If there is a baseclass list, read it.
1395 if (Lex.getCode() == tgtok::colon) {
1396 Lex.Lex();
1397
1398 // Read all of the subclasses.
1399 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1400 while (1) {
1401 // Check for error.
1402 if (SubClass.Rec == 0) return true;
1403
1404 // Add it.
1405 if (AddSubClass(CurRec, SubClass))
1406 return true;
1407
1408 if (Lex.getCode() != tgtok::comma) break;
1409 Lex.Lex(); // eat ','.
1410 SubClass = ParseSubClassReference(CurRec, false);
1411 }
1412 }
1413
1414 // Process any variables on the let stack.
1415 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1416 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1417 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1418 LetStack[i][j].Bits, LetStack[i][j].Value))
1419 return true;
1420
1421 return ParseBody(CurRec);
1422}
1423
1424
1425/// ParseDef - Parse and return a top level or multiclass def, return the record
1426/// corresponding to it. This returns null on error.
1427///
1428/// DefInst ::= DEF ObjectName ObjectBody
1429///
1430llvm::Record *TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1c8ae592009-03-13 16:01:53 +00001431 TGLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001432 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
1433 Lex.Lex(); // Eat the 'def' token.
1434
1435 // Parse ObjectName and make a record for it.
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001436 Record *CurRec = new Record(ParseObjectName(), DefLoc);
Chris Lattnerf4601652007-11-22 20:49:04 +00001437
1438 if (!CurMultiClass) {
1439 // Top-level def definition.
1440
1441 // Ensure redefinition doesn't happen.
1442 if (Records.getDef(CurRec->getName())) {
1443 Error(DefLoc, "def '" + CurRec->getName() + "' already defined");
1444 return 0;
1445 }
1446 Records.addDef(CurRec);
1447 } else {
1448 // Otherwise, a def inside a multiclass, add it to the multiclass.
1449 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
1450 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
1451 Error(DefLoc, "def '" + CurRec->getName() +
1452 "' already defined in this multiclass!");
1453 return 0;
1454 }
1455 CurMultiClass->DefPrototypes.push_back(CurRec);
1456 }
1457
1458 if (ParseObjectBody(CurRec))
1459 return 0;
1460
1461 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
1462 CurRec->resolveReferences();
1463
1464 // If ObjectBody has template arguments, it's an error.
1465 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
1466 return CurRec;
1467}
1468
1469
1470/// ParseClass - Parse a tblgen class definition.
1471///
1472/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
1473///
1474bool TGParser::ParseClass() {
1475 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
1476 Lex.Lex();
1477
1478 if (Lex.getCode() != tgtok::Id)
1479 return TokError("expected class name after 'class' keyword");
1480
1481 Record *CurRec = Records.getClass(Lex.getCurStrVal());
1482 if (CurRec) {
1483 // If the body was previously defined, this is an error.
1484 if (!CurRec->getValues().empty() ||
1485 !CurRec->getSuperClasses().empty() ||
1486 !CurRec->getTemplateArgs().empty())
1487 return TokError("Class '" + CurRec->getName() + "' already defined");
1488 } else {
1489 // If this is the first reference to this class, create and add it.
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001490 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc());
Chris Lattnerf4601652007-11-22 20:49:04 +00001491 Records.addClass(CurRec);
1492 }
1493 Lex.Lex(); // eat the name.
1494
1495 // If there are template args, parse them.
1496 if (Lex.getCode() == tgtok::less)
1497 if (ParseTemplateArgList(CurRec))
1498 return true;
1499
1500 // Finally, parse the object body.
1501 return ParseObjectBody(CurRec);
1502}
1503
1504/// ParseLetList - Parse a non-empty list of assignment expressions into a list
1505/// of LetRecords.
1506///
1507/// LetList ::= LetItem (',' LetItem)*
1508/// LetItem ::= ID OptionalRangeList '=' Value
1509///
1510std::vector<LetRecord> TGParser::ParseLetList() {
1511 std::vector<LetRecord> Result;
1512
1513 while (1) {
1514 if (Lex.getCode() != tgtok::Id) {
1515 TokError("expected identifier in let definition");
1516 return std::vector<LetRecord>();
1517 }
1518 std::string Name = Lex.getCurStrVal();
Chris Lattner1c8ae592009-03-13 16:01:53 +00001519 TGLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001520 Lex.Lex(); // Eat the identifier.
1521
1522 // Check for an optional RangeList.
1523 std::vector<unsigned> Bits;
1524 if (ParseOptionalRangeList(Bits))
1525 return std::vector<LetRecord>();
1526 std::reverse(Bits.begin(), Bits.end());
1527
1528 if (Lex.getCode() != tgtok::equal) {
1529 TokError("expected '=' in let expression");
1530 return std::vector<LetRecord>();
1531 }
1532 Lex.Lex(); // eat the '='.
1533
1534 Init *Val = ParseValue(0);
1535 if (Val == 0) return std::vector<LetRecord>();
1536
1537 // Now that we have everything, add the record.
1538 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
1539
1540 if (Lex.getCode() != tgtok::comma)
1541 return Result;
1542 Lex.Lex(); // eat the comma.
1543 }
1544}
1545
1546/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
1547/// different related productions.
1548///
1549/// Object ::= LET LetList IN '{' ObjectList '}'
1550/// Object ::= LET LetList IN Object
1551///
1552bool TGParser::ParseTopLevelLet() {
1553 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
1554 Lex.Lex();
1555
1556 // Add this entry to the let stack.
1557 std::vector<LetRecord> LetInfo = ParseLetList();
1558 if (LetInfo.empty()) return true;
1559 LetStack.push_back(LetInfo);
1560
1561 if (Lex.getCode() != tgtok::In)
1562 return TokError("expected 'in' at end of top-level 'let'");
1563 Lex.Lex();
1564
1565 // If this is a scalar let, just handle it now
1566 if (Lex.getCode() != tgtok::l_brace) {
1567 // LET LetList IN Object
1568 if (ParseObject())
1569 return true;
1570 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1c8ae592009-03-13 16:01:53 +00001571 TGLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001572 // Otherwise, this is a group let.
1573 Lex.Lex(); // eat the '{'.
1574
1575 // Parse the object list.
1576 if (ParseObjectList())
1577 return true;
1578
1579 if (Lex.getCode() != tgtok::r_brace) {
1580 TokError("expected '}' at end of top level let command");
1581 return Error(BraceLoc, "to match this '{'");
1582 }
1583 Lex.Lex();
1584 }
1585
1586 // Outside this let scope, this let block is not active.
1587 LetStack.pop_back();
1588 return false;
1589}
1590
1591/// ParseMultiClassDef - Parse a def in a multiclass context.
1592///
1593/// MultiClassDef ::= DefInst
1594///
1595bool TGParser::ParseMultiClassDef(MultiClass *CurMC) {
1596 if (Lex.getCode() != tgtok::Def)
1597 return TokError("expected 'def' in multiclass body");
1598
1599 Record *D = ParseDef(CurMC);
1600 if (D == 0) return true;
1601
1602 // Copy the template arguments for the multiclass into the def.
1603 const std::vector<std::string> &TArgs = CurMC->Rec.getTemplateArgs();
1604
1605 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1606 const RecordVal *RV = CurMC->Rec.getValue(TArgs[i]);
1607 assert(RV && "Template arg doesn't exist?");
1608 D->addValue(*RV);
1609 }
1610
1611 return false;
1612}
1613
1614/// ParseMultiClass - Parse a multiclass definition.
1615///
Bob Wilson32558652009-04-28 19:41:44 +00001616/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
1617/// ':' BaseMultiClassList '{' MultiClassDef+ '}'
Chris Lattnerf4601652007-11-22 20:49:04 +00001618///
1619bool TGParser::ParseMultiClass() {
1620 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
1621 Lex.Lex(); // Eat the multiclass token.
1622
1623 if (Lex.getCode() != tgtok::Id)
1624 return TokError("expected identifier after multiclass for name");
1625 std::string Name = Lex.getCurStrVal();
1626
1627 if (MultiClasses.count(Name))
1628 return TokError("multiclass '" + Name + "' already defined");
1629
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001630 CurMultiClass = MultiClasses[Name] = new MultiClass(Name, Lex.getLoc());
Chris Lattnerf4601652007-11-22 20:49:04 +00001631 Lex.Lex(); // Eat the identifier.
1632
1633 // If there are template args, parse them.
1634 if (Lex.getCode() == tgtok::less)
1635 if (ParseTemplateArgList(0))
1636 return true;
1637
David Greened34a73b2009-04-24 16:55:41 +00001638 bool inherits = false;
1639
David Greenede444af2009-04-22 16:42:54 +00001640 // If there are submulticlasses, parse them.
1641 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00001642 inherits = true;
1643
David Greenede444af2009-04-22 16:42:54 +00001644 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00001645
David Greenede444af2009-04-22 16:42:54 +00001646 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00001647 SubMultiClassReference SubMultiClass =
1648 ParseSubMultiClassReference(CurMultiClass);
David Greenede444af2009-04-22 16:42:54 +00001649 while (1) {
1650 // Check for error.
1651 if (SubMultiClass.MC == 0) return true;
Bob Wilson32558652009-04-28 19:41:44 +00001652
David Greenede444af2009-04-22 16:42:54 +00001653 // Add it.
1654 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
1655 return true;
Bob Wilson32558652009-04-28 19:41:44 +00001656
David Greenede444af2009-04-22 16:42:54 +00001657 if (Lex.getCode() != tgtok::comma) break;
1658 Lex.Lex(); // eat ','.
1659 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
1660 }
1661 }
1662
David Greened34a73b2009-04-24 16:55:41 +00001663 if (Lex.getCode() != tgtok::l_brace) {
1664 if (!inherits)
1665 return TokError("expected '{' in multiclass definition");
1666 else
1667 if (Lex.getCode() != tgtok::semi)
1668 return TokError("expected ';' in multiclass definition");
1669 else
1670 Lex.Lex(); // eat the ';'.
1671 }
1672 else {
1673 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
1674 return TokError("multiclass must contain at least one def");
Chris Lattnerf4601652007-11-22 20:49:04 +00001675
David Greened34a73b2009-04-24 16:55:41 +00001676 while (Lex.getCode() != tgtok::r_brace)
1677 if (ParseMultiClassDef(CurMultiClass))
1678 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001679
David Greened34a73b2009-04-24 16:55:41 +00001680 Lex.Lex(); // eat the '}'.
1681 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001682
1683 CurMultiClass = 0;
1684 return false;
1685}
1686
1687/// ParseDefm - Parse the instantiation of a multiclass.
1688///
1689/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
1690///
1691bool TGParser::ParseDefm() {
1692 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
1693 if (Lex.Lex() != tgtok::Id) // eat the defm.
1694 return TokError("expected identifier after defm");
1695
Chris Lattner1c8ae592009-03-13 16:01:53 +00001696 TGLoc DefmPrefixLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001697 std::string DefmPrefix = Lex.getCurStrVal();
1698 if (Lex.Lex() != tgtok::colon)
1699 return TokError("expected ':' after defm identifier");
1700
1701 // eat the colon.
1702 Lex.Lex();
1703
Chris Lattner1c8ae592009-03-13 16:01:53 +00001704 TGLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001705 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene56546132009-04-22 22:17:51 +00001706
1707 while (1) {
1708 if (Ref.Rec == 0) return true;
1709
1710 // To instantiate a multiclass, we need to first get the multiclass, then
1711 // instantiate each def contained in the multiclass with the SubClassRef
1712 // template parameters.
1713 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
1714 assert(MC && "Didn't lookup multiclass correctly?");
1715 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
1716
1717 // Verify that the correct number of template arguments were specified.
1718 const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs();
1719 if (TArgs.size() < TemplateVals.size())
1720 return Error(SubClassLoc,
1721 "more template args specified than multiclass expects");
1722
1723 // Loop over all the def's in the multiclass, instantiating each one.
1724 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
1725 Record *DefProto = MC->DefPrototypes[i];
1726
David Greene065f2592009-05-05 16:28:25 +00001727 // Add in the defm name
1728 std::string DefName = DefProto->getName();
1729 std::string::size_type idx = DefName.find("#NAME#");
1730 if (idx != std::string::npos) {
1731 DefName.replace(idx, 6, DefmPrefix);
1732 }
1733 else {
1734 // Add the suffix to the defm name to get the new name.
1735 DefName = DefmPrefix + DefName;
1736 }
1737
1738 Record *CurRec = new Record(DefName, DefmPrefixLoc);
David Greene56546132009-04-22 22:17:51 +00001739
1740 SubClassReference Ref;
1741 Ref.RefLoc = DefmPrefixLoc;
1742 Ref.Rec = DefProto;
1743 AddSubClass(CurRec, Ref);
1744
1745 // Loop over all of the template arguments, setting them to the specified
1746 // value or leaving them as the default if necessary.
1747 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
Bob Wilson32558652009-04-28 19:41:44 +00001748 // Check if a value is specified for this temp-arg.
1749 if (i < TemplateVals.size()) {
David Greene56546132009-04-22 22:17:51 +00001750 // Set it now.
1751 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
1752 TemplateVals[i]))
1753 return true;
1754
1755 // Resolve it next.
1756 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
1757
1758 // Now remove it.
1759 CurRec->removeValue(TArgs[i]);
1760
1761 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Bob Wilson32558652009-04-28 19:41:44 +00001762 return Error(SubClassLoc,
1763 "value not specified for template argument #"+
David Greene56546132009-04-22 22:17:51 +00001764 utostr(i) + " (" + TArgs[i] + ") of multiclassclass '" +
1765 MC->Rec.getName() + "'");
1766 }
1767 }
1768
1769 // If the mdef is inside a 'let' expression, add to each def.
1770 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1771 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1772 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1773 LetStack[i][j].Bits, LetStack[i][j].Value)) {
1774 Error(DefmPrefixLoc, "when instantiating this defm");
1775 return true;
1776 }
1777
1778 // Ensure redefinition doesn't happen.
1779 if (Records.getDef(CurRec->getName()))
1780 return Error(DefmPrefixLoc, "def '" + CurRec->getName() +
1781 "' already defined, instantiating defm with subdef '" +
1782 DefProto->getName() + "'");
1783 Records.addDef(CurRec);
1784 CurRec->resolveReferences();
1785 }
1786
1787 if (Lex.getCode() != tgtok::comma) break;
1788 Lex.Lex(); // eat ','.
1789
1790 SubClassLoc = Lex.getLoc();
1791 Ref = ParseSubClassReference(0, true);
1792 }
1793
Chris Lattnerf4601652007-11-22 20:49:04 +00001794 if (Lex.getCode() != tgtok::semi)
1795 return TokError("expected ';' at end of defm");
1796 Lex.Lex();
1797
Chris Lattnerf4601652007-11-22 20:49:04 +00001798 return false;
1799}
1800
1801/// ParseObject
1802/// Object ::= ClassInst
1803/// Object ::= DefInst
1804/// Object ::= MultiClassInst
1805/// Object ::= DefMInst
1806/// Object ::= LETCommand '{' ObjectList '}'
1807/// Object ::= LETCommand Object
1808bool TGParser::ParseObject() {
1809 switch (Lex.getCode()) {
1810 default: assert(0 && "This is not an object");
1811 case tgtok::Let: return ParseTopLevelLet();
1812 case tgtok::Def: return ParseDef(0) == 0;
1813 case tgtok::Defm: return ParseDefm();
1814 case tgtok::Class: return ParseClass();
1815 case tgtok::MultiClass: return ParseMultiClass();
1816 }
1817}
1818
1819/// ParseObjectList
1820/// ObjectList :== Object*
1821bool TGParser::ParseObjectList() {
1822 while (isObjectStart(Lex.getCode())) {
1823 if (ParseObject())
1824 return true;
1825 }
1826 return false;
1827}
1828
1829
1830bool TGParser::ParseFile() {
1831 Lex.Lex(); // Prime the lexer.
1832 if (ParseObjectList()) return true;
1833
1834 // If we have unread input at the end of the file, report it.
1835 if (Lex.getCode() == tgtok::Eof)
1836 return false;
1837
1838 return TokError("Unexpected input at top level");
1839}
1840