Add support for automatically created anonymous definitions.
[oota-llvm.git] / utils / TableGen / FileParser.y
index 4731674e5217376635992fbe07dd99dba3fdf38e..319b0e3695bac21197deb63975afb45664667cba 100644 (file)
@@ -13,7 +13,7 @@
 
 %{
 #include "Record.h"
-#include "Support/StringExtras.h"
+#include "llvm/ADT/StringExtras.h"
 #include <algorithm>
 #include <cstdio>
 #define YYERROR_VERBOSE 1
@@ -25,6 +25,7 @@ namespace llvm {
 
 extern int Filelineno;
 static Record *CurRec = 0;
+static bool ParsingTemplateArgs = false;
 
 typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
 
@@ -133,6 +134,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<Init*> &TemplateArgs) {
   // Add all of the values in the subclass into the current class...
   const std::vector<RecordVal> &Vals = SC->getValues();
@@ -147,16 +150,24 @@ static void addSubClass(Record *SC, const std::vector<Init*> &TemplateArgs) {
     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.
+    // 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";
-       exit(1);
+        err() << "ERROR: Value not specified for template argument #"
+              << i << " (" << TArgs[i] << ") of subclass '" << SC->getName()
+              << "'!\n";
+        exit(1);
       }
     }
   }
@@ -189,6 +200,7 @@ using namespace llvm;
 };
 
 %token INT BIT STRING BITS LIST CODE DAG CLASS DEF FIELD LET IN
+%token SHLTOK SRATOK SRLTOK
 %token <IntVal>      INTVAL
 %token <StrVal>      ID VARNAME STRVAL CODEFRAGMENT
 
@@ -256,9 +268,9 @@ 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);
     }
@@ -267,6 +279,10 @@ Value : INTVAL {
   } | 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 (Record *D = Records.getDef(*$1)) {
       $$ = new DefInit(D);
     } else {
@@ -275,6 +291,34 @@ Value : INTVAL {
     }
     
     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) {
@@ -300,6 +344,32 @@ Value : INTVAL {
     }
     $$ = 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);
+    }
   };
 
 OptVarName : /* empty */ {
@@ -330,41 +400,61 @@ 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";
+    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";
+    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";
+    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()); };
@@ -387,9 +477,13 @@ 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 ';' {
@@ -445,36 +539,26 @@ 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;
            }
+           delete $4;   // Delete the class list...
 
-          // Process any variables on the set stack...
-          for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
+           // 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)
-      if (!CurRec->isTemplateArg(SuperClass->getTemplateArgs()[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())) {
@@ -485,12 +569,15 @@ ClassInst : CLASS ObjectBody {
 };
 
 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";
     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";
     exit(1);