llvm.org GIT mirror llvm / ec8cd06
Added typedef "SerializedPtrID" to represent the pointer handle written to disk instead of just using "unsigned". This gives us more flexibility in changing the definition of the handle later, and is more self-documenting. Added tracking of block stack in the Deserializer. Now clients can query if they are still within a block using the methods GetCurrentBlockLocation() and FinishedBlock(). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43903 91177308-0d34-0410-b5e6-96231b3b80d8 Ted Kremenek 12 years ago
5 changed file(s) with 85 addition(s) and 27 deletion(s). Raw diff Collapse all Expand all
5454 unsigned Raw;
5555
5656 public:
57 BPKey(unsigned PtrId) : Raw(PtrId << 1) { assert (PtrId > 0); }
57 BPKey(SerializedPtrID PtrId) : Raw(PtrId << 1) { assert (PtrId > 0); }
5858 BPKey(unsigned code, unsigned) : Raw(code) {}
5959
6060 void MarkFinal() { Raw |= 0x1; }
6161 bool hasFinalPtr() const { return Raw & 0x1 ? true : false; }
62 unsigned getID() const { return Raw >> 1; }
62 SerializedPtrID getID() const { return Raw >> 1; }
6363
6464 static inline BPKey getEmptyKey() { return BPKey(0,0); }
6565 static inline BPKey getTombstoneKey() { return BPKey(1,0); }
7575 typedef llvm::DenseMap MapTy;
7676
7777 //===----------------------------------------------------------===//
78 // Publicly visible types.
79 //===----------------------------------------------------------===//
80
81 public:
82 typedef uint64_t Location;
83
84 //===----------------------------------------------------------===//
7885 // Internal data members.
7986 //===----------------------------------------------------------===//
80
87
88 private:
8189 BitstreamReader& Stream;
8290 SmallVector Record;
8391 unsigned RecIdx;
8492 BumpPtrAllocator Allocator;
8593 BPNode* FreeList;
86 MapTy BPatchMap;
94 MapTy BPatchMap;
95 llvm::SmallVector BlockLocs;
8796
8897 //===----------------------------------------------------------===//
8998 // Public Interface.
9099 //===----------------------------------------------------------===//
91100
92 public:
101 public:
93102 Deserializer(BitstreamReader& stream);
94103 ~Deserializer();
95104
96105 uint64_t ReadInt();
97106 int64_t ReadSInt();
107 SerializedPtrID ReadPtrID() { return (SerializedPtrID) ReadInt(); }
108
98109
99110 bool ReadBool() {
100111 return ReadInt() ? true : false;
116127
117128 template
118129 inline T* ReadOwnedPtr(bool AutoRegister = true) {
119 unsigned PtrID = ReadInt();
130 SerializedPtrID PtrID = ReadPtrID();
120131
121132 if (!PtrID)
122133 return NULL;
138149 void BatchReadOwnedPtrs(T1*& P1, T2*& P2,
139150 bool A1=true, bool A2=true) {
140151
141 unsigned ID1 = ReadInt();
142 unsigned ID2 = ReadInt();
152 SerializedPtrID ID1 = ReadPtrID();
153 SerializedPtrID ID2 = ReadPtrID();
143154
144155 P1 = (ID1) ? SerializeTrait::Materialize(*this) : NULL;
145156 if (ID1 && A1) RegisterPtr(ID1,P1);
152163 void BatchReadOwnedPtrs(T1*& P1, T2*& P2, T3*& P3,
153164 bool A1=true, bool A2=true, bool A3=true) {
154165
155 unsigned ID1 = ReadInt();
156 unsigned ID2 = ReadInt();
157 unsigned ID3 = ReadInt();
166 SerializedPtrID ID1 = ReadPtrID();
167 SerializedPtrID ID2 = ReadPtrID();
168 SerializedPtrID ID3 = ReadPtrID();
158169
159170 P1 = (ID1) ? SerializeTrait::Materialize(*this) : NULL;
160171 if (ID1 && A1) RegisterPtr(ID1,P1);
169180 template
170181 void BatchReadOwnedPtrs(unsigned NumPtrs, T** Ptrs, bool AutoRegister=true) {
171182 for (unsigned i = 0; i < NumPtrs; ++i)
172 reinterpret_cast<uintptr_t&>(Ptrs[i]) = ReadInt();
183 reinterpret_cast<SerializedPtrID&>(Ptrs[i]) = ReadPtrID();
173184
174185 for (unsigned i = 0; i < NumPtrs; ++i) {
175 unsigned PtrID = reinterpret_cast>(Ptrs[i]);
186 SerializedPtrID PtrID = reinterpret_cast>(Ptrs[i]);
176187 T* p = PtrID ? SerializeTrait::Materialize(*this) : NULL;
177188
178189 if (PtrID && AutoRegister)
203214 return *p;
204215 }
205216
206 void RegisterPtr(unsigned PtrId, const void* Ptr);
217 void RegisterPtr(SerializedPtrID PtrId, const void* Ptr);
207218
208219 void RegisterPtr(const void* Ptr) {
209 RegisterPtr(ReadInt(),Ptr);
210 }
220 RegisterPtr(ReadPtrID(),Ptr);
221 }
222
223 template
224 void RegisterRef(const T& x) {
225 RegisterPtr(&x);
226 }
227
228 template
229 void RegisterRef(SerializedPtrID PtrID, const T& x) {
230 RegisterPtr(PtrID,&x);
231 }
232
233 Location GetCurrentBlockLocation();
234 bool FinishedBlock(Location BlockLoc);
211235
212236 bool AtEnd();
213
214237 bool inRecord();
238
215239 private:
216240 void ReadRecord();
217241 uintptr_t ReadInternalRefPtr();
1919 class Deserializer;
2020 template struct SerializeTrait;
2121
22 typedef unsigned SerializedPtrID;
23
2224 } // end namespace llvm
2325
2426 #endif
9292 for (unsigned i = 0; i < NumPtrs; ++i)
9393 if (Ptrs[i]) SerializeTrait::Emit(*this,*Ptrs[i]);
9494 }
95
96 bool isRegistered(const void* p) const;
9597
96 void FlushRecord() { if (inRecord()) EmitRecord(); }
97
98 void FlushRecord() { if (inRecord()) EmitRecord(); }
9899 void EnterBlock(unsigned BlockID = 8, unsigned CodeLen = 3);
99100 void ExitBlock();
100101
101102 private:
102103 void EmitRecord();
103104 inline bool inRecord() { return Record.size() > 0; }
104 unsigned getPtrId(const void* ptr);
105 SerializedPtrID getPtrId(const void* ptr);
105106 };
106107
107108 } // end namespace llvm
6262 Code = Stream.ReadCode();
6363
6464 if (Code == bitc::ENTER_SUBBLOCK) {
65 // No known subblocks, always skip them.
65 BlockLocs.push_back(Stream.GetCurrentBitNo());
6666 unsigned id = Stream.ReadSubBlockID();
6767 Stream.EnterSubBlock(id);
6868 continue;
6969 }
7070
71 if (Code == bitc::END_BLOCK) {
71 if (Code == bitc::END_BLOCK) {
7272 bool x = Stream.ReadBlockEnd();
7373 assert (!x && "Error at block end.");
74 BlockLocs.pop_back();
7475 continue;
7576 }
7677
8586 assert (Record.size() == 0);
8687 Stream.ReadRecord(Code,Record);
8788 assert (Record.size() > 0 || Stream.AtEndOfStream());
89 }
90
91 Deserializer::Location Deserializer::GetCurrentBlockLocation() {
92 if (!inRecord())
93 ReadRecord();
94
95 assert (!BlockLocs.empty());
96 return BlockLocs.back();
97 }
98
99 bool Deserializer::FinishedBlock(Location BlockLoc) {
100 if (!inRecord())
101 ReadRecord();
102
103 for (llvm::SmallVector::reverse_iterator
104 I=BlockLocs.rbegin(), E=BlockLocs.rend(); I!=E; ++I)
105 if (*I == BlockLoc)
106 return false;
107
108 return true;
88109 }
89110
90111 bool Deserializer::AtEnd() {
158179 }
159180
160181 void Deserializer::ReadUIntPtr(uintptr_t& PtrRef, bool AllowBackpatch) {
161 unsigned PtrId = ReadInt();
182 SerializedPtrID PtrId = ReadPtrID();
162183
163184 if (PtrId == 0) {
164185 PtrRef = 0;
193214 }
194215
195216 uintptr_t Deserializer::ReadInternalRefPtr() {
196 unsigned PtrId = ReadInt();
217 SerializedPtrID PtrId = ReadPtrID();
197218
198219 assert (PtrId != 0 && "References cannot refer the NULL address.");
199220
1212
1313 #include "llvm/Bitcode/Serialize.h"
1414 #include "string.h"
15
16 #ifdef DEBUG_BACKPATCH
17 #include "llvm/Support/Streams.h"
18 #endif
1519
1620 using namespace llvm;
1721
6670 Record.push_back(*s);
6771 ++s;
6872 }
69
70 EmitRecord();
7173 }
7274
7375 void Serializer::EmitCStr(const char* s) {
7476 EmitCStr(s,s+strlen(s));
7577 }
7678
77 unsigned Serializer::getPtrId(const void* ptr) {
79 SerializedPtrID Serializer::getPtrId(const void* ptr) {
7880 if (!ptr)
7981 return 0;
8082
8284
8385 if (I == PtrMap.end()) {
8486 unsigned id = PtrMap.size()+1;
87 #ifdef DEBUG_BACKPATCH
88 llvm::cerr << "Registered PTR: " << ptr << " => " << id << "\n";
89 #endif
8590 PtrMap[ptr] = id;
8691 return id;
8792 }
8893 else return I->second;
94 }
95
96 bool Serializer::isRegistered(const void* ptr) const {
97 MapTy::const_iterator I = PtrMap.find(ptr);
98 return I != PtrMap.end();
8999 }
90100
91101