Fixed bug with inconsistent serialization/deserialization in matching
[oota-llvm.git] / include / llvm / Bitcode / Deserialize.h
index 332a842d2800d468b419b33a1bdb621c6b230880..8013f66f36d16ec13e74f6b23b85489e4c3e3b3c 100644 (file)
@@ -55,14 +55,15 @@ class Deserializer {
     unsigned Raw;
     
   public:
-    BPKey(unsigned PtrId) : Raw(PtrId << 1) { assert (PtrId > 0); }
+    BPKey(SerializedPtrID PtrId) : Raw(PtrId << 1) { assert (PtrId > 0); }
+    BPKey(unsigned code, unsigned) : Raw(code) {}
     
     void MarkFinal() { Raw |= 0x1; }
     bool hasFinalPtr() const { return Raw & 0x1 ? true : false; }
-    unsigned getID() const { return Raw >> 1; }
+    SerializedPtrID getID() const { return Raw >> 1; }
     
-    static inline BPKey getEmptyKey() { return 0; }
-    static inline BPKey getTombstoneKey() { return 1; }
+    static inline BPKey getEmptyKey() { return BPKey(0,0); }
+    static inline BPKey getTombstoneKey() { return BPKey(1,0); }
     static inline unsigned getHashValue(const BPKey& K) { return K.Raw & ~0x1; }
 
     static bool isEqual(const BPKey& K1, const BPKey& K2) {
@@ -75,25 +76,71 @@ class Deserializer {
   typedef llvm::DenseMap<BPKey,BPEntry,BPKey,BPEntry> MapTy;
 
   //===----------------------------------------------------------===//
-  // Internal data members.
+  // Publicly visible types.
   //===----------------------------------------------------------===//
   
+public:  
+  struct Location {
+    uint64_t BitNo;
+    unsigned BlockID;
+    unsigned NumWords;
+    
+    Location(uint64_t bit, unsigned bid, unsigned words) 
+    : BitNo(bit), BlockID(bid), NumWords(words) {}
+    
+    Location() : BitNo(0), BlockID(0), NumWords(0) {}
+
+    Location& operator=(Location& RHS) {
+      BitNo = RHS.BitNo;
+      BlockID = RHS.BlockID;
+      NumWords = RHS.NumWords;
+      return *this;
+    }
+    
+    bool operator==(const Location& RHS) const { return BitNo == RHS.BitNo; }    
+    bool operator!=(const Location& RHS) const { return BitNo != RHS.BitNo; }
+    
+    bool contains(const Location& RHS) const {
+      if (RHS.BitNo < BitNo)
+        return false;
+
+      if ((RHS.BitNo - BitNo) >> 5 < NumWords)
+        return true;
+      
+      return false;
+    }
+  };
+  
+  //===----------------------------------------------------------===//
+  // Internal data members.
+  //===----------------------------------------------------------===//
+
+private:
   BitstreamReader& Stream;
-  SmallVector<uint64_t,10> Record;
+  SmallVector<uint64_t,20> Record;
   unsigned RecIdx;
   BumpPtrAllocator Allocator;
   BPNode* FreeList;
-  MapTy BPatchMap;  
+  MapTy BPatchMap;
+  llvm::SmallVector<Location,8> BlockStack;
+  unsigned AbbrevNo;
+  unsigned RecordCode;
+  Location StreamStart;
+  std::vector<SerializedPtrID> BatchIDVec;
   
   //===----------------------------------------------------------===//
   // Public Interface.
   //===----------------------------------------------------------===//
   
-public:
+public:  
   Deserializer(BitstreamReader& stream);
   ~Deserializer();
 
   uint64_t ReadInt();
+  int64_t ReadSInt();
+  SerializedPtrID ReadPtrID() { return (SerializedPtrID) ReadInt(); }
+  
+  
   bool ReadBool() {
     return ReadInt() ? true : false;
   }
@@ -113,33 +160,139 @@ public:
   void ReadCStr(std::vector<char>& buff, bool isNullTerm=false);
 
   template <typename T>
-  inline T* ReadOwnedPtr() {    
-    unsigned PtrId = ReadInt();
+  inline T* ReadOwnedPtr(bool AutoRegister = true) {
+    SerializedPtrID PtrID = ReadPtrID();    
 
-    if (PtrId == 0)
+    if (!PtrID)
       return NULL;
     
     T* x = SerializeTrait<T>::Materialize(*this);
-    RegisterPtr(PtrId,x);
+
+    if (AutoRegister)
+      RegisterPtr(PtrID,x);
+    
     return x;
   }
   
   template <typename T>
-  inline void ReadOwnedPtr(T*& Ptr) {
-    Ptr = ReadOwnedPtr<T>();
+  inline void ReadOwnedPtr(T*& Ptr, bool AutoRegister = true) {
+    Ptr = ReadOwnedPtr<T>(AutoRegister);
+  }
+  
+  template <typename T1, typename T2>
+  void BatchReadOwnedPtrs(T1*& P1, T2*& P2,
+                          bool A1=true, bool A2=true) {
+
+    SerializedPtrID ID1 = ReadPtrID();
+    SerializedPtrID ID2 = ReadPtrID();
+
+    P1 = (ID1) ? SerializeTrait<T1>::Materialize(*this) : NULL;
+    if (ID1 && A1) RegisterPtr(ID1,P1);
+
+    P2 = (ID2) ? SerializeTrait<T2>::Materialize(*this) : NULL;
+    if (ID2 && A2) RegisterPtr(ID2,P2);
+  }
+
+  template <typename T1, typename T2, typename T3>
+  void BatchReadOwnedPtrs(T1*& P1, T2*& P2, T3*& P3,
+                          bool A1=true, bool A2=true, bool A3=true) {
+    
+    SerializedPtrID ID1 = ReadPtrID();
+    SerializedPtrID ID2 = ReadPtrID();
+    SerializedPtrID ID3 = ReadPtrID();
+    
+    P1 = (ID1) ? SerializeTrait<T1>::Materialize(*this) : NULL;
+    if (ID1 && A1) RegisterPtr(ID1,P1);    
+    
+    P2 = (ID2) ? SerializeTrait<T2>::Materialize(*this) : NULL;
+    if (ID2 && A2) RegisterPtr(ID2,P2);
+    
+    P3 = (ID3) ? SerializeTrait<T2>::Materialize(*this) : NULL;
+    if (ID3 && A3) RegisterPtr(ID3,P3);
   }
   
   template <typename T>
-  void ReadPtr(T*& PtrRef) {
-    ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef));
+  void BatchReadOwnedPtrs(unsigned NumPtrs, T** Ptrs, bool AutoRegister=true) {
+    BatchIDVec.clear();
+    
+    for (unsigned i = 0; i < NumPtrs; ++i)
+      BatchIDVec.push_back(ReadPtrID());
+    
+    for (unsigned i = 0; i < NumPtrs; ++i) {
+      SerializedPtrID& PtrID = BatchIDVec[i];
+      
+      T* p = PtrID ? SerializeTrait<T>::Materialize(*this) : NULL;
+      
+      if (PtrID && AutoRegister)
+        RegisterPtr(PtrID,p);
+      
+      Ptrs[i] = p;
+    }
+  }
+  
+  template <typename T1, typename T2, typename T3>
+  void BatchReadOwnedPtrs(unsigned NumT1Ptrs, T1** Ptrs, 
+                          T2*& P2, T3*& P3,
+                          bool A1=true, bool A2=true, bool A3=true) {
+    
+    BatchIDVec.clear();
+    
+    for (unsigned i = 0; i < NumT1Ptrs; ++i)
+      BatchIDVec.push_back(ReadPtrID());
+    
+    SerializedPtrID ID2 = ReadPtrID();
+    SerializedPtrID ID3 = ReadPtrID();    
+    
+    for (unsigned i = 0; i < NumT1Ptrs; ++i) {
+      SerializedPtrID& PtrID = BatchIDVec[i];
+      
+      T1* p = PtrID ? SerializeTrait<T1>::Materialize(*this) : NULL;
+      
+      if (PtrID && A1)
+        RegisterPtr(PtrID,p);
+      
+      Ptrs[i] = p;
+    }
+    
+    P2 = (ID2) ? SerializeTrait<T2>::Materialize(*this) : NULL;
+    if (ID2 && A2) RegisterPtr(ID2,P2);
+    
+    P3 = (ID3) ? SerializeTrait<T3>::Materialize(*this) : NULL;
+    if (ID3 && A3) RegisterPtr(ID3,P3);    
+  }    
+  
+  template <typename T>
+  void ReadPtr(T*& PtrRef, bool AllowBackpatch = true) {
+    ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef), AllowBackpatch);
   }
   
   template <typename T>
-  void ReadPtr(const T*& PtrRef) {
-    ReadPtr(const_cast<T*&>(PtrRef));
-  }            
+  void ReadPtr(const T*& PtrRef, bool AllowBackpatch = true) {
+    ReadPtr(const_cast<T*&>(PtrRef), AllowBackpatch);
+  }
+  
+  
+  template <typename T>
+  void ReadPtr(T*& PtrRef, const SerializedPtrID& PtrID, bool AllowBackpatch = true) {
+    ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef), PtrID, AllowBackpatch);
+  }
+  
+  template <typename T>
+  void ReadPtr(const T*& PtrRef, const SerializedPtrID& PtrID, 
+               bool AllowBackpatch = true) {
+    
+    ReadPtr(const_cast<T*&>(PtrRef), PtrID, AllowBackpatch);
+  }
+  
+  template <typename T>
+  T* ReadPtr() { T* x; ReadPtr<T>(x,false); return x; }
 
-  void ReadUIntPtr(uintptr_t& PtrRef);
+  void ReadUIntPtr(uintptr_t& PtrRef, const SerializedPtrID& PtrID, 
+                   bool AllowBackpatch = true);
+  
+  void ReadUIntPtr(uintptr_t& PtrRef, bool AllowBackpatch = true) {
+    ReadUIntPtr(PtrRef,ReadPtrID(),AllowBackpatch);
+  }
   
   template <typename T>
   T& ReadRef() {
@@ -147,15 +300,43 @@ public:
     return *p;
   }
 
-  void RegisterPtr(unsigned PtrId, const void* Ptr);
+  void RegisterPtr(const SerializedPtrID& PtrID, const void* Ptr);
   
   void RegisterPtr(const void* Ptr) {
-    RegisterPtr(ReadInt(),Ptr);
+    RegisterPtr(ReadPtrID(),Ptr);
   }
-
-private:
-  void ReadRecord();  
+  
+  template<typename T>
+  void RegisterRef(const T& x) {
+    RegisterPtr(&x);
+  }
+  
+  template<typename T>
+  void RegisterRef(const SerializedPtrID& PtrID, const T& x) {
+    RegisterPtr(PtrID,&x);
+  }  
+  
+  Location getCurrentBlockLocation();
+  unsigned getCurrentBlockID();
+  unsigned getAbbrevNo();
+  
+  bool FinishedBlock(Location BlockLoc);
+  bool JumpTo(const Location& BlockLoc);
+  void Rewind() { JumpTo(StreamStart); }
+  
+  bool AtEnd();
   bool inRecord();
+  void SkipBlock();
+  bool SkipToBlock(unsigned BlockID);
+  
+  unsigned getRecordCode();
+  
+  BitstreamReader& getStream() { return Stream; }
+  
+private:
+  bool AdvanceStream();  
+  void ReadRecord();
+  
   uintptr_t ReadInternalRefPtr();
   
   static inline bool HasFinalPtr(MapTy::value_type& V) {