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