1 //===-- FileParser.y - Parser for TableGen files ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the bison parser for Table Generator files...
12 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/StringExtras.h"
19 #define YYERROR_VERBOSE 1
21 int yyerror(const char *ErrorMsg);
26 extern int Filelineno;
27 static Record *CurRec = 0;
29 typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
33 std::vector<unsigned> Bits;
36 LetRecord(const std::string &N, std::vector<unsigned> *B, Init *V)
37 : Name(N), Value(V), HasBits(B != 0) {
38 if (HasBits) Bits = *B;
42 static std::vector<std::vector<LetRecord> > LetStack;
45 extern std::ostream &err();
47 static void addValue(const RecordVal &RV) {
48 if (RecordVal *ERV = CurRec->getValue(RV.getName())) {
49 // The value already exists in the class, treat this as a set...
50 if (ERV->setValue(RV.getValue())) {
51 err() << "New definition of '" << RV.getName() << "' of type '"
52 << *RV.getType() << "' is incompatible with previous "
53 << "definition of type '" << *ERV->getType() << "'!\n";
61 static void addSuperClass(Record *SC) {
62 if (CurRec->isSubClassOf(SC)) {
63 err() << "Already subclass of '" << SC->getName() << "'!\n";
66 CurRec->addSuperClass(SC);
69 static void setValue(const std::string &ValName,
70 std::vector<unsigned> *BitList, Init *V) {
73 RecordVal *RV = CurRec->getValue(ValName);
75 err() << "Value '" << ValName << "' unknown!\n";
79 // Do not allow assignments like 'X = X'. This will just cause infinite loops
80 // in the resolution machinery.
82 if (VarInit *VI = dynamic_cast<VarInit*>(V))
83 if (VI->getName() == ValName)
86 // If we are assigning to a subset of the bits in the value... then we must be
87 // assigning to a field of BitsRecTy, which must have a BitsInit
91 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
93 err() << "Value '" << ValName << "' is not a bits type!\n";
97 // Convert the incoming value to a bits type of the appropriate size...
98 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList->size()));
100 V->convertInitializerTo(new BitsRecTy(BitList->size()));
101 err() << "Initializer '" << *V << "' not compatible with bit range!\n";
105 // We should have a BitsInit type now...
106 assert(dynamic_cast<BitsInit*>(BI) != 0 || &(std::cerr << *BI) == 0);
107 BitsInit *BInit = (BitsInit*)BI;
109 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
111 // Loop over bits, assigning values as appropriate...
112 for (unsigned i = 0, e = BitList->size(); i != e; ++i) {
113 unsigned Bit = (*BitList)[i];
114 if (NewVal->getBit(Bit)) {
115 err() << "Cannot set bit #" << Bit << " of value '" << ValName
116 << "' more than once!\n";
119 NewVal->setBit(Bit, BInit->getBit(i));
122 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
123 if (NewVal->getBit(i) == 0)
124 NewVal->setBit(i, CurVal->getBit(i));
129 if (RV->setValue(V)) {
130 err() << "Value '" << ValName << "' of type '" << *RV->getType()
131 << "' is incompatible with initializer '" << *V << "'!\n";
136 static void addSubClass(Record *SC, const std::vector<Init*> &TemplateArgs) {
137 // Add all of the values in the subclass into the current class...
138 const std::vector<RecordVal> &Vals = SC->getValues();
139 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
142 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
144 // Ensure that an appropriate number of template arguments are specified...
145 if (TArgs.size() < TemplateArgs.size()) {
146 err() << "ERROR: More template args specified than expected!\n";
148 } else { // This class expects template arguments...
149 // Loop over all of the template arguments, setting them to the specified
150 // value or leaving them as the default as necessary.
151 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
152 if (i < TemplateArgs.size()) { // A value is specified for this temp-arg?
154 setValue(TArgs[i], 0, TemplateArgs[i]);
155 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
156 err() << "ERROR: Value not specified for template argument #"
157 << i << " (" << TArgs[i] << ") of subclass '" << SC->getName()
164 // Since everything went well, we can now set the "superclass" list for the
166 const std::vector<Record*> &SCs = SC->getSuperClasses();
167 for (unsigned i = 0, e = SCs.size(); i != e; ++i)
168 addSuperClass(SCs[i]);
172 } // End llvm namespace
174 using namespace llvm;
182 llvm::Init* Initializer;
183 std::vector<llvm::Init*>* FieldList;
184 std::vector<unsigned>* BitList;
186 SubClassRefTy* SubClassRef;
187 std::vector<SubClassRefTy>* SubClassList;
188 std::vector<std::pair<llvm::Init*, std::string> >* DagValueList;
191 %token INT BIT STRING BITS LIST CODE DAG CLASS DEF FIELD LET IN
192 %token <IntVal> INTVAL
193 %token <StrVal> ID VARNAME STRVAL CODEFRAGMENT
196 %type <Rec> ClassInst DefInst Object ObjectBody ClassID
198 %type <SubClassRef> SubClassRef
199 %type <SubClassList> ClassList ClassListNE
200 %type <IntVal> OptPrefix
201 %type <Initializer> Value OptValue
202 %type <DagValueList> DagArgList DagArgListNE
203 %type <FieldList> ValueList ValueListNE
204 %type <BitList> BitList OptBitList RBitList
205 %type <StrVal> Declaration OptID OptVarName
212 $$ = Records.getClass(*$1);
214 err() << "Couldn't find class '" << *$1 << "'!\n";
222 Type : STRING { // string type
223 $$ = new StringRecTy();
224 } | BIT { // bit type
226 } | BITS '<' INTVAL '>' { // bits<x> type
227 $$ = new BitsRecTy($3);
228 } | INT { // int type
230 } | LIST '<' Type '>' { // list<x> type
231 $$ = new ListRecTy($3);
232 } | CODE { // code type
233 $$ = new CodeRecTy();
234 } | DAG { // dag type
236 } | ClassID { // Record Type
237 $$ = new RecordRecTy($1);
240 OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; };
242 OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; };
245 $$ = new IntInit($1);
247 $$ = new StringInit(*$1);
250 $$ = new CodeInit(*$1);
253 $$ = new UnsetInit();
254 } | '{' ValueList '}' {
255 BitsInit *Init = new BitsInit($2->size());
256 for (unsigned i = 0, e = $2->size(); i != e; ++i) {
257 struct Init *Bit = (*$2)[i]->convertInitializerTo(new BitRecTy());
259 err() << "Element #" << i << " (" << *(*$2)[i]
260 << ") is not convertable to a bit!\n";
263 Init->setBit($2->size()-i-1, Bit);
268 if (const RecordVal *RV = (CurRec ? CurRec->getValue(*$1) : 0)) {
269 $$ = new VarInit(*$1, RV->getType());
270 } else if (Record *D = Records.getDef(*$1)) {
273 err() << "Variable not defined: '" << *$1 << "'!\n";
278 } | Value '{' BitList '}' {
279 $$ = $1->convertInitializerBitRange(*$3);
281 err() << "Invalid bit range for value '" << *$1 << "'!\n";
285 } | '[' ValueList ']' {
286 $$ = new ListInit(*$2);
289 if (!$1->getFieldType(*$3)) {
290 err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n";
293 $$ = new FieldInit($1, *$3);
295 } | '(' ID DagArgList ')' {
296 Record *D = Records.getDef(*$2);
298 err() << "Invalid def '" << *$2 << "'!\n";
301 $$ = new DagInit(D, *$3);
302 delete $2; delete $3;
303 } | Value '[' BitList ']' {
304 std::reverse($3->begin(), $3->end());
305 $$ = $1->convertInitListSlice(*$3);
307 err() << "Invalid list slice for value '" << *$1 << "'!\n";
313 OptVarName : /* empty */ {
314 $$ = new std::string();
320 DagArgListNE : Value OptVarName {
321 $$ = new std::vector<std::pair<Init*, std::string> >();
322 $$->push_back(std::make_pair($1, *$2));
325 | DagArgListNE ',' Value OptVarName {
326 $1->push_back(std::make_pair($3, *$4));
331 DagArgList : /*empty*/ {
332 $$ = new std::vector<std::pair<Init*, std::string> >();
334 | DagArgListNE { $$ = $1; };
338 $$ = new std::vector<unsigned>();
340 } | INTVAL '-' INTVAL {
341 if ($1 < 0 || $3 < 0) {
342 err() << "Invalid range: " << $1 << "-" << $3 << "!\n";
345 $$ = new std::vector<unsigned>();
347 for (int i = $1; i <= $3; ++i)
350 for (int i = $1; i >= $3; --i)
355 if ($1 < 0 || $2 < 0) {
356 err() << "Invalid range: " << $1 << "-" << $2 << "!\n";
359 $$ = new std::vector<unsigned>();
361 for (int i = $1; i <= $2; ++i)
364 for (int i = $1; i >= $2; --i)
367 } | RBitList ',' INTVAL {
368 ($$=$1)->push_back($3);
369 } | RBitList ',' INTVAL '-' INTVAL {
370 if ($3 < 0 || $5 < 0) {
371 err() << "Invalid range: " << $3 << "-" << $5 << "!\n";
376 for (int i = $3; i <= $5; ++i)
379 for (int i = $3; i >= $5; --i)
382 } | RBitList ',' INTVAL INTVAL {
384 if ($3 < 0 || $4 < 0) {
385 err() << "Invalid range: " << $3 << "-" << $4 << "!\n";
390 for (int i = $3; i <= $4; ++i)
393 for (int i = $3; i >= $4; --i)
398 BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); };
400 OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; };
404 ValueList : /*empty*/ {
405 $$ = new std::vector<Init*>();
410 ValueListNE : Value {
411 $$ = new std::vector<Init*>();
413 } | ValueListNE ',' Value {
414 ($$ = $1)->push_back($3);
417 Declaration : OptPrefix Type ID OptValue {
418 addValue(RecordVal(*$3, $2, $1));
419 setValue(*$3, 0, $4);
423 BodyItem : Declaration ';' {
425 } | LET ID OptBitList '=' Value ';' {
426 setValue(*$2, $3, $5);
431 BodyList : /*empty*/ | BodyList BodyItem;
432 Body : ';' | '{' BodyList '}';
434 SubClassRef : ClassID {
435 $$ = new SubClassRefTy($1, new std::vector<Init*>());
436 } | ClassID '<' ValueListNE '>' {
437 $$ = new SubClassRefTy($1, $3);
440 ClassListNE : SubClassRef {
441 $$ = new std::vector<SubClassRefTy>();
445 | ClassListNE ',' SubClassRef {
446 ($$=$1)->push_back(*$3);
450 ClassList : /*empty */ {
451 $$ = new std::vector<SubClassRefTy>();
457 DeclListNE : Declaration {
458 CurRec->addTemplateArg(*$1);
460 } | DeclListNE ',' Declaration {
461 CurRec->addTemplateArg(*$3);
465 TemplateArgList : '<' DeclListNE '>' {};
466 OptTemplateArgList : /*empty*/ | TemplateArgList;
468 OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); };
471 static unsigned AnonCounter = 0;
473 *$1 = "anonymous."+utostr(AnonCounter++);
474 CurRec = new Record(*$1);
476 } OptTemplateArgList ClassList {
477 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
478 addSubClass((*$4)[i].first, *(*$4)[i].second);
479 // Delete the template arg values for the class
480 delete (*$4)[i].second;
483 // Process any variables on the set stack...
484 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
485 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
486 setValue(LetStack[i][j].Name,
487 LetStack[i][j].HasBits ? &LetStack[i][j].Bits : 0,
488 LetStack[i][j].Value);
490 CurRec->resolveReferences();
492 // Now that all of the references have been resolved, we can delete template
493 // arguments for superclasses, so they don't pollute our record, and so that
494 // their names won't conflict with later uses of the name...
495 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
496 Record *SuperClass = (*$4)[i].first;
497 for (unsigned i = 0, e = SuperClass->getTemplateArgs().size(); i != e; ++i)
498 if (!CurRec->isTemplateArg(SuperClass->getTemplateArgs()[i]))
499 CurRec->removeValue(SuperClass->getTemplateArgs()[i]);
501 delete $4; // Delete the class list...
507 ClassInst : CLASS ObjectBody {
508 if (Records.getClass($2->getName())) {
509 err() << "Class '" << $2->getName() << "' already defined!\n";
512 Records.addClass($$ = $2);
515 DefInst : DEF ObjectBody {
516 if (!$2->getTemplateArgs().empty()) {
517 err() << "Def '" << $2->getName()
518 << "' is not permitted to have template arguments!\n";
521 // If ObjectBody has template arguments, it's an error.
522 if (Records.getDef($2->getName())) {
523 err() << "Def '" << $2->getName() << "' already defined!\n";
526 Records.addDef($$ = $2);
530 Object : ClassInst | DefInst;
532 LETItem : ID OptBitList '=' Value {
533 LetStack.back().push_back(LetRecord(*$1, $2, $4));
534 delete $1; delete $2;
537 LETList : LETItem | LETList ',' LETItem;
539 // LETCommand - A 'LET' statement start...
540 LETCommand : LET { LetStack.push_back(std::vector<LetRecord>()); } LETList IN;
542 // Support Set commands wrapping objects... both with and without braces.
543 Object : LETCommand '{' ObjectList '}' {
546 | LETCommand Object {
550 ObjectList : Object {} | ObjectList Object {};
552 File : ObjectList {};
556 int yyerror(const char *ErrorMsg) {
557 err() << "Error parsing: " << ErrorMsg << "\n";