[llvm-commits] [llvm] r43570 - in /llvm/trunk: include/llvm/Bitcode/Deserialize.h lib/Bitcode/Reader/Deserialize.cpp

Ted Kremenek kremenek at apple.com
Wed Oct 31 12:58:32 PDT 2007


Author: kremenek
Date: Wed Oct 31 14:58:32 2007
New Revision: 43570

URL: http://llvm.org/viewvc/llvm-project?rev=43570&view=rev
Log:
Implemented deserialization of references.  References are handled
just like pointers, except that they cannot be backpatched.  This
means that references are essentially non-owning pointers where the
referred object must be deserialized prior to the reference being
deserialized.  Because of the nature of references, this ordering of
objects is always possible.

Fixed a bug in backpatching code (returning the backpatched pointer
would accidentally include a bit flag).

Modified:
    llvm/trunk/include/llvm/Bitcode/Deserialize.h
    llvm/trunk/lib/Bitcode/Reader/Deserialize.cpp

Modified: llvm/trunk/include/llvm/Bitcode/Deserialize.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Bitcode/Deserialize.h?rev=43570&r1=43569&r2=43570&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Bitcode/Deserialize.h (original)
+++ llvm/trunk/include/llvm/Bitcode/Deserialize.h Wed Oct 31 14:58:32 2007
@@ -56,7 +56,7 @@
       
     BPatchEntry(void* P) : Ptr(reinterpret_cast<uintptr_t>(P)) {}
 
-    bool hasFinalPtr() const { return Ptr & 0x1 ? true : false; }
+    bool hasFinalPtr() const { return Ptr & 0x1 ? false : true; }
     void setFinalPtr(BPNode*& FreeList, void* P);
 
     BPNode* getBPNode() const {
@@ -69,7 +69,10 @@
       Ptr = reinterpret_cast<uintptr_t>(N) | 0x1;
     }
     
-    uintptr_t getRawPtr() const { return Ptr; }
+    uintptr_t getFinalPtr() const {
+      assert (!(Ptr & 0x1) && "Backpatch pointer not yet deserialized.");
+      return Ptr;
+    }    
 
     static inline bool isPod() { return true; }
   };
@@ -132,17 +135,30 @@
   }
   
   template <typename T>
-  void ReadPtr(T*& PtrRef) { ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef));}
-
-  void ReadPtr(uintptr_t& PtrRef) { ReadUIntPtr(PtrRef); }
+  void ReadPtr(T*& PtrRef) {
+    ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef));
+  }
   
+  template <typename T>
+  void ReadPtr(const T*& PtrRef) {
+    ReadPtr(const_cast<T*&>(PtrRef));
+  }            
+
   void ReadUIntPtr(uintptr_t& PtrRef);
   
+  template <typename T>
+  T& ReadRef() {
+    T* p = reinterpret_cast<T*>(ReadInternalRefPtr());
+    return *p;
+  }
+
+  
   void RegisterPtr(unsigned PtrId, void* Ptr);
 
 private:
   void ReadRecord();  
   bool inRecord();
+  uintptr_t ReadInternalRefPtr();
 };
     
 } // end namespace llvm

Modified: llvm/trunk/lib/Bitcode/Reader/Deserialize.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/Deserialize.cpp?rev=43570&r1=43569&r2=43570&view=diff

==============================================================================
--- llvm/trunk/lib/Bitcode/Reader/Deserialize.cpp (original)
+++ llvm/trunk/lib/Bitcode/Reader/Deserialize.cpp Wed Oct 31 14:58:32 2007
@@ -115,7 +115,7 @@
   BPatchEntry& E = BPatchMap[PtrId];
   
   if (E.hasFinalPtr())
-    PtrRef = E.getRawPtr();
+    PtrRef = E.getFinalPtr();
   else {
     // Register backpatch.  Check the freelist for a BPNode.
     BPNode* N;
@@ -132,6 +132,18 @@
   }
 }
 
+uintptr_t Deserializer::ReadInternalRefPtr() {
+  unsigned PtrId = ReadInt();
+  
+  assert (PtrId != 0 && "References cannot refer the NULL address.");
+
+  BPatchEntry& E = BPatchMap[PtrId];
+  
+  assert (E.hasFinalPtr() &&
+          "Cannot backpatch references.  Object must be already deserialized.");
+  
+  return E.getFinalPtr();
+}
 
 void Deserializer::BPatchEntry::setFinalPtr(BPNode*& FreeList, void* P) {
   assert (!hasFinalPtr());





More information about the llvm-commits mailing list