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