c98b4e810a5bcd9e7718b5da37d485b8a5349b72
[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   typedef uint64_t Location;
84   
85   //===----------------------------------------------------------===//
86   // Internal data members.
87   //===----------------------------------------------------------===//
88
89 private:
90   BitstreamReader& Stream;
91   SmallVector<uint64_t,10> Record;
92   unsigned RecIdx;
93   BumpPtrAllocator Allocator;
94   BPNode* FreeList;
95   MapTy BPatchMap;
96   llvm::SmallVector<uint64_t,5> BlockLocs;
97   
98   //===----------------------------------------------------------===//
99   // Public Interface.
100   //===----------------------------------------------------------===//
101   
102 public:  
103   Deserializer(BitstreamReader& stream);
104   ~Deserializer();
105
106   uint64_t ReadInt();
107   int64_t ReadSInt();
108   SerializedPtrID ReadPtrID() { return (SerializedPtrID) ReadInt(); }
109   
110   
111   bool ReadBool() {
112     return ReadInt() ? true : false;
113   }
114
115   template <typename T>
116   inline T& Read(T& X) {
117     SerializeTrait<T>::Read(*this,X);
118     return X;
119   }
120
121   template <typename T>
122   inline T* Materialize() {
123     return SerializeTrait<T>::Materialize(*this);
124   }
125   
126   char* ReadCStr(char* cstr = NULL, unsigned MaxLen=0, bool isNullTerm=true);
127   void ReadCStr(std::vector<char>& buff, bool isNullTerm=false);
128
129   template <typename T>
130   inline T* ReadOwnedPtr(bool AutoRegister = true) {
131     SerializedPtrID PtrID = ReadPtrID();    
132
133     if (!PtrID)
134       return NULL;
135     
136     T* x = SerializeTrait<T>::Materialize(*this);
137
138     if (AutoRegister)
139       RegisterPtr(PtrID,x);
140     
141     return x;
142   }
143   
144   template <typename T>
145   inline void ReadOwnedPtr(T*& Ptr, bool AutoRegister = true) {
146     Ptr = ReadOwnedPtr<T>(AutoRegister);
147   }
148   
149   template <typename T1, typename T2>
150   void BatchReadOwnedPtrs(T1*& P1, T2*& P2,
151                           bool A1=true, bool A2=true) {
152
153     SerializedPtrID ID1 = ReadPtrID();
154     SerializedPtrID ID2 = ReadPtrID();
155
156     P1 = (ID1) ? SerializeTrait<T1>::Materialize(*this) : NULL;
157     if (ID1 && A1) RegisterPtr(ID1,P1);
158
159     P2 = (ID2) ? SerializeTrait<T2>::Materialize(*this) : NULL;
160     if (ID2 && A2) RegisterPtr(ID2,P2);
161   }
162
163   template <typename T1, typename T2, typename T3>
164   void BatchReadOwnedPtrs(T1*& P1, T2*& P2, T3*& P3,
165                           bool A1=true, bool A2=true, bool A3=true) {
166     
167     SerializedPtrID ID1 = ReadPtrID();
168     SerializedPtrID ID2 = ReadPtrID();
169     SerializedPtrID ID3 = ReadPtrID();
170     
171     P1 = (ID1) ? SerializeTrait<T1>::Materialize(*this) : NULL;
172     if (ID1 && A1) RegisterPtr(ID1,P1);    
173     
174     P2 = (ID2) ? SerializeTrait<T2>::Materialize(*this) : NULL;
175     if (ID2 && A2) RegisterPtr(ID2,P2);
176     
177     P3 = (ID3) ? SerializeTrait<T2>::Materialize(*this) : NULL;
178     if (ID3 && A3) RegisterPtr(ID3,P3);
179   }
180   
181   template <typename T>
182   void BatchReadOwnedPtrs(unsigned NumPtrs, T** Ptrs, bool AutoRegister=true) {
183     for (unsigned i = 0; i < NumPtrs; ++i)
184       reinterpret_cast<SerializedPtrID&>(Ptrs[i]) = ReadPtrID();
185     
186     for (unsigned i = 0; i < NumPtrs; ++i) {
187       SerializedPtrID PtrID = reinterpret_cast<SerializedPtrID>(Ptrs[i]);
188       T* p = PtrID ? SerializeTrait<T>::Materialize(*this) : NULL;
189       
190       if (PtrID && AutoRegister)
191         RegisterPtr(PtrID,p);
192       
193       Ptrs[i] = p;
194     }
195   }    
196   
197   template <typename T>
198   void ReadPtr(T*& PtrRef, bool AllowBackpatch = true) {
199     ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef), AllowBackpatch);
200   }
201   
202   template <typename T>
203   void ReadPtr(const T*& PtrRef, bool AllowBackpatch = true) {
204     ReadPtr(const_cast<T*&>(PtrRef), AllowBackpatch);
205   }
206   
207   template <typename T>
208   T* ReadPtr() { T* x; ReadPtr<T>(x,false); return x; }
209
210   void ReadUIntPtr(uintptr_t& PtrRef, bool AllowBackpatch = true);
211   
212   template <typename T>
213   T& ReadRef() {
214     T* p = reinterpret_cast<T*>(ReadInternalRefPtr());
215     return *p;
216   }
217
218   void RegisterPtr(SerializedPtrID PtrId, const void* Ptr);
219   
220   void RegisterPtr(const void* Ptr) {
221     RegisterPtr(ReadPtrID(),Ptr);
222   }
223   
224   template<typename T>
225   void RegisterRef(const T& x) {
226     RegisterPtr(&x);
227   }
228   
229   template<typename T>
230   void RegisterRef(SerializedPtrID PtrID, const T& x) {
231     RegisterPtr(PtrID,&x);
232   }  
233   
234   Location GetCurrentBlockLocation();
235   bool FinishedBlock(Location BlockLoc);
236   
237   bool AtEnd();
238   bool inRecord();
239   
240 private:
241   void ReadRecord();  
242   uintptr_t ReadInternalRefPtr();
243   
244   static inline bool HasFinalPtr(MapTy::value_type& V) {
245     return V.first.hasFinalPtr();
246   }
247   
248   static inline uintptr_t GetFinalPtr(MapTy::value_type& V) {
249     return reinterpret_cast<uintptr_t>(V.second.Ptr);
250   }
251   
252   static inline BPNode* GetBPNode(MapTy::value_type& V) {
253     return V.second.Head;
254   }
255     
256   static inline void SetBPNode(MapTy::value_type& V, BPNode* N) {
257     V.second.Head = N;
258   }
259   
260   void SetPtr(MapTy::value_type& V, const void* P) {
261     V.first.MarkFinal();
262     V.second.SetPtr(FreeList,const_cast<void*>(P));
263   }
264 };
265     
266 } // end namespace llvm
267
268 #endif