[llvm-commits] CVS: llvm/lib/CodeGen/AsmPrinter.cpp MachOWriter.cpp

Reid Spencer reid at x10sys.com
Thu Jan 11 23:06:07 PST 2007



Changes in directory llvm/lib/CodeGen:

AsmPrinter.cpp updated: 1.132 -> 1.133
MachOWriter.cpp updated: 1.11 -> 1.12
---
Log message:

For PR1064: http://llvm.org/PR1064 :
Implement the arbitrary bit-width integer feature. The feature allows
integers of any bitwidth (up to 64) to be defined instead of just 1, 8, 
16, 32, and 64 bit integers.  

This change does several things:
1. Introduces a new Derived Type, IntegerType, to represent the number of
   bits in an integer. The Type classes SubclassData field is used to
   store the number of bits. This allows 2^23 bits in an integer type. 
2. Removes the five integer Type::TypeID values for the 1, 8, 16, 32 and
   64-bit integers. These are replaced with just IntegerType which is not
   a primitive any more. 
3. Adjust the rest of LLVM to account for this change.

Note that while this incremental change lays the foundation for arbitrary
bit-width integers, LLVM has not yet been converted to actually deal with 
them in any significant way. Most optimization passes, for example, will 
still only deal with the byte-width integer types.  Future increments
will rectify this situation.



---
Diffs of the changes:  (+56 -45)

 AsmPrinter.cpp  |   33 ++++++++++++++-------------
 MachOWriter.cpp |   68 ++++++++++++++++++++++++++++++++------------------------
 2 files changed, 56 insertions(+), 45 deletions(-)


Index: llvm/lib/CodeGen/AsmPrinter.cpp
diff -u llvm/lib/CodeGen/AsmPrinter.cpp:1.132 llvm/lib/CodeGen/AsmPrinter.cpp:1.133
--- llvm/lib/CodeGen/AsmPrinter.cpp:1.132	Thu Jan 11 22:24:45 2007
+++ llvm/lib/CodeGen/AsmPrinter.cpp	Fri Jan 12 01:05:13 2007
@@ -459,7 +459,7 @@
       // We can emit the pointer value into this slot if the slot is an
       // integer slot greater or equal to the size of the pointer.
       if (Ty->isIntegral() &&
-          Ty->getPrimitiveSize() >= TD->getTypeSize(Op->getType()))
+          TD->getTypeSize(Ty) >= TD->getTypeSize(Op->getType()))
         return EmitConstantValueOnly(Op);
       
       assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
@@ -917,28 +917,29 @@
 void AsmPrinter::printDataDirective(const Type *type) {
   const TargetData *TD = TM.getTargetData();
   switch (type->getTypeID()) {
-  case Type::Int1TyID:
-  case Type::Int8TyID:
-    O << TAI->getData8bitsDirective();
-    break;
-  case Type::Int16TyID: 
-    O << TAI->getData16bitsDirective();
+  case Type::IntegerTyID: {
+    unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
+    if (BitWidth <= 8)
+      O << TAI->getData8bitsDirective();
+    else if (BitWidth <= 16)
+      O << TAI->getData16bitsDirective();
+    else if (BitWidth <= 32)
+      O << TAI->getData32bitsDirective();
+    else if (BitWidth <= 64) {
+      assert(TAI->getData64bitsDirective() &&
+             "Target cannot handle 64-bit constant exprs!");
+      O << TAI->getData64bitsDirective();
+    }
     break;
+  }
   case Type::PointerTyID:
     if (TD->getPointerSize() == 8) {
       assert(TAI->getData64bitsDirective() &&
              "Target cannot handle 64-bit pointer exprs!");
       O << TAI->getData64bitsDirective();
-      break;
+    } else {
+      O << TAI->getData32bitsDirective();
     }
-    //Fall through for pointer size == int size
-  case Type::Int32TyID: 
-    O << TAI->getData32bitsDirective();
-    break;
-  case Type::Int64TyID:
-    assert(TAI->getData64bitsDirective() &&
-           "Target cannot handle 64-bit constant exprs!");
-    O << TAI->getData64bitsDirective();
     break;
   case Type::FloatTyID: case Type::DoubleTyID:
     assert (0 && "Should have already output floating point constant.");


Index: llvm/lib/CodeGen/MachOWriter.cpp
diff -u llvm/lib/CodeGen/MachOWriter.cpp:1.11 llvm/lib/CodeGen/MachOWriter.cpp:1.12
--- llvm/lib/CodeGen/MachOWriter.cpp:1.11	Thu Jan 11 12:21:29 2007
+++ llvm/lib/CodeGen/MachOWriter.cpp	Fri Jan 12 01:05:13 2007
@@ -708,8 +708,7 @@
     if (isa<UndefValue>(PC)) {
       continue;
     } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(PC)) {
-      unsigned ElementSize = 
-        CP->getType()->getElementType()->getPrimitiveSize();
+      unsigned ElementSize = TD->getTypeSize(CP->getType()->getElementType());
       for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
         WorkList.push_back(CPair(CP->getOperand(i), PA+i*ElementSize));
     } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(PC)) {
@@ -726,27 +725,42 @@
       }
     } else if (PC->getType()->isFirstClassType()) {
       unsigned char *ptr = (unsigned char *)PA;
-      uint64_t val;
-      
       switch (PC->getType()->getTypeID()) {
-      case Type::Int1TyID:
-      case Type::Int8TyID:
-        ptr[0] = cast<ConstantInt>(PC)->getZExtValue();
-        break;
-      case Type::Int16TyID:
-        val = cast<ConstantInt>(PC)->getZExtValue();
-        if (TD->isBigEndian())
-          val = ByteSwap_16(val);
-        ptr[0] = val;
-        ptr[1] = val >> 8;
-        break;
-      case Type::Int32TyID:
-      case Type::FloatTyID:
-        if (PC->getType()->getTypeID() == Type::FloatTyID) {
-          val = FloatToBits(cast<ConstantFP>(PC)->getValue());
+      case Type::IntegerTyID: {
+        unsigned NumBits = cast<IntegerType>(PC->getType())->getBitWidth();
+        uint64_t val = cast<ConstantInt>(PC)->getZExtValue();
+        if (NumBits <= 8)
+          ptr[0] = val;
+        else if (NumBits <= 16) {
+          if (TD->isBigEndian())
+            val = ByteSwap_16(val);
+          ptr[0] = val;
+          ptr[1] = val >> 8;
+        } else if (NumBits <= 32) {
+          if (TD->isBigEndian())
+            val = ByteSwap_32(val);
+          ptr[0] = val;
+          ptr[1] = val >> 8;
+          ptr[2] = val >> 16;
+          ptr[3] = val >> 24;
+        } else if (NumBits <= 64) {
+          if (TD->isBigEndian())
+            val = ByteSwap_64(val);
+          ptr[0] = val;
+          ptr[1] = val >> 8;
+          ptr[2] = val >> 16;
+          ptr[3] = val >> 24;
+          ptr[4] = val >> 32;
+          ptr[5] = val >> 40;
+          ptr[6] = val >> 48;
+          ptr[7] = val >> 56;
         } else {
-          val = cast<ConstantInt>(PC)->getZExtValue();
+          assert(0 && "Not implemented: bit widths > 64");
         }
+        break;
+      }
+      case Type::FloatTyID: {
+        uint64_t val = FloatToBits(cast<ConstantFP>(PC)->getValue());
         if (TD->isBigEndian())
           val = ByteSwap_32(val);
         ptr[0] = val;
@@ -754,13 +768,9 @@
         ptr[2] = val >> 16;
         ptr[3] = val >> 24;
         break;
-      case Type::DoubleTyID:
-      case Type::Int64TyID:
-        if (PC->getType()->getTypeID() == Type::DoubleTyID) {
-          val = DoubleToBits(cast<ConstantFP>(PC)->getValue());
-        } else {
-          val = cast<ConstantInt>(PC)->getZExtValue();
-        }
+      }
+      case Type::DoubleTyID: {
+        uint64_t val = DoubleToBits(cast<ConstantFP>(PC)->getValue());
         if (TD->isBigEndian())
           val = ByteSwap_64(val);
         ptr[0] = val;
@@ -772,6 +782,7 @@
         ptr[6] = val >> 48;
         ptr[7] = val >> 56;
         break;
+      }
       case Type::PointerTyID:
         if (isa<ConstantPointerNull>(C))
           memset(ptr, 0, TD->getPointerSize());
@@ -790,8 +801,7 @@
     } else if (isa<ConstantAggregateZero>(PC)) {
       memset((void*)PA, 0, (size_t)TD->getTypeSize(PC->getType()));
     } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(PC)) {
-      unsigned ElementSize = 
-        CPA->getType()->getElementType()->getPrimitiveSize();
+      unsigned ElementSize = TD->getTypeSize(CPA->getType()->getElementType());
       for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
         WorkList.push_back(CPair(CPA->getOperand(i), PA+i*ElementSize));
     } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(PC)) {






More information about the llvm-commits mailing list