X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=utils%2FTableGen%2FFileParser.y;h=8e564564bdef4e6a8240bd07b491bb774319afbb;hb=dfdaeb276e50baea18abbe30a85f1c206bb3d154;hp=af8d3d4ff285ae6c252dadcb97a67cce6d84f8a0;hpb=f5761a5e68fa805d58422ca0db5f32f049e77551;p=oota-llvm.git diff --git a/utils/TableGen/FileParser.y b/utils/TableGen/FileParser.y index af8d3d4ff28..8e564564bde 100644 --- a/utils/TableGen/FileParser.y +++ b/utils/TableGen/FileParser.y @@ -13,7 +13,7 @@ %{ #include "Record.h" -#include "Support/StringExtras.h" +#include "llvm/ADT/StringExtras.h" #include #include #define YYERROR_VERBOSE 1 @@ -22,9 +22,21 @@ int yyerror(const char *ErrorMsg); int yylex(); namespace llvm { + struct MultiClass { + Record Rec; // Placeholder for template args and Name. + std::vector DefPrototypes; + + MultiClass(const std::string &Name) : Rec(Name) {} + }; + +static std::map MultiClasses; + extern int Filelineno; +static MultiClass *CurMultiClass = 0; // Set while parsing a multiclass. +static std::string *CurDefmPrefix = 0; // Set while parsing defm. static Record *CurRec = 0; +static bool ParsingTemplateArgs = false; typedef std::pair*> SubClassRefTy; @@ -44,8 +56,16 @@ static std::vector > LetStack; extern std::ostream &err(); +/// getActiveRec - If inside a def/class definition, return the def/class. +/// Otherwise, if within a multidef, return it. +static Record *getActiveRec() { + return CurRec ? CurRec : &CurMultiClass->Rec; +} + static void addValue(const RecordVal &RV) { - if (RecordVal *ERV = CurRec->getValue(RV.getName())) { + Record *TheRec = getActiveRec(); + + if (RecordVal *ERV = TheRec->getValue(RV.getName())) { // The value already exists in the class, treat this as a set... if (ERV->setValue(RV.getValue())) { err() << "New definition of '" << RV.getName() << "' of type '" @@ -54,7 +74,7 @@ static void addValue(const RecordVal &RV) { exit(1); } } else { - CurRec->addValue(RV); + TheRec->addValue(RV); } } @@ -67,14 +87,22 @@ static void addSuperClass(Record *SC) { } static void setValue(const std::string &ValName, - std::vector *BitList, Init *V) { - if (!V) return ; + std::vector *BitList, Init *V) { + if (!V) return; - RecordVal *RV = CurRec->getValue(ValName); + Record *TheRec = getActiveRec(); + RecordVal *RV = TheRec->getValue(ValName); if (RV == 0) { err() << "Value '" << ValName << "' unknown!\n"; exit(1); } + + // Do not allow assignments like 'X = X'. This will just cause infinite loops + // in the resolution machinery. + if (!BitList) + if (VarInit *VI = dynamic_cast(V)) + if (VI->getName() == ValName) + return; // If we are assigning to a subset of the bits in the value... then we must be // assigning to a field of BitsRecTy, which must have a BitsInit @@ -126,6 +154,8 @@ static void setValue(const std::string &ValName, } } +// addSubClass - Add SC as a subclass to CurRec, resolving TemplateArgs as SC's +// template arguments. static void addSubClass(Record *SC, const std::vector &TemplateArgs) { // Add all of the values in the subclass into the current class... const std::vector &Vals = SC->getValues(); @@ -138,26 +168,33 @@ static void addSubClass(Record *SC, const std::vector &TemplateArgs) { if (TArgs.size() < TemplateArgs.size()) { err() << "ERROR: More template args specified than expected!\n"; exit(1); - } else { // This class expects template arguments... - // Loop over all of the template arguments, setting them to the specified - // value or leaving them as the default as necessary. - for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { - if (i < TemplateArgs.size()) { // A value is specified for this temp-arg? - // Set it now. - setValue(TArgs[i], 0, TemplateArgs[i]); - } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) { - err() << "ERROR: Value not specified for template argument #" - << i << " (" << TArgs[i] << ") of subclass '" << SC->getName() - << "'!\n"; - exit(1); - } + } + + // Loop over all of the template arguments, setting them to the specified + // value or leaving them as the default if necessary. + for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { + if (i < TemplateArgs.size()) { // A value is specified for this temp-arg? + // Set it now. + setValue(TArgs[i], 0, TemplateArgs[i]); + + // Resolve it next. + CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i])); + + + // Now remove it. + CurRec->removeValue(TArgs[i]); + + } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) { + err() << "ERROR: Value not specified for template argument #" + << i << " (" << TArgs[i] << ") of subclass '" << SC->getName() + << "'!\n"; + exit(1); } } - // Since everything went well, we can now set the "superclass" list for the // current record. - const std::vector &SCs = SC->getSuperClasses(); + const std::vector &SCs = SC->getSuperClasses(); for (unsigned i = 0, e = SCs.size(); i != e; ++i) addSuperClass(SCs[i]); addSuperClass(SC); @@ -177,33 +214,47 @@ using namespace llvm; std::vector* FieldList; std::vector* BitList; llvm::Record* Rec; + std::vector* RecList; SubClassRefTy* SubClassRef; std::vector* SubClassList; std::vector >* DagValueList; }; -%token INT BIT STRING BITS LIST CODE DAG CLASS DEF FIELD LET IN +%token INT BIT STRING BITS LIST CODE DAG CLASS DEF MULTICLASS DEFM FIELD LET IN +%token SHLTOK SRATOK SRLTOK STRCONCATTOK %token INTVAL %token ID VARNAME STRVAL CODEFRAGMENT %type Type -%type ClassInst DefInst Object ObjectBody ClassID +%type ClassInst DefInst MultiClassDef ObjectBody ClassID +%type MultiClassBody %type SubClassRef %type ClassList ClassListNE %type OptPrefix -%type Value OptValue +%type Value OptValue IDValue %type DagArgList DagArgListNE %type ValueList ValueListNE %type BitList OptBitList RBitList -%type Declaration OptID OptVarName +%type Declaration OptID OptVarName ObjectName %start File %% ClassID : ID { - $$ = Records.getClass(*$1); + if (CurDefmPrefix) { + // If CurDefmPrefix is set, we're parsing a defm, which means that this is + // actually the name of a multiclass. + MultiClass *MC = MultiClasses[*$1]; + if (MC == 0) { + err() << "Couldn't find class '" << *$1 << "'!\n"; + exit(1); + } + $$ = &MC->Rec; + } else { + $$ = Records.getClass(*$1); + } if ($$ == 0) { err() << "Couldn't find class '" << *$1 << "'!\n"; exit(1); @@ -235,7 +286,32 @@ OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; }; OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; }; -Value : INTVAL { +IDValue : ID { + if (const RecordVal *RV = (CurRec ? CurRec->getValue(*$1) : 0)) { + $$ = new VarInit(*$1, RV->getType()); + } else if (CurRec && CurRec->isTemplateArg(CurRec->getName()+":"+*$1)) { + const RecordVal *RV = CurRec->getValue(CurRec->getName()+":"+*$1); + assert(RV && "Template arg doesn't exist??"); + $$ = new VarInit(CurRec->getName()+":"+*$1, RV->getType()); + } else if (CurMultiClass && + CurMultiClass->Rec.isTemplateArg(CurMultiClass->Rec.getName()+"::"+*$1)) { + std::string Name = CurMultiClass->Rec.getName()+"::"+*$1; + const RecordVal *RV = CurMultiClass->Rec.getValue(Name); + assert(RV && "Template arg doesn't exist??"); + $$ = new VarInit(Name, RV->getType()); + } else if (Record *D = Records.getDef(*$1)) { + $$ = new DefInit(D); + } else { + err() << "Variable not defined: '" << *$1 << "'!\n"; + exit(1); + } + + delete $1; +}; + +Value : IDValue { + $$ = $1; + } | INTVAL { $$ = new IntInit($1); } | STRVAL { $$ = new StringInit(*$1); @@ -250,25 +326,42 @@ Value : INTVAL { for (unsigned i = 0, e = $2->size(); i != e; ++i) { struct Init *Bit = (*$2)[i]->convertInitializerTo(new BitRecTy()); if (Bit == 0) { - err() << "Element #" << i << " (" << *(*$2)[i] - << ") is not convertable to a bit!\n"; - exit(1); + err() << "Element #" << i << " (" << *(*$2)[i] + << ") is not convertable to a bit!\n"; + exit(1); } Init->setBit($2->size()-i-1, Bit); } $$ = Init; delete $2; - } | ID { - if (const RecordVal *RV = (CurRec ? CurRec->getValue(*$1) : 0)) { - $$ = new VarInit(*$1, RV->getType()); - } else if (Record *D = Records.getDef(*$1)) { - $$ = new DefInit(D); - } else { - err() << "Variable not defined: '" << *$1 << "'!\n"; + } | ID '<' ValueListNE '>' { + // This is a CLASS expression. This is supposed to synthesize + // a new anonymous definition, deriving from CLASS with no + // body. + Record *Class = Records.getClass(*$1); + if (!Class) { + err() << "Expected a class, got '" << *$1 << "'!\n"; exit(1); } - delete $1; + + static unsigned AnonCounter = 0; + Record *OldRec = CurRec; // Save CurRec. + + // Create the new record, set it as CurRec temporarily. + CurRec = new Record("anonymous.val."+utostr(AnonCounter++)); + addSubClass(Class, *$3); // Add info about the subclass to CurRec. + delete $3; // Free up the template args. + + CurRec->resolveReferences(); + + Records.addDef(CurRec); + + // The result of the expression is a reference to the new record. + $$ = new DefInit(CurRec); + + // Restore the old CurRec + CurRec = OldRec; } | Value '{' BitList '}' { $$ = $1->convertInitializerBitRange(*$3); if ($$ == 0) { @@ -286,14 +379,25 @@ Value : INTVAL { } $$ = new FieldInit($1, *$3); delete $3; - } | '(' ID DagArgList ')' { - Record *D = Records.getDef(*$2); - if (D == 0) { - err() << "Invalid def '" << *$2 << "'!\n"; + } | '(' IDValue DagArgList ')' { + $$ = new DagInit($2, *$3); + delete $3; + } | Value '[' BitList ']' { + std::reverse($3->begin(), $3->end()); + $$ = $1->convertInitListSlice(*$3); + if ($$ == 0) { + err() << "Invalid list slice for value '" << *$1 << "'!\n"; exit(1); } - $$ = new DagInit(D, *$3); - delete $2; delete $3; + delete $3; + } | SHLTOK '(' Value ',' Value ')' { + $$ = (new BinOpInit(BinOpInit::SHL, $3, $5))->Fold(); + } | SRATOK '(' Value ',' Value ')' { + $$ = (new BinOpInit(BinOpInit::SRA, $3, $5))->Fold(); + } | SRLTOK '(' Value ',' Value ')' { + $$ = (new BinOpInit(BinOpInit::SRL, $3, $5))->Fold(); + } | STRCONCATTOK '(' Value ',' Value ')' { + $$ = (new BinOpInit(BinOpInit::STRCONCAT, $3, $5))->Fold(); }; OptVarName : /* empty */ { @@ -324,41 +428,61 @@ RBitList : INTVAL { $$ = new std::vector(); $$->push_back($1); } | INTVAL '-' INTVAL { - if ($1 < $3 || $1 < 0 || $3 < 0) { - err() << "Invalid bit range: " << $1 << "-" << $3 << "!\n"; + if ($1 < 0 || $3 < 0) { + err() << "Invalid range: " << $1 << "-" << $3 << "!\n"; exit(1); } $$ = new std::vector(); - for (int i = $1; i >= $3; --i) - $$->push_back(i); + if ($1 < $3) { + for (int i = $1; i <= $3; ++i) + $$->push_back(i); + } else { + for (int i = $1; i >= $3; --i) + $$->push_back(i); + } } | INTVAL INTVAL { $2 = -$2; - if ($1 < $2 || $1 < 0 || $2 < 0) { - err() << "Invalid bit range: " << $1 << "-" << $2 << "!\n"; + if ($1 < 0 || $2 < 0) { + err() << "Invalid range: " << $1 << "-" << $2 << "!\n"; exit(1); } $$ = new std::vector(); - for (int i = $1; i >= $2; --i) - $$->push_back(i); + if ($1 < $2) { + for (int i = $1; i <= $2; ++i) + $$->push_back(i); + } else { + for (int i = $1; i >= $2; --i) + $$->push_back(i); + } } | RBitList ',' INTVAL { ($$=$1)->push_back($3); } | RBitList ',' INTVAL '-' INTVAL { - if ($3 < $5 || $3 < 0 || $5 < 0) { - err() << "Invalid bit range: " << $3 << "-" << $5 << "!\n"; + if ($3 < 0 || $5 < 0) { + err() << "Invalid range: " << $3 << "-" << $5 << "!\n"; exit(1); } $$ = $1; - for (int i = $3; i >= $5; --i) - $$->push_back(i); + if ($3 < $5) { + for (int i = $3; i <= $5; ++i) + $$->push_back(i); + } else { + for (int i = $3; i >= $5; --i) + $$->push_back(i); + } } | RBitList ',' INTVAL INTVAL { $4 = -$4; - if ($3 < $4 || $3 < 0 || $4 < 0) { - err() << "Invalid bit range: " << $3 << "-" << $4 << "!\n"; + if ($3 < 0 || $4 < 0) { + err() << "Invalid range: " << $3 << "-" << $4 << "!\n"; exit(1); } $$ = $1; - for (int i = $3; i >= $4; --i) - $$->push_back(i); + if ($3 < $4) { + for (int i = $3; i <= $4; ++i) + $$->push_back(i); + } else { + for (int i = $3; i >= $4; --i) + $$->push_back(i); + } }; BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); }; @@ -381,9 +505,20 @@ ValueListNE : Value { }; Declaration : OptPrefix Type ID OptValue { - addValue(RecordVal(*$3, $2, $1)); - setValue(*$3, 0, $4); - $$ = $3; + std::string DecName = *$3; + if (ParsingTemplateArgs) { + if (CurRec) { + DecName = CurRec->getName() + ":" + DecName; + } else { + assert(CurMultiClass); + } + if (CurMultiClass) + DecName = CurMultiClass->Rec.getName() + "::" + DecName; + } + + addValue(RecordVal(DecName, $2, $1)); + setValue(DecName, 0, $4); + $$ = new std::string(DecName); }; BodyItem : Declaration ';' { @@ -421,10 +556,10 @@ ClassList : /*empty */ { }; DeclListNE : Declaration { - CurRec->addTemplateArg(*$1); + getActiveRec()->addTemplateArg(*$1); delete $1; } | DeclListNE ',' Declaration { - CurRec->addTemplateArg(*$3); + getActiveRec()->addTemplateArg(*$3); delete $3; }; @@ -433,66 +568,210 @@ OptTemplateArgList : /*empty*/ | TemplateArgList; OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); }; -ObjectBody : OptID { - static unsigned AnonCounter = 0; - if ($1->empty()) - *$1 = "anonymous."+utostr(AnonCounter++); - CurRec = new Record(*$1); - delete $1; - } OptTemplateArgList ClassList { - for (unsigned i = 0, e = $4->size(); i != e; ++i) { - addSubClass((*$4)[i].first, *(*$4)[i].second); +ObjectName : OptID { + static unsigned AnonCounter = 0; + if ($1->empty()) + *$1 = "anonymous."+utostr(AnonCounter++); + $$ = $1; +}; + +ClassName : ObjectName { + // If a class of this name already exists, it must be a forward ref. + if ((CurRec = Records.getClass(*$1))) { + // If the body was previously defined, this is an error. + if (!CurRec->getValues().empty() || + !CurRec->getSuperClasses().empty() || + !CurRec->getTemplateArgs().empty()) { + err() << "Class '" << CurRec->getName() << "' already defined!\n"; + exit(1); + } + } else { + // If this is the first reference to this class, create and add it. + CurRec = new Record(*$1); + Records.addClass(CurRec); + } + delete $1; +}; + +DefName : ObjectName { + CurRec = new Record(*$1); + delete $1; + + if (!CurMultiClass) { + // Top-level def definition. + + // Ensure redefinition doesn't happen. + if (Records.getDef(CurRec->getName())) { + err() << "def '" << CurRec->getName() << "' already defined!\n"; + exit(1); + } + Records.addDef(CurRec); + } else { + // Otherwise, a def inside a multiclass, add it to the multiclass. + for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i) + if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) { + err() << "def '" << CurRec->getName() + << "' already defined in this multiclass!\n"; + exit(1); + } + CurMultiClass->DefPrototypes.push_back(CurRec); + } +}; + +ObjectBody : ClassList { + for (unsigned i = 0, e = $1->size(); i != e; ++i) { + addSubClass((*$1)[i].first, *(*$1)[i].second); // Delete the template arg values for the class - delete (*$4)[i].second; + delete (*$1)[i].second; } - - // Process any variables on the set stack... - for (unsigned i = 0, e = LetStack.size(); i != e; ++i) + delete $1; // Delete the class list. + + // Process any variables on the let stack. + for (unsigned i = 0, e = LetStack.size(); i != e; ++i) for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j) setValue(LetStack[i][j].Name, LetStack[i][j].HasBits ? &LetStack[i][j].Bits : 0, LetStack[i][j].Value); } Body { - CurRec->resolveReferences(); - - // Now that all of the references have been resolved, we can delete template - // arguments for superclasses, so they don't pollute our record, and so that - // their names won't conflict with later uses of the name... - for (unsigned i = 0, e = $4->size(); i != e; ++i) { - Record *SuperClass = (*$4)[i].first; - for (unsigned i = 0, e = SuperClass->getTemplateArgs().size(); i != e; ++i) - CurRec->removeValue(SuperClass->getTemplateArgs()[i]); + $$ = CurRec; + CurRec = 0; + }; + +ClassInst : CLASS ClassName { + ParsingTemplateArgs = true; + } OptTemplateArgList { + ParsingTemplateArgs = false; + } ObjectBody { + $$ = $6; + }; + +DefInst : DEF DefName ObjectBody { + if (CurMultiClass == 0) // Def's in multiclasses aren't really defs. + $3->resolveReferences(); + + // If ObjectBody has template arguments, it's an error. + assert($3->getTemplateArgs().empty() && "How'd this get template args?"); + $$ = $3; +}; + +// MultiClassDef - A def instance specified inside a multiclass. +MultiClassDef : DefInst { + $$ = $1; + // Copy the template arguments for the multiclass into the def. + const std::vector &TArgs = CurMultiClass->Rec.getTemplateArgs(); + + for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { + const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]); + assert(RV && "Template arg doesn't exist?"); + $$->addValue(*RV); } - delete $4; // Delete the class list... +}; - $$ = CurRec; - CurRec = 0; +// MultiClassBody - Sequence of def's that are instantiated when a multiclass is +// used. +MultiClassBody : MultiClassDef { + $$ = new std::vector(); + $$->push_back($1); +} | MultiClassBody MultiClassDef { + $$->push_back($2); }; -ClassInst : CLASS ObjectBody { - if (Records.getClass($2->getName())) { - err() << "Class '" << $2->getName() << "' already defined!\n"; +MultiClassName : ID { + MultiClass *&MCE = MultiClasses[*$1]; + if (MCE) { + err() << "multiclass '" << *$1 << "' already defined!\n"; exit(1); } - Records.addClass($$ = $2); + MCE = CurMultiClass = new MultiClass(*$1); + delete $1; +}; + +// MultiClass - Multiple definitions. +MultiClassInst : MULTICLASS MultiClassName { + ParsingTemplateArgs = true; + } OptTemplateArgList { + ParsingTemplateArgs = false; + }'{' MultiClassBody '}' { + CurMultiClass = 0; }; -DefInst : DEF ObjectBody { - if (!$2->getTemplateArgs().empty()) { - err() << "Def '" << $2->getName() - << "' is not permitted to have template arguments!\n"; +// DefMInst - Instantiate a multiclass. +DefMInst : DEFM ID { CurDefmPrefix = $2; } ':' SubClassRef ';' { + // To instantiate a multiclass, we need to first get the multiclass, then + // instantiate each def contained in the multiclass with the SubClassRef + // template parameters. + MultiClass *MC = MultiClasses[$5->first->getName()]; + assert(MC && "Didn't lookup multiclass correctly?"); + std::vector &TemplateVals = *$5->second; + delete $5; + + // Verify that the correct number of template arguments were specified. + const std::vector &TArgs = MC->Rec.getTemplateArgs(); + if (TArgs.size() < TemplateVals.size()) { + err() << "ERROR: More template args specified than multiclass expects!\n"; exit(1); } - // If ObjectBody has template arguments, it's an error. - if (Records.getDef($2->getName())) { - err() << "Def '" << $2->getName() << "' already defined!\n"; - exit(1); + + // Loop over all the def's in the multiclass, instantiating each one. + for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) { + Record *DefProto = MC->DefPrototypes[i]; + + // Add the suffix to the defm name to get the new name. + assert(CurRec == 0 && "A def is current?"); + CurRec = new Record(*$2 + DefProto->getName()); + + addSubClass(DefProto, std::vector()); + + // Loop over all of the template arguments, setting them to the specified + // value or leaving them as the default if necessary. + for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { + if (i < TemplateVals.size()) { // A value is specified for this temp-arg? + // Set it now. + setValue(TArgs[i], 0, TemplateVals[i]); + + // Resolve it next. + CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i])); + + // Now remove it. + CurRec->removeValue(TArgs[i]); + + } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) { + err() << "ERROR: Value not specified for template argument #" + << i << " (" << TArgs[i] << ") of multiclassclass '" + << MC->Rec.getName() << "'!\n"; + exit(1); + } + } + + // If the mdef is inside a 'let' expression, add to each def. + for (unsigned i = 0, e = LetStack.size(); i != e; ++i) + for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j) + setValue(LetStack[i][j].Name, + LetStack[i][j].HasBits ? &LetStack[i][j].Bits : 0, + LetStack[i][j].Value); + + + // Ensure redefinition doesn't happen. + if (Records.getDef(CurRec->getName())) { + err() << "def '" << CurRec->getName() << "' already defined, " + << "instantiating defm '" << *$2 << "' with subdef '" + << DefProto->getName() << "'!\n"; + exit(1); + } + Records.addDef(CurRec); + + CurRec->resolveReferences(); + + CurRec = 0; } - Records.addDef($$ = $2); + + delete &TemplateVals; + delete $2; + CurDefmPrefix = 0; }; - -Object : ClassInst | DefInst; +Object : ClassInst {} | DefInst {}; +Object : MultiClassInst | DefMInst; LETItem : ID OptBitList '=' Value { LetStack.back().push_back(LetRecord(*$1, $2, $4)); @@ -514,7 +793,7 @@ Object : LETCommand '{' ObjectList '}' { ObjectList : Object {} | ObjectList Object {}; -File : ObjectList {}; +File : ObjectList; %%