[llvm-commits] [llvm] r74444 - in /llvm/trunk: docs/TableGenFundamentals.html utils/TableGen/Record.cpp utils/TableGen/Record.h

David Greene greened at obbligato.org
Mon Jun 29 13:05:39 PDT 2009


Author: greened
Date: Mon Jun 29 15:05:29 2009
New Revision: 74444

URL: http://llvm.org/viewvc/llvm-project?rev=74444&view=rev
Log:

Implement !cast<string>.

Modified:
    llvm/trunk/docs/TableGenFundamentals.html
    llvm/trunk/utils/TableGen/Record.cpp
    llvm/trunk/utils/TableGen/Record.h

Modified: llvm/trunk/docs/TableGenFundamentals.html
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/TableGenFundamentals.html?rev=74444&r1=74443&r2=74444&view=diff

==============================================================================
--- llvm/trunk/docs/TableGenFundamentals.html (original)
+++ llvm/trunk/docs/TableGenFundamentals.html Mon Jun 29 15:05:29 2009
@@ -411,7 +411,8 @@
 <dt><tt>!cast<type>(a)</tt></dt>
   <dd>A symbol of type <em>type</em> obtained by looking up the string 'a' in
 the symbol table.  If the type of 'a' does not match <em>type</em>, TableGen
-aborts with an error. </dd>
+aborts with an error. !cast<string> is a special case in that the argument must
+be an object defined by a 'def' construct.</dd>
 <dt><tt>!nameconcat<type>(a, b)</tt></dt>
   <dd>Shorthand for !cast<type>(!strconcat(a, b))</dd>
 <dt><tt>!subst(a, b, c)</tt></dt>

Modified: llvm/trunk/utils/TableGen/Record.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/Record.cpp?rev=74444&r1=74443&r2=74444&view=diff

==============================================================================
--- llvm/trunk/utils/TableGen/Record.cpp (original)
+++ llvm/trunk/utils/TableGen/Record.cpp Mon Jun 29 15:05:29 2009
@@ -537,52 +537,80 @@
   switch (getOpcode()) {
   default: assert(0 && "Unknown unop");
   case CAST: {
-    StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
-    if (LHSs) {
-      std::string Name = LHSs->getValue();
-
-      // From TGParser::ParseIDValue
-      if (CurRec) {
-        if (const RecordVal *RV = CurRec->getValue(Name)) {
-          if (RV->getType() != getType()) {
-            throw "type mismatch in nameconcat";
+    if (getType()->getAsString() == "string") {
+      StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
+      if (LHSs) {
+        return LHSs;
+      }
+
+      DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
+      if (LHSd) {
+        return new StringInit(LHSd->getDef()->getName());
+      }
+
+//       VarInit *LHSv = dynamic_cast<VarInit*>(LHS);
+//       if (LHSv) {
+//         // If this is not a template arg, cast it
+//         if (!CurRec->isTemplateArg(LHSv->getName())
+//             && !CurMultiClass) {
+//           return new StringInit(LHSv->getName());
+//         }
+//         break;
+//       }
+
+//       OpInit *LHSo = dynamic_cast<OpInit*>(LHS);
+//       if (!LHSo) {
+//         return new StringInit(LHS->getAsString());
+//       }
+    }
+    else {
+      StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
+      if (LHSs) {
+        std::string Name = LHSs->getValue();
+
+        // From TGParser::ParseIDValue
+        if (CurRec) {
+          if (const RecordVal *RV = CurRec->getValue(Name)) {
+            if (RV->getType() != getType()) {
+              throw "type mismatch in nameconcat";
+            }
+            return new VarInit(Name, RV->getType());
           }
-          return new VarInit(Name, RV->getType());
-        }
-        
-        std::string TemplateArgName = CurRec->getName()+":"+Name;
-        if (CurRec->isTemplateArg(TemplateArgName)) {
-          const RecordVal *RV = CurRec->getValue(TemplateArgName);
-          assert(RV && "Template arg doesn't exist??");
 
-          if (RV->getType() != getType()) {
-            throw "type mismatch in nameconcat";
-          }
+          std::string TemplateArgName = CurRec->getName()+":"+Name;
+          if (CurRec->isTemplateArg(TemplateArgName)) {
+            const RecordVal *RV = CurRec->getValue(TemplateArgName);
+            assert(RV && "Template arg doesn't exist??");
+
+            if (RV->getType() != getType()) {
+              throw "type mismatch in nameconcat";
+            }
 
-          return new VarInit(TemplateArgName, RV->getType());
+            return new VarInit(TemplateArgName, RV->getType());
+          }
         }
-      }
-
-      if (CurMultiClass) {
-        std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
-        if (CurMultiClass->Rec.isTemplateArg(MCName)) {
-          const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
-          assert(RV && "Template arg doesn't exist??");
 
-          if (RV->getType() != getType()) {
-            throw "type mismatch in nameconcat";
+        if (CurMultiClass) {
+          std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
+          if (CurMultiClass->Rec.isTemplateArg(MCName)) {
+            const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
+            assert(RV && "Template arg doesn't exist??");
+            
+            if (RV->getType() != getType()) {
+              throw "type mismatch in nameconcat";
+            }
+            
+            return new VarInit(MCName, RV->getType());
           }
-          
-          return new VarInit(MCName, RV->getType());
         }
-      }
-
-      if (Record *D = Records.getDef(Name))
-        return new DefInit(D);
+        
+        if (Record *D = Records.getDef(Name))
+          return new DefInit(D);
 
-      cerr << "Variable not defined: '" + Name + "'\n";
-      assert(0 && "Variable not found");
-      return 0;
+        cerr << "Variable not defined: '" + Name + "'\n";
+        assert(0 && "Variable not found");
+        return 0;
+      }
     }
     break;
   }
@@ -654,6 +682,23 @@
   return Result + "(" + LHS->getAsString() + ")";
 }
 
+RecTy *UnOpInit::getFieldType(const std::string &FieldName) const {
+  switch (getOpcode()) {
+  default: assert(0 && "Unknown unop");
+  case CAST: {
+    RecordRecTy *RecordType = dynamic_cast<RecordRecTy *>(getType());
+    if (RecordType) {
+      RecordVal *Field = RecordType->getRecord()->getValue(FieldName);
+      if (Field) {
+        return Field->getType();
+      }
+    }
+    break;
+  }
+  }
+  return 0;
+}
+
 Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
   switch (getOpcode()) {
   default: assert(0 && "Unknown binop");

Modified: llvm/trunk/utils/TableGen/Record.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/Record.h?rev=74444&r1=74443&r2=74444&view=diff

==============================================================================
--- llvm/trunk/utils/TableGen/Record.h (original)
+++ llvm/trunk/utils/TableGen/Record.h Mon Jun 29 15:05:29 2009
@@ -834,6 +834,12 @@
 
   virtual Init *resolveReferences(Record &R, const RecordVal *RV);
   
+  /// getFieldType - This method is used to implement the FieldInit class.
+  /// Implementors of this method should return the type of the named field if
+  /// they are of record type.
+  ///
+  virtual RecTy *getFieldType(const std::string &FieldName) const;
+
   virtual std::string getAsString() const;
 };
 





More information about the llvm-commits mailing list