1 //=- Deserialize.h - Generic Object Deserialization from Bitcode --*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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 uint64_t StreamStart;
130 //===----------------------------------------------------------===//
132 //===----------------------------------------------------------===//
135 Deserializer(BitstreamReader& stream);
140 SerializedPtrID ReadPtrID() { return (SerializedPtrID) ReadInt(); }
144 return ReadInt() ? true : false;
147 template <typename T>
148 inline T& Read(T& X) {
149 SerializeTrait<T>::Read(*this,X);
153 template <typename T>
155 return SerializeTrait<T>::Create(*this);
158 char* ReadCStr(char* cstr = NULL, unsigned MaxLen=0, bool isNullTerm=true);
159 void ReadCStr(std::vector<char>& buff, bool isNullTerm=false, unsigned Idx=0);
161 template <typename T>
162 inline T* ReadOwnedPtr(bool AutoRegister = true) {
163 SerializedPtrID PtrID = ReadPtrID();
168 T* x = SerializeTrait<T>::Create(*this);
171 RegisterPtr(PtrID,x);
176 template <typename T, typename Arg1>
177 inline T* ReadOwnedPtr(Arg1& arg1, bool AutoRegister = true) {
178 SerializedPtrID PtrID = ReadPtrID();
183 T* x = SerializeTrait<T>::Create(*this, arg1);
186 RegisterPtr(PtrID,x);
191 template <typename T>
192 inline void ReadOwnedPtr(T*& Ptr, bool AutoRegister = true) {
193 Ptr = ReadOwnedPtr<T>(AutoRegister);
196 template <typename T1, typename T2>
197 void BatchReadOwnedPtrs(T1*& P1, T2*& P2,
198 bool A1=true, bool A2=true) {
200 SerializedPtrID ID1 = ReadPtrID();
201 SerializedPtrID ID2 = ReadPtrID();
203 P1 = (ID1) ? SerializeTrait<T1>::Create(*this) : NULL;
204 if (ID1 && A1) RegisterPtr(ID1,P1);
206 P2 = (ID2) ? SerializeTrait<T2>::Create(*this) : NULL;
207 if (ID2 && A2) RegisterPtr(ID2,P2);
210 template <typename T1, typename T2, typename Arg1>
211 void BatchReadOwnedPtrs(T1*& P1, T2*& P2, Arg1& arg1,
212 bool A1=true, bool A2=true) {
214 SerializedPtrID ID1 = ReadPtrID();
215 SerializedPtrID ID2 = ReadPtrID();
217 P1 = (ID1) ? SerializeTrait<T1>::Create(*this, arg1) : NULL;
218 if (ID1 && A1) RegisterPtr(ID1,P1);
220 P2 = (ID2) ? SerializeTrait<T2>::Create(*this, arg1) : NULL;
221 if (ID2 && A2) RegisterPtr(ID2,P2);
224 template <typename T1, typename T2, typename T3>
225 void BatchReadOwnedPtrs(T1*& P1, T2*& P2, T3*& P3,
226 bool A1=true, bool A2=true, bool A3=true) {
228 SerializedPtrID ID1 = ReadPtrID();
229 SerializedPtrID ID2 = ReadPtrID();
230 SerializedPtrID ID3 = ReadPtrID();
232 P1 = (ID1) ? SerializeTrait<T1>::Create(*this) : NULL;
233 if (ID1 && A1) RegisterPtr(ID1,P1);
235 P2 = (ID2) ? SerializeTrait<T2>::Create(*this) : NULL;
236 if (ID2 && A2) RegisterPtr(ID2,P2);
238 P3 = (ID3) ? SerializeTrait<T3>::Create(*this) : NULL;
239 if (ID3 && A3) RegisterPtr(ID3,P3);
242 template <typename T1, typename T2, typename T3, typename Arg1>
243 void BatchReadOwnedPtrs(T1*& P1, T2*& P2, T3*& P3, Arg1& arg1,
244 bool A1=true, bool A2=true, bool A3=true) {
246 SerializedPtrID ID1 = ReadPtrID();
247 SerializedPtrID ID2 = ReadPtrID();
248 SerializedPtrID ID3 = ReadPtrID();
250 P1 = (ID1) ? SerializeTrait<T1>::Create(*this, arg1) : NULL;
251 if (ID1 && A1) RegisterPtr(ID1,P1);
253 P2 = (ID2) ? SerializeTrait<T2>::Create(*this, arg1) : NULL;
254 if (ID2 && A2) RegisterPtr(ID2,P2);
256 P3 = (ID3) ? SerializeTrait<T3>::Create(*this, arg1) : NULL;
257 if (ID3 && A3) RegisterPtr(ID3,P3);
260 template <typename T>
261 void BatchReadOwnedPtrs(unsigned NumPtrs, T** Ptrs, bool AutoRegister=true) {
262 llvm::SmallVector<SerializedPtrID,10> BatchIDVec;
264 for (unsigned i = 0; i < NumPtrs; ++i)
265 BatchIDVec.push_back(ReadPtrID());
267 for (unsigned i = 0; i < NumPtrs; ++i) {
268 SerializedPtrID& PtrID = BatchIDVec[i];
270 T* p = PtrID ? SerializeTrait<T>::Create(*this) : NULL;
272 if (PtrID && AutoRegister)
273 RegisterPtr(PtrID,p);
279 template <typename T, typename Arg1>
280 void BatchReadOwnedPtrs(unsigned NumPtrs, T** Ptrs, Arg1& arg1,
281 bool AutoRegister=true) {
283 llvm::SmallVector<SerializedPtrID,10> BatchIDVec;
285 for (unsigned i = 0; i < NumPtrs; ++i)
286 BatchIDVec.push_back(ReadPtrID());
288 for (unsigned i = 0; i < NumPtrs; ++i) {
289 SerializedPtrID& PtrID = BatchIDVec[i];
291 T* p = PtrID ? SerializeTrait<T>::Create(*this, arg1) : NULL;
293 if (PtrID && AutoRegister)
294 RegisterPtr(PtrID,p);
300 template <typename T1, typename T2>
301 void BatchReadOwnedPtrs(unsigned NumT1Ptrs, T1** Ptrs, T2*& P2,
302 bool A1=true, bool A2=true) {
304 llvm::SmallVector<SerializedPtrID,10> BatchIDVec;
306 for (unsigned i = 0; i < NumT1Ptrs; ++i)
307 BatchIDVec.push_back(ReadPtrID());
309 SerializedPtrID ID2 = ReadPtrID();
311 for (unsigned i = 0; i < NumT1Ptrs; ++i) {
312 SerializedPtrID& PtrID = BatchIDVec[i];
314 T1* p = PtrID ? SerializeTrait<T1>::Create(*this) : NULL;
317 RegisterPtr(PtrID,p);
322 P2 = (ID2) ? SerializeTrait<T2>::Create(*this) : NULL;
323 if (ID2 && A2) RegisterPtr(ID2,P2);
326 template <typename T1, typename T2, typename Arg1>
327 void BatchReadOwnedPtrs(unsigned NumT1Ptrs, T1** Ptrs, T2*& P2, Arg1& arg1,
328 bool A1=true, bool A2=true) {
330 llvm::SmallVector<SerializedPtrID,10> BatchIDVec;
332 for (unsigned i = 0; i < NumT1Ptrs; ++i)
333 BatchIDVec.push_back(ReadPtrID());
335 SerializedPtrID ID2 = ReadPtrID();
337 for (unsigned i = 0; i < NumT1Ptrs; ++i) {
338 SerializedPtrID& PtrID = BatchIDVec[i];
340 T1* p = PtrID ? SerializeTrait<T1>::Create(*this, arg1) : NULL;
343 RegisterPtr(PtrID,p);
348 P2 = (ID2) ? SerializeTrait<T2>::Create(*this, arg1) : NULL;
349 if (ID2 && A2) RegisterPtr(ID2,P2);
352 template <typename T1, typename T2, typename T3>
353 void BatchReadOwnedPtrs(unsigned NumT1Ptrs, T1** Ptrs,
355 bool A1=true, bool A2=true, bool A3=true) {
357 llvm::SmallVector<SerializedPtrID,10> BatchIDVec;
359 for (unsigned i = 0; i < NumT1Ptrs; ++i)
360 BatchIDVec.push_back(ReadPtrID());
362 SerializedPtrID ID2 = ReadPtrID();
363 SerializedPtrID ID3 = ReadPtrID();
365 for (unsigned i = 0; i < NumT1Ptrs; ++i) {
366 SerializedPtrID& PtrID = BatchIDVec[i];
368 T1* p = PtrID ? SerializeTrait<T1>::Create(*this) : NULL;
371 RegisterPtr(PtrID,p);
376 P2 = (ID2) ? SerializeTrait<T2>::Create(*this) : NULL;
377 if (ID2 && A2) RegisterPtr(ID2,P2);
379 P3 = (ID3) ? SerializeTrait<T3>::Create(*this) : NULL;
380 if (ID3 && A3) RegisterPtr(ID3,P3);
383 template <typename T1, typename T2, typename T3, typename Arg1>
384 void BatchReadOwnedPtrs(unsigned NumT1Ptrs, T1** Ptrs,
385 T2*& P2, T3*& P3, Arg1& arg1,
386 bool A1=true, bool A2=true, bool A3=true) {
388 llvm::SmallVector<SerializedPtrID,10> BatchIDVec;
390 for (unsigned i = 0; i < NumT1Ptrs; ++i)
391 BatchIDVec.push_back(ReadPtrID());
393 SerializedPtrID ID2 = ReadPtrID();
394 SerializedPtrID ID3 = ReadPtrID();
396 for (unsigned i = 0; i < NumT1Ptrs; ++i) {
397 SerializedPtrID& PtrID = BatchIDVec[i];
399 T1* p = PtrID ? SerializeTrait<T1>::Create(*this, arg1) : NULL;
402 RegisterPtr(PtrID,p);
407 P2 = (ID2) ? SerializeTrait<T2>::Create(*this, arg1) : NULL;
408 if (ID2 && A2) RegisterPtr(ID2,P2);
410 P3 = (ID3) ? SerializeTrait<T3>::Create(*this, arg1) : NULL;
411 if (ID3 && A3) RegisterPtr(ID3,P3);
414 template <typename T>
415 void ReadPtr(T*& PtrRef, bool AllowBackpatch = true) {
416 ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef), AllowBackpatch);
419 template <typename T>
420 void ReadPtr(const T*& PtrRef, bool AllowBackpatch = true) {
421 ReadPtr(const_cast<T*&>(PtrRef), AllowBackpatch);
425 template <typename T>
426 void ReadPtr(T*& PtrRef, const SerializedPtrID& PtrID,
427 bool AllowBackpatch = true) {
428 ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef), PtrID, AllowBackpatch);
431 template <typename T>
432 void ReadPtr(const T*& PtrRef, const SerializedPtrID& PtrID,
433 bool AllowBackpatch = true) {
435 ReadPtr(const_cast<T*&>(PtrRef), PtrID, AllowBackpatch);
438 template <typename T>
439 T* ReadPtr() { T* x = 0; ReadPtr<T>(x,false); return x; }
441 void ReadUIntPtr(uintptr_t& PtrRef, const SerializedPtrID& PtrID,
442 bool AllowBackpatch = true);
444 void ReadUIntPtr(uintptr_t& PtrRef, bool AllowBackpatch = true) {
445 ReadUIntPtr(PtrRef,ReadPtrID(),AllowBackpatch);
448 template <typename T>
450 T* p = reinterpret_cast<T*>(ReadInternalRefPtr());
454 void RegisterPtr(const SerializedPtrID& PtrID, const void* Ptr);
456 void RegisterPtr(const void* Ptr) {
457 RegisterPtr(ReadPtrID(),Ptr);
461 void RegisterRef(const T& x) {
466 void RegisterRef(const SerializedPtrID& PtrID, const T& x) {
467 RegisterPtr(PtrID,&x);
470 Location getCurrentBlockLocation();
471 unsigned getCurrentBlockID();
472 unsigned getAbbrevNo();
474 bool FinishedBlock(Location BlockLoc);
475 bool JumpTo(const Location& BlockLoc);
481 bool SkipToBlock(unsigned BlockID);
483 unsigned getRecordCode();
485 BitstreamReader& getStream() { return Stream; }
488 bool AdvanceStream();
491 uintptr_t ReadInternalRefPtr();
493 static inline bool HasFinalPtr(MapTy::value_type& V) {
494 return V.first.hasFinalPtr();
497 static inline uintptr_t GetFinalPtr(MapTy::value_type& V) {
498 return reinterpret_cast<uintptr_t>(V.second.Ptr);
501 static inline BPNode* GetBPNode(MapTy::value_type& V) {
502 return V.second.Head;
505 static inline void SetBPNode(MapTy::value_type& V, BPNode* N) {
509 void SetPtr(MapTy::value_type& V, const void* P) {
511 V.second.SetPtr(FreeList,const_cast<void*>(P));
515 } // end namespace llvm