1 //=- Deserialize.h - Generic Object Deserialization from Bitcode --*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Ted Kremenek and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the interface for generic object deserialization from
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_BITCODE_SERIALIZE_INPUT
16 #define LLVM_BITCODE_SERIALIZE_INPUT
18 #include "llvm/Bitcode/BitstreamReader.h"
19 #include "llvm/Bitcode/Serialization.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/Support/Allocator.h"
23 #include "llvm/Support/DataTypes.h"
30 //===----------------------------------------------------------===//
31 // Internal type definitions.
32 //===----------------------------------------------------------===//
38 BPNode(BPNode* n, uintptr_t& pref)
39 : Next(n), PtrRef(pref) {
45 union { BPNode* Head; void* Ptr; };
47 BPEntry() : Head(NULL) {}
49 static inline bool isPod() { return true; }
51 void SetPtr(BPNode*& FreeList, void* P);
58 BPKey(SerializedPtrID PtrId) : Raw(PtrId << 1) { assert (PtrId > 0); }
59 BPKey(unsigned code, unsigned) : Raw(code) {}
61 void MarkFinal() { Raw |= 0x1; }
62 bool hasFinalPtr() const { return Raw & 0x1 ? true : false; }
63 SerializedPtrID getID() const { return Raw >> 1; }
65 static inline BPKey getEmptyKey() { return BPKey(0,0); }
66 static inline BPKey getTombstoneKey() { return BPKey(1,0); }
67 static inline unsigned getHashValue(const BPKey& K) { return K.Raw & ~0x1; }
69 static bool isEqual(const BPKey& K1, const BPKey& K2) {
70 return (K1.Raw ^ K2.Raw) & ~0x1 ? false : true;
73 static bool isPod() { return true; }
76 typedef llvm::DenseMap<BPKey,BPEntry,BPKey,BPEntry> MapTy;
78 //===----------------------------------------------------------===//
79 // Publicly visible types.
80 //===----------------------------------------------------------===//
83 typedef uint64_t Location;
85 //===----------------------------------------------------------===//
86 // Internal data members.
87 //===----------------------------------------------------------===//
90 BitstreamReader& Stream;
91 SmallVector<uint64_t,10> Record;
93 BumpPtrAllocator Allocator;
96 llvm::SmallVector<uint64_t,5> BlockLocs;
98 //===----------------------------------------------------------===//
100 //===----------------------------------------------------------===//
103 Deserializer(BitstreamReader& stream);
108 SerializedPtrID ReadPtrID() { return (SerializedPtrID) ReadInt(); }
112 return ReadInt() ? true : false;
115 template <typename T>
116 inline T& Read(T& X) {
117 SerializeTrait<T>::Read(*this,X);
121 template <typename T>
122 inline T* Materialize() {
123 return SerializeTrait<T>::Materialize(*this);
126 char* ReadCStr(char* cstr = NULL, unsigned MaxLen=0, bool isNullTerm=true);
127 void ReadCStr(std::vector<char>& buff, bool isNullTerm=false);
129 template <typename T>
130 inline T* ReadOwnedPtr(bool AutoRegister = true) {
131 SerializedPtrID PtrID = ReadPtrID();
136 T* x = SerializeTrait<T>::Materialize(*this);
139 RegisterPtr(PtrID,x);
144 template <typename T>
145 inline void ReadOwnedPtr(T*& Ptr, bool AutoRegister = true) {
146 Ptr = ReadOwnedPtr<T>(AutoRegister);
149 template <typename T1, typename T2>
150 void BatchReadOwnedPtrs(T1*& P1, T2*& P2,
151 bool A1=true, bool A2=true) {
153 SerializedPtrID ID1 = ReadPtrID();
154 SerializedPtrID ID2 = ReadPtrID();
156 P1 = (ID1) ? SerializeTrait<T1>::Materialize(*this) : NULL;
157 if (ID1 && A1) RegisterPtr(ID1,P1);
159 P2 = (ID2) ? SerializeTrait<T2>::Materialize(*this) : NULL;
160 if (ID2 && A2) RegisterPtr(ID2,P2);
163 template <typename T1, typename T2, typename T3>
164 void BatchReadOwnedPtrs(T1*& P1, T2*& P2, T3*& P3,
165 bool A1=true, bool A2=true, bool A3=true) {
167 SerializedPtrID ID1 = ReadPtrID();
168 SerializedPtrID ID2 = ReadPtrID();
169 SerializedPtrID ID3 = ReadPtrID();
171 P1 = (ID1) ? SerializeTrait<T1>::Materialize(*this) : NULL;
172 if (ID1 && A1) RegisterPtr(ID1,P1);
174 P2 = (ID2) ? SerializeTrait<T2>::Materialize(*this) : NULL;
175 if (ID2 && A2) RegisterPtr(ID2,P2);
177 P3 = (ID3) ? SerializeTrait<T2>::Materialize(*this) : NULL;
178 if (ID3 && A3) RegisterPtr(ID3,P3);
181 template <typename T>
182 void BatchReadOwnedPtrs(unsigned NumPtrs, T** Ptrs, bool AutoRegister=true) {
183 for (unsigned i = 0; i < NumPtrs; ++i)
184 reinterpret_cast<SerializedPtrID&>(Ptrs[i]) = ReadPtrID();
186 for (unsigned i = 0; i < NumPtrs; ++i) {
187 SerializedPtrID PtrID = reinterpret_cast<SerializedPtrID>(Ptrs[i]);
188 T* p = PtrID ? SerializeTrait<T>::Materialize(*this) : NULL;
190 if (PtrID && AutoRegister)
191 RegisterPtr(PtrID,p);
197 template <typename T>
198 void ReadPtr(T*& PtrRef, bool AllowBackpatch = true) {
199 ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef), AllowBackpatch);
202 template <typename T>
203 void ReadPtr(const T*& PtrRef, bool AllowBackpatch = true) {
204 ReadPtr(const_cast<T*&>(PtrRef), AllowBackpatch);
207 template <typename T>
208 T* ReadPtr() { T* x; ReadPtr<T>(x,false); return x; }
210 void ReadUIntPtr(uintptr_t& PtrRef, bool AllowBackpatch = true);
212 template <typename T>
214 T* p = reinterpret_cast<T*>(ReadInternalRefPtr());
218 void RegisterPtr(SerializedPtrID PtrId, const void* Ptr);
220 void RegisterPtr(const void* Ptr) {
221 RegisterPtr(ReadPtrID(),Ptr);
225 void RegisterRef(const T& x) {
230 void RegisterRef(SerializedPtrID PtrID, const T& x) {
231 RegisterPtr(PtrID,&x);
234 Location GetCurrentBlockLocation();
235 bool FinishedBlock(Location BlockLoc);
242 uintptr_t ReadInternalRefPtr();
244 static inline bool HasFinalPtr(MapTy::value_type& V) {
245 return V.first.hasFinalPtr();
248 static inline uintptr_t GetFinalPtr(MapTy::value_type& V) {
249 return reinterpret_cast<uintptr_t>(V.second.Ptr);
252 static inline BPNode* GetBPNode(MapTy::value_type& V) {
253 return V.second.Head;
256 static inline void SetBPNode(MapTy::value_type& V, BPNode* N) {
260 void SetPtr(MapTy::value_type& V, const void* P) {
262 V.second.SetPtr(FreeList,const_cast<void*>(P));
266 } // end namespace llvm