[llvm-commits] CVS: llvm/lib/Bytecode/Reader/Reader.cpp Reader.h

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



Changes in directory llvm/lib/Bytecode/Reader:

Reader.cpp updated: 1.219 -> 1.220
Reader.h updated: 1.41 -> 1.42
---
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:  (+55 -39)

 Reader.cpp |   80 +++++++++++++++++++++++++++++++------------------------------
 Reader.h   |   14 ++++++++++
 2 files changed, 55 insertions(+), 39 deletions(-)


Index: llvm/lib/Bytecode/Reader/Reader.cpp
diff -u llvm/lib/Bytecode/Reader/Reader.cpp:1.219 llvm/lib/Bytecode/Reader/Reader.cpp:1.220
--- llvm/lib/Bytecode/Reader/Reader.cpp:1.219	Thu Jan 11 22:24:45 2007
+++ llvm/lib/Bytecode/Reader/Reader.cpp	Fri Jan 12 01:05:13 2007
@@ -189,7 +189,7 @@
 /// Obtain a type given a typeid and account for things like compaction tables,
 /// function level vs module level, and the offsetting for the primitive types.
 const Type *BytecodeReader::getType(unsigned ID) {
-  if (ID < Type::FirstDerivedTyID)
+  if (ID <= Type::LastPrimitiveTyID)
     if (const Type *T = Type::getPrimitiveType((Type::TypeID)ID))
       return T;   // Asked for a primitive type...
 
@@ -573,7 +573,7 @@
       if (Oprnds.size() != 2)
         error("Invalid extractelement instruction!");
       Value *V1 = getValue(iType, Oprnds[0]);
-      Value *V2 = getValue(Type::Int32TyID, Oprnds[1]);
+      Value *V2 = getValue(Int32TySlot, Oprnds[1]);
       
       if (!ExtractElementInst::isValidOperands(V1, V2))
         error("Invalid extractelement instruction!");
@@ -588,7 +588,7 @@
       
       Value *V1 = getValue(iType, Oprnds[0]);
       Value *V2 = getValue(getTypeSlot(PackedTy->getElementType()),Oprnds[1]);
-      Value *V3 = getValue(Type::Int32TyID, Oprnds[2]);
+      Value *V3 = getValue(Int32TySlot, Oprnds[2]);
         
       if (!InsertElementInst::isValidOperands(V1, V2, V3))
         error("Invalid insertelement instruction!");
@@ -684,7 +684,7 @@
     case Instruction::Select:
       if (Oprnds.size() != 3)
         error("Invalid Select instruction!");
-      Result = new SelectInst(getValue(Type::Int1TyID, Oprnds[0]),
+      Result = new SelectInst(getValue(BoolTySlot, Oprnds[0]),
                               getValue(iType, Oprnds[1]),
                               getValue(iType, Oprnds[2]));
       break;
@@ -714,7 +714,7 @@
     case Instruction::AShr:
       Result = new ShiftInst(Instruction::OtherOps(Opcode),
                              getValue(iType, Oprnds[0]),
-                             getValue(Type::Int8TyID, Oprnds[1]));
+                             getValue(Int8TySlot, Oprnds[1]));
       break;
     case Instruction::Ret:
       if (Oprnds.size() == 0)
@@ -730,7 +730,7 @@
         Result = new BranchInst(getBasicBlock(Oprnds[0]));
       else if (Oprnds.size() == 3)
         Result = new BranchInst(getBasicBlock(Oprnds[0]),
-            getBasicBlock(Oprnds[1]), getValue(Type::Int1TyID , Oprnds[2]));
+            getBasicBlock(Oprnds[1]), getValue(BoolTySlot, Oprnds[2]));
       else
         error("Invalid number of operands for a 'br' instruction!");
       break;
@@ -877,7 +877,7 @@
         error("Invalid malloc instruction!");
 
       Result = new MallocInst(cast<PointerType>(InstTy)->getElementType(),
-                              getValue(Type::Int32TyID, Oprnds[0]), Align);
+                              getValue(Int32TySlot, Oprnds[0]), Align);
       break;
     }
     case Instruction::Alloca: {
@@ -890,7 +890,7 @@
         error("Invalid alloca instruction!");
 
       Result = new AllocaInst(cast<PointerType>(InstTy)->getElementType(),
-                              getValue(Type::Int32TyID, Oprnds[0]), Align);
+                              getValue(Int32TySlot, Oprnds[0]), Align);
       break;
     }
     case Instruction::Free:
@@ -916,12 +916,12 @@
         // any of the 32 or 64-bit integer types.  The actual choice of 
         // type is encoded in the low bit of the slot number.
         if (isa<StructType>(TopTy))
-          IdxTy = Type::Int32TyID;
+          IdxTy = Int32TySlot;
         else {
           switch (ValIdx & 1) {
           default:
-          case 0: IdxTy = Type::Int32TyID; break;
-          case 1: IdxTy = Type::Int64TyID; break;
+          case 0: IdxTy = Int32TySlot; break;
+          case 1: IdxTy = Int64TySlot; break;
           }
           ValIdx >>= 1;
         }
@@ -1064,7 +1064,7 @@
       unsigned slot = read_vbr_uint();
       std::string Name = read_str();
       Value *V = 0;
-      if (Typ == Type::LabelTyID) {
+      if (Typ == LabelTySlot) {
         if (slot < BBMap.size())
           V = BBMap[slot];
       } else {
@@ -1160,6 +1160,11 @@
     return Result;
 
   switch (PrimType) {
+  case Type::IntegerTyID: {
+    unsigned NumBits = read_vbr_uint();
+    Result = IntegerType::get(NumBits);
+    break;
+  }
   case Type::FunctionTyID: {
     const Type *RetType = readType();
     unsigned RetAttr = read_vbr_uint();
@@ -1204,7 +1209,7 @@
     Result = StructType::get(Elements, false);
     break;
   }
-  case Type::BC_ONLY_PackedStructTyID: {
+  case Type::PackedStructTyID: {
     std::vector<const Type*> Elements;
     unsigned Typ = read_vbr_uint();
     while (Typ) {         // List is terminated by void/0 typeid
@@ -1399,32 +1404,29 @@
   const Type *Ty = getType(TypeID);
   Constant *Result = 0;
   switch (Ty->getTypeID()) {
-  case Type::Int1TyID: {
-    unsigned Val = read_vbr_uint();
-    if (Val != 0 && Val != 1)
-      error("Invalid boolean value read.");
-    Result = ConstantInt::get(Type::Int1Ty, Val == 1);
-    if (Handler) Handler->handleConstantValue(Result);
-    break;
-  }
-
-  case Type::Int8TyID:   // Unsigned integer types...
-  case Type::Int16TyID:
-  case Type::Int32TyID: {
-    unsigned Val = read_vbr_uint();
-    if (!ConstantInt::isValueValidForType(Ty, uint64_t(Val)))
-      error("Invalid unsigned byte/short/int read.");
-    Result = ConstantInt::get(Ty, Val);
-    if (Handler) Handler->handleConstantValue(Result);
-    break;
-  }
-
-  case Type::Int64TyID: {
-    uint64_t Val = read_vbr_uint64();
-    if (!ConstantInt::isValueValidForType(Ty, Val))
-      error("Invalid constant integer read.");
-    Result = ConstantInt::get(Ty, Val);
-    if (Handler) Handler->handleConstantValue(Result);
+  case Type::IntegerTyID: {
+    const IntegerType *IT = cast<IntegerType>(Ty);
+    if (IT->getBitWidth() <= 32) {
+      uint32_t Val = read_vbr_uint();
+      if (IT->getBitWidth() == 1) {
+        if (Val != 0 && Val != 1)
+          error("Invalid boolean value read.");
+        Result = ConstantInt::get(Type::Int1Ty, Val == 1);
+        if (Handler) Handler->handleConstantValue(Result);
+      } else {
+        if (!ConstantInt::isValueValidForType(Ty, uint64_t(Val)))
+          error("Integer value read is invalid for type.");
+        Result = ConstantInt::get(IT, Val);
+        if (Handler) Handler->handleConstantValue(Result);
+      }
+    } else if (IT->getBitWidth() <= 64) {
+      uint64_t Val = read_vbr_uint64();
+      if (!ConstantInt::isValueValidForType(Ty, Val))
+        error("Invalid constant integer read.");
+      Result = ConstantInt::get(IT, Val);
+      if (Handler) Handler->handleConstantValue(Result);
+    } else 
+      assert("Integer types > 64 bits not supported");
     break;
   }
   case Type::FloatTyID: {


Index: llvm/lib/Bytecode/Reader/Reader.h
diff -u llvm/lib/Bytecode/Reader/Reader.h:1.41 llvm/lib/Bytecode/Reader/Reader.h:1.42
--- llvm/lib/Bytecode/Reader/Reader.h:1.41	Sat Jan  6 01:24:43 2007
+++ llvm/lib/Bytecode/Reader/Reader.h	Fri Jan 12 01:05:13 2007
@@ -415,6 +415,20 @@
   BytecodeReader(const BytecodeReader &);  // DO NOT IMPLEMENT
   void operator=(const BytecodeReader &);  // DO NOT IMPLEMENT
 
+  // This enum provides the values of the well-known type slots that are always
+  // emitted as the first few types in the table by the BytecodeWriter class.
+  enum WellKnownTypeSlots {
+    VoidTypeSlot = 0, ///< TypeID == VoidTyID
+    FloatTySlot  = 1, ///< TypeID == FloatTyID
+    DoubleTySlot = 2, ///< TypeID == DoubleTyID
+    LabelTySlot  = 3, ///< TypeID == LabelTyID
+    BoolTySlot   = 4, ///< TypeID == IntegerTyID, width = 1
+    Int8TySlot   = 5, ///< TypeID == IntegerTyID, width = 8
+    Int16TySlot  = 6, ///< TypeID == IntegerTyID, width = 16
+    Int32TySlot  = 7, ///< TypeID == IntegerTyID, width = 32
+    Int64TySlot  = 8  ///< TypeID == IntegerTyID, width = 64
+  };
+
 /// @}
 /// @name Reader Primitives
 /// @{






More information about the llvm-commits mailing list