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 SetRecord(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<SetRecord> > SetStack;
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;
171 %token INT BIT STRING BITS LIST CODE CLASS DEF FIELD SET IN
172 %token <IntVal> INTVAL
173 %token <StrVal> ID STRVAL CODEFRAGMENT
176 %type <Rec> ClassInst DefInst Object ObjectBody ClassID
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 OptID
190 $$ = Records.getClass(*$1);
192 err() << "Couldn't find class '" << *$1 << "'!\n";
200 Type : STRING { // string type
201 $$ = new StringRecTy();
202 } | BIT { // bit type
204 } | BITS '<' INTVAL '>' { // bits<x> type
205 $$ = new BitsRecTy($3);
206 } | INT { // int type
208 } | LIST '<' Type '>' { // list<x> type
209 $$ = new ListRecTy($3);
210 } | CODE { // code type
211 $$ = new CodeRecTy();
212 } | ClassID { // Record Type
213 $$ = new RecordRecTy($1);
216 OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; };
218 OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; };
221 $$ = new IntInit($1);
223 $$ = new StringInit(*$1);
226 $$ = new CodeInit(*$1);
229 $$ = new UnsetInit();
230 } | '{' ValueList '}' {
231 BitsInit *Init = new BitsInit($2->size());
232 for (unsigned i = 0, e = $2->size(); i != e; ++i) {
233 struct Init *Bit = (*$2)[i]->convertInitializerTo(new BitRecTy());
235 err() << "Element #" << i << " (" << *(*$2)[i]
236 << ") is not convertable to a bit!\n";
239 Init->setBit($2->size()-i-1, Bit);
244 if (const RecordVal *RV = (CurRec ? CurRec->getValue(*$1) : 0)) {
245 $$ = new VarInit(*$1, RV->getType());
246 } else if (Record *D = Records.getDef(*$1)) {
249 err() << "Variable not defined: '" << *$1 << "'!\n";
254 } | Value '{' BitList '}' {
255 $$ = $1->convertInitializerBitRange(*$3);
257 err() << "Invalid bit range for value '" << *$1 << "'!\n";
261 } | '[' ValueList ']' {
262 $$ = new ListInit(*$2);
265 if (!$1->getFieldType(*$3)) {
266 err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n";
269 $$ = new FieldInit($1, *$3);
274 $$ = new std::vector<unsigned>();
276 } | INTVAL '-' INTVAL {
277 if ($1 < $3 || $1 < 0 || $3 < 0) {
278 err() << "Invalid bit range: " << $1 << "-" << $3 << "!\n";
281 $$ = new std::vector<unsigned>();
282 for (int i = $1; i >= $3; --i)
286 if ($1 < $2 || $1 < 0 || $2 < 0) {
287 err() << "Invalid bit range: " << $1 << "-" << $2 << "!\n";
290 $$ = new std::vector<unsigned>();
291 for (int i = $1; i >= $2; --i)
293 } | RBitList ',' INTVAL {
294 ($$=$1)->push_back($3);
295 } | RBitList ',' INTVAL '-' INTVAL {
296 if ($3 < $5 || $3 < 0 || $5 < 0) {
297 err() << "Invalid bit range: " << $3 << "-" << $5 << "!\n";
301 for (int i = $3; i >= $5; --i)
303 } | RBitList ',' INTVAL INTVAL {
305 if ($3 < $4 || $3 < 0 || $4 < 0) {
306 err() << "Invalid bit range: " << $3 << "-" << $4 << "!\n";
310 for (int i = $3; i >= $4; --i)
314 BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); };
316 OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; };
320 ValueList : /*empty*/ {
321 $$ = new std::vector<Init*>();
326 ValueListNE : Value {
327 $$ = new std::vector<Init*>();
329 } | ValueListNE ',' Value {
330 ($$ = $1)->push_back($3);
333 Declaration : OptPrefix Type ID OptValue {
334 addValue(RecordVal(*$3, $2, $1));
335 setValue(*$3, 0, $4);
339 BodyItem : Declaration ';' {
341 } | SET ID OptBitList '=' Value ';' {
342 setValue(*$2, $3, $5);
347 BodyList : /*empty*/ | BodyList BodyItem;
348 Body : ';' | '{' BodyList '}';
350 SubClassRef : ClassID {
351 $$ = new SubClassRefTy($1, new std::vector<Init*>());
352 } | ClassID '<' ValueListNE '>' {
353 $$ = new SubClassRefTy($1, $3);
356 ClassListNE : SubClassRef {
357 $$ = new std::vector<SubClassRefTy>();
361 | ClassListNE ',' SubClassRef {
362 ($$=$1)->push_back(*$3);
366 ClassList : /*empty */ {
367 $$ = new std::vector<SubClassRefTy>();
373 DeclListNE : Declaration {
374 CurRec->addTemplateArg(*$1);
376 } | DeclListNE ',' Declaration {
377 CurRec->addTemplateArg(*$3);
381 TemplateArgList : '<' DeclListNE '>' {};
382 OptTemplateArgList : /*empty*/ | TemplateArgList;
384 OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); };
387 static unsigned AnonCounter = 0;
389 *$1 = "anonymous."+utostr(AnonCounter++);
390 CurRec = new Record(*$1);
392 } OptTemplateArgList ClassList {
393 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
394 addSubClass((*$4)[i].first, *(*$4)[i].second);
395 // Delete the template arg values for the class
396 delete (*$4)[i].second;
399 // Process any variables on the set stack...
400 for (unsigned i = 0, e = SetStack.size(); i != e; ++i)
401 for (unsigned j = 0, e = SetStack[i].size(); j != e; ++j)
402 setValue(SetStack[i][j].Name,
403 SetStack[i][j].HasBits ? &SetStack[i][j].Bits : 0,
404 SetStack[i][j].Value);
406 CurRec->resolveReferences();
408 // Now that all of the references have been resolved, we can delete template
409 // arguments for superclasses, so they don't pollute our record, and so that
410 // their names won't conflict with later uses of the name...
411 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
412 Record *SuperClass = (*$4)[i].first;
413 for (unsigned i = 0, e = SuperClass->getTemplateArgs().size(); i != e; ++i)
414 CurRec->removeValue(SuperClass->getTemplateArgs()[i]);
416 delete $4; // Delete the class list...
422 ClassInst : CLASS ObjectBody {
423 if (Records.getClass($2->getName())) {
424 err() << "Class '" << $2->getName() << "' already defined!\n";
427 Records.addClass($$ = $2);
430 DefInst : DEF ObjectBody {
431 if (!$2->getTemplateArgs().empty()) {
432 err() << "Def '" << $2->getName()
433 << "' is not permitted to have template arguments!\n";
436 // If ObjectBody has template arguments, it's an error.
437 if (Records.getDef($2->getName())) {
438 err() << "Def '" << $2->getName() << "' already defined!\n";
441 Records.addDef($$ = $2);
445 Object : ClassInst | DefInst;
447 SETItem : ID OptBitList '=' Value {
448 SetStack.back().push_back(SetRecord(*$1, $2, $4));
449 delete $1; delete $2;
452 SETList : SETItem | SETList ',' SETItem;
454 // SETCommand - A 'SET' statement start...
455 SETCommand : SET { SetStack.push_back(std::vector<SetRecord>()); } SETList IN;
457 // Support Set commands wrapping objects... both with and without braces.
458 Object : SETCommand '{' ObjectList '}' {
461 | SETCommand Object {
465 ObjectList : Object {} | ObjectList Object {};
467 File : ObjectList {};
471 int yyerror(const char *ErrorMsg) {
472 err() << "Error parsing: " << ErrorMsg << "\n";