resolve the last fixme's in the new tblgen parser.
[oota-llvm.git] / utils / TableGen / TGParser.cpp
1 //===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Implement the Parser for TableGen.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "TGParser.h"
15 #include "Record.h"
16 #include "llvm/ADT/StringExtras.h"
17 using namespace llvm;
18
19 //===----------------------------------------------------------------------===//
20 // Support Code for the Semantic Actions.
21 //===----------------------------------------------------------------------===//
22
23 namespace llvm {
24 struct MultiClass {
25   Record Rec;  // Placeholder for template args and Name.
26   std::vector<Record*> DefPrototypes;
27     
28   MultiClass(const std::string &Name) : Rec(Name) {}
29 };
30   
31 struct SubClassReference {
32   TGParser::LocTy RefLoc;
33   Record *Rec;
34   std::vector<Init*> TemplateArgs;
35   SubClassReference() : RefLoc(0), Rec(0) {}
36   
37   bool isInvalid() const { return Rec == 0; }
38 };
39   
40 } // end namespace llvm
41
42 bool TGParser::AddValue(Record *CurRec, LocTy Loc, const RecordVal &RV) {
43   if (CurRec == 0)
44     CurRec = &CurMultiClass->Rec;
45   
46   if (RecordVal *ERV = CurRec->getValue(RV.getName())) {
47     // The value already exists in the class, treat this as a set.
48     if (ERV->setValue(RV.getValue()))
49       return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
50                    RV.getType()->getAsString() + "' is incompatible with " +
51                    "previous definition of type '" + 
52                    ERV->getType()->getAsString() + "'");
53   } else {
54     CurRec->addValue(RV);
55   }
56   return false;
57 }
58
59 /// SetValue -
60 /// Return true on error, false on success.
61 bool TGParser::SetValue(Record *CurRec, LocTy Loc, const std::string &ValName, 
62                         const std::vector<unsigned> &BitList, Init *V) {
63   if (!V) return false;
64
65   if (CurRec == 0) CurRec = &CurMultiClass->Rec;
66
67   RecordVal *RV = CurRec->getValue(ValName);
68   if (RV == 0)
69     return Error(Loc, "Value '" + ValName + "' unknown!");
70
71   // Do not allow assignments like 'X = X'.  This will just cause infinite loops
72   // in the resolution machinery.
73   if (BitList.empty())
74     if (VarInit *VI = dynamic_cast<VarInit*>(V))
75       if (VI->getName() == ValName)
76         return false;
77   
78   // If we are assigning to a subset of the bits in the value... then we must be
79   // assigning to a field of BitsRecTy, which must have a BitsInit
80   // initializer.
81   //
82   if (!BitList.empty()) {
83     BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
84     if (CurVal == 0)
85       return Error(Loc, "Value '" + ValName + "' is not a bits type");
86
87     // Convert the incoming value to a bits type of the appropriate size...
88     Init *BI = V->convertInitializerTo(new BitsRecTy(BitList.size()));
89     if (BI == 0) {
90       V->convertInitializerTo(new BitsRecTy(BitList.size()));
91       return Error(Loc, "Initializer is not compatible with bit range");
92     }
93                    
94     // We should have a BitsInit type now.
95     BitsInit *BInit = dynamic_cast<BitsInit*>(BI);
96     assert(BInit != 0);
97
98     BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
99
100     // Loop over bits, assigning values as appropriate.
101     for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
102       unsigned Bit = BitList[i];
103       if (NewVal->getBit(Bit))
104         return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
105                      ValName + "' more than once");
106       NewVal->setBit(Bit, BInit->getBit(i));
107     }
108
109     for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
110       if (NewVal->getBit(i) == 0)
111         NewVal->setBit(i, CurVal->getBit(i));
112
113     V = NewVal;
114   }
115
116   if (RV->setValue(V))
117    return Error(Loc, "Value '" + ValName + "' of type '" + 
118                 RV->getType()->getAsString() + 
119                 "' is incompatible with initializer '" + V->getAsString() +"'");
120   return false;
121 }
122
123 /// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
124 /// args as SubClass's template arguments.
125 bool TGParser::AddSubClass(Record *CurRec, class SubClassReference &SubClass) {
126   Record *SC = SubClass.Rec;
127   // Add all of the values in the subclass into the current class.
128   const std::vector<RecordVal> &Vals = SC->getValues();
129   for (unsigned i = 0, e = Vals.size(); i != e; ++i)
130     if (AddValue(CurRec, SubClass.RefLoc, Vals[i]))
131       return true;
132
133   const std::vector<std::string> &TArgs = SC->getTemplateArgs();
134
135   // Ensure that an appropriate number of template arguments are specified.
136   if (TArgs.size() < SubClass.TemplateArgs.size())
137     return Error(SubClass.RefLoc, "More template args specified than expected");
138   
139   // Loop over all of the template arguments, setting them to the specified
140   // value or leaving them as the default if necessary.
141   for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
142     if (i < SubClass.TemplateArgs.size()) {
143       // If a value is specified for this template arg, set it now.
144       if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(), 
145                    SubClass.TemplateArgs[i]))
146         return true;
147       
148       // Resolve it next.
149       CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
150       
151       // Now remove it.
152       CurRec->removeValue(TArgs[i]);
153
154     } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
155       return Error(SubClass.RefLoc,"Value not specified for template argument #"
156                    + utostr(i) + " (" + TArgs[i] + ") of subclass '" + 
157                    SC->getName() + "'!");
158     }
159   }
160
161   // Since everything went well, we can now set the "superclass" list for the
162   // current record.
163   const std::vector<Record*> &SCs = SC->getSuperClasses();
164   for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
165     if (CurRec->isSubClassOf(SCs[i]))
166       return Error(SubClass.RefLoc,
167                    "Already subclass of '" + SCs[i]->getName() + "'!\n");
168     CurRec->addSuperClass(SCs[i]);
169   }
170   
171   if (CurRec->isSubClassOf(SC))
172     return Error(SubClass.RefLoc,
173                  "Already subclass of '" + SC->getName() + "'!\n");
174   CurRec->addSuperClass(SC);
175   return false;
176 }
177
178 //===----------------------------------------------------------------------===//
179 // Parser Code
180 //===----------------------------------------------------------------------===//
181
182 /// isObjectStart - Return true if this is a valid first token for an Object.
183 static bool isObjectStart(tgtok::TokKind K) {
184   return K == tgtok::Class || K == tgtok::Def ||
185          K == tgtok::Defm || K == tgtok::Let || K == tgtok::MultiClass; 
186 }
187
188 /// ParseObjectName - If an object name is specified, return it.  Otherwise,
189 /// return an anonymous name.
190 ///   ObjectName ::= ID
191 ///   ObjectName ::= /*empty*/
192 ///
193 std::string TGParser::ParseObjectName() {
194   if (Lex.getCode() == tgtok::Id) {
195     std::string Ret = Lex.getCurStrVal();
196     Lex.Lex();
197     return Ret;
198   }
199   
200   static unsigned AnonCounter = 0;
201   return "anonymous."+utostr(AnonCounter++);
202 }
203
204
205 /// ParseClassID - Parse and resolve a reference to a class name.  This returns
206 /// null on error.
207 ///
208 ///    ClassID ::= ID
209 ///
210 Record *TGParser::ParseClassID() {
211   if (Lex.getCode() != tgtok::Id) {
212     TokError("expected name for ClassID");
213     return 0;
214   }
215   
216   Record *Result = Records.getClass(Lex.getCurStrVal());
217   if (Result == 0)
218     TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
219   
220   Lex.Lex();
221   return Result;
222 }
223
224 Record *TGParser::ParseDefmID() {
225   if (Lex.getCode() != tgtok::Id) {
226     TokError("expected multiclass name");
227     return 0;
228   }
229   
230   MultiClass *MC = MultiClasses[Lex.getCurStrVal()];
231   if (MC == 0) {
232     TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
233     return 0;
234   }
235   
236   Lex.Lex();
237   return &MC->Rec;
238 }  
239
240
241
242 /// ParseSubClassReference - Parse a reference to a subclass or to a templated
243 /// subclass.  This returns a SubClassRefTy with a null Record* on error.
244 ///
245 ///  SubClassRef ::= ClassID
246 ///  SubClassRef ::= ClassID '<' ValueList '>'
247 ///
248 SubClassReference TGParser::
249 ParseSubClassReference(Record *CurRec, bool isDefm) {
250   SubClassReference Result;
251   Result.RefLoc = Lex.getLoc();
252   
253   if (isDefm)
254     Result.Rec = ParseDefmID();
255   else
256     Result.Rec = ParseClassID();
257   if (Result.Rec == 0) return Result;
258   
259   // If there is no template arg list, we're done.
260   if (Lex.getCode() != tgtok::less)
261     return Result;
262   Lex.Lex();  // Eat the '<'
263   
264   if (Lex.getCode() == tgtok::greater) {
265     TokError("subclass reference requires a non-empty list of template values");
266     Result.Rec = 0;
267     return Result;
268   }
269   
270   Result.TemplateArgs = ParseValueList(CurRec);
271   if (Result.TemplateArgs.empty()) {
272     Result.Rec = 0;   // Error parsing value list.
273     return Result;
274   }
275     
276   if (Lex.getCode() != tgtok::greater) {
277     TokError("expected '>' in template value list");
278     Result.Rec = 0;
279     return Result;
280   }
281   Lex.Lex();
282   
283   return Result;
284 }
285
286 /// ParseRangePiece - Parse a bit/value range.
287 ///   RangePiece ::= INTVAL
288 ///   RangePiece ::= INTVAL '-' INTVAL
289 ///   RangePiece ::= INTVAL INTVAL
290 bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
291   assert(Lex.getCode() == tgtok::IntVal && "Invalid range");
292   int Start = Lex.getCurIntVal();
293   int End;
294   
295   if (Start < 0)
296     return TokError("invalid range, cannot be negative");
297   
298   switch (Lex.Lex()) {  // eat first character.
299   default: 
300     Ranges.push_back(Start);
301     return false;
302   case tgtok::minus:
303     if (Lex.Lex() != tgtok::IntVal) {
304       TokError("expected integer value as end of range");
305       return true;
306     }
307     End = Lex.getCurIntVal();
308     break;
309   case tgtok::IntVal:
310     End = -Lex.getCurIntVal();
311     break;
312   }
313   if (End < 0) 
314     return TokError("invalid range, cannot be negative");
315   Lex.Lex();
316   
317   // Add to the range.
318   if (Start < End) {
319     for (; Start <= End; ++Start)
320       Ranges.push_back(Start);
321   } else {
322     for (; Start >= End; --Start)
323       Ranges.push_back(Start);
324   }
325   return false;
326 }
327
328 /// ParseRangeList - Parse a list of scalars and ranges into scalar values.
329 ///
330 ///   RangeList ::= RangePiece (',' RangePiece)*
331 ///
332 std::vector<unsigned> TGParser::ParseRangeList() {
333   std::vector<unsigned> Result;
334   
335   // Parse the first piece.
336   if (ParseRangePiece(Result))
337     return std::vector<unsigned>();
338   while (Lex.getCode() == tgtok::comma) {
339     Lex.Lex();  // Eat the comma.
340
341     // Parse the next range piece.
342     if (ParseRangePiece(Result))
343       return std::vector<unsigned>();
344   }
345   return Result;
346 }
347
348 /// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
349 ///   OptionalRangeList ::= '<' RangeList '>'
350 ///   OptionalRangeList ::= /*empty*/
351 bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
352   if (Lex.getCode() != tgtok::less)
353     return false;
354   
355   LocTy StartLoc = Lex.getLoc();
356   Lex.Lex(); // eat the '<'
357   
358   // Parse the range list.
359   Ranges = ParseRangeList();
360   if (Ranges.empty()) return true;
361   
362   if (Lex.getCode() != tgtok::greater) {
363     TokError("expected '>' at end of range list");
364     return Error(StartLoc, "to match this '<'");
365   }
366   Lex.Lex();   // eat the '>'.
367   return false;
368 }
369
370 /// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
371 ///   OptionalBitList ::= '{' RangeList '}'
372 ///   OptionalBitList ::= /*empty*/
373 bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
374   if (Lex.getCode() != tgtok::l_brace)
375     return false;
376   
377   LocTy StartLoc = Lex.getLoc();
378   Lex.Lex(); // eat the '{'
379   
380   // Parse the range list.
381   Ranges = ParseRangeList();
382   if (Ranges.empty()) return true;
383   
384   if (Lex.getCode() != tgtok::r_brace) {
385     TokError("expected '}' at end of bit list");
386     return Error(StartLoc, "to match this '{'");
387   }
388   Lex.Lex();   // eat the '}'.
389   return false;
390 }
391
392
393 /// ParseType - Parse and return a tblgen type.  This returns null on error.
394 ///
395 ///   Type ::= STRING                       // string type
396 ///   Type ::= BIT                          // bit type
397 ///   Type ::= BITS '<' INTVAL '>'          // bits<x> type
398 ///   Type ::= INT                          // int type
399 ///   Type ::= LIST '<' Type '>'            // list<x> type
400 ///   Type ::= CODE                         // code type
401 ///   Type ::= DAG                          // dag type
402 ///   Type ::= ClassID                      // Record Type
403 ///
404 RecTy *TGParser::ParseType() {
405   switch (Lex.getCode()) {
406   default: TokError("Unknown token when expecting a type"); return 0;
407   case tgtok::String: Lex.Lex(); return new StringRecTy();
408   case tgtok::Bit:    Lex.Lex(); return new BitRecTy();
409   case tgtok::Int:    Lex.Lex(); return new IntRecTy();
410   case tgtok::Code:   Lex.Lex(); return new CodeRecTy();
411   case tgtok::Dag:    Lex.Lex(); return new DagRecTy();
412   case tgtok::Id:
413     if (Record *R = ParseClassID()) return new RecordRecTy(R);
414     return 0;
415   case tgtok::Bits: {
416     if (Lex.Lex() != tgtok::less) { // Eat 'bits'
417       TokError("expected '<' after bits type");
418       return 0;
419     }
420     if (Lex.Lex() != tgtok::IntVal) {  // Eat '<'
421       TokError("expected integer in bits<n> type");
422       return 0;
423     }
424     unsigned Val = Lex.getCurIntVal();
425     if (Lex.Lex() != tgtok::greater) {  // Eat count.
426       TokError("expected '>' at end of bits<n> type");
427       return 0;
428     }
429     Lex.Lex();  // Eat '>'
430     return new BitsRecTy(Val);
431   }
432   case tgtok::List: {
433     if (Lex.Lex() != tgtok::less) { // Eat 'bits'
434       TokError("expected '<' after list type");
435       return 0;
436     }
437     Lex.Lex();  // Eat '<'
438     RecTy *SubType = ParseType();
439     if (SubType == 0) return 0;
440     
441     if (Lex.getCode() != tgtok::greater) {
442       TokError("expected '>' at end of list<ty> type");
443       return 0;
444     }
445     Lex.Lex();  // Eat '>'
446     return new ListRecTy(SubType);
447   }
448   }      
449 }
450
451 /// ParseIDValue - Parse an ID as a value and decode what it means.
452 ///
453 ///  IDValue ::= ID [def local value]
454 ///  IDValue ::= ID [def template arg]
455 ///  IDValue ::= ID [multiclass local value]
456 ///  IDValue ::= ID [multiclass template argument]
457 ///  IDValue ::= ID [def name]
458 ///
459 Init *TGParser::ParseIDValue(Record *CurRec) {
460   assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
461   std::string Name = Lex.getCurStrVal();
462   LocTy Loc = Lex.getLoc();
463   Lex.Lex();
464   return ParseIDValue(CurRec, Name, Loc);
465 }
466
467 /// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
468 /// has already been read.
469 Init *TGParser::ParseIDValue(Record *CurRec, 
470                              const std::string &Name, LocTy NameLoc) {
471   if (CurRec) {
472     if (const RecordVal *RV = CurRec->getValue(Name))
473       return new VarInit(Name, RV->getType());
474     
475     std::string TemplateArgName = CurRec->getName()+":"+Name;
476     if (CurRec->isTemplateArg(TemplateArgName)) {
477       const RecordVal *RV = CurRec->getValue(TemplateArgName);
478       assert(RV && "Template arg doesn't exist??");
479       return new VarInit(TemplateArgName, RV->getType());
480     }
481   }
482   
483   if (CurMultiClass) {
484     std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
485     if (CurMultiClass->Rec.isTemplateArg(MCName)) {
486       const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
487       assert(RV && "Template arg doesn't exist??");
488       return new VarInit(MCName, RV->getType());
489     }
490   }
491   
492   if (Record *D = Records.getDef(Name))
493     return new DefInit(D);
494
495   Error(NameLoc, "Variable not defined: '" + Name + "'");
496   return 0;
497 }
498
499 /// ParseSimpleValue - Parse a tblgen value.  This returns null on error.
500 ///
501 ///   SimpleValue ::= IDValue
502 ///   SimpleValue ::= INTVAL
503 ///   SimpleValue ::= STRVAL
504 ///   SimpleValue ::= CODEFRAGMENT
505 ///   SimpleValue ::= '?'
506 ///   SimpleValue ::= '{' ValueList '}'
507 ///   SimpleValue ::= ID '<' ValueListNE '>'
508 ///   SimpleValue ::= '[' ValueList ']'
509 ///   SimpleValue ::= '(' IDValue DagArgList ')'
510 ///   SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
511 ///   SimpleValue ::= SHLTOK '(' Value ',' Value ')'
512 ///   SimpleValue ::= SRATOK '(' Value ',' Value ')'
513 ///   SimpleValue ::= SRLTOK '(' Value ',' Value ')'
514 ///   SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
515 ///
516 Init *TGParser::ParseSimpleValue(Record *CurRec) {
517   Init *R = 0;
518   switch (Lex.getCode()) {
519   default: TokError("Unknown token when parsing a value"); break;
520   case tgtok::IntVal: R = new IntInit(Lex.getCurIntVal()); Lex.Lex(); break;
521   case tgtok::StrVal: R = new StringInit(Lex.getCurStrVal()); Lex.Lex(); break;
522   case tgtok::CodeFragment:
523       R = new CodeInit(Lex.getCurStrVal()); Lex.Lex(); break;
524   case tgtok::question: R = new UnsetInit(); Lex.Lex(); break;
525   case tgtok::Id: {
526     LocTy NameLoc = Lex.getLoc();
527     std::string Name = Lex.getCurStrVal();
528     if (Lex.Lex() != tgtok::less)  // consume the Id.
529       return ParseIDValue(CurRec, Name, NameLoc);    // Value ::= IDValue
530     
531     // Value ::= ID '<' ValueListNE '>'
532     if (Lex.Lex() == tgtok::greater) {
533       TokError("expected non-empty value list");
534       return 0;
535     }
536     std::vector<Init*> ValueList = ParseValueList(CurRec);
537     if (ValueList.empty()) return 0;
538     
539     if (Lex.getCode() != tgtok::greater) {
540       TokError("expected '>' at end of value list");
541       return 0;
542     }
543     Lex.Lex();  // eat the '>'
544     
545     // This is a CLASS<initvalslist> expression.  This is supposed to synthesize
546     // a new anonymous definition, deriving from CLASS<initvalslist> with no
547     // body.
548     Record *Class = Records.getClass(Name);
549     if (!Class) {
550       Error(NameLoc, "Expected a class name, got '" + Name + "'");
551       return 0;
552     }
553     
554     // Create the new record, set it as CurRec temporarily.
555     static unsigned AnonCounter = 0;
556     Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++));
557     SubClassReference SCRef;
558     SCRef.RefLoc = NameLoc;
559     SCRef.Rec = Class;
560     SCRef.TemplateArgs = ValueList;
561     // Add info about the subclass to NewRec.
562     if (AddSubClass(NewRec, SCRef))
563       return 0;
564     NewRec->resolveReferences();
565     Records.addDef(NewRec);
566     
567     // The result of the expression is a reference to the new record.
568     return new DefInit(NewRec);
569   }    
570   case tgtok::l_brace: {           // Value ::= '{' ValueList '}'
571     LocTy BraceLoc = Lex.getLoc();
572     Lex.Lex(); // eat the '{'
573     std::vector<Init*> Vals;
574     
575     if (Lex.getCode() != tgtok::r_brace) {
576       Vals = ParseValueList(CurRec);
577       if (Vals.empty()) return 0;
578     }
579     if (Lex.getCode() != tgtok::r_brace) {
580       TokError("expected '}' at end of bit list value");
581       return 0;
582     }
583     Lex.Lex();  // eat the '}'
584     
585     BitsInit *Result = new BitsInit(Vals.size());
586     for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
587       Init *Bit = Vals[i]->convertInitializerTo(new BitRecTy());
588       if (Bit == 0) {
589         Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
590               ") is not convertable to a bit");
591         return 0;
592       }
593       Result->setBit(Vals.size()-i-1, Bit);
594     }
595     return Result;
596   }
597   case tgtok::l_square: {          // Value ::= '[' ValueList ']'
598     Lex.Lex(); // eat the '['
599     std::vector<Init*> Vals;
600     
601     if (Lex.getCode() != tgtok::r_square) {
602       Vals = ParseValueList(CurRec);
603       if (Vals.empty()) return 0;
604     }
605     if (Lex.getCode() != tgtok::r_square) {
606       TokError("expected ']' at end of list value");
607       return 0;
608     }
609     Lex.Lex();  // eat the ']'
610     return new ListInit(Vals);
611   }
612   case tgtok::l_paren: {         // Value ::= '(' IDValue DagArgList ')'
613     Lex.Lex();   // eat the '('
614     Init *Operator = ParseIDValue(CurRec);
615     if (Operator == 0) return 0;
616     
617     std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
618     if (Lex.getCode() != tgtok::r_paren) {
619       DagArgs = ParseDagArgList(CurRec);
620       if (DagArgs.empty()) return 0;
621     }
622     
623     if (Lex.getCode() != tgtok::r_paren) {
624       TokError("expected ')' in dag init");
625       return 0;
626     }
627     Lex.Lex();  // eat the ')'
628     
629     return new DagInit(Operator, DagArgs);
630   }
631   case tgtok::XConcat:
632   case tgtok::XSRA: 
633   case tgtok::XSRL:
634   case tgtok::XSHL:
635   case tgtok::XStrConcat: {  // Value ::= !binop '(' Value ',' Value ')'
636     BinOpInit::BinaryOp Code;
637     switch (Lex.getCode()) {
638     default: assert(0 && "Unhandled code!");
639     case tgtok::XConcat:    Code = BinOpInit::CONCAT; break;
640     case tgtok::XSRA:       Code = BinOpInit::SRA; break;
641     case tgtok::XSRL:       Code = BinOpInit::SRL; break;
642     case tgtok::XSHL:       Code = BinOpInit::SHL; break;
643     case tgtok::XStrConcat: Code = BinOpInit::STRCONCAT; break;
644     }
645     Lex.Lex();  // eat the operation
646     if (Lex.getCode() != tgtok::l_paren) {
647       TokError("expected '(' after binary operator");
648       return 0;
649     }
650     Lex.Lex();  // eat the '('
651     
652     Init *LHS = ParseValue(CurRec);
653     if (LHS == 0) return 0;
654
655     if (Lex.getCode() != tgtok::comma) {
656       TokError("expected ',' in binary operator");
657       return 0;
658     }
659     Lex.Lex();  // eat the ','
660     
661     Init *RHS = ParseValue(CurRec);
662     if (RHS == 0) return 0;
663
664     if (Lex.getCode() != tgtok::r_paren) {
665       TokError("expected ')' in binary operator");
666       return 0;
667     }
668     Lex.Lex();  // eat the ')'
669     return (new BinOpInit(Code, LHS, RHS))->Fold();
670   }
671   }
672   
673   return R;
674 }
675
676 /// ParseValue - Parse a tblgen value.  This returns null on error.
677 ///
678 ///   Value       ::= SimpleValue ValueSuffix*
679 ///   ValueSuffix ::= '{' BitList '}'
680 ///   ValueSuffix ::= '[' BitList ']'
681 ///   ValueSuffix ::= '.' ID
682 ///
683 Init *TGParser::ParseValue(Record *CurRec) {
684   Init *Result = ParseSimpleValue(CurRec);
685   if (Result == 0) return 0;
686   
687   // Parse the suffixes now if present.
688   while (1) {
689     switch (Lex.getCode()) {
690     default: return Result;
691     case tgtok::l_brace: {
692       LocTy CurlyLoc = Lex.getLoc();
693       Lex.Lex(); // eat the '{'
694       std::vector<unsigned> Ranges = ParseRangeList();
695       if (Ranges.empty()) return 0;
696       
697       // Reverse the bitlist.
698       std::reverse(Ranges.begin(), Ranges.end());
699       Result = Result->convertInitializerBitRange(Ranges);
700       if (Result == 0) {
701         Error(CurlyLoc, "Invalid bit range for value");
702         return 0;
703       }
704       
705       // Eat the '}'.
706       if (Lex.getCode() != tgtok::r_brace) {
707         TokError("expected '}' at end of bit range list");
708         return 0;
709       }
710       Lex.Lex();
711       break;
712     }
713     case tgtok::l_square: {
714       LocTy SquareLoc = Lex.getLoc();
715       Lex.Lex(); // eat the '['
716       std::vector<unsigned> Ranges = ParseRangeList();
717       if (Ranges.empty()) return 0;
718       
719       Result = Result->convertInitListSlice(Ranges);
720       if (Result == 0) {
721         Error(SquareLoc, "Invalid range for list slice");
722         return 0;
723       }
724       
725       // Eat the ']'.
726       if (Lex.getCode() != tgtok::r_square) {
727         TokError("expected ']' at end of list slice");
728         return 0;
729       }
730       Lex.Lex();
731       break;
732     }
733     case tgtok::period:
734       if (Lex.Lex() != tgtok::Id) {  // eat the .
735         TokError("expected field identifier after '.'");
736         return 0;
737       }
738       if (!Result->getFieldType(Lex.getCurStrVal())) {
739         TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
740                  Result->getAsString() + "'");
741         return 0;
742       }
743       Result = new FieldInit(Result, Lex.getCurStrVal());
744       Lex.Lex();  // eat field name
745       break;
746     }
747   }
748 }
749
750 /// ParseDagArgList - Parse the argument list for a dag literal expression.
751 ///
752 ///    ParseDagArgList ::= Value (':' VARNAME)?
753 ///    ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
754 std::vector<std::pair<llvm::Init*, std::string> > 
755 TGParser::ParseDagArgList(Record *CurRec) {
756   std::vector<std::pair<llvm::Init*, std::string> > Result;
757   
758   while (1) {
759     Init *Val = ParseValue(CurRec);
760     if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
761     
762     // If the variable name is present, add it.
763     std::string VarName;
764     if (Lex.getCode() == tgtok::colon) {
765       if (Lex.Lex() != tgtok::VarName) { // eat the ':'
766         TokError("expected variable name in dag literal");
767         return std::vector<std::pair<llvm::Init*, std::string> >();
768       }
769       VarName = Lex.getCurStrVal();
770       Lex.Lex();  // eat the VarName.
771     }
772     
773     Result.push_back(std::make_pair(Val, VarName));
774     
775     if (Lex.getCode() != tgtok::comma) break;
776     Lex.Lex(); // eat the ','    
777   }
778   
779   return Result;
780 }
781
782
783 /// ParseValueList - Parse a comma separated list of values, returning them as a
784 /// vector.  Note that this always expects to be able to parse at least one
785 /// value.  It returns an empty list if this is not possible.
786 ///
787 ///   ValueList ::= Value (',' Value)
788 ///
789 std::vector<Init*> TGParser::ParseValueList(Record *CurRec) {
790   std::vector<Init*> Result;
791   Result.push_back(ParseValue(CurRec));
792   if (Result.back() == 0) return std::vector<Init*>();
793   
794   while (Lex.getCode() == tgtok::comma) {
795     Lex.Lex();  // Eat the comma
796     
797     Result.push_back(ParseValue(CurRec));
798     if (Result.back() == 0) return std::vector<Init*>();
799   }
800   
801   return Result;
802 }
803
804
805
806 /// ParseDeclaration - Read a declaration, returning the name of field ID, or an
807 /// empty string on error.  This can happen in a number of different context's,
808 /// including within a def or in the template args for a def (which which case
809 /// CurRec will be non-null) and within the template args for a multiclass (in
810 /// which case CurRec will be null, but CurMultiClass will be set).  This can
811 /// also happen within a def that is within a multiclass, which will set both
812 /// CurRec and CurMultiClass.
813 ///
814 ///  Declaration ::= FIELD? Type ID ('=' Value)?
815 ///
816 std::string TGParser::ParseDeclaration(Record *CurRec, 
817                                        bool ParsingTemplateArgs) {
818   // Read the field prefix if present.
819   bool HasField = Lex.getCode() == tgtok::Field;
820   if (HasField) Lex.Lex();
821   
822   RecTy *Type = ParseType();
823   if (Type == 0) return "";
824   
825   if (Lex.getCode() != tgtok::Id) {
826     TokError("Expected identifier in declaration");
827     return "";
828   }
829   
830   LocTy IdLoc = Lex.getLoc();
831   std::string DeclName = Lex.getCurStrVal();
832   Lex.Lex();
833   
834   if (ParsingTemplateArgs) {
835     if (CurRec) {
836       DeclName = CurRec->getName() + ":" + DeclName;
837     } else {
838       assert(CurMultiClass);
839     }
840     if (CurMultiClass)
841       DeclName = CurMultiClass->Rec.getName() + "::" + DeclName;
842   }
843   
844   // Add the value.
845   if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
846     return "";
847   
848   // If a value is present, parse it.
849   if (Lex.getCode() == tgtok::equal) {
850     Lex.Lex();
851     LocTy ValLoc = Lex.getLoc();
852     Init *Val = ParseValue(CurRec);
853     if (Val == 0 ||
854         SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
855       return "";
856   }
857   
858   return DeclName;
859 }
860
861 /// ParseTemplateArgList - Read a template argument list, which is a non-empty
862 /// sequence of template-declarations in <>'s.  If CurRec is non-null, these are
863 /// template args for a def, which may or may not be in a multiclass.  If null,
864 /// these are the template args for a multiclass.
865 ///
866 ///    TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
867 /// 
868 bool TGParser::ParseTemplateArgList(Record *CurRec) {
869   assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
870   Lex.Lex(); // eat the '<'
871   
872   Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
873   
874   // Read the first declaration.
875   std::string TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
876   if (TemplArg.empty())
877     return true;
878   
879   TheRecToAddTo->addTemplateArg(TemplArg);
880   
881   while (Lex.getCode() == tgtok::comma) {
882     Lex.Lex(); // eat the ','
883     
884     // Read the following declarations.
885     TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
886     if (TemplArg.empty())
887       return true;
888     TheRecToAddTo->addTemplateArg(TemplArg);
889   }
890   
891   if (Lex.getCode() != tgtok::greater)
892     return TokError("expected '>' at end of template argument list");
893   Lex.Lex(); // eat the '>'.
894   return false;
895 }
896
897
898 /// ParseBodyItem - Parse a single item at within the body of a def or class.
899 ///
900 ///   BodyItem ::= Declaration ';'
901 ///   BodyItem ::= LET ID OptionalBitList '=' Value ';'
902 bool TGParser::ParseBodyItem(Record *CurRec) {
903   if (Lex.getCode() != tgtok::Let) {
904     if (ParseDeclaration(CurRec, false).empty()) 
905       return true;
906     
907     if (Lex.getCode() != tgtok::semi)
908       return TokError("expected ';' after declaration");
909     Lex.Lex();
910     return false;
911   }
912
913   // LET ID OptionalRangeList '=' Value ';'
914   if (Lex.Lex() != tgtok::Id)
915     return TokError("expected field identifier after let");
916   
917   LocTy IdLoc = Lex.getLoc();
918   std::string FieldName = Lex.getCurStrVal();
919   Lex.Lex();  // eat the field name.
920   
921   std::vector<unsigned> BitList;
922   if (ParseOptionalBitList(BitList)) 
923     return true;
924   std::reverse(BitList.begin(), BitList.end());
925   
926   if (Lex.getCode() != tgtok::equal)
927     return TokError("expected '=' in let expression");
928   Lex.Lex();  // eat the '='.
929   
930   Init *Val = ParseValue(CurRec);
931   if (Val == 0) return true;
932   
933   if (Lex.getCode() != tgtok::semi)
934     return TokError("expected ';' after let expression");
935   Lex.Lex();
936   
937   return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
938 }
939
940 /// ParseBody - Read the body of a class or def.  Return true on error, false on
941 /// success.
942 ///
943 ///   Body     ::= ';'
944 ///   Body     ::= '{' BodyList '}'
945 ///   BodyList BodyItem*
946 ///
947 bool TGParser::ParseBody(Record *CurRec) {
948   // If this is a null definition, just eat the semi and return.
949   if (Lex.getCode() == tgtok::semi) {
950     Lex.Lex();
951     return false;
952   }
953   
954   if (Lex.getCode() != tgtok::l_brace)
955     return TokError("Expected ';' or '{' to start body");
956   // Eat the '{'.
957   Lex.Lex();
958   
959   while (Lex.getCode() != tgtok::r_brace)
960     if (ParseBodyItem(CurRec))
961       return true;
962
963   // Eat the '}'.
964   Lex.Lex();
965   return false;
966 }
967
968 /// ParseObjectBody - Parse the body of a def or class.  This consists of an
969 /// optional ClassList followed by a Body.  CurRec is the current def or class
970 /// that is being parsed.
971 ///
972 ///   ObjectBody      ::= BaseClassList Body
973 ///   BaseClassList   ::= /*empty*/
974 ///   BaseClassList   ::= ':' BaseClassListNE
975 ///   BaseClassListNE ::= SubClassRef (',' SubClassRef)*
976 ///
977 bool TGParser::ParseObjectBody(Record *CurRec) {
978   // If there is a baseclass list, read it.
979   if (Lex.getCode() == tgtok::colon) {
980     Lex.Lex();
981     
982     // Read all of the subclasses.
983     SubClassReference SubClass = ParseSubClassReference(CurRec, false);
984     while (1) {
985       // Check for error.
986       if (SubClass.Rec == 0) return true;
987      
988       // Add it.
989       if (AddSubClass(CurRec, SubClass))
990         return true;
991       
992       if (Lex.getCode() != tgtok::comma) break;
993       Lex.Lex(); // eat ','.
994       SubClass = ParseSubClassReference(CurRec, false);
995     }
996   }
997
998   // Process any variables on the let stack.
999   for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1000     for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1001       if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1002                    LetStack[i][j].Bits, LetStack[i][j].Value))
1003         return true;
1004   
1005   return ParseBody(CurRec);
1006 }
1007
1008
1009 /// ParseDef - Parse and return a top level or multiclass def, return the record
1010 /// corresponding to it.  This returns null on error.
1011 ///
1012 ///   DefInst ::= DEF ObjectName ObjectBody
1013 ///
1014 llvm::Record *TGParser::ParseDef(MultiClass *CurMultiClass) {
1015   LocTy DefLoc = Lex.getLoc();
1016   assert(Lex.getCode() == tgtok::Def && "Unknown tok");
1017   Lex.Lex();  // Eat the 'def' token.  
1018
1019   // Parse ObjectName and make a record for it.
1020   Record *CurRec = new Record(ParseObjectName());
1021   
1022   if (!CurMultiClass) {
1023     // Top-level def definition.
1024     
1025     // Ensure redefinition doesn't happen.
1026     if (Records.getDef(CurRec->getName())) {
1027       Error(DefLoc, "def '" + CurRec->getName() + "' already defined");
1028       return 0;
1029     }
1030     Records.addDef(CurRec);
1031   } else {
1032     // Otherwise, a def inside a multiclass, add it to the multiclass.
1033     for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
1034       if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
1035         Error(DefLoc, "def '" + CurRec->getName() +
1036               "' already defined in this multiclass!");
1037         return 0;
1038       }
1039     CurMultiClass->DefPrototypes.push_back(CurRec);
1040   }
1041   
1042   if (ParseObjectBody(CurRec))
1043     return 0;
1044   
1045   if (CurMultiClass == 0)  // Def's in multiclasses aren't really defs.
1046     CurRec->resolveReferences();
1047   
1048   // If ObjectBody has template arguments, it's an error.
1049   assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
1050   return CurRec;
1051 }
1052
1053
1054 /// ParseClass - Parse a tblgen class definition.
1055 ///
1056 ///   ClassInst ::= CLASS ID TemplateArgList? ObjectBody
1057 ///
1058 bool TGParser::ParseClass() {
1059   assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
1060   Lex.Lex();
1061   
1062   if (Lex.getCode() != tgtok::Id)
1063     return TokError("expected class name after 'class' keyword");
1064   
1065   Record *CurRec = Records.getClass(Lex.getCurStrVal());
1066   if (CurRec) {
1067     // If the body was previously defined, this is an error.
1068     if (!CurRec->getValues().empty() ||
1069         !CurRec->getSuperClasses().empty() ||
1070         !CurRec->getTemplateArgs().empty())
1071       return TokError("Class '" + CurRec->getName() + "' already defined");
1072   } else {
1073     // If this is the first reference to this class, create and add it.
1074     CurRec = new Record(Lex.getCurStrVal());
1075     Records.addClass(CurRec);
1076   }
1077   Lex.Lex(); // eat the name.
1078   
1079   // If there are template args, parse them.
1080   if (Lex.getCode() == tgtok::less)
1081     if (ParseTemplateArgList(CurRec))
1082       return true;
1083
1084   // Finally, parse the object body.
1085   return ParseObjectBody(CurRec);
1086 }
1087
1088 /// ParseLetList - Parse a non-empty list of assignment expressions into a list
1089 /// of LetRecords.
1090 ///
1091 ///   LetList ::= LetItem (',' LetItem)*
1092 ///   LetItem ::= ID OptionalRangeList '=' Value
1093 ///
1094 std::vector<LetRecord> TGParser::ParseLetList() {
1095   std::vector<LetRecord> Result;
1096   
1097   while (1) {
1098     if (Lex.getCode() != tgtok::Id) {
1099       TokError("expected identifier in let definition");
1100       return std::vector<LetRecord>();
1101     }
1102     std::string Name = Lex.getCurStrVal();
1103     LocTy NameLoc = Lex.getLoc();
1104     Lex.Lex();  // Eat the identifier. 
1105
1106     // Check for an optional RangeList.
1107     std::vector<unsigned> Bits;
1108     if (ParseOptionalRangeList(Bits)) 
1109       return std::vector<LetRecord>();
1110     std::reverse(Bits.begin(), Bits.end());
1111     
1112     if (Lex.getCode() != tgtok::equal) {
1113       TokError("expected '=' in let expression");
1114       return std::vector<LetRecord>();
1115     }
1116     Lex.Lex();  // eat the '='.
1117     
1118     Init *Val = ParseValue(0);
1119     if (Val == 0) return std::vector<LetRecord>();
1120     
1121     // Now that we have everything, add the record.
1122     Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
1123     
1124     if (Lex.getCode() != tgtok::comma)
1125       return Result;
1126     Lex.Lex();  // eat the comma.    
1127   }
1128 }
1129
1130 /// ParseTopLevelLet - Parse a 'let' at top level.  This can be a couple of
1131 /// different related productions.
1132 ///
1133 ///   Object ::= LET LetList IN '{' ObjectList '}'
1134 ///   Object ::= LET LetList IN Object
1135 ///
1136 bool TGParser::ParseTopLevelLet() {
1137   assert(Lex.getCode() == tgtok::Let && "Unexpected token");
1138   Lex.Lex();
1139   
1140   // Add this entry to the let stack.
1141   std::vector<LetRecord> LetInfo = ParseLetList();
1142   if (LetInfo.empty()) return true;
1143   LetStack.push_back(LetInfo);
1144
1145   if (Lex.getCode() != tgtok::In)
1146     return TokError("expected 'in' at end of top-level 'let'");
1147   Lex.Lex();
1148   
1149   // If this is a scalar let, just handle it now
1150   if (Lex.getCode() != tgtok::l_brace) {
1151     // LET LetList IN Object
1152     if (ParseObject())
1153       return true;
1154   } else {   // Object ::= LETCommand '{' ObjectList '}'
1155     LocTy BraceLoc = Lex.getLoc();
1156     // Otherwise, this is a group let.
1157     Lex.Lex();  // eat the '{'.
1158     
1159     // Parse the object list.
1160     if (ParseObjectList())
1161       return true;
1162     
1163     if (Lex.getCode() != tgtok::r_brace) {
1164       TokError("expected '}' at end of top level let command");
1165       return Error(BraceLoc, "to match this '{'");
1166     }
1167     Lex.Lex();
1168   }
1169   
1170   // Outside this let scope, this let block is not active.
1171   LetStack.pop_back();
1172   return false;
1173 }
1174
1175 /// ParseMultiClassDef - Parse a def in a multiclass context.
1176 ///
1177 ///  MultiClassDef ::= DefInst
1178 ///
1179 bool TGParser::ParseMultiClassDef(MultiClass *CurMC) {
1180   if (Lex.getCode() != tgtok::Def) 
1181     return TokError("expected 'def' in multiclass body");
1182
1183   Record *D = ParseDef(CurMC);
1184   if (D == 0) return true;
1185   
1186   // Copy the template arguments for the multiclass into the def.
1187   const std::vector<std::string> &TArgs = CurMC->Rec.getTemplateArgs();
1188   
1189   for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1190     const RecordVal *RV = CurMC->Rec.getValue(TArgs[i]);
1191     assert(RV && "Template arg doesn't exist?");
1192     D->addValue(*RV);
1193   }
1194
1195   return false;
1196 }
1197
1198 /// ParseMultiClass - Parse a multiclass definition.
1199 ///
1200 ///  MultiClassInst ::= MULTICLASS ID TemplateArgList? '{' MultiClassDef+ '}'
1201 ///
1202 bool TGParser::ParseMultiClass() {
1203   assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
1204   Lex.Lex();  // Eat the multiclass token.
1205
1206   if (Lex.getCode() != tgtok::Id)
1207     return TokError("expected identifier after multiclass for name");
1208   std::string Name = Lex.getCurStrVal();
1209   
1210   if (MultiClasses.count(Name))
1211     return TokError("multiclass '" + Name + "' already defined");
1212   
1213   CurMultiClass  = MultiClasses[Name] = new MultiClass(Name);
1214   Lex.Lex();  // Eat the identifier.
1215   
1216   // If there are template args, parse them.
1217   if (Lex.getCode() == tgtok::less)
1218     if (ParseTemplateArgList(0))
1219       return true;
1220
1221   if (Lex.getCode() != tgtok::l_brace)
1222     return TokError("expected '{' in multiclass definition");
1223
1224   if (Lex.Lex() == tgtok::r_brace)  // eat the '{'.
1225     return TokError("multiclass must contain at least one def");
1226   
1227   while (Lex.getCode() != tgtok::r_brace)
1228     if (ParseMultiClassDef(CurMultiClass))
1229       return true;
1230   
1231   Lex.Lex();  // eat the '}'.
1232   
1233   CurMultiClass = 0;
1234   return false;
1235 }
1236
1237 /// ParseDefm - Parse the instantiation of a multiclass.
1238 ///
1239 ///   DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
1240 ///
1241 bool TGParser::ParseDefm() {
1242   assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
1243   if (Lex.Lex() != tgtok::Id)  // eat the defm.
1244     return TokError("expected identifier after defm");
1245   
1246   LocTy DefmPrefixLoc = Lex.getLoc();
1247   std::string DefmPrefix = Lex.getCurStrVal();
1248   if (Lex.Lex() != tgtok::colon)
1249     return TokError("expected ':' after defm identifier");
1250   
1251   // eat the colon.
1252   Lex.Lex();
1253
1254   LocTy SubClassLoc = Lex.getLoc();
1255   SubClassReference Ref = ParseSubClassReference(0, true);
1256   if (Ref.Rec == 0) return true;
1257   
1258   if (Lex.getCode() != tgtok::semi)
1259     return TokError("expected ';' at end of defm");
1260   Lex.Lex();
1261   
1262   // To instantiate a multiclass, we need to first get the multiclass, then
1263   // instantiate each def contained in the multiclass with the SubClassRef
1264   // template parameters.
1265   MultiClass *MC = MultiClasses[Ref.Rec->getName()];
1266   assert(MC && "Didn't lookup multiclass correctly?");
1267   std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
1268   
1269   // Verify that the correct number of template arguments were specified.
1270   const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs();
1271   if (TArgs.size() < TemplateVals.size())
1272     return Error(SubClassLoc,
1273                  "more template args specified than multiclass expects");
1274   
1275   // Loop over all the def's in the multiclass, instantiating each one.
1276   for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
1277     Record *DefProto = MC->DefPrototypes[i];
1278     
1279     // Add the suffix to the defm name to get the new name.
1280     Record *CurRec = new Record(DefmPrefix + DefProto->getName());
1281     
1282     SubClassReference Ref;
1283     Ref.RefLoc = DefmPrefixLoc;
1284     Ref.Rec = DefProto;
1285     AddSubClass(CurRec, Ref);
1286     
1287     // Loop over all of the template arguments, setting them to the specified
1288     // value or leaving them as the default if necessary.
1289     for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1290       if (i < TemplateVals.size()) { // A value is specified for this temp-arg?
1291         // Set it now.
1292         if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
1293                      TemplateVals[i]))
1294           return true;
1295         
1296         // Resolve it next.
1297         CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
1298         
1299         // Now remove it.
1300         CurRec->removeValue(TArgs[i]);
1301         
1302       } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
1303         return Error(SubClassLoc, "value not specified for template argument #"+
1304                      utostr(i) + " (" + TArgs[i] + ") of multiclassclass '" +
1305                      MC->Rec.getName() + "'");
1306       }
1307     }
1308     
1309     // If the mdef is inside a 'let' expression, add to each def.
1310     for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1311       for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1312         if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1313                      LetStack[i][j].Bits, LetStack[i][j].Value)) {
1314           Error(DefmPrefixLoc, "when instantiating this defm");
1315           return true;
1316         }
1317     
1318     
1319     // Ensure redefinition doesn't happen.
1320     if (Records.getDef(CurRec->getName()))
1321       return Error(DefmPrefixLoc, "def '" + CurRec->getName() + 
1322                    "' already defined, instantiating defm with subdef '" + 
1323                    DefProto->getName() + "'");
1324     Records.addDef(CurRec);
1325     CurRec->resolveReferences();
1326   }
1327   
1328   return false;
1329 }
1330
1331 /// ParseObject
1332 ///   Object ::= ClassInst
1333 ///   Object ::= DefInst
1334 ///   Object ::= MultiClassInst
1335 ///   Object ::= DefMInst
1336 ///   Object ::= LETCommand '{' ObjectList '}'
1337 ///   Object ::= LETCommand Object
1338 bool TGParser::ParseObject() {
1339   switch (Lex.getCode()) {
1340   default: assert(0 && "This is not an object");
1341   case tgtok::Let:   return ParseTopLevelLet();
1342   case tgtok::Def:   return ParseDef(0) == 0;
1343   case tgtok::Defm:  return ParseDefm();
1344   case tgtok::Class: return ParseClass();
1345   case tgtok::MultiClass: return ParseMultiClass();
1346   }
1347 }
1348
1349 /// ParseObjectList
1350 ///   ObjectList :== Object*
1351 bool TGParser::ParseObjectList() {
1352   while (isObjectStart(Lex.getCode())) {
1353     if (ParseObject())
1354       return true;
1355   }
1356   return false;
1357 }
1358
1359
1360 bool TGParser::ParseFile() {
1361   Lex.Lex(); // Prime the lexer.
1362   if (ParseObjectList()) return true;
1363   
1364   // If we have unread input at the end of the file, report it.
1365   if (Lex.getCode() == tgtok::Eof)
1366     return false;
1367   
1368   return TokError("Unexpected input at top level");
1369 }
1370