1 //===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implement the Parser for TableGen.
12 //===----------------------------------------------------------------------===//
18 #include "llvm/ADT/StringExtras.h"
21 //===----------------------------------------------------------------------===//
22 // Support Code for the Semantic Actions.
23 //===----------------------------------------------------------------------===//
26 struct SubClassReference {
29 std::vector<Init*> TemplateArgs;
30 SubClassReference() : Rec(0) {}
32 bool isInvalid() const { return Rec == 0; }
35 struct SubMultiClassReference {
38 std::vector<Init*> TemplateArgs;
39 SubMultiClassReference() : MC(0) {}
41 bool isInvalid() const { return MC == 0; }
44 } // end namespace llvm
46 bool TGParser::AddValue(Record *CurRec, TGLoc Loc, const RecordVal &RV) {
48 CurRec = &CurMultiClass->Rec;
50 if (RecordVal *ERV = CurRec->getValue(RV.getName())) {
51 // The value already exists in the class, treat this as a set.
52 if (ERV->setValue(RV.getValue()))
53 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
54 RV.getType()->getAsString() + "' is incompatible with " +
55 "previous definition of type '" +
56 ERV->getType()->getAsString() + "'");
64 /// Return true on error, false on success.
65 bool TGParser::SetValue(Record *CurRec, TGLoc Loc, const std::string &ValName,
66 const std::vector<unsigned> &BitList, Init *V) {
69 if (CurRec == 0) CurRec = &CurMultiClass->Rec;
71 RecordVal *RV = CurRec->getValue(ValName);
73 return Error(Loc, "Value '" + ValName + "' unknown!");
75 // Do not allow assignments like 'X = X'. This will just cause infinite loops
76 // in the resolution machinery.
78 if (VarInit *VI = dynamic_cast<VarInit*>(V))
79 if (VI->getName() == ValName)
82 // If we are assigning to a subset of the bits in the value... then we must be
83 // assigning to a field of BitsRecTy, which must have a BitsInit
86 if (!BitList.empty()) {
87 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
89 return Error(Loc, "Value '" + ValName + "' is not a bits type");
91 // Convert the incoming value to a bits type of the appropriate size...
92 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList.size()));
94 V->convertInitializerTo(new BitsRecTy(BitList.size()));
95 return Error(Loc, "Initializer is not compatible with bit range");
98 // We should have a BitsInit type now.
99 BitsInit *BInit = dynamic_cast<BitsInit*>(BI);
102 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
104 // Loop over bits, assigning values as appropriate.
105 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
106 unsigned Bit = BitList[i];
107 if (NewVal->getBit(Bit))
108 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
109 ValName + "' more than once");
110 NewVal->setBit(Bit, BInit->getBit(i));
113 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
114 if (NewVal->getBit(i) == 0)
115 NewVal->setBit(i, CurVal->getBit(i));
121 return Error(Loc, "Value '" + ValName + "' of type '" +
122 RV->getType()->getAsString() +
123 "' is incompatible with initializer '" + V->getAsString() +"'");
127 /// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
128 /// args as SubClass's template arguments.
129 bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
130 Record *SC = SubClass.Rec;
131 // Add all of the values in the subclass into the current class.
132 const std::vector<RecordVal> &Vals = SC->getValues();
133 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
134 if (AddValue(CurRec, SubClass.RefLoc, Vals[i]))
137 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
139 // Ensure that an appropriate number of template arguments are specified.
140 if (TArgs.size() < SubClass.TemplateArgs.size())
141 return Error(SubClass.RefLoc, "More template args specified than expected");
143 // Loop over all of the template arguments, setting them to the specified
144 // value or leaving them as the default if necessary.
145 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
146 if (i < SubClass.TemplateArgs.size()) {
147 // If a value is specified for this template arg, set it now.
148 if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(),
149 SubClass.TemplateArgs[i]))
153 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
156 CurRec->removeValue(TArgs[i]);
158 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
159 return Error(SubClass.RefLoc,"Value not specified for template argument #"
160 + utostr(i) + " (" + TArgs[i] + ") of subclass '" +
161 SC->getName() + "'!");
165 // Since everything went well, we can now set the "superclass" list for the
167 const std::vector<Record*> &SCs = SC->getSuperClasses();
168 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
169 if (CurRec->isSubClassOf(SCs[i]))
170 return Error(SubClass.RefLoc,
171 "Already subclass of '" + SCs[i]->getName() + "'!\n");
172 CurRec->addSuperClass(SCs[i]);
175 if (CurRec->isSubClassOf(SC))
176 return Error(SubClass.RefLoc,
177 "Already subclass of '" + SC->getName() + "'!\n");
178 CurRec->addSuperClass(SC);
182 /// AddSubMultiClass - Add SubMultiClass as a subclass to
183 /// CurMultiClass, resolving its template args as SubMultiClass's
184 /// template arguments.
185 bool TGParser::AddSubMultiClass(MultiClass *CurMultiClass, class SubMultiClassReference &SubMultiClass) {
186 MultiClass *SMC = SubMultiClass.MC;
187 Record *CurRec = &CurMultiClass->Rec;
189 const std::vector<RecordVal> &MCVals = CurMultiClass->Rec.getValues();
191 // Add all of the values in the subclass into the current class.
192 const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues();
193 for (unsigned i = 0, e = SMCVals.size(); i != e; ++i)
194 if (AddValue(CurRec, SubMultiClass.RefLoc, SMCVals[i]))
197 // Add all of the defs in the subclass into the current multiclass.
198 for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(),
199 iend = SMC->DefPrototypes.end();
202 // Clone the def and add it to the current multiclass
203 Record *NewDef = new Record(**i);
205 // Add all of the values in the superclass into the current def.
206 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
207 if (AddValue(NewDef, SubMultiClass.RefLoc, MCVals[i]))
210 CurMultiClass->DefPrototypes.push_back(NewDef);
213 const std::vector<std::string> &SMCTArgs = SMC->Rec.getTemplateArgs();
215 // Ensure that an appropriate number of template arguments are specified.
216 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
217 return Error(SubMultiClass.RefLoc, "More template args specified than expected");
219 // Loop over all of the template arguments, setting them to the specified
220 // value or leaving them as the default if necessary.
221 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
222 if (i < SubMultiClass.TemplateArgs.size()) {
223 // If a value is specified for this template arg, set it in the superclass now.
224 if (SetValue(CurRec, SubMultiClass.RefLoc, SMCTArgs[i], std::vector<unsigned>(),
225 SubMultiClass.TemplateArgs[i]))
229 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
232 CurRec->removeValue(SMCTArgs[i]);
234 // If a value is specified for this template arg, set it in the defs now.
235 for (MultiClass::RecordVector::iterator j = CurMultiClass->DefPrototypes.begin(),
236 jend = CurMultiClass->DefPrototypes.end();
241 if (SetValue(Def, SubMultiClass.RefLoc, SMCTArgs[i], std::vector<unsigned>(),
242 SubMultiClass.TemplateArgs[i]))
246 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
249 Def->removeValue(SMCTArgs[i]);
251 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
252 return Error(SubMultiClass.RefLoc,"Value not specified for template argument #"
253 + utostr(i) + " (" + SMCTArgs[i] + ") of subclass '" +
254 SMC->Rec.getName() + "'!");
261 //===----------------------------------------------------------------------===//
263 //===----------------------------------------------------------------------===//
265 /// isObjectStart - Return true if this is a valid first token for an Object.
266 static bool isObjectStart(tgtok::TokKind K) {
267 return K == tgtok::Class || K == tgtok::Def ||
268 K == tgtok::Defm || K == tgtok::Let || K == tgtok::MultiClass;
271 /// ParseObjectName - If an object name is specified, return it. Otherwise,
272 /// return an anonymous name.
273 /// ObjectName ::= ID
274 /// ObjectName ::= /*empty*/
276 std::string TGParser::ParseObjectName() {
277 if (Lex.getCode() == tgtok::Id) {
278 std::string Ret = Lex.getCurStrVal();
283 static unsigned AnonCounter = 0;
284 return "anonymous."+utostr(AnonCounter++);
288 /// ParseClassID - Parse and resolve a reference to a class name. This returns
293 Record *TGParser::ParseClassID() {
294 if (Lex.getCode() != tgtok::Id) {
295 TokError("expected name for ClassID");
299 Record *Result = Records.getClass(Lex.getCurStrVal());
301 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
307 /// ParseMultiClassID - Parse and resolve a reference to a multiclass name. This returns
310 /// MultiClassID ::= ID
312 MultiClass *TGParser::ParseMultiClassID() {
313 if (Lex.getCode() != tgtok::Id) {
314 TokError("expected name for ClassID");
318 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
320 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
326 Record *TGParser::ParseDefmID() {
327 if (Lex.getCode() != tgtok::Id) {
328 TokError("expected multiclass name");
332 MultiClass *MC = MultiClasses[Lex.getCurStrVal()];
334 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
344 /// ParseSubClassReference - Parse a reference to a subclass or to a templated
345 /// subclass. This returns a SubClassRefTy with a null Record* on error.
347 /// SubClassRef ::= ClassID
348 /// SubClassRef ::= ClassID '<' ValueList '>'
350 SubClassReference TGParser::
351 ParseSubClassReference(Record *CurRec, bool isDefm) {
352 SubClassReference Result;
353 Result.RefLoc = Lex.getLoc();
356 Result.Rec = ParseDefmID();
358 Result.Rec = ParseClassID();
359 if (Result.Rec == 0) return Result;
361 // If there is no template arg list, we're done.
362 if (Lex.getCode() != tgtok::less)
364 Lex.Lex(); // Eat the '<'
366 if (Lex.getCode() == tgtok::greater) {
367 TokError("subclass reference requires a non-empty list of template values");
372 Result.TemplateArgs = ParseValueList(CurRec);
373 if (Result.TemplateArgs.empty()) {
374 Result.Rec = 0; // Error parsing value list.
378 if (Lex.getCode() != tgtok::greater) {
379 TokError("expected '>' in template value list");
388 /// ParseSubMultiClassReference - Parse a reference to a subclass or to a templated
389 /// submulticlass. This returns a SubMultiClassRefTy with a null Record* on error.
391 /// SubMultiClassRef ::= MultiClassID
392 /// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
394 SubMultiClassReference TGParser::
395 ParseSubMultiClassReference(MultiClass *CurMC) {
396 SubMultiClassReference Result;
397 Result.RefLoc = Lex.getLoc();
399 Result.MC = ParseMultiClassID();
400 if (Result.MC == 0) return Result;
402 // If there is no template arg list, we're done.
403 if (Lex.getCode() != tgtok::less)
405 Lex.Lex(); // Eat the '<'
407 if (Lex.getCode() == tgtok::greater) {
408 TokError("subclass reference requires a non-empty list of template values");
413 Result.TemplateArgs = ParseValueList(&CurMC->Rec);
414 if (Result.TemplateArgs.empty()) {
415 Result.MC = 0; // Error parsing value list.
419 if (Lex.getCode() != tgtok::greater) {
420 TokError("expected '>' in template value list");
429 /// ParseRangePiece - Parse a bit/value range.
430 /// RangePiece ::= INTVAL
431 /// RangePiece ::= INTVAL '-' INTVAL
432 /// RangePiece ::= INTVAL INTVAL
433 bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
434 if (Lex.getCode() != tgtok::IntVal) {
435 TokError("expected integer or bitrange");
438 int64_t Start = Lex.getCurIntVal();
442 return TokError("invalid range, cannot be negative");
444 switch (Lex.Lex()) { // eat first character.
446 Ranges.push_back(Start);
449 if (Lex.Lex() != tgtok::IntVal) {
450 TokError("expected integer value as end of range");
453 End = Lex.getCurIntVal();
456 End = -Lex.getCurIntVal();
460 return TokError("invalid range, cannot be negative");
465 for (; Start <= End; ++Start)
466 Ranges.push_back(Start);
468 for (; Start >= End; --Start)
469 Ranges.push_back(Start);
474 /// ParseRangeList - Parse a list of scalars and ranges into scalar values.
476 /// RangeList ::= RangePiece (',' RangePiece)*
478 std::vector<unsigned> TGParser::ParseRangeList() {
479 std::vector<unsigned> Result;
481 // Parse the first piece.
482 if (ParseRangePiece(Result))
483 return std::vector<unsigned>();
484 while (Lex.getCode() == tgtok::comma) {
485 Lex.Lex(); // Eat the comma.
487 // Parse the next range piece.
488 if (ParseRangePiece(Result))
489 return std::vector<unsigned>();
494 /// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
495 /// OptionalRangeList ::= '<' RangeList '>'
496 /// OptionalRangeList ::= /*empty*/
497 bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
498 if (Lex.getCode() != tgtok::less)
501 TGLoc StartLoc = Lex.getLoc();
502 Lex.Lex(); // eat the '<'
504 // Parse the range list.
505 Ranges = ParseRangeList();
506 if (Ranges.empty()) return true;
508 if (Lex.getCode() != tgtok::greater) {
509 TokError("expected '>' at end of range list");
510 return Error(StartLoc, "to match this '<'");
512 Lex.Lex(); // eat the '>'.
516 /// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
517 /// OptionalBitList ::= '{' RangeList '}'
518 /// OptionalBitList ::= /*empty*/
519 bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
520 if (Lex.getCode() != tgtok::l_brace)
523 TGLoc StartLoc = Lex.getLoc();
524 Lex.Lex(); // eat the '{'
526 // Parse the range list.
527 Ranges = ParseRangeList();
528 if (Ranges.empty()) return true;
530 if (Lex.getCode() != tgtok::r_brace) {
531 TokError("expected '}' at end of bit list");
532 return Error(StartLoc, "to match this '{'");
534 Lex.Lex(); // eat the '}'.
539 /// ParseType - Parse and return a tblgen type. This returns null on error.
541 /// Type ::= STRING // string type
542 /// Type ::= BIT // bit type
543 /// Type ::= BITS '<' INTVAL '>' // bits<x> type
544 /// Type ::= INT // int type
545 /// Type ::= LIST '<' Type '>' // list<x> type
546 /// Type ::= CODE // code type
547 /// Type ::= DAG // dag type
548 /// Type ::= ClassID // Record Type
550 RecTy *TGParser::ParseType() {
551 switch (Lex.getCode()) {
552 default: TokError("Unknown token when expecting a type"); return 0;
553 case tgtok::String: Lex.Lex(); return new StringRecTy();
554 case tgtok::Bit: Lex.Lex(); return new BitRecTy();
555 case tgtok::Int: Lex.Lex(); return new IntRecTy();
556 case tgtok::Code: Lex.Lex(); return new CodeRecTy();
557 case tgtok::Dag: Lex.Lex(); return new DagRecTy();
559 if (Record *R = ParseClassID()) return new RecordRecTy(R);
562 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
563 TokError("expected '<' after bits type");
566 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
567 TokError("expected integer in bits<n> type");
570 uint64_t Val = Lex.getCurIntVal();
571 if (Lex.Lex() != tgtok::greater) { // Eat count.
572 TokError("expected '>' at end of bits<n> type");
575 Lex.Lex(); // Eat '>'
576 return new BitsRecTy(Val);
579 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
580 TokError("expected '<' after list type");
583 Lex.Lex(); // Eat '<'
584 RecTy *SubType = ParseType();
585 if (SubType == 0) return 0;
587 if (Lex.getCode() != tgtok::greater) {
588 TokError("expected '>' at end of list<ty> type");
591 Lex.Lex(); // Eat '>'
592 return new ListRecTy(SubType);
597 /// ParseIDValue - Parse an ID as a value and decode what it means.
599 /// IDValue ::= ID [def local value]
600 /// IDValue ::= ID [def template arg]
601 /// IDValue ::= ID [multiclass local value]
602 /// IDValue ::= ID [multiclass template argument]
603 /// IDValue ::= ID [def name]
605 Init *TGParser::ParseIDValue(Record *CurRec) {
606 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
607 std::string Name = Lex.getCurStrVal();
608 TGLoc Loc = Lex.getLoc();
610 return ParseIDValue(CurRec, Name, Loc);
613 /// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
614 /// has already been read.
615 Init *TGParser::ParseIDValue(Record *CurRec,
616 const std::string &Name, TGLoc NameLoc) {
618 if (const RecordVal *RV = CurRec->getValue(Name))
619 return new VarInit(Name, RV->getType());
621 std::string TemplateArgName = CurRec->getName()+":"+Name;
622 if (CurRec->isTemplateArg(TemplateArgName)) {
623 const RecordVal *RV = CurRec->getValue(TemplateArgName);
624 assert(RV && "Template arg doesn't exist??");
625 return new VarInit(TemplateArgName, RV->getType());
630 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
631 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
632 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
633 assert(RV && "Template arg doesn't exist??");
634 return new VarInit(MCName, RV->getType());
638 if (Record *D = Records.getDef(Name))
639 return new DefInit(D);
641 Error(NameLoc, "Variable not defined: '" + Name + "'");
645 /// ParseSimpleValue - Parse a tblgen value. This returns null on error.
647 /// SimpleValue ::= IDValue
648 /// SimpleValue ::= INTVAL
649 /// SimpleValue ::= STRVAL+
650 /// SimpleValue ::= CODEFRAGMENT
651 /// SimpleValue ::= '?'
652 /// SimpleValue ::= '{' ValueList '}'
653 /// SimpleValue ::= ID '<' ValueListNE '>'
654 /// SimpleValue ::= '[' ValueList ']'
655 /// SimpleValue ::= '(' IDValue DagArgList ')'
656 /// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
657 /// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
658 /// SimpleValue ::= SRATOK '(' Value ',' Value ')'
659 /// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
660 /// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
662 Init *TGParser::ParseSimpleValue(Record *CurRec) {
664 switch (Lex.getCode()) {
665 default: TokError("Unknown token when parsing a value"); break;
666 case tgtok::IntVal: R = new IntInit(Lex.getCurIntVal()); Lex.Lex(); break;
667 case tgtok::StrVal: {
668 std::string Val = Lex.getCurStrVal();
671 // Handle multiple consecutive concatenated strings.
672 while (Lex.getCode() == tgtok::StrVal) {
673 Val += Lex.getCurStrVal();
677 R = new StringInit(Val);
680 case tgtok::CodeFragment:
681 R = new CodeInit(Lex.getCurStrVal()); Lex.Lex(); break;
682 case tgtok::question: R = new UnsetInit(); Lex.Lex(); break;
684 TGLoc NameLoc = Lex.getLoc();
685 std::string Name = Lex.getCurStrVal();
686 if (Lex.Lex() != tgtok::less) // consume the Id.
687 return ParseIDValue(CurRec, Name, NameLoc); // Value ::= IDValue
689 // Value ::= ID '<' ValueListNE '>'
690 if (Lex.Lex() == tgtok::greater) {
691 TokError("expected non-empty value list");
694 std::vector<Init*> ValueList = ParseValueList(CurRec);
695 if (ValueList.empty()) return 0;
697 if (Lex.getCode() != tgtok::greater) {
698 TokError("expected '>' at end of value list");
701 Lex.Lex(); // eat the '>'
703 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
704 // a new anonymous definition, deriving from CLASS<initvalslist> with no
706 Record *Class = Records.getClass(Name);
708 Error(NameLoc, "Expected a class name, got '" + Name + "'");
712 // Create the new record, set it as CurRec temporarily.
713 static unsigned AnonCounter = 0;
714 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),NameLoc);
715 SubClassReference SCRef;
716 SCRef.RefLoc = NameLoc;
718 SCRef.TemplateArgs = ValueList;
719 // Add info about the subclass to NewRec.
720 if (AddSubClass(NewRec, SCRef))
722 NewRec->resolveReferences();
723 Records.addDef(NewRec);
725 // The result of the expression is a reference to the new record.
726 return new DefInit(NewRec);
728 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
729 TGLoc BraceLoc = Lex.getLoc();
730 Lex.Lex(); // eat the '{'
731 std::vector<Init*> Vals;
733 if (Lex.getCode() != tgtok::r_brace) {
734 Vals = ParseValueList(CurRec);
735 if (Vals.empty()) return 0;
737 if (Lex.getCode() != tgtok::r_brace) {
738 TokError("expected '}' at end of bit list value");
741 Lex.Lex(); // eat the '}'
743 BitsInit *Result = new BitsInit(Vals.size());
744 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
745 Init *Bit = Vals[i]->convertInitializerTo(new BitRecTy());
747 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
748 ") is not convertable to a bit");
751 Result->setBit(Vals.size()-i-1, Bit);
755 case tgtok::l_square: { // Value ::= '[' ValueList ']'
756 Lex.Lex(); // eat the '['
757 std::vector<Init*> Vals;
759 if (Lex.getCode() != tgtok::r_square) {
760 Vals = ParseValueList(CurRec);
761 if (Vals.empty()) return 0;
763 if (Lex.getCode() != tgtok::r_square) {
764 TokError("expected ']' at end of list value");
767 Lex.Lex(); // eat the ']'
768 return new ListInit(Vals);
770 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
771 Lex.Lex(); // eat the '('
772 if (Lex.getCode() != tgtok::Id
773 && Lex.getCode() != tgtok::XNameConcat) {
774 TokError("expected identifier in dag init");
779 if (Lex.getCode() == tgtok::Id) {
780 Operator = ParseIDValue(CurRec);
781 if (Operator == 0) return 0;
784 BinOpInit::BinaryOp Code = BinOpInit::NAMECONCAT;
786 Lex.Lex(); // eat the operation
787 if (Lex.getCode() != tgtok::l_paren) {
788 TokError("expected '(' after binary operator");
791 Lex.Lex(); // eat the '('
793 Init *LHS = ParseValue(CurRec);
794 if (LHS == 0) return 0;
796 if (Lex.getCode() != tgtok::comma) {
797 TokError("expected ',' in binary operator");
800 Lex.Lex(); // eat the ','
802 Init *RHS = ParseValue(CurRec);
803 if (RHS == 0) return 0;
805 if (Lex.getCode() != tgtok::r_paren) {
806 TokError("expected ')' in binary operator");
809 Lex.Lex(); // eat the ')'
810 Operator = (new BinOpInit(Code, LHS, RHS))->Fold(CurRec, CurMultiClass);
813 // If the operator name is present, parse it.
814 std::string OperatorName;
815 if (Lex.getCode() == tgtok::colon) {
816 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
817 TokError("expected variable name in dag operator");
820 OperatorName = Lex.getCurStrVal();
821 Lex.Lex(); // eat the VarName.
824 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
825 if (Lex.getCode() != tgtok::r_paren) {
826 DagArgs = ParseDagArgList(CurRec);
827 if (DagArgs.empty()) return 0;
830 if (Lex.getCode() != tgtok::r_paren) {
831 TokError("expected ')' in dag init");
834 Lex.Lex(); // eat the ')'
836 return new DagInit(Operator, OperatorName, DagArgs);
842 case tgtok::XStrConcat:
843 case tgtok::XNameConcat: { // Value ::= !binop '(' Value ',' Value ')'
844 BinOpInit::BinaryOp Code;
845 switch (Lex.getCode()) {
846 default: assert(0 && "Unhandled code!");
847 case tgtok::XConcat: Code = BinOpInit::CONCAT; break;
848 case tgtok::XSRA: Code = BinOpInit::SRA; break;
849 case tgtok::XSRL: Code = BinOpInit::SRL; break;
850 case tgtok::XSHL: Code = BinOpInit::SHL; break;
851 case tgtok::XStrConcat: Code = BinOpInit::STRCONCAT; break;
852 case tgtok::XNameConcat: Code = BinOpInit::NAMECONCAT; break;
854 Lex.Lex(); // eat the operation
855 if (Lex.getCode() != tgtok::l_paren) {
856 TokError("expected '(' after binary operator");
859 Lex.Lex(); // eat the '('
861 Init *LHS = ParseValue(CurRec);
862 if (LHS == 0) return 0;
864 if (Lex.getCode() != tgtok::comma) {
865 TokError("expected ',' in binary operator");
868 Lex.Lex(); // eat the ','
870 Init *RHS = ParseValue(CurRec);
871 if (RHS == 0) return 0;
873 if (Lex.getCode() != tgtok::r_paren) {
874 TokError("expected ')' in binary operator");
877 Lex.Lex(); // eat the ')'
878 return (new BinOpInit(Code, LHS, RHS))->Fold(CurRec, CurMultiClass);
885 /// ParseValue - Parse a tblgen value. This returns null on error.
887 /// Value ::= SimpleValue ValueSuffix*
888 /// ValueSuffix ::= '{' BitList '}'
889 /// ValueSuffix ::= '[' BitList ']'
890 /// ValueSuffix ::= '.' ID
892 Init *TGParser::ParseValue(Record *CurRec) {
893 Init *Result = ParseSimpleValue(CurRec);
894 if (Result == 0) return 0;
896 // Parse the suffixes now if present.
898 switch (Lex.getCode()) {
899 default: return Result;
900 case tgtok::l_brace: {
901 TGLoc CurlyLoc = Lex.getLoc();
902 Lex.Lex(); // eat the '{'
903 std::vector<unsigned> Ranges = ParseRangeList();
904 if (Ranges.empty()) return 0;
906 // Reverse the bitlist.
907 std::reverse(Ranges.begin(), Ranges.end());
908 Result = Result->convertInitializerBitRange(Ranges);
910 Error(CurlyLoc, "Invalid bit range for value");
915 if (Lex.getCode() != tgtok::r_brace) {
916 TokError("expected '}' at end of bit range list");
922 case tgtok::l_square: {
923 TGLoc SquareLoc = Lex.getLoc();
924 Lex.Lex(); // eat the '['
925 std::vector<unsigned> Ranges = ParseRangeList();
926 if (Ranges.empty()) return 0;
928 Result = Result->convertInitListSlice(Ranges);
930 Error(SquareLoc, "Invalid range for list slice");
935 if (Lex.getCode() != tgtok::r_square) {
936 TokError("expected ']' at end of list slice");
943 if (Lex.Lex() != tgtok::Id) { // eat the .
944 TokError("expected field identifier after '.'");
947 if (!Result->getFieldType(Lex.getCurStrVal())) {
948 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
949 Result->getAsString() + "'");
952 Result = new FieldInit(Result, Lex.getCurStrVal());
953 Lex.Lex(); // eat field name
959 /// ParseDagArgList - Parse the argument list for a dag literal expression.
961 /// ParseDagArgList ::= Value (':' VARNAME)?
962 /// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
963 std::vector<std::pair<llvm::Init*, std::string> >
964 TGParser::ParseDagArgList(Record *CurRec) {
965 std::vector<std::pair<llvm::Init*, std::string> > Result;
968 Init *Val = ParseValue(CurRec);
969 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
971 // If the variable name is present, add it.
973 if (Lex.getCode() == tgtok::colon) {
974 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
975 TokError("expected variable name in dag literal");
976 return std::vector<std::pair<llvm::Init*, std::string> >();
978 VarName = Lex.getCurStrVal();
979 Lex.Lex(); // eat the VarName.
982 Result.push_back(std::make_pair(Val, VarName));
984 if (Lex.getCode() != tgtok::comma) break;
985 Lex.Lex(); // eat the ','
992 /// ParseValueList - Parse a comma separated list of values, returning them as a
993 /// vector. Note that this always expects to be able to parse at least one
994 /// value. It returns an empty list if this is not possible.
996 /// ValueList ::= Value (',' Value)
998 std::vector<Init*> TGParser::ParseValueList(Record *CurRec) {
999 std::vector<Init*> Result;
1000 Result.push_back(ParseValue(CurRec));
1001 if (Result.back() == 0) return std::vector<Init*>();
1003 while (Lex.getCode() == tgtok::comma) {
1004 Lex.Lex(); // Eat the comma
1006 Result.push_back(ParseValue(CurRec));
1007 if (Result.back() == 0) return std::vector<Init*>();
1015 /// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1016 /// empty string on error. This can happen in a number of different context's,
1017 /// including within a def or in the template args for a def (which which case
1018 /// CurRec will be non-null) and within the template args for a multiclass (in
1019 /// which case CurRec will be null, but CurMultiClass will be set). This can
1020 /// also happen within a def that is within a multiclass, which will set both
1021 /// CurRec and CurMultiClass.
1023 /// Declaration ::= FIELD? Type ID ('=' Value)?
1025 std::string TGParser::ParseDeclaration(Record *CurRec,
1026 bool ParsingTemplateArgs) {
1027 // Read the field prefix if present.
1028 bool HasField = Lex.getCode() == tgtok::Field;
1029 if (HasField) Lex.Lex();
1031 RecTy *Type = ParseType();
1032 if (Type == 0) return "";
1034 if (Lex.getCode() != tgtok::Id) {
1035 TokError("Expected identifier in declaration");
1039 TGLoc IdLoc = Lex.getLoc();
1040 std::string DeclName = Lex.getCurStrVal();
1043 if (ParsingTemplateArgs) {
1045 DeclName = CurRec->getName() + ":" + DeclName;
1047 assert(CurMultiClass);
1050 DeclName = CurMultiClass->Rec.getName() + "::" + DeclName;
1054 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
1057 // If a value is present, parse it.
1058 if (Lex.getCode() == tgtok::equal) {
1060 TGLoc ValLoc = Lex.getLoc();
1061 Init *Val = ParseValue(CurRec);
1063 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
1070 /// ParseTemplateArgList - Read a template argument list, which is a non-empty
1071 /// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1072 /// template args for a def, which may or may not be in a multiclass. If null,
1073 /// these are the template args for a multiclass.
1075 /// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
1077 bool TGParser::ParseTemplateArgList(Record *CurRec) {
1078 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1079 Lex.Lex(); // eat the '<'
1081 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
1083 // Read the first declaration.
1084 std::string TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1085 if (TemplArg.empty())
1088 TheRecToAddTo->addTemplateArg(TemplArg);
1090 while (Lex.getCode() == tgtok::comma) {
1091 Lex.Lex(); // eat the ','
1093 // Read the following declarations.
1094 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1095 if (TemplArg.empty())
1097 TheRecToAddTo->addTemplateArg(TemplArg);
1100 if (Lex.getCode() != tgtok::greater)
1101 return TokError("expected '>' at end of template argument list");
1102 Lex.Lex(); // eat the '>'.
1107 /// ParseBodyItem - Parse a single item at within the body of a def or class.
1109 /// BodyItem ::= Declaration ';'
1110 /// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1111 bool TGParser::ParseBodyItem(Record *CurRec) {
1112 if (Lex.getCode() != tgtok::Let) {
1113 if (ParseDeclaration(CurRec, false).empty())
1116 if (Lex.getCode() != tgtok::semi)
1117 return TokError("expected ';' after declaration");
1122 // LET ID OptionalRangeList '=' Value ';'
1123 if (Lex.Lex() != tgtok::Id)
1124 return TokError("expected field identifier after let");
1126 TGLoc IdLoc = Lex.getLoc();
1127 std::string FieldName = Lex.getCurStrVal();
1128 Lex.Lex(); // eat the field name.
1130 std::vector<unsigned> BitList;
1131 if (ParseOptionalBitList(BitList))
1133 std::reverse(BitList.begin(), BitList.end());
1135 if (Lex.getCode() != tgtok::equal)
1136 return TokError("expected '=' in let expression");
1137 Lex.Lex(); // eat the '='.
1139 Init *Val = ParseValue(CurRec);
1140 if (Val == 0) return true;
1142 if (Lex.getCode() != tgtok::semi)
1143 return TokError("expected ';' after let expression");
1146 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1149 /// ParseBody - Read the body of a class or def. Return true on error, false on
1153 /// Body ::= '{' BodyList '}'
1154 /// BodyList BodyItem*
1156 bool TGParser::ParseBody(Record *CurRec) {
1157 // If this is a null definition, just eat the semi and return.
1158 if (Lex.getCode() == tgtok::semi) {
1163 if (Lex.getCode() != tgtok::l_brace)
1164 return TokError("Expected ';' or '{' to start body");
1168 while (Lex.getCode() != tgtok::r_brace)
1169 if (ParseBodyItem(CurRec))
1177 /// ParseObjectBody - Parse the body of a def or class. This consists of an
1178 /// optional ClassList followed by a Body. CurRec is the current def or class
1179 /// that is being parsed.
1181 /// ObjectBody ::= BaseClassList Body
1182 /// BaseClassList ::= /*empty*/
1183 /// BaseClassList ::= ':' BaseClassListNE
1184 /// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1186 bool TGParser::ParseObjectBody(Record *CurRec) {
1187 // If there is a baseclass list, read it.
1188 if (Lex.getCode() == tgtok::colon) {
1191 // Read all of the subclasses.
1192 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1195 if (SubClass.Rec == 0) return true;
1198 if (AddSubClass(CurRec, SubClass))
1201 if (Lex.getCode() != tgtok::comma) break;
1202 Lex.Lex(); // eat ','.
1203 SubClass = ParseSubClassReference(CurRec, false);
1207 // Process any variables on the let stack.
1208 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1209 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1210 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1211 LetStack[i][j].Bits, LetStack[i][j].Value))
1214 return ParseBody(CurRec);
1218 /// ParseDef - Parse and return a top level or multiclass def, return the record
1219 /// corresponding to it. This returns null on error.
1221 /// DefInst ::= DEF ObjectName ObjectBody
1223 llvm::Record *TGParser::ParseDef(MultiClass *CurMultiClass) {
1224 TGLoc DefLoc = Lex.getLoc();
1225 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
1226 Lex.Lex(); // Eat the 'def' token.
1228 // Parse ObjectName and make a record for it.
1229 Record *CurRec = new Record(ParseObjectName(), DefLoc);
1231 if (!CurMultiClass) {
1232 // Top-level def definition.
1234 // Ensure redefinition doesn't happen.
1235 if (Records.getDef(CurRec->getName())) {
1236 Error(DefLoc, "def '" + CurRec->getName() + "' already defined");
1239 Records.addDef(CurRec);
1241 // Otherwise, a def inside a multiclass, add it to the multiclass.
1242 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
1243 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
1244 Error(DefLoc, "def '" + CurRec->getName() +
1245 "' already defined in this multiclass!");
1248 CurMultiClass->DefPrototypes.push_back(CurRec);
1251 if (ParseObjectBody(CurRec))
1254 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
1255 CurRec->resolveReferences();
1257 // If ObjectBody has template arguments, it's an error.
1258 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
1263 /// ParseClass - Parse a tblgen class definition.
1265 /// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
1267 bool TGParser::ParseClass() {
1268 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
1271 if (Lex.getCode() != tgtok::Id)
1272 return TokError("expected class name after 'class' keyword");
1274 Record *CurRec = Records.getClass(Lex.getCurStrVal());
1276 // If the body was previously defined, this is an error.
1277 if (!CurRec->getValues().empty() ||
1278 !CurRec->getSuperClasses().empty() ||
1279 !CurRec->getTemplateArgs().empty())
1280 return TokError("Class '" + CurRec->getName() + "' already defined");
1282 // If this is the first reference to this class, create and add it.
1283 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc());
1284 Records.addClass(CurRec);
1286 Lex.Lex(); // eat the name.
1288 // If there are template args, parse them.
1289 if (Lex.getCode() == tgtok::less)
1290 if (ParseTemplateArgList(CurRec))
1293 // Finally, parse the object body.
1294 return ParseObjectBody(CurRec);
1297 /// ParseLetList - Parse a non-empty list of assignment expressions into a list
1300 /// LetList ::= LetItem (',' LetItem)*
1301 /// LetItem ::= ID OptionalRangeList '=' Value
1303 std::vector<LetRecord> TGParser::ParseLetList() {
1304 std::vector<LetRecord> Result;
1307 if (Lex.getCode() != tgtok::Id) {
1308 TokError("expected identifier in let definition");
1309 return std::vector<LetRecord>();
1311 std::string Name = Lex.getCurStrVal();
1312 TGLoc NameLoc = Lex.getLoc();
1313 Lex.Lex(); // Eat the identifier.
1315 // Check for an optional RangeList.
1316 std::vector<unsigned> Bits;
1317 if (ParseOptionalRangeList(Bits))
1318 return std::vector<LetRecord>();
1319 std::reverse(Bits.begin(), Bits.end());
1321 if (Lex.getCode() != tgtok::equal) {
1322 TokError("expected '=' in let expression");
1323 return std::vector<LetRecord>();
1325 Lex.Lex(); // eat the '='.
1327 Init *Val = ParseValue(0);
1328 if (Val == 0) return std::vector<LetRecord>();
1330 // Now that we have everything, add the record.
1331 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
1333 if (Lex.getCode() != tgtok::comma)
1335 Lex.Lex(); // eat the comma.
1339 /// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
1340 /// different related productions.
1342 /// Object ::= LET LetList IN '{' ObjectList '}'
1343 /// Object ::= LET LetList IN Object
1345 bool TGParser::ParseTopLevelLet() {
1346 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
1349 // Add this entry to the let stack.
1350 std::vector<LetRecord> LetInfo = ParseLetList();
1351 if (LetInfo.empty()) return true;
1352 LetStack.push_back(LetInfo);
1354 if (Lex.getCode() != tgtok::In)
1355 return TokError("expected 'in' at end of top-level 'let'");
1358 // If this is a scalar let, just handle it now
1359 if (Lex.getCode() != tgtok::l_brace) {
1360 // LET LetList IN Object
1363 } else { // Object ::= LETCommand '{' ObjectList '}'
1364 TGLoc BraceLoc = Lex.getLoc();
1365 // Otherwise, this is a group let.
1366 Lex.Lex(); // eat the '{'.
1368 // Parse the object list.
1369 if (ParseObjectList())
1372 if (Lex.getCode() != tgtok::r_brace) {
1373 TokError("expected '}' at end of top level let command");
1374 return Error(BraceLoc, "to match this '{'");
1379 // Outside this let scope, this let block is not active.
1380 LetStack.pop_back();
1384 /// ParseMultiClassDef - Parse a def in a multiclass context.
1386 /// MultiClassDef ::= DefInst
1388 bool TGParser::ParseMultiClassDef(MultiClass *CurMC) {
1389 if (Lex.getCode() != tgtok::Def)
1390 return TokError("expected 'def' in multiclass body");
1392 Record *D = ParseDef(CurMC);
1393 if (D == 0) return true;
1395 // Copy the template arguments for the multiclass into the def.
1396 const std::vector<std::string> &TArgs = CurMC->Rec.getTemplateArgs();
1398 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1399 const RecordVal *RV = CurMC->Rec.getValue(TArgs[i]);
1400 assert(RV && "Template arg doesn't exist?");
1407 /// ParseMultiClass - Parse a multiclass definition.
1409 /// MultiClassInst ::= MULTICLASS ID TemplateArgList? ':' BaseMultiClassList '{' MultiClassDef+ '}'
1411 bool TGParser::ParseMultiClass() {
1412 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
1413 Lex.Lex(); // Eat the multiclass token.
1415 if (Lex.getCode() != tgtok::Id)
1416 return TokError("expected identifier after multiclass for name");
1417 std::string Name = Lex.getCurStrVal();
1419 if (MultiClasses.count(Name))
1420 return TokError("multiclass '" + Name + "' already defined");
1422 CurMultiClass = MultiClasses[Name] = new MultiClass(Name, Lex.getLoc());
1423 Lex.Lex(); // Eat the identifier.
1425 // If there are template args, parse them.
1426 if (Lex.getCode() == tgtok::less)
1427 if (ParseTemplateArgList(0))
1430 // If there are submulticlasses, parse them.
1431 if (Lex.getCode() == tgtok::colon) {
1434 // Read all of the submulticlasses.
1435 SubMultiClassReference SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
1438 if (SubMultiClass.MC == 0) return true;
1441 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
1444 if (Lex.getCode() != tgtok::comma) break;
1445 Lex.Lex(); // eat ','.
1446 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
1450 if (Lex.getCode() != tgtok::l_brace)
1451 return TokError("expected '{' in multiclass definition");
1453 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
1454 return TokError("multiclass must contain at least one def");
1456 while (Lex.getCode() != tgtok::r_brace)
1457 if (ParseMultiClassDef(CurMultiClass))
1460 Lex.Lex(); // eat the '}'.
1466 /// ParseDefm - Parse the instantiation of a multiclass.
1468 /// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
1470 bool TGParser::ParseDefm() {
1471 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
1472 if (Lex.Lex() != tgtok::Id) // eat the defm.
1473 return TokError("expected identifier after defm");
1475 TGLoc DefmPrefixLoc = Lex.getLoc();
1476 std::string DefmPrefix = Lex.getCurStrVal();
1477 if (Lex.Lex() != tgtok::colon)
1478 return TokError("expected ':' after defm identifier");
1483 TGLoc SubClassLoc = Lex.getLoc();
1484 SubClassReference Ref = ParseSubClassReference(0, true);
1485 if (Ref.Rec == 0) return true;
1487 if (Lex.getCode() != tgtok::semi)
1488 return TokError("expected ';' at end of defm");
1491 // To instantiate a multiclass, we need to first get the multiclass, then
1492 // instantiate each def contained in the multiclass with the SubClassRef
1493 // template parameters.
1494 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
1495 assert(MC && "Didn't lookup multiclass correctly?");
1496 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
1498 // Verify that the correct number of template arguments were specified.
1499 const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs();
1500 if (TArgs.size() < TemplateVals.size())
1501 return Error(SubClassLoc,
1502 "more template args specified than multiclass expects");
1504 // Loop over all the def's in the multiclass, instantiating each one.
1505 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
1506 Record *DefProto = MC->DefPrototypes[i];
1508 // Add the suffix to the defm name to get the new name.
1509 Record *CurRec = new Record(DefmPrefix + DefProto->getName(),DefmPrefixLoc);
1511 SubClassReference Ref;
1512 Ref.RefLoc = DefmPrefixLoc;
1514 AddSubClass(CurRec, Ref);
1516 // Loop over all of the template arguments, setting them to the specified
1517 // value or leaving them as the default if necessary.
1518 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1519 if (i < TemplateVals.size()) { // A value is specified for this temp-arg?
1521 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
1526 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
1529 CurRec->removeValue(TArgs[i]);
1531 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
1532 return Error(SubClassLoc, "value not specified for template argument #"+
1533 utostr(i) + " (" + TArgs[i] + ") of multiclassclass '" +
1534 MC->Rec.getName() + "'");
1538 // If the mdef is inside a 'let' expression, add to each def.
1539 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1540 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1541 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1542 LetStack[i][j].Bits, LetStack[i][j].Value)) {
1543 Error(DefmPrefixLoc, "when instantiating this defm");
1548 // Ensure redefinition doesn't happen.
1549 if (Records.getDef(CurRec->getName()))
1550 return Error(DefmPrefixLoc, "def '" + CurRec->getName() +
1551 "' already defined, instantiating defm with subdef '" +
1552 DefProto->getName() + "'");
1553 Records.addDef(CurRec);
1554 CurRec->resolveReferences();
1561 /// Object ::= ClassInst
1562 /// Object ::= DefInst
1563 /// Object ::= MultiClassInst
1564 /// Object ::= DefMInst
1565 /// Object ::= LETCommand '{' ObjectList '}'
1566 /// Object ::= LETCommand Object
1567 bool TGParser::ParseObject() {
1568 switch (Lex.getCode()) {
1569 default: assert(0 && "This is not an object");
1570 case tgtok::Let: return ParseTopLevelLet();
1571 case tgtok::Def: return ParseDef(0) == 0;
1572 case tgtok::Defm: return ParseDefm();
1573 case tgtok::Class: return ParseClass();
1574 case tgtok::MultiClass: return ParseMultiClass();
1579 /// ObjectList :== Object*
1580 bool TGParser::ParseObjectList() {
1581 while (isObjectStart(Lex.getCode())) {
1589 bool TGParser::ParseFile() {
1590 Lex.Lex(); // Prime the lexer.
1591 if (ParseObjectList()) return true;
1593 // If we have unread input at the end of the file, report it.
1594 if (Lex.getCode() == tgtok::Eof)
1597 return TokError("Unexpected input at top level");