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;
28 static bool ParsingTemplateArgs = false;
30 typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
34 std::vector<unsigned> Bits;
37 LetRecord(const std::string &N, std::vector<unsigned> *B, Init *V)
38 : Name(N), Value(V), HasBits(B != 0) {
39 if (HasBits) Bits = *B;
43 static std::vector<std::vector<LetRecord> > LetStack;
46 extern std::ostream &err();
48 static void addValue(const RecordVal &RV) {
49 if (RecordVal *ERV = CurRec->getValue(RV.getName())) {
50 // The value already exists in the class, treat this as a set...
51 if (ERV->setValue(RV.getValue())) {
52 err() << "New definition of '" << RV.getName() << "' of type '"
53 << *RV.getType() << "' is incompatible with previous "
54 << "definition of type '" << *ERV->getType() << "'!\n";
62 static void addSuperClass(Record *SC) {
63 if (CurRec->isSubClassOf(SC)) {
64 err() << "Already subclass of '" << SC->getName() << "'!\n";
67 CurRec->addSuperClass(SC);
70 static void setValue(const std::string &ValName,
71 std::vector<unsigned> *BitList, Init *V) {
74 RecordVal *RV = CurRec->getValue(ValName);
76 err() << "Value '" << ValName << "' unknown!\n";
80 // Do not allow assignments like 'X = X'. This will just cause infinite loops
81 // in the resolution machinery.
83 if (VarInit *VI = dynamic_cast<VarInit*>(V))
84 if (VI->getName() == ValName)
87 // If we are assigning to a subset of the bits in the value... then we must be
88 // assigning to a field of BitsRecTy, which must have a BitsInit
92 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
94 err() << "Value '" << ValName << "' is not a bits type!\n";
98 // Convert the incoming value to a bits type of the appropriate size...
99 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList->size()));
101 V->convertInitializerTo(new BitsRecTy(BitList->size()));
102 err() << "Initializer '" << *V << "' not compatible with bit range!\n";
106 // We should have a BitsInit type now...
107 assert(dynamic_cast<BitsInit*>(BI) != 0 || &(std::cerr << *BI) == 0);
108 BitsInit *BInit = (BitsInit*)BI;
110 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
112 // Loop over bits, assigning values as appropriate...
113 for (unsigned i = 0, e = BitList->size(); i != e; ++i) {
114 unsigned Bit = (*BitList)[i];
115 if (NewVal->getBit(Bit)) {
116 err() << "Cannot set bit #" << Bit << " of value '" << ValName
117 << "' more than once!\n";
120 NewVal->setBit(Bit, BInit->getBit(i));
123 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
124 if (NewVal->getBit(i) == 0)
125 NewVal->setBit(i, CurVal->getBit(i));
130 if (RV->setValue(V)) {
131 err() << "Value '" << ValName << "' of type '" << *RV->getType()
132 << "' is incompatible with initializer '" << *V << "'!\n";
137 // addSubClass - Add SC as a subclass to CurRec, resolving TemplateArgs as SC's
138 // template arguments.
139 static void addSubClass(Record *SC, const std::vector<Init*> &TemplateArgs) {
140 // Add all of the values in the subclass into the current class...
141 const std::vector<RecordVal> &Vals = SC->getValues();
142 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
145 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
147 // Ensure that an appropriate number of template arguments are specified...
148 if (TArgs.size() < TemplateArgs.size()) {
149 err() << "ERROR: More template args specified than expected!\n";
151 } else { // This class expects template arguments...
152 // Loop over all of the template arguments, setting them to the specified
153 // value or leaving them as the default if necessary.
154 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
155 if (i < TemplateArgs.size()) { // A value is specified for this temp-arg?
157 setValue(TArgs[i], 0, TemplateArgs[i]);
160 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
164 CurRec->removeValue(TArgs[i]);
166 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
167 err() << "ERROR: Value not specified for template argument #"
168 << i << " (" << TArgs[i] << ") of subclass '" << SC->getName()
175 // Since everything went well, we can now set the "superclass" list for the
177 const std::vector<Record*> &SCs = SC->getSuperClasses();
178 for (unsigned i = 0, e = SCs.size(); i != e; ++i)
179 addSuperClass(SCs[i]);
183 } // End llvm namespace
185 using namespace llvm;
193 llvm::Init* Initializer;
194 std::vector<llvm::Init*>* FieldList;
195 std::vector<unsigned>* BitList;
197 SubClassRefTy* SubClassRef;
198 std::vector<SubClassRefTy>* SubClassList;
199 std::vector<std::pair<llvm::Init*, std::string> >* DagValueList;
202 %token INT BIT STRING BITS LIST CODE DAG CLASS DEF FIELD LET IN
203 %token SHLTOK SRATOK SRLTOK STRCONCATTOK
204 %token <IntVal> INTVAL
205 %token <StrVal> ID VARNAME STRVAL CODEFRAGMENT
208 %type <Rec> ClassInst DefInst Object ObjectBody ClassID
210 %type <SubClassRef> SubClassRef
211 %type <SubClassList> ClassList ClassListNE
212 %type <IntVal> OptPrefix
213 %type <Initializer> Value OptValue IDValue
214 %type <DagValueList> DagArgList DagArgListNE
215 %type <FieldList> ValueList ValueListNE
216 %type <BitList> BitList OptBitList RBitList
217 %type <StrVal> Declaration OptID OptVarName ObjectName
224 $$ = Records.getClass(*$1);
226 err() << "Couldn't find class '" << *$1 << "'!\n";
234 Type : STRING { // string type
235 $$ = new StringRecTy();
236 } | BIT { // bit type
238 } | BITS '<' INTVAL '>' { // bits<x> type
239 $$ = new BitsRecTy($3);
240 } | INT { // int type
242 } | LIST '<' Type '>' { // list<x> type
243 $$ = new ListRecTy($3);
244 } | CODE { // code type
245 $$ = new CodeRecTy();
246 } | DAG { // dag type
248 } | ClassID { // Record Type
249 $$ = new RecordRecTy($1);
252 OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; };
254 OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; };
257 if (const RecordVal *RV = (CurRec ? CurRec->getValue(*$1) : 0)) {
258 $$ = new VarInit(*$1, RV->getType());
259 } else if (CurRec && CurRec->isTemplateArg(CurRec->getName()+":"+*$1)) {
260 const RecordVal *RV = CurRec->getValue(CurRec->getName()+":"+*$1);
261 assert(RV && "Template arg doesn't exist??");
262 $$ = new VarInit(CurRec->getName()+":"+*$1, RV->getType());
263 } else if (Record *D = Records.getDef(*$1)) {
266 err() << "Variable not defined: '" << *$1 << "'!\n";
276 $$ = new IntInit($1);
278 $$ = new StringInit(*$1);
281 $$ = new CodeInit(*$1);
284 $$ = new UnsetInit();
285 } | '{' ValueList '}' {
286 BitsInit *Init = new BitsInit($2->size());
287 for (unsigned i = 0, e = $2->size(); i != e; ++i) {
288 struct Init *Bit = (*$2)[i]->convertInitializerTo(new BitRecTy());
290 err() << "Element #" << i << " (" << *(*$2)[i]
291 << ") is not convertable to a bit!\n";
294 Init->setBit($2->size()-i-1, Bit);
298 } | ID '<' ValueListNE '>' {
299 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
300 // a new anonymous definition, deriving from CLASS<initvalslist> with no
302 Record *Class = Records.getClass(*$1);
304 err() << "Expected a class, got '" << *$1 << "'!\n";
309 static unsigned AnonCounter = 0;
310 Record *OldRec = CurRec; // Save CurRec.
312 // Create the new record, set it as CurRec temporarily.
313 CurRec = new Record("anonymous.val."+utostr(AnonCounter++));
314 addSubClass(Class, *$3); // Add info about the subclass to CurRec.
315 delete $3; // Free up the template args.
317 CurRec->resolveReferences();
319 Records.addDef(CurRec);
321 // The result of the expression is a reference to the new record.
322 $$ = new DefInit(CurRec);
324 // Restore the old CurRec
326 } | Value '{' BitList '}' {
327 $$ = $1->convertInitializerBitRange(*$3);
329 err() << "Invalid bit range for value '" << *$1 << "'!\n";
333 } | '[' ValueList ']' {
334 $$ = new ListInit(*$2);
337 if (!$1->getFieldType(*$3)) {
338 err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n";
341 $$ = new FieldInit($1, *$3);
343 } | '(' IDValue DagArgList ')' {
344 $$ = new DagInit($2, *$3);
346 } | Value '[' BitList ']' {
347 std::reverse($3->begin(), $3->end());
348 $$ = $1->convertInitListSlice(*$3);
350 err() << "Invalid list slice for value '" << *$1 << "'!\n";
354 } | SHLTOK '(' Value ',' Value ')' {
355 $$ = (new BinOpInit(BinOpInit::SHL, $3, $5))->Fold();
356 } | SRATOK '(' Value ',' Value ')' {
357 $$ = (new BinOpInit(BinOpInit::SRA, $3, $5))->Fold();
358 } | SRLTOK '(' Value ',' Value ')' {
359 $$ = (new BinOpInit(BinOpInit::SRL, $3, $5))->Fold();
360 } | STRCONCATTOK '(' Value ',' Value ')' {
361 $$ = (new BinOpInit(BinOpInit::STRCONCAT, $3, $5))->Fold();
364 OptVarName : /* empty */ {
365 $$ = new std::string();
371 DagArgListNE : Value OptVarName {
372 $$ = new std::vector<std::pair<Init*, std::string> >();
373 $$->push_back(std::make_pair($1, *$2));
376 | DagArgListNE ',' Value OptVarName {
377 $1->push_back(std::make_pair($3, *$4));
382 DagArgList : /*empty*/ {
383 $$ = new std::vector<std::pair<Init*, std::string> >();
385 | DagArgListNE { $$ = $1; };
389 $$ = new std::vector<unsigned>();
391 } | INTVAL '-' INTVAL {
392 if ($1 < 0 || $3 < 0) {
393 err() << "Invalid range: " << $1 << "-" << $3 << "!\n";
396 $$ = new std::vector<unsigned>();
398 for (int i = $1; i <= $3; ++i)
401 for (int i = $1; i >= $3; --i)
406 if ($1 < 0 || $2 < 0) {
407 err() << "Invalid range: " << $1 << "-" << $2 << "!\n";
410 $$ = new std::vector<unsigned>();
412 for (int i = $1; i <= $2; ++i)
415 for (int i = $1; i >= $2; --i)
418 } | RBitList ',' INTVAL {
419 ($$=$1)->push_back($3);
420 } | RBitList ',' INTVAL '-' INTVAL {
421 if ($3 < 0 || $5 < 0) {
422 err() << "Invalid range: " << $3 << "-" << $5 << "!\n";
427 for (int i = $3; i <= $5; ++i)
430 for (int i = $3; i >= $5; --i)
433 } | RBitList ',' INTVAL INTVAL {
435 if ($3 < 0 || $4 < 0) {
436 err() << "Invalid range: " << $3 << "-" << $4 << "!\n";
441 for (int i = $3; i <= $4; ++i)
444 for (int i = $3; i >= $4; --i)
449 BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); };
451 OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; };
455 ValueList : /*empty*/ {
456 $$ = new std::vector<Init*>();
461 ValueListNE : Value {
462 $$ = new std::vector<Init*>();
464 } | ValueListNE ',' Value {
465 ($$ = $1)->push_back($3);
468 Declaration : OptPrefix Type ID OptValue {
469 std::string DecName = *$3;
470 if (ParsingTemplateArgs)
471 DecName = CurRec->getName() + ":" + DecName;
473 addValue(RecordVal(DecName, $2, $1));
474 setValue(DecName, 0, $4);
475 $$ = new std::string(DecName);
478 BodyItem : Declaration ';' {
480 } | LET ID OptBitList '=' Value ';' {
481 setValue(*$2, $3, $5);
486 BodyList : /*empty*/ | BodyList BodyItem;
487 Body : ';' | '{' BodyList '}';
489 SubClassRef : ClassID {
490 $$ = new SubClassRefTy($1, new std::vector<Init*>());
491 } | ClassID '<' ValueListNE '>' {
492 $$ = new SubClassRefTy($1, $3);
495 ClassListNE : SubClassRef {
496 $$ = new std::vector<SubClassRefTy>();
500 | ClassListNE ',' SubClassRef {
501 ($$=$1)->push_back(*$3);
505 ClassList : /*empty */ {
506 $$ = new std::vector<SubClassRefTy>();
512 DeclListNE : Declaration {
513 CurRec->addTemplateArg(*$1);
515 } | DeclListNE ',' Declaration {
516 CurRec->addTemplateArg(*$3);
520 TemplateArgList : '<' DeclListNE '>' {};
521 OptTemplateArgList : /*empty*/ | TemplateArgList;
523 OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); };
526 static unsigned AnonCounter = 0;
528 *$1 = "anonymous."+utostr(AnonCounter++);
532 ClassName : ObjectName {
533 // If a class of this name already exists, it must be a forward ref.
534 if ((CurRec = Records.getClass(*$1))) {
535 // If the body was previously defined, this is an error.
536 if (!CurRec->getValues().empty() ||
537 !CurRec->getSuperClasses().empty() ||
538 !CurRec->getTemplateArgs().empty()) {
539 err() << "Class '" << CurRec->getName() << "' already defined!\n";
543 // If this is the first reference to this class, create and add it.
544 CurRec = new Record(*$1);
545 Records.addClass(CurRec);
550 DefName : ObjectName {
551 CurRec = new Record(*$1);
554 // Ensure redefinition doesn't happen.
555 if (Records.getDef(CurRec->getName())) {
556 err() << "Def '" << CurRec->getName() << "' already defined!\n";
559 Records.addDef(CurRec);
562 ObjectBody : ClassList {
563 for (unsigned i = 0, e = $1->size(); i != e; ++i) {
564 addSubClass((*$1)[i].first, *(*$1)[i].second);
565 // Delete the template arg values for the class
566 delete (*$1)[i].second;
568 delete $1; // Delete the class list...
570 // Process any variables on the set stack...
571 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
572 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
573 setValue(LetStack[i][j].Name,
574 LetStack[i][j].HasBits ? &LetStack[i][j].Bits : 0,
575 LetStack[i][j].Value);
581 ClassInst : CLASS ClassName {
582 ParsingTemplateArgs = true;
583 } OptTemplateArgList {
584 ParsingTemplateArgs = false;
589 DefInst : DEF DefName ObjectBody {
590 $3->resolveReferences();
592 // If ObjectBody has template arguments, it's an error.
593 assert($3->getTemplateArgs().empty() && "How'd this get template args?");
598 Object : ClassInst | DefInst;
600 LETItem : ID OptBitList '=' Value {
601 LetStack.back().push_back(LetRecord(*$1, $2, $4));
602 delete $1; delete $2;
605 LETList : LETItem | LETList ',' LETItem;
607 // LETCommand - A 'LET' statement start...
608 LETCommand : LET { LetStack.push_back(std::vector<LetRecord>()); } LETList IN;
610 // Support Set commands wrapping objects... both with and without braces.
611 Object : LETCommand '{' ObjectList '}' {
614 | LETCommand Object {
618 ObjectList : Object {} | ObjectList Object {};
620 File : ObjectList {};
624 int yyerror(const char *ErrorMsg) {
625 err() << "Error parsing: " << ErrorMsg << "\n";