1 //===-- FileParser.y - Parser for TableGen files ----------------*- C++ -*-===//
3 // This file implements the bison parser for Table Generator files...
5 //===------------------------------------------------------------------------=//
13 #define YYERROR_VERBOSE 1
15 int yyerror(const char *ErrorMsg);
18 extern int Filelineno;
20 static Record *CurRec = 0;
22 typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
24 static std::vector<std::pair<std::pair<std::string, std::vector<unsigned>*>,
36 static std::ostream &err() {
37 return std::cerr << "Parsing Line #" << Filelineno << ": ";
40 static void addValue(const RecordVal &RV) {
41 if (CurRec->getValue(RV.getName())) {
42 err() << "Value '" << RV.getName() << "' multiply defined!\n";
49 static void addSuperClass(Record *SC) {
50 if (CurRec->isSubClassOf(SC)) {
51 err() << "Already subclass of '" << SC->getName() << "'!\n";
54 CurRec->addSuperClass(SC);
57 static void setValue(const std::string &ValName,
58 std::vector<unsigned> *BitList, Init *V) {
61 RecordVal *RV = CurRec->getValue(ValName);
63 err() << "Value '" << ValName << "' unknown!\n";
67 // If we are assigning to a subset of the bits in the value... then we must be
68 // assigning to a field of BitsRecTy, which must have a BitsInit
72 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
74 err() << "Value '" << ValName << "' is not a bits type!\n";
78 // Convert the incoming value to a bits type of the appropriate size...
79 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList->size()));
81 V->convertInitializerTo(new BitsRecTy(BitList->size()));
82 err() << "Initializer '" << *V << "' not compatible with bit range!\n";
86 // We should have a BitsInit type now...
87 assert(dynamic_cast<BitsInit*>(BI) != 0 || &(std::cerr << *BI) == 0);
88 BitsInit *BInit = (BitsInit*)BI;
90 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
92 // Loop over bits, assigning values as appropriate...
93 for (unsigned i = 0, e = BitList->size(); i != e; ++i) {
94 unsigned Bit = (*BitList)[i];
95 if (NewVal->getBit(Bit)) {
96 err() << "Cannot set bit #" << Bit << " of value '" << ValName
97 << "' more than once!\n";
100 NewVal->setBit(Bit, BInit->getBit(i));
103 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
104 if (NewVal->getBit(i) == 0)
105 NewVal->setBit(i, CurVal->getBit(i));
110 if (RV->setValue(V)) {
111 err() << "Value '" << ValName << "' of type '" << *RV->getType()
112 << "' is incompatible with initializer '" << *V << "'!\n";
117 static void addSubClass(Record *SC, const std::vector<Init*> &TemplateArgs) {
118 // Add all of the values in the subclass into the current class...
119 const std::vector<RecordVal> &Vals = SC->getValues();
120 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
123 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
125 // Ensure that an appropriate number of template arguments are specified...
126 if (TArgs.size() < TemplateArgs.size()) {
127 err() << "ERROR: More template args specified thang expected!\n";
129 } else { // This class expects template arguments...
130 // Loop over all of the template arguments, setting them to the specified
131 // value or leaving them as the default as neccesary.
132 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
133 if (i < TemplateArgs.size()) { // A value is specified for this temp-arg?
135 setValue(TArgs[i], 0, TemplateArgs[i]);
136 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
137 err() << "ERROR: Value not specified for template argument #"
138 << i << " (" << TArgs[i] << ") of subclass '" << SC->getName()
146 // Since everything went well, we can now set the "superclass" list for the
148 const std::vector<Record*> &SCs = SC->getSuperClasses();
149 for (unsigned i = 0, e = SCs.size(); i != e; ++i)
150 addSuperClass(SCs[i]);
162 std::vector<Init*> *FieldList;
163 std::vector<Record*> *RecPtr;
164 std::vector<unsigned>*BitList;
166 SubClassRefTy *SubClassRef;
167 std::vector<SubClassRefTy> *SubClassList;
170 %token INT BIT STRING BITS LIST CLASS DEF FIELD SET IN
171 %token <IntVal> INTVAL
172 %token <StrVal> ID STRVAL
175 %type <RecPtr> DefList DefListNE
176 %type <Rec> ClassInst DefInst Object ObjectBody ClassID DefID
178 %type <SubClassRef> SubClassRef
179 %type <SubClassList> ClassList ClassListNE
180 %type <IntVal> OptPrefix
181 %type <Initializer> Value OptValue
182 %type <FieldList> ValueList ValueListNE
183 %type <BitList> BitList OptBitList RBitList
184 %type <StrVal> Declaration
190 $$ = Records.getClass(*$1);
192 err() << "Couldn't find class '" << *$1 << "'!\n";
199 $$ = Records.getDef(*$1);
201 err() << "Couldn't find def '" << *$1 << "'!\n";
209 Type : STRING { // string type
210 $$ = new StringRecTy();
211 } | BIT { // bit type
213 } | BITS '<' INTVAL '>' { // bits<x> type
214 $$ = new BitsRecTy($3);
215 } | INT { // int type
217 } | LIST '<' ClassID '>' { // list<x> type
218 $$ = new ListRecTy($3);
219 } | ClassID { // Record Type
220 $$ = new RecordRecTy($1);
223 OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; };
225 OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; };
228 $$ = new IntInit($1);
230 $$ = new StringInit(*$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->getValue(*$1)) {
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 } | '[' DefList ']' {
266 $$ = new ListInit(*$2);
269 if (!$1->getFieldType(*$3)) {
270 err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n";
273 $$ = new FieldInit($1, *$3);
277 DefList : /*empty */ {
278 $$ = new std::vector<Record*>();
283 $$ = new std::vector<Record*>();
285 } | DefListNE ',' DefID {
286 ($$=$1)->push_back($3);
291 $$ = new std::vector<unsigned>();
293 } | INTVAL '-' INTVAL {
294 if ($1 < $3 || $1 < 0 || $3 < 0) {
295 err() << "Invalid bit range: " << $1 << "-" << $3 << "!\n";
298 $$ = new std::vector<unsigned>();
299 for (int i = $1; i >= $3; --i)
303 if ($1 < $2 || $1 < 0 || $2 < 0) {
304 err() << "Invalid bit range: " << $1 << "-" << $2 << "!\n";
307 $$ = new std::vector<unsigned>();
308 for (int i = $1; i >= $2; --i)
310 } | RBitList ',' INTVAL {
311 ($$=$1)->push_back($3);
312 } | RBitList ',' INTVAL '-' INTVAL {
313 if ($3 < $5 || $3 < 0 || $5 < 0) {
314 err() << "Invalid bit range: " << $3 << "-" << $5 << "!\n";
318 for (int i = $3; i >= $5; --i)
320 } | RBitList ',' INTVAL INTVAL {
322 if ($3 < $4 || $3 < 0 || $4 < 0) {
323 err() << "Invalid bit range: " << $3 << "-" << $4 << "!\n";
327 for (int i = $3; i >= $4; --i)
331 BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); };
333 OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; };
337 ValueList : /*empty*/ {
338 $$ = new std::vector<Init*>();
343 ValueListNE : Value {
344 $$ = new std::vector<Init*>();
346 } | ValueListNE ',' Value {
347 ($$ = $1)->push_back($3);
350 Declaration : OptPrefix Type ID OptValue {
351 addValue(RecordVal(*$3, $2, $1));
352 setValue(*$3, 0, $4);
356 BodyItem : Declaration ';' {
358 } | SET ID OptBitList '=' Value ';' {
359 setValue(*$2, $3, $5);
364 BodyList : /*empty*/ | BodyList BodyItem;
365 Body : ';' | '{' BodyList '}';
367 SubClassRef : ClassID {
368 $$ = new SubClassRefTy($1, new std::vector<Init*>());
369 } | ClassID '<' ValueListNE '>' {
370 $$ = new SubClassRefTy($1, $3);
373 ClassListNE : SubClassRef {
374 $$ = new std::vector<SubClassRefTy>();
378 | ClassListNE ',' SubClassRef {
379 ($$=$1)->push_back(*$3);
383 ClassList : /*empty */ {
384 $$ = new std::vector<SubClassRefTy>();
390 DeclListNE : Declaration {
391 CurRec->addTemplateArg(*$1);
393 } | DeclListNE ',' Declaration {
394 CurRec->addTemplateArg(*$3);
398 TemplateArgList : '<' DeclListNE '>' {};
399 OptTemplateArgList : /*empty*/ | TemplateArgList;
402 CurRec = new Record(*$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 (*$4)[i].second; // Delete the template list
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,
416 CurRec->resolveReferences();
421 ClassInst : CLASS ObjectBody {
422 if (Records.getClass($2->getName())) {
423 err() << "Class '" << $2->getName() << "' already defined!\n";
426 Records.addClass($$ = $2);
429 DefInst : DEF ObjectBody {
430 // TODO: If ObjectBody has template arguments, it's an error.
431 if (Records.getDef($2->getName())) {
432 err() << "Def '" << $2->getName() << "' already defined!\n";
435 Records.addDef($$ = $2);
439 Object : ClassInst | DefInst;
441 // Support Set commands wrapping objects...
442 Object : SET ID OptBitList '=' Value IN {
443 SetStack.push_back(std::make_pair(std::make_pair(*$2, $3), $5));
445 } '{' ObjectList '}' {
446 delete SetStack.back().first.second; // Delete OptBitList
450 ObjectList : Object {} | ObjectList Object {};
452 File : ObjectList {};
456 int yyerror(const char *ErrorMsg) {
457 err() << "Error parsing: " << ErrorMsg << "\n";