Added support for processing abbreviations in the Deserializer.
[oota-llvm.git] / include / llvm / Bitcode / Deserialize.h
index 72367e30d9839ddc7359ef9750c2010aaa04a640..283e6035c795c2fea50bb01dc71b7b47efb22d5e 100644 (file)
@@ -30,54 +30,50 @@ 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(unsigned 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);
-
-    BPNode* getBPNode() const {
-      assert (!hasFinalPtr());
-      return reinterpret_cast<BPNode*>(Ptr & ~0x1);
-    }
+    void MarkFinal() { Raw |= 0x1; }
+    bool hasFinalPtr() const { return Raw & 0x1 ? true : false; }
+    unsigned getID() const { return Raw >> 1; }
     
-    void setBPNode(BPNode* N) {
-      assert (!hasFinalPtr());
-      Ptr = reinterpret_cast<uintptr_t>(N) | 0x1;
+    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) {
+      return (K1.Raw ^ K2.Raw) & ~0x1 ? false : true;
     }
     
-    uintptr_t getFinalPtr() const {
-      assert (!(Ptr & 0x1) && "Backpatch pointer not yet deserialized.");
-      return Ptr;
-    }    
-
-    static inline bool isPod() { return true; }
+    static bool isPod() { return true; }
   };
-
-  typedef llvm::DenseMap<unsigned,BPatchEntry,PtrIdInfo,BPatchEntry> MapTy;
+  
+  typedef llvm::DenseMap<BPKey,BPEntry,BPKey,BPEntry> MapTy;
 
   //===----------------------------------------------------------===//
   // Internal data members.
@@ -108,11 +104,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() {
@@ -134,6 +125,11 @@ public:
     return x;
   }
   
+  template <typename T>
+  inline void ReadOwnedPtr(T*& Ptr) {
+    Ptr = ReadOwnedPtr<T>();
+  }
+  
   template <typename T>
   void ReadPtr(T*& PtrRef) {
     ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef));
@@ -152,13 +148,39 @@ public:
     return *p;
   }
 
+  void RegisterPtr(unsigned PtrId, const void* Ptr);
   
-  void RegisterPtr(unsigned PtrId, void* Ptr);
+  void RegisterPtr(const void* Ptr) {
+    RegisterPtr(ReadInt(),Ptr);
+  }
+  
+  bool AtEnd();
 
+  bool inRecord();
 private:
   void ReadRecord();  
-  bool inRecord();
   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