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

Chris Lattner lattner at cs.uiuc.edu
Sat Oct 4 15:01:01 PDT 2003


Changes in directory llvm/lib/Bytecode/Reader:

ConstantReader.cpp updated: 1.54 -> 1.55
Reader.cpp updated: 1.65 -> 1.66
ReaderInternals.h updated: 1.45 -> 1.46

---
Log message:

Transform two methods to return pointers directly instead of returning them
as 'by reference' arguments.


---
Diffs of the changes:

Index: llvm/lib/Bytecode/Reader/ConstantReader.cpp
diff -u llvm/lib/Bytecode/Reader/ConstantReader.cpp:1.54 llvm/lib/Bytecode/Reader/ConstantReader.cpp:1.55
--- llvm/lib/Bytecode/Reader/ConstantReader.cpp:1.54	Thu Oct  2 15:26:18 2003
+++ llvm/lib/Bytecode/Reader/ConstantReader.cpp	Sat Oct  4 15:00:03 2003
@@ -147,9 +147,9 @@
 }
 
 
-void BytecodeParser::parseConstantValue(const unsigned char *&Buf,
-                                        const unsigned char *EndBuf,
-                                        const Type *Ty, Constant *&V) {
+Constant *BytecodeParser::parseConstantValue(const unsigned char *&Buf,
+                                             const unsigned char *EndBuf,
+                                             const Type *Ty) {
 
   // We must check for a ConstantExpr before switching by type because
   // a ConstantExpr can be of any type, and has no explicit value.
@@ -183,14 +183,13 @@
     // Construct a ConstantExpr of the appropriate kind
     if (isExprNumArgs == 1) {           // All one-operand expressions
       assert(Opcode == Instruction::Cast);
-      V = ConstantExpr::getCast(ArgVec[0], Ty);
+      return ConstantExpr::getCast(ArgVec[0], Ty);
     } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr
       std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end());
-      V = ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
+      return ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
     } else {                            // All other 2-operand expressions
-      V = ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
+      return ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
     }
-    return;
   }
   
   // Ok, not an ConstantExpr.  We now know how to read the given type...
@@ -199,8 +198,7 @@
     unsigned Val;
     if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr;
     if (Val != 0 && Val != 1) throw std::string("Invalid boolean value read.");
-    V = ConstantBool::get(Val == 1);
-    break;
+    return ConstantBool::get(Val == 1);
   }
 
   case Type::UByteTyID:   // Unsigned integer types...
@@ -210,15 +208,13 @@
     if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr;
     if (!ConstantUInt::isValueValidForType(Ty, Val)) 
       throw std::string("Invalid unsigned byte/short/int read.");
-    V = ConstantUInt::get(Ty, Val);
-    break;
+    return ConstantUInt::get(Ty, Val);
   }
 
   case Type::ULongTyID: {
     uint64_t Val;
     if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr;
-    V = ConstantUInt::get(Ty, Val);
-    break;
+    return ConstantUInt::get(Ty, Val);
   }
 
   case Type::SByteTyID:   // Signed integer types...
@@ -229,27 +225,23 @@
     if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr;
     if (!ConstantSInt::isValueValidForType(Ty, Val)) 
       throw std::string("Invalid signed byte/short/int/long read.");
-    V = ConstantSInt::get(Ty, Val);
-    break;
+    return ConstantSInt::get(Ty, Val);
   }
 
   case Type::FloatTyID: {
     float F;
     if (input_data(Buf, EndBuf, &F, &F+1)) throw Error_inputdata;
-    V = ConstantFP::get(Ty, F);
-    break;
+    return ConstantFP::get(Ty, F);
   }
 
   case Type::DoubleTyID: {
     double Val;
     if (input_data(Buf, EndBuf, &Val, &Val+1)) throw Error_inputdata;
-    V = ConstantFP::get(Ty, Val);
-    break;
+    return ConstantFP::get(Ty, Val);
   }
 
   case Type::TypeTyID:
-    assert(0 && "Type constants should be handled separately!!!");
-    abort();
+    throw std::string("Type constants shouldn't live in constant table!");
 
   case Type::ArrayTyID: {
     const ArrayType *AT = cast<ArrayType>(Ty);
@@ -263,8 +255,7 @@
       if (!C) throw std::string("Unable to get const value of array slot.");
       Elements.push_back(C);
     }
-    V = ConstantArray::get(AT, Elements);
-    break;
+    return ConstantArray::get(AT, Elements);
   }
 
   case Type::StructTyID: {
@@ -280,8 +271,7 @@
       Elements.push_back(C);
     }
 
-    V = ConstantStruct::get(ST, Elements);
-    break;
+    return ConstantStruct::get(ST, Elements);
   }    
 
   case Type::PointerTyID: {
@@ -294,8 +284,7 @@
 
     switch (SubClass) {
     case 0:    // ConstantPointerNull value...
-      V = ConstantPointerNull::get(PT);
-      break;
+      return ConstantPointerNull::get(PT);
 
     case 1: {  // ConstantPointerRef value...
       unsigned Slot;
@@ -336,23 +325,18 @@
         }
       }
 
-      V = ConstantPointerRef::get(GV);
-      break;
+      return ConstantPointerRef::get(GV);
     }
     
     default:
       BCR_TRACE(5, "UNKNOWN Pointer Constant Type!\n");
       throw std::string("Unknown pointer constant type.");
     }
-    break;
   }
 
   default:
-    std::cerr << __FILE__ << ":" << __LINE__ 
-              << ": Don't know how to deserialize constant value of type '"
-              << Ty->getName() << "'\n";
     throw std::string("Don't know how to deserialize constant value of type '"+
-                      Ty->getName());
+                      Ty->getDescription());
   }
 }
 
@@ -379,11 +363,10 @@
       parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries);
     } else {
       for (unsigned i = 0; i < NumEntries; ++i) {
-        Constant *C;
-        int Slot;
-        parseConstantValue(Buf, EndBuf, Ty, C);
+        Constant *C = parseConstantValue(Buf, EndBuf, Ty);
         assert(C && "parseConstantValue returned NULL!");
         BCR_TRACE(4, "Read Constant: '" << *C << "'\n");
+        int Slot;
         if ((Slot = insertValue(C, Tab)) == -1)
           throw std::string("Could not insert value into ValueTable.");
 


Index: llvm/lib/Bytecode/Reader/Reader.cpp
diff -u llvm/lib/Bytecode/Reader/Reader.cpp:1.65 llvm/lib/Bytecode/Reader/Reader.cpp:1.66
--- llvm/lib/Bytecode/Reader/Reader.cpp:1.65	Sat Oct  4 14:29:21 2003
+++ llvm/lib/Bytecode/Reader/Reader.cpp	Sat Oct  4 15:00:03 2003
@@ -31,25 +31,21 @@
     throw std::string("Alignment error in buffer: read past end of block.");
 }
 
-void
-BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
-  if (Ty->isPrimitiveType()) {
-    Slot = Ty->getPrimitiveID();
-  } else {
-    // Check the function level types first...
-    TypeValuesListTy::iterator I = find(FunctionTypeValues.begin(),
-                                        FunctionTypeValues.end(), Ty);
-    if (I != FunctionTypeValues.end()) {
-      Slot = FirstDerivedTyID + ModuleTypeValues.size() +
-        (&*I - &FunctionTypeValues[0]);
-    } else {
-      I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
-      if (I == ModuleTypeValues.end())
-        throw std::string("Didn't find type in ModuleTypeValues.");
-      Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
-    }
-  }
-  //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n";
+unsigned BytecodeParser::getTypeSlot(const Type *Ty) {
+  if (Ty->isPrimitiveType())
+    return Ty->getPrimitiveID();
+
+  // Check the function level types first...
+  TypeValuesListTy::iterator I = find(FunctionTypeValues.begin(),
+                                      FunctionTypeValues.end(), Ty);
+  if (I != FunctionTypeValues.end())
+    return FirstDerivedTyID + ModuleTypeValues.size() +
+             (&*I - &FunctionTypeValues[0]);
+
+  I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
+  if (I == ModuleTypeValues.end())
+    throw std::string("Didn't find type in ModuleTypeValues.");
+  return FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
 }
 
 const Type *BytecodeParser::getType(unsigned ID) {
@@ -68,8 +64,7 @@
           Val->getType()->isPrimitiveType() ||
           !cast<Constant>(Val)->isNullValue()) &&
          "Cannot read null values from bytecode!");
-  unsigned type;
-  getTypeSlot(Val->getType(), type);
+  unsigned type = getTypeSlot(Val->getType());
   assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
  
   if (ValueTab.size() <= type) {
@@ -93,19 +88,16 @@
 void BytecodeParser::setValueTo(ValueTable &ValueTab, unsigned Slot,
                                 Value *Val) {
   assert(&ValueTab == &ModuleValues && "Can only setValueTo on Module values!");
-  unsigned type;
-  getTypeSlot(Val->getType(), type);
-
   assert((!HasImplicitZeroInitializer || Slot != 0) &&
          "Cannot change zero init");
+  unsigned type = getTypeSlot(Val->getType());
   assert(type < ValueTab.size() && Slot <= ValueTab[type]->size());
   ValueTab[type]->setOperand(Slot-HasImplicitZeroInitializer, Val);
 }
 
 Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
   unsigned Num = oNum;
-  unsigned type;   // The type plane it lives in...
-  getTypeSlot(Ty, type);
+  unsigned type = getTypeSlot(Ty);    // The type plane it lives in...
 
   if (type == Type::TypeTyID) {   // The 'type' plane has implicit values
     assert(Create == false);


Index: llvm/lib/Bytecode/Reader/ReaderInternals.h
diff -u llvm/lib/Bytecode/Reader/ReaderInternals.h:1.45 llvm/lib/Bytecode/Reader/ReaderInternals.h:1.46
--- llvm/lib/Bytecode/Reader/ReaderInternals.h:1.45	Thu Oct  2 15:26:18 2003
+++ llvm/lib/Bytecode/Reader/ReaderInternals.h	Sat Oct  4 15:00:03 2003
@@ -169,8 +169,9 @@
 
   void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf,
                          ValueTable &Tab, TypeValuesListTy &TypeTab);
-  void parseConstantValue(const unsigned char *&Buf, const unsigned char *End,
-                          const Type *Ty, Constant *&V);
+  Constant *parseConstantValue(const unsigned char *&Buf,
+                               const unsigned char *End,
+                               const Type *Ty);
   void parseTypeConstants(const unsigned char *&Buf,
                           const unsigned char *EndBuf,
                           TypeValuesListTy &Tab, unsigned NumEntries);
@@ -185,7 +186,7 @@
   void setValueTo(ValueTable &D, unsigned Slot, Value *V);
   void postResolveValues(ValueTable &ValTab);
 
-  void getTypeSlot(const Type *Ty, unsigned &Slot);
+  unsigned getTypeSlot(const Type *Ty);
 
   // resolve all references to the placeholder (if any) for the given value
   void ResolveReferencesToValue(Value *Val, unsigned Slot);





More information about the llvm-commits mailing list