d248242b2d6be08ad7f40fc0c9c277b4db854eff
[oota-llvm.git] / utils / TableGen / FileParser.y
1 //===-- FileParser.y - Parser for TableGen files ----------------*- C++ -*-===//
2 //
3 //  This file implements the bison parser for Table Generator files...
4 //
5 //===------------------------------------------------------------------------=//
6
7 %{
8 #include "Record.h"
9 #include "Support/StringExtras.h"
10 #include <algorithm>
11 #include <cstdio>
12 #define YYERROR_VERBOSE 1
13
14 int yyerror(const char *ErrorMsg);
15 int yylex();
16 extern int Filelineno;
17 static Record *CurRec = 0;
18
19 typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
20
21 static std::vector<std::pair<std::pair<std::string, std::vector<unsigned>*>,
22                              Init*> > SetStack;
23
24 extern std::ostream &err();
25
26 static void addValue(const RecordVal &RV) {
27   if (CurRec->getValue(RV.getName())) {
28     err() << "Value '" << RV.getName() << "' multiply defined!\n";
29     abort();
30   }
31
32   CurRec->addValue(RV);
33 }
34
35 static void addSuperClass(Record *SC) {
36   if (CurRec->isSubClassOf(SC)) {
37     err() << "Already subclass of '" << SC->getName() << "'!\n";
38     abort();
39   }
40   CurRec->addSuperClass(SC);
41 }
42
43 static void setValue(const std::string &ValName, 
44                      std::vector<unsigned> *BitList, Init *V) {
45   if (!V) return ;
46
47   RecordVal *RV = CurRec->getValue(ValName);
48   if (RV == 0) {
49     err() << "Value '" << ValName << "' unknown!\n";
50     abort();
51   }
52   
53   // If we are assigning to a subset of the bits in the value... then we must be
54   // assigning to a field of BitsRecTy, which must have a BitsInit
55   // initializer...
56   //
57   if (BitList) {
58     BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
59     if (CurVal == 0) {
60       err() << "Value '" << ValName << "' is not a bits type!\n";
61       abort();
62     }
63
64     // Convert the incoming value to a bits type of the appropriate size...
65     Init *BI = V->convertInitializerTo(new BitsRecTy(BitList->size()));
66     if (BI == 0) {
67       V->convertInitializerTo(new BitsRecTy(BitList->size()));
68       err() << "Initializer '" << *V << "' not compatible with bit range!\n";
69       abort();
70     }
71
72     // We should have a BitsInit type now...
73     assert(dynamic_cast<BitsInit*>(BI) != 0 || &(std::cerr << *BI) == 0);
74     BitsInit *BInit = (BitsInit*)BI;
75
76     BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
77
78     // Loop over bits, assigning values as appropriate...
79     for (unsigned i = 0, e = BitList->size(); i != e; ++i) {
80       unsigned Bit = (*BitList)[i];
81       if (NewVal->getBit(Bit)) {
82         err() << "Cannot set bit #" << Bit << " of value '" << ValName
83               << "' more than once!\n";
84         abort();
85       }
86       NewVal->setBit(Bit, BInit->getBit(i));
87     }
88
89     for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
90       if (NewVal->getBit(i) == 0)
91         NewVal->setBit(i, CurVal->getBit(i));
92
93     V = NewVal;
94   }
95
96   if (RV->setValue(V)) {
97     err() << "Value '" << ValName << "' of type '" << *RV->getType()
98           << "' is incompatible with initializer '" << *V << "'!\n";
99     abort();
100   }
101 }
102
103 static void addSubClass(Record *SC, const std::vector<Init*> &TemplateArgs) {
104   // Add all of the values in the subclass into the current class...
105   const std::vector<RecordVal> &Vals = SC->getValues();
106   for (unsigned i = 0, e = Vals.size(); i != e; ++i)
107     addValue(Vals[i]);
108
109   const std::vector<std::string> &TArgs = SC->getTemplateArgs();
110
111   // Ensure that an appropriate number of template arguments are specified...
112   if (TArgs.size() < TemplateArgs.size()) {
113     err() << "ERROR: More template args specified than expected!\n";
114     abort();
115   } else {    // This class expects template arguments...
116     // Loop over all of the template arguments, setting them to the specified
117     // value or leaving them as the default as neccesary.
118     for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
119       if (i < TemplateArgs.size()) {  // A value is specified for this temp-arg?
120         // Set it now.
121         setValue(TArgs[i], 0, TemplateArgs[i]);
122       } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
123         err() << "ERROR: Value not specified for template argument #"
124               << i << " (" << TArgs[i] << ") of subclass '" << SC->getName()
125               << "'!\n";
126         abort();
127       }
128     }
129   }
130
131
132   // Since everything went well, we can now set the "superclass" list for the
133   // current record.
134   const std::vector<Record*>   &SCs  = SC->getSuperClasses();
135   for (unsigned i = 0, e = SCs.size(); i != e; ++i)
136     addSuperClass(SCs[i]);
137   addSuperClass(SC);
138 }
139
140
141 %}
142
143 %union {
144   std::string          *StrVal;
145   int                   IntVal;
146   RecTy                *Ty;
147   Init                 *Initializer;
148   std::vector<Init*>   *FieldList;
149   std::vector<Record*> *RecPtr;
150   std::vector<unsigned>*BitList;
151   Record               *Rec;
152   SubClassRefTy        *SubClassRef;
153   std::vector<SubClassRefTy> *SubClassList;
154 };
155
156 %token INT BIT STRING BITS LIST CODE CLASS DEF FIELD SET IN
157 %token <IntVal>      INTVAL
158 %token <StrVal>      ID STRVAL CODEFRAGMENT
159
160 %type <Ty>           Type
161 %type <RecPtr>       DefList DefListNE
162 %type <Rec>          ClassInst DefInst Object ObjectBody ClassID DefID
163
164 %type <SubClassRef>  SubClassRef
165 %type <SubClassList> ClassList ClassListNE
166 %type <IntVal>       OptPrefix
167 %type <Initializer>  Value OptValue
168 %type <FieldList>    ValueList ValueListNE
169 %type <BitList>      BitList OptBitList RBitList
170 %type <StrVal>       Declaration OptID
171
172 %start File
173 %%
174
175 ClassID : ID {
176     $$ = Records.getClass(*$1);
177     if ($$ == 0) {
178       err() << "Couldn't find class '" << *$1 << "'!\n";
179       abort();
180     }
181     delete $1;
182   };
183
184 DefID : ID {
185     $$ = Records.getDef(*$1);
186     if ($$ == 0) {
187       err() << "Couldn't find def '" << *$1 << "'!\n";
188       abort();
189     }
190     delete $1;
191   };
192
193
194 // TableGen types...
195 Type : STRING {                       // string type
196     $$ = new StringRecTy();
197   } | BIT {                           // bit type
198     $$ = new BitRecTy();
199   } | BITS '<' INTVAL '>' {           // bits<x> type
200     $$ = new BitsRecTy($3);
201   } | INT {                           // int type
202     $$ = new IntRecTy();
203   } | LIST '<' ClassID '>' {          // list<x> type
204     $$ = new ListRecTy($3);
205   } | CODE {                          // code type
206     $$ = new CodeRecTy();
207   } | ClassID {                       // Record Type
208     $$ = new RecordRecTy($1);
209   };
210
211 OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; };
212
213 OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; };
214
215 Value : INTVAL {
216     $$ = new IntInit($1);
217   } | STRVAL {
218     $$ = new StringInit(*$1);
219     delete $1;
220   } | CODEFRAGMENT {
221     $$ = new CodeInit(*$1);
222     delete $1;
223   } | '?' {
224     $$ = new UnsetInit();
225   } | '{' ValueList '}' {
226     BitsInit *Init = new BitsInit($2->size());
227     for (unsigned i = 0, e = $2->size(); i != e; ++i) {
228       struct Init *Bit = (*$2)[i]->convertInitializerTo(new BitRecTy());
229       if (Bit == 0) {
230         err() << "Element #" << i << " (" << *(*$2)[i]
231               << ") is not convertable to a bit!\n";
232         abort();
233       }
234       Init->setBit($2->size()-i-1, Bit);
235     }
236     $$ = Init;
237     delete $2;
238   } | ID {
239     if (CurRec == 0) {
240       err() << "Def/Class name '" << *$1 << "' not allowed here!\n";
241       abort();
242     }
243     if (const RecordVal *RV = CurRec->getValue(*$1)) {
244       $$ = new VarInit(*$1, RV->getType());
245     } else if (Record *D = Records.getDef(*$1)) {
246       $$ = new DefInit(D);
247     } else {
248       err() << "Variable not defined: '" << *$1 << "'!\n";
249       abort();
250     }
251     
252     delete $1;
253   } | Value '{' BitList '}' {
254     $$ = $1->convertInitializerBitRange(*$3);
255     if ($$ == 0) {
256       err() << "Invalid bit range for value '" << *$1 << "'!\n";
257       abort();
258     }
259     delete $3;
260   } | '[' DefList ']' {
261     $$ = new ListInit(*$2);
262     delete $2;
263   } | Value '.' ID {
264     if (!$1->getFieldType(*$3)) {
265       err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n";
266       abort();
267     }
268     $$ = new FieldInit($1, *$3);
269     delete $3;
270   };
271
272 DefList : /*empty */ {
273     $$ = new std::vector<Record*>();
274   } | DefListNE {
275     $$ = $1;
276   };
277 DefListNE : DefID {
278     $$ = new std::vector<Record*>();
279     $$->push_back($1);
280   } | DefListNE ',' DefID {
281     ($$=$1)->push_back($3);
282   };
283
284
285 RBitList : INTVAL {
286     $$ = new std::vector<unsigned>();
287     $$->push_back($1);
288   } | INTVAL '-' INTVAL {
289     if ($1 < $3 || $1 < 0 || $3 < 0) {
290       err() << "Invalid bit range: " << $1 << "-" << $3 << "!\n";
291       abort();
292     }
293     $$ = new std::vector<unsigned>();
294     for (int i = $1; i >= $3; --i)
295       $$->push_back(i);
296   } | INTVAL INTVAL {
297     $2 = -$2;
298     if ($1 < $2 || $1 < 0 || $2 < 0) {
299       err() << "Invalid bit range: " << $1 << "-" << $2 << "!\n";
300       abort();
301     }
302     $$ = new std::vector<unsigned>();
303     for (int i = $1; i >= $2; --i)
304       $$->push_back(i);
305   } | RBitList ',' INTVAL {
306     ($$=$1)->push_back($3);
307   } | RBitList ',' INTVAL '-' INTVAL {
308     if ($3 < $5 || $3 < 0 || $5 < 0) {
309       err() << "Invalid bit range: " << $3 << "-" << $5 << "!\n";
310       abort();
311     }
312     $$ = $1;
313     for (int i = $3; i >= $5; --i)
314       $$->push_back(i);
315   } | RBitList ',' INTVAL INTVAL {
316     $4 = -$4;
317     if ($3 < $4 || $3 < 0 || $4 < 0) {
318       err() << "Invalid bit range: " << $3 << "-" << $4 << "!\n";
319       abort();
320     }
321     $$ = $1;
322     for (int i = $3; i >= $4; --i)
323       $$->push_back(i);
324   };
325
326 BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); };
327
328 OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; };
329
330
331
332 ValueList : /*empty*/ {
333     $$ = new std::vector<Init*>();
334   } | ValueListNE {
335     $$ = $1;
336   };
337
338 ValueListNE : Value {
339     $$ = new std::vector<Init*>();
340     $$->push_back($1);
341   } | ValueListNE ',' Value {
342     ($$ = $1)->push_back($3);
343   };
344
345 Declaration : OptPrefix Type ID OptValue {
346   addValue(RecordVal(*$3, $2, $1));
347   setValue(*$3, 0, $4);
348   $$ = $3;
349 };
350
351 BodyItem : Declaration ';' {
352   delete $1;
353 } | SET ID OptBitList '=' Value ';' {
354   setValue(*$2, $3, $5);
355   delete $2;
356   delete $3;
357 };
358
359 BodyList : /*empty*/ | BodyList BodyItem;
360 Body : ';' | '{' BodyList '}';
361
362 SubClassRef : ClassID {
363     $$ = new SubClassRefTy($1, new std::vector<Init*>());
364   } | ClassID '<' ValueListNE '>' {
365     $$ = new SubClassRefTy($1, $3);
366   };
367
368 ClassListNE : SubClassRef {
369     $$ = new std::vector<SubClassRefTy>();
370     $$->push_back(*$1);
371     delete $1;
372   }
373   | ClassListNE ',' SubClassRef {
374     ($$=$1)->push_back(*$3);
375     delete $3;
376   };
377
378 ClassList : /*empty */ {
379     $$ = new std::vector<SubClassRefTy>();
380   }
381   | ':' ClassListNE {
382     $$ = $2;
383   };
384
385 DeclListNE : Declaration {
386   CurRec->addTemplateArg(*$1);
387   delete $1;
388 } | DeclListNE ',' Declaration {
389   CurRec->addTemplateArg(*$3);
390   delete $3;
391 };
392
393 TemplateArgList : '<' DeclListNE '>' {};
394 OptTemplateArgList : /*empty*/ | TemplateArgList;
395
396 OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); };
397
398 ObjectBody : OptID {
399            static unsigned AnonCounter = 0;
400            if ($1->empty())
401              *$1 = "anonymous."+utostr(AnonCounter++);
402            CurRec = new Record(*$1);
403            delete $1;
404          } OptTemplateArgList ClassList {
405            for (unsigned i = 0, e = $4->size(); i != e; ++i) {
406              addSubClass((*$4)[i].first, *(*$4)[i].second);
407              // Delete the template arg values for the class
408              delete (*$4)[i].second;
409            }
410
411            // Process any variables on the set stack...
412            for (unsigned i = 0, e = SetStack.size(); i != e; ++i)
413              setValue(SetStack[i].first.first, SetStack[i].first.second,
414                       SetStack[i].second);
415          } Body {
416   CurRec->resolveReferences();
417
418   // Now that all of the references have been resolved, we can delete template
419   // arguments for superclasses, so they don't pollute our record, and so that
420   // their names won't conflict with later uses of the name...
421   for (unsigned i = 0, e = $4->size(); i != e; ++i) {
422     Record *SuperClass = (*$4)[i].first;
423     for (unsigned i = 0, e = SuperClass->getTemplateArgs().size(); i != e; ++i)
424     CurRec->removeValue(SuperClass->getTemplateArgs()[i]);
425   }
426   delete $4;   // Delete the class list...
427
428   $$ = CurRec;
429   CurRec = 0;
430 };
431
432 ClassInst : CLASS ObjectBody {
433   if (Records.getClass($2->getName())) {
434     err() << "Class '" << $2->getName() << "' already defined!\n";
435     abort();
436   }
437   Records.addClass($$ = $2);
438 };
439
440 DefInst : DEF ObjectBody {
441   if (!$2->getTemplateArgs().empty()) {
442     err() << "Def '" << $2->getName()
443           << "' is not permitted to have template arguments!\n";
444     abort();
445   }
446   // If ObjectBody has template arguments, it's an error.
447   if (Records.getDef($2->getName())) {
448     err() << "Def '" << $2->getName() << "' already defined!\n";
449     abort();
450   }
451   Records.addDef($$ = $2);
452 };
453
454
455 Object : ClassInst | DefInst;
456
457 // SETCommand - A 'SET' statement start...
458 SETCommand : SET ID OptBitList '=' Value IN {
459   SetStack.push_back(std::make_pair(std::make_pair(*$2, $3), $5));
460   delete $2;
461 };
462
463 // Support Set commands wrapping objects... both with and without braces.
464 Object : SETCommand '{' ObjectList '}' {
465     delete SetStack.back().first.second; // Delete OptBitList
466     SetStack.pop_back();
467   }
468   | SETCommand Object {
469     delete SetStack.back().first.second; // Delete OptBitList
470     SetStack.pop_back();
471   };
472
473 ObjectList : Object {} | ObjectList Object {};
474
475 File : ObjectList {};
476
477 %%
478
479 int yyerror(const char *ErrorMsg) {
480   err() << "Error parsing: " << ErrorMsg << "\n";
481   abort();
482 }