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