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

Chris Lattner lattner at cs.uiuc.edu
Thu Oct 9 13:37:01 PDT 2003


Changes in directory llvm/lib/Bytecode/Reader:

ConstantReader.cpp updated: 1.55 -> 1.56
InstructionReader.cpp updated: 1.55 -> 1.56
Reader.cpp updated: 1.73 -> 1.74

---
Log message:

Another cleanup: BytecodeReader::getType(...) used to return a null pointer
on error.  This was only checked about half the time.  Now we convert it to
throw an exception, and delete the half that checked for error.


---
Diffs of the changes:  (+24 -50)

Index: llvm/lib/Bytecode/Reader/ConstantReader.cpp
diff -u llvm/lib/Bytecode/Reader/ConstantReader.cpp:1.55 llvm/lib/Bytecode/Reader/ConstantReader.cpp:1.56
--- llvm/lib/Bytecode/Reader/ConstantReader.cpp:1.55	Sat Oct  4 15:00:03 2003
+++ llvm/lib/Bytecode/Reader/ConstantReader.cpp	Thu Oct  9 13:36:26 2003
@@ -27,7 +27,6 @@
     unsigned Typ;
     if (read_vbr(Buf, EndBuf, Typ)) return Val;
     const Type *RetType = getType(Typ);
-    if (RetType == 0) return Val;
 
     unsigned NumParams;
     if (read_vbr(Buf, EndBuf, NumParams)) return Val;
@@ -35,9 +34,7 @@
     std::vector<const Type*> Params;
     while (NumParams--) {
       if (read_vbr(Buf, EndBuf, Typ)) return Val;
-      const Type *Ty = getType(Typ);
-      if (Ty == 0) return Val;
-      Params.push_back(Ty);
+      Params.push_back(getType(Typ));
     }
 
     bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
@@ -49,7 +46,6 @@
     unsigned ElTyp;
     if (read_vbr(Buf, EndBuf, ElTyp)) return Val;
     const Type *ElementType = getType(ElTyp);
-    if (ElementType == 0) return Val;
 
     unsigned NumElements;
     if (read_vbr(Buf, EndBuf, NumElements)) return Val;
@@ -64,10 +60,7 @@
 
     if (read_vbr(Buf, EndBuf, Typ)) return Val;
     while (Typ) {         // List is terminated by void/0 typeid
-      const Type *Ty = getType(Typ);
-      if (Ty == 0) return Val;
-      Elements.push_back(Ty);
-      
+      Elements.push_back(getType(Typ));
       if (read_vbr(Buf, EndBuf, Typ)) return Val;
     }
 
@@ -77,9 +70,7 @@
     unsigned ElTyp;
     if (read_vbr(Buf, EndBuf, ElTyp)) return Val;
     BCR_TRACE(5, "Pointer Type Constant #" << ElTyp << "\n");
-    const Type *ElementType = getType(ElTyp);
-    if (ElementType == 0) return Val;
-    return PointerType::get(ElementType);
+    return PointerType::get(getType(ElTyp));
   }
 
   case Type::OpaqueTyID: {
@@ -169,9 +160,8 @@
       if (read_vbr(Buf, EndBuf, ArgValSlot)) throw Error_readvbr;
       if (read_vbr(Buf, EndBuf, ArgTypeSlot)) throw Error_readvbr;
       const Type *ArgTy = getType(ArgTypeSlot);
-      if (ArgTy == 0) throw std::string("Argument type slot not found.");
       
-      BCR_TRACE(4, "CE Arg " << i << ": Type: '" << ArgTy << "'  slot: "
+      BCR_TRACE(4, "CE Arg " << i << ": Type: '" << *ArgTy << "'  slot: "
                 << ArgValSlot << "\n");
       
       // Get the arg value from its slot if it exists, otherwise a placeholder
@@ -355,13 +345,13 @@
 
     if (read_vbr(Buf, EndBuf, NumEntries) ||
         read_vbr(Buf, EndBuf, Typ)) throw Error_readvbr;
-    const Type *Ty = getType(Typ);
-    if (Ty == 0) throw std::string("Invalid type read.");
-    BCR_TRACE(3, "Type: '" << Ty << "'  NumEntries: " << NumEntries << "\n");
-
     if (Typ == Type::TypeTyID) {
+      BCR_TRACE(3, "Type: 'type'  NumEntries: " << NumEntries << "\n");
       parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries);
     } else {
+      const Type *Ty = getType(Typ);
+      BCR_TRACE(3, "Type: '" << *Ty << "'  NumEntries: " << NumEntries << "\n");
+
       for (unsigned i = 0; i < NumEntries; ++i) {
         Constant *C = parseConstantValue(Buf, EndBuf, Ty);
         assert(C && "parseConstantValue returned NULL!");


Index: llvm/lib/Bytecode/Reader/InstructionReader.cpp
diff -u llvm/lib/Bytecode/Reader/InstructionReader.cpp:1.55 llvm/lib/Bytecode/Reader/InstructionReader.cpp:1.56
--- llvm/lib/Bytecode/Reader/InstructionReader.cpp:1.55	Thu Oct  9 13:25:19 2003
+++ llvm/lib/Bytecode/Reader/InstructionReader.cpp	Thu Oct  9 13:36:26 2003
@@ -76,8 +76,6 @@
     if (read_vbr(Buf, EndBuf, Typ))
       throw std::string("Error reading from buffer.");
     Result->Ty = getType(Typ);
-    if (Result->Ty == 0) 
-      throw std::string("Invalid type read in instruction.");
     if (read_vbr(Buf, EndBuf, Result->NumOperands))
       throw std::string("Error reading from buffer.");
 
@@ -138,15 +136,9 @@
 
   switch (Raw->Opcode) {
   case Instruction::VarArg:
-  case Instruction::Cast: {
-    Value *V = getValue(Raw->Ty, Raw->Arg1);
-    const Type *Ty = getType(Raw->Arg2);
-    if (Ty == 0) throw std::string("Invalid cast!\n");
-    if (Raw->Opcode == Instruction::Cast)
-      return new CastInst(V, Ty);
-    else
-      return new VarArgInst(V, Ty);
-  }
+    return new VarArgInst(getValue(Raw->Ty, Raw->Arg1), getType(Raw->Arg2));
+  case Instruction::Cast:
+    return new CastInst(getValue(Raw->Ty, Raw->Arg1), getType(Raw->Arg2));
   case Instruction::PHINode: {
     PHINode *PN = new PHINode(Raw->Ty);
     switch (Raw->NumOperands) {
@@ -255,11 +247,8 @@
 
 	if ((args.size() & 1) != 0)  // Must be pairs of type/value
           throw std::string("Invalid call instruction!");
-	for (unsigned i = 0; i < args.size(); i+=2) {
-	  const Type *Ty = getType(args[i]);
-	  if (Ty == 0) throw std::string("Invalid call instruction!");
-	  Params.push_back(getValue(Ty, args[i+1]));
-	}
+	for (unsigned i = 0; i < args.size(); i += 2)
+	  Params.push_back(getValue(getType(args[i]), args[i+1]));
 	delete Raw->VarArgs;
       }
     }


Index: llvm/lib/Bytecode/Reader/Reader.cpp
diff -u llvm/lib/Bytecode/Reader/Reader.cpp:1.73 llvm/lib/Bytecode/Reader/Reader.cpp:1.74
--- llvm/lib/Bytecode/Reader/Reader.cpp:1.73	Thu Oct  9 13:25:19 2003
+++ llvm/lib/Bytecode/Reader/Reader.cpp	Thu Oct  9 13:36:26 2003
@@ -49,17 +49,15 @@
 }
 
 const Type *BytecodeParser::getType(unsigned ID) {
-  if (ID < Type::NumPrimitiveIDs) {
-    const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
-    if (T) return T;
-  }
+  if (ID < Type::NumPrimitiveIDs)
+    if (const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID))
+      return T;
   
   //cerr << "Looking up Type ID: " << ID << "\n";
 
-  if (ID < Type::NumPrimitiveIDs) {
-    const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
-    if (T) return T;   // Asked for a primitive type...
-  }
+  if (ID < Type::NumPrimitiveIDs)
+    if (const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID))
+      return T;   // Asked for a primitive type...
 
   // Otherwise, derived types need offset...
   ID -= FirstDerivedTyID;
@@ -73,7 +71,7 @@
   if (ID < FunctionTypeValues.size())
     return FunctionTypeValues[ID].get();
 
-  return 0;
+  throw std::string("Illegal type reference!");
 }
 
 int BytecodeParser::insertValue(Value *Val, ValueTable &ValueTab) {
@@ -221,9 +219,7 @@
     if (read_vbr(Buf, EndBuf, NumEntries) ||
         read_vbr(Buf, EndBuf, Typ)) throw Error_readvbr;
     const Type *Ty = getType(Typ);
-    if (Ty == 0) throw std::string("Invalid type read in symbol table.");
-
-    BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
+    BCR_TRACE(3, "Plane Type: '" << *Ty << "' with " << NumEntries <<
                  " entries\n");
 
     for (unsigned i = 0; i < NumEntries; ++i) {
@@ -435,7 +431,7 @@
     }
 
     const Type *Ty = getType(SlotNo);
-    if (!Ty || !isa<PointerType>(Ty))
+    if (!isa<PointerType>(Ty))
       throw std::string("Global not pointer type!  Ty = " + 
                         Ty->getDescription());
 
@@ -462,11 +458,10 @@
   if (read_vbr(Buf, End, FnSignature)) throw Error_readvbr;
   while (FnSignature != Type::VoidTyID) { // List is terminated by Void
     const Type *Ty = getType(FnSignature);
-    if (!Ty || !isa<PointerType>(Ty) ||
-        !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) { 
+    if (!isa<PointerType>(Ty) ||
+        !isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
       throw std::string("Function not ptr to func type!  Ty = " +
                         Ty->getDescription());
-    }
 
     // We create functions by passing the underlying FunctionType to create...
     Ty = cast<PointerType>(Ty)->getElementType();





More information about the llvm-commits mailing list