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"
17 #include "llvm/Support/Streams.h"
20 #define YYERROR_VERBOSE 1
22 int yyerror(const char *ErrorMsg);
27 Record Rec; // Placeholder for template args and Name.
28 std::vector<Record*> DefPrototypes;
30 MultiClass(const std::string &Name) : Rec(Name) {}
34 static std::map<std::string, MultiClass*> MultiClasses;
36 extern int Filelineno;
37 static MultiClass *CurMultiClass = 0; // Set while parsing a multiclass.
38 static std::string *CurDefmPrefix = 0; // Set while parsing defm.
39 static Record *CurRec = 0;
40 static bool ParsingTemplateArgs = false;
42 typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
46 std::vector<unsigned> Bits;
49 LetRecord(const std::string &N, std::vector<unsigned> *B, Init *V)
50 : Name(N), Value(V), HasBits(B != 0) {
51 if (HasBits) Bits = *B;
55 static std::vector<std::vector<LetRecord> > LetStack;
58 extern std::ostream &err();
60 /// getActiveRec - If inside a def/class definition, return the def/class.
61 /// Otherwise, if within a multidef, return it.
62 static Record *getActiveRec() {
63 return CurRec ? CurRec : &CurMultiClass->Rec;
66 static void addValue(const RecordVal &RV) {
67 Record *TheRec = getActiveRec();
69 if (RecordVal *ERV = TheRec->getValue(RV.getName())) {
70 // The value already exists in the class, treat this as a set...
71 if (ERV->setValue(RV.getValue())) {
72 err() << "New definition of '" << RV.getName() << "' of type '"
73 << *RV.getType() << "' is incompatible with previous "
74 << "definition of type '" << *ERV->getType() << "'!\n";
82 static void addSuperClass(Record *SC) {
83 if (CurRec->isSubClassOf(SC)) {
84 err() << "Already subclass of '" << SC->getName() << "'!\n";
87 CurRec->addSuperClass(SC);
90 static void setValue(const std::string &ValName,
91 std::vector<unsigned> *BitList, Init *V) {
94 Record *TheRec = getActiveRec();
95 RecordVal *RV = TheRec->getValue(ValName);
97 err() << "Value '" << ValName << "' unknown!\n";
101 // Do not allow assignments like 'X = X'. This will just cause infinite loops
102 // in the resolution machinery.
104 if (VarInit *VI = dynamic_cast<VarInit*>(V))
105 if (VI->getName() == ValName)
108 // If we are assigning to a subset of the bits in the value... then we must be
109 // assigning to a field of BitsRecTy, which must have a BitsInit
113 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
115 err() << "Value '" << ValName << "' is not a bits type!\n";
119 // Convert the incoming value to a bits type of the appropriate size...
120 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList->size()));
122 V->convertInitializerTo(new BitsRecTy(BitList->size()));
123 err() << "Initializer '" << *V << "' not compatible with bit range!\n";
127 // We should have a BitsInit type now...
128 assert(dynamic_cast<BitsInit*>(BI) != 0 || (cerr << *BI).stream() == 0);
129 BitsInit *BInit = (BitsInit*)BI;
131 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
133 // Loop over bits, assigning values as appropriate...
134 for (unsigned i = 0, e = BitList->size(); i != e; ++i) {
135 unsigned Bit = (*BitList)[i];
136 if (NewVal->getBit(Bit)) {
137 err() << "Cannot set bit #" << Bit << " of value '" << ValName
138 << "' more than once!\n";
141 NewVal->setBit(Bit, BInit->getBit(i));
144 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
145 if (NewVal->getBit(i) == 0)
146 NewVal->setBit(i, CurVal->getBit(i));
151 if (RV->setValue(V)) {
152 err() << "Value '" << ValName << "' of type '" << *RV->getType()
153 << "' is incompatible with initializer '" << *V << "'!\n";
158 // addSubClass - Add SC as a subclass to CurRec, resolving TemplateArgs as SC's
159 // template arguments.
160 static void addSubClass(Record *SC, const std::vector<Init*> &TemplateArgs) {
161 // Add all of the values in the subclass into the current class...
162 const std::vector<RecordVal> &Vals = SC->getValues();
163 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
166 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
168 // Ensure that an appropriate number of template arguments are specified...
169 if (TArgs.size() < TemplateArgs.size()) {
170 err() << "ERROR: More template args specified than expected!\n";
174 // Loop over all of the template arguments, setting them to the specified
175 // value or leaving them as the default if necessary.
176 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
177 if (i < TemplateArgs.size()) { // A value is specified for this temp-arg?
179 setValue(TArgs[i], 0, TemplateArgs[i]);
182 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
186 CurRec->removeValue(TArgs[i]);
188 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
189 err() << "ERROR: Value not specified for template argument #"
190 << i << " (" << TArgs[i] << ") of subclass '" << SC->getName()
196 // Since everything went well, we can now set the "superclass" list for the
198 const std::vector<Record*> &SCs = SC->getSuperClasses();
199 for (unsigned i = 0, e = SCs.size(); i != e; ++i)
200 addSuperClass(SCs[i]);
204 } // End llvm namespace
206 using namespace llvm;
214 llvm::Init* Initializer;
215 std::vector<llvm::Init*>* FieldList;
216 std::vector<unsigned>* BitList;
218 std::vector<llvm::Record*>* RecList;
219 SubClassRefTy* SubClassRef;
220 std::vector<SubClassRefTy>* SubClassList;
221 std::vector<std::pair<llvm::Init*, std::string> >* DagValueList;
224 %token INT BIT STRING BITS LIST CODE DAG CLASS DEF MULTICLASS DEFM FIELD LET IN
225 %token CONCATTOK SHLTOK SRATOK SRLTOK STRCONCATTOK
226 %token <IntVal> INTVAL
227 %token <StrVal> ID VARNAME STRVAL CODEFRAGMENT
230 %type <Rec> ClassInst DefInst MultiClassDef ObjectBody ClassID
231 %type <RecList> MultiClassBody
233 %type <SubClassRef> SubClassRef
234 %type <SubClassList> ClassList ClassListNE
235 %type <IntVal> OptPrefix
236 %type <Initializer> Value OptValue IDValue
237 %type <DagValueList> DagArgList DagArgListNE
238 %type <FieldList> ValueList ValueListNE
239 %type <BitList> BitList OptBitList RBitList
240 %type <StrVal> Declaration OptID OptVarName ObjectName
248 // If CurDefmPrefix is set, we're parsing a defm, which means that this is
249 // actually the name of a multiclass.
250 MultiClass *MC = MultiClasses[*$1];
252 err() << "Couldn't find class '" << *$1 << "'!\n";
257 $$ = Records.getClass(*$1);
260 err() << "Couldn't find class '" << *$1 << "'!\n";
268 Type : STRING { // string type
269 $$ = new StringRecTy();
270 } | BIT { // bit type
272 } | BITS '<' INTVAL '>' { // bits<x> type
273 $$ = new BitsRecTy($3);
274 } | INT { // int type
276 } | LIST '<' Type '>' { // list<x> type
277 $$ = new ListRecTy($3);
278 } | CODE { // code type
279 $$ = new CodeRecTy();
280 } | DAG { // dag type
282 } | ClassID { // Record Type
283 $$ = new RecordRecTy($1);
286 OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; };
288 OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; };
291 if (const RecordVal *RV = (CurRec ? CurRec->getValue(*$1) : 0)) {
292 $$ = new VarInit(*$1, RV->getType());
293 } else if (CurRec && CurRec->isTemplateArg(CurRec->getName()+":"+*$1)) {
294 const RecordVal *RV = CurRec->getValue(CurRec->getName()+":"+*$1);
295 assert(RV && "Template arg doesn't exist??");
296 $$ = new VarInit(CurRec->getName()+":"+*$1, RV->getType());
297 } else if (CurMultiClass &&
298 CurMultiClass->Rec.isTemplateArg(CurMultiClass->Rec.getName()+"::"+*$1)) {
299 std::string Name = CurMultiClass->Rec.getName()+"::"+*$1;
300 const RecordVal *RV = CurMultiClass->Rec.getValue(Name);
301 assert(RV && "Template arg doesn't exist??");
302 $$ = new VarInit(Name, RV->getType());
303 } else if (Record *D = Records.getDef(*$1)) {
306 err() << "Variable not defined: '" << *$1 << "'!\n";
316 $$ = new IntInit($1);
318 $$ = new StringInit(*$1);
321 $$ = new CodeInit(*$1);
324 $$ = new UnsetInit();
325 } | '{' ValueList '}' {
326 BitsInit *Init = new BitsInit($2->size());
327 for (unsigned i = 0, e = $2->size(); i != e; ++i) {
328 struct Init *Bit = (*$2)[i]->convertInitializerTo(new BitRecTy());
330 err() << "Element #" << i << " (" << *(*$2)[i]
331 << ") is not convertable to a bit!\n";
334 Init->setBit($2->size()-i-1, Bit);
338 } | ID '<' ValueListNE '>' {
339 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
340 // a new anonymous definition, deriving from CLASS<initvalslist> with no
342 Record *Class = Records.getClass(*$1);
344 err() << "Expected a class, got '" << *$1 << "'!\n";
349 static unsigned AnonCounter = 0;
350 Record *OldRec = CurRec; // Save CurRec.
352 // Create the new record, set it as CurRec temporarily.
353 CurRec = new Record("anonymous.val."+utostr(AnonCounter++));
354 addSubClass(Class, *$3); // Add info about the subclass to CurRec.
355 delete $3; // Free up the template args.
357 CurRec->resolveReferences();
359 Records.addDef(CurRec);
361 // The result of the expression is a reference to the new record.
362 $$ = new DefInit(CurRec);
364 // Restore the old CurRec
366 } | Value '{' BitList '}' {
367 $$ = $1->convertInitializerBitRange(*$3);
369 err() << "Invalid bit range for value '" << *$1 << "'!\n";
373 } | '[' ValueList ']' {
374 $$ = new ListInit(*$2);
377 if (!$1->getFieldType(*$3)) {
378 err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n";
381 $$ = new FieldInit($1, *$3);
383 } | '(' IDValue DagArgList ')' {
384 $$ = new DagInit($2, *$3);
386 } | Value '[' BitList ']' {
387 std::reverse($3->begin(), $3->end());
388 $$ = $1->convertInitListSlice(*$3);
390 err() << "Invalid list slice for value '" << *$1 << "'!\n";
394 } | CONCATTOK '(' Value ',' Value ')' {
395 $$ = (new BinOpInit(BinOpInit::CONCAT, $3, $5))->Fold();
396 } | SHLTOK '(' Value ',' Value ')' {
397 $$ = (new BinOpInit(BinOpInit::SHL, $3, $5))->Fold();
398 } | SRATOK '(' Value ',' Value ')' {
399 $$ = (new BinOpInit(BinOpInit::SRA, $3, $5))->Fold();
400 } | SRLTOK '(' Value ',' Value ')' {
401 $$ = (new BinOpInit(BinOpInit::SRL, $3, $5))->Fold();
402 } | STRCONCATTOK '(' Value ',' Value ')' {
403 $$ = (new BinOpInit(BinOpInit::STRCONCAT, $3, $5))->Fold();
406 OptVarName : /* empty */ {
407 $$ = new std::string();
413 DagArgListNE : Value OptVarName {
414 $$ = new std::vector<std::pair<Init*, std::string> >();
415 $$->push_back(std::make_pair($1, *$2));
418 | DagArgListNE ',' Value OptVarName {
419 $1->push_back(std::make_pair($3, *$4));
424 DagArgList : /*empty*/ {
425 $$ = new std::vector<std::pair<Init*, std::string> >();
427 | DagArgListNE { $$ = $1; };
431 $$ = new std::vector<unsigned>();
433 } | INTVAL '-' INTVAL {
434 if ($1 < 0 || $3 < 0) {
435 err() << "Invalid range: " << $1 << "-" << $3 << "!\n";
438 $$ = new std::vector<unsigned>();
440 for (int i = $1; i <= $3; ++i)
443 for (int i = $1; i >= $3; --i)
448 if ($1 < 0 || $2 < 0) {
449 err() << "Invalid range: " << $1 << "-" << $2 << "!\n";
452 $$ = new std::vector<unsigned>();
454 for (int i = $1; i <= $2; ++i)
457 for (int i = $1; i >= $2; --i)
460 } | RBitList ',' INTVAL {
461 ($$=$1)->push_back($3);
462 } | RBitList ',' INTVAL '-' INTVAL {
463 if ($3 < 0 || $5 < 0) {
464 err() << "Invalid range: " << $3 << "-" << $5 << "!\n";
469 for (int i = $3; i <= $5; ++i)
472 for (int i = $3; i >= $5; --i)
475 } | RBitList ',' INTVAL INTVAL {
477 if ($3 < 0 || $4 < 0) {
478 err() << "Invalid range: " << $3 << "-" << $4 << "!\n";
483 for (int i = $3; i <= $4; ++i)
486 for (int i = $3; i >= $4; --i)
491 BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); };
493 OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; };
497 ValueList : /*empty*/ {
498 $$ = new std::vector<Init*>();
503 ValueListNE : Value {
504 $$ = new std::vector<Init*>();
506 } | ValueListNE ',' Value {
507 ($$ = $1)->push_back($3);
510 Declaration : OptPrefix Type ID OptValue {
511 std::string DecName = *$3;
512 if (ParsingTemplateArgs) {
514 DecName = CurRec->getName() + ":" + DecName;
516 assert(CurMultiClass);
519 DecName = CurMultiClass->Rec.getName() + "::" + DecName;
522 addValue(RecordVal(DecName, $2, $1));
523 setValue(DecName, 0, $4);
524 $$ = new std::string(DecName);
527 BodyItem : Declaration ';' {
529 } | LET ID OptBitList '=' Value ';' {
530 setValue(*$2, $3, $5);
535 BodyList : /*empty*/ | BodyList BodyItem;
536 Body : ';' | '{' BodyList '}';
538 SubClassRef : ClassID {
539 $$ = new SubClassRefTy($1, new std::vector<Init*>());
540 } | ClassID '<' ValueListNE '>' {
541 $$ = new SubClassRefTy($1, $3);
544 ClassListNE : SubClassRef {
545 $$ = new std::vector<SubClassRefTy>();
549 | ClassListNE ',' SubClassRef {
550 ($$=$1)->push_back(*$3);
554 ClassList : /*empty */ {
555 $$ = new std::vector<SubClassRefTy>();
561 DeclListNE : Declaration {
562 getActiveRec()->addTemplateArg(*$1);
564 } | DeclListNE ',' Declaration {
565 getActiveRec()->addTemplateArg(*$3);
569 TemplateArgList : '<' DeclListNE '>' {};
570 OptTemplateArgList : /*empty*/ | TemplateArgList;
572 OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); };
575 static unsigned AnonCounter = 0;
577 *$1 = "anonymous."+utostr(AnonCounter++);
581 ClassName : ObjectName {
582 // If a class of this name already exists, it must be a forward ref.
583 if ((CurRec = Records.getClass(*$1))) {
584 // If the body was previously defined, this is an error.
585 if (!CurRec->getValues().empty() ||
586 !CurRec->getSuperClasses().empty() ||
587 !CurRec->getTemplateArgs().empty()) {
588 err() << "Class '" << CurRec->getName() << "' already defined!\n";
592 // If this is the first reference to this class, create and add it.
593 CurRec = new Record(*$1);
594 Records.addClass(CurRec);
599 DefName : ObjectName {
600 CurRec = new Record(*$1);
603 if (!CurMultiClass) {
604 // Top-level def definition.
606 // Ensure redefinition doesn't happen.
607 if (Records.getDef(CurRec->getName())) {
608 err() << "def '" << CurRec->getName() << "' already defined!\n";
611 Records.addDef(CurRec);
613 // Otherwise, a def inside a multiclass, add it to the multiclass.
614 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
615 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
616 err() << "def '" << CurRec->getName()
617 << "' already defined in this multiclass!\n";
620 CurMultiClass->DefPrototypes.push_back(CurRec);
624 ObjectBody : ClassList {
625 for (unsigned i = 0, e = $1->size(); i != e; ++i) {
626 addSubClass((*$1)[i].first, *(*$1)[i].second);
627 // Delete the template arg values for the class
628 delete (*$1)[i].second;
630 delete $1; // Delete the class list.
632 // Process any variables on the let stack.
633 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
634 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
635 setValue(LetStack[i][j].Name,
636 LetStack[i][j].HasBits ? &LetStack[i][j].Bits : 0,
637 LetStack[i][j].Value);
643 ClassInst : CLASS ClassName {
644 ParsingTemplateArgs = true;
645 } OptTemplateArgList {
646 ParsingTemplateArgs = false;
651 DefInst : DEF DefName ObjectBody {
652 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
653 $3->resolveReferences();
655 // If ObjectBody has template arguments, it's an error.
656 assert($3->getTemplateArgs().empty() && "How'd this get template args?");
660 // MultiClassDef - A def instance specified inside a multiclass.
661 MultiClassDef : DefInst {
663 // Copy the template arguments for the multiclass into the def.
664 const std::vector<std::string> &TArgs = CurMultiClass->Rec.getTemplateArgs();
666 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
667 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
668 assert(RV && "Template arg doesn't exist?");
673 // MultiClassBody - Sequence of def's that are instantiated when a multiclass is
675 MultiClassBody : MultiClassDef {
676 $$ = new std::vector<Record*>();
678 } | MultiClassBody MultiClassDef {
682 MultiClassName : ID {
683 MultiClass *&MCE = MultiClasses[*$1];
685 err() << "multiclass '" << *$1 << "' already defined!\n";
688 MCE = CurMultiClass = new MultiClass(*$1);
692 // MultiClass - Multiple definitions.
693 MultiClassInst : MULTICLASS MultiClassName {
694 ParsingTemplateArgs = true;
695 } OptTemplateArgList {
696 ParsingTemplateArgs = false;
697 }'{' MultiClassBody '}' {
701 // DefMInst - Instantiate a multiclass.
702 DefMInst : DEFM ID { CurDefmPrefix = $2; } ':' SubClassRef ';' {
703 // To instantiate a multiclass, we need to first get the multiclass, then
704 // instantiate each def contained in the multiclass with the SubClassRef
705 // template parameters.
706 MultiClass *MC = MultiClasses[$5->first->getName()];
707 assert(MC && "Didn't lookup multiclass correctly?");
708 std::vector<Init*> &TemplateVals = *$5->second;
711 // Verify that the correct number of template arguments were specified.
712 const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs();
713 if (TArgs.size() < TemplateVals.size()) {
714 err() << "ERROR: More template args specified than multiclass expects!\n";
718 // Loop over all the def's in the multiclass, instantiating each one.
719 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
720 Record *DefProto = MC->DefPrototypes[i];
722 // Add the suffix to the defm name to get the new name.
723 assert(CurRec == 0 && "A def is current?");
724 CurRec = new Record(*$2 + DefProto->getName());
726 addSubClass(DefProto, std::vector<Init*>());
728 // Loop over all of the template arguments, setting them to the specified
729 // value or leaving them as the default if necessary.
730 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
731 if (i < TemplateVals.size()) { // A value is specified for this temp-arg?
733 setValue(TArgs[i], 0, TemplateVals[i]);
736 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
739 CurRec->removeValue(TArgs[i]);
741 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
742 err() << "ERROR: Value not specified for template argument #"
743 << i << " (" << TArgs[i] << ") of multiclassclass '"
744 << MC->Rec.getName() << "'!\n";
749 // If the mdef is inside a 'let' expression, add to each def.
750 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
751 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
752 setValue(LetStack[i][j].Name,
753 LetStack[i][j].HasBits ? &LetStack[i][j].Bits : 0,
754 LetStack[i][j].Value);
757 // Ensure redefinition doesn't happen.
758 if (Records.getDef(CurRec->getName())) {
759 err() << "def '" << CurRec->getName() << "' already defined, "
760 << "instantiating defm '" << *$2 << "' with subdef '"
761 << DefProto->getName() << "'!\n";
764 Records.addDef(CurRec);
766 CurRec->resolveReferences();
771 delete &TemplateVals;
776 Object : ClassInst {} | DefInst {};
777 Object : MultiClassInst | DefMInst;
779 LETItem : ID OptBitList '=' Value {
780 LetStack.back().push_back(LetRecord(*$1, $2, $4));
781 delete $1; delete $2;
784 LETList : LETItem | LETList ',' LETItem;
786 // LETCommand - A 'LET' statement start...
787 LETCommand : LET { LetStack.push_back(std::vector<LetRecord>()); } LETList IN;
789 // Support Set commands wrapping objects... both with and without braces.
790 Object : LETCommand '{' ObjectList '}' {
793 | LETCommand Object {
797 ObjectList : Object {} | ObjectList Object {};
803 int yyerror(const char *ErrorMsg) {
804 err() << "Error parsing: " << ErrorMsg << "\n";