dc1cdc0898dcee919fd874c71670d40340677572
[oota-llvm.git] / include / llvm / Bitcode / Deserialize.h
1 //=- Deserialize.h - Generic Object Deserialization from Bitcode --*- C++ -*-=//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the interface for generic object deserialization from
11 // LLVM bitcode.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_BITCODE_SERIALIZE_INPUT
16 #define LLVM_BITCODE_SERIALIZE_INPUT
17
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"
24 #include <vector>
25
26 namespace llvm {
27   
28 class Deserializer {  
29
30   //===----------------------------------------------------------===//
31   // Internal type definitions.
32   //===----------------------------------------------------------===//
33   
34   struct BPNode {
35     BPNode* Next;
36     uintptr_t& PtrRef;
37     
38     BPNode(BPNode* n, uintptr_t& pref) 
39       : Next(n), PtrRef(pref) {
40         PtrRef = 0;
41       }
42   };
43   
44   struct BPEntry { 
45     union { BPNode* Head; void* Ptr; };
46     
47     BPEntry() : Head(NULL) {}
48     
49     static inline bool isPod() { return true; }
50     
51     void SetPtr(BPNode*& FreeList, void* P);    
52   };  
53   
54   class BPKey {
55     unsigned Raw;
56     
57   public:
58     BPKey(SerializedPtrID PtrId) : Raw(PtrId << 1) { assert (PtrId > 0); }
59     BPKey(unsigned code, unsigned) : Raw(code) {}
60     
61     void MarkFinal() { Raw |= 0x1; }
62     bool hasFinalPtr() const { return Raw & 0x1 ? true : false; }
63     SerializedPtrID getID() const { return Raw >> 1; }
64     
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; }
68
69     static bool isEqual(const BPKey& K1, const BPKey& K2) {
70       return (K1.Raw ^ K2.Raw) & ~0x1 ? false : true;
71     }
72     
73     static bool isPod() { return true; }
74   };
75   
76   typedef llvm::DenseMap<BPKey,BPEntry,BPKey,BPEntry> MapTy;
77
78   //===----------------------------------------------------------===//
79   // Publicly visible types.
80   //===----------------------------------------------------------===//
81   
82 public:  
83   struct Location {
84     uint64_t BitNo;
85     unsigned BlockID;
86     unsigned NumWords;
87     
88     Location(uint64_t bit, unsigned bid, unsigned words) 
89     : BitNo(bit), BlockID(bid), NumWords(words) {}
90     
91     Location() : BitNo(0), BlockID(0), NumWords(0) {}
92
93     Location& operator=(Location& RHS) {
94       BitNo = RHS.BitNo;
95       BlockID = RHS.BlockID;
96       NumWords = RHS.NumWords;
97       return *this;
98     }
99     
100     bool operator==(const Location& RHS) const { return BitNo == RHS.BitNo; }    
101     bool operator!=(const Location& RHS) const { return BitNo != RHS.BitNo; }
102     
103     bool contains(const Location& RHS) const {
104       if (RHS.BitNo < BitNo)
105         return false;
106
107       if ((RHS.BitNo - BitNo) >> 5 < NumWords)
108         return true;
109       
110       return false;
111     }
112   };
113   
114   //===----------------------------------------------------------===//
115   // Internal data members.
116   //===----------------------------------------------------------===//
117
118 private:
119   BitstreamReader& Stream;
120   SmallVector<uint64_t,20> Record;
121   unsigned RecIdx;
122   BumpPtrAllocator Allocator;
123   BPNode* FreeList;
124   MapTy BPatchMap;
125   llvm::SmallVector<Location,8> BlockStack;
126   unsigned AbbrevNo;
127   unsigned RecordCode;
128   uint64_t StreamStart;
129   
130   //===----------------------------------------------------------===//
131   // Public Interface.
132   //===----------------------------------------------------------===//
133   
134 public:  
135   Deserializer(BitstreamReader& stream);
136   ~Deserializer();
137
138   uint64_t ReadInt();
139   int64_t ReadSInt();
140   SerializedPtrID ReadPtrID() { return (SerializedPtrID) ReadInt(); }
141   
142   
143   bool ReadBool() {
144     return ReadInt() ? true : false;
145   }
146
147   template <typename T>
148   inline T& Read(T& X) {
149     SerializeTrait<T>::Read(*this,X);
150     return X;
151   }
152
153   template <typename T>
154   inline T* Create() {
155     return SerializeTrait<T>::Create(*this);
156   }
157   
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);
160
161   template <typename T>
162   inline T* ReadOwnedPtr(bool AutoRegister = true) {
163     SerializedPtrID PtrID = ReadPtrID();    
164
165     if (!PtrID)
166       return NULL;
167     
168     T* x = SerializeTrait<T>::Create(*this);
169
170     if (AutoRegister)
171       RegisterPtr(PtrID,x);
172     
173     return x;
174   }
175   
176   template <typename T>
177   inline void ReadOwnedPtr(T*& Ptr, bool AutoRegister = true) {
178     Ptr = ReadOwnedPtr<T>(AutoRegister);
179   }
180   
181   template <typename T1, typename T2>
182   void BatchReadOwnedPtrs(T1*& P1, T2*& P2,
183                           bool A1=true, bool A2=true) {
184
185     SerializedPtrID ID1 = ReadPtrID();
186     SerializedPtrID ID2 = ReadPtrID();
187
188     P1 = (ID1) ? SerializeTrait<T1>::Create(*this) : NULL;
189     if (ID1 && A1) RegisterPtr(ID1,P1);
190
191     P2 = (ID2) ? SerializeTrait<T2>::Create(*this) : NULL;
192     if (ID2 && A2) RegisterPtr(ID2,P2);
193   }
194
195   template <typename T1, typename T2, typename T3>
196   void BatchReadOwnedPtrs(T1*& P1, T2*& P2, T3*& P3,
197                           bool A1=true, bool A2=true, bool A3=true) {
198     
199     SerializedPtrID ID1 = ReadPtrID();
200     SerializedPtrID ID2 = ReadPtrID();
201     SerializedPtrID ID3 = ReadPtrID();
202     
203     P1 = (ID1) ? SerializeTrait<T1>::Create(*this) : NULL;
204     if (ID1 && A1) RegisterPtr(ID1,P1);    
205     
206     P2 = (ID2) ? SerializeTrait<T2>::Create(*this) : NULL;
207     if (ID2 && A2) RegisterPtr(ID2,P2);
208     
209     P3 = (ID3) ? SerializeTrait<T3>::Create(*this) : NULL;
210     if (ID3 && A3) RegisterPtr(ID3,P3);
211   }
212   
213   template <typename T>
214   void BatchReadOwnedPtrs(unsigned NumPtrs, T** Ptrs, bool AutoRegister=true) {
215     llvm::SmallVector<SerializedPtrID,10> BatchIDVec;
216     
217     for (unsigned i = 0; i < NumPtrs; ++i)
218       BatchIDVec.push_back(ReadPtrID());
219     
220     for (unsigned i = 0; i < NumPtrs; ++i) {
221       SerializedPtrID& PtrID = BatchIDVec[i];
222       
223       T* p = PtrID ? SerializeTrait<T>::Create(*this) : NULL;
224       
225       if (PtrID && AutoRegister)
226         RegisterPtr(PtrID,p);
227       
228       Ptrs[i] = p;
229     }
230   }
231   
232   template <typename T1, typename T2>
233   void BatchReadOwnedPtrs(unsigned NumT1Ptrs, T1** Ptrs, T2*& P2,
234                           bool A1=true, bool A2=true) {
235     
236     llvm::SmallVector<SerializedPtrID,10> BatchIDVec;
237
238     for (unsigned i = 0; i < NumT1Ptrs; ++i)
239       BatchIDVec.push_back(ReadPtrID());
240     
241     SerializedPtrID ID2 = ReadPtrID();
242     
243     for (unsigned i = 0; i < NumT1Ptrs; ++i) {
244       SerializedPtrID& PtrID = BatchIDVec[i];
245       
246       T1* p = PtrID ? SerializeTrait<T1>::Create(*this) : NULL;
247       
248       if (PtrID && A1)
249         RegisterPtr(PtrID,p);
250       
251       Ptrs[i] = p;
252     }
253     
254     P2 = (ID2) ? SerializeTrait<T2>::Create(*this) : NULL;
255     if (ID2 && A2) RegisterPtr(ID2,P2);    
256   }    
257   
258   template <typename T1, typename T2, typename T3>
259   void BatchReadOwnedPtrs(unsigned NumT1Ptrs, T1** Ptrs, 
260                           T2*& P2, T3*& P3,
261                           bool A1=true, bool A2=true, bool A3=true) {
262     
263     llvm::SmallVector<SerializedPtrID,10> BatchIDVec;
264     
265     for (unsigned i = 0; i < NumT1Ptrs; ++i)
266       BatchIDVec.push_back(ReadPtrID());
267     
268     SerializedPtrID ID2 = ReadPtrID();
269     SerializedPtrID ID3 = ReadPtrID();    
270     
271     for (unsigned i = 0; i < NumT1Ptrs; ++i) {
272       SerializedPtrID& PtrID = BatchIDVec[i];
273       
274       T1* p = PtrID ? SerializeTrait<T1>::Create(*this) : NULL;
275       
276       if (PtrID && A1)
277         RegisterPtr(PtrID,p);
278       
279       Ptrs[i] = p;
280     }
281     
282     P2 = (ID2) ? SerializeTrait<T2>::Create(*this) : NULL;
283     if (ID2 && A2) RegisterPtr(ID2,P2);
284     
285     P3 = (ID3) ? SerializeTrait<T3>::Create(*this) : NULL;
286     if (ID3 && A3) RegisterPtr(ID3,P3);    
287   }    
288   
289   template <typename T>
290   void ReadPtr(T*& PtrRef, bool AllowBackpatch = true) {
291     ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef), AllowBackpatch);
292   }
293   
294   template <typename T>
295   void ReadPtr(const T*& PtrRef, bool AllowBackpatch = true) {
296     ReadPtr(const_cast<T*&>(PtrRef), AllowBackpatch);
297   }
298   
299   
300   template <typename T>
301   void ReadPtr(T*& PtrRef, const SerializedPtrID& PtrID, 
302                bool AllowBackpatch = true) {
303     ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef), PtrID, AllowBackpatch);
304   }
305   
306   template <typename T>
307   void ReadPtr(const T*& PtrRef, const SerializedPtrID& PtrID, 
308                bool AllowBackpatch = true) {
309     
310     ReadPtr(const_cast<T*&>(PtrRef), PtrID, AllowBackpatch);
311   }
312   
313   template <typename T>
314   T* ReadPtr() { T* x; ReadPtr<T>(x,false); return x; }
315
316   void ReadUIntPtr(uintptr_t& PtrRef, const SerializedPtrID& PtrID, 
317                    bool AllowBackpatch = true);
318   
319   void ReadUIntPtr(uintptr_t& PtrRef, bool AllowBackpatch = true) {
320     ReadUIntPtr(PtrRef,ReadPtrID(),AllowBackpatch);
321   }
322   
323   template <typename T>
324   T& ReadRef() {
325     T* p = reinterpret_cast<T*>(ReadInternalRefPtr());
326     return *p;
327   }
328
329   void RegisterPtr(const SerializedPtrID& PtrID, const void* Ptr);
330   
331   void RegisterPtr(const void* Ptr) {
332     RegisterPtr(ReadPtrID(),Ptr);
333   }
334   
335   template<typename T>
336   void RegisterRef(const T& x) {
337     RegisterPtr(&x);
338   }
339   
340   template<typename T>
341   void RegisterRef(const SerializedPtrID& PtrID, const T& x) {
342     RegisterPtr(PtrID,&x);
343   }  
344   
345   Location getCurrentBlockLocation();
346   unsigned getCurrentBlockID();
347   unsigned getAbbrevNo();
348   
349   bool FinishedBlock(Location BlockLoc);
350   bool JumpTo(const Location& BlockLoc);
351   void Rewind();
352   
353   bool AtEnd();
354   bool inRecord();
355   void SkipBlock();
356   bool SkipToBlock(unsigned BlockID);
357   
358   unsigned getRecordCode();
359   
360   BitstreamReader& getStream() { return Stream; }
361   
362 private:
363   bool AdvanceStream();  
364   void ReadRecord();
365   
366   uintptr_t ReadInternalRefPtr();
367   
368   static inline bool HasFinalPtr(MapTy::value_type& V) {
369     return V.first.hasFinalPtr();
370   }
371   
372   static inline uintptr_t GetFinalPtr(MapTy::value_type& V) {
373     return reinterpret_cast<uintptr_t>(V.second.Ptr);
374   }
375   
376   static inline BPNode* GetBPNode(MapTy::value_type& V) {
377     return V.second.Head;
378   }
379     
380   static inline void SetBPNode(MapTy::value_type& V, BPNode* N) {
381     V.second.Head = N;
382   }
383   
384   void SetPtr(MapTy::value_type& V, const void* P) {
385     V.first.MarkFinal();
386     V.second.SetPtr(FreeList,const_cast<void*>(P));
387   }
388 };
389     
390 } // end namespace llvm
391
392 #endif