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 Record Rec; // Placeholder for template args and Name.
27 std::vector<Record*> DefPrototypes;
29 MultiClass(const std::string &Name) : Rec(Name) {}
33 static std::map<std::string, MultiClass*> MultiClasses;
35 extern int Filelineno;
36 static MultiClass *CurMultiClass = 0; // Set while parsing a multiclass.
37 static std::string *CurDefmPrefix = 0; // Set while parsing defm.
38 static Record *CurRec = 0;
39 static bool ParsingTemplateArgs = false;
41 typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
45 std::vector<unsigned> Bits;
48 LetRecord(const std::string &N, std::vector<unsigned> *B, Init *V)
49 : Name(N), Value(V), HasBits(B != 0) {
50 if (HasBits) Bits = *B;
54 static std::vector<std::vector<LetRecord> > LetStack;
57 extern std::ostream &err();
59 /// getActiveRec - If inside a def/class definition, return the def/class.
60 /// Otherwise, if within a multidef, return it.
61 static Record *getActiveRec() {
62 return CurRec ? CurRec : &CurMultiClass->Rec;
65 static void addValue(const RecordVal &RV) {
66 Record *TheRec = getActiveRec();
68 if (RecordVal *ERV = TheRec->getValue(RV.getName())) {
69 // The value already exists in the class, treat this as a set...
70 if (ERV->setValue(RV.getValue())) {
71 err() << "New definition of '" << RV.getName() << "' of type '"
72 << *RV.getType() << "' is incompatible with previous "
73 << "definition of type '" << *ERV->getType() << "'!\n";
81 static void addSuperClass(Record *SC) {
82 if (CurRec->isSubClassOf(SC)) {
83 err() << "Already subclass of '" << SC->getName() << "'!\n";
86 CurRec->addSuperClass(SC);
89 static void setValue(const std::string &ValName,
90 std::vector<unsigned> *BitList, Init *V) {
93 Record *TheRec = getActiveRec();
94 RecordVal *RV = TheRec->getValue(ValName);
96 err() << "Value '" << ValName << "' unknown!\n";
100 // Do not allow assignments like 'X = X'. This will just cause infinite loops
101 // in the resolution machinery.
103 if (VarInit *VI = dynamic_cast<VarInit*>(V))
104 if (VI->getName() == ValName)
107 // If we are assigning to a subset of the bits in the value... then we must be
108 // assigning to a field of BitsRecTy, which must have a BitsInit
112 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
114 err() << "Value '" << ValName << "' is not a bits type!\n";
118 // Convert the incoming value to a bits type of the appropriate size...
119 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList->size()));
121 V->convertInitializerTo(new BitsRecTy(BitList->size()));
122 err() << "Initializer '" << *V << "' not compatible with bit range!\n";
126 // We should have a BitsInit type now...
127 assert(dynamic_cast<BitsInit*>(BI) != 0 || &(std::cerr << *BI) == 0);
128 BitsInit *BInit = (BitsInit*)BI;
130 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
132 // Loop over bits, assigning values as appropriate...
133 for (unsigned i = 0, e = BitList->size(); i != e; ++i) {
134 unsigned Bit = (*BitList)[i];
135 if (NewVal->getBit(Bit)) {
136 err() << "Cannot set bit #" << Bit << " of value '" << ValName
137 << "' more than once!\n";
140 NewVal->setBit(Bit, BInit->getBit(i));
143 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
144 if (NewVal->getBit(i) == 0)
145 NewVal->setBit(i, CurVal->getBit(i));
150 if (RV->setValue(V)) {
151 err() << "Value '" << ValName << "' of type '" << *RV->getType()
152 << "' is incompatible with initializer '" << *V << "'!\n";
157 // addSubClass - Add SC as a subclass to CurRec, resolving TemplateArgs as SC's
158 // template arguments.
159 static void addSubClass(Record *SC, const std::vector<Init*> &TemplateArgs) {
160 // Add all of the values in the subclass into the current class...
161 const std::vector<RecordVal> &Vals = SC->getValues();
162 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
165 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
167 // Ensure that an appropriate number of template arguments are specified...
168 if (TArgs.size() < TemplateArgs.size()) {
169 err() << "ERROR: More template args specified than expected!\n";
173 // Loop over all of the template arguments, setting them to the specified
174 // value or leaving them as the default if necessary.
175 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
176 if (i < TemplateArgs.size()) { // A value is specified for this temp-arg?
178 setValue(TArgs[i], 0, TemplateArgs[i]);
181 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
185 CurRec->removeValue(TArgs[i]);
187 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
188 err() << "ERROR: Value not specified for template argument #"
189 << i << " (" << TArgs[i] << ") of subclass '" << SC->getName()
195 // Since everything went well, we can now set the "superclass" list for the
197 const std::vector<Record*> &SCs = SC->getSuperClasses();
198 for (unsigned i = 0, e = SCs.size(); i != e; ++i)
199 addSuperClass(SCs[i]);
203 } // End llvm namespace
205 using namespace llvm;
213 llvm::Init* Initializer;
214 std::vector<llvm::Init*>* FieldList;
215 std::vector<unsigned>* BitList;
217 std::vector<llvm::Record*>* RecList;
218 SubClassRefTy* SubClassRef;
219 std::vector<SubClassRefTy>* SubClassList;
220 std::vector<std::pair<llvm::Init*, std::string> >* DagValueList;
223 %token INT BIT STRING BITS LIST CODE DAG CLASS DEF MULTICLASS DEFM FIELD LET IN
224 %token SHLTOK SRATOK SRLTOK STRCONCATTOK
225 %token <IntVal> INTVAL
226 %token <StrVal> ID VARNAME STRVAL CODEFRAGMENT
229 %type <Rec> ClassInst DefInst MultiClassDef ObjectBody ClassID
230 %type <RecList> MultiClassBody
232 %type <SubClassRef> SubClassRef
233 %type <SubClassList> ClassList ClassListNE
234 %type <IntVal> OptPrefix
235 %type <Initializer> Value OptValue IDValue
236 %type <DagValueList> DagArgList DagArgListNE
237 %type <FieldList> ValueList ValueListNE
238 %type <BitList> BitList OptBitList RBitList
239 %type <StrVal> Declaration OptID OptVarName ObjectName
247 // If CurDefmPrefix is set, we're parsing a defm, which means that this is
248 // actually the name of a multiclass.
249 MultiClass *MC = MultiClasses[*$1];
251 err() << "Couldn't find class '" << *$1 << "'!\n";
256 $$ = Records.getClass(*$1);
259 err() << "Couldn't find class '" << *$1 << "'!\n";
267 Type : STRING { // string type
268 $$ = new StringRecTy();
269 } | BIT { // bit type
271 } | BITS '<' INTVAL '>' { // bits<x> type
272 $$ = new BitsRecTy($3);
273 } | INT { // int type
275 } | LIST '<' Type '>' { // list<x> type
276 $$ = new ListRecTy($3);
277 } | CODE { // code type
278 $$ = new CodeRecTy();
279 } | DAG { // dag type
281 } | ClassID { // Record Type
282 $$ = new RecordRecTy($1);
285 OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; };
287 OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; };
290 if (const RecordVal *RV = (CurRec ? CurRec->getValue(*$1) : 0)) {
291 $$ = new VarInit(*$1, RV->getType());
292 } else if (CurRec && CurRec->isTemplateArg(CurRec->getName()+":"+*$1)) {
293 const RecordVal *RV = CurRec->getValue(CurRec->getName()+":"+*$1);
294 assert(RV && "Template arg doesn't exist??");
295 $$ = new VarInit(CurRec->getName()+":"+*$1, RV->getType());
296 } else if (CurMultiClass &&
297 CurMultiClass->Rec.isTemplateArg(CurMultiClass->Rec.getName()+"::"+*$1)) {
298 std::string Name = CurMultiClass->Rec.getName()+"::"+*$1;
299 const RecordVal *RV = CurMultiClass->Rec.getValue(Name);
300 assert(RV && "Template arg doesn't exist??");
301 $$ = new VarInit(Name, RV->getType());
302 } else if (Record *D = Records.getDef(*$1)) {
305 err() << "Variable not defined: '" << *$1 << "'!\n";
315 $$ = new IntInit($1);
317 $$ = new StringInit(*$1);
320 $$ = new CodeInit(*$1);
323 $$ = new UnsetInit();
324 } | '{' ValueList '}' {
325 BitsInit *Init = new BitsInit($2->size());
326 for (unsigned i = 0, e = $2->size(); i != e; ++i) {
327 struct Init *Bit = (*$2)[i]->convertInitializerTo(new BitRecTy());
329 err() << "Element #" << i << " (" << *(*$2)[i]
330 << ") is not convertable to a bit!\n";
333 Init->setBit($2->size()-i-1, Bit);
337 } | ID '<' ValueListNE '>' {
338 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
339 // a new anonymous definition, deriving from CLASS<initvalslist> with no
341 Record *Class = Records.getClass(*$1);
343 err() << "Expected a class, got '" << *$1 << "'!\n";
348 static unsigned AnonCounter = 0;
349 Record *OldRec = CurRec; // Save CurRec.
351 // Create the new record, set it as CurRec temporarily.
352 CurRec = new Record("anonymous.val."+utostr(AnonCounter++));
353 addSubClass(Class, *$3); // Add info about the subclass to CurRec.
354 delete $3; // Free up the template args.
356 CurRec->resolveReferences();
358 Records.addDef(CurRec);
360 // The result of the expression is a reference to the new record.
361 $$ = new DefInit(CurRec);
363 // Restore the old CurRec
365 } | Value '{' BitList '}' {
366 $$ = $1->convertInitializerBitRange(*$3);
368 err() << "Invalid bit range for value '" << *$1 << "'!\n";
372 } | '[' ValueList ']' {
373 $$ = new ListInit(*$2);
376 if (!$1->getFieldType(*$3)) {
377 err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n";
380 $$ = new FieldInit($1, *$3);
382 } | '(' IDValue DagArgList ')' {
383 $$ = new DagInit($2, *$3);
385 } | Value '[' BitList ']' {
386 std::reverse($3->begin(), $3->end());
387 $$ = $1->convertInitListSlice(*$3);
389 err() << "Invalid list slice for value '" << *$1 << "'!\n";
393 } | SHLTOK '(' Value ',' Value ')' {
394 $$ = (new BinOpInit(BinOpInit::SHL, $3, $5))->Fold();
395 } | SRATOK '(' Value ',' Value ')' {
396 $$ = (new BinOpInit(BinOpInit::SRA, $3, $5))->Fold();
397 } | SRLTOK '(' Value ',' Value ')' {
398 $$ = (new BinOpInit(BinOpInit::SRL, $3, $5))->Fold();
399 } | STRCONCATTOK '(' Value ',' Value ')' {
400 $$ = (new BinOpInit(BinOpInit::STRCONCAT, $3, $5))->Fold();
403 OptVarName : /* empty */ {
404 $$ = new std::string();
410 DagArgListNE : Value OptVarName {
411 $$ = new std::vector<std::pair<Init*, std::string> >();
412 $$->push_back(std::make_pair($1, *$2));
415 | DagArgListNE ',' Value OptVarName {
416 $1->push_back(std::make_pair($3, *$4));
421 DagArgList : /*empty*/ {
422 $$ = new std::vector<std::pair<Init*, std::string> >();
424 | DagArgListNE { $$ = $1; };
428 $$ = new std::vector<unsigned>();
430 } | INTVAL '-' INTVAL {
431 if ($1 < 0 || $3 < 0) {
432 err() << "Invalid range: " << $1 << "-" << $3 << "!\n";
435 $$ = new std::vector<unsigned>();
437 for (int i = $1; i <= $3; ++i)
440 for (int i = $1; i >= $3; --i)
445 if ($1 < 0 || $2 < 0) {
446 err() << "Invalid range: " << $1 << "-" << $2 << "!\n";
449 $$ = new std::vector<unsigned>();
451 for (int i = $1; i <= $2; ++i)
454 for (int i = $1; i >= $2; --i)
457 } | RBitList ',' INTVAL {
458 ($$=$1)->push_back($3);
459 } | RBitList ',' INTVAL '-' INTVAL {
460 if ($3 < 0 || $5 < 0) {
461 err() << "Invalid range: " << $3 << "-" << $5 << "!\n";
466 for (int i = $3; i <= $5; ++i)
469 for (int i = $3; i >= $5; --i)
472 } | RBitList ',' INTVAL INTVAL {
474 if ($3 < 0 || $4 < 0) {
475 err() << "Invalid range: " << $3 << "-" << $4 << "!\n";
480 for (int i = $3; i <= $4; ++i)
483 for (int i = $3; i >= $4; --i)
488 BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); };
490 OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; };
494 ValueList : /*empty*/ {
495 $$ = new std::vector<Init*>();
500 ValueListNE : Value {
501 $$ = new std::vector<Init*>();
503 } | ValueListNE ',' Value {
504 ($$ = $1)->push_back($3);
507 Declaration : OptPrefix Type ID OptValue {
508 std::string DecName = *$3;
509 if (ParsingTemplateArgs) {
511 DecName = CurRec->getName() + ":" + DecName;
513 assert(CurMultiClass);
516 DecName = CurMultiClass->Rec.getName() + "::" + DecName;
519 addValue(RecordVal(DecName, $2, $1));
520 setValue(DecName, 0, $4);
521 $$ = new std::string(DecName);
524 BodyItem : Declaration ';' {
526 } | LET ID OptBitList '=' Value ';' {
527 setValue(*$2, $3, $5);
532 BodyList : /*empty*/ | BodyList BodyItem;
533 Body : ';' | '{' BodyList '}';
535 SubClassRef : ClassID {
536 $$ = new SubClassRefTy($1, new std::vector<Init*>());
537 } | ClassID '<' ValueListNE '>' {
538 $$ = new SubClassRefTy($1, $3);
541 ClassListNE : SubClassRef {
542 $$ = new std::vector<SubClassRefTy>();
546 | ClassListNE ',' SubClassRef {
547 ($$=$1)->push_back(*$3);
551 ClassList : /*empty */ {
552 $$ = new std::vector<SubClassRefTy>();
558 DeclListNE : Declaration {
559 getActiveRec()->addTemplateArg(*$1);
561 } | DeclListNE ',' Declaration {
562 getActiveRec()->addTemplateArg(*$3);
566 TemplateArgList : '<' DeclListNE '>' {};
567 OptTemplateArgList : /*empty*/ | TemplateArgList;
569 OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); };
572 static unsigned AnonCounter = 0;
574 *$1 = "anonymous."+utostr(AnonCounter++);
578 ClassName : ObjectName {
579 // If a class of this name already exists, it must be a forward ref.
580 if ((CurRec = Records.getClass(*$1))) {
581 // If the body was previously defined, this is an error.
582 if (!CurRec->getValues().empty() ||
583 !CurRec->getSuperClasses().empty() ||
584 !CurRec->getTemplateArgs().empty()) {
585 err() << "Class '" << CurRec->getName() << "' already defined!\n";
589 // If this is the first reference to this class, create and add it.
590 CurRec = new Record(*$1);
591 Records.addClass(CurRec);
596 DefName : ObjectName {
597 CurRec = new Record(*$1);
600 if (!CurMultiClass) {
601 // Top-level def definition.
603 // Ensure redefinition doesn't happen.
604 if (Records.getDef(CurRec->getName())) {
605 err() << "def '" << CurRec->getName() << "' already defined!\n";
608 Records.addDef(CurRec);
610 // Otherwise, a def inside a multiclass, add it to the multiclass.
611 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
612 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
613 err() << "def '" << CurRec->getName()
614 << "' already defined in this multiclass!\n";
617 CurMultiClass->DefPrototypes.push_back(CurRec);
621 ObjectBody : ClassList {
622 for (unsigned i = 0, e = $1->size(); i != e; ++i) {
623 addSubClass((*$1)[i].first, *(*$1)[i].second);
624 // Delete the template arg values for the class
625 delete (*$1)[i].second;
627 delete $1; // Delete the class list.
629 // Process any variables on the let stack.
630 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
631 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
632 setValue(LetStack[i][j].Name,
633 LetStack[i][j].HasBits ? &LetStack[i][j].Bits : 0,
634 LetStack[i][j].Value);
640 ClassInst : CLASS ClassName {
641 ParsingTemplateArgs = true;
642 } OptTemplateArgList {
643 ParsingTemplateArgs = false;
648 DefInst : DEF DefName ObjectBody {
649 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
650 $3->resolveReferences();
652 // If ObjectBody has template arguments, it's an error.
653 assert($3->getTemplateArgs().empty() && "How'd this get template args?");
657 // MultiClassDef - A def instance specified inside a multiclass.
658 MultiClassDef : DefInst {
660 // Copy the template arguments for the multiclass into the def.
661 const std::vector<std::string> &TArgs = CurMultiClass->Rec.getTemplateArgs();
663 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
664 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
665 assert(RV && "Template arg doesn't exist?");
670 // MultiClassBody - Sequence of def's that are instantiated when a multiclass is
672 MultiClassBody : MultiClassDef {
673 $$ = new std::vector<Record*>();
675 } | MultiClassBody MultiClassDef {
679 MultiClassName : ID {
680 MultiClass *&MCE = MultiClasses[*$1];
682 err() << "multiclass '" << *$1 << "' already defined!\n";
685 MCE = CurMultiClass = new MultiClass(*$1);
689 // MultiClass - Multiple definitions.
690 MultiClassInst : MULTICLASS MultiClassName {
691 ParsingTemplateArgs = true;
692 } OptTemplateArgList {
693 ParsingTemplateArgs = false;
694 }'{' MultiClassBody '}' {
698 // DefMInst - Instantiate a multiclass.
699 DefMInst : DEFM ID { CurDefmPrefix = $2; } ':' SubClassRef ';' {
700 // To instantiate a multiclass, we need to first get the multiclass, then
701 // instantiate each def contained in the multiclass with the SubClassRef
702 // template parameters.
703 MultiClass *MC = MultiClasses[$5->first->getName()];
704 assert(MC && "Didn't lookup multiclass correctly?");
705 std::vector<Init*> &TemplateVals = *$5->second;
708 // Verify that the correct number of template arguments were specified.
709 const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs();
710 if (TArgs.size() < TemplateVals.size()) {
711 err() << "ERROR: More template args specified than multiclass expects!\n";
715 // Loop over all the def's in the multiclass, instantiating each one.
716 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
717 Record *DefProto = MC->DefPrototypes[i];
719 // Add the suffix to the defm name to get the new name.
720 assert(CurRec == 0 && "A def is current?");
721 CurRec = new Record(*$2 + DefProto->getName());
723 addSubClass(DefProto, std::vector<Init*>());
725 // Loop over all of the template arguments, setting them to the specified
726 // value or leaving them as the default if necessary.
727 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
728 if (i < TemplateVals.size()) { // A value is specified for this temp-arg?
730 setValue(TArgs[i], 0, TemplateVals[i]);
733 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
736 CurRec->removeValue(TArgs[i]);
738 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
739 err() << "ERROR: Value not specified for template argument #"
740 << i << " (" << TArgs[i] << ") of multiclassclass '"
741 << MC->Rec.getName() << "'!\n";
746 // If the mdef is inside a 'let' expression, add to each def.
747 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
748 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
749 setValue(LetStack[i][j].Name,
750 LetStack[i][j].HasBits ? &LetStack[i][j].Bits : 0,
751 LetStack[i][j].Value);
754 // Ensure redefinition doesn't happen.
755 if (Records.getDef(CurRec->getName())) {
756 err() << "def '" << CurRec->getName() << "' already defined, "
757 << "instantiating defm '" << *$2 << "' with subdef '"
758 << DefProto->getName() << "'!\n";
761 Records.addDef(CurRec);
763 CurRec->resolveReferences();
768 delete &TemplateVals;
773 Object : ClassInst {} | DefInst {};
774 Object : MultiClassInst | DefMInst;
776 LETItem : ID OptBitList '=' Value {
777 LetStack.back().push_back(LetRecord(*$1, $2, $4));
778 delete $1; delete $2;
781 LETList : LETItem | LETList ',' LETItem;
783 // LETCommand - A 'LET' statement start...
784 LETCommand : LET { LetStack.push_back(std::vector<LetRecord>()); } LETList IN;
786 // Support Set commands wrapping objects... both with and without braces.
787 Object : LETCommand '{' ObjectList '}' {
790 | LETCommand Object {
794 ObjectList : Object {} | ObjectList Object {};
800 int yyerror(const char *ErrorMsg) {
801 err() << "Error parsing: " << ErrorMsg << "\n";