Fixed hack in BatchReadOwnedPtrs to no longer use the array of pointers passed in for
[oota-llvm.git] / include / llvm / Bitcode / Deserialize.h
index 72367e30d9839ddc7359ef9750c2010aaa04a640..87d0f12728aa79c9bd7e383181d7560d08eb5487 100644 (file)
@@ -30,75 +30,117 @@ class Deserializer {
   //===----------------------------------------------------------===//
   // Internal type definitions.
   //===----------------------------------------------------------===//
-
-  struct PtrIdInfo {
-    static inline unsigned getEmptyKey() { return ~((unsigned) 0x0); }
-    static inline unsigned getTombstoneKey() { return getEmptyKey()-1; }
-    static inline unsigned getHashValue(unsigned X) { return X; }
-    static inline bool isEqual(unsigned X, unsigned Y) { return X == Y; }
-    static inline bool isPod() { return true; }
-  };
   
   struct BPNode {
     BPNode* Next;
     uintptr_t& PtrRef;
+    
     BPNode(BPNode* n, uintptr_t& pref) 
       : Next(n), PtrRef(pref) {
         PtrRef = 0;
       }
   };
   
-  class BPatchEntry {
-    uintptr_t Ptr;
+  struct BPEntry { 
+    union { BPNode* Head; void* Ptr; };
+    
+    BPEntry() : Head(NULL) {}
+    
+    static inline bool isPod() { return true; }
+    
+    void SetPtr(BPNode*& FreeList, void* P);    
+  };  
+  
+  class BPKey {
+    unsigned Raw;
+    
   public:
+    BPKey(SerializedPtrID PtrId) : Raw(PtrId << 1) { assert (PtrId > 0); }
+    BPKey(unsigned code, unsigned) : Raw(code) {}
     
-    BPatchEntry() : Ptr(0x1) {}
-      
-    BPatchEntry(void* P) : Ptr(reinterpret_cast<uintptr_t>(P)) {}
-
-    bool hasFinalPtr() const { return Ptr & 0x1 ? false : true; }
-    void setFinalPtr(BPNode*& FreeList, void* P);
+    void MarkFinal() { Raw |= 0x1; }
+    bool hasFinalPtr() const { return Raw & 0x1 ? true : false; }
+    SerializedPtrID getID() const { return Raw >> 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; }
 
-    BPNode* getBPNode() const {
-      assert (!hasFinalPtr());
-      return reinterpret_cast<BPNode*>(Ptr & ~0x1);
+    static bool isEqual(const BPKey& K1, const BPKey& K2) {
+      return (K1.Raw ^ K2.Raw) & ~0x1 ? false : true;
     }
     
-    void setBPNode(BPNode* N) {
-      assert (!hasFinalPtr());
-      Ptr = reinterpret_cast<uintptr_t>(N) | 0x1;
+    static bool isPod() { return true; }
+  };
+  
+  typedef llvm::DenseMap<BPKey,BPEntry,BPKey,BPEntry> MapTy;
+
+  //===----------------------------------------------------------===//
+  // 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;
     }
     
-    uintptr_t getFinalPtr() const {
-      assert (!(Ptr & 0x1) && "Backpatch pointer not yet deserialized.");
-      return Ptr;
-    }    
+    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;
 
-    static inline bool isPod() { return true; }
+      if ((RHS.BitNo - BitNo) >> 5 < NumWords)
+        return true;
+      
+      return false;
+    }
   };
-
-  typedef llvm::DenseMap<unsigned,BPatchEntry,PtrIdInfo,BPatchEntry> MapTy;
-
+  
   //===----------------------------------------------------------===//
   // 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;
   }
@@ -108,11 +150,6 @@ public:
     SerializeTrait<T>::Read(*this,X);
     return X;
   }
-  
-  template <typename T>
-  inline T ReadVal() {
-    return SerializeTrait<T>::ReadVal(*this);
-  }
 
   template <typename T>
   inline T* Materialize() {
@@ -123,28 +160,88 @@ 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>
-  void ReadPtr(T*& PtrRef) {
-    ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef));
+  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 BatchReadOwnedPtrs(unsigned NumPtrs, T** Ptrs, bool AutoRegister=true) {
+    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 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>
+  T* ReadPtr() { T* x; ReadPtr<T>(x,false); return x; }
 
-  void ReadUIntPtr(uintptr_t& PtrRef);
+  void ReadUIntPtr(uintptr_t& PtrRef, bool AllowBackpatch = true);
   
   template <typename T>
   T& ReadRef() {
@@ -152,13 +249,65 @@ public:
     return *p;
   }
 
+  void RegisterPtr(SerializedPtrID PtrId, const void* Ptr);
   
-  void RegisterPtr(unsigned PtrId, void* Ptr);
-
-private:
-  void ReadRecord();  
+  void RegisterPtr(const void* Ptr) {
+    RegisterPtr(ReadPtrID(),Ptr);
+  }
+  
+  template<typename T>
+  void RegisterRef(const T& x) {
+    RegisterPtr(&x);
+  }
+  
+  template<typename T>
+  void RegisterRef(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) {
+    return V.first.hasFinalPtr();
+  }
+  
+  static inline uintptr_t GetFinalPtr(MapTy::value_type& V) {
+    return reinterpret_cast<uintptr_t>(V.second.Ptr);
+  }
+  
+  static inline BPNode* GetBPNode(MapTy::value_type& V) {
+    return V.second.Head;
+  }
+    
+  static inline void SetBPNode(MapTy::value_type& V, BPNode* N) {
+    V.second.Head = N;
+  }
+  
+  void SetPtr(MapTy::value_type& V, const void* P) {
+    V.first.MarkFinal();
+    V.second.SetPtr(FreeList,const_cast<void*>(P));
+  }
 };
     
 } // end namespace llvm