ef25da71409e24f4fb3280f4a00ed126f2633c36
[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   Location 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   
141   SerializedPtrID ReadPtrID() { return (SerializedPtrID) ReadInt(); }
142
143   SerializedPtrID ReadDiffPtrID(SerializedPtrID& PrevID) {
144     bool x = ReadBool();    
145     return (SerializedPtrID) (x ? (PrevID+1) : 0);
146   }
147   
148   
149   bool ReadBool() {
150     return ReadInt() ? true : false;
151   }
152
153   template <typename T>
154   inline T& Read(T& X) {
155     SerializeTrait<T>::Read(*this,X);
156     return X;
157   }
158
159   template <typename T>
160   inline T* Create() {
161     return SerializeTrait<T>::Create(*this);
162   }
163   
164   char* ReadCStr(char* cstr = NULL, unsigned MaxLen=0, bool isNullTerm=true);
165   void ReadCStr(std::vector<char>& buff, bool isNullTerm=false);
166
167   template <typename T>
168   inline T* ReadOwnedPtr(bool AutoRegister = true) {
169     SerializedPtrID PtrID = ReadPtrID();    
170
171     if (!PtrID)
172       return NULL;
173     
174     T* x = SerializeTrait<T>::Create(*this);
175
176     if (AutoRegister)
177       RegisterPtr(PtrID,x);
178     
179     return x;
180   }
181   
182   template <typename T>
183   inline void ReadOwnedPtr(T*& Ptr, bool AutoRegister = true) {
184     Ptr = ReadOwnedPtr<T>(AutoRegister);
185   }
186   
187   template <typename T1, typename T2>
188   void BatchReadOwnedPtrs(T1*& P1, T2*& P2,
189                           bool A1=true, bool A2=true) {
190
191     SerializedPtrID ID1 = ReadPtrID();
192     SerializedPtrID ID2 = ReadDiffPtrID(ID2);
193
194     P1 = (ID1) ? SerializeTrait<T1>::Create(*this) : NULL;
195     if (ID1 && A1) RegisterPtr(ID1,P1);
196
197     P2 = (ID2) ? SerializeTrait<T2>::Create(*this) : NULL;
198     if (ID2 && A2) RegisterPtr(ID2,P2);
199   }
200
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) {
204     
205     SerializedPtrID ID1 = ReadPtrID();
206     SerializedPtrID ID2 = ReadDiffPtrID(ID1);
207     SerializedPtrID ID3 = ReadDiffPtrID(ID2);
208     
209     P1 = (ID1) ? SerializeTrait<T1>::Create(*this) : NULL;
210     if (ID1 && A1) RegisterPtr(ID1,P1);    
211     
212     P2 = (ID2) ? SerializeTrait<T2>::Create(*this) : NULL;
213     if (ID2 && A2) RegisterPtr(ID2,P2);
214     
215     P3 = (ID3) ? SerializeTrait<T3>::Create(*this) : NULL;
216     if (ID3 && A3) RegisterPtr(ID3,P3);
217   }
218   
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) {
222     
223     SerializedPtrID ID1 = ReadPtrID();
224     SerializedPtrID ID2 = ReadDiffPtrID(ID1);
225     SerializedPtrID ID3 = ReadDiffPtrID(ID2);
226     SerializedPtrID ID4 = ReadDiffPtrID(ID3);
227     
228     P1 = (ID1) ? SerializeTrait<T1>::Create(*this) : NULL;
229     if (ID1 && A1) RegisterPtr(ID1,P1);    
230     
231     P2 = (ID2) ? SerializeTrait<T2>::Create(*this) : NULL;
232     if (ID2 && A2) RegisterPtr(ID2,P2);
233     
234     P3 = (ID3) ? SerializeTrait<T3>::Create(*this) : NULL;
235     if (ID3 && A3) RegisterPtr(ID3,P3);
236     
237     P4 = (ID4) ? SerializeTrait<T4>::Create(*this) : NULL;
238     if (ID4 && A4) RegisterPtr(ID4,P4);
239   }
240   
241   template <typename T>
242   void BatchReadOwnedPtrs(unsigned NumPtrs, T** Ptrs, bool AutoRegister=true) {
243     llvm::SmallVector<SerializedPtrID,10> BatchIDVec;
244     SerializedPtrID TempPtrID;
245     
246     for (unsigned i = 0; i < NumPtrs; ++i) {
247       TempPtrID = i ? ReadDiffPtrID(TempPtrID) : ReadPtrID();
248       BatchIDVec.push_back(TempPtrID);
249     }
250     
251     for (unsigned i = 0; i < NumPtrs; ++i) {
252       SerializedPtrID& PtrID = BatchIDVec[i];
253       
254       T* p = PtrID ? SerializeTrait<T>::Create(*this) : NULL;
255       
256       if (PtrID && AutoRegister)
257         RegisterPtr(PtrID,p);
258       
259       Ptrs[i] = p;
260     }
261   }
262   
263   template <typename T1, typename T2>
264   void BatchReadOwnedPtrs(unsigned NumT1Ptrs, T1** Ptrs, T2*& P2,
265                           bool A1=true, bool A2=true) {
266
267     SerializedPtrID ID2 = ReadPtrID();
268     SerializedPtrID TempID = ID2;
269
270     llvm::SmallVector<SerializedPtrID,10> BatchIDVec;
271     
272     for (unsigned i = 0; i < NumT1Ptrs; ++i) {
273       TempID = ReadDiffPtrID(TempID);
274       BatchIDVec.push_back(TempID);
275     }
276     
277     P2 = (ID2) ? SerializeTrait<T2>::Create(*this) : NULL;
278     if (ID2 && A2) RegisterPtr(ID2,P2);    
279     
280     for (unsigned i = 0; i < NumT1Ptrs; ++i) {
281       SerializedPtrID& PtrID = BatchIDVec[i];
282       
283       T1* p = PtrID ? SerializeTrait<T1>::Create(*this) : NULL;
284       
285       if (PtrID && A1)
286         RegisterPtr(PtrID,p);
287       
288       Ptrs[i] = p;
289     }    
290   }    
291   
292   template <typename T1, typename T2, typename T3>
293   void BatchReadOwnedPtrs(unsigned NumT1Ptrs, T1** Ptrs, 
294                           T2*& P2, T3*& P3,
295                           bool A1=true, bool A2=true, bool A3=true) {
296
297     SerializedPtrID ID2 = ReadPtrID();
298     SerializedPtrID ID3 = ReadDiffPtrID(ID2);  
299     
300     SerializedPtrID TempID = ID3;
301     
302     llvm::SmallVector<SerializedPtrID,10> BatchIDVec;
303     
304     for (unsigned i = 0; i < NumT1Ptrs; ++i) {
305       TempID = ReadDiffPtrID(TempID);
306       BatchIDVec.push_back(TempID);
307     }
308     
309     P2 = (ID2) ? SerializeTrait<T2>::Create(*this) : NULL;
310     if (ID2 && A2) RegisterPtr(ID2,P2);
311     
312     P3 = (ID3) ? SerializeTrait<T3>::Create(*this) : NULL;
313     if (ID3 && A3) RegisterPtr(ID3,P3);  
314     
315     for (unsigned i = 0; i < NumT1Ptrs; ++i) {
316       SerializedPtrID& PtrID = BatchIDVec[i];
317       
318       T1* p = PtrID ? SerializeTrait<T1>::Create(*this) : NULL;
319       
320       if (PtrID && A1)
321         RegisterPtr(PtrID,p);
322       
323       Ptrs[i] = p;
324     }
325   }    
326   
327   template <typename T>
328   void ReadPtr(T*& PtrRef, bool AllowBackpatch = true) {
329     ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef), AllowBackpatch);
330   }
331   
332   template <typename T>
333   void ReadPtr(const T*& PtrRef, bool AllowBackpatch = true) {
334     ReadPtr(const_cast<T*&>(PtrRef), AllowBackpatch);
335   }
336   
337   
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);
342   }
343   
344   template <typename T>
345   void ReadPtr(const T*& PtrRef, const SerializedPtrID& PtrID, 
346                bool AllowBackpatch = true) {
347     
348     ReadPtr(const_cast<T*&>(PtrRef), PtrID, AllowBackpatch);
349   }
350   
351   template <typename T>
352   T* ReadPtr() { T* x; ReadPtr<T>(x,false); return x; }
353
354   void ReadUIntPtr(uintptr_t& PtrRef, const SerializedPtrID& PtrID, 
355                    bool AllowBackpatch = true);
356   
357   void ReadUIntPtr(uintptr_t& PtrRef, bool AllowBackpatch = true) {
358     ReadUIntPtr(PtrRef,ReadPtrID(),AllowBackpatch);
359   }
360   
361   template <typename T>
362   T& ReadRef() {
363     T* p = reinterpret_cast<T*>(ReadInternalRefPtr());
364     return *p;
365   }
366
367   void RegisterPtr(const SerializedPtrID& PtrID, const void* Ptr);
368   
369   void RegisterPtr(const void* Ptr) {
370     RegisterPtr(ReadPtrID(),Ptr);
371   }
372   
373   template<typename T>
374   void RegisterRef(const T& x) {
375     RegisterPtr(&x);
376   }
377   
378   template<typename T>
379   void RegisterRef(const SerializedPtrID& PtrID, const T& x) {
380     RegisterPtr(PtrID,&x);
381   }  
382   
383   Location getCurrentBlockLocation();
384   unsigned getCurrentBlockID();
385   unsigned getAbbrevNo();
386   
387   bool FinishedBlock(Location BlockLoc);
388   bool JumpTo(const Location& BlockLoc);
389   void Rewind() { JumpTo(StreamStart); }
390   
391   bool AtEnd();
392   bool inRecord();
393   void SkipBlock();
394   bool SkipToBlock(unsigned BlockID);
395   
396   unsigned getRecordCode();
397   
398   BitstreamReader& getStream() { return Stream; }
399   
400 private:
401   bool AdvanceStream();  
402   void ReadRecord();
403   
404   uintptr_t ReadInternalRefPtr();
405   
406   static inline bool HasFinalPtr(MapTy::value_type& V) {
407     return V.first.hasFinalPtr();
408   }
409   
410   static inline uintptr_t GetFinalPtr(MapTy::value_type& V) {
411     return reinterpret_cast<uintptr_t>(V.second.Ptr);
412   }
413   
414   static inline BPNode* GetBPNode(MapTy::value_type& V) {
415     return V.second.Head;
416   }
417     
418   static inline void SetBPNode(MapTy::value_type& V, BPNode* N) {
419     V.second.Head = N;
420   }
421   
422   void SetPtr(MapTy::value_type& V, const void* P) {
423     V.first.MarkFinal();
424     V.second.SetPtr(FreeList,const_cast<void*>(P));
425   }
426 };
427     
428 } // end namespace llvm
429
430 #endif