Change approach so that we get codegen for free for intrinsics. With this,
[oota-llvm.git] / utils / TableGen / Record.cpp
index 077476d07bccb366ec44d4b1d6660f69cb08e191..2798afba6ccdc526f591980777cd59ffc3617aa4 100644 (file)
@@ -12,6 +12,8 @@
 
 #include "Record.h"
 #include "llvm/Support/DataTypes.h"
+#include <ios>
+
 using namespace llvm;
 
 //===----------------------------------------------------------------------===//
@@ -67,13 +69,13 @@ Init *BitsRecTy::convertValue(IntInit *II) {
     if (Value & ~((1LL << Size)-1))
       return 0;
   } else {
-    if ((Value >> Size) != -1 || ((Value & (1 << (Size-1))) == 0))
+    if ((Value >> Size) != -1 || ((Value & (1LL << (Size-1))) == 0))
       return 0;
   }
 
   BitsInit *Ret = new BitsInit(Size);
   for (unsigned i = 0; i != Size; ++i)
-    Ret->setBit(i, new BitInit(Value & (1 << i)));
+    Ret->setBit(i, new BitInit(Value & (1LL << i)));
 
   return Ret;
 }
@@ -552,6 +554,17 @@ Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
   return this;
 }
 
+Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
+  std::vector<Init*> NewArgs;
+  for (unsigned i = 0, e = Args.size(); i != e; ++i)
+    NewArgs.push_back(Args[i]->resolveReferences(R, RV));
+  
+  if (Args != NewArgs)
+    return new DagInit(NodeTypeDef, NewArgs, ArgNames);
+    
+  return this;
+}
+
 
 void DagInit::print(std::ostream &OS) const {
   OS << "(" << NodeTypeDef->getName();
@@ -709,6 +722,25 @@ ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
         "' does not have a list initializer!";
 }
 
+/// getValueAsListOfDefs - This method looks up the specified field and returns
+/// its value as a vector of records, throwing an exception if the field does
+/// not exist or if the value is not the right type.
+///
+std::vector<Record*> 
+Record::getValueAsListOfDefs(const std::string &FieldName) const {
+  ListInit *List = getValueAsListInit(FieldName);
+  std::vector<Record*> Defs;
+  for (unsigned i = 0; i < List->getSize(); i++) {
+    if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
+      Defs.push_back(DI->getDef());
+    } else {
+      throw "Record `" + getName() + "', field `" + FieldName +
+            "' list is not entirely DefInit!";
+    }
+  }
+  return Defs;
+}
+
 /// getValueAsInt - This method looks up the specified field and returns its
 /// value as an int, throwing an exception if the field does not exist or if
 /// the value is not the right type.
@@ -722,7 +754,7 @@ int Record::getValueAsInt(const std::string &FieldName) const {
   if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
     return II->getValue();
   throw "Record `" + getName() + "', field `" + FieldName +
-        "' does not have a list initializer!";
+        "' does not have an int initializer!";
 }
 
 /// getValueAsDef - This method looks up the specified field and returns its
@@ -738,7 +770,7 @@ Record *Record::getValueAsDef(const std::string &FieldName) const {
   if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
     return DI->getDef();
   throw "Record `" + getName() + "', field `" + FieldName +
-        "' does not have a list initializer!";
+        "' does not have a def initializer!";
 }
 
 /// getValueAsBit - This method looks up the specified field and returns its
@@ -773,6 +805,18 @@ DagInit *Record::getValueAsDag(const std::string &FieldName) const {
         "' does not have a dag initializer!";
 }
 
+std::string Record::getValueAsCode(const std::string &FieldName) const {
+  const RecordVal *R = getValue(FieldName);
+  if (R == 0 || R->getValue() == 0)
+    throw "Record `" + getName() + "' does not have a field named `" +
+      FieldName + "'!\n";
+  
+  if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
+    return CI->getValue();
+  throw "Record `" + getName() + "', field `" + FieldName +
+    "' does not have a code initializer!";
+}
+
 
 void RecordKeeper::dump() const { std::cerr << *this; }