1 //=- Deserialize.h - Generic Object Deserialization from Bitcode --*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file defines the interface for generic object deserialization from
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_BITCODE_SERIALIZE_INPUT
16 #define LLVM_BITCODE_SERIALIZE_INPUT
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"
30 //===----------------------------------------------------------===//
31 // Internal type definitions.
32 //===----------------------------------------------------------===//
35 static inline unsigned getEmptyKey() { return ~((unsigned) 0x0); }
36 static inline unsigned getTombstoneKey() { return getEmptyKey()-1; }
37 static inline unsigned getHashValue(unsigned X) { return X; }
38 static inline bool isEqual(unsigned X, unsigned Y) { return X == Y; }
39 static inline bool isPod() { return true; }
45 BPNode(BPNode* n, uintptr_t& pref)
46 : Next(n), PtrRef(pref) {
55 BPatchEntry() : Ptr(0x1) {}
57 BPatchEntry(void* P) : Ptr(reinterpret_cast<uintptr_t>(P)) {}
59 bool hasFinalPtr() const { return Ptr & 0x1 ? false : true; }
60 void setFinalPtr(BPNode*& FreeList, void* P);
62 BPNode* getBPNode() const {
63 assert (!hasFinalPtr());
64 return reinterpret_cast<BPNode*>(Ptr & ~0x1);
67 void setBPNode(BPNode* N) {
68 assert (!hasFinalPtr());
69 Ptr = reinterpret_cast<uintptr_t>(N) | 0x1;
72 uintptr_t getFinalPtr() const {
73 assert (!(Ptr & 0x1) && "Backpatch pointer not yet deserialized.");
77 static inline bool isPod() { return true; }
80 typedef llvm::DenseMap<unsigned,BPatchEntry,PtrIdInfo,BPatchEntry> MapTy;
82 //===----------------------------------------------------------===//
83 // Internal data members.
84 //===----------------------------------------------------------===//
86 BitstreamReader& Stream;
87 SmallVector<uint64_t,10> Record;
89 BumpPtrAllocator Allocator;
93 //===----------------------------------------------------------===//
95 //===----------------------------------------------------------===//
98 Deserializer(BitstreamReader& stream);
103 return ReadInt() ? true : false;
106 template <typename T>
107 inline T& Read(T& X) {
108 SerializeTrait<T>::Read(*this,X);
112 template <typename T>
114 return SerializeTrait<T>::ReadVal(*this);
117 template <typename T>
118 inline T* Materialize() {
119 return SerializeTrait<T>::Materialize(*this);
122 char* ReadCStr(char* cstr = NULL, unsigned MaxLen=0, bool isNullTerm=true);
123 void ReadCStr(std::vector<char>& buff, bool isNullTerm=false);
125 template <typename T>
126 inline T* ReadOwnedPtr() {
127 unsigned PtrId = ReadInt();
132 T* x = SerializeTrait<T>::Materialize(*this);
133 RegisterPtr(PtrId,x);
137 template <typename T>
138 void ReadPtr(T*& PtrRef) {
139 ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef));
142 template <typename T>
143 void ReadPtr(const T*& PtrRef) {
144 ReadPtr(const_cast<T*&>(PtrRef));
147 void ReadUIntPtr(uintptr_t& PtrRef);
149 template <typename T>
151 T* p = reinterpret_cast<T*>(ReadInternalRefPtr());
156 void RegisterPtr(unsigned PtrId, void* Ptr);
161 uintptr_t ReadInternalRefPtr();
164 } // end namespace llvm