Augmented ReadPtr and ReadOwnedPtr to control whether or not a pointer is allowed...
[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   bool ReadBool() {
99     return ReadInt() ? true : false;
100   }
101
102   template <typename T>
103   inline T& Read(T& X) {
104     SerializeTrait<T>::Read(*this,X);
105     return X;
106   }
107
108   template <typename T>
109   inline T* Materialize() {
110     return SerializeTrait<T>::Materialize(*this);
111   }
112   
113   char* ReadCStr(char* cstr = NULL, unsigned MaxLen=0, bool isNullTerm=true);
114   void ReadCStr(std::vector<char>& buff, bool isNullTerm=false);
115
116   template <typename T>
117   inline T* ReadOwnedPtr(bool AutoRegister = true) {
118     unsigned PtrId = ReadInt();
119
120     if (PtrId == 0)
121       return NULL;
122     
123     T* x = SerializeTrait<T>::Materialize(*this);
124     
125     if (AutoRegister)
126       RegisterPtr(PtrId,x);
127     
128     return x;
129   }
130   
131   template <typename T>
132   inline void ReadOwnedPtr(T*& Ptr, bool AutoRegister = true) {
133     Ptr = ReadOwnedPtr<T>(AutoRegister);
134   }
135   
136   template <typename T>
137   void ReadPtr(T*& PtrRef, bool AllowBackpatch = true) {
138     ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef), AllowBackpatch);
139   }
140   
141   template <typename T>
142   void ReadPtr(const T*& PtrRef, bool AllowBackpatch = true) {
143     ReadPtr(const_cast<T*&>(PtrRef), AllowBackpatch);
144   }            
145
146   void ReadUIntPtr(uintptr_t& PtrRef, bool AllowBackpatch = true);
147   
148   template <typename T>
149   T& ReadRef() {
150     T* p = reinterpret_cast<T*>(ReadInternalRefPtr());
151     return *p;
152   }
153
154   void RegisterPtr(unsigned PtrId, const void* Ptr);
155   
156   void RegisterPtr(const void* Ptr) {
157     RegisterPtr(ReadInt(),Ptr);
158   }
159   
160   bool AtEnd();
161
162   bool inRecord();
163 private:
164   void ReadRecord();  
165   uintptr_t ReadInternalRefPtr();
166   
167   static inline bool HasFinalPtr(MapTy::value_type& V) {
168     return V.first.hasFinalPtr();
169   }
170   
171   static inline uintptr_t GetFinalPtr(MapTy::value_type& V) {
172     return reinterpret_cast<uintptr_t>(V.second.Ptr);
173   }
174   
175   static inline BPNode* GetBPNode(MapTy::value_type& V) {
176     return V.second.Head;
177   }
178     
179   static inline void SetBPNode(MapTy::value_type& V, BPNode* N) {
180     V.second.Head = N;
181   }
182   
183   void SetPtr(MapTy::value_type& V, const void* P) {
184     V.first.MarkFinal();
185     V.second.SetPtr(FreeList,const_cast<void*>(P));
186   }
187 };
188     
189 } // end namespace llvm
190
191 #endif