3e90227591a5cdb38df1c21d54ac94de2312ec52
[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 is distributed under the University of Illinois Open Source
6 // 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   BitstreamCursor 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, typename Arg1>
177   inline T* ReadOwnedPtr(Arg1& arg1, bool AutoRegister = true) {
178     SerializedPtrID PtrID = ReadPtrID();
179
180     if (!PtrID)
181       return NULL;
182
183     T* x = SerializeTrait<T>::Create(*this, arg1);
184
185     if (AutoRegister)
186       RegisterPtr(PtrID,x);
187
188     return x;
189   }
190
191   template <typename T>
192   inline void ReadOwnedPtr(T*& Ptr, bool AutoRegister = true) {
193     Ptr = ReadOwnedPtr<T>(AutoRegister);
194   }
195
196   template <typename T1, typename T2>
197   void BatchReadOwnedPtrs(T1*& P1, T2*& P2,
198                           bool A1=true, bool A2=true) {
199
200     SerializedPtrID ID1 = ReadPtrID();
201     SerializedPtrID ID2 = 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
210   template <typename T1, typename T2, typename Arg1>
211   void BatchReadOwnedPtrs(T1*& P1, T2*& P2, Arg1& arg1,
212                           bool A1=true, bool A2=true) {
213
214     SerializedPtrID ID1 = ReadPtrID();
215     SerializedPtrID ID2 = ReadPtrID();
216
217     P1 = (ID1) ? SerializeTrait<T1>::Create(*this, arg1) : NULL;
218     if (ID1 && A1) RegisterPtr(ID1,P1);
219
220     P2 = (ID2) ? SerializeTrait<T2>::Create(*this, arg1) : NULL;
221     if (ID2 && A2) RegisterPtr(ID2,P2);
222   }
223
224   template <typename T1, typename T2, typename T3>
225   void BatchReadOwnedPtrs(T1*& P1, T2*& P2, T3*& P3,
226                           bool A1=true, bool A2=true, bool A3=true) {
227
228     SerializedPtrID ID1 = ReadPtrID();
229     SerializedPtrID ID2 = ReadPtrID();
230     SerializedPtrID ID3 = ReadPtrID();
231
232     P1 = (ID1) ? SerializeTrait<T1>::Create(*this) : NULL;
233     if (ID1 && A1) RegisterPtr(ID1,P1);
234
235     P2 = (ID2) ? SerializeTrait<T2>::Create(*this) : NULL;
236     if (ID2 && A2) RegisterPtr(ID2,P2);
237
238     P3 = (ID3) ? SerializeTrait<T3>::Create(*this) : NULL;
239     if (ID3 && A3) RegisterPtr(ID3,P3);
240   }
241
242   template <typename T1, typename T2, typename T3, typename Arg1>
243   void BatchReadOwnedPtrs(T1*& P1, T2*& P2, T3*& P3, Arg1& arg1,
244                           bool A1=true, bool A2=true, bool A3=true) {
245
246     SerializedPtrID ID1 = ReadPtrID();
247     SerializedPtrID ID2 = ReadPtrID();
248     SerializedPtrID ID3 = ReadPtrID();
249
250     P1 = (ID1) ? SerializeTrait<T1>::Create(*this, arg1) : NULL;
251     if (ID1 && A1) RegisterPtr(ID1,P1);
252
253     P2 = (ID2) ? SerializeTrait<T2>::Create(*this, arg1) : NULL;
254     if (ID2 && A2) RegisterPtr(ID2,P2);
255
256     P3 = (ID3) ? SerializeTrait<T3>::Create(*this, arg1) : NULL;
257     if (ID3 && A3) RegisterPtr(ID3,P3);
258   }
259
260   template <typename T>
261   void BatchReadOwnedPtrs(unsigned NumPtrs, T** Ptrs, bool AutoRegister=true) {
262     llvm::SmallVector<SerializedPtrID,10> BatchIDVec;
263
264     for (unsigned i = 0; i < NumPtrs; ++i)
265       BatchIDVec.push_back(ReadPtrID());
266
267     for (unsigned i = 0; i < NumPtrs; ++i) {
268       SerializedPtrID& PtrID = BatchIDVec[i];
269
270       T* p = PtrID ? SerializeTrait<T>::Create(*this) : NULL;
271
272       if (PtrID && AutoRegister)
273         RegisterPtr(PtrID,p);
274
275       Ptrs[i] = p;
276     }
277   }
278
279   template <typename T, typename Arg1>
280   void BatchReadOwnedPtrs(unsigned NumPtrs, T** Ptrs, Arg1& arg1,
281                           bool AutoRegister=true) {
282
283     llvm::SmallVector<SerializedPtrID,10> BatchIDVec;
284
285     for (unsigned i = 0; i < NumPtrs; ++i)
286       BatchIDVec.push_back(ReadPtrID());
287
288     for (unsigned i = 0; i < NumPtrs; ++i) {
289       SerializedPtrID& PtrID = BatchIDVec[i];
290
291       T* p = PtrID ? SerializeTrait<T>::Create(*this, arg1) : NULL;
292
293       if (PtrID && AutoRegister)
294         RegisterPtr(PtrID,p);
295
296       Ptrs[i] = p;
297     }
298   }
299
300   template <typename T1, typename T2>
301   void BatchReadOwnedPtrs(unsigned NumT1Ptrs, T1** Ptrs, T2*& P2,
302                           bool A1=true, bool A2=true) {
303
304     llvm::SmallVector<SerializedPtrID,10> BatchIDVec;
305
306     for (unsigned i = 0; i < NumT1Ptrs; ++i)
307       BatchIDVec.push_back(ReadPtrID());
308
309     SerializedPtrID ID2 = ReadPtrID();
310
311     for (unsigned i = 0; i < NumT1Ptrs; ++i) {
312       SerializedPtrID& PtrID = BatchIDVec[i];
313
314       T1* p = PtrID ? SerializeTrait<T1>::Create(*this) : NULL;
315
316       if (PtrID && A1)
317         RegisterPtr(PtrID,p);
318
319       Ptrs[i] = p;
320     }
321
322     P2 = (ID2) ? SerializeTrait<T2>::Create(*this) : NULL;
323     if (ID2 && A2) RegisterPtr(ID2,P2);
324   }
325
326   template <typename T1, typename T2, typename Arg1>
327   void BatchReadOwnedPtrs(unsigned NumT1Ptrs, T1** Ptrs, T2*& P2, Arg1& arg1,
328                           bool A1=true, bool A2=true) {
329
330     llvm::SmallVector<SerializedPtrID,10> BatchIDVec;
331
332     for (unsigned i = 0; i < NumT1Ptrs; ++i)
333       BatchIDVec.push_back(ReadPtrID());
334
335     SerializedPtrID ID2 = ReadPtrID();
336
337     for (unsigned i = 0; i < NumT1Ptrs; ++i) {
338       SerializedPtrID& PtrID = BatchIDVec[i];
339
340       T1* p = PtrID ? SerializeTrait<T1>::Create(*this, arg1) : NULL;
341
342       if (PtrID && A1)
343         RegisterPtr(PtrID,p);
344
345       Ptrs[i] = p;
346     }
347
348     P2 = (ID2) ? SerializeTrait<T2>::Create(*this, arg1) : NULL;
349     if (ID2 && A2) RegisterPtr(ID2,P2);
350   }
351
352   template <typename T1, typename T2, typename T3>
353   void BatchReadOwnedPtrs(unsigned NumT1Ptrs, T1** Ptrs,
354                           T2*& P2, T3*& P3,
355                           bool A1=true, bool A2=true, bool A3=true) {
356
357     llvm::SmallVector<SerializedPtrID,10> BatchIDVec;
358
359     for (unsigned i = 0; i < NumT1Ptrs; ++i)
360       BatchIDVec.push_back(ReadPtrID());
361
362     SerializedPtrID ID2 = ReadPtrID();
363     SerializedPtrID ID3 = ReadPtrID();
364
365     for (unsigned i = 0; i < NumT1Ptrs; ++i) {
366       SerializedPtrID& PtrID = BatchIDVec[i];
367
368       T1* p = PtrID ? SerializeTrait<T1>::Create(*this) : NULL;
369
370       if (PtrID && A1)
371         RegisterPtr(PtrID,p);
372
373       Ptrs[i] = p;
374     }
375
376     P2 = (ID2) ? SerializeTrait<T2>::Create(*this) : NULL;
377     if (ID2 && A2) RegisterPtr(ID2,P2);
378
379     P3 = (ID3) ? SerializeTrait<T3>::Create(*this) : NULL;
380     if (ID3 && A3) RegisterPtr(ID3,P3);
381   }
382
383   template <typename T1, typename T2, typename T3, typename Arg1>
384   void BatchReadOwnedPtrs(unsigned NumT1Ptrs, T1** Ptrs,
385                           T2*& P2, T3*& P3, Arg1& arg1,
386                           bool A1=true, bool A2=true, bool A3=true) {
387
388     llvm::SmallVector<SerializedPtrID,10> BatchIDVec;
389
390     for (unsigned i = 0; i < NumT1Ptrs; ++i)
391       BatchIDVec.push_back(ReadPtrID());
392
393     SerializedPtrID ID2 = ReadPtrID();
394     SerializedPtrID ID3 = ReadPtrID();
395
396     for (unsigned i = 0; i < NumT1Ptrs; ++i) {
397       SerializedPtrID& PtrID = BatchIDVec[i];
398
399       T1* p = PtrID ? SerializeTrait<T1>::Create(*this, arg1) : NULL;
400
401       if (PtrID && A1)
402         RegisterPtr(PtrID,p);
403
404       Ptrs[i] = p;
405     }
406
407     P2 = (ID2) ? SerializeTrait<T2>::Create(*this, arg1) : NULL;
408     if (ID2 && A2) RegisterPtr(ID2,P2);
409
410     P3 = (ID3) ? SerializeTrait<T3>::Create(*this, arg1) : NULL;
411     if (ID3 && A3) RegisterPtr(ID3,P3);
412   }
413
414   template <typename T>
415   void ReadPtr(T*& PtrRef, bool AllowBackpatch = true) {
416     ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef), AllowBackpatch);
417   }
418
419   template <typename T>
420   void ReadPtr(const T*& PtrRef, bool AllowBackpatch = true) {
421     ReadPtr(const_cast<T*&>(PtrRef), AllowBackpatch);
422   }
423
424
425   template <typename T>
426   void ReadPtr(T*& PtrRef, const SerializedPtrID& PtrID,
427                bool AllowBackpatch = true) {
428     ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef), PtrID, AllowBackpatch);
429   }
430
431   template <typename T>
432   void ReadPtr(const T*& PtrRef, const SerializedPtrID& PtrID,
433                bool AllowBackpatch = true) {
434
435     ReadPtr(const_cast<T*&>(PtrRef), PtrID, AllowBackpatch);
436   }
437
438   template <typename T>
439   T* ReadPtr() { T* x = 0; ReadPtr<T>(x,false); return x; }
440
441   void ReadUIntPtr(uintptr_t& PtrRef, const SerializedPtrID& PtrID,
442                    bool AllowBackpatch = true);
443
444   void ReadUIntPtr(uintptr_t& PtrRef, bool AllowBackpatch = true) {
445     ReadUIntPtr(PtrRef,ReadPtrID(),AllowBackpatch);
446   }
447
448   template <typename T>
449   T& ReadRef() {
450     T* p = reinterpret_cast<T*>(ReadInternalRefPtr());
451     return *p;
452   }
453
454   void RegisterPtr(const SerializedPtrID& PtrID, const void* Ptr);
455
456   void RegisterPtr(const void* Ptr) {
457     RegisterPtr(ReadPtrID(),Ptr);
458   }
459
460   template<typename T>
461   void RegisterRef(const T& x) {
462     RegisterPtr(&x);
463   }
464
465   template<typename T>
466   void RegisterRef(const SerializedPtrID& PtrID, const T& x) {
467     RegisterPtr(PtrID,&x);
468   }
469
470   Location getCurrentBlockLocation();
471   unsigned getCurrentBlockID();
472   unsigned getAbbrevNo();
473
474   bool FinishedBlock(Location BlockLoc);
475   bool JumpTo(const Location& BlockLoc);
476   void Rewind();
477
478   bool AtEnd();
479   bool inRecord();
480   void SkipBlock();
481   bool SkipToBlock(unsigned BlockID);
482
483   unsigned getRecordCode();
484
485   BitstreamCursor &getStream() { return Stream; }
486
487 private:
488   bool AdvanceStream();
489   void ReadRecord();
490
491   uintptr_t ReadInternalRefPtr();
492
493   static inline bool HasFinalPtr(MapTy::value_type& V) {
494     return V.first.hasFinalPtr();
495   }
496
497   static inline uintptr_t GetFinalPtr(MapTy::value_type& V) {
498     return reinterpret_cast<uintptr_t>(V.second.Ptr);
499   }
500
501   static inline BPNode* GetBPNode(MapTy::value_type& V) {
502     return V.second.Head;
503   }
504
505   static inline void SetBPNode(MapTy::value_type& V, BPNode* N) {
506     V.second.Head = N;
507   }
508
509   void SetPtr(MapTy::value_type& V, const void* P) {
510     V.first.MarkFinal();
511     V.second.SetPtr(FreeList,const_cast<void*>(P));
512   }
513 };
514
515 } // end namespace llvm
516
517 #endif