1 //===-- FileParser.y - Parser for TableGen files ----------------*- C++ -*-===//
3 // This file implements the bison parser for Table Generator files...
5 //===------------------------------------------------------------------------=//
9 #include "Support/StringExtras.h"
12 #define YYERROR_VERBOSE 1
14 int yyerror(const char *ErrorMsg);
16 extern int Filelineno;
17 static Record *CurRec = 0;
19 typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
23 std::vector<unsigned> Bits;
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;
32 static std::vector<std::vector<LetRecord> > LetStack;
35 extern std::ostream &err();
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";
51 static void addSuperClass(Record *SC) {
52 if (CurRec->isSubClassOf(SC)) {
53 err() << "Already subclass of '" << SC->getName() << "'!\n";
56 CurRec->addSuperClass(SC);
59 static void setValue(const std::string &ValName,
60 std::vector<unsigned> *BitList, Init *V) {
63 RecordVal *RV = CurRec->getValue(ValName);
65 err() << "Value '" << ValName << "' unknown!\n";
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
74 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
76 err() << "Value '" << ValName << "' is not a bits type!\n";
80 // Convert the incoming value to a bits type of the appropriate size...
81 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList->size()));
83 V->convertInitializerTo(new BitsRecTy(BitList->size()));
84 err() << "Initializer '" << *V << "' not compatible with bit range!\n";
88 // We should have a BitsInit type now...
89 assert(dynamic_cast<BitsInit*>(BI) != 0 || &(std::cerr << *BI) == 0);
90 BitsInit *BInit = (BitsInit*)BI;
92 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
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";
102 NewVal->setBit(Bit, BInit->getBit(i));
105 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
106 if (NewVal->getBit(i) == 0)
107 NewVal->setBit(i, CurVal->getBit(i));
112 if (RV->setValue(V)) {
113 err() << "Value '" << ValName << "' of type '" << *RV->getType()
114 << "' is incompatible with initializer '" << *V << "'!\n";
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)
125 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
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";
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?
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()
148 // Since everything went well, we can now set the "superclass" list for the
150 const std::vector<Record*> &SCs = SC->getSuperClasses();
151 for (unsigned i = 0, e = SCs.size(); i != e; ++i)
152 addSuperClass(SCs[i]);
164 std::vector<Init*> *FieldList;
165 std::vector<unsigned>*BitList;
167 SubClassRefTy *SubClassRef;
168 std::vector<SubClassRefTy> *SubClassList;
169 std::vector<std::pair<Init*, std::string> > *DagValueList;
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
177 %type <Rec> ClassInst DefInst Object ObjectBody ClassID
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 OptVarName
192 $$ = Records.getClass(*$1);
194 err() << "Couldn't find class '" << *$1 << "'!\n";
202 Type : STRING { // string type
203 $$ = new StringRecTy();
204 } | BIT { // bit type
206 } | BITS '<' INTVAL '>' { // bits<x> type
207 $$ = new BitsRecTy($3);
208 } | INT { // int type
210 } | LIST '<' Type '>' { // list<x> type
211 $$ = new ListRecTy($3);
212 } | CODE { // code type
213 $$ = new CodeRecTy();
214 } | DAG { // dag type
216 } | ClassID { // Record Type
217 $$ = new RecordRecTy($1);
220 OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; };
222 OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; };
225 $$ = new IntInit($1);
227 $$ = new StringInit(*$1);
230 $$ = new CodeInit(*$1);
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());
239 err() << "Element #" << i << " (" << *(*$2)[i]
240 << ") is not convertable to a bit!\n";
243 Init->setBit($2->size()-i-1, Bit);
248 if (const RecordVal *RV = (CurRec ? CurRec->getValue(*$1) : 0)) {
249 $$ = new VarInit(*$1, RV->getType());
250 } else if (Record *D = Records.getDef(*$1)) {
253 err() << "Variable not defined: '" << *$1 << "'!\n";
258 } | Value '{' BitList '}' {
259 $$ = $1->convertInitializerBitRange(*$3);
261 err() << "Invalid bit range for value '" << *$1 << "'!\n";
265 } | '[' ValueList ']' {
266 $$ = new ListInit(*$2);
269 if (!$1->getFieldType(*$3)) {
270 err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n";
273 $$ = new FieldInit($1, *$3);
275 } | '(' ID DagArgList ')' {
276 Record *D = Records.getDef(*$2);
278 err() << "Invalid def '" << *$2 << "'!\n";
281 $$ = new DagInit(D, *$3);
282 delete $2; delete $3;
285 OptVarName : /* empty */ {
286 $$ = new std::string();
292 DagArgListNE : Value OptVarName {
293 $$ = new std::vector<std::pair<Init*, std::string> >();
294 $$->push_back(std::make_pair($1, *$2));
297 | DagArgListNE ',' Value OptVarName {
298 $1->push_back(std::make_pair($3, *$4));
303 DagArgList : /*empty*/ {
304 $$ = new std::vector<std::pair<Init*, std::string> >();
306 | DagArgListNE { $$ = $1; };
310 $$ = new std::vector<unsigned>();
312 } | INTVAL '-' INTVAL {
313 if ($1 < $3 || $1 < 0 || $3 < 0) {
314 err() << "Invalid bit range: " << $1 << "-" << $3 << "!\n";
317 $$ = new std::vector<unsigned>();
318 for (int i = $1; i >= $3; --i)
322 if ($1 < $2 || $1 < 0 || $2 < 0) {
323 err() << "Invalid bit range: " << $1 << "-" << $2 << "!\n";
326 $$ = new std::vector<unsigned>();
327 for (int i = $1; i >= $2; --i)
329 } | RBitList ',' INTVAL {
330 ($$=$1)->push_back($3);
331 } | RBitList ',' INTVAL '-' INTVAL {
332 if ($3 < $5 || $3 < 0 || $5 < 0) {
333 err() << "Invalid bit range: " << $3 << "-" << $5 << "!\n";
337 for (int i = $3; i >= $5; --i)
339 } | RBitList ',' INTVAL INTVAL {
341 if ($3 < $4 || $3 < 0 || $4 < 0) {
342 err() << "Invalid bit range: " << $3 << "-" << $4 << "!\n";
346 for (int i = $3; i >= $4; --i)
350 BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); };
352 OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; };
356 ValueList : /*empty*/ {
357 $$ = new std::vector<Init*>();
362 ValueListNE : Value {
363 $$ = new std::vector<Init*>();
365 } | ValueListNE ',' Value {
366 ($$ = $1)->push_back($3);
369 Declaration : OptPrefix Type ID OptValue {
370 addValue(RecordVal(*$3, $2, $1));
371 setValue(*$3, 0, $4);
375 BodyItem : Declaration ';' {
377 } | LET ID OptBitList '=' Value ';' {
378 setValue(*$2, $3, $5);
383 BodyList : /*empty*/ | BodyList BodyItem;
384 Body : ';' | '{' BodyList '}';
386 SubClassRef : ClassID {
387 $$ = new SubClassRefTy($1, new std::vector<Init*>());
388 } | ClassID '<' ValueListNE '>' {
389 $$ = new SubClassRefTy($1, $3);
392 ClassListNE : SubClassRef {
393 $$ = new std::vector<SubClassRefTy>();
397 | ClassListNE ',' SubClassRef {
398 ($$=$1)->push_back(*$3);
402 ClassList : /*empty */ {
403 $$ = new std::vector<SubClassRefTy>();
409 DeclListNE : Declaration {
410 CurRec->addTemplateArg(*$1);
412 } | DeclListNE ',' Declaration {
413 CurRec->addTemplateArg(*$3);
417 TemplateArgList : '<' DeclListNE '>' {};
418 OptTemplateArgList : /*empty*/ | TemplateArgList;
420 OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); };
423 static unsigned AnonCounter = 0;
425 *$1 = "anonymous."+utostr(AnonCounter++);
426 CurRec = new Record(*$1);
428 } OptTemplateArgList ClassList {
429 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
430 addSubClass((*$4)[i].first, *(*$4)[i].second);
431 // Delete the template arg values for the class
432 delete (*$4)[i].second;
435 // Process any variables on the set stack...
436 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
437 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
438 setValue(LetStack[i][j].Name,
439 LetStack[i][j].HasBits ? &LetStack[i][j].Bits : 0,
440 LetStack[i][j].Value);
442 CurRec->resolveReferences();
444 // Now that all of the references have been resolved, we can delete template
445 // arguments for superclasses, so they don't pollute our record, and so that
446 // their names won't conflict with later uses of the name...
447 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
448 Record *SuperClass = (*$4)[i].first;
449 for (unsigned i = 0, e = SuperClass->getTemplateArgs().size(); i != e; ++i)
450 CurRec->removeValue(SuperClass->getTemplateArgs()[i]);
452 delete $4; // Delete the class list...
458 ClassInst : CLASS ObjectBody {
459 if (Records.getClass($2->getName())) {
460 err() << "Class '" << $2->getName() << "' already defined!\n";
463 Records.addClass($$ = $2);
466 DefInst : DEF ObjectBody {
467 if (!$2->getTemplateArgs().empty()) {
468 err() << "Def '" << $2->getName()
469 << "' is not permitted to have template arguments!\n";
472 // If ObjectBody has template arguments, it's an error.
473 if (Records.getDef($2->getName())) {
474 err() << "Def '" << $2->getName() << "' already defined!\n";
477 Records.addDef($$ = $2);
481 Object : ClassInst | DefInst;
483 LETItem : ID OptBitList '=' Value {
484 LetStack.back().push_back(LetRecord(*$1, $2, $4));
485 delete $1; delete $2;
488 LETList : LETItem | LETList ',' LETItem;
490 // LETCommand - A 'LET' statement start...
491 LETCommand : LET { LetStack.push_back(std::vector<LetRecord>()); } LETList IN;
493 // Support Set commands wrapping objects... both with and without braces.
494 Object : LETCommand '{' ObjectList '}' {
497 | LETCommand Object {
501 ObjectList : Object {} | ObjectList Object {};
503 File : ObjectList {};
507 int yyerror(const char *ErrorMsg) {
508 err() << "Error parsing: " << ErrorMsg << "\n";