Fixed bug with inconsistent serialization/deserialization in matching
[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   std::vector<SerializedPtrID> BatchIDVec;
130   
131   //===----------------------------------------------------------===//
132   // Public Interface.
133   //===----------------------------------------------------------===//
134   
135 public:  
136   Deserializer(BitstreamReader& stream);
137   ~Deserializer();
138
139   uint64_t ReadInt();
140   int64_t ReadSInt();
141   SerializedPtrID ReadPtrID() { return (SerializedPtrID) ReadInt(); }
142   
143   
144   bool ReadBool() {
145     return ReadInt() ? true : false;
146   }
147
148   template <typename T>
149   inline T& Read(T& X) {
150     SerializeTrait<T>::Read(*this,X);
151     return X;
152   }
153
154   template <typename T>
155   inline T* Materialize() {
156     return SerializeTrait<T>::Materialize(*this);
157   }
158   
159   char* ReadCStr(char* cstr = NULL, unsigned MaxLen=0, bool isNullTerm=true);
160   void ReadCStr(std::vector<char>& buff, bool isNullTerm=false);
161
162   template <typename T>
163   inline T* ReadOwnedPtr(bool AutoRegister = true) {
164     SerializedPtrID PtrID = ReadPtrID();    
165
166     if (!PtrID)
167       return NULL;
168     
169     T* x = SerializeTrait<T>::Materialize(*this);
170
171     if (AutoRegister)
172       RegisterPtr(PtrID,x);
173     
174     return x;
175   }
176   
177   template <typename T>
178   inline void ReadOwnedPtr(T*& Ptr, bool AutoRegister = true) {
179     Ptr = ReadOwnedPtr<T>(AutoRegister);
180   }
181   
182   template <typename T1, typename T2>
183   void BatchReadOwnedPtrs(T1*& P1, T2*& P2,
184                           bool A1=true, bool A2=true) {
185
186     SerializedPtrID ID1 = ReadPtrID();
187     SerializedPtrID ID2 = ReadPtrID();
188
189     P1 = (ID1) ? SerializeTrait<T1>::Materialize(*this) : NULL;
190     if (ID1 && A1) RegisterPtr(ID1,P1);
191
192     P2 = (ID2) ? SerializeTrait<T2>::Materialize(*this) : NULL;
193     if (ID2 && A2) RegisterPtr(ID2,P2);
194   }
195
196   template <typename T1, typename T2, typename T3>
197   void BatchReadOwnedPtrs(T1*& P1, T2*& P2, T3*& P3,
198                           bool A1=true, bool A2=true, bool A3=true) {
199     
200     SerializedPtrID ID1 = ReadPtrID();
201     SerializedPtrID ID2 = ReadPtrID();
202     SerializedPtrID ID3 = ReadPtrID();
203     
204     P1 = (ID1) ? SerializeTrait<T1>::Materialize(*this) : NULL;
205     if (ID1 && A1) RegisterPtr(ID1,P1);    
206     
207     P2 = (ID2) ? SerializeTrait<T2>::Materialize(*this) : NULL;
208     if (ID2 && A2) RegisterPtr(ID2,P2);
209     
210     P3 = (ID3) ? SerializeTrait<T2>::Materialize(*this) : NULL;
211     if (ID3 && A3) RegisterPtr(ID3,P3);
212   }
213   
214   template <typename T>
215   void BatchReadOwnedPtrs(unsigned NumPtrs, T** Ptrs, bool AutoRegister=true) {
216     BatchIDVec.clear();
217     
218     for (unsigned i = 0; i < NumPtrs; ++i)
219       BatchIDVec.push_back(ReadPtrID());
220     
221     for (unsigned i = 0; i < NumPtrs; ++i) {
222       SerializedPtrID& PtrID = BatchIDVec[i];
223       
224       T* p = PtrID ? SerializeTrait<T>::Materialize(*this) : NULL;
225       
226       if (PtrID && AutoRegister)
227         RegisterPtr(PtrID,p);
228       
229       Ptrs[i] = p;
230     }
231   }
232   
233   template <typename T1, typename T2, typename T3>
234   void BatchReadOwnedPtrs(unsigned NumT1Ptrs, T1** Ptrs, 
235                           T2*& P2, T3*& P3,
236                           bool A1=true, bool A2=true, bool A3=true) {
237     
238     BatchIDVec.clear();
239     
240     for (unsigned i = 0; i < NumT1Ptrs; ++i)
241       BatchIDVec.push_back(ReadPtrID());
242     
243     SerializedPtrID ID2 = ReadPtrID();
244     SerializedPtrID ID3 = ReadPtrID();    
245     
246     for (unsigned i = 0; i < NumT1Ptrs; ++i) {
247       SerializedPtrID& PtrID = BatchIDVec[i];
248       
249       T1* p = PtrID ? SerializeTrait<T1>::Materialize(*this) : NULL;
250       
251       if (PtrID && A1)
252         RegisterPtr(PtrID,p);
253       
254       Ptrs[i] = p;
255     }
256     
257     P2 = (ID2) ? SerializeTrait<T2>::Materialize(*this) : NULL;
258     if (ID2 && A2) RegisterPtr(ID2,P2);
259     
260     P3 = (ID3) ? SerializeTrait<T3>::Materialize(*this) : NULL;
261     if (ID3 && A3) RegisterPtr(ID3,P3);    
262   }    
263   
264   template <typename T>
265   void ReadPtr(T*& PtrRef, bool AllowBackpatch = true) {
266     ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef), AllowBackpatch);
267   }
268   
269   template <typename T>
270   void ReadPtr(const T*& PtrRef, bool AllowBackpatch = true) {
271     ReadPtr(const_cast<T*&>(PtrRef), AllowBackpatch);
272   }
273   
274   
275   template <typename T>
276   void ReadPtr(T*& PtrRef, const SerializedPtrID& PtrID, bool AllowBackpatch = true) {
277     ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef), PtrID, AllowBackpatch);
278   }
279   
280   template <typename T>
281   void ReadPtr(const T*& PtrRef, const SerializedPtrID& PtrID, 
282                bool AllowBackpatch = true) {
283     
284     ReadPtr(const_cast<T*&>(PtrRef), PtrID, AllowBackpatch);
285   }
286   
287   template <typename T>
288   T* ReadPtr() { T* x; ReadPtr<T>(x,false); return x; }
289
290   void ReadUIntPtr(uintptr_t& PtrRef, const SerializedPtrID& PtrID, 
291                    bool AllowBackpatch = true);
292   
293   void ReadUIntPtr(uintptr_t& PtrRef, bool AllowBackpatch = true) {
294     ReadUIntPtr(PtrRef,ReadPtrID(),AllowBackpatch);
295   }
296   
297   template <typename T>
298   T& ReadRef() {
299     T* p = reinterpret_cast<T*>(ReadInternalRefPtr());
300     return *p;
301   }
302
303   void RegisterPtr(const SerializedPtrID& PtrID, const void* Ptr);
304   
305   void RegisterPtr(const void* Ptr) {
306     RegisterPtr(ReadPtrID(),Ptr);
307   }
308   
309   template<typename T>
310   void RegisterRef(const T& x) {
311     RegisterPtr(&x);
312   }
313   
314   template<typename T>
315   void RegisterRef(const SerializedPtrID& PtrID, const T& x) {
316     RegisterPtr(PtrID,&x);
317   }  
318   
319   Location getCurrentBlockLocation();
320   unsigned getCurrentBlockID();
321   unsigned getAbbrevNo();
322   
323   bool FinishedBlock(Location BlockLoc);
324   bool JumpTo(const Location& BlockLoc);
325   void Rewind() { JumpTo(StreamStart); }
326   
327   bool AtEnd();
328   bool inRecord();
329   void SkipBlock();
330   bool SkipToBlock(unsigned BlockID);
331   
332   unsigned getRecordCode();
333   
334   BitstreamReader& getStream() { return Stream; }
335   
336 private:
337   bool AdvanceStream();  
338   void ReadRecord();
339   
340   uintptr_t ReadInternalRefPtr();
341   
342   static inline bool HasFinalPtr(MapTy::value_type& V) {
343     return V.first.hasFinalPtr();
344   }
345   
346   static inline uintptr_t GetFinalPtr(MapTy::value_type& V) {
347     return reinterpret_cast<uintptr_t>(V.second.Ptr);
348   }
349   
350   static inline BPNode* GetBPNode(MapTy::value_type& V) {
351     return V.second.Head;
352   }
353     
354   static inline void SetBPNode(MapTy::value_type& V, BPNode* N) {
355     V.second.Head = N;
356   }
357   
358   void SetPtr(MapTy::value_type& V, const void* P) {
359     V.first.MarkFinal();
360     V.second.SetPtr(FreeList,const_cast<void*>(P));
361   }
362 };
363     
364 } // end namespace llvm
365
366 #endif