For PR1195:
[oota-llvm.git] / lib / Bytecode / Reader / Reader.cpp
index 1ba79915e72e42470adb90a290d02fd25c7184fd..200f0d7a2fe3fa1080ea0eec02b792e71510484a 100644 (file)
@@ -132,6 +132,17 @@ inline std::string BytecodeReader::read_str() {
   return std::string((char*)OldAt, Size);
 }
 
+void BytecodeReader::read_str(SmallVectorImpl<char> &StrData) {
+  StrData.clear();
+  unsigned Size = read_vbr_uint();
+  const unsigned char *OldAt = At;
+  At += Size;
+  if (At > BlockEnd)             // Size invalid?
+    error("Ran out of data reading a string!");
+  StrData.append(OldAt, At);
+}
+
+
 /// Read an arbitrary block of data
 inline void BytecodeReader::read_data(void *Ptr, void *End) {
   unsigned char *Start = (unsigned char *)Ptr;
@@ -471,12 +482,12 @@ void BytecodeReader::ParseInstruction(SmallVector<unsigned, 8> &Oprnds,
       break;
     }
     case Instruction::InsertElement: {
-      const PackedType *PackedTy = dyn_cast<PackedType>(InstTy);
-      if (!PackedTy || Oprnds.size() != 3)
+      const VectorType *VectorTy = dyn_cast<VectorType>(InstTy);
+      if (!VectorTy || Oprnds.size() != 3)
         error("Invalid insertelement instruction!");
       
       Value *V1 = getValue(iType, Oprnds[0]);
-      Value *V2 = getValue(getTypeSlot(PackedTy->getElementType()),Oprnds[1]);
+      Value *V2 = getValue(getTypeSlot(VectorTy->getElementType()),Oprnds[1]);
       Value *V3 = getValue(Int32TySlot, Oprnds[2]);
         
       if (!InsertElementInst::isValidOperands(V1, V2, V3))
@@ -485,13 +496,13 @@ void BytecodeReader::ParseInstruction(SmallVector<unsigned, 8> &Oprnds,
       break;
     }
     case Instruction::ShuffleVector: {
-      const PackedType *PackedTy = dyn_cast<PackedType>(InstTy);
-      if (!PackedTy || Oprnds.size() != 3)
+      const VectorType *VectorTy = dyn_cast<VectorType>(InstTy);
+      if (!VectorTy || Oprnds.size() != 3)
         error("Invalid shufflevector instruction!");
       Value *V1 = getValue(iType, Oprnds[0]);
       Value *V2 = getValue(iType, Oprnds[1]);
-      const PackedType *EltTy = 
-        PackedType::get(Type::Int32Ty, PackedTy->getNumElements());
+      const VectorType *EltTy = 
+        VectorType::get(Type::Int32Ty, VectorTy->getNumElements());
       Value *V3 = getValue(getTypeSlot(EltTy), Oprnds[2]);
       if (!ShuffleVectorInst::isValidOperands(V1, V2, V3))
         error("Invalid shufflevector instruction!");
@@ -658,7 +669,7 @@ void BytecodeReader::ParseInstruction(SmallVector<unsigned, 8> &Oprnds,
       const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
       if (FTy == 0) error("Call to non function pointer value!");
 
-      std::vector<Value *> Params;
+      SmallVector<Value *, 8> Params;
       if (!FTy->isVarArg()) {
         FunctionType::param_iterator It = FTy->param_begin();
 
@@ -691,7 +702,7 @@ void BytecodeReader::ParseInstruction(SmallVector<unsigned, 8> &Oprnds,
           Params.push_back(getValue(Oprnds[i], Oprnds[i+1]));
       }
 
-      Result = new CallInst(F, Params);
+      Result = new CallInst(F, &Params[0], Params.size());
       if (isTailCall) cast<CallInst>(Result)->setTailCall();
       if (CallingConv) cast<CallInst>(Result)->setCallingConv(CallingConv);
       break;
@@ -709,7 +720,7 @@ void BytecodeReader::ParseInstruction(SmallVector<unsigned, 8> &Oprnds,
       if (FTy == 0)
         error("Invoke to non function pointer value!");
 
-      std::vector<Value *> Params;
+      SmallVector<Value *, 8> Params;
       BasicBlock *Normal, *Except;
       unsigned CallingConv = Oprnds.back();
       Oprnds.pop_back();
@@ -745,7 +756,7 @@ void BytecodeReader::ParseInstruction(SmallVector<unsigned, 8> &Oprnds,
           Params.push_back(getValue(Oprnds[i], Oprnds[i+1]));
       }
 
-      Result = new InvokeInst(F, Normal, Except, Params);
+      Result = new InvokeInst(F, Normal, Except, &Params[0], Params.size());
       if (CallingConv) cast<InvokeInst>(Result)->setCallingConv(CallingConv);
       break;
     }
@@ -943,6 +954,8 @@ void BytecodeReader::ParseValueSymbolTable(Function *CurrentFunction,
            E = CurrentFunction->end(); I != E; ++I)
       BBMap.push_back(I);
 
+  SmallVector<char, 32> NameStr;
+  
   while (moreInBlock()) {
     // Symtab block header: [num entries][type id number]
     unsigned NumEntries = read_vbr_uint();
@@ -951,19 +964,22 @@ void BytecodeReader::ParseValueSymbolTable(Function *CurrentFunction,
     for (unsigned i = 0; i != NumEntries; ++i) {
       // Symtab entry: [def slot #][name]
       unsigned slot = read_vbr_uint();
-      std::string Name = read_str();
+      read_str(NameStr);
       Value *V = 0;
       if (Typ == LabelTySlot) {
-        if (slot < BBMap.size())
-          V = BBMap[slot];
+        V = (slot < BBMap.size()) ? BBMap[slot] : 0;
       } else {
-        V = getValue(Typ, slot, false); // Find mapping...
+        V = getValue(Typ, slot, false); // Find mapping.
       }
-      if (Handler) Handler->handleSymbolTableValue(Typ, slot, Name);
+      if (Handler) Handler->handleSymbolTableValue(Typ, slot,
+                                                   &NameStr[0], NameStr.size());
       if (V == 0)
-        error("Failed value look-up for name '" + Name + "', type #" + 
+        error("Failed value look-up for name '" + 
+              std::string(NameStr.begin(), NameStr.end()) + "', type #" + 
               utostr(Typ) + " slot #" + utostr(slot));
-      V->setName(Name);
+      V->setName(&NameStr[0], NameStr.size());
+      
+      NameStr.clear();
     }
   }
   checkPastBlockEnd("Symbol Table");
@@ -1013,10 +1029,10 @@ const Type *BytecodeReader::ParseType() {
     Result =  ArrayType::get(ElementType, NumElements);
     break;
   }
-  case Type::PackedTyID: {
+  case Type::VectorTyID: {
     const Type *ElementType = readType();
     unsigned NumElements = read_vbr_uint();
-    Result =  PackedType::get(ElementType, NumElements);
+    Result =  VectorType::get(ElementType, NumElements);
     break;
   }
   case Type::StructTyID: {
@@ -1249,7 +1265,7 @@ Value *BytecodeReader::ParseConstantPoolValue(unsigned TypeID) {
       Result = ConstantInt::get(IT, Val);
       if (Handler) Handler->handleConstantValue(Result);
     } else 
-      assert("Integer types > 64 bits not supported");
+      assert(0 && "Integer types > 64 bits not supported");
     break;
   }
   case Type::FloatTyID: {
@@ -1298,8 +1314,8 @@ Value *BytecodeReader::ParseConstantPoolValue(unsigned TypeID) {
     break;
   }
 
-  case Type::PackedTyID: {
-    const PackedType *PT = cast<PackedType>(Ty);
+  case Type::VectorTyID: {
+    const VectorType *PT = cast<VectorType>(Ty);
     unsigned NumElements = PT->getNumElements();
     unsigned TypeSlot = getTypeSlot(PT->getElementType());
     std::vector<Constant*> Elements;
@@ -1307,8 +1323,8 @@ Value *BytecodeReader::ParseConstantPoolValue(unsigned TypeID) {
     while (NumElements--)     // Read all of the elements of the constant.
       Elements.push_back(getConstantValue(TypeSlot,
                                           read_vbr_uint()));
-    Result = ConstantPacked::get(PT, Elements);
-    if (Handler) Handler->handleConstantPacked(PT, &Elements[0],Elements.size(),
+    Result = ConstantVector::get(PT, Elements);
+    if (Handler) Handler->handleConstantVector(PT, &Elements[0],Elements.size(),
                                                TypeSlot, Result);
     break;
   }
@@ -1981,7 +1997,7 @@ void BytecodeReader::ParseModule() {
 /// and \p Length parameters.
 bool BytecodeReader::ParseBytecode(volatile BufPtr Buf, unsigned Length,
                                    const std::string &ModuleID,
-                                   Decompressor_t *Decompressor, 
+                                   BCDecompressor_t *Decompressor, 
                                    std::string* ErrMsg) {
 
   /// We handle errors by
@@ -2016,6 +2032,9 @@ bool BytecodeReader::ParseBytecode(volatile BufPtr Buf, unsigned Length,
 
   // If this is a compressed file
   if (Sig == ('l' | ('l' << 8) | ('v' << 16) | ('c' << 24))) {
+    if (!Decompressor) {
+      error("Compressed bytecode found, but not decompressor available");
+    }
 
     // Invoke the decompression of the bytecode. Note that we have to skip the
     // file's magic number which is not part of the compressed block. Hence,