Add support for automatically created anonymous definitions.
[oota-llvm.git] / utils / TableGen / FileParser.y
index d248242b2d6be08ad7f40fc0c9c277b4db854eff..319b0e3695bac21197deb63975afb45664667cba 100644 (file)
@@ -1,54 +1,88 @@
 //===-- FileParser.y - Parser for TableGen files ----------------*- C++ -*-===//
+// 
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
 //
 //  This file implements the bison parser for Table Generator files...
 //
-//===------------------------------------------------------------------------=//
+//===----------------------------------------------------------------------===//
 
 %{
 #include "Record.h"
-#include "Support/StringExtras.h"
+#include "llvm/ADT/StringExtras.h"
 #include <algorithm>
 #include <cstdio>
 #define YYERROR_VERBOSE 1
 
 int yyerror(const char *ErrorMsg);
 int yylex();
+
+namespace llvm {
+
 extern int Filelineno;
 static Record *CurRec = 0;
+static bool ParsingTemplateArgs = false;
 
 typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
 
-static std::vector<std::pair<std::pair<std::string, std::vector<unsigned>*>,
-                             Init*> > SetStack;
+struct LetRecord {
+  std::string Name;
+  std::vector<unsigned> Bits;
+  Init *Value;
+  bool HasBits;
+  LetRecord(const std::string &N, std::vector<unsigned> *B, Init *V)
+    : Name(N), Value(V), HasBits(B != 0) {
+    if (HasBits) Bits = *B;
+  }
+};
+
+static std::vector<std::vector<LetRecord> > LetStack;
+
 
 extern std::ostream &err();
 
 static void addValue(const RecordVal &RV) {
-  if (CurRec->getValue(RV.getName())) {
-    err() << "Value '" << RV.getName() << "' multiply defined!\n";
-    abort();
+  if (RecordVal *ERV = CurRec->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 '"
+            << *RV.getType() << "' is incompatible with previous "
+            << "definition of type '" << *ERV->getType() << "'!\n";
+      exit(1);
+    }
+  } else {
+    CurRec->addValue(RV);
   }
-
-  CurRec->addValue(RV);
 }
 
 static void addSuperClass(Record *SC) {
   if (CurRec->isSubClassOf(SC)) {
     err() << "Already subclass of '" << SC->getName() << "'!\n";
-    abort();
+    exit(1);
   }
   CurRec->addSuperClass(SC);
 }
 
 static void setValue(const std::string &ValName, 
                     std::vector<unsigned> *BitList, Init *V) {
-  if (!V) return ;
+  if (!V) return;
 
   RecordVal *RV = CurRec->getValue(ValName);
   if (RV == 0) {
     err() << "Value '" << ValName << "' unknown!\n";
-    abort();
+    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<VarInit*>(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
@@ -58,7 +92,7 @@ static void setValue(const std::string &ValName,
     BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
     if (CurVal == 0) {
       err() << "Value '" << ValName << "' is not a bits type!\n";
-      abort();
+      exit(1);
     }
 
     // Convert the incoming value to a bits type of the appropriate size...
@@ -66,7 +100,7 @@ static void setValue(const std::string &ValName,
     if (BI == 0) {
       V->convertInitializerTo(new BitsRecTy(BitList->size()));
       err() << "Initializer '" << *V << "' not compatible with bit range!\n";
-      abort();
+      exit(1);
     }
 
     // We should have a BitsInit type now...
@@ -81,7 +115,7 @@ static void setValue(const std::string &ValName,
       if (NewVal->getBit(Bit)) {
         err() << "Cannot set bit #" << Bit << " of value '" << ValName
               << "' more than once!\n";
-        abort();
+        exit(1);
       }
       NewVal->setBit(Bit, BInit->getBit(i));
     }
@@ -96,10 +130,12 @@ static void setValue(const std::string &ValName,
   if (RV->setValue(V)) {
     err() << "Value '" << ValName << "' of type '" << *RV->getType()
          << "' is incompatible with initializer '" << *V << "'!\n";
-    abort();
+    exit(1);
   }
 }
 
+// addSubClass - Add SC as a subclass to CurRec, resolving TemplateArgs as SC's
+// template arguments.
 static void addSubClass(Record *SC, const std::vector<Init*> &TemplateArgs) {
   // Add all of the values in the subclass into the current class...
   const std::vector<RecordVal> &Vals = SC->getValues();
@@ -111,81 +147,84 @@ static void addSubClass(Record *SC, const std::vector<Init*> &TemplateArgs) {
   // Ensure that an appropriate number of template arguments are specified...
   if (TArgs.size() < TemplateArgs.size()) {
     err() << "ERROR: More template args specified than expected!\n";
-    abort();
+    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 neccesary.
+    // 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]);
+        // 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";
-       abort();
+        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<Record*>   &SCs  = SC->getSuperClasses();
+  const std::vector<Record*> &SCs  = SC->getSuperClasses();
   for (unsigned i = 0, e = SCs.size(); i != e; ++i)
     addSuperClass(SCs[i]);
   addSuperClass(SC);
 }
 
+} // End llvm namespace
+
+using namespace llvm;
 
 %}
 
 %union {
-  std::string          *StrVal;
-  int                   IntVal;
-  RecTy                *Ty;
-  Init                 *Initializer;
-  std::vector<Init*>   *FieldList;
-  std::vector<Record*> *RecPtr;
-  std::vector<unsigned>*BitList;
-  Record               *Rec;
-  SubClassRefTy        *SubClassRef;
-  std::vector<SubClassRefTy> *SubClassList;
+  std::string*                StrVal;
+  int                         IntVal;
+  llvm::RecTy*                Ty;
+  llvm::Init*                 Initializer;
+  std::vector<llvm::Init*>*   FieldList;
+  std::vector<unsigned>*      BitList;
+  llvm::Record*               Rec;
+  SubClassRefTy*              SubClassRef;
+  std::vector<SubClassRefTy>* SubClassList;
+  std::vector<std::pair<llvm::Init*, std::string> >* DagValueList;
 };
 
-%token INT BIT STRING BITS LIST CODE CLASS DEF FIELD SET IN
+%token INT BIT STRING BITS LIST CODE DAG CLASS DEF FIELD LET IN
+%token SHLTOK SRATOK SRLTOK
 %token <IntVal>      INTVAL
-%token <StrVal>      ID STRVAL CODEFRAGMENT
+%token <StrVal>      ID VARNAME STRVAL CODEFRAGMENT
 
 %type <Ty>           Type
-%type <RecPtr>       DefList DefListNE
-%type <Rec>          ClassInst DefInst Object ObjectBody ClassID DefID
+%type <Rec>          ClassInst DefInst Object ObjectBody ClassID
 
 %type <SubClassRef>  SubClassRef
 %type <SubClassList> ClassList ClassListNE
 %type <IntVal>       OptPrefix
 %type <Initializer>  Value OptValue
+%type <DagValueList> DagArgList DagArgListNE
 %type <FieldList>    ValueList ValueListNE
 %type <BitList>      BitList OptBitList RBitList
-%type <StrVal>       Declaration OptID
+%type <StrVal>       Declaration OptID OptVarName
 
 %start File
+
 %%
 
 ClassID : ID {
     $$ = Records.getClass(*$1);
     if ($$ == 0) {
       err() << "Couldn't find class '" << *$1 << "'!\n";
-      abort();
-    }
-    delete $1;
-  };
-
-DefID : ID {
-    $$ = Records.getDef(*$1);
-    if ($$ == 0) {
-      err() << "Couldn't find def '" << *$1 << "'!\n";
-      abort();
+      exit(1);
     }
     delete $1;
   };
@@ -200,10 +239,12 @@ Type : STRING {                       // string type
     $$ = new BitsRecTy($3);
   } | INT {                           // int type
     $$ = new IntRecTy();
-  } | LIST '<' ClassID '>' {          // list<x> type
+  } | LIST '<' Type '>'    {          // list<x> type
     $$ = new ListRecTy($3);
   } | CODE {                          // code type
     $$ = new CodeRecTy();
+  } | DAG {                           // dag type
+    $$ = new DagRecTy();
   } | ClassID {                       // Record Type
     $$ = new RecordRecTy($1);
   };
@@ -227,100 +268,193 @@ 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";
-       abort();
+        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 (CurRec == 0) {
-      err() << "Def/Class name '" << *$1 << "' not allowed here!\n";
-      abort();
-    }
-    if (const RecordVal *RV = CurRec->getValue(*$1)) {
+    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 (Record *D = Records.getDef(*$1)) {
       $$ = new DefInit(D);
     } else {
       err() << "Variable not defined: '" << *$1 << "'!\n";
-      abort();
+      exit(1);
     }
     
     delete $1;
+  } | ID '<' ValueListNE '>' {
+    // This is a CLASS<initvalslist> expression.  This is supposed to synthesize
+    // a new anonymous definition, deriving from CLASS<initvalslist> 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) {
       err() << "Invalid bit range for value '" << *$1 << "'!\n";
-      abort();
+      exit(1);
     }
     delete $3;
-  } | '[' DefList ']' {
+  } | '[' ValueList ']' {
     $$ = new ListInit(*$2);
     delete $2;
   } | Value '.' ID {
     if (!$1->getFieldType(*$3)) {
       err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n";
-      abort();
+      exit(1);
     }
     $$ = new FieldInit($1, *$3);
     delete $3;
+  } | '(' ID DagArgList ')' {
+    Record *D = Records.getDef(*$2);
+    if (D == 0) {
+      err() << "Invalid def '" << *$2 << "'!\n";
+      exit(1);
+    }
+    $$ = new DagInit(D, *$3);
+    delete $2; 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);
+    }
+    delete $3;
+  } | SHLTOK '(' Value ',' Value ')' {
+    $$ = $3->getBinaryOp(Init::SHL, $5);
+    if ($$ == 0) {
+      err() << "Cannot shift values '" << *$3 << "' and '" << *$5 << "'!\n";
+      exit(1);
+    }
+  } | SRATOK '(' Value ',' Value ')' {
+    $$ = $3->getBinaryOp(Init::SRA, $5);
+    if ($$ == 0) {
+      err() << "Cannot shift values '" << *$3 << "' and '" << *$5 << "'!\n";
+      exit(1);
+    }
+  } | SRLTOK '(' Value ',' Value ')' {
+    $$ = $3->getBinaryOp(Init::SRL, $5);
+    if ($$ == 0) {
+      err() << "Cannot shift values '" << *$3 << "' and '" << *$5 << "'!\n";
+      exit(1);
+    }
   };
 
-DefList : /*empty */ {
-    $$ = new std::vector<Record*>();
-  } | DefListNE {
-    $$ = $1;
+OptVarName : /* empty */ {
+    $$ = new std::string();
+  }
+  | ':' VARNAME {
+    $$ = $2;
   };
-DefListNE : DefID {
-    $$ = new std::vector<Record*>();
-    $$->push_back($1);
-  } | DefListNE ',' DefID {
-    ($$=$1)->push_back($3);
+
+DagArgListNE : Value OptVarName {
+    $$ = new std::vector<std::pair<Init*, std::string> >();
+    $$->push_back(std::make_pair($1, *$2));
+    delete $2;
+  }
+  | DagArgListNE ',' Value OptVarName {
+    $1->push_back(std::make_pair($3, *$4));
+    delete $4;
+    $$ = $1;
   };
 
+DagArgList : /*empty*/ {
+    $$ = new std::vector<std::pair<Init*, std::string> >();
+  }
+  | DagArgListNE { $$ = $1; };
+
 
 RBitList : INTVAL {
     $$ = new std::vector<unsigned>();
     $$->push_back($1);
   } | INTVAL '-' INTVAL {
-    if ($1 < $3 || $1 < 0 || $3 < 0) {
-      err() << "Invalid bit range: " << $1 << "-" << $3 << "!\n";
-      abort();
+    if ($1 < 0 || $3 < 0) {
+      err() << "Invalid range: " << $1 << "-" << $3 << "!\n";
+      exit(1);
     }
     $$ = new std::vector<unsigned>();
-    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";
-      abort();
+    if ($1 < 0 || $2 < 0) {
+      err() << "Invalid range: " << $1 << "-" << $2 << "!\n";
+      exit(1);
     }
     $$ = new std::vector<unsigned>();
-    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";
-      abort();
+    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";
-      abort();
+    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()); };
@@ -343,14 +477,18 @@ ValueListNE : Value {
   };
 
 Declaration : OptPrefix Type ID OptValue {
-  addValue(RecordVal(*$3, $2, $1));
-  setValue(*$3, 0, $4);
-  $$ = $3;
+  std::string DecName = *$3;
+  if (ParsingTemplateArgs)
+    DecName = CurRec->getName() + ":" + DecName;
+
+  addValue(RecordVal(DecName, $2, $1));
+  setValue(DecName, 0, $4);
+  $$ = new std::string(DecName);
 };
 
 BodyItem : Declaration ';' {
   delete $1;
-} | SET ID OptBitList '=' Value ';' {
+} | LET ID OptBitList '=' Value ';' {
   setValue(*$2, $3, $5);
   delete $2;
   delete $3;
@@ -401,52 +539,48 @@ ObjectBody : OptID {
              *$1 = "anonymous."+utostr(AnonCounter++);
            CurRec = new Record(*$1);
            delete $1;
+           ParsingTemplateArgs = true;
          } OptTemplateArgList ClassList {
+           ParsingTemplateArgs = false;
            for (unsigned i = 0, e = $4->size(); i != e; ++i) {
-            addSubClass((*$4)[i].first, *(*$4)[i].second);
+             addSubClass((*$4)[i].first, *(*$4)[i].second);
              // Delete the template arg values for the class
              delete (*$4)[i].second;
            }
-
-          // Process any variables on the set stack...
-          for (unsigned i = 0, e = SetStack.size(); i != e; ++i)
-            setValue(SetStack[i].first.first, SetStack[i].first.second,
-                     SetStack[i].second);
+           delete $4;   // Delete the class list...
+
+           // Process any variables on the set 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]);
-  }
-  delete $4;   // Delete the class list...
-
-  $$ = CurRec;
-  CurRec = 0;
-};
+           $$ = CurRec;
+           CurRec = 0;
+         };
 
 ClassInst : CLASS ObjectBody {
   if (Records.getClass($2->getName())) {
     err() << "Class '" << $2->getName() << "' already defined!\n";
-    abort();
+    exit(1);
   }
   Records.addClass($$ = $2);
 };
 
 DefInst : DEF ObjectBody {
+  $2->resolveReferences();
+
+  // If ObjectBody has template arguments, it's an error.
   if (!$2->getTemplateArgs().empty()) {
     err() << "Def '" << $2->getName()
           << "' is not permitted to have template arguments!\n";
-    abort();
+    exit(1);
   }
-  // If ObjectBody has template arguments, it's an error.
+  // Ensure redefinition doesn't happen.
   if (Records.getDef($2->getName())) {
     err() << "Def '" << $2->getName() << "' already defined!\n";
-    abort();
+    exit(1);
   }
   Records.addDef($$ = $2);
 };
@@ -454,20 +588,22 @@ DefInst : DEF ObjectBody {
 
 Object : ClassInst | DefInst;
 
-// SETCommand - A 'SET' statement start...
-SETCommand : SET ID OptBitList '=' Value IN {
-  SetStack.push_back(std::make_pair(std::make_pair(*$2, $3), $5));
-  delete $2;
+LETItem : ID OptBitList '=' Value {
+  LetStack.back().push_back(LetRecord(*$1, $2, $4));
+  delete $1; delete $2;
 };
 
+LETList : LETItem | LETList ',' LETItem;
+
+// LETCommand - A 'LET' statement start...
+LETCommand : LET { LetStack.push_back(std::vector<LetRecord>()); } LETList IN;
+
 // Support Set commands wrapping objects... both with and without braces.
-Object : SETCommand '{' ObjectList '}' {
-    delete SetStack.back().first.second; // Delete OptBitList
-    SetStack.pop_back();
+Object : LETCommand '{' ObjectList '}' {
+    LetStack.pop_back();
   }
-  | SETCommand Object {
-    delete SetStack.back().first.second; // Delete OptBitList
-    SetStack.pop_back();
+  | LETCommand Object {
+    LetStack.pop_back();
   };
 
 ObjectList : Object {} | ObjectList Object {};
@@ -478,5 +614,5 @@ File : ObjectList {};
 
 int yyerror(const char *ErrorMsg) {
   err() << "Error parsing: " << ErrorMsg << "\n";
-  abort();
+  exit(1);
 }