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 //===----------------------------------------------------------===//
88 Location(uint64_t bit, unsigned bid, unsigned words)
89 : BitNo(bit), BlockID(bid), NumWords(words) {}
91 Location() : BitNo(0), BlockID(0), NumWords(0) {}
93 Location& operator=(Location& RHS) {
95 BlockID = RHS.BlockID;
96 NumWords = RHS.NumWords;
100 bool operator==(const Location& RHS) const { return BitNo == RHS.BitNo; }
101 bool operator!=(const Location& RHS) const { return BitNo != RHS.BitNo; }
103 bool contains(const Location& RHS) const {
104 if (RHS.BitNo < BitNo)
107 if ((RHS.BitNo - BitNo) >> 5 < NumWords)
114 //===----------------------------------------------------------===//
115 // Internal data members.
116 //===----------------------------------------------------------===//
119 BitstreamReader& Stream;
120 SmallVector<uint64_t,20> Record;
122 BumpPtrAllocator Allocator;
125 llvm::SmallVector<Location,8> BlockStack;
128 Location StreamStart;
130 //===----------------------------------------------------------===//
132 //===----------------------------------------------------------===//
135 Deserializer(BitstreamReader& stream);
141 SerializedPtrID ReadPtrID() { return (SerializedPtrID) ReadInt(); }
143 SerializedPtrID ReadDiffPtrID(SerializedPtrID& PrevID) {
145 return (SerializedPtrID) (x ? (PrevID+1) : 0);
150 return ReadInt() ? true : false;
153 template <typename T>
154 inline T& Read(T& X) {
155 SerializeTrait<T>::Read(*this,X);
159 template <typename T>
161 return SerializeTrait<T>::Create(*this);
164 char* ReadCStr(char* cstr = NULL, unsigned MaxLen=0, bool isNullTerm=true);
165 void ReadCStr(std::vector<char>& buff, bool isNullTerm=false);
167 template <typename T>
168 inline T* ReadOwnedPtr(bool AutoRegister = true) {
169 SerializedPtrID PtrID = ReadPtrID();
174 T* x = SerializeTrait<T>::Create(*this);
177 RegisterPtr(PtrID,x);
182 template <typename T>
183 inline void ReadOwnedPtr(T*& Ptr, bool AutoRegister = true) {
184 Ptr = ReadOwnedPtr<T>(AutoRegister);
187 template <typename T1, typename T2>
188 void BatchReadOwnedPtrs(T1*& P1, T2*& P2,
189 bool A1=true, bool A2=true) {
191 SerializedPtrID ID1 = ReadPtrID();
192 SerializedPtrID ID2 = ReadDiffPtrID(ID2);
194 P1 = (ID1) ? SerializeTrait<T1>::Create(*this) : NULL;
195 if (ID1 && A1) RegisterPtr(ID1,P1);
197 P2 = (ID2) ? SerializeTrait<T2>::Create(*this) : NULL;
198 if (ID2 && A2) RegisterPtr(ID2,P2);
201 template <typename T1, typename T2, typename T3>
202 void BatchReadOwnedPtrs(T1*& P1, T2*& P2, T3*& P3,
203 bool A1=true, bool A2=true, bool A3=true) {
205 SerializedPtrID ID1 = ReadPtrID();
206 SerializedPtrID ID2 = ReadDiffPtrID(ID1);
207 SerializedPtrID ID3 = ReadDiffPtrID(ID2);
209 P1 = (ID1) ? SerializeTrait<T1>::Create(*this) : NULL;
210 if (ID1 && A1) RegisterPtr(ID1,P1);
212 P2 = (ID2) ? SerializeTrait<T2>::Create(*this) : NULL;
213 if (ID2 && A2) RegisterPtr(ID2,P2);
215 P3 = (ID3) ? SerializeTrait<T3>::Create(*this) : NULL;
216 if (ID3 && A3) RegisterPtr(ID3,P3);
219 template <typename T1, typename T2, typename T3, typename T4>
220 void BatchReadOwnedPtrs(T1*& P1, T2*& P2, T3*& P3, T4*& P4,
221 bool A1=true, bool A2=true, bool A3=true, bool A4=true) {
223 SerializedPtrID ID1 = ReadPtrID();
224 SerializedPtrID ID2 = ReadDiffPtrID(ID1);
225 SerializedPtrID ID3 = ReadDiffPtrID(ID2);
226 SerializedPtrID ID4 = ReadDiffPtrID(ID3);
228 P1 = (ID1) ? SerializeTrait<T1>::Create(*this) : NULL;
229 if (ID1 && A1) RegisterPtr(ID1,P1);
231 P2 = (ID2) ? SerializeTrait<T2>::Create(*this) : NULL;
232 if (ID2 && A2) RegisterPtr(ID2,P2);
234 P3 = (ID3) ? SerializeTrait<T3>::Create(*this) : NULL;
235 if (ID3 && A3) RegisterPtr(ID3,P3);
237 P4 = (ID4) ? SerializeTrait<T4>::Create(*this) : NULL;
238 if (ID4 && A4) RegisterPtr(ID4,P4);
241 template <typename T>
242 void BatchReadOwnedPtrs(unsigned NumPtrs, T** Ptrs, bool AutoRegister=true) {
243 llvm::SmallVector<SerializedPtrID,10> BatchIDVec;
244 SerializedPtrID TempPtrID;
246 for (unsigned i = 0; i < NumPtrs; ++i) {
247 TempPtrID = i ? ReadDiffPtrID(TempPtrID) : ReadPtrID();
248 BatchIDVec.push_back(TempPtrID);
251 for (unsigned i = 0; i < NumPtrs; ++i) {
252 SerializedPtrID& PtrID = BatchIDVec[i];
254 T* p = PtrID ? SerializeTrait<T>::Create(*this) : NULL;
256 if (PtrID && AutoRegister)
257 RegisterPtr(PtrID,p);
263 template <typename T1, typename T2>
264 void BatchReadOwnedPtrs(unsigned NumT1Ptrs, T1** Ptrs, T2*& P2,
265 bool A1=true, bool A2=true) {
267 SerializedPtrID ID2 = ReadPtrID();
268 SerializedPtrID TempID = ID2;
270 llvm::SmallVector<SerializedPtrID,10> BatchIDVec;
272 for (unsigned i = 0; i < NumT1Ptrs; ++i) {
273 TempID = ReadDiffPtrID(TempID);
274 BatchIDVec.push_back(TempID);
277 P2 = (ID2) ? SerializeTrait<T2>::Create(*this) : NULL;
278 if (ID2 && A2) RegisterPtr(ID2,P2);
280 for (unsigned i = 0; i < NumT1Ptrs; ++i) {
281 SerializedPtrID& PtrID = BatchIDVec[i];
283 T1* p = PtrID ? SerializeTrait<T1>::Create(*this) : NULL;
286 RegisterPtr(PtrID,p);
292 template <typename T1, typename T2, typename T3>
293 void BatchReadOwnedPtrs(unsigned NumT1Ptrs, T1** Ptrs,
295 bool A1=true, bool A2=true, bool A3=true) {
297 SerializedPtrID ID2 = ReadPtrID();
298 SerializedPtrID ID3 = ReadDiffPtrID(ID2);
300 SerializedPtrID TempID = ID3;
302 llvm::SmallVector<SerializedPtrID,10> BatchIDVec;
304 for (unsigned i = 0; i < NumT1Ptrs; ++i) {
305 TempID = ReadDiffPtrID(TempID);
306 BatchIDVec.push_back(TempID);
309 P2 = (ID2) ? SerializeTrait<T2>::Create(*this) : NULL;
310 if (ID2 && A2) RegisterPtr(ID2,P2);
312 P3 = (ID3) ? SerializeTrait<T3>::Create(*this) : NULL;
313 if (ID3 && A3) RegisterPtr(ID3,P3);
315 for (unsigned i = 0; i < NumT1Ptrs; ++i) {
316 SerializedPtrID& PtrID = BatchIDVec[i];
318 T1* p = PtrID ? SerializeTrait<T1>::Create(*this) : NULL;
321 RegisterPtr(PtrID,p);
327 template <typename T>
328 void ReadPtr(T*& PtrRef, bool AllowBackpatch = true) {
329 ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef), AllowBackpatch);
332 template <typename T>
333 void ReadPtr(const T*& PtrRef, bool AllowBackpatch = true) {
334 ReadPtr(const_cast<T*&>(PtrRef), AllowBackpatch);
338 template <typename T>
339 void ReadPtr(T*& PtrRef, const SerializedPtrID& PtrID,
340 bool AllowBackpatch = true) {
341 ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef), PtrID, AllowBackpatch);
344 template <typename T>
345 void ReadPtr(const T*& PtrRef, const SerializedPtrID& PtrID,
346 bool AllowBackpatch = true) {
348 ReadPtr(const_cast<T*&>(PtrRef), PtrID, AllowBackpatch);
351 template <typename T>
352 T* ReadPtr() { T* x; ReadPtr<T>(x,false); return x; }
354 void ReadUIntPtr(uintptr_t& PtrRef, const SerializedPtrID& PtrID,
355 bool AllowBackpatch = true);
357 void ReadUIntPtr(uintptr_t& PtrRef, bool AllowBackpatch = true) {
358 ReadUIntPtr(PtrRef,ReadPtrID(),AllowBackpatch);
361 template <typename T>
363 T* p = reinterpret_cast<T*>(ReadInternalRefPtr());
367 void RegisterPtr(const SerializedPtrID& PtrID, const void* Ptr);
369 void RegisterPtr(const void* Ptr) {
370 RegisterPtr(ReadPtrID(),Ptr);
374 void RegisterRef(const T& x) {
379 void RegisterRef(const SerializedPtrID& PtrID, const T& x) {
380 RegisterPtr(PtrID,&x);
383 Location getCurrentBlockLocation();
384 unsigned getCurrentBlockID();
385 unsigned getAbbrevNo();
387 bool FinishedBlock(Location BlockLoc);
388 bool JumpTo(const Location& BlockLoc);
389 void Rewind() { JumpTo(StreamStart); }
394 bool SkipToBlock(unsigned BlockID);
396 unsigned getRecordCode();
398 BitstreamReader& getStream() { return Stream; }
401 bool AdvanceStream();
404 uintptr_t ReadInternalRefPtr();
406 static inline bool HasFinalPtr(MapTy::value_type& V) {
407 return V.first.hasFinalPtr();
410 static inline uintptr_t GetFinalPtr(MapTy::value_type& V) {
411 return reinterpret_cast<uintptr_t>(V.second.Ptr);
414 static inline BPNode* GetBPNode(MapTy::value_type& V) {
415 return V.second.Head;
418 static inline void SetBPNode(MapTy::value_type& V, BPNode* N) {
422 void SetPtr(MapTy::value_type& V, const void* P) {
424 V.second.SetPtr(FreeList,const_cast<void*>(P));
428 } // end namespace llvm