Fix more static dtor issues
[oota-llvm.git] / utils / TableGen / FileParser.y.cvs
1 //===-- FileParser.y - Parser for TableGen files ----------------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements the bison parser for Table Generator files...
11 //
12 //===----------------------------------------------------------------------===//
13
14 %{
15 #include "Record.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include <algorithm>
18 #include <cstdio>
19 #define YYERROR_VERBOSE 1
20
21 int yyerror(const char *ErrorMsg);
22 int yylex();
23
24 namespace llvm {
25   struct MultiClass {
26     Record Rec;  // Placeholder for template args and Name.
27     std::vector<Record*> DefPrototypes;
28     
29     MultiClass(const std::string &Name) : Rec(Name) {}
30   };
31
32   
33 static std::map<std::string, MultiClass*> MultiClasses;
34   
35 extern int Filelineno;
36 static MultiClass *CurMultiClass = 0;    // Set while parsing a multiclass.
37 static std::string *CurDefmPrefix = 0;   // Set while parsing defm.
38 static Record *CurRec = 0;
39 static bool ParsingTemplateArgs = false;
40
41 typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
42
43 struct LetRecord {
44   std::string Name;
45   std::vector<unsigned> Bits;
46   Init *Value;
47   bool HasBits;
48   LetRecord(const std::string &N, std::vector<unsigned> *B, Init *V)
49     : Name(N), Value(V), HasBits(B != 0) {
50     if (HasBits) Bits = *B;
51   }
52 };
53
54 static std::vector<std::vector<LetRecord> > LetStack;
55
56
57 extern std::ostream &err();
58
59 /// getActiveRec - If inside a def/class definition, return the def/class.
60 /// Otherwise, if within a multidef, return it.
61 static Record *getActiveRec() {
62   return CurRec ? CurRec : &CurMultiClass->Rec;
63 }
64
65 static void addValue(const RecordVal &RV) {
66   Record *TheRec = getActiveRec();
67   
68   if (RecordVal *ERV = TheRec->getValue(RV.getName())) {
69     // The value already exists in the class, treat this as a set...
70     if (ERV->setValue(RV.getValue())) {
71       err() << "New definition of '" << RV.getName() << "' of type '"
72             << *RV.getType() << "' is incompatible with previous "
73             << "definition of type '" << *ERV->getType() << "'!\n";
74       exit(1);
75     }
76   } else {
77     TheRec->addValue(RV);
78   }
79 }
80
81 static void addSuperClass(Record *SC) {
82   if (CurRec->isSubClassOf(SC)) {
83     err() << "Already subclass of '" << SC->getName() << "'!\n";
84     exit(1);
85   }
86   CurRec->addSuperClass(SC);
87 }
88
89 static void setValue(const std::string &ValName, 
90                      std::vector<unsigned> *BitList, Init *V) {
91   if (!V) return;
92
93   RecordVal *RV = CurRec->getValue(ValName);
94   if (RV == 0) {
95     err() << "Value '" << ValName << "' unknown!\n";
96     exit(1);
97   }
98
99   // Do not allow assignments like 'X = X'.  This will just cause infinite loops
100   // in the resolution machinery.
101   if (!BitList)
102     if (VarInit *VI = dynamic_cast<VarInit*>(V))
103       if (VI->getName() == ValName)
104         return;
105   
106   // If we are assigning to a subset of the bits in the value... then we must be
107   // assigning to a field of BitsRecTy, which must have a BitsInit
108   // initializer...
109   //
110   if (BitList) {
111     BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
112     if (CurVal == 0) {
113       err() << "Value '" << ValName << "' is not a bits type!\n";
114       exit(1);
115     }
116
117     // Convert the incoming value to a bits type of the appropriate size...
118     Init *BI = V->convertInitializerTo(new BitsRecTy(BitList->size()));
119     if (BI == 0) {
120       V->convertInitializerTo(new BitsRecTy(BitList->size()));
121       err() << "Initializer '" << *V << "' not compatible with bit range!\n";
122       exit(1);
123     }
124
125     // We should have a BitsInit type now...
126     assert(dynamic_cast<BitsInit*>(BI) != 0 || &(std::cerr << *BI) == 0);
127     BitsInit *BInit = (BitsInit*)BI;
128
129     BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
130
131     // Loop over bits, assigning values as appropriate...
132     for (unsigned i = 0, e = BitList->size(); i != e; ++i) {
133       unsigned Bit = (*BitList)[i];
134       if (NewVal->getBit(Bit)) {
135         err() << "Cannot set bit #" << Bit << " of value '" << ValName
136               << "' more than once!\n";
137         exit(1);
138       }
139       NewVal->setBit(Bit, BInit->getBit(i));
140     }
141
142     for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
143       if (NewVal->getBit(i) == 0)
144         NewVal->setBit(i, CurVal->getBit(i));
145
146     V = NewVal;
147   }
148
149   if (RV->setValue(V)) {
150     err() << "Value '" << ValName << "' of type '" << *RV->getType()
151           << "' is incompatible with initializer '" << *V << "'!\n";
152     exit(1);
153   }
154 }
155
156 // addSubClass - Add SC as a subclass to CurRec, resolving TemplateArgs as SC's
157 // template arguments.
158 static void addSubClass(Record *SC, const std::vector<Init*> &TemplateArgs) {
159   // Add all of the values in the subclass into the current class...
160   const std::vector<RecordVal> &Vals = SC->getValues();
161   for (unsigned i = 0, e = Vals.size(); i != e; ++i)
162     addValue(Vals[i]);
163
164   const std::vector<std::string> &TArgs = SC->getTemplateArgs();
165
166   // Ensure that an appropriate number of template arguments are specified...
167   if (TArgs.size() < TemplateArgs.size()) {
168     err() << "ERROR: More template args specified than expected!\n";
169     exit(1);
170   }
171   
172   // Loop over all of the template arguments, setting them to the specified
173   // value or leaving them as the default if necessary.
174   for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
175     if (i < TemplateArgs.size()) {  // A value is specified for this temp-arg?
176       // Set it now.
177       setValue(TArgs[i], 0, TemplateArgs[i]);
178
179       // Resolve it next.
180       CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
181                                   
182       
183       // Now remove it.
184       CurRec->removeValue(TArgs[i]);
185
186     } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
187       err() << "ERROR: Value not specified for template argument #"
188             << i << " (" << TArgs[i] << ") of subclass '" << SC->getName()
189             << "'!\n";
190       exit(1);
191     }
192   }
193
194   // Since everything went well, we can now set the "superclass" list for the
195   // current record.
196   const std::vector<Record*> &SCs = SC->getSuperClasses();
197   for (unsigned i = 0, e = SCs.size(); i != e; ++i)
198     addSuperClass(SCs[i]);
199   addSuperClass(SC);
200 }
201
202 } // End llvm namespace
203
204 using namespace llvm;
205
206 %}
207
208 %union {
209   std::string*                StrVal;
210   int                         IntVal;
211   llvm::RecTy*                Ty;
212   llvm::Init*                 Initializer;
213   std::vector<llvm::Init*>*   FieldList;
214   std::vector<unsigned>*      BitList;
215   llvm::Record*               Rec;
216   std::vector<llvm::Record*>* RecList;
217   SubClassRefTy*              SubClassRef;
218   std::vector<SubClassRefTy>* SubClassList;
219   std::vector<std::pair<llvm::Init*, std::string> >* DagValueList;
220 };
221
222 %token INT BIT STRING BITS LIST CODE DAG CLASS DEF MULTICLASS DEFM FIELD LET IN
223 %token SHLTOK SRATOK SRLTOK STRCONCATTOK
224 %token <IntVal>      INTVAL
225 %token <StrVal>      ID VARNAME STRVAL CODEFRAGMENT
226
227 %type <Ty>           Type
228 %type <Rec>          ClassInst DefInst MultiClassDef ObjectBody ClassID
229 %type <RecList>      MultiClassBody
230
231 %type <SubClassRef>  SubClassRef
232 %type <SubClassList> ClassList ClassListNE
233 %type <IntVal>       OptPrefix
234 %type <Initializer>  Value OptValue IDValue
235 %type <DagValueList> DagArgList DagArgListNE
236 %type <FieldList>    ValueList ValueListNE
237 %type <BitList>      BitList OptBitList RBitList
238 %type <StrVal>       Declaration OptID OptVarName ObjectName
239
240 %start File
241
242 %%
243
244 ClassID : ID {
245     if (CurDefmPrefix) {
246       // If CurDefmPrefix is set, we're parsing a defm, which means that this is
247       // actually the name of a multiclass.
248       MultiClass *MC = MultiClasses[*$1];
249       if (MC == 0) {
250         err() << "Couldn't find class '" << *$1 << "'!\n";
251         exit(1);
252       }
253       $$ = &MC->Rec;
254     } else {
255       $$ = Records.getClass(*$1);
256     }
257     if ($$ == 0) {
258       err() << "Couldn't find class '" << *$1 << "'!\n";
259       exit(1);
260     }
261     delete $1;
262   };
263
264
265 // TableGen types...
266 Type : STRING {                       // string type
267     $$ = new StringRecTy();
268   } | BIT {                           // bit type
269     $$ = new BitRecTy();
270   } | BITS '<' INTVAL '>' {           // bits<x> type
271     $$ = new BitsRecTy($3);
272   } | INT {                           // int type
273     $$ = new IntRecTy();
274   } | LIST '<' Type '>'    {          // list<x> type
275     $$ = new ListRecTy($3);
276   } | CODE {                          // code type
277     $$ = new CodeRecTy();
278   } | DAG {                           // dag type
279     $$ = new DagRecTy();
280   } | ClassID {                       // Record Type
281     $$ = new RecordRecTy($1);
282   };
283
284 OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; };
285
286 OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; };
287
288 IDValue : ID {
289   if (const RecordVal *RV = (CurRec ? CurRec->getValue(*$1) : 0)) {
290     $$ = new VarInit(*$1, RV->getType());
291   } else if (CurRec && CurRec->isTemplateArg(CurRec->getName()+":"+*$1)) {
292     const RecordVal *RV = CurRec->getValue(CurRec->getName()+":"+*$1);
293     assert(RV && "Template arg doesn't exist??");
294     $$ = new VarInit(CurRec->getName()+":"+*$1, RV->getType());
295   } else if (CurMultiClass &&
296       CurMultiClass->Rec.isTemplateArg(CurMultiClass->Rec.getName()+"::"+*$1)) {
297     std::string Name = CurMultiClass->Rec.getName()+"::"+*$1;
298     const RecordVal *RV = CurMultiClass->Rec.getValue(Name);
299     assert(RV && "Template arg doesn't exist??");
300     $$ = new VarInit(Name, RV->getType());
301   } else if (Record *D = Records.getDef(*$1)) {
302     $$ = new DefInit(D);
303   } else {
304     err() << "Variable not defined: '" << *$1 << "'!\n";
305     exit(1);
306   }
307   
308   delete $1;
309 };
310
311 Value : IDValue {
312     $$ = $1;
313   } | INTVAL {
314     $$ = new IntInit($1);
315   } | STRVAL {
316     $$ = new StringInit(*$1);
317     delete $1;
318   } | CODEFRAGMENT {
319     $$ = new CodeInit(*$1);
320     delete $1;
321   } | '?' {
322     $$ = new UnsetInit();
323   } | '{' ValueList '}' {
324     BitsInit *Init = new BitsInit($2->size());
325     for (unsigned i = 0, e = $2->size(); i != e; ++i) {
326       struct Init *Bit = (*$2)[i]->convertInitializerTo(new BitRecTy());
327       if (Bit == 0) {
328         err() << "Element #" << i << " (" << *(*$2)[i]
329               << ") is not convertable to a bit!\n";
330         exit(1);
331       }
332       Init->setBit($2->size()-i-1, Bit);
333     }
334     $$ = Init;
335     delete $2;
336   } | ID '<' ValueListNE '>' {
337     // This is a CLASS<initvalslist> expression.  This is supposed to synthesize
338     // a new anonymous definition, deriving from CLASS<initvalslist> with no
339     // body.
340     Record *Class = Records.getClass(*$1);
341     if (!Class) {
342       err() << "Expected a class, got '" << *$1 << "'!\n";
343       exit(1);
344     }
345     delete $1;
346     
347     static unsigned AnonCounter = 0;
348     Record *OldRec = CurRec;  // Save CurRec.
349     
350     // Create the new record, set it as CurRec temporarily.
351     CurRec = new Record("anonymous.val."+utostr(AnonCounter++));
352     addSubClass(Class, *$3);    // Add info about the subclass to CurRec.
353     delete $3;  // Free up the template args.
354     
355     CurRec->resolveReferences();
356     
357     Records.addDef(CurRec);
358     
359     // The result of the expression is a reference to the new record.
360     $$ = new DefInit(CurRec);
361     
362     // Restore the old CurRec
363     CurRec = OldRec;
364   } | Value '{' BitList '}' {
365     $$ = $1->convertInitializerBitRange(*$3);
366     if ($$ == 0) {
367       err() << "Invalid bit range for value '" << *$1 << "'!\n";
368       exit(1);
369     }
370     delete $3;
371   } | '[' ValueList ']' {
372     $$ = new ListInit(*$2);
373     delete $2;
374   } | Value '.' ID {
375     if (!$1->getFieldType(*$3)) {
376       err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n";
377       exit(1);
378     }
379     $$ = new FieldInit($1, *$3);
380     delete $3;
381   } | '(' IDValue DagArgList ')' {
382     $$ = new DagInit($2, *$3);
383     delete $3;
384   } | Value '[' BitList ']' {
385     std::reverse($3->begin(), $3->end());
386     $$ = $1->convertInitListSlice(*$3);
387     if ($$ == 0) {
388       err() << "Invalid list slice for value '" << *$1 << "'!\n";
389       exit(1);
390     }
391     delete $3;
392   } | SHLTOK '(' Value ',' Value ')' {
393     $$ = (new BinOpInit(BinOpInit::SHL, $3, $5))->Fold();
394   } | SRATOK '(' Value ',' Value ')' {
395     $$ = (new BinOpInit(BinOpInit::SRA, $3, $5))->Fold();
396   } | SRLTOK '(' Value ',' Value ')' {
397     $$ = (new BinOpInit(BinOpInit::SRL, $3, $5))->Fold();
398   } | STRCONCATTOK '(' Value ',' Value ')' {
399     $$ = (new BinOpInit(BinOpInit::STRCONCAT, $3, $5))->Fold();
400   };
401
402 OptVarName : /* empty */ {
403     $$ = new std::string();
404   }
405   | ':' VARNAME {
406     $$ = $2;
407   };
408
409 DagArgListNE : Value OptVarName {
410     $$ = new std::vector<std::pair<Init*, std::string> >();
411     $$->push_back(std::make_pair($1, *$2));
412     delete $2;
413   }
414   | DagArgListNE ',' Value OptVarName {
415     $1->push_back(std::make_pair($3, *$4));
416     delete $4;
417     $$ = $1;
418   };
419
420 DagArgList : /*empty*/ {
421     $$ = new std::vector<std::pair<Init*, std::string> >();
422   }
423   | DagArgListNE { $$ = $1; };
424
425
426 RBitList : INTVAL {
427     $$ = new std::vector<unsigned>();
428     $$->push_back($1);
429   } | INTVAL '-' INTVAL {
430     if ($1 < 0 || $3 < 0) {
431       err() << "Invalid range: " << $1 << "-" << $3 << "!\n";
432       exit(1);
433     }
434     $$ = new std::vector<unsigned>();
435     if ($1 < $3) {
436       for (int i = $1; i <= $3; ++i)
437         $$->push_back(i);
438     } else {
439       for (int i = $1; i >= $3; --i)
440         $$->push_back(i);
441     }
442   } | INTVAL INTVAL {
443     $2 = -$2;
444     if ($1 < 0 || $2 < 0) {
445       err() << "Invalid range: " << $1 << "-" << $2 << "!\n";
446       exit(1);
447     }
448     $$ = new std::vector<unsigned>();
449     if ($1 < $2) {
450       for (int i = $1; i <= $2; ++i)
451         $$->push_back(i);
452     } else {
453       for (int i = $1; i >= $2; --i)
454         $$->push_back(i);
455     }
456   } | RBitList ',' INTVAL {
457     ($$=$1)->push_back($3);
458   } | RBitList ',' INTVAL '-' INTVAL {
459     if ($3 < 0 || $5 < 0) {
460       err() << "Invalid range: " << $3 << "-" << $5 << "!\n";
461       exit(1);
462     }
463     $$ = $1;
464     if ($3 < $5) {
465       for (int i = $3; i <= $5; ++i)
466         $$->push_back(i);
467     } else {
468       for (int i = $3; i >= $5; --i)
469         $$->push_back(i);
470     }
471   } | RBitList ',' INTVAL INTVAL {
472     $4 = -$4;
473     if ($3 < 0 || $4 < 0) {
474       err() << "Invalid range: " << $3 << "-" << $4 << "!\n";
475       exit(1);
476     }
477     $$ = $1;
478     if ($3 < $4) {
479       for (int i = $3; i <= $4; ++i)
480         $$->push_back(i);
481     } else {
482       for (int i = $3; i >= $4; --i)
483         $$->push_back(i);
484     }
485   };
486
487 BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); };
488
489 OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; };
490
491
492
493 ValueList : /*empty*/ {
494     $$ = new std::vector<Init*>();
495   } | ValueListNE {
496     $$ = $1;
497   };
498
499 ValueListNE : Value {
500     $$ = new std::vector<Init*>();
501     $$->push_back($1);
502   } | ValueListNE ',' Value {
503     ($$ = $1)->push_back($3);
504   };
505
506 Declaration : OptPrefix Type ID OptValue {
507   std::string DecName = *$3;
508   if (ParsingTemplateArgs) {
509     if (CurRec) {
510       DecName = CurRec->getName() + ":" + DecName;
511     } else {
512       assert(CurMultiClass);
513     }
514     if (CurMultiClass)
515       DecName = CurMultiClass->Rec.getName() + "::" + DecName;
516   }
517
518   addValue(RecordVal(DecName, $2, $1));
519   setValue(DecName, 0, $4);
520   $$ = new std::string(DecName);
521 };
522
523 BodyItem : Declaration ';' {
524   delete $1;
525 } | LET ID OptBitList '=' Value ';' {
526   setValue(*$2, $3, $5);
527   delete $2;
528   delete $3;
529 };
530
531 BodyList : /*empty*/ | BodyList BodyItem;
532 Body : ';' | '{' BodyList '}';
533
534 SubClassRef : ClassID {
535     $$ = new SubClassRefTy($1, new std::vector<Init*>());
536   } | ClassID '<' ValueListNE '>' {
537     $$ = new SubClassRefTy($1, $3);
538   };
539
540 ClassListNE : SubClassRef {
541     $$ = new std::vector<SubClassRefTy>();
542     $$->push_back(*$1);
543     delete $1;
544   }
545   | ClassListNE ',' SubClassRef {
546     ($$=$1)->push_back(*$3);
547     delete $3;
548   };
549
550 ClassList : /*empty */ {
551     $$ = new std::vector<SubClassRefTy>();
552   }
553   | ':' ClassListNE {
554     $$ = $2;
555   };
556
557 DeclListNE : Declaration {
558   getActiveRec()->addTemplateArg(*$1);
559   delete $1;
560 } | DeclListNE ',' Declaration {
561   getActiveRec()->addTemplateArg(*$3);
562   delete $3;
563 };
564
565 TemplateArgList : '<' DeclListNE '>' {};
566 OptTemplateArgList : /*empty*/ | TemplateArgList;
567
568 OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); };
569
570 ObjectName : OptID {
571   static unsigned AnonCounter = 0;
572   if ($1->empty())
573     *$1 = "anonymous."+utostr(AnonCounter++);
574   $$ = $1;
575 };
576
577 ClassName : ObjectName {
578   // If a class of this name already exists, it must be a forward ref.
579   if ((CurRec = Records.getClass(*$1))) {
580     // If the body was previously defined, this is an error.
581     if (!CurRec->getValues().empty() ||
582         !CurRec->getSuperClasses().empty() ||
583         !CurRec->getTemplateArgs().empty()) {
584       err() << "Class '" << CurRec->getName() << "' already defined!\n";
585       exit(1);
586     }
587   } else {
588     // If this is the first reference to this class, create and add it.
589     CurRec = new Record(*$1);
590     Records.addClass(CurRec);
591   }
592   delete $1;
593 };
594
595 DefName : ObjectName {
596   CurRec = new Record(*$1);
597   delete $1;
598   
599   if (!CurMultiClass) {
600     // Top-level def definition.
601     
602     // Ensure redefinition doesn't happen.
603     if (Records.getDef(CurRec->getName())) {
604       err() << "def '" << CurRec->getName() << "' already defined!\n";
605       exit(1);
606     }
607     Records.addDef(CurRec);
608   } else {
609     // Otherwise, a def inside a multiclass, add it to the multiclass.
610     for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
611       if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
612         err() << "def '" << CurRec->getName()
613               << "' already defined in this multiclass!\n";
614         exit(1);
615       }
616     CurMultiClass->DefPrototypes.push_back(CurRec);
617   }
618 };
619
620 ObjectBody : ClassList {
621            for (unsigned i = 0, e = $1->size(); i != e; ++i) {
622              addSubClass((*$1)[i].first, *(*$1)[i].second);
623              // Delete the template arg values for the class
624              delete (*$1)[i].second;
625            }
626            delete $1;   // Delete the class list...
627   
628            // Process any variables on the set stack...
629            for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
630              for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
631                setValue(LetStack[i][j].Name,
632                         LetStack[i][j].HasBits ? &LetStack[i][j].Bits : 0,
633                         LetStack[i][j].Value);
634          } Body {
635            $$ = CurRec;
636            CurRec = 0;
637          };
638
639 ClassInst : CLASS ClassName {
640                 ParsingTemplateArgs = true;
641             } OptTemplateArgList {
642                 ParsingTemplateArgs = false;
643             } ObjectBody {
644         $$ = $6;
645      };
646
647 DefInst : DEF DefName ObjectBody {
648   if (CurMultiClass == 0)  // Def's in multiclasses aren't really defs.
649     $3->resolveReferences();
650
651   // If ObjectBody has template arguments, it's an error.
652   assert($3->getTemplateArgs().empty() && "How'd this get template args?");
653   $$ = $3;
654 };
655
656 // MultiClassDef - A def instance specified inside a multiclass.
657 MultiClassDef : DefInst {
658   $$ = $1;
659   // Copy the template arguments for the multiclass into the def.
660   const std::vector<std::string> &TArgs = CurMultiClass->Rec.getTemplateArgs();
661   
662   for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
663     const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
664     assert(RV && "Template arg doesn't exist?");
665     $$->addValue(*RV);
666   }
667 };
668
669 // MultiClassBody - Sequence of def's that are instantiated when a multiclass is
670 // used.
671 MultiClassBody : MultiClassDef {
672   $$ = new std::vector<Record*>();
673   $$->push_back($1);
674 } | MultiClassBody MultiClassDef {
675   $$->push_back($2);  
676 };
677
678 MultiClassName : ID {
679   MultiClass *&MCE = MultiClasses[*$1];
680   if (MCE) {
681     err() << "multiclass '" << *$1 << "' already defined!\n";
682     exit(1);
683   }
684   MCE = CurMultiClass = new MultiClass(*$1);
685   delete $1;
686 };
687
688 // MultiClass - Multiple definitions.
689 MultiClassInst : MULTICLASS MultiClassName {
690                                              ParsingTemplateArgs = true;
691                                            } OptTemplateArgList {
692                                              ParsingTemplateArgs = false;
693                                            }'{' MultiClassBody '}' {
694   CurMultiClass = 0;
695 };
696
697 // DefMInst - Instantiate a multiclass.
698 DefMInst : DEFM ID { CurDefmPrefix = $2; } ':' SubClassRef ';' {
699   // To instantiate a multiclass, we need to first get the multiclass, then
700   // instantiate each def contained in the multiclass with the SubClassRef
701   // template parameters.
702   MultiClass *MC = MultiClasses[$5->first->getName()];
703   assert(MC && "Didn't lookup multiclass correctly?");
704   std::vector<Init*> &TemplateVals = *$5->second;
705   delete $5;
706   
707   // Verify that the correct number of template arguments were specified.
708   const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs();
709   if (TArgs.size() < TemplateVals.size()) {
710     err() << "ERROR: More template args specified than multiclass expects!\n";
711     exit(1);
712   }
713   
714   // Loop over all the def's in the multiclass, instantiating each one.
715   for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
716     Record *DefProto = MC->DefPrototypes[i];
717     
718     // Add the suffix to the defm name to get the new name.
719     assert(CurRec == 0 && "A def is current?");
720     CurRec = new Record(*$2 + DefProto->getName());
721     
722     addSubClass(DefProto, std::vector<Init*>());
723     
724     // Loop over all of the template arguments, setting them to the specified
725     // value or leaving them as the default if necessary.
726     for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
727       if (i < TemplateVals.size()) { // A value is specified for this temp-arg?
728         // Set it now.
729         setValue(TArgs[i], 0, TemplateVals[i]);
730         
731         // Resolve it next.
732         CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
733         
734         // Now remove it.
735         CurRec->removeValue(TArgs[i]);
736         
737       } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
738         err() << "ERROR: Value not specified for template argument #"
739         << i << " (" << TArgs[i] << ") of multiclassclass '"
740         << MC->Rec.getName() << "'!\n";
741         exit(1);
742       }
743     }
744     
745     // Ensure redefinition doesn't happen.
746     if (Records.getDef(CurRec->getName())) {
747       err() << "def '" << CurRec->getName() << "' already defined, "
748             << "instantiating defm '" << *$2 << "' with subdef '"
749             << DefProto->getName() << "'!\n";
750       exit(1);
751     }
752     Records.addDef(CurRec);
753     
754     CurRec->resolveReferences();
755
756     CurRec = 0;
757   }
758   
759   delete &TemplateVals;
760   delete $2;
761   CurDefmPrefix = 0;
762 };
763
764 Object : ClassInst {} | DefInst {};
765 Object : MultiClassInst | DefMInst;
766
767 LETItem : ID OptBitList '=' Value {
768   LetStack.back().push_back(LetRecord(*$1, $2, $4));
769   delete $1; delete $2;
770 };
771
772 LETList : LETItem | LETList ',' LETItem;
773
774 // LETCommand - A 'LET' statement start...
775 LETCommand : LET { LetStack.push_back(std::vector<LetRecord>()); } LETList IN;
776
777 // Support Set commands wrapping objects... both with and without braces.
778 Object : LETCommand '{' ObjectList '}' {
779     LetStack.pop_back();
780   }
781   | LETCommand Object {
782     LetStack.pop_back();
783   };
784
785 ObjectList : Object {} | ObjectList Object {};
786
787 File : ObjectList;
788
789 %%
790
791 int yyerror(const char *ErrorMsg) {
792   err() << "Error parsing: " << ErrorMsg << "\n";
793   exit(1);
794 }