[llvm-commits] [hlvm] r38055 - in /hlvm/trunk/hlvm: AST/ContainerType.cpp AST/ContainerType.h AST/Type.cpp AST/Type.h Writer/XML/XMLWriter.cpp

Reid Spencer reid at x10sys.com
Sat Jul 7 16:59:22 PDT 2007


Author: reid
Date: Sat Jul  7 18:59:22 2007
New Revision: 38055

URL: http://llvm.org/viewvc/llvm-project?rev=38055&view=rev
Log:
Don't spread knowledge about the primitive types around outside of the AST
library. Provide a getPrimitiveName() virtual method that each type class can
implement to return the corresponding primitive type name. This can then be
used by the XMLWriter to eliminate many lines of code and make it clearer.

Modified:
    hlvm/trunk/hlvm/AST/ContainerType.cpp
    hlvm/trunk/hlvm/AST/ContainerType.h
    hlvm/trunk/hlvm/AST/Type.cpp
    hlvm/trunk/hlvm/AST/Type.h
    hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp

Modified: hlvm/trunk/hlvm/AST/ContainerType.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/AST/ContainerType.cpp?rev=38055&r1=38054&r2=38055&view=diff

==============================================================================
--- hlvm/trunk/hlvm/AST/ContainerType.cpp (original)
+++ hlvm/trunk/hlvm/AST/ContainerType.cpp Sat Jul  7 18:59:22 2007
@@ -55,6 +55,12 @@
   assert(!"That node isn't my child");
 }
 
+const char* 
+ContainerType::getPrimitiveName()
+{
+  return 0;
+}
+
 PointerType::~PointerType()
 {
 }

Modified: hlvm/trunk/hlvm/AST/ContainerType.h
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/AST/ContainerType.h?rev=38055&r1=38054&r2=38055&view=diff

==============================================================================
--- hlvm/trunk/hlvm/AST/ContainerType.h (original)
+++ hlvm/trunk/hlvm/AST/ContainerType.h Sat Jul  7 18:59:22 2007
@@ -59,6 +59,7 @@
     /// @name Accessors
     /// @{
     public:
+      virtual const char* getPrimitiveName();
       // Methods to support type inquiry via is, cast, dyn_cast
       static inline bool classof(const ContainerType*) { return true; }
       static inline bool classof(const Type* T) { return T->isContainerType(); }

Modified: hlvm/trunk/hlvm/AST/Type.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/AST/Type.cpp?rev=38055&r1=38054&r2=38055&view=diff

==============================================================================
--- hlvm/trunk/hlvm/AST/Type.cpp (original)
+++ hlvm/trunk/hlvm/AST/Type.cpp Sat Jul  7 18:59:22 2007
@@ -46,32 +46,155 @@
 {
 }
 
+const char* 
+AnyType::getPrimitiveName()
+{
+  return "any";
+}
+
 BooleanType::~BooleanType()
 {
 }
 
+const char* 
+BooleanType::getPrimitiveName()
+{
+  return "bool";
+}
+
 CharacterType::~CharacterType()
 {
 }
 
+const char* 
+CharacterType::getPrimitiveName()
+{
+  return "char";
+}
+
 IntegerType::~IntegerType()
 {
 }
 
+const char* 
+IntegerType::getPrimitiveName()
+{
+  if (numBits > 128)
+    return 0;
+
+  if (signedness) {
+    if (numBits > 64)
+      return "s128";
+    else if (numBits > 32)
+      return "s64";
+    else if (numBits > 16)
+      return "s32";
+    else if (numBits > 8)
+      return "s16";
+    else
+      return "s8";
+  } else {
+    if (numBits > 64)
+      return "u128";
+    else if (numBits > 32)
+      return "u64";
+    else if (numBits > 16)
+      return "u32";
+    else if (numBits > 8)
+      return "u16";
+    else
+      return "u8";
+  }
+  assert(!"Can't get here");
+}
+
 OctetType::~OctetType()
 {
 }
 
+const char* 
+OctetType::getPrimitiveName()
+{
+  return "octet";
+}
+
 RangeType::~RangeType()
 {
 }
 
+const char* 
+RangeType::getPrimitiveName()
+{
+  if (min < 0) {
+    if (min >= 0 && max <= 255U)
+      return "u8";
+    else if (min >= 0 && max <= 65535U)
+      return "u16";
+    else if (min >= 0 && max <= 4294967295U)
+      return "u32";
+    else if (min >= 0 && max <= 9223372036854775807LL)
+      return "u64";
+    else
+      // FIXME: handle u128 case
+      return 0;
+  } else {
+    if (min >= -127 && max <= 127)
+      return "s8";
+    else if (min >= -32767 && max <= 32767)
+      return "s16";
+    else if (min >= -2147483647 && max <= 2147483647)
+      return "s32";
+    else if (min >= -9223372036854775807LL && max <= 9223372036854775807LL)
+      return "s64";
+    else
+      // FIXME: handle s128 case
+      return 0;
+  }
+  return 0;
+}
+
 RealType::~RealType()
 {
 }
 
+const char* 
+RealType::getPrimitiveName()
+{
+  switch (mantissa) {
+    case 23:
+      if (exponent  == 8)
+        return "f32";
+      break;
+    case 32:
+      if (exponent == 11)
+        return "f43";
+      break;
+    case 52:
+      if (exponent == 11) 
+        return "f64";
+      break;
+    case 64:
+      if (exponent == 15) 
+        return "f80";
+      break;
+    case 112:
+      if (exponent == 15)
+        return "f128";
+      break;
+    default:
+      break;
+  }
+  return 0;
+}
+
 VoidType::~VoidType()
 {
 }
 
+const char* 
+VoidType::getPrimitiveName()
+{
+  return "void";
+}
+
 }}

Modified: hlvm/trunk/hlvm/AST/Type.h
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/AST/Type.h?rev=38055&r1=38054&r2=38055&view=diff

==============================================================================
--- hlvm/trunk/hlvm/AST/Type.h (original)
+++ hlvm/trunk/hlvm/AST/Type.h Sat Jul  7 18:59:22 2007
@@ -55,6 +55,13 @@
     /// @name Accessors
     /// @{
     public:
+      virtual const char* getPrimitiveName() = 0;
+      bool isPrimitive() { return getPrimitiveName() != 0; }
+
+    /// @}
+    /// @name Type Identification
+    /// @{
+    public:
       inline bool isPrimitiveType() const { return id <= LastPrimitiveTypeID; }
       inline bool isIntegralType()  const { 
         return id == IntegerTypeID || id == RangeTypeID; 
@@ -113,6 +120,7 @@
     /// @name Accessors
     /// @{
     public:
+      virtual const char* getPrimitiveName();
       // Get the name for the type
       const std::string&  getName() const { return name; }
       
@@ -154,6 +162,7 @@
     /// @name Accessors
     /// @{
     public:
+      virtual const char* getPrimitiveName();
       // Methods to support type inquiry via is, cast, dyn_cast
       static inline bool classof(const AnyType*) { return true; }
       static inline bool classof(const Type* T) { return T->isAnyType(); }
@@ -174,6 +183,7 @@
     /// @name Accessors
     /// @{
     public:
+      virtual const char* getPrimitiveName();
       // Methods to support type inquiry via is, cast, dyn_cast
       static inline bool classof(const BooleanType*) { return true; }
       static inline bool classof(const Type* T) { return T->isBooleanType(); }
@@ -194,6 +204,7 @@
     /// @name Accessors
     /// @{
     public:
+      virtual const char* getPrimitiveName();
       // Methods to support type inquiry via is, cast, dyn_cast
       static inline bool classof(const CharacterType*) { return true; }
       static inline bool classof(const Type* T) { return T->isCharacterType(); }
@@ -215,6 +226,7 @@
     /// @name Accessors
     /// @{
     public:
+      virtual const char* getPrimitiveName();
       // Methods to support type inquiry via is, cast, dyn_cast
       static inline bool classof(const OctetType*) { return true; }
       static inline bool classof(const Type* T) { return T->isOctetType(); }
@@ -235,6 +247,7 @@
     /// @name Accessors
     /// @{
     public:
+      virtual const char* getPrimitiveName();
       // Methods to support type inquiry via is, cast, dyn_cast
       static inline bool classof(const VoidType*) { return true; }
       static inline bool classof(const Type* T) { return T->isVoidType(); }
@@ -260,6 +273,7 @@
     /// @name Accessors
     /// @{
     public:
+      virtual const char* getPrimitiveName();
 
       /// Return the number of bits
       uint64_t getBits()  const { return numBits; }
@@ -308,6 +322,7 @@
     /// @name Accessors
     /// @{
     public:
+      virtual const char* getPrimitiveName();
       /// Get min value of range
       int64_t getMin() { return min; }
 
@@ -357,6 +372,7 @@
     /// @name Accessors
     /// @{
     public:
+      virtual const char* getPrimitiveName();
       /// Get the mantissa bits
       uint32_t getMantissa() { return mantissa; }
 

Modified: hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp?rev=38055&r1=38054&r2=38055&view=diff

==============================================================================
--- hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp (original)
+++ hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp Sat Jul  7 18:59:22 2007
@@ -159,43 +159,19 @@
 {
   startElement("atom");
   writeAttribute("name",t->getName().c_str());
-  if (t->getBits() <= 128) {
-    if (t->isSigned()) {
-      switch (t->getBits()) {
-        case 128: startElement("intrinsic"); writeAttribute("is","s128"); break;
-        case 64:  startElement("intrinsic"); writeAttribute("is","s64"); break;
-        case 32:  startElement("intrinsic"); writeAttribute("is","s32"); break;
-        case 16:  startElement("intrinsic"); writeAttribute("is","s16"); break;
-        case 8 :  startElement("intrinsic"); writeAttribute("is","s8"); break;
-        default:
-          startElement("signed");
-          writeAttribute("bits", llvm::utostr(t->getBits()));
-          break;
-      }
-    } else {
-      switch (t->getBits()) {
-        case 128: startElement("intrinsic"); writeAttribute("is","u128"); break;
-        case 64:  startElement("intrinsic"); writeAttribute("is","u64"); break;
-        case 32:  startElement("intrinsic"); writeAttribute("is","u32"); break;
-        case 16:  startElement("intrinsic"); writeAttribute("is","u16"); break;
-        case 8 :  startElement("intrinsic"); writeAttribute("is","u8"); break;
-        default:
-          startElement("unsigned");
-          writeAttribute("bits", llvm::utostr(t->getBits()));
-          break;
-      }
-    }
-    endElement();
+  const char* primName = t->getPrimitiveName();
+  if (primName) {
+    startElement("intrinsic");
+    writeAttribute("is",primName);
+  } else if (t->isSigned()) {
+    startElement("signed");
+    writeAttribute("bits", llvm::utostr(t->getBits()));
   } else {
-    if (t->isSigned()) {
-      startElement("signed");
-    } else {
-      startElement("unsigned");
-    }
+    startElement("unsigned");
     writeAttribute("bits", llvm::utostr(t->getBits()));
-    endElement();
   }
   endElement();
+  endElement();
 }
 
 void
@@ -208,48 +184,10 @@
 {
   startElement("atom");
   writeAttribute("name",t->getName().c_str());
-  bool done = false;
-  switch (t->getMantissa()) {
-    case 23:
-      if (t->getExponent() == 8) {
-        startElement("intrinsic"); 
-        writeAttribute("is","f32"); 
-        done = true; 
-      }
-      break;
-    case 32:
-      if (t->getExponent() == 11) {
-        startElement("intrinsic"); 
-        writeAttribute("is","f43"); 
-        done = true; 
-      }
-      break;
-    case 52:
-      if (t->getExponent() == 11) {
-        startElement("intrinsic"); 
-        writeAttribute("is","f64"); 
-        done = true; 
-      }
-      break;
-    case 64:
-      if (t->getExponent() == 15) {
-        startElement("intrinsic"); 
-        writeAttribute("is","f80"); 
-        done = true; 
-      }
-      break;
-    case 112:
-      if (t->getExponent() == 15) {
-        startElement("intrinsic"); 
-        writeAttribute("is","f128"); 
-        done = true; 
-      }
-      break;
-    default:
-      break;
-  }
-
-  if (done) {
+  const char* primName = t->getPrimitiveName();
+  if (primName) {
+    startElement("intrinsic");
+    writeAttribute("is",primName);
     endElement();
   } else {
     startElement("real");





More information about the llvm-commits mailing list